blob: 986799d64993fd0034f8875a69b120974db16296 [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
Markus Elfring61282c02017-08-24 22:24:38 +020095 if (!resource) {
Martyn Welchead1f3e2009-12-15 08:43:02 +000096 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +010097 return NULL;
98 }
99
100 bridge = find_bridge(resource);
Markus Elfring61282c02017-08-24 22:24:38 +0200101 if (!bridge) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000102 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100103 return NULL;
104 }
105
Markus Elfring61282c02017-08-24 22:24:38 +0200106 if (!bridge->parent) {
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
Markus Elfring61282c02017-08-24 22:24:38 +0200111 if (!bridge->alloc_consistent) {
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
Markus Elfring61282c02017-08-24 22:24:38 +0200135 if (!resource) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000136 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100137 return;
138 }
139
140 bridge = find_bridge(resource);
Markus Elfring61282c02017-08-24 22:24:38 +0200141 if (!bridge) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000142 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100143 return;
144 }
145
Markus Elfring61282c02017-08-24 22:24:38 +0200146 if (!bridge->parent) {
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
Markus Elfring61282c02017-08-24 22:24:38 +0200151 if (!bridge->free_consistent) {
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;
Markus Elfring61282c02017-08-24 22:24:38 +0200306 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100307 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
Markus Elfring61282c02017-08-24 22:24:38 +0200316 if (!slave_image) {
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 */
Markus Elfring61282c02017-08-24 22:24:38 +0200336 if (!allocated_image)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100337 goto err_image;
338
Markus Elfring1ff0a192017-08-24 21:52:00 +0200339 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200340 if (!resource)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100341 goto err_alloc;
Markus Elfring94eefcc2017-08-24 21:38:20 +0200342
Martyn Welcha17a75e2009-07-31 09:28:17 +0100343 resource->type = VME_SLAVE;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000344 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100345
346 return resource;
347
348err_alloc:
349 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000350 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100351 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000352 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100353err_image:
354err_bus:
355 return NULL;
356}
357EXPORT_SYMBOL(vme_slave_request);
358
Martyn Welchb5bc9802017-03-04 00:34:29 +0000359/**
360 * vme_slave_set - Set VME slave window configuration.
361 * @resource: Pointer to VME slave resource.
362 * @enabled: State to which the window should be configured.
363 * @vme_base: Base address for the window.
364 * @size: Size of the VME window.
365 * @buf_base: Based address of buffer used to provide VME slave window storage.
366 * @aspace: VME address space for the VME window.
367 * @cycle: VME data transfer cycle type for the VME window.
368 *
369 * Set configuration for provided VME slave window.
370 *
371 * Return: Zero on success, -EINVAL if operation is not supported on this
372 * device, if an invalid resource has been provided or invalid
373 * attributes are provided. Hardware specific errors may also be
374 * returned.
375 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000376int vme_slave_set(struct vme_resource *resource, int enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100377 unsigned long long vme_base, unsigned long long size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000378 dma_addr_t buf_base, u32 aspace, u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100379{
380 struct vme_bridge *bridge = find_bridge(resource);
381 struct vme_slave_resource *image;
382 int retval;
383
384 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000385 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100386 return -EINVAL;
387 }
388
389 image = list_entry(resource->entry, struct vme_slave_resource, list);
390
Markus Elfring61282c02017-08-24 22:24:38 +0200391 if (!bridge->slave_set) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000392 printk(KERN_ERR "Function not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100393 return -ENOSYS;
394 }
395
Martyn Welchead1f3e2009-12-15 08:43:02 +0000396 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100397 ((image->cycle_attr & cycle) == cycle))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000398 printk(KERN_ERR "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100399 return -EINVAL;
400 }
401
402 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000403 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100404 return retval;
405
406 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
407 aspace, cycle);
408}
409EXPORT_SYMBOL(vme_slave_set);
410
Martyn Welchb5bc9802017-03-04 00:34:29 +0000411/**
412 * vme_slave_get - Retrieve VME slave window configuration.
413 * @resource: Pointer to VME slave resource.
414 * @enabled: Pointer to variable for storing state.
415 * @vme_base: Pointer to variable for storing window base address.
416 * @size: Pointer to variable for storing window size.
417 * @buf_base: Pointer to variable for storing slave buffer base address.
418 * @aspace: Pointer to variable for storing VME address space.
419 * @cycle: Pointer to variable for storing VME data transfer cycle type.
420 *
421 * Return configuration for provided VME slave window.
422 *
423 * Return: Zero on success, -EINVAL if operation is not supported on this
424 * device or if an invalid resource has been provided.
425 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000426int vme_slave_get(struct vme_resource *resource, int *enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100427 unsigned long long *vme_base, unsigned long long *size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000428 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100429{
430 struct vme_bridge *bridge = find_bridge(resource);
431 struct vme_slave_resource *image;
432
433 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000434 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100435 return -EINVAL;
436 }
437
438 image = list_entry(resource->entry, struct vme_slave_resource, list);
439
Markus Elfring61282c02017-08-24 22:24:38 +0200440 if (!bridge->slave_get) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000441 printk(KERN_ERR "vme_slave_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100442 return -EINVAL;
443 }
444
445 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
446 aspace, cycle);
447}
448EXPORT_SYMBOL(vme_slave_get);
449
Martyn Welchb5bc9802017-03-04 00:34:29 +0000450/**
451 * vme_slave_free - Free VME slave window
452 * @resource: Pointer to VME slave resource.
453 *
454 * Free the provided slave resource so that it may be reallocated.
455 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100456void vme_slave_free(struct vme_resource *resource)
457{
458 struct vme_slave_resource *slave_image;
459
460 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000461 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100462 return;
463 }
464
465 slave_image = list_entry(resource->entry, struct vme_slave_resource,
466 list);
Markus Elfring61282c02017-08-24 22:24:38 +0200467 if (!slave_image) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000468 printk(KERN_ERR "Can't find slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100469 return;
470 }
471
472 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000473 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100474 if (slave_image->locked == 0)
475 printk(KERN_ERR "Image is already free\n");
476
477 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000478 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100479
480 /* Free up resource memory */
481 kfree(resource);
482}
483EXPORT_SYMBOL(vme_slave_free);
484
Martyn Welchb5bc9802017-03-04 00:34:29 +0000485/**
486 * vme_master_request - Request a VME master window resource.
487 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
488 * @address: Required VME address space.
489 * @cycle: Required VME data transfer cycle type.
490 * @dwidth: Required VME data transfer width.
491 *
492 * Request use of a VME window resource capable of being set for the requested
493 * address space, data transfer cycle and width.
494 *
495 * Return: Pointer to VME resource on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100496 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000497struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
498 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100499{
500 struct vme_bridge *bridge;
501 struct list_head *master_pos = NULL;
502 struct vme_master_resource *allocated_image = NULL;
503 struct vme_master_resource *master_image = NULL;
504 struct vme_resource *resource = NULL;
505
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200506 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +0200507 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100508 printk(KERN_ERR "Can't find VME bus\n");
509 goto err_bus;
510 }
511
512 /* Loop through master resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000513 list_for_each(master_pos, &bridge->master_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100514 master_image = list_entry(master_pos,
515 struct vme_master_resource, list);
516
Markus Elfring61282c02017-08-24 22:24:38 +0200517 if (!master_image) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100518 printk(KERN_WARNING "Registered NULL master resource\n");
519 continue;
520 }
521
522 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000523 spin_lock(&master_image->lock);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000524 if (((master_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100525 ((master_image->cycle_attr & cycle) == cycle) &&
526 ((master_image->width_attr & dwidth) == dwidth) &&
527 (master_image->locked == 0)) {
528
529 master_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000530 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100531 allocated_image = master_image;
532 break;
533 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000534 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100535 }
536
537 /* Check to see if we found a resource */
Markus Elfring61282c02017-08-24 22:24:38 +0200538 if (!allocated_image) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100539 printk(KERN_ERR "Can't find a suitable resource\n");
540 goto err_image;
541 }
542
Markus Elfring1ff0a192017-08-24 21:52:00 +0200543 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200544 if (!resource)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100545 goto err_alloc;
Markus Elfring94eefcc2017-08-24 21:38:20 +0200546
Martyn Welcha17a75e2009-07-31 09:28:17 +0100547 resource->type = VME_MASTER;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000548 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100549
550 return resource;
551
Martyn Welcha17a75e2009-07-31 09:28:17 +0100552err_alloc:
553 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000554 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100555 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000556 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100557err_image:
558err_bus:
559 return NULL;
560}
561EXPORT_SYMBOL(vme_master_request);
562
Martyn Welchb5bc9802017-03-04 00:34:29 +0000563/**
564 * vme_master_set - Set VME master window configuration.
565 * @resource: Pointer to VME master resource.
566 * @enabled: State to which the window should be configured.
567 * @vme_base: Base address for the window.
568 * @size: Size of the VME window.
569 * @aspace: VME address space for the VME window.
570 * @cycle: VME data transfer cycle type for the VME window.
571 * @dwidth: VME data transfer width for the VME window.
572 *
573 * Set configuration for provided VME master window.
574 *
575 * Return: Zero on success, -EINVAL if operation is not supported on this
576 * device, if an invalid resource has been provided or invalid
577 * attributes are provided. Hardware specific errors may also be
578 * returned.
579 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000580int vme_master_set(struct vme_resource *resource, int enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000581 unsigned long long vme_base, unsigned long long size, u32 aspace,
582 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100583{
584 struct vme_bridge *bridge = find_bridge(resource);
585 struct vme_master_resource *image;
586 int retval;
587
588 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000589 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100590 return -EINVAL;
591 }
592
593 image = list_entry(resource->entry, struct vme_master_resource, list);
594
Markus Elfring61282c02017-08-24 22:24:38 +0200595 if (!bridge->master_set) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000596 printk(KERN_WARNING "vme_master_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100597 return -EINVAL;
598 }
599
Martyn Welchead1f3e2009-12-15 08:43:02 +0000600 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100601 ((image->cycle_attr & cycle) == cycle) &&
602 ((image->width_attr & dwidth) == dwidth))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000603 printk(KERN_WARNING "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100604 return -EINVAL;
605 }
606
607 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000608 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100609 return retval;
610
611 return bridge->master_set(image, enabled, vme_base, size, aspace,
612 cycle, dwidth);
613}
614EXPORT_SYMBOL(vme_master_set);
615
Martyn Welchb5bc9802017-03-04 00:34:29 +0000616/**
617 * vme_master_get - Retrieve VME master window configuration.
618 * @resource: Pointer to VME master resource.
619 * @enabled: Pointer to variable for storing state.
620 * @vme_base: Pointer to variable for storing window base address.
621 * @size: Pointer to variable for storing window size.
622 * @aspace: Pointer to variable for storing VME address space.
623 * @cycle: Pointer to variable for storing VME data transfer cycle type.
624 * @dwidth: Pointer to variable for storing VME data transfer width.
625 *
626 * Return configuration for provided VME master window.
627 *
628 * Return: Zero on success, -EINVAL if operation is not supported on this
629 * device or if an invalid resource has been provided.
630 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000631int vme_master_get(struct vme_resource *resource, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000632 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
633 u32 *cycle, u32 *dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100634{
635 struct vme_bridge *bridge = find_bridge(resource);
636 struct vme_master_resource *image;
637
638 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000639 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100640 return -EINVAL;
641 }
642
643 image = list_entry(resource->entry, struct vme_master_resource, list);
644
Markus Elfring61282c02017-08-24 22:24:38 +0200645 if (!bridge->master_get) {
Julia Lawallc1038302014-12-07 20:20:53 +0100646 printk(KERN_WARNING "%s not supported\n", __func__);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100647 return -EINVAL;
648 }
649
650 return bridge->master_get(image, enabled, vme_base, size, aspace,
651 cycle, dwidth);
652}
653EXPORT_SYMBOL(vme_master_get);
654
Martyn Welchb5bc9802017-03-04 00:34:29 +0000655/**
656 * vme_master_write - Read data from VME space into a buffer.
657 * @resource: Pointer to VME master resource.
658 * @buf: Pointer to buffer where data should be transferred.
659 * @count: Number of bytes to transfer.
660 * @offset: Offset into VME master window at which to start transfer.
661 *
662 * Perform read of count bytes of data from location on VME bus which maps into
663 * the VME master window at offset to buf.
664 *
665 * Return: Number of bytes read, -EINVAL if resource is not a VME master
666 * resource or read operation is not supported. -EFAULT returned if
667 * invalid offset is provided. Hardware specific errors may also be
668 * returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100669 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000670ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100671 loff_t offset)
672{
673 struct vme_bridge *bridge = find_bridge(resource);
674 struct vme_master_resource *image;
675 size_t length;
676
Markus Elfring61282c02017-08-24 22:24:38 +0200677 if (!bridge->master_read) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000678 printk(KERN_WARNING "Reading from resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100679 return -EINVAL;
680 }
681
682 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000683 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100684 return -EINVAL;
685 }
686
687 image = list_entry(resource->entry, struct vme_master_resource, list);
688
689 length = vme_get_size(resource);
690
691 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000692 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100693 return -EFAULT;
694 }
695
696 if ((offset + count) > length)
697 count = length - offset;
698
699 return bridge->master_read(image, buf, count, offset);
700
701}
702EXPORT_SYMBOL(vme_master_read);
703
Martyn Welchb5bc9802017-03-04 00:34:29 +0000704/**
705 * vme_master_write - Write data out to VME space from a buffer.
706 * @resource: Pointer to VME master resource.
707 * @buf: Pointer to buffer holding data to transfer.
708 * @count: Number of bytes to transfer.
709 * @offset: Offset into VME master window at which to start transfer.
710 *
711 * Perform write of count bytes of data from buf to location on VME bus which
712 * maps into the VME master window at offset.
713 *
714 * Return: Number of bytes written, -EINVAL if resource is not a VME master
715 * resource or write operation is not supported. -EFAULT returned if
716 * invalid offset is provided. Hardware specific errors may also be
717 * returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100718 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000719ssize_t vme_master_write(struct vme_resource *resource, void *buf,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100720 size_t count, loff_t offset)
721{
722 struct vme_bridge *bridge = find_bridge(resource);
723 struct vme_master_resource *image;
724 size_t length;
725
Markus Elfring61282c02017-08-24 22:24:38 +0200726 if (!bridge->master_write) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000727 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100728 return -EINVAL;
729 }
730
731 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000732 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100733 return -EINVAL;
734 }
735
736 image = list_entry(resource->entry, struct vme_master_resource, list);
737
738 length = vme_get_size(resource);
739
740 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000741 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100742 return -EFAULT;
743 }
744
745 if ((offset + count) > length)
746 count = length - offset;
747
748 return bridge->master_write(image, buf, count, offset);
749}
750EXPORT_SYMBOL(vme_master_write);
751
Martyn Welchb5bc9802017-03-04 00:34:29 +0000752/**
753 * vme_master_rmw - Perform read-modify-write cycle.
754 * @resource: Pointer to VME master resource.
755 * @mask: Bits to be compared and swapped in operation.
756 * @compare: Bits to be compared with data read from offset.
757 * @swap: Bits to be swapped in data read from offset.
758 * @offset: Offset into VME master window at which to perform operation.
759 *
760 * Perform read-modify-write cycle on provided location:
761 * - Location on VME bus is read.
762 * - Bits selected by mask are compared with compare.
763 * - Where a selected bit matches that in compare and are selected in swap,
764 * the bit is swapped.
765 * - Result written back to location on VME bus.
766 *
767 * Return: Bytes written on success, -EINVAL if resource is not a VME master
768 * resource or RMW operation is not supported. Hardware specific
769 * errors may also be returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100770 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000771unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100772 unsigned int compare, unsigned int swap, loff_t offset)
773{
774 struct vme_bridge *bridge = find_bridge(resource);
775 struct vme_master_resource *image;
776
Markus Elfring61282c02017-08-24 22:24:38 +0200777 if (!bridge->master_rmw) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000778 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100779 return -EINVAL;
780 }
781
782 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000783 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100784 return -EINVAL;
785 }
786
787 image = list_entry(resource->entry, struct vme_master_resource, list);
788
789 return bridge->master_rmw(image, mask, compare, swap, offset);
790}
791EXPORT_SYMBOL(vme_master_rmw);
792
Martyn Welchb5bc9802017-03-04 00:34:29 +0000793/**
794 * vme_master_mmap - Mmap region of VME master window.
795 * @resource: Pointer to VME master resource.
796 * @vma: Pointer to definition of user mapping.
797 *
798 * Memory map a region of the VME master window into user space.
799 *
800 * Return: Zero on success, -EINVAL if resource is not a VME master
801 * resource or -EFAULT if map exceeds window size. Other generic mmap
802 * errors may also be returned.
803 */
Dmitry Kalinkinc74a8042015-02-26 18:53:10 +0300804int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
805{
806 struct vme_master_resource *image;
807 phys_addr_t phys_addr;
808 unsigned long vma_size;
809
810 if (resource->type != VME_MASTER) {
811 pr_err("Not a master resource\n");
812 return -EINVAL;
813 }
814
815 image = list_entry(resource->entry, struct vme_master_resource, list);
816 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
817 vma_size = vma->vm_end - vma->vm_start;
818
819 if (phys_addr + vma_size > image->bus_resource.end + 1) {
820 pr_err("Map size cannot exceed the window size\n");
821 return -EFAULT;
822 }
823
824 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
825
826 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
827}
828EXPORT_SYMBOL(vme_master_mmap);
829
Martyn Welchb5bc9802017-03-04 00:34:29 +0000830/**
831 * vme_master_free - Free VME master window
832 * @resource: Pointer to VME master resource.
833 *
834 * Free the provided master resource so that it may be reallocated.
835 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100836void vme_master_free(struct vme_resource *resource)
837{
838 struct vme_master_resource *master_image;
839
840 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000841 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100842 return;
843 }
844
845 master_image = list_entry(resource->entry, struct vme_master_resource,
846 list);
Markus Elfring61282c02017-08-24 22:24:38 +0200847 if (!master_image) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000848 printk(KERN_ERR "Can't find master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100849 return;
850 }
851
852 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000853 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100854 if (master_image->locked == 0)
855 printk(KERN_ERR "Image is already free\n");
856
857 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000858 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100859
860 /* Free up resource memory */
861 kfree(resource);
862}
863EXPORT_SYMBOL(vme_master_free);
864
Martyn Welchb5bc9802017-03-04 00:34:29 +0000865/**
866 * vme_dma_request - Request a DMA controller.
867 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
868 * @route: Required src/destination combination.
869 *
870 * Request a VME DMA controller with capability to perform transfers bewteen
871 * requested source/destination combination.
872 *
873 * Return: Pointer to VME DMA resource on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100874 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000875struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100876{
877 struct vme_bridge *bridge;
878 struct list_head *dma_pos = NULL;
879 struct vme_dma_resource *allocated_ctrlr = NULL;
880 struct vme_dma_resource *dma_ctrlr = NULL;
881 struct vme_resource *resource = NULL;
882
883 /* XXX Not checking resource attributes */
884 printk(KERN_ERR "No VME resource Attribute tests done\n");
885
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200886 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +0200887 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100888 printk(KERN_ERR "Can't find VME bus\n");
889 goto err_bus;
890 }
891
892 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000893 list_for_each(dma_pos, &bridge->dma_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100894 dma_ctrlr = list_entry(dma_pos,
895 struct vme_dma_resource, list);
Markus Elfring61282c02017-08-24 22:24:38 +0200896 if (!dma_ctrlr) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000897 printk(KERN_ERR "Registered NULL DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100898 continue;
899 }
900
Martyn Welch4f723df2010-02-18 15:12:58 +0000901 /* Find an unlocked and compatible controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000902 mutex_lock(&dma_ctrlr->mtx);
Martyn Welch4f723df2010-02-18 15:12:58 +0000903 if (((dma_ctrlr->route_attr & route) == route) &&
904 (dma_ctrlr->locked == 0)) {
905
Martyn Welcha17a75e2009-07-31 09:28:17 +0100906 dma_ctrlr->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000907 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100908 allocated_ctrlr = dma_ctrlr;
909 break;
910 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000911 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100912 }
913
914 /* Check to see if we found a resource */
Markus Elfring61282c02017-08-24 22:24:38 +0200915 if (!allocated_ctrlr)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100916 goto err_ctrlr;
917
Markus Elfring1ff0a192017-08-24 21:52:00 +0200918 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200919 if (!resource)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100920 goto err_alloc;
Markus Elfring94eefcc2017-08-24 21:38:20 +0200921
Martyn Welcha17a75e2009-07-31 09:28:17 +0100922 resource->type = VME_DMA;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000923 resource->entry = &allocated_ctrlr->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100924
925 return resource;
926
927err_alloc:
928 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000929 mutex_lock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100930 dma_ctrlr->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000931 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100932err_ctrlr:
933err_bus:
934 return NULL;
935}
Martyn Welch58e50792009-10-29 16:35:20 +0000936EXPORT_SYMBOL(vme_dma_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100937
Martyn Welchb5bc9802017-03-04 00:34:29 +0000938/**
939 * vme_new_dma_list - Create new VME DMA list.
940 * @resource: Pointer to VME DMA resource.
941 *
942 * Create a new VME DMA list. It is the responsibility of the user to free
943 * the list once it is no longer required with vme_dma_list_free().
944 *
945 * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
946 * VME DMA resource.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100947 */
948struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
949{
Martyn Welcha17a75e2009-07-31 09:28:17 +0100950 struct vme_dma_list *dma_list;
951
952 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000953 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100954 return NULL;
955 }
956
Markus Elfring1ff0a192017-08-24 21:52:00 +0200957 dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200958 if (!dma_list)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100959 return NULL;
Markus Elfring94eefcc2017-08-24 21:38:20 +0200960
Emilio G. Cota886953e2010-11-12 11:14:07 +0000961 INIT_LIST_HEAD(&dma_list->entries);
Markus Elfringa384b2c2017-08-24 22:04:45 +0200962 dma_list->parent = list_entry(resource->entry,
963 struct vme_dma_resource,
964 list);
Emilio G. Cota886953e2010-11-12 11:14:07 +0000965 mutex_init(&dma_list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100966
967 return dma_list;
968}
969EXPORT_SYMBOL(vme_new_dma_list);
970
Martyn Welchb5bc9802017-03-04 00:34:29 +0000971/**
972 * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
973 * @pattern: Value to use used as pattern
974 * @type: Type of pattern to be written.
975 *
976 * Create VME DMA list attribute for pattern generation. It is the
977 * responsibility of the user to free used attributes using
978 * vme_dma_free_attribute().
979 *
980 * Return: Pointer to VME DMA attribute, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100981 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000982struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100983{
984 struct vme_dma_attr *attributes;
985 struct vme_dma_pattern *pattern_attr;
986
Markus Elfring1ff0a192017-08-24 21:52:00 +0200987 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200988 if (!attributes)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100989 goto err_attr;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100990
Markus Elfring1ff0a192017-08-24 21:52:00 +0200991 pattern_attr = kmalloc(sizeof(*pattern_attr), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +0200992 if (!pattern_attr)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100993 goto err_pat;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100994
995 attributes->type = VME_DMA_PATTERN;
996 attributes->private = (void *)pattern_attr;
997
998 pattern_attr->pattern = pattern;
999 pattern_attr->type = type;
1000
1001 return attributes;
1002
Martyn Welcha17a75e2009-07-31 09:28:17 +01001003err_pat:
1004 kfree(attributes);
1005err_attr:
1006 return NULL;
1007}
1008EXPORT_SYMBOL(vme_dma_pattern_attribute);
1009
Martyn Welchb5bc9802017-03-04 00:34:29 +00001010/**
1011 * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
1012 * @address: PCI base address for DMA transfer.
1013 *
1014 * Create VME DMA list attribute pointing to a location on PCI for DMA
1015 * transfers. It is the responsibility of the user to free used attributes
1016 * using vme_dma_free_attribute().
1017 *
1018 * Return: Pointer to VME DMA attribute, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001019 */
1020struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
1021{
1022 struct vme_dma_attr *attributes;
1023 struct vme_dma_pci *pci_attr;
1024
1025 /* XXX Run some sanity checks here */
1026
Markus Elfring1ff0a192017-08-24 21:52:00 +02001027 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001028 if (!attributes)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001029 goto err_attr;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001030
Markus Elfring1ff0a192017-08-24 21:52:00 +02001031 pci_attr = kmalloc(sizeof(*pci_attr), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001032 if (!pci_attr)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001033 goto err_pci;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001034
1035 attributes->type = VME_DMA_PCI;
1036 attributes->private = (void *)pci_attr;
1037
1038 pci_attr->address = address;
1039
1040 return attributes;
1041
Martyn Welcha17a75e2009-07-31 09:28:17 +01001042err_pci:
1043 kfree(attributes);
1044err_attr:
1045 return NULL;
1046}
1047EXPORT_SYMBOL(vme_dma_pci_attribute);
1048
Martyn Welchb5bc9802017-03-04 00:34:29 +00001049/**
1050 * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
1051 * @address: VME base address for DMA transfer.
1052 * @aspace: VME address space to use for DMA transfer.
1053 * @cycle: VME bus cycle to use for DMA transfer.
1054 * @dwidth: VME data width to use for DMA transfer.
1055 *
1056 * Create VME DMA list attribute pointing to a location on the VME bus for DMA
1057 * transfers. It is the responsibility of the user to free used attributes
1058 * using vme_dma_free_attribute().
1059 *
1060 * Return: Pointer to VME DMA attribute, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001061 */
1062struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
Martyn Welch6af04b02011-12-01 17:06:29 +00001063 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001064{
1065 struct vme_dma_attr *attributes;
1066 struct vme_dma_vme *vme_attr;
1067
Markus Elfring1ff0a192017-08-24 21:52:00 +02001068 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001069 if (!attributes)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001070 goto err_attr;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001071
Markus Elfring1ff0a192017-08-24 21:52:00 +02001072 vme_attr = kmalloc(sizeof(*vme_attr), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001073 if (!vme_attr)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001074 goto err_vme;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001075
1076 attributes->type = VME_DMA_VME;
1077 attributes->private = (void *)vme_attr;
1078
1079 vme_attr->address = address;
1080 vme_attr->aspace = aspace;
1081 vme_attr->cycle = cycle;
1082 vme_attr->dwidth = dwidth;
1083
1084 return attributes;
1085
Martyn Welcha17a75e2009-07-31 09:28:17 +01001086err_vme:
1087 kfree(attributes);
1088err_attr:
1089 return NULL;
1090}
1091EXPORT_SYMBOL(vme_dma_vme_attribute);
1092
Martyn Welchb5bc9802017-03-04 00:34:29 +00001093/**
1094 * vme_dma_free_attribute - Free DMA list attribute.
1095 * @attributes: Pointer to DMA list attribute.
1096 *
1097 * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1098 * once vme_dma_list_add() has returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001099 */
1100void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1101{
1102 kfree(attributes->private);
1103 kfree(attributes);
1104}
1105EXPORT_SYMBOL(vme_dma_free_attribute);
1106
Martyn Welchb5bc9802017-03-04 00:34:29 +00001107/**
1108 * vme_dma_list_add - Add enty to a VME DMA list.
1109 * @list: Pointer to VME list.
1110 * @src: Pointer to DMA list attribute to use as source.
1111 * @dest: Pointer to DMA list attribute to use as destination.
1112 * @count: Number of bytes to transfer.
1113 *
1114 * Add an entry to the provided VME DMA list. Entry requires pointers to source
1115 * and destination DMA attributes and a count.
1116 *
1117 * Please note, the attributes supported as source and destinations for
1118 * transfers are hardware dependent.
1119 *
1120 * Return: Zero on success, -EINVAL if operation is not supported on this
1121 * device or if the link list has already been submitted for execution.
1122 * Hardware specific errors also possible.
1123 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001124int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
1125 struct vme_dma_attr *dest, size_t count)
1126{
1127 struct vme_bridge *bridge = list->parent->parent;
1128 int retval;
1129
Markus Elfring61282c02017-08-24 22:24:38 +02001130 if (!bridge->dma_list_add) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001131 printk(KERN_WARNING "Link List DMA generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001132 return -EINVAL;
1133 }
1134
Emilio G. Cota886953e2010-11-12 11:14:07 +00001135 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001136 printk(KERN_ERR "Link List already submitted\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001137 return -EINVAL;
1138 }
1139
1140 retval = bridge->dma_list_add(list, src, dest, count);
1141
Emilio G. Cota886953e2010-11-12 11:14:07 +00001142 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001143
1144 return retval;
1145}
1146EXPORT_SYMBOL(vme_dma_list_add);
1147
Martyn Welchb5bc9802017-03-04 00:34:29 +00001148/**
1149 * vme_dma_list_exec - Queue a VME DMA list for execution.
1150 * @list: Pointer to VME list.
1151 *
1152 * Queue the provided VME DMA list for execution. The call will return once the
1153 * list has been executed.
1154 *
1155 * Return: Zero on success, -EINVAL if operation is not supported on this
1156 * device. Hardware specific errors also possible.
1157 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001158int vme_dma_list_exec(struct vme_dma_list *list)
1159{
1160 struct vme_bridge *bridge = list->parent->parent;
1161 int retval;
1162
Markus Elfring61282c02017-08-24 22:24:38 +02001163 if (!bridge->dma_list_exec) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001164 printk(KERN_ERR "Link List DMA execution not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001165 return -EINVAL;
1166 }
1167
Emilio G. Cota886953e2010-11-12 11:14:07 +00001168 mutex_lock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001169
1170 retval = bridge->dma_list_exec(list);
1171
Emilio G. Cota886953e2010-11-12 11:14:07 +00001172 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001173
1174 return retval;
1175}
1176EXPORT_SYMBOL(vme_dma_list_exec);
1177
Martyn Welchb5bc9802017-03-04 00:34:29 +00001178/**
1179 * vme_dma_list_free - Free a VME DMA list.
1180 * @list: Pointer to VME list.
1181 *
1182 * Free the provided DMA list and all its entries.
1183 *
1184 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1185 * is still in use. Hardware specific errors also possible.
1186 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001187int vme_dma_list_free(struct vme_dma_list *list)
1188{
1189 struct vme_bridge *bridge = list->parent->parent;
1190 int retval;
1191
Markus Elfring61282c02017-08-24 22:24:38 +02001192 if (!bridge->dma_list_empty) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001193 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001194 return -EINVAL;
1195 }
1196
Emilio G. Cota886953e2010-11-12 11:14:07 +00001197 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001198 printk(KERN_ERR "Link List in use\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001199 return -EINVAL;
1200 }
1201
1202 /*
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001203 * Empty out all of the entries from the DMA list. We need to go to the
1204 * low level driver as DMA entries are driver specific.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001205 */
1206 retval = bridge->dma_list_empty(list);
1207 if (retval) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001208 printk(KERN_ERR "Unable to empty link-list entries\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +00001209 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001210 return retval;
1211 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001212 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001213 kfree(list);
1214
1215 return retval;
1216}
1217EXPORT_SYMBOL(vme_dma_list_free);
1218
Martyn Welchb5bc9802017-03-04 00:34:29 +00001219/**
1220 * vme_dma_free - Free a VME DMA resource.
1221 * @resource: Pointer to VME DMA resource.
1222 *
1223 * Free the provided DMA resource so that it may be reallocated.
1224 *
1225 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1226 * is still active.
1227 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001228int vme_dma_free(struct vme_resource *resource)
1229{
1230 struct vme_dma_resource *ctrlr;
1231
1232 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001233 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001234 return -EINVAL;
1235 }
1236
1237 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1238
Emilio G. Cota886953e2010-11-12 11:14:07 +00001239 if (!mutex_trylock(&ctrlr->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001240 printk(KERN_ERR "Resource busy, can't free\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001241 return -EBUSY;
1242 }
1243
Emilio G. Cota886953e2010-11-12 11:14:07 +00001244 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001245 printk(KERN_WARNING "Resource still processing transfers\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +00001246 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001247 return -EBUSY;
1248 }
1249
1250 ctrlr->locked = 0;
1251
Emilio G. Cota886953e2010-11-12 11:14:07 +00001252 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001253
Martyn Welchfd5c2562013-06-06 12:29:16 +01001254 kfree(resource);
1255
Martyn Welcha17a75e2009-07-31 09:28:17 +01001256 return 0;
1257}
1258EXPORT_SYMBOL(vme_dma_free);
1259
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001260void vme_bus_error_handler(struct vme_bridge *bridge,
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +03001261 unsigned long long address, int am)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001262{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001263 struct list_head *handler_pos = NULL;
1264 struct vme_error_handler *handler;
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001265 int handler_triggered = 0;
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001266 u32 aspace = vme_get_aspace(am);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001267
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001268 list_for_each(handler_pos, &bridge->vme_error_handlers) {
1269 handler = list_entry(handler_pos, struct vme_error_handler,
1270 list);
1271 if ((aspace == handler->aspace) &&
1272 (address >= handler->start) &&
1273 (address < handler->end)) {
1274 if (!handler->num_errors)
1275 handler->first_error = address;
1276 if (handler->num_errors != UINT_MAX)
1277 handler->num_errors++;
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001278 handler_triggered = 1;
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001279 }
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001280 }
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001281
1282 if (!handler_triggered)
1283 dev_err(bridge->parent,
1284 "Unhandled VME access error at address 0x%llx\n",
1285 address);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001286}
1287EXPORT_SYMBOL(vme_bus_error_handler);
1288
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001289struct vme_error_handler *vme_register_error_handler(
1290 struct vme_bridge *bridge, u32 aspace,
1291 unsigned long long address, size_t len)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001292{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001293 struct vme_error_handler *handler;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001294
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001295 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
1296 if (!handler)
1297 return NULL;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001298
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001299 handler->aspace = aspace;
1300 handler->start = address;
1301 handler->end = address + len;
1302 handler->num_errors = 0;
1303 handler->first_error = 0;
1304 list_add_tail(&handler->list, &bridge->vme_error_handlers);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001305
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001306 return handler;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001307}
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001308EXPORT_SYMBOL(vme_register_error_handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001309
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001310void vme_unregister_error_handler(struct vme_error_handler *handler)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001311{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001312 list_del(&handler->list);
1313 kfree(handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001314}
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001315EXPORT_SYMBOL(vme_unregister_error_handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001316
Martyn Welchc813f592009-10-29 16:34:54 +00001317void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1318{
1319 void (*call)(int, int, void *);
1320 void *priv_data;
1321
1322 call = bridge->irq[level - 1].callback[statid].func;
1323 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
Markus Elfring61282c02017-08-24 22:24:38 +02001324 if (call)
Martyn Welchc813f592009-10-29 16:34:54 +00001325 call(level, statid, priv_data);
1326 else
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001327 printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001328 level, statid);
Martyn Welchc813f592009-10-29 16:34:54 +00001329}
1330EXPORT_SYMBOL(vme_irq_handler);
1331
Martyn Welchb5bc9802017-03-04 00:34:29 +00001332/**
1333 * vme_irq_request - Request a specific VME interrupt.
1334 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1335 * @level: Interrupt priority being requested.
1336 * @statid: Interrupt vector being requested.
1337 * @callback: Pointer to callback function called when VME interrupt/vector
1338 * received.
1339 * @priv_data: Generic pointer that will be passed to the callback function.
1340 *
1341 * Request callback to be attached as a handler for VME interrupts with provided
1342 * level and statid.
1343 *
1344 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1345 * function is not supported, -EBUSY if the level/statid combination is
1346 * already in use. Hardware specific errors also possible.
1347 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001348int vme_irq_request(struct vme_dev *vdev, int level, int statid,
Martyn Welch29848ac2010-02-18 15:13:05 +00001349 void (*callback)(int, int, void *),
Martyn Welcha17a75e2009-07-31 09:28:17 +01001350 void *priv_data)
1351{
1352 struct vme_bridge *bridge;
1353
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001354 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001355 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001356 printk(KERN_ERR "Can't find VME bus\n");
1357 return -EINVAL;
1358 }
1359
Martyn Welchead1f3e2009-12-15 08:43:02 +00001360 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001361 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001362 return -EINVAL;
1363 }
1364
Markus Elfring61282c02017-08-24 22:24:38 +02001365 if (!bridge->irq_set) {
Martyn Welchc813f592009-10-29 16:34:54 +00001366 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001367 return -EINVAL;
1368 }
1369
Emilio G. Cota886953e2010-11-12 11:14:07 +00001370 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001371
1372 if (bridge->irq[level - 1].callback[statid].func) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001373 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001374 printk(KERN_WARNING "VME Interrupt already taken\n");
1375 return -EBUSY;
1376 }
1377
1378 bridge->irq[level - 1].count++;
1379 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1380 bridge->irq[level - 1].callback[statid].func = callback;
1381
1382 /* Enable IRQ level */
Martyn Welch29848ac2010-02-18 15:13:05 +00001383 bridge->irq_set(bridge, level, 1, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001384
Emilio G. Cota886953e2010-11-12 11:14:07 +00001385 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001386
1387 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001388}
Martyn Welchc813f592009-10-29 16:34:54 +00001389EXPORT_SYMBOL(vme_irq_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001390
Martyn Welchb5bc9802017-03-04 00:34:29 +00001391/**
1392 * vme_irq_free - Free a VME interrupt.
1393 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1394 * @level: Interrupt priority of interrupt being freed.
1395 * @statid: Interrupt vector of interrupt being freed.
1396 *
1397 * Remove previously attached callback from VME interrupt priority/vector.
1398 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001399void vme_irq_free(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001400{
1401 struct vme_bridge *bridge;
1402
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001403 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001404 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001405 printk(KERN_ERR "Can't find VME bus\n");
1406 return;
1407 }
1408
Martyn Welchead1f3e2009-12-15 08:43:02 +00001409 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001410 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001411 return;
1412 }
1413
Markus Elfring61282c02017-08-24 22:24:38 +02001414 if (!bridge->irq_set) {
Martyn Welchc813f592009-10-29 16:34:54 +00001415 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001416 return;
1417 }
1418
Emilio G. Cota886953e2010-11-12 11:14:07 +00001419 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001420
1421 bridge->irq[level - 1].count--;
1422
1423 /* Disable IRQ level if no more interrupts attached at this level*/
1424 if (bridge->irq[level - 1].count == 0)
Martyn Welch29848ac2010-02-18 15:13:05 +00001425 bridge->irq_set(bridge, level, 0, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001426
1427 bridge->irq[level - 1].callback[statid].func = NULL;
1428 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1429
Emilio G. Cota886953e2010-11-12 11:14:07 +00001430 mutex_unlock(&bridge->irq_mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001431}
Martyn Welchc813f592009-10-29 16:34:54 +00001432EXPORT_SYMBOL(vme_irq_free);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001433
Martyn Welchb5bc9802017-03-04 00:34:29 +00001434/**
1435 * vme_irq_generate - Generate VME interrupt.
1436 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1437 * @level: Interrupt priority at which to assert the interrupt.
1438 * @statid: Interrupt vector to associate with the interrupt.
1439 *
1440 * Generate a VME interrupt of the provided level and with the provided
1441 * statid.
1442 *
1443 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1444 * function is not supported. Hardware specific errors also possible.
1445 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001446int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001447{
1448 struct vme_bridge *bridge;
1449
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001450 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001451 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001452 printk(KERN_ERR "Can't find VME bus\n");
1453 return -EINVAL;
1454 }
1455
Martyn Welchead1f3e2009-12-15 08:43:02 +00001456 if ((level < 1) || (level > 7)) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001457 printk(KERN_WARNING "Invalid interrupt level\n");
1458 return -EINVAL;
1459 }
1460
Markus Elfring61282c02017-08-24 22:24:38 +02001461 if (!bridge->irq_generate) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001462 printk(KERN_WARNING "Interrupt generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001463 return -EINVAL;
1464 }
1465
Martyn Welch29848ac2010-02-18 15:13:05 +00001466 return bridge->irq_generate(bridge, level, statid);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001467}
Martyn Welchc813f592009-10-29 16:34:54 +00001468EXPORT_SYMBOL(vme_irq_generate);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001469
Martyn Welchb5bc9802017-03-04 00:34:29 +00001470/**
1471 * vme_lm_request - Request a VME location monitor
1472 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1473 *
1474 * Allocate a location monitor resource to the driver. A location monitor
1475 * allows the driver to monitor accesses to a contiguous number of
1476 * addresses on the VME bus.
1477 *
1478 * Return: Pointer to a VME resource on success or NULL on failure.
Martyn Welch42fb5032009-08-11 17:44:56 +01001479 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001480struct vme_resource *vme_lm_request(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001481{
1482 struct vme_bridge *bridge;
Martyn Welch42fb5032009-08-11 17:44:56 +01001483 struct list_head *lm_pos = NULL;
1484 struct vme_lm_resource *allocated_lm = NULL;
1485 struct vme_lm_resource *lm = NULL;
1486 struct vme_resource *resource = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001487
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001488 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001489 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001490 printk(KERN_ERR "Can't find VME bus\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001491 goto err_bus;
1492 }
1493
Martyn Welchb5bc9802017-03-04 00:34:29 +00001494 /* Loop through LM resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001495 list_for_each(lm_pos, &bridge->lm_resources) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001496 lm = list_entry(lm_pos,
1497 struct vme_lm_resource, list);
Markus Elfring61282c02017-08-24 22:24:38 +02001498 if (!lm) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001499 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001500 continue;
1501 }
1502
1503 /* Find an unlocked controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001504 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001505 if (lm->locked == 0) {
1506 lm->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001507 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001508 allocated_lm = lm;
1509 break;
1510 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001511 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001512 }
1513
1514 /* Check to see if we found a resource */
Markus Elfring61282c02017-08-24 22:24:38 +02001515 if (!allocated_lm)
Martyn Welch42fb5032009-08-11 17:44:56 +01001516 goto err_lm;
1517
Markus Elfring1ff0a192017-08-24 21:52:00 +02001518 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
Markus Elfring94eefcc2017-08-24 21:38:20 +02001519 if (!resource)
Martyn Welch42fb5032009-08-11 17:44:56 +01001520 goto err_alloc;
Markus Elfring94eefcc2017-08-24 21:38:20 +02001521
Martyn Welch42fb5032009-08-11 17:44:56 +01001522 resource->type = VME_LM;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001523 resource->entry = &allocated_lm->list;
Martyn Welch42fb5032009-08-11 17:44:56 +01001524
1525 return resource;
1526
1527err_alloc:
1528 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001529 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001530 lm->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001531 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001532err_lm:
1533err_bus:
1534 return NULL;
1535}
1536EXPORT_SYMBOL(vme_lm_request);
1537
Martyn Welchb5bc9802017-03-04 00:34:29 +00001538/**
1539 * vme_lm_count - Determine number of VME Addresses monitored
1540 * @resource: Pointer to VME location monitor resource.
1541 *
1542 * The number of contiguous addresses monitored is hardware dependent.
1543 * Return the number of contiguous addresses monitored by the
1544 * location monitor.
1545 *
1546 * Return: Count of addresses monitored or -EINVAL when provided with an
1547 * invalid location monitor resource.
1548 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001549int vme_lm_count(struct vme_resource *resource)
1550{
1551 struct vme_lm_resource *lm;
1552
1553 if (resource->type != VME_LM) {
1554 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001555 return -EINVAL;
1556 }
1557
Martyn Welch42fb5032009-08-11 17:44:56 +01001558 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1559
1560 return lm->monitors;
1561}
1562EXPORT_SYMBOL(vme_lm_count);
1563
Martyn Welchb5bc9802017-03-04 00:34:29 +00001564/**
1565 * vme_lm_set - Configure location monitor
1566 * @resource: Pointer to VME location monitor resource.
1567 * @lm_base: Base address to monitor.
1568 * @aspace: VME address space to monitor.
1569 * @cycle: VME bus cycle type to monitor.
1570 *
1571 * Set the base address, address space and cycle type of accesses to be
1572 * monitored by the location monitor.
1573 *
1574 * Return: Zero on success, -EINVAL when provided with an invalid location
1575 * monitor resource or function is not supported. Hardware specific
1576 * errors may also be returned.
1577 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001578int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001579 u32 aspace, u32 cycle)
Martyn Welch42fb5032009-08-11 17:44:56 +01001580{
1581 struct vme_bridge *bridge = find_bridge(resource);
1582 struct vme_lm_resource *lm;
1583
1584 if (resource->type != VME_LM) {
1585 printk(KERN_ERR "Not a Location Monitor resource\n");
1586 return -EINVAL;
1587 }
1588
1589 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1590
Markus Elfring61282c02017-08-24 22:24:38 +02001591 if (!bridge->lm_set) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001592 printk(KERN_ERR "vme_lm_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001593 return -EINVAL;
1594 }
1595
Martyn Welch8be92262009-10-29 16:35:08 +00001596 return bridge->lm_set(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001597}
1598EXPORT_SYMBOL(vme_lm_set);
1599
Martyn Welchb5bc9802017-03-04 00:34:29 +00001600/**
1601 * vme_lm_get - Retrieve location monitor settings
1602 * @resource: Pointer to VME location monitor resource.
1603 * @lm_base: Pointer used to output the base address monitored.
1604 * @aspace: Pointer used to output the address space monitored.
1605 * @cycle: Pointer used to output the VME bus cycle type monitored.
1606 *
1607 * Retrieve the base address, address space and cycle type of accesses to
1608 * be monitored by the location monitor.
1609 *
1610 * Return: Zero on success, -EINVAL when provided with an invalid location
1611 * monitor resource or function is not supported. Hardware specific
1612 * errors may also be returned.
1613 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001614int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001615 u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001616{
Martyn Welch42fb5032009-08-11 17:44:56 +01001617 struct vme_bridge *bridge = find_bridge(resource);
1618 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001619
Martyn Welch42fb5032009-08-11 17:44:56 +01001620 if (resource->type != VME_LM) {
1621 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001622 return -EINVAL;
1623 }
1624
Martyn Welch42fb5032009-08-11 17:44:56 +01001625 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1626
Markus Elfring61282c02017-08-24 22:24:38 +02001627 if (!bridge->lm_get) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001628 printk(KERN_ERR "vme_lm_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001629 return -EINVAL;
1630 }
1631
Martyn Welch42fb5032009-08-11 17:44:56 +01001632 return bridge->lm_get(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001633}
1634EXPORT_SYMBOL(vme_lm_get);
1635
Martyn Welchb5bc9802017-03-04 00:34:29 +00001636/**
1637 * vme_lm_attach - Provide callback for location monitor address
1638 * @resource: Pointer to VME location monitor resource.
1639 * @monitor: Offset to which callback should be attached.
1640 * @callback: Pointer to callback function called when triggered.
1641 * @data: Generic pointer that will be passed to the callback function.
1642 *
1643 * Attach a callback to the specificed offset into the location monitors
1644 * monitored addresses. A generic pointer is provided to allow data to be
1645 * passed to the callback when called.
1646 *
1647 * Return: Zero on success, -EINVAL when provided with an invalid location
1648 * monitor resource or function is not supported. Hardware specific
1649 * errors may also be returned.
1650 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001651int vme_lm_attach(struct vme_resource *resource, int monitor,
Aaron Sierrafa54b322016-04-29 16:41:02 -05001652 void (*callback)(void *), void *data)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001653{
Martyn Welch42fb5032009-08-11 17:44:56 +01001654 struct vme_bridge *bridge = find_bridge(resource);
1655 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001656
Martyn Welch42fb5032009-08-11 17:44:56 +01001657 if (resource->type != VME_LM) {
1658 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001659 return -EINVAL;
1660 }
1661
Martyn Welch42fb5032009-08-11 17:44:56 +01001662 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1663
Markus Elfring61282c02017-08-24 22:24:38 +02001664 if (!bridge->lm_attach) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001665 printk(KERN_ERR "vme_lm_attach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001666 return -EINVAL;
1667 }
1668
Aaron Sierrafa54b322016-04-29 16:41:02 -05001669 return bridge->lm_attach(lm, monitor, callback, data);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001670}
1671EXPORT_SYMBOL(vme_lm_attach);
1672
Martyn Welchb5bc9802017-03-04 00:34:29 +00001673/**
1674 * vme_lm_detach - Remove callback for location monitor address
1675 * @resource: Pointer to VME location monitor resource.
1676 * @monitor: Offset to which callback should be removed.
1677 *
1678 * Remove the callback associated with the specificed offset into the
1679 * location monitors monitored addresses.
1680 *
1681 * Return: Zero on success, -EINVAL when provided with an invalid location
1682 * monitor resource or function is not supported. Hardware specific
1683 * errors may also be returned.
1684 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001685int vme_lm_detach(struct vme_resource *resource, int monitor)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001686{
Martyn Welch42fb5032009-08-11 17:44:56 +01001687 struct vme_bridge *bridge = find_bridge(resource);
1688 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001689
Martyn Welch42fb5032009-08-11 17:44:56 +01001690 if (resource->type != VME_LM) {
1691 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001692 return -EINVAL;
1693 }
1694
Martyn Welch42fb5032009-08-11 17:44:56 +01001695 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1696
Markus Elfring61282c02017-08-24 22:24:38 +02001697 if (!bridge->lm_detach) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001698 printk(KERN_ERR "vme_lm_detach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001699 return -EINVAL;
1700 }
1701
Martyn Welch42fb5032009-08-11 17:44:56 +01001702 return bridge->lm_detach(lm, monitor);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001703}
1704EXPORT_SYMBOL(vme_lm_detach);
1705
Martyn Welchb5bc9802017-03-04 00:34:29 +00001706/**
1707 * vme_lm_free - Free allocated VME location monitor
1708 * @resource: Pointer to VME location monitor resource.
1709 *
1710 * Free allocation of a VME location monitor.
1711 *
1712 * WARNING: This function currently expects that any callbacks that have
1713 * been attached to the location monitor have been removed.
1714 *
1715 * Return: Zero on success, -EINVAL when provided with an invalid location
1716 * monitor resource.
1717 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001718void vme_lm_free(struct vme_resource *resource)
1719{
1720 struct vme_lm_resource *lm;
1721
1722 if (resource->type != VME_LM) {
1723 printk(KERN_ERR "Not a Location Monitor resource\n");
1724 return;
1725 }
1726
1727 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1728
Emilio G. Cota886953e2010-11-12 11:14:07 +00001729 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001730
Martyn Welch8be92262009-10-29 16:35:08 +00001731 /* XXX
1732 * Check to see that there aren't any callbacks still attached, if
1733 * there are we should probably be detaching them!
1734 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001735
1736 lm->locked = 0;
1737
Emilio G. Cota886953e2010-11-12 11:14:07 +00001738 mutex_unlock(&lm->mtx);
Martyn Welch8be92262009-10-29 16:35:08 +00001739
1740 kfree(resource);
Martyn Welch42fb5032009-08-11 17:44:56 +01001741}
1742EXPORT_SYMBOL(vme_lm_free);
1743
Martyn Welchb5bc9802017-03-04 00:34:29 +00001744/**
1745 * vme_slot_num - Retrieve slot ID
1746 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1747 *
1748 * Retrieve the slot ID associated with the provided VME device.
1749 *
1750 * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1751 * or the function is not supported. Hardware specific errors may also
1752 * be returned.
1753 */
Martyn Welchd7729f02013-11-08 11:58:35 +00001754int vme_slot_num(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001755{
1756 struct vme_bridge *bridge;
1757
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001758 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001759 if (!bridge) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001760 printk(KERN_ERR "Can't find VME bus\n");
1761 return -EINVAL;
1762 }
1763
Markus Elfring61282c02017-08-24 22:24:38 +02001764 if (!bridge->slot_get) {
Martyn Welchd7729f02013-11-08 11:58:35 +00001765 printk(KERN_WARNING "vme_slot_num not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001766 return -EINVAL;
1767 }
1768
Martyn Welch29848ac2010-02-18 15:13:05 +00001769 return bridge->slot_get(bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001770}
Martyn Welchd7729f02013-11-08 11:58:35 +00001771EXPORT_SYMBOL(vme_slot_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001772
Martyn Welchb5bc9802017-03-04 00:34:29 +00001773/**
1774 * vme_bus_num - Retrieve bus number
1775 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1776 *
1777 * Retrieve the bus enumeration associated with the provided VME device.
1778 *
1779 * Return: The bus number on success, -EINVAL if VME bridge cannot be
1780 * determined.
1781 */
Martyn Welch978f47d2013-11-08 11:58:34 +00001782int vme_bus_num(struct vme_dev *vdev)
1783{
1784 struct vme_bridge *bridge;
1785
1786 bridge = vdev->bridge;
Markus Elfring61282c02017-08-24 22:24:38 +02001787 if (!bridge) {
Martyn Welch978f47d2013-11-08 11:58:34 +00001788 pr_err("Can't find VME bus\n");
1789 return -EINVAL;
1790 }
1791
1792 return bridge->num;
1793}
1794EXPORT_SYMBOL(vme_bus_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001795
1796/* - Bridge Registration --------------------------------------------------- */
1797
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001798static void vme_dev_release(struct device *dev)
1799{
1800 kfree(dev_to_vme_dev(dev));
1801}
1802
Aaron Sierra326071b2016-04-24 15:11:38 -05001803/* Common bridge initialization */
1804struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
1805{
1806 INIT_LIST_HEAD(&bridge->vme_error_handlers);
1807 INIT_LIST_HEAD(&bridge->master_resources);
1808 INIT_LIST_HEAD(&bridge->slave_resources);
1809 INIT_LIST_HEAD(&bridge->dma_resources);
1810 INIT_LIST_HEAD(&bridge->lm_resources);
1811 mutex_init(&bridge->irq_mtx);
1812
1813 return bridge;
1814}
1815EXPORT_SYMBOL(vme_init_bridge);
1816
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001817int vme_register_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001818{
1819 int i;
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001820 int ret = -1;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001821
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001822 mutex_lock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001823 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001824 if ((vme_bus_numbers & (1 << i)) == 0) {
1825 vme_bus_numbers |= (1 << i);
1826 bridge->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001827 INIT_LIST_HEAD(&bridge->devices);
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001828 list_add_tail(&bridge->bus_list, &vme_bus_list);
1829 ret = 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001830 break;
1831 }
1832 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001833 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001834
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001835 return ret;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001836}
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001837EXPORT_SYMBOL(vme_register_bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001838
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001839void vme_unregister_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001840{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001841 struct vme_dev *vdev;
1842 struct vme_dev *tmp;
1843
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001844 mutex_lock(&vme_buses_lock);
1845 vme_bus_numbers &= ~(1 << bridge->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001846 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1847 list_del(&vdev->drv_list);
1848 list_del(&vdev->bridge_list);
1849 device_unregister(&vdev->dev);
1850 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001851 list_del(&bridge->bus_list);
1852 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001853}
Martyn Welcha17a75e2009-07-31 09:28:17 +01001854EXPORT_SYMBOL(vme_unregister_bridge);
1855
Martyn Welcha17a75e2009-07-31 09:28:17 +01001856/* - Driver Registration --------------------------------------------------- */
1857
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001858static int __vme_register_driver_bus(struct vme_driver *drv,
1859 struct vme_bridge *bridge, unsigned int ndevs)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001860{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001861 int err;
1862 unsigned int i;
1863 struct vme_dev *vdev;
1864 struct vme_dev *tmp;
1865
1866 for (i = 0; i < ndevs; i++) {
Markus Elfring1ff0a192017-08-24 21:52:00 +02001867 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001868 if (!vdev) {
1869 err = -ENOMEM;
1870 goto err_devalloc;
1871 }
Manohar Vangaa916a392011-09-26 11:27:17 +02001872 vdev->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001873 vdev->bridge = bridge;
1874 vdev->dev.platform_data = drv;
1875 vdev->dev.release = vme_dev_release;
1876 vdev->dev.parent = bridge->parent;
1877 vdev->dev.bus = &vme_bus_type;
Manohar Vangaa916a392011-09-26 11:27:17 +02001878 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1879 vdev->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001880
1881 err = device_register(&vdev->dev);
1882 if (err)
1883 goto err_reg;
1884
1885 if (vdev->dev.platform_data) {
1886 list_add_tail(&vdev->drv_list, &drv->devices);
1887 list_add_tail(&vdev->bridge_list, &bridge->devices);
1888 } else
1889 device_unregister(&vdev->dev);
1890 }
1891 return 0;
1892
1893err_reg:
Emilio G. Cotadef18202013-02-13 13:47:54 -05001894 put_device(&vdev->dev);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001895 kfree(vdev);
1896err_devalloc:
1897 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1898 list_del(&vdev->drv_list);
1899 list_del(&vdev->bridge_list);
1900 device_unregister(&vdev->dev);
1901 }
1902 return err;
1903}
1904
1905static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1906{
1907 struct vme_bridge *bridge;
1908 int err = 0;
1909
1910 mutex_lock(&vme_buses_lock);
1911 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1912 /*
1913 * This cannot cause trouble as we already have vme_buses_lock
1914 * and if the bridge is removed, it will have to go through
1915 * vme_unregister_bridge() to do it (which calls remove() on
1916 * the bridge which in turn tries to acquire vme_buses_lock and
Manohar Vangac26f6112011-11-04 11:12:29 +01001917 * will have to wait).
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001918 */
1919 err = __vme_register_driver_bus(drv, bridge, ndevs);
1920 if (err)
1921 break;
1922 }
1923 mutex_unlock(&vme_buses_lock);
1924 return err;
1925}
1926
Martyn Welchb5bc9802017-03-04 00:34:29 +00001927/**
1928 * vme_register_driver - Register a VME driver
1929 * @drv: Pointer to VME driver structure to register.
1930 * @ndevs: Maximum number of devices to allow to be enumerated.
1931 *
1932 * Register a VME device driver with the VME subsystem.
1933 *
1934 * Return: Zero on success, error value on registration failure.
1935 */
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001936int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1937{
1938 int err;
1939
Martyn Welcha17a75e2009-07-31 09:28:17 +01001940 drv->driver.name = drv->name;
1941 drv->driver.bus = &vme_bus_type;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001942 INIT_LIST_HEAD(&drv->devices);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001943
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001944 err = driver_register(&drv->driver);
1945 if (err)
1946 return err;
1947
1948 err = __vme_register_driver(drv, ndevs);
1949 if (err)
1950 driver_unregister(&drv->driver);
1951
1952 return err;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001953}
1954EXPORT_SYMBOL(vme_register_driver);
1955
Martyn Welchb5bc9802017-03-04 00:34:29 +00001956/**
1957 * vme_unregister_driver - Unregister a VME driver
1958 * @drv: Pointer to VME driver structure to unregister.
1959 *
1960 * Unregister a VME device driver from the VME subsystem.
1961 */
Martyn Welchead1f3e2009-12-15 08:43:02 +00001962void vme_unregister_driver(struct vme_driver *drv)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001963{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001964 struct vme_dev *dev, *dev_tmp;
1965
1966 mutex_lock(&vme_buses_lock);
1967 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1968 list_del(&dev->drv_list);
1969 list_del(&dev->bridge_list);
1970 device_unregister(&dev->dev);
1971 }
1972 mutex_unlock(&vme_buses_lock);
1973
Martyn Welcha17a75e2009-07-31 09:28:17 +01001974 driver_unregister(&drv->driver);
1975}
1976EXPORT_SYMBOL(vme_unregister_driver);
1977
1978/* - Bus Registration ------------------------------------------------------ */
1979
Martyn Welcha17a75e2009-07-31 09:28:17 +01001980static int vme_bus_match(struct device *dev, struct device_driver *drv)
1981{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001982 struct vme_driver *vme_drv;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001983
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001984 vme_drv = container_of(drv, struct vme_driver, driver);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001985
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001986 if (dev->platform_data == vme_drv) {
1987 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001988
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001989 if (vme_drv->match && vme_drv->match(vdev))
1990 return 1;
1991
1992 dev->platform_data = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001993 }
Martyn Welcha17a75e2009-07-31 09:28:17 +01001994 return 0;
1995}
1996
1997static int vme_bus_probe(struct device *dev)
1998{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001999 struct vme_driver *driver;
2000 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002001
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002002 driver = dev->platform_data;
Markus Elfring61282c02017-08-24 22:24:38 +02002003 if (driver->probe)
Markus Elfring8af70cd92017-08-24 22:32:14 +02002004 return driver->probe(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002005
Markus Elfring8af70cd92017-08-24 22:32:14 +02002006 return -ENODEV;
Martyn Welcha17a75e2009-07-31 09:28:17 +01002007}
2008
Stefano Babic97974842017-01-20 10:38:20 -05002009static int vme_bus_remove(struct device *dev)
2010{
Stefano Babic97974842017-01-20 10:38:20 -05002011 struct vme_driver *driver;
2012 struct vme_dev *vdev = dev_to_vme_dev(dev);
2013
2014 driver = dev->platform_data;
Markus Elfring61282c02017-08-24 22:24:38 +02002015 if (driver->remove)
Markus Elfring8af70cd92017-08-24 22:32:14 +02002016 return driver->remove(vdev);
Stefano Babic97974842017-01-20 10:38:20 -05002017
Markus Elfring8af70cd92017-08-24 22:32:14 +02002018 return -ENODEV;
Stefano Babic97974842017-01-20 10:38:20 -05002019}
2020
Martyn Welcha17a75e2009-07-31 09:28:17 +01002021struct bus_type vme_bus_type = {
2022 .name = "vme",
2023 .match = vme_bus_match,
2024 .probe = vme_bus_probe,
Stefano Babic97974842017-01-20 10:38:20 -05002025 .remove = vme_bus_remove,
Martyn Welcha17a75e2009-07-31 09:28:17 +01002026};
2027EXPORT_SYMBOL(vme_bus_type);
2028
Martyn Welchead1f3e2009-12-15 08:43:02 +00002029static int __init vme_init(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01002030{
2031 return bus_register(&vme_bus_type);
2032}
Aaron Sierrac326cc02013-12-09 09:54:42 -06002033subsys_initcall(vme_init);