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