vme.c 37.6 KB
Newer Older
1 2 3
/*
 * VME Bridge Framework
 *
4 5
 * Author: Martyn Welch <martyn.welch@ge.com>
 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Based on work by Tom Armistead and Ajit Prem
 * Copyright 2004 Motorola Inc.
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mm.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/pci.h>
#include <linux/poll.h>
#include <linux/highmem.h>
#include <linux/interrupt.h>
#include <linux/pagemap.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#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 36

#include "vme_bridge.h"

37
/* Bitmask and list of registered buses both protected by common mutex */
38
static unsigned int vme_bus_numbers;
39 40
static LIST_HEAD(vme_bus_list);
static DEFINE_MUTEX(vme_buses_lock);
41

42 43
static void __exit vme_exit(void);
static int __init vme_init(void);
44

45
static struct vme_dev *dev_to_vme_dev(struct device *dev)
46
{
47
	return container_of(dev, struct vme_dev, dev);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
}

/*
 * Find the bridge that the resource is associated with.
 */
static struct vme_bridge *find_bridge(struct vme_resource *resource)
{
	/* Get list to search */
	switch (resource->type) {
	case VME_MASTER:
		return list_entry(resource->entry, struct vme_master_resource,
			list)->parent;
		break;
	case VME_SLAVE:
		return list_entry(resource->entry, struct vme_slave_resource,
			list)->parent;
		break;
	case VME_DMA:
		return list_entry(resource->entry, struct vme_dma_resource,
			list)->parent;
		break;
69 70 71 72
	case VME_LM:
		return list_entry(resource->entry, struct vme_lm_resource,
			list)->parent;
		break;
73 74 75 76 77 78 79 80 81 82 83
	default:
		printk(KERN_ERR "Unknown resource type\n");
		return NULL;
		break;
	}
}

/*
 * Allocate a contiguous block of memory for use by the driver. This is used to
 * create the buffers for the slave windows.
 */
84
void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
85 86 87 88
	dma_addr_t *dma)
{
	struct vme_bridge *bridge;

89 90
	if (resource == NULL) {
		printk(KERN_ERR "No resource\n");
91 92 93 94
		return NULL;
	}

	bridge = find_bridge(resource);
95 96
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find bridge\n");
97 98 99 100
		return NULL;
	}

	if (bridge->parent == NULL) {
101
		printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
102 103 104 105
		return NULL;
	}

	if (bridge->alloc_consistent == NULL) {
106 107
		printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
		       bridge->name);
108 109 110
		return NULL;
	}

111
	return bridge->alloc_consistent(bridge->parent, size, dma);
112 113 114 115 116 117 118 119 120 121 122
}
EXPORT_SYMBOL(vme_alloc_consistent);

/*
 * Free previously allocated contiguous block of memory.
 */
void vme_free_consistent(struct vme_resource *resource, size_t size,
	void *vaddr, dma_addr_t dma)
{
	struct vme_bridge *bridge;

123 124
	if (resource == NULL) {
		printk(KERN_ERR "No resource\n");
125 126 127 128
		return;
	}

	bridge = find_bridge(resource);
129 130
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find bridge\n");
131 132 133
		return;
	}

134
	if (bridge->parent == NULL) {
135
		printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
136 137 138 139
		return;
	}

	if (bridge->free_consistent == NULL) {
140 141
		printk(KERN_ERR "free_consistent not supported by bridge %s\n",
		       bridge->name);
142 143
		return;
	}
144

145
	bridge->free_consistent(bridge->parent, size, vaddr, dma);
146 147 148 149 150 151 152 153
}
EXPORT_SYMBOL(vme_free_consistent);

size_t vme_get_size(struct vme_resource *resource)
{
	int enabled, retval;
	unsigned long long base, size;
	dma_addr_t buf_base;
M
Martyn Welch 已提交
154
	u32 aspace, cycle, dwidth;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

	switch (resource->type) {
	case VME_MASTER:
		retval = vme_master_get(resource, &enabled, &base, &size,
			&aspace, &cycle, &dwidth);

		return size;
		break;
	case VME_SLAVE:
		retval = vme_slave_get(resource, &enabled, &base, &size,
			&buf_base, &aspace, &cycle);

		return size;
		break;
	case VME_DMA:
		return 0;
		break;
	default:
		printk(KERN_ERR "Unknown resource type\n");
		return 0;
		break;
	}
}
EXPORT_SYMBOL(vme_get_size);

D
Dmitry Kalinkin 已提交
180 181
int vme_check_window(u32 aspace, unsigned long long vme_base,
		     unsigned long long size)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
{
	int retval = 0;

	switch (aspace) {
	case VME_A16:
		if (((vme_base + size) > VME_A16_MAX) ||
				(vme_base > VME_A16_MAX))
			retval = -EFAULT;
		break;
	case VME_A24:
		if (((vme_base + size) > VME_A24_MAX) ||
				(vme_base > VME_A24_MAX))
			retval = -EFAULT;
		break;
	case VME_A32:
		if (((vme_base + size) > VME_A32_MAX) ||
				(vme_base > VME_A32_MAX))
			retval = -EFAULT;
		break;
	case VME_A64:
202 203
		if ((size != 0) && (vme_base > U64_MAX + 1 - size))
			retval = -EFAULT;
204 205 206 207 208 209 210 211 212 213 214 215 216
		break;
	case VME_CRCSR:
		if (((vme_base + size) > VME_CRCSR_MAX) ||
				(vme_base > VME_CRCSR_MAX))
			retval = -EFAULT;
		break;
	case VME_USER1:
	case VME_USER2:
	case VME_USER3:
	case VME_USER4:
		/* User Defined */
		break;
	default:
217
		printk(KERN_ERR "Invalid address space\n");
218 219 220 221 222 223
		retval = -EINVAL;
		break;
	}

	return retval;
}
D
Dmitry Kalinkin 已提交
224
EXPORT_SYMBOL(vme_check_window);
225 226 227 228 229

/*
 * Request a slave image with specific attributes, return some unique
 * identifier.
 */
M
Martyn Welch 已提交
230 231
struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
	u32 cycle)
232 233 234 235 236 237 238
{
	struct vme_bridge *bridge;
	struct list_head *slave_pos = NULL;
	struct vme_slave_resource *allocated_image = NULL;
	struct vme_slave_resource *slave_image = NULL;
	struct vme_resource *resource = NULL;

239
	bridge = vdev->bridge;
240 241 242 243 244 245
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		goto err_bus;
	}

	/* Loop through slave resources */
246
	list_for_each(slave_pos, &bridge->slave_resources) {
247 248 249 250
		slave_image = list_entry(slave_pos,
			struct vme_slave_resource, list);

		if (slave_image == NULL) {
251
			printk(KERN_ERR "Registered NULL Slave resource\n");
252 253 254 255
			continue;
		}

		/* Find an unlocked and compatible image */
256
		mutex_lock(&slave_image->mtx);
257
		if (((slave_image->address_attr & address) == address) &&
258 259 260 261
			((slave_image->cycle_attr & cycle) == cycle) &&
			(slave_image->locked == 0)) {

			slave_image->locked = 1;
262
			mutex_unlock(&slave_image->mtx);
263 264 265
			allocated_image = slave_image;
			break;
		}
266
		mutex_unlock(&slave_image->mtx);
267 268 269 270 271 272 273 274 275 276 277 278
	}

	/* No free image */
	if (allocated_image == NULL)
		goto err_image;

	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
	if (resource == NULL) {
		printk(KERN_WARNING "Unable to allocate resource structure\n");
		goto err_alloc;
	}
	resource->type = VME_SLAVE;
279
	resource->entry = &allocated_image->list;
280 281 282 283 284

	return resource;

err_alloc:
	/* Unlock image */
285
	mutex_lock(&slave_image->mtx);
286
	slave_image->locked = 0;
287
	mutex_unlock(&slave_image->mtx);
288 289 290 291 292 293
err_image:
err_bus:
	return NULL;
}
EXPORT_SYMBOL(vme_slave_request);

294
int vme_slave_set(struct vme_resource *resource, int enabled,
295
	unsigned long long vme_base, unsigned long long size,
M
Martyn Welch 已提交
296
	dma_addr_t buf_base, u32 aspace, u32 cycle)
297 298 299 300 301 302
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_slave_resource *image;
	int retval;

	if (resource->type != VME_SLAVE) {
303
		printk(KERN_ERR "Not a slave resource\n");
304 305 306 307 308 309
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_slave_resource, list);

	if (bridge->slave_set == NULL) {
310
		printk(KERN_ERR "Function not supported\n");
311 312 313
		return -ENOSYS;
	}

314
	if (!(((image->address_attr & aspace) == aspace) &&
315
		((image->cycle_attr & cycle) == cycle))) {
316
		printk(KERN_ERR "Invalid attributes\n");
317 318 319 320
		return -EINVAL;
	}

	retval = vme_check_window(aspace, vme_base, size);
321
	if (retval)
322 323 324 325 326 327 328
		return retval;

	return bridge->slave_set(image, enabled, vme_base, size, buf_base,
		aspace, cycle);
}
EXPORT_SYMBOL(vme_slave_set);

329
int vme_slave_get(struct vme_resource *resource, int *enabled,
330
	unsigned long long *vme_base, unsigned long long *size,
M
Martyn Welch 已提交
331
	dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
332 333 334 335 336
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_slave_resource *image;

	if (resource->type != VME_SLAVE) {
337
		printk(KERN_ERR "Not a slave resource\n");
338 339 340 341 342
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_slave_resource, list);

343
	if (bridge->slave_get == NULL) {
344
		printk(KERN_ERR "vme_slave_get not supported\n");
345 346 347 348 349 350 351 352 353 354 355 356 357
		return -EINVAL;
	}

	return bridge->slave_get(image, enabled, vme_base, size, buf_base,
		aspace, cycle);
}
EXPORT_SYMBOL(vme_slave_get);

void vme_slave_free(struct vme_resource *resource)
{
	struct vme_slave_resource *slave_image;

	if (resource->type != VME_SLAVE) {
358
		printk(KERN_ERR "Not a slave resource\n");
359 360 361 362 363 364
		return;
	}

	slave_image = list_entry(resource->entry, struct vme_slave_resource,
		list);
	if (slave_image == NULL) {
365
		printk(KERN_ERR "Can't find slave resource\n");
366 367 368 369
		return;
	}

	/* Unlock image */
370
	mutex_lock(&slave_image->mtx);
371 372 373 374
	if (slave_image->locked == 0)
		printk(KERN_ERR "Image is already free\n");

	slave_image->locked = 0;
375
	mutex_unlock(&slave_image->mtx);
376 377 378 379 380 381 382 383 384 385

	/* Free up resource memory */
	kfree(resource);
}
EXPORT_SYMBOL(vme_slave_free);

/*
 * Request a master image with specific attributes, return some unique
 * identifier.
 */
M
Martyn Welch 已提交
386 387
struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
	u32 cycle, u32 dwidth)
388 389 390 391 392 393 394
{
	struct vme_bridge *bridge;
	struct list_head *master_pos = NULL;
	struct vme_master_resource *allocated_image = NULL;
	struct vme_master_resource *master_image = NULL;
	struct vme_resource *resource = NULL;

395
	bridge = vdev->bridge;
396 397 398 399 400 401
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		goto err_bus;
	}

	/* Loop through master resources */
402
	list_for_each(master_pos, &bridge->master_resources) {
403 404 405 406 407 408 409 410 411
		master_image = list_entry(master_pos,
			struct vme_master_resource, list);

		if (master_image == NULL) {
			printk(KERN_WARNING "Registered NULL master resource\n");
			continue;
		}

		/* Find an unlocked and compatible image */
412
		spin_lock(&master_image->lock);
413
		if (((master_image->address_attr & address) == address) &&
414 415 416 417 418
			((master_image->cycle_attr & cycle) == cycle) &&
			((master_image->width_attr & dwidth) == dwidth) &&
			(master_image->locked == 0)) {

			master_image->locked = 1;
419
			spin_unlock(&master_image->lock);
420 421 422
			allocated_image = master_image;
			break;
		}
423
		spin_unlock(&master_image->lock);
424 425 426 427 428 429 430 431 432 433 434 435 436 437
	}

	/* Check to see if we found a resource */
	if (allocated_image == NULL) {
		printk(KERN_ERR "Can't find a suitable resource\n");
		goto err_image;
	}

	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
	if (resource == NULL) {
		printk(KERN_ERR "Unable to allocate resource structure\n");
		goto err_alloc;
	}
	resource->type = VME_MASTER;
438
	resource->entry = &allocated_image->list;
439 440 441 442 443

	return resource;

err_alloc:
	/* Unlock image */
444
	spin_lock(&master_image->lock);
445
	master_image->locked = 0;
446
	spin_unlock(&master_image->lock);
447 448 449 450 451 452
err_image:
err_bus:
	return NULL;
}
EXPORT_SYMBOL(vme_master_request);

453
int vme_master_set(struct vme_resource *resource, int enabled,
M
Martyn Welch 已提交
454 455
	unsigned long long vme_base, unsigned long long size, u32 aspace,
	u32 cycle, u32 dwidth)
456 457 458 459 460 461
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;
	int retval;

	if (resource->type != VME_MASTER) {
462
		printk(KERN_ERR "Not a master resource\n");
463 464 465 466 467 468
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

	if (bridge->master_set == NULL) {
469
		printk(KERN_WARNING "vme_master_set not supported\n");
470 471 472
		return -EINVAL;
	}

473
	if (!(((image->address_attr & aspace) == aspace) &&
474 475
		((image->cycle_attr & cycle) == cycle) &&
		((image->width_attr & dwidth) == dwidth))) {
476
		printk(KERN_WARNING "Invalid attributes\n");
477 478 479 480
		return -EINVAL;
	}

	retval = vme_check_window(aspace, vme_base, size);
481
	if (retval)
482 483 484 485 486 487 488
		return retval;

	return bridge->master_set(image, enabled, vme_base, size, aspace,
		cycle, dwidth);
}
EXPORT_SYMBOL(vme_master_set);

489
int vme_master_get(struct vme_resource *resource, int *enabled,
M
Martyn Welch 已提交
490 491
	unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
	u32 *cycle, u32 *dwidth)
492 493 494 495 496
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;

	if (resource->type != VME_MASTER) {
497
		printk(KERN_ERR "Not a master resource\n");
498 499 500 501 502
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

503
	if (bridge->master_get == NULL) {
504
		printk(KERN_WARNING "%s not supported\n", __func__);
505 506 507 508 509 510 511 512 513 514 515
		return -EINVAL;
	}

	return bridge->master_get(image, enabled, vme_base, size, aspace,
		cycle, dwidth);
}
EXPORT_SYMBOL(vme_master_get);

/*
 * Read data out of VME space into a buffer.
 */
516
ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
517 518 519 520 521 522 523
	loff_t offset)
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;
	size_t length;

	if (bridge->master_read == NULL) {
524
		printk(KERN_WARNING "Reading from resource not supported\n");
525 526 527 528
		return -EINVAL;
	}

	if (resource->type != VME_MASTER) {
529
		printk(KERN_ERR "Not a master resource\n");
530 531 532 533 534 535 536 537
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

	length = vme_get_size(resource);

	if (offset > length) {
538
		printk(KERN_WARNING "Invalid Offset\n");
539 540 541 542 543 544 545 546 547 548 549 550 551 552
		return -EFAULT;
	}

	if ((offset + count) > length)
		count = length - offset;

	return bridge->master_read(image, buf, count, offset);

}
EXPORT_SYMBOL(vme_master_read);

/*
 * Write data out to VME space from a buffer.
 */
553
ssize_t vme_master_write(struct vme_resource *resource, void *buf,
554 555 556 557 558 559 560
	size_t count, loff_t offset)
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;
	size_t length;

	if (bridge->master_write == NULL) {
561
		printk(KERN_WARNING "Writing to resource not supported\n");
562 563 564 565
		return -EINVAL;
	}

	if (resource->type != VME_MASTER) {
566
		printk(KERN_ERR "Not a master resource\n");
567 568 569 570 571 572 573 574
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

	length = vme_get_size(resource);

	if (offset > length) {
575
		printk(KERN_WARNING "Invalid Offset\n");
576 577 578 579 580 581 582 583 584 585 586 587 588
		return -EFAULT;
	}

	if ((offset + count) > length)
		count = length - offset;

	return bridge->master_write(image, buf, count, offset);
}
EXPORT_SYMBOL(vme_master_write);

/*
 * Perform RMW cycle to provided location.
 */
589
unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
590 591 592 593 594 595
	unsigned int compare, unsigned int swap, loff_t offset)
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_master_resource *image;

	if (bridge->master_rmw == NULL) {
596
		printk(KERN_WARNING "Writing to resource not supported\n");
597 598 599 600
		return -EINVAL;
	}

	if (resource->type != VME_MASTER) {
601
		printk(KERN_ERR "Not a master resource\n");
602 603 604 605 606 607 608 609 610
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);

	return bridge->master_rmw(image, mask, compare, swap, offset);
}
EXPORT_SYMBOL(vme_master_rmw);

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
{
	struct vme_master_resource *image;
	phys_addr_t phys_addr;
	unsigned long vma_size;

	if (resource->type != VME_MASTER) {
		pr_err("Not a master resource\n");
		return -EINVAL;
	}

	image = list_entry(resource->entry, struct vme_master_resource, list);
	phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
	vma_size = vma->vm_end - vma->vm_start;

	if (phys_addr + vma_size > image->bus_resource.end + 1) {
		pr_err("Map size cannot exceed the window size\n");
		return -EFAULT;
	}

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

	return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
}
EXPORT_SYMBOL(vme_master_mmap);

637 638 639 640 641
void vme_master_free(struct vme_resource *resource)
{
	struct vme_master_resource *master_image;

	if (resource->type != VME_MASTER) {
642
		printk(KERN_ERR "Not a master resource\n");
643 644 645 646 647 648
		return;
	}

	master_image = list_entry(resource->entry, struct vme_master_resource,
		list);
	if (master_image == NULL) {
649
		printk(KERN_ERR "Can't find master resource\n");
650 651 652 653
		return;
	}

	/* Unlock image */
654
	spin_lock(&master_image->lock);
655 656 657 658
	if (master_image->locked == 0)
		printk(KERN_ERR "Image is already free\n");

	master_image->locked = 0;
659
	spin_unlock(&master_image->lock);
660 661 662 663 664 665 666 667 668 669

	/* Free up resource memory */
	kfree(resource);
}
EXPORT_SYMBOL(vme_master_free);

/*
 * Request a DMA controller with specific attributes, return some unique
 * identifier.
 */
M
Martyn Welch 已提交
670
struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
671 672 673 674 675 676 677 678 679 680
{
	struct vme_bridge *bridge;
	struct list_head *dma_pos = NULL;
	struct vme_dma_resource *allocated_ctrlr = NULL;
	struct vme_dma_resource *dma_ctrlr = NULL;
	struct vme_resource *resource = NULL;

	/* XXX Not checking resource attributes */
	printk(KERN_ERR "No VME resource Attribute tests done\n");

681
	bridge = vdev->bridge;
682 683 684 685 686 687
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		goto err_bus;
	}

	/* Loop through DMA resources */
688
	list_for_each(dma_pos, &bridge->dma_resources) {
689 690 691 692
		dma_ctrlr = list_entry(dma_pos,
			struct vme_dma_resource, list);

		if (dma_ctrlr == NULL) {
693
			printk(KERN_ERR "Registered NULL DMA resource\n");
694 695 696
			continue;
		}

697
		/* Find an unlocked and compatible controller */
698
		mutex_lock(&dma_ctrlr->mtx);
699 700 701
		if (((dma_ctrlr->route_attr & route) == route) &&
			(dma_ctrlr->locked == 0)) {

702
			dma_ctrlr->locked = 1;
703
			mutex_unlock(&dma_ctrlr->mtx);
704 705 706
			allocated_ctrlr = dma_ctrlr;
			break;
		}
707
		mutex_unlock(&dma_ctrlr->mtx);
708 709 710 711 712 713 714 715 716 717 718 719
	}

	/* Check to see if we found a resource */
	if (allocated_ctrlr == NULL)
		goto err_ctrlr;

	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
	if (resource == NULL) {
		printk(KERN_WARNING "Unable to allocate resource structure\n");
		goto err_alloc;
	}
	resource->type = VME_DMA;
720
	resource->entry = &allocated_ctrlr->list;
721 722 723 724 725

	return resource;

err_alloc:
	/* Unlock image */
726
	mutex_lock(&dma_ctrlr->mtx);
727
	dma_ctrlr->locked = 0;
728
	mutex_unlock(&dma_ctrlr->mtx);
729 730 731 732
err_ctrlr:
err_bus:
	return NULL;
}
733
EXPORT_SYMBOL(vme_dma_request);
734 735 736 737 738 739 740 741 742 743

/*
 * Start new list
 */
struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
{
	struct vme_dma_resource *ctrlr;
	struct vme_dma_list *dma_list;

	if (resource->type != VME_DMA) {
744
		printk(KERN_ERR "Not a DMA resource\n");
745 746 747 748 749
		return NULL;
	}

	ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);

750 751 752
	dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
	if (dma_list == NULL) {
		printk(KERN_ERR "Unable to allocate memory for new dma list\n");
753 754
		return NULL;
	}
755
	INIT_LIST_HEAD(&dma_list->entries);
756
	dma_list->parent = ctrlr;
757
	mutex_init(&dma_list->mtx);
758 759 760 761 762 763 764 765

	return dma_list;
}
EXPORT_SYMBOL(vme_new_dma_list);

/*
 * Create "Pattern" type attributes
 */
M
Martyn Welch 已提交
766
struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
767 768 769 770
{
	struct vme_dma_attr *attributes;
	struct vme_dma_pattern *pattern_attr;

771 772
	attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
	if (attributes == NULL) {
773
		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
774 775 776
		goto err_attr;
	}

777 778
	pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
	if (pattern_attr == NULL) {
779
		printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
		goto err_pat;
	}

	attributes->type = VME_DMA_PATTERN;
	attributes->private = (void *)pattern_attr;

	pattern_attr->pattern = pattern;
	pattern_attr->type = type;

	return attributes;

err_pat:
	kfree(attributes);
err_attr:
	return NULL;
}
EXPORT_SYMBOL(vme_dma_pattern_attribute);

/*
 * Create "PCI" type attributes
 */
struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
{
	struct vme_dma_attr *attributes;
	struct vme_dma_pci *pci_attr;

	/* XXX Run some sanity checks here */

808 809
	attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
	if (attributes == NULL) {
810
		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
811 812 813
		goto err_attr;
	}

814 815
	pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
	if (pci_attr == NULL) {
816
		printk(KERN_ERR "Unable to allocate memory for pci attributes\n");
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
		goto err_pci;
	}



	attributes->type = VME_DMA_PCI;
	attributes->private = (void *)pci_attr;

	pci_attr->address = address;

	return attributes;

err_pci:
	kfree(attributes);
err_attr:
	return NULL;
}
EXPORT_SYMBOL(vme_dma_pci_attribute);

/*
 * Create "VME" type attributes
 */
struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
M
Martyn Welch 已提交
840
	u32 aspace, u32 cycle, u32 dwidth)
841 842 843 844
{
	struct vme_dma_attr *attributes;
	struct vme_dma_vme *vme_attr;

845
	attributes = kmalloc(
846
		sizeof(struct vme_dma_attr), GFP_KERNEL);
847
	if (attributes == NULL) {
848
		printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
849 850 851
		goto err_attr;
	}

852 853
	vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
	if (vme_attr == NULL) {
854
		printk(KERN_ERR "Unable to allocate memory for vme attributes\n");
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
		goto err_vme;
	}

	attributes->type = VME_DMA_VME;
	attributes->private = (void *)vme_attr;

	vme_attr->address = address;
	vme_attr->aspace = aspace;
	vme_attr->cycle = cycle;
	vme_attr->dwidth = dwidth;

	return attributes;

err_vme:
	kfree(attributes);
err_attr:
	return NULL;
}
EXPORT_SYMBOL(vme_dma_vme_attribute);

/*
 * Free attribute
 */
void vme_dma_free_attribute(struct vme_dma_attr *attributes)
{
	kfree(attributes->private);
	kfree(attributes);
}
EXPORT_SYMBOL(vme_dma_free_attribute);

int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
	struct vme_dma_attr *dest, size_t count)
{
	struct vme_bridge *bridge = list->parent->parent;
	int retval;

	if (bridge->dma_list_add == NULL) {
892
		printk(KERN_WARNING "Link List DMA generation not supported\n");
893 894 895
		return -EINVAL;
	}

896
	if (!mutex_trylock(&list->mtx)) {
897
		printk(KERN_ERR "Link List already submitted\n");
898 899 900 901 902
		return -EINVAL;
	}

	retval = bridge->dma_list_add(list, src, dest, count);

903
	mutex_unlock(&list->mtx);
904 905 906 907 908 909 910 911 912 913 914

	return retval;
}
EXPORT_SYMBOL(vme_dma_list_add);

int vme_dma_list_exec(struct vme_dma_list *list)
{
	struct vme_bridge *bridge = list->parent->parent;
	int retval;

	if (bridge->dma_list_exec == NULL) {
915
		printk(KERN_ERR "Link List DMA execution not supported\n");
916 917 918
		return -EINVAL;
	}

919
	mutex_lock(&list->mtx);
920 921 922

	retval = bridge->dma_list_exec(list);

923
	mutex_unlock(&list->mtx);
924 925 926 927 928 929 930 931 932 933 934

	return retval;
}
EXPORT_SYMBOL(vme_dma_list_exec);

int vme_dma_list_free(struct vme_dma_list *list)
{
	struct vme_bridge *bridge = list->parent->parent;
	int retval;

	if (bridge->dma_list_empty == NULL) {
935
		printk(KERN_WARNING "Emptying of Link Lists not supported\n");
936 937 938
		return -EINVAL;
	}

939
	if (!mutex_trylock(&list->mtx)) {
940
		printk(KERN_ERR "Link List in use\n");
941 942 943 944 945 946 947 948 949
		return -EINVAL;
	}

	/*
	 * Empty out all of the entries from the dma list. We need to go to the
	 * low level driver as dma entries are driver specific.
	 */
	retval = bridge->dma_list_empty(list);
	if (retval) {
950
		printk(KERN_ERR "Unable to empty link-list entries\n");
951
		mutex_unlock(&list->mtx);
952 953
		return retval;
	}
954
	mutex_unlock(&list->mtx);
955 956 957 958 959 960 961 962 963 964 965
	kfree(list);

	return retval;
}
EXPORT_SYMBOL(vme_dma_list_free);

int vme_dma_free(struct vme_resource *resource)
{
	struct vme_dma_resource *ctrlr;

	if (resource->type != VME_DMA) {
966
		printk(KERN_ERR "Not a DMA resource\n");
967 968 969 970 971
		return -EINVAL;
	}

	ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);

972
	if (!mutex_trylock(&ctrlr->mtx)) {
973
		printk(KERN_ERR "Resource busy, can't free\n");
974 975 976
		return -EBUSY;
	}

977
	if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
978
		printk(KERN_WARNING "Resource still processing transfers\n");
979
		mutex_unlock(&ctrlr->mtx);
980 981 982 983 984
		return -EBUSY;
	}

	ctrlr->locked = 0;

985
	mutex_unlock(&ctrlr->mtx);
986

987 988
	kfree(resource);

989 990 991 992
	return 0;
}
EXPORT_SYMBOL(vme_dma_free);

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
void vme_bus_error_handler(struct vme_bridge *bridge,
			   unsigned long long address, u32 attributes)
{
	struct vme_bus_error *error;

	error = kmalloc(sizeof(struct vme_bus_error), GFP_ATOMIC);
	if (error) {
		error->address = address;
		error->attributes = attributes;
		list_add_tail(&error->list, &bridge->vme_errors);
	} else {
		dev_err(bridge->parent,
			"Unable to alloc memory for VMEbus Error reporting\n");
	}
}
EXPORT_SYMBOL(vme_bus_error_handler);

/*
 * Find the first error in this address range
 */
struct vme_bus_error *vme_find_error(struct vme_bridge *bridge, u32 aspace,
				     unsigned long long address, size_t count)
{
	struct list_head *err_pos;
	struct vme_bus_error *vme_err, *valid = NULL;
	unsigned long long bound;

	bound = address + count;

	/*
	 * XXX We are currently not looking at the address space when parsing
	 *     for errors. This is because parsing the Address Modifier Codes
	 *     is going to be quite resource intensive to do properly. We
	 *     should be OK just looking at the addresses and this is certainly
	 *     much better than what we had before.
	 */
	err_pos = NULL;
	/* Iterate through errors */
	list_for_each(err_pos, &bridge->vme_errors) {
		vme_err = list_entry(err_pos, struct vme_bus_error, list);
		if ((vme_err->address >= address) &&
			(vme_err->address < bound)) {

			valid = vme_err;
			break;
		}
	}

	return valid;
}
EXPORT_SYMBOL(vme_find_error);

/*
 * Clear errors in the provided address range.
 */
void vme_clear_errors(struct vme_bridge *bridge, u32 aspace,
		      unsigned long long address, size_t count)
{
	struct list_head *err_pos, *temp;
	struct vme_bus_error *vme_err;
	unsigned long long bound;

	bound = address + count;

	/*
	 * XXX We are currently not looking at the address space when parsing
	 *     for errors. This is because parsing the Address Modifier Codes
	 *     is going to be quite resource intensive to do properly. We
	 *     should be OK just looking at the addresses and this is certainly
	 *     much better than what we had before.
	 */
	err_pos = NULL;
	/* Iterate through errors */
	list_for_each_safe(err_pos, temp, &bridge->vme_errors) {
		vme_err = list_entry(err_pos, struct vme_bus_error, list);

		if ((vme_err->address >= address) &&
			(vme_err->address < bound)) {

			list_del(err_pos);
			kfree(vme_err);
		}
	}
}
EXPORT_SYMBOL(vme_clear_errors);

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
{
	void (*call)(int, int, void *);
	void *priv_data;

	call = bridge->irq[level - 1].callback[statid].func;
	priv_data = bridge->irq[level - 1].callback[statid].priv_data;

	if (call != NULL)
		call(level, statid, priv_data);
	else
1090 1091
		printk(KERN_WARNING "Spurilous VME interrupt, level:%x, vector:%x\n",
		       level, statid);
1092 1093 1094
}
EXPORT_SYMBOL(vme_irq_handler);

1095
int vme_irq_request(struct vme_dev *vdev, int level, int statid,
1096
	void (*callback)(int, int, void *),
1097 1098 1099 1100
	void *priv_data)
{
	struct vme_bridge *bridge;

1101
	bridge = vdev->bridge;
1102 1103 1104 1105 1106
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return -EINVAL;
	}

1107
	if ((level < 1) || (level > 7)) {
1108
		printk(KERN_ERR "Invalid interrupt level\n");
1109 1110 1111
		return -EINVAL;
	}

1112 1113
	if (bridge->irq_set == NULL) {
		printk(KERN_ERR "Configuring interrupts not supported\n");
1114 1115 1116
		return -EINVAL;
	}

1117
	mutex_lock(&bridge->irq_mtx);
1118 1119

	if (bridge->irq[level - 1].callback[statid].func) {
1120
		mutex_unlock(&bridge->irq_mtx);
1121 1122 1123 1124 1125 1126 1127 1128 1129
		printk(KERN_WARNING "VME Interrupt already taken\n");
		return -EBUSY;
	}

	bridge->irq[level - 1].count++;
	bridge->irq[level - 1].callback[statid].priv_data = priv_data;
	bridge->irq[level - 1].callback[statid].func = callback;

	/* Enable IRQ level */
1130
	bridge->irq_set(bridge, level, 1, 1);
1131

1132
	mutex_unlock(&bridge->irq_mtx);
1133 1134

	return 0;
1135
}
1136
EXPORT_SYMBOL(vme_irq_request);
1137

1138
void vme_irq_free(struct vme_dev *vdev, int level, int statid)
1139 1140 1141
{
	struct vme_bridge *bridge;

1142
	bridge = vdev->bridge;
1143 1144 1145 1146 1147
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return;
	}

1148
	if ((level < 1) || (level > 7)) {
1149
		printk(KERN_ERR "Invalid interrupt level\n");
1150 1151 1152
		return;
	}

1153 1154
	if (bridge->irq_set == NULL) {
		printk(KERN_ERR "Configuring interrupts not supported\n");
1155 1156 1157
		return;
	}

1158
	mutex_lock(&bridge->irq_mtx);
1159 1160 1161 1162 1163

	bridge->irq[level - 1].count--;

	/* Disable IRQ level if no more interrupts attached at this level*/
	if (bridge->irq[level - 1].count == 0)
1164
		bridge->irq_set(bridge, level, 0, 1);
1165 1166 1167 1168

	bridge->irq[level - 1].callback[statid].func = NULL;
	bridge->irq[level - 1].callback[statid].priv_data = NULL;

1169
	mutex_unlock(&bridge->irq_mtx);
1170
}
1171
EXPORT_SYMBOL(vme_irq_free);
1172

1173
int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
1174 1175 1176
{
	struct vme_bridge *bridge;

1177
	bridge = vdev->bridge;
1178 1179 1180 1181 1182
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return -EINVAL;
	}

1183
	if ((level < 1) || (level > 7)) {
1184 1185 1186 1187
		printk(KERN_WARNING "Invalid interrupt level\n");
		return -EINVAL;
	}

1188
	if (bridge->irq_generate == NULL) {
1189
		printk(KERN_WARNING "Interrupt generation not supported\n");
1190 1191 1192
		return -EINVAL;
	}

1193
	return bridge->irq_generate(bridge, level, statid);
1194
}
1195
EXPORT_SYMBOL(vme_irq_generate);
1196

1197 1198 1199
/*
 * Request the location monitor, return resource or NULL
 */
1200
struct vme_resource *vme_lm_request(struct vme_dev *vdev)
1201 1202
{
	struct vme_bridge *bridge;
1203 1204 1205 1206
	struct list_head *lm_pos = NULL;
	struct vme_lm_resource *allocated_lm = NULL;
	struct vme_lm_resource *lm = NULL;
	struct vme_resource *resource = NULL;
1207

1208
	bridge = vdev->bridge;
1209 1210
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
1211 1212 1213 1214
		goto err_bus;
	}

	/* Loop through DMA resources */
1215
	list_for_each(lm_pos, &bridge->lm_resources) {
1216 1217 1218 1219
		lm = list_entry(lm_pos,
			struct vme_lm_resource, list);

		if (lm == NULL) {
1220
			printk(KERN_ERR "Registered NULL Location Monitor resource\n");
1221 1222 1223 1224
			continue;
		}

		/* Find an unlocked controller */
1225
		mutex_lock(&lm->mtx);
1226 1227
		if (lm->locked == 0) {
			lm->locked = 1;
1228
			mutex_unlock(&lm->mtx);
1229 1230 1231
			allocated_lm = lm;
			break;
		}
1232
		mutex_unlock(&lm->mtx);
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
	}

	/* Check to see if we found a resource */
	if (allocated_lm == NULL)
		goto err_lm;

	resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
	if (resource == NULL) {
		printk(KERN_ERR "Unable to allocate resource structure\n");
		goto err_alloc;
	}
	resource->type = VME_LM;
1245
	resource->entry = &allocated_lm->list;
1246 1247 1248 1249 1250

	return resource;

err_alloc:
	/* Unlock image */
1251
	mutex_lock(&lm->mtx);
1252
	lm->locked = 0;
1253
	mutex_unlock(&lm->mtx);
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
err_lm:
err_bus:
	return NULL;
}
EXPORT_SYMBOL(vme_lm_request);

int vme_lm_count(struct vme_resource *resource)
{
	struct vme_lm_resource *lm;

	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
		return -EINVAL;
	}

	lm = list_entry(resource->entry, struct vme_lm_resource, list);

	return lm->monitors;
}
EXPORT_SYMBOL(vme_lm_count);

int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
M
Martyn Welch 已提交
1276
	u32 aspace, u32 cycle)
1277 1278 1279 1280 1281 1282
{
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;

	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
1283 1284 1285
		return -EINVAL;
	}

1286 1287
	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1288
	if (bridge->lm_set == NULL) {
1289
		printk(KERN_ERR "vme_lm_set not supported\n");
1290 1291 1292
		return -EINVAL;
	}

1293
	return bridge->lm_set(lm, lm_base, aspace, cycle);
1294 1295 1296
}
EXPORT_SYMBOL(vme_lm_set);

1297
int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
M
Martyn Welch 已提交
1298
	u32 *aspace, u32 *cycle)
1299
{
1300 1301
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;
1302

1303 1304
	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
1305 1306 1307
		return -EINVAL;
	}

1308 1309
	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1310
	if (bridge->lm_get == NULL) {
1311
		printk(KERN_ERR "vme_lm_get not supported\n");
1312 1313 1314
		return -EINVAL;
	}

1315
	return bridge->lm_get(lm, lm_base, aspace, cycle);
1316 1317 1318
}
EXPORT_SYMBOL(vme_lm_get);

1319 1320
int vme_lm_attach(struct vme_resource *resource, int monitor,
	void (*callback)(int))
1321
{
1322 1323
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;
1324

1325 1326
	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
1327 1328 1329
		return -EINVAL;
	}

1330 1331
	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1332
	if (bridge->lm_attach == NULL) {
1333
		printk(KERN_ERR "vme_lm_attach not supported\n");
1334 1335 1336
		return -EINVAL;
	}

1337
	return bridge->lm_attach(lm, monitor, callback);
1338 1339 1340
}
EXPORT_SYMBOL(vme_lm_attach);

1341
int vme_lm_detach(struct vme_resource *resource, int monitor)
1342
{
1343 1344
	struct vme_bridge *bridge = find_bridge(resource);
	struct vme_lm_resource *lm;
1345

1346 1347
	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
1348 1349 1350
		return -EINVAL;
	}

1351 1352
	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1353
	if (bridge->lm_detach == NULL) {
1354
		printk(KERN_ERR "vme_lm_detach not supported\n");
1355 1356 1357
		return -EINVAL;
	}

1358
	return bridge->lm_detach(lm, monitor);
1359 1360 1361
}
EXPORT_SYMBOL(vme_lm_detach);

1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
void vme_lm_free(struct vme_resource *resource)
{
	struct vme_lm_resource *lm;

	if (resource->type != VME_LM) {
		printk(KERN_ERR "Not a Location Monitor resource\n");
		return;
	}

	lm = list_entry(resource->entry, struct vme_lm_resource, list);

1373
	mutex_lock(&lm->mtx);
1374

1375 1376 1377 1378
	/* XXX
	 * Check to see that there aren't any callbacks still attached, if
	 * there are we should probably be detaching them!
	 */
1379 1380 1381

	lm->locked = 0;

1382
	mutex_unlock(&lm->mtx);
1383 1384

	kfree(resource);
1385 1386 1387
}
EXPORT_SYMBOL(vme_lm_free);

1388
int vme_slot_num(struct vme_dev *vdev)
1389 1390 1391
{
	struct vme_bridge *bridge;

1392
	bridge = vdev->bridge;
1393 1394 1395 1396 1397 1398
	if (bridge == NULL) {
		printk(KERN_ERR "Can't find VME bus\n");
		return -EINVAL;
	}

	if (bridge->slot_get == NULL) {
1399
		printk(KERN_WARNING "vme_slot_num not supported\n");
1400 1401 1402
		return -EINVAL;
	}

1403
	return bridge->slot_get(bridge);
1404
}
1405
EXPORT_SYMBOL(vme_slot_num);
1406

1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
int vme_bus_num(struct vme_dev *vdev)
{
	struct vme_bridge *bridge;

	bridge = vdev->bridge;
	if (bridge == NULL) {
		pr_err("Can't find VME bus\n");
		return -EINVAL;
	}

	return bridge->num;
}
EXPORT_SYMBOL(vme_bus_num);
1420 1421 1422

/* - Bridge Registration --------------------------------------------------- */

1423 1424 1425 1426 1427 1428
static void vme_dev_release(struct device *dev)
{
	kfree(dev_to_vme_dev(dev));
}

int vme_register_bridge(struct vme_bridge *bridge)
1429 1430
{
	int i;
1431
	int ret = -1;
1432

1433
	mutex_lock(&vme_buses_lock);
1434
	for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1435 1436 1437
		if ((vme_bus_numbers & (1 << i)) == 0) {
			vme_bus_numbers |= (1 << i);
			bridge->num = i;
1438
			INIT_LIST_HEAD(&bridge->devices);
1439 1440
			list_add_tail(&bridge->bus_list, &vme_bus_list);
			ret = 0;
1441 1442 1443
			break;
		}
	}
1444
	mutex_unlock(&vme_buses_lock);
1445

1446
	return ret;
1447
}
1448
EXPORT_SYMBOL(vme_register_bridge);
1449

1450
void vme_unregister_bridge(struct vme_bridge *bridge)
1451
{
1452 1453 1454
	struct vme_dev *vdev;
	struct vme_dev *tmp;

1455 1456
	mutex_lock(&vme_buses_lock);
	vme_bus_numbers &= ~(1 << bridge->num);
1457 1458 1459 1460 1461
	list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
		list_del(&vdev->drv_list);
		list_del(&vdev->bridge_list);
		device_unregister(&vdev->dev);
	}
1462 1463
	list_del(&bridge->bus_list);
	mutex_unlock(&vme_buses_lock);
1464
}
1465
EXPORT_SYMBOL(vme_unregister_bridge);
1466

1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
/* - Driver Registration --------------------------------------------------- */

static int __vme_register_driver_bus(struct vme_driver *drv,
	struct vme_bridge *bridge, unsigned int ndevs)
{
	int err;
	unsigned int i;
	struct vme_dev *vdev;
	struct vme_dev *tmp;

	for (i = 0; i < ndevs; i++) {
		vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
		if (!vdev) {
			err = -ENOMEM;
1481 1482
			goto err_devalloc;
		}
1483
		vdev->num = i;
1484
		vdev->bridge = bridge;
1485 1486
		vdev->dev.platform_data = drv;
		vdev->dev.release = vme_dev_release;
1487 1488
		vdev->dev.parent = bridge->parent;
		vdev->dev.bus = &vme_bus_type;
1489 1490
		dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
			vdev->num);
1491

1492 1493
		err = device_register(&vdev->dev);
		if (err)
1494 1495
			goto err_reg;

1496 1497 1498 1499 1500 1501 1502
		if (vdev->dev.platform_data) {
			list_add_tail(&vdev->drv_list, &drv->devices);
			list_add_tail(&vdev->bridge_list, &bridge->devices);
		} else
			device_unregister(&vdev->dev);
	}
	return 0;
1503 1504

err_reg:
1505
	put_device(&vdev->dev);
1506
	kfree(vdev);
1507
err_devalloc:
1508 1509 1510
	list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
		list_del(&vdev->drv_list);
		list_del(&vdev->bridge_list);
1511
		device_unregister(&vdev->dev);
1512
	}
1513
	return err;
1514 1515
}

1516
static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1517
{
1518 1519
	struct vme_bridge *bridge;
	int err = 0;
1520

1521 1522 1523 1524 1525 1526 1527
	mutex_lock(&vme_buses_lock);
	list_for_each_entry(bridge, &vme_bus_list, bus_list) {
		/*
		 * This cannot cause trouble as we already have vme_buses_lock
		 * and if the bridge is removed, it will have to go through
		 * vme_unregister_bridge() to do it (which calls remove() on
		 * the bridge which in turn tries to acquire vme_buses_lock and
1528
		 * will have to wait).
1529 1530 1531 1532
		 */
		err = __vme_register_driver_bus(drv, bridge, ndevs);
		if (err)
			break;
1533
	}
1534 1535
	mutex_unlock(&vme_buses_lock);
	return err;
1536 1537
}

1538
int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1539
{
1540 1541
	int err;

1542 1543
	drv->driver.name = drv->name;
	drv->driver.bus = &vme_bus_type;
1544 1545 1546 1547 1548
	INIT_LIST_HEAD(&drv->devices);

	err = driver_register(&drv->driver);
	if (err)
		return err;
1549

1550 1551 1552 1553 1554
	err = __vme_register_driver(drv, ndevs);
	if (err)
		driver_unregister(&drv->driver);

	return err;
1555 1556 1557
}
EXPORT_SYMBOL(vme_register_driver);

1558
void vme_unregister_driver(struct vme_driver *drv)
1559
{
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
	struct vme_dev *dev, *dev_tmp;

	mutex_lock(&vme_buses_lock);
	list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
		list_del(&dev->drv_list);
		list_del(&dev->bridge_list);
		device_unregister(&dev->dev);
	}
	mutex_unlock(&vme_buses_lock);

1570 1571 1572 1573 1574 1575 1576 1577
	driver_unregister(&drv->driver);
}
EXPORT_SYMBOL(vme_unregister_driver);

/* - Bus Registration ------------------------------------------------------ */

static int vme_bus_match(struct device *dev, struct device_driver *drv)
{
1578
	struct vme_driver *vme_drv;
1579

1580
	vme_drv = container_of(drv, struct vme_driver, driver);
1581

1582 1583
	if (dev->platform_data == vme_drv) {
		struct vme_dev *vdev = dev_to_vme_dev(dev);
1584

1585 1586
		if (vme_drv->match && vme_drv->match(vdev))
			return 1;
1587

1588
		dev->platform_data = NULL;
1589 1590 1591 1592 1593 1594 1595
	}
	return 0;
}

static int vme_bus_probe(struct device *dev)
{
	int retval = -ENODEV;
1596 1597
	struct vme_driver *driver;
	struct vme_dev *vdev = dev_to_vme_dev(dev);
1598

1599
	driver = dev->platform_data;
1600

1601
	if (driver->probe != NULL)
1602
		retval = driver->probe(vdev);
1603 1604 1605 1606 1607 1608 1609

	return retval;
}

static int vme_bus_remove(struct device *dev)
{
	int retval = -ENODEV;
1610 1611
	struct vme_driver *driver;
	struct vme_dev *vdev = dev_to_vme_dev(dev);
1612

1613
	driver = dev->platform_data;
1614

1615
	if (driver->remove != NULL)
1616
		retval = driver->remove(vdev);
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628

	return retval;
}

struct bus_type vme_bus_type = {
	.name = "vme",
	.match = vme_bus_match,
	.probe = vme_bus_probe,
	.remove = vme_bus_remove,
};
EXPORT_SYMBOL(vme_bus_type);

1629
static int __init vme_init(void)
1630 1631 1632 1633
{
	return bus_register(&vme_bus_type);
}

1634
static void __exit vme_exit(void)
1635 1636 1637 1638
{
	bus_unregister(&vme_bus_type);
}

1639
subsys_initcall(vme_init);
1640
module_exit(vme_exit);