2 * Xen SCSI frontend driver
4 * Copyright (c) 2008, FUJITSU Limited
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation; or, when distributed
9 * separately from the Linux kernel or incorporated into other
10 * software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/device.h>
34 #include <linux/wait.h>
35 #include <linux/interrupt.h>
36 #include <linux/mutex.h>
37 #include <linux/spinlock.h>
38 #include <linux/sched.h>
39 #include <linux/blkdev.h>
40 #include <linux/pfn.h>
41 #include <linux/slab.h>
42 #include <linux/bitops.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_device.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_host.h>
50 #include <xen/xenbus.h>
51 #include <xen/grant_table.h>
52 #include <xen/events.h>
55 #include <xen/interface/grant_table.h>
56 #include <xen/interface/io/vscsiif.h>
57 #include <xen/interface/io/protocols.h>
59 #include <asm/xen/hypervisor.h>
62 #define GRANT_INVALID_REF 0
64 #define VSCSIFRONT_OP_ADD_LUN 1
65 #define VSCSIFRONT_OP_DEL_LUN 2
66 #define VSCSIFRONT_OP_READD_LUN 3
69 #define VSCSIIF_DEFAULT_CMD_PER_LUN 10
70 #define VSCSIIF_MAX_TARGET 64
71 #define VSCSIIF_MAX_LUN 255
73 #define VSCSIIF_RING_SIZE __CONST_RING_SIZE(vscsiif, PAGE_SIZE)
74 #define VSCSIIF_MAX_REQS VSCSIIF_RING_SIZE
76 #define vscsiif_grants_sg(_sg) (PFN_UP((_sg) * \
77 sizeof(struct scsiif_request_segment)))
79 struct vscsifrnt_shadow {
80 /* command between backend and frontend */
86 unsigned int nr_grants; /* number of grants in gref[] */
87 struct scsiif_request_segment *sg; /* scatter/gather elements */
88 struct scsiif_request_segment seg[VSCSIIF_SG_TABLESIZE];
90 /* Do reset or abort function. */
91 wait_queue_head_t wq_reset; /* reset work queue */
92 int wait_reset; /* reset work queue condition */
93 int32_t rslt_reset; /* reset response status: */
94 /* SUCCESS or FAILED or: */
95 #define RSLT_RESET_WAITING 0
96 #define RSLT_RESET_ERR -1
98 /* Requested struct scsi_cmnd is stored from kernel. */
100 int gref[vscsiif_grants_sg(SG_ALL) + SG_ALL];
103 struct vscsifrnt_info {
104 struct xenbus_device *dev;
106 struct Scsi_Host *host;
112 grant_ref_t ring_ref;
113 struct vscsiif_front_ring ring;
114 struct vscsiif_response ring_rsp;
116 spinlock_t shadow_lock;
117 DECLARE_BITMAP(shadow_free_bitmap, VSCSIIF_MAX_REQS);
118 struct vscsifrnt_shadow *shadow[VSCSIIF_MAX_REQS];
120 /* Following items are protected by the host lock. */
121 wait_queue_head_t wq_sync;
122 wait_queue_head_t wq_pause;
123 unsigned int wait_ring_available:1;
124 unsigned int waiting_pause:1;
125 unsigned int pause:1;
128 char dev_state_path[64];
129 struct task_struct *curr;
132 static DEFINE_MUTEX(scsifront_mutex);
134 static void scsifront_wake_up(struct vscsifrnt_info *info)
136 info->wait_ring_available = 0;
137 wake_up(&info->wq_sync);
140 static int scsifront_get_rqid(struct vscsifrnt_info *info)
145 spin_lock_irqsave(&info->shadow_lock, flags);
147 free = find_first_bit(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
148 __clear_bit(free, info->shadow_free_bitmap);
150 spin_unlock_irqrestore(&info->shadow_lock, flags);
155 static int _scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
157 int empty = bitmap_empty(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
159 __set_bit(id, info->shadow_free_bitmap);
160 info->shadow[id] = NULL;
162 return empty || info->wait_ring_available;
165 static void scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
170 spin_lock_irqsave(&info->shadow_lock, flags);
171 kick = _scsifront_put_rqid(info, id);
172 spin_unlock_irqrestore(&info->shadow_lock, flags);
175 scsifront_wake_up(info);
178 static int scsifront_do_request(struct vscsifrnt_info *info,
179 struct vscsifrnt_shadow *shadow)
181 struct vscsiif_front_ring *ring = &(info->ring);
182 struct vscsiif_request *ring_req;
183 struct scsi_cmnd *sc = shadow->sc;
187 if (RING_FULL(&info->ring))
190 id = scsifront_get_rqid(info); /* use id in response */
191 if (id >= VSCSIIF_MAX_REQS)
194 info->shadow[id] = shadow;
197 ring_req = RING_GET_REQUEST(&(info->ring), ring->req_prod_pvt);
198 ring->req_prod_pvt++;
201 ring_req->act = shadow->act;
202 ring_req->ref_rqid = shadow->ref_rqid;
203 ring_req->nr_segments = shadow->nr_segments;
205 ring_req->id = sc->device->id;
206 ring_req->lun = sc->device->lun;
207 ring_req->channel = sc->device->channel;
208 ring_req->cmd_len = sc->cmd_len;
210 BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
212 memcpy(ring_req->cmnd, sc->cmnd, sc->cmd_len);
214 ring_req->sc_data_direction = (uint8_t)sc->sc_data_direction;
215 ring_req->timeout_per_command = sc->request->timeout / HZ;
217 for (i = 0; i < (shadow->nr_segments & ~VSCSIIF_SG_GRANT); i++)
218 ring_req->seg[i] = shadow->seg[i];
220 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(ring, notify);
222 notify_remote_via_irq(info->irq);
227 static void scsifront_gnttab_done(struct vscsifrnt_info *info,
228 struct vscsifrnt_shadow *shadow)
232 if (shadow->sc->sc_data_direction == DMA_NONE)
235 for (i = 0; i < shadow->nr_grants; i++) {
236 if (unlikely(!gnttab_try_end_foreign_access(shadow->gref[i]))) {
237 shost_printk(KERN_ALERT, info->host, KBUILD_MODNAME
238 "grant still in use by backend\n");
246 static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info,
247 struct vscsiif_response *ring_rsp)
249 struct vscsifrnt_shadow *shadow;
250 struct scsi_cmnd *sc;
255 shadow = info->shadow[id];
260 scsifront_gnttab_done(info, shadow);
261 scsifront_put_rqid(info, id);
263 sc->result = ring_rsp->rslt;
264 scsi_set_resid(sc, ring_rsp->residual_len);
266 sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE,
267 ring_rsp->sense_len);
270 memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len);
275 static void scsifront_sync_cmd_done(struct vscsifrnt_info *info,
276 struct vscsiif_response *ring_rsp)
278 uint16_t id = ring_rsp->rqid;
280 struct vscsifrnt_shadow *shadow = info->shadow[id];
283 spin_lock_irqsave(&info->shadow_lock, flags);
284 shadow->wait_reset = 1;
285 switch (shadow->rslt_reset) {
286 case RSLT_RESET_WAITING:
287 shadow->rslt_reset = ring_rsp->rslt;
290 kick = _scsifront_put_rqid(info, id);
291 spin_unlock_irqrestore(&info->shadow_lock, flags);
294 scsifront_wake_up(info);
297 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
298 "bad reset state %d, possibly leaking %u\n",
299 shadow->rslt_reset, id);
302 spin_unlock_irqrestore(&info->shadow_lock, flags);
304 wake_up(&shadow->wq_reset);
307 static void scsifront_do_response(struct vscsifrnt_info *info,
308 struct vscsiif_response *ring_rsp)
310 if (WARN(ring_rsp->rqid >= VSCSIIF_MAX_REQS ||
311 test_bit(ring_rsp->rqid, info->shadow_free_bitmap),
312 "illegal rqid %u returned by backend!\n", ring_rsp->rqid))
315 if (info->shadow[ring_rsp->rqid]->act == VSCSIIF_ACT_SCSI_CDB)
316 scsifront_cdb_cmd_done(info, ring_rsp);
318 scsifront_sync_cmd_done(info, ring_rsp);
321 static int scsifront_ring_drain(struct vscsifrnt_info *info)
323 struct vscsiif_response *ring_rsp;
327 rp = info->ring.sring->rsp_prod;
328 rmb(); /* ordering required respective to dom0 */
329 for (i = info->ring.rsp_cons; i != rp; i++) {
330 ring_rsp = RING_GET_RESPONSE(&info->ring, i);
331 scsifront_do_response(info, ring_rsp);
334 info->ring.rsp_cons = i;
336 if (i != info->ring.req_prod_pvt)
337 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
339 info->ring.sring->rsp_event = i + 1;
344 static int scsifront_cmd_done(struct vscsifrnt_info *info)
349 spin_lock_irqsave(info->host->host_lock, flags);
351 more_to_do = scsifront_ring_drain(info);
353 info->wait_ring_available = 0;
355 spin_unlock_irqrestore(info->host->host_lock, flags);
357 wake_up(&info->wq_sync);
362 static irqreturn_t scsifront_irq_fn(int irq, void *dev_id)
364 struct vscsifrnt_info *info = dev_id;
366 while (scsifront_cmd_done(info))
367 /* Yield point for this unbounded loop. */
373 static void scsifront_finish_all(struct vscsifrnt_info *info)
376 struct vscsiif_response resp;
378 scsifront_ring_drain(info);
380 for (i = 0; i < VSCSIIF_MAX_REQS; i++) {
381 if (test_bit(i, info->shadow_free_bitmap))
385 resp.rslt = DID_RESET << 16;
386 resp.residual_len = 0;
387 scsifront_do_response(info, &resp);
391 static int map_data_for_request(struct vscsifrnt_info *info,
392 struct scsi_cmnd *sc,
393 struct vscsifrnt_shadow *shadow)
395 grant_ref_t gref_head;
397 int err, ref, ref_cnt = 0;
398 int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE);
399 unsigned int i, off, len, bytes;
400 unsigned int data_len = scsi_bufflen(sc);
401 unsigned int data_grants = 0, seg_grants = 0;
402 struct scatterlist *sg;
403 struct scsiif_request_segment *seg;
405 if (sc->sc_data_direction == DMA_NONE || !data_len)
408 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i)
409 data_grants += PFN_UP(sg->offset + sg->length);
411 if (data_grants > VSCSIIF_SG_TABLESIZE) {
412 if (data_grants > info->host->sg_tablesize) {
413 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
414 "Unable to map request_buffer for command!\n");
417 seg_grants = vscsiif_grants_sg(data_grants);
418 shadow->sg = kcalloc(data_grants,
419 sizeof(struct scsiif_request_segment), GFP_ATOMIC);
423 seg = shadow->sg ? : shadow->seg;
425 err = gnttab_alloc_grant_references(seg_grants + data_grants,
429 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
430 "gnttab_alloc_grant_references() error\n");
435 page = virt_to_page(seg);
436 off = offset_in_page(seg);
437 len = sizeof(struct scsiif_request_segment) * data_grants;
439 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
441 ref = gnttab_claim_grant_reference(&gref_head);
442 BUG_ON(ref == -ENOSPC);
444 gnttab_grant_foreign_access_ref(ref,
445 info->dev->otherend_id,
446 xen_page_to_gfn(page), 1);
447 shadow->gref[ref_cnt] = ref;
448 shadow->seg[ref_cnt].gref = ref;
449 shadow->seg[ref_cnt].offset = (uint16_t)off;
450 shadow->seg[ref_cnt].length = (uint16_t)bytes;
457 BUG_ON(seg_grants < ref_cnt);
458 seg_grants = ref_cnt;
461 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) {
466 while (len > 0 && data_len > 0) {
468 * sg sends a scatterlist that is larger than
469 * the data_len it wants transferred for certain
472 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
473 bytes = min(bytes, data_len);
475 ref = gnttab_claim_grant_reference(&gref_head);
476 BUG_ON(ref == -ENOSPC);
478 gnttab_grant_foreign_access_ref(ref,
479 info->dev->otherend_id,
480 xen_page_to_gfn(page),
483 shadow->gref[ref_cnt] = ref;
485 seg->offset = (uint16_t)off;
486 seg->length = (uint16_t)bytes;
498 shadow->nr_segments = VSCSIIF_SG_GRANT | seg_grants;
500 shadow->nr_segments = (uint8_t)ref_cnt;
501 shadow->nr_grants = ref_cnt;
506 static int scsifront_enter(struct vscsifrnt_info *info)
514 static void scsifront_return(struct vscsifrnt_info *info)
520 if (!info->waiting_pause)
523 info->waiting_pause = 0;
524 wake_up(&info->wq_pause);
527 static int scsifront_queuecommand(struct Scsi_Host *shost,
528 struct scsi_cmnd *sc)
530 struct vscsifrnt_info *info = shost_priv(shost);
531 struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc);
538 shadow->act = VSCSIIF_ACT_SCSI_CDB;
540 spin_lock_irqsave(shost->host_lock, flags);
541 if (scsifront_enter(info)) {
542 spin_unlock_irqrestore(shost->host_lock, flags);
543 return SCSI_MLQUEUE_HOST_BUSY;
546 err = map_data_for_request(info, sc, shadow);
548 pr_debug("%s: err %d\n", __func__, err);
549 scsifront_return(info);
550 spin_unlock_irqrestore(shost->host_lock, flags);
552 return SCSI_MLQUEUE_HOST_BUSY;
553 sc->result = DID_ERROR << 16;
558 if (scsifront_do_request(info, shadow)) {
559 scsifront_gnttab_done(info, shadow);
563 scsifront_return(info);
564 spin_unlock_irqrestore(shost->host_lock, flags);
569 scsifront_return(info);
570 spin_unlock_irqrestore(shost->host_lock, flags);
571 pr_debug("%s: busy\n", __func__);
572 return SCSI_MLQUEUE_HOST_BUSY;
576 * Any exception handling (reset or abort) must be forwarded to the backend.
577 * We have to wait until an answer is returned. This answer contains the
578 * result to be returned to the requestor.
580 static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)
582 struct Scsi_Host *host = sc->device->host;
583 struct vscsifrnt_info *info = shost_priv(host);
584 struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc);
587 shadow = kzalloc(sizeof(*shadow), GFP_NOIO);
592 shadow->rslt_reset = RSLT_RESET_WAITING;
594 shadow->ref_rqid = s->rqid;
595 init_waitqueue_head(&shadow->wq_reset);
597 spin_lock_irq(host->host_lock);
600 if (scsifront_enter(info))
603 if (!scsifront_do_request(info, shadow))
606 scsifront_return(info);
609 info->wait_ring_available = 1;
610 spin_unlock_irq(host->host_lock);
611 err = wait_event_interruptible(info->wq_sync,
612 !info->wait_ring_available);
613 spin_lock_irq(host->host_lock);
616 spin_unlock_irq(host->host_lock);
617 err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset);
618 spin_lock_irq(host->host_lock);
621 err = shadow->rslt_reset;
622 scsifront_put_rqid(info, shadow->rqid);
625 spin_lock(&info->shadow_lock);
626 shadow->rslt_reset = RSLT_RESET_ERR;
627 spin_unlock(&info->shadow_lock);
631 scsifront_return(info);
632 spin_unlock_irq(host->host_lock);
636 spin_unlock_irq(host->host_lock);
641 static int scsifront_eh_abort_handler(struct scsi_cmnd *sc)
643 pr_debug("%s\n", __func__);
644 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT);
647 static int scsifront_dev_reset_handler(struct scsi_cmnd *sc)
649 pr_debug("%s\n", __func__);
650 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET);
653 static int scsifront_sdev_configure(struct scsi_device *sdev)
655 struct vscsifrnt_info *info = shost_priv(sdev->host);
658 if (info && current == info->curr) {
659 err = xenbus_printf(XBT_NIL, info->dev->nodename,
660 info->dev_state_path, "%d", XenbusStateConnected);
662 xenbus_dev_error(info->dev, err,
663 "%s: writing dev_state_path", __func__);
671 static void scsifront_sdev_destroy(struct scsi_device *sdev)
673 struct vscsifrnt_info *info = shost_priv(sdev->host);
676 if (info && current == info->curr) {
677 err = xenbus_printf(XBT_NIL, info->dev->nodename,
678 info->dev_state_path, "%d", XenbusStateClosed);
680 xenbus_dev_error(info->dev, err,
681 "%s: writing dev_state_path", __func__);
685 static struct scsi_host_template scsifront_sht = {
686 .module = THIS_MODULE,
687 .name = "Xen SCSI frontend driver",
688 .queuecommand = scsifront_queuecommand,
689 .eh_abort_handler = scsifront_eh_abort_handler,
690 .eh_device_reset_handler = scsifront_dev_reset_handler,
691 .slave_configure = scsifront_sdev_configure,
692 .slave_destroy = scsifront_sdev_destroy,
693 .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN,
694 .can_queue = VSCSIIF_MAX_REQS,
696 .cmd_size = sizeof(struct vscsifrnt_shadow),
697 .sg_tablesize = VSCSIIF_SG_TABLESIZE,
698 .proc_name = "scsifront",
701 static int scsifront_alloc_ring(struct vscsifrnt_info *info)
703 struct xenbus_device *dev = info->dev;
704 struct vscsiif_sring *sring;
708 /***** Frontend to Backend ring start *****/
709 sring = (struct vscsiif_sring *)__get_free_page(GFP_KERNEL);
711 xenbus_dev_fatal(dev, err,
712 "fail to allocate shared ring (Front to Back)");
715 SHARED_RING_INIT(sring);
716 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
718 err = xenbus_grant_ring(dev, sring, 1, &gref);
720 free_page((unsigned long)sring);
721 xenbus_dev_fatal(dev, err,
722 "fail to grant shared ring (Front to Back)");
725 info->ring_ref = gref;
727 err = xenbus_alloc_evtchn(dev, &info->evtchn);
729 xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
733 err = bind_evtchn_to_irq(info->evtchn);
735 xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
741 err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn,
742 IRQF_ONESHOT, "scsifront", info);
744 xenbus_dev_fatal(dev, err, "request_threaded_irq");
752 unbind_from_irqhandler(info->irq, info);
754 gnttab_end_foreign_access(info->ring_ref, 0,
755 (unsigned long)info->ring.sring);
760 static void scsifront_free_ring(struct vscsifrnt_info *info)
762 unbind_from_irqhandler(info->irq, info);
763 gnttab_end_foreign_access(info->ring_ref, 0,
764 (unsigned long)info->ring.sring);
767 static int scsifront_init_ring(struct vscsifrnt_info *info)
769 struct xenbus_device *dev = info->dev;
770 struct xenbus_transaction xbt;
773 pr_debug("%s\n", __func__);
775 err = scsifront_alloc_ring(info);
778 pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn);
781 err = xenbus_transaction_start(&xbt);
783 xenbus_dev_fatal(dev, err, "starting transaction");
785 err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u",
788 xenbus_dev_fatal(dev, err, "%s", "writing ring-ref");
792 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
796 xenbus_dev_fatal(dev, err, "%s", "writing event-channel");
800 err = xenbus_transaction_end(xbt, 0);
804 xenbus_dev_fatal(dev, err, "completing transaction");
811 xenbus_transaction_end(xbt, 1);
813 scsifront_free_ring(info);
819 static int scsifront_probe(struct xenbus_device *dev,
820 const struct xenbus_device_id *id)
822 struct vscsifrnt_info *info;
823 struct Scsi_Host *host;
825 char name[TASK_COMM_LEN];
827 host = scsi_host_alloc(&scsifront_sht, sizeof(*info));
829 xenbus_dev_fatal(dev, err, "fail to allocate scsi host");
832 info = (struct vscsifrnt_info *)host->hostdata;
834 dev_set_drvdata(&dev->dev, info);
837 bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
839 err = scsifront_init_ring(info);
845 init_waitqueue_head(&info->wq_sync);
846 init_waitqueue_head(&info->wq_pause);
847 spin_lock_init(&info->shadow_lock);
849 snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no);
851 host->max_id = VSCSIIF_MAX_TARGET;
852 host->max_channel = 0;
853 host->max_lun = VSCSIIF_MAX_LUN;
854 host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512;
855 host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE;
857 err = scsi_add_host(host, &dev->dev);
859 dev_err(&dev->dev, "fail to add scsi host %d\n", err);
863 info->host_active = 1;
865 xenbus_switch_state(dev, XenbusStateInitialised);
870 scsifront_free_ring(info);
875 static int scsifront_resume(struct xenbus_device *dev)
877 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
878 struct Scsi_Host *host = info->host;
881 spin_lock_irq(host->host_lock);
883 /* Finish all still pending commands. */
884 scsifront_finish_all(info);
886 spin_unlock_irq(host->host_lock);
888 /* Reconnect to dom0. */
889 scsifront_free_ring(info);
890 err = scsifront_init_ring(info);
892 dev_err(&dev->dev, "fail to resume %d\n", err);
897 xenbus_switch_state(dev, XenbusStateInitialised);
902 static int scsifront_suspend(struct xenbus_device *dev)
904 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
905 struct Scsi_Host *host = info->host;
908 /* No new commands for the backend. */
909 spin_lock_irq(host->host_lock);
911 while (info->callers && !err) {
912 info->waiting_pause = 1;
913 info->wait_ring_available = 0;
914 spin_unlock_irq(host->host_lock);
915 wake_up(&info->wq_sync);
916 err = wait_event_interruptible(info->wq_pause,
917 !info->waiting_pause);
918 spin_lock_irq(host->host_lock);
920 spin_unlock_irq(host->host_lock);
924 static int scsifront_remove(struct xenbus_device *dev)
926 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
928 pr_debug("%s: %s removed\n", __func__, dev->nodename);
930 mutex_lock(&scsifront_mutex);
931 if (info->host_active) {
932 /* Scsi_host not yet removed */
933 scsi_remove_host(info->host);
934 info->host_active = 0;
936 mutex_unlock(&scsifront_mutex);
938 scsifront_free_ring(info);
939 scsi_host_put(info->host);
944 static void scsifront_disconnect(struct vscsifrnt_info *info)
946 struct xenbus_device *dev = info->dev;
947 struct Scsi_Host *host = info->host;
949 pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
952 * When this function is executed, all devices of
953 * Frontend have been deleted.
954 * Therefore, it need not block I/O before remove_host.
957 mutex_lock(&scsifront_mutex);
958 if (info->host_active) {
959 scsi_remove_host(host);
960 info->host_active = 0;
962 mutex_unlock(&scsifront_mutex);
964 xenbus_frontend_closed(dev);
967 static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
969 struct xenbus_device *dev = info->dev;
973 unsigned int dir_n = 0;
974 unsigned int device_state;
975 unsigned int hst, chn, tgt, lun;
976 struct scsi_device *sdev;
978 dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
982 /* mark current task as the one allowed to modify device states */
984 info->curr = current;
986 for (i = 0; i < dir_n; i++) {
988 snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
989 err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
991 if (XENBUS_EXIST_ERR(err))
994 /* virtual SCSI device */
995 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
996 err = xenbus_scanf(XBT_NIL, dev->otherend, str,
997 "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
998 if (XENBUS_EXIST_ERR(err))
1002 * Front device state path, used in slave_configure called
1003 * on successfull scsi_add_device, and in slave_destroy called
1004 * on remove of a device.
1006 snprintf(info->dev_state_path, sizeof(info->dev_state_path),
1007 "vscsi-devs/%s/state", dir[i]);
1010 case VSCSIFRONT_OP_ADD_LUN:
1011 if (device_state != XenbusStateInitialised)
1014 if (scsi_add_device(info->host, chn, tgt, lun)) {
1015 dev_err(&dev->dev, "scsi_add_device\n");
1016 err = xenbus_printf(XBT_NIL, dev->nodename,
1017 info->dev_state_path,
1018 "%d", XenbusStateClosed);
1020 xenbus_dev_error(dev, err,
1021 "%s: writing dev_state_path", __func__);
1024 case VSCSIFRONT_OP_DEL_LUN:
1025 if (device_state != XenbusStateClosing)
1028 sdev = scsi_device_lookup(info->host, chn, tgt, lun);
1030 scsi_remove_device(sdev);
1031 scsi_device_put(sdev);
1034 case VSCSIFRONT_OP_READD_LUN:
1035 if (device_state == XenbusStateConnected) {
1036 err = xenbus_printf(XBT_NIL, dev->nodename,
1037 info->dev_state_path,
1038 "%d", XenbusStateConnected);
1040 xenbus_dev_error(dev, err,
1041 "%s: writing dev_state_path", __func__);
1054 static void scsifront_read_backend_params(struct xenbus_device *dev,
1055 struct vscsifrnt_info *info)
1057 unsigned int sg_grant, nr_segs;
1058 struct Scsi_Host *host = info->host;
1060 sg_grant = xenbus_read_unsigned(dev->otherend, "feature-sg-grant", 0);
1061 nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
1062 nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
1063 nr_segs = min_t(unsigned int, nr_segs,
1064 VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
1065 sizeof(struct scsiif_request_segment));
1067 if (!info->pause && sg_grant)
1068 dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
1069 else if (info->pause && nr_segs < host->sg_tablesize)
1071 "SG entries decreased from %d to %u - device may not work properly anymore\n",
1072 host->sg_tablesize, nr_segs);
1074 host->sg_tablesize = nr_segs;
1075 host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
1078 static void scsifront_backend_changed(struct xenbus_device *dev,
1079 enum xenbus_state backend_state)
1081 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
1083 pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
1085 switch (backend_state) {
1086 case XenbusStateUnknown:
1087 case XenbusStateInitialising:
1088 case XenbusStateInitWait:
1089 case XenbusStateInitialised:
1092 case XenbusStateConnected:
1093 scsifront_read_backend_params(dev, info);
1096 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
1097 xenbus_switch_state(dev, XenbusStateConnected);
1102 if (xenbus_read_driver_state(dev->nodename) ==
1103 XenbusStateInitialised)
1104 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1106 if (dev->state != XenbusStateConnected)
1107 xenbus_switch_state(dev, XenbusStateConnected);
1110 case XenbusStateClosed:
1111 if (dev->state == XenbusStateClosed)
1113 fallthrough; /* Missed the backend's Closing state */
1114 case XenbusStateClosing:
1115 scsifront_disconnect(info);
1118 case XenbusStateReconfiguring:
1119 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
1120 xenbus_switch_state(dev, XenbusStateReconfiguring);
1123 case XenbusStateReconfigured:
1124 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1125 xenbus_switch_state(dev, XenbusStateConnected);
1130 static const struct xenbus_device_id scsifront_ids[] = {
1135 static struct xenbus_driver scsifront_driver = {
1136 .ids = scsifront_ids,
1137 .probe = scsifront_probe,
1138 .remove = scsifront_remove,
1139 .resume = scsifront_resume,
1140 .suspend = scsifront_suspend,
1141 .otherend_changed = scsifront_backend_changed,
1144 static int __init scsifront_init(void)
1149 return xenbus_register_frontend(&scsifront_driver);
1151 module_init(scsifront_init);
1153 static void __exit scsifront_exit(void)
1155 xenbus_unregister_driver(&scsifront_driver);
1157 module_exit(scsifront_exit);
1159 MODULE_DESCRIPTION("Xen SCSI frontend driver");
1160 MODULE_LICENSE("GPL");
1161 MODULE_ALIAS("xen:vscsi");
1162 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");