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_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
;
256 shadow
= info
->shadow
[id
];
261 scsifront_gnttab_done(info
, shadow
);
262 scsifront_put_rqid(info
, id
);
264 sc
->result
= ring_rsp
->rslt
;
265 scsi_set_resid(sc
, ring_rsp
->residual_len
);
267 sense_len
= min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE
,
268 ring_rsp
->sense_len
);
271 memcpy(sc
->sense_buffer
, ring_rsp
->sense_buffer
, sense_len
);
276 static void scsifront_sync_cmd_done(struct vscsifrnt_info
*info
,
277 struct vscsiif_response
*ring_rsp
)
279 uint16_t id
= ring_rsp
->rqid
;
281 struct vscsifrnt_shadow
*shadow
= info
->shadow
[id
];
284 spin_lock_irqsave(&info
->shadow_lock
, flags
);
285 shadow
->wait_reset
= 1;
286 switch (shadow
->rslt_reset
) {
287 case RSLT_RESET_WAITING
:
288 shadow
->rslt_reset
= ring_rsp
->rslt
;
291 kick
= _scsifront_put_rqid(info
, id
);
292 spin_unlock_irqrestore(&info
->shadow_lock
, flags
);
295 scsifront_wake_up(info
);
298 shost_printk(KERN_ERR
, info
->host
, KBUILD_MODNAME
299 "bad reset state %d, possibly leaking %u\n",
300 shadow
->rslt_reset
, id
);
303 spin_unlock_irqrestore(&info
->shadow_lock
, flags
);
305 wake_up(&shadow
->wq_reset
);
308 static void scsifront_do_response(struct vscsifrnt_info
*info
,
309 struct vscsiif_response
*ring_rsp
)
311 if (WARN(ring_rsp
->rqid
>= VSCSIIF_MAX_REQS
||
312 test_bit(ring_rsp
->rqid
, info
->shadow_free_bitmap
),
313 "illegal rqid %u returned by backend!\n", ring_rsp
->rqid
))
316 if (info
->shadow
[ring_rsp
->rqid
]->act
== VSCSIIF_ACT_SCSI_CDB
)
317 scsifront_cdb_cmd_done(info
, ring_rsp
);
319 scsifront_sync_cmd_done(info
, ring_rsp
);
322 static int scsifront_ring_drain(struct vscsifrnt_info
*info
)
324 struct vscsiif_response
*ring_rsp
;
328 rp
= info
->ring
.sring
->rsp_prod
;
329 rmb(); /* ordering required respective to dom0 */
330 for (i
= info
->ring
.rsp_cons
; i
!= rp
; i
++) {
331 ring_rsp
= RING_GET_RESPONSE(&info
->ring
, i
);
332 scsifront_do_response(info
, ring_rsp
);
335 info
->ring
.rsp_cons
= i
;
337 if (i
!= info
->ring
.req_prod_pvt
)
338 RING_FINAL_CHECK_FOR_RESPONSES(&info
->ring
, more_to_do
);
340 info
->ring
.sring
->rsp_event
= i
+ 1;
345 static int scsifront_cmd_done(struct vscsifrnt_info
*info
)
350 spin_lock_irqsave(info
->host
->host_lock
, flags
);
352 more_to_do
= scsifront_ring_drain(info
);
354 info
->wait_ring_available
= 0;
356 spin_unlock_irqrestore(info
->host
->host_lock
, flags
);
358 wake_up(&info
->wq_sync
);
363 static irqreturn_t
scsifront_irq_fn(int irq
, void *dev_id
)
365 struct vscsifrnt_info
*info
= dev_id
;
367 while (scsifront_cmd_done(info
))
368 /* Yield point for this unbounded loop. */
374 static void scsifront_finish_all(struct vscsifrnt_info
*info
)
377 struct vscsiif_response resp
;
379 scsifront_ring_drain(info
);
381 for (i
= 0; i
< VSCSIIF_MAX_REQS
; i
++) {
382 if (test_bit(i
, info
->shadow_free_bitmap
))
386 resp
.rslt
= DID_RESET
<< 16;
387 resp
.residual_len
= 0;
388 scsifront_do_response(info
, &resp
);
392 static int map_data_for_request(struct vscsifrnt_info
*info
,
393 struct scsi_cmnd
*sc
,
394 struct vscsifrnt_shadow
*shadow
)
396 grant_ref_t gref_head
;
398 int err
, ref
, ref_cnt
= 0;
399 int grant_ro
= (sc
->sc_data_direction
== DMA_TO_DEVICE
);
400 unsigned int i
, off
, len
, bytes
;
401 unsigned int data_len
= scsi_bufflen(sc
);
402 unsigned int data_grants
= 0, seg_grants
= 0;
403 struct scatterlist
*sg
;
404 struct scsiif_request_segment
*seg
;
406 if (sc
->sc_data_direction
== DMA_NONE
|| !data_len
)
409 scsi_for_each_sg(sc
, sg
, scsi_sg_count(sc
), i
)
410 data_grants
+= PFN_UP(sg
->offset
+ sg
->length
);
412 if (data_grants
> VSCSIIF_SG_TABLESIZE
) {
413 if (data_grants
> info
->host
->sg_tablesize
) {
414 shost_printk(KERN_ERR
, info
->host
, KBUILD_MODNAME
415 "Unable to map request_buffer for command!\n");
418 seg_grants
= vscsiif_grants_sg(data_grants
);
419 shadow
->sg
= kcalloc(data_grants
,
420 sizeof(struct scsiif_request_segment
), GFP_ATOMIC
);
424 seg
= shadow
->sg
? : shadow
->seg
;
426 err
= gnttab_alloc_grant_references(seg_grants
+ data_grants
,
430 shost_printk(KERN_ERR
, info
->host
, KBUILD_MODNAME
431 "gnttab_alloc_grant_references() error\n");
436 page
= virt_to_page(seg
);
437 off
= (unsigned long)seg
& ~PAGE_MASK
;
438 len
= sizeof(struct scsiif_request_segment
) * data_grants
;
440 bytes
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
442 ref
= gnttab_claim_grant_reference(&gref_head
);
443 BUG_ON(ref
== -ENOSPC
);
445 gnttab_grant_foreign_access_ref(ref
,
446 info
->dev
->otherend_id
,
447 xen_page_to_gfn(page
), 1);
448 shadow
->gref
[ref_cnt
] = ref
;
449 shadow
->seg
[ref_cnt
].gref
= ref
;
450 shadow
->seg
[ref_cnt
].offset
= (uint16_t)off
;
451 shadow
->seg
[ref_cnt
].length
= (uint16_t)bytes
;
458 BUG_ON(seg_grants
< ref_cnt
);
459 seg_grants
= ref_cnt
;
462 scsi_for_each_sg(sc
, sg
, scsi_sg_count(sc
), i
) {
467 while (len
> 0 && data_len
> 0) {
469 * sg sends a scatterlist that is larger than
470 * the data_len it wants transferred for certain
473 bytes
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
474 bytes
= min(bytes
, data_len
);
476 ref
= gnttab_claim_grant_reference(&gref_head
);
477 BUG_ON(ref
== -ENOSPC
);
479 gnttab_grant_foreign_access_ref(ref
,
480 info
->dev
->otherend_id
,
481 xen_page_to_gfn(page
),
484 shadow
->gref
[ref_cnt
] = ref
;
486 seg
->offset
= (uint16_t)off
;
487 seg
->length
= (uint16_t)bytes
;
499 shadow
->nr_segments
= VSCSIIF_SG_GRANT
| seg_grants
;
501 shadow
->nr_segments
= (uint8_t)ref_cnt
;
502 shadow
->nr_grants
= ref_cnt
;
507 static int scsifront_enter(struct vscsifrnt_info
*info
)
515 static void scsifront_return(struct vscsifrnt_info
*info
)
521 if (!info
->waiting_pause
)
524 info
->waiting_pause
= 0;
525 wake_up(&info
->wq_pause
);
528 static int scsifront_queuecommand(struct Scsi_Host
*shost
,
529 struct scsi_cmnd
*sc
)
531 struct vscsifrnt_info
*info
= shost_priv(shost
);
532 struct vscsifrnt_shadow
*shadow
= scsi_cmd_priv(sc
);
537 memset(shadow
, 0, sizeof(*shadow
));
540 shadow
->act
= VSCSIIF_ACT_SCSI_CDB
;
542 spin_lock_irqsave(shost
->host_lock
, flags
);
543 if (scsifront_enter(info
)) {
544 spin_unlock_irqrestore(shost
->host_lock
, flags
);
545 return SCSI_MLQUEUE_HOST_BUSY
;
548 err
= map_data_for_request(info
, sc
, shadow
);
550 pr_debug("%s: err %d\n", __func__
, err
);
551 scsifront_return(info
);
552 spin_unlock_irqrestore(shost
->host_lock
, flags
);
554 return SCSI_MLQUEUE_HOST_BUSY
;
555 sc
->result
= DID_ERROR
<< 16;
560 if (scsifront_do_request(info
, shadow
)) {
561 scsifront_gnttab_done(info
, shadow
);
565 scsifront_return(info
);
566 spin_unlock_irqrestore(shost
->host_lock
, flags
);
571 scsifront_return(info
);
572 spin_unlock_irqrestore(shost
->host_lock
, flags
);
573 pr_debug("%s: busy\n", __func__
);
574 return SCSI_MLQUEUE_HOST_BUSY
;
578 * Any exception handling (reset or abort) must be forwarded to the backend.
579 * We have to wait until an answer is returned. This answer contains the
580 * result to be returned to the requestor.
582 static int scsifront_action_handler(struct scsi_cmnd
*sc
, uint8_t act
)
584 struct Scsi_Host
*host
= sc
->device
->host
;
585 struct vscsifrnt_info
*info
= shost_priv(host
);
586 struct vscsifrnt_shadow
*shadow
, *s
= scsi_cmd_priv(sc
);
589 shadow
= kzalloc(sizeof(*shadow
), GFP_NOIO
);
594 shadow
->rslt_reset
= RSLT_RESET_WAITING
;
596 shadow
->ref_rqid
= s
->rqid
;
597 init_waitqueue_head(&shadow
->wq_reset
);
599 spin_lock_irq(host
->host_lock
);
602 if (scsifront_enter(info
))
605 if (!scsifront_do_request(info
, shadow
))
608 scsifront_return(info
);
611 info
->wait_ring_available
= 1;
612 spin_unlock_irq(host
->host_lock
);
613 err
= wait_event_interruptible(info
->wq_sync
,
614 !info
->wait_ring_available
);
615 spin_lock_irq(host
->host_lock
);
618 spin_unlock_irq(host
->host_lock
);
619 err
= wait_event_interruptible(shadow
->wq_reset
, shadow
->wait_reset
);
620 spin_lock_irq(host
->host_lock
);
623 err
= shadow
->rslt_reset
;
624 scsifront_put_rqid(info
, shadow
->rqid
);
627 spin_lock(&info
->shadow_lock
);
628 shadow
->rslt_reset
= RSLT_RESET_ERR
;
629 spin_unlock(&info
->shadow_lock
);
633 scsifront_return(info
);
634 spin_unlock_irq(host
->host_lock
);
638 spin_unlock_irq(host
->host_lock
);
643 static int scsifront_eh_abort_handler(struct scsi_cmnd
*sc
)
645 pr_debug("%s\n", __func__
);
646 return scsifront_action_handler(sc
, VSCSIIF_ACT_SCSI_ABORT
);
649 static int scsifront_dev_reset_handler(struct scsi_cmnd
*sc
)
651 pr_debug("%s\n", __func__
);
652 return scsifront_action_handler(sc
, VSCSIIF_ACT_SCSI_RESET
);
655 static int scsifront_sdev_configure(struct scsi_device
*sdev
)
657 struct vscsifrnt_info
*info
= shost_priv(sdev
->host
);
659 if (info
&& current
== info
->curr
)
660 xenbus_printf(XBT_NIL
, info
->dev
->nodename
,
661 info
->dev_state_path
, "%d", XenbusStateConnected
);
666 static void scsifront_sdev_destroy(struct scsi_device
*sdev
)
668 struct vscsifrnt_info
*info
= shost_priv(sdev
->host
);
670 if (info
&& current
== info
->curr
)
671 xenbus_printf(XBT_NIL
, info
->dev
->nodename
,
672 info
->dev_state_path
, "%d", XenbusStateClosed
);
675 static struct scsi_host_template scsifront_sht
= {
676 .module
= THIS_MODULE
,
677 .name
= "Xen SCSI frontend driver",
678 .queuecommand
= scsifront_queuecommand
,
679 .eh_abort_handler
= scsifront_eh_abort_handler
,
680 .eh_device_reset_handler
= scsifront_dev_reset_handler
,
681 .slave_configure
= scsifront_sdev_configure
,
682 .slave_destroy
= scsifront_sdev_destroy
,
683 .cmd_per_lun
= VSCSIIF_DEFAULT_CMD_PER_LUN
,
684 .can_queue
= VSCSIIF_MAX_REQS
,
686 .cmd_size
= sizeof(struct vscsifrnt_shadow
),
687 .sg_tablesize
= VSCSIIF_SG_TABLESIZE
,
688 .use_clustering
= DISABLE_CLUSTERING
,
689 .proc_name
= "scsifront",
692 static int scsifront_alloc_ring(struct vscsifrnt_info
*info
)
694 struct xenbus_device
*dev
= info
->dev
;
695 struct vscsiif_sring
*sring
;
699 /***** Frontend to Backend ring start *****/
700 sring
= (struct vscsiif_sring
*)__get_free_page(GFP_KERNEL
);
702 xenbus_dev_fatal(dev
, err
,
703 "fail to allocate shared ring (Front to Back)");
706 SHARED_RING_INIT(sring
);
707 FRONT_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
709 err
= xenbus_grant_ring(dev
, sring
, 1, &gref
);
711 free_page((unsigned long)sring
);
712 xenbus_dev_fatal(dev
, err
,
713 "fail to grant shared ring (Front to Back)");
716 info
->ring_ref
= gref
;
718 err
= xenbus_alloc_evtchn(dev
, &info
->evtchn
);
720 xenbus_dev_fatal(dev
, err
, "xenbus_alloc_evtchn");
724 err
= bind_evtchn_to_irq(info
->evtchn
);
726 xenbus_dev_fatal(dev
, err
, "bind_evtchn_to_irq");
732 err
= request_threaded_irq(info
->irq
, NULL
, scsifront_irq_fn
,
733 IRQF_ONESHOT
, "scsifront", info
);
735 xenbus_dev_fatal(dev
, err
, "request_threaded_irq");
743 unbind_from_irqhandler(info
->irq
, info
);
745 gnttab_end_foreign_access(info
->ring_ref
, 0,
746 (unsigned long)info
->ring
.sring
);
751 static void scsifront_free_ring(struct vscsifrnt_info
*info
)
753 unbind_from_irqhandler(info
->irq
, info
);
754 gnttab_end_foreign_access(info
->ring_ref
, 0,
755 (unsigned long)info
->ring
.sring
);
758 static int scsifront_init_ring(struct vscsifrnt_info
*info
)
760 struct xenbus_device
*dev
= info
->dev
;
761 struct xenbus_transaction xbt
;
764 pr_debug("%s\n", __func__
);
766 err
= scsifront_alloc_ring(info
);
769 pr_debug("%s: %u %u\n", __func__
, info
->ring_ref
, info
->evtchn
);
772 err
= xenbus_transaction_start(&xbt
);
774 xenbus_dev_fatal(dev
, err
, "starting transaction");
776 err
= xenbus_printf(xbt
, dev
->nodename
, "ring-ref", "%u",
779 xenbus_dev_fatal(dev
, err
, "%s", "writing ring-ref");
783 err
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
787 xenbus_dev_fatal(dev
, err
, "%s", "writing event-channel");
791 err
= xenbus_transaction_end(xbt
, 0);
795 xenbus_dev_fatal(dev
, err
, "completing transaction");
802 xenbus_transaction_end(xbt
, 1);
804 scsifront_free_ring(info
);
810 static int scsifront_probe(struct xenbus_device
*dev
,
811 const struct xenbus_device_id
*id
)
813 struct vscsifrnt_info
*info
;
814 struct Scsi_Host
*host
;
816 char name
[TASK_COMM_LEN
];
818 host
= scsi_host_alloc(&scsifront_sht
, sizeof(*info
));
820 xenbus_dev_fatal(dev
, err
, "fail to allocate scsi host");
823 info
= (struct vscsifrnt_info
*)host
->hostdata
;
825 dev_set_drvdata(&dev
->dev
, info
);
828 bitmap_fill(info
->shadow_free_bitmap
, VSCSIIF_MAX_REQS
);
830 err
= scsifront_init_ring(info
);
836 init_waitqueue_head(&info
->wq_sync
);
837 init_waitqueue_head(&info
->wq_pause
);
838 spin_lock_init(&info
->shadow_lock
);
840 snprintf(name
, TASK_COMM_LEN
, "vscsiif.%d", host
->host_no
);
842 host
->max_id
= VSCSIIF_MAX_TARGET
;
843 host
->max_channel
= 0;
844 host
->max_lun
= VSCSIIF_MAX_LUN
;
845 host
->max_sectors
= (host
->sg_tablesize
- 1) * PAGE_SIZE
/ 512;
846 host
->max_cmd_len
= VSCSIIF_MAX_COMMAND_SIZE
;
848 err
= scsi_add_host(host
, &dev
->dev
);
850 dev_err(&dev
->dev
, "fail to add scsi host %d\n", err
);
854 info
->host_active
= 1;
856 xenbus_switch_state(dev
, XenbusStateInitialised
);
861 scsifront_free_ring(info
);
866 static int scsifront_resume(struct xenbus_device
*dev
)
868 struct vscsifrnt_info
*info
= dev_get_drvdata(&dev
->dev
);
869 struct Scsi_Host
*host
= info
->host
;
872 spin_lock_irq(host
->host_lock
);
874 /* Finish all still pending commands. */
875 scsifront_finish_all(info
);
877 spin_unlock_irq(host
->host_lock
);
879 /* Reconnect to dom0. */
880 scsifront_free_ring(info
);
881 err
= scsifront_init_ring(info
);
883 dev_err(&dev
->dev
, "fail to resume %d\n", err
);
888 xenbus_switch_state(dev
, XenbusStateInitialised
);
893 static int scsifront_suspend(struct xenbus_device
*dev
)
895 struct vscsifrnt_info
*info
= dev_get_drvdata(&dev
->dev
);
896 struct Scsi_Host
*host
= info
->host
;
899 /* No new commands for the backend. */
900 spin_lock_irq(host
->host_lock
);
902 while (info
->callers
&& !err
) {
903 info
->waiting_pause
= 1;
904 info
->wait_ring_available
= 0;
905 spin_unlock_irq(host
->host_lock
);
906 wake_up(&info
->wq_sync
);
907 err
= wait_event_interruptible(info
->wq_pause
,
908 !info
->waiting_pause
);
909 spin_lock_irq(host
->host_lock
);
911 spin_unlock_irq(host
->host_lock
);
915 static int scsifront_remove(struct xenbus_device
*dev
)
917 struct vscsifrnt_info
*info
= dev_get_drvdata(&dev
->dev
);
919 pr_debug("%s: %s removed\n", __func__
, dev
->nodename
);
921 mutex_lock(&scsifront_mutex
);
922 if (info
->host_active
) {
923 /* Scsi_host not yet removed */
924 scsi_remove_host(info
->host
);
925 info
->host_active
= 0;
927 mutex_unlock(&scsifront_mutex
);
929 scsifront_free_ring(info
);
930 scsi_host_put(info
->host
);
935 static void scsifront_disconnect(struct vscsifrnt_info
*info
)
937 struct xenbus_device
*dev
= info
->dev
;
938 struct Scsi_Host
*host
= info
->host
;
940 pr_debug("%s: %s disconnect\n", __func__
, dev
->nodename
);
943 * When this function is executed, all devices of
944 * Frontend have been deleted.
945 * Therefore, it need not block I/O before remove_host.
948 mutex_lock(&scsifront_mutex
);
949 if (info
->host_active
) {
950 scsi_remove_host(host
);
951 info
->host_active
= 0;
953 mutex_unlock(&scsifront_mutex
);
955 xenbus_frontend_closed(dev
);
958 static void scsifront_do_lun_hotplug(struct vscsifrnt_info
*info
, int op
)
960 struct xenbus_device
*dev
= info
->dev
;
964 unsigned int dir_n
= 0;
965 unsigned int device_state
;
966 unsigned int hst
, chn
, tgt
, lun
;
967 struct scsi_device
*sdev
;
969 dir
= xenbus_directory(XBT_NIL
, dev
->otherend
, "vscsi-devs", &dir_n
);
973 /* mark current task as the one allowed to modify device states */
975 info
->curr
= current
;
977 for (i
= 0; i
< dir_n
; i
++) {
979 snprintf(str
, sizeof(str
), "vscsi-devs/%s/state", dir
[i
]);
980 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, str
, "%u",
982 if (XENBUS_EXIST_ERR(err
))
985 /* virtual SCSI device */
986 snprintf(str
, sizeof(str
), "vscsi-devs/%s/v-dev", dir
[i
]);
987 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, str
,
988 "%u:%u:%u:%u", &hst
, &chn
, &tgt
, &lun
);
989 if (XENBUS_EXIST_ERR(err
))
993 * Front device state path, used in slave_configure called
994 * on successfull scsi_add_device, and in slave_destroy called
995 * on remove of a device.
997 snprintf(info
->dev_state_path
, sizeof(info
->dev_state_path
),
998 "vscsi-devs/%s/state", dir
[i
]);
1001 case VSCSIFRONT_OP_ADD_LUN
:
1002 if (device_state
!= XenbusStateInitialised
)
1005 if (scsi_add_device(info
->host
, chn
, tgt
, lun
)) {
1006 dev_err(&dev
->dev
, "scsi_add_device\n");
1007 xenbus_printf(XBT_NIL
, dev
->nodename
,
1008 info
->dev_state_path
,
1009 "%d", XenbusStateClosed
);
1012 case VSCSIFRONT_OP_DEL_LUN
:
1013 if (device_state
!= XenbusStateClosing
)
1016 sdev
= scsi_device_lookup(info
->host
, chn
, tgt
, lun
);
1018 scsi_remove_device(sdev
);
1019 scsi_device_put(sdev
);
1022 case VSCSIFRONT_OP_READD_LUN
:
1023 if (device_state
== XenbusStateConnected
)
1024 xenbus_printf(XBT_NIL
, dev
->nodename
,
1025 info
->dev_state_path
,
1026 "%d", XenbusStateConnected
);
1038 static void scsifront_read_backend_params(struct xenbus_device
*dev
,
1039 struct vscsifrnt_info
*info
)
1041 unsigned int sg_grant
, nr_segs
;
1042 struct Scsi_Host
*host
= info
->host
;
1044 sg_grant
= xenbus_read_unsigned(dev
->otherend
, "feature-sg-grant", 0);
1045 nr_segs
= min_t(unsigned int, sg_grant
, SG_ALL
);
1046 nr_segs
= max_t(unsigned int, nr_segs
, VSCSIIF_SG_TABLESIZE
);
1047 nr_segs
= min_t(unsigned int, nr_segs
,
1048 VSCSIIF_SG_TABLESIZE
* PAGE_SIZE
/
1049 sizeof(struct scsiif_request_segment
));
1051 if (!info
->pause
&& sg_grant
)
1052 dev_info(&dev
->dev
, "using up to %d SG entries\n", nr_segs
);
1053 else if (info
->pause
&& nr_segs
< host
->sg_tablesize
)
1055 "SG entries decreased from %d to %u - device may not work properly anymore\n",
1056 host
->sg_tablesize
, nr_segs
);
1058 host
->sg_tablesize
= nr_segs
;
1059 host
->max_sectors
= (nr_segs
- 1) * PAGE_SIZE
/ 512;
1062 static void scsifront_backend_changed(struct xenbus_device
*dev
,
1063 enum xenbus_state backend_state
)
1065 struct vscsifrnt_info
*info
= dev_get_drvdata(&dev
->dev
);
1067 pr_debug("%s: %p %u %u\n", __func__
, dev
, dev
->state
, backend_state
);
1069 switch (backend_state
) {
1070 case XenbusStateUnknown
:
1071 case XenbusStateInitialising
:
1072 case XenbusStateInitWait
:
1073 case XenbusStateInitialised
:
1076 case XenbusStateConnected
:
1077 scsifront_read_backend_params(dev
, info
);
1080 scsifront_do_lun_hotplug(info
, VSCSIFRONT_OP_READD_LUN
);
1081 xenbus_switch_state(dev
, XenbusStateConnected
);
1086 if (xenbus_read_driver_state(dev
->nodename
) ==
1087 XenbusStateInitialised
)
1088 scsifront_do_lun_hotplug(info
, VSCSIFRONT_OP_ADD_LUN
);
1090 if (dev
->state
!= XenbusStateConnected
)
1091 xenbus_switch_state(dev
, XenbusStateConnected
);
1094 case XenbusStateClosed
:
1095 if (dev
->state
== XenbusStateClosed
)
1097 /* Missed the backend's Closing state -- fallthrough */
1098 case XenbusStateClosing
:
1099 scsifront_disconnect(info
);
1102 case XenbusStateReconfiguring
:
1103 scsifront_do_lun_hotplug(info
, VSCSIFRONT_OP_DEL_LUN
);
1104 xenbus_switch_state(dev
, XenbusStateReconfiguring
);
1107 case XenbusStateReconfigured
:
1108 scsifront_do_lun_hotplug(info
, VSCSIFRONT_OP_ADD_LUN
);
1109 xenbus_switch_state(dev
, XenbusStateConnected
);
1114 static const struct xenbus_device_id scsifront_ids
[] = {
1119 static struct xenbus_driver scsifront_driver
= {
1120 .ids
= scsifront_ids
,
1121 .probe
= scsifront_probe
,
1122 .remove
= scsifront_remove
,
1123 .resume
= scsifront_resume
,
1124 .suspend
= scsifront_suspend
,
1125 .otherend_changed
= scsifront_backend_changed
,
1128 static int __init
scsifront_init(void)
1133 return xenbus_register_frontend(&scsifront_driver
);
1135 module_init(scsifront_init
);
1137 static void __exit
scsifront_exit(void)
1139 xenbus_unregister_driver(&scsifront_driver
);
1141 module_exit(scsifront_exit
);
1143 MODULE_DESCRIPTION("Xen SCSI frontend driver");
1144 MODULE_LICENSE("GPL");
1145 MODULE_ALIAS("xen:vscsi");
1146 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");