blob: 1e8cfd80a4e6b45c066811156b82a9f63ea4c725 [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;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200137
Bart Van Asschee3eac122017-05-23 16:48:37 -0700138 struct completion tmr_done;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200139};
140
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800141#define VSCSI_DEFAULT_SESSION_TAGS 128
142
Juergen Grossd9d660f2014-08-28 06:44:12 +0200143struct scsiback_nexus {
144 /* Pointer to TCM session for I_T Nexus */
145 struct se_session *tvn_se_sess;
146};
147
148struct scsiback_tport {
149 /* SCSI protocol the tport is providing */
150 u8 tport_proto_id;
151 /* Binary World Wide unique Port Name for pvscsi Target port */
152 u64 tport_wwpn;
153 /* ASCII formatted WWPN for pvscsi Target port */
154 char tport_name[VSCSI_NAMELEN];
155 /* Returned by scsiback_make_tport() */
156 struct se_wwn tport_wwn;
157};
158
159struct scsiback_tpg {
160 /* scsiback port target portal group tag for TCM */
161 u16 tport_tpgt;
162 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
163 int tv_tpg_port_count;
164 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
165 int tv_tpg_fe_count;
166 /* list for scsiback_list */
167 struct list_head tv_tpg_list;
168 /* Used to protect access for tpg_nexus */
169 struct mutex tv_tpg_mutex;
170 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
171 struct scsiback_nexus *tpg_nexus;
172 /* Pointer back to scsiback_tport */
173 struct scsiback_tport *tport;
174 /* Returned by scsiback_make_tpg() */
175 struct se_portal_group se_tpg;
176 /* alias used in xenstore */
177 char param_alias[VSCSI_NAMELEN];
178 /* list of info structures related to this target portal group */
179 struct list_head info_list;
180};
181
182#define SCSIBACK_INVALID_HANDLE (~0)
183
184static bool log_print_stat;
185module_param(log_print_stat, bool, 0644);
186
187static int scsiback_max_buffer_pages = 1024;
188module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
189MODULE_PARM_DESC(max_buffer_pages,
190"Maximum number of free pages to keep in backend buffer");
191
Juergen Grossd9d660f2014-08-28 06:44:12 +0200192static DEFINE_SPINLOCK(free_pages_lock);
193static int free_pages_num;
194static LIST_HEAD(scsiback_free_pages);
195
196/* Global spinlock to protect scsiback TPG list */
197static DEFINE_MUTEX(scsiback_mutex);
198static LIST_HEAD(scsiback_list);
199
Juergen Grossd9d660f2014-08-28 06:44:12 +0200200static void scsiback_get(struct vscsibk_info *info)
201{
202 atomic_inc(&info->nr_unreplied_reqs);
203}
204
205static void scsiback_put(struct vscsibk_info *info)
206{
207 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
208 wake_up(&info->waiting_to_free);
209}
210
211static void put_free_pages(struct page **page, int num)
212{
213 unsigned long flags;
214 int i = free_pages_num + num, n = num;
215
216 if (num == 0)
217 return;
218 if (i > scsiback_max_buffer_pages) {
219 n = min(num, i - scsiback_max_buffer_pages);
David Vrabelff4b1562015-01-08 18:06:01 +0000220 gnttab_free_pages(n, page + num - n);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200221 n = num - n;
222 }
223 spin_lock_irqsave(&free_pages_lock, flags);
224 for (i = 0; i < n; i++)
225 list_add(&page[i]->lru, &scsiback_free_pages);
226 free_pages_num += n;
227 spin_unlock_irqrestore(&free_pages_lock, flags);
228}
229
230static int get_free_page(struct page **page)
231{
232 unsigned long flags;
233
234 spin_lock_irqsave(&free_pages_lock, flags);
235 if (list_empty(&scsiback_free_pages)) {
236 spin_unlock_irqrestore(&free_pages_lock, flags);
David Vrabelff4b1562015-01-08 18:06:01 +0000237 return gnttab_alloc_pages(1, page);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200238 }
239 page[0] = list_first_entry(&scsiback_free_pages, struct page, lru);
240 list_del(&page[0]->lru);
241 free_pages_num--;
242 spin_unlock_irqrestore(&free_pages_lock, flags);
243 return 0;
244}
245
246static unsigned long vaddr_page(struct page *page)
247{
248 unsigned long pfn = page_to_pfn(page);
249
250 return (unsigned long)pfn_to_kaddr(pfn);
251}
252
253static unsigned long vaddr(struct vscsibk_pend *req, int seg)
254{
255 return vaddr_page(req->pages[seg]);
256}
257
258static void scsiback_print_status(char *sense_buffer, int errors,
259 struct vscsibk_pend *pending_req)
260{
261 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
262
Tao Chen78574872015-03-10 20:49:18 +0000263 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200264 tpg->tport->tport_name, pending_req->v2p->lun,
265 pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
266 host_byte(errors), driver_byte(errors));
Juergen Grossd9d660f2014-08-28 06:44:12 +0200267}
268
269static void scsiback_fast_flush_area(struct vscsibk_pend *req)
270{
271 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
272 struct page *pages[VSCSI_GRANT_BATCH];
273 unsigned int i, invcount = 0;
274 grant_handle_t handle;
275 int err;
276
277 kfree(req->sgl);
278 req->sgl = NULL;
279 req->n_sg = 0;
280
281 if (!req->n_grants)
282 return;
283
284 for (i = 0; i < req->n_grants; i++) {
285 handle = req->grant_handles[i];
286 if (handle == SCSIBACK_INVALID_HANDLE)
287 continue;
288 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
289 GNTMAP_host_map, handle);
290 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
291 pages[invcount] = req->pages[i];
292 put_page(pages[invcount]);
293 invcount++;
294 if (invcount < VSCSI_GRANT_BATCH)
295 continue;
296 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
297 BUG_ON(err);
298 invcount = 0;
299 }
300
301 if (invcount) {
302 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
303 BUG_ON(err);
304 }
305
306 put_free_pages(req->pages, req->n_grants);
307 req->n_grants = 0;
308}
309
310static void scsiback_free_translation_entry(struct kref *kref)
311{
312 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
313 struct scsiback_tpg *tpg = entry->tpg;
314
315 mutex_lock(&tpg->tv_tpg_mutex);
316 tpg->tv_tpg_fe_count--;
317 mutex_unlock(&tpg->tv_tpg_mutex);
318
319 kfree(entry);
320}
321
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800322static void scsiback_send_response(struct vscsibk_info *info,
323 char *sense_buffer, int32_t result, uint32_t resid,
324 uint16_t rqid)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200325{
326 struct vscsiif_response *ring_res;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200327 int notify;
328 struct scsi_sense_hdr sshdr;
329 unsigned long flags;
330 unsigned len;
331
332 spin_lock_irqsave(&info->ring_lock, flags);
333
334 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
335 info->ring.rsp_prod_pvt++;
336
337 ring_res->rslt = result;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800338 ring_res->rqid = rqid;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200339
340 if (sense_buffer != NULL &&
341 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
342 &sshdr)) {
343 len = min_t(unsigned, 8 + sense_buffer[7],
344 VSCSIIF_SENSE_BUFFERSIZE);
345 memcpy(ring_res->sense_buffer, sense_buffer, len);
346 ring_res->sense_len = len;
347 } else {
348 ring_res->sense_len = 0;
349 }
350
351 ring_res->residual_len = resid;
352
353 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
354 spin_unlock_irqrestore(&info->ring_lock, flags);
355
356 if (notify)
357 notify_remote_via_irq(info->irq);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800358}
359
360static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
361 uint32_t resid, struct vscsibk_pend *pending_req)
362{
363 scsiback_send_response(pending_req->info, sense_buffer, result,
364 resid, pending_req->rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200365
366 if (pending_req->v2p)
367 kref_put(&pending_req->v2p->kref,
368 scsiback_free_translation_entry);
369}
370
371static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
372{
373 struct vscsibk_info *info = pending_req->info;
374 unsigned char *sense_buffer;
375 unsigned int resid;
376 int errors;
377
378 sense_buffer = pending_req->sense_buffer;
379 resid = pending_req->se_cmd.residual_count;
380 errors = pending_req->result;
381
382 if (errors && log_print_stat)
383 scsiback_print_status(sense_buffer, errors, pending_req);
384
385 scsiback_fast_flush_area(pending_req);
386 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
387 scsiback_put(info);
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800388 /*
389 * Drop the extra KREF_ACK reference taken by target_submit_cmd_map_sgls()
390 * ahead of scsiback_check_stop_free() -> transport_generic_free_cmd()
391 * final se_cmd->cmd_kref put.
392 */
393 target_put_sess_cmd(&pending_req->se_cmd);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200394}
395
396static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
397{
398 struct se_cmd *se_cmd = &pending_req->se_cmd;
399 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
400 int rc;
401
Juergen Grossd9d660f2014-08-28 06:44:12 +0200402 scsiback_get(pending_req->info);
Bart Van Assche649ee052015-04-14 13:26:44 +0200403 se_cmd->tag = pending_req->rqid;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200404 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
405 pending_req->sense_buffer, pending_req->v2p->lun,
406 pending_req->data_len, 0,
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800407 pending_req->sc_data_direction, TARGET_SCF_ACK_KREF,
Juergen Grossd9d660f2014-08-28 06:44:12 +0200408 pending_req->sgl, pending_req->n_sg,
409 NULL, 0, NULL, 0);
410 if (rc < 0) {
411 transport_send_check_condition_and_sense(se_cmd,
412 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
413 transport_generic_free_cmd(se_cmd, 0);
414 }
415}
416
417static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
418 struct page **pg, grant_handle_t *grant, int cnt)
419{
420 int err, i;
421
422 if (!cnt)
423 return 0;
424
425 err = gnttab_map_refs(map, NULL, pg, cnt);
426 BUG_ON(err);
427 for (i = 0; i < cnt; i++) {
428 if (unlikely(map[i].status != GNTST_okay)) {
Tao Chen78574872015-03-10 20:49:18 +0000429 pr_err("invalid buffer -- could not remap it\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200430 map[i].handle = SCSIBACK_INVALID_HANDLE;
431 err = -ENOMEM;
432 } else {
433 get_page(pg[i]);
434 }
435 grant[i] = map[i].handle;
436 }
437 return err;
438}
439
440static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
441 struct scsiif_request_segment *seg, struct page **pg,
442 grant_handle_t *grant, int cnt, u32 flags)
443{
444 int mapcount = 0, i, err = 0;
445 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
446 struct vscsibk_info *info = pending_req->info;
447
448 for (i = 0; i < cnt; i++) {
449 if (get_free_page(pg + mapcount)) {
450 put_free_pages(pg, mapcount);
Tao Chen78574872015-03-10 20:49:18 +0000451 pr_err("no grant page\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200452 return -ENOMEM;
453 }
454 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
455 flags, seg[i].gref, info->domid);
456 mapcount++;
457 if (mapcount < VSCSI_GRANT_BATCH)
458 continue;
459 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
460 pg += mapcount;
461 grant += mapcount;
462 pending_req->n_grants += mapcount;
463 if (err)
464 return err;
465 mapcount = 0;
466 }
467 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
468 pending_req->n_grants += mapcount;
469 return err;
470}
471
472static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
473 struct vscsibk_pend *pending_req)
474{
475 u32 flags;
476 int i, err, n_segs, i_seg = 0;
477 struct page **pg;
478 struct scsiif_request_segment *seg;
479 unsigned long end_seg = 0;
480 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
481 unsigned int nr_sgl = 0;
482 struct scatterlist *sg;
483 grant_handle_t *grant;
484
485 pending_req->n_sg = 0;
486 pending_req->n_grants = 0;
487 pending_req->data_len = 0;
488
489 nr_segments &= ~VSCSIIF_SG_GRANT;
490 if (!nr_segments)
491 return 0;
492
493 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
Tao Chen78574872015-03-10 20:49:18 +0000494 pr_debug("invalid parameter nr_seg = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200495 ring_req->nr_segments);
496 return -EINVAL;
497 }
498
499 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
500 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
501 pending_req->pages, pending_req->grant_handles,
502 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
503 if (err)
504 return err;
505 nr_sgl = nr_segments;
506 nr_segments = 0;
507 for (i = 0; i < nr_sgl; i++) {
508 n_segs = ring_req->seg[i].length /
509 sizeof(struct scsiif_request_segment);
510 if ((unsigned)ring_req->seg[i].offset +
511 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
512 n_segs * sizeof(struct scsiif_request_segment) !=
513 ring_req->seg[i].length)
514 return -EINVAL;
515 nr_segments += n_segs;
516 }
517 if (nr_segments > SG_ALL) {
Tao Chen78574872015-03-10 20:49:18 +0000518 pr_debug("invalid nr_seg = %d\n", nr_segments);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200519 return -EINVAL;
520 }
521 }
522
Tao Chen78574872015-03-10 20:49:18 +0000523 /* free of (sgl) in fast_flush_area() */
Juergen Grossd9d660f2014-08-28 06:44:12 +0200524 pending_req->sgl = kmalloc_array(nr_segments,
525 sizeof(struct scatterlist), GFP_KERNEL);
526 if (!pending_req->sgl)
527 return -ENOMEM;
528
529 sg_init_table(pending_req->sgl, nr_segments);
530 pending_req->n_sg = nr_segments;
531
532 flags = GNTMAP_host_map;
533 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
534 flags |= GNTMAP_readonly;
535
536 pg = pending_req->pages + nr_sgl;
537 grant = pending_req->grant_handles + nr_sgl;
538 if (!nr_sgl) {
539 seg = ring_req->seg;
540 err = scsiback_gnttab_data_map_list(pending_req, seg,
541 pg, grant, nr_segments, flags);
542 if (err)
543 return err;
544 } else {
545 for (i = 0; i < nr_sgl; i++) {
546 seg = (struct scsiif_request_segment *)(
547 vaddr(pending_req, i) + ring_req->seg[i].offset);
548 n_segs = ring_req->seg[i].length /
549 sizeof(struct scsiif_request_segment);
550 err = scsiback_gnttab_data_map_list(pending_req, seg,
551 pg, grant, n_segs, flags);
552 if (err)
553 return err;
554 pg += n_segs;
555 grant += n_segs;
556 }
557 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
558 seg = (struct scsiif_request_segment *)end_seg;
559 end_seg += ring_req->seg[0].length;
560 pg = pending_req->pages + nr_sgl;
561 }
562
563 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
564 sg_set_page(sg, pg[i], seg->length, seg->offset);
565 pending_req->data_len += seg->length;
566 seg++;
567 if (nr_sgl && (unsigned long)seg >= end_seg) {
568 i_seg++;
569 end_seg = vaddr(pending_req, i_seg) +
570 ring_req->seg[i_seg].offset;
571 seg = (struct scsiif_request_segment *)end_seg;
572 end_seg += ring_req->seg[i_seg].length;
573 }
574 if (sg->offset >= PAGE_SIZE ||
575 sg->length > PAGE_SIZE ||
576 sg->offset + sg->length > PAGE_SIZE)
577 return -EINVAL;
578 }
579
580 return 0;
581}
582
583static void scsiback_disconnect(struct vscsibk_info *info)
584{
585 wait_event(info->waiting_to_free,
586 atomic_read(&info->nr_unreplied_reqs) == 0);
587
588 unbind_from_irqhandler(info->irq, info);
589 info->irq = 0;
590 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
591}
592
593static void scsiback_device_action(struct vscsibk_pend *pending_req,
594 enum tcm_tmreq_table act, int tag)
595{
Juergen Grossd9d660f2014-08-28 06:44:12 +0200596 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800597 struct scsiback_nexus *nexus = tpg->tpg_nexus;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200598 struct se_cmd *se_cmd = &pending_req->se_cmd;
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800599 u64 unpacked_lun = pending_req->v2p->lun;
600 int rc, err = FAILED;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200601
Bart Van Asschee3eac122017-05-23 16:48:37 -0700602 init_completion(&pending_req->tmr_done);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200603
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800604 rc = target_submit_tmr(&pending_req->se_cmd, nexus->tvn_se_sess,
605 &pending_req->sense_buffer[0],
Bart Van Assche9f4ab182017-05-23 16:48:36 -0700606 unpacked_lun, NULL, act, GFP_KERNEL,
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800607 tag, TARGET_SCF_ACK_KREF);
608 if (rc)
609 goto err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200610
Bart Van Asschee3eac122017-05-23 16:48:37 -0700611 wait_for_completion(&pending_req->tmr_done);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200612
613 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
614 SUCCESS : FAILED;
615
Juergen Grossd9d660f2014-08-28 06:44:12 +0200616 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
Bart Van Asscheaf90e842017-05-23 16:48:38 -0700617 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800618 return;
Bart Van Assche9f4ab182017-05-23 16:48:36 -0700619
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800620err:
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800621 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200622}
623
624/*
625 Perform virtual to physical translation
626*/
627static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
628 struct ids_tuple *v)
629{
630 struct v2p_entry *entry;
631 struct list_head *head = &(info->v2p_entry_lists);
632 unsigned long flags;
633
634 spin_lock_irqsave(&info->v2p_lock, flags);
635 list_for_each_entry(entry, head, l) {
636 if ((entry->v.chn == v->chn) &&
637 (entry->v.tgt == v->tgt) &&
638 (entry->v.lun == v->lun)) {
639 kref_get(&entry->kref);
640 goto out;
641 }
642 }
643 entry = NULL;
644
645out:
646 spin_unlock_irqrestore(&info->v2p_lock, flags);
647 return entry;
648}
649
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800650static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring,
651 struct v2p_entry *v2p)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200652{
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800653 struct scsiback_tpg *tpg = v2p->tpg;
654 struct scsiback_nexus *nexus = tpg->tpg_nexus;
655 struct se_session *se_sess = nexus->tvn_se_sess;
656 struct vscsibk_pend *req;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -0700657 int tag, cpu, i;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800658
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -0700659 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800660 if (tag < 0) {
661 pr_err("Unable to obtain tag for vscsiif_request\n");
662 return ERR_PTR(-ENOMEM);
663 }
664
665 req = &((struct vscsibk_pend *)se_sess->sess_cmd_map)[tag];
666 memset(req, 0, sizeof(*req));
667 req->se_cmd.map_tag = tag;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -0700668 req->se_cmd.map_cpu = cpu;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800669
670 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
671 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
672
673 return req;
674}
675
676static struct vscsibk_pend *prepare_pending_reqs(struct vscsibk_info *info,
677 struct vscsiif_back_ring *ring,
678 struct vscsiif_request *ring_req)
679{
680 struct vscsibk_pend *pending_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200681 struct v2p_entry *v2p;
682 struct ids_tuple vir;
683
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800684 /* request range check from frontend */
685 if ((ring_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
686 (ring_req->sc_data_direction != DMA_TO_DEVICE) &&
687 (ring_req->sc_data_direction != DMA_FROM_DEVICE) &&
688 (ring_req->sc_data_direction != DMA_NONE)) {
689 pr_debug("invalid parameter data_dir = %d\n",
690 ring_req->sc_data_direction);
691 return ERR_PTR(-EINVAL);
692 }
693 if (ring_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
694 pr_debug("invalid parameter cmd_len = %d\n",
695 ring_req->cmd_len);
696 return ERR_PTR(-EINVAL);
697 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200698
699 vir.chn = ring_req->channel;
700 vir.tgt = ring_req->id;
701 vir.lun = ring_req->lun;
702
703 v2p = scsiback_do_translation(info, &vir);
704 if (!v2p) {
Tao Chen78574872015-03-10 20:49:18 +0000705 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800706 vir.chn, vir.tgt, vir.lun);
707 return ERR_PTR(-ENODEV);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200708 }
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800709
710 pending_req = scsiback_get_pend_req(ring, v2p);
711 if (IS_ERR(pending_req)) {
712 kref_put(&v2p->kref, scsiback_free_translation_entry);
713 return ERR_PTR(-ENOMEM);
714 }
715 pending_req->rqid = ring_req->rqid;
716 pending_req->info = info;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200717 pending_req->v2p = v2p;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200718 pending_req->sc_data_direction = ring_req->sc_data_direction;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200719 pending_req->cmd_len = ring_req->cmd_len;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200720 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
721
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800722 return pending_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200723}
724
725static int scsiback_do_cmd_fn(struct vscsibk_info *info)
726{
727 struct vscsiif_back_ring *ring = &info->ring;
Juergen Grossfacb5732015-02-17 08:02:47 +0100728 struct vscsiif_request ring_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200729 struct vscsibk_pend *pending_req;
730 RING_IDX rc, rp;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800731 int more_to_do;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200732 uint32_t result;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200733
734 rc = ring->req_cons;
735 rp = ring->sring->req_prod;
736 rmb(); /* guest system is accessing ring, too */
737
738 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
739 rc = ring->rsp_prod_pvt;
Tao Chen78574872015-03-10 20:49:18 +0000740 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200741 info->domid, rp, rc, rp - rc);
742 info->ring_error = 1;
743 return 0;
744 }
745
746 while ((rc != rp)) {
747 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
748 break;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200749
David Vrabelbe697462015-11-16 18:02:32 +0000750 RING_COPY_REQUEST(ring, rc, &ring_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200751 ring->req_cons = ++rc;
752
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800753 pending_req = prepare_pending_reqs(info, ring, &ring_req);
754 if (IS_ERR(pending_req)) {
755 switch (PTR_ERR(pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200756 case -ENODEV:
757 result = DID_NO_CONNECT;
758 break;
759 default:
760 result = DRIVER_ERROR;
761 break;
762 }
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800763 scsiback_send_response(info, NULL, result << 24, 0,
764 ring_req.rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200765 return 1;
766 }
767
Juergen Grossfacb5732015-02-17 08:02:47 +0100768 switch (ring_req.act) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200769 case VSCSIIF_ACT_SCSI_CDB:
Juergen Grossfacb5732015-02-17 08:02:47 +0100770 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200771 scsiback_fast_flush_area(pending_req);
772 scsiback_do_resp_with_sense(NULL,
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800773 DRIVER_ERROR << 24, 0, pending_req);
774 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200775 } else {
776 scsiback_cmd_exec(pending_req);
777 }
778 break;
779 case VSCSIIF_ACT_SCSI_ABORT:
780 scsiback_device_action(pending_req, TMR_ABORT_TASK,
Juergen Grossfacb5732015-02-17 08:02:47 +0100781 ring_req.ref_rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200782 break;
783 case VSCSIIF_ACT_SCSI_RESET:
784 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
785 break;
786 default:
Tao Chen78574872015-03-10 20:49:18 +0000787 pr_err_ratelimited("invalid request\n");
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800788 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24, 0,
789 pending_req);
790 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200791 break;
792 }
793
794 /* Yield point for this unbounded loop. */
795 cond_resched();
796 }
797
798 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
799 return more_to_do;
800}
801
802static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
803{
804 struct vscsibk_info *info = dev_id;
805
806 if (info->ring_error)
807 return IRQ_HANDLED;
808
809 while (scsiback_do_cmd_fn(info))
810 cond_resched();
811
812 return IRQ_HANDLED;
813}
814
815static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
816 evtchn_port_t evtchn)
817{
818 void *area;
819 struct vscsiif_sring *sring;
820 int err;
821
822 if (info->irq)
823 return -1;
824
Wei Liuccc9d902015-04-03 14:44:59 +0800825 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200826 if (err)
827 return err;
828
829 sring = (struct vscsiif_sring *)area;
830 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
831
832 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
833 if (err < 0)
834 goto unmap_page;
835
836 info->irq = err;
837
838 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
839 IRQF_ONESHOT, "vscsiif-backend", info);
840 if (err)
841 goto free_irq;
842
843 return 0;
844
845free_irq:
846 unbind_from_irqhandler(info->irq, info);
847 info->irq = 0;
848unmap_page:
849 xenbus_unmap_ring_vfree(info->dev, area);
850
851 return err;
852}
853
854static int scsiback_map(struct vscsibk_info *info)
855{
856 struct xenbus_device *dev = info->dev;
Yan Yankovskyi0102e4e2020-03-23 18:15:11 +0200857 unsigned int ring_ref;
858 evtchn_port_t evtchn;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200859 int err;
860
861 err = xenbus_gather(XBT_NIL, dev->otherend,
862 "ring-ref", "%u", &ring_ref,
863 "event-channel", "%u", &evtchn, NULL);
864 if (err) {
865 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
866 return err;
867 }
868
869 return scsiback_init_sring(info, ring_ref, evtchn);
870}
871
872/*
Juergen Grossc9e2f532016-02-08 15:30:19 +0100873 Check for a translation entry being present
874*/
875static struct v2p_entry *scsiback_chk_translation_entry(
876 struct vscsibk_info *info, struct ids_tuple *v)
877{
878 struct list_head *head = &(info->v2p_entry_lists);
879 struct v2p_entry *entry;
880
881 list_for_each_entry(entry, head, l)
882 if ((entry->v.chn == v->chn) &&
883 (entry->v.tgt == v->tgt) &&
884 (entry->v.lun == v->lun))
885 return entry;
886
887 return NULL;
888}
889
890/*
Juergen Grossd9d660f2014-08-28 06:44:12 +0200891 Add a new translation entry
892*/
893static int scsiback_add_translation_entry(struct vscsibk_info *info,
894 char *phy, struct ids_tuple *v)
895{
896 int err = 0;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200897 struct v2p_entry *new;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200898 unsigned long flags;
899 char *lunp;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200900 unsigned long long unpacked_lun;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700901 struct se_lun *se_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200902 struct scsiback_tpg *tpg_entry, *tpg = NULL;
903 char *error = "doesn't exist";
904
905 lunp = strrchr(phy, ':');
906 if (!lunp) {
Tao Chen78574872015-03-10 20:49:18 +0000907 pr_err("illegal format of physical device %s\n", phy);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200908 return -EINVAL;
909 }
910 *lunp = 0;
911 lunp++;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200912 err = kstrtoull(lunp, 10, &unpacked_lun);
913 if (err < 0) {
Tao Chen78574872015-03-10 20:49:18 +0000914 pr_err("lun number not valid: %s\n", lunp);
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200915 return err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200916 }
917
918 mutex_lock(&scsiback_mutex);
919 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
920 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
921 !strcmp(phy, tpg_entry->param_alias)) {
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700922 mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
923 hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
924 if (se_lun->unpacked_lun == unpacked_lun) {
925 if (!tpg_entry->tpg_nexus)
926 error = "nexus undefined";
927 else
928 tpg = tpg_entry;
929 break;
930 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200931 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700932 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200933 break;
934 }
935 }
936 if (tpg) {
937 mutex_lock(&tpg->tv_tpg_mutex);
938 tpg->tv_tpg_fe_count++;
939 mutex_unlock(&tpg->tv_tpg_mutex);
940 }
941 mutex_unlock(&scsiback_mutex);
942
943 if (!tpg) {
Nicholas Bellinger0fb1f142015-06-16 00:37:16 -0700944 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200945 return -ENODEV;
946 }
947
948 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
949 if (new == NULL) {
950 err = -ENOMEM;
951 goto out_free;
952 }
953
954 spin_lock_irqsave(&info->v2p_lock, flags);
955
956 /* Check double assignment to identical virtual ID */
Juergen Grossc9e2f532016-02-08 15:30:19 +0100957 if (scsiback_chk_translation_entry(info, v)) {
958 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
959 err = -EEXIST;
960 goto out;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200961 }
962
963 /* Create a new translation entry and add to the list */
964 kref_init(&new->kref);
965 new->v = *v;
966 new->tpg = tpg;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700967 new->lun = unpacked_lun;
Juergen Grossc9e2f532016-02-08 15:30:19 +0100968 list_add_tail(&new->l, &info->v2p_entry_lists);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200969
970out:
971 spin_unlock_irqrestore(&info->v2p_lock, flags);
972
973out_free:
Juergen Grossf285aa82016-02-08 15:30:18 +0100974 if (err) {
975 mutex_lock(&tpg->tv_tpg_mutex);
976 tpg->tv_tpg_fe_count--;
977 mutex_unlock(&tpg->tv_tpg_mutex);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200978 kfree(new);
Juergen Grossf285aa82016-02-08 15:30:18 +0100979 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200980
981 return err;
982}
983
984static void __scsiback_del_translation_entry(struct v2p_entry *entry)
985{
986 list_del(&entry->l);
987 kref_put(&entry->kref, scsiback_free_translation_entry);
988}
989
990/*
Juergen Grossc9e2f532016-02-08 15:30:19 +0100991 Delete the translation entry specified
Juergen Grossd9d660f2014-08-28 06:44:12 +0200992*/
993static int scsiback_del_translation_entry(struct vscsibk_info *info,
994 struct ids_tuple *v)
995{
996 struct v2p_entry *entry;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200997 unsigned long flags;
Juergen Grossc9e2f532016-02-08 15:30:19 +0100998 int ret = 0;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200999
1000 spin_lock_irqsave(&info->v2p_lock, flags);
1001 /* Find out the translation entry specified */
Juergen Grossc9e2f532016-02-08 15:30:19 +01001002 entry = scsiback_chk_translation_entry(info, v);
1003 if (entry)
1004 __scsiback_del_translation_entry(entry);
1005 else
1006 ret = -ENOENT;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001007
1008 spin_unlock_irqrestore(&info->v2p_lock, flags);
Juergen Grossc9e2f532016-02-08 15:30:19 +01001009 return ret;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001010}
1011
1012static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
Juergen Gross169e6cf2015-02-17 08:02:48 +01001013 char *phy, struct ids_tuple *vir, int try)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001014{
Juergen Grossc9e2f532016-02-08 15:30:19 +01001015 struct v2p_entry *entry;
1016 unsigned long flags;
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001017 int err;
Juergen Grossc9e2f532016-02-08 15:30:19 +01001018
1019 if (try) {
1020 spin_lock_irqsave(&info->v2p_lock, flags);
1021 entry = scsiback_chk_translation_entry(info, vir);
1022 spin_unlock_irqrestore(&info->v2p_lock, flags);
1023 if (entry)
1024 return;
1025 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001026 if (!scsiback_add_translation_entry(info, phy, vir)) {
1027 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1028 "%d", XenbusStateInitialised)) {
Tao Chen78574872015-03-10 20:49:18 +00001029 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001030 scsiback_del_translation_entry(info, vir);
1031 }
Juergen Gross169e6cf2015-02-17 08:02:48 +01001032 } else if (!try) {
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001033 err = xenbus_printf(XBT_NIL, info->dev->nodename, state,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001034 "%d", XenbusStateClosed);
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001035 if (err)
1036 xenbus_dev_error(info->dev, err,
1037 "%s: writing %s", __func__, state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001038 }
1039}
1040
1041static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1042 struct ids_tuple *vir)
1043{
1044 if (!scsiback_del_translation_entry(info, vir)) {
1045 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1046 "%d", XenbusStateClosed))
Tao Chen78574872015-03-10 20:49:18 +00001047 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001048 }
1049}
1050
1051#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1052#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1053
1054static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1055 char *ent)
1056{
1057 int err;
1058 struct ids_tuple vir;
1059 char *val;
1060 int device_state;
1061 char phy[VSCSI_NAMELEN];
1062 char str[64];
1063 char state[64];
1064 struct xenbus_device *dev = info->dev;
1065
1066 /* read status */
1067 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1068 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1069 if (XENBUS_EXIST_ERR(err))
1070 return;
1071
1072 /* physical SCSI device */
1073 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1074 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1075 if (IS_ERR(val)) {
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001076 err = xenbus_printf(XBT_NIL, dev->nodename, state,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001077 "%d", XenbusStateClosed);
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001078 if (err)
1079 xenbus_dev_error(info->dev, err,
1080 "%s: writing %s", __func__, state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001081 return;
1082 }
1083 strlcpy(phy, val, VSCSI_NAMELEN);
1084 kfree(val);
1085
1086 /* virtual SCSI device */
1087 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1088 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1089 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1090 if (XENBUS_EXIST_ERR(err)) {
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001091 err = xenbus_printf(XBT_NIL, dev->nodename, state,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001092 "%d", XenbusStateClosed);
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001093 if (err)
1094 xenbus_dev_error(info->dev, err,
1095 "%s: writing %s", __func__, state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001096 return;
1097 }
1098
1099 switch (op) {
1100 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
Juergen Gross169e6cf2015-02-17 08:02:48 +01001101 switch (device_state) {
1102 case XenbusStateInitialising:
1103 scsiback_do_add_lun(info, state, phy, &vir, 0);
1104 break;
1105 case XenbusStateConnected:
1106 scsiback_do_add_lun(info, state, phy, &vir, 1);
1107 break;
1108 case XenbusStateClosing:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001109 scsiback_do_del_lun(info, state, &vir);
Juergen Gross169e6cf2015-02-17 08:02:48 +01001110 break;
1111 default:
1112 break;
1113 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001114 break;
1115
1116 case VSCSIBACK_OP_UPDATEDEV_STATE:
1117 if (device_state == XenbusStateInitialised) {
1118 /* modify vscsi-devs/dev-x/state */
1119 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1120 "%d", XenbusStateConnected)) {
Tao Chen78574872015-03-10 20:49:18 +00001121 pr_err("xenbus_printf error %s\n", str);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001122 scsiback_del_translation_entry(info, &vir);
1123 xenbus_printf(XBT_NIL, dev->nodename, state,
1124 "%d", XenbusStateClosed);
1125 }
1126 }
1127 break;
Tao Chen78574872015-03-10 20:49:18 +00001128 /* When it is necessary, processing is added here. */
Juergen Grossd9d660f2014-08-28 06:44:12 +02001129 default:
1130 break;
1131 }
1132}
1133
1134static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1135{
1136 int i;
1137 char **dir;
1138 unsigned int ndir = 0;
1139
1140 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1141 &ndir);
1142 if (IS_ERR(dir))
1143 return;
1144
1145 for (i = 0; i < ndir; i++)
1146 scsiback_do_1lun_hotplug(info, op, dir[i]);
1147
1148 kfree(dir);
1149}
1150
1151static void scsiback_frontend_changed(struct xenbus_device *dev,
1152 enum xenbus_state frontend_state)
1153{
1154 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1155
1156 switch (frontend_state) {
1157 case XenbusStateInitialising:
1158 break;
1159
1160 case XenbusStateInitialised:
1161 if (scsiback_map(info))
1162 break;
1163
1164 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1165 xenbus_switch_state(dev, XenbusStateConnected);
1166 break;
1167
1168 case XenbusStateConnected:
1169 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1170
1171 if (dev->state == XenbusStateConnected)
1172 break;
1173
1174 xenbus_switch_state(dev, XenbusStateConnected);
1175 break;
1176
1177 case XenbusStateClosing:
1178 if (info->irq)
1179 scsiback_disconnect(info);
1180
1181 xenbus_switch_state(dev, XenbusStateClosing);
1182 break;
1183
1184 case XenbusStateClosed:
1185 xenbus_switch_state(dev, XenbusStateClosed);
1186 if (xenbus_dev_is_online(dev))
1187 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001188 fallthrough; /* if not online */
Juergen Grossd9d660f2014-08-28 06:44:12 +02001189 case XenbusStateUnknown:
1190 device_unregister(&dev->dev);
1191 break;
1192
1193 case XenbusStateReconfiguring:
1194 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1195 xenbus_switch_state(dev, XenbusStateReconfigured);
1196
1197 break;
1198
1199 default:
1200 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1201 frontend_state);
1202 break;
1203 }
1204}
1205
1206/*
1207 Release the translation entry specfied
1208*/
1209static void scsiback_release_translation_entry(struct vscsibk_info *info)
1210{
1211 struct v2p_entry *entry, *tmp;
1212 struct list_head *head = &(info->v2p_entry_lists);
1213 unsigned long flags;
1214
1215 spin_lock_irqsave(&info->v2p_lock, flags);
1216
1217 list_for_each_entry_safe(entry, tmp, head, l)
1218 __scsiback_del_translation_entry(entry);
1219
1220 spin_unlock_irqrestore(&info->v2p_lock, flags);
1221}
1222
1223static int scsiback_remove(struct xenbus_device *dev)
1224{
1225 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1226
1227 if (info->irq)
1228 scsiback_disconnect(info);
1229
1230 scsiback_release_translation_entry(info);
1231
1232 dev_set_drvdata(&dev->dev, NULL);
1233
1234 return 0;
1235}
1236
1237static int scsiback_probe(struct xenbus_device *dev,
1238 const struct xenbus_device_id *id)
1239{
1240 int err;
1241
1242 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1243 GFP_KERNEL);
1244
Tao Chen78574872015-03-10 20:49:18 +00001245 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001246
1247 if (!info) {
1248 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1249 return -ENOMEM;
1250 }
1251 info->dev = dev;
1252 dev_set_drvdata(&dev->dev, info);
1253
1254 info->domid = dev->otherend_id;
1255 spin_lock_init(&info->ring_lock);
1256 info->ring_error = 0;
1257 atomic_set(&info->nr_unreplied_reqs, 0);
1258 init_waitqueue_head(&info->waiting_to_free);
1259 info->dev = dev;
1260 info->irq = 0;
1261 INIT_LIST_HEAD(&info->v2p_entry_lists);
1262 spin_lock_init(&info->v2p_lock);
1263
1264 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1265 SG_ALL);
1266 if (err)
1267 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1268
1269 err = xenbus_switch_state(dev, XenbusStateInitWait);
1270 if (err)
1271 goto fail;
1272
1273 return 0;
1274
1275fail:
Tao Chen78574872015-03-10 20:49:18 +00001276 pr_warn("%s failed\n", __func__);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001277 scsiback_remove(dev);
1278
1279 return err;
1280}
1281
1282static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1283{
1284 switch (tport->tport_proto_id) {
1285 case SCSI_PROTOCOL_SAS:
1286 return "SAS";
1287 case SCSI_PROTOCOL_FCP:
1288 return "FCP";
1289 case SCSI_PROTOCOL_ISCSI:
1290 return "iSCSI";
1291 default:
1292 break;
1293 }
1294
1295 return "Unknown";
1296}
1297
Juergen Grossd9d660f2014-08-28 06:44:12 +02001298static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1299{
1300 struct scsiback_tpg *tpg = container_of(se_tpg,
1301 struct scsiback_tpg, se_tpg);
1302 struct scsiback_tport *tport = tpg->tport;
1303
1304 return &tport->tport_name[0];
1305}
1306
1307static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1308{
1309 struct scsiback_tpg *tpg = container_of(se_tpg,
1310 struct scsiback_tpg, se_tpg);
1311 return tpg->tport_tpgt;
1312}
1313
Juergen Grossd9d660f2014-08-28 06:44:12 +02001314static struct se_wwn *
1315scsiback_make_tport(struct target_fabric_configfs *tf,
1316 struct config_group *group,
1317 const char *name)
1318{
1319 struct scsiback_tport *tport;
1320 char *ptr;
1321 u64 wwpn = 0;
1322 int off = 0;
1323
1324 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1325 if (!tport)
1326 return ERR_PTR(-ENOMEM);
1327
1328 tport->tport_wwpn = wwpn;
1329 /*
1330 * Determine the emulated Protocol Identifier and Target Port Name
1331 * based on the incoming configfs directory name.
1332 */
1333 ptr = strstr(name, "naa.");
1334 if (ptr) {
1335 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1336 goto check_len;
1337 }
1338 ptr = strstr(name, "fc.");
1339 if (ptr) {
1340 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1341 off = 3; /* Skip over "fc." */
1342 goto check_len;
1343 }
1344 ptr = strstr(name, "iqn.");
1345 if (ptr) {
1346 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1347 goto check_len;
1348 }
1349
1350 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1351 kfree(tport);
1352 return ERR_PTR(-EINVAL);
1353
1354check_len:
1355 if (strlen(name) >= VSCSI_NAMELEN) {
1356 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1357 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1358 kfree(tport);
1359 return ERR_PTR(-EINVAL);
1360 }
1361 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1362
Tao Chen78574872015-03-10 20:49:18 +00001363 pr_debug("Allocated emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001364 scsiback_dump_proto_id(tport), name);
1365
1366 return &tport->tport_wwn;
1367}
1368
1369static void scsiback_drop_tport(struct se_wwn *wwn)
1370{
1371 struct scsiback_tport *tport = container_of(wwn,
1372 struct scsiback_tport, tport_wwn);
1373
Tao Chen78574872015-03-10 20:49:18 +00001374 pr_debug("Deallocating emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001375 scsiback_dump_proto_id(tport), tport->tport_name);
1376
1377 kfree(tport);
1378}
1379
Juergen Grossd9d660f2014-08-28 06:44:12 +02001380static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1381{
1382 return 1;
1383}
1384
1385static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1386{
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -08001387 return transport_generic_free_cmd(se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001388}
1389
1390static void scsiback_release_cmd(struct se_cmd *se_cmd)
1391{
Matthew Wilcox83c2b542018-06-12 12:05:43 -07001392 target_free_tag(se_cmd->se_sess, se_cmd);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001393}
1394
Juergen Grossd9d660f2014-08-28 06:44:12 +02001395static u32 scsiback_sess_get_index(struct se_session *se_sess)
1396{
1397 return 0;
1398}
1399
1400static int scsiback_write_pending(struct se_cmd *se_cmd)
1401{
1402 /* Go ahead and process the write immediately */
1403 target_execute_cmd(se_cmd);
1404
1405 return 0;
1406}
1407
Juergen Grossd9d660f2014-08-28 06:44:12 +02001408static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1409{
1410}
1411
Juergen Grossd9d660f2014-08-28 06:44:12 +02001412static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1413{
1414 return 0;
1415}
1416
1417static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1418{
1419 struct vscsibk_pend *pending_req = container_of(se_cmd,
1420 struct vscsibk_pend, se_cmd);
1421
1422 pending_req->result = SAM_STAT_GOOD;
1423 scsiback_cmd_done(pending_req);
1424 return 0;
1425}
1426
1427static int scsiback_queue_status(struct se_cmd *se_cmd)
1428{
1429 struct vscsibk_pend *pending_req = container_of(se_cmd,
1430 struct vscsibk_pend, se_cmd);
1431
1432 if (se_cmd->sense_buffer &&
1433 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1434 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1435 pending_req->result = (DRIVER_SENSE << 24) |
1436 SAM_STAT_CHECK_CONDITION;
1437 else
1438 pending_req->result = se_cmd->scsi_status;
1439
1440 scsiback_cmd_done(pending_req);
1441 return 0;
1442}
1443
1444static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1445{
Bart Van Assche9f4ab182017-05-23 16:48:36 -07001446 struct vscsibk_pend *pending_req = container_of(se_cmd,
1447 struct vscsibk_pend, se_cmd);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001448
Bart Van Asschee3eac122017-05-23 16:48:37 -07001449 complete(&pending_req->tmr_done);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001450}
1451
1452static void scsiback_aborted_task(struct se_cmd *se_cmd)
1453{
1454}
1455
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001456static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001457 char *page)
1458{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001459 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001460 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1461 se_tpg);
1462 ssize_t rb;
1463
1464 mutex_lock(&tpg->tv_tpg_mutex);
1465 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1466 mutex_unlock(&tpg->tv_tpg_mutex);
1467
1468 return rb;
1469}
1470
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001471static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001472 const char *page, size_t count)
1473{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001474 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001475 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1476 se_tpg);
1477 int len;
1478
1479 if (strlen(page) >= VSCSI_NAMELEN) {
1480 pr_err("param alias: %s, exceeds max: %d\n", page,
1481 VSCSI_NAMELEN);
1482 return -EINVAL;
1483 }
1484
1485 mutex_lock(&tpg->tv_tpg_mutex);
1486 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1487 if (tpg->param_alias[len - 1] == '\n')
1488 tpg->param_alias[len - 1] = '\0';
1489 mutex_unlock(&tpg->tv_tpg_mutex);
1490
1491 return count;
1492}
1493
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001494CONFIGFS_ATTR(scsiback_tpg_param_, alias);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001495
1496static struct configfs_attribute *scsiback_param_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001497 &scsiback_tpg_param_attr_alias,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001498 NULL,
1499};
1500
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001501static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg,
1502 struct se_session *se_sess, void *p)
1503{
1504 struct scsiback_tpg *tpg = container_of(se_tpg,
1505 struct scsiback_tpg, se_tpg);
1506
1507 tpg->tpg_nexus = p;
1508 return 0;
1509}
1510
Juergen Grossd9d660f2014-08-28 06:44:12 +02001511static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1512 const char *name)
1513{
Juergen Grossd9d660f2014-08-28 06:44:12 +02001514 struct scsiback_nexus *tv_nexus;
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001515 int ret = 0;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001516
1517 mutex_lock(&tpg->tv_tpg_mutex);
1518 if (tpg->tpg_nexus) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001519 pr_debug("tpg->tpg_nexus already exists\n");
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001520 ret = -EEXIST;
1521 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001522 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001523
1524 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1525 if (!tv_nexus) {
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001526 ret = -ENOMEM;
1527 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001528 }
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001529
Mike Christiefa834282018-08-02 12:12:23 -05001530 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg,
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001531 VSCSI_DEFAULT_SESSION_TAGS,
1532 sizeof(struct vscsibk_pend),
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001533 TARGET_PROT_NORMAL, name,
1534 tv_nexus, scsiback_alloc_sess_cb);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001535 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001536 kfree(tv_nexus);
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001537 ret = -ENOMEM;
1538 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001539 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001540
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001541out_unlock:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001542 mutex_unlock(&tpg->tv_tpg_mutex);
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001543 return ret;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001544}
1545
1546static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1547{
1548 struct se_session *se_sess;
1549 struct scsiback_nexus *tv_nexus;
1550
1551 mutex_lock(&tpg->tv_tpg_mutex);
1552 tv_nexus = tpg->tpg_nexus;
1553 if (!tv_nexus) {
1554 mutex_unlock(&tpg->tv_tpg_mutex);
1555 return -ENODEV;
1556 }
1557
1558 se_sess = tv_nexus->tvn_se_sess;
1559 if (!se_sess) {
1560 mutex_unlock(&tpg->tv_tpg_mutex);
1561 return -ENODEV;
1562 }
1563
1564 if (tpg->tv_tpg_port_count != 0) {
1565 mutex_unlock(&tpg->tv_tpg_mutex);
1566 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1567 tpg->tv_tpg_port_count);
1568 return -EBUSY;
1569 }
1570
1571 if (tpg->tv_tpg_fe_count != 0) {
1572 mutex_unlock(&tpg->tv_tpg_mutex);
1573 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1574 tpg->tv_tpg_fe_count);
1575 return -EBUSY;
1576 }
1577
Tao Chen78574872015-03-10 20:49:18 +00001578 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001579 scsiback_dump_proto_id(tpg->tport),
1580 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1581
1582 /*
1583 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1584 */
Mike Christie25b88552018-08-02 12:12:27 -05001585 target_remove_session(se_sess);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001586 tpg->tpg_nexus = NULL;
1587 mutex_unlock(&tpg->tv_tpg_mutex);
1588
1589 kfree(tv_nexus);
1590 return 0;
1591}
1592
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001593static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001594{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001595 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001596 struct scsiback_tpg *tpg = container_of(se_tpg,
1597 struct scsiback_tpg, se_tpg);
1598 struct scsiback_nexus *tv_nexus;
1599 ssize_t ret;
1600
1601 mutex_lock(&tpg->tv_tpg_mutex);
1602 tv_nexus = tpg->tpg_nexus;
1603 if (!tv_nexus) {
1604 mutex_unlock(&tpg->tv_tpg_mutex);
1605 return -ENODEV;
1606 }
1607 ret = snprintf(page, PAGE_SIZE, "%s\n",
1608 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1609 mutex_unlock(&tpg->tv_tpg_mutex);
1610
1611 return ret;
1612}
1613
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001614static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1615 const char *page, size_t count)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001616{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001617 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001618 struct scsiback_tpg *tpg = container_of(se_tpg,
1619 struct scsiback_tpg, se_tpg);
1620 struct scsiback_tport *tport_wwn = tpg->tport;
1621 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1622 int ret;
1623 /*
Tao Chen78574872015-03-10 20:49:18 +00001624 * Shutdown the active I_T nexus if 'NULL' is passed.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001625 */
1626 if (!strncmp(page, "NULL", 4)) {
1627 ret = scsiback_drop_nexus(tpg);
1628 return (!ret) ? count : ret;
1629 }
1630 /*
1631 * Otherwise make sure the passed virtual Initiator port WWN matches
1632 * the fabric protocol_id set in scsiback_make_tport(), and call
1633 * scsiback_make_nexus().
1634 */
1635 if (strlen(page) >= VSCSI_NAMELEN) {
1636 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1637 page, VSCSI_NAMELEN);
1638 return -EINVAL;
1639 }
1640 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1641
1642 ptr = strstr(i_port, "naa.");
1643 if (ptr) {
1644 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1645 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1646 i_port, scsiback_dump_proto_id(tport_wwn));
1647 return -EINVAL;
1648 }
1649 port_ptr = &i_port[0];
1650 goto check_newline;
1651 }
1652 ptr = strstr(i_port, "fc.");
1653 if (ptr) {
1654 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1655 pr_err("Passed FCP 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[3]; /* Skip over "fc." */
1660 goto check_newline;
1661 }
1662 ptr = strstr(i_port, "iqn.");
1663 if (ptr) {
1664 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1665 pr_err("Passed iSCSI 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[0];
1670 goto check_newline;
1671 }
1672 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1673 i_port);
1674 return -EINVAL;
1675 /*
1676 * Clear any trailing newline for the NAA WWN
1677 */
1678check_newline:
1679 if (i_port[strlen(i_port) - 1] == '\n')
1680 i_port[strlen(i_port) - 1] = '\0';
1681
1682 ret = scsiback_make_nexus(tpg, port_ptr);
1683 if (ret < 0)
1684 return ret;
1685
1686 return count;
1687}
1688
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001689CONFIGFS_ATTR(scsiback_tpg_, nexus);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001690
1691static struct configfs_attribute *scsiback_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001692 &scsiback_tpg_attr_nexus,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001693 NULL,
1694};
1695
1696static ssize_t
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001697scsiback_wwn_version_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001698{
1699 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1700 UTS_RELEASE"\n",
1701 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1702}
1703
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001704CONFIGFS_ATTR_RO(scsiback_wwn_, version);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001705
1706static struct configfs_attribute *scsiback_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001707 &scsiback_wwn_attr_version,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001708 NULL,
1709};
1710
Juergen Grossd9d660f2014-08-28 06:44:12 +02001711static int scsiback_port_link(struct se_portal_group *se_tpg,
1712 struct se_lun *lun)
1713{
1714 struct scsiback_tpg *tpg = container_of(se_tpg,
1715 struct scsiback_tpg, se_tpg);
1716
1717 mutex_lock(&tpg->tv_tpg_mutex);
1718 tpg->tv_tpg_port_count++;
1719 mutex_unlock(&tpg->tv_tpg_mutex);
1720
1721 return 0;
1722}
1723
1724static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1725 struct se_lun *lun)
1726{
1727 struct scsiback_tpg *tpg = container_of(se_tpg,
1728 struct scsiback_tpg, se_tpg);
1729
1730 mutex_lock(&tpg->tv_tpg_mutex);
1731 tpg->tv_tpg_port_count--;
1732 mutex_unlock(&tpg->tv_tpg_mutex);
1733}
1734
1735static struct se_portal_group *
Bart Van Asscheaa090ea2018-06-22 14:53:02 -07001736scsiback_make_tpg(struct se_wwn *wwn, const char *name)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001737{
1738 struct scsiback_tport *tport = container_of(wwn,
1739 struct scsiback_tport, tport_wwn);
1740
1741 struct scsiback_tpg *tpg;
Dan Carpenter495daef2014-09-08 14:17:35 +03001742 u16 tpgt;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001743 int ret;
1744
1745 if (strstr(name, "tpgt_") != name)
1746 return ERR_PTR(-EINVAL);
Dan Carpenter495daef2014-09-08 14:17:35 +03001747 ret = kstrtou16(name + 5, 10, &tpgt);
1748 if (ret)
1749 return ERR_PTR(ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001750
1751 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1752 if (!tpg)
1753 return ERR_PTR(-ENOMEM);
1754
1755 mutex_init(&tpg->tv_tpg_mutex);
1756 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1757 INIT_LIST_HEAD(&tpg->info_list);
1758 tpg->tport = tport;
1759 tpg->tport_tpgt = tpgt;
1760
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07001761 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001762 if (ret < 0) {
1763 kfree(tpg);
1764 return NULL;
1765 }
1766 mutex_lock(&scsiback_mutex);
1767 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1768 mutex_unlock(&scsiback_mutex);
1769
1770 return &tpg->se_tpg;
1771}
1772
1773static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1774{
1775 struct scsiback_tpg *tpg = container_of(se_tpg,
1776 struct scsiback_tpg, se_tpg);
1777
1778 mutex_lock(&scsiback_mutex);
1779 list_del(&tpg->tv_tpg_list);
1780 mutex_unlock(&scsiback_mutex);
1781 /*
1782 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1783 */
1784 scsiback_drop_nexus(tpg);
1785 /*
Tao Chen78574872015-03-10 20:49:18 +00001786 * Deregister the se_tpg from TCM.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001787 */
1788 core_tpg_deregister(se_tpg);
1789 kfree(tpg);
1790}
1791
1792static int scsiback_check_true(struct se_portal_group *se_tpg)
1793{
1794 return 1;
1795}
1796
1797static int scsiback_check_false(struct se_portal_group *se_tpg)
1798{
1799 return 0;
1800}
1801
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001802static const struct target_core_fabric_ops scsiback_ops = {
1803 .module = THIS_MODULE,
David Disseldorp30c7ca92018-11-23 18:36:12 +01001804 .fabric_name = "xen-pvscsi",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001805 .tpg_get_wwn = scsiback_get_fabric_wwn,
1806 .tpg_get_tag = scsiback_get_tag,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001807 .tpg_check_demo_mode = scsiback_check_true,
1808 .tpg_check_demo_mode_cache = scsiback_check_true,
1809 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1810 .tpg_check_prod_mode_write_protect = scsiback_check_false,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001811 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1812 .check_stop_free = scsiback_check_stop_free,
1813 .release_cmd = scsiback_release_cmd,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001814 .sess_get_index = scsiback_sess_get_index,
1815 .sess_get_initiator_sid = NULL,
1816 .write_pending = scsiback_write_pending,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001817 .set_default_node_attributes = scsiback_set_default_node_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001818 .get_cmd_state = scsiback_get_cmd_state,
1819 .queue_data_in = scsiback_queue_data_in,
1820 .queue_status = scsiback_queue_status,
1821 .queue_tm_rsp = scsiback_queue_tm_rsp,
1822 .aborted_task = scsiback_aborted_task,
1823 /*
1824 * Setup callers for generic logic in target_core_fabric_configfs.c
1825 */
1826 .fabric_make_wwn = scsiback_make_tport,
1827 .fabric_drop_wwn = scsiback_drop_tport,
1828 .fabric_make_tpg = scsiback_make_tpg,
1829 .fabric_drop_tpg = scsiback_drop_tpg,
1830 .fabric_post_link = scsiback_port_link,
1831 .fabric_pre_unlink = scsiback_port_unlink,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001832
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001833 .tfc_wwn_attrs = scsiback_wwn_attrs,
1834 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1835 .tfc_tpg_param_attrs = scsiback_param_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001836};
1837
1838static const struct xenbus_device_id scsiback_ids[] = {
1839 { "vscsi" },
1840 { "" }
1841};
1842
David Vrabel95afae42014-09-08 17:30:41 +01001843static struct xenbus_driver scsiback_driver = {
1844 .ids = scsiback_ids,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001845 .probe = scsiback_probe,
1846 .remove = scsiback_remove,
1847 .otherend_changed = scsiback_frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001848};
Juergen Grossd9d660f2014-08-28 06:44:12 +02001849
Juergen Grossd9d660f2014-08-28 06:44:12 +02001850static int __init scsiback_init(void)
1851{
1852 int ret;
1853
1854 if (!xen_domain())
1855 return -ENODEV;
1856
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001857 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1858 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1859
Juergen Grossd9d660f2014-08-28 06:44:12 +02001860 ret = xenbus_register_backend(&scsiback_driver);
1861 if (ret)
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001862 goto out;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001863
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001864 ret = target_register_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001865 if (ret)
1866 goto out_unregister_xenbus;
1867
1868 return 0;
1869
1870out_unregister_xenbus:
1871 xenbus_unregister_driver(&scsiback_driver);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001872out:
Tao Chen78574872015-03-10 20:49:18 +00001873 pr_err("%s: error %d\n", __func__, ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001874 return ret;
1875}
1876
1877static void __exit scsiback_exit(void)
1878{
1879 struct page *page;
1880
1881 while (free_pages_num) {
1882 if (get_free_page(&page))
1883 BUG();
David Vrabelff4b1562015-01-08 18:06:01 +00001884 gnttab_free_pages(1, &page);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001885 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001886 target_unregister_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001887 xenbus_unregister_driver(&scsiback_driver);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001888}
1889
1890module_init(scsiback_init);
1891module_exit(scsiback_exit);
1892
1893MODULE_DESCRIPTION("Xen SCSI backend driver");
1894MODULE_LICENSE("Dual BSD/GPL");
1895MODULE_ALIAS("xen-backend:vscsi");
1896MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");