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 Integrated Device Technology, Inc.
9 * Alex Bounine <alexandre.bounine@idt.com>
10 * - Added Port-Write/Error Management initialization and handling
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <linux/types.h>
19 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/rio.h>
24 #include <linux/rio_drv.h>
25 #include <linux/rio_ids.h>
26 #include <linux/rio_regs.h>
27 #include <linux/module.h>
28 #include <linux/spinlock.h>
29 #include <linux/slab.h>
30 #include <linux/interrupt.h>
34 static LIST_HEAD(rio_devices);
35 static DEFINE_SPINLOCK(rio_global_list_lock);
37 static LIST_HEAD(rio_mports);
38 static DEFINE_MUTEX(rio_mport_list_lock);
39 static unsigned char next_portid;
40 static DEFINE_SPINLOCK(rio_mmap_lock);
43 * rio_local_get_device_id - Get the base/extended device id for a port
44 * @port: RIO master port from which to get the deviceid
46 * Reads the base/extended device id from the local device
47 * implementing the master port. Returns the 8/16-bit device
50 u16 rio_local_get_device_id(struct rio_mport *port)
54 rio_local_read_config_32(port, RIO_DID_CSR, &result);
56 return (RIO_GET_DID(port->sys_size, result));
60 * rio_add_device- Adds a RIO device to the device model
63 * Adds the RIO device to the global device list and adds the RIO
64 * device to the RIO device list. Creates the generic sysfs nodes
67 int rio_add_device(struct rio_dev *rdev)
71 err = device_add(&rdev->dev);
75 spin_lock(&rio_global_list_lock);
76 list_add_tail(&rdev->global_list, &rio_devices);
77 spin_unlock(&rio_global_list_lock);
79 rio_create_sysfs_dev_files(rdev);
83 EXPORT_SYMBOL_GPL(rio_add_device);
86 * rio_request_inb_mbox - request inbound mailbox service
87 * @mport: RIO master port from which to allocate the mailbox resource
88 * @dev_id: Device specific pointer to pass on event
89 * @mbox: Mailbox number to claim
90 * @entries: Number of entries in inbound mailbox queue
91 * @minb: Callback to execute when inbound message is received
93 * Requests ownership of an inbound mailbox resource and binds
94 * a callback function to the resource. Returns %0 on success.
96 int rio_request_inb_mbox(struct rio_mport *mport,
100 void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
104 struct resource *res;
106 if (mport->ops->open_inb_mbox == NULL)
109 res = kmalloc(sizeof(struct resource), GFP_KERNEL);
112 rio_init_mbox_res(res, mbox, mbox);
114 /* Make sure this mailbox isn't in use */
116 request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
122 mport->inb_msg[mbox].res = res;
124 /* Hook the inbound message callback */
125 mport->inb_msg[mbox].mcback = minb;
127 rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
136 * rio_release_inb_mbox - release inbound mailbox message service
137 * @mport: RIO master port from which to release the mailbox resource
138 * @mbox: Mailbox number to release
140 * Releases ownership of an inbound mailbox resource. Returns 0
141 * if the request has been satisfied.
143 int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
145 if (mport->ops->close_inb_mbox) {
146 mport->ops->close_inb_mbox(mport, mbox);
148 /* Release the mailbox resource */
149 return release_resource(mport->inb_msg[mbox].res);
155 * rio_request_outb_mbox - request outbound mailbox service
156 * @mport: RIO master port from which to allocate the mailbox resource
157 * @dev_id: Device specific pointer to pass on event
158 * @mbox: Mailbox number to claim
159 * @entries: Number of entries in outbound mailbox queue
160 * @moutb: Callback to execute when outbound message is sent
162 * Requests ownership of an outbound mailbox resource and binds
163 * a callback function to the resource. Returns 0 on success.
165 int rio_request_outb_mbox(struct rio_mport *mport,
169 void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
172 struct resource *res;
174 if (mport->ops->open_outb_mbox == NULL)
177 res = kmalloc(sizeof(struct resource), GFP_KERNEL);
180 rio_init_mbox_res(res, mbox, mbox);
182 /* Make sure this outbound mailbox isn't in use */
184 request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
190 mport->outb_msg[mbox].res = res;
192 /* Hook the inbound message callback */
193 mport->outb_msg[mbox].mcback = moutb;
195 rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
204 * rio_release_outb_mbox - release outbound mailbox message service
205 * @mport: RIO master port from which to release the mailbox resource
206 * @mbox: Mailbox number to release
208 * Releases ownership of an inbound mailbox resource. Returns 0
209 * if the request has been satisfied.
211 int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
213 if (mport->ops->close_outb_mbox) {
214 mport->ops->close_outb_mbox(mport, mbox);
216 /* Release the mailbox resource */
217 return release_resource(mport->outb_msg[mbox].res);
223 * rio_setup_inb_dbell - bind inbound doorbell callback
224 * @mport: RIO master port to bind the doorbell callback
225 * @dev_id: Device specific pointer to pass on event
226 * @res: Doorbell message resource
227 * @dinb: Callback to execute when doorbell is received
229 * Adds a doorbell resource/callback pair into a port's
230 * doorbell event list. Returns 0 if the request has been
234 rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
235 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
239 struct rio_dbell *dbell;
241 if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
248 dbell->dev_id = dev_id;
250 list_add_tail(&dbell->node, &mport->dbells);
257 * rio_request_inb_dbell - request inbound doorbell message service
258 * @mport: RIO master port from which to allocate the doorbell resource
259 * @dev_id: Device specific pointer to pass on event
260 * @start: Doorbell info range start
261 * @end: Doorbell info range end
262 * @dinb: Callback to execute when doorbell is received
264 * Requests ownership of an inbound doorbell resource and binds
265 * a callback function to the resource. Returns 0 if the request
266 * has been satisfied.
268 int rio_request_inb_dbell(struct rio_mport *mport,
272 void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
277 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
280 rio_init_dbell_res(res, start, end);
282 /* Make sure these doorbells aren't in use */
284 request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
290 /* Hook the doorbell callback */
291 rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
300 * rio_release_inb_dbell - release inbound doorbell message service
301 * @mport: RIO master port from which to release the doorbell resource
302 * @start: Doorbell info range start
303 * @end: Doorbell info range end
305 * Releases ownership of an inbound doorbell resource and removes
306 * callback from the doorbell event list. Returns 0 if the request
307 * has been satisfied.
309 int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
311 int rc = 0, found = 0;
312 struct rio_dbell *dbell;
314 list_for_each_entry(dbell, &mport->dbells, node) {
315 if ((dbell->res->start == start) && (dbell->res->end == end)) {
321 /* If we can't find an exact match, fail */
327 /* Delete from list */
328 list_del(&dbell->node);
330 /* Release the doorbell resource */
331 rc = release_resource(dbell->res);
333 /* Free the doorbell event */
341 * rio_request_outb_dbell - request outbound doorbell message range
342 * @rdev: RIO device from which to allocate the doorbell resource
343 * @start: Doorbell message range start
344 * @end: Doorbell message range end
346 * Requests ownership of a doorbell message range. Returns a resource
347 * if the request has been satisfied or %NULL on failure.
349 struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
352 struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
355 rio_init_dbell_res(res, start, end);
357 /* Make sure these doorbells aren't in use */
358 if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
369 * rio_release_outb_dbell - release outbound doorbell message range
370 * @rdev: RIO device from which to release the doorbell resource
371 * @res: Doorbell resource to be freed
373 * Releases ownership of a doorbell message range. Returns 0 if the
374 * request has been satisfied.
376 int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
378 int rc = release_resource(res);
386 * rio_request_inb_pwrite - request inbound port-write message service
387 * @rdev: RIO device to which register inbound port-write callback routine
388 * @pwcback: Callback routine to execute when port-write is received
390 * Binds a port-write callback function to the RapidIO device.
391 * Returns 0 if the request has been satisfied.
393 int rio_request_inb_pwrite(struct rio_dev *rdev,
394 int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
398 spin_lock(&rio_global_list_lock);
399 if (rdev->pwcback != NULL)
402 rdev->pwcback = pwcback;
404 spin_unlock(&rio_global_list_lock);
407 EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
410 * rio_release_inb_pwrite - release inbound port-write message service
411 * @rdev: RIO device which registered for inbound port-write callback
413 * Removes callback from the rio_dev structure. Returns 0 if the request
414 * has been satisfied.
416 int rio_release_inb_pwrite(struct rio_dev *rdev)
420 spin_lock(&rio_global_list_lock);
422 rdev->pwcback = NULL;
426 spin_unlock(&rio_global_list_lock);
429 EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
432 * rio_map_inb_region -- Map inbound memory region.
433 * @mport: Master port.
434 * @local: physical address of memory region to be mapped
435 * @rbase: RIO base address assigned to this window
436 * @size: Size of the memory region
437 * @rflags: Flags for mapping.
439 * Return: 0 -- Success.
441 * This function will create the mapping from RIO space to local memory.
443 int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
444 u64 rbase, u32 size, u32 rflags)
449 if (!mport->ops->map_inb)
451 spin_lock_irqsave(&rio_mmap_lock, flags);
452 rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
453 spin_unlock_irqrestore(&rio_mmap_lock, flags);
456 EXPORT_SYMBOL_GPL(rio_map_inb_region);
459 * rio_unmap_inb_region -- Unmap the inbound memory region
460 * @mport: Master port
461 * @lstart: physical address of memory region to be unmapped
463 void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
466 if (!mport->ops->unmap_inb)
468 spin_lock_irqsave(&rio_mmap_lock, flags);
469 mport->ops->unmap_inb(mport, lstart);
470 spin_unlock_irqrestore(&rio_mmap_lock, flags);
472 EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
475 * rio_mport_get_physefb - Helper function that returns register offset
476 * for Physical Layer Extended Features Block.
477 * @port: Master port to issue transaction
478 * @local: Indicate a local master port or remote device access
479 * @destid: Destination ID of the device
480 * @hopcount: Number of switch hops to the device
483 rio_mport_get_physefb(struct rio_mport *port, int local,
484 u16 destid, u8 hopcount)
489 ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
491 while (ext_ftr_ptr) {
493 rio_local_read_config_32(port, ext_ftr_ptr,
496 rio_mport_read_config_32(port, destid, hopcount,
497 ext_ftr_ptr, &ftr_header);
499 ftr_header = RIO_GET_BLOCK_ID(ftr_header);
500 switch (ftr_header) {
502 case RIO_EFB_SER_EP_ID_V13P:
503 case RIO_EFB_SER_EP_REC_ID_V13P:
504 case RIO_EFB_SER_EP_FREE_ID_V13P:
505 case RIO_EFB_SER_EP_ID:
506 case RIO_EFB_SER_EP_REC_ID:
507 case RIO_EFB_SER_EP_FREE_ID:
508 case RIO_EFB_SER_EP_FREC_ID:
516 ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
517 hopcount, ext_ftr_ptr);
522 EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
525 * rio_get_comptag - Begin or continue searching for a RIO device by component tag
526 * @comp_tag: RIO component tag to match
527 * @from: Previous RIO device found in search, or %NULL for new search
529 * Iterates through the list of known RIO devices. If a RIO device is
530 * found with a matching @comp_tag, a pointer to its device
531 * structure is returned. Otherwise, %NULL is returned. A new search
532 * is initiated by passing %NULL to the @from argument. Otherwise, if
533 * @from is not %NULL, searches continue from next device on the global
536 struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
539 struct rio_dev *rdev;
541 spin_lock(&rio_global_list_lock);
542 n = from ? from->global_list.next : rio_devices.next;
544 while (n && (n != &rio_devices)) {
546 if (rdev->comp_tag == comp_tag)
552 spin_unlock(&rio_global_list_lock);
555 EXPORT_SYMBOL_GPL(rio_get_comptag);
558 * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
559 * @rdev: Pointer to RIO device control structure
560 * @pnum: Switch port number to set LOCKOUT bit
561 * @lock: Operation : set (=1) or clear (=0)
563 int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
567 rio_read_config_32(rdev,
568 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
571 regval |= RIO_PORT_N_CTL_LOCKOUT;
573 regval &= ~RIO_PORT_N_CTL_LOCKOUT;
575 rio_write_config_32(rdev,
576 rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
580 EXPORT_SYMBOL_GPL(rio_set_port_lockout);
583 * rio_switch_init - Sets switch operations for a particular vendor switch
585 * @do_enum: Enumeration/Discovery mode flag
587 * Searches the RIO switch ops table for known switch types. If the vid
588 * and did match a switch table entry, then call switch initialization
589 * routine to setup switch-specific routines.
591 void rio_switch_init(struct rio_dev *rdev, int do_enum)
593 struct rio_switch_ops *cur = __start_rio_switch_ops;
594 struct rio_switch_ops *end = __end_rio_switch_ops;
597 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
598 pr_debug("RIO: calling init routine for %s\n",
600 cur->init_hook(rdev, do_enum);
606 if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
607 pr_debug("RIO: adding STD routing ops for %s\n",
609 rdev->rswitch->add_entry = rio_std_route_add_entry;
610 rdev->rswitch->get_entry = rio_std_route_get_entry;
611 rdev->rswitch->clr_table = rio_std_route_clr_table;
614 if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
615 printk(KERN_ERR "RIO: missing routing ops for %s\n",
618 EXPORT_SYMBOL_GPL(rio_switch_init);
621 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
623 * @port: Master port associated with the RIO network
624 * @local: local=1 select local port otherwise a far device is reached
625 * @destid: Destination ID of the device to check host bit
626 * @hopcount: Number of hops to reach the target
627 * @port_num: Port (-number on switch) to enable on a far end device
629 * Returns 0 or 1 from on General Control Command and Status Register
632 int rio_enable_rx_tx_port(struct rio_mport *port,
633 int local, u16 destid,
634 u8 hopcount, u8 port_num)
636 #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
641 * enable rx input tx output port
643 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
644 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
646 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
649 rio_local_read_config_32(port, ext_ftr_ptr +
650 RIO_PORT_N_CTL_CSR(0),
653 if (rio_mport_read_config_32(port, destid, hopcount,
654 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), ®val) < 0)
658 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
660 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
661 | RIO_PORT_N_CTL_EN_TX_SER;
664 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
665 | RIO_PORT_N_CTL_EN_TX_PAR;
669 rio_local_write_config_32(port, ext_ftr_ptr +
670 RIO_PORT_N_CTL_CSR(0), regval);
672 if (rio_mport_write_config_32(port, destid, hopcount,
673 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
679 EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
683 * rio_chk_dev_route - Validate route to the specified device.
684 * @rdev: RIO device failed to respond
685 * @nrdev: Last active device on the route to rdev
686 * @npnum: nrdev's port number on the route to rdev
688 * Follows a route to the specified RIO device to determine the last available
689 * device (and corresponding RIO port) on the route.
692 rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
695 int p_port, rc = -EIO;
696 struct rio_dev *prev = NULL;
698 /* Find switch with failed RIO link */
699 while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
700 if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
710 p_port = prev->rswitch->route_table[rdev->destid];
712 if (p_port != RIO_INVALID_ROUTE) {
713 pr_debug("RIO: link failed on [%s]-P%d\n",
714 rio_name(prev), p_port);
719 pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
725 * rio_mport_chk_dev_access - Validate access to the specified device.
726 * @mport: Master port to send transactions
727 * @destid: Device destination ID in network
728 * @hopcount: Number of hops into the network
731 rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
736 while (rio_mport_read_config_32(mport, destid, hopcount,
737 RIO_DEV_ID_CAR, &tmp)) {
739 if (i == RIO_MAX_CHK_RETRY)
746 EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
749 * rio_chk_dev_access - Validate access to the specified device.
750 * @rdev: Pointer to RIO device control structure
752 static int rio_chk_dev_access(struct rio_dev *rdev)
754 return rio_mport_chk_dev_access(rdev->net->hport,
755 rdev->destid, rdev->hopcount);
759 * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
760 * returns link-response (if requested).
761 * @rdev: RIO devive to issue Input-status command
762 * @pnum: Device port number to issue the command
763 * @lnkresp: Response from a link partner
766 rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
772 /* Read from link maintenance response register
773 * to clear valid bit */
774 rio_read_config_32(rdev,
775 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
780 /* Issue Input-status command */
781 rio_write_config_32(rdev,
782 rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
785 /* Exit if the response is not expected */
790 while (checkcount--) {
792 rio_read_config_32(rdev,
793 rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
795 if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
805 * rio_clr_err_stopped - Clears port Error-stopped states.
806 * @rdev: Pointer to RIO device control structure
807 * @pnum: Switch port number to clear errors
808 * @err_status: port error status (if 0 reads register from device)
810 static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
812 struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
814 u32 far_ackid, far_linkstat, near_ackid;
817 rio_read_config_32(rdev,
818 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
821 if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
822 pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
824 * Send a Link-Request/Input-Status control symbol
826 if (rio_get_input_status(rdev, pnum, ®val)) {
827 pr_debug("RIO_EM: Input-status response timeout\n");
831 pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
833 far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
834 far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
835 rio_read_config_32(rdev,
836 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
838 pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
839 near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
840 pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
841 " near_ackID=0x%02x\n",
842 pnum, far_ackid, far_linkstat, near_ackid);
845 * If required, synchronize ackIDs of near and
848 if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
849 (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
850 /* Align near outstanding/outbound ackIDs with
853 rio_write_config_32(rdev,
854 rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
856 (far_ackid << 8) | far_ackid);
857 /* Align far outstanding/outbound ackIDs with
862 rio_write_config_32(nextdev,
863 nextdev->phys_efptr +
864 RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
866 (near_ackid << 8) | near_ackid);
868 pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
871 rio_read_config_32(rdev,
872 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
874 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
877 if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
878 pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
879 rio_get_input_status(nextdev,
880 RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
883 rio_read_config_32(rdev,
884 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
886 pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
889 return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
890 RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
894 * rio_inb_pwrite_handler - process inbound port-write message
895 * @pw_msg: pointer to inbound port-write message
897 * Processes an inbound port-write message. Returns 0 if the request
898 * has been satisfied.
900 int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
902 struct rio_dev *rdev;
903 u32 err_status, em_perrdet, em_ltlerrdet;
906 rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
908 /* Device removed or enumeration error */
909 pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
910 __func__, pw_msg->em.comptag);
914 pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
919 for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
920 pr_debug("0x%02x: %08x %08x %08x %08x\n",
921 i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
922 pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
928 /* Call an external service function (if such is registered
929 * for this device). This may be the service for endpoints that send
930 * device-specific port-write messages. End-point messages expected
931 * to be handled completely by EP specific device driver.
932 * For switches rc==0 signals that no standard processing required.
934 if (rdev->pwcback != NULL) {
935 rc = rdev->pwcback(rdev, pw_msg, 0);
940 portnum = pw_msg->em.is_port & 0xFF;
942 /* Check if device and route to it are functional:
943 * Sometimes devices may send PW message(s) just before being
944 * powered down (or link being lost).
946 if (rio_chk_dev_access(rdev)) {
947 pr_debug("RIO: device access failed - get link partner\n");
948 /* Scan route to the device and identify failed link.
949 * This will replace device and port reported in PW message.
950 * PW message should not be used after this point.
952 if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
953 pr_err("RIO: Route trace for %s failed\n",
960 /* For End-point devices processing stops here */
961 if (!(rdev->pef & RIO_PEF_SWITCH))
964 if (rdev->phys_efptr == 0) {
965 pr_err("RIO_PW: Bad switch initialization for %s\n",
971 * Process the port-write notification from switch
973 if (rdev->rswitch->em_handle)
974 rdev->rswitch->em_handle(rdev, portnum);
976 rio_read_config_32(rdev,
977 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
979 pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
981 if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
983 if (!(rdev->rswitch->port_ok & (1 << portnum))) {
984 rdev->rswitch->port_ok |= (1 << portnum);
985 rio_set_port_lockout(rdev, portnum, 0);
986 /* Schedule Insertion Service */
987 pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
988 rio_name(rdev), portnum);
991 /* Clear error-stopped states (if reported).
992 * Depending on the link partner state, two attempts
993 * may be needed for successful recovery.
995 if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
996 RIO_PORT_N_ERR_STS_PW_INP_ES)) {
997 if (rio_clr_err_stopped(rdev, portnum, err_status))
998 rio_clr_err_stopped(rdev, portnum, 0);
1000 } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
1002 if (rdev->rswitch->port_ok & (1 << portnum)) {
1003 rdev->rswitch->port_ok &= ~(1 << portnum);
1004 rio_set_port_lockout(rdev, portnum, 1);
1006 rio_write_config_32(rdev,
1008 RIO_PORT_N_ACK_STS_CSR(portnum),
1009 RIO_PORT_N_ACK_CLEAR);
1011 /* Schedule Extraction Service */
1012 pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
1013 rio_name(rdev), portnum);
1017 rio_read_config_32(rdev,
1018 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
1020 pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
1021 portnum, em_perrdet);
1022 /* Clear EM Port N Error Detect CSR */
1023 rio_write_config_32(rdev,
1024 rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
1027 rio_read_config_32(rdev,
1028 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
1030 pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
1032 /* Clear EM L/T Layer Error Detect CSR */
1033 rio_write_config_32(rdev,
1034 rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
1037 /* Clear remaining error bits and Port-Write Pending bit */
1038 rio_write_config_32(rdev,
1039 rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
1044 EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
1047 * rio_mport_get_efb - get pointer to next extended features block
1048 * @port: Master port to issue transaction
1049 * @local: Indicate a local master port or remote device access
1050 * @destid: Destination ID of the device
1051 * @hopcount: Number of switch hops to the device
1052 * @from: Offset of current Extended Feature block header (if 0 starts
1053 * from ExtFeaturePtr)
1056 rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
1057 u8 hopcount, u32 from)
1063 rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
1066 rio_mport_read_config_32(port, destid, hopcount,
1067 RIO_ASM_INFO_CAR, ®_val);
1068 return reg_val & RIO_EXT_FTR_PTR_MASK;
1071 rio_local_read_config_32(port, from, ®_val);
1073 rio_mport_read_config_32(port, destid, hopcount,
1075 return RIO_GET_BLOCK_ID(reg_val);
1078 EXPORT_SYMBOL_GPL(rio_mport_get_efb);
1081 * rio_mport_get_feature - query for devices' extended features
1082 * @port: Master port to issue transaction
1083 * @local: Indicate a local master port or remote device access
1084 * @destid: Destination ID of the device
1085 * @hopcount: Number of switch hops to the device
1086 * @ftr: Extended feature code
1088 * Tell if a device supports a given RapidIO capability.
1089 * Returns the offset of the requested extended feature
1090 * block within the device's RIO configuration space or
1091 * 0 in case the device does not support it. Possible
1094 * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
1096 * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
1098 * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
1100 * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
1102 * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
1104 * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
1107 rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
1108 u8 hopcount, int ftr)
1110 u32 asm_info, ext_ftr_ptr, ftr_header;
1113 rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
1115 rio_mport_read_config_32(port, destid, hopcount,
1116 RIO_ASM_INFO_CAR, &asm_info);
1118 ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
1120 while (ext_ftr_ptr) {
1122 rio_local_read_config_32(port, ext_ftr_ptr,
1125 rio_mport_read_config_32(port, destid, hopcount,
1126 ext_ftr_ptr, &ftr_header);
1127 if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
1129 if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
1135 EXPORT_SYMBOL_GPL(rio_mport_get_feature);
1138 * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
1139 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1140 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1141 * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
1142 * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
1143 * @from: Previous RIO device found in search, or %NULL for new search
1145 * Iterates through the list of known RIO devices. If a RIO device is
1146 * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
1147 * count to the device is incrememted and a pointer to its device
1148 * structure is returned. Otherwise, %NULL is returned. A new search
1149 * is initiated by passing %NULL to the @from argument. Otherwise, if
1150 * @from is not %NULL, searches continue from next device on the global
1151 * list. The reference count for @from is always decremented if it is
1154 struct rio_dev *rio_get_asm(u16 vid, u16 did,
1155 u16 asm_vid, u16 asm_did, struct rio_dev *from)
1157 struct list_head *n;
1158 struct rio_dev *rdev;
1160 WARN_ON(in_interrupt());
1161 spin_lock(&rio_global_list_lock);
1162 n = from ? from->global_list.next : rio_devices.next;
1164 while (n && (n != &rio_devices)) {
1165 rdev = rio_dev_g(n);
1166 if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
1167 (did == RIO_ANY_ID || rdev->did == did) &&
1168 (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
1169 (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
1176 rdev = rio_dev_get(rdev);
1177 spin_unlock(&rio_global_list_lock);
1182 * rio_get_device - Begin or continue searching for a RIO device by vid/did
1183 * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
1184 * @did: RIO did to match or %RIO_ANY_ID to match all dids
1185 * @from: Previous RIO device found in search, or %NULL for new search
1187 * Iterates through the list of known RIO devices. If a RIO device is
1188 * found with a matching @vid and @did, the reference count to the
1189 * device is incrememted and a pointer to its device structure is returned.
1190 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
1191 * to the @from argument. Otherwise, if @from is not %NULL, searches
1192 * continue from next device on the global list. The reference count for
1193 * @from is always decremented if it is not %NULL.
1195 struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
1197 return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
1201 * rio_std_route_add_entry - Add switch route table entry using standard
1202 * registers defined in RIO specification rev.1.3
1203 * @mport: Master port to issue transaction
1204 * @destid: Destination ID of the device
1205 * @hopcount: Number of switch hops to the device
1206 * @table: routing table ID (global or port-specific)
1207 * @route_destid: destID entry in the RT
1208 * @route_port: destination port for specified destID
1210 int rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1211 u16 table, u16 route_destid, u8 route_port)
1213 if (table == RIO_GLOBAL_TABLE) {
1214 rio_mport_write_config_32(mport, destid, hopcount,
1215 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1217 rio_mport_write_config_32(mport, destid, hopcount,
1218 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1227 * rio_std_route_get_entry - Read switch route table entry (port number)
1228 * associated with specified destID using standard registers defined in RIO
1229 * specification rev.1.3
1230 * @mport: Master port to issue transaction
1231 * @destid: Destination ID of the device
1232 * @hopcount: Number of switch hops to the device
1233 * @table: routing table ID (global or port-specific)
1234 * @route_destid: destID entry in the RT
1235 * @route_port: returned destination port for specified destID
1237 int rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
1238 u16 table, u16 route_destid, u8 *route_port)
1242 if (table == RIO_GLOBAL_TABLE) {
1243 rio_mport_write_config_32(mport, destid, hopcount,
1244 RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
1245 rio_mport_read_config_32(mport, destid, hopcount,
1246 RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
1248 *route_port = (u8)result;
1255 * rio_std_route_clr_table - Clear swotch route table using standard registers
1256 * defined in RIO specification rev.1.3.
1257 * @mport: Master port to issue transaction
1258 * @destid: Destination ID of the device
1259 * @hopcount: Number of switch hops to the device
1260 * @table: routing table ID (global or port-specific)
1262 int rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
1265 u32 max_destid = 0xff;
1266 u32 i, pef, id_inc = 1, ext_cfg = 0;
1267 u32 port_sel = RIO_INVALID_ROUTE;
1269 if (table == RIO_GLOBAL_TABLE) {
1270 rio_mport_read_config_32(mport, destid, hopcount,
1273 if (mport->sys_size) {
1274 rio_mport_read_config_32(mport, destid, hopcount,
1275 RIO_SWITCH_RT_LIMIT,
1277 max_destid &= RIO_RT_MAX_DESTID;
1280 if (pef & RIO_PEF_EXT_RT) {
1281 ext_cfg = 0x80000000;
1283 port_sel = (RIO_INVALID_ROUTE << 24) |
1284 (RIO_INVALID_ROUTE << 16) |
1285 (RIO_INVALID_ROUTE << 8) |
1289 for (i = 0; i <= max_destid;) {
1290 rio_mport_write_config_32(mport, destid, hopcount,
1291 RIO_STD_RTE_CONF_DESTID_SEL_CSR,
1293 rio_mport_write_config_32(mport, destid, hopcount,
1294 RIO_STD_RTE_CONF_PORT_SEL_CSR,
1304 #ifdef CONFIG_RAPIDIO_DMA_ENGINE
1306 static bool rio_chan_filter(struct dma_chan *chan, void *arg)
1308 struct rio_dev *rdev = arg;
1310 /* Check that DMA device belongs to the right MPORT */
1311 return (rdev->net->hport ==
1312 container_of(chan->device, struct rio_mport, dma));
1316 * rio_request_dma - request RapidIO capable DMA channel that supports
1317 * specified target RapidIO device.
1318 * @rdev: RIO device control structure
1320 * Returns pointer to allocated DMA channel or NULL if failed.
1322 struct dma_chan *rio_request_dma(struct rio_dev *rdev)
1324 dma_cap_mask_t mask;
1325 struct dma_chan *dchan;
1328 dma_cap_set(DMA_SLAVE, mask);
1329 dchan = dma_request_channel(mask, rio_chan_filter, rdev);
1333 EXPORT_SYMBOL_GPL(rio_request_dma);
1336 * rio_release_dma - release specified DMA channel
1337 * @dchan: DMA channel to release
1339 void rio_release_dma(struct dma_chan *dchan)
1341 dma_release_channel(dchan);
1343 EXPORT_SYMBOL_GPL(rio_release_dma);
1346 * rio_dma_prep_slave_sg - RapidIO specific wrapper
1347 * for device_prep_slave_sg callback defined by DMAENGINE.
1348 * @rdev: RIO device control structure
1349 * @dchan: DMA channel to configure
1350 * @data: RIO specific data descriptor
1351 * @direction: DMA data transfer direction (TO or FROM the device)
1352 * @flags: dmaengine defined flags
1354 * Initializes RapidIO capable DMA channel for the specified data transfer.
1355 * Uses DMA channel private extension to pass information related to remote
1356 * target RIO device.
1357 * Returns pointer to DMA transaction descriptor or NULL if failed.
1359 struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
1360 struct dma_chan *dchan, struct rio_dma_data *data,
1361 enum dma_transfer_direction direction, unsigned long flags)
1363 struct dma_async_tx_descriptor *txd = NULL;
1364 struct rio_dma_ext rio_ext;
1366 if (dchan->device->device_prep_slave_sg == NULL) {
1367 pr_err("%s: prep_rio_sg == NULL\n", __func__);
1371 rio_ext.destid = rdev->destid;
1372 rio_ext.rio_addr_u = data->rio_addr_u;
1373 rio_ext.rio_addr = data->rio_addr;
1374 rio_ext.wr_type = data->wr_type;
1376 txd = dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
1377 direction, flags, &rio_ext);
1381 EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
1383 #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
1386 * rio_find_mport - find RIO mport by its ID
1387 * @mport_id: number (ID) of mport device
1389 * Given a RIO mport number, the desired mport is located
1390 * in the global list of mports. If the mport is found, a pointer to its
1391 * data structure is returned. If no mport is found, %NULL is returned.
1393 struct rio_mport *rio_find_mport(int mport_id)
1395 struct rio_mport *port;
1397 mutex_lock(&rio_mport_list_lock);
1398 list_for_each_entry(port, &rio_mports, node) {
1399 if (port->id == mport_id)
1404 mutex_unlock(&rio_mport_list_lock);
1410 * rio_register_scan - enumeration/discovery method registration interface
1411 * @mport_id: mport device ID for which fabric scan routine has to be set
1412 * (RIO_MPORT_ANY = set for all available mports)
1413 * @scan_ops: enumeration/discovery control structure
1415 * Assigns enumeration or discovery method to the specified mport device (or all
1416 * available mports if RIO_MPORT_ANY is specified).
1417 * Returns error if the mport already has an enumerator attached to it.
1418 * In case of RIO_MPORT_ANY ignores ports with valid scan routines and returns
1419 * an error if was unable to find at least one available mport.
1421 int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
1423 struct rio_mport *port;
1426 mutex_lock(&rio_mport_list_lock);
1427 list_for_each_entry(port, &rio_mports, node) {
1428 if (port->id == mport_id || mport_id == RIO_MPORT_ANY) {
1429 if (port->nscan && mport_id == RIO_MPORT_ANY)
1431 else if (port->nscan)
1434 port->nscan = scan_ops;
1437 if (mport_id != RIO_MPORT_ANY)
1441 mutex_unlock(&rio_mport_list_lock);
1445 EXPORT_SYMBOL_GPL(rio_register_scan);
1448 * rio_unregister_scan - removes enumeration/discovery method from mport
1449 * @mport_id: mport device ID for which fabric scan routine has to be
1450 * unregistered (RIO_MPORT_ANY = set for all available mports)
1452 * Removes enumeration or discovery method assigned to the specified mport
1453 * device (or all available mports if RIO_MPORT_ANY is specified).
1455 int rio_unregister_scan(int mport_id)
1457 struct rio_mport *port;
1459 mutex_lock(&rio_mport_list_lock);
1460 list_for_each_entry(port, &rio_mports, node) {
1461 if (port->id == mport_id || mport_id == RIO_MPORT_ANY) {
1464 if (mport_id != RIO_MPORT_ANY)
1468 mutex_unlock(&rio_mport_list_lock);
1472 EXPORT_SYMBOL_GPL(rio_unregister_scan);
1474 static void rio_fixup_device(struct rio_dev *dev)
1478 static int rio_init(void)
1480 struct rio_dev *dev = NULL;
1482 while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
1483 rio_fixup_device(dev);
1488 static struct workqueue_struct *rio_wq;
1490 struct rio_disc_work {
1491 struct work_struct work;
1492 struct rio_mport *mport;
1495 static void disc_work_handler(struct work_struct *_work)
1497 struct rio_disc_work *work;
1499 work = container_of(_work, struct rio_disc_work, work);
1500 pr_debug("RIO: discovery work for mport %d %s\n",
1501 work->mport->id, work->mport->name);
1502 work->mport->nscan->discover(work->mport, 0);
1505 int rio_init_mports(void)
1507 struct rio_mport *port;
1508 struct rio_disc_work *work;
1515 * First, run enumerations and check if we need to perform discovery
1516 * on any of the registered mports.
1518 mutex_lock(&rio_mport_list_lock);
1519 list_for_each_entry(port, &rio_mports, node) {
1520 if (port->host_deviceid >= 0) {
1522 port->nscan->enumerate(port, 0);
1526 mutex_unlock(&rio_mport_list_lock);
1532 * If we have mports that require discovery schedule a discovery work
1533 * for each of them. If the code below fails to allocate needed
1534 * resources, exit without error to keep results of enumeration
1536 * TODO: Implement restart of dicovery process for all or
1537 * individual discovering mports.
1539 rio_wq = alloc_workqueue("riodisc", 0, 0);
1541 pr_err("RIO: unable allocate rio_wq\n");
1545 work = kcalloc(n, sizeof *work, GFP_KERNEL);
1547 pr_err("RIO: no memory for work struct\n");
1548 destroy_workqueue(rio_wq);
1553 mutex_lock(&rio_mport_list_lock);
1554 list_for_each_entry(port, &rio_mports, node) {
1555 if (port->host_deviceid < 0 && port->nscan) {
1556 work[n].mport = port;
1557 INIT_WORK(&work[n].work, disc_work_handler);
1558 queue_work(rio_wq, &work[n].work);
1562 mutex_unlock(&rio_mport_list_lock);
1564 flush_workqueue(rio_wq);
1565 pr_debug("RIO: destroy discovery workqueue\n");
1566 destroy_workqueue(rio_wq);
1575 static int hdids[RIO_MAX_MPORTS + 1];
1577 static int rio_get_hdid(int index)
1579 if (!hdids[0] || hdids[0] <= index || index >= RIO_MAX_MPORTS)
1582 return hdids[index + 1];
1585 static int rio_hdid_setup(char *str)
1587 (void)get_options(str, ARRAY_SIZE(hdids), hdids);
1591 __setup("riohdid=", rio_hdid_setup);
1593 int rio_register_mport(struct rio_mport *port)
1595 if (next_portid >= RIO_MAX_MPORTS) {
1596 pr_err("RIO: reached specified max number of mports\n");
1600 port->id = next_portid++;
1601 port->host_deviceid = rio_get_hdid(port->id);
1603 mutex_lock(&rio_mport_list_lock);
1604 list_add_tail(&port->node, &rio_mports);
1605 mutex_unlock(&rio_mport_list_lock);
1609 EXPORT_SYMBOL_GPL(rio_local_get_device_id);
1610 EXPORT_SYMBOL_GPL(rio_get_device);
1611 EXPORT_SYMBOL_GPL(rio_get_asm);
1612 EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
1613 EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
1614 EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
1615 EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
1616 EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
1617 EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
1618 EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
1619 EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
1620 EXPORT_SYMBOL_GPL(rio_init_mports);