- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / blkback / common.h
1 /* 
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License version 2
4  * as published by the Free Software Foundation; or, when distributed
5  * separately from the Linux kernel or incorporated into other
6  * software packages, subject to the following license:
7  * 
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this source file (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  * 
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24  * IN THE SOFTWARE.
25  */
26
27 #ifndef __BLKIF__BACKEND__COMMON_H__
28 #define __BLKIF__BACKEND__COMMON_H__
29
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
32 #include <linux/blkdev.h>
33 #include <linux/wait.h>
34 #include <asm/hypervisor.h>
35 #include <xen/blkif.h>
36 #include <xen/xenbus.h>
37 #include <xen/interface/event_channel.h>
38 #include "blkback-pagemap.h"
39
40
41 #define DPRINTK(_f, _a...)                      \
42         pr_debug("(file=%s, line=%d) " _f,      \
43                  __FILE__ , __LINE__ , ## _a )
44
45 enum blkif_backend_type {
46         BLKIF_BACKEND_PHY  = 1,
47         BLKIF_BACKEND_FILE = 2,
48 };
49
50 struct vbd {
51         blkif_vdev_t   handle;      /* what the domain refers to this vbd as */
52         fmode_t        mode;        /* FMODE_xxx */
53         unsigned char  type;        /* VDISK_xxx */
54         bool           flush_support;
55         bool           discard_secure;
56         u32            pdevice;     /* phys device that this vbd maps to */
57         struct block_device *bdev;
58         sector_t       size;        /* Cached size parameter */
59 };
60
61 struct backend_info;
62
63 typedef struct blkif_st {
64         /* Unique identifier for this interface. */
65         domid_t           domid;
66         unsigned int      handle;
67         /* Physical parameters of the comms window. */
68         unsigned int      irq;
69         /* Comms information. */
70         enum blkif_protocol blk_protocol;
71         enum blkif_backend_type blk_backend_type;
72         blkif_back_rings_t blk_rings;
73         struct vm_struct *blk_ring_area;
74         /* The VBD attached to this interface. */
75         struct vbd        vbd;
76         /* Back pointer to the backend_info. */
77         struct backend_info *be;
78         /* Private fields. */
79         spinlock_t       blk_ring_lock;
80         atomic_t         refcnt;
81
82         wait_queue_head_t   wq;
83         /* for barrier (drain) requests */
84         struct completion   drain_complete;
85         atomic_t            drain;
86         struct task_struct  *xenblkd;
87         unsigned int        waiting_reqs;
88         struct request_queue *plug;
89
90         /* statistics */
91         unsigned long       st_print;
92         int                 st_rd_req;
93         int                 st_wr_req;
94         int                 st_oo_req;
95         int                 st_br_req;
96         int                 st_fl_req;
97         int                 st_ds_req;
98         int                 st_pk_req;
99         int                 st_rd_sect;
100         int                 st_wr_sect;
101
102         wait_queue_head_t waiting_to_free;
103 } blkif_t;
104
105 struct backend_info
106 {
107         struct xenbus_device *dev;
108         blkif_t *blkif;
109         struct xenbus_watch backend_watch;
110         struct xenbus_watch cdrom_watch;
111         unsigned major;
112         unsigned minor;
113         char *mode;
114 };
115
116 blkif_t *blkif_alloc(domid_t domid);
117 void blkif_disconnect(blkif_t *blkif);
118 void blkif_free(blkif_t *blkif);
119 int blkif_map(blkif_t *blkif, grant_ref_t, evtchn_port_t);
120 void vbd_resize(blkif_t *blkif);
121
122 #define blkif_get(_b) (atomic_inc(&(_b)->refcnt))
123 #define blkif_put(_b)                                   \
124         do {                                            \
125                 if (atomic_dec_and_test(&(_b)->refcnt)) \
126                         wake_up(&(_b)->waiting_to_free);\
127         } while (0)
128
129 /* Create a vbd. */
130 int vbd_create(blkif_t *blkif, blkif_vdev_t vdevice, unsigned major,
131                unsigned minor, fmode_t mode, bool cdrom);
132 void vbd_free(struct vbd *vbd);
133
134 unsigned long long vbd_size(struct vbd *vbd);
135 unsigned long vbd_secsize(struct vbd *vbd);
136
137 struct phys_req {
138         unsigned short       dev;
139         blkif_sector_t       nr_sects;
140         struct block_device *bdev;
141         blkif_sector_t       sector_number;
142 };
143
144 int vbd_translate(struct phys_req *req, blkif_t *blkif, int operation);
145
146 void blkif_interface_init(void);
147
148 void blkif_xenbus_init(void);
149
150 irqreturn_t blkif_be_int(int irq, void *dev_id);
151 int blkif_schedule(void *arg);
152
153 int blkback_barrier(struct xenbus_transaction xbt,
154                     struct backend_info *be, int state);
155 int blkback_flush_diskcache(struct xenbus_transaction,
156                             struct backend_info *, int state);
157
158 /* cdrom media change */
159 void cdrom_add_media_watch(struct backend_info *be);
160
161 #endif /* __BLKIF__BACKEND__COMMON_H__ */