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
38 #include <linux/module.h>
39 #include <linux/utsname.h>
40 #include <linux/interrupt.h>
41 #include <linux/slab.h>
42 #include <linux/wait.h>
43 #include <linux/sched.h>
44 #include <linux/list.h>
45 #include <linux/gfp.h>
46 #include <linux/delay.h>
47 #include <linux/spinlock.h>
48 #include <linux/configfs.h>
50 #include <generated/utsrelease.h>
52 #include <scsi/scsi_host.h> /* SG_ALL */
54 #include <target/target_core_base.h>
55 #include <target/target_core_fabric.h>
57 #include <asm/hypervisor.h>
60 #include <xen/balloon.h>
61 #include <xen/events.h>
62 #include <xen/xenbus.h>
63 #include <xen/grant_table.h>
66 #include <xen/interface/grant_table.h>
67 #include <xen/interface/io/vscsiif.h>
69 #define VSCSI_VERSION "v0.1"
70 #define VSCSI_NAMELEN 32
73 unsigned int hst
; /* host */
74 unsigned int chn
; /* channel */
75 unsigned int tgt
; /* target */
76 unsigned int lun
; /* LUN */
80 struct ids_tuple v
; /* translate from */
81 struct scsiback_tpg
*tpg
; /* translate to */
88 struct xenbus_device
*dev
;
93 struct vscsiif_back_ring ring
;
96 atomic_t nr_unreplied_reqs
;
99 struct list_head v2p_entry_lists
;
101 wait_queue_head_t waiting_to_free
;
103 struct gnttab_page_cache free_pages
;
106 /* theoretical maximum of grants for one request */
107 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
110 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
111 * call to map/unmap grants. Don't choose it too large, as there are arrays
112 * with VSCSI_GRANT_BATCH elements allocated on the stack.
114 #define VSCSI_GRANT_BATCH 16
116 struct vscsibk_pend
{
119 uint8_t cmnd
[VSCSIIF_MAX_COMMAND_SIZE
];
122 uint8_t sc_data_direction
;
123 uint16_t n_sg
; /* real length of SG list */
124 uint16_t n_grants
; /* SG pages and potentially SG list */
128 struct vscsibk_info
*info
;
129 struct v2p_entry
*v2p
;
130 struct scatterlist
*sgl
;
132 uint8_t sense_buffer
[VSCSIIF_SENSE_BUFFERSIZE
];
134 grant_handle_t grant_handles
[VSCSI_MAX_GRANTS
];
135 struct page
*pages
[VSCSI_MAX_GRANTS
];
137 struct se_cmd se_cmd
;
139 struct completion tmr_done
;
142 #define VSCSI_DEFAULT_SESSION_TAGS 128
144 struct scsiback_nexus
{
145 /* Pointer to TCM session for I_T Nexus */
146 struct se_session
*tvn_se_sess
;
149 struct scsiback_tport
{
150 /* SCSI protocol the tport is providing */
152 /* Binary World Wide unique Port Name for pvscsi Target port */
154 /* ASCII formatted WWPN for pvscsi Target port */
155 char tport_name
[VSCSI_NAMELEN
];
156 /* Returned by scsiback_make_tport() */
157 struct se_wwn tport_wwn
;
160 struct scsiback_tpg
{
161 /* scsiback port target portal group tag for TCM */
163 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
164 int tv_tpg_port_count
;
165 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
167 /* list for scsiback_list */
168 struct list_head tv_tpg_list
;
169 /* Used to protect access for tpg_nexus */
170 struct mutex tv_tpg_mutex
;
171 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
172 struct scsiback_nexus
*tpg_nexus
;
173 /* Pointer back to scsiback_tport */
174 struct scsiback_tport
*tport
;
175 /* Returned by scsiback_make_tpg() */
176 struct se_portal_group se_tpg
;
177 /* alias used in xenstore */
178 char param_alias
[VSCSI_NAMELEN
];
179 /* list of info structures related to this target portal group */
180 struct list_head info_list
;
183 #define SCSIBACK_INVALID_HANDLE (~0)
185 static bool log_print_stat
;
186 module_param(log_print_stat
, bool, 0644);
188 static int scsiback_max_buffer_pages
= 1024;
189 module_param_named(max_buffer_pages
, scsiback_max_buffer_pages
, int, 0644);
190 MODULE_PARM_DESC(max_buffer_pages
,
191 "Maximum number of free pages to keep in backend buffer");
193 /* Global spinlock to protect scsiback TPG list */
194 static DEFINE_MUTEX(scsiback_mutex
);
195 static LIST_HEAD(scsiback_list
);
197 static void scsiback_get(struct vscsibk_info
*info
)
199 atomic_inc(&info
->nr_unreplied_reqs
);
202 static void scsiback_put(struct vscsibk_info
*info
)
204 if (atomic_dec_and_test(&info
->nr_unreplied_reqs
))
205 wake_up(&info
->waiting_to_free
);
208 static unsigned long vaddr_page(struct page
*page
)
210 unsigned long pfn
= page_to_pfn(page
);
212 return (unsigned long)pfn_to_kaddr(pfn
);
215 static unsigned long vaddr(struct vscsibk_pend
*req
, int seg
)
217 return vaddr_page(req
->pages
[seg
]);
220 static void scsiback_print_status(char *sense_buffer
, int errors
,
221 struct vscsibk_pend
*pending_req
)
223 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
225 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
226 tpg
->tport
->tport_name
, pending_req
->v2p
->lun
,
227 pending_req
->cmnd
[0], status_byte(errors
), msg_byte(errors
),
228 host_byte(errors
), driver_byte(errors
));
231 static void scsiback_fast_flush_area(struct vscsibk_pend
*req
)
233 struct gnttab_unmap_grant_ref unmap
[VSCSI_GRANT_BATCH
];
234 struct page
*pages
[VSCSI_GRANT_BATCH
];
235 unsigned int i
, invcount
= 0;
236 grant_handle_t handle
;
246 for (i
= 0; i
< req
->n_grants
; i
++) {
247 handle
= req
->grant_handles
[i
];
248 if (handle
== SCSIBACK_INVALID_HANDLE
)
250 gnttab_set_unmap_op(&unmap
[invcount
], vaddr(req
, i
),
251 GNTMAP_host_map
, handle
);
252 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
253 pages
[invcount
] = req
->pages
[i
];
254 put_page(pages
[invcount
]);
256 if (invcount
< VSCSI_GRANT_BATCH
)
258 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
264 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
268 gnttab_page_cache_put(&req
->info
->free_pages
, req
->pages
,
273 static void scsiback_free_translation_entry(struct kref
*kref
)
275 struct v2p_entry
*entry
= container_of(kref
, struct v2p_entry
, kref
);
276 struct scsiback_tpg
*tpg
= entry
->tpg
;
278 mutex_lock(&tpg
->tv_tpg_mutex
);
279 tpg
->tv_tpg_fe_count
--;
280 mutex_unlock(&tpg
->tv_tpg_mutex
);
285 static void scsiback_send_response(struct vscsibk_info
*info
,
286 char *sense_buffer
, int32_t result
, uint32_t resid
,
289 struct vscsiif_response
*ring_res
;
291 struct scsi_sense_hdr sshdr
;
295 spin_lock_irqsave(&info
->ring_lock
, flags
);
297 ring_res
= RING_GET_RESPONSE(&info
->ring
, info
->ring
.rsp_prod_pvt
);
298 info
->ring
.rsp_prod_pvt
++;
300 ring_res
->rslt
= result
;
301 ring_res
->rqid
= rqid
;
303 if (sense_buffer
!= NULL
&&
304 scsi_normalize_sense(sense_buffer
, VSCSIIF_SENSE_BUFFERSIZE
,
306 len
= min_t(unsigned, 8 + sense_buffer
[7],
307 VSCSIIF_SENSE_BUFFERSIZE
);
308 memcpy(ring_res
->sense_buffer
, sense_buffer
, len
);
309 ring_res
->sense_len
= len
;
311 ring_res
->sense_len
= 0;
314 ring_res
->residual_len
= resid
;
316 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info
->ring
, notify
);
317 spin_unlock_irqrestore(&info
->ring_lock
, flags
);
320 notify_remote_via_irq(info
->irq
);
323 static void scsiback_do_resp_with_sense(char *sense_buffer
, int32_t result
,
324 uint32_t resid
, struct vscsibk_pend
*pending_req
)
326 scsiback_send_response(pending_req
->info
, sense_buffer
, result
,
327 resid
, pending_req
->rqid
);
329 if (pending_req
->v2p
)
330 kref_put(&pending_req
->v2p
->kref
,
331 scsiback_free_translation_entry
);
334 static void scsiback_cmd_done(struct vscsibk_pend
*pending_req
)
336 struct vscsibk_info
*info
= pending_req
->info
;
337 unsigned char *sense_buffer
;
341 sense_buffer
= pending_req
->sense_buffer
;
342 resid
= pending_req
->se_cmd
.residual_count
;
343 errors
= pending_req
->result
;
345 if (errors
&& log_print_stat
)
346 scsiback_print_status(sense_buffer
, errors
, pending_req
);
348 scsiback_fast_flush_area(pending_req
);
349 scsiback_do_resp_with_sense(sense_buffer
, errors
, resid
, pending_req
);
352 * Drop the extra KREF_ACK reference taken by target_submit_cmd_map_sgls()
353 * ahead of scsiback_check_stop_free() -> transport_generic_free_cmd()
354 * final se_cmd->cmd_kref put.
356 target_put_sess_cmd(&pending_req
->se_cmd
);
359 static void scsiback_cmd_exec(struct vscsibk_pend
*pending_req
)
361 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
362 struct se_session
*sess
= pending_req
->v2p
->tpg
->tpg_nexus
->tvn_se_sess
;
365 scsiback_get(pending_req
->info
);
366 se_cmd
->tag
= pending_req
->rqid
;
367 rc
= target_submit_cmd_map_sgls(se_cmd
, sess
, pending_req
->cmnd
,
368 pending_req
->sense_buffer
, pending_req
->v2p
->lun
,
369 pending_req
->data_len
, 0,
370 pending_req
->sc_data_direction
, TARGET_SCF_ACK_KREF
,
371 pending_req
->sgl
, pending_req
->n_sg
,
374 transport_send_check_condition_and_sense(se_cmd
,
375 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
376 transport_generic_free_cmd(se_cmd
, 0);
380 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref
*map
,
381 struct page
**pg
, grant_handle_t
*grant
, int cnt
)
388 err
= gnttab_map_refs(map
, NULL
, pg
, cnt
);
390 for (i
= 0; i
< cnt
; i
++) {
391 if (unlikely(map
[i
].status
!= GNTST_okay
)) {
392 pr_err("invalid buffer -- could not remap it\n");
393 map
[i
].handle
= SCSIBACK_INVALID_HANDLE
;
398 grant
[i
] = map
[i
].handle
;
403 static int scsiback_gnttab_data_map_list(struct vscsibk_pend
*pending_req
,
404 struct scsiif_request_segment
*seg
, struct page
**pg
,
405 grant_handle_t
*grant
, int cnt
, u32 flags
)
407 int mapcount
= 0, i
, err
= 0;
408 struct gnttab_map_grant_ref map
[VSCSI_GRANT_BATCH
];
409 struct vscsibk_info
*info
= pending_req
->info
;
411 for (i
= 0; i
< cnt
; i
++) {
412 if (gnttab_page_cache_get(&info
->free_pages
, pg
+ mapcount
)) {
413 gnttab_page_cache_put(&info
->free_pages
, pg
, mapcount
);
414 pr_err("no grant page\n");
417 gnttab_set_map_op(&map
[mapcount
], vaddr_page(pg
[mapcount
]),
418 flags
, seg
[i
].gref
, info
->domid
);
420 if (mapcount
< VSCSI_GRANT_BATCH
)
422 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
425 pending_req
->n_grants
+= mapcount
;
430 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
431 pending_req
->n_grants
+= mapcount
;
435 static int scsiback_gnttab_data_map(struct vscsiif_request
*ring_req
,
436 struct vscsibk_pend
*pending_req
)
439 int i
, err
, n_segs
, i_seg
= 0;
441 struct scsiif_request_segment
*seg
;
442 unsigned long end_seg
= 0;
443 unsigned int nr_segments
= (unsigned int)ring_req
->nr_segments
;
444 unsigned int nr_sgl
= 0;
445 struct scatterlist
*sg
;
446 grant_handle_t
*grant
;
448 pending_req
->n_sg
= 0;
449 pending_req
->n_grants
= 0;
450 pending_req
->data_len
= 0;
452 nr_segments
&= ~VSCSIIF_SG_GRANT
;
456 if (nr_segments
> VSCSIIF_SG_TABLESIZE
) {
457 pr_debug("invalid parameter nr_seg = %d\n",
458 ring_req
->nr_segments
);
462 if (ring_req
->nr_segments
& VSCSIIF_SG_GRANT
) {
463 err
= scsiback_gnttab_data_map_list(pending_req
, ring_req
->seg
,
464 pending_req
->pages
, pending_req
->grant_handles
,
465 nr_segments
, GNTMAP_host_map
| GNTMAP_readonly
);
468 nr_sgl
= nr_segments
;
470 for (i
= 0; i
< nr_sgl
; i
++) {
471 n_segs
= ring_req
->seg
[i
].length
/
472 sizeof(struct scsiif_request_segment
);
473 if ((unsigned)ring_req
->seg
[i
].offset
+
474 (unsigned)ring_req
->seg
[i
].length
> PAGE_SIZE
||
475 n_segs
* sizeof(struct scsiif_request_segment
) !=
476 ring_req
->seg
[i
].length
)
478 nr_segments
+= n_segs
;
480 if (nr_segments
> SG_ALL
) {
481 pr_debug("invalid nr_seg = %d\n", nr_segments
);
486 /* free of (sgl) in fast_flush_area() */
487 pending_req
->sgl
= kmalloc_array(nr_segments
,
488 sizeof(struct scatterlist
), GFP_KERNEL
);
489 if (!pending_req
->sgl
)
492 sg_init_table(pending_req
->sgl
, nr_segments
);
493 pending_req
->n_sg
= nr_segments
;
495 flags
= GNTMAP_host_map
;
496 if (pending_req
->sc_data_direction
== DMA_TO_DEVICE
)
497 flags
|= GNTMAP_readonly
;
499 pg
= pending_req
->pages
+ nr_sgl
;
500 grant
= pending_req
->grant_handles
+ nr_sgl
;
503 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
504 pg
, grant
, nr_segments
, flags
);
508 for (i
= 0; i
< nr_sgl
; i
++) {
509 seg
= (struct scsiif_request_segment
*)(
510 vaddr(pending_req
, i
) + ring_req
->seg
[i
].offset
);
511 n_segs
= ring_req
->seg
[i
].length
/
512 sizeof(struct scsiif_request_segment
);
513 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
514 pg
, grant
, n_segs
, flags
);
520 end_seg
= vaddr(pending_req
, 0) + ring_req
->seg
[0].offset
;
521 seg
= (struct scsiif_request_segment
*)end_seg
;
522 end_seg
+= ring_req
->seg
[0].length
;
523 pg
= pending_req
->pages
+ nr_sgl
;
526 for_each_sg(pending_req
->sgl
, sg
, nr_segments
, i
) {
527 sg_set_page(sg
, pg
[i
], seg
->length
, seg
->offset
);
528 pending_req
->data_len
+= seg
->length
;
530 if (nr_sgl
&& (unsigned long)seg
>= end_seg
) {
532 end_seg
= vaddr(pending_req
, i_seg
) +
533 ring_req
->seg
[i_seg
].offset
;
534 seg
= (struct scsiif_request_segment
*)end_seg
;
535 end_seg
+= ring_req
->seg
[i_seg
].length
;
537 if (sg
->offset
>= PAGE_SIZE
||
538 sg
->length
> PAGE_SIZE
||
539 sg
->offset
+ sg
->length
> PAGE_SIZE
)
546 static void scsiback_disconnect(struct vscsibk_info
*info
)
548 wait_event(info
->waiting_to_free
,
549 atomic_read(&info
->nr_unreplied_reqs
) == 0);
551 unbind_from_irqhandler(info
->irq
, info
);
553 xenbus_unmap_ring_vfree(info
->dev
, info
->ring
.sring
);
556 static void scsiback_device_action(struct vscsibk_pend
*pending_req
,
557 enum tcm_tmreq_table act
, int tag
)
559 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
560 struct scsiback_nexus
*nexus
= tpg
->tpg_nexus
;
561 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
562 u64 unpacked_lun
= pending_req
->v2p
->lun
;
563 int rc
, err
= FAILED
;
565 init_completion(&pending_req
->tmr_done
);
567 rc
= target_submit_tmr(&pending_req
->se_cmd
, nexus
->tvn_se_sess
,
568 &pending_req
->sense_buffer
[0],
569 unpacked_lun
, NULL
, act
, GFP_KERNEL
,
570 tag
, TARGET_SCF_ACK_KREF
);
574 wait_for_completion(&pending_req
->tmr_done
);
576 err
= (se_cmd
->se_tmr_req
->response
== TMR_FUNCTION_COMPLETE
) ?
579 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
580 transport_generic_free_cmd(&pending_req
->se_cmd
, 0);
584 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
588 Perform virtual to physical translation
590 static struct v2p_entry
*scsiback_do_translation(struct vscsibk_info
*info
,
593 struct v2p_entry
*entry
;
594 struct list_head
*head
= &(info
->v2p_entry_lists
);
597 spin_lock_irqsave(&info
->v2p_lock
, flags
);
598 list_for_each_entry(entry
, head
, l
) {
599 if ((entry
->v
.chn
== v
->chn
) &&
600 (entry
->v
.tgt
== v
->tgt
) &&
601 (entry
->v
.lun
== v
->lun
)) {
602 kref_get(&entry
->kref
);
609 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
613 static struct vscsibk_pend
*scsiback_get_pend_req(struct vscsiif_back_ring
*ring
,
614 struct v2p_entry
*v2p
)
616 struct scsiback_tpg
*tpg
= v2p
->tpg
;
617 struct scsiback_nexus
*nexus
= tpg
->tpg_nexus
;
618 struct se_session
*se_sess
= nexus
->tvn_se_sess
;
619 struct vscsibk_pend
*req
;
622 tag
= sbitmap_queue_get(&se_sess
->sess_tag_pool
, &cpu
);
624 pr_err("Unable to obtain tag for vscsiif_request\n");
625 return ERR_PTR(-ENOMEM
);
628 req
= &((struct vscsibk_pend
*)se_sess
->sess_cmd_map
)[tag
];
629 memset(req
, 0, sizeof(*req
));
630 req
->se_cmd
.map_tag
= tag
;
631 req
->se_cmd
.map_cpu
= cpu
;
633 for (i
= 0; i
< VSCSI_MAX_GRANTS
; i
++)
634 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
639 static struct vscsibk_pend
*prepare_pending_reqs(struct vscsibk_info
*info
,
640 struct vscsiif_back_ring
*ring
,
641 struct vscsiif_request
*ring_req
)
643 struct vscsibk_pend
*pending_req
;
644 struct v2p_entry
*v2p
;
645 struct ids_tuple vir
;
647 /* request range check from frontend */
648 if ((ring_req
->sc_data_direction
!= DMA_BIDIRECTIONAL
) &&
649 (ring_req
->sc_data_direction
!= DMA_TO_DEVICE
) &&
650 (ring_req
->sc_data_direction
!= DMA_FROM_DEVICE
) &&
651 (ring_req
->sc_data_direction
!= DMA_NONE
)) {
652 pr_debug("invalid parameter data_dir = %d\n",
653 ring_req
->sc_data_direction
);
654 return ERR_PTR(-EINVAL
);
656 if (ring_req
->cmd_len
> VSCSIIF_MAX_COMMAND_SIZE
) {
657 pr_debug("invalid parameter cmd_len = %d\n",
659 return ERR_PTR(-EINVAL
);
662 vir
.chn
= ring_req
->channel
;
663 vir
.tgt
= ring_req
->id
;
664 vir
.lun
= ring_req
->lun
;
666 v2p
= scsiback_do_translation(info
, &vir
);
668 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
669 vir
.chn
, vir
.tgt
, vir
.lun
);
670 return ERR_PTR(-ENODEV
);
673 pending_req
= scsiback_get_pend_req(ring
, v2p
);
674 if (IS_ERR(pending_req
)) {
675 kref_put(&v2p
->kref
, scsiback_free_translation_entry
);
676 return ERR_PTR(-ENOMEM
);
678 pending_req
->rqid
= ring_req
->rqid
;
679 pending_req
->info
= info
;
680 pending_req
->v2p
= v2p
;
681 pending_req
->sc_data_direction
= ring_req
->sc_data_direction
;
682 pending_req
->cmd_len
= ring_req
->cmd_len
;
683 memcpy(pending_req
->cmnd
, ring_req
->cmnd
, pending_req
->cmd_len
);
688 static int scsiback_do_cmd_fn(struct vscsibk_info
*info
,
689 unsigned int *eoi_flags
)
691 struct vscsiif_back_ring
*ring
= &info
->ring
;
692 struct vscsiif_request ring_req
;
693 struct vscsibk_pend
*pending_req
;
699 rp
= ring
->sring
->req_prod
;
700 rmb(); /* guest system is accessing ring, too */
702 if (RING_REQUEST_PROD_OVERFLOW(ring
, rp
)) {
703 rc
= ring
->rsp_prod_pvt
;
704 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
705 info
->domid
, rp
, rc
, rp
- rc
);
710 *eoi_flags
&= ~XEN_EOI_FLAG_SPURIOUS
;
712 if (RING_REQUEST_CONS_OVERFLOW(ring
, rc
))
715 RING_COPY_REQUEST(ring
, rc
, &ring_req
);
716 ring
->req_cons
= ++rc
;
718 pending_req
= prepare_pending_reqs(info
, ring
, &ring_req
);
719 if (IS_ERR(pending_req
)) {
720 switch (PTR_ERR(pending_req
)) {
722 result
= DID_NO_CONNECT
;
725 result
= DRIVER_ERROR
;
728 scsiback_send_response(info
, NULL
, result
<< 24, 0,
733 switch (ring_req
.act
) {
734 case VSCSIIF_ACT_SCSI_CDB
:
735 if (scsiback_gnttab_data_map(&ring_req
, pending_req
)) {
736 scsiback_fast_flush_area(pending_req
);
737 scsiback_do_resp_with_sense(NULL
,
738 DRIVER_ERROR
<< 24, 0, pending_req
);
739 transport_generic_free_cmd(&pending_req
->se_cmd
, 0);
741 scsiback_cmd_exec(pending_req
);
744 case VSCSIIF_ACT_SCSI_ABORT
:
745 scsiback_device_action(pending_req
, TMR_ABORT_TASK
,
748 case VSCSIIF_ACT_SCSI_RESET
:
749 scsiback_device_action(pending_req
, TMR_LUN_RESET
, 0);
752 pr_err_ratelimited("invalid request\n");
753 scsiback_do_resp_with_sense(NULL
, DRIVER_ERROR
<< 24, 0,
755 transport_generic_free_cmd(&pending_req
->se_cmd
, 0);
759 /* Yield point for this unbounded loop. */
763 gnttab_page_cache_shrink(&info
->free_pages
, scsiback_max_buffer_pages
);
765 RING_FINAL_CHECK_FOR_REQUESTS(&info
->ring
, more_to_do
);
769 static irqreturn_t
scsiback_irq_fn(int irq
, void *dev_id
)
771 struct vscsibk_info
*info
= dev_id
;
773 unsigned int eoi_flags
= XEN_EOI_FLAG_SPURIOUS
;
775 while ((rc
= scsiback_do_cmd_fn(info
, &eoi_flags
)) > 0)
778 /* In case of a ring error we keep the event channel masked. */
780 xen_irq_lateeoi(irq
, eoi_flags
);
785 static int scsiback_init_sring(struct vscsibk_info
*info
, grant_ref_t ring_ref
,
786 evtchn_port_t evtchn
)
789 struct vscsiif_sring
*sring
;
795 err
= xenbus_map_ring_valloc(info
->dev
, &ring_ref
, 1, &area
);
799 sring
= (struct vscsiif_sring
*)area
;
800 BACK_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
802 err
= bind_interdomain_evtchn_to_irq_lateeoi(info
->domid
, evtchn
);
808 err
= request_threaded_irq(info
->irq
, NULL
, scsiback_irq_fn
,
809 IRQF_ONESHOT
, "vscsiif-backend", info
);
816 unbind_from_irqhandler(info
->irq
, info
);
819 xenbus_unmap_ring_vfree(info
->dev
, area
);
824 static int scsiback_map(struct vscsibk_info
*info
)
826 struct xenbus_device
*dev
= info
->dev
;
827 unsigned int ring_ref
;
828 evtchn_port_t evtchn
;
831 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
832 "ring-ref", "%u", &ring_ref
,
833 "event-channel", "%u", &evtchn
, NULL
);
835 xenbus_dev_fatal(dev
, err
, "reading %s ring", dev
->otherend
);
839 return scsiback_init_sring(info
, ring_ref
, evtchn
);
843 Check for a translation entry being present
845 static struct v2p_entry
*scsiback_chk_translation_entry(
846 struct vscsibk_info
*info
, struct ids_tuple
*v
)
848 struct list_head
*head
= &(info
->v2p_entry_lists
);
849 struct v2p_entry
*entry
;
851 list_for_each_entry(entry
, head
, l
)
852 if ((entry
->v
.chn
== v
->chn
) &&
853 (entry
->v
.tgt
== v
->tgt
) &&
854 (entry
->v
.lun
== v
->lun
))
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
*new;
870 unsigned long long unpacked_lun
;
871 struct se_lun
*se_lun
;
872 struct scsiback_tpg
*tpg_entry
, *tpg
= NULL
;
873 char *error
= "doesn't exist";
875 lunp
= strrchr(phy
, ':');
877 pr_err("illegal format of physical device %s\n", phy
);
882 err
= kstrtoull(lunp
, 10, &unpacked_lun
);
884 pr_err("lun number not valid: %s\n", lunp
);
888 mutex_lock(&scsiback_mutex
);
889 list_for_each_entry(tpg_entry
, &scsiback_list
, tv_tpg_list
) {
890 if (!strcmp(phy
, tpg_entry
->tport
->tport_name
) ||
891 !strcmp(phy
, tpg_entry
->param_alias
)) {
892 mutex_lock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
893 hlist_for_each_entry(se_lun
, &tpg_entry
->se_tpg
.tpg_lun_hlist
, link
) {
894 if (se_lun
->unpacked_lun
== unpacked_lun
) {
895 if (!tpg_entry
->tpg_nexus
)
896 error
= "nexus undefined";
902 mutex_unlock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
907 mutex_lock(&tpg
->tv_tpg_mutex
);
908 tpg
->tv_tpg_fe_count
++;
909 mutex_unlock(&tpg
->tv_tpg_mutex
);
911 mutex_unlock(&scsiback_mutex
);
914 pr_err("%s:%llu %s\n", phy
, unpacked_lun
, error
);
918 new = kmalloc(sizeof(struct v2p_entry
), GFP_KERNEL
);
924 spin_lock_irqsave(&info
->v2p_lock
, flags
);
926 /* Check double assignment to identical virtual ID */
927 if (scsiback_chk_translation_entry(info
, v
)) {
928 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
933 /* Create a new translation entry and add to the list */
934 kref_init(&new->kref
);
937 new->lun
= unpacked_lun
;
938 list_add_tail(&new->l
, &info
->v2p_entry_lists
);
941 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
945 mutex_lock(&tpg
->tv_tpg_mutex
);
946 tpg
->tv_tpg_fe_count
--;
947 mutex_unlock(&tpg
->tv_tpg_mutex
);
954 static void __scsiback_del_translation_entry(struct v2p_entry
*entry
)
957 kref_put(&entry
->kref
, scsiback_free_translation_entry
);
961 Delete the translation entry specified
963 static int scsiback_del_translation_entry(struct vscsibk_info
*info
,
966 struct v2p_entry
*entry
;
970 spin_lock_irqsave(&info
->v2p_lock
, flags
);
971 /* Find out the translation entry specified */
972 entry
= scsiback_chk_translation_entry(info
, v
);
974 __scsiback_del_translation_entry(entry
);
978 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
982 static void scsiback_do_add_lun(struct vscsibk_info
*info
, const char *state
,
983 char *phy
, struct ids_tuple
*vir
, int try)
985 struct v2p_entry
*entry
;
990 spin_lock_irqsave(&info
->v2p_lock
, flags
);
991 entry
= scsiback_chk_translation_entry(info
, vir
);
992 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
996 if (!scsiback_add_translation_entry(info
, phy
, vir
)) {
997 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
998 "%d", XenbusStateInitialised
)) {
999 pr_err("xenbus_printf error %s\n", state
);
1000 scsiback_del_translation_entry(info
, vir
);
1003 err
= xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1004 "%d", XenbusStateClosed
);
1006 xenbus_dev_error(info
->dev
, err
,
1007 "%s: writing %s", __func__
, state
);
1011 static void scsiback_do_del_lun(struct vscsibk_info
*info
, const char *state
,
1012 struct ids_tuple
*vir
)
1014 if (!scsiback_del_translation_entry(info
, vir
)) {
1015 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1016 "%d", XenbusStateClosed
))
1017 pr_err("xenbus_printf error %s\n", state
);
1021 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1022 #define VSCSIBACK_OP_UPDATEDEV_STATE 2
1024 static void scsiback_do_1lun_hotplug(struct vscsibk_info
*info
, int op
,
1028 struct ids_tuple vir
;
1031 char phy
[VSCSI_NAMELEN
];
1034 struct xenbus_device
*dev
= info
->dev
;
1037 snprintf(state
, sizeof(state
), "vscsi-devs/%s/state", ent
);
1038 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, state
, "%u", &device_state
);
1039 if (XENBUS_EXIST_ERR(err
))
1042 /* physical SCSI device */
1043 snprintf(str
, sizeof(str
), "vscsi-devs/%s/p-dev", ent
);
1044 val
= xenbus_read(XBT_NIL
, dev
->nodename
, str
, NULL
);
1046 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1047 "%d", XenbusStateClosed
);
1049 xenbus_dev_error(info
->dev
, err
,
1050 "%s: writing %s", __func__
, state
);
1053 strlcpy(phy
, val
, VSCSI_NAMELEN
);
1056 /* virtual SCSI device */
1057 snprintf(str
, sizeof(str
), "vscsi-devs/%s/v-dev", ent
);
1058 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, str
, "%u:%u:%u:%u",
1059 &vir
.hst
, &vir
.chn
, &vir
.tgt
, &vir
.lun
);
1060 if (XENBUS_EXIST_ERR(err
)) {
1061 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1062 "%d", XenbusStateClosed
);
1064 xenbus_dev_error(info
->dev
, err
,
1065 "%s: writing %s", __func__
, state
);
1070 case VSCSIBACK_OP_ADD_OR_DEL_LUN
:
1071 switch (device_state
) {
1072 case XenbusStateInitialising
:
1073 scsiback_do_add_lun(info
, state
, phy
, &vir
, 0);
1075 case XenbusStateConnected
:
1076 scsiback_do_add_lun(info
, state
, phy
, &vir
, 1);
1078 case XenbusStateClosing
:
1079 scsiback_do_del_lun(info
, state
, &vir
);
1086 case VSCSIBACK_OP_UPDATEDEV_STATE
:
1087 if (device_state
== XenbusStateInitialised
) {
1088 /* modify vscsi-devs/dev-x/state */
1089 if (xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1090 "%d", XenbusStateConnected
)) {
1091 pr_err("xenbus_printf error %s\n", str
);
1092 scsiback_del_translation_entry(info
, &vir
);
1093 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1094 "%d", XenbusStateClosed
);
1098 /* When it is necessary, processing is added here. */
1104 static void scsiback_do_lun_hotplug(struct vscsibk_info
*info
, int op
)
1108 unsigned int ndir
= 0;
1110 dir
= xenbus_directory(XBT_NIL
, info
->dev
->nodename
, "vscsi-devs",
1115 for (i
= 0; i
< ndir
; i
++)
1116 scsiback_do_1lun_hotplug(info
, op
, dir
[i
]);
1121 static void scsiback_frontend_changed(struct xenbus_device
*dev
,
1122 enum xenbus_state frontend_state
)
1124 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1126 switch (frontend_state
) {
1127 case XenbusStateInitialising
:
1130 case XenbusStateInitialised
:
1131 if (scsiback_map(info
))
1134 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1135 xenbus_switch_state(dev
, XenbusStateConnected
);
1138 case XenbusStateConnected
:
1139 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_UPDATEDEV_STATE
);
1141 if (dev
->state
== XenbusStateConnected
)
1144 xenbus_switch_state(dev
, XenbusStateConnected
);
1147 case XenbusStateClosing
:
1149 scsiback_disconnect(info
);
1151 xenbus_switch_state(dev
, XenbusStateClosing
);
1154 case XenbusStateClosed
:
1155 xenbus_switch_state(dev
, XenbusStateClosed
);
1156 if (xenbus_dev_is_online(dev
))
1158 fallthrough
; /* if not online */
1159 case XenbusStateUnknown
:
1160 device_unregister(&dev
->dev
);
1163 case XenbusStateReconfiguring
:
1164 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1165 xenbus_switch_state(dev
, XenbusStateReconfigured
);
1170 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
1177 Release the translation entry specfied
1179 static void scsiback_release_translation_entry(struct vscsibk_info
*info
)
1181 struct v2p_entry
*entry
, *tmp
;
1182 struct list_head
*head
= &(info
->v2p_entry_lists
);
1183 unsigned long flags
;
1185 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1187 list_for_each_entry_safe(entry
, tmp
, head
, l
)
1188 __scsiback_del_translation_entry(entry
);
1190 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1193 static int scsiback_remove(struct xenbus_device
*dev
)
1195 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1198 scsiback_disconnect(info
);
1200 scsiback_release_translation_entry(info
);
1202 gnttab_page_cache_shrink(&info
->free_pages
, 0);
1204 dev_set_drvdata(&dev
->dev
, NULL
);
1209 static int scsiback_probe(struct xenbus_device
*dev
,
1210 const struct xenbus_device_id
*id
)
1214 struct vscsibk_info
*info
= kzalloc(sizeof(struct vscsibk_info
),
1217 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
1220 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating backend structure");
1224 dev_set_drvdata(&dev
->dev
, info
);
1226 info
->domid
= dev
->otherend_id
;
1227 spin_lock_init(&info
->ring_lock
);
1228 atomic_set(&info
->nr_unreplied_reqs
, 0);
1229 init_waitqueue_head(&info
->waiting_to_free
);
1232 INIT_LIST_HEAD(&info
->v2p_entry_lists
);
1233 spin_lock_init(&info
->v2p_lock
);
1234 gnttab_page_cache_init(&info
->free_pages
);
1236 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, "feature-sg-grant", "%u",
1239 xenbus_dev_error(dev
, err
, "writing feature-sg-grant");
1241 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
1248 pr_warn("%s failed\n", __func__
);
1249 scsiback_remove(dev
);
1254 static char *scsiback_dump_proto_id(struct scsiback_tport
*tport
)
1256 switch (tport
->tport_proto_id
) {
1257 case SCSI_PROTOCOL_SAS
:
1259 case SCSI_PROTOCOL_FCP
:
1261 case SCSI_PROTOCOL_ISCSI
:
1270 static char *scsiback_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1272 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1273 struct scsiback_tpg
, se_tpg
);
1274 struct scsiback_tport
*tport
= tpg
->tport
;
1276 return &tport
->tport_name
[0];
1279 static u16
scsiback_get_tag(struct se_portal_group
*se_tpg
)
1281 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1282 struct scsiback_tpg
, se_tpg
);
1283 return tpg
->tport_tpgt
;
1286 static struct se_wwn
*
1287 scsiback_make_tport(struct target_fabric_configfs
*tf
,
1288 struct config_group
*group
,
1291 struct scsiback_tport
*tport
;
1296 tport
= kzalloc(sizeof(struct scsiback_tport
), GFP_KERNEL
);
1298 return ERR_PTR(-ENOMEM
);
1300 tport
->tport_wwpn
= wwpn
;
1302 * Determine the emulated Protocol Identifier and Target Port Name
1303 * based on the incoming configfs directory name.
1305 ptr
= strstr(name
, "naa.");
1307 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1310 ptr
= strstr(name
, "fc.");
1312 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1313 off
= 3; /* Skip over "fc." */
1316 ptr
= strstr(name
, "iqn.");
1318 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1322 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name
);
1324 return ERR_PTR(-EINVAL
);
1327 if (strlen(name
) >= VSCSI_NAMELEN
) {
1328 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name
,
1329 scsiback_dump_proto_id(tport
), VSCSI_NAMELEN
);
1331 return ERR_PTR(-EINVAL
);
1333 snprintf(&tport
->tport_name
[0], VSCSI_NAMELEN
, "%s", &name
[off
]);
1335 pr_debug("Allocated emulated Target %s Address: %s\n",
1336 scsiback_dump_proto_id(tport
), name
);
1338 return &tport
->tport_wwn
;
1341 static void scsiback_drop_tport(struct se_wwn
*wwn
)
1343 struct scsiback_tport
*tport
= container_of(wwn
,
1344 struct scsiback_tport
, tport_wwn
);
1346 pr_debug("Deallocating emulated Target %s Address: %s\n",
1347 scsiback_dump_proto_id(tport
), tport
->tport_name
);
1352 static u32
scsiback_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1357 static int scsiback_check_stop_free(struct se_cmd
*se_cmd
)
1359 return transport_generic_free_cmd(se_cmd
, 0);
1362 static void scsiback_release_cmd(struct se_cmd
*se_cmd
)
1364 target_free_tag(se_cmd
->se_sess
, se_cmd
);
1367 static u32
scsiback_sess_get_index(struct se_session
*se_sess
)
1372 static int scsiback_write_pending(struct se_cmd
*se_cmd
)
1374 /* Go ahead and process the write immediately */
1375 target_execute_cmd(se_cmd
);
1380 static void scsiback_set_default_node_attrs(struct se_node_acl
*nacl
)
1384 static int scsiback_get_cmd_state(struct se_cmd
*se_cmd
)
1389 static int scsiback_queue_data_in(struct se_cmd
*se_cmd
)
1391 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1392 struct vscsibk_pend
, se_cmd
);
1394 pending_req
->result
= SAM_STAT_GOOD
;
1395 scsiback_cmd_done(pending_req
);
1399 static int scsiback_queue_status(struct se_cmd
*se_cmd
)
1401 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1402 struct vscsibk_pend
, se_cmd
);
1404 if (se_cmd
->sense_buffer
&&
1405 ((se_cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
1406 (se_cmd
->se_cmd_flags
& SCF_EMULATED_TASK_SENSE
)))
1407 pending_req
->result
= (DRIVER_SENSE
<< 24) |
1408 SAM_STAT_CHECK_CONDITION
;
1410 pending_req
->result
= se_cmd
->scsi_status
;
1412 scsiback_cmd_done(pending_req
);
1416 static void scsiback_queue_tm_rsp(struct se_cmd
*se_cmd
)
1418 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1419 struct vscsibk_pend
, se_cmd
);
1421 complete(&pending_req
->tmr_done
);
1424 static void scsiback_aborted_task(struct se_cmd
*se_cmd
)
1428 static ssize_t
scsiback_tpg_param_alias_show(struct config_item
*item
,
1431 struct se_portal_group
*se_tpg
= param_to_tpg(item
);
1432 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1436 mutex_lock(&tpg
->tv_tpg_mutex
);
1437 rb
= snprintf(page
, PAGE_SIZE
, "%s\n", tpg
->param_alias
);
1438 mutex_unlock(&tpg
->tv_tpg_mutex
);
1443 static ssize_t
scsiback_tpg_param_alias_store(struct config_item
*item
,
1444 const char *page
, size_t count
)
1446 struct se_portal_group
*se_tpg
= param_to_tpg(item
);
1447 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1451 if (strlen(page
) >= VSCSI_NAMELEN
) {
1452 pr_err("param alias: %s, exceeds max: %d\n", page
,
1457 mutex_lock(&tpg
->tv_tpg_mutex
);
1458 len
= snprintf(tpg
->param_alias
, VSCSI_NAMELEN
, "%s", page
);
1459 if (tpg
->param_alias
[len
- 1] == '\n')
1460 tpg
->param_alias
[len
- 1] = '\0';
1461 mutex_unlock(&tpg
->tv_tpg_mutex
);
1466 CONFIGFS_ATTR(scsiback_tpg_param_
, alias
);
1468 static struct configfs_attribute
*scsiback_param_attrs
[] = {
1469 &scsiback_tpg_param_attr_alias
,
1473 static int scsiback_alloc_sess_cb(struct se_portal_group
*se_tpg
,
1474 struct se_session
*se_sess
, void *p
)
1476 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1477 struct scsiback_tpg
, se_tpg
);
1483 static int scsiback_make_nexus(struct scsiback_tpg
*tpg
,
1486 struct scsiback_nexus
*tv_nexus
;
1489 mutex_lock(&tpg
->tv_tpg_mutex
);
1490 if (tpg
->tpg_nexus
) {
1491 pr_debug("tpg->tpg_nexus already exists\n");
1496 tv_nexus
= kzalloc(sizeof(struct scsiback_nexus
), GFP_KERNEL
);
1502 tv_nexus
->tvn_se_sess
= target_setup_session(&tpg
->se_tpg
,
1503 VSCSI_DEFAULT_SESSION_TAGS
,
1504 sizeof(struct vscsibk_pend
),
1505 TARGET_PROT_NORMAL
, name
,
1506 tv_nexus
, scsiback_alloc_sess_cb
);
1507 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1514 mutex_unlock(&tpg
->tv_tpg_mutex
);
1518 static int scsiback_drop_nexus(struct scsiback_tpg
*tpg
)
1520 struct se_session
*se_sess
;
1521 struct scsiback_nexus
*tv_nexus
;
1523 mutex_lock(&tpg
->tv_tpg_mutex
);
1524 tv_nexus
= tpg
->tpg_nexus
;
1526 mutex_unlock(&tpg
->tv_tpg_mutex
);
1530 se_sess
= tv_nexus
->tvn_se_sess
;
1532 mutex_unlock(&tpg
->tv_tpg_mutex
);
1536 if (tpg
->tv_tpg_port_count
!= 0) {
1537 mutex_unlock(&tpg
->tv_tpg_mutex
);
1538 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1539 tpg
->tv_tpg_port_count
);
1543 if (tpg
->tv_tpg_fe_count
!= 0) {
1544 mutex_unlock(&tpg
->tv_tpg_mutex
);
1545 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1546 tpg
->tv_tpg_fe_count
);
1550 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1551 scsiback_dump_proto_id(tpg
->tport
),
1552 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1555 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1557 target_remove_session(se_sess
);
1558 tpg
->tpg_nexus
= NULL
;
1559 mutex_unlock(&tpg
->tv_tpg_mutex
);
1565 static ssize_t
scsiback_tpg_nexus_show(struct config_item
*item
, char *page
)
1567 struct se_portal_group
*se_tpg
= to_tpg(item
);
1568 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1569 struct scsiback_tpg
, se_tpg
);
1570 struct scsiback_nexus
*tv_nexus
;
1573 mutex_lock(&tpg
->tv_tpg_mutex
);
1574 tv_nexus
= tpg
->tpg_nexus
;
1576 mutex_unlock(&tpg
->tv_tpg_mutex
);
1579 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1580 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1581 mutex_unlock(&tpg
->tv_tpg_mutex
);
1586 static ssize_t
scsiback_tpg_nexus_store(struct config_item
*item
,
1587 const char *page
, size_t count
)
1589 struct se_portal_group
*se_tpg
= to_tpg(item
);
1590 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1591 struct scsiback_tpg
, se_tpg
);
1592 struct scsiback_tport
*tport_wwn
= tpg
->tport
;
1593 unsigned char i_port
[VSCSI_NAMELEN
], *ptr
, *port_ptr
;
1596 * Shutdown the active I_T nexus if 'NULL' is passed.
1598 if (!strncmp(page
, "NULL", 4)) {
1599 ret
= scsiback_drop_nexus(tpg
);
1600 return (!ret
) ? count
: ret
;
1603 * Otherwise make sure the passed virtual Initiator port WWN matches
1604 * the fabric protocol_id set in scsiback_make_tport(), and call
1605 * scsiback_make_nexus().
1607 if (strlen(page
) >= VSCSI_NAMELEN
) {
1608 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1609 page
, VSCSI_NAMELEN
);
1612 snprintf(&i_port
[0], VSCSI_NAMELEN
, "%s", page
);
1614 ptr
= strstr(i_port
, "naa.");
1616 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1617 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1618 i_port
, scsiback_dump_proto_id(tport_wwn
));
1621 port_ptr
= &i_port
[0];
1624 ptr
= strstr(i_port
, "fc.");
1626 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1627 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1628 i_port
, scsiback_dump_proto_id(tport_wwn
));
1631 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1634 ptr
= strstr(i_port
, "iqn.");
1636 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1637 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1638 i_port
, scsiback_dump_proto_id(tport_wwn
));
1641 port_ptr
= &i_port
[0];
1644 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1648 * Clear any trailing newline for the NAA WWN
1651 if (i_port
[strlen(i_port
) - 1] == '\n')
1652 i_port
[strlen(i_port
) - 1] = '\0';
1654 ret
= scsiback_make_nexus(tpg
, port_ptr
);
1661 CONFIGFS_ATTR(scsiback_tpg_
, nexus
);
1663 static struct configfs_attribute
*scsiback_tpg_attrs
[] = {
1664 &scsiback_tpg_attr_nexus
,
1669 scsiback_wwn_version_show(struct config_item
*item
, char *page
)
1671 return sprintf(page
, "xen-pvscsi fabric module %s on %s/%s on "
1673 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1676 CONFIGFS_ATTR_RO(scsiback_wwn_
, version
);
1678 static struct configfs_attribute
*scsiback_wwn_attrs
[] = {
1679 &scsiback_wwn_attr_version
,
1683 static int scsiback_port_link(struct se_portal_group
*se_tpg
,
1686 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1687 struct scsiback_tpg
, se_tpg
);
1689 mutex_lock(&tpg
->tv_tpg_mutex
);
1690 tpg
->tv_tpg_port_count
++;
1691 mutex_unlock(&tpg
->tv_tpg_mutex
);
1696 static void scsiback_port_unlink(struct se_portal_group
*se_tpg
,
1699 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1700 struct scsiback_tpg
, se_tpg
);
1702 mutex_lock(&tpg
->tv_tpg_mutex
);
1703 tpg
->tv_tpg_port_count
--;
1704 mutex_unlock(&tpg
->tv_tpg_mutex
);
1707 static struct se_portal_group
*
1708 scsiback_make_tpg(struct se_wwn
*wwn
, const char *name
)
1710 struct scsiback_tport
*tport
= container_of(wwn
,
1711 struct scsiback_tport
, tport_wwn
);
1713 struct scsiback_tpg
*tpg
;
1717 if (strstr(name
, "tpgt_") != name
)
1718 return ERR_PTR(-EINVAL
);
1719 ret
= kstrtou16(name
+ 5, 10, &tpgt
);
1721 return ERR_PTR(ret
);
1723 tpg
= kzalloc(sizeof(struct scsiback_tpg
), GFP_KERNEL
);
1725 return ERR_PTR(-ENOMEM
);
1727 mutex_init(&tpg
->tv_tpg_mutex
);
1728 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1729 INIT_LIST_HEAD(&tpg
->info_list
);
1731 tpg
->tport_tpgt
= tpgt
;
1733 ret
= core_tpg_register(wwn
, &tpg
->se_tpg
, tport
->tport_proto_id
);
1738 mutex_lock(&scsiback_mutex
);
1739 list_add_tail(&tpg
->tv_tpg_list
, &scsiback_list
);
1740 mutex_unlock(&scsiback_mutex
);
1742 return &tpg
->se_tpg
;
1745 static void scsiback_drop_tpg(struct se_portal_group
*se_tpg
)
1747 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1748 struct scsiback_tpg
, se_tpg
);
1750 mutex_lock(&scsiback_mutex
);
1751 list_del(&tpg
->tv_tpg_list
);
1752 mutex_unlock(&scsiback_mutex
);
1754 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1756 scsiback_drop_nexus(tpg
);
1758 * Deregister the se_tpg from TCM.
1760 core_tpg_deregister(se_tpg
);
1764 static int scsiback_check_true(struct se_portal_group
*se_tpg
)
1769 static int scsiback_check_false(struct se_portal_group
*se_tpg
)
1774 static const struct target_core_fabric_ops scsiback_ops
= {
1775 .module
= THIS_MODULE
,
1776 .fabric_name
= "xen-pvscsi",
1777 .tpg_get_wwn
= scsiback_get_fabric_wwn
,
1778 .tpg_get_tag
= scsiback_get_tag
,
1779 .tpg_check_demo_mode
= scsiback_check_true
,
1780 .tpg_check_demo_mode_cache
= scsiback_check_true
,
1781 .tpg_check_demo_mode_write_protect
= scsiback_check_false
,
1782 .tpg_check_prod_mode_write_protect
= scsiback_check_false
,
1783 .tpg_get_inst_index
= scsiback_tpg_get_inst_index
,
1784 .check_stop_free
= scsiback_check_stop_free
,
1785 .release_cmd
= scsiback_release_cmd
,
1786 .sess_get_index
= scsiback_sess_get_index
,
1787 .sess_get_initiator_sid
= NULL
,
1788 .write_pending
= scsiback_write_pending
,
1789 .set_default_node_attributes
= scsiback_set_default_node_attrs
,
1790 .get_cmd_state
= scsiback_get_cmd_state
,
1791 .queue_data_in
= scsiback_queue_data_in
,
1792 .queue_status
= scsiback_queue_status
,
1793 .queue_tm_rsp
= scsiback_queue_tm_rsp
,
1794 .aborted_task
= scsiback_aborted_task
,
1796 * Setup callers for generic logic in target_core_fabric_configfs.c
1798 .fabric_make_wwn
= scsiback_make_tport
,
1799 .fabric_drop_wwn
= scsiback_drop_tport
,
1800 .fabric_make_tpg
= scsiback_make_tpg
,
1801 .fabric_drop_tpg
= scsiback_drop_tpg
,
1802 .fabric_post_link
= scsiback_port_link
,
1803 .fabric_pre_unlink
= scsiback_port_unlink
,
1805 .tfc_wwn_attrs
= scsiback_wwn_attrs
,
1806 .tfc_tpg_base_attrs
= scsiback_tpg_attrs
,
1807 .tfc_tpg_param_attrs
= scsiback_param_attrs
,
1810 static const struct xenbus_device_id scsiback_ids
[] = {
1815 static struct xenbus_driver scsiback_driver
= {
1816 .ids
= scsiback_ids
,
1817 .probe
= scsiback_probe
,
1818 .remove
= scsiback_remove
,
1819 .otherend_changed
= scsiback_frontend_changed
1822 static int __init
scsiback_init(void)
1829 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE
"\n",
1830 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1832 ret
= xenbus_register_backend(&scsiback_driver
);
1836 ret
= target_register_template(&scsiback_ops
);
1838 goto out_unregister_xenbus
;
1842 out_unregister_xenbus
:
1843 xenbus_unregister_driver(&scsiback_driver
);
1845 pr_err("%s: error %d\n", __func__
, ret
);
1849 static void __exit
scsiback_exit(void)
1851 target_unregister_template(&scsiback_ops
);
1852 xenbus_unregister_driver(&scsiback_driver
);
1855 module_init(scsiback_init
);
1856 module_exit(scsiback_exit
);
1858 MODULE_DESCRIPTION("Xen SCSI backend driver");
1859 MODULE_LICENSE("Dual BSD/GPL");
1860 MODULE_ALIAS("xen-backend:vscsi");
1861 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");