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