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 |
| 20 | #include "pci.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 | 135f3f6 | 2013-09-14 23:57:26 -0400 | [diff] [blame] | 23 | #include "std/disk.h" // DISK_RET_SUCCESS |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 24 | #include "string.h" // memset |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 25 | #include "util.h" // usleep |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 26 | |
| 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 | |
| 45 | struct lsi_lun_s { |
| 46 | struct drive_s drive; |
| 47 | struct pci_device *pci; |
| 48 | u32 iobase; |
| 49 | u8 target; |
| 50 | u8 lun; |
| 51 | }; |
| 52 | |
Kevin O'Connor | 7d3ca01 | 2015-07-07 11:51:08 -0400 | [diff] [blame] | 53 | int |
| 54 | lsi_scsi_process_op(struct disk_op_s *op) |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 55 | { |
Kevin O'Connor | 7d3ca01 | 2015-07-07 11:51:08 -0400 | [diff] [blame] | 56 | if (!CONFIG_LSI_SCSI) |
| 57 | return DISK_RET_EBADTRACK; |
| 58 | struct lsi_lun_s *llun_gf = |
| 59 | container_of(op->drive_gf, struct lsi_lun_s, drive); |
| 60 | u16 target = GET_GLOBALFLAT(llun_gf->target); |
| 61 | u16 lun = GET_GLOBALFLAT(llun_gf->lun); |
| 62 | u8 cdbcmd[16]; |
| 63 | int blocksize = scsi_fill_cmd(op, cdbcmd, sizeof(cdbcmd)); |
| 64 | if (blocksize < 0) |
| 65 | return default_process_op(op); |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 66 | u32 iobase = GET_GLOBALFLAT(llun_gf->iobase); |
Kevin O'Connor | 5dcd1ee | 2015-07-07 14:43:01 -0400 | [diff] [blame] | 67 | u32 dma = ((scsi_is_read(op) ? 0x01000000 : 0x00000000) | |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 68 | (op->count * blocksize)); |
| 69 | u8 msgout[] = { |
| 70 | 0x80 | lun, // select lun |
| 71 | 0x08, |
| 72 | }; |
| 73 | u8 status = 0xff; |
| 74 | u8 msgin_tmp[2]; |
| 75 | u8 msgin = 0xff; |
| 76 | |
| 77 | u32 script[] = { |
| 78 | /* select target, send scsi command */ |
| 79 | 0x40000000 | target << 16, // select target |
| 80 | 0x00000000, |
| 81 | 0x06000001, // msgout |
| 82 | (u32)MAKE_FLATPTR(GET_SEG(SS), &msgout), |
| 83 | 0x02000010, // scsi command |
| 84 | (u32)MAKE_FLATPTR(GET_SEG(SS), cdbcmd), |
| 85 | |
| 86 | /* handle disconnect */ |
| 87 | 0x87820000, // phase == msgin ? |
| 88 | 0x00000018, |
| 89 | 0x07000002, // msgin |
| 90 | (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp), |
| 91 | 0x50000000, // re-select |
| 92 | 0x00000000, |
| 93 | 0x07000002, // msgin |
| 94 | (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp), |
| 95 | |
| 96 | /* dma data, get status, raise irq */ |
| 97 | dma, // dma data |
| 98 | (u32)op->buf_fl, |
| 99 | 0x03000001, // status |
| 100 | (u32)MAKE_FLATPTR(GET_SEG(SS), &status), |
| 101 | 0x07000001, // msgin |
| 102 | (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin), |
| 103 | 0x98080000, // dma irq |
| 104 | 0x00000000, |
| 105 | }; |
| 106 | u32 dsp = (u32)MAKE_FLATPTR(GET_SEG(SS), &script); |
| 107 | |
| 108 | outb(dsp & 0xff, iobase + LSI_REG_DSP0); |
| 109 | outb((dsp >> 8) & 0xff, iobase + LSI_REG_DSP1); |
| 110 | outb((dsp >> 16) & 0xff, iobase + LSI_REG_DSP2); |
| 111 | outb((dsp >> 24) & 0xff, iobase + LSI_REG_DSP3); |
| 112 | |
| 113 | for (;;) { |
| 114 | u8 dstat = inb(iobase + LSI_REG_DSTAT); |
| 115 | u8 sist0 = inb(iobase + LSI_REG_SIST0); |
| 116 | u8 sist1 = inb(iobase + LSI_REG_SIST1); |
| 117 | if (sist0 || sist1) { |
| 118 | goto fail; |
| 119 | } |
| 120 | if (dstat & 0x04) { |
| 121 | break; |
| 122 | } |
| 123 | usleep(5); |
| 124 | } |
| 125 | |
| 126 | if (msgin == 0 && status == 0) { |
| 127 | return DISK_RET_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | fail: |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 131 | return DISK_RET_EBADTRACK; |
| 132 | } |
| 133 | |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 134 | static int |
| 135 | lsi_scsi_add_lun(struct pci_device *pci, u32 iobase, u8 target, u8 lun) |
| 136 | { |
| 137 | struct lsi_lun_s *llun = malloc_fseg(sizeof(*llun)); |
| 138 | if (!llun) { |
| 139 | warn_noalloc(); |
| 140 | return -1; |
| 141 | } |
| 142 | memset(llun, 0, sizeof(*llun)); |
| 143 | llun->drive.type = DTYPE_LSI_SCSI; |
| 144 | llun->drive.cntl_id = pci->bdf; |
| 145 | llun->pci = pci; |
| 146 | llun->target = target; |
| 147 | llun->lun = lun; |
| 148 | llun->iobase = iobase; |
| 149 | |
| 150 | char *name = znprintf(16, "lsi %02x:%02x.%x %d:%d", |
| 151 | pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf), |
| 152 | pci_bdf_to_fn(pci->bdf), target, lun); |
| 153 | int prio = bootprio_find_scsi_device(pci, target, lun); |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 154 | int ret = scsi_drive_setup(&llun->drive, name, prio); |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 155 | free(name); |
| 156 | if (ret) |
| 157 | goto fail; |
| 158 | return 0; |
| 159 | |
| 160 | fail: |
| 161 | free(llun); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | lsi_scsi_scan_target(struct pci_device *pci, u32 iobase, u8 target) |
| 167 | { |
| 168 | /* TODO: send REPORT LUNS. For now, only LUN 0 is recognized. */ |
| 169 | lsi_scsi_add_lun(pci, iobase, target, 0); |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | init_lsi_scsi(struct pci_device *pci) |
| 174 | { |
Kevin O'Connor | 5f7f341 | 2016-02-02 22:18:54 -0500 | [diff] [blame^] | 175 | u32 iobase = pci_enable_iobar(pci, PCI_BASE_ADDRESS_0); |
| 176 | if (!iobase) |
| 177 | return; |
| 178 | pci_enable_busmaster(pci); |
Gerd Hoffmann | 7d05257 | 2012-11-20 12:02:52 +0100 | [diff] [blame] | 179 | |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 180 | dprintf(1, "found lsi53c895a at %02x:%02x.%x, io @ %x\n", |
Kevin O'Connor | 5f7f341 | 2016-02-02 22:18:54 -0500 | [diff] [blame^] | 181 | pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf), |
| 182 | pci_bdf_to_fn(pci->bdf), iobase); |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 183 | |
| 184 | // reset |
| 185 | outb(LSI_ISTAT0_SRST, iobase + LSI_REG_ISTAT0); |
| 186 | |
| 187 | int i; |
| 188 | for (i = 0; i < 7; i++) |
| 189 | lsi_scsi_scan_target(pci, iobase, i); |
| 190 | |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | lsi_scsi_setup(void) |
| 196 | { |
| 197 | ASSERT32FLAT(); |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 198 | if (!CONFIG_LSI_SCSI || !runningOnQEMU()) |
Gerd Hoffmann | 9d6bac1 | 2012-07-20 10:59:25 +0200 | [diff] [blame] | 199 | return; |
| 200 | |
| 201 | dprintf(3, "init lsi53c895a\n"); |
| 202 | |
| 203 | struct pci_device *pci; |
| 204 | foreachpci(pci) { |
| 205 | if (pci->vendor != PCI_VENDOR_ID_LSI_LOGIC |
| 206 | || pci->device != PCI_DEVICE_ID_LSI_53C895A) |
| 207 | continue; |
| 208 | init_lsi_scsi(pci); |
| 209 | } |
| 210 | } |