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