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