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