blob: 652c8cec800f189579995daffa296b613f9b89d6 [file] [log] [blame]
Juergen Grossd9d660f2014-08-28 06:44:12 +02001/*
2 * Xen SCSI backend driver
3 *
4 * Copyright (c) 2008, FUJITSU Limited
5 *
6 * Based on the blkback driver code.
7 * Adaption to kernel taget core infrastructure taken from vhost/scsi.c
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 * IN THE SOFTWARE.
32 */
33
Tao Chen78574872015-03-10 20:49:18 +000034#define pr_fmt(fmt) "xen-pvscsi: " fmt
35
Juergen Grossd9d660f2014-08-28 06:44:12 +020036#include <stdarg.h>
37
38#include <linux/module.h>
39#include <linux/utsname.h>
40#include <linux/interrupt.h>
41#include <linux/slab.h>
42#include <linux/wait.h>
43#include <linux/sched.h>
44#include <linux/list.h>
45#include <linux/gfp.h>
46#include <linux/delay.h>
47#include <linux/spinlock.h>
48#include <linux/configfs.h>
49
50#include <generated/utsrelease.h>
51
Bart Van Asscheba929992015-05-08 10:11:12 +020052#include <scsi/scsi_host.h> /* SG_ALL */
Juergen Grossd9d660f2014-08-28 06:44:12 +020053
54#include <target/target_core_base.h>
55#include <target/target_core_fabric.h>
Juergen Grossd9d660f2014-08-28 06:44:12 +020056
57#include <asm/hypervisor.h>
58
59#include <xen/xen.h>
60#include <xen/balloon.h>
61#include <xen/events.h>
62#include <xen/xenbus.h>
63#include <xen/grant_table.h>
64#include <xen/page.h>
65
66#include <xen/interface/grant_table.h>
67#include <xen/interface/io/vscsiif.h>
68
Juergen Grossd9d660f2014-08-28 06:44:12 +020069#define VSCSI_VERSION "v0.1"
70#define VSCSI_NAMELEN 32
71
72struct ids_tuple {
73 unsigned int hst; /* host */
74 unsigned int chn; /* channel */
75 unsigned int tgt; /* target */
76 unsigned int lun; /* LUN */
77};
78
79struct v2p_entry {
80 struct ids_tuple v; /* translate from */
81 struct scsiback_tpg *tpg; /* translate to */
82 unsigned int lun;
83 struct kref kref;
84 struct list_head l;
85};
86
87struct vscsibk_info {
88 struct xenbus_device *dev;
89
90 domid_t domid;
91 unsigned int irq;
92
93 struct vscsiif_back_ring ring;
94 int ring_error;
95
96 spinlock_t ring_lock;
97 atomic_t nr_unreplied_reqs;
98
99 spinlock_t v2p_lock;
100 struct list_head v2p_entry_lists;
101
102 wait_queue_head_t waiting_to_free;
103};
104
105/* theoretical maximum of grants for one request */
106#define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
107
108/*
109 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
110 * call to map/unmap grants. Don't choose it too large, as there are arrays
111 * with VSCSI_GRANT_BATCH elements allocated on the stack.
112 */
113#define VSCSI_GRANT_BATCH 16
114
115struct vscsibk_pend {
116 uint16_t rqid;
117
118 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
119 uint8_t cmd_len;
120
121 uint8_t sc_data_direction;
122 uint16_t n_sg; /* real length of SG list */
123 uint16_t n_grants; /* SG pages and potentially SG list */
124 uint32_t data_len;
125 uint32_t result;
126
127 struct vscsibk_info *info;
128 struct v2p_entry *v2p;
129 struct scatterlist *sgl;
130
131 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
132
133 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
134 struct page *pages[VSCSI_MAX_GRANTS];
135
136 struct se_cmd se_cmd;
137};
138
139struct scsiback_tmr {
140 atomic_t tmr_complete;
141 wait_queue_head_t tmr_wait;
142};
143
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800144#define VSCSI_DEFAULT_SESSION_TAGS 128
145
Juergen Grossd9d660f2014-08-28 06:44:12 +0200146struct scsiback_nexus {
147 /* Pointer to TCM session for I_T Nexus */
148 struct se_session *tvn_se_sess;
149};
150
151struct scsiback_tport {
152 /* SCSI protocol the tport is providing */
153 u8 tport_proto_id;
154 /* Binary World Wide unique Port Name for pvscsi Target port */
155 u64 tport_wwpn;
156 /* ASCII formatted WWPN for pvscsi Target port */
157 char tport_name[VSCSI_NAMELEN];
158 /* Returned by scsiback_make_tport() */
159 struct se_wwn tport_wwn;
160};
161
162struct scsiback_tpg {
163 /* scsiback port target portal group tag for TCM */
164 u16 tport_tpgt;
165 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
166 int tv_tpg_port_count;
167 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
168 int tv_tpg_fe_count;
169 /* list for scsiback_list */
170 struct list_head tv_tpg_list;
171 /* Used to protect access for tpg_nexus */
172 struct mutex tv_tpg_mutex;
173 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
174 struct scsiback_nexus *tpg_nexus;
175 /* Pointer back to scsiback_tport */
176 struct scsiback_tport *tport;
177 /* Returned by scsiback_make_tpg() */
178 struct se_portal_group se_tpg;
179 /* alias used in xenstore */
180 char param_alias[VSCSI_NAMELEN];
181 /* list of info structures related to this target portal group */
182 struct list_head info_list;
183};
184
185#define SCSIBACK_INVALID_HANDLE (~0)
186
187static bool log_print_stat;
188module_param(log_print_stat, bool, 0644);
189
190static int scsiback_max_buffer_pages = 1024;
191module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
192MODULE_PARM_DESC(max_buffer_pages,
193"Maximum number of free pages to keep in backend buffer");
194
Juergen Grossd9d660f2014-08-28 06:44:12 +0200195static DEFINE_SPINLOCK(free_pages_lock);
196static int free_pages_num;
197static LIST_HEAD(scsiback_free_pages);
198
199/* Global spinlock to protect scsiback TPG list */
200static DEFINE_MUTEX(scsiback_mutex);
201static LIST_HEAD(scsiback_list);
202
Juergen Grossd9d660f2014-08-28 06:44:12 +0200203static void scsiback_get(struct vscsibk_info *info)
204{
205 atomic_inc(&info->nr_unreplied_reqs);
206}
207
208static void scsiback_put(struct vscsibk_info *info)
209{
210 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
211 wake_up(&info->waiting_to_free);
212}
213
214static void put_free_pages(struct page **page, int num)
215{
216 unsigned long flags;
217 int i = free_pages_num + num, n = num;
218
219 if (num == 0)
220 return;
221 if (i > scsiback_max_buffer_pages) {
222 n = min(num, i - scsiback_max_buffer_pages);
David Vrabelff4b1562015-01-08 18:06:01 +0000223 gnttab_free_pages(n, page + num - n);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200224 n = num - n;
225 }
226 spin_lock_irqsave(&free_pages_lock, flags);
227 for (i = 0; i < n; i++)
228 list_add(&page[i]->lru, &scsiback_free_pages);
229 free_pages_num += n;
230 spin_unlock_irqrestore(&free_pages_lock, flags);
231}
232
233static int get_free_page(struct page **page)
234{
235 unsigned long flags;
236
237 spin_lock_irqsave(&free_pages_lock, flags);
238 if (list_empty(&scsiback_free_pages)) {
239 spin_unlock_irqrestore(&free_pages_lock, flags);
David Vrabelff4b1562015-01-08 18:06:01 +0000240 return gnttab_alloc_pages(1, page);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200241 }
242 page[0] = list_first_entry(&scsiback_free_pages, struct page, lru);
243 list_del(&page[0]->lru);
244 free_pages_num--;
245 spin_unlock_irqrestore(&free_pages_lock, flags);
246 return 0;
247}
248
249static unsigned long vaddr_page(struct page *page)
250{
251 unsigned long pfn = page_to_pfn(page);
252
253 return (unsigned long)pfn_to_kaddr(pfn);
254}
255
256static unsigned long vaddr(struct vscsibk_pend *req, int seg)
257{
258 return vaddr_page(req->pages[seg]);
259}
260
261static void scsiback_print_status(char *sense_buffer, int errors,
262 struct vscsibk_pend *pending_req)
263{
264 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
265
Tao Chen78574872015-03-10 20:49:18 +0000266 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200267 tpg->tport->tport_name, pending_req->v2p->lun,
268 pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
269 host_byte(errors), driver_byte(errors));
Juergen Grossd9d660f2014-08-28 06:44:12 +0200270}
271
272static void scsiback_fast_flush_area(struct vscsibk_pend *req)
273{
274 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
275 struct page *pages[VSCSI_GRANT_BATCH];
276 unsigned int i, invcount = 0;
277 grant_handle_t handle;
278 int err;
279
280 kfree(req->sgl);
281 req->sgl = NULL;
282 req->n_sg = 0;
283
284 if (!req->n_grants)
285 return;
286
287 for (i = 0; i < req->n_grants; i++) {
288 handle = req->grant_handles[i];
289 if (handle == SCSIBACK_INVALID_HANDLE)
290 continue;
291 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
292 GNTMAP_host_map, handle);
293 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
294 pages[invcount] = req->pages[i];
295 put_page(pages[invcount]);
296 invcount++;
297 if (invcount < VSCSI_GRANT_BATCH)
298 continue;
299 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
300 BUG_ON(err);
301 invcount = 0;
302 }
303
304 if (invcount) {
305 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
306 BUG_ON(err);
307 }
308
309 put_free_pages(req->pages, req->n_grants);
310 req->n_grants = 0;
311}
312
313static void scsiback_free_translation_entry(struct kref *kref)
314{
315 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
316 struct scsiback_tpg *tpg = entry->tpg;
317
318 mutex_lock(&tpg->tv_tpg_mutex);
319 tpg->tv_tpg_fe_count--;
320 mutex_unlock(&tpg->tv_tpg_mutex);
321
322 kfree(entry);
323}
324
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800325static void scsiback_send_response(struct vscsibk_info *info,
326 char *sense_buffer, int32_t result, uint32_t resid,
327 uint16_t rqid)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200328{
329 struct vscsiif_response *ring_res;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200330 int notify;
331 struct scsi_sense_hdr sshdr;
332 unsigned long flags;
333 unsigned len;
334
335 spin_lock_irqsave(&info->ring_lock, flags);
336
337 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
338 info->ring.rsp_prod_pvt++;
339
340 ring_res->rslt = result;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800341 ring_res->rqid = rqid;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200342
343 if (sense_buffer != NULL &&
344 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
345 &sshdr)) {
346 len = min_t(unsigned, 8 + sense_buffer[7],
347 VSCSIIF_SENSE_BUFFERSIZE);
348 memcpy(ring_res->sense_buffer, sense_buffer, len);
349 ring_res->sense_len = len;
350 } else {
351 ring_res->sense_len = 0;
352 }
353
354 ring_res->residual_len = resid;
355
356 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
357 spin_unlock_irqrestore(&info->ring_lock, flags);
358
359 if (notify)
360 notify_remote_via_irq(info->irq);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800361}
362
363static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
364 uint32_t resid, struct vscsibk_pend *pending_req)
365{
366 scsiback_send_response(pending_req->info, sense_buffer, result,
367 resid, pending_req->rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200368
369 if (pending_req->v2p)
370 kref_put(&pending_req->v2p->kref,
371 scsiback_free_translation_entry);
372}
373
374static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
375{
376 struct vscsibk_info *info = pending_req->info;
377 unsigned char *sense_buffer;
378 unsigned int resid;
379 int errors;
380
381 sense_buffer = pending_req->sense_buffer;
382 resid = pending_req->se_cmd.residual_count;
383 errors = pending_req->result;
384
385 if (errors && log_print_stat)
386 scsiback_print_status(sense_buffer, errors, pending_req);
387
388 scsiback_fast_flush_area(pending_req);
389 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
390 scsiback_put(info);
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800391 /*
392 * Drop the extra KREF_ACK reference taken by target_submit_cmd_map_sgls()
393 * ahead of scsiback_check_stop_free() -> transport_generic_free_cmd()
394 * final se_cmd->cmd_kref put.
395 */
396 target_put_sess_cmd(&pending_req->se_cmd);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200397}
398
399static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
400{
401 struct se_cmd *se_cmd = &pending_req->se_cmd;
402 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
403 int rc;
404
Juergen Grossd9d660f2014-08-28 06:44:12 +0200405 scsiback_get(pending_req->info);
Bart Van Assche649ee052015-04-14 13:26:44 +0200406 se_cmd->tag = pending_req->rqid;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200407 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
408 pending_req->sense_buffer, pending_req->v2p->lun,
409 pending_req->data_len, 0,
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800410 pending_req->sc_data_direction, TARGET_SCF_ACK_KREF,
Juergen Grossd9d660f2014-08-28 06:44:12 +0200411 pending_req->sgl, pending_req->n_sg,
412 NULL, 0, NULL, 0);
413 if (rc < 0) {
414 transport_send_check_condition_and_sense(se_cmd,
415 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
416 transport_generic_free_cmd(se_cmd, 0);
417 }
418}
419
420static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
421 struct page **pg, grant_handle_t *grant, int cnt)
422{
423 int err, i;
424
425 if (!cnt)
426 return 0;
427
428 err = gnttab_map_refs(map, NULL, pg, cnt);
429 BUG_ON(err);
430 for (i = 0; i < cnt; i++) {
431 if (unlikely(map[i].status != GNTST_okay)) {
Tao Chen78574872015-03-10 20:49:18 +0000432 pr_err("invalid buffer -- could not remap it\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200433 map[i].handle = SCSIBACK_INVALID_HANDLE;
434 err = -ENOMEM;
435 } else {
436 get_page(pg[i]);
437 }
438 grant[i] = map[i].handle;
439 }
440 return err;
441}
442
443static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
444 struct scsiif_request_segment *seg, struct page **pg,
445 grant_handle_t *grant, int cnt, u32 flags)
446{
447 int mapcount = 0, i, err = 0;
448 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
449 struct vscsibk_info *info = pending_req->info;
450
451 for (i = 0; i < cnt; i++) {
452 if (get_free_page(pg + mapcount)) {
453 put_free_pages(pg, mapcount);
Tao Chen78574872015-03-10 20:49:18 +0000454 pr_err("no grant page\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200455 return -ENOMEM;
456 }
457 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
458 flags, seg[i].gref, info->domid);
459 mapcount++;
460 if (mapcount < VSCSI_GRANT_BATCH)
461 continue;
462 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
463 pg += mapcount;
464 grant += mapcount;
465 pending_req->n_grants += mapcount;
466 if (err)
467 return err;
468 mapcount = 0;
469 }
470 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
471 pending_req->n_grants += mapcount;
472 return err;
473}
474
475static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
476 struct vscsibk_pend *pending_req)
477{
478 u32 flags;
479 int i, err, n_segs, i_seg = 0;
480 struct page **pg;
481 struct scsiif_request_segment *seg;
482 unsigned long end_seg = 0;
483 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
484 unsigned int nr_sgl = 0;
485 struct scatterlist *sg;
486 grant_handle_t *grant;
487
488 pending_req->n_sg = 0;
489 pending_req->n_grants = 0;
490 pending_req->data_len = 0;
491
492 nr_segments &= ~VSCSIIF_SG_GRANT;
493 if (!nr_segments)
494 return 0;
495
496 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
Tao Chen78574872015-03-10 20:49:18 +0000497 pr_debug("invalid parameter nr_seg = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200498 ring_req->nr_segments);
499 return -EINVAL;
500 }
501
502 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
503 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
504 pending_req->pages, pending_req->grant_handles,
505 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
506 if (err)
507 return err;
508 nr_sgl = nr_segments;
509 nr_segments = 0;
510 for (i = 0; i < nr_sgl; i++) {
511 n_segs = ring_req->seg[i].length /
512 sizeof(struct scsiif_request_segment);
513 if ((unsigned)ring_req->seg[i].offset +
514 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
515 n_segs * sizeof(struct scsiif_request_segment) !=
516 ring_req->seg[i].length)
517 return -EINVAL;
518 nr_segments += n_segs;
519 }
520 if (nr_segments > SG_ALL) {
Tao Chen78574872015-03-10 20:49:18 +0000521 pr_debug("invalid nr_seg = %d\n", nr_segments);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200522 return -EINVAL;
523 }
524 }
525
Tao Chen78574872015-03-10 20:49:18 +0000526 /* free of (sgl) in fast_flush_area() */
Juergen Grossd9d660f2014-08-28 06:44:12 +0200527 pending_req->sgl = kmalloc_array(nr_segments,
528 sizeof(struct scatterlist), GFP_KERNEL);
529 if (!pending_req->sgl)
530 return -ENOMEM;
531
532 sg_init_table(pending_req->sgl, nr_segments);
533 pending_req->n_sg = nr_segments;
534
535 flags = GNTMAP_host_map;
536 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
537 flags |= GNTMAP_readonly;
538
539 pg = pending_req->pages + nr_sgl;
540 grant = pending_req->grant_handles + nr_sgl;
541 if (!nr_sgl) {
542 seg = ring_req->seg;
543 err = scsiback_gnttab_data_map_list(pending_req, seg,
544 pg, grant, nr_segments, flags);
545 if (err)
546 return err;
547 } else {
548 for (i = 0; i < nr_sgl; i++) {
549 seg = (struct scsiif_request_segment *)(
550 vaddr(pending_req, i) + ring_req->seg[i].offset);
551 n_segs = ring_req->seg[i].length /
552 sizeof(struct scsiif_request_segment);
553 err = scsiback_gnttab_data_map_list(pending_req, seg,
554 pg, grant, n_segs, flags);
555 if (err)
556 return err;
557 pg += n_segs;
558 grant += n_segs;
559 }
560 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
561 seg = (struct scsiif_request_segment *)end_seg;
562 end_seg += ring_req->seg[0].length;
563 pg = pending_req->pages + nr_sgl;
564 }
565
566 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
567 sg_set_page(sg, pg[i], seg->length, seg->offset);
568 pending_req->data_len += seg->length;
569 seg++;
570 if (nr_sgl && (unsigned long)seg >= end_seg) {
571 i_seg++;
572 end_seg = vaddr(pending_req, i_seg) +
573 ring_req->seg[i_seg].offset;
574 seg = (struct scsiif_request_segment *)end_seg;
575 end_seg += ring_req->seg[i_seg].length;
576 }
577 if (sg->offset >= PAGE_SIZE ||
578 sg->length > PAGE_SIZE ||
579 sg->offset + sg->length > PAGE_SIZE)
580 return -EINVAL;
581 }
582
583 return 0;
584}
585
586static void scsiback_disconnect(struct vscsibk_info *info)
587{
588 wait_event(info->waiting_to_free,
589 atomic_read(&info->nr_unreplied_reqs) == 0);
590
591 unbind_from_irqhandler(info->irq, info);
592 info->irq = 0;
593 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
594}
595
596static void scsiback_device_action(struct vscsibk_pend *pending_req,
597 enum tcm_tmreq_table act, int tag)
598{
Juergen Grossd9d660f2014-08-28 06:44:12 +0200599 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800600 struct scsiback_nexus *nexus = tpg->tpg_nexus;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200601 struct se_cmd *se_cmd = &pending_req->se_cmd;
602 struct scsiback_tmr *tmr;
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800603 u64 unpacked_lun = pending_req->v2p->lun;
604 int rc, err = FAILED;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200605
606 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800607 if (!tmr) {
608 target_put_sess_cmd(se_cmd);
609 goto err;
610 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200611
612 init_waitqueue_head(&tmr->tmr_wait);
613
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800614 rc = target_submit_tmr(&pending_req->se_cmd, nexus->tvn_se_sess,
615 &pending_req->sense_buffer[0],
616 unpacked_lun, tmr, act, GFP_KERNEL,
617 tag, TARGET_SCF_ACK_KREF);
618 if (rc)
619 goto err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200620
Juergen Grossd9d660f2014-08-28 06:44:12 +0200621 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
622
623 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
624 SUCCESS : FAILED;
625
Juergen Grossd9d660f2014-08-28 06:44:12 +0200626 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800627 transport_generic_free_cmd(&pending_req->se_cmd, 1);
628 return;
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -0800629err:
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800630 if (tmr)
631 kfree(tmr);
632 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200633}
634
635/*
636 Perform virtual to physical translation
637*/
638static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
639 struct ids_tuple *v)
640{
641 struct v2p_entry *entry;
642 struct list_head *head = &(info->v2p_entry_lists);
643 unsigned long flags;
644
645 spin_lock_irqsave(&info->v2p_lock, flags);
646 list_for_each_entry(entry, head, l) {
647 if ((entry->v.chn == v->chn) &&
648 (entry->v.tgt == v->tgt) &&
649 (entry->v.lun == v->lun)) {
650 kref_get(&entry->kref);
651 goto out;
652 }
653 }
654 entry = NULL;
655
656out:
657 spin_unlock_irqrestore(&info->v2p_lock, flags);
658 return entry;
659}
660
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800661static struct vscsibk_pend *scsiback_get_pend_req(struct vscsiif_back_ring *ring,
662 struct v2p_entry *v2p)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200663{
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800664 struct scsiback_tpg *tpg = v2p->tpg;
665 struct scsiback_nexus *nexus = tpg->tpg_nexus;
666 struct se_session *se_sess = nexus->tvn_se_sess;
667 struct vscsibk_pend *req;
668 int tag, i;
669
670 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
671 if (tag < 0) {
672 pr_err("Unable to obtain tag for vscsiif_request\n");
673 return ERR_PTR(-ENOMEM);
674 }
675
676 req = &((struct vscsibk_pend *)se_sess->sess_cmd_map)[tag];
677 memset(req, 0, sizeof(*req));
678 req->se_cmd.map_tag = tag;
679
680 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
681 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
682
683 return req;
684}
685
686static struct vscsibk_pend *prepare_pending_reqs(struct vscsibk_info *info,
687 struct vscsiif_back_ring *ring,
688 struct vscsiif_request *ring_req)
689{
690 struct vscsibk_pend *pending_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200691 struct v2p_entry *v2p;
692 struct ids_tuple vir;
693
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800694 /* request range check from frontend */
695 if ((ring_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
696 (ring_req->sc_data_direction != DMA_TO_DEVICE) &&
697 (ring_req->sc_data_direction != DMA_FROM_DEVICE) &&
698 (ring_req->sc_data_direction != DMA_NONE)) {
699 pr_debug("invalid parameter data_dir = %d\n",
700 ring_req->sc_data_direction);
701 return ERR_PTR(-EINVAL);
702 }
703 if (ring_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
704 pr_debug("invalid parameter cmd_len = %d\n",
705 ring_req->cmd_len);
706 return ERR_PTR(-EINVAL);
707 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200708
709 vir.chn = ring_req->channel;
710 vir.tgt = ring_req->id;
711 vir.lun = ring_req->lun;
712
713 v2p = scsiback_do_translation(info, &vir);
714 if (!v2p) {
Tao Chen78574872015-03-10 20:49:18 +0000715 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800716 vir.chn, vir.tgt, vir.lun);
717 return ERR_PTR(-ENODEV);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200718 }
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800719
720 pending_req = scsiback_get_pend_req(ring, v2p);
721 if (IS_ERR(pending_req)) {
722 kref_put(&v2p->kref, scsiback_free_translation_entry);
723 return ERR_PTR(-ENOMEM);
724 }
725 pending_req->rqid = ring_req->rqid;
726 pending_req->info = info;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200727 pending_req->v2p = v2p;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200728 pending_req->sc_data_direction = ring_req->sc_data_direction;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200729 pending_req->cmd_len = ring_req->cmd_len;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200730 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
731
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800732 return pending_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200733}
734
735static int scsiback_do_cmd_fn(struct vscsibk_info *info)
736{
737 struct vscsiif_back_ring *ring = &info->ring;
Juergen Grossfacb5732015-02-17 08:02:47 +0100738 struct vscsiif_request ring_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200739 struct vscsibk_pend *pending_req;
740 RING_IDX rc, rp;
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800741 int more_to_do;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200742 uint32_t result;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200743
744 rc = ring->req_cons;
745 rp = ring->sring->req_prod;
746 rmb(); /* guest system is accessing ring, too */
747
748 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
749 rc = ring->rsp_prod_pvt;
Tao Chen78574872015-03-10 20:49:18 +0000750 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200751 info->domid, rp, rc, rp - rc);
752 info->ring_error = 1;
753 return 0;
754 }
755
756 while ((rc != rp)) {
757 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
758 break;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200759
David Vrabelbe697462015-11-16 18:02:32 +0000760 RING_COPY_REQUEST(ring, rc, &ring_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200761 ring->req_cons = ++rc;
762
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800763 pending_req = prepare_pending_reqs(info, ring, &ring_req);
764 if (IS_ERR(pending_req)) {
765 switch (PTR_ERR(pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200766 case -ENODEV:
767 result = DID_NO_CONNECT;
768 break;
769 default:
770 result = DRIVER_ERROR;
771 break;
772 }
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800773 scsiback_send_response(info, NULL, result << 24, 0,
774 ring_req.rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200775 return 1;
776 }
777
Juergen Grossfacb5732015-02-17 08:02:47 +0100778 switch (ring_req.act) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200779 case VSCSIIF_ACT_SCSI_CDB:
Juergen Grossfacb5732015-02-17 08:02:47 +0100780 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200781 scsiback_fast_flush_area(pending_req);
782 scsiback_do_resp_with_sense(NULL,
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800783 DRIVER_ERROR << 24, 0, pending_req);
784 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200785 } else {
786 scsiback_cmd_exec(pending_req);
787 }
788 break;
789 case VSCSIIF_ACT_SCSI_ABORT:
790 scsiback_device_action(pending_req, TMR_ABORT_TASK,
Juergen Grossfacb5732015-02-17 08:02:47 +0100791 ring_req.ref_rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200792 break;
793 case VSCSIIF_ACT_SCSI_RESET:
794 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
795 break;
796 default:
Tao Chen78574872015-03-10 20:49:18 +0000797 pr_err_ratelimited("invalid request\n");
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -0800798 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24, 0,
799 pending_req);
800 transport_generic_free_cmd(&pending_req->se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200801 break;
802 }
803
804 /* Yield point for this unbounded loop. */
805 cond_resched();
806 }
807
808 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
809 return more_to_do;
810}
811
812static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
813{
814 struct vscsibk_info *info = dev_id;
815
816 if (info->ring_error)
817 return IRQ_HANDLED;
818
819 while (scsiback_do_cmd_fn(info))
820 cond_resched();
821
822 return IRQ_HANDLED;
823}
824
825static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
826 evtchn_port_t evtchn)
827{
828 void *area;
829 struct vscsiif_sring *sring;
830 int err;
831
832 if (info->irq)
833 return -1;
834
Wei Liuccc9d902015-04-03 14:44:59 +0800835 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200836 if (err)
837 return err;
838
839 sring = (struct vscsiif_sring *)area;
840 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
841
842 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
843 if (err < 0)
844 goto unmap_page;
845
846 info->irq = err;
847
848 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
849 IRQF_ONESHOT, "vscsiif-backend", info);
850 if (err)
851 goto free_irq;
852
853 return 0;
854
855free_irq:
856 unbind_from_irqhandler(info->irq, info);
857 info->irq = 0;
858unmap_page:
859 xenbus_unmap_ring_vfree(info->dev, area);
860
861 return err;
862}
863
864static int scsiback_map(struct vscsibk_info *info)
865{
866 struct xenbus_device *dev = info->dev;
867 unsigned int ring_ref, evtchn;
868 int err;
869
870 err = xenbus_gather(XBT_NIL, dev->otherend,
871 "ring-ref", "%u", &ring_ref,
872 "event-channel", "%u", &evtchn, NULL);
873 if (err) {
874 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
875 return err;
876 }
877
878 return scsiback_init_sring(info, ring_ref, evtchn);
879}
880
881/*
882 Add a new translation entry
883*/
884static int scsiback_add_translation_entry(struct vscsibk_info *info,
885 char *phy, struct ids_tuple *v)
886{
887 int err = 0;
888 struct v2p_entry *entry;
889 struct v2p_entry *new;
890 struct list_head *head = &(info->v2p_entry_lists);
891 unsigned long flags;
892 char *lunp;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200893 unsigned long long unpacked_lun;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700894 struct se_lun *se_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200895 struct scsiback_tpg *tpg_entry, *tpg = NULL;
896 char *error = "doesn't exist";
897
898 lunp = strrchr(phy, ':');
899 if (!lunp) {
Tao Chen78574872015-03-10 20:49:18 +0000900 pr_err("illegal format of physical device %s\n", phy);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200901 return -EINVAL;
902 }
903 *lunp = 0;
904 lunp++;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200905 err = kstrtoull(lunp, 10, &unpacked_lun);
906 if (err < 0) {
Tao Chen78574872015-03-10 20:49:18 +0000907 pr_err("lun number not valid: %s\n", lunp);
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200908 return err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200909 }
910
911 mutex_lock(&scsiback_mutex);
912 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
913 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
914 !strcmp(phy, tpg_entry->param_alias)) {
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700915 mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
916 hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
917 if (se_lun->unpacked_lun == unpacked_lun) {
918 if (!tpg_entry->tpg_nexus)
919 error = "nexus undefined";
920 else
921 tpg = tpg_entry;
922 break;
923 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200924 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700925 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200926 break;
927 }
928 }
929 if (tpg) {
930 mutex_lock(&tpg->tv_tpg_mutex);
931 tpg->tv_tpg_fe_count++;
932 mutex_unlock(&tpg->tv_tpg_mutex);
933 }
934 mutex_unlock(&scsiback_mutex);
935
936 if (!tpg) {
Nicholas Bellinger0fb1f142015-06-16 00:37:16 -0700937 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200938 return -ENODEV;
939 }
940
941 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
942 if (new == NULL) {
943 err = -ENOMEM;
944 goto out_free;
945 }
946
947 spin_lock_irqsave(&info->v2p_lock, flags);
948
949 /* Check double assignment to identical virtual ID */
950 list_for_each_entry(entry, head, l) {
951 if ((entry->v.chn == v->chn) &&
952 (entry->v.tgt == v->tgt) &&
953 (entry->v.lun == v->lun)) {
Tao Chen78574872015-03-10 20:49:18 +0000954 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200955 err = -EEXIST;
956 goto out;
957 }
958
959 }
960
961 /* Create a new translation entry and add to the list */
962 kref_init(&new->kref);
963 new->v = *v;
964 new->tpg = tpg;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700965 new->lun = unpacked_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200966 list_add_tail(&new->l, head);
967
968out:
969 spin_unlock_irqrestore(&info->v2p_lock, flags);
970
971out_free:
972 mutex_lock(&tpg->tv_tpg_mutex);
973 tpg->tv_tpg_fe_count--;
974 mutex_unlock(&tpg->tv_tpg_mutex);
975
976 if (err)
977 kfree(new);
978
979 return err;
980}
981
982static void __scsiback_del_translation_entry(struct v2p_entry *entry)
983{
984 list_del(&entry->l);
985 kref_put(&entry->kref, scsiback_free_translation_entry);
986}
987
988/*
989 Delete the translation entry specfied
990*/
991static int scsiback_del_translation_entry(struct vscsibk_info *info,
992 struct ids_tuple *v)
993{
994 struct v2p_entry *entry;
995 struct list_head *head = &(info->v2p_entry_lists);
996 unsigned long flags;
997
998 spin_lock_irqsave(&info->v2p_lock, flags);
999 /* Find out the translation entry specified */
1000 list_for_each_entry(entry, head, l) {
1001 if ((entry->v.chn == v->chn) &&
1002 (entry->v.tgt == v->tgt) &&
1003 (entry->v.lun == v->lun)) {
1004 goto found;
1005 }
1006 }
1007
1008 spin_unlock_irqrestore(&info->v2p_lock, flags);
1009 return 1;
1010
1011found:
1012 /* Delete the translation entry specfied */
1013 __scsiback_del_translation_entry(entry);
1014
1015 spin_unlock_irqrestore(&info->v2p_lock, flags);
1016 return 0;
1017}
1018
1019static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
Juergen Gross169e6cf2015-02-17 08:02:48 +01001020 char *phy, struct ids_tuple *vir, int try)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001021{
1022 if (!scsiback_add_translation_entry(info, phy, vir)) {
1023 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1024 "%d", XenbusStateInitialised)) {
Tao Chen78574872015-03-10 20:49:18 +00001025 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001026 scsiback_del_translation_entry(info, vir);
1027 }
Juergen Gross169e6cf2015-02-17 08:02:48 +01001028 } else if (!try) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001029 xenbus_printf(XBT_NIL, info->dev->nodename, state,
1030 "%d", XenbusStateClosed);
1031 }
1032}
1033
1034static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1035 struct ids_tuple *vir)
1036{
1037 if (!scsiback_del_translation_entry(info, vir)) {
1038 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1039 "%d", XenbusStateClosed))
Tao Chen78574872015-03-10 20:49:18 +00001040 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001041 }
1042}
1043
1044#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1045#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1046
1047static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1048 char *ent)
1049{
1050 int err;
1051 struct ids_tuple vir;
1052 char *val;
1053 int device_state;
1054 char phy[VSCSI_NAMELEN];
1055 char str[64];
1056 char state[64];
1057 struct xenbus_device *dev = info->dev;
1058
1059 /* read status */
1060 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1061 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1062 if (XENBUS_EXIST_ERR(err))
1063 return;
1064
1065 /* physical SCSI device */
1066 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1067 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1068 if (IS_ERR(val)) {
1069 xenbus_printf(XBT_NIL, dev->nodename, state,
1070 "%d", XenbusStateClosed);
1071 return;
1072 }
1073 strlcpy(phy, val, VSCSI_NAMELEN);
1074 kfree(val);
1075
1076 /* virtual SCSI device */
1077 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1078 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1079 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1080 if (XENBUS_EXIST_ERR(err)) {
1081 xenbus_printf(XBT_NIL, dev->nodename, state,
1082 "%d", XenbusStateClosed);
1083 return;
1084 }
1085
1086 switch (op) {
1087 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
Juergen Gross169e6cf2015-02-17 08:02:48 +01001088 switch (device_state) {
1089 case XenbusStateInitialising:
1090 scsiback_do_add_lun(info, state, phy, &vir, 0);
1091 break;
1092 case XenbusStateConnected:
1093 scsiback_do_add_lun(info, state, phy, &vir, 1);
1094 break;
1095 case XenbusStateClosing:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001096 scsiback_do_del_lun(info, state, &vir);
Juergen Gross169e6cf2015-02-17 08:02:48 +01001097 break;
1098 default:
1099 break;
1100 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001101 break;
1102
1103 case VSCSIBACK_OP_UPDATEDEV_STATE:
1104 if (device_state == XenbusStateInitialised) {
1105 /* modify vscsi-devs/dev-x/state */
1106 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1107 "%d", XenbusStateConnected)) {
Tao Chen78574872015-03-10 20:49:18 +00001108 pr_err("xenbus_printf error %s\n", str);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001109 scsiback_del_translation_entry(info, &vir);
1110 xenbus_printf(XBT_NIL, dev->nodename, state,
1111 "%d", XenbusStateClosed);
1112 }
1113 }
1114 break;
Tao Chen78574872015-03-10 20:49:18 +00001115 /* When it is necessary, processing is added here. */
Juergen Grossd9d660f2014-08-28 06:44:12 +02001116 default:
1117 break;
1118 }
1119}
1120
1121static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1122{
1123 int i;
1124 char **dir;
1125 unsigned int ndir = 0;
1126
1127 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1128 &ndir);
1129 if (IS_ERR(dir))
1130 return;
1131
1132 for (i = 0; i < ndir; i++)
1133 scsiback_do_1lun_hotplug(info, op, dir[i]);
1134
1135 kfree(dir);
1136}
1137
1138static void scsiback_frontend_changed(struct xenbus_device *dev,
1139 enum xenbus_state frontend_state)
1140{
1141 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1142
1143 switch (frontend_state) {
1144 case XenbusStateInitialising:
1145 break;
1146
1147 case XenbusStateInitialised:
1148 if (scsiback_map(info))
1149 break;
1150
1151 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1152 xenbus_switch_state(dev, XenbusStateConnected);
1153 break;
1154
1155 case XenbusStateConnected:
1156 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1157
1158 if (dev->state == XenbusStateConnected)
1159 break;
1160
1161 xenbus_switch_state(dev, XenbusStateConnected);
1162 break;
1163
1164 case XenbusStateClosing:
1165 if (info->irq)
1166 scsiback_disconnect(info);
1167
1168 xenbus_switch_state(dev, XenbusStateClosing);
1169 break;
1170
1171 case XenbusStateClosed:
1172 xenbus_switch_state(dev, XenbusStateClosed);
1173 if (xenbus_dev_is_online(dev))
1174 break;
1175 /* fall through if not online */
1176 case XenbusStateUnknown:
1177 device_unregister(&dev->dev);
1178 break;
1179
1180 case XenbusStateReconfiguring:
1181 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1182 xenbus_switch_state(dev, XenbusStateReconfigured);
1183
1184 break;
1185
1186 default:
1187 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1188 frontend_state);
1189 break;
1190 }
1191}
1192
1193/*
1194 Release the translation entry specfied
1195*/
1196static void scsiback_release_translation_entry(struct vscsibk_info *info)
1197{
1198 struct v2p_entry *entry, *tmp;
1199 struct list_head *head = &(info->v2p_entry_lists);
1200 unsigned long flags;
1201
1202 spin_lock_irqsave(&info->v2p_lock, flags);
1203
1204 list_for_each_entry_safe(entry, tmp, head, l)
1205 __scsiback_del_translation_entry(entry);
1206
1207 spin_unlock_irqrestore(&info->v2p_lock, flags);
1208}
1209
1210static int scsiback_remove(struct xenbus_device *dev)
1211{
1212 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1213
1214 if (info->irq)
1215 scsiback_disconnect(info);
1216
1217 scsiback_release_translation_entry(info);
1218
1219 dev_set_drvdata(&dev->dev, NULL);
1220
1221 return 0;
1222}
1223
1224static int scsiback_probe(struct xenbus_device *dev,
1225 const struct xenbus_device_id *id)
1226{
1227 int err;
1228
1229 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1230 GFP_KERNEL);
1231
Tao Chen78574872015-03-10 20:49:18 +00001232 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001233
1234 if (!info) {
1235 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1236 return -ENOMEM;
1237 }
1238 info->dev = dev;
1239 dev_set_drvdata(&dev->dev, info);
1240
1241 info->domid = dev->otherend_id;
1242 spin_lock_init(&info->ring_lock);
1243 info->ring_error = 0;
1244 atomic_set(&info->nr_unreplied_reqs, 0);
1245 init_waitqueue_head(&info->waiting_to_free);
1246 info->dev = dev;
1247 info->irq = 0;
1248 INIT_LIST_HEAD(&info->v2p_entry_lists);
1249 spin_lock_init(&info->v2p_lock);
1250
1251 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1252 SG_ALL);
1253 if (err)
1254 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1255
1256 err = xenbus_switch_state(dev, XenbusStateInitWait);
1257 if (err)
1258 goto fail;
1259
1260 return 0;
1261
1262fail:
Tao Chen78574872015-03-10 20:49:18 +00001263 pr_warn("%s failed\n", __func__);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001264 scsiback_remove(dev);
1265
1266 return err;
1267}
1268
1269static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1270{
1271 switch (tport->tport_proto_id) {
1272 case SCSI_PROTOCOL_SAS:
1273 return "SAS";
1274 case SCSI_PROTOCOL_FCP:
1275 return "FCP";
1276 case SCSI_PROTOCOL_ISCSI:
1277 return "iSCSI";
1278 default:
1279 break;
1280 }
1281
1282 return "Unknown";
1283}
1284
Juergen Grossd9d660f2014-08-28 06:44:12 +02001285static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1286{
1287 struct scsiback_tpg *tpg = container_of(se_tpg,
1288 struct scsiback_tpg, se_tpg);
1289 struct scsiback_tport *tport = tpg->tport;
1290
1291 return &tport->tport_name[0];
1292}
1293
1294static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1295{
1296 struct scsiback_tpg *tpg = container_of(se_tpg,
1297 struct scsiback_tpg, se_tpg);
1298 return tpg->tport_tpgt;
1299}
1300
Juergen Grossd9d660f2014-08-28 06:44:12 +02001301static struct se_wwn *
1302scsiback_make_tport(struct target_fabric_configfs *tf,
1303 struct config_group *group,
1304 const char *name)
1305{
1306 struct scsiback_tport *tport;
1307 char *ptr;
1308 u64 wwpn = 0;
1309 int off = 0;
1310
1311 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1312 if (!tport)
1313 return ERR_PTR(-ENOMEM);
1314
1315 tport->tport_wwpn = wwpn;
1316 /*
1317 * Determine the emulated Protocol Identifier and Target Port Name
1318 * based on the incoming configfs directory name.
1319 */
1320 ptr = strstr(name, "naa.");
1321 if (ptr) {
1322 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1323 goto check_len;
1324 }
1325 ptr = strstr(name, "fc.");
1326 if (ptr) {
1327 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1328 off = 3; /* Skip over "fc." */
1329 goto check_len;
1330 }
1331 ptr = strstr(name, "iqn.");
1332 if (ptr) {
1333 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1334 goto check_len;
1335 }
1336
1337 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1338 kfree(tport);
1339 return ERR_PTR(-EINVAL);
1340
1341check_len:
1342 if (strlen(name) >= VSCSI_NAMELEN) {
1343 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1344 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1345 kfree(tport);
1346 return ERR_PTR(-EINVAL);
1347 }
1348 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1349
Tao Chen78574872015-03-10 20:49:18 +00001350 pr_debug("Allocated emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001351 scsiback_dump_proto_id(tport), name);
1352
1353 return &tport->tport_wwn;
1354}
1355
1356static void scsiback_drop_tport(struct se_wwn *wwn)
1357{
1358 struct scsiback_tport *tport = container_of(wwn,
1359 struct scsiback_tport, tport_wwn);
1360
Tao Chen78574872015-03-10 20:49:18 +00001361 pr_debug("Deallocating emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001362 scsiback_dump_proto_id(tport), tport->tport_name);
1363
1364 kfree(tport);
1365}
1366
Juergen Grossd9d660f2014-08-28 06:44:12 +02001367static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1368{
1369 return 1;
1370}
1371
1372static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1373{
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -08001374 return transport_generic_free_cmd(se_cmd, 0);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001375}
1376
1377static void scsiback_release_cmd(struct se_cmd *se_cmd)
1378{
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001379 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingerfa22e7b2016-01-24 22:44:38 -08001380 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1381
1382 if (se_tmr && se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
1383 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1384 kfree(tmr);
1385 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001386
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001387 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001388}
1389
1390static int scsiback_shutdown_session(struct se_session *se_sess)
1391{
1392 return 0;
1393}
1394
1395static void scsiback_close_session(struct se_session *se_sess)
1396{
1397}
1398
1399static u32 scsiback_sess_get_index(struct se_session *se_sess)
1400{
1401 return 0;
1402}
1403
1404static int scsiback_write_pending(struct se_cmd *se_cmd)
1405{
1406 /* Go ahead and process the write immediately */
1407 target_execute_cmd(se_cmd);
1408
1409 return 0;
1410}
1411
1412static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1413{
1414 return 0;
1415}
1416
1417static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1418{
1419}
1420
Juergen Grossd9d660f2014-08-28 06:44:12 +02001421static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1422{
1423 return 0;
1424}
1425
1426static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1427{
1428 struct vscsibk_pend *pending_req = container_of(se_cmd,
1429 struct vscsibk_pend, se_cmd);
1430
1431 pending_req->result = SAM_STAT_GOOD;
1432 scsiback_cmd_done(pending_req);
1433 return 0;
1434}
1435
1436static int scsiback_queue_status(struct se_cmd *se_cmd)
1437{
1438 struct vscsibk_pend *pending_req = container_of(se_cmd,
1439 struct vscsibk_pend, se_cmd);
1440
1441 if (se_cmd->sense_buffer &&
1442 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1443 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1444 pending_req->result = (DRIVER_SENSE << 24) |
1445 SAM_STAT_CHECK_CONDITION;
1446 else
1447 pending_req->result = se_cmd->scsi_status;
1448
1449 scsiback_cmd_done(pending_req);
1450 return 0;
1451}
1452
1453static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1454{
1455 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1456 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1457
1458 atomic_set(&tmr->tmr_complete, 1);
1459 wake_up(&tmr->tmr_wait);
1460}
1461
1462static void scsiback_aborted_task(struct se_cmd *se_cmd)
1463{
1464}
1465
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001466static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001467 char *page)
1468{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001469 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001470 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1471 se_tpg);
1472 ssize_t rb;
1473
1474 mutex_lock(&tpg->tv_tpg_mutex);
1475 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1476 mutex_unlock(&tpg->tv_tpg_mutex);
1477
1478 return rb;
1479}
1480
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001481static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001482 const char *page, size_t count)
1483{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001484 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001485 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1486 se_tpg);
1487 int len;
1488
1489 if (strlen(page) >= VSCSI_NAMELEN) {
1490 pr_err("param alias: %s, exceeds max: %d\n", page,
1491 VSCSI_NAMELEN);
1492 return -EINVAL;
1493 }
1494
1495 mutex_lock(&tpg->tv_tpg_mutex);
1496 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1497 if (tpg->param_alias[len - 1] == '\n')
1498 tpg->param_alias[len - 1] = '\0';
1499 mutex_unlock(&tpg->tv_tpg_mutex);
1500
1501 return count;
1502}
1503
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001504CONFIGFS_ATTR(scsiback_tpg_param_, alias);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001505
1506static struct configfs_attribute *scsiback_param_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001507 &scsiback_tpg_param_attr_alias,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001508 NULL,
1509};
1510
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001511static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg,
1512 struct se_session *se_sess, void *p)
1513{
1514 struct scsiback_tpg *tpg = container_of(se_tpg,
1515 struct scsiback_tpg, se_tpg);
1516
1517 tpg->tpg_nexus = p;
1518 return 0;
1519}
1520
Juergen Grossd9d660f2014-08-28 06:44:12 +02001521static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1522 const char *name)
1523{
Juergen Grossd9d660f2014-08-28 06:44:12 +02001524 struct scsiback_nexus *tv_nexus;
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001525 int ret = 0;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001526
1527 mutex_lock(&tpg->tv_tpg_mutex);
1528 if (tpg->tpg_nexus) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001529 pr_debug("tpg->tpg_nexus already exists\n");
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001530 ret = -EEXIST;
1531 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001532 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001533
1534 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1535 if (!tv_nexus) {
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001536 ret = -ENOMEM;
1537 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001538 }
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001539
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001540 tv_nexus->tvn_se_sess = target_alloc_session(&tpg->se_tpg,
1541 VSCSI_DEFAULT_SESSION_TAGS,
1542 sizeof(struct vscsibk_pend),
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001543 TARGET_PROT_NORMAL, name,
1544 tv_nexus, scsiback_alloc_sess_cb);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001545 if (IS_ERR(tv_nexus->tvn_se_sess)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001546 kfree(tv_nexus);
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001547 ret = -ENOMEM;
1548 goto out_unlock;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001549 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001550
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001551out_unlock:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001552 mutex_unlock(&tpg->tv_tpg_mutex);
Christoph Hellwigfb444abe2016-01-09 05:30:45 -08001553 return ret;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001554}
1555
1556static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1557{
1558 struct se_session *se_sess;
1559 struct scsiback_nexus *tv_nexus;
1560
1561 mutex_lock(&tpg->tv_tpg_mutex);
1562 tv_nexus = tpg->tpg_nexus;
1563 if (!tv_nexus) {
1564 mutex_unlock(&tpg->tv_tpg_mutex);
1565 return -ENODEV;
1566 }
1567
1568 se_sess = tv_nexus->tvn_se_sess;
1569 if (!se_sess) {
1570 mutex_unlock(&tpg->tv_tpg_mutex);
1571 return -ENODEV;
1572 }
1573
1574 if (tpg->tv_tpg_port_count != 0) {
1575 mutex_unlock(&tpg->tv_tpg_mutex);
1576 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1577 tpg->tv_tpg_port_count);
1578 return -EBUSY;
1579 }
1580
1581 if (tpg->tv_tpg_fe_count != 0) {
1582 mutex_unlock(&tpg->tv_tpg_mutex);
1583 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1584 tpg->tv_tpg_fe_count);
1585 return -EBUSY;
1586 }
1587
Tao Chen78574872015-03-10 20:49:18 +00001588 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001589 scsiback_dump_proto_id(tpg->tport),
1590 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1591
1592 /*
1593 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1594 */
1595 transport_deregister_session(tv_nexus->tvn_se_sess);
1596 tpg->tpg_nexus = NULL;
1597 mutex_unlock(&tpg->tv_tpg_mutex);
1598
1599 kfree(tv_nexus);
1600 return 0;
1601}
1602
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001603static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001604{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001605 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001606 struct scsiback_tpg *tpg = container_of(se_tpg,
1607 struct scsiback_tpg, se_tpg);
1608 struct scsiback_nexus *tv_nexus;
1609 ssize_t ret;
1610
1611 mutex_lock(&tpg->tv_tpg_mutex);
1612 tv_nexus = tpg->tpg_nexus;
1613 if (!tv_nexus) {
1614 mutex_unlock(&tpg->tv_tpg_mutex);
1615 return -ENODEV;
1616 }
1617 ret = snprintf(page, PAGE_SIZE, "%s\n",
1618 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1619 mutex_unlock(&tpg->tv_tpg_mutex);
1620
1621 return ret;
1622}
1623
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001624static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1625 const char *page, size_t count)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001626{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001627 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001628 struct scsiback_tpg *tpg = container_of(se_tpg,
1629 struct scsiback_tpg, se_tpg);
1630 struct scsiback_tport *tport_wwn = tpg->tport;
1631 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1632 int ret;
1633 /*
Tao Chen78574872015-03-10 20:49:18 +00001634 * Shutdown the active I_T nexus if 'NULL' is passed.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001635 */
1636 if (!strncmp(page, "NULL", 4)) {
1637 ret = scsiback_drop_nexus(tpg);
1638 return (!ret) ? count : ret;
1639 }
1640 /*
1641 * Otherwise make sure the passed virtual Initiator port WWN matches
1642 * the fabric protocol_id set in scsiback_make_tport(), and call
1643 * scsiback_make_nexus().
1644 */
1645 if (strlen(page) >= VSCSI_NAMELEN) {
1646 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1647 page, VSCSI_NAMELEN);
1648 return -EINVAL;
1649 }
1650 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1651
1652 ptr = strstr(i_port, "naa.");
1653 if (ptr) {
1654 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1655 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1656 i_port, scsiback_dump_proto_id(tport_wwn));
1657 return -EINVAL;
1658 }
1659 port_ptr = &i_port[0];
1660 goto check_newline;
1661 }
1662 ptr = strstr(i_port, "fc.");
1663 if (ptr) {
1664 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1665 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1666 i_port, scsiback_dump_proto_id(tport_wwn));
1667 return -EINVAL;
1668 }
1669 port_ptr = &i_port[3]; /* Skip over "fc." */
1670 goto check_newline;
1671 }
1672 ptr = strstr(i_port, "iqn.");
1673 if (ptr) {
1674 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1675 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1676 i_port, scsiback_dump_proto_id(tport_wwn));
1677 return -EINVAL;
1678 }
1679 port_ptr = &i_port[0];
1680 goto check_newline;
1681 }
1682 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1683 i_port);
1684 return -EINVAL;
1685 /*
1686 * Clear any trailing newline for the NAA WWN
1687 */
1688check_newline:
1689 if (i_port[strlen(i_port) - 1] == '\n')
1690 i_port[strlen(i_port) - 1] = '\0';
1691
1692 ret = scsiback_make_nexus(tpg, port_ptr);
1693 if (ret < 0)
1694 return ret;
1695
1696 return count;
1697}
1698
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001699CONFIGFS_ATTR(scsiback_tpg_, nexus);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001700
1701static struct configfs_attribute *scsiback_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001702 &scsiback_tpg_attr_nexus,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001703 NULL,
1704};
1705
1706static ssize_t
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001707scsiback_wwn_version_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001708{
1709 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1710 UTS_RELEASE"\n",
1711 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1712}
1713
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001714CONFIGFS_ATTR_RO(scsiback_wwn_, version);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001715
1716static struct configfs_attribute *scsiback_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001717 &scsiback_wwn_attr_version,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001718 NULL,
1719};
1720
1721static char *scsiback_get_fabric_name(void)
1722{
1723 return "xen-pvscsi";
1724}
1725
1726static int scsiback_port_link(struct se_portal_group *se_tpg,
1727 struct se_lun *lun)
1728{
1729 struct scsiback_tpg *tpg = container_of(se_tpg,
1730 struct scsiback_tpg, se_tpg);
1731
1732 mutex_lock(&tpg->tv_tpg_mutex);
1733 tpg->tv_tpg_port_count++;
1734 mutex_unlock(&tpg->tv_tpg_mutex);
1735
1736 return 0;
1737}
1738
1739static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1740 struct se_lun *lun)
1741{
1742 struct scsiback_tpg *tpg = container_of(se_tpg,
1743 struct scsiback_tpg, se_tpg);
1744
1745 mutex_lock(&tpg->tv_tpg_mutex);
1746 tpg->tv_tpg_port_count--;
1747 mutex_unlock(&tpg->tv_tpg_mutex);
1748}
1749
1750static struct se_portal_group *
1751scsiback_make_tpg(struct se_wwn *wwn,
1752 struct config_group *group,
1753 const char *name)
1754{
1755 struct scsiback_tport *tport = container_of(wwn,
1756 struct scsiback_tport, tport_wwn);
1757
1758 struct scsiback_tpg *tpg;
Dan Carpenter495daef2014-09-08 14:17:35 +03001759 u16 tpgt;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001760 int ret;
1761
1762 if (strstr(name, "tpgt_") != name)
1763 return ERR_PTR(-EINVAL);
Dan Carpenter495daef2014-09-08 14:17:35 +03001764 ret = kstrtou16(name + 5, 10, &tpgt);
1765 if (ret)
1766 return ERR_PTR(ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001767
1768 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1769 if (!tpg)
1770 return ERR_PTR(-ENOMEM);
1771
1772 mutex_init(&tpg->tv_tpg_mutex);
1773 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1774 INIT_LIST_HEAD(&tpg->info_list);
1775 tpg->tport = tport;
1776 tpg->tport_tpgt = tpgt;
1777
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07001778 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001779 if (ret < 0) {
1780 kfree(tpg);
1781 return NULL;
1782 }
1783 mutex_lock(&scsiback_mutex);
1784 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1785 mutex_unlock(&scsiback_mutex);
1786
1787 return &tpg->se_tpg;
1788}
1789
1790static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1791{
1792 struct scsiback_tpg *tpg = container_of(se_tpg,
1793 struct scsiback_tpg, se_tpg);
1794
1795 mutex_lock(&scsiback_mutex);
1796 list_del(&tpg->tv_tpg_list);
1797 mutex_unlock(&scsiback_mutex);
1798 /*
1799 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1800 */
1801 scsiback_drop_nexus(tpg);
1802 /*
Tao Chen78574872015-03-10 20:49:18 +00001803 * Deregister the se_tpg from TCM.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001804 */
1805 core_tpg_deregister(se_tpg);
1806 kfree(tpg);
1807}
1808
1809static int scsiback_check_true(struct se_portal_group *se_tpg)
1810{
1811 return 1;
1812}
1813
1814static int scsiback_check_false(struct se_portal_group *se_tpg)
1815{
1816 return 0;
1817}
1818
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001819static const struct target_core_fabric_ops scsiback_ops = {
1820 .module = THIS_MODULE,
1821 .name = "xen-pvscsi",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001822 .get_fabric_name = scsiback_get_fabric_name,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001823 .tpg_get_wwn = scsiback_get_fabric_wwn,
1824 .tpg_get_tag = scsiback_get_tag,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001825 .tpg_check_demo_mode = scsiback_check_true,
1826 .tpg_check_demo_mode_cache = scsiback_check_true,
1827 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1828 .tpg_check_prod_mode_write_protect = scsiback_check_false,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001829 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1830 .check_stop_free = scsiback_check_stop_free,
1831 .release_cmd = scsiback_release_cmd,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001832 .shutdown_session = scsiback_shutdown_session,
1833 .close_session = scsiback_close_session,
1834 .sess_get_index = scsiback_sess_get_index,
1835 .sess_get_initiator_sid = NULL,
1836 .write_pending = scsiback_write_pending,
1837 .write_pending_status = scsiback_write_pending_status,
1838 .set_default_node_attributes = scsiback_set_default_node_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001839 .get_cmd_state = scsiback_get_cmd_state,
1840 .queue_data_in = scsiback_queue_data_in,
1841 .queue_status = scsiback_queue_status,
1842 .queue_tm_rsp = scsiback_queue_tm_rsp,
1843 .aborted_task = scsiback_aborted_task,
1844 /*
1845 * Setup callers for generic logic in target_core_fabric_configfs.c
1846 */
1847 .fabric_make_wwn = scsiback_make_tport,
1848 .fabric_drop_wwn = scsiback_drop_tport,
1849 .fabric_make_tpg = scsiback_make_tpg,
1850 .fabric_drop_tpg = scsiback_drop_tpg,
1851 .fabric_post_link = scsiback_port_link,
1852 .fabric_pre_unlink = scsiback_port_unlink,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001853
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001854 .tfc_wwn_attrs = scsiback_wwn_attrs,
1855 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1856 .tfc_tpg_param_attrs = scsiback_param_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001857};
1858
1859static const struct xenbus_device_id scsiback_ids[] = {
1860 { "vscsi" },
1861 { "" }
1862};
1863
David Vrabel95afae42014-09-08 17:30:41 +01001864static struct xenbus_driver scsiback_driver = {
1865 .ids = scsiback_ids,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001866 .probe = scsiback_probe,
1867 .remove = scsiback_remove,
1868 .otherend_changed = scsiback_frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001869};
Juergen Grossd9d660f2014-08-28 06:44:12 +02001870
Juergen Grossd9d660f2014-08-28 06:44:12 +02001871static int __init scsiback_init(void)
1872{
1873 int ret;
1874
1875 if (!xen_domain())
1876 return -ENODEV;
1877
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001878 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1879 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1880
Juergen Grossd9d660f2014-08-28 06:44:12 +02001881 ret = xenbus_register_backend(&scsiback_driver);
1882 if (ret)
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001883 goto out;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001884
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001885 ret = target_register_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001886 if (ret)
1887 goto out_unregister_xenbus;
1888
1889 return 0;
1890
1891out_unregister_xenbus:
1892 xenbus_unregister_driver(&scsiback_driver);
Nicholas Bellinger2dbcdf32016-01-24 22:41:50 -08001893out:
Tao Chen78574872015-03-10 20:49:18 +00001894 pr_err("%s: error %d\n", __func__, ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001895 return ret;
1896}
1897
1898static void __exit scsiback_exit(void)
1899{
1900 struct page *page;
1901
1902 while (free_pages_num) {
1903 if (get_free_page(&page))
1904 BUG();
David Vrabelff4b1562015-01-08 18:06:01 +00001905 gnttab_free_pages(1, &page);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001906 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001907 target_unregister_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001908 xenbus_unregister_driver(&scsiback_driver);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001909}
1910
1911module_init(scsiback_init);
1912module_exit(scsiback_exit);
1913
1914MODULE_DESCRIPTION("Xen SCSI backend driver");
1915MODULE_LICENSE("Dual BSD/GPL");
1916MODULE_ALIAS("xen-backend:vscsi");
1917MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");