Blame view

drivers/block/xen-blkback/common.h 9.47 KB
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /*
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License version 2
   * as published by the Free Software Foundation; or, when distributed
   * separately from the Linux kernel or incorporated into other
   * software packages, subject to the following license:
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this source file (the "Software"), to deal in the Software without
   * restriction, including without limitation the rights to use, copy, modify,
   * merge, publish, distribute, sublicense, and/or sell copies of the Software,
   * and to permit persons to whom the Software is furnished to do so, subject to
   * the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in
   * all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
   * IN THE SOFTWARE.
   */
5a577e387   Konrad Rzeszutek Wilk   xen/blkback: Add ...
26
27
  #ifndef __XEN_BLKIF__BACKEND__COMMON_H__
  #define __XEN_BLKIF__BACKEND__COMMON_H__
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
28

4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
29
30
31
32
33
34
  #include <linux/module.h>
  #include <linux/interrupt.h>
  #include <linux/slab.h>
  #include <linux/blkdev.h>
  #include <linux/vmalloc.h>
  #include <linux/wait.h>
5489377ce   Konrad Rzeszutek Wilk   xen/blkback: blki...
35
  #include <linux/io.h>
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
36
37
  #include <asm/setup.h>
  #include <asm/pgalloc.h>
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
38
  #include <asm/hypervisor.h>
881229332   Jeremy Fitzhardinge   xen-blkback-porting
39
  #include <xen/grant_table.h>
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
40
  #include <xen/xenbus.h>
452a6b2bb   Konrad Rzeszutek Wilk   xen/blkback: Move...
41
42
43
  #include <xen/interface/io/ring.h>
  #include <xen/interface/io/blkif.h>
  #include <xen/interface/io/protocols.h>
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
44

22b20f2df   Konrad Rzeszutek Wilk   xen/blkback: Use ...
45
  #define DRV_PFX "xen-blkback:"
1afbd730a   Konrad Rzeszutek Wilk   xen/blkback: Make...
46
  #define DPRINTK(fmt, args...)				\
1bc05b0ae   Joe Jin   xen-blkback: fixe...
47
48
  	pr_debug(DRV_PFX "(%s:%d) " fmt ".
  ",		\
1afbd730a   Konrad Rzeszutek Wilk   xen/blkback: Make...
49
  		 __func__, __LINE__, ##args)
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
50

452a6b2bb   Konrad Rzeszutek Wilk   xen/blkback: Move...
51
52
53
54
55
56
57
58
59
60
61
  
  /* Not a real protocol.  Used to generate ring structs which contain
   * the elements common to all protocols only.  This way we get a
   * compiler-checkable way to use common struct elements, so we can
   * avoid using switch(protocol) in a number of places.  */
  struct blkif_common_request {
  	char dummy;
  };
  struct blkif_common_response {
  	char dummy;
  };
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
62
  struct blkif_x86_32_request_rw {
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
63
64
65
  	uint8_t        nr_segments;  /* number of segments                   */
  	blkif_vdev_t   handle;       /* only for read/write requests         */
  	uint64_t       id;           /* private guest value, echoed in resp  */
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
66
67
  	blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
  	struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
68
  } __attribute__((__packed__));
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
69
70
  
  struct blkif_x86_32_request_discard {
5ea429866   Konrad Rzeszutek Wilk   xen/blk[front|bac...
71
  	uint8_t        flag;         /* BLKIF_DISCARD_SECURE or zero         */
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
72
73
  	blkif_vdev_t   _pad1;        /* was "handle" for read/write requests */
  	uint64_t       id;           /* private guest value, echoed in resp  */
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
74
  	blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
75
76
  	uint64_t       nr_sectors;
  } __attribute__((__packed__));
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
77

452a6b2bb   Konrad Rzeszutek Wilk   xen/blkback: Move...
78
79
  struct blkif_x86_32_request {
  	uint8_t        operation;    /* BLKIF_OP_???                         */
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
80
81
82
83
  	union {
  		struct blkif_x86_32_request_rw rw;
  		struct blkif_x86_32_request_discard discard;
  	} u;
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
84
85
86
87
  } __attribute__((__packed__));
  
  /* i386 protocol version */
  #pragma pack(push, 4)
452a6b2bb   Konrad Rzeszutek Wilk   xen/blkback: Move...
88
89
90
91
92
  struct blkif_x86_32_response {
  	uint64_t        id;              /* copied from request */
  	uint8_t         operation;       /* copied from request */
  	int16_t         status;          /* BLKIF_RSP_???       */
  };
452a6b2bb   Konrad Rzeszutek Wilk   xen/blkback: Move...
93
  #pragma pack(pop)
452a6b2bb   Konrad Rzeszutek Wilk   xen/blkback: Move...
94
  /* x86_64 protocol version */
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
95
96
  
  struct blkif_x86_64_request_rw {
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
97
98
99
100
  	uint8_t        nr_segments;  /* number of segments                   */
  	blkif_vdev_t   handle;       /* only for read/write requests         */
  	uint32_t       _pad1;        /* offsetof(blkif_reqest..,u.rw.id)==8  */
  	uint64_t       id;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
101
102
  	blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
  	struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
103
  } __attribute__((__packed__));
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
104
105
  
  struct blkif_x86_64_request_discard {
5ea429866   Konrad Rzeszutek Wilk   xen/blk[front|bac...
106
  	uint8_t        flag;         /* BLKIF_DISCARD_SECURE or zero         */
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
107
108
109
  	blkif_vdev_t   _pad1;        /* was "handle" for read/write requests */
          uint32_t       _pad2;        /* offsetof(blkif_..,u.discard.id)==8   */
  	uint64_t       id;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
110
  	blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
111
112
  	uint64_t       nr_sectors;
  } __attribute__((__packed__));
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
113

452a6b2bb   Konrad Rzeszutek Wilk   xen/blkback: Move...
114
115
  struct blkif_x86_64_request {
  	uint8_t        operation;    /* BLKIF_OP_???                         */
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
116
117
118
119
  	union {
  		struct blkif_x86_64_request_rw rw;
  		struct blkif_x86_64_request_discard discard;
  	} u;
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
120
  } __attribute__((__packed__));
452a6b2bb   Konrad Rzeszutek Wilk   xen/blkback: Move...
121
122
123
124
125
  struct blkif_x86_64_response {
  	uint64_t       __attribute__((__aligned__(8))) id;
  	uint8_t         operation;       /* copied from request */
  	int16_t         status;          /* BLKIF_RSP_???       */
  };
452a6b2bb   Konrad Rzeszutek Wilk   xen/blkback: Move...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  
  DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
  		  struct blkif_common_response);
  DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
  		  struct blkif_x86_32_response);
  DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
  		  struct blkif_x86_64_response);
  
  union blkif_back_rings {
  	struct blkif_back_ring        native;
  	struct blkif_common_back_ring common;
  	struct blkif_x86_32_back_ring x86_32;
  	struct blkif_x86_64_back_ring x86_64;
  };
  
  enum blkif_protocol {
  	BLKIF_PROTOCOL_NATIVE = 1,
  	BLKIF_PROTOCOL_X86_32 = 2,
  	BLKIF_PROTOCOL_X86_64 = 3,
  };
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
146
147
148
149
  enum blkif_backend_type {
  	BLKIF_BACKEND_PHY  = 1,
  	BLKIF_BACKEND_FILE = 2,
  };
3d814731b   Konrad Rzeszutek Wilk   xen/blkback: Pref...
150
  struct xen_vbd {
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
151
152
153
154
155
156
157
158
159
160
161
162
  	/* What the domain refers to this vbd as. */
  	blkif_vdev_t		handle;
  	/* Non-zero -> read-only */
  	unsigned char		readonly;
  	/* VDISK_xxx */
  	unsigned char		type;
  	/* phys device that this vbd maps to. */
  	u32			pdevice;
  	struct block_device	*bdev;
  	/* Cached size parameter. */
  	sector_t		size;
  	bool			flush_support;
5ea429866   Konrad Rzeszutek Wilk   xen/blk[front|bac...
163
  	bool			discard_secure;
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
164
165
166
  };
  
  struct backend_info;
30fd15020   Konrad Rzeszutek Wilk   xen/blkback: Chan...
167
  struct xen_blkif {
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
168
  	/* Unique identifier for this interface. */
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
169
170
  	domid_t			domid;
  	unsigned int		handle;
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
171
  	/* Physical parameters of the comms window. */
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
172
  	unsigned int		irq;
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
173
  	/* Comms information. */
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
174
  	enum blkif_protocol	blk_protocol;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
175
  	enum blkif_backend_type blk_backend_type;
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
176
  	union blkif_back_rings	blk_rings;
2d073846b   David Vrabel   block: xen-blkbac...
177
  	void			*blk_ring;
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
178
  	/* The VBD attached to this interface. */
3d814731b   Konrad Rzeszutek Wilk   xen/blkback: Pref...
179
  	struct xen_vbd		vbd;
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
180
  	/* Back pointer to the backend_info. */
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
181
  	struct backend_info	*be;
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
182
  	/* Private fields. */
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
183
184
  	spinlock_t		blk_ring_lock;
  	atomic_t		refcnt;
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
185

01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
186
  	wait_queue_head_t	wq;
29bde0937   Konrad Rzeszutek Wilk   xen/blkback: Supp...
187
188
189
  	/* for barrier (drain) requests */
  	struct completion	drain_complete;
  	atomic_t		drain;
a1397fa30   Konrad Rzeszutek Wilk   xen/blkback: Add ...
190
  	/* One thread per one blkif. */
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
191
192
  	struct task_struct	*xenblkd;
  	unsigned int		waiting_reqs;
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
193
194
  
  	/* statistics */
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
195
196
197
198
199
  	unsigned long		st_print;
  	int			st_rd_req;
  	int			st_wr_req;
  	int			st_oo_req;
  	int			st_f_req;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
200
  	int			st_ds_req;
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
201
202
203
204
  	int			st_rd_sect;
  	int			st_wr_sect;
  
  	wait_queue_head_t	waiting_to_free;
5489377ce   Konrad Rzeszutek Wilk   xen/blkback: blki...
205
  };
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
206

42c7841d1   Konrad Rzeszutek Wilk   xen-blkback: Inli...
207
208
209
210
  
  #define vbd_sz(_v)	((_v)->bdev->bd_part ? \
  			 (_v)->bdev->bd_part->nr_sects : \
  			  get_capacity((_v)->bdev->bd_disk))
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
211

8b6bf747d   Konrad Rzeszutek Wilk   xen/blkback: Pref...
212
213
  #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  #define xen_blkif_put(_b)				\
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
214
215
216
217
  	do {						\
  		if (atomic_dec_and_test(&(_b)->refcnt))	\
  			wake_up(&(_b)->waiting_to_free);\
  	} while (0)
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
218
  struct phys_req {
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
219
  	unsigned short		dev;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
220
  	blkif_sector_t		nr_sects;
01f37f2d5   Konrad Rzeszutek Wilk   xen/blkback: Fixe...
221
222
  	struct block_device	*bdev;
  	blkif_sector_t		sector_number;
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
223
  };
8b6bf747d   Konrad Rzeszutek Wilk   xen/blkback: Pref...
224
  int xen_blkif_interface_init(void);
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
225

8b6bf747d   Konrad Rzeszutek Wilk   xen/blkback: Pref...
226
  int xen_blkif_xenbus_init(void);
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
227

8b6bf747d   Konrad Rzeszutek Wilk   xen/blkback: Pref...
228
229
  irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  int xen_blkif_schedule(void *arg);
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
230

24f567f95   Konrad Rzeszutek Wilk   xen/blkback: Add ...
231
232
  int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  			      struct backend_info *be, int state);
4d05a28db   Konrad Rzeszutek Wilk   xen: add blkback ...
233

29bde0937   Konrad Rzeszutek Wilk   xen/blkback: Supp...
234
235
  int xen_blkbk_barrier(struct xenbus_transaction xbt,
  		      struct backend_info *be, int state);
8b6bf747d   Konrad Rzeszutek Wilk   xen/blkback: Pref...
236
  struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
98e036a35   Jeremy Fitzhardinge   xen/blkback: add ...
237

b0f801273   Konrad Rzeszutek Wilk   xen/blkback: Fixi...
238
  static inline void blkif_get_x86_32_req(struct blkif_request *dst,
68c88dd7d   Konrad Rzeszutek Wilk   xen/blkback: Move...
239
240
241
242
  					struct blkif_x86_32_request *src)
  {
  	int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  	dst->operation = src->operation;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
243
244
245
246
247
  	switch (src->operation) {
  	case BLKIF_OP_READ:
  	case BLKIF_OP_WRITE:
  	case BLKIF_OP_WRITE_BARRIER:
  	case BLKIF_OP_FLUSH_DISKCACHE:
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
248
249
250
  		dst->u.rw.nr_segments = src->u.rw.nr_segments;
  		dst->u.rw.handle = src->u.rw.handle;
  		dst->u.rw.id = src->u.rw.id;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
251
252
  		dst->u.rw.sector_number = src->u.rw.sector_number;
  		barrier();
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
253
254
  		if (n > dst->u.rw.nr_segments)
  			n = dst->u.rw.nr_segments;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
255
256
257
258
  		for (i = 0; i < n; i++)
  			dst->u.rw.seg[i] = src->u.rw.seg[i];
  		break;
  	case BLKIF_OP_DISCARD:
5ea429866   Konrad Rzeszutek Wilk   xen/blk[front|bac...
259
  		dst->u.discard.flag = src->u.discard.flag;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
260
261
262
263
264
265
  		dst->u.discard.sector_number = src->u.discard.sector_number;
  		dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  		break;
  	default:
  		break;
  	}
68c88dd7d   Konrad Rzeszutek Wilk   xen/blkback: Move...
266
  }
b0f801273   Konrad Rzeszutek Wilk   xen/blkback: Fixi...
267
  static inline void blkif_get_x86_64_req(struct blkif_request *dst,
68c88dd7d   Konrad Rzeszutek Wilk   xen/blkback: Move...
268
269
270
271
  					struct blkif_x86_64_request *src)
  {
  	int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  	dst->operation = src->operation;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
272
273
274
275
276
  	switch (src->operation) {
  	case BLKIF_OP_READ:
  	case BLKIF_OP_WRITE:
  	case BLKIF_OP_WRITE_BARRIER:
  	case BLKIF_OP_FLUSH_DISKCACHE:
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
277
278
279
  		dst->u.rw.nr_segments = src->u.rw.nr_segments;
  		dst->u.rw.handle = src->u.rw.handle;
  		dst->u.rw.id = src->u.rw.id;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
280
281
  		dst->u.rw.sector_number = src->u.rw.sector_number;
  		barrier();
97e36834f   Konrad Rzeszutek Wilk   xen/blk[front|bac...
282
283
  		if (n > dst->u.rw.nr_segments)
  			n = dst->u.rw.nr_segments;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
284
285
286
287
  		for (i = 0; i < n; i++)
  			dst->u.rw.seg[i] = src->u.rw.seg[i];
  		break;
  	case BLKIF_OP_DISCARD:
5ea429866   Konrad Rzeszutek Wilk   xen/blk[front|bac...
288
  		dst->u.discard.flag = src->u.discard.flag;
b3cb0d6ad   Li Dongyang   xen-blkback: Impl...
289
290
291
292
293
294
  		dst->u.discard.sector_number = src->u.discard.sector_number;
  		dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  		break;
  	default:
  		break;
  	}
68c88dd7d   Konrad Rzeszutek Wilk   xen/blkback: Move...
295
  }
5a577e387   Konrad Rzeszutek Wilk   xen/blkback: Add ...
296
  #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */