blob: 3e32146472a52c535e48cbfdae294c62985e8862 [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
34#include <stdarg.h>
35
36#include <linux/module.h>
37#include <linux/utsname.h>
38#include <linux/interrupt.h>
39#include <linux/slab.h>
40#include <linux/wait.h>
41#include <linux/sched.h>
42#include <linux/list.h>
43#include <linux/gfp.h>
44#include <linux/delay.h>
45#include <linux/spinlock.h>
46#include <linux/configfs.h>
47
48#include <generated/utsrelease.h>
49
50#include <scsi/scsi_dbg.h>
51#include <scsi/scsi_eh.h>
52#include <scsi/scsi_tcq.h>
53
54#include <target/target_core_base.h>
55#include <target/target_core_fabric.h>
56#include <target/target_core_configfs.h>
57#include <target/target_core_fabric_configfs.h>
58
59#include <asm/hypervisor.h>
60
61#include <xen/xen.h>
62#include <xen/balloon.h>
63#include <xen/events.h>
64#include <xen/xenbus.h>
65#include <xen/grant_table.h>
66#include <xen/page.h>
67
68#include <xen/interface/grant_table.h>
69#include <xen/interface/io/vscsiif.h>
70
71#define DPRINTK(_f, _a...) \
72 pr_debug("(file=%s, line=%d) " _f, __FILE__ , __LINE__ , ## _a)
73
74#define VSCSI_VERSION "v0.1"
75#define VSCSI_NAMELEN 32
76
77struct ids_tuple {
78 unsigned int hst; /* host */
79 unsigned int chn; /* channel */
80 unsigned int tgt; /* target */
81 unsigned int lun; /* LUN */
82};
83
84struct v2p_entry {
85 struct ids_tuple v; /* translate from */
86 struct scsiback_tpg *tpg; /* translate to */
87 unsigned int lun;
88 struct kref kref;
89 struct list_head l;
90};
91
92struct vscsibk_info {
93 struct xenbus_device *dev;
94
95 domid_t domid;
96 unsigned int irq;
97
98 struct vscsiif_back_ring ring;
99 int ring_error;
100
101 spinlock_t ring_lock;
102 atomic_t nr_unreplied_reqs;
103
104 spinlock_t v2p_lock;
105 struct list_head v2p_entry_lists;
106
107 wait_queue_head_t waiting_to_free;
108};
109
110/* theoretical maximum of grants for one request */
111#define VSCSI_MAX_GRANTS (SG_ALL + VSCSIIF_SG_TABLESIZE)
112
113/*
114 * VSCSI_GRANT_BATCH is the maximum number of grants to be processed in one
115 * call to map/unmap grants. Don't choose it too large, as there are arrays
116 * with VSCSI_GRANT_BATCH elements allocated on the stack.
117 */
118#define VSCSI_GRANT_BATCH 16
119
120struct vscsibk_pend {
121 uint16_t rqid;
122
123 uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE];
124 uint8_t cmd_len;
125
126 uint8_t sc_data_direction;
127 uint16_t n_sg; /* real length of SG list */
128 uint16_t n_grants; /* SG pages and potentially SG list */
129 uint32_t data_len;
130 uint32_t result;
131
132 struct vscsibk_info *info;
133 struct v2p_entry *v2p;
134 struct scatterlist *sgl;
135
136 uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
137
138 grant_handle_t grant_handles[VSCSI_MAX_GRANTS];
139 struct page *pages[VSCSI_MAX_GRANTS];
140
141 struct se_cmd se_cmd;
142};
143
144struct scsiback_tmr {
145 atomic_t tmr_complete;
146 wait_queue_head_t tmr_wait;
147};
148
149struct scsiback_nexus {
150 /* Pointer to TCM session for I_T Nexus */
151 struct se_session *tvn_se_sess;
152};
153
154struct scsiback_tport {
155 /* SCSI protocol the tport is providing */
156 u8 tport_proto_id;
157 /* Binary World Wide unique Port Name for pvscsi Target port */
158 u64 tport_wwpn;
159 /* ASCII formatted WWPN for pvscsi Target port */
160 char tport_name[VSCSI_NAMELEN];
161 /* Returned by scsiback_make_tport() */
162 struct se_wwn tport_wwn;
163};
164
165struct scsiback_tpg {
166 /* scsiback port target portal group tag for TCM */
167 u16 tport_tpgt;
168 /* track number of TPG Port/Lun Links wrt explicit I_T Nexus shutdown */
169 int tv_tpg_port_count;
170 /* xen-pvscsi references to tpg_nexus, protected by tv_tpg_mutex */
171 int tv_tpg_fe_count;
172 /* list for scsiback_list */
173 struct list_head tv_tpg_list;
174 /* Used to protect access for tpg_nexus */
175 struct mutex tv_tpg_mutex;
176 /* Pointer to the TCM pvscsi I_T Nexus for this TPG endpoint */
177 struct scsiback_nexus *tpg_nexus;
178 /* Pointer back to scsiback_tport */
179 struct scsiback_tport *tport;
180 /* Returned by scsiback_make_tpg() */
181 struct se_portal_group se_tpg;
182 /* alias used in xenstore */
183 char param_alias[VSCSI_NAMELEN];
184 /* list of info structures related to this target portal group */
185 struct list_head info_list;
186};
187
188#define SCSIBACK_INVALID_HANDLE (~0)
189
190static bool log_print_stat;
191module_param(log_print_stat, bool, 0644);
192
193static int scsiback_max_buffer_pages = 1024;
194module_param_named(max_buffer_pages, scsiback_max_buffer_pages, int, 0644);
195MODULE_PARM_DESC(max_buffer_pages,
196"Maximum number of free pages to keep in backend buffer");
197
198static struct kmem_cache *scsiback_cachep;
199static DEFINE_SPINLOCK(free_pages_lock);
200static int free_pages_num;
201static LIST_HEAD(scsiback_free_pages);
202
203/* Global spinlock to protect scsiback TPG list */
204static DEFINE_MUTEX(scsiback_mutex);
205static LIST_HEAD(scsiback_list);
206
207/* Local pointer to allocated TCM configfs fabric module */
208static struct target_fabric_configfs *scsiback_fabric_configfs;
209
210static void scsiback_get(struct vscsibk_info *info)
211{
212 atomic_inc(&info->nr_unreplied_reqs);
213}
214
215static void scsiback_put(struct vscsibk_info *info)
216{
217 if (atomic_dec_and_test(&info->nr_unreplied_reqs))
218 wake_up(&info->waiting_to_free);
219}
220
221static void put_free_pages(struct page **page, int num)
222{
223 unsigned long flags;
224 int i = free_pages_num + num, n = num;
225
226 if (num == 0)
227 return;
228 if (i > scsiback_max_buffer_pages) {
229 n = min(num, i - scsiback_max_buffer_pages);
230 free_xenballooned_pages(n, page + num - n);
231 n = num - n;
232 }
233 spin_lock_irqsave(&free_pages_lock, flags);
234 for (i = 0; i < n; i++)
235 list_add(&page[i]->lru, &scsiback_free_pages);
236 free_pages_num += n;
237 spin_unlock_irqrestore(&free_pages_lock, flags);
238}
239
240static int get_free_page(struct page **page)
241{
242 unsigned long flags;
243
244 spin_lock_irqsave(&free_pages_lock, flags);
245 if (list_empty(&scsiback_free_pages)) {
246 spin_unlock_irqrestore(&free_pages_lock, flags);
247 return alloc_xenballooned_pages(1, page, false);
248 }
249 page[0] = list_first_entry(&scsiback_free_pages, struct page, lru);
250 list_del(&page[0]->lru);
251 free_pages_num--;
252 spin_unlock_irqrestore(&free_pages_lock, flags);
253 return 0;
254}
255
256static unsigned long vaddr_page(struct page *page)
257{
258 unsigned long pfn = page_to_pfn(page);
259
260 return (unsigned long)pfn_to_kaddr(pfn);
261}
262
263static unsigned long vaddr(struct vscsibk_pend *req, int seg)
264{
265 return vaddr_page(req->pages[seg]);
266}
267
268static void scsiback_print_status(char *sense_buffer, int errors,
269 struct vscsibk_pend *pending_req)
270{
271 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
272
273 pr_err("xen-pvscsi[%s:%d] cmnd[0]=%02x -> st=%02x msg=%02x host=%02x drv=%02x\n",
274 tpg->tport->tport_name, pending_req->v2p->lun,
275 pending_req->cmnd[0], status_byte(errors), msg_byte(errors),
276 host_byte(errors), driver_byte(errors));
277
278 if (CHECK_CONDITION & status_byte(errors))
279 __scsi_print_sense("xen-pvscsi", sense_buffer,
280 SCSI_SENSE_BUFFERSIZE);
281}
282
283static void scsiback_fast_flush_area(struct vscsibk_pend *req)
284{
285 struct gnttab_unmap_grant_ref unmap[VSCSI_GRANT_BATCH];
286 struct page *pages[VSCSI_GRANT_BATCH];
287 unsigned int i, invcount = 0;
288 grant_handle_t handle;
289 int err;
290
291 kfree(req->sgl);
292 req->sgl = NULL;
293 req->n_sg = 0;
294
295 if (!req->n_grants)
296 return;
297
298 for (i = 0; i < req->n_grants; i++) {
299 handle = req->grant_handles[i];
300 if (handle == SCSIBACK_INVALID_HANDLE)
301 continue;
302 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
303 GNTMAP_host_map, handle);
304 req->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
305 pages[invcount] = req->pages[i];
306 put_page(pages[invcount]);
307 invcount++;
308 if (invcount < VSCSI_GRANT_BATCH)
309 continue;
310 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
311 BUG_ON(err);
312 invcount = 0;
313 }
314
315 if (invcount) {
316 err = gnttab_unmap_refs(unmap, NULL, pages, invcount);
317 BUG_ON(err);
318 }
319
320 put_free_pages(req->pages, req->n_grants);
321 req->n_grants = 0;
322}
323
324static void scsiback_free_translation_entry(struct kref *kref)
325{
326 struct v2p_entry *entry = container_of(kref, struct v2p_entry, kref);
327 struct scsiback_tpg *tpg = entry->tpg;
328
329 mutex_lock(&tpg->tv_tpg_mutex);
330 tpg->tv_tpg_fe_count--;
331 mutex_unlock(&tpg->tv_tpg_mutex);
332
333 kfree(entry);
334}
335
336static void scsiback_do_resp_with_sense(char *sense_buffer, int32_t result,
337 uint32_t resid, struct vscsibk_pend *pending_req)
338{
339 struct vscsiif_response *ring_res;
340 struct vscsibk_info *info = pending_req->info;
341 int notify;
342 struct scsi_sense_hdr sshdr;
343 unsigned long flags;
344 unsigned len;
345
346 spin_lock_irqsave(&info->ring_lock, flags);
347
348 ring_res = RING_GET_RESPONSE(&info->ring, info->ring.rsp_prod_pvt);
349 info->ring.rsp_prod_pvt++;
350
351 ring_res->rslt = result;
352 ring_res->rqid = pending_req->rqid;
353
354 if (sense_buffer != NULL &&
355 scsi_normalize_sense(sense_buffer, VSCSIIF_SENSE_BUFFERSIZE,
356 &sshdr)) {
357 len = min_t(unsigned, 8 + sense_buffer[7],
358 VSCSIIF_SENSE_BUFFERSIZE);
359 memcpy(ring_res->sense_buffer, sense_buffer, len);
360 ring_res->sense_len = len;
361 } else {
362 ring_res->sense_len = 0;
363 }
364
365 ring_res->residual_len = resid;
366
367 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&info->ring, notify);
368 spin_unlock_irqrestore(&info->ring_lock, flags);
369
370 if (notify)
371 notify_remote_via_irq(info->irq);
372
373 if (pending_req->v2p)
374 kref_put(&pending_req->v2p->kref,
375 scsiback_free_translation_entry);
376}
377
378static void scsiback_cmd_done(struct vscsibk_pend *pending_req)
379{
380 struct vscsibk_info *info = pending_req->info;
381 unsigned char *sense_buffer;
382 unsigned int resid;
383 int errors;
384
385 sense_buffer = pending_req->sense_buffer;
386 resid = pending_req->se_cmd.residual_count;
387 errors = pending_req->result;
388
389 if (errors && log_print_stat)
390 scsiback_print_status(sense_buffer, errors, pending_req);
391
392 scsiback_fast_flush_area(pending_req);
393 scsiback_do_resp_with_sense(sense_buffer, errors, resid, pending_req);
394 scsiback_put(info);
395}
396
397static void scsiback_cmd_exec(struct vscsibk_pend *pending_req)
398{
399 struct se_cmd *se_cmd = &pending_req->se_cmd;
400 struct se_session *sess = pending_req->v2p->tpg->tpg_nexus->tvn_se_sess;
401 int rc;
402
403 memset(pending_req->sense_buffer, 0, VSCSIIF_SENSE_BUFFERSIZE);
404
405 memset(se_cmd, 0, sizeof(*se_cmd));
406
407 scsiback_get(pending_req->info);
408 rc = target_submit_cmd_map_sgls(se_cmd, sess, pending_req->cmnd,
409 pending_req->sense_buffer, pending_req->v2p->lun,
410 pending_req->data_len, 0,
411 pending_req->sc_data_direction, 0,
412 pending_req->sgl, pending_req->n_sg,
413 NULL, 0, NULL, 0);
414 if (rc < 0) {
415 transport_send_check_condition_and_sense(se_cmd,
416 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
417 transport_generic_free_cmd(se_cmd, 0);
418 }
419}
420
421static int scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref *map,
422 struct page **pg, grant_handle_t *grant, int cnt)
423{
424 int err, i;
425
426 if (!cnt)
427 return 0;
428
429 err = gnttab_map_refs(map, NULL, pg, cnt);
430 BUG_ON(err);
431 for (i = 0; i < cnt; i++) {
432 if (unlikely(map[i].status != GNTST_okay)) {
433 pr_err("xen-pvscsi: invalid buffer -- could not remap it\n");
434 map[i].handle = SCSIBACK_INVALID_HANDLE;
435 err = -ENOMEM;
436 } else {
437 get_page(pg[i]);
438 }
439 grant[i] = map[i].handle;
440 }
441 return err;
442}
443
444static int scsiback_gnttab_data_map_list(struct vscsibk_pend *pending_req,
445 struct scsiif_request_segment *seg, struct page **pg,
446 grant_handle_t *grant, int cnt, u32 flags)
447{
448 int mapcount = 0, i, err = 0;
449 struct gnttab_map_grant_ref map[VSCSI_GRANT_BATCH];
450 struct vscsibk_info *info = pending_req->info;
451
452 for (i = 0; i < cnt; i++) {
453 if (get_free_page(pg + mapcount)) {
454 put_free_pages(pg, mapcount);
455 pr_err("xen-pvscsi: no grant page\n");
456 return -ENOMEM;
457 }
458 gnttab_set_map_op(&map[mapcount], vaddr_page(pg[mapcount]),
459 flags, seg[i].gref, info->domid);
460 mapcount++;
461 if (mapcount < VSCSI_GRANT_BATCH)
462 continue;
463 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
464 pg += mapcount;
465 grant += mapcount;
466 pending_req->n_grants += mapcount;
467 if (err)
468 return err;
469 mapcount = 0;
470 }
471 err = scsiback_gnttab_data_map_batch(map, pg, grant, mapcount);
472 pending_req->n_grants += mapcount;
473 return err;
474}
475
476static int scsiback_gnttab_data_map(struct vscsiif_request *ring_req,
477 struct vscsibk_pend *pending_req)
478{
479 u32 flags;
480 int i, err, n_segs, i_seg = 0;
481 struct page **pg;
482 struct scsiif_request_segment *seg;
483 unsigned long end_seg = 0;
484 unsigned int nr_segments = (unsigned int)ring_req->nr_segments;
485 unsigned int nr_sgl = 0;
486 struct scatterlist *sg;
487 grant_handle_t *grant;
488
489 pending_req->n_sg = 0;
490 pending_req->n_grants = 0;
491 pending_req->data_len = 0;
492
493 nr_segments &= ~VSCSIIF_SG_GRANT;
494 if (!nr_segments)
495 return 0;
496
497 if (nr_segments > VSCSIIF_SG_TABLESIZE) {
498 DPRINTK("xen-pvscsi: invalid parameter nr_seg = %d\n",
499 ring_req->nr_segments);
500 return -EINVAL;
501 }
502
503 if (ring_req->nr_segments & VSCSIIF_SG_GRANT) {
504 err = scsiback_gnttab_data_map_list(pending_req, ring_req->seg,
505 pending_req->pages, pending_req->grant_handles,
506 nr_segments, GNTMAP_host_map | GNTMAP_readonly);
507 if (err)
508 return err;
509 nr_sgl = nr_segments;
510 nr_segments = 0;
511 for (i = 0; i < nr_sgl; i++) {
512 n_segs = ring_req->seg[i].length /
513 sizeof(struct scsiif_request_segment);
514 if ((unsigned)ring_req->seg[i].offset +
515 (unsigned)ring_req->seg[i].length > PAGE_SIZE ||
516 n_segs * sizeof(struct scsiif_request_segment) !=
517 ring_req->seg[i].length)
518 return -EINVAL;
519 nr_segments += n_segs;
520 }
521 if (nr_segments > SG_ALL) {
522 DPRINTK("xen-pvscsi: invalid nr_seg = %d\n",
523 nr_segments);
524 return -EINVAL;
525 }
526 }
527
528 /* free of (sgl) in fast_flush_area()*/
529 pending_req->sgl = kmalloc_array(nr_segments,
530 sizeof(struct scatterlist), GFP_KERNEL);
531 if (!pending_req->sgl)
532 return -ENOMEM;
533
534 sg_init_table(pending_req->sgl, nr_segments);
535 pending_req->n_sg = nr_segments;
536
537 flags = GNTMAP_host_map;
538 if (pending_req->sc_data_direction == DMA_TO_DEVICE)
539 flags |= GNTMAP_readonly;
540
541 pg = pending_req->pages + nr_sgl;
542 grant = pending_req->grant_handles + nr_sgl;
543 if (!nr_sgl) {
544 seg = ring_req->seg;
545 err = scsiback_gnttab_data_map_list(pending_req, seg,
546 pg, grant, nr_segments, flags);
547 if (err)
548 return err;
549 } else {
550 for (i = 0; i < nr_sgl; i++) {
551 seg = (struct scsiif_request_segment *)(
552 vaddr(pending_req, i) + ring_req->seg[i].offset);
553 n_segs = ring_req->seg[i].length /
554 sizeof(struct scsiif_request_segment);
555 err = scsiback_gnttab_data_map_list(pending_req, seg,
556 pg, grant, n_segs, flags);
557 if (err)
558 return err;
559 pg += n_segs;
560 grant += n_segs;
561 }
562 end_seg = vaddr(pending_req, 0) + ring_req->seg[0].offset;
563 seg = (struct scsiif_request_segment *)end_seg;
564 end_seg += ring_req->seg[0].length;
565 pg = pending_req->pages + nr_sgl;
566 }
567
568 for_each_sg(pending_req->sgl, sg, nr_segments, i) {
569 sg_set_page(sg, pg[i], seg->length, seg->offset);
570 pending_req->data_len += seg->length;
571 seg++;
572 if (nr_sgl && (unsigned long)seg >= end_seg) {
573 i_seg++;
574 end_seg = vaddr(pending_req, i_seg) +
575 ring_req->seg[i_seg].offset;
576 seg = (struct scsiif_request_segment *)end_seg;
577 end_seg += ring_req->seg[i_seg].length;
578 }
579 if (sg->offset >= PAGE_SIZE ||
580 sg->length > PAGE_SIZE ||
581 sg->offset + sg->length > PAGE_SIZE)
582 return -EINVAL;
583 }
584
585 return 0;
586}
587
588static void scsiback_disconnect(struct vscsibk_info *info)
589{
590 wait_event(info->waiting_to_free,
591 atomic_read(&info->nr_unreplied_reqs) == 0);
592
593 unbind_from_irqhandler(info->irq, info);
594 info->irq = 0;
595 xenbus_unmap_ring_vfree(info->dev, info->ring.sring);
596}
597
598static void scsiback_device_action(struct vscsibk_pend *pending_req,
599 enum tcm_tmreq_table act, int tag)
600{
601 int rc, err = FAILED;
602 struct scsiback_tpg *tpg = pending_req->v2p->tpg;
603 struct se_cmd *se_cmd = &pending_req->se_cmd;
604 struct scsiback_tmr *tmr;
605
606 tmr = kzalloc(sizeof(struct scsiback_tmr), GFP_KERNEL);
607 if (!tmr)
608 goto out;
609
610 init_waitqueue_head(&tmr->tmr_wait);
611
612 transport_init_se_cmd(se_cmd, tpg->se_tpg.se_tpg_tfo,
613 tpg->tpg_nexus->tvn_se_sess, 0, DMA_NONE, MSG_SIMPLE_TAG,
614 &pending_req->sense_buffer[0]);
615
616 rc = core_tmr_alloc_req(se_cmd, tmr, act, GFP_KERNEL);
617 if (rc < 0)
618 goto out;
619
620 se_cmd->se_tmr_req->ref_task_tag = tag;
621
622 if (transport_lookup_tmr_lun(se_cmd, pending_req->v2p->lun) < 0)
623 goto out;
624
625 transport_generic_handle_tmr(se_cmd);
626 wait_event(tmr->tmr_wait, atomic_read(&tmr->tmr_complete));
627
628 err = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
629 SUCCESS : FAILED;
630
631out:
632 if (tmr) {
633 transport_generic_free_cmd(&pending_req->se_cmd, 1);
634 kfree(tmr);
635 }
636
637 scsiback_do_resp_with_sense(NULL, err, 0, pending_req);
638
639 kmem_cache_free(scsiback_cachep, pending_req);
640}
641
642/*
643 Perform virtual to physical translation
644*/
645static struct v2p_entry *scsiback_do_translation(struct vscsibk_info *info,
646 struct ids_tuple *v)
647{
648 struct v2p_entry *entry;
649 struct list_head *head = &(info->v2p_entry_lists);
650 unsigned long flags;
651
652 spin_lock_irqsave(&info->v2p_lock, flags);
653 list_for_each_entry(entry, head, l) {
654 if ((entry->v.chn == v->chn) &&
655 (entry->v.tgt == v->tgt) &&
656 (entry->v.lun == v->lun)) {
657 kref_get(&entry->kref);
658 goto out;
659 }
660 }
661 entry = NULL;
662
663out:
664 spin_unlock_irqrestore(&info->v2p_lock, flags);
665 return entry;
666}
667
668static int prepare_pending_reqs(struct vscsibk_info *info,
669 struct vscsiif_request *ring_req,
670 struct vscsibk_pend *pending_req)
671{
672 struct v2p_entry *v2p;
673 struct ids_tuple vir;
674
675 pending_req->rqid = ring_req->rqid;
676 pending_req->info = info;
677
678 vir.chn = ring_req->channel;
679 vir.tgt = ring_req->id;
680 vir.lun = ring_req->lun;
681
682 v2p = scsiback_do_translation(info, &vir);
683 if (!v2p) {
684 pending_req->v2p = NULL;
685 DPRINTK("xen-pvscsi: doesn't exist.\n");
686 return -ENODEV;
687 }
688 pending_req->v2p = v2p;
689
690 /* request range check from frontend */
691 pending_req->sc_data_direction = ring_req->sc_data_direction;
692 if ((pending_req->sc_data_direction != DMA_BIDIRECTIONAL) &&
693 (pending_req->sc_data_direction != DMA_TO_DEVICE) &&
694 (pending_req->sc_data_direction != DMA_FROM_DEVICE) &&
695 (pending_req->sc_data_direction != DMA_NONE)) {
696 DPRINTK("xen-pvscsi: invalid parameter data_dir = %d\n",
697 pending_req->sc_data_direction);
698 return -EINVAL;
699 }
700
701 pending_req->cmd_len = ring_req->cmd_len;
702 if (pending_req->cmd_len > VSCSIIF_MAX_COMMAND_SIZE) {
703 DPRINTK("xen-pvscsi: invalid parameter cmd_len = %d\n",
704 pending_req->cmd_len);
705 return -EINVAL;
706 }
707 memcpy(pending_req->cmnd, ring_req->cmnd, pending_req->cmd_len);
708
709 return 0;
710}
711
712static int scsiback_do_cmd_fn(struct vscsibk_info *info)
713{
714 struct vscsiif_back_ring *ring = &info->ring;
715 struct vscsiif_request *ring_req;
716 struct vscsibk_pend *pending_req;
717 RING_IDX rc, rp;
718 int err, more_to_do;
719 uint32_t result;
720 uint8_t act;
721
722 rc = ring->req_cons;
723 rp = ring->sring->req_prod;
724 rmb(); /* guest system is accessing ring, too */
725
726 if (RING_REQUEST_PROD_OVERFLOW(ring, rp)) {
727 rc = ring->rsp_prod_pvt;
728 pr_warn("xen-pvscsi: Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
729 info->domid, rp, rc, rp - rc);
730 info->ring_error = 1;
731 return 0;
732 }
733
734 while ((rc != rp)) {
735 if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
736 break;
737 pending_req = kmem_cache_alloc(scsiback_cachep, GFP_KERNEL);
738 if (!pending_req)
739 return 1;
740
741 ring_req = RING_GET_REQUEST(ring, rc);
742 ring->req_cons = ++rc;
743
744 act = ring_req->act;
745 err = prepare_pending_reqs(info, ring_req, pending_req);
746 if (err) {
747 switch (err) {
748 case -ENODEV:
749 result = DID_NO_CONNECT;
750 break;
751 default:
752 result = DRIVER_ERROR;
753 break;
754 }
755 scsiback_do_resp_with_sense(NULL, result << 24, 0,
756 pending_req);
757 kmem_cache_free(scsiback_cachep, pending_req);
758 return 1;
759 }
760
761 switch (act) {
762 case VSCSIIF_ACT_SCSI_CDB:
763 if (scsiback_gnttab_data_map(ring_req, pending_req)) {
764 scsiback_fast_flush_area(pending_req);
765 scsiback_do_resp_with_sense(NULL,
766 DRIVER_ERROR << 24, 0, pending_req);
767 kmem_cache_free(scsiback_cachep, pending_req);
768 } else {
769 scsiback_cmd_exec(pending_req);
770 }
771 break;
772 case VSCSIIF_ACT_SCSI_ABORT:
773 scsiback_device_action(pending_req, TMR_ABORT_TASK,
774 ring_req->ref_rqid);
775 break;
776 case VSCSIIF_ACT_SCSI_RESET:
777 scsiback_device_action(pending_req, TMR_LUN_RESET, 0);
778 break;
779 default:
780 pr_err_ratelimited("xen-pvscsi: invalid request\n");
781 scsiback_do_resp_with_sense(NULL, DRIVER_ERROR << 24,
782 0, pending_req);
783 kmem_cache_free(scsiback_cachep, pending_req);
784 break;
785 }
786
787 /* Yield point for this unbounded loop. */
788 cond_resched();
789 }
790
791 RING_FINAL_CHECK_FOR_REQUESTS(&info->ring, more_to_do);
792 return more_to_do;
793}
794
795static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
796{
797 struct vscsibk_info *info = dev_id;
798
799 if (info->ring_error)
800 return IRQ_HANDLED;
801
802 while (scsiback_do_cmd_fn(info))
803 cond_resched();
804
805 return IRQ_HANDLED;
806}
807
808static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
809 evtchn_port_t evtchn)
810{
811 void *area;
812 struct vscsiif_sring *sring;
813 int err;
814
815 if (info->irq)
816 return -1;
817
818 err = xenbus_map_ring_valloc(info->dev, ring_ref, &area);
819 if (err)
820 return err;
821
822 sring = (struct vscsiif_sring *)area;
823 BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
824
825 err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
826 if (err < 0)
827 goto unmap_page;
828
829 info->irq = err;
830
831 err = request_threaded_irq(info->irq, NULL, scsiback_irq_fn,
832 IRQF_ONESHOT, "vscsiif-backend", info);
833 if (err)
834 goto free_irq;
835
836 return 0;
837
838free_irq:
839 unbind_from_irqhandler(info->irq, info);
840 info->irq = 0;
841unmap_page:
842 xenbus_unmap_ring_vfree(info->dev, area);
843
844 return err;
845}
846
847static int scsiback_map(struct vscsibk_info *info)
848{
849 struct xenbus_device *dev = info->dev;
850 unsigned int ring_ref, evtchn;
851 int err;
852
853 err = xenbus_gather(XBT_NIL, dev->otherend,
854 "ring-ref", "%u", &ring_ref,
855 "event-channel", "%u", &evtchn, NULL);
856 if (err) {
857 xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
858 return err;
859 }
860
861 return scsiback_init_sring(info, ring_ref, evtchn);
862}
863
864/*
865 Add a new translation entry
866*/
867static int scsiback_add_translation_entry(struct vscsibk_info *info,
868 char *phy, struct ids_tuple *v)
869{
870 int err = 0;
871 struct v2p_entry *entry;
872 struct v2p_entry *new;
873 struct list_head *head = &(info->v2p_entry_lists);
874 unsigned long flags;
875 char *lunp;
876 unsigned int lun;
877 struct scsiback_tpg *tpg_entry, *tpg = NULL;
878 char *error = "doesn't exist";
879
880 lunp = strrchr(phy, ':');
881 if (!lunp) {
882 pr_err("xen-pvscsi: illegal format of physical device %s\n",
883 phy);
884 return -EINVAL;
885 }
886 *lunp = 0;
887 lunp++;
888 if (kstrtouint(lunp, 10, &lun) || lun >= TRANSPORT_MAX_LUNS_PER_TPG) {
889 pr_err("xen-pvscsi: lun number not valid: %s\n", lunp);
890 return -EINVAL;
891 }
892
893 mutex_lock(&scsiback_mutex);
894 list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
895 if (!strcmp(phy, tpg_entry->tport->tport_name) ||
896 !strcmp(phy, tpg_entry->param_alias)) {
897 spin_lock(&tpg_entry->se_tpg.tpg_lun_lock);
898 if (tpg_entry->se_tpg.tpg_lun_list[lun]->lun_status ==
899 TRANSPORT_LUN_STATUS_ACTIVE) {
900 if (!tpg_entry->tpg_nexus)
901 error = "nexus undefined";
902 else
903 tpg = tpg_entry;
904 }
905 spin_unlock(&tpg_entry->se_tpg.tpg_lun_lock);
906 break;
907 }
908 }
909 if (tpg) {
910 mutex_lock(&tpg->tv_tpg_mutex);
911 tpg->tv_tpg_fe_count++;
912 mutex_unlock(&tpg->tv_tpg_mutex);
913 }
914 mutex_unlock(&scsiback_mutex);
915
916 if (!tpg) {
917 pr_err("xen-pvscsi: %s:%d %s\n", phy, lun, error);
918 return -ENODEV;
919 }
920
921 new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
922 if (new == NULL) {
923 err = -ENOMEM;
924 goto out_free;
925 }
926
927 spin_lock_irqsave(&info->v2p_lock, flags);
928
929 /* Check double assignment to identical virtual ID */
930 list_for_each_entry(entry, head, l) {
931 if ((entry->v.chn == v->chn) &&
932 (entry->v.tgt == v->tgt) &&
933 (entry->v.lun == v->lun)) {
934 pr_warn("xen-pvscsi: Virtual ID is already used. Assignment was not performed.\n");
935 err = -EEXIST;
936 goto out;
937 }
938
939 }
940
941 /* Create a new translation entry and add to the list */
942 kref_init(&new->kref);
943 new->v = *v;
944 new->tpg = tpg;
945 new->lun = lun;
946 list_add_tail(&new->l, head);
947
948out:
949 spin_unlock_irqrestore(&info->v2p_lock, flags);
950
951out_free:
952 mutex_lock(&tpg->tv_tpg_mutex);
953 tpg->tv_tpg_fe_count--;
954 mutex_unlock(&tpg->tv_tpg_mutex);
955
956 if (err)
957 kfree(new);
958
959 return err;
960}
961
962static void __scsiback_del_translation_entry(struct v2p_entry *entry)
963{
964 list_del(&entry->l);
965 kref_put(&entry->kref, scsiback_free_translation_entry);
966}
967
968/*
969 Delete the translation entry specfied
970*/
971static int scsiback_del_translation_entry(struct vscsibk_info *info,
972 struct ids_tuple *v)
973{
974 struct v2p_entry *entry;
975 struct list_head *head = &(info->v2p_entry_lists);
976 unsigned long flags;
977
978 spin_lock_irqsave(&info->v2p_lock, flags);
979 /* Find out the translation entry specified */
980 list_for_each_entry(entry, head, l) {
981 if ((entry->v.chn == v->chn) &&
982 (entry->v.tgt == v->tgt) &&
983 (entry->v.lun == v->lun)) {
984 goto found;
985 }
986 }
987
988 spin_unlock_irqrestore(&info->v2p_lock, flags);
989 return 1;
990
991found:
992 /* Delete the translation entry specfied */
993 __scsiback_del_translation_entry(entry);
994
995 spin_unlock_irqrestore(&info->v2p_lock, flags);
996 return 0;
997}
998
999static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
1000 char *phy, struct ids_tuple *vir)
1001{
1002 if (!scsiback_add_translation_entry(info, phy, vir)) {
1003 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1004 "%d", XenbusStateInitialised)) {
1005 pr_err("xen-pvscsi: xenbus_printf error %s\n", state);
1006 scsiback_del_translation_entry(info, vir);
1007 }
1008 } else {
1009 xenbus_printf(XBT_NIL, info->dev->nodename, state,
1010 "%d", XenbusStateClosed);
1011 }
1012}
1013
1014static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1015 struct ids_tuple *vir)
1016{
1017 if (!scsiback_del_translation_entry(info, vir)) {
1018 if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1019 "%d", XenbusStateClosed))
1020 pr_err("xen-pvscsi: xenbus_printf error %s\n", state);
1021 }
1022}
1023
1024#define VSCSIBACK_OP_ADD_OR_DEL_LUN 1
1025#define VSCSIBACK_OP_UPDATEDEV_STATE 2
1026
1027static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1028 char *ent)
1029{
1030 int err;
1031 struct ids_tuple vir;
1032 char *val;
1033 int device_state;
1034 char phy[VSCSI_NAMELEN];
1035 char str[64];
1036 char state[64];
1037 struct xenbus_device *dev = info->dev;
1038
1039 /* read status */
1040 snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1041 err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1042 if (XENBUS_EXIST_ERR(err))
1043 return;
1044
1045 /* physical SCSI device */
1046 snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1047 val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1048 if (IS_ERR(val)) {
1049 xenbus_printf(XBT_NIL, dev->nodename, state,
1050 "%d", XenbusStateClosed);
1051 return;
1052 }
1053 strlcpy(phy, val, VSCSI_NAMELEN);
1054 kfree(val);
1055
1056 /* virtual SCSI device */
1057 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1058 err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1059 &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1060 if (XENBUS_EXIST_ERR(err)) {
1061 xenbus_printf(XBT_NIL, dev->nodename, state,
1062 "%d", XenbusStateClosed);
1063 return;
1064 }
1065
1066 switch (op) {
1067 case VSCSIBACK_OP_ADD_OR_DEL_LUN:
1068 if (device_state == XenbusStateInitialising)
1069 scsiback_do_add_lun(info, state, phy, &vir);
1070 if (device_state == XenbusStateClosing)
1071 scsiback_do_del_lun(info, state, &vir);
1072 break;
1073
1074 case VSCSIBACK_OP_UPDATEDEV_STATE:
1075 if (device_state == XenbusStateInitialised) {
1076 /* modify vscsi-devs/dev-x/state */
1077 if (xenbus_printf(XBT_NIL, dev->nodename, state,
1078 "%d", XenbusStateConnected)) {
1079 pr_err("xen-pvscsi: xenbus_printf error %s\n",
1080 str);
1081 scsiback_del_translation_entry(info, &vir);
1082 xenbus_printf(XBT_NIL, dev->nodename, state,
1083 "%d", XenbusStateClosed);
1084 }
1085 }
1086 break;
1087 /*When it is necessary, processing is added here.*/
1088 default:
1089 break;
1090 }
1091}
1092
1093static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1094{
1095 int i;
1096 char **dir;
1097 unsigned int ndir = 0;
1098
1099 dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1100 &ndir);
1101 if (IS_ERR(dir))
1102 return;
1103
1104 for (i = 0; i < ndir; i++)
1105 scsiback_do_1lun_hotplug(info, op, dir[i]);
1106
1107 kfree(dir);
1108}
1109
1110static void scsiback_frontend_changed(struct xenbus_device *dev,
1111 enum xenbus_state frontend_state)
1112{
1113 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1114
1115 switch (frontend_state) {
1116 case XenbusStateInitialising:
1117 break;
1118
1119 case XenbusStateInitialised:
1120 if (scsiback_map(info))
1121 break;
1122
1123 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1124 xenbus_switch_state(dev, XenbusStateConnected);
1125 break;
1126
1127 case XenbusStateConnected:
1128 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1129
1130 if (dev->state == XenbusStateConnected)
1131 break;
1132
1133 xenbus_switch_state(dev, XenbusStateConnected);
1134 break;
1135
1136 case XenbusStateClosing:
1137 if (info->irq)
1138 scsiback_disconnect(info);
1139
1140 xenbus_switch_state(dev, XenbusStateClosing);
1141 break;
1142
1143 case XenbusStateClosed:
1144 xenbus_switch_state(dev, XenbusStateClosed);
1145 if (xenbus_dev_is_online(dev))
1146 break;
1147 /* fall through if not online */
1148 case XenbusStateUnknown:
1149 device_unregister(&dev->dev);
1150 break;
1151
1152 case XenbusStateReconfiguring:
1153 scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1154 xenbus_switch_state(dev, XenbusStateReconfigured);
1155
1156 break;
1157
1158 default:
1159 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1160 frontend_state);
1161 break;
1162 }
1163}
1164
1165/*
1166 Release the translation entry specfied
1167*/
1168static void scsiback_release_translation_entry(struct vscsibk_info *info)
1169{
1170 struct v2p_entry *entry, *tmp;
1171 struct list_head *head = &(info->v2p_entry_lists);
1172 unsigned long flags;
1173
1174 spin_lock_irqsave(&info->v2p_lock, flags);
1175
1176 list_for_each_entry_safe(entry, tmp, head, l)
1177 __scsiback_del_translation_entry(entry);
1178
1179 spin_unlock_irqrestore(&info->v2p_lock, flags);
1180}
1181
1182static int scsiback_remove(struct xenbus_device *dev)
1183{
1184 struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1185
1186 if (info->irq)
1187 scsiback_disconnect(info);
1188
1189 scsiback_release_translation_entry(info);
1190
1191 dev_set_drvdata(&dev->dev, NULL);
1192
1193 return 0;
1194}
1195
1196static int scsiback_probe(struct xenbus_device *dev,
1197 const struct xenbus_device_id *id)
1198{
1199 int err;
1200
1201 struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1202 GFP_KERNEL);
1203
1204 DPRINTK("%p %d\n", dev, dev->otherend_id);
1205
1206 if (!info) {
1207 xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1208 return -ENOMEM;
1209 }
1210 info->dev = dev;
1211 dev_set_drvdata(&dev->dev, info);
1212
1213 info->domid = dev->otherend_id;
1214 spin_lock_init(&info->ring_lock);
1215 info->ring_error = 0;
1216 atomic_set(&info->nr_unreplied_reqs, 0);
1217 init_waitqueue_head(&info->waiting_to_free);
1218 info->dev = dev;
1219 info->irq = 0;
1220 INIT_LIST_HEAD(&info->v2p_entry_lists);
1221 spin_lock_init(&info->v2p_lock);
1222
1223 err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1224 SG_ALL);
1225 if (err)
1226 xenbus_dev_error(dev, err, "writing feature-sg-grant");
1227
1228 err = xenbus_switch_state(dev, XenbusStateInitWait);
1229 if (err)
1230 goto fail;
1231
1232 return 0;
1233
1234fail:
1235 pr_warn("xen-pvscsi: %s failed\n", __func__);
1236 scsiback_remove(dev);
1237
1238 return err;
1239}
1240
1241static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1242{
1243 switch (tport->tport_proto_id) {
1244 case SCSI_PROTOCOL_SAS:
1245 return "SAS";
1246 case SCSI_PROTOCOL_FCP:
1247 return "FCP";
1248 case SCSI_PROTOCOL_ISCSI:
1249 return "iSCSI";
1250 default:
1251 break;
1252 }
1253
1254 return "Unknown";
1255}
1256
1257static u8 scsiback_get_fabric_proto_ident(struct se_portal_group *se_tpg)
1258{
1259 struct scsiback_tpg *tpg = container_of(se_tpg,
1260 struct scsiback_tpg, se_tpg);
1261 struct scsiback_tport *tport = tpg->tport;
1262
1263 switch (tport->tport_proto_id) {
1264 case SCSI_PROTOCOL_SAS:
1265 return sas_get_fabric_proto_ident(se_tpg);
1266 case SCSI_PROTOCOL_FCP:
1267 return fc_get_fabric_proto_ident(se_tpg);
1268 case SCSI_PROTOCOL_ISCSI:
1269 return iscsi_get_fabric_proto_ident(se_tpg);
1270 default:
1271 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1272 tport->tport_proto_id);
1273 break;
1274 }
1275
1276 return sas_get_fabric_proto_ident(se_tpg);
1277}
1278
1279static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1280{
1281 struct scsiback_tpg *tpg = container_of(se_tpg,
1282 struct scsiback_tpg, se_tpg);
1283 struct scsiback_tport *tport = tpg->tport;
1284
1285 return &tport->tport_name[0];
1286}
1287
1288static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1289{
1290 struct scsiback_tpg *tpg = container_of(se_tpg,
1291 struct scsiback_tpg, se_tpg);
1292 return tpg->tport_tpgt;
1293}
1294
1295static u32 scsiback_get_default_depth(struct se_portal_group *se_tpg)
1296{
1297 return 1;
1298}
1299
1300static u32
1301scsiback_get_pr_transport_id(struct se_portal_group *se_tpg,
1302 struct se_node_acl *se_nacl,
1303 struct t10_pr_registration *pr_reg,
1304 int *format_code,
1305 unsigned char *buf)
1306{
1307 struct scsiback_tpg *tpg = container_of(se_tpg,
1308 struct scsiback_tpg, se_tpg);
1309 struct scsiback_tport *tport = tpg->tport;
1310
1311 switch (tport->tport_proto_id) {
1312 case SCSI_PROTOCOL_SAS:
1313 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1314 format_code, buf);
1315 case SCSI_PROTOCOL_FCP:
1316 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1317 format_code, buf);
1318 case SCSI_PROTOCOL_ISCSI:
1319 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1320 format_code, buf);
1321 default:
1322 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1323 tport->tport_proto_id);
1324 break;
1325 }
1326
1327 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
1328 format_code, buf);
1329}
1330
1331static u32
1332scsiback_get_pr_transport_id_len(struct se_portal_group *se_tpg,
1333 struct se_node_acl *se_nacl,
1334 struct t10_pr_registration *pr_reg,
1335 int *format_code)
1336{
1337 struct scsiback_tpg *tpg = container_of(se_tpg,
1338 struct scsiback_tpg, se_tpg);
1339 struct scsiback_tport *tport = tpg->tport;
1340
1341 switch (tport->tport_proto_id) {
1342 case SCSI_PROTOCOL_SAS:
1343 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1344 format_code);
1345 case SCSI_PROTOCOL_FCP:
1346 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1347 format_code);
1348 case SCSI_PROTOCOL_ISCSI:
1349 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1350 format_code);
1351 default:
1352 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1353 tport->tport_proto_id);
1354 break;
1355 }
1356
1357 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
1358 format_code);
1359}
1360
1361static char *
1362scsiback_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
1363 const char *buf,
1364 u32 *out_tid_len,
1365 char **port_nexus_ptr)
1366{
1367 struct scsiback_tpg *tpg = container_of(se_tpg,
1368 struct scsiback_tpg, se_tpg);
1369 struct scsiback_tport *tport = tpg->tport;
1370
1371 switch (tport->tport_proto_id) {
1372 case SCSI_PROTOCOL_SAS:
1373 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1374 port_nexus_ptr);
1375 case SCSI_PROTOCOL_FCP:
1376 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1377 port_nexus_ptr);
1378 case SCSI_PROTOCOL_ISCSI:
1379 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1380 port_nexus_ptr);
1381 default:
1382 pr_err("Unknown tport_proto_id: 0x%02x, using SAS emulation\n",
1383 tport->tport_proto_id);
1384 break;
1385 }
1386
1387 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
1388 port_nexus_ptr);
1389}
1390
1391static struct se_wwn *
1392scsiback_make_tport(struct target_fabric_configfs *tf,
1393 struct config_group *group,
1394 const char *name)
1395{
1396 struct scsiback_tport *tport;
1397 char *ptr;
1398 u64 wwpn = 0;
1399 int off = 0;
1400
1401 tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1402 if (!tport)
1403 return ERR_PTR(-ENOMEM);
1404
1405 tport->tport_wwpn = wwpn;
1406 /*
1407 * Determine the emulated Protocol Identifier and Target Port Name
1408 * based on the incoming configfs directory name.
1409 */
1410 ptr = strstr(name, "naa.");
1411 if (ptr) {
1412 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1413 goto check_len;
1414 }
1415 ptr = strstr(name, "fc.");
1416 if (ptr) {
1417 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1418 off = 3; /* Skip over "fc." */
1419 goto check_len;
1420 }
1421 ptr = strstr(name, "iqn.");
1422 if (ptr) {
1423 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1424 goto check_len;
1425 }
1426
1427 pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1428 kfree(tport);
1429 return ERR_PTR(-EINVAL);
1430
1431check_len:
1432 if (strlen(name) >= VSCSI_NAMELEN) {
1433 pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1434 scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1435 kfree(tport);
1436 return ERR_PTR(-EINVAL);
1437 }
1438 snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1439
1440 pr_debug("xen-pvscsi: Allocated emulated Target %s Address: %s\n",
1441 scsiback_dump_proto_id(tport), name);
1442
1443 return &tport->tport_wwn;
1444}
1445
1446static void scsiback_drop_tport(struct se_wwn *wwn)
1447{
1448 struct scsiback_tport *tport = container_of(wwn,
1449 struct scsiback_tport, tport_wwn);
1450
1451 pr_debug("xen-pvscsi: Deallocating emulated Target %s Address: %s\n",
1452 scsiback_dump_proto_id(tport), tport->tport_name);
1453
1454 kfree(tport);
1455}
1456
1457static struct se_node_acl *
1458scsiback_alloc_fabric_acl(struct se_portal_group *se_tpg)
1459{
1460 return kzalloc(sizeof(struct se_node_acl), GFP_KERNEL);
1461}
1462
1463static void
1464scsiback_release_fabric_acl(struct se_portal_group *se_tpg,
1465 struct se_node_acl *se_nacl)
1466{
1467 kfree(se_nacl);
1468}
1469
1470static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1471{
1472 return 1;
1473}
1474
1475static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1476{
1477 /*
1478 * Do not release struct se_cmd's containing a valid TMR
1479 * pointer. These will be released directly in scsiback_device_action()
1480 * with transport_generic_free_cmd().
1481 */
1482 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
1483 return 0;
1484
1485 transport_generic_free_cmd(se_cmd, 0);
1486 return 1;
1487}
1488
1489static void scsiback_release_cmd(struct se_cmd *se_cmd)
1490{
1491 struct vscsibk_pend *pending_req = container_of(se_cmd,
1492 struct vscsibk_pend, se_cmd);
1493
1494 kmem_cache_free(scsiback_cachep, pending_req);
1495}
1496
1497static int scsiback_shutdown_session(struct se_session *se_sess)
1498{
1499 return 0;
1500}
1501
1502static void scsiback_close_session(struct se_session *se_sess)
1503{
1504}
1505
1506static u32 scsiback_sess_get_index(struct se_session *se_sess)
1507{
1508 return 0;
1509}
1510
1511static int scsiback_write_pending(struct se_cmd *se_cmd)
1512{
1513 /* Go ahead and process the write immediately */
1514 target_execute_cmd(se_cmd);
1515
1516 return 0;
1517}
1518
1519static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1520{
1521 return 0;
1522}
1523
1524static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1525{
1526}
1527
1528static u32 scsiback_get_task_tag(struct se_cmd *se_cmd)
1529{
1530 struct vscsibk_pend *pending_req = container_of(se_cmd,
1531 struct vscsibk_pend, se_cmd);
1532
1533 return pending_req->rqid;
1534}
1535
1536static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1537{
1538 return 0;
1539}
1540
1541static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1542{
1543 struct vscsibk_pend *pending_req = container_of(se_cmd,
1544 struct vscsibk_pend, se_cmd);
1545
1546 pending_req->result = SAM_STAT_GOOD;
1547 scsiback_cmd_done(pending_req);
1548 return 0;
1549}
1550
1551static int scsiback_queue_status(struct se_cmd *se_cmd)
1552{
1553 struct vscsibk_pend *pending_req = container_of(se_cmd,
1554 struct vscsibk_pend, se_cmd);
1555
1556 if (se_cmd->sense_buffer &&
1557 ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1558 (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1559 pending_req->result = (DRIVER_SENSE << 24) |
1560 SAM_STAT_CHECK_CONDITION;
1561 else
1562 pending_req->result = se_cmd->scsi_status;
1563
1564 scsiback_cmd_done(pending_req);
1565 return 0;
1566}
1567
1568static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1569{
1570 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1571 struct scsiback_tmr *tmr = se_tmr->fabric_tmr_ptr;
1572
1573 atomic_set(&tmr->tmr_complete, 1);
1574 wake_up(&tmr->tmr_wait);
1575}
1576
1577static void scsiback_aborted_task(struct se_cmd *se_cmd)
1578{
1579}
1580
1581static ssize_t scsiback_tpg_param_show_alias(struct se_portal_group *se_tpg,
1582 char *page)
1583{
1584 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1585 se_tpg);
1586 ssize_t rb;
1587
1588 mutex_lock(&tpg->tv_tpg_mutex);
1589 rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1590 mutex_unlock(&tpg->tv_tpg_mutex);
1591
1592 return rb;
1593}
1594
1595static ssize_t scsiback_tpg_param_store_alias(struct se_portal_group *se_tpg,
1596 const char *page, size_t count)
1597{
1598 struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1599 se_tpg);
1600 int len;
1601
1602 if (strlen(page) >= VSCSI_NAMELEN) {
1603 pr_err("param alias: %s, exceeds max: %d\n", page,
1604 VSCSI_NAMELEN);
1605 return -EINVAL;
1606 }
1607
1608 mutex_lock(&tpg->tv_tpg_mutex);
1609 len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1610 if (tpg->param_alias[len - 1] == '\n')
1611 tpg->param_alias[len - 1] = '\0';
1612 mutex_unlock(&tpg->tv_tpg_mutex);
1613
1614 return count;
1615}
1616
1617TF_TPG_PARAM_ATTR(scsiback, alias, S_IRUGO | S_IWUSR);
1618
1619static struct configfs_attribute *scsiback_param_attrs[] = {
1620 &scsiback_tpg_param_alias.attr,
1621 NULL,
1622};
1623
1624static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1625 const char *name)
1626{
1627 struct se_portal_group *se_tpg;
1628 struct se_session *se_sess;
1629 struct scsiback_nexus *tv_nexus;
1630
1631 mutex_lock(&tpg->tv_tpg_mutex);
1632 if (tpg->tpg_nexus) {
1633 mutex_unlock(&tpg->tv_tpg_mutex);
1634 pr_debug("tpg->tpg_nexus already exists\n");
1635 return -EEXIST;
1636 }
1637 se_tpg = &tpg->se_tpg;
1638
1639 tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1640 if (!tv_nexus) {
1641 mutex_unlock(&tpg->tv_tpg_mutex);
1642 return -ENOMEM;
1643 }
1644 /*
1645 * Initialize the struct se_session pointer
1646 */
1647 tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
1648 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1649 mutex_unlock(&tpg->tv_tpg_mutex);
1650 kfree(tv_nexus);
1651 return -ENOMEM;
1652 }
1653 se_sess = tv_nexus->tvn_se_sess;
1654 /*
1655 * Since we are running in 'demo mode' this call with generate a
1656 * struct se_node_acl for the scsiback struct se_portal_group with
1657 * the SCSI Initiator port name of the passed configfs group 'name'.
1658 */
1659 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1660 se_tpg, (unsigned char *)name);
1661 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1662 mutex_unlock(&tpg->tv_tpg_mutex);
1663 pr_debug("core_tpg_check_initiator_node_acl() failed for %s\n",
1664 name);
1665 goto out;
1666 }
1667 /*
1668 * Now register the TCM pvscsi virtual I_T Nexus as active with the
1669 * call to __transport_register_session()
1670 */
1671 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1672 tv_nexus->tvn_se_sess, tv_nexus);
1673 tpg->tpg_nexus = tv_nexus;
1674
1675 mutex_unlock(&tpg->tv_tpg_mutex);
1676 return 0;
1677
1678out:
1679 transport_free_session(se_sess);
1680 kfree(tv_nexus);
1681 return -ENOMEM;
1682}
1683
1684static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1685{
1686 struct se_session *se_sess;
1687 struct scsiback_nexus *tv_nexus;
1688
1689 mutex_lock(&tpg->tv_tpg_mutex);
1690 tv_nexus = tpg->tpg_nexus;
1691 if (!tv_nexus) {
1692 mutex_unlock(&tpg->tv_tpg_mutex);
1693 return -ENODEV;
1694 }
1695
1696 se_sess = tv_nexus->tvn_se_sess;
1697 if (!se_sess) {
1698 mutex_unlock(&tpg->tv_tpg_mutex);
1699 return -ENODEV;
1700 }
1701
1702 if (tpg->tv_tpg_port_count != 0) {
1703 mutex_unlock(&tpg->tv_tpg_mutex);
1704 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1705 tpg->tv_tpg_port_count);
1706 return -EBUSY;
1707 }
1708
1709 if (tpg->tv_tpg_fe_count != 0) {
1710 mutex_unlock(&tpg->tv_tpg_mutex);
1711 pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1712 tpg->tv_tpg_fe_count);
1713 return -EBUSY;
1714 }
1715
1716 pr_debug("xen-pvscsi: Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1717 scsiback_dump_proto_id(tpg->tport),
1718 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1719
1720 /*
1721 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1722 */
1723 transport_deregister_session(tv_nexus->tvn_se_sess);
1724 tpg->tpg_nexus = NULL;
1725 mutex_unlock(&tpg->tv_tpg_mutex);
1726
1727 kfree(tv_nexus);
1728 return 0;
1729}
1730
1731static ssize_t scsiback_tpg_show_nexus(struct se_portal_group *se_tpg,
1732 char *page)
1733{
1734 struct scsiback_tpg *tpg = container_of(se_tpg,
1735 struct scsiback_tpg, se_tpg);
1736 struct scsiback_nexus *tv_nexus;
1737 ssize_t ret;
1738
1739 mutex_lock(&tpg->tv_tpg_mutex);
1740 tv_nexus = tpg->tpg_nexus;
1741 if (!tv_nexus) {
1742 mutex_unlock(&tpg->tv_tpg_mutex);
1743 return -ENODEV;
1744 }
1745 ret = snprintf(page, PAGE_SIZE, "%s\n",
1746 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1747 mutex_unlock(&tpg->tv_tpg_mutex);
1748
1749 return ret;
1750}
1751
1752static ssize_t scsiback_tpg_store_nexus(struct se_portal_group *se_tpg,
1753 const char *page,
1754 size_t count)
1755{
1756 struct scsiback_tpg *tpg = container_of(se_tpg,
1757 struct scsiback_tpg, se_tpg);
1758 struct scsiback_tport *tport_wwn = tpg->tport;
1759 unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1760 int ret;
1761 /*
1762 * Shutdown the active I_T nexus if 'NULL' is passed..
1763 */
1764 if (!strncmp(page, "NULL", 4)) {
1765 ret = scsiback_drop_nexus(tpg);
1766 return (!ret) ? count : ret;
1767 }
1768 /*
1769 * Otherwise make sure the passed virtual Initiator port WWN matches
1770 * the fabric protocol_id set in scsiback_make_tport(), and call
1771 * scsiback_make_nexus().
1772 */
1773 if (strlen(page) >= VSCSI_NAMELEN) {
1774 pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1775 page, VSCSI_NAMELEN);
1776 return -EINVAL;
1777 }
1778 snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1779
1780 ptr = strstr(i_port, "naa.");
1781 if (ptr) {
1782 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1783 pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1784 i_port, scsiback_dump_proto_id(tport_wwn));
1785 return -EINVAL;
1786 }
1787 port_ptr = &i_port[0];
1788 goto check_newline;
1789 }
1790 ptr = strstr(i_port, "fc.");
1791 if (ptr) {
1792 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1793 pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1794 i_port, scsiback_dump_proto_id(tport_wwn));
1795 return -EINVAL;
1796 }
1797 port_ptr = &i_port[3]; /* Skip over "fc." */
1798 goto check_newline;
1799 }
1800 ptr = strstr(i_port, "iqn.");
1801 if (ptr) {
1802 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1803 pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1804 i_port, scsiback_dump_proto_id(tport_wwn));
1805 return -EINVAL;
1806 }
1807 port_ptr = &i_port[0];
1808 goto check_newline;
1809 }
1810 pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1811 i_port);
1812 return -EINVAL;
1813 /*
1814 * Clear any trailing newline for the NAA WWN
1815 */
1816check_newline:
1817 if (i_port[strlen(i_port) - 1] == '\n')
1818 i_port[strlen(i_port) - 1] = '\0';
1819
1820 ret = scsiback_make_nexus(tpg, port_ptr);
1821 if (ret < 0)
1822 return ret;
1823
1824 return count;
1825}
1826
1827TF_TPG_BASE_ATTR(scsiback, nexus, S_IRUGO | S_IWUSR);
1828
1829static struct configfs_attribute *scsiback_tpg_attrs[] = {
1830 &scsiback_tpg_nexus.attr,
1831 NULL,
1832};
1833
1834static ssize_t
1835scsiback_wwn_show_attr_version(struct target_fabric_configfs *tf,
1836 char *page)
1837{
1838 return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1839 UTS_RELEASE"\n",
1840 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1841}
1842
1843TF_WWN_ATTR_RO(scsiback, version);
1844
1845static struct configfs_attribute *scsiback_wwn_attrs[] = {
1846 &scsiback_wwn_version.attr,
1847 NULL,
1848};
1849
1850static char *scsiback_get_fabric_name(void)
1851{
1852 return "xen-pvscsi";
1853}
1854
1855static int scsiback_port_link(struct se_portal_group *se_tpg,
1856 struct se_lun *lun)
1857{
1858 struct scsiback_tpg *tpg = container_of(se_tpg,
1859 struct scsiback_tpg, se_tpg);
1860
1861 mutex_lock(&tpg->tv_tpg_mutex);
1862 tpg->tv_tpg_port_count++;
1863 mutex_unlock(&tpg->tv_tpg_mutex);
1864
1865 return 0;
1866}
1867
1868static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1869 struct se_lun *lun)
1870{
1871 struct scsiback_tpg *tpg = container_of(se_tpg,
1872 struct scsiback_tpg, se_tpg);
1873
1874 mutex_lock(&tpg->tv_tpg_mutex);
1875 tpg->tv_tpg_port_count--;
1876 mutex_unlock(&tpg->tv_tpg_mutex);
1877}
1878
1879static struct se_portal_group *
1880scsiback_make_tpg(struct se_wwn *wwn,
1881 struct config_group *group,
1882 const char *name)
1883{
1884 struct scsiback_tport *tport = container_of(wwn,
1885 struct scsiback_tport, tport_wwn);
1886
1887 struct scsiback_tpg *tpg;
Dan Carpenter495daef2014-09-08 14:17:35 +03001888 u16 tpgt;
Juergen Grossd9d660f2014-08-28 06:44:12 +02001889 int ret;
1890
1891 if (strstr(name, "tpgt_") != name)
1892 return ERR_PTR(-EINVAL);
Dan Carpenter495daef2014-09-08 14:17:35 +03001893 ret = kstrtou16(name + 5, 10, &tpgt);
1894 if (ret)
1895 return ERR_PTR(ret);
Juergen Grossd9d660f2014-08-28 06:44:12 +02001896
1897 tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1898 if (!tpg)
1899 return ERR_PTR(-ENOMEM);
1900
1901 mutex_init(&tpg->tv_tpg_mutex);
1902 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1903 INIT_LIST_HEAD(&tpg->info_list);
1904 tpg->tport = tport;
1905 tpg->tport_tpgt = tpgt;
1906
1907 ret = core_tpg_register(&scsiback_fabric_configfs->tf_ops, wwn,
1908 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1909 if (ret < 0) {
1910 kfree(tpg);
1911 return NULL;
1912 }
1913 mutex_lock(&scsiback_mutex);
1914 list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1915 mutex_unlock(&scsiback_mutex);
1916
1917 return &tpg->se_tpg;
1918}
1919
1920static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1921{
1922 struct scsiback_tpg *tpg = container_of(se_tpg,
1923 struct scsiback_tpg, se_tpg);
1924
1925 mutex_lock(&scsiback_mutex);
1926 list_del(&tpg->tv_tpg_list);
1927 mutex_unlock(&scsiback_mutex);
1928 /*
1929 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1930 */
1931 scsiback_drop_nexus(tpg);
1932 /*
1933 * Deregister the se_tpg from TCM..
1934 */
1935 core_tpg_deregister(se_tpg);
1936 kfree(tpg);
1937}
1938
1939static int scsiback_check_true(struct se_portal_group *se_tpg)
1940{
1941 return 1;
1942}
1943
1944static int scsiback_check_false(struct se_portal_group *se_tpg)
1945{
1946 return 0;
1947}
1948
1949static struct target_core_fabric_ops scsiback_ops = {
1950 .get_fabric_name = scsiback_get_fabric_name,
1951 .get_fabric_proto_ident = scsiback_get_fabric_proto_ident,
1952 .tpg_get_wwn = scsiback_get_fabric_wwn,
1953 .tpg_get_tag = scsiback_get_tag,
1954 .tpg_get_default_depth = scsiback_get_default_depth,
1955 .tpg_get_pr_transport_id = scsiback_get_pr_transport_id,
1956 .tpg_get_pr_transport_id_len = scsiback_get_pr_transport_id_len,
1957 .tpg_parse_pr_out_transport_id = scsiback_parse_pr_out_transport_id,
1958 .tpg_check_demo_mode = scsiback_check_true,
1959 .tpg_check_demo_mode_cache = scsiback_check_true,
1960 .tpg_check_demo_mode_write_protect = scsiback_check_false,
1961 .tpg_check_prod_mode_write_protect = scsiback_check_false,
1962 .tpg_alloc_fabric_acl = scsiback_alloc_fabric_acl,
1963 .tpg_release_fabric_acl = scsiback_release_fabric_acl,
1964 .tpg_get_inst_index = scsiback_tpg_get_inst_index,
1965 .check_stop_free = scsiback_check_stop_free,
1966 .release_cmd = scsiback_release_cmd,
1967 .put_session = NULL,
1968 .shutdown_session = scsiback_shutdown_session,
1969 .close_session = scsiback_close_session,
1970 .sess_get_index = scsiback_sess_get_index,
1971 .sess_get_initiator_sid = NULL,
1972 .write_pending = scsiback_write_pending,
1973 .write_pending_status = scsiback_write_pending_status,
1974 .set_default_node_attributes = scsiback_set_default_node_attrs,
1975 .get_task_tag = scsiback_get_task_tag,
1976 .get_cmd_state = scsiback_get_cmd_state,
1977 .queue_data_in = scsiback_queue_data_in,
1978 .queue_status = scsiback_queue_status,
1979 .queue_tm_rsp = scsiback_queue_tm_rsp,
1980 .aborted_task = scsiback_aborted_task,
1981 /*
1982 * Setup callers for generic logic in target_core_fabric_configfs.c
1983 */
1984 .fabric_make_wwn = scsiback_make_tport,
1985 .fabric_drop_wwn = scsiback_drop_tport,
1986 .fabric_make_tpg = scsiback_make_tpg,
1987 .fabric_drop_tpg = scsiback_drop_tpg,
1988 .fabric_post_link = scsiback_port_link,
1989 .fabric_pre_unlink = scsiback_port_unlink,
1990 .fabric_make_np = NULL,
1991 .fabric_drop_np = NULL,
1992#if 0
1993 .fabric_make_nodeacl = scsiback_make_nodeacl,
1994 .fabric_drop_nodeacl = scsiback_drop_nodeacl,
1995#endif
1996};
1997
1998static int scsiback_register_configfs(void)
1999{
2000 struct target_fabric_configfs *fabric;
2001 int ret;
2002
2003 pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
2004 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
2005 /*
2006 * Register the top level struct config_item_type with TCM core
2007 */
2008 fabric = target_fabric_configfs_init(THIS_MODULE, "xen-pvscsi");
2009 if (IS_ERR(fabric))
2010 return PTR_ERR(fabric);
2011
2012 /*
2013 * Setup fabric->tf_ops from our local scsiback_ops
2014 */
2015 fabric->tf_ops = scsiback_ops;
2016 /*
2017 * Setup default attribute lists for various fabric->tf_cit_tmpl
2018 */
2019 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = scsiback_wwn_attrs;
2020 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = scsiback_tpg_attrs;
2021 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
2022 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = scsiback_param_attrs;
2023 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
2024 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2025 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2026 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2027 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
2028 /*
2029 * Register the fabric for use within TCM
2030 */
2031 ret = target_fabric_configfs_register(fabric);
2032 if (ret < 0) {
2033 target_fabric_configfs_free(fabric);
2034 return ret;
2035 }
2036 /*
2037 * Setup our local pointer to *fabric
2038 */
2039 scsiback_fabric_configfs = fabric;
2040 pr_debug("xen-pvscsi: Set fabric -> scsiback_fabric_configfs\n");
2041 return 0;
2042};
2043
2044static void scsiback_deregister_configfs(void)
2045{
2046 if (!scsiback_fabric_configfs)
2047 return;
2048
2049 target_fabric_configfs_deregister(scsiback_fabric_configfs);
2050 scsiback_fabric_configfs = NULL;
2051 pr_debug("xen-pvscsi: Cleared scsiback_fabric_configfs\n");
2052};
2053
2054static const struct xenbus_device_id scsiback_ids[] = {
2055 { "vscsi" },
2056 { "" }
2057};
2058
David Vrabel95afae42014-09-08 17:30:41 +01002059static struct xenbus_driver scsiback_driver = {
2060 .ids = scsiback_ids,
Juergen Grossd9d660f2014-08-28 06:44:12 +02002061 .probe = scsiback_probe,
2062 .remove = scsiback_remove,
2063 .otherend_changed = scsiback_frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01002064};
Juergen Grossd9d660f2014-08-28 06:44:12 +02002065
2066static void scsiback_init_pend(void *p)
2067{
2068 struct vscsibk_pend *pend = p;
2069 int i;
2070
2071 memset(pend, 0, sizeof(*pend));
2072 for (i = 0; i < VSCSI_MAX_GRANTS; i++)
2073 pend->grant_handles[i] = SCSIBACK_INVALID_HANDLE;
2074}
2075
2076static int __init scsiback_init(void)
2077{
2078 int ret;
2079
2080 if (!xen_domain())
2081 return -ENODEV;
2082
2083 scsiback_cachep = kmem_cache_create("vscsiif_cache",
2084 sizeof(struct vscsibk_pend), 0, 0, scsiback_init_pend);
2085 if (!scsiback_cachep)
2086 return -ENOMEM;
2087
2088 ret = xenbus_register_backend(&scsiback_driver);
2089 if (ret)
2090 goto out_cache_destroy;
2091
2092 ret = scsiback_register_configfs();
2093 if (ret)
2094 goto out_unregister_xenbus;
2095
2096 return 0;
2097
2098out_unregister_xenbus:
2099 xenbus_unregister_driver(&scsiback_driver);
2100out_cache_destroy:
2101 kmem_cache_destroy(scsiback_cachep);
2102 pr_err("xen-pvscsi: %s: error %d\n", __func__, ret);
2103 return ret;
2104}
2105
2106static void __exit scsiback_exit(void)
2107{
2108 struct page *page;
2109
2110 while (free_pages_num) {
2111 if (get_free_page(&page))
2112 BUG();
2113 free_xenballooned_pages(1, &page);
2114 }
2115 scsiback_deregister_configfs();
2116 xenbus_unregister_driver(&scsiback_driver);
2117 kmem_cache_destroy(scsiback_cachep);
2118}
2119
2120module_init(scsiback_init);
2121module_exit(scsiback_exit);
2122
2123MODULE_DESCRIPTION("Xen SCSI backend driver");
2124MODULE_LICENSE("Dual BSD/GPL");
2125MODULE_ALIAS("xen-backend:vscsi");
2126MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");