Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 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/version.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/interrupt.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/blkdev.h> |
| 35 | #include <linux/vmalloc.h> |
| 36 | #include <linux/wait.h> |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame] | 37 | #include <linux/io.h> |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 38 | #include <asm/setup.h> |
| 39 | #include <asm/pgalloc.h> |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 40 | #include <asm/hypervisor.h> |
| 41 | #include <xen/blkif.h> |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 42 | #include <xen/grant_table.h> |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 43 | #include <xen/xenbus.h> |
| 44 | |
Konrad Rzeszutek Wilk | 22b20f2 | 2011-05-12 16:43:12 -0400 | [diff] [blame] | 45 | #define DRV_PFX "xen-blkback:" |
Konrad Rzeszutek Wilk | 1afbd73 | 2011-05-11 16:15:24 -0400 | [diff] [blame] | 46 | #define DPRINTK(fmt, args...) \ |
Konrad Rzeszutek Wilk | 22b20f2 | 2011-05-12 16:43:12 -0400 | [diff] [blame] | 47 | pr_debug(DRV_PFX "(%s:%d) " fmt ".\n", \ |
Konrad Rzeszutek Wilk | 1afbd73 | 2011-05-11 16:15:24 -0400 | [diff] [blame] | 48 | __func__, __LINE__, ##args) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 49 | |
| 50 | struct vbd { |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 51 | /* What the domain refers to this vbd as. */ |
| 52 | blkif_vdev_t handle; |
| 53 | /* Non-zero -> read-only */ |
| 54 | unsigned char readonly; |
| 55 | /* VDISK_xxx */ |
| 56 | unsigned char type; |
| 57 | /* phys device that this vbd maps to. */ |
| 58 | u32 pdevice; |
| 59 | struct block_device *bdev; |
| 60 | /* Cached size parameter. */ |
| 61 | sector_t size; |
| 62 | bool flush_support; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | struct backend_info; |
| 66 | |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame] | 67 | struct blkif_st { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 68 | /* Unique identifier for this interface. */ |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 69 | domid_t domid; |
| 70 | unsigned int handle; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 71 | /* Physical parameters of the comms window. */ |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 72 | unsigned int irq; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 73 | /* Comms information. */ |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 74 | enum blkif_protocol blk_protocol; |
| 75 | union blkif_back_rings blk_rings; |
| 76 | struct vm_struct *blk_ring_area; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 77 | /* The VBD attached to this interface. */ |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 78 | struct vbd vbd; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 79 | /* Back pointer to the backend_info. */ |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 80 | struct backend_info *be; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 81 | /* Private fields. */ |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 82 | spinlock_t blk_ring_lock; |
| 83 | atomic_t refcnt; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 84 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 85 | wait_queue_head_t wq; |
Konrad Rzeszutek Wilk | a1397fa | 2011-04-14 17:05:23 -0400 | [diff] [blame] | 86 | /* One thread per one blkif. */ |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 87 | struct task_struct *xenblkd; |
| 88 | unsigned int waiting_reqs; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 89 | |
| 90 | /* statistics */ |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 91 | unsigned long st_print; |
| 92 | int st_rd_req; |
| 93 | int st_wr_req; |
| 94 | int st_oo_req; |
| 95 | int st_f_req; |
| 96 | int st_rd_sect; |
| 97 | int st_wr_sect; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 98 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 99 | wait_queue_head_t waiting_to_free; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 100 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 101 | grant_handle_t shmem_handle; |
| 102 | grant_ref_t shmem_ref; |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame] | 103 | }; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 104 | |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 105 | |
| 106 | #define vbd_sz(_v) ((_v)->bdev->bd_part ? \ |
| 107 | (_v)->bdev->bd_part->nr_sects : \ |
| 108 | get_capacity((_v)->bdev->bd_disk)) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 109 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 110 | #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt)) |
| 111 | #define xen_blkif_put(_b) \ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 112 | do { \ |
| 113 | if (atomic_dec_and_test(&(_b)->refcnt)) \ |
| 114 | wake_up(&(_b)->waiting_to_free);\ |
| 115 | } while (0) |
| 116 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 117 | struct phys_req { |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 118 | unsigned short dev; |
| 119 | unsigned short nr_sects; |
| 120 | struct block_device *bdev; |
| 121 | blkif_sector_t sector_number; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 122 | }; |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 123 | int xen_blkif_interface_init(void); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 124 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 125 | int xen_blkif_xenbus_init(void); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 126 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 127 | irqreturn_t xen_blkif_be_int(int irq, void *dev_id); |
| 128 | int xen_blkif_schedule(void *arg); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 129 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 130 | int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt, |
| 131 | struct backend_info *be, int state); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 132 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 133 | struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be); |
Jeremy Fitzhardinge | 98e036a | 2010-03-18 15:35:05 -0700 | [diff] [blame] | 134 | |
Konrad Rzeszutek Wilk | 68c88dd | 2011-05-11 16:23:39 -0400 | [diff] [blame^] | 135 | static void inline blkif_get_x86_32_req(struct blkif_request *dst, |
| 136 | struct blkif_x86_32_request *src) |
| 137 | { |
| 138 | int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST; |
| 139 | dst->operation = src->operation; |
| 140 | dst->nr_segments = src->nr_segments; |
| 141 | dst->handle = src->handle; |
| 142 | dst->id = src->id; |
| 143 | dst->u.rw.sector_number = src->sector_number; |
| 144 | barrier(); |
| 145 | if (n > dst->nr_segments) |
| 146 | n = dst->nr_segments; |
| 147 | for (i = 0; i < n; i++) |
| 148 | dst->u.rw.seg[i] = src->seg[i]; |
| 149 | } |
| 150 | |
| 151 | static void inline blkif_get_x86_64_req(struct blkif_request *dst, |
| 152 | struct blkif_x86_64_request *src) |
| 153 | { |
| 154 | int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST; |
| 155 | dst->operation = src->operation; |
| 156 | dst->nr_segments = src->nr_segments; |
| 157 | dst->handle = src->handle; |
| 158 | dst->id = src->id; |
| 159 | dst->u.rw.sector_number = src->sector_number; |
| 160 | barrier(); |
| 161 | if (n > dst->nr_segments) |
| 162 | n = dst->nr_segments; |
| 163 | for (i = 0; i < n; i++) |
| 164 | dst->u.rw.seg[i] = src->seg[i]; |
| 165 | } |
| 166 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 167 | #endif /* __BLKIF__BACKEND__COMMON_H__ */ |