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