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