fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / drivers / rapidio / rio.c
blobf644807da2f911b92503abcb18c91a6868fd5a00
1 /*
2 * RapidIO interconnect services
3 * (RapidIO Interconnect Specification, http://www.rapidio.org)
5 * Copyright 2005 MontaVista Software, Inc.
6 * Matt Porter <mporter@kernel.crashing.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/types.h>
15 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/rio.h>
20 #include <linux/rio_drv.h>
21 #include <linux/rio_ids.h>
22 #include <linux/rio_regs.h>
23 #include <linux/module.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
27 #include "rio.h"
29 static LIST_HEAD(rio_mports);
31 /**
32 * rio_local_get_device_id - Get the base/extended device id for a port
33 * @port: RIO master port from which to get the deviceid
35 * Reads the base/extended device id from the local device
36 * implementing the master port. Returns the 8/16-bit device
37 * id.
39 u16 rio_local_get_device_id(struct rio_mport *port)
41 u32 result;
43 rio_local_read_config_32(port, RIO_DID_CSR, &result);
45 return (RIO_GET_DID(result));
48 /**
49 * rio_request_inb_mbox - request inbound mailbox service
50 * @mport: RIO master port from which to allocate the mailbox resource
51 * @dev_id: Device specific pointer to pass on event
52 * @mbox: Mailbox number to claim
53 * @entries: Number of entries in inbound mailbox queue
54 * @minb: Callback to execute when inbound message is received
56 * Requests ownership of an inbound mailbox resource and binds
57 * a callback function to the resource. Returns %0 on success.
59 int rio_request_inb_mbox(struct rio_mport *mport,
60 void *dev_id,
61 int mbox,
62 int entries,
63 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
64 int slot))
66 int rc = 0;
68 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
70 if (res) {
71 rio_init_mbox_res(res, mbox, mbox);
73 /* Make sure this mailbox isn't in use */
74 if ((rc =
75 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
76 res)) < 0) {
77 kfree(res);
78 goto out;
81 mport->inb_msg[mbox].res = res;
83 /* Hook the inbound message callback */
84 mport->inb_msg[mbox].mcback = minb;
86 rc = rio_open_inb_mbox(mport, dev_id, mbox, entries);
87 } else
88 rc = -ENOMEM;
90 out:
91 return rc;
94 /**
95 * rio_release_inb_mbox - release inbound mailbox message service
96 * @mport: RIO master port from which to release the mailbox resource
97 * @mbox: Mailbox number to release
99 * Releases ownership of an inbound mailbox resource. Returns 0
100 * if the request has been satisfied.
102 int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
104 rio_close_inb_mbox(mport, mbox);
106 /* Release the mailbox resource */
107 return release_resource(mport->inb_msg[mbox].res);
111 * rio_request_outb_mbox - request outbound mailbox service
112 * @mport: RIO master port from which to allocate the mailbox resource
113 * @dev_id: Device specific pointer to pass on event
114 * @mbox: Mailbox number to claim
115 * @entries: Number of entries in outbound mailbox queue
116 * @moutb: Callback to execute when outbound message is sent
118 * Requests ownership of an outbound mailbox resource and binds
119 * a callback function to the resource. Returns 0 on success.
121 int rio_request_outb_mbox(struct rio_mport *mport,
122 void *dev_id,
123 int mbox,
124 int entries,
125 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
127 int rc = 0;
129 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
131 if (res) {
132 rio_init_mbox_res(res, mbox, mbox);
134 /* Make sure this outbound mailbox isn't in use */
135 if ((rc =
136 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
137 res)) < 0) {
138 kfree(res);
139 goto out;
142 mport->outb_msg[mbox].res = res;
144 /* Hook the inbound message callback */
145 mport->outb_msg[mbox].mcback = moutb;
147 rc = rio_open_outb_mbox(mport, dev_id, mbox, entries);
148 } else
149 rc = -ENOMEM;
151 out:
152 return rc;
156 * rio_release_outb_mbox - release outbound mailbox message service
157 * @mport: RIO master port from which to release the mailbox resource
158 * @mbox: Mailbox number to release
160 * Releases ownership of an inbound mailbox resource. Returns 0
161 * if the request has been satisfied.
163 int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
165 rio_close_outb_mbox(mport, mbox);
167 /* Release the mailbox resource */
168 return release_resource(mport->outb_msg[mbox].res);
172 * rio_setup_inb_dbell - bind inbound doorbell callback
173 * @mport: RIO master port to bind the doorbell callback
174 * @dev_id: Device specific pointer to pass on event
175 * @res: Doorbell message resource
176 * @dinb: Callback to execute when doorbell is received
178 * Adds a doorbell resource/callback pair into a port's
179 * doorbell event list. Returns 0 if the request has been
180 * satisfied.
182 static int
183 rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
184 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
185 u16 info))
187 int rc = 0;
188 struct rio_dbell *dbell;
190 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
191 rc = -ENOMEM;
192 goto out;
195 dbell->res = res;
196 dbell->dinb = dinb;
197 dbell->dev_id = dev_id;
199 list_add_tail(&dbell->node, &mport->dbells);
201 out:
202 return rc;
206 * rio_request_inb_dbell - request inbound doorbell message service
207 * @mport: RIO master port from which to allocate the doorbell resource
208 * @dev_id: Device specific pointer to pass on event
209 * @start: Doorbell info range start
210 * @end: Doorbell info range end
211 * @dinb: Callback to execute when doorbell is received
213 * Requests ownership of an inbound doorbell resource and binds
214 * a callback function to the resource. Returns 0 if the request
215 * has been satisfied.
217 int rio_request_inb_dbell(struct rio_mport *mport,
218 void *dev_id,
219 u16 start,
220 u16 end,
221 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
222 u16 dst, u16 info))
224 int rc = 0;
226 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
228 if (res) {
229 rio_init_dbell_res(res, start, end);
231 /* Make sure these doorbells aren't in use */
232 if ((rc =
233 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
234 res)) < 0) {
235 kfree(res);
236 goto out;
239 /* Hook the doorbell callback */
240 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
241 } else
242 rc = -ENOMEM;
244 out:
245 return rc;
249 * rio_release_inb_dbell - release inbound doorbell message service
250 * @mport: RIO master port from which to release the doorbell resource
251 * @start: Doorbell info range start
252 * @end: Doorbell info range end
254 * Releases ownership of an inbound doorbell resource and removes
255 * callback from the doorbell event list. Returns 0 if the request
256 * has been satisfied.
258 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
260 int rc = 0, found = 0;
261 struct rio_dbell *dbell;
263 list_for_each_entry(dbell, &mport->dbells, node) {
264 if ((dbell->res->start == start) && (dbell->res->end == end)) {
265 found = 1;
266 break;
270 /* If we can't find an exact match, fail */
271 if (!found) {
272 rc = -EINVAL;
273 goto out;
276 /* Delete from list */
277 list_del(&dbell->node);
279 /* Release the doorbell resource */
280 rc = release_resource(dbell->res);
282 /* Free the doorbell event */
283 kfree(dbell);
285 out:
286 return rc;
290 * rio_request_outb_dbell - request outbound doorbell message range
291 * @rdev: RIO device from which to allocate the doorbell resource
292 * @start: Doorbell message range start
293 * @end: Doorbell message range end
295 * Requests ownership of a doorbell message range. Returns a resource
296 * if the request has been satisfied or %NULL on failure.
298 struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
299 u16 end)
301 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
303 if (res) {
304 rio_init_dbell_res(res, start, end);
306 /* Make sure these doorbells aren't in use */
307 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
308 < 0) {
309 kfree(res);
310 res = NULL;
314 return res;
318 * rio_release_outb_dbell - release outbound doorbell message range
319 * @rdev: RIO device from which to release the doorbell resource
320 * @res: Doorbell resource to be freed
322 * Releases ownership of a doorbell message range. Returns 0 if the
323 * request has been satisfied.
325 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
327 int rc = release_resource(res);
329 kfree(res);
331 return rc;
335 * rio_mport_get_feature - query for devices' extended features
336 * @port: Master port to issue transaction
337 * @local: Indicate a local master port or remote device access
338 * @destid: Destination ID of the device
339 * @hopcount: Number of switch hops to the device
340 * @ftr: Extended feature code
342 * Tell if a device supports a given RapidIO capability.
343 * Returns the offset of the requested extended feature
344 * block within the device's RIO configuration space or
345 * 0 in case the device does not support it. Possible
346 * values for @ftr:
348 * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
350 * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
352 * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
354 * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
356 * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
358 * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
361 rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
362 u8 hopcount, int ftr)
364 u32 asm_info, ext_ftr_ptr, ftr_header;
366 if (local)
367 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
368 else
369 rio_mport_read_config_32(port, destid, hopcount,
370 RIO_ASM_INFO_CAR, &asm_info);
372 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
374 while (ext_ftr_ptr) {
375 if (local)
376 rio_local_read_config_32(port, ext_ftr_ptr,
377 &ftr_header);
378 else
379 rio_mport_read_config_32(port, destid, hopcount,
380 ext_ftr_ptr, &ftr_header);
381 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
382 return ext_ftr_ptr;
383 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
384 break;
387 return 0;
391 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
392 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
393 * @did: RIO did to match or %RIO_ANY_ID to match all dids
394 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
395 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
396 * @from: Previous RIO device found in search, or %NULL for new search
398 * Iterates through the list of known RIO devices. If a RIO device is
399 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
400 * count to the device is incrememted and a pointer to its device
401 * structure is returned. Otherwise, %NULL is returned. A new search
402 * is initiated by passing %NULL to the @from argument. Otherwise, if
403 * @from is not %NULL, searches continue from next device on the global
404 * list. The reference count for @from is always decremented if it is
405 * not %NULL.
407 struct rio_dev *rio_get_asm(u16 vid, u16 did,
408 u16 asm_vid, u16 asm_did, struct rio_dev *from)
410 struct list_head *n;
411 struct rio_dev *rdev;
413 WARN_ON(in_interrupt());
414 spin_lock(&rio_global_list_lock);
415 n = from ? from->global_list.next : rio_devices.next;
417 while (n && (n != &rio_devices)) {
418 rdev = rio_dev_g(n);
419 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
420 (did == RIO_ANY_ID || rdev->did == did) &&
421 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
422 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
423 goto exit;
424 n = n->next;
426 rdev = NULL;
427 exit:
428 rio_dev_put(from);
429 rdev = rio_dev_get(rdev);
430 spin_unlock(&rio_global_list_lock);
431 return rdev;
435 * rio_get_device - Begin or continue searching for a RIO device by vid/did
436 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
437 * @did: RIO did to match or %RIO_ANY_ID to match all dids
438 * @from: Previous RIO device found in search, or %NULL for new search
440 * Iterates through the list of known RIO devices. If a RIO device is
441 * found with a matching @vid and @did, the reference count to the
442 * device is incrememted and a pointer to its device structure is returned.
443 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
444 * to the @from argument. Otherwise, if @from is not %NULL, searches
445 * continue from next device on the global list. The reference count for
446 * @from is always decremented if it is not %NULL.
448 struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
450 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
453 static void rio_fixup_device(struct rio_dev *dev)
457 static int __devinit rio_init(void)
459 struct rio_dev *dev = NULL;
461 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
462 rio_fixup_device(dev);
464 return 0;
467 device_initcall(rio_init);
469 int rio_init_mports(void)
471 int rc = 0;
472 struct rio_mport *port;
474 list_for_each_entry(port, &rio_mports, node) {
475 if (!request_mem_region(port->iores.start,
476 port->iores.end - port->iores.start,
477 port->name)) {
478 printk(KERN_ERR
479 "RIO: Error requesting master port region %8.8lx-%8.8lx\n",
480 port->iores.start, port->iores.end - 1);
481 rc = -ENOMEM;
482 goto out;
485 if (port->host_deviceid >= 0)
486 rio_enum_mport(port);
487 else
488 rio_disc_mport(port);
491 out:
492 return rc;
495 void rio_register_mport(struct rio_mport *port)
497 list_add_tail(&port->node, &rio_mports);
500 EXPORT_SYMBOL_GPL(rio_local_get_device_id);
501 EXPORT_SYMBOL_GPL(rio_get_device);
502 EXPORT_SYMBOL_GPL(rio_get_asm);
503 EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
504 EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
505 EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
506 EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
507 EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
508 EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
509 EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
510 EXPORT_SYMBOL_GPL(rio_release_outb_mbox);