Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[linux-2.6.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
37 static kmem_cache_t *blkif_cachep;
38
39 blkif_t *blkif_alloc(domid_t domid)
40 {
41         blkif_t *blkif;
42
43         blkif = kmem_cache_alloc(blkif_cachep, GFP_KERNEL);
44         if (!blkif)
45                 return ERR_PTR(-ENOMEM);
46
47         memset(blkif, 0, sizeof(*blkif));
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 static int map_frontend_page(blkif_t *blkif, unsigned long shared_page)
59 {
60         struct gnttab_map_grant_ref op;
61         int ret;
62
63         gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr,
64                           GNTMAP_host_map, shared_page, blkif->domid);
65
66         lock_vm_area(blkif->blk_ring_area);
67         ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1);
68         unlock_vm_area(blkif->blk_ring_area);
69         BUG_ON(ret);
70
71         if (op.status) {
72                 DPRINTK(" Grant table operation failure !\n");
73                 return op.status;
74         }
75
76         blkif->shmem_ref = shared_page;
77         blkif->shmem_handle = op.handle;
78
79         return 0;
80 }
81
82 static void unmap_frontend_page(blkif_t *blkif)
83 {
84         struct gnttab_unmap_grant_ref op;
85         int ret;
86
87         gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr,
88                             GNTMAP_host_map, blkif->shmem_handle);
89
90         lock_vm_area(blkif->blk_ring_area);
91         ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1);
92         unlock_vm_area(blkif->blk_ring_area);
93         BUG_ON(ret);
94 }
95
96 int blkif_map(blkif_t *blkif, unsigned long shared_page, unsigned int evtchn)
97 {
98         blkif_sring_t *sring;
99         int err;
100         struct evtchn_bind_interdomain bind_interdomain;
101
102         /* Already connected through? */
103         if (blkif->irq)
104                 return 0;
105
106         if ( (blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE)) == NULL )
107                 return -ENOMEM;
108
109         err = map_frontend_page(blkif, shared_page);
110         if (err) {
111                 free_vm_area(blkif->blk_ring_area);
112                 return err;
113         }
114
115         bind_interdomain.remote_dom  = blkif->domid;
116         bind_interdomain.remote_port = evtchn;
117
118         err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
119                                           &bind_interdomain);
120         if (err) {
121                 unmap_frontend_page(blkif);
122                 free_vm_area(blkif->blk_ring_area);
123                 return err;
124         }
125
126         blkif->evtchn = bind_interdomain.local_port;
127
128         sring = (blkif_sring_t *)blkif->blk_ring_area->addr;
129         BACK_RING_INIT(&blkif->blk_ring, sring, PAGE_SIZE);
130
131         blkif->irq = bind_evtchn_to_irqhandler(
132                 blkif->evtchn, blkif_be_int, 0, "blkif-backend", blkif);
133
134         return 0;
135 }
136
137 void blkif_disconnect(blkif_t *blkif)
138 {
139         if (blkif->xenblkd) {
140                 kthread_stop(blkif->xenblkd);
141                 blkif->xenblkd = NULL;
142         }
143
144         atomic_dec(&blkif->refcnt);
145         wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
146         atomic_inc(&blkif->refcnt);
147
148         if (blkif->irq) {
149                 unbind_from_irqhandler(blkif->irq, blkif);
150                 blkif->irq = 0;
151         }
152
153         if (blkif->blk_ring.sring) {
154                 unmap_frontend_page(blkif);
155                 free_vm_area(blkif->blk_ring_area);
156                 blkif->blk_ring.sring = NULL;
157         }
158 }
159
160 void blkif_free(blkif_t *blkif)
161 {
162         if (!atomic_dec_and_test(&blkif->refcnt))
163                 BUG();
164         kmem_cache_free(blkif_cachep, blkif);
165 }
166
167 void __init blkif_interface_init(void)
168 {
169         blkif_cachep = kmem_cache_create("blkif_cache", sizeof(blkif_t), 
170                                          0, 0, NULL, NULL);
171 }