1 // (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 
13 #include "biosvar.h" // GET_GLOBALFLAT
14 #include "block.h" // struct drive_s
15 #include "blockcmd.h" // scsi_drive_setup
16 #include "config.h" // CONFIG_*
17 #include "byteorder.h" // cpu_to_*
18 #include "fw/paravirt.h" // runningOnQEMU
19 #include "malloc.h" // free
20 #include "output.h" // dprintf
21 #include "pcidevice.h" // foreachpci
22 #include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK
23 #include "pci_regs.h" // PCI_VENDOR_ID
24 #include "stacks.h" // run_thread
25 #include "std/disk.h" // DISK_RET_SUCCESS
26 #include "string.h" // memset
27 #include "util.h" // usleep
28 
29 #define LSI_REG_DSTAT     0x0c
30 #define LSI_REG_ISTAT0    0x14
31 #define LSI_REG_DSP0      0x2c
32 #define LSI_REG_DSP1      0x2d
33 #define LSI_REG_DSP2      0x2e
34 #define LSI_REG_DSP3      0x2f
35 #define LSI_REG_SIST0     0x42
36 #define LSI_REG_SIST1     0x43
37 
38 #define LSI_ISTAT0_DIP    0x01
39 #define LSI_ISTAT0_SIP    0x02
40 #define LSI_ISTAT0_INTF   0x04
41 #define LSI_ISTAT0_CON    0x08
42 #define LSI_ISTAT0_SEM    0x10
43 #define LSI_ISTAT0_SIGP   0x20
44 #define LSI_ISTAT0_SRST   0x40
45 #define LSI_ISTAT0_ABRT   0x80
46 
47 struct lsi_lun_s {
48     struct drive_s drive;
49     struct pci_device *pci;
50     u32 iobase;
51     u8 target;
52     u8 lun;
53 };
54 
55 int
lsi_scsi_process_op(struct disk_op_s * op)56 lsi_scsi_process_op(struct disk_op_s *op)
57 {
58     if (!CONFIG_LSI_SCSI)
59         return DISK_RET_EBADTRACK;
60     struct lsi_lun_s *llun_gf =
61         container_of(op->drive_fl, struct lsi_lun_s, drive);
62     u16 target = GET_GLOBALFLAT(llun_gf->target);
63     u16 lun = GET_GLOBALFLAT(llun_gf->lun);
64     u8 cdbcmd[16];
65     int blocksize = scsi_fill_cmd(op, cdbcmd, sizeof(cdbcmd));
66     if (blocksize < 0)
67         return default_process_op(op);
68     u32 iobase = GET_GLOBALFLAT(llun_gf->iobase);
69     u32 dma = ((scsi_is_read(op) ? 0x01000000 : 0x00000000) |
70                (op->count * blocksize));
71     u8 msgout[] = {
72         0x80 | lun,                 // select lun
73         0x08,
74     };
75     u8 status = 0xff;
76     u8 msgin_tmp[2];
77     u8 msgin = 0xff;
78 
79     u32 script[] = {
80         /* select target, send scsi command */
81         0x40000000 | target << 16,  // select target
82         0x00000000,
83         0x06000001,                 // msgout
84         (u32)MAKE_FLATPTR(GET_SEG(SS), &msgout),
85         0x02000010,                 // scsi command
86         (u32)MAKE_FLATPTR(GET_SEG(SS), cdbcmd),
87 
88         /* handle disconnect */
89         0x87820000,                 // phase == msgin ?
90         0x00000018,
91         0x07000002,                 // msgin
92         (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp),
93         0x50000000,                 // re-select
94         0x00000000,
95         0x07000002,                 // msgin
96         (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp),
97 
98         /* dma data, get status, raise irq */
99         dma,                        // dma data
100         (u32)op->buf_fl,
101         0x03000001,                 // status
102         (u32)MAKE_FLATPTR(GET_SEG(SS), &status),
103         0x07000001,                 // msgin
104         (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin),
105         0x98080000,                 // dma irq
106         0x00000000,
107     };
108     u32 dsp = (u32)MAKE_FLATPTR(GET_SEG(SS), &script);
109 
110     /* convert to little endian for PCI */
111     convert_to_le32(script, sizeof(script));
112 
113     outb(dsp         & 0xff, iobase + LSI_REG_DSP0);
114     outb((dsp >>  8) & 0xff, iobase + LSI_REG_DSP1);
115     outb((dsp >> 16) & 0xff, iobase + LSI_REG_DSP2);
116     outb((dsp >> 24) & 0xff, iobase + LSI_REG_DSP3);
117 
118     for (;;) {
119         u8 dstat = inb(iobase + LSI_REG_DSTAT);
120         u8 sist0 = inb(iobase + LSI_REG_SIST0);
121         u8 sist1 = inb(iobase + LSI_REG_SIST1);
122         if (sist0 || sist1) {
123             goto fail;
124         }
125         if (dstat & 0x04) {
126             break;
127         }
128         usleep(5);
129     }
130 
131     if (msgin == 0 && status == 0) {
132         return DISK_RET_SUCCESS;
133     }
134 
135 fail:
136     return DISK_RET_EBADTRACK;
137 }
138 
139 static void
lsi_scsi_init_lun(struct lsi_lun_s * llun,struct pci_device * pci,u32 iobase,u8 target,u8 lun)140 lsi_scsi_init_lun(struct lsi_lun_s *llun, struct pci_device *pci, u32 iobase,
141                   u8 target, u8 lun)
142 {
143     memset(llun, 0, sizeof(*llun));
144     llun->drive.type = DTYPE_LSI_SCSI;
145     llun->drive.cntl_id = pci->bdf;
146     llun->pci = pci;
147     llun->target = target;
148     llun->lun = lun;
149     llun->iobase = iobase;
150 }
151 
152 static int
lsi_scsi_add_lun(u32 lun,struct drive_s * tmpl_drv)153 lsi_scsi_add_lun(u32 lun, struct drive_s *tmpl_drv)
154 {
155     struct lsi_lun_s *tmpl_llun =
156         container_of(tmpl_drv, struct lsi_lun_s, drive);
157     struct lsi_lun_s *llun = malloc_fseg(sizeof(*llun));
158     if (!llun) {
159         warn_noalloc();
160         return -1;
161     }
162     lsi_scsi_init_lun(llun, tmpl_llun->pci, tmpl_llun->iobase,
163                       tmpl_llun->target, lun);
164 
165     boot_lchs_find_scsi_device(llun->pci, llun->target, llun->lun,
166                                &(llun->drive.lchs));
167     char *name = znprintf(MAXDESCSIZE, "lsi %pP %d:%d",
168                           llun->pci, llun->target, llun->lun);
169     int prio = bootprio_find_scsi_device(llun->pci, llun->target, llun->lun);
170     int ret = scsi_drive_setup(&llun->drive, name, prio, llun->target, llun->lun);
171     free(name);
172     if (ret)
173         goto fail;
174     return 0;
175 
176 fail:
177     free(llun);
178     return -1;
179 }
180 
181 static void
lsi_scsi_scan_target(struct pci_device * pci,u32 iobase,u8 target)182 lsi_scsi_scan_target(struct pci_device *pci, u32 iobase, u8 target)
183 {
184     struct lsi_lun_s llun0;
185 
186     lsi_scsi_init_lun(&llun0, pci, iobase, target, 0);
187 
188     if (scsi_rep_luns_scan(&llun0.drive, lsi_scsi_add_lun) < 0)
189         scsi_sequential_scan(&llun0.drive, 8, lsi_scsi_add_lun);
190 }
191 
192 static void
init_lsi_scsi(void * data)193 init_lsi_scsi(void *data)
194 {
195     struct pci_device *pci = data;
196     u32 iobase = pci_enable_iobar(pci, PCI_BASE_ADDRESS_0);
197     if (!iobase)
198         return;
199     pci_enable_busmaster(pci);
200 
201     dprintf(1, "found lsi53c895a at %pP, io @ %x\n", pci, iobase);
202 
203     // reset
204     outb(LSI_ISTAT0_SRST, iobase + LSI_REG_ISTAT0);
205 
206     int i;
207     for (i = 0; i < 7; i++)
208         lsi_scsi_scan_target(pci, iobase, i);
209 }
210 
211 void
lsi_scsi_setup(void)212 lsi_scsi_setup(void)
213 {
214     ASSERT32FLAT();
215     if (!CONFIG_LSI_SCSI || !runningOnQEMU())
216         return;
217 
218     dprintf(3, "init lsi53c895a\n");
219 
220     struct pci_device *pci;
221     foreachpci(pci) {
222         if (pci->vendor != PCI_VENDOR_ID_LSI_LOGIC
223             || pci->device != PCI_DEVICE_ID_LSI_53C895A)
224             continue;
225         run_thread(init_lsi_scsi, pci);
226     }
227 }
228