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