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