blob: 18b9549f6fa3c0d2cdcb13815e92d9d3978770b4 [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;
Juergen Grossd9d660f2014-08-28 06:44:12 +020094
95 spinlock_t ring_lock;
96 atomic_t nr_unreplied_reqs;
97
98 spinlock_t v2p_lock;
99 struct list_head v2p_entry_lists;
100
101 wait_queue_head_t waiting_to_free;
Juergen Grossca334792020-12-07 08:31:22 +0100102
103 struct gnttab_page_cache free_pages;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200104};
105
106/* theoretical maximum of grants for one request */
107#define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
108
109/*
110 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
111 * call to map/unmap grants. Don't choose it too large, as there are arrays
112 * with VSCSI_GRANT_BATCH elements allocated on the stack.
113 */
114#define VSCSI_GRANT_BATCH 16
115
116struct vscsibk_pend {
117 uint16_t rqid;
118
119 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
120 uint8_t cmd_len;
121
122 uint8_t sc_data_direction;
123 uint16_t n_sg; /* real length of SG list */
124 uint16_t n_grants; /* SG pages and potentially SG list */
125 uint32_t data_len;
126 uint32_t result;
127
128 struct vscsibk_info *info;
129 struct v2p_entry *v2p;
130 struct scatterlist *sgl;
131
132 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
133
134 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
135 struct page *pages[VSCSI_MAX_GRANTS];
136
137 struct se_cmd se_cmd;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200138
Bart Van Asschee3eac122017-05-23 16:48:37 -0700139 struct completion tmr_done;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200140};
141
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800142#define VSCSI_DEFAULT_SESSION_TAGS 128
143
Juergen Grossd9d660f2014-08-28 06:44:12 +0200144struct scsiback_nexus {
145 /* Pointer to TCM session for I_T Nexus */
146 struct se_session *tvn_se_sess;
147};
148
149struct scsiback_tport {
150 /* SCSI protocol the tport is providing */
151 u8 tport_proto_id;
152 /* Binary World Wide unique Port Name for pvscsi Target port */
153 u64 tport_wwpn;
154 /* ASCII formatted WWPN for pvscsi Target port */
155 char tport_name[VSCSI_NAMELEN];
156 /* Returned by scsiback_make_tport() */
157 struct se_wwn tport_wwn;
158};
159
160struct scsiback_tpg {
161 /* scsiback port target portal group tag for TCM */
162 u16 tport_tpgt;
163 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
164 int tv_tpg_port_count;
165 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
166 int tv_tpg_fe_count;
167 /* list for scsiback_list */
168 struct list_head tv_tpg_list;
169 /* Used to protect access for tpg_nexus */
170 struct mutex tv_tpg_mutex;
171 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
172 struct scsiback_nexus *tpg_nexus;
173 /* Pointer back to scsiback_tport */
174 struct scsiback_tport *tport;
175 /* Returned by scsiback_make_tpg() */
176 struct se_portal_group se_tpg;
177 /* alias used in xenstore */
178 char param_alias[VSCSI_NAMELEN];
179 /* list of info structures related to this target portal group */
180 struct list_head info_list;
181};
182
183#define SCSIBACK_INVALID_HANDLE (~0)
184
185static bool log_print_stat;
186module_param(log_print_stat, bool, 0644);
187
188static int scsiback_max_buffer_pages = 1024;
189module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
190MODULE_PARM_DESC(max_buffer_pages,
191"Maximum number of free pages to keep in backend buffer");
192
Juergen Grossd9d660f2014-08-28 06:44:12 +0200193/* Global spinlock to protect scsiback TPG list */
194static DEFINE_MUTEX(scsiback_mutex);
195static LIST_HEAD(scsiback_list);
196
Juergen Grossd9d660f2014-08-28 06:44:12 +0200197static void scsiback_get(struct vscsibk_info *info)
198{
199 atomic_inc(&info->nr_unreplied_reqs);
200}
201
202static void scsiback_put(struct vscsibk_info *info)
203{
204 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
205 wake_up(&info->waiting_to_free);
206}
207
Juergen Grossd9d660f2014-08-28 06:44:12 +0200208static unsigned long vaddr_page(struct page *page)
209{
210 unsigned long pfn = page_to_pfn(page);
211
212 return (unsigned long)pfn_to_kaddr(pfn);
213}
214
215static unsigned long vaddr(struct vscsibk_pend *req, int seg)
216{
217 return vaddr_page(req->pages[seg]);
218}
219
220static void scsiback_print_status(char *sense_buffer, int errors,
221 struct vscsibk_pend *pending_req)
222{
223 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
224
Tao Chen78574872015-03-10 20:49:18 +0000225 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200226 tpg->tport->tport_name, pending_req->v2p->lun,
227 pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
228 host_byte(errors), driver_byte(errors));
Juergen Grossd9d660f2014-08-28 06:44:12 +0200229}
230
231static void scsiback_fast_flush_area(struct vscsibk_pend *req)
232{
233 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
234 struct page *pages[VSCSI_GRANT_BATCH];
235 unsigned int i, invcount = 0;
236 grant_handle_t handle;
237 int err;
238
239 kfree(req->sgl);
240 req->sgl = NULL;
241 req->n_sg = 0;
242
243 if (!req->n_grants)
244 return;
245
246 for (i = 0; i < req->n_grants; i++) {
247 handle = req->grant_handles[i];
248 if (handle == SCSIBACK_INVALID_HANDLE)
249 continue;
250 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
251 GNTMAP_host_map, handle);
252 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
253 pages[invcount] = req->pages[i];
254 put_page(pages[invcount]);
255 invcount++;
256 if (invcount < VSCSI_GRANT_BATCH)
257 continue;
258 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
259 BUG_ON(err);
260 invcount = 0;
261 }
262
263 if (invcount) {
264 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
265 BUG_ON(err);
266 }
267
Juergen Grossca334792020-12-07 08:31:22 +0100268 gnttab_page_cache_put(&req->info->free_pages, req->pages,
269 req->n_grants);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200270 req->n_grants = 0;
271}
272
273static void scsiback_free_translation_entry(struct kref *kref)
274{
275 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
276 struct scsiback_tpg *tpg = entry->tpg;
277
278 mutex_lock(&tpg->tv_tpg_mutex);
279 tpg->tv_tpg_fe_count--;
280 mutex_unlock(&tpg->tv_tpg_mutex);
281
282 kfree(entry);
283}
284
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800285static void scsiback_send_response(struct vscsibk_info *info,
286 char *sense_buffer, int32_t result, uint32_t resid,
287 uint16_t rqid)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200288{
289 struct vscsiif_response *ring_res;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200290 int notify;
291 struct scsi_sense_hdr sshdr;
292 unsigned long flags;
293 unsigned len;
294
295 spin_lock_irqsave(&info->ring_lock, flags);
296
297 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
298 info->ring.rsp_prod_pvt++;
299
300 ring_res->rslt = result;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800301 ring_res->rqid = rqid;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200302
303 if (sense_buffer != NULL &&
304 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
305 &sshdr)) {
306 len = min_t(unsigned, 8 + sense_buffer[7],
307 VSCSIIF_SENSE_BUFFERSIZE);
308 memcpy(ring_res->sense_buffer, sense_buffer, len);
309 ring_res->sense_len = len;
310 } else {
311 ring_res->sense_len = 0;
312 }
313
314 ring_res->residual_len = resid;
315
316 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
317 spin_unlock_irqrestore(&info->ring_lock, flags);
318
319 if (notify)
320 notify_remote_via_irq(info->irq);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800321}
322
323static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
324 uint32_t resid, struct vscsibk_pend *pending_req)
325{
326 scsiback_send_response(pending_req->info, sense_buffer, result,
327 resid, pending_req->rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200328
329 if (pending_req->v2p)
330 kref_put(&pending_req->v2p->kref,
331 scsiback_free_translation_entry);
332}
333
334static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
335{
336 struct vscsibk_info *info = pending_req->info;
337 unsigned char *sense_buffer;
338 unsigned int resid;
339 int errors;
340
341 sense_buffer = pending_req->sense_buffer;
342 resid = pending_req->se_cmd.residual_count;
343 errors = pending_req->result;
344
345 if (errors && log_print_stat)
346 scsiback_print_status(sense_buffer, errors, pending_req);
347
348 scsiback_fast_flush_area(pending_req);
349 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
350 scsiback_put(info);
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800351 /*
352 * Drop the extra KREF_ACK reference taken by target_submit_cmd_map_sgls()
353 * ahead of scsiback_check_stop_free() -> transport_generic_free_cmd()
354 * final se_cmd->cmd_kref put.
355 */
356 target_put_sess_cmd(&pending_req->se_cmd);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200357}
358
359static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
360{
361 struct se_cmd *se_cmd = &pending_req->se_cmd;
362 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200363
Juergen Grossd9d660f2014-08-28 06:44:12 +0200364 scsiback_get(pending_req->info);
Bart Van Assche649ee052015-04-14 13:26:44 +0200365 se_cmd->tag = pending_req->rqid;
Mike Christie1f48b062021-02-27 10:59:53 -0600366 target_init_cmd(se_cmd, sess, pending_req->sense_buffer,
367 pending_req->v2p->lun, pending_req->data_len, 0,
368 pending_req->sc_data_direction, TARGET_SCF_ACK_KREF);
369
370 if (target_submit_prep(se_cmd, pending_req->cmnd, pending_req->sgl,
371 pending_req->n_sg, NULL, 0, NULL, 0))
372 return;
373
374 target_submit(se_cmd);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200375}
376
377static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
378 struct page **pg, grant_handle_t *grant, int cnt)
379{
380 int err, i;
381
382 if (!cnt)
383 return 0;
384
385 err = gnttab_map_refs(map, NULL, pg, cnt);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200386 for (i = 0; i < cnt; i++) {
387 if (unlikely(map[i].status != GNTST_okay)) {
Tao Chen78574872015-03-10 20:49:18 +0000388 pr_err("invalid buffer -- could not remap it\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200389 map[i].handle = SCSIBACK_INVALID_HANDLE;
Jan Beulich7c774742021-02-15 08:55:57 +0100390 if (!err)
391 err = -ENOMEM;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200392 } else {
393 get_page(pg[i]);
394 }
395 grant[i] = map[i].handle;
396 }
397 return err;
398}
399
400static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
401 struct scsiif_request_segment *seg, struct page **pg,
402 grant_handle_t *grant, int cnt, u32 flags)
403{
404 int mapcount = 0, i, err = 0;
405 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
406 struct vscsibk_info *info = pending_req->info;
407
408 for (i = 0; i < cnt; i++) {
Juergen Grossca334792020-12-07 08:31:22 +0100409 if (gnttab_page_cache_get(&info->free_pages, pg + mapcount)) {
410 gnttab_page_cache_put(&info->free_pages, pg, mapcount);
Tao Chen78574872015-03-10 20:49:18 +0000411 pr_err("no grant page\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200412 return -ENOMEM;
413 }
414 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
415 flags, seg[i].gref, info->domid);
416 mapcount++;
417 if (mapcount < VSCSI_GRANT_BATCH)
418 continue;
419 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
420 pg += mapcount;
421 grant += mapcount;
422 pending_req->n_grants += mapcount;
423 if (err)
424 return err;
425 mapcount = 0;
426 }
427 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
428 pending_req->n_grants += mapcount;
429 return err;
430}
431
432static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
433 struct vscsibk_pend *pending_req)
434{
435 u32 flags;
436 int i, err, n_segs, i_seg = 0;
437 struct page **pg;
438 struct scsiif_request_segment *seg;
439 unsigned long end_seg = 0;
440 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
441 unsigned int nr_sgl = 0;
442 struct scatterlist *sg;
443 grant_handle_t *grant;
444
445 pending_req->n_sg = 0;
446 pending_req->n_grants = 0;
447 pending_req->data_len = 0;
448
449 nr_segments &= ~VSCSIIF_SG_GRANT;
450 if (!nr_segments)
451 return 0;
452
453 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
Tao Chen78574872015-03-10 20:49:18 +0000454 pr_debug("invalid parameter nr_seg = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200455 ring_req->nr_segments);
456 return -EINVAL;
457 }
458
459 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
460 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
461 pending_req->pages, pending_req->grant_handles,
462 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
463 if (err)
464 return err;
465 nr_sgl = nr_segments;
466 nr_segments = 0;
467 for (i = 0; i < nr_sgl; i++) {
468 n_segs = ring_req->seg[i].length /
469 sizeof(struct scsiif_request_segment);
470 if ((unsigned)ring_req->seg[i].offset +
471 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
472 n_segs * sizeof(struct scsiif_request_segment) !=
473 ring_req->seg[i].length)
474 return -EINVAL;
475 nr_segments += n_segs;
476 }
477 if (nr_segments > SG_ALL) {
Tao Chen78574872015-03-10 20:49:18 +0000478 pr_debug("invalid nr_seg = %d\n", nr_segments);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200479 return -EINVAL;
480 }
481 }
482
Tao Chen78574872015-03-10 20:49:18 +0000483 /* free of (sgl) in fast_flush_area() */
Juergen Grossd9d660f2014-08-28 06:44:12 +0200484 pending_req->sgl = kmalloc_array(nr_segments,
485 sizeof(struct scatterlist), GFP_KERNEL);
486 if (!pending_req->sgl)
487 return -ENOMEM;
488
489 sg_init_table(pending_req->sgl, nr_segments);
490 pending_req->n_sg = nr_segments;
491
492 flags = GNTMAP_host_map;
493 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
494 flags |= GNTMAP_readonly;
495
496 pg = pending_req->pages + nr_sgl;
497 grant = pending_req->grant_handles + nr_sgl;
498 if (!nr_sgl) {
499 seg = ring_req->seg;
500 err = scsiback_gnttab_data_map_list(pending_req, seg,
501 pg, grant, nr_segments, flags);
502 if (err)
503 return err;
504 } else {
505 for (i = 0; i < nr_sgl; i++) {
506 seg = (struct scsiif_request_segment *)(
507 vaddr(pending_req, i) + ring_req->seg[i].offset);
508 n_segs = ring_req->seg[i].length /
509 sizeof(struct scsiif_request_segment);
510 err = scsiback_gnttab_data_map_list(pending_req, seg,
511 pg, grant, n_segs, flags);
512 if (err)
513 return err;
514 pg += n_segs;
515 grant += n_segs;
516 }
517 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
518 seg = (struct scsiif_request_segment *)end_seg;
519 end_seg += ring_req->seg[0].length;
520 pg = pending_req->pages + nr_sgl;
521 }
522
523 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
524 sg_set_page(sg, pg[i], seg->length, seg->offset);
525 pending_req->data_len += seg->length;
526 seg++;
527 if (nr_sgl && (unsigned long)seg >= end_seg) {
528 i_seg++;
529 end_seg = vaddr(pending_req, i_seg) +
530 ring_req->seg[i_seg].offset;
531 seg = (struct scsiif_request_segment *)end_seg;
532 end_seg += ring_req->seg[i_seg].length;
533 }
534 if (sg->offset >= PAGE_SIZE ||
535 sg->length > PAGE_SIZE ||
536 sg->offset + sg->length > PAGE_SIZE)
537 return -EINVAL;
538 }
539
540 return 0;
541}
542
543static void scsiback_disconnect(struct vscsibk_info *info)
544{
545 wait_event(info->waiting_to_free,
546 atomic_read(&info->nr_unreplied_reqs) == 0);
547
548 unbind_from_irqhandler(info->irq, info);
549 info->irq = 0;
550 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
551}
552
553static void scsiback_device_action(struct vscsibk_pend *pending_req,
554 enum tcm_tmreq_table act, int tag)
555{
Juergen Grossd9d660f2014-08-28 06:44:12 +0200556 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800557 struct scsiback_nexus *nexus = tpg->tpg_nexus;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200558 struct se_cmd *se_cmd = &pending_req->se_cmd;
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800559 u64 unpacked_lun = pending_req->v2p->lun;
560 int rc, err = FAILED;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200561
Bart Van Asschee3eac122017-05-23 16:48:37 -0700562 init_completion(&pending_req->tmr_done);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200563
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800564 rc = target_submit_tmr(&pending_req->se_cmd, nexus->tvn_se_sess,
565 &pending_req->sense_buffer[0],
Bart Van Assche9f4ab182017-05-23 16:48:36 -0700566 unpacked_lun, NULL, act, GFP_KERNEL,
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800567 tag, TARGET_SCF_ACK_KREF);
568 if (rc)
569 goto err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200570
Bart Van Asschee3eac122017-05-23 16:48:37 -0700571 wait_for_completion(&pending_req->tmr_done);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200572
573 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
574 SUCCESS : FAILED;
575
Juergen Grossd9d660f2014-08-28 06:44:12 +0200576 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
Bart Van Asscheaf90e842017-05-23 16:48:38 -0700577 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800578 return;
Bart Van Assche9f4ab182017-05-23 16:48:36 -0700579
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800580err:
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800581 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200582}
583
584/*
585 Perform virtual to physical translation
586*/
587static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
588 struct ids_tuple *v)
589{
590 struct v2p_entry *entry;
591 struct list_head *head = &(info->v2p_entry_lists);
592 unsigned long flags;
593
594 spin_lock_irqsave(&info->v2p_lock, flags);
595 list_for_each_entry(entry, head, l) {
596 if ((entry->v.chn == v->chn) &&
597 (entry->v.tgt == v->tgt) &&
598 (entry->v.lun == v->lun)) {
599 kref_get(&entry->kref);
600 goto out;
601 }
602 }
603 entry = NULL;
604
605out:
606 spin_unlock_irqrestore(&info->v2p_lock, flags);
607 return entry;
608}
609
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800610static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring,
611 struct v2p_entry *v2p)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200612{
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800613 struct scsiback_tpg *tpg = v2p->tpg;
614 struct scsiback_nexus *nexus = tpg->tpg_nexus;
615 struct se_session *se_sess = nexus->tvn_se_sess;
616 struct vscsibk_pend *req;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -0700617 int tag, cpu, i;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800618
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -0700619 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800620 if (tag < 0) {
621 pr_err("Unable to obtain tag for vscsiif_request\n");
622 return ERR_PTR(-ENOMEM);
623 }
624
625 req = &((struct vscsibk_pend *)se_sess->sess_cmd_map)[tag];
626 memset(req, 0, sizeof(*req));
627 req->se_cmd.map_tag = tag;
Matthew Wilcox10e9cbb2018-06-12 12:05:44 -0700628 req->se_cmd.map_cpu = cpu;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800629
630 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
631 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
632
633 return req;
634}
635
636static struct vscsibk_pend *prepare_pending_reqs(struct vscsibk_info *info,
637 struct vscsiif_back_ring *ring,
638 struct vscsiif_request *ring_req)
639{
640 struct vscsibk_pend *pending_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200641 struct v2p_entry *v2p;
642 struct ids_tuple vir;
643
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800644 /* request range check from frontend */
645 if ((ring_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
646 (ring_req->sc_data_direction != DMA_TO_DEVICE) &&
647 (ring_req->sc_data_direction != DMA_FROM_DEVICE) &&
648 (ring_req->sc_data_direction != DMA_NONE)) {
649 pr_debug("invalid parameter data_dir = %d\n",
650 ring_req->sc_data_direction);
651 return ERR_PTR(-EINVAL);
652 }
653 if (ring_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
654 pr_debug("invalid parameter cmd_len = %d\n",
655 ring_req->cmd_len);
656 return ERR_PTR(-EINVAL);
657 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200658
659 vir.chn = ring_req->channel;
660 vir.tgt = ring_req->id;
661 vir.lun = ring_req->lun;
662
663 v2p = scsiback_do_translation(info, &vir);
664 if (!v2p) {
Tao Chen78574872015-03-10 20:49:18 +0000665 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800666 vir.chn, vir.tgt, vir.lun);
667 return ERR_PTR(-ENODEV);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200668 }
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800669
670 pending_req = scsiback_get_pend_req(ring, v2p);
671 if (IS_ERR(pending_req)) {
672 kref_put(&v2p->kref, scsiback_free_translation_entry);
673 return ERR_PTR(-ENOMEM);
674 }
675 pending_req->rqid = ring_req->rqid;
676 pending_req->info = info;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200677 pending_req->v2p = v2p;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200678 pending_req->sc_data_direction = ring_req->sc_data_direction;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200679 pending_req->cmd_len = ring_req->cmd_len;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200680 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
681
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800682 return pending_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200683}
684
Juergen Gross86991b62020-09-07 15:47:28 +0200685static int scsiback_do_cmd_fn(struct vscsibk_info *info,
686 unsigned int *eoi_flags)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200687{
688 struct vscsiif_back_ring *ring = &info->ring;
Juergen Grossfacb5732015-02-17 08:02:47 +0100689 struct vscsiif_request ring_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200690 struct vscsibk_pend *pending_req;
691 RING_IDX rc, rp;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800692 int more_to_do;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200693 uint32_t result;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200694
695 rc = ring->req_cons;
696 rp = ring->sring->req_prod;
697 rmb(); /* guest system is accessing ring, too */
698
699 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
700 rc = ring->rsp_prod_pvt;
Tao Chen78574872015-03-10 20:49:18 +0000701 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200702 info->domid, rp, rc, rp - rc);
Juergen Gross86991b62020-09-07 15:47:28 +0200703 return -EINVAL;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200704 }
705
706 while ((rc != rp)) {
Juergen Gross86991b62020-09-07 15:47:28 +0200707 *eoi_flags &= ~XEN_EOI_FLAG_SPURIOUS;
708
Juergen Grossd9d660f2014-08-28 06:44:12 +0200709 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
710 break;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200711
David Vrabelbe697462015-11-16 18:02:32 +0000712 RING_COPY_REQUEST(ring, rc, &ring_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200713 ring->req_cons = ++rc;
714
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800715 pending_req = prepare_pending_reqs(info, ring, &ring_req);
716 if (IS_ERR(pending_req)) {
717 switch (PTR_ERR(pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200718 case -ENODEV:
719 result = DID_NO_CONNECT;
720 break;
721 default:
722 result = DRIVER_ERROR;
723 break;
724 }
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800725 scsiback_send_response(info, NULL, result << 24, 0,
726 ring_req.rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200727 return 1;
728 }
729
Juergen Grossfacb5732015-02-17 08:02:47 +0100730 switch (ring_req.act) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200731 case VSCSIIF_ACT_SCSI_CDB:
Juergen Grossfacb5732015-02-17 08:02:47 +0100732 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200733 scsiback_fast_flush_area(pending_req);
734 scsiback_do_resp_with_sense(NULL,
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800735 DRIVER_ERROR << 24, 0, pending_req);
736 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200737 } else {
738 scsiback_cmd_exec(pending_req);
739 }
740 break;
741 case VSCSIIF_ACT_SCSI_ABORT:
742 scsiback_device_action(pending_req, TMR_ABORT_TASK,
Juergen Grossfacb5732015-02-17 08:02:47 +0100743 ring_req.ref_rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200744 break;
745 case VSCSIIF_ACT_SCSI_RESET:
746 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
747 break;
748 default:
Tao Chen78574872015-03-10 20:49:18 +0000749 pr_err_ratelimited("invalid request\n");
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800750 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24, 0,
751 pending_req);
752 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200753 break;
754 }
755
756 /* Yield point for this unbounded loop. */
757 cond_resched();
758 }
759
Juergen Grossca334792020-12-07 08:31:22 +0100760 gnttab_page_cache_shrink(&info->free_pages, scsiback_max_buffer_pages);
761
Juergen Grossd9d660f2014-08-28 06:44:12 +0200762 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
763 return more_to_do;
764}
765
766static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
767{
768 struct vscsibk_info *info = dev_id;
Juergen Gross86991b62020-09-07 15:47:28 +0200769 int rc;
770 unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200771
Juergen Gross86991b62020-09-07 15:47:28 +0200772 while ((rc = scsiback_do_cmd_fn(info, &eoi_flags)) > 0)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200773 cond_resched();
774
Juergen Gross86991b62020-09-07 15:47:28 +0200775 /* In case of a ring error we keep the event channel masked. */
776 if (!rc)
777 xen_irq_lateeoi(irq, eoi_flags);
778
Juergen Grossd9d660f2014-08-28 06:44:12 +0200779 return IRQ_HANDLED;
780}
781
782static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
783 evtchn_port_t evtchn)
784{
785 void *area;
786 struct vscsiif_sring *sring;
787 int err;
788
789 if (info->irq)
790 return -1;
791
Wei Liuccc9d902015-04-03 14:44:59 +0800792 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200793 if (err)
794 return err;
795
796 sring = (struct vscsiif_sring *)area;
797 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
798
Juergen Grossf2fa0e52021-02-11 11:16:13 +0100799 err = bind_interdomain_evtchn_to_irq_lateeoi(info->dev, evtchn);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200800 if (err < 0)
801 goto unmap_page;
802
803 info->irq = err;
804
805 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
806 IRQF_ONESHOT, "vscsiif-backend", info);
807 if (err)
808 goto free_irq;
809
810 return 0;
811
812free_irq:
813 unbind_from_irqhandler(info->irq, info);
814 info->irq = 0;
815unmap_page:
816 xenbus_unmap_ring_vfree(info->dev, area);
817
818 return err;
819}
820
821static int scsiback_map(struct vscsibk_info *info)
822{
823 struct xenbus_device *dev = info->dev;
Yan Yankovskyi0102e4e2020-03-23 18:15:11 +0200824 unsigned int ring_ref;
825 evtchn_port_t evtchn;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200826 int err;
827
828 err = xenbus_gather(XBT_NIL, dev->otherend,
829 "ring-ref", "%u", &ring_ref,
830 "event-channel", "%u", &evtchn, NULL);
831 if (err) {
832 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
833 return err;
834 }
835
836 return scsiback_init_sring(info, ring_ref, evtchn);
837}
838
839/*
Juergen Grossc9e2f532016-02-08 15:30:19 +0100840 Check for a translation entry being present
841*/
842static struct v2p_entry *scsiback_chk_translation_entry(
843 struct vscsibk_info *info, struct ids_tuple *v)
844{
845 struct list_head *head = &(info->v2p_entry_lists);
846 struct v2p_entry *entry;
847
848 list_for_each_entry(entry, head, l)
849 if ((entry->v.chn == v->chn) &&
850 (entry->v.tgt == v->tgt) &&
851 (entry->v.lun == v->lun))
852 return entry;
853
854 return NULL;
855}
856
857/*
Juergen Grossd9d660f2014-08-28 06:44:12 +0200858 Add a new translation entry
859*/
860static int scsiback_add_translation_entry(struct vscsibk_info *info,
861 char *phy, struct ids_tuple *v)
862{
863 int err = 0;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200864 struct v2p_entry *new;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200865 unsigned long flags;
866 char *lunp;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200867 unsigned long long unpacked_lun;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700868 struct se_lun *se_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200869 struct scsiback_tpg *tpg_entry, *tpg = NULL;
870 char *error = "doesn't exist";
871
872 lunp = strrchr(phy, ':');
873 if (!lunp) {
Tao Chen78574872015-03-10 20:49:18 +0000874 pr_err("illegal format of physical device %s\n", phy);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200875 return -EINVAL;
876 }
877 *lunp = 0;
878 lunp++;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200879 err = kstrtoull(lunp, 10, &unpacked_lun);
880 if (err < 0) {
Tao Chen78574872015-03-10 20:49:18 +0000881 pr_err("lun number not valid: %s\n", lunp);
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200882 return err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200883 }
884
885 mutex_lock(&scsiback_mutex);
886 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
887 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
888 !strcmp(phy, tpg_entry->param_alias)) {
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700889 mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
890 hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
891 if (se_lun->unpacked_lun == unpacked_lun) {
892 if (!tpg_entry->tpg_nexus)
893 error = "nexus undefined";
894 else
895 tpg = tpg_entry;
896 break;
897 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200898 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700899 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200900 break;
901 }
902 }
903 if (tpg) {
904 mutex_lock(&tpg->tv_tpg_mutex);
905 tpg->tv_tpg_fe_count++;
906 mutex_unlock(&tpg->tv_tpg_mutex);
907 }
908 mutex_unlock(&scsiback_mutex);
909
910 if (!tpg) {
Nicholas Bellinger0fb1f142015-06-16 00:37:16 -0700911 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200912 return -ENODEV;
913 }
914
915 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
916 if (new == NULL) {
917 err = -ENOMEM;
918 goto out_free;
919 }
920
921 spin_lock_irqsave(&info->v2p_lock, flags);
922
923 /* Check double assignment to identical virtual ID */
Juergen Grossc9e2f532016-02-08 15:30:19 +0100924 if (scsiback_chk_translation_entry(info, v)) {
925 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
926 err = -EEXIST;
927 goto out;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200928 }
929
930 /* Create a new translation entry and add to the list */
931 kref_init(&new->kref);
932 new->v = *v;
933 new->tpg = tpg;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700934 new->lun = unpacked_lun;
Juergen Grossc9e2f532016-02-08 15:30:19 +0100935 list_add_tail(&new->l, &info->v2p_entry_lists);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200936
937out:
938 spin_unlock_irqrestore(&info->v2p_lock, flags);
939
940out_free:
Juergen Grossf285aa82016-02-08 15:30:18 +0100941 if (err) {
942 mutex_lock(&tpg->tv_tpg_mutex);
943 tpg->tv_tpg_fe_count--;
944 mutex_unlock(&tpg->tv_tpg_mutex);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200945 kfree(new);
Juergen Grossf285aa82016-02-08 15:30:18 +0100946 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200947
948 return err;
949}
950
951static void __scsiback_del_translation_entry(struct v2p_entry *entry)
952{
953 list_del(&entry->l);
954 kref_put(&entry->kref, scsiback_free_translation_entry);
955}
956
957/*
Juergen Grossc9e2f532016-02-08 15:30:19 +0100958 Delete the translation entry specified
Juergen Grossd9d660f2014-08-28 06:44:12 +0200959*/
960static int scsiback_del_translation_entry(struct vscsibk_info *info,
961 struct ids_tuple *v)
962{
963 struct v2p_entry *entry;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200964 unsigned long flags;
Juergen Grossc9e2f532016-02-08 15:30:19 +0100965 int ret = 0;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200966
967 spin_lock_irqsave(&info->v2p_lock, flags);
968 /* Find out the translation entry specified */
Juergen Grossc9e2f532016-02-08 15:30:19 +0100969 entry = scsiback_chk_translation_entry(info, v);
970 if (entry)
971 __scsiback_del_translation_entry(entry);
972 else
973 ret = -ENOENT;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200974
975 spin_unlock_irqrestore(&info->v2p_lock, flags);
Juergen Grossc9e2f532016-02-08 15:30:19 +0100976 return ret;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200977}
978
979static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
Juergen Gross169e6cf2015-02-17 08:02:48 +0100980 char *phy, struct ids_tuple *vir, int try)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200981{
Juergen Grossc9e2f532016-02-08 15:30:19 +0100982 struct v2p_entry *entry;
983 unsigned long flags;
Zhouyang Jia7c63ca22018-06-16 08:14:37 +0800984 int err;
Juergen Grossc9e2f532016-02-08 15:30:19 +0100985
986 if (try) {
987 spin_lock_irqsave(&info->v2p_lock, flags);
988 entry = scsiback_chk_translation_entry(info, vir);
989 spin_unlock_irqrestore(&info->v2p_lock, flags);
990 if (entry)
991 return;
992 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200993 if (!scsiback_add_translation_entry(info, phy, vir)) {
994 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
995 "%d", XenbusStateInitialised)) {
Tao Chen78574872015-03-10 20:49:18 +0000996 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200997 scsiback_del_translation_entry(info, vir);
998 }
Juergen Gross169e6cf2015-02-17 08:02:48 +0100999 } else if (!try) {
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001000 err = xenbus_printf(XBT_NIL, info->dev->nodename, state,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001001 "%d", XenbusStateClosed);
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001002 if (err)
1003 xenbus_dev_error(info->dev, err,
1004 "%s: writing %s", __func__, state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001005 }
1006}
1007
1008static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1009 struct ids_tuple *vir)
1010{
1011 if (!scsiback_del_translation_entry(info, vir)) {
1012 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1013 "%d", XenbusStateClosed))
Tao Chen78574872015-03-10 20:49:18 +00001014 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001015 }
1016}
1017
1018#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1019#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1020
1021static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1022 char *ent)
1023{
1024 int err;
1025 struct ids_tuple vir;
1026 char *val;
1027 int device_state;
1028 char phy[VSCSI_NAMELEN];
1029 char str[64];
1030 char state[64];
1031 struct xenbus_device *dev = info->dev;
1032
1033 /* read status */
1034 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1035 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1036 if (XENBUS_EXIST_ERR(err))
1037 return;
1038
1039 /* physical SCSI device */
1040 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1041 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1042 if (IS_ERR(val)) {
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001043 err = xenbus_printf(XBT_NIL, dev->nodename, state,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001044 "%d", XenbusStateClosed);
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001045 if (err)
1046 xenbus_dev_error(info->dev, err,
1047 "%s: writing %s", __func__, state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001048 return;
1049 }
1050 strlcpy(phy, val, VSCSI_NAMELEN);
1051 kfree(val);
1052
1053 /* virtual SCSI device */
1054 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1055 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1056 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1057 if (XENBUS_EXIST_ERR(err)) {
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001058 err = xenbus_printf(XBT_NIL, dev->nodename, state,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001059 "%d", XenbusStateClosed);
Zhouyang Jia7c63ca22018-06-16 08:14:37 +08001060 if (err)
1061 xenbus_dev_error(info->dev, err,
1062 "%s: writing %s", __func__, state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001063 return;
1064 }
1065
1066 switch (op) {
1067 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
Juergen Gross169e6cf2015-02-17 08:02:48 +01001068 switch (device_state) {
1069 case XenbusStateInitialising:
1070 scsiback_do_add_lun(info, state, phy, &vir, 0);
1071 break;
1072 case XenbusStateConnected:
1073 scsiback_do_add_lun(info, state, phy, &vir, 1);
1074 break;
1075 case XenbusStateClosing:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001076 scsiback_do_del_lun(info, state, &vir);
Juergen Gross169e6cf2015-02-17 08:02:48 +01001077 break;
1078 default:
1079 break;
1080 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001081 break;
1082
1083 case VSCSIBACK_OP_UPDATEDEV_STATE:
1084 if (device_state == XenbusStateInitialised) {
1085 /* modify vscsi-devs/dev-x/state */
1086 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1087 "%d", XenbusStateConnected)) {
Tao Chen78574872015-03-10 20:49:18 +00001088 pr_err("xenbus_printf error %s\n", str);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001089 scsiback_del_translation_entry(info, &vir);
1090 xenbus_printf(XBT_NIL, dev->nodename, state,
1091 "%d", XenbusStateClosed);
1092 }
1093 }
1094 break;
Tao Chen78574872015-03-10 20:49:18 +00001095 /* When it is necessary, processing is added here. */
Juergen Grossd9d660f2014-08-28 06:44:12 +02001096 default:
1097 break;
1098 }
1099}
1100
1101static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1102{
1103 int i;
1104 char **dir;
1105 unsigned int ndir = 0;
1106
1107 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1108 &ndir);
1109 if (IS_ERR(dir))
1110 return;
1111
1112 for (i = 0; i < ndir; i++)
1113 scsiback_do_1lun_hotplug(info, op, dir[i]);
1114
1115 kfree(dir);
1116}
1117
1118static void scsiback_frontend_changed(struct xenbus_device *dev,
1119 enum xenbus_state frontend_state)
1120{
1121 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1122
1123 switch (frontend_state) {
1124 case XenbusStateInitialising:
1125 break;
1126
1127 case XenbusStateInitialised:
1128 if (scsiback_map(info))
1129 break;
1130
1131 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1132 xenbus_switch_state(dev, XenbusStateConnected);
1133 break;
1134
1135 case XenbusStateConnected:
1136 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1137
1138 if (dev->state == XenbusStateConnected)
1139 break;
1140
1141 xenbus_switch_state(dev, XenbusStateConnected);
1142 break;
1143
1144 case XenbusStateClosing:
1145 if (info->irq)
1146 scsiback_disconnect(info);
1147
1148 xenbus_switch_state(dev, XenbusStateClosing);
1149 break;
1150
1151 case XenbusStateClosed:
1152 xenbus_switch_state(dev, XenbusStateClosed);
1153 if (xenbus_dev_is_online(dev))
1154 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001155 fallthrough; /* if not online */
Juergen Grossd9d660f2014-08-28 06:44:12 +02001156 case XenbusStateUnknown:
1157 device_unregister(&dev->dev);
1158 break;
1159
1160 case XenbusStateReconfiguring:
1161 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1162 xenbus_switch_state(dev, XenbusStateReconfigured);
1163
1164 break;
1165
1166 default:
1167 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1168 frontend_state);
1169 break;
1170 }
1171}
1172
1173/*
1174 Release the translation entry specfied
1175*/
1176static void scsiback_release_translation_entry(struct vscsibk_info *info)
1177{
1178 struct v2p_entry *entry, *tmp;
1179 struct list_head *head = &(info->v2p_entry_lists);
1180 unsigned long flags;
1181
1182 spin_lock_irqsave(&info->v2p_lock, flags);
1183
1184 list_for_each_entry_safe(entry, tmp, head, l)
1185 __scsiback_del_translation_entry(entry);
1186
1187 spin_unlock_irqrestore(&info->v2p_lock, flags);
1188}
1189
1190static int scsiback_remove(struct xenbus_device *dev)
1191{
1192 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1193
1194 if (info->irq)
1195 scsiback_disconnect(info);
1196
1197 scsiback_release_translation_entry(info);
1198
Juergen Grossca334792020-12-07 08:31:22 +01001199 gnttab_page_cache_shrink(&info->free_pages, 0);
1200
Juergen Grossd9d660f2014-08-28 06:44:12 +02001201 dev_set_drvdata(&dev->dev, NULL);
1202
1203 return 0;
1204}
1205
1206static int scsiback_probe(struct xenbus_device *dev,
1207 const struct xenbus_device_id *id)
1208{
1209 int err;
1210
1211 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1212 GFP_KERNEL);
1213
Tao Chen78574872015-03-10 20:49:18 +00001214 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001215
1216 if (!info) {
1217 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1218 return -ENOMEM;
1219 }
1220 info->dev = dev;
1221 dev_set_drvdata(&dev->dev, info);
1222
1223 info->domid = dev->otherend_id;
1224 spin_lock_init(&info->ring_lock);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001225 atomic_set(&info->nr_unreplied_reqs, 0);
1226 init_waitqueue_head(&info->waiting_to_free);
1227 info->dev = dev;
1228 info->irq = 0;
1229 INIT_LIST_HEAD(&info->v2p_entry_lists);
1230 spin_lock_init(&info->v2p_lock);
Juergen Grossca334792020-12-07 08:31:22 +01001231 gnttab_page_cache_init(&info->free_pages);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001232
1233 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1234 SG_ALL);
1235 if (err)
1236 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1237
1238 err = xenbus_switch_state(dev, XenbusStateInitWait);
1239 if (err)
1240 goto fail;
1241
1242 return 0;
1243
1244fail:
Tao Chen78574872015-03-10 20:49:18 +00001245 pr_warn("%s failed\n", __func__);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001246 scsiback_remove(dev);
1247
1248 return err;
1249}
1250
1251static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1252{
1253 switch (tport->tport_proto_id) {
1254 case SCSI_PROTOCOL_SAS:
1255 return "SAS";
1256 case SCSI_PROTOCOL_FCP:
1257 return "FCP";
1258 case SCSI_PROTOCOL_ISCSI:
1259 return "iSCSI";
1260 default:
1261 break;
1262 }
1263
1264 return "Unknown";
1265}
1266
Juergen Grossd9d660f2014-08-28 06:44:12 +02001267static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1268{
1269 struct scsiback_tpg *tpg = container_of(se_tpg,
1270 struct scsiback_tpg, se_tpg);
1271 struct scsiback_tport *tport = tpg->tport;
1272
1273 return &tport->tport_name[0];
1274}
1275
1276static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1277{
1278 struct scsiback_tpg *tpg = container_of(se_tpg,
1279 struct scsiback_tpg, se_tpg);
1280 return tpg->tport_tpgt;
1281}
1282
Juergen Grossd9d660f2014-08-28 06:44:12 +02001283static struct se_wwn *
1284scsiback_make_tport(struct target_fabric_configfs *tf,
1285 struct config_group *group,
1286 const char *name)
1287{
1288 struct scsiback_tport *tport;
1289 char *ptr;
1290 u64 wwpn = 0;
1291 int off = 0;
1292
1293 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1294 if (!tport)
1295 return ERR_PTR(-ENOMEM);
1296
1297 tport->tport_wwpn = wwpn;
1298 /*
1299 * Determine the emulated Protocol Identifier and Target Port Name
1300 * based on the incoming configfs directory name.
1301 */
1302 ptr = strstr(name, "naa.");
1303 if (ptr) {
1304 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1305 goto check_len;
1306 }
1307 ptr = strstr(name, "fc.");
1308 if (ptr) {
1309 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1310 off = 3; /* Skip over "fc." */
1311 goto check_len;
1312 }
1313 ptr = strstr(name, "iqn.");
1314 if (ptr) {
1315 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1316 goto check_len;
1317 }
1318
1319 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1320 kfree(tport);
1321 return ERR_PTR(-EINVAL);
1322
1323check_len:
1324 if (strlen(name) >= VSCSI_NAMELEN) {
1325 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1326 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1327 kfree(tport);
1328 return ERR_PTR(-EINVAL);
1329 }
1330 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1331
Tao Chen78574872015-03-10 20:49:18 +00001332 pr_debug("Allocated emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001333 scsiback_dump_proto_id(tport), name);
1334
1335 return &tport->tport_wwn;
1336}
1337
1338static void scsiback_drop_tport(struct se_wwn *wwn)
1339{
1340 struct scsiback_tport *tport = container_of(wwn,
1341 struct scsiback_tport, tport_wwn);
1342
Tao Chen78574872015-03-10 20:49:18 +00001343 pr_debug("Deallocating emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001344 scsiback_dump_proto_id(tport), tport->tport_name);
1345
1346 kfree(tport);
1347}
1348
Juergen Grossd9d660f2014-08-28 06:44:12 +02001349static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1350{
1351 return 1;
1352}
1353
1354static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1355{
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -08001356 return transport_generic_free_cmd(se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001357}
1358
1359static void scsiback_release_cmd(struct se_cmd *se_cmd)
1360{
Matthew Wilcox83c2b542018-06-12 12:05:43 -07001361 target_free_tag(se_cmd->se_sess, se_cmd);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001362}
1363
Juergen Grossd9d660f2014-08-28 06:44:12 +02001364static u32 scsiback_sess_get_index(struct se_session *se_sess)
1365{
1366 return 0;
1367}
1368
1369static int scsiback_write_pending(struct se_cmd *se_cmd)
1370{
1371 /* Go ahead and process the write immediately */
1372 target_execute_cmd(se_cmd);
1373
1374 return 0;
1375}
1376
Juergen Grossd9d660f2014-08-28 06:44:12 +02001377static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1378{
1379}
1380
Juergen Grossd9d660f2014-08-28 06:44:12 +02001381static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1382{
1383 return 0;
1384}
1385
1386static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1387{
1388 struct vscsibk_pend *pending_req = container_of(se_cmd,
1389 struct vscsibk_pend, se_cmd);
1390
1391 pending_req->result = SAM_STAT_GOOD;
1392 scsiback_cmd_done(pending_req);
1393 return 0;
1394}
1395
1396static int scsiback_queue_status(struct se_cmd *se_cmd)
1397{
1398 struct vscsibk_pend *pending_req = container_of(se_cmd,
1399 struct vscsibk_pend, se_cmd);
1400
1401 if (se_cmd->sense_buffer &&
1402 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1403 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1404 pending_req->result = (DRIVER_SENSE << 24) |
1405 SAM_STAT_CHECK_CONDITION;
1406 else
1407 pending_req->result = se_cmd->scsi_status;
1408
1409 scsiback_cmd_done(pending_req);
1410 return 0;
1411}
1412
1413static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1414{
Bart Van Assche9f4ab182017-05-23 16:48:36 -07001415 struct vscsibk_pend *pending_req = container_of(se_cmd,
1416 struct vscsibk_pend, se_cmd);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001417
Bart Van Asschee3eac122017-05-23 16:48:37 -07001418 complete(&pending_req->tmr_done);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001419}
1420
1421static void scsiback_aborted_task(struct se_cmd *se_cmd)
1422{
1423}
1424
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001425static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001426 char *page)
1427{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001428 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001429 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1430 se_tpg);
1431 ssize_t rb;
1432
1433 mutex_lock(&tpg->tv_tpg_mutex);
1434 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1435 mutex_unlock(&tpg->tv_tpg_mutex);
1436
1437 return rb;
1438}
1439
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001440static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001441 const char *page, size_t count)
1442{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001443 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001444 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1445 se_tpg);
1446 int len;
1447
1448 if (strlen(page) >= VSCSI_NAMELEN) {
1449 pr_err("param alias: %s, exceeds max: %d\n", page,
1450 VSCSI_NAMELEN);
1451 return -EINVAL;
1452 }
1453
1454 mutex_lock(&tpg->tv_tpg_mutex);
1455 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1456 if (tpg->param_alias[len - 1] == '\n')
1457 tpg->param_alias[len - 1] = '\0';
1458 mutex_unlock(&tpg->tv_tpg_mutex);
1459
1460 return count;
1461}
1462
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001463CONFIGFS_ATTR(scsiback_tpg_param_, alias);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001464
1465static struct configfs_attribute *scsiback_param_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001466 &scsiback_tpg_param_attr_alias,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001467 NULL,
1468};
1469
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001470static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg,
1471 struct se_session *se_sess, void *p)
1472{
1473 struct scsiback_tpg *tpg = container_of(se_tpg,
1474 struct scsiback_tpg, se_tpg);
1475
1476 tpg->tpg_nexus = p;
1477 return 0;
1478}
1479
Juergen Grossd9d660f2014-08-28 06:44:12 +02001480static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1481 const char *name)
1482{
Juergen Grossd9d660f2014-08-28 06:44:12 +02001483 struct scsiback_nexus *tv_nexus;
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001484 int ret = 0;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001485
1486 mutex_lock(&tpg->tv_tpg_mutex);
1487 if (tpg->tpg_nexus) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001488 pr_debug("tpg->tpg_nexus already exists\n");
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001489 ret = -EEXIST;
1490 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001491 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001492
1493 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1494 if (!tv_nexus) {
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001495 ret = -ENOMEM;
1496 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001497 }
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001498
Mike Christiefa834282018-08-02 12:12:23 -05001499 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg,
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001500 VSCSI_DEFAULT_SESSION_TAGS,
1501 sizeof(struct vscsibk_pend),
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001502 TARGET_PROT_NORMAL, name,
1503 tv_nexus, scsiback_alloc_sess_cb);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001504 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001505 kfree(tv_nexus);
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001506 ret = -ENOMEM;
1507 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001508 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001509
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001510out_unlock:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001511 mutex_unlock(&tpg->tv_tpg_mutex);
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001512 return ret;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001513}
1514
1515static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1516{
1517 struct se_session *se_sess;
1518 struct scsiback_nexus *tv_nexus;
1519
1520 mutex_lock(&tpg->tv_tpg_mutex);
1521 tv_nexus = tpg->tpg_nexus;
1522 if (!tv_nexus) {
1523 mutex_unlock(&tpg->tv_tpg_mutex);
1524 return -ENODEV;
1525 }
1526
1527 se_sess = tv_nexus->tvn_se_sess;
1528 if (!se_sess) {
1529 mutex_unlock(&tpg->tv_tpg_mutex);
1530 return -ENODEV;
1531 }
1532
1533 if (tpg->tv_tpg_port_count != 0) {
1534 mutex_unlock(&tpg->tv_tpg_mutex);
1535 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1536 tpg->tv_tpg_port_count);
1537 return -EBUSY;
1538 }
1539
1540 if (tpg->tv_tpg_fe_count != 0) {
1541 mutex_unlock(&tpg->tv_tpg_mutex);
1542 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1543 tpg->tv_tpg_fe_count);
1544 return -EBUSY;
1545 }
1546
Tao Chen78574872015-03-10 20:49:18 +00001547 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001548 scsiback_dump_proto_id(tpg->tport),
1549 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1550
1551 /*
1552 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1553 */
Mike Christie25b88552018-08-02 12:12:27 -05001554 target_remove_session(se_sess);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001555 tpg->tpg_nexus = NULL;
1556 mutex_unlock(&tpg->tv_tpg_mutex);
1557
1558 kfree(tv_nexus);
1559 return 0;
1560}
1561
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001562static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001563{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001564 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001565 struct scsiback_tpg *tpg = container_of(se_tpg,
1566 struct scsiback_tpg, se_tpg);
1567 struct scsiback_nexus *tv_nexus;
1568 ssize_t ret;
1569
1570 mutex_lock(&tpg->tv_tpg_mutex);
1571 tv_nexus = tpg->tpg_nexus;
1572 if (!tv_nexus) {
1573 mutex_unlock(&tpg->tv_tpg_mutex);
1574 return -ENODEV;
1575 }
1576 ret = snprintf(page, PAGE_SIZE, "%s\n",
1577 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1578 mutex_unlock(&tpg->tv_tpg_mutex);
1579
1580 return ret;
1581}
1582
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001583static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1584 const char *page, size_t count)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001585{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001586 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001587 struct scsiback_tpg *tpg = container_of(se_tpg,
1588 struct scsiback_tpg, se_tpg);
1589 struct scsiback_tport *tport_wwn = tpg->tport;
1590 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1591 int ret;
1592 /*
Tao Chen78574872015-03-10 20:49:18 +00001593 * Shutdown the active I_T nexus if 'NULL' is passed.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001594 */
1595 if (!strncmp(page, "NULL", 4)) {
1596 ret = scsiback_drop_nexus(tpg);
1597 return (!ret) ? count : ret;
1598 }
1599 /*
1600 * Otherwise make sure the passed virtual Initiator port WWN matches
1601 * the fabric protocol_id set in scsiback_make_tport(), and call
1602 * scsiback_make_nexus().
1603 */
1604 if (strlen(page) >= VSCSI_NAMELEN) {
1605 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1606 page, VSCSI_NAMELEN);
1607 return -EINVAL;
1608 }
1609 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1610
1611 ptr = strstr(i_port, "naa.");
1612 if (ptr) {
1613 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1614 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1615 i_port, scsiback_dump_proto_id(tport_wwn));
1616 return -EINVAL;
1617 }
1618 port_ptr = &i_port[0];
1619 goto check_newline;
1620 }
1621 ptr = strstr(i_port, "fc.");
1622 if (ptr) {
1623 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1624 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1625 i_port, scsiback_dump_proto_id(tport_wwn));
1626 return -EINVAL;
1627 }
1628 port_ptr = &i_port[3]; /* Skip over "fc." */
1629 goto check_newline;
1630 }
1631 ptr = strstr(i_port, "iqn.");
1632 if (ptr) {
1633 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1634 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1635 i_port, scsiback_dump_proto_id(tport_wwn));
1636 return -EINVAL;
1637 }
1638 port_ptr = &i_port[0];
1639 goto check_newline;
1640 }
1641 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1642 i_port);
1643 return -EINVAL;
1644 /*
1645 * Clear any trailing newline for the NAA WWN
1646 */
1647check_newline:
1648 if (i_port[strlen(i_port) - 1] == '\n')
1649 i_port[strlen(i_port) - 1] = '\0';
1650
1651 ret = scsiback_make_nexus(tpg, port_ptr);
1652 if (ret < 0)
1653 return ret;
1654
1655 return count;
1656}
1657
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001658CONFIGFS_ATTR(scsiback_tpg_, nexus);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001659
1660static struct configfs_attribute *scsiback_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001661 &scsiback_tpg_attr_nexus,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001662 NULL,
1663};
1664
1665static ssize_t
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001666scsiback_wwn_version_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001667{
1668 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1669 UTS_RELEASE"\n",
1670 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1671}
1672
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001673CONFIGFS_ATTR_RO(scsiback_wwn_, version);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001674
1675static struct configfs_attribute *scsiback_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001676 &scsiback_wwn_attr_version,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001677 NULL,
1678};
1679
Juergen Grossd9d660f2014-08-28 06:44:12 +02001680static int scsiback_port_link(struct se_portal_group *se_tpg,
1681 struct se_lun *lun)
1682{
1683 struct scsiback_tpg *tpg = container_of(se_tpg,
1684 struct scsiback_tpg, se_tpg);
1685
1686 mutex_lock(&tpg->tv_tpg_mutex);
1687 tpg->tv_tpg_port_count++;
1688 mutex_unlock(&tpg->tv_tpg_mutex);
1689
1690 return 0;
1691}
1692
1693static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1694 struct se_lun *lun)
1695{
1696 struct scsiback_tpg *tpg = container_of(se_tpg,
1697 struct scsiback_tpg, se_tpg);
1698
1699 mutex_lock(&tpg->tv_tpg_mutex);
1700 tpg->tv_tpg_port_count--;
1701 mutex_unlock(&tpg->tv_tpg_mutex);
1702}
1703
1704static struct se_portal_group *
Bart Van Asscheaa090ea2018-06-22 14:53:02 -07001705scsiback_make_tpg(struct se_wwn *wwn, const char *name)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001706{
1707 struct scsiback_tport *tport = container_of(wwn,
1708 struct scsiback_tport, tport_wwn);
1709
1710 struct scsiback_tpg *tpg;
Dan Carpenter495daef2014-09-08 14:17:35 +03001711 u16 tpgt;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001712 int ret;
1713
1714 if (strstr(name, "tpgt_") != name)
1715 return ERR_PTR(-EINVAL);
Dan Carpenter495daef2014-09-08 14:17:35 +03001716 ret = kstrtou16(name + 5, 10, &tpgt);
1717 if (ret)
1718 return ERR_PTR(ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001719
1720 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1721 if (!tpg)
1722 return ERR_PTR(-ENOMEM);
1723
1724 mutex_init(&tpg->tv_tpg_mutex);
1725 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1726 INIT_LIST_HEAD(&tpg->info_list);
1727 tpg->tport = tport;
1728 tpg->tport_tpgt = tpgt;
1729
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07001730 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001731 if (ret < 0) {
1732 kfree(tpg);
1733 return NULL;
1734 }
1735 mutex_lock(&scsiback_mutex);
1736 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1737 mutex_unlock(&scsiback_mutex);
1738
1739 return &tpg->se_tpg;
1740}
1741
1742static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1743{
1744 struct scsiback_tpg *tpg = container_of(se_tpg,
1745 struct scsiback_tpg, se_tpg);
1746
1747 mutex_lock(&scsiback_mutex);
1748 list_del(&tpg->tv_tpg_list);
1749 mutex_unlock(&scsiback_mutex);
1750 /*
1751 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1752 */
1753 scsiback_drop_nexus(tpg);
1754 /*
Tao Chen78574872015-03-10 20:49:18 +00001755 * Deregister the se_tpg from TCM.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001756 */
1757 core_tpg_deregister(se_tpg);
1758 kfree(tpg);
1759}
1760
1761static int scsiback_check_true(struct se_portal_group *se_tpg)
1762{
1763 return 1;
1764}
1765
1766static int scsiback_check_false(struct se_portal_group *se_tpg)
1767{
1768 return 0;
1769}
1770
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001771static const struct target_core_fabric_ops scsiback_ops = {
1772 .module = THIS_MODULE,
David Disseldorp30c7ca92018-11-23 18:36:12 +01001773 .fabric_name = "xen-pvscsi",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001774 .tpg_get_wwn = scsiback_get_fabric_wwn,
1775 .tpg_get_tag = scsiback_get_tag,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001776 .tpg_check_demo_mode = scsiback_check_true,
1777 .tpg_check_demo_mode_cache = scsiback_check_true,
1778 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1779 .tpg_check_prod_mode_write_protect = scsiback_check_false,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001780 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1781 .check_stop_free = scsiback_check_stop_free,
1782 .release_cmd = scsiback_release_cmd,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001783 .sess_get_index = scsiback_sess_get_index,
1784 .sess_get_initiator_sid = NULL,
1785 .write_pending = scsiback_write_pending,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001786 .set_default_node_attributes = scsiback_set_default_node_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001787 .get_cmd_state = scsiback_get_cmd_state,
1788 .queue_data_in = scsiback_queue_data_in,
1789 .queue_status = scsiback_queue_status,
1790 .queue_tm_rsp = scsiback_queue_tm_rsp,
1791 .aborted_task = scsiback_aborted_task,
1792 /*
1793 * Setup callers for generic logic in target_core_fabric_configfs.c
1794 */
1795 .fabric_make_wwn = scsiback_make_tport,
1796 .fabric_drop_wwn = scsiback_drop_tport,
1797 .fabric_make_tpg = scsiback_make_tpg,
1798 .fabric_drop_tpg = scsiback_drop_tpg,
1799 .fabric_post_link = scsiback_port_link,
1800 .fabric_pre_unlink = scsiback_port_unlink,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001801
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001802 .tfc_wwn_attrs = scsiback_wwn_attrs,
1803 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1804 .tfc_tpg_param_attrs = scsiback_param_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001805};
1806
1807static const struct xenbus_device_id scsiback_ids[] = {
1808 { "vscsi" },
1809 { "" }
1810};
1811
David Vrabel95afae42014-09-08 17:30:41 +01001812static struct xenbus_driver scsiback_driver = {
1813 .ids = scsiback_ids,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001814 .probe = scsiback_probe,
1815 .remove = scsiback_remove,
1816 .otherend_changed = scsiback_frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001817};
Juergen Grossd9d660f2014-08-28 06:44:12 +02001818
Juergen Grossd9d660f2014-08-28 06:44:12 +02001819static int __init scsiback_init(void)
1820{
1821 int ret;
1822
1823 if (!xen_domain())
1824 return -ENODEV;
1825
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001826 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1827 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1828
Juergen Grossd9d660f2014-08-28 06:44:12 +02001829 ret = xenbus_register_backend(&scsiback_driver);
1830 if (ret)
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001831 goto out;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001832
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001833 ret = target_register_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001834 if (ret)
1835 goto out_unregister_xenbus;
1836
1837 return 0;
1838
1839out_unregister_xenbus:
1840 xenbus_unregister_driver(&scsiback_driver);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001841out:
Tao Chen78574872015-03-10 20:49:18 +00001842 pr_err("%s: error %d\n", __func__, ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001843 return ret;
1844}
1845
1846static void __exit scsiback_exit(void)
1847{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001848 target_unregister_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001849 xenbus_unregister_driver(&scsiback_driver);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001850}
1851
1852module_init(scsiback_init);
1853module_exit(scsiback_exit);
1854
1855MODULE_DESCRIPTION("Xen SCSI backend driver");
1856MODULE_LICENSE("Dual BSD/GPL");
1857MODULE_ALIAS("xen-backend:vscsi");
1858MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");