Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 1 | /* |
| 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 Gao | ba06d1e | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/mempool.h> |
Christoph Hellwig | 0d9f0a5 | 2017-02-05 18:15:26 +0100 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 22 | #include <linux/virtio.h> |
| 23 | #include <linux/virtio_ids.h> |
| 24 | #include <linux/virtio_config.h> |
| 25 | #include <linux/virtio_scsi.h> |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 26 | #include <linux/cpu.h> |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 27 | #include <linux/blkdev.h> |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 28 | #include <scsi/scsi_host.h> |
| 29 | #include <scsi/scsi_device.h> |
| 30 | #include <scsi/scsi_cmnd.h> |
Venkatesh Srinivas | 761f119 | 2014-07-06 16:39:27 +0200 | [diff] [blame] | 31 | #include <scsi/scsi_tcq.h> |
David Gibson | 25d1d50 | 2017-04-13 12:13:00 +1000 | [diff] [blame] | 32 | #include <scsi/scsi_devinfo.h> |
Ming Lei | 938ece7 | 2014-07-06 16:39:26 +0200 | [diff] [blame] | 33 | #include <linux/seqlock.h> |
Christoph Hellwig | 0d9f0a5 | 2017-02-05 18:15:26 +0100 | [diff] [blame] | 34 | #include <linux/blk-mq-virtio.h> |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 35 | |
| 36 | #define VIRTIO_SCSI_MEMPOOL_SZ 64 |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 37 | #define VIRTIO_SCSI_EVENT_LEN 8 |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 38 | #define VIRTIO_SCSI_VQ_BASE 2 |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 39 | |
| 40 | /* Command queue element */ |
| 41 | struct virtio_scsi_cmd { |
| 42 | struct scsi_cmnd *sc; |
| 43 | struct completion *comp; |
| 44 | union { |
| 45 | struct virtio_scsi_cmd_req cmd; |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 46 | struct virtio_scsi_cmd_req_pi cmd_pi; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 47 | 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 Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 58 | struct virtio_scsi_event_node { |
| 59 | struct virtio_scsi *vscsi; |
| 60 | struct virtio_scsi_event event; |
| 61 | struct work_struct work; |
| 62 | }; |
| 63 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 64 | struct virtio_scsi_vq { |
| 65 | /* Protects vq */ |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 66 | spinlock_t vq_lock; |
| 67 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 68 | struct virtqueue *vq; |
| 69 | }; |
| 70 | |
| 71 | /* Driver instance state */ |
| 72 | struct virtio_scsi { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 73 | struct virtio_device *vdev; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 74 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 75 | /* Get some buffers ready for event vq */ |
| 76 | struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN]; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 77 | |
| 78 | u32 num_queues; |
| 79 | |
| 80 | /* If the affinity hint is set for virtqueues */ |
| 81 | bool affinity_hint_set; |
| 82 | |
Sebastian Andrzej Siewior | 8904f5a | 2016-09-06 19:04:46 +0200 | [diff] [blame] | 83 | struct hlist_node node; |
Wanlong Gao | 285e71e | 2013-04-08 23:05:49 +0930 | [diff] [blame] | 84 | |
Michael S. Tsirkin | e67423c | 2014-10-15 10:22:33 +1030 | [diff] [blame] | 85 | /* Protected by event_vq lock */ |
| 86 | bool stop_events; |
| 87 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 88 | struct virtio_scsi_vq ctrl_vq; |
| 89 | struct virtio_scsi_vq event_vq; |
| 90 | struct virtio_scsi_vq req_vqs[]; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static struct kmem_cache *virtscsi_cmd_cache; |
| 94 | static mempool_t *virtscsi_cmd_pool; |
| 95 | |
| 96 | static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev) |
| 97 | { |
| 98 | return vdev->priv; |
| 99 | } |
| 100 | |
| 101 | static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid) |
| 102 | { |
Christoph Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame^] | 103 | if (resid) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 104 | scsi_set_resid(sc, resid); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done |
| 109 | * |
| 110 | * Called with vq_lock held. |
| 111 | */ |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 112 | static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 113 | { |
| 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. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 123 | virtscsi_compute_resid(sc, virtio32_to_cpu(vscsi->vdev, resp->resid)); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 124 | 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. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 161 | WARN_ON(virtio32_to_cpu(vscsi->vdev, resp->sense_len) > |
| 162 | VIRTIO_SCSI_SENSE_SIZE); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 163 | if (sc->sense_buffer) { |
| 164 | memcpy(sc->sense_buffer, resp->sense, |
Michael S. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 165 | min_t(u32, |
| 166 | virtio32_to_cpu(vscsi->vdev, resp->sense_len), |
| 167 | VIRTIO_SCSI_SENSE_SIZE)); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 168 | if (resp->sense_len) |
| 169 | set_driver_byte(sc, DRIVER_SENSE); |
| 170 | } |
| 171 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 172 | sc->scsi_done(sc); |
| 173 | } |
| 174 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 175 | static void virtscsi_vq_done(struct virtio_scsi *vscsi, |
| 176 | struct virtio_scsi_vq *virtscsi_vq, |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 177 | void (*fn)(struct virtio_scsi *vscsi, void *buf)) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 178 | { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 179 | void *buf; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 180 | unsigned int len; |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 181 | unsigned long flags; |
| 182 | struct virtqueue *vq = virtscsi_vq->vq; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 183 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 184 | spin_lock_irqsave(&virtscsi_vq->vq_lock, flags); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 185 | do { |
| 186 | virtqueue_disable_cb(vq); |
| 187 | while ((buf = virtqueue_get_buf(vq, &len)) != NULL) |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 188 | fn(vscsi, buf); |
Heinz Graalfs | 2bf4fd3 | 2013-11-11 11:52:43 +1030 | [diff] [blame] | 189 | |
| 190 | if (unlikely(virtqueue_is_broken(vq))) |
| 191 | break; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 192 | } while (!virtqueue_enable_cb(vq)); |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 193 | spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static void virtscsi_req_done(struct virtqueue *vq) |
| 197 | { |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 198 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
| 199 | struct virtio_scsi *vscsi = shost_priv(sh); |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 200 | int index = vq->index - VIRTIO_SCSI_VQ_BASE; |
| 201 | struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index]; |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 202 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 203 | virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 204 | }; |
| 205 | |
Paolo Bonzini | 8faeb52 | 2014-06-04 13:34:58 +0200 | [diff] [blame] | 206 | static 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 Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 216 | static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 217 | { |
| 218 | struct virtio_scsi_cmd *cmd = buf; |
| 219 | |
| 220 | if (cmd->comp) |
Daniel Wagner | e8f8142 | 2016-09-13 10:58:50 +0200 | [diff] [blame] | 221 | complete(cmd->comp); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static void virtscsi_ctrl_done(struct virtqueue *vq) |
| 225 | { |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 226 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
| 227 | struct virtio_scsi *vscsi = shost_priv(sh); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 228 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 229 | virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 230 | }; |
| 231 | |
Paolo Bonzini | cdda0e5 | 2014-06-04 13:34:56 +0200 | [diff] [blame] | 232 | static void virtscsi_handle_event(struct work_struct *work); |
| 233 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 234 | static int virtscsi_kick_event(struct virtio_scsi *vscsi, |
| 235 | struct virtio_scsi_event_node *event_node) |
| 236 | { |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 237 | int err; |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 238 | struct scatterlist sg; |
| 239 | unsigned long flags; |
| 240 | |
Paolo Bonzini | cdda0e5 | 2014-06-04 13:34:56 +0200 | [diff] [blame] | 241 | INIT_WORK(&event_node->work, virtscsi_handle_event); |
Richard W.M. Jones | 2e9c9df | 2012-10-02 17:25:46 +0200 | [diff] [blame] | 242 | sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event)); |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 243 | |
| 244 | spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags); |
| 245 | |
Rusty Russell | bf95829 | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 246 | err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node, |
| 247 | GFP_ATOMIC); |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 248 | if (!err) |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 249 | virtqueue_kick(vscsi->event_vq.vq); |
| 250 | |
| 251 | spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags); |
| 252 | |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 253 | return err; |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static 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 | |
| 268 | static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi) |
| 269 | { |
| 270 | int i; |
| 271 | |
Michael S. Tsirkin | e67423c | 2014-10-15 10:22:33 +1030 | [diff] [blame] | 272 | /* 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 Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 277 | for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) |
| 278 | cancel_work_sync(&vscsi->event_list[i].work); |
| 279 | } |
| 280 | |
| 281 | static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi, |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 282 | struct virtio_scsi_event *event) |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 283 | { |
| 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. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 289 | switch (virtio32_to_cpu(vscsi->vdev, event->reason)) { |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 290 | 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 Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 308 | static 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. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 315 | u8 asc = virtio32_to_cpu(vscsi->vdev, event->reason) & 255; |
| 316 | u8 ascq = virtio32_to_cpu(vscsi->vdev, event->reason) >> 8; |
Paolo Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 317 | |
| 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 Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 333 | static 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. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 340 | 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 Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 344 | scsi_scan_host(virtio_scsi_host(vscsi->vdev)); |
| 345 | } |
| 346 | |
Michael S. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 347 | switch (virtio32_to_cpu(vscsi->vdev, event->event)) { |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 348 | 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 Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 353 | case VIRTIO_SCSI_T_PARAM_CHANGE: |
| 354 | virtscsi_handle_param_change(vscsi, event); |
| 355 | break; |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 356 | default: |
| 357 | pr_err("Unsupport virtio scsi event %x\n", event->event); |
| 358 | } |
| 359 | virtscsi_kick_event(vscsi, event_node); |
| 360 | } |
| 361 | |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 362 | static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf) |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 363 | { |
| 364 | struct virtio_scsi_event_node *event_node = buf; |
| 365 | |
Michael S. Tsirkin | e67423c | 2014-10-15 10:22:33 +1030 | [diff] [blame] | 366 | if (!vscsi->stop_events) |
| 367 | queue_work(system_freezable_wq, &event_node->work); |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 368 | } |
| 369 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 370 | static void virtscsi_event_done(struct virtqueue *vq) |
| 371 | { |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 372 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
| 373 | struct virtio_scsi *vscsi = shost_priv(sh); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 374 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 375 | virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 376 | }; |
| 377 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 378 | /** |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 379 | * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue |
| 380 | * @vq : the struct virtqueue we're talking about |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 381 | * @cmd : command structure |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 382 | * @req_size : size of the request buffer |
| 383 | * @resp_size : size of the response buffer |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 384 | */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 385 | static int virtscsi_add_cmd(struct virtqueue *vq, |
| 386 | struct virtio_scsi_cmd *cmd, |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 387 | size_t req_size, size_t resp_size) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 388 | { |
| 389 | struct scsi_cmnd *sc = cmd->sc; |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 390 | struct scatterlist *sgs[6], req, resp; |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 391 | 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 Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame^] | 398 | out = &sc->sdb.table; |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 399 | if (sc->sc_data_direction != DMA_TO_DEVICE) |
Christoph Hellwig | ae3d56d | 2019-01-29 09:33:07 +0100 | [diff] [blame^] | 400 | in = &sc->sdb.table; |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 401 | } |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 402 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 403 | /* Request header. */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 404 | sg_init_one(&req, &cmd->req, req_size); |
| 405 | sgs[out_num++] = &req; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 406 | |
| 407 | /* Data-out buffer. */ |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 408 | 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 Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 412 | sgs[out_num++] = out->sgl; |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 413 | } |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 414 | |
| 415 | /* Response header. */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 416 | sg_init_one(&resp, &cmd->resp, resp_size); |
| 417 | sgs[out_num + in_num++] = &resp; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 418 | |
| 419 | /* Data-in buffer */ |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 420 | 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 Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 424 | sgs[out_num + in_num++] = in->sgl; |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 425 | } |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 426 | |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 427 | return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 430 | static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 431 | struct virtio_scsi_cmd *cmd, |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 432 | size_t req_size, size_t resp_size) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 433 | { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 434 | unsigned long flags; |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 435 | int err; |
| 436 | bool needs_kick = false; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 437 | |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 438 | spin_lock_irqsave(&vq->vq_lock, flags); |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 439 | err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size); |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 440 | if (!err) |
| 441 | needs_kick = virtqueue_kick_prepare(vq->vq); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 442 | |
Paolo Bonzini | bce750b | 2012-06-13 16:56:33 +0200 | [diff] [blame] | 443 | spin_unlock_irqrestore(&vq->vq_lock, flags); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 444 | |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 445 | if (needs_kick) |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 446 | virtqueue_notify(vq->vq); |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 447 | return err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Michael S. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 450 | static void virtio_scsi_init_hdr(struct virtio_device *vdev, |
| 451 | struct virtio_scsi_cmd_req *cmd, |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 452 | 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. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 458 | cmd->tag = cpu_to_virtio64(vdev, (unsigned long)sc); |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 459 | cmd->task_attr = VIRTIO_SCSI_S_SIMPLE; |
| 460 | cmd->prio = 0; |
| 461 | cmd->crn = 0; |
| 462 | } |
| 463 | |
Christoph Hellwig | c5c2567 | 2015-04-27 14:56:14 +0200 | [diff] [blame] | 464 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Michael S. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 465 | static void virtio_scsi_init_hdr_pi(struct virtio_device *vdev, |
| 466 | struct virtio_scsi_cmd_req_pi *cmd_pi, |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 467 | struct scsi_cmnd *sc) |
| 468 | { |
| 469 | struct request *rq = sc->request; |
| 470 | struct blk_integrity *bi; |
| 471 | |
Michael S. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 472 | virtio_scsi_init_hdr(vdev, (struct virtio_scsi_cmd_req *)cmd_pi, sc); |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 473 | |
| 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. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 480 | cmd_pi->pi_bytesout = cpu_to_virtio32(vdev, |
Greg Edwards | cdcdcaa | 2018-07-26 15:52:54 -0400 | [diff] [blame] | 481 | bio_integrity_bytes(bi, |
| 482 | blk_rq_sectors(rq))); |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 483 | else if (sc->sc_data_direction == DMA_FROM_DEVICE) |
Michael S. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 484 | cmd_pi->pi_bytesin = cpu_to_virtio32(vdev, |
Greg Edwards | cdcdcaa | 2018-07-26 15:52:54 -0400 | [diff] [blame] | 485 | bio_integrity_bytes(bi, |
| 486 | blk_rq_sectors(rq))); |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 487 | } |
Christoph Hellwig | c5c2567 | 2015-04-27 14:56:14 +0200 | [diff] [blame] | 488 | #endif |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 489 | |
Ming Lei | c3506df | 2018-03-13 17:42:43 +0800 | [diff] [blame] | 490 | static 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 | |
| 499 | static int virtscsi_queuecommand(struct Scsi_Host *shost, |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 500 | struct scsi_cmnd *sc) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 501 | { |
Ming Lei | c3506df | 2018-03-13 17:42:43 +0800 | [diff] [blame] | 502 | struct virtio_scsi *vscsi = shost_priv(shost); |
| 503 | struct virtio_scsi_vq *req_vq = virtscsi_pick_vq_mq(vscsi, sc); |
Christoph Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 504 | struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc); |
Eric Farman | 773c722 | 2017-01-13 12:48:06 -0500 | [diff] [blame] | 505 | unsigned long flags; |
Linus Torvalds | ed9ea4e | 2014-06-12 22:38:32 -0700 | [diff] [blame] | 506 | int req_size; |
Eric Farman | 773c722 | 2017-01-13 12:48:06 -0500 | [diff] [blame] | 507 | int ret; |
Christoph Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 508 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 509 | 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 Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 514 | dev_dbg(&sc->device->sdev_gendev, |
| 515 | "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]); |
| 516 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 517 | cmd->sc = sc; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 518 | |
| 519 | BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 520 | |
Christoph Hellwig | c5c2567 | 2015-04-27 14:56:14 +0200 | [diff] [blame] | 521 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 522 | if (virtio_has_feature(vscsi->vdev, VIRTIO_SCSI_F_T10_PI)) { |
Michael S. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 523 | virtio_scsi_init_hdr_pi(vscsi->vdev, &cmd->req.cmd_pi, sc); |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 524 | memcpy(cmd->req.cmd_pi.cdb, sc->cmnd, sc->cmd_len); |
| 525 | req_size = sizeof(cmd->req.cmd_pi); |
Christoph Hellwig | c5c2567 | 2015-04-27 14:56:14 +0200 | [diff] [blame] | 526 | } else |
| 527 | #endif |
| 528 | { |
Michael S. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 529 | virtio_scsi_init_hdr(vscsi->vdev, &cmd->req.cmd, sc); |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 530 | memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len); |
| 531 | req_size = sizeof(cmd->req.cmd); |
| 532 | } |
| 533 | |
Eric Farman | 773c722 | 2017-01-13 12:48:06 -0500 | [diff] [blame] | 534 | 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 Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 541 | return SCSI_MLQUEUE_HOST_BUSY; |
Eric Farman | 773c722 | 2017-01-13 12:48:06 -0500 | [diff] [blame] | 542 | } |
Christoph Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 543 | return 0; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd) |
| 547 | { |
| 548 | DECLARE_COMPLETION_ONSTACK(comp); |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 549 | int ret = FAILED; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 550 | |
| 551 | cmd->comp = ∁ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 552 | if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd, |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 553 | sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0) |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 554 | goto out; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 555 | |
| 556 | wait_for_completion(&comp); |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 557 | if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK || |
| 558 | cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED) |
| 559 | ret = SUCCESS; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 560 | |
Paolo Bonzini | 8faeb52 | 2014-06-04 13:34:58 +0200 | [diff] [blame] | 561 | /* |
| 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 Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 573 | out: |
| 574 | mempool_free(cmd, virtscsi_cmd_pool); |
| 575 | return ret; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static 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. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 592 | .subtype = cpu_to_virtio32(vscsi->vdev, |
| 593 | VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET), |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 594 | .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 Gibson | 25d1d50 | 2017-04-13 12:13:00 +1000 | [diff] [blame] | 602 | static 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 Srinivas | 761f119 | 2014-07-06 16:39:27 +0200 | [diff] [blame] | 624 | /** |
| 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 Srinivas | 761f119 | 2014-07-06 16:39:27 +0200 | [diff] [blame] | 628 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 629 | static int virtscsi_change_queue_depth(struct scsi_device *sdev, int qdepth) |
Venkatesh Srinivas | 761f119 | 2014-07-06 16:39:27 +0200 | [diff] [blame] | 630 | { |
| 631 | struct Scsi_Host *shost = sdev->host; |
| 632 | int max_depth = shost->cmd_per_lun; |
| 633 | |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 634 | return scsi_change_queue_depth(sdev, min(max_depth, qdepth)); |
Venkatesh Srinivas | 761f119 | 2014-07-06 16:39:27 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 637 | static 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. Tsirkin | d75dff3 | 2014-11-23 17:28:57 +0200 | [diff] [blame] | 656 | .tag = cpu_to_virtio64(vscsi->vdev, (unsigned long)sc), |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 657 | }; |
| 658 | return virtscsi_tmf(vscsi, cmd); |
| 659 | } |
| 660 | |
Christoph Hellwig | 0d9f0a5 | 2017-02-05 18:15:26 +0100 | [diff] [blame] | 661 | static int virtscsi_map_queues(struct Scsi_Host *shost) |
| 662 | { |
| 663 | struct virtio_scsi *vscsi = shost_priv(shost); |
Jens Axboe | ed76e32 | 2018-10-29 13:06:14 -0600 | [diff] [blame] | 664 | struct blk_mq_queue_map *qmap = &shost->tag_set.map[0]; |
Christoph Hellwig | 0d9f0a5 | 2017-02-05 18:15:26 +0100 | [diff] [blame] | 665 | |
Jens Axboe | ed76e32 | 2018-10-29 13:06:14 -0600 | [diff] [blame] | 666 | return blk_mq_virtio_map_queues(qmap, vscsi->vdev, 2); |
Christoph Hellwig | 0d9f0a5 | 2017-02-05 18:15:26 +0100 | [diff] [blame] | 667 | } |
| 668 | |
Paolo Bonzini | e72c9a2 | 2017-06-21 16:35:46 +0200 | [diff] [blame] | 669 | /* |
| 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 | */ |
| 674 | static enum blk_eh_timer_return virtscsi_eh_timed_out(struct scsi_cmnd *scmnd) |
| 675 | { |
| 676 | return BLK_EH_RESET_TIMER; |
| 677 | } |
| 678 | |
Ming Lei | c3506df | 2018-03-13 17:42:43 +0800 | [diff] [blame] | 679 | static struct scsi_host_template virtscsi_host_template = { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 680 | .module = THIS_MODULE, |
| 681 | .name = "Virtio SCSI HBA", |
| 682 | .proc_name = "virtio_scsi", |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 683 | .this_id = -1, |
Christoph Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 684 | .cmd_size = sizeof(struct virtio_scsi_cmd), |
Ming Lei | c3506df | 2018-03-13 17:42:43 +0800 | [diff] [blame] | 685 | .queuecommand = virtscsi_queuecommand, |
Venkatesh Srinivas | 761f119 | 2014-07-06 16:39:27 +0200 | [diff] [blame] | 686 | .change_queue_depth = virtscsi_change_queue_depth, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 687 | .eh_abort_handler = virtscsi_abort, |
| 688 | .eh_device_reset_handler = virtscsi_device_reset, |
Paolo Bonzini | e72c9a2 | 2017-06-21 16:35:46 +0200 | [diff] [blame] | 689 | .eh_timed_out = virtscsi_eh_timed_out, |
Paolo Bonzini | a680f1d | 2017-07-05 10:30:56 +0200 | [diff] [blame] | 690 | .slave_alloc = virtscsi_device_alloc, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 691 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 692 | .dma_boundary = UINT_MAX, |
Christoph Hellwig | 0d9f0a5 | 2017-02-05 18:15:26 +0100 | [diff] [blame] | 693 | .map_queues = virtscsi_map_queues, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 694 | .track_queue_depth = 1, |
Ming Lei | b5b6e8c | 2018-03-13 17:42:42 +0800 | [diff] [blame] | 695 | .force_blk_mq = 1, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | #define virtscsi_config_get(vdev, fld) \ |
| 699 | ({ \ |
| 700 | typeof(((struct virtio_scsi_config *)0)->fld) __val; \ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 701 | virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \ |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 702 | __val; \ |
| 703 | }) |
| 704 | |
| 705 | #define virtscsi_config_set(vdev, fld, val) \ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 706 | do { \ |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 707 | typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 708 | virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \ |
| 709 | } while(0) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 710 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 711 | static 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 Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 718 | static void virtscsi_remove_vqs(struct virtio_device *vdev) |
| 719 | { |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 720 | /* Stop all the virtqueues. */ |
| 721 | vdev->config->reset(vdev); |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 722 | vdev->config->del_vqs(vdev); |
| 723 | } |
| 724 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 725 | static int virtscsi_init(struct virtio_device *vdev, |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 726 | struct virtio_scsi *vscsi) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 727 | { |
| 728 | int err; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 729 | u32 i; |
| 730 | u32 num_vqs; |
| 731 | vq_callback_t **callbacks; |
| 732 | const char **names; |
| 733 | struct virtqueue **vqs; |
Christoph Hellwig | 0d9f0a5 | 2017-02-05 18:15:26 +0100 | [diff] [blame] | 734 | struct irq_affinity desc = { .pre_vectors = 2 }; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 735 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 736 | num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 737 | 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 Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 741 | |
| 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 Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 755 | |
| 756 | /* Discover virtqueues and write information to configuration. */ |
Michael S. Tsirkin | 9b2bbdb | 2017-03-06 18:19:39 +0200 | [diff] [blame] | 757 | err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 758 | if (err) |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 759 | goto out; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 760 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 761 | virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]); |
| 762 | virtscsi_init_vq(&vscsi->event_vq, vqs[1]); |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 763 | 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 Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 767 | virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE); |
| 768 | virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE); |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 769 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 770 | err = 0; |
| 771 | |
| 772 | out: |
| 773 | kfree(names); |
| 774 | kfree(callbacks); |
| 775 | kfree(vqs); |
| 776 | if (err) |
| 777 | virtscsi_remove_vqs(vdev); |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 778 | return err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 781 | static int virtscsi_probe(struct virtio_device *vdev) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 782 | { |
| 783 | struct Scsi_Host *shost; |
| 784 | struct virtio_scsi *vscsi; |
Stephen Rothwell | 908a554 | 2015-06-30 10:59:04 +1000 | [diff] [blame] | 785 | int err; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 786 | u32 sg_elems, num_targets; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 787 | u32 cmd_per_lun; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 788 | u32 num_queues; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 789 | |
Michael S. Tsirkin | 8cab3cd | 2015-01-12 16:23:37 +0200 | [diff] [blame] | 790 | if (!vdev->config->get) { |
| 791 | dev_err(&vdev->dev, "%s failure: config access disabled\n", |
| 792 | __func__); |
| 793 | return -EINVAL; |
| 794 | } |
| 795 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 796 | /* We need to know how many queues before we allocate. */ |
| 797 | num_queues = virtscsi_config_get(vdev, num_queues) ? : 1; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 798 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 799 | num_targets = virtscsi_config_get(vdev, max_target) + 1; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 800 | |
Ming Lei | c3506df | 2018-03-13 17:42:43 +0800 | [diff] [blame] | 801 | shost = scsi_host_alloc(&virtscsi_host_template, |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 802 | sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 803 | if (!shost) |
| 804 | return -ENOMEM; |
| 805 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 806 | sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 807 | shost->sg_tablesize = sg_elems; |
| 808 | vscsi = shost_priv(shost); |
| 809 | vscsi->vdev = vdev; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 810 | vscsi->num_queues = num_queues; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 811 | vdev->priv = shost; |
| 812 | |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 813 | err = virtscsi_init(vdev, vscsi); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 814 | if (err) |
| 815 | goto virtscsi_init_failed; |
| 816 | |
Richard W.M. Jones | 582b0ab | 2017-08-10 17:56:52 +0100 | [diff] [blame] | 817 | shost->can_queue = virtqueue_get_vring_size(vscsi->req_vqs[0].vq); |
| 818 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 819 | 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 Bonzini | 9da5f5a | 2012-10-02 17:25:47 +0200 | [diff] [blame] | 822 | |
| 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 Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 827 | shost->max_id = num_targets; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 828 | shost->max_channel = 0; |
| 829 | shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE; |
Ming Lei | ccbedf1 | 2014-11-15 11:47:14 +0800 | [diff] [blame] | 830 | shost->nr_hw_queues = num_queues; |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 831 | |
Christoph Hellwig | c5c2567 | 2015-04-27 14:56:14 +0200 | [diff] [blame] | 832 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 833 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) { |
Stephen Rothwell | 908a554 | 2015-06-30 10:59:04 +1000 | [diff] [blame] | 834 | int host_prot; |
| 835 | |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 836 | 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 Hellwig | c5c2567 | 2015-04-27 14:56:14 +0200 | [diff] [blame] | 843 | #endif |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 844 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 845 | err = scsi_add_host(shost, &vdev->dev); |
| 846 | if (err) |
| 847 | goto scsi_add_host_failed; |
Michael S. Tsirkin | 5d8f16d | 2014-10-15 10:22:33 +1030 | [diff] [blame] | 848 | |
| 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 Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 855 | return 0; |
| 856 | |
| 857 | scsi_add_host_failed: |
| 858 | vdev->config->del_vqs(vdev); |
| 859 | virtscsi_init_failed: |
| 860 | scsi_host_put(shost); |
| 861 | return err; |
| 862 | } |
| 863 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 864 | static void virtscsi_remove(struct virtio_device *vdev) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 865 | { |
| 866 | struct Scsi_Host *shost = virtio_scsi_host(vdev); |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 867 | 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 Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 871 | |
| 872 | scsi_remove_host(shost); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 873 | virtscsi_remove_vqs(vdev); |
| 874 | scsi_host_put(shost); |
| 875 | } |
| 876 | |
Aaron Lu | 8910700 | 2013-09-17 09:25:23 +0930 | [diff] [blame] | 877 | #ifdef CONFIG_PM_SLEEP |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 878 | static int virtscsi_freeze(struct virtio_device *vdev) |
| 879 | { |
| 880 | virtscsi_remove_vqs(vdev); |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static 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 He | f466f75 | 2014-01-16 10:18:48 +1030 | [diff] [blame] | 888 | int err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 889 | |
Asias He | f466f75 | 2014-01-16 10:18:48 +1030 | [diff] [blame] | 890 | err = virtscsi_init(vdev, vscsi); |
| 891 | if (err) |
| 892 | return err; |
| 893 | |
Michael S. Tsirkin | 52c9cf1 | 2014-10-15 10:22:32 +1030 | [diff] [blame] | 894 | virtio_device_ready(vdev); |
| 895 | |
Michael S. Tsirkin | cd67904 | 2014-10-15 10:22:31 +1030 | [diff] [blame] | 896 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) |
| 897 | virtscsi_kick_event_all(vscsi); |
Asias He | f466f75 | 2014-01-16 10:18:48 +1030 | [diff] [blame] | 898 | |
| 899 | return err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 900 | } |
| 901 | #endif |
| 902 | |
| 903 | static struct virtio_device_id id_table[] = { |
| 904 | { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID }, |
| 905 | { 0 }, |
| 906 | }; |
| 907 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 908 | static unsigned int features[] = { |
Paolo Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 909 | VIRTIO_SCSI_F_HOTPLUG, |
| 910 | VIRTIO_SCSI_F_CHANGE, |
Christoph Hellwig | c5c2567 | 2015-04-27 14:56:14 +0200 | [diff] [blame] | 911 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
Nicholas Bellinger | e6dc783a | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 912 | VIRTIO_SCSI_F_T10_PI, |
Christoph Hellwig | c5c2567 | 2015-04-27 14:56:14 +0200 | [diff] [blame] | 913 | #endif |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 914 | }; |
| 915 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 916 | static struct virtio_driver virtio_scsi_driver = { |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 917 | .feature_table = features, |
| 918 | .feature_table_size = ARRAY_SIZE(features), |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 919 | .driver.name = KBUILD_MODNAME, |
| 920 | .driver.owner = THIS_MODULE, |
| 921 | .id_table = id_table, |
| 922 | .probe = virtscsi_probe, |
Aaron Lu | 8910700 | 2013-09-17 09:25:23 +0930 | [diff] [blame] | 923 | #ifdef CONFIG_PM_SLEEP |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 924 | .freeze = virtscsi_freeze, |
| 925 | .restore = virtscsi_restore, |
| 926 | #endif |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 927 | .remove = virtscsi_remove, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 928 | }; |
| 929 | |
| 930 | static 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 Gao | ba06d1e | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 936 | pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n"); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 937 | 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 Gao | ba06d1e | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 945 | pr_err("mempool_create() for virtscsi_cmd_pool failed\n"); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 946 | goto error; |
| 947 | } |
| 948 | ret = register_virtio_driver(&virtio_scsi_driver); |
| 949 | if (ret < 0) |
| 950 | goto error; |
| 951 | |
| 952 | return 0; |
| 953 | |
| 954 | error: |
| 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 | |
| 966 | static void __exit fini(void) |
| 967 | { |
| 968 | unregister_virtio_driver(&virtio_scsi_driver); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 969 | mempool_destroy(virtscsi_cmd_pool); |
| 970 | kmem_cache_destroy(virtscsi_cmd_cache); |
| 971 | } |
| 972 | module_init(init); |
| 973 | module_exit(fini); |
| 974 | |
| 975 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 976 | MODULE_DESCRIPTION("Virtio SCSI HBA driver"); |
| 977 | MODULE_LICENSE("GPL"); |