hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/scsi/virtio_scsi.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Virtio SCSI HBA driver
34 *
....@@ -7,10 +8,6 @@
78 * Authors:
89 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
910 * 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
- *
1411 */
1512
1613 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -32,6 +29,8 @@
3229 #include <scsi/scsi_devinfo.h>
3330 #include <linux/seqlock.h>
3431 #include <linux/blk-mq-virtio.h>
32
+
33
+#include "sd.h"
3534
3635 #define VIRTIO_SCSI_MEMPOOL_SZ 64
3736 #define VIRTIO_SCSI_EVENT_LEN 8
....@@ -68,33 +67,6 @@
6867 struct virtqueue *vq;
6968 };
7069
71
-/*
72
- * Per-target queue state.
73
- *
74
- * This struct holds the data needed by the queue steering policy. When a
75
- * target is sent multiple requests, we need to drive them to the same queue so
76
- * that FIFO processing order is kept. However, if a target was idle, we can
77
- * choose a queue arbitrarily. In this case the queue is chosen according to
78
- * the current VCPU, so the driver expects the number of request queues to be
79
- * equal to the number of VCPUs. This makes it easy and fast to select the
80
- * queue, and also lets the driver optimize the IRQ affinity for the virtqueues
81
- * (each virtqueue's affinity is set to the CPU that "owns" the queue).
82
- *
83
- * tgt_seq is held to serialize reading and writing req_vq.
84
- *
85
- * Decrements of reqs are never concurrent with writes of req_vq: before the
86
- * decrement reqs will be != 0; after the decrement the virtqueue completion
87
- * routine will not use the req_vq so it can be changed by a new request.
88
- * Thus they can happen outside the tgt_seq, provided of course we make reqs
89
- * an atomic_t.
90
- */
91
-struct virtio_scsi_target_state {
92
- seqcount_t tgt_seq;
93
-
94
- /* Currently active virtqueue for requests sent to this target. */
95
- struct virtio_scsi_vq *req_vq;
96
-};
97
-
9870 /* Driver instance state */
9971 struct virtio_scsi {
10072 struct virtio_device *vdev;
....@@ -103,9 +75,6 @@
10375 struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
10476
10577 u32 num_queues;
106
-
107
- /* If the affinity hint is set for virtqueues */
108
- bool affinity_hint_set;
10978
11079 struct hlist_node node;
11180
....@@ -127,19 +96,11 @@
12796
12897 static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
12998 {
130
- if (!resid)
131
- return;
132
-
133
- if (!scsi_bidi_cmnd(sc)) {
99
+ if (resid)
134100 scsi_set_resid(sc, resid);
135
- return;
136
- }
137
-
138
- scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
139
- scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
140101 }
141102
142
-/**
103
+/*
143104 * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
144105 *
145106 * Called with vq_lock held.
....@@ -187,7 +148,7 @@
187148 default:
188149 scmd_printk(KERN_WARNING, sc, "Unknown response %d",
189150 resp->response);
190
- /* fall through */
151
+ fallthrough;
191152 case VIRTIO_SCSI_S_FAILURE:
192153 set_host_byte(sc, DID_ERROR);
193154 break;
....@@ -323,7 +284,12 @@
323284
324285 switch (virtio32_to_cpu(vscsi->vdev, event->reason)) {
325286 case VIRTIO_SCSI_EVT_RESET_RESCAN:
326
- scsi_add_device(shost, 0, target, lun);
287
+ if (lun == 0) {
288
+ scsi_scan_target(&shost->shost_gendev, 0, target,
289
+ SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
290
+ } else {
291
+ scsi_add_device(shost, 0, target, lun);
292
+ }
327293 break;
328294 case VIRTIO_SCSI_EVT_RESET_REMOVED:
329295 sdev = scsi_device_lookup(shost, 0, target, lun);
....@@ -365,6 +331,44 @@
365331 scsi_device_put(sdev);
366332 }
367333
334
+static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
335
+{
336
+ struct scsi_device *sdev;
337
+ struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
338
+ unsigned char scsi_cmd[MAX_COMMAND_SIZE];
339
+ int result, inquiry_len, inq_result_len = 256;
340
+ char *inq_result = kmalloc(inq_result_len, GFP_KERNEL);
341
+
342
+ shost_for_each_device(sdev, shost) {
343
+ inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
344
+
345
+ memset(scsi_cmd, 0, sizeof(scsi_cmd));
346
+ scsi_cmd[0] = INQUIRY;
347
+ scsi_cmd[4] = (unsigned char) inquiry_len;
348
+
349
+ memset(inq_result, 0, inq_result_len);
350
+
351
+ result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
352
+ inq_result, inquiry_len, NULL,
353
+ SD_TIMEOUT, SD_MAX_RETRIES, NULL);
354
+
355
+ if (result == 0 && inq_result[0] >> 5) {
356
+ /* PQ indicates the LUN is not attached */
357
+ scsi_remove_device(sdev);
358
+ } else if (host_byte(result) == DID_BAD_TARGET) {
359
+ /*
360
+ * If all LUNs of a virtio-scsi device are unplugged
361
+ * it will respond with BAD TARGET on any INQUIRY
362
+ * command.
363
+ * Remove the device in this case as well.
364
+ */
365
+ scsi_remove_device(sdev);
366
+ }
367
+ }
368
+
369
+ kfree(inq_result);
370
+}
371
+
368372 static void virtscsi_handle_event(struct work_struct *work)
369373 {
370374 struct virtio_scsi_event_node *event_node =
....@@ -376,6 +380,7 @@
376380 cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
377381 event->event &= ~cpu_to_virtio32(vscsi->vdev,
378382 VIRTIO_SCSI_T_EVENTS_MISSED);
383
+ virtscsi_rescan_hotunplug(vscsi);
379384 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
380385 }
381386
....@@ -410,14 +415,7 @@
410415 virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
411416 };
412417
413
-/**
414
- * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
415
- * @vq : the struct virtqueue we're talking about
416
- * @cmd : command structure
417
- * @req_size : size of the request buffer
418
- * @resp_size : size of the response buffer
419
- */
420
-static int virtscsi_add_cmd(struct virtqueue *vq,
418
+static int __virtscsi_add_cmd(struct virtqueue *vq,
421419 struct virtio_scsi_cmd *cmd,
422420 size_t req_size, size_t resp_size)
423421 {
....@@ -430,9 +428,9 @@
430428
431429 if (sc && sc->sc_data_direction != DMA_NONE) {
432430 if (sc->sc_data_direction != DMA_FROM_DEVICE)
433
- out = &scsi_out(sc)->table;
431
+ out = &sc->sdb.table;
434432 if (sc->sc_data_direction != DMA_TO_DEVICE)
435
- in = &scsi_in(sc)->table;
433
+ in = &sc->sdb.table;
436434 }
437435
438436 /* Request header. */
....@@ -462,17 +460,39 @@
462460 return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
463461 }
464462
465
-static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
463
+static void virtscsi_kick_vq(struct virtio_scsi_vq *vq)
464
+{
465
+ bool needs_kick;
466
+ unsigned long flags;
467
+
468
+ spin_lock_irqsave(&vq->vq_lock, flags);
469
+ needs_kick = virtqueue_kick_prepare(vq->vq);
470
+ spin_unlock_irqrestore(&vq->vq_lock, flags);
471
+
472
+ if (needs_kick)
473
+ virtqueue_notify(vq->vq);
474
+}
475
+
476
+/**
477
+ * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue, optionally kick it
478
+ * @vq : the struct virtqueue we're talking about
479
+ * @cmd : command structure
480
+ * @req_size : size of the request buffer
481
+ * @resp_size : size of the response buffer
482
+ * @kick : whether to kick the virtqueue immediately
483
+ */
484
+static int virtscsi_add_cmd(struct virtio_scsi_vq *vq,
466485 struct virtio_scsi_cmd *cmd,
467
- size_t req_size, size_t resp_size)
486
+ size_t req_size, size_t resp_size,
487
+ bool kick)
468488 {
469489 unsigned long flags;
470490 int err;
471491 bool needs_kick = false;
472492
473493 spin_lock_irqsave(&vq->vq_lock, flags);
474
- err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
475
- if (!err)
494
+ err = __virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
495
+ if (!err && kick)
476496 needs_kick = virtqueue_kick_prepare(vq->vq);
477497
478498 spin_unlock_irqrestore(&vq->vq_lock, flags);
....@@ -537,6 +557,7 @@
537557 struct virtio_scsi *vscsi = shost_priv(shost);
538558 struct virtio_scsi_vq *req_vq = virtscsi_pick_vq_mq(vscsi, sc);
539559 struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc);
560
+ bool kick;
540561 unsigned long flags;
541562 int req_size;
542563 int ret;
....@@ -566,7 +587,8 @@
566587 req_size = sizeof(cmd->req.cmd);
567588 }
568589
569
- ret = virtscsi_kick_cmd(req_vq, cmd, req_size, sizeof(cmd->resp.cmd));
590
+ kick = (sc->flags & SCMD_LAST) != 0;
591
+ ret = virtscsi_add_cmd(req_vq, cmd, req_size, sizeof(cmd->resp.cmd), kick);
570592 if (ret == -EIO) {
571593 cmd->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
572594 spin_lock_irqsave(&req_vq->vq_lock, flags);
....@@ -584,8 +606,8 @@
584606 int ret = FAILED;
585607
586608 cmd->comp = &comp;
587
- if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
588
- sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0)
609
+ if (virtscsi_add_cmd(&vscsi->ctrl_vq, cmd,
610
+ sizeof cmd->req.tmf, sizeof cmd->resp.tmf, true) < 0)
589611 goto out;
590612
591613 wait_for_completion(&comp);
....@@ -691,34 +713,19 @@
691713 return virtscsi_tmf(vscsi, cmd);
692714 }
693715
694
-static int virtscsi_target_alloc(struct scsi_target *starget)
695
-{
696
- struct Scsi_Host *sh = dev_to_shost(starget->dev.parent);
697
- struct virtio_scsi *vscsi = shost_priv(sh);
698
-
699
- struct virtio_scsi_target_state *tgt =
700
- kmalloc(sizeof(*tgt), GFP_KERNEL);
701
- if (!tgt)
702
- return -ENOMEM;
703
-
704
- seqcount_init(&tgt->tgt_seq);
705
- tgt->req_vq = &vscsi->req_vqs[0];
706
-
707
- starget->hostdata = tgt;
708
- return 0;
709
-}
710
-
711
-static void virtscsi_target_destroy(struct scsi_target *starget)
712
-{
713
- struct virtio_scsi_target_state *tgt = starget->hostdata;
714
- kfree(tgt);
715
-}
716
-
717716 static int virtscsi_map_queues(struct Scsi_Host *shost)
718717 {
719718 struct virtio_scsi *vscsi = shost_priv(shost);
719
+ struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
720720
721
- return blk_mq_virtio_map_queues(&shost->tag_set, vscsi->vdev, 2);
721
+ return blk_mq_virtio_map_queues(qmap, vscsi->vdev, 2);
722
+}
723
+
724
+static void virtscsi_commit_rqs(struct Scsi_Host *shost, u16 hwq)
725
+{
726
+ struct virtio_scsi *vscsi = shost_priv(shost);
727
+
728
+ virtscsi_kick_vq(&vscsi->req_vqs[hwq]);
722729 }
723730
724731 /*
....@@ -738,6 +745,7 @@
738745 .this_id = -1,
739746 .cmd_size = sizeof(struct virtio_scsi_cmd),
740747 .queuecommand = virtscsi_queuecommand,
748
+ .commit_rqs = virtscsi_commit_rqs,
741749 .change_queue_depth = virtscsi_change_queue_depth,
742750 .eh_abort_handler = virtscsi_abort,
743751 .eh_device_reset_handler = virtscsi_device_reset,
....@@ -745,24 +753,20 @@
745753 .slave_alloc = virtscsi_device_alloc,
746754
747755 .dma_boundary = UINT_MAX,
748
- .use_clustering = ENABLE_CLUSTERING,
749
- .target_alloc = virtscsi_target_alloc,
750
- .target_destroy = virtscsi_target_destroy,
751756 .map_queues = virtscsi_map_queues,
752757 .track_queue_depth = 1,
753
- .force_blk_mq = 1,
754758 };
755759
756760 #define virtscsi_config_get(vdev, fld) \
757761 ({ \
758
- typeof(((struct virtio_scsi_config *)0)->fld) __val; \
762
+ __virtio_native_type(struct virtio_scsi_config, fld) __val; \
759763 virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
760764 __val; \
761765 })
762766
763767 #define virtscsi_config_set(vdev, fld, val) \
764768 do { \
765
- typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
769
+ __virtio_native_type(struct virtio_scsi_config, fld) __val = (val); \
766770 virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
767771 } while(0)
768772
....@@ -853,11 +857,12 @@
853857
854858 /* We need to know how many queues before we allocate. */
855859 num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
860
+ num_queues = min_t(unsigned int, nr_cpu_ids, num_queues);
856861
857862 num_targets = virtscsi_config_get(vdev, max_target) + 1;
858863
859864 shost = scsi_host_alloc(&virtscsi_host_template,
860
- sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
865
+ struct_size(vscsi, req_vqs, num_queues));
861866 if (!shost)
862867 return -ENOMEM;
863868
....@@ -1010,14 +1015,10 @@
10101015 return 0;
10111016
10121017 error:
1013
- if (virtscsi_cmd_pool) {
1014
- mempool_destroy(virtscsi_cmd_pool);
1015
- virtscsi_cmd_pool = NULL;
1016
- }
1017
- if (virtscsi_cmd_cache) {
1018
- kmem_cache_destroy(virtscsi_cmd_cache);
1019
- virtscsi_cmd_cache = NULL;
1020
- }
1018
+ mempool_destroy(virtscsi_cmd_pool);
1019
+ virtscsi_cmd_pool = NULL;
1020
+ kmem_cache_destroy(virtscsi_cmd_cache);
1021
+ virtscsi_cmd_cache = NULL;
10211022 return ret;
10221023 }
10231024