Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-block.git] / drivers / scsi / virtio_scsi.c
CommitLineData
f33f5fe2 1// SPDX-License-Identifier: GPL-2.0-or-later
4fe74b1c
PB
2/*
3 * Virtio SCSI HBA driver
4 *
5 * Copyright IBM Corp. 2010
6 * Copyright Red Hat, Inc. 2011
7 *
8 * Authors:
9 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
4fe74b1c
PB
11 */
12
ba06d1e1
WG
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
4fe74b1c
PB
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/mempool.h>
0d9f0a52 18#include <linux/interrupt.h>
4fe74b1c
PB
19#include <linux/virtio.h>
20#include <linux/virtio_ids.h>
21#include <linux/virtio_config.h>
22#include <linux/virtio_scsi.h>
9141a4ca 23#include <linux/cpu.h>
e6dc783a 24#include <linux/blkdev.h>
4fe74b1c
PB
25#include <scsi/scsi_host.h>
26#include <scsi/scsi_device.h>
27#include <scsi/scsi_cmnd.h>
761f1193 28#include <scsi/scsi_tcq.h>
25d1d50e 29#include <scsi/scsi_devinfo.h>
938ece71 30#include <linux/seqlock.h>
0d9f0a52 31#include <linux/blk-mq-virtio.h>
4fe74b1c
PB
32
33#define VIRTIO_SCSI_MEMPOOL_SZ 64
365a7150 34#define VIRTIO_SCSI_EVENT_LEN 8
9141a4ca 35#define VIRTIO_SCSI_VQ_BASE 2
4fe74b1c
PB
36
37/* Command queue element */
38struct virtio_scsi_cmd {
39 struct scsi_cmnd *sc;
40 struct completion *comp;
41 union {
42 struct virtio_scsi_cmd_req cmd;
e6dc783a 43 struct virtio_scsi_cmd_req_pi cmd_pi;
4fe74b1c
PB
44 struct virtio_scsi_ctrl_tmf_req tmf;
45 struct virtio_scsi_ctrl_an_req an;
46 } req;
47 union {
48 struct virtio_scsi_cmd_resp cmd;
49 struct virtio_scsi_ctrl_tmf_resp tmf;
50 struct virtio_scsi_ctrl_an_resp an;
51 struct virtio_scsi_event evt;
52 } resp;
53} ____cacheline_aligned_in_smp;
54
365a7150
CM
55struct virtio_scsi_event_node {
56 struct virtio_scsi *vscsi;
57 struct virtio_scsi_event event;
58 struct work_struct work;
59};
60
139fe45a
PB
61struct virtio_scsi_vq {
62 /* Protects vq */
63 spinlock_t vq_lock;
64
65 struct virtqueue *vq;
66};
67
4fe74b1c
PB
68/* Driver instance state */
69struct virtio_scsi {
4fe74b1c 70 struct virtio_device *vdev;
2bd37f0f 71
365a7150
CM
72 /* Get some buffers ready for event vq */
73 struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
9141a4ca
PB
74
75 u32 num_queues;
76
77 /* If the affinity hint is set for virtqueues */
78 bool affinity_hint_set;
79
8904f5a5 80 struct hlist_node node;
285e71ea 81
e67423c7
MT
82 /* Protected by event_vq lock */
83 bool stop_events;
84
9141a4ca
PB
85 struct virtio_scsi_vq ctrl_vq;
86 struct virtio_scsi_vq event_vq;
87 struct virtio_scsi_vq req_vqs[];
4fe74b1c
PB
88};
89
90static struct kmem_cache *virtscsi_cmd_cache;
91static mempool_t *virtscsi_cmd_pool;
92
93static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
94{
95 return vdev->priv;
96}
97
98static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
99{
ae3d56d8 100 if (resid)
4fe74b1c 101 scsi_set_resid(sc, resid);
4fe74b1c
PB
102}
103
104/**
105 * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
106 *
107 * Called with vq_lock held.
108 */
7f82b3c9 109static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
4fe74b1c
PB
110{
111 struct virtio_scsi_cmd *cmd = buf;
112 struct scsi_cmnd *sc = cmd->sc;
113 struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
114
115 dev_dbg(&sc->device->sdev_gendev,
116 "cmd %p response %u status %#02x sense_len %u\n",
117 sc, resp->response, resp->status, resp->sense_len);
118
119 sc->result = resp->status;
d75dff39 120 virtscsi_compute_resid(sc, virtio32_to_cpu(vscsi->vdev, resp->resid));
4fe74b1c
PB
121 switch (resp->response) {
122 case VIRTIO_SCSI_S_OK:
123 set_host_byte(sc, DID_OK);
124 break;
125 case VIRTIO_SCSI_S_OVERRUN:
126 set_host_byte(sc, DID_ERROR);
127 break;
128 case VIRTIO_SCSI_S_ABORTED:
129 set_host_byte(sc, DID_ABORT);
130 break;
131 case VIRTIO_SCSI_S_BAD_TARGET:
132 set_host_byte(sc, DID_BAD_TARGET);
133 break;
134 case VIRTIO_SCSI_S_RESET:
135 set_host_byte(sc, DID_RESET);
136 break;
137 case VIRTIO_SCSI_S_BUSY:
138 set_host_byte(sc, DID_BUS_BUSY);
139 break;
140 case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
141 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
142 break;
143 case VIRTIO_SCSI_S_TARGET_FAILURE:
144 set_host_byte(sc, DID_TARGET_FAILURE);
145 break;
146 case VIRTIO_SCSI_S_NEXUS_FAILURE:
147 set_host_byte(sc, DID_NEXUS_FAILURE);
148 break;
149 default:
150 scmd_printk(KERN_WARNING, sc, "Unknown response %d",
151 resp->response);
152 /* fall through */
153 case VIRTIO_SCSI_S_FAILURE:
154 set_host_byte(sc, DID_ERROR);
155 break;
156 }
157
d75dff39
MT
158 WARN_ON(virtio32_to_cpu(vscsi->vdev, resp->sense_len) >
159 VIRTIO_SCSI_SENSE_SIZE);
4fe74b1c
PB
160 if (sc->sense_buffer) {
161 memcpy(sc->sense_buffer, resp->sense,
d75dff39
MT
162 min_t(u32,
163 virtio32_to_cpu(vscsi->vdev, resp->sense_len),
164 VIRTIO_SCSI_SENSE_SIZE));
4fe74b1c
PB
165 if (resp->sense_len)
166 set_driver_byte(sc, DRIVER_SENSE);
167 }
168
4fe74b1c
PB
169 sc->scsi_done(sc);
170}
171
10f34f64
PB
172static void virtscsi_vq_done(struct virtio_scsi *vscsi,
173 struct virtio_scsi_vq *virtscsi_vq,
7f82b3c9 174 void (*fn)(struct virtio_scsi *vscsi, void *buf))
4fe74b1c 175{
4fe74b1c 176 void *buf;
4fe74b1c 177 unsigned int len;
10f34f64
PB
178 unsigned long flags;
179 struct virtqueue *vq = virtscsi_vq->vq;
4fe74b1c 180
10f34f64 181 spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
4fe74b1c
PB
182 do {
183 virtqueue_disable_cb(vq);
184 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
7f82b3c9 185 fn(vscsi, buf);
2bf4fd31
HG
186
187 if (unlikely(virtqueue_is_broken(vq)))
188 break;
4fe74b1c 189 } while (!virtqueue_enable_cb(vq));
10f34f64 190 spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
4fe74b1c
PB
191}
192
193static void virtscsi_req_done(struct virtqueue *vq)
194{
139fe45a
PB
195 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
196 struct virtio_scsi *vscsi = shost_priv(sh);
9141a4ca
PB
197 int index = vq->index - VIRTIO_SCSI_VQ_BASE;
198 struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index];
199
9141a4ca 200 virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
4fe74b1c
PB
201};
202
8faeb529
PB
203static void virtscsi_poll_requests(struct virtio_scsi *vscsi)
204{
205 int i, num_vqs;
206
207 num_vqs = vscsi->num_queues;
208 for (i = 0; i < num_vqs; i++)
209 virtscsi_vq_done(vscsi, &vscsi->req_vqs[i],
210 virtscsi_complete_cmd);
211}
212
7f82b3c9 213static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
4fe74b1c
PB
214{
215 struct virtio_scsi_cmd *cmd = buf;
216
217 if (cmd->comp)
e8f81420 218 complete(cmd->comp);
4fe74b1c
PB
219}
220
221static void virtscsi_ctrl_done(struct virtqueue *vq)
222{
139fe45a
PB
223 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
224 struct virtio_scsi *vscsi = shost_priv(sh);
139fe45a 225
10f34f64 226 virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
4fe74b1c
PB
227};
228
cdda0e5a
PB
229static void virtscsi_handle_event(struct work_struct *work);
230
365a7150
CM
231static int virtscsi_kick_event(struct virtio_scsi *vscsi,
232 struct virtio_scsi_event_node *event_node)
233{
4614e51c 234 int err;
365a7150
CM
235 struct scatterlist sg;
236 unsigned long flags;
237
cdda0e5a 238 INIT_WORK(&event_node->work, virtscsi_handle_event);
2e9c9dfd 239 sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
365a7150
CM
240
241 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
242
bf958291
RR
243 err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
244 GFP_ATOMIC);
4614e51c 245 if (!err)
365a7150
CM
246 virtqueue_kick(vscsi->event_vq.vq);
247
248 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
249
4614e51c 250 return err;
365a7150
CM
251}
252
253static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
254{
255 int i;
256
257 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
258 vscsi->event_list[i].vscsi = vscsi;
259 virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
260 }
261
262 return 0;
263}
264
265static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
266{
267 int i;
268
e67423c7
MT
269 /* Stop scheduling work before calling cancel_work_sync. */
270 spin_lock_irq(&vscsi->event_vq.vq_lock);
271 vscsi->stop_events = true;
272 spin_unlock_irq(&vscsi->event_vq.vq_lock);
273
365a7150
CM
274 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
275 cancel_work_sync(&vscsi->event_list[i].work);
276}
277
278static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
9141a4ca 279 struct virtio_scsi_event *event)
365a7150
CM
280{
281 struct scsi_device *sdev;
282 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
283 unsigned int target = event->lun[1];
284 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
285
d75dff39 286 switch (virtio32_to_cpu(vscsi->vdev, event->reason)) {
365a7150
CM
287 case VIRTIO_SCSI_EVT_RESET_RESCAN:
288 scsi_add_device(shost, 0, target, lun);
289 break;
290 case VIRTIO_SCSI_EVT_RESET_REMOVED:
291 sdev = scsi_device_lookup(shost, 0, target, lun);
292 if (sdev) {
293 scsi_remove_device(sdev);
294 scsi_device_put(sdev);
295 } else {
296 pr_err("SCSI device %d 0 %d %d not found\n",
297 shost->host_no, target, lun);
298 }
299 break;
300 default:
301 pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
302 }
303}
304
865b58c0
PB
305static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
306 struct virtio_scsi_event *event)
307{
308 struct scsi_device *sdev;
309 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
310 unsigned int target = event->lun[1];
311 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
d75dff39
MT
312 u8 asc = virtio32_to_cpu(vscsi->vdev, event->reason) & 255;
313 u8 ascq = virtio32_to_cpu(vscsi->vdev, event->reason) >> 8;
865b58c0
PB
314
315 sdev = scsi_device_lookup(shost, 0, target, lun);
316 if (!sdev) {
317 pr_err("SCSI device %d 0 %d %d not found\n",
318 shost->host_no, target, lun);
319 return;
320 }
321
322 /* Handle "Parameters changed", "Mode parameters changed", and
323 "Capacity data has changed". */
324 if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
325 scsi_rescan_device(&sdev->sdev_gendev);
326
327 scsi_device_put(sdev);
328}
329
365a7150
CM
330static void virtscsi_handle_event(struct work_struct *work)
331{
332 struct virtio_scsi_event_node *event_node =
333 container_of(work, struct virtio_scsi_event_node, work);
334 struct virtio_scsi *vscsi = event_node->vscsi;
335 struct virtio_scsi_event *event = &event_node->event;
336
d75dff39
MT
337 if (event->event &
338 cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
339 event->event &= ~cpu_to_virtio32(vscsi->vdev,
340 VIRTIO_SCSI_T_EVENTS_MISSED);
365a7150
CM
341 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
342 }
343
d75dff39 344 switch (virtio32_to_cpu(vscsi->vdev, event->event)) {
365a7150
CM
345 case VIRTIO_SCSI_T_NO_EVENT:
346 break;
347 case VIRTIO_SCSI_T_TRANSPORT_RESET:
348 virtscsi_handle_transport_reset(vscsi, event);
349 break;
865b58c0
PB
350 case VIRTIO_SCSI_T_PARAM_CHANGE:
351 virtscsi_handle_param_change(vscsi, event);
352 break;
365a7150
CM
353 default:
354 pr_err("Unsupport virtio scsi event %x\n", event->event);
355 }
356 virtscsi_kick_event(vscsi, event_node);
357}
358
7f82b3c9 359static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
365a7150
CM
360{
361 struct virtio_scsi_event_node *event_node = buf;
362
e67423c7
MT
363 if (!vscsi->stop_events)
364 queue_work(system_freezable_wq, &event_node->work);
365a7150
CM
365}
366
4fe74b1c
PB
367static void virtscsi_event_done(struct virtqueue *vq)
368{
139fe45a
PB
369 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
370 struct virtio_scsi *vscsi = shost_priv(sh);
139fe45a 371
10f34f64 372 virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
4fe74b1c
PB
373};
374
4fe74b1c 375/**
682993b4
WG
376 * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
377 * @vq : the struct virtqueue we're talking about
4fe74b1c 378 * @cmd : command structure
4fe74b1c
PB
379 * @req_size : size of the request buffer
380 * @resp_size : size of the response buffer
4fe74b1c 381 */
682993b4
WG
382static int virtscsi_add_cmd(struct virtqueue *vq,
383 struct virtio_scsi_cmd *cmd,
c77fba9a 384 size_t req_size, size_t resp_size)
4fe74b1c
PB
385{
386 struct scsi_cmnd *sc = cmd->sc;
e6dc783a 387 struct scatterlist *sgs[6], req, resp;
682993b4
WG
388 struct sg_table *out, *in;
389 unsigned out_num = 0, in_num = 0;
390
391 out = in = NULL;
392
393 if (sc && sc->sc_data_direction != DMA_NONE) {
394 if (sc->sc_data_direction != DMA_FROM_DEVICE)
ae3d56d8 395 out = &sc->sdb.table;
682993b4 396 if (sc->sc_data_direction != DMA_TO_DEVICE)
ae3d56d8 397 in = &sc->sdb.table;
682993b4 398 }
4fe74b1c 399
4fe74b1c 400 /* Request header. */
682993b4
WG
401 sg_init_one(&req, &cmd->req, req_size);
402 sgs[out_num++] = &req;
4fe74b1c
PB
403
404 /* Data-out buffer. */
e6dc783a
NB
405 if (out) {
406 /* Place WRITE protection SGLs before Data OUT payload */
407 if (scsi_prot_sg_count(sc))
408 sgs[out_num++] = scsi_prot_sglist(sc);
682993b4 409 sgs[out_num++] = out->sgl;
e6dc783a 410 }
4fe74b1c
PB
411
412 /* Response header. */
682993b4
WG
413 sg_init_one(&resp, &cmd->resp, resp_size);
414 sgs[out_num + in_num++] = &resp;
4fe74b1c
PB
415
416 /* Data-in buffer */
e6dc783a
NB
417 if (in) {
418 /* Place READ protection SGLs before Data IN payload */
419 if (scsi_prot_sg_count(sc))
420 sgs[out_num + in_num++] = scsi_prot_sglist(sc);
682993b4 421 sgs[out_num + in_num++] = in->sgl;
e6dc783a 422 }
4fe74b1c 423
c77fba9a 424 return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
4fe74b1c
PB
425}
426
682993b4 427static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
4fe74b1c 428 struct virtio_scsi_cmd *cmd,
c77fba9a 429 size_t req_size, size_t resp_size)
4fe74b1c 430{
4fe74b1c 431 unsigned long flags;
4614e51c
RR
432 int err;
433 bool needs_kick = false;
4fe74b1c 434
682993b4 435 spin_lock_irqsave(&vq->vq_lock, flags);
c77fba9a 436 err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
4614e51c
RR
437 if (!err)
438 needs_kick = virtqueue_kick_prepare(vq->vq);
139fe45a 439
bce750b1 440 spin_unlock_irqrestore(&vq->vq_lock, flags);
4fe74b1c 441
4614e51c 442 if (needs_kick)
139fe45a 443 virtqueue_notify(vq->vq);
4614e51c 444 return err;
4fe74b1c
PB
445}
446
d75dff39
MT
447static void virtio_scsi_init_hdr(struct virtio_device *vdev,
448 struct virtio_scsi_cmd_req *cmd,
e6dc783a
NB
449 struct scsi_cmnd *sc)
450{
451 cmd->lun[0] = 1;
452 cmd->lun[1] = sc->device->id;
453 cmd->lun[2] = (sc->device->lun >> 8) | 0x40;
454 cmd->lun[3] = sc->device->lun & 0xff;
d75dff39 455 cmd->tag = cpu_to_virtio64(vdev, (unsigned long)sc);
e6dc783a
NB
456 cmd->task_attr = VIRTIO_SCSI_S_SIMPLE;
457 cmd->prio = 0;
458 cmd->crn = 0;
459}
460
c5c2567f 461#ifdef CONFIG_BLK_DEV_INTEGRITY
d75dff39
MT
462static void virtio_scsi_init_hdr_pi(struct virtio_device *vdev,
463 struct virtio_scsi_cmd_req_pi *cmd_pi,
e6dc783a
NB
464 struct scsi_cmnd *sc)
465{
466 struct request *rq = sc->request;
467 struct blk_integrity *bi;
468
d75dff39 469 virtio_scsi_init_hdr(vdev, (struct virtio_scsi_cmd_req *)cmd_pi, sc);
e6dc783a
NB
470
471 if (!rq || !scsi_prot_sg_count(sc))
472 return;
473
474 bi = blk_get_integrity(rq->rq_disk);
475
476 if (sc->sc_data_direction == DMA_TO_DEVICE)
d75dff39 477 cmd_pi->pi_bytesout = cpu_to_virtio32(vdev,
cdcdcaae
GE
478 bio_integrity_bytes(bi,
479 blk_rq_sectors(rq)));
e6dc783a 480 else if (sc->sc_data_direction == DMA_FROM_DEVICE)
d75dff39 481 cmd_pi->pi_bytesin = cpu_to_virtio32(vdev,
cdcdcaae
GE
482 bio_integrity_bytes(bi,
483 blk_rq_sectors(rq)));
e6dc783a 484}
c5c2567f 485#endif
e6dc783a 486
c3506df8
ML
487static struct virtio_scsi_vq *virtscsi_pick_vq_mq(struct virtio_scsi *vscsi,
488 struct scsi_cmnd *sc)
489{
490 u32 tag = blk_mq_unique_tag(sc->request);
491 u16 hwq = blk_mq_unique_tag_to_hwq(tag);
492
493 return &vscsi->req_vqs[hwq];
494}
495
496static int virtscsi_queuecommand(struct Scsi_Host *shost,
9141a4ca 497 struct scsi_cmnd *sc)
4fe74b1c 498{
c3506df8
ML
499 struct virtio_scsi *vscsi = shost_priv(shost);
500 struct virtio_scsi_vq *req_vq = virtscsi_pick_vq_mq(vscsi, sc);
b54197c4 501 struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc);
773c7220 502 unsigned long flags;
ed9ea4ed 503 int req_size;
773c7220 504 int ret;
b54197c4 505
2bd37f0f
PB
506 BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
507
508 /* TODO: check feature bit and fail if unsupported? */
509 BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
510
4fe74b1c
PB
511 dev_dbg(&sc->device->sdev_gendev,
512 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
513
4fe74b1c 514 cmd->sc = sc;
4fe74b1c
PB
515
516 BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
4fe74b1c 517
c5c2567f 518#ifdef CONFIG_BLK_DEV_INTEGRITY
e6dc783a 519 if (virtio_has_feature(vscsi->vdev, VIRTIO_SCSI_F_T10_PI)) {
d75dff39 520 virtio_scsi_init_hdr_pi(vscsi->vdev, &cmd->req.cmd_pi, sc);
e6dc783a
NB
521 memcpy(cmd->req.cmd_pi.cdb, sc->cmnd, sc->cmd_len);
522 req_size = sizeof(cmd->req.cmd_pi);
c5c2567f
CH
523 } else
524#endif
525 {
d75dff39 526 virtio_scsi_init_hdr(vscsi->vdev, &cmd->req.cmd, sc);
e6dc783a
NB
527 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
528 req_size = sizeof(cmd->req.cmd);
529 }
530
773c7220
EF
531 ret = virtscsi_kick_cmd(req_vq, cmd, req_size, sizeof(cmd->resp.cmd));
532 if (ret == -EIO) {
533 cmd->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
534 spin_lock_irqsave(&req_vq->vq_lock, flags);
535 virtscsi_complete_cmd(vscsi, cmd);
536 spin_unlock_irqrestore(&req_vq->vq_lock, flags);
537 } else if (ret != 0) {
b54197c4 538 return SCSI_MLQUEUE_HOST_BUSY;
773c7220 539 }
b54197c4 540 return 0;
4fe74b1c
PB
541}
542
543static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
544{
545 DECLARE_COMPLETION_ONSTACK(comp);
e4594bb5 546 int ret = FAILED;
4fe74b1c
PB
547
548 cmd->comp = &comp;
682993b4 549 if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
c77fba9a 550 sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0)
e4594bb5 551 goto out;
4fe74b1c
PB
552
553 wait_for_completion(&comp);
e4594bb5
PB
554 if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
555 cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
556 ret = SUCCESS;
4fe74b1c 557
8faeb529
PB
558 /*
559 * The spec guarantees that all requests related to the TMF have
560 * been completed, but the callback might not have run yet if
561 * we're using independent interrupts (e.g. MSI). Poll the
562 * virtqueues once.
563 *
564 * In the abort case, sc->scsi_done will do nothing, because
565 * the block layer must have detected a timeout and as a result
566 * REQ_ATOM_COMPLETE has been set.
567 */
568 virtscsi_poll_requests(vscsi);
569
e4594bb5
PB
570out:
571 mempool_free(cmd, virtscsi_cmd_pool);
572 return ret;
4fe74b1c
PB
573}
574
575static int virtscsi_device_reset(struct scsi_cmnd *sc)
576{
577 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
578 struct virtio_scsi_cmd *cmd;
579
580 sdev_printk(KERN_INFO, sc->device, "device reset\n");
581 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
582 if (!cmd)
583 return FAILED;
584
585 memset(cmd, 0, sizeof(*cmd));
4fe74b1c
PB
586 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
587 .type = VIRTIO_SCSI_T_TMF,
d75dff39
MT
588 .subtype = cpu_to_virtio32(vscsi->vdev,
589 VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET),
4fe74b1c
PB
590 .lun[0] = 1,
591 .lun[1] = sc->device->id,
592 .lun[2] = (sc->device->lun >> 8) | 0x40,
593 .lun[3] = sc->device->lun & 0xff,
594 };
595 return virtscsi_tmf(vscsi, cmd);
596}
597
25d1d50e
DG
598static int virtscsi_device_alloc(struct scsi_device *sdevice)
599{
600 /*
601 * Passed through SCSI targets (e.g. with qemu's 'scsi-block')
602 * may have transfer limits which come from the host SCSI
603 * controller or something on the host side other than the
604 * target itself.
605 *
606 * To make this work properly, the hypervisor can adjust the
607 * target's VPD information to advertise these limits. But
608 * for that to work, the guest has to look at the VPD pages,
609 * which we won't do by default if it is an SPC-2 device, even
610 * if it does actually support it.
611 *
612 * So, set the blist to always try to read the VPD pages.
613 */
614 sdevice->sdev_bflags = BLIST_TRY_VPD_PAGES;
615
616 return 0;
617}
618
619
761f1193
VS
620/**
621 * virtscsi_change_queue_depth() - Change a virtscsi target's queue depth
622 * @sdev: Virtscsi target whose queue depth to change
623 * @qdepth: New queue depth
761f1193 624 */
db5ed4df 625static int virtscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
761f1193
VS
626{
627 struct Scsi_Host *shost = sdev->host;
628 int max_depth = shost->cmd_per_lun;
629
db5ed4df 630 return scsi_change_queue_depth(sdev, min(max_depth, qdepth));
761f1193
VS
631}
632
4fe74b1c
PB
633static int virtscsi_abort(struct scsi_cmnd *sc)
634{
635 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
636 struct virtio_scsi_cmd *cmd;
637
638 scmd_printk(KERN_INFO, sc, "abort\n");
639 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
640 if (!cmd)
641 return FAILED;
642
643 memset(cmd, 0, sizeof(*cmd));
4fe74b1c
PB
644 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
645 .type = VIRTIO_SCSI_T_TMF,
646 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
647 .lun[0] = 1,
648 .lun[1] = sc->device->id,
649 .lun[2] = (sc->device->lun >> 8) | 0x40,
650 .lun[3] = sc->device->lun & 0xff,
d75dff39 651 .tag = cpu_to_virtio64(vscsi->vdev, (unsigned long)sc),
4fe74b1c
PB
652 };
653 return virtscsi_tmf(vscsi, cmd);
654}
655
0d9f0a52
CH
656static int virtscsi_map_queues(struct Scsi_Host *shost)
657{
658 struct virtio_scsi *vscsi = shost_priv(shost);
6343e3ef 659 struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
0d9f0a52 660
ed76e329 661 return blk_mq_virtio_map_queues(qmap, vscsi->vdev, 2);
0d9f0a52
CH
662}
663
e72c9a2a
PB
664/*
665 * The host guarantees to respond to each command, although I/O
666 * latencies might be higher than on bare metal. Reset the timer
667 * unconditionally to give the host a chance to perform EH.
668 */
669static enum blk_eh_timer_return virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
670{
671 return BLK_EH_RESET_TIMER;
672}
673
c3506df8 674static struct scsi_host_template virtscsi_host_template = {
4fe74b1c
PB
675 .module = THIS_MODULE,
676 .name = "Virtio SCSI HBA",
677 .proc_name = "virtio_scsi",
4fe74b1c 678 .this_id = -1,
b54197c4 679 .cmd_size = sizeof(struct virtio_scsi_cmd),
c3506df8 680 .queuecommand = virtscsi_queuecommand,
761f1193 681 .change_queue_depth = virtscsi_change_queue_depth,
4fe74b1c
PB
682 .eh_abort_handler = virtscsi_abort,
683 .eh_device_reset_handler = virtscsi_device_reset,
e72c9a2a 684 .eh_timed_out = virtscsi_eh_timed_out,
a680f1d4 685 .slave_alloc = virtscsi_device_alloc,
4fe74b1c 686
4fe74b1c 687 .dma_boundary = UINT_MAX,
0d9f0a52 688 .map_queues = virtscsi_map_queues,
c40ecc12 689 .track_queue_depth = 1,
b5b6e8c8 690 .force_blk_mq = 1,
4fe74b1c
PB
691};
692
693#define virtscsi_config_get(vdev, fld) \
694 ({ \
695 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
855e0c52 696 virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
4fe74b1c
PB
697 __val; \
698 })
699
700#define virtscsi_config_set(vdev, fld, val) \
855e0c52 701 do { \
4fe74b1c 702 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
855e0c52
RR
703 virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
704 } while(0)
4fe74b1c 705
139fe45a
PB
706static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
707 struct virtqueue *vq)
708{
709 spin_lock_init(&virtscsi_vq->vq_lock);
710 virtscsi_vq->vq = vq;
711}
712
2bd37f0f
PB
713static void virtscsi_remove_vqs(struct virtio_device *vdev)
714{
2bd37f0f
PB
715 /* Stop all the virtqueues. */
716 vdev->config->reset(vdev);
2bd37f0f
PB
717 vdev->config->del_vqs(vdev);
718}
719
4fe74b1c 720static int virtscsi_init(struct virtio_device *vdev,
5c370194 721 struct virtio_scsi *vscsi)
4fe74b1c
PB
722{
723 int err;
9141a4ca
PB
724 u32 i;
725 u32 num_vqs;
726 vq_callback_t **callbacks;
727 const char **names;
728 struct virtqueue **vqs;
0d9f0a52 729 struct irq_affinity desc = { .pre_vectors = 2 };
9141a4ca
PB
730
731 num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
6da2ec56
KC
732 vqs = kmalloc_array(num_vqs, sizeof(struct virtqueue *), GFP_KERNEL);
733 callbacks = kmalloc_array(num_vqs, sizeof(vq_callback_t *),
734 GFP_KERNEL);
735 names = kmalloc_array(num_vqs, sizeof(char *), GFP_KERNEL);
9141a4ca
PB
736
737 if (!callbacks || !vqs || !names) {
738 err = -ENOMEM;
739 goto out;
740 }
2bd37f0f 741
9141a4ca
PB
742 callbacks[0] = virtscsi_ctrl_done;
743 callbacks[1] = virtscsi_event_done;
744 names[0] = "control";
745 names[1] = "event";
746 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
747 callbacks[i] = virtscsi_req_done;
748 names[i] = "request";
749 }
4fe74b1c
PB
750
751 /* Discover virtqueues and write information to configuration. */
9b2bbdb2 752 err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
4fe74b1c 753 if (err)
9141a4ca 754 goto out;
4fe74b1c 755
139fe45a
PB
756 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
757 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
9141a4ca
PB
758 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
759 virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
760 vqs[i]);
761
4fe74b1c
PB
762 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
763 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
2bd37f0f 764
9141a4ca
PB
765 err = 0;
766
767out:
768 kfree(names);
769 kfree(callbacks);
770 kfree(vqs);
771 if (err)
772 virtscsi_remove_vqs(vdev);
2bd37f0f 773 return err;
4fe74b1c
PB
774}
775
6f039790 776static int virtscsi_probe(struct virtio_device *vdev)
4fe74b1c
PB
777{
778 struct Scsi_Host *shost;
779 struct virtio_scsi *vscsi;
908a5544 780 int err;
2bd37f0f 781 u32 sg_elems, num_targets;
4fe74b1c 782 u32 cmd_per_lun;
9141a4ca 783 u32 num_queues;
9141a4ca 784
8cab3cd6
MT
785 if (!vdev->config->get) {
786 dev_err(&vdev->dev, "%s failure: config access disabled\n",
787 __func__);
788 return -EINVAL;
789 }
790
9141a4ca
PB
791 /* We need to know how many queues before we allocate. */
792 num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
1978f30a 793 num_queues = min_t(unsigned int, nr_cpu_ids, num_queues);
4fe74b1c 794
2bd37f0f 795 num_targets = virtscsi_config_get(vdev, max_target) + 1;
4fe74b1c 796
c3506df8 797 shost = scsi_host_alloc(&virtscsi_host_template,
9141a4ca 798 sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
4fe74b1c
PB
799 if (!shost)
800 return -ENOMEM;
801
2bd37f0f 802 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
4fe74b1c
PB
803 shost->sg_tablesize = sg_elems;
804 vscsi = shost_priv(shost);
805 vscsi->vdev = vdev;
9141a4ca 806 vscsi->num_queues = num_queues;
4fe74b1c
PB
807 vdev->priv = shost;
808
5c370194 809 err = virtscsi_init(vdev, vscsi);
4fe74b1c
PB
810 if (err)
811 goto virtscsi_init_failed;
812
582b0ab2
RJ
813 shost->can_queue = virtqueue_get_vring_size(vscsi->req_vqs[0].vq);
814
4fe74b1c
PB
815 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
816 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
817 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
9da5f5ac
PB
818
819 /* LUNs > 256 are reported with format 1, so they go in the range
820 * 16640-32767.
821 */
822 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
2bd37f0f 823 shost->max_id = num_targets;
4fe74b1c
PB
824 shost->max_channel = 0;
825 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
ccbedf11 826 shost->nr_hw_queues = num_queues;
e6dc783a 827
c5c2567f 828#ifdef CONFIG_BLK_DEV_INTEGRITY
e6dc783a 829 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) {
908a5544
SR
830 int host_prot;
831
e6dc783a
NB
832 host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
833 SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
834 SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
835
836 scsi_host_set_prot(shost, host_prot);
837 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
838 }
c5c2567f 839#endif
e6dc783a 840
4fe74b1c
PB
841 err = scsi_add_host(shost, &vdev->dev);
842 if (err)
843 goto scsi_add_host_failed;
5d8f16d0
MT
844
845 virtio_device_ready(vdev);
846
847 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
848 virtscsi_kick_event_all(vscsi);
849
850 scsi_scan_host(shost);
4fe74b1c
PB
851 return 0;
852
853scsi_add_host_failed:
854 vdev->config->del_vqs(vdev);
855virtscsi_init_failed:
856 scsi_host_put(shost);
857 return err;
858}
859
6f039790 860static void virtscsi_remove(struct virtio_device *vdev)
4fe74b1c
PB
861{
862 struct Scsi_Host *shost = virtio_scsi_host(vdev);
365a7150
CM
863 struct virtio_scsi *vscsi = shost_priv(shost);
864
865 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
866 virtscsi_cancel_event_work(vscsi);
4fe74b1c
PB
867
868 scsi_remove_host(shost);
4fe74b1c
PB
869 virtscsi_remove_vqs(vdev);
870 scsi_host_put(shost);
871}
872
89107000 873#ifdef CONFIG_PM_SLEEP
4fe74b1c
PB
874static int virtscsi_freeze(struct virtio_device *vdev)
875{
876 virtscsi_remove_vqs(vdev);
877 return 0;
878}
879
880static int virtscsi_restore(struct virtio_device *vdev)
881{
882 struct Scsi_Host *sh = virtio_scsi_host(vdev);
883 struct virtio_scsi *vscsi = shost_priv(sh);
f466f753
AH
884 int err;
885
886 err = virtscsi_init(vdev, vscsi);
887 if (err)
888 return err;
889
52c9cf1a
MT
890 virtio_device_ready(vdev);
891
cd679048
MT
892 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
893 virtscsi_kick_event_all(vscsi);
4fe74b1c 894
f466f753 895 return err;
4fe74b1c
PB
896}
897#endif
898
899static struct virtio_device_id id_table[] = {
900 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
901 { 0 },
902};
903
365a7150 904static unsigned int features[] = {
865b58c0
PB
905 VIRTIO_SCSI_F_HOTPLUG,
906 VIRTIO_SCSI_F_CHANGE,
c5c2567f 907#ifdef CONFIG_BLK_DEV_INTEGRITY
e6dc783a 908 VIRTIO_SCSI_F_T10_PI,
c5c2567f 909#endif
365a7150
CM
910};
911
4fe74b1c 912static struct virtio_driver virtio_scsi_driver = {
365a7150
CM
913 .feature_table = features,
914 .feature_table_size = ARRAY_SIZE(features),
4fe74b1c
PB
915 .driver.name = KBUILD_MODNAME,
916 .driver.owner = THIS_MODULE,
917 .id_table = id_table,
918 .probe = virtscsi_probe,
89107000 919#ifdef CONFIG_PM_SLEEP
4fe74b1c
PB
920 .freeze = virtscsi_freeze,
921 .restore = virtscsi_restore,
922#endif
6f039790 923 .remove = virtscsi_remove,
4fe74b1c
PB
924};
925
926static int __init init(void)
927{
928 int ret = -ENOMEM;
929
930 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
931 if (!virtscsi_cmd_cache) {
ba06d1e1 932 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
4fe74b1c
PB
933 goto error;
934 }
935
936
937 virtscsi_cmd_pool =
938 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
939 virtscsi_cmd_cache);
940 if (!virtscsi_cmd_pool) {
ba06d1e1 941 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
4fe74b1c
PB
942 goto error;
943 }
944 ret = register_virtio_driver(&virtio_scsi_driver);
945 if (ret < 0)
946 goto error;
947
948 return 0;
949
950error:
951 if (virtscsi_cmd_pool) {
952 mempool_destroy(virtscsi_cmd_pool);
953 virtscsi_cmd_pool = NULL;
954 }
955 if (virtscsi_cmd_cache) {
956 kmem_cache_destroy(virtscsi_cmd_cache);
957 virtscsi_cmd_cache = NULL;
958 }
959 return ret;
960}
961
962static void __exit fini(void)
963{
964 unregister_virtio_driver(&virtio_scsi_driver);
965 mempool_destroy(virtscsi_cmd_pool);
966 kmem_cache_destroy(virtscsi_cmd_cache);
967}
968module_init(init);
969module_exit(fini);
970
971MODULE_DEVICE_TABLE(virtio, id_table);
972MODULE_DESCRIPTION("Virtio SCSI HBA driver");
973MODULE_LICENSE("GPL");