Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * VME Bridge Framework |
| 3 | * |
Martyn Welch | 66bd8db | 2010-02-18 15:12:52 +0000 | [diff] [blame] | 4 | * Author: Martyn Welch <martyn.welch@ge.com> |
| 5 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 6 | * |
| 7 | * Based on work by Tom Armistead and Ajit Prem |
| 8 | * Copyright 2004 Motorola Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | */ |
| 15 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/pci.h> |
| 23 | #include <linux/poll.h> |
| 24 | #include <linux/highmem.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/pagemap.h> |
| 27 | #include <linux/device.h> |
| 28 | #include <linux/dma-mapping.h> |
| 29 | #include <linux/syscalls.h> |
Martyn Welch | 400822f | 2009-08-11 16:20:22 +0100 | [diff] [blame] | 30 | #include <linux/mutex.h> |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 31 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Greg Kroah-Hartman | db3b9e9 | 2012-04-26 12:34:58 -0700 | [diff] [blame] | 33 | #include <linux/vme.h> |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 34 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 35 | #include "vme_bridge.h" |
| 36 | |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 37 | /* Bitmask and list of registered buses both protected by common mutex */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 38 | static unsigned int vme_bus_numbers; |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 39 | static LIST_HEAD(vme_bus_list); |
| 40 | static DEFINE_MUTEX(vme_buses_lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 41 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 42 | static void __exit vme_exit(void); |
| 43 | static int __init vme_init(void); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 44 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 45 | static struct vme_dev *dev_to_vme_dev(struct device *dev) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 46 | { |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 47 | return container_of(dev, struct vme_dev, dev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Find the bridge that the resource is associated with. |
| 52 | */ |
| 53 | static struct vme_bridge *find_bridge(struct vme_resource *resource) |
| 54 | { |
| 55 | /* Get list to search */ |
| 56 | switch (resource->type) { |
| 57 | case VME_MASTER: |
| 58 | return list_entry(resource->entry, struct vme_master_resource, |
| 59 | list)->parent; |
| 60 | break; |
| 61 | case VME_SLAVE: |
| 62 | return list_entry(resource->entry, struct vme_slave_resource, |
| 63 | list)->parent; |
| 64 | break; |
| 65 | case VME_DMA: |
| 66 | return list_entry(resource->entry, struct vme_dma_resource, |
| 67 | list)->parent; |
| 68 | break; |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 69 | case VME_LM: |
| 70 | return list_entry(resource->entry, struct vme_lm_resource, |
| 71 | list)->parent; |
| 72 | break; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 73 | default: |
| 74 | printk(KERN_ERR "Unknown resource type\n"); |
| 75 | return NULL; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Allocate a contiguous block of memory for use by the driver. This is used to |
| 82 | * create the buffers for the slave windows. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 83 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 84 | void *vme_alloc_consistent(struct vme_resource *resource, size_t size, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 85 | dma_addr_t *dma) |
| 86 | { |
| 87 | struct vme_bridge *bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 88 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 89 | if (resource == NULL) { |
| 90 | printk(KERN_ERR "No resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | bridge = find_bridge(resource); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 95 | if (bridge == NULL) { |
| 96 | printk(KERN_ERR "Can't find bridge\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
| 99 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 100 | if (bridge->parent == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 101 | printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 102 | return NULL; |
| 103 | } |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 104 | |
Manohar Vanga | 7f58f02 | 2011-08-10 11:33:46 +0200 | [diff] [blame] | 105 | if (bridge->alloc_consistent == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 106 | printk(KERN_ERR "alloc_consistent not supported by bridge %s\n", |
| 107 | bridge->name); |
Manohar Vanga | 7f58f02 | 2011-08-10 11:33:46 +0200 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | return bridge->alloc_consistent(bridge->parent, size, dma); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 112 | } |
| 113 | EXPORT_SYMBOL(vme_alloc_consistent); |
| 114 | |
| 115 | /* |
| 116 | * Free previously allocated contiguous block of memory. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 117 | */ |
| 118 | void vme_free_consistent(struct vme_resource *resource, size_t size, |
| 119 | void *vaddr, dma_addr_t dma) |
| 120 | { |
| 121 | struct vme_bridge *bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 122 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 123 | if (resource == NULL) { |
| 124 | printk(KERN_ERR "No resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | |
| 128 | bridge = find_bridge(resource); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 129 | if (bridge == NULL) { |
| 130 | printk(KERN_ERR "Can't find bridge\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
Manohar Vanga | 7f58f02 | 2011-08-10 11:33:46 +0200 | [diff] [blame] | 134 | if (bridge->parent == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 135 | printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name); |
Manohar Vanga | 7f58f02 | 2011-08-10 11:33:46 +0200 | [diff] [blame] | 136 | return; |
| 137 | } |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 138 | |
Manohar Vanga | 7f58f02 | 2011-08-10 11:33:46 +0200 | [diff] [blame] | 139 | if (bridge->free_consistent == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 140 | printk(KERN_ERR "free_consistent not supported by bridge %s\n", |
| 141 | bridge->name); |
Manohar Vanga | 7f58f02 | 2011-08-10 11:33:46 +0200 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | |
| 145 | bridge->free_consistent(bridge->parent, size, vaddr, dma); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 146 | } |
| 147 | EXPORT_SYMBOL(vme_free_consistent); |
| 148 | |
| 149 | size_t vme_get_size(struct vme_resource *resource) |
| 150 | { |
| 151 | int enabled, retval; |
| 152 | unsigned long long base, size; |
| 153 | dma_addr_t buf_base; |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 154 | u32 aspace, cycle, dwidth; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 155 | |
| 156 | switch (resource->type) { |
| 157 | case VME_MASTER: |
| 158 | retval = vme_master_get(resource, &enabled, &base, &size, |
| 159 | &aspace, &cycle, &dwidth); |
| 160 | |
| 161 | return size; |
| 162 | break; |
| 163 | case VME_SLAVE: |
| 164 | retval = vme_slave_get(resource, &enabled, &base, &size, |
| 165 | &buf_base, &aspace, &cycle); |
| 166 | |
| 167 | return size; |
| 168 | break; |
| 169 | case VME_DMA: |
| 170 | return 0; |
| 171 | break; |
| 172 | default: |
| 173 | printk(KERN_ERR "Unknown resource type\n"); |
| 174 | return 0; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | EXPORT_SYMBOL(vme_get_size); |
| 179 | |
Dmitry Kalinkin | ef73f88 | 2015-05-28 15:07:04 +0300 | [diff] [blame] | 180 | int vme_check_window(u32 aspace, unsigned long long vme_base, |
| 181 | unsigned long long size) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 182 | { |
| 183 | int retval = 0; |
| 184 | |
| 185 | switch (aspace) { |
| 186 | case VME_A16: |
| 187 | if (((vme_base + size) > VME_A16_MAX) || |
| 188 | (vme_base > VME_A16_MAX)) |
| 189 | retval = -EFAULT; |
| 190 | break; |
| 191 | case VME_A24: |
| 192 | if (((vme_base + size) > VME_A24_MAX) || |
| 193 | (vme_base > VME_A24_MAX)) |
| 194 | retval = -EFAULT; |
| 195 | break; |
| 196 | case VME_A32: |
| 197 | if (((vme_base + size) > VME_A32_MAX) || |
| 198 | (vme_base > VME_A32_MAX)) |
| 199 | retval = -EFAULT; |
| 200 | break; |
| 201 | case VME_A64: |
Dmitry Kalinkin | e7fd80c | 2015-05-28 15:07:03 +0300 | [diff] [blame] | 202 | if ((size != 0) && (vme_base > U64_MAX + 1 - size)) |
| 203 | retval = -EFAULT; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 204 | break; |
| 205 | case VME_CRCSR: |
| 206 | if (((vme_base + size) > VME_CRCSR_MAX) || |
| 207 | (vme_base > VME_CRCSR_MAX)) |
| 208 | retval = -EFAULT; |
| 209 | break; |
| 210 | case VME_USER1: |
| 211 | case VME_USER2: |
| 212 | case VME_USER3: |
| 213 | case VME_USER4: |
| 214 | /* User Defined */ |
| 215 | break; |
| 216 | default: |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 217 | printk(KERN_ERR "Invalid address space\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 218 | retval = -EINVAL; |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | return retval; |
| 223 | } |
Dmitry Kalinkin | ef73f88 | 2015-05-28 15:07:04 +0300 | [diff] [blame] | 224 | EXPORT_SYMBOL(vme_check_window); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 225 | |
| 226 | /* |
| 227 | * Request a slave image with specific attributes, return some unique |
| 228 | * identifier. |
| 229 | */ |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 230 | struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address, |
| 231 | u32 cycle) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 232 | { |
| 233 | struct vme_bridge *bridge; |
| 234 | struct list_head *slave_pos = NULL; |
| 235 | struct vme_slave_resource *allocated_image = NULL; |
| 236 | struct vme_slave_resource *slave_image = NULL; |
| 237 | struct vme_resource *resource = NULL; |
| 238 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 239 | bridge = vdev->bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 240 | if (bridge == NULL) { |
| 241 | printk(KERN_ERR "Can't find VME bus\n"); |
| 242 | goto err_bus; |
| 243 | } |
| 244 | |
| 245 | /* Loop through slave resources */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 246 | list_for_each(slave_pos, &bridge->slave_resources) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 247 | slave_image = list_entry(slave_pos, |
| 248 | struct vme_slave_resource, list); |
| 249 | |
| 250 | if (slave_image == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 251 | printk(KERN_ERR "Registered NULL Slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 252 | continue; |
| 253 | } |
| 254 | |
| 255 | /* Find an unlocked and compatible image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 256 | mutex_lock(&slave_image->mtx); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 257 | if (((slave_image->address_attr & address) == address) && |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 258 | ((slave_image->cycle_attr & cycle) == cycle) && |
| 259 | (slave_image->locked == 0)) { |
| 260 | |
| 261 | slave_image->locked = 1; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 262 | mutex_unlock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 263 | allocated_image = slave_image; |
| 264 | break; |
| 265 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 266 | mutex_unlock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* No free image */ |
| 270 | if (allocated_image == NULL) |
| 271 | goto err_image; |
| 272 | |
| 273 | resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL); |
| 274 | if (resource == NULL) { |
| 275 | printk(KERN_WARNING "Unable to allocate resource structure\n"); |
| 276 | goto err_alloc; |
| 277 | } |
| 278 | resource->type = VME_SLAVE; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 279 | resource->entry = &allocated_image->list; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 280 | |
| 281 | return resource; |
| 282 | |
| 283 | err_alloc: |
| 284 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 285 | mutex_lock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 286 | slave_image->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 287 | mutex_unlock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 288 | err_image: |
| 289 | err_bus: |
| 290 | return NULL; |
| 291 | } |
| 292 | EXPORT_SYMBOL(vme_slave_request); |
| 293 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 294 | int vme_slave_set(struct vme_resource *resource, int enabled, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 295 | unsigned long long vme_base, unsigned long long size, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 296 | dma_addr_t buf_base, u32 aspace, u32 cycle) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 297 | { |
| 298 | struct vme_bridge *bridge = find_bridge(resource); |
| 299 | struct vme_slave_resource *image; |
| 300 | int retval; |
| 301 | |
| 302 | if (resource->type != VME_SLAVE) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 303 | printk(KERN_ERR "Not a slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 304 | return -EINVAL; |
| 305 | } |
| 306 | |
| 307 | image = list_entry(resource->entry, struct vme_slave_resource, list); |
| 308 | |
| 309 | if (bridge->slave_set == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 310 | printk(KERN_ERR "Function not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 311 | return -ENOSYS; |
| 312 | } |
| 313 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 314 | if (!(((image->address_attr & aspace) == aspace) && |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 315 | ((image->cycle_attr & cycle) == cycle))) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 316 | printk(KERN_ERR "Invalid attributes\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 317 | return -EINVAL; |
| 318 | } |
| 319 | |
| 320 | retval = vme_check_window(aspace, vme_base, size); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 321 | if (retval) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 322 | return retval; |
| 323 | |
| 324 | return bridge->slave_set(image, enabled, vme_base, size, buf_base, |
| 325 | aspace, cycle); |
| 326 | } |
| 327 | EXPORT_SYMBOL(vme_slave_set); |
| 328 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 329 | int vme_slave_get(struct vme_resource *resource, int *enabled, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 330 | unsigned long long *vme_base, unsigned long long *size, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 331 | dma_addr_t *buf_base, u32 *aspace, u32 *cycle) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 332 | { |
| 333 | struct vme_bridge *bridge = find_bridge(resource); |
| 334 | struct vme_slave_resource *image; |
| 335 | |
| 336 | if (resource->type != VME_SLAVE) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 337 | printk(KERN_ERR "Not a slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | image = list_entry(resource->entry, struct vme_slave_resource, list); |
| 342 | |
Emilio G. Cota | 51a569f | 2009-08-10 16:52:42 +0200 | [diff] [blame] | 343 | if (bridge->slave_get == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 344 | printk(KERN_ERR "vme_slave_get not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | return bridge->slave_get(image, enabled, vme_base, size, buf_base, |
| 349 | aspace, cycle); |
| 350 | } |
| 351 | EXPORT_SYMBOL(vme_slave_get); |
| 352 | |
| 353 | void vme_slave_free(struct vme_resource *resource) |
| 354 | { |
| 355 | struct vme_slave_resource *slave_image; |
| 356 | |
| 357 | if (resource->type != VME_SLAVE) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 358 | printk(KERN_ERR "Not a slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 359 | return; |
| 360 | } |
| 361 | |
| 362 | slave_image = list_entry(resource->entry, struct vme_slave_resource, |
| 363 | list); |
| 364 | if (slave_image == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 365 | printk(KERN_ERR "Can't find slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | |
| 369 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 370 | mutex_lock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 371 | if (slave_image->locked == 0) |
| 372 | printk(KERN_ERR "Image is already free\n"); |
| 373 | |
| 374 | slave_image->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 375 | mutex_unlock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 376 | |
| 377 | /* Free up resource memory */ |
| 378 | kfree(resource); |
| 379 | } |
| 380 | EXPORT_SYMBOL(vme_slave_free); |
| 381 | |
| 382 | /* |
| 383 | * Request a master image with specific attributes, return some unique |
| 384 | * identifier. |
| 385 | */ |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 386 | struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address, |
| 387 | u32 cycle, u32 dwidth) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 388 | { |
| 389 | struct vme_bridge *bridge; |
| 390 | struct list_head *master_pos = NULL; |
| 391 | struct vme_master_resource *allocated_image = NULL; |
| 392 | struct vme_master_resource *master_image = NULL; |
| 393 | struct vme_resource *resource = NULL; |
| 394 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 395 | bridge = vdev->bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 396 | if (bridge == NULL) { |
| 397 | printk(KERN_ERR "Can't find VME bus\n"); |
| 398 | goto err_bus; |
| 399 | } |
| 400 | |
| 401 | /* Loop through master resources */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 402 | list_for_each(master_pos, &bridge->master_resources) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 403 | master_image = list_entry(master_pos, |
| 404 | struct vme_master_resource, list); |
| 405 | |
| 406 | if (master_image == NULL) { |
| 407 | printk(KERN_WARNING "Registered NULL master resource\n"); |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | /* Find an unlocked and compatible image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 412 | spin_lock(&master_image->lock); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 413 | if (((master_image->address_attr & address) == address) && |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 414 | ((master_image->cycle_attr & cycle) == cycle) && |
| 415 | ((master_image->width_attr & dwidth) == dwidth) && |
| 416 | (master_image->locked == 0)) { |
| 417 | |
| 418 | master_image->locked = 1; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 419 | spin_unlock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 420 | allocated_image = master_image; |
| 421 | break; |
| 422 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 423 | spin_unlock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* Check to see if we found a resource */ |
| 427 | if (allocated_image == NULL) { |
| 428 | printk(KERN_ERR "Can't find a suitable resource\n"); |
| 429 | goto err_image; |
| 430 | } |
| 431 | |
| 432 | resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL); |
| 433 | if (resource == NULL) { |
| 434 | printk(KERN_ERR "Unable to allocate resource structure\n"); |
| 435 | goto err_alloc; |
| 436 | } |
| 437 | resource->type = VME_MASTER; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 438 | resource->entry = &allocated_image->list; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 439 | |
| 440 | return resource; |
| 441 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 442 | err_alloc: |
| 443 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 444 | spin_lock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 445 | master_image->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 446 | spin_unlock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 447 | err_image: |
| 448 | err_bus: |
| 449 | return NULL; |
| 450 | } |
| 451 | EXPORT_SYMBOL(vme_master_request); |
| 452 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 453 | int vme_master_set(struct vme_resource *resource, int enabled, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 454 | unsigned long long vme_base, unsigned long long size, u32 aspace, |
| 455 | u32 cycle, u32 dwidth) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 456 | { |
| 457 | struct vme_bridge *bridge = find_bridge(resource); |
| 458 | struct vme_master_resource *image; |
| 459 | int retval; |
| 460 | |
| 461 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 462 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 467 | |
| 468 | if (bridge->master_set == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 469 | printk(KERN_WARNING "vme_master_set not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 470 | return -EINVAL; |
| 471 | } |
| 472 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 473 | if (!(((image->address_attr & aspace) == aspace) && |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 474 | ((image->cycle_attr & cycle) == cycle) && |
| 475 | ((image->width_attr & dwidth) == dwidth))) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 476 | printk(KERN_WARNING "Invalid attributes\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 477 | return -EINVAL; |
| 478 | } |
| 479 | |
| 480 | retval = vme_check_window(aspace, vme_base, size); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 481 | if (retval) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 482 | return retval; |
| 483 | |
| 484 | return bridge->master_set(image, enabled, vme_base, size, aspace, |
| 485 | cycle, dwidth); |
| 486 | } |
| 487 | EXPORT_SYMBOL(vme_master_set); |
| 488 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 489 | int vme_master_get(struct vme_resource *resource, int *enabled, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 490 | unsigned long long *vme_base, unsigned long long *size, u32 *aspace, |
| 491 | u32 *cycle, u32 *dwidth) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 492 | { |
| 493 | struct vme_bridge *bridge = find_bridge(resource); |
| 494 | struct vme_master_resource *image; |
| 495 | |
| 496 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 497 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 498 | return -EINVAL; |
| 499 | } |
| 500 | |
| 501 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 502 | |
Emilio G. Cota | 51a569f | 2009-08-10 16:52:42 +0200 | [diff] [blame] | 503 | if (bridge->master_get == NULL) { |
Julia Lawall | c103830 | 2014-12-07 20:20:53 +0100 | [diff] [blame] | 504 | printk(KERN_WARNING "%s not supported\n", __func__); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | return bridge->master_get(image, enabled, vme_base, size, aspace, |
| 509 | cycle, dwidth); |
| 510 | } |
| 511 | EXPORT_SYMBOL(vme_master_get); |
| 512 | |
| 513 | /* |
| 514 | * Read data out of VME space into a buffer. |
| 515 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 516 | ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 517 | loff_t offset) |
| 518 | { |
| 519 | struct vme_bridge *bridge = find_bridge(resource); |
| 520 | struct vme_master_resource *image; |
| 521 | size_t length; |
| 522 | |
| 523 | if (bridge->master_read == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 524 | printk(KERN_WARNING "Reading from resource not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 525 | return -EINVAL; |
| 526 | } |
| 527 | |
| 528 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 529 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 530 | return -EINVAL; |
| 531 | } |
| 532 | |
| 533 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 534 | |
| 535 | length = vme_get_size(resource); |
| 536 | |
| 537 | if (offset > length) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 538 | printk(KERN_WARNING "Invalid Offset\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 539 | return -EFAULT; |
| 540 | } |
| 541 | |
| 542 | if ((offset + count) > length) |
| 543 | count = length - offset; |
| 544 | |
| 545 | return bridge->master_read(image, buf, count, offset); |
| 546 | |
| 547 | } |
| 548 | EXPORT_SYMBOL(vme_master_read); |
| 549 | |
| 550 | /* |
| 551 | * Write data out to VME space from a buffer. |
| 552 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 553 | ssize_t vme_master_write(struct vme_resource *resource, void *buf, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 554 | size_t count, loff_t offset) |
| 555 | { |
| 556 | struct vme_bridge *bridge = find_bridge(resource); |
| 557 | struct vme_master_resource *image; |
| 558 | size_t length; |
| 559 | |
| 560 | if (bridge->master_write == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 561 | printk(KERN_WARNING "Writing to resource not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 562 | return -EINVAL; |
| 563 | } |
| 564 | |
| 565 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 566 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 567 | return -EINVAL; |
| 568 | } |
| 569 | |
| 570 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 571 | |
| 572 | length = vme_get_size(resource); |
| 573 | |
| 574 | if (offset > length) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 575 | printk(KERN_WARNING "Invalid Offset\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 576 | return -EFAULT; |
| 577 | } |
| 578 | |
| 579 | if ((offset + count) > length) |
| 580 | count = length - offset; |
| 581 | |
| 582 | return bridge->master_write(image, buf, count, offset); |
| 583 | } |
| 584 | EXPORT_SYMBOL(vme_master_write); |
| 585 | |
| 586 | /* |
| 587 | * Perform RMW cycle to provided location. |
| 588 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 589 | unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 590 | unsigned int compare, unsigned int swap, loff_t offset) |
| 591 | { |
| 592 | struct vme_bridge *bridge = find_bridge(resource); |
| 593 | struct vme_master_resource *image; |
| 594 | |
| 595 | if (bridge->master_rmw == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 596 | printk(KERN_WARNING "Writing to resource not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 597 | return -EINVAL; |
| 598 | } |
| 599 | |
| 600 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 601 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 602 | return -EINVAL; |
| 603 | } |
| 604 | |
| 605 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 606 | |
| 607 | return bridge->master_rmw(image, mask, compare, swap, offset); |
| 608 | } |
| 609 | EXPORT_SYMBOL(vme_master_rmw); |
| 610 | |
Dmitry Kalinkin | c74a804 | 2015-02-26 18:53:10 +0300 | [diff] [blame] | 611 | int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma) |
| 612 | { |
| 613 | struct vme_master_resource *image; |
| 614 | phys_addr_t phys_addr; |
| 615 | unsigned long vma_size; |
| 616 | |
| 617 | if (resource->type != VME_MASTER) { |
| 618 | pr_err("Not a master resource\n"); |
| 619 | return -EINVAL; |
| 620 | } |
| 621 | |
| 622 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 623 | phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT); |
| 624 | vma_size = vma->vm_end - vma->vm_start; |
| 625 | |
| 626 | if (phys_addr + vma_size > image->bus_resource.end + 1) { |
| 627 | pr_err("Map size cannot exceed the window size\n"); |
| 628 | return -EFAULT; |
| 629 | } |
| 630 | |
| 631 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 632 | |
| 633 | return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start); |
| 634 | } |
| 635 | EXPORT_SYMBOL(vme_master_mmap); |
| 636 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 637 | void vme_master_free(struct vme_resource *resource) |
| 638 | { |
| 639 | struct vme_master_resource *master_image; |
| 640 | |
| 641 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 642 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 643 | return; |
| 644 | } |
| 645 | |
| 646 | master_image = list_entry(resource->entry, struct vme_master_resource, |
| 647 | list); |
| 648 | if (master_image == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 649 | printk(KERN_ERR "Can't find master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 650 | return; |
| 651 | } |
| 652 | |
| 653 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 654 | spin_lock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 655 | if (master_image->locked == 0) |
| 656 | printk(KERN_ERR "Image is already free\n"); |
| 657 | |
| 658 | master_image->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 659 | spin_unlock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 660 | |
| 661 | /* Free up resource memory */ |
| 662 | kfree(resource); |
| 663 | } |
| 664 | EXPORT_SYMBOL(vme_master_free); |
| 665 | |
| 666 | /* |
| 667 | * Request a DMA controller with specific attributes, return some unique |
| 668 | * identifier. |
| 669 | */ |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 670 | struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 671 | { |
| 672 | struct vme_bridge *bridge; |
| 673 | struct list_head *dma_pos = NULL; |
| 674 | struct vme_dma_resource *allocated_ctrlr = NULL; |
| 675 | struct vme_dma_resource *dma_ctrlr = NULL; |
| 676 | struct vme_resource *resource = NULL; |
| 677 | |
| 678 | /* XXX Not checking resource attributes */ |
| 679 | printk(KERN_ERR "No VME resource Attribute tests done\n"); |
| 680 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 681 | bridge = vdev->bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 682 | if (bridge == NULL) { |
| 683 | printk(KERN_ERR "Can't find VME bus\n"); |
| 684 | goto err_bus; |
| 685 | } |
| 686 | |
| 687 | /* Loop through DMA resources */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 688 | list_for_each(dma_pos, &bridge->dma_resources) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 689 | dma_ctrlr = list_entry(dma_pos, |
| 690 | struct vme_dma_resource, list); |
| 691 | |
| 692 | if (dma_ctrlr == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 693 | printk(KERN_ERR "Registered NULL DMA resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 694 | continue; |
| 695 | } |
| 696 | |
Martyn Welch | 4f723df | 2010-02-18 15:12:58 +0000 | [diff] [blame] | 697 | /* Find an unlocked and compatible controller */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 698 | mutex_lock(&dma_ctrlr->mtx); |
Martyn Welch | 4f723df | 2010-02-18 15:12:58 +0000 | [diff] [blame] | 699 | if (((dma_ctrlr->route_attr & route) == route) && |
| 700 | (dma_ctrlr->locked == 0)) { |
| 701 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 702 | dma_ctrlr->locked = 1; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 703 | mutex_unlock(&dma_ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 704 | allocated_ctrlr = dma_ctrlr; |
| 705 | break; |
| 706 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 707 | mutex_unlock(&dma_ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* Check to see if we found a resource */ |
| 711 | if (allocated_ctrlr == NULL) |
| 712 | goto err_ctrlr; |
| 713 | |
| 714 | resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL); |
| 715 | if (resource == NULL) { |
| 716 | printk(KERN_WARNING "Unable to allocate resource structure\n"); |
| 717 | goto err_alloc; |
| 718 | } |
| 719 | resource->type = VME_DMA; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 720 | resource->entry = &allocated_ctrlr->list; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 721 | |
| 722 | return resource; |
| 723 | |
| 724 | err_alloc: |
| 725 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 726 | mutex_lock(&dma_ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 727 | dma_ctrlr->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 728 | mutex_unlock(&dma_ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 729 | err_ctrlr: |
| 730 | err_bus: |
| 731 | return NULL; |
| 732 | } |
Martyn Welch | 58e5079 | 2009-10-29 16:35:20 +0000 | [diff] [blame] | 733 | EXPORT_SYMBOL(vme_dma_request); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Start new list |
| 737 | */ |
| 738 | struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource) |
| 739 | { |
| 740 | struct vme_dma_resource *ctrlr; |
| 741 | struct vme_dma_list *dma_list; |
| 742 | |
| 743 | if (resource->type != VME_DMA) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 744 | printk(KERN_ERR "Not a DMA resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 745 | return NULL; |
| 746 | } |
| 747 | |
| 748 | ctrlr = list_entry(resource->entry, struct vme_dma_resource, list); |
| 749 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 750 | dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL); |
| 751 | if (dma_list == NULL) { |
| 752 | printk(KERN_ERR "Unable to allocate memory for new dma list\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 753 | return NULL; |
| 754 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 755 | INIT_LIST_HEAD(&dma_list->entries); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 756 | dma_list->parent = ctrlr; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 757 | mutex_init(&dma_list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 758 | |
| 759 | return dma_list; |
| 760 | } |
| 761 | EXPORT_SYMBOL(vme_new_dma_list); |
| 762 | |
| 763 | /* |
| 764 | * Create "Pattern" type attributes |
| 765 | */ |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 766 | struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 767 | { |
| 768 | struct vme_dma_attr *attributes; |
| 769 | struct vme_dma_pattern *pattern_attr; |
| 770 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 771 | attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL); |
| 772 | if (attributes == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 773 | printk(KERN_ERR "Unable to allocate memory for attributes structure\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 774 | goto err_attr; |
| 775 | } |
| 776 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 777 | pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL); |
| 778 | if (pattern_attr == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 779 | printk(KERN_ERR "Unable to allocate memory for pattern attributes\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 780 | goto err_pat; |
| 781 | } |
| 782 | |
| 783 | attributes->type = VME_DMA_PATTERN; |
| 784 | attributes->private = (void *)pattern_attr; |
| 785 | |
| 786 | pattern_attr->pattern = pattern; |
| 787 | pattern_attr->type = type; |
| 788 | |
| 789 | return attributes; |
| 790 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 791 | err_pat: |
| 792 | kfree(attributes); |
| 793 | err_attr: |
| 794 | return NULL; |
| 795 | } |
| 796 | EXPORT_SYMBOL(vme_dma_pattern_attribute); |
| 797 | |
| 798 | /* |
| 799 | * Create "PCI" type attributes |
| 800 | */ |
| 801 | struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address) |
| 802 | { |
| 803 | struct vme_dma_attr *attributes; |
| 804 | struct vme_dma_pci *pci_attr; |
| 805 | |
| 806 | /* XXX Run some sanity checks here */ |
| 807 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 808 | attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL); |
| 809 | if (attributes == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 810 | printk(KERN_ERR "Unable to allocate memory for attributes structure\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 811 | goto err_attr; |
| 812 | } |
| 813 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 814 | pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL); |
| 815 | if (pci_attr == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 816 | printk(KERN_ERR "Unable to allocate memory for pci attributes\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 817 | goto err_pci; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | |
| 822 | attributes->type = VME_DMA_PCI; |
| 823 | attributes->private = (void *)pci_attr; |
| 824 | |
| 825 | pci_attr->address = address; |
| 826 | |
| 827 | return attributes; |
| 828 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 829 | err_pci: |
| 830 | kfree(attributes); |
| 831 | err_attr: |
| 832 | return NULL; |
| 833 | } |
| 834 | EXPORT_SYMBOL(vme_dma_pci_attribute); |
| 835 | |
| 836 | /* |
| 837 | * Create "VME" type attributes |
| 838 | */ |
| 839 | struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 840 | u32 aspace, u32 cycle, u32 dwidth) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 841 | { |
| 842 | struct vme_dma_attr *attributes; |
| 843 | struct vme_dma_vme *vme_attr; |
| 844 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 845 | attributes = kmalloc( |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 846 | sizeof(struct vme_dma_attr), GFP_KERNEL); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 847 | if (attributes == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 848 | printk(KERN_ERR "Unable to allocate memory for attributes structure\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 849 | goto err_attr; |
| 850 | } |
| 851 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 852 | vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL); |
| 853 | if (vme_attr == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 854 | printk(KERN_ERR "Unable to allocate memory for vme attributes\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 855 | goto err_vme; |
| 856 | } |
| 857 | |
| 858 | attributes->type = VME_DMA_VME; |
| 859 | attributes->private = (void *)vme_attr; |
| 860 | |
| 861 | vme_attr->address = address; |
| 862 | vme_attr->aspace = aspace; |
| 863 | vme_attr->cycle = cycle; |
| 864 | vme_attr->dwidth = dwidth; |
| 865 | |
| 866 | return attributes; |
| 867 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 868 | err_vme: |
| 869 | kfree(attributes); |
| 870 | err_attr: |
| 871 | return NULL; |
| 872 | } |
| 873 | EXPORT_SYMBOL(vme_dma_vme_attribute); |
| 874 | |
| 875 | /* |
| 876 | * Free attribute |
| 877 | */ |
| 878 | void vme_dma_free_attribute(struct vme_dma_attr *attributes) |
| 879 | { |
| 880 | kfree(attributes->private); |
| 881 | kfree(attributes); |
| 882 | } |
| 883 | EXPORT_SYMBOL(vme_dma_free_attribute); |
| 884 | |
| 885 | int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src, |
| 886 | struct vme_dma_attr *dest, size_t count) |
| 887 | { |
| 888 | struct vme_bridge *bridge = list->parent->parent; |
| 889 | int retval; |
| 890 | |
| 891 | if (bridge->dma_list_add == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 892 | printk(KERN_WARNING "Link List DMA generation not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 893 | return -EINVAL; |
| 894 | } |
| 895 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 896 | if (!mutex_trylock(&list->mtx)) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 897 | printk(KERN_ERR "Link List already submitted\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 898 | return -EINVAL; |
| 899 | } |
| 900 | |
| 901 | retval = bridge->dma_list_add(list, src, dest, count); |
| 902 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 903 | mutex_unlock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 904 | |
| 905 | return retval; |
| 906 | } |
| 907 | EXPORT_SYMBOL(vme_dma_list_add); |
| 908 | |
| 909 | int vme_dma_list_exec(struct vme_dma_list *list) |
| 910 | { |
| 911 | struct vme_bridge *bridge = list->parent->parent; |
| 912 | int retval; |
| 913 | |
| 914 | if (bridge->dma_list_exec == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 915 | printk(KERN_ERR "Link List DMA execution not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 916 | return -EINVAL; |
| 917 | } |
| 918 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 919 | mutex_lock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 920 | |
| 921 | retval = bridge->dma_list_exec(list); |
| 922 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 923 | mutex_unlock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 924 | |
| 925 | return retval; |
| 926 | } |
| 927 | EXPORT_SYMBOL(vme_dma_list_exec); |
| 928 | |
| 929 | int vme_dma_list_free(struct vme_dma_list *list) |
| 930 | { |
| 931 | struct vme_bridge *bridge = list->parent->parent; |
| 932 | int retval; |
| 933 | |
| 934 | if (bridge->dma_list_empty == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 935 | printk(KERN_WARNING "Emptying of Link Lists not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 936 | return -EINVAL; |
| 937 | } |
| 938 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 939 | if (!mutex_trylock(&list->mtx)) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 940 | printk(KERN_ERR "Link List in use\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 941 | return -EINVAL; |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | * Empty out all of the entries from the dma list. We need to go to the |
| 946 | * low level driver as dma entries are driver specific. |
| 947 | */ |
| 948 | retval = bridge->dma_list_empty(list); |
| 949 | if (retval) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 950 | printk(KERN_ERR "Unable to empty link-list entries\n"); |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 951 | mutex_unlock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 952 | return retval; |
| 953 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 954 | mutex_unlock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 955 | kfree(list); |
| 956 | |
| 957 | return retval; |
| 958 | } |
| 959 | EXPORT_SYMBOL(vme_dma_list_free); |
| 960 | |
| 961 | int vme_dma_free(struct vme_resource *resource) |
| 962 | { |
| 963 | struct vme_dma_resource *ctrlr; |
| 964 | |
| 965 | if (resource->type != VME_DMA) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 966 | printk(KERN_ERR "Not a DMA resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 967 | return -EINVAL; |
| 968 | } |
| 969 | |
| 970 | ctrlr = list_entry(resource->entry, struct vme_dma_resource, list); |
| 971 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 972 | if (!mutex_trylock(&ctrlr->mtx)) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 973 | printk(KERN_ERR "Resource busy, can't free\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 974 | return -EBUSY; |
| 975 | } |
| 976 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 977 | if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 978 | printk(KERN_WARNING "Resource still processing transfers\n"); |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 979 | mutex_unlock(&ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 980 | return -EBUSY; |
| 981 | } |
| 982 | |
| 983 | ctrlr->locked = 0; |
| 984 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 985 | mutex_unlock(&ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 986 | |
Martyn Welch | fd5c256 | 2013-06-06 12:29:16 +0100 | [diff] [blame] | 987 | kfree(resource); |
| 988 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | EXPORT_SYMBOL(vme_dma_free); |
| 992 | |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 993 | void vme_irq_handler(struct vme_bridge *bridge, int level, int statid) |
| 994 | { |
| 995 | void (*call)(int, int, void *); |
| 996 | void *priv_data; |
| 997 | |
| 998 | call = bridge->irq[level - 1].callback[statid].func; |
| 999 | priv_data = bridge->irq[level - 1].callback[statid].priv_data; |
| 1000 | |
| 1001 | if (call != NULL) |
| 1002 | call(level, statid, priv_data); |
| 1003 | else |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 1004 | printk(KERN_WARNING "Spurilous VME interrupt, level:%x, vector:%x\n", |
| 1005 | level, statid); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1006 | } |
| 1007 | EXPORT_SYMBOL(vme_irq_handler); |
| 1008 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1009 | int vme_irq_request(struct vme_dev *vdev, int level, int statid, |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1010 | void (*callback)(int, int, void *), |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1011 | void *priv_data) |
| 1012 | { |
| 1013 | struct vme_bridge *bridge; |
| 1014 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1015 | bridge = vdev->bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1016 | if (bridge == NULL) { |
| 1017 | printk(KERN_ERR "Can't find VME bus\n"); |
| 1018 | return -EINVAL; |
| 1019 | } |
| 1020 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1021 | if ((level < 1) || (level > 7)) { |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1022 | printk(KERN_ERR "Invalid interrupt level\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1023 | return -EINVAL; |
| 1024 | } |
| 1025 | |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1026 | if (bridge->irq_set == NULL) { |
| 1027 | printk(KERN_ERR "Configuring interrupts not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1028 | return -EINVAL; |
| 1029 | } |
| 1030 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1031 | mutex_lock(&bridge->irq_mtx); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1032 | |
| 1033 | if (bridge->irq[level - 1].callback[statid].func) { |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1034 | mutex_unlock(&bridge->irq_mtx); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1035 | printk(KERN_WARNING "VME Interrupt already taken\n"); |
| 1036 | return -EBUSY; |
| 1037 | } |
| 1038 | |
| 1039 | bridge->irq[level - 1].count++; |
| 1040 | bridge->irq[level - 1].callback[statid].priv_data = priv_data; |
| 1041 | bridge->irq[level - 1].callback[statid].func = callback; |
| 1042 | |
| 1043 | /* Enable IRQ level */ |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1044 | bridge->irq_set(bridge, level, 1, 1); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1045 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1046 | mutex_unlock(&bridge->irq_mtx); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1047 | |
| 1048 | return 0; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1049 | } |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1050 | EXPORT_SYMBOL(vme_irq_request); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1051 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1052 | void vme_irq_free(struct vme_dev *vdev, int level, int statid) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1053 | { |
| 1054 | struct vme_bridge *bridge; |
| 1055 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1056 | bridge = vdev->bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1057 | if (bridge == NULL) { |
| 1058 | printk(KERN_ERR "Can't find VME bus\n"); |
| 1059 | return; |
| 1060 | } |
| 1061 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1062 | if ((level < 1) || (level > 7)) { |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1063 | printk(KERN_ERR "Invalid interrupt level\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1064 | return; |
| 1065 | } |
| 1066 | |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1067 | if (bridge->irq_set == NULL) { |
| 1068 | printk(KERN_ERR "Configuring interrupts not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1069 | return; |
| 1070 | } |
| 1071 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1072 | mutex_lock(&bridge->irq_mtx); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1073 | |
| 1074 | bridge->irq[level - 1].count--; |
| 1075 | |
| 1076 | /* Disable IRQ level if no more interrupts attached at this level*/ |
| 1077 | if (bridge->irq[level - 1].count == 0) |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1078 | bridge->irq_set(bridge, level, 0, 1); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1079 | |
| 1080 | bridge->irq[level - 1].callback[statid].func = NULL; |
| 1081 | bridge->irq[level - 1].callback[statid].priv_data = NULL; |
| 1082 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1083 | mutex_unlock(&bridge->irq_mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1084 | } |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1085 | EXPORT_SYMBOL(vme_irq_free); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1086 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1087 | int vme_irq_generate(struct vme_dev *vdev, int level, int statid) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1088 | { |
| 1089 | struct vme_bridge *bridge; |
| 1090 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1091 | bridge = vdev->bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1092 | if (bridge == NULL) { |
| 1093 | printk(KERN_ERR "Can't find VME bus\n"); |
| 1094 | return -EINVAL; |
| 1095 | } |
| 1096 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1097 | if ((level < 1) || (level > 7)) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1098 | printk(KERN_WARNING "Invalid interrupt level\n"); |
| 1099 | return -EINVAL; |
| 1100 | } |
| 1101 | |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1102 | if (bridge->irq_generate == NULL) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1103 | printk(KERN_WARNING "Interrupt generation not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1104 | return -EINVAL; |
| 1105 | } |
| 1106 | |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1107 | return bridge->irq_generate(bridge, level, statid); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1108 | } |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1109 | EXPORT_SYMBOL(vme_irq_generate); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1110 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1111 | /* |
| 1112 | * Request the location monitor, return resource or NULL |
| 1113 | */ |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1114 | struct vme_resource *vme_lm_request(struct vme_dev *vdev) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1115 | { |
| 1116 | struct vme_bridge *bridge; |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1117 | struct list_head *lm_pos = NULL; |
| 1118 | struct vme_lm_resource *allocated_lm = NULL; |
| 1119 | struct vme_lm_resource *lm = NULL; |
| 1120 | struct vme_resource *resource = NULL; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1121 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1122 | bridge = vdev->bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1123 | if (bridge == NULL) { |
| 1124 | printk(KERN_ERR "Can't find VME bus\n"); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1125 | goto err_bus; |
| 1126 | } |
| 1127 | |
| 1128 | /* Loop through DMA resources */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1129 | list_for_each(lm_pos, &bridge->lm_resources) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1130 | lm = list_entry(lm_pos, |
| 1131 | struct vme_lm_resource, list); |
| 1132 | |
| 1133 | if (lm == NULL) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 1134 | printk(KERN_ERR "Registered NULL Location Monitor resource\n"); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1135 | continue; |
| 1136 | } |
| 1137 | |
| 1138 | /* Find an unlocked controller */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1139 | mutex_lock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1140 | if (lm->locked == 0) { |
| 1141 | lm->locked = 1; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1142 | mutex_unlock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1143 | allocated_lm = lm; |
| 1144 | break; |
| 1145 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1146 | mutex_unlock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /* Check to see if we found a resource */ |
| 1150 | if (allocated_lm == NULL) |
| 1151 | goto err_lm; |
| 1152 | |
| 1153 | resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL); |
| 1154 | if (resource == NULL) { |
| 1155 | printk(KERN_ERR "Unable to allocate resource structure\n"); |
| 1156 | goto err_alloc; |
| 1157 | } |
| 1158 | resource->type = VME_LM; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1159 | resource->entry = &allocated_lm->list; |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1160 | |
| 1161 | return resource; |
| 1162 | |
| 1163 | err_alloc: |
| 1164 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1165 | mutex_lock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1166 | lm->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1167 | mutex_unlock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1168 | err_lm: |
| 1169 | err_bus: |
| 1170 | return NULL; |
| 1171 | } |
| 1172 | EXPORT_SYMBOL(vme_lm_request); |
| 1173 | |
| 1174 | int vme_lm_count(struct vme_resource *resource) |
| 1175 | { |
| 1176 | struct vme_lm_resource *lm; |
| 1177 | |
| 1178 | if (resource->type != VME_LM) { |
| 1179 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1180 | return -EINVAL; |
| 1181 | } |
| 1182 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1183 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1184 | |
| 1185 | return lm->monitors; |
| 1186 | } |
| 1187 | EXPORT_SYMBOL(vme_lm_count); |
| 1188 | |
| 1189 | int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 1190 | u32 aspace, u32 cycle) |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1191 | { |
| 1192 | struct vme_bridge *bridge = find_bridge(resource); |
| 1193 | struct vme_lm_resource *lm; |
| 1194 | |
| 1195 | if (resource->type != VME_LM) { |
| 1196 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
| 1197 | return -EINVAL; |
| 1198 | } |
| 1199 | |
| 1200 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1201 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1202 | if (bridge->lm_set == NULL) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1203 | printk(KERN_ERR "vme_lm_set not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1204 | return -EINVAL; |
| 1205 | } |
| 1206 | |
Martyn Welch | 8be9226 | 2009-10-29 16:35:08 +0000 | [diff] [blame] | 1207 | return bridge->lm_set(lm, lm_base, aspace, cycle); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1208 | } |
| 1209 | EXPORT_SYMBOL(vme_lm_set); |
| 1210 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1211 | int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 1212 | u32 *aspace, u32 *cycle) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1213 | { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1214 | struct vme_bridge *bridge = find_bridge(resource); |
| 1215 | struct vme_lm_resource *lm; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1216 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1217 | if (resource->type != VME_LM) { |
| 1218 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1219 | return -EINVAL; |
| 1220 | } |
| 1221 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1222 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1223 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1224 | if (bridge->lm_get == NULL) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1225 | printk(KERN_ERR "vme_lm_get not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1226 | return -EINVAL; |
| 1227 | } |
| 1228 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1229 | return bridge->lm_get(lm, lm_base, aspace, cycle); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1230 | } |
| 1231 | EXPORT_SYMBOL(vme_lm_get); |
| 1232 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1233 | int vme_lm_attach(struct vme_resource *resource, int monitor, |
| 1234 | void (*callback)(int)) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1235 | { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1236 | struct vme_bridge *bridge = find_bridge(resource); |
| 1237 | struct vme_lm_resource *lm; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1238 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1239 | if (resource->type != VME_LM) { |
| 1240 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1241 | return -EINVAL; |
| 1242 | } |
| 1243 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1244 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1245 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1246 | if (bridge->lm_attach == NULL) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1247 | printk(KERN_ERR "vme_lm_attach not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1248 | return -EINVAL; |
| 1249 | } |
| 1250 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1251 | return bridge->lm_attach(lm, monitor, callback); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1252 | } |
| 1253 | EXPORT_SYMBOL(vme_lm_attach); |
| 1254 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1255 | int vme_lm_detach(struct vme_resource *resource, int monitor) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1256 | { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1257 | struct vme_bridge *bridge = find_bridge(resource); |
| 1258 | struct vme_lm_resource *lm; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1259 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1260 | if (resource->type != VME_LM) { |
| 1261 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1262 | return -EINVAL; |
| 1263 | } |
| 1264 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1265 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1266 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1267 | if (bridge->lm_detach == NULL) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1268 | printk(KERN_ERR "vme_lm_detach not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1269 | return -EINVAL; |
| 1270 | } |
| 1271 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1272 | return bridge->lm_detach(lm, monitor); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1273 | } |
| 1274 | EXPORT_SYMBOL(vme_lm_detach); |
| 1275 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1276 | void vme_lm_free(struct vme_resource *resource) |
| 1277 | { |
| 1278 | struct vme_lm_resource *lm; |
| 1279 | |
| 1280 | if (resource->type != VME_LM) { |
| 1281 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
| 1282 | return; |
| 1283 | } |
| 1284 | |
| 1285 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1286 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1287 | mutex_lock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1288 | |
Martyn Welch | 8be9226 | 2009-10-29 16:35:08 +0000 | [diff] [blame] | 1289 | /* XXX |
| 1290 | * Check to see that there aren't any callbacks still attached, if |
| 1291 | * there are we should probably be detaching them! |
| 1292 | */ |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1293 | |
| 1294 | lm->locked = 0; |
| 1295 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1296 | mutex_unlock(&lm->mtx); |
Martyn Welch | 8be9226 | 2009-10-29 16:35:08 +0000 | [diff] [blame] | 1297 | |
| 1298 | kfree(resource); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1299 | } |
| 1300 | EXPORT_SYMBOL(vme_lm_free); |
| 1301 | |
Martyn Welch | d7729f0 | 2013-11-08 11:58:35 +0000 | [diff] [blame] | 1302 | int vme_slot_num(struct vme_dev *vdev) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1303 | { |
| 1304 | struct vme_bridge *bridge; |
| 1305 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1306 | bridge = vdev->bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1307 | if (bridge == NULL) { |
| 1308 | printk(KERN_ERR "Can't find VME bus\n"); |
| 1309 | return -EINVAL; |
| 1310 | } |
| 1311 | |
| 1312 | if (bridge->slot_get == NULL) { |
Martyn Welch | d7729f0 | 2013-11-08 11:58:35 +0000 | [diff] [blame] | 1313 | printk(KERN_WARNING "vme_slot_num not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1314 | return -EINVAL; |
| 1315 | } |
| 1316 | |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1317 | return bridge->slot_get(bridge); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1318 | } |
Martyn Welch | d7729f0 | 2013-11-08 11:58:35 +0000 | [diff] [blame] | 1319 | EXPORT_SYMBOL(vme_slot_num); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1320 | |
Martyn Welch | 978f47d | 2013-11-08 11:58:34 +0000 | [diff] [blame] | 1321 | int vme_bus_num(struct vme_dev *vdev) |
| 1322 | { |
| 1323 | struct vme_bridge *bridge; |
| 1324 | |
| 1325 | bridge = vdev->bridge; |
| 1326 | if (bridge == NULL) { |
| 1327 | pr_err("Can't find VME bus\n"); |
| 1328 | return -EINVAL; |
| 1329 | } |
| 1330 | |
| 1331 | return bridge->num; |
| 1332 | } |
| 1333 | EXPORT_SYMBOL(vme_bus_num); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1334 | |
| 1335 | /* - Bridge Registration --------------------------------------------------- */ |
| 1336 | |
Manohar Vanga | 5b93c2a | 2011-11-04 11:12:30 +0100 | [diff] [blame] | 1337 | static void vme_dev_release(struct device *dev) |
| 1338 | { |
| 1339 | kfree(dev_to_vme_dev(dev)); |
| 1340 | } |
| 1341 | |
| 1342 | int vme_register_bridge(struct vme_bridge *bridge) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1343 | { |
| 1344 | int i; |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1345 | int ret = -1; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1346 | |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1347 | mutex_lock(&vme_buses_lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1348 | for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) { |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1349 | if ((vme_bus_numbers & (1 << i)) == 0) { |
| 1350 | vme_bus_numbers |= (1 << i); |
| 1351 | bridge->num = i; |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1352 | INIT_LIST_HEAD(&bridge->devices); |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1353 | list_add_tail(&bridge->bus_list, &vme_bus_list); |
| 1354 | ret = 0; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1355 | break; |
| 1356 | } |
| 1357 | } |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1358 | mutex_unlock(&vme_buses_lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1359 | |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1360 | return ret; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1361 | } |
Manohar Vanga | 5b93c2a | 2011-11-04 11:12:30 +0100 | [diff] [blame] | 1362 | EXPORT_SYMBOL(vme_register_bridge); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1363 | |
Manohar Vanga | 5b93c2a | 2011-11-04 11:12:30 +0100 | [diff] [blame] | 1364 | void vme_unregister_bridge(struct vme_bridge *bridge) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1365 | { |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1366 | struct vme_dev *vdev; |
| 1367 | struct vme_dev *tmp; |
| 1368 | |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1369 | mutex_lock(&vme_buses_lock); |
| 1370 | vme_bus_numbers &= ~(1 << bridge->num); |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1371 | list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) { |
| 1372 | list_del(&vdev->drv_list); |
| 1373 | list_del(&vdev->bridge_list); |
| 1374 | device_unregister(&vdev->dev); |
| 1375 | } |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1376 | list_del(&bridge->bus_list); |
| 1377 | mutex_unlock(&vme_buses_lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1378 | } |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1379 | EXPORT_SYMBOL(vme_unregister_bridge); |
| 1380 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1381 | /* - Driver Registration --------------------------------------------------- */ |
| 1382 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1383 | static int __vme_register_driver_bus(struct vme_driver *drv, |
| 1384 | struct vme_bridge *bridge, unsigned int ndevs) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1385 | { |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1386 | int err; |
| 1387 | unsigned int i; |
| 1388 | struct vme_dev *vdev; |
| 1389 | struct vme_dev *tmp; |
| 1390 | |
| 1391 | for (i = 0; i < ndevs; i++) { |
| 1392 | vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL); |
| 1393 | if (!vdev) { |
| 1394 | err = -ENOMEM; |
| 1395 | goto err_devalloc; |
| 1396 | } |
Manohar Vanga | a916a39 | 2011-09-26 11:27:17 +0200 | [diff] [blame] | 1397 | vdev->num = i; |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1398 | vdev->bridge = bridge; |
| 1399 | vdev->dev.platform_data = drv; |
| 1400 | vdev->dev.release = vme_dev_release; |
| 1401 | vdev->dev.parent = bridge->parent; |
| 1402 | vdev->dev.bus = &vme_bus_type; |
Manohar Vanga | a916a39 | 2011-09-26 11:27:17 +0200 | [diff] [blame] | 1403 | dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num, |
| 1404 | vdev->num); |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1405 | |
| 1406 | err = device_register(&vdev->dev); |
| 1407 | if (err) |
| 1408 | goto err_reg; |
| 1409 | |
| 1410 | if (vdev->dev.platform_data) { |
| 1411 | list_add_tail(&vdev->drv_list, &drv->devices); |
| 1412 | list_add_tail(&vdev->bridge_list, &bridge->devices); |
| 1413 | } else |
| 1414 | device_unregister(&vdev->dev); |
| 1415 | } |
| 1416 | return 0; |
| 1417 | |
| 1418 | err_reg: |
Emilio G. Cota | def1820 | 2013-02-13 13:47:54 -0500 | [diff] [blame] | 1419 | put_device(&vdev->dev); |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1420 | kfree(vdev); |
| 1421 | err_devalloc: |
| 1422 | list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) { |
| 1423 | list_del(&vdev->drv_list); |
| 1424 | list_del(&vdev->bridge_list); |
| 1425 | device_unregister(&vdev->dev); |
| 1426 | } |
| 1427 | return err; |
| 1428 | } |
| 1429 | |
| 1430 | static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs) |
| 1431 | { |
| 1432 | struct vme_bridge *bridge; |
| 1433 | int err = 0; |
| 1434 | |
| 1435 | mutex_lock(&vme_buses_lock); |
| 1436 | list_for_each_entry(bridge, &vme_bus_list, bus_list) { |
| 1437 | /* |
| 1438 | * This cannot cause trouble as we already have vme_buses_lock |
| 1439 | * and if the bridge is removed, it will have to go through |
| 1440 | * vme_unregister_bridge() to do it (which calls remove() on |
| 1441 | * the bridge which in turn tries to acquire vme_buses_lock and |
Manohar Vanga | c26f611 | 2011-11-04 11:12:29 +0100 | [diff] [blame] | 1442 | * will have to wait). |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1443 | */ |
| 1444 | err = __vme_register_driver_bus(drv, bridge, ndevs); |
| 1445 | if (err) |
| 1446 | break; |
| 1447 | } |
| 1448 | mutex_unlock(&vme_buses_lock); |
| 1449 | return err; |
| 1450 | } |
| 1451 | |
| 1452 | int vme_register_driver(struct vme_driver *drv, unsigned int ndevs) |
| 1453 | { |
| 1454 | int err; |
| 1455 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1456 | drv->driver.name = drv->name; |
| 1457 | drv->driver.bus = &vme_bus_type; |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1458 | INIT_LIST_HEAD(&drv->devices); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1459 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1460 | err = driver_register(&drv->driver); |
| 1461 | if (err) |
| 1462 | return err; |
| 1463 | |
| 1464 | err = __vme_register_driver(drv, ndevs); |
| 1465 | if (err) |
| 1466 | driver_unregister(&drv->driver); |
| 1467 | |
| 1468 | return err; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1469 | } |
| 1470 | EXPORT_SYMBOL(vme_register_driver); |
| 1471 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1472 | void vme_unregister_driver(struct vme_driver *drv) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1473 | { |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1474 | struct vme_dev *dev, *dev_tmp; |
| 1475 | |
| 1476 | mutex_lock(&vme_buses_lock); |
| 1477 | list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) { |
| 1478 | list_del(&dev->drv_list); |
| 1479 | list_del(&dev->bridge_list); |
| 1480 | device_unregister(&dev->dev); |
| 1481 | } |
| 1482 | mutex_unlock(&vme_buses_lock); |
| 1483 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1484 | driver_unregister(&drv->driver); |
| 1485 | } |
| 1486 | EXPORT_SYMBOL(vme_unregister_driver); |
| 1487 | |
| 1488 | /* - Bus Registration ------------------------------------------------------ */ |
| 1489 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1490 | static int vme_bus_match(struct device *dev, struct device_driver *drv) |
| 1491 | { |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1492 | struct vme_driver *vme_drv; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1493 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1494 | vme_drv = container_of(drv, struct vme_driver, driver); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1495 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1496 | if (dev->platform_data == vme_drv) { |
| 1497 | struct vme_dev *vdev = dev_to_vme_dev(dev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1498 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1499 | if (vme_drv->match && vme_drv->match(vdev)) |
| 1500 | return 1; |
| 1501 | |
| 1502 | dev->platform_data = NULL; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1503 | } |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | static int vme_bus_probe(struct device *dev) |
| 1508 | { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1509 | int retval = -ENODEV; |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1510 | struct vme_driver *driver; |
| 1511 | struct vme_dev *vdev = dev_to_vme_dev(dev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1512 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1513 | driver = dev->platform_data; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1514 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1515 | if (driver->probe != NULL) |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1516 | retval = driver->probe(vdev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1517 | |
| 1518 | return retval; |
| 1519 | } |
| 1520 | |
| 1521 | static int vme_bus_remove(struct device *dev) |
| 1522 | { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1523 | int retval = -ENODEV; |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1524 | struct vme_driver *driver; |
| 1525 | struct vme_dev *vdev = dev_to_vme_dev(dev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1526 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1527 | driver = dev->platform_data; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1528 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1529 | if (driver->remove != NULL) |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1530 | retval = driver->remove(vdev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1531 | |
| 1532 | return retval; |
| 1533 | } |
| 1534 | |
| 1535 | struct bus_type vme_bus_type = { |
| 1536 | .name = "vme", |
| 1537 | .match = vme_bus_match, |
| 1538 | .probe = vme_bus_probe, |
| 1539 | .remove = vme_bus_remove, |
| 1540 | }; |
| 1541 | EXPORT_SYMBOL(vme_bus_type); |
| 1542 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1543 | static int __init vme_init(void) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1544 | { |
| 1545 | return bus_register(&vme_bus_type); |
| 1546 | } |
| 1547 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1548 | static void __exit vme_exit(void) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1549 | { |
| 1550 | bus_unregister(&vme_bus_type); |
| 1551 | } |
| 1552 | |
Aaron Sierra | c326cc0 | 2013-12-09 09:54:42 -0600 | [diff] [blame] | 1553 | subsys_initcall(vme_init); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1554 | module_exit(vme_exit); |