xref: /openbmc/linux/drivers/scsi/xen-scsifront.c (revision 297ce026)
1 /*
2  * Xen SCSI frontend driver
3  *
4  * Copyright (c) 2008, FUJITSU Limited
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation; or, when distributed
9  * separately from the Linux kernel or incorporated into other
10  * software packages, subject to the following license:
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this source file (the "Software"), to deal in the Software without
14  * restriction, including without limitation the rights to use, copy, modify,
15  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16  * and to permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28  * IN THE SOFTWARE.
29  */
30 
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/device.h>
34 #include <linux/wait.h>
35 #include <linux/interrupt.h>
36 #include <linux/mutex.h>
37 #include <linux/spinlock.h>
38 #include <linux/sched.h>
39 #include <linux/blkdev.h>
40 #include <linux/pfn.h>
41 #include <linux/slab.h>
42 #include <linux/bitops.h>
43 
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_device.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_host.h>
48 
49 #include <xen/xen.h>
50 #include <xen/xenbus.h>
51 #include <xen/grant_table.h>
52 #include <xen/events.h>
53 #include <xen/page.h>
54 
55 #include <xen/interface/grant_table.h>
56 #include <xen/interface/io/vscsiif.h>
57 #include <xen/interface/io/protocols.h>
58 
59 #include <asm/xen/hypervisor.h>
60 
61 #define VSCSIFRONT_OP_ADD_LUN	1
62 #define VSCSIFRONT_OP_DEL_LUN	2
63 #define VSCSIFRONT_OP_READD_LUN	3
64 
65 /* Tuning point. */
66 #define VSCSIIF_DEFAULT_CMD_PER_LUN 10
67 #define VSCSIIF_MAX_TARGET          64
68 #define VSCSIIF_MAX_LUN             255
69 
70 #define VSCSIIF_RING_SIZE	__CONST_RING_SIZE(vscsiif, PAGE_SIZE)
71 #define VSCSIIF_MAX_REQS	VSCSIIF_RING_SIZE
72 
73 #define vscsiif_grants_sg(_sg)	(PFN_UP((_sg) *		\
74 				sizeof(struct scsiif_request_segment)))
75 
76 struct vscsifrnt_shadow {
77 	/* command between backend and frontend */
78 	unsigned char act;
79 	uint8_t nr_segments;
80 	uint16_t rqid;
81 	uint16_t ref_rqid;
82 
83 	bool inflight;
84 
85 	unsigned int nr_grants;		/* number of grants in gref[] */
86 	struct scsiif_request_segment *sg;	/* scatter/gather elements */
87 	struct scsiif_request_segment seg[VSCSIIF_SG_TABLESIZE];
88 
89 	/* Do reset or abort function. */
90 	wait_queue_head_t wq_reset;	/* reset work queue           */
91 	int wait_reset;			/* reset work queue condition */
92 	int32_t rslt_reset;		/* reset response status:     */
93 					/* SUCCESS or FAILED or:      */
94 #define RSLT_RESET_WAITING	0
95 #define RSLT_RESET_ERR		-1
96 
97 	/* Requested struct scsi_cmnd is stored from kernel. */
98 	struct scsi_cmnd *sc;
99 	int gref[vscsiif_grants_sg(SG_ALL) + SG_ALL];
100 };
101 
102 struct vscsifrnt_info {
103 	struct xenbus_device *dev;
104 
105 	struct Scsi_Host *host;
106 	enum {
107 		STATE_INACTIVE,
108 		STATE_ACTIVE,
109 		STATE_ERROR
110 	}  host_active;
111 
112 	unsigned int evtchn;
113 	unsigned int irq;
114 
115 	grant_ref_t ring_ref;
116 	struct vscsiif_front_ring ring;
117 	struct vscsiif_response	ring_rsp;
118 
119 	spinlock_t shadow_lock;
120 	DECLARE_BITMAP(shadow_free_bitmap, VSCSIIF_MAX_REQS);
121 	struct vscsifrnt_shadow *shadow[VSCSIIF_MAX_REQS];
122 
123 	/* Following items are protected by the host lock. */
124 	wait_queue_head_t wq_sync;
125 	wait_queue_head_t wq_pause;
126 	unsigned int wait_ring_available:1;
127 	unsigned int waiting_pause:1;
128 	unsigned int pause:1;
129 	unsigned callers;
130 
131 	char dev_state_path[64];
132 	struct task_struct *curr;
133 };
134 
135 static DEFINE_MUTEX(scsifront_mutex);
136 
137 static void scsifront_wake_up(struct vscsifrnt_info *info)
138 {
139 	info->wait_ring_available = 0;
140 	wake_up(&info->wq_sync);
141 }
142 
143 static int scsifront_get_rqid(struct vscsifrnt_info *info)
144 {
145 	unsigned long flags;
146 	int free;
147 
148 	spin_lock_irqsave(&info->shadow_lock, flags);
149 
150 	free = find_first_bit(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
151 	__clear_bit(free, info->shadow_free_bitmap);
152 
153 	spin_unlock_irqrestore(&info->shadow_lock, flags);
154 
155 	return free;
156 }
157 
158 static int _scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
159 {
160 	int empty = bitmap_empty(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
161 
162 	__set_bit(id, info->shadow_free_bitmap);
163 	info->shadow[id] = NULL;
164 
165 	return empty || info->wait_ring_available;
166 }
167 
168 static void scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
169 {
170 	unsigned long flags;
171 	int kick;
172 
173 	spin_lock_irqsave(&info->shadow_lock, flags);
174 	kick = _scsifront_put_rqid(info, id);
175 	spin_unlock_irqrestore(&info->shadow_lock, flags);
176 
177 	if (kick)
178 		scsifront_wake_up(info);
179 }
180 
181 static int scsifront_do_request(struct vscsifrnt_info *info,
182 				struct vscsifrnt_shadow *shadow)
183 {
184 	struct vscsiif_front_ring *ring = &(info->ring);
185 	struct vscsiif_request *ring_req;
186 	struct scsi_cmnd *sc = shadow->sc;
187 	uint32_t id;
188 	int i, notify;
189 
190 	if (RING_FULL(&info->ring))
191 		return -EBUSY;
192 
193 	id = scsifront_get_rqid(info);	/* use id in response */
194 	if (id >= VSCSIIF_MAX_REQS)
195 		return -EBUSY;
196 
197 	info->shadow[id] = shadow;
198 	shadow->rqid = id;
199 
200 	ring_req = RING_GET_REQUEST(&(info->ring), ring->req_prod_pvt);
201 	ring->req_prod_pvt++;
202 
203 	ring_req->rqid        = id;
204 	ring_req->act         = shadow->act;
205 	ring_req->ref_rqid    = shadow->ref_rqid;
206 	ring_req->nr_segments = shadow->nr_segments;
207 
208 	ring_req->id      = sc->device->id;
209 	ring_req->lun     = sc->device->lun;
210 	ring_req->channel = sc->device->channel;
211 	ring_req->cmd_len = sc->cmd_len;
212 
213 	BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
214 
215 	memcpy(ring_req->cmnd, sc->cmnd, sc->cmd_len);
216 
217 	ring_req->sc_data_direction   = (uint8_t)sc->sc_data_direction;
218 	ring_req->timeout_per_command = scsi_cmd_to_rq(sc)->timeout / HZ;
219 
220 	for (i = 0; i < (shadow->nr_segments & ~VSCSIIF_SG_GRANT); i++)
221 		ring_req->seg[i] = shadow->seg[i];
222 
223 	shadow->inflight = true;
224 
225 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(ring, notify);
226 	if (notify)
227 		notify_remote_via_irq(info->irq);
228 
229 	return 0;
230 }
231 
232 static void scsifront_set_error(struct vscsifrnt_info *info, const char *msg)
233 {
234 	shost_printk(KERN_ERR, info->host, KBUILD_MODNAME "%s\n"
235 		     "Disabling device for further use\n", msg);
236 	info->host_active = STATE_ERROR;
237 }
238 
239 static void scsifront_gnttab_done(struct vscsifrnt_info *info,
240 				  struct vscsifrnt_shadow *shadow)
241 {
242 	int i;
243 
244 	if (shadow->sc->sc_data_direction == DMA_NONE)
245 		return;
246 
247 	for (i = 0; i < shadow->nr_grants; i++) {
248 		if (unlikely(!gnttab_try_end_foreign_access(shadow->gref[i]))) {
249 			scsifront_set_error(info, "grant still in use by backend");
250 			return;
251 		}
252 	}
253 
254 	kfree(shadow->sg);
255 }
256 
257 static unsigned int scsifront_host_byte(int32_t rslt)
258 {
259 	switch (XEN_VSCSIIF_RSLT_HOST(rslt)) {
260 	case XEN_VSCSIIF_RSLT_HOST_OK:
261 		return DID_OK;
262 	case XEN_VSCSIIF_RSLT_HOST_NO_CONNECT:
263 		return DID_NO_CONNECT;
264 	case XEN_VSCSIIF_RSLT_HOST_BUS_BUSY:
265 		return DID_BUS_BUSY;
266 	case XEN_VSCSIIF_RSLT_HOST_TIME_OUT:
267 		return DID_TIME_OUT;
268 	case XEN_VSCSIIF_RSLT_HOST_BAD_TARGET:
269 		return DID_BAD_TARGET;
270 	case XEN_VSCSIIF_RSLT_HOST_ABORT:
271 		return DID_ABORT;
272 	case XEN_VSCSIIF_RSLT_HOST_PARITY:
273 		return DID_PARITY;
274 	case XEN_VSCSIIF_RSLT_HOST_ERROR:
275 		return DID_ERROR;
276 	case XEN_VSCSIIF_RSLT_HOST_RESET:
277 		return DID_RESET;
278 	case XEN_VSCSIIF_RSLT_HOST_BAD_INTR:
279 		return DID_BAD_INTR;
280 	case XEN_VSCSIIF_RSLT_HOST_PASSTHROUGH:
281 		return DID_PASSTHROUGH;
282 	case XEN_VSCSIIF_RSLT_HOST_SOFT_ERROR:
283 		return DID_SOFT_ERROR;
284 	case XEN_VSCSIIF_RSLT_HOST_IMM_RETRY:
285 		return DID_IMM_RETRY;
286 	case XEN_VSCSIIF_RSLT_HOST_REQUEUE:
287 		return DID_REQUEUE;
288 	case XEN_VSCSIIF_RSLT_HOST_TRANSPORT_DISRUPTED:
289 		return DID_TRANSPORT_DISRUPTED;
290 	case XEN_VSCSIIF_RSLT_HOST_TRANSPORT_FAILFAST:
291 		return DID_TRANSPORT_FAILFAST;
292 	case XEN_VSCSIIF_RSLT_HOST_TARGET_FAILURE:
293 		return DID_TARGET_FAILURE;
294 	case XEN_VSCSIIF_RSLT_HOST_NEXUS_FAILURE:
295 		return DID_NEXUS_FAILURE;
296 	case XEN_VSCSIIF_RSLT_HOST_ALLOC_FAILURE:
297 		return DID_ALLOC_FAILURE;
298 	case XEN_VSCSIIF_RSLT_HOST_MEDIUM_ERROR:
299 		return DID_MEDIUM_ERROR;
300 	case XEN_VSCSIIF_RSLT_HOST_TRANSPORT_MARGINAL:
301 		return DID_TRANSPORT_MARGINAL;
302 	default:
303 		return DID_ERROR;
304 	}
305 }
306 
307 static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info,
308 				   struct vscsiif_response *ring_rsp)
309 {
310 	struct vscsifrnt_shadow *shadow;
311 	struct scsi_cmnd *sc;
312 	uint32_t id;
313 	uint8_t sense_len;
314 
315 	id = ring_rsp->rqid;
316 	shadow = info->shadow[id];
317 	sc = shadow->sc;
318 
319 	BUG_ON(sc == NULL);
320 
321 	scsifront_gnttab_done(info, shadow);
322 	if (info->host_active == STATE_ERROR)
323 		return;
324 	scsifront_put_rqid(info, id);
325 
326 	set_host_byte(sc, scsifront_host_byte(ring_rsp->rslt));
327 	set_status_byte(sc, XEN_VSCSIIF_RSLT_STATUS(ring_rsp->rslt));
328 	scsi_set_resid(sc, ring_rsp->residual_len);
329 
330 	sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE,
331 			  ring_rsp->sense_len);
332 
333 	if (sense_len)
334 		memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len);
335 
336 	scsi_done(sc);
337 }
338 
339 static void scsifront_sync_cmd_done(struct vscsifrnt_info *info,
340 				    struct vscsiif_response *ring_rsp)
341 {
342 	uint16_t id = ring_rsp->rqid;
343 	unsigned long flags;
344 	struct vscsifrnt_shadow *shadow = info->shadow[id];
345 	int kick;
346 
347 	spin_lock_irqsave(&info->shadow_lock, flags);
348 	shadow->wait_reset = 1;
349 	switch (shadow->rslt_reset) {
350 	case RSLT_RESET_WAITING:
351 		if (ring_rsp->rslt == XEN_VSCSIIF_RSLT_RESET_SUCCESS)
352 			shadow->rslt_reset = SUCCESS;
353 		else
354 			shadow->rslt_reset = FAILED;
355 		break;
356 	case RSLT_RESET_ERR:
357 		kick = _scsifront_put_rqid(info, id);
358 		spin_unlock_irqrestore(&info->shadow_lock, flags);
359 		kfree(shadow);
360 		if (kick)
361 			scsifront_wake_up(info);
362 		return;
363 	default:
364 		scsifront_set_error(info, "bad reset state");
365 		break;
366 	}
367 	spin_unlock_irqrestore(&info->shadow_lock, flags);
368 
369 	wake_up(&shadow->wq_reset);
370 }
371 
372 static void scsifront_do_response(struct vscsifrnt_info *info,
373 				  struct vscsiif_response *ring_rsp)
374 {
375 	struct vscsifrnt_shadow *shadow;
376 
377 	if (ring_rsp->rqid >= VSCSIIF_MAX_REQS ||
378 	    !info->shadow[ring_rsp->rqid]->inflight) {
379 		scsifront_set_error(info, "illegal rqid returned by backend!");
380 		return;
381 	}
382 	shadow = info->shadow[ring_rsp->rqid];
383 	shadow->inflight = false;
384 
385 	if (shadow->act == VSCSIIF_ACT_SCSI_CDB)
386 		scsifront_cdb_cmd_done(info, ring_rsp);
387 	else
388 		scsifront_sync_cmd_done(info, ring_rsp);
389 }
390 
391 static int scsifront_ring_drain(struct vscsifrnt_info *info,
392 				unsigned int *eoiflag)
393 {
394 	struct vscsiif_response ring_rsp;
395 	RING_IDX i, rp;
396 	int more_to_do = 0;
397 
398 	rp = READ_ONCE(info->ring.sring->rsp_prod);
399 	virt_rmb();	/* ordering required respective to backend */
400 	if (RING_RESPONSE_PROD_OVERFLOW(&info->ring, rp)) {
401 		scsifront_set_error(info, "illegal number of responses");
402 		return 0;
403 	}
404 	for (i = info->ring.rsp_cons; i != rp; i++) {
405 		RING_COPY_RESPONSE(&info->ring, i, &ring_rsp);
406 		scsifront_do_response(info, &ring_rsp);
407 		if (info->host_active == STATE_ERROR)
408 			return 0;
409 		*eoiflag &= ~XEN_EOI_FLAG_SPURIOUS;
410 	}
411 
412 	info->ring.rsp_cons = i;
413 
414 	if (i != info->ring.req_prod_pvt)
415 		RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
416 	else
417 		info->ring.sring->rsp_event = i + 1;
418 
419 	return more_to_do;
420 }
421 
422 static int scsifront_cmd_done(struct vscsifrnt_info *info,
423 			      unsigned int *eoiflag)
424 {
425 	int more_to_do;
426 	unsigned long flags;
427 
428 	spin_lock_irqsave(info->host->host_lock, flags);
429 
430 	more_to_do = scsifront_ring_drain(info, eoiflag);
431 
432 	info->wait_ring_available = 0;
433 
434 	spin_unlock_irqrestore(info->host->host_lock, flags);
435 
436 	wake_up(&info->wq_sync);
437 
438 	return more_to_do;
439 }
440 
441 static irqreturn_t scsifront_irq_fn(int irq, void *dev_id)
442 {
443 	struct vscsifrnt_info *info = dev_id;
444 	unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
445 
446 	if (info->host_active == STATE_ERROR) {
447 		xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
448 		return IRQ_HANDLED;
449 	}
450 
451 	while (scsifront_cmd_done(info, &eoiflag))
452 		/* Yield point for this unbounded loop. */
453 		cond_resched();
454 
455 	xen_irq_lateeoi(irq, eoiflag);
456 
457 	return IRQ_HANDLED;
458 }
459 
460 static void scsifront_finish_all(struct vscsifrnt_info *info)
461 {
462 	unsigned int i, dummy;
463 	struct vscsiif_response resp;
464 
465 	scsifront_ring_drain(info, &dummy);
466 
467 	for (i = 0; i < VSCSIIF_MAX_REQS; i++) {
468 		if (test_bit(i, info->shadow_free_bitmap))
469 			continue;
470 		resp.rqid = i;
471 		resp.sense_len = 0;
472 		resp.rslt = DID_RESET << 16;
473 		resp.residual_len = 0;
474 		scsifront_do_response(info, &resp);
475 	}
476 }
477 
478 static int map_data_for_request(struct vscsifrnt_info *info,
479 				struct scsi_cmnd *sc,
480 				struct vscsifrnt_shadow *shadow)
481 {
482 	grant_ref_t gref_head;
483 	struct page *page;
484 	int err, ref, ref_cnt = 0;
485 	int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE);
486 	unsigned int i, off, len, bytes;
487 	unsigned int data_len = scsi_bufflen(sc);
488 	unsigned int data_grants = 0, seg_grants = 0;
489 	struct scatterlist *sg;
490 	struct scsiif_request_segment *seg;
491 
492 	if (sc->sc_data_direction == DMA_NONE || !data_len)
493 		return 0;
494 
495 	scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i)
496 		data_grants += PFN_UP(sg->offset + sg->length);
497 
498 	if (data_grants > VSCSIIF_SG_TABLESIZE) {
499 		if (data_grants > info->host->sg_tablesize) {
500 			shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
501 			     "Unable to map request_buffer for command!\n");
502 			return -E2BIG;
503 		}
504 		seg_grants = vscsiif_grants_sg(data_grants);
505 		shadow->sg = kcalloc(data_grants,
506 			sizeof(struct scsiif_request_segment), GFP_ATOMIC);
507 		if (!shadow->sg)
508 			return -ENOMEM;
509 	}
510 	seg = shadow->sg ? : shadow->seg;
511 
512 	err = gnttab_alloc_grant_references(seg_grants + data_grants,
513 					    &gref_head);
514 	if (err) {
515 		kfree(shadow->sg);
516 		shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
517 			     "gnttab_alloc_grant_references() error\n");
518 		return -ENOMEM;
519 	}
520 
521 	if (seg_grants) {
522 		page = virt_to_page(seg);
523 		off = offset_in_page(seg);
524 		len = sizeof(struct scsiif_request_segment) * data_grants;
525 		while (len > 0) {
526 			bytes = min_t(unsigned int, len, PAGE_SIZE - off);
527 
528 			ref = gnttab_claim_grant_reference(&gref_head);
529 			BUG_ON(ref == -ENOSPC);
530 
531 			gnttab_grant_foreign_access_ref(ref,
532 				info->dev->otherend_id,
533 				xen_page_to_gfn(page), 1);
534 			shadow->gref[ref_cnt] = ref;
535 			shadow->seg[ref_cnt].gref   = ref;
536 			shadow->seg[ref_cnt].offset = (uint16_t)off;
537 			shadow->seg[ref_cnt].length = (uint16_t)bytes;
538 
539 			page++;
540 			len -= bytes;
541 			off = 0;
542 			ref_cnt++;
543 		}
544 		BUG_ON(seg_grants < ref_cnt);
545 		seg_grants = ref_cnt;
546 	}
547 
548 	scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) {
549 		page = sg_page(sg);
550 		off = sg->offset;
551 		len = sg->length;
552 
553 		while (len > 0 && data_len > 0) {
554 			/*
555 			 * sg sends a scatterlist that is larger than
556 			 * the data_len it wants transferred for certain
557 			 * IO sizes.
558 			 */
559 			bytes = min_t(unsigned int, len, PAGE_SIZE - off);
560 			bytes = min(bytes, data_len);
561 
562 			ref = gnttab_claim_grant_reference(&gref_head);
563 			BUG_ON(ref == -ENOSPC);
564 
565 			gnttab_grant_foreign_access_ref(ref,
566 				info->dev->otherend_id,
567 				xen_page_to_gfn(page),
568 				grant_ro);
569 
570 			shadow->gref[ref_cnt] = ref;
571 			seg->gref   = ref;
572 			seg->offset = (uint16_t)off;
573 			seg->length = (uint16_t)bytes;
574 
575 			page++;
576 			seg++;
577 			len -= bytes;
578 			data_len -= bytes;
579 			off = 0;
580 			ref_cnt++;
581 		}
582 	}
583 
584 	if (seg_grants)
585 		shadow->nr_segments = VSCSIIF_SG_GRANT | seg_grants;
586 	else
587 		shadow->nr_segments = (uint8_t)ref_cnt;
588 	shadow->nr_grants = ref_cnt;
589 
590 	return 0;
591 }
592 
593 static int scsifront_enter(struct vscsifrnt_info *info)
594 {
595 	if (info->pause)
596 		return 1;
597 	info->callers++;
598 	return 0;
599 }
600 
601 static void scsifront_return(struct vscsifrnt_info *info)
602 {
603 	info->callers--;
604 	if (info->callers)
605 		return;
606 
607 	if (!info->waiting_pause)
608 		return;
609 
610 	info->waiting_pause = 0;
611 	wake_up(&info->wq_pause);
612 }
613 
614 static int scsifront_queuecommand(struct Scsi_Host *shost,
615 				  struct scsi_cmnd *sc)
616 {
617 	struct vscsifrnt_info *info = shost_priv(shost);
618 	struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc);
619 	unsigned long flags;
620 	int err;
621 
622 	if (info->host_active == STATE_ERROR)
623 		return SCSI_MLQUEUE_HOST_BUSY;
624 
625 	sc->result = 0;
626 
627 	shadow->sc  = sc;
628 	shadow->act = VSCSIIF_ACT_SCSI_CDB;
629 
630 	spin_lock_irqsave(shost->host_lock, flags);
631 	if (scsifront_enter(info)) {
632 		spin_unlock_irqrestore(shost->host_lock, flags);
633 		return SCSI_MLQUEUE_HOST_BUSY;
634 	}
635 
636 	err = map_data_for_request(info, sc, shadow);
637 	if (err < 0) {
638 		pr_debug("%s: err %d\n", __func__, err);
639 		scsifront_return(info);
640 		spin_unlock_irqrestore(shost->host_lock, flags);
641 		if (err == -ENOMEM)
642 			return SCSI_MLQUEUE_HOST_BUSY;
643 		sc->result = DID_ERROR << 16;
644 		scsi_done(sc);
645 		return 0;
646 	}
647 
648 	if (scsifront_do_request(info, shadow)) {
649 		scsifront_gnttab_done(info, shadow);
650 		goto busy;
651 	}
652 
653 	scsifront_return(info);
654 	spin_unlock_irqrestore(shost->host_lock, flags);
655 
656 	return 0;
657 
658 busy:
659 	scsifront_return(info);
660 	spin_unlock_irqrestore(shost->host_lock, flags);
661 	pr_debug("%s: busy\n", __func__);
662 	return SCSI_MLQUEUE_HOST_BUSY;
663 }
664 
665 /*
666  * Any exception handling (reset or abort) must be forwarded to the backend.
667  * We have to wait until an answer is returned. This answer contains the
668  * result to be returned to the requestor.
669  */
670 static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)
671 {
672 	struct Scsi_Host *host = sc->device->host;
673 	struct vscsifrnt_info *info = shost_priv(host);
674 	struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc);
675 	int err = 0;
676 
677 	if (info->host_active == STATE_ERROR)
678 		return FAILED;
679 
680 	shadow = kzalloc(sizeof(*shadow), GFP_NOIO);
681 	if (!shadow)
682 		return FAILED;
683 
684 	shadow->act = act;
685 	shadow->rslt_reset = RSLT_RESET_WAITING;
686 	shadow->sc = sc;
687 	shadow->ref_rqid = s->rqid;
688 	init_waitqueue_head(&shadow->wq_reset);
689 
690 	spin_lock_irq(host->host_lock);
691 
692 	for (;;) {
693 		if (scsifront_enter(info))
694 			goto fail;
695 
696 		if (!scsifront_do_request(info, shadow))
697 			break;
698 
699 		scsifront_return(info);
700 		if (err)
701 			goto fail;
702 		info->wait_ring_available = 1;
703 		spin_unlock_irq(host->host_lock);
704 		err = wait_event_interruptible(info->wq_sync,
705 					       !info->wait_ring_available);
706 		spin_lock_irq(host->host_lock);
707 	}
708 
709 	spin_unlock_irq(host->host_lock);
710 	err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset);
711 	spin_lock_irq(host->host_lock);
712 
713 	if (!err) {
714 		err = shadow->rslt_reset;
715 		scsifront_put_rqid(info, shadow->rqid);
716 		kfree(shadow);
717 	} else {
718 		spin_lock(&info->shadow_lock);
719 		shadow->rslt_reset = RSLT_RESET_ERR;
720 		spin_unlock(&info->shadow_lock);
721 		err = FAILED;
722 	}
723 
724 	scsifront_return(info);
725 	spin_unlock_irq(host->host_lock);
726 	return err;
727 
728 fail:
729 	spin_unlock_irq(host->host_lock);
730 	kfree(shadow);
731 	return FAILED;
732 }
733 
734 static int scsifront_eh_abort_handler(struct scsi_cmnd *sc)
735 {
736 	pr_debug("%s\n", __func__);
737 	return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT);
738 }
739 
740 static int scsifront_dev_reset_handler(struct scsi_cmnd *sc)
741 {
742 	pr_debug("%s\n", __func__);
743 	return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET);
744 }
745 
746 static int scsifront_sdev_configure(struct scsi_device *sdev)
747 {
748 	struct vscsifrnt_info *info = shost_priv(sdev->host);
749 	int err;
750 
751 	if (info->host_active == STATE_ERROR)
752 		return -EIO;
753 
754 	if (info && current == info->curr) {
755 		err = xenbus_printf(XBT_NIL, info->dev->nodename,
756 			      info->dev_state_path, "%d", XenbusStateConnected);
757 		if (err) {
758 			xenbus_dev_error(info->dev, err,
759 				"%s: writing dev_state_path", __func__);
760 			return err;
761 		}
762 	}
763 
764 	return 0;
765 }
766 
767 static void scsifront_sdev_destroy(struct scsi_device *sdev)
768 {
769 	struct vscsifrnt_info *info = shost_priv(sdev->host);
770 	int err;
771 
772 	if (info && current == info->curr) {
773 		err = xenbus_printf(XBT_NIL, info->dev->nodename,
774 			      info->dev_state_path, "%d", XenbusStateClosed);
775 		if (err)
776 			xenbus_dev_error(info->dev, err,
777 				"%s: writing dev_state_path", __func__);
778 	}
779 }
780 
781 static struct scsi_host_template scsifront_sht = {
782 	.module			= THIS_MODULE,
783 	.name			= "Xen SCSI frontend driver",
784 	.queuecommand		= scsifront_queuecommand,
785 	.eh_abort_handler	= scsifront_eh_abort_handler,
786 	.eh_device_reset_handler = scsifront_dev_reset_handler,
787 	.slave_configure	= scsifront_sdev_configure,
788 	.slave_destroy		= scsifront_sdev_destroy,
789 	.cmd_per_lun		= VSCSIIF_DEFAULT_CMD_PER_LUN,
790 	.can_queue		= VSCSIIF_MAX_REQS,
791 	.this_id		= -1,
792 	.cmd_size		= sizeof(struct vscsifrnt_shadow),
793 	.sg_tablesize		= VSCSIIF_SG_TABLESIZE,
794 	.proc_name		= "scsifront",
795 };
796 
797 static int scsifront_alloc_ring(struct vscsifrnt_info *info)
798 {
799 	struct xenbus_device *dev = info->dev;
800 	struct vscsiif_sring *sring;
801 	grant_ref_t gref;
802 	int err = -ENOMEM;
803 
804 	/***** Frontend to Backend ring start *****/
805 	sring = (struct vscsiif_sring *)__get_free_page(GFP_KERNEL);
806 	if (!sring) {
807 		xenbus_dev_fatal(dev, err,
808 			"fail to allocate shared ring (Front to Back)");
809 		return err;
810 	}
811 	SHARED_RING_INIT(sring);
812 	FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
813 
814 	err = xenbus_grant_ring(dev, sring, 1, &gref);
815 	if (err < 0) {
816 		free_page((unsigned long)sring);
817 		xenbus_dev_fatal(dev, err,
818 			"fail to grant shared ring (Front to Back)");
819 		return err;
820 	}
821 	info->ring_ref = gref;
822 
823 	err = xenbus_alloc_evtchn(dev, &info->evtchn);
824 	if (err) {
825 		xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
826 		goto free_gnttab;
827 	}
828 
829 	err = bind_evtchn_to_irq_lateeoi(info->evtchn);
830 	if (err <= 0) {
831 		xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
832 		goto free_gnttab;
833 	}
834 
835 	info->irq = err;
836 
837 	err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn,
838 				   IRQF_ONESHOT, "scsifront", info);
839 	if (err) {
840 		xenbus_dev_fatal(dev, err, "request_threaded_irq");
841 		goto free_irq;
842 	}
843 
844 	return 0;
845 
846 /* free resource */
847 free_irq:
848 	unbind_from_irqhandler(info->irq, info);
849 free_gnttab:
850 	gnttab_end_foreign_access(info->ring_ref,
851 				  (unsigned long)info->ring.sring);
852 
853 	return err;
854 }
855 
856 static void scsifront_free_ring(struct vscsifrnt_info *info)
857 {
858 	unbind_from_irqhandler(info->irq, info);
859 	gnttab_end_foreign_access(info->ring_ref,
860 				  (unsigned long)info->ring.sring);
861 }
862 
863 static int scsifront_init_ring(struct vscsifrnt_info *info)
864 {
865 	struct xenbus_device *dev = info->dev;
866 	struct xenbus_transaction xbt;
867 	int err;
868 
869 	pr_debug("%s\n", __func__);
870 
871 	err = scsifront_alloc_ring(info);
872 	if (err)
873 		return err;
874 	pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn);
875 
876 again:
877 	err = xenbus_transaction_start(&xbt);
878 	if (err)
879 		xenbus_dev_fatal(dev, err, "starting transaction");
880 
881 	err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u",
882 			    info->ring_ref);
883 	if (err) {
884 		xenbus_dev_fatal(dev, err, "%s", "writing ring-ref");
885 		goto fail;
886 	}
887 
888 	err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
889 			    info->evtchn);
890 
891 	if (err) {
892 		xenbus_dev_fatal(dev, err, "%s", "writing event-channel");
893 		goto fail;
894 	}
895 
896 	err = xenbus_transaction_end(xbt, 0);
897 	if (err) {
898 		if (err == -EAGAIN)
899 			goto again;
900 		xenbus_dev_fatal(dev, err, "completing transaction");
901 		goto free_sring;
902 	}
903 
904 	return 0;
905 
906 fail:
907 	xenbus_transaction_end(xbt, 1);
908 free_sring:
909 	scsifront_free_ring(info);
910 
911 	return err;
912 }
913 
914 
915 static int scsifront_probe(struct xenbus_device *dev,
916 			   const struct xenbus_device_id *id)
917 {
918 	struct vscsifrnt_info *info;
919 	struct Scsi_Host *host;
920 	int err = -ENOMEM;
921 	char name[TASK_COMM_LEN];
922 
923 	host = scsi_host_alloc(&scsifront_sht, sizeof(*info));
924 	if (!host) {
925 		xenbus_dev_fatal(dev, err, "fail to allocate scsi host");
926 		return err;
927 	}
928 	info = (struct vscsifrnt_info *)host->hostdata;
929 
930 	dev_set_drvdata(&dev->dev, info);
931 	info->dev = dev;
932 
933 	bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
934 
935 	err = scsifront_init_ring(info);
936 	if (err) {
937 		scsi_host_put(host);
938 		return err;
939 	}
940 
941 	init_waitqueue_head(&info->wq_sync);
942 	init_waitqueue_head(&info->wq_pause);
943 	spin_lock_init(&info->shadow_lock);
944 
945 	snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no);
946 
947 	host->max_id      = VSCSIIF_MAX_TARGET;
948 	host->max_channel = 0;
949 	host->max_lun     = VSCSIIF_MAX_LUN;
950 	host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512;
951 	host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE;
952 
953 	err = scsi_add_host(host, &dev->dev);
954 	if (err) {
955 		dev_err(&dev->dev, "fail to add scsi host %d\n", err);
956 		goto free_sring;
957 	}
958 	info->host = host;
959 	info->host_active = STATE_ACTIVE;
960 
961 	xenbus_switch_state(dev, XenbusStateInitialised);
962 
963 	return 0;
964 
965 free_sring:
966 	scsifront_free_ring(info);
967 	scsi_host_put(host);
968 	return err;
969 }
970 
971 static int scsifront_resume(struct xenbus_device *dev)
972 {
973 	struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
974 	struct Scsi_Host *host = info->host;
975 	int err;
976 
977 	spin_lock_irq(host->host_lock);
978 
979 	/* Finish all still pending commands. */
980 	scsifront_finish_all(info);
981 
982 	spin_unlock_irq(host->host_lock);
983 
984 	/* Reconnect to dom0. */
985 	scsifront_free_ring(info);
986 	err = scsifront_init_ring(info);
987 	if (err) {
988 		dev_err(&dev->dev, "fail to resume %d\n", err);
989 		scsi_host_put(host);
990 		return err;
991 	}
992 
993 	xenbus_switch_state(dev, XenbusStateInitialised);
994 
995 	return 0;
996 }
997 
998 static int scsifront_suspend(struct xenbus_device *dev)
999 {
1000 	struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
1001 	struct Scsi_Host *host = info->host;
1002 	int err = 0;
1003 
1004 	/* No new commands for the backend. */
1005 	spin_lock_irq(host->host_lock);
1006 	info->pause = 1;
1007 	while (info->callers && !err) {
1008 		info->waiting_pause = 1;
1009 		info->wait_ring_available = 0;
1010 		spin_unlock_irq(host->host_lock);
1011 		wake_up(&info->wq_sync);
1012 		err = wait_event_interruptible(info->wq_pause,
1013 					       !info->waiting_pause);
1014 		spin_lock_irq(host->host_lock);
1015 	}
1016 	spin_unlock_irq(host->host_lock);
1017 	return err;
1018 }
1019 
1020 static int scsifront_remove(struct xenbus_device *dev)
1021 {
1022 	struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
1023 
1024 	pr_debug("%s: %s removed\n", __func__, dev->nodename);
1025 
1026 	mutex_lock(&scsifront_mutex);
1027 	if (info->host_active != STATE_INACTIVE) {
1028 		/* Scsi_host not yet removed */
1029 		scsi_remove_host(info->host);
1030 		info->host_active = STATE_INACTIVE;
1031 	}
1032 	mutex_unlock(&scsifront_mutex);
1033 
1034 	scsifront_free_ring(info);
1035 	scsi_host_put(info->host);
1036 
1037 	return 0;
1038 }
1039 
1040 static void scsifront_disconnect(struct vscsifrnt_info *info)
1041 {
1042 	struct xenbus_device *dev = info->dev;
1043 	struct Scsi_Host *host = info->host;
1044 
1045 	pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
1046 
1047 	/*
1048 	 * When this function is executed, all devices of
1049 	 * Frontend have been deleted.
1050 	 * Therefore, it need not block I/O before remove_host.
1051 	 */
1052 
1053 	mutex_lock(&scsifront_mutex);
1054 	if (info->host_active != STATE_INACTIVE) {
1055 		scsi_remove_host(host);
1056 		info->host_active = STATE_INACTIVE;
1057 	}
1058 	mutex_unlock(&scsifront_mutex);
1059 
1060 	xenbus_frontend_closed(dev);
1061 }
1062 
1063 static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
1064 {
1065 	struct xenbus_device *dev = info->dev;
1066 	int i, err = 0;
1067 	char str[64];
1068 	char **dir;
1069 	unsigned int dir_n = 0;
1070 	unsigned int device_state;
1071 	unsigned int hst, chn, tgt, lun;
1072 	struct scsi_device *sdev;
1073 
1074 	if (info->host_active == STATE_ERROR)
1075 		return;
1076 
1077 	dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
1078 	if (IS_ERR(dir))
1079 		return;
1080 
1081 	/* mark current task as the one allowed to modify device states */
1082 	BUG_ON(info->curr);
1083 	info->curr = current;
1084 
1085 	for (i = 0; i < dir_n; i++) {
1086 		/* read status */
1087 		snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
1088 		err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
1089 				   &device_state);
1090 		if (XENBUS_EXIST_ERR(err))
1091 			continue;
1092 
1093 		/* virtual SCSI device */
1094 		snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
1095 		err = xenbus_scanf(XBT_NIL, dev->otherend, str,
1096 				   "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
1097 		if (XENBUS_EXIST_ERR(err))
1098 			continue;
1099 
1100 		/*
1101 		 * Front device state path, used in slave_configure called
1102 		 * on successfull scsi_add_device, and in slave_destroy called
1103 		 * on remove of a device.
1104 		 */
1105 		snprintf(info->dev_state_path, sizeof(info->dev_state_path),
1106 			 "vscsi-devs/%s/state", dir[i]);
1107 
1108 		switch (op) {
1109 		case VSCSIFRONT_OP_ADD_LUN:
1110 			if (device_state != XenbusStateInitialised)
1111 				break;
1112 
1113 			if (scsi_add_device(info->host, chn, tgt, lun)) {
1114 				dev_err(&dev->dev, "scsi_add_device\n");
1115 				err = xenbus_printf(XBT_NIL, dev->nodename,
1116 					      info->dev_state_path,
1117 					      "%d", XenbusStateClosed);
1118 				if (err)
1119 					xenbus_dev_error(dev, err,
1120 						"%s: writing dev_state_path", __func__);
1121 			}
1122 			break;
1123 		case VSCSIFRONT_OP_DEL_LUN:
1124 			if (device_state != XenbusStateClosing)
1125 				break;
1126 
1127 			sdev = scsi_device_lookup(info->host, chn, tgt, lun);
1128 			if (sdev) {
1129 				scsi_remove_device(sdev);
1130 				scsi_device_put(sdev);
1131 			}
1132 			break;
1133 		case VSCSIFRONT_OP_READD_LUN:
1134 			if (device_state == XenbusStateConnected) {
1135 				err = xenbus_printf(XBT_NIL, dev->nodename,
1136 					      info->dev_state_path,
1137 					      "%d", XenbusStateConnected);
1138 				if (err)
1139 					xenbus_dev_error(dev, err,
1140 						"%s: writing dev_state_path", __func__);
1141 			}
1142 			break;
1143 		default:
1144 			break;
1145 		}
1146 	}
1147 
1148 	info->curr = NULL;
1149 
1150 	kfree(dir);
1151 }
1152 
1153 static void scsifront_read_backend_params(struct xenbus_device *dev,
1154 					  struct vscsifrnt_info *info)
1155 {
1156 	unsigned int sg_grant, nr_segs;
1157 	struct Scsi_Host *host = info->host;
1158 
1159 	sg_grant = xenbus_read_unsigned(dev->otherend, "feature-sg-grant", 0);
1160 	nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
1161 	nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
1162 	nr_segs = min_t(unsigned int, nr_segs,
1163 			VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
1164 			sizeof(struct scsiif_request_segment));
1165 
1166 	if (!info->pause && sg_grant)
1167 		dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
1168 	else if (info->pause && nr_segs < host->sg_tablesize)
1169 		dev_warn(&dev->dev,
1170 			 "SG entries decreased from %d to %u - device may not work properly anymore\n",
1171 			 host->sg_tablesize, nr_segs);
1172 
1173 	host->sg_tablesize = nr_segs;
1174 	host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
1175 }
1176 
1177 static void scsifront_backend_changed(struct xenbus_device *dev,
1178 				      enum xenbus_state backend_state)
1179 {
1180 	struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
1181 
1182 	pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
1183 
1184 	switch (backend_state) {
1185 	case XenbusStateUnknown:
1186 	case XenbusStateInitialising:
1187 	case XenbusStateInitWait:
1188 	case XenbusStateInitialised:
1189 		break;
1190 
1191 	case XenbusStateConnected:
1192 		scsifront_read_backend_params(dev, info);
1193 
1194 		if (info->pause) {
1195 			scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
1196 			xenbus_switch_state(dev, XenbusStateConnected);
1197 			info->pause = 0;
1198 			return;
1199 		}
1200 
1201 		if (xenbus_read_driver_state(dev->nodename) ==
1202 		    XenbusStateInitialised)
1203 			scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1204 
1205 		if (dev->state != XenbusStateConnected)
1206 			xenbus_switch_state(dev, XenbusStateConnected);
1207 		break;
1208 
1209 	case XenbusStateClosed:
1210 		if (dev->state == XenbusStateClosed)
1211 			break;
1212 		fallthrough;	/* Missed the backend's Closing state */
1213 	case XenbusStateClosing:
1214 		scsifront_disconnect(info);
1215 		break;
1216 
1217 	case XenbusStateReconfiguring:
1218 		scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
1219 		xenbus_switch_state(dev, XenbusStateReconfiguring);
1220 		break;
1221 
1222 	case XenbusStateReconfigured:
1223 		scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1224 		xenbus_switch_state(dev, XenbusStateConnected);
1225 		break;
1226 	}
1227 }
1228 
1229 static const struct xenbus_device_id scsifront_ids[] = {
1230 	{ "vscsi" },
1231 	{ "" }
1232 };
1233 
1234 static struct xenbus_driver scsifront_driver = {
1235 	.ids			= scsifront_ids,
1236 	.probe			= scsifront_probe,
1237 	.remove			= scsifront_remove,
1238 	.resume			= scsifront_resume,
1239 	.suspend		= scsifront_suspend,
1240 	.otherend_changed	= scsifront_backend_changed,
1241 };
1242 
1243 static int __init scsifront_init(void)
1244 {
1245 	if (!xen_domain())
1246 		return -ENODEV;
1247 
1248 	return xenbus_register_frontend(&scsifront_driver);
1249 }
1250 module_init(scsifront_init);
1251 
1252 static void __exit scsifront_exit(void)
1253 {
1254 	xenbus_unregister_driver(&scsifront_driver);
1255 }
1256 module_exit(scsifront_exit);
1257 
1258 MODULE_DESCRIPTION("Xen SCSI frontend driver");
1259 MODULE_LICENSE("GPL");
1260 MODULE_ALIAS("xen:vscsi");
1261 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");
1262