blob: 823bdf4cc36e37db73caf6c1479bfedefb0f1eb2 [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;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200867 unsigned long long unpacked_lun;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700868 struct se_lun *se_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200869 struct scsiback_tpg *tpg_entry, *tpg = NULL;
870 char *error = "doesn't exist";
871
872 lunp = strrchr(phy, ':');
873 if (!lunp) {
Tao Chen78574872015-03-10 20:49:18 +0000874 pr_err("illegal format of physical device %s\n", phy);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200875 return -EINVAL;
876 }
877 *lunp = 0;
878 lunp++;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200879 err = kstrtoull(lunp, 10, &unpacked_lun);
880 if (err < 0) {
Tao Chen78574872015-03-10 20:49:18 +0000881 pr_err("lun number not valid: %s\n", lunp);
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200882 return err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200883 }
884
885 mutex_lock(&scsiback_mutex);
886 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
887 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
888 !strcmp(phy, tpg_entry->param_alias)) {
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700889 mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
890 hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
891 if (se_lun->unpacked_lun == unpacked_lun) {
892 if (!tpg_entry->tpg_nexus)
893 error = "nexus undefined";
894 else
895 tpg = tpg_entry;
896 break;
897 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200898 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700899 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200900 break;
901 }
902 }
903 if (tpg) {
904 mutex_lock(&tpg->tv_tpg_mutex);
905 tpg->tv_tpg_fe_count++;
906 mutex_unlock(&tpg->tv_tpg_mutex);
907 }
908 mutex_unlock(&scsiback_mutex);
909
910 if (!tpg) {
Nicholas Bellinger0fb1f142015-06-16 00:37:16 -0700911 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200912 return -ENODEV;
913 }
914
915 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
916 if (new == NULL) {
917 err = -ENOMEM;
918 goto out_free;
919 }
920
921 spin_lock_irqsave(&info->v2p_lock, flags);
922
923 /* Check double assignment to identical virtual ID */
924 list_for_each_entry(entry, head, l) {
925 if ((entry->v.chn == v->chn) &&
926 (entry->v.tgt == v->tgt) &&
927 (entry->v.lun == v->lun)) {
Tao Chen78574872015-03-10 20:49:18 +0000928 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200929 err = -EEXIST;
930 goto out;
931 }
932
933 }
934
935 /* Create a new translation entry and add to the list */
936 kref_init(&new->kref);
937 new->v = *v;
938 new->tpg = tpg;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700939 new->lun = unpacked_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200940 list_add_tail(&new->l, head);
941
942out:
943 spin_unlock_irqrestore(&info->v2p_lock, flags);
944
945out_free:
946 mutex_lock(&tpg->tv_tpg_mutex);
947 tpg->tv_tpg_fe_count--;
948 mutex_unlock(&tpg->tv_tpg_mutex);
949
950 if (err)
951 kfree(new);
952
953 return err;
954}
955
956static void __scsiback_del_translation_entry(struct v2p_entry *entry)
957{
958 list_del(&entry->l);
959 kref_put(&entry->kref, scsiback_free_translation_entry);
960}
961
962/*
963 Delete the translation entry specfied
964*/
965static int scsiback_del_translation_entry(struct vscsibk_info *info,
966 struct ids_tuple *v)
967{
968 struct v2p_entry *entry;
969 struct list_head *head = &(info->v2p_entry_lists);
970 unsigned long flags;
971
972 spin_lock_irqsave(&info->v2p_lock, flags);
973 /* Find out the translation entry specified */
974 list_for_each_entry(entry, head, l) {
975 if ((entry->v.chn == v->chn) &&
976 (entry->v.tgt == v->tgt) &&
977 (entry->v.lun == v->lun)) {
978 goto found;
979 }
980 }
981
982 spin_unlock_irqrestore(&info->v2p_lock, flags);
983 return 1;
984
985found:
986 /* Delete the translation entry specfied */
987 __scsiback_del_translation_entry(entry);
988
989 spin_unlock_irqrestore(&info->v2p_lock, flags);
990 return 0;
991}
992
993static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
Juergen Gross169e6cf2015-02-17 08:02:48 +0100994 char *phy, struct ids_tuple *vir, int try)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200995{
996 if (!scsiback_add_translation_entry(info, phy, vir)) {
997 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
998 "%d", XenbusStateInitialised)) {
Tao Chen78574872015-03-10 20:49:18 +0000999 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001000 scsiback_del_translation_entry(info, vir);
1001 }
Juergen Gross169e6cf2015-02-17 08:02:48 +01001002 } else if (!try) {
Juergen Grossd9d660f2014-08-28 06:44:12 +02001003 xenbus_printf(XBT_NIL, info->dev->nodename, state,
1004 "%d", XenbusStateClosed);
1005 }
1006}
1007
1008static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1009 struct ids_tuple *vir)
1010{
1011 if (!scsiback_del_translation_entry(info, vir)) {
1012 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1013 "%d", XenbusStateClosed))
Tao Chen78574872015-03-10 20:49:18 +00001014 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001015 }
1016}
1017
1018#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1019#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1020
1021static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1022 char *ent)
1023{
1024 int err;
1025 struct ids_tuple vir;
1026 char *val;
1027 int device_state;
1028 char phy[VSCSI_NAMELEN];
1029 char str[64];
1030 char state[64];
1031 struct xenbus_device *dev = info->dev;
1032
1033 /* read status */
1034 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1035 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1036 if (XENBUS_EXIST_ERR(err))
1037 return;
1038
1039 /* physical SCSI device */
1040 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1041 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1042 if (IS_ERR(val)) {
1043 xenbus_printf(XBT_NIL, dev->nodename, state,
1044 "%d", XenbusStateClosed);
1045 return;
1046 }
1047 strlcpy(phy, val, VSCSI_NAMELEN);
1048 kfree(val);
1049
1050 /* virtual SCSI device */
1051 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1052 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1053 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1054 if (XENBUS_EXIST_ERR(err)) {
1055 xenbus_printf(XBT_NIL, dev->nodename, state,
1056 "%d", XenbusStateClosed);
1057 return;
1058 }
1059
1060 switch (op) {
1061 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
Juergen Gross169e6cf2015-02-17 08:02:48 +01001062 switch (device_state) {
1063 case XenbusStateInitialising:
1064 scsiback_do_add_lun(info, state, phy, &vir, 0);
1065 break;
1066 case XenbusStateConnected:
1067 scsiback_do_add_lun(info, state, phy, &vir, 1);
1068 break;
1069 case XenbusStateClosing:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001070 scsiback_do_del_lun(info, state, &vir);
Juergen Gross169e6cf2015-02-17 08:02:48 +01001071 break;
1072 default:
1073 break;
1074 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001075 break;
1076
1077 case VSCSIBACK_OP_UPDATEDEV_STATE:
1078 if (device_state == XenbusStateInitialised) {
1079 /* modify vscsi-devs/dev-x/state */
1080 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1081 "%d", XenbusStateConnected)) {
Tao Chen78574872015-03-10 20:49:18 +00001082 pr_err("xenbus_printf error %s\n", str);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001083 scsiback_del_translation_entry(info, &vir);
1084 xenbus_printf(XBT_NIL, dev->nodename, state,
1085 "%d", XenbusStateClosed);
1086 }
1087 }
1088 break;
Tao Chen78574872015-03-10 20:49:18 +00001089 /* When it is necessary, processing is added here. */
Juergen Grossd9d660f2014-08-28 06:44:12 +02001090 default:
1091 break;
1092 }
1093}
1094
1095static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1096{
1097 int i;
1098 char **dir;
1099 unsigned int ndir = 0;
1100
1101 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1102 &ndir);
1103 if (IS_ERR(dir))
1104 return;
1105
1106 for (i = 0; i < ndir; i++)
1107 scsiback_do_1lun_hotplug(info, op, dir[i]);
1108
1109 kfree(dir);
1110}
1111
1112static void scsiback_frontend_changed(struct xenbus_device *dev,
1113 enum xenbus_state frontend_state)
1114{
1115 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1116
1117 switch (frontend_state) {
1118 case XenbusStateInitialising:
1119 break;
1120
1121 case XenbusStateInitialised:
1122 if (scsiback_map(info))
1123 break;
1124
1125 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1126 xenbus_switch_state(dev, XenbusStateConnected);
1127 break;
1128
1129 case XenbusStateConnected:
1130 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1131
1132 if (dev->state == XenbusStateConnected)
1133 break;
1134
1135 xenbus_switch_state(dev, XenbusStateConnected);
1136 break;
1137
1138 case XenbusStateClosing:
1139 if (info->irq)
1140 scsiback_disconnect(info);
1141
1142 xenbus_switch_state(dev, XenbusStateClosing);
1143 break;
1144
1145 case XenbusStateClosed:
1146 xenbus_switch_state(dev, XenbusStateClosed);
1147 if (xenbus_dev_is_online(dev))
1148 break;
1149 /* fall through if not online */
1150 case XenbusStateUnknown:
1151 device_unregister(&dev->dev);
1152 break;
1153
1154 case XenbusStateReconfiguring:
1155 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1156 xenbus_switch_state(dev, XenbusStateReconfigured);
1157
1158 break;
1159
1160 default:
1161 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1162 frontend_state);
1163 break;
1164 }
1165}
1166
1167/*
1168 Release the translation entry specfied
1169*/
1170static void scsiback_release_translation_entry(struct vscsibk_info *info)
1171{
1172 struct v2p_entry *entry, *tmp;
1173 struct list_head *head = &(info->v2p_entry_lists);
1174 unsigned long flags;
1175
1176 spin_lock_irqsave(&info->v2p_lock, flags);
1177
1178 list_for_each_entry_safe(entry, tmp, head, l)
1179 __scsiback_del_translation_entry(entry);
1180
1181 spin_unlock_irqrestore(&info->v2p_lock, flags);
1182}
1183
1184static int scsiback_remove(struct xenbus_device *dev)
1185{
1186 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1187
1188 if (info->irq)
1189 scsiback_disconnect(info);
1190
1191 scsiback_release_translation_entry(info);
1192
1193 dev_set_drvdata(&dev->dev, NULL);
1194
1195 return 0;
1196}
1197
1198static int scsiback_probe(struct xenbus_device *dev,
1199 const struct xenbus_device_id *id)
1200{
1201 int err;
1202
1203 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1204 GFP_KERNEL);
1205
Tao Chen78574872015-03-10 20:49:18 +00001206 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001207
1208 if (!info) {
1209 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1210 return -ENOMEM;
1211 }
1212 info->dev = dev;
1213 dev_set_drvdata(&dev->dev, info);
1214
1215 info->domid = dev->otherend_id;
1216 spin_lock_init(&info->ring_lock);
1217 info->ring_error = 0;
1218 atomic_set(&info->nr_unreplied_reqs, 0);
1219 init_waitqueue_head(&info->waiting_to_free);
1220 info->dev = dev;
1221 info->irq = 0;
1222 INIT_LIST_HEAD(&info->v2p_entry_lists);
1223 spin_lock_init(&info->v2p_lock);
1224
1225 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1226 SG_ALL);
1227 if (err)
1228 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1229
1230 err = xenbus_switch_state(dev, XenbusStateInitWait);
1231 if (err)
1232 goto fail;
1233
1234 return 0;
1235
1236fail:
Tao Chen78574872015-03-10 20:49:18 +00001237 pr_warn("%s failed\n", __func__);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001238 scsiback_remove(dev);
1239
1240 return err;
1241}
1242
1243static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1244{
1245 switch (tport->tport_proto_id) {
1246 case SCSI_PROTOCOL_SAS:
1247 return "SAS";
1248 case SCSI_PROTOCOL_FCP:
1249 return "FCP";
1250 case SCSI_PROTOCOL_ISCSI:
1251 return "iSCSI";
1252 default:
1253 break;
1254 }
1255
1256 return "Unknown";
1257}
1258
Juergen Grossd9d660f2014-08-28 06:44:12 +02001259static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1260{
1261 struct scsiback_tpg *tpg = container_of(se_tpg,
1262 struct scsiback_tpg, se_tpg);
1263 struct scsiback_tport *tport = tpg->tport;
1264
1265 return &tport->tport_name[0];
1266}
1267
1268static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1269{
1270 struct scsiback_tpg *tpg = container_of(se_tpg,
1271 struct scsiback_tpg, se_tpg);
1272 return tpg->tport_tpgt;
1273}
1274
Juergen Grossd9d660f2014-08-28 06:44:12 +02001275static struct se_wwn *
1276scsiback_make_tport(struct target_fabric_configfs *tf,
1277 struct config_group *group,
1278 const char *name)
1279{
1280 struct scsiback_tport *tport;
1281 char *ptr;
1282 u64 wwpn = 0;
1283 int off = 0;
1284
1285 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1286 if (!tport)
1287 return ERR_PTR(-ENOMEM);
1288
1289 tport->tport_wwpn = wwpn;
1290 /*
1291 * Determine the emulated Protocol Identifier and Target Port Name
1292 * based on the incoming configfs directory name.
1293 */
1294 ptr = strstr(name, "naa.");
1295 if (ptr) {
1296 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1297 goto check_len;
1298 }
1299 ptr = strstr(name, "fc.");
1300 if (ptr) {
1301 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1302 off = 3; /* Skip over "fc." */
1303 goto check_len;
1304 }
1305 ptr = strstr(name, "iqn.");
1306 if (ptr) {
1307 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1308 goto check_len;
1309 }
1310
1311 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1312 kfree(tport);
1313 return ERR_PTR(-EINVAL);
1314
1315check_len:
1316 if (strlen(name) >= VSCSI_NAMELEN) {
1317 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1318 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1319 kfree(tport);
1320 return ERR_PTR(-EINVAL);
1321 }
1322 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1323
Tao Chen78574872015-03-10 20:49:18 +00001324 pr_debug("Allocated emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001325 scsiback_dump_proto_id(tport), name);
1326
1327 return &tport->tport_wwn;
1328}
1329
1330static void scsiback_drop_tport(struct se_wwn *wwn)
1331{
1332 struct scsiback_tport *tport = container_of(wwn,
1333 struct scsiback_tport, tport_wwn);
1334
Tao Chen78574872015-03-10 20:49:18 +00001335 pr_debug("Deallocating emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001336 scsiback_dump_proto_id(tport), tport->tport_name);
1337
1338 kfree(tport);
1339}
1340
Juergen Grossd9d660f2014-08-28 06:44:12 +02001341static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1342{
1343 return 1;
1344}
1345
1346static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1347{
1348 /*
Tao Chen78574872015-03-10 20:49:18 +00001349 * Do not release struct se_cmd's containing a valid TMR pointer.
1350 * These will be released directly in scsiback_device_action()
Juergen Grossd9d660f2014-08-28 06:44:12 +02001351 * with transport_generic_free_cmd().
1352 */
1353 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
1354 return 0;
1355
1356 transport_generic_free_cmd(se_cmd, 0);
1357 return 1;
1358}
1359
1360static void scsiback_release_cmd(struct se_cmd *se_cmd)
1361{
1362 struct vscsibk_pend *pending_req = container_of(se_cmd,
1363 struct vscsibk_pend, se_cmd);
1364
1365 kmem_cache_free(scsiback_cachep, pending_req);
1366}
1367
1368static int scsiback_shutdown_session(struct se_session *se_sess)
1369{
1370 return 0;
1371}
1372
1373static void scsiback_close_session(struct se_session *se_sess)
1374{
1375}
1376
1377static u32 scsiback_sess_get_index(struct se_session *se_sess)
1378{
1379 return 0;
1380}
1381
1382static int scsiback_write_pending(struct se_cmd *se_cmd)
1383{
1384 /* Go ahead and process the write immediately */
1385 target_execute_cmd(se_cmd);
1386
1387 return 0;
1388}
1389
1390static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1391{
1392 return 0;
1393}
1394
1395static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1396{
1397}
1398
Juergen Grossd9d660f2014-08-28 06:44:12 +02001399static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1400{
1401 return 0;
1402}
1403
1404static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1405{
1406 struct vscsibk_pend *pending_req = container_of(se_cmd,
1407 struct vscsibk_pend, se_cmd);
1408
1409 pending_req->result = SAM_STAT_GOOD;
1410 scsiback_cmd_done(pending_req);
1411 return 0;
1412}
1413
1414static int scsiback_queue_status(struct se_cmd *se_cmd)
1415{
1416 struct vscsibk_pend *pending_req = container_of(se_cmd,
1417 struct vscsibk_pend, se_cmd);
1418
1419 if (se_cmd->sense_buffer &&
1420 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1421 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1422 pending_req->result = (DRIVER_SENSE << 24) |
1423 SAM_STAT_CHECK_CONDITION;
1424 else
1425 pending_req->result = se_cmd->scsi_status;
1426
1427 scsiback_cmd_done(pending_req);
1428 return 0;
1429}
1430
1431static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1432{
1433 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1434 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1435
1436 atomic_set(&tmr->tmr_complete, 1);
1437 wake_up(&tmr->tmr_wait);
1438}
1439
1440static void scsiback_aborted_task(struct se_cmd *se_cmd)
1441{
1442}
1443
1444static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg,
1445 char *page)
1446{
1447 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1448 se_tpg);
1449 ssize_t rb;
1450
1451 mutex_lock(&tpg->tv_tpg_mutex);
1452 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1453 mutex_unlock(&tpg->tv_tpg_mutex);
1454
1455 return rb;
1456}
1457
1458static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
1459 const char *page, size_t count)
1460{
1461 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1462 se_tpg);
1463 int len;
1464
1465 if (strlen(page) >= VSCSI_NAMELEN) {
1466 pr_err("param alias: %s, exceeds max: %d\n", page,
1467 VSCSI_NAMELEN);
1468 return -EINVAL;
1469 }
1470
1471 mutex_lock(&tpg->tv_tpg_mutex);
1472 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1473 if (tpg->param_alias[len - 1] == '\n')
1474 tpg->param_alias[len - 1] = '\0';
1475 mutex_unlock(&tpg->tv_tpg_mutex);
1476
1477 return count;
1478}
1479
1480TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
1481
1482static struct configfs_attribute *scsiback_param_attrs[] = {
1483 &scsiback_tpg_param_alias.attr,
1484 NULL,
1485};
1486
1487static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1488 const char *name)
1489{
1490 struct se_portal_group *se_tpg;
1491 struct se_session *se_sess;
1492 struct scsiback_nexus *tv_nexus;
1493
1494 mutex_lock(&tpg->tv_tpg_mutex);
1495 if (tpg->tpg_nexus) {
1496 mutex_unlock(&tpg->tv_tpg_mutex);
1497 pr_debug("tpg->tpg_nexus already exists\n");
1498 return -EEXIST;
1499 }
1500 se_tpg = &tpg->se_tpg;
1501
1502 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1503 if (!tv_nexus) {
1504 mutex_unlock(&tpg->tv_tpg_mutex);
1505 return -ENOMEM;
1506 }
1507 /*
Tao Chen78574872015-03-10 20:49:18 +00001508 * Initialize the struct se_session pointer
Juergen Grossd9d660f2014-08-28 06:44:12 +02001509 */
1510 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1511 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1512 mutex_unlock(&tpg->tv_tpg_mutex);
1513 kfree(tv_nexus);
1514 return -ENOMEM;
1515 }
1516 se_sess = tv_nexus->tvn_se_sess;
1517 /*
1518 * Since we are running in 'demo mode' this call with generate a
1519 * struct se_node_acl for the scsiback struct se_portal_group with
1520 * the SCSI Initiator port name of the passed configfs group 'name'.
1521 */
1522 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1523 se_tpg, (unsigned char *)name);
1524 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1525 mutex_unlock(&tpg->tv_tpg_mutex);
1526 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1527 name);
1528 goto out;
1529 }
Bart Van Assche2f450cc2015-02-12 11:48:49 +01001530 /* Now register the TCM pvscsi virtual I_T Nexus as active. */
1531 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001532 tv_nexus->tvn_se_sess, tv_nexus);
1533 tpg->tpg_nexus = tv_nexus;
1534
1535 mutex_unlock(&tpg->tv_tpg_mutex);
1536 return 0;
1537
1538out:
1539 transport_free_session(se_sess);
1540 kfree(tv_nexus);
1541 return -ENOMEM;
1542}
1543
1544static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1545{
1546 struct se_session *se_sess;
1547 struct scsiback_nexus *tv_nexus;
1548
1549 mutex_lock(&tpg->tv_tpg_mutex);
1550 tv_nexus = tpg->tpg_nexus;
1551 if (!tv_nexus) {
1552 mutex_unlock(&tpg->tv_tpg_mutex);
1553 return -ENODEV;
1554 }
1555
1556 se_sess = tv_nexus->tvn_se_sess;
1557 if (!se_sess) {
1558 mutex_unlock(&tpg->tv_tpg_mutex);
1559 return -ENODEV;
1560 }
1561
1562 if (tpg->tv_tpg_port_count != 0) {
1563 mutex_unlock(&tpg->tv_tpg_mutex);
1564 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1565 tpg->tv_tpg_port_count);
1566 return -EBUSY;
1567 }
1568
1569 if (tpg->tv_tpg_fe_count != 0) {
1570 mutex_unlock(&tpg->tv_tpg_mutex);
1571 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1572 tpg->tv_tpg_fe_count);
1573 return -EBUSY;
1574 }
1575
Tao Chen78574872015-03-10 20:49:18 +00001576 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001577 scsiback_dump_proto_id(tpg->tport),
1578 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1579
1580 /*
1581 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1582 */
1583 transport_deregister_session(tv_nexus->tvn_se_sess);
1584 tpg->tpg_nexus = NULL;
1585 mutex_unlock(&tpg->tv_tpg_mutex);
1586
1587 kfree(tv_nexus);
1588 return 0;
1589}
1590
1591static ssize_t scsiback_tpg_show_nexus(struct se_portal_group *se_tpg,
1592 char *page)
1593{
1594 struct scsiback_tpg *tpg = container_of(se_tpg,
1595 struct scsiback_tpg, se_tpg);
1596 struct scsiback_nexus *tv_nexus;
1597 ssize_t ret;
1598
1599 mutex_lock(&tpg->tv_tpg_mutex);
1600 tv_nexus = tpg->tpg_nexus;
1601 if (!tv_nexus) {
1602 mutex_unlock(&tpg->tv_tpg_mutex);
1603 return -ENODEV;
1604 }
1605 ret = snprintf(page, PAGE_SIZE, "%s\n",
1606 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1607 mutex_unlock(&tpg->tv_tpg_mutex);
1608
1609 return ret;
1610}
1611
1612static ssize_t scsiback_tpg_store_nexus(struct se_portal_group *se_tpg,
1613 const char *page,
1614 size_t count)
1615{
1616 struct scsiback_tpg *tpg = container_of(se_tpg,
1617 struct scsiback_tpg, se_tpg);
1618 struct scsiback_tport *tport_wwn = tpg->tport;
1619 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1620 int ret;
1621 /*
Tao Chen78574872015-03-10 20:49:18 +00001622 * Shutdown the active I_T nexus if 'NULL' is passed.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001623 */
1624 if (!strncmp(page, "NULL", 4)) {
1625 ret = scsiback_drop_nexus(tpg);
1626 return (!ret) ? count : ret;
1627 }
1628 /*
1629 * Otherwise make sure the passed virtual Initiator port WWN matches
1630 * the fabric protocol_id set in scsiback_make_tport(), and call
1631 * scsiback_make_nexus().
1632 */
1633 if (strlen(page) >= VSCSI_NAMELEN) {
1634 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1635 page, VSCSI_NAMELEN);
1636 return -EINVAL;
1637 }
1638 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1639
1640 ptr = strstr(i_port, "naa.");
1641 if (ptr) {
1642 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1643 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1644 i_port, scsiback_dump_proto_id(tport_wwn));
1645 return -EINVAL;
1646 }
1647 port_ptr = &i_port[0];
1648 goto check_newline;
1649 }
1650 ptr = strstr(i_port, "fc.");
1651 if (ptr) {
1652 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1653 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1654 i_port, scsiback_dump_proto_id(tport_wwn));
1655 return -EINVAL;
1656 }
1657 port_ptr = &i_port[3]; /* Skip over "fc." */
1658 goto check_newline;
1659 }
1660 ptr = strstr(i_port, "iqn.");
1661 if (ptr) {
1662 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1663 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1664 i_port, scsiback_dump_proto_id(tport_wwn));
1665 return -EINVAL;
1666 }
1667 port_ptr = &i_port[0];
1668 goto check_newline;
1669 }
1670 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1671 i_port);
1672 return -EINVAL;
1673 /*
1674 * Clear any trailing newline for the NAA WWN
1675 */
1676check_newline:
1677 if (i_port[strlen(i_port) - 1] == '\n')
1678 i_port[strlen(i_port) - 1] = '\0';
1679
1680 ret = scsiback_make_nexus(tpg, port_ptr);
1681 if (ret < 0)
1682 return ret;
1683
1684 return count;
1685}
1686
1687TF_TPG_BASE_ATTR(scsiback, nexus, S_IRUGO | S_IWUSR);
1688
1689static struct configfs_attribute *scsiback_tpg_attrs[] = {
1690 &scsiback_tpg_nexus.attr,
1691 NULL,
1692};
1693
1694static ssize_t
1695scsiback_wwn_show_attr_version(struct target_fabric_configfs *tf,
1696 char *page)
1697{
1698 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1699 UTS_RELEASE"\n",
1700 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1701}
1702
1703TF_WWN_ATTR_RO(scsiback, version);
1704
1705static struct configfs_attribute *scsiback_wwn_attrs[] = {
1706 &scsiback_wwn_version.attr,
1707 NULL,
1708};
1709
1710static char *scsiback_get_fabric_name(void)
1711{
1712 return "xen-pvscsi";
1713}
1714
1715static int scsiback_port_link(struct se_portal_group *se_tpg,
1716 struct se_lun *lun)
1717{
1718 struct scsiback_tpg *tpg = container_of(se_tpg,
1719 struct scsiback_tpg, se_tpg);
1720
1721 mutex_lock(&tpg->tv_tpg_mutex);
1722 tpg->tv_tpg_port_count++;
1723 mutex_unlock(&tpg->tv_tpg_mutex);
1724
1725 return 0;
1726}
1727
1728static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1729 struct se_lun *lun)
1730{
1731 struct scsiback_tpg *tpg = container_of(se_tpg,
1732 struct scsiback_tpg, se_tpg);
1733
1734 mutex_lock(&tpg->tv_tpg_mutex);
1735 tpg->tv_tpg_port_count--;
1736 mutex_unlock(&tpg->tv_tpg_mutex);
1737}
1738
1739static struct se_portal_group *
1740scsiback_make_tpg(struct se_wwn *wwn,
1741 struct config_group *group,
1742 const char *name)
1743{
1744 struct scsiback_tport *tport = container_of(wwn,
1745 struct scsiback_tport, tport_wwn);
1746
1747 struct scsiback_tpg *tpg;
Dan Carpenter495daef2014-09-08 14:17:35 +03001748 u16 tpgt;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001749 int ret;
1750
1751 if (strstr(name, "tpgt_") != name)
1752 return ERR_PTR(-EINVAL);
Dan Carpenter495daef2014-09-08 14:17:35 +03001753 ret = kstrtou16(name + 5, 10, &tpgt);
1754 if (ret)
1755 return ERR_PTR(ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001756
1757 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1758 if (!tpg)
1759 return ERR_PTR(-ENOMEM);
1760
1761 mutex_init(&tpg->tv_tpg_mutex);
1762 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1763 INIT_LIST_HEAD(&tpg->info_list);
1764 tpg->tport = tport;
1765 tpg->tport_tpgt = tpgt;
1766
Nicholas Bellingerbc0c94b2015-05-20 21:48:03 -07001767 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001768 if (ret < 0) {
1769 kfree(tpg);
1770 return NULL;
1771 }
1772 mutex_lock(&scsiback_mutex);
1773 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1774 mutex_unlock(&scsiback_mutex);
1775
1776 return &tpg->se_tpg;
1777}
1778
1779static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1780{
1781 struct scsiback_tpg *tpg = container_of(se_tpg,
1782 struct scsiback_tpg, se_tpg);
1783
1784 mutex_lock(&scsiback_mutex);
1785 list_del(&tpg->tv_tpg_list);
1786 mutex_unlock(&scsiback_mutex);
1787 /*
1788 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1789 */
1790 scsiback_drop_nexus(tpg);
1791 /*
Tao Chen78574872015-03-10 20:49:18 +00001792 * Deregister the se_tpg from TCM.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001793 */
1794 core_tpg_deregister(se_tpg);
1795 kfree(tpg);
1796}
1797
1798static int scsiback_check_true(struct se_portal_group *se_tpg)
1799{
1800 return 1;
1801}
1802
1803static int scsiback_check_false(struct se_portal_group *se_tpg)
1804{
1805 return 0;
1806}
1807
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001808static const struct target_core_fabric_ops scsiback_ops = {
1809 .module = THIS_MODULE,
1810 .name = "xen-pvscsi",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001811 .get_fabric_name = scsiback_get_fabric_name,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001812 .tpg_get_wwn = scsiback_get_fabric_wwn,
1813 .tpg_get_tag = scsiback_get_tag,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001814 .tpg_check_demo_mode = scsiback_check_true,
1815 .tpg_check_demo_mode_cache = scsiback_check_true,
1816 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1817 .tpg_check_prod_mode_write_protect = scsiback_check_false,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001818 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1819 .check_stop_free = scsiback_check_stop_free,
1820 .release_cmd = scsiback_release_cmd,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001821 .shutdown_session = scsiback_shutdown_session,
1822 .close_session = scsiback_close_session,
1823 .sess_get_index = scsiback_sess_get_index,
1824 .sess_get_initiator_sid = NULL,
1825 .write_pending = scsiback_write_pending,
1826 .write_pending_status = scsiback_write_pending_status,
1827 .set_default_node_attributes = scsiback_set_default_node_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001828 .get_cmd_state = scsiback_get_cmd_state,
1829 .queue_data_in = scsiback_queue_data_in,
1830 .queue_status = scsiback_queue_status,
1831 .queue_tm_rsp = scsiback_queue_tm_rsp,
1832 .aborted_task = scsiback_aborted_task,
1833 /*
1834 * Setup callers for generic logic in target_core_fabric_configfs.c
1835 */
1836 .fabric_make_wwn = scsiback_make_tport,
1837 .fabric_drop_wwn = scsiback_drop_tport,
1838 .fabric_make_tpg = scsiback_make_tpg,
1839 .fabric_drop_tpg = scsiback_drop_tpg,
1840 .fabric_post_link = scsiback_port_link,
1841 .fabric_pre_unlink = scsiback_port_unlink,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001842
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001843 .tfc_wwn_attrs = scsiback_wwn_attrs,
1844 .tfc_tpg_base_attrs = scsiback_tpg_attrs,
1845 .tfc_tpg_param_attrs = scsiback_param_attrs,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001846};
1847
1848static const struct xenbus_device_id scsiback_ids[] = {
1849 { "vscsi" },
1850 { "" }
1851};
1852
David Vrabel95afae42014-09-08 17:30:41 +01001853static struct xenbus_driver scsiback_driver = {
1854 .ids = scsiback_ids,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001855 .probe = scsiback_probe,
1856 .remove = scsiback_remove,
1857 .otherend_changed = scsiback_frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001858};
Juergen Grossd9d660f2014-08-28 06:44:12 +02001859
1860static void scsiback_init_pend(void *p)
1861{
1862 struct vscsibk_pend *pend = p;
1863 int i;
1864
1865 memset(pend, 0, sizeof(*pend));
1866 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
1867 pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
1868}
1869
1870static int __init scsiback_init(void)
1871{
1872 int ret;
1873
1874 if (!xen_domain())
1875 return -ENODEV;
1876
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001877 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1878 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1879
Juergen Grossd9d660f2014-08-28 06:44:12 +02001880 scsiback_cachep = kmem_cache_create("vscsiif_cache",
1881 sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend);
1882 if (!scsiback_cachep)
1883 return -ENOMEM;
1884
1885 ret = xenbus_register_backend(&scsiback_driver);
1886 if (ret)
1887 goto out_cache_destroy;
1888
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001889 ret = target_register_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001890 if (ret)
1891 goto out_unregister_xenbus;
1892
1893 return 0;
1894
1895out_unregister_xenbus:
1896 xenbus_unregister_driver(&scsiback_driver);
1897out_cache_destroy:
1898 kmem_cache_destroy(scsiback_cachep);
Tao Chen78574872015-03-10 20:49:18 +00001899 pr_err("%s: error %d\n", __func__, ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001900 return ret;
1901}
1902
1903static void __exit scsiback_exit(void)
1904{
1905 struct page *page;
1906
1907 while (free_pages_num) {
1908 if (get_free_page(&page))
1909 BUG();
David Vrabelff4b1562015-01-08 18:06:01 +00001910 gnttab_free_pages(1, &page);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001911 }
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001912 target_unregister_template(&scsiback_ops);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001913 xenbus_unregister_driver(&scsiback_driver);
1914 kmem_cache_destroy(scsiback_cachep);
1915}
1916
1917module_init(scsiback_init);
1918module_exit(scsiback_exit);
1919
1920MODULE_DESCRIPTION("Xen SCSI backend driver");
1921MODULE_LICENSE("Dual BSD/GPL");
1922MODULE_ALIAS("xen-backend:vscsi");
1923MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");