blob: b1d6bbf4b90751ccd484d0a50e21eb8f77873dd9 [file] [log] [blame]
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +02001// (qemu-emulated) lsi53c895a boot support.
2//
3// Copyright (C) 2012 Red Hat Inc.
4//
5// Authors:
6// Gerd Hoffmann <kraxel@redhat.com>
7//
8// based on virtio-scsi.c which is written by:
9// Paolo Bonzini <pbonzini@redhat.com>
10//
11// This file may be distributed under the terms of the GNU LGPLv3 license.
12
Kevin O'Connor1902c942013-10-26 11:48:06 -040013#include "biosvar.h" // GET_GLOBALFLAT
Kevin O'Connor135f3f62013-09-14 23:57:26 -040014#include "block.h" // struct drive_s
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040015#include "blockcmd.h" // scsi_drive_setup
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "config.h" // CONFIG_*
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040017#include "fw/paravirt.h" // runningOnQEMU
18#include "malloc.h" // free
19#include "output.h" // dprintf
20#include "pci.h" // foreachpci
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +020021#include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK
22#include "pci_regs.h" // PCI_VENDOR_ID
Kevin O'Connor135f3f62013-09-14 23:57:26 -040023#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040024#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040025#include "util.h" // usleep
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +020026
27#define LSI_REG_DSTAT 0x0c
28#define LSI_REG_ISTAT0 0x14
29#define LSI_REG_DSP0 0x2c
30#define LSI_REG_DSP1 0x2d
31#define LSI_REG_DSP2 0x2e
32#define LSI_REG_DSP3 0x2f
33#define LSI_REG_SIST0 0x42
34#define LSI_REG_SIST1 0x43
35
36#define LSI_ISTAT0_DIP 0x01
37#define LSI_ISTAT0_SIP 0x02
38#define LSI_ISTAT0_INTF 0x04
39#define LSI_ISTAT0_CON 0x08
40#define LSI_ISTAT0_SEM 0x10
41#define LSI_ISTAT0_SIGP 0x20
42#define LSI_ISTAT0_SRST 0x40
43#define LSI_ISTAT0_ABRT 0x80
44
45struct lsi_lun_s {
46 struct drive_s drive;
47 struct pci_device *pci;
48 u32 iobase;
49 u8 target;
50 u8 lun;
51};
52
53static int
Kevin O'Connor1902c942013-10-26 11:48:06 -040054lsi_scsi_cmd(struct lsi_lun_s *llun_gf, struct disk_op_s *op,
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +020055 void *cdbcmd, u16 target, u16 lun, u16 blocksize)
56{
Kevin O'Connor1902c942013-10-26 11:48:06 -040057 u32 iobase = GET_GLOBALFLAT(llun_gf->iobase);
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +020058 u32 dma = ((cdb_is_read(cdbcmd, blocksize) ? 0x01000000 : 0x00000000) |
59 (op->count * blocksize));
60 u8 msgout[] = {
61 0x80 | lun, // select lun
62 0x08,
63 };
64 u8 status = 0xff;
65 u8 msgin_tmp[2];
66 u8 msgin = 0xff;
67
68 u32 script[] = {
69 /* select target, send scsi command */
70 0x40000000 | target << 16, // select target
71 0x00000000,
72 0x06000001, // msgout
73 (u32)MAKE_FLATPTR(GET_SEG(SS), &msgout),
74 0x02000010, // scsi command
75 (u32)MAKE_FLATPTR(GET_SEG(SS), cdbcmd),
76
77 /* handle disconnect */
78 0x87820000, // phase == msgin ?
79 0x00000018,
80 0x07000002, // msgin
81 (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp),
82 0x50000000, // re-select
83 0x00000000,
84 0x07000002, // msgin
85 (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp),
86
87 /* dma data, get status, raise irq */
88 dma, // dma data
89 (u32)op->buf_fl,
90 0x03000001, // status
91 (u32)MAKE_FLATPTR(GET_SEG(SS), &status),
92 0x07000001, // msgin
93 (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin),
94 0x98080000, // dma irq
95 0x00000000,
96 };
97 u32 dsp = (u32)MAKE_FLATPTR(GET_SEG(SS), &script);
98
99 outb(dsp & 0xff, iobase + LSI_REG_DSP0);
100 outb((dsp >> 8) & 0xff, iobase + LSI_REG_DSP1);
101 outb((dsp >> 16) & 0xff, iobase + LSI_REG_DSP2);
102 outb((dsp >> 24) & 0xff, iobase + LSI_REG_DSP3);
103
104 for (;;) {
105 u8 dstat = inb(iobase + LSI_REG_DSTAT);
106 u8 sist0 = inb(iobase + LSI_REG_SIST0);
107 u8 sist1 = inb(iobase + LSI_REG_SIST1);
108 if (sist0 || sist1) {
109 goto fail;
110 }
111 if (dstat & 0x04) {
112 break;
113 }
114 usleep(5);
115 }
116
117 if (msgin == 0 && status == 0) {
118 return DISK_RET_SUCCESS;
119 }
120
121fail:
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200122 return DISK_RET_EBADTRACK;
123}
124
125int
126lsi_scsi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
127{
128 if (!CONFIG_LSI_SCSI)
129 return DISK_RET_EBADTRACK;
130
Kevin O'Connor1902c942013-10-26 11:48:06 -0400131 struct lsi_lun_s *llun_gf =
132 container_of(op->drive_gf, struct lsi_lun_s, drive);
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200133
Kevin O'Connor1902c942013-10-26 11:48:06 -0400134 return lsi_scsi_cmd(llun_gf, op, cdbcmd,
135 GET_GLOBALFLAT(llun_gf->target),
136 GET_GLOBALFLAT(llun_gf->lun),
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200137 blocksize);
138}
139
140static int
141lsi_scsi_add_lun(struct pci_device *pci, u32 iobase, u8 target, u8 lun)
142{
143 struct lsi_lun_s *llun = malloc_fseg(sizeof(*llun));
144 if (!llun) {
145 warn_noalloc();
146 return -1;
147 }
148 memset(llun, 0, sizeof(*llun));
149 llun->drive.type = DTYPE_LSI_SCSI;
150 llun->drive.cntl_id = pci->bdf;
151 llun->pci = pci;
152 llun->target = target;
153 llun->lun = lun;
154 llun->iobase = iobase;
155
156 char *name = znprintf(16, "lsi %02x:%02x.%x %d:%d",
157 pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf),
158 pci_bdf_to_fn(pci->bdf), target, lun);
159 int prio = bootprio_find_scsi_device(pci, target, lun);
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500160 int ret = scsi_drive_setup(&llun->drive, name, prio);
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200161 free(name);
162 if (ret)
163 goto fail;
164 return 0;
165
166fail:
167 free(llun);
168 return -1;
169}
170
171static void
172lsi_scsi_scan_target(struct pci_device *pci, u32 iobase, u8 target)
173{
174 /* TODO: send REPORT LUNS. For now, only LUN 0 is recognized. */
175 lsi_scsi_add_lun(pci, iobase, target, 0);
176}
177
178static void
179init_lsi_scsi(struct pci_device *pci)
180{
181 u16 bdf = pci->bdf;
182 u32 iobase = pci_config_readl(pci->bdf, PCI_BASE_ADDRESS_0)
183 & PCI_BASE_ADDRESS_IO_MASK;
184
Gerd Hoffmann7d052572012-11-20 12:02:52 +0100185 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
186
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200187 dprintf(1, "found lsi53c895a at %02x:%02x.%x, io @ %x\n",
188 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
189 pci_bdf_to_fn(bdf), iobase);
190
191 // reset
192 outb(LSI_ISTAT0_SRST, iobase + LSI_REG_ISTAT0);
193
194 int i;
195 for (i = 0; i < 7; i++)
196 lsi_scsi_scan_target(pci, iobase, i);
197
198 return;
199}
200
201void
202lsi_scsi_setup(void)
203{
204 ASSERT32FLAT();
Kevin O'Connor897fb112013-02-07 23:32:48 -0500205 if (!CONFIG_LSI_SCSI || !runningOnQEMU())
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200206 return;
207
208 dprintf(3, "init lsi53c895a\n");
209
210 struct pci_device *pci;
211 foreachpci(pci) {
212 if (pci->vendor != PCI_VENDOR_ID_LSI_LOGIC
213 || pci->device != PCI_DEVICE_ID_LSI_53C895A)
214 continue;
215 init_lsi_scsi(pci);
216 }
217}