Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 1 | /****************************************************************************** |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 2 | * Block-device interface management. |
| 3 | * |
| 4 | * Copyright (c) 2004, Keir Fraser |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License version 2 |
| 8 | * as published by the Free Software Foundation; or, when distributed |
| 9 | * separately from the Linux kernel or incorporated into other |
| 10 | * software packages, subject to the following license: |
| 11 | * |
| 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | * of this source file (the "Software"), to deal in the Software without |
| 14 | * restriction, including without limitation the rights to use, copy, modify, |
| 15 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 16 | * and to permit persons to whom the Software is furnished to do so, subject to |
| 17 | * the following conditions: |
| 18 | * |
| 19 | * The above copyright notice and this permission notice shall be included in |
| 20 | * all copies or substantial portions of the Software. |
| 21 | * |
| 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 28 | * IN THE SOFTWARE. |
| 29 | */ |
| 30 | |
| 31 | #include "common.h" |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 32 | #include <xen/events.h> |
| 33 | #include <xen/grant_table.h> |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 34 | #include <linux/kthread.h> |
| 35 | |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 36 | static struct kmem_cache *blkif_cachep; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 37 | |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame^] | 38 | struct blkif_st *blkif_alloc(domid_t domid) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 39 | { |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame^] | 40 | struct blkif_st *blkif; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 41 | |
| 42 | blkif = kmem_cache_alloc(blkif_cachep, GFP_KERNEL); |
| 43 | if (!blkif) |
| 44 | return ERR_PTR(-ENOMEM); |
| 45 | |
| 46 | memset(blkif, 0, sizeof(*blkif)); |
| 47 | blkif->domid = domid; |
| 48 | spin_lock_init(&blkif->blk_ring_lock); |
| 49 | atomic_set(&blkif->refcnt, 1); |
| 50 | init_waitqueue_head(&blkif->wq); |
| 51 | blkif->st_print = jiffies; |
| 52 | init_waitqueue_head(&blkif->waiting_to_free); |
| 53 | |
| 54 | return blkif; |
| 55 | } |
| 56 | |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame^] | 57 | static int map_frontend_page(struct blkif_st *blkif, unsigned long shared_page) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 58 | { |
| 59 | struct gnttab_map_grant_ref op; |
| 60 | |
| 61 | gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr, |
| 62 | GNTMAP_host_map, shared_page, blkif->domid); |
| 63 | |
| 64 | if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1)) |
| 65 | BUG(); |
| 66 | |
| 67 | if (op.status) { |
| 68 | DPRINTK(" Grant table operation failure !\n"); |
| 69 | return op.status; |
| 70 | } |
| 71 | |
| 72 | blkif->shmem_ref = shared_page; |
| 73 | blkif->shmem_handle = op.handle; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame^] | 78 | static void unmap_frontend_page(struct blkif_st *blkif) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 79 | { |
| 80 | struct gnttab_unmap_grant_ref op; |
| 81 | |
| 82 | gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr, |
| 83 | GNTMAP_host_map, blkif->shmem_handle); |
| 84 | |
| 85 | if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1)) |
| 86 | BUG(); |
| 87 | } |
| 88 | |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame^] | 89 | int blkif_map(struct blkif_st *blkif, unsigned long shared_page, unsigned int evtchn) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 90 | { |
| 91 | int err; |
| 92 | |
| 93 | /* Already connected through? */ |
| 94 | if (blkif->irq) |
| 95 | return 0; |
| 96 | |
| 97 | if ( (blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE)) == NULL ) |
| 98 | return -ENOMEM; |
| 99 | |
| 100 | err = map_frontend_page(blkif, shared_page); |
| 101 | if (err) { |
| 102 | free_vm_area(blkif->blk_ring_area); |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | switch (blkif->blk_protocol) { |
| 107 | case BLKIF_PROTOCOL_NATIVE: |
| 108 | { |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 109 | struct blkif_sring *sring; |
| 110 | sring = (struct blkif_sring *)blkif->blk_ring_area->addr; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 111 | BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE); |
| 112 | break; |
| 113 | } |
| 114 | case BLKIF_PROTOCOL_X86_32: |
| 115 | { |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 116 | struct blkif_x86_32_sring *sring_x86_32; |
| 117 | sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring_area->addr; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 118 | BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE); |
| 119 | break; |
| 120 | } |
| 121 | case BLKIF_PROTOCOL_X86_64: |
| 122 | { |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 123 | struct blkif_x86_64_sring *sring_x86_64; |
| 124 | sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring_area->addr; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 125 | BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE); |
| 126 | break; |
| 127 | } |
| 128 | default: |
| 129 | BUG(); |
| 130 | } |
| 131 | |
| 132 | err = bind_interdomain_evtchn_to_irqhandler( |
| 133 | blkif->domid, evtchn, blkif_be_int, 0, "blkif-backend", blkif); |
| 134 | if (err < 0) |
| 135 | { |
| 136 | unmap_frontend_page(blkif); |
| 137 | free_vm_area(blkif->blk_ring_area); |
| 138 | blkif->blk_rings.common.sring = NULL; |
| 139 | return err; |
| 140 | } |
| 141 | blkif->irq = err; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame^] | 146 | void blkif_disconnect(struct blkif_st *blkif) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 147 | { |
| 148 | if (blkif->xenblkd) { |
| 149 | kthread_stop(blkif->xenblkd); |
| 150 | blkif->xenblkd = NULL; |
| 151 | } |
| 152 | |
| 153 | atomic_dec(&blkif->refcnt); |
| 154 | wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0); |
| 155 | atomic_inc(&blkif->refcnt); |
| 156 | |
| 157 | if (blkif->irq) { |
| 158 | unbind_from_irqhandler(blkif->irq, blkif); |
| 159 | blkif->irq = 0; |
| 160 | } |
| 161 | |
| 162 | if (blkif->blk_rings.common.sring) { |
| 163 | unmap_frontend_page(blkif); |
| 164 | free_vm_area(blkif->blk_ring_area); |
| 165 | blkif->blk_rings.common.sring = NULL; |
| 166 | } |
| 167 | } |
| 168 | |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame^] | 169 | void blkif_free(struct blkif_st *blkif) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 170 | { |
| 171 | if (!atomic_dec_and_test(&blkif->refcnt)) |
| 172 | BUG(); |
| 173 | kmem_cache_free(blkif_cachep, blkif); |
| 174 | } |
| 175 | |
Konrad Rzeszutek Wilk | 8770b26 | 2009-10-08 13:23:09 -0400 | [diff] [blame] | 176 | int __init blkif_interface_init(void) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 177 | { |
Konrad Rzeszutek Wilk | 5489377 | 2011-04-14 17:21:50 -0400 | [diff] [blame^] | 178 | blkif_cachep = kmem_cache_create("blkif_cache", sizeof(struct blkif_st), |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 179 | 0, 0, NULL); |
Konrad Rzeszutek Wilk | 8770b26 | 2009-10-08 13:23:09 -0400 | [diff] [blame] | 180 | if (!blkif_cachep) |
| 181 | return -ENOMEM; |
| 182 | |
| 183 | return 0; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 184 | } |