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