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 
scsiback_get(struct vscsibk_info * info)199 static void scsiback_get(struct vscsibk_info *info)
200 {
201 	atomic_inc(&info->nr_unreplied_reqs);
202 }
203 
scsiback_put(struct vscsibk_info * info)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 
put_free_pages(struct page ** page,int num)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 
get_free_page(struct page ** page)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 
vaddr_page(struct page * page)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 
vaddr(struct vscsibk_pend * req,int seg)252 static unsigned long vaddr(struct vscsibk_pend *req, int seg)
253 {
254 	return vaddr_page(req->pages[seg]);
255 }
256 
scsiback_print_status(char * sense_buffer,int errors,struct vscsibk_pend * pending_req)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 
scsiback_fast_flush_area(struct vscsibk_pend * req)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 
scsiback_free_translation_entry(struct kref * kref)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 
scsiback_send_response(struct vscsibk_info * info,char * sense_buffer,int32_t result,uint32_t resid,uint16_t rqid)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 
scsiback_do_resp_with_sense(char * sense_buffer,int32_t result,uint32_t resid,struct vscsibk_pend * pending_req)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 
scsiback_cmd_done(struct vscsibk_pend * pending_req)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 
scsiback_cmd_exec(struct vscsibk_pend * pending_req)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 
scsiback_gnttab_data_map_batch(struct gnttab_map_grant_ref * map,struct page ** pg,grant_handle_t * grant,int cnt)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 	for (i = 0; i < cnt; i++) {
426 		if (unlikely(map[i].status != GNTST_okay)) {
427 			pr_err("invalid buffer -- could not remap it\n");
428 			map[i].handle = SCSIBACK_INVALID_HANDLE;
429 			if (!err)
430 				err = -ENOMEM;
431 		} else {
432 			get_page(pg[i]);
433 		}
434 		grant[i] = map[i].handle;
435 	}
436 	return err;
437 }
438 
scsiback_gnttab_data_map_list(struct vscsibk_pend * pending_req,struct scsiif_request_segment * seg,struct page ** pg,grant_handle_t * grant,int cnt,u32 flags)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 
scsiback_gnttab_data_map(struct vscsiif_request * ring_req,struct vscsibk_pend * pending_req)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 
scsiback_disconnect(struct vscsibk_info * info)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 
scsiback_device_action(struct vscsibk_pend * pending_req,enum tcm_tmreq_table act,int tag)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 */
scsiback_do_translation(struct vscsibk_info * info,struct ids_tuple * v)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 
scsiback_get_pend_req(struct vscsiif_back_ring * ring,struct v2p_entry * v2p)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 
prepare_pending_reqs(struct vscsibk_info * info,struct vscsiif_back_ring * ring,struct vscsiif_request * ring_req)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 
scsiback_do_cmd_fn(struct vscsibk_info * info,unsigned int * eoi_flags)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 
scsiback_irq_fn(int irq,void * dev_id)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 
scsiback_init_sring(struct vscsibk_info * info,grant_ref_t ring_ref,evtchn_port_t evtchn)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 
scsiback_map(struct vscsibk_info * info)858 static int scsiback_map(struct vscsibk_info *info)
859 {
860 	struct xenbus_device *dev = info->dev;
861 	unsigned int ring_ref, evtchn;
862 	int err;
863 
864 	err = xenbus_gather(XBT_NIL, dev->otherend,
865 			"ring-ref", "%u", &ring_ref,
866 			"event-channel", "%u", &evtchn, NULL);
867 	if (err) {
868 		xenbus_dev_fatal(dev, err, "reading %s ring", dev->otherend);
869 		return err;
870 	}
871 
872 	return scsiback_init_sring(info, ring_ref, evtchn);
873 }
874 
875 /*
876   Check for a translation entry being present
877 */
scsiback_chk_translation_entry(struct vscsibk_info * info,struct ids_tuple * v)878 static struct v2p_entry *scsiback_chk_translation_entry(
879 	struct vscsibk_info *info, struct ids_tuple *v)
880 {
881 	struct list_head *head = &(info->v2p_entry_lists);
882 	struct v2p_entry *entry;
883 
884 	list_for_each_entry(entry, head, l)
885 		if ((entry->v.chn == v->chn) &&
886 		    (entry->v.tgt == v->tgt) &&
887 		    (entry->v.lun == v->lun))
888 			return entry;
889 
890 	return NULL;
891 }
892 
893 /*
894   Add a new translation entry
895 */
scsiback_add_translation_entry(struct vscsibk_info * info,char * phy,struct ids_tuple * v)896 static int scsiback_add_translation_entry(struct vscsibk_info *info,
897 					  char *phy, struct ids_tuple *v)
898 {
899 	int err = 0;
900 	struct v2p_entry *new;
901 	unsigned long flags;
902 	char *lunp;
903 	unsigned long long unpacked_lun;
904 	struct se_lun *se_lun;
905 	struct scsiback_tpg *tpg_entry, *tpg = NULL;
906 	char *error = "doesn't exist";
907 
908 	lunp = strrchr(phy, ':');
909 	if (!lunp) {
910 		pr_err("illegal format of physical device %s\n", phy);
911 		return -EINVAL;
912 	}
913 	*lunp = 0;
914 	lunp++;
915 	err = kstrtoull(lunp, 10, &unpacked_lun);
916 	if (err < 0) {
917 		pr_err("lun number not valid: %s\n", lunp);
918 		return err;
919 	}
920 
921 	mutex_lock(&scsiback_mutex);
922 	list_for_each_entry(tpg_entry, &scsiback_list, tv_tpg_list) {
923 		if (!strcmp(phy, tpg_entry->tport->tport_name) ||
924 		    !strcmp(phy, tpg_entry->param_alias)) {
925 			mutex_lock(&tpg_entry->se_tpg.tpg_lun_mutex);
926 			hlist_for_each_entry(se_lun, &tpg_entry->se_tpg.tpg_lun_hlist, link) {
927 				if (se_lun->unpacked_lun == unpacked_lun) {
928 					if (!tpg_entry->tpg_nexus)
929 						error = "nexus undefined";
930 					else
931 						tpg = tpg_entry;
932 					break;
933 				}
934 			}
935 			mutex_unlock(&tpg_entry->se_tpg.tpg_lun_mutex);
936 			break;
937 		}
938 	}
939 	if (tpg) {
940 		mutex_lock(&tpg->tv_tpg_mutex);
941 		tpg->tv_tpg_fe_count++;
942 		mutex_unlock(&tpg->tv_tpg_mutex);
943 	}
944 	mutex_unlock(&scsiback_mutex);
945 
946 	if (!tpg) {
947 		pr_err("%s:%llu %s\n", phy, unpacked_lun, error);
948 		return -ENODEV;
949 	}
950 
951 	new = kmalloc(sizeof(struct v2p_entry), GFP_KERNEL);
952 	if (new == NULL) {
953 		err = -ENOMEM;
954 		goto out_free;
955 	}
956 
957 	spin_lock_irqsave(&info->v2p_lock, flags);
958 
959 	/* Check double assignment to identical virtual ID */
960 	if (scsiback_chk_translation_entry(info, v)) {
961 		pr_warn("Virtual ID is already used. Assignment was not performed.\n");
962 		err = -EEXIST;
963 		goto out;
964 	}
965 
966 	/* Create a new translation entry and add to the list */
967 	kref_init(&new->kref);
968 	new->v = *v;
969 	new->tpg = tpg;
970 	new->lun = unpacked_lun;
971 	list_add_tail(&new->l, &info->v2p_entry_lists);
972 
973 out:
974 	spin_unlock_irqrestore(&info->v2p_lock, flags);
975 
976 out_free:
977 	if (err) {
978 		mutex_lock(&tpg->tv_tpg_mutex);
979 		tpg->tv_tpg_fe_count--;
980 		mutex_unlock(&tpg->tv_tpg_mutex);
981 		kfree(new);
982 	}
983 
984 	return err;
985 }
986 
__scsiback_del_translation_entry(struct v2p_entry * entry)987 static void __scsiback_del_translation_entry(struct v2p_entry *entry)
988 {
989 	list_del(&entry->l);
990 	kref_put(&entry->kref, scsiback_free_translation_entry);
991 }
992 
993 /*
994   Delete the translation entry specified
995 */
scsiback_del_translation_entry(struct vscsibk_info * info,struct ids_tuple * v)996 static int scsiback_del_translation_entry(struct vscsibk_info *info,
997 					  struct ids_tuple *v)
998 {
999 	struct v2p_entry *entry;
1000 	unsigned long flags;
1001 	int ret = 0;
1002 
1003 	spin_lock_irqsave(&info->v2p_lock, flags);
1004 	/* Find out the translation entry specified */
1005 	entry = scsiback_chk_translation_entry(info, v);
1006 	if (entry)
1007 		__scsiback_del_translation_entry(entry);
1008 	else
1009 		ret = -ENOENT;
1010 
1011 	spin_unlock_irqrestore(&info->v2p_lock, flags);
1012 	return ret;
1013 }
1014 
scsiback_do_add_lun(struct vscsibk_info * info,const char * state,char * phy,struct ids_tuple * vir,int try)1015 static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state,
1016 				char *phy, struct ids_tuple *vir, int try)
1017 {
1018 	struct v2p_entry *entry;
1019 	unsigned long flags;
1020 	int err;
1021 
1022 	if (try) {
1023 		spin_lock_irqsave(&info->v2p_lock, flags);
1024 		entry = scsiback_chk_translation_entry(info, vir);
1025 		spin_unlock_irqrestore(&info->v2p_lock, flags);
1026 		if (entry)
1027 			return;
1028 	}
1029 	if (!scsiback_add_translation_entry(info, phy, vir)) {
1030 		if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1031 				  "%d", XenbusStateInitialised)) {
1032 			pr_err("xenbus_printf error %s\n", state);
1033 			scsiback_del_translation_entry(info, vir);
1034 		}
1035 	} else if (!try) {
1036 		err = xenbus_printf(XBT_NIL, info->dev->nodename, state,
1037 			      "%d", XenbusStateClosed);
1038 		if (err)
1039 			xenbus_dev_error(info->dev, err,
1040 				"%s: writing %s", __func__, state);
1041 	}
1042 }
1043 
scsiback_do_del_lun(struct vscsibk_info * info,const char * state,struct ids_tuple * vir)1044 static void scsiback_do_del_lun(struct vscsibk_info *info, const char *state,
1045 				struct ids_tuple *vir)
1046 {
1047 	if (!scsiback_del_translation_entry(info, vir)) {
1048 		if (xenbus_printf(XBT_NIL, info->dev->nodename, state,
1049 				  "%d", XenbusStateClosed))
1050 			pr_err("xenbus_printf error %s\n", state);
1051 	}
1052 }
1053 
1054 #define VSCSIBACK_OP_ADD_OR_DEL_LUN	1
1055 #define VSCSIBACK_OP_UPDATEDEV_STATE	2
1056 
scsiback_do_1lun_hotplug(struct vscsibk_info * info,int op,char * ent)1057 static void scsiback_do_1lun_hotplug(struct vscsibk_info *info, int op,
1058 				     char *ent)
1059 {
1060 	int err;
1061 	struct ids_tuple vir;
1062 	char *val;
1063 	int device_state;
1064 	char phy[VSCSI_NAMELEN];
1065 	char str[64];
1066 	char state[64];
1067 	struct xenbus_device *dev = info->dev;
1068 
1069 	/* read status */
1070 	snprintf(state, sizeof(state), "vscsi-devs/%s/state", ent);
1071 	err = xenbus_scanf(XBT_NIL, dev->nodename, state, "%u", &device_state);
1072 	if (XENBUS_EXIST_ERR(err))
1073 		return;
1074 
1075 	/* physical SCSI device */
1076 	snprintf(str, sizeof(str), "vscsi-devs/%s/p-dev", ent);
1077 	val = xenbus_read(XBT_NIL, dev->nodename, str, NULL);
1078 	if (IS_ERR(val)) {
1079 		err = xenbus_printf(XBT_NIL, dev->nodename, state,
1080 			      "%d", XenbusStateClosed);
1081 		if (err)
1082 			xenbus_dev_error(info->dev, err,
1083 				"%s: writing %s", __func__, state);
1084 		return;
1085 	}
1086 	strlcpy(phy, val, VSCSI_NAMELEN);
1087 	kfree(val);
1088 
1089 	/* virtual SCSI device */
1090 	snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", ent);
1091 	err = xenbus_scanf(XBT_NIL, dev->nodename, str, "%u:%u:%u:%u",
1092 			   &vir.hst, &vir.chn, &vir.tgt, &vir.lun);
1093 	if (XENBUS_EXIST_ERR(err)) {
1094 		err = xenbus_printf(XBT_NIL, dev->nodename, state,
1095 			      "%d", XenbusStateClosed);
1096 		if (err)
1097 			xenbus_dev_error(info->dev, err,
1098 				"%s: writing %s", __func__, state);
1099 		return;
1100 	}
1101 
1102 	switch (op) {
1103 	case VSCSIBACK_OP_ADD_OR_DEL_LUN:
1104 		switch (device_state) {
1105 		case XenbusStateInitialising:
1106 			scsiback_do_add_lun(info, state, phy, &vir, 0);
1107 			break;
1108 		case XenbusStateConnected:
1109 			scsiback_do_add_lun(info, state, phy, &vir, 1);
1110 			break;
1111 		case XenbusStateClosing:
1112 			scsiback_do_del_lun(info, state, &vir);
1113 			break;
1114 		default:
1115 			break;
1116 		}
1117 		break;
1118 
1119 	case VSCSIBACK_OP_UPDATEDEV_STATE:
1120 		if (device_state == XenbusStateInitialised) {
1121 			/* modify vscsi-devs/dev-x/state */
1122 			if (xenbus_printf(XBT_NIL, dev->nodename, state,
1123 					  "%d", XenbusStateConnected)) {
1124 				pr_err("xenbus_printf error %s\n", str);
1125 				scsiback_del_translation_entry(info, &vir);
1126 				xenbus_printf(XBT_NIL, dev->nodename, state,
1127 					      "%d", XenbusStateClosed);
1128 			}
1129 		}
1130 		break;
1131 	/* When it is necessary, processing is added here. */
1132 	default:
1133 		break;
1134 	}
1135 }
1136 
scsiback_do_lun_hotplug(struct vscsibk_info * info,int op)1137 static void scsiback_do_lun_hotplug(struct vscsibk_info *info, int op)
1138 {
1139 	int i;
1140 	char **dir;
1141 	unsigned int ndir = 0;
1142 
1143 	dir = xenbus_directory(XBT_NIL, info->dev->nodename, "vscsi-devs",
1144 			       &ndir);
1145 	if (IS_ERR(dir))
1146 		return;
1147 
1148 	for (i = 0; i < ndir; i++)
1149 		scsiback_do_1lun_hotplug(info, op, dir[i]);
1150 
1151 	kfree(dir);
1152 }
1153 
scsiback_frontend_changed(struct xenbus_device * dev,enum xenbus_state frontend_state)1154 static void scsiback_frontend_changed(struct xenbus_device *dev,
1155 					enum xenbus_state frontend_state)
1156 {
1157 	struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1158 
1159 	switch (frontend_state) {
1160 	case XenbusStateInitialising:
1161 		break;
1162 
1163 	case XenbusStateInitialised:
1164 		if (scsiback_map(info))
1165 			break;
1166 
1167 		scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1168 		xenbus_switch_state(dev, XenbusStateConnected);
1169 		break;
1170 
1171 	case XenbusStateConnected:
1172 		scsiback_do_lun_hotplug(info, VSCSIBACK_OP_UPDATEDEV_STATE);
1173 
1174 		if (dev->state == XenbusStateConnected)
1175 			break;
1176 
1177 		xenbus_switch_state(dev, XenbusStateConnected);
1178 		break;
1179 
1180 	case XenbusStateClosing:
1181 		if (info->irq)
1182 			scsiback_disconnect(info);
1183 
1184 		xenbus_switch_state(dev, XenbusStateClosing);
1185 		break;
1186 
1187 	case XenbusStateClosed:
1188 		xenbus_switch_state(dev, XenbusStateClosed);
1189 		if (xenbus_dev_is_online(dev))
1190 			break;
1191 		/* fall through if not online */
1192 	case XenbusStateUnknown:
1193 		device_unregister(&dev->dev);
1194 		break;
1195 
1196 	case XenbusStateReconfiguring:
1197 		scsiback_do_lun_hotplug(info, VSCSIBACK_OP_ADD_OR_DEL_LUN);
1198 		xenbus_switch_state(dev, XenbusStateReconfigured);
1199 
1200 		break;
1201 
1202 	default:
1203 		xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1204 					frontend_state);
1205 		break;
1206 	}
1207 }
1208 
1209 /*
1210   Release the translation entry specfied
1211 */
scsiback_release_translation_entry(struct vscsibk_info * info)1212 static void scsiback_release_translation_entry(struct vscsibk_info *info)
1213 {
1214 	struct v2p_entry *entry, *tmp;
1215 	struct list_head *head = &(info->v2p_entry_lists);
1216 	unsigned long flags;
1217 
1218 	spin_lock_irqsave(&info->v2p_lock, flags);
1219 
1220 	list_for_each_entry_safe(entry, tmp, head, l)
1221 		__scsiback_del_translation_entry(entry);
1222 
1223 	spin_unlock_irqrestore(&info->v2p_lock, flags);
1224 }
1225 
scsiback_remove(struct xenbus_device * dev)1226 static int scsiback_remove(struct xenbus_device *dev)
1227 {
1228 	struct vscsibk_info *info = dev_get_drvdata(&dev->dev);
1229 
1230 	if (info->irq)
1231 		scsiback_disconnect(info);
1232 
1233 	scsiback_release_translation_entry(info);
1234 
1235 	dev_set_drvdata(&dev->dev, NULL);
1236 
1237 	return 0;
1238 }
1239 
scsiback_probe(struct xenbus_device * dev,const struct xenbus_device_id * id)1240 static int scsiback_probe(struct xenbus_device *dev,
1241 			   const struct xenbus_device_id *id)
1242 {
1243 	int err;
1244 
1245 	struct vscsibk_info *info = kzalloc(sizeof(struct vscsibk_info),
1246 					    GFP_KERNEL);
1247 
1248 	pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
1249 
1250 	if (!info) {
1251 		xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure");
1252 		return -ENOMEM;
1253 	}
1254 	info->dev = dev;
1255 	dev_set_drvdata(&dev->dev, info);
1256 
1257 	info->domid = dev->otherend_id;
1258 	spin_lock_init(&info->ring_lock);
1259 	atomic_set(&info->nr_unreplied_reqs, 0);
1260 	init_waitqueue_head(&info->waiting_to_free);
1261 	info->dev = dev;
1262 	info->irq = 0;
1263 	INIT_LIST_HEAD(&info->v2p_entry_lists);
1264 	spin_lock_init(&info->v2p_lock);
1265 
1266 	err = xenbus_printf(XBT_NIL, dev->nodename, "feature-sg-grant", "%u",
1267 			    SG_ALL);
1268 	if (err)
1269 		xenbus_dev_error(dev, err, "writing feature-sg-grant");
1270 
1271 	err = xenbus_switch_state(dev, XenbusStateInitWait);
1272 	if (err)
1273 		goto fail;
1274 
1275 	return 0;
1276 
1277 fail:
1278 	pr_warn("%s failed\n", __func__);
1279 	scsiback_remove(dev);
1280 
1281 	return err;
1282 }
1283 
scsiback_dump_proto_id(struct scsiback_tport * tport)1284 static char *scsiback_dump_proto_id(struct scsiback_tport *tport)
1285 {
1286 	switch (tport->tport_proto_id) {
1287 	case SCSI_PROTOCOL_SAS:
1288 		return "SAS";
1289 	case SCSI_PROTOCOL_FCP:
1290 		return "FCP";
1291 	case SCSI_PROTOCOL_ISCSI:
1292 		return "iSCSI";
1293 	default:
1294 		break;
1295 	}
1296 
1297 	return "Unknown";
1298 }
1299 
scsiback_get_fabric_wwn(struct se_portal_group * se_tpg)1300 static char *scsiback_get_fabric_wwn(struct se_portal_group *se_tpg)
1301 {
1302 	struct scsiback_tpg *tpg = container_of(se_tpg,
1303 				struct scsiback_tpg, se_tpg);
1304 	struct scsiback_tport *tport = tpg->tport;
1305 
1306 	return &tport->tport_name[0];
1307 }
1308 
scsiback_get_tag(struct se_portal_group * se_tpg)1309 static u16 scsiback_get_tag(struct se_portal_group *se_tpg)
1310 {
1311 	struct scsiback_tpg *tpg = container_of(se_tpg,
1312 				struct scsiback_tpg, se_tpg);
1313 	return tpg->tport_tpgt;
1314 }
1315 
1316 static struct se_wwn *
scsiback_make_tport(struct target_fabric_configfs * tf,struct config_group * group,const char * name)1317 scsiback_make_tport(struct target_fabric_configfs *tf,
1318 		     struct config_group *group,
1319 		     const char *name)
1320 {
1321 	struct scsiback_tport *tport;
1322 	char *ptr;
1323 	u64 wwpn = 0;
1324 	int off = 0;
1325 
1326 	tport = kzalloc(sizeof(struct scsiback_tport), GFP_KERNEL);
1327 	if (!tport)
1328 		return ERR_PTR(-ENOMEM);
1329 
1330 	tport->tport_wwpn = wwpn;
1331 	/*
1332 	 * Determine the emulated Protocol Identifier and Target Port Name
1333 	 * based on the incoming configfs directory name.
1334 	 */
1335 	ptr = strstr(name, "naa.");
1336 	if (ptr) {
1337 		tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1338 		goto check_len;
1339 	}
1340 	ptr = strstr(name, "fc.");
1341 	if (ptr) {
1342 		tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1343 		off = 3; /* Skip over "fc." */
1344 		goto check_len;
1345 	}
1346 	ptr = strstr(name, "iqn.");
1347 	if (ptr) {
1348 		tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1349 		goto check_len;
1350 	}
1351 
1352 	pr_err("Unable to locate prefix for emulated Target Port: %s\n", name);
1353 	kfree(tport);
1354 	return ERR_PTR(-EINVAL);
1355 
1356 check_len:
1357 	if (strlen(name) >= VSCSI_NAMELEN) {
1358 		pr_err("Emulated %s Address: %s, exceeds max: %d\n", name,
1359 			scsiback_dump_proto_id(tport), VSCSI_NAMELEN);
1360 		kfree(tport);
1361 		return ERR_PTR(-EINVAL);
1362 	}
1363 	snprintf(&tport->tport_name[0], VSCSI_NAMELEN, "%s", &name[off]);
1364 
1365 	pr_debug("Allocated emulated Target %s Address: %s\n",
1366 		 scsiback_dump_proto_id(tport), name);
1367 
1368 	return &tport->tport_wwn;
1369 }
1370 
scsiback_drop_tport(struct se_wwn * wwn)1371 static void scsiback_drop_tport(struct se_wwn *wwn)
1372 {
1373 	struct scsiback_tport *tport = container_of(wwn,
1374 				struct scsiback_tport, tport_wwn);
1375 
1376 	pr_debug("Deallocating emulated Target %s Address: %s\n",
1377 		 scsiback_dump_proto_id(tport), tport->tport_name);
1378 
1379 	kfree(tport);
1380 }
1381 
scsiback_tpg_get_inst_index(struct se_portal_group * se_tpg)1382 static u32 scsiback_tpg_get_inst_index(struct se_portal_group *se_tpg)
1383 {
1384 	return 1;
1385 }
1386 
scsiback_check_stop_free(struct se_cmd * se_cmd)1387 static int scsiback_check_stop_free(struct se_cmd *se_cmd)
1388 {
1389 	return transport_generic_free_cmd(se_cmd, 0);
1390 }
1391 
scsiback_release_cmd(struct se_cmd * se_cmd)1392 static void scsiback_release_cmd(struct se_cmd *se_cmd)
1393 {
1394 	target_free_tag(se_cmd->se_sess, se_cmd);
1395 }
1396 
scsiback_sess_get_index(struct se_session * se_sess)1397 static u32 scsiback_sess_get_index(struct se_session *se_sess)
1398 {
1399 	return 0;
1400 }
1401 
scsiback_write_pending(struct se_cmd * se_cmd)1402 static int scsiback_write_pending(struct se_cmd *se_cmd)
1403 {
1404 	/* Go ahead and process the write immediately */
1405 	target_execute_cmd(se_cmd);
1406 
1407 	return 0;
1408 }
1409 
scsiback_write_pending_status(struct se_cmd * se_cmd)1410 static int scsiback_write_pending_status(struct se_cmd *se_cmd)
1411 {
1412 	return 0;
1413 }
1414 
scsiback_set_default_node_attrs(struct se_node_acl * nacl)1415 static void scsiback_set_default_node_attrs(struct se_node_acl *nacl)
1416 {
1417 }
1418 
scsiback_get_cmd_state(struct se_cmd * se_cmd)1419 static int scsiback_get_cmd_state(struct se_cmd *se_cmd)
1420 {
1421 	return 0;
1422 }
1423 
scsiback_queue_data_in(struct se_cmd * se_cmd)1424 static int scsiback_queue_data_in(struct se_cmd *se_cmd)
1425 {
1426 	struct vscsibk_pend *pending_req = container_of(se_cmd,
1427 				struct vscsibk_pend, se_cmd);
1428 
1429 	pending_req->result = SAM_STAT_GOOD;
1430 	scsiback_cmd_done(pending_req);
1431 	return 0;
1432 }
1433 
scsiback_queue_status(struct se_cmd * se_cmd)1434 static int scsiback_queue_status(struct se_cmd *se_cmd)
1435 {
1436 	struct vscsibk_pend *pending_req = container_of(se_cmd,
1437 				struct vscsibk_pend, se_cmd);
1438 
1439 	if (se_cmd->sense_buffer &&
1440 	    ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
1441 	     (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE)))
1442 		pending_req->result = (DRIVER_SENSE << 24) |
1443 				      SAM_STAT_CHECK_CONDITION;
1444 	else
1445 		pending_req->result = se_cmd->scsi_status;
1446 
1447 	scsiback_cmd_done(pending_req);
1448 	return 0;
1449 }
1450 
scsiback_queue_tm_rsp(struct se_cmd * se_cmd)1451 static void scsiback_queue_tm_rsp(struct se_cmd *se_cmd)
1452 {
1453 	struct vscsibk_pend *pending_req = container_of(se_cmd,
1454 				struct vscsibk_pend, se_cmd);
1455 
1456 	complete(&pending_req->tmr_done);
1457 }
1458 
scsiback_aborted_task(struct se_cmd * se_cmd)1459 static void scsiback_aborted_task(struct se_cmd *se_cmd)
1460 {
1461 }
1462 
scsiback_tpg_param_alias_show(struct config_item * item,char * page)1463 static ssize_t scsiback_tpg_param_alias_show(struct config_item *item,
1464 					     char *page)
1465 {
1466 	struct se_portal_group *se_tpg = param_to_tpg(item);
1467 	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1468 						se_tpg);
1469 	ssize_t rb;
1470 
1471 	mutex_lock(&tpg->tv_tpg_mutex);
1472 	rb = snprintf(page, PAGE_SIZE, "%s\n", tpg->param_alias);
1473 	mutex_unlock(&tpg->tv_tpg_mutex);
1474 
1475 	return rb;
1476 }
1477 
scsiback_tpg_param_alias_store(struct config_item * item,const char * page,size_t count)1478 static ssize_t scsiback_tpg_param_alias_store(struct config_item *item,
1479 					      const char *page, size_t count)
1480 {
1481 	struct se_portal_group *se_tpg = param_to_tpg(item);
1482 	struct scsiback_tpg *tpg = container_of(se_tpg, struct scsiback_tpg,
1483 						se_tpg);
1484 	int len;
1485 
1486 	if (strlen(page) >= VSCSI_NAMELEN) {
1487 		pr_err("param alias: %s, exceeds max: %d\n", page,
1488 			VSCSI_NAMELEN);
1489 		return -EINVAL;
1490 	}
1491 
1492 	mutex_lock(&tpg->tv_tpg_mutex);
1493 	len = snprintf(tpg->param_alias, VSCSI_NAMELEN, "%s", page);
1494 	if (tpg->param_alias[len - 1] == '\n')
1495 		tpg->param_alias[len - 1] = '\0';
1496 	mutex_unlock(&tpg->tv_tpg_mutex);
1497 
1498 	return count;
1499 }
1500 
1501 CONFIGFS_ATTR(scsiback_tpg_param_, alias);
1502 
1503 static struct configfs_attribute *scsiback_param_attrs[] = {
1504 	&scsiback_tpg_param_attr_alias,
1505 	NULL,
1506 };
1507 
scsiback_alloc_sess_cb(struct se_portal_group * se_tpg,struct se_session * se_sess,void * p)1508 static int scsiback_alloc_sess_cb(struct se_portal_group *se_tpg,
1509 				  struct se_session *se_sess, void *p)
1510 {
1511 	struct scsiback_tpg *tpg = container_of(se_tpg,
1512 				struct scsiback_tpg, se_tpg);
1513 
1514 	tpg->tpg_nexus = p;
1515 	return 0;
1516 }
1517 
scsiback_make_nexus(struct scsiback_tpg * tpg,const char * name)1518 static int scsiback_make_nexus(struct scsiback_tpg *tpg,
1519 				const char *name)
1520 {
1521 	struct scsiback_nexus *tv_nexus;
1522 	int ret = 0;
1523 
1524 	mutex_lock(&tpg->tv_tpg_mutex);
1525 	if (tpg->tpg_nexus) {
1526 		pr_debug("tpg->tpg_nexus already exists\n");
1527 		ret = -EEXIST;
1528 		goto out_unlock;
1529 	}
1530 
1531 	tv_nexus = kzalloc(sizeof(struct scsiback_nexus), GFP_KERNEL);
1532 	if (!tv_nexus) {
1533 		ret = -ENOMEM;
1534 		goto out_unlock;
1535 	}
1536 
1537 	tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg,
1538 						     VSCSI_DEFAULT_SESSION_TAGS,
1539 						     sizeof(struct vscsibk_pend),
1540 						     TARGET_PROT_NORMAL, name,
1541 						     tv_nexus, scsiback_alloc_sess_cb);
1542 	if (IS_ERR(tv_nexus->tvn_se_sess)) {
1543 		kfree(tv_nexus);
1544 		ret = -ENOMEM;
1545 		goto out_unlock;
1546 	}
1547 
1548 out_unlock:
1549 	mutex_unlock(&tpg->tv_tpg_mutex);
1550 	return ret;
1551 }
1552 
scsiback_drop_nexus(struct scsiback_tpg * tpg)1553 static int scsiback_drop_nexus(struct scsiback_tpg *tpg)
1554 {
1555 	struct se_session *se_sess;
1556 	struct scsiback_nexus *tv_nexus;
1557 
1558 	mutex_lock(&tpg->tv_tpg_mutex);
1559 	tv_nexus = tpg->tpg_nexus;
1560 	if (!tv_nexus) {
1561 		mutex_unlock(&tpg->tv_tpg_mutex);
1562 		return -ENODEV;
1563 	}
1564 
1565 	se_sess = tv_nexus->tvn_se_sess;
1566 	if (!se_sess) {
1567 		mutex_unlock(&tpg->tv_tpg_mutex);
1568 		return -ENODEV;
1569 	}
1570 
1571 	if (tpg->tv_tpg_port_count != 0) {
1572 		mutex_unlock(&tpg->tv_tpg_mutex);
1573 		pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG port count: %d\n",
1574 			tpg->tv_tpg_port_count);
1575 		return -EBUSY;
1576 	}
1577 
1578 	if (tpg->tv_tpg_fe_count != 0) {
1579 		mutex_unlock(&tpg->tv_tpg_mutex);
1580 		pr_err("Unable to remove xen-pvscsi I_T Nexus with active TPG frontend count: %d\n",
1581 			tpg->tv_tpg_fe_count);
1582 		return -EBUSY;
1583 	}
1584 
1585 	pr_debug("Removing I_T Nexus to emulated %s Initiator Port: %s\n",
1586 		scsiback_dump_proto_id(tpg->tport),
1587 		tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1588 
1589 	/*
1590 	 * Release the SCSI I_T Nexus to the emulated xen-pvscsi Target Port
1591 	 */
1592 	target_remove_session(se_sess);
1593 	tpg->tpg_nexus = NULL;
1594 	mutex_unlock(&tpg->tv_tpg_mutex);
1595 
1596 	kfree(tv_nexus);
1597 	return 0;
1598 }
1599 
scsiback_tpg_nexus_show(struct config_item * item,char * page)1600 static ssize_t scsiback_tpg_nexus_show(struct config_item *item, char *page)
1601 {
1602 	struct se_portal_group *se_tpg = to_tpg(item);
1603 	struct scsiback_tpg *tpg = container_of(se_tpg,
1604 				struct scsiback_tpg, se_tpg);
1605 	struct scsiback_nexus *tv_nexus;
1606 	ssize_t ret;
1607 
1608 	mutex_lock(&tpg->tv_tpg_mutex);
1609 	tv_nexus = tpg->tpg_nexus;
1610 	if (!tv_nexus) {
1611 		mutex_unlock(&tpg->tv_tpg_mutex);
1612 		return -ENODEV;
1613 	}
1614 	ret = snprintf(page, PAGE_SIZE, "%s\n",
1615 			tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1616 	mutex_unlock(&tpg->tv_tpg_mutex);
1617 
1618 	return ret;
1619 }
1620 
scsiback_tpg_nexus_store(struct config_item * item,const char * page,size_t count)1621 static ssize_t scsiback_tpg_nexus_store(struct config_item *item,
1622 		const char *page, size_t count)
1623 {
1624 	struct se_portal_group *se_tpg = to_tpg(item);
1625 	struct scsiback_tpg *tpg = container_of(se_tpg,
1626 				struct scsiback_tpg, se_tpg);
1627 	struct scsiback_tport *tport_wwn = tpg->tport;
1628 	unsigned char i_port[VSCSI_NAMELEN], *ptr, *port_ptr;
1629 	int ret;
1630 	/*
1631 	 * Shutdown the active I_T nexus if 'NULL' is passed.
1632 	 */
1633 	if (!strncmp(page, "NULL", 4)) {
1634 		ret = scsiback_drop_nexus(tpg);
1635 		return (!ret) ? count : ret;
1636 	}
1637 	/*
1638 	 * Otherwise make sure the passed virtual Initiator port WWN matches
1639 	 * the fabric protocol_id set in scsiback_make_tport(), and call
1640 	 * scsiback_make_nexus().
1641 	 */
1642 	if (strlen(page) >= VSCSI_NAMELEN) {
1643 		pr_err("Emulated NAA Sas Address: %s, exceeds max: %d\n",
1644 			page, VSCSI_NAMELEN);
1645 		return -EINVAL;
1646 	}
1647 	snprintf(&i_port[0], VSCSI_NAMELEN, "%s", page);
1648 
1649 	ptr = strstr(i_port, "naa.");
1650 	if (ptr) {
1651 		if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1652 			pr_err("Passed SAS Initiator Port %s does not match target port protoid: %s\n",
1653 				i_port, scsiback_dump_proto_id(tport_wwn));
1654 			return -EINVAL;
1655 		}
1656 		port_ptr = &i_port[0];
1657 		goto check_newline;
1658 	}
1659 	ptr = strstr(i_port, "fc.");
1660 	if (ptr) {
1661 		if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1662 			pr_err("Passed FCP Initiator Port %s does not match target port protoid: %s\n",
1663 				i_port, scsiback_dump_proto_id(tport_wwn));
1664 			return -EINVAL;
1665 		}
1666 		port_ptr = &i_port[3]; /* Skip over "fc." */
1667 		goto check_newline;
1668 	}
1669 	ptr = strstr(i_port, "iqn.");
1670 	if (ptr) {
1671 		if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1672 			pr_err("Passed iSCSI Initiator Port %s does not match target port protoid: %s\n",
1673 				i_port, scsiback_dump_proto_id(tport_wwn));
1674 			return -EINVAL;
1675 		}
1676 		port_ptr = &i_port[0];
1677 		goto check_newline;
1678 	}
1679 	pr_err("Unable to locate prefix for emulated Initiator Port: %s\n",
1680 		i_port);
1681 	return -EINVAL;
1682 	/*
1683 	 * Clear any trailing newline for the NAA WWN
1684 	 */
1685 check_newline:
1686 	if (i_port[strlen(i_port) - 1] == '\n')
1687 		i_port[strlen(i_port) - 1] = '\0';
1688 
1689 	ret = scsiback_make_nexus(tpg, port_ptr);
1690 	if (ret < 0)
1691 		return ret;
1692 
1693 	return count;
1694 }
1695 
1696 CONFIGFS_ATTR(scsiback_tpg_, nexus);
1697 
1698 static struct configfs_attribute *scsiback_tpg_attrs[] = {
1699 	&scsiback_tpg_attr_nexus,
1700 	NULL,
1701 };
1702 
1703 static ssize_t
scsiback_wwn_version_show(struct config_item * item,char * page)1704 scsiback_wwn_version_show(struct config_item *item, char *page)
1705 {
1706 	return sprintf(page, "xen-pvscsi fabric module %s on %s/%s on "
1707 		UTS_RELEASE"\n",
1708 		VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1709 }
1710 
1711 CONFIGFS_ATTR_RO(scsiback_wwn_, version);
1712 
1713 static struct configfs_attribute *scsiback_wwn_attrs[] = {
1714 	&scsiback_wwn_attr_version,
1715 	NULL,
1716 };
1717 
scsiback_get_fabric_name(void)1718 static char *scsiback_get_fabric_name(void)
1719 {
1720 	return "xen-pvscsi";
1721 }
1722 
scsiback_port_link(struct se_portal_group * se_tpg,struct se_lun * lun)1723 static int scsiback_port_link(struct se_portal_group *se_tpg,
1724 			       struct se_lun *lun)
1725 {
1726 	struct scsiback_tpg *tpg = container_of(se_tpg,
1727 				struct scsiback_tpg, se_tpg);
1728 
1729 	mutex_lock(&tpg->tv_tpg_mutex);
1730 	tpg->tv_tpg_port_count++;
1731 	mutex_unlock(&tpg->tv_tpg_mutex);
1732 
1733 	return 0;
1734 }
1735 
scsiback_port_unlink(struct se_portal_group * se_tpg,struct se_lun * lun)1736 static void scsiback_port_unlink(struct se_portal_group *se_tpg,
1737 				  struct se_lun *lun)
1738 {
1739 	struct scsiback_tpg *tpg = container_of(se_tpg,
1740 				struct scsiback_tpg, se_tpg);
1741 
1742 	mutex_lock(&tpg->tv_tpg_mutex);
1743 	tpg->tv_tpg_port_count--;
1744 	mutex_unlock(&tpg->tv_tpg_mutex);
1745 }
1746 
1747 static struct se_portal_group *
scsiback_make_tpg(struct se_wwn * wwn,const char * name)1748 scsiback_make_tpg(struct se_wwn *wwn, const char *name)
1749 {
1750 	struct scsiback_tport *tport = container_of(wwn,
1751 			struct scsiback_tport, tport_wwn);
1752 
1753 	struct scsiback_tpg *tpg;
1754 	u16 tpgt;
1755 	int ret;
1756 
1757 	if (strstr(name, "tpgt_") != name)
1758 		return ERR_PTR(-EINVAL);
1759 	ret = kstrtou16(name + 5, 10, &tpgt);
1760 	if (ret)
1761 		return ERR_PTR(ret);
1762 
1763 	tpg = kzalloc(sizeof(struct scsiback_tpg), GFP_KERNEL);
1764 	if (!tpg)
1765 		return ERR_PTR(-ENOMEM);
1766 
1767 	mutex_init(&tpg->tv_tpg_mutex);
1768 	INIT_LIST_HEAD(&tpg->tv_tpg_list);
1769 	INIT_LIST_HEAD(&tpg->info_list);
1770 	tpg->tport = tport;
1771 	tpg->tport_tpgt = tpgt;
1772 
1773 	ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
1774 	if (ret < 0) {
1775 		kfree(tpg);
1776 		return NULL;
1777 	}
1778 	mutex_lock(&scsiback_mutex);
1779 	list_add_tail(&tpg->tv_tpg_list, &scsiback_list);
1780 	mutex_unlock(&scsiback_mutex);
1781 
1782 	return &tpg->se_tpg;
1783 }
1784 
scsiback_drop_tpg(struct se_portal_group * se_tpg)1785 static void scsiback_drop_tpg(struct se_portal_group *se_tpg)
1786 {
1787 	struct scsiback_tpg *tpg = container_of(se_tpg,
1788 				struct scsiback_tpg, se_tpg);
1789 
1790 	mutex_lock(&scsiback_mutex);
1791 	list_del(&tpg->tv_tpg_list);
1792 	mutex_unlock(&scsiback_mutex);
1793 	/*
1794 	 * Release the virtual I_T Nexus for this xen-pvscsi TPG
1795 	 */
1796 	scsiback_drop_nexus(tpg);
1797 	/*
1798 	 * Deregister the se_tpg from TCM.
1799 	 */
1800 	core_tpg_deregister(se_tpg);
1801 	kfree(tpg);
1802 }
1803 
scsiback_check_true(struct se_portal_group * se_tpg)1804 static int scsiback_check_true(struct se_portal_group *se_tpg)
1805 {
1806 	return 1;
1807 }
1808 
scsiback_check_false(struct se_portal_group * se_tpg)1809 static int scsiback_check_false(struct se_portal_group *se_tpg)
1810 {
1811 	return 0;
1812 }
1813 
1814 static const struct target_core_fabric_ops scsiback_ops = {
1815 	.module				= THIS_MODULE,
1816 	.name				= "xen-pvscsi",
1817 	.get_fabric_name		= scsiback_get_fabric_name,
1818 	.tpg_get_wwn			= scsiback_get_fabric_wwn,
1819 	.tpg_get_tag			= scsiback_get_tag,
1820 	.tpg_check_demo_mode		= scsiback_check_true,
1821 	.tpg_check_demo_mode_cache	= scsiback_check_true,
1822 	.tpg_check_demo_mode_write_protect = scsiback_check_false,
1823 	.tpg_check_prod_mode_write_protect = scsiback_check_false,
1824 	.tpg_get_inst_index		= scsiback_tpg_get_inst_index,
1825 	.check_stop_free		= scsiback_check_stop_free,
1826 	.release_cmd			= scsiback_release_cmd,
1827 	.sess_get_index			= scsiback_sess_get_index,
1828 	.sess_get_initiator_sid		= NULL,
1829 	.write_pending			= scsiback_write_pending,
1830 	.write_pending_status		= scsiback_write_pending_status,
1831 	.set_default_node_attributes	= scsiback_set_default_node_attrs,
1832 	.get_cmd_state			= scsiback_get_cmd_state,
1833 	.queue_data_in			= scsiback_queue_data_in,
1834 	.queue_status			= scsiback_queue_status,
1835 	.queue_tm_rsp			= scsiback_queue_tm_rsp,
1836 	.aborted_task			= scsiback_aborted_task,
1837 	/*
1838 	 * Setup callers for generic logic in target_core_fabric_configfs.c
1839 	 */
1840 	.fabric_make_wwn		= scsiback_make_tport,
1841 	.fabric_drop_wwn		= scsiback_drop_tport,
1842 	.fabric_make_tpg		= scsiback_make_tpg,
1843 	.fabric_drop_tpg		= scsiback_drop_tpg,
1844 	.fabric_post_link		= scsiback_port_link,
1845 	.fabric_pre_unlink		= scsiback_port_unlink,
1846 
1847 	.tfc_wwn_attrs			= scsiback_wwn_attrs,
1848 	.tfc_tpg_base_attrs		= scsiback_tpg_attrs,
1849 	.tfc_tpg_param_attrs		= scsiback_param_attrs,
1850 };
1851 
1852 static const struct xenbus_device_id scsiback_ids[] = {
1853 	{ "vscsi" },
1854 	{ "" }
1855 };
1856 
1857 static struct xenbus_driver scsiback_driver = {
1858 	.ids			= scsiback_ids,
1859 	.probe			= scsiback_probe,
1860 	.remove			= scsiback_remove,
1861 	.otherend_changed	= scsiback_frontend_changed
1862 };
1863 
scsiback_init(void)1864 static int __init scsiback_init(void)
1865 {
1866 	int ret;
1867 
1868 	if (!xen_domain())
1869 		return -ENODEV;
1870 
1871 	pr_debug("xen-pvscsi: fabric module %s on %s/%s on "UTS_RELEASE"\n",
1872 		 VSCSI_VERSION, utsname()->sysname, utsname()->machine);
1873 
1874 	ret = xenbus_register_backend(&scsiback_driver);
1875 	if (ret)
1876 		goto out;
1877 
1878 	ret = target_register_template(&scsiback_ops);
1879 	if (ret)
1880 		goto out_unregister_xenbus;
1881 
1882 	return 0;
1883 
1884 out_unregister_xenbus:
1885 	xenbus_unregister_driver(&scsiback_driver);
1886 out:
1887 	pr_err("%s: error %d\n", __func__, ret);
1888 	return ret;
1889 }
1890 
scsiback_exit(void)1891 static void __exit scsiback_exit(void)
1892 {
1893 	struct page *page;
1894 
1895 	while (free_pages_num) {
1896 		if (get_free_page(&page))
1897 			BUG();
1898 		gnttab_free_pages(1, &page);
1899 	}
1900 	target_unregister_template(&scsiback_ops);
1901 	xenbus_unregister_driver(&scsiback_driver);
1902 }
1903 
1904 module_init(scsiback_init);
1905 module_exit(scsiback_exit);
1906 
1907 MODULE_DESCRIPTION("Xen SCSI backend driver");
1908 MODULE_LICENSE("Dual BSD/GPL");
1909 MODULE_ALIAS("xen-backend:vscsi");
1910 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");
1911