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> |
| 21 | #include <linux/virtio.h> |
| 22 | #include <linux/virtio_ids.h> |
| 23 | #include <linux/virtio_config.h> |
| 24 | #include <linux/virtio_scsi.h> |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 25 | #include <linux/cpu.h> |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 26 | #include <linux/blkdev.h> |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 27 | #include <scsi/scsi_host.h> |
| 28 | #include <scsi/scsi_device.h> |
| 29 | #include <scsi/scsi_cmnd.h> |
| 30 | |
| 31 | #define VIRTIO_SCSI_MEMPOOL_SZ 64 |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 32 | #define VIRTIO_SCSI_EVENT_LEN 8 |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 33 | #define VIRTIO_SCSI_VQ_BASE 2 |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 34 | |
| 35 | /* Command queue element */ |
| 36 | struct virtio_scsi_cmd { |
| 37 | struct scsi_cmnd *sc; |
| 38 | struct completion *comp; |
| 39 | union { |
| 40 | struct virtio_scsi_cmd_req cmd; |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 41 | struct virtio_scsi_cmd_req_pi cmd_pi; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 42 | struct virtio_scsi_ctrl_tmf_req tmf; |
| 43 | struct virtio_scsi_ctrl_an_req an; |
| 44 | } req; |
| 45 | union { |
| 46 | struct virtio_scsi_cmd_resp cmd; |
| 47 | struct virtio_scsi_ctrl_tmf_resp tmf; |
| 48 | struct virtio_scsi_ctrl_an_resp an; |
| 49 | struct virtio_scsi_event evt; |
| 50 | } resp; |
| 51 | } ____cacheline_aligned_in_smp; |
| 52 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 53 | struct virtio_scsi_event_node { |
| 54 | struct virtio_scsi *vscsi; |
| 55 | struct virtio_scsi_event event; |
| 56 | struct work_struct work; |
| 57 | }; |
| 58 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 59 | struct virtio_scsi_vq { |
| 60 | /* Protects vq */ |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 61 | spinlock_t vq_lock; |
| 62 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 63 | struct virtqueue *vq; |
| 64 | }; |
| 65 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 66 | /* |
| 67 | * Per-target queue state. |
| 68 | * |
| 69 | * This struct holds the data needed by the queue steering policy. When a |
| 70 | * target is sent multiple requests, we need to drive them to the same queue so |
| 71 | * that FIFO processing order is kept. However, if a target was idle, we can |
| 72 | * choose a queue arbitrarily. In this case the queue is chosen according to |
| 73 | * the current VCPU, so the driver expects the number of request queues to be |
| 74 | * equal to the number of VCPUs. This makes it easy and fast to select the |
| 75 | * queue, and also lets the driver optimize the IRQ affinity for the virtqueues |
| 76 | * (each virtqueue's affinity is set to the CPU that "owns" the queue). |
| 77 | * |
Ming Lei | f259d9b | 2014-05-08 15:23:45 +0800 | [diff] [blame] | 78 | * tgt_lock is held to serialize reading and writing req_vq. Reading req_vq |
| 79 | * could be done locklessly, but we do not do it yet. |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 80 | * |
Ming Lei | f259d9b | 2014-05-08 15:23:45 +0800 | [diff] [blame] | 81 | * Decrements of reqs are never concurrent with writes of req_vq: before the |
| 82 | * decrement reqs will be != 0; after the decrement the virtqueue completion |
| 83 | * routine will not use the req_vq so it can be changed by a new request. |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 84 | * Thus they can happen outside the tgt_lock, provided of course we make reqs |
| 85 | * an atomic_t. |
| 86 | */ |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 87 | struct virtio_scsi_target_state { |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 88 | /* This spinlock never held at the same time as vq_lock. */ |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 89 | spinlock_t tgt_lock; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 90 | |
| 91 | /* Count of outstanding requests. */ |
| 92 | atomic_t reqs; |
| 93 | |
| 94 | /* Currently active virtqueue for requests sent to this target. */ |
| 95 | struct virtio_scsi_vq *req_vq; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 98 | /* Driver instance state */ |
| 99 | struct virtio_scsi { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 100 | struct virtio_device *vdev; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 101 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 102 | /* Get some buffers ready for event vq */ |
| 103 | struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN]; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 104 | |
| 105 | u32 num_queues; |
| 106 | |
| 107 | /* If the affinity hint is set for virtqueues */ |
| 108 | bool affinity_hint_set; |
| 109 | |
Wanlong Gao | 285e71e | 2013-04-08 23:05:49 +0930 | [diff] [blame] | 110 | /* CPU hotplug notifier */ |
| 111 | struct notifier_block nb; |
| 112 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 113 | struct virtio_scsi_vq ctrl_vq; |
| 114 | struct virtio_scsi_vq event_vq; |
| 115 | struct virtio_scsi_vq req_vqs[]; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | static struct kmem_cache *virtscsi_cmd_cache; |
| 119 | static mempool_t *virtscsi_cmd_pool; |
| 120 | |
| 121 | static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev) |
| 122 | { |
| 123 | return vdev->priv; |
| 124 | } |
| 125 | |
| 126 | static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid) |
| 127 | { |
| 128 | if (!resid) |
| 129 | return; |
| 130 | |
| 131 | if (!scsi_bidi_cmnd(sc)) { |
| 132 | scsi_set_resid(sc, resid); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | scsi_in(sc)->resid = min(resid, scsi_in(sc)->length); |
| 137 | scsi_out(sc)->resid = resid - scsi_in(sc)->resid; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done |
| 142 | * |
| 143 | * Called with vq_lock held. |
| 144 | */ |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 145 | static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 146 | { |
| 147 | struct virtio_scsi_cmd *cmd = buf; |
| 148 | struct scsi_cmnd *sc = cmd->sc; |
| 149 | struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 150 | struct virtio_scsi_target_state *tgt = |
| 151 | scsi_target(sc->device)->hostdata; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 152 | |
| 153 | dev_dbg(&sc->device->sdev_gendev, |
| 154 | "cmd %p response %u status %#02x sense_len %u\n", |
| 155 | sc, resp->response, resp->status, resp->sense_len); |
| 156 | |
| 157 | sc->result = resp->status; |
| 158 | virtscsi_compute_resid(sc, resp->resid); |
| 159 | switch (resp->response) { |
| 160 | case VIRTIO_SCSI_S_OK: |
| 161 | set_host_byte(sc, DID_OK); |
| 162 | break; |
| 163 | case VIRTIO_SCSI_S_OVERRUN: |
| 164 | set_host_byte(sc, DID_ERROR); |
| 165 | break; |
| 166 | case VIRTIO_SCSI_S_ABORTED: |
| 167 | set_host_byte(sc, DID_ABORT); |
| 168 | break; |
| 169 | case VIRTIO_SCSI_S_BAD_TARGET: |
| 170 | set_host_byte(sc, DID_BAD_TARGET); |
| 171 | break; |
| 172 | case VIRTIO_SCSI_S_RESET: |
| 173 | set_host_byte(sc, DID_RESET); |
| 174 | break; |
| 175 | case VIRTIO_SCSI_S_BUSY: |
| 176 | set_host_byte(sc, DID_BUS_BUSY); |
| 177 | break; |
| 178 | case VIRTIO_SCSI_S_TRANSPORT_FAILURE: |
| 179 | set_host_byte(sc, DID_TRANSPORT_DISRUPTED); |
| 180 | break; |
| 181 | case VIRTIO_SCSI_S_TARGET_FAILURE: |
| 182 | set_host_byte(sc, DID_TARGET_FAILURE); |
| 183 | break; |
| 184 | case VIRTIO_SCSI_S_NEXUS_FAILURE: |
| 185 | set_host_byte(sc, DID_NEXUS_FAILURE); |
| 186 | break; |
| 187 | default: |
| 188 | scmd_printk(KERN_WARNING, sc, "Unknown response %d", |
| 189 | resp->response); |
| 190 | /* fall through */ |
| 191 | case VIRTIO_SCSI_S_FAILURE: |
| 192 | set_host_byte(sc, DID_ERROR); |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE); |
| 197 | if (sc->sense_buffer) { |
| 198 | memcpy(sc->sense_buffer, resp->sense, |
| 199 | min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE)); |
| 200 | if (resp->sense_len) |
| 201 | set_driver_byte(sc, DRIVER_SENSE); |
| 202 | } |
| 203 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 204 | sc->scsi_done(sc); |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 205 | |
| 206 | atomic_dec(&tgt->reqs); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 209 | static void virtscsi_vq_done(struct virtio_scsi *vscsi, |
| 210 | struct virtio_scsi_vq *virtscsi_vq, |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 211 | void (*fn)(struct virtio_scsi *vscsi, void *buf)) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 212 | { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 213 | void *buf; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 214 | unsigned int len; |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 215 | unsigned long flags; |
| 216 | struct virtqueue *vq = virtscsi_vq->vq; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 217 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 218 | spin_lock_irqsave(&virtscsi_vq->vq_lock, flags); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 219 | do { |
| 220 | virtqueue_disable_cb(vq); |
| 221 | while ((buf = virtqueue_get_buf(vq, &len)) != NULL) |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 222 | fn(vscsi, buf); |
Heinz Graalfs | 2bf4fd3 | 2013-11-11 11:52:43 +1030 | [diff] [blame] | 223 | |
| 224 | if (unlikely(virtqueue_is_broken(vq))) |
| 225 | break; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 226 | } while (!virtqueue_enable_cb(vq)); |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 227 | spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void virtscsi_req_done(struct virtqueue *vq) |
| 231 | { |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 232 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
| 233 | struct virtio_scsi *vscsi = shost_priv(sh); |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 234 | int index = vq->index - VIRTIO_SCSI_VQ_BASE; |
| 235 | struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index]; |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 236 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 237 | virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 238 | }; |
| 239 | |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 240 | static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 241 | { |
| 242 | struct virtio_scsi_cmd *cmd = buf; |
| 243 | |
| 244 | if (cmd->comp) |
| 245 | complete_all(cmd->comp); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static void virtscsi_ctrl_done(struct virtqueue *vq) |
| 249 | { |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 250 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
| 251 | struct virtio_scsi *vscsi = shost_priv(sh); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 252 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 253 | virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 254 | }; |
| 255 | |
Paolo Bonzini | cdda0e5 | 2014-06-04 13:34:56 +0200 | [diff] [blame^] | 256 | static void virtscsi_handle_event(struct work_struct *work); |
| 257 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 258 | static int virtscsi_kick_event(struct virtio_scsi *vscsi, |
| 259 | struct virtio_scsi_event_node *event_node) |
| 260 | { |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 261 | int err; |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 262 | struct scatterlist sg; |
| 263 | unsigned long flags; |
| 264 | |
Paolo Bonzini | cdda0e5 | 2014-06-04 13:34:56 +0200 | [diff] [blame^] | 265 | INIT_WORK(&event_node->work, virtscsi_handle_event); |
Richard W.M. Jones | 2e9c9df | 2012-10-02 17:25:46 +0200 | [diff] [blame] | 266 | sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event)); |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 267 | |
| 268 | spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags); |
| 269 | |
Rusty Russell | bf95829 | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 270 | err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node, |
| 271 | GFP_ATOMIC); |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 272 | if (!err) |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 273 | virtqueue_kick(vscsi->event_vq.vq); |
| 274 | |
| 275 | spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags); |
| 276 | |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 277 | return err; |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static int virtscsi_kick_event_all(struct virtio_scsi *vscsi) |
| 281 | { |
| 282 | int i; |
| 283 | |
| 284 | for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) { |
| 285 | vscsi->event_list[i].vscsi = vscsi; |
| 286 | virtscsi_kick_event(vscsi, &vscsi->event_list[i]); |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi) |
| 293 | { |
| 294 | int i; |
| 295 | |
| 296 | for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) |
| 297 | cancel_work_sync(&vscsi->event_list[i].work); |
| 298 | } |
| 299 | |
| 300 | static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi, |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 301 | struct virtio_scsi_event *event) |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 302 | { |
| 303 | struct scsi_device *sdev; |
| 304 | struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev); |
| 305 | unsigned int target = event->lun[1]; |
| 306 | unsigned int lun = (event->lun[2] << 8) | event->lun[3]; |
| 307 | |
| 308 | switch (event->reason) { |
| 309 | case VIRTIO_SCSI_EVT_RESET_RESCAN: |
| 310 | scsi_add_device(shost, 0, target, lun); |
| 311 | break; |
| 312 | case VIRTIO_SCSI_EVT_RESET_REMOVED: |
| 313 | sdev = scsi_device_lookup(shost, 0, target, lun); |
| 314 | if (sdev) { |
| 315 | scsi_remove_device(sdev); |
| 316 | scsi_device_put(sdev); |
| 317 | } else { |
| 318 | pr_err("SCSI device %d 0 %d %d not found\n", |
| 319 | shost->host_no, target, lun); |
| 320 | } |
| 321 | break; |
| 322 | default: |
| 323 | pr_info("Unsupport virtio scsi event reason %x\n", event->reason); |
| 324 | } |
| 325 | } |
| 326 | |
Paolo Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 327 | static void virtscsi_handle_param_change(struct virtio_scsi *vscsi, |
| 328 | struct virtio_scsi_event *event) |
| 329 | { |
| 330 | struct scsi_device *sdev; |
| 331 | struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev); |
| 332 | unsigned int target = event->lun[1]; |
| 333 | unsigned int lun = (event->lun[2] << 8) | event->lun[3]; |
| 334 | u8 asc = event->reason & 255; |
| 335 | u8 ascq = event->reason >> 8; |
| 336 | |
| 337 | sdev = scsi_device_lookup(shost, 0, target, lun); |
| 338 | if (!sdev) { |
| 339 | pr_err("SCSI device %d 0 %d %d not found\n", |
| 340 | shost->host_no, target, lun); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | /* Handle "Parameters changed", "Mode parameters changed", and |
| 345 | "Capacity data has changed". */ |
| 346 | if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09)) |
| 347 | scsi_rescan_device(&sdev->sdev_gendev); |
| 348 | |
| 349 | scsi_device_put(sdev); |
| 350 | } |
| 351 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 352 | static void virtscsi_handle_event(struct work_struct *work) |
| 353 | { |
| 354 | struct virtio_scsi_event_node *event_node = |
| 355 | container_of(work, struct virtio_scsi_event_node, work); |
| 356 | struct virtio_scsi *vscsi = event_node->vscsi; |
| 357 | struct virtio_scsi_event *event = &event_node->event; |
| 358 | |
| 359 | if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) { |
| 360 | event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED; |
| 361 | scsi_scan_host(virtio_scsi_host(vscsi->vdev)); |
| 362 | } |
| 363 | |
| 364 | switch (event->event) { |
| 365 | case VIRTIO_SCSI_T_NO_EVENT: |
| 366 | break; |
| 367 | case VIRTIO_SCSI_T_TRANSPORT_RESET: |
| 368 | virtscsi_handle_transport_reset(vscsi, event); |
| 369 | break; |
Paolo Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 370 | case VIRTIO_SCSI_T_PARAM_CHANGE: |
| 371 | virtscsi_handle_param_change(vscsi, event); |
| 372 | break; |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 373 | default: |
| 374 | pr_err("Unsupport virtio scsi event %x\n", event->event); |
| 375 | } |
| 376 | virtscsi_kick_event(vscsi, event_node); |
| 377 | } |
| 378 | |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 379 | static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf) |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 380 | { |
| 381 | struct virtio_scsi_event_node *event_node = buf; |
| 382 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 383 | schedule_work(&event_node->work); |
| 384 | } |
| 385 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 386 | static void virtscsi_event_done(struct virtqueue *vq) |
| 387 | { |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 388 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
| 389 | struct virtio_scsi *vscsi = shost_priv(sh); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 390 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame] | 391 | virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 392 | }; |
| 393 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 394 | /** |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 395 | * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue |
| 396 | * @vq : the struct virtqueue we're talking about |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 397 | * @cmd : command structure |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 398 | * @req_size : size of the request buffer |
| 399 | * @resp_size : size of the response buffer |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 400 | */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 401 | static int virtscsi_add_cmd(struct virtqueue *vq, |
| 402 | struct virtio_scsi_cmd *cmd, |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 403 | size_t req_size, size_t resp_size) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 404 | { |
| 405 | struct scsi_cmnd *sc = cmd->sc; |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 406 | struct scatterlist *sgs[6], req, resp; |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 407 | struct sg_table *out, *in; |
| 408 | unsigned out_num = 0, in_num = 0; |
| 409 | |
| 410 | out = in = NULL; |
| 411 | |
| 412 | if (sc && sc->sc_data_direction != DMA_NONE) { |
| 413 | if (sc->sc_data_direction != DMA_FROM_DEVICE) |
| 414 | out = &scsi_out(sc)->table; |
| 415 | if (sc->sc_data_direction != DMA_TO_DEVICE) |
| 416 | in = &scsi_in(sc)->table; |
| 417 | } |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 418 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 419 | /* Request header. */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 420 | sg_init_one(&req, &cmd->req, req_size); |
| 421 | sgs[out_num++] = &req; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 422 | |
| 423 | /* Data-out buffer. */ |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 424 | if (out) { |
| 425 | /* Place WRITE protection SGLs before Data OUT payload */ |
| 426 | if (scsi_prot_sg_count(sc)) |
| 427 | sgs[out_num++] = scsi_prot_sglist(sc); |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 428 | sgs[out_num++] = out->sgl; |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 429 | } |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 430 | |
| 431 | /* Response header. */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 432 | sg_init_one(&resp, &cmd->resp, resp_size); |
| 433 | sgs[out_num + in_num++] = &resp; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 434 | |
| 435 | /* Data-in buffer */ |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 436 | if (in) { |
| 437 | /* Place READ protection SGLs before Data IN payload */ |
| 438 | if (scsi_prot_sg_count(sc)) |
| 439 | sgs[out_num + in_num++] = scsi_prot_sglist(sc); |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 440 | sgs[out_num + in_num++] = in->sgl; |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 441 | } |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 442 | |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 443 | 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] | 444 | } |
| 445 | |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 446 | static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 447 | struct virtio_scsi_cmd *cmd, |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 448 | size_t req_size, size_t resp_size) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 449 | { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 450 | unsigned long flags; |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 451 | int err; |
| 452 | bool needs_kick = false; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 453 | |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 454 | spin_lock_irqsave(&vq->vq_lock, flags); |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 455 | err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size); |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 456 | if (!err) |
| 457 | needs_kick = virtqueue_kick_prepare(vq->vq); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 458 | |
Paolo Bonzini | bce750b | 2012-06-13 16:56:33 +0200 | [diff] [blame] | 459 | spin_unlock_irqrestore(&vq->vq_lock, flags); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 460 | |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 461 | if (needs_kick) |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 462 | virtqueue_notify(vq->vq); |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 463 | return err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 466 | static void virtio_scsi_init_hdr(struct virtio_scsi_cmd_req *cmd, |
| 467 | struct scsi_cmnd *sc) |
| 468 | { |
| 469 | cmd->lun[0] = 1; |
| 470 | cmd->lun[1] = sc->device->id; |
| 471 | cmd->lun[2] = (sc->device->lun >> 8) | 0x40; |
| 472 | cmd->lun[3] = sc->device->lun & 0xff; |
| 473 | cmd->tag = (unsigned long)sc; |
| 474 | cmd->task_attr = VIRTIO_SCSI_S_SIMPLE; |
| 475 | cmd->prio = 0; |
| 476 | cmd->crn = 0; |
| 477 | } |
| 478 | |
| 479 | static void virtio_scsi_init_hdr_pi(struct virtio_scsi_cmd_req_pi *cmd_pi, |
| 480 | struct scsi_cmnd *sc) |
| 481 | { |
| 482 | struct request *rq = sc->request; |
| 483 | struct blk_integrity *bi; |
| 484 | |
| 485 | virtio_scsi_init_hdr((struct virtio_scsi_cmd_req *)cmd_pi, sc); |
| 486 | |
| 487 | if (!rq || !scsi_prot_sg_count(sc)) |
| 488 | return; |
| 489 | |
| 490 | bi = blk_get_integrity(rq->rq_disk); |
| 491 | |
| 492 | if (sc->sc_data_direction == DMA_TO_DEVICE) |
| 493 | cmd_pi->pi_bytesout = blk_rq_sectors(rq) * bi->tuple_size; |
| 494 | else if (sc->sc_data_direction == DMA_FROM_DEVICE) |
| 495 | cmd_pi->pi_bytesin = blk_rq_sectors(rq) * bi->tuple_size; |
| 496 | } |
| 497 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 498 | static int virtscsi_queuecommand(struct virtio_scsi *vscsi, |
| 499 | struct virtio_scsi_vq *req_vq, |
| 500 | struct scsi_cmnd *sc) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 501 | { |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 502 | struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev); |
Christoph Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 503 | struct virtio_scsi_cmd *cmd = scsi_cmd_priv(sc); |
Linus Torvalds | ed9ea4e | 2014-06-12 22:38:32 -0700 | [diff] [blame] | 504 | int req_size; |
Christoph Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 505 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 511 | dev_dbg(&sc->device->sdev_gendev, |
| 512 | "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]); |
| 513 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 514 | memset(cmd, 0, sizeof(*cmd)); |
| 515 | cmd->sc = sc; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 516 | |
| 517 | BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 518 | |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 519 | if (virtio_has_feature(vscsi->vdev, VIRTIO_SCSI_F_T10_PI)) { |
| 520 | virtio_scsi_init_hdr_pi(&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 | virtio_scsi_init_hdr(&cmd->req.cmd, sc); |
| 525 | memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len); |
| 526 | req_size = sizeof(cmd->req.cmd); |
| 527 | } |
| 528 | |
Linus Torvalds | ed9ea4e | 2014-06-12 22:38:32 -0700 | [diff] [blame] | 529 | if (virtscsi_kick_cmd(req_vq, cmd, req_size, sizeof(cmd->resp.cmd)) != 0) |
Christoph Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 530 | return SCSI_MLQUEUE_HOST_BUSY; |
| 531 | return 0; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 534 | static int virtscsi_queuecommand_single(struct Scsi_Host *sh, |
| 535 | struct scsi_cmnd *sc) |
| 536 | { |
| 537 | struct virtio_scsi *vscsi = shost_priv(sh); |
| 538 | struct virtio_scsi_target_state *tgt = |
| 539 | scsi_target(sc->device)->hostdata; |
| 540 | |
| 541 | atomic_inc(&tgt->reqs); |
| 542 | return virtscsi_queuecommand(vscsi, &vscsi->req_vqs[0], sc); |
| 543 | } |
| 544 | |
| 545 | static struct virtio_scsi_vq *virtscsi_pick_vq(struct virtio_scsi *vscsi, |
| 546 | struct virtio_scsi_target_state *tgt) |
| 547 | { |
| 548 | struct virtio_scsi_vq *vq; |
| 549 | unsigned long flags; |
| 550 | u32 queue_num; |
| 551 | |
| 552 | spin_lock_irqsave(&tgt->tgt_lock, flags); |
| 553 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 554 | if (atomic_inc_return(&tgt->reqs) > 1) |
Ming Lei | f259d9b | 2014-05-08 15:23:45 +0800 | [diff] [blame] | 555 | vq = tgt->req_vq; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 556 | else { |
| 557 | queue_num = smp_processor_id(); |
| 558 | while (unlikely(queue_num >= vscsi->num_queues)) |
| 559 | queue_num -= vscsi->num_queues; |
| 560 | |
| 561 | tgt->req_vq = vq = &vscsi->req_vqs[queue_num]; |
| 562 | } |
| 563 | |
| 564 | spin_unlock_irqrestore(&tgt->tgt_lock, flags); |
| 565 | return vq; |
| 566 | } |
| 567 | |
| 568 | static int virtscsi_queuecommand_multi(struct Scsi_Host *sh, |
| 569 | struct scsi_cmnd *sc) |
| 570 | { |
| 571 | struct virtio_scsi *vscsi = shost_priv(sh); |
| 572 | struct virtio_scsi_target_state *tgt = |
| 573 | scsi_target(sc->device)->hostdata; |
| 574 | struct virtio_scsi_vq *req_vq = virtscsi_pick_vq(vscsi, tgt); |
| 575 | |
| 576 | return virtscsi_queuecommand(vscsi, req_vq, sc); |
| 577 | } |
| 578 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 579 | static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd) |
| 580 | { |
| 581 | DECLARE_COMPLETION_ONSTACK(comp); |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 582 | int ret = FAILED; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 583 | |
| 584 | cmd->comp = ∁ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 585 | if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd, |
Rusty Russell | c77fba9 | 2014-05-21 11:25:04 +0930 | [diff] [blame] | 586 | sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0) |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 587 | goto out; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 588 | |
| 589 | wait_for_completion(&comp); |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 590 | if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK || |
| 591 | cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED) |
| 592 | ret = SUCCESS; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 593 | |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 594 | out: |
| 595 | mempool_free(cmd, virtscsi_cmd_pool); |
| 596 | return ret; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static int virtscsi_device_reset(struct scsi_cmnd *sc) |
| 600 | { |
| 601 | struct virtio_scsi *vscsi = shost_priv(sc->device->host); |
| 602 | struct virtio_scsi_cmd *cmd; |
| 603 | |
| 604 | sdev_printk(KERN_INFO, sc->device, "device reset\n"); |
| 605 | cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO); |
| 606 | if (!cmd) |
| 607 | return FAILED; |
| 608 | |
| 609 | memset(cmd, 0, sizeof(*cmd)); |
| 610 | cmd->sc = sc; |
| 611 | cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){ |
| 612 | .type = VIRTIO_SCSI_T_TMF, |
| 613 | .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET, |
| 614 | .lun[0] = 1, |
| 615 | .lun[1] = sc->device->id, |
| 616 | .lun[2] = (sc->device->lun >> 8) | 0x40, |
| 617 | .lun[3] = sc->device->lun & 0xff, |
| 618 | }; |
| 619 | return virtscsi_tmf(vscsi, cmd); |
| 620 | } |
| 621 | |
| 622 | static int virtscsi_abort(struct scsi_cmnd *sc) |
| 623 | { |
| 624 | struct virtio_scsi *vscsi = shost_priv(sc->device->host); |
| 625 | struct virtio_scsi_cmd *cmd; |
| 626 | |
| 627 | scmd_printk(KERN_INFO, sc, "abort\n"); |
| 628 | cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO); |
| 629 | if (!cmd) |
| 630 | return FAILED; |
| 631 | |
| 632 | memset(cmd, 0, sizeof(*cmd)); |
| 633 | cmd->sc = sc; |
| 634 | cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){ |
| 635 | .type = VIRTIO_SCSI_T_TMF, |
| 636 | .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK, |
| 637 | .lun[0] = 1, |
| 638 | .lun[1] = sc->device->id, |
| 639 | .lun[2] = (sc->device->lun >> 8) | 0x40, |
| 640 | .lun[3] = sc->device->lun & 0xff, |
| 641 | .tag = (unsigned long)sc, |
| 642 | }; |
| 643 | return virtscsi_tmf(vscsi, cmd); |
| 644 | } |
| 645 | |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 646 | static int virtscsi_target_alloc(struct scsi_target *starget) |
| 647 | { |
| 648 | struct virtio_scsi_target_state *tgt = |
| 649 | kmalloc(sizeof(*tgt), GFP_KERNEL); |
| 650 | if (!tgt) |
| 651 | return -ENOMEM; |
| 652 | |
| 653 | spin_lock_init(&tgt->tgt_lock); |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 654 | atomic_set(&tgt->reqs, 0); |
| 655 | tgt->req_vq = NULL; |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 656 | |
| 657 | starget->hostdata = tgt; |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static void virtscsi_target_destroy(struct scsi_target *starget) |
| 662 | { |
| 663 | struct virtio_scsi_target_state *tgt = starget->hostdata; |
| 664 | kfree(tgt); |
| 665 | } |
| 666 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 667 | static struct scsi_host_template virtscsi_host_template_single = { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 668 | .module = THIS_MODULE, |
| 669 | .name = "Virtio SCSI HBA", |
| 670 | .proc_name = "virtio_scsi", |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 671 | .this_id = -1, |
Christoph Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 672 | .cmd_size = sizeof(struct virtio_scsi_cmd), |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 673 | .queuecommand = virtscsi_queuecommand_single, |
| 674 | .eh_abort_handler = virtscsi_abort, |
| 675 | .eh_device_reset_handler = virtscsi_device_reset, |
| 676 | |
| 677 | .can_queue = 1024, |
| 678 | .dma_boundary = UINT_MAX, |
| 679 | .use_clustering = ENABLE_CLUSTERING, |
| 680 | .target_alloc = virtscsi_target_alloc, |
| 681 | .target_destroy = virtscsi_target_destroy, |
| 682 | }; |
| 683 | |
| 684 | static struct scsi_host_template virtscsi_host_template_multi = { |
| 685 | .module = THIS_MODULE, |
| 686 | .name = "Virtio SCSI HBA", |
| 687 | .proc_name = "virtio_scsi", |
| 688 | .this_id = -1, |
Christoph Hellwig | b54197c | 2014-05-01 16:51:50 +0200 | [diff] [blame] | 689 | .cmd_size = sizeof(struct virtio_scsi_cmd), |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 690 | .queuecommand = virtscsi_queuecommand_multi, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 691 | .eh_abort_handler = virtscsi_abort, |
| 692 | .eh_device_reset_handler = virtscsi_device_reset, |
| 693 | |
| 694 | .can_queue = 1024, |
| 695 | .dma_boundary = UINT_MAX, |
| 696 | .use_clustering = ENABLE_CLUSTERING, |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 697 | .target_alloc = virtscsi_target_alloc, |
| 698 | .target_destroy = virtscsi_target_destroy, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 699 | }; |
| 700 | |
| 701 | #define virtscsi_config_get(vdev, fld) \ |
| 702 | ({ \ |
| 703 | typeof(((struct virtio_scsi_config *)0)->fld) __val; \ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 704 | virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \ |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 705 | __val; \ |
| 706 | }) |
| 707 | |
| 708 | #define virtscsi_config_set(vdev, fld, val) \ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 709 | do { \ |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 710 | typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \ |
Rusty Russell | 855e0c5 | 2013-10-14 18:11:51 +1030 | [diff] [blame] | 711 | virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \ |
| 712 | } while(0) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 713 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 714 | static void __virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity) |
| 715 | { |
| 716 | int i; |
| 717 | int cpu; |
| 718 | |
| 719 | /* In multiqueue mode, when the number of cpu is equal |
| 720 | * to the number of request queues, we let the qeueues |
| 721 | * to be private to one cpu by setting the affinity hint |
| 722 | * to eliminate the contention. |
| 723 | */ |
| 724 | if ((vscsi->num_queues == 1 || |
| 725 | vscsi->num_queues != num_online_cpus()) && affinity) { |
| 726 | if (vscsi->affinity_hint_set) |
| 727 | affinity = false; |
| 728 | else |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | if (affinity) { |
| 733 | i = 0; |
| 734 | for_each_online_cpu(cpu) { |
| 735 | virtqueue_set_affinity(vscsi->req_vqs[i].vq, cpu); |
| 736 | i++; |
| 737 | } |
| 738 | |
| 739 | vscsi->affinity_hint_set = true; |
| 740 | } else { |
Fam Zheng | 0c8482a | 2014-04-14 10:16:09 +0800 | [diff] [blame] | 741 | for (i = 0; i < vscsi->num_queues; i++) { |
| 742 | if (!vscsi->req_vqs[i].vq) |
| 743 | continue; |
| 744 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 745 | virtqueue_set_affinity(vscsi->req_vqs[i].vq, -1); |
Fam Zheng | 0c8482a | 2014-04-14 10:16:09 +0800 | [diff] [blame] | 746 | } |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 747 | |
| 748 | vscsi->affinity_hint_set = false; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | static void virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity) |
| 753 | { |
| 754 | get_online_cpus(); |
| 755 | __virtscsi_set_affinity(vscsi, affinity); |
| 756 | put_online_cpus(); |
| 757 | } |
| 758 | |
Wanlong Gao | 285e71e | 2013-04-08 23:05:49 +0930 | [diff] [blame] | 759 | static int virtscsi_cpu_callback(struct notifier_block *nfb, |
| 760 | unsigned long action, void *hcpu) |
| 761 | { |
| 762 | struct virtio_scsi *vscsi = container_of(nfb, struct virtio_scsi, nb); |
| 763 | switch(action) { |
| 764 | case CPU_ONLINE: |
| 765 | case CPU_ONLINE_FROZEN: |
| 766 | case CPU_DEAD: |
| 767 | case CPU_DEAD_FROZEN: |
| 768 | __virtscsi_set_affinity(vscsi, true); |
| 769 | break; |
| 770 | default: |
| 771 | break; |
| 772 | } |
| 773 | return NOTIFY_OK; |
| 774 | } |
| 775 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 776 | static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq, |
| 777 | struct virtqueue *vq) |
| 778 | { |
| 779 | spin_lock_init(&virtscsi_vq->vq_lock); |
| 780 | virtscsi_vq->vq = vq; |
| 781 | } |
| 782 | |
Nicholas Bellinger | 59057fb | 2012-07-11 21:22:16 +0000 | [diff] [blame] | 783 | static void virtscsi_scan(struct virtio_device *vdev) |
| 784 | { |
| 785 | struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv; |
| 786 | |
| 787 | scsi_scan_host(shost); |
| 788 | } |
| 789 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 790 | static void virtscsi_remove_vqs(struct virtio_device *vdev) |
| 791 | { |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 792 | struct Scsi_Host *sh = virtio_scsi_host(vdev); |
| 793 | struct virtio_scsi *vscsi = shost_priv(sh); |
| 794 | |
| 795 | virtscsi_set_affinity(vscsi, false); |
| 796 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 797 | /* Stop all the virtqueues. */ |
| 798 | vdev->config->reset(vdev); |
| 799 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 800 | vdev->config->del_vqs(vdev); |
| 801 | } |
| 802 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 803 | static int virtscsi_init(struct virtio_device *vdev, |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 804 | struct virtio_scsi *vscsi) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 805 | { |
| 806 | int err; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 807 | u32 i; |
| 808 | u32 num_vqs; |
| 809 | vq_callback_t **callbacks; |
| 810 | const char **names; |
| 811 | struct virtqueue **vqs; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 812 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 813 | num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE; |
| 814 | vqs = kmalloc(num_vqs * sizeof(struct virtqueue *), GFP_KERNEL); |
| 815 | callbacks = kmalloc(num_vqs * sizeof(vq_callback_t *), GFP_KERNEL); |
| 816 | names = kmalloc(num_vqs * sizeof(char *), GFP_KERNEL); |
| 817 | |
| 818 | if (!callbacks || !vqs || !names) { |
| 819 | err = -ENOMEM; |
| 820 | goto out; |
| 821 | } |
| 822 | |
| 823 | callbacks[0] = virtscsi_ctrl_done; |
| 824 | callbacks[1] = virtscsi_event_done; |
| 825 | names[0] = "control"; |
| 826 | names[1] = "event"; |
| 827 | for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) { |
| 828 | callbacks[i] = virtscsi_req_done; |
| 829 | names[i] = "request"; |
| 830 | } |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 831 | |
| 832 | /* Discover virtqueues and write information to configuration. */ |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 833 | err = vdev->config->find_vqs(vdev, num_vqs, vqs, callbacks, names); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 834 | if (err) |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 835 | goto out; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 836 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 837 | virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]); |
| 838 | virtscsi_init_vq(&vscsi->event_vq, vqs[1]); |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 839 | for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) |
| 840 | virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE], |
| 841 | vqs[i]); |
| 842 | |
| 843 | virtscsi_set_affinity(vscsi, true); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 844 | |
| 845 | virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE); |
| 846 | virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE); |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 847 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 848 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) |
| 849 | virtscsi_kick_event_all(vscsi); |
| 850 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 851 | err = 0; |
| 852 | |
| 853 | out: |
| 854 | kfree(names); |
| 855 | kfree(callbacks); |
| 856 | kfree(vqs); |
| 857 | if (err) |
| 858 | virtscsi_remove_vqs(vdev); |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 859 | return err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 862 | static int virtscsi_probe(struct virtio_device *vdev) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 863 | { |
| 864 | struct Scsi_Host *shost; |
| 865 | struct virtio_scsi *vscsi; |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 866 | int err, host_prot; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 867 | u32 sg_elems, num_targets; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 868 | u32 cmd_per_lun; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 869 | u32 num_queues; |
| 870 | struct scsi_host_template *hostt; |
| 871 | |
| 872 | /* We need to know how many queues before we allocate. */ |
| 873 | num_queues = virtscsi_config_get(vdev, num_queues) ? : 1; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 874 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 875 | num_targets = virtscsi_config_get(vdev, max_target) + 1; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 876 | |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 877 | if (num_queues == 1) |
| 878 | hostt = &virtscsi_host_template_single; |
| 879 | else |
| 880 | hostt = &virtscsi_host_template_multi; |
| 881 | |
| 882 | shost = scsi_host_alloc(hostt, |
| 883 | sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 884 | if (!shost) |
| 885 | return -ENOMEM; |
| 886 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 887 | sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 888 | shost->sg_tablesize = sg_elems; |
| 889 | vscsi = shost_priv(shost); |
| 890 | vscsi->vdev = vdev; |
Paolo Bonzini | 9141a4c | 2013-04-08 23:03:25 +0930 | [diff] [blame] | 891 | vscsi->num_queues = num_queues; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 892 | vdev->priv = shost; |
| 893 | |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 894 | err = virtscsi_init(vdev, vscsi); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 895 | if (err) |
| 896 | goto virtscsi_init_failed; |
| 897 | |
Wanlong Gao | 285e71e | 2013-04-08 23:05:49 +0930 | [diff] [blame] | 898 | vscsi->nb.notifier_call = &virtscsi_cpu_callback; |
| 899 | err = register_hotcpu_notifier(&vscsi->nb); |
| 900 | if (err) { |
| 901 | pr_err("registering cpu notifier failed\n"); |
| 902 | goto scsi_add_host_failed; |
| 903 | } |
| 904 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 905 | cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1; |
| 906 | shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue); |
| 907 | shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF; |
Paolo Bonzini | 9da5f5a | 2012-10-02 17:25:47 +0200 | [diff] [blame] | 908 | |
| 909 | /* LUNs > 256 are reported with format 1, so they go in the range |
| 910 | * 16640-32767. |
| 911 | */ |
| 912 | shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 913 | shost->max_id = num_targets; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 914 | shost->max_channel = 0; |
| 915 | shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE; |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 916 | |
| 917 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_T10_PI)) { |
| 918 | host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION | |
| 919 | SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION | |
| 920 | SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION; |
| 921 | |
| 922 | scsi_host_set_prot(shost, host_prot); |
| 923 | scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC); |
| 924 | } |
| 925 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 926 | err = scsi_add_host(shost, &vdev->dev); |
| 927 | if (err) |
| 928 | goto scsi_add_host_failed; |
Nicholas Bellinger | 59057fb | 2012-07-11 21:22:16 +0000 | [diff] [blame] | 929 | /* |
| 930 | * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan() |
| 931 | * after VIRTIO_CONFIG_S_DRIVER_OK has been set.. |
| 932 | */ |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 933 | return 0; |
| 934 | |
| 935 | scsi_add_host_failed: |
| 936 | vdev->config->del_vqs(vdev); |
| 937 | virtscsi_init_failed: |
| 938 | scsi_host_put(shost); |
| 939 | return err; |
| 940 | } |
| 941 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 942 | static void virtscsi_remove(struct virtio_device *vdev) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 943 | { |
| 944 | struct Scsi_Host *shost = virtio_scsi_host(vdev); |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 945 | struct virtio_scsi *vscsi = shost_priv(shost); |
| 946 | |
| 947 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) |
| 948 | virtscsi_cancel_event_work(vscsi); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 949 | |
| 950 | scsi_remove_host(shost); |
| 951 | |
Wanlong Gao | 285e71e | 2013-04-08 23:05:49 +0930 | [diff] [blame] | 952 | unregister_hotcpu_notifier(&vscsi->nb); |
| 953 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 954 | virtscsi_remove_vqs(vdev); |
| 955 | scsi_host_put(shost); |
| 956 | } |
| 957 | |
Aaron Lu | 8910700 | 2013-09-17 09:25:23 +0930 | [diff] [blame] | 958 | #ifdef CONFIG_PM_SLEEP |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 959 | static int virtscsi_freeze(struct virtio_device *vdev) |
| 960 | { |
Asias He | f466f75 | 2014-01-16 10:18:48 +1030 | [diff] [blame] | 961 | struct Scsi_Host *sh = virtio_scsi_host(vdev); |
| 962 | struct virtio_scsi *vscsi = shost_priv(sh); |
| 963 | |
| 964 | unregister_hotcpu_notifier(&vscsi->nb); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 965 | virtscsi_remove_vqs(vdev); |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | static int virtscsi_restore(struct virtio_device *vdev) |
| 970 | { |
| 971 | struct Scsi_Host *sh = virtio_scsi_host(vdev); |
| 972 | struct virtio_scsi *vscsi = shost_priv(sh); |
Asias He | f466f75 | 2014-01-16 10:18:48 +1030 | [diff] [blame] | 973 | int err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 974 | |
Asias He | f466f75 | 2014-01-16 10:18:48 +1030 | [diff] [blame] | 975 | err = virtscsi_init(vdev, vscsi); |
| 976 | if (err) |
| 977 | return err; |
| 978 | |
| 979 | err = register_hotcpu_notifier(&vscsi->nb); |
| 980 | if (err) |
| 981 | vdev->config->del_vqs(vdev); |
| 982 | |
| 983 | return err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 984 | } |
| 985 | #endif |
| 986 | |
| 987 | static struct virtio_device_id id_table[] = { |
| 988 | { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID }, |
| 989 | { 0 }, |
| 990 | }; |
| 991 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 992 | static unsigned int features[] = { |
Paolo Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 993 | VIRTIO_SCSI_F_HOTPLUG, |
| 994 | VIRTIO_SCSI_F_CHANGE, |
Nicholas Bellinger | e6dc783 | 2014-02-22 18:23:33 -0800 | [diff] [blame] | 995 | VIRTIO_SCSI_F_T10_PI, |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 996 | }; |
| 997 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 998 | static struct virtio_driver virtio_scsi_driver = { |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 999 | .feature_table = features, |
| 1000 | .feature_table_size = ARRAY_SIZE(features), |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 1001 | .driver.name = KBUILD_MODNAME, |
| 1002 | .driver.owner = THIS_MODULE, |
| 1003 | .id_table = id_table, |
| 1004 | .probe = virtscsi_probe, |
Nicholas Bellinger | 59057fb | 2012-07-11 21:22:16 +0000 | [diff] [blame] | 1005 | .scan = virtscsi_scan, |
Aaron Lu | 8910700 | 2013-09-17 09:25:23 +0930 | [diff] [blame] | 1006 | #ifdef CONFIG_PM_SLEEP |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 1007 | .freeze = virtscsi_freeze, |
| 1008 | .restore = virtscsi_restore, |
| 1009 | #endif |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 1010 | .remove = virtscsi_remove, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 1011 | }; |
| 1012 | |
| 1013 | static int __init init(void) |
| 1014 | { |
| 1015 | int ret = -ENOMEM; |
| 1016 | |
| 1017 | virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0); |
| 1018 | if (!virtscsi_cmd_cache) { |
Wanlong Gao | ba06d1e | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 1019 | pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n"); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 1020 | goto error; |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | virtscsi_cmd_pool = |
| 1025 | mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ, |
| 1026 | virtscsi_cmd_cache); |
| 1027 | if (!virtscsi_cmd_pool) { |
Wanlong Gao | ba06d1e | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 1028 | pr_err("mempool_create() for virtscsi_cmd_pool failed\n"); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 1029 | goto error; |
| 1030 | } |
| 1031 | ret = register_virtio_driver(&virtio_scsi_driver); |
| 1032 | if (ret < 0) |
| 1033 | goto error; |
| 1034 | |
| 1035 | return 0; |
| 1036 | |
| 1037 | error: |
| 1038 | if (virtscsi_cmd_pool) { |
| 1039 | mempool_destroy(virtscsi_cmd_pool); |
| 1040 | virtscsi_cmd_pool = NULL; |
| 1041 | } |
| 1042 | if (virtscsi_cmd_cache) { |
| 1043 | kmem_cache_destroy(virtscsi_cmd_cache); |
| 1044 | virtscsi_cmd_cache = NULL; |
| 1045 | } |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | static void __exit fini(void) |
| 1050 | { |
| 1051 | unregister_virtio_driver(&virtio_scsi_driver); |
| 1052 | mempool_destroy(virtscsi_cmd_pool); |
| 1053 | kmem_cache_destroy(virtscsi_cmd_cache); |
| 1054 | } |
| 1055 | module_init(init); |
| 1056 | module_exit(fini); |
| 1057 | |
| 1058 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 1059 | MODULE_DESCRIPTION("Virtio SCSI HBA driver"); |
| 1060 | MODULE_LICENSE("GPL"); |