blob: 6a3ead42aba876cef534b8e9acc34b30cf6cef3a [file] [log] [blame]
Martyn Welcha17a75e2009-07-31 09:28:17 +01001/*
2 * VME Bridge Framework
3 *
Martyn Welch66bd8db2010-02-18 15:12:52 +00004 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
Martyn Welcha17a75e2009-07-31 09:28:17 +01006 *
7 * Based on work by Tom Armistead and Ajit Prem
8 * Copyright 2004 Motorola Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 */
15
Paul Gortmaker050c3d52016-07-03 14:05:56 -040016#include <linux/init.h>
17#include <linux/export.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010018#include <linux/mm.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/pci.h>
23#include <linux/poll.h>
24#include <linux/highmem.h>
25#include <linux/interrupt.h>
26#include <linux/pagemap.h>
27#include <linux/device.h>
28#include <linux/dma-mapping.h>
29#include <linux/syscalls.h>
Martyn Welch400822f2009-08-11 16:20:22 +010030#include <linux/mutex.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010031#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Greg Kroah-Hartmandb3b9e92012-04-26 12:34:58 -070033#include <linux/vme.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010034
Martyn Welcha17a75e2009-07-31 09:28:17 +010035#include "vme_bridge.h"
36
Manohar Vanga733e3ef2011-08-12 12:30:48 +020037/* Bitmask and list of registered buses both protected by common mutex */
Martyn Welcha17a75e2009-07-31 09:28:17 +010038static unsigned int vme_bus_numbers;
Manohar Vanga733e3ef2011-08-12 12:30:48 +020039static LIST_HEAD(vme_bus_list);
40static DEFINE_MUTEX(vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +010041
Martyn Welchead1f3e2009-12-15 08:43:02 +000042static int __init vme_init(void);
Martyn Welcha17a75e2009-07-31 09:28:17 +010043
Manohar Vanga8f966dc2011-09-26 11:27:15 +020044static struct vme_dev *dev_to_vme_dev(struct device *dev)
Martyn Welcha17a75e2009-07-31 09:28:17 +010045{
Manohar Vanga8f966dc2011-09-26 11:27:15 +020046 return container_of(dev, struct vme_dev, dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +010047}
48
49/*
50 * Find the bridge that the resource is associated with.
51 */
52static struct vme_bridge *find_bridge(struct vme_resource *resource)
53{
54 /* Get list to search */
55 switch (resource->type) {
56 case VME_MASTER:
57 return list_entry(resource->entry, struct vme_master_resource,
58 list)->parent;
59 break;
60 case VME_SLAVE:
61 return list_entry(resource->entry, struct vme_slave_resource,
62 list)->parent;
63 break;
64 case VME_DMA:
65 return list_entry(resource->entry, struct vme_dma_resource,
66 list)->parent;
67 break;
Martyn Welch42fb5032009-08-11 17:44:56 +010068 case VME_LM:
69 return list_entry(resource->entry, struct vme_lm_resource,
70 list)->parent;
71 break;
Martyn Welcha17a75e2009-07-31 09:28:17 +010072 default:
73 printk(KERN_ERR "Unknown resource type\n");
74 return NULL;
75 break;
76 }
77}
78
Martyn Welchb5bc9802017-03-04 00:34:29 +000079/**
80 * vme_free_consistent - Allocate contiguous memory.
81 * @resource: Pointer to VME resource.
82 * @size: Size of allocation required.
83 * @dma: Pointer to variable to store physical address of allocation.
84 *
Martyn Welcha17a75e2009-07-31 09:28:17 +010085 * Allocate a contiguous block of memory for use by the driver. This is used to
86 * create the buffers for the slave windows.
Martyn Welchb5bc9802017-03-04 00:34:29 +000087 *
88 * Return: Virtual address of allocation on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +010089 */
Martyn Welchead1f3e2009-12-15 08:43:02 +000090void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
Martyn Welcha17a75e2009-07-31 09:28:17 +010091 dma_addr_t *dma)
92{
93 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +010094
Martyn Welchead1f3e2009-12-15 08:43:02 +000095 if (resource == NULL) {
96 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +010097 return NULL;
98 }
99
100 bridge = find_bridge(resource);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000101 if (bridge == NULL) {
102 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100103 return NULL;
104 }
105
Martyn Welcha17a75e2009-07-31 09:28:17 +0100106 if (bridge->parent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700107 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100108 return NULL;
109 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100110
Manohar Vanga7f58f022011-08-10 11:33:46 +0200111 if (bridge->alloc_consistent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700112 printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
113 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200114 return NULL;
115 }
116
117 return bridge->alloc_consistent(bridge->parent, size, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100118}
119EXPORT_SYMBOL(vme_alloc_consistent);
120
Martyn Welchb5bc9802017-03-04 00:34:29 +0000121/**
122 * vme_free_consistent - Free previously allocated memory.
123 * @resource: Pointer to VME resource.
124 * @size: Size of allocation to free.
125 * @vaddr: Virtual address of allocation.
126 * @dma: Physical address of allocation.
127 *
128 * Free previously allocated block of contiguous memory.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100129 */
130void vme_free_consistent(struct vme_resource *resource, size_t size,
131 void *vaddr, dma_addr_t dma)
132{
133 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100134
Martyn Welchead1f3e2009-12-15 08:43:02 +0000135 if (resource == NULL) {
136 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100137 return;
138 }
139
140 bridge = find_bridge(resource);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000141 if (bridge == NULL) {
142 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100143 return;
144 }
145
Manohar Vanga7f58f022011-08-10 11:33:46 +0200146 if (bridge->parent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700147 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200148 return;
149 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100150
Manohar Vanga7f58f022011-08-10 11:33:46 +0200151 if (bridge->free_consistent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700152 printk(KERN_ERR "free_consistent not supported by bridge %s\n",
153 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200154 return;
155 }
156
157 bridge->free_consistent(bridge->parent, size, vaddr, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100158}
159EXPORT_SYMBOL(vme_free_consistent);
160
Martyn Welchb5bc9802017-03-04 00:34:29 +0000161/**
162 * vme_get_size - Helper function returning size of a VME window
163 * @resource: Pointer to VME slave or master resource.
164 *
165 * Determine the size of the VME window provided. This is a helper
166 * function, wrappering the call to vme_master_get or vme_slave_get
167 * depending on the type of window resource handed to it.
168 *
169 * Return: Size of the window on success, zero on failure.
170 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100171size_t vme_get_size(struct vme_resource *resource)
172{
173 int enabled, retval;
174 unsigned long long base, size;
175 dma_addr_t buf_base;
Martyn Welch6af04b02011-12-01 17:06:29 +0000176 u32 aspace, cycle, dwidth;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100177
178 switch (resource->type) {
179 case VME_MASTER:
180 retval = vme_master_get(resource, &enabled, &base, &size,
181 &aspace, &cycle, &dwidth);
Martyn Welch6ad37562016-10-21 17:36:19 +0100182 if (retval)
183 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100184
185 return size;
186 break;
187 case VME_SLAVE:
188 retval = vme_slave_get(resource, &enabled, &base, &size,
189 &buf_base, &aspace, &cycle);
Martyn Welch6ad37562016-10-21 17:36:19 +0100190 if (retval)
191 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100192
193 return size;
194 break;
195 case VME_DMA:
196 return 0;
197 break;
198 default:
199 printk(KERN_ERR "Unknown resource type\n");
200 return 0;
201 break;
202 }
203}
204EXPORT_SYMBOL(vme_get_size);
205
Dmitry Kalinkinef73f882015-05-28 15:07:04 +0300206int vme_check_window(u32 aspace, unsigned long long vme_base,
207 unsigned long long size)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100208{
209 int retval = 0;
210
211 switch (aspace) {
212 case VME_A16:
213 if (((vme_base + size) > VME_A16_MAX) ||
214 (vme_base > VME_A16_MAX))
215 retval = -EFAULT;
216 break;
217 case VME_A24:
218 if (((vme_base + size) > VME_A24_MAX) ||
219 (vme_base > VME_A24_MAX))
220 retval = -EFAULT;
221 break;
222 case VME_A32:
223 if (((vme_base + size) > VME_A32_MAX) ||
224 (vme_base > VME_A32_MAX))
225 retval = -EFAULT;
226 break;
227 case VME_A64:
Dmitry Kalinkine7fd80c2015-05-28 15:07:03 +0300228 if ((size != 0) && (vme_base > U64_MAX + 1 - size))
229 retval = -EFAULT;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100230 break;
231 case VME_CRCSR:
232 if (((vme_base + size) > VME_CRCSR_MAX) ||
233 (vme_base > VME_CRCSR_MAX))
234 retval = -EFAULT;
235 break;
236 case VME_USER1:
237 case VME_USER2:
238 case VME_USER3:
239 case VME_USER4:
240 /* User Defined */
241 break;
242 default:
Martyn Welchead1f3e2009-12-15 08:43:02 +0000243 printk(KERN_ERR "Invalid address space\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100244 retval = -EINVAL;
245 break;
246 }
247
248 return retval;
249}
Dmitry Kalinkinef73f882015-05-28 15:07:04 +0300250EXPORT_SYMBOL(vme_check_window);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100251
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +0300252static u32 vme_get_aspace(int am)
253{
254 switch (am) {
255 case 0x29:
256 case 0x2D:
257 return VME_A16;
258 case 0x38:
259 case 0x39:
260 case 0x3A:
261 case 0x3B:
262 case 0x3C:
263 case 0x3D:
264 case 0x3E:
265 case 0x3F:
266 return VME_A24;
267 case 0x8:
268 case 0x9:
269 case 0xA:
270 case 0xB:
271 case 0xC:
272 case 0xD:
273 case 0xE:
274 case 0xF:
275 return VME_A32;
276 case 0x0:
277 case 0x1:
278 case 0x3:
279 return VME_A64;
280 }
281
282 return 0;
283}
284
Martyn Welchb5bc9802017-03-04 00:34:29 +0000285/**
286 * vme_slave_request - Request a VME slave window resource.
287 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
288 * @address: Required VME address space.
289 * @cycle: Required VME data transfer cycle type.
290 *
291 * Request use of a VME window resource capable of being set for the requested
292 * address space and data transfer cycle.
293 *
294 * Return: Pointer to VME resource on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100295 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000296struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
297 u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100298{
299 struct vme_bridge *bridge;
300 struct list_head *slave_pos = NULL;
301 struct vme_slave_resource *allocated_image = NULL;
302 struct vme_slave_resource *slave_image = NULL;
303 struct vme_resource *resource = NULL;
304
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200305 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100306 if (bridge == NULL) {
307 printk(KERN_ERR "Can't find VME bus\n");
308 goto err_bus;
309 }
310
311 /* Loop through slave resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000312 list_for_each(slave_pos, &bridge->slave_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100313 slave_image = list_entry(slave_pos,
314 struct vme_slave_resource, list);
315
316 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000317 printk(KERN_ERR "Registered NULL Slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100318 continue;
319 }
320
321 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000322 mutex_lock(&slave_image->mtx);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000323 if (((slave_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100324 ((slave_image->cycle_attr & cycle) == cycle) &&
325 (slave_image->locked == 0)) {
326
327 slave_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000328 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100329 allocated_image = slave_image;
330 break;
331 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000332 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100333 }
334
335 /* No free image */
336 if (allocated_image == NULL)
337 goto err_image;
338
339 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
340 if (resource == NULL) {
341 printk(KERN_WARNING "Unable to allocate resource structure\n");
342 goto err_alloc;
343 }
344 resource->type = VME_SLAVE;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000345 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100346
347 return resource;
348
349err_alloc:
350 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000351 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100352 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000353 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100354err_image:
355err_bus:
356 return NULL;
357}
358EXPORT_SYMBOL(vme_slave_request);
359
Martyn Welchb5bc9802017-03-04 00:34:29 +0000360/**
361 * vme_slave_set - Set VME slave window configuration.
362 * @resource: Pointer to VME slave resource.
363 * @enabled: State to which the window should be configured.
364 * @vme_base: Base address for the window.
365 * @size: Size of the VME window.
366 * @buf_base: Based address of buffer used to provide VME slave window storage.
367 * @aspace: VME address space for the VME window.
368 * @cycle: VME data transfer cycle type for the VME window.
369 *
370 * Set configuration for provided VME slave window.
371 *
372 * Return: Zero on success, -EINVAL if operation is not supported on this
373 * device, if an invalid resource has been provided or invalid
374 * attributes are provided. Hardware specific errors may also be
375 * returned.
376 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000377int vme_slave_set(struct vme_resource *resource, int enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100378 unsigned long long vme_base, unsigned long long size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000379 dma_addr_t buf_base, u32 aspace, u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100380{
381 struct vme_bridge *bridge = find_bridge(resource);
382 struct vme_slave_resource *image;
383 int retval;
384
385 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000386 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100387 return -EINVAL;
388 }
389
390 image = list_entry(resource->entry, struct vme_slave_resource, list);
391
392 if (bridge->slave_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000393 printk(KERN_ERR "Function not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100394 return -ENOSYS;
395 }
396
Martyn Welchead1f3e2009-12-15 08:43:02 +0000397 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100398 ((image->cycle_attr & cycle) == cycle))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000399 printk(KERN_ERR "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100400 return -EINVAL;
401 }
402
403 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000404 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100405 return retval;
406
407 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
408 aspace, cycle);
409}
410EXPORT_SYMBOL(vme_slave_set);
411
Martyn Welchb5bc9802017-03-04 00:34:29 +0000412/**
413 * vme_slave_get - Retrieve VME slave window configuration.
414 * @resource: Pointer to VME slave resource.
415 * @enabled: Pointer to variable for storing state.
416 * @vme_base: Pointer to variable for storing window base address.
417 * @size: Pointer to variable for storing window size.
418 * @buf_base: Pointer to variable for storing slave buffer base address.
419 * @aspace: Pointer to variable for storing VME address space.
420 * @cycle: Pointer to variable for storing VME data transfer cycle type.
421 *
422 * Return configuration for provided VME slave window.
423 *
424 * Return: Zero on success, -EINVAL if operation is not supported on this
425 * device or if an invalid resource has been provided.
426 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000427int vme_slave_get(struct vme_resource *resource, int *enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100428 unsigned long long *vme_base, unsigned long long *size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000429 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100430{
431 struct vme_bridge *bridge = find_bridge(resource);
432 struct vme_slave_resource *image;
433
434 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000435 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100436 return -EINVAL;
437 }
438
439 image = list_entry(resource->entry, struct vme_slave_resource, list);
440
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200441 if (bridge->slave_get == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000442 printk(KERN_ERR "vme_slave_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100443 return -EINVAL;
444 }
445
446 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
447 aspace, cycle);
448}
449EXPORT_SYMBOL(vme_slave_get);
450
Martyn Welchb5bc9802017-03-04 00:34:29 +0000451/**
452 * vme_slave_free - Free VME slave window
453 * @resource: Pointer to VME slave resource.
454 *
455 * Free the provided slave resource so that it may be reallocated.
456 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100457void vme_slave_free(struct vme_resource *resource)
458{
459 struct vme_slave_resource *slave_image;
460
461 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000462 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100463 return;
464 }
465
466 slave_image = list_entry(resource->entry, struct vme_slave_resource,
467 list);
468 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000469 printk(KERN_ERR "Can't find slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100470 return;
471 }
472
473 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000474 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100475 if (slave_image->locked == 0)
476 printk(KERN_ERR "Image is already free\n");
477
478 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000479 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100480
481 /* Free up resource memory */
482 kfree(resource);
483}
484EXPORT_SYMBOL(vme_slave_free);
485
Martyn Welchb5bc9802017-03-04 00:34:29 +0000486/**
487 * vme_master_request - Request a VME master window resource.
488 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
489 * @address: Required VME address space.
490 * @cycle: Required VME data transfer cycle type.
491 * @dwidth: Required VME data transfer width.
492 *
493 * Request use of a VME window resource capable of being set for the requested
494 * address space, data transfer cycle and width.
495 *
496 * Return: Pointer to VME resource on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100497 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000498struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
499 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100500{
501 struct vme_bridge *bridge;
502 struct list_head *master_pos = NULL;
503 struct vme_master_resource *allocated_image = NULL;
504 struct vme_master_resource *master_image = NULL;
505 struct vme_resource *resource = NULL;
506
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200507 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100508 if (bridge == NULL) {
509 printk(KERN_ERR "Can't find VME bus\n");
510 goto err_bus;
511 }
512
513 /* Loop through master resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000514 list_for_each(master_pos, &bridge->master_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100515 master_image = list_entry(master_pos,
516 struct vme_master_resource, list);
517
518 if (master_image == NULL) {
519 printk(KERN_WARNING "Registered NULL master resource\n");
520 continue;
521 }
522
523 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000524 spin_lock(&master_image->lock);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000525 if (((master_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100526 ((master_image->cycle_attr & cycle) == cycle) &&
527 ((master_image->width_attr & dwidth) == dwidth) &&
528 (master_image->locked == 0)) {
529
530 master_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000531 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100532 allocated_image = master_image;
533 break;
534 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000535 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100536 }
537
538 /* Check to see if we found a resource */
539 if (allocated_image == NULL) {
540 printk(KERN_ERR "Can't find a suitable resource\n");
541 goto err_image;
542 }
543
544 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
545 if (resource == NULL) {
546 printk(KERN_ERR "Unable to allocate resource structure\n");
547 goto err_alloc;
548 }
549 resource->type = VME_MASTER;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000550 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100551
552 return resource;
553
Martyn Welcha17a75e2009-07-31 09:28:17 +0100554err_alloc:
555 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000556 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100557 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000558 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100559err_image:
560err_bus:
561 return NULL;
562}
563EXPORT_SYMBOL(vme_master_request);
564
Martyn Welchb5bc9802017-03-04 00:34:29 +0000565/**
566 * vme_master_set - Set VME master window configuration.
567 * @resource: Pointer to VME master resource.
568 * @enabled: State to which the window should be configured.
569 * @vme_base: Base address for the window.
570 * @size: Size of the VME window.
571 * @aspace: VME address space for the VME window.
572 * @cycle: VME data transfer cycle type for the VME window.
573 * @dwidth: VME data transfer width for the VME window.
574 *
575 * Set configuration for provided VME master window.
576 *
577 * Return: Zero on success, -EINVAL if operation is not supported on this
578 * device, if an invalid resource has been provided or invalid
579 * attributes are provided. Hardware specific errors may also be
580 * returned.
581 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000582int vme_master_set(struct vme_resource *resource, int enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000583 unsigned long long vme_base, unsigned long long size, u32 aspace,
584 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100585{
586 struct vme_bridge *bridge = find_bridge(resource);
587 struct vme_master_resource *image;
588 int retval;
589
590 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000591 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100592 return -EINVAL;
593 }
594
595 image = list_entry(resource->entry, struct vme_master_resource, list);
596
597 if (bridge->master_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000598 printk(KERN_WARNING "vme_master_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100599 return -EINVAL;
600 }
601
Martyn Welchead1f3e2009-12-15 08:43:02 +0000602 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100603 ((image->cycle_attr & cycle) == cycle) &&
604 ((image->width_attr & dwidth) == dwidth))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000605 printk(KERN_WARNING "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100606 return -EINVAL;
607 }
608
609 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000610 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100611 return retval;
612
613 return bridge->master_set(image, enabled, vme_base, size, aspace,
614 cycle, dwidth);
615}
616EXPORT_SYMBOL(vme_master_set);
617
Martyn Welchb5bc9802017-03-04 00:34:29 +0000618/**
619 * vme_master_get - Retrieve VME master window configuration.
620 * @resource: Pointer to VME master resource.
621 * @enabled: Pointer to variable for storing state.
622 * @vme_base: Pointer to variable for storing window base address.
623 * @size: Pointer to variable for storing window size.
624 * @aspace: Pointer to variable for storing VME address space.
625 * @cycle: Pointer to variable for storing VME data transfer cycle type.
626 * @dwidth: Pointer to variable for storing VME data transfer width.
627 *
628 * Return configuration for provided VME master window.
629 *
630 * Return: Zero on success, -EINVAL if operation is not supported on this
631 * device or if an invalid resource has been provided.
632 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000633int vme_master_get(struct vme_resource *resource, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000634 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
635 u32 *cycle, u32 *dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100636{
637 struct vme_bridge *bridge = find_bridge(resource);
638 struct vme_master_resource *image;
639
640 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000641 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100642 return -EINVAL;
643 }
644
645 image = list_entry(resource->entry, struct vme_master_resource, list);
646
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200647 if (bridge->master_get == NULL) {
Julia Lawallc1038302014-12-07 20:20:53 +0100648 printk(KERN_WARNING "%s not supported\n", __func__);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100649 return -EINVAL;
650 }
651
652 return bridge->master_get(image, enabled, vme_base, size, aspace,
653 cycle, dwidth);
654}
655EXPORT_SYMBOL(vme_master_get);
656
Martyn Welchb5bc9802017-03-04 00:34:29 +0000657/**
658 * vme_master_write - Read data from VME space into a buffer.
659 * @resource: Pointer to VME master resource.
660 * @buf: Pointer to buffer where data should be transferred.
661 * @count: Number of bytes to transfer.
662 * @offset: Offset into VME master window at which to start transfer.
663 *
664 * Perform read of count bytes of data from location on VME bus which maps into
665 * the VME master window at offset to buf.
666 *
667 * Return: Number of bytes read, -EINVAL if resource is not a VME master
668 * resource or read operation is not supported. -EFAULT returned if
669 * invalid offset is provided. Hardware specific errors may also be
670 * returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100671 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000672ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100673 loff_t offset)
674{
675 struct vme_bridge *bridge = find_bridge(resource);
676 struct vme_master_resource *image;
677 size_t length;
678
679 if (bridge->master_read == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000680 printk(KERN_WARNING "Reading from resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100681 return -EINVAL;
682 }
683
684 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000685 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100686 return -EINVAL;
687 }
688
689 image = list_entry(resource->entry, struct vme_master_resource, list);
690
691 length = vme_get_size(resource);
692
693 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000694 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100695 return -EFAULT;
696 }
697
698 if ((offset + count) > length)
699 count = length - offset;
700
701 return bridge->master_read(image, buf, count, offset);
702
703}
704EXPORT_SYMBOL(vme_master_read);
705
Martyn Welchb5bc9802017-03-04 00:34:29 +0000706/**
707 * vme_master_write - Write data out to VME space from a buffer.
708 * @resource: Pointer to VME master resource.
709 * @buf: Pointer to buffer holding data to transfer.
710 * @count: Number of bytes to transfer.
711 * @offset: Offset into VME master window at which to start transfer.
712 *
713 * Perform write of count bytes of data from buf to location on VME bus which
714 * maps into the VME master window at offset.
715 *
716 * Return: Number of bytes written, -EINVAL if resource is not a VME master
717 * resource or write operation is not supported. -EFAULT returned if
718 * invalid offset is provided. Hardware specific errors may also be
719 * returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100720 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000721ssize_t vme_master_write(struct vme_resource *resource, void *buf,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100722 size_t count, loff_t offset)
723{
724 struct vme_bridge *bridge = find_bridge(resource);
725 struct vme_master_resource *image;
726 size_t length;
727
728 if (bridge->master_write == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000729 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100730 return -EINVAL;
731 }
732
733 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000734 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100735 return -EINVAL;
736 }
737
738 image = list_entry(resource->entry, struct vme_master_resource, list);
739
740 length = vme_get_size(resource);
741
742 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000743 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100744 return -EFAULT;
745 }
746
747 if ((offset + count) > length)
748 count = length - offset;
749
750 return bridge->master_write(image, buf, count, offset);
751}
752EXPORT_SYMBOL(vme_master_write);
753
Martyn Welchb5bc9802017-03-04 00:34:29 +0000754/**
755 * vme_master_rmw - Perform read-modify-write cycle.
756 * @resource: Pointer to VME master resource.
757 * @mask: Bits to be compared and swapped in operation.
758 * @compare: Bits to be compared with data read from offset.
759 * @swap: Bits to be swapped in data read from offset.
760 * @offset: Offset into VME master window at which to perform operation.
761 *
762 * Perform read-modify-write cycle on provided location:
763 * - Location on VME bus is read.
764 * - Bits selected by mask are compared with compare.
765 * - Where a selected bit matches that in compare and are selected in swap,
766 * the bit is swapped.
767 * - Result written back to location on VME bus.
768 *
769 * Return: Bytes written on success, -EINVAL if resource is not a VME master
770 * resource or RMW operation is not supported. Hardware specific
771 * errors may also be returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100772 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000773unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100774 unsigned int compare, unsigned int swap, loff_t offset)
775{
776 struct vme_bridge *bridge = find_bridge(resource);
777 struct vme_master_resource *image;
778
779 if (bridge->master_rmw == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000780 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100781 return -EINVAL;
782 }
783
784 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000785 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100786 return -EINVAL;
787 }
788
789 image = list_entry(resource->entry, struct vme_master_resource, list);
790
791 return bridge->master_rmw(image, mask, compare, swap, offset);
792}
793EXPORT_SYMBOL(vme_master_rmw);
794
Martyn Welchb5bc9802017-03-04 00:34:29 +0000795/**
796 * vme_master_mmap - Mmap region of VME master window.
797 * @resource: Pointer to VME master resource.
798 * @vma: Pointer to definition of user mapping.
799 *
800 * Memory map a region of the VME master window into user space.
801 *
802 * Return: Zero on success, -EINVAL if resource is not a VME master
803 * resource or -EFAULT if map exceeds window size. Other generic mmap
804 * errors may also be returned.
805 */
Dmitry Kalinkinc74a8042015-02-26 18:53:10 +0300806int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
807{
808 struct vme_master_resource *image;
809 phys_addr_t phys_addr;
810 unsigned long vma_size;
811
812 if (resource->type != VME_MASTER) {
813 pr_err("Not a master resource\n");
814 return -EINVAL;
815 }
816
817 image = list_entry(resource->entry, struct vme_master_resource, list);
818 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
819 vma_size = vma->vm_end - vma->vm_start;
820
821 if (phys_addr + vma_size > image->bus_resource.end + 1) {
822 pr_err("Map size cannot exceed the window size\n");
823 return -EFAULT;
824 }
825
826 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
827
828 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
829}
830EXPORT_SYMBOL(vme_master_mmap);
831
Martyn Welchb5bc9802017-03-04 00:34:29 +0000832/**
833 * vme_master_free - Free VME master window
834 * @resource: Pointer to VME master resource.
835 *
836 * Free the provided master resource so that it may be reallocated.
837 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100838void vme_master_free(struct vme_resource *resource)
839{
840 struct vme_master_resource *master_image;
841
842 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000843 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100844 return;
845 }
846
847 master_image = list_entry(resource->entry, struct vme_master_resource,
848 list);
849 if (master_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000850 printk(KERN_ERR "Can't find master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100851 return;
852 }
853
854 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000855 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100856 if (master_image->locked == 0)
857 printk(KERN_ERR "Image is already free\n");
858
859 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000860 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100861
862 /* Free up resource memory */
863 kfree(resource);
864}
865EXPORT_SYMBOL(vme_master_free);
866
Martyn Welchb5bc9802017-03-04 00:34:29 +0000867/**
868 * vme_dma_request - Request a DMA controller.
869 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
870 * @route: Required src/destination combination.
871 *
872 * Request a VME DMA controller with capability to perform transfers bewteen
873 * requested source/destination combination.
874 *
875 * Return: Pointer to VME DMA resource on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100876 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000877struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100878{
879 struct vme_bridge *bridge;
880 struct list_head *dma_pos = NULL;
881 struct vme_dma_resource *allocated_ctrlr = NULL;
882 struct vme_dma_resource *dma_ctrlr = NULL;
883 struct vme_resource *resource = NULL;
884
885 /* XXX Not checking resource attributes */
886 printk(KERN_ERR "No VME resource Attribute tests done\n");
887
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200888 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100889 if (bridge == NULL) {
890 printk(KERN_ERR "Can't find VME bus\n");
891 goto err_bus;
892 }
893
894 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000895 list_for_each(dma_pos, &bridge->dma_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100896 dma_ctrlr = list_entry(dma_pos,
897 struct vme_dma_resource, list);
898
899 if (dma_ctrlr == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000900 printk(KERN_ERR "Registered NULL DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100901 continue;
902 }
903
Martyn Welch4f723df2010-02-18 15:12:58 +0000904 /* Find an unlocked and compatible controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000905 mutex_lock(&dma_ctrlr->mtx);
Martyn Welch4f723df2010-02-18 15:12:58 +0000906 if (((dma_ctrlr->route_attr & route) == route) &&
907 (dma_ctrlr->locked == 0)) {
908
Martyn Welcha17a75e2009-07-31 09:28:17 +0100909 dma_ctrlr->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000910 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100911 allocated_ctrlr = dma_ctrlr;
912 break;
913 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000914 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100915 }
916
917 /* Check to see if we found a resource */
918 if (allocated_ctrlr == NULL)
919 goto err_ctrlr;
920
921 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
922 if (resource == NULL) {
923 printk(KERN_WARNING "Unable to allocate resource structure\n");
924 goto err_alloc;
925 }
926 resource->type = VME_DMA;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000927 resource->entry = &allocated_ctrlr->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100928
929 return resource;
930
931err_alloc:
932 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000933 mutex_lock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100934 dma_ctrlr->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000935 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100936err_ctrlr:
937err_bus:
938 return NULL;
939}
Martyn Welch58e50792009-10-29 16:35:20 +0000940EXPORT_SYMBOL(vme_dma_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100941
Martyn Welchb5bc9802017-03-04 00:34:29 +0000942/**
943 * vme_new_dma_list - Create new VME DMA list.
944 * @resource: Pointer to VME DMA resource.
945 *
946 * Create a new VME DMA list. It is the responsibility of the user to free
947 * the list once it is no longer required with vme_dma_list_free().
948 *
949 * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
950 * VME DMA resource.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100951 */
952struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
953{
954 struct vme_dma_resource *ctrlr;
955 struct vme_dma_list *dma_list;
956
957 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000958 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100959 return NULL;
960 }
961
962 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
963
Martyn Welchead1f3e2009-12-15 08:43:02 +0000964 dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
965 if (dma_list == NULL) {
Aaron Sierraf56c3d42016-04-21 11:18:22 -0500966 printk(KERN_ERR "Unable to allocate memory for new DMA list\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100967 return NULL;
968 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000969 INIT_LIST_HEAD(&dma_list->entries);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100970 dma_list->parent = ctrlr;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000971 mutex_init(&dma_list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100972
973 return dma_list;
974}
975EXPORT_SYMBOL(vme_new_dma_list);
976
Martyn Welchb5bc9802017-03-04 00:34:29 +0000977/**
978 * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
979 * @pattern: Value to use used as pattern
980 * @type: Type of pattern to be written.
981 *
982 * Create VME DMA list attribute for pattern generation. It is the
983 * responsibility of the user to free used attributes using
984 * vme_dma_free_attribute().
985 *
986 * Return: Pointer to VME DMA attribute, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100987 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000988struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100989{
990 struct vme_dma_attr *attributes;
991 struct vme_dma_pattern *pattern_attr;
992
Martyn Welchead1f3e2009-12-15 08:43:02 +0000993 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
994 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700995 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100996 goto err_attr;
997 }
998
Martyn Welchead1f3e2009-12-15 08:43:02 +0000999 pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
1000 if (pattern_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001001 printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001002 goto err_pat;
1003 }
1004
1005 attributes->type = VME_DMA_PATTERN;
1006 attributes->private = (void *)pattern_attr;
1007
1008 pattern_attr->pattern = pattern;
1009 pattern_attr->type = type;
1010
1011 return attributes;
1012
Martyn Welcha17a75e2009-07-31 09:28:17 +01001013err_pat:
1014 kfree(attributes);
1015err_attr:
1016 return NULL;
1017}
1018EXPORT_SYMBOL(vme_dma_pattern_attribute);
1019
Martyn Welchb5bc9802017-03-04 00:34:29 +00001020/**
1021 * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
1022 * @address: PCI base address for DMA transfer.
1023 *
1024 * Create VME DMA list attribute pointing to a location on PCI for DMA
1025 * transfers. It is the responsibility of the user to free used attributes
1026 * using vme_dma_free_attribute().
1027 *
1028 * Return: Pointer to VME DMA attribute, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001029 */
1030struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
1031{
1032 struct vme_dma_attr *attributes;
1033 struct vme_dma_pci *pci_attr;
1034
1035 /* XXX Run some sanity checks here */
1036
Martyn Welchead1f3e2009-12-15 08:43:02 +00001037 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
1038 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001039 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001040 goto err_attr;
1041 }
1042
Martyn Welchead1f3e2009-12-15 08:43:02 +00001043 pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
1044 if (pci_attr == NULL) {
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001045 printk(KERN_ERR "Unable to allocate memory for PCI attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001046 goto err_pci;
1047 }
1048
1049
1050
1051 attributes->type = VME_DMA_PCI;
1052 attributes->private = (void *)pci_attr;
1053
1054 pci_attr->address = address;
1055
1056 return attributes;
1057
Martyn Welcha17a75e2009-07-31 09:28:17 +01001058err_pci:
1059 kfree(attributes);
1060err_attr:
1061 return NULL;
1062}
1063EXPORT_SYMBOL(vme_dma_pci_attribute);
1064
Martyn Welchb5bc9802017-03-04 00:34:29 +00001065/**
1066 * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
1067 * @address: VME base address for DMA transfer.
1068 * @aspace: VME address space to use for DMA transfer.
1069 * @cycle: VME bus cycle to use for DMA transfer.
1070 * @dwidth: VME data width to use for DMA transfer.
1071 *
1072 * Create VME DMA list attribute pointing to a location on the VME bus for DMA
1073 * transfers. It is the responsibility of the user to free used attributes
1074 * using vme_dma_free_attribute().
1075 *
1076 * Return: Pointer to VME DMA attribute, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001077 */
1078struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
Martyn Welch6af04b02011-12-01 17:06:29 +00001079 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001080{
1081 struct vme_dma_attr *attributes;
1082 struct vme_dma_vme *vme_attr;
1083
Martyn Welchead1f3e2009-12-15 08:43:02 +00001084 attributes = kmalloc(
Martyn Welcha17a75e2009-07-31 09:28:17 +01001085 sizeof(struct vme_dma_attr), GFP_KERNEL);
Martyn Welchead1f3e2009-12-15 08:43:02 +00001086 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001087 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001088 goto err_attr;
1089 }
1090
Martyn Welchead1f3e2009-12-15 08:43:02 +00001091 vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
1092 if (vme_attr == NULL) {
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001093 printk(KERN_ERR "Unable to allocate memory for VME attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001094 goto err_vme;
1095 }
1096
1097 attributes->type = VME_DMA_VME;
1098 attributes->private = (void *)vme_attr;
1099
1100 vme_attr->address = address;
1101 vme_attr->aspace = aspace;
1102 vme_attr->cycle = cycle;
1103 vme_attr->dwidth = dwidth;
1104
1105 return attributes;
1106
Martyn Welcha17a75e2009-07-31 09:28:17 +01001107err_vme:
1108 kfree(attributes);
1109err_attr:
1110 return NULL;
1111}
1112EXPORT_SYMBOL(vme_dma_vme_attribute);
1113
Martyn Welchb5bc9802017-03-04 00:34:29 +00001114/**
1115 * vme_dma_free_attribute - Free DMA list attribute.
1116 * @attributes: Pointer to DMA list attribute.
1117 *
1118 * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1119 * once vme_dma_list_add() has returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001120 */
1121void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1122{
1123 kfree(attributes->private);
1124 kfree(attributes);
1125}
1126EXPORT_SYMBOL(vme_dma_free_attribute);
1127
Martyn Welchb5bc9802017-03-04 00:34:29 +00001128/**
1129 * vme_dma_list_add - Add enty to a VME DMA list.
1130 * @list: Pointer to VME list.
1131 * @src: Pointer to DMA list attribute to use as source.
1132 * @dest: Pointer to DMA list attribute to use as destination.
1133 * @count: Number of bytes to transfer.
1134 *
1135 * Add an entry to the provided VME DMA list. Entry requires pointers to source
1136 * and destination DMA attributes and a count.
1137 *
1138 * Please note, the attributes supported as source and destinations for
1139 * transfers are hardware dependent.
1140 *
1141 * Return: Zero on success, -EINVAL if operation is not supported on this
1142 * device or if the link list has already been submitted for execution.
1143 * Hardware specific errors also possible.
1144 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001145int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
1146 struct vme_dma_attr *dest, size_t count)
1147{
1148 struct vme_bridge *bridge = list->parent->parent;
1149 int retval;
1150
1151 if (bridge->dma_list_add == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001152 printk(KERN_WARNING "Link List DMA generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001153 return -EINVAL;
1154 }
1155
Emilio G. Cota886953e2010-11-12 11:14:07 +00001156 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001157 printk(KERN_ERR "Link List already submitted\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001158 return -EINVAL;
1159 }
1160
1161 retval = bridge->dma_list_add(list, src, dest, count);
1162
Emilio G. Cota886953e2010-11-12 11:14:07 +00001163 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001164
1165 return retval;
1166}
1167EXPORT_SYMBOL(vme_dma_list_add);
1168
Martyn Welchb5bc9802017-03-04 00:34:29 +00001169/**
1170 * vme_dma_list_exec - Queue a VME DMA list for execution.
1171 * @list: Pointer to VME list.
1172 *
1173 * Queue the provided VME DMA list for execution. The call will return once the
1174 * list has been executed.
1175 *
1176 * Return: Zero on success, -EINVAL if operation is not supported on this
1177 * device. Hardware specific errors also possible.
1178 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001179int vme_dma_list_exec(struct vme_dma_list *list)
1180{
1181 struct vme_bridge *bridge = list->parent->parent;
1182 int retval;
1183
1184 if (bridge->dma_list_exec == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001185 printk(KERN_ERR "Link List DMA execution not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001186 return -EINVAL;
1187 }
1188
Emilio G. Cota886953e2010-11-12 11:14:07 +00001189 mutex_lock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001190
1191 retval = bridge->dma_list_exec(list);
1192
Emilio G. Cota886953e2010-11-12 11:14:07 +00001193 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001194
1195 return retval;
1196}
1197EXPORT_SYMBOL(vme_dma_list_exec);
1198
Martyn Welchb5bc9802017-03-04 00:34:29 +00001199/**
1200 * vme_dma_list_free - Free a VME DMA list.
1201 * @list: Pointer to VME list.
1202 *
1203 * Free the provided DMA list and all its entries.
1204 *
1205 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1206 * is still in use. Hardware specific errors also possible.
1207 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001208int vme_dma_list_free(struct vme_dma_list *list)
1209{
1210 struct vme_bridge *bridge = list->parent->parent;
1211 int retval;
1212
1213 if (bridge->dma_list_empty == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001214 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001215 return -EINVAL;
1216 }
1217
Emilio G. Cota886953e2010-11-12 11:14:07 +00001218 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001219 printk(KERN_ERR "Link List in use\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001220 return -EINVAL;
1221 }
1222
1223 /*
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001224 * Empty out all of the entries from the DMA list. We need to go to the
1225 * low level driver as DMA entries are driver specific.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001226 */
1227 retval = bridge->dma_list_empty(list);
1228 if (retval) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001229 printk(KERN_ERR "Unable to empty link-list entries\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +00001230 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001231 return retval;
1232 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001233 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001234 kfree(list);
1235
1236 return retval;
1237}
1238EXPORT_SYMBOL(vme_dma_list_free);
1239
Martyn Welchb5bc9802017-03-04 00:34:29 +00001240/**
1241 * vme_dma_free - Free a VME DMA resource.
1242 * @resource: Pointer to VME DMA resource.
1243 *
1244 * Free the provided DMA resource so that it may be reallocated.
1245 *
1246 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1247 * is still active.
1248 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001249int vme_dma_free(struct vme_resource *resource)
1250{
1251 struct vme_dma_resource *ctrlr;
1252
1253 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001254 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001255 return -EINVAL;
1256 }
1257
1258 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1259
Emilio G. Cota886953e2010-11-12 11:14:07 +00001260 if (!mutex_trylock(&ctrlr->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001261 printk(KERN_ERR "Resource busy, can't free\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001262 return -EBUSY;
1263 }
1264
Emilio G. Cota886953e2010-11-12 11:14:07 +00001265 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001266 printk(KERN_WARNING "Resource still processing transfers\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +00001267 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001268 return -EBUSY;
1269 }
1270
1271 ctrlr->locked = 0;
1272
Emilio G. Cota886953e2010-11-12 11:14:07 +00001273 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001274
Martyn Welchfd5c2562013-06-06 12:29:16 +01001275 kfree(resource);
1276
Martyn Welcha17a75e2009-07-31 09:28:17 +01001277 return 0;
1278}
1279EXPORT_SYMBOL(vme_dma_free);
1280
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001281void vme_bus_error_handler(struct vme_bridge *bridge,
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +03001282 unsigned long long address, int am)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001283{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001284 struct list_head *handler_pos = NULL;
1285 struct vme_error_handler *handler;
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001286 int handler_triggered = 0;
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001287 u32 aspace = vme_get_aspace(am);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001288
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001289 list_for_each(handler_pos, &bridge->vme_error_handlers) {
1290 handler = list_entry(handler_pos, struct vme_error_handler,
1291 list);
1292 if ((aspace == handler->aspace) &&
1293 (address >= handler->start) &&
1294 (address < handler->end)) {
1295 if (!handler->num_errors)
1296 handler->first_error = address;
1297 if (handler->num_errors != UINT_MAX)
1298 handler->num_errors++;
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001299 handler_triggered = 1;
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001300 }
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001301 }
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001302
1303 if (!handler_triggered)
1304 dev_err(bridge->parent,
1305 "Unhandled VME access error at address 0x%llx\n",
1306 address);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001307}
1308EXPORT_SYMBOL(vme_bus_error_handler);
1309
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001310struct vme_error_handler *vme_register_error_handler(
1311 struct vme_bridge *bridge, u32 aspace,
1312 unsigned long long address, size_t len)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001313{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001314 struct vme_error_handler *handler;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001315
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001316 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
1317 if (!handler)
1318 return NULL;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001319
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001320 handler->aspace = aspace;
1321 handler->start = address;
1322 handler->end = address + len;
1323 handler->num_errors = 0;
1324 handler->first_error = 0;
1325 list_add_tail(&handler->list, &bridge->vme_error_handlers);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001326
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001327 return handler;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001328}
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001329EXPORT_SYMBOL(vme_register_error_handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001330
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001331void vme_unregister_error_handler(struct vme_error_handler *handler)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001332{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001333 list_del(&handler->list);
1334 kfree(handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001335}
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001336EXPORT_SYMBOL(vme_unregister_error_handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001337
Martyn Welchc813f592009-10-29 16:34:54 +00001338void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1339{
1340 void (*call)(int, int, void *);
1341 void *priv_data;
1342
1343 call = bridge->irq[level - 1].callback[statid].func;
1344 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1345
1346 if (call != NULL)
1347 call(level, statid, priv_data);
1348 else
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001349 printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001350 level, statid);
Martyn Welchc813f592009-10-29 16:34:54 +00001351}
1352EXPORT_SYMBOL(vme_irq_handler);
1353
Martyn Welchb5bc9802017-03-04 00:34:29 +00001354/**
1355 * vme_irq_request - Request a specific VME interrupt.
1356 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1357 * @level: Interrupt priority being requested.
1358 * @statid: Interrupt vector being requested.
1359 * @callback: Pointer to callback function called when VME interrupt/vector
1360 * received.
1361 * @priv_data: Generic pointer that will be passed to the callback function.
1362 *
1363 * Request callback to be attached as a handler for VME interrupts with provided
1364 * level and statid.
1365 *
1366 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1367 * function is not supported, -EBUSY if the level/statid combination is
1368 * already in use. Hardware specific errors also possible.
1369 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001370int vme_irq_request(struct vme_dev *vdev, int level, int statid,
Martyn Welch29848ac2010-02-18 15:13:05 +00001371 void (*callback)(int, int, void *),
Martyn Welcha17a75e2009-07-31 09:28:17 +01001372 void *priv_data)
1373{
1374 struct vme_bridge *bridge;
1375
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001376 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001377 if (bridge == NULL) {
1378 printk(KERN_ERR "Can't find VME bus\n");
1379 return -EINVAL;
1380 }
1381
Martyn Welchead1f3e2009-12-15 08:43:02 +00001382 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001383 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001384 return -EINVAL;
1385 }
1386
Martyn Welchc813f592009-10-29 16:34:54 +00001387 if (bridge->irq_set == NULL) {
1388 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001389 return -EINVAL;
1390 }
1391
Emilio G. Cota886953e2010-11-12 11:14:07 +00001392 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001393
1394 if (bridge->irq[level - 1].callback[statid].func) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001395 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001396 printk(KERN_WARNING "VME Interrupt already taken\n");
1397 return -EBUSY;
1398 }
1399
1400 bridge->irq[level - 1].count++;
1401 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1402 bridge->irq[level - 1].callback[statid].func = callback;
1403
1404 /* Enable IRQ level */
Martyn Welch29848ac2010-02-18 15:13:05 +00001405 bridge->irq_set(bridge, level, 1, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001406
Emilio G. Cota886953e2010-11-12 11:14:07 +00001407 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001408
1409 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001410}
Martyn Welchc813f592009-10-29 16:34:54 +00001411EXPORT_SYMBOL(vme_irq_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001412
Martyn Welchb5bc9802017-03-04 00:34:29 +00001413/**
1414 * vme_irq_free - Free a VME interrupt.
1415 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1416 * @level: Interrupt priority of interrupt being freed.
1417 * @statid: Interrupt vector of interrupt being freed.
1418 *
1419 * Remove previously attached callback from VME interrupt priority/vector.
1420 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001421void vme_irq_free(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001422{
1423 struct vme_bridge *bridge;
1424
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001425 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001426 if (bridge == NULL) {
1427 printk(KERN_ERR "Can't find VME bus\n");
1428 return;
1429 }
1430
Martyn Welchead1f3e2009-12-15 08:43:02 +00001431 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001432 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001433 return;
1434 }
1435
Martyn Welchc813f592009-10-29 16:34:54 +00001436 if (bridge->irq_set == NULL) {
1437 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001438 return;
1439 }
1440
Emilio G. Cota886953e2010-11-12 11:14:07 +00001441 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001442
1443 bridge->irq[level - 1].count--;
1444
1445 /* Disable IRQ level if no more interrupts attached at this level*/
1446 if (bridge->irq[level - 1].count == 0)
Martyn Welch29848ac2010-02-18 15:13:05 +00001447 bridge->irq_set(bridge, level, 0, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001448
1449 bridge->irq[level - 1].callback[statid].func = NULL;
1450 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1451
Emilio G. Cota886953e2010-11-12 11:14:07 +00001452 mutex_unlock(&bridge->irq_mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001453}
Martyn Welchc813f592009-10-29 16:34:54 +00001454EXPORT_SYMBOL(vme_irq_free);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001455
Martyn Welchb5bc9802017-03-04 00:34:29 +00001456/**
1457 * vme_irq_generate - Generate VME interrupt.
1458 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1459 * @level: Interrupt priority at which to assert the interrupt.
1460 * @statid: Interrupt vector to associate with the interrupt.
1461 *
1462 * Generate a VME interrupt of the provided level and with the provided
1463 * statid.
1464 *
1465 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1466 * function is not supported. Hardware specific errors also possible.
1467 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001468int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001469{
1470 struct vme_bridge *bridge;
1471
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001472 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001473 if (bridge == NULL) {
1474 printk(KERN_ERR "Can't find VME bus\n");
1475 return -EINVAL;
1476 }
1477
Martyn Welchead1f3e2009-12-15 08:43:02 +00001478 if ((level < 1) || (level > 7)) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001479 printk(KERN_WARNING "Invalid interrupt level\n");
1480 return -EINVAL;
1481 }
1482
Martyn Welchc813f592009-10-29 16:34:54 +00001483 if (bridge->irq_generate == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001484 printk(KERN_WARNING "Interrupt generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001485 return -EINVAL;
1486 }
1487
Martyn Welch29848ac2010-02-18 15:13:05 +00001488 return bridge->irq_generate(bridge, level, statid);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001489}
Martyn Welchc813f592009-10-29 16:34:54 +00001490EXPORT_SYMBOL(vme_irq_generate);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001491
Martyn Welchb5bc9802017-03-04 00:34:29 +00001492/**
1493 * vme_lm_request - Request a VME location monitor
1494 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1495 *
1496 * Allocate a location monitor resource to the driver. A location monitor
1497 * allows the driver to monitor accesses to a contiguous number of
1498 * addresses on the VME bus.
1499 *
1500 * Return: Pointer to a VME resource on success or NULL on failure.
Martyn Welch42fb5032009-08-11 17:44:56 +01001501 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001502struct vme_resource *vme_lm_request(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001503{
1504 struct vme_bridge *bridge;
Martyn Welch42fb5032009-08-11 17:44:56 +01001505 struct list_head *lm_pos = NULL;
1506 struct vme_lm_resource *allocated_lm = NULL;
1507 struct vme_lm_resource *lm = NULL;
1508 struct vme_resource *resource = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001509
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001510 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001511 if (bridge == NULL) {
1512 printk(KERN_ERR "Can't find VME bus\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001513 goto err_bus;
1514 }
1515
Martyn Welchb5bc9802017-03-04 00:34:29 +00001516 /* Loop through LM resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001517 list_for_each(lm_pos, &bridge->lm_resources) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001518 lm = list_entry(lm_pos,
1519 struct vme_lm_resource, list);
1520
1521 if (lm == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001522 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001523 continue;
1524 }
1525
1526 /* Find an unlocked controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001527 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001528 if (lm->locked == 0) {
1529 lm->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001530 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001531 allocated_lm = lm;
1532 break;
1533 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001534 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001535 }
1536
1537 /* Check to see if we found a resource */
1538 if (allocated_lm == NULL)
1539 goto err_lm;
1540
1541 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
1542 if (resource == NULL) {
1543 printk(KERN_ERR "Unable to allocate resource structure\n");
1544 goto err_alloc;
1545 }
1546 resource->type = VME_LM;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001547 resource->entry = &allocated_lm->list;
Martyn Welch42fb5032009-08-11 17:44:56 +01001548
1549 return resource;
1550
1551err_alloc:
1552 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001553 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001554 lm->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001555 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001556err_lm:
1557err_bus:
1558 return NULL;
1559}
1560EXPORT_SYMBOL(vme_lm_request);
1561
Martyn Welchb5bc9802017-03-04 00:34:29 +00001562/**
1563 * vme_lm_count - Determine number of VME Addresses monitored
1564 * @resource: Pointer to VME location monitor resource.
1565 *
1566 * The number of contiguous addresses monitored is hardware dependent.
1567 * Return the number of contiguous addresses monitored by the
1568 * location monitor.
1569 *
1570 * Return: Count of addresses monitored or -EINVAL when provided with an
1571 * invalid location monitor resource.
1572 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001573int vme_lm_count(struct vme_resource *resource)
1574{
1575 struct vme_lm_resource *lm;
1576
1577 if (resource->type != VME_LM) {
1578 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001579 return -EINVAL;
1580 }
1581
Martyn Welch42fb5032009-08-11 17:44:56 +01001582 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1583
1584 return lm->monitors;
1585}
1586EXPORT_SYMBOL(vme_lm_count);
1587
Martyn Welchb5bc9802017-03-04 00:34:29 +00001588/**
1589 * vme_lm_set - Configure location monitor
1590 * @resource: Pointer to VME location monitor resource.
1591 * @lm_base: Base address to monitor.
1592 * @aspace: VME address space to monitor.
1593 * @cycle: VME bus cycle type to monitor.
1594 *
1595 * Set the base address, address space and cycle type of accesses to be
1596 * monitored by the location monitor.
1597 *
1598 * Return: Zero on success, -EINVAL when provided with an invalid location
1599 * monitor resource or function is not supported. Hardware specific
1600 * errors may also be returned.
1601 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001602int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001603 u32 aspace, u32 cycle)
Martyn Welch42fb5032009-08-11 17:44:56 +01001604{
1605 struct vme_bridge *bridge = find_bridge(resource);
1606 struct vme_lm_resource *lm;
1607
1608 if (resource->type != VME_LM) {
1609 printk(KERN_ERR "Not a Location Monitor resource\n");
1610 return -EINVAL;
1611 }
1612
1613 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1614
Martyn Welcha17a75e2009-07-31 09:28:17 +01001615 if (bridge->lm_set == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001616 printk(KERN_ERR "vme_lm_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001617 return -EINVAL;
1618 }
1619
Martyn Welch8be92262009-10-29 16:35:08 +00001620 return bridge->lm_set(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001621}
1622EXPORT_SYMBOL(vme_lm_set);
1623
Martyn Welchb5bc9802017-03-04 00:34:29 +00001624/**
1625 * vme_lm_get - Retrieve location monitor settings
1626 * @resource: Pointer to VME location monitor resource.
1627 * @lm_base: Pointer used to output the base address monitored.
1628 * @aspace: Pointer used to output the address space monitored.
1629 * @cycle: Pointer used to output the VME bus cycle type monitored.
1630 *
1631 * Retrieve the base address, address space and cycle type of accesses to
1632 * be monitored by the location monitor.
1633 *
1634 * Return: Zero on success, -EINVAL when provided with an invalid location
1635 * monitor resource or function is not supported. Hardware specific
1636 * errors may also be returned.
1637 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001638int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001639 u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001640{
Martyn Welch42fb5032009-08-11 17:44:56 +01001641 struct vme_bridge *bridge = find_bridge(resource);
1642 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001643
Martyn Welch42fb5032009-08-11 17:44:56 +01001644 if (resource->type != VME_LM) {
1645 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001646 return -EINVAL;
1647 }
1648
Martyn Welch42fb5032009-08-11 17:44:56 +01001649 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1650
Martyn Welcha17a75e2009-07-31 09:28:17 +01001651 if (bridge->lm_get == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001652 printk(KERN_ERR "vme_lm_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001653 return -EINVAL;
1654 }
1655
Martyn Welch42fb5032009-08-11 17:44:56 +01001656 return bridge->lm_get(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001657}
1658EXPORT_SYMBOL(vme_lm_get);
1659
Martyn Welchb5bc9802017-03-04 00:34:29 +00001660/**
1661 * vme_lm_attach - Provide callback for location monitor address
1662 * @resource: Pointer to VME location monitor resource.
1663 * @monitor: Offset to which callback should be attached.
1664 * @callback: Pointer to callback function called when triggered.
1665 * @data: Generic pointer that will be passed to the callback function.
1666 *
1667 * Attach a callback to the specificed offset into the location monitors
1668 * monitored addresses. A generic pointer is provided to allow data to be
1669 * passed to the callback when called.
1670 *
1671 * Return: Zero on success, -EINVAL when provided with an invalid location
1672 * monitor resource or function is not supported. Hardware specific
1673 * errors may also be returned.
1674 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001675int vme_lm_attach(struct vme_resource *resource, int monitor,
Aaron Sierrafa54b322016-04-29 16:41:02 -05001676 void (*callback)(void *), void *data)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001677{
Martyn Welch42fb5032009-08-11 17:44:56 +01001678 struct vme_bridge *bridge = find_bridge(resource);
1679 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001680
Martyn Welch42fb5032009-08-11 17:44:56 +01001681 if (resource->type != VME_LM) {
1682 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001683 return -EINVAL;
1684 }
1685
Martyn Welch42fb5032009-08-11 17:44:56 +01001686 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1687
Martyn Welcha17a75e2009-07-31 09:28:17 +01001688 if (bridge->lm_attach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001689 printk(KERN_ERR "vme_lm_attach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001690 return -EINVAL;
1691 }
1692
Aaron Sierrafa54b322016-04-29 16:41:02 -05001693 return bridge->lm_attach(lm, monitor, callback, data);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001694}
1695EXPORT_SYMBOL(vme_lm_attach);
1696
Martyn Welchb5bc9802017-03-04 00:34:29 +00001697/**
1698 * vme_lm_detach - Remove callback for location monitor address
1699 * @resource: Pointer to VME location monitor resource.
1700 * @monitor: Offset to which callback should be removed.
1701 *
1702 * Remove the callback associated with the specificed offset into the
1703 * location monitors monitored addresses.
1704 *
1705 * Return: Zero on success, -EINVAL when provided with an invalid location
1706 * monitor resource or function is not supported. Hardware specific
1707 * errors may also be returned.
1708 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001709int vme_lm_detach(struct vme_resource *resource, int monitor)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001710{
Martyn Welch42fb5032009-08-11 17:44:56 +01001711 struct vme_bridge *bridge = find_bridge(resource);
1712 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001713
Martyn Welch42fb5032009-08-11 17:44:56 +01001714 if (resource->type != VME_LM) {
1715 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001716 return -EINVAL;
1717 }
1718
Martyn Welch42fb5032009-08-11 17:44:56 +01001719 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1720
Martyn Welcha17a75e2009-07-31 09:28:17 +01001721 if (bridge->lm_detach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001722 printk(KERN_ERR "vme_lm_detach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001723 return -EINVAL;
1724 }
1725
Martyn Welch42fb5032009-08-11 17:44:56 +01001726 return bridge->lm_detach(lm, monitor);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001727}
1728EXPORT_SYMBOL(vme_lm_detach);
1729
Martyn Welchb5bc9802017-03-04 00:34:29 +00001730/**
1731 * vme_lm_free - Free allocated VME location monitor
1732 * @resource: Pointer to VME location monitor resource.
1733 *
1734 * Free allocation of a VME location monitor.
1735 *
1736 * WARNING: This function currently expects that any callbacks that have
1737 * been attached to the location monitor have been removed.
1738 *
1739 * Return: Zero on success, -EINVAL when provided with an invalid location
1740 * monitor resource.
1741 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001742void vme_lm_free(struct vme_resource *resource)
1743{
1744 struct vme_lm_resource *lm;
1745
1746 if (resource->type != VME_LM) {
1747 printk(KERN_ERR "Not a Location Monitor resource\n");
1748 return;
1749 }
1750
1751 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1752
Emilio G. Cota886953e2010-11-12 11:14:07 +00001753 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001754
Martyn Welch8be92262009-10-29 16:35:08 +00001755 /* XXX
1756 * Check to see that there aren't any callbacks still attached, if
1757 * there are we should probably be detaching them!
1758 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001759
1760 lm->locked = 0;
1761
Emilio G. Cota886953e2010-11-12 11:14:07 +00001762 mutex_unlock(&lm->mtx);
Martyn Welch8be92262009-10-29 16:35:08 +00001763
1764 kfree(resource);
Martyn Welch42fb5032009-08-11 17:44:56 +01001765}
1766EXPORT_SYMBOL(vme_lm_free);
1767
Martyn Welchb5bc9802017-03-04 00:34:29 +00001768/**
1769 * vme_slot_num - Retrieve slot ID
1770 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1771 *
1772 * Retrieve the slot ID associated with the provided VME device.
1773 *
1774 * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1775 * or the function is not supported. Hardware specific errors may also
1776 * be returned.
1777 */
Martyn Welchd7729f02013-11-08 11:58:35 +00001778int vme_slot_num(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001779{
1780 struct vme_bridge *bridge;
1781
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001782 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001783 if (bridge == NULL) {
1784 printk(KERN_ERR "Can't find VME bus\n");
1785 return -EINVAL;
1786 }
1787
1788 if (bridge->slot_get == NULL) {
Martyn Welchd7729f02013-11-08 11:58:35 +00001789 printk(KERN_WARNING "vme_slot_num not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001790 return -EINVAL;
1791 }
1792
Martyn Welch29848ac2010-02-18 15:13:05 +00001793 return bridge->slot_get(bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001794}
Martyn Welchd7729f02013-11-08 11:58:35 +00001795EXPORT_SYMBOL(vme_slot_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001796
Martyn Welchb5bc9802017-03-04 00:34:29 +00001797/**
1798 * vme_bus_num - Retrieve bus number
1799 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1800 *
1801 * Retrieve the bus enumeration associated with the provided VME device.
1802 *
1803 * Return: The bus number on success, -EINVAL if VME bridge cannot be
1804 * determined.
1805 */
Martyn Welch978f47d2013-11-08 11:58:34 +00001806int vme_bus_num(struct vme_dev *vdev)
1807{
1808 struct vme_bridge *bridge;
1809
1810 bridge = vdev->bridge;
1811 if (bridge == NULL) {
1812 pr_err("Can't find VME bus\n");
1813 return -EINVAL;
1814 }
1815
1816 return bridge->num;
1817}
1818EXPORT_SYMBOL(vme_bus_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001819
1820/* - Bridge Registration --------------------------------------------------- */
1821
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001822static void vme_dev_release(struct device *dev)
1823{
1824 kfree(dev_to_vme_dev(dev));
1825}
1826
Aaron Sierra326071b2016-04-24 15:11:38 -05001827/* Common bridge initialization */
1828struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
1829{
1830 INIT_LIST_HEAD(&bridge->vme_error_handlers);
1831 INIT_LIST_HEAD(&bridge->master_resources);
1832 INIT_LIST_HEAD(&bridge->slave_resources);
1833 INIT_LIST_HEAD(&bridge->dma_resources);
1834 INIT_LIST_HEAD(&bridge->lm_resources);
1835 mutex_init(&bridge->irq_mtx);
1836
1837 return bridge;
1838}
1839EXPORT_SYMBOL(vme_init_bridge);
1840
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001841int vme_register_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001842{
1843 int i;
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001844 int ret = -1;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001845
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001846 mutex_lock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001847 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001848 if ((vme_bus_numbers & (1 << i)) == 0) {
1849 vme_bus_numbers |= (1 << i);
1850 bridge->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001851 INIT_LIST_HEAD(&bridge->devices);
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001852 list_add_tail(&bridge->bus_list, &vme_bus_list);
1853 ret = 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001854 break;
1855 }
1856 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001857 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001858
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001859 return ret;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001860}
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001861EXPORT_SYMBOL(vme_register_bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001862
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001863void vme_unregister_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001864{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001865 struct vme_dev *vdev;
1866 struct vme_dev *tmp;
1867
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001868 mutex_lock(&vme_buses_lock);
1869 vme_bus_numbers &= ~(1 << bridge->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001870 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1871 list_del(&vdev->drv_list);
1872 list_del(&vdev->bridge_list);
1873 device_unregister(&vdev->dev);
1874 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001875 list_del(&bridge->bus_list);
1876 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001877}
Martyn Welcha17a75e2009-07-31 09:28:17 +01001878EXPORT_SYMBOL(vme_unregister_bridge);
1879
Martyn Welcha17a75e2009-07-31 09:28:17 +01001880/* - Driver Registration --------------------------------------------------- */
1881
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001882static int __vme_register_driver_bus(struct vme_driver *drv,
1883 struct vme_bridge *bridge, unsigned int ndevs)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001884{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001885 int err;
1886 unsigned int i;
1887 struct vme_dev *vdev;
1888 struct vme_dev *tmp;
1889
1890 for (i = 0; i < ndevs; i++) {
1891 vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
1892 if (!vdev) {
1893 err = -ENOMEM;
1894 goto err_devalloc;
1895 }
Manohar Vangaa916a392011-09-26 11:27:17 +02001896 vdev->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001897 vdev->bridge = bridge;
1898 vdev->dev.platform_data = drv;
1899 vdev->dev.release = vme_dev_release;
1900 vdev->dev.parent = bridge->parent;
1901 vdev->dev.bus = &vme_bus_type;
Manohar Vangaa916a392011-09-26 11:27:17 +02001902 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1903 vdev->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001904
1905 err = device_register(&vdev->dev);
1906 if (err)
1907 goto err_reg;
1908
1909 if (vdev->dev.platform_data) {
1910 list_add_tail(&vdev->drv_list, &drv->devices);
1911 list_add_tail(&vdev->bridge_list, &bridge->devices);
1912 } else
1913 device_unregister(&vdev->dev);
1914 }
1915 return 0;
1916
1917err_reg:
Emilio G. Cotadef18202013-02-13 13:47:54 -05001918 put_device(&vdev->dev);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001919 kfree(vdev);
1920err_devalloc:
1921 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1922 list_del(&vdev->drv_list);
1923 list_del(&vdev->bridge_list);
1924 device_unregister(&vdev->dev);
1925 }
1926 return err;
1927}
1928
1929static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1930{
1931 struct vme_bridge *bridge;
1932 int err = 0;
1933
1934 mutex_lock(&vme_buses_lock);
1935 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1936 /*
1937 * This cannot cause trouble as we already have vme_buses_lock
1938 * and if the bridge is removed, it will have to go through
1939 * vme_unregister_bridge() to do it (which calls remove() on
1940 * the bridge which in turn tries to acquire vme_buses_lock and
Manohar Vangac26f6112011-11-04 11:12:29 +01001941 * will have to wait).
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001942 */
1943 err = __vme_register_driver_bus(drv, bridge, ndevs);
1944 if (err)
1945 break;
1946 }
1947 mutex_unlock(&vme_buses_lock);
1948 return err;
1949}
1950
Martyn Welchb5bc9802017-03-04 00:34:29 +00001951/**
1952 * vme_register_driver - Register a VME driver
1953 * @drv: Pointer to VME driver structure to register.
1954 * @ndevs: Maximum number of devices to allow to be enumerated.
1955 *
1956 * Register a VME device driver with the VME subsystem.
1957 *
1958 * Return: Zero on success, error value on registration failure.
1959 */
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001960int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1961{
1962 int err;
1963
Martyn Welcha17a75e2009-07-31 09:28:17 +01001964 drv->driver.name = drv->name;
1965 drv->driver.bus = &vme_bus_type;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001966 INIT_LIST_HEAD(&drv->devices);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001967
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001968 err = driver_register(&drv->driver);
1969 if (err)
1970 return err;
1971
1972 err = __vme_register_driver(drv, ndevs);
1973 if (err)
1974 driver_unregister(&drv->driver);
1975
1976 return err;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001977}
1978EXPORT_SYMBOL(vme_register_driver);
1979
Martyn Welchb5bc9802017-03-04 00:34:29 +00001980/**
1981 * vme_unregister_driver - Unregister a VME driver
1982 * @drv: Pointer to VME driver structure to unregister.
1983 *
1984 * Unregister a VME device driver from the VME subsystem.
1985 */
Martyn Welchead1f3e2009-12-15 08:43:02 +00001986void vme_unregister_driver(struct vme_driver *drv)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001987{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001988 struct vme_dev *dev, *dev_tmp;
1989
1990 mutex_lock(&vme_buses_lock);
1991 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1992 list_del(&dev->drv_list);
1993 list_del(&dev->bridge_list);
1994 device_unregister(&dev->dev);
1995 }
1996 mutex_unlock(&vme_buses_lock);
1997
Martyn Welcha17a75e2009-07-31 09:28:17 +01001998 driver_unregister(&drv->driver);
1999}
2000EXPORT_SYMBOL(vme_unregister_driver);
2001
2002/* - Bus Registration ------------------------------------------------------ */
2003
Martyn Welcha17a75e2009-07-31 09:28:17 +01002004static int vme_bus_match(struct device *dev, struct device_driver *drv)
2005{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002006 struct vme_driver *vme_drv;
Martyn Welcha17a75e2009-07-31 09:28:17 +01002007
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002008 vme_drv = container_of(drv, struct vme_driver, driver);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002009
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002010 if (dev->platform_data == vme_drv) {
2011 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002012
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002013 if (vme_drv->match && vme_drv->match(vdev))
2014 return 1;
2015
2016 dev->platform_data = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01002017 }
Martyn Welcha17a75e2009-07-31 09:28:17 +01002018 return 0;
2019}
2020
2021static int vme_bus_probe(struct device *dev)
2022{
Martyn Welcha17a75e2009-07-31 09:28:17 +01002023 int retval = -ENODEV;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002024 struct vme_driver *driver;
2025 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002026
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002027 driver = dev->platform_data;
Martyn Welcha17a75e2009-07-31 09:28:17 +01002028
Martyn Welchead1f3e2009-12-15 08:43:02 +00002029 if (driver->probe != NULL)
Manohar Vanga8f966dc2011-09-26 11:27:15 +02002030 retval = driver->probe(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002031
2032 return retval;
2033}
2034
Stefano Babic97974842017-01-20 10:38:20 -05002035static int vme_bus_remove(struct device *dev)
2036{
2037 int retval = -ENODEV;
2038 struct vme_driver *driver;
2039 struct vme_dev *vdev = dev_to_vme_dev(dev);
2040
2041 driver = dev->platform_data;
2042
2043 if (driver->remove != NULL)
2044 retval = driver->remove(vdev);
2045
2046 return retval;
2047}
2048
Martyn Welcha17a75e2009-07-31 09:28:17 +01002049struct bus_type vme_bus_type = {
2050 .name = "vme",
2051 .match = vme_bus_match,
2052 .probe = vme_bus_probe,
Stefano Babic97974842017-01-20 10:38:20 -05002053 .remove = vme_bus_remove,
Martyn Welcha17a75e2009-07-31 09:28:17 +01002054};
2055EXPORT_SYMBOL(vme_bus_type);
2056
Martyn Welchead1f3e2009-12-15 08:43:02 +00002057static int __init vme_init(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01002058{
2059 return bus_register(&vme_bus_type);
2060}
Aaron Sierrac326cc02013-12-09 09:54:42 -06002061subsys_initcall(vme_init);