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
68 #define VSCSIIF_DEFAULT_CMD_PER_LUN 10
69 #define VSCSIIF_MAX_TARGET 64
70 #define VSCSIIF_MAX_LUN 255
72 #define VSCSIIF_RING_SIZE __CONST_RING_SIZE(vscsiif, PAGE_SIZE)
73 #define VSCSIIF_MAX_REQS VSCSIIF_RING_SIZE
75 #define vscsiif_grants_sg(_sg) (PFN_UP((_sg) * \
76 sizeof(struct scsiif_request_segment)))
78 struct vscsifrnt_shadow
{
79 /* command between backend and frontend */
83 unsigned int nr_grants
; /* number of grants in gref[] */
84 struct scsiif_request_segment
*sg
; /* scatter/gather elements */
86 /* Do reset or abort function. */
87 wait_queue_head_t wq_reset
; /* reset work queue */
88 int wait_reset
; /* reset work queue condition */
89 int32_t rslt_reset
; /* reset response status: */
90 /* SUCCESS or FAILED or: */
91 #define RSLT_RESET_WAITING 0
92 #define RSLT_RESET_ERR -1
94 /* Requested struct scsi_cmnd is stored from kernel. */
96 int gref
[vscsiif_grants_sg(SG_ALL
) + SG_ALL
];
99 struct vscsifrnt_info
{
100 struct xenbus_device
*dev
;
102 struct Scsi_Host
*host
;
108 grant_ref_t ring_ref
;
109 struct vscsiif_front_ring ring
;
110 struct vscsiif_response ring_rsp
;
112 spinlock_t shadow_lock
;
113 DECLARE_BITMAP(shadow_free_bitmap
, VSCSIIF_MAX_REQS
);
114 struct vscsifrnt_shadow
*shadow
[VSCSIIF_MAX_REQS
];
116 wait_queue_head_t wq_sync
;
117 unsigned int wait_ring_available
:1;
119 char dev_state_path
[64];
120 struct task_struct
*curr
;
123 static DEFINE_MUTEX(scsifront_mutex
);
125 static void scsifront_wake_up(struct vscsifrnt_info
*info
)
127 info
->wait_ring_available
= 0;
128 wake_up(&info
->wq_sync
);
131 static int scsifront_get_rqid(struct vscsifrnt_info
*info
)
136 spin_lock_irqsave(&info
->shadow_lock
, flags
);
138 free
= find_first_bit(info
->shadow_free_bitmap
, VSCSIIF_MAX_REQS
);
139 __clear_bit(free
, info
->shadow_free_bitmap
);
141 spin_unlock_irqrestore(&info
->shadow_lock
, flags
);
146 static int _scsifront_put_rqid(struct vscsifrnt_info
*info
, uint32_t id
)
148 int empty
= bitmap_empty(info
->shadow_free_bitmap
, VSCSIIF_MAX_REQS
);
150 __set_bit(id
, info
->shadow_free_bitmap
);
151 info
->shadow
[id
] = NULL
;
153 return empty
|| info
->wait_ring_available
;
156 static void scsifront_put_rqid(struct vscsifrnt_info
*info
, uint32_t id
)
161 spin_lock_irqsave(&info
->shadow_lock
, flags
);
162 kick
= _scsifront_put_rqid(info
, id
);
163 spin_unlock_irqrestore(&info
->shadow_lock
, flags
);
166 scsifront_wake_up(info
);
169 static struct vscsiif_request
*scsifront_pre_req(struct vscsifrnt_info
*info
)
171 struct vscsiif_front_ring
*ring
= &(info
->ring
);
172 struct vscsiif_request
*ring_req
;
175 id
= scsifront_get_rqid(info
); /* use id in response */
176 if (id
>= VSCSIIF_MAX_REQS
)
179 ring_req
= RING_GET_REQUEST(&(info
->ring
), ring
->req_prod_pvt
);
181 ring
->req_prod_pvt
++;
183 ring_req
->rqid
= (uint16_t)id
;
188 static void scsifront_do_request(struct vscsifrnt_info
*info
)
190 struct vscsiif_front_ring
*ring
= &(info
->ring
);
193 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(ring
, notify
);
195 notify_remote_via_irq(info
->irq
);
198 static void scsifront_gnttab_done(struct vscsifrnt_info
*info
, uint32_t id
)
200 struct vscsifrnt_shadow
*s
= info
->shadow
[id
];
203 if (s
->sc
->sc_data_direction
== DMA_NONE
)
206 for (i
= 0; i
< s
->nr_grants
; i
++) {
207 if (unlikely(gnttab_query_foreign_access(s
->gref
[i
]) != 0)) {
208 shost_printk(KERN_ALERT
, info
->host
, KBUILD_MODNAME
209 "grant still in use by backend\n");
212 gnttab_end_foreign_access(s
->gref
[i
], 0, 0UL);
218 static void scsifront_cdb_cmd_done(struct vscsifrnt_info
*info
,
219 struct vscsiif_response
*ring_rsp
)
221 struct scsi_cmnd
*sc
;
226 sc
= info
->shadow
[id
]->sc
;
230 scsifront_gnttab_done(info
, id
);
231 scsifront_put_rqid(info
, id
);
233 sc
->result
= ring_rsp
->rslt
;
234 scsi_set_resid(sc
, ring_rsp
->residual_len
);
236 sense_len
= min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE
,
237 ring_rsp
->sense_len
);
240 memcpy(sc
->sense_buffer
, ring_rsp
->sense_buffer
, sense_len
);
245 static void scsifront_sync_cmd_done(struct vscsifrnt_info
*info
,
246 struct vscsiif_response
*ring_rsp
)
248 uint16_t id
= ring_rsp
->rqid
;
250 struct vscsifrnt_shadow
*shadow
= info
->shadow
[id
];
253 spin_lock_irqsave(&info
->shadow_lock
, flags
);
254 shadow
->wait_reset
= 1;
255 switch (shadow
->rslt_reset
) {
256 case RSLT_RESET_WAITING
:
257 shadow
->rslt_reset
= ring_rsp
->rslt
;
260 kick
= _scsifront_put_rqid(info
, id
);
261 spin_unlock_irqrestore(&info
->shadow_lock
, flags
);
264 scsifront_wake_up(info
);
267 shost_printk(KERN_ERR
, info
->host
, KBUILD_MODNAME
268 "bad reset state %d, possibly leaking %u\n",
269 shadow
->rslt_reset
, id
);
272 spin_unlock_irqrestore(&info
->shadow_lock
, flags
);
274 wake_up(&shadow
->wq_reset
);
277 static int scsifront_cmd_done(struct vscsifrnt_info
*info
)
279 struct vscsiif_response
*ring_rsp
;
284 spin_lock_irqsave(info
->host
->host_lock
, flags
);
286 rp
= info
->ring
.sring
->rsp_prod
;
287 rmb(); /* ordering required respective to dom0 */
288 for (i
= info
->ring
.rsp_cons
; i
!= rp
; i
++) {
290 ring_rsp
= RING_GET_RESPONSE(&info
->ring
, i
);
292 if (WARN(ring_rsp
->rqid
>= VSCSIIF_MAX_REQS
||
293 test_bit(ring_rsp
->rqid
, info
->shadow_free_bitmap
),
294 "illegal rqid %u returned by backend!\n",
298 if (info
->shadow
[ring_rsp
->rqid
]->act
== VSCSIIF_ACT_SCSI_CDB
)
299 scsifront_cdb_cmd_done(info
, ring_rsp
);
301 scsifront_sync_cmd_done(info
, ring_rsp
);
304 info
->ring
.rsp_cons
= i
;
306 if (i
!= info
->ring
.req_prod_pvt
)
307 RING_FINAL_CHECK_FOR_RESPONSES(&info
->ring
, more_to_do
);
309 info
->ring
.sring
->rsp_event
= i
+ 1;
311 info
->wait_ring_available
= 0;
313 spin_unlock_irqrestore(info
->host
->host_lock
, flags
);
315 wake_up(&info
->wq_sync
);
320 static irqreturn_t
scsifront_irq_fn(int irq
, void *dev_id
)
322 struct vscsifrnt_info
*info
= dev_id
;
324 while (scsifront_cmd_done(info
))
325 /* Yield point for this unbounded loop. */
331 static int map_data_for_request(struct vscsifrnt_info
*info
,
332 struct scsi_cmnd
*sc
,
333 struct vscsiif_request
*ring_req
,
334 struct vscsifrnt_shadow
*shadow
)
336 grant_ref_t gref_head
;
338 int err
, ref
, ref_cnt
= 0;
339 int grant_ro
= (sc
->sc_data_direction
== DMA_TO_DEVICE
);
340 unsigned int i
, off
, len
, bytes
;
341 unsigned int data_len
= scsi_bufflen(sc
);
342 unsigned int data_grants
= 0, seg_grants
= 0;
343 struct scatterlist
*sg
;
345 struct scsiif_request_segment
*seg
;
347 ring_req
->nr_segments
= 0;
348 if (sc
->sc_data_direction
== DMA_NONE
|| !data_len
)
351 scsi_for_each_sg(sc
, sg
, scsi_sg_count(sc
), i
)
352 data_grants
+= PFN_UP(sg
->offset
+ sg
->length
);
354 if (data_grants
> VSCSIIF_SG_TABLESIZE
) {
355 if (data_grants
> info
->host
->sg_tablesize
) {
356 shost_printk(KERN_ERR
, info
->host
, KBUILD_MODNAME
357 "Unable to map request_buffer for command!\n");
360 seg_grants
= vscsiif_grants_sg(data_grants
);
361 shadow
->sg
= kcalloc(data_grants
,
362 sizeof(struct scsiif_request_segment
), GFP_ATOMIC
);
366 seg
= shadow
->sg
? : ring_req
->seg
;
368 err
= gnttab_alloc_grant_references(seg_grants
+ data_grants
,
372 shost_printk(KERN_ERR
, info
->host
, KBUILD_MODNAME
373 "gnttab_alloc_grant_references() error\n");
378 page
= virt_to_page(seg
);
379 off
= (unsigned long)seg
& ~PAGE_MASK
;
380 len
= sizeof(struct scsiif_request_segment
) * data_grants
;
382 bytes
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
384 ref
= gnttab_claim_grant_reference(&gref_head
);
385 BUG_ON(ref
== -ENOSPC
);
387 mfn
= pfn_to_mfn(page_to_pfn(page
));
388 gnttab_grant_foreign_access_ref(ref
,
389 info
->dev
->otherend_id
, mfn
, 1);
390 shadow
->gref
[ref_cnt
] = ref
;
391 ring_req
->seg
[ref_cnt
].gref
= ref
;
392 ring_req
->seg
[ref_cnt
].offset
= (uint16_t)off
;
393 ring_req
->seg
[ref_cnt
].length
= (uint16_t)bytes
;
400 BUG_ON(seg_grants
< ref_cnt
);
401 seg_grants
= ref_cnt
;
404 scsi_for_each_sg(sc
, sg
, scsi_sg_count(sc
), i
) {
409 while (len
> 0 && data_len
> 0) {
411 * sg sends a scatterlist that is larger than
412 * the data_len it wants transferred for certain
415 bytes
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
416 bytes
= min(bytes
, data_len
);
418 ref
= gnttab_claim_grant_reference(&gref_head
);
419 BUG_ON(ref
== -ENOSPC
);
421 mfn
= pfn_to_mfn(page_to_pfn(page
));
422 gnttab_grant_foreign_access_ref(ref
,
423 info
->dev
->otherend_id
, mfn
, grant_ro
);
425 shadow
->gref
[ref_cnt
] = ref
;
427 seg
->offset
= (uint16_t)off
;
428 seg
->length
= (uint16_t)bytes
;
440 ring_req
->nr_segments
= VSCSIIF_SG_GRANT
| seg_grants
;
442 ring_req
->nr_segments
= (uint8_t)ref_cnt
;
443 shadow
->nr_grants
= ref_cnt
;
448 static struct vscsiif_request
*scsifront_command2ring(
449 struct vscsifrnt_info
*info
, struct scsi_cmnd
*sc
,
450 struct vscsifrnt_shadow
*shadow
)
452 struct vscsiif_request
*ring_req
;
454 memset(shadow
, 0, sizeof(*shadow
));
456 ring_req
= scsifront_pre_req(info
);
460 info
->shadow
[ring_req
->rqid
] = shadow
;
461 shadow
->rqid
= ring_req
->rqid
;
463 ring_req
->id
= sc
->device
->id
;
464 ring_req
->lun
= sc
->device
->lun
;
465 ring_req
->channel
= sc
->device
->channel
;
466 ring_req
->cmd_len
= sc
->cmd_len
;
468 BUG_ON(sc
->cmd_len
> VSCSIIF_MAX_COMMAND_SIZE
);
470 memcpy(ring_req
->cmnd
, sc
->cmnd
, sc
->cmd_len
);
472 ring_req
->sc_data_direction
= (uint8_t)sc
->sc_data_direction
;
473 ring_req
->timeout_per_command
= sc
->request
->timeout
/ HZ
;
478 static int scsifront_queuecommand(struct Scsi_Host
*shost
,
479 struct scsi_cmnd
*sc
)
481 struct vscsifrnt_info
*info
= shost_priv(shost
);
482 struct vscsiif_request
*ring_req
;
483 struct vscsifrnt_shadow
*shadow
= scsi_cmd_priv(sc
);
488 spin_lock_irqsave(shost
->host_lock
, flags
);
489 if (RING_FULL(&info
->ring
))
492 ring_req
= scsifront_command2ring(info
, sc
, shadow
);
498 rqid
= ring_req
->rqid
;
499 ring_req
->act
= VSCSIIF_ACT_SCSI_CDB
;
502 shadow
->act
= VSCSIIF_ACT_SCSI_CDB
;
504 err
= map_data_for_request(info
, sc
, ring_req
, shadow
);
506 pr_debug("%s: err %d\n", __func__
, err
);
507 scsifront_put_rqid(info
, rqid
);
508 spin_unlock_irqrestore(shost
->host_lock
, flags
);
510 return SCSI_MLQUEUE_HOST_BUSY
;
511 sc
->result
= DID_ERROR
<< 16;
516 scsifront_do_request(info
);
517 spin_unlock_irqrestore(shost
->host_lock
, flags
);
522 spin_unlock_irqrestore(shost
->host_lock
, flags
);
523 pr_debug("%s: busy\n", __func__
);
524 return SCSI_MLQUEUE_HOST_BUSY
;
528 * Any exception handling (reset or abort) must be forwarded to the backend.
529 * We have to wait until an answer is returned. This answer contains the
530 * result to be returned to the requestor.
532 static int scsifront_action_handler(struct scsi_cmnd
*sc
, uint8_t act
)
534 struct Scsi_Host
*host
= sc
->device
->host
;
535 struct vscsifrnt_info
*info
= shost_priv(host
);
536 struct vscsifrnt_shadow
*shadow
, *s
= scsi_cmd_priv(sc
);
537 struct vscsiif_request
*ring_req
;
540 shadow
= kmalloc(sizeof(*shadow
), GFP_NOIO
);
544 spin_lock_irq(host
->host_lock
);
547 if (!RING_FULL(&info
->ring
)) {
548 ring_req
= scsifront_command2ring(info
, sc
, shadow
);
553 spin_unlock_irq(host
->host_lock
);
557 info
->wait_ring_available
= 1;
558 spin_unlock_irq(host
->host_lock
);
559 err
= wait_event_interruptible(info
->wq_sync
,
560 !info
->wait_ring_available
);
561 spin_lock_irq(host
->host_lock
);
565 ring_req
->ref_rqid
= s
->rqid
;
568 shadow
->rslt_reset
= RSLT_RESET_WAITING
;
569 init_waitqueue_head(&shadow
->wq_reset
);
571 ring_req
->nr_segments
= 0;
573 scsifront_do_request(info
);
575 spin_unlock_irq(host
->host_lock
);
576 err
= wait_event_interruptible(shadow
->wq_reset
, shadow
->wait_reset
);
577 spin_lock_irq(host
->host_lock
);
580 err
= shadow
->rslt_reset
;
581 scsifront_put_rqid(info
, shadow
->rqid
);
584 spin_lock(&info
->shadow_lock
);
585 shadow
->rslt_reset
= RSLT_RESET_ERR
;
586 spin_unlock(&info
->shadow_lock
);
590 spin_unlock_irq(host
->host_lock
);
594 static int scsifront_eh_abort_handler(struct scsi_cmnd
*sc
)
596 pr_debug("%s\n", __func__
);
597 return scsifront_action_handler(sc
, VSCSIIF_ACT_SCSI_ABORT
);
600 static int scsifront_dev_reset_handler(struct scsi_cmnd
*sc
)
602 pr_debug("%s\n", __func__
);
603 return scsifront_action_handler(sc
, VSCSIIF_ACT_SCSI_RESET
);
606 static int scsifront_sdev_configure(struct scsi_device
*sdev
)
608 struct vscsifrnt_info
*info
= shost_priv(sdev
->host
);
610 if (info
&& current
== info
->curr
)
611 xenbus_printf(XBT_NIL
, info
->dev
->nodename
,
612 info
->dev_state_path
, "%d", XenbusStateConnected
);
617 static void scsifront_sdev_destroy(struct scsi_device
*sdev
)
619 struct vscsifrnt_info
*info
= shost_priv(sdev
->host
);
621 if (info
&& current
== info
->curr
)
622 xenbus_printf(XBT_NIL
, info
->dev
->nodename
,
623 info
->dev_state_path
, "%d", XenbusStateClosed
);
626 static struct scsi_host_template scsifront_sht
= {
627 .module
= THIS_MODULE
,
628 .name
= "Xen SCSI frontend driver",
629 .queuecommand
= scsifront_queuecommand
,
630 .eh_abort_handler
= scsifront_eh_abort_handler
,
631 .eh_device_reset_handler
= scsifront_dev_reset_handler
,
632 .slave_configure
= scsifront_sdev_configure
,
633 .slave_destroy
= scsifront_sdev_destroy
,
634 .cmd_per_lun
= VSCSIIF_DEFAULT_CMD_PER_LUN
,
635 .can_queue
= VSCSIIF_MAX_REQS
,
637 .cmd_size
= sizeof(struct vscsifrnt_shadow
),
638 .sg_tablesize
= VSCSIIF_SG_TABLESIZE
,
639 .use_clustering
= DISABLE_CLUSTERING
,
640 .proc_name
= "scsifront",
643 static int scsifront_alloc_ring(struct vscsifrnt_info
*info
)
645 struct xenbus_device
*dev
= info
->dev
;
646 struct vscsiif_sring
*sring
;
649 /***** Frontend to Backend ring start *****/
650 sring
= (struct vscsiif_sring
*)__get_free_page(GFP_KERNEL
);
652 xenbus_dev_fatal(dev
, err
,
653 "fail to allocate shared ring (Front to Back)");
656 SHARED_RING_INIT(sring
);
657 FRONT_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
659 err
= xenbus_grant_ring(dev
, virt_to_mfn(sring
));
661 free_page((unsigned long)sring
);
662 xenbus_dev_fatal(dev
, err
,
663 "fail to grant shared ring (Front to Back)");
666 info
->ring_ref
= err
;
668 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
670 xenbus_dev_fatal(dev
, err
, "xenbus_alloc_evtchn");
674 err
= bind_evtchn_to_irq(info
->evtchn
);
676 xenbus_dev_fatal(dev
, err
, "bind_evtchn_to_irq");
682 err
= request_threaded_irq(info
->irq
, NULL
, scsifront_irq_fn
,
683 IRQF_ONESHOT
, "scsifront", info
);
685 xenbus_dev_fatal(dev
, err
, "request_threaded_irq");
693 unbind_from_irqhandler(info
->irq
, info
);
695 gnttab_end_foreign_access(info
->ring_ref
, 0,
696 (unsigned long)info
->ring
.sring
);
701 static int scsifront_init_ring(struct vscsifrnt_info
*info
)
703 struct xenbus_device
*dev
= info
->dev
;
704 struct xenbus_transaction xbt
;
707 pr_debug("%s\n", __func__
);
709 err
= scsifront_alloc_ring(info
);
712 pr_debug("%s: %u %u\n", __func__
, info
->ring_ref
, info
->evtchn
);
715 err
= xenbus_transaction_start(&xbt
);
717 xenbus_dev_fatal(dev
, err
, "starting transaction");
719 err
= xenbus_printf(xbt
, dev
->nodename
, "ring-ref", "%u",
722 xenbus_dev_fatal(dev
, err
, "%s", "writing ring-ref");
726 err
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
730 xenbus_dev_fatal(dev
, err
, "%s", "writing event-channel");
734 err
= xenbus_transaction_end(xbt
, 0);
738 xenbus_dev_fatal(dev
, err
, "completing transaction");
745 xenbus_transaction_end(xbt
, 1);
747 unbind_from_irqhandler(info
->irq
, info
);
748 gnttab_end_foreign_access(info
->ring_ref
, 0,
749 (unsigned long)info
->ring
.sring
);
755 static int scsifront_probe(struct xenbus_device
*dev
,
756 const struct xenbus_device_id
*id
)
758 struct vscsifrnt_info
*info
;
759 struct Scsi_Host
*host
;
761 char name
[TASK_COMM_LEN
];
763 host
= scsi_host_alloc(&scsifront_sht
, sizeof(*info
));
765 xenbus_dev_fatal(dev
, err
, "fail to allocate scsi host");
768 info
= (struct vscsifrnt_info
*)host
->hostdata
;
770 dev_set_drvdata(&dev
->dev
, info
);
773 bitmap_fill(info
->shadow_free_bitmap
, VSCSIIF_MAX_REQS
);
775 err
= scsifront_init_ring(info
);
781 init_waitqueue_head(&info
->wq_sync
);
782 spin_lock_init(&info
->shadow_lock
);
784 snprintf(name
, TASK_COMM_LEN
, "vscsiif.%d", host
->host_no
);
786 host
->max_id
= VSCSIIF_MAX_TARGET
;
787 host
->max_channel
= 0;
788 host
->max_lun
= VSCSIIF_MAX_LUN
;
789 host
->max_sectors
= (host
->sg_tablesize
- 1) * PAGE_SIZE
/ 512;
790 host
->max_cmd_len
= VSCSIIF_MAX_COMMAND_SIZE
;
792 err
= scsi_add_host(host
, &dev
->dev
);
794 dev_err(&dev
->dev
, "fail to add scsi host %d\n", err
);
798 info
->host_active
= 1;
800 xenbus_switch_state(dev
, XenbusStateInitialised
);
805 unbind_from_irqhandler(info
->irq
, info
);
806 gnttab_end_foreign_access(info
->ring_ref
, 0,
807 (unsigned long)info
->ring
.sring
);
812 static int scsifront_remove(struct xenbus_device
*dev
)
814 struct vscsifrnt_info
*info
= dev_get_drvdata(&dev
->dev
);
816 pr_debug("%s: %s removed\n", __func__
, dev
->nodename
);
818 mutex_lock(&scsifront_mutex
);
819 if (info
->host_active
) {
820 /* Scsi_host not yet removed */
821 scsi_remove_host(info
->host
);
822 info
->host_active
= 0;
824 mutex_unlock(&scsifront_mutex
);
826 gnttab_end_foreign_access(info
->ring_ref
, 0,
827 (unsigned long)info
->ring
.sring
);
828 unbind_from_irqhandler(info
->irq
, info
);
830 scsi_host_put(info
->host
);
835 static void scsifront_disconnect(struct vscsifrnt_info
*info
)
837 struct xenbus_device
*dev
= info
->dev
;
838 struct Scsi_Host
*host
= info
->host
;
840 pr_debug("%s: %s disconnect\n", __func__
, dev
->nodename
);
843 * When this function is executed, all devices of
844 * Frontend have been deleted.
845 * Therefore, it need not block I/O before remove_host.
848 mutex_lock(&scsifront_mutex
);
849 if (info
->host_active
) {
850 scsi_remove_host(host
);
851 info
->host_active
= 0;
853 mutex_unlock(&scsifront_mutex
);
855 xenbus_frontend_closed(dev
);
858 static void scsifront_do_lun_hotplug(struct vscsifrnt_info
*info
, int op
)
860 struct xenbus_device
*dev
= info
->dev
;
864 unsigned int dir_n
= 0;
865 unsigned int device_state
;
866 unsigned int hst
, chn
, tgt
, lun
;
867 struct scsi_device
*sdev
;
869 dir
= xenbus_directory(XBT_NIL
, dev
->otherend
, "vscsi-devs", &dir_n
);
873 /* mark current task as the one allowed to modify device states */
875 info
->curr
= current
;
877 for (i
= 0; i
< dir_n
; i
++) {
879 snprintf(str
, sizeof(str
), "vscsi-devs/%s/state", dir
[i
]);
880 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, str
, "%u",
882 if (XENBUS_EXIST_ERR(err
))
885 /* virtual SCSI device */
886 snprintf(str
, sizeof(str
), "vscsi-devs/%s/v-dev", dir
[i
]);
887 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, str
,
888 "%u:%u:%u:%u", &hst
, &chn
, &tgt
, &lun
);
889 if (XENBUS_EXIST_ERR(err
))
893 * Front device state path, used in slave_configure called
894 * on successfull scsi_add_device, and in slave_destroy called
895 * on remove of a device.
897 snprintf(info
->dev_state_path
, sizeof(info
->dev_state_path
),
898 "vscsi-devs/%s/state", dir
[i
]);
901 case VSCSIFRONT_OP_ADD_LUN
:
902 if (device_state
!= XenbusStateInitialised
)
905 if (scsi_add_device(info
->host
, chn
, tgt
, lun
)) {
906 dev_err(&dev
->dev
, "scsi_add_device\n");
907 xenbus_printf(XBT_NIL
, dev
->nodename
,
908 info
->dev_state_path
,
909 "%d", XenbusStateClosed
);
912 case VSCSIFRONT_OP_DEL_LUN
:
913 if (device_state
!= XenbusStateClosing
)
916 sdev
= scsi_device_lookup(info
->host
, chn
, tgt
, lun
);
918 scsi_remove_device(sdev
);
919 scsi_device_put(sdev
);
932 static void scsifront_read_backend_params(struct xenbus_device
*dev
,
933 struct vscsifrnt_info
*info
)
935 unsigned int sg_grant
;
937 struct Scsi_Host
*host
= info
->host
;
939 ret
= xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-sg-grant", "%u",
941 if (ret
== 1 && sg_grant
) {
942 sg_grant
= min_t(unsigned int, sg_grant
, SG_ALL
);
943 sg_grant
= max_t(unsigned int, sg_grant
, VSCSIIF_SG_TABLESIZE
);
944 host
->sg_tablesize
= min_t(unsigned int, sg_grant
,
945 VSCSIIF_SG_TABLESIZE
* PAGE_SIZE
/
946 sizeof(struct scsiif_request_segment
));
947 host
->max_sectors
= (host
->sg_tablesize
- 1) * PAGE_SIZE
/ 512;
949 dev_info(&dev
->dev
, "using up to %d SG entries\n", host
->sg_tablesize
);
952 static void scsifront_backend_changed(struct xenbus_device
*dev
,
953 enum xenbus_state backend_state
)
955 struct vscsifrnt_info
*info
= dev_get_drvdata(&dev
->dev
);
957 pr_debug("%s: %p %u %u\n", __func__
, dev
, dev
->state
, backend_state
);
959 switch (backend_state
) {
960 case XenbusStateUnknown
:
961 case XenbusStateInitialising
:
962 case XenbusStateInitWait
:
963 case XenbusStateInitialised
:
966 case XenbusStateConnected
:
967 scsifront_read_backend_params(dev
, info
);
968 if (xenbus_read_driver_state(dev
->nodename
) ==
969 XenbusStateInitialised
)
970 scsifront_do_lun_hotplug(info
, VSCSIFRONT_OP_ADD_LUN
);
972 if (dev
->state
!= XenbusStateConnected
)
973 xenbus_switch_state(dev
, XenbusStateConnected
);
976 case XenbusStateClosed
:
977 if (dev
->state
== XenbusStateClosed
)
979 /* Missed the backend's Closing state -- fallthrough */
980 case XenbusStateClosing
:
981 scsifront_disconnect(info
);
984 case XenbusStateReconfiguring
:
985 scsifront_do_lun_hotplug(info
, VSCSIFRONT_OP_DEL_LUN
);
986 xenbus_switch_state(dev
, XenbusStateReconfiguring
);
989 case XenbusStateReconfigured
:
990 scsifront_do_lun_hotplug(info
, VSCSIFRONT_OP_ADD_LUN
);
991 xenbus_switch_state(dev
, XenbusStateConnected
);
996 static const struct xenbus_device_id scsifront_ids
[] = {
1001 static struct xenbus_driver scsifront_driver
= {
1002 .ids
= scsifront_ids
,
1003 .probe
= scsifront_probe
,
1004 .remove
= scsifront_remove
,
1005 .otherend_changed
= scsifront_backend_changed
,
1008 static int __init
scsifront_init(void)
1013 return xenbus_register_frontend(&scsifront_driver
);
1015 module_init(scsifront_init
);
1017 static void __exit
scsifront_exit(void)
1019 xenbus_unregister_driver(&scsifront_driver
);
1021 module_exit(scsifront_exit
);
1023 MODULE_DESCRIPTION("Xen SCSI frontend driver");
1024 MODULE_LICENSE("GPL");
1025 MODULE_ALIAS("xen:vscsi");
1026 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");