blob: 1a6f150cd2d873682d21e58660958d2c7bb51893 [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));
589 cmd->sc = sc;
590 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
591 .type = VIRTIO_SCSI_T_TMF,
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200592 .subtype = cpu_to_virtio32(vscsi->vdev,
593 VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100594 .lun[0] = 1,
595 .lun[1] = sc->device->id,
596 .lun[2] = (sc->device->lun >> 8) | 0x40,
597 .lun[3] = sc->device->lun & 0xff,
598 };
599 return virtscsi_tmf(vscsi, cmd);
600}
601
David Gibson25d1d502017-04-13 12:13:00 +1000602static int virtscsi_device_alloc(struct scsi_device *sdevice)
603{
604 /*
605 * Passed through SCSI targets (e.g. with qemu's 'scsi-block')
606 * may have transfer limits which come from the host SCSI
607 * controller or something on the host side other than the
608 * target itself.
609 *
610 * To make this work properly, the hypervisor can adjust the
611 * target's VPD information to advertise these limits. But
612 * for that to work, the guest has to look at the VPD pages,
613 * which we won't do by default if it is an SPC-2 device, even
614 * if it does actually support it.
615 *
616 * So, set the blist to always try to read the VPD pages.
617 */
618 sdevice->sdev_bflags = BLIST_TRY_VPD_PAGES;
619
620 return 0;
621}
622
623
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200624/**
625 * virtscsi_change_queue_depth() - Change a virtscsi target's queue depth
626 * @sdev: Virtscsi target whose queue depth to change
627 * @qdepth: New queue depth
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200628 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100629static int virtscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200630{
631 struct Scsi_Host *shost = sdev->host;
632 int max_depth = shost->cmd_per_lun;
633
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100634 return scsi_change_queue_depth(sdev, min(max_depth, qdepth));
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200635}
636
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100637static int virtscsi_abort(struct scsi_cmnd *sc)
638{
639 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
640 struct virtio_scsi_cmd *cmd;
641
642 scmd_printk(KERN_INFO, sc, "abort\n");
643 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
644 if (!cmd)
645 return FAILED;
646
647 memset(cmd, 0, sizeof(*cmd));
648 cmd->sc = sc;
649 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
650 .type = VIRTIO_SCSI_T_TMF,
651 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
652 .lun[0] = 1,
653 .lun[1] = sc->device->id,
654 .lun[2] = (sc->device->lun >> 8) | 0x40,
655 .lun[3] = sc->device->lun & 0xff,
Michael S. Tsirkind75dff32014-11-23 17:28:57 +0200656 .tag = cpu_to_virtio64(vscsi->vdev, (unsigned long)sc),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100657 };
658 return virtscsi_tmf(vscsi, cmd);
659}
660
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100661static int virtscsi_map_queues(struct Scsi_Host *shost)
662{
663 struct virtio_scsi *vscsi = shost_priv(shost);
Jens Axboeed76e322018-10-29 13:06:14 -0600664 struct blk_mq_queue_map *qmap = &shost->tag_set.map[0];
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100665
Jens Axboeed76e322018-10-29 13:06:14 -0600666 return blk_mq_virtio_map_queues(qmap, vscsi->vdev, 2);
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100667}
668
Paolo Bonzinie72c9a22017-06-21 16:35:46 +0200669/*
670 * The host guarantees to respond to each command, although I/O
671 * latencies might be higher than on bare metal. Reset the timer
672 * unconditionally to give the host a chance to perform EH.
673 */
674static enum blk_eh_timer_return virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
675{
676 return BLK_EH_RESET_TIMER;
677}
678
Ming Leic3506df2018-03-13 17:42:43 +0800679static struct scsi_host_template virtscsi_host_template = {
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100680 .module = THIS_MODULE,
681 .name = "Virtio SCSI HBA",
682 .proc_name = "virtio_scsi",
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100683 .this_id = -1,
Christoph Hellwigb54197c2014-05-01 16:51:50 +0200684 .cmd_size = sizeof(struct virtio_scsi_cmd),
Ming Leic3506df2018-03-13 17:42:43 +0800685 .queuecommand = virtscsi_queuecommand,
Venkatesh Srinivas761f1192014-07-06 16:39:27 +0200686 .change_queue_depth = virtscsi_change_queue_depth,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100687 .eh_abort_handler = virtscsi_abort,
688 .eh_device_reset_handler = virtscsi_device_reset,
Paolo Bonzinie72c9a22017-06-21 16:35:46 +0200689 .eh_timed_out = virtscsi_eh_timed_out,
Paolo Bonzinia680f1d2017-07-05 10:30:56 +0200690 .slave_alloc = virtscsi_device_alloc,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100691
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100692 .dma_boundary = UINT_MAX,
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100693 .map_queues = virtscsi_map_queues,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100694 .track_queue_depth = 1,
Ming Leib5b6e8c2018-03-13 17:42:42 +0800695 .force_blk_mq = 1,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100696};
697
698#define virtscsi_config_get(vdev, fld) \
699 ({ \
700 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
Rusty Russell855e0c52013-10-14 18:11:51 +1030701 virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100702 __val; \
703 })
704
705#define virtscsi_config_set(vdev, fld, val) \
Rusty Russell855e0c52013-10-14 18:11:51 +1030706 do { \
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100707 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
Rusty Russell855e0c52013-10-14 18:11:51 +1030708 virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
709 } while(0)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100710
Paolo Bonzini139fe452012-06-13 16:56:32 +0200711static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
712 struct virtqueue *vq)
713{
714 spin_lock_init(&virtscsi_vq->vq_lock);
715 virtscsi_vq->vq = vq;
716}
717
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200718static void virtscsi_remove_vqs(struct virtio_device *vdev)
719{
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200720 /* Stop all the virtqueues. */
721 vdev->config->reset(vdev);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200722 vdev->config->del_vqs(vdev);
723}
724
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100725static int virtscsi_init(struct virtio_device *vdev,
Wanlong Gao5c370192013-04-08 23:01:16 +0930726 struct virtio_scsi *vscsi)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100727{
728 int err;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930729 u32 i;
730 u32 num_vqs;
731 vq_callback_t **callbacks;
732 const char **names;
733 struct virtqueue **vqs;
Christoph Hellwig0d9f0a52017-02-05 18:15:26 +0100734 struct irq_affinity desc = { .pre_vectors = 2 };
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200735
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930736 num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
Kees Cook6da2ec52018-06-12 13:55:00 -0700737 vqs = kmalloc_array(num_vqs, sizeof(struct virtqueue *), GFP_KERNEL);
738 callbacks = kmalloc_array(num_vqs, sizeof(vq_callback_t *),
739 GFP_KERNEL);
740 names = kmalloc_array(num_vqs, sizeof(char *), GFP_KERNEL);
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930741
742 if (!callbacks || !vqs || !names) {
743 err = -ENOMEM;
744 goto out;
745 }
746
747 callbacks[0] = virtscsi_ctrl_done;
748 callbacks[1] = virtscsi_event_done;
749 names[0] = "control";
750 names[1] = "event";
751 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
752 callbacks[i] = virtscsi_req_done;
753 names[i] = "request";
754 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100755
756 /* Discover virtqueues and write information to configuration. */
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200757 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100758 if (err)
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930759 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100760
Paolo Bonzini139fe452012-06-13 16:56:32 +0200761 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
762 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930763 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
764 virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
765 vqs[i]);
766
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100767 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
768 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200769
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930770 err = 0;
771
772out:
773 kfree(names);
774 kfree(callbacks);
775 kfree(vqs);
776 if (err)
777 virtscsi_remove_vqs(vdev);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200778 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100779}
780
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800781static int virtscsi_probe(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100782{
783 struct Scsi_Host *shost;
784 struct virtio_scsi *vscsi;
Stephen Rothwell908a5542015-06-30 10:59:04 +1000785 int err;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200786 u32 sg_elems, num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100787 u32 cmd_per_lun;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930788 u32 num_queues;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930789
Michael S. Tsirkin8cab3cd2015-01-12 16:23:37 +0200790 if (!vdev->config->get) {
791 dev_err(&vdev->dev, "%s failure: config access disabled\n",
792 __func__);
793 return -EINVAL;
794 }
795
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930796 /* We need to know how many queues before we allocate. */
797 num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100798
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200799 num_targets = virtscsi_config_get(vdev, max_target) + 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100800
Ming Leic3506df2018-03-13 17:42:43 +0800801 shost = scsi_host_alloc(&virtscsi_host_template,
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930802 sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100803 if (!shost)
804 return -ENOMEM;
805
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200806 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100807 shost->sg_tablesize = sg_elems;
808 vscsi = shost_priv(shost);
809 vscsi->vdev = vdev;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930810 vscsi->num_queues = num_queues;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100811 vdev->priv = shost;
812
Wanlong Gao5c370192013-04-08 23:01:16 +0930813 err = virtscsi_init(vdev, vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100814 if (err)
815 goto virtscsi_init_failed;
816
Richard W.M. Jones582b0ab2017-08-10 17:56:52 +0100817 shost->can_queue = virtqueue_get_vring_size(vscsi->req_vqs[0].vq);
818
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100819 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
820 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
821 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
Paolo Bonzini9da5f5a2012-10-02 17:25:47 +0200822
823 /* LUNs > 256 are reported with format 1, so they go in the range
824 * 16640-32767.
825 */
826 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200827 shost->max_id = num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100828 shost->max_channel = 0;
829 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
Ming Leiccbedf12014-11-15 11:47:14 +0800830 shost->nr_hw_queues = num_queues;
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800831
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200832#ifdef CONFIG_BLK_DEV_INTEGRITY
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800833 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) {
Stephen Rothwell908a5542015-06-30 10:59:04 +1000834 int host_prot;
835
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800836 host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
837 SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
838 SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
839
840 scsi_host_set_prot(shost, host_prot);
841 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
842 }
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200843#endif
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800844
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100845 err = scsi_add_host(shost, &vdev->dev);
846 if (err)
847 goto scsi_add_host_failed;
Michael S. Tsirkin5d8f16d2014-10-15 10:22:33 +1030848
849 virtio_device_ready(vdev);
850
851 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
852 virtscsi_kick_event_all(vscsi);
853
854 scsi_scan_host(shost);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100855 return 0;
856
857scsi_add_host_failed:
858 vdev->config->del_vqs(vdev);
859virtscsi_init_failed:
860 scsi_host_put(shost);
861 return err;
862}
863
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800864static void virtscsi_remove(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100865{
866 struct Scsi_Host *shost = virtio_scsi_host(vdev);
Cong Meng365a7152012-07-05 17:06:43 +0800867 struct virtio_scsi *vscsi = shost_priv(shost);
868
869 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
870 virtscsi_cancel_event_work(vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100871
872 scsi_remove_host(shost);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100873 virtscsi_remove_vqs(vdev);
874 scsi_host_put(shost);
875}
876
Aaron Lu89107002013-09-17 09:25:23 +0930877#ifdef CONFIG_PM_SLEEP
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100878static int virtscsi_freeze(struct virtio_device *vdev)
879{
880 virtscsi_remove_vqs(vdev);
881 return 0;
882}
883
884static int virtscsi_restore(struct virtio_device *vdev)
885{
886 struct Scsi_Host *sh = virtio_scsi_host(vdev);
887 struct virtio_scsi *vscsi = shost_priv(sh);
Asias Hef466f752014-01-16 10:18:48 +1030888 int err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100889
Asias Hef466f752014-01-16 10:18:48 +1030890 err = virtscsi_init(vdev, vscsi);
891 if (err)
892 return err;
893
Michael S. Tsirkin52c9cf12014-10-15 10:22:32 +1030894 virtio_device_ready(vdev);
895
Michael S. Tsirkincd679042014-10-15 10:22:31 +1030896 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
897 virtscsi_kick_event_all(vscsi);
Asias Hef466f752014-01-16 10:18:48 +1030898
899 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100900}
901#endif
902
903static struct virtio_device_id id_table[] = {
904 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
905 { 0 },
906};
907
Cong Meng365a7152012-07-05 17:06:43 +0800908static unsigned int features[] = {
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200909 VIRTIO_SCSI_F_HOTPLUG,
910 VIRTIO_SCSI_F_CHANGE,
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200911#ifdef CONFIG_BLK_DEV_INTEGRITY
Nicholas Bellingere6dc783a2014-02-22 18:23:33 -0800912 VIRTIO_SCSI_F_T10_PI,
Christoph Hellwigc5c25672015-04-27 14:56:14 +0200913#endif
Cong Meng365a7152012-07-05 17:06:43 +0800914};
915
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100916static struct virtio_driver virtio_scsi_driver = {
Cong Meng365a7152012-07-05 17:06:43 +0800917 .feature_table = features,
918 .feature_table_size = ARRAY_SIZE(features),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100919 .driver.name = KBUILD_MODNAME,
920 .driver.owner = THIS_MODULE,
921 .id_table = id_table,
922 .probe = virtscsi_probe,
Aaron Lu89107002013-09-17 09:25:23 +0930923#ifdef CONFIG_PM_SLEEP
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100924 .freeze = virtscsi_freeze,
925 .restore = virtscsi_restore,
926#endif
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800927 .remove = virtscsi_remove,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100928};
929
930static int __init init(void)
931{
932 int ret = -ENOMEM;
933
934 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
935 if (!virtscsi_cmd_cache) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030936 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100937 goto error;
938 }
939
940
941 virtscsi_cmd_pool =
942 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
943 virtscsi_cmd_cache);
944 if (!virtscsi_cmd_pool) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030945 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100946 goto error;
947 }
948 ret = register_virtio_driver(&virtio_scsi_driver);
949 if (ret < 0)
950 goto error;
951
952 return 0;
953
954error:
955 if (virtscsi_cmd_pool) {
956 mempool_destroy(virtscsi_cmd_pool);
957 virtscsi_cmd_pool = NULL;
958 }
959 if (virtscsi_cmd_cache) {
960 kmem_cache_destroy(virtscsi_cmd_cache);
961 virtscsi_cmd_cache = NULL;
962 }
963 return ret;
964}
965
966static void __exit fini(void)
967{
968 unregister_virtio_driver(&virtio_scsi_driver);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100969 mempool_destroy(virtscsi_cmd_pool);
970 kmem_cache_destroy(virtscsi_cmd_cache);
971}
972module_init(init);
973module_exit(fini);
974
975MODULE_DEVICE_TABLE(virtio, id_table);
976MODULE_DESCRIPTION("Virtio SCSI HBA driver");
977MODULE_LICENSE("GPL");