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