2 * Xen SCSI backend driver
4 * Copyright (c) 2008, FUJITSU Limited
6 * Based on the blkback driver code.
7 * Adaption to kernel taget core infrastructure taken from vhost/scsi.c
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 #define pr_fmt(fmt) "xen-pvscsi: " fmt
38 #include <linux/module.h>
39 #include <linux/utsname.h>
40 #include <linux/interrupt.h>
41 #include <linux/slab.h>
42 #include <linux/wait.h>
43 #include <linux/sched.h>
44 #include <linux/list.h>
45 #include <linux/gfp.h>
46 #include <linux/delay.h>
47 #include <linux/spinlock.h>
48 #include <linux/configfs.h>
50 #include <generated/utsrelease.h>
52 #include <scsi/scsi_host.h> /* SG_ALL */
54 #include <target/target_core_base.h>
55 #include <target/target_core_fabric.h>
57 #include <asm/hypervisor.h>
60 #include <xen/balloon.h>
61 #include <xen/events.h>
62 #include <xen/xenbus.h>
63 #include <xen/grant_table.h>
66 #include <xen/interface/grant_table.h>
67 #include <xen/interface/io/vscsiif.h>
69 #define VSCSI_VERSION "v0.1"
70 #define VSCSI_NAMELEN 32
73 unsigned int hst; /* host */
74 unsigned int chn; /* channel */
75 unsigned int tgt; /* target */
76 unsigned int lun; /* LUN */
80 struct ids_tuple v; /* translate from */
81 struct scsiback_tpg *tpg; /* translate to */
88 struct xenbus_device *dev;
93 struct vscsiif_back_ring ring;
97 atomic_t nr_unreplied_reqs;
100 struct list_head v2p_entry_lists;
102 wait_queue_head_t waiting_to_free;
105 /* theoretical maximum of grants for one request */
106 #define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
109 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
110 * call to map/unmap grants. Don't choose it too large, as there are arrays
111 * with VSCSI_GRANT_BATCH elements allocated on the stack.
113 #define VSCSI_GRANT_BATCH 16
115 struct vscsibk_pend {
118 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
121 uint8_t sc_data_direction;
122 uint16_t n_sg; /* real length of SG list */
123 uint16_t n_grants; /* SG pages and potentially SG list */
127 struct vscsibk_info *info;
128 struct v2p_entry *v2p;
129 struct scatterlist *sgl;
131 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
133 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
134 struct page *pages[VSCSI_MAX_GRANTS];
136 struct se_cmd se_cmd;
139 struct scsiback_tmr {
140 atomic_t tmr_complete;
141 wait_queue_head_t tmr_wait;
144 #define VSCSI_DEFAULT_SESSION_TAGS 128
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 DEFINE_SPINLOCK(free_pages_lock);
196 static int free_pages_num;
197 static LIST_HEAD(scsiback_free_pages);
199 /* Global spinlock to protect scsiback TPG list */
200 static DEFINE_MUTEX(scsiback_mutex);
201 static LIST_HEAD(scsiback_list);
203 static void scsiback_get(struct vscsibk_info *info)
205 atomic_inc(&info->nr_unreplied_reqs);
208 static void scsiback_put(struct vscsibk_info *info)
210 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
211 wake_up(&info->waiting_to_free);
214 static void put_free_pages(struct page **page, int num)
217 int i = free_pages_num + num, n = num;
221 if (i > scsiback_max_buffer_pages) {
222 n = min(num, i - scsiback_max_buffer_pages);
223 gnttab_free_pages(n, page + num - n);
226 spin_lock_irqsave(&free_pages_lock, flags);
227 for (i = 0; i < n; i++)
228 list_add(&page[i]->lru, &scsiback_free_pages);
230 spin_unlock_irqrestore(&free_pages_lock, flags);
233 static int get_free_page(struct page **page)
237 spin_lock_irqsave(&free_pages_lock, flags);
238 if (list_empty(&scsiback_free_pages)) {
239 spin_unlock_irqrestore(&free_pages_lock, flags);
240 return gnttab_alloc_pages(1, page);
242 page[0] = list_first_entry(&scsiback_free_pages, struct page, lru);
243 list_del(&page[0]->lru);
245 spin_unlock_irqrestore(&free_pages_lock, flags);
249 static unsigned long vaddr_page(struct page *page)
251 unsigned long pfn = page_to_pfn(page);
253 return (unsigned long)pfn_to_kaddr(pfn);
256 static unsigned long vaddr(struct vscsibk_pend *req, int seg)
258 return vaddr_page(req->pages[seg]);
261 static void scsiback_print_status(char *sense_buffer, int errors,
262 struct vscsibk_pend *pending_req)
264 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
266 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
267 tpg->tport->tport_name, pending_req->v2p->lun,
268 pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
269 host_byte(errors), driver_byte(errors));
272 static void scsiback_fast_flush_area(struct vscsibk_pend *req)
274 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
275 struct page *pages[VSCSI_GRANT_BATCH];
276 unsigned int i, invcount = 0;
277 grant_handle_t handle;
287 for (i = 0; i < req->n_grants; i++) {
288 handle = req->grant_handles[i];
289 if (handle == SCSIBACK_INVALID_HANDLE)
291 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
292 GNTMAP_host_map, handle);
293 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
294 pages[invcount] = req->pages[i];
295 put_page(pages[invcount]);
297 if (invcount < VSCSI_GRANT_BATCH)
299 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
305 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
309 put_free_pages(req->pages, req->n_grants);
313 static void scsiback_free_translation_entry(struct kref *kref)
315 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
316 struct scsiback_tpg *tpg = entry->tpg;
318 mutex_lock(&tpg->tv_tpg_mutex);
319 tpg->tv_tpg_fe_count--;
320 mutex_unlock(&tpg->tv_tpg_mutex);
325 static void scsiback_send_response(struct vscsibk_info *info,
326 char *sense_buffer, int32_t result, uint32_t resid,
329 struct vscsiif_response *ring_res;
331 struct scsi_sense_hdr sshdr;
335 spin_lock_irqsave(&info->ring_lock, flags);
337 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
338 info->ring.rsp_prod_pvt++;
340 ring_res->rslt = result;
341 ring_res->rqid = rqid;
343 if (sense_buffer != NULL &&
344 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
346 len = min_t(unsigned, 8 + sense_buffer[7],
347 VSCSIIF_SENSE_BUFFERSIZE);
348 memcpy(ring_res->sense_buffer, sense_buffer, len);
349 ring_res->sense_len = len;
351 ring_res->sense_len = 0;
354 ring_res->residual_len = resid;
356 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
357 spin_unlock_irqrestore(&info->ring_lock, flags);
360 notify_remote_via_irq(info->irq);
363 static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
364 uint32_t resid, struct vscsibk_pend *pending_req)
366 scsiback_send_response(pending_req->info, sense_buffer, result,
367 resid, pending_req->rqid);
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);
392 * Drop the extra KREF_ACK reference taken by target_submit_cmd_map_sgls()
393 * ahead of scsiback_check_stop_free() -> transport_generic_free_cmd()
394 * final se_cmd->cmd_kref put.
396 target_put_sess_cmd(&pending_req->se_cmd);
399 static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
401 struct se_cmd *se_cmd = &pending_req->se_cmd;
402 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
405 scsiback_get(pending_req->info);
406 se_cmd->tag = pending_req->rqid;
407 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
408 pending_req->sense_buffer, pending_req->v2p->lun,
409 pending_req->data_len, 0,
410 pending_req->sc_data_direction, TARGET_SCF_ACK_KREF,
411 pending_req->sgl, pending_req->n_sg,
414 transport_send_check_condition_and_sense(se_cmd,
415 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
416 transport_generic_free_cmd(se_cmd, 0);
420 static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
421 struct page **pg, grant_handle_t *grant, int cnt)
428 err = gnttab_map_refs(map, NULL, pg, cnt);
430 for (i = 0; i < cnt; i++) {
431 if (unlikely(map[i].status != GNTST_okay)) {
432 pr_err("invalid buffer -- could not remap it\n");
433 map[i].handle = SCSIBACK_INVALID_HANDLE;
438 grant[i] = map[i].handle;
443 static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
444 struct scsiif_request_segment *seg, struct page **pg,
445 grant_handle_t *grant, int cnt, u32 flags)
447 int mapcount = 0, i, err = 0;
448 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
449 struct vscsibk_info *info = pending_req->info;
451 for (i = 0; i < cnt; i++) {
452 if (get_free_page(pg + mapcount)) {
453 put_free_pages(pg, mapcount);
454 pr_err("no grant page\n");
457 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
458 flags, seg[i].gref, info->domid);
460 if (mapcount < VSCSI_GRANT_BATCH)
462 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
465 pending_req->n_grants += mapcount;
470 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
471 pending_req->n_grants += mapcount;
475 static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
476 struct vscsibk_pend *pending_req)
479 int i, err, n_segs, i_seg = 0;
481 struct scsiif_request_segment *seg;
482 unsigned long end_seg = 0;
483 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
484 unsigned int nr_sgl = 0;
485 struct scatterlist *sg;
486 grant_handle_t *grant;
488 pending_req->n_sg = 0;
489 pending_req->n_grants = 0;
490 pending_req->data_len = 0;
492 nr_segments &= ~VSCSIIF_SG_GRANT;
496 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
497 pr_debug("invalid parameter nr_seg = %d\n",
498 ring_req->nr_segments);
502 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
503 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
504 pending_req->pages, pending_req->grant_handles,
505 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
508 nr_sgl = nr_segments;
510 for (i = 0; i < nr_sgl; i++) {
511 n_segs = ring_req->seg[i].length /
512 sizeof(struct scsiif_request_segment);
513 if ((unsigned)ring_req->seg[i].offset +
514 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
515 n_segs * sizeof(struct scsiif_request_segment) !=
516 ring_req->seg[i].length)
518 nr_segments += n_segs;
520 if (nr_segments > SG_ALL) {
521 pr_debug("invalid nr_seg = %d\n", nr_segments);
526 /* free of (sgl) in fast_flush_area() */
527 pending_req->sgl = kmalloc_array(nr_segments,
528 sizeof(struct scatterlist), GFP_KERNEL);
529 if (!pending_req->sgl)
532 sg_init_table(pending_req->sgl, nr_segments);
533 pending_req->n_sg = nr_segments;
535 flags = GNTMAP_host_map;
536 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
537 flags |= GNTMAP_readonly;
539 pg = pending_req->pages + nr_sgl;
540 grant = pending_req->grant_handles + nr_sgl;
543 err = scsiback_gnttab_data_map_list(pending_req, seg,
544 pg, grant, nr_segments, flags);
548 for (i = 0; i < nr_sgl; i++) {
549 seg = (struct scsiif_request_segment *)(
550 vaddr(pending_req, i) + ring_req->seg[i].offset);
551 n_segs = ring_req->seg[i].length /
552 sizeof(struct scsiif_request_segment);
553 err = scsiback_gnttab_data_map_list(pending_req, seg,
554 pg, grant, n_segs, flags);
560 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
561 seg = (struct scsiif_request_segment *)end_seg;
562 end_seg += ring_req->seg[0].length;
563 pg = pending_req->pages + nr_sgl;
566 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
567 sg_set_page(sg, pg[i], seg->length, seg->offset);
568 pending_req->data_len += seg->length;
570 if (nr_sgl && (unsigned long)seg >= end_seg) {
572 end_seg = vaddr(pending_req, i_seg) +
573 ring_req->seg[i_seg].offset;
574 seg = (struct scsiif_request_segment *)end_seg;
575 end_seg += ring_req->seg[i_seg].length;
577 if (sg->offset >= PAGE_SIZE ||
578 sg->length > PAGE_SIZE ||
579 sg->offset + sg->length > PAGE_SIZE)
586 static void scsiback_disconnect(struct vscsibk_info *info)
588 wait_event(info->waiting_to_free,
589 atomic_read(&info->nr_unreplied_reqs) == 0);
591 unbind_from_irqhandler(info->irq, info);
593 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
596 static void scsiback_device_action(struct vscsibk_pend *pending_req,
597 enum tcm_tmreq_table act, int tag)
599 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
600 struct scsiback_nexus *nexus = tpg->tpg_nexus;
601 struct se_cmd *se_cmd = &pending_req->se_cmd;
602 struct scsiback_tmr *tmr;
603 u64 unpacked_lun = pending_req->v2p->lun;
604 int rc, err = FAILED;
606 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
608 target_put_sess_cmd(se_cmd);
612 init_waitqueue_head(&tmr->tmr_wait);
614 rc = target_submit_tmr(&pending_req->se_cmd, nexus->tvn_se_sess,
615 &pending_req->sense_buffer[0],
616 unpacked_lun, tmr, act, GFP_KERNEL,
617 tag, TARGET_SCF_ACK_KREF);
621 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
623 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
626 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
627 transport_generic_free_cmd(&pending_req->se_cmd, 1);
632 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
636 Perform virtual to physical translation
638 static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
641 struct v2p_entry *entry;
642 struct list_head *head = &(info->v2p_entry_lists);
645 spin_lock_irqsave(&info->v2p_lock, flags);
646 list_for_each_entry(entry, head, l) {
647 if ((entry->v.chn == v->chn) &&
648 (entry->v.tgt == v->tgt) &&
649 (entry->v.lun == v->lun)) {
650 kref_get(&entry->kref);
657 spin_unlock_irqrestore(&info->v2p_lock, flags);
661 static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring,
662 struct v2p_entry *v2p)
664 struct scsiback_tpg *tpg = v2p->tpg;
665 struct scsiback_nexus *nexus = tpg->tpg_nexus;
666 struct se_session *se_sess = nexus->tvn_se_sess;
667 struct vscsibk_pend *req;
670 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
672 pr_err("Unable to obtain tag for vscsiif_request\n");
673 return ERR_PTR(-ENOMEM);
676 req = &((struct vscsibk_pend *)se_sess->sess_cmd_map)[tag];
677 memset(req, 0, sizeof(*req));
678 req->se_cmd.map_tag = tag;
680 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
681 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
686 static struct vscsibk_pend *prepare_pending_reqs(struct vscsibk_info *info,
687 struct vscsiif_back_ring *ring,
688 struct vscsiif_request *ring_req)
690 struct vscsibk_pend *pending_req;
691 struct v2p_entry *v2p;
692 struct ids_tuple vir;
694 /* request range check from frontend */
695 if ((ring_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
696 (ring_req->sc_data_direction != DMA_TO_DEVICE) &&
697 (ring_req->sc_data_direction != DMA_FROM_DEVICE) &&
698 (ring_req->sc_data_direction != DMA_NONE)) {
699 pr_debug("invalid parameter data_dir = %d\n",
700 ring_req->sc_data_direction);
701 return ERR_PTR(-EINVAL);
703 if (ring_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
704 pr_debug("invalid parameter cmd_len = %d\n",
706 return ERR_PTR(-EINVAL);
709 vir.chn = ring_req->channel;
710 vir.tgt = ring_req->id;
711 vir.lun = ring_req->lun;
713 v2p = scsiback_do_translation(info, &vir);
715 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
716 vir.chn, vir.tgt, vir.lun);
717 return ERR_PTR(-ENODEV);
720 pending_req = scsiback_get_pend_req(ring, v2p);
721 if (IS_ERR(pending_req)) {
722 kref_put(&v2p->kref, scsiback_free_translation_entry);
723 return ERR_PTR(-ENOMEM);
725 pending_req->rqid = ring_req->rqid;
726 pending_req->info = info;
727 pending_req->v2p = v2p;
728 pending_req->sc_data_direction = ring_req->sc_data_direction;
729 pending_req->cmd_len = ring_req->cmd_len;
730 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
735 static int scsiback_do_cmd_fn(struct vscsibk_info *info)
737 struct vscsiif_back_ring *ring = &info->ring;
738 struct vscsiif_request ring_req;
739 struct vscsibk_pend *pending_req;
745 rp = ring->sring->req_prod;
746 rmb(); /* guest system is accessing ring, too */
748 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
749 rc = ring->rsp_prod_pvt;
750 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
751 info->domid, rp, rc, rp - rc);
752 info->ring_error = 1;
757 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
760 RING_COPY_REQUEST(ring, rc, &ring_req);
761 ring->req_cons = ++rc;
763 pending_req = prepare_pending_reqs(info, ring, &ring_req);
764 if (IS_ERR(pending_req)) {
765 switch (PTR_ERR(pending_req)) {
767 result = DID_NO_CONNECT;
770 result = DRIVER_ERROR;
773 scsiback_send_response(info, NULL, result << 24, 0,
778 switch (ring_req.act) {
779 case VSCSIIF_ACT_SCSI_CDB:
780 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
781 scsiback_fast_flush_area(pending_req);
782 scsiback_do_resp_with_sense(NULL,
783 DRIVER_ERROR << 24, 0, pending_req);
784 transport_generic_free_cmd(&pending_req->se_cmd, 0);
786 scsiback_cmd_exec(pending_req);
789 case VSCSIIF_ACT_SCSI_ABORT:
790 scsiback_device_action(pending_req, TMR_ABORT_TASK,
793 case VSCSIIF_ACT_SCSI_RESET:
794 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
797 pr_err_ratelimited("invalid request\n");
798 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24, 0,
800 transport_generic_free_cmd(&pending_req->se_cmd, 0);
804 /* Yield point for this unbounded loop. */
808 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
812 static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
814 struct vscsibk_info *info = dev_id;
816 if (info->ring_error)
819 while (scsiback_do_cmd_fn(info))
825 static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
826 evtchn_port_t evtchn)
829 struct vscsiif_sring *sring;
835 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
839 sring = (struct vscsiif_sring *)area;
840 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
842 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
848 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
849 IRQF_ONESHOT, "vscsiif-backend", info);
856 unbind_from_irqhandler(info->irq, info);
859 xenbus_unmap_ring_vfree(info->dev, area);
864 static int scsiback_map(struct vscsibk_info *info)
866 struct xenbus_device *dev = info->dev;
867 unsigned int ring_ref, evtchn;
870 err = xenbus_gather(XBT_NIL, dev->otherend,
871 "ring-ref", "%u", &ring_ref,
872 "event-channel", "%u", &evtchn, NULL);
874 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
878 return scsiback_init_sring(info, ring_ref, evtchn);
882 Check for a translation entry being present
884 static struct v2p_entry *scsiback_chk_translation_entry(
885 struct vscsibk_info *info, struct ids_tuple *v)
887 struct list_head *head = &(info->v2p_entry_lists);
888 struct v2p_entry *entry;
890 list_for_each_entry(entry, head, l)
891 if ((entry->v.chn == v->chn) &&
892 (entry->v.tgt == v->tgt) &&
893 (entry->v.lun == v->lun))
900 Add a new translation entry
902 static int scsiback_add_translation_entry(struct vscsibk_info *info,
903 char *phy, struct ids_tuple *v)
906 struct v2p_entry *new;
909 unsigned long long unpacked_lun;
910 struct se_lun *se_lun;
911 struct scsiback_tpg *tpg_entry, *tpg = NULL;
912 char *error = "doesn't exist";
914 lunp = strrchr(phy, ':');
916 pr_err("illegal format of physical device %s\n", phy);
921 err = kstrtoull(lunp, 10, &unpacked_lun);
923 pr_err("lun number not valid: %s\n", lunp);
927 mutex_lock(&scsiback_mutex);
928 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
929 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
930 !strcmp(phy, tpg_entry->param_alias)) {
931 mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
932 hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
933 if (se_lun->unpacked_lun == unpacked_lun) {
934 if (!tpg_entry->tpg_nexus)
935 error = "nexus undefined";
941 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
946 mutex_lock(&tpg->tv_tpg_mutex);
947 tpg->tv_tpg_fe_count++;
948 mutex_unlock(&tpg->tv_tpg_mutex);
950 mutex_unlock(&scsiback_mutex);
953 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
957 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
963 spin_lock_irqsave(&info->v2p_lock, flags);
965 /* Check double assignment to identical virtual ID */
966 if (scsiback_chk_translation_entry(info, v)) {
967 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
972 /* Create a new translation entry and add to the list */
973 kref_init(&new->kref);
976 new->lun = unpacked_lun;
977 list_add_tail(&new->l, &info->v2p_entry_lists);
980 spin_unlock_irqrestore(&info->v2p_lock, flags);
984 mutex_lock(&tpg->tv_tpg_mutex);
985 tpg->tv_tpg_fe_count--;
986 mutex_unlock(&tpg->tv_tpg_mutex);
993 static void __scsiback_del_translation_entry(struct v2p_entry *entry)
996 kref_put(&entry->kref, scsiback_free_translation_entry);
1000 Delete the translation entry specified
1002 static int scsiback_del_translation_entry(struct vscsibk_info *info,
1003 struct ids_tuple *v)
1005 struct v2p_entry *entry;
1006 unsigned long flags;
1009 spin_lock_irqsave(&info->v2p_lock, flags);
1010 /* Find out the translation entry specified */
1011 entry = scsiback_chk_translation_entry(info, v);
1013 __scsiback_del_translation_entry(entry);
1017 spin_unlock_irqrestore(&info->v2p_lock, flags);
1021 static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
1022 char *phy, struct ids_tuple *vir, int try)
1024 struct v2p_entry *entry;
1025 unsigned long flags;
1028 spin_lock_irqsave(&info->v2p_lock, flags);
1029 entry = scsiback_chk_translation_entry(info, vir);
1030 spin_unlock_irqrestore(&info->v2p_lock, flags);
1034 if (!scsiback_add_translation_entry(info, phy, vir)) {
1035 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1036 "%d", XenbusStateInitialised)) {
1037 pr_err("xenbus_printf error %s\n", state);
1038 scsiback_del_translation_entry(info, vir);
1041 xenbus_printf(XBT_NIL, info->dev->nodename, state,
1042 "%d", XenbusStateClosed);
1046 static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1047 struct ids_tuple *vir)
1049 if (!scsiback_del_translation_entry(info, vir)) {
1050 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1051 "%d", XenbusStateClosed))
1052 pr_err("xenbus_printf error %s\n", state);
1056 #define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1057 #define VSCSIBACK_OP_UPDATEDEV_STATE 2
1059 static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1063 struct ids_tuple vir;
1066 char phy[VSCSI_NAMELEN];
1069 struct xenbus_device *dev = info->dev;
1072 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1073 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1074 if (XENBUS_EXIST_ERR(err))
1077 /* physical SCSI device */
1078 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1079 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1081 xenbus_printf(XBT_NIL, dev->nodename, state,
1082 "%d", XenbusStateClosed);
1085 strlcpy(phy, val, VSCSI_NAMELEN);
1088 /* virtual SCSI device */
1089 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1090 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1091 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1092 if (XENBUS_EXIST_ERR(err)) {
1093 xenbus_printf(XBT_NIL, dev->nodename, state,
1094 "%d", XenbusStateClosed);
1099 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
1100 switch (device_state) {
1101 case XenbusStateInitialising:
1102 scsiback_do_add_lun(info, state, phy, &vir, 0);
1104 case XenbusStateConnected:
1105 scsiback_do_add_lun(info, state, phy, &vir, 1);
1107 case XenbusStateClosing:
1108 scsiback_do_del_lun(info, state, &vir);
1115 case VSCSIBACK_OP_UPDATEDEV_STATE:
1116 if (device_state == XenbusStateInitialised) {
1117 /* modify vscsi-devs/dev-x/state */
1118 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1119 "%d", XenbusStateConnected)) {
1120 pr_err("xenbus_printf error %s\n", str);
1121 scsiback_del_translation_entry(info, &vir);
1122 xenbus_printf(XBT_NIL, dev->nodename, state,
1123 "%d", XenbusStateClosed);
1127 /* When it is necessary, processing is added here. */
1133 static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1137 unsigned int ndir = 0;
1139 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1144 for (i = 0; i < ndir; i++)
1145 scsiback_do_1lun_hotplug(info, op, dir[i]);
1150 static void scsiback_frontend_changed(struct xenbus_device *dev,
1151 enum xenbus_state frontend_state)
1153 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1155 switch (frontend_state) {
1156 case XenbusStateInitialising:
1159 case XenbusStateInitialised:
1160 if (scsiback_map(info))
1163 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1164 xenbus_switch_state(dev, XenbusStateConnected);
1167 case XenbusStateConnected:
1168 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1170 if (dev->state == XenbusStateConnected)
1173 xenbus_switch_state(dev, XenbusStateConnected);
1176 case XenbusStateClosing:
1178 scsiback_disconnect(info);
1180 xenbus_switch_state(dev, XenbusStateClosing);
1183 case XenbusStateClosed:
1184 xenbus_switch_state(dev, XenbusStateClosed);
1185 if (xenbus_dev_is_online(dev))
1187 /* fall through if not online */
1188 case XenbusStateUnknown:
1189 device_unregister(&dev->dev);
1192 case XenbusStateReconfiguring:
1193 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1194 xenbus_switch_state(dev, XenbusStateReconfigured);
1199 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1206 Release the translation entry specfied
1208 static void scsiback_release_translation_entry(struct vscsibk_info *info)
1210 struct v2p_entry *entry, *tmp;
1211 struct list_head *head = &(info->v2p_entry_lists);
1212 unsigned long flags;
1214 spin_lock_irqsave(&info->v2p_lock, flags);
1216 list_for_each_entry_safe(entry, tmp, head, l)
1217 __scsiback_del_translation_entry(entry);
1219 spin_unlock_irqrestore(&info->v2p_lock, flags);
1222 static int scsiback_remove(struct xenbus_device *dev)
1224 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1227 scsiback_disconnect(info);
1229 scsiback_release_translation_entry(info);
1231 dev_set_drvdata(&dev->dev, NULL);
1236 static int scsiback_probe(struct xenbus_device *dev,
1237 const struct xenbus_device_id *id)
1241 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1244 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
1247 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1251 dev_set_drvdata(&dev->dev, info);
1253 info->domid = dev->otherend_id;
1254 spin_lock_init(&info->ring_lock);
1255 info->ring_error = 0;
1256 atomic_set(&info->nr_unreplied_reqs, 0);
1257 init_waitqueue_head(&info->waiting_to_free);
1260 INIT_LIST_HEAD(&info->v2p_entry_lists);
1261 spin_lock_init(&info->v2p_lock);
1263 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1266 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1268 err = xenbus_switch_state(dev, XenbusStateInitWait);
1275 pr_warn("%s failed\n", __func__);
1276 scsiback_remove(dev);
1281 static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1283 switch (tport->tport_proto_id) {
1284 case SCSI_PROTOCOL_SAS:
1286 case SCSI_PROTOCOL_FCP:
1288 case SCSI_PROTOCOL_ISCSI:
1297 static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1299 struct scsiback_tpg *tpg = container_of(se_tpg,
1300 struct scsiback_tpg, se_tpg);
1301 struct scsiback_tport *tport = tpg->tport;
1303 return &tport->tport_name[0];
1306 static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1308 struct scsiback_tpg *tpg = container_of(se_tpg,
1309 struct scsiback_tpg, se_tpg);
1310 return tpg->tport_tpgt;
1313 static struct se_wwn *
1314 scsiback_make_tport(struct target_fabric_configfs *tf,
1315 struct config_group *group,
1318 struct scsiback_tport *tport;
1323 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1325 return ERR_PTR(-ENOMEM);
1327 tport->tport_wwpn = wwpn;
1329 * Determine the emulated Protocol Identifier and Target Port Name
1330 * based on the incoming configfs directory name.
1332 ptr = strstr(name, "naa.");
1334 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1337 ptr = strstr(name, "fc.");
1339 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1340 off = 3; /* Skip over "fc." */
1343 ptr = strstr(name, "iqn.");
1345 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1349 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1351 return ERR_PTR(-EINVAL);
1354 if (strlen(name) >= VSCSI_NAMELEN) {
1355 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1356 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1358 return ERR_PTR(-EINVAL);
1360 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1362 pr_debug("Allocated emulated Target %s Address: %s\n",
1363 scsiback_dump_proto_id(tport), name);
1365 return &tport->tport_wwn;
1368 static void scsiback_drop_tport(struct se_wwn *wwn)
1370 struct scsiback_tport *tport = container_of(wwn,
1371 struct scsiback_tport, tport_wwn);
1373 pr_debug("Deallocating emulated Target %s Address: %s\n",
1374 scsiback_dump_proto_id(tport), tport->tport_name);
1379 static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1384 static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1386 return transport_generic_free_cmd(se_cmd, 0);
1389 static void scsiback_release_cmd(struct se_cmd *se_cmd)
1391 struct se_session *se_sess = se_cmd->se_sess;
1392 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1394 if (se_tmr && se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
1395 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1399 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
1402 static int scsiback_shutdown_session(struct se_session *se_sess)
1407 static void scsiback_close_session(struct se_session *se_sess)
1411 static u32 scsiback_sess_get_index(struct se_session *se_sess)
1416 static int scsiback_write_pending(struct se_cmd *se_cmd)
1418 /* Go ahead and process the write immediately */
1419 target_execute_cmd(se_cmd);
1424 static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1429 static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1433 static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1438 static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1440 struct vscsibk_pend *pending_req = container_of(se_cmd,
1441 struct vscsibk_pend, se_cmd);
1443 pending_req->result = SAM_STAT_GOOD;
1444 scsiback_cmd_done(pending_req);
1448 static int scsiback_queue_status(struct se_cmd *se_cmd)
1450 struct vscsibk_pend *pending_req = container_of(se_cmd,
1451 struct vscsibk_pend, se_cmd);
1453 if (se_cmd->sense_buffer &&
1454 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1455 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1456 pending_req->result = (DRIVER_SENSE << 24) |
1457 SAM_STAT_CHECK_CONDITION;
1459 pending_req->result = se_cmd->scsi_status;
1461 scsiback_cmd_done(pending_req);
1465 static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1467 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1468 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1470 atomic_set(&tmr->tmr_complete, 1);
1471 wake_up(&tmr->tmr_wait);
1474 static void scsiback_aborted_task(struct se_cmd *se_cmd)
1478 static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
1481 struct se_portal_group *se_tpg = param_to_tpg(item);
1482 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1486 mutex_lock(&tpg->tv_tpg_mutex);
1487 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1488 mutex_unlock(&tpg->tv_tpg_mutex);
1493 static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
1494 const char *page, size_t count)
1496 struct se_portal_group *se_tpg = param_to_tpg(item);
1497 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1501 if (strlen(page) >= VSCSI_NAMELEN) {
1502 pr_err("param alias: %s, exceeds max: %d\n", page,
1507 mutex_lock(&tpg->tv_tpg_mutex);
1508 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1509 if (tpg->param_alias[len - 1] == '\n')
1510 tpg->param_alias[len - 1] = '\0';
1511 mutex_unlock(&tpg->tv_tpg_mutex);
1516 CONFIGFS_ATTR(scsiback_tpg_param_, alias);
1518 static struct configfs_attribute *scsiback_param_attrs[] = {
1519 &scsiback_tpg_param_attr_alias,
1523 static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg,
1524 struct se_session *se_sess, void *p)
1526 struct scsiback_tpg *tpg = container_of(se_tpg,
1527 struct scsiback_tpg, se_tpg);
1533 static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1536 struct scsiback_nexus *tv_nexus;
1539 mutex_lock(&tpg->tv_tpg_mutex);
1540 if (tpg->tpg_nexus) {
1541 pr_debug("tpg->tpg_nexus already exists\n");
1546 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1552 tv_nexus->tvn_se_sess = target_alloc_session(&tpg->se_tpg,
1553 VSCSI_DEFAULT_SESSION_TAGS,
1554 sizeof(struct vscsibk_pend),
1555 TARGET_PROT_NORMAL, name,
1556 tv_nexus, scsiback_alloc_sess_cb);
1557 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1564 mutex_unlock(&tpg->tv_tpg_mutex);
1568 static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1570 struct se_session *se_sess;
1571 struct scsiback_nexus *tv_nexus;
1573 mutex_lock(&tpg->tv_tpg_mutex);
1574 tv_nexus = tpg->tpg_nexus;
1576 mutex_unlock(&tpg->tv_tpg_mutex);
1580 se_sess = tv_nexus->tvn_se_sess;
1582 mutex_unlock(&tpg->tv_tpg_mutex);
1586 if (tpg->tv_tpg_port_count != 0) {
1587 mutex_unlock(&tpg->tv_tpg_mutex);
1588 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1589 tpg->tv_tpg_port_count);
1593 if (tpg->tv_tpg_fe_count != 0) {
1594 mutex_unlock(&tpg->tv_tpg_mutex);
1595 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1596 tpg->tv_tpg_fe_count);
1600 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1601 scsiback_dump_proto_id(tpg->tport),
1602 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1605 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1607 transport_deregister_session(tv_nexus->tvn_se_sess);
1608 tpg->tpg_nexus = NULL;
1609 mutex_unlock(&tpg->tv_tpg_mutex);
1615 static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
1617 struct se_portal_group *se_tpg = to_tpg(item);
1618 struct scsiback_tpg *tpg = container_of(se_tpg,
1619 struct scsiback_tpg, se_tpg);
1620 struct scsiback_nexus *tv_nexus;
1623 mutex_lock(&tpg->tv_tpg_mutex);
1624 tv_nexus = tpg->tpg_nexus;
1626 mutex_unlock(&tpg->tv_tpg_mutex);
1629 ret = snprintf(page, PAGE_SIZE, "%s\n",
1630 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1631 mutex_unlock(&tpg->tv_tpg_mutex);
1636 static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1637 const char *page, size_t count)
1639 struct se_portal_group *se_tpg = to_tpg(item);
1640 struct scsiback_tpg *tpg = container_of(se_tpg,
1641 struct scsiback_tpg, se_tpg);
1642 struct scsiback_tport *tport_wwn = tpg->tport;
1643 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1646 * Shutdown the active I_T nexus if 'NULL' is passed.
1648 if (!strncmp(page, "NULL", 4)) {
1649 ret = scsiback_drop_nexus(tpg);
1650 return (!ret) ? count : ret;
1653 * Otherwise make sure the passed virtual Initiator port WWN matches
1654 * the fabric protocol_id set in scsiback_make_tport(), and call
1655 * scsiback_make_nexus().
1657 if (strlen(page) >= VSCSI_NAMELEN) {
1658 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1659 page, VSCSI_NAMELEN);
1662 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1664 ptr = strstr(i_port, "naa.");
1666 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1667 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1668 i_port, scsiback_dump_proto_id(tport_wwn));
1671 port_ptr = &i_port[0];
1674 ptr = strstr(i_port, "fc.");
1676 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1677 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1678 i_port, scsiback_dump_proto_id(tport_wwn));
1681 port_ptr = &i_port[3]; /* Skip over "fc." */
1684 ptr = strstr(i_port, "iqn.");
1686 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1687 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1688 i_port, scsiback_dump_proto_id(tport_wwn));
1691 port_ptr = &i_port[0];
1694 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1698 * Clear any trailing newline for the NAA WWN
1701 if (i_port[strlen(i_port) - 1] == '\n')
1702 i_port[strlen(i_port) - 1] = '\0';
1704 ret = scsiback_make_nexus(tpg, port_ptr);
1711 CONFIGFS_ATTR(scsiback_tpg_, nexus);
1713 static struct configfs_attribute *scsiback_tpg_attrs[] = {
1714 &scsiback_tpg_attr_nexus,
1719 scsiback_wwn_version_show(struct config_item *item, char *page)
1721 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1723 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1726 CONFIGFS_ATTR_RO(scsiback_wwn_, version);
1728 static struct configfs_attribute *scsiback_wwn_attrs[] = {
1729 &scsiback_wwn_attr_version,
1733 static char *scsiback_get_fabric_name(void)
1735 return "xen-pvscsi";
1738 static int scsiback_port_link(struct se_portal_group *se_tpg,
1741 struct scsiback_tpg *tpg = container_of(se_tpg,
1742 struct scsiback_tpg, se_tpg);
1744 mutex_lock(&tpg->tv_tpg_mutex);
1745 tpg->tv_tpg_port_count++;
1746 mutex_unlock(&tpg->tv_tpg_mutex);
1751 static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1754 struct scsiback_tpg *tpg = container_of(se_tpg,
1755 struct scsiback_tpg, se_tpg);
1757 mutex_lock(&tpg->tv_tpg_mutex);
1758 tpg->tv_tpg_port_count--;
1759 mutex_unlock(&tpg->tv_tpg_mutex);
1762 static struct se_portal_group *
1763 scsiback_make_tpg(struct se_wwn *wwn,
1764 struct config_group *group,
1767 struct scsiback_tport *tport = container_of(wwn,
1768 struct scsiback_tport, tport_wwn);
1770 struct scsiback_tpg *tpg;
1774 if (strstr(name, "tpgt_") != name)
1775 return ERR_PTR(-EINVAL);
1776 ret = kstrtou16(name + 5, 10, &tpgt);
1778 return ERR_PTR(ret);
1780 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1782 return ERR_PTR(-ENOMEM);
1784 mutex_init(&tpg->tv_tpg_mutex);
1785 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1786 INIT_LIST_HEAD(&tpg->info_list);
1788 tpg->tport_tpgt = tpgt;
1790 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
1795 mutex_lock(&scsiback_mutex);
1796 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1797 mutex_unlock(&scsiback_mutex);
1799 return &tpg->se_tpg;
1802 static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1804 struct scsiback_tpg *tpg = container_of(se_tpg,
1805 struct scsiback_tpg, se_tpg);
1807 mutex_lock(&scsiback_mutex);
1808 list_del(&tpg->tv_tpg_list);
1809 mutex_unlock(&scsiback_mutex);
1811 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1813 scsiback_drop_nexus(tpg);
1815 * Deregister the se_tpg from TCM.
1817 core_tpg_deregister(se_tpg);
1821 static int scsiback_check_true(struct se_portal_group *se_tpg)
1826 static int scsiback_check_false(struct se_portal_group *se_tpg)
1831 static const struct target_core_fabric_ops scsiback_ops = {
1832 .module = THIS_MODULE,
1833 .name = "xen-pvscsi",
1834 .get_fabric_name = scsiback_get_fabric_name,
1835 .tpg_get_wwn = scsiback_get_fabric_wwn,
1836 .tpg_get_tag = scsiback_get_tag,
1837 .tpg_check_demo_mode = scsiback_check_true,
1838 .tpg_check_demo_mode_cache = scsiback_check_true,
1839 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1840 .tpg_check_prod_mode_write_protect = scsiback_check_false,
1841 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1842 .check_stop_free = scsiback_check_stop_free,
1843 .release_cmd = scsiback_release_cmd,
1844 .shutdown_session = scsiback_shutdown_session,
1845 .close_session = scsiback_close_session,
1846 .sess_get_index = scsiback_sess_get_index,
1847 .sess_get_initiator_sid = NULL,
1848 .write_pending = scsiback_write_pending,
1849 .write_pending_status = scsiback_write_pending_status,
1850 .set_default_node_attributes = scsiback_set_default_node_attrs,
1851 .get_cmd_state = scsiback_get_cmd_state,
1852 .queue_data_in = scsiback_queue_data_in,
1853 .queue_status = scsiback_queue_status,
1854 .queue_tm_rsp = scsiback_queue_tm_rsp,
1855 .aborted_task = scsiback_aborted_task,
1857 * Setup callers for generic logic in target_core_fabric_configfs.c
1859 .fabric_make_wwn = scsiback_make_tport,
1860 .fabric_drop_wwn = scsiback_drop_tport,
1861 .fabric_make_tpg = scsiback_make_tpg,
1862 .fabric_drop_tpg = scsiback_drop_tpg,
1863 .fabric_post_link = scsiback_port_link,
1864 .fabric_pre_unlink = scsiback_port_unlink,
1866 .tfc_wwn_attrs = scsiback_wwn_attrs,
1867 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1868 .tfc_tpg_param_attrs = scsiback_param_attrs,
1871 static const struct xenbus_device_id scsiback_ids[] = {
1876 static struct xenbus_driver scsiback_driver = {
1877 .ids = scsiback_ids,
1878 .probe = scsiback_probe,
1879 .remove = scsiback_remove,
1880 .otherend_changed = scsiback_frontend_changed
1883 static int __init scsiback_init(void)
1890 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1891 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1893 ret = xenbus_register_backend(&scsiback_driver);
1897 ret = target_register_template(&scsiback_ops);
1899 goto out_unregister_xenbus;
1903 out_unregister_xenbus:
1904 xenbus_unregister_driver(&scsiback_driver);
1906 pr_err("%s: error %d\n", __func__, ret);
1910 static void __exit scsiback_exit(void)
1914 while (free_pages_num) {
1915 if (get_free_page(&page))
1917 gnttab_free_pages(1, &page);
1919 target_unregister_template(&scsiback_ops);
1920 xenbus_unregister_driver(&scsiback_driver);
1923 module_init(scsiback_init);
1924 module_exit(scsiback_exit);
1926 MODULE_DESCRIPTION("Xen SCSI backend driver");
1927 MODULE_LICENSE("Dual BSD/GPL");
1928 MODULE_ALIAS("xen-backend:vscsi");
1929 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");