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