• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * VME Bridge Framework
3  *
4  * Author: Martyn Welch <martyn.welch@ge.com>
5  * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
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 
16 #include <linux/init.h>
17 #include <linux/export.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>
30 #include <linux/mutex.h>
31 #include <linux/spinlock.h>
32 #include <linux/slab.h>
33 #include <linux/vme.h>
34 
35 #include "vme_bridge.h"
36 
37 /* Bitmask and list of registered buses both protected by common mutex */
38 static unsigned int vme_bus_numbers;
39 static LIST_HEAD(vme_bus_list);
40 static DEFINE_MUTEX(vme_buses_lock);
41 
42 static int __init vme_init(void);
43 
dev_to_vme_dev(struct device * dev)44 static struct vme_dev *dev_to_vme_dev(struct device *dev)
45 {
46 	return container_of(dev, struct vme_dev, dev);
47 }
48 
49 /*
50  * Find the bridge that the resource is associated with.
51  */
find_bridge(struct vme_resource * resource)52 static struct vme_bridge *find_bridge(struct vme_resource *resource)
53 {
54 	/* Get list to search */
55 	switch (resource->type) {
56 	case VME_MASTER:
57 		return list_entry(resource->entry, struct vme_master_resource,
58 			list)->parent;
59 		break;
60 	case VME_SLAVE:
61 		return list_entry(resource->entry, struct vme_slave_resource,
62 			list)->parent;
63 		break;
64 	case VME_DMA:
65 		return list_entry(resource->entry, struct vme_dma_resource,
66 			list)->parent;
67 		break;
68 	case VME_LM:
69 		return list_entry(resource->entry, struct vme_lm_resource,
70 			list)->parent;
71 		break;
72 	default:
73 		printk(KERN_ERR "Unknown resource type\n");
74 		return NULL;
75 		break;
76 	}
77 }
78 
79 /**
80  * vme_free_consistent - Allocate contiguous memory.
81  * @resource: Pointer to VME resource.
82  * @size: Size of allocation required.
83  * @dma: Pointer to variable to store physical address of allocation.
84  *
85  * Allocate a contiguous block of memory for use by the driver. This is used to
86  * create the buffers for the slave windows.
87  *
88  * Return: Virtual address of allocation on success, NULL on failure.
89  */
vme_alloc_consistent(struct vme_resource * resource,size_t size,dma_addr_t * dma)90 void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
91 	dma_addr_t *dma)
92 {
93 	struct vme_bridge *bridge;
94 
95 	if (resource == NULL) {
96 		printk(KERN_ERR "No resource\n");
97 		return NULL;
98 	}
99 
100 	bridge = find_bridge(resource);
101 	if (bridge == NULL) {
102 		printk(KERN_ERR "Can't find bridge\n");
103 		return NULL;
104 	}
105 
106 	if (bridge->parent == NULL) {
107 		printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
108 		return NULL;
109 	}
110 
111 	if (bridge->alloc_consistent == NULL) {
112 		printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
113 		       bridge->name);
114 		return NULL;
115 	}
116 
117 	return bridge->alloc_consistent(bridge->parent, size, dma);
118 }
119 EXPORT_SYMBOL(vme_alloc_consistent);
120 
121 /**
122  * vme_free_consistent - Free previously allocated memory.
123  * @resource: Pointer to VME resource.
124  * @size: Size of allocation to free.
125  * @vaddr: Virtual address of allocation.
126  * @dma: Physical address of allocation.
127  *
128  * Free previously allocated block of contiguous memory.
129  */
vme_free_consistent(struct vme_resource * resource,size_t size,void * vaddr,dma_addr_t dma)130 void vme_free_consistent(struct vme_resource *resource, size_t size,
131 	void *vaddr, dma_addr_t dma)
132 {
133 	struct vme_bridge *bridge;
134 
135 	if (resource == NULL) {
136 		printk(KERN_ERR "No resource\n");
137 		return;
138 	}
139 
140 	bridge = find_bridge(resource);
141 	if (bridge == NULL) {
142 		printk(KERN_ERR "Can't find bridge\n");
143 		return;
144 	}
145 
146 	if (bridge->parent == NULL) {
147 		printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
148 		return;
149 	}
150 
151 	if (bridge->free_consistent == NULL) {
152 		printk(KERN_ERR "free_consistent not supported by bridge %s\n",
153 		       bridge->name);
154 		return;
155 	}
156 
157 	bridge->free_consistent(bridge->parent, size, vaddr, dma);
158 }
159 EXPORT_SYMBOL(vme_free_consistent);
160 
161 /**
162  * vme_get_size - Helper function returning size of a VME window
163  * @resource: Pointer to VME slave or master resource.
164  *
165  * Determine the size of the VME window provided. This is a helper
166  * function, wrappering the call to vme_master_get or vme_slave_get
167  * depending on the type of window resource handed to it.
168  *
169  * Return: Size of the window on success, zero on failure.
170  */
vme_get_size(struct vme_resource * resource)171 size_t vme_get_size(struct vme_resource *resource)
172 {
173 	int enabled, retval;
174 	unsigned long long base, size;
175 	dma_addr_t buf_base;
176 	u32 aspace, cycle, dwidth;
177 
178 	switch (resource->type) {
179 	case VME_MASTER:
180 		retval = vme_master_get(resource, &enabled, &base, &size,
181 			&aspace, &cycle, &dwidth);
182 		if (retval)
183 			return 0;
184 
185 		return size;
186 		break;
187 	case VME_SLAVE:
188 		retval = vme_slave_get(resource, &enabled, &base, &size,
189 			&buf_base, &aspace, &cycle);
190 		if (retval)
191 			return 0;
192 
193 		return size;
194 		break;
195 	case VME_DMA:
196 		return 0;
197 		break;
198 	default:
199 		printk(KERN_ERR "Unknown resource type\n");
200 		return 0;
201 		break;
202 	}
203 }
204 EXPORT_SYMBOL(vme_get_size);
205 
vme_check_window(u32 aspace,unsigned long long vme_base,unsigned long long size)206 int vme_check_window(u32 aspace, unsigned long long vme_base,
207 		     unsigned long long size)
208 {
209 	int retval = 0;
210 
211 	switch (aspace) {
212 	case VME_A16:
213 		if (((vme_base + size) > VME_A16_MAX) ||
214 				(vme_base > VME_A16_MAX))
215 			retval = -EFAULT;
216 		break;
217 	case VME_A24:
218 		if (((vme_base + size) > VME_A24_MAX) ||
219 				(vme_base > VME_A24_MAX))
220 			retval = -EFAULT;
221 		break;
222 	case VME_A32:
223 		if (((vme_base + size) > VME_A32_MAX) ||
224 				(vme_base > VME_A32_MAX))
225 			retval = -EFAULT;
226 		break;
227 	case VME_A64:
228 		if ((size != 0) && (vme_base > U64_MAX + 1 - size))
229 			retval = -EFAULT;
230 		break;
231 	case VME_CRCSR:
232 		if (((vme_base + size) > VME_CRCSR_MAX) ||
233 				(vme_base > VME_CRCSR_MAX))
234 			retval = -EFAULT;
235 		break;
236 	case VME_USER1:
237 	case VME_USER2:
238 	case VME_USER3:
239 	case VME_USER4:
240 		/* User Defined */
241 		break;
242 	default:
243 		printk(KERN_ERR "Invalid address space\n");
244 		retval = -EINVAL;
245 		break;
246 	}
247 
248 	return retval;
249 }
250 EXPORT_SYMBOL(vme_check_window);
251 
vme_get_aspace(int am)252 static u32 vme_get_aspace(int am)
253 {
254 	switch (am) {
255 	case 0x29:
256 	case 0x2D:
257 		return VME_A16;
258 	case 0x38:
259 	case 0x39:
260 	case 0x3A:
261 	case 0x3B:
262 	case 0x3C:
263 	case 0x3D:
264 	case 0x3E:
265 	case 0x3F:
266 		return VME_A24;
267 	case 0x8:
268 	case 0x9:
269 	case 0xA:
270 	case 0xB:
271 	case 0xC:
272 	case 0xD:
273 	case 0xE:
274 	case 0xF:
275 		return VME_A32;
276 	case 0x0:
277 	case 0x1:
278 	case 0x3:
279 		return VME_A64;
280 	}
281 
282 	return 0;
283 }
284 
285 /**
286  * vme_slave_request - Request a VME slave window resource.
287  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
288  * @address: Required VME address space.
289  * @cycle: Required VME data transfer cycle type.
290  *
291  * Request use of a VME window resource capable of being set for the requested
292  * address space and data transfer cycle.
293  *
294  * Return: Pointer to VME resource on success, NULL on failure.
295  */
vme_slave_request(struct vme_dev * vdev,u32 address,u32 cycle)296 struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
297 	u32 cycle)
298 {
299 	struct vme_bridge *bridge;
300 	struct list_head *slave_pos = NULL;
301 	struct vme_slave_resource *allocated_image = NULL;
302 	struct vme_slave_resource *slave_image = NULL;
303 	struct vme_resource *resource = NULL;
304 
305 	bridge = vdev->bridge;
306 	if (bridge == NULL) {
307 		printk(KERN_ERR "Can't find VME bus\n");
308 		goto err_bus;
309 	}
310 
311 	/* Loop through slave resources */
312 	list_for_each(slave_pos, &bridge->slave_resources) {
313 		slave_image = list_entry(slave_pos,
314 			struct vme_slave_resource, list);
315 
316 		if (slave_image == NULL) {
317 			printk(KERN_ERR "Registered NULL Slave resource\n");
318 			continue;
319 		}
320 
321 		/* Find an unlocked and compatible image */
322 		mutex_lock(&slave_image->mtx);
323 		if (((slave_image->address_attr & address) == address) &&
324 			((slave_image->cycle_attr & cycle) == cycle) &&
325 			(slave_image->locked == 0)) {
326 
327 			slave_image->locked = 1;
328 			mutex_unlock(&slave_image->mtx);
329 			allocated_image = slave_image;
330 			break;
331 		}
332 		mutex_unlock(&slave_image->mtx);
333 	}
334 
335 	/* No free image */
336 	if (allocated_image == NULL)
337 		goto err_image;
338 
339 	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
340 	if (resource == NULL) {
341 		printk(KERN_WARNING "Unable to allocate resource structure\n");
342 		goto err_alloc;
343 	}
344 	resource->type = VME_SLAVE;
345 	resource->entry = &allocated_image->list;
346 
347 	return resource;
348 
349 err_alloc:
350 	/* Unlock image */
351 	mutex_lock(&slave_image->mtx);
352 	slave_image->locked = 0;
353 	mutex_unlock(&slave_image->mtx);
354 err_image:
355 err_bus:
356 	return NULL;
357 }
358 EXPORT_SYMBOL(vme_slave_request);
359 
360 /**
361  * vme_slave_set - Set VME slave window configuration.
362  * @resource: Pointer to VME slave resource.
363  * @enabled: State to which the window should be configured.
364  * @vme_base: Base address for the window.
365  * @size: Size of the VME window.
366  * @buf_base: Based address of buffer used to provide VME slave window storage.
367  * @aspace: VME address space for the VME window.
368  * @cycle: VME data transfer cycle type for the VME window.
369  *
370  * Set configuration for provided VME slave window.
371  *
372  * Return: Zero on success, -EINVAL if operation is not supported on this
373  *         device, if an invalid resource has been provided or invalid
374  *         attributes are provided. Hardware specific errors may also be
375  *         returned.
376  */
vme_slave_set(struct vme_resource * resource,int enabled,unsigned long long vme_base,unsigned long long size,dma_addr_t buf_base,u32 aspace,u32 cycle)377 int vme_slave_set(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)
380 {
381 	struct vme_bridge *bridge = find_bridge(resource);
382 	struct vme_slave_resource *image;
383 	int retval;
384 
385 	if (resource->type != VME_SLAVE) {
386 		printk(KERN_ERR "Not a slave resource\n");
387 		return -EINVAL;
388 	}
389 
390 	image = list_entry(resource->entry, struct vme_slave_resource, list);
391 
392 	if (bridge->slave_set == NULL) {
393 		printk(KERN_ERR "Function not supported\n");
394 		return -ENOSYS;
395 	}
396 
397 	if (!(((image->address_attr & aspace) == aspace) &&
398 		((image->cycle_attr & cycle) == cycle))) {
399 		printk(KERN_ERR "Invalid attributes\n");
400 		return -EINVAL;
401 	}
402 
403 	retval = vme_check_window(aspace, vme_base, size);
404 	if (retval)
405 		return retval;
406 
407 	return bridge->slave_set(image, enabled, vme_base, size, buf_base,
408 		aspace, cycle);
409 }
410 EXPORT_SYMBOL(vme_slave_set);
411 
412 /**
413  * vme_slave_get - Retrieve VME slave window configuration.
414  * @resource: Pointer to VME slave resource.
415  * @enabled: Pointer to variable for storing state.
416  * @vme_base: Pointer to variable for storing window base address.
417  * @size: Pointer to variable for storing window size.
418  * @buf_base: Pointer to variable for storing slave buffer base address.
419  * @aspace: Pointer to variable for storing VME address space.
420  * @cycle: Pointer to variable for storing VME data transfer cycle type.
421  *
422  * Return configuration for provided VME slave window.
423  *
424  * Return: Zero on success, -EINVAL if operation is not supported on this
425  *         device or if an invalid resource has been provided.
426  */
vme_slave_get(struct vme_resource * resource,int * enabled,unsigned long long * vme_base,unsigned long long * size,dma_addr_t * buf_base,u32 * aspace,u32 * cycle)427 int vme_slave_get(struct vme_resource *resource, int *enabled,
428 	unsigned long long *vme_base, unsigned long long *size,
429 	dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
430 {
431 	struct vme_bridge *bridge = find_bridge(resource);
432 	struct vme_slave_resource *image;
433 
434 	if (resource->type != VME_SLAVE) {
435 		printk(KERN_ERR "Not a slave resource\n");
436 		return -EINVAL;
437 	}
438 
439 	image = list_entry(resource->entry, struct vme_slave_resource, list);
440 
441 	if (bridge->slave_get == NULL) {
442 		printk(KERN_ERR "vme_slave_get not supported\n");
443 		return -EINVAL;
444 	}
445 
446 	return bridge->slave_get(image, enabled, vme_base, size, buf_base,
447 		aspace, cycle);
448 }
449 EXPORT_SYMBOL(vme_slave_get);
450 
451 /**
452  * vme_slave_free - Free VME slave window
453  * @resource: Pointer to VME slave resource.
454  *
455  * Free the provided slave resource so that it may be reallocated.
456  */
vme_slave_free(struct vme_resource * resource)457 void vme_slave_free(struct vme_resource *resource)
458 {
459 	struct vme_slave_resource *slave_image;
460 
461 	if (resource->type != VME_SLAVE) {
462 		printk(KERN_ERR "Not a slave resource\n");
463 		return;
464 	}
465 
466 	slave_image = list_entry(resource->entry, struct vme_slave_resource,
467 		list);
468 	if (slave_image == NULL) {
469 		printk(KERN_ERR "Can't find slave resource\n");
470 		return;
471 	}
472 
473 	/* Unlock image */
474 	mutex_lock(&slave_image->mtx);
475 	if (slave_image->locked == 0)
476 		printk(KERN_ERR "Image is already free\n");
477 
478 	slave_image->locked = 0;
479 	mutex_unlock(&slave_image->mtx);
480 
481 	/* Free up resource memory */
482 	kfree(resource);
483 }
484 EXPORT_SYMBOL(vme_slave_free);
485 
486 /**
487  * vme_master_request - Request a VME master window resource.
488  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
489  * @address: Required VME address space.
490  * @cycle: Required VME data transfer cycle type.
491  * @dwidth: Required VME data transfer width.
492  *
493  * Request use of a VME window resource capable of being set for the requested
494  * address space, data transfer cycle and width.
495  *
496  * Return: Pointer to VME resource on success, NULL on failure.
497  */
vme_master_request(struct vme_dev * vdev,u32 address,u32 cycle,u32 dwidth)498 struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
499 	u32 cycle, u32 dwidth)
500 {
501 	struct vme_bridge *bridge;
502 	struct list_head *master_pos = NULL;
503 	struct vme_master_resource *allocated_image = NULL;
504 	struct vme_master_resource *master_image = NULL;
505 	struct vme_resource *resource = NULL;
506 
507 	bridge = vdev->bridge;
508 	if (bridge == NULL) {
509 		printk(KERN_ERR "Can't find VME bus\n");
510 		goto err_bus;
511 	}
512 
513 	/* Loop through master resources */
514 	list_for_each(master_pos, &bridge->master_resources) {
515 		master_image = list_entry(master_pos,
516 			struct vme_master_resource, list);
517 
518 		if (master_image == NULL) {
519 			printk(KERN_WARNING "Registered NULL master resource\n");
520 			continue;
521 		}
522 
523 		/* Find an unlocked and compatible image */
524 		spin_lock(&master_image->lock);
525 		if (((master_image->address_attr & address) == address) &&
526 			((master_image->cycle_attr & cycle) == cycle) &&
527 			((master_image->width_attr & dwidth) == dwidth) &&
528 			(master_image->locked == 0)) {
529 
530 			master_image->locked = 1;
531 			spin_unlock(&master_image->lock);
532 			allocated_image = master_image;
533 			break;
534 		}
535 		spin_unlock(&master_image->lock);
536 	}
537 
538 	/* Check to see if we found a resource */
539 	if (allocated_image == NULL) {
540 		printk(KERN_ERR "Can't find a suitable resource\n");
541 		goto err_image;
542 	}
543 
544 	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
545 	if (resource == NULL) {
546 		printk(KERN_ERR "Unable to allocate resource structure\n");
547 		goto err_alloc;
548 	}
549 	resource->type = VME_MASTER;
550 	resource->entry = &allocated_image->list;
551 
552 	return resource;
553 
554 err_alloc:
555 	/* Unlock image */
556 	spin_lock(&master_image->lock);
557 	master_image->locked = 0;
558 	spin_unlock(&master_image->lock);
559 err_image:
560 err_bus:
561 	return NULL;
562 }
563 EXPORT_SYMBOL(vme_master_request);
564 
565 /**
566  * vme_master_set - Set VME master window configuration.
567  * @resource: Pointer to VME master resource.
568  * @enabled: State to which the window should be configured.
569  * @vme_base: Base address for the window.
570  * @size: Size of the VME window.
571  * @aspace: VME address space for the VME window.
572  * @cycle: VME data transfer cycle type for the VME window.
573  * @dwidth: VME data transfer width for the VME window.
574  *
575  * Set configuration for provided VME master window.
576  *
577  * Return: Zero on success, -EINVAL if operation is not supported on this
578  *         device, if an invalid resource has been provided or invalid
579  *         attributes are provided. Hardware specific errors may also be
580  *         returned.
581  */
vme_master_set(struct vme_resource * resource,int enabled,unsigned long long vme_base,unsigned long long size,u32 aspace,u32 cycle,u32 dwidth)582 int vme_master_set(struct vme_resource *resource, int enabled,
583 	unsigned long long vme_base, unsigned long long size, u32 aspace,
584 	u32 cycle, u32 dwidth)
585 {
586 	struct vme_bridge *bridge = find_bridge(resource);
587 	struct vme_master_resource *image;
588 	int retval;
589 
590 	if (resource->type != VME_MASTER) {
591 		printk(KERN_ERR "Not a master resource\n");
592 		return -EINVAL;
593 	}
594 
595 	image = list_entry(resource->entry, struct vme_master_resource, list);
596 
597 	if (bridge->master_set == NULL) {
598 		printk(KERN_WARNING "vme_master_set not supported\n");
599 		return -EINVAL;
600 	}
601 
602 	if (!(((image->address_attr & aspace) == aspace) &&
603 		((image->cycle_attr & cycle) == cycle) &&
604 		((image->width_attr & dwidth) == dwidth))) {
605 		printk(KERN_WARNING "Invalid attributes\n");
606 		return -EINVAL;
607 	}
608 
609 	retval = vme_check_window(aspace, vme_base, size);
610 	if (retval)
611 		return retval;
612 
613 	return bridge->master_set(image, enabled, vme_base, size, aspace,
614 		cycle, dwidth);
615 }
616 EXPORT_SYMBOL(vme_master_set);
617 
618 /**
619  * vme_master_get - Retrieve VME master window configuration.
620  * @resource: Pointer to VME master resource.
621  * @enabled: Pointer to variable for storing state.
622  * @vme_base: Pointer to variable for storing window base address.
623  * @size: Pointer to variable for storing window size.
624  * @aspace: Pointer to variable for storing VME address space.
625  * @cycle: Pointer to variable for storing VME data transfer cycle type.
626  * @dwidth: Pointer to variable for storing VME data transfer width.
627  *
628  * Return configuration for provided VME master window.
629  *
630  * Return: Zero on success, -EINVAL if operation is not supported on this
631  *         device or if an invalid resource has been provided.
632  */
vme_master_get(struct vme_resource * resource,int * enabled,unsigned long long * vme_base,unsigned long long * size,u32 * aspace,u32 * cycle,u32 * dwidth)633 int vme_master_get(struct vme_resource *resource, int *enabled,
634 	unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
635 	u32 *cycle, u32 *dwidth)
636 {
637 	struct vme_bridge *bridge = find_bridge(resource);
638 	struct vme_master_resource *image;
639 
640 	if (resource->type != VME_MASTER) {
641 		printk(KERN_ERR "Not a master resource\n");
642 		return -EINVAL;
643 	}
644 
645 	image = list_entry(resource->entry, struct vme_master_resource, list);
646 
647 	if (bridge->master_get == NULL) {
648 		printk(KERN_WARNING "%s not supported\n", __func__);
649 		return -EINVAL;
650 	}
651 
652 	return bridge->master_get(image, enabled, vme_base, size, aspace,
653 		cycle, dwidth);
654 }
655 EXPORT_SYMBOL(vme_master_get);
656 
657 /**
658  * vme_master_write - Read data from VME space into a buffer.
659  * @resource: Pointer to VME master resource.
660  * @buf: Pointer to buffer where data should be transferred.
661  * @count: Number of bytes to transfer.
662  * @offset: Offset into VME master window at which to start transfer.
663  *
664  * Perform read of count bytes of data from location on VME bus which maps into
665  * the VME master window at offset to buf.
666  *
667  * Return: Number of bytes read, -EINVAL if resource is not a VME master
668  *         resource or read operation is not supported. -EFAULT returned if
669  *         invalid offset is provided. Hardware specific errors may also be
670  *         returned.
671  */
vme_master_read(struct vme_resource * resource,void * buf,size_t count,loff_t offset)672 ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
673 	loff_t offset)
674 {
675 	struct vme_bridge *bridge = find_bridge(resource);
676 	struct vme_master_resource *image;
677 	size_t length;
678 
679 	if (bridge->master_read == NULL) {
680 		printk(KERN_WARNING "Reading from resource not supported\n");
681 		return -EINVAL;
682 	}
683 
684 	if (resource->type != VME_MASTER) {
685 		printk(KERN_ERR "Not a master resource\n");
686 		return -EINVAL;
687 	}
688 
689 	image = list_entry(resource->entry, struct vme_master_resource, list);
690 
691 	length = vme_get_size(resource);
692 
693 	if (offset > length) {
694 		printk(KERN_WARNING "Invalid Offset\n");
695 		return -EFAULT;
696 	}
697 
698 	if ((offset + count) > length)
699 		count = length - offset;
700 
701 	return bridge->master_read(image, buf, count, offset);
702 
703 }
704 EXPORT_SYMBOL(vme_master_read);
705 
706 /**
707  * vme_master_write - Write data out to VME space from a buffer.
708  * @resource: Pointer to VME master resource.
709  * @buf: Pointer to buffer holding data to transfer.
710  * @count: Number of bytes to transfer.
711  * @offset: Offset into VME master window at which to start transfer.
712  *
713  * Perform write of count bytes of data from buf to location on VME bus which
714  * maps into the VME master window at offset.
715  *
716  * Return: Number of bytes written, -EINVAL if resource is not a VME master
717  *         resource or write operation is not supported. -EFAULT returned if
718  *         invalid offset is provided. Hardware specific errors may also be
719  *         returned.
720  */
vme_master_write(struct vme_resource * resource,void * buf,size_t count,loff_t offset)721 ssize_t vme_master_write(struct vme_resource *resource, void *buf,
722 	size_t count, loff_t offset)
723 {
724 	struct vme_bridge *bridge = find_bridge(resource);
725 	struct vme_master_resource *image;
726 	size_t length;
727 
728 	if (bridge->master_write == NULL) {
729 		printk(KERN_WARNING "Writing to resource not supported\n");
730 		return -EINVAL;
731 	}
732 
733 	if (resource->type != VME_MASTER) {
734 		printk(KERN_ERR "Not a master resource\n");
735 		return -EINVAL;
736 	}
737 
738 	image = list_entry(resource->entry, struct vme_master_resource, list);
739 
740 	length = vme_get_size(resource);
741 
742 	if (offset > length) {
743 		printk(KERN_WARNING "Invalid Offset\n");
744 		return -EFAULT;
745 	}
746 
747 	if ((offset + count) > length)
748 		count = length - offset;
749 
750 	return bridge->master_write(image, buf, count, offset);
751 }
752 EXPORT_SYMBOL(vme_master_write);
753 
754 /**
755  * vme_master_rmw - Perform read-modify-write cycle.
756  * @resource: Pointer to VME master resource.
757  * @mask: Bits to be compared and swapped in operation.
758  * @compare: Bits to be compared with data read from offset.
759  * @swap: Bits to be swapped in data read from offset.
760  * @offset: Offset into VME master window at which to perform operation.
761  *
762  * Perform read-modify-write cycle on provided location:
763  * - Location on VME bus is read.
764  * - Bits selected by mask are compared with compare.
765  * - Where a selected bit matches that in compare and are selected in swap,
766  * the bit is swapped.
767  * - Result written back to location on VME bus.
768  *
769  * Return: Bytes written on success, -EINVAL if resource is not a VME master
770  *         resource or RMW operation is not supported. Hardware specific
771  *         errors may also be returned.
772  */
vme_master_rmw(struct vme_resource * resource,unsigned int mask,unsigned int compare,unsigned int swap,loff_t offset)773 unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
774 	unsigned int compare, unsigned int swap, loff_t offset)
775 {
776 	struct vme_bridge *bridge = find_bridge(resource);
777 	struct vme_master_resource *image;
778 
779 	if (bridge->master_rmw == NULL) {
780 		printk(KERN_WARNING "Writing to resource not supported\n");
781 		return -EINVAL;
782 	}
783 
784 	if (resource->type != VME_MASTER) {
785 		printk(KERN_ERR "Not a master resource\n");
786 		return -EINVAL;
787 	}
788 
789 	image = list_entry(resource->entry, struct vme_master_resource, list);
790 
791 	return bridge->master_rmw(image, mask, compare, swap, offset);
792 }
793 EXPORT_SYMBOL(vme_master_rmw);
794 
795 /**
796  * vme_master_mmap - Mmap region of VME master window.
797  * @resource: Pointer to VME master resource.
798  * @vma: Pointer to definition of user mapping.
799  *
800  * Memory map a region of the VME master window into user space.
801  *
802  * Return: Zero on success, -EINVAL if resource is not a VME master
803  *         resource or -EFAULT if map exceeds window size. Other generic mmap
804  *         errors may also be returned.
805  */
vme_master_mmap(struct vme_resource * resource,struct vm_area_struct * vma)806 int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
807 {
808 	struct vme_master_resource *image;
809 	phys_addr_t phys_addr;
810 	unsigned long vma_size;
811 
812 	if (resource->type != VME_MASTER) {
813 		pr_err("Not a master resource\n");
814 		return -EINVAL;
815 	}
816 
817 	image = list_entry(resource->entry, struct vme_master_resource, list);
818 	phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
819 	vma_size = vma->vm_end - vma->vm_start;
820 
821 	if (phys_addr + vma_size > image->bus_resource.end + 1) {
822 		pr_err("Map size cannot exceed the window size\n");
823 		return -EFAULT;
824 	}
825 
826 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
827 
828 	return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
829 }
830 EXPORT_SYMBOL(vme_master_mmap);
831 
832 /**
833  * vme_master_free - Free VME master window
834  * @resource: Pointer to VME master resource.
835  *
836  * Free the provided master resource so that it may be reallocated.
837  */
vme_master_free(struct vme_resource * resource)838 void vme_master_free(struct vme_resource *resource)
839 {
840 	struct vme_master_resource *master_image;
841 
842 	if (resource->type != VME_MASTER) {
843 		printk(KERN_ERR "Not a master resource\n");
844 		return;
845 	}
846 
847 	master_image = list_entry(resource->entry, struct vme_master_resource,
848 		list);
849 	if (master_image == NULL) {
850 		printk(KERN_ERR "Can't find master resource\n");
851 		return;
852 	}
853 
854 	/* Unlock image */
855 	spin_lock(&master_image->lock);
856 	if (master_image->locked == 0)
857 		printk(KERN_ERR "Image is already free\n");
858 
859 	master_image->locked = 0;
860 	spin_unlock(&master_image->lock);
861 
862 	/* Free up resource memory */
863 	kfree(resource);
864 }
865 EXPORT_SYMBOL(vme_master_free);
866 
867 /**
868  * vme_dma_request - Request a DMA controller.
869  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
870  * @route: Required src/destination combination.
871  *
872  * Request a VME DMA controller with capability to perform transfers bewteen
873  * requested source/destination combination.
874  *
875  * Return: Pointer to VME DMA resource on success, NULL on failure.
876  */
vme_dma_request(struct vme_dev * vdev,u32 route)877 struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
878 {
879 	struct vme_bridge *bridge;
880 	struct list_head *dma_pos = NULL;
881 	struct vme_dma_resource *allocated_ctrlr = NULL;
882 	struct vme_dma_resource *dma_ctrlr = NULL;
883 	struct vme_resource *resource = NULL;
884 
885 	/* XXX Not checking resource attributes */
886 	printk(KERN_ERR "No VME resource Attribute tests done\n");
887 
888 	bridge = vdev->bridge;
889 	if (bridge == NULL) {
890 		printk(KERN_ERR "Can't find VME bus\n");
891 		goto err_bus;
892 	}
893 
894 	/* Loop through DMA resources */
895 	list_for_each(dma_pos, &bridge->dma_resources) {
896 		dma_ctrlr = list_entry(dma_pos,
897 			struct vme_dma_resource, list);
898 
899 		if (dma_ctrlr == NULL) {
900 			printk(KERN_ERR "Registered NULL DMA resource\n");
901 			continue;
902 		}
903 
904 		/* Find an unlocked and compatible controller */
905 		mutex_lock(&dma_ctrlr->mtx);
906 		if (((dma_ctrlr->route_attr & route) == route) &&
907 			(dma_ctrlr->locked == 0)) {
908 
909 			dma_ctrlr->locked = 1;
910 			mutex_unlock(&dma_ctrlr->mtx);
911 			allocated_ctrlr = dma_ctrlr;
912 			break;
913 		}
914 		mutex_unlock(&dma_ctrlr->mtx);
915 	}
916 
917 	/* Check to see if we found a resource */
918 	if (allocated_ctrlr == NULL)
919 		goto err_ctrlr;
920 
921 	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
922 	if (resource == NULL) {
923 		printk(KERN_WARNING "Unable to allocate resource structure\n");
924 		goto err_alloc;
925 	}
926 	resource->type = VME_DMA;
927 	resource->entry = &allocated_ctrlr->list;
928 
929 	return resource;
930 
931 err_alloc:
932 	/* Unlock image */
933 	mutex_lock(&dma_ctrlr->mtx);
934 	dma_ctrlr->locked = 0;
935 	mutex_unlock(&dma_ctrlr->mtx);
936 err_ctrlr:
937 err_bus:
938 	return NULL;
939 }
940 EXPORT_SYMBOL(vme_dma_request);
941 
942 /**
943  * vme_new_dma_list - Create new VME DMA list.
944  * @resource: Pointer to VME DMA resource.
945  *
946  * Create a new VME DMA list. It is the responsibility of the user to free
947  * the list once it is no longer required with vme_dma_list_free().
948  *
949  * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
950  *         VME DMA resource.
951  */
vme_new_dma_list(struct vme_resource * resource)952 struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
953 {
954 	struct vme_dma_resource *ctrlr;
955 	struct vme_dma_list *dma_list;
956 
957 	if (resource->type != VME_DMA) {
958 		printk(KERN_ERR "Not a DMA resource\n");
959 		return NULL;
960 	}
961 
962 	ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
963 
964 	dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
965 	if (dma_list == NULL) {
966 		printk(KERN_ERR "Unable to allocate memory for new DMA list\n");
967 		return NULL;
968 	}
969 	INIT_LIST_HEAD(&dma_list->entries);
970 	dma_list->parent = ctrlr;
971 	mutex_init(&dma_list->mtx);
972 
973 	return dma_list;
974 }
975 EXPORT_SYMBOL(vme_new_dma_list);
976 
977 /**
978  * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
979  * @pattern: Value to use used as pattern
980  * @type: Type of pattern to be written.
981  *
982  * Create VME DMA list attribute for pattern generation. It is the
983  * responsibility of the user to free used attributes using
984  * vme_dma_free_attribute().
985  *
986  * Return: Pointer to VME DMA attribute, NULL on failure.
987  */
vme_dma_pattern_attribute(u32 pattern,u32 type)988 struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
989 {
990 	struct vme_dma_attr *attributes;
991 	struct vme_dma_pattern *pattern_attr;
992 
993 	attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
994 	if (attributes == NULL) {
995 		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
996 		goto err_attr;
997 	}
998 
999 	pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
1000 	if (pattern_attr == NULL) {
1001 		printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
1002 		goto err_pat;
1003 	}
1004 
1005 	attributes->type = VME_DMA_PATTERN;
1006 	attributes->private = (void *)pattern_attr;
1007 
1008 	pattern_attr->pattern = pattern;
1009 	pattern_attr->type = type;
1010 
1011 	return attributes;
1012 
1013 err_pat:
1014 	kfree(attributes);
1015 err_attr:
1016 	return NULL;
1017 }
1018 EXPORT_SYMBOL(vme_dma_pattern_attribute);
1019 
1020 /**
1021  * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
1022  * @address: PCI base address for DMA transfer.
1023  *
1024  * Create VME DMA list attribute pointing to a location on PCI for DMA
1025  * transfers. It is the responsibility of the user to free used attributes
1026  * using vme_dma_free_attribute().
1027  *
1028  * Return: Pointer to VME DMA attribute, NULL on failure.
1029  */
vme_dma_pci_attribute(dma_addr_t address)1030 struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
1031 {
1032 	struct vme_dma_attr *attributes;
1033 	struct vme_dma_pci *pci_attr;
1034 
1035 	/* XXX Run some sanity checks here */
1036 
1037 	attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
1038 	if (attributes == NULL) {
1039 		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
1040 		goto err_attr;
1041 	}
1042 
1043 	pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
1044 	if (pci_attr == NULL) {
1045 		printk(KERN_ERR "Unable to allocate memory for PCI attributes\n");
1046 		goto err_pci;
1047 	}
1048 
1049 
1050 
1051 	attributes->type = VME_DMA_PCI;
1052 	attributes->private = (void *)pci_attr;
1053 
1054 	pci_attr->address = address;
1055 
1056 	return attributes;
1057 
1058 err_pci:
1059 	kfree(attributes);
1060 err_attr:
1061 	return NULL;
1062 }
1063 EXPORT_SYMBOL(vme_dma_pci_attribute);
1064 
1065 /**
1066  * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
1067  * @address: VME base address for DMA transfer.
1068  * @aspace: VME address space to use for DMA transfer.
1069  * @cycle: VME bus cycle to use for DMA transfer.
1070  * @dwidth: VME data width to use for DMA transfer.
1071  *
1072  * Create VME DMA list attribute pointing to a location on the VME bus for DMA
1073  * transfers. It is the responsibility of the user to free used attributes
1074  * using vme_dma_free_attribute().
1075  *
1076  * Return: Pointer to VME DMA attribute, NULL on failure.
1077  */
vme_dma_vme_attribute(unsigned long long address,u32 aspace,u32 cycle,u32 dwidth)1078 struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
1079 	u32 aspace, u32 cycle, u32 dwidth)
1080 {
1081 	struct vme_dma_attr *attributes;
1082 	struct vme_dma_vme *vme_attr;
1083 
1084 	attributes = kmalloc(
1085 		sizeof(struct vme_dma_attr), GFP_KERNEL);
1086 	if (attributes == NULL) {
1087 		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
1088 		goto err_attr;
1089 	}
1090 
1091 	vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
1092 	if (vme_attr == NULL) {
1093 		printk(KERN_ERR "Unable to allocate memory for VME attributes\n");
1094 		goto err_vme;
1095 	}
1096 
1097 	attributes->type = VME_DMA_VME;
1098 	attributes->private = (void *)vme_attr;
1099 
1100 	vme_attr->address = address;
1101 	vme_attr->aspace = aspace;
1102 	vme_attr->cycle = cycle;
1103 	vme_attr->dwidth = dwidth;
1104 
1105 	return attributes;
1106 
1107 err_vme:
1108 	kfree(attributes);
1109 err_attr:
1110 	return NULL;
1111 }
1112 EXPORT_SYMBOL(vme_dma_vme_attribute);
1113 
1114 /**
1115  * vme_dma_free_attribute - Free DMA list attribute.
1116  * @attributes: Pointer to DMA list attribute.
1117  *
1118  * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1119  * once vme_dma_list_add() has returned.
1120  */
vme_dma_free_attribute(struct vme_dma_attr * attributes)1121 void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1122 {
1123 	kfree(attributes->private);
1124 	kfree(attributes);
1125 }
1126 EXPORT_SYMBOL(vme_dma_free_attribute);
1127 
1128 /**
1129  * vme_dma_list_add - Add enty to a VME DMA list.
1130  * @list: Pointer to VME list.
1131  * @src: Pointer to DMA list attribute to use as source.
1132  * @dest: Pointer to DMA list attribute to use as destination.
1133  * @count: Number of bytes to transfer.
1134  *
1135  * Add an entry to the provided VME DMA list. Entry requires pointers to source
1136  * and destination DMA attributes and a count.
1137  *
1138  * Please note, the attributes supported as source and destinations for
1139  * transfers are hardware dependent.
1140  *
1141  * Return: Zero on success, -EINVAL if operation is not supported on this
1142  *         device or if the link list has already been submitted for execution.
1143  *         Hardware specific errors also possible.
1144  */
vme_dma_list_add(struct vme_dma_list * list,struct vme_dma_attr * src,struct vme_dma_attr * dest,size_t count)1145 int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
1146 	struct vme_dma_attr *dest, size_t count)
1147 {
1148 	struct vme_bridge *bridge = list->parent->parent;
1149 	int retval;
1150 
1151 	if (bridge->dma_list_add == NULL) {
1152 		printk(KERN_WARNING "Link List DMA generation not supported\n");
1153 		return -EINVAL;
1154 	}
1155 
1156 	if (!mutex_trylock(&list->mtx)) {
1157 		printk(KERN_ERR "Link List already submitted\n");
1158 		return -EINVAL;
1159 	}
1160 
1161 	retval = bridge->dma_list_add(list, src, dest, count);
1162 
1163 	mutex_unlock(&list->mtx);
1164 
1165 	return retval;
1166 }
1167 EXPORT_SYMBOL(vme_dma_list_add);
1168 
1169 /**
1170  * vme_dma_list_exec - Queue a VME DMA list for execution.
1171  * @list: Pointer to VME list.
1172  *
1173  * Queue the provided VME DMA list for execution. The call will return once the
1174  * list has been executed.
1175  *
1176  * Return: Zero on success, -EINVAL if operation is not supported on this
1177  *         device. Hardware specific errors also possible.
1178  */
vme_dma_list_exec(struct vme_dma_list * list)1179 int vme_dma_list_exec(struct vme_dma_list *list)
1180 {
1181 	struct vme_bridge *bridge = list->parent->parent;
1182 	int retval;
1183 
1184 	if (bridge->dma_list_exec == NULL) {
1185 		printk(KERN_ERR "Link List DMA execution not supported\n");
1186 		return -EINVAL;
1187 	}
1188 
1189 	mutex_lock(&list->mtx);
1190 
1191 	retval = bridge->dma_list_exec(list);
1192 
1193 	mutex_unlock(&list->mtx);
1194 
1195 	return retval;
1196 }
1197 EXPORT_SYMBOL(vme_dma_list_exec);
1198 
1199 /**
1200  * vme_dma_list_free - Free a VME DMA list.
1201  * @list: Pointer to VME list.
1202  *
1203  * Free the provided DMA list and all its entries.
1204  *
1205  * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1206  *         is still in use. Hardware specific errors also possible.
1207  */
vme_dma_list_free(struct vme_dma_list * list)1208 int vme_dma_list_free(struct vme_dma_list *list)
1209 {
1210 	struct vme_bridge *bridge = list->parent->parent;
1211 	int retval;
1212 
1213 	if (bridge->dma_list_empty == NULL) {
1214 		printk(KERN_WARNING "Emptying of Link Lists not supported\n");
1215 		return -EINVAL;
1216 	}
1217 
1218 	if (!mutex_trylock(&list->mtx)) {
1219 		printk(KERN_ERR "Link List in use\n");
1220 		return -EINVAL;
1221 	}
1222 
1223 	/*
1224 	 * Empty out all of the entries from the DMA list. We need to go to the
1225 	 * low level driver as DMA entries are driver specific.
1226 	 */
1227 	retval = bridge->dma_list_empty(list);
1228 	if (retval) {
1229 		printk(KERN_ERR "Unable to empty link-list entries\n");
1230 		mutex_unlock(&list->mtx);
1231 		return retval;
1232 	}
1233 	mutex_unlock(&list->mtx);
1234 	kfree(list);
1235 
1236 	return retval;
1237 }
1238 EXPORT_SYMBOL(vme_dma_list_free);
1239 
1240 /**
1241  * vme_dma_free - Free a VME DMA resource.
1242  * @resource: Pointer to VME DMA resource.
1243  *
1244  * Free the provided DMA resource so that it may be reallocated.
1245  *
1246  * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1247  *         is still active.
1248  */
vme_dma_free(struct vme_resource * resource)1249 int vme_dma_free(struct vme_resource *resource)
1250 {
1251 	struct vme_dma_resource *ctrlr;
1252 
1253 	if (resource->type != VME_DMA) {
1254 		printk(KERN_ERR "Not a DMA resource\n");
1255 		return -EINVAL;
1256 	}
1257 
1258 	ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1259 
1260 	if (!mutex_trylock(&ctrlr->mtx)) {
1261 		printk(KERN_ERR "Resource busy, can't free\n");
1262 		return -EBUSY;
1263 	}
1264 
1265 	if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
1266 		printk(KERN_WARNING "Resource still processing transfers\n");
1267 		mutex_unlock(&ctrlr->mtx);
1268 		return -EBUSY;
1269 	}
1270 
1271 	ctrlr->locked = 0;
1272 
1273 	mutex_unlock(&ctrlr->mtx);
1274 
1275 	kfree(resource);
1276 
1277 	return 0;
1278 }
1279 EXPORT_SYMBOL(vme_dma_free);
1280 
vme_bus_error_handler(struct vme_bridge * bridge,unsigned long long address,int am)1281 void vme_bus_error_handler(struct vme_bridge *bridge,
1282 			   unsigned long long address, int am)
1283 {
1284 	struct list_head *handler_pos = NULL;
1285 	struct vme_error_handler *handler;
1286 	int handler_triggered = 0;
1287 	u32 aspace = vme_get_aspace(am);
1288 
1289 	list_for_each(handler_pos, &bridge->vme_error_handlers) {
1290 		handler = list_entry(handler_pos, struct vme_error_handler,
1291 				     list);
1292 		if ((aspace == handler->aspace) &&
1293 		    (address >= handler->start) &&
1294 		    (address < handler->end)) {
1295 			if (!handler->num_errors)
1296 				handler->first_error = address;
1297 			if (handler->num_errors != UINT_MAX)
1298 				handler->num_errors++;
1299 			handler_triggered = 1;
1300 		}
1301 	}
1302 
1303 	if (!handler_triggered)
1304 		dev_err(bridge->parent,
1305 			"Unhandled VME access error at address 0x%llx\n",
1306 			address);
1307 }
1308 EXPORT_SYMBOL(vme_bus_error_handler);
1309 
vme_register_error_handler(struct vme_bridge * bridge,u32 aspace,unsigned long long address,size_t len)1310 struct vme_error_handler *vme_register_error_handler(
1311 	struct vme_bridge *bridge, u32 aspace,
1312 	unsigned long long address, size_t len)
1313 {
1314 	struct vme_error_handler *handler;
1315 
1316 	handler = kmalloc(sizeof(*handler), GFP_KERNEL);
1317 	if (!handler)
1318 		return NULL;
1319 
1320 	handler->aspace = aspace;
1321 	handler->start = address;
1322 	handler->end = address + len;
1323 	handler->num_errors = 0;
1324 	handler->first_error = 0;
1325 	list_add_tail(&handler->list, &bridge->vme_error_handlers);
1326 
1327 	return handler;
1328 }
1329 EXPORT_SYMBOL(vme_register_error_handler);
1330 
vme_unregister_error_handler(struct vme_error_handler * handler)1331 void vme_unregister_error_handler(struct vme_error_handler *handler)
1332 {
1333 	list_del(&handler->list);
1334 	kfree(handler);
1335 }
1336 EXPORT_SYMBOL(vme_unregister_error_handler);
1337 
vme_irq_handler(struct vme_bridge * bridge,int level,int statid)1338 void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1339 {
1340 	void (*call)(int, int, void *);
1341 	void *priv_data;
1342 
1343 	call = bridge->irq[level - 1].callback[statid].func;
1344 	priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1345 
1346 	if (call != NULL)
1347 		call(level, statid, priv_data);
1348 	else
1349 		printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
1350 		       level, statid);
1351 }
1352 EXPORT_SYMBOL(vme_irq_handler);
1353 
1354 /**
1355  * vme_irq_request - Request a specific VME interrupt.
1356  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1357  * @level: Interrupt priority being requested.
1358  * @statid: Interrupt vector being requested.
1359  * @callback: Pointer to callback function called when VME interrupt/vector
1360  *            received.
1361  * @priv_data: Generic pointer that will be passed to the callback function.
1362  *
1363  * Request callback to be attached as a handler for VME interrupts with provided
1364  * level and statid.
1365  *
1366  * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1367  *         function is not supported, -EBUSY if the level/statid combination is
1368  *         already in use. Hardware specific errors also possible.
1369  */
vme_irq_request(struct vme_dev * vdev,int level,int statid,void (* callback)(int,int,void *),void * priv_data)1370 int vme_irq_request(struct vme_dev *vdev, int level, int statid,
1371 	void (*callback)(int, int, void *),
1372 	void *priv_data)
1373 {
1374 	struct vme_bridge *bridge;
1375 
1376 	bridge = vdev->bridge;
1377 	if (bridge == NULL) {
1378 		printk(KERN_ERR "Can't find VME bus\n");
1379 		return -EINVAL;
1380 	}
1381 
1382 	if ((level < 1) || (level > 7)) {
1383 		printk(KERN_ERR "Invalid interrupt level\n");
1384 		return -EINVAL;
1385 	}
1386 
1387 	if (bridge->irq_set == NULL) {
1388 		printk(KERN_ERR "Configuring interrupts not supported\n");
1389 		return -EINVAL;
1390 	}
1391 
1392 	mutex_lock(&bridge->irq_mtx);
1393 
1394 	if (bridge->irq[level - 1].callback[statid].func) {
1395 		mutex_unlock(&bridge->irq_mtx);
1396 		printk(KERN_WARNING "VME Interrupt already taken\n");
1397 		return -EBUSY;
1398 	}
1399 
1400 	bridge->irq[level - 1].count++;
1401 	bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1402 	bridge->irq[level - 1].callback[statid].func = callback;
1403 
1404 	/* Enable IRQ level */
1405 	bridge->irq_set(bridge, level, 1, 1);
1406 
1407 	mutex_unlock(&bridge->irq_mtx);
1408 
1409 	return 0;
1410 }
1411 EXPORT_SYMBOL(vme_irq_request);
1412 
1413 /**
1414  * vme_irq_free - Free a VME interrupt.
1415  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1416  * @level: Interrupt priority of interrupt being freed.
1417  * @statid: Interrupt vector of interrupt being freed.
1418  *
1419  * Remove previously attached callback from VME interrupt priority/vector.
1420  */
vme_irq_free(struct vme_dev * vdev,int level,int statid)1421 void vme_irq_free(struct vme_dev *vdev, int level, int statid)
1422 {
1423 	struct vme_bridge *bridge;
1424 
1425 	bridge = vdev->bridge;
1426 	if (bridge == NULL) {
1427 		printk(KERN_ERR "Can't find VME bus\n");
1428 		return;
1429 	}
1430 
1431 	if ((level < 1) || (level > 7)) {
1432 		printk(KERN_ERR "Invalid interrupt level\n");
1433 		return;
1434 	}
1435 
1436 	if (bridge->irq_set == NULL) {
1437 		printk(KERN_ERR "Configuring interrupts not supported\n");
1438 		return;
1439 	}
1440 
1441 	mutex_lock(&bridge->irq_mtx);
1442 
1443 	bridge->irq[level - 1].count--;
1444 
1445 	/* Disable IRQ level if no more interrupts attached at this level*/
1446 	if (bridge->irq[level - 1].count == 0)
1447 		bridge->irq_set(bridge, level, 0, 1);
1448 
1449 	bridge->irq[level - 1].callback[statid].func = NULL;
1450 	bridge->irq[level - 1].callback[statid].priv_data = NULL;
1451 
1452 	mutex_unlock(&bridge->irq_mtx);
1453 }
1454 EXPORT_SYMBOL(vme_irq_free);
1455 
1456 /**
1457  * vme_irq_generate - Generate VME interrupt.
1458  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1459  * @level: Interrupt priority at which to assert the interrupt.
1460  * @statid: Interrupt vector to associate with the interrupt.
1461  *
1462  * Generate a VME interrupt of the provided level and with the provided
1463  * statid.
1464  *
1465  * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1466  *         function is not supported. Hardware specific errors also possible.
1467  */
vme_irq_generate(struct vme_dev * vdev,int level,int statid)1468 int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
1469 {
1470 	struct vme_bridge *bridge;
1471 
1472 	bridge = vdev->bridge;
1473 	if (bridge == NULL) {
1474 		printk(KERN_ERR "Can't find VME bus\n");
1475 		return -EINVAL;
1476 	}
1477 
1478 	if ((level < 1) || (level > 7)) {
1479 		printk(KERN_WARNING "Invalid interrupt level\n");
1480 		return -EINVAL;
1481 	}
1482 
1483 	if (bridge->irq_generate == NULL) {
1484 		printk(KERN_WARNING "Interrupt generation not supported\n");
1485 		return -EINVAL;
1486 	}
1487 
1488 	return bridge->irq_generate(bridge, level, statid);
1489 }
1490 EXPORT_SYMBOL(vme_irq_generate);
1491 
1492 /**
1493  * vme_lm_request - Request a VME location monitor
1494  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1495  *
1496  * Allocate a location monitor resource to the driver. A location monitor
1497  * allows the driver to monitor accesses to a contiguous number of
1498  * addresses on the VME bus.
1499  *
1500  * Return: Pointer to a VME resource on success or NULL on failure.
1501  */
vme_lm_request(struct vme_dev * vdev)1502 struct vme_resource *vme_lm_request(struct vme_dev *vdev)
1503 {
1504 	struct vme_bridge *bridge;
1505 	struct list_head *lm_pos = NULL;
1506 	struct vme_lm_resource *allocated_lm = NULL;
1507 	struct vme_lm_resource *lm = NULL;
1508 	struct vme_resource *resource = NULL;
1509 
1510 	bridge = vdev->bridge;
1511 	if (bridge == NULL) {
1512 		printk(KERN_ERR "Can't find VME bus\n");
1513 		goto err_bus;
1514 	}
1515 
1516 	/* Loop through LM resources */
1517 	list_for_each(lm_pos, &bridge->lm_resources) {
1518 		lm = list_entry(lm_pos,
1519 			struct vme_lm_resource, list);
1520 
1521 		if (lm == NULL) {
1522 			printk(KERN_ERR "Registered NULL Location Monitor resource\n");
1523 			continue;
1524 		}
1525 
1526 		/* Find an unlocked controller */
1527 		mutex_lock(&lm->mtx);
1528 		if (lm->locked == 0) {
1529 			lm->locked = 1;
1530 			mutex_unlock(&lm->mtx);
1531 			allocated_lm = lm;
1532 			break;
1533 		}
1534 		mutex_unlock(&lm->mtx);
1535 	}
1536 
1537 	/* Check to see if we found a resource */
1538 	if (allocated_lm == NULL)
1539 		goto err_lm;
1540 
1541 	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
1542 	if (resource == NULL) {
1543 		printk(KERN_ERR "Unable to allocate resource structure\n");
1544 		goto err_alloc;
1545 	}
1546 	resource->type = VME_LM;
1547 	resource->entry = &allocated_lm->list;
1548 
1549 	return resource;
1550 
1551 err_alloc:
1552 	/* Unlock image */
1553 	mutex_lock(&lm->mtx);
1554 	lm->locked = 0;
1555 	mutex_unlock(&lm->mtx);
1556 err_lm:
1557 err_bus:
1558 	return NULL;
1559 }
1560 EXPORT_SYMBOL(vme_lm_request);
1561 
1562 /**
1563  * vme_lm_count - Determine number of VME Addresses monitored
1564  * @resource: Pointer to VME location monitor resource.
1565  *
1566  * The number of contiguous addresses monitored is hardware dependent.
1567  * Return the number of contiguous addresses monitored by the
1568  * location monitor.
1569  *
1570  * Return: Count of addresses monitored or -EINVAL when provided with an
1571  *	   invalid location monitor resource.
1572  */
vme_lm_count(struct vme_resource * resource)1573 int vme_lm_count(struct vme_resource *resource)
1574 {
1575 	struct vme_lm_resource *lm;
1576 
1577 	if (resource->type != VME_LM) {
1578 		printk(KERN_ERR "Not a Location Monitor resource\n");
1579 		return -EINVAL;
1580 	}
1581 
1582 	lm = list_entry(resource->entry, struct vme_lm_resource, list);
1583 
1584 	return lm->monitors;
1585 }
1586 EXPORT_SYMBOL(vme_lm_count);
1587 
1588 /**
1589  * vme_lm_set - Configure location monitor
1590  * @resource: Pointer to VME location monitor resource.
1591  * @lm_base: Base address to monitor.
1592  * @aspace: VME address space to monitor.
1593  * @cycle: VME bus cycle type to monitor.
1594  *
1595  * Set the base address, address space and cycle type of accesses to be
1596  * monitored by the location monitor.
1597  *
1598  * Return: Zero on success, -EINVAL when provided with an invalid location
1599  *	   monitor resource or function is not supported. Hardware specific
1600  *	   errors may also be returned.
1601  */
vme_lm_set(struct vme_resource * resource,unsigned long long lm_base,u32 aspace,u32 cycle)1602 int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
1603 	u32 aspace, u32 cycle)
1604 {
1605 	struct vme_bridge *bridge = find_bridge(resource);
1606 	struct vme_lm_resource *lm;
1607 
1608 	if (resource->type != VME_LM) {
1609 		printk(KERN_ERR "Not a Location Monitor resource\n");
1610 		return -EINVAL;
1611 	}
1612 
1613 	lm = list_entry(resource->entry, struct vme_lm_resource, list);
1614 
1615 	if (bridge->lm_set == NULL) {
1616 		printk(KERN_ERR "vme_lm_set not supported\n");
1617 		return -EINVAL;
1618 	}
1619 
1620 	return bridge->lm_set(lm, lm_base, aspace, cycle);
1621 }
1622 EXPORT_SYMBOL(vme_lm_set);
1623 
1624 /**
1625  * vme_lm_get - Retrieve location monitor settings
1626  * @resource: Pointer to VME location monitor resource.
1627  * @lm_base: Pointer used to output the base address monitored.
1628  * @aspace: Pointer used to output the address space monitored.
1629  * @cycle: Pointer used to output the VME bus cycle type monitored.
1630  *
1631  * Retrieve the base address, address space and cycle type of accesses to
1632  * be monitored by the location monitor.
1633  *
1634  * Return: Zero on success, -EINVAL when provided with an invalid location
1635  *	   monitor resource or function is not supported. Hardware specific
1636  *	   errors may also be returned.
1637  */
vme_lm_get(struct vme_resource * resource,unsigned long long * lm_base,u32 * aspace,u32 * cycle)1638 int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
1639 	u32 *aspace, u32 *cycle)
1640 {
1641 	struct vme_bridge *bridge = find_bridge(resource);
1642 	struct vme_lm_resource *lm;
1643 
1644 	if (resource->type != VME_LM) {
1645 		printk(KERN_ERR "Not a Location Monitor resource\n");
1646 		return -EINVAL;
1647 	}
1648 
1649 	lm = list_entry(resource->entry, struct vme_lm_resource, list);
1650 
1651 	if (bridge->lm_get == NULL) {
1652 		printk(KERN_ERR "vme_lm_get not supported\n");
1653 		return -EINVAL;
1654 	}
1655 
1656 	return bridge->lm_get(lm, lm_base, aspace, cycle);
1657 }
1658 EXPORT_SYMBOL(vme_lm_get);
1659 
1660 /**
1661  * vme_lm_attach - Provide callback for location monitor address
1662  * @resource: Pointer to VME location monitor resource.
1663  * @monitor: Offset to which callback should be attached.
1664  * @callback: Pointer to callback function called when triggered.
1665  * @data: Generic pointer that will be passed to the callback function.
1666  *
1667  * Attach a callback to the specificed offset into the location monitors
1668  * monitored addresses. A generic pointer is provided to allow data to be
1669  * passed to the callback when called.
1670  *
1671  * Return: Zero on success, -EINVAL when provided with an invalid location
1672  *	   monitor resource or function is not supported. Hardware specific
1673  *	   errors may also be returned.
1674  */
vme_lm_attach(struct vme_resource * resource,int monitor,void (* callback)(void *),void * data)1675 int vme_lm_attach(struct vme_resource *resource, int monitor,
1676 	void (*callback)(void *), void *data)
1677 {
1678 	struct vme_bridge *bridge = find_bridge(resource);
1679 	struct vme_lm_resource *lm;
1680 
1681 	if (resource->type != VME_LM) {
1682 		printk(KERN_ERR "Not a Location Monitor resource\n");
1683 		return -EINVAL;
1684 	}
1685 
1686 	lm = list_entry(resource->entry, struct vme_lm_resource, list);
1687 
1688 	if (bridge->lm_attach == NULL) {
1689 		printk(KERN_ERR "vme_lm_attach not supported\n");
1690 		return -EINVAL;
1691 	}
1692 
1693 	return bridge->lm_attach(lm, monitor, callback, data);
1694 }
1695 EXPORT_SYMBOL(vme_lm_attach);
1696 
1697 /**
1698  * vme_lm_detach - Remove callback for location monitor address
1699  * @resource: Pointer to VME location monitor resource.
1700  * @monitor: Offset to which callback should be removed.
1701  *
1702  * Remove the callback associated with the specificed offset into the
1703  * location monitors monitored addresses.
1704  *
1705  * Return: Zero on success, -EINVAL when provided with an invalid location
1706  *	   monitor resource or function is not supported. Hardware specific
1707  *	   errors may also be returned.
1708  */
vme_lm_detach(struct vme_resource * resource,int monitor)1709 int vme_lm_detach(struct vme_resource *resource, int monitor)
1710 {
1711 	struct vme_bridge *bridge = find_bridge(resource);
1712 	struct vme_lm_resource *lm;
1713 
1714 	if (resource->type != VME_LM) {
1715 		printk(KERN_ERR "Not a Location Monitor resource\n");
1716 		return -EINVAL;
1717 	}
1718 
1719 	lm = list_entry(resource->entry, struct vme_lm_resource, list);
1720 
1721 	if (bridge->lm_detach == NULL) {
1722 		printk(KERN_ERR "vme_lm_detach not supported\n");
1723 		return -EINVAL;
1724 	}
1725 
1726 	return bridge->lm_detach(lm, monitor);
1727 }
1728 EXPORT_SYMBOL(vme_lm_detach);
1729 
1730 /**
1731  * vme_lm_free - Free allocated VME location monitor
1732  * @resource: Pointer to VME location monitor resource.
1733  *
1734  * Free allocation of a VME location monitor.
1735  *
1736  * WARNING: This function currently expects that any callbacks that have
1737  *          been attached to the location monitor have been removed.
1738  *
1739  * Return: Zero on success, -EINVAL when provided with an invalid location
1740  *	   monitor resource.
1741  */
vme_lm_free(struct vme_resource * resource)1742 void vme_lm_free(struct vme_resource *resource)
1743 {
1744 	struct vme_lm_resource *lm;
1745 
1746 	if (resource->type != VME_LM) {
1747 		printk(KERN_ERR "Not a Location Monitor resource\n");
1748 		return;
1749 	}
1750 
1751 	lm = list_entry(resource->entry, struct vme_lm_resource, list);
1752 
1753 	mutex_lock(&lm->mtx);
1754 
1755 	/* XXX
1756 	 * Check to see that there aren't any callbacks still attached, if
1757 	 * there are we should probably be detaching them!
1758 	 */
1759 
1760 	lm->locked = 0;
1761 
1762 	mutex_unlock(&lm->mtx);
1763 
1764 	kfree(resource);
1765 }
1766 EXPORT_SYMBOL(vme_lm_free);
1767 
1768 /**
1769  * vme_slot_num - Retrieve slot ID
1770  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1771  *
1772  * Retrieve the slot ID associated with the provided VME device.
1773  *
1774  * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1775  *         or the function is not supported. Hardware specific errors may also
1776  *         be returned.
1777  */
vme_slot_num(struct vme_dev * vdev)1778 int vme_slot_num(struct vme_dev *vdev)
1779 {
1780 	struct vme_bridge *bridge;
1781 
1782 	bridge = vdev->bridge;
1783 	if (bridge == NULL) {
1784 		printk(KERN_ERR "Can't find VME bus\n");
1785 		return -EINVAL;
1786 	}
1787 
1788 	if (bridge->slot_get == NULL) {
1789 		printk(KERN_WARNING "vme_slot_num not supported\n");
1790 		return -EINVAL;
1791 	}
1792 
1793 	return bridge->slot_get(bridge);
1794 }
1795 EXPORT_SYMBOL(vme_slot_num);
1796 
1797 /**
1798  * vme_bus_num - Retrieve bus number
1799  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1800  *
1801  * Retrieve the bus enumeration associated with the provided VME device.
1802  *
1803  * Return: The bus number on success, -EINVAL if VME bridge cannot be
1804  *         determined.
1805  */
vme_bus_num(struct vme_dev * vdev)1806 int vme_bus_num(struct vme_dev *vdev)
1807 {
1808 	struct vme_bridge *bridge;
1809 
1810 	bridge = vdev->bridge;
1811 	if (bridge == NULL) {
1812 		pr_err("Can't find VME bus\n");
1813 		return -EINVAL;
1814 	}
1815 
1816 	return bridge->num;
1817 }
1818 EXPORT_SYMBOL(vme_bus_num);
1819 
1820 /* - Bridge Registration --------------------------------------------------- */
1821 
vme_dev_release(struct device * dev)1822 static void vme_dev_release(struct device *dev)
1823 {
1824 	kfree(dev_to_vme_dev(dev));
1825 }
1826 
1827 /* Common bridge initialization */
vme_init_bridge(struct vme_bridge * bridge)1828 struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
1829 {
1830 	INIT_LIST_HEAD(&bridge->vme_error_handlers);
1831 	INIT_LIST_HEAD(&bridge->master_resources);
1832 	INIT_LIST_HEAD(&bridge->slave_resources);
1833 	INIT_LIST_HEAD(&bridge->dma_resources);
1834 	INIT_LIST_HEAD(&bridge->lm_resources);
1835 	mutex_init(&bridge->irq_mtx);
1836 
1837 	return bridge;
1838 }
1839 EXPORT_SYMBOL(vme_init_bridge);
1840 
vme_register_bridge(struct vme_bridge * bridge)1841 int vme_register_bridge(struct vme_bridge *bridge)
1842 {
1843 	int i;
1844 	int ret = -1;
1845 
1846 	mutex_lock(&vme_buses_lock);
1847 	for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1848 		if ((vme_bus_numbers & (1 << i)) == 0) {
1849 			vme_bus_numbers |= (1 << i);
1850 			bridge->num = i;
1851 			INIT_LIST_HEAD(&bridge->devices);
1852 			list_add_tail(&bridge->bus_list, &vme_bus_list);
1853 			ret = 0;
1854 			break;
1855 		}
1856 	}
1857 	mutex_unlock(&vme_buses_lock);
1858 
1859 	return ret;
1860 }
1861 EXPORT_SYMBOL(vme_register_bridge);
1862 
vme_unregister_bridge(struct vme_bridge * bridge)1863 void vme_unregister_bridge(struct vme_bridge *bridge)
1864 {
1865 	struct vme_dev *vdev;
1866 	struct vme_dev *tmp;
1867 
1868 	mutex_lock(&vme_buses_lock);
1869 	vme_bus_numbers &= ~(1 << bridge->num);
1870 	list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1871 		list_del(&vdev->drv_list);
1872 		list_del(&vdev->bridge_list);
1873 		device_unregister(&vdev->dev);
1874 	}
1875 	list_del(&bridge->bus_list);
1876 	mutex_unlock(&vme_buses_lock);
1877 }
1878 EXPORT_SYMBOL(vme_unregister_bridge);
1879 
1880 /* - Driver Registration --------------------------------------------------- */
1881 
__vme_register_driver_bus(struct vme_driver * drv,struct vme_bridge * bridge,unsigned int ndevs)1882 static int __vme_register_driver_bus(struct vme_driver *drv,
1883 	struct vme_bridge *bridge, unsigned int ndevs)
1884 {
1885 	int err;
1886 	unsigned int i;
1887 	struct vme_dev *vdev;
1888 	struct vme_dev *tmp;
1889 
1890 	for (i = 0; i < ndevs; i++) {
1891 		vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
1892 		if (!vdev) {
1893 			err = -ENOMEM;
1894 			goto err_devalloc;
1895 		}
1896 		vdev->num = i;
1897 		vdev->bridge = bridge;
1898 		vdev->dev.platform_data = drv;
1899 		vdev->dev.release = vme_dev_release;
1900 		vdev->dev.parent = bridge->parent;
1901 		vdev->dev.bus = &vme_bus_type;
1902 		dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1903 			vdev->num);
1904 
1905 		err = device_register(&vdev->dev);
1906 		if (err)
1907 			goto err_reg;
1908 
1909 		if (vdev->dev.platform_data) {
1910 			list_add_tail(&vdev->drv_list, &drv->devices);
1911 			list_add_tail(&vdev->bridge_list, &bridge->devices);
1912 		} else
1913 			device_unregister(&vdev->dev);
1914 	}
1915 	return 0;
1916 
1917 err_reg:
1918 	put_device(&vdev->dev);
1919 	kfree(vdev);
1920 err_devalloc:
1921 	list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1922 		list_del(&vdev->drv_list);
1923 		list_del(&vdev->bridge_list);
1924 		device_unregister(&vdev->dev);
1925 	}
1926 	return err;
1927 }
1928 
__vme_register_driver(struct vme_driver * drv,unsigned int ndevs)1929 static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1930 {
1931 	struct vme_bridge *bridge;
1932 	int err = 0;
1933 
1934 	mutex_lock(&vme_buses_lock);
1935 	list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1936 		/*
1937 		 * This cannot cause trouble as we already have vme_buses_lock
1938 		 * and if the bridge is removed, it will have to go through
1939 		 * vme_unregister_bridge() to do it (which calls remove() on
1940 		 * the bridge which in turn tries to acquire vme_buses_lock and
1941 		 * will have to wait).
1942 		 */
1943 		err = __vme_register_driver_bus(drv, bridge, ndevs);
1944 		if (err)
1945 			break;
1946 	}
1947 	mutex_unlock(&vme_buses_lock);
1948 	return err;
1949 }
1950 
1951 /**
1952  * vme_register_driver - Register a VME driver
1953  * @drv: Pointer to VME driver structure to register.
1954  * @ndevs: Maximum number of devices to allow to be enumerated.
1955  *
1956  * Register a VME device driver with the VME subsystem.
1957  *
1958  * Return: Zero on success, error value on registration failure.
1959  */
vme_register_driver(struct vme_driver * drv,unsigned int ndevs)1960 int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1961 {
1962 	int err;
1963 
1964 	drv->driver.name = drv->name;
1965 	drv->driver.bus = &vme_bus_type;
1966 	INIT_LIST_HEAD(&drv->devices);
1967 
1968 	err = driver_register(&drv->driver);
1969 	if (err)
1970 		return err;
1971 
1972 	err = __vme_register_driver(drv, ndevs);
1973 	if (err)
1974 		driver_unregister(&drv->driver);
1975 
1976 	return err;
1977 }
1978 EXPORT_SYMBOL(vme_register_driver);
1979 
1980 /**
1981  * vme_unregister_driver - Unregister a VME driver
1982  * @drv: Pointer to VME driver structure to unregister.
1983  *
1984  * Unregister a VME device driver from the VME subsystem.
1985  */
vme_unregister_driver(struct vme_driver * drv)1986 void vme_unregister_driver(struct vme_driver *drv)
1987 {
1988 	struct vme_dev *dev, *dev_tmp;
1989 
1990 	mutex_lock(&vme_buses_lock);
1991 	list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1992 		list_del(&dev->drv_list);
1993 		list_del(&dev->bridge_list);
1994 		device_unregister(&dev->dev);
1995 	}
1996 	mutex_unlock(&vme_buses_lock);
1997 
1998 	driver_unregister(&drv->driver);
1999 }
2000 EXPORT_SYMBOL(vme_unregister_driver);
2001 
2002 /* - Bus Registration ------------------------------------------------------ */
2003 
vme_bus_match(struct device * dev,struct device_driver * drv)2004 static int vme_bus_match(struct device *dev, struct device_driver *drv)
2005 {
2006 	struct vme_driver *vme_drv;
2007 
2008 	vme_drv = container_of(drv, struct vme_driver, driver);
2009 
2010 	if (dev->platform_data == vme_drv) {
2011 		struct vme_dev *vdev = dev_to_vme_dev(dev);
2012 
2013 		if (vme_drv->match && vme_drv->match(vdev))
2014 			return 1;
2015 
2016 		dev->platform_data = NULL;
2017 	}
2018 	return 0;
2019 }
2020 
vme_bus_probe(struct device * dev)2021 static int vme_bus_probe(struct device *dev)
2022 {
2023 	int retval = -ENODEV;
2024 	struct vme_driver *driver;
2025 	struct vme_dev *vdev = dev_to_vme_dev(dev);
2026 
2027 	driver = dev->platform_data;
2028 
2029 	if (driver->probe != NULL)
2030 		retval = driver->probe(vdev);
2031 
2032 	return retval;
2033 }
2034 
vme_bus_remove(struct device * dev)2035 static int vme_bus_remove(struct device *dev)
2036 {
2037 	int retval = -ENODEV;
2038 	struct vme_driver *driver;
2039 	struct vme_dev *vdev = dev_to_vme_dev(dev);
2040 
2041 	driver = dev->platform_data;
2042 
2043 	if (driver->remove != NULL)
2044 		retval = driver->remove(vdev);
2045 
2046 	return retval;
2047 }
2048 
2049 struct bus_type vme_bus_type = {
2050 	.name = "vme",
2051 	.match = vme_bus_match,
2052 	.probe = vme_bus_probe,
2053 	.remove = vme_bus_remove,
2054 };
2055 EXPORT_SYMBOL(vme_bus_type);
2056 
vme_init(void)2057 static int __init vme_init(void)
2058 {
2059 	return bus_register(&vme_bus_type);
2060 }
2061 subsys_initcall(vme_init);
2062