pinctrl: mcp23s08: generalize irq property handling
[linux-stable.git] / drivers / xen / xen-scsiback.c
blobd6950e0802b7df16b8fe5084fe4008104c19df61
1 /*
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
31 * IN THE SOFTWARE.
34 #define pr_fmt(fmt) "xen-pvscsi: " fmt
36 #include <stdarg.h>
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>
59 #include <xen/xen.h>
60 #include <xen/balloon.h>
61 #include <xen/events.h>
62 #include <xen/xenbus.h>
63 #include <xen/grant_table.h>
64 #include <xen/page.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
72 struct ids_tuple {
73 unsigned int hst; /* host */
74 unsigned int chn; /* channel */
75 unsigned int tgt; /* target */
76 unsigned int lun; /* LUN */
79 struct v2p_entry {
80 struct ids_tuple v; /* translate from */
81 struct scsiback_tpg *tpg; /* translate to */
82 unsigned int lun;
83 struct kref kref;
84 struct list_head l;
87 struct vscsibk_info {
88 struct xenbus_device *dev;
90 domid_t domid;
91 unsigned int irq;
93 struct vscsiif_back_ring ring;
94 int ring_error;
96 spinlock_t ring_lock;
97 atomic_t nr_unreplied_reqs;
99 spinlock_t v2p_lock;
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 {
116 uint16_t rqid;
118 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
119 uint8_t cmd_len;
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 */
124 uint32_t data_len;
125 uint32_t result;
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 */
153 u8 tport_proto_id;
154 /* Binary World Wide unique Port Name for pvscsi Target port */
155 u64 tport_wwpn;
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 */
164 u16 tport_tpgt;
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 */
168 int tv_tpg_fe_count;
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)
216 unsigned long flags;
217 int i = free_pages_num + num, n = num;
219 if (num == 0)
220 return;
221 if (i > scsiback_max_buffer_pages) {
222 n = min(num, i - scsiback_max_buffer_pages);
223 gnttab_free_pages(n, page + num - n);
224 n = 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);
229 free_pages_num += n;
230 spin_unlock_irqrestore(&free_pages_lock, flags);
233 static int get_free_page(struct page **page)
235 unsigned long flags;
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);
244 free_pages_num--;
245 spin_unlock_irqrestore(&free_pages_lock, flags);
246 return 0;
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;
278 int err;
280 kfree(req->sgl);
281 req->sgl = NULL;
282 req->n_sg = 0;
284 if (!req->n_grants)
285 return;
287 for (i = 0; i < req->n_grants; i++) {
288 handle = req->grant_handles[i];
289 if (handle == SCSIBACK_INVALID_HANDLE)
290 continue;
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]);
296 invcount++;
297 if (invcount < VSCSI_GRANT_BATCH)
298 continue;
299 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
300 BUG_ON(err);
301 invcount = 0;
304 if (invcount) {
305 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
306 BUG_ON(err);
309 put_free_pages(req->pages, req->n_grants);
310 req->n_grants = 0;
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);
322 kfree(entry);
325 static void scsiback_send_response(struct vscsibk_info *info,
326 char *sense_buffer, int32_t result, uint32_t resid,
327 uint16_t rqid)
329 struct vscsiif_response *ring_res;
330 int notify;
331 struct scsi_sense_hdr sshdr;
332 unsigned long flags;
333 unsigned len;
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,
345 &sshdr)) {
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;
350 } else {
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);
359 if (notify)
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;
378 unsigned int resid;
379 int errors;
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);
390 scsiback_put(info);
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;
403 int rc;
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,
412 NULL, 0, NULL, 0);
413 if (rc < 0) {
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)
423 int err, i;
425 if (!cnt)
426 return 0;
428 err = gnttab_map_refs(map, NULL, pg, cnt);
429 BUG_ON(err);
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;
434 err = -ENOMEM;
435 } else {
436 get_page(pg[i]);
438 grant[i] = map[i].handle;
440 return err;
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");
455 return -ENOMEM;
457 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
458 flags, seg[i].gref, info->domid);
459 mapcount++;
460 if (mapcount < VSCSI_GRANT_BATCH)
461 continue;
462 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
463 pg += mapcount;
464 grant += mapcount;
465 pending_req->n_grants += mapcount;
466 if (err)
467 return err;
468 mapcount = 0;
470 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
471 pending_req->n_grants += mapcount;
472 return err;
475 static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
476 struct vscsibk_pend *pending_req)
478 u32 flags;
479 int i, err, n_segs, i_seg = 0;
480 struct page **pg;
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;
493 if (!nr_segments)
494 return 0;
496 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
497 pr_debug("invalid parameter nr_seg = %d\n",
498 ring_req->nr_segments);
499 return -EINVAL;
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);
506 if (err)
507 return err;
508 nr_sgl = nr_segments;
509 nr_segments = 0;
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)
517 return -EINVAL;
518 nr_segments += n_segs;
520 if (nr_segments > SG_ALL) {
521 pr_debug("invalid nr_seg = %d\n", nr_segments);
522 return -EINVAL;
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)
530 return -ENOMEM;
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;
541 if (!nr_sgl) {
542 seg = ring_req->seg;
543 err = scsiback_gnttab_data_map_list(pending_req, seg,
544 pg, grant, nr_segments, flags);
545 if (err)
546 return err;
547 } else {
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);
555 if (err)
556 return err;
557 pg += n_segs;
558 grant += n_segs;
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;
569 seg++;
570 if (nr_sgl && (unsigned long)seg >= end_seg) {
571 i_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)
580 return -EINVAL;
583 return 0;
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);
592 info->irq = 0;
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);
607 if (!tmr) {
608 target_put_sess_cmd(se_cmd);
609 goto err;
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);
618 if (rc)
619 goto err;
621 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
623 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
624 SUCCESS : FAILED;
626 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
627 transport_generic_free_cmd(&pending_req->se_cmd, 1);
628 return;
629 err:
630 if (tmr)
631 kfree(tmr);
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,
639 struct ids_tuple *v)
641 struct v2p_entry *entry;
642 struct list_head *head = &(info->v2p_entry_lists);
643 unsigned long flags;
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);
651 goto out;
654 entry = NULL;
656 out:
657 spin_unlock_irqrestore(&info->v2p_lock, flags);
658 return entry;
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;
668 int tag, i;
670 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
671 if (tag < 0) {
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;
683 return req;
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",
705 ring_req->cmd_len);
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);
714 if (!v2p) {
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);
732 return pending_req;
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;
740 RING_IDX rc, rp;
741 int more_to_do;
742 uint32_t result;
744 rc = ring->req_cons;
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;
753 return 0;
756 while ((rc != rp)) {
757 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
758 break;
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)) {
766 case -ENODEV:
767 result = DID_NO_CONNECT;
768 break;
769 default:
770 result = DRIVER_ERROR;
771 break;
773 scsiback_send_response(info, NULL, result << 24, 0,
774 ring_req.rqid);
775 return 1;
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);
785 } else {
786 scsiback_cmd_exec(pending_req);
788 break;
789 case VSCSIIF_ACT_SCSI_ABORT:
790 scsiback_device_action(pending_req, TMR_ABORT_TASK,
791 ring_req.ref_rqid);
792 break;
793 case VSCSIIF_ACT_SCSI_RESET:
794 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
795 break;
796 default:
797 pr_err_ratelimited("invalid request\n");
798 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24, 0,
799 pending_req);
800 transport_generic_free_cmd(&pending_req->se_cmd, 0);
801 break;
804 /* Yield point for this unbounded loop. */
805 cond_resched();
808 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
809 return 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)
817 return IRQ_HANDLED;
819 while (scsiback_do_cmd_fn(info))
820 cond_resched();
822 return IRQ_HANDLED;
825 static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
826 evtchn_port_t evtchn)
828 void *area;
829 struct vscsiif_sring *sring;
830 int err;
832 if (info->irq)
833 return -1;
835 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
836 if (err)
837 return err;
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);
843 if (err < 0)
844 goto unmap_page;
846 info->irq = err;
848 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
849 IRQF_ONESHOT, "vscsiif-backend", info);
850 if (err)
851 goto free_irq;
853 return 0;
855 free_irq:
856 unbind_from_irqhandler(info->irq, info);
857 info->irq = 0;
858 unmap_page:
859 xenbus_unmap_ring_vfree(info->dev, area);
861 return err;
864 static int scsiback_map(struct vscsibk_info *info)
866 struct xenbus_device *dev = info->dev;
867 unsigned int ring_ref, evtchn;
868 int err;
870 err = xenbus_gather(XBT_NIL, dev->otherend,
871 "ring-ref", "%u", &ring_ref,
872 "event-channel", "%u", &evtchn, NULL);
873 if (err) {
874 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
875 return err;
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))
894 return entry;
896 return NULL;
900 Add a new translation entry
902 static int scsiback_add_translation_entry(struct vscsibk_info *info,
903 char *phy, struct ids_tuple *v)
905 int err = 0;
906 struct v2p_entry *new;
907 unsigned long flags;
908 char *lunp;
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, ':');
915 if (!lunp) {
916 pr_err("illegal format of physical device %s\n", phy);
917 return -EINVAL;
919 *lunp = 0;
920 lunp++;
921 err = kstrtoull(lunp, 10, &unpacked_lun);
922 if (err < 0) {
923 pr_err("lun number not valid: %s\n", lunp);
924 return err;
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";
936 else
937 tpg = tpg_entry;
938 break;
941 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
942 break;
945 if (tpg) {
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);
952 if (!tpg) {
953 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
954 return -ENODEV;
957 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
958 if (new == NULL) {
959 err = -ENOMEM;
960 goto out_free;
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");
968 err = -EEXIST;
969 goto out;
972 /* Create a new translation entry and add to the list */
973 kref_init(&new->kref);
974 new->v = *v;
975 new->tpg = tpg;
976 new->lun = unpacked_lun;
977 list_add_tail(&new->l, &info->v2p_entry_lists);
979 out:
980 spin_unlock_irqrestore(&info->v2p_lock, flags);
982 out_free:
983 if (err) {
984 mutex_lock(&tpg->tv_tpg_mutex);
985 tpg->tv_tpg_fe_count--;
986 mutex_unlock(&tpg->tv_tpg_mutex);
987 kfree(new);
990 return err;
993 static void __scsiback_del_translation_entry(struct v2p_entry *entry)
995 list_del(&entry->l);
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;
1007 int ret = 0;
1009 spin_lock_irqsave(&info->v2p_lock, flags);
1010 /* Find out the translation entry specified */
1011 entry = scsiback_chk_translation_entry(info, v);
1012 if (entry)
1013 __scsiback_del_translation_entry(entry);
1014 else
1015 ret = -ENOENT;
1017 spin_unlock_irqrestore(&info->v2p_lock, flags);
1018 return ret;
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;
1027 if (try) {
1028 spin_lock_irqsave(&info->v2p_lock, flags);
1029 entry = scsiback_chk_translation_entry(info, vir);
1030 spin_unlock_irqrestore(&info->v2p_lock, flags);
1031 if (entry)
1032 return;
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);
1040 } else if (!try) {
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,
1060 char *ent)
1062 int err;
1063 struct ids_tuple vir;
1064 char *val;
1065 int device_state;
1066 char phy[VSCSI_NAMELEN];
1067 char str[64];
1068 char state[64];
1069 struct xenbus_device *dev = info->dev;
1071 /* read status */
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))
1075 return;
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);
1080 if (IS_ERR(val)) {
1081 xenbus_printf(XBT_NIL, dev->nodename, state,
1082 "%d", XenbusStateClosed);
1083 return;
1085 strlcpy(phy, val, VSCSI_NAMELEN);
1086 kfree(val);
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);
1095 return;
1098 switch (op) {
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);
1103 break;
1104 case XenbusStateConnected:
1105 scsiback_do_add_lun(info, state, phy, &vir, 1);
1106 break;
1107 case XenbusStateClosing:
1108 scsiback_do_del_lun(info, state, &vir);
1109 break;
1110 default:
1111 break;
1113 break;
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);
1126 break;
1127 /* When it is necessary, processing is added here. */
1128 default:
1129 break;
1133 static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1135 int i;
1136 char **dir;
1137 unsigned int ndir = 0;
1139 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1140 &ndir);
1141 if (IS_ERR(dir))
1142 return;
1144 for (i = 0; i < ndir; i++)
1145 scsiback_do_1lun_hotplug(info, op, dir[i]);
1147 kfree(dir);
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:
1157 break;
1159 case XenbusStateInitialised:
1160 if (scsiback_map(info))
1161 break;
1163 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1164 xenbus_switch_state(dev, XenbusStateConnected);
1165 break;
1167 case XenbusStateConnected:
1168 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1170 if (dev->state == XenbusStateConnected)
1171 break;
1173 xenbus_switch_state(dev, XenbusStateConnected);
1174 break;
1176 case XenbusStateClosing:
1177 if (info->irq)
1178 scsiback_disconnect(info);
1180 xenbus_switch_state(dev, XenbusStateClosing);
1181 break;
1183 case XenbusStateClosed:
1184 xenbus_switch_state(dev, XenbusStateClosed);
1185 if (xenbus_dev_is_online(dev))
1186 break;
1187 /* fall through if not online */
1188 case XenbusStateUnknown:
1189 device_unregister(&dev->dev);
1190 break;
1192 case XenbusStateReconfiguring:
1193 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1194 xenbus_switch_state(dev, XenbusStateReconfigured);
1196 break;
1198 default:
1199 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1200 frontend_state);
1201 break;
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);
1226 if (info->irq)
1227 scsiback_disconnect(info);
1229 scsiback_release_translation_entry(info);
1231 dev_set_drvdata(&dev->dev, NULL);
1233 return 0;
1236 static int scsiback_probe(struct xenbus_device *dev,
1237 const struct xenbus_device_id *id)
1239 int err;
1241 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1242 GFP_KERNEL);
1244 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
1246 if (!info) {
1247 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1248 return -ENOMEM;
1250 info->dev = dev;
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);
1258 info->dev = dev;
1259 info->irq = 0;
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",
1264 SG_ALL);
1265 if (err)
1266 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1268 err = xenbus_switch_state(dev, XenbusStateInitWait);
1269 if (err)
1270 goto fail;
1272 return 0;
1274 fail:
1275 pr_warn("%s failed\n", __func__);
1276 scsiback_remove(dev);
1278 return err;
1281 static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1283 switch (tport->tport_proto_id) {
1284 case SCSI_PROTOCOL_SAS:
1285 return "SAS";
1286 case SCSI_PROTOCOL_FCP:
1287 return "FCP";
1288 case SCSI_PROTOCOL_ISCSI:
1289 return "iSCSI";
1290 default:
1291 break;
1294 return "Unknown";
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,
1316 const char *name)
1318 struct scsiback_tport *tport;
1319 char *ptr;
1320 u64 wwpn = 0;
1321 int off = 0;
1323 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1324 if (!tport)
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.");
1333 if (ptr) {
1334 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1335 goto check_len;
1337 ptr = strstr(name, "fc.");
1338 if (ptr) {
1339 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1340 off = 3; /* Skip over "fc." */
1341 goto check_len;
1343 ptr = strstr(name, "iqn.");
1344 if (ptr) {
1345 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1346 goto check_len;
1349 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1350 kfree(tport);
1351 return ERR_PTR(-EINVAL);
1353 check_len:
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);
1357 kfree(tport);
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);
1376 kfree(tport);
1379 static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1381 return 1;
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;
1396 kfree(tmr);
1399 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
1402 static u32 scsiback_sess_get_index(struct se_session *se_sess)
1404 return 0;
1407 static int scsiback_write_pending(struct se_cmd *se_cmd)
1409 /* Go ahead and process the write immediately */
1410 target_execute_cmd(se_cmd);
1412 return 0;
1415 static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1417 return 0;
1420 static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1424 static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1426 return 0;
1429 static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1431 struct vscsibk_pend *pending_req = container_of(se_cmd,
1432 struct vscsibk_pend, se_cmd);
1434 pending_req->result = SAM_STAT_GOOD;
1435 scsiback_cmd_done(pending_req);
1436 return 0;
1439 static int scsiback_queue_status(struct se_cmd *se_cmd)
1441 struct vscsibk_pend *pending_req = container_of(se_cmd,
1442 struct vscsibk_pend, se_cmd);
1444 if (se_cmd->sense_buffer &&
1445 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1446 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1447 pending_req->result = (DRIVER_SENSE << 24) |
1448 SAM_STAT_CHECK_CONDITION;
1449 else
1450 pending_req->result = se_cmd->scsi_status;
1452 scsiback_cmd_done(pending_req);
1453 return 0;
1456 static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1458 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1459 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1461 atomic_set(&tmr->tmr_complete, 1);
1462 wake_up(&tmr->tmr_wait);
1465 static void scsiback_aborted_task(struct se_cmd *se_cmd)
1469 static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
1470 char *page)
1472 struct se_portal_group *se_tpg = param_to_tpg(item);
1473 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1474 se_tpg);
1475 ssize_t rb;
1477 mutex_lock(&tpg->tv_tpg_mutex);
1478 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1479 mutex_unlock(&tpg->tv_tpg_mutex);
1481 return rb;
1484 static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
1485 const char *page, size_t count)
1487 struct se_portal_group *se_tpg = param_to_tpg(item);
1488 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1489 se_tpg);
1490 int len;
1492 if (strlen(page) >= VSCSI_NAMELEN) {
1493 pr_err("param alias: %s, exceeds max: %d\n", page,
1494 VSCSI_NAMELEN);
1495 return -EINVAL;
1498 mutex_lock(&tpg->tv_tpg_mutex);
1499 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1500 if (tpg->param_alias[len - 1] == '\n')
1501 tpg->param_alias[len - 1] = '\0';
1502 mutex_unlock(&tpg->tv_tpg_mutex);
1504 return count;
1507 CONFIGFS_ATTR(scsiback_tpg_param_, alias);
1509 static struct configfs_attribute *scsiback_param_attrs[] = {
1510 &scsiback_tpg_param_attr_alias,
1511 NULL,
1514 static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg,
1515 struct se_session *se_sess, void *p)
1517 struct scsiback_tpg *tpg = container_of(se_tpg,
1518 struct scsiback_tpg, se_tpg);
1520 tpg->tpg_nexus = p;
1521 return 0;
1524 static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1525 const char *name)
1527 struct scsiback_nexus *tv_nexus;
1528 int ret = 0;
1530 mutex_lock(&tpg->tv_tpg_mutex);
1531 if (tpg->tpg_nexus) {
1532 pr_debug("tpg->tpg_nexus already exists\n");
1533 ret = -EEXIST;
1534 goto out_unlock;
1537 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1538 if (!tv_nexus) {
1539 ret = -ENOMEM;
1540 goto out_unlock;
1543 tv_nexus->tvn_se_sess = target_alloc_session(&tpg->se_tpg,
1544 VSCSI_DEFAULT_SESSION_TAGS,
1545 sizeof(struct vscsibk_pend),
1546 TARGET_PROT_NORMAL, name,
1547 tv_nexus, scsiback_alloc_sess_cb);
1548 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1549 kfree(tv_nexus);
1550 ret = -ENOMEM;
1551 goto out_unlock;
1554 out_unlock:
1555 mutex_unlock(&tpg->tv_tpg_mutex);
1556 return ret;
1559 static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1561 struct se_session *se_sess;
1562 struct scsiback_nexus *tv_nexus;
1564 mutex_lock(&tpg->tv_tpg_mutex);
1565 tv_nexus = tpg->tpg_nexus;
1566 if (!tv_nexus) {
1567 mutex_unlock(&tpg->tv_tpg_mutex);
1568 return -ENODEV;
1571 se_sess = tv_nexus->tvn_se_sess;
1572 if (!se_sess) {
1573 mutex_unlock(&tpg->tv_tpg_mutex);
1574 return -ENODEV;
1577 if (tpg->tv_tpg_port_count != 0) {
1578 mutex_unlock(&tpg->tv_tpg_mutex);
1579 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1580 tpg->tv_tpg_port_count);
1581 return -EBUSY;
1584 if (tpg->tv_tpg_fe_count != 0) {
1585 mutex_unlock(&tpg->tv_tpg_mutex);
1586 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1587 tpg->tv_tpg_fe_count);
1588 return -EBUSY;
1591 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1592 scsiback_dump_proto_id(tpg->tport),
1593 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1596 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1598 transport_deregister_session(tv_nexus->tvn_se_sess);
1599 tpg->tpg_nexus = NULL;
1600 mutex_unlock(&tpg->tv_tpg_mutex);
1602 kfree(tv_nexus);
1603 return 0;
1606 static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
1608 struct se_portal_group *se_tpg = to_tpg(item);
1609 struct scsiback_tpg *tpg = container_of(se_tpg,
1610 struct scsiback_tpg, se_tpg);
1611 struct scsiback_nexus *tv_nexus;
1612 ssize_t ret;
1614 mutex_lock(&tpg->tv_tpg_mutex);
1615 tv_nexus = tpg->tpg_nexus;
1616 if (!tv_nexus) {
1617 mutex_unlock(&tpg->tv_tpg_mutex);
1618 return -ENODEV;
1620 ret = snprintf(page, PAGE_SIZE, "%s\n",
1621 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1622 mutex_unlock(&tpg->tv_tpg_mutex);
1624 return ret;
1627 static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1628 const char *page, size_t count)
1630 struct se_portal_group *se_tpg = to_tpg(item);
1631 struct scsiback_tpg *tpg = container_of(se_tpg,
1632 struct scsiback_tpg, se_tpg);
1633 struct scsiback_tport *tport_wwn = tpg->tport;
1634 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1635 int ret;
1637 * Shutdown the active I_T nexus if 'NULL' is passed.
1639 if (!strncmp(page, "NULL", 4)) {
1640 ret = scsiback_drop_nexus(tpg);
1641 return (!ret) ? count : ret;
1644 * Otherwise make sure the passed virtual Initiator port WWN matches
1645 * the fabric protocol_id set in scsiback_make_tport(), and call
1646 * scsiback_make_nexus().
1648 if (strlen(page) >= VSCSI_NAMELEN) {
1649 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1650 page, VSCSI_NAMELEN);
1651 return -EINVAL;
1653 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1655 ptr = strstr(i_port, "naa.");
1656 if (ptr) {
1657 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1658 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1659 i_port, scsiback_dump_proto_id(tport_wwn));
1660 return -EINVAL;
1662 port_ptr = &i_port[0];
1663 goto check_newline;
1665 ptr = strstr(i_port, "fc.");
1666 if (ptr) {
1667 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1668 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1669 i_port, scsiback_dump_proto_id(tport_wwn));
1670 return -EINVAL;
1672 port_ptr = &i_port[3]; /* Skip over "fc." */
1673 goto check_newline;
1675 ptr = strstr(i_port, "iqn.");
1676 if (ptr) {
1677 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1678 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1679 i_port, scsiback_dump_proto_id(tport_wwn));
1680 return -EINVAL;
1682 port_ptr = &i_port[0];
1683 goto check_newline;
1685 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1686 i_port);
1687 return -EINVAL;
1689 * Clear any trailing newline for the NAA WWN
1691 check_newline:
1692 if (i_port[strlen(i_port) - 1] == '\n')
1693 i_port[strlen(i_port) - 1] = '\0';
1695 ret = scsiback_make_nexus(tpg, port_ptr);
1696 if (ret < 0)
1697 return ret;
1699 return count;
1702 CONFIGFS_ATTR(scsiback_tpg_, nexus);
1704 static struct configfs_attribute *scsiback_tpg_attrs[] = {
1705 &scsiback_tpg_attr_nexus,
1706 NULL,
1709 static ssize_t
1710 scsiback_wwn_version_show(struct config_item *item, char *page)
1712 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1713 UTS_RELEASE"\n",
1714 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1717 CONFIGFS_ATTR_RO(scsiback_wwn_, version);
1719 static struct configfs_attribute *scsiback_wwn_attrs[] = {
1720 &scsiback_wwn_attr_version,
1721 NULL,
1724 static char *scsiback_get_fabric_name(void)
1726 return "xen-pvscsi";
1729 static int scsiback_port_link(struct se_portal_group *se_tpg,
1730 struct se_lun *lun)
1732 struct scsiback_tpg *tpg = container_of(se_tpg,
1733 struct scsiback_tpg, se_tpg);
1735 mutex_lock(&tpg->tv_tpg_mutex);
1736 tpg->tv_tpg_port_count++;
1737 mutex_unlock(&tpg->tv_tpg_mutex);
1739 return 0;
1742 static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1743 struct se_lun *lun)
1745 struct scsiback_tpg *tpg = container_of(se_tpg,
1746 struct scsiback_tpg, se_tpg);
1748 mutex_lock(&tpg->tv_tpg_mutex);
1749 tpg->tv_tpg_port_count--;
1750 mutex_unlock(&tpg->tv_tpg_mutex);
1753 static struct se_portal_group *
1754 scsiback_make_tpg(struct se_wwn *wwn,
1755 struct config_group *group,
1756 const char *name)
1758 struct scsiback_tport *tport = container_of(wwn,
1759 struct scsiback_tport, tport_wwn);
1761 struct scsiback_tpg *tpg;
1762 u16 tpgt;
1763 int ret;
1765 if (strstr(name, "tpgt_") != name)
1766 return ERR_PTR(-EINVAL);
1767 ret = kstrtou16(name + 5, 10, &tpgt);
1768 if (ret)
1769 return ERR_PTR(ret);
1771 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1772 if (!tpg)
1773 return ERR_PTR(-ENOMEM);
1775 mutex_init(&tpg->tv_tpg_mutex);
1776 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1777 INIT_LIST_HEAD(&tpg->info_list);
1778 tpg->tport = tport;
1779 tpg->tport_tpgt = tpgt;
1781 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
1782 if (ret < 0) {
1783 kfree(tpg);
1784 return NULL;
1786 mutex_lock(&scsiback_mutex);
1787 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1788 mutex_unlock(&scsiback_mutex);
1790 return &tpg->se_tpg;
1793 static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1795 struct scsiback_tpg *tpg = container_of(se_tpg,
1796 struct scsiback_tpg, se_tpg);
1798 mutex_lock(&scsiback_mutex);
1799 list_del(&tpg->tv_tpg_list);
1800 mutex_unlock(&scsiback_mutex);
1802 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1804 scsiback_drop_nexus(tpg);
1806 * Deregister the se_tpg from TCM.
1808 core_tpg_deregister(se_tpg);
1809 kfree(tpg);
1812 static int scsiback_check_true(struct se_portal_group *se_tpg)
1814 return 1;
1817 static int scsiback_check_false(struct se_portal_group *se_tpg)
1819 return 0;
1822 static const struct target_core_fabric_ops scsiback_ops = {
1823 .module = THIS_MODULE,
1824 .name = "xen-pvscsi",
1825 .get_fabric_name = scsiback_get_fabric_name,
1826 .tpg_get_wwn = scsiback_get_fabric_wwn,
1827 .tpg_get_tag = scsiback_get_tag,
1828 .tpg_check_demo_mode = scsiback_check_true,
1829 .tpg_check_demo_mode_cache = scsiback_check_true,
1830 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1831 .tpg_check_prod_mode_write_protect = scsiback_check_false,
1832 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1833 .check_stop_free = scsiback_check_stop_free,
1834 .release_cmd = scsiback_release_cmd,
1835 .sess_get_index = scsiback_sess_get_index,
1836 .sess_get_initiator_sid = NULL,
1837 .write_pending = scsiback_write_pending,
1838 .write_pending_status = scsiback_write_pending_status,
1839 .set_default_node_attributes = scsiback_set_default_node_attrs,
1840 .get_cmd_state = scsiback_get_cmd_state,
1841 .queue_data_in = scsiback_queue_data_in,
1842 .queue_status = scsiback_queue_status,
1843 .queue_tm_rsp = scsiback_queue_tm_rsp,
1844 .aborted_task = scsiback_aborted_task,
1846 * Setup callers for generic logic in target_core_fabric_configfs.c
1848 .fabric_make_wwn = scsiback_make_tport,
1849 .fabric_drop_wwn = scsiback_drop_tport,
1850 .fabric_make_tpg = scsiback_make_tpg,
1851 .fabric_drop_tpg = scsiback_drop_tpg,
1852 .fabric_post_link = scsiback_port_link,
1853 .fabric_pre_unlink = scsiback_port_unlink,
1855 .tfc_wwn_attrs = scsiback_wwn_attrs,
1856 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1857 .tfc_tpg_param_attrs = scsiback_param_attrs,
1860 static const struct xenbus_device_id scsiback_ids[] = {
1861 { "vscsi" },
1862 { "" }
1865 static struct xenbus_driver scsiback_driver = {
1866 .ids = scsiback_ids,
1867 .probe = scsiback_probe,
1868 .remove = scsiback_remove,
1869 .otherend_changed = scsiback_frontend_changed
1872 static int __init scsiback_init(void)
1874 int ret;
1876 if (!xen_domain())
1877 return -ENODEV;
1879 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1880 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1882 ret = xenbus_register_backend(&scsiback_driver);
1883 if (ret)
1884 goto out;
1886 ret = target_register_template(&scsiback_ops);
1887 if (ret)
1888 goto out_unregister_xenbus;
1890 return 0;
1892 out_unregister_xenbus:
1893 xenbus_unregister_driver(&scsiback_driver);
1894 out:
1895 pr_err("%s: error %d\n", __func__, ret);
1896 return ret;
1899 static void __exit scsiback_exit(void)
1901 struct page *page;
1903 while (free_pages_num) {
1904 if (get_free_page(&page))
1905 BUG();
1906 gnttab_free_pages(1, &page);
1908 target_unregister_template(&scsiback_ops);
1909 xenbus_unregister_driver(&scsiback_driver);
1912 module_init(scsiback_init);
1913 module_exit(scsiback_exit);
1915 MODULE_DESCRIPTION("Xen SCSI backend driver");
1916 MODULE_LICENSE("Dual BSD/GPL");
1917 MODULE_ALIAS("xen-backend:vscsi");
1918 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");