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
;
97 atomic_t nr_unreplied_reqs
;
100 struct list_head v2p_entry_lists
;
102 wait_queue_head_t waiting_to_free
;
105 /* theoretical maximum of grants for one request */
106 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
109 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
110 * call to map/unmap grants. Don't choose it too large, as there are arrays
111 * with VSCSI_GRANT_BATCH elements allocated on the stack.
113 #define VSCSI_GRANT_BATCH 16
115 struct vscsibk_pend
{
118 uint8_t cmnd
[VSCSIIF_MAX_COMMAND_SIZE
];
121 uint8_t sc_data_direction
;
122 uint16_t n_sg
; /* real length of SG list */
123 uint16_t n_grants
; /* SG pages and potentially SG list */
127 struct vscsibk_info
*info
;
128 struct v2p_entry
*v2p
;
129 struct scatterlist
*sgl
;
131 uint8_t sense_buffer
[VSCSIIF_SENSE_BUFFERSIZE
];
133 grant_handle_t grant_handles
[VSCSI_MAX_GRANTS
];
134 struct page
*pages
[VSCSI_MAX_GRANTS
];
136 struct se_cmd se_cmd
;
138 struct completion tmr_done
;
141 #define VSCSI_DEFAULT_SESSION_TAGS 128
143 struct scsiback_nexus
{
144 /* Pointer to TCM session for I_T Nexus */
145 struct se_session
*tvn_se_sess
;
148 struct scsiback_tport
{
149 /* SCSI protocol the tport is providing */
151 /* Binary World Wide unique Port Name for pvscsi Target port */
153 /* ASCII formatted WWPN for pvscsi Target port */
154 char tport_name
[VSCSI_NAMELEN
];
155 /* Returned by scsiback_make_tport() */
156 struct se_wwn tport_wwn
;
159 struct scsiback_tpg
{
160 /* scsiback port target portal group tag for TCM */
162 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
163 int tv_tpg_port_count
;
164 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
166 /* list for scsiback_list */
167 struct list_head tv_tpg_list
;
168 /* Used to protect access for tpg_nexus */
169 struct mutex tv_tpg_mutex
;
170 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
171 struct scsiback_nexus
*tpg_nexus
;
172 /* Pointer back to scsiback_tport */
173 struct scsiback_tport
*tport
;
174 /* Returned by scsiback_make_tpg() */
175 struct se_portal_group se_tpg
;
176 /* alias used in xenstore */
177 char param_alias
[VSCSI_NAMELEN
];
178 /* list of info structures related to this target portal group */
179 struct list_head info_list
;
182 #define SCSIBACK_INVALID_HANDLE (~0)
184 static bool log_print_stat
;
185 module_param(log_print_stat
, bool, 0644);
187 static int scsiback_max_buffer_pages
= 1024;
188 module_param_named(max_buffer_pages
, scsiback_max_buffer_pages
, int, 0644);
189 MODULE_PARM_DESC(max_buffer_pages
,
190 "Maximum number of free pages to keep in backend buffer");
192 static DEFINE_SPINLOCK(free_pages_lock
);
193 static int free_pages_num
;
194 static LIST_HEAD(scsiback_free_pages
);
196 /* Global spinlock to protect scsiback TPG list */
197 static DEFINE_MUTEX(scsiback_mutex
);
198 static LIST_HEAD(scsiback_list
);
200 static void scsiback_get(struct vscsibk_info
*info
)
202 atomic_inc(&info
->nr_unreplied_reqs
);
205 static void scsiback_put(struct vscsibk_info
*info
)
207 if (atomic_dec_and_test(&info
->nr_unreplied_reqs
))
208 wake_up(&info
->waiting_to_free
);
211 static void put_free_pages(struct page
**page
, int num
)
214 int i
= free_pages_num
+ num
, n
= num
;
218 if (i
> scsiback_max_buffer_pages
) {
219 n
= min(num
, i
- scsiback_max_buffer_pages
);
220 gnttab_free_pages(n
, page
+ num
- n
);
223 spin_lock_irqsave(&free_pages_lock
, flags
);
224 for (i
= 0; i
< n
; i
++)
225 list_add(&page
[i
]->lru
, &scsiback_free_pages
);
227 spin_unlock_irqrestore(&free_pages_lock
, flags
);
230 static int get_free_page(struct page
**page
)
234 spin_lock_irqsave(&free_pages_lock
, flags
);
235 if (list_empty(&scsiback_free_pages
)) {
236 spin_unlock_irqrestore(&free_pages_lock
, flags
);
237 return gnttab_alloc_pages(1, page
);
239 page
[0] = list_first_entry(&scsiback_free_pages
, struct page
, lru
);
240 list_del(&page
[0]->lru
);
242 spin_unlock_irqrestore(&free_pages_lock
, flags
);
246 static unsigned long vaddr_page(struct page
*page
)
248 unsigned long pfn
= page_to_pfn(page
);
250 return (unsigned long)pfn_to_kaddr(pfn
);
253 static unsigned long vaddr(struct vscsibk_pend
*req
, int seg
)
255 return vaddr_page(req
->pages
[seg
]);
258 static void scsiback_print_status(char *sense_buffer
, int errors
,
259 struct vscsibk_pend
*pending_req
)
261 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
263 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
264 tpg
->tport
->tport_name
, pending_req
->v2p
->lun
,
265 pending_req
->cmnd
[0], status_byte(errors
), msg_byte(errors
),
266 host_byte(errors
), driver_byte(errors
));
269 static void scsiback_fast_flush_area(struct vscsibk_pend
*req
)
271 struct gnttab_unmap_grant_ref unmap
[VSCSI_GRANT_BATCH
];
272 struct page
*pages
[VSCSI_GRANT_BATCH
];
273 unsigned int i
, invcount
= 0;
274 grant_handle_t handle
;
284 for (i
= 0; i
< req
->n_grants
; i
++) {
285 handle
= req
->grant_handles
[i
];
286 if (handle
== SCSIBACK_INVALID_HANDLE
)
288 gnttab_set_unmap_op(&unmap
[invcount
], vaddr(req
, i
),
289 GNTMAP_host_map
, handle
);
290 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
291 pages
[invcount
] = req
->pages
[i
];
292 put_page(pages
[invcount
]);
294 if (invcount
< VSCSI_GRANT_BATCH
)
296 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
302 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
306 put_free_pages(req
->pages
, req
->n_grants
);
310 static void scsiback_free_translation_entry(struct kref
*kref
)
312 struct v2p_entry
*entry
= container_of(kref
, struct v2p_entry
, kref
);
313 struct scsiback_tpg
*tpg
= entry
->tpg
;
315 mutex_lock(&tpg
->tv_tpg_mutex
);
316 tpg
->tv_tpg_fe_count
--;
317 mutex_unlock(&tpg
->tv_tpg_mutex
);
322 static void scsiback_send_response(struct vscsibk_info
*info
,
323 char *sense_buffer
, int32_t result
, uint32_t resid
,
326 struct vscsiif_response
*ring_res
;
328 struct scsi_sense_hdr sshdr
;
332 spin_lock_irqsave(&info
->ring_lock
, flags
);
334 ring_res
= RING_GET_RESPONSE(&info
->ring
, info
->ring
.rsp_prod_pvt
);
335 info
->ring
.rsp_prod_pvt
++;
337 ring_res
->rslt
= result
;
338 ring_res
->rqid
= rqid
;
340 if (sense_buffer
!= NULL
&&
341 scsi_normalize_sense(sense_buffer
, VSCSIIF_SENSE_BUFFERSIZE
,
343 len
= min_t(unsigned, 8 + sense_buffer
[7],
344 VSCSIIF_SENSE_BUFFERSIZE
);
345 memcpy(ring_res
->sense_buffer
, sense_buffer
, len
);
346 ring_res
->sense_len
= len
;
348 ring_res
->sense_len
= 0;
351 ring_res
->residual_len
= resid
;
353 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info
->ring
, notify
);
354 spin_unlock_irqrestore(&info
->ring_lock
, flags
);
357 notify_remote_via_irq(info
->irq
);
360 static void scsiback_do_resp_with_sense(char *sense_buffer
, int32_t result
,
361 uint32_t resid
, struct vscsibk_pend
*pending_req
)
363 scsiback_send_response(pending_req
->info
, sense_buffer
, result
,
364 resid
, pending_req
->rqid
);
366 if (pending_req
->v2p
)
367 kref_put(&pending_req
->v2p
->kref
,
368 scsiback_free_translation_entry
);
371 static void scsiback_cmd_done(struct vscsibk_pend
*pending_req
)
373 struct vscsibk_info
*info
= pending_req
->info
;
374 unsigned char *sense_buffer
;
378 sense_buffer
= pending_req
->sense_buffer
;
379 resid
= pending_req
->se_cmd
.residual_count
;
380 errors
= pending_req
->result
;
382 if (errors
&& log_print_stat
)
383 scsiback_print_status(sense_buffer
, errors
, pending_req
);
385 scsiback_fast_flush_area(pending_req
);
386 scsiback_do_resp_with_sense(sense_buffer
, errors
, resid
, pending_req
);
389 * Drop the extra KREF_ACK reference taken by target_submit_cmd_map_sgls()
390 * ahead of scsiback_check_stop_free() -> transport_generic_free_cmd()
391 * final se_cmd->cmd_kref put.
393 target_put_sess_cmd(&pending_req
->se_cmd
);
396 static void scsiback_cmd_exec(struct vscsibk_pend
*pending_req
)
398 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
399 struct se_session
*sess
= pending_req
->v2p
->tpg
->tpg_nexus
->tvn_se_sess
;
402 scsiback_get(pending_req
->info
);
403 se_cmd
->tag
= pending_req
->rqid
;
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
, TARGET_SCF_ACK_KREF
,
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("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("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 pr_debug("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 pr_debug("invalid nr_seg = %d\n", nr_segments
);
523 /* free of (sgl) in fast_flush_area() */
524 pending_req
->sgl
= kmalloc_array(nr_segments
,
525 sizeof(struct scatterlist
), GFP_KERNEL
);
526 if (!pending_req
->sgl
)
529 sg_init_table(pending_req
->sgl
, nr_segments
);
530 pending_req
->n_sg
= nr_segments
;
532 flags
= GNTMAP_host_map
;
533 if (pending_req
->sc_data_direction
== DMA_TO_DEVICE
)
534 flags
|= GNTMAP_readonly
;
536 pg
= pending_req
->pages
+ nr_sgl
;
537 grant
= pending_req
->grant_handles
+ nr_sgl
;
540 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
541 pg
, grant
, nr_segments
, flags
);
545 for (i
= 0; i
< nr_sgl
; i
++) {
546 seg
= (struct scsiif_request_segment
*)(
547 vaddr(pending_req
, i
) + ring_req
->seg
[i
].offset
);
548 n_segs
= ring_req
->seg
[i
].length
/
549 sizeof(struct scsiif_request_segment
);
550 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
551 pg
, grant
, n_segs
, flags
);
557 end_seg
= vaddr(pending_req
, 0) + ring_req
->seg
[0].offset
;
558 seg
= (struct scsiif_request_segment
*)end_seg
;
559 end_seg
+= ring_req
->seg
[0].length
;
560 pg
= pending_req
->pages
+ nr_sgl
;
563 for_each_sg(pending_req
->sgl
, sg
, nr_segments
, i
) {
564 sg_set_page(sg
, pg
[i
], seg
->length
, seg
->offset
);
565 pending_req
->data_len
+= seg
->length
;
567 if (nr_sgl
&& (unsigned long)seg
>= end_seg
) {
569 end_seg
= vaddr(pending_req
, i_seg
) +
570 ring_req
->seg
[i_seg
].offset
;
571 seg
= (struct scsiif_request_segment
*)end_seg
;
572 end_seg
+= ring_req
->seg
[i_seg
].length
;
574 if (sg
->offset
>= PAGE_SIZE
||
575 sg
->length
> PAGE_SIZE
||
576 sg
->offset
+ sg
->length
> PAGE_SIZE
)
583 static void scsiback_disconnect(struct vscsibk_info
*info
)
585 wait_event(info
->waiting_to_free
,
586 atomic_read(&info
->nr_unreplied_reqs
) == 0);
588 unbind_from_irqhandler(info
->irq
, info
);
590 xenbus_unmap_ring_vfree(info
->dev
, info
->ring
.sring
);
593 static void scsiback_device_action(struct vscsibk_pend
*pending_req
,
594 enum tcm_tmreq_table act
, int tag
)
596 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
597 struct scsiback_nexus
*nexus
= tpg
->tpg_nexus
;
598 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
599 u64 unpacked_lun
= pending_req
->v2p
->lun
;
600 int rc
, err
= FAILED
;
602 init_completion(&pending_req
->tmr_done
);
604 rc
= target_submit_tmr(&pending_req
->se_cmd
, nexus
->tvn_se_sess
,
605 &pending_req
->sense_buffer
[0],
606 unpacked_lun
, NULL
, act
, GFP_KERNEL
,
607 tag
, TARGET_SCF_ACK_KREF
);
611 wait_for_completion(&pending_req
->tmr_done
);
613 err
= (se_cmd
->se_tmr_req
->response
== TMR_FUNCTION_COMPLETE
) ?
616 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
617 transport_generic_free_cmd(&pending_req
->se_cmd
, 0);
621 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
625 Perform virtual to physical translation
627 static struct v2p_entry
*scsiback_do_translation(struct vscsibk_info
*info
,
630 struct v2p_entry
*entry
;
631 struct list_head
*head
= &(info
->v2p_entry_lists
);
634 spin_lock_irqsave(&info
->v2p_lock
, flags
);
635 list_for_each_entry(entry
, head
, l
) {
636 if ((entry
->v
.chn
== v
->chn
) &&
637 (entry
->v
.tgt
== v
->tgt
) &&
638 (entry
->v
.lun
== v
->lun
)) {
639 kref_get(&entry
->kref
);
646 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
650 static struct vscsibk_pend
*scsiback_get_pend_req(struct vscsiif_back_ring
*ring
,
651 struct v2p_entry
*v2p
)
653 struct scsiback_tpg
*tpg
= v2p
->tpg
;
654 struct scsiback_nexus
*nexus
= tpg
->tpg_nexus
;
655 struct se_session
*se_sess
= nexus
->tvn_se_sess
;
656 struct vscsibk_pend
*req
;
659 tag
= percpu_ida_alloc(&se_sess
->sess_tag_pool
, TASK_RUNNING
);
661 pr_err("Unable to obtain tag for vscsiif_request\n");
662 return ERR_PTR(-ENOMEM
);
665 req
= &((struct vscsibk_pend
*)se_sess
->sess_cmd_map
)[tag
];
666 memset(req
, 0, sizeof(*req
));
667 req
->se_cmd
.map_tag
= tag
;
669 for (i
= 0; i
< VSCSI_MAX_GRANTS
; i
++)
670 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
675 static struct vscsibk_pend
*prepare_pending_reqs(struct vscsibk_info
*info
,
676 struct vscsiif_back_ring
*ring
,
677 struct vscsiif_request
*ring_req
)
679 struct vscsibk_pend
*pending_req
;
680 struct v2p_entry
*v2p
;
681 struct ids_tuple vir
;
683 /* request range check from frontend */
684 if ((ring_req
->sc_data_direction
!= DMA_BIDIRECTIONAL
) &&
685 (ring_req
->sc_data_direction
!= DMA_TO_DEVICE
) &&
686 (ring_req
->sc_data_direction
!= DMA_FROM_DEVICE
) &&
687 (ring_req
->sc_data_direction
!= DMA_NONE
)) {
688 pr_debug("invalid parameter data_dir = %d\n",
689 ring_req
->sc_data_direction
);
690 return ERR_PTR(-EINVAL
);
692 if (ring_req
->cmd_len
> VSCSIIF_MAX_COMMAND_SIZE
) {
693 pr_debug("invalid parameter cmd_len = %d\n",
695 return ERR_PTR(-EINVAL
);
698 vir
.chn
= ring_req
->channel
;
699 vir
.tgt
= ring_req
->id
;
700 vir
.lun
= ring_req
->lun
;
702 v2p
= scsiback_do_translation(info
, &vir
);
704 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
705 vir
.chn
, vir
.tgt
, vir
.lun
);
706 return ERR_PTR(-ENODEV
);
709 pending_req
= scsiback_get_pend_req(ring
, v2p
);
710 if (IS_ERR(pending_req
)) {
711 kref_put(&v2p
->kref
, scsiback_free_translation_entry
);
712 return ERR_PTR(-ENOMEM
);
714 pending_req
->rqid
= ring_req
->rqid
;
715 pending_req
->info
= info
;
716 pending_req
->v2p
= v2p
;
717 pending_req
->sc_data_direction
= ring_req
->sc_data_direction
;
718 pending_req
->cmd_len
= ring_req
->cmd_len
;
719 memcpy(pending_req
->cmnd
, ring_req
->cmnd
, pending_req
->cmd_len
);
724 static int scsiback_do_cmd_fn(struct vscsibk_info
*info
)
726 struct vscsiif_back_ring
*ring
= &info
->ring
;
727 struct vscsiif_request ring_req
;
728 struct vscsibk_pend
*pending_req
;
734 rp
= ring
->sring
->req_prod
;
735 rmb(); /* guest system is accessing ring, too */
737 if (RING_REQUEST_PROD_OVERFLOW(ring
, rp
)) {
738 rc
= ring
->rsp_prod_pvt
;
739 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
740 info
->domid
, rp
, rc
, rp
- rc
);
741 info
->ring_error
= 1;
746 if (RING_REQUEST_CONS_OVERFLOW(ring
, rc
))
749 RING_COPY_REQUEST(ring
, rc
, &ring_req
);
750 ring
->req_cons
= ++rc
;
752 pending_req
= prepare_pending_reqs(info
, ring
, &ring_req
);
753 if (IS_ERR(pending_req
)) {
754 switch (PTR_ERR(pending_req
)) {
756 result
= DID_NO_CONNECT
;
759 result
= DRIVER_ERROR
;
762 scsiback_send_response(info
, NULL
, result
<< 24, 0,
767 switch (ring_req
.act
) {
768 case VSCSIIF_ACT_SCSI_CDB
:
769 if (scsiback_gnttab_data_map(&ring_req
, pending_req
)) {
770 scsiback_fast_flush_area(pending_req
);
771 scsiback_do_resp_with_sense(NULL
,
772 DRIVER_ERROR
<< 24, 0, pending_req
);
773 transport_generic_free_cmd(&pending_req
->se_cmd
, 0);
775 scsiback_cmd_exec(pending_req
);
778 case VSCSIIF_ACT_SCSI_ABORT
:
779 scsiback_device_action(pending_req
, TMR_ABORT_TASK
,
782 case VSCSIIF_ACT_SCSI_RESET
:
783 scsiback_device_action(pending_req
, TMR_LUN_RESET
, 0);
786 pr_err_ratelimited("invalid request\n");
787 scsiback_do_resp_with_sense(NULL
, DRIVER_ERROR
<< 24, 0,
789 transport_generic_free_cmd(&pending_req
->se_cmd
, 0);
793 /* Yield point for this unbounded loop. */
797 RING_FINAL_CHECK_FOR_REQUESTS(&info
->ring
, more_to_do
);
801 static irqreturn_t
scsiback_irq_fn(int irq
, void *dev_id
)
803 struct vscsibk_info
*info
= dev_id
;
805 if (info
->ring_error
)
808 while (scsiback_do_cmd_fn(info
))
814 static int scsiback_init_sring(struct vscsibk_info
*info
, grant_ref_t ring_ref
,
815 evtchn_port_t evtchn
)
818 struct vscsiif_sring
*sring
;
824 err
= xenbus_map_ring_valloc(info
->dev
, &ring_ref
, 1, &area
);
828 sring
= (struct vscsiif_sring
*)area
;
829 BACK_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
831 err
= bind_interdomain_evtchn_to_irq(info
->domid
, evtchn
);
837 err
= request_threaded_irq(info
->irq
, NULL
, scsiback_irq_fn
,
838 IRQF_ONESHOT
, "vscsiif-backend", info
);
845 unbind_from_irqhandler(info
->irq
, info
);
848 xenbus_unmap_ring_vfree(info
->dev
, area
);
853 static int scsiback_map(struct vscsibk_info
*info
)
855 struct xenbus_device
*dev
= info
->dev
;
856 unsigned int ring_ref
, evtchn
;
859 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
860 "ring-ref", "%u", &ring_ref
,
861 "event-channel", "%u", &evtchn
, NULL
);
863 xenbus_dev_fatal(dev
, err
, "reading %s ring", dev
->otherend
);
867 return scsiback_init_sring(info
, ring_ref
, evtchn
);
871 Check for a translation entry being present
873 static struct v2p_entry
*scsiback_chk_translation_entry(
874 struct vscsibk_info
*info
, struct ids_tuple
*v
)
876 struct list_head
*head
= &(info
->v2p_entry_lists
);
877 struct v2p_entry
*entry
;
879 list_for_each_entry(entry
, head
, l
)
880 if ((entry
->v
.chn
== v
->chn
) &&
881 (entry
->v
.tgt
== v
->tgt
) &&
882 (entry
->v
.lun
== v
->lun
))
889 Add a new translation entry
891 static int scsiback_add_translation_entry(struct vscsibk_info
*info
,
892 char *phy
, struct ids_tuple
*v
)
895 struct v2p_entry
*new;
898 unsigned long long unpacked_lun
;
899 struct se_lun
*se_lun
;
900 struct scsiback_tpg
*tpg_entry
, *tpg
= NULL
;
901 char *error
= "doesn't exist";
903 lunp
= strrchr(phy
, ':');
905 pr_err("illegal format of physical device %s\n", phy
);
910 err
= kstrtoull(lunp
, 10, &unpacked_lun
);
912 pr_err("lun number not valid: %s\n", lunp
);
916 mutex_lock(&scsiback_mutex
);
917 list_for_each_entry(tpg_entry
, &scsiback_list
, tv_tpg_list
) {
918 if (!strcmp(phy
, tpg_entry
->tport
->tport_name
) ||
919 !strcmp(phy
, tpg_entry
->param_alias
)) {
920 mutex_lock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
921 hlist_for_each_entry(se_lun
, &tpg_entry
->se_tpg
.tpg_lun_hlist
, link
) {
922 if (se_lun
->unpacked_lun
== unpacked_lun
) {
923 if (!tpg_entry
->tpg_nexus
)
924 error
= "nexus undefined";
930 mutex_unlock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
935 mutex_lock(&tpg
->tv_tpg_mutex
);
936 tpg
->tv_tpg_fe_count
++;
937 mutex_unlock(&tpg
->tv_tpg_mutex
);
939 mutex_unlock(&scsiback_mutex
);
942 pr_err("%s:%llu %s\n", phy
, unpacked_lun
, error
);
946 new = kmalloc(sizeof(struct v2p_entry
), GFP_KERNEL
);
952 spin_lock_irqsave(&info
->v2p_lock
, flags
);
954 /* Check double assignment to identical virtual ID */
955 if (scsiback_chk_translation_entry(info
, v
)) {
956 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
961 /* Create a new translation entry and add to the list */
962 kref_init(&new->kref
);
965 new->lun
= unpacked_lun
;
966 list_add_tail(&new->l
, &info
->v2p_entry_lists
);
969 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
973 mutex_lock(&tpg
->tv_tpg_mutex
);
974 tpg
->tv_tpg_fe_count
--;
975 mutex_unlock(&tpg
->tv_tpg_mutex
);
982 static void __scsiback_del_translation_entry(struct v2p_entry
*entry
)
985 kref_put(&entry
->kref
, scsiback_free_translation_entry
);
989 Delete the translation entry specified
991 static int scsiback_del_translation_entry(struct vscsibk_info
*info
,
994 struct v2p_entry
*entry
;
998 spin_lock_irqsave(&info
->v2p_lock
, flags
);
999 /* Find out the translation entry specified */
1000 entry
= scsiback_chk_translation_entry(info
, v
);
1002 __scsiback_del_translation_entry(entry
);
1006 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1010 static void scsiback_do_add_lun(struct vscsibk_info
*info
, const char *state
,
1011 char *phy
, struct ids_tuple
*vir
, int try)
1013 struct v2p_entry
*entry
;
1014 unsigned long flags
;
1018 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1019 entry
= scsiback_chk_translation_entry(info
, vir
);
1020 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1024 if (!scsiback_add_translation_entry(info
, phy
, vir
)) {
1025 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1026 "%d", XenbusStateInitialised
)) {
1027 pr_err("xenbus_printf error %s\n", state
);
1028 scsiback_del_translation_entry(info
, vir
);
1031 err
= xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1032 "%d", XenbusStateClosed
);
1034 xenbus_dev_error(info
->dev
, err
,
1035 "%s: writing %s", __func__
, state
);
1039 static void scsiback_do_del_lun(struct vscsibk_info
*info
, const char *state
,
1040 struct ids_tuple
*vir
)
1042 if (!scsiback_del_translation_entry(info
, vir
)) {
1043 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1044 "%d", XenbusStateClosed
))
1045 pr_err("xenbus_printf error %s\n", state
);
1049 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1050 #define VSCSIBACK_OP_UPDATEDEV_STATE 2
1052 static void scsiback_do_1lun_hotplug(struct vscsibk_info
*info
, int op
,
1056 struct ids_tuple vir
;
1059 char phy
[VSCSI_NAMELEN
];
1062 struct xenbus_device
*dev
= info
->dev
;
1065 snprintf(state
, sizeof(state
), "vscsi-devs/%s/state", ent
);
1066 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, state
, "%u", &device_state
);
1067 if (XENBUS_EXIST_ERR(err
))
1070 /* physical SCSI device */
1071 snprintf(str
, sizeof(str
), "vscsi-devs/%s/p-dev", ent
);
1072 val
= xenbus_read(XBT_NIL
, dev
->nodename
, str
, NULL
);
1074 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1075 "%d", XenbusStateClosed
);
1077 xenbus_dev_error(info
->dev
, err
,
1078 "%s: writing %s", __func__
, state
);
1081 strlcpy(phy
, val
, VSCSI_NAMELEN
);
1084 /* virtual SCSI device */
1085 snprintf(str
, sizeof(str
), "vscsi-devs/%s/v-dev", ent
);
1086 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, str
, "%u:%u:%u:%u",
1087 &vir
.hst
, &vir
.chn
, &vir
.tgt
, &vir
.lun
);
1088 if (XENBUS_EXIST_ERR(err
)) {
1089 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1090 "%d", XenbusStateClosed
);
1092 xenbus_dev_error(info
->dev
, err
,
1093 "%s: writing %s", __func__
, state
);
1098 case VSCSIBACK_OP_ADD_OR_DEL_LUN
:
1099 switch (device_state
) {
1100 case XenbusStateInitialising
:
1101 scsiback_do_add_lun(info
, state
, phy
, &vir
, 0);
1103 case XenbusStateConnected
:
1104 scsiback_do_add_lun(info
, state
, phy
, &vir
, 1);
1106 case XenbusStateClosing
:
1107 scsiback_do_del_lun(info
, state
, &vir
);
1114 case VSCSIBACK_OP_UPDATEDEV_STATE
:
1115 if (device_state
== XenbusStateInitialised
) {
1116 /* modify vscsi-devs/dev-x/state */
1117 if (xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1118 "%d", XenbusStateConnected
)) {
1119 pr_err("xenbus_printf error %s\n", str
);
1120 scsiback_del_translation_entry(info
, &vir
);
1121 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1122 "%d", XenbusStateClosed
);
1126 /* When it is necessary, processing is added here. */
1132 static void scsiback_do_lun_hotplug(struct vscsibk_info
*info
, int op
)
1136 unsigned int ndir
= 0;
1138 dir
= xenbus_directory(XBT_NIL
, info
->dev
->nodename
, "vscsi-devs",
1143 for (i
= 0; i
< ndir
; i
++)
1144 scsiback_do_1lun_hotplug(info
, op
, dir
[i
]);
1149 static void scsiback_frontend_changed(struct xenbus_device
*dev
,
1150 enum xenbus_state frontend_state
)
1152 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1154 switch (frontend_state
) {
1155 case XenbusStateInitialising
:
1158 case XenbusStateInitialised
:
1159 if (scsiback_map(info
))
1162 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1163 xenbus_switch_state(dev
, XenbusStateConnected
);
1166 case XenbusStateConnected
:
1167 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_UPDATEDEV_STATE
);
1169 if (dev
->state
== XenbusStateConnected
)
1172 xenbus_switch_state(dev
, XenbusStateConnected
);
1175 case XenbusStateClosing
:
1177 scsiback_disconnect(info
);
1179 xenbus_switch_state(dev
, XenbusStateClosing
);
1182 case XenbusStateClosed
:
1183 xenbus_switch_state(dev
, XenbusStateClosed
);
1184 if (xenbus_dev_is_online(dev
))
1186 /* fall through if not online */
1187 case XenbusStateUnknown
:
1188 device_unregister(&dev
->dev
);
1191 case XenbusStateReconfiguring
:
1192 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1193 xenbus_switch_state(dev
, XenbusStateReconfigured
);
1198 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
1205 Release the translation entry specfied
1207 static void scsiback_release_translation_entry(struct vscsibk_info
*info
)
1209 struct v2p_entry
*entry
, *tmp
;
1210 struct list_head
*head
= &(info
->v2p_entry_lists
);
1211 unsigned long flags
;
1213 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1215 list_for_each_entry_safe(entry
, tmp
, head
, l
)
1216 __scsiback_del_translation_entry(entry
);
1218 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1221 static int scsiback_remove(struct xenbus_device
*dev
)
1223 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1226 scsiback_disconnect(info
);
1228 scsiback_release_translation_entry(info
);
1230 dev_set_drvdata(&dev
->dev
, NULL
);
1235 static int scsiback_probe(struct xenbus_device
*dev
,
1236 const struct xenbus_device_id
*id
)
1240 struct vscsibk_info
*info
= kzalloc(sizeof(struct vscsibk_info
),
1243 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
1246 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating backend structure");
1250 dev_set_drvdata(&dev
->dev
, info
);
1252 info
->domid
= dev
->otherend_id
;
1253 spin_lock_init(&info
->ring_lock
);
1254 info
->ring_error
= 0;
1255 atomic_set(&info
->nr_unreplied_reqs
, 0);
1256 init_waitqueue_head(&info
->waiting_to_free
);
1259 INIT_LIST_HEAD(&info
->v2p_entry_lists
);
1260 spin_lock_init(&info
->v2p_lock
);
1262 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, "feature-sg-grant", "%u",
1265 xenbus_dev_error(dev
, err
, "writing feature-sg-grant");
1267 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
1274 pr_warn("%s failed\n", __func__
);
1275 scsiback_remove(dev
);
1280 static char *scsiback_dump_proto_id(struct scsiback_tport
*tport
)
1282 switch (tport
->tport_proto_id
) {
1283 case SCSI_PROTOCOL_SAS
:
1285 case SCSI_PROTOCOL_FCP
:
1287 case SCSI_PROTOCOL_ISCSI
:
1296 static char *scsiback_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1298 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1299 struct scsiback_tpg
, se_tpg
);
1300 struct scsiback_tport
*tport
= tpg
->tport
;
1302 return &tport
->tport_name
[0];
1305 static u16
scsiback_get_tag(struct se_portal_group
*se_tpg
)
1307 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1308 struct scsiback_tpg
, se_tpg
);
1309 return tpg
->tport_tpgt
;
1312 static struct se_wwn
*
1313 scsiback_make_tport(struct target_fabric_configfs
*tf
,
1314 struct config_group
*group
,
1317 struct scsiback_tport
*tport
;
1322 tport
= kzalloc(sizeof(struct scsiback_tport
), GFP_KERNEL
);
1324 return ERR_PTR(-ENOMEM
);
1326 tport
->tport_wwpn
= wwpn
;
1328 * Determine the emulated Protocol Identifier and Target Port Name
1329 * based on the incoming configfs directory name.
1331 ptr
= strstr(name
, "naa.");
1333 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1336 ptr
= strstr(name
, "fc.");
1338 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1339 off
= 3; /* Skip over "fc." */
1342 ptr
= strstr(name
, "iqn.");
1344 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1348 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name
);
1350 return ERR_PTR(-EINVAL
);
1353 if (strlen(name
) >= VSCSI_NAMELEN
) {
1354 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name
,
1355 scsiback_dump_proto_id(tport
), VSCSI_NAMELEN
);
1357 return ERR_PTR(-EINVAL
);
1359 snprintf(&tport
->tport_name
[0], VSCSI_NAMELEN
, "%s", &name
[off
]);
1361 pr_debug("Allocated emulated Target %s Address: %s\n",
1362 scsiback_dump_proto_id(tport
), name
);
1364 return &tport
->tport_wwn
;
1367 static void scsiback_drop_tport(struct se_wwn
*wwn
)
1369 struct scsiback_tport
*tport
= container_of(wwn
,
1370 struct scsiback_tport
, tport_wwn
);
1372 pr_debug("Deallocating emulated Target %s Address: %s\n",
1373 scsiback_dump_proto_id(tport
), tport
->tport_name
);
1378 static u32
scsiback_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1383 static int scsiback_check_stop_free(struct se_cmd
*se_cmd
)
1385 return transport_generic_free_cmd(se_cmd
, 0);
1388 static void scsiback_release_cmd(struct se_cmd
*se_cmd
)
1390 struct se_session
*se_sess
= se_cmd
->se_sess
;
1392 percpu_ida_free(&se_sess
->sess_tag_pool
, se_cmd
->map_tag
);
1395 static u32
scsiback_sess_get_index(struct se_session
*se_sess
)
1400 static int scsiback_write_pending(struct se_cmd
*se_cmd
)
1402 /* Go ahead and process the write immediately */
1403 target_execute_cmd(se_cmd
);
1408 static int scsiback_write_pending_status(struct se_cmd
*se_cmd
)
1413 static void scsiback_set_default_node_attrs(struct se_node_acl
*nacl
)
1417 static int scsiback_get_cmd_state(struct se_cmd
*se_cmd
)
1422 static int scsiback_queue_data_in(struct se_cmd
*se_cmd
)
1424 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1425 struct vscsibk_pend
, se_cmd
);
1427 pending_req
->result
= SAM_STAT_GOOD
;
1428 scsiback_cmd_done(pending_req
);
1432 static int scsiback_queue_status(struct se_cmd
*se_cmd
)
1434 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1435 struct vscsibk_pend
, se_cmd
);
1437 if (se_cmd
->sense_buffer
&&
1438 ((se_cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
1439 (se_cmd
->se_cmd_flags
& SCF_EMULATED_TASK_SENSE
)))
1440 pending_req
->result
= (DRIVER_SENSE
<< 24) |
1441 SAM_STAT_CHECK_CONDITION
;
1443 pending_req
->result
= se_cmd
->scsi_status
;
1445 scsiback_cmd_done(pending_req
);
1449 static void scsiback_queue_tm_rsp(struct se_cmd
*se_cmd
)
1451 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1452 struct vscsibk_pend
, se_cmd
);
1454 complete(&pending_req
->tmr_done
);
1457 static void scsiback_aborted_task(struct se_cmd
*se_cmd
)
1461 static ssize_t
scsiback_tpg_param_alias_show(struct config_item
*item
,
1464 struct se_portal_group
*se_tpg
= param_to_tpg(item
);
1465 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1469 mutex_lock(&tpg
->tv_tpg_mutex
);
1470 rb
= snprintf(page
, PAGE_SIZE
, "%s\n", tpg
->param_alias
);
1471 mutex_unlock(&tpg
->tv_tpg_mutex
);
1476 static ssize_t
scsiback_tpg_param_alias_store(struct config_item
*item
,
1477 const char *page
, size_t count
)
1479 struct se_portal_group
*se_tpg
= param_to_tpg(item
);
1480 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1484 if (strlen(page
) >= VSCSI_NAMELEN
) {
1485 pr_err("param alias: %s, exceeds max: %d\n", page
,
1490 mutex_lock(&tpg
->tv_tpg_mutex
);
1491 len
= snprintf(tpg
->param_alias
, VSCSI_NAMELEN
, "%s", page
);
1492 if (tpg
->param_alias
[len
- 1] == '\n')
1493 tpg
->param_alias
[len
- 1] = '\0';
1494 mutex_unlock(&tpg
->tv_tpg_mutex
);
1499 CONFIGFS_ATTR(scsiback_tpg_param_
, alias
);
1501 static struct configfs_attribute
*scsiback_param_attrs
[] = {
1502 &scsiback_tpg_param_attr_alias
,
1506 static int scsiback_alloc_sess_cb(struct se_portal_group
*se_tpg
,
1507 struct se_session
*se_sess
, void *p
)
1509 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1510 struct scsiback_tpg
, se_tpg
);
1516 static int scsiback_make_nexus(struct scsiback_tpg
*tpg
,
1519 struct scsiback_nexus
*tv_nexus
;
1522 mutex_lock(&tpg
->tv_tpg_mutex
);
1523 if (tpg
->tpg_nexus
) {
1524 pr_debug("tpg->tpg_nexus already exists\n");
1529 tv_nexus
= kzalloc(sizeof(struct scsiback_nexus
), GFP_KERNEL
);
1535 tv_nexus
->tvn_se_sess
= target_alloc_session(&tpg
->se_tpg
,
1536 VSCSI_DEFAULT_SESSION_TAGS
,
1537 sizeof(struct vscsibk_pend
),
1538 TARGET_PROT_NORMAL
, name
,
1539 tv_nexus
, scsiback_alloc_sess_cb
);
1540 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1547 mutex_unlock(&tpg
->tv_tpg_mutex
);
1551 static int scsiback_drop_nexus(struct scsiback_tpg
*tpg
)
1553 struct se_session
*se_sess
;
1554 struct scsiback_nexus
*tv_nexus
;
1556 mutex_lock(&tpg
->tv_tpg_mutex
);
1557 tv_nexus
= tpg
->tpg_nexus
;
1559 mutex_unlock(&tpg
->tv_tpg_mutex
);
1563 se_sess
= tv_nexus
->tvn_se_sess
;
1565 mutex_unlock(&tpg
->tv_tpg_mutex
);
1569 if (tpg
->tv_tpg_port_count
!= 0) {
1570 mutex_unlock(&tpg
->tv_tpg_mutex
);
1571 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1572 tpg
->tv_tpg_port_count
);
1576 if (tpg
->tv_tpg_fe_count
!= 0) {
1577 mutex_unlock(&tpg
->tv_tpg_mutex
);
1578 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1579 tpg
->tv_tpg_fe_count
);
1583 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1584 scsiback_dump_proto_id(tpg
->tport
),
1585 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1588 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1590 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1591 tpg
->tpg_nexus
= NULL
;
1592 mutex_unlock(&tpg
->tv_tpg_mutex
);
1598 static ssize_t
scsiback_tpg_nexus_show(struct config_item
*item
, char *page
)
1600 struct se_portal_group
*se_tpg
= to_tpg(item
);
1601 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1602 struct scsiback_tpg
, se_tpg
);
1603 struct scsiback_nexus
*tv_nexus
;
1606 mutex_lock(&tpg
->tv_tpg_mutex
);
1607 tv_nexus
= tpg
->tpg_nexus
;
1609 mutex_unlock(&tpg
->tv_tpg_mutex
);
1612 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1613 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1614 mutex_unlock(&tpg
->tv_tpg_mutex
);
1619 static ssize_t
scsiback_tpg_nexus_store(struct config_item
*item
,
1620 const char *page
, size_t count
)
1622 struct se_portal_group
*se_tpg
= to_tpg(item
);
1623 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1624 struct scsiback_tpg
, se_tpg
);
1625 struct scsiback_tport
*tport_wwn
= tpg
->tport
;
1626 unsigned char i_port
[VSCSI_NAMELEN
], *ptr
, *port_ptr
;
1629 * Shutdown the active I_T nexus if 'NULL' is passed.
1631 if (!strncmp(page
, "NULL", 4)) {
1632 ret
= scsiback_drop_nexus(tpg
);
1633 return (!ret
) ? count
: ret
;
1636 * Otherwise make sure the passed virtual Initiator port WWN matches
1637 * the fabric protocol_id set in scsiback_make_tport(), and call
1638 * scsiback_make_nexus().
1640 if (strlen(page
) >= VSCSI_NAMELEN
) {
1641 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1642 page
, VSCSI_NAMELEN
);
1645 snprintf(&i_port
[0], VSCSI_NAMELEN
, "%s", page
);
1647 ptr
= strstr(i_port
, "naa.");
1649 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1650 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1651 i_port
, scsiback_dump_proto_id(tport_wwn
));
1654 port_ptr
= &i_port
[0];
1657 ptr
= strstr(i_port
, "fc.");
1659 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1660 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1661 i_port
, scsiback_dump_proto_id(tport_wwn
));
1664 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1667 ptr
= strstr(i_port
, "iqn.");
1669 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1670 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1671 i_port
, scsiback_dump_proto_id(tport_wwn
));
1674 port_ptr
= &i_port
[0];
1677 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1681 * Clear any trailing newline for the NAA WWN
1684 if (i_port
[strlen(i_port
) - 1] == '\n')
1685 i_port
[strlen(i_port
) - 1] = '\0';
1687 ret
= scsiback_make_nexus(tpg
, port_ptr
);
1694 CONFIGFS_ATTR(scsiback_tpg_
, nexus
);
1696 static struct configfs_attribute
*scsiback_tpg_attrs
[] = {
1697 &scsiback_tpg_attr_nexus
,
1702 scsiback_wwn_version_show(struct config_item
*item
, char *page
)
1704 return sprintf(page
, "xen-pvscsi fabric module %s on %s/%s on "
1706 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1709 CONFIGFS_ATTR_RO(scsiback_wwn_
, version
);
1711 static struct configfs_attribute
*scsiback_wwn_attrs
[] = {
1712 &scsiback_wwn_attr_version
,
1716 static char *scsiback_get_fabric_name(void)
1718 return "xen-pvscsi";
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
,
1747 struct config_group
*group
,
1750 struct scsiback_tport
*tport
= container_of(wwn
,
1751 struct scsiback_tport
, tport_wwn
);
1753 struct scsiback_tpg
*tpg
;
1757 if (strstr(name
, "tpgt_") != name
)
1758 return ERR_PTR(-EINVAL
);
1759 ret
= kstrtou16(name
+ 5, 10, &tpgt
);
1761 return ERR_PTR(ret
);
1763 tpg
= kzalloc(sizeof(struct scsiback_tpg
), GFP_KERNEL
);
1765 return ERR_PTR(-ENOMEM
);
1767 mutex_init(&tpg
->tv_tpg_mutex
);
1768 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1769 INIT_LIST_HEAD(&tpg
->info_list
);
1771 tpg
->tport_tpgt
= tpgt
;
1773 ret
= core_tpg_register(wwn
, &tpg
->se_tpg
, tport
->tport_proto_id
);
1778 mutex_lock(&scsiback_mutex
);
1779 list_add_tail(&tpg
->tv_tpg_list
, &scsiback_list
);
1780 mutex_unlock(&scsiback_mutex
);
1782 return &tpg
->se_tpg
;
1785 static void scsiback_drop_tpg(struct se_portal_group
*se_tpg
)
1787 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1788 struct scsiback_tpg
, se_tpg
);
1790 mutex_lock(&scsiback_mutex
);
1791 list_del(&tpg
->tv_tpg_list
);
1792 mutex_unlock(&scsiback_mutex
);
1794 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1796 scsiback_drop_nexus(tpg
);
1798 * Deregister the se_tpg from TCM.
1800 core_tpg_deregister(se_tpg
);
1804 static int scsiback_check_true(struct se_portal_group
*se_tpg
)
1809 static int scsiback_check_false(struct se_portal_group
*se_tpg
)
1814 static const struct target_core_fabric_ops scsiback_ops
= {
1815 .module
= THIS_MODULE
,
1816 .name
= "xen-pvscsi",
1817 .get_fabric_name
= scsiback_get_fabric_name
,
1818 .tpg_get_wwn
= scsiback_get_fabric_wwn
,
1819 .tpg_get_tag
= scsiback_get_tag
,
1820 .tpg_check_demo_mode
= scsiback_check_true
,
1821 .tpg_check_demo_mode_cache
= scsiback_check_true
,
1822 .tpg_check_demo_mode_write_protect
= scsiback_check_false
,
1823 .tpg_check_prod_mode_write_protect
= scsiback_check_false
,
1824 .tpg_get_inst_index
= scsiback_tpg_get_inst_index
,
1825 .check_stop_free
= scsiback_check_stop_free
,
1826 .release_cmd
= scsiback_release_cmd
,
1827 .sess_get_index
= scsiback_sess_get_index
,
1828 .sess_get_initiator_sid
= NULL
,
1829 .write_pending
= scsiback_write_pending
,
1830 .write_pending_status
= scsiback_write_pending_status
,
1831 .set_default_node_attributes
= scsiback_set_default_node_attrs
,
1832 .get_cmd_state
= scsiback_get_cmd_state
,
1833 .queue_data_in
= scsiback_queue_data_in
,
1834 .queue_status
= scsiback_queue_status
,
1835 .queue_tm_rsp
= scsiback_queue_tm_rsp
,
1836 .aborted_task
= scsiback_aborted_task
,
1838 * Setup callers for generic logic in target_core_fabric_configfs.c
1840 .fabric_make_wwn
= scsiback_make_tport
,
1841 .fabric_drop_wwn
= scsiback_drop_tport
,
1842 .fabric_make_tpg
= scsiback_make_tpg
,
1843 .fabric_drop_tpg
= scsiback_drop_tpg
,
1844 .fabric_post_link
= scsiback_port_link
,
1845 .fabric_pre_unlink
= scsiback_port_unlink
,
1847 .tfc_wwn_attrs
= scsiback_wwn_attrs
,
1848 .tfc_tpg_base_attrs
= scsiback_tpg_attrs
,
1849 .tfc_tpg_param_attrs
= scsiback_param_attrs
,
1852 static const struct xenbus_device_id scsiback_ids
[] = {
1857 static struct xenbus_driver scsiback_driver
= {
1858 .ids
= scsiback_ids
,
1859 .probe
= scsiback_probe
,
1860 .remove
= scsiback_remove
,
1861 .otherend_changed
= scsiback_frontend_changed
1864 static int __init
scsiback_init(void)
1871 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE
"\n",
1872 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1874 ret
= xenbus_register_backend(&scsiback_driver
);
1878 ret
= target_register_template(&scsiback_ops
);
1880 goto out_unregister_xenbus
;
1884 out_unregister_xenbus
:
1885 xenbus_unregister_driver(&scsiback_driver
);
1887 pr_err("%s: error %d\n", __func__
, ret
);
1891 static void __exit
scsiback_exit(void)
1895 while (free_pages_num
) {
1896 if (get_free_page(&page
))
1898 gnttab_free_pages(1, &page
);
1900 target_unregister_template(&scsiback_ops
);
1901 xenbus_unregister_driver(&scsiback_driver
);
1904 module_init(scsiback_init
);
1905 module_exit(scsiback_exit
);
1907 MODULE_DESCRIPTION("Xen SCSI backend driver");
1908 MODULE_LICENSE("Dual BSD/GPL");
1909 MODULE_ALIAS("xen-backend:vscsi");
1910 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");