2 * Xen SCSI backend driver
4 * Copyright (c) 2008, FUJITSU Limited
6 * Based on the blkback driver code.
7 * Adaption to kernel taget core infrastructure taken from vhost/scsi.c
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 #define pr_fmt(fmt) "xen-pvscsi: " fmt
38 #include <linux/module.h>
39 #include <linux/utsname.h>
40 #include <linux/interrupt.h>
41 #include <linux/slab.h>
42 #include <linux/wait.h>
43 #include <linux/sched.h>
44 #include <linux/list.h>
45 #include <linux/gfp.h>
46 #include <linux/delay.h>
47 #include <linux/spinlock.h>
48 #include <linux/configfs.h>
50 #include <generated/utsrelease.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_dbg.h>
54 #include <scsi/scsi_eh.h>
55 #include <scsi/scsi_tcq.h>
57 #include <target/target_core_base.h>
58 #include <target/target_core_fabric.h>
59 #include <target/target_core_configfs.h>
60 #include <target/target_core_fabric_configfs.h>
62 #include <asm/hypervisor.h>
65 #include <xen/balloon.h>
66 #include <xen/events.h>
67 #include <xen/xenbus.h>
68 #include <xen/grant_table.h>
71 #include <xen/interface/grant_table.h>
72 #include <xen/interface/io/vscsiif.h>
74 #define VSCSI_VERSION "v0.1"
75 #define VSCSI_NAMELEN 32
78 unsigned int hst; /* host */
79 unsigned int chn; /* channel */
80 unsigned int tgt; /* target */
81 unsigned int lun; /* LUN */
85 struct ids_tuple v; /* translate from */
86 struct scsiback_tpg *tpg; /* translate to */
93 struct xenbus_device *dev;
98 struct vscsiif_back_ring ring;
101 spinlock_t ring_lock;
102 atomic_t nr_unreplied_reqs;
105 struct list_head v2p_entry_lists;
107 wait_queue_head_t waiting_to_free;
110 /* theoretical maximum of grants for one request */
111 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
114 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
115 * call to map/unmap grants. Don't choose it too large, as there are arrays
116 * with VSCSI_GRANT_BATCH elements allocated on the stack.
118 #define VSCSI_GRANT_BATCH 16
120 struct vscsibk_pend {
123 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
126 uint8_t sc_data_direction;
127 uint16_t n_sg; /* real length of SG list */
128 uint16_t n_grants; /* SG pages and potentially SG list */
132 struct vscsibk_info *info;
133 struct v2p_entry *v2p;
134 struct scatterlist *sgl;
136 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
138 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
139 struct page *pages[VSCSI_MAX_GRANTS];
141 struct se_cmd se_cmd;
144 struct scsiback_tmr {
145 atomic_t tmr_complete;
146 wait_queue_head_t tmr_wait;
149 struct scsiback_nexus {
150 /* Pointer to TCM session for I_T Nexus */
151 struct se_session *tvn_se_sess;
154 struct scsiback_tport {
155 /* SCSI protocol the tport is providing */
157 /* Binary World Wide unique Port Name for pvscsi Target port */
159 /* ASCII formatted WWPN for pvscsi Target port */
160 char tport_name[VSCSI_NAMELEN];
161 /* Returned by scsiback_make_tport() */
162 struct se_wwn tport_wwn;
165 struct scsiback_tpg {
166 /* scsiback port target portal group tag for TCM */
168 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
169 int tv_tpg_port_count;
170 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
172 /* list for scsiback_list */
173 struct list_head tv_tpg_list;
174 /* Used to protect access for tpg_nexus */
175 struct mutex tv_tpg_mutex;
176 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
177 struct scsiback_nexus *tpg_nexus;
178 /* Pointer back to scsiback_tport */
179 struct scsiback_tport *tport;
180 /* Returned by scsiback_make_tpg() */
181 struct se_portal_group se_tpg;
182 /* alias used in xenstore */
183 char param_alias[VSCSI_NAMELEN];
184 /* list of info structures related to this target portal group */
185 struct list_head info_list;
188 #define SCSIBACK_INVALID_HANDLE (~0)
190 static bool log_print_stat;
191 module_param(log_print_stat, bool, 0644);
193 static int scsiback_max_buffer_pages = 1024;
194 module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
195 MODULE_PARM_DESC(max_buffer_pages,
196 "Maximum number of free pages to keep in backend buffer");
198 static struct kmem_cache *scsiback_cachep;
199 static DEFINE_SPINLOCK(free_pages_lock);
200 static int free_pages_num;
201 static LIST_HEAD(scsiback_free_pages);
203 /* Global spinlock to protect scsiback TPG list */
204 static DEFINE_MUTEX(scsiback_mutex);
205 static LIST_HEAD(scsiback_list);
207 /* Local pointer to allocated TCM configfs fabric module */
208 static struct target_fabric_configfs *scsiback_fabric_configfs;
210 static void scsiback_get(struct vscsibk_info *info)
212 atomic_inc(&info->nr_unreplied_reqs);
215 static void scsiback_put(struct vscsibk_info *info)
217 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
218 wake_up(&info->waiting_to_free);
221 static void put_free_pages(struct page **page, int num)
224 int i = free_pages_num + num, n = num;
228 if (i > scsiback_max_buffer_pages) {
229 n = min(num, i - scsiback_max_buffer_pages);
230 gnttab_free_pages(n, page + num - n);
233 spin_lock_irqsave(&free_pages_lock, flags);
234 for (i = 0; i < n; i++)
235 list_add(&page[i]->lru, &scsiback_free_pages);
237 spin_unlock_irqrestore(&free_pages_lock, flags);
240 static int get_free_page(struct page **page)
244 spin_lock_irqsave(&free_pages_lock, flags);
245 if (list_empty(&scsiback_free_pages)) {
246 spin_unlock_irqrestore(&free_pages_lock, flags);
247 return gnttab_alloc_pages(1, page);
249 page[0] = list_first_entry(&scsiback_free_pages, struct page, lru);
250 list_del(&page[0]->lru);
252 spin_unlock_irqrestore(&free_pages_lock, flags);
256 static unsigned long vaddr_page(struct page *page)
258 unsigned long pfn = page_to_pfn(page);
260 return (unsigned long)pfn_to_kaddr(pfn);
263 static unsigned long vaddr(struct vscsibk_pend *req, int seg)
265 return vaddr_page(req->pages[seg]);
268 static void scsiback_print_status(char *sense_buffer, int errors,
269 struct vscsibk_pend *pending_req)
271 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
273 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
274 tpg->tport->tport_name, pending_req->v2p->lun,
275 pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
276 host_byte(errors), driver_byte(errors));
279 static void scsiback_fast_flush_area(struct vscsibk_pend *req)
281 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
282 struct page *pages[VSCSI_GRANT_BATCH];
283 unsigned int i, invcount = 0;
284 grant_handle_t handle;
294 for (i = 0; i < req->n_grants; i++) {
295 handle = req->grant_handles[i];
296 if (handle == SCSIBACK_INVALID_HANDLE)
298 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
299 GNTMAP_host_map, handle);
300 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
301 pages[invcount] = req->pages[i];
302 put_page(pages[invcount]);
304 if (invcount < VSCSI_GRANT_BATCH)
306 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
312 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
316 put_free_pages(req->pages, req->n_grants);
320 static void scsiback_free_translation_entry(struct kref *kref)
322 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
323 struct scsiback_tpg *tpg = entry->tpg;
325 mutex_lock(&tpg->tv_tpg_mutex);
326 tpg->tv_tpg_fe_count--;
327 mutex_unlock(&tpg->tv_tpg_mutex);
332 static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
333 uint32_t resid, struct vscsibk_pend *pending_req)
335 struct vscsiif_response *ring_res;
336 struct vscsibk_info *info = pending_req->info;
338 struct scsi_sense_hdr sshdr;
342 spin_lock_irqsave(&info->ring_lock, flags);
344 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
345 info->ring.rsp_prod_pvt++;
347 ring_res->rslt = result;
348 ring_res->rqid = pending_req->rqid;
350 if (sense_buffer != NULL &&
351 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
353 len = min_t(unsigned, 8 + sense_buffer[7],
354 VSCSIIF_SENSE_BUFFERSIZE);
355 memcpy(ring_res->sense_buffer, sense_buffer, len);
356 ring_res->sense_len = len;
358 ring_res->sense_len = 0;
361 ring_res->residual_len = resid;
363 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
364 spin_unlock_irqrestore(&info->ring_lock, flags);
367 notify_remote_via_irq(info->irq);
369 if (pending_req->v2p)
370 kref_put(&pending_req->v2p->kref,
371 scsiback_free_translation_entry);
374 static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
376 struct vscsibk_info *info = pending_req->info;
377 unsigned char *sense_buffer;
381 sense_buffer = pending_req->sense_buffer;
382 resid = pending_req->se_cmd.residual_count;
383 errors = pending_req->result;
385 if (errors && log_print_stat)
386 scsiback_print_status(sense_buffer, errors, pending_req);
388 scsiback_fast_flush_area(pending_req);
389 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
393 static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
395 struct se_cmd *se_cmd = &pending_req->se_cmd;
396 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
399 memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
401 memset(se_cmd, 0, sizeof(*se_cmd));
403 scsiback_get(pending_req->info);
404 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
405 pending_req->sense_buffer, pending_req->v2p->lun,
406 pending_req->data_len, 0,
407 pending_req->sc_data_direction, 0,
408 pending_req->sgl, pending_req->n_sg,
411 transport_send_check_condition_and_sense(se_cmd,
412 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
413 transport_generic_free_cmd(se_cmd, 0);
417 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
418 struct page **pg, grant_handle_t *grant, int cnt)
425 err = gnttab_map_refs(map, NULL, pg, cnt);
427 for (i = 0; i < cnt; i++) {
428 if (unlikely(map[i].status != GNTST_okay)) {
429 pr_err("invalid buffer -- could not remap it\n");
430 map[i].handle = SCSIBACK_INVALID_HANDLE;
435 grant[i] = map[i].handle;
440 static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
441 struct scsiif_request_segment *seg, struct page **pg,
442 grant_handle_t *grant, int cnt, u32 flags)
444 int mapcount = 0, i, err = 0;
445 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
446 struct vscsibk_info *info = pending_req->info;
448 for (i = 0; i < cnt; i++) {
449 if (get_free_page(pg + mapcount)) {
450 put_free_pages(pg, mapcount);
451 pr_err("no grant page\n");
454 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
455 flags, seg[i].gref, info->domid);
457 if (mapcount < VSCSI_GRANT_BATCH)
459 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
462 pending_req->n_grants += mapcount;
467 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
468 pending_req->n_grants += mapcount;
472 static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
473 struct vscsibk_pend *pending_req)
476 int i, err, n_segs, i_seg = 0;
478 struct scsiif_request_segment *seg;
479 unsigned long end_seg = 0;
480 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
481 unsigned int nr_sgl = 0;
482 struct scatterlist *sg;
483 grant_handle_t *grant;
485 pending_req->n_sg = 0;
486 pending_req->n_grants = 0;
487 pending_req->data_len = 0;
489 nr_segments &= ~VSCSIIF_SG_GRANT;
493 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
494 pr_debug("invalid parameter nr_seg = %d\n",
495 ring_req->nr_segments);
499 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
500 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
501 pending_req->pages, pending_req->grant_handles,
502 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
505 nr_sgl = nr_segments;
507 for (i = 0; i < nr_sgl; i++) {
508 n_segs = ring_req->seg[i].length /
509 sizeof(struct scsiif_request_segment);
510 if ((unsigned)ring_req->seg[i].offset +
511 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
512 n_segs * sizeof(struct scsiif_request_segment) !=
513 ring_req->seg[i].length)
515 nr_segments += n_segs;
517 if (nr_segments > SG_ALL) {
518 pr_debug("invalid nr_seg = %d\n", nr_segments);
523 /* free of (sgl) in fast_flush_area() */
524 pending_req->sgl = kmalloc_array(nr_segments,
525 sizeof(struct scatterlist), GFP_KERNEL);
526 if (!pending_req->sgl)
529 sg_init_table(pending_req->sgl, nr_segments);
530 pending_req->n_sg = nr_segments;
532 flags = GNTMAP_host_map;
533 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
534 flags |= GNTMAP_readonly;
536 pg = pending_req->pages + nr_sgl;
537 grant = pending_req->grant_handles + nr_sgl;
540 err = scsiback_gnttab_data_map_list(pending_req, seg,
541 pg, grant, nr_segments, flags);
545 for (i = 0; i < nr_sgl; i++) {
546 seg = (struct scsiif_request_segment *)(
547 vaddr(pending_req, i) + ring_req->seg[i].offset);
548 n_segs = ring_req->seg[i].length /
549 sizeof(struct scsiif_request_segment);
550 err = scsiback_gnttab_data_map_list(pending_req, seg,
551 pg, grant, n_segs, flags);
557 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
558 seg = (struct scsiif_request_segment *)end_seg;
559 end_seg += ring_req->seg[0].length;
560 pg = pending_req->pages + nr_sgl;
563 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
564 sg_set_page(sg, pg[i], seg->length, seg->offset);
565 pending_req->data_len += seg->length;
567 if (nr_sgl && (unsigned long)seg >= end_seg) {
569 end_seg = vaddr(pending_req, i_seg) +
570 ring_req->seg[i_seg].offset;
571 seg = (struct scsiif_request_segment *)end_seg;
572 end_seg += ring_req->seg[i_seg].length;
574 if (sg->offset >= PAGE_SIZE ||
575 sg->length > PAGE_SIZE ||
576 sg->offset + sg->length > PAGE_SIZE)
583 static void scsiback_disconnect(struct vscsibk_info *info)
585 wait_event(info->waiting_to_free,
586 atomic_read(&info->nr_unreplied_reqs) == 0);
588 unbind_from_irqhandler(info->irq, info);
590 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
593 static void scsiback_device_action(struct vscsibk_pend *pending_req,
594 enum tcm_tmreq_table act, int tag)
596 int rc, err = FAILED;
597 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
598 struct se_cmd *se_cmd = &pending_req->se_cmd;
599 struct scsiback_tmr *tmr;
601 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
605 init_waitqueue_head(&tmr->tmr_wait);
607 transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
608 tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, TCM_SIMPLE_TAG,
609 &pending_req->sense_buffer[0]);
611 rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
615 se_cmd->se_tmr_req->ref_task_tag = tag;
617 if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
620 transport_generic_handle_tmr(se_cmd);
621 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
623 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
628 transport_generic_free_cmd(&pending_req->se_cmd, 1);
632 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
634 kmem_cache_free(scsiback_cachep, pending_req);
638 Perform virtual to physical translation
640 static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
643 struct v2p_entry *entry;
644 struct list_head *head = &(info->v2p_entry_lists);
647 spin_lock_irqsave(&info->v2p_lock, flags);
648 list_for_each_entry(entry, head, l) {
649 if ((entry->v.chn == v->chn) &&
650 (entry->v.tgt == v->tgt) &&
651 (entry->v.lun == v->lun)) {
652 kref_get(&entry->kref);
659 spin_unlock_irqrestore(&info->v2p_lock, flags);
663 static int prepare_pending_reqs(struct vscsibk_info *info,
664 struct vscsiif_request *ring_req,
665 struct vscsibk_pend *pending_req)
667 struct v2p_entry *v2p;
668 struct ids_tuple vir;
670 pending_req->rqid = ring_req->rqid;
671 pending_req->info = info;
673 vir.chn = ring_req->channel;
674 vir.tgt = ring_req->id;
675 vir.lun = ring_req->lun;
677 v2p = scsiback_do_translation(info, &vir);
679 pending_req->v2p = NULL;
680 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
681 vir.chn, vir.tgt, vir.lun);
684 pending_req->v2p = v2p;
686 /* request range check from frontend */
687 pending_req->sc_data_direction = ring_req->sc_data_direction;
688 if ((pending_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
689 (pending_req->sc_data_direction != DMA_TO_DEVICE) &&
690 (pending_req->sc_data_direction != DMA_FROM_DEVICE) &&
691 (pending_req->sc_data_direction != DMA_NONE)) {
692 pr_debug("invalid parameter data_dir = %d\n",
693 pending_req->sc_data_direction);
697 pending_req->cmd_len = ring_req->cmd_len;
698 if (pending_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
699 pr_debug("invalid parameter cmd_len = %d\n",
700 pending_req->cmd_len);
703 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
708 static int scsiback_do_cmd_fn(struct vscsibk_info *info)
710 struct vscsiif_back_ring *ring = &info->ring;
711 struct vscsiif_request ring_req;
712 struct vscsibk_pend *pending_req;
718 rp = ring->sring->req_prod;
719 rmb(); /* guest system is accessing ring, too */
721 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
722 rc = ring->rsp_prod_pvt;
723 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
724 info->domid, rp, rc, rp - rc);
725 info->ring_error = 1;
730 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
732 pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
736 ring_req = *RING_GET_REQUEST(ring, rc);
737 ring->req_cons = ++rc;
739 err = prepare_pending_reqs(info, &ring_req, pending_req);
743 result = DID_NO_CONNECT;
746 result = DRIVER_ERROR;
749 scsiback_do_resp_with_sense(NULL, result << 24, 0,
751 kmem_cache_free(scsiback_cachep, pending_req);
755 switch (ring_req.act) {
756 case VSCSIIF_ACT_SCSI_CDB:
757 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
758 scsiback_fast_flush_area(pending_req);
759 scsiback_do_resp_with_sense(NULL,
760 DRIVER_ERROR << 24, 0, pending_req);
761 kmem_cache_free(scsiback_cachep, pending_req);
763 scsiback_cmd_exec(pending_req);
766 case VSCSIIF_ACT_SCSI_ABORT:
767 scsiback_device_action(pending_req, TMR_ABORT_TASK,
770 case VSCSIIF_ACT_SCSI_RESET:
771 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
774 pr_err_ratelimited("invalid request\n");
775 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
777 kmem_cache_free(scsiback_cachep, pending_req);
781 /* Yield point for this unbounded loop. */
785 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
789 static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
791 struct vscsibk_info *info = dev_id;
793 if (info->ring_error)
796 while (scsiback_do_cmd_fn(info))
802 static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
803 evtchn_port_t evtchn)
806 struct vscsiif_sring *sring;
812 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
816 sring = (struct vscsiif_sring *)area;
817 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
819 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
825 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
826 IRQF_ONESHOT, "vscsiif-backend", info);
833 unbind_from_irqhandler(info->irq, info);
836 xenbus_unmap_ring_vfree(info->dev, area);
841 static int scsiback_map(struct vscsibk_info *info)
843 struct xenbus_device *dev = info->dev;
844 unsigned int ring_ref, evtchn;
847 err = xenbus_gather(XBT_NIL, dev->otherend,
848 "ring-ref", "%u", &ring_ref,
849 "event-channel", "%u", &evtchn, NULL);
851 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
855 return scsiback_init_sring(info, ring_ref, evtchn);
859 Add a new translation entry
861 static int scsiback_add_translation_entry(struct vscsibk_info *info,
862 char *phy, struct ids_tuple *v)
865 struct v2p_entry *entry;
866 struct v2p_entry *new;
867 struct list_head *head = &(info->v2p_entry_lists);
871 struct scsiback_tpg *tpg_entry, *tpg = NULL;
872 char *error = "doesn't exist";
874 lunp = strrchr(phy, ':');
876 pr_err("illegal format of physical device %s\n", phy);
881 if (kstrtouint(lunp, 10, &lun) || lun >= TRANSPORT_MAX_LUNS_PER_TPG) {
882 pr_err("lun number not valid: %s\n", lunp);
886 mutex_lock(&scsiback_mutex);
887 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
888 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
889 !strcmp(phy, tpg_entry->param_alias)) {
890 spin_lock(&tpg_entry->se_tpg.tpg_lun_lock);
891 if (tpg_entry->se_tpg.tpg_lun_list[lun]->lun_status ==
892 TRANSPORT_LUN_STATUS_ACTIVE) {
893 if (!tpg_entry->tpg_nexus)
894 error = "nexus undefined";
898 spin_unlock(&tpg_entry->se_tpg.tpg_lun_lock);
903 mutex_lock(&tpg->tv_tpg_mutex);
904 tpg->tv_tpg_fe_count++;
905 mutex_unlock(&tpg->tv_tpg_mutex);
907 mutex_unlock(&scsiback_mutex);
910 pr_err("%s:%d %s\n", phy, lun, error);
914 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
920 spin_lock_irqsave(&info->v2p_lock, flags);
922 /* Check double assignment to identical virtual ID */
923 list_for_each_entry(entry, head, l) {
924 if ((entry->v.chn == v->chn) &&
925 (entry->v.tgt == v->tgt) &&
926 (entry->v.lun == v->lun)) {
927 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
934 /* Create a new translation entry and add to the list */
935 kref_init(&new->kref);
939 list_add_tail(&new->l, head);
942 spin_unlock_irqrestore(&info->v2p_lock, flags);
945 mutex_lock(&tpg->tv_tpg_mutex);
946 tpg->tv_tpg_fe_count--;
947 mutex_unlock(&tpg->tv_tpg_mutex);
955 static void __scsiback_del_translation_entry(struct v2p_entry *entry)
958 kref_put(&entry->kref, scsiback_free_translation_entry);
962 Delete the translation entry specfied
964 static int scsiback_del_translation_entry(struct vscsibk_info *info,
967 struct v2p_entry *entry;
968 struct list_head *head = &(info->v2p_entry_lists);
971 spin_lock_irqsave(&info->v2p_lock, flags);
972 /* Find out the translation entry specified */
973 list_for_each_entry(entry, head, l) {
974 if ((entry->v.chn == v->chn) &&
975 (entry->v.tgt == v->tgt) &&
976 (entry->v.lun == v->lun)) {
981 spin_unlock_irqrestore(&info->v2p_lock, flags);
985 /* Delete the translation entry specfied */
986 __scsiback_del_translation_entry(entry);
988 spin_unlock_irqrestore(&info->v2p_lock, flags);
992 static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
993 char *phy, struct ids_tuple *vir, int try)
995 if (!scsiback_add_translation_entry(info, phy, vir)) {
996 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
997 "%d", XenbusStateInitialised)) {
998 pr_err("xenbus_printf error %s\n", state);
999 scsiback_del_translation_entry(info, vir);
1002 xenbus_printf(XBT_NIL, info->dev->nodename, state,
1003 "%d", XenbusStateClosed);
1007 static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1008 struct ids_tuple *vir)
1010 if (!scsiback_del_translation_entry(info, vir)) {
1011 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1012 "%d", XenbusStateClosed))
1013 pr_err("xenbus_printf error %s\n", state);
1017 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1018 #define VSCSIBACK_OP_UPDATEDEV_STATE 2
1020 static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1024 struct ids_tuple vir;
1027 char phy[VSCSI_NAMELEN];
1030 struct xenbus_device *dev = info->dev;
1033 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1034 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1035 if (XENBUS_EXIST_ERR(err))
1038 /* physical SCSI device */
1039 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1040 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1042 xenbus_printf(XBT_NIL, dev->nodename, state,
1043 "%d", XenbusStateClosed);
1046 strlcpy(phy, val, VSCSI_NAMELEN);
1049 /* virtual SCSI device */
1050 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1051 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1052 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1053 if (XENBUS_EXIST_ERR(err)) {
1054 xenbus_printf(XBT_NIL, dev->nodename, state,
1055 "%d", XenbusStateClosed);
1060 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
1061 switch (device_state) {
1062 case XenbusStateInitialising:
1063 scsiback_do_add_lun(info, state, phy, &vir, 0);
1065 case XenbusStateConnected:
1066 scsiback_do_add_lun(info, state, phy, &vir, 1);
1068 case XenbusStateClosing:
1069 scsiback_do_del_lun(info, state, &vir);
1076 case VSCSIBACK_OP_UPDATEDEV_STATE:
1077 if (device_state == XenbusStateInitialised) {
1078 /* modify vscsi-devs/dev-x/state */
1079 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1080 "%d", XenbusStateConnected)) {
1081 pr_err("xenbus_printf error %s\n", str);
1082 scsiback_del_translation_entry(info, &vir);
1083 xenbus_printf(XBT_NIL, dev->nodename, state,
1084 "%d", XenbusStateClosed);
1088 /* When it is necessary, processing is added here. */
1094 static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1098 unsigned int ndir = 0;
1100 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1105 for (i = 0; i < ndir; i++)
1106 scsiback_do_1lun_hotplug(info, op, dir[i]);
1111 static void scsiback_frontend_changed(struct xenbus_device *dev,
1112 enum xenbus_state frontend_state)
1114 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1116 switch (frontend_state) {
1117 case XenbusStateInitialising:
1120 case XenbusStateInitialised:
1121 if (scsiback_map(info))
1124 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1125 xenbus_switch_state(dev, XenbusStateConnected);
1128 case XenbusStateConnected:
1129 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1131 if (dev->state == XenbusStateConnected)
1134 xenbus_switch_state(dev, XenbusStateConnected);
1137 case XenbusStateClosing:
1139 scsiback_disconnect(info);
1141 xenbus_switch_state(dev, XenbusStateClosing);
1144 case XenbusStateClosed:
1145 xenbus_switch_state(dev, XenbusStateClosed);
1146 if (xenbus_dev_is_online(dev))
1148 /* fall through if not online */
1149 case XenbusStateUnknown:
1150 device_unregister(&dev->dev);
1153 case XenbusStateReconfiguring:
1154 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1155 xenbus_switch_state(dev, XenbusStateReconfigured);
1160 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1167 Release the translation entry specfied
1169 static void scsiback_release_translation_entry(struct vscsibk_info *info)
1171 struct v2p_entry *entry, *tmp;
1172 struct list_head *head = &(info->v2p_entry_lists);
1173 unsigned long flags;
1175 spin_lock_irqsave(&info->v2p_lock, flags);
1177 list_for_each_entry_safe(entry, tmp, head, l)
1178 __scsiback_del_translation_entry(entry);
1180 spin_unlock_irqrestore(&info->v2p_lock, flags);
1183 static int scsiback_remove(struct xenbus_device *dev)
1185 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1188 scsiback_disconnect(info);
1190 scsiback_release_translation_entry(info);
1192 dev_set_drvdata(&dev->dev, NULL);
1197 static int scsiback_probe(struct xenbus_device *dev,
1198 const struct xenbus_device_id *id)
1202 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1205 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
1208 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1212 dev_set_drvdata(&dev->dev, info);
1214 info->domid = dev->otherend_id;
1215 spin_lock_init(&info->ring_lock);
1216 info->ring_error = 0;
1217 atomic_set(&info->nr_unreplied_reqs, 0);
1218 init_waitqueue_head(&info->waiting_to_free);
1221 INIT_LIST_HEAD(&info->v2p_entry_lists);
1222 spin_lock_init(&info->v2p_lock);
1224 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1227 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1229 err = xenbus_switch_state(dev, XenbusStateInitWait);
1236 pr_warn("%s failed\n", __func__);
1237 scsiback_remove(dev);
1242 static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1244 switch (tport->tport_proto_id) {
1245 case SCSI_PROTOCOL_SAS:
1247 case SCSI_PROTOCOL_FCP:
1249 case SCSI_PROTOCOL_ISCSI:
1258 static u8 scsiback_get_fabric_proto_ident(struct se_portal_group *se_tpg)
1260 struct scsiback_tpg *tpg = container_of(se_tpg,
1261 struct scsiback_tpg, se_tpg);
1262 struct scsiback_tport *tport = tpg->tport;
1264 switch (tport->tport_proto_id) {
1265 case SCSI_PROTOCOL_SAS:
1266 return sas_get_fabric_proto_ident(se_tpg);
1267 case SCSI_PROTOCOL_FCP:
1268 return fc_get_fabric_proto_ident(se_tpg);
1269 case SCSI_PROTOCOL_ISCSI:
1270 return iscsi_get_fabric_proto_ident(se_tpg);
1272 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1273 tport->tport_proto_id);
1277 return sas_get_fabric_proto_ident(se_tpg);
1280 static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1282 struct scsiback_tpg *tpg = container_of(se_tpg,
1283 struct scsiback_tpg, se_tpg);
1284 struct scsiback_tport *tport = tpg->tport;
1286 return &tport->tport_name[0];
1289 static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1291 struct scsiback_tpg *tpg = container_of(se_tpg,
1292 struct scsiback_tpg, se_tpg);
1293 return tpg->tport_tpgt;
1296 static u32 scsiback_get_default_depth(struct se_portal_group *se_tpg)
1302 scsiback_get_pr_transport_id(struct se_portal_group *se_tpg,
1303 struct se_node_acl *se_nacl,
1304 struct t10_pr_registration *pr_reg,
1308 struct scsiback_tpg *tpg = container_of(se_tpg,
1309 struct scsiback_tpg, se_tpg);
1310 struct scsiback_tport *tport = tpg->tport;
1312 switch (tport->tport_proto_id) {
1313 case SCSI_PROTOCOL_SAS:
1314 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1316 case SCSI_PROTOCOL_FCP:
1317 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1319 case SCSI_PROTOCOL_ISCSI:
1320 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1323 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1324 tport->tport_proto_id);
1328 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1333 scsiback_get_pr_transport_id_len(struct se_portal_group *se_tpg,
1334 struct se_node_acl *se_nacl,
1335 struct t10_pr_registration *pr_reg,
1338 struct scsiback_tpg *tpg = container_of(se_tpg,
1339 struct scsiback_tpg, se_tpg);
1340 struct scsiback_tport *tport = tpg->tport;
1342 switch (tport->tport_proto_id) {
1343 case SCSI_PROTOCOL_SAS:
1344 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1346 case SCSI_PROTOCOL_FCP:
1347 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1349 case SCSI_PROTOCOL_ISCSI:
1350 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1353 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1354 tport->tport_proto_id);
1358 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1363 scsiback_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
1366 char **port_nexus_ptr)
1368 struct scsiback_tpg *tpg = container_of(se_tpg,
1369 struct scsiback_tpg, se_tpg);
1370 struct scsiback_tport *tport = tpg->tport;
1372 switch (tport->tport_proto_id) {
1373 case SCSI_PROTOCOL_SAS:
1374 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1376 case SCSI_PROTOCOL_FCP:
1377 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1379 case SCSI_PROTOCOL_ISCSI:
1380 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1383 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1384 tport->tport_proto_id);
1388 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1392 static struct se_wwn *
1393 scsiback_make_tport(struct target_fabric_configfs *tf,
1394 struct config_group *group,
1397 struct scsiback_tport *tport;
1402 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1404 return ERR_PTR(-ENOMEM);
1406 tport->tport_wwpn = wwpn;
1408 * Determine the emulated Protocol Identifier and Target Port Name
1409 * based on the incoming configfs directory name.
1411 ptr = strstr(name, "naa.");
1413 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1416 ptr = strstr(name, "fc.");
1418 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1419 off = 3; /* Skip over "fc." */
1422 ptr = strstr(name, "iqn.");
1424 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1428 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1430 return ERR_PTR(-EINVAL);
1433 if (strlen(name) >= VSCSI_NAMELEN) {
1434 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1435 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1437 return ERR_PTR(-EINVAL);
1439 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1441 pr_debug("Allocated emulated Target %s Address: %s\n",
1442 scsiback_dump_proto_id(tport), name);
1444 return &tport->tport_wwn;
1447 static void scsiback_drop_tport(struct se_wwn *wwn)
1449 struct scsiback_tport *tport = container_of(wwn,
1450 struct scsiback_tport, tport_wwn);
1452 pr_debug("Deallocating emulated Target %s Address: %s\n",
1453 scsiback_dump_proto_id(tport), tport->tport_name);
1458 static struct se_node_acl *
1459 scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
1461 return kzalloc(sizeof(struct se_node_acl), GFP_KERNEL);
1465 scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
1466 struct se_node_acl *se_nacl)
1471 static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1476 static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1479 * Do not release struct se_cmd's containing a valid TMR pointer.
1480 * These will be released directly in scsiback_device_action()
1481 * with transport_generic_free_cmd().
1483 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
1486 transport_generic_free_cmd(se_cmd, 0);
1490 static void scsiback_release_cmd(struct se_cmd *se_cmd)
1492 struct vscsibk_pend *pending_req = container_of(se_cmd,
1493 struct vscsibk_pend, se_cmd);
1495 kmem_cache_free(scsiback_cachep, pending_req);
1498 static int scsiback_shutdown_session(struct se_session *se_sess)
1503 static void scsiback_close_session(struct se_session *se_sess)
1507 static u32 scsiback_sess_get_index(struct se_session *se_sess)
1512 static int scsiback_write_pending(struct se_cmd *se_cmd)
1514 /* Go ahead and process the write immediately */
1515 target_execute_cmd(se_cmd);
1520 static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1525 static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1529 static u32 scsiback_get_task_tag(struct se_cmd *se_cmd)
1531 struct vscsibk_pend *pending_req = container_of(se_cmd,
1532 struct vscsibk_pend, se_cmd);
1534 return pending_req->rqid;
1537 static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1542 static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1544 struct vscsibk_pend *pending_req = container_of(se_cmd,
1545 struct vscsibk_pend, se_cmd);
1547 pending_req->result = SAM_STAT_GOOD;
1548 scsiback_cmd_done(pending_req);
1552 static int scsiback_queue_status(struct se_cmd *se_cmd)
1554 struct vscsibk_pend *pending_req = container_of(se_cmd,
1555 struct vscsibk_pend, se_cmd);
1557 if (se_cmd->sense_buffer &&
1558 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1559 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1560 pending_req->result = (DRIVER_SENSE << 24) |
1561 SAM_STAT_CHECK_CONDITION;
1563 pending_req->result = se_cmd->scsi_status;
1565 scsiback_cmd_done(pending_req);
1569 static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1571 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1572 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1574 atomic_set(&tmr->tmr_complete, 1);
1575 wake_up(&tmr->tmr_wait);
1578 static void scsiback_aborted_task(struct se_cmd *se_cmd)
1582 static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg,
1585 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1589 mutex_lock(&tpg->tv_tpg_mutex);
1590 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1591 mutex_unlock(&tpg->tv_tpg_mutex);
1596 static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
1597 const char *page, size_t count)
1599 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1603 if (strlen(page) >= VSCSI_NAMELEN) {
1604 pr_err("param alias: %s, exceeds max: %d\n", page,
1609 mutex_lock(&tpg->tv_tpg_mutex);
1610 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1611 if (tpg->param_alias[len - 1] == '\n')
1612 tpg->param_alias[len - 1] = '\0';
1613 mutex_unlock(&tpg->tv_tpg_mutex);
1618 TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
1620 static struct configfs_attribute *scsiback_param_attrs[] = {
1621 &scsiback_tpg_param_alias.attr,
1625 static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1628 struct se_portal_group *se_tpg;
1629 struct se_session *se_sess;
1630 struct scsiback_nexus *tv_nexus;
1632 mutex_lock(&tpg->tv_tpg_mutex);
1633 if (tpg->tpg_nexus) {
1634 mutex_unlock(&tpg->tv_tpg_mutex);
1635 pr_debug("tpg->tpg_nexus already exists\n");
1638 se_tpg = &tpg->se_tpg;
1640 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1642 mutex_unlock(&tpg->tv_tpg_mutex);
1646 * Initialize the struct se_session pointer
1648 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1649 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1650 mutex_unlock(&tpg->tv_tpg_mutex);
1654 se_sess = tv_nexus->tvn_se_sess;
1656 * Since we are running in 'demo mode' this call with generate a
1657 * struct se_node_acl for the scsiback struct se_portal_group with
1658 * the SCSI Initiator port name of the passed configfs group 'name'.
1660 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1661 se_tpg, (unsigned char *)name);
1662 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1663 mutex_unlock(&tpg->tv_tpg_mutex);
1664 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1668 /* Now register the TCM pvscsi virtual I_T Nexus as active. */
1669 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1670 tv_nexus->tvn_se_sess, tv_nexus);
1671 tpg->tpg_nexus = tv_nexus;
1673 mutex_unlock(&tpg->tv_tpg_mutex);
1677 transport_free_session(se_sess);
1682 static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1684 struct se_session *se_sess;
1685 struct scsiback_nexus *tv_nexus;
1687 mutex_lock(&tpg->tv_tpg_mutex);
1688 tv_nexus = tpg->tpg_nexus;
1690 mutex_unlock(&tpg->tv_tpg_mutex);
1694 se_sess = tv_nexus->tvn_se_sess;
1696 mutex_unlock(&tpg->tv_tpg_mutex);
1700 if (tpg->tv_tpg_port_count != 0) {
1701 mutex_unlock(&tpg->tv_tpg_mutex);
1702 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1703 tpg->tv_tpg_port_count);
1707 if (tpg->tv_tpg_fe_count != 0) {
1708 mutex_unlock(&tpg->tv_tpg_mutex);
1709 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1710 tpg->tv_tpg_fe_count);
1714 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1715 scsiback_dump_proto_id(tpg->tport),
1716 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1719 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1721 transport_deregister_session(tv_nexus->tvn_se_sess);
1722 tpg->tpg_nexus = NULL;
1723 mutex_unlock(&tpg->tv_tpg_mutex);
1729 static ssize_t scsiback_tpg_show_nexus(struct se_portal_group *se_tpg,
1732 struct scsiback_tpg *tpg = container_of(se_tpg,
1733 struct scsiback_tpg, se_tpg);
1734 struct scsiback_nexus *tv_nexus;
1737 mutex_lock(&tpg->tv_tpg_mutex);
1738 tv_nexus = tpg->tpg_nexus;
1740 mutex_unlock(&tpg->tv_tpg_mutex);
1743 ret = snprintf(page, PAGE_SIZE, "%s\n",
1744 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1745 mutex_unlock(&tpg->tv_tpg_mutex);
1750 static ssize_t scsiback_tpg_store_nexus(struct se_portal_group *se_tpg,
1754 struct scsiback_tpg *tpg = container_of(se_tpg,
1755 struct scsiback_tpg, se_tpg);
1756 struct scsiback_tport *tport_wwn = tpg->tport;
1757 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1760 * Shutdown the active I_T nexus if 'NULL' is passed.
1762 if (!strncmp(page, "NULL", 4)) {
1763 ret = scsiback_drop_nexus(tpg);
1764 return (!ret) ? count : ret;
1767 * Otherwise make sure the passed virtual Initiator port WWN matches
1768 * the fabric protocol_id set in scsiback_make_tport(), and call
1769 * scsiback_make_nexus().
1771 if (strlen(page) >= VSCSI_NAMELEN) {
1772 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1773 page, VSCSI_NAMELEN);
1776 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1778 ptr = strstr(i_port, "naa.");
1780 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1781 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1782 i_port, scsiback_dump_proto_id(tport_wwn));
1785 port_ptr = &i_port[0];
1788 ptr = strstr(i_port, "fc.");
1790 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1791 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1792 i_port, scsiback_dump_proto_id(tport_wwn));
1795 port_ptr = &i_port[3]; /* Skip over "fc." */
1798 ptr = strstr(i_port, "iqn.");
1800 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1801 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1802 i_port, scsiback_dump_proto_id(tport_wwn));
1805 port_ptr = &i_port[0];
1808 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1812 * Clear any trailing newline for the NAA WWN
1815 if (i_port[strlen(i_port) - 1] == '\n')
1816 i_port[strlen(i_port) - 1] = '\0';
1818 ret = scsiback_make_nexus(tpg, port_ptr);
1825 TF_TPG_BASE_ATTR(scsiback, nexus, S_IRUGO | S_IWUSR);
1827 static struct configfs_attribute *scsiback_tpg_attrs[] = {
1828 &scsiback_tpg_nexus.attr,
1833 scsiback_wwn_show_attr_version(struct target_fabric_configfs *tf,
1836 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1838 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1841 TF_WWN_ATTR_RO(scsiback, version);
1843 static struct configfs_attribute *scsiback_wwn_attrs[] = {
1844 &scsiback_wwn_version.attr,
1848 static char *scsiback_get_fabric_name(void)
1850 return "xen-pvscsi";
1853 static int scsiback_port_link(struct se_portal_group *se_tpg,
1856 struct scsiback_tpg *tpg = container_of(se_tpg,
1857 struct scsiback_tpg, se_tpg);
1859 mutex_lock(&tpg->tv_tpg_mutex);
1860 tpg->tv_tpg_port_count++;
1861 mutex_unlock(&tpg->tv_tpg_mutex);
1866 static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1869 struct scsiback_tpg *tpg = container_of(se_tpg,
1870 struct scsiback_tpg, se_tpg);
1872 mutex_lock(&tpg->tv_tpg_mutex);
1873 tpg->tv_tpg_port_count--;
1874 mutex_unlock(&tpg->tv_tpg_mutex);
1877 static struct se_portal_group *
1878 scsiback_make_tpg(struct se_wwn *wwn,
1879 struct config_group *group,
1882 struct scsiback_tport *tport = container_of(wwn,
1883 struct scsiback_tport, tport_wwn);
1885 struct scsiback_tpg *tpg;
1889 if (strstr(name, "tpgt_") != name)
1890 return ERR_PTR(-EINVAL);
1891 ret = kstrtou16(name + 5, 10, &tpgt);
1893 return ERR_PTR(ret);
1895 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1897 return ERR_PTR(-ENOMEM);
1899 mutex_init(&tpg->tv_tpg_mutex);
1900 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1901 INIT_LIST_HEAD(&tpg->info_list);
1903 tpg->tport_tpgt = tpgt;
1905 ret = core_tpg_register(&scsiback_fabric_configfs->tf_ops, wwn,
1906 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1911 mutex_lock(&scsiback_mutex);
1912 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1913 mutex_unlock(&scsiback_mutex);
1915 return &tpg->se_tpg;
1918 static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1920 struct scsiback_tpg *tpg = container_of(se_tpg,
1921 struct scsiback_tpg, se_tpg);
1923 mutex_lock(&scsiback_mutex);
1924 list_del(&tpg->tv_tpg_list);
1925 mutex_unlock(&scsiback_mutex);
1927 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1929 scsiback_drop_nexus(tpg);
1931 * Deregister the se_tpg from TCM.
1933 core_tpg_deregister(se_tpg);
1937 static int scsiback_check_true(struct se_portal_group *se_tpg)
1942 static int scsiback_check_false(struct se_portal_group *se_tpg)
1947 static struct target_core_fabric_ops scsiback_ops = {
1948 .get_fabric_name = scsiback_get_fabric_name,
1949 .get_fabric_proto_ident = scsiback_get_fabric_proto_ident,
1950 .tpg_get_wwn = scsiback_get_fabric_wwn,
1951 .tpg_get_tag = scsiback_get_tag,
1952 .tpg_get_default_depth = scsiback_get_default_depth,
1953 .tpg_get_pr_transport_id = scsiback_get_pr_transport_id,
1954 .tpg_get_pr_transport_id_len = scsiback_get_pr_transport_id_len,
1955 .tpg_parse_pr_out_transport_id = scsiback_parse_pr_out_transport_id,
1956 .tpg_check_demo_mode = scsiback_check_true,
1957 .tpg_check_demo_mode_cache = scsiback_check_true,
1958 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1959 .tpg_check_prod_mode_write_protect = scsiback_check_false,
1960 .tpg_alloc_fabric_acl = scsiback_alloc_fabric_acl,
1961 .tpg_release_fabric_acl = scsiback_release_fabric_acl,
1962 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1963 .check_stop_free = scsiback_check_stop_free,
1964 .release_cmd = scsiback_release_cmd,
1965 .put_session = NULL,
1966 .shutdown_session = scsiback_shutdown_session,
1967 .close_session = scsiback_close_session,
1968 .sess_get_index = scsiback_sess_get_index,
1969 .sess_get_initiator_sid = NULL,
1970 .write_pending = scsiback_write_pending,
1971 .write_pending_status = scsiback_write_pending_status,
1972 .set_default_node_attributes = scsiback_set_default_node_attrs,
1973 .get_task_tag = scsiback_get_task_tag,
1974 .get_cmd_state = scsiback_get_cmd_state,
1975 .queue_data_in = scsiback_queue_data_in,
1976 .queue_status = scsiback_queue_status,
1977 .queue_tm_rsp = scsiback_queue_tm_rsp,
1978 .aborted_task = scsiback_aborted_task,
1980 * Setup callers for generic logic in target_core_fabric_configfs.c
1982 .fabric_make_wwn = scsiback_make_tport,
1983 .fabric_drop_wwn = scsiback_drop_tport,
1984 .fabric_make_tpg = scsiback_make_tpg,
1985 .fabric_drop_tpg = scsiback_drop_tpg,
1986 .fabric_post_link = scsiback_port_link,
1987 .fabric_pre_unlink = scsiback_port_unlink,
1988 .fabric_make_np = NULL,
1989 .fabric_drop_np = NULL,
1991 .fabric_make_nodeacl = scsiback_make_nodeacl,
1992 .fabric_drop_nodeacl = scsiback_drop_nodeacl,
1996 static int scsiback_register_configfs(void)
1998 struct target_fabric_configfs *fabric;
2001 pr_debug("fabric module %s on %s/%s on "UTS_RELEASE"\n",
2002 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
2004 * Register the top level struct config_item_type with TCM core
2006 fabric = target_fabric_configfs_init(THIS_MODULE, "xen-pvscsi");
2008 return PTR_ERR(fabric);
2011 * Setup fabric->tf_ops from our local scsiback_ops
2013 fabric->tf_ops = scsiback_ops;
2015 * Setup default attribute lists for various fabric->tf_cit_tmpl
2017 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = scsiback_wwn_attrs;
2018 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = scsiback_tpg_attrs;
2019 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
2020 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = scsiback_param_attrs;
2021 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
2022 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2023 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2024 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2025 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
2027 * Register the fabric for use within TCM
2029 ret = target_fabric_configfs_register(fabric);
2031 target_fabric_configfs_free(fabric);
2035 * Setup our local pointer to *fabric
2037 scsiback_fabric_configfs = fabric;
2038 pr_debug("Set fabric -> scsiback_fabric_configfs\n");
2042 static void scsiback_deregister_configfs(void)
2044 if (!scsiback_fabric_configfs)
2047 target_fabric_configfs_deregister(scsiback_fabric_configfs);
2048 scsiback_fabric_configfs = NULL;
2049 pr_debug("Cleared scsiback_fabric_configfs\n");
2052 static const struct xenbus_device_id scsiback_ids[] = {
2057 static struct xenbus_driver scsiback_driver = {
2058 .ids = scsiback_ids,
2059 .probe = scsiback_probe,
2060 .remove = scsiback_remove,
2061 .otherend_changed = scsiback_frontend_changed
2064 static void scsiback_init_pend(void *p)
2066 struct vscsibk_pend *pend = p;
2069 memset(pend, 0, sizeof(*pend));
2070 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
2071 pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
2074 static int __init scsiback_init(void)
2081 scsiback_cachep = kmem_cache_create("vscsiif_cache",
2082 sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend);
2083 if (!scsiback_cachep)
2086 ret = xenbus_register_backend(&scsiback_driver);
2088 goto out_cache_destroy;
2090 ret = scsiback_register_configfs();
2092 goto out_unregister_xenbus;
2096 out_unregister_xenbus:
2097 xenbus_unregister_driver(&scsiback_driver);
2099 kmem_cache_destroy(scsiback_cachep);
2100 pr_err("%s: error %d\n", __func__, ret);
2104 static void __exit scsiback_exit(void)
2108 while (free_pages_num) {
2109 if (get_free_page(&page))
2111 gnttab_free_pages(1, &page);
2113 scsiback_deregister_configfs();
2114 xenbus_unregister_driver(&scsiback_driver);
2115 kmem_cache_destroy(scsiback_cachep);
2118 module_init(scsiback_init);
2119 module_exit(scsiback_exit);
2121 MODULE_DESCRIPTION("Xen SCSI backend driver");
2122 MODULE_LICENSE("Dual BSD/GPL");
2123 MODULE_ALIAS("xen-backend:vscsi");
2124 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");