]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/rapidio/rio.c
[PATCH] RapidIO support: core base
[linux-2.6-omap-h63xx.git] / drivers / rapidio / rio.c
1 /*
2  * RapidIO interconnect services
3  * (RapidIO Interconnect Specification, http://www.rapidio.org)
4  *
5  * Copyright 2005 MontaVista Software, Inc.
6  * Matt Porter <mporter@kernel.crashing.org>
7  *
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.
12  */
13
14 #include <linux/config.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/rio.h>
21 #include <linux/rio_drv.h>
22 #include <linux/rio_ids.h>
23 #include <linux/rio_regs.h>
24 #include <linux/module.h>
25 #include <linux/spinlock.h>
26
27 #include "rio.h"
28
29 static LIST_HEAD(rio_mports);
30
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
34  *
35  * Reads the base/extended device id from the local device
36  * implementing the master port. Returns the 8/16-bit device
37  * id.
38  */
39 u16 rio_local_get_device_id(struct rio_mport *port)
40 {
41         u32 result;
42
43         rio_local_read_config_32(port, RIO_DID_CSR, &result);
44
45         return (RIO_GET_DID(result));
46 }
47
48 /**
49  * rio_request_inb_mbox - request inbound mailbox service
50  * @mport: RIO master port from which to allocate the mailbox resource
51  * @mbox: Mailbox number to claim
52  * @entries: Number of entries in inbound mailbox queue
53  * @minb: Callback to execute when inbound message is received
54  *
55  * Requests ownership of an inbound mailbox resource and binds
56  * a callback function to the resource. Returns %0 on success.
57  */
58 int rio_request_inb_mbox(struct rio_mport *mport,
59                          int mbox,
60                          int entries,
61                          void (*minb) (struct rio_mport * mport, int mbox,
62                                        int slot))
63 {
64         int rc = 0;
65
66         struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
67
68         if (res) {
69                 rio_init_mbox_res(res, mbox, mbox);
70
71                 /* Make sure this mailbox isn't in use */
72                 if ((rc =
73                      request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
74                                       res)) < 0) {
75                         kfree(res);
76                         goto out;
77                 }
78
79                 mport->inb_msg[mbox].res = res;
80
81                 /* Hook the inbound message callback */
82                 mport->inb_msg[mbox].mcback = minb;
83
84                 rc = rio_open_inb_mbox(mport, mbox, entries);
85         } else
86                 rc = -ENOMEM;
87
88       out:
89         return rc;
90 }
91
92 /**
93  * rio_release_inb_mbox - release inbound mailbox message service
94  * @mport: RIO master port from which to release the mailbox resource
95  * @mbox: Mailbox number to release
96  *
97  * Releases ownership of an inbound mailbox resource. Returns 0
98  * if the request has been satisfied.
99  */
100 int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
101 {
102         rio_close_inb_mbox(mport, mbox);
103
104         /* Release the mailbox resource */
105         return release_resource(mport->inb_msg[mbox].res);
106 }
107
108 /**
109  * rio_request_outb_mbox - request outbound mailbox service
110  * @mport: RIO master port from which to allocate the mailbox resource
111  * @mbox: Mailbox number to claim
112  * @entries: Number of entries in outbound mailbox queue
113  * @moutb: Callback to execute when outbound message is sent
114  *
115  * Requests ownership of an outbound mailbox resource and binds
116  * a callback function to the resource. Returns 0 on success.
117  */
118 int rio_request_outb_mbox(struct rio_mport *mport,
119                           int mbox,
120                           int entries,
121                           void (*moutb) (struct rio_mport * mport, int mbox,
122                                          int slot))
123 {
124         int rc = 0;
125
126         struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
127
128         if (res) {
129                 rio_init_mbox_res(res, mbox, mbox);
130
131                 /* Make sure this outbound mailbox isn't in use */
132                 if ((rc =
133                      request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
134                                       res)) < 0) {
135                         kfree(res);
136                         goto out;
137                 }
138
139                 mport->outb_msg[mbox].res = res;
140
141                 /* Hook the inbound message callback */
142                 mport->outb_msg[mbox].mcback = moutb;
143
144                 rc = rio_open_outb_mbox(mport, mbox, entries);
145         } else
146                 rc = -ENOMEM;
147
148       out:
149         return rc;
150 }
151
152 /**
153  * rio_release_outb_mbox - release outbound mailbox message service
154  * @mport: RIO master port from which to release the mailbox resource
155  * @mbox: Mailbox number to release
156  *
157  * Releases ownership of an inbound mailbox resource. Returns 0
158  * if the request has been satisfied.
159  */
160 int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
161 {
162         rio_close_outb_mbox(mport, mbox);
163
164         /* Release the mailbox resource */
165         return release_resource(mport->outb_msg[mbox].res);
166 }
167
168 /**
169  * rio_setup_inb_dbell - bind inbound doorbell callback
170  * @mport: RIO master port to bind the doorbell callback
171  * @res: Doorbell message resource
172  * @dinb: Callback to execute when doorbell is received
173  *
174  * Adds a doorbell resource/callback pair into a port's
175  * doorbell event list. Returns 0 if the request has been
176  * satisfied.
177  */
178 static int
179 rio_setup_inb_dbell(struct rio_mport *mport, struct resource *res,
180                     void (*dinb) (struct rio_mport * mport, u16 src, u16 dst,
181                                   u16 info))
182 {
183         int rc = 0;
184         struct rio_dbell *dbell;
185
186         if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
187                 rc = -ENOMEM;
188                 goto out;
189         }
190
191         dbell->res = res;
192         dbell->dinb = dinb;
193
194         list_add_tail(&dbell->node, &mport->dbells);
195
196       out:
197         return rc;
198 }
199
200 /**
201  * rio_request_inb_dbell - request inbound doorbell message service
202  * @mport: RIO master port from which to allocate the doorbell resource
203  * @start: Doorbell info range start
204  * @end: Doorbell info range end
205  * @dinb: Callback to execute when doorbell is received
206  *
207  * Requests ownership of an inbound doorbell resource and binds
208  * a callback function to the resource. Returns 0 if the request
209  * has been satisfied.
210  */
211 int rio_request_inb_dbell(struct rio_mport *mport,
212                           u16 start,
213                           u16 end,
214                           void (*dinb) (struct rio_mport * mport, u16 src,
215                                         u16 dst, u16 info))
216 {
217         int rc = 0;
218
219         struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
220
221         if (res) {
222                 rio_init_dbell_res(res, start, end);
223
224                 /* Make sure these doorbells aren't in use */
225                 if ((rc =
226                      request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
227                                       res)) < 0) {
228                         kfree(res);
229                         goto out;
230                 }
231
232                 /* Hook the doorbell callback */
233                 rc = rio_setup_inb_dbell(mport, res, dinb);
234         } else
235                 rc = -ENOMEM;
236
237       out:
238         return rc;
239 }
240
241 /**
242  * rio_release_inb_dbell - release inbound doorbell message service
243  * @mport: RIO master port from which to release the doorbell resource
244  * @start: Doorbell info range start
245  * @end: Doorbell info range end
246  *
247  * Releases ownership of an inbound doorbell resource and removes
248  * callback from the doorbell event list. Returns 0 if the request
249  * has been satisfied.
250  */
251 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
252 {
253         int rc = 0, found = 0;
254         struct rio_dbell *dbell;
255
256         list_for_each_entry(dbell, &mport->dbells, node) {
257                 if ((dbell->res->start == start) && (dbell->res->end == end)) {
258                         found = 1;
259                         break;
260                 }
261         }
262
263         /* If we can't find an exact match, fail */
264         if (!found) {
265                 rc = -EINVAL;
266                 goto out;
267         }
268
269         /* Delete from list */
270         list_del(&dbell->node);
271
272         /* Release the doorbell resource */
273         rc = release_resource(dbell->res);
274
275         /* Free the doorbell event */
276         kfree(dbell);
277
278       out:
279         return rc;
280 }
281
282 /**
283  * rio_request_outb_dbell - request outbound doorbell message range
284  * @rdev: RIO device from which to allocate the doorbell resource
285  * @start: Doorbell message range start
286  * @end: Doorbell message range end
287  *
288  * Requests ownership of a doorbell message range. Returns a resource
289  * if the request has been satisfied or %NULL on failure.
290  */
291 struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
292                                         u16 end)
293 {
294         struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
295
296         if (res) {
297                 rio_init_dbell_res(res, start, end);
298
299                 /* Make sure these doorbells aren't in use */
300                 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
301                     < 0) {
302                         kfree(res);
303                         res = NULL;
304                 }
305         }
306
307         return res;
308 }
309
310 /**
311  * rio_release_outb_dbell - release outbound doorbell message range
312  * @rdev: RIO device from which to release the doorbell resource
313  * @res: Doorbell resource to be freed
314  *
315  * Releases ownership of a doorbell message range. Returns 0 if the
316  * request has been satisfied.
317  */
318 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
319 {
320         int rc = release_resource(res);
321
322         kfree(res);
323
324         return rc;
325 }
326
327 /**
328  * rio_mport_get_feature - query for devices' extended features
329  * @port: Master port to issue transaction
330  * @local: Indicate a local master port or remote device access
331  * @destid: Destination ID of the device
332  * @hopcount: Number of switch hops to the device
333  * @ftr: Extended feature code
334  *
335  * Tell if a device supports a given RapidIO capability.
336  * Returns the offset of the requested extended feature
337  * block within the device's RIO configuration space or
338  * 0 in case the device does not support it.  Possible
339  * values for @ftr:
340  *
341  * %RIO_EFB_PAR_EP_ID           LP/LVDS EP Devices
342  *
343  * %RIO_EFB_PAR_EP_REC_ID       LP/LVDS EP Recovery Devices
344  *
345  * %RIO_EFB_PAR_EP_FREE_ID      LP/LVDS EP Free Devices
346  *
347  * %RIO_EFB_SER_EP_ID           LP/Serial EP Devices
348  *
349  * %RIO_EFB_SER_EP_REC_ID       LP/Serial EP Recovery Devices
350  *
351  * %RIO_EFB_SER_EP_FREE_ID      LP/Serial EP Free Devices
352  */
353 u32
354 rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
355                       u8 hopcount, int ftr)
356 {
357         u32 asm_info, ext_ftr_ptr, ftr_header;
358
359         if (local)
360                 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
361         else
362                 rio_mport_read_config_32(port, destid, hopcount,
363                                          RIO_ASM_INFO_CAR, &asm_info);
364
365         ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
366
367         while (ext_ftr_ptr) {
368                 if (local)
369                         rio_local_read_config_32(port, ext_ftr_ptr,
370                                                  &ftr_header);
371                 else
372                         rio_mport_read_config_32(port, destid, hopcount,
373                                                  ext_ftr_ptr, &ftr_header);
374                 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
375                         return ext_ftr_ptr;
376                 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
377                         break;
378         }
379
380         return 0;
381 }
382
383 /**
384  * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
385  * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
386  * @did: RIO did to match or %RIO_ANY_ID to match all dids
387  * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
388  * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
389  * @from: Previous RIO device found in search, or %NULL for new search
390  *
391  * Iterates through the list of known RIO devices. If a RIO device is
392  * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
393  * count to the device is incrememted and a pointer to its device
394  * structure is returned. Otherwise, %NULL is returned. A new search
395  * is initiated by passing %NULL to the @from argument. Otherwise, if
396  * @from is not %NULL, searches continue from next device on the global
397  * list. The reference count for @from is always decremented if it is
398  * not %NULL.
399  */
400 struct rio_dev *rio_get_asm(u16 vid, u16 did,
401                             u16 asm_vid, u16 asm_did, struct rio_dev *from)
402 {
403         struct list_head *n;
404         struct rio_dev *rdev;
405
406         WARN_ON(in_interrupt());
407         spin_lock(&rio_global_list_lock);
408         n = from ? from->global_list.next : rio_devices.next;
409
410         while (n && (n != &rio_devices)) {
411                 rdev = rio_dev_g(n);
412                 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
413                     (did == RIO_ANY_ID || rdev->did == did) &&
414                     (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
415                     (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
416                         goto exit;
417                 n = n->next;
418         }
419         rdev = NULL;
420       exit:
421         rio_dev_put(from);
422         rdev = rio_dev_get(rdev);
423         spin_unlock(&rio_global_list_lock);
424         return rdev;
425 }
426
427 /**
428  * rio_get_device - Begin or continue searching for a RIO device by vid/did
429  * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
430  * @did: RIO did to match or %RIO_ANY_ID to match all dids
431  * @from: Previous RIO device found in search, or %NULL for new search
432  *
433  * Iterates through the list of known RIO devices. If a RIO device is
434  * found with a matching @vid and @did, the reference count to the
435  * device is incrememted and a pointer to its device structure is returned.
436  * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
437  * to the @from argument. Otherwise, if @from is not %NULL, searches
438  * continue from next device on the global list. The reference count for
439  * @from is always decremented if it is not %NULL.
440  */
441 struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
442 {
443         return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
444 }
445
446 static void rio_fixup_device(struct rio_dev *dev)
447 {
448 }
449
450 static int __devinit rio_init(void)
451 {
452         struct rio_dev *dev = NULL;
453
454         while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
455                 rio_fixup_device(dev);
456         }
457         return 0;
458 }
459
460 device_initcall(rio_init);
461
462 int rio_init_mports(void)
463 {
464         int rc = 0;
465         struct rio_mport *port;
466
467         list_for_each_entry(port, &rio_mports, node) {
468                 if (!request_mem_region(port->iores.start,
469                                         port->iores.end - port->iores.start,
470                                         port->name)) {
471                         printk(KERN_ERR
472                                "RIO: Error requesting master port region %8.8lx-%8.8lx\n",
473                                port->iores.start, port->iores.end - 1);
474                         rc = -ENOMEM;
475                         goto out;
476                 }
477
478                 if (port->host_deviceid >= 0)
479                         rio_enum_mport(port);
480                 else
481                         rio_disc_mport(port);
482         }
483
484       out:
485         return rc;
486 }
487
488 void rio_register_mport(struct rio_mport *port)
489 {
490         list_add_tail(&port->node, &rio_mports);
491 }
492
493 EXPORT_SYMBOL_GPL(rio_local_get_device_id);
494 EXPORT_SYMBOL_GPL(rio_get_device);
495 EXPORT_SYMBOL_GPL(rio_get_asm);
496 EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
497 EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
498 EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
499 EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
500 EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
501 EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
502 EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
503 EXPORT_SYMBOL_GPL(rio_release_outb_mbox);