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
34 #define pr_fmt(fmt) "xen-pvscsi: " fmt
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_host.h> /* SG_ALL */
52 #include <target/target_core_base.h>
53 #include <target/target_core_fabric.h>
55 #include <asm/hypervisor.h>
58 #include <xen/balloon.h>
59 #include <xen/events.h>
60 #include <xen/xenbus.h>
61 #include <xen/grant_table.h>
64 #include <xen/interface/grant_table.h>
65 #include <xen/interface/io/vscsiif.h>
67 #define VSCSI_VERSION "v0.1"
68 #define VSCSI_NAMELEN 32
71 unsigned int hst
; /* host */
72 unsigned int chn
; /* channel */
73 unsigned int tgt
; /* target */
74 unsigned int lun
; /* LUN */
78 struct ids_tuple v
; /* translate from */
79 struct scsiback_tpg
*tpg
; /* translate to */
86 struct xenbus_device
*dev
;
91 struct vscsiif_back_ring ring
;
94 atomic_t nr_unreplied_reqs
;
97 struct list_head v2p_entry_lists
;
99 wait_queue_head_t waiting_to_free
;
101 struct gnttab_page_cache free_pages
;
104 /* theoretical maximum of grants for one request */
105 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
108 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
109 * call to map/unmap grants. Don't choose it too large, as there are arrays
110 * with VSCSI_GRANT_BATCH elements allocated on the stack.
112 #define VSCSI_GRANT_BATCH 16
114 struct vscsibk_pend
{
117 uint8_t cmnd
[VSCSIIF_MAX_COMMAND_SIZE
];
120 uint8_t sc_data_direction
;
121 uint16_t n_sg
; /* real length of SG list */
122 uint16_t n_grants
; /* SG pages and potentially SG list */
126 struct vscsibk_info
*info
;
127 struct v2p_entry
*v2p
;
128 struct scatterlist
*sgl
;
130 uint8_t sense_buffer
[VSCSIIF_SENSE_BUFFERSIZE
];
132 grant_handle_t grant_handles
[VSCSI_MAX_GRANTS
];
133 struct page
*pages
[VSCSI_MAX_GRANTS
];
135 struct se_cmd se_cmd
;
137 struct completion tmr_done
;
140 #define VSCSI_DEFAULT_SESSION_TAGS 128
142 struct scsiback_nexus
{
143 /* Pointer to TCM session for I_T Nexus */
144 struct se_session
*tvn_se_sess
;
147 struct scsiback_tport
{
148 /* SCSI protocol the tport is providing */
150 /* Binary World Wide unique Port Name for pvscsi Target port */
152 /* ASCII formatted WWPN for pvscsi Target port */
153 char tport_name
[VSCSI_NAMELEN
];
154 /* Returned by scsiback_make_tport() */
155 struct se_wwn tport_wwn
;
158 struct scsiback_tpg
{
159 /* scsiback port target portal group tag for TCM */
161 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
162 int tv_tpg_port_count
;
163 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
165 /* list for scsiback_list */
166 struct list_head tv_tpg_list
;
167 /* Used to protect access for tpg_nexus */
168 struct mutex tv_tpg_mutex
;
169 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
170 struct scsiback_nexus
*tpg_nexus
;
171 /* Pointer back to scsiback_tport */
172 struct scsiback_tport
*tport
;
173 /* Returned by scsiback_make_tpg() */
174 struct se_portal_group se_tpg
;
175 /* alias used in xenstore */
176 char param_alias
[VSCSI_NAMELEN
];
177 /* list of info structures related to this target portal group */
178 struct list_head info_list
;
181 #define SCSIBACK_INVALID_HANDLE (~0)
183 static bool log_print_stat
;
184 module_param(log_print_stat
, bool, 0644);
186 static int scsiback_max_buffer_pages
= 1024;
187 module_param_named(max_buffer_pages
, scsiback_max_buffer_pages
, int, 0644);
188 MODULE_PARM_DESC(max_buffer_pages
,
189 "Maximum number of free pages to keep in backend buffer");
191 /* Global spinlock to protect scsiback TPG list */
192 static DEFINE_MUTEX(scsiback_mutex
);
193 static LIST_HEAD(scsiback_list
);
195 static void scsiback_get(struct vscsibk_info
*info
)
197 atomic_inc(&info
->nr_unreplied_reqs
);
200 static void scsiback_put(struct vscsibk_info
*info
)
202 if (atomic_dec_and_test(&info
->nr_unreplied_reqs
))
203 wake_up(&info
->waiting_to_free
);
206 static unsigned long vaddr_page(struct page
*page
)
208 unsigned long pfn
= page_to_pfn(page
);
210 return (unsigned long)pfn_to_kaddr(pfn
);
213 static unsigned long vaddr(struct vscsibk_pend
*req
, int seg
)
215 return vaddr_page(req
->pages
[seg
]);
218 static void scsiback_print_status(char *sense_buffer
, int errors
,
219 struct vscsibk_pend
*pending_req
)
221 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
223 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x\n",
224 tpg
->tport
->tport_name
, pending_req
->v2p
->lun
,
225 pending_req
->cmnd
[0], errors
& 0xff, COMMAND_COMPLETE
,
229 static void scsiback_fast_flush_area(struct vscsibk_pend
*req
)
231 struct gnttab_unmap_grant_ref unmap
[VSCSI_GRANT_BATCH
];
232 struct page
*pages
[VSCSI_GRANT_BATCH
];
233 unsigned int i
, invcount
= 0;
234 grant_handle_t handle
;
244 for (i
= 0; i
< req
->n_grants
; i
++) {
245 handle
= req
->grant_handles
[i
];
246 if (handle
== SCSIBACK_INVALID_HANDLE
)
248 gnttab_set_unmap_op(&unmap
[invcount
], vaddr(req
, i
),
249 GNTMAP_host_map
, handle
);
250 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
251 pages
[invcount
] = req
->pages
[i
];
252 put_page(pages
[invcount
]);
254 if (invcount
< VSCSI_GRANT_BATCH
)
256 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
262 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
266 gnttab_page_cache_put(&req
->info
->free_pages
, req
->pages
,
271 static void scsiback_free_translation_entry(struct kref
*kref
)
273 struct v2p_entry
*entry
= container_of(kref
, struct v2p_entry
, kref
);
274 struct scsiback_tpg
*tpg
= entry
->tpg
;
276 mutex_lock(&tpg
->tv_tpg_mutex
);
277 tpg
->tv_tpg_fe_count
--;
278 mutex_unlock(&tpg
->tv_tpg_mutex
);
283 static int32_t scsiback_result(int32_t result
)
287 switch (XEN_VSCSIIF_RSLT_HOST(result
)) {
289 host_status
= XEN_VSCSIIF_RSLT_HOST_OK
;
292 host_status
= XEN_VSCSIIF_RSLT_HOST_NO_CONNECT
;
295 host_status
= XEN_VSCSIIF_RSLT_HOST_BUS_BUSY
;
298 host_status
= XEN_VSCSIIF_RSLT_HOST_TIME_OUT
;
301 host_status
= XEN_VSCSIIF_RSLT_HOST_BAD_TARGET
;
304 host_status
= XEN_VSCSIIF_RSLT_HOST_ABORT
;
307 host_status
= XEN_VSCSIIF_RSLT_HOST_PARITY
;
310 host_status
= XEN_VSCSIIF_RSLT_HOST_ERROR
;
313 host_status
= XEN_VSCSIIF_RSLT_HOST_RESET
;
316 host_status
= XEN_VSCSIIF_RSLT_HOST_BAD_INTR
;
318 case DID_PASSTHROUGH
:
319 host_status
= XEN_VSCSIIF_RSLT_HOST_PASSTHROUGH
;
322 host_status
= XEN_VSCSIIF_RSLT_HOST_SOFT_ERROR
;
325 host_status
= XEN_VSCSIIF_RSLT_HOST_IMM_RETRY
;
328 host_status
= XEN_VSCSIIF_RSLT_HOST_REQUEUE
;
330 case DID_TRANSPORT_DISRUPTED
:
331 host_status
= XEN_VSCSIIF_RSLT_HOST_TRANSPORT_DISRUPTED
;
333 case DID_TRANSPORT_FAILFAST
:
334 host_status
= XEN_VSCSIIF_RSLT_HOST_TRANSPORT_FAILFAST
;
336 case DID_TRANSPORT_MARGINAL
:
337 host_status
= XEN_VSCSIIF_RSLT_HOST_TRANSPORT_MARGINAL
;
340 host_status
= XEN_VSCSIIF_RSLT_HOST_ERROR
;
344 return (host_status
<< 16) | (result
& 0x00ffff);
347 static void scsiback_send_response(struct vscsibk_info
*info
,
348 char *sense_buffer
, int32_t result
, uint32_t resid
,
351 struct vscsiif_response
*ring_res
;
353 struct scsi_sense_hdr sshdr
;
357 spin_lock_irqsave(&info
->ring_lock
, flags
);
359 ring_res
= RING_GET_RESPONSE(&info
->ring
, info
->ring
.rsp_prod_pvt
);
360 info
->ring
.rsp_prod_pvt
++;
362 ring_res
->rslt
= scsiback_result(result
);
363 ring_res
->rqid
= rqid
;
365 if (sense_buffer
!= NULL
&&
366 scsi_normalize_sense(sense_buffer
, VSCSIIF_SENSE_BUFFERSIZE
,
368 len
= min_t(unsigned, 8 + sense_buffer
[7],
369 VSCSIIF_SENSE_BUFFERSIZE
);
370 memcpy(ring_res
->sense_buffer
, sense_buffer
, len
);
371 ring_res
->sense_len
= len
;
373 ring_res
->sense_len
= 0;
376 ring_res
->residual_len
= resid
;
378 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info
->ring
, notify
);
379 spin_unlock_irqrestore(&info
->ring_lock
, flags
);
382 notify_remote_via_irq(info
->irq
);
385 static void scsiback_do_resp_with_sense(char *sense_buffer
, int32_t result
,
386 uint32_t resid
, struct vscsibk_pend
*pending_req
)
388 scsiback_send_response(pending_req
->info
, sense_buffer
, result
,
389 resid
, pending_req
->rqid
);
391 if (pending_req
->v2p
)
392 kref_put(&pending_req
->v2p
->kref
,
393 scsiback_free_translation_entry
);
396 static void scsiback_cmd_done(struct vscsibk_pend
*pending_req
)
398 struct vscsibk_info
*info
= pending_req
->info
;
399 unsigned char *sense_buffer
;
403 sense_buffer
= pending_req
->sense_buffer
;
404 resid
= pending_req
->se_cmd
.residual_count
;
405 errors
= pending_req
->result
;
407 if (errors
&& log_print_stat
)
408 scsiback_print_status(sense_buffer
, errors
, pending_req
);
410 scsiback_fast_flush_area(pending_req
);
411 scsiback_do_resp_with_sense(sense_buffer
, errors
, resid
, pending_req
);
414 * Drop the extra KREF_ACK reference taken by target_submit_cmd_map_sgls()
415 * ahead of scsiback_check_stop_free() -> transport_generic_free_cmd()
416 * final se_cmd->cmd_kref put.
418 target_put_sess_cmd(&pending_req
->se_cmd
);
421 static void scsiback_cmd_exec(struct vscsibk_pend
*pending_req
)
423 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
424 struct se_session
*sess
= pending_req
->v2p
->tpg
->tpg_nexus
->tvn_se_sess
;
426 scsiback_get(pending_req
->info
);
427 se_cmd
->tag
= pending_req
->rqid
;
428 target_init_cmd(se_cmd
, sess
, pending_req
->sense_buffer
,
429 pending_req
->v2p
->lun
, pending_req
->data_len
, 0,
430 pending_req
->sc_data_direction
, TARGET_SCF_ACK_KREF
);
432 if (target_submit_prep(se_cmd
, pending_req
->cmnd
, pending_req
->sgl
,
433 pending_req
->n_sg
, NULL
, 0, NULL
, 0, GFP_KERNEL
))
436 target_submit(se_cmd
);
439 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref
*map
,
440 struct page
**pg
, grant_handle_t
*grant
, int cnt
)
447 err
= gnttab_map_refs(map
, NULL
, pg
, cnt
);
448 for (i
= 0; i
< cnt
; i
++) {
449 if (unlikely(map
[i
].status
!= GNTST_okay
)) {
450 pr_err("invalid buffer -- could not remap it\n");
451 map
[i
].handle
= SCSIBACK_INVALID_HANDLE
;
457 grant
[i
] = map
[i
].handle
;
462 static int scsiback_gnttab_data_map_list(struct vscsibk_pend
*pending_req
,
463 struct scsiif_request_segment
*seg
, struct page
**pg
,
464 grant_handle_t
*grant
, int cnt
, u32 flags
)
466 int mapcount
= 0, i
, err
= 0;
467 struct gnttab_map_grant_ref map
[VSCSI_GRANT_BATCH
];
468 struct vscsibk_info
*info
= pending_req
->info
;
470 for (i
= 0; i
< cnt
; i
++) {
471 if (gnttab_page_cache_get(&info
->free_pages
, pg
+ mapcount
)) {
472 gnttab_page_cache_put(&info
->free_pages
, pg
, mapcount
);
473 pr_err("no grant page\n");
476 gnttab_set_map_op(&map
[mapcount
], vaddr_page(pg
[mapcount
]),
477 flags
, seg
[i
].gref
, info
->domid
);
479 if (mapcount
< VSCSI_GRANT_BATCH
)
481 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
484 pending_req
->n_grants
+= mapcount
;
489 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
490 pending_req
->n_grants
+= mapcount
;
494 static int scsiback_gnttab_data_map(struct vscsiif_request
*ring_req
,
495 struct vscsibk_pend
*pending_req
)
498 int i
, err
, n_segs
, i_seg
= 0;
500 struct scsiif_request_segment
*seg
;
501 unsigned long end_seg
= 0;
502 unsigned int nr_segments
= (unsigned int)ring_req
->nr_segments
;
503 unsigned int nr_sgl
= 0;
504 struct scatterlist
*sg
;
505 grant_handle_t
*grant
;
507 pending_req
->n_sg
= 0;
508 pending_req
->n_grants
= 0;
509 pending_req
->data_len
= 0;
511 nr_segments
&= ~VSCSIIF_SG_GRANT
;
515 if (nr_segments
> VSCSIIF_SG_TABLESIZE
) {
516 pr_debug("invalid parameter nr_seg = %d\n",
517 ring_req
->nr_segments
);
521 if (ring_req
->nr_segments
& VSCSIIF_SG_GRANT
) {
522 err
= scsiback_gnttab_data_map_list(pending_req
, ring_req
->seg
,
523 pending_req
->pages
, pending_req
->grant_handles
,
524 nr_segments
, GNTMAP_host_map
| GNTMAP_readonly
);
527 nr_sgl
= nr_segments
;
529 for (i
= 0; i
< nr_sgl
; i
++) {
530 n_segs
= ring_req
->seg
[i
].length
/
531 sizeof(struct scsiif_request_segment
);
532 if ((unsigned)ring_req
->seg
[i
].offset
+
533 (unsigned)ring_req
->seg
[i
].length
> PAGE_SIZE
||
534 n_segs
* sizeof(struct scsiif_request_segment
) !=
535 ring_req
->seg
[i
].length
)
537 nr_segments
+= n_segs
;
539 if (nr_segments
> SG_ALL
) {
540 pr_debug("invalid nr_seg = %d\n", nr_segments
);
545 /* free of (sgl) in fast_flush_area() */
546 pending_req
->sgl
= kmalloc_array(nr_segments
,
547 sizeof(struct scatterlist
), GFP_KERNEL
);
548 if (!pending_req
->sgl
)
551 sg_init_table(pending_req
->sgl
, nr_segments
);
552 pending_req
->n_sg
= nr_segments
;
554 flags
= GNTMAP_host_map
;
555 if (pending_req
->sc_data_direction
== DMA_TO_DEVICE
)
556 flags
|= GNTMAP_readonly
;
558 pg
= pending_req
->pages
+ nr_sgl
;
559 grant
= pending_req
->grant_handles
+ nr_sgl
;
562 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
563 pg
, grant
, nr_segments
, flags
);
567 for (i
= 0; i
< nr_sgl
; i
++) {
568 seg
= (struct scsiif_request_segment
*)(
569 vaddr(pending_req
, i
) + ring_req
->seg
[i
].offset
);
570 n_segs
= ring_req
->seg
[i
].length
/
571 sizeof(struct scsiif_request_segment
);
572 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
573 pg
, grant
, n_segs
, flags
);
579 end_seg
= vaddr(pending_req
, 0) + ring_req
->seg
[0].offset
;
580 seg
= (struct scsiif_request_segment
*)end_seg
;
581 end_seg
+= ring_req
->seg
[0].length
;
582 pg
= pending_req
->pages
+ nr_sgl
;
585 for_each_sg(pending_req
->sgl
, sg
, nr_segments
, i
) {
586 sg_set_page(sg
, pg
[i
], seg
->length
, seg
->offset
);
587 pending_req
->data_len
+= seg
->length
;
589 if (nr_sgl
&& (unsigned long)seg
>= end_seg
) {
591 end_seg
= vaddr(pending_req
, i_seg
) +
592 ring_req
->seg
[i_seg
].offset
;
593 seg
= (struct scsiif_request_segment
*)end_seg
;
594 end_seg
+= ring_req
->seg
[i_seg
].length
;
596 if (sg
->offset
>= PAGE_SIZE
||
597 sg
->length
> PAGE_SIZE
||
598 sg
->offset
+ sg
->length
> PAGE_SIZE
)
605 static void scsiback_disconnect(struct vscsibk_info
*info
)
607 wait_event(info
->waiting_to_free
,
608 atomic_read(&info
->nr_unreplied_reqs
) == 0);
610 unbind_from_irqhandler(info
->irq
, info
);
612 xenbus_unmap_ring_vfree(info
->dev
, info
->ring
.sring
);
615 static void scsiback_device_action(struct vscsibk_pend
*pending_req
,
616 enum tcm_tmreq_table act
, int tag
)
618 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
619 struct scsiback_nexus
*nexus
= tpg
->tpg_nexus
;
620 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
621 u64 unpacked_lun
= pending_req
->v2p
->lun
;
622 int rc
, err
= XEN_VSCSIIF_RSLT_RESET_FAILED
;
624 init_completion(&pending_req
->tmr_done
);
626 rc
= target_submit_tmr(&pending_req
->se_cmd
, nexus
->tvn_se_sess
,
627 &pending_req
->sense_buffer
[0],
628 unpacked_lun
, NULL
, act
, GFP_KERNEL
,
629 tag
, TARGET_SCF_ACK_KREF
);
633 wait_for_completion(&pending_req
->tmr_done
);
635 err
= (se_cmd
->se_tmr_req
->response
== TMR_FUNCTION_COMPLETE
) ?
636 XEN_VSCSIIF_RSLT_RESET_SUCCESS
: XEN_VSCSIIF_RSLT_RESET_FAILED
;
638 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
639 transport_generic_free_cmd(&pending_req
->se_cmd
, 0);
643 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
647 Perform virtual to physical translation
649 static struct v2p_entry
*scsiback_do_translation(struct vscsibk_info
*info
,
652 struct v2p_entry
*entry
;
653 struct list_head
*head
= &(info
->v2p_entry_lists
);
656 spin_lock_irqsave(&info
->v2p_lock
, flags
);
657 list_for_each_entry(entry
, head
, l
) {
658 if ((entry
->v
.chn
== v
->chn
) &&
659 (entry
->v
.tgt
== v
->tgt
) &&
660 (entry
->v
.lun
== v
->lun
)) {
661 kref_get(&entry
->kref
);
668 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
672 static struct vscsibk_pend
*scsiback_get_pend_req(struct vscsiif_back_ring
*ring
,
673 struct v2p_entry
*v2p
)
675 struct scsiback_tpg
*tpg
= v2p
->tpg
;
676 struct scsiback_nexus
*nexus
= tpg
->tpg_nexus
;
677 struct se_session
*se_sess
= nexus
->tvn_se_sess
;
678 struct vscsibk_pend
*req
;
681 tag
= sbitmap_queue_get(&se_sess
->sess_tag_pool
, &cpu
);
683 pr_err("Unable to obtain tag for vscsiif_request\n");
684 return ERR_PTR(-ENOMEM
);
687 req
= &((struct vscsibk_pend
*)se_sess
->sess_cmd_map
)[tag
];
688 memset(req
, 0, sizeof(*req
));
689 req
->se_cmd
.map_tag
= tag
;
690 req
->se_cmd
.map_cpu
= cpu
;
692 for (i
= 0; i
< VSCSI_MAX_GRANTS
; i
++)
693 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
698 static struct vscsibk_pend
*prepare_pending_reqs(struct vscsibk_info
*info
,
699 struct vscsiif_back_ring
*ring
,
700 struct vscsiif_request
*ring_req
)
702 struct vscsibk_pend
*pending_req
;
703 struct v2p_entry
*v2p
;
704 struct ids_tuple vir
;
706 /* request range check from frontend */
707 if ((ring_req
->sc_data_direction
!= DMA_BIDIRECTIONAL
) &&
708 (ring_req
->sc_data_direction
!= DMA_TO_DEVICE
) &&
709 (ring_req
->sc_data_direction
!= DMA_FROM_DEVICE
) &&
710 (ring_req
->sc_data_direction
!= DMA_NONE
)) {
711 pr_debug("invalid parameter data_dir = %d\n",
712 ring_req
->sc_data_direction
);
713 return ERR_PTR(-EINVAL
);
715 if (ring_req
->cmd_len
> VSCSIIF_MAX_COMMAND_SIZE
) {
716 pr_debug("invalid parameter cmd_len = %d\n",
718 return ERR_PTR(-EINVAL
);
721 vir
.chn
= ring_req
->channel
;
722 vir
.tgt
= ring_req
->id
;
723 vir
.lun
= ring_req
->lun
;
725 v2p
= scsiback_do_translation(info
, &vir
);
727 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
728 vir
.chn
, vir
.tgt
, vir
.lun
);
729 return ERR_PTR(-ENODEV
);
732 pending_req
= scsiback_get_pend_req(ring
, v2p
);
733 if (IS_ERR(pending_req
)) {
734 kref_put(&v2p
->kref
, scsiback_free_translation_entry
);
735 return ERR_PTR(-ENOMEM
);
737 pending_req
->rqid
= ring_req
->rqid
;
738 pending_req
->info
= info
;
739 pending_req
->v2p
= v2p
;
740 pending_req
->sc_data_direction
= ring_req
->sc_data_direction
;
741 pending_req
->cmd_len
= ring_req
->cmd_len
;
742 memcpy(pending_req
->cmnd
, ring_req
->cmnd
, pending_req
->cmd_len
);
747 static int scsiback_do_cmd_fn(struct vscsibk_info
*info
,
748 unsigned int *eoi_flags
)
750 struct vscsiif_back_ring
*ring
= &info
->ring
;
751 struct vscsiif_request ring_req
;
752 struct vscsibk_pend
*pending_req
;
758 rp
= ring
->sring
->req_prod
;
759 rmb(); /* guest system is accessing ring, too */
761 if (RING_REQUEST_PROD_OVERFLOW(ring
, rp
)) {
762 rc
= ring
->rsp_prod_pvt
;
763 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
764 info
->domid
, rp
, rc
, rp
- rc
);
769 *eoi_flags
&= ~XEN_EOI_FLAG_SPURIOUS
;
771 if (RING_REQUEST_CONS_OVERFLOW(ring
, rc
))
774 RING_COPY_REQUEST(ring
, rc
, &ring_req
);
775 ring
->req_cons
= ++rc
;
777 pending_req
= prepare_pending_reqs(info
, ring
, &ring_req
);
778 if (IS_ERR(pending_req
)) {
779 switch (PTR_ERR(pending_req
)) {
781 result
= DID_NO_CONNECT
;
787 scsiback_send_response(info
, NULL
, result
<< 16, 0,
792 switch (ring_req
.act
) {
793 case VSCSIIF_ACT_SCSI_CDB
:
794 if (scsiback_gnttab_data_map(&ring_req
, pending_req
)) {
795 scsiback_fast_flush_area(pending_req
);
796 scsiback_do_resp_with_sense(NULL
,
797 DID_ERROR
<< 16, 0, pending_req
);
798 transport_generic_free_cmd(&pending_req
->se_cmd
, 0);
800 scsiback_cmd_exec(pending_req
);
803 case VSCSIIF_ACT_SCSI_ABORT
:
804 scsiback_device_action(pending_req
, TMR_ABORT_TASK
,
807 case VSCSIIF_ACT_SCSI_RESET
:
808 scsiback_device_action(pending_req
, TMR_LUN_RESET
, 0);
811 pr_err_ratelimited("invalid request\n");
812 scsiback_do_resp_with_sense(NULL
, DID_ERROR
<< 16, 0,
814 transport_generic_free_cmd(&pending_req
->se_cmd
, 0);
818 /* Yield point for this unbounded loop. */
822 gnttab_page_cache_shrink(&info
->free_pages
, scsiback_max_buffer_pages
);
824 RING_FINAL_CHECK_FOR_REQUESTS(&info
->ring
, more_to_do
);
828 static irqreturn_t
scsiback_irq_fn(int irq
, void *dev_id
)
830 struct vscsibk_info
*info
= dev_id
;
832 unsigned int eoi_flags
= XEN_EOI_FLAG_SPURIOUS
;
834 while ((rc
= scsiback_do_cmd_fn(info
, &eoi_flags
)) > 0)
837 /* In case of a ring error we keep the event channel masked. */
839 xen_irq_lateeoi(irq
, eoi_flags
);
844 static int scsiback_init_sring(struct vscsibk_info
*info
, grant_ref_t ring_ref
,
845 evtchn_port_t evtchn
)
848 struct vscsiif_sring
*sring
;
854 err
= xenbus_map_ring_valloc(info
->dev
, &ring_ref
, 1, &area
);
858 sring
= (struct vscsiif_sring
*)area
;
859 BACK_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
861 err
= bind_interdomain_evtchn_to_irq_lateeoi(info
->dev
, evtchn
);
867 err
= request_threaded_irq(info
->irq
, NULL
, scsiback_irq_fn
,
868 IRQF_ONESHOT
, "vscsiif-backend", info
);
875 unbind_from_irqhandler(info
->irq
, info
);
878 xenbus_unmap_ring_vfree(info
->dev
, area
);
883 static int scsiback_map(struct vscsibk_info
*info
)
885 struct xenbus_device
*dev
= info
->dev
;
886 unsigned int ring_ref
;
887 evtchn_port_t evtchn
;
890 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
891 "ring-ref", "%u", &ring_ref
,
892 "event-channel", "%u", &evtchn
, NULL
);
894 xenbus_dev_fatal(dev
, err
, "reading %s ring", dev
->otherend
);
898 return scsiback_init_sring(info
, ring_ref
, evtchn
);
902 Check for a translation entry being present
904 static struct v2p_entry
*scsiback_chk_translation_entry(
905 struct vscsibk_info
*info
, struct ids_tuple
*v
)
907 struct list_head
*head
= &(info
->v2p_entry_lists
);
908 struct v2p_entry
*entry
;
910 list_for_each_entry(entry
, head
, l
)
911 if ((entry
->v
.chn
== v
->chn
) &&
912 (entry
->v
.tgt
== v
->tgt
) &&
913 (entry
->v
.lun
== v
->lun
))
920 Add a new translation entry
922 static int scsiback_add_translation_entry(struct vscsibk_info
*info
,
923 char *phy
, struct ids_tuple
*v
)
926 struct v2p_entry
*new;
929 unsigned long long unpacked_lun
;
930 struct se_lun
*se_lun
;
931 struct scsiback_tpg
*tpg_entry
, *tpg
= NULL
;
932 char *error
= "doesn't exist";
934 lunp
= strrchr(phy
, ':');
936 pr_err("illegal format of physical device %s\n", phy
);
941 err
= kstrtoull(lunp
, 10, &unpacked_lun
);
943 pr_err("lun number not valid: %s\n", lunp
);
947 mutex_lock(&scsiback_mutex
);
948 list_for_each_entry(tpg_entry
, &scsiback_list
, tv_tpg_list
) {
949 if (!strcmp(phy
, tpg_entry
->tport
->tport_name
) ||
950 !strcmp(phy
, tpg_entry
->param_alias
)) {
951 mutex_lock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
952 hlist_for_each_entry(se_lun
, &tpg_entry
->se_tpg
.tpg_lun_hlist
, link
) {
953 if (se_lun
->unpacked_lun
== unpacked_lun
) {
954 if (!tpg_entry
->tpg_nexus
)
955 error
= "nexus undefined";
961 mutex_unlock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
966 mutex_lock(&tpg
->tv_tpg_mutex
);
967 tpg
->tv_tpg_fe_count
++;
968 mutex_unlock(&tpg
->tv_tpg_mutex
);
970 mutex_unlock(&scsiback_mutex
);
973 pr_err("%s:%llu %s\n", phy
, unpacked_lun
, error
);
977 new = kmalloc(sizeof(struct v2p_entry
), GFP_KERNEL
);
983 spin_lock_irqsave(&info
->v2p_lock
, flags
);
985 /* Check double assignment to identical virtual ID */
986 if (scsiback_chk_translation_entry(info
, v
)) {
987 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
992 /* Create a new translation entry and add to the list */
993 kref_init(&new->kref
);
996 new->lun
= unpacked_lun
;
997 list_add_tail(&new->l
, &info
->v2p_entry_lists
);
1000 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1004 mutex_lock(&tpg
->tv_tpg_mutex
);
1005 tpg
->tv_tpg_fe_count
--;
1006 mutex_unlock(&tpg
->tv_tpg_mutex
);
1014 Delete the translation entry specified
1016 static int scsiback_del_translation_entry(struct vscsibk_info
*info
,
1017 struct ids_tuple
*v
)
1019 struct v2p_entry
*entry
;
1020 unsigned long flags
;
1022 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1023 /* Find out the translation entry specified */
1024 entry
= scsiback_chk_translation_entry(info
, v
);
1026 list_del(&entry
->l
);
1028 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1033 kref_put(&entry
->kref
, scsiback_free_translation_entry
);
1037 static void scsiback_do_add_lun(struct vscsibk_info
*info
, const char *state
,
1038 char *phy
, struct ids_tuple
*vir
, int try)
1040 struct v2p_entry
*entry
;
1041 unsigned long flags
;
1045 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1046 entry
= scsiback_chk_translation_entry(info
, vir
);
1047 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1051 if (!scsiback_add_translation_entry(info
, phy
, vir
)) {
1052 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1053 "%d", XenbusStateInitialised
)) {
1054 pr_err("xenbus_printf error %s\n", state
);
1055 scsiback_del_translation_entry(info
, vir
);
1058 err
= xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1059 "%d", XenbusStateClosed
);
1061 xenbus_dev_error(info
->dev
, err
,
1062 "%s: writing %s", __func__
, state
);
1066 static void scsiback_do_del_lun(struct vscsibk_info
*info
, const char *state
,
1067 struct ids_tuple
*vir
)
1069 if (!scsiback_del_translation_entry(info
, vir
)) {
1070 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1071 "%d", XenbusStateClosed
))
1072 pr_err("xenbus_printf error %s\n", state
);
1076 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1077 #define VSCSIBACK_OP_UPDATEDEV_STATE 2
1079 static void scsiback_do_1lun_hotplug(struct vscsibk_info
*info
, int op
,
1083 struct ids_tuple vir
;
1086 char phy
[VSCSI_NAMELEN
];
1089 struct xenbus_device
*dev
= info
->dev
;
1092 snprintf(state
, sizeof(state
), "vscsi-devs/%s/state", ent
);
1093 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, state
, "%u", &device_state
);
1094 if (XENBUS_EXIST_ERR(err
))
1097 /* physical SCSI device */
1098 snprintf(str
, sizeof(str
), "vscsi-devs/%s/p-dev", ent
);
1099 val
= xenbus_read(XBT_NIL
, dev
->nodename
, str
, NULL
);
1101 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1102 "%d", XenbusStateClosed
);
1104 xenbus_dev_error(info
->dev
, err
,
1105 "%s: writing %s", __func__
, state
);
1108 strscpy(phy
, val
, VSCSI_NAMELEN
);
1111 /* virtual SCSI device */
1112 snprintf(str
, sizeof(str
), "vscsi-devs/%s/v-dev", ent
);
1113 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, str
, "%u:%u:%u:%u",
1114 &vir
.hst
, &vir
.chn
, &vir
.tgt
, &vir
.lun
);
1115 if (XENBUS_EXIST_ERR(err
)) {
1116 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1117 "%d", XenbusStateClosed
);
1119 xenbus_dev_error(info
->dev
, err
,
1120 "%s: writing %s", __func__
, state
);
1125 case VSCSIBACK_OP_ADD_OR_DEL_LUN
:
1126 switch (device_state
) {
1127 case XenbusStateInitialising
:
1128 scsiback_do_add_lun(info
, state
, phy
, &vir
, 0);
1130 case XenbusStateConnected
:
1131 scsiback_do_add_lun(info
, state
, phy
, &vir
, 1);
1133 case XenbusStateClosing
:
1134 scsiback_do_del_lun(info
, state
, &vir
);
1141 case VSCSIBACK_OP_UPDATEDEV_STATE
:
1142 if (device_state
== XenbusStateInitialised
) {
1143 /* modify vscsi-devs/dev-x/state */
1144 if (xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1145 "%d", XenbusStateConnected
)) {
1146 pr_err("xenbus_printf error %s\n", str
);
1147 scsiback_del_translation_entry(info
, &vir
);
1148 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1149 "%d", XenbusStateClosed
);
1153 /* When it is necessary, processing is added here. */
1159 static void scsiback_do_lun_hotplug(struct vscsibk_info
*info
, int op
)
1163 unsigned int ndir
= 0;
1165 dir
= xenbus_directory(XBT_NIL
, info
->dev
->nodename
, "vscsi-devs",
1170 for (i
= 0; i
< ndir
; i
++)
1171 scsiback_do_1lun_hotplug(info
, op
, dir
[i
]);
1176 static void scsiback_frontend_changed(struct xenbus_device
*dev
,
1177 enum xenbus_state frontend_state
)
1179 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1181 switch (frontend_state
) {
1182 case XenbusStateInitialising
:
1185 case XenbusStateInitialised
:
1186 if (scsiback_map(info
))
1189 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1190 xenbus_switch_state(dev
, XenbusStateConnected
);
1193 case XenbusStateConnected
:
1194 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_UPDATEDEV_STATE
);
1196 if (dev
->state
== XenbusStateConnected
)
1199 xenbus_switch_state(dev
, XenbusStateConnected
);
1202 case XenbusStateClosing
:
1204 scsiback_disconnect(info
);
1206 xenbus_switch_state(dev
, XenbusStateClosing
);
1209 case XenbusStateClosed
:
1210 xenbus_switch_state(dev
, XenbusStateClosed
);
1211 if (xenbus_dev_is_online(dev
))
1213 fallthrough
; /* if not online */
1214 case XenbusStateUnknown
:
1215 device_unregister(&dev
->dev
);
1218 case XenbusStateReconfiguring
:
1219 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1220 xenbus_switch_state(dev
, XenbusStateReconfigured
);
1225 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
1232 Release the translation entry specfied
1234 static void scsiback_release_translation_entry(struct vscsibk_info
*info
)
1236 struct v2p_entry
*entry
, *tmp
;
1237 struct list_head
*head
= &(info
->v2p_entry_lists
);
1238 struct list_head tmp_list
;
1239 unsigned long flags
;
1241 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1243 list_cut_before(&tmp_list
, head
, head
);
1245 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1247 list_for_each_entry_safe(entry
, tmp
, &tmp_list
, l
) {
1248 list_del(&entry
->l
);
1249 kref_put(&entry
->kref
, scsiback_free_translation_entry
);
1253 static void scsiback_remove(struct xenbus_device
*dev
)
1255 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1258 scsiback_disconnect(info
);
1260 scsiback_release_translation_entry(info
);
1262 gnttab_page_cache_shrink(&info
->free_pages
, 0);
1264 dev_set_drvdata(&dev
->dev
, NULL
);
1267 static int scsiback_probe(struct xenbus_device
*dev
,
1268 const struct xenbus_device_id
*id
)
1272 struct vscsibk_info
*info
= kzalloc(sizeof(struct vscsibk_info
),
1275 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
1278 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating backend structure");
1282 dev_set_drvdata(&dev
->dev
, info
);
1284 info
->domid
= dev
->otherend_id
;
1285 spin_lock_init(&info
->ring_lock
);
1286 atomic_set(&info
->nr_unreplied_reqs
, 0);
1287 init_waitqueue_head(&info
->waiting_to_free
);
1290 INIT_LIST_HEAD(&info
->v2p_entry_lists
);
1291 spin_lock_init(&info
->v2p_lock
);
1292 gnttab_page_cache_init(&info
->free_pages
);
1294 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, "feature-sg-grant", "%u",
1297 xenbus_dev_error(dev
, err
, "writing feature-sg-grant");
1299 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
1306 pr_warn("%s failed\n", __func__
);
1307 scsiback_remove(dev
);
1312 static char *scsiback_dump_proto_id(struct scsiback_tport
*tport
)
1314 switch (tport
->tport_proto_id
) {
1315 case SCSI_PROTOCOL_SAS
:
1317 case SCSI_PROTOCOL_FCP
:
1319 case SCSI_PROTOCOL_ISCSI
:
1328 static char *scsiback_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1330 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1331 struct scsiback_tpg
, se_tpg
);
1332 struct scsiback_tport
*tport
= tpg
->tport
;
1334 return &tport
->tport_name
[0];
1337 static u16
scsiback_get_tag(struct se_portal_group
*se_tpg
)
1339 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1340 struct scsiback_tpg
, se_tpg
);
1341 return tpg
->tport_tpgt
;
1344 static struct se_wwn
*
1345 scsiback_make_tport(struct target_fabric_configfs
*tf
,
1346 struct config_group
*group
,
1349 struct scsiback_tport
*tport
;
1354 tport
= kzalloc(sizeof(struct scsiback_tport
), GFP_KERNEL
);
1356 return ERR_PTR(-ENOMEM
);
1358 tport
->tport_wwpn
= wwpn
;
1360 * Determine the emulated Protocol Identifier and Target Port Name
1361 * based on the incoming configfs directory name.
1363 ptr
= strstr(name
, "naa.");
1365 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1368 ptr
= strstr(name
, "fc.");
1370 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1371 off
= 3; /* Skip over "fc." */
1374 ptr
= strstr(name
, "iqn.");
1376 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1380 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name
);
1382 return ERR_PTR(-EINVAL
);
1385 if (strlen(name
) >= VSCSI_NAMELEN
) {
1386 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name
,
1387 scsiback_dump_proto_id(tport
), VSCSI_NAMELEN
);
1389 return ERR_PTR(-EINVAL
);
1391 snprintf(&tport
->tport_name
[0], VSCSI_NAMELEN
, "%s", &name
[off
]);
1393 pr_debug("Allocated emulated Target %s Address: %s\n",
1394 scsiback_dump_proto_id(tport
), name
);
1396 return &tport
->tport_wwn
;
1399 static void scsiback_drop_tport(struct se_wwn
*wwn
)
1401 struct scsiback_tport
*tport
= container_of(wwn
,
1402 struct scsiback_tport
, tport_wwn
);
1404 pr_debug("Deallocating emulated Target %s Address: %s\n",
1405 scsiback_dump_proto_id(tport
), tport
->tport_name
);
1410 static int scsiback_check_stop_free(struct se_cmd
*se_cmd
)
1412 return transport_generic_free_cmd(se_cmd
, 0);
1415 static void scsiback_release_cmd(struct se_cmd
*se_cmd
)
1417 target_free_tag(se_cmd
->se_sess
, se_cmd
);
1420 static int scsiback_write_pending(struct se_cmd
*se_cmd
)
1422 /* Go ahead and process the write immediately */
1423 target_execute_cmd(se_cmd
);
1428 static int scsiback_queue_data_in(struct se_cmd
*se_cmd
)
1430 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1431 struct vscsibk_pend
, se_cmd
);
1433 pending_req
->result
= SAM_STAT_GOOD
;
1434 scsiback_cmd_done(pending_req
);
1438 static int scsiback_queue_status(struct se_cmd
*se_cmd
)
1440 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1441 struct vscsibk_pend
, se_cmd
);
1443 if (se_cmd
->sense_buffer
&&
1444 ((se_cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
1445 (se_cmd
->se_cmd_flags
& SCF_EMULATED_TASK_SENSE
)))
1446 pending_req
->result
= SAM_STAT_CHECK_CONDITION
;
1448 pending_req
->result
= se_cmd
->scsi_status
;
1450 scsiback_cmd_done(pending_req
);
1454 static void scsiback_queue_tm_rsp(struct se_cmd
*se_cmd
)
1456 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1457 struct vscsibk_pend
, se_cmd
);
1459 complete(&pending_req
->tmr_done
);
1462 static void scsiback_aborted_task(struct se_cmd
*se_cmd
)
1466 static ssize_t
scsiback_tpg_param_alias_show(struct config_item
*item
,
1469 struct se_portal_group
*se_tpg
= param_to_tpg(item
);
1470 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1474 mutex_lock(&tpg
->tv_tpg_mutex
);
1475 rb
= snprintf(page
, PAGE_SIZE
, "%s\n", tpg
->param_alias
);
1476 mutex_unlock(&tpg
->tv_tpg_mutex
);
1481 static ssize_t
scsiback_tpg_param_alias_store(struct config_item
*item
,
1482 const char *page
, size_t count
)
1484 struct se_portal_group
*se_tpg
= param_to_tpg(item
);
1485 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1489 if (strlen(page
) >= VSCSI_NAMELEN
) {
1490 pr_err("param alias: %s, exceeds max: %d\n", page
,
1495 mutex_lock(&tpg
->tv_tpg_mutex
);
1496 len
= snprintf(tpg
->param_alias
, VSCSI_NAMELEN
, "%s", page
);
1497 if (tpg
->param_alias
[len
- 1] == '\n')
1498 tpg
->param_alias
[len
- 1] = '\0';
1499 mutex_unlock(&tpg
->tv_tpg_mutex
);
1504 CONFIGFS_ATTR(scsiback_tpg_param_
, alias
);
1506 static struct configfs_attribute
*scsiback_param_attrs
[] = {
1507 &scsiback_tpg_param_attr_alias
,
1511 static int scsiback_alloc_sess_cb(struct se_portal_group
*se_tpg
,
1512 struct se_session
*se_sess
, void *p
)
1514 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1515 struct scsiback_tpg
, se_tpg
);
1521 static int scsiback_make_nexus(struct scsiback_tpg
*tpg
,
1524 struct scsiback_nexus
*tv_nexus
;
1527 mutex_lock(&tpg
->tv_tpg_mutex
);
1528 if (tpg
->tpg_nexus
) {
1529 pr_debug("tpg->tpg_nexus already exists\n");
1534 tv_nexus
= kzalloc(sizeof(struct scsiback_nexus
), GFP_KERNEL
);
1540 tv_nexus
->tvn_se_sess
= target_setup_session(&tpg
->se_tpg
,
1541 VSCSI_DEFAULT_SESSION_TAGS
,
1542 sizeof(struct vscsibk_pend
),
1543 TARGET_PROT_NORMAL
, name
,
1544 tv_nexus
, scsiback_alloc_sess_cb
);
1545 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1552 mutex_unlock(&tpg
->tv_tpg_mutex
);
1556 static int scsiback_drop_nexus(struct scsiback_tpg
*tpg
)
1558 struct se_session
*se_sess
;
1559 struct scsiback_nexus
*tv_nexus
;
1561 mutex_lock(&tpg
->tv_tpg_mutex
);
1562 tv_nexus
= tpg
->tpg_nexus
;
1564 mutex_unlock(&tpg
->tv_tpg_mutex
);
1568 se_sess
= tv_nexus
->tvn_se_sess
;
1570 mutex_unlock(&tpg
->tv_tpg_mutex
);
1574 if (tpg
->tv_tpg_port_count
!= 0) {
1575 mutex_unlock(&tpg
->tv_tpg_mutex
);
1576 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1577 tpg
->tv_tpg_port_count
);
1581 if (tpg
->tv_tpg_fe_count
!= 0) {
1582 mutex_unlock(&tpg
->tv_tpg_mutex
);
1583 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1584 tpg
->tv_tpg_fe_count
);
1588 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1589 scsiback_dump_proto_id(tpg
->tport
),
1590 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1593 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1595 target_remove_session(se_sess
);
1596 tpg
->tpg_nexus
= NULL
;
1597 mutex_unlock(&tpg
->tv_tpg_mutex
);
1603 static ssize_t
scsiback_tpg_nexus_show(struct config_item
*item
, char *page
)
1605 struct se_portal_group
*se_tpg
= to_tpg(item
);
1606 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1607 struct scsiback_tpg
, se_tpg
);
1608 struct scsiback_nexus
*tv_nexus
;
1611 mutex_lock(&tpg
->tv_tpg_mutex
);
1612 tv_nexus
= tpg
->tpg_nexus
;
1614 mutex_unlock(&tpg
->tv_tpg_mutex
);
1617 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1618 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1619 mutex_unlock(&tpg
->tv_tpg_mutex
);
1624 static ssize_t
scsiback_tpg_nexus_store(struct config_item
*item
,
1625 const char *page
, size_t count
)
1627 struct se_portal_group
*se_tpg
= to_tpg(item
);
1628 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1629 struct scsiback_tpg
, se_tpg
);
1630 struct scsiback_tport
*tport_wwn
= tpg
->tport
;
1631 unsigned char i_port
[VSCSI_NAMELEN
], *ptr
, *port_ptr
;
1634 * Shutdown the active I_T nexus if 'NULL' is passed.
1636 if (!strncmp(page
, "NULL", 4)) {
1637 ret
= scsiback_drop_nexus(tpg
);
1638 return (!ret
) ? count
: ret
;
1641 * Otherwise make sure the passed virtual Initiator port WWN matches
1642 * the fabric protocol_id set in scsiback_make_tport(), and call
1643 * scsiback_make_nexus().
1645 if (strlen(page
) >= VSCSI_NAMELEN
) {
1646 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1647 page
, VSCSI_NAMELEN
);
1650 snprintf(&i_port
[0], VSCSI_NAMELEN
, "%s", page
);
1652 ptr
= strstr(i_port
, "naa.");
1654 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1655 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1656 i_port
, scsiback_dump_proto_id(tport_wwn
));
1659 port_ptr
= &i_port
[0];
1662 ptr
= strstr(i_port
, "fc.");
1664 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1665 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1666 i_port
, scsiback_dump_proto_id(tport_wwn
));
1669 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1672 ptr
= strstr(i_port
, "iqn.");
1674 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1675 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1676 i_port
, scsiback_dump_proto_id(tport_wwn
));
1679 port_ptr
= &i_port
[0];
1682 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1686 * Clear any trailing newline for the NAA WWN
1689 if (i_port
[strlen(i_port
) - 1] == '\n')
1690 i_port
[strlen(i_port
) - 1] = '\0';
1692 ret
= scsiback_make_nexus(tpg
, port_ptr
);
1699 CONFIGFS_ATTR(scsiback_tpg_
, nexus
);
1701 static struct configfs_attribute
*scsiback_tpg_attrs
[] = {
1702 &scsiback_tpg_attr_nexus
,
1707 scsiback_wwn_version_show(struct config_item
*item
, char *page
)
1709 return sprintf(page
, "xen-pvscsi fabric module %s on %s/%s on "
1711 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1714 CONFIGFS_ATTR_RO(scsiback_wwn_
, version
);
1716 static struct configfs_attribute
*scsiback_wwn_attrs
[] = {
1717 &scsiback_wwn_attr_version
,
1721 static int scsiback_port_link(struct se_portal_group
*se_tpg
,
1724 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1725 struct scsiback_tpg
, se_tpg
);
1727 mutex_lock(&tpg
->tv_tpg_mutex
);
1728 tpg
->tv_tpg_port_count
++;
1729 mutex_unlock(&tpg
->tv_tpg_mutex
);
1734 static void scsiback_port_unlink(struct se_portal_group
*se_tpg
,
1737 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1738 struct scsiback_tpg
, se_tpg
);
1740 mutex_lock(&tpg
->tv_tpg_mutex
);
1741 tpg
->tv_tpg_port_count
--;
1742 mutex_unlock(&tpg
->tv_tpg_mutex
);
1745 static struct se_portal_group
*
1746 scsiback_make_tpg(struct se_wwn
*wwn
, const char *name
)
1748 struct scsiback_tport
*tport
= container_of(wwn
,
1749 struct scsiback_tport
, tport_wwn
);
1751 struct scsiback_tpg
*tpg
;
1755 if (strstr(name
, "tpgt_") != name
)
1756 return ERR_PTR(-EINVAL
);
1757 ret
= kstrtou16(name
+ 5, 10, &tpgt
);
1759 return ERR_PTR(ret
);
1761 tpg
= kzalloc(sizeof(struct scsiback_tpg
), GFP_KERNEL
);
1763 return ERR_PTR(-ENOMEM
);
1765 mutex_init(&tpg
->tv_tpg_mutex
);
1766 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1767 INIT_LIST_HEAD(&tpg
->info_list
);
1769 tpg
->tport_tpgt
= tpgt
;
1771 ret
= core_tpg_register(wwn
, &tpg
->se_tpg
, tport
->tport_proto_id
);
1776 mutex_lock(&scsiback_mutex
);
1777 list_add_tail(&tpg
->tv_tpg_list
, &scsiback_list
);
1778 mutex_unlock(&scsiback_mutex
);
1780 return &tpg
->se_tpg
;
1783 static void scsiback_drop_tpg(struct se_portal_group
*se_tpg
)
1785 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1786 struct scsiback_tpg
, se_tpg
);
1788 mutex_lock(&scsiback_mutex
);
1789 list_del(&tpg
->tv_tpg_list
);
1790 mutex_unlock(&scsiback_mutex
);
1792 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1794 scsiback_drop_nexus(tpg
);
1796 * Deregister the se_tpg from TCM.
1798 core_tpg_deregister(se_tpg
);
1802 static int scsiback_check_true(struct se_portal_group
*se_tpg
)
1807 static const struct target_core_fabric_ops scsiback_ops
= {
1808 .module
= THIS_MODULE
,
1809 .fabric_name
= "xen-pvscsi",
1810 .tpg_get_wwn
= scsiback_get_fabric_wwn
,
1811 .tpg_get_tag
= scsiback_get_tag
,
1812 .tpg_check_demo_mode
= scsiback_check_true
,
1813 .tpg_check_demo_mode_cache
= scsiback_check_true
,
1814 .check_stop_free
= scsiback_check_stop_free
,
1815 .release_cmd
= scsiback_release_cmd
,
1816 .sess_get_initiator_sid
= NULL
,
1817 .write_pending
= scsiback_write_pending
,
1818 .queue_data_in
= scsiback_queue_data_in
,
1819 .queue_status
= scsiback_queue_status
,
1820 .queue_tm_rsp
= scsiback_queue_tm_rsp
,
1821 .aborted_task
= scsiback_aborted_task
,
1823 * Setup callers for generic logic in target_core_fabric_configfs.c
1825 .fabric_make_wwn
= scsiback_make_tport
,
1826 .fabric_drop_wwn
= scsiback_drop_tport
,
1827 .fabric_make_tpg
= scsiback_make_tpg
,
1828 .fabric_drop_tpg
= scsiback_drop_tpg
,
1829 .fabric_post_link
= scsiback_port_link
,
1830 .fabric_pre_unlink
= scsiback_port_unlink
,
1832 .tfc_wwn_attrs
= scsiback_wwn_attrs
,
1833 .tfc_tpg_base_attrs
= scsiback_tpg_attrs
,
1834 .tfc_tpg_param_attrs
= scsiback_param_attrs
,
1836 .default_submit_type
= TARGET_DIRECT_SUBMIT
,
1837 .direct_submit_supp
= 1,
1840 static const struct xenbus_device_id scsiback_ids
[] = {
1845 static struct xenbus_driver scsiback_driver
= {
1846 .ids
= scsiback_ids
,
1847 .probe
= scsiback_probe
,
1848 .remove
= scsiback_remove
,
1849 .otherend_changed
= scsiback_frontend_changed
1852 static int __init
scsiback_init(void)
1859 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE
"\n",
1860 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1862 ret
= xenbus_register_backend(&scsiback_driver
);
1866 ret
= target_register_template(&scsiback_ops
);
1868 goto out_unregister_xenbus
;
1872 out_unregister_xenbus
:
1873 xenbus_unregister_driver(&scsiback_driver
);
1875 pr_err("%s: error %d\n", __func__
, ret
);
1879 static void __exit
scsiback_exit(void)
1881 target_unregister_template(&scsiback_ops
);
1882 xenbus_unregister_driver(&scsiback_driver
);
1885 module_init(scsiback_init
);
1886 module_exit(scsiback_exit
);
1888 MODULE_DESCRIPTION("Xen SCSI backend driver");
1889 MODULE_LICENSE("Dual BSD/GPL");
1890 MODULE_ALIAS("xen-backend:vscsi");
1891 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");