Loading...
1/*
2 * Xen SCSI backend driver
3 *
4 * Copyright (c) 2008, FUJITSU Limited
5 *
6 * Based on the blkback driver code.
7 * Adaption to kernel taget core infrastructure taken from vhost/scsi.c
8 *
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:
14 *
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:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
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.
32 */
33
34#define pr_fmt(fmt) "xen-pvscsi: " fmt
35
36#include <stdarg.h>
37
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>
49
50#include <generated/utsrelease.h>
51
52#include <scsi/scsi_host.h> /* SG_ALL */
53
54#include <target/target_core_base.h>
55#include <target/target_core_fabric.h>
56
57#include <asm/hypervisor.h>
58
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>
65
66#include <xen/interface/grant_table.h>
67#include <xen/interface/io/vscsiif.h>
68
69#define VSCSI_VERSION "v0.1"
70#define VSCSI_NAMELEN 32
71
72struct ids_tuple {
73 unsigned int hst; /* host */
74 unsigned int chn; /* channel */
75 unsigned int tgt; /* target */
76 unsigned int lun; /* LUN */
77};
78
79struct 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;
85};
86
87struct vscsibk_info {
88 struct xenbus_device *dev;
89
90 domid_t domid;
91 unsigned int irq;
92
93 struct vscsiif_back_ring ring;
94 int ring_error;
95
96 spinlock_t ring_lock;
97 atomic_t nr_unreplied_reqs;
98
99 spinlock_t v2p_lock;
100 struct list_head v2p_entry_lists;
101
102 wait_queue_head_t waiting_to_free;
103};
104
105/* theoretical maximum of grants for one request */
106#define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
107
108/*
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.
112 */
113#define VSCSI_GRANT_BATCH 16
114
115struct vscsibk_pend {
116 uint16_t rqid;
117
118 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
119 uint8_t cmd_len;
120
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;
126
127 struct vscsibk_info *info;
128 struct v2p_entry *v2p;
129 struct scatterlist *sgl;
130
131 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
132
133 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
134 struct page *pages[VSCSI_MAX_GRANTS];
135
136 struct se_cmd se_cmd;
137};
138
139struct scsiback_tmr {
140 atomic_t tmr_complete;
141 wait_queue_head_t tmr_wait;
142};
143
144#define VSCSI_DEFAULT_SESSION_TAGS 128
145
146struct scsiback_nexus {
147 /* Pointer to TCM session for I_T Nexus */
148 struct se_session *tvn_se_sess;
149};
150
151struct 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;
160};
161
162struct 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;
183};
184
185#define SCSIBACK_INVALID_HANDLE (~0)
186
187static bool log_print_stat;
188module_param(log_print_stat, bool, 0644);
189
190static int scsiback_max_buffer_pages = 1024;
191module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
192MODULE_PARM_DESC(max_buffer_pages,
193"Maximum number of free pages to keep in backend buffer");
194
195static DEFINE_SPINLOCK(free_pages_lock);
196static int free_pages_num;
197static LIST_HEAD(scsiback_free_pages);
198
199/* Global spinlock to protect scsiback TPG list */
200static DEFINE_MUTEX(scsiback_mutex);
201static LIST_HEAD(scsiback_list);
202
203static void scsiback_get(struct vscsibk_info *info)
204{
205 atomic_inc(&info->nr_unreplied_reqs);
206}
207
208static void scsiback_put(struct vscsibk_info *info)
209{
210 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
211 wake_up(&info->waiting_to_free);
212}
213
214static void put_free_pages(struct page **page, int num)
215{
216 unsigned long flags;
217 int i = free_pages_num + num, n = num;
218
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;
225 }
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);
231}
232
233static int get_free_page(struct page **page)
234{
235 unsigned long flags;
236
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);
241 }
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;
247}
248
249static unsigned long vaddr_page(struct page *page)
250{
251 unsigned long pfn = page_to_pfn(page);
252
253 return (unsigned long)pfn_to_kaddr(pfn);
254}
255
256static unsigned long vaddr(struct vscsibk_pend *req, int seg)
257{
258 return vaddr_page(req->pages[seg]);
259}
260
261static void scsiback_print_status(char *sense_buffer, int errors,
262 struct vscsibk_pend *pending_req)
263{
264 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
265
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));
270}
271
272static void scsiback_fast_flush_area(struct vscsibk_pend *req)
273{
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;
279
280 kfree(req->sgl);
281 req->sgl = NULL;
282 req->n_sg = 0;
283
284 if (!req->n_grants)
285 return;
286
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;
302 }
303
304 if (invcount) {
305 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
306 BUG_ON(err);
307 }
308
309 put_free_pages(req->pages, req->n_grants);
310 req->n_grants = 0;
311}
312
313static void scsiback_free_translation_entry(struct kref *kref)
314{
315 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
316 struct scsiback_tpg *tpg = entry->tpg;
317
318 mutex_lock(&tpg->tv_tpg_mutex);
319 tpg->tv_tpg_fe_count--;
320 mutex_unlock(&tpg->tv_tpg_mutex);
321
322 kfree(entry);
323}
324
325static void scsiback_send_response(struct vscsibk_info *info,
326 char *sense_buffer, int32_t result, uint32_t resid,
327 uint16_t rqid)
328{
329 struct vscsiif_response *ring_res;
330 int notify;
331 struct scsi_sense_hdr sshdr;
332 unsigned long flags;
333 unsigned len;
334
335 spin_lock_irqsave(&info->ring_lock, flags);
336
337 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
338 info->ring.rsp_prod_pvt++;
339
340 ring_res->rslt = result;
341 ring_res->rqid = rqid;
342
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;
352 }
353
354 ring_res->residual_len = resid;
355
356 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
357 spin_unlock_irqrestore(&info->ring_lock, flags);
358
359 if (notify)
360 notify_remote_via_irq(info->irq);
361}
362
363static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
364 uint32_t resid, struct vscsibk_pend *pending_req)
365{
366 scsiback_send_response(pending_req->info, sense_buffer, result,
367 resid, pending_req->rqid);
368
369 if (pending_req->v2p)
370 kref_put(&pending_req->v2p->kref,
371 scsiback_free_translation_entry);
372}
373
374static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
375{
376 struct vscsibk_info *info = pending_req->info;
377 unsigned char *sense_buffer;
378 unsigned int resid;
379 int errors;
380
381 sense_buffer = pending_req->sense_buffer;
382 resid = pending_req->se_cmd.residual_count;
383 errors = pending_req->result;
384
385 if (errors && log_print_stat)
386 scsiback_print_status(sense_buffer, errors, pending_req);
387
388 scsiback_fast_flush_area(pending_req);
389 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
390 scsiback_put(info);
391 /*
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.
395 */
396 target_put_sess_cmd(&pending_req->se_cmd);
397}
398
399static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
400{
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;
404
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);
417 }
418}
419
420static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
421 struct page **pg, grant_handle_t *grant, int cnt)
422{
423 int err, i;
424
425 if (!cnt)
426 return 0;
427
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]);
437 }
438 grant[i] = map[i].handle;
439 }
440 return err;
441}
442
443static 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)
446{
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;
450
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;
456 }
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;
469 }
470 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
471 pending_req->n_grants += mapcount;
472 return err;
473}
474
475static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
476 struct vscsibk_pend *pending_req)
477{
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;
487
488 pending_req->n_sg = 0;
489 pending_req->n_grants = 0;
490 pending_req->data_len = 0;
491
492 nr_segments &= ~VSCSIIF_SG_GRANT;
493 if (!nr_segments)
494 return 0;
495
496 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
497 pr_debug("invalid parameter nr_seg = %d\n",
498 ring_req->nr_segments);
499 return -EINVAL;
500 }
501
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;
519 }
520 if (nr_segments > SG_ALL) {
521 pr_debug("invalid nr_seg = %d\n", nr_segments);
522 return -EINVAL;
523 }
524 }
525
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;
531
532 sg_init_table(pending_req->sgl, nr_segments);
533 pending_req->n_sg = nr_segments;
534
535 flags = GNTMAP_host_map;
536 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
537 flags |= GNTMAP_readonly;
538
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;
559 }
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;
564 }
565
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;
576 }
577 if (sg->offset >= PAGE_SIZE ||
578 sg->length > PAGE_SIZE ||
579 sg->offset + sg->length > PAGE_SIZE)
580 return -EINVAL;
581 }
582
583 return 0;
584}
585
586static void scsiback_disconnect(struct vscsibk_info *info)
587{
588 wait_event(info->waiting_to_free,
589 atomic_read(&info->nr_unreplied_reqs) == 0);
590
591 unbind_from_irqhandler(info->irq, info);
592 info->irq = 0;
593 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
594}
595
596static void scsiback_device_action(struct vscsibk_pend *pending_req,
597 enum tcm_tmreq_table act, int tag)
598{
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;
605
606 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
607 if (!tmr) {
608 target_put_sess_cmd(se_cmd);
609 goto err;
610 }
611
612 init_waitqueue_head(&tmr->tmr_wait);
613
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;
620
621 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
622
623 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
624 SUCCESS : FAILED;
625
626 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
627 transport_generic_free_cmd(&pending_req->se_cmd, 1);
628 return;
629err:
630 if (tmr)
631 kfree(tmr);
632 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
633}
634
635/*
636 Perform virtual to physical translation
637*/
638static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
639 struct ids_tuple *v)
640{
641 struct v2p_entry *entry;
642 struct list_head *head = &(info->v2p_entry_lists);
643 unsigned long flags;
644
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;
652 }
653 }
654 entry = NULL;
655
656out:
657 spin_unlock_irqrestore(&info->v2p_lock, flags);
658 return entry;
659}
660
661static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring,
662 struct v2p_entry *v2p)
663{
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;
669
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);
674 }
675
676 req = &((struct vscsibk_pend *)se_sess->sess_cmd_map)[tag];
677 memset(req, 0, sizeof(*req));
678 req->se_cmd.map_tag = tag;
679
680 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
681 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
682
683 return req;
684}
685
686static struct vscsibk_pend *prepare_pending_reqs(struct vscsibk_info *info,
687 struct vscsiif_back_ring *ring,
688 struct vscsiif_request *ring_req)
689{
690 struct vscsibk_pend *pending_req;
691 struct v2p_entry *v2p;
692 struct ids_tuple vir;
693
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);
702 }
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);
707 }
708
709 vir.chn = ring_req->channel;
710 vir.tgt = ring_req->id;
711 vir.lun = ring_req->lun;
712
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);
718 }
719
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);
724 }
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);
731
732 return pending_req;
733}
734
735static int scsiback_do_cmd_fn(struct vscsibk_info *info)
736{
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;
743
744 rc = ring->req_cons;
745 rp = ring->sring->req_prod;
746 rmb(); /* guest system is accessing ring, too */
747
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;
754 }
755
756 while ((rc != rp)) {
757 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
758 break;
759
760 RING_COPY_REQUEST(ring, rc, &ring_req);
761 ring->req_cons = ++rc;
762
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;
772 }
773 scsiback_send_response(info, NULL, result << 24, 0,
774 ring_req.rqid);
775 return 1;
776 }
777
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);
787 }
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;
802 }
803
804 /* Yield point for this unbounded loop. */
805 cond_resched();
806 }
807
808 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
809 return more_to_do;
810}
811
812static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
813{
814 struct vscsibk_info *info = dev_id;
815
816 if (info->ring_error)
817 return IRQ_HANDLED;
818
819 while (scsiback_do_cmd_fn(info))
820 cond_resched();
821
822 return IRQ_HANDLED;
823}
824
825static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
826 evtchn_port_t evtchn)
827{
828 void *area;
829 struct vscsiif_sring *sring;
830 int err;
831
832 if (info->irq)
833 return -1;
834
835 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
836 if (err)
837 return err;
838
839 sring = (struct vscsiif_sring *)area;
840 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
841
842 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
843 if (err < 0)
844 goto unmap_page;
845
846 info->irq = err;
847
848 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
849 IRQF_ONESHOT, "vscsiif-backend", info);
850 if (err)
851 goto free_irq;
852
853 return 0;
854
855free_irq:
856 unbind_from_irqhandler(info->irq, info);
857 info->irq = 0;
858unmap_page:
859 xenbus_unmap_ring_vfree(info->dev, area);
860
861 return err;
862}
863
864static int scsiback_map(struct vscsibk_info *info)
865{
866 struct xenbus_device *dev = info->dev;
867 unsigned int ring_ref, evtchn;
868 int err;
869
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;
876 }
877
878 return scsiback_init_sring(info, ring_ref, evtchn);
879}
880
881/*
882 Check for a translation entry being present
883*/
884static struct v2p_entry *scsiback_chk_translation_entry(
885 struct vscsibk_info *info, struct ids_tuple *v)
886{
887 struct list_head *head = &(info->v2p_entry_lists);
888 struct v2p_entry *entry;
889
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;
895
896 return NULL;
897}
898
899/*
900 Add a new translation entry
901*/
902static int scsiback_add_translation_entry(struct vscsibk_info *info,
903 char *phy, struct ids_tuple *v)
904{
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";
913
914 lunp = strrchr(phy, ':');
915 if (!lunp) {
916 pr_err("illegal format of physical device %s\n", phy);
917 return -EINVAL;
918 }
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;
925 }
926
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;
939 }
940 }
941 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
942 break;
943 }
944 }
945 if (tpg) {
946 mutex_lock(&tpg->tv_tpg_mutex);
947 tpg->tv_tpg_fe_count++;
948 mutex_unlock(&tpg->tv_tpg_mutex);
949 }
950 mutex_unlock(&scsiback_mutex);
951
952 if (!tpg) {
953 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
954 return -ENODEV;
955 }
956
957 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
958 if (new == NULL) {
959 err = -ENOMEM;
960 goto out_free;
961 }
962
963 spin_lock_irqsave(&info->v2p_lock, flags);
964
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;
970 }
971
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);
978
979out:
980 spin_unlock_irqrestore(&info->v2p_lock, flags);
981
982out_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);
988 }
989
990 return err;
991}
992
993static void __scsiback_del_translation_entry(struct v2p_entry *entry)
994{
995 list_del(&entry->l);
996 kref_put(&entry->kref, scsiback_free_translation_entry);
997}
998
999/*
1000 Delete the translation entry specified
1001*/
1002static int scsiback_del_translation_entry(struct vscsibk_info *info,
1003 struct ids_tuple *v)
1004{
1005 struct v2p_entry *entry;
1006 unsigned long flags;
1007 int ret = 0;
1008
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;
1016
1017 spin_unlock_irqrestore(&info->v2p_lock, flags);
1018 return ret;
1019}
1020
1021static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
1022 char *phy, struct ids_tuple *vir, int try)
1023{
1024 struct v2p_entry *entry;
1025 unsigned long flags;
1026
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;
1033 }
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);
1039 }
1040 } else if (!try) {
1041 xenbus_printf(XBT_NIL, info->dev->nodename, state,
1042 "%d", XenbusStateClosed);
1043 }
1044}
1045
1046static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1047 struct ids_tuple *vir)
1048{
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);
1053 }
1054}
1055
1056#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1057#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1058
1059static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1060 char *ent)
1061{
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;
1070
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;
1076
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;
1084 }
1085 strlcpy(phy, val, VSCSI_NAMELEN);
1086 kfree(val);
1087
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;
1096 }
1097
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;
1112 }
1113 break;
1114
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);
1124 }
1125 }
1126 break;
1127 /* When it is necessary, processing is added here. */
1128 default:
1129 break;
1130 }
1131}
1132
1133static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1134{
1135 int i;
1136 char **dir;
1137 unsigned int ndir = 0;
1138
1139 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1140 &ndir);
1141 if (IS_ERR(dir))
1142 return;
1143
1144 for (i = 0; i < ndir; i++)
1145 scsiback_do_1lun_hotplug(info, op, dir[i]);
1146
1147 kfree(dir);
1148}
1149
1150static void scsiback_frontend_changed(struct xenbus_device *dev,
1151 enum xenbus_state frontend_state)
1152{
1153 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1154
1155 switch (frontend_state) {
1156 case XenbusStateInitialising:
1157 break;
1158
1159 case XenbusStateInitialised:
1160 if (scsiback_map(info))
1161 break;
1162
1163 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1164 xenbus_switch_state(dev, XenbusStateConnected);
1165 break;
1166
1167 case XenbusStateConnected:
1168 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1169
1170 if (dev->state == XenbusStateConnected)
1171 break;
1172
1173 xenbus_switch_state(dev, XenbusStateConnected);
1174 break;
1175
1176 case XenbusStateClosing:
1177 if (info->irq)
1178 scsiback_disconnect(info);
1179
1180 xenbus_switch_state(dev, XenbusStateClosing);
1181 break;
1182
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;
1191
1192 case XenbusStateReconfiguring:
1193 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1194 xenbus_switch_state(dev, XenbusStateReconfigured);
1195
1196 break;
1197
1198 default:
1199 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1200 frontend_state);
1201 break;
1202 }
1203}
1204
1205/*
1206 Release the translation entry specfied
1207*/
1208static void scsiback_release_translation_entry(struct vscsibk_info *info)
1209{
1210 struct v2p_entry *entry, *tmp;
1211 struct list_head *head = &(info->v2p_entry_lists);
1212 unsigned long flags;
1213
1214 spin_lock_irqsave(&info->v2p_lock, flags);
1215
1216 list_for_each_entry_safe(entry, tmp, head, l)
1217 __scsiback_del_translation_entry(entry);
1218
1219 spin_unlock_irqrestore(&info->v2p_lock, flags);
1220}
1221
1222static int scsiback_remove(struct xenbus_device *dev)
1223{
1224 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1225
1226 if (info->irq)
1227 scsiback_disconnect(info);
1228
1229 scsiback_release_translation_entry(info);
1230
1231 dev_set_drvdata(&dev->dev, NULL);
1232
1233 return 0;
1234}
1235
1236static int scsiback_probe(struct xenbus_device *dev,
1237 const struct xenbus_device_id *id)
1238{
1239 int err;
1240
1241 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1242 GFP_KERNEL);
1243
1244 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
1245
1246 if (!info) {
1247 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1248 return -ENOMEM;
1249 }
1250 info->dev = dev;
1251 dev_set_drvdata(&dev->dev, info);
1252
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);
1262
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");
1267
1268 err = xenbus_switch_state(dev, XenbusStateInitWait);
1269 if (err)
1270 goto fail;
1271
1272 return 0;
1273
1274fail:
1275 pr_warn("%s failed\n", __func__);
1276 scsiback_remove(dev);
1277
1278 return err;
1279}
1280
1281static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1282{
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;
1292 }
1293
1294 return "Unknown";
1295}
1296
1297static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1298{
1299 struct scsiback_tpg *tpg = container_of(se_tpg,
1300 struct scsiback_tpg, se_tpg);
1301 struct scsiback_tport *tport = tpg->tport;
1302
1303 return &tport->tport_name[0];
1304}
1305
1306static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1307{
1308 struct scsiback_tpg *tpg = container_of(se_tpg,
1309 struct scsiback_tpg, se_tpg);
1310 return tpg->tport_tpgt;
1311}
1312
1313static struct se_wwn *
1314scsiback_make_tport(struct target_fabric_configfs *tf,
1315 struct config_group *group,
1316 const char *name)
1317{
1318 struct scsiback_tport *tport;
1319 char *ptr;
1320 u64 wwpn = 0;
1321 int off = 0;
1322
1323 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1324 if (!tport)
1325 return ERR_PTR(-ENOMEM);
1326
1327 tport->tport_wwpn = wwpn;
1328 /*
1329 * Determine the emulated Protocol Identifier and Target Port Name
1330 * based on the incoming configfs directory name.
1331 */
1332 ptr = strstr(name, "naa.");
1333 if (ptr) {
1334 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1335 goto check_len;
1336 }
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;
1342 }
1343 ptr = strstr(name, "iqn.");
1344 if (ptr) {
1345 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1346 goto check_len;
1347 }
1348
1349 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1350 kfree(tport);
1351 return ERR_PTR(-EINVAL);
1352
1353check_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);
1359 }
1360 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1361
1362 pr_debug("Allocated emulated Target %s Address: %s\n",
1363 scsiback_dump_proto_id(tport), name);
1364
1365 return &tport->tport_wwn;
1366}
1367
1368static void scsiback_drop_tport(struct se_wwn *wwn)
1369{
1370 struct scsiback_tport *tport = container_of(wwn,
1371 struct scsiback_tport, tport_wwn);
1372
1373 pr_debug("Deallocating emulated Target %s Address: %s\n",
1374 scsiback_dump_proto_id(tport), tport->tport_name);
1375
1376 kfree(tport);
1377}
1378
1379static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1380{
1381 return 1;
1382}
1383
1384static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1385{
1386 return transport_generic_free_cmd(se_cmd, 0);
1387}
1388
1389static void scsiback_release_cmd(struct se_cmd *se_cmd)
1390{
1391 struct se_session *se_sess = se_cmd->se_sess;
1392 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1393
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);
1397 }
1398
1399 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
1400}
1401
1402static int scsiback_shutdown_session(struct se_session *se_sess)
1403{
1404 return 0;
1405}
1406
1407static void scsiback_close_session(struct se_session *se_sess)
1408{
1409}
1410
1411static u32 scsiback_sess_get_index(struct se_session *se_sess)
1412{
1413 return 0;
1414}
1415
1416static int scsiback_write_pending(struct se_cmd *se_cmd)
1417{
1418 /* Go ahead and process the write immediately */
1419 target_execute_cmd(se_cmd);
1420
1421 return 0;
1422}
1423
1424static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1425{
1426 return 0;
1427}
1428
1429static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1430{
1431}
1432
1433static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1434{
1435 return 0;
1436}
1437
1438static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1439{
1440 struct vscsibk_pend *pending_req = container_of(se_cmd,
1441 struct vscsibk_pend, se_cmd);
1442
1443 pending_req->result = SAM_STAT_GOOD;
1444 scsiback_cmd_done(pending_req);
1445 return 0;
1446}
1447
1448static int scsiback_queue_status(struct se_cmd *se_cmd)
1449{
1450 struct vscsibk_pend *pending_req = container_of(se_cmd,
1451 struct vscsibk_pend, se_cmd);
1452
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;
1458 else
1459 pending_req->result = se_cmd->scsi_status;
1460
1461 scsiback_cmd_done(pending_req);
1462 return 0;
1463}
1464
1465static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1466{
1467 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1468 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1469
1470 atomic_set(&tmr->tmr_complete, 1);
1471 wake_up(&tmr->tmr_wait);
1472}
1473
1474static void scsiback_aborted_task(struct se_cmd *se_cmd)
1475{
1476}
1477
1478static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
1479 char *page)
1480{
1481 struct se_portal_group *se_tpg = param_to_tpg(item);
1482 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1483 se_tpg);
1484 ssize_t rb;
1485
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);
1489
1490 return rb;
1491}
1492
1493static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
1494 const char *page, size_t count)
1495{
1496 struct se_portal_group *se_tpg = param_to_tpg(item);
1497 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1498 se_tpg);
1499 int len;
1500
1501 if (strlen(page) >= VSCSI_NAMELEN) {
1502 pr_err("param alias: %s, exceeds max: %d\n", page,
1503 VSCSI_NAMELEN);
1504 return -EINVAL;
1505 }
1506
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);
1512
1513 return count;
1514}
1515
1516CONFIGFS_ATTR(scsiback_tpg_param_, alias);
1517
1518static struct configfs_attribute *scsiback_param_attrs[] = {
1519 &scsiback_tpg_param_attr_alias,
1520 NULL,
1521};
1522
1523static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg,
1524 struct se_session *se_sess, void *p)
1525{
1526 struct scsiback_tpg *tpg = container_of(se_tpg,
1527 struct scsiback_tpg, se_tpg);
1528
1529 tpg->tpg_nexus = p;
1530 return 0;
1531}
1532
1533static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1534 const char *name)
1535{
1536 struct scsiback_nexus *tv_nexus;
1537 int ret = 0;
1538
1539 mutex_lock(&tpg->tv_tpg_mutex);
1540 if (tpg->tpg_nexus) {
1541 pr_debug("tpg->tpg_nexus already exists\n");
1542 ret = -EEXIST;
1543 goto out_unlock;
1544 }
1545
1546 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1547 if (!tv_nexus) {
1548 ret = -ENOMEM;
1549 goto out_unlock;
1550 }
1551
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)) {
1558 kfree(tv_nexus);
1559 ret = -ENOMEM;
1560 goto out_unlock;
1561 }
1562
1563out_unlock:
1564 mutex_unlock(&tpg->tv_tpg_mutex);
1565 return ret;
1566}
1567
1568static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1569{
1570 struct se_session *se_sess;
1571 struct scsiback_nexus *tv_nexus;
1572
1573 mutex_lock(&tpg->tv_tpg_mutex);
1574 tv_nexus = tpg->tpg_nexus;
1575 if (!tv_nexus) {
1576 mutex_unlock(&tpg->tv_tpg_mutex);
1577 return -ENODEV;
1578 }
1579
1580 se_sess = tv_nexus->tvn_se_sess;
1581 if (!se_sess) {
1582 mutex_unlock(&tpg->tv_tpg_mutex);
1583 return -ENODEV;
1584 }
1585
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);
1590 return -EBUSY;
1591 }
1592
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);
1597 return -EBUSY;
1598 }
1599
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);
1603
1604 /*
1605 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1606 */
1607 transport_deregister_session(tv_nexus->tvn_se_sess);
1608 tpg->tpg_nexus = NULL;
1609 mutex_unlock(&tpg->tv_tpg_mutex);
1610
1611 kfree(tv_nexus);
1612 return 0;
1613}
1614
1615static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
1616{
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;
1621 ssize_t ret;
1622
1623 mutex_lock(&tpg->tv_tpg_mutex);
1624 tv_nexus = tpg->tpg_nexus;
1625 if (!tv_nexus) {
1626 mutex_unlock(&tpg->tv_tpg_mutex);
1627 return -ENODEV;
1628 }
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);
1632
1633 return ret;
1634}
1635
1636static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1637 const char *page, size_t count)
1638{
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;
1644 int ret;
1645 /*
1646 * Shutdown the active I_T nexus if 'NULL' is passed.
1647 */
1648 if (!strncmp(page, "NULL", 4)) {
1649 ret = scsiback_drop_nexus(tpg);
1650 return (!ret) ? count : ret;
1651 }
1652 /*
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().
1656 */
1657 if (strlen(page) >= VSCSI_NAMELEN) {
1658 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1659 page, VSCSI_NAMELEN);
1660 return -EINVAL;
1661 }
1662 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1663
1664 ptr = strstr(i_port, "naa.");
1665 if (ptr) {
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));
1669 return -EINVAL;
1670 }
1671 port_ptr = &i_port[0];
1672 goto check_newline;
1673 }
1674 ptr = strstr(i_port, "fc.");
1675 if (ptr) {
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));
1679 return -EINVAL;
1680 }
1681 port_ptr = &i_port[3]; /* Skip over "fc." */
1682 goto check_newline;
1683 }
1684 ptr = strstr(i_port, "iqn.");
1685 if (ptr) {
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));
1689 return -EINVAL;
1690 }
1691 port_ptr = &i_port[0];
1692 goto check_newline;
1693 }
1694 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1695 i_port);
1696 return -EINVAL;
1697 /*
1698 * Clear any trailing newline for the NAA WWN
1699 */
1700check_newline:
1701 if (i_port[strlen(i_port) - 1] == '\n')
1702 i_port[strlen(i_port) - 1] = '\0';
1703
1704 ret = scsiback_make_nexus(tpg, port_ptr);
1705 if (ret < 0)
1706 return ret;
1707
1708 return count;
1709}
1710
1711CONFIGFS_ATTR(scsiback_tpg_, nexus);
1712
1713static struct configfs_attribute *scsiback_tpg_attrs[] = {
1714 &scsiback_tpg_attr_nexus,
1715 NULL,
1716};
1717
1718static ssize_t
1719scsiback_wwn_version_show(struct config_item *item, char *page)
1720{
1721 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1722 UTS_RELEASE"\n",
1723 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1724}
1725
1726CONFIGFS_ATTR_RO(scsiback_wwn_, version);
1727
1728static struct configfs_attribute *scsiback_wwn_attrs[] = {
1729 &scsiback_wwn_attr_version,
1730 NULL,
1731};
1732
1733static char *scsiback_get_fabric_name(void)
1734{
1735 return "xen-pvscsi";
1736}
1737
1738static int scsiback_port_link(struct se_portal_group *se_tpg,
1739 struct se_lun *lun)
1740{
1741 struct scsiback_tpg *tpg = container_of(se_tpg,
1742 struct scsiback_tpg, se_tpg);
1743
1744 mutex_lock(&tpg->tv_tpg_mutex);
1745 tpg->tv_tpg_port_count++;
1746 mutex_unlock(&tpg->tv_tpg_mutex);
1747
1748 return 0;
1749}
1750
1751static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1752 struct se_lun *lun)
1753{
1754 struct scsiback_tpg *tpg = container_of(se_tpg,
1755 struct scsiback_tpg, se_tpg);
1756
1757 mutex_lock(&tpg->tv_tpg_mutex);
1758 tpg->tv_tpg_port_count--;
1759 mutex_unlock(&tpg->tv_tpg_mutex);
1760}
1761
1762static struct se_portal_group *
1763scsiback_make_tpg(struct se_wwn *wwn,
1764 struct config_group *group,
1765 const char *name)
1766{
1767 struct scsiback_tport *tport = container_of(wwn,
1768 struct scsiback_tport, tport_wwn);
1769
1770 struct scsiback_tpg *tpg;
1771 u16 tpgt;
1772 int ret;
1773
1774 if (strstr(name, "tpgt_") != name)
1775 return ERR_PTR(-EINVAL);
1776 ret = kstrtou16(name + 5, 10, &tpgt);
1777 if (ret)
1778 return ERR_PTR(ret);
1779
1780 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1781 if (!tpg)
1782 return ERR_PTR(-ENOMEM);
1783
1784 mutex_init(&tpg->tv_tpg_mutex);
1785 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1786 INIT_LIST_HEAD(&tpg->info_list);
1787 tpg->tport = tport;
1788 tpg->tport_tpgt = tpgt;
1789
1790 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
1791 if (ret < 0) {
1792 kfree(tpg);
1793 return NULL;
1794 }
1795 mutex_lock(&scsiback_mutex);
1796 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1797 mutex_unlock(&scsiback_mutex);
1798
1799 return &tpg->se_tpg;
1800}
1801
1802static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1803{
1804 struct scsiback_tpg *tpg = container_of(se_tpg,
1805 struct scsiback_tpg, se_tpg);
1806
1807 mutex_lock(&scsiback_mutex);
1808 list_del(&tpg->tv_tpg_list);
1809 mutex_unlock(&scsiback_mutex);
1810 /*
1811 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1812 */
1813 scsiback_drop_nexus(tpg);
1814 /*
1815 * Deregister the se_tpg from TCM.
1816 */
1817 core_tpg_deregister(se_tpg);
1818 kfree(tpg);
1819}
1820
1821static int scsiback_check_true(struct se_portal_group *se_tpg)
1822{
1823 return 1;
1824}
1825
1826static int scsiback_check_false(struct se_portal_group *se_tpg)
1827{
1828 return 0;
1829}
1830
1831static 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,
1856 /*
1857 * Setup callers for generic logic in target_core_fabric_configfs.c
1858 */
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,
1865
1866 .tfc_wwn_attrs = scsiback_wwn_attrs,
1867 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1868 .tfc_tpg_param_attrs = scsiback_param_attrs,
1869};
1870
1871static const struct xenbus_device_id scsiback_ids[] = {
1872 { "vscsi" },
1873 { "" }
1874};
1875
1876static struct xenbus_driver scsiback_driver = {
1877 .ids = scsiback_ids,
1878 .probe = scsiback_probe,
1879 .remove = scsiback_remove,
1880 .otherend_changed = scsiback_frontend_changed
1881};
1882
1883static int __init scsiback_init(void)
1884{
1885 int ret;
1886
1887 if (!xen_domain())
1888 return -ENODEV;
1889
1890 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1891 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1892
1893 ret = xenbus_register_backend(&scsiback_driver);
1894 if (ret)
1895 goto out;
1896
1897 ret = target_register_template(&scsiback_ops);
1898 if (ret)
1899 goto out_unregister_xenbus;
1900
1901 return 0;
1902
1903out_unregister_xenbus:
1904 xenbus_unregister_driver(&scsiback_driver);
1905out:
1906 pr_err("%s: error %d\n", __func__, ret);
1907 return ret;
1908}
1909
1910static void __exit scsiback_exit(void)
1911{
1912 struct page *page;
1913
1914 while (free_pages_num) {
1915 if (get_free_page(&page))
1916 BUG();
1917 gnttab_free_pages(1, &page);
1918 }
1919 target_unregister_template(&scsiback_ops);
1920 xenbus_unregister_driver(&scsiback_driver);
1921}
1922
1923module_init(scsiback_init);
1924module_exit(scsiback_exit);
1925
1926MODULE_DESCRIPTION("Xen SCSI backend driver");
1927MODULE_LICENSE("Dual BSD/GPL");
1928MODULE_ALIAS("xen-backend:vscsi");
1929MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");
1/*
2 * Xen SCSI backend driver
3 *
4 * Copyright (c) 2008, FUJITSU Limited
5 *
6 * Based on the blkback driver code.
7 * Adaption to kernel taget core infrastructure taken from vhost/scsi.c
8 *
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:
14 *
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:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
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.
32 */
33
34#define pr_fmt(fmt) "xen-pvscsi: " fmt
35
36#include <stdarg.h>
37
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>
49
50#include <generated/utsrelease.h>
51
52#include <scsi/scsi_host.h> /* SG_ALL */
53
54#include <target/target_core_base.h>
55#include <target/target_core_fabric.h>
56
57#include <asm/hypervisor.h>
58
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>
65
66#include <xen/interface/grant_table.h>
67#include <xen/interface/io/vscsiif.h>
68
69#define VSCSI_VERSION "v0.1"
70#define VSCSI_NAMELEN 32
71
72struct ids_tuple {
73 unsigned int hst; /* host */
74 unsigned int chn; /* channel */
75 unsigned int tgt; /* target */
76 unsigned int lun; /* LUN */
77};
78
79struct 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;
85};
86
87struct vscsibk_info {
88 struct xenbus_device *dev;
89
90 domid_t domid;
91 unsigned int irq;
92
93 struct vscsiif_back_ring ring;
94
95 spinlock_t ring_lock;
96 atomic_t nr_unreplied_reqs;
97
98 spinlock_t v2p_lock;
99 struct list_head v2p_entry_lists;
100
101 wait_queue_head_t waiting_to_free;
102
103 struct gnttab_page_cache free_pages;
104};
105
106/* theoretical maximum of grants for one request */
107#define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
108
109/*
110 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
111 * call to map/unmap grants. Don't choose it too large, as there are arrays
112 * with VSCSI_GRANT_BATCH elements allocated on the stack.
113 */
114#define VSCSI_GRANT_BATCH 16
115
116struct vscsibk_pend {
117 uint16_t rqid;
118
119 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
120 uint8_t cmd_len;
121
122 uint8_t sc_data_direction;
123 uint16_t n_sg; /* real length of SG list */
124 uint16_t n_grants; /* SG pages and potentially SG list */
125 uint32_t data_len;
126 uint32_t result;
127
128 struct vscsibk_info *info;
129 struct v2p_entry *v2p;
130 struct scatterlist *sgl;
131
132 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
133
134 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
135 struct page *pages[VSCSI_MAX_GRANTS];
136
137 struct se_cmd se_cmd;
138
139 struct completion tmr_done;
140};
141
142#define VSCSI_DEFAULT_SESSION_TAGS 128
143
144struct scsiback_nexus {
145 /* Pointer to TCM session for I_T Nexus */
146 struct se_session *tvn_se_sess;
147};
148
149struct scsiback_tport {
150 /* SCSI protocol the tport is providing */
151 u8 tport_proto_id;
152 /* Binary World Wide unique Port Name for pvscsi Target port */
153 u64 tport_wwpn;
154 /* ASCII formatted WWPN for pvscsi Target port */
155 char tport_name[VSCSI_NAMELEN];
156 /* Returned by scsiback_make_tport() */
157 struct se_wwn tport_wwn;
158};
159
160struct scsiback_tpg {
161 /* scsiback port target portal group tag for TCM */
162 u16 tport_tpgt;
163 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
164 int tv_tpg_port_count;
165 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
166 int tv_tpg_fe_count;
167 /* list for scsiback_list */
168 struct list_head tv_tpg_list;
169 /* Used to protect access for tpg_nexus */
170 struct mutex tv_tpg_mutex;
171 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
172 struct scsiback_nexus *tpg_nexus;
173 /* Pointer back to scsiback_tport */
174 struct scsiback_tport *tport;
175 /* Returned by scsiback_make_tpg() */
176 struct se_portal_group se_tpg;
177 /* alias used in xenstore */
178 char param_alias[VSCSI_NAMELEN];
179 /* list of info structures related to this target portal group */
180 struct list_head info_list;
181};
182
183#define SCSIBACK_INVALID_HANDLE (~0)
184
185static bool log_print_stat;
186module_param(log_print_stat, bool, 0644);
187
188static int scsiback_max_buffer_pages = 1024;
189module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
190MODULE_PARM_DESC(max_buffer_pages,
191"Maximum number of free pages to keep in backend buffer");
192
193/* Global spinlock to protect scsiback TPG list */
194static DEFINE_MUTEX(scsiback_mutex);
195static LIST_HEAD(scsiback_list);
196
197static void scsiback_get(struct vscsibk_info *info)
198{
199 atomic_inc(&info->nr_unreplied_reqs);
200}
201
202static void scsiback_put(struct vscsibk_info *info)
203{
204 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
205 wake_up(&info->waiting_to_free);
206}
207
208static unsigned long vaddr_page(struct page *page)
209{
210 unsigned long pfn = page_to_pfn(page);
211
212 return (unsigned long)pfn_to_kaddr(pfn);
213}
214
215static unsigned long vaddr(struct vscsibk_pend *req, int seg)
216{
217 return vaddr_page(req->pages[seg]);
218}
219
220static void scsiback_print_status(char *sense_buffer, int errors,
221 struct vscsibk_pend *pending_req)
222{
223 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
224
225 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x\n",
226 tpg->tport->tport_name, pending_req->v2p->lun,
227 pending_req->cmnd[0], errors & 0xff, COMMAND_COMPLETE,
228 host_byte(errors));
229}
230
231static void scsiback_fast_flush_area(struct vscsibk_pend *req)
232{
233 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
234 struct page *pages[VSCSI_GRANT_BATCH];
235 unsigned int i, invcount = 0;
236 grant_handle_t handle;
237 int err;
238
239 kfree(req->sgl);
240 req->sgl = NULL;
241 req->n_sg = 0;
242
243 if (!req->n_grants)
244 return;
245
246 for (i = 0; i < req->n_grants; i++) {
247 handle = req->grant_handles[i];
248 if (handle == SCSIBACK_INVALID_HANDLE)
249 continue;
250 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
251 GNTMAP_host_map, handle);
252 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
253 pages[invcount] = req->pages[i];
254 put_page(pages[invcount]);
255 invcount++;
256 if (invcount < VSCSI_GRANT_BATCH)
257 continue;
258 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
259 BUG_ON(err);
260 invcount = 0;
261 }
262
263 if (invcount) {
264 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
265 BUG_ON(err);
266 }
267
268 gnttab_page_cache_put(&req->info->free_pages, req->pages,
269 req->n_grants);
270 req->n_grants = 0;
271}
272
273static void scsiback_free_translation_entry(struct kref *kref)
274{
275 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
276 struct scsiback_tpg *tpg = entry->tpg;
277
278 mutex_lock(&tpg->tv_tpg_mutex);
279 tpg->tv_tpg_fe_count--;
280 mutex_unlock(&tpg->tv_tpg_mutex);
281
282 kfree(entry);
283}
284
285static void scsiback_send_response(struct vscsibk_info *info,
286 char *sense_buffer, int32_t result, uint32_t resid,
287 uint16_t rqid)
288{
289 struct vscsiif_response *ring_res;
290 int notify;
291 struct scsi_sense_hdr sshdr;
292 unsigned long flags;
293 unsigned len;
294
295 spin_lock_irqsave(&info->ring_lock, flags);
296
297 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
298 info->ring.rsp_prod_pvt++;
299
300 ring_res->rslt = result;
301 ring_res->rqid = rqid;
302
303 if (sense_buffer != NULL &&
304 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
305 &sshdr)) {
306 len = min_t(unsigned, 8 + sense_buffer[7],
307 VSCSIIF_SENSE_BUFFERSIZE);
308 memcpy(ring_res->sense_buffer, sense_buffer, len);
309 ring_res->sense_len = len;
310 } else {
311 ring_res->sense_len = 0;
312 }
313
314 ring_res->residual_len = resid;
315
316 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
317 spin_unlock_irqrestore(&info->ring_lock, flags);
318
319 if (notify)
320 notify_remote_via_irq(info->irq);
321}
322
323static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
324 uint32_t resid, struct vscsibk_pend *pending_req)
325{
326 scsiback_send_response(pending_req->info, sense_buffer, result,
327 resid, pending_req->rqid);
328
329 if (pending_req->v2p)
330 kref_put(&pending_req->v2p->kref,
331 scsiback_free_translation_entry);
332}
333
334static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
335{
336 struct vscsibk_info *info = pending_req->info;
337 unsigned char *sense_buffer;
338 unsigned int resid;
339 int errors;
340
341 sense_buffer = pending_req->sense_buffer;
342 resid = pending_req->se_cmd.residual_count;
343 errors = pending_req->result;
344
345 if (errors && log_print_stat)
346 scsiback_print_status(sense_buffer, errors, pending_req);
347
348 scsiback_fast_flush_area(pending_req);
349 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
350 scsiback_put(info);
351 /*
352 * Drop the extra KREF_ACK reference taken by target_submit_cmd_map_sgls()
353 * ahead of scsiback_check_stop_free() -> transport_generic_free_cmd()
354 * final se_cmd->cmd_kref put.
355 */
356 target_put_sess_cmd(&pending_req->se_cmd);
357}
358
359static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
360{
361 struct se_cmd *se_cmd = &pending_req->se_cmd;
362 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
363
364 scsiback_get(pending_req->info);
365 se_cmd->tag = pending_req->rqid;
366 target_init_cmd(se_cmd, sess, pending_req->sense_buffer,
367 pending_req->v2p->lun, pending_req->data_len, 0,
368 pending_req->sc_data_direction, TARGET_SCF_ACK_KREF);
369
370 if (target_submit_prep(se_cmd, pending_req->cmnd, pending_req->sgl,
371 pending_req->n_sg, NULL, 0, NULL, 0, GFP_KERNEL))
372 return;
373
374 target_submit(se_cmd);
375}
376
377static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
378 struct page **pg, grant_handle_t *grant, int cnt)
379{
380 int err, i;
381
382 if (!cnt)
383 return 0;
384
385 err = gnttab_map_refs(map, NULL, pg, cnt);
386 for (i = 0; i < cnt; i++) {
387 if (unlikely(map[i].status != GNTST_okay)) {
388 pr_err("invalid buffer -- could not remap it\n");
389 map[i].handle = SCSIBACK_INVALID_HANDLE;
390 if (!err)
391 err = -ENOMEM;
392 } else {
393 get_page(pg[i]);
394 }
395 grant[i] = map[i].handle;
396 }
397 return err;
398}
399
400static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
401 struct scsiif_request_segment *seg, struct page **pg,
402 grant_handle_t *grant, int cnt, u32 flags)
403{
404 int mapcount = 0, i, err = 0;
405 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
406 struct vscsibk_info *info = pending_req->info;
407
408 for (i = 0; i < cnt; i++) {
409 if (gnttab_page_cache_get(&info->free_pages, pg + mapcount)) {
410 gnttab_page_cache_put(&info->free_pages, pg, mapcount);
411 pr_err("no grant page\n");
412 return -ENOMEM;
413 }
414 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
415 flags, seg[i].gref, info->domid);
416 mapcount++;
417 if (mapcount < VSCSI_GRANT_BATCH)
418 continue;
419 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
420 pg += mapcount;
421 grant += mapcount;
422 pending_req->n_grants += mapcount;
423 if (err)
424 return err;
425 mapcount = 0;
426 }
427 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
428 pending_req->n_grants += mapcount;
429 return err;
430}
431
432static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
433 struct vscsibk_pend *pending_req)
434{
435 u32 flags;
436 int i, err, n_segs, i_seg = 0;
437 struct page **pg;
438 struct scsiif_request_segment *seg;
439 unsigned long end_seg = 0;
440 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
441 unsigned int nr_sgl = 0;
442 struct scatterlist *sg;
443 grant_handle_t *grant;
444
445 pending_req->n_sg = 0;
446 pending_req->n_grants = 0;
447 pending_req->data_len = 0;
448
449 nr_segments &= ~VSCSIIF_SG_GRANT;
450 if (!nr_segments)
451 return 0;
452
453 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
454 pr_debug("invalid parameter nr_seg = %d\n",
455 ring_req->nr_segments);
456 return -EINVAL;
457 }
458
459 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
460 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
461 pending_req->pages, pending_req->grant_handles,
462 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
463 if (err)
464 return err;
465 nr_sgl = nr_segments;
466 nr_segments = 0;
467 for (i = 0; i < nr_sgl; i++) {
468 n_segs = ring_req->seg[i].length /
469 sizeof(struct scsiif_request_segment);
470 if ((unsigned)ring_req->seg[i].offset +
471 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
472 n_segs * sizeof(struct scsiif_request_segment) !=
473 ring_req->seg[i].length)
474 return -EINVAL;
475 nr_segments += n_segs;
476 }
477 if (nr_segments > SG_ALL) {
478 pr_debug("invalid nr_seg = %d\n", nr_segments);
479 return -EINVAL;
480 }
481 }
482
483 /* free of (sgl) in fast_flush_area() */
484 pending_req->sgl = kmalloc_array(nr_segments,
485 sizeof(struct scatterlist), GFP_KERNEL);
486 if (!pending_req->sgl)
487 return -ENOMEM;
488
489 sg_init_table(pending_req->sgl, nr_segments);
490 pending_req->n_sg = nr_segments;
491
492 flags = GNTMAP_host_map;
493 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
494 flags |= GNTMAP_readonly;
495
496 pg = pending_req->pages + nr_sgl;
497 grant = pending_req->grant_handles + nr_sgl;
498 if (!nr_sgl) {
499 seg = ring_req->seg;
500 err = scsiback_gnttab_data_map_list(pending_req, seg,
501 pg, grant, nr_segments, flags);
502 if (err)
503 return err;
504 } else {
505 for (i = 0; i < nr_sgl; i++) {
506 seg = (struct scsiif_request_segment *)(
507 vaddr(pending_req, i) + ring_req->seg[i].offset);
508 n_segs = ring_req->seg[i].length /
509 sizeof(struct scsiif_request_segment);
510 err = scsiback_gnttab_data_map_list(pending_req, seg,
511 pg, grant, n_segs, flags);
512 if (err)
513 return err;
514 pg += n_segs;
515 grant += n_segs;
516 }
517 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
518 seg = (struct scsiif_request_segment *)end_seg;
519 end_seg += ring_req->seg[0].length;
520 pg = pending_req->pages + nr_sgl;
521 }
522
523 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
524 sg_set_page(sg, pg[i], seg->length, seg->offset);
525 pending_req->data_len += seg->length;
526 seg++;
527 if (nr_sgl && (unsigned long)seg >= end_seg) {
528 i_seg++;
529 end_seg = vaddr(pending_req, i_seg) +
530 ring_req->seg[i_seg].offset;
531 seg = (struct scsiif_request_segment *)end_seg;
532 end_seg += ring_req->seg[i_seg].length;
533 }
534 if (sg->offset >= PAGE_SIZE ||
535 sg->length > PAGE_SIZE ||
536 sg->offset + sg->length > PAGE_SIZE)
537 return -EINVAL;
538 }
539
540 return 0;
541}
542
543static void scsiback_disconnect(struct vscsibk_info *info)
544{
545 wait_event(info->waiting_to_free,
546 atomic_read(&info->nr_unreplied_reqs) == 0);
547
548 unbind_from_irqhandler(info->irq, info);
549 info->irq = 0;
550 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
551}
552
553static void scsiback_device_action(struct vscsibk_pend *pending_req,
554 enum tcm_tmreq_table act, int tag)
555{
556 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
557 struct scsiback_nexus *nexus = tpg->tpg_nexus;
558 struct se_cmd *se_cmd = &pending_req->se_cmd;
559 u64 unpacked_lun = pending_req->v2p->lun;
560 int rc, err = FAILED;
561
562 init_completion(&pending_req->tmr_done);
563
564 rc = target_submit_tmr(&pending_req->se_cmd, nexus->tvn_se_sess,
565 &pending_req->sense_buffer[0],
566 unpacked_lun, NULL, act, GFP_KERNEL,
567 tag, TARGET_SCF_ACK_KREF);
568 if (rc)
569 goto err;
570
571 wait_for_completion(&pending_req->tmr_done);
572
573 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
574 SUCCESS : FAILED;
575
576 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
577 transport_generic_free_cmd(&pending_req->se_cmd, 0);
578 return;
579
580err:
581 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
582}
583
584/*
585 Perform virtual to physical translation
586*/
587static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
588 struct ids_tuple *v)
589{
590 struct v2p_entry *entry;
591 struct list_head *head = &(info->v2p_entry_lists);
592 unsigned long flags;
593
594 spin_lock_irqsave(&info->v2p_lock, flags);
595 list_for_each_entry(entry, head, l) {
596 if ((entry->v.chn == v->chn) &&
597 (entry->v.tgt == v->tgt) &&
598 (entry->v.lun == v->lun)) {
599 kref_get(&entry->kref);
600 goto out;
601 }
602 }
603 entry = NULL;
604
605out:
606 spin_unlock_irqrestore(&info->v2p_lock, flags);
607 return entry;
608}
609
610static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring,
611 struct v2p_entry *v2p)
612{
613 struct scsiback_tpg *tpg = v2p->tpg;
614 struct scsiback_nexus *nexus = tpg->tpg_nexus;
615 struct se_session *se_sess = nexus->tvn_se_sess;
616 struct vscsibk_pend *req;
617 int tag, cpu, i;
618
619 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
620 if (tag < 0) {
621 pr_err("Unable to obtain tag for vscsiif_request\n");
622 return ERR_PTR(-ENOMEM);
623 }
624
625 req = &((struct vscsibk_pend *)se_sess->sess_cmd_map)[tag];
626 memset(req, 0, sizeof(*req));
627 req->se_cmd.map_tag = tag;
628 req->se_cmd.map_cpu = cpu;
629
630 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
631 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
632
633 return req;
634}
635
636static struct vscsibk_pend *prepare_pending_reqs(struct vscsibk_info *info,
637 struct vscsiif_back_ring *ring,
638 struct vscsiif_request *ring_req)
639{
640 struct vscsibk_pend *pending_req;
641 struct v2p_entry *v2p;
642 struct ids_tuple vir;
643
644 /* request range check from frontend */
645 if ((ring_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
646 (ring_req->sc_data_direction != DMA_TO_DEVICE) &&
647 (ring_req->sc_data_direction != DMA_FROM_DEVICE) &&
648 (ring_req->sc_data_direction != DMA_NONE)) {
649 pr_debug("invalid parameter data_dir = %d\n",
650 ring_req->sc_data_direction);
651 return ERR_PTR(-EINVAL);
652 }
653 if (ring_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
654 pr_debug("invalid parameter cmd_len = %d\n",
655 ring_req->cmd_len);
656 return ERR_PTR(-EINVAL);
657 }
658
659 vir.chn = ring_req->channel;
660 vir.tgt = ring_req->id;
661 vir.lun = ring_req->lun;
662
663 v2p = scsiback_do_translation(info, &vir);
664 if (!v2p) {
665 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
666 vir.chn, vir.tgt, vir.lun);
667 return ERR_PTR(-ENODEV);
668 }
669
670 pending_req = scsiback_get_pend_req(ring, v2p);
671 if (IS_ERR(pending_req)) {
672 kref_put(&v2p->kref, scsiback_free_translation_entry);
673 return ERR_PTR(-ENOMEM);
674 }
675 pending_req->rqid = ring_req->rqid;
676 pending_req->info = info;
677 pending_req->v2p = v2p;
678 pending_req->sc_data_direction = ring_req->sc_data_direction;
679 pending_req->cmd_len = ring_req->cmd_len;
680 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
681
682 return pending_req;
683}
684
685static int scsiback_do_cmd_fn(struct vscsibk_info *info,
686 unsigned int *eoi_flags)
687{
688 struct vscsiif_back_ring *ring = &info->ring;
689 struct vscsiif_request ring_req;
690 struct vscsibk_pend *pending_req;
691 RING_IDX rc, rp;
692 int more_to_do;
693 uint32_t result;
694
695 rc = ring->req_cons;
696 rp = ring->sring->req_prod;
697 rmb(); /* guest system is accessing ring, too */
698
699 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
700 rc = ring->rsp_prod_pvt;
701 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
702 info->domid, rp, rc, rp - rc);
703 return -EINVAL;
704 }
705
706 while ((rc != rp)) {
707 *eoi_flags &= ~XEN_EOI_FLAG_SPURIOUS;
708
709 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
710 break;
711
712 RING_COPY_REQUEST(ring, rc, &ring_req);
713 ring->req_cons = ++rc;
714
715 pending_req = prepare_pending_reqs(info, ring, &ring_req);
716 if (IS_ERR(pending_req)) {
717 switch (PTR_ERR(pending_req)) {
718 case -ENODEV:
719 result = DID_NO_CONNECT;
720 break;
721 default:
722 result = DID_ERROR;
723 break;
724 }
725 scsiback_send_response(info, NULL, result << 16, 0,
726 ring_req.rqid);
727 return 1;
728 }
729
730 switch (ring_req.act) {
731 case VSCSIIF_ACT_SCSI_CDB:
732 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
733 scsiback_fast_flush_area(pending_req);
734 scsiback_do_resp_with_sense(NULL,
735 DID_ERROR << 16, 0, pending_req);
736 transport_generic_free_cmd(&pending_req->se_cmd, 0);
737 } else {
738 scsiback_cmd_exec(pending_req);
739 }
740 break;
741 case VSCSIIF_ACT_SCSI_ABORT:
742 scsiback_device_action(pending_req, TMR_ABORT_TASK,
743 ring_req.ref_rqid);
744 break;
745 case VSCSIIF_ACT_SCSI_RESET:
746 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
747 break;
748 default:
749 pr_err_ratelimited("invalid request\n");
750 scsiback_do_resp_with_sense(NULL, DID_ERROR << 16, 0,
751 pending_req);
752 transport_generic_free_cmd(&pending_req->se_cmd, 0);
753 break;
754 }
755
756 /* Yield point for this unbounded loop. */
757 cond_resched();
758 }
759
760 gnttab_page_cache_shrink(&info->free_pages, scsiback_max_buffer_pages);
761
762 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
763 return more_to_do;
764}
765
766static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
767{
768 struct vscsibk_info *info = dev_id;
769 int rc;
770 unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
771
772 while ((rc = scsiback_do_cmd_fn(info, &eoi_flags)) > 0)
773 cond_resched();
774
775 /* In case of a ring error we keep the event channel masked. */
776 if (!rc)
777 xen_irq_lateeoi(irq, eoi_flags);
778
779 return IRQ_HANDLED;
780}
781
782static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
783 evtchn_port_t evtchn)
784{
785 void *area;
786 struct vscsiif_sring *sring;
787 int err;
788
789 if (info->irq)
790 return -1;
791
792 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
793 if (err)
794 return err;
795
796 sring = (struct vscsiif_sring *)area;
797 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
798
799 err = bind_interdomain_evtchn_to_irq_lateeoi(info->dev, evtchn);
800 if (err < 0)
801 goto unmap_page;
802
803 info->irq = err;
804
805 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
806 IRQF_ONESHOT, "vscsiif-backend", info);
807 if (err)
808 goto free_irq;
809
810 return 0;
811
812free_irq:
813 unbind_from_irqhandler(info->irq, info);
814 info->irq = 0;
815unmap_page:
816 xenbus_unmap_ring_vfree(info->dev, area);
817
818 return err;
819}
820
821static int scsiback_map(struct vscsibk_info *info)
822{
823 struct xenbus_device *dev = info->dev;
824 unsigned int ring_ref;
825 evtchn_port_t evtchn;
826 int err;
827
828 err = xenbus_gather(XBT_NIL, dev->otherend,
829 "ring-ref", "%u", &ring_ref,
830 "event-channel", "%u", &evtchn, NULL);
831 if (err) {
832 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
833 return err;
834 }
835
836 return scsiback_init_sring(info, ring_ref, evtchn);
837}
838
839/*
840 Check for a translation entry being present
841*/
842static struct v2p_entry *scsiback_chk_translation_entry(
843 struct vscsibk_info *info, struct ids_tuple *v)
844{
845 struct list_head *head = &(info->v2p_entry_lists);
846 struct v2p_entry *entry;
847
848 list_for_each_entry(entry, head, l)
849 if ((entry->v.chn == v->chn) &&
850 (entry->v.tgt == v->tgt) &&
851 (entry->v.lun == v->lun))
852 return entry;
853
854 return NULL;
855}
856
857/*
858 Add a new translation entry
859*/
860static int scsiback_add_translation_entry(struct vscsibk_info *info,
861 char *phy, struct ids_tuple *v)
862{
863 int err = 0;
864 struct v2p_entry *new;
865 unsigned long flags;
866 char *lunp;
867 unsigned long long unpacked_lun;
868 struct se_lun *se_lun;
869 struct scsiback_tpg *tpg_entry, *tpg = NULL;
870 char *error = "doesn't exist";
871
872 lunp = strrchr(phy, ':');
873 if (!lunp) {
874 pr_err("illegal format of physical device %s\n", phy);
875 return -EINVAL;
876 }
877 *lunp = 0;
878 lunp++;
879 err = kstrtoull(lunp, 10, &unpacked_lun);
880 if (err < 0) {
881 pr_err("lun number not valid: %s\n", lunp);
882 return err;
883 }
884
885 mutex_lock(&scsiback_mutex);
886 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
887 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
888 !strcmp(phy, tpg_entry->param_alias)) {
889 mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
890 hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
891 if (se_lun->unpacked_lun == unpacked_lun) {
892 if (!tpg_entry->tpg_nexus)
893 error = "nexus undefined";
894 else
895 tpg = tpg_entry;
896 break;
897 }
898 }
899 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
900 break;
901 }
902 }
903 if (tpg) {
904 mutex_lock(&tpg->tv_tpg_mutex);
905 tpg->tv_tpg_fe_count++;
906 mutex_unlock(&tpg->tv_tpg_mutex);
907 }
908 mutex_unlock(&scsiback_mutex);
909
910 if (!tpg) {
911 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
912 return -ENODEV;
913 }
914
915 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
916 if (new == NULL) {
917 err = -ENOMEM;
918 goto out_free;
919 }
920
921 spin_lock_irqsave(&info->v2p_lock, flags);
922
923 /* Check double assignment to identical virtual ID */
924 if (scsiback_chk_translation_entry(info, v)) {
925 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
926 err = -EEXIST;
927 goto out;
928 }
929
930 /* Create a new translation entry and add to the list */
931 kref_init(&new->kref);
932 new->v = *v;
933 new->tpg = tpg;
934 new->lun = unpacked_lun;
935 list_add_tail(&new->l, &info->v2p_entry_lists);
936
937out:
938 spin_unlock_irqrestore(&info->v2p_lock, flags);
939
940out_free:
941 if (err) {
942 mutex_lock(&tpg->tv_tpg_mutex);
943 tpg->tv_tpg_fe_count--;
944 mutex_unlock(&tpg->tv_tpg_mutex);
945 kfree(new);
946 }
947
948 return err;
949}
950
951static void __scsiback_del_translation_entry(struct v2p_entry *entry)
952{
953 list_del(&entry->l);
954 kref_put(&entry->kref, scsiback_free_translation_entry);
955}
956
957/*
958 Delete the translation entry specified
959*/
960static int scsiback_del_translation_entry(struct vscsibk_info *info,
961 struct ids_tuple *v)
962{
963 struct v2p_entry *entry;
964 unsigned long flags;
965 int ret = 0;
966
967 spin_lock_irqsave(&info->v2p_lock, flags);
968 /* Find out the translation entry specified */
969 entry = scsiback_chk_translation_entry(info, v);
970 if (entry)
971 __scsiback_del_translation_entry(entry);
972 else
973 ret = -ENOENT;
974
975 spin_unlock_irqrestore(&info->v2p_lock, flags);
976 return ret;
977}
978
979static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
980 char *phy, struct ids_tuple *vir, int try)
981{
982 struct v2p_entry *entry;
983 unsigned long flags;
984 int err;
985
986 if (try) {
987 spin_lock_irqsave(&info->v2p_lock, flags);
988 entry = scsiback_chk_translation_entry(info, vir);
989 spin_unlock_irqrestore(&info->v2p_lock, flags);
990 if (entry)
991 return;
992 }
993 if (!scsiback_add_translation_entry(info, phy, vir)) {
994 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
995 "%d", XenbusStateInitialised)) {
996 pr_err("xenbus_printf error %s\n", state);
997 scsiback_del_translation_entry(info, vir);
998 }
999 } else if (!try) {
1000 err = xenbus_printf(XBT_NIL, info->dev->nodename, state,
1001 "%d", XenbusStateClosed);
1002 if (err)
1003 xenbus_dev_error(info->dev, err,
1004 "%s: writing %s", __func__, state);
1005 }
1006}
1007
1008static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1009 struct ids_tuple *vir)
1010{
1011 if (!scsiback_del_translation_entry(info, vir)) {
1012 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1013 "%d", XenbusStateClosed))
1014 pr_err("xenbus_printf error %s\n", state);
1015 }
1016}
1017
1018#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1019#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1020
1021static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1022 char *ent)
1023{
1024 int err;
1025 struct ids_tuple vir;
1026 char *val;
1027 int device_state;
1028 char phy[VSCSI_NAMELEN];
1029 char str[64];
1030 char state[64];
1031 struct xenbus_device *dev = info->dev;
1032
1033 /* read status */
1034 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1035 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1036 if (XENBUS_EXIST_ERR(err))
1037 return;
1038
1039 /* physical SCSI device */
1040 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1041 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1042 if (IS_ERR(val)) {
1043 err = xenbus_printf(XBT_NIL, dev->nodename, state,
1044 "%d", XenbusStateClosed);
1045 if (err)
1046 xenbus_dev_error(info->dev, err,
1047 "%s: writing %s", __func__, state);
1048 return;
1049 }
1050 strlcpy(phy, val, VSCSI_NAMELEN);
1051 kfree(val);
1052
1053 /* virtual SCSI device */
1054 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1055 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1056 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1057 if (XENBUS_EXIST_ERR(err)) {
1058 err = xenbus_printf(XBT_NIL, dev->nodename, state,
1059 "%d", XenbusStateClosed);
1060 if (err)
1061 xenbus_dev_error(info->dev, err,
1062 "%s: writing %s", __func__, state);
1063 return;
1064 }
1065
1066 switch (op) {
1067 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
1068 switch (device_state) {
1069 case XenbusStateInitialising:
1070 scsiback_do_add_lun(info, state, phy, &vir, 0);
1071 break;
1072 case XenbusStateConnected:
1073 scsiback_do_add_lun(info, state, phy, &vir, 1);
1074 break;
1075 case XenbusStateClosing:
1076 scsiback_do_del_lun(info, state, &vir);
1077 break;
1078 default:
1079 break;
1080 }
1081 break;
1082
1083 case VSCSIBACK_OP_UPDATEDEV_STATE:
1084 if (device_state == XenbusStateInitialised) {
1085 /* modify vscsi-devs/dev-x/state */
1086 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1087 "%d", XenbusStateConnected)) {
1088 pr_err("xenbus_printf error %s\n", str);
1089 scsiback_del_translation_entry(info, &vir);
1090 xenbus_printf(XBT_NIL, dev->nodename, state,
1091 "%d", XenbusStateClosed);
1092 }
1093 }
1094 break;
1095 /* When it is necessary, processing is added here. */
1096 default:
1097 break;
1098 }
1099}
1100
1101static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1102{
1103 int i;
1104 char **dir;
1105 unsigned int ndir = 0;
1106
1107 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1108 &ndir);
1109 if (IS_ERR(dir))
1110 return;
1111
1112 for (i = 0; i < ndir; i++)
1113 scsiback_do_1lun_hotplug(info, op, dir[i]);
1114
1115 kfree(dir);
1116}
1117
1118static void scsiback_frontend_changed(struct xenbus_device *dev,
1119 enum xenbus_state frontend_state)
1120{
1121 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1122
1123 switch (frontend_state) {
1124 case XenbusStateInitialising:
1125 break;
1126
1127 case XenbusStateInitialised:
1128 if (scsiback_map(info))
1129 break;
1130
1131 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1132 xenbus_switch_state(dev, XenbusStateConnected);
1133 break;
1134
1135 case XenbusStateConnected:
1136 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1137
1138 if (dev->state == XenbusStateConnected)
1139 break;
1140
1141 xenbus_switch_state(dev, XenbusStateConnected);
1142 break;
1143
1144 case XenbusStateClosing:
1145 if (info->irq)
1146 scsiback_disconnect(info);
1147
1148 xenbus_switch_state(dev, XenbusStateClosing);
1149 break;
1150
1151 case XenbusStateClosed:
1152 xenbus_switch_state(dev, XenbusStateClosed);
1153 if (xenbus_dev_is_online(dev))
1154 break;
1155 fallthrough; /* if not online */
1156 case XenbusStateUnknown:
1157 device_unregister(&dev->dev);
1158 break;
1159
1160 case XenbusStateReconfiguring:
1161 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1162 xenbus_switch_state(dev, XenbusStateReconfigured);
1163
1164 break;
1165
1166 default:
1167 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1168 frontend_state);
1169 break;
1170 }
1171}
1172
1173/*
1174 Release the translation entry specfied
1175*/
1176static void scsiback_release_translation_entry(struct vscsibk_info *info)
1177{
1178 struct v2p_entry *entry, *tmp;
1179 struct list_head *head = &(info->v2p_entry_lists);
1180 unsigned long flags;
1181
1182 spin_lock_irqsave(&info->v2p_lock, flags);
1183
1184 list_for_each_entry_safe(entry, tmp, head, l)
1185 __scsiback_del_translation_entry(entry);
1186
1187 spin_unlock_irqrestore(&info->v2p_lock, flags);
1188}
1189
1190static int scsiback_remove(struct xenbus_device *dev)
1191{
1192 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1193
1194 if (info->irq)
1195 scsiback_disconnect(info);
1196
1197 scsiback_release_translation_entry(info);
1198
1199 gnttab_page_cache_shrink(&info->free_pages, 0);
1200
1201 dev_set_drvdata(&dev->dev, NULL);
1202
1203 return 0;
1204}
1205
1206static int scsiback_probe(struct xenbus_device *dev,
1207 const struct xenbus_device_id *id)
1208{
1209 int err;
1210
1211 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1212 GFP_KERNEL);
1213
1214 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
1215
1216 if (!info) {
1217 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1218 return -ENOMEM;
1219 }
1220 info->dev = dev;
1221 dev_set_drvdata(&dev->dev, info);
1222
1223 info->domid = dev->otherend_id;
1224 spin_lock_init(&info->ring_lock);
1225 atomic_set(&info->nr_unreplied_reqs, 0);
1226 init_waitqueue_head(&info->waiting_to_free);
1227 info->dev = dev;
1228 info->irq = 0;
1229 INIT_LIST_HEAD(&info->v2p_entry_lists);
1230 spin_lock_init(&info->v2p_lock);
1231 gnttab_page_cache_init(&info->free_pages);
1232
1233 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1234 SG_ALL);
1235 if (err)
1236 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1237
1238 err = xenbus_switch_state(dev, XenbusStateInitWait);
1239 if (err)
1240 goto fail;
1241
1242 return 0;
1243
1244fail:
1245 pr_warn("%s failed\n", __func__);
1246 scsiback_remove(dev);
1247
1248 return err;
1249}
1250
1251static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1252{
1253 switch (tport->tport_proto_id) {
1254 case SCSI_PROTOCOL_SAS:
1255 return "SAS";
1256 case SCSI_PROTOCOL_FCP:
1257 return "FCP";
1258 case SCSI_PROTOCOL_ISCSI:
1259 return "iSCSI";
1260 default:
1261 break;
1262 }
1263
1264 return "Unknown";
1265}
1266
1267static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1268{
1269 struct scsiback_tpg *tpg = container_of(se_tpg,
1270 struct scsiback_tpg, se_tpg);
1271 struct scsiback_tport *tport = tpg->tport;
1272
1273 return &tport->tport_name[0];
1274}
1275
1276static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1277{
1278 struct scsiback_tpg *tpg = container_of(se_tpg,
1279 struct scsiback_tpg, se_tpg);
1280 return tpg->tport_tpgt;
1281}
1282
1283static struct se_wwn *
1284scsiback_make_tport(struct target_fabric_configfs *tf,
1285 struct config_group *group,
1286 const char *name)
1287{
1288 struct scsiback_tport *tport;
1289 char *ptr;
1290 u64 wwpn = 0;
1291 int off = 0;
1292
1293 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1294 if (!tport)
1295 return ERR_PTR(-ENOMEM);
1296
1297 tport->tport_wwpn = wwpn;
1298 /*
1299 * Determine the emulated Protocol Identifier and Target Port Name
1300 * based on the incoming configfs directory name.
1301 */
1302 ptr = strstr(name, "naa.");
1303 if (ptr) {
1304 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1305 goto check_len;
1306 }
1307 ptr = strstr(name, "fc.");
1308 if (ptr) {
1309 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1310 off = 3; /* Skip over "fc." */
1311 goto check_len;
1312 }
1313 ptr = strstr(name, "iqn.");
1314 if (ptr) {
1315 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1316 goto check_len;
1317 }
1318
1319 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1320 kfree(tport);
1321 return ERR_PTR(-EINVAL);
1322
1323check_len:
1324 if (strlen(name) >= VSCSI_NAMELEN) {
1325 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1326 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1327 kfree(tport);
1328 return ERR_PTR(-EINVAL);
1329 }
1330 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1331
1332 pr_debug("Allocated emulated Target %s Address: %s\n",
1333 scsiback_dump_proto_id(tport), name);
1334
1335 return &tport->tport_wwn;
1336}
1337
1338static void scsiback_drop_tport(struct se_wwn *wwn)
1339{
1340 struct scsiback_tport *tport = container_of(wwn,
1341 struct scsiback_tport, tport_wwn);
1342
1343 pr_debug("Deallocating emulated Target %s Address: %s\n",
1344 scsiback_dump_proto_id(tport), tport->tport_name);
1345
1346 kfree(tport);
1347}
1348
1349static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1350{
1351 return 1;
1352}
1353
1354static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1355{
1356 return transport_generic_free_cmd(se_cmd, 0);
1357}
1358
1359static void scsiback_release_cmd(struct se_cmd *se_cmd)
1360{
1361 target_free_tag(se_cmd->se_sess, se_cmd);
1362}
1363
1364static u32 scsiback_sess_get_index(struct se_session *se_sess)
1365{
1366 return 0;
1367}
1368
1369static int scsiback_write_pending(struct se_cmd *se_cmd)
1370{
1371 /* Go ahead and process the write immediately */
1372 target_execute_cmd(se_cmd);
1373
1374 return 0;
1375}
1376
1377static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1378{
1379}
1380
1381static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1382{
1383 return 0;
1384}
1385
1386static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1387{
1388 struct vscsibk_pend *pending_req = container_of(se_cmd,
1389 struct vscsibk_pend, se_cmd);
1390
1391 pending_req->result = SAM_STAT_GOOD;
1392 scsiback_cmd_done(pending_req);
1393 return 0;
1394}
1395
1396static int scsiback_queue_status(struct se_cmd *se_cmd)
1397{
1398 struct vscsibk_pend *pending_req = container_of(se_cmd,
1399 struct vscsibk_pend, se_cmd);
1400
1401 if (se_cmd->sense_buffer &&
1402 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1403 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1404 pending_req->result = SAM_STAT_CHECK_CONDITION;
1405 else
1406 pending_req->result = se_cmd->scsi_status;
1407
1408 scsiback_cmd_done(pending_req);
1409 return 0;
1410}
1411
1412static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1413{
1414 struct vscsibk_pend *pending_req = container_of(se_cmd,
1415 struct vscsibk_pend, se_cmd);
1416
1417 complete(&pending_req->tmr_done);
1418}
1419
1420static void scsiback_aborted_task(struct se_cmd *se_cmd)
1421{
1422}
1423
1424static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
1425 char *page)
1426{
1427 struct se_portal_group *se_tpg = param_to_tpg(item);
1428 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1429 se_tpg);
1430 ssize_t rb;
1431
1432 mutex_lock(&tpg->tv_tpg_mutex);
1433 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1434 mutex_unlock(&tpg->tv_tpg_mutex);
1435
1436 return rb;
1437}
1438
1439static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
1440 const char *page, size_t count)
1441{
1442 struct se_portal_group *se_tpg = param_to_tpg(item);
1443 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1444 se_tpg);
1445 int len;
1446
1447 if (strlen(page) >= VSCSI_NAMELEN) {
1448 pr_err("param alias: %s, exceeds max: %d\n", page,
1449 VSCSI_NAMELEN);
1450 return -EINVAL;
1451 }
1452
1453 mutex_lock(&tpg->tv_tpg_mutex);
1454 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1455 if (tpg->param_alias[len - 1] == '\n')
1456 tpg->param_alias[len - 1] = '\0';
1457 mutex_unlock(&tpg->tv_tpg_mutex);
1458
1459 return count;
1460}
1461
1462CONFIGFS_ATTR(scsiback_tpg_param_, alias);
1463
1464static struct configfs_attribute *scsiback_param_attrs[] = {
1465 &scsiback_tpg_param_attr_alias,
1466 NULL,
1467};
1468
1469static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg,
1470 struct se_session *se_sess, void *p)
1471{
1472 struct scsiback_tpg *tpg = container_of(se_tpg,
1473 struct scsiback_tpg, se_tpg);
1474
1475 tpg->tpg_nexus = p;
1476 return 0;
1477}
1478
1479static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1480 const char *name)
1481{
1482 struct scsiback_nexus *tv_nexus;
1483 int ret = 0;
1484
1485 mutex_lock(&tpg->tv_tpg_mutex);
1486 if (tpg->tpg_nexus) {
1487 pr_debug("tpg->tpg_nexus already exists\n");
1488 ret = -EEXIST;
1489 goto out_unlock;
1490 }
1491
1492 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1493 if (!tv_nexus) {
1494 ret = -ENOMEM;
1495 goto out_unlock;
1496 }
1497
1498 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg,
1499 VSCSI_DEFAULT_SESSION_TAGS,
1500 sizeof(struct vscsibk_pend),
1501 TARGET_PROT_NORMAL, name,
1502 tv_nexus, scsiback_alloc_sess_cb);
1503 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1504 kfree(tv_nexus);
1505 ret = -ENOMEM;
1506 goto out_unlock;
1507 }
1508
1509out_unlock:
1510 mutex_unlock(&tpg->tv_tpg_mutex);
1511 return ret;
1512}
1513
1514static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1515{
1516 struct se_session *se_sess;
1517 struct scsiback_nexus *tv_nexus;
1518
1519 mutex_lock(&tpg->tv_tpg_mutex);
1520 tv_nexus = tpg->tpg_nexus;
1521 if (!tv_nexus) {
1522 mutex_unlock(&tpg->tv_tpg_mutex);
1523 return -ENODEV;
1524 }
1525
1526 se_sess = tv_nexus->tvn_se_sess;
1527 if (!se_sess) {
1528 mutex_unlock(&tpg->tv_tpg_mutex);
1529 return -ENODEV;
1530 }
1531
1532 if (tpg->tv_tpg_port_count != 0) {
1533 mutex_unlock(&tpg->tv_tpg_mutex);
1534 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1535 tpg->tv_tpg_port_count);
1536 return -EBUSY;
1537 }
1538
1539 if (tpg->tv_tpg_fe_count != 0) {
1540 mutex_unlock(&tpg->tv_tpg_mutex);
1541 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1542 tpg->tv_tpg_fe_count);
1543 return -EBUSY;
1544 }
1545
1546 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1547 scsiback_dump_proto_id(tpg->tport),
1548 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1549
1550 /*
1551 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1552 */
1553 target_remove_session(se_sess);
1554 tpg->tpg_nexus = NULL;
1555 mutex_unlock(&tpg->tv_tpg_mutex);
1556
1557 kfree(tv_nexus);
1558 return 0;
1559}
1560
1561static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
1562{
1563 struct se_portal_group *se_tpg = to_tpg(item);
1564 struct scsiback_tpg *tpg = container_of(se_tpg,
1565 struct scsiback_tpg, se_tpg);
1566 struct scsiback_nexus *tv_nexus;
1567 ssize_t ret;
1568
1569 mutex_lock(&tpg->tv_tpg_mutex);
1570 tv_nexus = tpg->tpg_nexus;
1571 if (!tv_nexus) {
1572 mutex_unlock(&tpg->tv_tpg_mutex);
1573 return -ENODEV;
1574 }
1575 ret = snprintf(page, PAGE_SIZE, "%s\n",
1576 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1577 mutex_unlock(&tpg->tv_tpg_mutex);
1578
1579 return ret;
1580}
1581
1582static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1583 const char *page, size_t count)
1584{
1585 struct se_portal_group *se_tpg = to_tpg(item);
1586 struct scsiback_tpg *tpg = container_of(se_tpg,
1587 struct scsiback_tpg, se_tpg);
1588 struct scsiback_tport *tport_wwn = tpg->tport;
1589 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1590 int ret;
1591 /*
1592 * Shutdown the active I_T nexus if 'NULL' is passed.
1593 */
1594 if (!strncmp(page, "NULL", 4)) {
1595 ret = scsiback_drop_nexus(tpg);
1596 return (!ret) ? count : ret;
1597 }
1598 /*
1599 * Otherwise make sure the passed virtual Initiator port WWN matches
1600 * the fabric protocol_id set in scsiback_make_tport(), and call
1601 * scsiback_make_nexus().
1602 */
1603 if (strlen(page) >= VSCSI_NAMELEN) {
1604 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1605 page, VSCSI_NAMELEN);
1606 return -EINVAL;
1607 }
1608 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1609
1610 ptr = strstr(i_port, "naa.");
1611 if (ptr) {
1612 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1613 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1614 i_port, scsiback_dump_proto_id(tport_wwn));
1615 return -EINVAL;
1616 }
1617 port_ptr = &i_port[0];
1618 goto check_newline;
1619 }
1620 ptr = strstr(i_port, "fc.");
1621 if (ptr) {
1622 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1623 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1624 i_port, scsiback_dump_proto_id(tport_wwn));
1625 return -EINVAL;
1626 }
1627 port_ptr = &i_port[3]; /* Skip over "fc." */
1628 goto check_newline;
1629 }
1630 ptr = strstr(i_port, "iqn.");
1631 if (ptr) {
1632 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1633 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1634 i_port, scsiback_dump_proto_id(tport_wwn));
1635 return -EINVAL;
1636 }
1637 port_ptr = &i_port[0];
1638 goto check_newline;
1639 }
1640 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1641 i_port);
1642 return -EINVAL;
1643 /*
1644 * Clear any trailing newline for the NAA WWN
1645 */
1646check_newline:
1647 if (i_port[strlen(i_port) - 1] == '\n')
1648 i_port[strlen(i_port) - 1] = '\0';
1649
1650 ret = scsiback_make_nexus(tpg, port_ptr);
1651 if (ret < 0)
1652 return ret;
1653
1654 return count;
1655}
1656
1657CONFIGFS_ATTR(scsiback_tpg_, nexus);
1658
1659static struct configfs_attribute *scsiback_tpg_attrs[] = {
1660 &scsiback_tpg_attr_nexus,
1661 NULL,
1662};
1663
1664static ssize_t
1665scsiback_wwn_version_show(struct config_item *item, char *page)
1666{
1667 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1668 UTS_RELEASE"\n",
1669 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1670}
1671
1672CONFIGFS_ATTR_RO(scsiback_wwn_, version);
1673
1674static struct configfs_attribute *scsiback_wwn_attrs[] = {
1675 &scsiback_wwn_attr_version,
1676 NULL,
1677};
1678
1679static int scsiback_port_link(struct se_portal_group *se_tpg,
1680 struct se_lun *lun)
1681{
1682 struct scsiback_tpg *tpg = container_of(se_tpg,
1683 struct scsiback_tpg, se_tpg);
1684
1685 mutex_lock(&tpg->tv_tpg_mutex);
1686 tpg->tv_tpg_port_count++;
1687 mutex_unlock(&tpg->tv_tpg_mutex);
1688
1689 return 0;
1690}
1691
1692static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1693 struct se_lun *lun)
1694{
1695 struct scsiback_tpg *tpg = container_of(se_tpg,
1696 struct scsiback_tpg, se_tpg);
1697
1698 mutex_lock(&tpg->tv_tpg_mutex);
1699 tpg->tv_tpg_port_count--;
1700 mutex_unlock(&tpg->tv_tpg_mutex);
1701}
1702
1703static struct se_portal_group *
1704scsiback_make_tpg(struct se_wwn *wwn, const char *name)
1705{
1706 struct scsiback_tport *tport = container_of(wwn,
1707 struct scsiback_tport, tport_wwn);
1708
1709 struct scsiback_tpg *tpg;
1710 u16 tpgt;
1711 int ret;
1712
1713 if (strstr(name, "tpgt_") != name)
1714 return ERR_PTR(-EINVAL);
1715 ret = kstrtou16(name + 5, 10, &tpgt);
1716 if (ret)
1717 return ERR_PTR(ret);
1718
1719 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1720 if (!tpg)
1721 return ERR_PTR(-ENOMEM);
1722
1723 mutex_init(&tpg->tv_tpg_mutex);
1724 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1725 INIT_LIST_HEAD(&tpg->info_list);
1726 tpg->tport = tport;
1727 tpg->tport_tpgt = tpgt;
1728
1729 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
1730 if (ret < 0) {
1731 kfree(tpg);
1732 return NULL;
1733 }
1734 mutex_lock(&scsiback_mutex);
1735 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1736 mutex_unlock(&scsiback_mutex);
1737
1738 return &tpg->se_tpg;
1739}
1740
1741static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1742{
1743 struct scsiback_tpg *tpg = container_of(se_tpg,
1744 struct scsiback_tpg, se_tpg);
1745
1746 mutex_lock(&scsiback_mutex);
1747 list_del(&tpg->tv_tpg_list);
1748 mutex_unlock(&scsiback_mutex);
1749 /*
1750 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1751 */
1752 scsiback_drop_nexus(tpg);
1753 /*
1754 * Deregister the se_tpg from TCM.
1755 */
1756 core_tpg_deregister(se_tpg);
1757 kfree(tpg);
1758}
1759
1760static int scsiback_check_true(struct se_portal_group *se_tpg)
1761{
1762 return 1;
1763}
1764
1765static int scsiback_check_false(struct se_portal_group *se_tpg)
1766{
1767 return 0;
1768}
1769
1770static const struct target_core_fabric_ops scsiback_ops = {
1771 .module = THIS_MODULE,
1772 .fabric_name = "xen-pvscsi",
1773 .tpg_get_wwn = scsiback_get_fabric_wwn,
1774 .tpg_get_tag = scsiback_get_tag,
1775 .tpg_check_demo_mode = scsiback_check_true,
1776 .tpg_check_demo_mode_cache = scsiback_check_true,
1777 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1778 .tpg_check_prod_mode_write_protect = scsiback_check_false,
1779 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1780 .check_stop_free = scsiback_check_stop_free,
1781 .release_cmd = scsiback_release_cmd,
1782 .sess_get_index = scsiback_sess_get_index,
1783 .sess_get_initiator_sid = NULL,
1784 .write_pending = scsiback_write_pending,
1785 .set_default_node_attributes = scsiback_set_default_node_attrs,
1786 .get_cmd_state = scsiback_get_cmd_state,
1787 .queue_data_in = scsiback_queue_data_in,
1788 .queue_status = scsiback_queue_status,
1789 .queue_tm_rsp = scsiback_queue_tm_rsp,
1790 .aborted_task = scsiback_aborted_task,
1791 /*
1792 * Setup callers for generic logic in target_core_fabric_configfs.c
1793 */
1794 .fabric_make_wwn = scsiback_make_tport,
1795 .fabric_drop_wwn = scsiback_drop_tport,
1796 .fabric_make_tpg = scsiback_make_tpg,
1797 .fabric_drop_tpg = scsiback_drop_tpg,
1798 .fabric_post_link = scsiback_port_link,
1799 .fabric_pre_unlink = scsiback_port_unlink,
1800
1801 .tfc_wwn_attrs = scsiback_wwn_attrs,
1802 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1803 .tfc_tpg_param_attrs = scsiback_param_attrs,
1804};
1805
1806static const struct xenbus_device_id scsiback_ids[] = {
1807 { "vscsi" },
1808 { "" }
1809};
1810
1811static struct xenbus_driver scsiback_driver = {
1812 .ids = scsiback_ids,
1813 .probe = scsiback_probe,
1814 .remove = scsiback_remove,
1815 .otherend_changed = scsiback_frontend_changed
1816};
1817
1818static int __init scsiback_init(void)
1819{
1820 int ret;
1821
1822 if (!xen_domain())
1823 return -ENODEV;
1824
1825 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1826 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1827
1828 ret = xenbus_register_backend(&scsiback_driver);
1829 if (ret)
1830 goto out;
1831
1832 ret = target_register_template(&scsiback_ops);
1833 if (ret)
1834 goto out_unregister_xenbus;
1835
1836 return 0;
1837
1838out_unregister_xenbus:
1839 xenbus_unregister_driver(&scsiback_driver);
1840out:
1841 pr_err("%s: error %d\n", __func__, ret);
1842 return ret;
1843}
1844
1845static void __exit scsiback_exit(void)
1846{
1847 target_unregister_template(&scsiback_ops);
1848 xenbus_unregister_driver(&scsiback_driver);
1849}
1850
1851module_init(scsiback_init);
1852module_exit(scsiback_exit);
1853
1854MODULE_DESCRIPTION("Xen SCSI backend driver");
1855MODULE_LICENSE("Dual BSD/GPL");
1856MODULE_ALIAS("xen-backend:vscsi");
1857MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");