blob: 1b15afea28ee973662721ea2b2b3456fb5bd3599 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Martyn Welcha17a75e2009-07-31 09:28:17 +01002/*
3 * VME Bridge Framework
4 *
Martyn Welch66bd8db2010-02-18 15:12:52 +00005 * Author: Martyn Welch <martyn.welch@ge.com>
6 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
Martyn Welcha17a75e2009-07-31 09:28:17 +01007 *
8 * Based on work by Tom Armistead and Ajit Prem
9 * Copyright 2004 Motorola Inc.
Martyn Welcha17a75e2009-07-31 09:28:17 +010010 */
11
Paul Gortmaker050c3d52016-07-03 14:05:56 -040012#include <linux/init.h>
13#include <linux/export.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010014#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 Welch400822f2009-08-11 16:20:22 +010026#include <linux/mutex.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010027#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Greg Kroah-Hartmandb3b9e92012-04-26 12:34:58 -070029#include <linux/vme.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010030
Martyn Welcha17a75e2009-07-31 09:28:17 +010031#include "vme_bridge.h"
32
Manohar Vanga733e3ef2011-08-12 12:30:48 +020033/* Bitmask and list of registered buses both protected by common mutex */
Martyn Welcha17a75e2009-07-31 09:28:17 +010034static unsigned int vme_bus_numbers;
Manohar Vanga733e3ef2011-08-12 12:30:48 +020035static LIST_HEAD(vme_bus_list);
36static DEFINE_MUTEX(vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +010037
Martyn Welchead1f3e2009-12-15 08:43:02 +000038static int __init vme_init(void);
Martyn Welcha17a75e2009-07-31 09:28:17 +010039
Manohar Vanga8f966dc2011-09-26 11:27:15 +020040static struct vme_dev *dev_to_vme_dev(struct device *dev)
Martyn Welcha17a75e2009-07-31 09:28:17 +010041{
Manohar Vanga8f966dc2011-09-26 11:27:15 +020042 return container_of(dev, struct vme_dev, dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +010043}
44
45/*
46 * Find the bridge that the resource is associated with.
47 */
48static 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 Welcha17a75e2009-07-31 09:28:17 +010055 case VME_SLAVE:
56 return list_entry(resource->entry, struct vme_slave_resource,
57 list)->parent;
Martyn Welcha17a75e2009-07-31 09:28:17 +010058 case VME_DMA:
59 return list_entry(resource->entry, struct vme_dma_resource,
60 list)->parent;
Martyn Welch42fb5032009-08-11 17:44:56 +010061 case VME_LM:
62 return list_entry(resource->entry, struct vme_lm_resource,
63 list)->parent;
Martyn Welcha17a75e2009-07-31 09:28:17 +010064 default:
65 printk(KERN_ERR "Unknown resource type\n");
66 return NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +010067 }
68}
69
Martyn Welchb5bc9802017-03-04 00:34:29 +000070/**
Mauro Carvalho Chehab8bd16062020-10-23 18:33:20 +020071 * vme_alloc_consistent - Allocate contiguous memory.
Martyn Welchb5bc9802017-03-04 00:34:29 +000072 * @resource: Pointer to VME resource.
73 * @size: Size of allocation required.
74 * @dma: Pointer to variable to store physical address of allocation.
75 *
Martyn Welcha17a75e2009-07-31 09:28:17 +010076 * Allocate a contiguous block of memory for use by the driver. This is used to
77 * create the buffers for the slave windows.
Martyn Welchb5bc9802017-03-04 00:34:29 +000078 *
79 * Return: Virtual address of allocation on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +010080 */
Martyn Welchead1f3e2009-12-15 08:43:02 +000081void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
Martyn Welcha17a75e2009-07-31 09:28:17 +010082 dma_addr_t *dma)
83{
84 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +010085
Markus Elfring61282c02017-08-24 22:24:38 +020086 if (!resource) {
Martyn Welchead1f3e2009-12-15 08:43:02 +000087 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +010088 return NULL;
89 }
90
91 bridge = find_bridge(resource);
Markus Elfring61282c02017-08-24 22:24:38 +020092 if (!bridge) {
Martyn Welchead1f3e2009-12-15 08:43:02 +000093 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +010094 return NULL;
95 }
96
Markus Elfring61282c02017-08-24 22:24:38 +020097 if (!bridge->parent) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -070098 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Martyn Welcha17a75e2009-07-31 09:28:17 +010099 return NULL;
100 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100101
Markus Elfring61282c02017-08-24 22:24:38 +0200102 if (!bridge->alloc_consistent) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700103 printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
104 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200105 return NULL;
106 }
107
108 return bridge->alloc_consistent(bridge->parent, size, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100109}
110EXPORT_SYMBOL(vme_alloc_consistent);
111
Martyn Welchb5bc9802017-03-04 00:34:29 +0000112/**
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 Welcha17a75e2009-07-31 09:28:17 +0100120 */
121void vme_free_consistent(struct vme_resource *resource, size_t size,
122 void *vaddr, dma_addr_t dma)
123{
124 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100125
Markus Elfring61282c02017-08-24 22:24:38 +0200126 if (!resource) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000127 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100128 return;
129 }
130
131 bridge = find_bridge(resource);
Markus Elfring61282c02017-08-24 22:24:38 +0200132 if (!bridge) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000133 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100134 return;
135 }
136
Markus Elfring61282c02017-08-24 22:24:38 +0200137 if (!bridge->parent) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700138 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200139 return;
140 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100141
Markus Elfring61282c02017-08-24 22:24:38 +0200142 if (!bridge->free_consistent) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700143 printk(KERN_ERR "free_consistent not supported by bridge %s\n",
144 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200145 return;
146 }
147
148 bridge->free_consistent(bridge->parent, size, vaddr, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100149}
150EXPORT_SYMBOL(vme_free_consistent);
151
Martyn Welchb5bc9802017-03-04 00:34:29 +0000152/**
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 Welcha17a75e2009-07-31 09:28:17 +0100162size_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 Welch6af04b02011-12-01 17:06:29 +0000167 u32 aspace, cycle, dwidth;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100168
169 switch (resource->type) {
170 case VME_MASTER:
171 retval = vme_master_get(resource, &enabled, &base, &size,
172 &aspace, &cycle, &dwidth);
Martyn Welch6ad37562016-10-21 17:36:19 +0100173 if (retval)
174 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100175
176 return size;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100177 case VME_SLAVE:
178 retval = vme_slave_get(resource, &enabled, &base, &size,
179 &buf_base, &aspace, &cycle);
Martyn Welch6ad37562016-10-21 17:36:19 +0100180 if (retval)
181 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100182
183 return size;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100184 case VME_DMA:
185 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100186 default:
187 printk(KERN_ERR "Unknown resource type\n");
188 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100189 }
190}
191EXPORT_SYMBOL(vme_get_size);
192
Dmitry Kalinkinef73f882015-05-28 15:07:04 +0300193int vme_check_window(u32 aspace, unsigned long long vme_base,
194 unsigned long long size)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100195{
196 int retval = 0;
197
Dan Carpenterbd147982017-09-30 14:27:41 +0300198 if (vme_base + size < size)
199 return -EINVAL;
200
Martyn Welcha17a75e2009-07-31 09:28:17 +0100201 switch (aspace) {
202 case VME_A16:
Dan Carpenterbd147982017-09-30 14:27:41 +0300203 if (vme_base + size > VME_A16_MAX)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100204 retval = -EFAULT;
205 break;
206 case VME_A24:
Dan Carpenterbd147982017-09-30 14:27:41 +0300207 if (vme_base + size > VME_A24_MAX)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100208 retval = -EFAULT;
209 break;
210 case VME_A32:
Dan Carpenterbd147982017-09-30 14:27:41 +0300211 if (vme_base + size > VME_A32_MAX)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100212 retval = -EFAULT;
213 break;
214 case VME_A64:
Dan Carpenterbd147982017-09-30 14:27:41 +0300215 /* The VME_A64_MAX limit is actually U64_MAX + 1 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100216 break;
217 case VME_CRCSR:
Dan Carpenterbd147982017-09-30 14:27:41 +0300218 if (vme_base + size > VME_CRCSR_MAX)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100219 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 Welchead1f3e2009-12-15 08:43:02 +0000228 printk(KERN_ERR "Invalid address space\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100229 retval = -EINVAL;
230 break;
231 }
232
233 return retval;
234}
Dmitry Kalinkinef73f882015-05-28 15:07:04 +0300235EXPORT_SYMBOL(vme_check_window);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100236
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +0300237static 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 Welchb5bc9802017-03-04 00:34:29 +0000270/**
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 Welcha17a75e2009-07-31 09:28:17 +0100280 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000281struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
282 u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100283{
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 Vanga8f966dc2011-09-26 11:27:15 +0200290 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +0200291 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100292 printk(KERN_ERR "Can't find VME bus\n");
293 goto err_bus;
294 }
295
296 /* Loop through slave resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000297 list_for_each(slave_pos, &bridge->slave_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100298 slave_image = list_entry(slave_pos,
299 struct vme_slave_resource, list);
300
Markus Elfring61282c02017-08-24 22:24:38 +0200301 if (!slave_image) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000302 printk(KERN_ERR "Registered NULL Slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100303 continue;
304 }
305
306 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000307 mutex_lock(&slave_image->mtx);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000308 if (((slave_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100309 ((slave_image->cycle_attr & cycle) == cycle) &&
310 (slave_image->locked == 0)) {
311
312 slave_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000313 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100314 allocated_image = slave_image;
315 break;
316 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000317 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100318 }
319
320 /* No free image */
Markus Elfring61282c02017-08-24 22:24:38 +0200321 if (!allocated_image)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100322 goto err_image;
323
Markus Elfring1ff0a192017-08-24 21:52:00 +0200324 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200325 if (!resource)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100326 goto err_alloc;
Markus Elfring94eefcc2017-08-24 21:38:20 +0200327
Martyn Welcha17a75e2009-07-31 09:28:17 +0100328 resource->type = VME_SLAVE;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000329 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100330
331 return resource;
332
333err_alloc:
334 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000335 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100336 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000337 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100338err_image:
339err_bus:
340 return NULL;
341}
342EXPORT_SYMBOL(vme_slave_request);
343
Martyn Welchb5bc9802017-03-04 00:34:29 +0000344/**
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 Welchead1f3e2009-12-15 08:43:02 +0000361int vme_slave_set(struct vme_resource *resource, int enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100362 unsigned long long vme_base, unsigned long long size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000363 dma_addr_t buf_base, u32 aspace, u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100364{
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 Welchead1f3e2009-12-15 08:43:02 +0000370 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100371 return -EINVAL;
372 }
373
374 image = list_entry(resource->entry, struct vme_slave_resource, list);
375
Markus Elfring61282c02017-08-24 22:24:38 +0200376 if (!bridge->slave_set) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000377 printk(KERN_ERR "Function not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100378 return -ENOSYS;
379 }
380
Martyn Welchead1f3e2009-12-15 08:43:02 +0000381 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100382 ((image->cycle_attr & cycle) == cycle))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000383 printk(KERN_ERR "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100384 return -EINVAL;
385 }
386
387 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000388 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100389 return retval;
390
391 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
392 aspace, cycle);
393}
394EXPORT_SYMBOL(vme_slave_set);
395
Martyn Welchb5bc9802017-03-04 00:34:29 +0000396/**
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 Welchead1f3e2009-12-15 08:43:02 +0000411int vme_slave_get(struct vme_resource *resource, int *enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100412 unsigned long long *vme_base, unsigned long long *size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000413 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100414{
415 struct vme_bridge *bridge = find_bridge(resource);
416 struct vme_slave_resource *image;
417
418 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000419 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100420 return -EINVAL;
421 }
422
423 image = list_entry(resource->entry, struct vme_slave_resource, list);
424
Markus Elfring61282c02017-08-24 22:24:38 +0200425 if (!bridge->slave_get) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000426 printk(KERN_ERR "vme_slave_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100427 return -EINVAL;
428 }
429
430 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
431 aspace, cycle);
432}
433EXPORT_SYMBOL(vme_slave_get);
434
Martyn Welchb5bc9802017-03-04 00:34:29 +0000435/**
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 Welcha17a75e2009-07-31 09:28:17 +0100441void vme_slave_free(struct vme_resource *resource)
442{
443 struct vme_slave_resource *slave_image;
444
445 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000446 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100447 return;
448 }
449
450 slave_image = list_entry(resource->entry, struct vme_slave_resource,
451 list);
Markus Elfring61282c02017-08-24 22:24:38 +0200452 if (!slave_image) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000453 printk(KERN_ERR "Can't find slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100454 return;
455 }
456
457 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000458 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100459 if (slave_image->locked == 0)
460 printk(KERN_ERR "Image is already free\n");
461
462 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000463 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100464
465 /* Free up resource memory */
466 kfree(resource);
467}
468EXPORT_SYMBOL(vme_slave_free);
469
Martyn Welchb5bc9802017-03-04 00:34:29 +0000470/**
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 Welcha17a75e2009-07-31 09:28:17 +0100481 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000482struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
483 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100484{
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 Vanga8f966dc2011-09-26 11:27:15 +0200491 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +0200492 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100493 printk(KERN_ERR "Can't find VME bus\n");
494 goto err_bus;
495 }
496
497 /* Loop through master resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000498 list_for_each(master_pos, &bridge->master_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100499 master_image = list_entry(master_pos,
500 struct vme_master_resource, list);
501
Markus Elfring61282c02017-08-24 22:24:38 +0200502 if (!master_image) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100503 printk(KERN_WARNING "Registered NULL master resource\n");
504 continue;
505 }
506
507 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000508 spin_lock(&master_image->lock);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000509 if (((master_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100510 ((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. Cota886953e2010-11-12 11:14:07 +0000515 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100516 allocated_image = master_image;
517 break;
518 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000519 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100520 }
521
522 /* Check to see if we found a resource */
Markus Elfring61282c02017-08-24 22:24:38 +0200523 if (!allocated_image) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100524 printk(KERN_ERR "Can't find a suitable resource\n");
525 goto err_image;
526 }
527
Markus Elfring1ff0a192017-08-24 21:52:00 +0200528 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200529 if (!resource)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100530 goto err_alloc;
Markus Elfring94eefcc2017-08-24 21:38:20 +0200531
Martyn Welcha17a75e2009-07-31 09:28:17 +0100532 resource->type = VME_MASTER;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000533 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100534
535 return resource;
536
Martyn Welcha17a75e2009-07-31 09:28:17 +0100537err_alloc:
538 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000539 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100540 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000541 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100542err_image:
543err_bus:
544 return NULL;
545}
546EXPORT_SYMBOL(vme_master_request);
547
Martyn Welchb5bc9802017-03-04 00:34:29 +0000548/**
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 Welchead1f3e2009-12-15 08:43:02 +0000565int vme_master_set(struct vme_resource *resource, int enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000566 unsigned long long vme_base, unsigned long long size, u32 aspace,
567 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100568{
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 Welchead1f3e2009-12-15 08:43:02 +0000574 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100575 return -EINVAL;
576 }
577
578 image = list_entry(resource->entry, struct vme_master_resource, list);
579
Markus Elfring61282c02017-08-24 22:24:38 +0200580 if (!bridge->master_set) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000581 printk(KERN_WARNING "vme_master_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100582 return -EINVAL;
583 }
584
Martyn Welchead1f3e2009-12-15 08:43:02 +0000585 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100586 ((image->cycle_attr & cycle) == cycle) &&
587 ((image->width_attr & dwidth) == dwidth))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000588 printk(KERN_WARNING "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100589 return -EINVAL;
590 }
591
592 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000593 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100594 return retval;
595
596 return bridge->master_set(image, enabled, vme_base, size, aspace,
597 cycle, dwidth);
598}
599EXPORT_SYMBOL(vme_master_set);
600
Martyn Welchb5bc9802017-03-04 00:34:29 +0000601/**
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 Welchead1f3e2009-12-15 08:43:02 +0000616int vme_master_get(struct vme_resource *resource, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000617 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
618 u32 *cycle, u32 *dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100619{
620 struct vme_bridge *bridge = find_bridge(resource);
621 struct vme_master_resource *image;
622
623 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000624 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100625 return -EINVAL;
626 }
627
628 image = list_entry(resource->entry, struct vme_master_resource, list);
629
Markus Elfring61282c02017-08-24 22:24:38 +0200630 if (!bridge->master_get) {
Julia Lawallc1038302014-12-07 20:20:53 +0100631 printk(KERN_WARNING "%s not supported\n", __func__);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100632 return -EINVAL;
633 }
634
635 return bridge->master_get(image, enabled, vme_base, size, aspace,
636 cycle, dwidth);
637}
638EXPORT_SYMBOL(vme_master_get);
639
Martyn Welchb5bc9802017-03-04 00:34:29 +0000640/**
Mauro Carvalho Chehab8bd16062020-10-23 18:33:20 +0200641 * vme_master_read - Read data from VME space into a buffer.
Martyn Welchb5bc9802017-03-04 00:34:29 +0000642 * @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 Welcha17a75e2009-07-31 09:28:17 +0100654 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000655ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100656 loff_t offset)
657{
658 struct vme_bridge *bridge = find_bridge(resource);
659 struct vme_master_resource *image;
660 size_t length;
661
Markus Elfring61282c02017-08-24 22:24:38 +0200662 if (!bridge->master_read) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000663 printk(KERN_WARNING "Reading from resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100664 return -EINVAL;
665 }
666
667 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000668 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100669 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 Welchead1f3e2009-12-15 08:43:02 +0000677 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100678 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}
687EXPORT_SYMBOL(vme_master_read);
688
Martyn Welchb5bc9802017-03-04 00:34:29 +0000689/**
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 Welcha17a75e2009-07-31 09:28:17 +0100703 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000704ssize_t vme_master_write(struct vme_resource *resource, void *buf,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100705 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 Elfring61282c02017-08-24 22:24:38 +0200711 if (!bridge->master_write) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000712 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100713 return -EINVAL;
714 }
715
716 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000717 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100718 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 Welchead1f3e2009-12-15 08:43:02 +0000726 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100727 return -EFAULT;
728 }
729
730 if ((offset + count) > length)
731 count = length - offset;
732
733 return bridge->master_write(image, buf, count, offset);
734}
735EXPORT_SYMBOL(vme_master_write);
736
Martyn Welchb5bc9802017-03-04 00:34:29 +0000737/**
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 Welcha17a75e2009-07-31 09:28:17 +0100755 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000756unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100757 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 Elfring61282c02017-08-24 22:24:38 +0200762 if (!bridge->master_rmw) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000763 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100764 return -EINVAL;
765 }
766
767 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000768 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100769 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}
776EXPORT_SYMBOL(vme_master_rmw);
777
Martyn Welchb5bc9802017-03-04 00:34:29 +0000778/**
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 Kalinkinc74a804f2015-02-26 18:53:10 +0300789int 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}
813EXPORT_SYMBOL(vme_master_mmap);
814
Martyn Welchb5bc9802017-03-04 00:34:29 +0000815/**
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 Welcha17a75e2009-07-31 09:28:17 +0100821void vme_master_free(struct vme_resource *resource)
822{
823 struct vme_master_resource *master_image;
824
825 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000826 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100827 return;
828 }
829
830 master_image = list_entry(resource->entry, struct vme_master_resource,
831 list);
Markus Elfring61282c02017-08-24 22:24:38 +0200832 if (!master_image) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000833 printk(KERN_ERR "Can't find master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100834 return;
835 }
836
837 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000838 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100839 if (master_image->locked == 0)
840 printk(KERN_ERR "Image is already free\n");
841
842 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000843 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100844
845 /* Free up resource memory */
846 kfree(resource);
847}
848EXPORT_SYMBOL(vme_master_free);
849
Martyn Welchb5bc9802017-03-04 00:34:29 +0000850/**
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 Welcha17a75e2009-07-31 09:28:17 +0100859 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000860struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100861{
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 Vanga8f966dc2011-09-26 11:27:15 +0200871 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +0200872 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100873 printk(KERN_ERR "Can't find VME bus\n");
874 goto err_bus;
875 }
876
877 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000878 list_for_each(dma_pos, &bridge->dma_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100879 dma_ctrlr = list_entry(dma_pos,
880 struct vme_dma_resource, list);
Markus Elfring61282c02017-08-24 22:24:38 +0200881 if (!dma_ctrlr) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000882 printk(KERN_ERR "Registered NULL DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100883 continue;
884 }
885
Martyn Welch4f723df2010-02-18 15:12:58 +0000886 /* Find an unlocked and compatible controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000887 mutex_lock(&dma_ctrlr->mtx);
Martyn Welch4f723df2010-02-18 15:12:58 +0000888 if (((dma_ctrlr->route_attr & route) == route) &&
889 (dma_ctrlr->locked == 0)) {
890
Martyn Welcha17a75e2009-07-31 09:28:17 +0100891 dma_ctrlr->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000892 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100893 allocated_ctrlr = dma_ctrlr;
894 break;
895 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000896 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100897 }
898
899 /* Check to see if we found a resource */
Markus Elfring61282c02017-08-24 22:24:38 +0200900 if (!allocated_ctrlr)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100901 goto err_ctrlr;
902
Markus Elfring1ff0a192017-08-24 21:52:00 +0200903 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200904 if (!resource)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100905 goto err_alloc;
Markus Elfring94eefcc2017-08-24 21:38:20 +0200906
Martyn Welcha17a75e2009-07-31 09:28:17 +0100907 resource->type = VME_DMA;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000908 resource->entry = &allocated_ctrlr->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100909
910 return resource;
911
912err_alloc:
913 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000914 mutex_lock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100915 dma_ctrlr->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000916 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100917err_ctrlr:
918err_bus:
919 return NULL;
920}
Martyn Welch58e50792009-10-29 16:35:20 +0000921EXPORT_SYMBOL(vme_dma_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100922
Martyn Welchb5bc9802017-03-04 00:34:29 +0000923/**
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 Welcha17a75e2009-07-31 09:28:17 +0100932 */
933struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
934{
Martyn Welcha17a75e2009-07-31 09:28:17 +0100935 struct vme_dma_list *dma_list;
936
937 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000938 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100939 return NULL;
940 }
941
Markus Elfring1ff0a192017-08-24 21:52:00 +0200942 dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200943 if (!dma_list)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100944 return NULL;
Markus Elfring94eefcc2017-08-24 21:38:20 +0200945
Emilio G. Cota886953e2010-11-12 11:14:07 +0000946 INIT_LIST_HEAD(&dma_list->entries);
Markus Elfringa384b2c2017-08-24 22:04:45 +0200947 dma_list->parent = list_entry(resource->entry,
948 struct vme_dma_resource,
949 list);
Emilio G. Cota886953e2010-11-12 11:14:07 +0000950 mutex_init(&dma_list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100951
952 return dma_list;
953}
954EXPORT_SYMBOL(vme_new_dma_list);
955
Martyn Welchb5bc9802017-03-04 00:34:29 +0000956/**
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 Welcha17a75e2009-07-31 09:28:17 +0100966 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000967struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100968{
969 struct vme_dma_attr *attributes;
970 struct vme_dma_pattern *pattern_attr;
971
Markus Elfring1ff0a192017-08-24 21:52:00 +0200972 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200973 if (!attributes)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100974 goto err_attr;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100975
Markus Elfring1ff0a192017-08-24 21:52:00 +0200976 pattern_attr = kmalloc(sizeof(*pattern_attr), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200977 if (!pattern_attr)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100978 goto err_pat;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100979
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 Welcha17a75e2009-07-31 09:28:17 +0100988err_pat:
989 kfree(attributes);
990err_attr:
991 return NULL;
992}
993EXPORT_SYMBOL(vme_dma_pattern_attribute);
994
Martyn Welchb5bc9802017-03-04 00:34:29 +0000995/**
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 Welcha17a75e2009-07-31 09:28:17 +01001004 */
1005struct 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 Elfring1ff0a192017-08-24 21:52:00 +02001012 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001013 if (!attributes)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001014 goto err_attr;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001015
Markus Elfring1ff0a192017-08-24 21:52:00 +02001016 pci_attr = kmalloc(sizeof(*pci_attr), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001017 if (!pci_attr)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001018 goto err_pci;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001019
1020 attributes->type = VME_DMA_PCI;
1021 attributes->private = (void *)pci_attr;
1022
1023 pci_attr->address = address;
1024
1025 return attributes;
1026
Martyn Welcha17a75e2009-07-31 09:28:17 +01001027err_pci:
1028 kfree(attributes);
1029err_attr:
1030 return NULL;
1031}
1032EXPORT_SYMBOL(vme_dma_pci_attribute);
1033
Martyn Welchb5bc9802017-03-04 00:34:29 +00001034/**
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 Welcha17a75e2009-07-31 09:28:17 +01001046 */
1047struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
Martyn Welch6af04b02011-12-01 17:06:29 +00001048 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001049{
1050 struct vme_dma_attr *attributes;
1051 struct vme_dma_vme *vme_attr;
1052
Markus Elfring1ff0a192017-08-24 21:52:00 +02001053 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001054 if (!attributes)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001055 goto err_attr;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001056
Markus Elfring1ff0a192017-08-24 21:52:00 +02001057 vme_attr = kmalloc(sizeof(*vme_attr), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001058 if (!vme_attr)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001059 goto err_vme;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001060
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 Welcha17a75e2009-07-31 09:28:17 +01001071err_vme:
1072 kfree(attributes);
1073err_attr:
1074 return NULL;
1075}
1076EXPORT_SYMBOL(vme_dma_vme_attribute);
1077
Martyn Welchb5bc9802017-03-04 00:34:29 +00001078/**
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 Welcha17a75e2009-07-31 09:28:17 +01001084 */
1085void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1086{
1087 kfree(attributes->private);
1088 kfree(attributes);
1089}
1090EXPORT_SYMBOL(vme_dma_free_attribute);
1091
Martyn Welchb5bc9802017-03-04 00:34:29 +00001092/**
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 Welcha17a75e2009-07-31 09:28:17 +01001109int 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 Elfring61282c02017-08-24 22:24:38 +02001115 if (!bridge->dma_list_add) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001116 printk(KERN_WARNING "Link List DMA generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001117 return -EINVAL;
1118 }
1119
Emilio G. Cota886953e2010-11-12 11:14:07 +00001120 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001121 printk(KERN_ERR "Link List already submitted\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001122 return -EINVAL;
1123 }
1124
1125 retval = bridge->dma_list_add(list, src, dest, count);
1126
Emilio G. Cota886953e2010-11-12 11:14:07 +00001127 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001128
1129 return retval;
1130}
1131EXPORT_SYMBOL(vme_dma_list_add);
1132
Martyn Welchb5bc9802017-03-04 00:34:29 +00001133/**
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 Welcha17a75e2009-07-31 09:28:17 +01001143int vme_dma_list_exec(struct vme_dma_list *list)
1144{
1145 struct vme_bridge *bridge = list->parent->parent;
1146 int retval;
1147
Markus Elfring61282c02017-08-24 22:24:38 +02001148 if (!bridge->dma_list_exec) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001149 printk(KERN_ERR "Link List DMA execution not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001150 return -EINVAL;
1151 }
1152
Emilio G. Cota886953e2010-11-12 11:14:07 +00001153 mutex_lock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001154
1155 retval = bridge->dma_list_exec(list);
1156
Emilio G. Cota886953e2010-11-12 11:14:07 +00001157 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001158
1159 return retval;
1160}
1161EXPORT_SYMBOL(vme_dma_list_exec);
1162
Martyn Welchb5bc9802017-03-04 00:34:29 +00001163/**
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 Welcha17a75e2009-07-31 09:28:17 +01001172int vme_dma_list_free(struct vme_dma_list *list)
1173{
1174 struct vme_bridge *bridge = list->parent->parent;
1175 int retval;
1176
Markus Elfring61282c02017-08-24 22:24:38 +02001177 if (!bridge->dma_list_empty) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001178 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001179 return -EINVAL;
1180 }
1181
Emilio G. Cota886953e2010-11-12 11:14:07 +00001182 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001183 printk(KERN_ERR "Link List in use\n");
Martyn Welchf13d1a82017-10-24 08:27:40 +02001184 return -EBUSY;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001185 }
1186
1187 /*
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001188 * 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 Welcha17a75e2009-07-31 09:28:17 +01001190 */
1191 retval = bridge->dma_list_empty(list);
1192 if (retval) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001193 printk(KERN_ERR "Unable to empty link-list entries\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +00001194 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001195 return retval;
1196 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001197 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001198 kfree(list);
1199
1200 return retval;
1201}
1202EXPORT_SYMBOL(vme_dma_list_free);
1203
Martyn Welchb5bc9802017-03-04 00:34:29 +00001204/**
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 Welcha17a75e2009-07-31 09:28:17 +01001213int vme_dma_free(struct vme_resource *resource)
1214{
1215 struct vme_dma_resource *ctrlr;
1216
1217 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001218 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001219 return -EINVAL;
1220 }
1221
1222 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1223
Emilio G. Cota886953e2010-11-12 11:14:07 +00001224 if (!mutex_trylock(&ctrlr->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001225 printk(KERN_ERR "Resource busy, can't free\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001226 return -EBUSY;
1227 }
1228
Emilio G. Cota886953e2010-11-12 11:14:07 +00001229 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001230 printk(KERN_WARNING "Resource still processing transfers\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +00001231 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001232 return -EBUSY;
1233 }
1234
1235 ctrlr->locked = 0;
1236
Emilio G. Cota886953e2010-11-12 11:14:07 +00001237 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001238
Martyn Welchfd5c2562013-06-06 12:29:16 +01001239 kfree(resource);
1240
Martyn Welcha17a75e2009-07-31 09:28:17 +01001241 return 0;
1242}
1243EXPORT_SYMBOL(vme_dma_free);
1244
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001245void vme_bus_error_handler(struct vme_bridge *bridge,
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +03001246 unsigned long long address, int am)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001247{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001248 struct list_head *handler_pos = NULL;
1249 struct vme_error_handler *handler;
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001250 int handler_triggered = 0;
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001251 u32 aspace = vme_get_aspace(am);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001252
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001253 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 Kalinkin448535a2015-09-18 02:01:45 +03001263 handler_triggered = 1;
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001264 }
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001265 }
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001266
1267 if (!handler_triggered)
1268 dev_err(bridge->parent,
1269 "Unhandled VME access error at address 0x%llx\n",
1270 address);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001271}
1272EXPORT_SYMBOL(vme_bus_error_handler);
1273
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001274struct vme_error_handler *vme_register_error_handler(
1275 struct vme_bridge *bridge, u32 aspace,
1276 unsigned long long address, size_t len)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001277{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001278 struct vme_error_handler *handler;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001279
Jia-Ju Bai97784612017-12-12 20:51:53 +08001280 handler = kmalloc(sizeof(*handler), GFP_ATOMIC);
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001281 if (!handler)
1282 return NULL;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001283
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001284 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 Kalinkine2c63932015-09-18 02:01:42 +03001290
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001291 return handler;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001292}
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001293EXPORT_SYMBOL(vme_register_error_handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001294
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001295void vme_unregister_error_handler(struct vme_error_handler *handler)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001296{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001297 list_del(&handler->list);
1298 kfree(handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001299}
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001300EXPORT_SYMBOL(vme_unregister_error_handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001301
Martyn Welchc813f592009-10-29 16:34:54 +00001302void 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 Elfring61282c02017-08-24 22:24:38 +02001309 if (call)
Martyn Welchc813f592009-10-29 16:34:54 +00001310 call(level, statid, priv_data);
1311 else
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001312 printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001313 level, statid);
Martyn Welchc813f592009-10-29 16:34:54 +00001314}
1315EXPORT_SYMBOL(vme_irq_handler);
1316
Martyn Welchb5bc9802017-03-04 00:34:29 +00001317/**
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 Vanga8f966dc2011-09-26 11:27:15 +02001333int vme_irq_request(struct vme_dev *vdev, int level, int statid,
Martyn Welch29848ac2010-02-18 15:13:05 +00001334 void (*callback)(int, int, void *),
Martyn Welcha17a75e2009-07-31 09:28:17 +01001335 void *priv_data)
1336{
1337 struct vme_bridge *bridge;
1338
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001339 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001340 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001341 printk(KERN_ERR "Can't find VME bus\n");
1342 return -EINVAL;
1343 }
1344
Martyn Welchead1f3e2009-12-15 08:43:02 +00001345 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001346 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001347 return -EINVAL;
1348 }
1349
Markus Elfring61282c02017-08-24 22:24:38 +02001350 if (!bridge->irq_set) {
Martyn Welchc813f592009-10-29 16:34:54 +00001351 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001352 return -EINVAL;
1353 }
1354
Emilio G. Cota886953e2010-11-12 11:14:07 +00001355 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001356
1357 if (bridge->irq[level - 1].callback[statid].func) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001358 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001359 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 Welch29848ac2010-02-18 15:13:05 +00001368 bridge->irq_set(bridge, level, 1, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001369
Emilio G. Cota886953e2010-11-12 11:14:07 +00001370 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001371
1372 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001373}
Martyn Welchc813f592009-10-29 16:34:54 +00001374EXPORT_SYMBOL(vme_irq_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001375
Martyn Welchb5bc9802017-03-04 00:34:29 +00001376/**
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 Vanga8f966dc2011-09-26 11:27:15 +02001384void vme_irq_free(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001385{
1386 struct vme_bridge *bridge;
1387
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001388 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001389 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001390 printk(KERN_ERR "Can't find VME bus\n");
1391 return;
1392 }
1393
Martyn Welchead1f3e2009-12-15 08:43:02 +00001394 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001395 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001396 return;
1397 }
1398
Markus Elfring61282c02017-08-24 22:24:38 +02001399 if (!bridge->irq_set) {
Martyn Welchc813f592009-10-29 16:34:54 +00001400 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001401 return;
1402 }
1403
Emilio G. Cota886953e2010-11-12 11:14:07 +00001404 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001405
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 Welch29848ac2010-02-18 15:13:05 +00001410 bridge->irq_set(bridge, level, 0, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001411
1412 bridge->irq[level - 1].callback[statid].func = NULL;
1413 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1414
Emilio G. Cota886953e2010-11-12 11:14:07 +00001415 mutex_unlock(&bridge->irq_mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001416}
Martyn Welchc813f592009-10-29 16:34:54 +00001417EXPORT_SYMBOL(vme_irq_free);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001418
Martyn Welchb5bc9802017-03-04 00:34:29 +00001419/**
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 Vanga8f966dc2011-09-26 11:27:15 +02001431int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001432{
1433 struct vme_bridge *bridge;
1434
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001435 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001436 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001437 printk(KERN_ERR "Can't find VME bus\n");
1438 return -EINVAL;
1439 }
1440
Martyn Welchead1f3e2009-12-15 08:43:02 +00001441 if ((level < 1) || (level > 7)) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001442 printk(KERN_WARNING "Invalid interrupt level\n");
1443 return -EINVAL;
1444 }
1445
Markus Elfring61282c02017-08-24 22:24:38 +02001446 if (!bridge->irq_generate) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001447 printk(KERN_WARNING "Interrupt generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001448 return -EINVAL;
1449 }
1450
Martyn Welch29848ac2010-02-18 15:13:05 +00001451 return bridge->irq_generate(bridge, level, statid);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001452}
Martyn Welchc813f592009-10-29 16:34:54 +00001453EXPORT_SYMBOL(vme_irq_generate);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001454
Martyn Welchb5bc9802017-03-04 00:34:29 +00001455/**
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 Welch42fb5032009-08-11 17:44:56 +01001464 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001465struct vme_resource *vme_lm_request(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001466{
1467 struct vme_bridge *bridge;
Martyn Welch42fb5032009-08-11 17:44:56 +01001468 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 Welcha17a75e2009-07-31 09:28:17 +01001472
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001473 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001474 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001475 printk(KERN_ERR "Can't find VME bus\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001476 goto err_bus;
1477 }
1478
Martyn Welchb5bc9802017-03-04 00:34:29 +00001479 /* Loop through LM resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001480 list_for_each(lm_pos, &bridge->lm_resources) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001481 lm = list_entry(lm_pos,
1482 struct vme_lm_resource, list);
Markus Elfring61282c02017-08-24 22:24:38 +02001483 if (!lm) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001484 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001485 continue;
1486 }
1487
1488 /* Find an unlocked controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001489 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001490 if (lm->locked == 0) {
1491 lm->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001492 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001493 allocated_lm = lm;
1494 break;
1495 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001496 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001497 }
1498
1499 /* Check to see if we found a resource */
Markus Elfring61282c02017-08-24 22:24:38 +02001500 if (!allocated_lm)
Martyn Welch42fb5032009-08-11 17:44:56 +01001501 goto err_lm;
1502
Markus Elfring1ff0a192017-08-24 21:52:00 +02001503 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001504 if (!resource)
Martyn Welch42fb5032009-08-11 17:44:56 +01001505 goto err_alloc;
Markus Elfring94eefcc2017-08-24 21:38:20 +02001506
Martyn Welch42fb5032009-08-11 17:44:56 +01001507 resource->type = VME_LM;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001508 resource->entry = &allocated_lm->list;
Martyn Welch42fb5032009-08-11 17:44:56 +01001509
1510 return resource;
1511
1512err_alloc:
1513 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001514 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001515 lm->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001516 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001517err_lm:
1518err_bus:
1519 return NULL;
1520}
1521EXPORT_SYMBOL(vme_lm_request);
1522
Martyn Welchb5bc9802017-03-04 00:34:29 +00001523/**
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 Welch42fb5032009-08-11 17:44:56 +01001534int 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 Welcha17a75e2009-07-31 09:28:17 +01001540 return -EINVAL;
1541 }
1542
Martyn Welch42fb5032009-08-11 17:44:56 +01001543 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1544
1545 return lm->monitors;
1546}
1547EXPORT_SYMBOL(vme_lm_count);
1548
Martyn Welchb5bc9802017-03-04 00:34:29 +00001549/**
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 Welch42fb5032009-08-11 17:44:56 +01001563int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001564 u32 aspace, u32 cycle)
Martyn Welch42fb5032009-08-11 17:44:56 +01001565{
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 Elfring61282c02017-08-24 22:24:38 +02001576 if (!bridge->lm_set) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001577 printk(KERN_ERR "vme_lm_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001578 return -EINVAL;
1579 }
1580
Martyn Welch8be92262009-10-29 16:35:08 +00001581 return bridge->lm_set(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001582}
1583EXPORT_SYMBOL(vme_lm_set);
1584
Martyn Welchb5bc9802017-03-04 00:34:29 +00001585/**
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 Welch42fb5032009-08-11 17:44:56 +01001599int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001600 u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001601{
Martyn Welch42fb5032009-08-11 17:44:56 +01001602 struct vme_bridge *bridge = find_bridge(resource);
1603 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001604
Martyn Welch42fb5032009-08-11 17:44:56 +01001605 if (resource->type != VME_LM) {
1606 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001607 return -EINVAL;
1608 }
1609
Martyn Welch42fb5032009-08-11 17:44:56 +01001610 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1611
Markus Elfring61282c02017-08-24 22:24:38 +02001612 if (!bridge->lm_get) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001613 printk(KERN_ERR "vme_lm_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001614 return -EINVAL;
1615 }
1616
Martyn Welch42fb5032009-08-11 17:44:56 +01001617 return bridge->lm_get(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001618}
1619EXPORT_SYMBOL(vme_lm_get);
1620
Martyn Welchb5bc9802017-03-04 00:34:29 +00001621/**
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 Welch42fb5032009-08-11 17:44:56 +01001636int vme_lm_attach(struct vme_resource *resource, int monitor,
Aaron Sierrafa54b322016-04-29 16:41:02 -05001637 void (*callback)(void *), void *data)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001638{
Martyn Welch42fb5032009-08-11 17:44:56 +01001639 struct vme_bridge *bridge = find_bridge(resource);
1640 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001641
Martyn Welch42fb5032009-08-11 17:44:56 +01001642 if (resource->type != VME_LM) {
1643 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001644 return -EINVAL;
1645 }
1646
Martyn Welch42fb5032009-08-11 17:44:56 +01001647 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1648
Markus Elfring61282c02017-08-24 22:24:38 +02001649 if (!bridge->lm_attach) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001650 printk(KERN_ERR "vme_lm_attach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001651 return -EINVAL;
1652 }
1653
Aaron Sierrafa54b322016-04-29 16:41:02 -05001654 return bridge->lm_attach(lm, monitor, callback, data);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001655}
1656EXPORT_SYMBOL(vme_lm_attach);
1657
Martyn Welchb5bc9802017-03-04 00:34:29 +00001658/**
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 Welch42fb5032009-08-11 17:44:56 +01001670int vme_lm_detach(struct vme_resource *resource, int monitor)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001671{
Martyn Welch42fb5032009-08-11 17:44:56 +01001672 struct vme_bridge *bridge = find_bridge(resource);
1673 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001674
Martyn Welch42fb5032009-08-11 17:44:56 +01001675 if (resource->type != VME_LM) {
1676 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001677 return -EINVAL;
1678 }
1679
Martyn Welch42fb5032009-08-11 17:44:56 +01001680 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1681
Markus Elfring61282c02017-08-24 22:24:38 +02001682 if (!bridge->lm_detach) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001683 printk(KERN_ERR "vme_lm_detach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001684 return -EINVAL;
1685 }
1686
Martyn Welch42fb5032009-08-11 17:44:56 +01001687 return bridge->lm_detach(lm, monitor);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001688}
1689EXPORT_SYMBOL(vme_lm_detach);
1690
Martyn Welchb5bc9802017-03-04 00:34:29 +00001691/**
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 Welch42fb5032009-08-11 17:44:56 +01001703void 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. Cota886953e2010-11-12 11:14:07 +00001714 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001715
Martyn Welch8be92262009-10-29 16:35:08 +00001716 /* 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 Welch42fb5032009-08-11 17:44:56 +01001720
1721 lm->locked = 0;
1722
Emilio G. Cota886953e2010-11-12 11:14:07 +00001723 mutex_unlock(&lm->mtx);
Martyn Welch8be92262009-10-29 16:35:08 +00001724
1725 kfree(resource);
Martyn Welch42fb5032009-08-11 17:44:56 +01001726}
1727EXPORT_SYMBOL(vme_lm_free);
1728
Martyn Welchb5bc9802017-03-04 00:34:29 +00001729/**
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 Welchd7729f02013-11-08 11:58:35 +00001739int vme_slot_num(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001740{
1741 struct vme_bridge *bridge;
1742
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001743 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001744 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001745 printk(KERN_ERR "Can't find VME bus\n");
1746 return -EINVAL;
1747 }
1748
Markus Elfring61282c02017-08-24 22:24:38 +02001749 if (!bridge->slot_get) {
Martyn Welchd7729f02013-11-08 11:58:35 +00001750 printk(KERN_WARNING "vme_slot_num not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001751 return -EINVAL;
1752 }
1753
Martyn Welch29848ac2010-02-18 15:13:05 +00001754 return bridge->slot_get(bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001755}
Martyn Welchd7729f02013-11-08 11:58:35 +00001756EXPORT_SYMBOL(vme_slot_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001757
Martyn Welchb5bc9802017-03-04 00:34:29 +00001758/**
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 Welch978f47d2013-11-08 11:58:34 +00001767int vme_bus_num(struct vme_dev *vdev)
1768{
1769 struct vme_bridge *bridge;
1770
1771 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001772 if (!bridge) {
Martyn Welch978f47d2013-11-08 11:58:34 +00001773 pr_err("Can't find VME bus\n");
1774 return -EINVAL;
1775 }
1776
1777 return bridge->num;
1778}
1779EXPORT_SYMBOL(vme_bus_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001780
1781/* - Bridge Registration --------------------------------------------------- */
1782
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001783static void vme_dev_release(struct device *dev)
1784{
1785 kfree(dev_to_vme_dev(dev));
1786}
1787
Aaron Sierra326071b2016-04-24 15:11:38 -05001788/* Common bridge initialization */
1789struct 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}
1800EXPORT_SYMBOL(vme_init_bridge);
1801
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001802int vme_register_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001803{
1804 int i;
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001805 int ret = -1;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001806
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001807 mutex_lock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001808 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001809 if ((vme_bus_numbers & (1 << i)) == 0) {
1810 vme_bus_numbers |= (1 << i);
1811 bridge->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001812 INIT_LIST_HEAD(&bridge->devices);
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001813 list_add_tail(&bridge->bus_list, &vme_bus_list);
1814 ret = 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001815 break;
1816 }
1817 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001818 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001819
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001820 return ret;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001821}
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001822EXPORT_SYMBOL(vme_register_bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001823
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001824void vme_unregister_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001825{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001826 struct vme_dev *vdev;
1827 struct vme_dev *tmp;
1828
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001829 mutex_lock(&vme_buses_lock);
1830 vme_bus_numbers &= ~(1 << bridge->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001831 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 Vanga733e3ef2011-08-12 12:30:48 +02001836 list_del(&bridge->bus_list);
1837 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001838}
Martyn Welcha17a75e2009-07-31 09:28:17 +01001839EXPORT_SYMBOL(vme_unregister_bridge);
1840
Martyn Welcha17a75e2009-07-31 09:28:17 +01001841/* - Driver Registration --------------------------------------------------- */
1842
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001843static int __vme_register_driver_bus(struct vme_driver *drv,
1844 struct vme_bridge *bridge, unsigned int ndevs)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001845{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001846 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 Elfring1ff0a192017-08-24 21:52:00 +02001852 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001853 if (!vdev) {
1854 err = -ENOMEM;
1855 goto err_devalloc;
1856 }
Manohar Vangaa916a392011-09-26 11:27:17 +02001857 vdev->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001858 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 Vangaa916a392011-09-26 11:27:17 +02001863 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1864 vdev->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001865
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
1878err_reg:
Emilio G. Cotadef18202013-02-13 13:47:54 -05001879 put_device(&vdev->dev);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001880err_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
1889static 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 Vangac26f6112011-11-04 11:12:29 +01001901 * will have to wait).
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001902 */
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 Welchb5bc9802017-03-04 00:34:29 +00001911/**
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 Vanga5d6abf32011-09-26 11:27:16 +02001920int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1921{
1922 int err;
1923
Martyn Welcha17a75e2009-07-31 09:28:17 +01001924 drv->driver.name = drv->name;
1925 drv->driver.bus = &vme_bus_type;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001926 INIT_LIST_HEAD(&drv->devices);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001927
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001928 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 Welcha17a75e2009-07-31 09:28:17 +01001937}
1938EXPORT_SYMBOL(vme_register_driver);
1939
Martyn Welchb5bc9802017-03-04 00:34:29 +00001940/**
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 Welchead1f3e2009-12-15 08:43:02 +00001946void vme_unregister_driver(struct vme_driver *drv)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001947{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001948 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 Welcha17a75e2009-07-31 09:28:17 +01001958 driver_unregister(&drv->driver);
1959}
1960EXPORT_SYMBOL(vme_unregister_driver);
1961
1962/* - Bus Registration ------------------------------------------------------ */
1963
Martyn Welcha17a75e2009-07-31 09:28:17 +01001964static int vme_bus_match(struct device *dev, struct device_driver *drv)
1965{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001966 struct vme_driver *vme_drv;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001967
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001968 vme_drv = container_of(drv, struct vme_driver, driver);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001969
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001970 if (dev->platform_data == vme_drv) {
1971 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001972
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001973 if (vme_drv->match && vme_drv->match(vdev))
1974 return 1;
1975
1976 dev->platform_data = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001977 }
Martyn Welcha17a75e2009-07-31 09:28:17 +01001978 return 0;
1979}
1980
1981static int vme_bus_probe(struct device *dev)
1982{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001983 struct vme_driver *driver;
1984 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001985
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001986 driver = dev->platform_data;
Markus Elfring61282c02017-08-24 22:24:38 +02001987 if (driver->probe)
Markus Elfring8af70cd92017-08-24 22:32:14 +02001988 return driver->probe(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001989
Markus Elfring8af70cd92017-08-24 22:32:14 +02001990 return -ENODEV;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001991}
1992
Stefano Babic97974842017-01-20 10:38:20 -05001993static int vme_bus_remove(struct device *dev)
1994{
Stefano Babic97974842017-01-20 10:38:20 -05001995 struct vme_driver *driver;
1996 struct vme_dev *vdev = dev_to_vme_dev(dev);
1997
1998 driver = dev->platform_data;
Markus Elfring61282c02017-08-24 22:24:38 +02001999 if (driver->remove)
Uwe Kleine-König2adc75f2021-01-27 22:23:29 +01002000 driver->remove(vdev);
Stefano Babic97974842017-01-20 10:38:20 -05002001
Uwe Kleine-König2adc75f2021-01-27 22:23:29 +01002002 return 0;
Stefano Babic97974842017-01-20 10:38:20 -05002003}
2004
Martyn Welcha17a75e2009-07-31 09:28:17 +01002005struct bus_type vme_bus_type = {
2006 .name = "vme",
2007 .match = vme_bus_match,
2008 .probe = vme_bus_probe,
Stefano Babic97974842017-01-20 10:38:20 -05002009 .remove = vme_bus_remove,
Martyn Welcha17a75e2009-07-31 09:28:17 +01002010};
2011EXPORT_SYMBOL(vme_bus_type);
2012
Martyn Welchead1f3e2009-12-15 08:43:02 +00002013static int __init vme_init(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01002014{
2015 return bus_register(&vme_bus_type);
2016}
Aaron Sierrac326cc02013-12-09 09:54:42 -06002017subsys_initcall(vme_init);