blob: 5b4c898d75099b63f528295d6cda25f31efd9857 [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
Paul Gortmaker050c3d52016-07-03 14:05:56 -040016#include <linux/init.h>
17#include <linux/export.h>
Martyn Welcha17a75e2009-07-31 09:28:17 +010018#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 int __init vme_init(void);
Martyn Welcha17a75e2009-07-31 09:28:17 +010043
Manohar Vanga8f966dc2011-09-26 11:27:15 +020044static struct vme_dev *dev_to_vme_dev(struct device *dev)
Martyn Welcha17a75e2009-07-31 09:28:17 +010045{
Manohar Vanga8f966dc2011-09-26 11:27:15 +020046 return container_of(dev, struct vme_dev, dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +010047}
48
49/*
50 * Find the bridge that the resource is associated with.
51 */
52static struct vme_bridge *find_bridge(struct vme_resource *resource)
53{
54 /* Get list to search */
55 switch (resource->type) {
56 case VME_MASTER:
57 return list_entry(resource->entry, struct vme_master_resource,
58 list)->parent;
59 break;
60 case VME_SLAVE:
61 return list_entry(resource->entry, struct vme_slave_resource,
62 list)->parent;
63 break;
64 case VME_DMA:
65 return list_entry(resource->entry, struct vme_dma_resource,
66 list)->parent;
67 break;
Martyn Welch42fb5032009-08-11 17:44:56 +010068 case VME_LM:
69 return list_entry(resource->entry, struct vme_lm_resource,
70 list)->parent;
71 break;
Martyn Welcha17a75e2009-07-31 09:28:17 +010072 default:
73 printk(KERN_ERR "Unknown resource type\n");
74 return NULL;
75 break;
76 }
77}
78
Martyn Welchb5bc9802017-03-04 00:34:29 +000079/**
80 * vme_free_consistent - Allocate contiguous memory.
81 * @resource: Pointer to VME resource.
82 * @size: Size of allocation required.
83 * @dma: Pointer to variable to store physical address of allocation.
84 *
Martyn Welcha17a75e2009-07-31 09:28:17 +010085 * Allocate a contiguous block of memory for use by the driver. This is used to
86 * create the buffers for the slave windows.
Martyn Welchb5bc9802017-03-04 00:34:29 +000087 *
88 * Return: Virtual address of allocation on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +010089 */
Martyn Welchead1f3e2009-12-15 08:43:02 +000090void *vme_alloc_consistent(struct vme_resource *resource, size_t size,
Martyn Welcha17a75e2009-07-31 09:28:17 +010091 dma_addr_t *dma)
92{
93 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +010094
Martyn Welchead1f3e2009-12-15 08:43:02 +000095 if (resource == NULL) {
96 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +010097 return NULL;
98 }
99
100 bridge = find_bridge(resource);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000101 if (bridge == NULL) {
102 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100103 return NULL;
104 }
105
Martyn Welcha17a75e2009-07-31 09:28:17 +0100106 if (bridge->parent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700107 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100108 return NULL;
109 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100110
Manohar Vanga7f58f022011-08-10 11:33:46 +0200111 if (bridge->alloc_consistent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700112 printk(KERN_ERR "alloc_consistent not supported by bridge %s\n",
113 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200114 return NULL;
115 }
116
117 return bridge->alloc_consistent(bridge->parent, size, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100118}
119EXPORT_SYMBOL(vme_alloc_consistent);
120
Martyn Welchb5bc9802017-03-04 00:34:29 +0000121/**
122 * vme_free_consistent - Free previously allocated memory.
123 * @resource: Pointer to VME resource.
124 * @size: Size of allocation to free.
125 * @vaddr: Virtual address of allocation.
126 * @dma: Physical address of allocation.
127 *
128 * Free previously allocated block of contiguous memory.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100129 */
130void vme_free_consistent(struct vme_resource *resource, size_t size,
131 void *vaddr, dma_addr_t dma)
132{
133 struct vme_bridge *bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100134
Martyn Welchead1f3e2009-12-15 08:43:02 +0000135 if (resource == NULL) {
136 printk(KERN_ERR "No resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100137 return;
138 }
139
140 bridge = find_bridge(resource);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000141 if (bridge == NULL) {
142 printk(KERN_ERR "Can't find bridge\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100143 return;
144 }
145
Manohar Vanga7f58f022011-08-10 11:33:46 +0200146 if (bridge->parent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700147 printk(KERN_ERR "Dev entry NULL for bridge %s\n", bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200148 return;
149 }
Martyn Welcha17a75e2009-07-31 09:28:17 +0100150
Manohar Vanga7f58f022011-08-10 11:33:46 +0200151 if (bridge->free_consistent == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700152 printk(KERN_ERR "free_consistent not supported by bridge %s\n",
153 bridge->name);
Manohar Vanga7f58f022011-08-10 11:33:46 +0200154 return;
155 }
156
157 bridge->free_consistent(bridge->parent, size, vaddr, dma);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100158}
159EXPORT_SYMBOL(vme_free_consistent);
160
Martyn Welchb5bc9802017-03-04 00:34:29 +0000161/**
162 * vme_get_size - Helper function returning size of a VME window
163 * @resource: Pointer to VME slave or master resource.
164 *
165 * Determine the size of the VME window provided. This is a helper
166 * function, wrappering the call to vme_master_get or vme_slave_get
167 * depending on the type of window resource handed to it.
168 *
169 * Return: Size of the window on success, zero on failure.
170 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100171size_t vme_get_size(struct vme_resource *resource)
172{
173 int enabled, retval;
174 unsigned long long base, size;
175 dma_addr_t buf_base;
Martyn Welch6af04b02011-12-01 17:06:29 +0000176 u32 aspace, cycle, dwidth;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100177
178 switch (resource->type) {
179 case VME_MASTER:
180 retval = vme_master_get(resource, &enabled, &base, &size,
181 &aspace, &cycle, &dwidth);
Martyn Welch6ad37562016-10-21 17:36:19 +0100182 if (retval)
183 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100184
185 return size;
186 break;
187 case VME_SLAVE:
188 retval = vme_slave_get(resource, &enabled, &base, &size,
189 &buf_base, &aspace, &cycle);
Martyn Welch6ad37562016-10-21 17:36:19 +0100190 if (retval)
191 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100192
193 return size;
194 break;
195 case VME_DMA:
196 return 0;
197 break;
198 default:
199 printk(KERN_ERR "Unknown resource type\n");
200 return 0;
201 break;
202 }
203}
204EXPORT_SYMBOL(vme_get_size);
205
Dmitry Kalinkinef73f882015-05-28 15:07:04 +0300206int vme_check_window(u32 aspace, unsigned long long vme_base,
207 unsigned long long size)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100208{
209 int retval = 0;
210
Dan Carpenterbd147982017-09-30 14:27:41 +0300211 if (vme_base + size < size)
212 return -EINVAL;
213
Martyn Welcha17a75e2009-07-31 09:28:17 +0100214 switch (aspace) {
215 case VME_A16:
Dan Carpenterbd147982017-09-30 14:27:41 +0300216 if (vme_base + size > VME_A16_MAX)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100217 retval = -EFAULT;
218 break;
219 case VME_A24:
Dan Carpenterbd147982017-09-30 14:27:41 +0300220 if (vme_base + size > VME_A24_MAX)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100221 retval = -EFAULT;
222 break;
223 case VME_A32:
Dan Carpenterbd147982017-09-30 14:27:41 +0300224 if (vme_base + size > VME_A32_MAX)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100225 retval = -EFAULT;
226 break;
227 case VME_A64:
Dan Carpenterbd147982017-09-30 14:27:41 +0300228 /* The VME_A64_MAX limit is actually U64_MAX + 1 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100229 break;
230 case VME_CRCSR:
Dan Carpenterbd147982017-09-30 14:27:41 +0300231 if (vme_base + size > VME_CRCSR_MAX)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100232 retval = -EFAULT;
233 break;
234 case VME_USER1:
235 case VME_USER2:
236 case VME_USER3:
237 case VME_USER4:
238 /* User Defined */
239 break;
240 default:
Martyn Welchead1f3e2009-12-15 08:43:02 +0000241 printk(KERN_ERR "Invalid address space\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100242 retval = -EINVAL;
243 break;
244 }
245
246 return retval;
247}
Dmitry Kalinkinef73f882015-05-28 15:07:04 +0300248EXPORT_SYMBOL(vme_check_window);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100249
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +0300250static u32 vme_get_aspace(int am)
251{
252 switch (am) {
253 case 0x29:
254 case 0x2D:
255 return VME_A16;
256 case 0x38:
257 case 0x39:
258 case 0x3A:
259 case 0x3B:
260 case 0x3C:
261 case 0x3D:
262 case 0x3E:
263 case 0x3F:
264 return VME_A24;
265 case 0x8:
266 case 0x9:
267 case 0xA:
268 case 0xB:
269 case 0xC:
270 case 0xD:
271 case 0xE:
272 case 0xF:
273 return VME_A32;
274 case 0x0:
275 case 0x1:
276 case 0x3:
277 return VME_A64;
278 }
279
280 return 0;
281}
282
Martyn Welchb5bc9802017-03-04 00:34:29 +0000283/**
284 * vme_slave_request - Request a VME slave window resource.
285 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
286 * @address: Required VME address space.
287 * @cycle: Required VME data transfer cycle type.
288 *
289 * Request use of a VME window resource capable of being set for the requested
290 * address space and data transfer cycle.
291 *
292 * Return: Pointer to VME resource on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100293 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000294struct vme_resource *vme_slave_request(struct vme_dev *vdev, u32 address,
295 u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100296{
297 struct vme_bridge *bridge;
298 struct list_head *slave_pos = NULL;
299 struct vme_slave_resource *allocated_image = NULL;
300 struct vme_slave_resource *slave_image = NULL;
301 struct vme_resource *resource = NULL;
302
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200303 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100304 if (bridge == NULL) {
305 printk(KERN_ERR "Can't find VME bus\n");
306 goto err_bus;
307 }
308
309 /* Loop through slave resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000310 list_for_each(slave_pos, &bridge->slave_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100311 slave_image = list_entry(slave_pos,
312 struct vme_slave_resource, list);
313
314 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000315 printk(KERN_ERR "Registered NULL Slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100316 continue;
317 }
318
319 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000320 mutex_lock(&slave_image->mtx);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000321 if (((slave_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100322 ((slave_image->cycle_attr & cycle) == cycle) &&
323 (slave_image->locked == 0)) {
324
325 slave_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000326 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100327 allocated_image = slave_image;
328 break;
329 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000330 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100331 }
332
333 /* No free image */
334 if (allocated_image == NULL)
335 goto err_image;
336
337 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
338 if (resource == NULL) {
339 printk(KERN_WARNING "Unable to allocate resource structure\n");
340 goto err_alloc;
341 }
342 resource->type = VME_SLAVE;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000343 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100344
345 return resource;
346
347err_alloc:
348 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000349 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100350 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000351 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100352err_image:
353err_bus:
354 return NULL;
355}
356EXPORT_SYMBOL(vme_slave_request);
357
Martyn Welchb5bc9802017-03-04 00:34:29 +0000358/**
359 * vme_slave_set - Set VME slave window configuration.
360 * @resource: Pointer to VME slave resource.
361 * @enabled: State to which the window should be configured.
362 * @vme_base: Base address for the window.
363 * @size: Size of the VME window.
364 * @buf_base: Based address of buffer used to provide VME slave window storage.
365 * @aspace: VME address space for the VME window.
366 * @cycle: VME data transfer cycle type for the VME window.
367 *
368 * Set configuration for provided VME slave window.
369 *
370 * Return: Zero on success, -EINVAL if operation is not supported on this
371 * device, if an invalid resource has been provided or invalid
372 * attributes are provided. Hardware specific errors may also be
373 * returned.
374 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000375int vme_slave_set(struct vme_resource *resource, int enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100376 unsigned long long vme_base, unsigned long long size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000377 dma_addr_t buf_base, u32 aspace, u32 cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100378{
379 struct vme_bridge *bridge = find_bridge(resource);
380 struct vme_slave_resource *image;
381 int retval;
382
383 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000384 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100385 return -EINVAL;
386 }
387
388 image = list_entry(resource->entry, struct vme_slave_resource, list);
389
390 if (bridge->slave_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000391 printk(KERN_ERR "Function not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100392 return -ENOSYS;
393 }
394
Martyn Welchead1f3e2009-12-15 08:43:02 +0000395 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100396 ((image->cycle_attr & cycle) == cycle))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000397 printk(KERN_ERR "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100398 return -EINVAL;
399 }
400
401 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000402 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100403 return retval;
404
405 return bridge->slave_set(image, enabled, vme_base, size, buf_base,
406 aspace, cycle);
407}
408EXPORT_SYMBOL(vme_slave_set);
409
Martyn Welchb5bc9802017-03-04 00:34:29 +0000410/**
411 * vme_slave_get - Retrieve VME slave window configuration.
412 * @resource: Pointer to VME slave resource.
413 * @enabled: Pointer to variable for storing state.
414 * @vme_base: Pointer to variable for storing window base address.
415 * @size: Pointer to variable for storing window size.
416 * @buf_base: Pointer to variable for storing slave buffer base address.
417 * @aspace: Pointer to variable for storing VME address space.
418 * @cycle: Pointer to variable for storing VME data transfer cycle type.
419 *
420 * Return configuration for provided VME slave window.
421 *
422 * Return: Zero on success, -EINVAL if operation is not supported on this
423 * device or if an invalid resource has been provided.
424 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000425int vme_slave_get(struct vme_resource *resource, int *enabled,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100426 unsigned long long *vme_base, unsigned long long *size,
Martyn Welch6af04b02011-12-01 17:06:29 +0000427 dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100428{
429 struct vme_bridge *bridge = find_bridge(resource);
430 struct vme_slave_resource *image;
431
432 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000433 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100434 return -EINVAL;
435 }
436
437 image = list_entry(resource->entry, struct vme_slave_resource, list);
438
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200439 if (bridge->slave_get == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000440 printk(KERN_ERR "vme_slave_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100441 return -EINVAL;
442 }
443
444 return bridge->slave_get(image, enabled, vme_base, size, buf_base,
445 aspace, cycle);
446}
447EXPORT_SYMBOL(vme_slave_get);
448
Martyn Welchb5bc9802017-03-04 00:34:29 +0000449/**
450 * vme_slave_free - Free VME slave window
451 * @resource: Pointer to VME slave resource.
452 *
453 * Free the provided slave resource so that it may be reallocated.
454 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100455void vme_slave_free(struct vme_resource *resource)
456{
457 struct vme_slave_resource *slave_image;
458
459 if (resource->type != VME_SLAVE) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000460 printk(KERN_ERR "Not a slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100461 return;
462 }
463
464 slave_image = list_entry(resource->entry, struct vme_slave_resource,
465 list);
466 if (slave_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000467 printk(KERN_ERR "Can't find slave resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100468 return;
469 }
470
471 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000472 mutex_lock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100473 if (slave_image->locked == 0)
474 printk(KERN_ERR "Image is already free\n");
475
476 slave_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000477 mutex_unlock(&slave_image->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100478
479 /* Free up resource memory */
480 kfree(resource);
481}
482EXPORT_SYMBOL(vme_slave_free);
483
Martyn Welchb5bc9802017-03-04 00:34:29 +0000484/**
485 * vme_master_request - Request a VME master window resource.
486 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
487 * @address: Required VME address space.
488 * @cycle: Required VME data transfer cycle type.
489 * @dwidth: Required VME data transfer width.
490 *
491 * Request use of a VME window resource capable of being set for the requested
492 * address space, data transfer cycle and width.
493 *
494 * Return: Pointer to VME resource on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100495 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000496struct vme_resource *vme_master_request(struct vme_dev *vdev, u32 address,
497 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100498{
499 struct vme_bridge *bridge;
500 struct list_head *master_pos = NULL;
501 struct vme_master_resource *allocated_image = NULL;
502 struct vme_master_resource *master_image = NULL;
503 struct vme_resource *resource = NULL;
504
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200505 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100506 if (bridge == NULL) {
507 printk(KERN_ERR "Can't find VME bus\n");
508 goto err_bus;
509 }
510
511 /* Loop through master resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000512 list_for_each(master_pos, &bridge->master_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100513 master_image = list_entry(master_pos,
514 struct vme_master_resource, list);
515
516 if (master_image == NULL) {
517 printk(KERN_WARNING "Registered NULL master resource\n");
518 continue;
519 }
520
521 /* Find an unlocked and compatible image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000522 spin_lock(&master_image->lock);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000523 if (((master_image->address_attr & address) == address) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100524 ((master_image->cycle_attr & cycle) == cycle) &&
525 ((master_image->width_attr & dwidth) == dwidth) &&
526 (master_image->locked == 0)) {
527
528 master_image->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000529 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100530 allocated_image = master_image;
531 break;
532 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000533 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100534 }
535
536 /* Check to see if we found a resource */
537 if (allocated_image == NULL) {
538 printk(KERN_ERR "Can't find a suitable resource\n");
539 goto err_image;
540 }
541
542 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
543 if (resource == NULL) {
544 printk(KERN_ERR "Unable to allocate resource structure\n");
545 goto err_alloc;
546 }
547 resource->type = VME_MASTER;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000548 resource->entry = &allocated_image->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100549
550 return resource;
551
Martyn Welcha17a75e2009-07-31 09:28:17 +0100552err_alloc:
553 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000554 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100555 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000556 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100557err_image:
558err_bus:
559 return NULL;
560}
561EXPORT_SYMBOL(vme_master_request);
562
Martyn Welchb5bc9802017-03-04 00:34:29 +0000563/**
564 * vme_master_set - Set VME master window configuration.
565 * @resource: Pointer to VME master resource.
566 * @enabled: State to which the window should be configured.
567 * @vme_base: Base address for the window.
568 * @size: Size of the VME window.
569 * @aspace: VME address space for the VME window.
570 * @cycle: VME data transfer cycle type for the VME window.
571 * @dwidth: VME data transfer width for the VME window.
572 *
573 * Set configuration for provided VME master window.
574 *
575 * Return: Zero on success, -EINVAL if operation is not supported on this
576 * device, if an invalid resource has been provided or invalid
577 * attributes are provided. Hardware specific errors may also be
578 * returned.
579 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000580int vme_master_set(struct vme_resource *resource, int enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000581 unsigned long long vme_base, unsigned long long size, u32 aspace,
582 u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100583{
584 struct vme_bridge *bridge = find_bridge(resource);
585 struct vme_master_resource *image;
586 int retval;
587
588 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000589 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100590 return -EINVAL;
591 }
592
593 image = list_entry(resource->entry, struct vme_master_resource, list);
594
595 if (bridge->master_set == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000596 printk(KERN_WARNING "vme_master_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100597 return -EINVAL;
598 }
599
Martyn Welchead1f3e2009-12-15 08:43:02 +0000600 if (!(((image->address_attr & aspace) == aspace) &&
Martyn Welcha17a75e2009-07-31 09:28:17 +0100601 ((image->cycle_attr & cycle) == cycle) &&
602 ((image->width_attr & dwidth) == dwidth))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000603 printk(KERN_WARNING "Invalid attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100604 return -EINVAL;
605 }
606
607 retval = vme_check_window(aspace, vme_base, size);
Martyn Welchead1f3e2009-12-15 08:43:02 +0000608 if (retval)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100609 return retval;
610
611 return bridge->master_set(image, enabled, vme_base, size, aspace,
612 cycle, dwidth);
613}
614EXPORT_SYMBOL(vme_master_set);
615
Martyn Welchb5bc9802017-03-04 00:34:29 +0000616/**
617 * vme_master_get - Retrieve VME master window configuration.
618 * @resource: Pointer to VME master resource.
619 * @enabled: Pointer to variable for storing state.
620 * @vme_base: Pointer to variable for storing window base address.
621 * @size: Pointer to variable for storing window size.
622 * @aspace: Pointer to variable for storing VME address space.
623 * @cycle: Pointer to variable for storing VME data transfer cycle type.
624 * @dwidth: Pointer to variable for storing VME data transfer width.
625 *
626 * Return configuration for provided VME master window.
627 *
628 * Return: Zero on success, -EINVAL if operation is not supported on this
629 * device or if an invalid resource has been provided.
630 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000631int vme_master_get(struct vme_resource *resource, int *enabled,
Martyn Welch6af04b02011-12-01 17:06:29 +0000632 unsigned long long *vme_base, unsigned long long *size, u32 *aspace,
633 u32 *cycle, u32 *dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100634{
635 struct vme_bridge *bridge = find_bridge(resource);
636 struct vme_master_resource *image;
637
638 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000639 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100640 return -EINVAL;
641 }
642
643 image = list_entry(resource->entry, struct vme_master_resource, list);
644
Emilio G. Cota51a569f2009-08-10 16:52:42 +0200645 if (bridge->master_get == NULL) {
Julia Lawallc1038302014-12-07 20:20:53 +0100646 printk(KERN_WARNING "%s not supported\n", __func__);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100647 return -EINVAL;
648 }
649
650 return bridge->master_get(image, enabled, vme_base, size, aspace,
651 cycle, dwidth);
652}
653EXPORT_SYMBOL(vme_master_get);
654
Martyn Welchb5bc9802017-03-04 00:34:29 +0000655/**
656 * vme_master_write - Read data from VME space into a buffer.
657 * @resource: Pointer to VME master resource.
658 * @buf: Pointer to buffer where data should be transferred.
659 * @count: Number of bytes to transfer.
660 * @offset: Offset into VME master window at which to start transfer.
661 *
662 * Perform read of count bytes of data from location on VME bus which maps into
663 * the VME master window at offset to buf.
664 *
665 * Return: Number of bytes read, -EINVAL if resource is not a VME master
666 * resource or read operation is not supported. -EFAULT returned if
667 * invalid offset is provided. Hardware specific errors may also be
668 * returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100669 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000670ssize_t vme_master_read(struct vme_resource *resource, void *buf, size_t count,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100671 loff_t offset)
672{
673 struct vme_bridge *bridge = find_bridge(resource);
674 struct vme_master_resource *image;
675 size_t length;
676
677 if (bridge->master_read == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000678 printk(KERN_WARNING "Reading from resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100679 return -EINVAL;
680 }
681
682 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000683 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100684 return -EINVAL;
685 }
686
687 image = list_entry(resource->entry, struct vme_master_resource, list);
688
689 length = vme_get_size(resource);
690
691 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000692 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100693 return -EFAULT;
694 }
695
696 if ((offset + count) > length)
697 count = length - offset;
698
699 return bridge->master_read(image, buf, count, offset);
700
701}
702EXPORT_SYMBOL(vme_master_read);
703
Martyn Welchb5bc9802017-03-04 00:34:29 +0000704/**
705 * vme_master_write - Write data out to VME space from a buffer.
706 * @resource: Pointer to VME master resource.
707 * @buf: Pointer to buffer holding data to transfer.
708 * @count: Number of bytes to transfer.
709 * @offset: Offset into VME master window at which to start transfer.
710 *
711 * Perform write of count bytes of data from buf to location on VME bus which
712 * maps into the VME master window at offset.
713 *
714 * Return: Number of bytes written, -EINVAL if resource is not a VME master
715 * resource or write operation is not supported. -EFAULT returned if
716 * invalid offset is provided. Hardware specific errors may also be
717 * returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100718 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000719ssize_t vme_master_write(struct vme_resource *resource, void *buf,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100720 size_t count, loff_t offset)
721{
722 struct vme_bridge *bridge = find_bridge(resource);
723 struct vme_master_resource *image;
724 size_t length;
725
726 if (bridge->master_write == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000727 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100728 return -EINVAL;
729 }
730
731 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000732 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100733 return -EINVAL;
734 }
735
736 image = list_entry(resource->entry, struct vme_master_resource, list);
737
738 length = vme_get_size(resource);
739
740 if (offset > length) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000741 printk(KERN_WARNING "Invalid Offset\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100742 return -EFAULT;
743 }
744
745 if ((offset + count) > length)
746 count = length - offset;
747
748 return bridge->master_write(image, buf, count, offset);
749}
750EXPORT_SYMBOL(vme_master_write);
751
Martyn Welchb5bc9802017-03-04 00:34:29 +0000752/**
753 * vme_master_rmw - Perform read-modify-write cycle.
754 * @resource: Pointer to VME master resource.
755 * @mask: Bits to be compared and swapped in operation.
756 * @compare: Bits to be compared with data read from offset.
757 * @swap: Bits to be swapped in data read from offset.
758 * @offset: Offset into VME master window at which to perform operation.
759 *
760 * Perform read-modify-write cycle on provided location:
761 * - Location on VME bus is read.
762 * - Bits selected by mask are compared with compare.
763 * - Where a selected bit matches that in compare and are selected in swap,
764 * the bit is swapped.
765 * - Result written back to location on VME bus.
766 *
767 * Return: Bytes written on success, -EINVAL if resource is not a VME master
768 * resource or RMW operation is not supported. Hardware specific
769 * errors may also be returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100770 */
Martyn Welchead1f3e2009-12-15 08:43:02 +0000771unsigned int vme_master_rmw(struct vme_resource *resource, unsigned int mask,
Martyn Welcha17a75e2009-07-31 09:28:17 +0100772 unsigned int compare, unsigned int swap, loff_t offset)
773{
774 struct vme_bridge *bridge = find_bridge(resource);
775 struct vme_master_resource *image;
776
777 if (bridge->master_rmw == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000778 printk(KERN_WARNING "Writing to resource not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100779 return -EINVAL;
780 }
781
782 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000783 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100784 return -EINVAL;
785 }
786
787 image = list_entry(resource->entry, struct vme_master_resource, list);
788
789 return bridge->master_rmw(image, mask, compare, swap, offset);
790}
791EXPORT_SYMBOL(vme_master_rmw);
792
Martyn Welchb5bc9802017-03-04 00:34:29 +0000793/**
794 * vme_master_mmap - Mmap region of VME master window.
795 * @resource: Pointer to VME master resource.
796 * @vma: Pointer to definition of user mapping.
797 *
798 * Memory map a region of the VME master window into user space.
799 *
800 * Return: Zero on success, -EINVAL if resource is not a VME master
801 * resource or -EFAULT if map exceeds window size. Other generic mmap
802 * errors may also be returned.
803 */
Dmitry Kalinkinc74a8042015-02-26 18:53:10 +0300804int vme_master_mmap(struct vme_resource *resource, struct vm_area_struct *vma)
805{
806 struct vme_master_resource *image;
807 phys_addr_t phys_addr;
808 unsigned long vma_size;
809
810 if (resource->type != VME_MASTER) {
811 pr_err("Not a master resource\n");
812 return -EINVAL;
813 }
814
815 image = list_entry(resource->entry, struct vme_master_resource, list);
816 phys_addr = image->bus_resource.start + (vma->vm_pgoff << PAGE_SHIFT);
817 vma_size = vma->vm_end - vma->vm_start;
818
819 if (phys_addr + vma_size > image->bus_resource.end + 1) {
820 pr_err("Map size cannot exceed the window size\n");
821 return -EFAULT;
822 }
823
824 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
825
826 return vm_iomap_memory(vma, phys_addr, vma->vm_end - vma->vm_start);
827}
828EXPORT_SYMBOL(vme_master_mmap);
829
Martyn Welchb5bc9802017-03-04 00:34:29 +0000830/**
831 * vme_master_free - Free VME master window
832 * @resource: Pointer to VME master resource.
833 *
834 * Free the provided master resource so that it may be reallocated.
835 */
Martyn Welcha17a75e2009-07-31 09:28:17 +0100836void vme_master_free(struct vme_resource *resource)
837{
838 struct vme_master_resource *master_image;
839
840 if (resource->type != VME_MASTER) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000841 printk(KERN_ERR "Not a master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100842 return;
843 }
844
845 master_image = list_entry(resource->entry, struct vme_master_resource,
846 list);
847 if (master_image == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000848 printk(KERN_ERR "Can't find master resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100849 return;
850 }
851
852 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000853 spin_lock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100854 if (master_image->locked == 0)
855 printk(KERN_ERR "Image is already free\n");
856
857 master_image->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000858 spin_unlock(&master_image->lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100859
860 /* Free up resource memory */
861 kfree(resource);
862}
863EXPORT_SYMBOL(vme_master_free);
864
Martyn Welchb5bc9802017-03-04 00:34:29 +0000865/**
866 * vme_dma_request - Request a DMA controller.
867 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
868 * @route: Required src/destination combination.
869 *
870 * Request a VME DMA controller with capability to perform transfers bewteen
871 * requested source/destination combination.
872 *
873 * Return: Pointer to VME DMA resource on success, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100874 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000875struct vme_resource *vme_dma_request(struct vme_dev *vdev, u32 route)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100876{
877 struct vme_bridge *bridge;
878 struct list_head *dma_pos = NULL;
879 struct vme_dma_resource *allocated_ctrlr = NULL;
880 struct vme_dma_resource *dma_ctrlr = NULL;
881 struct vme_resource *resource = NULL;
882
883 /* XXX Not checking resource attributes */
884 printk(KERN_ERR "No VME resource Attribute tests done\n");
885
Manohar Vanga8f966dc2011-09-26 11:27:15 +0200886 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100887 if (bridge == NULL) {
888 printk(KERN_ERR "Can't find VME bus\n");
889 goto err_bus;
890 }
891
892 /* Loop through DMA resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000893 list_for_each(dma_pos, &bridge->dma_resources) {
Martyn Welcha17a75e2009-07-31 09:28:17 +0100894 dma_ctrlr = list_entry(dma_pos,
895 struct vme_dma_resource, list);
896
897 if (dma_ctrlr == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000898 printk(KERN_ERR "Registered NULL DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100899 continue;
900 }
901
Martyn Welch4f723df2010-02-18 15:12:58 +0000902 /* Find an unlocked and compatible controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000903 mutex_lock(&dma_ctrlr->mtx);
Martyn Welch4f723df2010-02-18 15:12:58 +0000904 if (((dma_ctrlr->route_attr & route) == route) &&
905 (dma_ctrlr->locked == 0)) {
906
Martyn Welcha17a75e2009-07-31 09:28:17 +0100907 dma_ctrlr->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000908 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100909 allocated_ctrlr = dma_ctrlr;
910 break;
911 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000912 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100913 }
914
915 /* Check to see if we found a resource */
916 if (allocated_ctrlr == NULL)
917 goto err_ctrlr;
918
919 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
920 if (resource == NULL) {
921 printk(KERN_WARNING "Unable to allocate resource structure\n");
922 goto err_alloc;
923 }
924 resource->type = VME_DMA;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000925 resource->entry = &allocated_ctrlr->list;
Martyn Welcha17a75e2009-07-31 09:28:17 +0100926
927 return resource;
928
929err_alloc:
930 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +0000931 mutex_lock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100932 dma_ctrlr->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000933 mutex_unlock(&dma_ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100934err_ctrlr:
935err_bus:
936 return NULL;
937}
Martyn Welch58e50792009-10-29 16:35:20 +0000938EXPORT_SYMBOL(vme_dma_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100939
Martyn Welchb5bc9802017-03-04 00:34:29 +0000940/**
941 * vme_new_dma_list - Create new VME DMA list.
942 * @resource: Pointer to VME DMA resource.
943 *
944 * Create a new VME DMA list. It is the responsibility of the user to free
945 * the list once it is no longer required with vme_dma_list_free().
946 *
947 * Return: Pointer to new VME DMA list, NULL on allocation failure or invalid
948 * VME DMA resource.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100949 */
950struct vme_dma_list *vme_new_dma_list(struct vme_resource *resource)
951{
952 struct vme_dma_resource *ctrlr;
953 struct vme_dma_list *dma_list;
954
955 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +0000956 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100957 return NULL;
958 }
959
960 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
961
Martyn Welchead1f3e2009-12-15 08:43:02 +0000962 dma_list = kmalloc(sizeof(struct vme_dma_list), GFP_KERNEL);
963 if (dma_list == NULL) {
Aaron Sierraf56c3d42016-04-21 11:18:22 -0500964 printk(KERN_ERR "Unable to allocate memory for new DMA list\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100965 return NULL;
966 }
Emilio G. Cota886953e2010-11-12 11:14:07 +0000967 INIT_LIST_HEAD(&dma_list->entries);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100968 dma_list->parent = ctrlr;
Emilio G. Cota886953e2010-11-12 11:14:07 +0000969 mutex_init(&dma_list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +0100970
971 return dma_list;
972}
973EXPORT_SYMBOL(vme_new_dma_list);
974
Martyn Welchb5bc9802017-03-04 00:34:29 +0000975/**
976 * vme_dma_pattern_attribute - Create "Pattern" type VME DMA list attribute.
977 * @pattern: Value to use used as pattern
978 * @type: Type of pattern to be written.
979 *
980 * Create VME DMA list attribute for pattern generation. It is the
981 * responsibility of the user to free used attributes using
982 * vme_dma_free_attribute().
983 *
984 * Return: Pointer to VME DMA attribute, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +0100985 */
Martyn Welch6af04b02011-12-01 17:06:29 +0000986struct vme_dma_attr *vme_dma_pattern_attribute(u32 pattern, u32 type)
Martyn Welcha17a75e2009-07-31 09:28:17 +0100987{
988 struct vme_dma_attr *attributes;
989 struct vme_dma_pattern *pattern_attr;
990
Martyn Welchead1f3e2009-12-15 08:43:02 +0000991 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
992 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700993 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +0100994 goto err_attr;
995 }
996
Martyn Welchead1f3e2009-12-15 08:43:02 +0000997 pattern_attr = kmalloc(sizeof(struct vme_dma_pattern), GFP_KERNEL);
998 if (pattern_attr == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -0700999 printk(KERN_ERR "Unable to allocate memory for pattern attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001000 goto err_pat;
1001 }
1002
1003 attributes->type = VME_DMA_PATTERN;
1004 attributes->private = (void *)pattern_attr;
1005
1006 pattern_attr->pattern = pattern;
1007 pattern_attr->type = type;
1008
1009 return attributes;
1010
Martyn Welcha17a75e2009-07-31 09:28:17 +01001011err_pat:
1012 kfree(attributes);
1013err_attr:
1014 return NULL;
1015}
1016EXPORT_SYMBOL(vme_dma_pattern_attribute);
1017
Martyn Welchb5bc9802017-03-04 00:34:29 +00001018/**
1019 * vme_dma_pci_attribute - Create "PCI" type VME DMA list attribute.
1020 * @address: PCI base address for DMA transfer.
1021 *
1022 * Create VME DMA list attribute pointing to a location on PCI for DMA
1023 * transfers. It is the responsibility of the user to free used attributes
1024 * using vme_dma_free_attribute().
1025 *
1026 * Return: Pointer to VME DMA attribute, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001027 */
1028struct vme_dma_attr *vme_dma_pci_attribute(dma_addr_t address)
1029{
1030 struct vme_dma_attr *attributes;
1031 struct vme_dma_pci *pci_attr;
1032
1033 /* XXX Run some sanity checks here */
1034
Martyn Welchead1f3e2009-12-15 08:43:02 +00001035 attributes = kmalloc(sizeof(struct vme_dma_attr), GFP_KERNEL);
1036 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001037 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001038 goto err_attr;
1039 }
1040
Martyn Welchead1f3e2009-12-15 08:43:02 +00001041 pci_attr = kmalloc(sizeof(struct vme_dma_pci), GFP_KERNEL);
1042 if (pci_attr == NULL) {
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001043 printk(KERN_ERR "Unable to allocate memory for PCI attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001044 goto err_pci;
1045 }
1046
1047
1048
1049 attributes->type = VME_DMA_PCI;
1050 attributes->private = (void *)pci_attr;
1051
1052 pci_attr->address = address;
1053
1054 return attributes;
1055
Martyn Welcha17a75e2009-07-31 09:28:17 +01001056err_pci:
1057 kfree(attributes);
1058err_attr:
1059 return NULL;
1060}
1061EXPORT_SYMBOL(vme_dma_pci_attribute);
1062
Martyn Welchb5bc9802017-03-04 00:34:29 +00001063/**
1064 * vme_dma_vme_attribute - Create "VME" type VME DMA list attribute.
1065 * @address: VME base address for DMA transfer.
1066 * @aspace: VME address space to use for DMA transfer.
1067 * @cycle: VME bus cycle to use for DMA transfer.
1068 * @dwidth: VME data width to use for DMA transfer.
1069 *
1070 * Create VME DMA list attribute pointing to a location on the VME bus for DMA
1071 * transfers. It is the responsibility of the user to free used attributes
1072 * using vme_dma_free_attribute().
1073 *
1074 * Return: Pointer to VME DMA attribute, NULL on failure.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001075 */
1076struct vme_dma_attr *vme_dma_vme_attribute(unsigned long long address,
Martyn Welch6af04b02011-12-01 17:06:29 +00001077 u32 aspace, u32 cycle, u32 dwidth)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001078{
1079 struct vme_dma_attr *attributes;
1080 struct vme_dma_vme *vme_attr;
1081
Martyn Welchead1f3e2009-12-15 08:43:02 +00001082 attributes = kmalloc(
Martyn Welcha17a75e2009-07-31 09:28:17 +01001083 sizeof(struct vme_dma_attr), GFP_KERNEL);
Martyn Welchead1f3e2009-12-15 08:43:02 +00001084 if (attributes == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001085 printk(KERN_ERR "Unable to allocate memory for attributes structure\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001086 goto err_attr;
1087 }
1088
Martyn Welchead1f3e2009-12-15 08:43:02 +00001089 vme_attr = kmalloc(sizeof(struct vme_dma_vme), GFP_KERNEL);
1090 if (vme_attr == NULL) {
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001091 printk(KERN_ERR "Unable to allocate memory for VME attributes\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001092 goto err_vme;
1093 }
1094
1095 attributes->type = VME_DMA_VME;
1096 attributes->private = (void *)vme_attr;
1097
1098 vme_attr->address = address;
1099 vme_attr->aspace = aspace;
1100 vme_attr->cycle = cycle;
1101 vme_attr->dwidth = dwidth;
1102
1103 return attributes;
1104
Martyn Welcha17a75e2009-07-31 09:28:17 +01001105err_vme:
1106 kfree(attributes);
1107err_attr:
1108 return NULL;
1109}
1110EXPORT_SYMBOL(vme_dma_vme_attribute);
1111
Martyn Welchb5bc9802017-03-04 00:34:29 +00001112/**
1113 * vme_dma_free_attribute - Free DMA list attribute.
1114 * @attributes: Pointer to DMA list attribute.
1115 *
1116 * Free VME DMA list attribute. VME DMA list attributes can be safely freed
1117 * once vme_dma_list_add() has returned.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001118 */
1119void vme_dma_free_attribute(struct vme_dma_attr *attributes)
1120{
1121 kfree(attributes->private);
1122 kfree(attributes);
1123}
1124EXPORT_SYMBOL(vme_dma_free_attribute);
1125
Martyn Welchb5bc9802017-03-04 00:34:29 +00001126/**
1127 * vme_dma_list_add - Add enty to a VME DMA list.
1128 * @list: Pointer to VME list.
1129 * @src: Pointer to DMA list attribute to use as source.
1130 * @dest: Pointer to DMA list attribute to use as destination.
1131 * @count: Number of bytes to transfer.
1132 *
1133 * Add an entry to the provided VME DMA list. Entry requires pointers to source
1134 * and destination DMA attributes and a count.
1135 *
1136 * Please note, the attributes supported as source and destinations for
1137 * transfers are hardware dependent.
1138 *
1139 * Return: Zero on success, -EINVAL if operation is not supported on this
1140 * device or if the link list has already been submitted for execution.
1141 * Hardware specific errors also possible.
1142 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001143int vme_dma_list_add(struct vme_dma_list *list, struct vme_dma_attr *src,
1144 struct vme_dma_attr *dest, size_t count)
1145{
1146 struct vme_bridge *bridge = list->parent->parent;
1147 int retval;
1148
1149 if (bridge->dma_list_add == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001150 printk(KERN_WARNING "Link List DMA generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001151 return -EINVAL;
1152 }
1153
Emilio G. Cota886953e2010-11-12 11:14:07 +00001154 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001155 printk(KERN_ERR "Link List already submitted\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001156 return -EINVAL;
1157 }
1158
1159 retval = bridge->dma_list_add(list, src, dest, count);
1160
Emilio G. Cota886953e2010-11-12 11:14:07 +00001161 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001162
1163 return retval;
1164}
1165EXPORT_SYMBOL(vme_dma_list_add);
1166
Martyn Welchb5bc9802017-03-04 00:34:29 +00001167/**
1168 * vme_dma_list_exec - Queue a VME DMA list for execution.
1169 * @list: Pointer to VME list.
1170 *
1171 * Queue the provided VME DMA list for execution. The call will return once the
1172 * list has been executed.
1173 *
1174 * Return: Zero on success, -EINVAL if operation is not supported on this
1175 * device. Hardware specific errors also possible.
1176 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001177int vme_dma_list_exec(struct vme_dma_list *list)
1178{
1179 struct vme_bridge *bridge = list->parent->parent;
1180 int retval;
1181
1182 if (bridge->dma_list_exec == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001183 printk(KERN_ERR "Link List DMA execution not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001184 return -EINVAL;
1185 }
1186
Emilio G. Cota886953e2010-11-12 11:14:07 +00001187 mutex_lock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001188
1189 retval = bridge->dma_list_exec(list);
1190
Emilio G. Cota886953e2010-11-12 11:14:07 +00001191 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001192
1193 return retval;
1194}
1195EXPORT_SYMBOL(vme_dma_list_exec);
1196
Martyn Welchb5bc9802017-03-04 00:34:29 +00001197/**
1198 * vme_dma_list_free - Free a VME DMA list.
1199 * @list: Pointer to VME list.
1200 *
1201 * Free the provided DMA list and all its entries.
1202 *
1203 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1204 * is still in use. Hardware specific errors also possible.
1205 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001206int vme_dma_list_free(struct vme_dma_list *list)
1207{
1208 struct vme_bridge *bridge = list->parent->parent;
1209 int retval;
1210
1211 if (bridge->dma_list_empty == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001212 printk(KERN_WARNING "Emptying of Link Lists not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001213 return -EINVAL;
1214 }
1215
Emilio G. Cota886953e2010-11-12 11:14:07 +00001216 if (!mutex_trylock(&list->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001217 printk(KERN_ERR "Link List in use\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001218 return -EINVAL;
1219 }
1220
1221 /*
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001222 * Empty out all of the entries from the DMA list. We need to go to the
1223 * low level driver as DMA entries are driver specific.
Martyn Welcha17a75e2009-07-31 09:28:17 +01001224 */
1225 retval = bridge->dma_list_empty(list);
1226 if (retval) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001227 printk(KERN_ERR "Unable to empty link-list entries\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +00001228 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001229 return retval;
1230 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001231 mutex_unlock(&list->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001232 kfree(list);
1233
1234 return retval;
1235}
1236EXPORT_SYMBOL(vme_dma_list_free);
1237
Martyn Welchb5bc9802017-03-04 00:34:29 +00001238/**
1239 * vme_dma_free - Free a VME DMA resource.
1240 * @resource: Pointer to VME DMA resource.
1241 *
1242 * Free the provided DMA resource so that it may be reallocated.
1243 *
1244 * Return: Zero on success, -EINVAL on invalid VME resource, -EBUSY if resource
1245 * is still active.
1246 */
Martyn Welcha17a75e2009-07-31 09:28:17 +01001247int vme_dma_free(struct vme_resource *resource)
1248{
1249 struct vme_dma_resource *ctrlr;
1250
1251 if (resource->type != VME_DMA) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001252 printk(KERN_ERR "Not a DMA resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001253 return -EINVAL;
1254 }
1255
1256 ctrlr = list_entry(resource->entry, struct vme_dma_resource, list);
1257
Emilio G. Cota886953e2010-11-12 11:14:07 +00001258 if (!mutex_trylock(&ctrlr->mtx)) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001259 printk(KERN_ERR "Resource busy, can't free\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001260 return -EBUSY;
1261 }
1262
Emilio G. Cota886953e2010-11-12 11:14:07 +00001263 if (!(list_empty(&ctrlr->pending) && list_empty(&ctrlr->running))) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001264 printk(KERN_WARNING "Resource still processing transfers\n");
Emilio G. Cota886953e2010-11-12 11:14:07 +00001265 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001266 return -EBUSY;
1267 }
1268
1269 ctrlr->locked = 0;
1270
Emilio G. Cota886953e2010-11-12 11:14:07 +00001271 mutex_unlock(&ctrlr->mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001272
Martyn Welchfd5c2562013-06-06 12:29:16 +01001273 kfree(resource);
1274
Martyn Welcha17a75e2009-07-31 09:28:17 +01001275 return 0;
1276}
1277EXPORT_SYMBOL(vme_dma_free);
1278
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001279void vme_bus_error_handler(struct vme_bridge *bridge,
Dmitry Kalinkin472f16f2015-09-18 02:01:43 +03001280 unsigned long long address, int am)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001281{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001282 struct list_head *handler_pos = NULL;
1283 struct vme_error_handler *handler;
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001284 int handler_triggered = 0;
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001285 u32 aspace = vme_get_aspace(am);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001286
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001287 list_for_each(handler_pos, &bridge->vme_error_handlers) {
1288 handler = list_entry(handler_pos, struct vme_error_handler,
1289 list);
1290 if ((aspace == handler->aspace) &&
1291 (address >= handler->start) &&
1292 (address < handler->end)) {
1293 if (!handler->num_errors)
1294 handler->first_error = address;
1295 if (handler->num_errors != UINT_MAX)
1296 handler->num_errors++;
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001297 handler_triggered = 1;
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001298 }
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001299 }
Dmitry Kalinkin448535a2015-09-18 02:01:45 +03001300
1301 if (!handler_triggered)
1302 dev_err(bridge->parent,
1303 "Unhandled VME access error at address 0x%llx\n",
1304 address);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001305}
1306EXPORT_SYMBOL(vme_bus_error_handler);
1307
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001308struct vme_error_handler *vme_register_error_handler(
1309 struct vme_bridge *bridge, u32 aspace,
1310 unsigned long long address, size_t len)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001311{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001312 struct vme_error_handler *handler;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001313
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001314 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
1315 if (!handler)
1316 return NULL;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001317
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001318 handler->aspace = aspace;
1319 handler->start = address;
1320 handler->end = address + len;
1321 handler->num_errors = 0;
1322 handler->first_error = 0;
1323 list_add_tail(&handler->list, &bridge->vme_error_handlers);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001324
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001325 return handler;
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001326}
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001327EXPORT_SYMBOL(vme_register_error_handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001328
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001329void vme_unregister_error_handler(struct vme_error_handler *handler)
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001330{
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001331 list_del(&handler->list);
1332 kfree(handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001333}
Dmitry Kalinkin0b049662015-09-18 02:01:44 +03001334EXPORT_SYMBOL(vme_unregister_error_handler);
Dmitry Kalinkine2c63932015-09-18 02:01:42 +03001335
Martyn Welchc813f592009-10-29 16:34:54 +00001336void vme_irq_handler(struct vme_bridge *bridge, int level, int statid)
1337{
1338 void (*call)(int, int, void *);
1339 void *priv_data;
1340
1341 call = bridge->irq[level - 1].callback[statid].func;
1342 priv_data = bridge->irq[level - 1].callback[statid].priv_data;
1343
1344 if (call != NULL)
1345 call(level, statid, priv_data);
1346 else
Aaron Sierraf56c3d42016-04-21 11:18:22 -05001347 printk(KERN_WARNING "Spurious VME interrupt, level:%x, vector:%x\n",
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001348 level, statid);
Martyn Welchc813f592009-10-29 16:34:54 +00001349}
1350EXPORT_SYMBOL(vme_irq_handler);
1351
Martyn Welchb5bc9802017-03-04 00:34:29 +00001352/**
1353 * vme_irq_request - Request a specific VME interrupt.
1354 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1355 * @level: Interrupt priority being requested.
1356 * @statid: Interrupt vector being requested.
1357 * @callback: Pointer to callback function called when VME interrupt/vector
1358 * received.
1359 * @priv_data: Generic pointer that will be passed to the callback function.
1360 *
1361 * Request callback to be attached as a handler for VME interrupts with provided
1362 * level and statid.
1363 *
1364 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1365 * function is not supported, -EBUSY if the level/statid combination is
1366 * already in use. Hardware specific errors also possible.
1367 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001368int vme_irq_request(struct vme_dev *vdev, int level, int statid,
Martyn Welch29848ac2010-02-18 15:13:05 +00001369 void (*callback)(int, int, void *),
Martyn Welcha17a75e2009-07-31 09:28:17 +01001370 void *priv_data)
1371{
1372 struct vme_bridge *bridge;
1373
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001374 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001375 if (bridge == NULL) {
1376 printk(KERN_ERR "Can't find VME bus\n");
1377 return -EINVAL;
1378 }
1379
Martyn Welchead1f3e2009-12-15 08:43:02 +00001380 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001381 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001382 return -EINVAL;
1383 }
1384
Martyn Welchc813f592009-10-29 16:34:54 +00001385 if (bridge->irq_set == NULL) {
1386 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001387 return -EINVAL;
1388 }
1389
Emilio G. Cota886953e2010-11-12 11:14:07 +00001390 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001391
1392 if (bridge->irq[level - 1].callback[statid].func) {
Emilio G. Cota886953e2010-11-12 11:14:07 +00001393 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001394 printk(KERN_WARNING "VME Interrupt already taken\n");
1395 return -EBUSY;
1396 }
1397
1398 bridge->irq[level - 1].count++;
1399 bridge->irq[level - 1].callback[statid].priv_data = priv_data;
1400 bridge->irq[level - 1].callback[statid].func = callback;
1401
1402 /* Enable IRQ level */
Martyn Welch29848ac2010-02-18 15:13:05 +00001403 bridge->irq_set(bridge, level, 1, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001404
Emilio G. Cota886953e2010-11-12 11:14:07 +00001405 mutex_unlock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001406
1407 return 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001408}
Martyn Welchc813f592009-10-29 16:34:54 +00001409EXPORT_SYMBOL(vme_irq_request);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001410
Martyn Welchb5bc9802017-03-04 00:34:29 +00001411/**
1412 * vme_irq_free - Free a VME interrupt.
1413 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1414 * @level: Interrupt priority of interrupt being freed.
1415 * @statid: Interrupt vector of interrupt being freed.
1416 *
1417 * Remove previously attached callback from VME interrupt priority/vector.
1418 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001419void vme_irq_free(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001420{
1421 struct vme_bridge *bridge;
1422
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001423 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001424 if (bridge == NULL) {
1425 printk(KERN_ERR "Can't find VME bus\n");
1426 return;
1427 }
1428
Martyn Welchead1f3e2009-12-15 08:43:02 +00001429 if ((level < 1) || (level > 7)) {
Martyn Welchc813f592009-10-29 16:34:54 +00001430 printk(KERN_ERR "Invalid interrupt level\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001431 return;
1432 }
1433
Martyn Welchc813f592009-10-29 16:34:54 +00001434 if (bridge->irq_set == NULL) {
1435 printk(KERN_ERR "Configuring interrupts not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001436 return;
1437 }
1438
Emilio G. Cota886953e2010-11-12 11:14:07 +00001439 mutex_lock(&bridge->irq_mtx);
Martyn Welchc813f592009-10-29 16:34:54 +00001440
1441 bridge->irq[level - 1].count--;
1442
1443 /* Disable IRQ level if no more interrupts attached at this level*/
1444 if (bridge->irq[level - 1].count == 0)
Martyn Welch29848ac2010-02-18 15:13:05 +00001445 bridge->irq_set(bridge, level, 0, 1);
Martyn Welchc813f592009-10-29 16:34:54 +00001446
1447 bridge->irq[level - 1].callback[statid].func = NULL;
1448 bridge->irq[level - 1].callback[statid].priv_data = NULL;
1449
Emilio G. Cota886953e2010-11-12 11:14:07 +00001450 mutex_unlock(&bridge->irq_mtx);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001451}
Martyn Welchc813f592009-10-29 16:34:54 +00001452EXPORT_SYMBOL(vme_irq_free);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001453
Martyn Welchb5bc9802017-03-04 00:34:29 +00001454/**
1455 * vme_irq_generate - Generate VME interrupt.
1456 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1457 * @level: Interrupt priority at which to assert the interrupt.
1458 * @statid: Interrupt vector to associate with the interrupt.
1459 *
1460 * Generate a VME interrupt of the provided level and with the provided
1461 * statid.
1462 *
1463 * Return: Zero on success, -EINVAL on invalid vme device, level or if the
1464 * function is not supported. Hardware specific errors also possible.
1465 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001466int vme_irq_generate(struct vme_dev *vdev, int level, int statid)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001467{
1468 struct vme_bridge *bridge;
1469
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001470 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001471 if (bridge == NULL) {
1472 printk(KERN_ERR "Can't find VME bus\n");
1473 return -EINVAL;
1474 }
1475
Martyn Welchead1f3e2009-12-15 08:43:02 +00001476 if ((level < 1) || (level > 7)) {
Martyn Welcha17a75e2009-07-31 09:28:17 +01001477 printk(KERN_WARNING "Invalid interrupt level\n");
1478 return -EINVAL;
1479 }
1480
Martyn Welchc813f592009-10-29 16:34:54 +00001481 if (bridge->irq_generate == NULL) {
Martyn Welchead1f3e2009-12-15 08:43:02 +00001482 printk(KERN_WARNING "Interrupt generation not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001483 return -EINVAL;
1484 }
1485
Martyn Welch29848ac2010-02-18 15:13:05 +00001486 return bridge->irq_generate(bridge, level, statid);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001487}
Martyn Welchc813f592009-10-29 16:34:54 +00001488EXPORT_SYMBOL(vme_irq_generate);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001489
Martyn Welchb5bc9802017-03-04 00:34:29 +00001490/**
1491 * vme_lm_request - Request a VME location monitor
1492 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1493 *
1494 * Allocate a location monitor resource to the driver. A location monitor
1495 * allows the driver to monitor accesses to a contiguous number of
1496 * addresses on the VME bus.
1497 *
1498 * Return: Pointer to a VME resource on success or NULL on failure.
Martyn Welch42fb5032009-08-11 17:44:56 +01001499 */
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001500struct vme_resource *vme_lm_request(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001501{
1502 struct vme_bridge *bridge;
Martyn Welch42fb5032009-08-11 17:44:56 +01001503 struct list_head *lm_pos = NULL;
1504 struct vme_lm_resource *allocated_lm = NULL;
1505 struct vme_lm_resource *lm = NULL;
1506 struct vme_resource *resource = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001507
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001508 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001509 if (bridge == NULL) {
1510 printk(KERN_ERR "Can't find VME bus\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001511 goto err_bus;
1512 }
1513
Martyn Welchb5bc9802017-03-04 00:34:29 +00001514 /* Loop through LM resources */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001515 list_for_each(lm_pos, &bridge->lm_resources) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001516 lm = list_entry(lm_pos,
1517 struct vme_lm_resource, list);
1518
1519 if (lm == NULL) {
Greg Kroah-Hartman25958ce2012-04-25 11:25:46 -07001520 printk(KERN_ERR "Registered NULL Location Monitor resource\n");
Martyn Welch42fb5032009-08-11 17:44:56 +01001521 continue;
1522 }
1523
1524 /* Find an unlocked controller */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001525 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001526 if (lm->locked == 0) {
1527 lm->locked = 1;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001528 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001529 allocated_lm = lm;
1530 break;
1531 }
Emilio G. Cota886953e2010-11-12 11:14:07 +00001532 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001533 }
1534
1535 /* Check to see if we found a resource */
1536 if (allocated_lm == NULL)
1537 goto err_lm;
1538
1539 resource = kmalloc(sizeof(struct vme_resource), GFP_KERNEL);
1540 if (resource == NULL) {
1541 printk(KERN_ERR "Unable to allocate resource structure\n");
1542 goto err_alloc;
1543 }
1544 resource->type = VME_LM;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001545 resource->entry = &allocated_lm->list;
Martyn Welch42fb5032009-08-11 17:44:56 +01001546
1547 return resource;
1548
1549err_alloc:
1550 /* Unlock image */
Emilio G. Cota886953e2010-11-12 11:14:07 +00001551 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001552 lm->locked = 0;
Emilio G. Cota886953e2010-11-12 11:14:07 +00001553 mutex_unlock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001554err_lm:
1555err_bus:
1556 return NULL;
1557}
1558EXPORT_SYMBOL(vme_lm_request);
1559
Martyn Welchb5bc9802017-03-04 00:34:29 +00001560/**
1561 * vme_lm_count - Determine number of VME Addresses monitored
1562 * @resource: Pointer to VME location monitor resource.
1563 *
1564 * The number of contiguous addresses monitored is hardware dependent.
1565 * Return the number of contiguous addresses monitored by the
1566 * location monitor.
1567 *
1568 * Return: Count of addresses monitored or -EINVAL when provided with an
1569 * invalid location monitor resource.
1570 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001571int vme_lm_count(struct vme_resource *resource)
1572{
1573 struct vme_lm_resource *lm;
1574
1575 if (resource->type != VME_LM) {
1576 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001577 return -EINVAL;
1578 }
1579
Martyn Welch42fb5032009-08-11 17:44:56 +01001580 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1581
1582 return lm->monitors;
1583}
1584EXPORT_SYMBOL(vme_lm_count);
1585
Martyn Welchb5bc9802017-03-04 00:34:29 +00001586/**
1587 * vme_lm_set - Configure location monitor
1588 * @resource: Pointer to VME location monitor resource.
1589 * @lm_base: Base address to monitor.
1590 * @aspace: VME address space to monitor.
1591 * @cycle: VME bus cycle type to monitor.
1592 *
1593 * Set the base address, address space and cycle type of accesses to be
1594 * monitored by the location monitor.
1595 *
1596 * Return: Zero on success, -EINVAL when provided with an invalid location
1597 * monitor resource or function is not supported. Hardware specific
1598 * errors may also be returned.
1599 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001600int vme_lm_set(struct vme_resource *resource, unsigned long long lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001601 u32 aspace, u32 cycle)
Martyn Welch42fb5032009-08-11 17:44:56 +01001602{
1603 struct vme_bridge *bridge = find_bridge(resource);
1604 struct vme_lm_resource *lm;
1605
1606 if (resource->type != VME_LM) {
1607 printk(KERN_ERR "Not a Location Monitor resource\n");
1608 return -EINVAL;
1609 }
1610
1611 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1612
Martyn Welcha17a75e2009-07-31 09:28:17 +01001613 if (bridge->lm_set == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001614 printk(KERN_ERR "vme_lm_set not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001615 return -EINVAL;
1616 }
1617
Martyn Welch8be92262009-10-29 16:35:08 +00001618 return bridge->lm_set(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001619}
1620EXPORT_SYMBOL(vme_lm_set);
1621
Martyn Welchb5bc9802017-03-04 00:34:29 +00001622/**
1623 * vme_lm_get - Retrieve location monitor settings
1624 * @resource: Pointer to VME location monitor resource.
1625 * @lm_base: Pointer used to output the base address monitored.
1626 * @aspace: Pointer used to output the address space monitored.
1627 * @cycle: Pointer used to output the VME bus cycle type monitored.
1628 *
1629 * Retrieve the base address, address space and cycle type of accesses to
1630 * be monitored by the location monitor.
1631 *
1632 * Return: Zero on success, -EINVAL when provided with an invalid location
1633 * monitor resource or function is not supported. Hardware specific
1634 * errors may also be returned.
1635 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001636int vme_lm_get(struct vme_resource *resource, unsigned long long *lm_base,
Martyn Welch6af04b02011-12-01 17:06:29 +00001637 u32 *aspace, u32 *cycle)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001638{
Martyn Welch42fb5032009-08-11 17:44:56 +01001639 struct vme_bridge *bridge = find_bridge(resource);
1640 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001641
Martyn Welch42fb5032009-08-11 17:44:56 +01001642 if (resource->type != VME_LM) {
1643 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001644 return -EINVAL;
1645 }
1646
Martyn Welch42fb5032009-08-11 17:44:56 +01001647 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1648
Martyn Welcha17a75e2009-07-31 09:28:17 +01001649 if (bridge->lm_get == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001650 printk(KERN_ERR "vme_lm_get not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001651 return -EINVAL;
1652 }
1653
Martyn Welch42fb5032009-08-11 17:44:56 +01001654 return bridge->lm_get(lm, lm_base, aspace, cycle);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001655}
1656EXPORT_SYMBOL(vme_lm_get);
1657
Martyn Welchb5bc9802017-03-04 00:34:29 +00001658/**
1659 * vme_lm_attach - Provide callback for location monitor address
1660 * @resource: Pointer to VME location monitor resource.
1661 * @monitor: Offset to which callback should be attached.
1662 * @callback: Pointer to callback function called when triggered.
1663 * @data: Generic pointer that will be passed to the callback function.
1664 *
1665 * Attach a callback to the specificed offset into the location monitors
1666 * monitored addresses. A generic pointer is provided to allow data to be
1667 * passed to the callback when called.
1668 *
1669 * Return: Zero on success, -EINVAL when provided with an invalid location
1670 * monitor resource or function is not supported. Hardware specific
1671 * errors may also be returned.
1672 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001673int vme_lm_attach(struct vme_resource *resource, int monitor,
Aaron Sierrafa54b322016-04-29 16:41:02 -05001674 void (*callback)(void *), void *data)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001675{
Martyn Welch42fb5032009-08-11 17:44:56 +01001676 struct vme_bridge *bridge = find_bridge(resource);
1677 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001678
Martyn Welch42fb5032009-08-11 17:44:56 +01001679 if (resource->type != VME_LM) {
1680 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001681 return -EINVAL;
1682 }
1683
Martyn Welch42fb5032009-08-11 17:44:56 +01001684 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1685
Martyn Welcha17a75e2009-07-31 09:28:17 +01001686 if (bridge->lm_attach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001687 printk(KERN_ERR "vme_lm_attach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001688 return -EINVAL;
1689 }
1690
Aaron Sierrafa54b322016-04-29 16:41:02 -05001691 return bridge->lm_attach(lm, monitor, callback, data);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001692}
1693EXPORT_SYMBOL(vme_lm_attach);
1694
Martyn Welchb5bc9802017-03-04 00:34:29 +00001695/**
1696 * vme_lm_detach - Remove callback for location monitor address
1697 * @resource: Pointer to VME location monitor resource.
1698 * @monitor: Offset to which callback should be removed.
1699 *
1700 * Remove the callback associated with the specificed offset into the
1701 * location monitors monitored addresses.
1702 *
1703 * Return: Zero on success, -EINVAL when provided with an invalid location
1704 * monitor resource or function is not supported. Hardware specific
1705 * errors may also be returned.
1706 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001707int vme_lm_detach(struct vme_resource *resource, int monitor)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001708{
Martyn Welch42fb5032009-08-11 17:44:56 +01001709 struct vme_bridge *bridge = find_bridge(resource);
1710 struct vme_lm_resource *lm;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001711
Martyn Welch42fb5032009-08-11 17:44:56 +01001712 if (resource->type != VME_LM) {
1713 printk(KERN_ERR "Not a Location Monitor resource\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001714 return -EINVAL;
1715 }
1716
Martyn Welch42fb5032009-08-11 17:44:56 +01001717 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1718
Martyn Welcha17a75e2009-07-31 09:28:17 +01001719 if (bridge->lm_detach == NULL) {
Martyn Welch42fb5032009-08-11 17:44:56 +01001720 printk(KERN_ERR "vme_lm_detach not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001721 return -EINVAL;
1722 }
1723
Martyn Welch42fb5032009-08-11 17:44:56 +01001724 return bridge->lm_detach(lm, monitor);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001725}
1726EXPORT_SYMBOL(vme_lm_detach);
1727
Martyn Welchb5bc9802017-03-04 00:34:29 +00001728/**
1729 * vme_lm_free - Free allocated VME location monitor
1730 * @resource: Pointer to VME location monitor resource.
1731 *
1732 * Free allocation of a VME location monitor.
1733 *
1734 * WARNING: This function currently expects that any callbacks that have
1735 * been attached to the location monitor have been removed.
1736 *
1737 * Return: Zero on success, -EINVAL when provided with an invalid location
1738 * monitor resource.
1739 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001740void vme_lm_free(struct vme_resource *resource)
1741{
1742 struct vme_lm_resource *lm;
1743
1744 if (resource->type != VME_LM) {
1745 printk(KERN_ERR "Not a Location Monitor resource\n");
1746 return;
1747 }
1748
1749 lm = list_entry(resource->entry, struct vme_lm_resource, list);
1750
Emilio G. Cota886953e2010-11-12 11:14:07 +00001751 mutex_lock(&lm->mtx);
Martyn Welch42fb5032009-08-11 17:44:56 +01001752
Martyn Welch8be92262009-10-29 16:35:08 +00001753 /* XXX
1754 * Check to see that there aren't any callbacks still attached, if
1755 * there are we should probably be detaching them!
1756 */
Martyn Welch42fb5032009-08-11 17:44:56 +01001757
1758 lm->locked = 0;
1759
Emilio G. Cota886953e2010-11-12 11:14:07 +00001760 mutex_unlock(&lm->mtx);
Martyn Welch8be92262009-10-29 16:35:08 +00001761
1762 kfree(resource);
Martyn Welch42fb5032009-08-11 17:44:56 +01001763}
1764EXPORT_SYMBOL(vme_lm_free);
1765
Martyn Welchb5bc9802017-03-04 00:34:29 +00001766/**
1767 * vme_slot_num - Retrieve slot ID
1768 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1769 *
1770 * Retrieve the slot ID associated with the provided VME device.
1771 *
1772 * Return: The slot ID on success, -EINVAL if VME bridge cannot be determined
1773 * or the function is not supported. Hardware specific errors may also
1774 * be returned.
1775 */
Martyn Welchd7729f02013-11-08 11:58:35 +00001776int vme_slot_num(struct vme_dev *vdev)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001777{
1778 struct vme_bridge *bridge;
1779
Manohar Vanga8f966dc2011-09-26 11:27:15 +02001780 bridge = vdev->bridge;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001781 if (bridge == NULL) {
1782 printk(KERN_ERR "Can't find VME bus\n");
1783 return -EINVAL;
1784 }
1785
1786 if (bridge->slot_get == NULL) {
Martyn Welchd7729f02013-11-08 11:58:35 +00001787 printk(KERN_WARNING "vme_slot_num not supported\n");
Martyn Welcha17a75e2009-07-31 09:28:17 +01001788 return -EINVAL;
1789 }
1790
Martyn Welch29848ac2010-02-18 15:13:05 +00001791 return bridge->slot_get(bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001792}
Martyn Welchd7729f02013-11-08 11:58:35 +00001793EXPORT_SYMBOL(vme_slot_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001794
Martyn Welchb5bc9802017-03-04 00:34:29 +00001795/**
1796 * vme_bus_num - Retrieve bus number
1797 * @vdev: Pointer to VME device struct vme_dev assigned to driver instance.
1798 *
1799 * Retrieve the bus enumeration associated with the provided VME device.
1800 *
1801 * Return: The bus number on success, -EINVAL if VME bridge cannot be
1802 * determined.
1803 */
Martyn Welch978f47d2013-11-08 11:58:34 +00001804int vme_bus_num(struct vme_dev *vdev)
1805{
1806 struct vme_bridge *bridge;
1807
1808 bridge = vdev->bridge;
1809 if (bridge == NULL) {
1810 pr_err("Can't find VME bus\n");
1811 return -EINVAL;
1812 }
1813
1814 return bridge->num;
1815}
1816EXPORT_SYMBOL(vme_bus_num);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001817
1818/* - Bridge Registration --------------------------------------------------- */
1819
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001820static void vme_dev_release(struct device *dev)
1821{
1822 kfree(dev_to_vme_dev(dev));
1823}
1824
Aaron Sierra326071b2016-04-24 15:11:38 -05001825/* Common bridge initialization */
1826struct vme_bridge *vme_init_bridge(struct vme_bridge *bridge)
1827{
1828 INIT_LIST_HEAD(&bridge->vme_error_handlers);
1829 INIT_LIST_HEAD(&bridge->master_resources);
1830 INIT_LIST_HEAD(&bridge->slave_resources);
1831 INIT_LIST_HEAD(&bridge->dma_resources);
1832 INIT_LIST_HEAD(&bridge->lm_resources);
1833 mutex_init(&bridge->irq_mtx);
1834
1835 return bridge;
1836}
1837EXPORT_SYMBOL(vme_init_bridge);
1838
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001839int vme_register_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001840{
1841 int i;
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001842 int ret = -1;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001843
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001844 mutex_lock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001845 for (i = 0; i < sizeof(vme_bus_numbers) * 8; i++) {
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001846 if ((vme_bus_numbers & (1 << i)) == 0) {
1847 vme_bus_numbers |= (1 << i);
1848 bridge->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001849 INIT_LIST_HEAD(&bridge->devices);
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001850 list_add_tail(&bridge->bus_list, &vme_bus_list);
1851 ret = 0;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001852 break;
1853 }
1854 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001855 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001856
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001857 return ret;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001858}
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001859EXPORT_SYMBOL(vme_register_bridge);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001860
Manohar Vanga5b93c2a2011-11-04 11:12:30 +01001861void vme_unregister_bridge(struct vme_bridge *bridge)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001862{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001863 struct vme_dev *vdev;
1864 struct vme_dev *tmp;
1865
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001866 mutex_lock(&vme_buses_lock);
1867 vme_bus_numbers &= ~(1 << bridge->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001868 list_for_each_entry_safe(vdev, tmp, &bridge->devices, bridge_list) {
1869 list_del(&vdev->drv_list);
1870 list_del(&vdev->bridge_list);
1871 device_unregister(&vdev->dev);
1872 }
Manohar Vanga733e3ef2011-08-12 12:30:48 +02001873 list_del(&bridge->bus_list);
1874 mutex_unlock(&vme_buses_lock);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001875}
Martyn Welcha17a75e2009-07-31 09:28:17 +01001876EXPORT_SYMBOL(vme_unregister_bridge);
1877
Martyn Welcha17a75e2009-07-31 09:28:17 +01001878/* - Driver Registration --------------------------------------------------- */
1879
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001880static int __vme_register_driver_bus(struct vme_driver *drv,
1881 struct vme_bridge *bridge, unsigned int ndevs)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001882{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001883 int err;
1884 unsigned int i;
1885 struct vme_dev *vdev;
1886 struct vme_dev *tmp;
1887
1888 for (i = 0; i < ndevs; i++) {
1889 vdev = kzalloc(sizeof(struct vme_dev), GFP_KERNEL);
1890 if (!vdev) {
1891 err = -ENOMEM;
1892 goto err_devalloc;
1893 }
Manohar Vangaa916a392011-09-26 11:27:17 +02001894 vdev->num = i;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001895 vdev->bridge = bridge;
1896 vdev->dev.platform_data = drv;
1897 vdev->dev.release = vme_dev_release;
1898 vdev->dev.parent = bridge->parent;
1899 vdev->dev.bus = &vme_bus_type;
Manohar Vangaa916a392011-09-26 11:27:17 +02001900 dev_set_name(&vdev->dev, "%s.%u-%u", drv->name, bridge->num,
1901 vdev->num);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001902
1903 err = device_register(&vdev->dev);
1904 if (err)
1905 goto err_reg;
1906
1907 if (vdev->dev.platform_data) {
1908 list_add_tail(&vdev->drv_list, &drv->devices);
1909 list_add_tail(&vdev->bridge_list, &bridge->devices);
1910 } else
1911 device_unregister(&vdev->dev);
1912 }
1913 return 0;
1914
1915err_reg:
Emilio G. Cotadef18202013-02-13 13:47:54 -05001916 put_device(&vdev->dev);
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001917 kfree(vdev);
1918err_devalloc:
1919 list_for_each_entry_safe(vdev, tmp, &drv->devices, drv_list) {
1920 list_del(&vdev->drv_list);
1921 list_del(&vdev->bridge_list);
1922 device_unregister(&vdev->dev);
1923 }
1924 return err;
1925}
1926
1927static int __vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1928{
1929 struct vme_bridge *bridge;
1930 int err = 0;
1931
1932 mutex_lock(&vme_buses_lock);
1933 list_for_each_entry(bridge, &vme_bus_list, bus_list) {
1934 /*
1935 * This cannot cause trouble as we already have vme_buses_lock
1936 * and if the bridge is removed, it will have to go through
1937 * vme_unregister_bridge() to do it (which calls remove() on
1938 * the bridge which in turn tries to acquire vme_buses_lock and
Manohar Vangac26f6112011-11-04 11:12:29 +01001939 * will have to wait).
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001940 */
1941 err = __vme_register_driver_bus(drv, bridge, ndevs);
1942 if (err)
1943 break;
1944 }
1945 mutex_unlock(&vme_buses_lock);
1946 return err;
1947}
1948
Martyn Welchb5bc9802017-03-04 00:34:29 +00001949/**
1950 * vme_register_driver - Register a VME driver
1951 * @drv: Pointer to VME driver structure to register.
1952 * @ndevs: Maximum number of devices to allow to be enumerated.
1953 *
1954 * Register a VME device driver with the VME subsystem.
1955 *
1956 * Return: Zero on success, error value on registration failure.
1957 */
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001958int vme_register_driver(struct vme_driver *drv, unsigned int ndevs)
1959{
1960 int err;
1961
Martyn Welcha17a75e2009-07-31 09:28:17 +01001962 drv->driver.name = drv->name;
1963 drv->driver.bus = &vme_bus_type;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001964 INIT_LIST_HEAD(&drv->devices);
Martyn Welcha17a75e2009-07-31 09:28:17 +01001965
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001966 err = driver_register(&drv->driver);
1967 if (err)
1968 return err;
1969
1970 err = __vme_register_driver(drv, ndevs);
1971 if (err)
1972 driver_unregister(&drv->driver);
1973
1974 return err;
Martyn Welcha17a75e2009-07-31 09:28:17 +01001975}
1976EXPORT_SYMBOL(vme_register_driver);
1977
Martyn Welchb5bc9802017-03-04 00:34:29 +00001978/**
1979 * vme_unregister_driver - Unregister a VME driver
1980 * @drv: Pointer to VME driver structure to unregister.
1981 *
1982 * Unregister a VME device driver from the VME subsystem.
1983 */
Martyn Welchead1f3e2009-12-15 08:43:02 +00001984void vme_unregister_driver(struct vme_driver *drv)
Martyn Welcha17a75e2009-07-31 09:28:17 +01001985{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02001986 struct vme_dev *dev, *dev_tmp;
1987
1988 mutex_lock(&vme_buses_lock);
1989 list_for_each_entry_safe(dev, dev_tmp, &drv->devices, drv_list) {
1990 list_del(&dev->drv_list);
1991 list_del(&dev->bridge_list);
1992 device_unregister(&dev->dev);
1993 }
1994 mutex_unlock(&vme_buses_lock);
1995
Martyn Welcha17a75e2009-07-31 09:28:17 +01001996 driver_unregister(&drv->driver);
1997}
1998EXPORT_SYMBOL(vme_unregister_driver);
1999
2000/* - Bus Registration ------------------------------------------------------ */
2001
Martyn Welcha17a75e2009-07-31 09:28:17 +01002002static int vme_bus_match(struct device *dev, struct device_driver *drv)
2003{
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002004 struct vme_driver *vme_drv;
Martyn Welcha17a75e2009-07-31 09:28:17 +01002005
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002006 vme_drv = container_of(drv, struct vme_driver, driver);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002007
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002008 if (dev->platform_data == vme_drv) {
2009 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002010
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002011 if (vme_drv->match && vme_drv->match(vdev))
2012 return 1;
2013
2014 dev->platform_data = NULL;
Martyn Welcha17a75e2009-07-31 09:28:17 +01002015 }
Martyn Welcha17a75e2009-07-31 09:28:17 +01002016 return 0;
2017}
2018
2019static int vme_bus_probe(struct device *dev)
2020{
Martyn Welcha17a75e2009-07-31 09:28:17 +01002021 int retval = -ENODEV;
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002022 struct vme_driver *driver;
2023 struct vme_dev *vdev = dev_to_vme_dev(dev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002024
Manohar Vanga5d6abf32011-09-26 11:27:16 +02002025 driver = dev->platform_data;
Martyn Welcha17a75e2009-07-31 09:28:17 +01002026
Martyn Welchead1f3e2009-12-15 08:43:02 +00002027 if (driver->probe != NULL)
Manohar Vanga8f966dc2011-09-26 11:27:15 +02002028 retval = driver->probe(vdev);
Martyn Welcha17a75e2009-07-31 09:28:17 +01002029
2030 return retval;
2031}
2032
Stefano Babic97974842017-01-20 10:38:20 -05002033static int vme_bus_remove(struct device *dev)
2034{
2035 int retval = -ENODEV;
2036 struct vme_driver *driver;
2037 struct vme_dev *vdev = dev_to_vme_dev(dev);
2038
2039 driver = dev->platform_data;
2040
2041 if (driver->remove != NULL)
2042 retval = driver->remove(vdev);
2043
2044 return retval;
2045}
2046
Martyn Welcha17a75e2009-07-31 09:28:17 +01002047struct bus_type vme_bus_type = {
2048 .name = "vme",
2049 .match = vme_bus_match,
2050 .probe = vme_bus_probe,
Stefano Babic97974842017-01-20 10:38:20 -05002051 .remove = vme_bus_remove,
Martyn Welcha17a75e2009-07-31 09:28:17 +01002052};
2053EXPORT_SYMBOL(vme_bus_type);
2054
Martyn Welchead1f3e2009-12-15 08:43:02 +00002055static int __init vme_init(void)
Martyn Welcha17a75e2009-07-31 09:28:17 +01002056{
2057 return bus_register(&vme_bus_type);
2058}
Aaron Sierrac326cc02013-12-09 09:54:42 -06002059subsys_initcall(vme_init);