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 = scsi_cmd_to_rq(sc)->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_query_foreign_access(shadow->gref[i]))) {
237 shost_printk(KERN_ALERT, info->host, KBUILD_MODNAME
238 "grant still in use by backend\n");
241 gnttab_end_foreign_access(shadow->gref[i], 0, 0UL);
247 static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info,
248 struct vscsiif_response *ring_rsp)
250 struct vscsifrnt_shadow *shadow;
251 struct scsi_cmnd *sc;
257 shadow = info->shadow[id];
262 scsifront_gnttab_done(info, shadow);
263 scsifront_put_rqid(info, id);
265 result = ring_rsp->rslt;
267 set_host_byte(sc, DID_ERROR);
269 set_host_byte(sc, host_byte(result));
270 set_status_byte(sc, result & 0xff);
271 scsi_set_resid(sc, ring_rsp->residual_len);
273 sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE,
274 ring_rsp->sense_len);
277 memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len);
282 static void scsifront_sync_cmd_done(struct vscsifrnt_info *info,
283 struct vscsiif_response *ring_rsp)
285 uint16_t id = ring_rsp->rqid;
287 struct vscsifrnt_shadow *shadow = info->shadow[id];
290 spin_lock_irqsave(&info->shadow_lock, flags);
291 shadow->wait_reset = 1;
292 switch (shadow->rslt_reset) {
293 case RSLT_RESET_WAITING:
294 shadow->rslt_reset = ring_rsp->rslt;
297 kick = _scsifront_put_rqid(info, id);
298 spin_unlock_irqrestore(&info->shadow_lock, flags);
301 scsifront_wake_up(info);
304 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
305 "bad reset state %d, possibly leaking %u\n",
306 shadow->rslt_reset, id);
309 spin_unlock_irqrestore(&info->shadow_lock, flags);
311 wake_up(&shadow->wq_reset);
314 static void scsifront_do_response(struct vscsifrnt_info *info,
315 struct vscsiif_response *ring_rsp)
317 if (WARN(ring_rsp->rqid >= VSCSIIF_MAX_REQS ||
318 test_bit(ring_rsp->rqid, info->shadow_free_bitmap),
319 "illegal rqid %u returned by backend!\n", ring_rsp->rqid))
322 if (info->shadow[ring_rsp->rqid]->act == VSCSIIF_ACT_SCSI_CDB)
323 scsifront_cdb_cmd_done(info, ring_rsp);
325 scsifront_sync_cmd_done(info, ring_rsp);
328 static int scsifront_ring_drain(struct vscsifrnt_info *info)
330 struct vscsiif_response *ring_rsp;
334 rp = info->ring.sring->rsp_prod;
335 rmb(); /* ordering required respective to dom0 */
336 for (i = info->ring.rsp_cons; i != rp; i++) {
337 ring_rsp = RING_GET_RESPONSE(&info->ring, i);
338 scsifront_do_response(info, ring_rsp);
341 info->ring.rsp_cons = i;
343 if (i != info->ring.req_prod_pvt)
344 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
346 info->ring.sring->rsp_event = i + 1;
351 static int scsifront_cmd_done(struct vscsifrnt_info *info)
356 spin_lock_irqsave(info->host->host_lock, flags);
358 more_to_do = scsifront_ring_drain(info);
360 info->wait_ring_available = 0;
362 spin_unlock_irqrestore(info->host->host_lock, flags);
364 wake_up(&info->wq_sync);
369 static irqreturn_t scsifront_irq_fn(int irq, void *dev_id)
371 struct vscsifrnt_info *info = dev_id;
373 while (scsifront_cmd_done(info))
374 /* Yield point for this unbounded loop. */
380 static void scsifront_finish_all(struct vscsifrnt_info *info)
383 struct vscsiif_response resp;
385 scsifront_ring_drain(info);
387 for (i = 0; i < VSCSIIF_MAX_REQS; i++) {
388 if (test_bit(i, info->shadow_free_bitmap))
392 resp.rslt = DID_RESET << 16;
393 resp.residual_len = 0;
394 scsifront_do_response(info, &resp);
398 static int map_data_for_request(struct vscsifrnt_info *info,
399 struct scsi_cmnd *sc,
400 struct vscsifrnt_shadow *shadow)
402 grant_ref_t gref_head;
404 int err, ref, ref_cnt = 0;
405 int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE);
406 unsigned int i, off, len, bytes;
407 unsigned int data_len = scsi_bufflen(sc);
408 unsigned int data_grants = 0, seg_grants = 0;
409 struct scatterlist *sg;
410 struct scsiif_request_segment *seg;
412 if (sc->sc_data_direction == DMA_NONE || !data_len)
415 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i)
416 data_grants += PFN_UP(sg->offset + sg->length);
418 if (data_grants > VSCSIIF_SG_TABLESIZE) {
419 if (data_grants > info->host->sg_tablesize) {
420 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
421 "Unable to map request_buffer for command!\n");
424 seg_grants = vscsiif_grants_sg(data_grants);
425 shadow->sg = kcalloc(data_grants,
426 sizeof(struct scsiif_request_segment), GFP_ATOMIC);
430 seg = shadow->sg ? : shadow->seg;
432 err = gnttab_alloc_grant_references(seg_grants + data_grants,
436 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
437 "gnttab_alloc_grant_references() error\n");
442 page = virt_to_page(seg);
443 off = offset_in_page(seg);
444 len = sizeof(struct scsiif_request_segment) * data_grants;
446 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
448 ref = gnttab_claim_grant_reference(&gref_head);
449 BUG_ON(ref == -ENOSPC);
451 gnttab_grant_foreign_access_ref(ref,
452 info->dev->otherend_id,
453 xen_page_to_gfn(page), 1);
454 shadow->gref[ref_cnt] = ref;
455 shadow->seg[ref_cnt].gref = ref;
456 shadow->seg[ref_cnt].offset = (uint16_t)off;
457 shadow->seg[ref_cnt].length = (uint16_t)bytes;
464 BUG_ON(seg_grants < ref_cnt);
465 seg_grants = ref_cnt;
468 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) {
473 while (len > 0 && data_len > 0) {
475 * sg sends a scatterlist that is larger than
476 * the data_len it wants transferred for certain
479 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
480 bytes = min(bytes, data_len);
482 ref = gnttab_claim_grant_reference(&gref_head);
483 BUG_ON(ref == -ENOSPC);
485 gnttab_grant_foreign_access_ref(ref,
486 info->dev->otherend_id,
487 xen_page_to_gfn(page),
490 shadow->gref[ref_cnt] = ref;
492 seg->offset = (uint16_t)off;
493 seg->length = (uint16_t)bytes;
505 shadow->nr_segments = VSCSIIF_SG_GRANT | seg_grants;
507 shadow->nr_segments = (uint8_t)ref_cnt;
508 shadow->nr_grants = ref_cnt;
513 static int scsifront_enter(struct vscsifrnt_info *info)
521 static void scsifront_return(struct vscsifrnt_info *info)
527 if (!info->waiting_pause)
530 info->waiting_pause = 0;
531 wake_up(&info->wq_pause);
534 static int scsifront_queuecommand(struct Scsi_Host *shost,
535 struct scsi_cmnd *sc)
537 struct vscsifrnt_info *info = shost_priv(shost);
538 struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc);
545 shadow->act = VSCSIIF_ACT_SCSI_CDB;
547 spin_lock_irqsave(shost->host_lock, flags);
548 if (scsifront_enter(info)) {
549 spin_unlock_irqrestore(shost->host_lock, flags);
550 return SCSI_MLQUEUE_HOST_BUSY;
553 err = map_data_for_request(info, sc, shadow);
555 pr_debug("%s: err %d\n", __func__, err);
556 scsifront_return(info);
557 spin_unlock_irqrestore(shost->host_lock, flags);
559 return SCSI_MLQUEUE_HOST_BUSY;
560 sc->result = DID_ERROR << 16;
565 if (scsifront_do_request(info, shadow)) {
566 scsifront_gnttab_done(info, shadow);
570 scsifront_return(info);
571 spin_unlock_irqrestore(shost->host_lock, flags);
576 scsifront_return(info);
577 spin_unlock_irqrestore(shost->host_lock, flags);
578 pr_debug("%s: busy\n", __func__);
579 return SCSI_MLQUEUE_HOST_BUSY;
583 * Any exception handling (reset or abort) must be forwarded to the backend.
584 * We have to wait until an answer is returned. This answer contains the
585 * result to be returned to the requestor.
587 static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)
589 struct Scsi_Host *host = sc->device->host;
590 struct vscsifrnt_info *info = shost_priv(host);
591 struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc);
594 shadow = kzalloc(sizeof(*shadow), GFP_NOIO);
599 shadow->rslt_reset = RSLT_RESET_WAITING;
601 shadow->ref_rqid = s->rqid;
602 init_waitqueue_head(&shadow->wq_reset);
604 spin_lock_irq(host->host_lock);
607 if (scsifront_enter(info))
610 if (!scsifront_do_request(info, shadow))
613 scsifront_return(info);
616 info->wait_ring_available = 1;
617 spin_unlock_irq(host->host_lock);
618 err = wait_event_interruptible(info->wq_sync,
619 !info->wait_ring_available);
620 spin_lock_irq(host->host_lock);
623 spin_unlock_irq(host->host_lock);
624 err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset);
625 spin_lock_irq(host->host_lock);
628 err = shadow->rslt_reset;
629 scsifront_put_rqid(info, shadow->rqid);
632 spin_lock(&info->shadow_lock);
633 shadow->rslt_reset = RSLT_RESET_ERR;
634 spin_unlock(&info->shadow_lock);
638 scsifront_return(info);
639 spin_unlock_irq(host->host_lock);
643 spin_unlock_irq(host->host_lock);
648 static int scsifront_eh_abort_handler(struct scsi_cmnd *sc)
650 pr_debug("%s\n", __func__);
651 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT);
654 static int scsifront_dev_reset_handler(struct scsi_cmnd *sc)
656 pr_debug("%s\n", __func__);
657 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET);
660 static int scsifront_sdev_configure(struct scsi_device *sdev)
662 struct vscsifrnt_info *info = shost_priv(sdev->host);
665 if (info && current == info->curr) {
666 err = xenbus_printf(XBT_NIL, info->dev->nodename,
667 info->dev_state_path, "%d", XenbusStateConnected);
669 xenbus_dev_error(info->dev, err,
670 "%s: writing dev_state_path", __func__);
678 static void scsifront_sdev_destroy(struct scsi_device *sdev)
680 struct vscsifrnt_info *info = shost_priv(sdev->host);
683 if (info && current == info->curr) {
684 err = xenbus_printf(XBT_NIL, info->dev->nodename,
685 info->dev_state_path, "%d", XenbusStateClosed);
687 xenbus_dev_error(info->dev, err,
688 "%s: writing dev_state_path", __func__);
692 static struct scsi_host_template scsifront_sht = {
693 .module = THIS_MODULE,
694 .name = "Xen SCSI frontend driver",
695 .queuecommand = scsifront_queuecommand,
696 .eh_abort_handler = scsifront_eh_abort_handler,
697 .eh_device_reset_handler = scsifront_dev_reset_handler,
698 .slave_configure = scsifront_sdev_configure,
699 .slave_destroy = scsifront_sdev_destroy,
700 .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN,
701 .can_queue = VSCSIIF_MAX_REQS,
703 .cmd_size = sizeof(struct vscsifrnt_shadow),
704 .sg_tablesize = VSCSIIF_SG_TABLESIZE,
705 .proc_name = "scsifront",
708 static int scsifront_alloc_ring(struct vscsifrnt_info *info)
710 struct xenbus_device *dev = info->dev;
711 struct vscsiif_sring *sring;
715 /***** Frontend to Backend ring start *****/
716 sring = (struct vscsiif_sring *)__get_free_page(GFP_KERNEL);
718 xenbus_dev_fatal(dev, err,
719 "fail to allocate shared ring (Front to Back)");
722 SHARED_RING_INIT(sring);
723 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
725 err = xenbus_grant_ring(dev, sring, 1, &gref);
727 free_page((unsigned long)sring);
728 xenbus_dev_fatal(dev, err,
729 "fail to grant shared ring (Front to Back)");
732 info->ring_ref = gref;
734 err = xenbus_alloc_evtchn(dev, &info->evtchn);
736 xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
740 err = bind_evtchn_to_irq(info->evtchn);
742 xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
748 err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn,
749 IRQF_ONESHOT, "scsifront", info);
751 xenbus_dev_fatal(dev, err, "request_threaded_irq");
759 unbind_from_irqhandler(info->irq, info);
761 gnttab_end_foreign_access(info->ring_ref, 0,
762 (unsigned long)info->ring.sring);
767 static void scsifront_free_ring(struct vscsifrnt_info *info)
769 unbind_from_irqhandler(info->irq, info);
770 gnttab_end_foreign_access(info->ring_ref, 0,
771 (unsigned long)info->ring.sring);
774 static int scsifront_init_ring(struct vscsifrnt_info *info)
776 struct xenbus_device *dev = info->dev;
777 struct xenbus_transaction xbt;
780 pr_debug("%s\n", __func__);
782 err = scsifront_alloc_ring(info);
785 pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn);
788 err = xenbus_transaction_start(&xbt);
790 xenbus_dev_fatal(dev, err, "starting transaction");
792 err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u",
795 xenbus_dev_fatal(dev, err, "%s", "writing ring-ref");
799 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
803 xenbus_dev_fatal(dev, err, "%s", "writing event-channel");
807 err = xenbus_transaction_end(xbt, 0);
811 xenbus_dev_fatal(dev, err, "completing transaction");
818 xenbus_transaction_end(xbt, 1);
820 scsifront_free_ring(info);
826 static int scsifront_probe(struct xenbus_device *dev,
827 const struct xenbus_device_id *id)
829 struct vscsifrnt_info *info;
830 struct Scsi_Host *host;
832 char name[TASK_COMM_LEN];
834 host = scsi_host_alloc(&scsifront_sht, sizeof(*info));
836 xenbus_dev_fatal(dev, err, "fail to allocate scsi host");
839 info = (struct vscsifrnt_info *)host->hostdata;
841 dev_set_drvdata(&dev->dev, info);
844 bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
846 err = scsifront_init_ring(info);
852 init_waitqueue_head(&info->wq_sync);
853 init_waitqueue_head(&info->wq_pause);
854 spin_lock_init(&info->shadow_lock);
856 snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no);
858 host->max_id = VSCSIIF_MAX_TARGET;
859 host->max_channel = 0;
860 host->max_lun = VSCSIIF_MAX_LUN;
861 host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512;
862 host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE;
864 err = scsi_add_host(host, &dev->dev);
866 dev_err(&dev->dev, "fail to add scsi host %d\n", err);
870 info->host_active = 1;
872 xenbus_switch_state(dev, XenbusStateInitialised);
877 scsifront_free_ring(info);
882 static int scsifront_resume(struct xenbus_device *dev)
884 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
885 struct Scsi_Host *host = info->host;
888 spin_lock_irq(host->host_lock);
890 /* Finish all still pending commands. */
891 scsifront_finish_all(info);
893 spin_unlock_irq(host->host_lock);
895 /* Reconnect to dom0. */
896 scsifront_free_ring(info);
897 err = scsifront_init_ring(info);
899 dev_err(&dev->dev, "fail to resume %d\n", err);
904 xenbus_switch_state(dev, XenbusStateInitialised);
909 static int scsifront_suspend(struct xenbus_device *dev)
911 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
912 struct Scsi_Host *host = info->host;
915 /* No new commands for the backend. */
916 spin_lock_irq(host->host_lock);
918 while (info->callers && !err) {
919 info->waiting_pause = 1;
920 info->wait_ring_available = 0;
921 spin_unlock_irq(host->host_lock);
922 wake_up(&info->wq_sync);
923 err = wait_event_interruptible(info->wq_pause,
924 !info->waiting_pause);
925 spin_lock_irq(host->host_lock);
927 spin_unlock_irq(host->host_lock);
931 static int scsifront_remove(struct xenbus_device *dev)
933 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
935 pr_debug("%s: %s removed\n", __func__, dev->nodename);
937 mutex_lock(&scsifront_mutex);
938 if (info->host_active) {
939 /* Scsi_host not yet removed */
940 scsi_remove_host(info->host);
941 info->host_active = 0;
943 mutex_unlock(&scsifront_mutex);
945 scsifront_free_ring(info);
946 scsi_host_put(info->host);
951 static void scsifront_disconnect(struct vscsifrnt_info *info)
953 struct xenbus_device *dev = info->dev;
954 struct Scsi_Host *host = info->host;
956 pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
959 * When this function is executed, all devices of
960 * Frontend have been deleted.
961 * Therefore, it need not block I/O before remove_host.
964 mutex_lock(&scsifront_mutex);
965 if (info->host_active) {
966 scsi_remove_host(host);
967 info->host_active = 0;
969 mutex_unlock(&scsifront_mutex);
971 xenbus_frontend_closed(dev);
974 static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
976 struct xenbus_device *dev = info->dev;
980 unsigned int dir_n = 0;
981 unsigned int device_state;
982 unsigned int hst, chn, tgt, lun;
983 struct scsi_device *sdev;
985 dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
989 /* mark current task as the one allowed to modify device states */
991 info->curr = current;
993 for (i = 0; i < dir_n; i++) {
995 snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
996 err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
998 if (XENBUS_EXIST_ERR(err))
1001 /* virtual SCSI device */
1002 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
1003 err = xenbus_scanf(XBT_NIL, dev->otherend, str,
1004 "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
1005 if (XENBUS_EXIST_ERR(err))
1009 * Front device state path, used in slave_configure called
1010 * on successfull scsi_add_device, and in slave_destroy called
1011 * on remove of a device.
1013 snprintf(info->dev_state_path, sizeof(info->dev_state_path),
1014 "vscsi-devs/%s/state", dir[i]);
1017 case VSCSIFRONT_OP_ADD_LUN:
1018 if (device_state != XenbusStateInitialised)
1021 if (scsi_add_device(info->host, chn, tgt, lun)) {
1022 dev_err(&dev->dev, "scsi_add_device\n");
1023 err = xenbus_printf(XBT_NIL, dev->nodename,
1024 info->dev_state_path,
1025 "%d", XenbusStateClosed);
1027 xenbus_dev_error(dev, err,
1028 "%s: writing dev_state_path", __func__);
1031 case VSCSIFRONT_OP_DEL_LUN:
1032 if (device_state != XenbusStateClosing)
1035 sdev = scsi_device_lookup(info->host, chn, tgt, lun);
1037 scsi_remove_device(sdev);
1038 scsi_device_put(sdev);
1041 case VSCSIFRONT_OP_READD_LUN:
1042 if (device_state == XenbusStateConnected) {
1043 err = xenbus_printf(XBT_NIL, dev->nodename,
1044 info->dev_state_path,
1045 "%d", XenbusStateConnected);
1047 xenbus_dev_error(dev, err,
1048 "%s: writing dev_state_path", __func__);
1061 static void scsifront_read_backend_params(struct xenbus_device *dev,
1062 struct vscsifrnt_info *info)
1064 unsigned int sg_grant, nr_segs;
1065 struct Scsi_Host *host = info->host;
1067 sg_grant = xenbus_read_unsigned(dev->otherend, "feature-sg-grant", 0);
1068 nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
1069 nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
1070 nr_segs = min_t(unsigned int, nr_segs,
1071 VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
1072 sizeof(struct scsiif_request_segment));
1074 if (!info->pause && sg_grant)
1075 dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
1076 else if (info->pause && nr_segs < host->sg_tablesize)
1078 "SG entries decreased from %d to %u - device may not work properly anymore\n",
1079 host->sg_tablesize, nr_segs);
1081 host->sg_tablesize = nr_segs;
1082 host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
1085 static void scsifront_backend_changed(struct xenbus_device *dev,
1086 enum xenbus_state backend_state)
1088 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
1090 pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
1092 switch (backend_state) {
1093 case XenbusStateUnknown:
1094 case XenbusStateInitialising:
1095 case XenbusStateInitWait:
1096 case XenbusStateInitialised:
1099 case XenbusStateConnected:
1100 scsifront_read_backend_params(dev, info);
1103 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
1104 xenbus_switch_state(dev, XenbusStateConnected);
1109 if (xenbus_read_driver_state(dev->nodename) ==
1110 XenbusStateInitialised)
1111 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1113 if (dev->state != XenbusStateConnected)
1114 xenbus_switch_state(dev, XenbusStateConnected);
1117 case XenbusStateClosed:
1118 if (dev->state == XenbusStateClosed)
1120 fallthrough; /* Missed the backend's Closing state */
1121 case XenbusStateClosing:
1122 scsifront_disconnect(info);
1125 case XenbusStateReconfiguring:
1126 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
1127 xenbus_switch_state(dev, XenbusStateReconfiguring);
1130 case XenbusStateReconfigured:
1131 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1132 xenbus_switch_state(dev, XenbusStateConnected);
1137 static const struct xenbus_device_id scsifront_ids[] = {
1142 static struct xenbus_driver scsifront_driver = {
1143 .ids = scsifront_ids,
1144 .probe = scsifront_probe,
1145 .remove = scsifront_remove,
1146 .resume = scsifront_resume,
1147 .suspend = scsifront_suspend,
1148 .otherend_changed = scsifront_backend_changed,
1151 static int __init scsifront_init(void)
1156 return xenbus_register_frontend(&scsifront_driver);
1158 module_init(scsifront_init);
1160 static void __exit scsifront_exit(void)
1162 xenbus_unregister_driver(&scsifront_driver);
1164 module_exit(scsifront_exit);
1166 MODULE_DESCRIPTION("Xen SCSI frontend driver");
1167 MODULE_LICENSE("GPL");
1168 MODULE_ALIAS("xen:vscsi");