blob: ad4eb1024d1ffb7663db4e770fdd4c5c2663e821 [file] [log] [blame]
Juergen Grossd9d660f2014-08-28 06:44:12 +02001/*
2 * Xen SCSI backend driver
3 *
4 * Copyright (c) 2008, FUJITSU Limited
5 *
6 * Based on the blkback driver code.
7 * Adaption to kernel taget core infrastructure taken from vhost/scsi.c
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 * IN THE SOFTWARE.
32 */
33
Tao Chen78574872015-03-10 20:49:18 +000034#define pr_fmt(fmt) "xen-pvscsi: " fmt
35
Juergen Grossd9d660f2014-08-28 06:44:12 +020036#include <stdarg.h>
37
38#include <linux/module.h>
39#include <linux/utsname.h>
40#include <linux/interrupt.h>
41#include <linux/slab.h>
42#include <linux/wait.h>
43#include <linux/sched.h>
44#include <linux/list.h>
45#include <linux/gfp.h>
46#include <linux/delay.h>
47#include <linux/spinlock.h>
48#include <linux/configfs.h>
49
50#include <generated/utsrelease.h>
51
Bart Van Asscheba929992015-05-08 10:11:12 +020052#include <scsi/scsi_host.h> /* SG_ALL */
Juergen Grossd9d660f2014-08-28 06:44:12 +020053
54#include <target/target_core_base.h>
55#include <target/target_core_fabric.h>
Juergen Grossd9d660f2014-08-28 06:44:12 +020056
57#include <asm/hypervisor.h>
58
59#include <xen/xen.h>
60#include <xen/balloon.h>
61#include <xen/events.h>
62#include <xen/xenbus.h>
63#include <xen/grant_table.h>
64#include <xen/page.h>
65
66#include <xen/interface/grant_table.h>
67#include <xen/interface/io/vscsiif.h>
68
Juergen Grossd9d660f2014-08-28 06:44:12 +020069#define VSCSI_VERSION "v0.1"
70#define VSCSI_NAMELEN 32
71
72struct ids_tuple {
73 unsigned int hst; /* host */
74 unsigned int chn; /* channel */
75 unsigned int tgt; /* target */
76 unsigned int lun; /* LUN */
77};
78
79struct v2p_entry {
80 struct ids_tuple v; /* translate from */
81 struct scsiback_tpg *tpg; /* translate to */
82 unsigned int lun;
83 struct kref kref;
84 struct list_head l;
85};
86
87struct vscsibk_info {
88 struct xenbus_device *dev;
89
90 domid_t domid;
91 unsigned int irq;
92
93 struct vscsiif_back_ring ring;
94 int ring_error;
95
96 spinlock_t ring_lock;
97 atomic_t nr_unreplied_reqs;
98
99 spinlock_t v2p_lock;
100 struct list_head v2p_entry_lists;
101
102 wait_queue_head_t waiting_to_free;
103};
104
105/* theoretical maximum of grants for one request */
106#define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
107
108/*
109 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
110 * call to map/unmap grants. Don't choose it too large, as there are arrays
111 * with VSCSI_GRANT_BATCH elements allocated on the stack.
112 */
113#define VSCSI_GRANT_BATCH 16
114
115struct vscsibk_pend {
116 uint16_t rqid;
117
118 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
119 uint8_t cmd_len;
120
121 uint8_t sc_data_direction;
122 uint16_t n_sg; /* real length of SG list */
123 uint16_t n_grants; /* SG pages and potentially SG list */
124 uint32_t data_len;
125 uint32_t result;
126
127 struct vscsibk_info *info;
128 struct v2p_entry *v2p;
129 struct scatterlist *sgl;
130
131 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
132
133 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
134 struct page *pages[VSCSI_MAX_GRANTS];
135
136 struct se_cmd se_cmd;
137};
138
139struct scsiback_tmr {
140 atomic_t tmr_complete;
141 wait_queue_head_t tmr_wait;
142};
143
144struct scsiback_nexus {
145 /* Pointer to TCM session for I_T Nexus */
146 struct se_session *tvn_se_sess;
147};
148
149struct scsiback_tport {
150 /* SCSI protocol the tport is providing */
151 u8 tport_proto_id;
152 /* Binary World Wide unique Port Name for pvscsi Target port */
153 u64 tport_wwpn;
154 /* ASCII formatted WWPN for pvscsi Target port */
155 char tport_name[VSCSI_NAMELEN];
156 /* Returned by scsiback_make_tport() */
157 struct se_wwn tport_wwn;
158};
159
160struct scsiback_tpg {
161 /* scsiback port target portal group tag for TCM */
162 u16 tport_tpgt;
163 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
164 int tv_tpg_port_count;
165 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
166 int tv_tpg_fe_count;
167 /* list for scsiback_list */
168 struct list_head tv_tpg_list;
169 /* Used to protect access for tpg_nexus */
170 struct mutex tv_tpg_mutex;
171 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
172 struct scsiback_nexus *tpg_nexus;
173 /* Pointer back to scsiback_tport */
174 struct scsiback_tport *tport;
175 /* Returned by scsiback_make_tpg() */
176 struct se_portal_group se_tpg;
177 /* alias used in xenstore */
178 char param_alias[VSCSI_NAMELEN];
179 /* list of info structures related to this target portal group */
180 struct list_head info_list;
181};
182
183#define SCSIBACK_INVALID_HANDLE (~0)
184
185static bool log_print_stat;
186module_param(log_print_stat, bool, 0644);
187
188static int scsiback_max_buffer_pages = 1024;
189module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
190MODULE_PARM_DESC(max_buffer_pages,
191"Maximum number of free pages to keep in backend buffer");
192
193static struct kmem_cache *scsiback_cachep;
194static DEFINE_SPINLOCK(free_pages_lock);
195static int free_pages_num;
196static LIST_HEAD(scsiback_free_pages);
197
198/* Global spinlock to protect scsiback TPG list */
199static DEFINE_MUTEX(scsiback_mutex);
200static LIST_HEAD(scsiback_list);
201
Juergen Grossd9d660f2014-08-28 06:44:12 +0200202static void scsiback_get(struct vscsibk_info *info)
203{
204 atomic_inc(&info->nr_unreplied_reqs);
205}
206
207static void scsiback_put(struct vscsibk_info *info)
208{
209 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
210 wake_up(&info->waiting_to_free);
211}
212
213static void put_free_pages(struct page **page, int num)
214{
215 unsigned long flags;
216 int i = free_pages_num + num, n = num;
217
218 if (num == 0)
219 return;
220 if (i > scsiback_max_buffer_pages) {
221 n = min(num, i - scsiback_max_buffer_pages);
David Vrabelff4b1562015-01-08 18:06:01 +0000222 gnttab_free_pages(n, page + num - n);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200223 n = num - n;
224 }
225 spin_lock_irqsave(&free_pages_lock, flags);
226 for (i = 0; i < n; i++)
227 list_add(&page[i]->lru, &scsiback_free_pages);
228 free_pages_num += n;
229 spin_unlock_irqrestore(&free_pages_lock, flags);
230}
231
232static int get_free_page(struct page **page)
233{
234 unsigned long flags;
235
236 spin_lock_irqsave(&free_pages_lock, flags);
237 if (list_empty(&scsiback_free_pages)) {
238 spin_unlock_irqrestore(&free_pages_lock, flags);
David Vrabelff4b1562015-01-08 18:06:01 +0000239 return gnttab_alloc_pages(1, page);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200240 }
241 page[0] = list_first_entry(&scsiback_free_pages, struct page, lru);
242 list_del(&page[0]->lru);
243 free_pages_num--;
244 spin_unlock_irqrestore(&free_pages_lock, flags);
245 return 0;
246}
247
248static unsigned long vaddr_page(struct page *page)
249{
250 unsigned long pfn = page_to_pfn(page);
251
252 return (unsigned long)pfn_to_kaddr(pfn);
253}
254
255static unsigned long vaddr(struct vscsibk_pend *req, int seg)
256{
257 return vaddr_page(req->pages[seg]);
258}
259
260static void scsiback_print_status(char *sense_buffer, int errors,
261 struct vscsibk_pend *pending_req)
262{
263 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
264
Tao Chen78574872015-03-10 20:49:18 +0000265 pr_err("[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200266 tpg->tport->tport_name, pending_req->v2p->lun,
267 pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
268 host_byte(errors), driver_byte(errors));
Juergen Grossd9d660f2014-08-28 06:44:12 +0200269}
270
271static void scsiback_fast_flush_area(struct vscsibk_pend *req)
272{
273 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
274 struct page *pages[VSCSI_GRANT_BATCH];
275 unsigned int i, invcount = 0;
276 grant_handle_t handle;
277 int err;
278
279 kfree(req->sgl);
280 req->sgl = NULL;
281 req->n_sg = 0;
282
283 if (!req->n_grants)
284 return;
285
286 for (i = 0; i < req->n_grants; i++) {
287 handle = req->grant_handles[i];
288 if (handle == SCSIBACK_INVALID_HANDLE)
289 continue;
290 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
291 GNTMAP_host_map, handle);
292 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
293 pages[invcount] = req->pages[i];
294 put_page(pages[invcount]);
295 invcount++;
296 if (invcount < VSCSI_GRANT_BATCH)
297 continue;
298 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
299 BUG_ON(err);
300 invcount = 0;
301 }
302
303 if (invcount) {
304 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
305 BUG_ON(err);
306 }
307
308 put_free_pages(req->pages, req->n_grants);
309 req->n_grants = 0;
310}
311
312static void scsiback_free_translation_entry(struct kref *kref)
313{
314 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
315 struct scsiback_tpg *tpg = entry->tpg;
316
317 mutex_lock(&tpg->tv_tpg_mutex);
318 tpg->tv_tpg_fe_count--;
319 mutex_unlock(&tpg->tv_tpg_mutex);
320
321 kfree(entry);
322}
323
324static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
325 uint32_t resid, struct vscsibk_pend *pending_req)
326{
327 struct vscsiif_response *ring_res;
328 struct vscsibk_info *info = pending_req->info;
329 int notify;
330 struct scsi_sense_hdr sshdr;
331 unsigned long flags;
332 unsigned len;
333
334 spin_lock_irqsave(&info->ring_lock, flags);
335
336 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
337 info->ring.rsp_prod_pvt++;
338
339 ring_res->rslt = result;
340 ring_res->rqid = pending_req->rqid;
341
342 if (sense_buffer != NULL &&
343 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
344 &sshdr)) {
345 len = min_t(unsigned, 8 + sense_buffer[7],
346 VSCSIIF_SENSE_BUFFERSIZE);
347 memcpy(ring_res->sense_buffer, sense_buffer, len);
348 ring_res->sense_len = len;
349 } else {
350 ring_res->sense_len = 0;
351 }
352
353 ring_res->residual_len = resid;
354
355 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
356 spin_unlock_irqrestore(&info->ring_lock, flags);
357
358 if (notify)
359 notify_remote_via_irq(info->irq);
360
361 if (pending_req->v2p)
362 kref_put(&pending_req->v2p->kref,
363 scsiback_free_translation_entry);
364}
365
366static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
367{
368 struct vscsibk_info *info = pending_req->info;
369 unsigned char *sense_buffer;
370 unsigned int resid;
371 int errors;
372
373 sense_buffer = pending_req->sense_buffer;
374 resid = pending_req->se_cmd.residual_count;
375 errors = pending_req->result;
376
377 if (errors && log_print_stat)
378 scsiback_print_status(sense_buffer, errors, pending_req);
379
380 scsiback_fast_flush_area(pending_req);
381 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
382 scsiback_put(info);
383}
384
385static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
386{
387 struct se_cmd *se_cmd = &pending_req->se_cmd;
388 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
389 int rc;
390
391 memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
392
393 memset(se_cmd, 0, sizeof(*se_cmd));
394
395 scsiback_get(pending_req->info);
Bart Van Assche649ee052015-04-14 13:26:44 +0200396 se_cmd->tag = pending_req->rqid;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200397 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
398 pending_req->sense_buffer, pending_req->v2p->lun,
399 pending_req->data_len, 0,
400 pending_req->sc_data_direction, 0,
401 pending_req->sgl, pending_req->n_sg,
402 NULL, 0, NULL, 0);
403 if (rc < 0) {
404 transport_send_check_condition_and_sense(se_cmd,
405 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
406 transport_generic_free_cmd(se_cmd, 0);
407 }
408}
409
410static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
411 struct page **pg, grant_handle_t *grant, int cnt)
412{
413 int err, i;
414
415 if (!cnt)
416 return 0;
417
418 err = gnttab_map_refs(map, NULL, pg, cnt);
419 BUG_ON(err);
420 for (i = 0; i < cnt; i++) {
421 if (unlikely(map[i].status != GNTST_okay)) {
Tao Chen78574872015-03-10 20:49:18 +0000422 pr_err("invalid buffer -- could not remap it\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200423 map[i].handle = SCSIBACK_INVALID_HANDLE;
424 err = -ENOMEM;
425 } else {
426 get_page(pg[i]);
427 }
428 grant[i] = map[i].handle;
429 }
430 return err;
431}
432
433static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
434 struct scsiif_request_segment *seg, struct page **pg,
435 grant_handle_t *grant, int cnt, u32 flags)
436{
437 int mapcount = 0, i, err = 0;
438 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
439 struct vscsibk_info *info = pending_req->info;
440
441 for (i = 0; i < cnt; i++) {
442 if (get_free_page(pg + mapcount)) {
443 put_free_pages(pg, mapcount);
Tao Chen78574872015-03-10 20:49:18 +0000444 pr_err("no grant page\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200445 return -ENOMEM;
446 }
447 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
448 flags, seg[i].gref, info->domid);
449 mapcount++;
450 if (mapcount < VSCSI_GRANT_BATCH)
451 continue;
452 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
453 pg += mapcount;
454 grant += mapcount;
455 pending_req->n_grants += mapcount;
456 if (err)
457 return err;
458 mapcount = 0;
459 }
460 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
461 pending_req->n_grants += mapcount;
462 return err;
463}
464
465static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
466 struct vscsibk_pend *pending_req)
467{
468 u32 flags;
469 int i, err, n_segs, i_seg = 0;
470 struct page **pg;
471 struct scsiif_request_segment *seg;
472 unsigned long end_seg = 0;
473 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
474 unsigned int nr_sgl = 0;
475 struct scatterlist *sg;
476 grant_handle_t *grant;
477
478 pending_req->n_sg = 0;
479 pending_req->n_grants = 0;
480 pending_req->data_len = 0;
481
482 nr_segments &= ~VSCSIIF_SG_GRANT;
483 if (!nr_segments)
484 return 0;
485
486 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
Tao Chen78574872015-03-10 20:49:18 +0000487 pr_debug("invalid parameter nr_seg = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200488 ring_req->nr_segments);
489 return -EINVAL;
490 }
491
492 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
493 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
494 pending_req->pages, pending_req->grant_handles,
495 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
496 if (err)
497 return err;
498 nr_sgl = nr_segments;
499 nr_segments = 0;
500 for (i = 0; i < nr_sgl; i++) {
501 n_segs = ring_req->seg[i].length /
502 sizeof(struct scsiif_request_segment);
503 if ((unsigned)ring_req->seg[i].offset +
504 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
505 n_segs * sizeof(struct scsiif_request_segment) !=
506 ring_req->seg[i].length)
507 return -EINVAL;
508 nr_segments += n_segs;
509 }
510 if (nr_segments > SG_ALL) {
Tao Chen78574872015-03-10 20:49:18 +0000511 pr_debug("invalid nr_seg = %d\n", nr_segments);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200512 return -EINVAL;
513 }
514 }
515
Tao Chen78574872015-03-10 20:49:18 +0000516 /* free of (sgl) in fast_flush_area() */
Juergen Grossd9d660f2014-08-28 06:44:12 +0200517 pending_req->sgl = kmalloc_array(nr_segments,
518 sizeof(struct scatterlist), GFP_KERNEL);
519 if (!pending_req->sgl)
520 return -ENOMEM;
521
522 sg_init_table(pending_req->sgl, nr_segments);
523 pending_req->n_sg = nr_segments;
524
525 flags = GNTMAP_host_map;
526 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
527 flags |= GNTMAP_readonly;
528
529 pg = pending_req->pages + nr_sgl;
530 grant = pending_req->grant_handles + nr_sgl;
531 if (!nr_sgl) {
532 seg = ring_req->seg;
533 err = scsiback_gnttab_data_map_list(pending_req, seg,
534 pg, grant, nr_segments, flags);
535 if (err)
536 return err;
537 } else {
538 for (i = 0; i < nr_sgl; i++) {
539 seg = (struct scsiif_request_segment *)(
540 vaddr(pending_req, i) + ring_req->seg[i].offset);
541 n_segs = ring_req->seg[i].length /
542 sizeof(struct scsiif_request_segment);
543 err = scsiback_gnttab_data_map_list(pending_req, seg,
544 pg, grant, n_segs, flags);
545 if (err)
546 return err;
547 pg += n_segs;
548 grant += n_segs;
549 }
550 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
551 seg = (struct scsiif_request_segment *)end_seg;
552 end_seg += ring_req->seg[0].length;
553 pg = pending_req->pages + nr_sgl;
554 }
555
556 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
557 sg_set_page(sg, pg[i], seg->length, seg->offset);
558 pending_req->data_len += seg->length;
559 seg++;
560 if (nr_sgl && (unsigned long)seg >= end_seg) {
561 i_seg++;
562 end_seg = vaddr(pending_req, i_seg) +
563 ring_req->seg[i_seg].offset;
564 seg = (struct scsiif_request_segment *)end_seg;
565 end_seg += ring_req->seg[i_seg].length;
566 }
567 if (sg->offset >= PAGE_SIZE ||
568 sg->length > PAGE_SIZE ||
569 sg->offset + sg->length > PAGE_SIZE)
570 return -EINVAL;
571 }
572
573 return 0;
574}
575
576static void scsiback_disconnect(struct vscsibk_info *info)
577{
578 wait_event(info->waiting_to_free,
579 atomic_read(&info->nr_unreplied_reqs) == 0);
580
581 unbind_from_irqhandler(info->irq, info);
582 info->irq = 0;
583 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
584}
585
586static void scsiback_device_action(struct vscsibk_pend *pending_req,
587 enum tcm_tmreq_table act, int tag)
588{
589 int rc, err = FAILED;
590 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
591 struct se_cmd *se_cmd = &pending_req->se_cmd;
592 struct scsiback_tmr *tmr;
593
594 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
595 if (!tmr)
596 goto out;
597
598 init_waitqueue_head(&tmr->tmr_wait);
599
600 transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800601 tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, TCM_SIMPLE_TAG,
Juergen Grossd9d660f2014-08-28 06:44:12 +0200602 &pending_req->sense_buffer[0]);
603
604 rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
605 if (rc < 0)
606 goto out;
607
608 se_cmd->se_tmr_req->ref_task_tag = tag;
609
610 if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
611 goto out;
612
613 transport_generic_handle_tmr(se_cmd);
614 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
615
616 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
617 SUCCESS : FAILED;
618
619out:
620 if (tmr) {
621 transport_generic_free_cmd(&pending_req->se_cmd, 1);
622 kfree(tmr);
623 }
624
625 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
626
627 kmem_cache_free(scsiback_cachep, pending_req);
628}
629
630/*
631 Perform virtual to physical translation
632*/
633static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
634 struct ids_tuple *v)
635{
636 struct v2p_entry *entry;
637 struct list_head *head = &(info->v2p_entry_lists);
638 unsigned long flags;
639
640 spin_lock_irqsave(&info->v2p_lock, flags);
641 list_for_each_entry(entry, head, l) {
642 if ((entry->v.chn == v->chn) &&
643 (entry->v.tgt == v->tgt) &&
644 (entry->v.lun == v->lun)) {
645 kref_get(&entry->kref);
646 goto out;
647 }
648 }
649 entry = NULL;
650
651out:
652 spin_unlock_irqrestore(&info->v2p_lock, flags);
653 return entry;
654}
655
656static int prepare_pending_reqs(struct vscsibk_info *info,
657 struct vscsiif_request *ring_req,
658 struct vscsibk_pend *pending_req)
659{
660 struct v2p_entry *v2p;
661 struct ids_tuple vir;
662
663 pending_req->rqid = ring_req->rqid;
664 pending_req->info = info;
665
666 vir.chn = ring_req->channel;
667 vir.tgt = ring_req->id;
668 vir.lun = ring_req->lun;
669
670 v2p = scsiback_do_translation(info, &vir);
671 if (!v2p) {
672 pending_req->v2p = NULL;
Tao Chen78574872015-03-10 20:49:18 +0000673 pr_debug("the v2p of (chn:%d, tgt:%d, lun:%d) doesn't exist.\n",
674 vir.chn, vir.tgt, vir.lun);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200675 return -ENODEV;
676 }
677 pending_req->v2p = v2p;
678
679 /* request range check from frontend */
680 pending_req->sc_data_direction = ring_req->sc_data_direction;
681 if ((pending_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
682 (pending_req->sc_data_direction != DMA_TO_DEVICE) &&
683 (pending_req->sc_data_direction != DMA_FROM_DEVICE) &&
684 (pending_req->sc_data_direction != DMA_NONE)) {
Tao Chen78574872015-03-10 20:49:18 +0000685 pr_debug("invalid parameter data_dir = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200686 pending_req->sc_data_direction);
687 return -EINVAL;
688 }
689
690 pending_req->cmd_len = ring_req->cmd_len;
691 if (pending_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
Tao Chen78574872015-03-10 20:49:18 +0000692 pr_debug("invalid parameter cmd_len = %d\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200693 pending_req->cmd_len);
694 return -EINVAL;
695 }
696 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
697
698 return 0;
699}
700
701static int scsiback_do_cmd_fn(struct vscsibk_info *info)
702{
703 struct vscsiif_back_ring *ring = &info->ring;
Juergen Grossfacb5732015-02-17 08:02:47 +0100704 struct vscsiif_request ring_req;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200705 struct vscsibk_pend *pending_req;
706 RING_IDX rc, rp;
707 int err, more_to_do;
708 uint32_t result;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200709
710 rc = ring->req_cons;
711 rp = ring->sring->req_prod;
712 rmb(); /* guest system is accessing ring, too */
713
714 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
715 rc = ring->rsp_prod_pvt;
Tao Chen78574872015-03-10 20:49:18 +0000716 pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +0200717 info->domid, rp, rc, rp - rc);
718 info->ring_error = 1;
719 return 0;
720 }
721
722 while ((rc != rp)) {
723 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
724 break;
725 pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
726 if (!pending_req)
727 return 1;
728
David Vrabelbe697462015-11-16 18:02:32 +0000729 RING_COPY_REQUEST(ring, rc, &ring_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200730 ring->req_cons = ++rc;
731
Juergen Grossfacb5732015-02-17 08:02:47 +0100732 err = prepare_pending_reqs(info, &ring_req, pending_req);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200733 if (err) {
734 switch (err) {
735 case -ENODEV:
736 result = DID_NO_CONNECT;
737 break;
738 default:
739 result = DRIVER_ERROR;
740 break;
741 }
742 scsiback_do_resp_with_sense(NULL, result << 24, 0,
743 pending_req);
744 kmem_cache_free(scsiback_cachep, pending_req);
745 return 1;
746 }
747
Juergen Grossfacb5732015-02-17 08:02:47 +0100748 switch (ring_req.act) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200749 case VSCSIIF_ACT_SCSI_CDB:
Juergen Grossfacb5732015-02-17 08:02:47 +0100750 if (scsiback_gnttab_data_map(&ring_req, pending_req)) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200751 scsiback_fast_flush_area(pending_req);
752 scsiback_do_resp_with_sense(NULL,
753 DRIVER_ERROR << 24, 0, pending_req);
754 kmem_cache_free(scsiback_cachep, pending_req);
755 } else {
756 scsiback_cmd_exec(pending_req);
757 }
758 break;
759 case VSCSIIF_ACT_SCSI_ABORT:
760 scsiback_device_action(pending_req, TMR_ABORT_TASK,
Juergen Grossfacb5732015-02-17 08:02:47 +0100761 ring_req.ref_rqid);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200762 break;
763 case VSCSIIF_ACT_SCSI_RESET:
764 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
765 break;
766 default:
Tao Chen78574872015-03-10 20:49:18 +0000767 pr_err_ratelimited("invalid request\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200768 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
769 0, pending_req);
770 kmem_cache_free(scsiback_cachep, pending_req);
771 break;
772 }
773
774 /* Yield point for this unbounded loop. */
775 cond_resched();
776 }
777
778 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
779 return more_to_do;
780}
781
782static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
783{
784 struct vscsibk_info *info = dev_id;
785
786 if (info->ring_error)
787 return IRQ_HANDLED;
788
789 while (scsiback_do_cmd_fn(info))
790 cond_resched();
791
792 return IRQ_HANDLED;
793}
794
795static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
796 evtchn_port_t evtchn)
797{
798 void *area;
799 struct vscsiif_sring *sring;
800 int err;
801
802 if (info->irq)
803 return -1;
804
Wei Liuccc9d902015-04-03 14:44:59 +0800805 err = xenbus_map_ring_valloc(info->dev, &ring_ref, 1, &area);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200806 if (err)
807 return err;
808
809 sring = (struct vscsiif_sring *)area;
810 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
811
812 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
813 if (err < 0)
814 goto unmap_page;
815
816 info->irq = err;
817
818 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
819 IRQF_ONESHOT, "vscsiif-backend", info);
820 if (err)
821 goto free_irq;
822
823 return 0;
824
825free_irq:
826 unbind_from_irqhandler(info->irq, info);
827 info->irq = 0;
828unmap_page:
829 xenbus_unmap_ring_vfree(info->dev, area);
830
831 return err;
832}
833
834static int scsiback_map(struct vscsibk_info *info)
835{
836 struct xenbus_device *dev = info->dev;
837 unsigned int ring_ref, evtchn;
838 int err;
839
840 err = xenbus_gather(XBT_NIL, dev->otherend,
841 "ring-ref", "%u", &ring_ref,
842 "event-channel", "%u", &evtchn, NULL);
843 if (err) {
844 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
845 return err;
846 }
847
848 return scsiback_init_sring(info, ring_ref, evtchn);
849}
850
851/*
852 Add a new translation entry
853*/
854static int scsiback_add_translation_entry(struct vscsibk_info *info,
855 char *phy, struct ids_tuple *v)
856{
857 int err = 0;
858 struct v2p_entry *entry;
859 struct v2p_entry *new;
860 struct list_head *head = &(info->v2p_entry_lists);
861 unsigned long flags;
862 char *lunp;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200863 unsigned long long unpacked_lun;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700864 struct se_lun *se_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200865 struct scsiback_tpg *tpg_entry, *tpg = NULL;
866 char *error = "doesn't exist";
867
868 lunp = strrchr(phy, ':');
869 if (!lunp) {
Tao Chen78574872015-03-10 20:49:18 +0000870 pr_err("illegal format of physical device %s\n", phy);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200871 return -EINVAL;
872 }
873 *lunp = 0;
874 lunp++;
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200875 err = kstrtoull(lunp, 10, &unpacked_lun);
876 if (err < 0) {
Tao Chen78574872015-03-10 20:49:18 +0000877 pr_err("lun number not valid: %s\n", lunp);
Hannes Reinecke196e2e22015-06-10 08:41:23 +0200878 return err;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200879 }
880
881 mutex_lock(&scsiback_mutex);
882 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
883 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
884 !strcmp(phy, tpg_entry->param_alias)) {
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700885 mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
886 hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
887 if (se_lun->unpacked_lun == unpacked_lun) {
888 if (!tpg_entry->tpg_nexus)
889 error = "nexus undefined";
890 else
891 tpg = tpg_entry;
892 break;
893 }
Juergen Grossd9d660f2014-08-28 06:44:12 +0200894 }
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700895 mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200896 break;
897 }
898 }
899 if (tpg) {
900 mutex_lock(&tpg->tv_tpg_mutex);
901 tpg->tv_tpg_fe_count++;
902 mutex_unlock(&tpg->tv_tpg_mutex);
903 }
904 mutex_unlock(&scsiback_mutex);
905
906 if (!tpg) {
Nicholas Bellinger0fb1f142015-06-16 00:37:16 -0700907 pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200908 return -ENODEV;
909 }
910
911 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
912 if (new == NULL) {
913 err = -ENOMEM;
914 goto out_free;
915 }
916
917 spin_lock_irqsave(&info->v2p_lock, flags);
918
919 /* Check double assignment to identical virtual ID */
920 list_for_each_entry(entry, head, l) {
921 if ((entry->v.chn == v->chn) &&
922 (entry->v.tgt == v->tgt) &&
923 (entry->v.lun == v->lun)) {
Tao Chen78574872015-03-10 20:49:18 +0000924 pr_warn("Virtual ID is already used. Assignment was not performed.\n");
Juergen Grossd9d660f2014-08-28 06:44:12 +0200925 err = -EEXIST;
926 goto out;
927 }
928
929 }
930
931 /* Create a new translation entry and add to the list */
932 kref_init(&new->kref);
933 new->v = *v;
934 new->tpg = tpg;
Nicholas Bellinger6bb82612015-05-10 19:31:10 -0700935 new->lun = unpacked_lun;
Juergen Grossd9d660f2014-08-28 06:44:12 +0200936 list_add_tail(&new->l, head);
937
938out:
939 spin_unlock_irqrestore(&info->v2p_lock, flags);
940
941out_free:
942 mutex_lock(&tpg->tv_tpg_mutex);
943 tpg->tv_tpg_fe_count--;
944 mutex_unlock(&tpg->tv_tpg_mutex);
945
946 if (err)
947 kfree(new);
948
949 return err;
950}
951
952static void __scsiback_del_translation_entry(struct v2p_entry *entry)
953{
954 list_del(&entry->l);
955 kref_put(&entry->kref, scsiback_free_translation_entry);
956}
957
958/*
959 Delete the translation entry specfied
960*/
961static int scsiback_del_translation_entry(struct vscsibk_info *info,
962 struct ids_tuple *v)
963{
964 struct v2p_entry *entry;
965 struct list_head *head = &(info->v2p_entry_lists);
966 unsigned long flags;
967
968 spin_lock_irqsave(&info->v2p_lock, flags);
969 /* Find out the translation entry specified */
970 list_for_each_entry(entry, head, l) {
971 if ((entry->v.chn == v->chn) &&
972 (entry->v.tgt == v->tgt) &&
973 (entry->v.lun == v->lun)) {
974 goto found;
975 }
976 }
977
978 spin_unlock_irqrestore(&info->v2p_lock, flags);
979 return 1;
980
981found:
982 /* Delete the translation entry specfied */
983 __scsiback_del_translation_entry(entry);
984
985 spin_unlock_irqrestore(&info->v2p_lock, flags);
986 return 0;
987}
988
989static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
Juergen Gross169e6cf2015-02-17 08:02:48 +0100990 char *phy, struct ids_tuple *vir, int try)
Juergen Grossd9d660f2014-08-28 06:44:12 +0200991{
992 if (!scsiback_add_translation_entry(info, phy, vir)) {
993 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
994 "%d", XenbusStateInitialised)) {
Tao Chen78574872015-03-10 20:49:18 +0000995 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +0200996 scsiback_del_translation_entry(info, vir);
997 }
Juergen Gross169e6cf2015-02-17 08:02:48 +0100998 } else if (!try) {
Juergen Grossd9d660f2014-08-28 06:44:12 +0200999 xenbus_printf(XBT_NIL, info->dev->nodename, state,
1000 "%d", XenbusStateClosed);
1001 }
1002}
1003
1004static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1005 struct ids_tuple *vir)
1006{
1007 if (!scsiback_del_translation_entry(info, vir)) {
1008 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1009 "%d", XenbusStateClosed))
Tao Chen78574872015-03-10 20:49:18 +00001010 pr_err("xenbus_printf error %s\n", state);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001011 }
1012}
1013
1014#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1015#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1016
1017static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1018 char *ent)
1019{
1020 int err;
1021 struct ids_tuple vir;
1022 char *val;
1023 int device_state;
1024 char phy[VSCSI_NAMELEN];
1025 char str[64];
1026 char state[64];
1027 struct xenbus_device *dev = info->dev;
1028
1029 /* read status */
1030 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1031 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1032 if (XENBUS_EXIST_ERR(err))
1033 return;
1034
1035 /* physical SCSI device */
1036 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1037 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1038 if (IS_ERR(val)) {
1039 xenbus_printf(XBT_NIL, dev->nodename, state,
1040 "%d", XenbusStateClosed);
1041 return;
1042 }
1043 strlcpy(phy, val, VSCSI_NAMELEN);
1044 kfree(val);
1045
1046 /* virtual SCSI device */
1047 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1048 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1049 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1050 if (XENBUS_EXIST_ERR(err)) {
1051 xenbus_printf(XBT_NIL, dev->nodename, state,
1052 "%d", XenbusStateClosed);
1053 return;
1054 }
1055
1056 switch (op) {
1057 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
Juergen Gross169e6cf2015-02-17 08:02:48 +01001058 switch (device_state) {
1059 case XenbusStateInitialising:
1060 scsiback_do_add_lun(info, state, phy, &vir, 0);
1061 break;
1062 case XenbusStateConnected:
1063 scsiback_do_add_lun(info, state, phy, &vir, 1);
1064 break;
1065 case XenbusStateClosing:
Juergen Grossd9d660f2014-08-28 06:44:12 +02001066 scsiback_do_del_lun(info, state, &vir);
Juergen Gross169e6cf2015-02-17 08:02:48 +01001067 break;
1068 default:
1069 break;
1070 }
Juergen Grossd9d660f2014-08-28 06:44:12 +02001071 break;
1072
1073 case VSCSIBACK_OP_UPDATEDEV_STATE:
1074 if (device_state == XenbusStateInitialised) {
1075 /* modify vscsi-devs/dev-x/state */
1076 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1077 "%d", XenbusStateConnected)) {
Tao Chen78574872015-03-10 20:49:18 +00001078 pr_err("xenbus_printf error %s\n", str);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001079 scsiback_del_translation_entry(info, &vir);
1080 xenbus_printf(XBT_NIL, dev->nodename, state,
1081 "%d", XenbusStateClosed);
1082 }
1083 }
1084 break;
Tao Chen78574872015-03-10 20:49:18 +00001085 /* When it is necessary, processing is added here. */
Juergen Grossd9d660f2014-08-28 06:44:12 +02001086 default:
1087 break;
1088 }
1089}
1090
1091static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1092{
1093 int i;
1094 char **dir;
1095 unsigned int ndir = 0;
1096
1097 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1098 &ndir);
1099 if (IS_ERR(dir))
1100 return;
1101
1102 for (i = 0; i < ndir; i++)
1103 scsiback_do_1lun_hotplug(info, op, dir[i]);
1104
1105 kfree(dir);
1106}
1107
1108static void scsiback_frontend_changed(struct xenbus_device *dev,
1109 enum xenbus_state frontend_state)
1110{
1111 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1112
1113 switch (frontend_state) {
1114 case XenbusStateInitialising:
1115 break;
1116
1117 case XenbusStateInitialised:
1118 if (scsiback_map(info))
1119 break;
1120
1121 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1122 xenbus_switch_state(dev, XenbusStateConnected);
1123 break;
1124
1125 case XenbusStateConnected:
1126 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1127
1128 if (dev->state == XenbusStateConnected)
1129 break;
1130
1131 xenbus_switch_state(dev, XenbusStateConnected);
1132 break;
1133
1134 case XenbusStateClosing:
1135 if (info->irq)
1136 scsiback_disconnect(info);
1137
1138 xenbus_switch_state(dev, XenbusStateClosing);
1139 break;
1140
1141 case XenbusStateClosed:
1142 xenbus_switch_state(dev, XenbusStateClosed);
1143 if (xenbus_dev_is_online(dev))
1144 break;
1145 /* fall through if not online */
1146 case XenbusStateUnknown:
1147 device_unregister(&dev->dev);
1148 break;
1149
1150 case XenbusStateReconfiguring:
1151 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1152 xenbus_switch_state(dev, XenbusStateReconfigured);
1153
1154 break;
1155
1156 default:
1157 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1158 frontend_state);
1159 break;
1160 }
1161}
1162
1163/*
1164 Release the translation entry specfied
1165*/
1166static void scsiback_release_translation_entry(struct vscsibk_info *info)
1167{
1168 struct v2p_entry *entry, *tmp;
1169 struct list_head *head = &(info->v2p_entry_lists);
1170 unsigned long flags;
1171
1172 spin_lock_irqsave(&info->v2p_lock, flags);
1173
1174 list_for_each_entry_safe(entry, tmp, head, l)
1175 __scsiback_del_translation_entry(entry);
1176
1177 spin_unlock_irqrestore(&info->v2p_lock, flags);
1178}
1179
1180static int scsiback_remove(struct xenbus_device *dev)
1181{
1182 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1183
1184 if (info->irq)
1185 scsiback_disconnect(info);
1186
1187 scsiback_release_translation_entry(info);
1188
1189 dev_set_drvdata(&dev->dev, NULL);
1190
1191 return 0;
1192}
1193
1194static int scsiback_probe(struct xenbus_device *dev,
1195 const struct xenbus_device_id *id)
1196{
1197 int err;
1198
1199 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1200 GFP_KERNEL);
1201
Tao Chen78574872015-03-10 20:49:18 +00001202 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001203
1204 if (!info) {
1205 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1206 return -ENOMEM;
1207 }
1208 info->dev = dev;
1209 dev_set_drvdata(&dev->dev, info);
1210
1211 info->domid = dev->otherend_id;
1212 spin_lock_init(&info->ring_lock);
1213 info->ring_error = 0;
1214 atomic_set(&info->nr_unreplied_reqs, 0);
1215 init_waitqueue_head(&info->waiting_to_free);
1216 info->dev = dev;
1217 info->irq = 0;
1218 INIT_LIST_HEAD(&info->v2p_entry_lists);
1219 spin_lock_init(&info->v2p_lock);
1220
1221 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1222 SG_ALL);
1223 if (err)
1224 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1225
1226 err = xenbus_switch_state(dev, XenbusStateInitWait);
1227 if (err)
1228 goto fail;
1229
1230 return 0;
1231
1232fail:
Tao Chen78574872015-03-10 20:49:18 +00001233 pr_warn("%s failed\n", __func__);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001234 scsiback_remove(dev);
1235
1236 return err;
1237}
1238
1239static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1240{
1241 switch (tport->tport_proto_id) {
1242 case SCSI_PROTOCOL_SAS:
1243 return "SAS";
1244 case SCSI_PROTOCOL_FCP:
1245 return "FCP";
1246 case SCSI_PROTOCOL_ISCSI:
1247 return "iSCSI";
1248 default:
1249 break;
1250 }
1251
1252 return "Unknown";
1253}
1254
Juergen Grossd9d660f2014-08-28 06:44:12 +02001255static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1256{
1257 struct scsiback_tpg *tpg = container_of(se_tpg,
1258 struct scsiback_tpg, se_tpg);
1259 struct scsiback_tport *tport = tpg->tport;
1260
1261 return &tport->tport_name[0];
1262}
1263
1264static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1265{
1266 struct scsiback_tpg *tpg = container_of(se_tpg,
1267 struct scsiback_tpg, se_tpg);
1268 return tpg->tport_tpgt;
1269}
1270
Juergen Grossd9d660f2014-08-28 06:44:12 +02001271static struct se_wwn *
1272scsiback_make_tport(struct target_fabric_configfs *tf,
1273 struct config_group *group,
1274 const char *name)
1275{
1276 struct scsiback_tport *tport;
1277 char *ptr;
1278 u64 wwpn = 0;
1279 int off = 0;
1280
1281 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1282 if (!tport)
1283 return ERR_PTR(-ENOMEM);
1284
1285 tport->tport_wwpn = wwpn;
1286 /*
1287 * Determine the emulated Protocol Identifier and Target Port Name
1288 * based on the incoming configfs directory name.
1289 */
1290 ptr = strstr(name, "naa.");
1291 if (ptr) {
1292 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1293 goto check_len;
1294 }
1295 ptr = strstr(name, "fc.");
1296 if (ptr) {
1297 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1298 off = 3; /* Skip over "fc." */
1299 goto check_len;
1300 }
1301 ptr = strstr(name, "iqn.");
1302 if (ptr) {
1303 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1304 goto check_len;
1305 }
1306
1307 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1308 kfree(tport);
1309 return ERR_PTR(-EINVAL);
1310
1311check_len:
1312 if (strlen(name) >= VSCSI_NAMELEN) {
1313 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1314 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1315 kfree(tport);
1316 return ERR_PTR(-EINVAL);
1317 }
1318 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1319
Tao Chen78574872015-03-10 20:49:18 +00001320 pr_debug("Allocated emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001321 scsiback_dump_proto_id(tport), name);
1322
1323 return &tport->tport_wwn;
1324}
1325
1326static void scsiback_drop_tport(struct se_wwn *wwn)
1327{
1328 struct scsiback_tport *tport = container_of(wwn,
1329 struct scsiback_tport, tport_wwn);
1330
Tao Chen78574872015-03-10 20:49:18 +00001331 pr_debug("Deallocating emulated Target %s Address: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001332 scsiback_dump_proto_id(tport), tport->tport_name);
1333
1334 kfree(tport);
1335}
1336
Juergen Grossd9d660f2014-08-28 06:44:12 +02001337static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1338{
1339 return 1;
1340}
1341
1342static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1343{
1344 /*
Tao Chen78574872015-03-10 20:49:18 +00001345 * Do not release struct se_cmd's containing a valid TMR pointer.
1346 * These will be released directly in scsiback_device_action()
Juergen Grossd9d660f2014-08-28 06:44:12 +02001347 * with transport_generic_free_cmd().
1348 */
1349 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
1350 return 0;
1351
1352 transport_generic_free_cmd(se_cmd, 0);
1353 return 1;
1354}
1355
1356static void scsiback_release_cmd(struct se_cmd *se_cmd)
1357{
1358 struct vscsibk_pend *pending_req = container_of(se_cmd,
1359 struct vscsibk_pend, se_cmd);
1360
1361 kmem_cache_free(scsiback_cachep, pending_req);
1362}
1363
1364static int scsiback_shutdown_session(struct se_session *se_sess)
1365{
1366 return 0;
1367}
1368
1369static void scsiback_close_session(struct se_session *se_sess)
1370{
1371}
1372
1373static u32 scsiback_sess_get_index(struct se_session *se_sess)
1374{
1375 return 0;
1376}
1377
1378static int scsiback_write_pending(struct se_cmd *se_cmd)
1379{
1380 /* Go ahead and process the write immediately */
1381 target_execute_cmd(se_cmd);
1382
1383 return 0;
1384}
1385
1386static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1387{
1388 return 0;
1389}
1390
1391static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1392{
1393}
1394
Juergen Grossd9d660f2014-08-28 06:44:12 +02001395static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1396{
1397 return 0;
1398}
1399
1400static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1401{
1402 struct vscsibk_pend *pending_req = container_of(se_cmd,
1403 struct vscsibk_pend, se_cmd);
1404
1405 pending_req->result = SAM_STAT_GOOD;
1406 scsiback_cmd_done(pending_req);
1407 return 0;
1408}
1409
1410static int scsiback_queue_status(struct se_cmd *se_cmd)
1411{
1412 struct vscsibk_pend *pending_req = container_of(se_cmd,
1413 struct vscsibk_pend, se_cmd);
1414
1415 if (se_cmd->sense_buffer &&
1416 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1417 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1418 pending_req->result = (DRIVER_SENSE << 24) |
1419 SAM_STAT_CHECK_CONDITION;
1420 else
1421 pending_req->result = se_cmd->scsi_status;
1422
1423 scsiback_cmd_done(pending_req);
1424 return 0;
1425}
1426
1427static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1428{
1429 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1430 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1431
1432 atomic_set(&tmr->tmr_complete, 1);
1433 wake_up(&tmr->tmr_wait);
1434}
1435
1436static void scsiback_aborted_task(struct se_cmd *se_cmd)
1437{
1438}
1439
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001440static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001441 char *page)
1442{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001443 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001444 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1445 se_tpg);
1446 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
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001455static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001456 const char *page, size_t count)
1457{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001458 struct se_portal_group *se_tpg = param_to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001459 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1460 se_tpg);
1461 int len;
1462
1463 if (strlen(page) >= VSCSI_NAMELEN) {
1464 pr_err("param alias: %s, exceeds max: %d\n", page,
1465 VSCSI_NAMELEN);
1466 return -EINVAL;
1467 }
1468
1469 mutex_lock(&tpg->tv_tpg_mutex);
1470 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1471 if (tpg->param_alias[len - 1] == '\n')
1472 tpg->param_alias[len - 1] = '\0';
1473 mutex_unlock(&tpg->tv_tpg_mutex);
1474
1475 return count;
1476}
1477
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001478CONFIGFS_ATTR(scsiback_tpg_param_, alias);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001479
1480static struct configfs_attribute *scsiback_param_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001481 &scsiback_tpg_param_attr_alias,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001482 NULL,
1483};
1484
1485static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1486 const char *name)
1487{
1488 struct se_portal_group *se_tpg;
1489 struct se_session *se_sess;
1490 struct scsiback_nexus *tv_nexus;
1491
1492 mutex_lock(&tpg->tv_tpg_mutex);
1493 if (tpg->tpg_nexus) {
1494 mutex_unlock(&tpg->tv_tpg_mutex);
1495 pr_debug("tpg->tpg_nexus already exists\n");
1496 return -EEXIST;
1497 }
1498 se_tpg = &tpg->se_tpg;
1499
1500 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1501 if (!tv_nexus) {
1502 mutex_unlock(&tpg->tv_tpg_mutex);
1503 return -ENOMEM;
1504 }
1505 /*
Tao Chen78574872015-03-10 20:49:18 +00001506 * Initialize the struct se_session pointer
Juergen Grossd9d660f2014-08-28 06:44:12 +02001507 */
1508 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1509 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1510 mutex_unlock(&tpg->tv_tpg_mutex);
1511 kfree(tv_nexus);
1512 return -ENOMEM;
1513 }
1514 se_sess = tv_nexus->tvn_se_sess;
1515 /*
1516 * Since we are running in 'demo mode' this call with generate a
1517 * struct se_node_acl for the scsiback struct se_portal_group with
1518 * the SCSI Initiator port name of the passed configfs group 'name'.
1519 */
1520 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1521 se_tpg, (unsigned char *)name);
1522 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1523 mutex_unlock(&tpg->tv_tpg_mutex);
1524 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1525 name);
1526 goto out;
1527 }
Bart Van Assche2f450cc2015-02-12 11:48:49 +01001528 /* Now register the TCM pvscsi virtual I_T Nexus as active. */
1529 transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001530 tv_nexus->tvn_se_sess, tv_nexus);
1531 tpg->tpg_nexus = tv_nexus;
1532
1533 mutex_unlock(&tpg->tv_tpg_mutex);
1534 return 0;
1535
1536out:
1537 transport_free_session(se_sess);
1538 kfree(tv_nexus);
1539 return -ENOMEM;
1540}
1541
1542static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1543{
1544 struct se_session *se_sess;
1545 struct scsiback_nexus *tv_nexus;
1546
1547 mutex_lock(&tpg->tv_tpg_mutex);
1548 tv_nexus = tpg->tpg_nexus;
1549 if (!tv_nexus) {
1550 mutex_unlock(&tpg->tv_tpg_mutex);
1551 return -ENODEV;
1552 }
1553
1554 se_sess = tv_nexus->tvn_se_sess;
1555 if (!se_sess) {
1556 mutex_unlock(&tpg->tv_tpg_mutex);
1557 return -ENODEV;
1558 }
1559
1560 if (tpg->tv_tpg_port_count != 0) {
1561 mutex_unlock(&tpg->tv_tpg_mutex);
1562 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1563 tpg->tv_tpg_port_count);
1564 return -EBUSY;
1565 }
1566
1567 if (tpg->tv_tpg_fe_count != 0) {
1568 mutex_unlock(&tpg->tv_tpg_mutex);
1569 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1570 tpg->tv_tpg_fe_count);
1571 return -EBUSY;
1572 }
1573
Tao Chen78574872015-03-10 20:49:18 +00001574 pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
Juergen Grossd9d660f2014-08-28 06:44:12 +02001575 scsiback_dump_proto_id(tpg->tport),
1576 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1577
1578 /*
1579 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1580 */
1581 transport_deregister_session(tv_nexus->tvn_se_sess);
1582 tpg->tpg_nexus = NULL;
1583 mutex_unlock(&tpg->tv_tpg_mutex);
1584
1585 kfree(tv_nexus);
1586 return 0;
1587}
1588
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001589static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001590{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001591 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001592 struct scsiback_tpg *tpg = container_of(se_tpg,
1593 struct scsiback_tpg, se_tpg);
1594 struct scsiback_nexus *tv_nexus;
1595 ssize_t ret;
1596
1597 mutex_lock(&tpg->tv_tpg_mutex);
1598 tv_nexus = tpg->tpg_nexus;
1599 if (!tv_nexus) {
1600 mutex_unlock(&tpg->tv_tpg_mutex);
1601 return -ENODEV;
1602 }
1603 ret = snprintf(page, PAGE_SIZE, "%s\n",
1604 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1605 mutex_unlock(&tpg->tv_tpg_mutex);
1606
1607 return ret;
1608}
1609
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001610static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1611 const char *page, size_t count)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001612{
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001613 struct se_portal_group *se_tpg = to_tpg(item);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001614 struct scsiback_tpg *tpg = container_of(se_tpg,
1615 struct scsiback_tpg, se_tpg);
1616 struct scsiback_tport *tport_wwn = tpg->tport;
1617 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1618 int ret;
1619 /*
Tao Chen78574872015-03-10 20:49:18 +00001620 * Shutdown the active I_T nexus if 'NULL' is passed.
Juergen Grossd9d660f2014-08-28 06:44:12 +02001621 */
1622 if (!strncmp(page, "NULL", 4)) {
1623 ret = scsiback_drop_nexus(tpg);
1624 return (!ret) ? count : ret;
1625 }
1626 /*
1627 * Otherwise make sure the passed virtual Initiator port WWN matches
1628 * the fabric protocol_id set in scsiback_make_tport(), and call
1629 * scsiback_make_nexus().
1630 */
1631 if (strlen(page) >= VSCSI_NAMELEN) {
1632 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1633 page, VSCSI_NAMELEN);
1634 return -EINVAL;
1635 }
1636 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1637
1638 ptr = strstr(i_port, "naa.");
1639 if (ptr) {
1640 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1641 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1642 i_port, scsiback_dump_proto_id(tport_wwn));
1643 return -EINVAL;
1644 }
1645 port_ptr = &i_port[0];
1646 goto check_newline;
1647 }
1648 ptr = strstr(i_port, "fc.");
1649 if (ptr) {
1650 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1651 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1652 i_port, scsiback_dump_proto_id(tport_wwn));
1653 return -EINVAL;
1654 }
1655 port_ptr = &i_port[3]; /* Skip over "fc." */
1656 goto check_newline;
1657 }
1658 ptr = strstr(i_port, "iqn.");
1659 if (ptr) {
1660 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1661 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1662 i_port, scsiback_dump_proto_id(tport_wwn));
1663 return -EINVAL;
1664 }
1665 port_ptr = &i_port[0];
1666 goto check_newline;
1667 }
1668 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1669 i_port);
1670 return -EINVAL;
1671 /*
1672 * Clear any trailing newline for the NAA WWN
1673 */
1674check_newline:
1675 if (i_port[strlen(i_port) - 1] == '\n')
1676 i_port[strlen(i_port) - 1] = '\0';
1677
1678 ret = scsiback_make_nexus(tpg, port_ptr);
1679 if (ret < 0)
1680 return ret;
1681
1682 return count;
1683}
1684
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001685CONFIGFS_ATTR(scsiback_tpg_, nexus);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001686
1687static struct configfs_attribute *scsiback_tpg_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001688 &scsiback_tpg_attr_nexus,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001689 NULL,
1690};
1691
1692static ssize_t
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001693scsiback_wwn_version_show(struct config_item *item, char *page)
Juergen Grossd9d660f2014-08-28 06:44:12 +02001694{
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
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001700CONFIGFS_ATTR_RO(scsiback_wwn_, version);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001701
1702static struct configfs_attribute *scsiback_wwn_attrs[] = {
Christoph Hellwig2eafd722015-10-03 15:32:55 +02001703 &scsiback_wwn_attr_version,
Juergen Grossd9d660f2014-08-28 06:44:12 +02001704 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>");