xen-blkback-porting
[firefly-linux-kernel-4.4.55.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/events.h>
35 #include <xen/grant_table.h>
36 #include <linux/kthread.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_alloc(blkif_cachep, GFP_KERNEL);
45         if (!blkif)
46                 return ERR_PTR(-ENOMEM);
47
48         memset(blkif, 0, sizeof(*blkif));
49         blkif->domid = domid;
50         spin_lock_init(&blkif->blk_ring_lock);
51         atomic_set(&blkif->refcnt, 1);
52         init_waitqueue_head(&blkif->wq);
53         blkif->st_print = jiffies;
54         init_waitqueue_head(&blkif->waiting_to_free);
55
56         return blkif;
57 }
58
59 static int map_frontend_page(blkif_t *blkif, unsigned long shared_page)
60 {
61         struct gnttab_map_grant_ref op;
62
63         gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr,
64                           GNTMAP_host_map, shared_page, blkif->domid);
65
66         if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
67                 BUG();
68
69         if (op.status) {
70                 DPRINTK(" Grant table operation failure !\n");
71                 return op.status;
72         }
73
74         blkif->shmem_ref = shared_page;
75         blkif->shmem_handle = op.handle;
76
77         return 0;
78 }
79
80 static void unmap_frontend_page(blkif_t *blkif)
81 {
82         struct gnttab_unmap_grant_ref op;
83
84         gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr,
85                             GNTMAP_host_map, blkif->shmem_handle);
86
87         if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
88                 BUG();
89 }
90
91 int blkif_map(blkif_t *blkif, unsigned long shared_page, unsigned int evtchn)
92 {
93         int err;
94
95         /* Already connected through? */
96         if (blkif->irq)
97                 return 0;
98
99         if ( (blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE)) == NULL )
100                 return -ENOMEM;
101
102         err = map_frontend_page(blkif, shared_page);
103         if (err) {
104                 free_vm_area(blkif->blk_ring_area);
105                 return err;
106         }
107
108         switch (blkif->blk_protocol) {
109         case BLKIF_PROTOCOL_NATIVE:
110         {
111                 struct blkif_sring *sring;
112                 sring = (struct blkif_sring *)blkif->blk_ring_area->addr;
113                 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
114                 break;
115         }
116         case BLKIF_PROTOCOL_X86_32:
117         {
118                 struct blkif_x86_32_sring *sring_x86_32;
119                 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring_area->addr;
120                 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
121                 break;
122         }
123         case BLKIF_PROTOCOL_X86_64:
124         {
125                 struct blkif_x86_64_sring *sring_x86_64;
126                 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring_area->addr;
127                 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
128                 break;
129         }
130         default:
131                 BUG();
132         }
133
134         err = bind_interdomain_evtchn_to_irqhandler(
135                 blkif->domid, evtchn, blkif_be_int, 0, "blkif-backend", blkif);
136         if (err < 0)
137         {
138                 unmap_frontend_page(blkif);
139                 free_vm_area(blkif->blk_ring_area);
140                 blkif->blk_rings.common.sring = NULL;
141                 return err;
142         }
143         blkif->irq = err;
144
145         return 0;
146 }
147
148 void blkif_disconnect(blkif_t *blkif)
149 {
150         if (blkif->xenblkd) {
151                 kthread_stop(blkif->xenblkd);
152                 blkif->xenblkd = NULL;
153         }
154
155         atomic_dec(&blkif->refcnt);
156         wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
157         atomic_inc(&blkif->refcnt);
158
159         if (blkif->irq) {
160                 unbind_from_irqhandler(blkif->irq, blkif);
161                 blkif->irq = 0;
162         }
163
164         if (blkif->blk_rings.common.sring) {
165                 unmap_frontend_page(blkif);
166                 free_vm_area(blkif->blk_ring_area);
167                 blkif->blk_rings.common.sring = NULL;
168         }
169 }
170
171 void blkif_free(blkif_t *blkif)
172 {
173         if (!atomic_dec_and_test(&blkif->refcnt))
174                 BUG();
175         kmem_cache_free(blkif_cachep, blkif);
176 }
177
178 void __init blkif_interface_init(void)
179 {
180         blkif_cachep = kmem_cache_create("blkif_cache", sizeof(blkif_t),
181                                          0, 0, NULL);
182 }