Commit | Line | Data |
---|---|---|
a17a75e2 MW |
1 | /* |
2 | * VME Bridge Framework | |
3 | * | |
66bd8db5 MW |
4 | * Author: Martyn Welch <martyn.welch@ge.com> |
5 | * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc. | |
a17a75e2 MW |
6 | * |
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 | ||
a17a75e2 MW |
16 | #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> | |
400822fe | 30 | #include <linux/mutex.h> |
a17a75e2 | 31 | #include <linux/spinlock.h> |
5a0e3ad6 | 32 | #include <linux/slab.h> |
a17a75e2 MW |
33 | |
34 | #include "vme.h" | |
35 | #include "vme_bridge.h" | |
36 | ||
733e3ef0 | 37 | /* Bitmask and list of registered buses both protected by common mutex */ |
a17a75e2 | 38 | static unsigned int vme_bus_numbers; |
733e3ef0 MV |
39 | static LIST_HEAD(vme_bus_list); |
40 | static DEFINE_MUTEX(vme_buses_lock); | |
a17a75e2 | 41 | |
ead1f3e3 MW |
42 | static void __exit vme_exit(void); |
43 | static int __init vme_init(void); | |
a17a75e2 | 44 | |
8f966dc4 | 45 | static struct vme_dev *dev_to_vme_dev(struct device *dev) |
a17a75e2 | 46 | { |
8f966dc4 | 47 | return container_of(dev, struct vme_dev, dev); |
a17a75e2 MW |
48 | } |
49 | ||
50 | /* | |
51 | * Find the bridge that the resource is associated with. | |
52 | */ | |
53 | static 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; | |
42fb5031 MW |
69 | case VME_LM: |
70 | return list_entry(resource->entry, struct vme_lm_resource, | |
71 | list)->parent; | |
72 | break; | |
a17a75e2 MW |
73 | 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. | |
a17a75e2 | 83 | */ |
ead1f3e3 | 84 | void *vme_alloc_consistent(struct vme_resource *resource, size_t size, |
a17a75e2 MW |
85 | dma_addr_t *dma) |
86 | { | |
87 | struct vme_bridge *bridge; | |
a17a75e2 | 88 | |
ead1f3e3 MW |
89 | if (resource == NULL) { |
90 | printk(KERN_ERR "No resource\n"); | |
a17a75e2 MW |
91 | return NULL; |
92 | } | |
93 | ||
94 | bridge = find_bridge(resource); | |
ead1f3e3 MW |
95 | if (bridge == NULL) { |
96 | printk(KERN_ERR "Can't find bridge\n"); | |
a17a75e2 MW |
97 | return NULL; |
98 | } | |
99 | ||
a17a75e2 | 100 | if (bridge->parent == NULL) { |
7f58f025 MV |
101 | printk(KERN_ERR "Dev entry NULL for" |
102 | " bridge %s\n", bridge->name); | |
103 | return NULL; | |
104 | } | |
105 | ||
106 | if (bridge->alloc_consistent == NULL) { | |
107 | printk(KERN_ERR "alloc_consistent not supported by" | |
108 | " bridge %s\n", bridge->name); | |
a17a75e2 MW |
109 | return NULL; |
110 | } | |
a17a75e2 | 111 | |
7f58f025 | 112 | return bridge->alloc_consistent(bridge->parent, size, dma); |
a17a75e2 MW |
113 | } |
114 | EXPORT_SYMBOL(vme_alloc_consistent); | |
115 | ||
116 | /* | |
117 | * Free previously allocated contiguous block of memory. | |
a17a75e2 MW |
118 | */ |
119 | void vme_free_consistent(struct vme_resource *resource, size_t size, | |
120 | void *vaddr, dma_addr_t dma) | |
121 | { | |
122 | struct vme_bridge *bridge; | |
a17a75e2 | 123 | |
ead1f3e3 MW |
124 | if (resource == NULL) { |
125 | printk(KERN_ERR "No resource\n"); | |
a17a75e2 MW |
126 | return; |
127 | } | |
128 | ||
129 | bridge = find_bridge(resource); | |
ead1f3e3 MW |
130 | if (bridge == NULL) { |
131 | printk(KERN_ERR "Can't find bridge\n"); | |
a17a75e2 MW |
132 | return; |
133 | } | |
134 | ||
7f58f025 MV |
135 | if (bridge->parent == NULL) { |
136 | printk(KERN_ERR "Dev entry NULL for" | |
137 | " bridge %s\n", bridge->name); | |
138 | return; | |
139 | } | |
140 | ||
141 | if (bridge->free_consistent == NULL) { | |
142 | printk(KERN_ERR "free_consistent not supported by" | |
143 | " bridge %s\n", bridge->name); | |
144 | return; | |
145 | } | |
a17a75e2 | 146 | |
7f58f025 | 147 | bridge->free_consistent(bridge->parent, size, vaddr, dma); |
a17a75e2 MW |
148 | } |
149 | EXPORT_SYMBOL(vme_free_consistent); | |
150 | ||
151 | size_t vme_get_size(struct vme_resource *resource) | |
152 | { | |
153 | int enabled, retval; | |
154 | unsigned long long base, size; | |
155 | dma_addr_t buf_base; | |
156 | vme_address_t aspace; | |
157 | vme_cycle_t cycle; | |
158 | vme_width_t dwidth; | |
159 | ||
160 | switch (resource->type) { | |
161 | case VME_MASTER: | |
162 | retval = vme_master_get(resource, &enabled, &base, &size, | |
163 | &aspace, &cycle, &dwidth); | |
164 | ||
165 | return size; | |
166 | break; | |
167 | case VME_SLAVE: | |
168 | retval = vme_slave_get(resource, &enabled, &base, &size, | |
169 | &buf_base, &aspace, &cycle); | |
170 | ||
171 | return size; | |
172 | break; | |
173 | case VME_DMA: | |
174 | return 0; | |
175 | break; | |
176 | default: | |
177 | printk(KERN_ERR "Unknown resource type\n"); | |
178 | return 0; | |
179 | break; | |
180 | } | |
181 | } | |
182 | EXPORT_SYMBOL(vme_get_size); | |
183 | ||
184 | static int vme_check_window(vme_address_t aspace, unsigned long long vme_base, | |
185 | unsigned long long size) | |
186 | { | |
187 | int retval = 0; | |
188 | ||
189 | switch (aspace) { | |
190 | case VME_A16: | |
191 | if (((vme_base + size) > VME_A16_MAX) || | |
192 | (vme_base > VME_A16_MAX)) | |
193 | retval = -EFAULT; | |
194 | break; | |
195 | case VME_A24: | |
196 | if (((vme_base + size) > VME_A24_MAX) || | |
197 | (vme_base > VME_A24_MAX)) | |
198 | retval = -EFAULT; | |
199 | break; | |
200 | case VME_A32: | |
201 | if (((vme_base + size) > VME_A32_MAX) || | |
202 | (vme_base > VME_A32_MAX)) | |
203 | retval = -EFAULT; | |
204 | break; | |
205 | case VME_A64: | |
206 | /* | |
207 | * Any value held in an unsigned long long can be used as the | |
208 | * base | |
209 | */ | |
210 | break; | |
211 | case VME_CRCSR: | |
212 | if (((vme_base + size) > VME_CRCSR_MAX) || | |
213 | (vme_base > VME_CRCSR_MAX)) | |
214 | retval = -EFAULT; | |
215 | break; | |
216 | case VME_USER1: | |
217 | case VME_USER2: | |
218 | case VME_USER3: | |
219 | case VME_USER4: | |
220 | /* User Defined */ | |
221 | break; | |
222 | default: | |
ead1f3e3 | 223 | printk(KERN_ERR "Invalid address space\n"); |
a17a75e2 MW |
224 | retval = -EINVAL; |
225 | break; | |
226 | } | |
227 | ||
228 | return retval; | |
229 | } | |
230 | ||
231 | /* | |
232 | * Request a slave image with specific attributes, return some unique | |
233 | * identifier. | |
234 | */ | |
8f966dc4 | 235 | struct vme_resource *vme_slave_request(struct vme_dev *vdev, |
a17a75e2 MW |
236 | vme_address_t address, vme_cycle_t cycle) |
237 | { | |
238 | struct vme_bridge *bridge; | |
239 | struct list_head *slave_pos = NULL; | |
240 | struct vme_slave_resource *allocated_image = NULL; | |
241 | struct vme_slave_resource *slave_image = NULL; | |
242 | struct vme_resource *resource = NULL; | |
243 | ||
8f966dc4 | 244 | bridge = vdev->bridge; |
a17a75e2 MW |
245 | if (bridge == NULL) { |
246 | printk(KERN_ERR "Can't find VME bus\n"); | |
247 | goto err_bus; | |
248 | } | |
249 | ||
250 | /* Loop through slave resources */ | |
886953e9 | 251 | list_for_each(slave_pos, &bridge->slave_resources) { |
a17a75e2 MW |
252 | slave_image = list_entry(slave_pos, |
253 | struct vme_slave_resource, list); | |
254 | ||
255 | if (slave_image == NULL) { | |
ead1f3e3 | 256 | printk(KERN_ERR "Registered NULL Slave resource\n"); |
a17a75e2 MW |
257 | continue; |
258 | } | |
259 | ||
260 | /* Find an unlocked and compatible image */ | |
886953e9 | 261 | mutex_lock(&slave_image->mtx); |
ead1f3e3 | 262 | if (((slave_image->address_attr & address) == address) && |
a17a75e2 MW |
263 | ((slave_image->cycle_attr & cycle) == cycle) && |
264 | (slave_image->locked == 0)) { | |
265 | ||
266 | slave_image->locked = 1; | |
886953e9 | 267 | mutex_unlock(&slave_image->mtx); |
a17a75e2 MW |
268 | allocated_image = slave_image; |
269 | break; | |
270 | } | |
886953e9 | 271 | mutex_unlock(&slave_image->mtx); |
a17a75e2 MW |
272 | } |
273 | ||
274 | /* No free image */ | |
275 | if (allocated_image == NULL) | |
276 | goto err_image; | |
277 | ||
278 | resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL); | |
279 | if (resource == NULL) { | |
280 | printk(KERN_WARNING "Unable to allocate resource structure\n"); | |
281 | goto err_alloc; | |
282 | } | |
283 | resource->type = VME_SLAVE; | |
886953e9 | 284 | resource->entry = &allocated_image->list; |
a17a75e2 MW |
285 | |
286 | return resource; | |
287 | ||
288 | err_alloc: | |
289 | /* Unlock image */ | |
886953e9 | 290 | mutex_lock(&slave_image->mtx); |
a17a75e2 | 291 | slave_image->locked = 0; |
886953e9 | 292 | mutex_unlock(&slave_image->mtx); |
a17a75e2 MW |
293 | err_image: |
294 | err_bus: | |
295 | return NULL; | |
296 | } | |
297 | EXPORT_SYMBOL(vme_slave_request); | |
298 | ||
ead1f3e3 | 299 | int vme_slave_set(struct vme_resource *resource, int enabled, |
a17a75e2 MW |
300 | unsigned long long vme_base, unsigned long long size, |
301 | dma_addr_t buf_base, vme_address_t aspace, vme_cycle_t cycle) | |
302 | { | |
303 | struct vme_bridge *bridge = find_bridge(resource); | |
304 | struct vme_slave_resource *image; | |
305 | int retval; | |
306 | ||
307 | if (resource->type != VME_SLAVE) { | |
ead1f3e3 | 308 | printk(KERN_ERR "Not a slave resource\n"); |
a17a75e2 MW |
309 | return -EINVAL; |
310 | } | |
311 | ||
312 | image = list_entry(resource->entry, struct vme_slave_resource, list); | |
313 | ||
314 | if (bridge->slave_set == NULL) { | |
ead1f3e3 | 315 | printk(KERN_ERR "Function not supported\n"); |
a17a75e2 MW |
316 | return -ENOSYS; |
317 | } | |
318 | ||
ead1f3e3 | 319 | if (!(((image->address_attr & aspace) == aspace) && |
a17a75e2 | 320 | ((image->cycle_attr & cycle) == cycle))) { |
ead1f3e3 | 321 | printk(KERN_ERR "Invalid attributes\n"); |
a17a75e2 MW |
322 | return -EINVAL; |
323 | } | |
324 | ||
325 | retval = vme_check_window(aspace, vme_base, size); | |
ead1f3e3 | 326 | if (retval) |
a17a75e2 MW |
327 | return retval; |
328 | ||
329 | return bridge->slave_set(image, enabled, vme_base, size, buf_base, | |
330 | aspace, cycle); | |
331 | } | |
332 | EXPORT_SYMBOL(vme_slave_set); | |
333 | ||
ead1f3e3 | 334 | int vme_slave_get(struct vme_resource *resource, int *enabled, |
a17a75e2 MW |
335 | unsigned long long *vme_base, unsigned long long *size, |
336 | dma_addr_t *buf_base, vme_address_t *aspace, vme_cycle_t *cycle) | |
337 | { | |
338 | struct vme_bridge *bridge = find_bridge(resource); | |
339 | struct vme_slave_resource *image; | |
340 | ||
341 | if (resource->type != VME_SLAVE) { | |
ead1f3e3 | 342 | printk(KERN_ERR "Not a slave resource\n"); |
a17a75e2 MW |
343 | return -EINVAL; |
344 | } | |
345 | ||
346 | image = list_entry(resource->entry, struct vme_slave_resource, list); | |
347 | ||
51a569f7 | 348 | if (bridge->slave_get == NULL) { |
ead1f3e3 | 349 | printk(KERN_ERR "vme_slave_get not supported\n"); |
a17a75e2 MW |
350 | return -EINVAL; |
351 | } | |
352 | ||
353 | return bridge->slave_get(image, enabled, vme_base, size, buf_base, | |
354 | aspace, cycle); | |
355 | } | |
356 | EXPORT_SYMBOL(vme_slave_get); | |
357 | ||
358 | void vme_slave_free(struct vme_resource *resource) | |
359 | { | |
360 | struct vme_slave_resource *slave_image; | |
361 | ||
362 | if (resource->type != VME_SLAVE) { | |
ead1f3e3 | 363 | printk(KERN_ERR "Not a slave resource\n"); |
a17a75e2 MW |
364 | return; |
365 | } | |
366 | ||
367 | slave_image = list_entry(resource->entry, struct vme_slave_resource, | |
368 | list); | |
369 | if (slave_image == NULL) { | |
ead1f3e3 | 370 | printk(KERN_ERR "Can't find slave resource\n"); |
a17a75e2 MW |
371 | return; |
372 | } | |
373 | ||
374 | /* Unlock image */ | |
886953e9 | 375 | mutex_lock(&slave_image->mtx); |
a17a75e2 MW |
376 | if (slave_image->locked == 0) |
377 | printk(KERN_ERR "Image is already free\n"); | |
378 | ||
379 | slave_image->locked = 0; | |
886953e9 | 380 | mutex_unlock(&slave_image->mtx); |
a17a75e2 MW |
381 | |
382 | /* Free up resource memory */ | |
383 | kfree(resource); | |
384 | } | |
385 | EXPORT_SYMBOL(vme_slave_free); | |
386 | ||
387 | /* | |
388 | * Request a master image with specific attributes, return some unique | |
389 | * identifier. | |
390 | */ | |
8f966dc4 | 391 | struct vme_resource *vme_master_request(struct vme_dev *vdev, |
a17a75e2 MW |
392 | vme_address_t address, vme_cycle_t cycle, vme_width_t dwidth) |
393 | { | |
394 | struct vme_bridge *bridge; | |
395 | struct list_head *master_pos = NULL; | |
396 | struct vme_master_resource *allocated_image = NULL; | |
397 | struct vme_master_resource *master_image = NULL; | |
398 | struct vme_resource *resource = NULL; | |
399 | ||
8f966dc4 | 400 | bridge = vdev->bridge; |
a17a75e2 MW |
401 | if (bridge == NULL) { |
402 | printk(KERN_ERR "Can't find VME bus\n"); | |
403 | goto err_bus; | |
404 | } | |
405 | ||
406 | /* Loop through master resources */ | |
886953e9 | 407 | list_for_each(master_pos, &bridge->master_resources) { |
a17a75e2 MW |
408 | master_image = list_entry(master_pos, |
409 | struct vme_master_resource, list); | |
410 | ||
411 | if (master_image == NULL) { | |
412 | printk(KERN_WARNING "Registered NULL master resource\n"); | |
413 | continue; | |
414 | } | |
415 | ||
416 | /* Find an unlocked and compatible image */ | |
886953e9 | 417 | spin_lock(&master_image->lock); |
ead1f3e3 | 418 | if (((master_image->address_attr & address) == address) && |
a17a75e2 MW |
419 | ((master_image->cycle_attr & cycle) == cycle) && |
420 | ((master_image->width_attr & dwidth) == dwidth) && | |
421 | (master_image->locked == 0)) { | |
422 | ||
423 | master_image->locked = 1; | |
886953e9 | 424 | spin_unlock(&master_image->lock); |
a17a75e2 MW |
425 | allocated_image = master_image; |
426 | break; | |
427 | } | |
886953e9 | 428 | spin_unlock(&master_image->lock); |
a17a75e2 MW |
429 | } |
430 | ||
431 | /* Check to see if we found a resource */ | |
432 | if (allocated_image == NULL) { | |
433 | printk(KERN_ERR "Can't find a suitable resource\n"); | |
434 | goto err_image; | |
435 | } | |
436 | ||
437 | resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL); | |
438 | if (resource == NULL) { | |
439 | printk(KERN_ERR "Unable to allocate resource structure\n"); | |
440 | goto err_alloc; | |
441 | } | |
442 | resource->type = VME_MASTER; | |
886953e9 | 443 | resource->entry = &allocated_image->list; |
a17a75e2 MW |
444 | |
445 | return resource; | |
446 | ||
a17a75e2 MW |
447 | err_alloc: |
448 | /* Unlock image */ | |
886953e9 | 449 | spin_lock(&master_image->lock); |
a17a75e2 | 450 | master_image->locked = 0; |
886953e9 | 451 | spin_unlock(&master_image->lock); |
a17a75e2 MW |
452 | err_image: |
453 | err_bus: | |
454 | return NULL; | |
455 | } | |
456 | EXPORT_SYMBOL(vme_master_request); | |
457 | ||
ead1f3e3 | 458 | int vme_master_set(struct vme_resource *resource, int enabled, |
a17a75e2 MW |
459 | unsigned long long vme_base, unsigned long long size, |
460 | vme_address_t aspace, vme_cycle_t cycle, vme_width_t dwidth) | |
461 | { | |
462 | struct vme_bridge *bridge = find_bridge(resource); | |
463 | struct vme_master_resource *image; | |
464 | int retval; | |
465 | ||
466 | if (resource->type != VME_MASTER) { | |
ead1f3e3 | 467 | printk(KERN_ERR "Not a master resource\n"); |
a17a75e2 MW |
468 | return -EINVAL; |
469 | } | |
470 | ||
471 | image = list_entry(resource->entry, struct vme_master_resource, list); | |
472 | ||
473 | if (bridge->master_set == NULL) { | |
ead1f3e3 | 474 | printk(KERN_WARNING "vme_master_set not supported\n"); |
a17a75e2 MW |
475 | return -EINVAL; |
476 | } | |
477 | ||
ead1f3e3 | 478 | if (!(((image->address_attr & aspace) == aspace) && |
a17a75e2 MW |
479 | ((image->cycle_attr & cycle) == cycle) && |
480 | ((image->width_attr & dwidth) == dwidth))) { | |
ead1f3e3 | 481 | printk(KERN_WARNING "Invalid attributes\n"); |
a17a75e2 MW |
482 | return -EINVAL; |
483 | } | |
484 | ||
485 | retval = vme_check_window(aspace, vme_base, size); | |
ead1f3e3 | 486 | if (retval) |
a17a75e2 MW |
487 | return retval; |
488 | ||
489 | return bridge->master_set(image, enabled, vme_base, size, aspace, | |
490 | cycle, dwidth); | |
491 | } | |
492 | EXPORT_SYMBOL(vme_master_set); | |
493 | ||
ead1f3e3 | 494 | int vme_master_get(struct vme_resource *resource, int *enabled, |
a17a75e2 MW |
495 | unsigned long long *vme_base, unsigned long long *size, |
496 | vme_address_t *aspace, vme_cycle_t *cycle, vme_width_t *dwidth) | |
497 | { | |
498 | struct vme_bridge *bridge = find_bridge(resource); | |
499 | struct vme_master_resource *image; | |
500 | ||
501 | if (resource->type != VME_MASTER) { | |
ead1f3e3 | 502 | printk(KERN_ERR "Not a master resource\n"); |
a17a75e2 MW |
503 | return -EINVAL; |
504 | } | |
505 | ||
506 | image = list_entry(resource->entry, struct vme_master_resource, list); | |
507 | ||
51a569f7 | 508 | if (bridge->master_get == NULL) { |
ead1f3e3 | 509 | printk(KERN_WARNING "vme_master_set not supported\n"); |
a17a75e2 MW |
510 | return -EINVAL; |
511 | } | |
512 | ||
513 | return bridge->master_get(image, enabled, vme_base, size, aspace, | |
514 | cycle, dwidth); | |
515 | } | |
516 | EXPORT_SYMBOL(vme_master_get); | |
517 | ||
518 | /* | |
519 | * Read data out of VME space into a buffer. | |
520 | */ | |
ead1f3e3 | 521 | ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count, |
a17a75e2 MW |
522 | loff_t offset) |
523 | { | |
524 | struct vme_bridge *bridge = find_bridge(resource); | |
525 | struct vme_master_resource *image; | |
526 | size_t length; | |
527 | ||
528 | if (bridge->master_read == NULL) { | |
ead1f3e3 | 529 | printk(KERN_WARNING "Reading from resource not supported\n"); |
a17a75e2 MW |
530 | return -EINVAL; |
531 | } | |
532 | ||
533 | if (resource->type != VME_MASTER) { | |
ead1f3e3 | 534 | printk(KERN_ERR "Not a master resource\n"); |
a17a75e2 MW |
535 | return -EINVAL; |
536 | } | |
537 | ||
538 | image = list_entry(resource->entry, struct vme_master_resource, list); | |
539 | ||
540 | length = vme_get_size(resource); | |
541 | ||
542 | if (offset > length) { | |
ead1f3e3 | 543 | printk(KERN_WARNING "Invalid Offset\n"); |
a17a75e2 MW |
544 | return -EFAULT; |
545 | } | |
546 | ||
547 | if ((offset + count) > length) | |
548 | count = length - offset; | |
549 | ||
550 | return bridge->master_read(image, buf, count, offset); | |
551 | ||
552 | } | |
553 | EXPORT_SYMBOL(vme_master_read); | |
554 | ||
555 | /* | |
556 | * Write data out to VME space from a buffer. | |
557 | */ | |
ead1f3e3 | 558 | ssize_t vme_master_write(struct vme_resource *resource, void *buf, |
a17a75e2 MW |
559 | size_t count, loff_t offset) |
560 | { | |
561 | struct vme_bridge *bridge = find_bridge(resource); | |
562 | struct vme_master_resource *image; | |
563 | size_t length; | |
564 | ||
565 | if (bridge->master_write == NULL) { | |
ead1f3e3 | 566 | printk(KERN_WARNING "Writing to resource not supported\n"); |
a17a75e2 MW |
567 | return -EINVAL; |
568 | } | |
569 | ||
570 | if (resource->type != VME_MASTER) { | |
ead1f3e3 | 571 | printk(KERN_ERR "Not a master resource\n"); |
a17a75e2 MW |
572 | return -EINVAL; |
573 | } | |
574 | ||
575 | image = list_entry(resource->entry, struct vme_master_resource, list); | |
576 | ||
577 | length = vme_get_size(resource); | |
578 | ||
579 | if (offset > length) { | |
ead1f3e3 | 580 | printk(KERN_WARNING "Invalid Offset\n"); |
a17a75e2 MW |
581 | return -EFAULT; |
582 | } | |
583 | ||
584 | if ((offset + count) > length) | |
585 | count = length - offset; | |
586 | ||
587 | return bridge->master_write(image, buf, count, offset); | |
588 | } | |
589 | EXPORT_SYMBOL(vme_master_write); | |
590 | ||
591 | /* | |
592 | * Perform RMW cycle to provided location. | |
593 | */ | |
ead1f3e3 | 594 | unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask, |
a17a75e2 MW |
595 | unsigned int compare, unsigned int swap, loff_t offset) |
596 | { | |
597 | struct vme_bridge *bridge = find_bridge(resource); | |
598 | struct vme_master_resource *image; | |
599 | ||
600 | if (bridge->master_rmw == NULL) { | |
ead1f3e3 | 601 | printk(KERN_WARNING "Writing to resource not supported\n"); |
a17a75e2 MW |
602 | return -EINVAL; |
603 | } | |
604 | ||
605 | if (resource->type != VME_MASTER) { | |
ead1f3e3 | 606 | printk(KERN_ERR "Not a master resource\n"); |
a17a75e2 MW |
607 | return -EINVAL; |
608 | } | |
609 | ||
610 | image = list_entry(resource->entry, struct vme_master_resource, list); | |
611 | ||
612 | return bridge->master_rmw(image, mask, compare, swap, offset); | |
613 | } | |
614 | EXPORT_SYMBOL(vme_master_rmw); | |
615 | ||
616 | void vme_master_free(struct vme_resource *resource) | |
617 | { | |
618 | struct vme_master_resource *master_image; | |
619 | ||
620 | if (resource->type != VME_MASTER) { | |
ead1f3e3 | 621 | printk(KERN_ERR "Not a master resource\n"); |
a17a75e2 MW |
622 | return; |
623 | } | |
624 | ||
625 | master_image = list_entry(resource->entry, struct vme_master_resource, | |
626 | list); | |
627 | if (master_image == NULL) { | |
ead1f3e3 | 628 | printk(KERN_ERR "Can't find master resource\n"); |
a17a75e2 MW |
629 | return; |
630 | } | |
631 | ||
632 | /* Unlock image */ | |
886953e9 | 633 | spin_lock(&master_image->lock); |
a17a75e2 MW |
634 | if (master_image->locked == 0) |
635 | printk(KERN_ERR "Image is already free\n"); | |
636 | ||
637 | master_image->locked = 0; | |
886953e9 | 638 | spin_unlock(&master_image->lock); |
a17a75e2 MW |
639 | |
640 | /* Free up resource memory */ | |
641 | kfree(resource); | |
642 | } | |
643 | EXPORT_SYMBOL(vme_master_free); | |
644 | ||
645 | /* | |
646 | * Request a DMA controller with specific attributes, return some unique | |
647 | * identifier. | |
648 | */ | |
8f966dc4 MV |
649 | struct vme_resource *vme_dma_request(struct vme_dev *vdev, |
650 | vme_dma_route_t route) | |
a17a75e2 MW |
651 | { |
652 | struct vme_bridge *bridge; | |
653 | struct list_head *dma_pos = NULL; | |
654 | struct vme_dma_resource *allocated_ctrlr = NULL; | |
655 | struct vme_dma_resource *dma_ctrlr = NULL; | |
656 | struct vme_resource *resource = NULL; | |
657 | ||
658 | /* XXX Not checking resource attributes */ | |
659 | printk(KERN_ERR "No VME resource Attribute tests done\n"); | |
660 | ||
8f966dc4 | 661 | bridge = vdev->bridge; |
a17a75e2 MW |
662 | if (bridge == NULL) { |
663 | printk(KERN_ERR "Can't find VME bus\n"); | |
664 | goto err_bus; | |
665 | } | |
666 | ||
667 | /* Loop through DMA resources */ | |
886953e9 | 668 | list_for_each(dma_pos, &bridge->dma_resources) { |
a17a75e2 MW |
669 | dma_ctrlr = list_entry(dma_pos, |
670 | struct vme_dma_resource, list); | |
671 | ||
672 | if (dma_ctrlr == NULL) { | |
ead1f3e3 | 673 | printk(KERN_ERR "Registered NULL DMA resource\n"); |
a17a75e2 MW |
674 | continue; |
675 | } | |
676 | ||
4f723df4 | 677 | /* Find an unlocked and compatible controller */ |
886953e9 | 678 | mutex_lock(&dma_ctrlr->mtx); |
4f723df4 MW |
679 | if (((dma_ctrlr->route_attr & route) == route) && |
680 | (dma_ctrlr->locked == 0)) { | |
681 | ||
a17a75e2 | 682 | dma_ctrlr->locked = 1; |
886953e9 | 683 | mutex_unlock(&dma_ctrlr->mtx); |
a17a75e2 MW |
684 | allocated_ctrlr = dma_ctrlr; |
685 | break; | |
686 | } | |
886953e9 | 687 | mutex_unlock(&dma_ctrlr->mtx); |
a17a75e2 MW |
688 | } |
689 | ||
690 | /* Check to see if we found a resource */ | |
691 | if (allocated_ctrlr == NULL) | |
692 | goto err_ctrlr; | |
693 | ||
694 | resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL); | |
695 | if (resource == NULL) { | |
696 | printk(KERN_WARNING "Unable to allocate resource structure\n"); | |
697 | goto err_alloc; | |
698 | } | |
699 | resource->type = VME_DMA; | |
886953e9 | 700 | resource->entry = &allocated_ctrlr->list; |
a17a75e2 MW |
701 | |
702 | return resource; | |
703 | ||
704 | err_alloc: | |
705 | /* Unlock image */ | |
886953e9 | 706 | mutex_lock(&dma_ctrlr->mtx); |
a17a75e2 | 707 | dma_ctrlr->locked = 0; |
886953e9 | 708 | mutex_unlock(&dma_ctrlr->mtx); |
a17a75e2 MW |
709 | err_ctrlr: |
710 | err_bus: | |
711 | return NULL; | |
712 | } | |
58e50798 | 713 | EXPORT_SYMBOL(vme_dma_request); |
a17a75e2 MW |
714 | |
715 | /* | |
716 | * Start new list | |
717 | */ | |
718 | struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource) | |
719 | { | |
720 | struct vme_dma_resource *ctrlr; | |
721 | struct vme_dma_list *dma_list; | |
722 | ||
723 | if (resource->type != VME_DMA) { | |
ead1f3e3 | 724 | printk(KERN_ERR "Not a DMA resource\n"); |
a17a75e2 MW |
725 | return NULL; |
726 | } | |
727 | ||
728 | ctrlr = list_entry(resource->entry, struct vme_dma_resource, list); | |
729 | ||
ead1f3e3 MW |
730 | dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL); |
731 | if (dma_list == NULL) { | |
732 | printk(KERN_ERR "Unable to allocate memory for new dma list\n"); | |
a17a75e2 MW |
733 | return NULL; |
734 | } | |
886953e9 | 735 | INIT_LIST_HEAD(&dma_list->entries); |
a17a75e2 | 736 | dma_list->parent = ctrlr; |
886953e9 | 737 | mutex_init(&dma_list->mtx); |
a17a75e2 MW |
738 | |
739 | return dma_list; | |
740 | } | |
741 | EXPORT_SYMBOL(vme_new_dma_list); | |
742 | ||
743 | /* | |
744 | * Create "Pattern" type attributes | |
745 | */ | |
746 | struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, | |
747 | vme_pattern_t type) | |
748 | { | |
749 | struct vme_dma_attr *attributes; | |
750 | struct vme_dma_pattern *pattern_attr; | |
751 | ||
ead1f3e3 MW |
752 | attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL); |
753 | if (attributes == NULL) { | |
754 | printk(KERN_ERR "Unable to allocate memory for attributes " | |
755 | "structure\n"); | |
a17a75e2 MW |
756 | goto err_attr; |
757 | } | |
758 | ||
ead1f3e3 MW |
759 | pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL); |
760 | if (pattern_attr == NULL) { | |
761 | printk(KERN_ERR "Unable to allocate memory for pattern " | |
762 | "attributes\n"); | |
a17a75e2 MW |
763 | goto err_pat; |
764 | } | |
765 | ||
766 | attributes->type = VME_DMA_PATTERN; | |
767 | attributes->private = (void *)pattern_attr; | |
768 | ||
769 | pattern_attr->pattern = pattern; | |
770 | pattern_attr->type = type; | |
771 | ||
772 | return attributes; | |
773 | ||
a17a75e2 MW |
774 | err_pat: |
775 | kfree(attributes); | |
776 | err_attr: | |
777 | return NULL; | |
778 | } | |
779 | EXPORT_SYMBOL(vme_dma_pattern_attribute); | |
780 | ||
781 | /* | |
782 | * Create "PCI" type attributes | |
783 | */ | |
784 | struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address) | |
785 | { | |
786 | struct vme_dma_attr *attributes; | |
787 | struct vme_dma_pci *pci_attr; | |
788 | ||
789 | /* XXX Run some sanity checks here */ | |
790 | ||
ead1f3e3 MW |
791 | attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL); |
792 | if (attributes == NULL) { | |
793 | printk(KERN_ERR "Unable to allocate memory for attributes " | |
794 | "structure\n"); | |
a17a75e2 MW |
795 | goto err_attr; |
796 | } | |
797 | ||
ead1f3e3 MW |
798 | pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL); |
799 | if (pci_attr == NULL) { | |
800 | printk(KERN_ERR "Unable to allocate memory for pci " | |
801 | "attributes\n"); | |
a17a75e2 MW |
802 | goto err_pci; |
803 | } | |
804 | ||
805 | ||
806 | ||
807 | attributes->type = VME_DMA_PCI; | |
808 | attributes->private = (void *)pci_attr; | |
809 | ||
810 | pci_attr->address = address; | |
811 | ||
812 | return attributes; | |
813 | ||
a17a75e2 MW |
814 | err_pci: |
815 | kfree(attributes); | |
816 | err_attr: | |
817 | return NULL; | |
818 | } | |
819 | EXPORT_SYMBOL(vme_dma_pci_attribute); | |
820 | ||
821 | /* | |
822 | * Create "VME" type attributes | |
823 | */ | |
824 | struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address, | |
825 | vme_address_t aspace, vme_cycle_t cycle, vme_width_t dwidth) | |
826 | { | |
827 | struct vme_dma_attr *attributes; | |
828 | struct vme_dma_vme *vme_attr; | |
829 | ||
ead1f3e3 | 830 | attributes = kmalloc( |
a17a75e2 | 831 | sizeof(struct vme_dma_attr), GFP_KERNEL); |
ead1f3e3 MW |
832 | if (attributes == NULL) { |
833 | printk(KERN_ERR "Unable to allocate memory for attributes " | |
834 | "structure\n"); | |
a17a75e2 MW |
835 | goto err_attr; |
836 | } | |
837 | ||
ead1f3e3 MW |
838 | vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL); |
839 | if (vme_attr == NULL) { | |
840 | printk(KERN_ERR "Unable to allocate memory for vme " | |
841 | "attributes\n"); | |
a17a75e2 MW |
842 | goto err_vme; |
843 | } | |
844 | ||
845 | attributes->type = VME_DMA_VME; | |
846 | attributes->private = (void *)vme_attr; | |
847 | ||
848 | vme_attr->address = address; | |
849 | vme_attr->aspace = aspace; | |
850 | vme_attr->cycle = cycle; | |
851 | vme_attr->dwidth = dwidth; | |
852 | ||
853 | return attributes; | |
854 | ||
a17a75e2 MW |
855 | err_vme: |
856 | kfree(attributes); | |
857 | err_attr: | |
858 | return NULL; | |
859 | } | |
860 | EXPORT_SYMBOL(vme_dma_vme_attribute); | |
861 | ||
862 | /* | |
863 | * Free attribute | |
864 | */ | |
865 | void vme_dma_free_attribute(struct vme_dma_attr *attributes) | |
866 | { | |
867 | kfree(attributes->private); | |
868 | kfree(attributes); | |
869 | } | |
870 | EXPORT_SYMBOL(vme_dma_free_attribute); | |
871 | ||
872 | int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src, | |
873 | struct vme_dma_attr *dest, size_t count) | |
874 | { | |
875 | struct vme_bridge *bridge = list->parent->parent; | |
876 | int retval; | |
877 | ||
878 | if (bridge->dma_list_add == NULL) { | |
ead1f3e3 | 879 | printk(KERN_WARNING "Link List DMA generation not supported\n"); |
a17a75e2 MW |
880 | return -EINVAL; |
881 | } | |
882 | ||
886953e9 | 883 | if (!mutex_trylock(&list->mtx)) { |
ead1f3e3 | 884 | printk(KERN_ERR "Link List already submitted\n"); |
a17a75e2 MW |
885 | return -EINVAL; |
886 | } | |
887 | ||
888 | retval = bridge->dma_list_add(list, src, dest, count); | |
889 | ||
886953e9 | 890 | mutex_unlock(&list->mtx); |
a17a75e2 MW |
891 | |
892 | return retval; | |
893 | } | |
894 | EXPORT_SYMBOL(vme_dma_list_add); | |
895 | ||
896 | int vme_dma_list_exec(struct vme_dma_list *list) | |
897 | { | |
898 | struct vme_bridge *bridge = list->parent->parent; | |
899 | int retval; | |
900 | ||
901 | if (bridge->dma_list_exec == NULL) { | |
ead1f3e3 | 902 | printk(KERN_ERR "Link List DMA execution not supported\n"); |
a17a75e2 MW |
903 | return -EINVAL; |
904 | } | |
905 | ||
886953e9 | 906 | mutex_lock(&list->mtx); |
a17a75e2 MW |
907 | |
908 | retval = bridge->dma_list_exec(list); | |
909 | ||
886953e9 | 910 | mutex_unlock(&list->mtx); |
a17a75e2 MW |
911 | |
912 | return retval; | |
913 | } | |
914 | EXPORT_SYMBOL(vme_dma_list_exec); | |
915 | ||
916 | int vme_dma_list_free(struct vme_dma_list *list) | |
917 | { | |
918 | struct vme_bridge *bridge = list->parent->parent; | |
919 | int retval; | |
920 | ||
921 | if (bridge->dma_list_empty == NULL) { | |
ead1f3e3 | 922 | printk(KERN_WARNING "Emptying of Link Lists not supported\n"); |
a17a75e2 MW |
923 | return -EINVAL; |
924 | } | |
925 | ||
886953e9 | 926 | if (!mutex_trylock(&list->mtx)) { |
ead1f3e3 | 927 | printk(KERN_ERR "Link List in use\n"); |
a17a75e2 MW |
928 | return -EINVAL; |
929 | } | |
930 | ||
931 | /* | |
932 | * Empty out all of the entries from the dma list. We need to go to the | |
933 | * low level driver as dma entries are driver specific. | |
934 | */ | |
935 | retval = bridge->dma_list_empty(list); | |
936 | if (retval) { | |
ead1f3e3 | 937 | printk(KERN_ERR "Unable to empty link-list entries\n"); |
886953e9 | 938 | mutex_unlock(&list->mtx); |
a17a75e2 MW |
939 | return retval; |
940 | } | |
886953e9 | 941 | mutex_unlock(&list->mtx); |
a17a75e2 MW |
942 | kfree(list); |
943 | ||
944 | return retval; | |
945 | } | |
946 | EXPORT_SYMBOL(vme_dma_list_free); | |
947 | ||
948 | int vme_dma_free(struct vme_resource *resource) | |
949 | { | |
950 | struct vme_dma_resource *ctrlr; | |
951 | ||
952 | if (resource->type != VME_DMA) { | |
ead1f3e3 | 953 | printk(KERN_ERR "Not a DMA resource\n"); |
a17a75e2 MW |
954 | return -EINVAL; |
955 | } | |
956 | ||
957 | ctrlr = list_entry(resource->entry, struct vme_dma_resource, list); | |
958 | ||
886953e9 | 959 | if (!mutex_trylock(&ctrlr->mtx)) { |
ead1f3e3 | 960 | printk(KERN_ERR "Resource busy, can't free\n"); |
a17a75e2 MW |
961 | return -EBUSY; |
962 | } | |
963 | ||
886953e9 | 964 | if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) { |
ead1f3e3 | 965 | printk(KERN_WARNING "Resource still processing transfers\n"); |
886953e9 | 966 | mutex_unlock(&ctrlr->mtx); |
a17a75e2 MW |
967 | return -EBUSY; |
968 | } | |
969 | ||
970 | ctrlr->locked = 0; | |
971 | ||
886953e9 | 972 | mutex_unlock(&ctrlr->mtx); |
a17a75e2 MW |
973 | |
974 | return 0; | |
975 | } | |
976 | EXPORT_SYMBOL(vme_dma_free); | |
977 | ||
c813f592 MW |
978 | void vme_irq_handler(struct vme_bridge *bridge, int level, int statid) |
979 | { | |
980 | void (*call)(int, int, void *); | |
981 | void *priv_data; | |
982 | ||
983 | call = bridge->irq[level - 1].callback[statid].func; | |
984 | priv_data = bridge->irq[level - 1].callback[statid].priv_data; | |
985 | ||
986 | if (call != NULL) | |
987 | call(level, statid, priv_data); | |
988 | else | |
989 | printk(KERN_WARNING "Spurilous VME interrupt, level:%x, " | |
990 | "vector:%x\n", level, statid); | |
991 | } | |
992 | EXPORT_SYMBOL(vme_irq_handler); | |
993 | ||
8f966dc4 | 994 | int vme_irq_request(struct vme_dev *vdev, int level, int statid, |
29848ac9 | 995 | void (*callback)(int, int, void *), |
a17a75e2 MW |
996 | void *priv_data) |
997 | { | |
998 | struct vme_bridge *bridge; | |
999 | ||
8f966dc4 | 1000 | bridge = vdev->bridge; |
a17a75e2 MW |
1001 | if (bridge == NULL) { |
1002 | printk(KERN_ERR "Can't find VME bus\n"); | |
1003 | return -EINVAL; | |
1004 | } | |
1005 | ||
ead1f3e3 | 1006 | if ((level < 1) || (level > 7)) { |
c813f592 | 1007 | printk(KERN_ERR "Invalid interrupt level\n"); |
a17a75e2 MW |
1008 | return -EINVAL; |
1009 | } | |
1010 | ||
c813f592 MW |
1011 | if (bridge->irq_set == NULL) { |
1012 | printk(KERN_ERR "Configuring interrupts not supported\n"); | |
a17a75e2 MW |
1013 | return -EINVAL; |
1014 | } | |
1015 | ||
886953e9 | 1016 | mutex_lock(&bridge->irq_mtx); |
c813f592 MW |
1017 | |
1018 | if (bridge->irq[level - 1].callback[statid].func) { | |
886953e9 | 1019 | mutex_unlock(&bridge->irq_mtx); |
c813f592 MW |
1020 | printk(KERN_WARNING "VME Interrupt already taken\n"); |
1021 | return -EBUSY; | |
1022 | } | |
1023 | ||
1024 | bridge->irq[level - 1].count++; | |
1025 | bridge->irq[level - 1].callback[statid].priv_data = priv_data; | |
1026 | bridge->irq[level - 1].callback[statid].func = callback; | |
1027 | ||
1028 | /* Enable IRQ level */ | |
29848ac9 | 1029 | bridge->irq_set(bridge, level, 1, 1); |
c813f592 | 1030 | |
886953e9 | 1031 | mutex_unlock(&bridge->irq_mtx); |
c813f592 MW |
1032 | |
1033 | return 0; | |
a17a75e2 | 1034 | } |
c813f592 | 1035 | EXPORT_SYMBOL(vme_irq_request); |
a17a75e2 | 1036 | |
8f966dc4 | 1037 | void vme_irq_free(struct vme_dev *vdev, int level, int statid) |
a17a75e2 MW |
1038 | { |
1039 | struct vme_bridge *bridge; | |
1040 | ||
8f966dc4 | 1041 | bridge = vdev->bridge; |
a17a75e2 MW |
1042 | if (bridge == NULL) { |
1043 | printk(KERN_ERR "Can't find VME bus\n"); | |
1044 | return; | |
1045 | } | |
1046 | ||
ead1f3e3 | 1047 | if ((level < 1) || (level > 7)) { |
c813f592 | 1048 | printk(KERN_ERR "Invalid interrupt level\n"); |
a17a75e2 MW |
1049 | return; |
1050 | } | |
1051 | ||
c813f592 MW |
1052 | if (bridge->irq_set == NULL) { |
1053 | printk(KERN_ERR "Configuring interrupts not supported\n"); | |
a17a75e2 MW |
1054 | return; |
1055 | } | |
1056 | ||
886953e9 | 1057 | mutex_lock(&bridge->irq_mtx); |
c813f592 MW |
1058 | |
1059 | bridge->irq[level - 1].count--; | |
1060 | ||
1061 | /* Disable IRQ level if no more interrupts attached at this level*/ | |
1062 | if (bridge->irq[level - 1].count == 0) | |
29848ac9 | 1063 | bridge->irq_set(bridge, level, 0, 1); |
c813f592 MW |
1064 | |
1065 | bridge->irq[level - 1].callback[statid].func = NULL; | |
1066 | bridge->irq[level - 1].callback[statid].priv_data = NULL; | |
1067 | ||
886953e9 | 1068 | mutex_unlock(&bridge->irq_mtx); |
a17a75e2 | 1069 | } |
c813f592 | 1070 | EXPORT_SYMBOL(vme_irq_free); |
a17a75e2 | 1071 | |
8f966dc4 | 1072 | int vme_irq_generate(struct vme_dev *vdev, int level, int statid) |
a17a75e2 MW |
1073 | { |
1074 | struct vme_bridge *bridge; | |
1075 | ||
8f966dc4 | 1076 | bridge = vdev->bridge; |
a17a75e2 MW |
1077 | if (bridge == NULL) { |
1078 | printk(KERN_ERR "Can't find VME bus\n"); | |
1079 | return -EINVAL; | |
1080 | } | |
1081 | ||
ead1f3e3 | 1082 | if ((level < 1) || (level > 7)) { |
a17a75e2 MW |
1083 | printk(KERN_WARNING "Invalid interrupt level\n"); |
1084 | return -EINVAL; | |
1085 | } | |
1086 | ||
c813f592 | 1087 | if (bridge->irq_generate == NULL) { |
ead1f3e3 | 1088 | printk(KERN_WARNING "Interrupt generation not supported\n"); |
a17a75e2 MW |
1089 | return -EINVAL; |
1090 | } | |
1091 | ||
29848ac9 | 1092 | return bridge->irq_generate(bridge, level, statid); |
a17a75e2 | 1093 | } |
c813f592 | 1094 | EXPORT_SYMBOL(vme_irq_generate); |
a17a75e2 | 1095 | |
42fb5031 MW |
1096 | /* |
1097 | * Request the location monitor, return resource or NULL | |
1098 | */ | |
8f966dc4 | 1099 | struct vme_resource *vme_lm_request(struct vme_dev *vdev) |
a17a75e2 MW |
1100 | { |
1101 | struct vme_bridge *bridge; | |
42fb5031 MW |
1102 | struct list_head *lm_pos = NULL; |
1103 | struct vme_lm_resource *allocated_lm = NULL; | |
1104 | struct vme_lm_resource *lm = NULL; | |
1105 | struct vme_resource *resource = NULL; | |
a17a75e2 | 1106 | |
8f966dc4 | 1107 | bridge = vdev->bridge; |
a17a75e2 MW |
1108 | if (bridge == NULL) { |
1109 | printk(KERN_ERR "Can't find VME bus\n"); | |
42fb5031 MW |
1110 | goto err_bus; |
1111 | } | |
1112 | ||
1113 | /* Loop through DMA resources */ | |
886953e9 | 1114 | list_for_each(lm_pos, &bridge->lm_resources) { |
42fb5031 MW |
1115 | lm = list_entry(lm_pos, |
1116 | struct vme_lm_resource, list); | |
1117 | ||
1118 | if (lm == NULL) { | |
1119 | printk(KERN_ERR "Registered NULL Location Monitor " | |
1120 | "resource\n"); | |
1121 | continue; | |
1122 | } | |
1123 | ||
1124 | /* Find an unlocked controller */ | |
886953e9 | 1125 | mutex_lock(&lm->mtx); |
42fb5031 MW |
1126 | if (lm->locked == 0) { |
1127 | lm->locked = 1; | |
886953e9 | 1128 | mutex_unlock(&lm->mtx); |
42fb5031 MW |
1129 | allocated_lm = lm; |
1130 | break; | |
1131 | } | |
886953e9 | 1132 | mutex_unlock(&lm->mtx); |
42fb5031 MW |
1133 | } |
1134 | ||
1135 | /* Check to see if we found a resource */ | |
1136 | if (allocated_lm == NULL) | |
1137 | goto err_lm; | |
1138 | ||
1139 | resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL); | |
1140 | if (resource == NULL) { | |
1141 | printk(KERN_ERR "Unable to allocate resource structure\n"); | |
1142 | goto err_alloc; | |
1143 | } | |
1144 | resource->type = VME_LM; | |
886953e9 | 1145 | resource->entry = &allocated_lm->list; |
42fb5031 MW |
1146 | |
1147 | return resource; | |
1148 | ||
1149 | err_alloc: | |
1150 | /* Unlock image */ | |
886953e9 | 1151 | mutex_lock(&lm->mtx); |
42fb5031 | 1152 | lm->locked = 0; |
886953e9 | 1153 | mutex_unlock(&lm->mtx); |
42fb5031 MW |
1154 | err_lm: |
1155 | err_bus: | |
1156 | return NULL; | |
1157 | } | |
1158 | EXPORT_SYMBOL(vme_lm_request); | |
1159 | ||
1160 | int vme_lm_count(struct vme_resource *resource) | |
1161 | { | |
1162 | struct vme_lm_resource *lm; | |
1163 | ||
1164 | if (resource->type != VME_LM) { | |
1165 | printk(KERN_ERR "Not a Location Monitor resource\n"); | |
1166 | return -EINVAL; | |
1167 | } | |
1168 | ||
1169 | lm = list_entry(resource->entry, struct vme_lm_resource, list); | |
1170 | ||
1171 | return lm->monitors; | |
1172 | } | |
1173 | EXPORT_SYMBOL(vme_lm_count); | |
1174 | ||
1175 | int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base, | |
1176 | vme_address_t aspace, vme_cycle_t cycle) | |
1177 | { | |
1178 | struct vme_bridge *bridge = find_bridge(resource); | |
1179 | struct vme_lm_resource *lm; | |
1180 | ||
1181 | if (resource->type != VME_LM) { | |
1182 | printk(KERN_ERR "Not a Location Monitor resource\n"); | |
a17a75e2 MW |
1183 | return -EINVAL; |
1184 | } | |
1185 | ||
42fb5031 MW |
1186 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
1187 | ||
a17a75e2 | 1188 | if (bridge->lm_set == NULL) { |
42fb5031 | 1189 | printk(KERN_ERR "vme_lm_set not supported\n"); |
a17a75e2 MW |
1190 | return -EINVAL; |
1191 | } | |
1192 | ||
8be9226c | 1193 | return bridge->lm_set(lm, lm_base, aspace, cycle); |
a17a75e2 MW |
1194 | } |
1195 | EXPORT_SYMBOL(vme_lm_set); | |
1196 | ||
42fb5031 MW |
1197 | int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base, |
1198 | vme_address_t *aspace, vme_cycle_t *cycle) | |
a17a75e2 | 1199 | { |
42fb5031 MW |
1200 | struct vme_bridge *bridge = find_bridge(resource); |
1201 | struct vme_lm_resource *lm; | |
a17a75e2 | 1202 | |
42fb5031 MW |
1203 | if (resource->type != VME_LM) { |
1204 | printk(KERN_ERR "Not a Location Monitor resource\n"); | |
a17a75e2 MW |
1205 | return -EINVAL; |
1206 | } | |
1207 | ||
42fb5031 MW |
1208 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
1209 | ||
a17a75e2 | 1210 | if (bridge->lm_get == NULL) { |
42fb5031 | 1211 | printk(KERN_ERR "vme_lm_get not supported\n"); |
a17a75e2 MW |
1212 | return -EINVAL; |
1213 | } | |
1214 | ||
42fb5031 | 1215 | return bridge->lm_get(lm, lm_base, aspace, cycle); |
a17a75e2 MW |
1216 | } |
1217 | EXPORT_SYMBOL(vme_lm_get); | |
1218 | ||
42fb5031 MW |
1219 | int vme_lm_attach(struct vme_resource *resource, int monitor, |
1220 | void (*callback)(int)) | |
a17a75e2 | 1221 | { |
42fb5031 MW |
1222 | struct vme_bridge *bridge = find_bridge(resource); |
1223 | struct vme_lm_resource *lm; | |
a17a75e2 | 1224 | |
42fb5031 MW |
1225 | if (resource->type != VME_LM) { |
1226 | printk(KERN_ERR "Not a Location Monitor resource\n"); | |
a17a75e2 MW |
1227 | return -EINVAL; |
1228 | } | |
1229 | ||
42fb5031 MW |
1230 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
1231 | ||
a17a75e2 | 1232 | if (bridge->lm_attach == NULL) { |
42fb5031 | 1233 | printk(KERN_ERR "vme_lm_attach not supported\n"); |
a17a75e2 MW |
1234 | return -EINVAL; |
1235 | } | |
1236 | ||
42fb5031 | 1237 | return bridge->lm_attach(lm, monitor, callback); |
a17a75e2 MW |
1238 | } |
1239 | EXPORT_SYMBOL(vme_lm_attach); | |
1240 | ||
42fb5031 | 1241 | int vme_lm_detach(struct vme_resource *resource, int monitor) |
a17a75e2 | 1242 | { |
42fb5031 MW |
1243 | struct vme_bridge *bridge = find_bridge(resource); |
1244 | struct vme_lm_resource *lm; | |
a17a75e2 | 1245 | |
42fb5031 MW |
1246 | if (resource->type != VME_LM) { |
1247 | printk(KERN_ERR "Not a Location Monitor resource\n"); | |
a17a75e2 MW |
1248 | return -EINVAL; |
1249 | } | |
1250 | ||
42fb5031 MW |
1251 | lm = list_entry(resource->entry, struct vme_lm_resource, list); |
1252 | ||
a17a75e2 | 1253 | if (bridge->lm_detach == NULL) { |
42fb5031 | 1254 | printk(KERN_ERR "vme_lm_detach not supported\n"); |
a17a75e2 MW |
1255 | return -EINVAL; |
1256 | } | |
1257 | ||
42fb5031 | 1258 | return bridge->lm_detach(lm, monitor); |
a17a75e2 MW |
1259 | } |
1260 | EXPORT_SYMBOL(vme_lm_detach); | |
1261 | ||
42fb5031 MW |
1262 | void vme_lm_free(struct vme_resource *resource) |
1263 | { | |
1264 | struct vme_lm_resource *lm; | |
1265 | ||
1266 | if (resource->type != VME_LM) { | |
1267 | printk(KERN_ERR "Not a Location Monitor resource\n"); | |
1268 | return; | |
1269 | } | |
1270 | ||
1271 | lm = list_entry(resource->entry, struct vme_lm_resource, list); | |
1272 | ||
886953e9 | 1273 | mutex_lock(&lm->mtx); |
42fb5031 | 1274 | |
8be9226c MW |
1275 | /* XXX |
1276 | * Check to see that there aren't any callbacks still attached, if | |
1277 | * there are we should probably be detaching them! | |
1278 | */ | |
42fb5031 MW |
1279 | |
1280 | lm->locked = 0; | |
1281 | ||
886953e9 | 1282 | mutex_unlock(&lm->mtx); |
8be9226c MW |
1283 | |
1284 | kfree(resource); | |
42fb5031 MW |
1285 | } |
1286 | EXPORT_SYMBOL(vme_lm_free); | |
1287 | ||
8f966dc4 | 1288 | int vme_slot_get(struct vme_dev *vdev) |
a17a75e2 MW |
1289 | { |
1290 | struct vme_bridge *bridge; | |
1291 | ||
8f966dc4 | 1292 | bridge = vdev->bridge; |
a17a75e2 MW |
1293 | if (bridge == NULL) { |
1294 | printk(KERN_ERR "Can't find VME bus\n"); | |
1295 | return -EINVAL; | |
1296 | } | |
1297 | ||
1298 | if (bridge->slot_get == NULL) { | |
ead1f3e3 | 1299 | printk(KERN_WARNING "vme_slot_get not supported\n"); |
a17a75e2 MW |
1300 | return -EINVAL; |
1301 | } | |
1302 | ||
29848ac9 | 1303 | return bridge->slot_get(bridge); |
a17a75e2 MW |
1304 | } |
1305 | EXPORT_SYMBOL(vme_slot_get); | |
1306 | ||
1307 | ||
1308 | /* - Bridge Registration --------------------------------------------------- */ | |
1309 | ||
733e3ef0 | 1310 | static int vme_add_bus(struct vme_bridge *bridge) |
a17a75e2 MW |
1311 | { |
1312 | int i; | |
733e3ef0 | 1313 | int ret = -1; |
a17a75e2 | 1314 | |
733e3ef0 | 1315 | mutex_lock(&vme_buses_lock); |
a17a75e2 | 1316 | for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) { |
733e3ef0 MV |
1317 | if ((vme_bus_numbers & (1 << i)) == 0) { |
1318 | vme_bus_numbers |= (1 << i); | |
1319 | bridge->num = i; | |
5d6abf37 | 1320 | INIT_LIST_HEAD(&bridge->devices); |
733e3ef0 MV |
1321 | list_add_tail(&bridge->bus_list, &vme_bus_list); |
1322 | ret = 0; | |
a17a75e2 MW |
1323 | break; |
1324 | } | |
1325 | } | |
733e3ef0 | 1326 | mutex_unlock(&vme_buses_lock); |
a17a75e2 | 1327 | |
733e3ef0 | 1328 | return ret; |
a17a75e2 MW |
1329 | } |
1330 | ||
733e3ef0 | 1331 | static void vme_remove_bus(struct vme_bridge *bridge) |
a17a75e2 | 1332 | { |
5d6abf37 MV |
1333 | struct vme_dev *vdev; |
1334 | struct vme_dev *tmp; | |
1335 | ||
733e3ef0 MV |
1336 | mutex_lock(&vme_buses_lock); |
1337 | vme_bus_numbers &= ~(1 << bridge->num); | |
5d6abf37 MV |
1338 | list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) { |
1339 | list_del(&vdev->drv_list); | |
1340 | list_del(&vdev->bridge_list); | |
1341 | device_unregister(&vdev->dev); | |
1342 | } | |
733e3ef0 MV |
1343 | list_del(&bridge->bus_list); |
1344 | mutex_unlock(&vme_buses_lock); | |
a17a75e2 MW |
1345 | } |
1346 | ||
f6c39d4f MV |
1347 | static void vme_dev_release(struct device *dev) |
1348 | { | |
8f966dc4 | 1349 | kfree(dev_to_vme_dev(dev)); |
f6c39d4f MV |
1350 | } |
1351 | ||
ead1f3e3 | 1352 | int vme_register_bridge(struct vme_bridge *bridge) |
a17a75e2 | 1353 | { |
5d6abf37 MV |
1354 | return vme_add_bus(bridge); |
1355 | } | |
1356 | EXPORT_SYMBOL(vme_register_bridge); | |
a17a75e2 | 1357 | |
5d6abf37 MV |
1358 | void vme_unregister_bridge(struct vme_bridge *bridge) |
1359 | { | |
1360 | vme_remove_bus(bridge); | |
1361 | } | |
1362 | EXPORT_SYMBOL(vme_unregister_bridge); | |
a17a75e2 | 1363 | |
5d6abf37 MV |
1364 | /* - Driver Registration --------------------------------------------------- */ |
1365 | ||
1366 | static int __vme_register_driver_bus(struct vme_driver *drv, | |
1367 | struct vme_bridge *bridge, unsigned int ndevs) | |
1368 | { | |
1369 | int err; | |
1370 | unsigned int i; | |
1371 | struct vme_dev *vdev; | |
1372 | struct vme_dev *tmp; | |
1373 | ||
1374 | for (i = 0; i < ndevs; i++) { | |
1375 | vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL); | |
1376 | if (!vdev) { | |
1377 | err = -ENOMEM; | |
f6c39d4f MV |
1378 | goto err_devalloc; |
1379 | } | |
a916a391 | 1380 | vdev->num = i; |
8f966dc4 | 1381 | vdev->bridge = bridge; |
5d6abf37 MV |
1382 | vdev->dev.platform_data = drv; |
1383 | vdev->dev.release = vme_dev_release; | |
8f966dc4 MV |
1384 | vdev->dev.parent = bridge->parent; |
1385 | vdev->dev.bus = &vme_bus_type; | |
a916a391 MV |
1386 | dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num, |
1387 | vdev->num); | |
a17a75e2 | 1388 | |
5d6abf37 MV |
1389 | err = device_register(&vdev->dev); |
1390 | if (err) | |
a17a75e2 | 1391 | goto err_reg; |
a17a75e2 | 1392 | |
5d6abf37 MV |
1393 | if (vdev->dev.platform_data) { |
1394 | list_add_tail(&vdev->drv_list, &drv->devices); | |
1395 | list_add_tail(&vdev->bridge_list, &bridge->devices); | |
1396 | } else | |
1397 | device_unregister(&vdev->dev); | |
1398 | } | |
1399 | return 0; | |
a17a75e2 | 1400 | |
a17a75e2 | 1401 | err_reg: |
8f966dc4 | 1402 | kfree(vdev); |
f6c39d4f | 1403 | err_devalloc: |
5d6abf37 MV |
1404 | list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) { |
1405 | list_del(&vdev->drv_list); | |
1406 | list_del(&vdev->bridge_list); | |
8f966dc4 | 1407 | device_unregister(&vdev->dev); |
a17a75e2 | 1408 | } |
5d6abf37 | 1409 | return err; |
a17a75e2 | 1410 | } |
a17a75e2 | 1411 | |
5d6abf37 | 1412 | static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs) |
a17a75e2 | 1413 | { |
5d6abf37 MV |
1414 | struct vme_bridge *bridge; |
1415 | int err = 0; | |
a17a75e2 | 1416 | |
5d6abf37 MV |
1417 | mutex_lock(&vme_buses_lock); |
1418 | list_for_each_entry(bridge, &vme_bus_list, bus_list) { | |
1419 | /* | |
1420 | * This cannot cause trouble as we already have vme_buses_lock | |
1421 | * and if the bridge is removed, it will have to go through | |
1422 | * vme_unregister_bridge() to do it (which calls remove() on | |
1423 | * the bridge which in turn tries to acquire vme_buses_lock and | |
1424 | * will have to wait). The probe() called after device | |
1425 | * registration in __vme_register_driver below will also fail | |
1426 | * as the bridge is being removed (since the probe() calls | |
1427 | * vme_bridge_get()). | |
1428 | */ | |
1429 | err = __vme_register_driver_bus(drv, bridge, ndevs); | |
1430 | if (err) | |
1431 | break; | |
a17a75e2 | 1432 | } |
5d6abf37 MV |
1433 | mutex_unlock(&vme_buses_lock); |
1434 | return err; | |
a17a75e2 | 1435 | } |
a17a75e2 | 1436 | |
5d6abf37 | 1437 | int vme_register_driver(struct vme_driver *drv, unsigned int ndevs) |
a17a75e2 | 1438 | { |
5d6abf37 MV |
1439 | int err; |
1440 | ||
a17a75e2 MW |
1441 | drv->driver.name = drv->name; |
1442 | drv->driver.bus = &vme_bus_type; | |
5d6abf37 MV |
1443 | INIT_LIST_HEAD(&drv->devices); |
1444 | ||
1445 | err = driver_register(&drv->driver); | |
1446 | if (err) | |
1447 | return err; | |
a17a75e2 | 1448 | |
5d6abf37 MV |
1449 | err = __vme_register_driver(drv, ndevs); |
1450 | if (err) | |
1451 | driver_unregister(&drv->driver); | |
1452 | ||
1453 | return err; | |
a17a75e2 MW |
1454 | } |
1455 | EXPORT_SYMBOL(vme_register_driver); | |
1456 | ||
ead1f3e3 | 1457 | void vme_unregister_driver(struct vme_driver *drv) |
a17a75e2 | 1458 | { |
5d6abf37 MV |
1459 | struct vme_dev *dev, *dev_tmp; |
1460 | ||
1461 | mutex_lock(&vme_buses_lock); | |
1462 | list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) { | |
1463 | list_del(&dev->drv_list); | |
1464 | list_del(&dev->bridge_list); | |
1465 | device_unregister(&dev->dev); | |
1466 | } | |
1467 | mutex_unlock(&vme_buses_lock); | |
1468 | ||
a17a75e2 MW |
1469 | driver_unregister(&drv->driver); |
1470 | } | |
1471 | EXPORT_SYMBOL(vme_unregister_driver); | |
1472 | ||
1473 | /* - Bus Registration ------------------------------------------------------ */ | |
1474 | ||
a17a75e2 MW |
1475 | static int vme_bus_match(struct device *dev, struct device_driver *drv) |
1476 | { | |
5d6abf37 | 1477 | struct vme_driver *vme_drv; |
a17a75e2 | 1478 | |
5d6abf37 | 1479 | vme_drv = container_of(drv, struct vme_driver, driver); |
a17a75e2 | 1480 | |
5d6abf37 MV |
1481 | if (dev->platform_data == vme_drv) { |
1482 | struct vme_dev *vdev = dev_to_vme_dev(dev); | |
a17a75e2 | 1483 | |
5d6abf37 MV |
1484 | if (vme_drv->match && vme_drv->match(vdev)) |
1485 | return 1; | |
a37b0dad | 1486 | |
5d6abf37 | 1487 | dev->platform_data = NULL; |
a17a75e2 | 1488 | } |
a17a75e2 MW |
1489 | return 0; |
1490 | } | |
1491 | ||
1492 | static int vme_bus_probe(struct device *dev) | |
1493 | { | |
a17a75e2 | 1494 | int retval = -ENODEV; |
5d6abf37 MV |
1495 | struct vme_driver *driver; |
1496 | struct vme_dev *vdev = dev_to_vme_dev(dev); | |
a17a75e2 | 1497 | |
5d6abf37 | 1498 | driver = dev->platform_data; |
a17a75e2 | 1499 | |
ead1f3e3 | 1500 | if (driver->probe != NULL) |
8f966dc4 | 1501 | retval = driver->probe(vdev); |
a17a75e2 MW |
1502 | |
1503 | return retval; | |
1504 | } | |
1505 | ||
1506 | static int vme_bus_remove(struct device *dev) | |
1507 | { | |
a17a75e2 | 1508 | int retval = -ENODEV; |
5d6abf37 MV |
1509 | struct vme_driver *driver; |
1510 | struct vme_dev *vdev = dev_to_vme_dev(dev); | |
a17a75e2 | 1511 | |
5d6abf37 | 1512 | driver = dev->platform_data; |
a17a75e2 | 1513 | |
ead1f3e3 | 1514 | if (driver->remove != NULL) |
8f966dc4 | 1515 | retval = driver->remove(vdev); |
a17a75e2 MW |
1516 | |
1517 | return retval; | |
1518 | } | |
1519 | ||
1520 | struct bus_type vme_bus_type = { | |
1521 | .name = "vme", | |
1522 | .match = vme_bus_match, | |
1523 | .probe = vme_bus_probe, | |
1524 | .remove = vme_bus_remove, | |
1525 | }; | |
1526 | EXPORT_SYMBOL(vme_bus_type); | |
1527 | ||
ead1f3e3 | 1528 | static int __init vme_init(void) |
a17a75e2 MW |
1529 | { |
1530 | return bus_register(&vme_bus_type); | |
1531 | } | |
1532 | ||
ead1f3e3 | 1533 | static void __exit vme_exit(void) |
a17a75e2 MW |
1534 | { |
1535 | bus_unregister(&vme_bus_type); | |
1536 | } | |
1537 | ||
1538 | MODULE_DESCRIPTION("VME bridge driver framework"); | |
66bd8db5 | 1539 | MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com"); |
a17a75e2 MW |
1540 | MODULE_LICENSE("GPL"); |
1541 | ||
1542 | module_init(vme_init); | |
1543 | module_exit(vme_exit); |