Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 2 | /* |
| 3 | * VME Bridge Framework |
| 4 | * |
Martyn Welch | 66bd8db | 2010-02-18 15:12:52 +0000 | [diff] [blame] | 5 | * Author: Martyn Welch <martyn.welch@ge.com> |
| 6 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 7 | * |
| 8 | * Based on work by Tom Armistead and Ajit Prem |
| 9 | * Copyright 2004 Motorola Inc. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
Paul Gortmaker | 050c3d5 | 2016-07-03 14:05:56 -0400 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/export.h> |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 14 | #include <linux/mm.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/pci.h> |
| 19 | #include <linux/poll.h> |
| 20 | #include <linux/highmem.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/pagemap.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/dma-mapping.h> |
| 25 | #include <linux/syscalls.h> |
Martyn Welch | 400822f | 2009-08-11 16:20:22 +0100 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 27 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Greg Kroah-Hartman | db3b9e9 | 2012-04-26 12:34:58 -0700 | [diff] [blame] | 29 | #include <linux/vme.h> |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 30 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 31 | #include "vme_bridge.h" |
| 32 | |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 33 | /* Bitmask and list of registered buses both protected by common mutex */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 34 | static unsigned int vme_bus_numbers; |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 35 | static LIST_HEAD(vme_bus_list); |
| 36 | static DEFINE_MUTEX(vme_buses_lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 37 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 38 | static int __init vme_init(void); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 39 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 40 | static struct vme_dev *dev_to_vme_dev(struct device *dev) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 41 | { |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 42 | return container_of(dev, struct vme_dev, dev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Find the bridge that the resource is associated with. |
| 47 | */ |
| 48 | static struct vme_bridge *find_bridge(struct vme_resource *resource) |
| 49 | { |
| 50 | /* Get list to search */ |
| 51 | switch (resource->type) { |
| 52 | case VME_MASTER: |
| 53 | return list_entry(resource->entry, struct vme_master_resource, |
| 54 | list)->parent; |
| 55 | break; |
| 56 | case VME_SLAVE: |
| 57 | return list_entry(resource->entry, struct vme_slave_resource, |
| 58 | list)->parent; |
| 59 | break; |
| 60 | case VME_DMA: |
| 61 | return list_entry(resource->entry, struct vme_dma_resource, |
| 62 | list)->parent; |
| 63 | break; |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 64 | case VME_LM: |
| 65 | return list_entry(resource->entry, struct vme_lm_resource, |
| 66 | list)->parent; |
| 67 | break; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 68 | default: |
| 69 | printk(KERN_ERR "Unknown resource type\n"); |
| 70 | return NULL; |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 75 | /** |
| 76 | * vme_free_consistent - Allocate contiguous memory. |
| 77 | * @resource: Pointer to VME resource. |
| 78 | * @size: Size of allocation required. |
| 79 | * @dma: Pointer to variable to store physical address of allocation. |
| 80 | * |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 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 | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 83 | * |
| 84 | * Return: Virtual address of allocation on success, NULL on failure. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 85 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 86 | void *vme_alloc_consistent(struct vme_resource *resource, size_t size, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 87 | dma_addr_t *dma) |
| 88 | { |
| 89 | struct vme_bridge *bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 90 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 91 | if (!resource) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 92 | printk(KERN_ERR "No resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | bridge = find_bridge(resource); |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 97 | if (!bridge) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 98 | printk(KERN_ERR "Can't find bridge\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 99 | return NULL; |
| 100 | } |
| 101 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 102 | if (!bridge->parent) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 103 | printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 104 | return NULL; |
| 105 | } |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 106 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 107 | if (!bridge->alloc_consistent) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 108 | printk(KERN_ERR "alloc_consistent not supported by bridge %s\n", |
| 109 | bridge->name); |
Manohar Vanga | 7f58f02 | 2011-08-10 11:33:46 +0200 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | return bridge->alloc_consistent(bridge->parent, size, dma); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 114 | } |
| 115 | EXPORT_SYMBOL(vme_alloc_consistent); |
| 116 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 117 | /** |
| 118 | * vme_free_consistent - Free previously allocated memory. |
| 119 | * @resource: Pointer to VME resource. |
| 120 | * @size: Size of allocation to free. |
| 121 | * @vaddr: Virtual address of allocation. |
| 122 | * @dma: Physical address of allocation. |
| 123 | * |
| 124 | * Free previously allocated block of contiguous memory. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 125 | */ |
| 126 | void vme_free_consistent(struct vme_resource *resource, size_t size, |
| 127 | void *vaddr, dma_addr_t dma) |
| 128 | { |
| 129 | struct vme_bridge *bridge; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 130 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 131 | if (!resource) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 132 | printk(KERN_ERR "No resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
| 136 | bridge = find_bridge(resource); |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 137 | if (!bridge) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 138 | printk(KERN_ERR "Can't find bridge\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 142 | if (!bridge->parent) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 143 | printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name); |
Manohar Vanga | 7f58f02 | 2011-08-10 11:33:46 +0200 | [diff] [blame] | 144 | return; |
| 145 | } |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 146 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 147 | if (!bridge->free_consistent) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 148 | printk(KERN_ERR "free_consistent not supported by bridge %s\n", |
| 149 | bridge->name); |
Manohar Vanga | 7f58f02 | 2011-08-10 11:33:46 +0200 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
| 153 | bridge->free_consistent(bridge->parent, size, vaddr, dma); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 154 | } |
| 155 | EXPORT_SYMBOL(vme_free_consistent); |
| 156 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 157 | /** |
| 158 | * vme_get_size - Helper function returning size of a VME window |
| 159 | * @resource: Pointer to VME slave or master resource. |
| 160 | * |
| 161 | * Determine the size of the VME window provided. This is a helper |
| 162 | * function, wrappering the call to vme_master_get or vme_slave_get |
| 163 | * depending on the type of window resource handed to it. |
| 164 | * |
| 165 | * Return: Size of the window on success, zero on failure. |
| 166 | */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 167 | size_t vme_get_size(struct vme_resource *resource) |
| 168 | { |
| 169 | int enabled, retval; |
| 170 | unsigned long long base, size; |
| 171 | dma_addr_t buf_base; |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 172 | u32 aspace, cycle, dwidth; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 173 | |
| 174 | switch (resource->type) { |
| 175 | case VME_MASTER: |
| 176 | retval = vme_master_get(resource, &enabled, &base, &size, |
| 177 | &aspace, &cycle, &dwidth); |
Martyn Welch | 6ad3756 | 2016-10-21 17:36:19 +0100 | [diff] [blame] | 178 | if (retval) |
| 179 | return 0; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 180 | |
| 181 | return size; |
| 182 | break; |
| 183 | case VME_SLAVE: |
| 184 | retval = vme_slave_get(resource, &enabled, &base, &size, |
| 185 | &buf_base, &aspace, &cycle); |
Martyn Welch | 6ad3756 | 2016-10-21 17:36:19 +0100 | [diff] [blame] | 186 | if (retval) |
| 187 | return 0; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 188 | |
| 189 | return size; |
| 190 | break; |
| 191 | case VME_DMA: |
| 192 | return 0; |
| 193 | break; |
| 194 | default: |
| 195 | printk(KERN_ERR "Unknown resource type\n"); |
| 196 | return 0; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | EXPORT_SYMBOL(vme_get_size); |
| 201 | |
Dmitry Kalinkin | ef73f88 | 2015-05-28 15:07:04 +0300 | [diff] [blame] | 202 | int vme_check_window(u32 aspace, unsigned long long vme_base, |
| 203 | unsigned long long size) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 204 | { |
| 205 | int retval = 0; |
| 206 | |
Dan Carpenter | bd14798 | 2017-09-30 14:27:41 +0300 | [diff] [blame] | 207 | if (vme_base + size < size) |
| 208 | return -EINVAL; |
| 209 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 210 | switch (aspace) { |
| 211 | case VME_A16: |
Dan Carpenter | bd14798 | 2017-09-30 14:27:41 +0300 | [diff] [blame] | 212 | if (vme_base + size > VME_A16_MAX) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 213 | retval = -EFAULT; |
| 214 | break; |
| 215 | case VME_A24: |
Dan Carpenter | bd14798 | 2017-09-30 14:27:41 +0300 | [diff] [blame] | 216 | if (vme_base + size > VME_A24_MAX) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 217 | retval = -EFAULT; |
| 218 | break; |
| 219 | case VME_A32: |
Dan Carpenter | bd14798 | 2017-09-30 14:27:41 +0300 | [diff] [blame] | 220 | if (vme_base + size > VME_A32_MAX) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 221 | retval = -EFAULT; |
| 222 | break; |
| 223 | case VME_A64: |
Dan Carpenter | bd14798 | 2017-09-30 14:27:41 +0300 | [diff] [blame] | 224 | /* The VME_A64_MAX limit is actually U64_MAX + 1 */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 225 | break; |
| 226 | case VME_CRCSR: |
Dan Carpenter | bd14798 | 2017-09-30 14:27:41 +0300 | [diff] [blame] | 227 | if (vme_base + size > VME_CRCSR_MAX) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 228 | retval = -EFAULT; |
| 229 | break; |
| 230 | case VME_USER1: |
| 231 | case VME_USER2: |
| 232 | case VME_USER3: |
| 233 | case VME_USER4: |
| 234 | /* User Defined */ |
| 235 | break; |
| 236 | default: |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 237 | printk(KERN_ERR "Invalid address space\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 238 | retval = -EINVAL; |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | return retval; |
| 243 | } |
Dmitry Kalinkin | ef73f88 | 2015-05-28 15:07:04 +0300 | [diff] [blame] | 244 | EXPORT_SYMBOL(vme_check_window); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 245 | |
Dmitry Kalinkin | 472f16f | 2015-09-18 02:01:43 +0300 | [diff] [blame] | 246 | static u32 vme_get_aspace(int am) |
| 247 | { |
| 248 | switch (am) { |
| 249 | case 0x29: |
| 250 | case 0x2D: |
| 251 | return VME_A16; |
| 252 | case 0x38: |
| 253 | case 0x39: |
| 254 | case 0x3A: |
| 255 | case 0x3B: |
| 256 | case 0x3C: |
| 257 | case 0x3D: |
| 258 | case 0x3E: |
| 259 | case 0x3F: |
| 260 | return VME_A24; |
| 261 | case 0x8: |
| 262 | case 0x9: |
| 263 | case 0xA: |
| 264 | case 0xB: |
| 265 | case 0xC: |
| 266 | case 0xD: |
| 267 | case 0xE: |
| 268 | case 0xF: |
| 269 | return VME_A32; |
| 270 | case 0x0: |
| 271 | case 0x1: |
| 272 | case 0x3: |
| 273 | return VME_A64; |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 279 | /** |
| 280 | * vme_slave_request - Request a VME slave window resource. |
| 281 | * @vdev: Pointer to VME device struct vme_dev assigned to driver instance. |
| 282 | * @address: Required VME address space. |
| 283 | * @cycle: Required VME data transfer cycle type. |
| 284 | * |
| 285 | * Request use of a VME window resource capable of being set for the requested |
| 286 | * address space and data transfer cycle. |
| 287 | * |
| 288 | * Return: Pointer to VME resource on success, NULL on failure. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 289 | */ |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 290 | struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address, |
| 291 | u32 cycle) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 292 | { |
| 293 | struct vme_bridge *bridge; |
| 294 | struct list_head *slave_pos = NULL; |
| 295 | struct vme_slave_resource *allocated_image = NULL; |
| 296 | struct vme_slave_resource *slave_image = NULL; |
| 297 | struct vme_resource *resource = NULL; |
| 298 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 299 | bridge = vdev->bridge; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 300 | if (!bridge) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 301 | printk(KERN_ERR "Can't find VME bus\n"); |
| 302 | goto err_bus; |
| 303 | } |
| 304 | |
| 305 | /* Loop through slave resources */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 306 | list_for_each(slave_pos, &bridge->slave_resources) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 307 | slave_image = list_entry(slave_pos, |
| 308 | struct vme_slave_resource, list); |
| 309 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 310 | if (!slave_image) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 311 | printk(KERN_ERR "Registered NULL Slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 312 | continue; |
| 313 | } |
| 314 | |
| 315 | /* Find an unlocked and compatible image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 316 | mutex_lock(&slave_image->mtx); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 317 | if (((slave_image->address_attr & address) == address) && |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 318 | ((slave_image->cycle_attr & cycle) == cycle) && |
| 319 | (slave_image->locked == 0)) { |
| 320 | |
| 321 | slave_image->locked = 1; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 322 | mutex_unlock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 323 | allocated_image = slave_image; |
| 324 | break; |
| 325 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 326 | mutex_unlock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* No free image */ |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 330 | if (!allocated_image) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 331 | goto err_image; |
| 332 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 333 | resource = kmalloc(sizeof(*resource), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 334 | if (!resource) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 335 | goto err_alloc; |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 336 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 337 | resource->type = VME_SLAVE; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 338 | resource->entry = &allocated_image->list; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 339 | |
| 340 | return resource; |
| 341 | |
| 342 | err_alloc: |
| 343 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 344 | mutex_lock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 345 | slave_image->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 346 | mutex_unlock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 347 | err_image: |
| 348 | err_bus: |
| 349 | return NULL; |
| 350 | } |
| 351 | EXPORT_SYMBOL(vme_slave_request); |
| 352 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 353 | /** |
| 354 | * vme_slave_set - Set VME slave window configuration. |
| 355 | * @resource: Pointer to VME slave resource. |
| 356 | * @enabled: State to which the window should be configured. |
| 357 | * @vme_base: Base address for the window. |
| 358 | * @size: Size of the VME window. |
| 359 | * @buf_base: Based address of buffer used to provide VME slave window storage. |
| 360 | * @aspace: VME address space for the VME window. |
| 361 | * @cycle: VME data transfer cycle type for the VME window. |
| 362 | * |
| 363 | * Set configuration for provided VME slave window. |
| 364 | * |
| 365 | * Return: Zero on success, -EINVAL if operation is not supported on this |
| 366 | * device, if an invalid resource has been provided or invalid |
| 367 | * attributes are provided. Hardware specific errors may also be |
| 368 | * returned. |
| 369 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 370 | int vme_slave_set(struct vme_resource *resource, int enabled, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 371 | unsigned long long vme_base, unsigned long long size, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 372 | dma_addr_t buf_base, u32 aspace, u32 cycle) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 373 | { |
| 374 | struct vme_bridge *bridge = find_bridge(resource); |
| 375 | struct vme_slave_resource *image; |
| 376 | int retval; |
| 377 | |
| 378 | if (resource->type != VME_SLAVE) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 379 | printk(KERN_ERR "Not a slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | image = list_entry(resource->entry, struct vme_slave_resource, list); |
| 384 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 385 | if (!bridge->slave_set) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 386 | printk(KERN_ERR "Function not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 387 | return -ENOSYS; |
| 388 | } |
| 389 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 390 | if (!(((image->address_attr & aspace) == aspace) && |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 391 | ((image->cycle_attr & cycle) == cycle))) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 392 | printk(KERN_ERR "Invalid attributes\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 393 | return -EINVAL; |
| 394 | } |
| 395 | |
| 396 | retval = vme_check_window(aspace, vme_base, size); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 397 | if (retval) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 398 | return retval; |
| 399 | |
| 400 | return bridge->slave_set(image, enabled, vme_base, size, buf_base, |
| 401 | aspace, cycle); |
| 402 | } |
| 403 | EXPORT_SYMBOL(vme_slave_set); |
| 404 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 405 | /** |
| 406 | * vme_slave_get - Retrieve VME slave window configuration. |
| 407 | * @resource: Pointer to VME slave resource. |
| 408 | * @enabled: Pointer to variable for storing state. |
| 409 | * @vme_base: Pointer to variable for storing window base address. |
| 410 | * @size: Pointer to variable for storing window size. |
| 411 | * @buf_base: Pointer to variable for storing slave buffer base address. |
| 412 | * @aspace: Pointer to variable for storing VME address space. |
| 413 | * @cycle: Pointer to variable for storing VME data transfer cycle type. |
| 414 | * |
| 415 | * Return configuration for provided VME slave window. |
| 416 | * |
| 417 | * Return: Zero on success, -EINVAL if operation is not supported on this |
| 418 | * device or if an invalid resource has been provided. |
| 419 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 420 | int vme_slave_get(struct vme_resource *resource, int *enabled, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 421 | unsigned long long *vme_base, unsigned long long *size, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 422 | dma_addr_t *buf_base, u32 *aspace, u32 *cycle) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 423 | { |
| 424 | struct vme_bridge *bridge = find_bridge(resource); |
| 425 | struct vme_slave_resource *image; |
| 426 | |
| 427 | if (resource->type != VME_SLAVE) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 428 | printk(KERN_ERR "Not a slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 429 | return -EINVAL; |
| 430 | } |
| 431 | |
| 432 | image = list_entry(resource->entry, struct vme_slave_resource, list); |
| 433 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 434 | if (!bridge->slave_get) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 435 | printk(KERN_ERR "vme_slave_get not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 436 | return -EINVAL; |
| 437 | } |
| 438 | |
| 439 | return bridge->slave_get(image, enabled, vme_base, size, buf_base, |
| 440 | aspace, cycle); |
| 441 | } |
| 442 | EXPORT_SYMBOL(vme_slave_get); |
| 443 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 444 | /** |
| 445 | * vme_slave_free - Free VME slave window |
| 446 | * @resource: Pointer to VME slave resource. |
| 447 | * |
| 448 | * Free the provided slave resource so that it may be reallocated. |
| 449 | */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 450 | void vme_slave_free(struct vme_resource *resource) |
| 451 | { |
| 452 | struct vme_slave_resource *slave_image; |
| 453 | |
| 454 | if (resource->type != VME_SLAVE) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 455 | printk(KERN_ERR "Not a slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 456 | return; |
| 457 | } |
| 458 | |
| 459 | slave_image = list_entry(resource->entry, struct vme_slave_resource, |
| 460 | list); |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 461 | if (!slave_image) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 462 | printk(KERN_ERR "Can't find slave resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
| 466 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 467 | mutex_lock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 468 | if (slave_image->locked == 0) |
| 469 | printk(KERN_ERR "Image is already free\n"); |
| 470 | |
| 471 | slave_image->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 472 | mutex_unlock(&slave_image->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 473 | |
| 474 | /* Free up resource memory */ |
| 475 | kfree(resource); |
| 476 | } |
| 477 | EXPORT_SYMBOL(vme_slave_free); |
| 478 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 479 | /** |
| 480 | * vme_master_request - Request a VME master window resource. |
| 481 | * @vdev: Pointer to VME device struct vme_dev assigned to driver instance. |
| 482 | * @address: Required VME address space. |
| 483 | * @cycle: Required VME data transfer cycle type. |
| 484 | * @dwidth: Required VME data transfer width. |
| 485 | * |
| 486 | * Request use of a VME window resource capable of being set for the requested |
| 487 | * address space, data transfer cycle and width. |
| 488 | * |
| 489 | * Return: Pointer to VME resource on success, NULL on failure. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 490 | */ |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 491 | struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address, |
| 492 | u32 cycle, u32 dwidth) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 493 | { |
| 494 | struct vme_bridge *bridge; |
| 495 | struct list_head *master_pos = NULL; |
| 496 | struct vme_master_resource *allocated_image = NULL; |
| 497 | struct vme_master_resource *master_image = NULL; |
| 498 | struct vme_resource *resource = NULL; |
| 499 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 500 | bridge = vdev->bridge; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 501 | if (!bridge) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 502 | printk(KERN_ERR "Can't find VME bus\n"); |
| 503 | goto err_bus; |
| 504 | } |
| 505 | |
| 506 | /* Loop through master resources */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 507 | list_for_each(master_pos, &bridge->master_resources) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 508 | master_image = list_entry(master_pos, |
| 509 | struct vme_master_resource, list); |
| 510 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 511 | if (!master_image) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 512 | printk(KERN_WARNING "Registered NULL master resource\n"); |
| 513 | continue; |
| 514 | } |
| 515 | |
| 516 | /* Find an unlocked and compatible image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 517 | spin_lock(&master_image->lock); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 518 | if (((master_image->address_attr & address) == address) && |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 519 | ((master_image->cycle_attr & cycle) == cycle) && |
| 520 | ((master_image->width_attr & dwidth) == dwidth) && |
| 521 | (master_image->locked == 0)) { |
| 522 | |
| 523 | master_image->locked = 1; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 524 | spin_unlock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 525 | allocated_image = master_image; |
| 526 | break; |
| 527 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 528 | spin_unlock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* Check to see if we found a resource */ |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 532 | if (!allocated_image) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 533 | printk(KERN_ERR "Can't find a suitable resource\n"); |
| 534 | goto err_image; |
| 535 | } |
| 536 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 537 | resource = kmalloc(sizeof(*resource), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 538 | if (!resource) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 539 | goto err_alloc; |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 540 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 541 | resource->type = VME_MASTER; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 542 | resource->entry = &allocated_image->list; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 543 | |
| 544 | return resource; |
| 545 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 546 | err_alloc: |
| 547 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 548 | spin_lock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 549 | master_image->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 550 | spin_unlock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 551 | err_image: |
| 552 | err_bus: |
| 553 | return NULL; |
| 554 | } |
| 555 | EXPORT_SYMBOL(vme_master_request); |
| 556 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 557 | /** |
| 558 | * vme_master_set - Set VME master window configuration. |
| 559 | * @resource: Pointer to VME master resource. |
| 560 | * @enabled: State to which the window should be configured. |
| 561 | * @vme_base: Base address for the window. |
| 562 | * @size: Size of the VME window. |
| 563 | * @aspace: VME address space for the VME window. |
| 564 | * @cycle: VME data transfer cycle type for the VME window. |
| 565 | * @dwidth: VME data transfer width for the VME window. |
| 566 | * |
| 567 | * Set configuration for provided VME master window. |
| 568 | * |
| 569 | * Return: Zero on success, -EINVAL if operation is not supported on this |
| 570 | * device, if an invalid resource has been provided or invalid |
| 571 | * attributes are provided. Hardware specific errors may also be |
| 572 | * returned. |
| 573 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 574 | int vme_master_set(struct vme_resource *resource, int enabled, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 575 | unsigned long long vme_base, unsigned long long size, u32 aspace, |
| 576 | u32 cycle, u32 dwidth) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 577 | { |
| 578 | struct vme_bridge *bridge = find_bridge(resource); |
| 579 | struct vme_master_resource *image; |
| 580 | int retval; |
| 581 | |
| 582 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 583 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 584 | return -EINVAL; |
| 585 | } |
| 586 | |
| 587 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 588 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 589 | if (!bridge->master_set) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 590 | printk(KERN_WARNING "vme_master_set not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 591 | return -EINVAL; |
| 592 | } |
| 593 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 594 | if (!(((image->address_attr & aspace) == aspace) && |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 595 | ((image->cycle_attr & cycle) == cycle) && |
| 596 | ((image->width_attr & dwidth) == dwidth))) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 597 | printk(KERN_WARNING "Invalid attributes\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 598 | return -EINVAL; |
| 599 | } |
| 600 | |
| 601 | retval = vme_check_window(aspace, vme_base, size); |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 602 | if (retval) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 603 | return retval; |
| 604 | |
| 605 | return bridge->master_set(image, enabled, vme_base, size, aspace, |
| 606 | cycle, dwidth); |
| 607 | } |
| 608 | EXPORT_SYMBOL(vme_master_set); |
| 609 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 610 | /** |
| 611 | * vme_master_get - Retrieve VME master window configuration. |
| 612 | * @resource: Pointer to VME master resource. |
| 613 | * @enabled: Pointer to variable for storing state. |
| 614 | * @vme_base: Pointer to variable for storing window base address. |
| 615 | * @size: Pointer to variable for storing window size. |
| 616 | * @aspace: Pointer to variable for storing VME address space. |
| 617 | * @cycle: Pointer to variable for storing VME data transfer cycle type. |
| 618 | * @dwidth: Pointer to variable for storing VME data transfer width. |
| 619 | * |
| 620 | * Return configuration for provided VME master window. |
| 621 | * |
| 622 | * Return: Zero on success, -EINVAL if operation is not supported on this |
| 623 | * device or if an invalid resource has been provided. |
| 624 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 625 | int vme_master_get(struct vme_resource *resource, int *enabled, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 626 | unsigned long long *vme_base, unsigned long long *size, u32 *aspace, |
| 627 | u32 *cycle, u32 *dwidth) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 628 | { |
| 629 | struct vme_bridge *bridge = find_bridge(resource); |
| 630 | struct vme_master_resource *image; |
| 631 | |
| 632 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 633 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 634 | return -EINVAL; |
| 635 | } |
| 636 | |
| 637 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 638 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 639 | if (!bridge->master_get) { |
Julia Lawall | c103830 | 2014-12-07 20:20:53 +0100 | [diff] [blame] | 640 | printk(KERN_WARNING "%s not supported\n", __func__); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 641 | return -EINVAL; |
| 642 | } |
| 643 | |
| 644 | return bridge->master_get(image, enabled, vme_base, size, aspace, |
| 645 | cycle, dwidth); |
| 646 | } |
| 647 | EXPORT_SYMBOL(vme_master_get); |
| 648 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 649 | /** |
| 650 | * vme_master_write - Read data from VME space into a buffer. |
| 651 | * @resource: Pointer to VME master resource. |
| 652 | * @buf: Pointer to buffer where data should be transferred. |
| 653 | * @count: Number of bytes to transfer. |
| 654 | * @offset: Offset into VME master window at which to start transfer. |
| 655 | * |
| 656 | * Perform read of count bytes of data from location on VME bus which maps into |
| 657 | * the VME master window at offset to buf. |
| 658 | * |
| 659 | * Return: Number of bytes read, -EINVAL if resource is not a VME master |
| 660 | * resource or read operation is not supported. -EFAULT returned if |
| 661 | * invalid offset is provided. Hardware specific errors may also be |
| 662 | * returned. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 663 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 664 | 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] | 665 | loff_t offset) |
| 666 | { |
| 667 | struct vme_bridge *bridge = find_bridge(resource); |
| 668 | struct vme_master_resource *image; |
| 669 | size_t length; |
| 670 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 671 | if (!bridge->master_read) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 672 | printk(KERN_WARNING "Reading from resource not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 673 | return -EINVAL; |
| 674 | } |
| 675 | |
| 676 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 677 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 678 | return -EINVAL; |
| 679 | } |
| 680 | |
| 681 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 682 | |
| 683 | length = vme_get_size(resource); |
| 684 | |
| 685 | if (offset > length) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 686 | printk(KERN_WARNING "Invalid Offset\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 687 | return -EFAULT; |
| 688 | } |
| 689 | |
| 690 | if ((offset + count) > length) |
| 691 | count = length - offset; |
| 692 | |
| 693 | return bridge->master_read(image, buf, count, offset); |
| 694 | |
| 695 | } |
| 696 | EXPORT_SYMBOL(vme_master_read); |
| 697 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 698 | /** |
| 699 | * vme_master_write - Write data out to VME space from a buffer. |
| 700 | * @resource: Pointer to VME master resource. |
| 701 | * @buf: Pointer to buffer holding data to transfer. |
| 702 | * @count: Number of bytes to transfer. |
| 703 | * @offset: Offset into VME master window at which to start transfer. |
| 704 | * |
| 705 | * Perform write of count bytes of data from buf to location on VME bus which |
| 706 | * maps into the VME master window at offset. |
| 707 | * |
| 708 | * Return: Number of bytes written, -EINVAL if resource is not a VME master |
| 709 | * resource or write operation is not supported. -EFAULT returned if |
| 710 | * invalid offset is provided. Hardware specific errors may also be |
| 711 | * returned. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 712 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 713 | ssize_t vme_master_write(struct vme_resource *resource, void *buf, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 714 | size_t count, loff_t offset) |
| 715 | { |
| 716 | struct vme_bridge *bridge = find_bridge(resource); |
| 717 | struct vme_master_resource *image; |
| 718 | size_t length; |
| 719 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 720 | if (!bridge->master_write) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 721 | printk(KERN_WARNING "Writing to resource not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 722 | return -EINVAL; |
| 723 | } |
| 724 | |
| 725 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 726 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 727 | return -EINVAL; |
| 728 | } |
| 729 | |
| 730 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 731 | |
| 732 | length = vme_get_size(resource); |
| 733 | |
| 734 | if (offset > length) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 735 | printk(KERN_WARNING "Invalid Offset\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 736 | return -EFAULT; |
| 737 | } |
| 738 | |
| 739 | if ((offset + count) > length) |
| 740 | count = length - offset; |
| 741 | |
| 742 | return bridge->master_write(image, buf, count, offset); |
| 743 | } |
| 744 | EXPORT_SYMBOL(vme_master_write); |
| 745 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 746 | /** |
| 747 | * vme_master_rmw - Perform read-modify-write cycle. |
| 748 | * @resource: Pointer to VME master resource. |
| 749 | * @mask: Bits to be compared and swapped in operation. |
| 750 | * @compare: Bits to be compared with data read from offset. |
| 751 | * @swap: Bits to be swapped in data read from offset. |
| 752 | * @offset: Offset into VME master window at which to perform operation. |
| 753 | * |
| 754 | * Perform read-modify-write cycle on provided location: |
| 755 | * - Location on VME bus is read. |
| 756 | * - Bits selected by mask are compared with compare. |
| 757 | * - Where a selected bit matches that in compare and are selected in swap, |
| 758 | * the bit is swapped. |
| 759 | * - Result written back to location on VME bus. |
| 760 | * |
| 761 | * Return: Bytes written on success, -EINVAL if resource is not a VME master |
| 762 | * resource or RMW operation is not supported. Hardware specific |
| 763 | * errors may also be returned. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 764 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 765 | unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 766 | unsigned int compare, unsigned int swap, loff_t offset) |
| 767 | { |
| 768 | struct vme_bridge *bridge = find_bridge(resource); |
| 769 | struct vme_master_resource *image; |
| 770 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 771 | if (!bridge->master_rmw) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 772 | printk(KERN_WARNING "Writing to resource not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 773 | return -EINVAL; |
| 774 | } |
| 775 | |
| 776 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 777 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 778 | return -EINVAL; |
| 779 | } |
| 780 | |
| 781 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 782 | |
| 783 | return bridge->master_rmw(image, mask, compare, swap, offset); |
| 784 | } |
| 785 | EXPORT_SYMBOL(vme_master_rmw); |
| 786 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 787 | /** |
| 788 | * vme_master_mmap - Mmap region of VME master window. |
| 789 | * @resource: Pointer to VME master resource. |
| 790 | * @vma: Pointer to definition of user mapping. |
| 791 | * |
| 792 | * Memory map a region of the VME master window into user space. |
| 793 | * |
| 794 | * Return: Zero on success, -EINVAL if resource is not a VME master |
| 795 | * resource or -EFAULT if map exceeds window size. Other generic mmap |
| 796 | * errors may also be returned. |
| 797 | */ |
Dmitry Kalinkin | c74a804 | 2015-02-26 18:53:10 +0300 | [diff] [blame] | 798 | int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma) |
| 799 | { |
| 800 | struct vme_master_resource *image; |
| 801 | phys_addr_t phys_addr; |
| 802 | unsigned long vma_size; |
| 803 | |
| 804 | if (resource->type != VME_MASTER) { |
| 805 | pr_err("Not a master resource\n"); |
| 806 | return -EINVAL; |
| 807 | } |
| 808 | |
| 809 | image = list_entry(resource->entry, struct vme_master_resource, list); |
| 810 | phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT); |
| 811 | vma_size = vma->vm_end - vma->vm_start; |
| 812 | |
| 813 | if (phys_addr + vma_size > image->bus_resource.end + 1) { |
| 814 | pr_err("Map size cannot exceed the window size\n"); |
| 815 | return -EFAULT; |
| 816 | } |
| 817 | |
| 818 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 819 | |
| 820 | return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start); |
| 821 | } |
| 822 | EXPORT_SYMBOL(vme_master_mmap); |
| 823 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 824 | /** |
| 825 | * vme_master_free - Free VME master window |
| 826 | * @resource: Pointer to VME master resource. |
| 827 | * |
| 828 | * Free the provided master resource so that it may be reallocated. |
| 829 | */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 830 | void vme_master_free(struct vme_resource *resource) |
| 831 | { |
| 832 | struct vme_master_resource *master_image; |
| 833 | |
| 834 | if (resource->type != VME_MASTER) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 835 | printk(KERN_ERR "Not a master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 836 | return; |
| 837 | } |
| 838 | |
| 839 | master_image = list_entry(resource->entry, struct vme_master_resource, |
| 840 | list); |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 841 | if (!master_image) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 842 | printk(KERN_ERR "Can't find master resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 843 | return; |
| 844 | } |
| 845 | |
| 846 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 847 | spin_lock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 848 | if (master_image->locked == 0) |
| 849 | printk(KERN_ERR "Image is already free\n"); |
| 850 | |
| 851 | master_image->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 852 | spin_unlock(&master_image->lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 853 | |
| 854 | /* Free up resource memory */ |
| 855 | kfree(resource); |
| 856 | } |
| 857 | EXPORT_SYMBOL(vme_master_free); |
| 858 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 859 | /** |
| 860 | * vme_dma_request - Request a DMA controller. |
| 861 | * @vdev: Pointer to VME device struct vme_dev assigned to driver instance. |
| 862 | * @route: Required src/destination combination. |
| 863 | * |
| 864 | * Request a VME DMA controller with capability to perform transfers bewteen |
| 865 | * requested source/destination combination. |
| 866 | * |
| 867 | * Return: Pointer to VME DMA resource on success, NULL on failure. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 868 | */ |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 869 | struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 870 | { |
| 871 | struct vme_bridge *bridge; |
| 872 | struct list_head *dma_pos = NULL; |
| 873 | struct vme_dma_resource *allocated_ctrlr = NULL; |
| 874 | struct vme_dma_resource *dma_ctrlr = NULL; |
| 875 | struct vme_resource *resource = NULL; |
| 876 | |
| 877 | /* XXX Not checking resource attributes */ |
| 878 | printk(KERN_ERR "No VME resource Attribute tests done\n"); |
| 879 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 880 | bridge = vdev->bridge; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 881 | if (!bridge) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 882 | printk(KERN_ERR "Can't find VME bus\n"); |
| 883 | goto err_bus; |
| 884 | } |
| 885 | |
| 886 | /* Loop through DMA resources */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 887 | list_for_each(dma_pos, &bridge->dma_resources) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 888 | dma_ctrlr = list_entry(dma_pos, |
| 889 | struct vme_dma_resource, list); |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 890 | if (!dma_ctrlr) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 891 | printk(KERN_ERR "Registered NULL DMA resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 892 | continue; |
| 893 | } |
| 894 | |
Martyn Welch | 4f723df | 2010-02-18 15:12:58 +0000 | [diff] [blame] | 895 | /* Find an unlocked and compatible controller */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 896 | mutex_lock(&dma_ctrlr->mtx); |
Martyn Welch | 4f723df | 2010-02-18 15:12:58 +0000 | [diff] [blame] | 897 | if (((dma_ctrlr->route_attr & route) == route) && |
| 898 | (dma_ctrlr->locked == 0)) { |
| 899 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 900 | dma_ctrlr->locked = 1; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 901 | mutex_unlock(&dma_ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 902 | allocated_ctrlr = dma_ctrlr; |
| 903 | break; |
| 904 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 905 | mutex_unlock(&dma_ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* Check to see if we found a resource */ |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 909 | if (!allocated_ctrlr) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 910 | goto err_ctrlr; |
| 911 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 912 | resource = kmalloc(sizeof(*resource), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 913 | if (!resource) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 914 | goto err_alloc; |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 915 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 916 | resource->type = VME_DMA; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 917 | resource->entry = &allocated_ctrlr->list; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 918 | |
| 919 | return resource; |
| 920 | |
| 921 | err_alloc: |
| 922 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 923 | mutex_lock(&dma_ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 924 | dma_ctrlr->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 925 | mutex_unlock(&dma_ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 926 | err_ctrlr: |
| 927 | err_bus: |
| 928 | return NULL; |
| 929 | } |
Martyn Welch | 58e5079 | 2009-10-29 16:35:20 +0000 | [diff] [blame] | 930 | EXPORT_SYMBOL(vme_dma_request); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 931 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 932 | /** |
| 933 | * vme_new_dma_list - Create new VME DMA list. |
| 934 | * @resource: Pointer to VME DMA resource. |
| 935 | * |
| 936 | * Create a new VME DMA list. It is the responsibility of the user to free |
| 937 | * the list once it is no longer required with vme_dma_list_free(). |
| 938 | * |
| 939 | * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid |
| 940 | * VME DMA resource. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 941 | */ |
| 942 | struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource) |
| 943 | { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 944 | struct vme_dma_list *dma_list; |
| 945 | |
| 946 | if (resource->type != VME_DMA) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 947 | printk(KERN_ERR "Not a DMA resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 948 | return NULL; |
| 949 | } |
| 950 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 951 | dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 952 | if (!dma_list) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 953 | return NULL; |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 954 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 955 | INIT_LIST_HEAD(&dma_list->entries); |
Markus Elfring | a384b2c | 2017-08-24 22:04:45 +0200 | [diff] [blame] | 956 | dma_list->parent = list_entry(resource->entry, |
| 957 | struct vme_dma_resource, |
| 958 | list); |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 959 | mutex_init(&dma_list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 960 | |
| 961 | return dma_list; |
| 962 | } |
| 963 | EXPORT_SYMBOL(vme_new_dma_list); |
| 964 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 965 | /** |
| 966 | * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute. |
| 967 | * @pattern: Value to use used as pattern |
| 968 | * @type: Type of pattern to be written. |
| 969 | * |
| 970 | * Create VME DMA list attribute for pattern generation. It is the |
| 971 | * responsibility of the user to free used attributes using |
| 972 | * vme_dma_free_attribute(). |
| 973 | * |
| 974 | * Return: Pointer to VME DMA attribute, NULL on failure. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 975 | */ |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 976 | struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 977 | { |
| 978 | struct vme_dma_attr *attributes; |
| 979 | struct vme_dma_pattern *pattern_attr; |
| 980 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 981 | attributes = kmalloc(sizeof(*attributes), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 982 | if (!attributes) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 983 | goto err_attr; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 984 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 985 | pattern_attr = kmalloc(sizeof(*pattern_attr), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 986 | if (!pattern_attr) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 987 | goto err_pat; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 988 | |
| 989 | attributes->type = VME_DMA_PATTERN; |
| 990 | attributes->private = (void *)pattern_attr; |
| 991 | |
| 992 | pattern_attr->pattern = pattern; |
| 993 | pattern_attr->type = type; |
| 994 | |
| 995 | return attributes; |
| 996 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 997 | err_pat: |
| 998 | kfree(attributes); |
| 999 | err_attr: |
| 1000 | return NULL; |
| 1001 | } |
| 1002 | EXPORT_SYMBOL(vme_dma_pattern_attribute); |
| 1003 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1004 | /** |
| 1005 | * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute. |
| 1006 | * @address: PCI base address for DMA transfer. |
| 1007 | * |
| 1008 | * Create VME DMA list attribute pointing to a location on PCI for DMA |
| 1009 | * transfers. It is the responsibility of the user to free used attributes |
| 1010 | * using vme_dma_free_attribute(). |
| 1011 | * |
| 1012 | * Return: Pointer to VME DMA attribute, NULL on failure. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1013 | */ |
| 1014 | struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address) |
| 1015 | { |
| 1016 | struct vme_dma_attr *attributes; |
| 1017 | struct vme_dma_pci *pci_attr; |
| 1018 | |
| 1019 | /* XXX Run some sanity checks here */ |
| 1020 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 1021 | attributes = kmalloc(sizeof(*attributes), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 1022 | if (!attributes) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1023 | goto err_attr; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1024 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 1025 | pci_attr = kmalloc(sizeof(*pci_attr), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 1026 | if (!pci_attr) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1027 | goto err_pci; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1028 | |
| 1029 | attributes->type = VME_DMA_PCI; |
| 1030 | attributes->private = (void *)pci_attr; |
| 1031 | |
| 1032 | pci_attr->address = address; |
| 1033 | |
| 1034 | return attributes; |
| 1035 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1036 | err_pci: |
| 1037 | kfree(attributes); |
| 1038 | err_attr: |
| 1039 | return NULL; |
| 1040 | } |
| 1041 | EXPORT_SYMBOL(vme_dma_pci_attribute); |
| 1042 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1043 | /** |
| 1044 | * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute. |
| 1045 | * @address: VME base address for DMA transfer. |
| 1046 | * @aspace: VME address space to use for DMA transfer. |
| 1047 | * @cycle: VME bus cycle to use for DMA transfer. |
| 1048 | * @dwidth: VME data width to use for DMA transfer. |
| 1049 | * |
| 1050 | * Create VME DMA list attribute pointing to a location on the VME bus for DMA |
| 1051 | * transfers. It is the responsibility of the user to free used attributes |
| 1052 | * using vme_dma_free_attribute(). |
| 1053 | * |
| 1054 | * Return: Pointer to VME DMA attribute, NULL on failure. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1055 | */ |
| 1056 | struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address, |
Martyn Welch | 6af04b0 | 2011-12-01 17:06:29 +0000 | [diff] [blame] | 1057 | u32 aspace, u32 cycle, u32 dwidth) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1058 | { |
| 1059 | struct vme_dma_attr *attributes; |
| 1060 | struct vme_dma_vme *vme_attr; |
| 1061 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 1062 | attributes = kmalloc(sizeof(*attributes), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 1063 | if (!attributes) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1064 | goto err_attr; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1065 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 1066 | vme_attr = kmalloc(sizeof(*vme_attr), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 1067 | if (!vme_attr) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1068 | goto err_vme; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1069 | |
| 1070 | attributes->type = VME_DMA_VME; |
| 1071 | attributes->private = (void *)vme_attr; |
| 1072 | |
| 1073 | vme_attr->address = address; |
| 1074 | vme_attr->aspace = aspace; |
| 1075 | vme_attr->cycle = cycle; |
| 1076 | vme_attr->dwidth = dwidth; |
| 1077 | |
| 1078 | return attributes; |
| 1079 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1080 | err_vme: |
| 1081 | kfree(attributes); |
| 1082 | err_attr: |
| 1083 | return NULL; |
| 1084 | } |
| 1085 | EXPORT_SYMBOL(vme_dma_vme_attribute); |
| 1086 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1087 | /** |
| 1088 | * vme_dma_free_attribute - Free DMA list attribute. |
| 1089 | * @attributes: Pointer to DMA list attribute. |
| 1090 | * |
| 1091 | * Free VME DMA list attribute. VME DMA list attributes can be safely freed |
| 1092 | * once vme_dma_list_add() has returned. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1093 | */ |
| 1094 | void vme_dma_free_attribute(struct vme_dma_attr *attributes) |
| 1095 | { |
| 1096 | kfree(attributes->private); |
| 1097 | kfree(attributes); |
| 1098 | } |
| 1099 | EXPORT_SYMBOL(vme_dma_free_attribute); |
| 1100 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1101 | /** |
| 1102 | * vme_dma_list_add - Add enty to a VME DMA list. |
| 1103 | * @list: Pointer to VME list. |
| 1104 | * @src: Pointer to DMA list attribute to use as source. |
| 1105 | * @dest: Pointer to DMA list attribute to use as destination. |
| 1106 | * @count: Number of bytes to transfer. |
| 1107 | * |
| 1108 | * Add an entry to the provided VME DMA list. Entry requires pointers to source |
| 1109 | * and destination DMA attributes and a count. |
| 1110 | * |
| 1111 | * Please note, the attributes supported as source and destinations for |
| 1112 | * transfers are hardware dependent. |
| 1113 | * |
| 1114 | * Return: Zero on success, -EINVAL if operation is not supported on this |
| 1115 | * device or if the link list has already been submitted for execution. |
| 1116 | * Hardware specific errors also possible. |
| 1117 | */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1118 | int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src, |
| 1119 | struct vme_dma_attr *dest, size_t count) |
| 1120 | { |
| 1121 | struct vme_bridge *bridge = list->parent->parent; |
| 1122 | int retval; |
| 1123 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1124 | if (!bridge->dma_list_add) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1125 | printk(KERN_WARNING "Link List DMA generation not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1126 | return -EINVAL; |
| 1127 | } |
| 1128 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1129 | if (!mutex_trylock(&list->mtx)) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1130 | printk(KERN_ERR "Link List already submitted\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1131 | return -EINVAL; |
| 1132 | } |
| 1133 | |
| 1134 | retval = bridge->dma_list_add(list, src, dest, count); |
| 1135 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1136 | mutex_unlock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1137 | |
| 1138 | return retval; |
| 1139 | } |
| 1140 | EXPORT_SYMBOL(vme_dma_list_add); |
| 1141 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1142 | /** |
| 1143 | * vme_dma_list_exec - Queue a VME DMA list for execution. |
| 1144 | * @list: Pointer to VME list. |
| 1145 | * |
| 1146 | * Queue the provided VME DMA list for execution. The call will return once the |
| 1147 | * list has been executed. |
| 1148 | * |
| 1149 | * Return: Zero on success, -EINVAL if operation is not supported on this |
| 1150 | * device. Hardware specific errors also possible. |
| 1151 | */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1152 | int vme_dma_list_exec(struct vme_dma_list *list) |
| 1153 | { |
| 1154 | struct vme_bridge *bridge = list->parent->parent; |
| 1155 | int retval; |
| 1156 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1157 | if (!bridge->dma_list_exec) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1158 | printk(KERN_ERR "Link List DMA execution not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1159 | return -EINVAL; |
| 1160 | } |
| 1161 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1162 | mutex_lock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1163 | |
| 1164 | retval = bridge->dma_list_exec(list); |
| 1165 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1166 | mutex_unlock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1167 | |
| 1168 | return retval; |
| 1169 | } |
| 1170 | EXPORT_SYMBOL(vme_dma_list_exec); |
| 1171 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1172 | /** |
| 1173 | * vme_dma_list_free - Free a VME DMA list. |
| 1174 | * @list: Pointer to VME list. |
| 1175 | * |
| 1176 | * Free the provided DMA list and all its entries. |
| 1177 | * |
| 1178 | * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource |
| 1179 | * is still in use. Hardware specific errors also possible. |
| 1180 | */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1181 | int vme_dma_list_free(struct vme_dma_list *list) |
| 1182 | { |
| 1183 | struct vme_bridge *bridge = list->parent->parent; |
| 1184 | int retval; |
| 1185 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1186 | if (!bridge->dma_list_empty) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1187 | printk(KERN_WARNING "Emptying of Link Lists not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1188 | return -EINVAL; |
| 1189 | } |
| 1190 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1191 | if (!mutex_trylock(&list->mtx)) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1192 | printk(KERN_ERR "Link List in use\n"); |
Martyn Welch | f13d1a8 | 2017-10-24 08:27:40 +0200 | [diff] [blame] | 1193 | return -EBUSY; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /* |
Aaron Sierra | f56c3d4 | 2016-04-21 11:18:22 -0500 | [diff] [blame] | 1197 | * Empty out all of the entries from the DMA list. We need to go to the |
| 1198 | * low level driver as DMA entries are driver specific. |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1199 | */ |
| 1200 | retval = bridge->dma_list_empty(list); |
| 1201 | if (retval) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1202 | printk(KERN_ERR "Unable to empty link-list entries\n"); |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1203 | mutex_unlock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1204 | return retval; |
| 1205 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1206 | mutex_unlock(&list->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1207 | kfree(list); |
| 1208 | |
| 1209 | return retval; |
| 1210 | } |
| 1211 | EXPORT_SYMBOL(vme_dma_list_free); |
| 1212 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1213 | /** |
| 1214 | * vme_dma_free - Free a VME DMA resource. |
| 1215 | * @resource: Pointer to VME DMA resource. |
| 1216 | * |
| 1217 | * Free the provided DMA resource so that it may be reallocated. |
| 1218 | * |
| 1219 | * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource |
| 1220 | * is still active. |
| 1221 | */ |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1222 | int vme_dma_free(struct vme_resource *resource) |
| 1223 | { |
| 1224 | struct vme_dma_resource *ctrlr; |
| 1225 | |
| 1226 | if (resource->type != VME_DMA) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1227 | printk(KERN_ERR "Not a DMA resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1228 | return -EINVAL; |
| 1229 | } |
| 1230 | |
| 1231 | ctrlr = list_entry(resource->entry, struct vme_dma_resource, list); |
| 1232 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1233 | if (!mutex_trylock(&ctrlr->mtx)) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1234 | printk(KERN_ERR "Resource busy, can't free\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1235 | return -EBUSY; |
| 1236 | } |
| 1237 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1238 | if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1239 | printk(KERN_WARNING "Resource still processing transfers\n"); |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1240 | mutex_unlock(&ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1241 | return -EBUSY; |
| 1242 | } |
| 1243 | |
| 1244 | ctrlr->locked = 0; |
| 1245 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1246 | mutex_unlock(&ctrlr->mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1247 | |
Martyn Welch | fd5c256 | 2013-06-06 12:29:16 +0100 | [diff] [blame] | 1248 | kfree(resource); |
| 1249 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
| 1252 | EXPORT_SYMBOL(vme_dma_free); |
| 1253 | |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1254 | void vme_bus_error_handler(struct vme_bridge *bridge, |
Dmitry Kalinkin | 472f16f | 2015-09-18 02:01:43 +0300 | [diff] [blame] | 1255 | unsigned long long address, int am) |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1256 | { |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1257 | struct list_head *handler_pos = NULL; |
| 1258 | struct vme_error_handler *handler; |
Dmitry Kalinkin | 448535a | 2015-09-18 02:01:45 +0300 | [diff] [blame] | 1259 | int handler_triggered = 0; |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1260 | u32 aspace = vme_get_aspace(am); |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1261 | |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1262 | list_for_each(handler_pos, &bridge->vme_error_handlers) { |
| 1263 | handler = list_entry(handler_pos, struct vme_error_handler, |
| 1264 | list); |
| 1265 | if ((aspace == handler->aspace) && |
| 1266 | (address >= handler->start) && |
| 1267 | (address < handler->end)) { |
| 1268 | if (!handler->num_errors) |
| 1269 | handler->first_error = address; |
| 1270 | if (handler->num_errors != UINT_MAX) |
| 1271 | handler->num_errors++; |
Dmitry Kalinkin | 448535a | 2015-09-18 02:01:45 +0300 | [diff] [blame] | 1272 | handler_triggered = 1; |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1273 | } |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1274 | } |
Dmitry Kalinkin | 448535a | 2015-09-18 02:01:45 +0300 | [diff] [blame] | 1275 | |
| 1276 | if (!handler_triggered) |
| 1277 | dev_err(bridge->parent, |
| 1278 | "Unhandled VME access error at address 0x%llx\n", |
| 1279 | address); |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1280 | } |
| 1281 | EXPORT_SYMBOL(vme_bus_error_handler); |
| 1282 | |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1283 | struct vme_error_handler *vme_register_error_handler( |
| 1284 | struct vme_bridge *bridge, u32 aspace, |
| 1285 | unsigned long long address, size_t len) |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1286 | { |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1287 | struct vme_error_handler *handler; |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1288 | |
Jia-Ju Bai | 9778461 | 2017-12-12 20:51:53 +0800 | [diff] [blame] | 1289 | handler = kmalloc(sizeof(*handler), GFP_ATOMIC); |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1290 | if (!handler) |
| 1291 | return NULL; |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1292 | |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1293 | handler->aspace = aspace; |
| 1294 | handler->start = address; |
| 1295 | handler->end = address + len; |
| 1296 | handler->num_errors = 0; |
| 1297 | handler->first_error = 0; |
| 1298 | list_add_tail(&handler->list, &bridge->vme_error_handlers); |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1299 | |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1300 | return handler; |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1301 | } |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1302 | EXPORT_SYMBOL(vme_register_error_handler); |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1303 | |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1304 | void vme_unregister_error_handler(struct vme_error_handler *handler) |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1305 | { |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1306 | list_del(&handler->list); |
| 1307 | kfree(handler); |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1308 | } |
Dmitry Kalinkin | 0b04966 | 2015-09-18 02:01:44 +0300 | [diff] [blame] | 1309 | EXPORT_SYMBOL(vme_unregister_error_handler); |
Dmitry Kalinkin | e2c6393 | 2015-09-18 02:01:42 +0300 | [diff] [blame] | 1310 | |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1311 | void vme_irq_handler(struct vme_bridge *bridge, int level, int statid) |
| 1312 | { |
| 1313 | void (*call)(int, int, void *); |
| 1314 | void *priv_data; |
| 1315 | |
| 1316 | call = bridge->irq[level - 1].callback[statid].func; |
| 1317 | priv_data = bridge->irq[level - 1].callback[statid].priv_data; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1318 | if (call) |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1319 | call(level, statid, priv_data); |
| 1320 | else |
Aaron Sierra | f56c3d4 | 2016-04-21 11:18:22 -0500 | [diff] [blame] | 1321 | printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n", |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 1322 | level, statid); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1323 | } |
| 1324 | EXPORT_SYMBOL(vme_irq_handler); |
| 1325 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1326 | /** |
| 1327 | * vme_irq_request - Request a specific VME interrupt. |
| 1328 | * @vdev: Pointer to VME device struct vme_dev assigned to driver instance. |
| 1329 | * @level: Interrupt priority being requested. |
| 1330 | * @statid: Interrupt vector being requested. |
| 1331 | * @callback: Pointer to callback function called when VME interrupt/vector |
| 1332 | * received. |
| 1333 | * @priv_data: Generic pointer that will be passed to the callback function. |
| 1334 | * |
| 1335 | * Request callback to be attached as a handler for VME interrupts with provided |
| 1336 | * level and statid. |
| 1337 | * |
| 1338 | * Return: Zero on success, -EINVAL on invalid vme device, level or if the |
| 1339 | * function is not supported, -EBUSY if the level/statid combination is |
| 1340 | * already in use. Hardware specific errors also possible. |
| 1341 | */ |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1342 | int vme_irq_request(struct vme_dev *vdev, int level, int statid, |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1343 | void (*callback)(int, int, void *), |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1344 | void *priv_data) |
| 1345 | { |
| 1346 | struct vme_bridge *bridge; |
| 1347 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1348 | bridge = vdev->bridge; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1349 | if (!bridge) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1350 | printk(KERN_ERR "Can't find VME bus\n"); |
| 1351 | return -EINVAL; |
| 1352 | } |
| 1353 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1354 | if ((level < 1) || (level > 7)) { |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1355 | printk(KERN_ERR "Invalid interrupt level\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1356 | return -EINVAL; |
| 1357 | } |
| 1358 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1359 | if (!bridge->irq_set) { |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1360 | printk(KERN_ERR "Configuring interrupts not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1361 | return -EINVAL; |
| 1362 | } |
| 1363 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1364 | mutex_lock(&bridge->irq_mtx); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1365 | |
| 1366 | if (bridge->irq[level - 1].callback[statid].func) { |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1367 | mutex_unlock(&bridge->irq_mtx); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1368 | printk(KERN_WARNING "VME Interrupt already taken\n"); |
| 1369 | return -EBUSY; |
| 1370 | } |
| 1371 | |
| 1372 | bridge->irq[level - 1].count++; |
| 1373 | bridge->irq[level - 1].callback[statid].priv_data = priv_data; |
| 1374 | bridge->irq[level - 1].callback[statid].func = callback; |
| 1375 | |
| 1376 | /* Enable IRQ level */ |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1377 | bridge->irq_set(bridge, level, 1, 1); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1378 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1379 | mutex_unlock(&bridge->irq_mtx); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1380 | |
| 1381 | return 0; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1382 | } |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1383 | EXPORT_SYMBOL(vme_irq_request); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1384 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1385 | /** |
| 1386 | * vme_irq_free - Free a VME interrupt. |
| 1387 | * @vdev: Pointer to VME device struct vme_dev assigned to driver instance. |
| 1388 | * @level: Interrupt priority of interrupt being freed. |
| 1389 | * @statid: Interrupt vector of interrupt being freed. |
| 1390 | * |
| 1391 | * Remove previously attached callback from VME interrupt priority/vector. |
| 1392 | */ |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1393 | void vme_irq_free(struct vme_dev *vdev, int level, int statid) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1394 | { |
| 1395 | struct vme_bridge *bridge; |
| 1396 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1397 | bridge = vdev->bridge; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1398 | if (!bridge) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1399 | printk(KERN_ERR "Can't find VME bus\n"); |
| 1400 | return; |
| 1401 | } |
| 1402 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1403 | if ((level < 1) || (level > 7)) { |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1404 | printk(KERN_ERR "Invalid interrupt level\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1405 | return; |
| 1406 | } |
| 1407 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1408 | if (!bridge->irq_set) { |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1409 | printk(KERN_ERR "Configuring interrupts not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1410 | return; |
| 1411 | } |
| 1412 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1413 | mutex_lock(&bridge->irq_mtx); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1414 | |
| 1415 | bridge->irq[level - 1].count--; |
| 1416 | |
| 1417 | /* Disable IRQ level if no more interrupts attached at this level*/ |
| 1418 | if (bridge->irq[level - 1].count == 0) |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1419 | bridge->irq_set(bridge, level, 0, 1); |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1420 | |
| 1421 | bridge->irq[level - 1].callback[statid].func = NULL; |
| 1422 | bridge->irq[level - 1].callback[statid].priv_data = NULL; |
| 1423 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1424 | mutex_unlock(&bridge->irq_mtx); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1425 | } |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1426 | EXPORT_SYMBOL(vme_irq_free); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1427 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1428 | /** |
| 1429 | * vme_irq_generate - Generate VME interrupt. |
| 1430 | * @vdev: Pointer to VME device struct vme_dev assigned to driver instance. |
| 1431 | * @level: Interrupt priority at which to assert the interrupt. |
| 1432 | * @statid: Interrupt vector to associate with the interrupt. |
| 1433 | * |
| 1434 | * Generate a VME interrupt of the provided level and with the provided |
| 1435 | * statid. |
| 1436 | * |
| 1437 | * Return: Zero on success, -EINVAL on invalid vme device, level or if the |
| 1438 | * function is not supported. Hardware specific errors also possible. |
| 1439 | */ |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1440 | int vme_irq_generate(struct vme_dev *vdev, int level, int statid) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1441 | { |
| 1442 | struct vme_bridge *bridge; |
| 1443 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1444 | bridge = vdev->bridge; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1445 | if (!bridge) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1446 | printk(KERN_ERR "Can't find VME bus\n"); |
| 1447 | return -EINVAL; |
| 1448 | } |
| 1449 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1450 | if ((level < 1) || (level > 7)) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1451 | printk(KERN_WARNING "Invalid interrupt level\n"); |
| 1452 | return -EINVAL; |
| 1453 | } |
| 1454 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1455 | if (!bridge->irq_generate) { |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1456 | printk(KERN_WARNING "Interrupt generation not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1457 | return -EINVAL; |
| 1458 | } |
| 1459 | |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1460 | return bridge->irq_generate(bridge, level, statid); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1461 | } |
Martyn Welch | c813f59 | 2009-10-29 16:34:54 +0000 | [diff] [blame] | 1462 | EXPORT_SYMBOL(vme_irq_generate); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1463 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1464 | /** |
| 1465 | * vme_lm_request - Request a VME location monitor |
| 1466 | * @vdev: Pointer to VME device struct vme_dev assigned to driver instance. |
| 1467 | * |
| 1468 | * Allocate a location monitor resource to the driver. A location monitor |
| 1469 | * allows the driver to monitor accesses to a contiguous number of |
| 1470 | * addresses on the VME bus. |
| 1471 | * |
| 1472 | * Return: Pointer to a VME resource on success or NULL on failure. |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1473 | */ |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1474 | struct vme_resource *vme_lm_request(struct vme_dev *vdev) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1475 | { |
| 1476 | struct vme_bridge *bridge; |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1477 | struct list_head *lm_pos = NULL; |
| 1478 | struct vme_lm_resource *allocated_lm = NULL; |
| 1479 | struct vme_lm_resource *lm = NULL; |
| 1480 | struct vme_resource *resource = NULL; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1481 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1482 | bridge = vdev->bridge; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1483 | if (!bridge) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1484 | printk(KERN_ERR "Can't find VME bus\n"); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1485 | goto err_bus; |
| 1486 | } |
| 1487 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1488 | /* Loop through LM resources */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1489 | list_for_each(lm_pos, &bridge->lm_resources) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1490 | lm = list_entry(lm_pos, |
| 1491 | struct vme_lm_resource, list); |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1492 | if (!lm) { |
Greg Kroah-Hartman | 25958ce | 2012-04-25 11:25:46 -0700 | [diff] [blame] | 1493 | printk(KERN_ERR "Registered NULL Location Monitor resource\n"); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1494 | continue; |
| 1495 | } |
| 1496 | |
| 1497 | /* Find an unlocked controller */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1498 | mutex_lock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1499 | if (lm->locked == 0) { |
| 1500 | lm->locked = 1; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1501 | mutex_unlock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1502 | allocated_lm = lm; |
| 1503 | break; |
| 1504 | } |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1505 | mutex_unlock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | /* Check to see if we found a resource */ |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1509 | if (!allocated_lm) |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1510 | goto err_lm; |
| 1511 | |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 1512 | resource = kmalloc(sizeof(*resource), GFP_KERNEL); |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 1513 | if (!resource) |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1514 | goto err_alloc; |
Markus Elfring | 94eefcc | 2017-08-24 21:38:20 +0200 | [diff] [blame] | 1515 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1516 | resource->type = VME_LM; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1517 | resource->entry = &allocated_lm->list; |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1518 | |
| 1519 | return resource; |
| 1520 | |
| 1521 | err_alloc: |
| 1522 | /* Unlock image */ |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1523 | mutex_lock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1524 | lm->locked = 0; |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1525 | mutex_unlock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1526 | err_lm: |
| 1527 | err_bus: |
| 1528 | return NULL; |
| 1529 | } |
| 1530 | EXPORT_SYMBOL(vme_lm_request); |
| 1531 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1532 | /** |
| 1533 | * vme_lm_count - Determine number of VME Addresses monitored |
| 1534 | * @resource: Pointer to VME location monitor resource. |
| 1535 | * |
| 1536 | * The number of contiguous addresses monitored is hardware dependent. |
| 1537 | * Return the number of contiguous addresses monitored by the |
| 1538 | * location monitor. |
| 1539 | * |
| 1540 | * Return: Count of addresses monitored or -EINVAL when provided with an |
| 1541 | * invalid location monitor resource. |
| 1542 | */ |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1543 | int vme_lm_count(struct vme_resource *resource) |
| 1544 | { |
| 1545 | struct vme_lm_resource *lm; |
| 1546 | |
| 1547 | if (resource->type != VME_LM) { |
| 1548 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1549 | return -EINVAL; |
| 1550 | } |
| 1551 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1552 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1553 | |
| 1554 | return lm->monitors; |
| 1555 | } |
| 1556 | EXPORT_SYMBOL(vme_lm_count); |
| 1557 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1558 | /** |
| 1559 | * vme_lm_set - Configure location monitor |
| 1560 | * @resource: Pointer to VME location monitor resource. |
| 1561 | * @lm_base: Base address to monitor. |
| 1562 | * @aspace: VME address space to monitor. |
| 1563 | * @cycle: VME bus cycle type to monitor. |
| 1564 | * |
| 1565 | * Set the base address, address space and cycle type of accesses to be |
| 1566 | * monitored by the location monitor. |
| 1567 | * |
| 1568 | * Return: Zero on success, -EINVAL when provided with an invalid location |
| 1569 | * monitor resource or function is not supported. Hardware specific |
| 1570 | * errors may also be returned. |
| 1571 | */ |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1572 | 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] | 1573 | u32 aspace, u32 cycle) |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1574 | { |
| 1575 | struct vme_bridge *bridge = find_bridge(resource); |
| 1576 | struct vme_lm_resource *lm; |
| 1577 | |
| 1578 | if (resource->type != VME_LM) { |
| 1579 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
| 1580 | return -EINVAL; |
| 1581 | } |
| 1582 | |
| 1583 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1584 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1585 | if (!bridge->lm_set) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1586 | printk(KERN_ERR "vme_lm_set not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1587 | return -EINVAL; |
| 1588 | } |
| 1589 | |
Martyn Welch | 8be9226 | 2009-10-29 16:35:08 +0000 | [diff] [blame] | 1590 | return bridge->lm_set(lm, lm_base, aspace, cycle); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1591 | } |
| 1592 | EXPORT_SYMBOL(vme_lm_set); |
| 1593 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1594 | /** |
| 1595 | * vme_lm_get - Retrieve location monitor settings |
| 1596 | * @resource: Pointer to VME location monitor resource. |
| 1597 | * @lm_base: Pointer used to output the base address monitored. |
| 1598 | * @aspace: Pointer used to output the address space monitored. |
| 1599 | * @cycle: Pointer used to output the VME bus cycle type monitored. |
| 1600 | * |
| 1601 | * Retrieve the base address, address space and cycle type of accesses to |
| 1602 | * be monitored by the location monitor. |
| 1603 | * |
| 1604 | * Return: Zero on success, -EINVAL when provided with an invalid location |
| 1605 | * monitor resource or function is not supported. Hardware specific |
| 1606 | * errors may also be returned. |
| 1607 | */ |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1608 | 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] | 1609 | u32 *aspace, u32 *cycle) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1610 | { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1611 | struct vme_bridge *bridge = find_bridge(resource); |
| 1612 | struct vme_lm_resource *lm; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1613 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1614 | if (resource->type != VME_LM) { |
| 1615 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1616 | return -EINVAL; |
| 1617 | } |
| 1618 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1619 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1620 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1621 | if (!bridge->lm_get) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1622 | printk(KERN_ERR "vme_lm_get not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1623 | return -EINVAL; |
| 1624 | } |
| 1625 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1626 | return bridge->lm_get(lm, lm_base, aspace, cycle); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1627 | } |
| 1628 | EXPORT_SYMBOL(vme_lm_get); |
| 1629 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1630 | /** |
| 1631 | * vme_lm_attach - Provide callback for location monitor address |
| 1632 | * @resource: Pointer to VME location monitor resource. |
| 1633 | * @monitor: Offset to which callback should be attached. |
| 1634 | * @callback: Pointer to callback function called when triggered. |
| 1635 | * @data: Generic pointer that will be passed to the callback function. |
| 1636 | * |
| 1637 | * Attach a callback to the specificed offset into the location monitors |
| 1638 | * monitored addresses. A generic pointer is provided to allow data to be |
| 1639 | * passed to the callback when called. |
| 1640 | * |
| 1641 | * Return: Zero on success, -EINVAL when provided with an invalid location |
| 1642 | * monitor resource or function is not supported. Hardware specific |
| 1643 | * errors may also be returned. |
| 1644 | */ |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1645 | int vme_lm_attach(struct vme_resource *resource, int monitor, |
Aaron Sierra | fa54b32 | 2016-04-29 16:41:02 -0500 | [diff] [blame] | 1646 | void (*callback)(void *), void *data) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1647 | { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1648 | struct vme_bridge *bridge = find_bridge(resource); |
| 1649 | struct vme_lm_resource *lm; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1650 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1651 | if (resource->type != VME_LM) { |
| 1652 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1653 | return -EINVAL; |
| 1654 | } |
| 1655 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1656 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1657 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1658 | if (!bridge->lm_attach) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1659 | printk(KERN_ERR "vme_lm_attach not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1660 | return -EINVAL; |
| 1661 | } |
| 1662 | |
Aaron Sierra | fa54b32 | 2016-04-29 16:41:02 -0500 | [diff] [blame] | 1663 | return bridge->lm_attach(lm, monitor, callback, data); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1664 | } |
| 1665 | EXPORT_SYMBOL(vme_lm_attach); |
| 1666 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1667 | /** |
| 1668 | * vme_lm_detach - Remove callback for location monitor address |
| 1669 | * @resource: Pointer to VME location monitor resource. |
| 1670 | * @monitor: Offset to which callback should be removed. |
| 1671 | * |
| 1672 | * Remove the callback associated with the specificed offset into the |
| 1673 | * location monitors monitored addresses. |
| 1674 | * |
| 1675 | * Return: Zero on success, -EINVAL when provided with an invalid location |
| 1676 | * monitor resource or function is not supported. Hardware specific |
| 1677 | * errors may also be returned. |
| 1678 | */ |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1679 | int vme_lm_detach(struct vme_resource *resource, int monitor) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1680 | { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1681 | struct vme_bridge *bridge = find_bridge(resource); |
| 1682 | struct vme_lm_resource *lm; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1683 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1684 | if (resource->type != VME_LM) { |
| 1685 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1686 | return -EINVAL; |
| 1687 | } |
| 1688 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1689 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1690 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1691 | if (!bridge->lm_detach) { |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1692 | printk(KERN_ERR "vme_lm_detach not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1693 | return -EINVAL; |
| 1694 | } |
| 1695 | |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1696 | return bridge->lm_detach(lm, monitor); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1697 | } |
| 1698 | EXPORT_SYMBOL(vme_lm_detach); |
| 1699 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1700 | /** |
| 1701 | * vme_lm_free - Free allocated VME location monitor |
| 1702 | * @resource: Pointer to VME location monitor resource. |
| 1703 | * |
| 1704 | * Free allocation of a VME location monitor. |
| 1705 | * |
| 1706 | * WARNING: This function currently expects that any callbacks that have |
| 1707 | * been attached to the location monitor have been removed. |
| 1708 | * |
| 1709 | * Return: Zero on success, -EINVAL when provided with an invalid location |
| 1710 | * monitor resource. |
| 1711 | */ |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1712 | void vme_lm_free(struct vme_resource *resource) |
| 1713 | { |
| 1714 | struct vme_lm_resource *lm; |
| 1715 | |
| 1716 | if (resource->type != VME_LM) { |
| 1717 | printk(KERN_ERR "Not a Location Monitor resource\n"); |
| 1718 | return; |
| 1719 | } |
| 1720 | |
| 1721 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
| 1722 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1723 | mutex_lock(&lm->mtx); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1724 | |
Martyn Welch | 8be9226 | 2009-10-29 16:35:08 +0000 | [diff] [blame] | 1725 | /* XXX |
| 1726 | * Check to see that there aren't any callbacks still attached, if |
| 1727 | * there are we should probably be detaching them! |
| 1728 | */ |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1729 | |
| 1730 | lm->locked = 0; |
| 1731 | |
Emilio G. Cota | 886953e | 2010-11-12 11:14:07 +0000 | [diff] [blame] | 1732 | mutex_unlock(&lm->mtx); |
Martyn Welch | 8be9226 | 2009-10-29 16:35:08 +0000 | [diff] [blame] | 1733 | |
| 1734 | kfree(resource); |
Martyn Welch | 42fb503 | 2009-08-11 17:44:56 +0100 | [diff] [blame] | 1735 | } |
| 1736 | EXPORT_SYMBOL(vme_lm_free); |
| 1737 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1738 | /** |
| 1739 | * vme_slot_num - Retrieve slot ID |
| 1740 | * @vdev: Pointer to VME device struct vme_dev assigned to driver instance. |
| 1741 | * |
| 1742 | * Retrieve the slot ID associated with the provided VME device. |
| 1743 | * |
| 1744 | * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined |
| 1745 | * or the function is not supported. Hardware specific errors may also |
| 1746 | * be returned. |
| 1747 | */ |
Martyn Welch | d7729f0 | 2013-11-08 11:58:35 +0000 | [diff] [blame] | 1748 | int vme_slot_num(struct vme_dev *vdev) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1749 | { |
| 1750 | struct vme_bridge *bridge; |
| 1751 | |
Manohar Vanga | 8f966dc | 2011-09-26 11:27:15 +0200 | [diff] [blame] | 1752 | bridge = vdev->bridge; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1753 | if (!bridge) { |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1754 | printk(KERN_ERR "Can't find VME bus\n"); |
| 1755 | return -EINVAL; |
| 1756 | } |
| 1757 | |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1758 | if (!bridge->slot_get) { |
Martyn Welch | d7729f0 | 2013-11-08 11:58:35 +0000 | [diff] [blame] | 1759 | printk(KERN_WARNING "vme_slot_num not supported\n"); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1760 | return -EINVAL; |
| 1761 | } |
| 1762 | |
Martyn Welch | 29848ac | 2010-02-18 15:13:05 +0000 | [diff] [blame] | 1763 | return bridge->slot_get(bridge); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1764 | } |
Martyn Welch | d7729f0 | 2013-11-08 11:58:35 +0000 | [diff] [blame] | 1765 | EXPORT_SYMBOL(vme_slot_num); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1766 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1767 | /** |
| 1768 | * vme_bus_num - Retrieve bus number |
| 1769 | * @vdev: Pointer to VME device struct vme_dev assigned to driver instance. |
| 1770 | * |
| 1771 | * Retrieve the bus enumeration associated with the provided VME device. |
| 1772 | * |
| 1773 | * Return: The bus number on success, -EINVAL if VME bridge cannot be |
| 1774 | * determined. |
| 1775 | */ |
Martyn Welch | 978f47d | 2013-11-08 11:58:34 +0000 | [diff] [blame] | 1776 | int vme_bus_num(struct vme_dev *vdev) |
| 1777 | { |
| 1778 | struct vme_bridge *bridge; |
| 1779 | |
| 1780 | bridge = vdev->bridge; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1781 | if (!bridge) { |
Martyn Welch | 978f47d | 2013-11-08 11:58:34 +0000 | [diff] [blame] | 1782 | pr_err("Can't find VME bus\n"); |
| 1783 | return -EINVAL; |
| 1784 | } |
| 1785 | |
| 1786 | return bridge->num; |
| 1787 | } |
| 1788 | EXPORT_SYMBOL(vme_bus_num); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1789 | |
| 1790 | /* - Bridge Registration --------------------------------------------------- */ |
| 1791 | |
Manohar Vanga | 5b93c2a | 2011-11-04 11:12:30 +0100 | [diff] [blame] | 1792 | static void vme_dev_release(struct device *dev) |
| 1793 | { |
| 1794 | kfree(dev_to_vme_dev(dev)); |
| 1795 | } |
| 1796 | |
Aaron Sierra | 326071b | 2016-04-24 15:11:38 -0500 | [diff] [blame] | 1797 | /* Common bridge initialization */ |
| 1798 | struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge) |
| 1799 | { |
| 1800 | INIT_LIST_HEAD(&bridge->vme_error_handlers); |
| 1801 | INIT_LIST_HEAD(&bridge->master_resources); |
| 1802 | INIT_LIST_HEAD(&bridge->slave_resources); |
| 1803 | INIT_LIST_HEAD(&bridge->dma_resources); |
| 1804 | INIT_LIST_HEAD(&bridge->lm_resources); |
| 1805 | mutex_init(&bridge->irq_mtx); |
| 1806 | |
| 1807 | return bridge; |
| 1808 | } |
| 1809 | EXPORT_SYMBOL(vme_init_bridge); |
| 1810 | |
Manohar Vanga | 5b93c2a | 2011-11-04 11:12:30 +0100 | [diff] [blame] | 1811 | int vme_register_bridge(struct vme_bridge *bridge) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1812 | { |
| 1813 | int i; |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1814 | int ret = -1; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1815 | |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1816 | mutex_lock(&vme_buses_lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1817 | for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) { |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1818 | if ((vme_bus_numbers & (1 << i)) == 0) { |
| 1819 | vme_bus_numbers |= (1 << i); |
| 1820 | bridge->num = i; |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1821 | INIT_LIST_HEAD(&bridge->devices); |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1822 | list_add_tail(&bridge->bus_list, &vme_bus_list); |
| 1823 | ret = 0; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1824 | break; |
| 1825 | } |
| 1826 | } |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1827 | mutex_unlock(&vme_buses_lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1828 | |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1829 | return ret; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1830 | } |
Manohar Vanga | 5b93c2a | 2011-11-04 11:12:30 +0100 | [diff] [blame] | 1831 | EXPORT_SYMBOL(vme_register_bridge); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1832 | |
Manohar Vanga | 5b93c2a | 2011-11-04 11:12:30 +0100 | [diff] [blame] | 1833 | void vme_unregister_bridge(struct vme_bridge *bridge) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1834 | { |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1835 | struct vme_dev *vdev; |
| 1836 | struct vme_dev *tmp; |
| 1837 | |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1838 | mutex_lock(&vme_buses_lock); |
| 1839 | vme_bus_numbers &= ~(1 << bridge->num); |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1840 | list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) { |
| 1841 | list_del(&vdev->drv_list); |
| 1842 | list_del(&vdev->bridge_list); |
| 1843 | device_unregister(&vdev->dev); |
| 1844 | } |
Manohar Vanga | 733e3ef | 2011-08-12 12:30:48 +0200 | [diff] [blame] | 1845 | list_del(&bridge->bus_list); |
| 1846 | mutex_unlock(&vme_buses_lock); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1847 | } |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1848 | EXPORT_SYMBOL(vme_unregister_bridge); |
| 1849 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1850 | /* - Driver Registration --------------------------------------------------- */ |
| 1851 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1852 | static int __vme_register_driver_bus(struct vme_driver *drv, |
| 1853 | struct vme_bridge *bridge, unsigned int ndevs) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1854 | { |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1855 | int err; |
| 1856 | unsigned int i; |
| 1857 | struct vme_dev *vdev; |
| 1858 | struct vme_dev *tmp; |
| 1859 | |
| 1860 | for (i = 0; i < ndevs; i++) { |
Markus Elfring | 1ff0a19 | 2017-08-24 21:52:00 +0200 | [diff] [blame] | 1861 | vdev = kzalloc(sizeof(*vdev), GFP_KERNEL); |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1862 | if (!vdev) { |
| 1863 | err = -ENOMEM; |
| 1864 | goto err_devalloc; |
| 1865 | } |
Manohar Vanga | a916a39 | 2011-09-26 11:27:17 +0200 | [diff] [blame] | 1866 | vdev->num = i; |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1867 | vdev->bridge = bridge; |
| 1868 | vdev->dev.platform_data = drv; |
| 1869 | vdev->dev.release = vme_dev_release; |
| 1870 | vdev->dev.parent = bridge->parent; |
| 1871 | vdev->dev.bus = &vme_bus_type; |
Manohar Vanga | a916a39 | 2011-09-26 11:27:17 +0200 | [diff] [blame] | 1872 | dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num, |
| 1873 | vdev->num); |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1874 | |
| 1875 | err = device_register(&vdev->dev); |
| 1876 | if (err) |
| 1877 | goto err_reg; |
| 1878 | |
| 1879 | if (vdev->dev.platform_data) { |
| 1880 | list_add_tail(&vdev->drv_list, &drv->devices); |
| 1881 | list_add_tail(&vdev->bridge_list, &bridge->devices); |
| 1882 | } else |
| 1883 | device_unregister(&vdev->dev); |
| 1884 | } |
| 1885 | return 0; |
| 1886 | |
| 1887 | err_reg: |
Emilio G. Cota | def1820 | 2013-02-13 13:47:54 -0500 | [diff] [blame] | 1888 | put_device(&vdev->dev); |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1889 | err_devalloc: |
| 1890 | list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) { |
| 1891 | list_del(&vdev->drv_list); |
| 1892 | list_del(&vdev->bridge_list); |
| 1893 | device_unregister(&vdev->dev); |
| 1894 | } |
| 1895 | return err; |
| 1896 | } |
| 1897 | |
| 1898 | static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs) |
| 1899 | { |
| 1900 | struct vme_bridge *bridge; |
| 1901 | int err = 0; |
| 1902 | |
| 1903 | mutex_lock(&vme_buses_lock); |
| 1904 | list_for_each_entry(bridge, &vme_bus_list, bus_list) { |
| 1905 | /* |
| 1906 | * This cannot cause trouble as we already have vme_buses_lock |
| 1907 | * and if the bridge is removed, it will have to go through |
| 1908 | * vme_unregister_bridge() to do it (which calls remove() on |
| 1909 | * the bridge which in turn tries to acquire vme_buses_lock and |
Manohar Vanga | c26f611 | 2011-11-04 11:12:29 +0100 | [diff] [blame] | 1910 | * will have to wait). |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1911 | */ |
| 1912 | err = __vme_register_driver_bus(drv, bridge, ndevs); |
| 1913 | if (err) |
| 1914 | break; |
| 1915 | } |
| 1916 | mutex_unlock(&vme_buses_lock); |
| 1917 | return err; |
| 1918 | } |
| 1919 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1920 | /** |
| 1921 | * vme_register_driver - Register a VME driver |
| 1922 | * @drv: Pointer to VME driver structure to register. |
| 1923 | * @ndevs: Maximum number of devices to allow to be enumerated. |
| 1924 | * |
| 1925 | * Register a VME device driver with the VME subsystem. |
| 1926 | * |
| 1927 | * Return: Zero on success, error value on registration failure. |
| 1928 | */ |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1929 | int vme_register_driver(struct vme_driver *drv, unsigned int ndevs) |
| 1930 | { |
| 1931 | int err; |
| 1932 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1933 | drv->driver.name = drv->name; |
| 1934 | drv->driver.bus = &vme_bus_type; |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1935 | INIT_LIST_HEAD(&drv->devices); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1936 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1937 | err = driver_register(&drv->driver); |
| 1938 | if (err) |
| 1939 | return err; |
| 1940 | |
| 1941 | err = __vme_register_driver(drv, ndevs); |
| 1942 | if (err) |
| 1943 | driver_unregister(&drv->driver); |
| 1944 | |
| 1945 | return err; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1946 | } |
| 1947 | EXPORT_SYMBOL(vme_register_driver); |
| 1948 | |
Martyn Welch | b5bc980 | 2017-03-04 00:34:29 +0000 | [diff] [blame] | 1949 | /** |
| 1950 | * vme_unregister_driver - Unregister a VME driver |
| 1951 | * @drv: Pointer to VME driver structure to unregister. |
| 1952 | * |
| 1953 | * Unregister a VME device driver from the VME subsystem. |
| 1954 | */ |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 1955 | void vme_unregister_driver(struct vme_driver *drv) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1956 | { |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1957 | struct vme_dev *dev, *dev_tmp; |
| 1958 | |
| 1959 | mutex_lock(&vme_buses_lock); |
| 1960 | list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) { |
| 1961 | list_del(&dev->drv_list); |
| 1962 | list_del(&dev->bridge_list); |
| 1963 | device_unregister(&dev->dev); |
| 1964 | } |
| 1965 | mutex_unlock(&vme_buses_lock); |
| 1966 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1967 | driver_unregister(&drv->driver); |
| 1968 | } |
| 1969 | EXPORT_SYMBOL(vme_unregister_driver); |
| 1970 | |
| 1971 | /* - Bus Registration ------------------------------------------------------ */ |
| 1972 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1973 | static int vme_bus_match(struct device *dev, struct device_driver *drv) |
| 1974 | { |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1975 | struct vme_driver *vme_drv; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1976 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1977 | vme_drv = container_of(drv, struct vme_driver, driver); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1978 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1979 | if (dev->platform_data == vme_drv) { |
| 1980 | struct vme_dev *vdev = dev_to_vme_dev(dev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1981 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1982 | if (vme_drv->match && vme_drv->match(vdev)) |
| 1983 | return 1; |
| 1984 | |
| 1985 | dev->platform_data = NULL; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1986 | } |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1987 | return 0; |
| 1988 | } |
| 1989 | |
| 1990 | static int vme_bus_probe(struct device *dev) |
| 1991 | { |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1992 | struct vme_driver *driver; |
| 1993 | struct vme_dev *vdev = dev_to_vme_dev(dev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1994 | |
Manohar Vanga | 5d6abf3 | 2011-09-26 11:27:16 +0200 | [diff] [blame] | 1995 | driver = dev->platform_data; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 1996 | if (driver->probe) |
Markus Elfring | 8af70cd9 | 2017-08-24 22:32:14 +0200 | [diff] [blame] | 1997 | return driver->probe(vdev); |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 1998 | |
Markus Elfring | 8af70cd9 | 2017-08-24 22:32:14 +0200 | [diff] [blame] | 1999 | return -ENODEV; |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 2000 | } |
| 2001 | |
Stefano Babic | 9797484 | 2017-01-20 10:38:20 -0500 | [diff] [blame] | 2002 | static int vme_bus_remove(struct device *dev) |
| 2003 | { |
Stefano Babic | 9797484 | 2017-01-20 10:38:20 -0500 | [diff] [blame] | 2004 | struct vme_driver *driver; |
| 2005 | struct vme_dev *vdev = dev_to_vme_dev(dev); |
| 2006 | |
| 2007 | driver = dev->platform_data; |
Markus Elfring | 61282c0 | 2017-08-24 22:24:38 +0200 | [diff] [blame] | 2008 | if (driver->remove) |
Markus Elfring | 8af70cd9 | 2017-08-24 22:32:14 +0200 | [diff] [blame] | 2009 | return driver->remove(vdev); |
Stefano Babic | 9797484 | 2017-01-20 10:38:20 -0500 | [diff] [blame] | 2010 | |
Markus Elfring | 8af70cd9 | 2017-08-24 22:32:14 +0200 | [diff] [blame] | 2011 | return -ENODEV; |
Stefano Babic | 9797484 | 2017-01-20 10:38:20 -0500 | [diff] [blame] | 2012 | } |
| 2013 | |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 2014 | struct bus_type vme_bus_type = { |
| 2015 | .name = "vme", |
| 2016 | .match = vme_bus_match, |
| 2017 | .probe = vme_bus_probe, |
Stefano Babic | 9797484 | 2017-01-20 10:38:20 -0500 | [diff] [blame] | 2018 | .remove = vme_bus_remove, |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 2019 | }; |
| 2020 | EXPORT_SYMBOL(vme_bus_type); |
| 2021 | |
Martyn Welch | ead1f3e | 2009-12-15 08:43:02 +0000 | [diff] [blame] | 2022 | static int __init vme_init(void) |
Martyn Welch | a17a75e | 2009-07-31 09:28:17 +0100 | [diff] [blame] | 2023 | { |
| 2024 | return bus_register(&vme_bus_type); |
| 2025 | } |
Aaron Sierra | c326cc0 | 2013-12-09 09:54:42 -0600 | [diff] [blame] | 2026 | subsys_initcall(vme_init); |