blob: 1b78d27aa72884cbea4ac92bd5f0c27d4ce5487c [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
Martyn Welch6af04b02011-12-01 17:06:29 +0000180static int vme_check_window(u32 aspace, unsigned long long vme_base,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100181 unsigned long long size)
182{
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}
224
225/*
226 * Request a slave image with specific attributes, return some unique
227 * identifier.
228 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000229struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
230 u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100231{
232 struct vme_bridge *bridge;
233 struct list_head *slave_pos = NULL;
234 struct vme_slave_resource *allocated_image = NULL;
235 struct vme_slave_resource *slave_image = NULL;
236 struct vme_resource *resource = NULL;
237
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200238 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100239 if (bridge == NULL) {
240 printk(KERN_ERR "Can't find VME bus\n");
241 goto err_bus;
242 }
243
244 /* Loop through slave resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000245 list_for_each(slave_pos, &bridge->slave_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100246 slave_image = list_entry(slave_pos,
247 struct vme_slave_resource, list);
248
249 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000250 printk(KERN_ERR "Registered NULL Slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100251 continue;
252 }
253
254 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000255 mutex_lock(&slave_image->mtx);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000256 if (((slave_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100257 ((slave_image->cycle_attr & cycle) == cycle) &&
258 (slave_image->locked == 0)) {
259
260 slave_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000261 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100262 allocated_image = slave_image;
263 break;
264 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000265 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100266 }
267
268 /* No free image */
269 if (allocated_image == NULL)
270 goto err_image;
271
272 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
273 if (resource == NULL) {
274 printk(KERN_WARNING "Unable to allocate resource structure\n");
275 goto err_alloc;
276 }
277 resource->type = VME_SLAVE;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000278 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100279
280 return resource;
281
282err_alloc:
283 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000284 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100285 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000286 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100287err_image:
288err_bus:
289 return NULL;
290}
291EXPORT_SYMBOL(vme_slave_request);
292
Martyn Welchead1f3e2009-12-15 08:43:02 +0000293int vme_slave_set(struct vme_resource *resource, int enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100294 unsigned long long vme_base, unsigned long long size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000295 dma_addr_t buf_base, u32 aspace, u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100296{
297 struct vme_bridge *bridge = find_bridge(resource);
298 struct vme_slave_resource *image;
299 int retval;
300
301 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000302 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100303 return -EINVAL;
304 }
305
306 image = list_entry(resource->entry, struct vme_slave_resource, list);
307
308 if (bridge->slave_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000309 printk(KERN_ERR "Function not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100310 return -ENOSYS;
311 }
312
Martyn Welchead1f3e2009-12-15 08:43:02 +0000313 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100314 ((image->cycle_attr & cycle) == cycle))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000315 printk(KERN_ERR "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100316 return -EINVAL;
317 }
318
319 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000320 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100321 return retval;
322
323 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
324 aspace, cycle);
325}
326EXPORT_SYMBOL(vme_slave_set);
327
Martyn Welchead1f3e2009-12-15 08:43:02 +0000328int vme_slave_get(struct vme_resource *resource, int *enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100329 unsigned long long *vme_base, unsigned long long *size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000330 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100331{
332 struct vme_bridge *bridge = find_bridge(resource);
333 struct vme_slave_resource *image;
334
335 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000336 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100337 return -EINVAL;
338 }
339
340 image = list_entry(resource->entry, struct vme_slave_resource, list);
341
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200342 if (bridge->slave_get == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000343 printk(KERN_ERR "vme_slave_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100344 return -EINVAL;
345 }
346
347 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
348 aspace, cycle);
349}
350EXPORT_SYMBOL(vme_slave_get);
351
352void vme_slave_free(struct vme_resource *resource)
353{
354 struct vme_slave_resource *slave_image;
355
356 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000357 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100358 return;
359 }
360
361 slave_image = list_entry(resource->entry, struct vme_slave_resource,
362 list);
363 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000364 printk(KERN_ERR "Can't find slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100365 return;
366 }
367
368 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000369 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100370 if (slave_image->locked == 0)
371 printk(KERN_ERR "Image is already free\n");
372
373 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000374 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100375
376 /* Free up resource memory */
377 kfree(resource);
378}
379EXPORT_SYMBOL(vme_slave_free);
380
381/*
382 * Request a master image with specific attributes, return some unique
383 * identifier.
384 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000385struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
386 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100387{
388 struct vme_bridge *bridge;
389 struct list_head *master_pos = NULL;
390 struct vme_master_resource *allocated_image = NULL;
391 struct vme_master_resource *master_image = NULL;
392 struct vme_resource *resource = NULL;
393
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200394 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100395 if (bridge == NULL) {
396 printk(KERN_ERR "Can't find VME bus\n");
397 goto err_bus;
398 }
399
400 /* Loop through master resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000401 list_for_each(master_pos, &bridge->master_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100402 master_image = list_entry(master_pos,
403 struct vme_master_resource, list);
404
405 if (master_image == NULL) {
406 printk(KERN_WARNING "Registered NULL master resource\n");
407 continue;
408 }
409
410 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000411 spin_lock(&master_image->lock);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000412 if (((master_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100413 ((master_image->cycle_attr & cycle) == cycle) &&
414 ((master_image->width_attr & dwidth) == dwidth) &&
415 (master_image->locked == 0)) {
416
417 master_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000418 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100419 allocated_image = master_image;
420 break;
421 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000422 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100423 }
424
425 /* Check to see if we found a resource */
426 if (allocated_image == NULL) {
427 printk(KERN_ERR "Can't find a suitable resource\n");
428 goto err_image;
429 }
430
431 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
432 if (resource == NULL) {
433 printk(KERN_ERR "Unable to allocate resource structure\n");
434 goto err_alloc;
435 }
436 resource->type = VME_MASTER;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000437 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100438
439 return resource;
440
Martyn Welcha17a75e2009-07-31 09:28:17 +0100441err_alloc:
442 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000443 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100444 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000445 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100446err_image:
447err_bus:
448 return NULL;
449}
450EXPORT_SYMBOL(vme_master_request);
451
Martyn Welchead1f3e2009-12-15 08:43:02 +0000452int vme_master_set(struct vme_resource *resource, int enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000453 unsigned long long vme_base, unsigned long long size, u32 aspace,
454 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100455{
456 struct vme_bridge *bridge = find_bridge(resource);
457 struct vme_master_resource *image;
458 int retval;
459
460 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000461 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100462 return -EINVAL;
463 }
464
465 image = list_entry(resource->entry, struct vme_master_resource, list);
466
467 if (bridge->master_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000468 printk(KERN_WARNING "vme_master_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100469 return -EINVAL;
470 }
471
Martyn Welchead1f3e2009-12-15 08:43:02 +0000472 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100473 ((image->cycle_attr & cycle) == cycle) &&
474 ((image->width_attr & dwidth) == dwidth))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000475 printk(KERN_WARNING "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100476 return -EINVAL;
477 }
478
479 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000480 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100481 return retval;
482
483 return bridge->master_set(image, enabled, vme_base, size, aspace,
484 cycle, dwidth);
485}
486EXPORT_SYMBOL(vme_master_set);
487
Martyn Welchead1f3e2009-12-15 08:43:02 +0000488int vme_master_get(struct vme_resource *resource, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000489 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
490 u32 *cycle, u32 *dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100491{
492 struct vme_bridge *bridge = find_bridge(resource);
493 struct vme_master_resource *image;
494
495 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000496 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100497 return -EINVAL;
498 }
499
500 image = list_entry(resource->entry, struct vme_master_resource, list);
501
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200502 if (bridge->master_get == NULL) {
Julia Lawallc1038302014-12-07 20:20:53 +0100503 printk(KERN_WARNING "%s not supported\n", __func__);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100504 return -EINVAL;
505 }
506
507 return bridge->master_get(image, enabled, vme_base, size, aspace,
508 cycle, dwidth);
509}
510EXPORT_SYMBOL(vme_master_get);
511
512/*
513 * Read data out of VME space into a buffer.
514 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000515ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100516 loff_t offset)
517{
518 struct vme_bridge *bridge = find_bridge(resource);
519 struct vme_master_resource *image;
520 size_t length;
521
522 if (bridge->master_read == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000523 printk(KERN_WARNING "Reading from resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100524 return -EINVAL;
525 }
526
527 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000528 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100529 return -EINVAL;
530 }
531
532 image = list_entry(resource->entry, struct vme_master_resource, list);
533
534 length = vme_get_size(resource);
535
536 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000537 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100538 return -EFAULT;
539 }
540
541 if ((offset + count) > length)
542 count = length - offset;
543
544 return bridge->master_read(image, buf, count, offset);
545
546}
547EXPORT_SYMBOL(vme_master_read);
548
549/*
550 * Write data out to VME space from a buffer.
551 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000552ssize_t vme_master_write(struct vme_resource *resource, void *buf,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100553 size_t count, loff_t offset)
554{
555 struct vme_bridge *bridge = find_bridge(resource);
556 struct vme_master_resource *image;
557 size_t length;
558
559 if (bridge->master_write == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000560 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100561 return -EINVAL;
562 }
563
564 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000565 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100566 return -EINVAL;
567 }
568
569 image = list_entry(resource->entry, struct vme_master_resource, list);
570
571 length = vme_get_size(resource);
572
573 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000574 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100575 return -EFAULT;
576 }
577
578 if ((offset + count) > length)
579 count = length - offset;
580
581 return bridge->master_write(image, buf, count, offset);
582}
583EXPORT_SYMBOL(vme_master_write);
584
585/*
586 * Perform RMW cycle to provided location.
587 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000588unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100589 unsigned int compare, unsigned int swap, loff_t offset)
590{
591 struct vme_bridge *bridge = find_bridge(resource);
592 struct vme_master_resource *image;
593
594 if (bridge->master_rmw == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000595 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100596 return -EINVAL;
597 }
598
599 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000600 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100601 return -EINVAL;
602 }
603
604 image = list_entry(resource->entry, struct vme_master_resource, list);
605
606 return bridge->master_rmw(image, mask, compare, swap, offset);
607}
608EXPORT_SYMBOL(vme_master_rmw);
609
Dmitry Kalinkinc74a8042015-02-26 18:53:10 +0300610int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
611{
612 struct vme_master_resource *image;
613 phys_addr_t phys_addr;
614 unsigned long vma_size;
615
616 if (resource->type != VME_MASTER) {
617 pr_err("Not a master resource\n");
618 return -EINVAL;
619 }
620
621 image = list_entry(resource->entry, struct vme_master_resource, list);
622 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
623 vma_size = vma->vm_end - vma->vm_start;
624
625 if (phys_addr + vma_size > image->bus_resource.end + 1) {
626 pr_err("Map size cannot exceed the window size\n");
627 return -EFAULT;
628 }
629
630 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
631
632 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
633}
634EXPORT_SYMBOL(vme_master_mmap);
635
Martyn Welcha17a75e2009-07-31 09:28:17 +0100636void vme_master_free(struct vme_resource *resource)
637{
638 struct vme_master_resource *master_image;
639
640 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000641 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100642 return;
643 }
644
645 master_image = list_entry(resource->entry, struct vme_master_resource,
646 list);
647 if (master_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000648 printk(KERN_ERR "Can't find master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100649 return;
650 }
651
652 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000653 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100654 if (master_image->locked == 0)
655 printk(KERN_ERR "Image is already free\n");
656
657 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000658 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100659
660 /* Free up resource memory */
661 kfree(resource);
662}
663EXPORT_SYMBOL(vme_master_free);
664
665/*
666 * Request a DMA controller with specific attributes, return some unique
667 * identifier.
668 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000669struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100670{
671 struct vme_bridge *bridge;
672 struct list_head *dma_pos = NULL;
673 struct vme_dma_resource *allocated_ctrlr = NULL;
674 struct vme_dma_resource *dma_ctrlr = NULL;
675 struct vme_resource *resource = NULL;
676
677 /* XXX Not checking resource attributes */
678 printk(KERN_ERR "No VME resource Attribute tests done\n");
679
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200680 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100681 if (bridge == NULL) {
682 printk(KERN_ERR "Can't find VME bus\n");
683 goto err_bus;
684 }
685
686 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000687 list_for_each(dma_pos, &bridge->dma_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100688 dma_ctrlr = list_entry(dma_pos,
689 struct vme_dma_resource, list);
690
691 if (dma_ctrlr == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000692 printk(KERN_ERR "Registered NULL DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100693 continue;
694 }
695
Martyn Welch4f723df2010-02-18 15:12:58 +0000696 /* Find an unlocked and compatible controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000697 mutex_lock(&dma_ctrlr->mtx);
Martyn Welch4f723df2010-02-18 15:12:58 +0000698 if (((dma_ctrlr->route_attr & route) == route) &&
699 (dma_ctrlr->locked == 0)) {
700
Martyn Welcha17a75e2009-07-31 09:28:17 +0100701 dma_ctrlr->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000702 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100703 allocated_ctrlr = dma_ctrlr;
704 break;
705 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000706 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100707 }
708
709 /* Check to see if we found a resource */
710 if (allocated_ctrlr == NULL)
711 goto err_ctrlr;
712
713 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
714 if (resource == NULL) {
715 printk(KERN_WARNING "Unable to allocate resource structure\n");
716 goto err_alloc;
717 }
718 resource->type = VME_DMA;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000719 resource->entry = &allocated_ctrlr->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100720
721 return resource;
722
723err_alloc:
724 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000725 mutex_lock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100726 dma_ctrlr->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000727 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100728err_ctrlr:
729err_bus:
730 return NULL;
731}
Martyn Welch58e50792009-10-29 16:35:20 +0000732EXPORT_SYMBOL(vme_dma_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100733
734/*
735 * Start new list
736 */
737struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
738{
739 struct vme_dma_resource *ctrlr;
740 struct vme_dma_list *dma_list;
741
742 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000743 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100744 return NULL;
745 }
746
747 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
748
Martyn Welchead1f3e2009-12-15 08:43:02 +0000749 dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
750 if (dma_list == NULL) {
751 printk(KERN_ERR "Unable to allocate memory for new dma list\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100752 return NULL;
753 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000754 INIT_LIST_HEAD(&dma_list->entries);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100755 dma_list->parent = ctrlr;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000756 mutex_init(&dma_list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100757
758 return dma_list;
759}
760EXPORT_SYMBOL(vme_new_dma_list);
761
762/*
763 * Create "Pattern" type attributes
764 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000765struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100766{
767 struct vme_dma_attr *attributes;
768 struct vme_dma_pattern *pattern_attr;
769
Martyn Welchead1f3e2009-12-15 08:43:02 +0000770 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
771 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700772 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100773 goto err_attr;
774 }
775
Martyn Welchead1f3e2009-12-15 08:43:02 +0000776 pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
777 if (pattern_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700778 printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100779 goto err_pat;
780 }
781
782 attributes->type = VME_DMA_PATTERN;
783 attributes->private = (void *)pattern_attr;
784
785 pattern_attr->pattern = pattern;
786 pattern_attr->type = type;
787
788 return attributes;
789
Martyn Welcha17a75e2009-07-31 09:28:17 +0100790err_pat:
791 kfree(attributes);
792err_attr:
793 return NULL;
794}
795EXPORT_SYMBOL(vme_dma_pattern_attribute);
796
797/*
798 * Create "PCI" type attributes
799 */
800struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
801{
802 struct vme_dma_attr *attributes;
803 struct vme_dma_pci *pci_attr;
804
805 /* XXX Run some sanity checks here */
806
Martyn Welchead1f3e2009-12-15 08:43:02 +0000807 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
808 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700809 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100810 goto err_attr;
811 }
812
Martyn Welchead1f3e2009-12-15 08:43:02 +0000813 pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
814 if (pci_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700815 printk(KERN_ERR "Unable to allocate memory for pci attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100816 goto err_pci;
817 }
818
819
820
821 attributes->type = VME_DMA_PCI;
822 attributes->private = (void *)pci_attr;
823
824 pci_attr->address = address;
825
826 return attributes;
827
Martyn Welcha17a75e2009-07-31 09:28:17 +0100828err_pci:
829 kfree(attributes);
830err_attr:
831 return NULL;
832}
833EXPORT_SYMBOL(vme_dma_pci_attribute);
834
835/*
836 * Create "VME" type attributes
837 */
838struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
Martyn Welch6af04b02011-12-01 17:06:29 +0000839 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100840{
841 struct vme_dma_attr *attributes;
842 struct vme_dma_vme *vme_attr;
843
Martyn Welchead1f3e2009-12-15 08:43:02 +0000844 attributes = kmalloc(
Martyn Welcha17a75e2009-07-31 09:28:17 +0100845 sizeof(struct vme_dma_attr), GFP_KERNEL);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000846 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700847 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100848 goto err_attr;
849 }
850
Martyn Welchead1f3e2009-12-15 08:43:02 +0000851 vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
852 if (vme_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700853 printk(KERN_ERR "Unable to allocate memory for vme attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100854 goto err_vme;
855 }
856
857 attributes->type = VME_DMA_VME;
858 attributes->private = (void *)vme_attr;
859
860 vme_attr->address = address;
861 vme_attr->aspace = aspace;
862 vme_attr->cycle = cycle;
863 vme_attr->dwidth = dwidth;
864
865 return attributes;
866
Martyn Welcha17a75e2009-07-31 09:28:17 +0100867err_vme:
868 kfree(attributes);
869err_attr:
870 return NULL;
871}
872EXPORT_SYMBOL(vme_dma_vme_attribute);
873
874/*
875 * Free attribute
876 */
877void vme_dma_free_attribute(struct vme_dma_attr *attributes)
878{
879 kfree(attributes->private);
880 kfree(attributes);
881}
882EXPORT_SYMBOL(vme_dma_free_attribute);
883
884int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
885 struct vme_dma_attr *dest, size_t count)
886{
887 struct vme_bridge *bridge = list->parent->parent;
888 int retval;
889
890 if (bridge->dma_list_add == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000891 printk(KERN_WARNING "Link List DMA generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100892 return -EINVAL;
893 }
894
Emilio G. Cota886953e2010-11-12 11:14:07 +0000895 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000896 printk(KERN_ERR "Link List already submitted\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100897 return -EINVAL;
898 }
899
900 retval = bridge->dma_list_add(list, src, dest, count);
901
Emilio G. Cota886953e2010-11-12 11:14:07 +0000902 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100903
904 return retval;
905}
906EXPORT_SYMBOL(vme_dma_list_add);
907
908int vme_dma_list_exec(struct vme_dma_list *list)
909{
910 struct vme_bridge *bridge = list->parent->parent;
911 int retval;
912
913 if (bridge->dma_list_exec == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000914 printk(KERN_ERR "Link List DMA execution not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100915 return -EINVAL;
916 }
917
Emilio G. Cota886953e2010-11-12 11:14:07 +0000918 mutex_lock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100919
920 retval = bridge->dma_list_exec(list);
921
Emilio G. Cota886953e2010-11-12 11:14:07 +0000922 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100923
924 return retval;
925}
926EXPORT_SYMBOL(vme_dma_list_exec);
927
928int vme_dma_list_free(struct vme_dma_list *list)
929{
930 struct vme_bridge *bridge = list->parent->parent;
931 int retval;
932
933 if (bridge->dma_list_empty == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000934 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100935 return -EINVAL;
936 }
937
Emilio G. Cota886953e2010-11-12 11:14:07 +0000938 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000939 printk(KERN_ERR "Link List in use\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100940 return -EINVAL;
941 }
942
943 /*
944 * Empty out all of the entries from the dma list. We need to go to the
945 * low level driver as dma entries are driver specific.
946 */
947 retval = bridge->dma_list_empty(list);
948 if (retval) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000949 printk(KERN_ERR "Unable to empty link-list entries\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +0000950 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100951 return retval;
952 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000953 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100954 kfree(list);
955
956 return retval;
957}
958EXPORT_SYMBOL(vme_dma_list_free);
959
960int vme_dma_free(struct vme_resource *resource)
961{
962 struct vme_dma_resource *ctrlr;
963
964 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000965 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100966 return -EINVAL;
967 }
968
969 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
970
Emilio G. Cota886953e2010-11-12 11:14:07 +0000971 if (!mutex_trylock(&ctrlr->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000972 printk(KERN_ERR "Resource busy, can't free\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100973 return -EBUSY;
974 }
975
Emilio G. Cota886953e2010-11-12 11:14:07 +0000976 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000977 printk(KERN_WARNING "Resource still processing transfers\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +0000978 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100979 return -EBUSY;
980 }
981
982 ctrlr->locked = 0;
983
Emilio G. Cota886953e2010-11-12 11:14:07 +0000984 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100985
Martyn Welchfd5c2562013-06-06 12:29:16 +0100986 kfree(resource);
987
Martyn Welcha17a75e2009-07-31 09:28:17 +0100988 return 0;
989}
990EXPORT_SYMBOL(vme_dma_free);
991
Martyn Welchc813f592009-10-29 16:34:54 +0000992void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
993{
994 void (*call)(int, int, void *);
995 void *priv_data;
996
997 call = bridge->irq[level - 1].callback[statid].func;
998 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
999
1000 if (call != NULL)
1001 call(level, statid, priv_data);
1002 else
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001003 printk(KERN_WARNING "Spurilous VME interrupt, level:%x, vector:%x\n",
1004 level, statid);
Martyn Welchc813f592009-10-29 16:34:54 +00001005}
1006EXPORT_SYMBOL(vme_irq_handler);
1007
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001008int vme_irq_request(struct vme_dev *vdev, int level, int statid,
Martyn Welch29848ac2010-02-18 15:13:05 +00001009 void (*callback)(int, int, void *),
Martyn Welcha17a75e2009-07-31 09:28:17 +01001010 void *priv_data)
1011{
1012 struct vme_bridge *bridge;
1013
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001014 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001015 if (bridge == NULL) {
1016 printk(KERN_ERR "Can't find VME bus\n");
1017 return -EINVAL;
1018 }
1019
Martyn Welchead1f3e2009-12-15 08:43:02 +00001020 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001021 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001022 return -EINVAL;
1023 }
1024
Martyn Welchc813f592009-10-29 16:34:54 +00001025 if (bridge->irq_set == NULL) {
1026 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001027 return -EINVAL;
1028 }
1029
Emilio G. Cota886953e2010-11-12 11:14:07 +00001030 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001031
1032 if (bridge->irq[level - 1].callback[statid].func) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001033 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001034 printk(KERN_WARNING "VME Interrupt already taken\n");
1035 return -EBUSY;
1036 }
1037
1038 bridge->irq[level - 1].count++;
1039 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1040 bridge->irq[level - 1].callback[statid].func = callback;
1041
1042 /* Enable IRQ level */
Martyn Welch29848ac2010-02-18 15:13:05 +00001043 bridge->irq_set(bridge, level, 1, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001044
Emilio G. Cota886953e2010-11-12 11:14:07 +00001045 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001046
1047 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001048}
Martyn Welchc813f592009-10-29 16:34:54 +00001049EXPORT_SYMBOL(vme_irq_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001050
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001051void vme_irq_free(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001052{
1053 struct vme_bridge *bridge;
1054
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001055 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001056 if (bridge == NULL) {
1057 printk(KERN_ERR "Can't find VME bus\n");
1058 return;
1059 }
1060
Martyn Welchead1f3e2009-12-15 08:43:02 +00001061 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001062 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001063 return;
1064 }
1065
Martyn Welchc813f592009-10-29 16:34:54 +00001066 if (bridge->irq_set == NULL) {
1067 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001068 return;
1069 }
1070
Emilio G. Cota886953e2010-11-12 11:14:07 +00001071 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001072
1073 bridge->irq[level - 1].count--;
1074
1075 /* Disable IRQ level if no more interrupts attached at this level*/
1076 if (bridge->irq[level - 1].count == 0)
Martyn Welch29848ac2010-02-18 15:13:05 +00001077 bridge->irq_set(bridge, level, 0, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001078
1079 bridge->irq[level - 1].callback[statid].func = NULL;
1080 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1081
Emilio G. Cota886953e2010-11-12 11:14:07 +00001082 mutex_unlock(&bridge->irq_mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001083}
Martyn Welchc813f592009-10-29 16:34:54 +00001084EXPORT_SYMBOL(vme_irq_free);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001085
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001086int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001087{
1088 struct vme_bridge *bridge;
1089
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001090 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001091 if (bridge == NULL) {
1092 printk(KERN_ERR "Can't find VME bus\n");
1093 return -EINVAL;
1094 }
1095
Martyn Welchead1f3e2009-12-15 08:43:02 +00001096 if ((level < 1) || (level > 7)) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001097 printk(KERN_WARNING "Invalid interrupt level\n");
1098 return -EINVAL;
1099 }
1100
Martyn Welchc813f592009-10-29 16:34:54 +00001101 if (bridge->irq_generate == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001102 printk(KERN_WARNING "Interrupt generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001103 return -EINVAL;
1104 }
1105
Martyn Welch29848ac2010-02-18 15:13:05 +00001106 return bridge->irq_generate(bridge, level, statid);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001107}
Martyn Welchc813f592009-10-29 16:34:54 +00001108EXPORT_SYMBOL(vme_irq_generate);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001109
Martyn Welch42fb5032009-08-11 17:44:56 +01001110/*
1111 * Request the location monitor, return resource or NULL
1112 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001113struct vme_resource *vme_lm_request(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001114{
1115 struct vme_bridge *bridge;
Martyn Welch42fb5032009-08-11 17:44:56 +01001116 struct list_head *lm_pos = NULL;
1117 struct vme_lm_resource *allocated_lm = NULL;
1118 struct vme_lm_resource *lm = NULL;
1119 struct vme_resource *resource = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001120
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001121 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001122 if (bridge == NULL) {
1123 printk(KERN_ERR "Can't find VME bus\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001124 goto err_bus;
1125 }
1126
1127 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001128 list_for_each(lm_pos, &bridge->lm_resources) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001129 lm = list_entry(lm_pos,
1130 struct vme_lm_resource, list);
1131
1132 if (lm == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001133 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001134 continue;
1135 }
1136
1137 /* Find an unlocked controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001138 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001139 if (lm->locked == 0) {
1140 lm->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001141 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001142 allocated_lm = lm;
1143 break;
1144 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001145 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001146 }
1147
1148 /* Check to see if we found a resource */
1149 if (allocated_lm == NULL)
1150 goto err_lm;
1151
1152 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
1153 if (resource == NULL) {
1154 printk(KERN_ERR "Unable to allocate resource structure\n");
1155 goto err_alloc;
1156 }
1157 resource->type = VME_LM;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001158 resource->entry = &allocated_lm->list;
Martyn Welch42fb5032009-08-11 17:44:56 +01001159
1160 return resource;
1161
1162err_alloc:
1163 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001164 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001165 lm->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001166 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001167err_lm:
1168err_bus:
1169 return NULL;
1170}
1171EXPORT_SYMBOL(vme_lm_request);
1172
1173int vme_lm_count(struct vme_resource *resource)
1174{
1175 struct vme_lm_resource *lm;
1176
1177 if (resource->type != VME_LM) {
1178 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001179 return -EINVAL;
1180 }
1181
Martyn Welch42fb5032009-08-11 17:44:56 +01001182 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1183
1184 return lm->monitors;
1185}
1186EXPORT_SYMBOL(vme_lm_count);
1187
1188int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001189 u32 aspace, u32 cycle)
Martyn Welch42fb5032009-08-11 17:44:56 +01001190{
1191 struct vme_bridge *bridge = find_bridge(resource);
1192 struct vme_lm_resource *lm;
1193
1194 if (resource->type != VME_LM) {
1195 printk(KERN_ERR "Not a Location Monitor resource\n");
1196 return -EINVAL;
1197 }
1198
1199 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1200
Martyn Welcha17a75e2009-07-31 09:28:17 +01001201 if (bridge->lm_set == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001202 printk(KERN_ERR "vme_lm_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001203 return -EINVAL;
1204 }
1205
Martyn Welch8be92262009-10-29 16:35:08 +00001206 return bridge->lm_set(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001207}
1208EXPORT_SYMBOL(vme_lm_set);
1209
Martyn Welch42fb5032009-08-11 17:44:56 +01001210int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001211 u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001212{
Martyn Welch42fb5032009-08-11 17:44:56 +01001213 struct vme_bridge *bridge = find_bridge(resource);
1214 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001215
Martyn Welch42fb5032009-08-11 17:44:56 +01001216 if (resource->type != VME_LM) {
1217 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001218 return -EINVAL;
1219 }
1220
Martyn Welch42fb5032009-08-11 17:44:56 +01001221 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1222
Martyn Welcha17a75e2009-07-31 09:28:17 +01001223 if (bridge->lm_get == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001224 printk(KERN_ERR "vme_lm_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001225 return -EINVAL;
1226 }
1227
Martyn Welch42fb5032009-08-11 17:44:56 +01001228 return bridge->lm_get(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001229}
1230EXPORT_SYMBOL(vme_lm_get);
1231
Martyn Welch42fb5032009-08-11 17:44:56 +01001232int vme_lm_attach(struct vme_resource *resource, int monitor,
1233 void (*callback)(int))
Martyn Welcha17a75e2009-07-31 09:28:17 +01001234{
Martyn Welch42fb5032009-08-11 17:44:56 +01001235 struct vme_bridge *bridge = find_bridge(resource);
1236 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001237
Martyn Welch42fb5032009-08-11 17:44:56 +01001238 if (resource->type != VME_LM) {
1239 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001240 return -EINVAL;
1241 }
1242
Martyn Welch42fb5032009-08-11 17:44:56 +01001243 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1244
Martyn Welcha17a75e2009-07-31 09:28:17 +01001245 if (bridge->lm_attach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001246 printk(KERN_ERR "vme_lm_attach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001247 return -EINVAL;
1248 }
1249
Martyn Welch42fb5032009-08-11 17:44:56 +01001250 return bridge->lm_attach(lm, monitor, callback);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001251}
1252EXPORT_SYMBOL(vme_lm_attach);
1253
Martyn Welch42fb5032009-08-11 17:44:56 +01001254int vme_lm_detach(struct vme_resource *resource, int monitor)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001255{
Martyn Welch42fb5032009-08-11 17:44:56 +01001256 struct vme_bridge *bridge = find_bridge(resource);
1257 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001258
Martyn Welch42fb5032009-08-11 17:44:56 +01001259 if (resource->type != VME_LM) {
1260 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001261 return -EINVAL;
1262 }
1263
Martyn Welch42fb5032009-08-11 17:44:56 +01001264 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1265
Martyn Welcha17a75e2009-07-31 09:28:17 +01001266 if (bridge->lm_detach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001267 printk(KERN_ERR "vme_lm_detach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001268 return -EINVAL;
1269 }
1270
Martyn Welch42fb5032009-08-11 17:44:56 +01001271 return bridge->lm_detach(lm, monitor);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001272}
1273EXPORT_SYMBOL(vme_lm_detach);
1274
Martyn Welch42fb5032009-08-11 17:44:56 +01001275void vme_lm_free(struct vme_resource *resource)
1276{
1277 struct vme_lm_resource *lm;
1278
1279 if (resource->type != VME_LM) {
1280 printk(KERN_ERR "Not a Location Monitor resource\n");
1281 return;
1282 }
1283
1284 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1285
Emilio G. Cota886953e2010-11-12 11:14:07 +00001286 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001287
Martyn Welch8be92262009-10-29 16:35:08 +00001288 /* XXX
1289 * Check to see that there aren't any callbacks still attached, if
1290 * there are we should probably be detaching them!
1291 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001292
1293 lm->locked = 0;
1294
Emilio G. Cota886953e2010-11-12 11:14:07 +00001295 mutex_unlock(&lm->mtx);
Martyn Welch8be92262009-10-29 16:35:08 +00001296
1297 kfree(resource);
Martyn Welch42fb5032009-08-11 17:44:56 +01001298}
1299EXPORT_SYMBOL(vme_lm_free);
1300
Martyn Welchd7729f02013-11-08 11:58:35 +00001301int vme_slot_num(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001302{
1303 struct vme_bridge *bridge;
1304
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001305 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001306 if (bridge == NULL) {
1307 printk(KERN_ERR "Can't find VME bus\n");
1308 return -EINVAL;
1309 }
1310
1311 if (bridge->slot_get == NULL) {
Martyn Welchd7729f02013-11-08 11:58:35 +00001312 printk(KERN_WARNING "vme_slot_num not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001313 return -EINVAL;
1314 }
1315
Martyn Welch29848ac2010-02-18 15:13:05 +00001316 return bridge->slot_get(bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001317}
Martyn Welchd7729f02013-11-08 11:58:35 +00001318EXPORT_SYMBOL(vme_slot_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001319
Martyn Welch978f47d2013-11-08 11:58:34 +00001320int vme_bus_num(struct vme_dev *vdev)
1321{
1322 struct vme_bridge *bridge;
1323
1324 bridge = vdev->bridge;
1325 if (bridge == NULL) {
1326 pr_err("Can't find VME bus\n");
1327 return -EINVAL;
1328 }
1329
1330 return bridge->num;
1331}
1332EXPORT_SYMBOL(vme_bus_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001333
1334/* - Bridge Registration --------------------------------------------------- */
1335
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001336static void vme_dev_release(struct device *dev)
1337{
1338 kfree(dev_to_vme_dev(dev));
1339}
1340
1341int vme_register_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001342{
1343 int i;
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001344 int ret = -1;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001345
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001346 mutex_lock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001347 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001348 if ((vme_bus_numbers & (1 << i)) == 0) {
1349 vme_bus_numbers |= (1 << i);
1350 bridge->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001351 INIT_LIST_HEAD(&bridge->devices);
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001352 list_add_tail(&bridge->bus_list, &vme_bus_list);
1353 ret = 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001354 break;
1355 }
1356 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001357 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001358
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001359 return ret;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001360}
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001361EXPORT_SYMBOL(vme_register_bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001362
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001363void vme_unregister_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001364{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001365 struct vme_dev *vdev;
1366 struct vme_dev *tmp;
1367
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001368 mutex_lock(&vme_buses_lock);
1369 vme_bus_numbers &= ~(1 << bridge->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001370 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1371 list_del(&vdev->drv_list);
1372 list_del(&vdev->bridge_list);
1373 device_unregister(&vdev->dev);
1374 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001375 list_del(&bridge->bus_list);
1376 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001377}
Martyn Welcha17a75e2009-07-31 09:28:17 +01001378EXPORT_SYMBOL(vme_unregister_bridge);
1379
Martyn Welcha17a75e2009-07-31 09:28:17 +01001380/* - Driver Registration --------------------------------------------------- */
1381
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001382static int __vme_register_driver_bus(struct vme_driver *drv,
1383 struct vme_bridge *bridge, unsigned int ndevs)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001384{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001385 int err;
1386 unsigned int i;
1387 struct vme_dev *vdev;
1388 struct vme_dev *tmp;
1389
1390 for (i = 0; i < ndevs; i++) {
1391 vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
1392 if (!vdev) {
1393 err = -ENOMEM;
1394 goto err_devalloc;
1395 }
Manohar Vangaa916a392011-09-26 11:27:17 +02001396 vdev->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001397 vdev->bridge = bridge;
1398 vdev->dev.platform_data = drv;
1399 vdev->dev.release = vme_dev_release;
1400 vdev->dev.parent = bridge->parent;
1401 vdev->dev.bus = &vme_bus_type;
Manohar Vangaa916a392011-09-26 11:27:17 +02001402 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1403 vdev->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001404
1405 err = device_register(&vdev->dev);
1406 if (err)
1407 goto err_reg;
1408
1409 if (vdev->dev.platform_data) {
1410 list_add_tail(&vdev->drv_list, &drv->devices);
1411 list_add_tail(&vdev->bridge_list, &bridge->devices);
1412 } else
1413 device_unregister(&vdev->dev);
1414 }
1415 return 0;
1416
1417err_reg:
Emilio G. Cotadef18202013-02-13 13:47:54 -05001418 put_device(&vdev->dev);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001419 kfree(vdev);
1420err_devalloc:
1421 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1422 list_del(&vdev->drv_list);
1423 list_del(&vdev->bridge_list);
1424 device_unregister(&vdev->dev);
1425 }
1426 return err;
1427}
1428
1429static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1430{
1431 struct vme_bridge *bridge;
1432 int err = 0;
1433
1434 mutex_lock(&vme_buses_lock);
1435 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1436 /*
1437 * This cannot cause trouble as we already have vme_buses_lock
1438 * and if the bridge is removed, it will have to go through
1439 * vme_unregister_bridge() to do it (which calls remove() on
1440 * the bridge which in turn tries to acquire vme_buses_lock and
Manohar Vangac26f6112011-11-04 11:12:29 +01001441 * will have to wait).
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001442 */
1443 err = __vme_register_driver_bus(drv, bridge, ndevs);
1444 if (err)
1445 break;
1446 }
1447 mutex_unlock(&vme_buses_lock);
1448 return err;
1449}
1450
1451int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1452{
1453 int err;
1454
Martyn Welcha17a75e2009-07-31 09:28:17 +01001455 drv->driver.name = drv->name;
1456 drv->driver.bus = &vme_bus_type;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001457 INIT_LIST_HEAD(&drv->devices);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001458
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001459 err = driver_register(&drv->driver);
1460 if (err)
1461 return err;
1462
1463 err = __vme_register_driver(drv, ndevs);
1464 if (err)
1465 driver_unregister(&drv->driver);
1466
1467 return err;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001468}
1469EXPORT_SYMBOL(vme_register_driver);
1470
Martyn Welchead1f3e2009-12-15 08:43:02 +00001471void vme_unregister_driver(struct vme_driver *drv)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001472{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001473 struct vme_dev *dev, *dev_tmp;
1474
1475 mutex_lock(&vme_buses_lock);
1476 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1477 list_del(&dev->drv_list);
1478 list_del(&dev->bridge_list);
1479 device_unregister(&dev->dev);
1480 }
1481 mutex_unlock(&vme_buses_lock);
1482
Martyn Welcha17a75e2009-07-31 09:28:17 +01001483 driver_unregister(&drv->driver);
1484}
1485EXPORT_SYMBOL(vme_unregister_driver);
1486
1487/* - Bus Registration ------------------------------------------------------ */
1488
Martyn Welcha17a75e2009-07-31 09:28:17 +01001489static int vme_bus_match(struct device *dev, struct device_driver *drv)
1490{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001491 struct vme_driver *vme_drv;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001492
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001493 vme_drv = container_of(drv, struct vme_driver, driver);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001494
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001495 if (dev->platform_data == vme_drv) {
1496 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001497
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001498 if (vme_drv->match && vme_drv->match(vdev))
1499 return 1;
1500
1501 dev->platform_data = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001502 }
Martyn Welcha17a75e2009-07-31 09:28:17 +01001503 return 0;
1504}
1505
1506static int vme_bus_probe(struct device *dev)
1507{
Martyn Welcha17a75e2009-07-31 09:28:17 +01001508 int retval = -ENODEV;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001509 struct vme_driver *driver;
1510 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001511
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001512 driver = dev->platform_data;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001513
Martyn Welchead1f3e2009-12-15 08:43:02 +00001514 if (driver->probe != NULL)
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001515 retval = driver->probe(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001516
1517 return retval;
1518}
1519
1520static int vme_bus_remove(struct device *dev)
1521{
Martyn Welcha17a75e2009-07-31 09:28:17 +01001522 int retval = -ENODEV;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001523 struct vme_driver *driver;
1524 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001525
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001526 driver = dev->platform_data;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001527
Martyn Welchead1f3e2009-12-15 08:43:02 +00001528 if (driver->remove != NULL)
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001529 retval = driver->remove(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001530
1531 return retval;
1532}
1533
1534struct bus_type vme_bus_type = {
1535 .name = "vme",
1536 .match = vme_bus_match,
1537 .probe = vme_bus_probe,
1538 .remove = vme_bus_remove,
1539};
1540EXPORT_SYMBOL(vme_bus_type);
1541
Martyn Welchead1f3e2009-12-15 08:43:02 +00001542static int __init vme_init(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001543{
1544 return bus_register(&vme_bus_type);
1545}
1546
Martyn Welchead1f3e2009-12-15 08:43:02 +00001547static void __exit vme_exit(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001548{
1549 bus_unregister(&vme_bus_type);
1550}
1551
Aaron Sierrac326cc02013-12-09 09:54:42 -06001552subsys_initcall(vme_init);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001553module_exit(vme_exit);