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