1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RapidIO interconnect services
4 * (RapidIO Interconnect Specification, http://www.rapidio.org)
6 * Copyright 2005 MontaVista Software, Inc.
7 * Matt Porter <mporter@kernel.crashing.org>
9 * Copyright 2009 - 2013 Integrated Device Technology, Inc.
10 * Alex Bounine <alexandre.bounine@idt.com>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/rio.h>
19 #include <linux/rio_drv.h>
20 #include <linux/rio_ids.h>
21 #include <linux/rio_regs.h>
22 #include <linux/module.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
30 * struct rio_pwrite - RIO portwrite event
31 * @node: Node in list of doorbell events
32 * @pwcback: Doorbell event callback
33 * @context: Handler specific context to pass on event
36 struct list_head node;
38 int (*pwcback)(struct rio_mport *mport, void *context,
39 union rio_pw_msg *msg, int step);
43 MODULE_DESCRIPTION("RapidIO Subsystem Core");
44 MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
45 MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
46 MODULE_LICENSE("GPL");
48 static int hdid[RIO_MAX_MPORTS];
50 module_param_array(hdid, int, &ids_num, 0);
51 MODULE_PARM_DESC(hdid,
52 "Destination ID assignment to local RapidIO controllers");
54 static LIST_HEAD(rio_devices);
55 static LIST_HEAD(rio_nets);
56 static DEFINE_SPINLOCK(rio_global_list_lock);
58 static LIST_HEAD(rio_mports);
59 static LIST_HEAD(rio_scans);
60 static DEFINE_MUTEX(rio_mport_list_lock);
61 static unsigned char next_portid;
62 static DEFINE_SPINLOCK(rio_mmap_lock);
65 * rio_local_get_device_id - Get the base/extended device id for a port
66 * @port: RIO master port from which to get the deviceid
68 * Reads the base/extended device id from the local device
69 * implementing the master port. Returns the 8/16-bit device
72 u16 rio_local_get_device_id(struct rio_mport *port)
76 rio_local_read_config_32(port, RIO_DID_CSR, &result);
78 return (RIO_GET_DID(port->sys_size, result));
80 EXPORT_SYMBOL_GPL(rio_local_get_device_id);
83 * rio_query_mport - Query mport device attributes
84 * @port: mport device to query
85 * @mport_attr: mport attributes data structure
87 * Returns attributes of specified mport through the
88 * pointer to attributes data structure.
90 int rio_query_mport(struct rio_mport *port,
91 struct rio_mport_attr *mport_attr)
93 if (!port->ops->query_mport)
95 return port->ops->query_mport(port, mport_attr);
97 EXPORT_SYMBOL(rio_query_mport);
100 * rio_alloc_net- Allocate and initialize a new RIO network data structure
101 * @mport: Master port associated with the RIO network
103 * Allocates a RIO network structure, initializes per-network
104 * list heads, and adds the associated master port to the
105 * network list of associated master ports. Returns a
106 * RIO network pointer on success or %NULL on failure.
108 struct rio_net *rio_alloc_net(struct rio_mport *mport)
110 struct rio_net *net = kzalloc(sizeof(*net), GFP_KERNEL);
113 INIT_LIST_HEAD(&net->node);
114 INIT_LIST_HEAD(&net->devices);
115 INIT_LIST_HEAD(&net->switches);
116 INIT_LIST_HEAD(&net->mports);
121 EXPORT_SYMBOL_GPL(rio_alloc_net);
123 int rio_add_net(struct rio_net *net)
127 err = device_register(&net->dev);
130 spin_lock(&rio_global_list_lock);
131 list_add_tail(&net->node, &rio_nets);
132 spin_unlock(&rio_global_list_lock);
136 EXPORT_SYMBOL_GPL(rio_add_net);
138 void rio_free_net(struct rio_net *net)
140 spin_lock(&rio_global_list_lock);
141 if (!list_empty(&net->node))
142 list_del(&net->node);
143 spin_unlock(&rio_global_list_lock);
146 device_unregister(&net->dev);
148 EXPORT_SYMBOL_GPL(rio_free_net);
151 * rio_local_set_device_id - Set the base/extended device id for a port
152 * @port: RIO master port
153 * @did: Device ID value to be written
155 * Writes the base/extended device id from a device.
157 void rio_local_set_device_id(struct rio_mport *port, u16 did)
159 rio_local_write_config_32(port, RIO_DID_CSR,
160 RIO_SET_DID(port->sys_size, did));
162 EXPORT_SYMBOL_GPL(rio_local_set_device_id);
165 * rio_add_device- Adds a RIO device to the device model
168 * Adds the RIO device to the global device list and adds the RIO
169 * device to the RIO device list. Creates the generic sysfs nodes
172 int rio_add_device(struct rio_dev *rdev)
176 atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
177 err = device_register(&rdev->dev);
181 spin_lock(&rio_global_list_lock);
182 list_add_tail(&rdev->global_list, &rio_devices);
184 list_add_tail(&rdev->net_list, &rdev->net->devices);
185 if (rdev->pef & RIO_PEF_SWITCH)
186 list_add_tail(&rdev->rswitch->node,
187 &rdev->net->switches);
189 spin_unlock(&rio_global_list_lock);
193 EXPORT_SYMBOL_GPL(rio_add_device);
196 * rio_del_device - removes a RIO device from the device model
198 * @state: device state to set during removal process
200 * Removes the RIO device to the kernel device list and subsystem's device list.
201 * Clears sysfs entries for the removed device.
203 void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
205 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
206 atomic_set(&rdev->state, state);
207 spin_lock(&rio_global_list_lock);
208 list_del(&rdev->global_list);
210 list_del(&rdev->net_list);
211 if (rdev->pef & RIO_PEF_SWITCH) {
212 list_del(&rdev->rswitch->node);
213 kfree(rdev->rswitch->route_table);
216 spin_unlock(&rio_global_list_lock);
217 device_unregister(&rdev->dev);
219 EXPORT_SYMBOL_GPL(rio_del_device);
222 * rio_request_inb_mbox - request inbound mailbox service
223 * @mport: RIO master port from which to allocate the mailbox resource
224 * @dev_id: Device specific pointer to pass on event
225 * @mbox: Mailbox number to claim
226 * @entries: Number of entries in inbound mailbox queue
227 * @minb: Callback to execute when inbound message is received
229 * Requests ownership of an inbound mailbox resource and binds
230 * a callback function to the resource. Returns %0 on success.
232 int rio_request_inb_mbox(struct rio_mport *mport,
236 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
240 struct resource *res;
242 if (!mport->ops->open_inb_mbox)
245 res = kzalloc(sizeof(*res), GFP_KERNEL);
247 rio_init_mbox_res(res, mbox, mbox);
249 /* Make sure this mailbox isn't in use */
250 rc = request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
257 mport->inb_msg[mbox].res = res;
259 /* Hook the inbound message callback */
260 mport->inb_msg[mbox].mcback = minb;
262 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
264 mport->inb_msg[mbox].mcback = NULL;
265 mport->inb_msg[mbox].res = NULL;
266 release_resource(res);
275 EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
278 * rio_release_inb_mbox - release inbound mailbox message service
279 * @mport: RIO master port from which to release the mailbox resource
280 * @mbox: Mailbox number to release
282 * Releases ownership of an inbound mailbox resource. Returns 0
283 * if the request has been satisfied.
285 int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
289 if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res)
292 mport->ops->close_inb_mbox(mport, mbox);
293 mport->inb_msg[mbox].mcback = NULL;
295 rc = release_resource(mport->inb_msg[mbox].res);
299 kfree(mport->inb_msg[mbox].res);
300 mport->inb_msg[mbox].res = NULL;
304 EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
307 * rio_request_outb_mbox - request outbound mailbox service
308 * @mport: RIO master port from which to allocate the mailbox resource
309 * @dev_id: Device specific pointer to pass on event
310 * @mbox: Mailbox number to claim
311 * @entries: Number of entries in outbound mailbox queue
312 * @moutb: Callback to execute when outbound message is sent
314 * Requests ownership of an outbound mailbox resource and binds
315 * a callback function to the resource. Returns 0 on success.
317 int rio_request_outb_mbox(struct rio_mport *mport,
321 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
324 struct resource *res;
326 if (!mport->ops->open_outb_mbox)
329 res = kzalloc(sizeof(*res), GFP_KERNEL);
331 rio_init_mbox_res(res, mbox, mbox);
333 /* Make sure this outbound mailbox isn't in use */
334 rc = request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
341 mport->outb_msg[mbox].res = res;
343 /* Hook the inbound message callback */
344 mport->outb_msg[mbox].mcback = moutb;
346 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
348 mport->outb_msg[mbox].mcback = NULL;
349 mport->outb_msg[mbox].res = NULL;
350 release_resource(res);
359 EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
362 * rio_release_outb_mbox - release outbound mailbox message service
363 * @mport: RIO master port from which to release the mailbox resource
364 * @mbox: Mailbox number to release
366 * Releases ownership of an inbound mailbox resource. Returns 0
367 * if the request has been satisfied.
369 int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
373 if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res)
376 mport->ops->close_outb_mbox(mport, mbox);
377 mport->outb_msg[mbox].mcback = NULL;
379 rc = release_resource(mport->outb_msg[mbox].res);
383 kfree(mport->outb_msg[mbox].res);
384 mport->outb_msg[mbox].res = NULL;
388 EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
391 * rio_setup_inb_dbell - bind inbound doorbell callback
392 * @mport: RIO master port to bind the doorbell callback
393 * @dev_id: Device specific pointer to pass on event
394 * @res: Doorbell message resource
395 * @dinb: Callback to execute when doorbell is received
397 * Adds a doorbell resource/callback pair into a port's
398 * doorbell event list. Returns 0 if the request has been
402 rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
403 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
406 struct rio_dbell *dbell = kmalloc(sizeof(*dbell), GFP_KERNEL);
413 dbell->dev_id = dev_id;
415 mutex_lock(&mport->lock);
416 list_add_tail(&dbell->node, &mport->dbells);
417 mutex_unlock(&mport->lock);
422 * rio_request_inb_dbell - request inbound doorbell message service
423 * @mport: RIO master port from which to allocate the doorbell resource
424 * @dev_id: Device specific pointer to pass on event
425 * @start: Doorbell info range start
426 * @end: Doorbell info range end
427 * @dinb: Callback to execute when doorbell is received
429 * Requests ownership of an inbound doorbell resource and binds
430 * a callback function to the resource. Returns 0 if the request
431 * has been satisfied.
433 int rio_request_inb_dbell(struct rio_mport *mport,
437 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
441 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
444 rio_init_dbell_res(res, start, end);
446 /* Make sure these doorbells aren't in use */
447 rc = request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
454 /* Hook the doorbell callback */
455 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
462 EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
465 * rio_release_inb_dbell - release inbound doorbell message service
466 * @mport: RIO master port from which to release the doorbell resource
467 * @start: Doorbell info range start
468 * @end: Doorbell info range end
470 * Releases ownership of an inbound doorbell resource and removes
471 * callback from the doorbell event list. Returns 0 if the request
472 * has been satisfied.
474 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
476 int rc = 0, found = 0;
477 struct rio_dbell *dbell;
479 mutex_lock(&mport->lock);
480 list_for_each_entry(dbell, &mport->dbells, node) {
481 if ((dbell->res->start == start) && (dbell->res->end == end)) {
482 list_del(&dbell->node);
487 mutex_unlock(&mport->lock);
489 /* If we can't find an exact match, fail */
495 /* Release the doorbell resource */
496 rc = release_resource(dbell->res);
498 /* Free the doorbell event */
504 EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
507 * rio_request_outb_dbell - request outbound doorbell message range
508 * @rdev: RIO device from which to allocate the doorbell resource
509 * @start: Doorbell message range start
510 * @end: Doorbell message range end
512 * Requests ownership of a doorbell message range. Returns a resource
513 * if the request has been satisfied or %NULL on failure.
515 struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
518 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
521 rio_init_dbell_res(res, start, end);
523 /* Make sure these doorbells aren't in use */
524 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
533 EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
536 * rio_release_outb_dbell - release outbound doorbell message range
537 * @rdev: RIO device from which to release the doorbell resource
538 * @res: Doorbell resource to be freed
540 * Releases ownership of a doorbell message range. Returns 0 if the
541 * request has been satisfied.
543 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
545 int rc = release_resource(res);
551 EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
554 * rio_add_mport_pw_handler - add port-write message handler into the list
555 * of mport specific pw handlers
556 * @mport: RIO master port to bind the portwrite callback
557 * @context: Handler specific context to pass on event
558 * @pwcback: Callback to execute when portwrite is received
560 * Returns 0 if the request has been satisfied.
562 int rio_add_mport_pw_handler(struct rio_mport *mport, void *context,
563 int (*pwcback)(struct rio_mport *mport,
564 void *context, union rio_pw_msg *msg, int step))
566 struct rio_pwrite *pwrite = kzalloc(sizeof(*pwrite), GFP_KERNEL);
571 pwrite->pwcback = pwcback;
572 pwrite->context = context;
573 mutex_lock(&mport->lock);
574 list_add_tail(&pwrite->node, &mport->pwrites);
575 mutex_unlock(&mport->lock);
578 EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler);
581 * rio_del_mport_pw_handler - remove port-write message handler from the list
582 * of mport specific pw handlers
583 * @mport: RIO master port to bind the portwrite callback
584 * @context: Registered handler specific context to pass on event
585 * @pwcback: Registered callback function
587 * Returns 0 if the request has been satisfied.
589 int rio_del_mport_pw_handler(struct rio_mport *mport, void *context,
590 int (*pwcback)(struct rio_mport *mport,
591 void *context, union rio_pw_msg *msg, int step))
594 struct rio_pwrite *pwrite;
596 mutex_lock(&mport->lock);
597 list_for_each_entry(pwrite, &mport->pwrites, node) {
598 if (pwrite->pwcback == pwcback && pwrite->context == context) {
599 list_del(&pwrite->node);
605 mutex_unlock(&mport->lock);
609 EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler);
612 * rio_request_inb_pwrite - request inbound port-write message service for
613 * specific RapidIO device
614 * @rdev: RIO device to which register inbound port-write callback routine
615 * @pwcback: Callback routine to execute when port-write is received
617 * Binds a port-write callback function to the RapidIO device.
618 * Returns 0 if the request has been satisfied.
620 int rio_request_inb_pwrite(struct rio_dev *rdev,
621 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
625 spin_lock(&rio_global_list_lock);
629 rdev->pwcback = pwcback;
631 spin_unlock(&rio_global_list_lock);
634 EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
637 * rio_release_inb_pwrite - release inbound port-write message service
638 * associated with specific RapidIO device
639 * @rdev: RIO device which registered for inbound port-write callback
641 * Removes callback from the rio_dev structure. Returns 0 if the request
642 * has been satisfied.
644 int rio_release_inb_pwrite(struct rio_dev *rdev)
648 spin_lock(&rio_global_list_lock);
650 rdev->pwcback = NULL;
654 spin_unlock(&rio_global_list_lock);
657 EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
660 * rio_pw_enable - Enables/disables port-write handling by a master port
661 * @mport: Master port associated with port-write handling
662 * @enable: 1=enable, 0=disable
664 void rio_pw_enable(struct rio_mport *mport, int enable)
666 if (mport->ops->pwenable) {
667 mutex_lock(&mport->lock);
669 if ((enable && ++mport->pwe_refcnt == 1) ||
670 (!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
671 mport->ops->pwenable(mport, enable);
672 mutex_unlock(&mport->lock);
675 EXPORT_SYMBOL_GPL(rio_pw_enable);
678 * rio_map_inb_region -- Map inbound memory region.
679 * @mport: Master port.
680 * @local: physical address of memory region to be mapped
681 * @rbase: RIO base address assigned to this window
682 * @size: Size of the memory region
683 * @rflags: Flags for mapping.
685 * Return: 0 -- Success.
687 * This function will create the mapping from RIO space to local memory.
689 int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
690 u64 rbase, u32 size, u32 rflags)
695 if (!mport->ops->map_inb)
697 spin_lock_irqsave(&rio_mmap_lock, flags);
698 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
699 spin_unlock_irqrestore(&rio_mmap_lock, flags);
702 EXPORT_SYMBOL_GPL(rio_map_inb_region);
705 * rio_unmap_inb_region -- Unmap the inbound memory region
706 * @mport: Master port
707 * @lstart: physical address of memory region to be unmapped
709 void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
712 if (!mport->ops->unmap_inb)
714 spin_lock_irqsave(&rio_mmap_lock, flags);
715 mport->ops->unmap_inb(mport, lstart);
716 spin_unlock_irqrestore(&rio_mmap_lock, flags);
718 EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
721 * rio_map_outb_region -- Map outbound memory region.
722 * @mport: Master port.
723 * @destid: destination id window points to
724 * @rbase: RIO base address window translates to
725 * @size: Size of the memory region
726 * @rflags: Flags for mapping.
727 * @local: physical address of memory region mapped
729 * Return: 0 -- Success.
731 * This function will create the mapping from RIO space to local memory.
733 int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase,
734 u32 size, u32 rflags, dma_addr_t *local)
739 if (!mport->ops->map_outb)
742 spin_lock_irqsave(&rio_mmap_lock, flags);
743 rc = mport->ops->map_outb(mport, destid, rbase, size,
745 spin_unlock_irqrestore(&rio_mmap_lock, flags);
749 EXPORT_SYMBOL_GPL(rio_map_outb_region);
752 * rio_unmap_outb_region -- Unmap the inbound memory region
753 * @mport: Master port
754 * @destid: destination id mapping points to
755 * @rstart: RIO base address window translates to
757 void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart)
761 if (!mport->ops->unmap_outb)
764 spin_lock_irqsave(&rio_mmap_lock, flags);
765 mport->ops->unmap_outb(mport, destid, rstart);
766 spin_unlock_irqrestore(&rio_mmap_lock, flags);
768 EXPORT_SYMBOL_GPL(rio_unmap_outb_region);
771 * rio_mport_get_physefb - Helper function that returns register offset
772 * for Physical Layer Extended Features Block.
773 * @port: Master port to issue transaction
774 * @local: Indicate a local master port or remote device access
775 * @destid: Destination ID of the device
776 * @hopcount: Number of switch hops to the device
777 * @rmap: pointer to location to store register map type info
780 rio_mport_get_physefb(struct rio_mport *port, int local,
781 u16 destid, u8 hopcount, u32 *rmap)
786 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
788 while (ext_ftr_ptr) {
790 rio_local_read_config_32(port, ext_ftr_ptr,
793 rio_mport_read_config_32(port, destid, hopcount,
794 ext_ftr_ptr, &ftr_header);
796 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
797 switch (ftr_header) {
799 case RIO_EFB_SER_EP_ID:
800 case RIO_EFB_SER_EP_REC_ID:
801 case RIO_EFB_SER_EP_FREE_ID:
802 case RIO_EFB_SER_EP_M1_ID:
803 case RIO_EFB_SER_EP_SW_M1_ID:
804 case RIO_EFB_SER_EPF_M1_ID:
805 case RIO_EFB_SER_EPF_SW_M1_ID:
809 case RIO_EFB_SER_EP_M2_ID:
810 case RIO_EFB_SER_EP_SW_M2_ID:
811 case RIO_EFB_SER_EPF_M2_ID:
812 case RIO_EFB_SER_EPF_SW_M2_ID:
820 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
821 hopcount, ext_ftr_ptr);
826 EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
829 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
830 * @comp_tag: RIO component tag to match
831 * @from: Previous RIO device found in search, or %NULL for new search
833 * Iterates through the list of known RIO devices. If a RIO device is
834 * found with a matching @comp_tag, a pointer to its device
835 * structure is returned. Otherwise, %NULL is returned. A new search
836 * is initiated by passing %NULL to the @from argument. Otherwise, if
837 * @from is not %NULL, searches continue from next device on the global
840 struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
843 struct rio_dev *rdev;
845 spin_lock(&rio_global_list_lock);
846 n = from ? from->global_list.next : rio_devices.next;
848 while (n && (n != &rio_devices)) {
850 if (rdev->comp_tag == comp_tag)
856 spin_unlock(&rio_global_list_lock);
859 EXPORT_SYMBOL_GPL(rio_get_comptag);
862 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
863 * @rdev: Pointer to RIO device control structure
864 * @pnum: Switch port number to set LOCKOUT bit
865 * @lock: Operation : set (=1) or clear (=0)
867 int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
871 rio_read_config_32(rdev,
872 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
875 regval |= RIO_PORT_N_CTL_LOCKOUT;
877 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
879 rio_write_config_32(rdev,
880 RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
884 EXPORT_SYMBOL_GPL(rio_set_port_lockout);
887 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
889 * @port: Master port associated with the RIO network
890 * @local: local=1 select local port otherwise a far device is reached
891 * @destid: Destination ID of the device to check host bit
892 * @hopcount: Number of hops to reach the target
893 * @port_num: Port (-number on switch) to enable on a far end device
895 * Returns 0 or 1 from on General Control Command and Status Register
898 int rio_enable_rx_tx_port(struct rio_mport *port,
899 int local, u16 destid,
900 u8 hopcount, u8 port_num)
902 #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
908 * enable rx input tx output port
910 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
911 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
913 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid,
917 rio_local_read_config_32(port,
918 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap),
921 if (rio_mport_read_config_32(port, destid, hopcount,
922 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
927 regval = regval | RIO_PORT_N_CTL_EN_RX | RIO_PORT_N_CTL_EN_TX;
930 rio_local_write_config_32(port,
931 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), regval);
933 if (rio_mport_write_config_32(port, destid, hopcount,
934 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
941 EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
945 * rio_chk_dev_route - Validate route to the specified device.
946 * @rdev: RIO device failed to respond
947 * @nrdev: Last active device on the route to rdev
948 * @npnum: nrdev's port number on the route to rdev
950 * Follows a route to the specified RIO device to determine the last available
951 * device (and corresponding RIO port) on the route.
954 rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
957 int p_port, rc = -EIO;
958 struct rio_dev *prev = NULL;
960 /* Find switch with failed RIO link */
961 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
962 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
972 p_port = prev->rswitch->route_table[rdev->destid];
974 if (p_port != RIO_INVALID_ROUTE) {
975 pr_debug("RIO: link failed on [%s]-P%d\n",
976 rio_name(prev), p_port);
981 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
987 * rio_mport_chk_dev_access - Validate access to the specified device.
988 * @mport: Master port to send transactions
989 * @destid: Device destination ID in network
990 * @hopcount: Number of hops into the network
993 rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
998 while (rio_mport_read_config_32(mport, destid, hopcount,
999 RIO_DEV_ID_CAR, &tmp)) {
1001 if (i == RIO_MAX_CHK_RETRY)
1008 EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
1011 * rio_chk_dev_access - Validate access to the specified device.
1012 * @rdev: Pointer to RIO device control structure
1014 static int rio_chk_dev_access(struct rio_dev *rdev)
1016 return rio_mport_chk_dev_access(rdev->net->hport,
1017 rdev->destid, rdev->hopcount);
1021 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
1022 * returns link-response (if requested).
1023 * @rdev: RIO devive to issue Input-status command
1024 * @pnum: Device port number to issue the command
1025 * @lnkresp: Response from a link partner
1028 rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
1034 /* Read from link maintenance response register
1035 * to clear valid bit */
1036 rio_read_config_32(rdev,
1037 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
1042 /* Issue Input-status command */
1043 rio_write_config_32(rdev,
1044 RIO_DEV_PORT_N_MNT_REQ_CSR(rdev, pnum),
1045 RIO_MNT_REQ_CMD_IS);
1047 /* Exit if the response is not expected */
1052 while (checkcount--) {
1054 rio_read_config_32(rdev,
1055 RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
1057 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
1067 * rio_clr_err_stopped - Clears port Error-stopped states.
1068 * @rdev: Pointer to RIO device control structure
1069 * @pnum: Switch port number to clear errors
1070 * @err_status: port error status (if 0 reads register from device)
1072 * TODO: Currently this routine is not compatible with recovery process
1073 * specified for idt_gen3 RapidIO switch devices. It has to be reviewed
1074 * to implement universal recovery process that is compatible full range
1075 * off available devices.
1076 * IDT gen3 switch driver now implements HW-specific error handler that
1077 * issues soft port reset to the port to reset ERR_STOP bits and ackIDs.
1079 static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
1081 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
1083 u32 far_ackid, far_linkstat, near_ackid;
1085 if (err_status == 0)
1086 rio_read_config_32(rdev,
1087 RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1090 if (err_status & RIO_PORT_N_ERR_STS_OUT_ES) {
1091 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
1093 * Send a Link-Request/Input-Status control symbol
1095 if (rio_get_input_status(rdev, pnum, ®val)) {
1096 pr_debug("RIO_EM: Input-status response timeout\n");
1100 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
1102 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
1103 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
1104 rio_read_config_32(rdev,
1105 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
1107 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
1108 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
1109 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
1110 " near_ackID=0x%02x\n",
1111 pnum, far_ackid, far_linkstat, near_ackid);
1114 * If required, synchronize ackIDs of near and
1117 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
1118 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
1119 /* Align near outstanding/outbound ackIDs with
1122 rio_write_config_32(rdev,
1123 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
1124 (near_ackid << 24) |
1125 (far_ackid << 8) | far_ackid);
1126 /* Align far outstanding/outbound ackIDs with
1131 pr_debug("RIO_EM: nextdev pointer == NULL\n");
1135 rio_write_config_32(nextdev,
1136 RIO_DEV_PORT_N_ACK_STS_CSR(nextdev,
1137 RIO_GET_PORT_NUM(nextdev->swpinfo)),
1139 (near_ackid << 8) | near_ackid);
1142 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1144 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1147 if ((err_status & RIO_PORT_N_ERR_STS_INP_ES) && nextdev) {
1148 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
1149 rio_get_input_status(nextdev,
1150 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
1153 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
1155 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
1158 return (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1159 RIO_PORT_N_ERR_STS_INP_ES)) ? 1 : 0;
1163 * rio_inb_pwrite_handler - inbound port-write message handler
1164 * @mport: mport device associated with port-write
1165 * @pw_msg: pointer to inbound port-write message
1167 * Processes an inbound port-write message. Returns 0 if the request
1168 * has been satisfied.
1170 int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg)
1172 struct rio_dev *rdev;
1173 u32 err_status, em_perrdet, em_ltlerrdet;
1175 struct rio_pwrite *pwrite;
1181 pr_debug("%s: PW to mport_%d:\n", __func__, mport->id);
1182 for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) {
1183 pr_debug("0x%02x: %08x %08x %08x %08x\n",
1184 i * 4, pw_msg->raw[i], pw_msg->raw[i + 1],
1185 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
1190 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
1192 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
1194 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
1195 __func__, pw_msg->em.comptag);
1198 /* Call a device-specific handler (if it is registered for the device).
1199 * This may be the service for endpoints that send device-specific
1200 * port-write messages. End-point messages expected to be handled
1201 * completely by EP specific device driver.
1202 * For switches rc==0 signals that no standard processing required.
1204 if (rdev && rdev->pwcback) {
1205 rc = rdev->pwcback(rdev, pw_msg, 0);
1210 mutex_lock(&mport->lock);
1211 list_for_each_entry(pwrite, &mport->pwrites, node)
1212 pwrite->pwcback(mport, pwrite->context, pw_msg, 0);
1213 mutex_unlock(&mport->lock);
1219 * FIXME: The code below stays as it was before for now until we decide
1220 * how to do default PW handling in combination with per-mport callbacks
1223 portnum = pw_msg->em.is_port & 0xFF;
1225 /* Check if device and route to it are functional:
1226 * Sometimes devices may send PW message(s) just before being
1227 * powered down (or link being lost).
1229 if (rio_chk_dev_access(rdev)) {
1230 pr_debug("RIO: device access failed - get link partner\n");
1231 /* Scan route to the device and identify failed link.
1232 * This will replace device and port reported in PW message.
1233 * PW message should not be used after this point.
1235 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
1236 pr_err("RIO: Route trace for %s failed\n",
1243 /* For End-point devices processing stops here */
1244 if (!(rdev->pef & RIO_PEF_SWITCH))
1247 if (rdev->phys_efptr == 0) {
1248 pr_err("RIO_PW: Bad switch initialization for %s\n",
1254 * Process the port-write notification from switch
1256 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
1257 rdev->rswitch->ops->em_handle(rdev, portnum);
1259 rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1261 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1263 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
1265 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1266 rdev->rswitch->port_ok |= (1 << portnum);
1267 rio_set_port_lockout(rdev, portnum, 0);
1268 /* Schedule Insertion Service */
1269 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1270 rio_name(rdev), portnum);
1273 /* Clear error-stopped states (if reported).
1274 * Depending on the link partner state, two attempts
1275 * may be needed for successful recovery.
1277 if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
1278 RIO_PORT_N_ERR_STS_INP_ES)) {
1279 if (rio_clr_err_stopped(rdev, portnum, err_status))
1280 rio_clr_err_stopped(rdev, portnum, 0);
1282 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
1284 if (rdev->rswitch->port_ok & (1 << portnum)) {
1285 rdev->rswitch->port_ok &= ~(1 << portnum);
1286 rio_set_port_lockout(rdev, portnum, 1);
1288 if (rdev->phys_rmap == 1) {
1289 rio_write_config_32(rdev,
1290 RIO_DEV_PORT_N_ACK_STS_CSR(rdev, portnum),
1291 RIO_PORT_N_ACK_CLEAR);
1293 rio_write_config_32(rdev,
1294 RIO_DEV_PORT_N_OB_ACK_CSR(rdev, portnum),
1295 RIO_PORT_N_OB_ACK_CLEAR);
1296 rio_write_config_32(rdev,
1297 RIO_DEV_PORT_N_IB_ACK_CSR(rdev, portnum),
1301 /* Schedule Extraction Service */
1302 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1303 rio_name(rdev), portnum);
1307 rio_read_config_32(rdev,
1308 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1310 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1311 portnum, em_perrdet);
1312 /* Clear EM Port N Error Detect CSR */
1313 rio_write_config_32(rdev,
1314 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1317 rio_read_config_32(rdev,
1318 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1320 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1322 /* Clear EM L/T Layer Error Detect CSR */
1323 rio_write_config_32(rdev,
1324 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1327 /* Clear remaining error bits and Port-Write Pending bit */
1328 rio_write_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
1333 EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1336 * rio_mport_get_efb - get pointer to next extended features block
1337 * @port: Master port to issue transaction
1338 * @local: Indicate a local master port or remote device access
1339 * @destid: Destination ID of the device
1340 * @hopcount: Number of switch hops to the device
1341 * @from: Offset of current Extended Feature block header (if 0 starts
1342 * from ExtFeaturePtr)
1345 rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1346 u8 hopcount, u32 from)
1352 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1355 rio_mport_read_config_32(port, destid, hopcount,
1356 RIO_ASM_INFO_CAR, ®_val);
1357 return reg_val & RIO_EXT_FTR_PTR_MASK;
1360 rio_local_read_config_32(port, from, ®_val);
1362 rio_mport_read_config_32(port, destid, hopcount,
1364 return RIO_GET_BLOCK_ID(reg_val);
1367 EXPORT_SYMBOL_GPL(rio_mport_get_efb);
1370 * rio_mport_get_feature - query for devices' extended features
1371 * @port: Master port to issue transaction
1372 * @local: Indicate a local master port or remote device access
1373 * @destid: Destination ID of the device
1374 * @hopcount: Number of switch hops to the device
1375 * @ftr: Extended feature code
1377 * Tell if a device supports a given RapidIO capability.
1378 * Returns the offset of the requested extended feature
1379 * block within the device's RIO configuration space or
1380 * 0 in case the device does not support it.
1383 rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1384 u8 hopcount, int ftr)
1386 u32 asm_info, ext_ftr_ptr, ftr_header;
1389 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1391 rio_mport_read_config_32(port, destid, hopcount,
1392 RIO_ASM_INFO_CAR, &asm_info);
1394 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1396 while (ext_ftr_ptr) {
1398 rio_local_read_config_32(port, ext_ftr_ptr,
1401 rio_mport_read_config_32(port, destid, hopcount,
1402 ext_ftr_ptr, &ftr_header);
1403 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1406 ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header);
1413 EXPORT_SYMBOL_GPL(rio_mport_get_feature);
1416 * rio_std_route_add_entry - Add switch route table entry using standard
1417 * registers defined in RIO specification rev.1.3
1418 * @mport: Master port to issue transaction
1419 * @destid: Destination ID of the device
1420 * @hopcount: Number of switch hops to the device
1421 * @table: routing table ID (global or port-specific)
1422 * @route_destid: destID entry in the RT
1423 * @route_port: destination port for specified destID
1426 rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1427 u16 table, u16 route_destid, u8 route_port)
1429 if (table == RIO_GLOBAL_TABLE) {
1430 rio_mport_write_config_32(mport, destid, hopcount,
1431 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1433 rio_mport_write_config_32(mport, destid, hopcount,
1434 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1443 * rio_std_route_get_entry - Read switch route table entry (port number)
1444 * associated with specified destID using standard registers defined in RIO
1445 * specification rev.1.3
1446 * @mport: Master port to issue transaction
1447 * @destid: Destination ID of the device
1448 * @hopcount: Number of switch hops to the device
1449 * @table: routing table ID (global or port-specific)
1450 * @route_destid: destID entry in the RT
1451 * @route_port: returned destination port for specified destID
1454 rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1455 u16 table, u16 route_destid, u8 *route_port)
1459 if (table == RIO_GLOBAL_TABLE) {
1460 rio_mport_write_config_32(mport, destid, hopcount,
1461 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1462 rio_mport_read_config_32(mport, destid, hopcount,
1463 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1465 *route_port = (u8)result;
1472 * rio_std_route_clr_table - Clear swotch route table using standard registers
1473 * defined in RIO specification rev.1.3.
1474 * @mport: Master port to issue transaction
1475 * @destid: Destination ID of the device
1476 * @hopcount: Number of switch hops to the device
1477 * @table: routing table ID (global or port-specific)
1480 rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1483 u32 max_destid = 0xff;
1484 u32 i, pef, id_inc = 1, ext_cfg = 0;
1485 u32 port_sel = RIO_INVALID_ROUTE;
1487 if (table == RIO_GLOBAL_TABLE) {
1488 rio_mport_read_config_32(mport, destid, hopcount,
1491 if (mport->sys_size) {
1492 rio_mport_read_config_32(mport, destid, hopcount,
1493 RIO_SWITCH_RT_LIMIT,
1495 max_destid &= RIO_RT_MAX_DESTID;
1498 if (pef & RIO_PEF_EXT_RT) {
1499 ext_cfg = 0x80000000;
1501 port_sel = (RIO_INVALID_ROUTE << 24) |
1502 (RIO_INVALID_ROUTE << 16) |
1503 (RIO_INVALID_ROUTE << 8) |
1507 for (i = 0; i <= max_destid;) {
1508 rio_mport_write_config_32(mport, destid, hopcount,
1509 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1511 rio_mport_write_config_32(mport, destid, hopcount,
1512 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1523 * rio_lock_device - Acquires host device lock for specified device
1524 * @port: Master port to send transaction
1525 * @destid: Destination ID for device/switch
1526 * @hopcount: Hopcount to reach switch
1527 * @wait_ms: Max wait time in msec (0 = no timeout)
1529 * Attepts to acquire host device lock for specified device
1530 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1532 int rio_lock_device(struct rio_mport *port, u16 destid,
1533 u8 hopcount, int wait_ms)
1538 /* Attempt to acquire device lock */
1539 rio_mport_write_config_32(port, destid, hopcount,
1540 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1541 rio_mport_read_config_32(port, destid, hopcount,
1542 RIO_HOST_DID_LOCK_CSR, &result);
1544 while (result != port->host_deviceid) {
1545 if (wait_ms != 0 && tcnt == wait_ms) {
1546 pr_debug("RIO: timeout when locking device %x:%x\n",
1554 /* Try to acquire device lock again */
1555 rio_mport_write_config_32(port, destid,
1557 RIO_HOST_DID_LOCK_CSR,
1558 port->host_deviceid);
1559 rio_mport_read_config_32(port, destid,
1561 RIO_HOST_DID_LOCK_CSR, &result);
1566 EXPORT_SYMBOL_GPL(rio_lock_device);
1569 * rio_unlock_device - Releases host device lock for specified device
1570 * @port: Master port to send transaction
1571 * @destid: Destination ID for device/switch
1572 * @hopcount: Hopcount to reach switch
1574 * Returns 0 if device lock released or EINVAL if fails.
1576 int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1580 /* Release device lock */
1581 rio_mport_write_config_32(port, destid,
1583 RIO_HOST_DID_LOCK_CSR,
1584 port->host_deviceid);
1585 rio_mport_read_config_32(port, destid, hopcount,
1586 RIO_HOST_DID_LOCK_CSR, &result);
1587 if ((result & 0xffff) != 0xffff) {
1588 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1595 EXPORT_SYMBOL_GPL(rio_unlock_device);
1598 * rio_route_add_entry- Add a route entry to a switch routing table
1600 * @table: Routing table ID
1601 * @route_destid: Destination ID to be routed
1602 * @route_port: Port number to be routed
1603 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1605 * If available calls the switch specific add_entry() method to add a route
1606 * entry into a switch routing table. Otherwise uses standard RT update method
1607 * as defined by RapidIO specification. A specific routing table can be selected
1608 * using the @table argument if a switch has per port routing tables or
1609 * the standard (or global) table may be used by passing
1610 * %RIO_GLOBAL_TABLE in @table.
1612 * Returns %0 on success or %-EINVAL on failure.
1614 int rio_route_add_entry(struct rio_dev *rdev,
1615 u16 table, u16 route_destid, u8 route_port, int lock)
1618 struct rio_switch_ops *ops = rdev->rswitch->ops;
1621 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1622 rdev->hopcount, 1000);
1627 spin_lock(&rdev->rswitch->lock);
1629 if (!ops || !ops->add_entry) {
1630 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1631 rdev->hopcount, table,
1632 route_destid, route_port);
1633 } else if (try_module_get(ops->owner)) {
1634 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1635 rdev->hopcount, table, route_destid,
1637 module_put(ops->owner);
1640 spin_unlock(&rdev->rswitch->lock);
1643 rio_unlock_device(rdev->net->hport, rdev->destid,
1648 EXPORT_SYMBOL_GPL(rio_route_add_entry);
1651 * rio_route_get_entry- Read an entry from a switch routing table
1653 * @table: Routing table ID
1654 * @route_destid: Destination ID to be routed
1655 * @route_port: Pointer to read port number into
1656 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1658 * If available calls the switch specific get_entry() method to fetch a route
1659 * entry from a switch routing table. Otherwise uses standard RT read method
1660 * as defined by RapidIO specification. A specific routing table can be selected
1661 * using the @table argument if a switch has per port routing tables or
1662 * the standard (or global) table may be used by passing
1663 * %RIO_GLOBAL_TABLE in @table.
1665 * Returns %0 on success or %-EINVAL on failure.
1667 int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1668 u16 route_destid, u8 *route_port, int lock)
1671 struct rio_switch_ops *ops = rdev->rswitch->ops;
1674 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1675 rdev->hopcount, 1000);
1680 spin_lock(&rdev->rswitch->lock);
1682 if (!ops || !ops->get_entry) {
1683 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1684 rdev->hopcount, table,
1685 route_destid, route_port);
1686 } else if (try_module_get(ops->owner)) {
1687 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1688 rdev->hopcount, table, route_destid,
1690 module_put(ops->owner);
1693 spin_unlock(&rdev->rswitch->lock);
1696 rio_unlock_device(rdev->net->hport, rdev->destid,
1700 EXPORT_SYMBOL_GPL(rio_route_get_entry);
1703 * rio_route_clr_table - Clear a switch routing table
1705 * @table: Routing table ID
1706 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1708 * If available calls the switch specific clr_table() method to clear a switch
1709 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1710 * specification. A specific routing table can be selected using the @table
1711 * argument if a switch has per port routing tables or the standard (or global)
1712 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1714 * Returns %0 on success or %-EINVAL on failure.
1716 int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1719 struct rio_switch_ops *ops = rdev->rswitch->ops;
1722 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1723 rdev->hopcount, 1000);
1728 spin_lock(&rdev->rswitch->lock);
1730 if (!ops || !ops->clr_table) {
1731 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1732 rdev->hopcount, table);
1733 } else if (try_module_get(ops->owner)) {
1734 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1735 rdev->hopcount, table);
1737 module_put(ops->owner);
1740 spin_unlock(&rdev->rswitch->lock);
1743 rio_unlock_device(rdev->net->hport, rdev->destid,
1748 EXPORT_SYMBOL_GPL(rio_route_clr_table);
1750 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1752 static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1754 struct rio_mport *mport = arg;
1756 /* Check that DMA device belongs to the right MPORT */
1757 return mport == container_of(chan->device, struct rio_mport, dma);
1761 * rio_request_mport_dma - request RapidIO capable DMA channel associated
1762 * with specified local RapidIO mport device.
1763 * @mport: RIO mport to perform DMA data transfers
1765 * Returns pointer to allocated DMA channel or NULL if failed.
1767 struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1769 dma_cap_mask_t mask;
1772 dma_cap_set(DMA_SLAVE, mask);
1773 return dma_request_channel(mask, rio_chan_filter, mport);
1775 EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1778 * rio_request_dma - request RapidIO capable DMA channel that supports
1779 * specified target RapidIO device.
1780 * @rdev: RIO device associated with DMA transfer
1782 * Returns pointer to allocated DMA channel or NULL if failed.
1784 struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1786 return rio_request_mport_dma(rdev->net->hport);
1788 EXPORT_SYMBOL_GPL(rio_request_dma);
1791 * rio_release_dma - release specified DMA channel
1792 * @dchan: DMA channel to release
1794 void rio_release_dma(struct dma_chan *dchan)
1796 dma_release_channel(dchan);
1798 EXPORT_SYMBOL_GPL(rio_release_dma);
1801 * rio_dma_prep_xfer - RapidIO specific wrapper
1802 * for device_prep_slave_sg callback defined by DMAENGINE.
1803 * @dchan: DMA channel to configure
1804 * @destid: target RapidIO device destination ID
1805 * @data: RIO specific data descriptor
1806 * @direction: DMA data transfer direction (TO or FROM the device)
1807 * @flags: dmaengine defined flags
1809 * Initializes RapidIO capable DMA channel for the specified data transfer.
1810 * Uses DMA channel private extension to pass information related to remote
1811 * target RIO device.
1813 * Returns: pointer to DMA transaction descriptor if successful,
1814 * error-valued pointer or NULL if failed.
1816 struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1817 u16 destid, struct rio_dma_data *data,
1818 enum dma_transfer_direction direction, unsigned long flags)
1820 struct rio_dma_ext rio_ext;
1822 if (!dchan->device->device_prep_slave_sg) {
1823 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1827 rio_ext.destid = destid;
1828 rio_ext.rio_addr_u = data->rio_addr_u;
1829 rio_ext.rio_addr = data->rio_addr;
1830 rio_ext.wr_type = data->wr_type;
1832 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1833 direction, flags, &rio_ext);
1835 EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1838 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1839 * for device_prep_slave_sg callback defined by DMAENGINE.
1840 * @rdev: RIO device control structure
1841 * @dchan: DMA channel to configure
1842 * @data: RIO specific data descriptor
1843 * @direction: DMA data transfer direction (TO or FROM the device)
1844 * @flags: dmaengine defined flags
1846 * Initializes RapidIO capable DMA channel for the specified data transfer.
1847 * Uses DMA channel private extension to pass information related to remote
1848 * target RIO device.
1850 * Returns: pointer to DMA transaction descriptor if successful,
1851 * error-valued pointer or NULL if failed.
1853 struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1854 struct dma_chan *dchan, struct rio_dma_data *data,
1855 enum dma_transfer_direction direction, unsigned long flags)
1857 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
1859 EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1861 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1864 * rio_find_mport - find RIO mport by its ID
1865 * @mport_id: number (ID) of mport device
1867 * Given a RIO mport number, the desired mport is located
1868 * in the global list of mports. If the mport is found, a pointer to its
1869 * data structure is returned. If no mport is found, %NULL is returned.
1871 struct rio_mport *rio_find_mport(int mport_id)
1873 struct rio_mport *port;
1875 mutex_lock(&rio_mport_list_lock);
1876 list_for_each_entry(port, &rio_mports, node) {
1877 if (port->id == mport_id)
1882 mutex_unlock(&rio_mport_list_lock);
1888 * rio_register_scan - enumeration/discovery method registration interface
1889 * @mport_id: mport device ID for which fabric scan routine has to be set
1890 * (RIO_MPORT_ANY = set for all available mports)
1891 * @scan_ops: enumeration/discovery operations structure
1893 * Registers enumeration/discovery operations with RapidIO subsystem and
1894 * attaches it to the specified mport device (or all available mports
1895 * if RIO_MPORT_ANY is specified).
1897 * Returns error if the mport already has an enumerator attached to it.
1898 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
1900 int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1902 struct rio_mport *port;
1903 struct rio_scan_node *scan;
1906 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1908 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1912 mutex_lock(&rio_mport_list_lock);
1915 * Check if there is another enumerator already registered for
1916 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1917 * for the same mport ID are not supported.
1919 list_for_each_entry(scan, &rio_scans, node) {
1920 if (scan->mport_id == mport_id) {
1927 * Allocate and initialize new scan registration node.
1929 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1935 scan->mport_id = mport_id;
1936 scan->ops = scan_ops;
1939 * Traverse the list of registered mports to attach this new scan.
1941 * The new scan with matching mport ID overrides any previously attached
1942 * scan assuming that old scan (if any) is the default one (based on the
1943 * enumerator registration check above).
1944 * If the new scan is the global one, it will be attached only to mports
1945 * that do not have their own individual operations already attached.
1947 list_for_each_entry(port, &rio_mports, node) {
1948 if (port->id == mport_id) {
1949 port->nscan = scan_ops;
1951 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
1952 port->nscan = scan_ops;
1955 list_add_tail(&scan->node, &rio_scans);
1958 mutex_unlock(&rio_mport_list_lock);
1962 EXPORT_SYMBOL_GPL(rio_register_scan);
1965 * rio_unregister_scan - removes enumeration/discovery method from mport
1966 * @mport_id: mport device ID for which fabric scan routine has to be
1967 * unregistered (RIO_MPORT_ANY = apply to all mports that use
1968 * the specified scan_ops)
1969 * @scan_ops: enumeration/discovery operations structure
1971 * Removes enumeration or discovery method assigned to the specified mport
1972 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
1973 * all mports that have them attached.
1975 int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
1977 struct rio_mport *port;
1978 struct rio_scan_node *scan;
1980 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1982 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
1985 mutex_lock(&rio_mport_list_lock);
1987 list_for_each_entry(port, &rio_mports, node)
1988 if (port->id == mport_id ||
1989 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
1992 list_for_each_entry(scan, &rio_scans, node) {
1993 if (scan->mport_id == mport_id) {
1994 list_del(&scan->node);
2000 mutex_unlock(&rio_mport_list_lock);
2004 EXPORT_SYMBOL_GPL(rio_unregister_scan);
2007 * rio_mport_scan - execute enumeration/discovery on the specified mport
2008 * @mport_id: number (ID) of mport device
2010 int rio_mport_scan(int mport_id)
2012 struct rio_mport *port = NULL;
2015 mutex_lock(&rio_mport_list_lock);
2016 list_for_each_entry(port, &rio_mports, node) {
2017 if (port->id == mport_id)
2020 mutex_unlock(&rio_mport_list_lock);
2024 mutex_unlock(&rio_mport_list_lock);
2028 if (!try_module_get(port->nscan->owner)) {
2029 mutex_unlock(&rio_mport_list_lock);
2033 mutex_unlock(&rio_mport_list_lock);
2035 if (port->host_deviceid >= 0)
2036 rc = port->nscan->enumerate(port, 0);
2038 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
2040 module_put(port->nscan->owner);
2044 static struct workqueue_struct *rio_wq;
2046 struct rio_disc_work {
2047 struct work_struct work;
2048 struct rio_mport *mport;
2051 static void disc_work_handler(struct work_struct *_work)
2053 struct rio_disc_work *work;
2055 work = container_of(_work, struct rio_disc_work, work);
2056 pr_debug("RIO: discovery work for mport %d %s\n",
2057 work->mport->id, work->mport->name);
2058 if (try_module_get(work->mport->nscan->owner)) {
2059 work->mport->nscan->discover(work->mport, 0);
2060 module_put(work->mport->nscan->owner);
2064 int rio_init_mports(void)
2066 struct rio_mport *port;
2067 struct rio_disc_work *work;
2074 * First, run enumerations and check if we need to perform discovery
2075 * on any of the registered mports.
2077 mutex_lock(&rio_mport_list_lock);
2078 list_for_each_entry(port, &rio_mports, node) {
2079 if (port->host_deviceid >= 0) {
2080 if (port->nscan && try_module_get(port->nscan->owner)) {
2081 port->nscan->enumerate(port, 0);
2082 module_put(port->nscan->owner);
2087 mutex_unlock(&rio_mport_list_lock);
2093 * If we have mports that require discovery schedule a discovery work
2094 * for each of them. If the code below fails to allocate needed
2095 * resources, exit without error to keep results of enumeration
2097 * TODO: Implement restart of discovery process for all or
2098 * individual discovering mports.
2100 rio_wq = alloc_workqueue("riodisc", 0, 0);
2102 pr_err("RIO: unable allocate rio_wq\n");
2106 work = kcalloc(n, sizeof *work, GFP_KERNEL);
2108 destroy_workqueue(rio_wq);
2113 mutex_lock(&rio_mport_list_lock);
2114 list_for_each_entry(port, &rio_mports, node) {
2115 if (port->host_deviceid < 0 && port->nscan) {
2116 work[n].mport = port;
2117 INIT_WORK(&work[n].work, disc_work_handler);
2118 queue_work(rio_wq, &work[n].work);
2123 flush_workqueue(rio_wq);
2124 mutex_unlock(&rio_mport_list_lock);
2125 pr_debug("RIO: destroy discovery workqueue\n");
2126 destroy_workqueue(rio_wq);
2132 EXPORT_SYMBOL_GPL(rio_init_mports);
2134 static int rio_get_hdid(int index)
2136 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
2142 int rio_mport_initialize(struct rio_mport *mport)
2144 if (next_portid >= RIO_MAX_MPORTS) {
2145 pr_err("RIO: reached specified max number of mports\n");
2149 atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
2150 mport->id = next_portid++;
2151 mport->host_deviceid = rio_get_hdid(mport->id);
2152 mport->nscan = NULL;
2153 mutex_init(&mport->lock);
2154 mport->pwe_refcnt = 0;
2155 INIT_LIST_HEAD(&mport->pwrites);
2159 EXPORT_SYMBOL_GPL(rio_mport_initialize);
2161 int rio_register_mport(struct rio_mport *port)
2163 struct rio_scan_node *scan = NULL;
2166 mutex_lock(&rio_mport_list_lock);
2169 * Check if there are any registered enumeration/discovery operations
2170 * that have to be attached to the added mport.
2172 list_for_each_entry(scan, &rio_scans, node) {
2173 if (port->id == scan->mport_id ||
2174 scan->mport_id == RIO_MPORT_ANY) {
2175 port->nscan = scan->ops;
2176 if (port->id == scan->mport_id)
2181 list_add_tail(&port->node, &rio_mports);
2182 mutex_unlock(&rio_mport_list_lock);
2184 dev_set_name(&port->dev, "rapidio%d", port->id);
2185 port->dev.class = &rio_mport_class;
2186 atomic_set(&port->state, RIO_DEVICE_RUNNING);
2188 res = device_register(&port->dev);
2190 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
2193 dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
2197 EXPORT_SYMBOL_GPL(rio_register_mport);
2199 static int rio_mport_cleanup_callback(struct device *dev, void *data)
2201 struct rio_dev *rdev = to_rio_dev(dev);
2203 if (dev->bus == &rio_bus_type)
2204 rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
2208 static int rio_net_remove_children(struct rio_net *net)
2211 * Unregister all RapidIO devices residing on this net (this will
2212 * invoke notification of registered subsystem interfaces as well).
2214 device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
2218 int rio_unregister_mport(struct rio_mport *port)
2220 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2222 /* Transition mport to the SHUTDOWN state */
2223 if (atomic_cmpxchg(&port->state,
2225 RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
2226 pr_err("RIO: %s unexpected state transition for mport %s\n",
2227 __func__, port->name);
2230 if (port->net && port->net->hport == port) {
2231 rio_net_remove_children(port->net);
2232 rio_free_net(port->net);
2236 * Unregister all RapidIO devices attached to this mport (this will
2237 * invoke notification of registered subsystem interfaces as well).
2239 mutex_lock(&rio_mport_list_lock);
2240 list_del(&port->node);
2241 mutex_unlock(&rio_mport_list_lock);
2242 device_unregister(&port->dev);
2246 EXPORT_SYMBOL_GPL(rio_unregister_mport);