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> |
| 25 | #include <scsi/scsi_host.h> |
| 26 | #include <scsi/scsi_device.h> |
| 27 | #include <scsi/scsi_cmnd.h> |
| 28 | |
| 29 | #define VIRTIO_SCSI_MEMPOOL_SZ 64 |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 30 | #define VIRTIO_SCSI_EVENT_LEN 8 |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 31 | |
| 32 | /* Command queue element */ |
| 33 | struct virtio_scsi_cmd { |
| 34 | struct scsi_cmnd *sc; |
| 35 | struct completion *comp; |
| 36 | union { |
| 37 | struct virtio_scsi_cmd_req cmd; |
| 38 | struct virtio_scsi_ctrl_tmf_req tmf; |
| 39 | struct virtio_scsi_ctrl_an_req an; |
| 40 | } req; |
| 41 | union { |
| 42 | struct virtio_scsi_cmd_resp cmd; |
| 43 | struct virtio_scsi_ctrl_tmf_resp tmf; |
| 44 | struct virtio_scsi_ctrl_an_resp an; |
| 45 | struct virtio_scsi_event evt; |
| 46 | } resp; |
| 47 | } ____cacheline_aligned_in_smp; |
| 48 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 49 | struct virtio_scsi_event_node { |
| 50 | struct virtio_scsi *vscsi; |
| 51 | struct virtio_scsi_event event; |
| 52 | struct work_struct work; |
| 53 | }; |
| 54 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 55 | struct virtio_scsi_vq { |
| 56 | /* Protects vq */ |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 57 | spinlock_t vq_lock; |
| 58 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 59 | struct virtqueue *vq; |
| 60 | }; |
| 61 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 62 | /* Per-target queue state */ |
| 63 | struct virtio_scsi_target_state { |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 64 | /* Never held at the same time as vq_lock. */ |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 65 | spinlock_t tgt_lock; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 66 | }; |
| 67 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 68 | /* Driver instance state */ |
| 69 | struct virtio_scsi { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 70 | struct virtio_device *vdev; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 71 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 72 | struct virtio_scsi_vq ctrl_vq; |
| 73 | struct virtio_scsi_vq event_vq; |
| 74 | struct virtio_scsi_vq req_vq; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 75 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 76 | /* Get some buffers ready for event vq */ |
| 77 | struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN]; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static struct kmem_cache *virtscsi_cmd_cache; |
| 81 | static mempool_t *virtscsi_cmd_pool; |
| 82 | |
| 83 | static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev) |
| 84 | { |
| 85 | return vdev->priv; |
| 86 | } |
| 87 | |
| 88 | static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid) |
| 89 | { |
| 90 | if (!resid) |
| 91 | return; |
| 92 | |
| 93 | if (!scsi_bidi_cmnd(sc)) { |
| 94 | scsi_set_resid(sc, resid); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | scsi_in(sc)->resid = min(resid, scsi_in(sc)->length); |
| 99 | scsi_out(sc)->resid = resid - scsi_in(sc)->resid; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done |
| 104 | * |
| 105 | * Called with vq_lock held. |
| 106 | */ |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 107 | static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 108 | { |
| 109 | struct virtio_scsi_cmd *cmd = buf; |
| 110 | struct scsi_cmnd *sc = cmd->sc; |
| 111 | struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd; |
| 112 | |
| 113 | dev_dbg(&sc->device->sdev_gendev, |
| 114 | "cmd %p response %u status %#02x sense_len %u\n", |
| 115 | sc, resp->response, resp->status, resp->sense_len); |
| 116 | |
| 117 | sc->result = resp->status; |
| 118 | virtscsi_compute_resid(sc, resp->resid); |
| 119 | switch (resp->response) { |
| 120 | case VIRTIO_SCSI_S_OK: |
| 121 | set_host_byte(sc, DID_OK); |
| 122 | break; |
| 123 | case VIRTIO_SCSI_S_OVERRUN: |
| 124 | set_host_byte(sc, DID_ERROR); |
| 125 | break; |
| 126 | case VIRTIO_SCSI_S_ABORTED: |
| 127 | set_host_byte(sc, DID_ABORT); |
| 128 | break; |
| 129 | case VIRTIO_SCSI_S_BAD_TARGET: |
| 130 | set_host_byte(sc, DID_BAD_TARGET); |
| 131 | break; |
| 132 | case VIRTIO_SCSI_S_RESET: |
| 133 | set_host_byte(sc, DID_RESET); |
| 134 | break; |
| 135 | case VIRTIO_SCSI_S_BUSY: |
| 136 | set_host_byte(sc, DID_BUS_BUSY); |
| 137 | break; |
| 138 | case VIRTIO_SCSI_S_TRANSPORT_FAILURE: |
| 139 | set_host_byte(sc, DID_TRANSPORT_DISRUPTED); |
| 140 | break; |
| 141 | case VIRTIO_SCSI_S_TARGET_FAILURE: |
| 142 | set_host_byte(sc, DID_TARGET_FAILURE); |
| 143 | break; |
| 144 | case VIRTIO_SCSI_S_NEXUS_FAILURE: |
| 145 | set_host_byte(sc, DID_NEXUS_FAILURE); |
| 146 | break; |
| 147 | default: |
| 148 | scmd_printk(KERN_WARNING, sc, "Unknown response %d", |
| 149 | resp->response); |
| 150 | /* fall through */ |
| 151 | case VIRTIO_SCSI_S_FAILURE: |
| 152 | set_host_byte(sc, DID_ERROR); |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE); |
| 157 | if (sc->sense_buffer) { |
| 158 | memcpy(sc->sense_buffer, resp->sense, |
| 159 | min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE)); |
| 160 | if (resp->sense_len) |
| 161 | set_driver_byte(sc, DRIVER_SENSE); |
| 162 | } |
| 163 | |
| 164 | mempool_free(cmd, virtscsi_cmd_pool); |
| 165 | sc->scsi_done(sc); |
| 166 | } |
| 167 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame^] | 168 | static void virtscsi_vq_done(struct virtio_scsi *vscsi, |
| 169 | struct virtio_scsi_vq *virtscsi_vq, |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 170 | void (*fn)(struct virtio_scsi *vscsi, void *buf)) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 171 | { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 172 | void *buf; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 173 | unsigned int len; |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame^] | 174 | unsigned long flags; |
| 175 | struct virtqueue *vq = virtscsi_vq->vq; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 176 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame^] | 177 | spin_lock_irqsave(&virtscsi_vq->vq_lock, flags); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 178 | do { |
| 179 | virtqueue_disable_cb(vq); |
| 180 | while ((buf = virtqueue_get_buf(vq, &len)) != NULL) |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 181 | fn(vscsi, buf); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 182 | } while (!virtqueue_enable_cb(vq)); |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame^] | 183 | spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void virtscsi_req_done(struct virtqueue *vq) |
| 187 | { |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 188 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
| 189 | struct virtio_scsi *vscsi = shost_priv(sh); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 190 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame^] | 191 | virtscsi_vq_done(vscsi, &vscsi->req_vq, virtscsi_complete_cmd); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 192 | }; |
| 193 | |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 194 | static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 195 | { |
| 196 | struct virtio_scsi_cmd *cmd = buf; |
| 197 | |
| 198 | if (cmd->comp) |
| 199 | complete_all(cmd->comp); |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 200 | else |
| 201 | mempool_free(cmd, virtscsi_cmd_pool); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void virtscsi_ctrl_done(struct virtqueue *vq) |
| 205 | { |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 206 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
| 207 | struct virtio_scsi *vscsi = shost_priv(sh); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 208 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame^] | 209 | virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 210 | }; |
| 211 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 212 | static int virtscsi_kick_event(struct virtio_scsi *vscsi, |
| 213 | struct virtio_scsi_event_node *event_node) |
| 214 | { |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 215 | int err; |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 216 | struct scatterlist sg; |
| 217 | unsigned long flags; |
| 218 | |
Richard W.M. Jones | 2e9c9df | 2012-10-02 17:25:46 +0200 | [diff] [blame] | 219 | sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event)); |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 220 | |
| 221 | spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags); |
| 222 | |
Rusty Russell | bf95829 | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 223 | err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node, |
| 224 | GFP_ATOMIC); |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 225 | if (!err) |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 226 | virtqueue_kick(vscsi->event_vq.vq); |
| 227 | |
| 228 | spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags); |
| 229 | |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 230 | return err; |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static int virtscsi_kick_event_all(struct virtio_scsi *vscsi) |
| 234 | { |
| 235 | int i; |
| 236 | |
| 237 | for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) { |
| 238 | vscsi->event_list[i].vscsi = vscsi; |
| 239 | virtscsi_kick_event(vscsi, &vscsi->event_list[i]); |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi) |
| 246 | { |
| 247 | int i; |
| 248 | |
| 249 | for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) |
| 250 | cancel_work_sync(&vscsi->event_list[i].work); |
| 251 | } |
| 252 | |
| 253 | static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi, |
| 254 | struct virtio_scsi_event *event) |
| 255 | { |
| 256 | struct scsi_device *sdev; |
| 257 | struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev); |
| 258 | unsigned int target = event->lun[1]; |
| 259 | unsigned int lun = (event->lun[2] << 8) | event->lun[3]; |
| 260 | |
| 261 | switch (event->reason) { |
| 262 | case VIRTIO_SCSI_EVT_RESET_RESCAN: |
| 263 | scsi_add_device(shost, 0, target, lun); |
| 264 | break; |
| 265 | case VIRTIO_SCSI_EVT_RESET_REMOVED: |
| 266 | sdev = scsi_device_lookup(shost, 0, target, lun); |
| 267 | if (sdev) { |
| 268 | scsi_remove_device(sdev); |
| 269 | scsi_device_put(sdev); |
| 270 | } else { |
| 271 | pr_err("SCSI device %d 0 %d %d not found\n", |
| 272 | shost->host_no, target, lun); |
| 273 | } |
| 274 | break; |
| 275 | default: |
| 276 | pr_info("Unsupport virtio scsi event reason %x\n", event->reason); |
| 277 | } |
| 278 | } |
| 279 | |
Paolo Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 280 | static void virtscsi_handle_param_change(struct virtio_scsi *vscsi, |
| 281 | struct virtio_scsi_event *event) |
| 282 | { |
| 283 | struct scsi_device *sdev; |
| 284 | struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev); |
| 285 | unsigned int target = event->lun[1]; |
| 286 | unsigned int lun = (event->lun[2] << 8) | event->lun[3]; |
| 287 | u8 asc = event->reason & 255; |
| 288 | u8 ascq = event->reason >> 8; |
| 289 | |
| 290 | sdev = scsi_device_lookup(shost, 0, target, lun); |
| 291 | if (!sdev) { |
| 292 | pr_err("SCSI device %d 0 %d %d not found\n", |
| 293 | shost->host_no, target, lun); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | /* Handle "Parameters changed", "Mode parameters changed", and |
| 298 | "Capacity data has changed". */ |
| 299 | if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09)) |
| 300 | scsi_rescan_device(&sdev->sdev_gendev); |
| 301 | |
| 302 | scsi_device_put(sdev); |
| 303 | } |
| 304 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 305 | static void virtscsi_handle_event(struct work_struct *work) |
| 306 | { |
| 307 | struct virtio_scsi_event_node *event_node = |
| 308 | container_of(work, struct virtio_scsi_event_node, work); |
| 309 | struct virtio_scsi *vscsi = event_node->vscsi; |
| 310 | struct virtio_scsi_event *event = &event_node->event; |
| 311 | |
| 312 | if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) { |
| 313 | event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED; |
| 314 | scsi_scan_host(virtio_scsi_host(vscsi->vdev)); |
| 315 | } |
| 316 | |
| 317 | switch (event->event) { |
| 318 | case VIRTIO_SCSI_T_NO_EVENT: |
| 319 | break; |
| 320 | case VIRTIO_SCSI_T_TRANSPORT_RESET: |
| 321 | virtscsi_handle_transport_reset(vscsi, event); |
| 322 | break; |
Paolo Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 323 | case VIRTIO_SCSI_T_PARAM_CHANGE: |
| 324 | virtscsi_handle_param_change(vscsi, event); |
| 325 | break; |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 326 | default: |
| 327 | pr_err("Unsupport virtio scsi event %x\n", event->event); |
| 328 | } |
| 329 | virtscsi_kick_event(vscsi, event_node); |
| 330 | } |
| 331 | |
Paolo Bonzini | 7f82b3c | 2013-04-08 23:01:38 +0930 | [diff] [blame] | 332 | static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf) |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 333 | { |
| 334 | struct virtio_scsi_event_node *event_node = buf; |
| 335 | |
| 336 | INIT_WORK(&event_node->work, virtscsi_handle_event); |
| 337 | schedule_work(&event_node->work); |
| 338 | } |
| 339 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 340 | static void virtscsi_event_done(struct virtqueue *vq) |
| 341 | { |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 342 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
| 343 | struct virtio_scsi *vscsi = shost_priv(sh); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 344 | |
Paolo Bonzini | 10f34f6 | 2013-04-08 23:02:07 +0930 | [diff] [blame^] | 345 | virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 346 | }; |
| 347 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 348 | /** |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 349 | * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue |
| 350 | * @vq : the struct virtqueue we're talking about |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 351 | * @cmd : command structure |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 352 | * @req_size : size of the request buffer |
| 353 | * @resp_size : size of the response buffer |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 354 | * @gfp : flags to use for memory allocations |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 355 | */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 356 | static int virtscsi_add_cmd(struct virtqueue *vq, |
| 357 | struct virtio_scsi_cmd *cmd, |
| 358 | size_t req_size, size_t resp_size, gfp_t gfp) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 359 | { |
| 360 | struct scsi_cmnd *sc = cmd->sc; |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 361 | struct scatterlist *sgs[4], req, resp; |
| 362 | struct sg_table *out, *in; |
| 363 | unsigned out_num = 0, in_num = 0; |
| 364 | |
| 365 | out = in = NULL; |
| 366 | |
| 367 | if (sc && sc->sc_data_direction != DMA_NONE) { |
| 368 | if (sc->sc_data_direction != DMA_FROM_DEVICE) |
| 369 | out = &scsi_out(sc)->table; |
| 370 | if (sc->sc_data_direction != DMA_TO_DEVICE) |
| 371 | in = &scsi_in(sc)->table; |
| 372 | } |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 373 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 374 | /* Request header. */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 375 | sg_init_one(&req, &cmd->req, req_size); |
| 376 | sgs[out_num++] = &req; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 377 | |
| 378 | /* Data-out buffer. */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 379 | if (out) |
| 380 | sgs[out_num++] = out->sgl; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 381 | |
| 382 | /* Response header. */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 383 | sg_init_one(&resp, &cmd->resp, resp_size); |
| 384 | sgs[out_num + in_num++] = &resp; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 385 | |
| 386 | /* Data-in buffer */ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 387 | if (in) |
| 388 | sgs[out_num + in_num++] = in->sgl; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 389 | |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 390 | return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, gfp); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 393 | static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 394 | struct virtio_scsi_cmd *cmd, |
| 395 | size_t req_size, size_t resp_size, gfp_t gfp) |
| 396 | { |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 397 | unsigned long flags; |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 398 | int err; |
| 399 | bool needs_kick = false; |
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 | spin_lock_irqsave(&vq->vq_lock, flags); |
| 402 | err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size, gfp); |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 403 | if (!err) |
| 404 | needs_kick = virtqueue_kick_prepare(vq->vq); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 405 | |
Paolo Bonzini | bce750b | 2012-06-13 16:56:33 +0200 | [diff] [blame] | 406 | spin_unlock_irqrestore(&vq->vq_lock, flags); |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 407 | |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 408 | if (needs_kick) |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 409 | virtqueue_notify(vq->vq); |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 410 | return err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc) |
| 414 | { |
| 415 | struct virtio_scsi *vscsi = shost_priv(sh); |
| 416 | struct virtio_scsi_cmd *cmd; |
| 417 | int ret; |
| 418 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 419 | struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev); |
| 420 | BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize); |
| 421 | |
| 422 | /* TODO: check feature bit and fail if unsupported? */ |
| 423 | BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL); |
| 424 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 425 | dev_dbg(&sc->device->sdev_gendev, |
| 426 | "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]); |
| 427 | |
| 428 | ret = SCSI_MLQUEUE_HOST_BUSY; |
| 429 | cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC); |
| 430 | if (!cmd) |
| 431 | goto out; |
| 432 | |
| 433 | memset(cmd, 0, sizeof(*cmd)); |
| 434 | cmd->sc = sc; |
| 435 | cmd->req.cmd = (struct virtio_scsi_cmd_req){ |
| 436 | .lun[0] = 1, |
| 437 | .lun[1] = sc->device->id, |
| 438 | .lun[2] = (sc->device->lun >> 8) | 0x40, |
| 439 | .lun[3] = sc->device->lun & 0xff, |
| 440 | .tag = (unsigned long)sc, |
| 441 | .task_attr = VIRTIO_SCSI_S_SIMPLE, |
| 442 | .prio = 0, |
| 443 | .crn = 0, |
| 444 | }; |
| 445 | |
| 446 | BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE); |
| 447 | memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len); |
| 448 | |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 449 | if (virtscsi_kick_cmd(&vscsi->req_vq, cmd, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 450 | sizeof cmd->req.cmd, sizeof cmd->resp.cmd, |
Rusty Russell | 4614e51 | 2012-10-16 23:56:16 +1030 | [diff] [blame] | 451 | GFP_ATOMIC) == 0) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 452 | ret = 0; |
Eric Northup | b56d100 | 2012-11-08 01:55:50 -0800 | [diff] [blame] | 453 | else |
| 454 | mempool_free(cmd, virtscsi_cmd_pool); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 455 | |
| 456 | out: |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd) |
| 461 | { |
| 462 | DECLARE_COMPLETION_ONSTACK(comp); |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 463 | int ret = FAILED; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 464 | |
| 465 | cmd->comp = ∁ |
Wanlong Gao | 682993b | 2013-03-20 15:44:28 +1030 | [diff] [blame] | 466 | if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd, |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 467 | sizeof cmd->req.tmf, sizeof cmd->resp.tmf, |
| 468 | GFP_NOIO) < 0) |
| 469 | goto out; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 470 | |
| 471 | wait_for_completion(&comp); |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 472 | if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK || |
| 473 | cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED) |
| 474 | ret = SUCCESS; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 475 | |
Paolo Bonzini | e4594bb | 2012-05-04 12:32:04 +0200 | [diff] [blame] | 476 | out: |
| 477 | mempool_free(cmd, virtscsi_cmd_pool); |
| 478 | return ret; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static int virtscsi_device_reset(struct scsi_cmnd *sc) |
| 482 | { |
| 483 | struct virtio_scsi *vscsi = shost_priv(sc->device->host); |
| 484 | struct virtio_scsi_cmd *cmd; |
| 485 | |
| 486 | sdev_printk(KERN_INFO, sc->device, "device reset\n"); |
| 487 | cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO); |
| 488 | if (!cmd) |
| 489 | return FAILED; |
| 490 | |
| 491 | memset(cmd, 0, sizeof(*cmd)); |
| 492 | cmd->sc = sc; |
| 493 | cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){ |
| 494 | .type = VIRTIO_SCSI_T_TMF, |
| 495 | .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET, |
| 496 | .lun[0] = 1, |
| 497 | .lun[1] = sc->device->id, |
| 498 | .lun[2] = (sc->device->lun >> 8) | 0x40, |
| 499 | .lun[3] = sc->device->lun & 0xff, |
| 500 | }; |
| 501 | return virtscsi_tmf(vscsi, cmd); |
| 502 | } |
| 503 | |
| 504 | static int virtscsi_abort(struct scsi_cmnd *sc) |
| 505 | { |
| 506 | struct virtio_scsi *vscsi = shost_priv(sc->device->host); |
| 507 | struct virtio_scsi_cmd *cmd; |
| 508 | |
| 509 | scmd_printk(KERN_INFO, sc, "abort\n"); |
| 510 | cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO); |
| 511 | if (!cmd) |
| 512 | return FAILED; |
| 513 | |
| 514 | memset(cmd, 0, sizeof(*cmd)); |
| 515 | cmd->sc = sc; |
| 516 | cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){ |
| 517 | .type = VIRTIO_SCSI_T_TMF, |
| 518 | .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK, |
| 519 | .lun[0] = 1, |
| 520 | .lun[1] = sc->device->id, |
| 521 | .lun[2] = (sc->device->lun >> 8) | 0x40, |
| 522 | .lun[3] = sc->device->lun & 0xff, |
| 523 | .tag = (unsigned long)sc, |
| 524 | }; |
| 525 | return virtscsi_tmf(vscsi, cmd); |
| 526 | } |
| 527 | |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 528 | static int virtscsi_target_alloc(struct scsi_target *starget) |
| 529 | { |
| 530 | struct virtio_scsi_target_state *tgt = |
| 531 | kmalloc(sizeof(*tgt), GFP_KERNEL); |
| 532 | if (!tgt) |
| 533 | return -ENOMEM; |
| 534 | |
| 535 | spin_lock_init(&tgt->tgt_lock); |
| 536 | |
| 537 | starget->hostdata = tgt; |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static void virtscsi_target_destroy(struct scsi_target *starget) |
| 542 | { |
| 543 | struct virtio_scsi_target_state *tgt = starget->hostdata; |
| 544 | kfree(tgt); |
| 545 | } |
| 546 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 547 | static struct scsi_host_template virtscsi_host_template = { |
| 548 | .module = THIS_MODULE, |
| 549 | .name = "Virtio SCSI HBA", |
| 550 | .proc_name = "virtio_scsi", |
| 551 | .queuecommand = virtscsi_queuecommand, |
| 552 | .this_id = -1, |
| 553 | .eh_abort_handler = virtscsi_abort, |
| 554 | .eh_device_reset_handler = virtscsi_device_reset, |
| 555 | |
| 556 | .can_queue = 1024, |
| 557 | .dma_boundary = UINT_MAX, |
| 558 | .use_clustering = ENABLE_CLUSTERING, |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 559 | .target_alloc = virtscsi_target_alloc, |
| 560 | .target_destroy = virtscsi_target_destroy, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 561 | }; |
| 562 | |
| 563 | #define virtscsi_config_get(vdev, fld) \ |
| 564 | ({ \ |
| 565 | typeof(((struct virtio_scsi_config *)0)->fld) __val; \ |
| 566 | vdev->config->get(vdev, \ |
| 567 | offsetof(struct virtio_scsi_config, fld), \ |
| 568 | &__val, sizeof(__val)); \ |
| 569 | __val; \ |
| 570 | }) |
| 571 | |
| 572 | #define virtscsi_config_set(vdev, fld, val) \ |
| 573 | (void)({ \ |
| 574 | typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \ |
| 575 | vdev->config->set(vdev, \ |
| 576 | offsetof(struct virtio_scsi_config, fld), \ |
| 577 | &__val, sizeof(__val)); \ |
| 578 | }) |
| 579 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 580 | static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq, |
| 581 | struct virtqueue *vq) |
| 582 | { |
| 583 | spin_lock_init(&virtscsi_vq->vq_lock); |
| 584 | virtscsi_vq->vq = vq; |
| 585 | } |
| 586 | |
Nicholas Bellinger | 59057fb | 2012-07-11 21:22:16 +0000 | [diff] [blame] | 587 | static void virtscsi_scan(struct virtio_device *vdev) |
| 588 | { |
| 589 | struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv; |
| 590 | |
| 591 | scsi_scan_host(shost); |
| 592 | } |
| 593 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 594 | static void virtscsi_remove_vqs(struct virtio_device *vdev) |
| 595 | { |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 596 | /* Stop all the virtqueues. */ |
| 597 | vdev->config->reset(vdev); |
| 598 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 599 | vdev->config->del_vqs(vdev); |
| 600 | } |
| 601 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 602 | static int virtscsi_init(struct virtio_device *vdev, |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 603 | struct virtio_scsi *vscsi) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 604 | { |
| 605 | int err; |
| 606 | struct virtqueue *vqs[3]; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 607 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 608 | vq_callback_t *callbacks[] = { |
| 609 | virtscsi_ctrl_done, |
| 610 | virtscsi_event_done, |
| 611 | virtscsi_req_done |
| 612 | }; |
| 613 | const char *names[] = { |
| 614 | "control", |
| 615 | "event", |
| 616 | "request" |
| 617 | }; |
| 618 | |
| 619 | /* Discover virtqueues and write information to configuration. */ |
| 620 | err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names); |
| 621 | if (err) |
| 622 | return err; |
| 623 | |
Paolo Bonzini | 139fe45 | 2012-06-13 16:56:32 +0200 | [diff] [blame] | 624 | virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]); |
| 625 | virtscsi_init_vq(&vscsi->event_vq, vqs[1]); |
| 626 | virtscsi_init_vq(&vscsi->req_vq, vqs[2]); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 627 | |
| 628 | virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE); |
| 629 | virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE); |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 630 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 631 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) |
| 632 | virtscsi_kick_event_all(vscsi); |
| 633 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 634 | return err; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 637 | static int virtscsi_probe(struct virtio_device *vdev) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 638 | { |
| 639 | struct Scsi_Host *shost; |
| 640 | struct virtio_scsi *vscsi; |
| 641 | int err; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 642 | u32 sg_elems, num_targets; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 643 | u32 cmd_per_lun; |
| 644 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 645 | num_targets = virtscsi_config_get(vdev, max_target) + 1; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 646 | |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 647 | shost = scsi_host_alloc(&virtscsi_host_template, sizeof(*vscsi)); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 648 | if (!shost) |
| 649 | return -ENOMEM; |
| 650 | |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 651 | sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 652 | shost->sg_tablesize = sg_elems; |
| 653 | vscsi = shost_priv(shost); |
| 654 | vscsi->vdev = vdev; |
| 655 | vdev->priv = shost; |
| 656 | |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 657 | err = virtscsi_init(vdev, vscsi); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 658 | if (err) |
| 659 | goto virtscsi_init_failed; |
| 660 | |
| 661 | cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1; |
| 662 | shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue); |
| 663 | shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF; |
Paolo Bonzini | 9da5f5a | 2012-10-02 17:25:47 +0200 | [diff] [blame] | 664 | |
| 665 | /* LUNs > 256 are reported with format 1, so they go in the range |
| 666 | * 16640-32767. |
| 667 | */ |
| 668 | shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000; |
Paolo Bonzini | 2bd37f0 | 2012-06-13 16:56:34 +0200 | [diff] [blame] | 669 | shost->max_id = num_targets; |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 670 | shost->max_channel = 0; |
| 671 | shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE; |
| 672 | err = scsi_add_host(shost, &vdev->dev); |
| 673 | if (err) |
| 674 | goto scsi_add_host_failed; |
Nicholas Bellinger | 59057fb | 2012-07-11 21:22:16 +0000 | [diff] [blame] | 675 | /* |
| 676 | * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan() |
| 677 | * after VIRTIO_CONFIG_S_DRIVER_OK has been set.. |
| 678 | */ |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 679 | return 0; |
| 680 | |
| 681 | scsi_add_host_failed: |
| 682 | vdev->config->del_vqs(vdev); |
| 683 | virtscsi_init_failed: |
| 684 | scsi_host_put(shost); |
| 685 | return err; |
| 686 | } |
| 687 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 688 | static void virtscsi_remove(struct virtio_device *vdev) |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 689 | { |
| 690 | struct Scsi_Host *shost = virtio_scsi_host(vdev); |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 691 | struct virtio_scsi *vscsi = shost_priv(shost); |
| 692 | |
| 693 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) |
| 694 | virtscsi_cancel_event_work(vscsi); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 695 | |
| 696 | scsi_remove_host(shost); |
| 697 | |
| 698 | virtscsi_remove_vqs(vdev); |
| 699 | scsi_host_put(shost); |
| 700 | } |
| 701 | |
| 702 | #ifdef CONFIG_PM |
| 703 | static int virtscsi_freeze(struct virtio_device *vdev) |
| 704 | { |
| 705 | virtscsi_remove_vqs(vdev); |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | static int virtscsi_restore(struct virtio_device *vdev) |
| 710 | { |
| 711 | struct Scsi_Host *sh = virtio_scsi_host(vdev); |
| 712 | struct virtio_scsi *vscsi = shost_priv(sh); |
| 713 | |
Wanlong Gao | 5c37019 | 2013-04-08 23:01:16 +0930 | [diff] [blame] | 714 | return virtscsi_init(vdev, vscsi); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 715 | } |
| 716 | #endif |
| 717 | |
| 718 | static struct virtio_device_id id_table[] = { |
| 719 | { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID }, |
| 720 | { 0 }, |
| 721 | }; |
| 722 | |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 723 | static unsigned int features[] = { |
Paolo Bonzini | 865b58c | 2012-10-02 17:25:48 +0200 | [diff] [blame] | 724 | VIRTIO_SCSI_F_HOTPLUG, |
| 725 | VIRTIO_SCSI_F_CHANGE, |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 726 | }; |
| 727 | |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 728 | static struct virtio_driver virtio_scsi_driver = { |
Cong Meng | 365a715 | 2012-07-05 17:06:43 +0800 | [diff] [blame] | 729 | .feature_table = features, |
| 730 | .feature_table_size = ARRAY_SIZE(features), |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 731 | .driver.name = KBUILD_MODNAME, |
| 732 | .driver.owner = THIS_MODULE, |
| 733 | .id_table = id_table, |
| 734 | .probe = virtscsi_probe, |
Nicholas Bellinger | 59057fb | 2012-07-11 21:22:16 +0000 | [diff] [blame] | 735 | .scan = virtscsi_scan, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 736 | #ifdef CONFIG_PM |
| 737 | .freeze = virtscsi_freeze, |
| 738 | .restore = virtscsi_restore, |
| 739 | #endif |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 740 | .remove = virtscsi_remove, |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 741 | }; |
| 742 | |
| 743 | static int __init init(void) |
| 744 | { |
| 745 | int ret = -ENOMEM; |
| 746 | |
| 747 | virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0); |
| 748 | if (!virtscsi_cmd_cache) { |
Wanlong Gao | ba06d1e | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 749 | pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n"); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 750 | goto error; |
| 751 | } |
| 752 | |
| 753 | |
| 754 | virtscsi_cmd_pool = |
| 755 | mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ, |
| 756 | virtscsi_cmd_cache); |
| 757 | if (!virtscsi_cmd_pool) { |
Wanlong Gao | ba06d1e | 2013-03-12 15:34:40 +1030 | [diff] [blame] | 758 | pr_err("mempool_create() for virtscsi_cmd_pool failed\n"); |
Paolo Bonzini | 4fe74b1 | 2012-02-05 12:16:00 +0100 | [diff] [blame] | 759 | goto error; |
| 760 | } |
| 761 | ret = register_virtio_driver(&virtio_scsi_driver); |
| 762 | if (ret < 0) |
| 763 | goto error; |
| 764 | |
| 765 | return 0; |
| 766 | |
| 767 | error: |
| 768 | if (virtscsi_cmd_pool) { |
| 769 | mempool_destroy(virtscsi_cmd_pool); |
| 770 | virtscsi_cmd_pool = NULL; |
| 771 | } |
| 772 | if (virtscsi_cmd_cache) { |
| 773 | kmem_cache_destroy(virtscsi_cmd_cache); |
| 774 | virtscsi_cmd_cache = NULL; |
| 775 | } |
| 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | static void __exit fini(void) |
| 780 | { |
| 781 | unregister_virtio_driver(&virtio_scsi_driver); |
| 782 | mempool_destroy(virtscsi_cmd_pool); |
| 783 | kmem_cache_destroy(virtscsi_cmd_cache); |
| 784 | } |
| 785 | module_init(init); |
| 786 | module_exit(fini); |
| 787 | |
| 788 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 789 | MODULE_DESCRIPTION("Virtio SCSI HBA driver"); |
| 790 | MODULE_LICENSE("GPL"); |