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