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
36 #include <linux/module.h>
37 #include <linux/utsname.h>
38 #include <linux/interrupt.h>
39 #include <linux/slab.h>
40 #include <linux/wait.h>
41 #include <linux/sched.h>
42 #include <linux/list.h>
43 #include <linux/gfp.h>
44 #include <linux/delay.h>
45 #include <linux/spinlock.h>
46 #include <linux/configfs.h>
48 #include <generated/utsrelease.h>
50 #include <scsi/scsi.h>
51 #include <scsi/scsi_dbg.h>
52 #include <scsi/scsi_eh.h>
53 #include <scsi/scsi_tcq.h>
55 #include <target/target_core_base.h>
56 #include <target/target_core_fabric.h>
57 #include <target/target_core_configfs.h>
58 #include <target/target_core_fabric_configfs.h>
60 #include <asm/hypervisor.h>
63 #include <xen/balloon.h>
64 #include <xen/events.h>
65 #include <xen/xenbus.h>
66 #include <xen/grant_table.h>
69 #include <xen/interface/grant_table.h>
70 #include <xen/interface/io/vscsiif.h>
72 #define DPRINTK(_f, _a...) \
73 pr_debug("(file=%s, line=%d) " _f, __FILE__ , __LINE__ , ## _a)
75 #define VSCSI_VERSION "v0.1"
76 #define VSCSI_NAMELEN 32
79 unsigned int hst
; /* host */
80 unsigned int chn
; /* channel */
81 unsigned int tgt
; /* target */
82 unsigned int lun
; /* LUN */
86 struct ids_tuple v
; /* translate from */
87 struct scsiback_tpg
*tpg
; /* translate to */
94 struct xenbus_device
*dev
;
99 struct vscsiif_back_ring ring
;
102 spinlock_t ring_lock
;
103 atomic_t nr_unreplied_reqs
;
106 struct list_head v2p_entry_lists
;
108 wait_queue_head_t waiting_to_free
;
111 /* theoretical maximum of grants for one request */
112 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
115 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
116 * call to map/unmap grants. Don't choose it too large, as there are arrays
117 * with VSCSI_GRANT_BATCH elements allocated on the stack.
119 #define VSCSI_GRANT_BATCH 16
121 struct vscsibk_pend
{
124 uint8_t cmnd
[VSCSIIF_MAX_COMMAND_SIZE
];
127 uint8_t sc_data_direction
;
128 uint16_t n_sg
; /* real length of SG list */
129 uint16_t n_grants
; /* SG pages and potentially SG list */
133 struct vscsibk_info
*info
;
134 struct v2p_entry
*v2p
;
135 struct scatterlist
*sgl
;
137 uint8_t sense_buffer
[VSCSIIF_SENSE_BUFFERSIZE
];
139 grant_handle_t grant_handles
[VSCSI_MAX_GRANTS
];
140 struct page
*pages
[VSCSI_MAX_GRANTS
];
142 struct se_cmd se_cmd
;
145 struct scsiback_tmr
{
146 atomic_t tmr_complete
;
147 wait_queue_head_t tmr_wait
;
150 struct scsiback_nexus
{
151 /* Pointer to TCM session for I_T Nexus */
152 struct se_session
*tvn_se_sess
;
155 struct scsiback_tport
{
156 /* SCSI protocol the tport is providing */
158 /* Binary World Wide unique Port Name for pvscsi Target port */
160 /* ASCII formatted WWPN for pvscsi Target port */
161 char tport_name
[VSCSI_NAMELEN
];
162 /* Returned by scsiback_make_tport() */
163 struct se_wwn tport_wwn
;
166 struct scsiback_tpg
{
167 /* scsiback port target portal group tag for TCM */
169 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
170 int tv_tpg_port_count
;
171 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
173 /* list for scsiback_list */
174 struct list_head tv_tpg_list
;
175 /* Used to protect access for tpg_nexus */
176 struct mutex tv_tpg_mutex
;
177 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
178 struct scsiback_nexus
*tpg_nexus
;
179 /* Pointer back to scsiback_tport */
180 struct scsiback_tport
*tport
;
181 /* Returned by scsiback_make_tpg() */
182 struct se_portal_group se_tpg
;
183 /* alias used in xenstore */
184 char param_alias
[VSCSI_NAMELEN
];
185 /* list of info structures related to this target portal group */
186 struct list_head info_list
;
189 #define SCSIBACK_INVALID_HANDLE (~0)
191 static bool log_print_stat
;
192 module_param(log_print_stat
, bool, 0644);
194 static int scsiback_max_buffer_pages
= 1024;
195 module_param_named(max_buffer_pages
, scsiback_max_buffer_pages
, int, 0644);
196 MODULE_PARM_DESC(max_buffer_pages
,
197 "Maximum number of free pages to keep in backend buffer");
199 static struct kmem_cache
*scsiback_cachep
;
200 static DEFINE_SPINLOCK(free_pages_lock
);
201 static int free_pages_num
;
202 static LIST_HEAD(scsiback_free_pages
);
204 /* Global spinlock to protect scsiback TPG list */
205 static DEFINE_MUTEX(scsiback_mutex
);
206 static LIST_HEAD(scsiback_list
);
208 /* Local pointer to allocated TCM configfs fabric module */
209 static struct target_fabric_configfs
*scsiback_fabric_configfs
;
211 static void scsiback_get(struct vscsibk_info
*info
)
213 atomic_inc(&info
->nr_unreplied_reqs
);
216 static void scsiback_put(struct vscsibk_info
*info
)
218 if (atomic_dec_and_test(&info
->nr_unreplied_reqs
))
219 wake_up(&info
->waiting_to_free
);
222 static void put_free_pages(struct page
**page
, int num
)
225 int i
= free_pages_num
+ num
, n
= num
;
229 if (i
> scsiback_max_buffer_pages
) {
230 n
= min(num
, i
- scsiback_max_buffer_pages
);
231 gnttab_free_pages(n
, page
+ num
- n
);
234 spin_lock_irqsave(&free_pages_lock
, flags
);
235 for (i
= 0; i
< n
; i
++)
236 list_add(&page
[i
]->lru
, &scsiback_free_pages
);
238 spin_unlock_irqrestore(&free_pages_lock
, flags
);
241 static int get_free_page(struct page
**page
)
245 spin_lock_irqsave(&free_pages_lock
, flags
);
246 if (list_empty(&scsiback_free_pages
)) {
247 spin_unlock_irqrestore(&free_pages_lock
, flags
);
248 return gnttab_alloc_pages(1, page
);
250 page
[0] = list_first_entry(&scsiback_free_pages
, struct page
, lru
);
251 list_del(&page
[0]->lru
);
253 spin_unlock_irqrestore(&free_pages_lock
, flags
);
257 static unsigned long vaddr_page(struct page
*page
)
259 unsigned long pfn
= page_to_pfn(page
);
261 return (unsigned long)pfn_to_kaddr(pfn
);
264 static unsigned long vaddr(struct vscsibk_pend
*req
, int seg
)
266 return vaddr_page(req
->pages
[seg
]);
269 static void scsiback_print_status(char *sense_buffer
, int errors
,
270 struct vscsibk_pend
*pending_req
)
272 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
274 pr_err("xen-pvscsi[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
275 tpg
->tport
->tport_name
, pending_req
->v2p
->lun
,
276 pending_req
->cmnd
[0], status_byte(errors
), msg_byte(errors
),
277 host_byte(errors
), driver_byte(errors
));
280 static void scsiback_fast_flush_area(struct vscsibk_pend
*req
)
282 struct gnttab_unmap_grant_ref unmap
[VSCSI_GRANT_BATCH
];
283 struct page
*pages
[VSCSI_GRANT_BATCH
];
284 unsigned int i
, invcount
= 0;
285 grant_handle_t handle
;
295 for (i
= 0; i
< req
->n_grants
; i
++) {
296 handle
= req
->grant_handles
[i
];
297 if (handle
== SCSIBACK_INVALID_HANDLE
)
299 gnttab_set_unmap_op(&unmap
[invcount
], vaddr(req
, i
),
300 GNTMAP_host_map
, handle
);
301 req
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
302 pages
[invcount
] = req
->pages
[i
];
303 put_page(pages
[invcount
]);
305 if (invcount
< VSCSI_GRANT_BATCH
)
307 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
313 err
= gnttab_unmap_refs(unmap
, NULL
, pages
, invcount
);
317 put_free_pages(req
->pages
, req
->n_grants
);
321 static void scsiback_free_translation_entry(struct kref
*kref
)
323 struct v2p_entry
*entry
= container_of(kref
, struct v2p_entry
, kref
);
324 struct scsiback_tpg
*tpg
= entry
->tpg
;
326 mutex_lock(&tpg
->tv_tpg_mutex
);
327 tpg
->tv_tpg_fe_count
--;
328 mutex_unlock(&tpg
->tv_tpg_mutex
);
333 static void scsiback_do_resp_with_sense(char *sense_buffer
, int32_t result
,
334 uint32_t resid
, struct vscsibk_pend
*pending_req
)
336 struct vscsiif_response
*ring_res
;
337 struct vscsibk_info
*info
= pending_req
->info
;
339 struct scsi_sense_hdr sshdr
;
343 spin_lock_irqsave(&info
->ring_lock
, flags
);
345 ring_res
= RING_GET_RESPONSE(&info
->ring
, info
->ring
.rsp_prod_pvt
);
346 info
->ring
.rsp_prod_pvt
++;
348 ring_res
->rslt
= result
;
349 ring_res
->rqid
= pending_req
->rqid
;
351 if (sense_buffer
!= NULL
&&
352 scsi_normalize_sense(sense_buffer
, VSCSIIF_SENSE_BUFFERSIZE
,
354 len
= min_t(unsigned, 8 + sense_buffer
[7],
355 VSCSIIF_SENSE_BUFFERSIZE
);
356 memcpy(ring_res
->sense_buffer
, sense_buffer
, len
);
357 ring_res
->sense_len
= len
;
359 ring_res
->sense_len
= 0;
362 ring_res
->residual_len
= resid
;
364 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info
->ring
, notify
);
365 spin_unlock_irqrestore(&info
->ring_lock
, flags
);
368 notify_remote_via_irq(info
->irq
);
370 if (pending_req
->v2p
)
371 kref_put(&pending_req
->v2p
->kref
,
372 scsiback_free_translation_entry
);
375 static void scsiback_cmd_done(struct vscsibk_pend
*pending_req
)
377 struct vscsibk_info
*info
= pending_req
->info
;
378 unsigned char *sense_buffer
;
382 sense_buffer
= pending_req
->sense_buffer
;
383 resid
= pending_req
->se_cmd
.residual_count
;
384 errors
= pending_req
->result
;
386 if (errors
&& log_print_stat
)
387 scsiback_print_status(sense_buffer
, errors
, pending_req
);
389 scsiback_fast_flush_area(pending_req
);
390 scsiback_do_resp_with_sense(sense_buffer
, errors
, resid
, pending_req
);
394 static void scsiback_cmd_exec(struct vscsibk_pend
*pending_req
)
396 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
397 struct se_session
*sess
= pending_req
->v2p
->tpg
->tpg_nexus
->tvn_se_sess
;
400 memset(pending_req
->sense_buffer
, 0, VSCSIIF_SENSE_BUFFERSIZE
);
402 memset(se_cmd
, 0, sizeof(*se_cmd
));
404 scsiback_get(pending_req
->info
);
405 rc
= target_submit_cmd_map_sgls(se_cmd
, sess
, pending_req
->cmnd
,
406 pending_req
->sense_buffer
, pending_req
->v2p
->lun
,
407 pending_req
->data_len
, 0,
408 pending_req
->sc_data_direction
, 0,
409 pending_req
->sgl
, pending_req
->n_sg
,
412 transport_send_check_condition_and_sense(se_cmd
,
413 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
414 transport_generic_free_cmd(se_cmd
, 0);
418 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref
*map
,
419 struct page
**pg
, grant_handle_t
*grant
, int cnt
)
426 err
= gnttab_map_refs(map
, NULL
, pg
, cnt
);
428 for (i
= 0; i
< cnt
; i
++) {
429 if (unlikely(map
[i
].status
!= GNTST_okay
)) {
430 pr_err("xen-pvscsi: invalid buffer -- could not remap it\n");
431 map
[i
].handle
= SCSIBACK_INVALID_HANDLE
;
436 grant
[i
] = map
[i
].handle
;
441 static int scsiback_gnttab_data_map_list(struct vscsibk_pend
*pending_req
,
442 struct scsiif_request_segment
*seg
, struct page
**pg
,
443 grant_handle_t
*grant
, int cnt
, u32 flags
)
445 int mapcount
= 0, i
, err
= 0;
446 struct gnttab_map_grant_ref map
[VSCSI_GRANT_BATCH
];
447 struct vscsibk_info
*info
= pending_req
->info
;
449 for (i
= 0; i
< cnt
; i
++) {
450 if (get_free_page(pg
+ mapcount
)) {
451 put_free_pages(pg
, mapcount
);
452 pr_err("xen-pvscsi: no grant page\n");
455 gnttab_set_map_op(&map
[mapcount
], vaddr_page(pg
[mapcount
]),
456 flags
, seg
[i
].gref
, info
->domid
);
458 if (mapcount
< VSCSI_GRANT_BATCH
)
460 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
463 pending_req
->n_grants
+= mapcount
;
468 err
= scsiback_gnttab_data_map_batch(map
, pg
, grant
, mapcount
);
469 pending_req
->n_grants
+= mapcount
;
473 static int scsiback_gnttab_data_map(struct vscsiif_request
*ring_req
,
474 struct vscsibk_pend
*pending_req
)
477 int i
, err
, n_segs
, i_seg
= 0;
479 struct scsiif_request_segment
*seg
;
480 unsigned long end_seg
= 0;
481 unsigned int nr_segments
= (unsigned int)ring_req
->nr_segments
;
482 unsigned int nr_sgl
= 0;
483 struct scatterlist
*sg
;
484 grant_handle_t
*grant
;
486 pending_req
->n_sg
= 0;
487 pending_req
->n_grants
= 0;
488 pending_req
->data_len
= 0;
490 nr_segments
&= ~VSCSIIF_SG_GRANT
;
494 if (nr_segments
> VSCSIIF_SG_TABLESIZE
) {
495 DPRINTK("xen-pvscsi: invalid parameter nr_seg = %d\n",
496 ring_req
->nr_segments
);
500 if (ring_req
->nr_segments
& VSCSIIF_SG_GRANT
) {
501 err
= scsiback_gnttab_data_map_list(pending_req
, ring_req
->seg
,
502 pending_req
->pages
, pending_req
->grant_handles
,
503 nr_segments
, GNTMAP_host_map
| GNTMAP_readonly
);
506 nr_sgl
= nr_segments
;
508 for (i
= 0; i
< nr_sgl
; i
++) {
509 n_segs
= ring_req
->seg
[i
].length
/
510 sizeof(struct scsiif_request_segment
);
511 if ((unsigned)ring_req
->seg
[i
].offset
+
512 (unsigned)ring_req
->seg
[i
].length
> PAGE_SIZE
||
513 n_segs
* sizeof(struct scsiif_request_segment
) !=
514 ring_req
->seg
[i
].length
)
516 nr_segments
+= n_segs
;
518 if (nr_segments
> SG_ALL
) {
519 DPRINTK("xen-pvscsi: invalid nr_seg = %d\n",
525 /* free of (sgl) in fast_flush_area()*/
526 pending_req
->sgl
= kmalloc_array(nr_segments
,
527 sizeof(struct scatterlist
), GFP_KERNEL
);
528 if (!pending_req
->sgl
)
531 sg_init_table(pending_req
->sgl
, nr_segments
);
532 pending_req
->n_sg
= nr_segments
;
534 flags
= GNTMAP_host_map
;
535 if (pending_req
->sc_data_direction
== DMA_TO_DEVICE
)
536 flags
|= GNTMAP_readonly
;
538 pg
= pending_req
->pages
+ nr_sgl
;
539 grant
= pending_req
->grant_handles
+ nr_sgl
;
542 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
543 pg
, grant
, nr_segments
, flags
);
547 for (i
= 0; i
< nr_sgl
; i
++) {
548 seg
= (struct scsiif_request_segment
*)(
549 vaddr(pending_req
, i
) + ring_req
->seg
[i
].offset
);
550 n_segs
= ring_req
->seg
[i
].length
/
551 sizeof(struct scsiif_request_segment
);
552 err
= scsiback_gnttab_data_map_list(pending_req
, seg
,
553 pg
, grant
, n_segs
, flags
);
559 end_seg
= vaddr(pending_req
, 0) + ring_req
->seg
[0].offset
;
560 seg
= (struct scsiif_request_segment
*)end_seg
;
561 end_seg
+= ring_req
->seg
[0].length
;
562 pg
= pending_req
->pages
+ nr_sgl
;
565 for_each_sg(pending_req
->sgl
, sg
, nr_segments
, i
) {
566 sg_set_page(sg
, pg
[i
], seg
->length
, seg
->offset
);
567 pending_req
->data_len
+= seg
->length
;
569 if (nr_sgl
&& (unsigned long)seg
>= end_seg
) {
571 end_seg
= vaddr(pending_req
, i_seg
) +
572 ring_req
->seg
[i_seg
].offset
;
573 seg
= (struct scsiif_request_segment
*)end_seg
;
574 end_seg
+= ring_req
->seg
[i_seg
].length
;
576 if (sg
->offset
>= PAGE_SIZE
||
577 sg
->length
> PAGE_SIZE
||
578 sg
->offset
+ sg
->length
> PAGE_SIZE
)
585 static void scsiback_disconnect(struct vscsibk_info
*info
)
587 wait_event(info
->waiting_to_free
,
588 atomic_read(&info
->nr_unreplied_reqs
) == 0);
590 unbind_from_irqhandler(info
->irq
, info
);
592 xenbus_unmap_ring_vfree(info
->dev
, info
->ring
.sring
);
595 static void scsiback_device_action(struct vscsibk_pend
*pending_req
,
596 enum tcm_tmreq_table act
, int tag
)
598 int rc
, err
= FAILED
;
599 struct scsiback_tpg
*tpg
= pending_req
->v2p
->tpg
;
600 struct se_cmd
*se_cmd
= &pending_req
->se_cmd
;
601 struct scsiback_tmr
*tmr
;
603 tmr
= kzalloc(sizeof(struct scsiback_tmr
), GFP_KERNEL
);
607 init_waitqueue_head(&tmr
->tmr_wait
);
609 transport_init_se_cmd(se_cmd
, tpg
->se_tpg
.se_tpg_tfo
,
610 tpg
->tpg_nexus
->tvn_se_sess
, 0, DMA_NONE
, TCM_SIMPLE_TAG
,
611 &pending_req
->sense_buffer
[0]);
613 rc
= core_tmr_alloc_req(se_cmd
, tmr
, act
, GFP_KERNEL
);
617 se_cmd
->se_tmr_req
->ref_task_tag
= tag
;
619 if (transport_lookup_tmr_lun(se_cmd
, pending_req
->v2p
->lun
) < 0)
622 transport_generic_handle_tmr(se_cmd
);
623 wait_event(tmr
->tmr_wait
, atomic_read(&tmr
->tmr_complete
));
625 err
= (se_cmd
->se_tmr_req
->response
== TMR_FUNCTION_COMPLETE
) ?
630 transport_generic_free_cmd(&pending_req
->se_cmd
, 1);
634 scsiback_do_resp_with_sense(NULL
, err
, 0, pending_req
);
636 kmem_cache_free(scsiback_cachep
, pending_req
);
640 Perform virtual to physical translation
642 static struct v2p_entry
*scsiback_do_translation(struct vscsibk_info
*info
,
645 struct v2p_entry
*entry
;
646 struct list_head
*head
= &(info
->v2p_entry_lists
);
649 spin_lock_irqsave(&info
->v2p_lock
, flags
);
650 list_for_each_entry(entry
, head
, l
) {
651 if ((entry
->v
.chn
== v
->chn
) &&
652 (entry
->v
.tgt
== v
->tgt
) &&
653 (entry
->v
.lun
== v
->lun
)) {
654 kref_get(&entry
->kref
);
661 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
665 static int prepare_pending_reqs(struct vscsibk_info
*info
,
666 struct vscsiif_request
*ring_req
,
667 struct vscsibk_pend
*pending_req
)
669 struct v2p_entry
*v2p
;
670 struct ids_tuple vir
;
672 pending_req
->rqid
= ring_req
->rqid
;
673 pending_req
->info
= info
;
675 vir
.chn
= ring_req
->channel
;
676 vir
.tgt
= ring_req
->id
;
677 vir
.lun
= ring_req
->lun
;
679 v2p
= scsiback_do_translation(info
, &vir
);
681 pending_req
->v2p
= NULL
;
682 DPRINTK("xen-pvscsi: doesn't exist.\n");
685 pending_req
->v2p
= v2p
;
687 /* request range check from frontend */
688 pending_req
->sc_data_direction
= ring_req
->sc_data_direction
;
689 if ((pending_req
->sc_data_direction
!= DMA_BIDIRECTIONAL
) &&
690 (pending_req
->sc_data_direction
!= DMA_TO_DEVICE
) &&
691 (pending_req
->sc_data_direction
!= DMA_FROM_DEVICE
) &&
692 (pending_req
->sc_data_direction
!= DMA_NONE
)) {
693 DPRINTK("xen-pvscsi: invalid parameter data_dir = %d\n",
694 pending_req
->sc_data_direction
);
698 pending_req
->cmd_len
= ring_req
->cmd_len
;
699 if (pending_req
->cmd_len
> VSCSIIF_MAX_COMMAND_SIZE
) {
700 DPRINTK("xen-pvscsi: invalid parameter cmd_len = %d\n",
701 pending_req
->cmd_len
);
704 memcpy(pending_req
->cmnd
, ring_req
->cmnd
, pending_req
->cmd_len
);
709 static int scsiback_do_cmd_fn(struct vscsibk_info
*info
)
711 struct vscsiif_back_ring
*ring
= &info
->ring
;
712 struct vscsiif_request ring_req
;
713 struct vscsibk_pend
*pending_req
;
719 rp
= ring
->sring
->req_prod
;
720 rmb(); /* guest system is accessing ring, too */
722 if (RING_REQUEST_PROD_OVERFLOW(ring
, rp
)) {
723 rc
= ring
->rsp_prod_pvt
;
724 pr_warn("xen-pvscsi: Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
725 info
->domid
, rp
, rc
, rp
- rc
);
726 info
->ring_error
= 1;
731 if (RING_REQUEST_CONS_OVERFLOW(ring
, rc
))
733 pending_req
= kmem_cache_alloc(scsiback_cachep
, GFP_KERNEL
);
737 ring_req
= *RING_GET_REQUEST(ring
, rc
);
738 ring
->req_cons
= ++rc
;
740 err
= prepare_pending_reqs(info
, &ring_req
, pending_req
);
744 result
= DID_NO_CONNECT
;
747 result
= DRIVER_ERROR
;
750 scsiback_do_resp_with_sense(NULL
, result
<< 24, 0,
752 kmem_cache_free(scsiback_cachep
, pending_req
);
756 switch (ring_req
.act
) {
757 case VSCSIIF_ACT_SCSI_CDB
:
758 if (scsiback_gnttab_data_map(&ring_req
, pending_req
)) {
759 scsiback_fast_flush_area(pending_req
);
760 scsiback_do_resp_with_sense(NULL
,
761 DRIVER_ERROR
<< 24, 0, pending_req
);
762 kmem_cache_free(scsiback_cachep
, pending_req
);
764 scsiback_cmd_exec(pending_req
);
767 case VSCSIIF_ACT_SCSI_ABORT
:
768 scsiback_device_action(pending_req
, TMR_ABORT_TASK
,
771 case VSCSIIF_ACT_SCSI_RESET
:
772 scsiback_device_action(pending_req
, TMR_LUN_RESET
, 0);
775 pr_err_ratelimited("xen-pvscsi: invalid request\n");
776 scsiback_do_resp_with_sense(NULL
, DRIVER_ERROR
<< 24,
778 kmem_cache_free(scsiback_cachep
, pending_req
);
782 /* Yield point for this unbounded loop. */
786 RING_FINAL_CHECK_FOR_REQUESTS(&info
->ring
, more_to_do
);
790 static irqreturn_t
scsiback_irq_fn(int irq
, void *dev_id
)
792 struct vscsibk_info
*info
= dev_id
;
794 if (info
->ring_error
)
797 while (scsiback_do_cmd_fn(info
))
803 static int scsiback_init_sring(struct vscsibk_info
*info
, grant_ref_t ring_ref
,
804 evtchn_port_t evtchn
)
807 struct vscsiif_sring
*sring
;
813 err
= xenbus_map_ring_valloc(info
->dev
, ring_ref
, &area
);
817 sring
= (struct vscsiif_sring
*)area
;
818 BACK_RING_INIT(&info
->ring
, sring
, PAGE_SIZE
);
820 err
= bind_interdomain_evtchn_to_irq(info
->domid
, evtchn
);
826 err
= request_threaded_irq(info
->irq
, NULL
, scsiback_irq_fn
,
827 IRQF_ONESHOT
, "vscsiif-backend", info
);
834 unbind_from_irqhandler(info
->irq
, info
);
837 xenbus_unmap_ring_vfree(info
->dev
, area
);
842 static int scsiback_map(struct vscsibk_info
*info
)
844 struct xenbus_device
*dev
= info
->dev
;
845 unsigned int ring_ref
, evtchn
;
848 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
849 "ring-ref", "%u", &ring_ref
,
850 "event-channel", "%u", &evtchn
, NULL
);
852 xenbus_dev_fatal(dev
, err
, "reading %s ring", dev
->otherend
);
856 return scsiback_init_sring(info
, ring_ref
, evtchn
);
860 Add a new translation entry
862 static int scsiback_add_translation_entry(struct vscsibk_info
*info
,
863 char *phy
, struct ids_tuple
*v
)
866 struct v2p_entry
*entry
;
867 struct v2p_entry
*new;
868 struct list_head
*head
= &(info
->v2p_entry_lists
);
872 struct scsiback_tpg
*tpg_entry
, *tpg
= NULL
;
873 char *error
= "doesn't exist";
875 lunp
= strrchr(phy
, ':');
877 pr_err("xen-pvscsi: illegal format of physical device %s\n",
883 if (kstrtouint(lunp
, 10, &lun
) || lun
>= TRANSPORT_MAX_LUNS_PER_TPG
) {
884 pr_err("xen-pvscsi: lun number not valid: %s\n", lunp
);
888 mutex_lock(&scsiback_mutex
);
889 list_for_each_entry(tpg_entry
, &scsiback_list
, tv_tpg_list
) {
890 if (!strcmp(phy
, tpg_entry
->tport
->tport_name
) ||
891 !strcmp(phy
, tpg_entry
->param_alias
)) {
892 spin_lock(&tpg_entry
->se_tpg
.tpg_lun_lock
);
893 if (tpg_entry
->se_tpg
.tpg_lun_list
[lun
]->lun_status
==
894 TRANSPORT_LUN_STATUS_ACTIVE
) {
895 if (!tpg_entry
->tpg_nexus
)
896 error
= "nexus undefined";
900 spin_unlock(&tpg_entry
->se_tpg
.tpg_lun_lock
);
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("xen-pvscsi: %s:%d %s\n", phy
, 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("xen-pvscsi: 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
);
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
)
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("xen-pvscsi: 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("xen-pvscsi: 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 if (device_state
== XenbusStateInitialising
)
1064 scsiback_do_add_lun(info
, state
, phy
, &vir
);
1065 if (device_state
== XenbusStateClosing
)
1066 scsiback_do_del_lun(info
, state
, &vir
);
1069 case VSCSIBACK_OP_UPDATEDEV_STATE
:
1070 if (device_state
== XenbusStateInitialised
) {
1071 /* modify vscsi-devs/dev-x/state */
1072 if (xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1073 "%d", XenbusStateConnected
)) {
1074 pr_err("xen-pvscsi: xenbus_printf error %s\n",
1076 scsiback_del_translation_entry(info
, &vir
);
1077 xenbus_printf(XBT_NIL
, dev
->nodename
, state
,
1078 "%d", XenbusStateClosed
);
1082 /*When it is necessary, processing is added here.*/
1088 static void scsiback_do_lun_hotplug(struct vscsibk_info
*info
, int op
)
1092 unsigned int ndir
= 0;
1094 dir
= xenbus_directory(XBT_NIL
, info
->dev
->nodename
, "vscsi-devs",
1099 for (i
= 0; i
< ndir
; i
++)
1100 scsiback_do_1lun_hotplug(info
, op
, dir
[i
]);
1105 static void scsiback_frontend_changed(struct xenbus_device
*dev
,
1106 enum xenbus_state frontend_state
)
1108 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1110 switch (frontend_state
) {
1111 case XenbusStateInitialising
:
1114 case XenbusStateInitialised
:
1115 if (scsiback_map(info
))
1118 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1119 xenbus_switch_state(dev
, XenbusStateConnected
);
1122 case XenbusStateConnected
:
1123 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_UPDATEDEV_STATE
);
1125 if (dev
->state
== XenbusStateConnected
)
1128 xenbus_switch_state(dev
, XenbusStateConnected
);
1131 case XenbusStateClosing
:
1133 scsiback_disconnect(info
);
1135 xenbus_switch_state(dev
, XenbusStateClosing
);
1138 case XenbusStateClosed
:
1139 xenbus_switch_state(dev
, XenbusStateClosed
);
1140 if (xenbus_dev_is_online(dev
))
1142 /* fall through if not online */
1143 case XenbusStateUnknown
:
1144 device_unregister(&dev
->dev
);
1147 case XenbusStateReconfiguring
:
1148 scsiback_do_lun_hotplug(info
, VSCSIBACK_OP_ADD_OR_DEL_LUN
);
1149 xenbus_switch_state(dev
, XenbusStateReconfigured
);
1154 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
1161 Release the translation entry specfied
1163 static void scsiback_release_translation_entry(struct vscsibk_info
*info
)
1165 struct v2p_entry
*entry
, *tmp
;
1166 struct list_head
*head
= &(info
->v2p_entry_lists
);
1167 unsigned long flags
;
1169 spin_lock_irqsave(&info
->v2p_lock
, flags
);
1171 list_for_each_entry_safe(entry
, tmp
, head
, l
)
1172 __scsiback_del_translation_entry(entry
);
1174 spin_unlock_irqrestore(&info
->v2p_lock
, flags
);
1177 static int scsiback_remove(struct xenbus_device
*dev
)
1179 struct vscsibk_info
*info
= dev_get_drvdata(&dev
->dev
);
1182 scsiback_disconnect(info
);
1184 scsiback_release_translation_entry(info
);
1186 dev_set_drvdata(&dev
->dev
, NULL
);
1191 static int scsiback_probe(struct xenbus_device
*dev
,
1192 const struct xenbus_device_id
*id
)
1196 struct vscsibk_info
*info
= kzalloc(sizeof(struct vscsibk_info
),
1199 DPRINTK("%p %d\n", dev
, dev
->otherend_id
);
1202 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating backend structure");
1206 dev_set_drvdata(&dev
->dev
, info
);
1208 info
->domid
= dev
->otherend_id
;
1209 spin_lock_init(&info
->ring_lock
);
1210 info
->ring_error
= 0;
1211 atomic_set(&info
->nr_unreplied_reqs
, 0);
1212 init_waitqueue_head(&info
->waiting_to_free
);
1215 INIT_LIST_HEAD(&info
->v2p_entry_lists
);
1216 spin_lock_init(&info
->v2p_lock
);
1218 err
= xenbus_printf(XBT_NIL
, dev
->nodename
, "feature-sg-grant", "%u",
1221 xenbus_dev_error(dev
, err
, "writing feature-sg-grant");
1223 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
1230 pr_warn("xen-pvscsi: %s failed\n", __func__
);
1231 scsiback_remove(dev
);
1236 static char *scsiback_dump_proto_id(struct scsiback_tport
*tport
)
1238 switch (tport
->tport_proto_id
) {
1239 case SCSI_PROTOCOL_SAS
:
1241 case SCSI_PROTOCOL_FCP
:
1243 case SCSI_PROTOCOL_ISCSI
:
1252 static u8
scsiback_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
1254 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1255 struct scsiback_tpg
, se_tpg
);
1256 struct scsiback_tport
*tport
= tpg
->tport
;
1258 switch (tport
->tport_proto_id
) {
1259 case SCSI_PROTOCOL_SAS
:
1260 return sas_get_fabric_proto_ident(se_tpg
);
1261 case SCSI_PROTOCOL_FCP
:
1262 return fc_get_fabric_proto_ident(se_tpg
);
1263 case SCSI_PROTOCOL_ISCSI
:
1264 return iscsi_get_fabric_proto_ident(se_tpg
);
1266 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1267 tport
->tport_proto_id
);
1271 return sas_get_fabric_proto_ident(se_tpg
);
1274 static char *scsiback_get_fabric_wwn(struct se_portal_group
*se_tpg
)
1276 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1277 struct scsiback_tpg
, se_tpg
);
1278 struct scsiback_tport
*tport
= tpg
->tport
;
1280 return &tport
->tport_name
[0];
1283 static u16
scsiback_get_tag(struct se_portal_group
*se_tpg
)
1285 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1286 struct scsiback_tpg
, se_tpg
);
1287 return tpg
->tport_tpgt
;
1290 static u32
scsiback_get_default_depth(struct se_portal_group
*se_tpg
)
1296 scsiback_get_pr_transport_id(struct se_portal_group
*se_tpg
,
1297 struct se_node_acl
*se_nacl
,
1298 struct t10_pr_registration
*pr_reg
,
1302 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1303 struct scsiback_tpg
, se_tpg
);
1304 struct scsiback_tport
*tport
= tpg
->tport
;
1306 switch (tport
->tport_proto_id
) {
1307 case SCSI_PROTOCOL_SAS
:
1308 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1310 case SCSI_PROTOCOL_FCP
:
1311 return fc_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1313 case SCSI_PROTOCOL_ISCSI
:
1314 return iscsi_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1317 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1318 tport
->tport_proto_id
);
1322 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
1327 scsiback_get_pr_transport_id_len(struct se_portal_group
*se_tpg
,
1328 struct se_node_acl
*se_nacl
,
1329 struct t10_pr_registration
*pr_reg
,
1332 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1333 struct scsiback_tpg
, se_tpg
);
1334 struct scsiback_tport
*tport
= tpg
->tport
;
1336 switch (tport
->tport_proto_id
) {
1337 case SCSI_PROTOCOL_SAS
:
1338 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1340 case SCSI_PROTOCOL_FCP
:
1341 return fc_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1343 case SCSI_PROTOCOL_ISCSI
:
1344 return iscsi_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1347 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1348 tport
->tport_proto_id
);
1352 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
1357 scsiback_parse_pr_out_transport_id(struct se_portal_group
*se_tpg
,
1360 char **port_nexus_ptr
)
1362 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1363 struct scsiback_tpg
, se_tpg
);
1364 struct scsiback_tport
*tport
= tpg
->tport
;
1366 switch (tport
->tport_proto_id
) {
1367 case SCSI_PROTOCOL_SAS
:
1368 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1370 case SCSI_PROTOCOL_FCP
:
1371 return fc_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1373 case SCSI_PROTOCOL_ISCSI
:
1374 return iscsi_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1377 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1378 tport
->tport_proto_id
);
1382 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
1386 static struct se_wwn
*
1387 scsiback_make_tport(struct target_fabric_configfs
*tf
,
1388 struct config_group
*group
,
1391 struct scsiback_tport
*tport
;
1396 tport
= kzalloc(sizeof(struct scsiback_tport
), GFP_KERNEL
);
1398 return ERR_PTR(-ENOMEM
);
1400 tport
->tport_wwpn
= wwpn
;
1402 * Determine the emulated Protocol Identifier and Target Port Name
1403 * based on the incoming configfs directory name.
1405 ptr
= strstr(name
, "naa.");
1407 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1410 ptr
= strstr(name
, "fc.");
1412 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1413 off
= 3; /* Skip over "fc." */
1416 ptr
= strstr(name
, "iqn.");
1418 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1422 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name
);
1424 return ERR_PTR(-EINVAL
);
1427 if (strlen(name
) >= VSCSI_NAMELEN
) {
1428 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name
,
1429 scsiback_dump_proto_id(tport
), VSCSI_NAMELEN
);
1431 return ERR_PTR(-EINVAL
);
1433 snprintf(&tport
->tport_name
[0], VSCSI_NAMELEN
, "%s", &name
[off
]);
1435 pr_debug("xen-pvscsi: Allocated emulated Target %s Address: %s\n",
1436 scsiback_dump_proto_id(tport
), name
);
1438 return &tport
->tport_wwn
;
1441 static void scsiback_drop_tport(struct se_wwn
*wwn
)
1443 struct scsiback_tport
*tport
= container_of(wwn
,
1444 struct scsiback_tport
, tport_wwn
);
1446 pr_debug("xen-pvscsi: Deallocating emulated Target %s Address: %s\n",
1447 scsiback_dump_proto_id(tport
), tport
->tport_name
);
1452 static struct se_node_acl
*
1453 scsiback_alloc_fabric_acl(struct se_portal_group
*se_tpg
)
1455 return kzalloc(sizeof(struct se_node_acl
), GFP_KERNEL
);
1459 scsiback_release_fabric_acl(struct se_portal_group
*se_tpg
,
1460 struct se_node_acl
*se_nacl
)
1465 static u32
scsiback_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
1470 static int scsiback_check_stop_free(struct se_cmd
*se_cmd
)
1473 * Do not release struct se_cmd's containing a valid TMR
1474 * pointer. These will be released directly in scsiback_device_action()
1475 * with transport_generic_free_cmd().
1477 if (se_cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)
1480 transport_generic_free_cmd(se_cmd
, 0);
1484 static void scsiback_release_cmd(struct se_cmd
*se_cmd
)
1486 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1487 struct vscsibk_pend
, se_cmd
);
1489 kmem_cache_free(scsiback_cachep
, pending_req
);
1492 static int scsiback_shutdown_session(struct se_session
*se_sess
)
1497 static void scsiback_close_session(struct se_session
*se_sess
)
1501 static u32
scsiback_sess_get_index(struct se_session
*se_sess
)
1506 static int scsiback_write_pending(struct se_cmd
*se_cmd
)
1508 /* Go ahead and process the write immediately */
1509 target_execute_cmd(se_cmd
);
1514 static int scsiback_write_pending_status(struct se_cmd
*se_cmd
)
1519 static void scsiback_set_default_node_attrs(struct se_node_acl
*nacl
)
1523 static u32
scsiback_get_task_tag(struct se_cmd
*se_cmd
)
1525 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1526 struct vscsibk_pend
, se_cmd
);
1528 return pending_req
->rqid
;
1531 static int scsiback_get_cmd_state(struct se_cmd
*se_cmd
)
1536 static int scsiback_queue_data_in(struct se_cmd
*se_cmd
)
1538 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1539 struct vscsibk_pend
, se_cmd
);
1541 pending_req
->result
= SAM_STAT_GOOD
;
1542 scsiback_cmd_done(pending_req
);
1546 static int scsiback_queue_status(struct se_cmd
*se_cmd
)
1548 struct vscsibk_pend
*pending_req
= container_of(se_cmd
,
1549 struct vscsibk_pend
, se_cmd
);
1551 if (se_cmd
->sense_buffer
&&
1552 ((se_cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) ||
1553 (se_cmd
->se_cmd_flags
& SCF_EMULATED_TASK_SENSE
)))
1554 pending_req
->result
= (DRIVER_SENSE
<< 24) |
1555 SAM_STAT_CHECK_CONDITION
;
1557 pending_req
->result
= se_cmd
->scsi_status
;
1559 scsiback_cmd_done(pending_req
);
1563 static void scsiback_queue_tm_rsp(struct se_cmd
*se_cmd
)
1565 struct se_tmr_req
*se_tmr
= se_cmd
->se_tmr_req
;
1566 struct scsiback_tmr
*tmr
= se_tmr
->fabric_tmr_ptr
;
1568 atomic_set(&tmr
->tmr_complete
, 1);
1569 wake_up(&tmr
->tmr_wait
);
1572 static void scsiback_aborted_task(struct se_cmd
*se_cmd
)
1576 static ssize_t
scsiback_tpg_param_show_alias(struct se_portal_group
*se_tpg
,
1579 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1583 mutex_lock(&tpg
->tv_tpg_mutex
);
1584 rb
= snprintf(page
, PAGE_SIZE
, "%s\n", tpg
->param_alias
);
1585 mutex_unlock(&tpg
->tv_tpg_mutex
);
1590 static ssize_t
scsiback_tpg_param_store_alias(struct se_portal_group
*se_tpg
,
1591 const char *page
, size_t count
)
1593 struct scsiback_tpg
*tpg
= container_of(se_tpg
, struct scsiback_tpg
,
1597 if (strlen(page
) >= VSCSI_NAMELEN
) {
1598 pr_err("param alias: %s, exceeds max: %d\n", page
,
1603 mutex_lock(&tpg
->tv_tpg_mutex
);
1604 len
= snprintf(tpg
->param_alias
, VSCSI_NAMELEN
, "%s", page
);
1605 if (tpg
->param_alias
[len
- 1] == '\n')
1606 tpg
->param_alias
[len
- 1] = '\0';
1607 mutex_unlock(&tpg
->tv_tpg_mutex
);
1612 TF_TPG_PARAM_ATTR(scsiback
, alias
, S_IRUGO
| S_IWUSR
);
1614 static struct configfs_attribute
*scsiback_param_attrs
[] = {
1615 &scsiback_tpg_param_alias
.attr
,
1619 static int scsiback_make_nexus(struct scsiback_tpg
*tpg
,
1622 struct se_portal_group
*se_tpg
;
1623 struct se_session
*se_sess
;
1624 struct scsiback_nexus
*tv_nexus
;
1626 mutex_lock(&tpg
->tv_tpg_mutex
);
1627 if (tpg
->tpg_nexus
) {
1628 mutex_unlock(&tpg
->tv_tpg_mutex
);
1629 pr_debug("tpg->tpg_nexus already exists\n");
1632 se_tpg
= &tpg
->se_tpg
;
1634 tv_nexus
= kzalloc(sizeof(struct scsiback_nexus
), GFP_KERNEL
);
1636 mutex_unlock(&tpg
->tv_tpg_mutex
);
1640 * Initialize the struct se_session pointer
1642 tv_nexus
->tvn_se_sess
= transport_init_session(TARGET_PROT_NORMAL
);
1643 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1644 mutex_unlock(&tpg
->tv_tpg_mutex
);
1648 se_sess
= tv_nexus
->tvn_se_sess
;
1650 * Since we are running in 'demo mode' this call with generate a
1651 * struct se_node_acl for the scsiback struct se_portal_group with
1652 * the SCSI Initiator port name of the passed configfs group 'name'.
1654 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1655 se_tpg
, (unsigned char *)name
);
1656 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1657 mutex_unlock(&tpg
->tv_tpg_mutex
);
1658 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1663 * Now register the TCM pvscsi virtual I_T Nexus as active with the
1664 * call to __transport_register_session()
1666 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1667 tv_nexus
->tvn_se_sess
, tv_nexus
);
1668 tpg
->tpg_nexus
= tv_nexus
;
1670 mutex_unlock(&tpg
->tv_tpg_mutex
);
1674 transport_free_session(se_sess
);
1679 static int scsiback_drop_nexus(struct scsiback_tpg
*tpg
)
1681 struct se_session
*se_sess
;
1682 struct scsiback_nexus
*tv_nexus
;
1684 mutex_lock(&tpg
->tv_tpg_mutex
);
1685 tv_nexus
= tpg
->tpg_nexus
;
1687 mutex_unlock(&tpg
->tv_tpg_mutex
);
1691 se_sess
= tv_nexus
->tvn_se_sess
;
1693 mutex_unlock(&tpg
->tv_tpg_mutex
);
1697 if (tpg
->tv_tpg_port_count
!= 0) {
1698 mutex_unlock(&tpg
->tv_tpg_mutex
);
1699 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1700 tpg
->tv_tpg_port_count
);
1704 if (tpg
->tv_tpg_fe_count
!= 0) {
1705 mutex_unlock(&tpg
->tv_tpg_mutex
);
1706 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1707 tpg
->tv_tpg_fe_count
);
1711 pr_debug("xen-pvscsi: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1712 scsiback_dump_proto_id(tpg
->tport
),
1713 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1716 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1718 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1719 tpg
->tpg_nexus
= NULL
;
1720 mutex_unlock(&tpg
->tv_tpg_mutex
);
1726 static ssize_t
scsiback_tpg_show_nexus(struct se_portal_group
*se_tpg
,
1729 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1730 struct scsiback_tpg
, se_tpg
);
1731 struct scsiback_nexus
*tv_nexus
;
1734 mutex_lock(&tpg
->tv_tpg_mutex
);
1735 tv_nexus
= tpg
->tpg_nexus
;
1737 mutex_unlock(&tpg
->tv_tpg_mutex
);
1740 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1741 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1742 mutex_unlock(&tpg
->tv_tpg_mutex
);
1747 static ssize_t
scsiback_tpg_store_nexus(struct se_portal_group
*se_tpg
,
1751 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1752 struct scsiback_tpg
, se_tpg
);
1753 struct scsiback_tport
*tport_wwn
= tpg
->tport
;
1754 unsigned char i_port
[VSCSI_NAMELEN
], *ptr
, *port_ptr
;
1757 * Shutdown the active I_T nexus if 'NULL' is passed..
1759 if (!strncmp(page
, "NULL", 4)) {
1760 ret
= scsiback_drop_nexus(tpg
);
1761 return (!ret
) ? count
: ret
;
1764 * Otherwise make sure the passed virtual Initiator port WWN matches
1765 * the fabric protocol_id set in scsiback_make_tport(), and call
1766 * scsiback_make_nexus().
1768 if (strlen(page
) >= VSCSI_NAMELEN
) {
1769 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1770 page
, VSCSI_NAMELEN
);
1773 snprintf(&i_port
[0], VSCSI_NAMELEN
, "%s", page
);
1775 ptr
= strstr(i_port
, "naa.");
1777 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1778 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1779 i_port
, scsiback_dump_proto_id(tport_wwn
));
1782 port_ptr
= &i_port
[0];
1785 ptr
= strstr(i_port
, "fc.");
1787 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1788 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1789 i_port
, scsiback_dump_proto_id(tport_wwn
));
1792 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1795 ptr
= strstr(i_port
, "iqn.");
1797 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1798 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1799 i_port
, scsiback_dump_proto_id(tport_wwn
));
1802 port_ptr
= &i_port
[0];
1805 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1809 * Clear any trailing newline for the NAA WWN
1812 if (i_port
[strlen(i_port
) - 1] == '\n')
1813 i_port
[strlen(i_port
) - 1] = '\0';
1815 ret
= scsiback_make_nexus(tpg
, port_ptr
);
1822 TF_TPG_BASE_ATTR(scsiback
, nexus
, S_IRUGO
| S_IWUSR
);
1824 static struct configfs_attribute
*scsiback_tpg_attrs
[] = {
1825 &scsiback_tpg_nexus
.attr
,
1830 scsiback_wwn_show_attr_version(struct target_fabric_configfs
*tf
,
1833 return sprintf(page
, "xen-pvscsi fabric module %s on %s/%s on "
1835 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
1838 TF_WWN_ATTR_RO(scsiback
, version
);
1840 static struct configfs_attribute
*scsiback_wwn_attrs
[] = {
1841 &scsiback_wwn_version
.attr
,
1845 static char *scsiback_get_fabric_name(void)
1847 return "xen-pvscsi";
1850 static int scsiback_port_link(struct se_portal_group
*se_tpg
,
1853 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1854 struct scsiback_tpg
, se_tpg
);
1856 mutex_lock(&tpg
->tv_tpg_mutex
);
1857 tpg
->tv_tpg_port_count
++;
1858 mutex_unlock(&tpg
->tv_tpg_mutex
);
1863 static void scsiback_port_unlink(struct se_portal_group
*se_tpg
,
1866 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1867 struct scsiback_tpg
, se_tpg
);
1869 mutex_lock(&tpg
->tv_tpg_mutex
);
1870 tpg
->tv_tpg_port_count
--;
1871 mutex_unlock(&tpg
->tv_tpg_mutex
);
1874 static struct se_portal_group
*
1875 scsiback_make_tpg(struct se_wwn
*wwn
,
1876 struct config_group
*group
,
1879 struct scsiback_tport
*tport
= container_of(wwn
,
1880 struct scsiback_tport
, tport_wwn
);
1882 struct scsiback_tpg
*tpg
;
1886 if (strstr(name
, "tpgt_") != name
)
1887 return ERR_PTR(-EINVAL
);
1888 ret
= kstrtou16(name
+ 5, 10, &tpgt
);
1890 return ERR_PTR(ret
);
1892 tpg
= kzalloc(sizeof(struct scsiback_tpg
), GFP_KERNEL
);
1894 return ERR_PTR(-ENOMEM
);
1896 mutex_init(&tpg
->tv_tpg_mutex
);
1897 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1898 INIT_LIST_HEAD(&tpg
->info_list
);
1900 tpg
->tport_tpgt
= tpgt
;
1902 ret
= core_tpg_register(&scsiback_fabric_configfs
->tf_ops
, wwn
,
1903 &tpg
->se_tpg
, tpg
, TRANSPORT_TPG_TYPE_NORMAL
);
1908 mutex_lock(&scsiback_mutex
);
1909 list_add_tail(&tpg
->tv_tpg_list
, &scsiback_list
);
1910 mutex_unlock(&scsiback_mutex
);
1912 return &tpg
->se_tpg
;
1915 static void scsiback_drop_tpg(struct se_portal_group
*se_tpg
)
1917 struct scsiback_tpg
*tpg
= container_of(se_tpg
,
1918 struct scsiback_tpg
, se_tpg
);
1920 mutex_lock(&scsiback_mutex
);
1921 list_del(&tpg
->tv_tpg_list
);
1922 mutex_unlock(&scsiback_mutex
);
1924 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1926 scsiback_drop_nexus(tpg
);
1928 * Deregister the se_tpg from TCM..
1930 core_tpg_deregister(se_tpg
);
1934 static int scsiback_check_true(struct se_portal_group
*se_tpg
)
1939 static int scsiback_check_false(struct se_portal_group
*se_tpg
)
1944 static struct target_core_fabric_ops scsiback_ops
= {
1945 .get_fabric_name
= scsiback_get_fabric_name
,
1946 .get_fabric_proto_ident
= scsiback_get_fabric_proto_ident
,
1947 .tpg_get_wwn
= scsiback_get_fabric_wwn
,
1948 .tpg_get_tag
= scsiback_get_tag
,
1949 .tpg_get_default_depth
= scsiback_get_default_depth
,
1950 .tpg_get_pr_transport_id
= scsiback_get_pr_transport_id
,
1951 .tpg_get_pr_transport_id_len
= scsiback_get_pr_transport_id_len
,
1952 .tpg_parse_pr_out_transport_id
= scsiback_parse_pr_out_transport_id
,
1953 .tpg_check_demo_mode
= scsiback_check_true
,
1954 .tpg_check_demo_mode_cache
= scsiback_check_true
,
1955 .tpg_check_demo_mode_write_protect
= scsiback_check_false
,
1956 .tpg_check_prod_mode_write_protect
= scsiback_check_false
,
1957 .tpg_alloc_fabric_acl
= scsiback_alloc_fabric_acl
,
1958 .tpg_release_fabric_acl
= scsiback_release_fabric_acl
,
1959 .tpg_get_inst_index
= scsiback_tpg_get_inst_index
,
1960 .check_stop_free
= scsiback_check_stop_free
,
1961 .release_cmd
= scsiback_release_cmd
,
1962 .put_session
= NULL
,
1963 .shutdown_session
= scsiback_shutdown_session
,
1964 .close_session
= scsiback_close_session
,
1965 .sess_get_index
= scsiback_sess_get_index
,
1966 .sess_get_initiator_sid
= NULL
,
1967 .write_pending
= scsiback_write_pending
,
1968 .write_pending_status
= scsiback_write_pending_status
,
1969 .set_default_node_attributes
= scsiback_set_default_node_attrs
,
1970 .get_task_tag
= scsiback_get_task_tag
,
1971 .get_cmd_state
= scsiback_get_cmd_state
,
1972 .queue_data_in
= scsiback_queue_data_in
,
1973 .queue_status
= scsiback_queue_status
,
1974 .queue_tm_rsp
= scsiback_queue_tm_rsp
,
1975 .aborted_task
= scsiback_aborted_task
,
1977 * Setup callers for generic logic in target_core_fabric_configfs.c
1979 .fabric_make_wwn
= scsiback_make_tport
,
1980 .fabric_drop_wwn
= scsiback_drop_tport
,
1981 .fabric_make_tpg
= scsiback_make_tpg
,
1982 .fabric_drop_tpg
= scsiback_drop_tpg
,
1983 .fabric_post_link
= scsiback_port_link
,
1984 .fabric_pre_unlink
= scsiback_port_unlink
,
1985 .fabric_make_np
= NULL
,
1986 .fabric_drop_np
= NULL
,
1988 .fabric_make_nodeacl
= scsiback_make_nodeacl
,
1989 .fabric_drop_nodeacl
= scsiback_drop_nodeacl
,
1993 static int scsiback_register_configfs(void)
1995 struct target_fabric_configfs
*fabric
;
1998 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE
"\n",
1999 VSCSI_VERSION
, utsname()->sysname
, utsname()->machine
);
2001 * Register the top level struct config_item_type with TCM core
2003 fabric
= target_fabric_configfs_init(THIS_MODULE
, "xen-pvscsi");
2005 return PTR_ERR(fabric
);
2008 * Setup fabric->tf_ops from our local scsiback_ops
2010 fabric
->tf_ops
= scsiback_ops
;
2012 * Setup default attribute lists for various fabric->tf_cit_tmpl
2014 fabric
->tf_cit_tmpl
.tfc_wwn_cit
.ct_attrs
= scsiback_wwn_attrs
;
2015 fabric
->tf_cit_tmpl
.tfc_tpg_base_cit
.ct_attrs
= scsiback_tpg_attrs
;
2016 fabric
->tf_cit_tmpl
.tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
2017 fabric
->tf_cit_tmpl
.tfc_tpg_param_cit
.ct_attrs
= scsiback_param_attrs
;
2018 fabric
->tf_cit_tmpl
.tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
2019 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
2020 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
2021 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
2022 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
2024 * Register the fabric for use within TCM
2026 ret
= target_fabric_configfs_register(fabric
);
2028 target_fabric_configfs_free(fabric
);
2032 * Setup our local pointer to *fabric
2034 scsiback_fabric_configfs
= fabric
;
2035 pr_debug("xen-pvscsi: Set fabric -> scsiback_fabric_configfs\n");
2039 static void scsiback_deregister_configfs(void)
2041 if (!scsiback_fabric_configfs
)
2044 target_fabric_configfs_deregister(scsiback_fabric_configfs
);
2045 scsiback_fabric_configfs
= NULL
;
2046 pr_debug("xen-pvscsi: Cleared scsiback_fabric_configfs\n");
2049 static const struct xenbus_device_id scsiback_ids
[] = {
2054 static struct xenbus_driver scsiback_driver
= {
2055 .ids
= scsiback_ids
,
2056 .probe
= scsiback_probe
,
2057 .remove
= scsiback_remove
,
2058 .otherend_changed
= scsiback_frontend_changed
2061 static void scsiback_init_pend(void *p
)
2063 struct vscsibk_pend
*pend
= p
;
2066 memset(pend
, 0, sizeof(*pend
));
2067 for (i
= 0; i
< VSCSI_MAX_GRANTS
; i
++)
2068 pend
->grant_handles
[i
] = SCSIBACK_INVALID_HANDLE
;
2071 static int __init
scsiback_init(void)
2078 scsiback_cachep
= kmem_cache_create("vscsiif_cache",
2079 sizeof(struct vscsibk_pend
), 0, 0, scsiback_init_pend
);
2080 if (!scsiback_cachep
)
2083 ret
= xenbus_register_backend(&scsiback_driver
);
2085 goto out_cache_destroy
;
2087 ret
= scsiback_register_configfs();
2089 goto out_unregister_xenbus
;
2093 out_unregister_xenbus
:
2094 xenbus_unregister_driver(&scsiback_driver
);
2096 kmem_cache_destroy(scsiback_cachep
);
2097 pr_err("xen-pvscsi: %s: error %d\n", __func__
, ret
);
2101 static void __exit
scsiback_exit(void)
2105 while (free_pages_num
) {
2106 if (get_free_page(&page
))
2108 gnttab_free_pages(1, &page
);
2110 scsiback_deregister_configfs();
2111 xenbus_unregister_driver(&scsiback_driver
);
2112 kmem_cache_destroy(scsiback_cachep
);
2115 module_init(scsiback_init
);
2116 module_exit(scsiback_exit
);
2118 MODULE_DESCRIPTION("Xen SCSI backend driver");
2119 MODULE_LICENSE("Dual BSD/GPL");
2120 MODULE_ALIAS("xen-backend:vscsi");
2121 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");