blob: 9eeefd7cad41127dcade5d81c06647218abeaccc [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#include <target/target_core_fabric_configfs.h>
57
58#include <asm/hypervisor.h>
59
60#include <xen/xen.h>
61#include <xen/balloon.h>
62#include <xen/events.h>
63#include <xen/xenbus.h>
64#include <xen/grant_table.h>
65#include <xen/page.h>
66
67#include <xen/interface/grant_table.h>
68#include <xen/interface/io/vscsiif.h>
69
Juergen Grossd9d660f2014-08-28 06:44:12 +020070#define VSCSI_VERSION "v0.1"
71#define VSCSI_NAMELEN 32
72
73struct ids_tuple {
74 unsigned int hst; /* host */
75 unsigned int chn; /* channel */
76 unsigned int tgt; /* target */
77 unsigned int lun; /* LUN */
78};
79
80struct v2p_entry {
81 struct ids_tuple v; /* translate from */
82 struct scsiback_tpg *tpg; /* translate to */
83 unsigned int lun;
84 struct kref kref;
85 struct list_head l;
86};
87
88struct vscsibk_info {
89 struct xenbus_device *dev;
90
91 domid_t domid;
92 unsigned int irq;
93
94 struct vscsiif_back_ring ring;
95 int ring_error;
96
97 spinlock_t ring_lock;
98 atomic_t nr_unreplied_reqs;
99
100 spinlock_t v2p_lock;
101 struct list_head v2p_entry_lists;
102
103 wait_queue_head_t waiting_to_free;
104};
105
106/* theoretical maximum of grants for one request */
107#define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
108
109/*
110 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
111 * call to map/unmap grants. Don't choose it too large, as there are arrays
112 * with VSCSI_GRANT_BATCH elements allocated on the stack.
113 */
114#define VSCSI_GRANT_BATCH 16
115
116struct vscsibk_pend {
117 uint16_t rqid;
118
119 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
120 uint8_t cmd_len;
121
122 uint8_t sc_data_direction;
123 uint16_t n_sg; /* real length of SG list */
124 uint16_t n_grants; /* SG pages and potentially SG list */
125 uint32_t data_len;
126 uint32_t result;
127
128 struct vscsibk_info *info;
129 struct v2p_entry *v2p;
130 struct scatterlist *sgl;
131
132 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
133
134 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
135 struct page *pages[VSCSI_MAX_GRANTS];
136
137 struct se_cmd se_cmd;
138};
139
140struct scsiback_tmr {
141 atomic_t tmr_complete;
142 wait_queue_head_t tmr_wait;
143};
144
145struct scsiback_nexus {
146 /* Pointer to TCM session for I_T Nexus */
147 struct se_session *tvn_se_sess;
148};
149
150struct scsiback_tport {
151 /* SCSI protocol the tport is providing */
152 u8 tport_proto_id;
153 /* Binary World Wide unique Port Name for pvscsi Target port */
154 u64 tport_wwpn;
155 /* ASCII formatted WWPN for pvscsi Target port */
156 char tport_name[VSCSI_NAMELEN];
157 /* Returned by scsiback_make_tport() */
158 struct se_wwn tport_wwn;
159};
160
161struct scsiback_tpg {
162 /* scsiback port target portal group tag for TCM */
163 u16 tport_tpgt;
164 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
165 int tv_tpg_port_count;
166 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
167 int tv_tpg_fe_count;
168 /* list for scsiback_list */
169 struct list_head tv_tpg_list;
170 /* Used to protect access for tpg_nexus */
171 struct mutex tv_tpg_mutex;
172 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
173 struct scsiback_nexus *tpg_nexus;
174 /* Pointer back to scsiback_tport */
175 struct scsiback_tport *tport;
176 /* Returned by scsiback_make_tpg() */
177 struct se_portal_group se_tpg;
178 /* alias used in xenstore */
179 char param_alias[VSCSI_NAMELEN];
180 /* list of info structures related to this target portal group */
181 struct list_head info_list;
182};
183
184#define SCSIBACK_INVALID_HANDLE (~0)
185
186static bool log_print_stat;
187module_param(log_print_stat, bool, 0644);
188
189static int scsiback_max_buffer_pages = 1024;
190module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
191MODULE_PARM_DESC(max_buffer_pages,
192"Maximum number of free pages to keep in backend buffer");
193
194static struct kmem_cache *scsiback_cachep;
195static 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
325static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
326 uint32_t resid, struct vscsibk_pend *pending_req)
327{
328 struct vscsiif_response *ring_res;
329 struct vscsibk_info *info = pending_req->info;
330 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;
341 ring_res->rqid = pending_req->rqid;
342
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);
361
362 if (pending_req->v2p)
363 kref_put(&pending_req->v2p->kref,
364 scsiback_free_translation_entry);
365}
366
367static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
368{
369 struct vscsibk_info *info = pending_req->info;
370 unsigned char *sense_buffer;
371 unsigned int resid;
372 int errors;
373
374 sense_buffer = pending_req->sense_buffer;
375 resid = pending_req->se_cmd.residual_count;
376 errors = pending_req->result;
377
378 if (errors && log_print_stat)
379 scsiback_print_status(sense_buffer, errors, pending_req);
380
381 scsiback_fast_flush_area(pending_req);
382 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
383 scsiback_put(info);
384}
385
386static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
387{
388 struct se_cmd *se_cmd = &pending_req->se_cmd;
389 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
390 int rc;
391
392 memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
393
394 memset(se_cmd, 0, sizeof(*se_cmd));
395
396 scsiback_get(pending_req->info);
Bart Van Assche649ee052015-04-14 13:26:44 +0200397 se_cmd->tag = pending_req->rqid;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200398 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
399 pending_req->sense_buffer, pending_req->v2p->lun,
400 pending_req->data_len, 0,
401 pending_req->sc_data_direction, 0,
402 pending_req->sgl, pending_req->n_sg,
403 NULL, 0, NULL, 0);
404 if (rc < 0) {
405 transport_send_check_condition_and_sense(se_cmd,
406 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
407 transport_generic_free_cmd(se_cmd, 0);
408 }
409}
410
411static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
412 struct page **pg, grant_handle_t *grant, int cnt)
413{
414 int err, i;
415
416 if (!cnt)
417 return 0;
418
419 err = gnttab_map_refs(map, NULL, pg, cnt);
420 BUG_ON(err);
421 for (i = 0; i < cnt; i++) {
422 if (unlikely(map[i].status != GNTST_okay)) {
Tao Chen78574872015-03-10 20:49:18 +0000423 pr_err("invalid buffer -- could not remap it\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200424 map[i].handle = SCSIBACK_INVALID_HANDLE;
425 err = -ENOMEM;
426 } else {
427 get_page(pg[i]);
428 }
429 grant[i] = map[i].handle;
430 }
431 return err;
432}
433
434static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
435 struct scsiif_request_segment *seg, struct page **pg,
436 grant_handle_t *grant, int cnt, u32 flags)
437{
438 int mapcount = 0, i, err = 0;
439 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
440 struct vscsibk_info *info = pending_req->info;
441
442 for (i = 0; i < cnt; i++) {
443 if (get_free_page(pg + mapcount)) {
444 put_free_pages(pg, mapcount);
Tao Chen78574872015-03-10 20:49:18 +0000445 pr_err("no grant page\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200446 return -ENOMEM;
447 }
448 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
449 flags, seg[i].gref, info->domid);
450 mapcount++;
451 if (mapcount < VSCSI_GRANT_BATCH)
452 continue;
453 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
454 pg += mapcount;
455 grant += mapcount;
456 pending_req->n_grants += mapcount;
457 if (err)
458 return err;
459 mapcount = 0;
460 }
461 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
462 pending_req->n_grants += mapcount;
463 return err;
464}
465
466static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
467 struct vscsibk_pend *pending_req)
468{
469 u32 flags;
470 int i, err, n_segs, i_seg = 0;
471 struct page **pg;
472 struct scsiif_request_segment *seg;
473 unsigned long end_seg = 0;
474 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
475 unsigned int nr_sgl = 0;
476 struct scatterlist *sg;
477 grant_handle_t *grant;
478
479 pending_req->n_sg = 0;
480 pending_req->n_grants = 0;
481 pending_req->data_len = 0;
482
483 nr_segments &= ~VSCSIIF_SG_GRANT;
484 if (!nr_segments)
485 return 0;
486
487 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
Tao Chen78574872015-03-10 20:49:18 +0000488 pr_debug("invalid parameter nr_seg = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200489 ring_req->nr_segments);
490 return -EINVAL;
491 }
492
493 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
494 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
495 pending_req->pages, pending_req->grant_handles,
496 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
497 if (err)
498 return err;
499 nr_sgl = nr_segments;
500 nr_segments = 0;
501 for (i = 0; i < nr_sgl; i++) {
502 n_segs = ring_req->seg[i].length /
503 sizeof(struct scsiif_request_segment);
504 if ((unsigned)ring_req->seg[i].offset +
505 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
506 n_segs * sizeof(struct scsiif_request_segment) !=
507 ring_req->seg[i].length)
508 return -EINVAL;
509 nr_segments += n_segs;
510 }
511 if (nr_segments > SG_ALL) {
Tao Chen78574872015-03-10 20:49:18 +0000512 pr_debug("invalid nr_seg = %d\n", nr_segments);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200513 return -EINVAL;
514 }
515 }
516
Tao Chen78574872015-03-10 20:49:18 +0000517 /* free of (sgl) in fast_flush_area() */
Juergen Grossd9d660f2014-08-28 06:44:12 +0200518 pending_req->sgl = kmalloc_array(nr_segments,
519 sizeof(struct scatterlist), GFP_KERNEL);
520 if (!pending_req->sgl)
521 return -ENOMEM;
522
523 sg_init_table(pending_req->sgl, nr_segments);
524 pending_req->n_sg = nr_segments;
525
526 flags = GNTMAP_host_map;
527 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
528 flags |= GNTMAP_readonly;
529
530 pg = pending_req->pages + nr_sgl;
531 grant = pending_req->grant_handles + nr_sgl;
532 if (!nr_sgl) {
533 seg = ring_req->seg;
534 err = scsiback_gnttab_data_map_list(pending_req, seg,
535 pg, grant, nr_segments, flags);
536 if (err)
537 return err;
538 } else {
539 for (i = 0; i < nr_sgl; i++) {
540 seg = (struct scsiif_request_segment *)(
541 vaddr(pending_req, i) + ring_req->seg[i].offset);
542 n_segs = ring_req->seg[i].length /
543 sizeof(struct scsiif_request_segment);
544 err = scsiback_gnttab_data_map_list(pending_req, seg,
545 pg, grant, n_segs, flags);
546 if (err)
547 return err;
548 pg += n_segs;
549 grant += n_segs;
550 }
551 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
552 seg = (struct scsiif_request_segment *)end_seg;
553 end_seg += ring_req->seg[0].length;
554 pg = pending_req->pages + nr_sgl;
555 }
556
557 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
558 sg_set_page(sg, pg[i], seg->length, seg->offset);
559 pending_req->data_len += seg->length;
560 seg++;
561 if (nr_sgl && (unsigned long)seg >= end_seg) {
562 i_seg++;
563 end_seg = vaddr(pending_req, i_seg) +
564 ring_req->seg[i_seg].offset;
565 seg = (struct scsiif_request_segment *)end_seg;
566 end_seg += ring_req->seg[i_seg].length;
567 }
568 if (sg->offset >= PAGE_SIZE ||
569 sg->length > PAGE_SIZE ||
570 sg->offset + sg->length > PAGE_SIZE)
571 return -EINVAL;
572 }
573
574 return 0;
575}
576
577static void scsiback_disconnect(struct vscsibk_info *info)
578{
579 wait_event(info->waiting_to_free,
580 atomic_read(&info->nr_unreplied_reqs) == 0);
581
582 unbind_from_irqhandler(info->irq, info);
583 info->irq = 0;
584 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
585}
586
587static void scsiback_device_action(struct vscsibk_pend *pending_req,
588 enum tcm_tmreq_table act, int tag)
589{
590 int rc, err = FAILED;
591 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
592 struct se_cmd *se_cmd = &pending_req->se_cmd;
593 struct scsiback_tmr *tmr;
594
595 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
596 if (!tmr)
597 goto out;
598
599 init_waitqueue_head(&tmr->tmr_wait);
600
601 transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800602 tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, TCM_SIMPLE_TAG,
Juergen Grossd9d660f2014-08-28 06:44:12 +0200603 &pending_req->sense_buffer[0]);
604
605 rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
606 if (rc < 0)
607 goto out;
608
609 se_cmd->se_tmr_req->ref_task_tag = tag;
610
611 if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
612 goto out;
613
614 transport_generic_handle_tmr(se_cmd);
615 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
616
617 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
618 SUCCESS : FAILED;
619
620out:
621 if (tmr) {
622 transport_generic_free_cmd(&pending_req->se_cmd, 1);
623 kfree(tmr);
624 }
625
626 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
627
628 kmem_cache_free(scsiback_cachep, pending_req);
629}
630
631/*
632 Perform virtual to physical translation
633*/
634static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
635 struct ids_tuple *v)
636{
637 struct v2p_entry *entry;
638 struct list_head *head = &(info->v2p_entry_lists);
639 unsigned long flags;
640
641 spin_lock_irqsave(&info->v2p_lock, flags);
642 list_for_each_entry(entry, head, l) {
643 if ((entry->v.chn == v->chn) &&
644 (entry->v.tgt == v->tgt) &&
645 (entry->v.lun == v->lun)) {
646 kref_get(&entry->kref);
647 goto out;
648 }
649 }
650 entry = NULL;
651
652out:
653 spin_unlock_irqrestore(&info->v2p_lock, flags);
654 return entry;
655}
656
657static int prepare_pending_reqs(struct vscsibk_info *info,
658 struct vscsiif_request *ring_req,
659 struct vscsibk_pend *pending_req)
660{
661 struct v2p_entry *v2p;
662 struct ids_tuple vir;
663
664 pending_req->rqid = ring_req->rqid;
665 pending_req->info = info;
666
667 vir.chn = ring_req->channel;
668 vir.tgt = ring_req->id;
669 vir.lun = ring_req->lun;
670
671 v2p = scsiback_do_translation(info, &vir);
672 if (!v2p) {
673 pending_req->v2p = NULL;
Tao Chen78574872015-03-10 20:49:18 +0000674 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
675 vir.chn, vir.tgt, vir.lun);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200676 return -ENODEV;
677 }
678 pending_req->v2p = v2p;
679
680 /* request range check from frontend */
681 pending_req->sc_data_direction = ring_req->sc_data_direction;
682 if ((pending_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
683 (pending_req->sc_data_direction != DMA_TO_DEVICE) &&
684 (pending_req->sc_data_direction != DMA_FROM_DEVICE) &&
685 (pending_req->sc_data_direction != DMA_NONE)) {
Tao Chen78574872015-03-10 20:49:18 +0000686 pr_debug("invalid parameter data_dir = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200687 pending_req->sc_data_direction);
688 return -EINVAL;
689 }
690
691 pending_req->cmd_len = ring_req->cmd_len;
692 if (pending_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
Tao Chen78574872015-03-10 20:49:18 +0000693 pr_debug("invalid parameter cmd_len = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200694 pending_req->cmd_len);
695 return -EINVAL;
696 }
697 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
698
699 return 0;
700}
701
702static int scsiback_do_cmd_fn(struct vscsibk_info *info)
703{
704 struct vscsiif_back_ring *ring = &info->ring;
Juergen Grossfacb5732015-02-17 08:02:47 +0100705 struct vscsiif_request ring_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200706 struct vscsibk_pend *pending_req;
707 RING_IDX rc, rp;
708 int err, more_to_do;
709 uint32_t result;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200710
711 rc = ring->req_cons;
712 rp = ring->sring->req_prod;
713 rmb(); /* guest system is accessing ring, too */
714
715 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
716 rc = ring->rsp_prod_pvt;
Tao Chen78574872015-03-10 20:49:18 +0000717 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200718 info->domid, rp, rc, rp - rc);
719 info->ring_error = 1;
720 return 0;
721 }
722
723 while ((rc != rp)) {
724 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
725 break;
726 pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
727 if (!pending_req)
728 return 1;
729
Juergen Grossfacb5732015-02-17 08:02:47 +0100730 ring_req = *RING_GET_REQUEST(ring, rc);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200731 ring->req_cons = ++rc;
732
Juergen Grossfacb5732015-02-17 08:02:47 +0100733 err = prepare_pending_reqs(info, &ring_req, pending_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200734 if (err) {
735 switch (err) {
736 case -ENODEV:
737 result = DID_NO_CONNECT;
738 break;
739 default:
740 result = DRIVER_ERROR;
741 break;
742 }
743 scsiback_do_resp_with_sense(NULL, result << 24, 0,
744 pending_req);
745 kmem_cache_free(scsiback_cachep, pending_req);
746 return 1;
747 }
748
Juergen Grossfacb5732015-02-17 08:02:47 +0100749 switch (ring_req.act) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200750 case VSCSIIF_ACT_SCSI_CDB:
Juergen Grossfacb5732015-02-17 08:02:47 +0100751 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200752 scsiback_fast_flush_area(pending_req);
753 scsiback_do_resp_with_sense(NULL,
754 DRIVER_ERROR << 24, 0, pending_req);
755 kmem_cache_free(scsiback_cachep, pending_req);
756 } else {
757 scsiback_cmd_exec(pending_req);
758 }
759 break;
760 case VSCSIIF_ACT_SCSI_ABORT:
761 scsiback_device_action(pending_req, TMR_ABORT_TASK,
Juergen Grossfacb5732015-02-17 08:02:47 +0100762 ring_req.ref_rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200763 break;
764 case VSCSIIF_ACT_SCSI_RESET:
765 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
766 break;
767 default:
Tao Chen78574872015-03-10 20:49:18 +0000768 pr_err_ratelimited("invalid request\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200769 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
770 0, pending_req);
771 kmem_cache_free(scsiback_cachep, pending_req);
772 break;
773 }
774
775 /* Yield point for this unbounded loop. */
776 cond_resched();
777 }
778
779 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
780 return more_to_do;
781}
782
783static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
784{
785 struct vscsibk_info *info = dev_id;
786
787 if (info->ring_error)
788 return IRQ_HANDLED;
789
790 while (scsiback_do_cmd_fn(info))
791 cond_resched();
792
793 return IRQ_HANDLED;
794}
795
796static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
797 evtchn_port_t evtchn)
798{
799 void *area;
800 struct vscsiif_sring *sring;
801 int err;
802
803 if (info->irq)
804 return -1;
805
Wei Liuccc9d902015-04-03 14:44:59 +0800806 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200807 if (err)
808 return err;
809
810 sring = (struct vscsiif_sring *)area;
811 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
812
813 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
814 if (err < 0)
815 goto unmap_page;
816
817 info->irq = err;
818
819 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
820 IRQF_ONESHOT, "vscsiif-backend", info);
821 if (err)
822 goto free_irq;
823
824 return 0;
825
826free_irq:
827 unbind_from_irqhandler(info->irq, info);
828 info->irq = 0;
829unmap_page:
830 xenbus_unmap_ring_vfree(info->dev, area);
831
832 return err;
833}
834
835static int scsiback_map(struct vscsibk_info *info)
836{
837 struct xenbus_device *dev = info->dev;
838 unsigned int ring_ref, evtchn;
839 int err;
840
841 err = xenbus_gather(XBT_NIL, dev->otherend,
842 "ring-ref", "%u", &ring_ref,
843 "event-channel", "%u", &evtchn, NULL);
844 if (err) {
845 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
846 return err;
847 }
848
849 return scsiback_init_sring(info, ring_ref, evtchn);
850}
851
852/*
853 Add a new translation entry
854*/
855static int scsiback_add_translation_entry(struct vscsibk_info *info,
856 char *phy, struct ids_tuple *v)
857{
858 int err = 0;
859 struct v2p_entry *entry;
860 struct v2p_entry *new;
861 struct list_head *head = &(info->v2p_entry_lists);
862 unsigned long flags;
863 char *lunp;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200864 unsigned long long unpacked_lun;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700865 struct se_lun *se_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200866 struct scsiback_tpg *tpg_entry, *tpg = NULL;
867 char *error = "doesn't exist";
868
869 lunp = strrchr(phy, ':');
870 if (!lunp) {
Tao Chen78574872015-03-10 20:49:18 +0000871 pr_err("illegal format of physical device %s\n", phy);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200872 return -EINVAL;
873 }
874 *lunp = 0;
875 lunp++;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200876 err = kstrtoull(lunp, 10, &unpacked_lun);
877 if (err < 0) {
Tao Chen78574872015-03-10 20:49:18 +0000878 pr_err("lun number not valid: %s\n", lunp);
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200879 return err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200880 }
881
882 mutex_lock(&scsiback_mutex);
883 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
884 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
885 !strcmp(phy, tpg_entry->param_alias)) {
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700886 mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
887 hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
888 if (se_lun->unpacked_lun == unpacked_lun) {
889 if (!tpg_entry->tpg_nexus)
890 error = "nexus undefined";
891 else
892 tpg = tpg_entry;
893 break;
894 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200895 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700896 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200897 break;
898 }
899 }
900 if (tpg) {
901 mutex_lock(&tpg->tv_tpg_mutex);
902 tpg->tv_tpg_fe_count++;
903 mutex_unlock(&tpg->tv_tpg_mutex);
904 }
905 mutex_unlock(&scsiback_mutex);
906
907 if (!tpg) {
Nicholas Bellinger0fb1f142015-06-16 00:37:16 -0700908 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200909 return -ENODEV;
910 }
911
912 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
913 if (new == NULL) {
914 err = -ENOMEM;
915 goto out_free;
916 }
917
918 spin_lock_irqsave(&info->v2p_lock, flags);
919
920 /* Check double assignment to identical virtual ID */
921 list_for_each_entry(entry, head, l) {
922 if ((entry->v.chn == v->chn) &&
923 (entry->v.tgt == v->tgt) &&
924 (entry->v.lun == v->lun)) {
Tao Chen78574872015-03-10 20:49:18 +0000925 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200926 err = -EEXIST;
927 goto out;
928 }
929
930 }
931
932 /* Create a new translation entry and add to the list */
933 kref_init(&new->kref);
934 new->v = *v;
935 new->tpg = tpg;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700936 new->lun = unpacked_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200937 list_add_tail(&new->l, head);
938
939out:
940 spin_unlock_irqrestore(&info->v2p_lock, flags);
941
942out_free:
943 mutex_lock(&tpg->tv_tpg_mutex);
944 tpg->tv_tpg_fe_count--;
945 mutex_unlock(&tpg->tv_tpg_mutex);
946
947 if (err)
948 kfree(new);
949
950 return err;
951}
952
953static void __scsiback_del_translation_entry(struct v2p_entry *entry)
954{
955 list_del(&entry->l);
956 kref_put(&entry->kref, scsiback_free_translation_entry);
957}
958
959/*
960 Delete the translation entry specfied
961*/
962static int scsiback_del_translation_entry(struct vscsibk_info *info,
963 struct ids_tuple *v)
964{
965 struct v2p_entry *entry;
966 struct list_head *head = &(info->v2p_entry_lists);
967 unsigned long flags;
968
969 spin_lock_irqsave(&info->v2p_lock, flags);
970 /* Find out the translation entry specified */
971 list_for_each_entry(entry, head, l) {
972 if ((entry->v.chn == v->chn) &&
973 (entry->v.tgt == v->tgt) &&
974 (entry->v.lun == v->lun)) {
975 goto found;
976 }
977 }
978
979 spin_unlock_irqrestore(&info->v2p_lock, flags);
980 return 1;
981
982found:
983 /* Delete the translation entry specfied */
984 __scsiback_del_translation_entry(entry);
985
986 spin_unlock_irqrestore(&info->v2p_lock, flags);
987 return 0;
988}
989
990static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
Juergen Gross169e6cf2015-02-17 08:02:48 +0100991 char *phy, struct ids_tuple *vir, int try)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200992{
993 if (!scsiback_add_translation_entry(info, phy, vir)) {
994 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
995 "%d", XenbusStateInitialised)) {
Tao Chen78574872015-03-10 20:49:18 +0000996 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200997 scsiback_del_translation_entry(info, vir);
998 }
Juergen Gross169e6cf2015-02-17 08:02:48 +0100999 } else if (!try) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001000 xenbus_printf(XBT_NIL, info->dev->nodename, state,
1001 "%d", XenbusStateClosed);
1002 }
1003}
1004
1005static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1006 struct ids_tuple *vir)
1007{
1008 if (!scsiback_del_translation_entry(info, vir)) {
1009 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1010 "%d", XenbusStateClosed))
Tao Chen78574872015-03-10 20:49:18 +00001011 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001012 }
1013}
1014
1015#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1016#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1017
1018static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1019 char *ent)
1020{
1021 int err;
1022 struct ids_tuple vir;
1023 char *val;
1024 int device_state;
1025 char phy[VSCSI_NAMELEN];
1026 char str[64];
1027 char state[64];
1028 struct xenbus_device *dev = info->dev;
1029
1030 /* read status */
1031 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1032 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1033 if (XENBUS_EXIST_ERR(err))
1034 return;
1035
1036 /* physical SCSI device */
1037 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1038 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1039 if (IS_ERR(val)) {
1040 xenbus_printf(XBT_NIL, dev->nodename, state,
1041 "%d", XenbusStateClosed);
1042 return;
1043 }
1044 strlcpy(phy, val, VSCSI_NAMELEN);
1045 kfree(val);
1046
1047 /* virtual SCSI device */
1048 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1049 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1050 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1051 if (XENBUS_EXIST_ERR(err)) {
1052 xenbus_printf(XBT_NIL, dev->nodename, state,
1053 "%d", XenbusStateClosed);
1054 return;
1055 }
1056
1057 switch (op) {
1058 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
Juergen Gross169e6cf2015-02-17 08:02:48 +01001059 switch (device_state) {
1060 case XenbusStateInitialising:
1061 scsiback_do_add_lun(info, state, phy, &vir, 0);
1062 break;
1063 case XenbusStateConnected:
1064 scsiback_do_add_lun(info, state, phy, &vir, 1);
1065 break;
1066 case XenbusStateClosing:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001067 scsiback_do_del_lun(info, state, &vir);
Juergen Gross169e6cf2015-02-17 08:02:48 +01001068 break;
1069 default:
1070 break;
1071 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001072 break;
1073
1074 case VSCSIBACK_OP_UPDATEDEV_STATE:
1075 if (device_state == XenbusStateInitialised) {
1076 /* modify vscsi-devs/dev-x/state */
1077 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1078 "%d", XenbusStateConnected)) {
Tao Chen78574872015-03-10 20:49:18 +00001079 pr_err("xenbus_printf error %s\n", str);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001080 scsiback_del_translation_entry(info, &vir);
1081 xenbus_printf(XBT_NIL, dev->nodename, state,
1082 "%d", XenbusStateClosed);
1083 }
1084 }
1085 break;
Tao Chen78574872015-03-10 20:49:18 +00001086 /* When it is necessary, processing is added here. */
Juergen Grossd9d660f2014-08-28 06:44:12 +02001087 default:
1088 break;
1089 }
1090}
1091
1092static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1093{
1094 int i;
1095 char **dir;
1096 unsigned int ndir = 0;
1097
1098 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1099 &ndir);
1100 if (IS_ERR(dir))
1101 return;
1102
1103 for (i = 0; i < ndir; i++)
1104 scsiback_do_1lun_hotplug(info, op, dir[i]);
1105
1106 kfree(dir);
1107}
1108
1109static void scsiback_frontend_changed(struct xenbus_device *dev,
1110 enum xenbus_state frontend_state)
1111{
1112 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1113
1114 switch (frontend_state) {
1115 case XenbusStateInitialising:
1116 break;
1117
1118 case XenbusStateInitialised:
1119 if (scsiback_map(info))
1120 break;
1121
1122 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1123 xenbus_switch_state(dev, XenbusStateConnected);
1124 break;
1125
1126 case XenbusStateConnected:
1127 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1128
1129 if (dev->state == XenbusStateConnected)
1130 break;
1131
1132 xenbus_switch_state(dev, XenbusStateConnected);
1133 break;
1134
1135 case XenbusStateClosing:
1136 if (info->irq)
1137 scsiback_disconnect(info);
1138
1139 xenbus_switch_state(dev, XenbusStateClosing);
1140 break;
1141
1142 case XenbusStateClosed:
1143 xenbus_switch_state(dev, XenbusStateClosed);
1144 if (xenbus_dev_is_online(dev))
1145 break;
1146 /* fall through if not online */
1147 case XenbusStateUnknown:
1148 device_unregister(&dev->dev);
1149 break;
1150
1151 case XenbusStateReconfiguring:
1152 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1153 xenbus_switch_state(dev, XenbusStateReconfigured);
1154
1155 break;
1156
1157 default:
1158 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1159 frontend_state);
1160 break;
1161 }
1162}
1163
1164/*
1165 Release the translation entry specfied
1166*/
1167static void scsiback_release_translation_entry(struct vscsibk_info *info)
1168{
1169 struct v2p_entry *entry, *tmp;
1170 struct list_head *head = &(info->v2p_entry_lists);
1171 unsigned long flags;
1172
1173 spin_lock_irqsave(&info->v2p_lock, flags);
1174
1175 list_for_each_entry_safe(entry, tmp, head, l)
1176 __scsiback_del_translation_entry(entry);
1177
1178 spin_unlock_irqrestore(&info->v2p_lock, flags);
1179}
1180
1181static int scsiback_remove(struct xenbus_device *dev)
1182{
1183 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1184
1185 if (info->irq)
1186 scsiback_disconnect(info);
1187
1188 scsiback_release_translation_entry(info);
1189
1190 dev_set_drvdata(&dev->dev, NULL);
1191
1192 return 0;
1193}
1194
1195static int scsiback_probe(struct xenbus_device *dev,
1196 const struct xenbus_device_id *id)
1197{
1198 int err;
1199
1200 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1201 GFP_KERNEL);
1202
Tao Chen78574872015-03-10 20:49:18 +00001203 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001204
1205 if (!info) {
1206 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1207 return -ENOMEM;
1208 }
1209 info->dev = dev;
1210 dev_set_drvdata(&dev->dev, info);
1211
1212 info->domid = dev->otherend_id;
1213 spin_lock_init(&info->ring_lock);
1214 info->ring_error = 0;
1215 atomic_set(&info->nr_unreplied_reqs, 0);
1216 init_waitqueue_head(&info->waiting_to_free);
1217 info->dev = dev;
1218 info->irq = 0;
1219 INIT_LIST_HEAD(&info->v2p_entry_lists);
1220 spin_lock_init(&info->v2p_lock);
1221
1222 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1223 SG_ALL);
1224 if (err)
1225 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1226
1227 err = xenbus_switch_state(dev, XenbusStateInitWait);
1228 if (err)
1229 goto fail;
1230
1231 return 0;
1232
1233fail:
Tao Chen78574872015-03-10 20:49:18 +00001234 pr_warn("%s failed\n", __func__);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001235 scsiback_remove(dev);
1236
1237 return err;
1238}
1239
1240static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1241{
1242 switch (tport->tport_proto_id) {
1243 case SCSI_PROTOCOL_SAS:
1244 return "SAS";
1245 case SCSI_PROTOCOL_FCP:
1246 return "FCP";
1247 case SCSI_PROTOCOL_ISCSI:
1248 return "iSCSI";
1249 default:
1250 break;
1251 }
1252
1253 return "Unknown";
1254}
1255
Juergen Grossd9d660f2014-08-28 06:44:12 +02001256static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1257{
1258 struct scsiback_tpg *tpg = container_of(se_tpg,
1259 struct scsiback_tpg, se_tpg);
1260 struct scsiback_tport *tport = tpg->tport;
1261
1262 return &tport->tport_name[0];
1263}
1264
1265static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1266{
1267 struct scsiback_tpg *tpg = container_of(se_tpg,
1268 struct scsiback_tpg, se_tpg);
1269 return tpg->tport_tpgt;
1270}
1271
Juergen Grossd9d660f2014-08-28 06:44:12 +02001272static struct se_wwn *
1273scsiback_make_tport(struct target_fabric_configfs *tf,
1274 struct config_group *group,
1275 const char *name)
1276{
1277 struct scsiback_tport *tport;
1278 char *ptr;
1279 u64 wwpn = 0;
1280 int off = 0;
1281
1282 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1283 if (!tport)
1284 return ERR_PTR(-ENOMEM);
1285
1286 tport->tport_wwpn = wwpn;
1287 /*
1288 * Determine the emulated Protocol Identifier and Target Port Name
1289 * based on the incoming configfs directory name.
1290 */
1291 ptr = strstr(name, "naa.");
1292 if (ptr) {
1293 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1294 goto check_len;
1295 }
1296 ptr = strstr(name, "fc.");
1297 if (ptr) {
1298 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1299 off = 3; /* Skip over "fc." */
1300 goto check_len;
1301 }
1302 ptr = strstr(name, "iqn.");
1303 if (ptr) {
1304 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1305 goto check_len;
1306 }
1307
1308 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1309 kfree(tport);
1310 return ERR_PTR(-EINVAL);
1311
1312check_len:
1313 if (strlen(name) >= VSCSI_NAMELEN) {
1314 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1315 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1316 kfree(tport);
1317 return ERR_PTR(-EINVAL);
1318 }
1319 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1320
Tao Chen78574872015-03-10 20:49:18 +00001321 pr_debug("Allocated emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001322 scsiback_dump_proto_id(tport), name);
1323
1324 return &tport->tport_wwn;
1325}
1326
1327static void scsiback_drop_tport(struct se_wwn *wwn)
1328{
1329 struct scsiback_tport *tport = container_of(wwn,
1330 struct scsiback_tport, tport_wwn);
1331
Tao Chen78574872015-03-10 20:49:18 +00001332 pr_debug("Deallocating emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001333 scsiback_dump_proto_id(tport), tport->tport_name);
1334
1335 kfree(tport);
1336}
1337
Juergen Grossd9d660f2014-08-28 06:44:12 +02001338static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1339{
1340 return 1;
1341}
1342
1343static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1344{
1345 /*
Tao Chen78574872015-03-10 20:49:18 +00001346 * Do not release struct se_cmd's containing a valid TMR pointer.
1347 * These will be released directly in scsiback_device_action()
Juergen Grossd9d660f2014-08-28 06:44:12 +02001348 * with transport_generic_free_cmd().
1349 */
1350 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
1351 return 0;
1352
1353 transport_generic_free_cmd(se_cmd, 0);
1354 return 1;
1355}
1356
1357static void scsiback_release_cmd(struct se_cmd *se_cmd)
1358{
1359 struct vscsibk_pend *pending_req = container_of(se_cmd,
1360 struct vscsibk_pend, se_cmd);
1361
1362 kmem_cache_free(scsiback_cachep, pending_req);
1363}
1364
1365static int scsiback_shutdown_session(struct se_session *se_sess)
1366{
1367 return 0;
1368}
1369
1370static void scsiback_close_session(struct se_session *se_sess)
1371{
1372}
1373
1374static u32 scsiback_sess_get_index(struct se_session *se_sess)
1375{
1376 return 0;
1377}
1378
1379static int scsiback_write_pending(struct se_cmd *se_cmd)
1380{
1381 /* Go ahead and process the write immediately */
1382 target_execute_cmd(se_cmd);
1383
1384 return 0;
1385}
1386
1387static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1388{
1389 return 0;
1390}
1391
1392static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1393{
1394}
1395
Juergen Grossd9d660f2014-08-28 06:44:12 +02001396static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1397{
1398 return 0;
1399}
1400
1401static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1402{
1403 struct vscsibk_pend *pending_req = container_of(se_cmd,
1404 struct vscsibk_pend, se_cmd);
1405
1406 pending_req->result = SAM_STAT_GOOD;
1407 scsiback_cmd_done(pending_req);
1408 return 0;
1409}
1410
1411static int scsiback_queue_status(struct se_cmd *se_cmd)
1412{
1413 struct vscsibk_pend *pending_req = container_of(se_cmd,
1414 struct vscsibk_pend, se_cmd);
1415
1416 if (se_cmd->sense_buffer &&
1417 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1418 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1419 pending_req->result = (DRIVER_SENSE << 24) |
1420 SAM_STAT_CHECK_CONDITION;
1421 else
1422 pending_req->result = se_cmd->scsi_status;
1423
1424 scsiback_cmd_done(pending_req);
1425 return 0;
1426}
1427
1428static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1429{
1430 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1431 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1432
1433 atomic_set(&tmr->tmr_complete, 1);
1434 wake_up(&tmr->tmr_wait);
1435}
1436
1437static void scsiback_aborted_task(struct se_cmd *se_cmd)
1438{
1439}
1440
1441static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg,
1442 char *page)
1443{
1444 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1445 se_tpg);
1446 ssize_t rb;
1447
1448 mutex_lock(&tpg->tv_tpg_mutex);
1449 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1450 mutex_unlock(&tpg->tv_tpg_mutex);
1451
1452 return rb;
1453}
1454
1455static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
1456 const char *page, size_t count)
1457{
1458 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1459 se_tpg);
1460 int len;
1461
1462 if (strlen(page) >= VSCSI_NAMELEN) {
1463 pr_err("param alias: %s, exceeds max: %d\n", page,
1464 VSCSI_NAMELEN);
1465 return -EINVAL;
1466 }
1467
1468 mutex_lock(&tpg->tv_tpg_mutex);
1469 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1470 if (tpg->param_alias[len - 1] == '\n')
1471 tpg->param_alias[len - 1] = '\0';
1472 mutex_unlock(&tpg->tv_tpg_mutex);
1473
1474 return count;
1475}
1476
1477TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
1478
1479static struct configfs_attribute *scsiback_param_attrs[] = {
1480 &scsiback_tpg_param_alias.attr,
1481 NULL,
1482};
1483
1484static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1485 const char *name)
1486{
1487 struct se_portal_group *se_tpg;
1488 struct se_session *se_sess;
1489 struct scsiback_nexus *tv_nexus;
1490
1491 mutex_lock(&tpg->tv_tpg_mutex);
1492 if (tpg->tpg_nexus) {
1493 mutex_unlock(&tpg->tv_tpg_mutex);
1494 pr_debug("tpg->tpg_nexus already exists\n");
1495 return -EEXIST;
1496 }
1497 se_tpg = &tpg->se_tpg;
1498
1499 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1500 if (!tv_nexus) {
1501 mutex_unlock(&tpg->tv_tpg_mutex);
1502 return -ENOMEM;
1503 }
1504 /*
Tao Chen78574872015-03-10 20:49:18 +00001505 * Initialize the struct se_session pointer
Juergen Grossd9d660f2014-08-28 06:44:12 +02001506 */
1507 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1508 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1509 mutex_unlock(&tpg->tv_tpg_mutex);
1510 kfree(tv_nexus);
1511 return -ENOMEM;
1512 }
1513 se_sess = tv_nexus->tvn_se_sess;
1514 /*
1515 * Since we are running in 'demo mode' this call with generate a
1516 * struct se_node_acl for the scsiback struct se_portal_group with
1517 * the SCSI Initiator port name of the passed configfs group 'name'.
1518 */
1519 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1520 se_tpg, (unsigned char *)name);
1521 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1522 mutex_unlock(&tpg->tv_tpg_mutex);
1523 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1524 name);
1525 goto out;
1526 }
Bart Van Assche2f450cc2015-02-12 11:48:49 +01001527 /* Now register the TCM pvscsi virtual I_T Nexus as active. */
1528 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001529 tv_nexus->tvn_se_sess, tv_nexus);
1530 tpg->tpg_nexus = tv_nexus;
1531
1532 mutex_unlock(&tpg->tv_tpg_mutex);
1533 return 0;
1534
1535out:
1536 transport_free_session(se_sess);
1537 kfree(tv_nexus);
1538 return -ENOMEM;
1539}
1540
1541static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1542{
1543 struct se_session *se_sess;
1544 struct scsiback_nexus *tv_nexus;
1545
1546 mutex_lock(&tpg->tv_tpg_mutex);
1547 tv_nexus = tpg->tpg_nexus;
1548 if (!tv_nexus) {
1549 mutex_unlock(&tpg->tv_tpg_mutex);
1550 return -ENODEV;
1551 }
1552
1553 se_sess = tv_nexus->tvn_se_sess;
1554 if (!se_sess) {
1555 mutex_unlock(&tpg->tv_tpg_mutex);
1556 return -ENODEV;
1557 }
1558
1559 if (tpg->tv_tpg_port_count != 0) {
1560 mutex_unlock(&tpg->tv_tpg_mutex);
1561 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1562 tpg->tv_tpg_port_count);
1563 return -EBUSY;
1564 }
1565
1566 if (tpg->tv_tpg_fe_count != 0) {
1567 mutex_unlock(&tpg->tv_tpg_mutex);
1568 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1569 tpg->tv_tpg_fe_count);
1570 return -EBUSY;
1571 }
1572
Tao Chen78574872015-03-10 20:49:18 +00001573 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001574 scsiback_dump_proto_id(tpg->tport),
1575 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1576
1577 /*
1578 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1579 */
1580 transport_deregister_session(tv_nexus->tvn_se_sess);
1581 tpg->tpg_nexus = NULL;
1582 mutex_unlock(&tpg->tv_tpg_mutex);
1583
1584 kfree(tv_nexus);
1585 return 0;
1586}
1587
1588static ssize_t scsiback_tpg_show_nexus(struct se_portal_group *se_tpg,
1589 char *page)
1590{
1591 struct scsiback_tpg *tpg = container_of(se_tpg,
1592 struct scsiback_tpg, se_tpg);
1593 struct scsiback_nexus *tv_nexus;
1594 ssize_t ret;
1595
1596 mutex_lock(&tpg->tv_tpg_mutex);
1597 tv_nexus = tpg->tpg_nexus;
1598 if (!tv_nexus) {
1599 mutex_unlock(&tpg->tv_tpg_mutex);
1600 return -ENODEV;
1601 }
1602 ret = snprintf(page, PAGE_SIZE, "%s\n",
1603 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1604 mutex_unlock(&tpg->tv_tpg_mutex);
1605
1606 return ret;
1607}
1608
1609static ssize_t scsiback_tpg_store_nexus(struct se_portal_group *se_tpg,
1610 const char *page,
1611 size_t count)
1612{
1613 struct scsiback_tpg *tpg = container_of(se_tpg,
1614 struct scsiback_tpg, se_tpg);
1615 struct scsiback_tport *tport_wwn = tpg->tport;
1616 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1617 int ret;
1618 /*
Tao Chen78574872015-03-10 20:49:18 +00001619 * Shutdown the active I_T nexus if 'NULL' is passed.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001620 */
1621 if (!strncmp(page, "NULL", 4)) {
1622 ret = scsiback_drop_nexus(tpg);
1623 return (!ret) ? count : ret;
1624 }
1625 /*
1626 * Otherwise make sure the passed virtual Initiator port WWN matches
1627 * the fabric protocol_id set in scsiback_make_tport(), and call
1628 * scsiback_make_nexus().
1629 */
1630 if (strlen(page) >= VSCSI_NAMELEN) {
1631 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1632 page, VSCSI_NAMELEN);
1633 return -EINVAL;
1634 }
1635 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1636
1637 ptr = strstr(i_port, "naa.");
1638 if (ptr) {
1639 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1640 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1641 i_port, scsiback_dump_proto_id(tport_wwn));
1642 return -EINVAL;
1643 }
1644 port_ptr = &i_port[0];
1645 goto check_newline;
1646 }
1647 ptr = strstr(i_port, "fc.");
1648 if (ptr) {
1649 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1650 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1651 i_port, scsiback_dump_proto_id(tport_wwn));
1652 return -EINVAL;
1653 }
1654 port_ptr = &i_port[3]; /* Skip over "fc." */
1655 goto check_newline;
1656 }
1657 ptr = strstr(i_port, "iqn.");
1658 if (ptr) {
1659 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1660 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1661 i_port, scsiback_dump_proto_id(tport_wwn));
1662 return -EINVAL;
1663 }
1664 port_ptr = &i_port[0];
1665 goto check_newline;
1666 }
1667 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1668 i_port);
1669 return -EINVAL;
1670 /*
1671 * Clear any trailing newline for the NAA WWN
1672 */
1673check_newline:
1674 if (i_port[strlen(i_port) - 1] == '\n')
1675 i_port[strlen(i_port) - 1] = '\0';
1676
1677 ret = scsiback_make_nexus(tpg, port_ptr);
1678 if (ret < 0)
1679 return ret;
1680
1681 return count;
1682}
1683
1684TF_TPG_BASE_ATTR(scsiback, nexus, S_IRUGO | S_IWUSR);
1685
1686static struct configfs_attribute *scsiback_tpg_attrs[] = {
1687 &scsiback_tpg_nexus.attr,
1688 NULL,
1689};
1690
1691static ssize_t
1692scsiback_wwn_show_attr_version(struct target_fabric_configfs *tf,
1693 char *page)
1694{
1695 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1696 UTS_RELEASE"\n",
1697 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1698}
1699
1700TF_WWN_ATTR_RO(scsiback, version);
1701
1702static struct configfs_attribute *scsiback_wwn_attrs[] = {
1703 &scsiback_wwn_version.attr,
1704 NULL,
1705};
1706
1707static char *scsiback_get_fabric_name(void)
1708{
1709 return "xen-pvscsi";
1710}
1711
1712static int scsiback_port_link(struct se_portal_group *se_tpg,
1713 struct se_lun *lun)
1714{
1715 struct scsiback_tpg *tpg = container_of(se_tpg,
1716 struct scsiback_tpg, se_tpg);
1717
1718 mutex_lock(&tpg->tv_tpg_mutex);
1719 tpg->tv_tpg_port_count++;
1720 mutex_unlock(&tpg->tv_tpg_mutex);
1721
1722 return 0;
1723}
1724
1725static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1726 struct se_lun *lun)
1727{
1728 struct scsiback_tpg *tpg = container_of(se_tpg,
1729 struct scsiback_tpg, se_tpg);
1730
1731 mutex_lock(&tpg->tv_tpg_mutex);
1732 tpg->tv_tpg_port_count--;
1733 mutex_unlock(&tpg->tv_tpg_mutex);
1734}
1735
1736static struct se_portal_group *
1737scsiback_make_tpg(struct se_wwn *wwn,
1738 struct config_group *group,
1739 const char *name)
1740{
1741 struct scsiback_tport *tport = container_of(wwn,
1742 struct scsiback_tport, tport_wwn);
1743
1744 struct scsiback_tpg *tpg;
Dan Carpenter495daef2014-09-08 14:17:35 +03001745 u16 tpgt;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001746 int ret;
1747
1748 if (strstr(name, "tpgt_") != name)
1749 return ERR_PTR(-EINVAL);
Dan Carpenter495daef2014-09-08 14:17:35 +03001750 ret = kstrtou16(name + 5, 10, &tpgt);
1751 if (ret)
1752 return ERR_PTR(ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001753
1754 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1755 if (!tpg)
1756 return ERR_PTR(-ENOMEM);
1757
1758 mutex_init(&tpg->tv_tpg_mutex);
1759 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1760 INIT_LIST_HEAD(&tpg->info_list);
1761 tpg->tport = tport;
1762 tpg->tport_tpgt = tpgt;
1763
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07001764 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001765 if (ret < 0) {
1766 kfree(tpg);
1767 return NULL;
1768 }
1769 mutex_lock(&scsiback_mutex);
1770 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1771 mutex_unlock(&scsiback_mutex);
1772
1773 return &tpg->se_tpg;
1774}
1775
1776static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1777{
1778 struct scsiback_tpg *tpg = container_of(se_tpg,
1779 struct scsiback_tpg, se_tpg);
1780
1781 mutex_lock(&scsiback_mutex);
1782 list_del(&tpg->tv_tpg_list);
1783 mutex_unlock(&scsiback_mutex);
1784 /*
1785 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1786 */
1787 scsiback_drop_nexus(tpg);
1788 /*
Tao Chen78574872015-03-10 20:49:18 +00001789 * Deregister the se_tpg from TCM.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001790 */
1791 core_tpg_deregister(se_tpg);
1792 kfree(tpg);
1793}
1794
1795static int scsiback_check_true(struct se_portal_group *se_tpg)
1796{
1797 return 1;
1798}
1799
1800static int scsiback_check_false(struct se_portal_group *se_tpg)
1801{
1802 return 0;
1803}
1804
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001805static const struct target_core_fabric_ops scsiback_ops = {
1806 .module = THIS_MODULE,
1807 .name = "xen-pvscsi",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001808 .get_fabric_name = scsiback_get_fabric_name,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001809 .tpg_get_wwn = scsiback_get_fabric_wwn,
1810 .tpg_get_tag = scsiback_get_tag,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001811 .tpg_check_demo_mode = scsiback_check_true,
1812 .tpg_check_demo_mode_cache = scsiback_check_true,
1813 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1814 .tpg_check_prod_mode_write_protect = scsiback_check_false,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001815 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1816 .check_stop_free = scsiback_check_stop_free,
1817 .release_cmd = scsiback_release_cmd,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001818 .shutdown_session = scsiback_shutdown_session,
1819 .close_session = scsiback_close_session,
1820 .sess_get_index = scsiback_sess_get_index,
1821 .sess_get_initiator_sid = NULL,
1822 .write_pending = scsiback_write_pending,
1823 .write_pending_status = scsiback_write_pending_status,
1824 .set_default_node_attributes = scsiback_set_default_node_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001825 .get_cmd_state = scsiback_get_cmd_state,
1826 .queue_data_in = scsiback_queue_data_in,
1827 .queue_status = scsiback_queue_status,
1828 .queue_tm_rsp = scsiback_queue_tm_rsp,
1829 .aborted_task = scsiback_aborted_task,
1830 /*
1831 * Setup callers for generic logic in target_core_fabric_configfs.c
1832 */
1833 .fabric_make_wwn = scsiback_make_tport,
1834 .fabric_drop_wwn = scsiback_drop_tport,
1835 .fabric_make_tpg = scsiback_make_tpg,
1836 .fabric_drop_tpg = scsiback_drop_tpg,
1837 .fabric_post_link = scsiback_port_link,
1838 .fabric_pre_unlink = scsiback_port_unlink,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001839
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001840 .tfc_wwn_attrs = scsiback_wwn_attrs,
1841 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1842 .tfc_tpg_param_attrs = scsiback_param_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001843};
1844
1845static const struct xenbus_device_id scsiback_ids[] = {
1846 { "vscsi" },
1847 { "" }
1848};
1849
David Vrabel95afae42014-09-08 17:30:41 +01001850static struct xenbus_driver scsiback_driver = {
1851 .ids = scsiback_ids,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001852 .probe = scsiback_probe,
1853 .remove = scsiback_remove,
1854 .otherend_changed = scsiback_frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001855};
Juergen Grossd9d660f2014-08-28 06:44:12 +02001856
1857static void scsiback_init_pend(void *p)
1858{
1859 struct vscsibk_pend *pend = p;
1860 int i;
1861
1862 memset(pend, 0, sizeof(*pend));
1863 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
1864 pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
1865}
1866
1867static int __init scsiback_init(void)
1868{
1869 int ret;
1870
1871 if (!xen_domain())
1872 return -ENODEV;
1873
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001874 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1875 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1876
Juergen Grossd9d660f2014-08-28 06:44:12 +02001877 scsiback_cachep = kmem_cache_create("vscsiif_cache",
1878 sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend);
1879 if (!scsiback_cachep)
1880 return -ENOMEM;
1881
1882 ret = xenbus_register_backend(&scsiback_driver);
1883 if (ret)
1884 goto out_cache_destroy;
1885
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001886 ret = target_register_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001887 if (ret)
1888 goto out_unregister_xenbus;
1889
1890 return 0;
1891
1892out_unregister_xenbus:
1893 xenbus_unregister_driver(&scsiback_driver);
1894out_cache_destroy:
1895 kmem_cache_destroy(scsiback_cachep);
Tao Chen78574872015-03-10 20:49:18 +00001896 pr_err("%s: error %d\n", __func__, ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001897 return ret;
1898}
1899
1900static void __exit scsiback_exit(void)
1901{
1902 struct page *page;
1903
1904 while (free_pages_num) {
1905 if (get_free_page(&page))
1906 BUG();
David Vrabelff4b1562015-01-08 18:06:01 +00001907 gnttab_free_pages(1, &page);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001908 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001909 target_unregister_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001910 xenbus_unregister_driver(&scsiback_driver);
1911 kmem_cache_destroy(scsiback_cachep);
1912}
1913
1914module_init(scsiback_init);
1915module_exit(scsiback_exit);
1916
1917MODULE_DESCRIPTION("Xen SCSI backend driver");
1918MODULE_LICENSE("Dual BSD/GPL");
1919MODULE_ALIAS("xen-backend:vscsi");
1920MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");