blob: 7cde3bcb405f3d533f5defdf03be8bbd6309f068 [file] [log] [blame]
Paolo Bonzinic5c488f2012-02-27 17:22:23 +01001// Virtio SCSI boot support.
2//
3// Copyright (C) 2012 Red Hat Inc.
4//
5// Authors:
6// Paolo Bonzini <pbonzini@redhat.com>
7//
8// This file may be distributed under the terms of the GNU LGPLv3 license.
9
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010010#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor135f3f62013-09-14 23:57:26 -040011#include "block.h" // struct drive_s
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "blockcmd.h" // scsi_drive_setup
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040013#include "config.h" // CONFIG_*
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "malloc.h" // free
15#include "output.h" // dprintf
16#include "pci.h" // foreachpci
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010017#include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK
18#include "pci_regs.h" // PCI_VENDOR_ID
Kevin O'Connor135f3f62013-09-14 23:57:26 -040019#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040020#include "string.h" // memset
21#include "util.h" // usleep
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010022#include "virtio-pci.h"
23#include "virtio-ring.h"
24#include "virtio-scsi.h"
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010025
26struct virtio_lun_s {
27 struct drive_s drive;
28 struct pci_device *pci;
29 struct vring_virtqueue *vq;
30 u16 ioaddr;
31 u16 target;
32 u16 lun;
33};
34
35static int
36virtio_scsi_cmd(u16 ioaddr, struct vring_virtqueue *vq, struct disk_op_s *op,
Kevin O'Connor1fd9a892012-03-14 21:27:45 -040037 void *cdbcmd, u16 target, u16 lun, u16 blocksize)
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010038{
39 struct virtio_scsi_req_cmd req;
40 struct virtio_scsi_resp_cmd resp;
41 struct vring_list sg[3];
42
43 memset(&req, 0, sizeof(req));
44 req.lun[0] = 1;
45 req.lun[1] = target;
46 req.lun[2] = (lun >> 8) | 0x40;
47 req.lun[3] = (lun & 0xff);
48 memcpy(req.cdb, cdbcmd, 16);
49
Kevin O'Connor1fd9a892012-03-14 21:27:45 -040050 u32 len = op->count * blocksize;
51 int datain = cdb_is_read(cdbcmd, blocksize);
Paolo Bonzini8c313072012-03-16 18:54:46 +010052 int in_num = (datain ? 2 : 1);
53 int out_num = (len ? 3 : 2) - in_num;
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010054
55 sg[0].addr = MAKE_FLATPTR(GET_SEG(SS), &req);
56 sg[0].length = sizeof(req);
57
58 sg[out_num].addr = MAKE_FLATPTR(GET_SEG(SS), &resp);
59 sg[out_num].length = sizeof(resp);
60
Paolo Bonzini8c313072012-03-16 18:54:46 +010061 if (len) {
62 int data_idx = (datain ? 2 : 1);
63 sg[data_idx].addr = op->buf_fl;
64 sg[data_idx].length = len;
65 }
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010066
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);
70
71 /* Wait for reply */
72 while (!vring_more_used(vq))
73 usleep(5);
74
75 /* Reclaim virtqueue element */
76 vring_get_buf(vq, NULL);
77
78 /* Clear interrupt status register. Avoid leaving interrupts stuck if
79 * VRING_AVAIL_F_NO_INTERRUPT was ignored and interrupts were raised.
80 */
81 vp_get_isr(ioaddr);
82
83 if (resp.response == VIRTIO_SCSI_S_OK && resp.status == 0) {
84 return DISK_RET_SUCCESS;
85 }
86 return DISK_RET_EBADTRACK;
87}
88
89int
90virtio_scsi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
91{
92 struct virtio_lun_s *vlun =
93 container_of(op->drive_g, struct virtio_lun_s, drive);
94
95 return virtio_scsi_cmd(GET_GLOBAL(vlun->ioaddr),
96 GET_GLOBAL(vlun->vq), op, cdbcmd,
97 GET_GLOBAL(vlun->target), GET_GLOBAL(vlun->lun),
Kevin O'Connor1fd9a892012-03-14 21:27:45 -040098 blocksize);
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010099}
100
101static int
102virtio_scsi_add_lun(struct pci_device *pci, u16 ioaddr,
103 struct vring_virtqueue *vq, u16 target, u16 lun)
104{
105 struct virtio_lun_s *vlun = malloc_fseg(sizeof(*vlun));
106 if (!vlun) {
107 warn_noalloc();
108 return -1;
109 }
110 memset(vlun, 0, sizeof(*vlun));
111 vlun->drive.type = DTYPE_VIRTIO_SCSI;
112 vlun->drive.cntl_id = pci->bdf;
113 vlun->pci = pci;
114 vlun->ioaddr = ioaddr;
115 vlun->vq = vq;
116 vlun->target = target;
117 vlun->lun = lun;
118
119 int prio = bootprio_find_scsi_device(pci, target, lun);
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500120 int ret = scsi_drive_setup(&vlun->drive, "virtio-scsi", prio);
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100121 if (ret)
122 goto fail;
123 return 0;
124
125fail:
126 free(vlun);
127 return -1;
128}
129
130static int
131virtio_scsi_scan_target(struct pci_device *pci, u16 ioaddr,
132 struct vring_virtqueue *vq, u16 target)
133{
134 /* TODO: send REPORT LUNS. For now, only LUN 0 is recognized. */
135 int ret = virtio_scsi_add_lun(pci, ioaddr, vq, target, 0);
136 return ret < 0 ? 0 : 1;
137}
138
139static void
140init_virtio_scsi(struct pci_device *pci)
141{
142 u16 bdf = pci->bdf;
143 dprintf(1, "found virtio-scsi at %x:%x\n", pci_bdf_to_bus(bdf),
144 pci_bdf_to_dev(bdf));
145 struct vring_virtqueue *vq = NULL;
146 u16 ioaddr = vp_init_simple(bdf);
147 if (vp_find_vq(ioaddr, 2, &vq) < 0 ) {
148 dprintf(1, "fail to find vq for virtio-scsi %x:%x\n",
149 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
150 goto fail;
151 }
152
Asias He5a7730d2013-03-15 09:45:15 +0800153 vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE |
154 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK);
155
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100156 int i, tot;
157 for (tot = 0, i = 0; i < 256; i++)
158 tot += virtio_scsi_scan_target(pci, ioaddr, vq, i);
159
160 if (!tot)
161 goto fail;
162
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100163 return;
164
165fail:
166 free(vq);
167}
168
169void
170virtio_scsi_setup(void)
171{
172 ASSERT32FLAT();
Kevin O'Connor897fb112013-02-07 23:32:48 -0500173 if (! CONFIG_VIRTIO_SCSI)
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100174 return;
175
176 dprintf(3, "init virtio-scsi\n");
177
178 struct pci_device *pci;
179 foreachpci(pci) {
180 if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET
181 || pci->device != PCI_DEVICE_ID_VIRTIO_SCSI)
182 continue;
183 init_virtio_scsi(pci);
184 }
185}