- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / blktap / interface.c
1 /******************************************************************************
2  * drivers/xen/blktap/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
34 #include "common.h"
35 #include <xen/evtchn.h>
36 #include <linux/vmalloc.h>
37
38 static struct kmem_cache *blkif_cachep;
39
40 blkif_t *tap_alloc_blkif(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         blkif->st_print = jiffies;
53         init_waitqueue_head(&blkif->waiting_to_free);
54
55         return blkif;
56 }
57
58 int tap_blkif_map(blkif_t *blkif, struct xenbus_device *dev,
59                   grant_ref_t ring_ref, evtchn_port_t evtchn)
60 {
61         struct vm_struct *area;
62         int err;
63
64         /* Already connected through? */
65         if (blkif->irq)
66                 return 0;
67
68         area = xenbus_map_ring_valloc(dev, ring_ref);
69         if (IS_ERR(area))
70                 return PTR_ERR(area);
71         blkif->blk_ring_area = area;
72
73         switch (blkif->blk_protocol) {
74         case BLKIF_PROTOCOL_NATIVE:
75         {
76                 blkif_sring_t *sring;
77                 sring = (blkif_sring_t *)area->addr;
78                 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
79                 break;
80         }
81         case BLKIF_PROTOCOL_X86_32:
82         {
83                 blkif_x86_32_sring_t *sring_x86_32;
84                 sring_x86_32 = (blkif_x86_32_sring_t *)area->addr;
85                 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
86                 break;
87         }
88         case BLKIF_PROTOCOL_X86_64:
89         {
90                 blkif_x86_64_sring_t *sring_x86_64;
91                 sring_x86_64 = (blkif_x86_64_sring_t *)area->addr;
92                 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
93                 break;
94         }
95         default:
96                 BUG();
97         }
98
99         err = bind_interdomain_evtchn_to_irqhandler(
100                 blkif->domid, evtchn, tap_blkif_be_int,
101                 0, "blkif-backend", blkif);
102         if (err < 0) {
103                 xenbus_unmap_ring_vfree(dev, area);
104                 blkif->blk_rings.common.sring = NULL;
105                 return err;
106         }
107         blkif->irq = err;
108
109         return 0;
110 }
111
112 void tap_blkif_free(blkif_t *blkif, struct xenbus_device *dev)
113 {
114         atomic_dec(&blkif->refcnt);
115         wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
116         atomic_inc(&blkif->refcnt);
117
118         if (blkif->irq) {
119                 unbind_from_irqhandler(blkif->irq, blkif);
120                 blkif->irq = 0;
121         }
122
123         if (blkif->blk_rings.common.sring) {
124                 xenbus_unmap_ring_vfree(dev, blkif->blk_ring_area);
125                 blkif->blk_rings.common.sring = NULL;
126         }
127 }
128
129 void tap_blkif_kmem_cache_free(blkif_t *blkif)
130 {
131         if (!atomic_dec_and_test(&blkif->refcnt))
132                 BUG();
133         kmem_cache_free(blkif_cachep, blkif);
134 }
135
136 void __init tap_blkif_interface_init(void)
137 {
138         blkif_cachep = kmem_cache_create("blktapif_cache", sizeof(blkif_t), 
139                                          0, 0, NULL);
140 }