Linux 6.13-rc4
[linux.git] / drivers / staging / vme_user / vme.c
blob42304c9f83a2dcf111a3596757a2717c562a8283
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * VME Bridge Framework
5 * Author: Martyn Welch <martyn.welch@ge.com>
6 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
8 * Based on work by Tom Armistead and Ajit Prem
9 * Copyright 2004 Motorola Inc.
12 #include <linux/init.h>
13 #include <linux/export.h>
14 #include <linux/mm.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/pci.h>
19 #include <linux/poll.h>
20 #include <linux/highmem.h>
21 #include <linux/interrupt.h>
22 #include <linux/pagemap.h>
23 #include <linux/device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/syscalls.h>
26 #include <linux/mutex.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
30 #include "vme.h"
31 #include "vme_bridge.h"
33 /* Bitmask and list of registered buses both protected by common mutex */
34 static unsigned int vme_bus_numbers;
35 static LIST_HEAD(vme_bus_list);
36 static DEFINE_MUTEX(vme_buses_lock);
38 static int __init vme_init(void);
40 static struct vme_dev *dev_to_vme_dev(struct device *dev)
42 return container_of(dev, struct vme_dev, dev);
46 * Find the bridge that the resource is associated with.
48 static struct vme_bridge *find_bridge(struct vme_resource *resource)
50 /* Get list to search */
51 switch (resource->type) {
52 case VME_MASTER:
53 return list_entry(resource->entry, struct vme_master_resource,
54 list)->parent;
55 case VME_SLAVE:
56 return list_entry(resource->entry, struct vme_slave_resource,
57 list)->parent;
58 case VME_DMA:
59 return list_entry(resource->entry, struct vme_dma_resource,
60 list)->parent;
61 case VME_LM:
62 return list_entry(resource->entry, struct vme_lm_resource,
63 list)->parent;
64 default:
65 return NULL;
69 /**
70 * vme_alloc_consistent - Allocate contiguous memory.
71 * @resource: Pointer to VME resource.
72 * @size: Size of allocation required.
73 * @dma: Pointer to variable to store physical address of allocation.
75 * Allocate a contiguous block of memory for use by the driver. This is used to
76 * create the buffers for the slave windows.
78 * Return: Virtual address of allocation on success, NULL on failure.
80 void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
81 dma_addr_t *dma)
83 struct vme_bridge *bridge = find_bridge(resource);
85 if (!bridge->alloc_consistent) {
86 dev_err(bridge->parent,
87 "alloc_consistent not supported by bridge %s\n",
88 bridge->name);
89 return NULL;
92 return bridge->alloc_consistent(bridge->parent, size, dma);
94 EXPORT_SYMBOL(vme_alloc_consistent);
96 /**
97 * vme_free_consistent - Free previously allocated memory.
98 * @resource: Pointer to VME resource.
99 * @size: Size of allocation to free.
100 * @vaddr: Virtual address of allocation.
101 * @dma: Physical address of allocation.
103 * Free previously allocated block of contiguous memory.
105 void vme_free_consistent(struct vme_resource *resource, size_t size,
106 void *vaddr, dma_addr_t dma)
108 struct vme_bridge *bridge = find_bridge(resource);
110 if (!bridge->free_consistent) {
111 dev_err(bridge->parent,
112 "free_consistent not supported by bridge %s\n",
113 bridge->name);
114 return;
117 bridge->free_consistent(bridge->parent, size, vaddr, dma);
119 EXPORT_SYMBOL(vme_free_consistent);
122 * vme_get_size - Helper function returning size of a VME window
123 * @resource: Pointer to VME slave or master resource.
125 * Determine the size of the VME window provided. This is a helper
126 * function, wrappering the call to vme_master_get or vme_slave_get
127 * depending on the type of window resource handed to it.
129 * Return: Size of the window on success, zero on failure.
131 size_t vme_get_size(struct vme_resource *resource)
133 struct vme_bridge *bridge = find_bridge(resource);
134 int enabled, retval;
135 unsigned long long base, size;
136 dma_addr_t buf_base;
137 u32 aspace, cycle, dwidth;
139 switch (resource->type) {
140 case VME_MASTER:
141 retval = vme_master_get(resource, &enabled, &base, &size,
142 &aspace, &cycle, &dwidth);
143 if (retval)
144 return 0;
146 return size;
147 case VME_SLAVE:
148 retval = vme_slave_get(resource, &enabled, &base, &size,
149 &buf_base, &aspace, &cycle);
150 if (retval)
151 return 0;
153 return size;
154 case VME_DMA:
155 return 0;
156 default:
157 dev_err(bridge->parent, "Unknown resource type\n");
158 return 0;
161 EXPORT_SYMBOL(vme_get_size);
163 int vme_check_window(struct vme_bridge *bridge, u32 aspace,
164 unsigned long long vme_base, unsigned long long size)
166 int retval = 0;
168 if (vme_base + size < size)
169 return -EINVAL;
171 switch (aspace) {
172 case VME_A16:
173 if (vme_base + size > VME_A16_MAX)
174 retval = -EFAULT;
175 break;
176 case VME_A24:
177 if (vme_base + size > VME_A24_MAX)
178 retval = -EFAULT;
179 break;
180 case VME_A32:
181 if (vme_base + size > VME_A32_MAX)
182 retval = -EFAULT;
183 break;
184 case VME_A64:
185 /* The VME_A64_MAX limit is actually U64_MAX + 1 */
186 break;
187 case VME_CRCSR:
188 if (vme_base + size > VME_CRCSR_MAX)
189 retval = -EFAULT;
190 break;
191 case VME_USER1:
192 case VME_USER2:
193 case VME_USER3:
194 case VME_USER4:
195 /* User Defined */
196 break;
197 default:
198 dev_err(bridge->parent, "Invalid address space\n");
199 retval = -EINVAL;
200 break;
203 return retval;
205 EXPORT_SYMBOL(vme_check_window);
207 static u32 vme_get_aspace(int am)
209 switch (am) {
210 case 0x29:
211 case 0x2D:
212 return VME_A16;
213 case 0x38:
214 case 0x39:
215 case 0x3A:
216 case 0x3B:
217 case 0x3C:
218 case 0x3D:
219 case 0x3E:
220 case 0x3F:
221 return VME_A24;
222 case 0x8:
223 case 0x9:
224 case 0xA:
225 case 0xB:
226 case 0xC:
227 case 0xD:
228 case 0xE:
229 case 0xF:
230 return VME_A32;
231 case 0x0:
232 case 0x1:
233 case 0x3:
234 return VME_A64;
237 return 0;
241 * vme_slave_request - Request a VME slave window resource.
242 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
243 * @address: Required VME address space.
244 * @cycle: Required VME data transfer cycle type.
246 * Request use of a VME window resource capable of being set for the requested
247 * address space and data transfer cycle.
249 * Return: Pointer to VME resource on success, NULL on failure.
251 struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
252 u32 cycle)
254 struct vme_bridge *bridge;
255 struct vme_slave_resource *allocated_image = NULL;
256 struct vme_slave_resource *slave_image = NULL;
257 struct vme_resource *resource = NULL;
259 bridge = vdev->bridge;
260 if (!bridge) {
261 dev_err(&vdev->dev, "Can't find VME bus\n");
262 goto err_bus;
265 /* Loop through slave resources */
266 list_for_each_entry(slave_image, &bridge->slave_resources, list) {
267 if (!slave_image) {
268 dev_err(bridge->parent,
269 "Registered NULL Slave resource\n");
270 continue;
273 /* Find an unlocked and compatible image */
274 mutex_lock(&slave_image->mtx);
275 if (((slave_image->address_attr & address) == address) &&
276 ((slave_image->cycle_attr & cycle) == cycle) &&
277 (slave_image->locked == 0)) {
278 slave_image->locked = 1;
279 mutex_unlock(&slave_image->mtx);
280 allocated_image = slave_image;
281 break;
283 mutex_unlock(&slave_image->mtx);
286 /* No free image */
287 if (!allocated_image)
288 goto err_image;
290 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
291 if (!resource)
292 goto err_alloc;
294 resource->type = VME_SLAVE;
295 resource->entry = &allocated_image->list;
297 return resource;
299 err_alloc:
300 /* Unlock image */
301 mutex_lock(&slave_image->mtx);
302 slave_image->locked = 0;
303 mutex_unlock(&slave_image->mtx);
304 err_image:
305 err_bus:
306 return NULL;
308 EXPORT_SYMBOL(vme_slave_request);
311 * vme_slave_set - Set VME slave window configuration.
312 * @resource: Pointer to VME slave resource.
313 * @enabled: State to which the window should be configured.
314 * @vme_base: Base address for the window.
315 * @size: Size of the VME window.
316 * @buf_base: Based address of buffer used to provide VME slave window storage.
317 * @aspace: VME address space for the VME window.
318 * @cycle: VME data transfer cycle type for the VME window.
320 * Set configuration for provided VME slave window.
322 * Return: Zero on success, -EINVAL if operation is not supported on this
323 * device, if an invalid resource has been provided or invalid
324 * attributes are provided. Hardware specific errors may also be
325 * returned.
327 int vme_slave_set(struct vme_resource *resource, int enabled,
328 unsigned long long vme_base, unsigned long long size,
329 dma_addr_t buf_base, u32 aspace, u32 cycle)
331 struct vme_bridge *bridge = find_bridge(resource);
332 struct vme_slave_resource *image;
333 int retval;
335 if (resource->type != VME_SLAVE) {
336 dev_err(bridge->parent, "Not a slave resource\n");
337 return -EINVAL;
340 image = list_entry(resource->entry, struct vme_slave_resource, list);
342 if (!bridge->slave_set) {
343 dev_err(bridge->parent, "%s not supported\n", __func__);
344 return -EINVAL;
347 if (!(((image->address_attr & aspace) == aspace) &&
348 ((image->cycle_attr & cycle) == cycle))) {
349 dev_err(bridge->parent, "Invalid attributes\n");
350 return -EINVAL;
353 retval = vme_check_window(bridge, aspace, vme_base, size);
354 if (retval)
355 return retval;
357 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
358 aspace, cycle);
360 EXPORT_SYMBOL(vme_slave_set);
363 * vme_slave_get - Retrieve VME slave window configuration.
364 * @resource: Pointer to VME slave resource.
365 * @enabled: Pointer to variable for storing state.
366 * @vme_base: Pointer to variable for storing window base address.
367 * @size: Pointer to variable for storing window size.
368 * @buf_base: Pointer to variable for storing slave buffer base address.
369 * @aspace: Pointer to variable for storing VME address space.
370 * @cycle: Pointer to variable for storing VME data transfer cycle type.
372 * Return configuration for provided VME slave window.
374 * Return: Zero on success, -EINVAL if operation is not supported on this
375 * device or if an invalid resource has been provided.
377 int vme_slave_get(struct vme_resource *resource, int *enabled,
378 unsigned long long *vme_base, unsigned long long *size,
379 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
381 struct vme_bridge *bridge = find_bridge(resource);
382 struct vme_slave_resource *image;
384 if (resource->type != VME_SLAVE) {
385 dev_err(bridge->parent, "Not a slave resource\n");
386 return -EINVAL;
389 image = list_entry(resource->entry, struct vme_slave_resource, list);
391 if (!bridge->slave_get) {
392 dev_err(bridge->parent, "%s not supported\n", __func__);
393 return -EINVAL;
396 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
397 aspace, cycle);
399 EXPORT_SYMBOL(vme_slave_get);
402 * vme_slave_free - Free VME slave window
403 * @resource: Pointer to VME slave resource.
405 * Free the provided slave resource so that it may be reallocated.
407 void vme_slave_free(struct vme_resource *resource)
409 struct vme_bridge *bridge = find_bridge(resource);
410 struct vme_slave_resource *slave_image;
412 if (resource->type != VME_SLAVE) {
413 dev_err(bridge->parent, "Not a slave resource\n");
414 return;
417 slave_image = list_entry(resource->entry, struct vme_slave_resource,
418 list);
420 /* Unlock image */
421 mutex_lock(&slave_image->mtx);
422 if (slave_image->locked == 0)
423 dev_err(bridge->parent, "Image is already free\n");
425 slave_image->locked = 0;
426 mutex_unlock(&slave_image->mtx);
428 /* Free up resource memory */
429 kfree(resource);
431 EXPORT_SYMBOL(vme_slave_free);
434 * vme_master_request - Request a VME master window resource.
435 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
436 * @address: Required VME address space.
437 * @cycle: Required VME data transfer cycle type.
438 * @dwidth: Required VME data transfer width.
440 * Request use of a VME window resource capable of being set for the requested
441 * address space, data transfer cycle and width.
443 * Return: Pointer to VME resource on success, NULL on failure.
445 struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
446 u32 cycle, u32 dwidth)
448 struct vme_bridge *bridge;
449 struct vme_master_resource *allocated_image = NULL;
450 struct vme_master_resource *master_image = NULL;
451 struct vme_resource *resource = NULL;
453 bridge = vdev->bridge;
454 if (!bridge) {
455 dev_err(&vdev->dev, "Can't find VME bus\n");
456 goto err_bus;
459 /* Loop through master resources */
460 list_for_each_entry(master_image, &bridge->master_resources, list) {
461 if (!master_image) {
462 dev_warn(bridge->parent,
463 "Registered NULL master resource\n");
464 continue;
467 /* Find an unlocked and compatible image */
468 spin_lock(&master_image->lock);
469 if (((master_image->address_attr & address) == address) &&
470 ((master_image->cycle_attr & cycle) == cycle) &&
471 ((master_image->width_attr & dwidth) == dwidth) &&
472 (master_image->locked == 0)) {
473 master_image->locked = 1;
474 spin_unlock(&master_image->lock);
475 allocated_image = master_image;
476 break;
478 spin_unlock(&master_image->lock);
481 /* Check to see if we found a resource */
482 if (!allocated_image) {
483 dev_err(&vdev->dev, "Can't find a suitable resource\n");
484 goto err_image;
487 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
488 if (!resource)
489 goto err_alloc;
491 resource->type = VME_MASTER;
492 resource->entry = &allocated_image->list;
494 return resource;
496 err_alloc:
497 /* Unlock image */
498 spin_lock(&master_image->lock);
499 master_image->locked = 0;
500 spin_unlock(&master_image->lock);
501 err_image:
502 err_bus:
503 return NULL;
505 EXPORT_SYMBOL(vme_master_request);
508 * vme_master_set - Set VME master window configuration.
509 * @resource: Pointer to VME master resource.
510 * @enabled: State to which the window should be configured.
511 * @vme_base: Base address for the window.
512 * @size: Size of the VME window.
513 * @aspace: VME address space for the VME window.
514 * @cycle: VME data transfer cycle type for the VME window.
515 * @dwidth: VME data transfer width for the VME window.
517 * Set configuration for provided VME master window.
519 * Return: Zero on success, -EINVAL if operation is not supported on this
520 * device, if an invalid resource has been provided or invalid
521 * attributes are provided. Hardware specific errors may also be
522 * returned.
524 int vme_master_set(struct vme_resource *resource, int enabled,
525 unsigned long long vme_base, unsigned long long size,
526 u32 aspace, u32 cycle, u32 dwidth)
528 struct vme_bridge *bridge = find_bridge(resource);
529 struct vme_master_resource *image;
530 int retval;
532 if (resource->type != VME_MASTER) {
533 dev_err(bridge->parent, "Not a master resource\n");
534 return -EINVAL;
537 image = list_entry(resource->entry, struct vme_master_resource, list);
539 if (!bridge->master_set) {
540 dev_warn(bridge->parent, "%s not supported\n", __func__);
541 return -EINVAL;
544 if (!(((image->address_attr & aspace) == aspace) &&
545 ((image->cycle_attr & cycle) == cycle) &&
546 ((image->width_attr & dwidth) == dwidth))) {
547 dev_warn(bridge->parent, "Invalid attributes\n");
548 return -EINVAL;
551 retval = vme_check_window(bridge, aspace, vme_base, size);
552 if (retval)
553 return retval;
555 return bridge->master_set(image, enabled, vme_base, size, aspace,
556 cycle, dwidth);
558 EXPORT_SYMBOL(vme_master_set);
561 * vme_master_get - Retrieve VME master window configuration.
562 * @resource: Pointer to VME master resource.
563 * @enabled: Pointer to variable for storing state.
564 * @vme_base: Pointer to variable for storing window base address.
565 * @size: Pointer to variable for storing window size.
566 * @aspace: Pointer to variable for storing VME address space.
567 * @cycle: Pointer to variable for storing VME data transfer cycle type.
568 * @dwidth: Pointer to variable for storing VME data transfer width.
570 * Return configuration for provided VME master window.
572 * Return: Zero on success, -EINVAL if operation is not supported on this
573 * device or if an invalid resource has been provided.
575 int vme_master_get(struct vme_resource *resource, int *enabled,
576 unsigned long long *vme_base, unsigned long long *size,
577 u32 *aspace, u32 *cycle, u32 *dwidth)
579 struct vme_bridge *bridge = find_bridge(resource);
580 struct vme_master_resource *image;
582 if (resource->type != VME_MASTER) {
583 dev_err(bridge->parent, "Not a master resource\n");
584 return -EINVAL;
587 image = list_entry(resource->entry, struct vme_master_resource, list);
589 if (!bridge->master_get) {
590 dev_warn(bridge->parent, "%s not supported\n", __func__);
591 return -EINVAL;
594 return bridge->master_get(image, enabled, vme_base, size, aspace,
595 cycle, dwidth);
597 EXPORT_SYMBOL(vme_master_get);
600 * vme_master_read - Read data from VME space into a buffer.
601 * @resource: Pointer to VME master resource.
602 * @buf: Pointer to buffer where data should be transferred.
603 * @count: Number of bytes to transfer.
604 * @offset: Offset into VME master window at which to start transfer.
606 * Perform read of count bytes of data from location on VME bus which maps into
607 * the VME master window at offset to buf.
609 * Return: Number of bytes read, -EINVAL if resource is not a VME master
610 * resource or read operation is not supported. -EFAULT returned if
611 * invalid offset is provided. Hardware specific errors may also be
612 * returned.
614 ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
615 loff_t offset)
617 struct vme_bridge *bridge = find_bridge(resource);
618 struct vme_master_resource *image;
619 size_t length;
621 if (!bridge->master_read) {
622 dev_warn(bridge->parent,
623 "Reading from resource not supported\n");
624 return -EINVAL;
627 if (resource->type != VME_MASTER) {
628 dev_err(bridge->parent, "Not a master resource\n");
629 return -EINVAL;
632 image = list_entry(resource->entry, struct vme_master_resource, list);
634 length = vme_get_size(resource);
636 if (offset > length) {
637 dev_warn(bridge->parent, "Invalid Offset\n");
638 return -EFAULT;
641 if ((offset + count) > length)
642 count = length - offset;
644 return bridge->master_read(image, buf, count, offset);
646 EXPORT_SYMBOL(vme_master_read);
649 * vme_master_write - Write data out to VME space from a buffer.
650 * @resource: Pointer to VME master resource.
651 * @buf: Pointer to buffer holding data to transfer.
652 * @count: Number of bytes to transfer.
653 * @offset: Offset into VME master window at which to start transfer.
655 * Perform write of count bytes of data from buf to location on VME bus which
656 * maps into the VME master window at offset.
658 * Return: Number of bytes written, -EINVAL if resource is not a VME master
659 * resource or write operation is not supported. -EFAULT returned if
660 * invalid offset is provided. Hardware specific errors may also be
661 * returned.
663 ssize_t vme_master_write(struct vme_resource *resource, void *buf,
664 size_t count, loff_t offset)
666 struct vme_bridge *bridge = find_bridge(resource);
667 struct vme_master_resource *image;
668 size_t length;
670 if (!bridge->master_write) {
671 dev_warn(bridge->parent, "Writing to resource not supported\n");
672 return -EINVAL;
675 if (resource->type != VME_MASTER) {
676 dev_err(bridge->parent, "Not a master resource\n");
677 return -EINVAL;
680 image = list_entry(resource->entry, struct vme_master_resource, list);
682 length = vme_get_size(resource);
684 if (offset > length) {
685 dev_warn(bridge->parent, "Invalid Offset\n");
686 return -EFAULT;
689 if ((offset + count) > length)
690 count = length - offset;
692 return bridge->master_write(image, buf, count, offset);
694 EXPORT_SYMBOL(vme_master_write);
697 * vme_master_rmw - Perform read-modify-write cycle.
698 * @resource: Pointer to VME master resource.
699 * @mask: Bits to be compared and swapped in operation.
700 * @compare: Bits to be compared with data read from offset.
701 * @swap: Bits to be swapped in data read from offset.
702 * @offset: Offset into VME master window at which to perform operation.
704 * Perform read-modify-write cycle on provided location:
705 * - Location on VME bus is read.
706 * - Bits selected by mask are compared with compare.
707 * - Where a selected bit matches that in compare and are selected in swap,
708 * the bit is swapped.
709 * - Result written back to location on VME bus.
711 * Return: Bytes written on success, -EINVAL if resource is not a VME master
712 * resource or RMW operation is not supported. Hardware specific
713 * errors may also be returned.
715 unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
716 unsigned int compare, unsigned int swap, loff_t offset)
718 struct vme_bridge *bridge = find_bridge(resource);
719 struct vme_master_resource *image;
721 if (!bridge->master_rmw) {
722 dev_warn(bridge->parent, "Writing to resource not supported\n");
723 return -EINVAL;
726 if (resource->type != VME_MASTER) {
727 dev_err(bridge->parent, "Not a master resource\n");
728 return -EINVAL;
731 image = list_entry(resource->entry, struct vme_master_resource, list);
733 return bridge->master_rmw(image, mask, compare, swap, offset);
735 EXPORT_SYMBOL(vme_master_rmw);
738 * vme_master_mmap - Mmap region of VME master window.
739 * @resource: Pointer to VME master resource.
740 * @vma: Pointer to definition of user mapping.
742 * Memory map a region of the VME master window into user space.
744 * Return: Zero on success, -EINVAL if resource is not a VME master
745 * resource or -EFAULT if map exceeds window size. Other generic mmap
746 * errors may also be returned.
748 int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
750 struct vme_bridge *bridge = find_bridge(resource);
751 struct vme_master_resource *image;
752 phys_addr_t phys_addr;
753 unsigned long vma_size;
755 if (resource->type != VME_MASTER) {
756 dev_err(bridge->parent, "Not a master resource\n");
757 return -EINVAL;
760 image = list_entry(resource->entry, struct vme_master_resource, list);
761 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
762 vma_size = vma->vm_end - vma->vm_start;
764 if (phys_addr + vma_size > image->bus_resource.end + 1) {
765 dev_err(bridge->parent, "Map size cannot exceed the window size\n");
766 return -EFAULT;
769 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
771 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
773 EXPORT_SYMBOL(vme_master_mmap);
776 * vme_master_free - Free VME master window
777 * @resource: Pointer to VME master resource.
779 * Free the provided master resource so that it may be reallocated.
781 void vme_master_free(struct vme_resource *resource)
783 struct vme_bridge *bridge = find_bridge(resource);
784 struct vme_master_resource *master_image;
786 if (resource->type != VME_MASTER) {
787 dev_err(bridge->parent, "Not a master resource\n");
788 return;
791 master_image = list_entry(resource->entry, struct vme_master_resource,
792 list);
794 /* Unlock image */
795 spin_lock(&master_image->lock);
796 if (master_image->locked == 0)
797 dev_err(bridge->parent, "Image is already free\n");
799 master_image->locked = 0;
800 spin_unlock(&master_image->lock);
802 /* Free up resource memory */
803 kfree(resource);
805 EXPORT_SYMBOL(vme_master_free);
808 * vme_dma_request - Request a DMA controller.
809 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
810 * @route: Required src/destination combination.
812 * Request a VME DMA controller with capability to perform transfers bewteen
813 * requested source/destination combination.
815 * Return: Pointer to VME DMA resource on success, NULL on failure.
817 struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
819 struct vme_bridge *bridge;
820 struct vme_dma_resource *allocated_ctrlr = NULL;
821 struct vme_dma_resource *dma_ctrlr = NULL;
822 struct vme_resource *resource = NULL;
824 /* XXX Not checking resource attributes */
825 dev_err(&vdev->dev, "No VME resource Attribute tests done\n");
827 bridge = vdev->bridge;
828 if (!bridge) {
829 dev_err(&vdev->dev, "Can't find VME bus\n");
830 goto err_bus;
833 /* Loop through DMA resources */
834 list_for_each_entry(dma_ctrlr, &bridge->dma_resources, list) {
835 if (!dma_ctrlr) {
836 dev_err(bridge->parent,
837 "Registered NULL DMA resource\n");
838 continue;
841 /* Find an unlocked and compatible controller */
842 mutex_lock(&dma_ctrlr->mtx);
843 if (((dma_ctrlr->route_attr & route) == route) &&
844 (dma_ctrlr->locked == 0)) {
845 dma_ctrlr->locked = 1;
846 mutex_unlock(&dma_ctrlr->mtx);
847 allocated_ctrlr = dma_ctrlr;
848 break;
850 mutex_unlock(&dma_ctrlr->mtx);
853 /* Check to see if we found a resource */
854 if (!allocated_ctrlr)
855 goto err_ctrlr;
857 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
858 if (!resource)
859 goto err_alloc;
861 resource->type = VME_DMA;
862 resource->entry = &allocated_ctrlr->list;
864 return resource;
866 err_alloc:
867 /* Unlock image */
868 mutex_lock(&dma_ctrlr->mtx);
869 dma_ctrlr->locked = 0;
870 mutex_unlock(&dma_ctrlr->mtx);
871 err_ctrlr:
872 err_bus:
873 return NULL;
875 EXPORT_SYMBOL(vme_dma_request);
878 * vme_new_dma_list - Create new VME DMA list.
879 * @resource: Pointer to VME DMA resource.
881 * Create a new VME DMA list. It is the responsibility of the user to free
882 * the list once it is no longer required with vme_dma_list_free().
884 * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
885 * VME DMA resource.
887 struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
889 struct vme_bridge *bridge = find_bridge(resource);
890 struct vme_dma_list *dma_list;
892 if (resource->type != VME_DMA) {
893 dev_err(bridge->parent, "Not a DMA resource\n");
894 return NULL;
897 dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL);
898 if (!dma_list)
899 return NULL;
901 INIT_LIST_HEAD(&dma_list->entries);
902 dma_list->parent = list_entry(resource->entry,
903 struct vme_dma_resource,
904 list);
905 mutex_init(&dma_list->mtx);
907 return dma_list;
909 EXPORT_SYMBOL(vme_new_dma_list);
912 * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
913 * @pattern: Value to use used as pattern
914 * @type: Type of pattern to be written.
916 * Create VME DMA list attribute for pattern generation. It is the
917 * responsibility of the user to free used attributes using
918 * vme_dma_free_attribute().
920 * Return: Pointer to VME DMA attribute, NULL on failure.
922 struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
924 struct vme_dma_attr *attributes;
925 struct vme_dma_pattern *pattern_attr;
927 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
928 if (!attributes)
929 goto err_attr;
931 pattern_attr = kmalloc(sizeof(*pattern_attr), GFP_KERNEL);
932 if (!pattern_attr)
933 goto err_pat;
935 attributes->type = VME_DMA_PATTERN;
936 attributes->private = (void *)pattern_attr;
938 pattern_attr->pattern = pattern;
939 pattern_attr->type = type;
941 return attributes;
943 err_pat:
944 kfree(attributes);
945 err_attr:
946 return NULL;
948 EXPORT_SYMBOL(vme_dma_pattern_attribute);
951 * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
952 * @address: PCI base address for DMA transfer.
954 * Create VME DMA list attribute pointing to a location on PCI for DMA
955 * transfers. It is the responsibility of the user to free used attributes
956 * using vme_dma_free_attribute().
958 * Return: Pointer to VME DMA attribute, NULL on failure.
960 struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
962 struct vme_dma_attr *attributes;
963 struct vme_dma_pci *pci_attr;
965 /* XXX Run some sanity checks here */
967 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
968 if (!attributes)
969 goto err_attr;
971 pci_attr = kmalloc(sizeof(*pci_attr), GFP_KERNEL);
972 if (!pci_attr)
973 goto err_pci;
975 attributes->type = VME_DMA_PCI;
976 attributes->private = (void *)pci_attr;
978 pci_attr->address = address;
980 return attributes;
982 err_pci:
983 kfree(attributes);
984 err_attr:
985 return NULL;
987 EXPORT_SYMBOL(vme_dma_pci_attribute);
990 * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
991 * @address: VME base address for DMA transfer.
992 * @aspace: VME address space to use for DMA transfer.
993 * @cycle: VME bus cycle to use for DMA transfer.
994 * @dwidth: VME data width to use for DMA transfer.
996 * Create VME DMA list attribute pointing to a location on the VME bus for DMA
997 * transfers. It is the responsibility of the user to free used attributes
998 * using vme_dma_free_attribute().
1000 * Return: Pointer to VME DMA attribute, NULL on failure.
1002 struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
1003 u32 aspace, u32 cycle, u32 dwidth)
1005 struct vme_dma_attr *attributes;
1006 struct vme_dma_vme *vme_attr;
1008 attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
1009 if (!attributes)
1010 goto err_attr;
1012 vme_attr = kmalloc(sizeof(*vme_attr), GFP_KERNEL);
1013 if (!vme_attr)
1014 goto err_vme;
1016 attributes->type = VME_DMA_VME;
1017 attributes->private = (void *)vme_attr;
1019 vme_attr->address = address;
1020 vme_attr->aspace = aspace;
1021 vme_attr->cycle = cycle;
1022 vme_attr->dwidth = dwidth;
1024 return attributes;
1026 err_vme:
1027 kfree(attributes);
1028 err_attr:
1029 return NULL;
1031 EXPORT_SYMBOL(vme_dma_vme_attribute);
1034 * vme_dma_free_attribute - Free DMA list attribute.
1035 * @attributes: Pointer to DMA list attribute.
1037 * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1038 * once vme_dma_list_add() has returned.
1040 void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1042 kfree(attributes->private);
1043 kfree(attributes);
1045 EXPORT_SYMBOL(vme_dma_free_attribute);
1048 * vme_dma_list_add - Add enty to a VME DMA list.
1049 * @list: Pointer to VME list.
1050 * @src: Pointer to DMA list attribute to use as source.
1051 * @dest: Pointer to DMA list attribute to use as destination.
1052 * @count: Number of bytes to transfer.
1054 * Add an entry to the provided VME DMA list. Entry requires pointers to source
1055 * and destination DMA attributes and a count.
1057 * Please note, the attributes supported as source and destinations for
1058 * transfers are hardware dependent.
1060 * Return: Zero on success, -EINVAL if operation is not supported on this
1061 * device or if the link list has already been submitted for execution.
1062 * Hardware specific errors also possible.
1064 int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
1065 struct vme_dma_attr *dest, size_t count)
1067 struct vme_bridge *bridge = list->parent->parent;
1068 int retval;
1070 if (!bridge->dma_list_add) {
1071 dev_warn(bridge->parent,
1072 "Link List DMA generation not supported\n");
1073 return -EINVAL;
1076 if (!mutex_trylock(&list->mtx)) {
1077 dev_err(bridge->parent, "Link List already submitted\n");
1078 return -EINVAL;
1081 retval = bridge->dma_list_add(list, src, dest, count);
1083 mutex_unlock(&list->mtx);
1085 return retval;
1087 EXPORT_SYMBOL(vme_dma_list_add);
1090 * vme_dma_list_exec - Queue a VME DMA list for execution.
1091 * @list: Pointer to VME list.
1093 * Queue the provided VME DMA list for execution. The call will return once the
1094 * list has been executed.
1096 * Return: Zero on success, -EINVAL if operation is not supported on this
1097 * device. Hardware specific errors also possible.
1099 int vme_dma_list_exec(struct vme_dma_list *list)
1101 struct vme_bridge *bridge = list->parent->parent;
1102 int retval;
1104 if (!bridge->dma_list_exec) {
1105 dev_err(bridge->parent,
1106 "Link List DMA execution not supported\n");
1107 return -EINVAL;
1110 mutex_lock(&list->mtx);
1112 retval = bridge->dma_list_exec(list);
1114 mutex_unlock(&list->mtx);
1116 return retval;
1118 EXPORT_SYMBOL(vme_dma_list_exec);
1121 * vme_dma_list_free - Free a VME DMA list.
1122 * @list: Pointer to VME list.
1124 * Free the provided DMA list and all its entries.
1126 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1127 * is still in use. Hardware specific errors also possible.
1129 int vme_dma_list_free(struct vme_dma_list *list)
1131 struct vme_bridge *bridge = list->parent->parent;
1132 int retval;
1134 if (!bridge->dma_list_empty) {
1135 dev_warn(bridge->parent,
1136 "Emptying of Link Lists not supported\n");
1137 return -EINVAL;
1140 if (!mutex_trylock(&list->mtx)) {
1141 dev_err(bridge->parent, "Link List in use\n");
1142 return -EBUSY;
1146 * Empty out all of the entries from the DMA list. We need to go to the
1147 * low level driver as DMA entries are driver specific.
1149 retval = bridge->dma_list_empty(list);
1150 if (retval) {
1151 dev_err(bridge->parent, "Unable to empty link-list entries\n");
1152 mutex_unlock(&list->mtx);
1153 return retval;
1155 mutex_unlock(&list->mtx);
1156 kfree(list);
1158 return retval;
1160 EXPORT_SYMBOL(vme_dma_list_free);
1163 * vme_dma_free - Free a VME DMA resource.
1164 * @resource: Pointer to VME DMA resource.
1166 * Free the provided DMA resource so that it may be reallocated.
1168 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1169 * is still active.
1171 int vme_dma_free(struct vme_resource *resource)
1173 struct vme_bridge *bridge = find_bridge(resource);
1174 struct vme_dma_resource *ctrlr;
1176 if (resource->type != VME_DMA) {
1177 dev_err(bridge->parent, "Not a DMA resource\n");
1178 return -EINVAL;
1181 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1183 if (!mutex_trylock(&ctrlr->mtx)) {
1184 dev_err(bridge->parent, "Resource busy, can't free\n");
1185 return -EBUSY;
1188 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
1189 dev_warn(bridge->parent,
1190 "Resource still processing transfers\n");
1191 mutex_unlock(&ctrlr->mtx);
1192 return -EBUSY;
1195 ctrlr->locked = 0;
1197 mutex_unlock(&ctrlr->mtx);
1199 kfree(resource);
1201 return 0;
1203 EXPORT_SYMBOL(vme_dma_free);
1205 void vme_bus_error_handler(struct vme_bridge *bridge,
1206 unsigned long long address, int am)
1208 struct vme_error_handler *handler;
1209 int handler_triggered = 0;
1210 u32 aspace = vme_get_aspace(am);
1212 list_for_each_entry(handler, &bridge->vme_error_handlers, list) {
1213 if ((aspace == handler->aspace) &&
1214 (address >= handler->start) &&
1215 (address < handler->end)) {
1216 if (!handler->num_errors)
1217 handler->first_error = address;
1218 if (handler->num_errors != UINT_MAX)
1219 handler->num_errors++;
1220 handler_triggered = 1;
1224 if (!handler_triggered)
1225 dev_err(bridge->parent,
1226 "Unhandled VME access error at address 0x%llx\n",
1227 address);
1229 EXPORT_SYMBOL(vme_bus_error_handler);
1231 struct vme_error_handler *vme_register_error_handler(struct vme_bridge *bridge, u32 aspace,
1232 unsigned long long address, size_t len)
1234 struct vme_error_handler *handler;
1236 handler = kmalloc(sizeof(*handler), GFP_ATOMIC);
1237 if (!handler)
1238 return NULL;
1240 handler->aspace = aspace;
1241 handler->start = address;
1242 handler->end = address + len;
1243 handler->num_errors = 0;
1244 handler->first_error = 0;
1245 list_add_tail(&handler->list, &bridge->vme_error_handlers);
1247 return handler;
1249 EXPORT_SYMBOL(vme_register_error_handler);
1251 void vme_unregister_error_handler(struct vme_error_handler *handler)
1253 list_del(&handler->list);
1254 kfree(handler);
1256 EXPORT_SYMBOL(vme_unregister_error_handler);
1258 void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1260 void (*call)(int level, int statid, void *priv_data);
1261 void *priv_data;
1263 call = bridge->irq[level - 1].callback[statid].func;
1264 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1265 if (call)
1266 call(level, statid, priv_data);
1267 else
1268 dev_warn(bridge->parent,
1269 "Spurious VME interrupt, level:%x, vector:%x\n", level,
1270 statid);
1272 EXPORT_SYMBOL(vme_irq_handler);
1275 * vme_irq_request - Request a specific VME interrupt.
1276 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1277 * @level: Interrupt priority being requested.
1278 * @statid: Interrupt vector being requested.
1279 * @callback: Pointer to callback function called when VME interrupt/vector
1280 * received.
1281 * @priv_data: Generic pointer that will be passed to the callback function.
1283 * Request callback to be attached as a handler for VME interrupts with provided
1284 * level and statid.
1286 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1287 * function is not supported, -EBUSY if the level/statid combination is
1288 * already in use. Hardware specific errors also possible.
1290 int vme_irq_request(struct vme_dev *vdev, int level, int statid,
1291 void (*callback)(int, int, void *),
1292 void *priv_data)
1294 struct vme_bridge *bridge;
1296 bridge = vdev->bridge;
1297 if (!bridge) {
1298 dev_err(&vdev->dev, "Can't find VME bus\n");
1299 return -EINVAL;
1302 if ((level < 1) || (level > 7)) {
1303 dev_err(bridge->parent, "Invalid interrupt level\n");
1304 return -EINVAL;
1307 if (!bridge->irq_set) {
1308 dev_err(bridge->parent,
1309 "Configuring interrupts not supported\n");
1310 return -EINVAL;
1313 mutex_lock(&bridge->irq_mtx);
1315 if (bridge->irq[level - 1].callback[statid].func) {
1316 mutex_unlock(&bridge->irq_mtx);
1317 dev_warn(bridge->parent, "VME Interrupt already taken\n");
1318 return -EBUSY;
1321 bridge->irq[level - 1].count++;
1322 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1323 bridge->irq[level - 1].callback[statid].func = callback;
1325 /* Enable IRQ level */
1326 bridge->irq_set(bridge, level, 1, 1);
1328 mutex_unlock(&bridge->irq_mtx);
1330 return 0;
1332 EXPORT_SYMBOL(vme_irq_request);
1335 * vme_irq_free - Free a VME interrupt.
1336 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1337 * @level: Interrupt priority of interrupt being freed.
1338 * @statid: Interrupt vector of interrupt being freed.
1340 * Remove previously attached callback from VME interrupt priority/vector.
1342 void vme_irq_free(struct vme_dev *vdev, int level, int statid)
1344 struct vme_bridge *bridge;
1346 bridge = vdev->bridge;
1347 if (!bridge) {
1348 dev_err(&vdev->dev, "Can't find VME bus\n");
1349 return;
1352 if ((level < 1) || (level > 7)) {
1353 dev_err(bridge->parent, "Invalid interrupt level\n");
1354 return;
1357 if (!bridge->irq_set) {
1358 dev_err(bridge->parent,
1359 "Configuring interrupts not supported\n");
1360 return;
1363 mutex_lock(&bridge->irq_mtx);
1365 bridge->irq[level - 1].count--;
1367 /* Disable IRQ level if no more interrupts attached at this level*/
1368 if (bridge->irq[level - 1].count == 0)
1369 bridge->irq_set(bridge, level, 0, 1);
1371 bridge->irq[level - 1].callback[statid].func = NULL;
1372 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1374 mutex_unlock(&bridge->irq_mtx);
1376 EXPORT_SYMBOL(vme_irq_free);
1379 * vme_irq_generate - Generate VME interrupt.
1380 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1381 * @level: Interrupt priority at which to assert the interrupt.
1382 * @statid: Interrupt vector to associate with the interrupt.
1384 * Generate a VME interrupt of the provided level and with the provided
1385 * statid.
1387 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1388 * function is not supported. Hardware specific errors also possible.
1390 int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
1392 struct vme_bridge *bridge;
1394 bridge = vdev->bridge;
1395 if (!bridge) {
1396 dev_err(&vdev->dev, "Can't find VME bus\n");
1397 return -EINVAL;
1400 if ((level < 1) || (level > 7)) {
1401 dev_warn(bridge->parent, "Invalid interrupt level\n");
1402 return -EINVAL;
1405 if (!bridge->irq_generate) {
1406 dev_warn(bridge->parent,
1407 "Interrupt generation not supported\n");
1408 return -EINVAL;
1411 return bridge->irq_generate(bridge, level, statid);
1413 EXPORT_SYMBOL(vme_irq_generate);
1416 * vme_lm_request - Request a VME location monitor
1417 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1419 * Allocate a location monitor resource to the driver. A location monitor
1420 * allows the driver to monitor accesses to a contiguous number of
1421 * addresses on the VME bus.
1423 * Return: Pointer to a VME resource on success or NULL on failure.
1425 struct vme_resource *vme_lm_request(struct vme_dev *vdev)
1427 struct vme_bridge *bridge;
1428 struct vme_lm_resource *allocated_lm = NULL;
1429 struct vme_lm_resource *lm = NULL;
1430 struct vme_resource *resource = NULL;
1432 bridge = vdev->bridge;
1433 if (!bridge) {
1434 dev_err(&vdev->dev, "Can't find VME bus\n");
1435 goto err_bus;
1438 /* Loop through LM resources */
1439 list_for_each_entry(lm, &bridge->lm_resources, list) {
1440 if (!lm) {
1441 dev_err(bridge->parent,
1442 "Registered NULL Location Monitor resource\n");
1443 continue;
1446 /* Find an unlocked controller */
1447 mutex_lock(&lm->mtx);
1448 if (lm->locked == 0) {
1449 lm->locked = 1;
1450 mutex_unlock(&lm->mtx);
1451 allocated_lm = lm;
1452 break;
1454 mutex_unlock(&lm->mtx);
1457 /* Check to see if we found a resource */
1458 if (!allocated_lm)
1459 goto err_lm;
1461 resource = kmalloc(sizeof(*resource), GFP_KERNEL);
1462 if (!resource)
1463 goto err_alloc;
1465 resource->type = VME_LM;
1466 resource->entry = &allocated_lm->list;
1468 return resource;
1470 err_alloc:
1471 /* Unlock image */
1472 mutex_lock(&lm->mtx);
1473 lm->locked = 0;
1474 mutex_unlock(&lm->mtx);
1475 err_lm:
1476 err_bus:
1477 return NULL;
1479 EXPORT_SYMBOL(vme_lm_request);
1482 * vme_lm_count - Determine number of VME Addresses monitored
1483 * @resource: Pointer to VME location monitor resource.
1485 * The number of contiguous addresses monitored is hardware dependent.
1486 * Return the number of contiguous addresses monitored by the
1487 * location monitor.
1489 * Return: Count of addresses monitored or -EINVAL when provided with an
1490 * invalid location monitor resource.
1492 int vme_lm_count(struct vme_resource *resource)
1494 struct vme_bridge *bridge = find_bridge(resource);
1495 struct vme_lm_resource *lm;
1497 if (resource->type != VME_LM) {
1498 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1499 return -EINVAL;
1502 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1504 return lm->monitors;
1506 EXPORT_SYMBOL(vme_lm_count);
1509 * vme_lm_set - Configure location monitor
1510 * @resource: Pointer to VME location monitor resource.
1511 * @lm_base: Base address to monitor.
1512 * @aspace: VME address space to monitor.
1513 * @cycle: VME bus cycle type to monitor.
1515 * Set the base address, address space and cycle type of accesses to be
1516 * monitored by the location monitor.
1518 * Return: Zero on success, -EINVAL when provided with an invalid location
1519 * monitor resource or function is not supported. Hardware specific
1520 * errors may also be returned.
1522 int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
1523 u32 aspace, u32 cycle)
1525 struct vme_bridge *bridge = find_bridge(resource);
1526 struct vme_lm_resource *lm;
1528 if (resource->type != VME_LM) {
1529 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1530 return -EINVAL;
1533 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1535 if (!bridge->lm_set) {
1536 dev_err(bridge->parent, "%s not supported\n", __func__);
1537 return -EINVAL;
1540 return bridge->lm_set(lm, lm_base, aspace, cycle);
1542 EXPORT_SYMBOL(vme_lm_set);
1545 * vme_lm_get - Retrieve location monitor settings
1546 * @resource: Pointer to VME location monitor resource.
1547 * @lm_base: Pointer used to output the base address monitored.
1548 * @aspace: Pointer used to output the address space monitored.
1549 * @cycle: Pointer used to output the VME bus cycle type monitored.
1551 * Retrieve the base address, address space and cycle type of accesses to
1552 * be monitored by the location monitor.
1554 * Return: Zero on success, -EINVAL when provided with an invalid location
1555 * monitor resource or function is not supported. Hardware specific
1556 * errors may also be returned.
1558 int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
1559 u32 *aspace, u32 *cycle)
1561 struct vme_bridge *bridge = find_bridge(resource);
1562 struct vme_lm_resource *lm;
1564 if (resource->type != VME_LM) {
1565 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1566 return -EINVAL;
1569 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1571 if (!bridge->lm_get) {
1572 dev_err(bridge->parent, "%s not supported\n", __func__);
1573 return -EINVAL;
1576 return bridge->lm_get(lm, lm_base, aspace, cycle);
1578 EXPORT_SYMBOL(vme_lm_get);
1581 * vme_lm_attach - Provide callback for location monitor address
1582 * @resource: Pointer to VME location monitor resource.
1583 * @monitor: Offset to which callback should be attached.
1584 * @callback: Pointer to callback function called when triggered.
1585 * @data: Generic pointer that will be passed to the callback function.
1587 * Attach a callback to the specified offset into the location monitors
1588 * monitored addresses. A generic pointer is provided to allow data to be
1589 * passed to the callback when called.
1591 * Return: Zero on success, -EINVAL when provided with an invalid location
1592 * monitor resource or function is not supported. Hardware specific
1593 * errors may also be returned.
1595 int vme_lm_attach(struct vme_resource *resource, int monitor,
1596 void (*callback)(void *), void *data)
1598 struct vme_bridge *bridge = find_bridge(resource);
1599 struct vme_lm_resource *lm;
1601 if (resource->type != VME_LM) {
1602 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1603 return -EINVAL;
1606 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1608 if (!bridge->lm_attach) {
1609 dev_err(bridge->parent, "%s not supported\n", __func__);
1610 return -EINVAL;
1613 return bridge->lm_attach(lm, monitor, callback, data);
1615 EXPORT_SYMBOL(vme_lm_attach);
1618 * vme_lm_detach - Remove callback for location monitor address
1619 * @resource: Pointer to VME location monitor resource.
1620 * @monitor: Offset to which callback should be removed.
1622 * Remove the callback associated with the specified offset into the
1623 * location monitors monitored addresses.
1625 * Return: Zero on success, -EINVAL when provided with an invalid location
1626 * monitor resource or function is not supported. Hardware specific
1627 * errors may also be returned.
1629 int vme_lm_detach(struct vme_resource *resource, int monitor)
1631 struct vme_bridge *bridge = find_bridge(resource);
1632 struct vme_lm_resource *lm;
1634 if (resource->type != VME_LM) {
1635 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1636 return -EINVAL;
1639 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1641 if (!bridge->lm_detach) {
1642 dev_err(bridge->parent, "%s not supported\n", __func__);
1643 return -EINVAL;
1646 return bridge->lm_detach(lm, monitor);
1648 EXPORT_SYMBOL(vme_lm_detach);
1651 * vme_lm_free - Free allocated VME location monitor
1652 * @resource: Pointer to VME location monitor resource.
1654 * Free allocation of a VME location monitor.
1656 * WARNING: This function currently expects that any callbacks that have
1657 * been attached to the location monitor have been removed.
1659 * Return: Zero on success, -EINVAL when provided with an invalid location
1660 * monitor resource.
1662 void vme_lm_free(struct vme_resource *resource)
1664 struct vme_bridge *bridge = find_bridge(resource);
1665 struct vme_lm_resource *lm;
1667 if (resource->type != VME_LM) {
1668 dev_err(bridge->parent, "Not a Location Monitor resource\n");
1669 return;
1672 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1674 mutex_lock(&lm->mtx);
1676 /* XXX
1677 * Check to see that there aren't any callbacks still attached, if
1678 * there are we should probably be detaching them!
1681 lm->locked = 0;
1683 mutex_unlock(&lm->mtx);
1685 kfree(resource);
1687 EXPORT_SYMBOL(vme_lm_free);
1690 * vme_slot_num - Retrieve slot ID
1691 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1693 * Retrieve the slot ID associated with the provided VME device.
1695 * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1696 * or the function is not supported. Hardware specific errors may also
1697 * be returned.
1699 int vme_slot_num(struct vme_dev *vdev)
1701 struct vme_bridge *bridge;
1703 bridge = vdev->bridge;
1704 if (!bridge) {
1705 dev_err(&vdev->dev, "Can't find VME bus\n");
1706 return -EINVAL;
1709 if (!bridge->slot_get) {
1710 dev_warn(bridge->parent, "%s not supported\n", __func__);
1711 return -EINVAL;
1714 return bridge->slot_get(bridge);
1716 EXPORT_SYMBOL(vme_slot_num);
1719 * vme_bus_num - Retrieve bus number
1720 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1722 * Retrieve the bus enumeration associated with the provided VME device.
1724 * Return: The bus number on success, -EINVAL if VME bridge cannot be
1725 * determined.
1727 int vme_bus_num(struct vme_dev *vdev)
1729 struct vme_bridge *bridge;
1731 bridge = vdev->bridge;
1732 if (!bridge) {
1733 dev_err(&vdev->dev, "Can't find VME bus\n");
1734 return -EINVAL;
1737 return bridge->num;
1739 EXPORT_SYMBOL(vme_bus_num);
1741 /* - Bridge Registration --------------------------------------------------- */
1743 static void vme_dev_release(struct device *dev)
1745 kfree(dev_to_vme_dev(dev));
1748 /* Common bridge initialization */
1749 struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
1751 INIT_LIST_HEAD(&bridge->vme_error_handlers);
1752 INIT_LIST_HEAD(&bridge->master_resources);
1753 INIT_LIST_HEAD(&bridge->slave_resources);
1754 INIT_LIST_HEAD(&bridge->dma_resources);
1755 INIT_LIST_HEAD(&bridge->lm_resources);
1756 mutex_init(&bridge->irq_mtx);
1758 return bridge;
1760 EXPORT_SYMBOL(vme_init_bridge);
1762 int vme_register_bridge(struct vme_bridge *bridge)
1764 int i;
1765 int ret = -1;
1767 mutex_lock(&vme_buses_lock);
1768 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1769 if ((vme_bus_numbers & (1 << i)) == 0) {
1770 vme_bus_numbers |= (1 << i);
1771 bridge->num = i;
1772 INIT_LIST_HEAD(&bridge->devices);
1773 list_add_tail(&bridge->bus_list, &vme_bus_list);
1774 ret = 0;
1775 break;
1778 mutex_unlock(&vme_buses_lock);
1780 return ret;
1782 EXPORT_SYMBOL(vme_register_bridge);
1784 void vme_unregister_bridge(struct vme_bridge *bridge)
1786 struct vme_dev *vdev;
1787 struct vme_dev *tmp;
1789 mutex_lock(&vme_buses_lock);
1790 vme_bus_numbers &= ~(1 << bridge->num);
1791 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1792 list_del(&vdev->drv_list);
1793 list_del(&vdev->bridge_list);
1794 device_unregister(&vdev->dev);
1796 list_del(&bridge->bus_list);
1797 mutex_unlock(&vme_buses_lock);
1799 EXPORT_SYMBOL(vme_unregister_bridge);
1801 /* - Driver Registration --------------------------------------------------- */
1803 static int __vme_register_driver_bus(struct vme_driver *drv,
1804 struct vme_bridge *bridge,
1805 unsigned int ndevs)
1807 int err;
1808 unsigned int i;
1809 struct vme_dev *vdev;
1810 struct vme_dev *tmp;
1812 for (i = 0; i < ndevs; i++) {
1813 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1814 if (!vdev) {
1815 err = -ENOMEM;
1816 goto err_devalloc;
1818 vdev->num = i;
1819 vdev->bridge = bridge;
1820 vdev->dev.platform_data = drv;
1821 vdev->dev.release = vme_dev_release;
1822 vdev->dev.parent = bridge->parent;
1823 vdev->dev.bus = &vme_bus_type;
1824 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1825 vdev->num);
1827 err = device_register(&vdev->dev);
1828 if (err)
1829 goto err_reg;
1831 if (vdev->dev.platform_data) {
1832 list_add_tail(&vdev->drv_list, &drv->devices);
1833 list_add_tail(&vdev->bridge_list, &bridge->devices);
1834 } else {
1835 device_unregister(&vdev->dev);
1838 return 0;
1840 err_reg:
1841 put_device(&vdev->dev);
1842 err_devalloc:
1843 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1844 list_del(&vdev->drv_list);
1845 list_del(&vdev->bridge_list);
1846 device_unregister(&vdev->dev);
1848 return err;
1851 static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1853 struct vme_bridge *bridge;
1854 int err = 0;
1856 mutex_lock(&vme_buses_lock);
1857 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1859 * This cannot cause trouble as we already have vme_buses_lock
1860 * and if the bridge is removed, it will have to go through
1861 * vme_unregister_bridge() to do it (which calls remove() on
1862 * the bridge which in turn tries to acquire vme_buses_lock and
1863 * will have to wait).
1865 err = __vme_register_driver_bus(drv, bridge, ndevs);
1866 if (err)
1867 break;
1869 mutex_unlock(&vme_buses_lock);
1870 return err;
1874 * vme_register_driver - Register a VME driver
1875 * @drv: Pointer to VME driver structure to register.
1876 * @ndevs: Maximum number of devices to allow to be enumerated.
1878 * Register a VME device driver with the VME subsystem.
1880 * Return: Zero on success, error value on registration failure.
1882 int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1884 int err;
1886 drv->driver.name = drv->name;
1887 drv->driver.bus = &vme_bus_type;
1888 INIT_LIST_HEAD(&drv->devices);
1890 err = driver_register(&drv->driver);
1891 if (err)
1892 return err;
1894 err = __vme_register_driver(drv, ndevs);
1895 if (err)
1896 driver_unregister(&drv->driver);
1898 return err;
1900 EXPORT_SYMBOL(vme_register_driver);
1903 * vme_unregister_driver - Unregister a VME driver
1904 * @drv: Pointer to VME driver structure to unregister.
1906 * Unregister a VME device driver from the VME subsystem.
1908 void vme_unregister_driver(struct vme_driver *drv)
1910 struct vme_dev *dev, *dev_tmp;
1912 mutex_lock(&vme_buses_lock);
1913 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1914 list_del(&dev->drv_list);
1915 list_del(&dev->bridge_list);
1916 device_unregister(&dev->dev);
1918 mutex_unlock(&vme_buses_lock);
1920 driver_unregister(&drv->driver);
1922 EXPORT_SYMBOL(vme_unregister_driver);
1924 /* - Bus Registration ------------------------------------------------------ */
1926 static int vme_bus_match(struct device *dev, const struct device_driver *drv)
1928 struct vme_driver *vme_drv;
1930 vme_drv = container_of(drv, struct vme_driver, driver);
1932 if (dev->platform_data == vme_drv) {
1933 struct vme_dev *vdev = dev_to_vme_dev(dev);
1935 if (vme_drv->match && vme_drv->match(vdev))
1936 return 1;
1938 dev->platform_data = NULL;
1940 return 0;
1943 static int vme_bus_probe(struct device *dev)
1945 struct vme_driver *driver;
1946 struct vme_dev *vdev = dev_to_vme_dev(dev);
1948 driver = dev->platform_data;
1949 if (driver->probe)
1950 return driver->probe(vdev);
1952 return -ENODEV;
1955 static void vme_bus_remove(struct device *dev)
1957 struct vme_driver *driver;
1958 struct vme_dev *vdev = dev_to_vme_dev(dev);
1960 driver = dev->platform_data;
1961 if (driver->remove)
1962 driver->remove(vdev);
1965 const struct bus_type vme_bus_type = {
1966 .name = "vme",
1967 .match = vme_bus_match,
1968 .probe = vme_bus_probe,
1969 .remove = vme_bus_remove,
1971 EXPORT_SYMBOL(vme_bus_type);
1973 static int __init vme_init(void)
1975 return bus_register(&vme_bus_type);
1977 subsys_initcall(vme_init);