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