Merge branch 'x86-mm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / drivers / scsi / virtio_scsi.c
index 9fc5e67a0ca55233098313f92ddd514ebad71c25..c7030fbee79c254ace6a41eed0a88e6b968e6bfb 100644 (file)
@@ -25,6 +25,7 @@
 #include <scsi/scsi_cmnd.h>
 
 #define VIRTIO_SCSI_MEMPOOL_SZ 64
+#define VIRTIO_SCSI_EVENT_LEN 8
 
 /* Command queue element */
 struct virtio_scsi_cmd {
@@ -43,6 +44,12 @@ struct virtio_scsi_cmd {
        } resp;
 } ____cacheline_aligned_in_smp;
 
+struct virtio_scsi_event_node {
+       struct virtio_scsi *vscsi;
+       struct virtio_scsi_event event;
+       struct work_struct work;
+};
+
 struct virtio_scsi_vq {
        /* Protects vq */
        spinlock_t vq_lock;
@@ -67,6 +74,9 @@ struct virtio_scsi {
        struct virtio_scsi_vq event_vq;
        struct virtio_scsi_vq req_vq;
 
+       /* Get some buffers ready for event vq */
+       struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
+
        struct virtio_scsi_target_state *tgt[];
 };
 
@@ -202,6 +212,105 @@ static void virtscsi_ctrl_done(struct virtqueue *vq)
        spin_unlock_irqrestore(&vscsi->ctrl_vq.vq_lock, flags);
 };
 
+static int virtscsi_kick_event(struct virtio_scsi *vscsi,
+                              struct virtio_scsi_event_node *event_node)
+{
+       int ret;
+       struct scatterlist sg;
+       unsigned long flags;
+
+       sg_set_buf(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
+
+       spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
+
+       ret = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node, GFP_ATOMIC);
+       if (ret >= 0)
+               virtqueue_kick(vscsi->event_vq.vq);
+
+       spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
+
+       return ret;
+}
+
+static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
+{
+       int i;
+
+       for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
+               vscsi->event_list[i].vscsi = vscsi;
+               virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
+       }
+
+       return 0;
+}
+
+static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
+{
+       int i;
+
+       for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
+               cancel_work_sync(&vscsi->event_list[i].work);
+}
+
+static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
+                                               struct virtio_scsi_event *event)
+{
+       struct scsi_device *sdev;
+       struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
+       unsigned int target = event->lun[1];
+       unsigned int lun = (event->lun[2] << 8) | event->lun[3];
+
+       switch (event->reason) {
+       case VIRTIO_SCSI_EVT_RESET_RESCAN:
+               scsi_add_device(shost, 0, target, lun);
+               break;
+       case VIRTIO_SCSI_EVT_RESET_REMOVED:
+               sdev = scsi_device_lookup(shost, 0, target, lun);
+               if (sdev) {
+                       scsi_remove_device(sdev);
+                       scsi_device_put(sdev);
+               } else {
+                       pr_err("SCSI device %d 0 %d %d not found\n",
+                               shost->host_no, target, lun);
+               }
+               break;
+       default:
+               pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
+       }
+}
+
+static void virtscsi_handle_event(struct work_struct *work)
+{
+       struct virtio_scsi_event_node *event_node =
+               container_of(work, struct virtio_scsi_event_node, work);
+       struct virtio_scsi *vscsi = event_node->vscsi;
+       struct virtio_scsi_event *event = &event_node->event;
+
+       if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
+               event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
+               scsi_scan_host(virtio_scsi_host(vscsi->vdev));
+       }
+
+       switch (event->event) {
+       case VIRTIO_SCSI_T_NO_EVENT:
+               break;
+       case VIRTIO_SCSI_T_TRANSPORT_RESET:
+               virtscsi_handle_transport_reset(vscsi, event);
+               break;
+       default:
+               pr_err("Unsupport virtio scsi event %x\n", event->event);
+       }
+       virtscsi_kick_event(vscsi, event_node);
+}
+
+static void virtscsi_complete_event(void *buf)
+{
+       struct virtio_scsi_event_node *event_node = buf;
+
+       INIT_WORK(&event_node->work, virtscsi_handle_event);
+       schedule_work(&event_node->work);
+}
+
 static void virtscsi_event_done(struct virtqueue *vq)
 {
        struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
@@ -209,7 +318,7 @@ static void virtscsi_event_done(struct virtqueue *vq)
        unsigned long flags;
 
        spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
-       virtscsi_vq_done(vq, virtscsi_complete_free);
+       virtscsi_vq_done(vq, virtscsi_complete_event);
        spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
 };
 
@@ -462,6 +571,13 @@ static struct virtio_scsi_target_state *virtscsi_alloc_tgt(
        return tgt;
 }
 
+static void virtscsi_scan(struct virtio_device *vdev)
+{
+       struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
+
+       scsi_scan_host(shost);
+}
+
 static void virtscsi_remove_vqs(struct virtio_device *vdev)
 {
        struct Scsi_Host *sh = virtio_scsi_host(vdev);
@@ -510,6 +626,9 @@ static int virtscsi_init(struct virtio_device *vdev,
        virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
        virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
 
+       if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
+               virtscsi_kick_event_all(vscsi);
+
        /* We need to know how many segments before we allocate.  */
        sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
 
@@ -565,9 +684,10 @@ static int __devinit virtscsi_probe(struct virtio_device *vdev)
        err = scsi_add_host(shost, &vdev->dev);
        if (err)
                goto scsi_add_host_failed;
-
-       scsi_scan_host(shost);
-
+       /*
+        * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
+        * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
+        */
        return 0;
 
 scsi_add_host_failed:
@@ -580,6 +700,10 @@ virtscsi_init_failed:
 static void __devexit virtscsi_remove(struct virtio_device *vdev)
 {
        struct Scsi_Host *shost = virtio_scsi_host(vdev);
+       struct virtio_scsi *vscsi = shost_priv(shost);
+
+       if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
+               virtscsi_cancel_event_work(vscsi);
 
        scsi_remove_host(shost);
 
@@ -608,11 +732,18 @@ static struct virtio_device_id id_table[] = {
        { 0 },
 };
 
+static unsigned int features[] = {
+       VIRTIO_SCSI_F_HOTPLUG
+};
+
 static struct virtio_driver virtio_scsi_driver = {
+       .feature_table = features,
+       .feature_table_size = ARRAY_SIZE(features),
        .driver.name = KBUILD_MODNAME,
        .driver.owner = THIS_MODULE,
        .id_table = id_table,
        .probe = virtscsi_probe,
+       .scan = virtscsi_scan,
 #ifdef CONFIG_PM
        .freeze = virtscsi_freeze,
        .restore = virtscsi_restore,
This page took 0.028164 seconds and 5 git commands to generate.