2 * Xen SCSI backend driver
4 * Copyright (c) 2008, FUJITSU Limited
6 * Based on the blkback driver code.
7 * Adaption to kernel taget core infrastructure taken from vhost/scsi.c
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
36 #include <linux/module.h>
37 #include <linux/utsname.h>
38 #include <linux/interrupt.h>
39 #include <linux/slab.h>
40 #include <linux/wait.h>
41 #include <linux/sched.h>
42 #include <linux/list.h>
43 #include <linux/gfp.h>
44 #include <linux/delay.h>
45 #include <linux/spinlock.h>
46 #include <linux/configfs.h>
48 #include <generated/utsrelease.h>
50 #include <scsi/scsi_dbg.h>
51 #include <scsi/scsi_eh.h>
52 #include <scsi/scsi_tcq.h>
54 #include <target/target_core_base.h>
55 #include <target/target_core_fabric.h>
56 #include <target/target_core_configfs.h>
57 #include <target/target_core_fabric_configfs.h>
59 #include <asm/hypervisor.h>
62 #include <xen/balloon.h>
63 #include <xen/events.h>
64 #include <xen/xenbus.h>
65 #include <xen/grant_table.h>
68 #include <xen/interface/grant_table.h>
69 #include <xen/interface/io/vscsiif.h>
71 #define DPRINTK(_f, _a...) \
72 pr_debug("(file=%s, line=%d) " _f, __FILE__ , __LINE__ , ## _a)
74 #define VSCSI_VERSION "v0.1"
75 #define VSCSI_NAMELEN 32
78 unsigned int hst
; /* host */
79 unsigned int chn
; /* channel */
80 unsigned int tgt
; /* target */
81 unsigned int lun
; /* LUN */
85 struct ids_tuple v
; /* translate from */
86 struct scsiback_tpg
*tpg
; /* translate to */
93 struct xenbus_device
*dev
;
98 struct vscsiif_back_ring ring
;
101 spinlock_t ring_lock
;
102 atomic_t nr_unreplied_reqs
;
105 struct list_head v2p_entry_lists
;
107 wait_queue_head_t waiting_to_free
;
110 /* theoretical maximum of grants for one request */
111 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
114 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
115 * call to map/unmap grants. Don't choose it too large, as there are arrays
116 * with VSCSI_GRANT_BATCH elements allocated on the stack.
118 #define VSCSI_GRANT_BATCH 16
120 struct vscsibk_pend
{
123 uint8_t cmnd
[VSCSIIF_MAX_COMMAND_SIZE
];
126 uint8_t sc_data_direction
;
127 uint16_t n_sg
; /* real length of SG list */
128 uint16_t n_grants
; /* SG pages and potentially SG list */
132 struct vscsibk_info
*info
;
133 struct v2p_entry
*v2p
;
134 struct scatterlist
*sgl
;
136 uint8_t sense_buffer
[VSCSIIF_SENSE_BUFFERSIZE
];
138 grant_handle_t grant_handles
[VSCSI_MAX_GRANTS
];
139 struct page
*pages
[VSCSI_MAX_GRANTS
];
141 struct se_cmd se_cmd
;
144 struct scsiback_tmr
{
145 atomic_t tmr_complete
;
146 wait_queue_head_t tmr_wait
;
149 struct scsiback_nexus
{
150 /* Pointer to TCM session for I_T Nexus */
151 struct se_session
*tvn_se_sess
;
154 struct scsiback_tport
{
155 /* SCSI protocol the tport is providing */
157 /* Binary World Wide unique Port Name for pvscsi Target port */
159 /* ASCII formatted WWPN for pvscsi Target port */
160 char tport_name
[VSCSI_NAMELEN
];
161 /* Returned by scsiback_make_tport() */
162 struct se_wwn tport_wwn
;
165 struct scsiback_tpg
{
166 /* scsiback port target portal group tag for TCM */
168 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
169 int tv_tpg_port_count
;
170 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
172 /* list for scsiback_list */
173 struct list_head tv_tpg_list
;
174 /* Used to protect access for tpg_nexus */
175 struct mutex tv_tpg_mutex
;
176 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
177 struct scsiback_nexus
*tpg_nexus
;
178 /* Pointer back to scsiback_tport */
179 struct scsiback_tport
*tport
;
180 /* Returned by scsiback_make_tpg() */
181 struct se_portal_group se_tpg
;
182 /* alias used in xenstore */
183 char param_alias
[VSCSI_NAMELEN
];
184 /* list of info structures related to this target portal group */
185 struct list_head info_list
;
188 #define SCSIBACK_INVALID_HANDLE (~0)
190 static bool log_print_stat
;
191 module_param(log_print_stat
, bool, 0644);
193 static int scsiback_max_buffer_pages
= 1024;
194 module_param_named(max_buffer_pages
, scsiback_max_buffer_pages
, int, 0644);
195 MODULE_PARM_DESC(max_buffer_pages
,
196 "Maximum number of free pages to keep in backend buffer");
198 static struct kmem_cache
*scsiback_cachep
;
199 static DEFINE_SPINLOCK(free_pages_lock
);
200 static int free_pages_num
;
201 static LIST_HEAD(scsiback_free_pages
);
203 /* Global spinlock to protect scsiback TPG list */
204 static DEFINE_MUTEX(scsiback_mutex
);
205 static LIST_HEAD(scsiback_list
);
207 /* Local pointer to allocated TCM configfs fabric module */
208 static struct target_fabric_configfs
*scsiback_fabric_configfs
;
210 static void scsiback_get(struct vscsibk_info
*info
)
212 atomic_inc(&info
->nr_unreplied_reqs
);
215 static void scsiback_put(struct vscsibk_info
*info
)
217 if (atomic_dec_and_test(&info
->nr_unreplied_reqs
))
218 wake_up(&info
->waiting_to_free
);
221 static void put_free_pages(struct page
**page
, int num
)
224 int i
= free_pages_num
+ num
, n
= num
;
228 if (i
> scsiback_max_buffer_pages
) {
229 n
= min(num
, i
- scsiback_max_buffer_pages
);
230 free_xenballooned_pages(n
, page
+ num
- n
);
233 spin_lock_irqsave(&free_pages_lock
, flags
);
234 for (i
= 0; i
< n
; i
++)
235 list_add(&page
[i
]->lru
, &scsiback_free_pages
);
237 spin_unlock_irqrestore(&free_pages_lock
, flags
);
240 static int get_free_page(struct page
**page
)
244 spin_lock_irqsave(&free_pages_lock
, flags
);
245 if (list_empty(&scsiback_free_pages
)) {
246 spin_unlock_irqrestore(&free_pages_lock
, flags
);
247 return alloc_xenballooned_pages(1, page
, false);
249 page
[0] = list_first_entry(&scsiback_free_pages
, struct page
, lru
);
250 list_del(&page
[0]->lru
);
252 spin_unlock_irqrestore(&free_pages_lock
, flags
);
256 static unsigned long vaddr_page(struct page
*page
)
258 unsigned long pfn
= page_to_pfn(page
);
260 return (unsigned long)pfn_to_kaddr(pfn
);
263 static unsigned long vaddr(struct vscsibk_pend
*req
, int seg
)
265 return vaddr_page(req
->pages
[seg
]);
268 static void scsiback_print_status(char *sense_buffer
, int errors
,
269 struct vscsibk_pend
*pending_req
)
271 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
273 pr_err("xen-pvscsi[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
274 tpg
->tport
->tport_name
, pending_req
->v2p
->lun
,
275 pending_req
->cmnd
[0], status_byte(errors
), msg_byte(errors
),
276 host_byte(errors
), driver_byte(errors
));
279 static void scsiback_fast_flush_area(struct vscsibk_pend
*req
)
281 struct gnttab_unmap_grant_ref unmap
[VSCSI_GRANT_BATCH
];
282 struct page
*pages
[VSCSI_GRANT_BATCH
];
283 unsigned int i
, invcount
= 0;
284 grant_handle_t handle
;
294 for (i
= 0; i
< req
->n_grants
; i
++) {
295 handle
= req
->grant_handles
[i
];
296 if (handle
== SCSIBACK_INVALID_HANDLE
)
298 gnttab_set_unmap_op(&unmap
[invcount
], vaddr(req
, i
),
299 GNTMAP_host_map
, handle
);
300 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
301 pages
[invcount
] = req
->pages
[i
];
302 put_page(pages
[invcount
]);
304 if (invcount
< VSCSI_GRANT_BATCH
)
306 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
312 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
316 put_free_pages(req
->pages
, req
->n_grants
);
320 static void scsiback_free_translation_entry(struct kref
*kref
)
322 struct v2p_entry
*entry
= container_of(kref
, struct v2p_entry
, kref
);
323 struct scsiback_tpg
*tpg
= entry
->tpg
;
325 mutex_lock(&tpg
->tv_tpg_mutex
);
326 tpg
->tv_tpg_fe_count
--;
327 mutex_unlock(&tpg
->tv_tpg_mutex
);
332 static void scsiback_do_resp_with_sense(char *sense_buffer
, int32_t result
,
333 uint32_t resid
, struct vscsibk_pend
*pending_req
)
335 struct vscsiif_response
*ring_res
;
336 struct vscsibk_info
*info
= pending_req
->info
;
338 struct scsi_sense_hdr sshdr
;
342 spin_lock_irqsave(&info
->ring_lock
, flags
);
344 ring_res
= RING_GET_RESPONSE(&info
->ring
, info
->ring
.rsp_prod_pvt
);
345 info
->ring
.rsp_prod_pvt
++;
347 ring_res
->rslt
= result
;
348 ring_res
->rqid
= pending_req
->rqid
;
350 if (sense_buffer
!= NULL
&&
351 scsi_normalize_sense(sense_buffer
, VSCSIIF_SENSE_BUFFERSIZE
,
353 len
= min_t(unsigned, 8 + sense_buffer
[7],
354 VSCSIIF_SENSE_BUFFERSIZE
);
355 memcpy(ring_res
->sense_buffer
, sense_buffer
, len
);
356 ring_res
->sense_len
= len
;
358 ring_res
->sense_len
= 0;
361 ring_res
->residual_len
= resid
;
363 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info
->ring
, notify
);
364 spin_unlock_irqrestore(&info
->ring_lock
, flags
);
367 notify_remote_via_irq(info
->irq
);
369 if (pending_req
->v2p
)
370 kref_put(&pending_req
->v2p
->kref
,
371 scsiback_free_translation_entry
);
374 static void scsiback_cmd_done(struct vscsibk_pend
*pending_req
)
376 struct vscsibk_info
*info
= pending_req
->info
;
377 unsigned char *sense_buffer
;
381 sense_buffer
= pending_req
->sense_buffer
;
382 resid
= pending_req
->se_cmd
.residual_count
;
383 errors
= pending_req
->result
;
385 if (errors
&& log_print_stat
)
386 scsiback_print_status(sense_buffer
, errors
, pending_req
);
388 scsiback_fast_flush_area(pending_req
);
389 scsiback_do_resp_with_sense(sense_buffer
, errors
, resid
, pending_req
);
393 static void scsiback_cmd_exec(struct vscsibk_pend
*pending_req
)
395 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
396 struct se_session
*sess
= pending_req
->v2p
->tpg
->tpg_nexus
->tvn_se_sess
;
399 memset(pending_req
->sense_buffer
, 0, VSCSIIF_SENSE_BUFFERSIZE
);
401 memset(se_cmd
, 0, sizeof(*se_cmd
));
403 scsiback_get(pending_req
->info
);
404 rc
= target_submit_cmd_map_sgls(se_cmd
, sess
, pending_req
->cmnd
,
405 pending_req
->sense_buffer
, pending_req
->v2p
->lun
,
406 pending_req
->data_len
, 0,
407 pending_req
->sc_data_direction
, 0,
408 pending_req
->sgl
, pending_req
->n_sg
,
411 transport_send_check_condition_and_sense(se_cmd
,
412 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
413 transport_generic_free_cmd(se_cmd
, 0);
417 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref
*map
,
418 struct page
**pg
, grant_handle_t
*grant
, int cnt
)
425 err
= gnttab_map_refs(map
, NULL
, pg
, cnt
);
427 for (i
= 0; i
< cnt
; i
++) {
428 if (unlikely(map
[i
].status
!= GNTST_okay
)) {
429 pr_err("xen-pvscsi: invalid buffer -- could not remap it\n");
430 map
[i
].handle
= SCSIBACK_INVALID_HANDLE
;
435 grant
[i
] = map
[i
].handle
;
440 static int scsiback_gnttab_data_map_list(struct vscsibk_pend
*pending_req
,
441 struct scsiif_request_segment
*seg
, struct page
**pg
,
442 grant_handle_t
*grant
, int cnt
, u32 flags
)
444 int mapcount
= 0, i
, err
= 0;
445 struct gnttab_map_grant_ref map
[VSCSI_GRANT_BATCH
];
446 struct vscsibk_info
*info
= pending_req
->info
;
448 for (i
= 0; i
< cnt
; i
++) {
449 if (get_free_page(pg
+ mapcount
)) {
450 put_free_pages(pg
, mapcount
);
451 pr_err("xen-pvscsi: no grant page\n");
454 gnttab_set_map_op(&map
[mapcount
], vaddr_page(pg
[mapcount
]),
455 flags
, seg
[i
].gref
, info
->domid
);
457 if (mapcount
< VSCSI_GRANT_BATCH
)
459 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
462 pending_req
->n_grants
+= mapcount
;
467 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
468 pending_req
->n_grants
+= mapcount
;
472 static int scsiback_gnttab_data_map(struct vscsiif_request
*ring_req
,
473 struct vscsibk_pend
*pending_req
)
476 int i
, err
, n_segs
, i_seg
= 0;
478 struct scsiif_request_segment
*seg
;
479 unsigned long end_seg
= 0;
480 unsigned int nr_segments
= (unsigned int)ring_req
->nr_segments
;
481 unsigned int nr_sgl
= 0;
482 struct scatterlist
*sg
;
483 grant_handle_t
*grant
;
485 pending_req
->n_sg
= 0;
486 pending_req
->n_grants
= 0;
487 pending_req
->data_len
= 0;
489 nr_segments
&= ~VSCSIIF_SG_GRANT
;
493 if (nr_segments
> VSCSIIF_SG_TABLESIZE
) {
494 DPRINTK("xen-pvscsi: invalid parameter nr_seg = %d\n",
495 ring_req
->nr_segments
);
499 if (ring_req
->nr_segments
& VSCSIIF_SG_GRANT
) {
500 err
= scsiback_gnttab_data_map_list(pending_req
, ring_req
->seg
,
501 pending_req
->pages
, pending_req
->grant_handles
,
502 nr_segments
, GNTMAP_host_map
| GNTMAP_readonly
);
505 nr_sgl
= nr_segments
;
507 for (i
= 0; i
< nr_sgl
; i
++) {
508 n_segs
= ring_req
->seg
[i
].length
/
509 sizeof(struct scsiif_request_segment
);
510 if ((unsigned)ring_req
->seg
[i
].offset
+
511 (unsigned)ring_req
->seg
[i
].length
> PAGE_SIZE
||
512 n_segs
* sizeof(struct scsiif_request_segment
) !=
513 ring_req
->seg
[i
].length
)
515 nr_segments
+= n_segs
;
517 if (nr_segments
> SG_ALL
) {
518 DPRINTK("xen-pvscsi: invalid nr_seg = %d\n",
524 /* free of (sgl) in fast_flush_area()*/
525 pending_req
->sgl
= kmalloc_array(nr_segments
,
526 sizeof(struct scatterlist
), GFP_KERNEL
);
527 if (!pending_req
->sgl
)
530 sg_init_table(pending_req
->sgl
, nr_segments
);
531 pending_req
->n_sg
= nr_segments
;
533 flags
= GNTMAP_host_map
;
534 if (pending_req
->sc_data_direction
== DMA_TO_DEVICE
)
535 flags
|= GNTMAP_readonly
;
537 pg
= pending_req
->pages
+ nr_sgl
;
538 grant
= pending_req
->grant_handles
+ nr_sgl
;
541 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
542 pg
, grant
, nr_segments
, flags
);
546 for (i
= 0; i
< nr_sgl
; i
++) {
547 seg
= (struct scsiif_request_segment
*)(
548 vaddr(pending_req
, i
) + ring_req
->seg
[i
].offset
);
549 n_segs
= ring_req
->seg
[i
].length
/
550 sizeof(struct scsiif_request_segment
);
551 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
552 pg
, grant
, n_segs
, flags
);
558 end_seg
= vaddr(pending_req
, 0) + ring_req
->seg
[0].offset
;
559 seg
= (struct scsiif_request_segment
*)end_seg
;
560 end_seg
+= ring_req
->seg
[0].length
;
561 pg
= pending_req
->pages
+ nr_sgl
;
564 for_each_sg(pending_req
->sgl
, sg
, nr_segments
, i
) {
565 sg_set_page(sg
, pg
[i
], seg
->length
, seg
->offset
);
566 pending_req
->data_len
+= seg
->length
;
568 if (nr_sgl
&& (unsigned long)seg
>= end_seg
) {
570 end_seg
= vaddr(pending_req
, i_seg
) +
571 ring_req
->seg
[i_seg
].offset
;
572 seg
= (struct scsiif_request_segment
*)end_seg
;
573 end_seg
+= ring_req
->seg
[i_seg
].length
;
575 if (sg
->offset
>= PAGE_SIZE
||
576 sg
->length
> PAGE_SIZE
||
577 sg
->offset
+ sg
->length
> PAGE_SIZE
)
584 static void scsiback_disconnect(struct vscsibk_info
*info
)
586 wait_event(info
->waiting_to_free
,
587 atomic_read(&info
->nr_unreplied_reqs
) == 0);
589 unbind_from_irqhandler(info
->irq
, info
);
591 xenbus_unmap_ring_vfree(info
->dev
, info
->ring
.sring
);
594 static void scsiback_device_action(struct vscsibk_pend
*pending_req
,
595 enum tcm_tmreq_table act
, int tag
)
597 int rc
, err
= FAILED
;
598 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
599 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
600 struct scsiback_tmr
*tmr
;
602 tmr
= kzalloc(sizeof(struct scsiback_tmr
), GFP_KERNEL
);
606 init_waitqueue_head(&tmr
->tmr_wait
);
608 transport_init_se_cmd(se_cmd
, tpg
->se_tpg
.se_tpg_tfo
,
609 tpg
->tpg_nexus
->tvn_se_sess
, 0, DMA_NONE
, MSG_SIMPLE_TAG
,
610 &pending_req
->sense_buffer
[0]);
612 rc
= core_tmr_alloc_req(se_cmd
, tmr
, act
, GFP_KERNEL
);
616 se_cmd
->se_tmr_req
->ref_task_tag
= tag
;
618 if (transport_lookup_tmr_lun(se_cmd
, pending_req
->v2p
->lun
) < 0)
621 transport_generic_handle_tmr(se_cmd
);
622 wait_event(tmr
->tmr_wait
, atomic_read(&tmr
->tmr_complete
));
624 err
= (se_cmd
->se_tmr_req
->response
== TMR_FUNCTION_COMPLETE
) ?
629 transport_generic_free_cmd(&pending_req
->se_cmd
, 1);
633 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
635 kmem_cache_free(scsiback_cachep
, pending_req
);
639 Perform virtual to physical translation
641 static struct v2p_entry
*scsiback_do_translation(struct vscsibk_info
*info
,
644 struct v2p_entry
*entry
;
645 struct list_head
*head
= &(info
->v2p_entry_lists
);
648 spin_lock_irqsave(&info
->v2p_lock
, flags
);
649 list_for_each_entry(entry
, head
, l
) {
650 if ((entry
->v
.chn
== v
->chn
) &&
651 (entry
->v
.tgt
== v
->tgt
) &&
652 (entry
->v
.lun
== v
->lun
)) {
653 kref_get(&entry
->kref
);
660 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
664 static int prepare_pending_reqs(struct vscsibk_info
*info
,
665 struct vscsiif_request
*ring_req
,
666 struct vscsibk_pend
*pending_req
)
668 struct v2p_entry
*v2p
;
669 struct ids_tuple vir
;
671 pending_req
->rqid
= ring_req
->rqid
;
672 pending_req
->info
= info
;
674 vir
.chn
= ring_req
->channel
;
675 vir
.tgt
= ring_req
->id
;
676 vir
.lun
= ring_req
->lun
;
678 v2p
= scsiback_do_translation(info
, &vir
);
680 pending_req
->v2p
= NULL
;
681 DPRINTK("xen-pvscsi: doesn't exist.\n");
684 pending_req
->v2p
= v2p
;
686 /* request range check from frontend */
687 pending_req
->sc_data_direction
= ring_req
->sc_data_direction
;
688 if ((pending_req
->sc_data_direction
!= DMA_BIDIRECTIONAL
) &&
689 (pending_req
->sc_data_direction
!= DMA_TO_DEVICE
) &&
690 (pending_req
->sc_data_direction
!= DMA_FROM_DEVICE
) &&
691 (pending_req
->sc_data_direction
!= DMA_NONE
)) {
692 DPRINTK("xen-pvscsi: invalid parameter data_dir = %d\n",
693 pending_req
->sc_data_direction
);
697 pending_req
->cmd_len
= ring_req
->cmd_len
;
698 if (pending_req
->cmd_len
> VSCSIIF_MAX_COMMAND_SIZE
) {
699 DPRINTK("xen-pvscsi: invalid parameter cmd_len = %d\n",
700 pending_req
->cmd_len
);
703 memcpy(pending_req
->cmnd
, ring_req
->cmnd
, pending_req
->cmd_len
);
708 static int scsiback_do_cmd_fn(struct vscsibk_info
*info
)
710 struct vscsiif_back_ring
*ring
= &info
->ring
;
711 struct vscsiif_request
*ring_req
;
712 struct vscsibk_pend
*pending_req
;
719 rp
= ring
->sring
->req_prod
;
720 rmb(); /* guest system is accessing ring, too */
722 if (RING_REQUEST_PROD_OVERFLOW(ring
, rp
)) {
723 rc
= ring
->rsp_prod_pvt
;
724 pr_warn("xen-pvscsi: Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
725 info
->domid
, rp
, rc
, rp
- rc
);
726 info
->ring_error
= 1;
731 if (RING_REQUEST_CONS_OVERFLOW(ring
, rc
))
733 pending_req
= kmem_cache_alloc(scsiback_cachep
, GFP_KERNEL
);
737 ring_req
= RING_GET_REQUEST(ring
, rc
);
738 ring
->req_cons
= ++rc
;
741 err
= prepare_pending_reqs(info
, ring_req
, pending_req
);
745 result
= DID_NO_CONNECT
;
748 result
= DRIVER_ERROR
;
751 scsiback_do_resp_with_sense(NULL
, result
<< 24, 0,
753 kmem_cache_free(scsiback_cachep
, pending_req
);
758 case VSCSIIF_ACT_SCSI_CDB
:
759 if (scsiback_gnttab_data_map(ring_req
, pending_req
)) {
760 scsiback_fast_flush_area(pending_req
);
761 scsiback_do_resp_with_sense(NULL
,
762 DRIVER_ERROR
<< 24, 0, pending_req
);
763 kmem_cache_free(scsiback_cachep
, pending_req
);
765 scsiback_cmd_exec(pending_req
);
768 case VSCSIIF_ACT_SCSI_ABORT
:
769 scsiback_device_action(pending_req
, TMR_ABORT_TASK
,
772 case VSCSIIF_ACT_SCSI_RESET
:
773 scsiback_device_action(pending_req
, TMR_LUN_RESET
, 0);
776 pr_err_ratelimited("xen-pvscsi: invalid request\n");
777 scsiback_do_resp_with_sense(NULL
, DRIVER_ERROR
<< 24,
779 kmem_cache_free(scsiback_cachep
, pending_req
);
783 /* Yield point for this unbounded loop. */
787 RING_FINAL_CHECK_FOR_REQUESTS(&info
->ring
, more_to_do
);
791 static irqreturn_t
scsiback_irq_fn(int irq
, void *dev_id
)
793 struct vscsibk_info
*info
= dev_id
;
795 if (info
->ring_error
)
798 while (scsiback_do_cmd_fn(info
))
804 static int scsiback_init_sring(struct vscsibk_info
*info
, grant_ref_t ring_ref
,
805 evtchn_port_t evtchn
)
808 struct vscsiif_sring
*sring
;
814 err
= xenbus_map_ring_valloc(info
->dev
, ring_ref
, &area
);
818 sring
= (struct vscsiif_sring
*)area
;
819 BACK_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
821 err
= bind_interdomain_evtchn_to_irq(info
->domid
, evtchn
);
827 err
= request_threaded_irq(info
->irq
, NULL
, scsiback_irq_fn
,
828 IRQF_ONESHOT
, "vscsiif-backend", info
);
835 unbind_from_irqhandler(info
->irq
, info
);
838 xenbus_unmap_ring_vfree(info
->dev
, area
);
843 static int scsiback_map(struct vscsibk_info
*info
)
845 struct xenbus_device
*dev
= info
->dev
;
846 unsigned int ring_ref
, evtchn
;
849 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
850 "ring-ref", "%u", &ring_ref
,
851 "event-channel", "%u", &evtchn
, NULL
);
853 xenbus_dev_fatal(dev
, err
, "reading %s ring", dev
->otherend
);
857 return scsiback_init_sring(info
, ring_ref
, evtchn
);
861 Add a new translation entry
863 static int scsiback_add_translation_entry(struct vscsibk_info
*info
,
864 char *phy
, struct ids_tuple
*v
)
867 struct v2p_entry
*entry
;
868 struct v2p_entry
*new;
869 struct list_head
*head
= &(info
->v2p_entry_lists
);
873 struct scsiback_tpg
*tpg_entry
, *tpg
= NULL
;
874 char *error
= "doesn't exist";
876 lunp
= strrchr(phy
, ':');
878 pr_err("xen-pvscsi: illegal format of physical device %s\n",
884 if (kstrtouint(lunp
, 10, &lun
) || lun
>= TRANSPORT_MAX_LUNS_PER_TPG
) {
885 pr_err("xen-pvscsi: lun number not valid: %s\n", lunp
);
889 mutex_lock(&scsiback_mutex
);
890 list_for_each_entry(tpg_entry
, &scsiback_list
, tv_tpg_list
) {
891 if (!strcmp(phy
, tpg_entry
->tport
->tport_name
) ||
892 !strcmp(phy
, tpg_entry
->param_alias
)) {
893 spin_lock(&tpg_entry
->se_tpg
.tpg_lun_lock
);
894 if (tpg_entry
->se_tpg
.tpg_lun_list
[lun
]->lun_status
==
895 TRANSPORT_LUN_STATUS_ACTIVE
) {
896 if (!tpg_entry
->tpg_nexus
)
897 error
= "nexus undefined";
901 spin_unlock(&tpg_entry
->se_tpg
.tpg_lun_lock
);
906 mutex_lock(&tpg
->tv_tpg_mutex
);
907 tpg
->tv_tpg_fe_count
++;
908 mutex_unlock(&tpg
->tv_tpg_mutex
);
910 mutex_unlock(&scsiback_mutex
);
913 pr_err("xen-pvscsi: %s:%d %s\n", phy
, lun
, error
);
917 new = kmalloc(sizeof(struct v2p_entry
), GFP_KERNEL
);
923 spin_lock_irqsave(&info
->v2p_lock
, flags
);
925 /* Check double assignment to identical virtual ID */
926 list_for_each_entry(entry
, head
, l
) {
927 if ((entry
->v
.chn
== v
->chn
) &&
928 (entry
->v
.tgt
== v
->tgt
) &&
929 (entry
->v
.lun
== v
->lun
)) {
930 pr_warn("xen-pvscsi: Virtual ID is already used. Assignment was not performed.\n");
937 /* Create a new translation entry and add to the list */
938 kref_init(&new->kref
);
942 list_add_tail(&new->l
, head
);
945 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
948 mutex_lock(&tpg
->tv_tpg_mutex
);
949 tpg
->tv_tpg_fe_count
--;
950 mutex_unlock(&tpg
->tv_tpg_mutex
);
958 static void __scsiback_del_translation_entry(struct v2p_entry
*entry
)
961 kref_put(&entry
->kref
, scsiback_free_translation_entry
);
965 Delete the translation entry specfied
967 static int scsiback_del_translation_entry(struct vscsibk_info
*info
,
970 struct v2p_entry
*entry
;
971 struct list_head
*head
= &(info
->v2p_entry_lists
);
974 spin_lock_irqsave(&info
->v2p_lock
, flags
);
975 /* Find out the translation entry specified */
976 list_for_each_entry(entry
, head
, l
) {
977 if ((entry
->v
.chn
== v
->chn
) &&
978 (entry
->v
.tgt
== v
->tgt
) &&
979 (entry
->v
.lun
== v
->lun
)) {
984 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
988 /* Delete the translation entry specfied */
989 __scsiback_del_translation_entry(entry
);
991 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
995 static void scsiback_do_add_lun(struct vscsibk_info
*info
, const char *state
,
996 char *phy
, struct ids_tuple
*vir
)
998 if (!scsiback_add_translation_entry(info
, phy
, vir
)) {
999 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1000 "%d", XenbusStateInitialised
)) {
1001 pr_err("xen-pvscsi: xenbus_printf error %s\n", state
);
1002 scsiback_del_translation_entry(info
, vir
);
1005 xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1006 "%d", XenbusStateClosed
);
1010 static void scsiback_do_del_lun(struct vscsibk_info
*info
, const char *state
,
1011 struct ids_tuple
*vir
)
1013 if (!scsiback_del_translation_entry(info
, vir
)) {
1014 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1015 "%d", XenbusStateClosed
))
1016 pr_err("xen-pvscsi: xenbus_printf error %s\n", state
);
1020 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1021 #define VSCSIBACK_OP_UPDATEDEV_STATE 2
1023 static void scsiback_do_1lun_hotplug(struct vscsibk_info
*info
, int op
,
1027 struct ids_tuple vir
;
1030 char phy
[VSCSI_NAMELEN
];
1033 struct xenbus_device
*dev
= info
->dev
;
1036 snprintf(state
, sizeof(state
), "vscsi-devs/%s/state", ent
);
1037 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, state
, "%u", &device_state
);
1038 if (XENBUS_EXIST_ERR(err
))
1041 /* physical SCSI device */
1042 snprintf(str
, sizeof(str
), "vscsi-devs/%s/p-dev", ent
);
1043 val
= xenbus_read(XBT_NIL
, dev
->nodename
, str
, NULL
);
1045 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1046 "%d", XenbusStateClosed
);
1049 strlcpy(phy
, val
, VSCSI_NAMELEN
);
1052 /* virtual SCSI device */
1053 snprintf(str
, sizeof(str
), "vscsi-devs/%s/v-dev", ent
);
1054 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, str
, "%u:%u:%u:%u",
1055 &vir
.hst
, &vir
.chn
, &vir
.tgt
, &vir
.lun
);
1056 if (XENBUS_EXIST_ERR(err
)) {
1057 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1058 "%d", XenbusStateClosed
);
1063 case VSCSIBACK_OP_ADD_OR_DEL_LUN
:
1064 if (device_state
== XenbusStateInitialising
)
1065 scsiback_do_add_lun(info
, state
, phy
, &vir
);
1066 if (device_state
== XenbusStateClosing
)
1067 scsiback_do_del_lun(info
, state
, &vir
);
1070 case VSCSIBACK_OP_UPDATEDEV_STATE
:
1071 if (device_state
== XenbusStateInitialised
) {
1072 /* modify vscsi-devs/dev-x/state */
1073 if (xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1074 "%d", XenbusStateConnected
)) {
1075 pr_err("xen-pvscsi: xenbus_printf error %s\n",
1077 scsiback_del_translation_entry(info
, &vir
);
1078 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1079 "%d", XenbusStateClosed
);
1083 /*When it is necessary, processing is added here.*/
1089 static void scsiback_do_lun_hotplug(struct vscsibk_info
*info
, int op
)
1093 unsigned int ndir
= 0;
1095 dir
= xenbus_directory(XBT_NIL
, info
->dev
->nodename
, "vscsi-devs",
1100 for (i
= 0; i
< ndir
; i
++)
1101 scsiback_do_1lun_hotplug(info
, op
, dir
[i
]);
1106 static void scsiback_frontend_changed(struct xenbus_device
*dev
,
1107 enum xenbus_state frontend_state
)
1109 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1111 switch (frontend_state
) {
1112 case XenbusStateInitialising
:
1115 case XenbusStateInitialised
:
1116 if (scsiback_map(info
))
1119 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1120 xenbus_switch_state(dev
, XenbusStateConnected
);
1123 case XenbusStateConnected
:
1124 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_UPDATEDEV_STATE
);
1126 if (dev
->state
== XenbusStateConnected
)
1129 xenbus_switch_state(dev
, XenbusStateConnected
);
1132 case XenbusStateClosing
:
1134 scsiback_disconnect(info
);
1136 xenbus_switch_state(dev
, XenbusStateClosing
);
1139 case XenbusStateClosed
:
1140 xenbus_switch_state(dev
, XenbusStateClosed
);
1141 if (xenbus_dev_is_online(dev
))
1143 /* fall through if not online */
1144 case XenbusStateUnknown
:
1145 device_unregister(&dev
->dev
);
1148 case XenbusStateReconfiguring
:
1149 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1150 xenbus_switch_state(dev
, XenbusStateReconfigured
);
1155 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
1162 Release the translation entry specfied
1164 static void scsiback_release_translation_entry(struct vscsibk_info
*info
)
1166 struct v2p_entry
*entry
, *tmp
;
1167 struct list_head
*head
= &(info
->v2p_entry_lists
);
1168 unsigned long flags
;
1170 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1172 list_for_each_entry_safe(entry
, tmp
, head
, l
)
1173 __scsiback_del_translation_entry(entry
);
1175 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1178 static int scsiback_remove(struct xenbus_device
*dev
)
1180 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1183 scsiback_disconnect(info
);
1185 scsiback_release_translation_entry(info
);
1187 dev_set_drvdata(&dev
->dev
, NULL
);
1192 static int scsiback_probe(struct xenbus_device
*dev
,
1193 const struct xenbus_device_id
*id
)
1197 struct vscsibk_info
*info
= kzalloc(sizeof(struct vscsibk_info
),
1200 DPRINTK("%p %d\n", dev
, dev
->otherend_id
);
1203 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating backend structure");
1207 dev_set_drvdata(&dev
->dev
, info
);
1209 info
->domid
= dev
->otherend_id
;
1210 spin_lock_init(&info
->ring_lock
);
1211 info
->ring_error
= 0;
1212 atomic_set(&info
->nr_unreplied_reqs
, 0);
1213 init_waitqueue_head(&info
->waiting_to_free
);
1216 INIT_LIST_HEAD(&info
->v2p_entry_lists
);
1217 spin_lock_init(&info
->v2p_lock
);
1219 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, "feature-sg-grant", "%u",
1222 xenbus_dev_error(dev
, err
, "writing feature-sg-grant");
1224 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
1231 pr_warn("xen-pvscsi: %s failed\n", __func__
);
1232 scsiback_remove(dev
);
1237 static char *scsiback_dump_proto_id(struct scsiback_tport
*tport
)
1239 switch (tport
->tport_proto_id
) {
1240 case SCSI_PROTOCOL_SAS
:
1242 case SCSI_PROTOCOL_FCP
:
1244 case SCSI_PROTOCOL_ISCSI
:
1253 static u8
scsiback_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
1255 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1256 struct scsiback_tpg
, se_tpg
);
1257 struct scsiback_tport
*tport
= tpg
->tport
;
1259 switch (tport
->tport_proto_id
) {
1260 case SCSI_PROTOCOL_SAS
:
1261 return sas_get_fabric_proto_ident(se_tpg
);
1262 case SCSI_PROTOCOL_FCP
:
1263 return fc_get_fabric_proto_ident(se_tpg
);
1264 case SCSI_PROTOCOL_ISCSI
:
1265 return iscsi_get_fabric_proto_ident(se_tpg
);
1267 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1268 tport
->tport_proto_id
);
1272 return sas_get_fabric_proto_ident(se_tpg
);
1275 static char *scsiback_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1277 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1278 struct scsiback_tpg
, se_tpg
);
1279 struct scsiback_tport
*tport
= tpg
->tport
;
1281 return &tport
->tport_name
[0];
1284 static u16
scsiback_get_tag(struct se_portal_group
*se_tpg
)
1286 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1287 struct scsiback_tpg
, se_tpg
);
1288 return tpg
->tport_tpgt
;
1291 static u32
scsiback_get_default_depth(struct se_portal_group
*se_tpg
)
1297 scsiback_get_pr_transport_id(struct se_portal_group
*se_tpg
,
1298 struct se_node_acl
*se_nacl
,
1299 struct t10_pr_registration
*pr_reg
,
1303 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1304 struct scsiback_tpg
, se_tpg
);
1305 struct scsiback_tport
*tport
= tpg
->tport
;
1307 switch (tport
->tport_proto_id
) {
1308 case SCSI_PROTOCOL_SAS
:
1309 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1311 case SCSI_PROTOCOL_FCP
:
1312 return fc_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1314 case SCSI_PROTOCOL_ISCSI
:
1315 return iscsi_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1318 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1319 tport
->tport_proto_id
);
1323 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1328 scsiback_get_pr_transport_id_len(struct se_portal_group
*se_tpg
,
1329 struct se_node_acl
*se_nacl
,
1330 struct t10_pr_registration
*pr_reg
,
1333 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1334 struct scsiback_tpg
, se_tpg
);
1335 struct scsiback_tport
*tport
= tpg
->tport
;
1337 switch (tport
->tport_proto_id
) {
1338 case SCSI_PROTOCOL_SAS
:
1339 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1341 case SCSI_PROTOCOL_FCP
:
1342 return fc_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1344 case SCSI_PROTOCOL_ISCSI
:
1345 return iscsi_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1348 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1349 tport
->tport_proto_id
);
1353 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1358 scsiback_parse_pr_out_transport_id(struct se_portal_group
*se_tpg
,
1361 char **port_nexus_ptr
)
1363 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1364 struct scsiback_tpg
, se_tpg
);
1365 struct scsiback_tport
*tport
= tpg
->tport
;
1367 switch (tport
->tport_proto_id
) {
1368 case SCSI_PROTOCOL_SAS
:
1369 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1371 case SCSI_PROTOCOL_FCP
:
1372 return fc_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1374 case SCSI_PROTOCOL_ISCSI
:
1375 return iscsi_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1378 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1379 tport
->tport_proto_id
);
1383 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1387 static struct se_wwn
*
1388 scsiback_make_tport(struct target_fabric_configfs
*tf
,
1389 struct config_group
*group
,
1392 struct scsiback_tport
*tport
;
1397 tport
= kzalloc(sizeof(struct scsiback_tport
), GFP_KERNEL
);
1399 return ERR_PTR(-ENOMEM
);
1401 tport
->tport_wwpn
= wwpn
;
1403 * Determine the emulated Protocol Identifier and Target Port Name
1404 * based on the incoming configfs directory name.
1406 ptr
= strstr(name
, "naa.");
1408 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1411 ptr
= strstr(name
, "fc.");
1413 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1414 off
= 3; /* Skip over "fc." */
1417 ptr
= strstr(name
, "iqn.");
1419 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1423 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name
);
1425 return ERR_PTR(-EINVAL
);
1428 if (strlen(name
) >= VSCSI_NAMELEN
) {
1429 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name
,
1430 scsiback_dump_proto_id(tport
), VSCSI_NAMELEN
);
1432 return ERR_PTR(-EINVAL
);
1434 snprintf(&tport
->tport_name
[0], VSCSI_NAMELEN
, "%s", &name
[off
]);
1436 pr_debug("xen-pvscsi: Allocated emulated Target %s Address: %s\n",
1437 scsiback_dump_proto_id(tport
), name
);
1439 return &tport
->tport_wwn
;
1442 static void scsiback_drop_tport(struct se_wwn
*wwn
)
1444 struct scsiback_tport
*tport
= container_of(wwn
,
1445 struct scsiback_tport
, tport_wwn
);
1447 pr_debug("xen-pvscsi: Deallocating emulated Target %s Address: %s\n",
1448 scsiback_dump_proto_id(tport
), tport
->tport_name
);
1453 static struct se_node_acl
*
1454 scsiback_alloc_fabric_acl(struct se_portal_group
*se_tpg
)
1456 return kzalloc(sizeof(struct se_node_acl
), GFP_KERNEL
);
1460 scsiback_release_fabric_acl(struct se_portal_group
*se_tpg
,
1461 struct se_node_acl
*se_nacl
)
1466 static u32
scsiback_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1471 static int scsiback_check_stop_free(struct se_cmd
*se_cmd
)
1474 * Do not release struct se_cmd's containing a valid TMR
1475 * pointer. These will be released directly in scsiback_device_action()
1476 * with transport_generic_free_cmd().
1478 if (se_cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)
1481 transport_generic_free_cmd(se_cmd
, 0);
1485 static void scsiback_release_cmd(struct se_cmd
*se_cmd
)
1487 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1488 struct vscsibk_pend
, se_cmd
);
1490 kmem_cache_free(scsiback_cachep
, pending_req
);
1493 static int scsiback_shutdown_session(struct se_session
*se_sess
)
1498 static void scsiback_close_session(struct se_session
*se_sess
)
1502 static u32
scsiback_sess_get_index(struct se_session
*se_sess
)
1507 static int scsiback_write_pending(struct se_cmd
*se_cmd
)
1509 /* Go ahead and process the write immediately */
1510 target_execute_cmd(se_cmd
);
1515 static int scsiback_write_pending_status(struct se_cmd
*se_cmd
)
1520 static void scsiback_set_default_node_attrs(struct se_node_acl
*nacl
)
1524 static u32
scsiback_get_task_tag(struct se_cmd
*se_cmd
)
1526 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1527 struct vscsibk_pend
, se_cmd
);
1529 return pending_req
->rqid
;
1532 static int scsiback_get_cmd_state(struct se_cmd
*se_cmd
)
1537 static int scsiback_queue_data_in(struct se_cmd
*se_cmd
)
1539 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1540 struct vscsibk_pend
, se_cmd
);
1542 pending_req
->result
= SAM_STAT_GOOD
;
1543 scsiback_cmd_done(pending_req
);
1547 static int scsiback_queue_status(struct se_cmd
*se_cmd
)
1549 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1550 struct vscsibk_pend
, se_cmd
);
1552 if (se_cmd
->sense_buffer
&&
1553 ((se_cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
1554 (se_cmd
->se_cmd_flags
& SCF_EMULATED_TASK_SENSE
)))
1555 pending_req
->result
= (DRIVER_SENSE
<< 24) |
1556 SAM_STAT_CHECK_CONDITION
;
1558 pending_req
->result
= se_cmd
->scsi_status
;
1560 scsiback_cmd_done(pending_req
);
1564 static void scsiback_queue_tm_rsp(struct se_cmd
*se_cmd
)
1566 struct se_tmr_req
*se_tmr
= se_cmd
->se_tmr_req
;
1567 struct scsiback_tmr
*tmr
= se_tmr
->fabric_tmr_ptr
;
1569 atomic_set(&tmr
->tmr_complete
, 1);
1570 wake_up(&tmr
->tmr_wait
);
1573 static void scsiback_aborted_task(struct se_cmd
*se_cmd
)
1577 static ssize_t
scsiback_tpg_param_show_alias(struct se_portal_group
*se_tpg
,
1580 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1584 mutex_lock(&tpg
->tv_tpg_mutex
);
1585 rb
= snprintf(page
, PAGE_SIZE
, "%s\n", tpg
->param_alias
);
1586 mutex_unlock(&tpg
->tv_tpg_mutex
);
1591 static ssize_t
scsiback_tpg_param_store_alias(struct se_portal_group
*se_tpg
,
1592 const char *page
, size_t count
)
1594 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1598 if (strlen(page
) >= VSCSI_NAMELEN
) {
1599 pr_err("param alias: %s, exceeds max: %d\n", page
,
1604 mutex_lock(&tpg
->tv_tpg_mutex
);
1605 len
= snprintf(tpg
->param_alias
, VSCSI_NAMELEN
, "%s", page
);
1606 if (tpg
->param_alias
[len
- 1] == '\n')
1607 tpg
->param_alias
[len
- 1] = '\0';
1608 mutex_unlock(&tpg
->tv_tpg_mutex
);
1613 TF_TPG_PARAM_ATTR(scsiback
, alias
, S_IRUGO
| S_IWUSR
);
1615 static struct configfs_attribute
*scsiback_param_attrs
[] = {
1616 &scsiback_tpg_param_alias
.attr
,
1620 static int scsiback_make_nexus(struct scsiback_tpg
*tpg
,
1623 struct se_portal_group
*se_tpg
;
1624 struct se_session
*se_sess
;
1625 struct scsiback_nexus
*tv_nexus
;
1627 mutex_lock(&tpg
->tv_tpg_mutex
);
1628 if (tpg
->tpg_nexus
) {
1629 mutex_unlock(&tpg
->tv_tpg_mutex
);
1630 pr_debug("tpg->tpg_nexus already exists\n");
1633 se_tpg
= &tpg
->se_tpg
;
1635 tv_nexus
= kzalloc(sizeof(struct scsiback_nexus
), GFP_KERNEL
);
1637 mutex_unlock(&tpg
->tv_tpg_mutex
);
1641 * Initialize the struct se_session pointer
1643 tv_nexus
->tvn_se_sess
= transport_init_session(TARGET_PROT_NORMAL
);
1644 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1645 mutex_unlock(&tpg
->tv_tpg_mutex
);
1649 se_sess
= tv_nexus
->tvn_se_sess
;
1651 * Since we are running in 'demo mode' this call with generate a
1652 * struct se_node_acl for the scsiback struct se_portal_group with
1653 * the SCSI Initiator port name of the passed configfs group 'name'.
1655 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1656 se_tpg
, (unsigned char *)name
);
1657 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1658 mutex_unlock(&tpg
->tv_tpg_mutex
);
1659 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1664 * Now register the TCM pvscsi virtual I_T Nexus as active with the
1665 * call to __transport_register_session()
1667 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1668 tv_nexus
->tvn_se_sess
, tv_nexus
);
1669 tpg
->tpg_nexus
= tv_nexus
;
1671 mutex_unlock(&tpg
->tv_tpg_mutex
);
1675 transport_free_session(se_sess
);
1680 static int scsiback_drop_nexus(struct scsiback_tpg
*tpg
)
1682 struct se_session
*se_sess
;
1683 struct scsiback_nexus
*tv_nexus
;
1685 mutex_lock(&tpg
->tv_tpg_mutex
);
1686 tv_nexus
= tpg
->tpg_nexus
;
1688 mutex_unlock(&tpg
->tv_tpg_mutex
);
1692 se_sess
= tv_nexus
->tvn_se_sess
;
1694 mutex_unlock(&tpg
->tv_tpg_mutex
);
1698 if (tpg
->tv_tpg_port_count
!= 0) {
1699 mutex_unlock(&tpg
->tv_tpg_mutex
);
1700 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1701 tpg
->tv_tpg_port_count
);
1705 if (tpg
->tv_tpg_fe_count
!= 0) {
1706 mutex_unlock(&tpg
->tv_tpg_mutex
);
1707 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1708 tpg
->tv_tpg_fe_count
);
1712 pr_debug("xen-pvscsi: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1713 scsiback_dump_proto_id(tpg
->tport
),
1714 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1717 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1719 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1720 tpg
->tpg_nexus
= NULL
;
1721 mutex_unlock(&tpg
->tv_tpg_mutex
);
1727 static ssize_t
scsiback_tpg_show_nexus(struct se_portal_group
*se_tpg
,
1730 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1731 struct scsiback_tpg
, se_tpg
);
1732 struct scsiback_nexus
*tv_nexus
;
1735 mutex_lock(&tpg
->tv_tpg_mutex
);
1736 tv_nexus
= tpg
->tpg_nexus
;
1738 mutex_unlock(&tpg
->tv_tpg_mutex
);
1741 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1742 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1743 mutex_unlock(&tpg
->tv_tpg_mutex
);
1748 static ssize_t
scsiback_tpg_store_nexus(struct se_portal_group
*se_tpg
,
1752 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1753 struct scsiback_tpg
, se_tpg
);
1754 struct scsiback_tport
*tport_wwn
= tpg
->tport
;
1755 unsigned char i_port
[VSCSI_NAMELEN
], *ptr
, *port_ptr
;
1758 * Shutdown the active I_T nexus if 'NULL' is passed..
1760 if (!strncmp(page
, "NULL", 4)) {
1761 ret
= scsiback_drop_nexus(tpg
);
1762 return (!ret
) ? count
: ret
;
1765 * Otherwise make sure the passed virtual Initiator port WWN matches
1766 * the fabric protocol_id set in scsiback_make_tport(), and call
1767 * scsiback_make_nexus().
1769 if (strlen(page
) >= VSCSI_NAMELEN
) {
1770 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1771 page
, VSCSI_NAMELEN
);
1774 snprintf(&i_port
[0], VSCSI_NAMELEN
, "%s", page
);
1776 ptr
= strstr(i_port
, "naa.");
1778 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1779 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1780 i_port
, scsiback_dump_proto_id(tport_wwn
));
1783 port_ptr
= &i_port
[0];
1786 ptr
= strstr(i_port
, "fc.");
1788 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1789 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1790 i_port
, scsiback_dump_proto_id(tport_wwn
));
1793 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1796 ptr
= strstr(i_port
, "iqn.");
1798 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1799 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1800 i_port
, scsiback_dump_proto_id(tport_wwn
));
1803 port_ptr
= &i_port
[0];
1806 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1810 * Clear any trailing newline for the NAA WWN
1813 if (i_port
[strlen(i_port
) - 1] == '\n')
1814 i_port
[strlen(i_port
) - 1] = '\0';
1816 ret
= scsiback_make_nexus(tpg
, port_ptr
);
1823 TF_TPG_BASE_ATTR(scsiback
, nexus
, S_IRUGO
| S_IWUSR
);
1825 static struct configfs_attribute
*scsiback_tpg_attrs
[] = {
1826 &scsiback_tpg_nexus
.attr
,
1831 scsiback_wwn_show_attr_version(struct target_fabric_configfs
*tf
,
1834 return sprintf(page
, "xen-pvscsi fabric module %s on %s/%s on "
1836 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1839 TF_WWN_ATTR_RO(scsiback
, version
);
1841 static struct configfs_attribute
*scsiback_wwn_attrs
[] = {
1842 &scsiback_wwn_version
.attr
,
1846 static char *scsiback_get_fabric_name(void)
1848 return "xen-pvscsi";
1851 static int scsiback_port_link(struct se_portal_group
*se_tpg
,
1854 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1855 struct scsiback_tpg
, se_tpg
);
1857 mutex_lock(&tpg
->tv_tpg_mutex
);
1858 tpg
->tv_tpg_port_count
++;
1859 mutex_unlock(&tpg
->tv_tpg_mutex
);
1864 static void scsiback_port_unlink(struct se_portal_group
*se_tpg
,
1867 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1868 struct scsiback_tpg
, se_tpg
);
1870 mutex_lock(&tpg
->tv_tpg_mutex
);
1871 tpg
->tv_tpg_port_count
--;
1872 mutex_unlock(&tpg
->tv_tpg_mutex
);
1875 static struct se_portal_group
*
1876 scsiback_make_tpg(struct se_wwn
*wwn
,
1877 struct config_group
*group
,
1880 struct scsiback_tport
*tport
= container_of(wwn
,
1881 struct scsiback_tport
, tport_wwn
);
1883 struct scsiback_tpg
*tpg
;
1887 if (strstr(name
, "tpgt_") != name
)
1888 return ERR_PTR(-EINVAL
);
1889 ret
= kstrtou16(name
+ 5, 10, &tpgt
);
1891 return ERR_PTR(ret
);
1893 tpg
= kzalloc(sizeof(struct scsiback_tpg
), GFP_KERNEL
);
1895 return ERR_PTR(-ENOMEM
);
1897 mutex_init(&tpg
->tv_tpg_mutex
);
1898 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1899 INIT_LIST_HEAD(&tpg
->info_list
);
1901 tpg
->tport_tpgt
= tpgt
;
1903 ret
= core_tpg_register(&scsiback_fabric_configfs
->tf_ops
, wwn
,
1904 &tpg
->se_tpg
, tpg
, TRANSPORT_TPG_TYPE_NORMAL
);
1909 mutex_lock(&scsiback_mutex
);
1910 list_add_tail(&tpg
->tv_tpg_list
, &scsiback_list
);
1911 mutex_unlock(&scsiback_mutex
);
1913 return &tpg
->se_tpg
;
1916 static void scsiback_drop_tpg(struct se_portal_group
*se_tpg
)
1918 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1919 struct scsiback_tpg
, se_tpg
);
1921 mutex_lock(&scsiback_mutex
);
1922 list_del(&tpg
->tv_tpg_list
);
1923 mutex_unlock(&scsiback_mutex
);
1925 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1927 scsiback_drop_nexus(tpg
);
1929 * Deregister the se_tpg from TCM..
1931 core_tpg_deregister(se_tpg
);
1935 static int scsiback_check_true(struct se_portal_group
*se_tpg
)
1940 static int scsiback_check_false(struct se_portal_group
*se_tpg
)
1945 static struct target_core_fabric_ops scsiback_ops
= {
1946 .get_fabric_name
= scsiback_get_fabric_name
,
1947 .get_fabric_proto_ident
= scsiback_get_fabric_proto_ident
,
1948 .tpg_get_wwn
= scsiback_get_fabric_wwn
,
1949 .tpg_get_tag
= scsiback_get_tag
,
1950 .tpg_get_default_depth
= scsiback_get_default_depth
,
1951 .tpg_get_pr_transport_id
= scsiback_get_pr_transport_id
,
1952 .tpg_get_pr_transport_id_len
= scsiback_get_pr_transport_id_len
,
1953 .tpg_parse_pr_out_transport_id
= scsiback_parse_pr_out_transport_id
,
1954 .tpg_check_demo_mode
= scsiback_check_true
,
1955 .tpg_check_demo_mode_cache
= scsiback_check_true
,
1956 .tpg_check_demo_mode_write_protect
= scsiback_check_false
,
1957 .tpg_check_prod_mode_write_protect
= scsiback_check_false
,
1958 .tpg_alloc_fabric_acl
= scsiback_alloc_fabric_acl
,
1959 .tpg_release_fabric_acl
= scsiback_release_fabric_acl
,
1960 .tpg_get_inst_index
= scsiback_tpg_get_inst_index
,
1961 .check_stop_free
= scsiback_check_stop_free
,
1962 .release_cmd
= scsiback_release_cmd
,
1963 .put_session
= NULL
,
1964 .shutdown_session
= scsiback_shutdown_session
,
1965 .close_session
= scsiback_close_session
,
1966 .sess_get_index
= scsiback_sess_get_index
,
1967 .sess_get_initiator_sid
= NULL
,
1968 .write_pending
= scsiback_write_pending
,
1969 .write_pending_status
= scsiback_write_pending_status
,
1970 .set_default_node_attributes
= scsiback_set_default_node_attrs
,
1971 .get_task_tag
= scsiback_get_task_tag
,
1972 .get_cmd_state
= scsiback_get_cmd_state
,
1973 .queue_data_in
= scsiback_queue_data_in
,
1974 .queue_status
= scsiback_queue_status
,
1975 .queue_tm_rsp
= scsiback_queue_tm_rsp
,
1976 .aborted_task
= scsiback_aborted_task
,
1978 * Setup callers for generic logic in target_core_fabric_configfs.c
1980 .fabric_make_wwn
= scsiback_make_tport
,
1981 .fabric_drop_wwn
= scsiback_drop_tport
,
1982 .fabric_make_tpg
= scsiback_make_tpg
,
1983 .fabric_drop_tpg
= scsiback_drop_tpg
,
1984 .fabric_post_link
= scsiback_port_link
,
1985 .fabric_pre_unlink
= scsiback_port_unlink
,
1986 .fabric_make_np
= NULL
,
1987 .fabric_drop_np
= NULL
,
1989 .fabric_make_nodeacl
= scsiback_make_nodeacl
,
1990 .fabric_drop_nodeacl
= scsiback_drop_nodeacl
,
1994 static int scsiback_register_configfs(void)
1996 struct target_fabric_configfs
*fabric
;
1999 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE
"\n",
2000 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
2002 * Register the top level struct config_item_type with TCM core
2004 fabric
= target_fabric_configfs_init(THIS_MODULE
, "xen-pvscsi");
2006 return PTR_ERR(fabric
);
2009 * Setup fabric->tf_ops from our local scsiback_ops
2011 fabric
->tf_ops
= scsiback_ops
;
2013 * Setup default attribute lists for various fabric->tf_cit_tmpl
2015 fabric
->tf_cit_tmpl
.tfc_wwn_cit
.ct_attrs
= scsiback_wwn_attrs
;
2016 fabric
->tf_cit_tmpl
.tfc_tpg_base_cit
.ct_attrs
= scsiback_tpg_attrs
;
2017 fabric
->tf_cit_tmpl
.tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
2018 fabric
->tf_cit_tmpl
.tfc_tpg_param_cit
.ct_attrs
= scsiback_param_attrs
;
2019 fabric
->tf_cit_tmpl
.tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
2020 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
2021 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
2022 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
2023 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
2025 * Register the fabric for use within TCM
2027 ret
= target_fabric_configfs_register(fabric
);
2029 target_fabric_configfs_free(fabric
);
2033 * Setup our local pointer to *fabric
2035 scsiback_fabric_configfs
= fabric
;
2036 pr_debug("xen-pvscsi: Set fabric -> scsiback_fabric_configfs\n");
2040 static void scsiback_deregister_configfs(void)
2042 if (!scsiback_fabric_configfs
)
2045 target_fabric_configfs_deregister(scsiback_fabric_configfs
);
2046 scsiback_fabric_configfs
= NULL
;
2047 pr_debug("xen-pvscsi: Cleared scsiback_fabric_configfs\n");
2050 static const struct xenbus_device_id scsiback_ids
[] = {
2055 static struct xenbus_driver scsiback_driver
= {
2056 .ids
= scsiback_ids
,
2057 .probe
= scsiback_probe
,
2058 .remove
= scsiback_remove
,
2059 .otherend_changed
= scsiback_frontend_changed
2062 static void scsiback_init_pend(void *p
)
2064 struct vscsibk_pend
*pend
= p
;
2067 memset(pend
, 0, sizeof(*pend
));
2068 for (i
= 0; i
< VSCSI_MAX_GRANTS
; i
++)
2069 pend
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
2072 static int __init
scsiback_init(void)
2079 scsiback_cachep
= kmem_cache_create("vscsiif_cache",
2080 sizeof(struct vscsibk_pend
), 0, 0, scsiback_init_pend
);
2081 if (!scsiback_cachep
)
2084 ret
= xenbus_register_backend(&scsiback_driver
);
2086 goto out_cache_destroy
;
2088 ret
= scsiback_register_configfs();
2090 goto out_unregister_xenbus
;
2094 out_unregister_xenbus
:
2095 xenbus_unregister_driver(&scsiback_driver
);
2097 kmem_cache_destroy(scsiback_cachep
);
2098 pr_err("xen-pvscsi: %s: error %d\n", __func__
, ret
);
2102 static void __exit
scsiback_exit(void)
2106 while (free_pages_num
) {
2107 if (get_free_page(&page
))
2109 free_xenballooned_pages(1, &page
);
2111 scsiback_deregister_configfs();
2112 xenbus_unregister_driver(&scsiback_driver
);
2113 kmem_cache_destroy(scsiback_cachep
);
2116 module_init(scsiback_init
);
2117 module_exit(scsiback_exit
);
2119 MODULE_DESCRIPTION("Xen SCSI backend driver");
2120 MODULE_LICENSE("Dual BSD/GPL");
2121 MODULE_ALIAS("xen-backend:vscsi");
2122 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");