Merge branch 'x86-topology-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / scsi / virtio_scsi.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
11  */
12
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/mempool.h>
18 #include <linux/interrupt.h>
19 #include <linux/virtio.h>
20 #include <linux/virtio_ids.h>
21 #include <linux/virtio_config.h>
22 #include <linux/virtio_scsi.h>
23 #include <linux/cpu.h>
24 #include <linux/blkdev.h>
25 #include <scsi/scsi_host.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_tcq.h>
29 #include <scsi/scsi_devinfo.h>
30 #include <linux/seqlock.h>
31 #include <linux/blk-mq-virtio.h>
32
33 #define VIRTIO_SCSI_MEMPOOL_SZ 64
34 #define VIRTIO_SCSI_EVENT_LEN 8
35 #define VIRTIO_SCSI_VQ_BASE 2
36
37 /* Command queue element */
38 struct virtio_scsi_cmd {
39         struct scsi_cmnd *sc;
40         struct completion *comp;
41         union {
42                 struct virtio_scsi_cmd_req       cmd;
43                 struct virtio_scsi_cmd_req_pi    cmd_pi;
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
55 struct virtio_scsi_event_node {
56         struct virtio_scsi *vscsi;
57         struct virtio_scsi_event event;
58         struct work_struct work;
59 };
60
61 struct virtio_scsi_vq {
62         /* Protects vq */
63         spinlock_t vq_lock;
64
65         struct virtqueue *vq;
66 };
67
68 /* Driver instance state */
69 struct virtio_scsi {
70         struct virtio_device *vdev;
71
72         /* Get some buffers ready for event vq */
73         struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
74
75         u32 num_queues;
76
77         /* If the affinity hint is set for virtqueues */
78         bool affinity_hint_set;
79
80         struct hlist_node node;
81
82         /* Protected by event_vq lock */
83         bool stop_events;
84
85         struct virtio_scsi_vq ctrl_vq;
86         struct virtio_scsi_vq event_vq;
87         struct virtio_scsi_vq req_vqs[];
88 };
89
90 static struct kmem_cache *virtscsi_cmd_cache;
91 static mempool_t *virtscsi_cmd_pool;
92
93 static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
94 {
95         return vdev->priv;
96 }
97
98 static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
99 {
100         if (resid)
101                 scsi_set_resid(sc, resid);
102 }
103
104 /**
105  * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
106  *
107  * Called with vq_lock held.
108  */
109 static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
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;
120         virtscsi_compute_resid(sc, virtio32_to_cpu(vscsi->vdev, resp->resid));
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
158         WARN_ON(virtio32_to_cpu(vscsi->vdev, resp->sense_len) >
159                 VIRTIO_SCSI_SENSE_SIZE);
160         if (sc->sense_buffer) {
161                 memcpy(sc->sense_buffer, resp->sense,
162                        min_t(u32,
163                              virtio32_to_cpu(vscsi->vdev, resp->sense_len),
164                              VIRTIO_SCSI_SENSE_SIZE));
165                 if (resp->sense_len)
166                         set_driver_byte(sc, DRIVER_SENSE);
167         }
168
169         sc->scsi_done(sc);
170 }
171
172 static void virtscsi_vq_done(struct virtio_scsi *vscsi,
173                              struct virtio_scsi_vq *virtscsi_vq,
174                              void (*fn)(struct virtio_scsi *vscsi, void *buf))
175 {
176         void *buf;
177         unsigned int len;
178         unsigned long flags;
179         struct virtqueue *vq = virtscsi_vq->vq;
180
181         spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
182         do {
183                 virtqueue_disable_cb(vq);
184                 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
185                         fn(vscsi, buf);
186
187                 if (unlikely(virtqueue_is_broken(vq)))
188                         break;
189         } while (!virtqueue_enable_cb(vq));
190         spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
191 }
192
193 static void virtscsi_req_done(struct virtqueue *vq)
194 {
195         struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
196         struct virtio_scsi *vscsi = shost_priv(sh);
197         int index = vq->index - VIRTIO_SCSI_VQ_BASE;
198         struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index];
199
200         virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
201 };
202
203 static 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
213 static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
214 {
215         struct virtio_scsi_cmd *cmd = buf;
216
217         if (cmd->comp)
218                 complete(cmd->comp);
219 }
220
221 static void virtscsi_ctrl_done(struct virtqueue *vq)
222 {
223         struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
224         struct virtio_scsi *vscsi = shost_priv(sh);
225
226         virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
227 };
228
229 static void virtscsi_handle_event(struct work_struct *work);
230
231 static int virtscsi_kick_event(struct virtio_scsi *vscsi,
232                                struct virtio_scsi_event_node *event_node)
233 {
234         int err;
235         struct scatterlist sg;
236         unsigned long flags;
237
238         INIT_WORK(&event_node->work, virtscsi_handle_event);
239         sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
240
241         spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
242
243         err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
244                                   GFP_ATOMIC);
245         if (!err)
246                 virtqueue_kick(vscsi->event_vq.vq);
247
248         spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
249
250         return err;
251 }
252
253 static 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
265 static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
266 {
267         int i;
268
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
274         for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
275                 cancel_work_sync(&vscsi->event_list[i].work);
276 }
277
278 static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
279                                             struct virtio_scsi_event *event)
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
286         switch (virtio32_to_cpu(vscsi->vdev, event->reason)) {
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
305 static 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];
312         u8 asc = virtio32_to_cpu(vscsi->vdev, event->reason) & 255;
313         u8 ascq = virtio32_to_cpu(vscsi->vdev, event->reason) >> 8;
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
330 static 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
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);
341                 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
342         }
343
344         switch (virtio32_to_cpu(vscsi->vdev, event->event)) {
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;
350         case VIRTIO_SCSI_T_PARAM_CHANGE:
351                 virtscsi_handle_param_change(vscsi, event);
352                 break;
353         default:
354                 pr_err("Unsupport virtio scsi event %x\n", event->event);
355         }
356         virtscsi_kick_event(vscsi, event_node);
357 }
358
359 static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
360 {
361         struct virtio_scsi_event_node *event_node = buf;
362
363         if (!vscsi->stop_events)
364                 queue_work(system_freezable_wq, &event_node->work);
365 }
366
367 static void virtscsi_event_done(struct virtqueue *vq)
368 {
369         struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
370         struct virtio_scsi *vscsi = shost_priv(sh);
371
372         virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
373 };
374
375 /**
376  * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
377  * @vq          : the struct virtqueue we're talking about
378  * @cmd         : command structure
379  * @req_size    : size of the request buffer
380  * @resp_size   : size of the response buffer
381  */
382 static int virtscsi_add_cmd(struct virtqueue *vq,
383                             struct virtio_scsi_cmd *cmd,
384                             size_t req_size, size_t resp_size)
385 {
386         struct scsi_cmnd *sc = cmd->sc;
387         struct scatterlist *sgs[6], req, resp;
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)
395                         out = &sc->sdb.table;
396                 if (sc->sc_data_direction != DMA_TO_DEVICE)
397                         in = &sc->sdb.table;
398         }
399
400         /* Request header.  */
401         sg_init_one(&req, &cmd->req, req_size);
402         sgs[out_num++] = &req;
403
404         /* Data-out buffer.  */
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);
409                 sgs[out_num++] = out->sgl;
410         }
411
412         /* Response header.  */
413         sg_init_one(&resp, &cmd->resp, resp_size);
414         sgs[out_num + in_num++] = &resp;
415
416         /* Data-in buffer */
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);
421                 sgs[out_num + in_num++] = in->sgl;
422         }
423
424         return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
425 }
426
427 static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
428                              struct virtio_scsi_cmd *cmd,
429                              size_t req_size, size_t resp_size)
430 {
431         unsigned long flags;
432         int err;
433         bool needs_kick = false;
434
435         spin_lock_irqsave(&vq->vq_lock, flags);
436         err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
437         if (!err)
438                 needs_kick = virtqueue_kick_prepare(vq->vq);
439
440         spin_unlock_irqrestore(&vq->vq_lock, flags);
441
442         if (needs_kick)
443                 virtqueue_notify(vq->vq);
444         return err;
445 }
446
447 static void virtio_scsi_init_hdr(struct virtio_device *vdev,
448                                  struct virtio_scsi_cmd_req *cmd,
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;
455         cmd->tag = cpu_to_virtio64(vdev, (unsigned long)sc);
456         cmd->task_attr = VIRTIO_SCSI_S_SIMPLE;
457         cmd->prio = 0;
458         cmd->crn = 0;
459 }
460
461 #ifdef CONFIG_BLK_DEV_INTEGRITY
462 static void virtio_scsi_init_hdr_pi(struct virtio_device *vdev,
463                                     struct virtio_scsi_cmd_req_pi *cmd_pi,
464                                     struct scsi_cmnd *sc)
465 {
466         struct request *rq = sc->request;
467         struct blk_integrity *bi;
468
469         virtio_scsi_init_hdr(vdev, (struct virtio_scsi_cmd_req *)cmd_pi, sc);
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)
477                 cmd_pi->pi_bytesout = cpu_to_virtio32(vdev,
478                                                       bio_integrity_bytes(bi,
479                                                         blk_rq_sectors(rq)));
480         else if (sc->sc_data_direction == DMA_FROM_DEVICE)
481                 cmd_pi->pi_bytesin = cpu_to_virtio32(vdev,
482                                                      bio_integrity_bytes(bi,
483                                                         blk_rq_sectors(rq)));
484 }
485 #endif
486
487 static 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
496 static int virtscsi_queuecommand(struct Scsi_Host *shost,
497                                  struct scsi_cmnd *sc)
498 {
499         struct virtio_scsi *vscsi = shost_priv(shost);
500         struct virtio_scsi_vq *req_vq = virtscsi_pick_vq_mq(vscsi, sc);
501         struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc);
502         unsigned long flags;
503         int req_size;
504         int ret;
505
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
511         dev_dbg(&sc->device->sdev_gendev,
512                 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
513
514         cmd->sc = sc;
515
516         BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
517
518 #ifdef CONFIG_BLK_DEV_INTEGRITY
519         if (virtio_has_feature(vscsi->vdev, VIRTIO_SCSI_F_T10_PI)) {
520                 virtio_scsi_init_hdr_pi(vscsi->vdev, &cmd->req.cmd_pi, sc);
521                 memcpy(cmd->req.cmd_pi.cdb, sc->cmnd, sc->cmd_len);
522                 req_size = sizeof(cmd->req.cmd_pi);
523         } else
524 #endif
525         {
526                 virtio_scsi_init_hdr(vscsi->vdev, &cmd->req.cmd, sc);
527                 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
528                 req_size = sizeof(cmd->req.cmd);
529         }
530
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) {
538                 return SCSI_MLQUEUE_HOST_BUSY;
539         }
540         return 0;
541 }
542
543 static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
544 {
545         DECLARE_COMPLETION_ONSTACK(comp);
546         int ret = FAILED;
547
548         cmd->comp = &comp;
549         if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
550                               sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0)
551                 goto out;
552
553         wait_for_completion(&comp);
554         if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
555             cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
556                 ret = SUCCESS;
557
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
570 out:
571         mempool_free(cmd, virtscsi_cmd_pool);
572         return ret;
573 }
574
575 static 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));
586         cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
587                 .type = VIRTIO_SCSI_T_TMF,
588                 .subtype = cpu_to_virtio32(vscsi->vdev,
589                                              VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET),
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
598 static 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
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
624  */
625 static int virtscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
626 {
627         struct Scsi_Host *shost = sdev->host;
628         int max_depth = shost->cmd_per_lun;
629
630         return scsi_change_queue_depth(sdev, min(max_depth, qdepth));
631 }
632
633 static 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));
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,
651                 .tag = cpu_to_virtio64(vscsi->vdev, (unsigned long)sc),
652         };
653         return virtscsi_tmf(vscsi, cmd);
654 }
655
656 static int virtscsi_map_queues(struct Scsi_Host *shost)
657 {
658         struct virtio_scsi *vscsi = shost_priv(shost);
659         struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
660
661         return blk_mq_virtio_map_queues(qmap, vscsi->vdev, 2);
662 }
663
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  */
669 static enum blk_eh_timer_return virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
670 {
671         return BLK_EH_RESET_TIMER;
672 }
673
674 static struct scsi_host_template virtscsi_host_template = {
675         .module = THIS_MODULE,
676         .name = "Virtio SCSI HBA",
677         .proc_name = "virtio_scsi",
678         .this_id = -1,
679         .cmd_size = sizeof(struct virtio_scsi_cmd),
680         .queuecommand = virtscsi_queuecommand,
681         .change_queue_depth = virtscsi_change_queue_depth,
682         .eh_abort_handler = virtscsi_abort,
683         .eh_device_reset_handler = virtscsi_device_reset,
684         .eh_timed_out = virtscsi_eh_timed_out,
685         .slave_alloc = virtscsi_device_alloc,
686
687         .dma_boundary = UINT_MAX,
688         .map_queues = virtscsi_map_queues,
689         .track_queue_depth = 1,
690         .force_blk_mq = 1,
691 };
692
693 #define virtscsi_config_get(vdev, fld) \
694         ({ \
695                 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
696                 virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
697                 __val; \
698         })
699
700 #define virtscsi_config_set(vdev, fld, val) \
701         do { \
702                 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
703                 virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
704         } while(0)
705
706 static 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
713 static void virtscsi_remove_vqs(struct virtio_device *vdev)
714 {
715         /* Stop all the virtqueues. */
716         vdev->config->reset(vdev);
717         vdev->config->del_vqs(vdev);
718 }
719
720 static int virtscsi_init(struct virtio_device *vdev,
721                          struct virtio_scsi *vscsi)
722 {
723         int err;
724         u32 i;
725         u32 num_vqs;
726         vq_callback_t **callbacks;
727         const char **names;
728         struct virtqueue **vqs;
729         struct irq_affinity desc = { .pre_vectors = 2 };
730
731         num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
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);
736
737         if (!callbacks || !vqs || !names) {
738                 err = -ENOMEM;
739                 goto out;
740         }
741
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         }
750
751         /* Discover virtqueues and write information to configuration.  */
752         err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
753         if (err)
754                 goto out;
755
756         virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
757         virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
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
762         virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
763         virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
764
765         err = 0;
766
767 out:
768         kfree(names);
769         kfree(callbacks);
770         kfree(vqs);
771         if (err)
772                 virtscsi_remove_vqs(vdev);
773         return err;
774 }
775
776 static int virtscsi_probe(struct virtio_device *vdev)
777 {
778         struct Scsi_Host *shost;
779         struct virtio_scsi *vscsi;
780         int err;
781         u32 sg_elems, num_targets;
782         u32 cmd_per_lun;
783         u32 num_queues;
784
785         if (!vdev->config->get) {
786                 dev_err(&vdev->dev, "%s failure: config access disabled\n",
787                         __func__);
788                 return -EINVAL;
789         }
790
791         /* We need to know how many queues before we allocate. */
792         num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
793         num_queues = min_t(unsigned int, nr_cpu_ids, num_queues);
794
795         num_targets = virtscsi_config_get(vdev, max_target) + 1;
796
797         shost = scsi_host_alloc(&virtscsi_host_template,
798                 sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
799         if (!shost)
800                 return -ENOMEM;
801
802         sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
803         shost->sg_tablesize = sg_elems;
804         vscsi = shost_priv(shost);
805         vscsi->vdev = vdev;
806         vscsi->num_queues = num_queues;
807         vdev->priv = shost;
808
809         err = virtscsi_init(vdev, vscsi);
810         if (err)
811                 goto virtscsi_init_failed;
812
813         shost->can_queue = virtqueue_get_vring_size(vscsi->req_vqs[0].vq);
814
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;
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;
823         shost->max_id = num_targets;
824         shost->max_channel = 0;
825         shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
826         shost->nr_hw_queues = num_queues;
827
828 #ifdef CONFIG_BLK_DEV_INTEGRITY
829         if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) {
830                 int host_prot;
831
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         }
839 #endif
840
841         err = scsi_add_host(shost, &vdev->dev);
842         if (err)
843                 goto scsi_add_host_failed;
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);
851         return 0;
852
853 scsi_add_host_failed:
854         vdev->config->del_vqs(vdev);
855 virtscsi_init_failed:
856         scsi_host_put(shost);
857         return err;
858 }
859
860 static void virtscsi_remove(struct virtio_device *vdev)
861 {
862         struct Scsi_Host *shost = virtio_scsi_host(vdev);
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);
867
868         scsi_remove_host(shost);
869         virtscsi_remove_vqs(vdev);
870         scsi_host_put(shost);
871 }
872
873 #ifdef CONFIG_PM_SLEEP
874 static int virtscsi_freeze(struct virtio_device *vdev)
875 {
876         virtscsi_remove_vqs(vdev);
877         return 0;
878 }
879
880 static 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);
884         int err;
885
886         err = virtscsi_init(vdev, vscsi);
887         if (err)
888                 return err;
889
890         virtio_device_ready(vdev);
891
892         if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
893                 virtscsi_kick_event_all(vscsi);
894
895         return err;
896 }
897 #endif
898
899 static struct virtio_device_id id_table[] = {
900         { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
901         { 0 },
902 };
903
904 static unsigned int features[] = {
905         VIRTIO_SCSI_F_HOTPLUG,
906         VIRTIO_SCSI_F_CHANGE,
907 #ifdef CONFIG_BLK_DEV_INTEGRITY
908         VIRTIO_SCSI_F_T10_PI,
909 #endif
910 };
911
912 static struct virtio_driver virtio_scsi_driver = {
913         .feature_table = features,
914         .feature_table_size = ARRAY_SIZE(features),
915         .driver.name = KBUILD_MODNAME,
916         .driver.owner = THIS_MODULE,
917         .id_table = id_table,
918         .probe = virtscsi_probe,
919 #ifdef CONFIG_PM_SLEEP
920         .freeze = virtscsi_freeze,
921         .restore = virtscsi_restore,
922 #endif
923         .remove = virtscsi_remove,
924 };
925
926 static 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) {
932                 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
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) {
941                 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
942                 goto error;
943         }
944         ret = register_virtio_driver(&virtio_scsi_driver);
945         if (ret < 0)
946                 goto error;
947
948         return 0;
949
950 error:
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
962 static 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 }
968 module_init(init);
969 module_exit(fini);
970
971 MODULE_DEVICE_TABLE(virtio, id_table);
972 MODULE_DESCRIPTION("Virtio SCSI HBA driver");
973 MODULE_LICENSE("GPL");