blob: 8af01777d09c74f344ad325256dbd30248febe32 [file] [log] [blame]
Paolo Bonzini4fe74b12012-02-05 12:16:00 +01001/*
2 * Virtio SCSI HBA driver
3 *
4 * Copyright IBM Corp. 2010
5 * Copyright Red Hat, Inc. 2011
6 *
7 * Authors:
8 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
13 *
14 */
15
Wanlong Gaoba06d1e2013-03-12 15:34:40 +103016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010018#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/mempool.h>
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +010021#include <linux/interrupt.h>
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010022#include <linux/virtio.h>
23#include <linux/virtio_ids.h>
24#include <linux/virtio_config.h>
25#include <linux/virtio_scsi.h>
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093026#include <linux/cpu.h>
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -080027#include <linux/blkdev.h>
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010028#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_cmnd.h>
Venkatesh Srinivas761f1192014-07-06 16:39:27 +020031#include <scsi/scsi_tcq.h>
David Gibson25d1d502017-04-13 12:13:00 +100032#include <scsi/scsi_devinfo.h>
Ming Lei938ece72014-07-06 16:39:26 +020033#include <linux/seqlock.h>
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +010034#include <linux/blk-mq-virtio.h>
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010035
36#define VIRTIO_SCSI_MEMPOOL_SZ 64
Cong Meng365a7152012-07-05 17:06:43 +080037#define VIRTIO_SCSI_EVENT_LEN 8
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093038#define VIRTIO_SCSI_VQ_BASE 2
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010039
40/* Command queue element */
41struct virtio_scsi_cmd {
42 struct scsi_cmnd *sc;
43 struct completion *comp;
44 union {
45 struct virtio_scsi_cmd_req cmd;
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -080046 struct virtio_scsi_cmd_req_pi cmd_pi;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010047 struct virtio_scsi_ctrl_tmf_req tmf;
48 struct virtio_scsi_ctrl_an_req an;
49 } req;
50 union {
51 struct virtio_scsi_cmd_resp cmd;
52 struct virtio_scsi_ctrl_tmf_resp tmf;
53 struct virtio_scsi_ctrl_an_resp an;
54 struct virtio_scsi_event evt;
55 } resp;
56} ____cacheline_aligned_in_smp;
57
Cong Meng365a7152012-07-05 17:06:43 +080058struct virtio_scsi_event_node {
59 struct virtio_scsi *vscsi;
60 struct virtio_scsi_event event;
61 struct work_struct work;
62};
63
Paolo Bonzini139fe452012-06-13 16:56:32 +020064struct virtio_scsi_vq {
65 /* Protects vq */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010066 spinlock_t vq_lock;
67
Paolo Bonzini139fe452012-06-13 16:56:32 +020068 struct virtqueue *vq;
69};
70
71/* Driver instance state */
72struct virtio_scsi {
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010073 struct virtio_device *vdev;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020074
Cong Meng365a7152012-07-05 17:06:43 +080075 /* Get some buffers ready for event vq */
76 struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093077
78 u32 num_queues;
79
80 /* If the affinity hint is set for virtqueues */
81 bool affinity_hint_set;
82
Sebastian Andrzej Siewior8904f5a2016-09-06 19:04:46 +020083 struct hlist_node node;
Wanlong Gao285e71e2013-04-08 23:05:49 +093084
Michael S. Tsirkine67423c2014-10-15 10:22:33 +103085 /* Protected by event_vq lock */
86 bool stop_events;
87
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093088 struct virtio_scsi_vq ctrl_vq;
89 struct virtio_scsi_vq event_vq;
90 struct virtio_scsi_vq req_vqs[];
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010091};
92
93static struct kmem_cache *virtscsi_cmd_cache;
94static mempool_t *virtscsi_cmd_pool;
95
96static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
97{
98 return vdev->priv;
99}
100
101static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
102{
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100103 if (resid)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100104 scsi_set_resid(sc, resid);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100105}
106
107/**
108 * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
109 *
110 * Called with vq_lock held.
111 */
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930112static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100113{
114 struct virtio_scsi_cmd *cmd = buf;
115 struct scsi_cmnd *sc = cmd->sc;
116 struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
117
118 dev_dbg(&sc->device->sdev_gendev,
119 "cmd %p response %u status %#02x sense_len %u\n",
120 sc, resp->response, resp->status, resp->sense_len);
121
122 sc->result = resp->status;
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200123 virtscsi_compute_resid(sc, virtio32_to_cpu(vscsi->vdev, resp->resid));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100124 switch (resp->response) {
125 case VIRTIO_SCSI_S_OK:
126 set_host_byte(sc, DID_OK);
127 break;
128 case VIRTIO_SCSI_S_OVERRUN:
129 set_host_byte(sc, DID_ERROR);
130 break;
131 case VIRTIO_SCSI_S_ABORTED:
132 set_host_byte(sc, DID_ABORT);
133 break;
134 case VIRTIO_SCSI_S_BAD_TARGET:
135 set_host_byte(sc, DID_BAD_TARGET);
136 break;
137 case VIRTIO_SCSI_S_RESET:
138 set_host_byte(sc, DID_RESET);
139 break;
140 case VIRTIO_SCSI_S_BUSY:
141 set_host_byte(sc, DID_BUS_BUSY);
142 break;
143 case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
144 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
145 break;
146 case VIRTIO_SCSI_S_TARGET_FAILURE:
147 set_host_byte(sc, DID_TARGET_FAILURE);
148 break;
149 case VIRTIO_SCSI_S_NEXUS_FAILURE:
150 set_host_byte(sc, DID_NEXUS_FAILURE);
151 break;
152 default:
153 scmd_printk(KERN_WARNING, sc, "Unknown response %d",
154 resp->response);
155 /* fall through */
156 case VIRTIO_SCSI_S_FAILURE:
157 set_host_byte(sc, DID_ERROR);
158 break;
159 }
160
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200161 WARN_ON(virtio32_to_cpu(vscsi->vdev, resp->sense_len) >
162 VIRTIO_SCSI_SENSE_SIZE);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100163 if (sc->sense_buffer) {
164 memcpy(sc->sense_buffer, resp->sense,
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200165 min_t(u32,
166 virtio32_to_cpu(vscsi->vdev, resp->sense_len),
167 VIRTIO_SCSI_SENSE_SIZE));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100168 if (resp->sense_len)
169 set_driver_byte(sc, DRIVER_SENSE);
170 }
171
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100172 sc->scsi_done(sc);
173}
174
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930175static void virtscsi_vq_done(struct virtio_scsi *vscsi,
176 struct virtio_scsi_vq *virtscsi_vq,
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930177 void (*fn)(struct virtio_scsi *vscsi, void *buf))
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100178{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100179 void *buf;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100180 unsigned int len;
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930181 unsigned long flags;
182 struct virtqueue *vq = virtscsi_vq->vq;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100183
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930184 spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100185 do {
186 virtqueue_disable_cb(vq);
187 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930188 fn(vscsi, buf);
Heinz Graalfs2bf4fd32013-11-11 11:52:43 +1030189
190 if (unlikely(virtqueue_is_broken(vq)))
191 break;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100192 } while (!virtqueue_enable_cb(vq));
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930193 spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100194}
195
196static void virtscsi_req_done(struct virtqueue *vq)
197{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200198 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
199 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930200 int index = vq->index - VIRTIO_SCSI_VQ_BASE;
201 struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index];
Paolo Bonzini139fe452012-06-13 16:56:32 +0200202
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930203 virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100204};
205
Paolo Bonzini8faeb522014-06-04 13:34:58 +0200206static void virtscsi_poll_requests(struct virtio_scsi *vscsi)
207{
208 int i, num_vqs;
209
210 num_vqs = vscsi->num_queues;
211 for (i = 0; i < num_vqs; i++)
212 virtscsi_vq_done(vscsi, &vscsi->req_vqs[i],
213 virtscsi_complete_cmd);
214}
215
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930216static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100217{
218 struct virtio_scsi_cmd *cmd = buf;
219
220 if (cmd->comp)
Daniel Wagnere8f81422016-09-13 10:58:50 +0200221 complete(cmd->comp);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100222}
223
224static void virtscsi_ctrl_done(struct virtqueue *vq)
225{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200226 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
227 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200228
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930229 virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100230};
231
Paolo Bonzinicdda0e52014-06-04 13:34:56 +0200232static void virtscsi_handle_event(struct work_struct *work);
233
Cong Meng365a7152012-07-05 17:06:43 +0800234static int virtscsi_kick_event(struct virtio_scsi *vscsi,
235 struct virtio_scsi_event_node *event_node)
236{
Rusty Russell4614e512012-10-16 23:56:16 +1030237 int err;
Cong Meng365a7152012-07-05 17:06:43 +0800238 struct scatterlist sg;
239 unsigned long flags;
240
Paolo Bonzinicdda0e52014-06-04 13:34:56 +0200241 INIT_WORK(&event_node->work, virtscsi_handle_event);
Richard W.M. Jones2e9c9df2012-10-02 17:25:46 +0200242 sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
Cong Meng365a7152012-07-05 17:06:43 +0800243
244 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
245
Rusty Russellbf958292013-03-20 15:44:28 +1030246 err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
247 GFP_ATOMIC);
Rusty Russell4614e512012-10-16 23:56:16 +1030248 if (!err)
Cong Meng365a7152012-07-05 17:06:43 +0800249 virtqueue_kick(vscsi->event_vq.vq);
250
251 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
252
Rusty Russell4614e512012-10-16 23:56:16 +1030253 return err;
Cong Meng365a7152012-07-05 17:06:43 +0800254}
255
256static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
257{
258 int i;
259
260 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
261 vscsi->event_list[i].vscsi = vscsi;
262 virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
263 }
264
265 return 0;
266}
267
268static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
269{
270 int i;
271
Michael S. Tsirkine67423c2014-10-15 10:22:33 +1030272 /* Stop scheduling work before calling cancel_work_sync. */
273 spin_lock_irq(&vscsi->event_vq.vq_lock);
274 vscsi->stop_events = true;
275 spin_unlock_irq(&vscsi->event_vq.vq_lock);
276
Cong Meng365a7152012-07-05 17:06:43 +0800277 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
278 cancel_work_sync(&vscsi->event_list[i].work);
279}
280
281static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930282 struct virtio_scsi_event *event)
Cong Meng365a7152012-07-05 17:06:43 +0800283{
284 struct scsi_device *sdev;
285 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
286 unsigned int target = event->lun[1];
287 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
288
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200289 switch (virtio32_to_cpu(vscsi->vdev, event->reason)) {
Cong Meng365a7152012-07-05 17:06:43 +0800290 case VIRTIO_SCSI_EVT_RESET_RESCAN:
291 scsi_add_device(shost, 0, target, lun);
292 break;
293 case VIRTIO_SCSI_EVT_RESET_REMOVED:
294 sdev = scsi_device_lookup(shost, 0, target, lun);
295 if (sdev) {
296 scsi_remove_device(sdev);
297 scsi_device_put(sdev);
298 } else {
299 pr_err("SCSI device %d 0 %d %d not found\n",
300 shost->host_no, target, lun);
301 }
302 break;
303 default:
304 pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
305 }
306}
307
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200308static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
309 struct virtio_scsi_event *event)
310{
311 struct scsi_device *sdev;
312 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
313 unsigned int target = event->lun[1];
314 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200315 u8 asc = virtio32_to_cpu(vscsi->vdev, event->reason) & 255;
316 u8 ascq = virtio32_to_cpu(vscsi->vdev, event->reason) >> 8;
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200317
318 sdev = scsi_device_lookup(shost, 0, target, lun);
319 if (!sdev) {
320 pr_err("SCSI device %d 0 %d %d not found\n",
321 shost->host_no, target, lun);
322 return;
323 }
324
325 /* Handle "Parameters changed", "Mode parameters changed", and
326 "Capacity data has changed". */
327 if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
328 scsi_rescan_device(&sdev->sdev_gendev);
329
330 scsi_device_put(sdev);
331}
332
Cong Meng365a7152012-07-05 17:06:43 +0800333static void virtscsi_handle_event(struct work_struct *work)
334{
335 struct virtio_scsi_event_node *event_node =
336 container_of(work, struct virtio_scsi_event_node, work);
337 struct virtio_scsi *vscsi = event_node->vscsi;
338 struct virtio_scsi_event *event = &event_node->event;
339
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200340 if (event->event &
341 cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
342 event->event &= ~cpu_to_virtio32(vscsi->vdev,
343 VIRTIO_SCSI_T_EVENTS_MISSED);
Cong Meng365a7152012-07-05 17:06:43 +0800344 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
345 }
346
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200347 switch (virtio32_to_cpu(vscsi->vdev, event->event)) {
Cong Meng365a7152012-07-05 17:06:43 +0800348 case VIRTIO_SCSI_T_NO_EVENT:
349 break;
350 case VIRTIO_SCSI_T_TRANSPORT_RESET:
351 virtscsi_handle_transport_reset(vscsi, event);
352 break;
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200353 case VIRTIO_SCSI_T_PARAM_CHANGE:
354 virtscsi_handle_param_change(vscsi, event);
355 break;
Cong Meng365a7152012-07-05 17:06:43 +0800356 default:
357 pr_err("Unsupport virtio scsi event %x\n", event->event);
358 }
359 virtscsi_kick_event(vscsi, event_node);
360}
361
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930362static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
Cong Meng365a7152012-07-05 17:06:43 +0800363{
364 struct virtio_scsi_event_node *event_node = buf;
365
Michael S. Tsirkine67423c2014-10-15 10:22:33 +1030366 if (!vscsi->stop_events)
367 queue_work(system_freezable_wq, &event_node->work);
Cong Meng365a7152012-07-05 17:06:43 +0800368}
369
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100370static void virtscsi_event_done(struct virtqueue *vq)
371{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200372 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
373 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200374
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930375 virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100376};
377
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100378/**
Wanlong Gao682993b2013-03-20 15:44:28 +1030379 * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
380 * @vq : the struct virtqueue we're talking about
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100381 * @cmd : command structure
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100382 * @req_size : size of the request buffer
383 * @resp_size : size of the response buffer
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100384 */
Wanlong Gao682993b2013-03-20 15:44:28 +1030385static int virtscsi_add_cmd(struct virtqueue *vq,
386 struct virtio_scsi_cmd *cmd,
Rusty Russellc77fba92014-05-21 11:25:04 +0930387 size_t req_size, size_t resp_size)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100388{
389 struct scsi_cmnd *sc = cmd->sc;
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800390 struct scatterlist *sgs[6], req, resp;
Wanlong Gao682993b2013-03-20 15:44:28 +1030391 struct sg_table *out, *in;
392 unsigned out_num = 0, in_num = 0;
393
394 out = in = NULL;
395
396 if (sc && sc->sc_data_direction != DMA_NONE) {
397 if (sc->sc_data_direction != DMA_FROM_DEVICE)
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100398 out = &sc->sdb.table;
Wanlong Gao682993b2013-03-20 15:44:28 +1030399 if (sc->sc_data_direction != DMA_TO_DEVICE)
Christoph Hellwigae3d56d2019-01-29 09:33:07 +0100400 in = &sc->sdb.table;
Wanlong Gao682993b2013-03-20 15:44:28 +1030401 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100402
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100403 /* Request header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030404 sg_init_one(&req, &cmd->req, req_size);
405 sgs[out_num++] = &req;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100406
407 /* Data-out buffer. */
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800408 if (out) {
409 /* Place WRITE protection SGLs before Data OUT payload */
410 if (scsi_prot_sg_count(sc))
411 sgs[out_num++] = scsi_prot_sglist(sc);
Wanlong Gao682993b2013-03-20 15:44:28 +1030412 sgs[out_num++] = out->sgl;
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800413 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100414
415 /* Response header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030416 sg_init_one(&resp, &cmd->resp, resp_size);
417 sgs[out_num + in_num++] = &resp;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100418
419 /* Data-in buffer */
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800420 if (in) {
421 /* Place READ protection SGLs before Data IN payload */
422 if (scsi_prot_sg_count(sc))
423 sgs[out_num + in_num++] = scsi_prot_sglist(sc);
Wanlong Gao682993b2013-03-20 15:44:28 +1030424 sgs[out_num + in_num++] = in->sgl;
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800425 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100426
Rusty Russellc77fba92014-05-21 11:25:04 +0930427 return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100428}
429
Wanlong Gao682993b2013-03-20 15:44:28 +1030430static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100431 struct virtio_scsi_cmd *cmd,
Rusty Russellc77fba92014-05-21 11:25:04 +0930432 size_t req_size, size_t resp_size)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100433{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100434 unsigned long flags;
Rusty Russell4614e512012-10-16 23:56:16 +1030435 int err;
436 bool needs_kick = false;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100437
Wanlong Gao682993b2013-03-20 15:44:28 +1030438 spin_lock_irqsave(&vq->vq_lock, flags);
Rusty Russellc77fba92014-05-21 11:25:04 +0930439 err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
Rusty Russell4614e512012-10-16 23:56:16 +1030440 if (!err)
441 needs_kick = virtqueue_kick_prepare(vq->vq);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100442
Paolo Bonzinibce750b2012-06-13 16:56:33 +0200443 spin_unlock_irqrestore(&vq->vq_lock, flags);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200444
Rusty Russell4614e512012-10-16 23:56:16 +1030445 if (needs_kick)
Paolo Bonzini139fe452012-06-13 16:56:32 +0200446 virtqueue_notify(vq->vq);
Rusty Russell4614e512012-10-16 23:56:16 +1030447 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100448}
449
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200450static void virtio_scsi_init_hdr(struct virtio_device *vdev,
451 struct virtio_scsi_cmd_req *cmd,
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800452 struct scsi_cmnd *sc)
453{
454 cmd->lun[0] = 1;
455 cmd->lun[1] = sc->device->id;
456 cmd->lun[2] = (sc->device->lun >> 8) | 0x40;
457 cmd->lun[3] = sc->device->lun & 0xff;
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200458 cmd->tag = cpu_to_virtio64(vdev, (unsigned long)sc);
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800459 cmd->task_attr = VIRTIO_SCSI_S_SIMPLE;
460 cmd->prio = 0;
461 cmd->crn = 0;
462}
463
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200464#ifdef CONFIG_BLK_DEV_INTEGRITY
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200465static void virtio_scsi_init_hdr_pi(struct virtio_device *vdev,
466 struct virtio_scsi_cmd_req_pi *cmd_pi,
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800467 struct scsi_cmnd *sc)
468{
469 struct request *rq = sc->request;
470 struct blk_integrity *bi;
471
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200472 virtio_scsi_init_hdr(vdev, (struct virtio_scsi_cmd_req *)cmd_pi, sc);
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800473
474 if (!rq || !scsi_prot_sg_count(sc))
475 return;
476
477 bi = blk_get_integrity(rq->rq_disk);
478
479 if (sc->sc_data_direction == DMA_TO_DEVICE)
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200480 cmd_pi->pi_bytesout = cpu_to_virtio32(vdev,
Greg Edwardscdcdcaa2018-07-26 15:52:54 -0400481 bio_integrity_bytes(bi,
482 blk_rq_sectors(rq)));
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800483 else if (sc->sc_data_direction == DMA_FROM_DEVICE)
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200484 cmd_pi->pi_bytesin = cpu_to_virtio32(vdev,
Greg Edwardscdcdcaa2018-07-26 15:52:54 -0400485 bio_integrity_bytes(bi,
486 blk_rq_sectors(rq)));
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800487}
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200488#endif
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800489
Ming Leic3506df2018-03-13 17:42:43 +0800490static struct virtio_scsi_vq *virtscsi_pick_vq_mq(struct virtio_scsi *vscsi,
491 struct scsi_cmnd *sc)
492{
493 u32 tag = blk_mq_unique_tag(sc->request);
494 u16 hwq = blk_mq_unique_tag_to_hwq(tag);
495
496 return &vscsi->req_vqs[hwq];
497}
498
499static int virtscsi_queuecommand(struct Scsi_Host *shost,
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930500 struct scsi_cmnd *sc)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100501{
Ming Leic3506df2018-03-13 17:42:43 +0800502 struct virtio_scsi *vscsi = shost_priv(shost);
503 struct virtio_scsi_vq *req_vq = virtscsi_pick_vq_mq(vscsi, sc);
Christoph Hellwigb54197c2014-05-01 16:51:50 +0200504 struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc);
Eric Farman773c7222017-01-13 12:48:06 -0500505 unsigned long flags;
Linus Torvaldsed9ea4e2014-06-12 22:38:32 -0700506 int req_size;
Eric Farman773c7222017-01-13 12:48:06 -0500507 int ret;
Christoph Hellwigb54197c2014-05-01 16:51:50 +0200508
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200509 BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
510
511 /* TODO: check feature bit and fail if unsupported? */
512 BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
513
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100514 dev_dbg(&sc->device->sdev_gendev,
515 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
516
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100517 cmd->sc = sc;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100518
519 BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100520
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200521#ifdef CONFIG_BLK_DEV_INTEGRITY
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800522 if (virtio_has_feature(vscsi->vdev, VIRTIO_SCSI_F_T10_PI)) {
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200523 virtio_scsi_init_hdr_pi(vscsi->vdev, &cmd->req.cmd_pi, sc);
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800524 memcpy(cmd->req.cmd_pi.cdb, sc->cmnd, sc->cmd_len);
525 req_size = sizeof(cmd->req.cmd_pi);
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200526 } else
527#endif
528 {
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200529 virtio_scsi_init_hdr(vscsi->vdev, &cmd->req.cmd, sc);
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800530 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
531 req_size = sizeof(cmd->req.cmd);
532 }
533
Eric Farman773c7222017-01-13 12:48:06 -0500534 ret = virtscsi_kick_cmd(req_vq, cmd, req_size, sizeof(cmd->resp.cmd));
535 if (ret == -EIO) {
536 cmd->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
537 spin_lock_irqsave(&req_vq->vq_lock, flags);
538 virtscsi_complete_cmd(vscsi, cmd);
539 spin_unlock_irqrestore(&req_vq->vq_lock, flags);
540 } else if (ret != 0) {
Christoph Hellwigb54197c2014-05-01 16:51:50 +0200541 return SCSI_MLQUEUE_HOST_BUSY;
Eric Farman773c7222017-01-13 12:48:06 -0500542 }
Christoph Hellwigb54197c2014-05-01 16:51:50 +0200543 return 0;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100544}
545
546static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
547{
548 DECLARE_COMPLETION_ONSTACK(comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200549 int ret = FAILED;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100550
551 cmd->comp = &comp;
Wanlong Gao682993b2013-03-20 15:44:28 +1030552 if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
Rusty Russellc77fba92014-05-21 11:25:04 +0930553 sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0)
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200554 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100555
556 wait_for_completion(&comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200557 if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
558 cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
559 ret = SUCCESS;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100560
Paolo Bonzini8faeb522014-06-04 13:34:58 +0200561 /*
562 * The spec guarantees that all requests related to the TMF have
563 * been completed, but the callback might not have run yet if
564 * we're using independent interrupts (e.g. MSI). Poll the
565 * virtqueues once.
566 *
567 * In the abort case, sc->scsi_done will do nothing, because
568 * the block layer must have detected a timeout and as a result
569 * REQ_ATOM_COMPLETE has been set.
570 */
571 virtscsi_poll_requests(vscsi);
572
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200573out:
574 mempool_free(cmd, virtscsi_cmd_pool);
575 return ret;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100576}
577
578static int virtscsi_device_reset(struct scsi_cmnd *sc)
579{
580 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
581 struct virtio_scsi_cmd *cmd;
582
583 sdev_printk(KERN_INFO, sc->device, "device reset\n");
584 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
585 if (!cmd)
586 return FAILED;
587
588 memset(cmd, 0, sizeof(*cmd));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100589 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
590 .type = VIRTIO_SCSI_T_TMF,
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200591 .subtype = cpu_to_virtio32(vscsi->vdev,
592 VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100593 .lun[0] = 1,
594 .lun[1] = sc->device->id,
595 .lun[2] = (sc->device->lun >> 8) | 0x40,
596 .lun[3] = sc->device->lun & 0xff,
597 };
598 return virtscsi_tmf(vscsi, cmd);
599}
600
David Gibson25d1d502017-04-13 12:13:00 +1000601static int virtscsi_device_alloc(struct scsi_device *sdevice)
602{
603 /*
604 * Passed through SCSI targets (e.g. with qemu's 'scsi-block')
605 * may have transfer limits which come from the host SCSI
606 * controller or something on the host side other than the
607 * target itself.
608 *
609 * To make this work properly, the hypervisor can adjust the
610 * target's VPD information to advertise these limits. But
611 * for that to work, the guest has to look at the VPD pages,
612 * which we won't do by default if it is an SPC-2 device, even
613 * if it does actually support it.
614 *
615 * So, set the blist to always try to read the VPD pages.
616 */
617 sdevice->sdev_bflags = BLIST_TRY_VPD_PAGES;
618
619 return 0;
620}
621
622
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200623/**
624 * virtscsi_change_queue_depth() - Change a virtscsi target's queue depth
625 * @sdev: Virtscsi target whose queue depth to change
626 * @qdepth: New queue depth
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200627 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100628static int virtscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200629{
630 struct Scsi_Host *shost = sdev->host;
631 int max_depth = shost->cmd_per_lun;
632
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100633 return scsi_change_queue_depth(sdev, min(max_depth, qdepth));
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200634}
635
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100636static int virtscsi_abort(struct scsi_cmnd *sc)
637{
638 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
639 struct virtio_scsi_cmd *cmd;
640
641 scmd_printk(KERN_INFO, sc, "abort\n");
642 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
643 if (!cmd)
644 return FAILED;
645
646 memset(cmd, 0, sizeof(*cmd));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100647 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
648 .type = VIRTIO_SCSI_T_TMF,
649 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
650 .lun[0] = 1,
651 .lun[1] = sc->device->id,
652 .lun[2] = (sc->device->lun >> 8) | 0x40,
653 .lun[3] = sc->device->lun & 0xff,
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200654 .tag = cpu_to_virtio64(vscsi->vdev, (unsigned long)sc),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100655 };
656 return virtscsi_tmf(vscsi, cmd);
657}
658
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100659static int virtscsi_map_queues(struct Scsi_Host *shost)
660{
661 struct virtio_scsi *vscsi = shost_priv(shost);
Jens Axboeed76e322018-10-29 13:06:14 -0600662 struct blk_mq_queue_map *qmap = &shost->tag_set.map[0];
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100663
Jens Axboeed76e322018-10-29 13:06:14 -0600664 return blk_mq_virtio_map_queues(qmap, vscsi->vdev, 2);
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100665}
666
Paolo Bonzinie72c9a22017-06-21 16:35:46 +0200667/*
668 * The host guarantees to respond to each command, although I/O
669 * latencies might be higher than on bare metal. Reset the timer
670 * unconditionally to give the host a chance to perform EH.
671 */
672static enum blk_eh_timer_return virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
673{
674 return BLK_EH_RESET_TIMER;
675}
676
Ming Leic3506df2018-03-13 17:42:43 +0800677static struct scsi_host_template virtscsi_host_template = {
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100678 .module = THIS_MODULE,
679 .name = "Virtio SCSI HBA",
680 .proc_name = "virtio_scsi",
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100681 .this_id = -1,
Christoph Hellwigb54197c2014-05-01 16:51:50 +0200682 .cmd_size = sizeof(struct virtio_scsi_cmd),
Ming Leic3506df2018-03-13 17:42:43 +0800683 .queuecommand = virtscsi_queuecommand,
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200684 .change_queue_depth = virtscsi_change_queue_depth,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100685 .eh_abort_handler = virtscsi_abort,
686 .eh_device_reset_handler = virtscsi_device_reset,
Paolo Bonzinie72c9a22017-06-21 16:35:46 +0200687 .eh_timed_out = virtscsi_eh_timed_out,
Paolo Bonzinia680f1d2017-07-05 10:30:56 +0200688 .slave_alloc = virtscsi_device_alloc,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100689
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100690 .dma_boundary = UINT_MAX,
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100691 .map_queues = virtscsi_map_queues,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100692 .track_queue_depth = 1,
Ming Leib5b6e8c2018-03-13 17:42:42 +0800693 .force_blk_mq = 1,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100694};
695
696#define virtscsi_config_get(vdev, fld) \
697 ({ \
698 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
Rusty Russell855e0c52013-10-14 18:11:51 +1030699 virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100700 __val; \
701 })
702
703#define virtscsi_config_set(vdev, fld, val) \
Rusty Russell855e0c52013-10-14 18:11:51 +1030704 do { \
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100705 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
Rusty Russell855e0c52013-10-14 18:11:51 +1030706 virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
707 } while(0)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100708
Paolo Bonzini139fe452012-06-13 16:56:32 +0200709static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
710 struct virtqueue *vq)
711{
712 spin_lock_init(&virtscsi_vq->vq_lock);
713 virtscsi_vq->vq = vq;
714}
715
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200716static void virtscsi_remove_vqs(struct virtio_device *vdev)
717{
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200718 /* Stop all the virtqueues. */
719 vdev->config->reset(vdev);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200720 vdev->config->del_vqs(vdev);
721}
722
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100723static int virtscsi_init(struct virtio_device *vdev,
Wanlong Gao5c370192013-04-08 23:01:16 +0930724 struct virtio_scsi *vscsi)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100725{
726 int err;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930727 u32 i;
728 u32 num_vqs;
729 vq_callback_t **callbacks;
730 const char **names;
731 struct virtqueue **vqs;
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100732 struct irq_affinity desc = { .pre_vectors = 2 };
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200733
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930734 num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
Kees Cook6da2ec52018-06-12 13:55:00 -0700735 vqs = kmalloc_array(num_vqs, sizeof(struct virtqueue *), GFP_KERNEL);
736 callbacks = kmalloc_array(num_vqs, sizeof(vq_callback_t *),
737 GFP_KERNEL);
738 names = kmalloc_array(num_vqs, sizeof(char *), GFP_KERNEL);
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930739
740 if (!callbacks || !vqs || !names) {
741 err = -ENOMEM;
742 goto out;
743 }
744
745 callbacks[0] = virtscsi_ctrl_done;
746 callbacks[1] = virtscsi_event_done;
747 names[0] = "control";
748 names[1] = "event";
749 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
750 callbacks[i] = virtscsi_req_done;
751 names[i] = "request";
752 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100753
754 /* Discover virtqueues and write information to configuration. */
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200755 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100756 if (err)
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930757 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100758
Paolo Bonzini139fe452012-06-13 16:56:32 +0200759 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
760 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930761 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
762 virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
763 vqs[i]);
764
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100765 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
766 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200767
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930768 err = 0;
769
770out:
771 kfree(names);
772 kfree(callbacks);
773 kfree(vqs);
774 if (err)
775 virtscsi_remove_vqs(vdev);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200776 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100777}
778
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800779static int virtscsi_probe(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100780{
781 struct Scsi_Host *shost;
782 struct virtio_scsi *vscsi;
Stephen Rothwell908a5542015-06-30 10:59:04 +1000783 int err;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200784 u32 sg_elems, num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100785 u32 cmd_per_lun;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930786 u32 num_queues;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930787
Michael S. Tsirkin8cab3cd2015-01-12 16:23:37 +0200788 if (!vdev->config->get) {
789 dev_err(&vdev->dev, "%s failure: config access disabled\n",
790 __func__);
791 return -EINVAL;
792 }
793
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930794 /* We need to know how many queues before we allocate. */
795 num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100796
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200797 num_targets = virtscsi_config_get(vdev, max_target) + 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100798
Ming Leic3506df2018-03-13 17:42:43 +0800799 shost = scsi_host_alloc(&virtscsi_host_template,
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930800 sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100801 if (!shost)
802 return -ENOMEM;
803
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200804 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100805 shost->sg_tablesize = sg_elems;
806 vscsi = shost_priv(shost);
807 vscsi->vdev = vdev;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930808 vscsi->num_queues = num_queues;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100809 vdev->priv = shost;
810
Wanlong Gao5c370192013-04-08 23:01:16 +0930811 err = virtscsi_init(vdev, vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100812 if (err)
813 goto virtscsi_init_failed;
814
Richard W.M. Jones582b0ab2017-08-10 17:56:52 +0100815 shost->can_queue = virtqueue_get_vring_size(vscsi->req_vqs[0].vq);
816
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100817 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
818 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
819 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
Paolo Bonzini9da5f5a2012-10-02 17:25:47 +0200820
821 /* LUNs > 256 are reported with format 1, so they go in the range
822 * 16640-32767.
823 */
824 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200825 shost->max_id = num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100826 shost->max_channel = 0;
827 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
Ming Leiccbedf12014-11-15 11:47:14 +0800828 shost->nr_hw_queues = num_queues;
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800829
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200830#ifdef CONFIG_BLK_DEV_INTEGRITY
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800831 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) {
Stephen Rothwell908a5542015-06-30 10:59:04 +1000832 int host_prot;
833
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800834 host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
835 SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
836 SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
837
838 scsi_host_set_prot(shost, host_prot);
839 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
840 }
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200841#endif
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800842
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100843 err = scsi_add_host(shost, &vdev->dev);
844 if (err)
845 goto scsi_add_host_failed;
Michael S. Tsirkin5d8f16d2014-10-15 10:22:33 +1030846
847 virtio_device_ready(vdev);
848
849 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
850 virtscsi_kick_event_all(vscsi);
851
852 scsi_scan_host(shost);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100853 return 0;
854
855scsi_add_host_failed:
856 vdev->config->del_vqs(vdev);
857virtscsi_init_failed:
858 scsi_host_put(shost);
859 return err;
860}
861
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800862static void virtscsi_remove(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100863{
864 struct Scsi_Host *shost = virtio_scsi_host(vdev);
Cong Meng365a7152012-07-05 17:06:43 +0800865 struct virtio_scsi *vscsi = shost_priv(shost);
866
867 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
868 virtscsi_cancel_event_work(vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100869
870 scsi_remove_host(shost);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100871 virtscsi_remove_vqs(vdev);
872 scsi_host_put(shost);
873}
874
Aaron Lu89107002013-09-17 09:25:23 +0930875#ifdef CONFIG_PM_SLEEP
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100876static int virtscsi_freeze(struct virtio_device *vdev)
877{
878 virtscsi_remove_vqs(vdev);
879 return 0;
880}
881
882static int virtscsi_restore(struct virtio_device *vdev)
883{
884 struct Scsi_Host *sh = virtio_scsi_host(vdev);
885 struct virtio_scsi *vscsi = shost_priv(sh);
Asias Hef466f752014-01-16 10:18:48 +1030886 int err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100887
Asias Hef466f752014-01-16 10:18:48 +1030888 err = virtscsi_init(vdev, vscsi);
889 if (err)
890 return err;
891
Michael S. Tsirkin52c9cf12014-10-15 10:22:32 +1030892 virtio_device_ready(vdev);
893
Michael S. Tsirkincd679042014-10-15 10:22:31 +1030894 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
895 virtscsi_kick_event_all(vscsi);
Asias Hef466f752014-01-16 10:18:48 +1030896
897 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100898}
899#endif
900
901static struct virtio_device_id id_table[] = {
902 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
903 { 0 },
904};
905
Cong Meng365a7152012-07-05 17:06:43 +0800906static unsigned int features[] = {
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200907 VIRTIO_SCSI_F_HOTPLUG,
908 VIRTIO_SCSI_F_CHANGE,
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200909#ifdef CONFIG_BLK_DEV_INTEGRITY
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800910 VIRTIO_SCSI_F_T10_PI,
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200911#endif
Cong Meng365a7152012-07-05 17:06:43 +0800912};
913
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100914static struct virtio_driver virtio_scsi_driver = {
Cong Meng365a7152012-07-05 17:06:43 +0800915 .feature_table = features,
916 .feature_table_size = ARRAY_SIZE(features),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100917 .driver.name = KBUILD_MODNAME,
918 .driver.owner = THIS_MODULE,
919 .id_table = id_table,
920 .probe = virtscsi_probe,
Aaron Lu89107002013-09-17 09:25:23 +0930921#ifdef CONFIG_PM_SLEEP
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100922 .freeze = virtscsi_freeze,
923 .restore = virtscsi_restore,
924#endif
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800925 .remove = virtscsi_remove,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100926};
927
928static int __init init(void)
929{
930 int ret = -ENOMEM;
931
932 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
933 if (!virtscsi_cmd_cache) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030934 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100935 goto error;
936 }
937
938
939 virtscsi_cmd_pool =
940 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
941 virtscsi_cmd_cache);
942 if (!virtscsi_cmd_pool) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030943 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100944 goto error;
945 }
946 ret = register_virtio_driver(&virtio_scsi_driver);
947 if (ret < 0)
948 goto error;
949
950 return 0;
951
952error:
953 if (virtscsi_cmd_pool) {
954 mempool_destroy(virtscsi_cmd_pool);
955 virtscsi_cmd_pool = NULL;
956 }
957 if (virtscsi_cmd_cache) {
958 kmem_cache_destroy(virtscsi_cmd_cache);
959 virtscsi_cmd_cache = NULL;
960 }
961 return ret;
962}
963
964static void __exit fini(void)
965{
966 unregister_virtio_driver(&virtio_scsi_driver);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100967 mempool_destroy(virtscsi_cmd_pool);
968 kmem_cache_destroy(virtscsi_cmd_cache);
969}
970module_init(init);
971module_exit(fini);
972
973MODULE_DEVICE_TABLE(virtio, id_table);
974MODULE_DESCRIPTION("Virtio SCSI HBA driver");
975MODULE_LICENSE("GPL");