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>
56 #include <target/target_core_configfs.h>
57 #include <target/target_core_fabric_configfs.h>
59 #include <asm/hypervisor.h>
62 #include <xen/balloon.h>
63 #include <xen/events.h>
64 #include <xen/xenbus.h>
65 #include <xen/grant_table.h>
68 #include <xen/interface/grant_table.h>
69 #include <xen/interface/io/vscsiif.h>
71 #define VSCSI_VERSION "v0.1"
72 #define VSCSI_NAMELEN 32
75 unsigned int hst; /* host */
76 unsigned int chn; /* channel */
77 unsigned int tgt; /* target */
78 unsigned int lun; /* LUN */
82 struct ids_tuple v; /* translate from */
83 struct scsiback_tpg *tpg; /* translate to */
90 struct xenbus_device *dev;
95 struct vscsiif_back_ring ring;
99 atomic_t nr_unreplied_reqs;
102 struct list_head v2p_entry_lists;
104 wait_queue_head_t waiting_to_free;
107 /* theoretical maximum of grants for one request */
108 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
111 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
112 * call to map/unmap grants. Don't choose it too large, as there are arrays
113 * with VSCSI_GRANT_BATCH elements allocated on the stack.
115 #define VSCSI_GRANT_BATCH 16
117 struct vscsibk_pend {
120 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
123 uint8_t sc_data_direction;
124 uint16_t n_sg; /* real length of SG list */
125 uint16_t n_grants; /* SG pages and potentially SG list */
129 struct vscsibk_info *info;
130 struct v2p_entry *v2p;
131 struct scatterlist *sgl;
133 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
135 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
136 struct page *pages[VSCSI_MAX_GRANTS];
138 struct se_cmd se_cmd;
141 struct scsiback_tmr {
142 atomic_t tmr_complete;
143 wait_queue_head_t tmr_wait;
146 struct scsiback_nexus {
147 /* Pointer to TCM session for I_T Nexus */
148 struct se_session *tvn_se_sess;
151 struct scsiback_tport {
152 /* SCSI protocol the tport is providing */
154 /* Binary World Wide unique Port Name for pvscsi Target port */
156 /* ASCII formatted WWPN for pvscsi Target port */
157 char tport_name[VSCSI_NAMELEN];
158 /* Returned by scsiback_make_tport() */
159 struct se_wwn tport_wwn;
162 struct scsiback_tpg {
163 /* scsiback port target portal group tag for TCM */
165 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
166 int tv_tpg_port_count;
167 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
169 /* list for scsiback_list */
170 struct list_head tv_tpg_list;
171 /* Used to protect access for tpg_nexus */
172 struct mutex tv_tpg_mutex;
173 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
174 struct scsiback_nexus *tpg_nexus;
175 /* Pointer back to scsiback_tport */
176 struct scsiback_tport *tport;
177 /* Returned by scsiback_make_tpg() */
178 struct se_portal_group se_tpg;
179 /* alias used in xenstore */
180 char param_alias[VSCSI_NAMELEN];
181 /* list of info structures related to this target portal group */
182 struct list_head info_list;
185 #define SCSIBACK_INVALID_HANDLE (~0)
187 static bool log_print_stat;
188 module_param(log_print_stat, bool, 0644);
190 static int scsiback_max_buffer_pages = 1024;
191 module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
192 MODULE_PARM_DESC(max_buffer_pages,
193 "Maximum number of free pages to keep in backend buffer");
195 static struct kmem_cache *scsiback_cachep;
196 static DEFINE_SPINLOCK(free_pages_lock);
197 static int free_pages_num;
198 static LIST_HEAD(scsiback_free_pages);
200 /* Global spinlock to protect scsiback TPG list */
201 static DEFINE_MUTEX(scsiback_mutex);
202 static LIST_HEAD(scsiback_list);
204 static const struct target_core_fabric_ops scsiback_ops;
206 static void scsiback_get(struct vscsibk_info *info)
208 atomic_inc(&info->nr_unreplied_reqs);
211 static void scsiback_put(struct vscsibk_info *info)
213 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
214 wake_up(&info->waiting_to_free);
217 static void put_free_pages(struct page **page, int num)
220 int i = free_pages_num + num, n = num;
224 if (i > scsiback_max_buffer_pages) {
225 n = min(num, i - scsiback_max_buffer_pages);
226 gnttab_free_pages(n, page + num - n);
229 spin_lock_irqsave(&free_pages_lock, flags);
230 for (i = 0; i < n; i++)
231 list_add(&page[i]->lru, &scsiback_free_pages);
233 spin_unlock_irqrestore(&free_pages_lock, flags);
236 static int get_free_page(struct page **page)
240 spin_lock_irqsave(&free_pages_lock, flags);
241 if (list_empty(&scsiback_free_pages)) {
242 spin_unlock_irqrestore(&free_pages_lock, flags);
243 return gnttab_alloc_pages(1, page);
245 page[0] = list_first_entry(&scsiback_free_pages, struct page, lru);
246 list_del(&page[0]->lru);
248 spin_unlock_irqrestore(&free_pages_lock, flags);
252 static unsigned long vaddr_page(struct page *page)
254 unsigned long pfn = page_to_pfn(page);
256 return (unsigned long)pfn_to_kaddr(pfn);
259 static unsigned long vaddr(struct vscsibk_pend *req, int seg)
261 return vaddr_page(req->pages[seg]);
264 static void scsiback_print_status(char *sense_buffer, int errors,
265 struct vscsibk_pend *pending_req)
267 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
269 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
270 tpg->tport->tport_name, pending_req->v2p->lun,
271 pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
272 host_byte(errors), driver_byte(errors));
275 static void scsiback_fast_flush_area(struct vscsibk_pend *req)
277 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
278 struct page *pages[VSCSI_GRANT_BATCH];
279 unsigned int i, invcount = 0;
280 grant_handle_t handle;
290 for (i = 0; i < req->n_grants; i++) {
291 handle = req->grant_handles[i];
292 if (handle == SCSIBACK_INVALID_HANDLE)
294 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
295 GNTMAP_host_map, handle);
296 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
297 pages[invcount] = req->pages[i];
298 put_page(pages[invcount]);
300 if (invcount < VSCSI_GRANT_BATCH)
302 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
308 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
312 put_free_pages(req->pages, req->n_grants);
316 static void scsiback_free_translation_entry(struct kref *kref)
318 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
319 struct scsiback_tpg *tpg = entry->tpg;
321 mutex_lock(&tpg->tv_tpg_mutex);
322 tpg->tv_tpg_fe_count--;
323 mutex_unlock(&tpg->tv_tpg_mutex);
328 static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
329 uint32_t resid, struct vscsibk_pend *pending_req)
331 struct vscsiif_response *ring_res;
332 struct vscsibk_info *info = pending_req->info;
334 struct scsi_sense_hdr sshdr;
338 spin_lock_irqsave(&info->ring_lock, flags);
340 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
341 info->ring.rsp_prod_pvt++;
343 ring_res->rslt = result;
344 ring_res->rqid = pending_req->rqid;
346 if (sense_buffer != NULL &&
347 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
349 len = min_t(unsigned, 8 + sense_buffer[7],
350 VSCSIIF_SENSE_BUFFERSIZE);
351 memcpy(ring_res->sense_buffer, sense_buffer, len);
352 ring_res->sense_len = len;
354 ring_res->sense_len = 0;
357 ring_res->residual_len = resid;
359 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
360 spin_unlock_irqrestore(&info->ring_lock, flags);
363 notify_remote_via_irq(info->irq);
365 if (pending_req->v2p)
366 kref_put(&pending_req->v2p->kref,
367 scsiback_free_translation_entry);
370 static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
372 struct vscsibk_info *info = pending_req->info;
373 unsigned char *sense_buffer;
377 sense_buffer = pending_req->sense_buffer;
378 resid = pending_req->se_cmd.residual_count;
379 errors = pending_req->result;
381 if (errors && log_print_stat)
382 scsiback_print_status(sense_buffer, errors, pending_req);
384 scsiback_fast_flush_area(pending_req);
385 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
389 static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
391 struct se_cmd *se_cmd = &pending_req->se_cmd;
392 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
395 memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
397 memset(se_cmd, 0, sizeof(*se_cmd));
399 scsiback_get(pending_req->info);
400 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
401 pending_req->sense_buffer, pending_req->v2p->lun,
402 pending_req->data_len, 0,
403 pending_req->sc_data_direction, 0,
404 pending_req->sgl, pending_req->n_sg,
407 transport_send_check_condition_and_sense(se_cmd,
408 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
409 transport_generic_free_cmd(se_cmd, 0);
413 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
414 struct page **pg, grant_handle_t *grant, int cnt)
421 err = gnttab_map_refs(map, NULL, pg, cnt);
423 for (i = 0; i < cnt; i++) {
424 if (unlikely(map[i].status != GNTST_okay)) {
425 pr_err("invalid buffer -- could not remap it\n");
426 map[i].handle = SCSIBACK_INVALID_HANDLE;
431 grant[i] = map[i].handle;
436 static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
437 struct scsiif_request_segment *seg, struct page **pg,
438 grant_handle_t *grant, int cnt, u32 flags)
440 int mapcount = 0, i, err = 0;
441 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
442 struct vscsibk_info *info = pending_req->info;
444 for (i = 0; i < cnt; i++) {
445 if (get_free_page(pg + mapcount)) {
446 put_free_pages(pg, mapcount);
447 pr_err("no grant page\n");
450 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
451 flags, seg[i].gref, info->domid);
453 if (mapcount < VSCSI_GRANT_BATCH)
455 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
458 pending_req->n_grants += mapcount;
463 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
464 pending_req->n_grants += mapcount;
468 static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
469 struct vscsibk_pend *pending_req)
472 int i, err, n_segs, i_seg = 0;
474 struct scsiif_request_segment *seg;
475 unsigned long end_seg = 0;
476 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
477 unsigned int nr_sgl = 0;
478 struct scatterlist *sg;
479 grant_handle_t *grant;
481 pending_req->n_sg = 0;
482 pending_req->n_grants = 0;
483 pending_req->data_len = 0;
485 nr_segments &= ~VSCSIIF_SG_GRANT;
489 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
490 pr_debug("invalid parameter nr_seg = %d\n",
491 ring_req->nr_segments);
495 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
496 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
497 pending_req->pages, pending_req->grant_handles,
498 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
501 nr_sgl = nr_segments;
503 for (i = 0; i < nr_sgl; i++) {
504 n_segs = ring_req->seg[i].length /
505 sizeof(struct scsiif_request_segment);
506 if ((unsigned)ring_req->seg[i].offset +
507 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
508 n_segs * sizeof(struct scsiif_request_segment) !=
509 ring_req->seg[i].length)
511 nr_segments += n_segs;
513 if (nr_segments > SG_ALL) {
514 pr_debug("invalid nr_seg = %d\n", nr_segments);
519 /* free of (sgl) in fast_flush_area() */
520 pending_req->sgl = kmalloc_array(nr_segments,
521 sizeof(struct scatterlist), GFP_KERNEL);
522 if (!pending_req->sgl)
525 sg_init_table(pending_req->sgl, nr_segments);
526 pending_req->n_sg = nr_segments;
528 flags = GNTMAP_host_map;
529 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
530 flags |= GNTMAP_readonly;
532 pg = pending_req->pages + nr_sgl;
533 grant = pending_req->grant_handles + nr_sgl;
536 err = scsiback_gnttab_data_map_list(pending_req, seg,
537 pg, grant, nr_segments, flags);
541 for (i = 0; i < nr_sgl; i++) {
542 seg = (struct scsiif_request_segment *)(
543 vaddr(pending_req, i) + ring_req->seg[i].offset);
544 n_segs = ring_req->seg[i].length /
545 sizeof(struct scsiif_request_segment);
546 err = scsiback_gnttab_data_map_list(pending_req, seg,
547 pg, grant, n_segs, flags);
553 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
554 seg = (struct scsiif_request_segment *)end_seg;
555 end_seg += ring_req->seg[0].length;
556 pg = pending_req->pages + nr_sgl;
559 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
560 sg_set_page(sg, pg[i], seg->length, seg->offset);
561 pending_req->data_len += seg->length;
563 if (nr_sgl && (unsigned long)seg >= end_seg) {
565 end_seg = vaddr(pending_req, i_seg) +
566 ring_req->seg[i_seg].offset;
567 seg = (struct scsiif_request_segment *)end_seg;
568 end_seg += ring_req->seg[i_seg].length;
570 if (sg->offset >= PAGE_SIZE ||
571 sg->length > PAGE_SIZE ||
572 sg->offset + sg->length > PAGE_SIZE)
579 static void scsiback_disconnect(struct vscsibk_info *info)
581 wait_event(info->waiting_to_free,
582 atomic_read(&info->nr_unreplied_reqs) == 0);
584 unbind_from_irqhandler(info->irq, info);
586 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
589 static void scsiback_device_action(struct vscsibk_pend *pending_req,
590 enum tcm_tmreq_table act, int tag)
592 int rc, err = FAILED;
593 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
594 struct se_cmd *se_cmd = &pending_req->se_cmd;
595 struct scsiback_tmr *tmr;
597 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
601 init_waitqueue_head(&tmr->tmr_wait);
603 transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
604 tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, TCM_SIMPLE_TAG,
605 &pending_req->sense_buffer[0]);
607 rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
611 se_cmd->se_tmr_req->ref_task_tag = tag;
613 if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
616 transport_generic_handle_tmr(se_cmd);
617 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
619 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
624 transport_generic_free_cmd(&pending_req->se_cmd, 1);
628 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
630 kmem_cache_free(scsiback_cachep, pending_req);
634 Perform virtual to physical translation
636 static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
639 struct v2p_entry *entry;
640 struct list_head *head = &(info->v2p_entry_lists);
643 spin_lock_irqsave(&info->v2p_lock, flags);
644 list_for_each_entry(entry, head, l) {
645 if ((entry->v.chn == v->chn) &&
646 (entry->v.tgt == v->tgt) &&
647 (entry->v.lun == v->lun)) {
648 kref_get(&entry->kref);
655 spin_unlock_irqrestore(&info->v2p_lock, flags);
659 static int prepare_pending_reqs(struct vscsibk_info *info,
660 struct vscsiif_request *ring_req,
661 struct vscsibk_pend *pending_req)
663 struct v2p_entry *v2p;
664 struct ids_tuple vir;
666 pending_req->rqid = ring_req->rqid;
667 pending_req->info = info;
669 vir.chn = ring_req->channel;
670 vir.tgt = ring_req->id;
671 vir.lun = ring_req->lun;
673 v2p = scsiback_do_translation(info, &vir);
675 pending_req->v2p = NULL;
676 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
677 vir.chn, vir.tgt, vir.lun);
680 pending_req->v2p = v2p;
682 /* request range check from frontend */
683 pending_req->sc_data_direction = ring_req->sc_data_direction;
684 if ((pending_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
685 (pending_req->sc_data_direction != DMA_TO_DEVICE) &&
686 (pending_req->sc_data_direction != DMA_FROM_DEVICE) &&
687 (pending_req->sc_data_direction != DMA_NONE)) {
688 pr_debug("invalid parameter data_dir = %d\n",
689 pending_req->sc_data_direction);
693 pending_req->cmd_len = ring_req->cmd_len;
694 if (pending_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
695 pr_debug("invalid parameter cmd_len = %d\n",
696 pending_req->cmd_len);
699 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
704 static int scsiback_do_cmd_fn(struct vscsibk_info *info)
706 struct vscsiif_back_ring *ring = &info->ring;
707 struct vscsiif_request ring_req;
708 struct vscsibk_pend *pending_req;
714 rp = ring->sring->req_prod;
715 rmb(); /* guest system is accessing ring, too */
717 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
718 rc = ring->rsp_prod_pvt;
719 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
720 info->domid, rp, rc, rp - rc);
721 info->ring_error = 1;
726 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
728 pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
732 ring_req = *RING_GET_REQUEST(ring, rc);
733 ring->req_cons = ++rc;
735 err = prepare_pending_reqs(info, &ring_req, pending_req);
739 result = DID_NO_CONNECT;
742 result = DRIVER_ERROR;
745 scsiback_do_resp_with_sense(NULL, result << 24, 0,
747 kmem_cache_free(scsiback_cachep, pending_req);
751 switch (ring_req.act) {
752 case VSCSIIF_ACT_SCSI_CDB:
753 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
754 scsiback_fast_flush_area(pending_req);
755 scsiback_do_resp_with_sense(NULL,
756 DRIVER_ERROR << 24, 0, pending_req);
757 kmem_cache_free(scsiback_cachep, pending_req);
759 scsiback_cmd_exec(pending_req);
762 case VSCSIIF_ACT_SCSI_ABORT:
763 scsiback_device_action(pending_req, TMR_ABORT_TASK,
766 case VSCSIIF_ACT_SCSI_RESET:
767 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
770 pr_err_ratelimited("invalid request\n");
771 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
773 kmem_cache_free(scsiback_cachep, pending_req);
777 /* Yield point for this unbounded loop. */
781 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
785 static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
787 struct vscsibk_info *info = dev_id;
789 if (info->ring_error)
792 while (scsiback_do_cmd_fn(info))
798 static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
799 evtchn_port_t evtchn)
802 struct vscsiif_sring *sring;
808 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
812 sring = (struct vscsiif_sring *)area;
813 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
815 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
821 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
822 IRQF_ONESHOT, "vscsiif-backend", info);
829 unbind_from_irqhandler(info->irq, info);
832 xenbus_unmap_ring_vfree(info->dev, area);
837 static int scsiback_map(struct vscsibk_info *info)
839 struct xenbus_device *dev = info->dev;
840 unsigned int ring_ref, evtchn;
843 err = xenbus_gather(XBT_NIL, dev->otherend,
844 "ring-ref", "%u", &ring_ref,
845 "event-channel", "%u", &evtchn, NULL);
847 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
851 return scsiback_init_sring(info, ring_ref, evtchn);
855 Add a new translation entry
857 static int scsiback_add_translation_entry(struct vscsibk_info *info,
858 char *phy, struct ids_tuple *v)
861 struct v2p_entry *entry;
862 struct v2p_entry *new;
863 struct list_head *head = &(info->v2p_entry_lists);
867 struct scsiback_tpg *tpg_entry, *tpg = NULL;
868 char *error = "doesn't exist";
870 lunp = strrchr(phy, ':');
872 pr_err("illegal format of physical device %s\n", phy);
877 if (kstrtouint(lunp, 10, &lun) || lun >= TRANSPORT_MAX_LUNS_PER_TPG) {
878 pr_err("lun number not valid: %s\n", lunp);
882 mutex_lock(&scsiback_mutex);
883 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
884 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
885 !strcmp(phy, tpg_entry->param_alias)) {
886 spin_lock(&tpg_entry->se_tpg.tpg_lun_lock);
887 if (tpg_entry->se_tpg.tpg_lun_list[lun]->lun_status ==
888 TRANSPORT_LUN_STATUS_ACTIVE) {
889 if (!tpg_entry->tpg_nexus)
890 error = "nexus undefined";
894 spin_unlock(&tpg_entry->se_tpg.tpg_lun_lock);
899 mutex_lock(&tpg->tv_tpg_mutex);
900 tpg->tv_tpg_fe_count++;
901 mutex_unlock(&tpg->tv_tpg_mutex);
903 mutex_unlock(&scsiback_mutex);
906 pr_err("%s:%d %s\n", phy, lun, error);
910 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
916 spin_lock_irqsave(&info->v2p_lock, flags);
918 /* Check double assignment to identical virtual ID */
919 list_for_each_entry(entry, head, l) {
920 if ((entry->v.chn == v->chn) &&
921 (entry->v.tgt == v->tgt) &&
922 (entry->v.lun == v->lun)) {
923 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
930 /* Create a new translation entry and add to the list */
931 kref_init(&new->kref);
935 list_add_tail(&new->l, head);
938 spin_unlock_irqrestore(&info->v2p_lock, flags);
941 mutex_lock(&tpg->tv_tpg_mutex);
942 tpg->tv_tpg_fe_count--;
943 mutex_unlock(&tpg->tv_tpg_mutex);
951 static void __scsiback_del_translation_entry(struct v2p_entry *entry)
954 kref_put(&entry->kref, scsiback_free_translation_entry);
958 Delete the translation entry specfied
960 static int scsiback_del_translation_entry(struct vscsibk_info *info,
963 struct v2p_entry *entry;
964 struct list_head *head = &(info->v2p_entry_lists);
967 spin_lock_irqsave(&info->v2p_lock, flags);
968 /* Find out the translation entry specified */
969 list_for_each_entry(entry, head, l) {
970 if ((entry->v.chn == v->chn) &&
971 (entry->v.tgt == v->tgt) &&
972 (entry->v.lun == v->lun)) {
977 spin_unlock_irqrestore(&info->v2p_lock, flags);
981 /* Delete the translation entry specfied */
982 __scsiback_del_translation_entry(entry);
984 spin_unlock_irqrestore(&info->v2p_lock, flags);
988 static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
989 char *phy, struct ids_tuple *vir, int try)
991 if (!scsiback_add_translation_entry(info, phy, vir)) {
992 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
993 "%d", XenbusStateInitialised)) {
994 pr_err("xenbus_printf error %s\n", state);
995 scsiback_del_translation_entry(info, vir);
998 xenbus_printf(XBT_NIL, info->dev->nodename, state,
999 "%d", XenbusStateClosed);
1003 static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1004 struct ids_tuple *vir)
1006 if (!scsiback_del_translation_entry(info, vir)) {
1007 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1008 "%d", XenbusStateClosed))
1009 pr_err("xenbus_printf error %s\n", state);
1013 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1014 #define VSCSIBACK_OP_UPDATEDEV_STATE 2
1016 static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1020 struct ids_tuple vir;
1023 char phy[VSCSI_NAMELEN];
1026 struct xenbus_device *dev = info->dev;
1029 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1030 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1031 if (XENBUS_EXIST_ERR(err))
1034 /* physical SCSI device */
1035 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1036 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1038 xenbus_printf(XBT_NIL, dev->nodename, state,
1039 "%d", XenbusStateClosed);
1042 strlcpy(phy, val, VSCSI_NAMELEN);
1045 /* virtual SCSI device */
1046 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1047 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1048 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1049 if (XENBUS_EXIST_ERR(err)) {
1050 xenbus_printf(XBT_NIL, dev->nodename, state,
1051 "%d", XenbusStateClosed);
1056 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
1057 switch (device_state) {
1058 case XenbusStateInitialising:
1059 scsiback_do_add_lun(info, state, phy, &vir, 0);
1061 case XenbusStateConnected:
1062 scsiback_do_add_lun(info, state, phy, &vir, 1);
1064 case XenbusStateClosing:
1065 scsiback_do_del_lun(info, state, &vir);
1072 case VSCSIBACK_OP_UPDATEDEV_STATE:
1073 if (device_state == XenbusStateInitialised) {
1074 /* modify vscsi-devs/dev-x/state */
1075 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1076 "%d", XenbusStateConnected)) {
1077 pr_err("xenbus_printf error %s\n", str);
1078 scsiback_del_translation_entry(info, &vir);
1079 xenbus_printf(XBT_NIL, dev->nodename, state,
1080 "%d", XenbusStateClosed);
1084 /* When it is necessary, processing is added here. */
1090 static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1094 unsigned int ndir = 0;
1096 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1101 for (i = 0; i < ndir; i++)
1102 scsiback_do_1lun_hotplug(info, op, dir[i]);
1107 static void scsiback_frontend_changed(struct xenbus_device *dev,
1108 enum xenbus_state frontend_state)
1110 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1112 switch (frontend_state) {
1113 case XenbusStateInitialising:
1116 case XenbusStateInitialised:
1117 if (scsiback_map(info))
1120 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1121 xenbus_switch_state(dev, XenbusStateConnected);
1124 case XenbusStateConnected:
1125 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1127 if (dev->state == XenbusStateConnected)
1130 xenbus_switch_state(dev, XenbusStateConnected);
1133 case XenbusStateClosing:
1135 scsiback_disconnect(info);
1137 xenbus_switch_state(dev, XenbusStateClosing);
1140 case XenbusStateClosed:
1141 xenbus_switch_state(dev, XenbusStateClosed);
1142 if (xenbus_dev_is_online(dev))
1144 /* fall through if not online */
1145 case XenbusStateUnknown:
1146 device_unregister(&dev->dev);
1149 case XenbusStateReconfiguring:
1150 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1151 xenbus_switch_state(dev, XenbusStateReconfigured);
1156 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1163 Release the translation entry specfied
1165 static void scsiback_release_translation_entry(struct vscsibk_info *info)
1167 struct v2p_entry *entry, *tmp;
1168 struct list_head *head = &(info->v2p_entry_lists);
1169 unsigned long flags;
1171 spin_lock_irqsave(&info->v2p_lock, flags);
1173 list_for_each_entry_safe(entry, tmp, head, l)
1174 __scsiback_del_translation_entry(entry);
1176 spin_unlock_irqrestore(&info->v2p_lock, flags);
1179 static int scsiback_remove(struct xenbus_device *dev)
1181 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1184 scsiback_disconnect(info);
1186 scsiback_release_translation_entry(info);
1188 dev_set_drvdata(&dev->dev, NULL);
1193 static int scsiback_probe(struct xenbus_device *dev,
1194 const struct xenbus_device_id *id)
1198 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1201 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
1204 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1208 dev_set_drvdata(&dev->dev, info);
1210 info->domid = dev->otherend_id;
1211 spin_lock_init(&info->ring_lock);
1212 info->ring_error = 0;
1213 atomic_set(&info->nr_unreplied_reqs, 0);
1214 init_waitqueue_head(&info->waiting_to_free);
1217 INIT_LIST_HEAD(&info->v2p_entry_lists);
1218 spin_lock_init(&info->v2p_lock);
1220 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1223 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1225 err = xenbus_switch_state(dev, XenbusStateInitWait);
1232 pr_warn("%s failed\n", __func__);
1233 scsiback_remove(dev);
1238 static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1240 switch (tport->tport_proto_id) {
1241 case SCSI_PROTOCOL_SAS:
1243 case SCSI_PROTOCOL_FCP:
1245 case SCSI_PROTOCOL_ISCSI:
1254 static u8 scsiback_get_fabric_proto_ident(struct se_portal_group *se_tpg)
1256 struct scsiback_tpg *tpg = container_of(se_tpg,
1257 struct scsiback_tpg, se_tpg);
1258 struct scsiback_tport *tport = tpg->tport;
1260 switch (tport->tport_proto_id) {
1261 case SCSI_PROTOCOL_SAS:
1262 return sas_get_fabric_proto_ident(se_tpg);
1263 case SCSI_PROTOCOL_FCP:
1264 return fc_get_fabric_proto_ident(se_tpg);
1265 case SCSI_PROTOCOL_ISCSI:
1266 return iscsi_get_fabric_proto_ident(se_tpg);
1268 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1269 tport->tport_proto_id);
1273 return sas_get_fabric_proto_ident(se_tpg);
1276 static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1278 struct scsiback_tpg *tpg = container_of(se_tpg,
1279 struct scsiback_tpg, se_tpg);
1280 struct scsiback_tport *tport = tpg->tport;
1282 return &tport->tport_name[0];
1285 static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1287 struct scsiback_tpg *tpg = container_of(se_tpg,
1288 struct scsiback_tpg, se_tpg);
1289 return tpg->tport_tpgt;
1292 static u32 scsiback_get_default_depth(struct se_portal_group *se_tpg)
1298 scsiback_get_pr_transport_id(struct se_portal_group *se_tpg,
1299 struct se_node_acl *se_nacl,
1300 struct t10_pr_registration *pr_reg,
1304 struct scsiback_tpg *tpg = container_of(se_tpg,
1305 struct scsiback_tpg, se_tpg);
1306 struct scsiback_tport *tport = tpg->tport;
1308 switch (tport->tport_proto_id) {
1309 case SCSI_PROTOCOL_SAS:
1310 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1312 case SCSI_PROTOCOL_FCP:
1313 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1315 case SCSI_PROTOCOL_ISCSI:
1316 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1319 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1320 tport->tport_proto_id);
1324 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1329 scsiback_get_pr_transport_id_len(struct se_portal_group *se_tpg,
1330 struct se_node_acl *se_nacl,
1331 struct t10_pr_registration *pr_reg,
1334 struct scsiback_tpg *tpg = container_of(se_tpg,
1335 struct scsiback_tpg, se_tpg);
1336 struct scsiback_tport *tport = tpg->tport;
1338 switch (tport->tport_proto_id) {
1339 case SCSI_PROTOCOL_SAS:
1340 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1342 case SCSI_PROTOCOL_FCP:
1343 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1345 case SCSI_PROTOCOL_ISCSI:
1346 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1349 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1350 tport->tport_proto_id);
1354 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1359 scsiback_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
1362 char **port_nexus_ptr)
1364 struct scsiback_tpg *tpg = container_of(se_tpg,
1365 struct scsiback_tpg, se_tpg);
1366 struct scsiback_tport *tport = tpg->tport;
1368 switch (tport->tport_proto_id) {
1369 case SCSI_PROTOCOL_SAS:
1370 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1372 case SCSI_PROTOCOL_FCP:
1373 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1375 case SCSI_PROTOCOL_ISCSI:
1376 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1379 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1380 tport->tport_proto_id);
1384 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1388 static struct se_wwn *
1389 scsiback_make_tport(struct target_fabric_configfs *tf,
1390 struct config_group *group,
1393 struct scsiback_tport *tport;
1398 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1400 return ERR_PTR(-ENOMEM);
1402 tport->tport_wwpn = wwpn;
1404 * Determine the emulated Protocol Identifier and Target Port Name
1405 * based on the incoming configfs directory name.
1407 ptr = strstr(name, "naa.");
1409 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1412 ptr = strstr(name, "fc.");
1414 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1415 off = 3; /* Skip over "fc." */
1418 ptr = strstr(name, "iqn.");
1420 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1424 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1426 return ERR_PTR(-EINVAL);
1429 if (strlen(name) >= VSCSI_NAMELEN) {
1430 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1431 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1433 return ERR_PTR(-EINVAL);
1435 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1437 pr_debug("Allocated emulated Target %s Address: %s\n",
1438 scsiback_dump_proto_id(tport), name);
1440 return &tport->tport_wwn;
1443 static void scsiback_drop_tport(struct se_wwn *wwn)
1445 struct scsiback_tport *tport = container_of(wwn,
1446 struct scsiback_tport, tport_wwn);
1448 pr_debug("Deallocating emulated Target %s Address: %s\n",
1449 scsiback_dump_proto_id(tport), tport->tport_name);
1454 static struct se_node_acl *
1455 scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
1457 return kzalloc(sizeof(struct se_node_acl), GFP_KERNEL);
1461 scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
1462 struct se_node_acl *se_nacl)
1467 static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1472 static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1475 * Do not release struct se_cmd's containing a valid TMR pointer.
1476 * These will be released directly in scsiback_device_action()
1477 * with transport_generic_free_cmd().
1479 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
1482 transport_generic_free_cmd(se_cmd, 0);
1486 static void scsiback_release_cmd(struct se_cmd *se_cmd)
1488 struct vscsibk_pend *pending_req = container_of(se_cmd,
1489 struct vscsibk_pend, se_cmd);
1491 kmem_cache_free(scsiback_cachep, pending_req);
1494 static int scsiback_shutdown_session(struct se_session *se_sess)
1499 static void scsiback_close_session(struct se_session *se_sess)
1503 static u32 scsiback_sess_get_index(struct se_session *se_sess)
1508 static int scsiback_write_pending(struct se_cmd *se_cmd)
1510 /* Go ahead and process the write immediately */
1511 target_execute_cmd(se_cmd);
1516 static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1521 static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1525 static u32 scsiback_get_task_tag(struct se_cmd *se_cmd)
1527 struct vscsibk_pend *pending_req = container_of(se_cmd,
1528 struct vscsibk_pend, se_cmd);
1530 return pending_req->rqid;
1533 static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1538 static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1540 struct vscsibk_pend *pending_req = container_of(se_cmd,
1541 struct vscsibk_pend, se_cmd);
1543 pending_req->result = SAM_STAT_GOOD;
1544 scsiback_cmd_done(pending_req);
1548 static int scsiback_queue_status(struct se_cmd *se_cmd)
1550 struct vscsibk_pend *pending_req = container_of(se_cmd,
1551 struct vscsibk_pend, se_cmd);
1553 if (se_cmd->sense_buffer &&
1554 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1555 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1556 pending_req->result = (DRIVER_SENSE << 24) |
1557 SAM_STAT_CHECK_CONDITION;
1559 pending_req->result = se_cmd->scsi_status;
1561 scsiback_cmd_done(pending_req);
1565 static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1567 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1568 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1570 atomic_set(&tmr->tmr_complete, 1);
1571 wake_up(&tmr->tmr_wait);
1574 static void scsiback_aborted_task(struct se_cmd *se_cmd)
1578 static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg,
1581 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1585 mutex_lock(&tpg->tv_tpg_mutex);
1586 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1587 mutex_unlock(&tpg->tv_tpg_mutex);
1592 static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
1593 const char *page, size_t count)
1595 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1599 if (strlen(page) >= VSCSI_NAMELEN) {
1600 pr_err("param alias: %s, exceeds max: %d\n", page,
1605 mutex_lock(&tpg->tv_tpg_mutex);
1606 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1607 if (tpg->param_alias[len - 1] == '\n')
1608 tpg->param_alias[len - 1] = '\0';
1609 mutex_unlock(&tpg->tv_tpg_mutex);
1614 TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
1616 static struct configfs_attribute *scsiback_param_attrs[] = {
1617 &scsiback_tpg_param_alias.attr,
1621 static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1624 struct se_portal_group *se_tpg;
1625 struct se_session *se_sess;
1626 struct scsiback_nexus *tv_nexus;
1628 mutex_lock(&tpg->tv_tpg_mutex);
1629 if (tpg->tpg_nexus) {
1630 mutex_unlock(&tpg->tv_tpg_mutex);
1631 pr_debug("tpg->tpg_nexus already exists\n");
1634 se_tpg = &tpg->se_tpg;
1636 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1638 mutex_unlock(&tpg->tv_tpg_mutex);
1642 * Initialize the struct se_session pointer
1644 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1645 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1646 mutex_unlock(&tpg->tv_tpg_mutex);
1650 se_sess = tv_nexus->tvn_se_sess;
1652 * Since we are running in 'demo mode' this call with generate a
1653 * struct se_node_acl for the scsiback struct se_portal_group with
1654 * the SCSI Initiator port name of the passed configfs group 'name'.
1656 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1657 se_tpg, (unsigned char *)name);
1658 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1659 mutex_unlock(&tpg->tv_tpg_mutex);
1660 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1664 /* Now register the TCM pvscsi virtual I_T Nexus as active. */
1665 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1666 tv_nexus->tvn_se_sess, tv_nexus);
1667 tpg->tpg_nexus = tv_nexus;
1669 mutex_unlock(&tpg->tv_tpg_mutex);
1673 transport_free_session(se_sess);
1678 static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1680 struct se_session *se_sess;
1681 struct scsiback_nexus *tv_nexus;
1683 mutex_lock(&tpg->tv_tpg_mutex);
1684 tv_nexus = tpg->tpg_nexus;
1686 mutex_unlock(&tpg->tv_tpg_mutex);
1690 se_sess = tv_nexus->tvn_se_sess;
1692 mutex_unlock(&tpg->tv_tpg_mutex);
1696 if (tpg->tv_tpg_port_count != 0) {
1697 mutex_unlock(&tpg->tv_tpg_mutex);
1698 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1699 tpg->tv_tpg_port_count);
1703 if (tpg->tv_tpg_fe_count != 0) {
1704 mutex_unlock(&tpg->tv_tpg_mutex);
1705 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1706 tpg->tv_tpg_fe_count);
1710 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1711 scsiback_dump_proto_id(tpg->tport),
1712 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1715 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1717 transport_deregister_session(tv_nexus->tvn_se_sess);
1718 tpg->tpg_nexus = NULL;
1719 mutex_unlock(&tpg->tv_tpg_mutex);
1725 static ssize_t scsiback_tpg_show_nexus(struct se_portal_group *se_tpg,
1728 struct scsiback_tpg *tpg = container_of(se_tpg,
1729 struct scsiback_tpg, se_tpg);
1730 struct scsiback_nexus *tv_nexus;
1733 mutex_lock(&tpg->tv_tpg_mutex);
1734 tv_nexus = tpg->tpg_nexus;
1736 mutex_unlock(&tpg->tv_tpg_mutex);
1739 ret = snprintf(page, PAGE_SIZE, "%s\n",
1740 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1741 mutex_unlock(&tpg->tv_tpg_mutex);
1746 static ssize_t scsiback_tpg_store_nexus(struct se_portal_group *se_tpg,
1750 struct scsiback_tpg *tpg = container_of(se_tpg,
1751 struct scsiback_tpg, se_tpg);
1752 struct scsiback_tport *tport_wwn = tpg->tport;
1753 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1756 * Shutdown the active I_T nexus if 'NULL' is passed.
1758 if (!strncmp(page, "NULL", 4)) {
1759 ret = scsiback_drop_nexus(tpg);
1760 return (!ret) ? count : ret;
1763 * Otherwise make sure the passed virtual Initiator port WWN matches
1764 * the fabric protocol_id set in scsiback_make_tport(), and call
1765 * scsiback_make_nexus().
1767 if (strlen(page) >= VSCSI_NAMELEN) {
1768 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1769 page, VSCSI_NAMELEN);
1772 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1774 ptr = strstr(i_port, "naa.");
1776 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1777 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1778 i_port, scsiback_dump_proto_id(tport_wwn));
1781 port_ptr = &i_port[0];
1784 ptr = strstr(i_port, "fc.");
1786 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1787 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1788 i_port, scsiback_dump_proto_id(tport_wwn));
1791 port_ptr = &i_port[3]; /* Skip over "fc." */
1794 ptr = strstr(i_port, "iqn.");
1796 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1797 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1798 i_port, scsiback_dump_proto_id(tport_wwn));
1801 port_ptr = &i_port[0];
1804 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1808 * Clear any trailing newline for the NAA WWN
1811 if (i_port[strlen(i_port) - 1] == '\n')
1812 i_port[strlen(i_port) - 1] = '\0';
1814 ret = scsiback_make_nexus(tpg, port_ptr);
1821 TF_TPG_BASE_ATTR(scsiback, nexus, S_IRUGO | S_IWUSR);
1823 static struct configfs_attribute *scsiback_tpg_attrs[] = {
1824 &scsiback_tpg_nexus.attr,
1829 scsiback_wwn_show_attr_version(struct target_fabric_configfs *tf,
1832 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1834 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1837 TF_WWN_ATTR_RO(scsiback, version);
1839 static struct configfs_attribute *scsiback_wwn_attrs[] = {
1840 &scsiback_wwn_version.attr,
1844 static char *scsiback_get_fabric_name(void)
1846 return "xen-pvscsi";
1849 static int scsiback_port_link(struct se_portal_group *se_tpg,
1852 struct scsiback_tpg *tpg = container_of(se_tpg,
1853 struct scsiback_tpg, se_tpg);
1855 mutex_lock(&tpg->tv_tpg_mutex);
1856 tpg->tv_tpg_port_count++;
1857 mutex_unlock(&tpg->tv_tpg_mutex);
1862 static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1865 struct scsiback_tpg *tpg = container_of(se_tpg,
1866 struct scsiback_tpg, se_tpg);
1868 mutex_lock(&tpg->tv_tpg_mutex);
1869 tpg->tv_tpg_port_count--;
1870 mutex_unlock(&tpg->tv_tpg_mutex);
1873 static struct se_portal_group *
1874 scsiback_make_tpg(struct se_wwn *wwn,
1875 struct config_group *group,
1878 struct scsiback_tport *tport = container_of(wwn,
1879 struct scsiback_tport, tport_wwn);
1881 struct scsiback_tpg *tpg;
1885 if (strstr(name, "tpgt_") != name)
1886 return ERR_PTR(-EINVAL);
1887 ret = kstrtou16(name + 5, 10, &tpgt);
1889 return ERR_PTR(ret);
1891 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1893 return ERR_PTR(-ENOMEM);
1895 mutex_init(&tpg->tv_tpg_mutex);
1896 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1897 INIT_LIST_HEAD(&tpg->info_list);
1899 tpg->tport_tpgt = tpgt;
1901 ret = core_tpg_register(&scsiback_ops, wwn,
1902 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1907 mutex_lock(&scsiback_mutex);
1908 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1909 mutex_unlock(&scsiback_mutex);
1911 return &tpg->se_tpg;
1914 static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1916 struct scsiback_tpg *tpg = container_of(se_tpg,
1917 struct scsiback_tpg, se_tpg);
1919 mutex_lock(&scsiback_mutex);
1920 list_del(&tpg->tv_tpg_list);
1921 mutex_unlock(&scsiback_mutex);
1923 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1925 scsiback_drop_nexus(tpg);
1927 * Deregister the se_tpg from TCM.
1929 core_tpg_deregister(se_tpg);
1933 static int scsiback_check_true(struct se_portal_group *se_tpg)
1938 static int scsiback_check_false(struct se_portal_group *se_tpg)
1943 static const struct target_core_fabric_ops scsiback_ops = {
1944 .module = THIS_MODULE,
1945 .name = "xen-pvscsi",
1946 .get_fabric_name = scsiback_get_fabric_name,
1947 .get_fabric_proto_ident = scsiback_get_fabric_proto_ident,
1948 .tpg_get_wwn = scsiback_get_fabric_wwn,
1949 .tpg_get_tag = scsiback_get_tag,
1950 .tpg_get_default_depth = scsiback_get_default_depth,
1951 .tpg_get_pr_transport_id = scsiback_get_pr_transport_id,
1952 .tpg_get_pr_transport_id_len = scsiback_get_pr_transport_id_len,
1953 .tpg_parse_pr_out_transport_id = scsiback_parse_pr_out_transport_id,
1954 .tpg_check_demo_mode = scsiback_check_true,
1955 .tpg_check_demo_mode_cache = scsiback_check_true,
1956 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1957 .tpg_check_prod_mode_write_protect = scsiback_check_false,
1958 .tpg_alloc_fabric_acl = scsiback_alloc_fabric_acl,
1959 .tpg_release_fabric_acl = scsiback_release_fabric_acl,
1960 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1961 .check_stop_free = scsiback_check_stop_free,
1962 .release_cmd = scsiback_release_cmd,
1963 .put_session = NULL,
1964 .shutdown_session = scsiback_shutdown_session,
1965 .close_session = scsiback_close_session,
1966 .sess_get_index = scsiback_sess_get_index,
1967 .sess_get_initiator_sid = NULL,
1968 .write_pending = scsiback_write_pending,
1969 .write_pending_status = scsiback_write_pending_status,
1970 .set_default_node_attributes = scsiback_set_default_node_attrs,
1971 .get_task_tag = scsiback_get_task_tag,
1972 .get_cmd_state = scsiback_get_cmd_state,
1973 .queue_data_in = scsiback_queue_data_in,
1974 .queue_status = scsiback_queue_status,
1975 .queue_tm_rsp = scsiback_queue_tm_rsp,
1976 .aborted_task = scsiback_aborted_task,
1978 * Setup callers for generic logic in target_core_fabric_configfs.c
1980 .fabric_make_wwn = scsiback_make_tport,
1981 .fabric_drop_wwn = scsiback_drop_tport,
1982 .fabric_make_tpg = scsiback_make_tpg,
1983 .fabric_drop_tpg = scsiback_drop_tpg,
1984 .fabric_post_link = scsiback_port_link,
1985 .fabric_pre_unlink = scsiback_port_unlink,
1986 .fabric_make_np = NULL,
1987 .fabric_drop_np = NULL,
1989 .fabric_make_nodeacl = scsiback_make_nodeacl,
1990 .fabric_drop_nodeacl = scsiback_drop_nodeacl,
1993 .tfc_wwn_attrs = scsiback_wwn_attrs,
1994 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1995 .tfc_tpg_param_attrs = scsiback_param_attrs,
1998 static const struct xenbus_device_id scsiback_ids[] = {
2003 static struct xenbus_driver scsiback_driver = {
2004 .ids = scsiback_ids,
2005 .probe = scsiback_probe,
2006 .remove = scsiback_remove,
2007 .otherend_changed = scsiback_frontend_changed
2010 static void scsiback_init_pend(void *p)
2012 struct vscsibk_pend *pend = p;
2015 memset(pend, 0, sizeof(*pend));
2016 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
2017 pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
2020 static int __init scsiback_init(void)
2027 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
2028 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
2030 scsiback_cachep = kmem_cache_create("vscsiif_cache",
2031 sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend);
2032 if (!scsiback_cachep)
2035 ret = xenbus_register_backend(&scsiback_driver);
2037 goto out_cache_destroy;
2039 ret = target_register_template(&scsiback_ops);
2041 goto out_unregister_xenbus;
2045 out_unregister_xenbus:
2046 xenbus_unregister_driver(&scsiback_driver);
2048 kmem_cache_destroy(scsiback_cachep);
2049 pr_err("%s: error %d\n", __func__, ret);
2053 static void __exit scsiback_exit(void)
2057 while (free_pages_num) {
2058 if (get_free_page(&page))
2060 gnttab_free_pages(1, &page);
2062 target_unregister_template(&scsiback_ops);
2063 xenbus_unregister_driver(&scsiback_driver);
2064 kmem_cache_destroy(scsiback_cachep);
2067 module_init(scsiback_init);
2068 module_exit(scsiback_exit);
2070 MODULE_DESCRIPTION("Xen SCSI backend driver");
2071 MODULE_LICENSE("Dual BSD/GPL");
2072 MODULE_ALIAS("xen-backend:vscsi");
2073 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");