at commit c03c1a14

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_TRANSPORT_MARGINAL:
293 return DID_TRANSPORT_MARGINAL;
294 default:
295 return DID_ERROR;
296 }
297 }
298
299 static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info,
300 struct vscsiif_response *ring_rsp)
301 {
302 struct vscsifrnt_shadow *shadow;
303 struct scsi_cmnd *sc;
304 uint32_t id;
305 uint8_t sense_len;
306
307 id = ring_rsp->rqid;
308 shadow = info->shadow[id];
309 sc = shadow->sc;
310
311 BUG_ON(sc == NULL);
312
313 scsifront_gnttab_done(info, shadow);
314 if (info->host_active == STATE_ERROR)
315 return;
316 scsifront_put_rqid(info, id);
317
318 set_host_byte(sc, scsifront_host_byte(ring_rsp->rslt));
319 set_status_byte(sc, XEN_VSCSIIF_RSLT_STATUS(ring_rsp->rslt));
320 scsi_set_resid(sc, ring_rsp->residual_len);
321
322 sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE,
323 ring_rsp->sense_len);
324
325 if (sense_len)
326 memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len);
327
328 scsi_done(sc);
329 }
330
331 static void scsifront_sync_cmd_done(struct vscsifrnt_info *info,
332 struct vscsiif_response *ring_rsp)
333 {
334 uint16_t id = ring_rsp->rqid;
335 unsigned long flags;
336 struct vscsifrnt_shadow *shadow = info->shadow[id];
337 int kick;
338
339 spin_lock_irqsave(&info->shadow_lock, flags);
340 shadow->wait_reset = 1;
341 switch (shadow->rslt_reset) {
342 case RSLT_RESET_WAITING:
343 if (ring_rsp->rslt == XEN_VSCSIIF_RSLT_RESET_SUCCESS)
344 shadow->rslt_reset = SUCCESS;
345 else
346 shadow->rslt_reset = FAILED;
347 break;
348 case RSLT_RESET_ERR:
349 kick = _scsifront_put_rqid(info, id);
350 spin_unlock_irqrestore(&info->shadow_lock, flags);
351 kfree(shadow);
352 if (kick)
353 scsifront_wake_up(info);
354 return;
355 default:
356 scsifront_set_error(info, "bad reset state");
357 break;
358 }
359 spin_unlock_irqrestore(&info->shadow_lock, flags);
360
361 wake_up(&shadow->wq_reset);
362 }
363
364 static void scsifront_do_response(struct vscsifrnt_info *info,
365 struct vscsiif_response *ring_rsp)
366 {
367 struct vscsifrnt_shadow *shadow;
368
369 if (ring_rsp->rqid >= VSCSIIF_MAX_REQS ||
370 !info->shadow[ring_rsp->rqid] ||
371 !info->shadow[ring_rsp->rqid]->inflight) {
372 scsifront_set_error(info, "illegal rqid returned by backend!");
373 return;
374 }
375 shadow = info->shadow[ring_rsp->rqid];
376 shadow->inflight = false;
377
378 if (shadow->act == VSCSIIF_ACT_SCSI_CDB)
379 scsifront_cdb_cmd_done(info, ring_rsp);
380 else
381 scsifront_sync_cmd_done(info, ring_rsp);
382 }
383
384 static int scsifront_ring_drain(struct vscsifrnt_info *info,
385 unsigned int *eoiflag)
386 {
387 struct vscsiif_response ring_rsp;
388 RING_IDX i, rp;
389 int more_to_do = 0;
390
391 rp = READ_ONCE(info->ring.sring->rsp_prod);
392 virt_rmb(); /* ordering required respective to backend */
393 if (RING_RESPONSE_PROD_OVERFLOW(&info->ring, rp)) {
394 scsifront_set_error(info, "illegal number of responses");
395 return 0;
396 }
397 for (i = info->ring.rsp_cons; i != rp; i++) {
398 RING_COPY_RESPONSE(&info->ring, i, &ring_rsp);
399 scsifront_do_response(info, &ring_rsp);
400 if (info->host_active == STATE_ERROR)
401 return 0;
402 *eoiflag &= ~XEN_EOI_FLAG_SPURIOUS;
403 }
404
405 info->ring.rsp_cons = i;
406
407 if (i != info->ring.req_prod_pvt)
408 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
409 else
410 info->ring.sring->rsp_event = i + 1;
411
412 return more_to_do;
413 }
414
415 static int scsifront_cmd_done(struct vscsifrnt_info *info,
416 unsigned int *eoiflag)
417 {
418 int more_to_do;
419 unsigned long flags;
420
421 spin_lock_irqsave(info->host->host_lock, flags);
422
423 more_to_do = scsifront_ring_drain(info, eoiflag);
424
425 info->wait_ring_available = 0;
426
427 spin_unlock_irqrestore(info->host->host_lock, flags);
428
429 wake_up(&info->wq_sync);
430
431 return more_to_do;
432 }
433
434 static irqreturn_t scsifront_irq_fn(int irq, void *dev_id)
435 {
436 struct vscsifrnt_info *info = dev_id;
437 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
438
439 if (info->host_active == STATE_ERROR) {
440 xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
441 return IRQ_HANDLED;
442 }
443
444 while (scsifront_cmd_done(info, &eoiflag))
445 /* Yield point for this unbounded loop. */
446 cond_resched();
447
448 xen_irq_lateeoi(irq, eoiflag);
449
450 return IRQ_HANDLED;
451 }
452
453 static void scsifront_finish_all(struct vscsifrnt_info *info)
454 {
455 unsigned int i, dummy;
456 struct vscsiif_response resp;
457
458 scsifront_ring_drain(info, &dummy);
459
460 for (i = 0; i < VSCSIIF_MAX_REQS; i++) {
461 if (test_bit(i, info->shadow_free_bitmap))
462 continue;
463 resp.rqid = i;
464 resp.sense_len = 0;
465 resp.rslt = DID_RESET << 16;
466 resp.residual_len = 0;
467 scsifront_do_response(info, &resp);
468 }
469 }
470
471 static int map_data_for_request(struct vscsifrnt_info *info,
472 struct scsi_cmnd *sc,
473 struct vscsifrnt_shadow *shadow)
474 {
475 grant_ref_t gref_head;
476 struct page *page;
477 int err, ref, ref_cnt = 0;
478 int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE);
479 unsigned int i, off, len, bytes;
480 unsigned int data_len = scsi_bufflen(sc);
481 unsigned int data_grants = 0, seg_grants = 0;
482 struct scatterlist *sg;
483 struct scsiif_request_segment *seg;
484
485 if (sc->sc_data_direction == DMA_NONE || !data_len)
486 return 0;
487
488 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i)
489 data_grants += PFN_UP(sg->offset + sg->length);
490
491 if (data_grants > VSCSIIF_SG_TABLESIZE) {
492 if (data_grants > info->host->sg_tablesize) {
493 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
494 "Unable to map request_buffer for command!\n");
495 return -E2BIG;
496 }
497 seg_grants = vscsiif_grants_sg(data_grants);
498 shadow->sg = kzalloc_objs(struct scsiif_request_segment,
499 data_grants, GFP_ATOMIC);
500 if (!shadow->sg)
501 return -ENOMEM;
502 }
503 seg = shadow->sg ? : shadow->seg;
504
505 err = gnttab_alloc_grant_references(seg_grants + data_grants,
506 &gref_head);
507 if (err) {
508 kfree(shadow->sg);
509 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
510 "gnttab_alloc_grant_references() error\n");
511 return -ENOMEM;
512 }
513
514 if (seg_grants) {
515 page = virt_to_page(seg);
516 off = offset_in_page(seg);
517 len = sizeof(struct scsiif_request_segment) * data_grants;
518 while (len > 0) {
519 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
520
521 ref = gnttab_claim_grant_reference(&gref_head);
522 BUG_ON(ref == -ENOSPC);
523
524 gnttab_grant_foreign_access_ref(ref,
525 info->dev->otherend_id,
526 xen_page_to_gfn(page), 1);
527 shadow->gref[ref_cnt] = ref;
528 shadow->seg[ref_cnt].gref = ref;
529 shadow->seg[ref_cnt].offset = (uint16_t)off;
530 shadow->seg[ref_cnt].length = (uint16_t)bytes;
531
532 page++;
533 len -= bytes;
534 off = 0;
535 ref_cnt++;
536 }
537 BUG_ON(seg_grants < ref_cnt);
538 seg_grants = ref_cnt;
539 }
540
541 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) {
542 page = sg_page(sg);
543 off = sg->offset;
544 len = sg->length;
545
546 while (len > 0 && data_len > 0) {
547 /*
548 * sg sends a scatterlist that is larger than
549 * the data_len it wants transferred for certain
550 * IO sizes.
551 */
552 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
553 bytes = min(bytes, data_len);
554
555 ref = gnttab_claim_grant_reference(&gref_head);
556 BUG_ON(ref == -ENOSPC);
557
558 gnttab_grant_foreign_access_ref(ref,
559 info->dev->otherend_id,
560 xen_page_to_gfn(page),
561 grant_ro);
562
563 shadow->gref[ref_cnt] = ref;
564 seg->gref = ref;
565 seg->offset = (uint16_t)off;
566 seg->length = (uint16_t)bytes;
567
568 page++;
569 seg++;
570 len -= bytes;
571 data_len -= bytes;
572 off = 0;
573 ref_cnt++;
574 }
575 }
576
577 if (seg_grants)
578 shadow->nr_segments = VSCSIIF_SG_GRANT | seg_grants;
579 else
580 shadow->nr_segments = (uint8_t)ref_cnt;
581 shadow->nr_grants = ref_cnt;
582
583 return 0;
584 }
585
586 static int scsifront_enter(struct vscsifrnt_info *info)
587 {
588 if (info->pause)
589 return 1;
590 info->callers++;
591 return 0;
592 }
593
594 static void scsifront_return(struct vscsifrnt_info *info)
595 {
596 info->callers--;
597 if (info->callers)
598 return;
599
600 if (!info->waiting_pause)
601 return;
602
603 info->waiting_pause = 0;
604 wake_up(&info->wq_pause);
605 }
606
607 static enum scsi_qc_status scsifront_queuecommand(struct Scsi_Host *shost,
608 struct scsi_cmnd *sc)
609 {
610 struct vscsifrnt_info *info = shost_priv(shost);
611 struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc);
612 unsigned long flags;
613 int err;
614
615 if (info->host_active == STATE_ERROR)
616 return SCSI_MLQUEUE_HOST_BUSY;
617
618 sc->result = 0;
619
620 shadow->sc = sc;
621 shadow->act = VSCSIIF_ACT_SCSI_CDB;
622
623 spin_lock_irqsave(shost->host_lock, flags);
624 if (scsifront_enter(info)) {
625 spin_unlock_irqrestore(shost->host_lock, flags);
626 return SCSI_MLQUEUE_HOST_BUSY;
627 }
628
629 err = map_data_for_request(info, sc, shadow);
630 if (err < 0) {
631 pr_debug("%s: err %d\n", __func__, err);
632 scsifront_return(info);
633 spin_unlock_irqrestore(shost->host_lock, flags);
634 if (err == -ENOMEM)
635 return SCSI_MLQUEUE_HOST_BUSY;
636 sc->result = DID_ERROR << 16;
637 scsi_done(sc);
638 return 0;
639 }
640
641 if (scsifront_do_request(info, shadow)) {
642 scsifront_gnttab_done(info, shadow);
643 goto busy;
644 }
645
646 scsifront_return(info);
647 spin_unlock_irqrestore(shost->host_lock, flags);
648
649 return 0;
650
651 busy:
652 scsifront_return(info);
653 spin_unlock_irqrestore(shost->host_lock, flags);
654 pr_debug("%s: busy\n", __func__);
655 return SCSI_MLQUEUE_HOST_BUSY;
656 }
657
658 /*
659 * Any exception handling (reset or abort) must be forwarded to the backend.
660 * We have to wait until an answer is returned. This answer contains the
661 * result to be returned to the requestor.
662 */
663 static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)
664 {
665 struct Scsi_Host *host = sc->device->host;
666 struct vscsifrnt_info *info = shost_priv(host);
667 struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc);
668 int err = 0;
669
670 if (info->host_active == STATE_ERROR)
671 return FAILED;
672
673 shadow = kzalloc_obj(*shadow, GFP_NOIO);
674 if (!shadow)
675 return FAILED;
676
677 shadow->act = act;
678 shadow->rslt_reset = RSLT_RESET_WAITING;
679 shadow->sc = sc;
680 shadow->ref_rqid = s->rqid;
681 init_waitqueue_head(&shadow->wq_reset);
682
683 spin_lock_irq(host->host_lock);
684
685 for (;;) {
686 if (scsifront_enter(info))
687 goto fail;
688
689 if (!scsifront_do_request(info, shadow))
690 break;
691
692 scsifront_return(info);
693 if (err)
694 goto fail;
695 info->wait_ring_available = 1;
696 spin_unlock_irq(host->host_lock);
697 err = wait_event_interruptible(info->wq_sync,
698 !info->wait_ring_available);
699 spin_lock_irq(host->host_lock);
700 }
701
702 spin_unlock_irq(host->host_lock);
703 err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset);
704 spin_lock_irq(host->host_lock);
705
706 if (!err) {
707 err = shadow->rslt_reset;
708 scsifront_put_rqid(info, shadow->rqid);
709 kfree(shadow);
710 } else {
711 spin_lock(&info->shadow_lock);
712 shadow->rslt_reset = RSLT_RESET_ERR;
713 spin_unlock(&info->shadow_lock);
714 err = FAILED;
715 }
716
717 scsifront_return(info);
718 spin_unlock_irq(host->host_lock);
719 return err;
720
721 fail:
722 spin_unlock_irq(host->host_lock);
723 kfree(shadow);
724 return FAILED;
725 }
726
727 static int scsifront_eh_abort_handler(struct scsi_cmnd *sc)
728 {
729 pr_debug("%s\n", __func__);
730 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT);
731 }
732
733 static int scsifront_dev_reset_handler(struct scsi_cmnd *sc)
734 {
735 pr_debug("%s\n", __func__);
736 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET);
737 }
738
739 static int scsifront_sdev_configure(struct scsi_device *sdev,
740 struct queue_limits *lim)
741 {
742 struct vscsifrnt_info *info = shost_priv(sdev->host);
743 int err;
744
745 if (info->host_active == STATE_ERROR)
746 return -EIO;
747
748 if (current == info->curr) {
749 err = xenbus_printf(XBT_NIL, info->dev->nodename,
750 info->dev_state_path, "%d", XenbusStateConnected);
751 if (err) {
752 xenbus_dev_error(info->dev, err,
753 "%s: writing dev_state_path", __func__);
754 return err;
755 }
756 }
757
758 return 0;
759 }
760
761 static void scsifront_sdev_destroy(struct scsi_device *sdev)
762 {
763 struct vscsifrnt_info *info = shost_priv(sdev->host);
764 int err;
765
766 if (current == info->curr) {
767 err = xenbus_printf(XBT_NIL, info->dev->nodename,
768 info->dev_state_path, "%d", XenbusStateClosed);
769 if (err)
770 xenbus_dev_error(info->dev, err,
771 "%s: writing dev_state_path", __func__);
772 }
773 }
774
775 static const struct scsi_host_template scsifront_sht = {
776 .module = THIS_MODULE,
777 .name = "Xen SCSI frontend driver",
778 .queuecommand = scsifront_queuecommand,
779 .eh_abort_handler = scsifront_eh_abort_handler,
780 .eh_device_reset_handler = scsifront_dev_reset_handler,
781 .sdev_configure = scsifront_sdev_configure,
782 .sdev_destroy = scsifront_sdev_destroy,
783 .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN,
784 .can_queue = VSCSIIF_MAX_REQS,
785 .this_id = -1,
786 .cmd_size = sizeof(struct vscsifrnt_shadow),
787 .sg_tablesize = VSCSIIF_SG_TABLESIZE,
788 .proc_name = "scsifront",
789 };
790
791 static int scsifront_alloc_ring(struct vscsifrnt_info *info)
792 {
793 struct xenbus_device *dev = info->dev;
794 struct vscsiif_sring *sring;
795 int err;
796
797 /***** Frontend to Backend ring start *****/
798 err = xenbus_setup_ring(dev, GFP_KERNEL, (void **)&sring, 1,
799 &info->ring_ref);
800 if (err)
801 return err;
802
803 XEN_FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
804
805 err = xenbus_alloc_evtchn(dev, &info->evtchn);
806 if (err) {
807 xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
808 goto free_gnttab;
809 }
810
811 err = bind_evtchn_to_irq_lateeoi(info->evtchn);
812 if (err <= 0) {
813 xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
814 goto free_gnttab;
815 }
816
817 info->irq = err;
818
819 err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn,
820 IRQF_ONESHOT, "scsifront", info);
821 if (err) {
822 xenbus_dev_fatal(dev, err, "request_threaded_irq");
823 goto free_irq;
824 }
825
826 return 0;
827
828 /* free resource */
829 free_irq:
830 unbind_from_irqhandler(info->irq, info);
831 free_gnttab:
832 xenbus_teardown_ring((void **)&sring, 1, &info->ring_ref);
833
834 return err;
835 }
836
837 static void scsifront_free_ring(struct vscsifrnt_info *info)
838 {
839 unbind_from_irqhandler(info->irq, info);
840 xenbus_teardown_ring((void **)&info->ring.sring, 1, &info->ring_ref);
841 }
842
843 static int scsifront_init_ring(struct vscsifrnt_info *info)
844 {
845 struct xenbus_device *dev = info->dev;
846 struct xenbus_transaction xbt;
847 int err;
848
849 pr_debug("%s\n", __func__);
850
851 err = scsifront_alloc_ring(info);
852 if (err)
853 return err;
854 pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn);
855
856 again:
857 err = xenbus_transaction_start(&xbt);
858 if (err)
859 xenbus_dev_fatal(dev, err, "starting transaction");
860
861 err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u",
862 info->ring_ref);
863 if (err) {
864 xenbus_dev_fatal(dev, err, "%s", "writing ring-ref");
865 goto fail;
866 }
867
868 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
869 info->evtchn);
870
871 if (err) {
872 xenbus_dev_fatal(dev, err, "%s", "writing event-channel");
873 goto fail;
874 }
875
876 err = xenbus_transaction_end(xbt, 0);
877 if (err) {
878 if (err == -EAGAIN)
879 goto again;
880 xenbus_dev_fatal(dev, err, "completing transaction");
881 goto free_sring;
882 }
883
884 return 0;
885
886 fail:
887 xenbus_transaction_end(xbt, 1);
888 free_sring:
889 scsifront_free_ring(info);
890
891 return err;
892 }
893
894
895 static int scsifront_probe(struct xenbus_device *dev,
896 const struct xenbus_device_id *id)
897 {
898 struct vscsifrnt_info *info;
899 struct Scsi_Host *host;
900 int err = -ENOMEM;
901 char name[TASK_COMM_LEN];
902
903 host = scsi_host_alloc(&scsifront_sht, sizeof(*info));
904 if (!host) {
905 xenbus_dev_fatal(dev, err, "fail to allocate scsi host");
906 return err;
907 }
908 info = shost_priv(host);
909
910 dev_set_drvdata(&dev->dev, info);
911 info->dev = dev;
912
913 bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
914
915 err = scsifront_init_ring(info);
916 if (err) {
917 scsi_host_put(host);
918 return err;
919 }
920
921 init_waitqueue_head(&info->wq_sync);
922 init_waitqueue_head(&info->wq_pause);
923 spin_lock_init(&info->shadow_lock);
924
925 snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no);
926
927 host->max_id = VSCSIIF_MAX_TARGET;
928 host->max_channel = 0;
929 host->max_lun = VSCSIIF_MAX_LUN;
930 host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512;
931 host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE;
932
933 err = scsi_add_host(host, &dev->dev);
934 if (err) {
935 dev_err(&dev->dev, "fail to add scsi host %d\n", err);
936 goto free_sring;
937 }
938 info->host = host;
939 info->host_active = STATE_ACTIVE;
940
941 xenbus_switch_state(dev, XenbusStateInitialised);
942
943 return 0;
944
945 free_sring:
946 scsifront_free_ring(info);
947 scsi_host_put(host);
948 return err;
949 }
950
951 static int scsifront_resume(struct xenbus_device *dev)
952 {
953 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
954 struct Scsi_Host *host = info->host;
955 int err;
956
957 spin_lock_irq(host->host_lock);
958
959 /* Finish all still pending commands. */
960 scsifront_finish_all(info);
961
962 spin_unlock_irq(host->host_lock);
963
964 /* Reconnect to dom0. */
965 scsifront_free_ring(info);
966 err = scsifront_init_ring(info);
967 if (err) {
968 dev_err(&dev->dev, "fail to resume %d\n", err);
969 scsi_host_put(host);
970 return err;
971 }
972
973 xenbus_switch_state(dev, XenbusStateInitialised);
974
975 return 0;
976 }
977
978 static int scsifront_suspend(struct xenbus_device *dev)
979 {
980 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
981 struct Scsi_Host *host = info->host;
982 int err = 0;
983
984 /* No new commands for the backend. */
985 spin_lock_irq(host->host_lock);
986 info->pause = 1;
987 while (info->callers && !err) {
988 info->waiting_pause = 1;
989 info->wait_ring_available = 0;
990 spin_unlock_irq(host->host_lock);
991 wake_up(&info->wq_sync);
992 err = wait_event_interruptible(info->wq_pause,
993 !info->waiting_pause);
994 spin_lock_irq(host->host_lock);
995 }
996 spin_unlock_irq(host->host_lock);
997 return err;
998 }
999
1000 static void scsifront_remove(struct xenbus_device *dev)
1001 {
1002 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
1003
1004 pr_debug("%s: %s removed\n", __func__, dev->nodename);
1005
1006 mutex_lock(&scsifront_mutex);
1007 if (info->host_active != STATE_INACTIVE) {
1008 /* Scsi_host not yet removed */
1009 scsi_remove_host(info->host);
1010 info->host_active = STATE_INACTIVE;
1011 }
1012 mutex_unlock(&scsifront_mutex);
1013
1014 scsifront_free_ring(info);
1015 scsi_host_put(info->host);
1016 }
1017
1018 static void scsifront_disconnect(struct vscsifrnt_info *info)
1019 {
1020 struct xenbus_device *dev = info->dev;
1021 struct Scsi_Host *host = info->host;
1022
1023 pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
1024
1025 /*
1026 * When this function is executed, all devices of
1027 * Frontend have been deleted.
1028 * Therefore, it need not block I/O before remove_host.
1029 */
1030
1031 mutex_lock(&scsifront_mutex);
1032 if (info->host_active != STATE_INACTIVE) {
1033 scsi_remove_host(host);
1034 info->host_active = STATE_INACTIVE;
1035 }
1036 mutex_unlock(&scsifront_mutex);
1037
1038 xenbus_frontend_closed(dev);
1039 }
1040
1041 static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
1042 {
1043 struct xenbus_device *dev = info->dev;
1044 int i, err = 0;
1045 char str[64];
1046 char **dir;
1047 unsigned int dir_n = 0;
1048 unsigned int device_state;
1049 unsigned int hst, chn, tgt, lun;
1050 struct scsi_device *sdev;
1051
1052 if (info->host_active == STATE_ERROR)
1053 return;
1054
1055 dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
1056 if (IS_ERR(dir))
1057 return;
1058
1059 /* mark current task as the one allowed to modify device states */
1060 BUG_ON(info->curr);
1061 info->curr = current;
1062
1063 for (i = 0; i < dir_n; i++) {
1064 /* read status */
1065 snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
1066 err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
1067 &device_state);
1068 if (XENBUS_EXIST_ERR(err))
1069 continue;
1070
1071 /* virtual SCSI device */
1072 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
1073 err = xenbus_scanf(XBT_NIL, dev->otherend, str,
1074 "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
1075 if (XENBUS_EXIST_ERR(err))
1076 continue;
1077
1078 /*
1079 * Front device state path, used in sdev_configure called
1080 * on successfull scsi_add_device, and in sdev_destroy called
1081 * on remove of a device.
1082 */
1083 snprintf(info->dev_state_path, sizeof(info->dev_state_path),
1084 "vscsi-devs/%s/state", dir[i]);
1085
1086 switch (op) {
1087 case VSCSIFRONT_OP_ADD_LUN:
1088 if (device_state != XenbusStateInitialised)
1089 break;
1090
1091 if (scsi_add_device(info->host, chn, tgt, lun)) {
1092 dev_err(&dev->dev, "scsi_add_device\n");
1093 err = xenbus_printf(XBT_NIL, dev->nodename,
1094 info->dev_state_path,
1095 "%d", XenbusStateClosed);
1096 if (err)
1097 xenbus_dev_error(dev, err,
1098 "%s: writing dev_state_path", __func__);
1099 }
1100 break;
1101 case VSCSIFRONT_OP_DEL_LUN:
1102 if (device_state != XenbusStateClosing)
1103 break;
1104
1105 sdev = scsi_device_lookup(info->host, chn, tgt, lun);
1106 if (sdev) {
1107 scsi_remove_device(sdev);
1108 scsi_device_put(sdev);
1109 }
1110 break;
1111 case VSCSIFRONT_OP_READD_LUN:
1112 if (device_state == XenbusStateConnected) {
1113 err = xenbus_printf(XBT_NIL, dev->nodename,
1114 info->dev_state_path,
1115 "%d", XenbusStateConnected);
1116 if (err)
1117 xenbus_dev_error(dev, err,
1118 "%s: writing dev_state_path", __func__);
1119 }
1120 break;
1121 default:
1122 break;
1123 }
1124 }
1125
1126 info->curr = NULL;
1127
1128 kfree(dir);
1129 }
1130
1131 static void scsifront_read_backend_params(struct xenbus_device *dev,
1132 struct vscsifrnt_info *info)
1133 {
1134 unsigned int sg_grant, nr_segs;
1135 struct Scsi_Host *host = info->host;
1136
1137 sg_grant = xenbus_read_unsigned(dev->otherend, "feature-sg-grant", 0);
1138 nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
1139 nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
1140 nr_segs = min_t(unsigned int, nr_segs,
1141 VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
1142 sizeof(struct scsiif_request_segment));
1143
1144 if (!info->pause && sg_grant)
1145 dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
1146 else if (info->pause && nr_segs < host->sg_tablesize)
1147 dev_warn(&dev->dev,
1148 "SG entries decreased from %d to %u - device may not work properly anymore\n",
1149 host->sg_tablesize, nr_segs);
1150
1151 host->sg_tablesize = nr_segs;
1152 host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
1153 }
1154
1155 static void scsifront_backend_changed(struct xenbus_device *dev,
1156 enum xenbus_state backend_state)
1157 {
1158 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
1159
1160 pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
1161
1162 switch (backend_state) {
1163 case XenbusStateUnknown:
1164 case XenbusStateInitialising:
1165 case XenbusStateInitWait:
1166 case XenbusStateInitialised:
1167 break;
1168
1169 case XenbusStateConnected:
1170 scsifront_read_backend_params(dev, info);
1171
1172 if (info->pause) {
1173 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
1174 xenbus_switch_state(dev, XenbusStateConnected);
1175 info->pause = 0;
1176 return;
1177 }
1178
1179 if (xenbus_read_driver_state(dev, dev->nodename) ==
1180 XenbusStateInitialised)
1181 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1182
1183 if (dev->state != XenbusStateConnected)
1184 xenbus_switch_state(dev, XenbusStateConnected);
1185 break;
1186
1187 case XenbusStateClosed:
1188 if (dev->state == XenbusStateClosed)
1189 break;
1190 fallthrough; /* Missed the backend's Closing state */
1191 case XenbusStateClosing:
1192 scsifront_disconnect(info);
1193 break;
1194
1195 case XenbusStateReconfiguring:
1196 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
1197 xenbus_switch_state(dev, XenbusStateReconfiguring);
1198 break;
1199
1200 case XenbusStateReconfigured:
1201 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1202 xenbus_switch_state(dev, XenbusStateConnected);
1203 break;
1204 }
1205 }
1206
1207 static const struct xenbus_device_id scsifront_ids[] = {
1208 { "vscsi" },
1209 { "" }
1210 };
1211
1212 static struct xenbus_driver scsifront_driver = {
1213 .ids = scsifront_ids,
1214 .probe = scsifront_probe,
1215 .remove = scsifront_remove,
1216 .resume = scsifront_resume,
1217 .suspend = scsifront_suspend,
1218 .otherend_changed = scsifront_backend_changed,
1219 };
1220
1221 static int __init scsifront_init(void)
1222 {
1223 if (!xen_domain())
1224 return -ENODEV;
1225
1226 return xenbus_register_frontend(&scsifront_driver);
1227 }
1228 module_init(scsifront_init);
1229
1230 static void __exit scsifront_exit(void)
1231 {
1232 xenbus_unregister_driver(&scsifront_driver);
1233 }
1234 module_exit(scsifront_exit);
1235
1236 MODULE_DESCRIPTION("Xen SCSI frontend driver");
1237 MODULE_LICENSE("GPL");
1238 MODULE_ALIAS("xen:vscsi");
1239 MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");