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>
33 MODULE_DESCRIPTION("RapidIO Subsystem Core");
34 MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
35 MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
36 MODULE_LICENSE("GPL");
38 static int hdid[RIO_MAX_MPORTS];
40 module_param_array(hdid, int, &ids_num, 0);
41 MODULE_PARM_DESC(hdid,
42 "Destination ID assignment to local RapidIO controllers");
44 static LIST_HEAD(rio_devices);
45 static DEFINE_SPINLOCK(rio_global_list_lock);
47 static LIST_HEAD(rio_mports);
48 static LIST_HEAD(rio_scans);
49 static DEFINE_MUTEX(rio_mport_list_lock);
50 static unsigned char next_portid;
51 static DEFINE_SPINLOCK(rio_mmap_lock);
54 * rio_local_get_device_id - Get the base/extended device id for a port
55 * @port: RIO master port from which to get the deviceid
57 * Reads the base/extended device id from the local device
58 * implementing the master port. Returns the 8/16-bit device
61 u16 rio_local_get_device_id(struct rio_mport *port)
65 rio_local_read_config_32(port, RIO_DID_CSR, &result);
67 return (RIO_GET_DID(port->sys_size, result));
71 * rio_query_mport - Query mport device attributes
72 * @port: mport device to query
73 * @mport_attr: mport attributes data structure
75 * Returns attributes of specified mport through the
76 * pointer to attributes data structure.
78 int rio_query_mport(struct rio_mport *port,
79 struct rio_mport_attr *mport_attr)
81 if (!port->ops->query_mport)
83 return port->ops->query_mport(port, mport_attr);
85 EXPORT_SYMBOL(rio_query_mport);
88 * rio_add_device- Adds a RIO device to the device model
91 * Adds the RIO device to the global device list and adds the RIO
92 * device to the RIO device list. Creates the generic sysfs nodes
95 int rio_add_device(struct rio_dev *rdev)
99 err = device_register(&rdev->dev);
103 spin_lock(&rio_global_list_lock);
104 list_add_tail(&rdev->global_list, &rio_devices);
106 list_add_tail(&rdev->net_list, &rdev->net->devices);
107 if (rdev->pef & RIO_PEF_SWITCH)
108 list_add_tail(&rdev->rswitch->node,
109 &rdev->net->switches);
111 spin_unlock(&rio_global_list_lock);
113 rio_create_sysfs_dev_files(rdev);
117 EXPORT_SYMBOL_GPL(rio_add_device);
120 * rio_del_device - removes a RIO device from the device model
123 * Removes the RIO device to the kernel device list and subsystem's device list.
124 * Clears sysfs entries for the removed device.
126 void rio_del_device(struct rio_dev *rdev)
128 pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
129 spin_lock(&rio_global_list_lock);
130 list_del(&rdev->global_list);
132 list_del(&rdev->net_list);
133 if (rdev->pef & RIO_PEF_SWITCH) {
134 list_del(&rdev->rswitch->node);
135 kfree(rdev->rswitch->route_table);
138 spin_unlock(&rio_global_list_lock);
139 rio_remove_sysfs_dev_files(rdev);
140 device_unregister(&rdev->dev);
142 EXPORT_SYMBOL_GPL(rio_del_device);
145 * rio_request_inb_mbox - request inbound mailbox service
146 * @mport: RIO master port from which to allocate the mailbox resource
147 * @dev_id: Device specific pointer to pass on event
148 * @mbox: Mailbox number to claim
149 * @entries: Number of entries in inbound mailbox queue
150 * @minb: Callback to execute when inbound message is received
152 * Requests ownership of an inbound mailbox resource and binds
153 * a callback function to the resource. Returns %0 on success.
155 int rio_request_inb_mbox(struct rio_mport *mport,
159 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
163 struct resource *res;
165 if (mport->ops->open_inb_mbox == NULL)
168 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
171 rio_init_mbox_res(res, mbox, mbox);
173 /* Make sure this mailbox isn't in use */
175 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
181 mport->inb_msg[mbox].res = res;
183 /* Hook the inbound message callback */
184 mport->inb_msg[mbox].mcback = minb;
186 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
195 * rio_release_inb_mbox - release inbound mailbox message service
196 * @mport: RIO master port from which to release the mailbox resource
197 * @mbox: Mailbox number to release
199 * Releases ownership of an inbound mailbox resource. Returns 0
200 * if the request has been satisfied.
202 int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
204 if (mport->ops->close_inb_mbox) {
205 mport->ops->close_inb_mbox(mport, mbox);
207 /* Release the mailbox resource */
208 return release_resource(mport->inb_msg[mbox].res);
214 * rio_request_outb_mbox - request outbound mailbox service
215 * @mport: RIO master port from which to allocate the mailbox resource
216 * @dev_id: Device specific pointer to pass on event
217 * @mbox: Mailbox number to claim
218 * @entries: Number of entries in outbound mailbox queue
219 * @moutb: Callback to execute when outbound message is sent
221 * Requests ownership of an outbound mailbox resource and binds
222 * a callback function to the resource. Returns 0 on success.
224 int rio_request_outb_mbox(struct rio_mport *mport,
228 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
231 struct resource *res;
233 if (mport->ops->open_outb_mbox == NULL)
236 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
239 rio_init_mbox_res(res, mbox, mbox);
241 /* Make sure this outbound mailbox isn't in use */
243 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
249 mport->outb_msg[mbox].res = res;
251 /* Hook the inbound message callback */
252 mport->outb_msg[mbox].mcback = moutb;
254 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
263 * rio_release_outb_mbox - release outbound mailbox message service
264 * @mport: RIO master port from which to release the mailbox resource
265 * @mbox: Mailbox number to release
267 * Releases ownership of an inbound mailbox resource. Returns 0
268 * if the request has been satisfied.
270 int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
272 if (mport->ops->close_outb_mbox) {
273 mport->ops->close_outb_mbox(mport, mbox);
275 /* Release the mailbox resource */
276 return release_resource(mport->outb_msg[mbox].res);
282 * rio_setup_inb_dbell - bind inbound doorbell callback
283 * @mport: RIO master port to bind the doorbell callback
284 * @dev_id: Device specific pointer to pass on event
285 * @res: Doorbell message resource
286 * @dinb: Callback to execute when doorbell is received
288 * Adds a doorbell resource/callback pair into a port's
289 * doorbell event list. Returns 0 if the request has been
293 rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
294 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
298 struct rio_dbell *dbell;
300 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
307 dbell->dev_id = dev_id;
309 list_add_tail(&dbell->node, &mport->dbells);
316 * rio_request_inb_dbell - request inbound doorbell message service
317 * @mport: RIO master port from which to allocate the doorbell resource
318 * @dev_id: Device specific pointer to pass on event
319 * @start: Doorbell info range start
320 * @end: Doorbell info range end
321 * @dinb: Callback to execute when doorbell is received
323 * Requests ownership of an inbound doorbell resource and binds
324 * a callback function to the resource. Returns 0 if the request
325 * has been satisfied.
327 int rio_request_inb_dbell(struct rio_mport *mport,
331 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
336 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
339 rio_init_dbell_res(res, start, end);
341 /* Make sure these doorbells aren't in use */
343 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
349 /* Hook the doorbell callback */
350 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
359 * rio_release_inb_dbell - release inbound doorbell message service
360 * @mport: RIO master port from which to release the doorbell resource
361 * @start: Doorbell info range start
362 * @end: Doorbell info range end
364 * Releases ownership of an inbound doorbell resource and removes
365 * callback from the doorbell event list. Returns 0 if the request
366 * has been satisfied.
368 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
370 int rc = 0, found = 0;
371 struct rio_dbell *dbell;
373 list_for_each_entry(dbell, &mport->dbells, node) {
374 if ((dbell->res->start == start) && (dbell->res->end == end)) {
380 /* If we can't find an exact match, fail */
386 /* Delete from list */
387 list_del(&dbell->node);
389 /* Release the doorbell resource */
390 rc = release_resource(dbell->res);
392 /* Free the doorbell event */
400 * rio_request_outb_dbell - request outbound doorbell message range
401 * @rdev: RIO device from which to allocate the doorbell resource
402 * @start: Doorbell message range start
403 * @end: Doorbell message range end
405 * Requests ownership of a doorbell message range. Returns a resource
406 * if the request has been satisfied or %NULL on failure.
408 struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
411 struct resource *res = kzalloc(sizeof(struct resource), GFP_KERNEL);
414 rio_init_dbell_res(res, start, end);
416 /* Make sure these doorbells aren't in use */
417 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
428 * rio_release_outb_dbell - release outbound doorbell message range
429 * @rdev: RIO device from which to release the doorbell resource
430 * @res: Doorbell resource to be freed
432 * Releases ownership of a doorbell message range. Returns 0 if the
433 * request has been satisfied.
435 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
437 int rc = release_resource(res);
445 * rio_request_inb_pwrite - request inbound port-write message service
446 * @rdev: RIO device to which register inbound port-write callback routine
447 * @pwcback: Callback routine to execute when port-write is received
449 * Binds a port-write callback function to the RapidIO device.
450 * Returns 0 if the request has been satisfied.
452 int rio_request_inb_pwrite(struct rio_dev *rdev,
453 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
457 spin_lock(&rio_global_list_lock);
458 if (rdev->pwcback != NULL)
461 rdev->pwcback = pwcback;
463 spin_unlock(&rio_global_list_lock);
466 EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
469 * rio_release_inb_pwrite - release inbound port-write message service
470 * @rdev: RIO device which registered for inbound port-write callback
472 * Removes callback from the rio_dev structure. Returns 0 if the request
473 * has been satisfied.
475 int rio_release_inb_pwrite(struct rio_dev *rdev)
479 spin_lock(&rio_global_list_lock);
481 rdev->pwcback = NULL;
485 spin_unlock(&rio_global_list_lock);
488 EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
491 * rio_map_inb_region -- Map inbound memory region.
492 * @mport: Master port.
493 * @local: physical address of memory region to be mapped
494 * @rbase: RIO base address assigned to this window
495 * @size: Size of the memory region
496 * @rflags: Flags for mapping.
498 * Return: 0 -- Success.
500 * This function will create the mapping from RIO space to local memory.
502 int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
503 u64 rbase, u32 size, u32 rflags)
508 if (!mport->ops->map_inb)
510 spin_lock_irqsave(&rio_mmap_lock, flags);
511 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
512 spin_unlock_irqrestore(&rio_mmap_lock, flags);
515 EXPORT_SYMBOL_GPL(rio_map_inb_region);
518 * rio_unmap_inb_region -- Unmap the inbound memory region
519 * @mport: Master port
520 * @lstart: physical address of memory region to be unmapped
522 void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
525 if (!mport->ops->unmap_inb)
527 spin_lock_irqsave(&rio_mmap_lock, flags);
528 mport->ops->unmap_inb(mport, lstart);
529 spin_unlock_irqrestore(&rio_mmap_lock, flags);
531 EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
534 * rio_mport_get_physefb - Helper function that returns register offset
535 * for Physical Layer Extended Features Block.
536 * @port: Master port to issue transaction
537 * @local: Indicate a local master port or remote device access
538 * @destid: Destination ID of the device
539 * @hopcount: Number of switch hops to the device
542 rio_mport_get_physefb(struct rio_mport *port, int local,
543 u16 destid, u8 hopcount)
548 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
550 while (ext_ftr_ptr) {
552 rio_local_read_config_32(port, ext_ftr_ptr,
555 rio_mport_read_config_32(port, destid, hopcount,
556 ext_ftr_ptr, &ftr_header);
558 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
559 switch (ftr_header) {
561 case RIO_EFB_SER_EP_ID_V13P:
562 case RIO_EFB_SER_EP_REC_ID_V13P:
563 case RIO_EFB_SER_EP_FREE_ID_V13P:
564 case RIO_EFB_SER_EP_ID:
565 case RIO_EFB_SER_EP_REC_ID:
566 case RIO_EFB_SER_EP_FREE_ID:
567 case RIO_EFB_SER_EP_FREC_ID:
575 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
576 hopcount, ext_ftr_ptr);
581 EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
584 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
585 * @comp_tag: RIO component tag to match
586 * @from: Previous RIO device found in search, or %NULL for new search
588 * Iterates through the list of known RIO devices. If a RIO device is
589 * found with a matching @comp_tag, a pointer to its device
590 * structure is returned. Otherwise, %NULL is returned. A new search
591 * is initiated by passing %NULL to the @from argument. Otherwise, if
592 * @from is not %NULL, searches continue from next device on the global
595 struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
598 struct rio_dev *rdev;
600 spin_lock(&rio_global_list_lock);
601 n = from ? from->global_list.next : rio_devices.next;
603 while (n && (n != &rio_devices)) {
605 if (rdev->comp_tag == comp_tag)
611 spin_unlock(&rio_global_list_lock);
614 EXPORT_SYMBOL_GPL(rio_get_comptag);
617 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
618 * @rdev: Pointer to RIO device control structure
619 * @pnum: Switch port number to set LOCKOUT bit
620 * @lock: Operation : set (=1) or clear (=0)
622 int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
626 rio_read_config_32(rdev,
627 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
630 regval |= RIO_PORT_N_CTL_LOCKOUT;
632 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
634 rio_write_config_32(rdev,
635 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
639 EXPORT_SYMBOL_GPL(rio_set_port_lockout);
642 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
644 * @port: Master port associated with the RIO network
645 * @local: local=1 select local port otherwise a far device is reached
646 * @destid: Destination ID of the device to check host bit
647 * @hopcount: Number of hops to reach the target
648 * @port_num: Port (-number on switch) to enable on a far end device
650 * Returns 0 or 1 from on General Control Command and Status Register
653 int rio_enable_rx_tx_port(struct rio_mport *port,
654 int local, u16 destid,
655 u8 hopcount, u8 port_num)
657 #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
662 * enable rx input tx output port
664 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
665 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
667 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
670 rio_local_read_config_32(port, ext_ftr_ptr +
671 RIO_PORT_N_CTL_CSR(0),
674 if (rio_mport_read_config_32(port, destid, hopcount,
675 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), ®val) < 0)
679 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
681 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
682 | RIO_PORT_N_CTL_EN_TX_SER;
685 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
686 | RIO_PORT_N_CTL_EN_TX_PAR;
690 rio_local_write_config_32(port, ext_ftr_ptr +
691 RIO_PORT_N_CTL_CSR(0), regval);
693 if (rio_mport_write_config_32(port, destid, hopcount,
694 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
700 EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
704 * rio_chk_dev_route - Validate route to the specified device.
705 * @rdev: RIO device failed to respond
706 * @nrdev: Last active device on the route to rdev
707 * @npnum: nrdev's port number on the route to rdev
709 * Follows a route to the specified RIO device to determine the last available
710 * device (and corresponding RIO port) on the route.
713 rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
716 int p_port, rc = -EIO;
717 struct rio_dev *prev = NULL;
719 /* Find switch with failed RIO link */
720 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
721 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
731 p_port = prev->rswitch->route_table[rdev->destid];
733 if (p_port != RIO_INVALID_ROUTE) {
734 pr_debug("RIO: link failed on [%s]-P%d\n",
735 rio_name(prev), p_port);
740 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
746 * rio_mport_chk_dev_access - Validate access to the specified device.
747 * @mport: Master port to send transactions
748 * @destid: Device destination ID in network
749 * @hopcount: Number of hops into the network
752 rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
757 while (rio_mport_read_config_32(mport, destid, hopcount,
758 RIO_DEV_ID_CAR, &tmp)) {
760 if (i == RIO_MAX_CHK_RETRY)
767 EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
770 * rio_chk_dev_access - Validate access to the specified device.
771 * @rdev: Pointer to RIO device control structure
773 static int rio_chk_dev_access(struct rio_dev *rdev)
775 return rio_mport_chk_dev_access(rdev->net->hport,
776 rdev->destid, rdev->hopcount);
780 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
781 * returns link-response (if requested).
782 * @rdev: RIO devive to issue Input-status command
783 * @pnum: Device port number to issue the command
784 * @lnkresp: Response from a link partner
787 rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
793 /* Read from link maintenance response register
794 * to clear valid bit */
795 rio_read_config_32(rdev,
796 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
801 /* Issue Input-status command */
802 rio_write_config_32(rdev,
803 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
806 /* Exit if the response is not expected */
811 while (checkcount--) {
813 rio_read_config_32(rdev,
814 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
816 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
826 * rio_clr_err_stopped - Clears port Error-stopped states.
827 * @rdev: Pointer to RIO device control structure
828 * @pnum: Switch port number to clear errors
829 * @err_status: port error status (if 0 reads register from device)
831 static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
833 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
835 u32 far_ackid, far_linkstat, near_ackid;
838 rio_read_config_32(rdev,
839 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
842 if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
843 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
845 * Send a Link-Request/Input-Status control symbol
847 if (rio_get_input_status(rdev, pnum, ®val)) {
848 pr_debug("RIO_EM: Input-status response timeout\n");
852 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
854 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
855 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
856 rio_read_config_32(rdev,
857 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
859 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
860 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
861 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
862 " near_ackID=0x%02x\n",
863 pnum, far_ackid, far_linkstat, near_ackid);
866 * If required, synchronize ackIDs of near and
869 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
870 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
871 /* Align near outstanding/outbound ackIDs with
874 rio_write_config_32(rdev,
875 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
877 (far_ackid << 8) | far_ackid);
878 /* Align far outstanding/outbound ackIDs with
883 rio_write_config_32(nextdev,
884 nextdev->phys_efptr +
885 RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
887 (near_ackid << 8) | near_ackid);
889 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
892 rio_read_config_32(rdev,
893 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
895 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
898 if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
899 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
900 rio_get_input_status(nextdev,
901 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
904 rio_read_config_32(rdev,
905 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
907 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
910 return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
911 RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
915 * rio_inb_pwrite_handler - process inbound port-write message
916 * @pw_msg: pointer to inbound port-write message
918 * Processes an inbound port-write message. Returns 0 if the request
919 * has been satisfied.
921 int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
923 struct rio_dev *rdev;
924 u32 err_status, em_perrdet, em_ltlerrdet;
927 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
929 /* Device removed or enumeration error */
930 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
931 __func__, pw_msg->em.comptag);
935 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
940 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
941 pr_debug("0x%02x: %08x %08x %08x %08x\n",
942 i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
943 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
949 /* Call an external service function (if such is registered
950 * for this device). This may be the service for endpoints that send
951 * device-specific port-write messages. End-point messages expected
952 * to be handled completely by EP specific device driver.
953 * For switches rc==0 signals that no standard processing required.
955 if (rdev->pwcback != NULL) {
956 rc = rdev->pwcback(rdev, pw_msg, 0);
961 portnum = pw_msg->em.is_port & 0xFF;
963 /* Check if device and route to it are functional:
964 * Sometimes devices may send PW message(s) just before being
965 * powered down (or link being lost).
967 if (rio_chk_dev_access(rdev)) {
968 pr_debug("RIO: device access failed - get link partner\n");
969 /* Scan route to the device and identify failed link.
970 * This will replace device and port reported in PW message.
971 * PW message should not be used after this point.
973 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
974 pr_err("RIO: Route trace for %s failed\n",
981 /* For End-point devices processing stops here */
982 if (!(rdev->pef & RIO_PEF_SWITCH))
985 if (rdev->phys_efptr == 0) {
986 pr_err("RIO_PW: Bad switch initialization for %s\n",
992 * Process the port-write notification from switch
994 if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
995 rdev->rswitch->ops->em_handle(rdev, portnum);
997 rio_read_config_32(rdev,
998 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
1000 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
1002 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
1004 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
1005 rdev->rswitch->port_ok |= (1 << portnum);
1006 rio_set_port_lockout(rdev, portnum, 0);
1007 /* Schedule Insertion Service */
1008 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
1009 rio_name(rdev), portnum);
1012 /* Clear error-stopped states (if reported).
1013 * Depending on the link partner state, two attempts
1014 * may be needed for successful recovery.
1016 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
1017 RIO_PORT_N_ERR_STS_PW_INP_ES)) {
1018 if (rio_clr_err_stopped(rdev, portnum, err_status))
1019 rio_clr_err_stopped(rdev, portnum, 0);
1021 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
1023 if (rdev->rswitch->port_ok & (1 << portnum)) {
1024 rdev->rswitch->port_ok &= ~(1 << portnum);
1025 rio_set_port_lockout(rdev, portnum, 1);
1027 rio_write_config_32(rdev,
1029 RIO_PORT_N_ACK_STS_CSR(portnum),
1030 RIO_PORT_N_ACK_CLEAR);
1032 /* Schedule Extraction Service */
1033 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1034 rio_name(rdev), portnum);
1038 rio_read_config_32(rdev,
1039 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1041 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1042 portnum, em_perrdet);
1043 /* Clear EM Port N Error Detect CSR */
1044 rio_write_config_32(rdev,
1045 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1048 rio_read_config_32(rdev,
1049 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1051 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1053 /* Clear EM L/T Layer Error Detect CSR */
1054 rio_write_config_32(rdev,
1055 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1058 /* Clear remaining error bits and Port-Write Pending bit */
1059 rio_write_config_32(rdev,
1060 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
1065 EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1068 * rio_mport_get_efb - get pointer to next extended features block
1069 * @port: Master port to issue transaction
1070 * @local: Indicate a local master port or remote device access
1071 * @destid: Destination ID of the device
1072 * @hopcount: Number of switch hops to the device
1073 * @from: Offset of current Extended Feature block header (if 0 starts
1074 * from ExtFeaturePtr)
1077 rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1078 u8 hopcount, u32 from)
1084 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1087 rio_mport_read_config_32(port, destid, hopcount,
1088 RIO_ASM_INFO_CAR, ®_val);
1089 return reg_val & RIO_EXT_FTR_PTR_MASK;
1092 rio_local_read_config_32(port, from, ®_val);
1094 rio_mport_read_config_32(port, destid, hopcount,
1096 return RIO_GET_BLOCK_ID(reg_val);
1099 EXPORT_SYMBOL_GPL(rio_mport_get_efb);
1102 * rio_mport_get_feature - query for devices' extended features
1103 * @port: Master port to issue transaction
1104 * @local: Indicate a local master port or remote device access
1105 * @destid: Destination ID of the device
1106 * @hopcount: Number of switch hops to the device
1107 * @ftr: Extended feature code
1109 * Tell if a device supports a given RapidIO capability.
1110 * Returns the offset of the requested extended feature
1111 * block within the device's RIO configuration space or
1112 * 0 in case the device does not support it. Possible
1115 * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
1117 * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
1119 * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
1121 * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
1123 * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
1125 * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
1128 rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1129 u8 hopcount, int ftr)
1131 u32 asm_info, ext_ftr_ptr, ftr_header;
1134 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1136 rio_mport_read_config_32(port, destid, hopcount,
1137 RIO_ASM_INFO_CAR, &asm_info);
1139 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1141 while (ext_ftr_ptr) {
1143 rio_local_read_config_32(port, ext_ftr_ptr,
1146 rio_mport_read_config_32(port, destid, hopcount,
1147 ext_ftr_ptr, &ftr_header);
1148 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1150 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
1156 EXPORT_SYMBOL_GPL(rio_mport_get_feature);
1159 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1160 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1161 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1162 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1163 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1164 * @from: Previous RIO device found in search, or %NULL for new search
1166 * Iterates through the list of known RIO devices. If a RIO device is
1167 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1168 * count to the device is incrememted and a pointer to its device
1169 * structure is returned. Otherwise, %NULL is returned. A new search
1170 * is initiated by passing %NULL to the @from argument. Otherwise, if
1171 * @from is not %NULL, searches continue from next device on the global
1172 * list. The reference count for @from is always decremented if it is
1175 struct rio_dev *rio_get_asm(u16 vid, u16 did,
1176 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1178 struct list_head *n;
1179 struct rio_dev *rdev;
1181 WARN_ON(in_interrupt());
1182 spin_lock(&rio_global_list_lock);
1183 n = from ? from->global_list.next : rio_devices.next;
1185 while (n && (n != &rio_devices)) {
1186 rdev = rio_dev_g(n);
1187 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1188 (did == RIO_ANY_ID || rdev->did == did) &&
1189 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1190 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1197 rdev = rio_dev_get(rdev);
1198 spin_unlock(&rio_global_list_lock);
1203 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1204 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1205 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1206 * @from: Previous RIO device found in search, or %NULL for new search
1208 * Iterates through the list of known RIO devices. If a RIO device is
1209 * found with a matching @vid and @did, the reference count to the
1210 * device is incrememted and a pointer to its device structure is returned.
1211 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1212 * to the @from argument. Otherwise, if @from is not %NULL, searches
1213 * continue from next device on the global list. The reference count for
1214 * @from is always decremented if it is not %NULL.
1216 struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1218 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1222 * rio_std_route_add_entry - Add switch route table entry using standard
1223 * registers defined in RIO specification rev.1.3
1224 * @mport: Master port to issue transaction
1225 * @destid: Destination ID of the device
1226 * @hopcount: Number of switch hops to the device
1227 * @table: routing table ID (global or port-specific)
1228 * @route_destid: destID entry in the RT
1229 * @route_port: destination port for specified destID
1232 rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1233 u16 table, u16 route_destid, u8 route_port)
1235 if (table == RIO_GLOBAL_TABLE) {
1236 rio_mport_write_config_32(mport, destid, hopcount,
1237 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1239 rio_mport_write_config_32(mport, destid, hopcount,
1240 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1249 * rio_std_route_get_entry - Read switch route table entry (port number)
1250 * associated with specified destID using standard registers defined in RIO
1251 * specification rev.1.3
1252 * @mport: Master port to issue transaction
1253 * @destid: Destination ID of the device
1254 * @hopcount: Number of switch hops to the device
1255 * @table: routing table ID (global or port-specific)
1256 * @route_destid: destID entry in the RT
1257 * @route_port: returned destination port for specified destID
1260 rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1261 u16 table, u16 route_destid, u8 *route_port)
1265 if (table == RIO_GLOBAL_TABLE) {
1266 rio_mport_write_config_32(mport, destid, hopcount,
1267 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1268 rio_mport_read_config_32(mport, destid, hopcount,
1269 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1271 *route_port = (u8)result;
1278 * rio_std_route_clr_table - Clear swotch route table using standard registers
1279 * defined in RIO specification rev.1.3.
1280 * @mport: Master port to issue transaction
1281 * @destid: Destination ID of the device
1282 * @hopcount: Number of switch hops to the device
1283 * @table: routing table ID (global or port-specific)
1286 rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1289 u32 max_destid = 0xff;
1290 u32 i, pef, id_inc = 1, ext_cfg = 0;
1291 u32 port_sel = RIO_INVALID_ROUTE;
1293 if (table == RIO_GLOBAL_TABLE) {
1294 rio_mport_read_config_32(mport, destid, hopcount,
1297 if (mport->sys_size) {
1298 rio_mport_read_config_32(mport, destid, hopcount,
1299 RIO_SWITCH_RT_LIMIT,
1301 max_destid &= RIO_RT_MAX_DESTID;
1304 if (pef & RIO_PEF_EXT_RT) {
1305 ext_cfg = 0x80000000;
1307 port_sel = (RIO_INVALID_ROUTE << 24) |
1308 (RIO_INVALID_ROUTE << 16) |
1309 (RIO_INVALID_ROUTE << 8) |
1313 for (i = 0; i <= max_destid;) {
1314 rio_mport_write_config_32(mport, destid, hopcount,
1315 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1317 rio_mport_write_config_32(mport, destid, hopcount,
1318 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1329 * rio_lock_device - Acquires host device lock for specified device
1330 * @port: Master port to send transaction
1331 * @destid: Destination ID for device/switch
1332 * @hopcount: Hopcount to reach switch
1333 * @wait_ms: Max wait time in msec (0 = no timeout)
1335 * Attepts to acquire host device lock for specified device
1336 * Returns 0 if device lock acquired or EINVAL if timeout expires.
1338 int rio_lock_device(struct rio_mport *port, u16 destid,
1339 u8 hopcount, int wait_ms)
1344 /* Attempt to acquire device lock */
1345 rio_mport_write_config_32(port, destid, hopcount,
1346 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
1347 rio_mport_read_config_32(port, destid, hopcount,
1348 RIO_HOST_DID_LOCK_CSR, &result);
1350 while (result != port->host_deviceid) {
1351 if (wait_ms != 0 && tcnt == wait_ms) {
1352 pr_debug("RIO: timeout when locking device %x:%x\n",
1360 /* Try to acquire device lock again */
1361 rio_mport_write_config_32(port, destid,
1363 RIO_HOST_DID_LOCK_CSR,
1364 port->host_deviceid);
1365 rio_mport_read_config_32(port, destid,
1367 RIO_HOST_DID_LOCK_CSR, &result);
1372 EXPORT_SYMBOL_GPL(rio_lock_device);
1375 * rio_unlock_device - Releases host device lock for specified device
1376 * @port: Master port to send transaction
1377 * @destid: Destination ID for device/switch
1378 * @hopcount: Hopcount to reach switch
1380 * Returns 0 if device lock released or EINVAL if fails.
1382 int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
1386 /* Release device lock */
1387 rio_mport_write_config_32(port, destid,
1389 RIO_HOST_DID_LOCK_CSR,
1390 port->host_deviceid);
1391 rio_mport_read_config_32(port, destid, hopcount,
1392 RIO_HOST_DID_LOCK_CSR, &result);
1393 if ((result & 0xffff) != 0xffff) {
1394 pr_debug("RIO: badness when releasing device lock %x:%x\n",
1401 EXPORT_SYMBOL_GPL(rio_unlock_device);
1404 * rio_route_add_entry- Add a route entry to a switch routing table
1406 * @table: Routing table ID
1407 * @route_destid: Destination ID to be routed
1408 * @route_port: Port number to be routed
1409 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1411 * If available calls the switch specific add_entry() method to add a route
1412 * entry into a switch routing table. Otherwise uses standard RT update method
1413 * as defined by RapidIO specification. A specific routing table can be selected
1414 * using the @table argument if a switch has per port routing tables or
1415 * the standard (or global) table may be used by passing
1416 * %RIO_GLOBAL_TABLE in @table.
1418 * Returns %0 on success or %-EINVAL on failure.
1420 int rio_route_add_entry(struct rio_dev *rdev,
1421 u16 table, u16 route_destid, u8 route_port, int lock)
1424 struct rio_switch_ops *ops = rdev->rswitch->ops;
1427 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1428 rdev->hopcount, 1000);
1433 spin_lock(&rdev->rswitch->lock);
1435 if (ops == NULL || ops->add_entry == NULL) {
1436 rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
1437 rdev->hopcount, table,
1438 route_destid, route_port);
1439 } else if (try_module_get(ops->owner)) {
1440 rc = ops->add_entry(rdev->net->hport, rdev->destid,
1441 rdev->hopcount, table, route_destid,
1443 module_put(ops->owner);
1446 spin_unlock(&rdev->rswitch->lock);
1449 rio_unlock_device(rdev->net->hport, rdev->destid,
1454 EXPORT_SYMBOL_GPL(rio_route_add_entry);
1457 * rio_route_get_entry- Read an entry from a switch routing table
1459 * @table: Routing table ID
1460 * @route_destid: Destination ID to be routed
1461 * @route_port: Pointer to read port number into
1462 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1464 * If available calls the switch specific get_entry() method to fetch a route
1465 * entry from a switch routing table. Otherwise uses standard RT read method
1466 * as defined by RapidIO specification. A specific routing table can be selected
1467 * using the @table argument if a switch has per port routing tables or
1468 * the standard (or global) table may be used by passing
1469 * %RIO_GLOBAL_TABLE in @table.
1471 * Returns %0 on success or %-EINVAL on failure.
1473 int rio_route_get_entry(struct rio_dev *rdev, u16 table,
1474 u16 route_destid, u8 *route_port, int lock)
1477 struct rio_switch_ops *ops = rdev->rswitch->ops;
1480 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1481 rdev->hopcount, 1000);
1486 spin_lock(&rdev->rswitch->lock);
1488 if (ops == NULL || ops->get_entry == NULL) {
1489 rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
1490 rdev->hopcount, table,
1491 route_destid, route_port);
1492 } else if (try_module_get(ops->owner)) {
1493 rc = ops->get_entry(rdev->net->hport, rdev->destid,
1494 rdev->hopcount, table, route_destid,
1496 module_put(ops->owner);
1499 spin_unlock(&rdev->rswitch->lock);
1502 rio_unlock_device(rdev->net->hport, rdev->destid,
1506 EXPORT_SYMBOL_GPL(rio_route_get_entry);
1509 * rio_route_clr_table - Clear a switch routing table
1511 * @table: Routing table ID
1512 * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
1514 * If available calls the switch specific clr_table() method to clear a switch
1515 * routing table. Otherwise uses standard RT write method as defined by RapidIO
1516 * specification. A specific routing table can be selected using the @table
1517 * argument if a switch has per port routing tables or the standard (or global)
1518 * table may be used by passing %RIO_GLOBAL_TABLE in @table.
1520 * Returns %0 on success or %-EINVAL on failure.
1522 int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
1525 struct rio_switch_ops *ops = rdev->rswitch->ops;
1528 rc = rio_lock_device(rdev->net->hport, rdev->destid,
1529 rdev->hopcount, 1000);
1534 spin_lock(&rdev->rswitch->lock);
1536 if (ops == NULL || ops->clr_table == NULL) {
1537 rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
1538 rdev->hopcount, table);
1539 } else if (try_module_get(ops->owner)) {
1540 rc = ops->clr_table(rdev->net->hport, rdev->destid,
1541 rdev->hopcount, table);
1543 module_put(ops->owner);
1546 spin_unlock(&rdev->rswitch->lock);
1549 rio_unlock_device(rdev->net->hport, rdev->destid,
1554 EXPORT_SYMBOL_GPL(rio_route_clr_table);
1556 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1558 static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1560 struct rio_mport *mport = arg;
1562 /* Check that DMA device belongs to the right MPORT */
1563 return mport == container_of(chan->device, struct rio_mport, dma);
1567 * rio_request_mport_dma - request RapidIO capable DMA channel associated
1568 * with specified local RapidIO mport device.
1569 * @mport: RIO mport to perform DMA data transfers
1571 * Returns pointer to allocated DMA channel or NULL if failed.
1573 struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
1575 dma_cap_mask_t mask;
1578 dma_cap_set(DMA_SLAVE, mask);
1579 return dma_request_channel(mask, rio_chan_filter, mport);
1581 EXPORT_SYMBOL_GPL(rio_request_mport_dma);
1584 * rio_request_dma - request RapidIO capable DMA channel that supports
1585 * specified target RapidIO device.
1586 * @rdev: RIO device associated with DMA transfer
1588 * Returns pointer to allocated DMA channel or NULL if failed.
1590 struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1592 return rio_request_mport_dma(rdev->net->hport);
1594 EXPORT_SYMBOL_GPL(rio_request_dma);
1597 * rio_release_dma - release specified DMA channel
1598 * @dchan: DMA channel to release
1600 void rio_release_dma(struct dma_chan *dchan)
1602 dma_release_channel(dchan);
1604 EXPORT_SYMBOL_GPL(rio_release_dma);
1607 * rio_dma_prep_xfer - RapidIO specific wrapper
1608 * for device_prep_slave_sg callback defined by DMAENGINE.
1609 * @dchan: DMA channel to configure
1610 * @destid: target RapidIO device destination ID
1611 * @data: RIO specific data descriptor
1612 * @direction: DMA data transfer direction (TO or FROM the device)
1613 * @flags: dmaengine defined flags
1615 * Initializes RapidIO capable DMA channel for the specified data transfer.
1616 * Uses DMA channel private extension to pass information related to remote
1617 * target RIO device.
1618 * Returns pointer to DMA transaction descriptor or NULL if failed.
1620 struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
1621 u16 destid, struct rio_dma_data *data,
1622 enum dma_transfer_direction direction, unsigned long flags)
1624 struct rio_dma_ext rio_ext;
1626 if (dchan->device->device_prep_slave_sg == NULL) {
1627 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1631 rio_ext.destid = destid;
1632 rio_ext.rio_addr_u = data->rio_addr_u;
1633 rio_ext.rio_addr = data->rio_addr;
1634 rio_ext.wr_type = data->wr_type;
1636 return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1637 direction, flags, &rio_ext);
1639 EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
1642 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1643 * for device_prep_slave_sg callback defined by DMAENGINE.
1644 * @rdev: RIO device control structure
1645 * @dchan: DMA channel to configure
1646 * @data: RIO specific data descriptor
1647 * @direction: DMA data transfer direction (TO or FROM the device)
1648 * @flags: dmaengine defined flags
1650 * Initializes RapidIO capable DMA channel for the specified data transfer.
1651 * Uses DMA channel private extension to pass information related to remote
1652 * target RIO device.
1653 * Returns pointer to DMA transaction descriptor or NULL if failed.
1655 struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1656 struct dma_chan *dchan, struct rio_dma_data *data,
1657 enum dma_transfer_direction direction, unsigned long flags)
1659 return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
1661 EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1663 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1666 * rio_find_mport - find RIO mport by its ID
1667 * @mport_id: number (ID) of mport device
1669 * Given a RIO mport number, the desired mport is located
1670 * in the global list of mports. If the mport is found, a pointer to its
1671 * data structure is returned. If no mport is found, %NULL is returned.
1673 struct rio_mport *rio_find_mport(int mport_id)
1675 struct rio_mport *port;
1677 mutex_lock(&rio_mport_list_lock);
1678 list_for_each_entry(port, &rio_mports, node) {
1679 if (port->id == mport_id)
1684 mutex_unlock(&rio_mport_list_lock);
1690 * rio_register_scan - enumeration/discovery method registration interface
1691 * @mport_id: mport device ID for which fabric scan routine has to be set
1692 * (RIO_MPORT_ANY = set for all available mports)
1693 * @scan_ops: enumeration/discovery operations structure
1695 * Registers enumeration/discovery operations with RapidIO subsystem and
1696 * attaches it to the specified mport device (or all available mports
1697 * if RIO_MPORT_ANY is specified).
1699 * Returns error if the mport already has an enumerator attached to it.
1700 * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
1702 int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1704 struct rio_mport *port;
1705 struct rio_scan_node *scan;
1708 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1710 if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
1714 mutex_lock(&rio_mport_list_lock);
1717 * Check if there is another enumerator already registered for
1718 * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
1719 * for the same mport ID are not supported.
1721 list_for_each_entry(scan, &rio_scans, node) {
1722 if (scan->mport_id == mport_id) {
1729 * Allocate and initialize new scan registration node.
1731 scan = kzalloc(sizeof(*scan), GFP_KERNEL);
1737 scan->mport_id = mport_id;
1738 scan->ops = scan_ops;
1741 * Traverse the list of registered mports to attach this new scan.
1743 * The new scan with matching mport ID overrides any previously attached
1744 * scan assuming that old scan (if any) is the default one (based on the
1745 * enumerator registration check above).
1746 * If the new scan is the global one, it will be attached only to mports
1747 * that do not have their own individual operations already attached.
1749 list_for_each_entry(port, &rio_mports, node) {
1750 if (port->id == mport_id) {
1751 port->nscan = scan_ops;
1753 } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
1754 port->nscan = scan_ops;
1757 list_add_tail(&scan->node, &rio_scans);
1760 mutex_unlock(&rio_mport_list_lock);
1764 EXPORT_SYMBOL_GPL(rio_register_scan);
1767 * rio_unregister_scan - removes enumeration/discovery method from mport
1768 * @mport_id: mport device ID for which fabric scan routine has to be
1769 * unregistered (RIO_MPORT_ANY = apply to all mports that use
1770 * the specified scan_ops)
1771 * @scan_ops: enumeration/discovery operations structure
1773 * Removes enumeration or discovery method assigned to the specified mport
1774 * device. If RIO_MPORT_ANY is specified, removes the specified operations from
1775 * all mports that have them attached.
1777 int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
1779 struct rio_mport *port;
1780 struct rio_scan_node *scan;
1782 pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
1784 if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
1787 mutex_lock(&rio_mport_list_lock);
1789 list_for_each_entry(port, &rio_mports, node)
1790 if (port->id == mport_id ||
1791 (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
1794 list_for_each_entry(scan, &rio_scans, node) {
1795 if (scan->mport_id == mport_id) {
1796 list_del(&scan->node);
1802 mutex_unlock(&rio_mport_list_lock);
1806 EXPORT_SYMBOL_GPL(rio_unregister_scan);
1809 * rio_mport_scan - execute enumeration/discovery on the specified mport
1810 * @mport_id: number (ID) of mport device
1812 int rio_mport_scan(int mport_id)
1814 struct rio_mport *port = NULL;
1817 mutex_lock(&rio_mport_list_lock);
1818 list_for_each_entry(port, &rio_mports, node) {
1819 if (port->id == mport_id)
1822 mutex_unlock(&rio_mport_list_lock);
1826 mutex_unlock(&rio_mport_list_lock);
1830 if (!try_module_get(port->nscan->owner)) {
1831 mutex_unlock(&rio_mport_list_lock);
1835 mutex_unlock(&rio_mport_list_lock);
1837 if (port->host_deviceid >= 0)
1838 rc = port->nscan->enumerate(port, 0);
1840 rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
1842 module_put(port->nscan->owner);
1846 static void rio_fixup_device(struct rio_dev *dev)
1850 static int rio_init(void)
1852 struct rio_dev *dev = NULL;
1854 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1855 rio_fixup_device(dev);
1860 static struct workqueue_struct *rio_wq;
1862 struct rio_disc_work {
1863 struct work_struct work;
1864 struct rio_mport *mport;
1867 static void disc_work_handler(struct work_struct *_work)
1869 struct rio_disc_work *work;
1871 work = container_of(_work, struct rio_disc_work, work);
1872 pr_debug("RIO: discovery work for mport %d %s\n",
1873 work->mport->id, work->mport->name);
1874 if (try_module_get(work->mport->nscan->owner)) {
1875 work->mport->nscan->discover(work->mport, 0);
1876 module_put(work->mport->nscan->owner);
1880 int rio_init_mports(void)
1882 struct rio_mport *port;
1883 struct rio_disc_work *work;
1890 * First, run enumerations and check if we need to perform discovery
1891 * on any of the registered mports.
1893 mutex_lock(&rio_mport_list_lock);
1894 list_for_each_entry(port, &rio_mports, node) {
1895 if (port->host_deviceid >= 0) {
1896 if (port->nscan && try_module_get(port->nscan->owner)) {
1897 port->nscan->enumerate(port, 0);
1898 module_put(port->nscan->owner);
1903 mutex_unlock(&rio_mport_list_lock);
1909 * If we have mports that require discovery schedule a discovery work
1910 * for each of them. If the code below fails to allocate needed
1911 * resources, exit without error to keep results of enumeration
1913 * TODO: Implement restart of discovery process for all or
1914 * individual discovering mports.
1916 rio_wq = alloc_workqueue("riodisc", 0, 0);
1918 pr_err("RIO: unable allocate rio_wq\n");
1922 work = kcalloc(n, sizeof *work, GFP_KERNEL);
1924 pr_err("RIO: no memory for work struct\n");
1925 destroy_workqueue(rio_wq);
1930 mutex_lock(&rio_mport_list_lock);
1931 list_for_each_entry(port, &rio_mports, node) {
1932 if (port->host_deviceid < 0 && port->nscan) {
1933 work[n].mport = port;
1934 INIT_WORK(&work[n].work, disc_work_handler);
1935 queue_work(rio_wq, &work[n].work);
1940 flush_workqueue(rio_wq);
1941 mutex_unlock(&rio_mport_list_lock);
1942 pr_debug("RIO: destroy discovery workqueue\n");
1943 destroy_workqueue(rio_wq);
1952 static int rio_get_hdid(int index)
1954 if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
1960 int rio_register_mport(struct rio_mport *port)
1962 struct rio_scan_node *scan = NULL;
1965 if (next_portid >= RIO_MAX_MPORTS) {
1966 pr_err("RIO: reached specified max number of mports\n");
1970 port->id = next_portid++;
1971 port->host_deviceid = rio_get_hdid(port->id);
1974 dev_set_name(&port->dev, "rapidio%d", port->id);
1975 port->dev.class = &rio_mport_class;
1977 res = device_register(&port->dev);
1979 dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
1982 dev_dbg(&port->dev, "RIO: mport%d registered\n", port->id);
1984 mutex_lock(&rio_mport_list_lock);
1985 list_add_tail(&port->node, &rio_mports);
1988 * Check if there are any registered enumeration/discovery operations
1989 * that have to be attached to the added mport.
1991 list_for_each_entry(scan, &rio_scans, node) {
1992 if (port->id == scan->mport_id ||
1993 scan->mport_id == RIO_MPORT_ANY) {
1994 port->nscan = scan->ops;
1995 if (port->id == scan->mport_id)
1999 mutex_unlock(&rio_mport_list_lock);
2001 pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
2004 EXPORT_SYMBOL_GPL(rio_register_mport);
2006 EXPORT_SYMBOL_GPL(rio_local_get_device_id);
2007 EXPORT_SYMBOL_GPL(rio_get_device);
2008 EXPORT_SYMBOL_GPL(rio_get_asm);
2009 EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
2010 EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
2011 EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
2012 EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
2013 EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
2014 EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
2015 EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
2016 EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
2017 EXPORT_SYMBOL_GPL(rio_init_mports);