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