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