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