1 // Virtio SCSI boot support.
3 // Copyright (C) 2012 Red Hat Inc.
6 // Paolo Bonzini <pbonzini@redhat.com>
8 // This file may be distributed under the terms of the GNU LGPLv3 license.
10 #include "biosvar.h" // GET_GLOBALFLAT
11 #include "block.h" // struct drive_s
12 #include "blockcmd.h" // scsi_drive_setup
13 #include "config.h" // CONFIG_*
14 #include "malloc.h" // free
15 #include "output.h" // dprintf
16 #include "pci.h" // foreachpci
17 #include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK
18 #include "pci_regs.h" // PCI_VENDOR_ID
19 #include "std/disk.h" // DISK_RET_SUCCESS
20 #include "string.h" // memset
21 #include "util.h" // usleep
22 #include "virtio-pci.h"
23 #include "virtio-ring.h"
24 #include "virtio-scsi.h"
28 struct pci_device *pci;
29 struct vring_virtqueue *vq;
36 virtio_scsi_cmd(u16 ioaddr, struct vring_virtqueue *vq, struct disk_op_s *op,
37 void *cdbcmd, u16 target, u16 lun, u16 blocksize)
39 struct virtio_scsi_req_cmd req;
40 struct virtio_scsi_resp_cmd resp;
41 struct vring_list sg[3];
43 memset(&req, 0, sizeof(req));
46 req.lun[2] = (lun >> 8) | 0x40;
47 req.lun[3] = (lun & 0xff);
48 memcpy(req.cdb, cdbcmd, 16);
50 u32 len = op->count * blocksize;
51 int datain = cdb_is_read(cdbcmd, blocksize);
52 int in_num = (datain ? 2 : 1);
53 int out_num = (len ? 3 : 2) - in_num;
55 sg[0].addr = MAKE_FLATPTR(GET_SEG(SS), &req);
56 sg[0].length = sizeof(req);
58 sg[out_num].addr = MAKE_FLATPTR(GET_SEG(SS), &resp);
59 sg[out_num].length = sizeof(resp);
62 int data_idx = (datain ? 2 : 1);
63 sg[data_idx].addr = op->buf_fl;
64 sg[data_idx].length = len;
67 /* Add to virtqueue and kick host */
68 vring_add_buf(vq, sg, out_num, in_num, 0, 0);
69 vring_kick(ioaddr, vq, 1);
72 while (!vring_more_used(vq))
75 /* Reclaim virtqueue element */
76 vring_get_buf(vq, NULL);
78 /* Clear interrupt status register. Avoid leaving interrupts stuck if
79 * VRING_AVAIL_F_NO_INTERRUPT was ignored and interrupts were raised.
83 if (resp.response == VIRTIO_SCSI_S_OK && resp.status == 0) {
84 return DISK_RET_SUCCESS;
86 return DISK_RET_EBADTRACK;
90 virtio_scsi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
92 struct virtio_lun_s *vlun_gf =
93 container_of(op->drive_gf, struct virtio_lun_s, drive);
95 return virtio_scsi_cmd(GET_GLOBALFLAT(vlun_gf->ioaddr),
96 GET_GLOBALFLAT(vlun_gf->vq), op, cdbcmd,
97 GET_GLOBALFLAT(vlun_gf->target),
98 GET_GLOBALFLAT(vlun_gf->lun),
103 virtio_scsi_add_lun(struct pci_device *pci, u16 ioaddr,
104 struct vring_virtqueue *vq, u16 target, u16 lun)
106 struct virtio_lun_s *vlun = malloc_fseg(sizeof(*vlun));
111 memset(vlun, 0, sizeof(*vlun));
112 vlun->drive.type = DTYPE_VIRTIO_SCSI;
113 vlun->drive.cntl_id = pci->bdf;
115 vlun->ioaddr = ioaddr;
117 vlun->target = target;
120 int prio = bootprio_find_scsi_device(pci, target, lun);
121 int ret = scsi_drive_setup(&vlun->drive, "virtio-scsi", prio);
132 virtio_scsi_scan_target(struct pci_device *pci, u16 ioaddr,
133 struct vring_virtqueue *vq, u16 target)
135 /* TODO: send REPORT LUNS. For now, only LUN 0 is recognized. */
136 int ret = virtio_scsi_add_lun(pci, ioaddr, vq, target, 0);
137 return ret < 0 ? 0 : 1;
141 init_virtio_scsi(struct pci_device *pci)
144 dprintf(1, "found virtio-scsi at %x:%x\n", pci_bdf_to_bus(bdf),
145 pci_bdf_to_dev(bdf));
146 struct vring_virtqueue *vq = NULL;
147 u16 ioaddr = vp_init_simple(bdf);
148 if (vp_find_vq(ioaddr, 2, &vq) < 0 ) {
149 dprintf(1, "fail to find vq for virtio-scsi %x:%x\n",
150 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
154 vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE |
155 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK);
158 for (tot = 0, i = 0; i < 256; i++)
159 tot += virtio_scsi_scan_target(pci, ioaddr, vq, i);
172 virtio_scsi_setup(void)
175 if (! CONFIG_VIRTIO_SCSI)
178 dprintf(3, "init virtio-scsi\n");
180 struct pci_device *pci;
182 if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET
183 || pci->device != PCI_DEVICE_ID_VIRTIO_SCSI)
185 init_virtio_scsi(pci);