Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * VME Bridge Framework
0004  *
0005  * Author: Martyn Welch <martyn.welch@ge.com>
0006  * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
0007  *
0008  * Based on work by Tom Armistead and Ajit Prem
0009  * Copyright 2004 Motorola Inc.
0010  */
0011 
0012 #include <linux/init.h>
0013 #include <linux/export.h>
0014 #include <linux/mm.h>
0015 #include <linux/types.h>
0016 #include <linux/kernel.h>
0017 #include <linux/errno.h>
0018 #include <linux/pci.h>
0019 #include <linux/poll.h>
0020 #include <linux/highmem.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/pagemap.h>
0023 #include <linux/device.h>
0024 #include <linux/dma-mapping.h>
0025 #include <linux/syscalls.h>
0026 #include <linux/mutex.h>
0027 #include <linux/spinlock.h>
0028 #include <linux/slab.h>
0029 
0030 #include "vme.h"
0031 #include "vme_bridge.h"
0032 
0033 /* Bitmask and list of registered buses both protected by common mutex */
0034 static unsigned int vme_bus_numbers;
0035 static LIST_HEAD(vme_bus_list);
0036 static DEFINE_MUTEX(vme_buses_lock);
0037 
0038 static int __init vme_init(void);
0039 
0040 static struct vme_dev *dev_to_vme_dev(struct device *dev)
0041 {
0042     return container_of(dev, struct vme_dev, dev);
0043 }
0044 
0045 /*
0046  * Find the bridge that the resource is associated with.
0047  */
0048 static struct vme_bridge *find_bridge(struct vme_resource *resource)
0049 {
0050     /* Get list to search */
0051     switch (resource->type) {
0052     case VME_MASTER:
0053         return list_entry(resource->entry, struct vme_master_resource,
0054             list)->parent;
0055     case VME_SLAVE:
0056         return list_entry(resource->entry, struct vme_slave_resource,
0057             list)->parent;
0058     case VME_DMA:
0059         return list_entry(resource->entry, struct vme_dma_resource,
0060             list)->parent;
0061     case VME_LM:
0062         return list_entry(resource->entry, struct vme_lm_resource,
0063             list)->parent;
0064     default:
0065         printk(KERN_ERR "Unknown resource type\n");
0066         return NULL;
0067     }
0068 }
0069 
0070 /**
0071  * vme_alloc_consistent - Allocate contiguous memory.
0072  * @resource: Pointer to VME resource.
0073  * @size: Size of allocation required.
0074  * @dma: Pointer to variable to store physical address of allocation.
0075  *
0076  * Allocate a contiguous block of memory for use by the driver. This is used to
0077  * create the buffers for the slave windows.
0078  *
0079  * Return: Virtual address of allocation on success, NULL on failure.
0080  */
0081 void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
0082     dma_addr_t *dma)
0083 {
0084     struct vme_bridge *bridge;
0085 
0086     if (!resource) {
0087         printk(KERN_ERR "No resource\n");
0088         return NULL;
0089     }
0090 
0091     bridge = find_bridge(resource);
0092     if (!bridge) {
0093         printk(KERN_ERR "Can't find bridge\n");
0094         return NULL;
0095     }
0096 
0097     if (!bridge->parent) {
0098         printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
0099         return NULL;
0100     }
0101 
0102     if (!bridge->alloc_consistent) {
0103         printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
0104                bridge->name);
0105         return NULL;
0106     }
0107 
0108     return bridge->alloc_consistent(bridge->parent, size, dma);
0109 }
0110 EXPORT_SYMBOL(vme_alloc_consistent);
0111 
0112 /**
0113  * vme_free_consistent - Free previously allocated memory.
0114  * @resource: Pointer to VME resource.
0115  * @size: Size of allocation to free.
0116  * @vaddr: Virtual address of allocation.
0117  * @dma: Physical address of allocation.
0118  *
0119  * Free previously allocated block of contiguous memory.
0120  */
0121 void vme_free_consistent(struct vme_resource *resource, size_t size,
0122     void *vaddr, dma_addr_t dma)
0123 {
0124     struct vme_bridge *bridge;
0125 
0126     if (!resource) {
0127         printk(KERN_ERR "No resource\n");
0128         return;
0129     }
0130 
0131     bridge = find_bridge(resource);
0132     if (!bridge) {
0133         printk(KERN_ERR "Can't find bridge\n");
0134         return;
0135     }
0136 
0137     if (!bridge->parent) {
0138         printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
0139         return;
0140     }
0141 
0142     if (!bridge->free_consistent) {
0143         printk(KERN_ERR "free_consistent not supported by bridge %s\n",
0144                bridge->name);
0145         return;
0146     }
0147 
0148     bridge->free_consistent(bridge->parent, size, vaddr, dma);
0149 }
0150 EXPORT_SYMBOL(vme_free_consistent);
0151 
0152 /**
0153  * vme_get_size - Helper function returning size of a VME window
0154  * @resource: Pointer to VME slave or master resource.
0155  *
0156  * Determine the size of the VME window provided. This is a helper
0157  * function, wrappering the call to vme_master_get or vme_slave_get
0158  * depending on the type of window resource handed to it.
0159  *
0160  * Return: Size of the window on success, zero on failure.
0161  */
0162 size_t vme_get_size(struct vme_resource *resource)
0163 {
0164     int enabled, retval;
0165     unsigned long long base, size;
0166     dma_addr_t buf_base;
0167     u32 aspace, cycle, dwidth;
0168 
0169     switch (resource->type) {
0170     case VME_MASTER:
0171         retval = vme_master_get(resource, &enabled, &base, &size,
0172             &aspace, &cycle, &dwidth);
0173         if (retval)
0174             return 0;
0175 
0176         return size;
0177     case VME_SLAVE:
0178         retval = vme_slave_get(resource, &enabled, &base, &size,
0179             &buf_base, &aspace, &cycle);
0180         if (retval)
0181             return 0;
0182 
0183         return size;
0184     case VME_DMA:
0185         return 0;
0186     default:
0187         printk(KERN_ERR "Unknown resource type\n");
0188         return 0;
0189     }
0190 }
0191 EXPORT_SYMBOL(vme_get_size);
0192 
0193 int vme_check_window(u32 aspace, unsigned long long vme_base,
0194              unsigned long long size)
0195 {
0196     int retval = 0;
0197 
0198     if (vme_base + size < size)
0199         return -EINVAL;
0200 
0201     switch (aspace) {
0202     case VME_A16:
0203         if (vme_base + size > VME_A16_MAX)
0204             retval = -EFAULT;
0205         break;
0206     case VME_A24:
0207         if (vme_base + size > VME_A24_MAX)
0208             retval = -EFAULT;
0209         break;
0210     case VME_A32:
0211         if (vme_base + size > VME_A32_MAX)
0212             retval = -EFAULT;
0213         break;
0214     case VME_A64:
0215         /* The VME_A64_MAX limit is actually U64_MAX + 1 */
0216         break;
0217     case VME_CRCSR:
0218         if (vme_base + size > VME_CRCSR_MAX)
0219             retval = -EFAULT;
0220         break;
0221     case VME_USER1:
0222     case VME_USER2:
0223     case VME_USER3:
0224     case VME_USER4:
0225         /* User Defined */
0226         break;
0227     default:
0228         printk(KERN_ERR "Invalid address space\n");
0229         retval = -EINVAL;
0230         break;
0231     }
0232 
0233     return retval;
0234 }
0235 EXPORT_SYMBOL(vme_check_window);
0236 
0237 static u32 vme_get_aspace(int am)
0238 {
0239     switch (am) {
0240     case 0x29:
0241     case 0x2D:
0242         return VME_A16;
0243     case 0x38:
0244     case 0x39:
0245     case 0x3A:
0246     case 0x3B:
0247     case 0x3C:
0248     case 0x3D:
0249     case 0x3E:
0250     case 0x3F:
0251         return VME_A24;
0252     case 0x8:
0253     case 0x9:
0254     case 0xA:
0255     case 0xB:
0256     case 0xC:
0257     case 0xD:
0258     case 0xE:
0259     case 0xF:
0260         return VME_A32;
0261     case 0x0:
0262     case 0x1:
0263     case 0x3:
0264         return VME_A64;
0265     }
0266 
0267     return 0;
0268 }
0269 
0270 /**
0271  * vme_slave_request - Request a VME slave window resource.
0272  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
0273  * @address: Required VME address space.
0274  * @cycle: Required VME data transfer cycle type.
0275  *
0276  * Request use of a VME window resource capable of being set for the requested
0277  * address space and data transfer cycle.
0278  *
0279  * Return: Pointer to VME resource on success, NULL on failure.
0280  */
0281 struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
0282     u32 cycle)
0283 {
0284     struct vme_bridge *bridge;
0285     struct list_head *slave_pos = NULL;
0286     struct vme_slave_resource *allocated_image = NULL;
0287     struct vme_slave_resource *slave_image = NULL;
0288     struct vme_resource *resource = NULL;
0289 
0290     bridge = vdev->bridge;
0291     if (!bridge) {
0292         printk(KERN_ERR "Can't find VME bus\n");
0293         goto err_bus;
0294     }
0295 
0296     /* Loop through slave resources */
0297     list_for_each(slave_pos, &bridge->slave_resources) {
0298         slave_image = list_entry(slave_pos,
0299             struct vme_slave_resource, list);
0300 
0301         if (!slave_image) {
0302             printk(KERN_ERR "Registered NULL Slave resource\n");
0303             continue;
0304         }
0305 
0306         /* Find an unlocked and compatible image */
0307         mutex_lock(&slave_image->mtx);
0308         if (((slave_image->address_attr & address) == address) &&
0309             ((slave_image->cycle_attr & cycle) == cycle) &&
0310             (slave_image->locked == 0)) {
0311 
0312             slave_image->locked = 1;
0313             mutex_unlock(&slave_image->mtx);
0314             allocated_image = slave_image;
0315             break;
0316         }
0317         mutex_unlock(&slave_image->mtx);
0318     }
0319 
0320     /* No free image */
0321     if (!allocated_image)
0322         goto err_image;
0323 
0324     resource = kmalloc(sizeof(*resource), GFP_KERNEL);
0325     if (!resource)
0326         goto err_alloc;
0327 
0328     resource->type = VME_SLAVE;
0329     resource->entry = &allocated_image->list;
0330 
0331     return resource;
0332 
0333 err_alloc:
0334     /* Unlock image */
0335     mutex_lock(&slave_image->mtx);
0336     slave_image->locked = 0;
0337     mutex_unlock(&slave_image->mtx);
0338 err_image:
0339 err_bus:
0340     return NULL;
0341 }
0342 EXPORT_SYMBOL(vme_slave_request);
0343 
0344 /**
0345  * vme_slave_set - Set VME slave window configuration.
0346  * @resource: Pointer to VME slave resource.
0347  * @enabled: State to which the window should be configured.
0348  * @vme_base: Base address for the window.
0349  * @size: Size of the VME window.
0350  * @buf_base: Based address of buffer used to provide VME slave window storage.
0351  * @aspace: VME address space for the VME window.
0352  * @cycle: VME data transfer cycle type for the VME window.
0353  *
0354  * Set configuration for provided VME slave window.
0355  *
0356  * Return: Zero on success, -EINVAL if operation is not supported on this
0357  *         device, if an invalid resource has been provided or invalid
0358  *         attributes are provided. Hardware specific errors may also be
0359  *         returned.
0360  */
0361 int vme_slave_set(struct vme_resource *resource, int enabled,
0362     unsigned long long vme_base, unsigned long long size,
0363     dma_addr_t buf_base, u32 aspace, u32 cycle)
0364 {
0365     struct vme_bridge *bridge = find_bridge(resource);
0366     struct vme_slave_resource *image;
0367     int retval;
0368 
0369     if (resource->type != VME_SLAVE) {
0370         printk(KERN_ERR "Not a slave resource\n");
0371         return -EINVAL;
0372     }
0373 
0374     image = list_entry(resource->entry, struct vme_slave_resource, list);
0375 
0376     if (!bridge->slave_set) {
0377         printk(KERN_ERR "Function not supported\n");
0378         return -ENOSYS;
0379     }
0380 
0381     if (!(((image->address_attr & aspace) == aspace) &&
0382         ((image->cycle_attr & cycle) == cycle))) {
0383         printk(KERN_ERR "Invalid attributes\n");
0384         return -EINVAL;
0385     }
0386 
0387     retval = vme_check_window(aspace, vme_base, size);
0388     if (retval)
0389         return retval;
0390 
0391     return bridge->slave_set(image, enabled, vme_base, size, buf_base,
0392         aspace, cycle);
0393 }
0394 EXPORT_SYMBOL(vme_slave_set);
0395 
0396 /**
0397  * vme_slave_get - Retrieve VME slave window configuration.
0398  * @resource: Pointer to VME slave resource.
0399  * @enabled: Pointer to variable for storing state.
0400  * @vme_base: Pointer to variable for storing window base address.
0401  * @size: Pointer to variable for storing window size.
0402  * @buf_base: Pointer to variable for storing slave buffer base address.
0403  * @aspace: Pointer to variable for storing VME address space.
0404  * @cycle: Pointer to variable for storing VME data transfer cycle type.
0405  *
0406  * Return configuration for provided VME slave window.
0407  *
0408  * Return: Zero on success, -EINVAL if operation is not supported on this
0409  *         device or if an invalid resource has been provided.
0410  */
0411 int vme_slave_get(struct vme_resource *resource, int *enabled,
0412     unsigned long long *vme_base, unsigned long long *size,
0413     dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
0414 {
0415     struct vme_bridge *bridge = find_bridge(resource);
0416     struct vme_slave_resource *image;
0417 
0418     if (resource->type != VME_SLAVE) {
0419         printk(KERN_ERR "Not a slave resource\n");
0420         return -EINVAL;
0421     }
0422 
0423     image = list_entry(resource->entry, struct vme_slave_resource, list);
0424 
0425     if (!bridge->slave_get) {
0426         printk(KERN_ERR "vme_slave_get not supported\n");
0427         return -EINVAL;
0428     }
0429 
0430     return bridge->slave_get(image, enabled, vme_base, size, buf_base,
0431         aspace, cycle);
0432 }
0433 EXPORT_SYMBOL(vme_slave_get);
0434 
0435 /**
0436  * vme_slave_free - Free VME slave window
0437  * @resource: Pointer to VME slave resource.
0438  *
0439  * Free the provided slave resource so that it may be reallocated.
0440  */
0441 void vme_slave_free(struct vme_resource *resource)
0442 {
0443     struct vme_slave_resource *slave_image;
0444 
0445     if (resource->type != VME_SLAVE) {
0446         printk(KERN_ERR "Not a slave resource\n");
0447         return;
0448     }
0449 
0450     slave_image = list_entry(resource->entry, struct vme_slave_resource,
0451         list);
0452     if (!slave_image) {
0453         printk(KERN_ERR "Can't find slave resource\n");
0454         return;
0455     }
0456 
0457     /* Unlock image */
0458     mutex_lock(&slave_image->mtx);
0459     if (slave_image->locked == 0)
0460         printk(KERN_ERR "Image is already free\n");
0461 
0462     slave_image->locked = 0;
0463     mutex_unlock(&slave_image->mtx);
0464 
0465     /* Free up resource memory */
0466     kfree(resource);
0467 }
0468 EXPORT_SYMBOL(vme_slave_free);
0469 
0470 /**
0471  * vme_master_request - Request a VME master window resource.
0472  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
0473  * @address: Required VME address space.
0474  * @cycle: Required VME data transfer cycle type.
0475  * @dwidth: Required VME data transfer width.
0476  *
0477  * Request use of a VME window resource capable of being set for the requested
0478  * address space, data transfer cycle and width.
0479  *
0480  * Return: Pointer to VME resource on success, NULL on failure.
0481  */
0482 struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
0483     u32 cycle, u32 dwidth)
0484 {
0485     struct vme_bridge *bridge;
0486     struct list_head *master_pos = NULL;
0487     struct vme_master_resource *allocated_image = NULL;
0488     struct vme_master_resource *master_image = NULL;
0489     struct vme_resource *resource = NULL;
0490 
0491     bridge = vdev->bridge;
0492     if (!bridge) {
0493         printk(KERN_ERR "Can't find VME bus\n");
0494         goto err_bus;
0495     }
0496 
0497     /* Loop through master resources */
0498     list_for_each(master_pos, &bridge->master_resources) {
0499         master_image = list_entry(master_pos,
0500             struct vme_master_resource, list);
0501 
0502         if (!master_image) {
0503             printk(KERN_WARNING "Registered NULL master resource\n");
0504             continue;
0505         }
0506 
0507         /* Find an unlocked and compatible image */
0508         spin_lock(&master_image->lock);
0509         if (((master_image->address_attr & address) == address) &&
0510             ((master_image->cycle_attr & cycle) == cycle) &&
0511             ((master_image->width_attr & dwidth) == dwidth) &&
0512             (master_image->locked == 0)) {
0513 
0514             master_image->locked = 1;
0515             spin_unlock(&master_image->lock);
0516             allocated_image = master_image;
0517             break;
0518         }
0519         spin_unlock(&master_image->lock);
0520     }
0521 
0522     /* Check to see if we found a resource */
0523     if (!allocated_image) {
0524         printk(KERN_ERR "Can't find a suitable resource\n");
0525         goto err_image;
0526     }
0527 
0528     resource = kmalloc(sizeof(*resource), GFP_KERNEL);
0529     if (!resource)
0530         goto err_alloc;
0531 
0532     resource->type = VME_MASTER;
0533     resource->entry = &allocated_image->list;
0534 
0535     return resource;
0536 
0537 err_alloc:
0538     /* Unlock image */
0539     spin_lock(&master_image->lock);
0540     master_image->locked = 0;
0541     spin_unlock(&master_image->lock);
0542 err_image:
0543 err_bus:
0544     return NULL;
0545 }
0546 EXPORT_SYMBOL(vme_master_request);
0547 
0548 /**
0549  * vme_master_set - Set VME master window configuration.
0550  * @resource: Pointer to VME master resource.
0551  * @enabled: State to which the window should be configured.
0552  * @vme_base: Base address for the window.
0553  * @size: Size of the VME window.
0554  * @aspace: VME address space for the VME window.
0555  * @cycle: VME data transfer cycle type for the VME window.
0556  * @dwidth: VME data transfer width for the VME window.
0557  *
0558  * Set configuration for provided VME master window.
0559  *
0560  * Return: Zero on success, -EINVAL if operation is not supported on this
0561  *         device, if an invalid resource has been provided or invalid
0562  *         attributes are provided. Hardware specific errors may also be
0563  *         returned.
0564  */
0565 int vme_master_set(struct vme_resource *resource, int enabled,
0566     unsigned long long vme_base, unsigned long long size, u32 aspace,
0567     u32 cycle, u32 dwidth)
0568 {
0569     struct vme_bridge *bridge = find_bridge(resource);
0570     struct vme_master_resource *image;
0571     int retval;
0572 
0573     if (resource->type != VME_MASTER) {
0574         printk(KERN_ERR "Not a master resource\n");
0575         return -EINVAL;
0576     }
0577 
0578     image = list_entry(resource->entry, struct vme_master_resource, list);
0579 
0580     if (!bridge->master_set) {
0581         printk(KERN_WARNING "vme_master_set not supported\n");
0582         return -EINVAL;
0583     }
0584 
0585     if (!(((image->address_attr & aspace) == aspace) &&
0586         ((image->cycle_attr & cycle) == cycle) &&
0587         ((image->width_attr & dwidth) == dwidth))) {
0588         printk(KERN_WARNING "Invalid attributes\n");
0589         return -EINVAL;
0590     }
0591 
0592     retval = vme_check_window(aspace, vme_base, size);
0593     if (retval)
0594         return retval;
0595 
0596     return bridge->master_set(image, enabled, vme_base, size, aspace,
0597         cycle, dwidth);
0598 }
0599 EXPORT_SYMBOL(vme_master_set);
0600 
0601 /**
0602  * vme_master_get - Retrieve VME master window configuration.
0603  * @resource: Pointer to VME master resource.
0604  * @enabled: Pointer to variable for storing state.
0605  * @vme_base: Pointer to variable for storing window base address.
0606  * @size: Pointer to variable for storing window size.
0607  * @aspace: Pointer to variable for storing VME address space.
0608  * @cycle: Pointer to variable for storing VME data transfer cycle type.
0609  * @dwidth: Pointer to variable for storing VME data transfer width.
0610  *
0611  * Return configuration for provided VME master window.
0612  *
0613  * Return: Zero on success, -EINVAL if operation is not supported on this
0614  *         device or if an invalid resource has been provided.
0615  */
0616 int vme_master_get(struct vme_resource *resource, int *enabled,
0617     unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
0618     u32 *cycle, u32 *dwidth)
0619 {
0620     struct vme_bridge *bridge = find_bridge(resource);
0621     struct vme_master_resource *image;
0622 
0623     if (resource->type != VME_MASTER) {
0624         printk(KERN_ERR "Not a master resource\n");
0625         return -EINVAL;
0626     }
0627 
0628     image = list_entry(resource->entry, struct vme_master_resource, list);
0629 
0630     if (!bridge->master_get) {
0631         printk(KERN_WARNING "%s not supported\n", __func__);
0632         return -EINVAL;
0633     }
0634 
0635     return bridge->master_get(image, enabled, vme_base, size, aspace,
0636         cycle, dwidth);
0637 }
0638 EXPORT_SYMBOL(vme_master_get);
0639 
0640 /**
0641  * vme_master_read - Read data from VME space into a buffer.
0642  * @resource: Pointer to VME master resource.
0643  * @buf: Pointer to buffer where data should be transferred.
0644  * @count: Number of bytes to transfer.
0645  * @offset: Offset into VME master window at which to start transfer.
0646  *
0647  * Perform read of count bytes of data from location on VME bus which maps into
0648  * the VME master window at offset to buf.
0649  *
0650  * Return: Number of bytes read, -EINVAL if resource is not a VME master
0651  *         resource or read operation is not supported. -EFAULT returned if
0652  *         invalid offset is provided. Hardware specific errors may also be
0653  *         returned.
0654  */
0655 ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
0656     loff_t offset)
0657 {
0658     struct vme_bridge *bridge = find_bridge(resource);
0659     struct vme_master_resource *image;
0660     size_t length;
0661 
0662     if (!bridge->master_read) {
0663         printk(KERN_WARNING "Reading from resource not supported\n");
0664         return -EINVAL;
0665     }
0666 
0667     if (resource->type != VME_MASTER) {
0668         printk(KERN_ERR "Not a master resource\n");
0669         return -EINVAL;
0670     }
0671 
0672     image = list_entry(resource->entry, struct vme_master_resource, list);
0673 
0674     length = vme_get_size(resource);
0675 
0676     if (offset > length) {
0677         printk(KERN_WARNING "Invalid Offset\n");
0678         return -EFAULT;
0679     }
0680 
0681     if ((offset + count) > length)
0682         count = length - offset;
0683 
0684     return bridge->master_read(image, buf, count, offset);
0685 
0686 }
0687 EXPORT_SYMBOL(vme_master_read);
0688 
0689 /**
0690  * vme_master_write - Write data out to VME space from a buffer.
0691  * @resource: Pointer to VME master resource.
0692  * @buf: Pointer to buffer holding data to transfer.
0693  * @count: Number of bytes to transfer.
0694  * @offset: Offset into VME master window at which to start transfer.
0695  *
0696  * Perform write of count bytes of data from buf to location on VME bus which
0697  * maps into the VME master window at offset.
0698  *
0699  * Return: Number of bytes written, -EINVAL if resource is not a VME master
0700  *         resource or write operation is not supported. -EFAULT returned if
0701  *         invalid offset is provided. Hardware specific errors may also be
0702  *         returned.
0703  */
0704 ssize_t vme_master_write(struct vme_resource *resource, void *buf,
0705     size_t count, loff_t offset)
0706 {
0707     struct vme_bridge *bridge = find_bridge(resource);
0708     struct vme_master_resource *image;
0709     size_t length;
0710 
0711     if (!bridge->master_write) {
0712         printk(KERN_WARNING "Writing to resource not supported\n");
0713         return -EINVAL;
0714     }
0715 
0716     if (resource->type != VME_MASTER) {
0717         printk(KERN_ERR "Not a master resource\n");
0718         return -EINVAL;
0719     }
0720 
0721     image = list_entry(resource->entry, struct vme_master_resource, list);
0722 
0723     length = vme_get_size(resource);
0724 
0725     if (offset > length) {
0726         printk(KERN_WARNING "Invalid Offset\n");
0727         return -EFAULT;
0728     }
0729 
0730     if ((offset + count) > length)
0731         count = length - offset;
0732 
0733     return bridge->master_write(image, buf, count, offset);
0734 }
0735 EXPORT_SYMBOL(vme_master_write);
0736 
0737 /**
0738  * vme_master_rmw - Perform read-modify-write cycle.
0739  * @resource: Pointer to VME master resource.
0740  * @mask: Bits to be compared and swapped in operation.
0741  * @compare: Bits to be compared with data read from offset.
0742  * @swap: Bits to be swapped in data read from offset.
0743  * @offset: Offset into VME master window at which to perform operation.
0744  *
0745  * Perform read-modify-write cycle on provided location:
0746  * - Location on VME bus is read.
0747  * - Bits selected by mask are compared with compare.
0748  * - Where a selected bit matches that in compare and are selected in swap,
0749  * the bit is swapped.
0750  * - Result written back to location on VME bus.
0751  *
0752  * Return: Bytes written on success, -EINVAL if resource is not a VME master
0753  *         resource or RMW operation is not supported. Hardware specific
0754  *         errors may also be returned.
0755  */
0756 unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
0757     unsigned int compare, unsigned int swap, loff_t offset)
0758 {
0759     struct vme_bridge *bridge = find_bridge(resource);
0760     struct vme_master_resource *image;
0761 
0762     if (!bridge->master_rmw) {
0763         printk(KERN_WARNING "Writing to resource not supported\n");
0764         return -EINVAL;
0765     }
0766 
0767     if (resource->type != VME_MASTER) {
0768         printk(KERN_ERR "Not a master resource\n");
0769         return -EINVAL;
0770     }
0771 
0772     image = list_entry(resource->entry, struct vme_master_resource, list);
0773 
0774     return bridge->master_rmw(image, mask, compare, swap, offset);
0775 }
0776 EXPORT_SYMBOL(vme_master_rmw);
0777 
0778 /**
0779  * vme_master_mmap - Mmap region of VME master window.
0780  * @resource: Pointer to VME master resource.
0781  * @vma: Pointer to definition of user mapping.
0782  *
0783  * Memory map a region of the VME master window into user space.
0784  *
0785  * Return: Zero on success, -EINVAL if resource is not a VME master
0786  *         resource or -EFAULT if map exceeds window size. Other generic mmap
0787  *         errors may also be returned.
0788  */
0789 int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
0790 {
0791     struct vme_master_resource *image;
0792     phys_addr_t phys_addr;
0793     unsigned long vma_size;
0794 
0795     if (resource->type != VME_MASTER) {
0796         pr_err("Not a master resource\n");
0797         return -EINVAL;
0798     }
0799 
0800     image = list_entry(resource->entry, struct vme_master_resource, list);
0801     phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
0802     vma_size = vma->vm_end - vma->vm_start;
0803 
0804     if (phys_addr + vma_size > image->bus_resource.end + 1) {
0805         pr_err("Map size cannot exceed the window size\n");
0806         return -EFAULT;
0807     }
0808 
0809     vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
0810 
0811     return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
0812 }
0813 EXPORT_SYMBOL(vme_master_mmap);
0814 
0815 /**
0816  * vme_master_free - Free VME master window
0817  * @resource: Pointer to VME master resource.
0818  *
0819  * Free the provided master resource so that it may be reallocated.
0820  */
0821 void vme_master_free(struct vme_resource *resource)
0822 {
0823     struct vme_master_resource *master_image;
0824 
0825     if (resource->type != VME_MASTER) {
0826         printk(KERN_ERR "Not a master resource\n");
0827         return;
0828     }
0829 
0830     master_image = list_entry(resource->entry, struct vme_master_resource,
0831         list);
0832     if (!master_image) {
0833         printk(KERN_ERR "Can't find master resource\n");
0834         return;
0835     }
0836 
0837     /* Unlock image */
0838     spin_lock(&master_image->lock);
0839     if (master_image->locked == 0)
0840         printk(KERN_ERR "Image is already free\n");
0841 
0842     master_image->locked = 0;
0843     spin_unlock(&master_image->lock);
0844 
0845     /* Free up resource memory */
0846     kfree(resource);
0847 }
0848 EXPORT_SYMBOL(vme_master_free);
0849 
0850 /**
0851  * vme_dma_request - Request a DMA controller.
0852  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
0853  * @route: Required src/destination combination.
0854  *
0855  * Request a VME DMA controller with capability to perform transfers bewteen
0856  * requested source/destination combination.
0857  *
0858  * Return: Pointer to VME DMA resource on success, NULL on failure.
0859  */
0860 struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
0861 {
0862     struct vme_bridge *bridge;
0863     struct list_head *dma_pos = NULL;
0864     struct vme_dma_resource *allocated_ctrlr = NULL;
0865     struct vme_dma_resource *dma_ctrlr = NULL;
0866     struct vme_resource *resource = NULL;
0867 
0868     /* XXX Not checking resource attributes */
0869     printk(KERN_ERR "No VME resource Attribute tests done\n");
0870 
0871     bridge = vdev->bridge;
0872     if (!bridge) {
0873         printk(KERN_ERR "Can't find VME bus\n");
0874         goto err_bus;
0875     }
0876 
0877     /* Loop through DMA resources */
0878     list_for_each(dma_pos, &bridge->dma_resources) {
0879         dma_ctrlr = list_entry(dma_pos,
0880             struct vme_dma_resource, list);
0881         if (!dma_ctrlr) {
0882             printk(KERN_ERR "Registered NULL DMA resource\n");
0883             continue;
0884         }
0885 
0886         /* Find an unlocked and compatible controller */
0887         mutex_lock(&dma_ctrlr->mtx);
0888         if (((dma_ctrlr->route_attr & route) == route) &&
0889             (dma_ctrlr->locked == 0)) {
0890 
0891             dma_ctrlr->locked = 1;
0892             mutex_unlock(&dma_ctrlr->mtx);
0893             allocated_ctrlr = dma_ctrlr;
0894             break;
0895         }
0896         mutex_unlock(&dma_ctrlr->mtx);
0897     }
0898 
0899     /* Check to see if we found a resource */
0900     if (!allocated_ctrlr)
0901         goto err_ctrlr;
0902 
0903     resource = kmalloc(sizeof(*resource), GFP_KERNEL);
0904     if (!resource)
0905         goto err_alloc;
0906 
0907     resource->type = VME_DMA;
0908     resource->entry = &allocated_ctrlr->list;
0909 
0910     return resource;
0911 
0912 err_alloc:
0913     /* Unlock image */
0914     mutex_lock(&dma_ctrlr->mtx);
0915     dma_ctrlr->locked = 0;
0916     mutex_unlock(&dma_ctrlr->mtx);
0917 err_ctrlr:
0918 err_bus:
0919     return NULL;
0920 }
0921 EXPORT_SYMBOL(vme_dma_request);
0922 
0923 /**
0924  * vme_new_dma_list - Create new VME DMA list.
0925  * @resource: Pointer to VME DMA resource.
0926  *
0927  * Create a new VME DMA list. It is the responsibility of the user to free
0928  * the list once it is no longer required with vme_dma_list_free().
0929  *
0930  * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
0931  *         VME DMA resource.
0932  */
0933 struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
0934 {
0935     struct vme_dma_list *dma_list;
0936 
0937     if (resource->type != VME_DMA) {
0938         printk(KERN_ERR "Not a DMA resource\n");
0939         return NULL;
0940     }
0941 
0942     dma_list = kmalloc(sizeof(*dma_list), GFP_KERNEL);
0943     if (!dma_list)
0944         return NULL;
0945 
0946     INIT_LIST_HEAD(&dma_list->entries);
0947     dma_list->parent = list_entry(resource->entry,
0948                       struct vme_dma_resource,
0949                       list);
0950     mutex_init(&dma_list->mtx);
0951 
0952     return dma_list;
0953 }
0954 EXPORT_SYMBOL(vme_new_dma_list);
0955 
0956 /**
0957  * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
0958  * @pattern: Value to use used as pattern
0959  * @type: Type of pattern to be written.
0960  *
0961  * Create VME DMA list attribute for pattern generation. It is the
0962  * responsibility of the user to free used attributes using
0963  * vme_dma_free_attribute().
0964  *
0965  * Return: Pointer to VME DMA attribute, NULL on failure.
0966  */
0967 struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
0968 {
0969     struct vme_dma_attr *attributes;
0970     struct vme_dma_pattern *pattern_attr;
0971 
0972     attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
0973     if (!attributes)
0974         goto err_attr;
0975 
0976     pattern_attr = kmalloc(sizeof(*pattern_attr), GFP_KERNEL);
0977     if (!pattern_attr)
0978         goto err_pat;
0979 
0980     attributes->type = VME_DMA_PATTERN;
0981     attributes->private = (void *)pattern_attr;
0982 
0983     pattern_attr->pattern = pattern;
0984     pattern_attr->type = type;
0985 
0986     return attributes;
0987 
0988 err_pat:
0989     kfree(attributes);
0990 err_attr:
0991     return NULL;
0992 }
0993 EXPORT_SYMBOL(vme_dma_pattern_attribute);
0994 
0995 /**
0996  * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
0997  * @address: PCI base address for DMA transfer.
0998  *
0999  * Create VME DMA list attribute pointing to a location on PCI for DMA
1000  * transfers. It is the responsibility of the user to free used attributes
1001  * using vme_dma_free_attribute().
1002  *
1003  * Return: Pointer to VME DMA attribute, NULL on failure.
1004  */
1005 struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
1006 {
1007     struct vme_dma_attr *attributes;
1008     struct vme_dma_pci *pci_attr;
1009 
1010     /* XXX Run some sanity checks here */
1011 
1012     attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
1013     if (!attributes)
1014         goto err_attr;
1015 
1016     pci_attr = kmalloc(sizeof(*pci_attr), GFP_KERNEL);
1017     if (!pci_attr)
1018         goto err_pci;
1019 
1020     attributes->type = VME_DMA_PCI;
1021     attributes->private = (void *)pci_attr;
1022 
1023     pci_attr->address = address;
1024 
1025     return attributes;
1026 
1027 err_pci:
1028     kfree(attributes);
1029 err_attr:
1030     return NULL;
1031 }
1032 EXPORT_SYMBOL(vme_dma_pci_attribute);
1033 
1034 /**
1035  * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
1036  * @address: VME base address for DMA transfer.
1037  * @aspace: VME address space to use for DMA transfer.
1038  * @cycle: VME bus cycle to use for DMA transfer.
1039  * @dwidth: VME data width to use for DMA transfer.
1040  *
1041  * Create VME DMA list attribute pointing to a location on the VME bus for DMA
1042  * transfers. It is the responsibility of the user to free used attributes
1043  * using vme_dma_free_attribute().
1044  *
1045  * Return: Pointer to VME DMA attribute, NULL on failure.
1046  */
1047 struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
1048     u32 aspace, u32 cycle, u32 dwidth)
1049 {
1050     struct vme_dma_attr *attributes;
1051     struct vme_dma_vme *vme_attr;
1052 
1053     attributes = kmalloc(sizeof(*attributes), GFP_KERNEL);
1054     if (!attributes)
1055         goto err_attr;
1056 
1057     vme_attr = kmalloc(sizeof(*vme_attr), GFP_KERNEL);
1058     if (!vme_attr)
1059         goto err_vme;
1060 
1061     attributes->type = VME_DMA_VME;
1062     attributes->private = (void *)vme_attr;
1063 
1064     vme_attr->address = address;
1065     vme_attr->aspace = aspace;
1066     vme_attr->cycle = cycle;
1067     vme_attr->dwidth = dwidth;
1068 
1069     return attributes;
1070 
1071 err_vme:
1072     kfree(attributes);
1073 err_attr:
1074     return NULL;
1075 }
1076 EXPORT_SYMBOL(vme_dma_vme_attribute);
1077 
1078 /**
1079  * vme_dma_free_attribute - Free DMA list attribute.
1080  * @attributes: Pointer to DMA list attribute.
1081  *
1082  * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1083  * once vme_dma_list_add() has returned.
1084  */
1085 void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1086 {
1087     kfree(attributes->private);
1088     kfree(attributes);
1089 }
1090 EXPORT_SYMBOL(vme_dma_free_attribute);
1091 
1092 /**
1093  * vme_dma_list_add - Add enty to a VME DMA list.
1094  * @list: Pointer to VME list.
1095  * @src: Pointer to DMA list attribute to use as source.
1096  * @dest: Pointer to DMA list attribute to use as destination.
1097  * @count: Number of bytes to transfer.
1098  *
1099  * Add an entry to the provided VME DMA list. Entry requires pointers to source
1100  * and destination DMA attributes and a count.
1101  *
1102  * Please note, the attributes supported as source and destinations for
1103  * transfers are hardware dependent.
1104  *
1105  * Return: Zero on success, -EINVAL if operation is not supported on this
1106  *         device or if the link list has already been submitted for execution.
1107  *         Hardware specific errors also possible.
1108  */
1109 int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
1110     struct vme_dma_attr *dest, size_t count)
1111 {
1112     struct vme_bridge *bridge = list->parent->parent;
1113     int retval;
1114 
1115     if (!bridge->dma_list_add) {
1116         printk(KERN_WARNING "Link List DMA generation not supported\n");
1117         return -EINVAL;
1118     }
1119 
1120     if (!mutex_trylock(&list->mtx)) {
1121         printk(KERN_ERR "Link List already submitted\n");
1122         return -EINVAL;
1123     }
1124 
1125     retval = bridge->dma_list_add(list, src, dest, count);
1126 
1127     mutex_unlock(&list->mtx);
1128 
1129     return retval;
1130 }
1131 EXPORT_SYMBOL(vme_dma_list_add);
1132 
1133 /**
1134  * vme_dma_list_exec - Queue a VME DMA list for execution.
1135  * @list: Pointer to VME list.
1136  *
1137  * Queue the provided VME DMA list for execution. The call will return once the
1138  * list has been executed.
1139  *
1140  * Return: Zero on success, -EINVAL if operation is not supported on this
1141  *         device. Hardware specific errors also possible.
1142  */
1143 int vme_dma_list_exec(struct vme_dma_list *list)
1144 {
1145     struct vme_bridge *bridge = list->parent->parent;
1146     int retval;
1147 
1148     if (!bridge->dma_list_exec) {
1149         printk(KERN_ERR "Link List DMA execution not supported\n");
1150         return -EINVAL;
1151     }
1152 
1153     mutex_lock(&list->mtx);
1154 
1155     retval = bridge->dma_list_exec(list);
1156 
1157     mutex_unlock(&list->mtx);
1158 
1159     return retval;
1160 }
1161 EXPORT_SYMBOL(vme_dma_list_exec);
1162 
1163 /**
1164  * vme_dma_list_free - Free a VME DMA list.
1165  * @list: Pointer to VME list.
1166  *
1167  * Free the provided DMA list and all its entries.
1168  *
1169  * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1170  *         is still in use. Hardware specific errors also possible.
1171  */
1172 int vme_dma_list_free(struct vme_dma_list *list)
1173 {
1174     struct vme_bridge *bridge = list->parent->parent;
1175     int retval;
1176 
1177     if (!bridge->dma_list_empty) {
1178         printk(KERN_WARNING "Emptying of Link Lists not supported\n");
1179         return -EINVAL;
1180     }
1181 
1182     if (!mutex_trylock(&list->mtx)) {
1183         printk(KERN_ERR "Link List in use\n");
1184         return -EBUSY;
1185     }
1186 
1187     /*
1188      * Empty out all of the entries from the DMA list. We need to go to the
1189      * low level driver as DMA entries are driver specific.
1190      */
1191     retval = bridge->dma_list_empty(list);
1192     if (retval) {
1193         printk(KERN_ERR "Unable to empty link-list entries\n");
1194         mutex_unlock(&list->mtx);
1195         return retval;
1196     }
1197     mutex_unlock(&list->mtx);
1198     kfree(list);
1199 
1200     return retval;
1201 }
1202 EXPORT_SYMBOL(vme_dma_list_free);
1203 
1204 /**
1205  * vme_dma_free - Free a VME DMA resource.
1206  * @resource: Pointer to VME DMA resource.
1207  *
1208  * Free the provided DMA resource so that it may be reallocated.
1209  *
1210  * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1211  *         is still active.
1212  */
1213 int vme_dma_free(struct vme_resource *resource)
1214 {
1215     struct vme_dma_resource *ctrlr;
1216 
1217     if (resource->type != VME_DMA) {
1218         printk(KERN_ERR "Not a DMA resource\n");
1219         return -EINVAL;
1220     }
1221 
1222     ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1223 
1224     if (!mutex_trylock(&ctrlr->mtx)) {
1225         printk(KERN_ERR "Resource busy, can't free\n");
1226         return -EBUSY;
1227     }
1228 
1229     if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
1230         printk(KERN_WARNING "Resource still processing transfers\n");
1231         mutex_unlock(&ctrlr->mtx);
1232         return -EBUSY;
1233     }
1234 
1235     ctrlr->locked = 0;
1236 
1237     mutex_unlock(&ctrlr->mtx);
1238 
1239     kfree(resource);
1240 
1241     return 0;
1242 }
1243 EXPORT_SYMBOL(vme_dma_free);
1244 
1245 void vme_bus_error_handler(struct vme_bridge *bridge,
1246                unsigned long long address, int am)
1247 {
1248     struct list_head *handler_pos = NULL;
1249     struct vme_error_handler *handler;
1250     int handler_triggered = 0;
1251     u32 aspace = vme_get_aspace(am);
1252 
1253     list_for_each(handler_pos, &bridge->vme_error_handlers) {
1254         handler = list_entry(handler_pos, struct vme_error_handler,
1255                      list);
1256         if ((aspace == handler->aspace) &&
1257             (address >= handler->start) &&
1258             (address < handler->end)) {
1259             if (!handler->num_errors)
1260                 handler->first_error = address;
1261             if (handler->num_errors != UINT_MAX)
1262                 handler->num_errors++;
1263             handler_triggered = 1;
1264         }
1265     }
1266 
1267     if (!handler_triggered)
1268         dev_err(bridge->parent,
1269             "Unhandled VME access error at address 0x%llx\n",
1270             address);
1271 }
1272 EXPORT_SYMBOL(vme_bus_error_handler);
1273 
1274 struct vme_error_handler *vme_register_error_handler(
1275     struct vme_bridge *bridge, u32 aspace,
1276     unsigned long long address, size_t len)
1277 {
1278     struct vme_error_handler *handler;
1279 
1280     handler = kmalloc(sizeof(*handler), GFP_ATOMIC);
1281     if (!handler)
1282         return NULL;
1283 
1284     handler->aspace = aspace;
1285     handler->start = address;
1286     handler->end = address + len;
1287     handler->num_errors = 0;
1288     handler->first_error = 0;
1289     list_add_tail(&handler->list, &bridge->vme_error_handlers);
1290 
1291     return handler;
1292 }
1293 EXPORT_SYMBOL(vme_register_error_handler);
1294 
1295 void vme_unregister_error_handler(struct vme_error_handler *handler)
1296 {
1297     list_del(&handler->list);
1298     kfree(handler);
1299 }
1300 EXPORT_SYMBOL(vme_unregister_error_handler);
1301 
1302 void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1303 {
1304     void (*call)(int, int, void *);
1305     void *priv_data;
1306 
1307     call = bridge->irq[level - 1].callback[statid].func;
1308     priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1309     if (call)
1310         call(level, statid, priv_data);
1311     else
1312         printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
1313                level, statid);
1314 }
1315 EXPORT_SYMBOL(vme_irq_handler);
1316 
1317 /**
1318  * vme_irq_request - Request a specific VME interrupt.
1319  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1320  * @level: Interrupt priority being requested.
1321  * @statid: Interrupt vector being requested.
1322  * @callback: Pointer to callback function called when VME interrupt/vector
1323  *            received.
1324  * @priv_data: Generic pointer that will be passed to the callback function.
1325  *
1326  * Request callback to be attached as a handler for VME interrupts with provided
1327  * level and statid.
1328  *
1329  * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1330  *         function is not supported, -EBUSY if the level/statid combination is
1331  *         already in use. Hardware specific errors also possible.
1332  */
1333 int vme_irq_request(struct vme_dev *vdev, int level, int statid,
1334     void (*callback)(int, int, void *),
1335     void *priv_data)
1336 {
1337     struct vme_bridge *bridge;
1338 
1339     bridge = vdev->bridge;
1340     if (!bridge) {
1341         printk(KERN_ERR "Can't find VME bus\n");
1342         return -EINVAL;
1343     }
1344 
1345     if ((level < 1) || (level > 7)) {
1346         printk(KERN_ERR "Invalid interrupt level\n");
1347         return -EINVAL;
1348     }
1349 
1350     if (!bridge->irq_set) {
1351         printk(KERN_ERR "Configuring interrupts not supported\n");
1352         return -EINVAL;
1353     }
1354 
1355     mutex_lock(&bridge->irq_mtx);
1356 
1357     if (bridge->irq[level - 1].callback[statid].func) {
1358         mutex_unlock(&bridge->irq_mtx);
1359         printk(KERN_WARNING "VME Interrupt already taken\n");
1360         return -EBUSY;
1361     }
1362 
1363     bridge->irq[level - 1].count++;
1364     bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1365     bridge->irq[level - 1].callback[statid].func = callback;
1366 
1367     /* Enable IRQ level */
1368     bridge->irq_set(bridge, level, 1, 1);
1369 
1370     mutex_unlock(&bridge->irq_mtx);
1371 
1372     return 0;
1373 }
1374 EXPORT_SYMBOL(vme_irq_request);
1375 
1376 /**
1377  * vme_irq_free - Free a VME interrupt.
1378  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1379  * @level: Interrupt priority of interrupt being freed.
1380  * @statid: Interrupt vector of interrupt being freed.
1381  *
1382  * Remove previously attached callback from VME interrupt priority/vector.
1383  */
1384 void vme_irq_free(struct vme_dev *vdev, int level, int statid)
1385 {
1386     struct vme_bridge *bridge;
1387 
1388     bridge = vdev->bridge;
1389     if (!bridge) {
1390         printk(KERN_ERR "Can't find VME bus\n");
1391         return;
1392     }
1393 
1394     if ((level < 1) || (level > 7)) {
1395         printk(KERN_ERR "Invalid interrupt level\n");
1396         return;
1397     }
1398 
1399     if (!bridge->irq_set) {
1400         printk(KERN_ERR "Configuring interrupts not supported\n");
1401         return;
1402     }
1403 
1404     mutex_lock(&bridge->irq_mtx);
1405 
1406     bridge->irq[level - 1].count--;
1407 
1408     /* Disable IRQ level if no more interrupts attached at this level*/
1409     if (bridge->irq[level - 1].count == 0)
1410         bridge->irq_set(bridge, level, 0, 1);
1411 
1412     bridge->irq[level - 1].callback[statid].func = NULL;
1413     bridge->irq[level - 1].callback[statid].priv_data = NULL;
1414 
1415     mutex_unlock(&bridge->irq_mtx);
1416 }
1417 EXPORT_SYMBOL(vme_irq_free);
1418 
1419 /**
1420  * vme_irq_generate - Generate VME interrupt.
1421  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1422  * @level: Interrupt priority at which to assert the interrupt.
1423  * @statid: Interrupt vector to associate with the interrupt.
1424  *
1425  * Generate a VME interrupt of the provided level and with the provided
1426  * statid.
1427  *
1428  * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1429  *         function is not supported. Hardware specific errors also possible.
1430  */
1431 int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
1432 {
1433     struct vme_bridge *bridge;
1434 
1435     bridge = vdev->bridge;
1436     if (!bridge) {
1437         printk(KERN_ERR "Can't find VME bus\n");
1438         return -EINVAL;
1439     }
1440 
1441     if ((level < 1) || (level > 7)) {
1442         printk(KERN_WARNING "Invalid interrupt level\n");
1443         return -EINVAL;
1444     }
1445 
1446     if (!bridge->irq_generate) {
1447         printk(KERN_WARNING "Interrupt generation not supported\n");
1448         return -EINVAL;
1449     }
1450 
1451     return bridge->irq_generate(bridge, level, statid);
1452 }
1453 EXPORT_SYMBOL(vme_irq_generate);
1454 
1455 /**
1456  * vme_lm_request - Request a VME location monitor
1457  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1458  *
1459  * Allocate a location monitor resource to the driver. A location monitor
1460  * allows the driver to monitor accesses to a contiguous number of
1461  * addresses on the VME bus.
1462  *
1463  * Return: Pointer to a VME resource on success or NULL on failure.
1464  */
1465 struct vme_resource *vme_lm_request(struct vme_dev *vdev)
1466 {
1467     struct vme_bridge *bridge;
1468     struct list_head *lm_pos = NULL;
1469     struct vme_lm_resource *allocated_lm = NULL;
1470     struct vme_lm_resource *lm = NULL;
1471     struct vme_resource *resource = NULL;
1472 
1473     bridge = vdev->bridge;
1474     if (!bridge) {
1475         printk(KERN_ERR "Can't find VME bus\n");
1476         goto err_bus;
1477     }
1478 
1479     /* Loop through LM resources */
1480     list_for_each(lm_pos, &bridge->lm_resources) {
1481         lm = list_entry(lm_pos,
1482             struct vme_lm_resource, list);
1483         if (!lm) {
1484             printk(KERN_ERR "Registered NULL Location Monitor resource\n");
1485             continue;
1486         }
1487 
1488         /* Find an unlocked controller */
1489         mutex_lock(&lm->mtx);
1490         if (lm->locked == 0) {
1491             lm->locked = 1;
1492             mutex_unlock(&lm->mtx);
1493             allocated_lm = lm;
1494             break;
1495         }
1496         mutex_unlock(&lm->mtx);
1497     }
1498 
1499     /* Check to see if we found a resource */
1500     if (!allocated_lm)
1501         goto err_lm;
1502 
1503     resource = kmalloc(sizeof(*resource), GFP_KERNEL);
1504     if (!resource)
1505         goto err_alloc;
1506 
1507     resource->type = VME_LM;
1508     resource->entry = &allocated_lm->list;
1509 
1510     return resource;
1511 
1512 err_alloc:
1513     /* Unlock image */
1514     mutex_lock(&lm->mtx);
1515     lm->locked = 0;
1516     mutex_unlock(&lm->mtx);
1517 err_lm:
1518 err_bus:
1519     return NULL;
1520 }
1521 EXPORT_SYMBOL(vme_lm_request);
1522 
1523 /**
1524  * vme_lm_count - Determine number of VME Addresses monitored
1525  * @resource: Pointer to VME location monitor resource.
1526  *
1527  * The number of contiguous addresses monitored is hardware dependent.
1528  * Return the number of contiguous addresses monitored by the
1529  * location monitor.
1530  *
1531  * Return: Count of addresses monitored or -EINVAL when provided with an
1532  *     invalid location monitor resource.
1533  */
1534 int vme_lm_count(struct vme_resource *resource)
1535 {
1536     struct vme_lm_resource *lm;
1537 
1538     if (resource->type != VME_LM) {
1539         printk(KERN_ERR "Not a Location Monitor resource\n");
1540         return -EINVAL;
1541     }
1542 
1543     lm = list_entry(resource->entry, struct vme_lm_resource, list);
1544 
1545     return lm->monitors;
1546 }
1547 EXPORT_SYMBOL(vme_lm_count);
1548 
1549 /**
1550  * vme_lm_set - Configure location monitor
1551  * @resource: Pointer to VME location monitor resource.
1552  * @lm_base: Base address to monitor.
1553  * @aspace: VME address space to monitor.
1554  * @cycle: VME bus cycle type to monitor.
1555  *
1556  * Set the base address, address space and cycle type of accesses to be
1557  * monitored by the location monitor.
1558  *
1559  * Return: Zero on success, -EINVAL when provided with an invalid location
1560  *     monitor resource or function is not supported. Hardware specific
1561  *     errors may also be returned.
1562  */
1563 int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
1564     u32 aspace, u32 cycle)
1565 {
1566     struct vme_bridge *bridge = find_bridge(resource);
1567     struct vme_lm_resource *lm;
1568 
1569     if (resource->type != VME_LM) {
1570         printk(KERN_ERR "Not a Location Monitor resource\n");
1571         return -EINVAL;
1572     }
1573 
1574     lm = list_entry(resource->entry, struct vme_lm_resource, list);
1575 
1576     if (!bridge->lm_set) {
1577         printk(KERN_ERR "vme_lm_set not supported\n");
1578         return -EINVAL;
1579     }
1580 
1581     return bridge->lm_set(lm, lm_base, aspace, cycle);
1582 }
1583 EXPORT_SYMBOL(vme_lm_set);
1584 
1585 /**
1586  * vme_lm_get - Retrieve location monitor settings
1587  * @resource: Pointer to VME location monitor resource.
1588  * @lm_base: Pointer used to output the base address monitored.
1589  * @aspace: Pointer used to output the address space monitored.
1590  * @cycle: Pointer used to output the VME bus cycle type monitored.
1591  *
1592  * Retrieve the base address, address space and cycle type of accesses to
1593  * be monitored by the location monitor.
1594  *
1595  * Return: Zero on success, -EINVAL when provided with an invalid location
1596  *     monitor resource or function is not supported. Hardware specific
1597  *     errors may also be returned.
1598  */
1599 int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
1600     u32 *aspace, u32 *cycle)
1601 {
1602     struct vme_bridge *bridge = find_bridge(resource);
1603     struct vme_lm_resource *lm;
1604 
1605     if (resource->type != VME_LM) {
1606         printk(KERN_ERR "Not a Location Monitor resource\n");
1607         return -EINVAL;
1608     }
1609 
1610     lm = list_entry(resource->entry, struct vme_lm_resource, list);
1611 
1612     if (!bridge->lm_get) {
1613         printk(KERN_ERR "vme_lm_get not supported\n");
1614         return -EINVAL;
1615     }
1616 
1617     return bridge->lm_get(lm, lm_base, aspace, cycle);
1618 }
1619 EXPORT_SYMBOL(vme_lm_get);
1620 
1621 /**
1622  * vme_lm_attach - Provide callback for location monitor address
1623  * @resource: Pointer to VME location monitor resource.
1624  * @monitor: Offset to which callback should be attached.
1625  * @callback: Pointer to callback function called when triggered.
1626  * @data: Generic pointer that will be passed to the callback function.
1627  *
1628  * Attach a callback to the specificed offset into the location monitors
1629  * monitored addresses. A generic pointer is provided to allow data to be
1630  * passed to the callback when called.
1631  *
1632  * Return: Zero on success, -EINVAL when provided with an invalid location
1633  *     monitor resource or function is not supported. Hardware specific
1634  *     errors may also be returned.
1635  */
1636 int vme_lm_attach(struct vme_resource *resource, int monitor,
1637     void (*callback)(void *), void *data)
1638 {
1639     struct vme_bridge *bridge = find_bridge(resource);
1640     struct vme_lm_resource *lm;
1641 
1642     if (resource->type != VME_LM) {
1643         printk(KERN_ERR "Not a Location Monitor resource\n");
1644         return -EINVAL;
1645     }
1646 
1647     lm = list_entry(resource->entry, struct vme_lm_resource, list);
1648 
1649     if (!bridge->lm_attach) {
1650         printk(KERN_ERR "vme_lm_attach not supported\n");
1651         return -EINVAL;
1652     }
1653 
1654     return bridge->lm_attach(lm, monitor, callback, data);
1655 }
1656 EXPORT_SYMBOL(vme_lm_attach);
1657 
1658 /**
1659  * vme_lm_detach - Remove callback for location monitor address
1660  * @resource: Pointer to VME location monitor resource.
1661  * @monitor: Offset to which callback should be removed.
1662  *
1663  * Remove the callback associated with the specificed offset into the
1664  * location monitors monitored addresses.
1665  *
1666  * Return: Zero on success, -EINVAL when provided with an invalid location
1667  *     monitor resource or function is not supported. Hardware specific
1668  *     errors may also be returned.
1669  */
1670 int vme_lm_detach(struct vme_resource *resource, int monitor)
1671 {
1672     struct vme_bridge *bridge = find_bridge(resource);
1673     struct vme_lm_resource *lm;
1674 
1675     if (resource->type != VME_LM) {
1676         printk(KERN_ERR "Not a Location Monitor resource\n");
1677         return -EINVAL;
1678     }
1679 
1680     lm = list_entry(resource->entry, struct vme_lm_resource, list);
1681 
1682     if (!bridge->lm_detach) {
1683         printk(KERN_ERR "vme_lm_detach not supported\n");
1684         return -EINVAL;
1685     }
1686 
1687     return bridge->lm_detach(lm, monitor);
1688 }
1689 EXPORT_SYMBOL(vme_lm_detach);
1690 
1691 /**
1692  * vme_lm_free - Free allocated VME location monitor
1693  * @resource: Pointer to VME location monitor resource.
1694  *
1695  * Free allocation of a VME location monitor.
1696  *
1697  * WARNING: This function currently expects that any callbacks that have
1698  *          been attached to the location monitor have been removed.
1699  *
1700  * Return: Zero on success, -EINVAL when provided with an invalid location
1701  *     monitor resource.
1702  */
1703 void vme_lm_free(struct vme_resource *resource)
1704 {
1705     struct vme_lm_resource *lm;
1706 
1707     if (resource->type != VME_LM) {
1708         printk(KERN_ERR "Not a Location Monitor resource\n");
1709         return;
1710     }
1711 
1712     lm = list_entry(resource->entry, struct vme_lm_resource, list);
1713 
1714     mutex_lock(&lm->mtx);
1715 
1716     /* XXX
1717      * Check to see that there aren't any callbacks still attached, if
1718      * there are we should probably be detaching them!
1719      */
1720 
1721     lm->locked = 0;
1722 
1723     mutex_unlock(&lm->mtx);
1724 
1725     kfree(resource);
1726 }
1727 EXPORT_SYMBOL(vme_lm_free);
1728 
1729 /**
1730  * vme_slot_num - Retrieve slot ID
1731  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1732  *
1733  * Retrieve the slot ID associated with the provided VME device.
1734  *
1735  * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1736  *         or the function is not supported. Hardware specific errors may also
1737  *         be returned.
1738  */
1739 int vme_slot_num(struct vme_dev *vdev)
1740 {
1741     struct vme_bridge *bridge;
1742 
1743     bridge = vdev->bridge;
1744     if (!bridge) {
1745         printk(KERN_ERR "Can't find VME bus\n");
1746         return -EINVAL;
1747     }
1748 
1749     if (!bridge->slot_get) {
1750         printk(KERN_WARNING "vme_slot_num not supported\n");
1751         return -EINVAL;
1752     }
1753 
1754     return bridge->slot_get(bridge);
1755 }
1756 EXPORT_SYMBOL(vme_slot_num);
1757 
1758 /**
1759  * vme_bus_num - Retrieve bus number
1760  * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1761  *
1762  * Retrieve the bus enumeration associated with the provided VME device.
1763  *
1764  * Return: The bus number on success, -EINVAL if VME bridge cannot be
1765  *         determined.
1766  */
1767 int vme_bus_num(struct vme_dev *vdev)
1768 {
1769     struct vme_bridge *bridge;
1770 
1771     bridge = vdev->bridge;
1772     if (!bridge) {
1773         pr_err("Can't find VME bus\n");
1774         return -EINVAL;
1775     }
1776 
1777     return bridge->num;
1778 }
1779 EXPORT_SYMBOL(vme_bus_num);
1780 
1781 /* - Bridge Registration --------------------------------------------------- */
1782 
1783 static void vme_dev_release(struct device *dev)
1784 {
1785     kfree(dev_to_vme_dev(dev));
1786 }
1787 
1788 /* Common bridge initialization */
1789 struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
1790 {
1791     INIT_LIST_HEAD(&bridge->vme_error_handlers);
1792     INIT_LIST_HEAD(&bridge->master_resources);
1793     INIT_LIST_HEAD(&bridge->slave_resources);
1794     INIT_LIST_HEAD(&bridge->dma_resources);
1795     INIT_LIST_HEAD(&bridge->lm_resources);
1796     mutex_init(&bridge->irq_mtx);
1797 
1798     return bridge;
1799 }
1800 EXPORT_SYMBOL(vme_init_bridge);
1801 
1802 int vme_register_bridge(struct vme_bridge *bridge)
1803 {
1804     int i;
1805     int ret = -1;
1806 
1807     mutex_lock(&vme_buses_lock);
1808     for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
1809         if ((vme_bus_numbers & (1 << i)) == 0) {
1810             vme_bus_numbers |= (1 << i);
1811             bridge->num = i;
1812             INIT_LIST_HEAD(&bridge->devices);
1813             list_add_tail(&bridge->bus_list, &vme_bus_list);
1814             ret = 0;
1815             break;
1816         }
1817     }
1818     mutex_unlock(&vme_buses_lock);
1819 
1820     return ret;
1821 }
1822 EXPORT_SYMBOL(vme_register_bridge);
1823 
1824 void vme_unregister_bridge(struct vme_bridge *bridge)
1825 {
1826     struct vme_dev *vdev;
1827     struct vme_dev *tmp;
1828 
1829     mutex_lock(&vme_buses_lock);
1830     vme_bus_numbers &= ~(1 << bridge->num);
1831     list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1832         list_del(&vdev->drv_list);
1833         list_del(&vdev->bridge_list);
1834         device_unregister(&vdev->dev);
1835     }
1836     list_del(&bridge->bus_list);
1837     mutex_unlock(&vme_buses_lock);
1838 }
1839 EXPORT_SYMBOL(vme_unregister_bridge);
1840 
1841 /* - Driver Registration --------------------------------------------------- */
1842 
1843 static int __vme_register_driver_bus(struct vme_driver *drv,
1844     struct vme_bridge *bridge, unsigned int ndevs)
1845 {
1846     int err;
1847     unsigned int i;
1848     struct vme_dev *vdev;
1849     struct vme_dev *tmp;
1850 
1851     for (i = 0; i < ndevs; i++) {
1852         vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1853         if (!vdev) {
1854             err = -ENOMEM;
1855             goto err_devalloc;
1856         }
1857         vdev->num = i;
1858         vdev->bridge = bridge;
1859         vdev->dev.platform_data = drv;
1860         vdev->dev.release = vme_dev_release;
1861         vdev->dev.parent = bridge->parent;
1862         vdev->dev.bus = &vme_bus_type;
1863         dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1864             vdev->num);
1865 
1866         err = device_register(&vdev->dev);
1867         if (err)
1868             goto err_reg;
1869 
1870         if (vdev->dev.platform_data) {
1871             list_add_tail(&vdev->drv_list, &drv->devices);
1872             list_add_tail(&vdev->bridge_list, &bridge->devices);
1873         } else
1874             device_unregister(&vdev->dev);
1875     }
1876     return 0;
1877 
1878 err_reg:
1879     put_device(&vdev->dev);
1880 err_devalloc:
1881     list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1882         list_del(&vdev->drv_list);
1883         list_del(&vdev->bridge_list);
1884         device_unregister(&vdev->dev);
1885     }
1886     return err;
1887 }
1888 
1889 static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1890 {
1891     struct vme_bridge *bridge;
1892     int err = 0;
1893 
1894     mutex_lock(&vme_buses_lock);
1895     list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1896         /*
1897          * This cannot cause trouble as we already have vme_buses_lock
1898          * and if the bridge is removed, it will have to go through
1899          * vme_unregister_bridge() to do it (which calls remove() on
1900          * the bridge which in turn tries to acquire vme_buses_lock and
1901          * will have to wait).
1902          */
1903         err = __vme_register_driver_bus(drv, bridge, ndevs);
1904         if (err)
1905             break;
1906     }
1907     mutex_unlock(&vme_buses_lock);
1908     return err;
1909 }
1910 
1911 /**
1912  * vme_register_driver - Register a VME driver
1913  * @drv: Pointer to VME driver structure to register.
1914  * @ndevs: Maximum number of devices to allow to be enumerated.
1915  *
1916  * Register a VME device driver with the VME subsystem.
1917  *
1918  * Return: Zero on success, error value on registration failure.
1919  */
1920 int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1921 {
1922     int err;
1923 
1924     drv->driver.name = drv->name;
1925     drv->driver.bus = &vme_bus_type;
1926     INIT_LIST_HEAD(&drv->devices);
1927 
1928     err = driver_register(&drv->driver);
1929     if (err)
1930         return err;
1931 
1932     err = __vme_register_driver(drv, ndevs);
1933     if (err)
1934         driver_unregister(&drv->driver);
1935 
1936     return err;
1937 }
1938 EXPORT_SYMBOL(vme_register_driver);
1939 
1940 /**
1941  * vme_unregister_driver - Unregister a VME driver
1942  * @drv: Pointer to VME driver structure to unregister.
1943  *
1944  * Unregister a VME device driver from the VME subsystem.
1945  */
1946 void vme_unregister_driver(struct vme_driver *drv)
1947 {
1948     struct vme_dev *dev, *dev_tmp;
1949 
1950     mutex_lock(&vme_buses_lock);
1951     list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1952         list_del(&dev->drv_list);
1953         list_del(&dev->bridge_list);
1954         device_unregister(&dev->dev);
1955     }
1956     mutex_unlock(&vme_buses_lock);
1957 
1958     driver_unregister(&drv->driver);
1959 }
1960 EXPORT_SYMBOL(vme_unregister_driver);
1961 
1962 /* - Bus Registration ------------------------------------------------------ */
1963 
1964 static int vme_bus_match(struct device *dev, struct device_driver *drv)
1965 {
1966     struct vme_driver *vme_drv;
1967 
1968     vme_drv = container_of(drv, struct vme_driver, driver);
1969 
1970     if (dev->platform_data == vme_drv) {
1971         struct vme_dev *vdev = dev_to_vme_dev(dev);
1972 
1973         if (vme_drv->match && vme_drv->match(vdev))
1974             return 1;
1975 
1976         dev->platform_data = NULL;
1977     }
1978     return 0;
1979 }
1980 
1981 static int vme_bus_probe(struct device *dev)
1982 {
1983     struct vme_driver *driver;
1984     struct vme_dev *vdev = dev_to_vme_dev(dev);
1985 
1986     driver = dev->platform_data;
1987     if (driver->probe)
1988         return driver->probe(vdev);
1989 
1990     return -ENODEV;
1991 }
1992 
1993 static void vme_bus_remove(struct device *dev)
1994 {
1995     struct vme_driver *driver;
1996     struct vme_dev *vdev = dev_to_vme_dev(dev);
1997 
1998     driver = dev->platform_data;
1999     if (driver->remove)
2000         driver->remove(vdev);
2001 }
2002 
2003 struct bus_type vme_bus_type = {
2004     .name = "vme",
2005     .match = vme_bus_match,
2006     .probe = vme_bus_probe,
2007     .remove = vme_bus_remove,
2008 };
2009 EXPORT_SYMBOL(vme_bus_type);
2010 
2011 static int __init vme_init(void)
2012 {
2013     return bus_register(&vme_bus_type);
2014 }
2015 subsys_initcall(vme_init);