Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / blkback / interface.c
1 /******************************************************************************
2  * arch/xen/drivers/blkif/backend/interface.c
3  * 
4  * Block-device interface management.
5  * 
6  * Copyright (c) 2004, Keir Fraser
7  * 
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation; or, when distributed
11  * separately from the Linux kernel or incorporated into other
12  * software packages, subject to the following license:
13  * 
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  * 
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  * 
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  */
32
33 #include "common.h"
34 #include <xen/evtchn.h>
35 #include <linux/kthread.h>
36 #include <linux/vmalloc.h>
37
38 static struct kmem_cache *blkif_cachep;
39
40 blkif_t *blkif_alloc(domid_t domid)
41 {
42         blkif_t *blkif;
43
44         blkif = kmem_cache_zalloc(blkif_cachep, GFP_KERNEL);
45         if (!blkif)
46                 return ERR_PTR(-ENOMEM);
47
48         blkif->domid = domid;
49         spin_lock_init(&blkif->blk_ring_lock);
50         atomic_set(&blkif->refcnt, 1);
51         init_waitqueue_head(&blkif->wq);
52         init_completion(&blkif->drain_complete);
53         atomic_set(&blkif->drain, 0);
54         blkif->st_print = jiffies;
55         init_waitqueue_head(&blkif->waiting_to_free);
56
57         return blkif;
58 }
59
60 int blkif_map(blkif_t *blkif, grant_ref_t ring_ref, evtchn_port_t evtchn)
61 {
62         struct vm_struct *area;
63         int err;
64
65         /* Already connected through? */
66         if (blkif->irq)
67                 return 0;
68
69         area = xenbus_map_ring_valloc(blkif->be->dev, ring_ref);
70         if (IS_ERR(area))
71                 return PTR_ERR(area);
72         blkif->blk_ring_area = area;
73
74         switch (blkif->blk_protocol) {
75 #define BLKBK_RING_INIT(p) ({ \
76                 struct blkif_##p##_sring *sring = area->addr; \
77                 BACK_RING_INIT(&blkif->blk_rings.p, sring, PAGE_SIZE); \
78         })
79         case BLKIF_PROTOCOL_NATIVE:
80                 BLKBK_RING_INIT(native);
81                 break;
82         case BLKIF_PROTOCOL_X86_32:
83                 BLKBK_RING_INIT(x86_32);
84                 break;
85         case BLKIF_PROTOCOL_X86_64:
86                 BLKBK_RING_INIT(x86_64);
87                 break;
88         default:
89                 BUG();
90 #undef BLKBK_RING_INIT
91         }
92
93         err = bind_interdomain_evtchn_to_irqhandler(
94                 blkif->domid, evtchn, blkif_be_int, 0, "blkif-backend", blkif);
95         if (err < 0)
96         {
97                 xenbus_unmap_ring_vfree(blkif->be->dev, area);
98                 blkif->blk_rings.common.sring = NULL;
99                 return err;
100         }
101         blkif->irq = err;
102
103         return 0;
104 }
105
106 void blkif_disconnect(blkif_t *blkif)
107 {
108         if (blkif->xenblkd) {
109                 kthread_stop(blkif->xenblkd);
110                 blkif->xenblkd = NULL;
111         }
112
113         atomic_dec(&blkif->refcnt);
114         wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
115         atomic_inc(&blkif->refcnt);
116
117         if (blkif->irq) {
118                 unbind_from_irqhandler(blkif->irq, blkif);
119                 blkif->irq = 0;
120         }
121
122         if (blkif->blk_rings.common.sring) {
123                 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring_area);
124                 blkif->blk_rings.common.sring = NULL;
125         }
126 }
127
128 void blkif_free(blkif_t *blkif)
129 {
130         if (!atomic_dec_and_test(&blkif->refcnt))
131                 BUG();
132         kmem_cache_free(blkif_cachep, blkif);
133 }
134
135 void __init blkif_interface_init(void)
136 {
137         blkif_cachep = kmem_cache_create("blkif_cache", sizeof(blkif_t), 
138                                          0, 0, NULL);
139 }