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
;
139 struct scsiback_tmr
{
140 atomic_t tmr_complete
;
141 wait_queue_head_t tmr_wait
;
144 struct scsiback_nexus
{
145 /* Pointer to TCM session for I_T Nexus */
146 struct se_session
*tvn_se_sess
;
149 struct scsiback_tport
{
150 /* SCSI protocol the tport is providing */
152 /* Binary World Wide unique Port Name for pvscsi Target port */
154 /* ASCII formatted WWPN for pvscsi Target port */
155 char tport_name
[VSCSI_NAMELEN
];
156 /* Returned by scsiback_make_tport() */
157 struct se_wwn tport_wwn
;
160 struct scsiback_tpg
{
161 /* scsiback port target portal group tag for TCM */
163 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
164 int tv_tpg_port_count
;
165 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
167 /* list for scsiback_list */
168 struct list_head tv_tpg_list
;
169 /* Used to protect access for tpg_nexus */
170 struct mutex tv_tpg_mutex
;
171 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
172 struct scsiback_nexus
*tpg_nexus
;
173 /* Pointer back to scsiback_tport */
174 struct scsiback_tport
*tport
;
175 /* Returned by scsiback_make_tpg() */
176 struct se_portal_group se_tpg
;
177 /* alias used in xenstore */
178 char param_alias
[VSCSI_NAMELEN
];
179 /* list of info structures related to this target portal group */
180 struct list_head info_list
;
183 #define SCSIBACK_INVALID_HANDLE (~0)
185 static bool log_print_stat
;
186 module_param(log_print_stat
, bool, 0644);
188 static int scsiback_max_buffer_pages
= 1024;
189 module_param_named(max_buffer_pages
, scsiback_max_buffer_pages
, int, 0644);
190 MODULE_PARM_DESC(max_buffer_pages
,
191 "Maximum number of free pages to keep in backend buffer");
193 static struct kmem_cache
*scsiback_cachep
;
194 static DEFINE_SPINLOCK(free_pages_lock
);
195 static int free_pages_num
;
196 static LIST_HEAD(scsiback_free_pages
);
198 /* Global spinlock to protect scsiback TPG list */
199 static DEFINE_MUTEX(scsiback_mutex
);
200 static LIST_HEAD(scsiback_list
);
202 static void scsiback_get(struct vscsibk_info
*info
)
204 atomic_inc(&info
->nr_unreplied_reqs
);
207 static void scsiback_put(struct vscsibk_info
*info
)
209 if (atomic_dec_and_test(&info
->nr_unreplied_reqs
))
210 wake_up(&info
->waiting_to_free
);
213 static void put_free_pages(struct page
**page
, int num
)
216 int i
= free_pages_num
+ num
, n
= num
;
220 if (i
> scsiback_max_buffer_pages
) {
221 n
= min(num
, i
- scsiback_max_buffer_pages
);
222 gnttab_free_pages(n
, page
+ num
- n
);
225 spin_lock_irqsave(&free_pages_lock
, flags
);
226 for (i
= 0; i
< n
; i
++)
227 list_add(&page
[i
]->lru
, &scsiback_free_pages
);
229 spin_unlock_irqrestore(&free_pages_lock
, flags
);
232 static int get_free_page(struct page
**page
)
236 spin_lock_irqsave(&free_pages_lock
, flags
);
237 if (list_empty(&scsiback_free_pages
)) {
238 spin_unlock_irqrestore(&free_pages_lock
, flags
);
239 return gnttab_alloc_pages(1, page
);
241 page
[0] = list_first_entry(&scsiback_free_pages
, struct page
, lru
);
242 list_del(&page
[0]->lru
);
244 spin_unlock_irqrestore(&free_pages_lock
, flags
);
248 static unsigned long vaddr_page(struct page
*page
)
250 unsigned long pfn
= page_to_pfn(page
);
252 return (unsigned long)pfn_to_kaddr(pfn
);
255 static unsigned long vaddr(struct vscsibk_pend
*req
, int seg
)
257 return vaddr_page(req
->pages
[seg
]);
260 static void scsiback_print_status(char *sense_buffer
, int errors
,
261 struct vscsibk_pend
*pending_req
)
263 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
265 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
266 tpg
->tport
->tport_name
, pending_req
->v2p
->lun
,
267 pending_req
->cmnd
[0], status_byte(errors
), msg_byte(errors
),
268 host_byte(errors
), driver_byte(errors
));
271 static void scsiback_fast_flush_area(struct vscsibk_pend
*req
)
273 struct gnttab_unmap_grant_ref unmap
[VSCSI_GRANT_BATCH
];
274 struct page
*pages
[VSCSI_GRANT_BATCH
];
275 unsigned int i
, invcount
= 0;
276 grant_handle_t handle
;
286 for (i
= 0; i
< req
->n_grants
; i
++) {
287 handle
= req
->grant_handles
[i
];
288 if (handle
== SCSIBACK_INVALID_HANDLE
)
290 gnttab_set_unmap_op(&unmap
[invcount
], vaddr(req
, i
),
291 GNTMAP_host_map
, handle
);
292 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
293 pages
[invcount
] = req
->pages
[i
];
294 put_page(pages
[invcount
]);
296 if (invcount
< VSCSI_GRANT_BATCH
)
298 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
304 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
308 put_free_pages(req
->pages
, req
->n_grants
);
312 static void scsiback_free_translation_entry(struct kref
*kref
)
314 struct v2p_entry
*entry
= container_of(kref
, struct v2p_entry
, kref
);
315 struct scsiback_tpg
*tpg
= entry
->tpg
;
317 mutex_lock(&tpg
->tv_tpg_mutex
);
318 tpg
->tv_tpg_fe_count
--;
319 mutex_unlock(&tpg
->tv_tpg_mutex
);
324 static void scsiback_do_resp_with_sense(char *sense_buffer
, int32_t result
,
325 uint32_t resid
, struct vscsibk_pend
*pending_req
)
327 struct vscsiif_response
*ring_res
;
328 struct vscsibk_info
*info
= pending_req
->info
;
330 struct scsi_sense_hdr sshdr
;
334 spin_lock_irqsave(&info
->ring_lock
, flags
);
336 ring_res
= RING_GET_RESPONSE(&info
->ring
, info
->ring
.rsp_prod_pvt
);
337 info
->ring
.rsp_prod_pvt
++;
339 ring_res
->rslt
= result
;
340 ring_res
->rqid
= pending_req
->rqid
;
342 if (sense_buffer
!= NULL
&&
343 scsi_normalize_sense(sense_buffer
, VSCSIIF_SENSE_BUFFERSIZE
,
345 len
= min_t(unsigned, 8 + sense_buffer
[7],
346 VSCSIIF_SENSE_BUFFERSIZE
);
347 memcpy(ring_res
->sense_buffer
, sense_buffer
, len
);
348 ring_res
->sense_len
= len
;
350 ring_res
->sense_len
= 0;
353 ring_res
->residual_len
= resid
;
355 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info
->ring
, notify
);
356 spin_unlock_irqrestore(&info
->ring_lock
, flags
);
359 notify_remote_via_irq(info
->irq
);
361 if (pending_req
->v2p
)
362 kref_put(&pending_req
->v2p
->kref
,
363 scsiback_free_translation_entry
);
366 static void scsiback_cmd_done(struct vscsibk_pend
*pending_req
)
368 struct vscsibk_info
*info
= pending_req
->info
;
369 unsigned char *sense_buffer
;
373 sense_buffer
= pending_req
->sense_buffer
;
374 resid
= pending_req
->se_cmd
.residual_count
;
375 errors
= pending_req
->result
;
377 if (errors
&& log_print_stat
)
378 scsiback_print_status(sense_buffer
, errors
, pending_req
);
380 scsiback_fast_flush_area(pending_req
);
381 scsiback_do_resp_with_sense(sense_buffer
, errors
, resid
, pending_req
);
385 static void scsiback_cmd_exec(struct vscsibk_pend
*pending_req
)
387 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
388 struct se_session
*sess
= pending_req
->v2p
->tpg
->tpg_nexus
->tvn_se_sess
;
391 memset(pending_req
->sense_buffer
, 0, VSCSIIF_SENSE_BUFFERSIZE
);
393 memset(se_cmd
, 0, sizeof(*se_cmd
));
395 scsiback_get(pending_req
->info
);
396 se_cmd
->tag
= pending_req
->rqid
;
397 rc
= target_submit_cmd_map_sgls(se_cmd
, sess
, pending_req
->cmnd
,
398 pending_req
->sense_buffer
, pending_req
->v2p
->lun
,
399 pending_req
->data_len
, 0,
400 pending_req
->sc_data_direction
, 0,
401 pending_req
->sgl
, pending_req
->n_sg
,
404 transport_send_check_condition_and_sense(se_cmd
,
405 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
406 transport_generic_free_cmd(se_cmd
, 0);
410 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref
*map
,
411 struct page
**pg
, grant_handle_t
*grant
, int cnt
)
418 err
= gnttab_map_refs(map
, NULL
, pg
, cnt
);
420 for (i
= 0; i
< cnt
; i
++) {
421 if (unlikely(map
[i
].status
!= GNTST_okay
)) {
422 pr_err("invalid buffer -- could not remap it\n");
423 map
[i
].handle
= SCSIBACK_INVALID_HANDLE
;
428 grant
[i
] = map
[i
].handle
;
433 static int scsiback_gnttab_data_map_list(struct vscsibk_pend
*pending_req
,
434 struct scsiif_request_segment
*seg
, struct page
**pg
,
435 grant_handle_t
*grant
, int cnt
, u32 flags
)
437 int mapcount
= 0, i
, err
= 0;
438 struct gnttab_map_grant_ref map
[VSCSI_GRANT_BATCH
];
439 struct vscsibk_info
*info
= pending_req
->info
;
441 for (i
= 0; i
< cnt
; i
++) {
442 if (get_free_page(pg
+ mapcount
)) {
443 put_free_pages(pg
, mapcount
);
444 pr_err("no grant page\n");
447 gnttab_set_map_op(&map
[mapcount
], vaddr_page(pg
[mapcount
]),
448 flags
, seg
[i
].gref
, info
->domid
);
450 if (mapcount
< VSCSI_GRANT_BATCH
)
452 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
455 pending_req
->n_grants
+= mapcount
;
460 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
461 pending_req
->n_grants
+= mapcount
;
465 static int scsiback_gnttab_data_map(struct vscsiif_request
*ring_req
,
466 struct vscsibk_pend
*pending_req
)
469 int i
, err
, n_segs
, i_seg
= 0;
471 struct scsiif_request_segment
*seg
;
472 unsigned long end_seg
= 0;
473 unsigned int nr_segments
= (unsigned int)ring_req
->nr_segments
;
474 unsigned int nr_sgl
= 0;
475 struct scatterlist
*sg
;
476 grant_handle_t
*grant
;
478 pending_req
->n_sg
= 0;
479 pending_req
->n_grants
= 0;
480 pending_req
->data_len
= 0;
482 nr_segments
&= ~VSCSIIF_SG_GRANT
;
486 if (nr_segments
> VSCSIIF_SG_TABLESIZE
) {
487 pr_debug("invalid parameter nr_seg = %d\n",
488 ring_req
->nr_segments
);
492 if (ring_req
->nr_segments
& VSCSIIF_SG_GRANT
) {
493 err
= scsiback_gnttab_data_map_list(pending_req
, ring_req
->seg
,
494 pending_req
->pages
, pending_req
->grant_handles
,
495 nr_segments
, GNTMAP_host_map
| GNTMAP_readonly
);
498 nr_sgl
= nr_segments
;
500 for (i
= 0; i
< nr_sgl
; i
++) {
501 n_segs
= ring_req
->seg
[i
].length
/
502 sizeof(struct scsiif_request_segment
);
503 if ((unsigned)ring_req
->seg
[i
].offset
+
504 (unsigned)ring_req
->seg
[i
].length
> PAGE_SIZE
||
505 n_segs
* sizeof(struct scsiif_request_segment
) !=
506 ring_req
->seg
[i
].length
)
508 nr_segments
+= n_segs
;
510 if (nr_segments
> SG_ALL
) {
511 pr_debug("invalid nr_seg = %d\n", nr_segments
);
516 /* free of (sgl) in fast_flush_area() */
517 pending_req
->sgl
= kmalloc_array(nr_segments
,
518 sizeof(struct scatterlist
), GFP_KERNEL
);
519 if (!pending_req
->sgl
)
522 sg_init_table(pending_req
->sgl
, nr_segments
);
523 pending_req
->n_sg
= nr_segments
;
525 flags
= GNTMAP_host_map
;
526 if (pending_req
->sc_data_direction
== DMA_TO_DEVICE
)
527 flags
|= GNTMAP_readonly
;
529 pg
= pending_req
->pages
+ nr_sgl
;
530 grant
= pending_req
->grant_handles
+ nr_sgl
;
533 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
534 pg
, grant
, nr_segments
, flags
);
538 for (i
= 0; i
< nr_sgl
; i
++) {
539 seg
= (struct scsiif_request_segment
*)(
540 vaddr(pending_req
, i
) + ring_req
->seg
[i
].offset
);
541 n_segs
= ring_req
->seg
[i
].length
/
542 sizeof(struct scsiif_request_segment
);
543 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
544 pg
, grant
, n_segs
, flags
);
550 end_seg
= vaddr(pending_req
, 0) + ring_req
->seg
[0].offset
;
551 seg
= (struct scsiif_request_segment
*)end_seg
;
552 end_seg
+= ring_req
->seg
[0].length
;
553 pg
= pending_req
->pages
+ nr_sgl
;
556 for_each_sg(pending_req
->sgl
, sg
, nr_segments
, i
) {
557 sg_set_page(sg
, pg
[i
], seg
->length
, seg
->offset
);
558 pending_req
->data_len
+= seg
->length
;
560 if (nr_sgl
&& (unsigned long)seg
>= end_seg
) {
562 end_seg
= vaddr(pending_req
, i_seg
) +
563 ring_req
->seg
[i_seg
].offset
;
564 seg
= (struct scsiif_request_segment
*)end_seg
;
565 end_seg
+= ring_req
->seg
[i_seg
].length
;
567 if (sg
->offset
>= PAGE_SIZE
||
568 sg
->length
> PAGE_SIZE
||
569 sg
->offset
+ sg
->length
> PAGE_SIZE
)
576 static void scsiback_disconnect(struct vscsibk_info
*info
)
578 wait_event(info
->waiting_to_free
,
579 atomic_read(&info
->nr_unreplied_reqs
) == 0);
581 unbind_from_irqhandler(info
->irq
, info
);
583 xenbus_unmap_ring_vfree(info
->dev
, info
->ring
.sring
);
586 static void scsiback_device_action(struct vscsibk_pend
*pending_req
,
587 enum tcm_tmreq_table act
, int tag
)
589 int rc
, err
= FAILED
;
590 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
591 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
592 struct scsiback_tmr
*tmr
;
594 tmr
= kzalloc(sizeof(struct scsiback_tmr
), GFP_KERNEL
);
598 init_waitqueue_head(&tmr
->tmr_wait
);
600 transport_init_se_cmd(se_cmd
, tpg
->se_tpg
.se_tpg_tfo
,
601 tpg
->tpg_nexus
->tvn_se_sess
, 0, DMA_NONE
, TCM_SIMPLE_TAG
,
602 &pending_req
->sense_buffer
[0]);
604 rc
= core_tmr_alloc_req(se_cmd
, tmr
, act
, GFP_KERNEL
);
608 se_cmd
->se_tmr_req
->ref_task_tag
= tag
;
610 if (transport_lookup_tmr_lun(se_cmd
, pending_req
->v2p
->lun
) < 0)
613 transport_generic_handle_tmr(se_cmd
);
614 wait_event(tmr
->tmr_wait
, atomic_read(&tmr
->tmr_complete
));
616 err
= (se_cmd
->se_tmr_req
->response
== TMR_FUNCTION_COMPLETE
) ?
621 transport_generic_free_cmd(&pending_req
->se_cmd
, 1);
625 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
627 kmem_cache_free(scsiback_cachep
, pending_req
);
631 Perform virtual to physical translation
633 static struct v2p_entry
*scsiback_do_translation(struct vscsibk_info
*info
,
636 struct v2p_entry
*entry
;
637 struct list_head
*head
= &(info
->v2p_entry_lists
);
640 spin_lock_irqsave(&info
->v2p_lock
, flags
);
641 list_for_each_entry(entry
, head
, l
) {
642 if ((entry
->v
.chn
== v
->chn
) &&
643 (entry
->v
.tgt
== v
->tgt
) &&
644 (entry
->v
.lun
== v
->lun
)) {
645 kref_get(&entry
->kref
);
652 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
656 static int prepare_pending_reqs(struct vscsibk_info
*info
,
657 struct vscsiif_request
*ring_req
,
658 struct vscsibk_pend
*pending_req
)
660 struct v2p_entry
*v2p
;
661 struct ids_tuple vir
;
663 pending_req
->rqid
= ring_req
->rqid
;
664 pending_req
->info
= info
;
666 vir
.chn
= ring_req
->channel
;
667 vir
.tgt
= ring_req
->id
;
668 vir
.lun
= ring_req
->lun
;
670 v2p
= scsiback_do_translation(info
, &vir
);
672 pending_req
->v2p
= NULL
;
673 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
674 vir
.chn
, vir
.tgt
, vir
.lun
);
677 pending_req
->v2p
= v2p
;
679 /* request range check from frontend */
680 pending_req
->sc_data_direction
= ring_req
->sc_data_direction
;
681 if ((pending_req
->sc_data_direction
!= DMA_BIDIRECTIONAL
) &&
682 (pending_req
->sc_data_direction
!= DMA_TO_DEVICE
) &&
683 (pending_req
->sc_data_direction
!= DMA_FROM_DEVICE
) &&
684 (pending_req
->sc_data_direction
!= DMA_NONE
)) {
685 pr_debug("invalid parameter data_dir = %d\n",
686 pending_req
->sc_data_direction
);
690 pending_req
->cmd_len
= ring_req
->cmd_len
;
691 if (pending_req
->cmd_len
> VSCSIIF_MAX_COMMAND_SIZE
) {
692 pr_debug("invalid parameter cmd_len = %d\n",
693 pending_req
->cmd_len
);
696 memcpy(pending_req
->cmnd
, ring_req
->cmnd
, pending_req
->cmd_len
);
701 static int scsiback_do_cmd_fn(struct vscsibk_info
*info
)
703 struct vscsiif_back_ring
*ring
= &info
->ring
;
704 struct vscsiif_request ring_req
;
705 struct vscsibk_pend
*pending_req
;
711 rp
= ring
->sring
->req_prod
;
712 rmb(); /* guest system is accessing ring, too */
714 if (RING_REQUEST_PROD_OVERFLOW(ring
, rp
)) {
715 rc
= ring
->rsp_prod_pvt
;
716 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
717 info
->domid
, rp
, rc
, rp
- rc
);
718 info
->ring_error
= 1;
723 if (RING_REQUEST_CONS_OVERFLOW(ring
, rc
))
725 pending_req
= kmem_cache_alloc(scsiback_cachep
, GFP_KERNEL
);
729 RING_COPY_REQUEST(ring
, rc
, &ring_req
);
730 ring
->req_cons
= ++rc
;
732 err
= prepare_pending_reqs(info
, &ring_req
, pending_req
);
736 result
= DID_NO_CONNECT
;
739 result
= DRIVER_ERROR
;
742 scsiback_do_resp_with_sense(NULL
, result
<< 24, 0,
744 kmem_cache_free(scsiback_cachep
, pending_req
);
748 switch (ring_req
.act
) {
749 case VSCSIIF_ACT_SCSI_CDB
:
750 if (scsiback_gnttab_data_map(&ring_req
, pending_req
)) {
751 scsiback_fast_flush_area(pending_req
);
752 scsiback_do_resp_with_sense(NULL
,
753 DRIVER_ERROR
<< 24, 0, pending_req
);
754 kmem_cache_free(scsiback_cachep
, pending_req
);
756 scsiback_cmd_exec(pending_req
);
759 case VSCSIIF_ACT_SCSI_ABORT
:
760 scsiback_device_action(pending_req
, TMR_ABORT_TASK
,
763 case VSCSIIF_ACT_SCSI_RESET
:
764 scsiback_device_action(pending_req
, TMR_LUN_RESET
, 0);
767 pr_err_ratelimited("invalid request\n");
768 scsiback_do_resp_with_sense(NULL
, DRIVER_ERROR
<< 24,
770 kmem_cache_free(scsiback_cachep
, pending_req
);
774 /* Yield point for this unbounded loop. */
778 RING_FINAL_CHECK_FOR_REQUESTS(&info
->ring
, more_to_do
);
782 static irqreturn_t
scsiback_irq_fn(int irq
, void *dev_id
)
784 struct vscsibk_info
*info
= dev_id
;
786 if (info
->ring_error
)
789 while (scsiback_do_cmd_fn(info
))
795 static int scsiback_init_sring(struct vscsibk_info
*info
, grant_ref_t ring_ref
,
796 evtchn_port_t evtchn
)
799 struct vscsiif_sring
*sring
;
805 err
= xenbus_map_ring_valloc(info
->dev
, &ring_ref
, 1, &area
);
809 sring
= (struct vscsiif_sring
*)area
;
810 BACK_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
812 err
= bind_interdomain_evtchn_to_irq(info
->domid
, evtchn
);
818 err
= request_threaded_irq(info
->irq
, NULL
, scsiback_irq_fn
,
819 IRQF_ONESHOT
, "vscsiif-backend", info
);
826 unbind_from_irqhandler(info
->irq
, info
);
829 xenbus_unmap_ring_vfree(info
->dev
, area
);
834 static int scsiback_map(struct vscsibk_info
*info
)
836 struct xenbus_device
*dev
= info
->dev
;
837 unsigned int ring_ref
, evtchn
;
840 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
841 "ring-ref", "%u", &ring_ref
,
842 "event-channel", "%u", &evtchn
, NULL
);
844 xenbus_dev_fatal(dev
, err
, "reading %s ring", dev
->otherend
);
848 return scsiback_init_sring(info
, ring_ref
, evtchn
);
852 Check for a translation entry being present
854 static struct v2p_entry
*scsiback_chk_translation_entry(
855 struct vscsibk_info
*info
, struct ids_tuple
*v
)
857 struct list_head
*head
= &(info
->v2p_entry_lists
);
858 struct v2p_entry
*entry
;
860 list_for_each_entry(entry
, head
, l
)
861 if ((entry
->v
.chn
== v
->chn
) &&
862 (entry
->v
.tgt
== v
->tgt
) &&
863 (entry
->v
.lun
== v
->lun
))
870 Add a new translation entry
872 static int scsiback_add_translation_entry(struct vscsibk_info
*info
,
873 char *phy
, struct ids_tuple
*v
)
876 struct v2p_entry
*new;
879 unsigned long long unpacked_lun
;
880 struct se_lun
*se_lun
;
881 struct scsiback_tpg
*tpg_entry
, *tpg
= NULL
;
882 char *error
= "doesn't exist";
884 lunp
= strrchr(phy
, ':');
886 pr_err("illegal format of physical device %s\n", phy
);
891 err
= kstrtoull(lunp
, 10, &unpacked_lun
);
893 pr_err("lun number not valid: %s\n", lunp
);
897 mutex_lock(&scsiback_mutex
);
898 list_for_each_entry(tpg_entry
, &scsiback_list
, tv_tpg_list
) {
899 if (!strcmp(phy
, tpg_entry
->tport
->tport_name
) ||
900 !strcmp(phy
, tpg_entry
->param_alias
)) {
901 mutex_lock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
902 hlist_for_each_entry(se_lun
, &tpg_entry
->se_tpg
.tpg_lun_hlist
, link
) {
903 if (se_lun
->unpacked_lun
== unpacked_lun
) {
904 if (!tpg_entry
->tpg_nexus
)
905 error
= "nexus undefined";
911 mutex_unlock(&tpg_entry
->se_tpg
.tpg_lun_mutex
);
916 mutex_lock(&tpg
->tv_tpg_mutex
);
917 tpg
->tv_tpg_fe_count
++;
918 mutex_unlock(&tpg
->tv_tpg_mutex
);
920 mutex_unlock(&scsiback_mutex
);
923 pr_err("%s:%llu %s\n", phy
, unpacked_lun
, error
);
927 new = kmalloc(sizeof(struct v2p_entry
), GFP_KERNEL
);
933 spin_lock_irqsave(&info
->v2p_lock
, flags
);
935 /* Check double assignment to identical virtual ID */
936 if (scsiback_chk_translation_entry(info
, v
)) {
937 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
942 /* Create a new translation entry and add to the list */
943 kref_init(&new->kref
);
946 new->lun
= unpacked_lun
;
947 list_add_tail(&new->l
, &info
->v2p_entry_lists
);
950 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
954 mutex_lock(&tpg
->tv_tpg_mutex
);
955 tpg
->tv_tpg_fe_count
--;
956 mutex_unlock(&tpg
->tv_tpg_mutex
);
963 static void __scsiback_del_translation_entry(struct v2p_entry
*entry
)
966 kref_put(&entry
->kref
, scsiback_free_translation_entry
);
970 Delete the translation entry specified
972 static int scsiback_del_translation_entry(struct vscsibk_info
*info
,
975 struct v2p_entry
*entry
;
979 spin_lock_irqsave(&info
->v2p_lock
, flags
);
980 /* Find out the translation entry specified */
981 entry
= scsiback_chk_translation_entry(info
, v
);
983 __scsiback_del_translation_entry(entry
);
987 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
991 static void scsiback_do_add_lun(struct vscsibk_info
*info
, const char *state
,
992 char *phy
, struct ids_tuple
*vir
, int try)
994 struct v2p_entry
*entry
;
998 spin_lock_irqsave(&info
->v2p_lock
, flags
);
999 entry
= scsiback_chk_translation_entry(info
, vir
);
1000 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1004 if (!scsiback_add_translation_entry(info
, phy
, vir
)) {
1005 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1006 "%d", XenbusStateInitialised
)) {
1007 pr_err("xenbus_printf error %s\n", state
);
1008 scsiback_del_translation_entry(info
, vir
);
1011 xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1012 "%d", XenbusStateClosed
);
1016 static void scsiback_do_del_lun(struct vscsibk_info
*info
, const char *state
,
1017 struct ids_tuple
*vir
)
1019 if (!scsiback_del_translation_entry(info
, vir
)) {
1020 if (xenbus_printf(XBT_NIL
, info
->dev
->nodename
, state
,
1021 "%d", XenbusStateClosed
))
1022 pr_err("xenbus_printf error %s\n", state
);
1026 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1027 #define VSCSIBACK_OP_UPDATEDEV_STATE 2
1029 static void scsiback_do_1lun_hotplug(struct vscsibk_info
*info
, int op
,
1033 struct ids_tuple vir
;
1036 char phy
[VSCSI_NAMELEN
];
1039 struct xenbus_device
*dev
= info
->dev
;
1042 snprintf(state
, sizeof(state
), "vscsi-devs/%s/state", ent
);
1043 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, state
, "%u", &device_state
);
1044 if (XENBUS_EXIST_ERR(err
))
1047 /* physical SCSI device */
1048 snprintf(str
, sizeof(str
), "vscsi-devs/%s/p-dev", ent
);
1049 val
= xenbus_read(XBT_NIL
, dev
->nodename
, str
, NULL
);
1051 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1052 "%d", XenbusStateClosed
);
1055 strlcpy(phy
, val
, VSCSI_NAMELEN
);
1058 /* virtual SCSI device */
1059 snprintf(str
, sizeof(str
), "vscsi-devs/%s/v-dev", ent
);
1060 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, str
, "%u:%u:%u:%u",
1061 &vir
.hst
, &vir
.chn
, &vir
.tgt
, &vir
.lun
);
1062 if (XENBUS_EXIST_ERR(err
)) {
1063 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1064 "%d", XenbusStateClosed
);
1069 case VSCSIBACK_OP_ADD_OR_DEL_LUN
:
1070 switch (device_state
) {
1071 case XenbusStateInitialising
:
1072 scsiback_do_add_lun(info
, state
, phy
, &vir
, 0);
1074 case XenbusStateConnected
:
1075 scsiback_do_add_lun(info
, state
, phy
, &vir
, 1);
1077 case XenbusStateClosing
:
1078 scsiback_do_del_lun(info
, state
, &vir
);
1085 case VSCSIBACK_OP_UPDATEDEV_STATE
:
1086 if (device_state
== XenbusStateInitialised
) {
1087 /* modify vscsi-devs/dev-x/state */
1088 if (xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1089 "%d", XenbusStateConnected
)) {
1090 pr_err("xenbus_printf error %s\n", str
);
1091 scsiback_del_translation_entry(info
, &vir
);
1092 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1093 "%d", XenbusStateClosed
);
1097 /* When it is necessary, processing is added here. */
1103 static void scsiback_do_lun_hotplug(struct vscsibk_info
*info
, int op
)
1107 unsigned int ndir
= 0;
1109 dir
= xenbus_directory(XBT_NIL
, info
->dev
->nodename
, "vscsi-devs",
1114 for (i
= 0; i
< ndir
; i
++)
1115 scsiback_do_1lun_hotplug(info
, op
, dir
[i
]);
1120 static void scsiback_frontend_changed(struct xenbus_device
*dev
,
1121 enum xenbus_state frontend_state
)
1123 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1125 switch (frontend_state
) {
1126 case XenbusStateInitialising
:
1129 case XenbusStateInitialised
:
1130 if (scsiback_map(info
))
1133 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1134 xenbus_switch_state(dev
, XenbusStateConnected
);
1137 case XenbusStateConnected
:
1138 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_UPDATEDEV_STATE
);
1140 if (dev
->state
== XenbusStateConnected
)
1143 xenbus_switch_state(dev
, XenbusStateConnected
);
1146 case XenbusStateClosing
:
1148 scsiback_disconnect(info
);
1150 xenbus_switch_state(dev
, XenbusStateClosing
);
1153 case XenbusStateClosed
:
1154 xenbus_switch_state(dev
, XenbusStateClosed
);
1155 if (xenbus_dev_is_online(dev
))
1157 /* fall through if not online */
1158 case XenbusStateUnknown
:
1159 device_unregister(&dev
->dev
);
1162 case XenbusStateReconfiguring
:
1163 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1164 xenbus_switch_state(dev
, XenbusStateReconfigured
);
1169 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
1176 Release the translation entry specfied
1178 static void scsiback_release_translation_entry(struct vscsibk_info
*info
)
1180 struct v2p_entry
*entry
, *tmp
;
1181 struct list_head
*head
= &(info
->v2p_entry_lists
);
1182 unsigned long flags
;
1184 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1186 list_for_each_entry_safe(entry
, tmp
, head
, l
)
1187 __scsiback_del_translation_entry(entry
);
1189 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1192 static int scsiback_remove(struct xenbus_device
*dev
)
1194 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1197 scsiback_disconnect(info
);
1199 scsiback_release_translation_entry(info
);
1201 dev_set_drvdata(&dev
->dev
, NULL
);
1206 static int scsiback_probe(struct xenbus_device
*dev
,
1207 const struct xenbus_device_id
*id
)
1211 struct vscsibk_info
*info
= kzalloc(sizeof(struct vscsibk_info
),
1214 pr_debug("%s %p %d\n", __func__
, dev
, dev
->otherend_id
);
1217 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating backend structure");
1221 dev_set_drvdata(&dev
->dev
, info
);
1223 info
->domid
= dev
->otherend_id
;
1224 spin_lock_init(&info
->ring_lock
);
1225 info
->ring_error
= 0;
1226 atomic_set(&info
->nr_unreplied_reqs
, 0);
1227 init_waitqueue_head(&info
->waiting_to_free
);
1230 INIT_LIST_HEAD(&info
->v2p_entry_lists
);
1231 spin_lock_init(&info
->v2p_lock
);
1233 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, "feature-sg-grant", "%u",
1236 xenbus_dev_error(dev
, err
, "writing feature-sg-grant");
1238 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
1245 pr_warn("%s failed\n", __func__
);
1246 scsiback_remove(dev
);
1251 static char *scsiback_dump_proto_id(struct scsiback_tport
*tport
)
1253 switch (tport
->tport_proto_id
) {
1254 case SCSI_PROTOCOL_SAS
:
1256 case SCSI_PROTOCOL_FCP
:
1258 case SCSI_PROTOCOL_ISCSI
:
1267 static char *scsiback_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1269 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1270 struct scsiback_tpg
, se_tpg
);
1271 struct scsiback_tport
*tport
= tpg
->tport
;
1273 return &tport
->tport_name
[0];
1276 static u16
scsiback_get_tag(struct se_portal_group
*se_tpg
)
1278 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1279 struct scsiback_tpg
, se_tpg
);
1280 return tpg
->tport_tpgt
;
1283 static struct se_wwn
*
1284 scsiback_make_tport(struct target_fabric_configfs
*tf
,
1285 struct config_group
*group
,
1288 struct scsiback_tport
*tport
;
1293 tport
= kzalloc(sizeof(struct scsiback_tport
), GFP_KERNEL
);
1295 return ERR_PTR(-ENOMEM
);
1297 tport
->tport_wwpn
= wwpn
;
1299 * Determine the emulated Protocol Identifier and Target Port Name
1300 * based on the incoming configfs directory name.
1302 ptr
= strstr(name
, "naa.");
1304 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1307 ptr
= strstr(name
, "fc.");
1309 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1310 off
= 3; /* Skip over "fc." */
1313 ptr
= strstr(name
, "iqn.");
1315 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1319 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name
);
1321 return ERR_PTR(-EINVAL
);
1324 if (strlen(name
) >= VSCSI_NAMELEN
) {
1325 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name
,
1326 scsiback_dump_proto_id(tport
), VSCSI_NAMELEN
);
1328 return ERR_PTR(-EINVAL
);
1330 snprintf(&tport
->tport_name
[0], VSCSI_NAMELEN
, "%s", &name
[off
]);
1332 pr_debug("Allocated emulated Target %s Address: %s\n",
1333 scsiback_dump_proto_id(tport
), name
);
1335 return &tport
->tport_wwn
;
1338 static void scsiback_drop_tport(struct se_wwn
*wwn
)
1340 struct scsiback_tport
*tport
= container_of(wwn
,
1341 struct scsiback_tport
, tport_wwn
);
1343 pr_debug("Deallocating emulated Target %s Address: %s\n",
1344 scsiback_dump_proto_id(tport
), tport
->tport_name
);
1349 static u32
scsiback_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1354 static int scsiback_check_stop_free(struct se_cmd
*se_cmd
)
1357 * Do not release struct se_cmd's containing a valid TMR pointer.
1358 * These will be released directly in scsiback_device_action()
1359 * with transport_generic_free_cmd().
1361 if (se_cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)
1364 transport_generic_free_cmd(se_cmd
, 0);
1368 static void scsiback_release_cmd(struct se_cmd
*se_cmd
)
1370 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1371 struct vscsibk_pend
, se_cmd
);
1373 kmem_cache_free(scsiback_cachep
, pending_req
);
1376 static int scsiback_shutdown_session(struct se_session
*se_sess
)
1381 static void scsiback_close_session(struct se_session
*se_sess
)
1385 static u32
scsiback_sess_get_index(struct se_session
*se_sess
)
1390 static int scsiback_write_pending(struct se_cmd
*se_cmd
)
1392 /* Go ahead and process the write immediately */
1393 target_execute_cmd(se_cmd
);
1398 static int scsiback_write_pending_status(struct se_cmd
*se_cmd
)
1403 static void scsiback_set_default_node_attrs(struct se_node_acl
*nacl
)
1407 static int scsiback_get_cmd_state(struct se_cmd
*se_cmd
)
1412 static int scsiback_queue_data_in(struct se_cmd
*se_cmd
)
1414 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1415 struct vscsibk_pend
, se_cmd
);
1417 pending_req
->result
= SAM_STAT_GOOD
;
1418 scsiback_cmd_done(pending_req
);
1422 static int scsiback_queue_status(struct se_cmd
*se_cmd
)
1424 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1425 struct vscsibk_pend
, se_cmd
);
1427 if (se_cmd
->sense_buffer
&&
1428 ((se_cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
1429 (se_cmd
->se_cmd_flags
& SCF_EMULATED_TASK_SENSE
)))
1430 pending_req
->result
= (DRIVER_SENSE
<< 24) |
1431 SAM_STAT_CHECK_CONDITION
;
1433 pending_req
->result
= se_cmd
->scsi_status
;
1435 scsiback_cmd_done(pending_req
);
1439 static void scsiback_queue_tm_rsp(struct se_cmd
*se_cmd
)
1441 struct se_tmr_req
*se_tmr
= se_cmd
->se_tmr_req
;
1442 struct scsiback_tmr
*tmr
= se_tmr
->fabric_tmr_ptr
;
1444 atomic_set(&tmr
->tmr_complete
, 1);
1445 wake_up(&tmr
->tmr_wait
);
1448 static void scsiback_aborted_task(struct se_cmd
*se_cmd
)
1452 static ssize_t
scsiback_tpg_param_alias_show(struct config_item
*item
,
1455 struct se_portal_group
*se_tpg
= param_to_tpg(item
);
1456 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1460 mutex_lock(&tpg
->tv_tpg_mutex
);
1461 rb
= snprintf(page
, PAGE_SIZE
, "%s\n", tpg
->param_alias
);
1462 mutex_unlock(&tpg
->tv_tpg_mutex
);
1467 static ssize_t
scsiback_tpg_param_alias_store(struct config_item
*item
,
1468 const char *page
, size_t count
)
1470 struct se_portal_group
*se_tpg
= param_to_tpg(item
);
1471 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1475 if (strlen(page
) >= VSCSI_NAMELEN
) {
1476 pr_err("param alias: %s, exceeds max: %d\n", page
,
1481 mutex_lock(&tpg
->tv_tpg_mutex
);
1482 len
= snprintf(tpg
->param_alias
, VSCSI_NAMELEN
, "%s", page
);
1483 if (tpg
->param_alias
[len
- 1] == '\n')
1484 tpg
->param_alias
[len
- 1] = '\0';
1485 mutex_unlock(&tpg
->tv_tpg_mutex
);
1490 CONFIGFS_ATTR(scsiback_tpg_param_
, alias
);
1492 static struct configfs_attribute
*scsiback_param_attrs
[] = {
1493 &scsiback_tpg_param_attr_alias
,
1497 static int scsiback_make_nexus(struct scsiback_tpg
*tpg
,
1500 struct se_portal_group
*se_tpg
;
1501 struct se_session
*se_sess
;
1502 struct scsiback_nexus
*tv_nexus
;
1504 mutex_lock(&tpg
->tv_tpg_mutex
);
1505 if (tpg
->tpg_nexus
) {
1506 mutex_unlock(&tpg
->tv_tpg_mutex
);
1507 pr_debug("tpg->tpg_nexus already exists\n");
1510 se_tpg
= &tpg
->se_tpg
;
1512 tv_nexus
= kzalloc(sizeof(struct scsiback_nexus
), GFP_KERNEL
);
1514 mutex_unlock(&tpg
->tv_tpg_mutex
);
1518 * Initialize the struct se_session pointer
1520 tv_nexus
->tvn_se_sess
= transport_init_session(TARGET_PROT_NORMAL
);
1521 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1522 mutex_unlock(&tpg
->tv_tpg_mutex
);
1526 se_sess
= tv_nexus
->tvn_se_sess
;
1528 * Since we are running in 'demo mode' this call with generate a
1529 * struct se_node_acl for the scsiback struct se_portal_group with
1530 * the SCSI Initiator port name of the passed configfs group 'name'.
1532 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1533 se_tpg
, (unsigned char *)name
);
1534 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1535 mutex_unlock(&tpg
->tv_tpg_mutex
);
1536 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1540 /* Now register the TCM pvscsi virtual I_T Nexus as active. */
1541 transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1542 tv_nexus
->tvn_se_sess
, tv_nexus
);
1543 tpg
->tpg_nexus
= tv_nexus
;
1545 mutex_unlock(&tpg
->tv_tpg_mutex
);
1549 transport_free_session(se_sess
);
1554 static int scsiback_drop_nexus(struct scsiback_tpg
*tpg
)
1556 struct se_session
*se_sess
;
1557 struct scsiback_nexus
*tv_nexus
;
1559 mutex_lock(&tpg
->tv_tpg_mutex
);
1560 tv_nexus
= tpg
->tpg_nexus
;
1562 mutex_unlock(&tpg
->tv_tpg_mutex
);
1566 se_sess
= tv_nexus
->tvn_se_sess
;
1568 mutex_unlock(&tpg
->tv_tpg_mutex
);
1572 if (tpg
->tv_tpg_port_count
!= 0) {
1573 mutex_unlock(&tpg
->tv_tpg_mutex
);
1574 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1575 tpg
->tv_tpg_port_count
);
1579 if (tpg
->tv_tpg_fe_count
!= 0) {
1580 mutex_unlock(&tpg
->tv_tpg_mutex
);
1581 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1582 tpg
->tv_tpg_fe_count
);
1586 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1587 scsiback_dump_proto_id(tpg
->tport
),
1588 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1591 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1593 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1594 tpg
->tpg_nexus
= NULL
;
1595 mutex_unlock(&tpg
->tv_tpg_mutex
);
1601 static ssize_t
scsiback_tpg_nexus_show(struct config_item
*item
, char *page
)
1603 struct se_portal_group
*se_tpg
= to_tpg(item
);
1604 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1605 struct scsiback_tpg
, se_tpg
);
1606 struct scsiback_nexus
*tv_nexus
;
1609 mutex_lock(&tpg
->tv_tpg_mutex
);
1610 tv_nexus
= tpg
->tpg_nexus
;
1612 mutex_unlock(&tpg
->tv_tpg_mutex
);
1615 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1616 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1617 mutex_unlock(&tpg
->tv_tpg_mutex
);
1622 static ssize_t
scsiback_tpg_nexus_store(struct config_item
*item
,
1623 const char *page
, size_t count
)
1625 struct se_portal_group
*se_tpg
= to_tpg(item
);
1626 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1627 struct scsiback_tpg
, se_tpg
);
1628 struct scsiback_tport
*tport_wwn
= tpg
->tport
;
1629 unsigned char i_port
[VSCSI_NAMELEN
], *ptr
, *port_ptr
;
1632 * Shutdown the active I_T nexus if 'NULL' is passed.
1634 if (!strncmp(page
, "NULL", 4)) {
1635 ret
= scsiback_drop_nexus(tpg
);
1636 return (!ret
) ? count
: ret
;
1639 * Otherwise make sure the passed virtual Initiator port WWN matches
1640 * the fabric protocol_id set in scsiback_make_tport(), and call
1641 * scsiback_make_nexus().
1643 if (strlen(page
) >= VSCSI_NAMELEN
) {
1644 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1645 page
, VSCSI_NAMELEN
);
1648 snprintf(&i_port
[0], VSCSI_NAMELEN
, "%s", page
);
1650 ptr
= strstr(i_port
, "naa.");
1652 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1653 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1654 i_port
, scsiback_dump_proto_id(tport_wwn
));
1657 port_ptr
= &i_port
[0];
1660 ptr
= strstr(i_port
, "fc.");
1662 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1663 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1664 i_port
, scsiback_dump_proto_id(tport_wwn
));
1667 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1670 ptr
= strstr(i_port
, "iqn.");
1672 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1673 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1674 i_port
, scsiback_dump_proto_id(tport_wwn
));
1677 port_ptr
= &i_port
[0];
1680 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1684 * Clear any trailing newline for the NAA WWN
1687 if (i_port
[strlen(i_port
) - 1] == '\n')
1688 i_port
[strlen(i_port
) - 1] = '\0';
1690 ret
= scsiback_make_nexus(tpg
, port_ptr
);
1697 CONFIGFS_ATTR(scsiback_tpg_
, nexus
);
1699 static struct configfs_attribute
*scsiback_tpg_attrs
[] = {
1700 &scsiback_tpg_attr_nexus
,
1705 scsiback_wwn_version_show(struct config_item
*item
, char *page
)
1707 return sprintf(page
, "xen-pvscsi fabric module %s on %s/%s on "
1709 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1712 CONFIGFS_ATTR_RO(scsiback_wwn_
, version
);
1714 static struct configfs_attribute
*scsiback_wwn_attrs
[] = {
1715 &scsiback_wwn_attr_version
,
1719 static char *scsiback_get_fabric_name(void)
1721 return "xen-pvscsi";
1724 static int scsiback_port_link(struct se_portal_group
*se_tpg
,
1727 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1728 struct scsiback_tpg
, se_tpg
);
1730 mutex_lock(&tpg
->tv_tpg_mutex
);
1731 tpg
->tv_tpg_port_count
++;
1732 mutex_unlock(&tpg
->tv_tpg_mutex
);
1737 static void scsiback_port_unlink(struct se_portal_group
*se_tpg
,
1740 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1741 struct scsiback_tpg
, se_tpg
);
1743 mutex_lock(&tpg
->tv_tpg_mutex
);
1744 tpg
->tv_tpg_port_count
--;
1745 mutex_unlock(&tpg
->tv_tpg_mutex
);
1748 static struct se_portal_group
*
1749 scsiback_make_tpg(struct se_wwn
*wwn
,
1750 struct config_group
*group
,
1753 struct scsiback_tport
*tport
= container_of(wwn
,
1754 struct scsiback_tport
, tport_wwn
);
1756 struct scsiback_tpg
*tpg
;
1760 if (strstr(name
, "tpgt_") != name
)
1761 return ERR_PTR(-EINVAL
);
1762 ret
= kstrtou16(name
+ 5, 10, &tpgt
);
1764 return ERR_PTR(ret
);
1766 tpg
= kzalloc(sizeof(struct scsiback_tpg
), GFP_KERNEL
);
1768 return ERR_PTR(-ENOMEM
);
1770 mutex_init(&tpg
->tv_tpg_mutex
);
1771 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1772 INIT_LIST_HEAD(&tpg
->info_list
);
1774 tpg
->tport_tpgt
= tpgt
;
1776 ret
= core_tpg_register(wwn
, &tpg
->se_tpg
, tport
->tport_proto_id
);
1781 mutex_lock(&scsiback_mutex
);
1782 list_add_tail(&tpg
->tv_tpg_list
, &scsiback_list
);
1783 mutex_unlock(&scsiback_mutex
);
1785 return &tpg
->se_tpg
;
1788 static void scsiback_drop_tpg(struct se_portal_group
*se_tpg
)
1790 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1791 struct scsiback_tpg
, se_tpg
);
1793 mutex_lock(&scsiback_mutex
);
1794 list_del(&tpg
->tv_tpg_list
);
1795 mutex_unlock(&scsiback_mutex
);
1797 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1799 scsiback_drop_nexus(tpg
);
1801 * Deregister the se_tpg from TCM.
1803 core_tpg_deregister(se_tpg
);
1807 static int scsiback_check_true(struct se_portal_group
*se_tpg
)
1812 static int scsiback_check_false(struct se_portal_group
*se_tpg
)
1817 static const struct target_core_fabric_ops scsiback_ops
= {
1818 .module
= THIS_MODULE
,
1819 .name
= "xen-pvscsi",
1820 .get_fabric_name
= scsiback_get_fabric_name
,
1821 .tpg_get_wwn
= scsiback_get_fabric_wwn
,
1822 .tpg_get_tag
= scsiback_get_tag
,
1823 .tpg_check_demo_mode
= scsiback_check_true
,
1824 .tpg_check_demo_mode_cache
= scsiback_check_true
,
1825 .tpg_check_demo_mode_write_protect
= scsiback_check_false
,
1826 .tpg_check_prod_mode_write_protect
= scsiback_check_false
,
1827 .tpg_get_inst_index
= scsiback_tpg_get_inst_index
,
1828 .check_stop_free
= scsiback_check_stop_free
,
1829 .release_cmd
= scsiback_release_cmd
,
1830 .shutdown_session
= scsiback_shutdown_session
,
1831 .close_session
= scsiback_close_session
,
1832 .sess_get_index
= scsiback_sess_get_index
,
1833 .sess_get_initiator_sid
= NULL
,
1834 .write_pending
= scsiback_write_pending
,
1835 .write_pending_status
= scsiback_write_pending_status
,
1836 .set_default_node_attributes
= scsiback_set_default_node_attrs
,
1837 .get_cmd_state
= scsiback_get_cmd_state
,
1838 .queue_data_in
= scsiback_queue_data_in
,
1839 .queue_status
= scsiback_queue_status
,
1840 .queue_tm_rsp
= scsiback_queue_tm_rsp
,
1841 .aborted_task
= scsiback_aborted_task
,
1843 * Setup callers for generic logic in target_core_fabric_configfs.c
1845 .fabric_make_wwn
= scsiback_make_tport
,
1846 .fabric_drop_wwn
= scsiback_drop_tport
,
1847 .fabric_make_tpg
= scsiback_make_tpg
,
1848 .fabric_drop_tpg
= scsiback_drop_tpg
,
1849 .fabric_post_link
= scsiback_port_link
,
1850 .fabric_pre_unlink
= scsiback_port_unlink
,
1852 .tfc_wwn_attrs
= scsiback_wwn_attrs
,
1853 .tfc_tpg_base_attrs
= scsiback_tpg_attrs
,
1854 .tfc_tpg_param_attrs
= scsiback_param_attrs
,
1857 static const struct xenbus_device_id scsiback_ids
[] = {
1862 static struct xenbus_driver scsiback_driver
= {
1863 .ids
= scsiback_ids
,
1864 .probe
= scsiback_probe
,
1865 .remove
= scsiback_remove
,
1866 .otherend_changed
= scsiback_frontend_changed
1869 static void scsiback_init_pend(void *p
)
1871 struct vscsibk_pend
*pend
= p
;
1874 memset(pend
, 0, sizeof(*pend
));
1875 for (i
= 0; i
< VSCSI_MAX_GRANTS
; i
++)
1876 pend
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
1879 static int __init
scsiback_init(void)
1886 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE
"\n",
1887 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1889 scsiback_cachep
= kmem_cache_create("vscsiif_cache",
1890 sizeof(struct vscsibk_pend
), 0, 0, scsiback_init_pend
);
1891 if (!scsiback_cachep
)
1894 ret
= xenbus_register_backend(&scsiback_driver
);
1896 goto out_cache_destroy
;
1898 ret
= target_register_template(&scsiback_ops
);
1900 goto out_unregister_xenbus
;
1904 out_unregister_xenbus
:
1905 xenbus_unregister_driver(&scsiback_driver
);
1907 kmem_cache_destroy(scsiback_cachep
);
1908 pr_err("%s: error %d\n", __func__
, ret
);
1912 static void __exit
scsiback_exit(void)
1916 while (free_pages_num
) {
1917 if (get_free_page(&page
))
1919 gnttab_free_pages(1, &page
);
1921 target_unregister_template(&scsiback_ops
);
1922 xenbus_unregister_driver(&scsiback_driver
);
1923 kmem_cache_destroy(scsiback_cachep
);
1926 module_init(scsiback_init
);
1927 module_exit(scsiback_exit
);
1929 MODULE_DESCRIPTION("Xen SCSI backend driver");
1930 MODULE_LICENSE("Dual BSD/GPL");
1931 MODULE_ALIAS("xen-backend:vscsi");
1932 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");