blob: 3f379f79cfb9393573abac0793039eedfcb9558b [file] [log] [blame]
Juergen Grossd9d660f2014-08-28 06:44:12 +02001/*
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
Tao Chen78574872015-03-10 20:49:18 +000034#define pr_fmt(fmt) "xen-pvscsi: " fmt
35
Juergen Grossd9d660f2014-08-28 06:44:12 +020036#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
Bart Van Asscheba929992015-05-08 10:11:12 +020052#include <scsi/scsi_host.h> /* SG_ALL */
Juergen Grossd9d660f2014-08-28 06:44:12 +020053
54#include <target/target_core_base.h>
55#include <target/target_core_fabric.h>
Juergen Grossd9d660f2014-08-28 06:44:12 +020056
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
Juergen Grossd9d660f2014-08-28 06:44:12 +020069#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
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800144#define VSCSI_DEFAULT_SESSION_TAGS 128
145
Juergen Grossd9d660f2014-08-28 06:44:12 +0200146struct 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
Juergen Grossd9d660f2014-08-28 06:44:12 +0200195static 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
Juergen Grossd9d660f2014-08-28 06:44:12 +0200203static 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);
David Vrabelff4b1562015-01-08 18:06:01 +0000223 gnttab_free_pages(n, page + num - n);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200224 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);
David Vrabelff4b1562015-01-08 18:06:01 +0000240 return gnttab_alloc_pages(1, page);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200241 }
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
Tao Chen78574872015-03-10 20:49:18 +0000266 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200267 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));
Juergen Grossd9d660f2014-08-28 06:44:12 +0200270}
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
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800325static void scsiback_send_response(struct vscsibk_info *info,
326 char *sense_buffer, int32_t result, uint32_t resid,
327 uint16_t rqid)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200328{
329 struct vscsiif_response *ring_res;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200330 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;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800341 ring_res->rqid = rqid;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200342
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);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800361}
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);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200368
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
393static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
394{
395 struct se_cmd *se_cmd = &pending_req->se_cmd;
396 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
397 int rc;
398
Juergen Grossd9d660f2014-08-28 06:44:12 +0200399 scsiback_get(pending_req->info);
Bart Van Assche649ee052015-04-14 13:26:44 +0200400 se_cmd->tag = pending_req->rqid;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200401 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
402 pending_req->sense_buffer, pending_req->v2p->lun,
403 pending_req->data_len, 0,
404 pending_req->sc_data_direction, 0,
405 pending_req->sgl, pending_req->n_sg,
406 NULL, 0, NULL, 0);
407 if (rc < 0) {
408 transport_send_check_condition_and_sense(se_cmd,
409 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
410 transport_generic_free_cmd(se_cmd, 0);
411 }
412}
413
414static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
415 struct page **pg, grant_handle_t *grant, int cnt)
416{
417 int err, i;
418
419 if (!cnt)
420 return 0;
421
422 err = gnttab_map_refs(map, NULL, pg, cnt);
423 BUG_ON(err);
424 for (i = 0; i < cnt; i++) {
425 if (unlikely(map[i].status != GNTST_okay)) {
Tao Chen78574872015-03-10 20:49:18 +0000426 pr_err("invalid buffer -- could not remap it\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200427 map[i].handle = SCSIBACK_INVALID_HANDLE;
428 err = -ENOMEM;
429 } else {
430 get_page(pg[i]);
431 }
432 grant[i] = map[i].handle;
433 }
434 return err;
435}
436
437static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
438 struct scsiif_request_segment *seg, struct page **pg,
439 grant_handle_t *grant, int cnt, u32 flags)
440{
441 int mapcount = 0, i, err = 0;
442 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
443 struct vscsibk_info *info = pending_req->info;
444
445 for (i = 0; i < cnt; i++) {
446 if (get_free_page(pg + mapcount)) {
447 put_free_pages(pg, mapcount);
Tao Chen78574872015-03-10 20:49:18 +0000448 pr_err("no grant page\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200449 return -ENOMEM;
450 }
451 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
452 flags, seg[i].gref, info->domid);
453 mapcount++;
454 if (mapcount < VSCSI_GRANT_BATCH)
455 continue;
456 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
457 pg += mapcount;
458 grant += mapcount;
459 pending_req->n_grants += mapcount;
460 if (err)
461 return err;
462 mapcount = 0;
463 }
464 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
465 pending_req->n_grants += mapcount;
466 return err;
467}
468
469static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
470 struct vscsibk_pend *pending_req)
471{
472 u32 flags;
473 int i, err, n_segs, i_seg = 0;
474 struct page **pg;
475 struct scsiif_request_segment *seg;
476 unsigned long end_seg = 0;
477 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
478 unsigned int nr_sgl = 0;
479 struct scatterlist *sg;
480 grant_handle_t *grant;
481
482 pending_req->n_sg = 0;
483 pending_req->n_grants = 0;
484 pending_req->data_len = 0;
485
486 nr_segments &= ~VSCSIIF_SG_GRANT;
487 if (!nr_segments)
488 return 0;
489
490 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
Tao Chen78574872015-03-10 20:49:18 +0000491 pr_debug("invalid parameter nr_seg = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200492 ring_req->nr_segments);
493 return -EINVAL;
494 }
495
496 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
497 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
498 pending_req->pages, pending_req->grant_handles,
499 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
500 if (err)
501 return err;
502 nr_sgl = nr_segments;
503 nr_segments = 0;
504 for (i = 0; i < nr_sgl; i++) {
505 n_segs = ring_req->seg[i].length /
506 sizeof(struct scsiif_request_segment);
507 if ((unsigned)ring_req->seg[i].offset +
508 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
509 n_segs * sizeof(struct scsiif_request_segment) !=
510 ring_req->seg[i].length)
511 return -EINVAL;
512 nr_segments += n_segs;
513 }
514 if (nr_segments > SG_ALL) {
Tao Chen78574872015-03-10 20:49:18 +0000515 pr_debug("invalid nr_seg = %d\n", nr_segments);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200516 return -EINVAL;
517 }
518 }
519
Tao Chen78574872015-03-10 20:49:18 +0000520 /* free of (sgl) in fast_flush_area() */
Juergen Grossd9d660f2014-08-28 06:44:12 +0200521 pending_req->sgl = kmalloc_array(nr_segments,
522 sizeof(struct scatterlist), GFP_KERNEL);
523 if (!pending_req->sgl)
524 return -ENOMEM;
525
526 sg_init_table(pending_req->sgl, nr_segments);
527 pending_req->n_sg = nr_segments;
528
529 flags = GNTMAP_host_map;
530 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
531 flags |= GNTMAP_readonly;
532
533 pg = pending_req->pages + nr_sgl;
534 grant = pending_req->grant_handles + nr_sgl;
535 if (!nr_sgl) {
536 seg = ring_req->seg;
537 err = scsiback_gnttab_data_map_list(pending_req, seg,
538 pg, grant, nr_segments, flags);
539 if (err)
540 return err;
541 } else {
542 for (i = 0; i < nr_sgl; i++) {
543 seg = (struct scsiif_request_segment *)(
544 vaddr(pending_req, i) + ring_req->seg[i].offset);
545 n_segs = ring_req->seg[i].length /
546 sizeof(struct scsiif_request_segment);
547 err = scsiback_gnttab_data_map_list(pending_req, seg,
548 pg, grant, n_segs, flags);
549 if (err)
550 return err;
551 pg += n_segs;
552 grant += n_segs;
553 }
554 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
555 seg = (struct scsiif_request_segment *)end_seg;
556 end_seg += ring_req->seg[0].length;
557 pg = pending_req->pages + nr_sgl;
558 }
559
560 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
561 sg_set_page(sg, pg[i], seg->length, seg->offset);
562 pending_req->data_len += seg->length;
563 seg++;
564 if (nr_sgl && (unsigned long)seg >= end_seg) {
565 i_seg++;
566 end_seg = vaddr(pending_req, i_seg) +
567 ring_req->seg[i_seg].offset;
568 seg = (struct scsiif_request_segment *)end_seg;
569 end_seg += ring_req->seg[i_seg].length;
570 }
571 if (sg->offset >= PAGE_SIZE ||
572 sg->length > PAGE_SIZE ||
573 sg->offset + sg->length > PAGE_SIZE)
574 return -EINVAL;
575 }
576
577 return 0;
578}
579
580static void scsiback_disconnect(struct vscsibk_info *info)
581{
582 wait_event(info->waiting_to_free,
583 atomic_read(&info->nr_unreplied_reqs) == 0);
584
585 unbind_from_irqhandler(info->irq, info);
586 info->irq = 0;
587 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
588}
589
590static void scsiback_device_action(struct vscsibk_pend *pending_req,
591 enum tcm_tmreq_table act, int tag)
592{
593 int rc, err = FAILED;
594 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
595 struct se_cmd *se_cmd = &pending_req->se_cmd;
596 struct scsiback_tmr *tmr;
597
598 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
599 if (!tmr)
600 goto out;
601
602 init_waitqueue_head(&tmr->tmr_wait);
603
604 transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800605 tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, TCM_SIMPLE_TAG,
Juergen Grossd9d660f2014-08-28 06:44:12 +0200606 &pending_req->sense_buffer[0]);
607
608 rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
609 if (rc < 0)
610 goto out;
611
612 se_cmd->se_tmr_req->ref_task_tag = tag;
613
614 if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
615 goto out;
616
617 transport_generic_handle_tmr(se_cmd);
618 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
619
620 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
621 SUCCESS : FAILED;
622
Juergen Grossd9d660f2014-08-28 06:44:12 +0200623 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800624 transport_generic_free_cmd(&pending_req->se_cmd, 1);
625 return;
626out:
627 if (tmr)
628 kfree(tmr);
629 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200630}
631
632/*
633 Perform virtual to physical translation
634*/
635static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
636 struct ids_tuple *v)
637{
638 struct v2p_entry *entry;
639 struct list_head *head = &(info->v2p_entry_lists);
640 unsigned long flags;
641
642 spin_lock_irqsave(&info->v2p_lock, flags);
643 list_for_each_entry(entry, head, l) {
644 if ((entry->v.chn == v->chn) &&
645 (entry->v.tgt == v->tgt) &&
646 (entry->v.lun == v->lun)) {
647 kref_get(&entry->kref);
648 goto out;
649 }
650 }
651 entry = NULL;
652
653out:
654 spin_unlock_irqrestore(&info->v2p_lock, flags);
655 return entry;
656}
657
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800658static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring,
659 struct v2p_entry *v2p)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200660{
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800661 struct scsiback_tpg *tpg = v2p->tpg;
662 struct scsiback_nexus *nexus = tpg->tpg_nexus;
663 struct se_session *se_sess = nexus->tvn_se_sess;
664 struct vscsibk_pend *req;
665 int tag, i;
666
667 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
668 if (tag < 0) {
669 pr_err("Unable to obtain tag for vscsiif_request\n");
670 return ERR_PTR(-ENOMEM);
671 }
672
673 req = &((struct vscsibk_pend *)se_sess->sess_cmd_map)[tag];
674 memset(req, 0, sizeof(*req));
675 req->se_cmd.map_tag = tag;
676
677 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
678 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
679
680 return req;
681}
682
683static struct vscsibk_pend *prepare_pending_reqs(struct vscsibk_info *info,
684 struct vscsiif_back_ring *ring,
685 struct vscsiif_request *ring_req)
686{
687 struct vscsibk_pend *pending_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200688 struct v2p_entry *v2p;
689 struct ids_tuple vir;
690
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800691 /* request range check from frontend */
692 if ((ring_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
693 (ring_req->sc_data_direction != DMA_TO_DEVICE) &&
694 (ring_req->sc_data_direction != DMA_FROM_DEVICE) &&
695 (ring_req->sc_data_direction != DMA_NONE)) {
696 pr_debug("invalid parameter data_dir = %d\n",
697 ring_req->sc_data_direction);
698 return ERR_PTR(-EINVAL);
699 }
700 if (ring_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
701 pr_debug("invalid parameter cmd_len = %d\n",
702 ring_req->cmd_len);
703 return ERR_PTR(-EINVAL);
704 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200705
706 vir.chn = ring_req->channel;
707 vir.tgt = ring_req->id;
708 vir.lun = ring_req->lun;
709
710 v2p = scsiback_do_translation(info, &vir);
711 if (!v2p) {
Tao Chen78574872015-03-10 20:49:18 +0000712 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800713 vir.chn, vir.tgt, vir.lun);
714 return ERR_PTR(-ENODEV);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200715 }
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800716
717 pending_req = scsiback_get_pend_req(ring, v2p);
718 if (IS_ERR(pending_req)) {
719 kref_put(&v2p->kref, scsiback_free_translation_entry);
720 return ERR_PTR(-ENOMEM);
721 }
722 pending_req->rqid = ring_req->rqid;
723 pending_req->info = info;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200724 pending_req->v2p = v2p;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200725 pending_req->sc_data_direction = ring_req->sc_data_direction;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200726 pending_req->cmd_len = ring_req->cmd_len;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200727 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
728
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800729 return pending_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200730}
731
732static int scsiback_do_cmd_fn(struct vscsibk_info *info)
733{
734 struct vscsiif_back_ring *ring = &info->ring;
Juergen Grossfacb5732015-02-17 08:02:47 +0100735 struct vscsiif_request ring_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200736 struct vscsibk_pend *pending_req;
737 RING_IDX rc, rp;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800738 int more_to_do;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200739 uint32_t result;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200740
741 rc = ring->req_cons;
742 rp = ring->sring->req_prod;
743 rmb(); /* guest system is accessing ring, too */
744
745 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
746 rc = ring->rsp_prod_pvt;
Tao Chen78574872015-03-10 20:49:18 +0000747 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200748 info->domid, rp, rc, rp - rc);
749 info->ring_error = 1;
750 return 0;
751 }
752
753 while ((rc != rp)) {
754 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
755 break;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200756
David Vrabelbe697462015-11-16 18:02:32 +0000757 RING_COPY_REQUEST(ring, rc, &ring_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200758 ring->req_cons = ++rc;
759
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800760 pending_req = prepare_pending_reqs(info, ring, &ring_req);
761 if (IS_ERR(pending_req)) {
762 switch (PTR_ERR(pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200763 case -ENODEV:
764 result = DID_NO_CONNECT;
765 break;
766 default:
767 result = DRIVER_ERROR;
768 break;
769 }
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800770 scsiback_send_response(info, NULL, result << 24, 0,
771 ring_req.rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200772 return 1;
773 }
774
Juergen Grossfacb5732015-02-17 08:02:47 +0100775 switch (ring_req.act) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200776 case VSCSIIF_ACT_SCSI_CDB:
Juergen Grossfacb5732015-02-17 08:02:47 +0100777 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200778 scsiback_fast_flush_area(pending_req);
779 scsiback_do_resp_with_sense(NULL,
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800780 DRIVER_ERROR << 24, 0, pending_req);
781 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200782 } else {
783 scsiback_cmd_exec(pending_req);
784 }
785 break;
786 case VSCSIIF_ACT_SCSI_ABORT:
787 scsiback_device_action(pending_req, TMR_ABORT_TASK,
Juergen Grossfacb5732015-02-17 08:02:47 +0100788 ring_req.ref_rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200789 break;
790 case VSCSIIF_ACT_SCSI_RESET:
791 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
792 break;
793 default:
Tao Chen78574872015-03-10 20:49:18 +0000794 pr_err_ratelimited("invalid request\n");
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800795 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24, 0,
796 pending_req);
797 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200798 break;
799 }
800
801 /* Yield point for this unbounded loop. */
802 cond_resched();
803 }
804
805 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
806 return more_to_do;
807}
808
809static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
810{
811 struct vscsibk_info *info = dev_id;
812
813 if (info->ring_error)
814 return IRQ_HANDLED;
815
816 while (scsiback_do_cmd_fn(info))
817 cond_resched();
818
819 return IRQ_HANDLED;
820}
821
822static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
823 evtchn_port_t evtchn)
824{
825 void *area;
826 struct vscsiif_sring *sring;
827 int err;
828
829 if (info->irq)
830 return -1;
831
Wei Liuccc9d902015-04-03 14:44:59 +0800832 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200833 if (err)
834 return err;
835
836 sring = (struct vscsiif_sring *)area;
837 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
838
839 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
840 if (err < 0)
841 goto unmap_page;
842
843 info->irq = err;
844
845 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
846 IRQF_ONESHOT, "vscsiif-backend", info);
847 if (err)
848 goto free_irq;
849
850 return 0;
851
852free_irq:
853 unbind_from_irqhandler(info->irq, info);
854 info->irq = 0;
855unmap_page:
856 xenbus_unmap_ring_vfree(info->dev, area);
857
858 return err;
859}
860
861static int scsiback_map(struct vscsibk_info *info)
862{
863 struct xenbus_device *dev = info->dev;
864 unsigned int ring_ref, evtchn;
865 int err;
866
867 err = xenbus_gather(XBT_NIL, dev->otherend,
868 "ring-ref", "%u", &ring_ref,
869 "event-channel", "%u", &evtchn, NULL);
870 if (err) {
871 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
872 return err;
873 }
874
875 return scsiback_init_sring(info, ring_ref, evtchn);
876}
877
878/*
879 Add a new translation entry
880*/
881static int scsiback_add_translation_entry(struct vscsibk_info *info,
882 char *phy, struct ids_tuple *v)
883{
884 int err = 0;
885 struct v2p_entry *entry;
886 struct v2p_entry *new;
887 struct list_head *head = &(info->v2p_entry_lists);
888 unsigned long flags;
889 char *lunp;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200890 unsigned long long unpacked_lun;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700891 struct se_lun *se_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200892 struct scsiback_tpg *tpg_entry, *tpg = NULL;
893 char *error = "doesn't exist";
894
895 lunp = strrchr(phy, ':');
896 if (!lunp) {
Tao Chen78574872015-03-10 20:49:18 +0000897 pr_err("illegal format of physical device %s\n", phy);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200898 return -EINVAL;
899 }
900 *lunp = 0;
901 lunp++;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200902 err = kstrtoull(lunp, 10, &unpacked_lun);
903 if (err < 0) {
Tao Chen78574872015-03-10 20:49:18 +0000904 pr_err("lun number not valid: %s\n", lunp);
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200905 return err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200906 }
907
908 mutex_lock(&scsiback_mutex);
909 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
910 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
911 !strcmp(phy, tpg_entry->param_alias)) {
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700912 mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
913 hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
914 if (se_lun->unpacked_lun == unpacked_lun) {
915 if (!tpg_entry->tpg_nexus)
916 error = "nexus undefined";
917 else
918 tpg = tpg_entry;
919 break;
920 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200921 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700922 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200923 break;
924 }
925 }
926 if (tpg) {
927 mutex_lock(&tpg->tv_tpg_mutex);
928 tpg->tv_tpg_fe_count++;
929 mutex_unlock(&tpg->tv_tpg_mutex);
930 }
931 mutex_unlock(&scsiback_mutex);
932
933 if (!tpg) {
Nicholas Bellinger0fb1f142015-06-16 00:37:16 -0700934 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200935 return -ENODEV;
936 }
937
938 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
939 if (new == NULL) {
940 err = -ENOMEM;
941 goto out_free;
942 }
943
944 spin_lock_irqsave(&info->v2p_lock, flags);
945
946 /* Check double assignment to identical virtual ID */
947 list_for_each_entry(entry, head, l) {
948 if ((entry->v.chn == v->chn) &&
949 (entry->v.tgt == v->tgt) &&
950 (entry->v.lun == v->lun)) {
Tao Chen78574872015-03-10 20:49:18 +0000951 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200952 err = -EEXIST;
953 goto out;
954 }
955
956 }
957
958 /* Create a new translation entry and add to the list */
959 kref_init(&new->kref);
960 new->v = *v;
961 new->tpg = tpg;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700962 new->lun = unpacked_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200963 list_add_tail(&new->l, head);
964
965out:
966 spin_unlock_irqrestore(&info->v2p_lock, flags);
967
968out_free:
969 mutex_lock(&tpg->tv_tpg_mutex);
970 tpg->tv_tpg_fe_count--;
971 mutex_unlock(&tpg->tv_tpg_mutex);
972
973 if (err)
974 kfree(new);
975
976 return err;
977}
978
979static void __scsiback_del_translation_entry(struct v2p_entry *entry)
980{
981 list_del(&entry->l);
982 kref_put(&entry->kref, scsiback_free_translation_entry);
983}
984
985/*
986 Delete the translation entry specfied
987*/
988static int scsiback_del_translation_entry(struct vscsibk_info *info,
989 struct ids_tuple *v)
990{
991 struct v2p_entry *entry;
992 struct list_head *head = &(info->v2p_entry_lists);
993 unsigned long flags;
994
995 spin_lock_irqsave(&info->v2p_lock, flags);
996 /* Find out the translation entry specified */
997 list_for_each_entry(entry, head, l) {
998 if ((entry->v.chn == v->chn) &&
999 (entry->v.tgt == v->tgt) &&
1000 (entry->v.lun == v->lun)) {
1001 goto found;
1002 }
1003 }
1004
1005 spin_unlock_irqrestore(&info->v2p_lock, flags);
1006 return 1;
1007
1008found:
1009 /* Delete the translation entry specfied */
1010 __scsiback_del_translation_entry(entry);
1011
1012 spin_unlock_irqrestore(&info->v2p_lock, flags);
1013 return 0;
1014}
1015
1016static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
Juergen Gross169e6cf2015-02-17 08:02:48 +01001017 char *phy, struct ids_tuple *vir, int try)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001018{
1019 if (!scsiback_add_translation_entry(info, phy, vir)) {
1020 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1021 "%d", XenbusStateInitialised)) {
Tao Chen78574872015-03-10 20:49:18 +00001022 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001023 scsiback_del_translation_entry(info, vir);
1024 }
Juergen Gross169e6cf2015-02-17 08:02:48 +01001025 } else if (!try) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001026 xenbus_printf(XBT_NIL, info->dev->nodename, state,
1027 "%d", XenbusStateClosed);
1028 }
1029}
1030
1031static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1032 struct ids_tuple *vir)
1033{
1034 if (!scsiback_del_translation_entry(info, vir)) {
1035 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1036 "%d", XenbusStateClosed))
Tao Chen78574872015-03-10 20:49:18 +00001037 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001038 }
1039}
1040
1041#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1042#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1043
1044static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1045 char *ent)
1046{
1047 int err;
1048 struct ids_tuple vir;
1049 char *val;
1050 int device_state;
1051 char phy[VSCSI_NAMELEN];
1052 char str[64];
1053 char state[64];
1054 struct xenbus_device *dev = info->dev;
1055
1056 /* read status */
1057 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1058 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1059 if (XENBUS_EXIST_ERR(err))
1060 return;
1061
1062 /* physical SCSI device */
1063 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1064 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1065 if (IS_ERR(val)) {
1066 xenbus_printf(XBT_NIL, dev->nodename, state,
1067 "%d", XenbusStateClosed);
1068 return;
1069 }
1070 strlcpy(phy, val, VSCSI_NAMELEN);
1071 kfree(val);
1072
1073 /* virtual SCSI device */
1074 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1075 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1076 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1077 if (XENBUS_EXIST_ERR(err)) {
1078 xenbus_printf(XBT_NIL, dev->nodename, state,
1079 "%d", XenbusStateClosed);
1080 return;
1081 }
1082
1083 switch (op) {
1084 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
Juergen Gross169e6cf2015-02-17 08:02:48 +01001085 switch (device_state) {
1086 case XenbusStateInitialising:
1087 scsiback_do_add_lun(info, state, phy, &vir, 0);
1088 break;
1089 case XenbusStateConnected:
1090 scsiback_do_add_lun(info, state, phy, &vir, 1);
1091 break;
1092 case XenbusStateClosing:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001093 scsiback_do_del_lun(info, state, &vir);
Juergen Gross169e6cf2015-02-17 08:02:48 +01001094 break;
1095 default:
1096 break;
1097 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001098 break;
1099
1100 case VSCSIBACK_OP_UPDATEDEV_STATE:
1101 if (device_state == XenbusStateInitialised) {
1102 /* modify vscsi-devs/dev-x/state */
1103 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1104 "%d", XenbusStateConnected)) {
Tao Chen78574872015-03-10 20:49:18 +00001105 pr_err("xenbus_printf error %s\n", str);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001106 scsiback_del_translation_entry(info, &vir);
1107 xenbus_printf(XBT_NIL, dev->nodename, state,
1108 "%d", XenbusStateClosed);
1109 }
1110 }
1111 break;
Tao Chen78574872015-03-10 20:49:18 +00001112 /* When it is necessary, processing is added here. */
Juergen Grossd9d660f2014-08-28 06:44:12 +02001113 default:
1114 break;
1115 }
1116}
1117
1118static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1119{
1120 int i;
1121 char **dir;
1122 unsigned int ndir = 0;
1123
1124 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1125 &ndir);
1126 if (IS_ERR(dir))
1127 return;
1128
1129 for (i = 0; i < ndir; i++)
1130 scsiback_do_1lun_hotplug(info, op, dir[i]);
1131
1132 kfree(dir);
1133}
1134
1135static void scsiback_frontend_changed(struct xenbus_device *dev,
1136 enum xenbus_state frontend_state)
1137{
1138 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1139
1140 switch (frontend_state) {
1141 case XenbusStateInitialising:
1142 break;
1143
1144 case XenbusStateInitialised:
1145 if (scsiback_map(info))
1146 break;
1147
1148 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1149 xenbus_switch_state(dev, XenbusStateConnected);
1150 break;
1151
1152 case XenbusStateConnected:
1153 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1154
1155 if (dev->state == XenbusStateConnected)
1156 break;
1157
1158 xenbus_switch_state(dev, XenbusStateConnected);
1159 break;
1160
1161 case XenbusStateClosing:
1162 if (info->irq)
1163 scsiback_disconnect(info);
1164
1165 xenbus_switch_state(dev, XenbusStateClosing);
1166 break;
1167
1168 case XenbusStateClosed:
1169 xenbus_switch_state(dev, XenbusStateClosed);
1170 if (xenbus_dev_is_online(dev))
1171 break;
1172 /* fall through if not online */
1173 case XenbusStateUnknown:
1174 device_unregister(&dev->dev);
1175 break;
1176
1177 case XenbusStateReconfiguring:
1178 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1179 xenbus_switch_state(dev, XenbusStateReconfigured);
1180
1181 break;
1182
1183 default:
1184 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1185 frontend_state);
1186 break;
1187 }
1188}
1189
1190/*
1191 Release the translation entry specfied
1192*/
1193static void scsiback_release_translation_entry(struct vscsibk_info *info)
1194{
1195 struct v2p_entry *entry, *tmp;
1196 struct list_head *head = &(info->v2p_entry_lists);
1197 unsigned long flags;
1198
1199 spin_lock_irqsave(&info->v2p_lock, flags);
1200
1201 list_for_each_entry_safe(entry, tmp, head, l)
1202 __scsiback_del_translation_entry(entry);
1203
1204 spin_unlock_irqrestore(&info->v2p_lock, flags);
1205}
1206
1207static int scsiback_remove(struct xenbus_device *dev)
1208{
1209 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1210
1211 if (info->irq)
1212 scsiback_disconnect(info);
1213
1214 scsiback_release_translation_entry(info);
1215
1216 dev_set_drvdata(&dev->dev, NULL);
1217
1218 return 0;
1219}
1220
1221static int scsiback_probe(struct xenbus_device *dev,
1222 const struct xenbus_device_id *id)
1223{
1224 int err;
1225
1226 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1227 GFP_KERNEL);
1228
Tao Chen78574872015-03-10 20:49:18 +00001229 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001230
1231 if (!info) {
1232 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1233 return -ENOMEM;
1234 }
1235 info->dev = dev;
1236 dev_set_drvdata(&dev->dev, info);
1237
1238 info->domid = dev->otherend_id;
1239 spin_lock_init(&info->ring_lock);
1240 info->ring_error = 0;
1241 atomic_set(&info->nr_unreplied_reqs, 0);
1242 init_waitqueue_head(&info->waiting_to_free);
1243 info->dev = dev;
1244 info->irq = 0;
1245 INIT_LIST_HEAD(&info->v2p_entry_lists);
1246 spin_lock_init(&info->v2p_lock);
1247
1248 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1249 SG_ALL);
1250 if (err)
1251 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1252
1253 err = xenbus_switch_state(dev, XenbusStateInitWait);
1254 if (err)
1255 goto fail;
1256
1257 return 0;
1258
1259fail:
Tao Chen78574872015-03-10 20:49:18 +00001260 pr_warn("%s failed\n", __func__);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001261 scsiback_remove(dev);
1262
1263 return err;
1264}
1265
1266static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1267{
1268 switch (tport->tport_proto_id) {
1269 case SCSI_PROTOCOL_SAS:
1270 return "SAS";
1271 case SCSI_PROTOCOL_FCP:
1272 return "FCP";
1273 case SCSI_PROTOCOL_ISCSI:
1274 return "iSCSI";
1275 default:
1276 break;
1277 }
1278
1279 return "Unknown";
1280}
1281
Juergen Grossd9d660f2014-08-28 06:44:12 +02001282static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1283{
1284 struct scsiback_tpg *tpg = container_of(se_tpg,
1285 struct scsiback_tpg, se_tpg);
1286 struct scsiback_tport *tport = tpg->tport;
1287
1288 return &tport->tport_name[0];
1289}
1290
1291static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1292{
1293 struct scsiback_tpg *tpg = container_of(se_tpg,
1294 struct scsiback_tpg, se_tpg);
1295 return tpg->tport_tpgt;
1296}
1297
Juergen Grossd9d660f2014-08-28 06:44:12 +02001298static struct se_wwn *
1299scsiback_make_tport(struct target_fabric_configfs *tf,
1300 struct config_group *group,
1301 const char *name)
1302{
1303 struct scsiback_tport *tport;
1304 char *ptr;
1305 u64 wwpn = 0;
1306 int off = 0;
1307
1308 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1309 if (!tport)
1310 return ERR_PTR(-ENOMEM);
1311
1312 tport->tport_wwpn = wwpn;
1313 /*
1314 * Determine the emulated Protocol Identifier and Target Port Name
1315 * based on the incoming configfs directory name.
1316 */
1317 ptr = strstr(name, "naa.");
1318 if (ptr) {
1319 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1320 goto check_len;
1321 }
1322 ptr = strstr(name, "fc.");
1323 if (ptr) {
1324 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1325 off = 3; /* Skip over "fc." */
1326 goto check_len;
1327 }
1328 ptr = strstr(name, "iqn.");
1329 if (ptr) {
1330 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1331 goto check_len;
1332 }
1333
1334 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1335 kfree(tport);
1336 return ERR_PTR(-EINVAL);
1337
1338check_len:
1339 if (strlen(name) >= VSCSI_NAMELEN) {
1340 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1341 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1342 kfree(tport);
1343 return ERR_PTR(-EINVAL);
1344 }
1345 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1346
Tao Chen78574872015-03-10 20:49:18 +00001347 pr_debug("Allocated emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001348 scsiback_dump_proto_id(tport), name);
1349
1350 return &tport->tport_wwn;
1351}
1352
1353static void scsiback_drop_tport(struct se_wwn *wwn)
1354{
1355 struct scsiback_tport *tport = container_of(wwn,
1356 struct scsiback_tport, tport_wwn);
1357
Tao Chen78574872015-03-10 20:49:18 +00001358 pr_debug("Deallocating emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001359 scsiback_dump_proto_id(tport), tport->tport_name);
1360
1361 kfree(tport);
1362}
1363
Juergen Grossd9d660f2014-08-28 06:44:12 +02001364static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1365{
1366 return 1;
1367}
1368
1369static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1370{
1371 /*
Tao Chen78574872015-03-10 20:49:18 +00001372 * Do not release struct se_cmd's containing a valid TMR pointer.
1373 * These will be released directly in scsiback_device_action()
Juergen Grossd9d660f2014-08-28 06:44:12 +02001374 * with transport_generic_free_cmd().
1375 */
1376 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
1377 return 0;
1378
1379 transport_generic_free_cmd(se_cmd, 0);
1380 return 1;
1381}
1382
1383static void scsiback_release_cmd(struct se_cmd *se_cmd)
1384{
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001385 struct se_session *se_sess = se_cmd->se_sess;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001386
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001387 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001388}
1389
1390static int scsiback_shutdown_session(struct se_session *se_sess)
1391{
1392 return 0;
1393}
1394
1395static void scsiback_close_session(struct se_session *se_sess)
1396{
1397}
1398
1399static u32 scsiback_sess_get_index(struct se_session *se_sess)
1400{
1401 return 0;
1402}
1403
1404static int scsiback_write_pending(struct se_cmd *se_cmd)
1405{
1406 /* Go ahead and process the write immediately */
1407 target_execute_cmd(se_cmd);
1408
1409 return 0;
1410}
1411
1412static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1413{
1414 return 0;
1415}
1416
1417static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1418{
1419}
1420
Juergen Grossd9d660f2014-08-28 06:44:12 +02001421static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1422{
1423 return 0;
1424}
1425
1426static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1427{
1428 struct vscsibk_pend *pending_req = container_of(se_cmd,
1429 struct vscsibk_pend, se_cmd);
1430
1431 pending_req->result = SAM_STAT_GOOD;
1432 scsiback_cmd_done(pending_req);
1433 return 0;
1434}
1435
1436static int scsiback_queue_status(struct se_cmd *se_cmd)
1437{
1438 struct vscsibk_pend *pending_req = container_of(se_cmd,
1439 struct vscsibk_pend, se_cmd);
1440
1441 if (se_cmd->sense_buffer &&
1442 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1443 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1444 pending_req->result = (DRIVER_SENSE << 24) |
1445 SAM_STAT_CHECK_CONDITION;
1446 else
1447 pending_req->result = se_cmd->scsi_status;
1448
1449 scsiback_cmd_done(pending_req);
1450 return 0;
1451}
1452
1453static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1454{
1455 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1456 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1457
1458 atomic_set(&tmr->tmr_complete, 1);
1459 wake_up(&tmr->tmr_wait);
1460}
1461
1462static void scsiback_aborted_task(struct se_cmd *se_cmd)
1463{
1464}
1465
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001466static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001467 char *page)
1468{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001469 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001470 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1471 se_tpg);
1472 ssize_t rb;
1473
1474 mutex_lock(&tpg->tv_tpg_mutex);
1475 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1476 mutex_unlock(&tpg->tv_tpg_mutex);
1477
1478 return rb;
1479}
1480
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001481static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001482 const char *page, size_t count)
1483{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001484 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001485 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1486 se_tpg);
1487 int len;
1488
1489 if (strlen(page) >= VSCSI_NAMELEN) {
1490 pr_err("param alias: %s, exceeds max: %d\n", page,
1491 VSCSI_NAMELEN);
1492 return -EINVAL;
1493 }
1494
1495 mutex_lock(&tpg->tv_tpg_mutex);
1496 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1497 if (tpg->param_alias[len - 1] == '\n')
1498 tpg->param_alias[len - 1] = '\0';
1499 mutex_unlock(&tpg->tv_tpg_mutex);
1500
1501 return count;
1502}
1503
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001504CONFIGFS_ATTR(scsiback_tpg_param_, alias);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001505
1506static struct configfs_attribute *scsiback_param_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001507 &scsiback_tpg_param_attr_alias,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001508 NULL,
1509};
1510
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001511static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg,
1512 struct se_session *se_sess, void *p)
1513{
1514 struct scsiback_tpg *tpg = container_of(se_tpg,
1515 struct scsiback_tpg, se_tpg);
1516
1517 tpg->tpg_nexus = p;
1518 return 0;
1519}
1520
Juergen Grossd9d660f2014-08-28 06:44:12 +02001521static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1522 const char *name)
1523{
Juergen Grossd9d660f2014-08-28 06:44:12 +02001524 struct scsiback_nexus *tv_nexus;
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001525 int ret = 0;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001526
1527 mutex_lock(&tpg->tv_tpg_mutex);
1528 if (tpg->tpg_nexus) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001529 pr_debug("tpg->tpg_nexus already exists\n");
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001530 ret = -EEXIST;
1531 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001532 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001533
1534 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1535 if (!tv_nexus) {
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001536 ret = -ENOMEM;
1537 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001538 }
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001539
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001540 tv_nexus->tvn_se_sess = target_alloc_session(&tpg->se_tpg,
1541 VSCSI_DEFAULT_SESSION_TAGS,
1542 sizeof(struct vscsibk_pend),
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001543 TARGET_PROT_NORMAL, name,
1544 tv_nexus, scsiback_alloc_sess_cb);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001545 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001546 kfree(tv_nexus);
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001547 ret = -ENOMEM;
1548 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001549 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001550
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001551out_unlock:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001552 mutex_unlock(&tpg->tv_tpg_mutex);
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001553 return ret;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001554}
1555
1556static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1557{
1558 struct se_session *se_sess;
1559 struct scsiback_nexus *tv_nexus;
1560
1561 mutex_lock(&tpg->tv_tpg_mutex);
1562 tv_nexus = tpg->tpg_nexus;
1563 if (!tv_nexus) {
1564 mutex_unlock(&tpg->tv_tpg_mutex);
1565 return -ENODEV;
1566 }
1567
1568 se_sess = tv_nexus->tvn_se_sess;
1569 if (!se_sess) {
1570 mutex_unlock(&tpg->tv_tpg_mutex);
1571 return -ENODEV;
1572 }
1573
1574 if (tpg->tv_tpg_port_count != 0) {
1575 mutex_unlock(&tpg->tv_tpg_mutex);
1576 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1577 tpg->tv_tpg_port_count);
1578 return -EBUSY;
1579 }
1580
1581 if (tpg->tv_tpg_fe_count != 0) {
1582 mutex_unlock(&tpg->tv_tpg_mutex);
1583 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1584 tpg->tv_tpg_fe_count);
1585 return -EBUSY;
1586 }
1587
Tao Chen78574872015-03-10 20:49:18 +00001588 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001589 scsiback_dump_proto_id(tpg->tport),
1590 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1591
1592 /*
1593 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1594 */
1595 transport_deregister_session(tv_nexus->tvn_se_sess);
1596 tpg->tpg_nexus = NULL;
1597 mutex_unlock(&tpg->tv_tpg_mutex);
1598
1599 kfree(tv_nexus);
1600 return 0;
1601}
1602
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001603static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001604{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001605 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001606 struct scsiback_tpg *tpg = container_of(se_tpg,
1607 struct scsiback_tpg, se_tpg);
1608 struct scsiback_nexus *tv_nexus;
1609 ssize_t ret;
1610
1611 mutex_lock(&tpg->tv_tpg_mutex);
1612 tv_nexus = tpg->tpg_nexus;
1613 if (!tv_nexus) {
1614 mutex_unlock(&tpg->tv_tpg_mutex);
1615 return -ENODEV;
1616 }
1617 ret = snprintf(page, PAGE_SIZE, "%s\n",
1618 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1619 mutex_unlock(&tpg->tv_tpg_mutex);
1620
1621 return ret;
1622}
1623
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001624static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1625 const char *page, size_t count)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001626{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001627 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001628 struct scsiback_tpg *tpg = container_of(se_tpg,
1629 struct scsiback_tpg, se_tpg);
1630 struct scsiback_tport *tport_wwn = tpg->tport;
1631 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1632 int ret;
1633 /*
Tao Chen78574872015-03-10 20:49:18 +00001634 * Shutdown the active I_T nexus if 'NULL' is passed.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001635 */
1636 if (!strncmp(page, "NULL", 4)) {
1637 ret = scsiback_drop_nexus(tpg);
1638 return (!ret) ? count : ret;
1639 }
1640 /*
1641 * Otherwise make sure the passed virtual Initiator port WWN matches
1642 * the fabric protocol_id set in scsiback_make_tport(), and call
1643 * scsiback_make_nexus().
1644 */
1645 if (strlen(page) >= VSCSI_NAMELEN) {
1646 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1647 page, VSCSI_NAMELEN);
1648 return -EINVAL;
1649 }
1650 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1651
1652 ptr = strstr(i_port, "naa.");
1653 if (ptr) {
1654 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1655 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1656 i_port, scsiback_dump_proto_id(tport_wwn));
1657 return -EINVAL;
1658 }
1659 port_ptr = &i_port[0];
1660 goto check_newline;
1661 }
1662 ptr = strstr(i_port, "fc.");
1663 if (ptr) {
1664 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1665 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1666 i_port, scsiback_dump_proto_id(tport_wwn));
1667 return -EINVAL;
1668 }
1669 port_ptr = &i_port[3]; /* Skip over "fc." */
1670 goto check_newline;
1671 }
1672 ptr = strstr(i_port, "iqn.");
1673 if (ptr) {
1674 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1675 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1676 i_port, scsiback_dump_proto_id(tport_wwn));
1677 return -EINVAL;
1678 }
1679 port_ptr = &i_port[0];
1680 goto check_newline;
1681 }
1682 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1683 i_port);
1684 return -EINVAL;
1685 /*
1686 * Clear any trailing newline for the NAA WWN
1687 */
1688check_newline:
1689 if (i_port[strlen(i_port) - 1] == '\n')
1690 i_port[strlen(i_port) - 1] = '\0';
1691
1692 ret = scsiback_make_nexus(tpg, port_ptr);
1693 if (ret < 0)
1694 return ret;
1695
1696 return count;
1697}
1698
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001699CONFIGFS_ATTR(scsiback_tpg_, nexus);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001700
1701static struct configfs_attribute *scsiback_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001702 &scsiback_tpg_attr_nexus,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001703 NULL,
1704};
1705
1706static ssize_t
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001707scsiback_wwn_version_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001708{
1709 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1710 UTS_RELEASE"\n",
1711 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1712}
1713
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001714CONFIGFS_ATTR_RO(scsiback_wwn_, version);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001715
1716static struct configfs_attribute *scsiback_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001717 &scsiback_wwn_attr_version,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001718 NULL,
1719};
1720
1721static char *scsiback_get_fabric_name(void)
1722{
1723 return "xen-pvscsi";
1724}
1725
1726static int scsiback_port_link(struct se_portal_group *se_tpg,
1727 struct se_lun *lun)
1728{
1729 struct scsiback_tpg *tpg = container_of(se_tpg,
1730 struct scsiback_tpg, se_tpg);
1731
1732 mutex_lock(&tpg->tv_tpg_mutex);
1733 tpg->tv_tpg_port_count++;
1734 mutex_unlock(&tpg->tv_tpg_mutex);
1735
1736 return 0;
1737}
1738
1739static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1740 struct se_lun *lun)
1741{
1742 struct scsiback_tpg *tpg = container_of(se_tpg,
1743 struct scsiback_tpg, se_tpg);
1744
1745 mutex_lock(&tpg->tv_tpg_mutex);
1746 tpg->tv_tpg_port_count--;
1747 mutex_unlock(&tpg->tv_tpg_mutex);
1748}
1749
1750static struct se_portal_group *
1751scsiback_make_tpg(struct se_wwn *wwn,
1752 struct config_group *group,
1753 const char *name)
1754{
1755 struct scsiback_tport *tport = container_of(wwn,
1756 struct scsiback_tport, tport_wwn);
1757
1758 struct scsiback_tpg *tpg;
Dan Carpenter495daef2014-09-08 14:17:35 +03001759 u16 tpgt;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001760 int ret;
1761
1762 if (strstr(name, "tpgt_") != name)
1763 return ERR_PTR(-EINVAL);
Dan Carpenter495daef2014-09-08 14:17:35 +03001764 ret = kstrtou16(name + 5, 10, &tpgt);
1765 if (ret)
1766 return ERR_PTR(ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001767
1768 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1769 if (!tpg)
1770 return ERR_PTR(-ENOMEM);
1771
1772 mutex_init(&tpg->tv_tpg_mutex);
1773 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1774 INIT_LIST_HEAD(&tpg->info_list);
1775 tpg->tport = tport;
1776 tpg->tport_tpgt = tpgt;
1777
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07001778 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001779 if (ret < 0) {
1780 kfree(tpg);
1781 return NULL;
1782 }
1783 mutex_lock(&scsiback_mutex);
1784 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1785 mutex_unlock(&scsiback_mutex);
1786
1787 return &tpg->se_tpg;
1788}
1789
1790static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1791{
1792 struct scsiback_tpg *tpg = container_of(se_tpg,
1793 struct scsiback_tpg, se_tpg);
1794
1795 mutex_lock(&scsiback_mutex);
1796 list_del(&tpg->tv_tpg_list);
1797 mutex_unlock(&scsiback_mutex);
1798 /*
1799 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1800 */
1801 scsiback_drop_nexus(tpg);
1802 /*
Tao Chen78574872015-03-10 20:49:18 +00001803 * Deregister the se_tpg from TCM.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001804 */
1805 core_tpg_deregister(se_tpg);
1806 kfree(tpg);
1807}
1808
1809static int scsiback_check_true(struct se_portal_group *se_tpg)
1810{
1811 return 1;
1812}
1813
1814static int scsiback_check_false(struct se_portal_group *se_tpg)
1815{
1816 return 0;
1817}
1818
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001819static const struct target_core_fabric_ops scsiback_ops = {
1820 .module = THIS_MODULE,
1821 .name = "xen-pvscsi",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001822 .get_fabric_name = scsiback_get_fabric_name,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001823 .tpg_get_wwn = scsiback_get_fabric_wwn,
1824 .tpg_get_tag = scsiback_get_tag,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001825 .tpg_check_demo_mode = scsiback_check_true,
1826 .tpg_check_demo_mode_cache = scsiback_check_true,
1827 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1828 .tpg_check_prod_mode_write_protect = scsiback_check_false,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001829 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1830 .check_stop_free = scsiback_check_stop_free,
1831 .release_cmd = scsiback_release_cmd,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001832 .shutdown_session = scsiback_shutdown_session,
1833 .close_session = scsiback_close_session,
1834 .sess_get_index = scsiback_sess_get_index,
1835 .sess_get_initiator_sid = NULL,
1836 .write_pending = scsiback_write_pending,
1837 .write_pending_status = scsiback_write_pending_status,
1838 .set_default_node_attributes = scsiback_set_default_node_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001839 .get_cmd_state = scsiback_get_cmd_state,
1840 .queue_data_in = scsiback_queue_data_in,
1841 .queue_status = scsiback_queue_status,
1842 .queue_tm_rsp = scsiback_queue_tm_rsp,
1843 .aborted_task = scsiback_aborted_task,
1844 /*
1845 * Setup callers for generic logic in target_core_fabric_configfs.c
1846 */
1847 .fabric_make_wwn = scsiback_make_tport,
1848 .fabric_drop_wwn = scsiback_drop_tport,
1849 .fabric_make_tpg = scsiback_make_tpg,
1850 .fabric_drop_tpg = scsiback_drop_tpg,
1851 .fabric_post_link = scsiback_port_link,
1852 .fabric_pre_unlink = scsiback_port_unlink,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001853
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001854 .tfc_wwn_attrs = scsiback_wwn_attrs,
1855 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1856 .tfc_tpg_param_attrs = scsiback_param_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001857};
1858
1859static const struct xenbus_device_id scsiback_ids[] = {
1860 { "vscsi" },
1861 { "" }
1862};
1863
David Vrabel95afae42014-09-08 17:30:41 +01001864static struct xenbus_driver scsiback_driver = {
1865 .ids = scsiback_ids,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001866 .probe = scsiback_probe,
1867 .remove = scsiback_remove,
1868 .otherend_changed = scsiback_frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001869};
Juergen Grossd9d660f2014-08-28 06:44:12 +02001870
Juergen Grossd9d660f2014-08-28 06:44:12 +02001871static int __init scsiback_init(void)
1872{
1873 int ret;
1874
1875 if (!xen_domain())
1876 return -ENODEV;
1877
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001878 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1879 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1880
Juergen Grossd9d660f2014-08-28 06:44:12 +02001881 ret = xenbus_register_backend(&scsiback_driver);
1882 if (ret)
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001883 goto out;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001884
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001885 ret = target_register_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001886 if (ret)
1887 goto out_unregister_xenbus;
1888
1889 return 0;
1890
1891out_unregister_xenbus:
1892 xenbus_unregister_driver(&scsiback_driver);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001893out:
Tao Chen78574872015-03-10 20:49:18 +00001894 pr_err("%s: error %d\n", __func__, ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001895 return ret;
1896}
1897
1898static void __exit scsiback_exit(void)
1899{
1900 struct page *page;
1901
1902 while (free_pages_num) {
1903 if (get_free_page(&page))
1904 BUG();
David Vrabelff4b1562015-01-08 18:06:01 +00001905 gnttab_free_pages(1, &page);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001906 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001907 target_unregister_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001908 xenbus_unregister_driver(&scsiback_driver);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001909}
1910
1911module_init(scsiback_init);
1912module_exit(scsiback_exit);
1913
1914MODULE_DESCRIPTION("Xen SCSI backend driver");
1915MODULE_LICENSE("Dual BSD/GPL");
1916MODULE_ALIAS("xen-backend:vscsi");
1917MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");