net: define the TSO header size in net/tso.h
[linux-2.6/btrfs-unstable.git] / drivers / rapidio / rio.c
blob38d9494056181068bfa841f3676e49500950759c
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 * Copyright 2009 - 2013 Integrated Device Technology, Inc.
9 * Alex Bounine <alexandre.bounine@idt.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/types.h>
18 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/rio.h>
23 #include <linux/rio_drv.h>
24 #include <linux/rio_ids.h>
25 #include <linux/rio_regs.h>
26 #include <linux/module.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
31 #include "rio.h"
34 * struct rio_pwrite - RIO portwrite event
35 * @node: Node in list of doorbell events
36 * @pwcback: Doorbell event callback
37 * @context: Handler specific context to pass on event
39 struct rio_pwrite {
40 struct list_head node;
42 int (*pwcback)(struct rio_mport *mport, void *context,
43 union rio_pw_msg *msg, int step);
44 void *context;
47 MODULE_DESCRIPTION("RapidIO Subsystem Core");
48 MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
49 MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
50 MODULE_LICENSE("GPL");
52 static int hdid[RIO_MAX_MPORTS];
53 static int ids_num;
54 module_param_array(hdid, int, &ids_num, 0);
55 MODULE_PARM_DESC(hdid,
56 "Destination ID assignment to local RapidIO controllers");
58 static LIST_HEAD(rio_devices);
59 static LIST_HEAD(rio_nets);
60 static DEFINE_SPINLOCK(rio_global_list_lock);
62 static LIST_HEAD(rio_mports);
63 static LIST_HEAD(rio_scans);
64 static DEFINE_MUTEX(rio_mport_list_lock);
65 static unsigned char next_portid;
66 static DEFINE_SPINLOCK(rio_mmap_lock);
68 /**
69 * rio_local_get_device_id - Get the base/extended device id for a port
70 * @port: RIO master port from which to get the deviceid
72 * Reads the base/extended device id from the local device
73 * implementing the master port. Returns the 8/16-bit device
74 * id.
76 u16 rio_local_get_device_id(struct rio_mport *port)
78 u32 result;
80 rio_local_read_config_32(port, RIO_DID_CSR, &result);
82 return (RIO_GET_DID(port->sys_size, result));
85 /**
86 * rio_query_mport - Query mport device attributes
87 * @port: mport device to query
88 * @mport_attr: mport attributes data structure
90 * Returns attributes of specified mport through the
91 * pointer to attributes data structure.
93 int rio_query_mport(struct rio_mport *port,
94 struct rio_mport_attr *mport_attr)
96 if (!port->ops->query_mport)
97 return -ENODATA;
98 return port->ops->query_mport(port, mport_attr);
100 EXPORT_SYMBOL(rio_query_mport);
103 * rio_alloc_net- Allocate and initialize a new RIO network data structure
104 * @mport: Master port associated with the RIO network
106 * Allocates a RIO network structure, initializes per-network
107 * list heads, and adds the associated master port to the
108 * network list of associated master ports. Returns a
109 * RIO network pointer on success or %NULL on failure.
111 struct rio_net *rio_alloc_net(struct rio_mport *mport)
113 struct rio_net *net;
115 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
116 if (net) {
117 INIT_LIST_HEAD(&net->node);
118 INIT_LIST_HEAD(&net->devices);
119 INIT_LIST_HEAD(&net->switches);
120 INIT_LIST_HEAD(&net->mports);
121 mport->net = net;
123 return net;
125 EXPORT_SYMBOL_GPL(rio_alloc_net);
127 int rio_add_net(struct rio_net *net)
129 int err;
131 err = device_register(&net->dev);
132 if (err)
133 return err;
134 spin_lock(&rio_global_list_lock);
135 list_add_tail(&net->node, &rio_nets);
136 spin_unlock(&rio_global_list_lock);
138 return 0;
140 EXPORT_SYMBOL_GPL(rio_add_net);
142 void rio_free_net(struct rio_net *net)
144 spin_lock(&rio_global_list_lock);
145 if (!list_empty(&net->node))
146 list_del(&net->node);
147 spin_unlock(&rio_global_list_lock);
148 if (net->release)
149 net->release(net);
150 device_unregister(&net->dev);
152 EXPORT_SYMBOL_GPL(rio_free_net);
155 * rio_local_set_device_id - Set the base/extended device id for a port
156 * @port: RIO master port
157 * @did: Device ID value to be written
159 * Writes the base/extended device id from a device.
161 void rio_local_set_device_id(struct rio_mport *port, u16 did)
163 rio_local_write_config_32(port, RIO_DID_CSR,
164 RIO_SET_DID(port->sys_size, did));
166 EXPORT_SYMBOL_GPL(rio_local_set_device_id);
169 * rio_add_device- Adds a RIO device to the device model
170 * @rdev: RIO device
172 * Adds the RIO device to the global device list and adds the RIO
173 * device to the RIO device list. Creates the generic sysfs nodes
174 * for an RIO device.
176 int rio_add_device(struct rio_dev *rdev)
178 int err;
180 atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
181 err = device_register(&rdev->dev);
182 if (err)
183 return err;
185 spin_lock(&rio_global_list_lock);
186 list_add_tail(&rdev->global_list, &rio_devices);
187 if (rdev->net) {
188 list_add_tail(&rdev->net_list, &rdev->net->devices);
189 if (rdev->pef & RIO_PEF_SWITCH)
190 list_add_tail(&rdev->rswitch->node,
191 &rdev->net->switches);
193 spin_unlock(&rio_global_list_lock);
195 return 0;
197 EXPORT_SYMBOL_GPL(rio_add_device);
200 * rio_del_device - removes a RIO device from the device model
201 * @rdev: RIO device
202 * @state: device state to set during removal process
204 * Removes the RIO device to the kernel device list and subsystem's device list.
205 * Clears sysfs entries for the removed device.
207 void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
209 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
210 atomic_set(&rdev->state, state);
211 spin_lock(&rio_global_list_lock);
212 list_del(&rdev->global_list);
213 if (rdev->net) {
214 list_del(&rdev->net_list);
215 if (rdev->pef & RIO_PEF_SWITCH) {
216 list_del(&rdev->rswitch->node);
217 kfree(rdev->rswitch->route_table);
220 spin_unlock(&rio_global_list_lock);
221 device_unregister(&rdev->dev);
223 EXPORT_SYMBOL_GPL(rio_del_device);
226 * rio_request_inb_mbox - request inbound mailbox service
227 * @mport: RIO master port from which to allocate the mailbox resource
228 * @dev_id: Device specific pointer to pass on event
229 * @mbox: Mailbox number to claim
230 * @entries: Number of entries in inbound mailbox queue
231 * @minb: Callback to execute when inbound message is received
233 * Requests ownership of an inbound mailbox resource and binds
234 * a callback function to the resource. Returns %0 on success.
236 int rio_request_inb_mbox(struct rio_mport *mport,
237 void *dev_id,
238 int mbox,
239 int entries,
240 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
241 int slot))
243 int rc = -ENOSYS;
244 struct resource *res;
246 if (mport->ops->open_inb_mbox == NULL)
247 goto out;
249 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
251 if (res) {
252 rio_init_mbox_res(res, mbox, mbox);
254 /* Make sure this mailbox isn't in use */
255 if ((rc =
256 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
257 res)) < 0) {
258 kfree(res);
259 goto out;
262 mport->inb_msg[mbox].res = res;
264 /* Hook the inbound message callback */
265 mport->inb_msg[mbox].mcback = minb;
267 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
268 if (rc) {
269 mport->inb_msg[mbox].mcback = NULL;
270 mport->inb_msg[mbox].res = NULL;
271 release_resource(res);
272 kfree(res);
274 } else
275 rc = -ENOMEM;
277 out:
278 return rc;
282 * rio_release_inb_mbox - release inbound mailbox message service
283 * @mport: RIO master port from which to release the mailbox resource
284 * @mbox: Mailbox number to release
286 * Releases ownership of an inbound mailbox resource. Returns 0
287 * if the request has been satisfied.
289 int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
291 int rc;
293 if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res)
294 return -EINVAL;
296 mport->ops->close_inb_mbox(mport, mbox);
297 mport->inb_msg[mbox].mcback = NULL;
299 rc = release_resource(mport->inb_msg[mbox].res);
300 if (rc)
301 return rc;
303 kfree(mport->inb_msg[mbox].res);
304 mport->inb_msg[mbox].res = NULL;
306 return 0;
310 * rio_request_outb_mbox - request outbound mailbox service
311 * @mport: RIO master port from which to allocate the mailbox resource
312 * @dev_id: Device specific pointer to pass on event
313 * @mbox: Mailbox number to claim
314 * @entries: Number of entries in outbound mailbox queue
315 * @moutb: Callback to execute when outbound message is sent
317 * Requests ownership of an outbound mailbox resource and binds
318 * a callback function to the resource. Returns 0 on success.
320 int rio_request_outb_mbox(struct rio_mport *mport,
321 void *dev_id,
322 int mbox,
323 int entries,
324 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
326 int rc = -ENOSYS;
327 struct resource *res;
329 if (mport->ops->open_outb_mbox == NULL)
330 goto out;
332 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
334 if (res) {
335 rio_init_mbox_res(res, mbox, mbox);
337 /* Make sure this outbound mailbox isn't in use */
338 if ((rc =
339 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
340 res)) < 0) {
341 kfree(res);
342 goto out;
345 mport->outb_msg[mbox].res = res;
347 /* Hook the inbound message callback */
348 mport->outb_msg[mbox].mcback = moutb;
350 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
351 if (rc) {
352 mport->outb_msg[mbox].mcback = NULL;
353 mport->outb_msg[mbox].res = NULL;
354 release_resource(res);
355 kfree(res);
357 } else
358 rc = -ENOMEM;
360 out:
361 return rc;
365 * rio_release_outb_mbox - release outbound mailbox message service
366 * @mport: RIO master port from which to release the mailbox resource
367 * @mbox: Mailbox number to release
369 * Releases ownership of an inbound mailbox resource. Returns 0
370 * if the request has been satisfied.
372 int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
374 int rc;
376 if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res)
377 return -EINVAL;
379 mport->ops->close_outb_mbox(mport, mbox);
380 mport->outb_msg[mbox].mcback = NULL;
382 rc = release_resource(mport->outb_msg[mbox].res);
383 if (rc)
384 return rc;
386 kfree(mport->outb_msg[mbox].res);
387 mport->outb_msg[mbox].res = NULL;
389 return 0;
393 * rio_setup_inb_dbell - bind inbound doorbell callback
394 * @mport: RIO master port to bind the doorbell callback
395 * @dev_id: Device specific pointer to pass on event
396 * @res: Doorbell message resource
397 * @dinb: Callback to execute when doorbell is received
399 * Adds a doorbell resource/callback pair into a port's
400 * doorbell event list. Returns 0 if the request has been
401 * satisfied.
403 static int
404 rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
405 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
406 u16 info))
408 int rc = 0;
409 struct rio_dbell *dbell;
411 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
412 rc = -ENOMEM;
413 goto out;
416 dbell->res = res;
417 dbell->dinb = dinb;
418 dbell->dev_id = dev_id;
420 mutex_lock(&mport->lock);
421 list_add_tail(&dbell->node, &mport->dbells);
422 mutex_unlock(&mport->lock);
424 out:
425 return rc;
429 * rio_request_inb_dbell - request inbound doorbell message service
430 * @mport: RIO master port from which to allocate the doorbell resource
431 * @dev_id: Device specific pointer to pass on event
432 * @start: Doorbell info range start
433 * @end: Doorbell info range end
434 * @dinb: Callback to execute when doorbell is received
436 * Requests ownership of an inbound doorbell resource and binds
437 * a callback function to the resource. Returns 0 if the request
438 * has been satisfied.
440 int rio_request_inb_dbell(struct rio_mport *mport,
441 void *dev_id,
442 u16 start,
443 u16 end,
444 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
445 u16 dst, u16 info))
447 int rc = 0;
449 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
451 if (res) {
452 rio_init_dbell_res(res, start, end);
454 /* Make sure these doorbells aren't in use */
455 if ((rc =
456 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
457 res)) < 0) {
458 kfree(res);
459 goto out;
462 /* Hook the doorbell callback */
463 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
464 } else
465 rc = -ENOMEM;
467 out:
468 return rc;
472 * rio_release_inb_dbell - release inbound doorbell message service
473 * @mport: RIO master port from which to release the doorbell resource
474 * @start: Doorbell info range start
475 * @end: Doorbell info range end
477 * Releases ownership of an inbound doorbell resource and removes
478 * callback from the doorbell event list. Returns 0 if the request
479 * has been satisfied.
481 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
483 int rc = 0, found = 0;
484 struct rio_dbell *dbell;
486 mutex_lock(&mport->lock);
487 list_for_each_entry(dbell, &mport->dbells, node) {
488 if ((dbell->res->start == start) && (dbell->res->end == end)) {
489 list_del(&dbell->node);
490 found = 1;
491 break;
494 mutex_unlock(&mport->lock);
496 /* If we can't find an exact match, fail */
497 if (!found) {
498 rc = -EINVAL;
499 goto out;
502 /* Release the doorbell resource */
503 rc = release_resource(dbell->res);
505 /* Free the doorbell event */
506 kfree(dbell);
508 out:
509 return rc;
513 * rio_request_outb_dbell - request outbound doorbell message range
514 * @rdev: RIO device from which to allocate the doorbell resource
515 * @start: Doorbell message range start
516 * @end: Doorbell message range end
518 * Requests ownership of a doorbell message range. Returns a resource
519 * if the request has been satisfied or %NULL on failure.
521 struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
522 u16 end)
524 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
526 if (res) {
527 rio_init_dbell_res(res, start, end);
529 /* Make sure these doorbells aren't in use */
530 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
531 < 0) {
532 kfree(res);
533 res = NULL;
537 return res;
541 * rio_release_outb_dbell - release outbound doorbell message range
542 * @rdev: RIO device from which to release the doorbell resource
543 * @res: Doorbell resource to be freed
545 * Releases ownership of a doorbell message range. Returns 0 if the
546 * request has been satisfied.
548 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
550 int rc = release_resource(res);
552 kfree(res);
554 return rc;
558 * rio_add_mport_pw_handler - add port-write message handler into the list
559 * of mport specific pw handlers
560 * @mport: RIO master port to bind the portwrite callback
561 * @context: Handler specific context to pass on event
562 * @pwcback: Callback to execute when portwrite is received
564 * Returns 0 if the request has been satisfied.
566 int rio_add_mport_pw_handler(struct rio_mport *mport, void *context,
567 int (*pwcback)(struct rio_mport *mport,
568 void *context, union rio_pw_msg *msg, int step))
570 int rc = 0;
571 struct rio_pwrite *pwrite;
573 pwrite = kzalloc(sizeof(struct rio_pwrite), GFP_KERNEL);
574 if (!pwrite) {
575 rc = -ENOMEM;
576 goto out;
579 pwrite->pwcback = pwcback;
580 pwrite->context = context;
581 mutex_lock(&mport->lock);
582 list_add_tail(&pwrite->node, &mport->pwrites);
583 mutex_unlock(&mport->lock);
584 out:
585 return rc;
587 EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler);
590 * rio_del_mport_pw_handler - remove port-write message handler from the list
591 * of mport specific pw handlers
592 * @mport: RIO master port to bind the portwrite callback
593 * @context: Registered handler specific context to pass on event
594 * @pwcback: Registered callback function
596 * Returns 0 if the request has been satisfied.
598 int rio_del_mport_pw_handler(struct rio_mport *mport, void *context,
599 int (*pwcback)(struct rio_mport *mport,
600 void *context, union rio_pw_msg *msg, int step))
602 int rc = -EINVAL;
603 struct rio_pwrite *pwrite;
605 mutex_lock(&mport->lock);
606 list_for_each_entry(pwrite, &mport->pwrites, node) {
607 if (pwrite->pwcback == pwcback && pwrite->context == context) {
608 list_del(&pwrite->node);
609 kfree(pwrite);
610 rc = 0;
611 break;
614 mutex_unlock(&mport->lock);
616 return rc;
618 EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler);
621 * rio_request_inb_pwrite - request inbound port-write message service for
622 * specific RapidIO device
623 * @rdev: RIO device to which register inbound port-write callback routine
624 * @pwcback: Callback routine to execute when port-write is received
626 * Binds a port-write callback function to the RapidIO device.
627 * Returns 0 if the request has been satisfied.
629 int rio_request_inb_pwrite(struct rio_dev *rdev,
630 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
632 int rc = 0;
634 spin_lock(&rio_global_list_lock);
635 if (rdev->pwcback != NULL)
636 rc = -ENOMEM;
637 else
638 rdev->pwcback = pwcback;
640 spin_unlock(&rio_global_list_lock);
641 return rc;
643 EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
646 * rio_release_inb_pwrite - release inbound port-write message service
647 * associated with specific RapidIO device
648 * @rdev: RIO device which registered for inbound port-write callback
650 * Removes callback from the rio_dev structure. Returns 0 if the request
651 * has been satisfied.
653 int rio_release_inb_pwrite(struct rio_dev *rdev)
655 int rc = -ENOMEM;
657 spin_lock(&rio_global_list_lock);
658 if (rdev->pwcback) {
659 rdev->pwcback = NULL;
660 rc = 0;
663 spin_unlock(&rio_global_list_lock);
664 return rc;
666 EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
669 * rio_pw_enable - Enables/disables port-write handling by a master port
670 * @mport: Master port associated with port-write handling
671 * @enable: 1=enable, 0=disable
673 void rio_pw_enable(struct rio_mport *mport, int enable)
675 if (mport->ops->pwenable) {
676 mutex_lock(&mport->lock);
678 if ((enable && ++mport->pwe_refcnt == 1) ||
679 (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
680 mport->ops->pwenable(mport, enable);
681 mutex_unlock(&mport->lock);
684 EXPORT_SYMBOL_GPL(rio_pw_enable);
687 * rio_map_inb_region -- Map inbound memory region.
688 * @mport: Master port.
689 * @local: physical address of memory region to be mapped
690 * @rbase: RIO base address assigned to this window
691 * @size: Size of the memory region
692 * @rflags: Flags for mapping.
694 * Return: 0 -- Success.
696 * This function will create the mapping from RIO space to local memory.
698 int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
699 u64 rbase, u32 size, u32 rflags)
701 int rc = 0;
702 unsigned long flags;
704 if (!mport->ops->map_inb)
705 return -1;
706 spin_lock_irqsave(&rio_mmap_lock, flags);
707 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
708 spin_unlock_irqrestore(&rio_mmap_lock, flags);
709 return rc;
711 EXPORT_SYMBOL_GPL(rio_map_inb_region);
714 * rio_unmap_inb_region -- Unmap the inbound memory region
715 * @mport: Master port
716 * @lstart: physical address of memory region to be unmapped
718 void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
720 unsigned long flags;
721 if (!mport->ops->unmap_inb)
722 return;
723 spin_lock_irqsave(&rio_mmap_lock, flags);
724 mport->ops->unmap_inb(mport, lstart);
725 spin_unlock_irqrestore(&rio_mmap_lock, flags);
727 EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
730 * rio_map_outb_region -- Map outbound memory region.
731 * @mport: Master port.
732 * @destid: destination id window points to
733 * @rbase: RIO base address window translates to
734 * @size: Size of the memory region
735 * @rflags: Flags for mapping.
736 * @local: physical address of memory region mapped
738 * Return: 0 -- Success.
740 * This function will create the mapping from RIO space to local memory.
742 int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase,
743 u32 size, u32 rflags, dma_addr_t *local)
745 int rc = 0;
746 unsigned long flags;
748 if (!mport->ops->map_outb)
749 return -ENODEV;
751 spin_lock_irqsave(&rio_mmap_lock, flags);
752 rc = mport->ops->map_outb(mport, destid, rbase, size,
753 rflags, local);
754 spin_unlock_irqrestore(&rio_mmap_lock, flags);
756 return rc;
758 EXPORT_SYMBOL_GPL(rio_map_outb_region);
761 * rio_unmap_inb_region -- Unmap the inbound memory region
762 * @mport: Master port
763 * @destid: destination id mapping points to
764 * @rstart: RIO base address window translates to
766 void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart)
768 unsigned long flags;
770 if (!mport->ops->unmap_outb)
771 return;
773 spin_lock_irqsave(&rio_mmap_lock, flags);
774 mport->ops->unmap_outb(mport, destid, rstart);
775 spin_unlock_irqrestore(&rio_mmap_lock, flags);
777 EXPORT_SYMBOL_GPL(rio_unmap_outb_region);
780 * rio_mport_get_physefb - Helper function that returns register offset
781 * for Physical Layer Extended Features Block.
782 * @port: Master port to issue transaction
783 * @local: Indicate a local master port or remote device access
784 * @destid: Destination ID of the device
785 * @hopcount: Number of switch hops to the device
786 * @rmap: pointer to location to store register map type info
789 rio_mport_get_physefb(struct rio_mport *port, int local,
790 u16 destid, u8 hopcount, u32 *rmap)
792 u32 ext_ftr_ptr;
793 u32 ftr_header;
795 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
797 while (ext_ftr_ptr) {
798 if (local)
799 rio_local_read_config_32(port, ext_ftr_ptr,
800 &ftr_header);
801 else
802 rio_mport_read_config_32(port, destid, hopcount,
803 ext_ftr_ptr, &ftr_header);
805 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
806 switch (ftr_header) {
808 case RIO_EFB_SER_EP_ID:
809 case RIO_EFB_SER_EP_REC_ID:
810 case RIO_EFB_SER_EP_FREE_ID:
811 case RIO_EFB_SER_EP_M1_ID:
812 case RIO_EFB_SER_EP_SW_M1_ID:
813 case RIO_EFB_SER_EPF_M1_ID:
814 case RIO_EFB_SER_EPF_SW_M1_ID:
815 *rmap = 1;
816 return ext_ftr_ptr;
818 case RIO_EFB_SER_EP_M2_ID:
819 case RIO_EFB_SER_EP_SW_M2_ID:
820 case RIO_EFB_SER_EPF_M2_ID:
821 case RIO_EFB_SER_EPF_SW_M2_ID:
822 *rmap = 2;
823 return ext_ftr_ptr;
825 default:
826 break;
829 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
830 hopcount, ext_ftr_ptr);
833 return ext_ftr_ptr;
835 EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
838 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
839 * @comp_tag: RIO component tag to match
840 * @from: Previous RIO device found in search, or %NULL for new search
842 * Iterates through the list of known RIO devices. If a RIO device is
843 * found with a matching @comp_tag, a pointer to its device
844 * structure is returned. Otherwise, %NULL is returned. A new search
845 * is initiated by passing %NULL to the @from argument. Otherwise, if
846 * @from is not %NULL, searches continue from next device on the global
847 * list.
849 struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
851 struct list_head *n;
852 struct rio_dev *rdev;
854 spin_lock(&rio_global_list_lock);
855 n = from ? from->global_list.next : rio_devices.next;
857 while (n && (n != &rio_devices)) {
858 rdev = rio_dev_g(n);
859 if (rdev->comp_tag == comp_tag)
860 goto exit;
861 n = n->next;
863 rdev = NULL;
864 exit:
865 spin_unlock(&rio_global_list_lock);
866 return rdev;
868 EXPORT_SYMBOL_GPL(rio_get_comptag);
871 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
872 * @rdev: Pointer to RIO device control structure
873 * @pnum: Switch port number to set LOCKOUT bit
874 * @lock: Operation : set (=1) or clear (=0)
876 int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
878 u32 regval;
880 rio_read_config_32(rdev,
881 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
882 &regval);
883 if (lock)
884 regval |= RIO_PORT_N_CTL_LOCKOUT;
885 else
886 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
888 rio_write_config_32(rdev,
889 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
890 regval);
891 return 0;
893 EXPORT_SYMBOL_GPL(rio_set_port_lockout);
896 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
897 * given port
898 * @port: Master port associated with the RIO network
899 * @local: local=1 select local port otherwise a far device is reached
900 * @destid: Destination ID of the device to check host bit
901 * @hopcount: Number of hops to reach the target
902 * @port_num: Port (-number on switch) to enable on a far end device
904 * Returns 0 or 1 from on General Control Command and Status Register
905 * (EXT_PTR+0x3C)
907 int rio_enable_rx_tx_port(struct rio_mport *port,
908 int local, u16 destid,
909 u8 hopcount, u8 port_num)
911 #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
912 u32 regval;
913 u32 ext_ftr_ptr;
914 u32 rmap;
917 * enable rx input tx output port
919 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
920 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
922 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid,
923 hopcount, &rmap);
925 if (local) {
926 rio_local_read_config_32(port,
927 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap),
928 &regval);
929 } else {
930 if (rio_mport_read_config_32(port, destid, hopcount,
931 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
932 &regval) < 0)
933 return -EIO;
936 regval = regval | RIO_PORT_N_CTL_EN_RX | RIO_PORT_N_CTL_EN_TX;
938 if (local) {
939 rio_local_write_config_32(port,
940 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), regval);
941 } else {
942 if (rio_mport_write_config_32(port, destid, hopcount,
943 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
944 regval) < 0)
945 return -EIO;
947 #endif
948 return 0;
950 EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
954 * rio_chk_dev_route - Validate route to the specified device.
955 * @rdev: RIO device failed to respond
956 * @nrdev: Last active device on the route to rdev
957 * @npnum: nrdev's port number on the route to rdev
959 * Follows a route to the specified RIO device to determine the last available
960 * device (and corresponding RIO port) on the route.
962 static int
963 rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
965 u32 result;
966 int p_port, rc = -EIO;
967 struct rio_dev *prev = NULL;
969 /* Find switch with failed RIO link */
970 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
971 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
972 prev = rdev->prev;
973 break;
975 rdev = rdev->prev;
978 if (prev == NULL)
979 goto err_out;
981 p_port = prev->rswitch->route_table[rdev->destid];
983 if (p_port != RIO_INVALID_ROUTE) {
984 pr_debug("RIO: link failed on [%s]-P%d\n",
985 rio_name(prev), p_port);
986 *nrdev = prev;
987 *npnum = p_port;
988 rc = 0;
989 } else
990 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
991 err_out:
992 return rc;
996 * rio_mport_chk_dev_access - Validate access to the specified device.
997 * @mport: Master port to send transactions
998 * @destid: Device destination ID in network
999 * @hopcount: Number of hops into the network
1002 rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
1004 int i = 0;
1005 u32 tmp;
1007 while (rio_mport_read_config_32(mport, destid, hopcount,
1008 RIO_DEV_ID_CAR, &tmp)) {
1009 i++;
1010 if (i == RIO_MAX_CHK_RETRY)
1011 return -EIO;
1012 mdelay(1);
1015 return 0;
1017 EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
1020 * rio_chk_dev_access - Validate access to the specified device.
1021 * @rdev: Pointer to RIO device control structure
1023 static int rio_chk_dev_access(struct rio_dev *rdev)
1025 return rio_mport_chk_dev_access(rdev->net->hport,
1026 rdev->destid, rdev->hopcount);
1030 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
1031 * returns link-response (if requested).
1032 * @rdev: RIO devive to issue Input-status command
1033 * @pnum: Device port number to issue the command
1034 * @lnkresp: Response from a link partner
1036 static int
1037 rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
1039 u32 regval;
1040 int checkcount;
1042 if (lnkresp) {
1043 /* Read from link maintenance response register
1044 * to clear valid bit */
1045 rio_read_config_32(rdev,
1046 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
1047 &regval);
1048 udelay(50);
1051 /* Issue Input-status command */
1052 rio_write_config_32(rdev,
1053 RIO_DEV_PORT_N_MNT_REQ_CSR(rdev, pnum),
1054 RIO_MNT_REQ_CMD_IS);
1056 /* Exit if the response is not expected */
1057 if (lnkresp == NULL)
1058 return 0;
1060 checkcount = 3;
1061 while (checkcount--) {
1062 udelay(50);
1063 rio_read_config_32(rdev,
1064 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
1065 &regval);
1066 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
1067 *lnkresp = regval;
1068 return 0;
1072 return -EIO;
1076 * rio_clr_err_stopped - Clears port Error-stopped states.
1077 * @rdev: Pointer to RIO device control structure
1078 * @pnum: Switch port number to clear errors
1079 * @err_status: port error status (if 0 reads register from device)
1081 * TODO: Currently this routine is not compatible with recovery process
1082 * specified for idt_gen3 RapidIO switch devices. It has to be reviewed
1083 * to implement universal recovery process that is compatible full range
1084 * off available devices.
1085 * IDT gen3 switch driver now implements HW-specific error handler that
1086 * issues soft port reset to the port to reset ERR_STOP bits and ackIDs.
1088 static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
1090 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
1091 u32 regval;
1092 u32 far_ackid, far_linkstat, near_ackid;
1094 if (err_status == 0)
1095 rio_read_config_32(rdev,
1096 RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1097 &err_status);
1099 if (err_status & RIO_PORT_N_ERR_STS_OUT_ES) {
1100 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
1102 * Send a Link-Request/Input-Status control symbol
1104 if (rio_get_input_status(rdev, pnum, &regval)) {
1105 pr_debug("RIO_EM: Input-status response timeout\n");
1106 goto rd_err;
1109 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
1110 pnum, regval);
1111 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
1112 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
1113 rio_read_config_32(rdev,
1114 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
1115 &regval);
1116 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
1117 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
1118 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
1119 " near_ackID=0x%02x\n",
1120 pnum, far_ackid, far_linkstat, near_ackid);
1123 * If required, synchronize ackIDs of near and
1124 * far sides.
1126 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
1127 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
1128 /* Align near outstanding/outbound ackIDs with
1129 * far inbound.
1131 rio_write_config_32(rdev,
1132 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
1133 (near_ackid << 24) |
1134 (far_ackid << 8) | far_ackid);
1135 /* Align far outstanding/outbound ackIDs with
1136 * near inbound.
1138 far_ackid++;
1139 if (!nextdev) {
1140 pr_debug("RIO_EM: nextdev pointer == NULL\n");
1141 goto rd_err;
1144 rio_write_config_32(nextdev,
1145 RIO_DEV_PORT_N_ACK_STS_CSR(nextdev,
1146 RIO_GET_PORT_NUM(nextdev->swpinfo)),
1147 (far_ackid << 24) |
1148 (near_ackid << 8) | near_ackid);
1150 rd_err:
1151 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1152 &err_status);
1153 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1156 if ((err_status & RIO_PORT_N_ERR_STS_INP_ES) && nextdev) {
1157 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
1158 rio_get_input_status(nextdev,
1159 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
1160 udelay(50);
1162 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1163 &err_status);
1164 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1167 return (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1168 RIO_PORT_N_ERR_STS_INP_ES)) ? 1 : 0;
1172 * rio_inb_pwrite_handler - inbound port-write message handler
1173 * @mport: mport device associated with port-write
1174 * @pw_msg: pointer to inbound port-write message
1176 * Processes an inbound port-write message. Returns 0 if the request
1177 * has been satisfied.
1179 int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg)
1181 struct rio_dev *rdev;
1182 u32 err_status, em_perrdet, em_ltlerrdet;
1183 int rc, portnum;
1184 struct rio_pwrite *pwrite;
1186 #ifdef DEBUG_PW
1188 u32 i;
1190 pr_debug("%s: PW to mport_%d:\n", __func__, mport->id);
1191 for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) {
1192 pr_debug("0x%02x: %08x %08x %08x %08x\n",
1193 i * 4, pw_msg->raw[i], pw_msg->raw[i + 1],
1194 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1197 #endif
1199 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
1200 if (rdev) {
1201 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
1202 } else {
1203 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
1204 __func__, pw_msg->em.comptag);
1207 /* Call a device-specific handler (if it is registered for the device).
1208 * This may be the service for endpoints that send device-specific
1209 * port-write messages. End-point messages expected to be handled
1210 * completely by EP specific device driver.
1211 * For switches rc==0 signals that no standard processing required.
1213 if (rdev && rdev->pwcback) {
1214 rc = rdev->pwcback(rdev, pw_msg, 0);
1215 if (rc == 0)
1216 return 0;
1219 mutex_lock(&mport->lock);
1220 list_for_each_entry(pwrite, &mport->pwrites, node)
1221 pwrite->pwcback(mport, pwrite->context, pw_msg, 0);
1222 mutex_unlock(&mport->lock);
1224 if (!rdev)
1225 return 0;
1228 * FIXME: The code below stays as it was before for now until we decide
1229 * how to do default PW handling in combination with per-mport callbacks
1232 portnum = pw_msg->em.is_port & 0xFF;
1234 /* Check if device and route to it are functional:
1235 * Sometimes devices may send PW message(s) just before being
1236 * powered down (or link being lost).
1238 if (rio_chk_dev_access(rdev)) {
1239 pr_debug("RIO: device access failed - get link partner\n");
1240 /* Scan route to the device and identify failed link.
1241 * This will replace device and port reported in PW message.
1242 * PW message should not be used after this point.
1244 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
1245 pr_err("RIO: Route trace for %s failed\n",
1246 rio_name(rdev));
1247 return -EIO;
1249 pw_msg = NULL;
1252 /* For End-point devices processing stops here */
1253 if (!(rdev->pef & RIO_PEF_SWITCH))
1254 return 0;
1256 if (rdev->phys_efptr == 0) {
1257 pr_err("RIO_PW: Bad switch initialization for %s\n",
1258 rio_name(rdev));
1259 return 0;
1263 * Process the port-write notification from switch
1265 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
1266 rdev->rswitch->ops->em_handle(rdev, portnum);
1268 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1269 &err_status);
1270 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1272 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
1274 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1275 rdev->rswitch->port_ok |= (1 << portnum);
1276 rio_set_port_lockout(rdev, portnum, 0);
1277 /* Schedule Insertion Service */
1278 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1279 rio_name(rdev), portnum);
1282 /* Clear error-stopped states (if reported).
1283 * Depending on the link partner state, two attempts
1284 * may be needed for successful recovery.
1286 if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1287 RIO_PORT_N_ERR_STS_INP_ES)) {
1288 if (rio_clr_err_stopped(rdev, portnum, err_status))
1289 rio_clr_err_stopped(rdev, portnum, 0);
1291 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
1293 if (rdev->rswitch->port_ok & (1 << portnum)) {
1294 rdev->rswitch->port_ok &= ~(1 << portnum);
1295 rio_set_port_lockout(rdev, portnum, 1);
1297 if (rdev->phys_rmap == 1) {
1298 rio_write_config_32(rdev,
1299 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, portnum),
1300 RIO_PORT_N_ACK_CLEAR);
1301 } else {
1302 rio_write_config_32(rdev,
1303 RIO_DEV_PORT_N_OB_ACK_CSR(rdev, portnum),
1304 RIO_PORT_N_OB_ACK_CLEAR);
1305 rio_write_config_32(rdev,
1306 RIO_DEV_PORT_N_IB_ACK_CSR(rdev, portnum),
1310 /* Schedule Extraction Service */
1311 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1312 rio_name(rdev), portnum);
1316 rio_read_config_32(rdev,
1317 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1318 if (em_perrdet) {
1319 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1320 portnum, em_perrdet);
1321 /* Clear EM Port N Error Detect CSR */
1322 rio_write_config_32(rdev,
1323 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1326 rio_read_config_32(rdev,
1327 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1328 if (em_ltlerrdet) {
1329 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1330 em_ltlerrdet);
1331 /* Clear EM L/T Layer Error Detect CSR */
1332 rio_write_config_32(rdev,
1333 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1336 /* Clear remaining error bits and Port-Write Pending bit */
1337 rio_write_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1338 err_status);
1340 return 0;
1342 EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1345 * rio_mport_get_efb - get pointer to next extended features block
1346 * @port: Master port to issue transaction
1347 * @local: Indicate a local master port or remote device access
1348 * @destid: Destination ID of the device
1349 * @hopcount: Number of switch hops to the device
1350 * @from: Offset of current Extended Feature block header (if 0 starts
1351 * from ExtFeaturePtr)
1354 rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1355 u8 hopcount, u32 from)
1357 u32 reg_val;
1359 if (from == 0) {
1360 if (local)
1361 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1362 &reg_val);
1363 else
1364 rio_mport_read_config_32(port, destid, hopcount,
1365 RIO_ASM_INFO_CAR, &reg_val);
1366 return reg_val & RIO_EXT_FTR_PTR_MASK;
1367 } else {
1368 if (local)
1369 rio_local_read_config_32(port, from, &reg_val);
1370 else
1371 rio_mport_read_config_32(port, destid, hopcount,
1372 from, &reg_val);
1373 return RIO_GET_BLOCK_ID(reg_val);
1376 EXPORT_SYMBOL_GPL(rio_mport_get_efb);
1379 * rio_mport_get_feature - query for devices' extended features
1380 * @port: Master port to issue transaction
1381 * @local: Indicate a local master port or remote device access
1382 * @destid: Destination ID of the device
1383 * @hopcount: Number of switch hops to the device
1384 * @ftr: Extended feature code
1386 * Tell if a device supports a given RapidIO capability.
1387 * Returns the offset of the requested extended feature
1388 * block within the device's RIO configuration space or
1389 * 0 in case the device does not support it.
1392 rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1393 u8 hopcount, int ftr)
1395 u32 asm_info, ext_ftr_ptr, ftr_header;
1397 if (local)
1398 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1399 else
1400 rio_mport_read_config_32(port, destid, hopcount,
1401 RIO_ASM_INFO_CAR, &asm_info);
1403 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1405 while (ext_ftr_ptr) {
1406 if (local)
1407 rio_local_read_config_32(port, ext_ftr_ptr,
1408 &ftr_header);
1409 else
1410 rio_mport_read_config_32(port, destid, hopcount,
1411 ext_ftr_ptr, &ftr_header);
1412 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1413 return ext_ftr_ptr;
1414 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
1415 break;
1418 return 0;
1420 EXPORT_SYMBOL_GPL(rio_mport_get_feature);
1423 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1424 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1425 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1426 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1427 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1428 * @from: Previous RIO device found in search, or %NULL for new search
1430 * Iterates through the list of known RIO devices. If a RIO device is
1431 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1432 * count to the device is incrememted and a pointer to its device
1433 * structure is returned. Otherwise, %NULL is returned. A new search
1434 * is initiated by passing %NULL to the @from argument. Otherwise, if
1435 * @from is not %NULL, searches continue from next device on the global
1436 * list. The reference count for @from is always decremented if it is
1437 * not %NULL.
1439 struct rio_dev *rio_get_asm(u16 vid, u16 did,
1440 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1442 struct list_head *n;
1443 struct rio_dev *rdev;
1445 WARN_ON(in_interrupt());
1446 spin_lock(&rio_global_list_lock);
1447 n = from ? from->global_list.next : rio_devices.next;
1449 while (n && (n != &rio_devices)) {
1450 rdev = rio_dev_g(n);
1451 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1452 (did == RIO_ANY_ID || rdev->did == did) &&
1453 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1454 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1455 goto exit;
1456 n = n->next;
1458 rdev = NULL;
1459 exit:
1460 rio_dev_put(from);
1461 rdev = rio_dev_get(rdev);
1462 spin_unlock(&rio_global_list_lock);
1463 return rdev;
1467 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1468 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1469 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1470 * @from: Previous RIO device found in search, or %NULL for new search
1472 * Iterates through the list of known RIO devices. If a RIO device is
1473 * found with a matching @vid and @did, the reference count to the
1474 * device is incrememted and a pointer to its device structure is returned.
1475 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1476 * to the @from argument. Otherwise, if @from is not %NULL, searches
1477 * continue from next device on the global list. The reference count for
1478 * @from is always decremented if it is not %NULL.
1480 struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1482 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1486 * rio_std_route_add_entry - Add switch route table entry using standard
1487 * registers defined in RIO specification rev.1.3
1488 * @mport: Master port to issue transaction
1489 * @destid: Destination ID of the device
1490 * @hopcount: Number of switch hops to the device
1491 * @table: routing table ID (global or port-specific)
1492 * @route_destid: destID entry in the RT
1493 * @route_port: destination port for specified destID
1495 static int
1496 rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1497 u16 table, u16 route_destid, u8 route_port)
1499 if (table == RIO_GLOBAL_TABLE) {
1500 rio_mport_write_config_32(mport, destid, hopcount,
1501 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1502 (u32)route_destid);
1503 rio_mport_write_config_32(mport, destid, hopcount,
1504 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1505 (u32)route_port);
1508 udelay(10);
1509 return 0;
1513 * rio_std_route_get_entry - Read switch route table entry (port number)
1514 * associated with specified destID using standard registers defined in RIO
1515 * specification rev.1.3
1516 * @mport: Master port to issue transaction
1517 * @destid: Destination ID of the device
1518 * @hopcount: Number of switch hops to the device
1519 * @table: routing table ID (global or port-specific)
1520 * @route_destid: destID entry in the RT
1521 * @route_port: returned destination port for specified destID
1523 static int
1524 rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1525 u16 table, u16 route_destid, u8 *route_port)
1527 u32 result;
1529 if (table == RIO_GLOBAL_TABLE) {
1530 rio_mport_write_config_32(mport, destid, hopcount,
1531 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1532 rio_mport_read_config_32(mport, destid, hopcount,
1533 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1535 *route_port = (u8)result;
1538 return 0;
1542 * rio_std_route_clr_table - Clear swotch route table using standard registers
1543 * defined in RIO specification rev.1.3.
1544 * @mport: Master port to issue transaction
1545 * @destid: Destination ID of the device
1546 * @hopcount: Number of switch hops to the device
1547 * @table: routing table ID (global or port-specific)
1549 static int
1550 rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1551 u16 table)
1553 u32 max_destid = 0xff;
1554 u32 i, pef, id_inc = 1, ext_cfg = 0;
1555 u32 port_sel = RIO_INVALID_ROUTE;
1557 if (table == RIO_GLOBAL_TABLE) {
1558 rio_mport_read_config_32(mport, destid, hopcount,
1559 RIO_PEF_CAR, &pef);
1561 if (mport->sys_size) {
1562 rio_mport_read_config_32(mport, destid, hopcount,
1563 RIO_SWITCH_RT_LIMIT,
1564 &max_destid);
1565 max_destid &= RIO_RT_MAX_DESTID;
1568 if (pef & RIO_PEF_EXT_RT) {
1569 ext_cfg = 0x80000000;
1570 id_inc = 4;
1571 port_sel = (RIO_INVALID_ROUTE << 24) |
1572 (RIO_INVALID_ROUTE << 16) |
1573 (RIO_INVALID_ROUTE << 8) |
1574 RIO_INVALID_ROUTE;
1577 for (i = 0; i <= max_destid;) {
1578 rio_mport_write_config_32(mport, destid, hopcount,
1579 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1580 ext_cfg | i);
1581 rio_mport_write_config_32(mport, destid, hopcount,
1582 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1583 port_sel);
1584 i += id_inc;
1588 udelay(10);
1589 return 0;
1593 * rio_lock_device - Acquires host device lock for specified device
1594 * @port: Master port to send transaction
1595 * @destid: Destination ID for device/switch
1596 * @hopcount: Hopcount to reach switch
1597 * @wait_ms: Max wait time in msec (0 = no timeout)
1599 * Attepts to acquire host device lock for specified device
1600 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1602 int rio_lock_device(struct rio_mport *port, u16 destid,
1603 u8 hopcount, int wait_ms)
1605 u32 result;
1606 int tcnt = 0;
1608 /* Attempt to acquire device lock */
1609 rio_mport_write_config_32(port, destid, hopcount,
1610 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1611 rio_mport_read_config_32(port, destid, hopcount,
1612 RIO_HOST_DID_LOCK_CSR, &result);
1614 while (result != port->host_deviceid) {
1615 if (wait_ms != 0 && tcnt == wait_ms) {
1616 pr_debug("RIO: timeout when locking device %x:%x\n",
1617 destid, hopcount);
1618 return -EINVAL;
1621 /* Delay a bit */
1622 mdelay(1);
1623 tcnt++;
1624 /* Try to acquire device lock again */
1625 rio_mport_write_config_32(port, destid,
1626 hopcount,
1627 RIO_HOST_DID_LOCK_CSR,
1628 port->host_deviceid);
1629 rio_mport_read_config_32(port, destid,
1630 hopcount,
1631 RIO_HOST_DID_LOCK_CSR, &result);
1634 return 0;
1636 EXPORT_SYMBOL_GPL(rio_lock_device);
1639 * rio_unlock_device - Releases host device lock for specified device
1640 * @port: Master port to send transaction
1641 * @destid: Destination ID for device/switch
1642 * @hopcount: Hopcount to reach switch
1644 * Returns 0 if device lock released or EINVAL if fails.
1646 int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1648 u32 result;
1650 /* Release device lock */
1651 rio_mport_write_config_32(port, destid,
1652 hopcount,
1653 RIO_HOST_DID_LOCK_CSR,
1654 port->host_deviceid);
1655 rio_mport_read_config_32(port, destid, hopcount,
1656 RIO_HOST_DID_LOCK_CSR, &result);
1657 if ((result & 0xffff) != 0xffff) {
1658 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1659 destid, hopcount);
1660 return -EINVAL;
1663 return 0;
1665 EXPORT_SYMBOL_GPL(rio_unlock_device);
1668 * rio_route_add_entry- Add a route entry to a switch routing table
1669 * @rdev: RIO device
1670 * @table: Routing table ID
1671 * @route_destid: Destination ID to be routed
1672 * @route_port: Port number to be routed
1673 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1675 * If available calls the switch specific add_entry() method to add a route
1676 * entry into a switch routing table. Otherwise uses standard RT update method
1677 * as defined by RapidIO specification. A specific routing table can be selected
1678 * using the @table argument if a switch has per port routing tables or
1679 * the standard (or global) table may be used by passing
1680 * %RIO_GLOBAL_TABLE in @table.
1682 * Returns %0 on success or %-EINVAL on failure.
1684 int rio_route_add_entry(struct rio_dev *rdev,
1685 u16 table, u16 route_destid, u8 route_port, int lock)
1687 int rc = -EINVAL;
1688 struct rio_switch_ops *ops = rdev->rswitch->ops;
1690 if (lock) {
1691 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1692 rdev->hopcount, 1000);
1693 if (rc)
1694 return rc;
1697 spin_lock(&rdev->rswitch->lock);
1699 if (ops == NULL || ops->add_entry == NULL) {
1700 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1701 rdev->hopcount, table,
1702 route_destid, route_port);
1703 } else if (try_module_get(ops->owner)) {
1704 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1705 rdev->hopcount, table, route_destid,
1706 route_port);
1707 module_put(ops->owner);
1710 spin_unlock(&rdev->rswitch->lock);
1712 if (lock)
1713 rio_unlock_device(rdev->net->hport, rdev->destid,
1714 rdev->hopcount);
1716 return rc;
1718 EXPORT_SYMBOL_GPL(rio_route_add_entry);
1721 * rio_route_get_entry- Read an entry from a switch routing table
1722 * @rdev: RIO device
1723 * @table: Routing table ID
1724 * @route_destid: Destination ID to be routed
1725 * @route_port: Pointer to read port number into
1726 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1728 * If available calls the switch specific get_entry() method to fetch a route
1729 * entry from a switch routing table. Otherwise uses standard RT read method
1730 * as defined by RapidIO specification. A specific routing table can be selected
1731 * using the @table argument if a switch has per port routing tables or
1732 * the standard (or global) table may be used by passing
1733 * %RIO_GLOBAL_TABLE in @table.
1735 * Returns %0 on success or %-EINVAL on failure.
1737 int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1738 u16 route_destid, u8 *route_port, int lock)
1740 int rc = -EINVAL;
1741 struct rio_switch_ops *ops = rdev->rswitch->ops;
1743 if (lock) {
1744 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1745 rdev->hopcount, 1000);
1746 if (rc)
1747 return rc;
1750 spin_lock(&rdev->rswitch->lock);
1752 if (ops == NULL || ops->get_entry == NULL) {
1753 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1754 rdev->hopcount, table,
1755 route_destid, route_port);
1756 } else if (try_module_get(ops->owner)) {
1757 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1758 rdev->hopcount, table, route_destid,
1759 route_port);
1760 module_put(ops->owner);
1763 spin_unlock(&rdev->rswitch->lock);
1765 if (lock)
1766 rio_unlock_device(rdev->net->hport, rdev->destid,
1767 rdev->hopcount);
1768 return rc;
1770 EXPORT_SYMBOL_GPL(rio_route_get_entry);
1773 * rio_route_clr_table - Clear a switch routing table
1774 * @rdev: RIO device
1775 * @table: Routing table ID
1776 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1778 * If available calls the switch specific clr_table() method to clear a switch
1779 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1780 * specification. A specific routing table can be selected using the @table
1781 * argument if a switch has per port routing tables or the standard (or global)
1782 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1784 * Returns %0 on success or %-EINVAL on failure.
1786 int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1788 int rc = -EINVAL;
1789 struct rio_switch_ops *ops = rdev->rswitch->ops;
1791 if (lock) {
1792 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1793 rdev->hopcount, 1000);
1794 if (rc)
1795 return rc;
1798 spin_lock(&rdev->rswitch->lock);
1800 if (ops == NULL || ops->clr_table == NULL) {
1801 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1802 rdev->hopcount, table);
1803 } else if (try_module_get(ops->owner)) {
1804 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1805 rdev->hopcount, table);
1807 module_put(ops->owner);
1810 spin_unlock(&rdev->rswitch->lock);
1812 if (lock)
1813 rio_unlock_device(rdev->net->hport, rdev->destid,
1814 rdev->hopcount);
1816 return rc;
1818 EXPORT_SYMBOL_GPL(rio_route_clr_table);
1820 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1822 static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1824 struct rio_mport *mport = arg;
1826 /* Check that DMA device belongs to the right MPORT */
1827 return mport == container_of(chan->device, struct rio_mport, dma);
1831 * rio_request_mport_dma - request RapidIO capable DMA channel associated
1832 * with specified local RapidIO mport device.
1833 * @mport: RIO mport to perform DMA data transfers
1835 * Returns pointer to allocated DMA channel or NULL if failed.
1837 struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1839 dma_cap_mask_t mask;
1841 dma_cap_zero(mask);
1842 dma_cap_set(DMA_SLAVE, mask);
1843 return dma_request_channel(mask, rio_chan_filter, mport);
1845 EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1848 * rio_request_dma - request RapidIO capable DMA channel that supports
1849 * specified target RapidIO device.
1850 * @rdev: RIO device associated with DMA transfer
1852 * Returns pointer to allocated DMA channel or NULL if failed.
1854 struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1856 return rio_request_mport_dma(rdev->net->hport);
1858 EXPORT_SYMBOL_GPL(rio_request_dma);
1861 * rio_release_dma - release specified DMA channel
1862 * @dchan: DMA channel to release
1864 void rio_release_dma(struct dma_chan *dchan)
1866 dma_release_channel(dchan);
1868 EXPORT_SYMBOL_GPL(rio_release_dma);
1871 * rio_dma_prep_xfer - RapidIO specific wrapper
1872 * for device_prep_slave_sg callback defined by DMAENGINE.
1873 * @dchan: DMA channel to configure
1874 * @destid: target RapidIO device destination ID
1875 * @data: RIO specific data descriptor
1876 * @direction: DMA data transfer direction (TO or FROM the device)
1877 * @flags: dmaengine defined flags
1879 * Initializes RapidIO capable DMA channel for the specified data transfer.
1880 * Uses DMA channel private extension to pass information related to remote
1881 * target RIO device.
1883 * Returns: pointer to DMA transaction descriptor if successful,
1884 * error-valued pointer or NULL if failed.
1886 struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1887 u16 destid, struct rio_dma_data *data,
1888 enum dma_transfer_direction direction, unsigned long flags)
1890 struct rio_dma_ext rio_ext;
1892 if (dchan->device->device_prep_slave_sg == NULL) {
1893 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1894 return NULL;
1897 rio_ext.destid = destid;
1898 rio_ext.rio_addr_u = data->rio_addr_u;
1899 rio_ext.rio_addr = data->rio_addr;
1900 rio_ext.wr_type = data->wr_type;
1902 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1903 direction, flags, &rio_ext);
1905 EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1908 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1909 * for device_prep_slave_sg callback defined by DMAENGINE.
1910 * @rdev: RIO device control structure
1911 * @dchan: DMA channel to configure
1912 * @data: RIO specific data descriptor
1913 * @direction: DMA data transfer direction (TO or FROM the device)
1914 * @flags: dmaengine defined flags
1916 * Initializes RapidIO capable DMA channel for the specified data transfer.
1917 * Uses DMA channel private extension to pass information related to remote
1918 * target RIO device.
1920 * Returns: pointer to DMA transaction descriptor if successful,
1921 * error-valued pointer or NULL if failed.
1923 struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1924 struct dma_chan *dchan, struct rio_dma_data *data,
1925 enum dma_transfer_direction direction, unsigned long flags)
1927 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
1929 EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1931 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1934 * rio_find_mport - find RIO mport by its ID
1935 * @mport_id: number (ID) of mport device
1937 * Given a RIO mport number, the desired mport is located
1938 * in the global list of mports. If the mport is found, a pointer to its
1939 * data structure is returned. If no mport is found, %NULL is returned.
1941 struct rio_mport *rio_find_mport(int mport_id)
1943 struct rio_mport *port;
1945 mutex_lock(&rio_mport_list_lock);
1946 list_for_each_entry(port, &rio_mports, node) {
1947 if (port->id == mport_id)
1948 goto found;
1950 port = NULL;
1951 found:
1952 mutex_unlock(&rio_mport_list_lock);
1954 return port;
1958 * rio_register_scan - enumeration/discovery method registration interface
1959 * @mport_id: mport device ID for which fabric scan routine has to be set
1960 * (RIO_MPORT_ANY = set for all available mports)
1961 * @scan_ops: enumeration/discovery operations structure
1963 * Registers enumeration/discovery operations with RapidIO subsystem and
1964 * attaches it to the specified mport device (or all available mports
1965 * if RIO_MPORT_ANY is specified).
1967 * Returns error if the mport already has an enumerator attached to it.
1968 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
1970 int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1972 struct rio_mport *port;
1973 struct rio_scan_node *scan;
1974 int rc = 0;
1976 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1978 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1979 !scan_ops)
1980 return -EINVAL;
1982 mutex_lock(&rio_mport_list_lock);
1985 * Check if there is another enumerator already registered for
1986 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1987 * for the same mport ID are not supported.
1989 list_for_each_entry(scan, &rio_scans, node) {
1990 if (scan->mport_id == mport_id) {
1991 rc = -EBUSY;
1992 goto err_out;
1997 * Allocate and initialize new scan registration node.
1999 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
2000 if (!scan) {
2001 rc = -ENOMEM;
2002 goto err_out;
2005 scan->mport_id = mport_id;
2006 scan->ops = scan_ops;
2009 * Traverse the list of registered mports to attach this new scan.
2011 * The new scan with matching mport ID overrides any previously attached
2012 * scan assuming that old scan (if any) is the default one (based on the
2013 * enumerator registration check above).
2014 * If the new scan is the global one, it will be attached only to mports
2015 * that do not have their own individual operations already attached.
2017 list_for_each_entry(port, &rio_mports, node) {
2018 if (port->id == mport_id) {
2019 port->nscan = scan_ops;
2020 break;
2021 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
2022 port->nscan = scan_ops;
2025 list_add_tail(&scan->node, &rio_scans);
2027 err_out:
2028 mutex_unlock(&rio_mport_list_lock);
2030 return rc;
2032 EXPORT_SYMBOL_GPL(rio_register_scan);
2035 * rio_unregister_scan - removes enumeration/discovery method from mport
2036 * @mport_id: mport device ID for which fabric scan routine has to be
2037 * unregistered (RIO_MPORT_ANY = apply to all mports that use
2038 * the specified scan_ops)
2039 * @scan_ops: enumeration/discovery operations structure
2041 * Removes enumeration or discovery method assigned to the specified mport
2042 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
2043 * all mports that have them attached.
2045 int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
2047 struct rio_mport *port;
2048 struct rio_scan_node *scan;
2050 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
2052 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
2053 return -EINVAL;
2055 mutex_lock(&rio_mport_list_lock);
2057 list_for_each_entry(port, &rio_mports, node)
2058 if (port->id == mport_id ||
2059 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
2060 port->nscan = NULL;
2062 list_for_each_entry(scan, &rio_scans, node) {
2063 if (scan->mport_id == mport_id) {
2064 list_del(&scan->node);
2065 kfree(scan);
2066 break;
2070 mutex_unlock(&rio_mport_list_lock);
2072 return 0;
2074 EXPORT_SYMBOL_GPL(rio_unregister_scan);
2077 * rio_mport_scan - execute enumeration/discovery on the specified mport
2078 * @mport_id: number (ID) of mport device
2080 int rio_mport_scan(int mport_id)
2082 struct rio_mport *port = NULL;
2083 int rc;
2085 mutex_lock(&rio_mport_list_lock);
2086 list_for_each_entry(port, &rio_mports, node) {
2087 if (port->id == mport_id)
2088 goto found;
2090 mutex_unlock(&rio_mport_list_lock);
2091 return -ENODEV;
2092 found:
2093 if (!port->nscan) {
2094 mutex_unlock(&rio_mport_list_lock);
2095 return -EINVAL;
2098 if (!try_module_get(port->nscan->owner)) {
2099 mutex_unlock(&rio_mport_list_lock);
2100 return -ENODEV;
2103 mutex_unlock(&rio_mport_list_lock);
2105 if (port->host_deviceid >= 0)
2106 rc = port->nscan->enumerate(port, 0);
2107 else
2108 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
2110 module_put(port->nscan->owner);
2111 return rc;
2114 static void rio_fixup_device(struct rio_dev *dev)
2118 static int rio_init(void)
2120 struct rio_dev *dev = NULL;
2122 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
2123 rio_fixup_device(dev);
2125 return 0;
2128 static struct workqueue_struct *rio_wq;
2130 struct rio_disc_work {
2131 struct work_struct work;
2132 struct rio_mport *mport;
2135 static void disc_work_handler(struct work_struct *_work)
2137 struct rio_disc_work *work;
2139 work = container_of(_work, struct rio_disc_work, work);
2140 pr_debug("RIO: discovery work for mport %d %s\n",
2141 work->mport->id, work->mport->name);
2142 if (try_module_get(work->mport->nscan->owner)) {
2143 work->mport->nscan->discover(work->mport, 0);
2144 module_put(work->mport->nscan->owner);
2148 int rio_init_mports(void)
2150 struct rio_mport *port;
2151 struct rio_disc_work *work;
2152 int n = 0;
2154 if (!next_portid)
2155 return -ENODEV;
2158 * First, run enumerations and check if we need to perform discovery
2159 * on any of the registered mports.
2161 mutex_lock(&rio_mport_list_lock);
2162 list_for_each_entry(port, &rio_mports, node) {
2163 if (port->host_deviceid >= 0) {
2164 if (port->nscan && try_module_get(port->nscan->owner)) {
2165 port->nscan->enumerate(port, 0);
2166 module_put(port->nscan->owner);
2168 } else
2169 n++;
2171 mutex_unlock(&rio_mport_list_lock);
2173 if (!n)
2174 goto no_disc;
2177 * If we have mports that require discovery schedule a discovery work
2178 * for each of them. If the code below fails to allocate needed
2179 * resources, exit without error to keep results of enumeration
2180 * process (if any).
2181 * TODO: Implement restart of discovery process for all or
2182 * individual discovering mports.
2184 rio_wq = alloc_workqueue("riodisc", 0, 0);
2185 if (!rio_wq) {
2186 pr_err("RIO: unable allocate rio_wq\n");
2187 goto no_disc;
2190 work = kcalloc(n, sizeof *work, GFP_KERNEL);
2191 if (!work) {
2192 pr_err("RIO: no memory for work struct\n");
2193 destroy_workqueue(rio_wq);
2194 goto no_disc;
2197 n = 0;
2198 mutex_lock(&rio_mport_list_lock);
2199 list_for_each_entry(port, &rio_mports, node) {
2200 if (port->host_deviceid < 0 && port->nscan) {
2201 work[n].mport = port;
2202 INIT_WORK(&work[n].work, disc_work_handler);
2203 queue_work(rio_wq, &work[n].work);
2204 n++;
2208 flush_workqueue(rio_wq);
2209 mutex_unlock(&rio_mport_list_lock);
2210 pr_debug("RIO: destroy discovery workqueue\n");
2211 destroy_workqueue(rio_wq);
2212 kfree(work);
2214 no_disc:
2215 rio_init();
2217 return 0;
2220 static int rio_get_hdid(int index)
2222 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
2223 return -1;
2225 return hdid[index];
2228 int rio_mport_initialize(struct rio_mport *mport)
2230 if (next_portid >= RIO_MAX_MPORTS) {
2231 pr_err("RIO: reached specified max number of mports\n");
2232 return -ENODEV;
2235 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2236 mport->id = next_portid++;
2237 mport->host_deviceid = rio_get_hdid(mport->id);
2238 mport->nscan = NULL;
2239 mutex_init(&mport->lock);
2240 mport->pwe_refcnt = 0;
2241 INIT_LIST_HEAD(&mport->pwrites);
2243 return 0;
2245 EXPORT_SYMBOL_GPL(rio_mport_initialize);
2247 int rio_register_mport(struct rio_mport *port)
2249 struct rio_scan_node *scan = NULL;
2250 int res = 0;
2252 mutex_lock(&rio_mport_list_lock);
2255 * Check if there are any registered enumeration/discovery operations
2256 * that have to be attached to the added mport.
2258 list_for_each_entry(scan, &rio_scans, node) {
2259 if (port->id == scan->mport_id ||
2260 scan->mport_id == RIO_MPORT_ANY) {
2261 port->nscan = scan->ops;
2262 if (port->id == scan->mport_id)
2263 break;
2267 list_add_tail(&port->node, &rio_mports);
2268 mutex_unlock(&rio_mport_list_lock);
2270 dev_set_name(&port->dev, "rapidio%d", port->id);
2271 port->dev.class = &rio_mport_class;
2272 atomic_set(&port->state, RIO_DEVICE_RUNNING);
2274 res = device_register(&port->dev);
2275 if (res)
2276 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2277 port->id, res);
2278 else
2279 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2281 return res;
2283 EXPORT_SYMBOL_GPL(rio_register_mport);
2285 static int rio_mport_cleanup_callback(struct device *dev, void *data)
2287 struct rio_dev *rdev = to_rio_dev(dev);
2289 if (dev->bus == &rio_bus_type)
2290 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2291 return 0;
2294 static int rio_net_remove_children(struct rio_net *net)
2297 * Unregister all RapidIO devices residing on this net (this will
2298 * invoke notification of registered subsystem interfaces as well).
2300 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2301 return 0;
2304 int rio_unregister_mport(struct rio_mport *port)
2306 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2308 /* Transition mport to the SHUTDOWN state */
2309 if (atomic_cmpxchg(&port->state,
2310 RIO_DEVICE_RUNNING,
2311 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2312 pr_err("RIO: %s unexpected state transition for mport %s\n",
2313 __func__, port->name);
2316 if (port->net && port->net->hport == port) {
2317 rio_net_remove_children(port->net);
2318 rio_free_net(port->net);
2322 * Unregister all RapidIO devices attached to this mport (this will
2323 * invoke notification of registered subsystem interfaces as well).
2325 mutex_lock(&rio_mport_list_lock);
2326 list_del(&port->node);
2327 mutex_unlock(&rio_mport_list_lock);
2328 device_unregister(&port->dev);
2330 return 0;
2332 EXPORT_SYMBOL_GPL(rio_unregister_mport);
2334 EXPORT_SYMBOL_GPL(rio_local_get_device_id);
2335 EXPORT_SYMBOL_GPL(rio_get_device);
2336 EXPORT_SYMBOL_GPL(rio_get_asm);
2337 EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
2338 EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
2339 EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
2340 EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
2341 EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
2342 EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
2343 EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
2344 EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
2345 EXPORT_SYMBOL_GPL(rio_init_mports);