blob: 6803744978b21f15f7b1a76c02b535f9381d8503 [file] [log] [blame]
Martyn Welcha17a75e2009-07-31 09:28:17 +01001/*
2 * VME Bridge Framework
3 *
Martyn Welch66bd8db2010-02-18 15:12:52 +00004 * Author: Martyn Welch <martyn.welch@ge.com>
5 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
Martyn Welcha17a75e2009-07-31 09:28:17 +01006 *
7 * Based on work by Tom Armistead and Ajit Prem
8 * Copyright 2004 Motorola Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 */
15
Martyn Welcha17a75e2009-07-31 09:28:17 +010016#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/mm.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/pci.h>
23#include <linux/poll.h>
24#include <linux/highmem.h>
25#include <linux/interrupt.h>
26#include <linux/pagemap.h>
27#include <linux/device.h>
28#include <linux/dma-mapping.h>
29#include <linux/syscalls.h>
Martyn Welch400822f2009-08-11 16:20:22 +010030#include <linux/mutex.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010031#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Greg Kroah-Hartmandb3b9e92012-04-26 12:34:58 -070033#include <linux/vme.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010034
Martyn Welcha17a75e2009-07-31 09:28:17 +010035#include "vme_bridge.h"
36
Manohar Vanga733e3ef2011-08-12 12:30:48 +020037/* Bitmask and list of registered buses both protected by common mutex */
Martyn Welcha17a75e2009-07-31 09:28:17 +010038static unsigned int vme_bus_numbers;
Manohar Vanga733e3ef2011-08-12 12:30:48 +020039static LIST_HEAD(vme_bus_list);
40static DEFINE_MUTEX(vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +010041
Martyn Welchead1f3e2009-12-15 08:43:02 +000042static void __exit vme_exit(void);
43static int __init vme_init(void);
Martyn Welcha17a75e2009-07-31 09:28:17 +010044
Manohar Vanga8f966dc2011-09-26 11:27:15 +020045static struct vme_dev *dev_to_vme_dev(struct device *dev)
Martyn Welcha17a75e2009-07-31 09:28:17 +010046{
Manohar Vanga8f966dc2011-09-26 11:27:15 +020047 return container_of(dev, struct vme_dev, dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +010048}
49
50/*
51 * Find the bridge that the resource is associated with.
52 */
53static struct vme_bridge *find_bridge(struct vme_resource *resource)
54{
55 /* Get list to search */
56 switch (resource->type) {
57 case VME_MASTER:
58 return list_entry(resource->entry, struct vme_master_resource,
59 list)->parent;
60 break;
61 case VME_SLAVE:
62 return list_entry(resource->entry, struct vme_slave_resource,
63 list)->parent;
64 break;
65 case VME_DMA:
66 return list_entry(resource->entry, struct vme_dma_resource,
67 list)->parent;
68 break;
Martyn Welch42fb5032009-08-11 17:44:56 +010069 case VME_LM:
70 return list_entry(resource->entry, struct vme_lm_resource,
71 list)->parent;
72 break;
Martyn Welcha17a75e2009-07-31 09:28:17 +010073 default:
74 printk(KERN_ERR "Unknown resource type\n");
75 return NULL;
76 break;
77 }
78}
79
80/*
81 * Allocate a contiguous block of memory for use by the driver. This is used to
82 * create the buffers for the slave windows.
Martyn Welcha17a75e2009-07-31 09:28:17 +010083 */
Martyn Welchead1f3e2009-12-15 08:43:02 +000084void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
Martyn Welcha17a75e2009-07-31 09:28:17 +010085 dma_addr_t *dma)
86{
87 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +010088
Martyn Welchead1f3e2009-12-15 08:43:02 +000089 if (resource == NULL) {
90 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +010091 return NULL;
92 }
93
94 bridge = find_bridge(resource);
Martyn Welchead1f3e2009-12-15 08:43:02 +000095 if (bridge == NULL) {
96 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +010097 return NULL;
98 }
99
Martyn Welcha17a75e2009-07-31 09:28:17 +0100100 if (bridge->parent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700101 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100102 return NULL;
103 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100104
Manohar Vanga7f58f022011-08-10 11:33:46 +0200105 if (bridge->alloc_consistent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700106 printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
107 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200108 return NULL;
109 }
110
111 return bridge->alloc_consistent(bridge->parent, size, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100112}
113EXPORT_SYMBOL(vme_alloc_consistent);
114
115/*
116 * Free previously allocated contiguous block of memory.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100117 */
118void vme_free_consistent(struct vme_resource *resource, size_t size,
119 void *vaddr, dma_addr_t dma)
120{
121 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100122
Martyn Welchead1f3e2009-12-15 08:43:02 +0000123 if (resource == NULL) {
124 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100125 return;
126 }
127
128 bridge = find_bridge(resource);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000129 if (bridge == NULL) {
130 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100131 return;
132 }
133
Manohar Vanga7f58f022011-08-10 11:33:46 +0200134 if (bridge->parent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700135 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200136 return;
137 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100138
Manohar Vanga7f58f022011-08-10 11:33:46 +0200139 if (bridge->free_consistent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700140 printk(KERN_ERR "free_consistent not supported by bridge %s\n",
141 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200142 return;
143 }
144
145 bridge->free_consistent(bridge->parent, size, vaddr, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100146}
147EXPORT_SYMBOL(vme_free_consistent);
148
149size_t vme_get_size(struct vme_resource *resource)
150{
151 int enabled, retval;
152 unsigned long long base, size;
153 dma_addr_t buf_base;
Martyn Welch6af04b02011-12-01 17:06:29 +0000154 u32 aspace, cycle, dwidth;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100155
156 switch (resource->type) {
157 case VME_MASTER:
158 retval = vme_master_get(resource, &enabled, &base, &size,
159 &aspace, &cycle, &dwidth);
160
161 return size;
162 break;
163 case VME_SLAVE:
164 retval = vme_slave_get(resource, &enabled, &base, &size,
165 &buf_base, &aspace, &cycle);
166
167 return size;
168 break;
169 case VME_DMA:
170 return 0;
171 break;
172 default:
173 printk(KERN_ERR "Unknown resource type\n");
174 return 0;
175 break;
176 }
177}
178EXPORT_SYMBOL(vme_get_size);
179
Dmitry Kalinkinef73f882015-05-28 15:07:04 +0300180int vme_check_window(u32 aspace, unsigned long long vme_base,
181 unsigned long long size)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100182{
183 int retval = 0;
184
185 switch (aspace) {
186 case VME_A16:
187 if (((vme_base + size) > VME_A16_MAX) ||
188 (vme_base > VME_A16_MAX))
189 retval = -EFAULT;
190 break;
191 case VME_A24:
192 if (((vme_base + size) > VME_A24_MAX) ||
193 (vme_base > VME_A24_MAX))
194 retval = -EFAULT;
195 break;
196 case VME_A32:
197 if (((vme_base + size) > VME_A32_MAX) ||
198 (vme_base > VME_A32_MAX))
199 retval = -EFAULT;
200 break;
201 case VME_A64:
Dmitry Kalinkine7fd80c2015-05-28 15:07:03 +0300202 if ((size != 0) && (vme_base > U64_MAX + 1 - size))
203 retval = -EFAULT;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100204 break;
205 case VME_CRCSR:
206 if (((vme_base + size) > VME_CRCSR_MAX) ||
207 (vme_base > VME_CRCSR_MAX))
208 retval = -EFAULT;
209 break;
210 case VME_USER1:
211 case VME_USER2:
212 case VME_USER3:
213 case VME_USER4:
214 /* User Defined */
215 break;
216 default:
Martyn Welchead1f3e2009-12-15 08:43:02 +0000217 printk(KERN_ERR "Invalid address space\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100218 retval = -EINVAL;
219 break;
220 }
221
222 return retval;
223}
Dmitry Kalinkinef73f882015-05-28 15:07:04 +0300224EXPORT_SYMBOL(vme_check_window);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100225
226/*
227 * Request a slave image with specific attributes, return some unique
228 * identifier.
229 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000230struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
231 u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100232{
233 struct vme_bridge *bridge;
234 struct list_head *slave_pos = NULL;
235 struct vme_slave_resource *allocated_image = NULL;
236 struct vme_slave_resource *slave_image = NULL;
237 struct vme_resource *resource = NULL;
238
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200239 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100240 if (bridge == NULL) {
241 printk(KERN_ERR "Can't find VME bus\n");
242 goto err_bus;
243 }
244
245 /* Loop through slave resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000246 list_for_each(slave_pos, &bridge->slave_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100247 slave_image = list_entry(slave_pos,
248 struct vme_slave_resource, list);
249
250 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000251 printk(KERN_ERR "Registered NULL Slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100252 continue;
253 }
254
255 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000256 mutex_lock(&slave_image->mtx);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000257 if (((slave_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100258 ((slave_image->cycle_attr & cycle) == cycle) &&
259 (slave_image->locked == 0)) {
260
261 slave_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000262 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100263 allocated_image = slave_image;
264 break;
265 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000266 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100267 }
268
269 /* No free image */
270 if (allocated_image == NULL)
271 goto err_image;
272
273 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
274 if (resource == NULL) {
275 printk(KERN_WARNING "Unable to allocate resource structure\n");
276 goto err_alloc;
277 }
278 resource->type = VME_SLAVE;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000279 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100280
281 return resource;
282
283err_alloc:
284 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000285 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100286 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000287 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100288err_image:
289err_bus:
290 return NULL;
291}
292EXPORT_SYMBOL(vme_slave_request);
293
Martyn Welchead1f3e2009-12-15 08:43:02 +0000294int vme_slave_set(struct vme_resource *resource, int enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100295 unsigned long long vme_base, unsigned long long size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000296 dma_addr_t buf_base, u32 aspace, u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100297{
298 struct vme_bridge *bridge = find_bridge(resource);
299 struct vme_slave_resource *image;
300 int retval;
301
302 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000303 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100304 return -EINVAL;
305 }
306
307 image = list_entry(resource->entry, struct vme_slave_resource, list);
308
309 if (bridge->slave_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000310 printk(KERN_ERR "Function not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100311 return -ENOSYS;
312 }
313
Martyn Welchead1f3e2009-12-15 08:43:02 +0000314 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100315 ((image->cycle_attr & cycle) == cycle))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000316 printk(KERN_ERR "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100317 return -EINVAL;
318 }
319
320 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000321 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100322 return retval;
323
324 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
325 aspace, cycle);
326}
327EXPORT_SYMBOL(vme_slave_set);
328
Martyn Welchead1f3e2009-12-15 08:43:02 +0000329int vme_slave_get(struct vme_resource *resource, int *enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100330 unsigned long long *vme_base, unsigned long long *size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000331 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100332{
333 struct vme_bridge *bridge = find_bridge(resource);
334 struct vme_slave_resource *image;
335
336 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000337 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100338 return -EINVAL;
339 }
340
341 image = list_entry(resource->entry, struct vme_slave_resource, list);
342
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200343 if (bridge->slave_get == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000344 printk(KERN_ERR "vme_slave_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100345 return -EINVAL;
346 }
347
348 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
349 aspace, cycle);
350}
351EXPORT_SYMBOL(vme_slave_get);
352
353void vme_slave_free(struct vme_resource *resource)
354{
355 struct vme_slave_resource *slave_image;
356
357 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000358 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100359 return;
360 }
361
362 slave_image = list_entry(resource->entry, struct vme_slave_resource,
363 list);
364 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000365 printk(KERN_ERR "Can't find slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100366 return;
367 }
368
369 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000370 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100371 if (slave_image->locked == 0)
372 printk(KERN_ERR "Image is already free\n");
373
374 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000375 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100376
377 /* Free up resource memory */
378 kfree(resource);
379}
380EXPORT_SYMBOL(vme_slave_free);
381
382/*
383 * Request a master image with specific attributes, return some unique
384 * identifier.
385 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000386struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
387 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100388{
389 struct vme_bridge *bridge;
390 struct list_head *master_pos = NULL;
391 struct vme_master_resource *allocated_image = NULL;
392 struct vme_master_resource *master_image = NULL;
393 struct vme_resource *resource = NULL;
394
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200395 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100396 if (bridge == NULL) {
397 printk(KERN_ERR "Can't find VME bus\n");
398 goto err_bus;
399 }
400
401 /* Loop through master resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000402 list_for_each(master_pos, &bridge->master_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100403 master_image = list_entry(master_pos,
404 struct vme_master_resource, list);
405
406 if (master_image == NULL) {
407 printk(KERN_WARNING "Registered NULL master resource\n");
408 continue;
409 }
410
411 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000412 spin_lock(&master_image->lock);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000413 if (((master_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100414 ((master_image->cycle_attr & cycle) == cycle) &&
415 ((master_image->width_attr & dwidth) == dwidth) &&
416 (master_image->locked == 0)) {
417
418 master_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000419 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100420 allocated_image = master_image;
421 break;
422 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000423 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100424 }
425
426 /* Check to see if we found a resource */
427 if (allocated_image == NULL) {
428 printk(KERN_ERR "Can't find a suitable resource\n");
429 goto err_image;
430 }
431
432 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
433 if (resource == NULL) {
434 printk(KERN_ERR "Unable to allocate resource structure\n");
435 goto err_alloc;
436 }
437 resource->type = VME_MASTER;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000438 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100439
440 return resource;
441
Martyn Welcha17a75e2009-07-31 09:28:17 +0100442err_alloc:
443 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000444 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100445 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000446 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100447err_image:
448err_bus:
449 return NULL;
450}
451EXPORT_SYMBOL(vme_master_request);
452
Martyn Welchead1f3e2009-12-15 08:43:02 +0000453int vme_master_set(struct vme_resource *resource, int enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000454 unsigned long long vme_base, unsigned long long size, u32 aspace,
455 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100456{
457 struct vme_bridge *bridge = find_bridge(resource);
458 struct vme_master_resource *image;
459 int retval;
460
461 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000462 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100463 return -EINVAL;
464 }
465
466 image = list_entry(resource->entry, struct vme_master_resource, list);
467
468 if (bridge->master_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000469 printk(KERN_WARNING "vme_master_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100470 return -EINVAL;
471 }
472
Martyn Welchead1f3e2009-12-15 08:43:02 +0000473 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100474 ((image->cycle_attr & cycle) == cycle) &&
475 ((image->width_attr & dwidth) == dwidth))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000476 printk(KERN_WARNING "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100477 return -EINVAL;
478 }
479
480 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000481 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100482 return retval;
483
484 return bridge->master_set(image, enabled, vme_base, size, aspace,
485 cycle, dwidth);
486}
487EXPORT_SYMBOL(vme_master_set);
488
Martyn Welchead1f3e2009-12-15 08:43:02 +0000489int vme_master_get(struct vme_resource *resource, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000490 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
491 u32 *cycle, u32 *dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100492{
493 struct vme_bridge *bridge = find_bridge(resource);
494 struct vme_master_resource *image;
495
496 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000497 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100498 return -EINVAL;
499 }
500
501 image = list_entry(resource->entry, struct vme_master_resource, list);
502
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200503 if (bridge->master_get == NULL) {
Julia Lawallc1038302014-12-07 20:20:53 +0100504 printk(KERN_WARNING "%s not supported\n", __func__);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100505 return -EINVAL;
506 }
507
508 return bridge->master_get(image, enabled, vme_base, size, aspace,
509 cycle, dwidth);
510}
511EXPORT_SYMBOL(vme_master_get);
512
513/*
514 * Read data out of VME space into a buffer.
515 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000516ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100517 loff_t offset)
518{
519 struct vme_bridge *bridge = find_bridge(resource);
520 struct vme_master_resource *image;
521 size_t length;
522
523 if (bridge->master_read == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000524 printk(KERN_WARNING "Reading from resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100525 return -EINVAL;
526 }
527
528 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000529 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100530 return -EINVAL;
531 }
532
533 image = list_entry(resource->entry, struct vme_master_resource, list);
534
535 length = vme_get_size(resource);
536
537 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000538 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100539 return -EFAULT;
540 }
541
542 if ((offset + count) > length)
543 count = length - offset;
544
545 return bridge->master_read(image, buf, count, offset);
546
547}
548EXPORT_SYMBOL(vme_master_read);
549
550/*
551 * Write data out to VME space from a buffer.
552 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000553ssize_t vme_master_write(struct vme_resource *resource, void *buf,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100554 size_t count, loff_t offset)
555{
556 struct vme_bridge *bridge = find_bridge(resource);
557 struct vme_master_resource *image;
558 size_t length;
559
560 if (bridge->master_write == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000561 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100562 return -EINVAL;
563 }
564
565 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000566 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100567 return -EINVAL;
568 }
569
570 image = list_entry(resource->entry, struct vme_master_resource, list);
571
572 length = vme_get_size(resource);
573
574 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000575 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100576 return -EFAULT;
577 }
578
579 if ((offset + count) > length)
580 count = length - offset;
581
582 return bridge->master_write(image, buf, count, offset);
583}
584EXPORT_SYMBOL(vme_master_write);
585
586/*
587 * Perform RMW cycle to provided location.
588 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000589unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100590 unsigned int compare, unsigned int swap, loff_t offset)
591{
592 struct vme_bridge *bridge = find_bridge(resource);
593 struct vme_master_resource *image;
594
595 if (bridge->master_rmw == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000596 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100597 return -EINVAL;
598 }
599
600 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000601 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100602 return -EINVAL;
603 }
604
605 image = list_entry(resource->entry, struct vme_master_resource, list);
606
607 return bridge->master_rmw(image, mask, compare, swap, offset);
608}
609EXPORT_SYMBOL(vme_master_rmw);
610
Dmitry Kalinkinc74a8042015-02-26 18:53:10 +0300611int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
612{
613 struct vme_master_resource *image;
614 phys_addr_t phys_addr;
615 unsigned long vma_size;
616
617 if (resource->type != VME_MASTER) {
618 pr_err("Not a master resource\n");
619 return -EINVAL;
620 }
621
622 image = list_entry(resource->entry, struct vme_master_resource, list);
623 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
624 vma_size = vma->vm_end - vma->vm_start;
625
626 if (phys_addr + vma_size > image->bus_resource.end + 1) {
627 pr_err("Map size cannot exceed the window size\n");
628 return -EFAULT;
629 }
630
631 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
632
633 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
634}
635EXPORT_SYMBOL(vme_master_mmap);
636
Martyn Welcha17a75e2009-07-31 09:28:17 +0100637void vme_master_free(struct vme_resource *resource)
638{
639 struct vme_master_resource *master_image;
640
641 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000642 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100643 return;
644 }
645
646 master_image = list_entry(resource->entry, struct vme_master_resource,
647 list);
648 if (master_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000649 printk(KERN_ERR "Can't find master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100650 return;
651 }
652
653 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000654 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100655 if (master_image->locked == 0)
656 printk(KERN_ERR "Image is already free\n");
657
658 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000659 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100660
661 /* Free up resource memory */
662 kfree(resource);
663}
664EXPORT_SYMBOL(vme_master_free);
665
666/*
667 * Request a DMA controller with specific attributes, return some unique
668 * identifier.
669 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000670struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100671{
672 struct vme_bridge *bridge;
673 struct list_head *dma_pos = NULL;
674 struct vme_dma_resource *allocated_ctrlr = NULL;
675 struct vme_dma_resource *dma_ctrlr = NULL;
676 struct vme_resource *resource = NULL;
677
678 /* XXX Not checking resource attributes */
679 printk(KERN_ERR "No VME resource Attribute tests done\n");
680
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200681 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100682 if (bridge == NULL) {
683 printk(KERN_ERR "Can't find VME bus\n");
684 goto err_bus;
685 }
686
687 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000688 list_for_each(dma_pos, &bridge->dma_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100689 dma_ctrlr = list_entry(dma_pos,
690 struct vme_dma_resource, list);
691
692 if (dma_ctrlr == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000693 printk(KERN_ERR "Registered NULL DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100694 continue;
695 }
696
Martyn Welch4f723df2010-02-18 15:12:58 +0000697 /* Find an unlocked and compatible controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000698 mutex_lock(&dma_ctrlr->mtx);
Martyn Welch4f723df2010-02-18 15:12:58 +0000699 if (((dma_ctrlr->route_attr & route) == route) &&
700 (dma_ctrlr->locked == 0)) {
701
Martyn Welcha17a75e2009-07-31 09:28:17 +0100702 dma_ctrlr->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000703 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100704 allocated_ctrlr = dma_ctrlr;
705 break;
706 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000707 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100708 }
709
710 /* Check to see if we found a resource */
711 if (allocated_ctrlr == NULL)
712 goto err_ctrlr;
713
714 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
715 if (resource == NULL) {
716 printk(KERN_WARNING "Unable to allocate resource structure\n");
717 goto err_alloc;
718 }
719 resource->type = VME_DMA;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000720 resource->entry = &allocated_ctrlr->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100721
722 return resource;
723
724err_alloc:
725 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000726 mutex_lock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100727 dma_ctrlr->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000728 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100729err_ctrlr:
730err_bus:
731 return NULL;
732}
Martyn Welch58e50792009-10-29 16:35:20 +0000733EXPORT_SYMBOL(vme_dma_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100734
735/*
736 * Start new list
737 */
738struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
739{
740 struct vme_dma_resource *ctrlr;
741 struct vme_dma_list *dma_list;
742
743 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000744 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100745 return NULL;
746 }
747
748 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
749
Martyn Welchead1f3e2009-12-15 08:43:02 +0000750 dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
751 if (dma_list == NULL) {
752 printk(KERN_ERR "Unable to allocate memory for new dma list\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100753 return NULL;
754 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000755 INIT_LIST_HEAD(&dma_list->entries);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100756 dma_list->parent = ctrlr;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000757 mutex_init(&dma_list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100758
759 return dma_list;
760}
761EXPORT_SYMBOL(vme_new_dma_list);
762
763/*
764 * Create "Pattern" type attributes
765 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000766struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100767{
768 struct vme_dma_attr *attributes;
769 struct vme_dma_pattern *pattern_attr;
770
Martyn Welchead1f3e2009-12-15 08:43:02 +0000771 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
772 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700773 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100774 goto err_attr;
775 }
776
Martyn Welchead1f3e2009-12-15 08:43:02 +0000777 pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
778 if (pattern_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700779 printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100780 goto err_pat;
781 }
782
783 attributes->type = VME_DMA_PATTERN;
784 attributes->private = (void *)pattern_attr;
785
786 pattern_attr->pattern = pattern;
787 pattern_attr->type = type;
788
789 return attributes;
790
Martyn Welcha17a75e2009-07-31 09:28:17 +0100791err_pat:
792 kfree(attributes);
793err_attr:
794 return NULL;
795}
796EXPORT_SYMBOL(vme_dma_pattern_attribute);
797
798/*
799 * Create "PCI" type attributes
800 */
801struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
802{
803 struct vme_dma_attr *attributes;
804 struct vme_dma_pci *pci_attr;
805
806 /* XXX Run some sanity checks here */
807
Martyn Welchead1f3e2009-12-15 08:43:02 +0000808 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
809 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700810 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100811 goto err_attr;
812 }
813
Martyn Welchead1f3e2009-12-15 08:43:02 +0000814 pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
815 if (pci_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700816 printk(KERN_ERR "Unable to allocate memory for pci attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100817 goto err_pci;
818 }
819
820
821
822 attributes->type = VME_DMA_PCI;
823 attributes->private = (void *)pci_attr;
824
825 pci_attr->address = address;
826
827 return attributes;
828
Martyn Welcha17a75e2009-07-31 09:28:17 +0100829err_pci:
830 kfree(attributes);
831err_attr:
832 return NULL;
833}
834EXPORT_SYMBOL(vme_dma_pci_attribute);
835
836/*
837 * Create "VME" type attributes
838 */
839struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
Martyn Welch6af04b02011-12-01 17:06:29 +0000840 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100841{
842 struct vme_dma_attr *attributes;
843 struct vme_dma_vme *vme_attr;
844
Martyn Welchead1f3e2009-12-15 08:43:02 +0000845 attributes = kmalloc(
Martyn Welcha17a75e2009-07-31 09:28:17 +0100846 sizeof(struct vme_dma_attr), GFP_KERNEL);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000847 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700848 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100849 goto err_attr;
850 }
851
Martyn Welchead1f3e2009-12-15 08:43:02 +0000852 vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
853 if (vme_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700854 printk(KERN_ERR "Unable to allocate memory for vme attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100855 goto err_vme;
856 }
857
858 attributes->type = VME_DMA_VME;
859 attributes->private = (void *)vme_attr;
860
861 vme_attr->address = address;
862 vme_attr->aspace = aspace;
863 vme_attr->cycle = cycle;
864 vme_attr->dwidth = dwidth;
865
866 return attributes;
867
Martyn Welcha17a75e2009-07-31 09:28:17 +0100868err_vme:
869 kfree(attributes);
870err_attr:
871 return NULL;
872}
873EXPORT_SYMBOL(vme_dma_vme_attribute);
874
875/*
876 * Free attribute
877 */
878void vme_dma_free_attribute(struct vme_dma_attr *attributes)
879{
880 kfree(attributes->private);
881 kfree(attributes);
882}
883EXPORT_SYMBOL(vme_dma_free_attribute);
884
885int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
886 struct vme_dma_attr *dest, size_t count)
887{
888 struct vme_bridge *bridge = list->parent->parent;
889 int retval;
890
891 if (bridge->dma_list_add == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000892 printk(KERN_WARNING "Link List DMA generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100893 return -EINVAL;
894 }
895
Emilio G. Cota886953e2010-11-12 11:14:07 +0000896 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000897 printk(KERN_ERR "Link List already submitted\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100898 return -EINVAL;
899 }
900
901 retval = bridge->dma_list_add(list, src, dest, count);
902
Emilio G. Cota886953e2010-11-12 11:14:07 +0000903 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100904
905 return retval;
906}
907EXPORT_SYMBOL(vme_dma_list_add);
908
909int vme_dma_list_exec(struct vme_dma_list *list)
910{
911 struct vme_bridge *bridge = list->parent->parent;
912 int retval;
913
914 if (bridge->dma_list_exec == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000915 printk(KERN_ERR "Link List DMA execution not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100916 return -EINVAL;
917 }
918
Emilio G. Cota886953e2010-11-12 11:14:07 +0000919 mutex_lock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100920
921 retval = bridge->dma_list_exec(list);
922
Emilio G. Cota886953e2010-11-12 11:14:07 +0000923 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100924
925 return retval;
926}
927EXPORT_SYMBOL(vme_dma_list_exec);
928
929int vme_dma_list_free(struct vme_dma_list *list)
930{
931 struct vme_bridge *bridge = list->parent->parent;
932 int retval;
933
934 if (bridge->dma_list_empty == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000935 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100936 return -EINVAL;
937 }
938
Emilio G. Cota886953e2010-11-12 11:14:07 +0000939 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000940 printk(KERN_ERR "Link List in use\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100941 return -EINVAL;
942 }
943
944 /*
945 * Empty out all of the entries from the dma list. We need to go to the
946 * low level driver as dma entries are driver specific.
947 */
948 retval = bridge->dma_list_empty(list);
949 if (retval) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000950 printk(KERN_ERR "Unable to empty link-list entries\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +0000951 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100952 return retval;
953 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000954 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100955 kfree(list);
956
957 return retval;
958}
959EXPORT_SYMBOL(vme_dma_list_free);
960
961int vme_dma_free(struct vme_resource *resource)
962{
963 struct vme_dma_resource *ctrlr;
964
965 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000966 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100967 return -EINVAL;
968 }
969
970 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
971
Emilio G. Cota886953e2010-11-12 11:14:07 +0000972 if (!mutex_trylock(&ctrlr->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000973 printk(KERN_ERR "Resource busy, can't free\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100974 return -EBUSY;
975 }
976
Emilio G. Cota886953e2010-11-12 11:14:07 +0000977 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000978 printk(KERN_WARNING "Resource still processing transfers\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +0000979 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100980 return -EBUSY;
981 }
982
983 ctrlr->locked = 0;
984
Emilio G. Cota886953e2010-11-12 11:14:07 +0000985 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100986
Martyn Welchfd5c2562013-06-06 12:29:16 +0100987 kfree(resource);
988
Martyn Welcha17a75e2009-07-31 09:28:17 +0100989 return 0;
990}
991EXPORT_SYMBOL(vme_dma_free);
992
Dmitry Kalinkine2c63932015-09-18 02:01:42 +0300993void vme_bus_error_handler(struct vme_bridge *bridge,
994 unsigned long long address, u32 attributes)
995{
996 struct vme_bus_error *error;
997
998 error = kmalloc(sizeof(struct vme_bus_error), GFP_ATOMIC);
999 if (error) {
1000 error->address = address;
1001 error->attributes = attributes;
1002 list_add_tail(&error->list, &bridge->vme_errors);
1003 } else {
1004 dev_err(bridge->parent,
1005 "Unable to alloc memory for VMEbus Error reporting\n");
1006 }
1007}
1008EXPORT_SYMBOL(vme_bus_error_handler);
1009
1010/*
1011 * Find the first error in this address range
1012 */
1013struct vme_bus_error *vme_find_error(struct vme_bridge *bridge, u32 aspace,
1014 unsigned long long address, size_t count)
1015{
1016 struct list_head *err_pos;
1017 struct vme_bus_error *vme_err, *valid = NULL;
1018 unsigned long long bound;
1019
1020 bound = address + count;
1021
1022 /*
1023 * XXX We are currently not looking at the address space when parsing
1024 * for errors. This is because parsing the Address Modifier Codes
1025 * is going to be quite resource intensive to do properly. We
1026 * should be OK just looking at the addresses and this is certainly
1027 * much better than what we had before.
1028 */
1029 err_pos = NULL;
1030 /* Iterate through errors */
1031 list_for_each(err_pos, &bridge->vme_errors) {
1032 vme_err = list_entry(err_pos, struct vme_bus_error, list);
1033 if ((vme_err->address >= address) &&
1034 (vme_err->address < bound)) {
1035
1036 valid = vme_err;
1037 break;
1038 }
1039 }
1040
1041 return valid;
1042}
1043EXPORT_SYMBOL(vme_find_error);
1044
1045/*
1046 * Clear errors in the provided address range.
1047 */
1048void vme_clear_errors(struct vme_bridge *bridge, u32 aspace,
1049 unsigned long long address, size_t count)
1050{
1051 struct list_head *err_pos, *temp;
1052 struct vme_bus_error *vme_err;
1053 unsigned long long bound;
1054
1055 bound = address + count;
1056
1057 /*
1058 * XXX We are currently not looking at the address space when parsing
1059 * for errors. This is because parsing the Address Modifier Codes
1060 * is going to be quite resource intensive to do properly. We
1061 * should be OK just looking at the addresses and this is certainly
1062 * much better than what we had before.
1063 */
1064 err_pos = NULL;
1065 /* Iterate through errors */
1066 list_for_each_safe(err_pos, temp, &bridge->vme_errors) {
1067 vme_err = list_entry(err_pos, struct vme_bus_error, list);
1068
1069 if ((vme_err->address >= address) &&
1070 (vme_err->address < bound)) {
1071
1072 list_del(err_pos);
1073 kfree(vme_err);
1074 }
1075 }
1076}
1077EXPORT_SYMBOL(vme_clear_errors);
1078
Martyn Welchc813f592009-10-29 16:34:54 +00001079void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1080{
1081 void (*call)(int, int, void *);
1082 void *priv_data;
1083
1084 call = bridge->irq[level - 1].callback[statid].func;
1085 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1086
1087 if (call != NULL)
1088 call(level, statid, priv_data);
1089 else
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001090 printk(KERN_WARNING "Spurilous VME interrupt, level:%x, vector:%x\n",
1091 level, statid);
Martyn Welchc813f592009-10-29 16:34:54 +00001092}
1093EXPORT_SYMBOL(vme_irq_handler);
1094
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001095int vme_irq_request(struct vme_dev *vdev, int level, int statid,
Martyn Welch29848ac2010-02-18 15:13:05 +00001096 void (*callback)(int, int, void *),
Martyn Welcha17a75e2009-07-31 09:28:17 +01001097 void *priv_data)
1098{
1099 struct vme_bridge *bridge;
1100
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001101 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001102 if (bridge == NULL) {
1103 printk(KERN_ERR "Can't find VME bus\n");
1104 return -EINVAL;
1105 }
1106
Martyn Welchead1f3e2009-12-15 08:43:02 +00001107 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001108 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001109 return -EINVAL;
1110 }
1111
Martyn Welchc813f592009-10-29 16:34:54 +00001112 if (bridge->irq_set == NULL) {
1113 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001114 return -EINVAL;
1115 }
1116
Emilio G. Cota886953e2010-11-12 11:14:07 +00001117 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001118
1119 if (bridge->irq[level - 1].callback[statid].func) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001120 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001121 printk(KERN_WARNING "VME Interrupt already taken\n");
1122 return -EBUSY;
1123 }
1124
1125 bridge->irq[level - 1].count++;
1126 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1127 bridge->irq[level - 1].callback[statid].func = callback;
1128
1129 /* Enable IRQ level */
Martyn Welch29848ac2010-02-18 15:13:05 +00001130 bridge->irq_set(bridge, level, 1, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001131
Emilio G. Cota886953e2010-11-12 11:14:07 +00001132 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001133
1134 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001135}
Martyn Welchc813f592009-10-29 16:34:54 +00001136EXPORT_SYMBOL(vme_irq_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001137
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001138void vme_irq_free(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001139{
1140 struct vme_bridge *bridge;
1141
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001142 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001143 if (bridge == NULL) {
1144 printk(KERN_ERR "Can't find VME bus\n");
1145 return;
1146 }
1147
Martyn Welchead1f3e2009-12-15 08:43:02 +00001148 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001149 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001150 return;
1151 }
1152
Martyn Welchc813f592009-10-29 16:34:54 +00001153 if (bridge->irq_set == NULL) {
1154 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001155 return;
1156 }
1157
Emilio G. Cota886953e2010-11-12 11:14:07 +00001158 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001159
1160 bridge->irq[level - 1].count--;
1161
1162 /* Disable IRQ level if no more interrupts attached at this level*/
1163 if (bridge->irq[level - 1].count == 0)
Martyn Welch29848ac2010-02-18 15:13:05 +00001164 bridge->irq_set(bridge, level, 0, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001165
1166 bridge->irq[level - 1].callback[statid].func = NULL;
1167 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1168
Emilio G. Cota886953e2010-11-12 11:14:07 +00001169 mutex_unlock(&bridge->irq_mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001170}
Martyn Welchc813f592009-10-29 16:34:54 +00001171EXPORT_SYMBOL(vme_irq_free);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001172
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001173int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001174{
1175 struct vme_bridge *bridge;
1176
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001177 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001178 if (bridge == NULL) {
1179 printk(KERN_ERR "Can't find VME bus\n");
1180 return -EINVAL;
1181 }
1182
Martyn Welchead1f3e2009-12-15 08:43:02 +00001183 if ((level < 1) || (level > 7)) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001184 printk(KERN_WARNING "Invalid interrupt level\n");
1185 return -EINVAL;
1186 }
1187
Martyn Welchc813f592009-10-29 16:34:54 +00001188 if (bridge->irq_generate == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001189 printk(KERN_WARNING "Interrupt generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001190 return -EINVAL;
1191 }
1192
Martyn Welch29848ac2010-02-18 15:13:05 +00001193 return bridge->irq_generate(bridge, level, statid);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001194}
Martyn Welchc813f592009-10-29 16:34:54 +00001195EXPORT_SYMBOL(vme_irq_generate);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001196
Martyn Welch42fb5032009-08-11 17:44:56 +01001197/*
1198 * Request the location monitor, return resource or NULL
1199 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001200struct vme_resource *vme_lm_request(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001201{
1202 struct vme_bridge *bridge;
Martyn Welch42fb5032009-08-11 17:44:56 +01001203 struct list_head *lm_pos = NULL;
1204 struct vme_lm_resource *allocated_lm = NULL;
1205 struct vme_lm_resource *lm = NULL;
1206 struct vme_resource *resource = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001207
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001208 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001209 if (bridge == NULL) {
1210 printk(KERN_ERR "Can't find VME bus\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001211 goto err_bus;
1212 }
1213
1214 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001215 list_for_each(lm_pos, &bridge->lm_resources) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001216 lm = list_entry(lm_pos,
1217 struct vme_lm_resource, list);
1218
1219 if (lm == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001220 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001221 continue;
1222 }
1223
1224 /* Find an unlocked controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001225 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001226 if (lm->locked == 0) {
1227 lm->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001228 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001229 allocated_lm = lm;
1230 break;
1231 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001232 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001233 }
1234
1235 /* Check to see if we found a resource */
1236 if (allocated_lm == NULL)
1237 goto err_lm;
1238
1239 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
1240 if (resource == NULL) {
1241 printk(KERN_ERR "Unable to allocate resource structure\n");
1242 goto err_alloc;
1243 }
1244 resource->type = VME_LM;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001245 resource->entry = &allocated_lm->list;
Martyn Welch42fb5032009-08-11 17:44:56 +01001246
1247 return resource;
1248
1249err_alloc:
1250 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001251 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001252 lm->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001253 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001254err_lm:
1255err_bus:
1256 return NULL;
1257}
1258EXPORT_SYMBOL(vme_lm_request);
1259
1260int vme_lm_count(struct vme_resource *resource)
1261{
1262 struct vme_lm_resource *lm;
1263
1264 if (resource->type != VME_LM) {
1265 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001266 return -EINVAL;
1267 }
1268
Martyn Welch42fb5032009-08-11 17:44:56 +01001269 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1270
1271 return lm->monitors;
1272}
1273EXPORT_SYMBOL(vme_lm_count);
1274
1275int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001276 u32 aspace, u32 cycle)
Martyn Welch42fb5032009-08-11 17:44:56 +01001277{
1278 struct vme_bridge *bridge = find_bridge(resource);
1279 struct vme_lm_resource *lm;
1280
1281 if (resource->type != VME_LM) {
1282 printk(KERN_ERR "Not a Location Monitor resource\n");
1283 return -EINVAL;
1284 }
1285
1286 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1287
Martyn Welcha17a75e2009-07-31 09:28:17 +01001288 if (bridge->lm_set == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001289 printk(KERN_ERR "vme_lm_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001290 return -EINVAL;
1291 }
1292
Martyn Welch8be92262009-10-29 16:35:08 +00001293 return bridge->lm_set(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001294}
1295EXPORT_SYMBOL(vme_lm_set);
1296
Martyn Welch42fb5032009-08-11 17:44:56 +01001297int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001298 u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001299{
Martyn Welch42fb5032009-08-11 17:44:56 +01001300 struct vme_bridge *bridge = find_bridge(resource);
1301 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001302
Martyn Welch42fb5032009-08-11 17:44:56 +01001303 if (resource->type != VME_LM) {
1304 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001305 return -EINVAL;
1306 }
1307
Martyn Welch42fb5032009-08-11 17:44:56 +01001308 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1309
Martyn Welcha17a75e2009-07-31 09:28:17 +01001310 if (bridge->lm_get == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001311 printk(KERN_ERR "vme_lm_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001312 return -EINVAL;
1313 }
1314
Martyn Welch42fb5032009-08-11 17:44:56 +01001315 return bridge->lm_get(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001316}
1317EXPORT_SYMBOL(vme_lm_get);
1318
Martyn Welch42fb5032009-08-11 17:44:56 +01001319int vme_lm_attach(struct vme_resource *resource, int monitor,
1320 void (*callback)(int))
Martyn Welcha17a75e2009-07-31 09:28:17 +01001321{
Martyn Welch42fb5032009-08-11 17:44:56 +01001322 struct vme_bridge *bridge = find_bridge(resource);
1323 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001324
Martyn Welch42fb5032009-08-11 17:44:56 +01001325 if (resource->type != VME_LM) {
1326 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001327 return -EINVAL;
1328 }
1329
Martyn Welch42fb5032009-08-11 17:44:56 +01001330 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1331
Martyn Welcha17a75e2009-07-31 09:28:17 +01001332 if (bridge->lm_attach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001333 printk(KERN_ERR "vme_lm_attach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001334 return -EINVAL;
1335 }
1336
Martyn Welch42fb5032009-08-11 17:44:56 +01001337 return bridge->lm_attach(lm, monitor, callback);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001338}
1339EXPORT_SYMBOL(vme_lm_attach);
1340
Martyn Welch42fb5032009-08-11 17:44:56 +01001341int vme_lm_detach(struct vme_resource *resource, int monitor)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001342{
Martyn Welch42fb5032009-08-11 17:44:56 +01001343 struct vme_bridge *bridge = find_bridge(resource);
1344 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001345
Martyn Welch42fb5032009-08-11 17:44:56 +01001346 if (resource->type != VME_LM) {
1347 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001348 return -EINVAL;
1349 }
1350
Martyn Welch42fb5032009-08-11 17:44:56 +01001351 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1352
Martyn Welcha17a75e2009-07-31 09:28:17 +01001353 if (bridge->lm_detach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001354 printk(KERN_ERR "vme_lm_detach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001355 return -EINVAL;
1356 }
1357
Martyn Welch42fb5032009-08-11 17:44:56 +01001358 return bridge->lm_detach(lm, monitor);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001359}
1360EXPORT_SYMBOL(vme_lm_detach);
1361
Martyn Welch42fb5032009-08-11 17:44:56 +01001362void vme_lm_free(struct vme_resource *resource)
1363{
1364 struct vme_lm_resource *lm;
1365
1366 if (resource->type != VME_LM) {
1367 printk(KERN_ERR "Not a Location Monitor resource\n");
1368 return;
1369 }
1370
1371 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1372
Emilio G. Cota886953e2010-11-12 11:14:07 +00001373 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001374
Martyn Welch8be92262009-10-29 16:35:08 +00001375 /* XXX
1376 * Check to see that there aren't any callbacks still attached, if
1377 * there are we should probably be detaching them!
1378 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001379
1380 lm->locked = 0;
1381
Emilio G. Cota886953e2010-11-12 11:14:07 +00001382 mutex_unlock(&lm->mtx);
Martyn Welch8be92262009-10-29 16:35:08 +00001383
1384 kfree(resource);
Martyn Welch42fb5032009-08-11 17:44:56 +01001385}
1386EXPORT_SYMBOL(vme_lm_free);
1387
Martyn Welchd7729f02013-11-08 11:58:35 +00001388int vme_slot_num(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001389{
1390 struct vme_bridge *bridge;
1391
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001392 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001393 if (bridge == NULL) {
1394 printk(KERN_ERR "Can't find VME bus\n");
1395 return -EINVAL;
1396 }
1397
1398 if (bridge->slot_get == NULL) {
Martyn Welchd7729f02013-11-08 11:58:35 +00001399 printk(KERN_WARNING "vme_slot_num not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001400 return -EINVAL;
1401 }
1402
Martyn Welch29848ac2010-02-18 15:13:05 +00001403 return bridge->slot_get(bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001404}
Martyn Welchd7729f02013-11-08 11:58:35 +00001405EXPORT_SYMBOL(vme_slot_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001406
Martyn Welch978f47d2013-11-08 11:58:34 +00001407int vme_bus_num(struct vme_dev *vdev)
1408{
1409 struct vme_bridge *bridge;
1410
1411 bridge = vdev->bridge;
1412 if (bridge == NULL) {
1413 pr_err("Can't find VME bus\n");
1414 return -EINVAL;
1415 }
1416
1417 return bridge->num;
1418}
1419EXPORT_SYMBOL(vme_bus_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001420
1421/* - Bridge Registration --------------------------------------------------- */
1422
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001423static void vme_dev_release(struct device *dev)
1424{
1425 kfree(dev_to_vme_dev(dev));
1426}
1427
1428int vme_register_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001429{
1430 int i;
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001431 int ret = -1;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001432
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001433 mutex_lock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001434 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001435 if ((vme_bus_numbers & (1 << i)) == 0) {
1436 vme_bus_numbers |= (1 << i);
1437 bridge->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001438 INIT_LIST_HEAD(&bridge->devices);
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001439 list_add_tail(&bridge->bus_list, &vme_bus_list);
1440 ret = 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001441 break;
1442 }
1443 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001444 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001445
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001446 return ret;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001447}
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001448EXPORT_SYMBOL(vme_register_bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001449
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001450void vme_unregister_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001451{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001452 struct vme_dev *vdev;
1453 struct vme_dev *tmp;
1454
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001455 mutex_lock(&vme_buses_lock);
1456 vme_bus_numbers &= ~(1 << bridge->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001457 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1458 list_del(&vdev->drv_list);
1459 list_del(&vdev->bridge_list);
1460 device_unregister(&vdev->dev);
1461 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001462 list_del(&bridge->bus_list);
1463 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001464}
Martyn Welcha17a75e2009-07-31 09:28:17 +01001465EXPORT_SYMBOL(vme_unregister_bridge);
1466
Martyn Welcha17a75e2009-07-31 09:28:17 +01001467/* - Driver Registration --------------------------------------------------- */
1468
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001469static int __vme_register_driver_bus(struct vme_driver *drv,
1470 struct vme_bridge *bridge, unsigned int ndevs)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001471{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001472 int err;
1473 unsigned int i;
1474 struct vme_dev *vdev;
1475 struct vme_dev *tmp;
1476
1477 for (i = 0; i < ndevs; i++) {
1478 vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
1479 if (!vdev) {
1480 err = -ENOMEM;
1481 goto err_devalloc;
1482 }
Manohar Vangaa916a392011-09-26 11:27:17 +02001483 vdev->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001484 vdev->bridge = bridge;
1485 vdev->dev.platform_data = drv;
1486 vdev->dev.release = vme_dev_release;
1487 vdev->dev.parent = bridge->parent;
1488 vdev->dev.bus = &vme_bus_type;
Manohar Vangaa916a392011-09-26 11:27:17 +02001489 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1490 vdev->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001491
1492 err = device_register(&vdev->dev);
1493 if (err)
1494 goto err_reg;
1495
1496 if (vdev->dev.platform_data) {
1497 list_add_tail(&vdev->drv_list, &drv->devices);
1498 list_add_tail(&vdev->bridge_list, &bridge->devices);
1499 } else
1500 device_unregister(&vdev->dev);
1501 }
1502 return 0;
1503
1504err_reg:
Emilio G. Cotadef18202013-02-13 13:47:54 -05001505 put_device(&vdev->dev);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001506 kfree(vdev);
1507err_devalloc:
1508 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1509 list_del(&vdev->drv_list);
1510 list_del(&vdev->bridge_list);
1511 device_unregister(&vdev->dev);
1512 }
1513 return err;
1514}
1515
1516static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1517{
1518 struct vme_bridge *bridge;
1519 int err = 0;
1520
1521 mutex_lock(&vme_buses_lock);
1522 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1523 /*
1524 * This cannot cause trouble as we already have vme_buses_lock
1525 * and if the bridge is removed, it will have to go through
1526 * vme_unregister_bridge() to do it (which calls remove() on
1527 * the bridge which in turn tries to acquire vme_buses_lock and
Manohar Vangac26f6112011-11-04 11:12:29 +01001528 * will have to wait).
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001529 */
1530 err = __vme_register_driver_bus(drv, bridge, ndevs);
1531 if (err)
1532 break;
1533 }
1534 mutex_unlock(&vme_buses_lock);
1535 return err;
1536}
1537
1538int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1539{
1540 int err;
1541
Martyn Welcha17a75e2009-07-31 09:28:17 +01001542 drv->driver.name = drv->name;
1543 drv->driver.bus = &vme_bus_type;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001544 INIT_LIST_HEAD(&drv->devices);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001545
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001546 err = driver_register(&drv->driver);
1547 if (err)
1548 return err;
1549
1550 err = __vme_register_driver(drv, ndevs);
1551 if (err)
1552 driver_unregister(&drv->driver);
1553
1554 return err;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001555}
1556EXPORT_SYMBOL(vme_register_driver);
1557
Martyn Welchead1f3e2009-12-15 08:43:02 +00001558void vme_unregister_driver(struct vme_driver *drv)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001559{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001560 struct vme_dev *dev, *dev_tmp;
1561
1562 mutex_lock(&vme_buses_lock);
1563 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1564 list_del(&dev->drv_list);
1565 list_del(&dev->bridge_list);
1566 device_unregister(&dev->dev);
1567 }
1568 mutex_unlock(&vme_buses_lock);
1569
Martyn Welcha17a75e2009-07-31 09:28:17 +01001570 driver_unregister(&drv->driver);
1571}
1572EXPORT_SYMBOL(vme_unregister_driver);
1573
1574/* - Bus Registration ------------------------------------------------------ */
1575
Martyn Welcha17a75e2009-07-31 09:28:17 +01001576static int vme_bus_match(struct device *dev, struct device_driver *drv)
1577{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001578 struct vme_driver *vme_drv;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001579
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001580 vme_drv = container_of(drv, struct vme_driver, driver);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001581
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001582 if (dev->platform_data == vme_drv) {
1583 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001584
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001585 if (vme_drv->match && vme_drv->match(vdev))
1586 return 1;
1587
1588 dev->platform_data = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001589 }
Martyn Welcha17a75e2009-07-31 09:28:17 +01001590 return 0;
1591}
1592
1593static int vme_bus_probe(struct device *dev)
1594{
Martyn Welcha17a75e2009-07-31 09:28:17 +01001595 int retval = -ENODEV;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001596 struct vme_driver *driver;
1597 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001598
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001599 driver = dev->platform_data;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001600
Martyn Welchead1f3e2009-12-15 08:43:02 +00001601 if (driver->probe != NULL)
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001602 retval = driver->probe(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001603
1604 return retval;
1605}
1606
1607static int vme_bus_remove(struct device *dev)
1608{
Martyn Welcha17a75e2009-07-31 09:28:17 +01001609 int retval = -ENODEV;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001610 struct vme_driver *driver;
1611 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001612
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001613 driver = dev->platform_data;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001614
Martyn Welchead1f3e2009-12-15 08:43:02 +00001615 if (driver->remove != NULL)
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001616 retval = driver->remove(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001617
1618 return retval;
1619}
1620
1621struct bus_type vme_bus_type = {
1622 .name = "vme",
1623 .match = vme_bus_match,
1624 .probe = vme_bus_probe,
1625 .remove = vme_bus_remove,
1626};
1627EXPORT_SYMBOL(vme_bus_type);
1628
Martyn Welchead1f3e2009-12-15 08:43:02 +00001629static int __init vme_init(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001630{
1631 return bus_register(&vme_bus_type);
1632}
1633
Martyn Welchead1f3e2009-12-15 08:43:02 +00001634static void __exit vme_exit(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001635{
1636 bus_unregister(&vme_bus_type);
1637}
1638
Aaron Sierrac326cc02013-12-09 09:54:42 -06001639subsys_initcall(vme_init);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001640module_exit(vme_exit);