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