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