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.h>
53 #include <scsi/scsi_dbg.h>
54 #include <scsi/scsi_eh.h>
55 #include <scsi/scsi_tcq.h>
57 #include <target/target_core_base.h>
58 #include <target/target_core_fabric.h>
59 #include <target/target_core_fabric_configfs.h>
61 #include <asm/hypervisor.h>
64 #include <xen/balloon.h>
65 #include <xen/events.h>
66 #include <xen/xenbus.h>
67 #include <xen/grant_table.h>
70 #include <xen/interface/grant_table.h>
71 #include <xen/interface/io/vscsiif.h>
73 #define VSCSI_VERSION "v0.1"
74 #define VSCSI_NAMELEN 32
77 unsigned int hst
; /* host */
78 unsigned int chn
; /* channel */
79 unsigned int tgt
; /* target */
80 unsigned int lun
; /* LUN */
84 struct ids_tuple v
; /* translate from */
85 struct scsiback_tpg
*tpg
; /* translate to */
92 struct xenbus_device
*dev
;
97 struct vscsiif_back_ring ring
;
100 spinlock_t ring_lock
;
101 atomic_t nr_unreplied_reqs
;
104 struct list_head v2p_entry_lists
;
106 wait_queue_head_t waiting_to_free
;
109 /* theoretical maximum of grants for one request */
110 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
113 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
114 * call to map/unmap grants. Don't choose it too large, as there are arrays
115 * with VSCSI_GRANT_BATCH elements allocated on the stack.
117 #define VSCSI_GRANT_BATCH 16
119 struct vscsibk_pend
{
122 uint8_t cmnd
[VSCSIIF_MAX_COMMAND_SIZE
];
125 uint8_t sc_data_direction
;
126 uint16_t n_sg
; /* real length of SG list */
127 uint16_t n_grants
; /* SG pages and potentially SG list */
131 struct vscsibk_info
*info
;
132 struct v2p_entry
*v2p
;
133 struct scatterlist
*sgl
;
135 uint8_t sense_buffer
[VSCSIIF_SENSE_BUFFERSIZE
];
137 grant_handle_t grant_handles
[VSCSI_MAX_GRANTS
];
138 struct page
*pages
[VSCSI_MAX_GRANTS
];
140 struct se_cmd se_cmd
;
143 struct scsiback_tmr
{
144 atomic_t tmr_complete
;
145 wait_queue_head_t tmr_wait
;
148 struct scsiback_nexus
{
149 /* Pointer to TCM session for I_T Nexus */
150 struct se_session
*tvn_se_sess
;
153 struct scsiback_tport
{
154 /* SCSI protocol the tport is providing */
156 /* Binary World Wide unique Port Name for pvscsi Target port */
158 /* ASCII formatted WWPN for pvscsi Target port */
159 char tport_name
[VSCSI_NAMELEN
];
160 /* Returned by scsiback_make_tport() */
161 struct se_wwn tport_wwn
;
164 struct scsiback_tpg
{
165 /* scsiback port target portal group tag for TCM */
167 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
168 int tv_tpg_port_count
;
169 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
171 /* list for scsiback_list */
172 struct list_head tv_tpg_list
;
173 /* Used to protect access for tpg_nexus */
174 struct mutex tv_tpg_mutex
;
175 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
176 struct scsiback_nexus
*tpg_nexus
;
177 /* Pointer back to scsiback_tport */
178 struct scsiback_tport
*tport
;
179 /* Returned by scsiback_make_tpg() */
180 struct se_portal_group se_tpg
;
181 /* alias used in xenstore */
182 char param_alias
[VSCSI_NAMELEN
];
183 /* list of info structures related to this target portal group */
184 struct list_head info_list
;
187 #define SCSIBACK_INVALID_HANDLE (~0)
189 static bool log_print_stat
;
190 module_param(log_print_stat
, bool, 0644);
192 static int scsiback_max_buffer_pages
= 1024;
193 module_param_named(max_buffer_pages
, scsiback_max_buffer_pages
, int, 0644);
194 MODULE_PARM_DESC(max_buffer_pages
,
195 "Maximum number of free pages to keep in backend buffer");
197 static struct kmem_cache
*scsiback_cachep
;
198 static DEFINE_SPINLOCK(free_pages_lock
);
199 static int free_pages_num
;
200 static LIST_HEAD(scsiback_free_pages
);
202 /* Global spinlock to protect scsiback TPG list */
203 static DEFINE_MUTEX(scsiback_mutex
);
204 static LIST_HEAD(scsiback_list
);
206 static const struct target_core_fabric_ops scsiback_ops
;
208 static void scsiback_get(struct vscsibk_info
*info
)
210 atomic_inc(&info
->nr_unreplied_reqs
);
213 static void scsiback_put(struct vscsibk_info
*info
)
215 if (atomic_dec_and_test(&info
->nr_unreplied_reqs
))
216 wake_up(&info
->waiting_to_free
);
219 static void put_free_pages(struct page
**page
, int num
)
222 int i
= free_pages_num
+ num
, n
= num
;
226 if (i
> scsiback_max_buffer_pages
) {
227 n
= min(num
, i
- scsiback_max_buffer_pages
);
228 gnttab_free_pages(n
, page
+ num
- n
);
231 spin_lock_irqsave(&free_pages_lock
, flags
);
232 for (i
= 0; i
< n
; i
++)
233 list_add(&page
[i
]->lru
, &scsiback_free_pages
);
235 spin_unlock_irqrestore(&free_pages_lock
, flags
);
238 static int get_free_page(struct page
**page
)
242 spin_lock_irqsave(&free_pages_lock
, flags
);
243 if (list_empty(&scsiback_free_pages
)) {
244 spin_unlock_irqrestore(&free_pages_lock
, flags
);
245 return gnttab_alloc_pages(1, page
);
247 page
[0] = list_first_entry(&scsiback_free_pages
, struct page
, lru
);
248 list_del(&page
[0]->lru
);
250 spin_unlock_irqrestore(&free_pages_lock
, flags
);
254 static unsigned long vaddr_page(struct page
*page
)
256 unsigned long pfn
= page_to_pfn(page
);
258 return (unsigned long)pfn_to_kaddr(pfn
);
261 static unsigned long vaddr(struct vscsibk_pend
*req
, int seg
)
263 return vaddr_page(req
->pages
[seg
]);
266 static void scsiback_print_status(char *sense_buffer
, int errors
,
267 struct vscsibk_pend
*pending_req
)
269 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
271 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
272 tpg
->tport
->tport_name
, pending_req
->v2p
->lun
,
273 pending_req
->cmnd
[0], status_byte(errors
), msg_byte(errors
),
274 host_byte(errors
), driver_byte(errors
));
277 static void scsiback_fast_flush_area(struct vscsibk_pend
*req
)
279 struct gnttab_unmap_grant_ref unmap
[VSCSI_GRANT_BATCH
];
280 struct page
*pages
[VSCSI_GRANT_BATCH
];
281 unsigned int i
, invcount
= 0;
282 grant_handle_t handle
;
292 for (i
= 0; i
< req
->n_grants
; i
++) {
293 handle
= req
->grant_handles
[i
];
294 if (handle
== SCSIBACK_INVALID_HANDLE
)
296 gnttab_set_unmap_op(&unmap
[invcount
], vaddr(req
, i
),
297 GNTMAP_host_map
, handle
);
298 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
299 pages
[invcount
] = req
->pages
[i
];
300 put_page(pages
[invcount
]);
302 if (invcount
< VSCSI_GRANT_BATCH
)
304 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
310 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
314 put_free_pages(req
->pages
, req
->n_grants
);
318 static void scsiback_free_translation_entry(struct kref
*kref
)
320 struct v2p_entry
*entry
= container_of(kref
, struct v2p_entry
, kref
);
321 struct scsiback_tpg
*tpg
= entry
->tpg
;
323 mutex_lock(&tpg
->tv_tpg_mutex
);
324 tpg
->tv_tpg_fe_count
--;
325 mutex_unlock(&tpg
->tv_tpg_mutex
);
330 static void scsiback_do_resp_with_sense(char *sense_buffer
, int32_t result
,
331 uint32_t resid
, struct vscsibk_pend
*pending_req
)
333 struct vscsiif_response
*ring_res
;
334 struct vscsibk_info
*info
= pending_req
->info
;
336 struct scsi_sense_hdr sshdr
;
340 spin_lock_irqsave(&info
->ring_lock
, flags
);
342 ring_res
= RING_GET_RESPONSE(&info
->ring
, info
->ring
.rsp_prod_pvt
);
343 info
->ring
.rsp_prod_pvt
++;
345 ring_res
->rslt
= result
;
346 ring_res
->rqid
= pending_req
->rqid
;
348 if (sense_buffer
!= NULL
&&
349 scsi_normalize_sense(sense_buffer
, VSCSIIF_SENSE_BUFFERSIZE
,
351 len
= min_t(unsigned, 8 + sense_buffer
[7],
352 VSCSIIF_SENSE_BUFFERSIZE
);
353 memcpy(ring_res
->sense_buffer
, sense_buffer
, len
);
354 ring_res
->sense_len
= len
;
356 ring_res
->sense_len
= 0;
359 ring_res
->residual_len
= resid
;
361 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info
->ring
, notify
);
362 spin_unlock_irqrestore(&info
->ring_lock
, flags
);
365 notify_remote_via_irq(info
->irq
);
367 if (pending_req
->v2p
)
368 kref_put(&pending_req
->v2p
->kref
,
369 scsiback_free_translation_entry
);
372 static void scsiback_cmd_done(struct vscsibk_pend
*pending_req
)
374 struct vscsibk_info
*info
= pending_req
->info
;
375 unsigned char *sense_buffer
;
379 sense_buffer
= pending_req
->sense_buffer
;
380 resid
= pending_req
->se_cmd
.residual_count
;
381 errors
= pending_req
->result
;
383 if (errors
&& log_print_stat
)
384 scsiback_print_status(sense_buffer
, errors
, pending_req
);
386 scsiback_fast_flush_area(pending_req
);
387 scsiback_do_resp_with_sense(sense_buffer
, errors
, resid
, pending_req
);
391 static void scsiback_cmd_exec(struct vscsibk_pend
*pending_req
)
393 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
394 struct se_session
*sess
= pending_req
->v2p
->tpg
->tpg_nexus
->tvn_se_sess
;
397 memset(pending_req
->sense_buffer
, 0, VSCSIIF_SENSE_BUFFERSIZE
);
399 memset(se_cmd
, 0, sizeof(*se_cmd
));
401 scsiback_get(pending_req
->info
);
402 se_cmd
->tag
= pending_req
->rqid
;
403 rc
= target_submit_cmd_map_sgls(se_cmd
, sess
, pending_req
->cmnd
,
404 pending_req
->sense_buffer
, pending_req
->v2p
->lun
,
405 pending_req
->data_len
, 0,
406 pending_req
->sc_data_direction
, 0,
407 pending_req
->sgl
, pending_req
->n_sg
,
410 transport_send_check_condition_and_sense(se_cmd
,
411 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
412 transport_generic_free_cmd(se_cmd
, 0);
416 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref
*map
,
417 struct page
**pg
, grant_handle_t
*grant
, int cnt
)
424 err
= gnttab_map_refs(map
, NULL
, pg
, cnt
);
426 for (i
= 0; i
< cnt
; i
++) {
427 if (unlikely(map
[i
].status
!= GNTST_okay
)) {
428 pr_err("invalid buffer -- could not remap it\n");
429 map
[i
].handle
= SCSIBACK_INVALID_HANDLE
;
434 grant
[i
] = map
[i
].handle
;
439 static int scsiback_gnttab_data_map_list(struct vscsibk_pend
*pending_req
,
440 struct scsiif_request_segment
*seg
, struct page
**pg
,
441 grant_handle_t
*grant
, int cnt
, u32 flags
)
443 int mapcount
= 0, i
, err
= 0;
444 struct gnttab_map_grant_ref map
[VSCSI_GRANT_BATCH
];
445 struct vscsibk_info
*info
= pending_req
->info
;
447 for (i
= 0; i
< cnt
; i
++) {
448 if (get_free_page(pg
+ mapcount
)) {
449 put_free_pages(pg
, mapcount
);
450 pr_err("no grant page\n");
453 gnttab_set_map_op(&map
[mapcount
], vaddr_page(pg
[mapcount
]),
454 flags
, seg
[i
].gref
, info
->domid
);
456 if (mapcount
< VSCSI_GRANT_BATCH
)
458 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
461 pending_req
->n_grants
+= mapcount
;
466 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
467 pending_req
->n_grants
+= mapcount
;
471 static int scsiback_gnttab_data_map(struct vscsiif_request
*ring_req
,
472 struct vscsibk_pend
*pending_req
)
475 int i
, err
, n_segs
, i_seg
= 0;
477 struct scsiif_request_segment
*seg
;
478 unsigned long end_seg
= 0;
479 unsigned int nr_segments
= (unsigned int)ring_req
->nr_segments
;
480 unsigned int nr_sgl
= 0;
481 struct scatterlist
*sg
;
482 grant_handle_t
*grant
;
484 pending_req
->n_sg
= 0;
485 pending_req
->n_grants
= 0;
486 pending_req
->data_len
= 0;
488 nr_segments
&= ~VSCSIIF_SG_GRANT
;
492 if (nr_segments
> VSCSIIF_SG_TABLESIZE
) {
493 pr_debug("invalid parameter nr_seg = %d\n",
494 ring_req
->nr_segments
);
498 if (ring_req
->nr_segments
& VSCSIIF_SG_GRANT
) {
499 err
= scsiback_gnttab_data_map_list(pending_req
, ring_req
->seg
,
500 pending_req
->pages
, pending_req
->grant_handles
,
501 nr_segments
, GNTMAP_host_map
| GNTMAP_readonly
);
504 nr_sgl
= nr_segments
;
506 for (i
= 0; i
< nr_sgl
; i
++) {
507 n_segs
= ring_req
->seg
[i
].length
/
508 sizeof(struct scsiif_request_segment
);
509 if ((unsigned)ring_req
->seg
[i
].offset
+
510 (unsigned)ring_req
->seg
[i
].length
> PAGE_SIZE
||
511 n_segs
* sizeof(struct scsiif_request_segment
) !=
512 ring_req
->seg
[i
].length
)
514 nr_segments
+= n_segs
;
516 if (nr_segments
> SG_ALL
) {
517 pr_debug("invalid nr_seg = %d\n", nr_segments
);
522 /* free of (sgl) in fast_flush_area() */
523 pending_req
->sgl
= kmalloc_array(nr_segments
,
524 sizeof(struct scatterlist
), GFP_KERNEL
);
525 if (!pending_req
->sgl
)
528 sg_init_table(pending_req
->sgl
, nr_segments
);
529 pending_req
->n_sg
= nr_segments
;
531 flags
= GNTMAP_host_map
;
532 if (pending_req
->sc_data_direction
== DMA_TO_DEVICE
)
533 flags
|= GNTMAP_readonly
;
535 pg
= pending_req
->pages
+ nr_sgl
;
536 grant
= pending_req
->grant_handles
+ nr_sgl
;
539 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
540 pg
, grant
, nr_segments
, flags
);
544 for (i
= 0; i
< nr_sgl
; i
++) {
545 seg
= (struct scsiif_request_segment
*)(
546 vaddr(pending_req
, i
) + ring_req
->seg
[i
].offset
);
547 n_segs
= ring_req
->seg
[i
].length
/
548 sizeof(struct scsiif_request_segment
);
549 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
550 pg
, grant
, n_segs
, flags
);
556 end_seg
= vaddr(pending_req
, 0) + ring_req
->seg
[0].offset
;
557 seg
= (struct scsiif_request_segment
*)end_seg
;
558 end_seg
+= ring_req
->seg
[0].length
;
559 pg
= pending_req
->pages
+ nr_sgl
;
562 for_each_sg(pending_req
->sgl
, sg
, nr_segments
, i
) {
563 sg_set_page(sg
, pg
[i
], seg
->length
, seg
->offset
);
564 pending_req
->data_len
+= seg
->length
;
566 if (nr_sgl
&& (unsigned long)seg
>= end_seg
) {
568 end_seg
= vaddr(pending_req
, i_seg
) +
569 ring_req
->seg
[i_seg
].offset
;
570 seg
= (struct scsiif_request_segment
*)end_seg
;
571 end_seg
+= ring_req
->seg
[i_seg
].length
;
573 if (sg
->offset
>= PAGE_SIZE
||
574 sg
->length
> PAGE_SIZE
||
575 sg
->offset
+ sg
->length
> PAGE_SIZE
)
582 static void scsiback_disconnect(struct vscsibk_info
*info
)
584 wait_event(info
->waiting_to_free
,
585 atomic_read(&info
->nr_unreplied_reqs
) == 0);
587 unbind_from_irqhandler(info
->irq
, info
);
589 xenbus_unmap_ring_vfree(info
->dev
, info
->ring
.sring
);
592 static void scsiback_device_action(struct vscsibk_pend
*pending_req
,
593 enum tcm_tmreq_table act
, int tag
)
595 int rc
, err
= FAILED
;
596 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
597 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
598 struct scsiback_tmr
*tmr
;
600 tmr
= kzalloc(sizeof(struct scsiback_tmr
), GFP_KERNEL
);
604 init_waitqueue_head(&tmr
->tmr_wait
);
606 transport_init_se_cmd(se_cmd
, tpg
->se_tpg
.se_tpg_tfo
,
607 tpg
->tpg_nexus
->tvn_se_sess
, 0, DMA_NONE
, TCM_SIMPLE_TAG
,
608 &pending_req
->sense_buffer
[0]);
610 rc
= core_tmr_alloc_req(se_cmd
, tmr
, act
, GFP_KERNEL
);
614 se_cmd
->se_tmr_req
->ref_task_tag
= tag
;
616 if (transport_lookup_tmr_lun(se_cmd
, pending_req
->v2p
->lun
) < 0)
619 transport_generic_handle_tmr(se_cmd
);
620 wait_event(tmr
->tmr_wait
, atomic_read(&tmr
->tmr_complete
));
622 err
= (se_cmd
->se_tmr_req
->response
== TMR_FUNCTION_COMPLETE
) ?
627 transport_generic_free_cmd(&pending_req
->se_cmd
, 1);
631 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
633 kmem_cache_free(scsiback_cachep
, pending_req
);
637 Perform virtual to physical translation
639 static struct v2p_entry
*scsiback_do_translation(struct vscsibk_info
*info
,
642 struct v2p_entry
*entry
;
643 struct list_head
*head
= &(info
->v2p_entry_lists
);
646 spin_lock_irqsave(&info
->v2p_lock
, flags
);
647 list_for_each_entry(entry
, head
, l
) {
648 if ((entry
->v
.chn
== v
->chn
) &&
649 (entry
->v
.tgt
== v
->tgt
) &&
650 (entry
->v
.lun
== v
->lun
)) {
651 kref_get(&entry
->kref
);
658 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
662 static int prepare_pending_reqs(struct vscsibk_info
*info
,
663 struct vscsiif_request
*ring_req
,
664 struct vscsibk_pend
*pending_req
)
666 struct v2p_entry
*v2p
;
667 struct ids_tuple vir
;
669 pending_req
->rqid
= ring_req
->rqid
;
670 pending_req
->info
= info
;
672 vir
.chn
= ring_req
->channel
;
673 vir
.tgt
= ring_req
->id
;
674 vir
.lun
= ring_req
->lun
;
676 v2p
= scsiback_do_translation(info
, &vir
);
678 pending_req
->v2p
= NULL
;
679 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
680 vir
.chn
, vir
.tgt
, vir
.lun
);
683 pending_req
->v2p
= v2p
;
685 /* request range check from frontend */
686 pending_req
->sc_data_direction
= ring_req
->sc_data_direction
;
687 if ((pending_req
->sc_data_direction
!= DMA_BIDIRECTIONAL
) &&
688 (pending_req
->sc_data_direction
!= DMA_TO_DEVICE
) &&
689 (pending_req
->sc_data_direction
!= DMA_FROM_DEVICE
) &&
690 (pending_req
->sc_data_direction
!= DMA_NONE
)) {
691 pr_debug("invalid parameter data_dir = %d\n",
692 pending_req
->sc_data_direction
);
696 pending_req
->cmd_len
= ring_req
->cmd_len
;
697 if (pending_req
->cmd_len
> VSCSIIF_MAX_COMMAND_SIZE
) {
698 pr_debug("invalid parameter cmd_len = %d\n",
699 pending_req
->cmd_len
);
702 memcpy(pending_req
->cmnd
, ring_req
->cmnd
, pending_req
->cmd_len
);
707 static int scsiback_do_cmd_fn(struct vscsibk_info
*info
)
709 struct vscsiif_back_ring
*ring
= &info
->ring
;
710 struct vscsiif_request ring_req
;
711 struct vscsibk_pend
*pending_req
;
717 rp
= ring
->sring
->req_prod
;
718 rmb(); /* guest system is accessing ring, too */
720 if (RING_REQUEST_PROD_OVERFLOW(ring
, rp
)) {
721 rc
= ring
->rsp_prod_pvt
;
722 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
723 info
->domid
, rp
, rc
, rp
- rc
);
724 info
->ring_error
= 1;
729 if (RING_REQUEST_CONS_OVERFLOW(ring
, rc
))
731 pending_req
= kmem_cache_alloc(scsiback_cachep
, GFP_KERNEL
);
735 ring_req
= *RING_GET_REQUEST(ring
, rc
);
736 ring
->req_cons
= ++rc
;
738 err
= prepare_pending_reqs(info
, &ring_req
, pending_req
);
742 result
= DID_NO_CONNECT
;
745 result
= DRIVER_ERROR
;
748 scsiback_do_resp_with_sense(NULL
, result
<< 24, 0,
750 kmem_cache_free(scsiback_cachep
, pending_req
);
754 switch (ring_req
.act
) {
755 case VSCSIIF_ACT_SCSI_CDB
:
756 if (scsiback_gnttab_data_map(&ring_req
, pending_req
)) {
757 scsiback_fast_flush_area(pending_req
);
758 scsiback_do_resp_with_sense(NULL
,
759 DRIVER_ERROR
<< 24, 0, pending_req
);
760 kmem_cache_free(scsiback_cachep
, pending_req
);
762 scsiback_cmd_exec(pending_req
);
765 case VSCSIIF_ACT_SCSI_ABORT
:
766 scsiback_device_action(pending_req
, TMR_ABORT_TASK
,
769 case VSCSIIF_ACT_SCSI_RESET
:
770 scsiback_device_action(pending_req
, TMR_LUN_RESET
, 0);
773 pr_err_ratelimited("invalid request\n");
774 scsiback_do_resp_with_sense(NULL
, DRIVER_ERROR
<< 24,
776 kmem_cache_free(scsiback_cachep
, pending_req
);
780 /* Yield point for this unbounded loop. */
784 RING_FINAL_CHECK_FOR_REQUESTS(&info
->ring
, more_to_do
);
788 static irqreturn_t
scsiback_irq_fn(int irq
, void *dev_id
)
790 struct vscsibk_info
*info
= dev_id
;
792 if (info
->ring_error
)
795 while (scsiback_do_cmd_fn(info
))
801 static int scsiback_init_sring(struct vscsibk_info
*info
, grant_ref_t ring_ref
,
802 evtchn_port_t evtchn
)
805 struct vscsiif_sring
*sring
;
811 err
= xenbus_map_ring_valloc(info
->dev
, &ring_ref
, 1, &area
);
815 sring
= (struct vscsiif_sring
*)area
;
816 BACK_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
818 err
= bind_interdomain_evtchn_to_irq(info
->domid
, evtchn
);
824 err
= request_threaded_irq(info
->irq
, NULL
, scsiback_irq_fn
,
825 IRQF_ONESHOT
, "vscsiif-backend", info
);
832 unbind_from_irqhandler(info
->irq
, info
);
835 xenbus_unmap_ring_vfree(info
->dev
, area
);
840 static int scsiback_map(struct vscsibk_info
*info
)
842 struct xenbus_device
*dev
= info
->dev
;
843 unsigned int ring_ref
, evtchn
;
846 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
847 "ring-ref", "%u", &ring_ref
,
848 "event-channel", "%u", &evtchn
, NULL
);
850 xenbus_dev_fatal(dev
, err
, "reading %s ring", dev
->otherend
);
854 return scsiback_init_sring(info
, ring_ref
, evtchn
);
858 Add a new translation entry
860 static int scsiback_add_translation_entry(struct vscsibk_info
*info
,
861 char *phy
, struct ids_tuple
*v
)
864 struct v2p_entry
*entry
;
865 struct v2p_entry
*new;
866 struct list_head
*head
= &(info
->v2p_entry_lists
);
869 unsigned int unpacked_lun
;
870 struct se_lun
*se_lun
;
871 struct scsiback_tpg
*tpg_entry
, *tpg
= NULL
;
872 char *error
= "doesn't exist";
874 lunp
= strrchr(phy
, ':');
876 pr_err("illegal format of physical device %s\n", phy
);
881 if (kstrtouint(lunp
, 10, &unpacked_lun
) || unpacked_lun
>= TRANSPORT_MAX_LUNS_PER_TPG
) {
882 pr_err("lun number not valid: %s\n", lunp
);
886 mutex_lock(&scsiback_mutex
);
887 list_for_each_entry(tpg_entry
, &scsiback_list
, tv_tpg_list
) {
888 if (!strcmp(phy
, tpg_entry
->tport
->tport_name
) ||
889 !strcmp(phy
, tpg_entry
->param_alias
)) {
890 mutex_lock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
891 hlist_for_each_entry(se_lun
, &tpg_entry
->se_tpg
.tpg_lun_hlist
, link
) {
892 if (se_lun
->unpacked_lun
== unpacked_lun
) {
893 if (!tpg_entry
->tpg_nexus
)
894 error
= "nexus undefined";
900 mutex_unlock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
905 mutex_lock(&tpg
->tv_tpg_mutex
);
906 tpg
->tv_tpg_fe_count
++;
907 mutex_unlock(&tpg
->tv_tpg_mutex
);
909 mutex_unlock(&scsiback_mutex
);
912 pr_err("%s:%d %s\n", phy
, unpacked_lun
, error
);
916 new = kmalloc(sizeof(struct v2p_entry
), GFP_KERNEL
);
922 spin_lock_irqsave(&info
->v2p_lock
, flags
);
924 /* Check double assignment to identical virtual ID */
925 list_for_each_entry(entry
, head
, l
) {
926 if ((entry
->v
.chn
== v
->chn
) &&
927 (entry
->v
.tgt
== v
->tgt
) &&
928 (entry
->v
.lun
== v
->lun
)) {
929 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
936 /* Create a new translation entry and add to the list */
937 kref_init(&new->kref
);
940 new->lun
= unpacked_lun
;
941 list_add_tail(&new->l
, head
);
944 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
947 mutex_lock(&tpg
->tv_tpg_mutex
);
948 tpg
->tv_tpg_fe_count
--;
949 mutex_unlock(&tpg
->tv_tpg_mutex
);
957 static void __scsiback_del_translation_entry(struct v2p_entry
*entry
)
960 kref_put(&entry
->kref
, scsiback_free_translation_entry
);
964 Delete the translation entry specfied
966 static int scsiback_del_translation_entry(struct vscsibk_info
*info
,
969 struct v2p_entry
*entry
;
970 struct list_head
*head
= &(info
->v2p_entry_lists
);
973 spin_lock_irqsave(&info
->v2p_lock
, flags
);
974 /* Find out the translation entry specified */
975 list_for_each_entry(entry
, head
, l
) {
976 if ((entry
->v
.chn
== v
->chn
) &&
977 (entry
->v
.tgt
== v
->tgt
) &&
978 (entry
->v
.lun
== v
->lun
)) {
983 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
987 /* Delete the translation entry specfied */
988 __scsiback_del_translation_entry(entry
);
990 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
994 static void scsiback_do_add_lun(struct vscsibk_info
*info
, const char *state
,
995 char *phy
, struct ids_tuple
*vir
, int try)
997 if (!scsiback_add_translation_entry(info
, phy
, vir
)) {
998 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
999 "%d", XenbusStateInitialised
)) {
1000 pr_err("xenbus_printf error %s\n", state
);
1001 scsiback_del_translation_entry(info
, vir
);
1004 xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1005 "%d", XenbusStateClosed
);
1009 static void scsiback_do_del_lun(struct vscsibk_info
*info
, const char *state
,
1010 struct ids_tuple
*vir
)
1012 if (!scsiback_del_translation_entry(info
, vir
)) {
1013 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1014 "%d", XenbusStateClosed
))
1015 pr_err("xenbus_printf error %s\n", state
);
1019 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1020 #define VSCSIBACK_OP_UPDATEDEV_STATE 2
1022 static void scsiback_do_1lun_hotplug(struct vscsibk_info
*info
, int op
,
1026 struct ids_tuple vir
;
1029 char phy
[VSCSI_NAMELEN
];
1032 struct xenbus_device
*dev
= info
->dev
;
1035 snprintf(state
, sizeof(state
), "vscsi-devs/%s/state", ent
);
1036 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, state
, "%u", &device_state
);
1037 if (XENBUS_EXIST_ERR(err
))
1040 /* physical SCSI device */
1041 snprintf(str
, sizeof(str
), "vscsi-devs/%s/p-dev", ent
);
1042 val
= xenbus_read(XBT_NIL
, dev
->nodename
, str
, NULL
);
1044 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1045 "%d", XenbusStateClosed
);
1048 strlcpy(phy
, val
, VSCSI_NAMELEN
);
1051 /* virtual SCSI device */
1052 snprintf(str
, sizeof(str
), "vscsi-devs/%s/v-dev", ent
);
1053 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, str
, "%u:%u:%u:%u",
1054 &vir
.hst
, &vir
.chn
, &vir
.tgt
, &vir
.lun
);
1055 if (XENBUS_EXIST_ERR(err
)) {
1056 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1057 "%d", XenbusStateClosed
);
1062 case VSCSIBACK_OP_ADD_OR_DEL_LUN
:
1063 switch (device_state
) {
1064 case XenbusStateInitialising
:
1065 scsiback_do_add_lun(info
, state
, phy
, &vir
, 0);
1067 case XenbusStateConnected
:
1068 scsiback_do_add_lun(info
, state
, phy
, &vir
, 1);
1070 case XenbusStateClosing
:
1071 scsiback_do_del_lun(info
, state
, &vir
);
1078 case VSCSIBACK_OP_UPDATEDEV_STATE
:
1079 if (device_state
== XenbusStateInitialised
) {
1080 /* modify vscsi-devs/dev-x/state */
1081 if (xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1082 "%d", XenbusStateConnected
)) {
1083 pr_err("xenbus_printf error %s\n", str
);
1084 scsiback_del_translation_entry(info
, &vir
);
1085 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1086 "%d", XenbusStateClosed
);
1090 /* When it is necessary, processing is added here. */
1096 static void scsiback_do_lun_hotplug(struct vscsibk_info
*info
, int op
)
1100 unsigned int ndir
= 0;
1102 dir
= xenbus_directory(XBT_NIL
, info
->dev
->nodename
, "vscsi-devs",
1107 for (i
= 0; i
< ndir
; i
++)
1108 scsiback_do_1lun_hotplug(info
, op
, dir
[i
]);
1113 static void scsiback_frontend_changed(struct xenbus_device
*dev
,
1114 enum xenbus_state frontend_state
)
1116 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1118 switch (frontend_state
) {
1119 case XenbusStateInitialising
:
1122 case XenbusStateInitialised
:
1123 if (scsiback_map(info
))
1126 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1127 xenbus_switch_state(dev
, XenbusStateConnected
);
1130 case XenbusStateConnected
:
1131 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_UPDATEDEV_STATE
);
1133 if (dev
->state
== XenbusStateConnected
)
1136 xenbus_switch_state(dev
, XenbusStateConnected
);
1139 case XenbusStateClosing
:
1141 scsiback_disconnect(info
);
1143 xenbus_switch_state(dev
, XenbusStateClosing
);
1146 case XenbusStateClosed
:
1147 xenbus_switch_state(dev
, XenbusStateClosed
);
1148 if (xenbus_dev_is_online(dev
))
1150 /* fall through if not online */
1151 case XenbusStateUnknown
:
1152 device_unregister(&dev
->dev
);
1155 case XenbusStateReconfiguring
:
1156 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1157 xenbus_switch_state(dev
, XenbusStateReconfigured
);
1162 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
1169 Release the translation entry specfied
1171 static void scsiback_release_translation_entry(struct vscsibk_info
*info
)
1173 struct v2p_entry
*entry
, *tmp
;
1174 struct list_head
*head
= &(info
->v2p_entry_lists
);
1175 unsigned long flags
;
1177 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1179 list_for_each_entry_safe(entry
, tmp
, head
, l
)
1180 __scsiback_del_translation_entry(entry
);
1182 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1185 static int scsiback_remove(struct xenbus_device
*dev
)
1187 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1190 scsiback_disconnect(info
);
1192 scsiback_release_translation_entry(info
);
1194 dev_set_drvdata(&dev
->dev
, NULL
);
1199 static int scsiback_probe(struct xenbus_device
*dev
,
1200 const struct xenbus_device_id
*id
)
1204 struct vscsibk_info
*info
= kzalloc(sizeof(struct vscsibk_info
),
1207 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
1210 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating backend structure");
1214 dev_set_drvdata(&dev
->dev
, info
);
1216 info
->domid
= dev
->otherend_id
;
1217 spin_lock_init(&info
->ring_lock
);
1218 info
->ring_error
= 0;
1219 atomic_set(&info
->nr_unreplied_reqs
, 0);
1220 init_waitqueue_head(&info
->waiting_to_free
);
1223 INIT_LIST_HEAD(&info
->v2p_entry_lists
);
1224 spin_lock_init(&info
->v2p_lock
);
1226 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, "feature-sg-grant", "%u",
1229 xenbus_dev_error(dev
, err
, "writing feature-sg-grant");
1231 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
1238 pr_warn("%s failed\n", __func__
);
1239 scsiback_remove(dev
);
1244 static char *scsiback_dump_proto_id(struct scsiback_tport
*tport
)
1246 switch (tport
->tport_proto_id
) {
1247 case SCSI_PROTOCOL_SAS
:
1249 case SCSI_PROTOCOL_FCP
:
1251 case SCSI_PROTOCOL_ISCSI
:
1260 static char *scsiback_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1262 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1263 struct scsiback_tpg
, se_tpg
);
1264 struct scsiback_tport
*tport
= tpg
->tport
;
1266 return &tport
->tport_name
[0];
1269 static u16
scsiback_get_tag(struct se_portal_group
*se_tpg
)
1271 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1272 struct scsiback_tpg
, se_tpg
);
1273 return tpg
->tport_tpgt
;
1276 static struct se_wwn
*
1277 scsiback_make_tport(struct target_fabric_configfs
*tf
,
1278 struct config_group
*group
,
1281 struct scsiback_tport
*tport
;
1286 tport
= kzalloc(sizeof(struct scsiback_tport
), GFP_KERNEL
);
1288 return ERR_PTR(-ENOMEM
);
1290 tport
->tport_wwpn
= wwpn
;
1292 * Determine the emulated Protocol Identifier and Target Port Name
1293 * based on the incoming configfs directory name.
1295 ptr
= strstr(name
, "naa.");
1297 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1300 ptr
= strstr(name
, "fc.");
1302 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1303 off
= 3; /* Skip over "fc." */
1306 ptr
= strstr(name
, "iqn.");
1308 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1312 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name
);
1314 return ERR_PTR(-EINVAL
);
1317 if (strlen(name
) >= VSCSI_NAMELEN
) {
1318 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name
,
1319 scsiback_dump_proto_id(tport
), VSCSI_NAMELEN
);
1321 return ERR_PTR(-EINVAL
);
1323 snprintf(&tport
->tport_name
[0], VSCSI_NAMELEN
, "%s", &name
[off
]);
1325 pr_debug("Allocated emulated Target %s Address: %s\n",
1326 scsiback_dump_proto_id(tport
), name
);
1328 return &tport
->tport_wwn
;
1331 static void scsiback_drop_tport(struct se_wwn
*wwn
)
1333 struct scsiback_tport
*tport
= container_of(wwn
,
1334 struct scsiback_tport
, tport_wwn
);
1336 pr_debug("Deallocating emulated Target %s Address: %s\n",
1337 scsiback_dump_proto_id(tport
), tport
->tport_name
);
1342 static u32
scsiback_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1347 static int scsiback_check_stop_free(struct se_cmd
*se_cmd
)
1350 * Do not release struct se_cmd's containing a valid TMR pointer.
1351 * These will be released directly in scsiback_device_action()
1352 * with transport_generic_free_cmd().
1354 if (se_cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)
1357 transport_generic_free_cmd(se_cmd
, 0);
1361 static void scsiback_release_cmd(struct se_cmd
*se_cmd
)
1363 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1364 struct vscsibk_pend
, se_cmd
);
1366 kmem_cache_free(scsiback_cachep
, pending_req
);
1369 static int scsiback_shutdown_session(struct se_session
*se_sess
)
1374 static void scsiback_close_session(struct se_session
*se_sess
)
1378 static u32
scsiback_sess_get_index(struct se_session
*se_sess
)
1383 static int scsiback_write_pending(struct se_cmd
*se_cmd
)
1385 /* Go ahead and process the write immediately */
1386 target_execute_cmd(se_cmd
);
1391 static int scsiback_write_pending_status(struct se_cmd
*se_cmd
)
1396 static void scsiback_set_default_node_attrs(struct se_node_acl
*nacl
)
1400 static int scsiback_get_cmd_state(struct se_cmd
*se_cmd
)
1405 static int scsiback_queue_data_in(struct se_cmd
*se_cmd
)
1407 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1408 struct vscsibk_pend
, se_cmd
);
1410 pending_req
->result
= SAM_STAT_GOOD
;
1411 scsiback_cmd_done(pending_req
);
1415 static int scsiback_queue_status(struct se_cmd
*se_cmd
)
1417 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1418 struct vscsibk_pend
, se_cmd
);
1420 if (se_cmd
->sense_buffer
&&
1421 ((se_cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
1422 (se_cmd
->se_cmd_flags
& SCF_EMULATED_TASK_SENSE
)))
1423 pending_req
->result
= (DRIVER_SENSE
<< 24) |
1424 SAM_STAT_CHECK_CONDITION
;
1426 pending_req
->result
= se_cmd
->scsi_status
;
1428 scsiback_cmd_done(pending_req
);
1432 static void scsiback_queue_tm_rsp(struct se_cmd
*se_cmd
)
1434 struct se_tmr_req
*se_tmr
= se_cmd
->se_tmr_req
;
1435 struct scsiback_tmr
*tmr
= se_tmr
->fabric_tmr_ptr
;
1437 atomic_set(&tmr
->tmr_complete
, 1);
1438 wake_up(&tmr
->tmr_wait
);
1441 static void scsiback_aborted_task(struct se_cmd
*se_cmd
)
1445 static ssize_t
scsiback_tpg_param_show_alias(struct se_portal_group
*se_tpg
,
1448 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1452 mutex_lock(&tpg
->tv_tpg_mutex
);
1453 rb
= snprintf(page
, PAGE_SIZE
, "%s\n", tpg
->param_alias
);
1454 mutex_unlock(&tpg
->tv_tpg_mutex
);
1459 static ssize_t
scsiback_tpg_param_store_alias(struct se_portal_group
*se_tpg
,
1460 const char *page
, size_t count
)
1462 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1466 if (strlen(page
) >= VSCSI_NAMELEN
) {
1467 pr_err("param alias: %s, exceeds max: %d\n", page
,
1472 mutex_lock(&tpg
->tv_tpg_mutex
);
1473 len
= snprintf(tpg
->param_alias
, VSCSI_NAMELEN
, "%s", page
);
1474 if (tpg
->param_alias
[len
- 1] == '\n')
1475 tpg
->param_alias
[len
- 1] = '\0';
1476 mutex_unlock(&tpg
->tv_tpg_mutex
);
1481 TF_TPG_PARAM_ATTR(scsiback
, alias
, S_IRUGO
| S_IWUSR
);
1483 static struct configfs_attribute
*scsiback_param_attrs
[] = {
1484 &scsiback_tpg_param_alias
.attr
,
1488 static int scsiback_make_nexus(struct scsiback_tpg
*tpg
,
1491 struct se_portal_group
*se_tpg
;
1492 struct se_session
*se_sess
;
1493 struct scsiback_nexus
*tv_nexus
;
1495 mutex_lock(&tpg
->tv_tpg_mutex
);
1496 if (tpg
->tpg_nexus
) {
1497 mutex_unlock(&tpg
->tv_tpg_mutex
);
1498 pr_debug("tpg->tpg_nexus already exists\n");
1501 se_tpg
= &tpg
->se_tpg
;
1503 tv_nexus
= kzalloc(sizeof(struct scsiback_nexus
), GFP_KERNEL
);
1505 mutex_unlock(&tpg
->tv_tpg_mutex
);
1509 * Initialize the struct se_session pointer
1511 tv_nexus
->tvn_se_sess
= transport_init_session(TARGET_PROT_NORMAL
);
1512 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1513 mutex_unlock(&tpg
->tv_tpg_mutex
);
1517 se_sess
= tv_nexus
->tvn_se_sess
;
1519 * Since we are running in 'demo mode' this call with generate a
1520 * struct se_node_acl for the scsiback struct se_portal_group with
1521 * the SCSI Initiator port name of the passed configfs group 'name'.
1523 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1524 se_tpg
, (unsigned char *)name
);
1525 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1526 mutex_unlock(&tpg
->tv_tpg_mutex
);
1527 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1531 /* Now register the TCM pvscsi virtual I_T Nexus as active. */
1532 transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1533 tv_nexus
->tvn_se_sess
, tv_nexus
);
1534 tpg
->tpg_nexus
= tv_nexus
;
1536 mutex_unlock(&tpg
->tv_tpg_mutex
);
1540 transport_free_session(se_sess
);
1545 static int scsiback_drop_nexus(struct scsiback_tpg
*tpg
)
1547 struct se_session
*se_sess
;
1548 struct scsiback_nexus
*tv_nexus
;
1550 mutex_lock(&tpg
->tv_tpg_mutex
);
1551 tv_nexus
= tpg
->tpg_nexus
;
1553 mutex_unlock(&tpg
->tv_tpg_mutex
);
1557 se_sess
= tv_nexus
->tvn_se_sess
;
1559 mutex_unlock(&tpg
->tv_tpg_mutex
);
1563 if (tpg
->tv_tpg_port_count
!= 0) {
1564 mutex_unlock(&tpg
->tv_tpg_mutex
);
1565 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1566 tpg
->tv_tpg_port_count
);
1570 if (tpg
->tv_tpg_fe_count
!= 0) {
1571 mutex_unlock(&tpg
->tv_tpg_mutex
);
1572 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1573 tpg
->tv_tpg_fe_count
);
1577 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1578 scsiback_dump_proto_id(tpg
->tport
),
1579 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1582 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1584 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1585 tpg
->tpg_nexus
= NULL
;
1586 mutex_unlock(&tpg
->tv_tpg_mutex
);
1592 static ssize_t
scsiback_tpg_show_nexus(struct se_portal_group
*se_tpg
,
1595 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1596 struct scsiback_tpg
, se_tpg
);
1597 struct scsiback_nexus
*tv_nexus
;
1600 mutex_lock(&tpg
->tv_tpg_mutex
);
1601 tv_nexus
= tpg
->tpg_nexus
;
1603 mutex_unlock(&tpg
->tv_tpg_mutex
);
1606 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1607 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1608 mutex_unlock(&tpg
->tv_tpg_mutex
);
1613 static ssize_t
scsiback_tpg_store_nexus(struct se_portal_group
*se_tpg
,
1617 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1618 struct scsiback_tpg
, se_tpg
);
1619 struct scsiback_tport
*tport_wwn
= tpg
->tport
;
1620 unsigned char i_port
[VSCSI_NAMELEN
], *ptr
, *port_ptr
;
1623 * Shutdown the active I_T nexus if 'NULL' is passed.
1625 if (!strncmp(page
, "NULL", 4)) {
1626 ret
= scsiback_drop_nexus(tpg
);
1627 return (!ret
) ? count
: ret
;
1630 * Otherwise make sure the passed virtual Initiator port WWN matches
1631 * the fabric protocol_id set in scsiback_make_tport(), and call
1632 * scsiback_make_nexus().
1634 if (strlen(page
) >= VSCSI_NAMELEN
) {
1635 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1636 page
, VSCSI_NAMELEN
);
1639 snprintf(&i_port
[0], VSCSI_NAMELEN
, "%s", page
);
1641 ptr
= strstr(i_port
, "naa.");
1643 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1644 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1645 i_port
, scsiback_dump_proto_id(tport_wwn
));
1648 port_ptr
= &i_port
[0];
1651 ptr
= strstr(i_port
, "fc.");
1653 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1654 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1655 i_port
, scsiback_dump_proto_id(tport_wwn
));
1658 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1661 ptr
= strstr(i_port
, "iqn.");
1663 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1664 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1665 i_port
, scsiback_dump_proto_id(tport_wwn
));
1668 port_ptr
= &i_port
[0];
1671 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1675 * Clear any trailing newline for the NAA WWN
1678 if (i_port
[strlen(i_port
) - 1] == '\n')
1679 i_port
[strlen(i_port
) - 1] = '\0';
1681 ret
= scsiback_make_nexus(tpg
, port_ptr
);
1688 TF_TPG_BASE_ATTR(scsiback
, nexus
, S_IRUGO
| S_IWUSR
);
1690 static struct configfs_attribute
*scsiback_tpg_attrs
[] = {
1691 &scsiback_tpg_nexus
.attr
,
1696 scsiback_wwn_show_attr_version(struct target_fabric_configfs
*tf
,
1699 return sprintf(page
, "xen-pvscsi fabric module %s on %s/%s on "
1701 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1704 TF_WWN_ATTR_RO(scsiback
, version
);
1706 static struct configfs_attribute
*scsiback_wwn_attrs
[] = {
1707 &scsiback_wwn_version
.attr
,
1711 static char *scsiback_get_fabric_name(void)
1713 return "xen-pvscsi";
1716 static int scsiback_port_link(struct se_portal_group
*se_tpg
,
1719 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1720 struct scsiback_tpg
, se_tpg
);
1722 mutex_lock(&tpg
->tv_tpg_mutex
);
1723 tpg
->tv_tpg_port_count
++;
1724 mutex_unlock(&tpg
->tv_tpg_mutex
);
1729 static void scsiback_port_unlink(struct se_portal_group
*se_tpg
,
1732 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1733 struct scsiback_tpg
, se_tpg
);
1735 mutex_lock(&tpg
->tv_tpg_mutex
);
1736 tpg
->tv_tpg_port_count
--;
1737 mutex_unlock(&tpg
->tv_tpg_mutex
);
1740 static struct se_portal_group
*
1741 scsiback_make_tpg(struct se_wwn
*wwn
,
1742 struct config_group
*group
,
1745 struct scsiback_tport
*tport
= container_of(wwn
,
1746 struct scsiback_tport
, tport_wwn
);
1748 struct scsiback_tpg
*tpg
;
1752 if (strstr(name
, "tpgt_") != name
)
1753 return ERR_PTR(-EINVAL
);
1754 ret
= kstrtou16(name
+ 5, 10, &tpgt
);
1756 return ERR_PTR(ret
);
1758 tpg
= kzalloc(sizeof(struct scsiback_tpg
), GFP_KERNEL
);
1760 return ERR_PTR(-ENOMEM
);
1762 mutex_init(&tpg
->tv_tpg_mutex
);
1763 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1764 INIT_LIST_HEAD(&tpg
->info_list
);
1766 tpg
->tport_tpgt
= tpgt
;
1768 ret
= core_tpg_register(&scsiback_ops
, wwn
, &tpg
->se_tpg
,
1769 tport
->tport_proto_id
);
1774 mutex_lock(&scsiback_mutex
);
1775 list_add_tail(&tpg
->tv_tpg_list
, &scsiback_list
);
1776 mutex_unlock(&scsiback_mutex
);
1778 return &tpg
->se_tpg
;
1781 static void scsiback_drop_tpg(struct se_portal_group
*se_tpg
)
1783 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1784 struct scsiback_tpg
, se_tpg
);
1786 mutex_lock(&scsiback_mutex
);
1787 list_del(&tpg
->tv_tpg_list
);
1788 mutex_unlock(&scsiback_mutex
);
1790 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1792 scsiback_drop_nexus(tpg
);
1794 * Deregister the se_tpg from TCM.
1796 core_tpg_deregister(se_tpg
);
1800 static int scsiback_check_true(struct se_portal_group
*se_tpg
)
1805 static int scsiback_check_false(struct se_portal_group
*se_tpg
)
1810 static const struct target_core_fabric_ops scsiback_ops
= {
1811 .module
= THIS_MODULE
,
1812 .name
= "xen-pvscsi",
1813 .get_fabric_name
= scsiback_get_fabric_name
,
1814 .tpg_get_wwn
= scsiback_get_fabric_wwn
,
1815 .tpg_get_tag
= scsiback_get_tag
,
1816 .tpg_check_demo_mode
= scsiback_check_true
,
1817 .tpg_check_demo_mode_cache
= scsiback_check_true
,
1818 .tpg_check_demo_mode_write_protect
= scsiback_check_false
,
1819 .tpg_check_prod_mode_write_protect
= scsiback_check_false
,
1820 .tpg_get_inst_index
= scsiback_tpg_get_inst_index
,
1821 .check_stop_free
= scsiback_check_stop_free
,
1822 .release_cmd
= scsiback_release_cmd
,
1823 .shutdown_session
= scsiback_shutdown_session
,
1824 .close_session
= scsiback_close_session
,
1825 .sess_get_index
= scsiback_sess_get_index
,
1826 .sess_get_initiator_sid
= NULL
,
1827 .write_pending
= scsiback_write_pending
,
1828 .write_pending_status
= scsiback_write_pending_status
,
1829 .set_default_node_attributes
= scsiback_set_default_node_attrs
,
1830 .get_cmd_state
= scsiback_get_cmd_state
,
1831 .queue_data_in
= scsiback_queue_data_in
,
1832 .queue_status
= scsiback_queue_status
,
1833 .queue_tm_rsp
= scsiback_queue_tm_rsp
,
1834 .aborted_task
= scsiback_aborted_task
,
1836 * Setup callers for generic logic in target_core_fabric_configfs.c
1838 .fabric_make_wwn
= scsiback_make_tport
,
1839 .fabric_drop_wwn
= scsiback_drop_tport
,
1840 .fabric_make_tpg
= scsiback_make_tpg
,
1841 .fabric_drop_tpg
= scsiback_drop_tpg
,
1842 .fabric_post_link
= scsiback_port_link
,
1843 .fabric_pre_unlink
= scsiback_port_unlink
,
1845 .tfc_wwn_attrs
= scsiback_wwn_attrs
,
1846 .tfc_tpg_base_attrs
= scsiback_tpg_attrs
,
1847 .tfc_tpg_param_attrs
= scsiback_param_attrs
,
1850 static const struct xenbus_device_id scsiback_ids
[] = {
1855 static struct xenbus_driver scsiback_driver
= {
1856 .ids
= scsiback_ids
,
1857 .probe
= scsiback_probe
,
1858 .remove
= scsiback_remove
,
1859 .otherend_changed
= scsiback_frontend_changed
1862 static void scsiback_init_pend(void *p
)
1864 struct vscsibk_pend
*pend
= p
;
1867 memset(pend
, 0, sizeof(*pend
));
1868 for (i
= 0; i
< VSCSI_MAX_GRANTS
; i
++)
1869 pend
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
1872 static int __init
scsiback_init(void)
1879 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE
"\n",
1880 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1882 scsiback_cachep
= kmem_cache_create("vscsiif_cache",
1883 sizeof(struct vscsibk_pend
), 0, 0, scsiback_init_pend
);
1884 if (!scsiback_cachep
)
1887 ret
= xenbus_register_backend(&scsiback_driver
);
1889 goto out_cache_destroy
;
1891 ret
= target_register_template(&scsiback_ops
);
1893 goto out_unregister_xenbus
;
1897 out_unregister_xenbus
:
1898 xenbus_unregister_driver(&scsiback_driver
);
1900 kmem_cache_destroy(scsiback_cachep
);
1901 pr_err("%s: error %d\n", __func__
, ret
);
1905 static void __exit
scsiback_exit(void)
1909 while (free_pages_num
) {
1910 if (get_free_page(&page
))
1912 gnttab_free_pages(1, &page
);
1914 target_unregister_template(&scsiback_ops
);
1915 xenbus_unregister_driver(&scsiback_driver
);
1916 kmem_cache_destroy(scsiback_cachep
);
1919 module_init(scsiback_init
);
1920 module_exit(scsiback_exit
);
1922 MODULE_DESCRIPTION("Xen SCSI backend driver");
1923 MODULE_LICENSE("Dual BSD/GPL");
1924 MODULE_ALIAS("xen-backend:vscsi");
1925 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");