]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/vme/vme.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec
[mirror_ubuntu-artful-kernel.git] / drivers / vme / vme.c
CommitLineData
a17a75e2
MW
1/*
2 * VME Bridge Framework
3 *
66bd8db5
MW
4 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
a17a75e2
MW
6 *
7 * Based on work by Tom Armistead and Ajit Prem
8 * Copyright 2004 Motorola Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 */
15
050c3d52
PG
16#include <linux/init.h>
17#include <linux/export.h>
a17a75e2
MW
18#include <linux/mm.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/pci.h>
23#include <linux/poll.h>
24#include <linux/highmem.h>
25#include <linux/interrupt.h>
26#include <linux/pagemap.h>
27#include <linux/device.h>
28#include <linux/dma-mapping.h>
29#include <linux/syscalls.h>
400822fe 30#include <linux/mutex.h>
a17a75e2 31#include <linux/spinlock.h>
5a0e3ad6 32#include <linux/slab.h>
db3b9e99 33#include <linux/vme.h>
a17a75e2 34
a17a75e2
MW
35#include "vme_bridge.h"
36
733e3ef0 37/* Bitmask and list of registered buses both protected by common mutex */
a17a75e2 38static unsigned int vme_bus_numbers;
733e3ef0
MV
39static LIST_HEAD(vme_bus_list);
40static DEFINE_MUTEX(vme_buses_lock);
a17a75e2 41
ead1f3e3 42static int __init vme_init(void);
a17a75e2 43
8f966dc4 44static struct vme_dev *dev_to_vme_dev(struct device *dev)
a17a75e2 45{
8f966dc4 46 return container_of(dev, struct vme_dev, dev);
a17a75e2
MW
47}
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;
42fb5031
MW
68 case VME_LM:
69 return list_entry(resource->entry, struct vme_lm_resource,
70 list)->parent;
71 break;
a17a75e2
MW
72 default:
73 printk(KERN_ERR "Unknown resource type\n");
74 return NULL;
75 break;
76 }
77}
78
b5bc980a
MW
79/**
80 * vme_free_consistent - Allocate contiguous memory.
81 * @resource: Pointer to VME resource.
82 * @size: Size of allocation required.
83 * @dma: Pointer to variable to store physical address of allocation.
84 *
a17a75e2
MW
85 * Allocate a contiguous block of memory for use by the driver. This is used to
86 * create the buffers for the slave windows.
b5bc980a
MW
87 *
88 * Return: Virtual address of allocation on success, NULL on failure.
a17a75e2 89 */
ead1f3e3 90void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
a17a75e2
MW
91 dma_addr_t *dma)
92{
93 struct vme_bridge *bridge;
a17a75e2 94
ead1f3e3
MW
95 if (resource == NULL) {
96 printk(KERN_ERR "No resource\n");
a17a75e2
MW
97 return NULL;
98 }
99
100 bridge = find_bridge(resource);
ead1f3e3
MW
101 if (bridge == NULL) {
102 printk(KERN_ERR "Can't find bridge\n");
a17a75e2
MW
103 return NULL;
104 }
105
a17a75e2 106 if (bridge->parent == NULL) {
25958ce3 107 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
7f58f025
MV
108 return NULL;
109 }
110
111 if (bridge->alloc_consistent == NULL) {
25958ce3
GKH
112 printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
113 bridge->name);
a17a75e2
MW
114 return NULL;
115 }
a17a75e2 116
7f58f025 117 return bridge->alloc_consistent(bridge->parent, size, dma);
a17a75e2
MW
118}
119EXPORT_SYMBOL(vme_alloc_consistent);
120
b5bc980a
MW
121/**
122 * vme_free_consistent - Free previously allocated memory.
123 * @resource: Pointer to VME resource.
124 * @size: Size of allocation to free.
125 * @vaddr: Virtual address of allocation.
126 * @dma: Physical address of allocation.
127 *
128 * Free previously allocated block of contiguous memory.
a17a75e2
MW
129 */
130void vme_free_consistent(struct vme_resource *resource, size_t size,
131 void *vaddr, dma_addr_t dma)
132{
133 struct vme_bridge *bridge;
a17a75e2 134
ead1f3e3
MW
135 if (resource == NULL) {
136 printk(KERN_ERR "No resource\n");
a17a75e2
MW
137 return;
138 }
139
140 bridge = find_bridge(resource);
ead1f3e3
MW
141 if (bridge == NULL) {
142 printk(KERN_ERR "Can't find bridge\n");
a17a75e2
MW
143 return;
144 }
145
7f58f025 146 if (bridge->parent == NULL) {
25958ce3 147 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
7f58f025
MV
148 return;
149 }
150
151 if (bridge->free_consistent == NULL) {
25958ce3
GKH
152 printk(KERN_ERR "free_consistent not supported by bridge %s\n",
153 bridge->name);
7f58f025
MV
154 return;
155 }
a17a75e2 156
7f58f025 157 bridge->free_consistent(bridge->parent, size, vaddr, dma);
a17a75e2
MW
158}
159EXPORT_SYMBOL(vme_free_consistent);
160
b5bc980a
MW
161/**
162 * vme_get_size - Helper function returning size of a VME window
163 * @resource: Pointer to VME slave or master resource.
164 *
165 * Determine the size of the VME window provided. This is a helper
166 * function, wrappering the call to vme_master_get or vme_slave_get
167 * depending on the type of window resource handed to it.
168 *
169 * Return: Size of the window on success, zero on failure.
170 */
a17a75e2
MW
171size_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;
6af04b06 176 u32 aspace, cycle, dwidth;
a17a75e2
MW
177
178 switch (resource->type) {
179 case VME_MASTER:
180 retval = vme_master_get(resource, &enabled, &base, &size,
181 &aspace, &cycle, &dwidth);
6ad37567
MW
182 if (retval)
183 return 0;
a17a75e2
MW
184
185 return size;
186 break;
187 case VME_SLAVE:
188 retval = vme_slave_get(resource, &enabled, &base, &size,
189 &buf_base, &aspace, &cycle);
6ad37567
MW
190 if (retval)
191 return 0;
a17a75e2
MW
192
193 return size;
194 break;
195 case VME_DMA:
196 return 0;
197 break;
198 default:
199 printk(KERN_ERR "Unknown resource type\n");
200 return 0;
201 break;
202 }
203}
204EXPORT_SYMBOL(vme_get_size);
205
ef73f886
DK
206int vme_check_window(u32 aspace, unsigned long long vme_base,
207 unsigned long long size)
a17a75e2
MW
208{
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:
e7fd80cb
DK
228 if ((size != 0) && (vme_base > U64_MAX + 1 - size))
229 retval = -EFAULT;
a17a75e2
MW
230 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:
ead1f3e3 243 printk(KERN_ERR "Invalid address space\n");
a17a75e2
MW
244 retval = -EINVAL;
245 break;
246 }
247
248 return retval;
249}
ef73f886 250EXPORT_SYMBOL(vme_check_window);
a17a75e2 251
472f16f3
DK
252static 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
b5bc980a
MW
285/**
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.
a17a75e2 295 */
6af04b06
MW
296struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
297 u32 cycle)
a17a75e2
MW
298{
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
8f966dc4 305 bridge = vdev->bridge;
a17a75e2
MW
306 if (bridge == NULL) {
307 printk(KERN_ERR "Can't find VME bus\n");
308 goto err_bus;
309 }
310
311 /* Loop through slave resources */
886953e9 312 list_for_each(slave_pos, &bridge->slave_resources) {
a17a75e2
MW
313 slave_image = list_entry(slave_pos,
314 struct vme_slave_resource, list);
315
316 if (slave_image == NULL) {
ead1f3e3 317 printk(KERN_ERR "Registered NULL Slave resource\n");
a17a75e2
MW
318 continue;
319 }
320
321 /* Find an unlocked and compatible image */
886953e9 322 mutex_lock(&slave_image->mtx);
ead1f3e3 323 if (((slave_image->address_attr & address) == address) &&
a17a75e2
MW
324 ((slave_image->cycle_attr & cycle) == cycle) &&
325 (slave_image->locked == 0)) {
326
327 slave_image->locked = 1;
886953e9 328 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
329 allocated_image = slave_image;
330 break;
331 }
886953e9 332 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
333 }
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;
886953e9 345 resource->entry = &allocated_image->list;
a17a75e2
MW
346
347 return resource;
348
349err_alloc:
350 /* Unlock image */
886953e9 351 mutex_lock(&slave_image->mtx);
a17a75e2 352 slave_image->locked = 0;
886953e9 353 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
354err_image:
355err_bus:
356 return NULL;
357}
358EXPORT_SYMBOL(vme_slave_request);
359
b5bc980a
MW
360/**
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 */
ead1f3e3 377int vme_slave_set(struct vme_resource *resource, int enabled,
a17a75e2 378 unsigned long long vme_base, unsigned long long size,
6af04b06 379 dma_addr_t buf_base, u32 aspace, u32 cycle)
a17a75e2
MW
380{
381 struct vme_bridge *bridge = find_bridge(resource);
382 struct vme_slave_resource *image;
383 int retval;
384
385 if (resource->type != VME_SLAVE) {
ead1f3e3 386 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
387 return -EINVAL;
388 }
389
390 image = list_entry(resource->entry, struct vme_slave_resource, list);
391
392 if (bridge->slave_set == NULL) {
ead1f3e3 393 printk(KERN_ERR "Function not supported\n");
a17a75e2
MW
394 return -ENOSYS;
395 }
396
ead1f3e3 397 if (!(((image->address_attr & aspace) == aspace) &&
a17a75e2 398 ((image->cycle_attr & cycle) == cycle))) {
ead1f3e3 399 printk(KERN_ERR "Invalid attributes\n");
a17a75e2
MW
400 return -EINVAL;
401 }
402
403 retval = vme_check_window(aspace, vme_base, size);
ead1f3e3 404 if (retval)
a17a75e2
MW
405 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
b5bc980a
MW
412/**
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 */
ead1f3e3 427int vme_slave_get(struct vme_resource *resource, int *enabled,
a17a75e2 428 unsigned long long *vme_base, unsigned long long *size,
6af04b06 429 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
a17a75e2
MW
430{
431 struct vme_bridge *bridge = find_bridge(resource);
432 struct vme_slave_resource *image;
433
434 if (resource->type != VME_SLAVE) {
ead1f3e3 435 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
436 return -EINVAL;
437 }
438
439 image = list_entry(resource->entry, struct vme_slave_resource, list);
440
51a569f7 441 if (bridge->slave_get == NULL) {
ead1f3e3 442 printk(KERN_ERR "vme_slave_get not supported\n");
a17a75e2
MW
443 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
b5bc980a
MW
451/**
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 */
a17a75e2
MW
457void vme_slave_free(struct vme_resource *resource)
458{
459 struct vme_slave_resource *slave_image;
460
461 if (resource->type != VME_SLAVE) {
ead1f3e3 462 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
463 return;
464 }
465
466 slave_image = list_entry(resource->entry, struct vme_slave_resource,
467 list);
468 if (slave_image == NULL) {
ead1f3e3 469 printk(KERN_ERR "Can't find slave resource\n");
a17a75e2
MW
470 return;
471 }
472
473 /* Unlock image */
886953e9 474 mutex_lock(&slave_image->mtx);
a17a75e2
MW
475 if (slave_image->locked == 0)
476 printk(KERN_ERR "Image is already free\n");
477
478 slave_image->locked = 0;
886953e9 479 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
480
481 /* Free up resource memory */
482 kfree(resource);
483}
484EXPORT_SYMBOL(vme_slave_free);
485
b5bc980a
MW
486/**
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.
a17a75e2 497 */
6af04b06
MW
498struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
499 u32 cycle, u32 dwidth)
a17a75e2
MW
500{
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
8f966dc4 507 bridge = vdev->bridge;
a17a75e2
MW
508 if (bridge == NULL) {
509 printk(KERN_ERR "Can't find VME bus\n");
510 goto err_bus;
511 }
512
513 /* Loop through master resources */
886953e9 514 list_for_each(master_pos, &bridge->master_resources) {
a17a75e2
MW
515 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 */
886953e9 524 spin_lock(&master_image->lock);
ead1f3e3 525 if (((master_image->address_attr & address) == address) &&
a17a75e2
MW
526 ((master_image->cycle_attr & cycle) == cycle) &&
527 ((master_image->width_attr & dwidth) == dwidth) &&
528 (master_image->locked == 0)) {
529
530 master_image->locked = 1;
886953e9 531 spin_unlock(&master_image->lock);
a17a75e2
MW
532 allocated_image = master_image;
533 break;
534 }
886953e9 535 spin_unlock(&master_image->lock);
a17a75e2
MW
536 }
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;
886953e9 550 resource->entry = &allocated_image->list;
a17a75e2
MW
551
552 return resource;
553
a17a75e2
MW
554err_alloc:
555 /* Unlock image */
886953e9 556 spin_lock(&master_image->lock);
a17a75e2 557 master_image->locked = 0;
886953e9 558 spin_unlock(&master_image->lock);
a17a75e2
MW
559err_image:
560err_bus:
561 return NULL;
562}
563EXPORT_SYMBOL(vme_master_request);
564
b5bc980a
MW
565/**
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 */
ead1f3e3 582int vme_master_set(struct vme_resource *resource, int enabled,
6af04b06
MW
583 unsigned long long vme_base, unsigned long long size, u32 aspace,
584 u32 cycle, u32 dwidth)
a17a75e2
MW
585{
586 struct vme_bridge *bridge = find_bridge(resource);
587 struct vme_master_resource *image;
588 int retval;
589
590 if (resource->type != VME_MASTER) {
ead1f3e3 591 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
592 return -EINVAL;
593 }
594
595 image = list_entry(resource->entry, struct vme_master_resource, list);
596
597 if (bridge->master_set == NULL) {
ead1f3e3 598 printk(KERN_WARNING "vme_master_set not supported\n");
a17a75e2
MW
599 return -EINVAL;
600 }
601
ead1f3e3 602 if (!(((image->address_attr & aspace) == aspace) &&
a17a75e2
MW
603 ((image->cycle_attr & cycle) == cycle) &&
604 ((image->width_attr & dwidth) == dwidth))) {
ead1f3e3 605 printk(KERN_WARNING "Invalid attributes\n");
a17a75e2
MW
606 return -EINVAL;
607 }
608
609 retval = vme_check_window(aspace, vme_base, size);
ead1f3e3 610 if (retval)
a17a75e2
MW
611 return retval;
612
613 return bridge->master_set(image, enabled, vme_base, size, aspace,
614 cycle, dwidth);
615}
616EXPORT_SYMBOL(vme_master_set);
617
b5bc980a
MW
618/**
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 */
ead1f3e3 633int vme_master_get(struct vme_resource *resource, int *enabled,
6af04b06
MW
634 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
635 u32 *cycle, u32 *dwidth)
a17a75e2
MW
636{
637 struct vme_bridge *bridge = find_bridge(resource);
638 struct vme_master_resource *image;
639
640 if (resource->type != VME_MASTER) {
ead1f3e3 641 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
642 return -EINVAL;
643 }
644
645 image = list_entry(resource->entry, struct vme_master_resource, list);
646
51a569f7 647 if (bridge->master_get == NULL) {
c1038307 648 printk(KERN_WARNING "%s not supported\n", __func__);
a17a75e2
MW
649 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
b5bc980a
MW
657/**
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.
a17a75e2 671 */
ead1f3e3 672ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
a17a75e2
MW
673 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) {
ead1f3e3 680 printk(KERN_WARNING "Reading from resource not supported\n");
a17a75e2
MW
681 return -EINVAL;
682 }
683
684 if (resource->type != VME_MASTER) {
ead1f3e3 685 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
686 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) {
ead1f3e3 694 printk(KERN_WARNING "Invalid Offset\n");
a17a75e2
MW
695 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
b5bc980a
MW
706/**
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.
a17a75e2 720 */
ead1f3e3 721ssize_t vme_master_write(struct vme_resource *resource, void *buf,
a17a75e2
MW
722 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) {
ead1f3e3 729 printk(KERN_WARNING "Writing to resource not supported\n");
a17a75e2
MW
730 return -EINVAL;
731 }
732
733 if (resource->type != VME_MASTER) {
ead1f3e3 734 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
735 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) {
ead1f3e3 743 printk(KERN_WARNING "Invalid Offset\n");
a17a75e2
MW
744 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
b5bc980a
MW
754/**
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.
a17a75e2 772 */
ead1f3e3 773unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
a17a75e2
MW
774 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) {
ead1f3e3 780 printk(KERN_WARNING "Writing to resource not supported\n");
a17a75e2
MW
781 return -EINVAL;
782 }
783
784 if (resource->type != VME_MASTER) {
ead1f3e3 785 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
786 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
b5bc980a
MW
795/**
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 */
c74a804f
DK
806int 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
b5bc980a
MW
832/**
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 */
a17a75e2
MW
838void vme_master_free(struct vme_resource *resource)
839{
840 struct vme_master_resource *master_image;
841
842 if (resource->type != VME_MASTER) {
ead1f3e3 843 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
844 return;
845 }
846
847 master_image = list_entry(resource->entry, struct vme_master_resource,
848 list);
849 if (master_image == NULL) {
ead1f3e3 850 printk(KERN_ERR "Can't find master resource\n");
a17a75e2
MW
851 return;
852 }
853
854 /* Unlock image */
886953e9 855 spin_lock(&master_image->lock);
a17a75e2
MW
856 if (master_image->locked == 0)
857 printk(KERN_ERR "Image is already free\n");
858
859 master_image->locked = 0;
886953e9 860 spin_unlock(&master_image->lock);
a17a75e2
MW
861
862 /* Free up resource memory */
863 kfree(resource);
864}
865EXPORT_SYMBOL(vme_master_free);
866
b5bc980a
MW
867/**
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.
a17a75e2 876 */
6af04b06 877struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
a17a75e2
MW
878{
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
8f966dc4 888 bridge = vdev->bridge;
a17a75e2
MW
889 if (bridge == NULL) {
890 printk(KERN_ERR "Can't find VME bus\n");
891 goto err_bus;
892 }
893
894 /* Loop through DMA resources */
886953e9 895 list_for_each(dma_pos, &bridge->dma_resources) {
a17a75e2
MW
896 dma_ctrlr = list_entry(dma_pos,
897 struct vme_dma_resource, list);
898
899 if (dma_ctrlr == NULL) {
ead1f3e3 900 printk(KERN_ERR "Registered NULL DMA resource\n");
a17a75e2
MW
901 continue;
902 }
903
4f723df4 904 /* Find an unlocked and compatible controller */
886953e9 905 mutex_lock(&dma_ctrlr->mtx);
4f723df4
MW
906 if (((dma_ctrlr->route_attr & route) == route) &&
907 (dma_ctrlr->locked == 0)) {
908
a17a75e2 909 dma_ctrlr->locked = 1;
886953e9 910 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
911 allocated_ctrlr = dma_ctrlr;
912 break;
913 }
886953e9 914 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
915 }
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;
886953e9 927 resource->entry = &allocated_ctrlr->list;
a17a75e2
MW
928
929 return resource;
930
931err_alloc:
932 /* Unlock image */
886953e9 933 mutex_lock(&dma_ctrlr->mtx);
a17a75e2 934 dma_ctrlr->locked = 0;
886953e9 935 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
936err_ctrlr:
937err_bus:
938 return NULL;
939}
58e50798 940EXPORT_SYMBOL(vme_dma_request);
a17a75e2 941
b5bc980a
MW
942/**
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.
a17a75e2
MW
951 */
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) {
ead1f3e3 958 printk(KERN_ERR "Not a DMA resource\n");
a17a75e2
MW
959 return NULL;
960 }
961
962 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
963
ead1f3e3
MW
964 dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
965 if (dma_list == NULL) {
f56c3d4f 966 printk(KERN_ERR "Unable to allocate memory for new DMA list\n");
a17a75e2
MW
967 return NULL;
968 }
886953e9 969 INIT_LIST_HEAD(&dma_list->entries);
a17a75e2 970 dma_list->parent = ctrlr;
886953e9 971 mutex_init(&dma_list->mtx);
a17a75e2
MW
972
973 return dma_list;
974}
975EXPORT_SYMBOL(vme_new_dma_list);
976
b5bc980a
MW
977/**
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.
a17a75e2 987 */
6af04b06 988struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
a17a75e2
MW
989{
990 struct vme_dma_attr *attributes;
991 struct vme_dma_pattern *pattern_attr;
992
ead1f3e3
MW
993 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
994 if (attributes == NULL) {
25958ce3 995 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
a17a75e2
MW
996 goto err_attr;
997 }
998
ead1f3e3
MW
999 pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
1000 if (pattern_attr == NULL) {
25958ce3 1001 printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
a17a75e2
MW
1002 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
a17a75e2
MW
1013err_pat:
1014 kfree(attributes);
1015err_attr:
1016 return NULL;
1017}
1018EXPORT_SYMBOL(vme_dma_pattern_attribute);
1019
b5bc980a
MW
1020/**
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.
a17a75e2
MW
1029 */
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
ead1f3e3
MW
1037 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
1038 if (attributes == NULL) {
25958ce3 1039 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
a17a75e2
MW
1040 goto err_attr;
1041 }
1042
ead1f3e3
MW
1043 pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
1044 if (pci_attr == NULL) {
f56c3d4f 1045 printk(KERN_ERR "Unable to allocate memory for PCI attributes\n");
a17a75e2
MW
1046 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
a17a75e2
MW
1058err_pci:
1059 kfree(attributes);
1060err_attr:
1061 return NULL;
1062}
1063EXPORT_SYMBOL(vme_dma_pci_attribute);
1064
b5bc980a
MW
1065/**
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.
a17a75e2
MW
1077 */
1078struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
6af04b06 1079 u32 aspace, u32 cycle, u32 dwidth)
a17a75e2
MW
1080{
1081 struct vme_dma_attr *attributes;
1082 struct vme_dma_vme *vme_attr;
1083
ead1f3e3 1084 attributes = kmalloc(
a17a75e2 1085 sizeof(struct vme_dma_attr), GFP_KERNEL);
ead1f3e3 1086 if (attributes == NULL) {
25958ce3 1087 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
a17a75e2
MW
1088 goto err_attr;
1089 }
1090
ead1f3e3
MW
1091 vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
1092 if (vme_attr == NULL) {
f56c3d4f 1093 printk(KERN_ERR "Unable to allocate memory for VME attributes\n");
a17a75e2
MW
1094 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
a17a75e2
MW
1107err_vme:
1108 kfree(attributes);
1109err_attr:
1110 return NULL;
1111}
1112EXPORT_SYMBOL(vme_dma_vme_attribute);
1113
b5bc980a
MW
1114/**
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.
a17a75e2
MW
1120 */
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
b5bc980a
MW
1128/**
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 */
a17a75e2
MW
1145int 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) {
ead1f3e3 1152 printk(KERN_WARNING "Link List DMA generation not supported\n");
a17a75e2
MW
1153 return -EINVAL;
1154 }
1155
886953e9 1156 if (!mutex_trylock(&list->mtx)) {
ead1f3e3 1157 printk(KERN_ERR "Link List already submitted\n");
a17a75e2
MW
1158 return -EINVAL;
1159 }
1160
1161 retval = bridge->dma_list_add(list, src, dest, count);
1162
886953e9 1163 mutex_unlock(&list->mtx);
a17a75e2
MW
1164
1165 return retval;
1166}
1167EXPORT_SYMBOL(vme_dma_list_add);
1168
b5bc980a
MW
1169/**
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 */
a17a75e2
MW
1179int 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) {
ead1f3e3 1185 printk(KERN_ERR "Link List DMA execution not supported\n");
a17a75e2
MW
1186 return -EINVAL;
1187 }
1188
886953e9 1189 mutex_lock(&list->mtx);
a17a75e2
MW
1190
1191 retval = bridge->dma_list_exec(list);
1192
886953e9 1193 mutex_unlock(&list->mtx);
a17a75e2
MW
1194
1195 return retval;
1196}
1197EXPORT_SYMBOL(vme_dma_list_exec);
1198
b5bc980a
MW
1199/**
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 */
a17a75e2
MW
1208int 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) {
ead1f3e3 1214 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
a17a75e2
MW
1215 return -EINVAL;
1216 }
1217
886953e9 1218 if (!mutex_trylock(&list->mtx)) {
ead1f3e3 1219 printk(KERN_ERR "Link List in use\n");
a17a75e2
MW
1220 return -EINVAL;
1221 }
1222
1223 /*
f56c3d4f
AS
1224 * 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.
a17a75e2
MW
1226 */
1227 retval = bridge->dma_list_empty(list);
1228 if (retval) {
ead1f3e3 1229 printk(KERN_ERR "Unable to empty link-list entries\n");
886953e9 1230 mutex_unlock(&list->mtx);
a17a75e2
MW
1231 return retval;
1232 }
886953e9 1233 mutex_unlock(&list->mtx);
a17a75e2
MW
1234 kfree(list);
1235
1236 return retval;
1237}
1238EXPORT_SYMBOL(vme_dma_list_free);
1239
b5bc980a
MW
1240/**
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 */
a17a75e2
MW
1249int vme_dma_free(struct vme_resource *resource)
1250{
1251 struct vme_dma_resource *ctrlr;
1252
1253 if (resource->type != VME_DMA) {
ead1f3e3 1254 printk(KERN_ERR "Not a DMA resource\n");
a17a75e2
MW
1255 return -EINVAL;
1256 }
1257
1258 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1259
886953e9 1260 if (!mutex_trylock(&ctrlr->mtx)) {
ead1f3e3 1261 printk(KERN_ERR "Resource busy, can't free\n");
a17a75e2
MW
1262 return -EBUSY;
1263 }
1264
886953e9 1265 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
ead1f3e3 1266 printk(KERN_WARNING "Resource still processing transfers\n");
886953e9 1267 mutex_unlock(&ctrlr->mtx);
a17a75e2
MW
1268 return -EBUSY;
1269 }
1270
1271 ctrlr->locked = 0;
1272
886953e9 1273 mutex_unlock(&ctrlr->mtx);
a17a75e2 1274
fd5c2561
MW
1275 kfree(resource);
1276
a17a75e2
MW
1277 return 0;
1278}
1279EXPORT_SYMBOL(vme_dma_free);
1280
e2c6393f 1281void vme_bus_error_handler(struct vme_bridge *bridge,
472f16f3 1282 unsigned long long address, int am)
e2c6393f 1283{
0b049662
DK
1284 struct list_head *handler_pos = NULL;
1285 struct vme_error_handler *handler;
448535a3 1286 int handler_triggered = 0;
0b049662
DK
1287 u32 aspace = vme_get_aspace(am);
1288
1289 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++;
448535a3 1299 handler_triggered = 1;
0b049662 1300 }
e2c6393f 1301 }
448535a3
DK
1302
1303 if (!handler_triggered)
1304 dev_err(bridge->parent,
1305 "Unhandled VME access error at address 0x%llx\n",
1306 address);
e2c6393f
DK
1307}
1308EXPORT_SYMBOL(vme_bus_error_handler);
1309
0b049662
DK
1310struct vme_error_handler *vme_register_error_handler(
1311 struct vme_bridge *bridge, u32 aspace,
1312 unsigned long long address, size_t len)
e2c6393f 1313{
0b049662 1314 struct vme_error_handler *handler;
e2c6393f 1315
0b049662
DK
1316 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
1317 if (!handler)
1318 return NULL;
e2c6393f 1319
0b049662
DK
1320 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);
e2c6393f 1326
0b049662 1327 return handler;
e2c6393f 1328}
0b049662 1329EXPORT_SYMBOL(vme_register_error_handler);
e2c6393f 1330
0b049662 1331void vme_unregister_error_handler(struct vme_error_handler *handler)
e2c6393f 1332{
0b049662
DK
1333 list_del(&handler->list);
1334 kfree(handler);
e2c6393f 1335}
0b049662 1336EXPORT_SYMBOL(vme_unregister_error_handler);
e2c6393f 1337
c813f592
MW
1338void 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
f56c3d4f 1349 printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
25958ce3 1350 level, statid);
c813f592
MW
1351}
1352EXPORT_SYMBOL(vme_irq_handler);
1353
b5bc980a
MW
1354/**
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 */
8f966dc4 1370int vme_irq_request(struct vme_dev *vdev, int level, int statid,
29848ac9 1371 void (*callback)(int, int, void *),
a17a75e2
MW
1372 void *priv_data)
1373{
1374 struct vme_bridge *bridge;
1375
8f966dc4 1376 bridge = vdev->bridge;
a17a75e2
MW
1377 if (bridge == NULL) {
1378 printk(KERN_ERR "Can't find VME bus\n");
1379 return -EINVAL;
1380 }
1381
ead1f3e3 1382 if ((level < 1) || (level > 7)) {
c813f592 1383 printk(KERN_ERR "Invalid interrupt level\n");
a17a75e2
MW
1384 return -EINVAL;
1385 }
1386
c813f592
MW
1387 if (bridge->irq_set == NULL) {
1388 printk(KERN_ERR "Configuring interrupts not supported\n");
a17a75e2
MW
1389 return -EINVAL;
1390 }
1391
886953e9 1392 mutex_lock(&bridge->irq_mtx);
c813f592
MW
1393
1394 if (bridge->irq[level - 1].callback[statid].func) {
886953e9 1395 mutex_unlock(&bridge->irq_mtx);
c813f592
MW
1396 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 */
29848ac9 1405 bridge->irq_set(bridge, level, 1, 1);
c813f592 1406
886953e9 1407 mutex_unlock(&bridge->irq_mtx);
c813f592
MW
1408
1409 return 0;
a17a75e2 1410}
c813f592 1411EXPORT_SYMBOL(vme_irq_request);
a17a75e2 1412
b5bc980a
MW
1413/**
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 */
8f966dc4 1421void vme_irq_free(struct vme_dev *vdev, int level, int statid)
a17a75e2
MW
1422{
1423 struct vme_bridge *bridge;
1424
8f966dc4 1425 bridge = vdev->bridge;
a17a75e2
MW
1426 if (bridge == NULL) {
1427 printk(KERN_ERR "Can't find VME bus\n");
1428 return;
1429 }
1430
ead1f3e3 1431 if ((level < 1) || (level > 7)) {
c813f592 1432 printk(KERN_ERR "Invalid interrupt level\n");
a17a75e2
MW
1433 return;
1434 }
1435
c813f592
MW
1436 if (bridge->irq_set == NULL) {
1437 printk(KERN_ERR "Configuring interrupts not supported\n");
a17a75e2
MW
1438 return;
1439 }
1440
886953e9 1441 mutex_lock(&bridge->irq_mtx);
c813f592
MW
1442
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)
29848ac9 1447 bridge->irq_set(bridge, level, 0, 1);
c813f592
MW
1448
1449 bridge->irq[level - 1].callback[statid].func = NULL;
1450 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1451
886953e9 1452 mutex_unlock(&bridge->irq_mtx);
a17a75e2 1453}
c813f592 1454EXPORT_SYMBOL(vme_irq_free);
a17a75e2 1455
b5bc980a
MW
1456/**
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 */
8f966dc4 1468int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
a17a75e2
MW
1469{
1470 struct vme_bridge *bridge;
1471
8f966dc4 1472 bridge = vdev->bridge;
a17a75e2
MW
1473 if (bridge == NULL) {
1474 printk(KERN_ERR "Can't find VME bus\n");
1475 return -EINVAL;
1476 }
1477
ead1f3e3 1478 if ((level < 1) || (level > 7)) {
a17a75e2
MW
1479 printk(KERN_WARNING "Invalid interrupt level\n");
1480 return -EINVAL;
1481 }
1482
c813f592 1483 if (bridge->irq_generate == NULL) {
ead1f3e3 1484 printk(KERN_WARNING "Interrupt generation not supported\n");
a17a75e2
MW
1485 return -EINVAL;
1486 }
1487
29848ac9 1488 return bridge->irq_generate(bridge, level, statid);
a17a75e2 1489}
c813f592 1490EXPORT_SYMBOL(vme_irq_generate);
a17a75e2 1491
b5bc980a
MW
1492/**
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.
42fb5031 1501 */
8f966dc4 1502struct vme_resource *vme_lm_request(struct vme_dev *vdev)
a17a75e2
MW
1503{
1504 struct vme_bridge *bridge;
42fb5031
MW
1505 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;
a17a75e2 1509
8f966dc4 1510 bridge = vdev->bridge;
a17a75e2
MW
1511 if (bridge == NULL) {
1512 printk(KERN_ERR "Can't find VME bus\n");
42fb5031
MW
1513 goto err_bus;
1514 }
1515
b5bc980a 1516 /* Loop through LM resources */
886953e9 1517 list_for_each(lm_pos, &bridge->lm_resources) {
42fb5031
MW
1518 lm = list_entry(lm_pos,
1519 struct vme_lm_resource, list);
1520
1521 if (lm == NULL) {
25958ce3 1522 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
42fb5031
MW
1523 continue;
1524 }
1525
1526 /* Find an unlocked controller */
886953e9 1527 mutex_lock(&lm->mtx);
42fb5031
MW
1528 if (lm->locked == 0) {
1529 lm->locked = 1;
886953e9 1530 mutex_unlock(&lm->mtx);
42fb5031
MW
1531 allocated_lm = lm;
1532 break;
1533 }
886953e9 1534 mutex_unlock(&lm->mtx);
42fb5031
MW
1535 }
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;
886953e9 1547 resource->entry = &allocated_lm->list;
42fb5031
MW
1548
1549 return resource;
1550
1551err_alloc:
1552 /* Unlock image */
886953e9 1553 mutex_lock(&lm->mtx);
42fb5031 1554 lm->locked = 0;
886953e9 1555 mutex_unlock(&lm->mtx);
42fb5031
MW
1556err_lm:
1557err_bus:
1558 return NULL;
1559}
1560EXPORT_SYMBOL(vme_lm_request);
1561
b5bc980a
MW
1562/**
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 */
42fb5031
MW
1573int 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");
1579 return -EINVAL;
1580 }
1581
1582 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1583
1584 return lm->monitors;
1585}
1586EXPORT_SYMBOL(vme_lm_count);
1587
b5bc980a
MW
1588/**
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 */
42fb5031 1602int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
6af04b06 1603 u32 aspace, u32 cycle)
42fb5031
MW
1604{
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");
a17a75e2
MW
1610 return -EINVAL;
1611 }
1612
42fb5031
MW
1613 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1614
a17a75e2 1615 if (bridge->lm_set == NULL) {
42fb5031 1616 printk(KERN_ERR "vme_lm_set not supported\n");
a17a75e2
MW
1617 return -EINVAL;
1618 }
1619
8be9226c 1620 return bridge->lm_set(lm, lm_base, aspace, cycle);
a17a75e2
MW
1621}
1622EXPORT_SYMBOL(vme_lm_set);
1623
b5bc980a
MW
1624/**
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 */
42fb5031 1638int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
6af04b06 1639 u32 *aspace, u32 *cycle)
a17a75e2 1640{
42fb5031
MW
1641 struct vme_bridge *bridge = find_bridge(resource);
1642 struct vme_lm_resource *lm;
a17a75e2 1643
42fb5031
MW
1644 if (resource->type != VME_LM) {
1645 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1646 return -EINVAL;
1647 }
1648
42fb5031
MW
1649 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1650
a17a75e2 1651 if (bridge->lm_get == NULL) {
42fb5031 1652 printk(KERN_ERR "vme_lm_get not supported\n");
a17a75e2
MW
1653 return -EINVAL;
1654 }
1655
42fb5031 1656 return bridge->lm_get(lm, lm_base, aspace, cycle);
a17a75e2
MW
1657}
1658EXPORT_SYMBOL(vme_lm_get);
1659
b5bc980a
MW
1660/**
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 */
42fb5031 1675int vme_lm_attach(struct vme_resource *resource, int monitor,
fa54b326 1676 void (*callback)(void *), void *data)
a17a75e2 1677{
42fb5031
MW
1678 struct vme_bridge *bridge = find_bridge(resource);
1679 struct vme_lm_resource *lm;
a17a75e2 1680
42fb5031
MW
1681 if (resource->type != VME_LM) {
1682 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1683 return -EINVAL;
1684 }
1685
42fb5031
MW
1686 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1687
a17a75e2 1688 if (bridge->lm_attach == NULL) {
42fb5031 1689 printk(KERN_ERR "vme_lm_attach not supported\n");
a17a75e2
MW
1690 return -EINVAL;
1691 }
1692
fa54b326 1693 return bridge->lm_attach(lm, monitor, callback, data);
a17a75e2
MW
1694}
1695EXPORT_SYMBOL(vme_lm_attach);
1696
b5bc980a
MW
1697/**
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 */
42fb5031 1709int vme_lm_detach(struct vme_resource *resource, int monitor)
a17a75e2 1710{
42fb5031
MW
1711 struct vme_bridge *bridge = find_bridge(resource);
1712 struct vme_lm_resource *lm;
a17a75e2 1713
42fb5031
MW
1714 if (resource->type != VME_LM) {
1715 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1716 return -EINVAL;
1717 }
1718
42fb5031
MW
1719 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1720
a17a75e2 1721 if (bridge->lm_detach == NULL) {
42fb5031 1722 printk(KERN_ERR "vme_lm_detach not supported\n");
a17a75e2
MW
1723 return -EINVAL;
1724 }
1725
42fb5031 1726 return bridge->lm_detach(lm, monitor);
a17a75e2
MW
1727}
1728EXPORT_SYMBOL(vme_lm_detach);
1729
b5bc980a
MW
1730/**
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 */
42fb5031
MW
1742void 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
886953e9 1753 mutex_lock(&lm->mtx);
42fb5031 1754
8be9226c
MW
1755 /* XXX
1756 * Check to see that there aren't any callbacks still attached, if
1757 * there are we should probably be detaching them!
1758 */
42fb5031
MW
1759
1760 lm->locked = 0;
1761
886953e9 1762 mutex_unlock(&lm->mtx);
8be9226c
MW
1763
1764 kfree(resource);
42fb5031
MW
1765}
1766EXPORT_SYMBOL(vme_lm_free);
1767
b5bc980a
MW
1768/**
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 */
d7729f0f 1778int vme_slot_num(struct vme_dev *vdev)
a17a75e2
MW
1779{
1780 struct vme_bridge *bridge;
1781
8f966dc4 1782 bridge = vdev->bridge;
a17a75e2
MW
1783 if (bridge == NULL) {
1784 printk(KERN_ERR "Can't find VME bus\n");
1785 return -EINVAL;
1786 }
1787
1788 if (bridge->slot_get == NULL) {
d7729f0f 1789 printk(KERN_WARNING "vme_slot_num not supported\n");
a17a75e2
MW
1790 return -EINVAL;
1791 }
1792
29848ac9 1793 return bridge->slot_get(bridge);
a17a75e2 1794}
d7729f0f 1795EXPORT_SYMBOL(vme_slot_num);
a17a75e2 1796
b5bc980a
MW
1797/**
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 */
978f47d6
MW
1806int 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);
a17a75e2
MW
1819
1820/* - Bridge Registration --------------------------------------------------- */
1821
5b93c2a2
MV
1822static void vme_dev_release(struct device *dev)
1823{
1824 kfree(dev_to_vme_dev(dev));
1825}
1826
326071b3
AS
1827/* 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
5b93c2a2 1841int vme_register_bridge(struct vme_bridge *bridge)
a17a75e2
MW
1842{
1843 int i;
733e3ef0 1844 int ret = -1;
a17a75e2 1845
733e3ef0 1846 mutex_lock(&vme_buses_lock);
a17a75e2 1847 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
733e3ef0
MV
1848 if ((vme_bus_numbers & (1 << i)) == 0) {
1849 vme_bus_numbers |= (1 << i);
1850 bridge->num = i;
5d6abf37 1851 INIT_LIST_HEAD(&bridge->devices);
733e3ef0
MV
1852 list_add_tail(&bridge->bus_list, &vme_bus_list);
1853 ret = 0;
a17a75e2
MW
1854 break;
1855 }
1856 }
733e3ef0 1857 mutex_unlock(&vme_buses_lock);
a17a75e2 1858
733e3ef0 1859 return ret;
a17a75e2 1860}
5b93c2a2 1861EXPORT_SYMBOL(vme_register_bridge);
a17a75e2 1862
5b93c2a2 1863void vme_unregister_bridge(struct vme_bridge *bridge)
a17a75e2 1864{
5d6abf37
MV
1865 struct vme_dev *vdev;
1866 struct vme_dev *tmp;
1867
733e3ef0
MV
1868 mutex_lock(&vme_buses_lock);
1869 vme_bus_numbers &= ~(1 << bridge->num);
5d6abf37
MV
1870 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 }
733e3ef0
MV
1875 list_del(&bridge->bus_list);
1876 mutex_unlock(&vme_buses_lock);
a17a75e2 1877}
5d6abf37 1878EXPORT_SYMBOL(vme_unregister_bridge);
a17a75e2 1879
5d6abf37
MV
1880/* - Driver Registration --------------------------------------------------- */
1881
1882static int __vme_register_driver_bus(struct vme_driver *drv,
1883 struct vme_bridge *bridge, unsigned int ndevs)
1884{
1885 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;
f6c39d4f
MV
1894 goto err_devalloc;
1895 }
a916a391 1896 vdev->num = i;
8f966dc4 1897 vdev->bridge = bridge;
5d6abf37
MV
1898 vdev->dev.platform_data = drv;
1899 vdev->dev.release = vme_dev_release;
8f966dc4
MV
1900 vdev->dev.parent = bridge->parent;
1901 vdev->dev.bus = &vme_bus_type;
a916a391
MV
1902 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1903 vdev->num);
a17a75e2 1904
5d6abf37
MV
1905 err = device_register(&vdev->dev);
1906 if (err)
a17a75e2 1907 goto err_reg;
a17a75e2 1908
5d6abf37
MV
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;
a17a75e2 1916
a17a75e2 1917err_reg:
def1820d 1918 put_device(&vdev->dev);
8f966dc4 1919 kfree(vdev);
f6c39d4f 1920err_devalloc:
5d6abf37
MV
1921 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1922 list_del(&vdev->drv_list);
1923 list_del(&vdev->bridge_list);
8f966dc4 1924 device_unregister(&vdev->dev);
a17a75e2 1925 }
5d6abf37 1926 return err;
a17a75e2 1927}
a17a75e2 1928
5d6abf37 1929static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
a17a75e2 1930{
5d6abf37
MV
1931 struct vme_bridge *bridge;
1932 int err = 0;
a17a75e2 1933
5d6abf37
MV
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
c26f6112 1941 * will have to wait).
5d6abf37
MV
1942 */
1943 err = __vme_register_driver_bus(drv, bridge, ndevs);
1944 if (err)
1945 break;
a17a75e2 1946 }
5d6abf37
MV
1947 mutex_unlock(&vme_buses_lock);
1948 return err;
a17a75e2 1949}
a17a75e2 1950
b5bc980a
MW
1951/**
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 */
5d6abf37 1960int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
a17a75e2 1961{
5d6abf37
MV
1962 int err;
1963
a17a75e2
MW
1964 drv->driver.name = drv->name;
1965 drv->driver.bus = &vme_bus_type;
5d6abf37
MV
1966 INIT_LIST_HEAD(&drv->devices);
1967
1968 err = driver_register(&drv->driver);
1969 if (err)
1970 return err;
a17a75e2 1971
5d6abf37
MV
1972 err = __vme_register_driver(drv, ndevs);
1973 if (err)
1974 driver_unregister(&drv->driver);
1975
1976 return err;
a17a75e2
MW
1977}
1978EXPORT_SYMBOL(vme_register_driver);
1979
b5bc980a
MW
1980/**
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 */
ead1f3e3 1986void vme_unregister_driver(struct vme_driver *drv)
a17a75e2 1987{
5d6abf37
MV
1988 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
a17a75e2
MW
1998 driver_unregister(&drv->driver);
1999}
2000EXPORT_SYMBOL(vme_unregister_driver);
2001
2002/* - Bus Registration ------------------------------------------------------ */
2003
a17a75e2
MW
2004static int vme_bus_match(struct device *dev, struct device_driver *drv)
2005{
5d6abf37 2006 struct vme_driver *vme_drv;
a17a75e2 2007
5d6abf37 2008 vme_drv = container_of(drv, struct vme_driver, driver);
a17a75e2 2009
5d6abf37
MV
2010 if (dev->platform_data == vme_drv) {
2011 struct vme_dev *vdev = dev_to_vme_dev(dev);
a17a75e2 2012
5d6abf37
MV
2013 if (vme_drv->match && vme_drv->match(vdev))
2014 return 1;
a37b0dad 2015
5d6abf37 2016 dev->platform_data = NULL;
a17a75e2 2017 }
a17a75e2
MW
2018 return 0;
2019}
2020
2021static int vme_bus_probe(struct device *dev)
2022{
a17a75e2 2023 int retval = -ENODEV;
5d6abf37
MV
2024 struct vme_driver *driver;
2025 struct vme_dev *vdev = dev_to_vme_dev(dev);
a17a75e2 2026
5d6abf37 2027 driver = dev->platform_data;
a17a75e2 2028
ead1f3e3 2029 if (driver->probe != NULL)
8f966dc4 2030 retval = driver->probe(vdev);
a17a75e2
MW
2031
2032 return retval;
2033}
2034
9797484b
SB
2035static 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
a17a75e2
MW
2049struct bus_type vme_bus_type = {
2050 .name = "vme",
2051 .match = vme_bus_match,
2052 .probe = vme_bus_probe,
9797484b 2053 .remove = vme_bus_remove,
a17a75e2
MW
2054};
2055EXPORT_SYMBOL(vme_bus_type);
2056
ead1f3e3 2057static int __init vme_init(void)
a17a75e2
MW
2058{
2059 return bus_register(&vme_bus_type);
2060}
c326cc02 2061subsys_initcall(vme_init);