]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/vme/vme.c
vme: include address space in error filtering
[mirror_ubuntu-hirsute-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
a17a75e2
MW
16#include <linux/module.h>
17#include <linux/moduleparam.h>
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
MW
42static void __exit vme_exit(void);
43static int __init vme_init(void);
a17a75e2 44
8f966dc4 45static struct vme_dev *dev_to_vme_dev(struct device *dev)
a17a75e2 46{
8f966dc4 47 return container_of(dev, struct vme_dev, dev);
a17a75e2
MW
48}
49
50/*
51 * Find the bridge that the resource is associated with.
52 */
53static struct vme_bridge *find_bridge(struct vme_resource *resource)
54{
55 /* Get list to search */
56 switch (resource->type) {
57 case VME_MASTER:
58 return list_entry(resource->entry, struct vme_master_resource,
59 list)->parent;
60 break;
61 case VME_SLAVE:
62 return list_entry(resource->entry, struct vme_slave_resource,
63 list)->parent;
64 break;
65 case VME_DMA:
66 return list_entry(resource->entry, struct vme_dma_resource,
67 list)->parent;
68 break;
42fb5031
MW
69 case VME_LM:
70 return list_entry(resource->entry, struct vme_lm_resource,
71 list)->parent;
72 break;
a17a75e2
MW
73 default:
74 printk(KERN_ERR "Unknown resource type\n");
75 return NULL;
76 break;
77 }
78}
79
80/*
81 * Allocate a contiguous block of memory for use by the driver. This is used to
82 * create the buffers for the slave windows.
a17a75e2 83 */
ead1f3e3 84void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
a17a75e2
MW
85 dma_addr_t *dma)
86{
87 struct vme_bridge *bridge;
a17a75e2 88
ead1f3e3
MW
89 if (resource == NULL) {
90 printk(KERN_ERR "No resource\n");
a17a75e2
MW
91 return NULL;
92 }
93
94 bridge = find_bridge(resource);
ead1f3e3
MW
95 if (bridge == NULL) {
96 printk(KERN_ERR "Can't find bridge\n");
a17a75e2
MW
97 return NULL;
98 }
99
a17a75e2 100 if (bridge->parent == NULL) {
25958ce3 101 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
7f58f025
MV
102 return NULL;
103 }
104
105 if (bridge->alloc_consistent == NULL) {
25958ce3
GKH
106 printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
107 bridge->name);
a17a75e2
MW
108 return NULL;
109 }
a17a75e2 110
7f58f025 111 return bridge->alloc_consistent(bridge->parent, size, dma);
a17a75e2
MW
112}
113EXPORT_SYMBOL(vme_alloc_consistent);
114
115/*
116 * Free previously allocated contiguous block of memory.
a17a75e2
MW
117 */
118void vme_free_consistent(struct vme_resource *resource, size_t size,
119 void *vaddr, dma_addr_t dma)
120{
121 struct vme_bridge *bridge;
a17a75e2 122
ead1f3e3
MW
123 if (resource == NULL) {
124 printk(KERN_ERR "No resource\n");
a17a75e2
MW
125 return;
126 }
127
128 bridge = find_bridge(resource);
ead1f3e3
MW
129 if (bridge == NULL) {
130 printk(KERN_ERR "Can't find bridge\n");
a17a75e2
MW
131 return;
132 }
133
7f58f025 134 if (bridge->parent == NULL) {
25958ce3 135 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
7f58f025
MV
136 return;
137 }
138
139 if (bridge->free_consistent == NULL) {
25958ce3
GKH
140 printk(KERN_ERR "free_consistent not supported by bridge %s\n",
141 bridge->name);
7f58f025
MV
142 return;
143 }
a17a75e2 144
7f58f025 145 bridge->free_consistent(bridge->parent, size, vaddr, dma);
a17a75e2
MW
146}
147EXPORT_SYMBOL(vme_free_consistent);
148
149size_t vme_get_size(struct vme_resource *resource)
150{
151 int enabled, retval;
152 unsigned long long base, size;
153 dma_addr_t buf_base;
6af04b06 154 u32 aspace, cycle, dwidth;
a17a75e2
MW
155
156 switch (resource->type) {
157 case VME_MASTER:
158 retval = vme_master_get(resource, &enabled, &base, &size,
159 &aspace, &cycle, &dwidth);
160
161 return size;
162 break;
163 case VME_SLAVE:
164 retval = vme_slave_get(resource, &enabled, &base, &size,
165 &buf_base, &aspace, &cycle);
166
167 return size;
168 break;
169 case VME_DMA:
170 return 0;
171 break;
172 default:
173 printk(KERN_ERR "Unknown resource type\n");
174 return 0;
175 break;
176 }
177}
178EXPORT_SYMBOL(vme_get_size);
179
ef73f886
DK
180int vme_check_window(u32 aspace, unsigned long long vme_base,
181 unsigned long long size)
a17a75e2
MW
182{
183 int retval = 0;
184
185 switch (aspace) {
186 case VME_A16:
187 if (((vme_base + size) > VME_A16_MAX) ||
188 (vme_base > VME_A16_MAX))
189 retval = -EFAULT;
190 break;
191 case VME_A24:
192 if (((vme_base + size) > VME_A24_MAX) ||
193 (vme_base > VME_A24_MAX))
194 retval = -EFAULT;
195 break;
196 case VME_A32:
197 if (((vme_base + size) > VME_A32_MAX) ||
198 (vme_base > VME_A32_MAX))
199 retval = -EFAULT;
200 break;
201 case VME_A64:
e7fd80cb
DK
202 if ((size != 0) && (vme_base > U64_MAX + 1 - size))
203 retval = -EFAULT;
a17a75e2
MW
204 break;
205 case VME_CRCSR:
206 if (((vme_base + size) > VME_CRCSR_MAX) ||
207 (vme_base > VME_CRCSR_MAX))
208 retval = -EFAULT;
209 break;
210 case VME_USER1:
211 case VME_USER2:
212 case VME_USER3:
213 case VME_USER4:
214 /* User Defined */
215 break;
216 default:
ead1f3e3 217 printk(KERN_ERR "Invalid address space\n");
a17a75e2
MW
218 retval = -EINVAL;
219 break;
220 }
221
222 return retval;
223}
ef73f886 224EXPORT_SYMBOL(vme_check_window);
a17a75e2 225
472f16f3
DK
226static u32 vme_get_aspace(int am)
227{
228 switch (am) {
229 case 0x29:
230 case 0x2D:
231 return VME_A16;
232 case 0x38:
233 case 0x39:
234 case 0x3A:
235 case 0x3B:
236 case 0x3C:
237 case 0x3D:
238 case 0x3E:
239 case 0x3F:
240 return VME_A24;
241 case 0x8:
242 case 0x9:
243 case 0xA:
244 case 0xB:
245 case 0xC:
246 case 0xD:
247 case 0xE:
248 case 0xF:
249 return VME_A32;
250 case 0x0:
251 case 0x1:
252 case 0x3:
253 return VME_A64;
254 }
255
256 return 0;
257}
258
a17a75e2
MW
259/*
260 * Request a slave image with specific attributes, return some unique
261 * identifier.
262 */
6af04b06
MW
263struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
264 u32 cycle)
a17a75e2
MW
265{
266 struct vme_bridge *bridge;
267 struct list_head *slave_pos = NULL;
268 struct vme_slave_resource *allocated_image = NULL;
269 struct vme_slave_resource *slave_image = NULL;
270 struct vme_resource *resource = NULL;
271
8f966dc4 272 bridge = vdev->bridge;
a17a75e2
MW
273 if (bridge == NULL) {
274 printk(KERN_ERR "Can't find VME bus\n");
275 goto err_bus;
276 }
277
278 /* Loop through slave resources */
886953e9 279 list_for_each(slave_pos, &bridge->slave_resources) {
a17a75e2
MW
280 slave_image = list_entry(slave_pos,
281 struct vme_slave_resource, list);
282
283 if (slave_image == NULL) {
ead1f3e3 284 printk(KERN_ERR "Registered NULL Slave resource\n");
a17a75e2
MW
285 continue;
286 }
287
288 /* Find an unlocked and compatible image */
886953e9 289 mutex_lock(&slave_image->mtx);
ead1f3e3 290 if (((slave_image->address_attr & address) == address) &&
a17a75e2
MW
291 ((slave_image->cycle_attr & cycle) == cycle) &&
292 (slave_image->locked == 0)) {
293
294 slave_image->locked = 1;
886953e9 295 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
296 allocated_image = slave_image;
297 break;
298 }
886953e9 299 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
300 }
301
302 /* No free image */
303 if (allocated_image == NULL)
304 goto err_image;
305
306 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
307 if (resource == NULL) {
308 printk(KERN_WARNING "Unable to allocate resource structure\n");
309 goto err_alloc;
310 }
311 resource->type = VME_SLAVE;
886953e9 312 resource->entry = &allocated_image->list;
a17a75e2
MW
313
314 return resource;
315
316err_alloc:
317 /* Unlock image */
886953e9 318 mutex_lock(&slave_image->mtx);
a17a75e2 319 slave_image->locked = 0;
886953e9 320 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
321err_image:
322err_bus:
323 return NULL;
324}
325EXPORT_SYMBOL(vme_slave_request);
326
ead1f3e3 327int vme_slave_set(struct vme_resource *resource, int enabled,
a17a75e2 328 unsigned long long vme_base, unsigned long long size,
6af04b06 329 dma_addr_t buf_base, u32 aspace, u32 cycle)
a17a75e2
MW
330{
331 struct vme_bridge *bridge = find_bridge(resource);
332 struct vme_slave_resource *image;
333 int retval;
334
335 if (resource->type != VME_SLAVE) {
ead1f3e3 336 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
337 return -EINVAL;
338 }
339
340 image = list_entry(resource->entry, struct vme_slave_resource, list);
341
342 if (bridge->slave_set == NULL) {
ead1f3e3 343 printk(KERN_ERR "Function not supported\n");
a17a75e2
MW
344 return -ENOSYS;
345 }
346
ead1f3e3 347 if (!(((image->address_attr & aspace) == aspace) &&
a17a75e2 348 ((image->cycle_attr & cycle) == cycle))) {
ead1f3e3 349 printk(KERN_ERR "Invalid attributes\n");
a17a75e2
MW
350 return -EINVAL;
351 }
352
353 retval = vme_check_window(aspace, vme_base, size);
ead1f3e3 354 if (retval)
a17a75e2
MW
355 return retval;
356
357 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
358 aspace, cycle);
359}
360EXPORT_SYMBOL(vme_slave_set);
361
ead1f3e3 362int vme_slave_get(struct vme_resource *resource, int *enabled,
a17a75e2 363 unsigned long long *vme_base, unsigned long long *size,
6af04b06 364 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
a17a75e2
MW
365{
366 struct vme_bridge *bridge = find_bridge(resource);
367 struct vme_slave_resource *image;
368
369 if (resource->type != VME_SLAVE) {
ead1f3e3 370 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
371 return -EINVAL;
372 }
373
374 image = list_entry(resource->entry, struct vme_slave_resource, list);
375
51a569f7 376 if (bridge->slave_get == NULL) {
ead1f3e3 377 printk(KERN_ERR "vme_slave_get not supported\n");
a17a75e2
MW
378 return -EINVAL;
379 }
380
381 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
382 aspace, cycle);
383}
384EXPORT_SYMBOL(vme_slave_get);
385
386void vme_slave_free(struct vme_resource *resource)
387{
388 struct vme_slave_resource *slave_image;
389
390 if (resource->type != VME_SLAVE) {
ead1f3e3 391 printk(KERN_ERR "Not a slave resource\n");
a17a75e2
MW
392 return;
393 }
394
395 slave_image = list_entry(resource->entry, struct vme_slave_resource,
396 list);
397 if (slave_image == NULL) {
ead1f3e3 398 printk(KERN_ERR "Can't find slave resource\n");
a17a75e2
MW
399 return;
400 }
401
402 /* Unlock image */
886953e9 403 mutex_lock(&slave_image->mtx);
a17a75e2
MW
404 if (slave_image->locked == 0)
405 printk(KERN_ERR "Image is already free\n");
406
407 slave_image->locked = 0;
886953e9 408 mutex_unlock(&slave_image->mtx);
a17a75e2
MW
409
410 /* Free up resource memory */
411 kfree(resource);
412}
413EXPORT_SYMBOL(vme_slave_free);
414
415/*
416 * Request a master image with specific attributes, return some unique
417 * identifier.
418 */
6af04b06
MW
419struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
420 u32 cycle, u32 dwidth)
a17a75e2
MW
421{
422 struct vme_bridge *bridge;
423 struct list_head *master_pos = NULL;
424 struct vme_master_resource *allocated_image = NULL;
425 struct vme_master_resource *master_image = NULL;
426 struct vme_resource *resource = NULL;
427
8f966dc4 428 bridge = vdev->bridge;
a17a75e2
MW
429 if (bridge == NULL) {
430 printk(KERN_ERR "Can't find VME bus\n");
431 goto err_bus;
432 }
433
434 /* Loop through master resources */
886953e9 435 list_for_each(master_pos, &bridge->master_resources) {
a17a75e2
MW
436 master_image = list_entry(master_pos,
437 struct vme_master_resource, list);
438
439 if (master_image == NULL) {
440 printk(KERN_WARNING "Registered NULL master resource\n");
441 continue;
442 }
443
444 /* Find an unlocked and compatible image */
886953e9 445 spin_lock(&master_image->lock);
ead1f3e3 446 if (((master_image->address_attr & address) == address) &&
a17a75e2
MW
447 ((master_image->cycle_attr & cycle) == cycle) &&
448 ((master_image->width_attr & dwidth) == dwidth) &&
449 (master_image->locked == 0)) {
450
451 master_image->locked = 1;
886953e9 452 spin_unlock(&master_image->lock);
a17a75e2
MW
453 allocated_image = master_image;
454 break;
455 }
886953e9 456 spin_unlock(&master_image->lock);
a17a75e2
MW
457 }
458
459 /* Check to see if we found a resource */
460 if (allocated_image == NULL) {
461 printk(KERN_ERR "Can't find a suitable resource\n");
462 goto err_image;
463 }
464
465 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
466 if (resource == NULL) {
467 printk(KERN_ERR "Unable to allocate resource structure\n");
468 goto err_alloc;
469 }
470 resource->type = VME_MASTER;
886953e9 471 resource->entry = &allocated_image->list;
a17a75e2
MW
472
473 return resource;
474
a17a75e2
MW
475err_alloc:
476 /* Unlock image */
886953e9 477 spin_lock(&master_image->lock);
a17a75e2 478 master_image->locked = 0;
886953e9 479 spin_unlock(&master_image->lock);
a17a75e2
MW
480err_image:
481err_bus:
482 return NULL;
483}
484EXPORT_SYMBOL(vme_master_request);
485
ead1f3e3 486int vme_master_set(struct vme_resource *resource, int enabled,
6af04b06
MW
487 unsigned long long vme_base, unsigned long long size, u32 aspace,
488 u32 cycle, u32 dwidth)
a17a75e2
MW
489{
490 struct vme_bridge *bridge = find_bridge(resource);
491 struct vme_master_resource *image;
492 int retval;
493
494 if (resource->type != VME_MASTER) {
ead1f3e3 495 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
496 return -EINVAL;
497 }
498
499 image = list_entry(resource->entry, struct vme_master_resource, list);
500
501 if (bridge->master_set == NULL) {
ead1f3e3 502 printk(KERN_WARNING "vme_master_set not supported\n");
a17a75e2
MW
503 return -EINVAL;
504 }
505
ead1f3e3 506 if (!(((image->address_attr & aspace) == aspace) &&
a17a75e2
MW
507 ((image->cycle_attr & cycle) == cycle) &&
508 ((image->width_attr & dwidth) == dwidth))) {
ead1f3e3 509 printk(KERN_WARNING "Invalid attributes\n");
a17a75e2
MW
510 return -EINVAL;
511 }
512
513 retval = vme_check_window(aspace, vme_base, size);
ead1f3e3 514 if (retval)
a17a75e2
MW
515 return retval;
516
517 return bridge->master_set(image, enabled, vme_base, size, aspace,
518 cycle, dwidth);
519}
520EXPORT_SYMBOL(vme_master_set);
521
ead1f3e3 522int vme_master_get(struct vme_resource *resource, int *enabled,
6af04b06
MW
523 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
524 u32 *cycle, u32 *dwidth)
a17a75e2
MW
525{
526 struct vme_bridge *bridge = find_bridge(resource);
527 struct vme_master_resource *image;
528
529 if (resource->type != VME_MASTER) {
ead1f3e3 530 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
531 return -EINVAL;
532 }
533
534 image = list_entry(resource->entry, struct vme_master_resource, list);
535
51a569f7 536 if (bridge->master_get == NULL) {
c1038307 537 printk(KERN_WARNING "%s not supported\n", __func__);
a17a75e2
MW
538 return -EINVAL;
539 }
540
541 return bridge->master_get(image, enabled, vme_base, size, aspace,
542 cycle, dwidth);
543}
544EXPORT_SYMBOL(vme_master_get);
545
546/*
547 * Read data out of VME space into a buffer.
548 */
ead1f3e3 549ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
a17a75e2
MW
550 loff_t offset)
551{
552 struct vme_bridge *bridge = find_bridge(resource);
553 struct vme_master_resource *image;
554 size_t length;
555
556 if (bridge->master_read == NULL) {
ead1f3e3 557 printk(KERN_WARNING "Reading from resource not supported\n");
a17a75e2
MW
558 return -EINVAL;
559 }
560
561 if (resource->type != VME_MASTER) {
ead1f3e3 562 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
563 return -EINVAL;
564 }
565
566 image = list_entry(resource->entry, struct vme_master_resource, list);
567
568 length = vme_get_size(resource);
569
570 if (offset > length) {
ead1f3e3 571 printk(KERN_WARNING "Invalid Offset\n");
a17a75e2
MW
572 return -EFAULT;
573 }
574
575 if ((offset + count) > length)
576 count = length - offset;
577
578 return bridge->master_read(image, buf, count, offset);
579
580}
581EXPORT_SYMBOL(vme_master_read);
582
583/*
584 * Write data out to VME space from a buffer.
585 */
ead1f3e3 586ssize_t vme_master_write(struct vme_resource *resource, void *buf,
a17a75e2
MW
587 size_t count, loff_t offset)
588{
589 struct vme_bridge *bridge = find_bridge(resource);
590 struct vme_master_resource *image;
591 size_t length;
592
593 if (bridge->master_write == NULL) {
ead1f3e3 594 printk(KERN_WARNING "Writing to resource not supported\n");
a17a75e2
MW
595 return -EINVAL;
596 }
597
598 if (resource->type != VME_MASTER) {
ead1f3e3 599 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
600 return -EINVAL;
601 }
602
603 image = list_entry(resource->entry, struct vme_master_resource, list);
604
605 length = vme_get_size(resource);
606
607 if (offset > length) {
ead1f3e3 608 printk(KERN_WARNING "Invalid Offset\n");
a17a75e2
MW
609 return -EFAULT;
610 }
611
612 if ((offset + count) > length)
613 count = length - offset;
614
615 return bridge->master_write(image, buf, count, offset);
616}
617EXPORT_SYMBOL(vme_master_write);
618
619/*
620 * Perform RMW cycle to provided location.
621 */
ead1f3e3 622unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
a17a75e2
MW
623 unsigned int compare, unsigned int swap, loff_t offset)
624{
625 struct vme_bridge *bridge = find_bridge(resource);
626 struct vme_master_resource *image;
627
628 if (bridge->master_rmw == NULL) {
ead1f3e3 629 printk(KERN_WARNING "Writing to resource not supported\n");
a17a75e2
MW
630 return -EINVAL;
631 }
632
633 if (resource->type != VME_MASTER) {
ead1f3e3 634 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
635 return -EINVAL;
636 }
637
638 image = list_entry(resource->entry, struct vme_master_resource, list);
639
640 return bridge->master_rmw(image, mask, compare, swap, offset);
641}
642EXPORT_SYMBOL(vme_master_rmw);
643
c74a804f
DK
644int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
645{
646 struct vme_master_resource *image;
647 phys_addr_t phys_addr;
648 unsigned long vma_size;
649
650 if (resource->type != VME_MASTER) {
651 pr_err("Not a master resource\n");
652 return -EINVAL;
653 }
654
655 image = list_entry(resource->entry, struct vme_master_resource, list);
656 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
657 vma_size = vma->vm_end - vma->vm_start;
658
659 if (phys_addr + vma_size > image->bus_resource.end + 1) {
660 pr_err("Map size cannot exceed the window size\n");
661 return -EFAULT;
662 }
663
664 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
665
666 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
667}
668EXPORT_SYMBOL(vme_master_mmap);
669
a17a75e2
MW
670void vme_master_free(struct vme_resource *resource)
671{
672 struct vme_master_resource *master_image;
673
674 if (resource->type != VME_MASTER) {
ead1f3e3 675 printk(KERN_ERR "Not a master resource\n");
a17a75e2
MW
676 return;
677 }
678
679 master_image = list_entry(resource->entry, struct vme_master_resource,
680 list);
681 if (master_image == NULL) {
ead1f3e3 682 printk(KERN_ERR "Can't find master resource\n");
a17a75e2
MW
683 return;
684 }
685
686 /* Unlock image */
886953e9 687 spin_lock(&master_image->lock);
a17a75e2
MW
688 if (master_image->locked == 0)
689 printk(KERN_ERR "Image is already free\n");
690
691 master_image->locked = 0;
886953e9 692 spin_unlock(&master_image->lock);
a17a75e2
MW
693
694 /* Free up resource memory */
695 kfree(resource);
696}
697EXPORT_SYMBOL(vme_master_free);
698
699/*
700 * Request a DMA controller with specific attributes, return some unique
701 * identifier.
702 */
6af04b06 703struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
a17a75e2
MW
704{
705 struct vme_bridge *bridge;
706 struct list_head *dma_pos = NULL;
707 struct vme_dma_resource *allocated_ctrlr = NULL;
708 struct vme_dma_resource *dma_ctrlr = NULL;
709 struct vme_resource *resource = NULL;
710
711 /* XXX Not checking resource attributes */
712 printk(KERN_ERR "No VME resource Attribute tests done\n");
713
8f966dc4 714 bridge = vdev->bridge;
a17a75e2
MW
715 if (bridge == NULL) {
716 printk(KERN_ERR "Can't find VME bus\n");
717 goto err_bus;
718 }
719
720 /* Loop through DMA resources */
886953e9 721 list_for_each(dma_pos, &bridge->dma_resources) {
a17a75e2
MW
722 dma_ctrlr = list_entry(dma_pos,
723 struct vme_dma_resource, list);
724
725 if (dma_ctrlr == NULL) {
ead1f3e3 726 printk(KERN_ERR "Registered NULL DMA resource\n");
a17a75e2
MW
727 continue;
728 }
729
4f723df4 730 /* Find an unlocked and compatible controller */
886953e9 731 mutex_lock(&dma_ctrlr->mtx);
4f723df4
MW
732 if (((dma_ctrlr->route_attr & route) == route) &&
733 (dma_ctrlr->locked == 0)) {
734
a17a75e2 735 dma_ctrlr->locked = 1;
886953e9 736 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
737 allocated_ctrlr = dma_ctrlr;
738 break;
739 }
886953e9 740 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
741 }
742
743 /* Check to see if we found a resource */
744 if (allocated_ctrlr == NULL)
745 goto err_ctrlr;
746
747 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
748 if (resource == NULL) {
749 printk(KERN_WARNING "Unable to allocate resource structure\n");
750 goto err_alloc;
751 }
752 resource->type = VME_DMA;
886953e9 753 resource->entry = &allocated_ctrlr->list;
a17a75e2
MW
754
755 return resource;
756
757err_alloc:
758 /* Unlock image */
886953e9 759 mutex_lock(&dma_ctrlr->mtx);
a17a75e2 760 dma_ctrlr->locked = 0;
886953e9 761 mutex_unlock(&dma_ctrlr->mtx);
a17a75e2
MW
762err_ctrlr:
763err_bus:
764 return NULL;
765}
58e50798 766EXPORT_SYMBOL(vme_dma_request);
a17a75e2
MW
767
768/*
769 * Start new list
770 */
771struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
772{
773 struct vme_dma_resource *ctrlr;
774 struct vme_dma_list *dma_list;
775
776 if (resource->type != VME_DMA) {
ead1f3e3 777 printk(KERN_ERR "Not a DMA resource\n");
a17a75e2
MW
778 return NULL;
779 }
780
781 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
782
ead1f3e3
MW
783 dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
784 if (dma_list == NULL) {
785 printk(KERN_ERR "Unable to allocate memory for new dma list\n");
a17a75e2
MW
786 return NULL;
787 }
886953e9 788 INIT_LIST_HEAD(&dma_list->entries);
a17a75e2 789 dma_list->parent = ctrlr;
886953e9 790 mutex_init(&dma_list->mtx);
a17a75e2
MW
791
792 return dma_list;
793}
794EXPORT_SYMBOL(vme_new_dma_list);
795
796/*
797 * Create "Pattern" type attributes
798 */
6af04b06 799struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
a17a75e2
MW
800{
801 struct vme_dma_attr *attributes;
802 struct vme_dma_pattern *pattern_attr;
803
ead1f3e3
MW
804 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
805 if (attributes == NULL) {
25958ce3 806 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
a17a75e2
MW
807 goto err_attr;
808 }
809
ead1f3e3
MW
810 pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
811 if (pattern_attr == NULL) {
25958ce3 812 printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
a17a75e2
MW
813 goto err_pat;
814 }
815
816 attributes->type = VME_DMA_PATTERN;
817 attributes->private = (void *)pattern_attr;
818
819 pattern_attr->pattern = pattern;
820 pattern_attr->type = type;
821
822 return attributes;
823
a17a75e2
MW
824err_pat:
825 kfree(attributes);
826err_attr:
827 return NULL;
828}
829EXPORT_SYMBOL(vme_dma_pattern_attribute);
830
831/*
832 * Create "PCI" type attributes
833 */
834struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
835{
836 struct vme_dma_attr *attributes;
837 struct vme_dma_pci *pci_attr;
838
839 /* XXX Run some sanity checks here */
840
ead1f3e3
MW
841 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
842 if (attributes == NULL) {
25958ce3 843 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
a17a75e2
MW
844 goto err_attr;
845 }
846
ead1f3e3
MW
847 pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
848 if (pci_attr == NULL) {
25958ce3 849 printk(KERN_ERR "Unable to allocate memory for pci attributes\n");
a17a75e2
MW
850 goto err_pci;
851 }
852
853
854
855 attributes->type = VME_DMA_PCI;
856 attributes->private = (void *)pci_attr;
857
858 pci_attr->address = address;
859
860 return attributes;
861
a17a75e2
MW
862err_pci:
863 kfree(attributes);
864err_attr:
865 return NULL;
866}
867EXPORT_SYMBOL(vme_dma_pci_attribute);
868
869/*
870 * Create "VME" type attributes
871 */
872struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
6af04b06 873 u32 aspace, u32 cycle, u32 dwidth)
a17a75e2
MW
874{
875 struct vme_dma_attr *attributes;
876 struct vme_dma_vme *vme_attr;
877
ead1f3e3 878 attributes = kmalloc(
a17a75e2 879 sizeof(struct vme_dma_attr), GFP_KERNEL);
ead1f3e3 880 if (attributes == NULL) {
25958ce3 881 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
a17a75e2
MW
882 goto err_attr;
883 }
884
ead1f3e3
MW
885 vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
886 if (vme_attr == NULL) {
25958ce3 887 printk(KERN_ERR "Unable to allocate memory for vme attributes\n");
a17a75e2
MW
888 goto err_vme;
889 }
890
891 attributes->type = VME_DMA_VME;
892 attributes->private = (void *)vme_attr;
893
894 vme_attr->address = address;
895 vme_attr->aspace = aspace;
896 vme_attr->cycle = cycle;
897 vme_attr->dwidth = dwidth;
898
899 return attributes;
900
a17a75e2
MW
901err_vme:
902 kfree(attributes);
903err_attr:
904 return NULL;
905}
906EXPORT_SYMBOL(vme_dma_vme_attribute);
907
908/*
909 * Free attribute
910 */
911void vme_dma_free_attribute(struct vme_dma_attr *attributes)
912{
913 kfree(attributes->private);
914 kfree(attributes);
915}
916EXPORT_SYMBOL(vme_dma_free_attribute);
917
918int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
919 struct vme_dma_attr *dest, size_t count)
920{
921 struct vme_bridge *bridge = list->parent->parent;
922 int retval;
923
924 if (bridge->dma_list_add == NULL) {
ead1f3e3 925 printk(KERN_WARNING "Link List DMA generation not supported\n");
a17a75e2
MW
926 return -EINVAL;
927 }
928
886953e9 929 if (!mutex_trylock(&list->mtx)) {
ead1f3e3 930 printk(KERN_ERR "Link List already submitted\n");
a17a75e2
MW
931 return -EINVAL;
932 }
933
934 retval = bridge->dma_list_add(list, src, dest, count);
935
886953e9 936 mutex_unlock(&list->mtx);
a17a75e2
MW
937
938 return retval;
939}
940EXPORT_SYMBOL(vme_dma_list_add);
941
942int vme_dma_list_exec(struct vme_dma_list *list)
943{
944 struct vme_bridge *bridge = list->parent->parent;
945 int retval;
946
947 if (bridge->dma_list_exec == NULL) {
ead1f3e3 948 printk(KERN_ERR "Link List DMA execution not supported\n");
a17a75e2
MW
949 return -EINVAL;
950 }
951
886953e9 952 mutex_lock(&list->mtx);
a17a75e2
MW
953
954 retval = bridge->dma_list_exec(list);
955
886953e9 956 mutex_unlock(&list->mtx);
a17a75e2
MW
957
958 return retval;
959}
960EXPORT_SYMBOL(vme_dma_list_exec);
961
962int vme_dma_list_free(struct vme_dma_list *list)
963{
964 struct vme_bridge *bridge = list->parent->parent;
965 int retval;
966
967 if (bridge->dma_list_empty == NULL) {
ead1f3e3 968 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
a17a75e2
MW
969 return -EINVAL;
970 }
971
886953e9 972 if (!mutex_trylock(&list->mtx)) {
ead1f3e3 973 printk(KERN_ERR "Link List in use\n");
a17a75e2
MW
974 return -EINVAL;
975 }
976
977 /*
978 * Empty out all of the entries from the dma list. We need to go to the
979 * low level driver as dma entries are driver specific.
980 */
981 retval = bridge->dma_list_empty(list);
982 if (retval) {
ead1f3e3 983 printk(KERN_ERR "Unable to empty link-list entries\n");
886953e9 984 mutex_unlock(&list->mtx);
a17a75e2
MW
985 return retval;
986 }
886953e9 987 mutex_unlock(&list->mtx);
a17a75e2
MW
988 kfree(list);
989
990 return retval;
991}
992EXPORT_SYMBOL(vme_dma_list_free);
993
994int vme_dma_free(struct vme_resource *resource)
995{
996 struct vme_dma_resource *ctrlr;
997
998 if (resource->type != VME_DMA) {
ead1f3e3 999 printk(KERN_ERR "Not a DMA resource\n");
a17a75e2
MW
1000 return -EINVAL;
1001 }
1002
1003 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1004
886953e9 1005 if (!mutex_trylock(&ctrlr->mtx)) {
ead1f3e3 1006 printk(KERN_ERR "Resource busy, can't free\n");
a17a75e2
MW
1007 return -EBUSY;
1008 }
1009
886953e9 1010 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
ead1f3e3 1011 printk(KERN_WARNING "Resource still processing transfers\n");
886953e9 1012 mutex_unlock(&ctrlr->mtx);
a17a75e2
MW
1013 return -EBUSY;
1014 }
1015
1016 ctrlr->locked = 0;
1017
886953e9 1018 mutex_unlock(&ctrlr->mtx);
a17a75e2 1019
fd5c2561
MW
1020 kfree(resource);
1021
a17a75e2
MW
1022 return 0;
1023}
1024EXPORT_SYMBOL(vme_dma_free);
1025
e2c6393f 1026void vme_bus_error_handler(struct vme_bridge *bridge,
472f16f3 1027 unsigned long long address, int am)
e2c6393f
DK
1028{
1029 struct vme_bus_error *error;
1030
1031 error = kmalloc(sizeof(struct vme_bus_error), GFP_ATOMIC);
1032 if (error) {
472f16f3 1033 error->aspace = vme_get_aspace(am);
e2c6393f 1034 error->address = address;
e2c6393f
DK
1035 list_add_tail(&error->list, &bridge->vme_errors);
1036 } else {
1037 dev_err(bridge->parent,
1038 "Unable to alloc memory for VMEbus Error reporting\n");
1039 }
1040}
1041EXPORT_SYMBOL(vme_bus_error_handler);
1042
1043/*
1044 * Find the first error in this address range
1045 */
1046struct vme_bus_error *vme_find_error(struct vme_bridge *bridge, u32 aspace,
1047 unsigned long long address, size_t count)
1048{
1049 struct list_head *err_pos;
1050 struct vme_bus_error *vme_err, *valid = NULL;
1051 unsigned long long bound;
1052
1053 bound = address + count;
1054
e2c6393f
DK
1055 err_pos = NULL;
1056 /* Iterate through errors */
1057 list_for_each(err_pos, &bridge->vme_errors) {
1058 vme_err = list_entry(err_pos, struct vme_bus_error, list);
472f16f3
DK
1059 if ((vme_err->aspace == aspace) &&
1060 (vme_err->address >= address) &&
1061 (vme_err->address < bound)) {
e2c6393f
DK
1062
1063 valid = vme_err;
1064 break;
1065 }
1066 }
1067
1068 return valid;
1069}
1070EXPORT_SYMBOL(vme_find_error);
1071
1072/*
1073 * Clear errors in the provided address range.
1074 */
1075void vme_clear_errors(struct vme_bridge *bridge, u32 aspace,
1076 unsigned long long address, size_t count)
1077{
1078 struct list_head *err_pos, *temp;
1079 struct vme_bus_error *vme_err;
1080 unsigned long long bound;
1081
1082 bound = address + count;
1083
e2c6393f
DK
1084 err_pos = NULL;
1085 /* Iterate through errors */
1086 list_for_each_safe(err_pos, temp, &bridge->vme_errors) {
1087 vme_err = list_entry(err_pos, struct vme_bus_error, list);
1088
472f16f3
DK
1089 if ((vme_err->aspace == aspace) &&
1090 (vme_err->address >= address) &&
1091 (vme_err->address < bound)) {
e2c6393f
DK
1092
1093 list_del(err_pos);
1094 kfree(vme_err);
1095 }
1096 }
1097}
1098EXPORT_SYMBOL(vme_clear_errors);
1099
c813f592
MW
1100void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1101{
1102 void (*call)(int, int, void *);
1103 void *priv_data;
1104
1105 call = bridge->irq[level - 1].callback[statid].func;
1106 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1107
1108 if (call != NULL)
1109 call(level, statid, priv_data);
1110 else
25958ce3
GKH
1111 printk(KERN_WARNING "Spurilous VME interrupt, level:%x, vector:%x\n",
1112 level, statid);
c813f592
MW
1113}
1114EXPORT_SYMBOL(vme_irq_handler);
1115
8f966dc4 1116int vme_irq_request(struct vme_dev *vdev, int level, int statid,
29848ac9 1117 void (*callback)(int, int, void *),
a17a75e2
MW
1118 void *priv_data)
1119{
1120 struct vme_bridge *bridge;
1121
8f966dc4 1122 bridge = vdev->bridge;
a17a75e2
MW
1123 if (bridge == NULL) {
1124 printk(KERN_ERR "Can't find VME bus\n");
1125 return -EINVAL;
1126 }
1127
ead1f3e3 1128 if ((level < 1) || (level > 7)) {
c813f592 1129 printk(KERN_ERR "Invalid interrupt level\n");
a17a75e2
MW
1130 return -EINVAL;
1131 }
1132
c813f592
MW
1133 if (bridge->irq_set == NULL) {
1134 printk(KERN_ERR "Configuring interrupts not supported\n");
a17a75e2
MW
1135 return -EINVAL;
1136 }
1137
886953e9 1138 mutex_lock(&bridge->irq_mtx);
c813f592
MW
1139
1140 if (bridge->irq[level - 1].callback[statid].func) {
886953e9 1141 mutex_unlock(&bridge->irq_mtx);
c813f592
MW
1142 printk(KERN_WARNING "VME Interrupt already taken\n");
1143 return -EBUSY;
1144 }
1145
1146 bridge->irq[level - 1].count++;
1147 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1148 bridge->irq[level - 1].callback[statid].func = callback;
1149
1150 /* Enable IRQ level */
29848ac9 1151 bridge->irq_set(bridge, level, 1, 1);
c813f592 1152
886953e9 1153 mutex_unlock(&bridge->irq_mtx);
c813f592
MW
1154
1155 return 0;
a17a75e2 1156}
c813f592 1157EXPORT_SYMBOL(vme_irq_request);
a17a75e2 1158
8f966dc4 1159void vme_irq_free(struct vme_dev *vdev, int level, int statid)
a17a75e2
MW
1160{
1161 struct vme_bridge *bridge;
1162
8f966dc4 1163 bridge = vdev->bridge;
a17a75e2
MW
1164 if (bridge == NULL) {
1165 printk(KERN_ERR "Can't find VME bus\n");
1166 return;
1167 }
1168
ead1f3e3 1169 if ((level < 1) || (level > 7)) {
c813f592 1170 printk(KERN_ERR "Invalid interrupt level\n");
a17a75e2
MW
1171 return;
1172 }
1173
c813f592
MW
1174 if (bridge->irq_set == NULL) {
1175 printk(KERN_ERR "Configuring interrupts not supported\n");
a17a75e2
MW
1176 return;
1177 }
1178
886953e9 1179 mutex_lock(&bridge->irq_mtx);
c813f592
MW
1180
1181 bridge->irq[level - 1].count--;
1182
1183 /* Disable IRQ level if no more interrupts attached at this level*/
1184 if (bridge->irq[level - 1].count == 0)
29848ac9 1185 bridge->irq_set(bridge, level, 0, 1);
c813f592
MW
1186
1187 bridge->irq[level - 1].callback[statid].func = NULL;
1188 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1189
886953e9 1190 mutex_unlock(&bridge->irq_mtx);
a17a75e2 1191}
c813f592 1192EXPORT_SYMBOL(vme_irq_free);
a17a75e2 1193
8f966dc4 1194int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
a17a75e2
MW
1195{
1196 struct vme_bridge *bridge;
1197
8f966dc4 1198 bridge = vdev->bridge;
a17a75e2
MW
1199 if (bridge == NULL) {
1200 printk(KERN_ERR "Can't find VME bus\n");
1201 return -EINVAL;
1202 }
1203
ead1f3e3 1204 if ((level < 1) || (level > 7)) {
a17a75e2
MW
1205 printk(KERN_WARNING "Invalid interrupt level\n");
1206 return -EINVAL;
1207 }
1208
c813f592 1209 if (bridge->irq_generate == NULL) {
ead1f3e3 1210 printk(KERN_WARNING "Interrupt generation not supported\n");
a17a75e2
MW
1211 return -EINVAL;
1212 }
1213
29848ac9 1214 return bridge->irq_generate(bridge, level, statid);
a17a75e2 1215}
c813f592 1216EXPORT_SYMBOL(vme_irq_generate);
a17a75e2 1217
42fb5031
MW
1218/*
1219 * Request the location monitor, return resource or NULL
1220 */
8f966dc4 1221struct vme_resource *vme_lm_request(struct vme_dev *vdev)
a17a75e2
MW
1222{
1223 struct vme_bridge *bridge;
42fb5031
MW
1224 struct list_head *lm_pos = NULL;
1225 struct vme_lm_resource *allocated_lm = NULL;
1226 struct vme_lm_resource *lm = NULL;
1227 struct vme_resource *resource = NULL;
a17a75e2 1228
8f966dc4 1229 bridge = vdev->bridge;
a17a75e2
MW
1230 if (bridge == NULL) {
1231 printk(KERN_ERR "Can't find VME bus\n");
42fb5031
MW
1232 goto err_bus;
1233 }
1234
1235 /* Loop through DMA resources */
886953e9 1236 list_for_each(lm_pos, &bridge->lm_resources) {
42fb5031
MW
1237 lm = list_entry(lm_pos,
1238 struct vme_lm_resource, list);
1239
1240 if (lm == NULL) {
25958ce3 1241 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
42fb5031
MW
1242 continue;
1243 }
1244
1245 /* Find an unlocked controller */
886953e9 1246 mutex_lock(&lm->mtx);
42fb5031
MW
1247 if (lm->locked == 0) {
1248 lm->locked = 1;
886953e9 1249 mutex_unlock(&lm->mtx);
42fb5031
MW
1250 allocated_lm = lm;
1251 break;
1252 }
886953e9 1253 mutex_unlock(&lm->mtx);
42fb5031
MW
1254 }
1255
1256 /* Check to see if we found a resource */
1257 if (allocated_lm == NULL)
1258 goto err_lm;
1259
1260 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
1261 if (resource == NULL) {
1262 printk(KERN_ERR "Unable to allocate resource structure\n");
1263 goto err_alloc;
1264 }
1265 resource->type = VME_LM;
886953e9 1266 resource->entry = &allocated_lm->list;
42fb5031
MW
1267
1268 return resource;
1269
1270err_alloc:
1271 /* Unlock image */
886953e9 1272 mutex_lock(&lm->mtx);
42fb5031 1273 lm->locked = 0;
886953e9 1274 mutex_unlock(&lm->mtx);
42fb5031
MW
1275err_lm:
1276err_bus:
1277 return NULL;
1278}
1279EXPORT_SYMBOL(vme_lm_request);
1280
1281int vme_lm_count(struct vme_resource *resource)
1282{
1283 struct vme_lm_resource *lm;
1284
1285 if (resource->type != VME_LM) {
1286 printk(KERN_ERR "Not a Location Monitor resource\n");
1287 return -EINVAL;
1288 }
1289
1290 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1291
1292 return lm->monitors;
1293}
1294EXPORT_SYMBOL(vme_lm_count);
1295
1296int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
6af04b06 1297 u32 aspace, u32 cycle)
42fb5031
MW
1298{
1299 struct vme_bridge *bridge = find_bridge(resource);
1300 struct vme_lm_resource *lm;
1301
1302 if (resource->type != VME_LM) {
1303 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1304 return -EINVAL;
1305 }
1306
42fb5031
MW
1307 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1308
a17a75e2 1309 if (bridge->lm_set == NULL) {
42fb5031 1310 printk(KERN_ERR "vme_lm_set not supported\n");
a17a75e2
MW
1311 return -EINVAL;
1312 }
1313
8be9226c 1314 return bridge->lm_set(lm, lm_base, aspace, cycle);
a17a75e2
MW
1315}
1316EXPORT_SYMBOL(vme_lm_set);
1317
42fb5031 1318int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
6af04b06 1319 u32 *aspace, u32 *cycle)
a17a75e2 1320{
42fb5031
MW
1321 struct vme_bridge *bridge = find_bridge(resource);
1322 struct vme_lm_resource *lm;
a17a75e2 1323
42fb5031
MW
1324 if (resource->type != VME_LM) {
1325 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1326 return -EINVAL;
1327 }
1328
42fb5031
MW
1329 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1330
a17a75e2 1331 if (bridge->lm_get == NULL) {
42fb5031 1332 printk(KERN_ERR "vme_lm_get not supported\n");
a17a75e2
MW
1333 return -EINVAL;
1334 }
1335
42fb5031 1336 return bridge->lm_get(lm, lm_base, aspace, cycle);
a17a75e2
MW
1337}
1338EXPORT_SYMBOL(vme_lm_get);
1339
42fb5031
MW
1340int vme_lm_attach(struct vme_resource *resource, int monitor,
1341 void (*callback)(int))
a17a75e2 1342{
42fb5031
MW
1343 struct vme_bridge *bridge = find_bridge(resource);
1344 struct vme_lm_resource *lm;
a17a75e2 1345
42fb5031
MW
1346 if (resource->type != VME_LM) {
1347 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1348 return -EINVAL;
1349 }
1350
42fb5031
MW
1351 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1352
a17a75e2 1353 if (bridge->lm_attach == NULL) {
42fb5031 1354 printk(KERN_ERR "vme_lm_attach not supported\n");
a17a75e2
MW
1355 return -EINVAL;
1356 }
1357
42fb5031 1358 return bridge->lm_attach(lm, monitor, callback);
a17a75e2
MW
1359}
1360EXPORT_SYMBOL(vme_lm_attach);
1361
42fb5031 1362int vme_lm_detach(struct vme_resource *resource, int monitor)
a17a75e2 1363{
42fb5031
MW
1364 struct vme_bridge *bridge = find_bridge(resource);
1365 struct vme_lm_resource *lm;
a17a75e2 1366
42fb5031
MW
1367 if (resource->type != VME_LM) {
1368 printk(KERN_ERR "Not a Location Monitor resource\n");
a17a75e2
MW
1369 return -EINVAL;
1370 }
1371
42fb5031
MW
1372 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1373
a17a75e2 1374 if (bridge->lm_detach == NULL) {
42fb5031 1375 printk(KERN_ERR "vme_lm_detach not supported\n");
a17a75e2
MW
1376 return -EINVAL;
1377 }
1378
42fb5031 1379 return bridge->lm_detach(lm, monitor);
a17a75e2
MW
1380}
1381EXPORT_SYMBOL(vme_lm_detach);
1382
42fb5031
MW
1383void vme_lm_free(struct vme_resource *resource)
1384{
1385 struct vme_lm_resource *lm;
1386
1387 if (resource->type != VME_LM) {
1388 printk(KERN_ERR "Not a Location Monitor resource\n");
1389 return;
1390 }
1391
1392 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1393
886953e9 1394 mutex_lock(&lm->mtx);
42fb5031 1395
8be9226c
MW
1396 /* XXX
1397 * Check to see that there aren't any callbacks still attached, if
1398 * there are we should probably be detaching them!
1399 */
42fb5031
MW
1400
1401 lm->locked = 0;
1402
886953e9 1403 mutex_unlock(&lm->mtx);
8be9226c
MW
1404
1405 kfree(resource);
42fb5031
MW
1406}
1407EXPORT_SYMBOL(vme_lm_free);
1408
d7729f0f 1409int vme_slot_num(struct vme_dev *vdev)
a17a75e2
MW
1410{
1411 struct vme_bridge *bridge;
1412
8f966dc4 1413 bridge = vdev->bridge;
a17a75e2
MW
1414 if (bridge == NULL) {
1415 printk(KERN_ERR "Can't find VME bus\n");
1416 return -EINVAL;
1417 }
1418
1419 if (bridge->slot_get == NULL) {
d7729f0f 1420 printk(KERN_WARNING "vme_slot_num not supported\n");
a17a75e2
MW
1421 return -EINVAL;
1422 }
1423
29848ac9 1424 return bridge->slot_get(bridge);
a17a75e2 1425}
d7729f0f 1426EXPORT_SYMBOL(vme_slot_num);
a17a75e2 1427
978f47d6
MW
1428int vme_bus_num(struct vme_dev *vdev)
1429{
1430 struct vme_bridge *bridge;
1431
1432 bridge = vdev->bridge;
1433 if (bridge == NULL) {
1434 pr_err("Can't find VME bus\n");
1435 return -EINVAL;
1436 }
1437
1438 return bridge->num;
1439}
1440EXPORT_SYMBOL(vme_bus_num);
a17a75e2
MW
1441
1442/* - Bridge Registration --------------------------------------------------- */
1443
5b93c2a2
MV
1444static void vme_dev_release(struct device *dev)
1445{
1446 kfree(dev_to_vme_dev(dev));
1447}
1448
1449int vme_register_bridge(struct vme_bridge *bridge)
a17a75e2
MW
1450{
1451 int i;
733e3ef0 1452 int ret = -1;
a17a75e2 1453
733e3ef0 1454 mutex_lock(&vme_buses_lock);
a17a75e2 1455 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
733e3ef0
MV
1456 if ((vme_bus_numbers & (1 << i)) == 0) {
1457 vme_bus_numbers |= (1 << i);
1458 bridge->num = i;
5d6abf37 1459 INIT_LIST_HEAD(&bridge->devices);
733e3ef0
MV
1460 list_add_tail(&bridge->bus_list, &vme_bus_list);
1461 ret = 0;
a17a75e2
MW
1462 break;
1463 }
1464 }
733e3ef0 1465 mutex_unlock(&vme_buses_lock);
a17a75e2 1466
733e3ef0 1467 return ret;
a17a75e2 1468}
5b93c2a2 1469EXPORT_SYMBOL(vme_register_bridge);
a17a75e2 1470
5b93c2a2 1471void vme_unregister_bridge(struct vme_bridge *bridge)
a17a75e2 1472{
5d6abf37
MV
1473 struct vme_dev *vdev;
1474 struct vme_dev *tmp;
1475
733e3ef0
MV
1476 mutex_lock(&vme_buses_lock);
1477 vme_bus_numbers &= ~(1 << bridge->num);
5d6abf37
MV
1478 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1479 list_del(&vdev->drv_list);
1480 list_del(&vdev->bridge_list);
1481 device_unregister(&vdev->dev);
1482 }
733e3ef0
MV
1483 list_del(&bridge->bus_list);
1484 mutex_unlock(&vme_buses_lock);
a17a75e2 1485}
5d6abf37 1486EXPORT_SYMBOL(vme_unregister_bridge);
a17a75e2 1487
5d6abf37
MV
1488/* - Driver Registration --------------------------------------------------- */
1489
1490static int __vme_register_driver_bus(struct vme_driver *drv,
1491 struct vme_bridge *bridge, unsigned int ndevs)
1492{
1493 int err;
1494 unsigned int i;
1495 struct vme_dev *vdev;
1496 struct vme_dev *tmp;
1497
1498 for (i = 0; i < ndevs; i++) {
1499 vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
1500 if (!vdev) {
1501 err = -ENOMEM;
f6c39d4f
MV
1502 goto err_devalloc;
1503 }
a916a391 1504 vdev->num = i;
8f966dc4 1505 vdev->bridge = bridge;
5d6abf37
MV
1506 vdev->dev.platform_data = drv;
1507 vdev->dev.release = vme_dev_release;
8f966dc4
MV
1508 vdev->dev.parent = bridge->parent;
1509 vdev->dev.bus = &vme_bus_type;
a916a391
MV
1510 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1511 vdev->num);
a17a75e2 1512
5d6abf37
MV
1513 err = device_register(&vdev->dev);
1514 if (err)
a17a75e2 1515 goto err_reg;
a17a75e2 1516
5d6abf37
MV
1517 if (vdev->dev.platform_data) {
1518 list_add_tail(&vdev->drv_list, &drv->devices);
1519 list_add_tail(&vdev->bridge_list, &bridge->devices);
1520 } else
1521 device_unregister(&vdev->dev);
1522 }
1523 return 0;
a17a75e2 1524
a17a75e2 1525err_reg:
def1820d 1526 put_device(&vdev->dev);
8f966dc4 1527 kfree(vdev);
f6c39d4f 1528err_devalloc:
5d6abf37
MV
1529 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1530 list_del(&vdev->drv_list);
1531 list_del(&vdev->bridge_list);
8f966dc4 1532 device_unregister(&vdev->dev);
a17a75e2 1533 }
5d6abf37 1534 return err;
a17a75e2 1535}
a17a75e2 1536
5d6abf37 1537static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
a17a75e2 1538{
5d6abf37
MV
1539 struct vme_bridge *bridge;
1540 int err = 0;
a17a75e2 1541
5d6abf37
MV
1542 mutex_lock(&vme_buses_lock);
1543 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1544 /*
1545 * This cannot cause trouble as we already have vme_buses_lock
1546 * and if the bridge is removed, it will have to go through
1547 * vme_unregister_bridge() to do it (which calls remove() on
1548 * the bridge which in turn tries to acquire vme_buses_lock and
c26f6112 1549 * will have to wait).
5d6abf37
MV
1550 */
1551 err = __vme_register_driver_bus(drv, bridge, ndevs);
1552 if (err)
1553 break;
a17a75e2 1554 }
5d6abf37
MV
1555 mutex_unlock(&vme_buses_lock);
1556 return err;
a17a75e2 1557}
a17a75e2 1558
5d6abf37 1559int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
a17a75e2 1560{
5d6abf37
MV
1561 int err;
1562
a17a75e2
MW
1563 drv->driver.name = drv->name;
1564 drv->driver.bus = &vme_bus_type;
5d6abf37
MV
1565 INIT_LIST_HEAD(&drv->devices);
1566
1567 err = driver_register(&drv->driver);
1568 if (err)
1569 return err;
a17a75e2 1570
5d6abf37
MV
1571 err = __vme_register_driver(drv, ndevs);
1572 if (err)
1573 driver_unregister(&drv->driver);
1574
1575 return err;
a17a75e2
MW
1576}
1577EXPORT_SYMBOL(vme_register_driver);
1578
ead1f3e3 1579void vme_unregister_driver(struct vme_driver *drv)
a17a75e2 1580{
5d6abf37
MV
1581 struct vme_dev *dev, *dev_tmp;
1582
1583 mutex_lock(&vme_buses_lock);
1584 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1585 list_del(&dev->drv_list);
1586 list_del(&dev->bridge_list);
1587 device_unregister(&dev->dev);
1588 }
1589 mutex_unlock(&vme_buses_lock);
1590
a17a75e2
MW
1591 driver_unregister(&drv->driver);
1592}
1593EXPORT_SYMBOL(vme_unregister_driver);
1594
1595/* - Bus Registration ------------------------------------------------------ */
1596
a17a75e2
MW
1597static int vme_bus_match(struct device *dev, struct device_driver *drv)
1598{
5d6abf37 1599 struct vme_driver *vme_drv;
a17a75e2 1600
5d6abf37 1601 vme_drv = container_of(drv, struct vme_driver, driver);
a17a75e2 1602
5d6abf37
MV
1603 if (dev->platform_data == vme_drv) {
1604 struct vme_dev *vdev = dev_to_vme_dev(dev);
a17a75e2 1605
5d6abf37
MV
1606 if (vme_drv->match && vme_drv->match(vdev))
1607 return 1;
a37b0dad 1608
5d6abf37 1609 dev->platform_data = NULL;
a17a75e2 1610 }
a17a75e2
MW
1611 return 0;
1612}
1613
1614static int vme_bus_probe(struct device *dev)
1615{
a17a75e2 1616 int retval = -ENODEV;
5d6abf37
MV
1617 struct vme_driver *driver;
1618 struct vme_dev *vdev = dev_to_vme_dev(dev);
a17a75e2 1619
5d6abf37 1620 driver = dev->platform_data;
a17a75e2 1621
ead1f3e3 1622 if (driver->probe != NULL)
8f966dc4 1623 retval = driver->probe(vdev);
a17a75e2
MW
1624
1625 return retval;
1626}
1627
1628static int vme_bus_remove(struct device *dev)
1629{
a17a75e2 1630 int retval = -ENODEV;
5d6abf37
MV
1631 struct vme_driver *driver;
1632 struct vme_dev *vdev = dev_to_vme_dev(dev);
a17a75e2 1633
5d6abf37 1634 driver = dev->platform_data;
a17a75e2 1635
ead1f3e3 1636 if (driver->remove != NULL)
8f966dc4 1637 retval = driver->remove(vdev);
a17a75e2
MW
1638
1639 return retval;
1640}
1641
1642struct bus_type vme_bus_type = {
1643 .name = "vme",
1644 .match = vme_bus_match,
1645 .probe = vme_bus_probe,
1646 .remove = vme_bus_remove,
1647};
1648EXPORT_SYMBOL(vme_bus_type);
1649
ead1f3e3 1650static int __init vme_init(void)
a17a75e2
MW
1651{
1652 return bus_register(&vme_bus_type);
1653}
1654
ead1f3e3 1655static void __exit vme_exit(void)
a17a75e2
MW
1656{
1657 bus_unregister(&vme_bus_type);
1658}
1659
c326cc02 1660subsys_initcall(vme_init);
a17a75e2 1661module_exit(vme_exit);