2 * RapidIO enumeration and discovery support
4 * Copyright 2005 MontaVista Software, Inc.
5 * Matt Porter <mporter@kernel.crashing.org>
7 * Copyright 2009 Integrated Device Technology, Inc.
8 * Alex Bounine <alexandre.bounine@idt.com>
9 * - Added Port-Write/Error Management initialization and handling
11 * Copyright 2009 Sysgo AG
12 * Thomas Moll <thomas.moll@sysgo.com>
13 * - Added Input- Output- enable functionality, to allow full communication
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
21 #include <linux/types.h>
22 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/init.h>
27 #include <linux/rio.h>
28 #include <linux/rio_drv.h>
29 #include <linux/rio_ids.h>
30 #include <linux/rio_regs.h>
31 #include <linux/module.h>
32 #include <linux/spinlock.h>
33 #include <linux/timer.h>
34 #include <linux/sched.h>
35 #include <linux/jiffies.h>
36 #include <linux/slab.h>
40 LIST_HEAD(rio_devices);
42 static void rio_init_em(struct rio_dev *rdev);
44 DEFINE_SPINLOCK(rio_global_list_lock);
46 static int next_destid = 0;
47 static int next_comptag = 1;
49 static int rio_mport_phys_table[] = {
51 RIO_EFB_PAR_EP_REC_ID,
53 RIO_EFB_SER_EP_REC_ID,
59 * rio_destid_alloc - Allocate next available destID for given network
62 * Returns next available device destination ID for the specified RIO network.
63 * Marks allocated ID as one in use.
64 * Returns RIO_INVALID_DESTID if new destID is not available.
66 static u16 rio_destid_alloc(struct rio_net *net)
69 struct rio_id_table *idtab = &net->destid_table;
71 spin_lock(&idtab->lock);
72 destid = find_next_zero_bit(idtab->table, idtab->max, idtab->next);
73 if (destid >= idtab->max)
74 destid = find_first_zero_bit(idtab->table, idtab->max);
76 if (destid < idtab->max) {
77 idtab->next = destid + 1;
78 if (idtab->next >= idtab->max)
80 set_bit(destid, idtab->table);
81 destid += idtab->start;
83 destid = RIO_INVALID_DESTID;
85 spin_unlock(&idtab->lock);
90 * rio_destid_reserve - Reserve the specivied destID
92 * destid: destID to reserve
94 * Tries to reserve the specified destID.
95 * Returns 0 if successfull.
97 static int rio_destid_reserve(struct rio_net *net, u16 destid)
100 struct rio_id_table *idtab = &net->destid_table;
102 destid -= idtab->start;
103 spin_lock(&idtab->lock);
104 oldbit = test_and_set_bit(destid, idtab->table);
105 spin_unlock(&idtab->lock);
110 * rio_destid_free - free a previously allocated destID
112 * destid: destID to free
114 * Makes the specified destID available for use.
116 static void rio_destid_free(struct rio_net *net, u16 destid)
118 struct rio_id_table *idtab = &net->destid_table;
120 destid -= idtab->start;
121 spin_lock(&idtab->lock);
122 clear_bit(destid, idtab->table);
123 spin_unlock(&idtab->lock);
127 * rio_destid_first - return first destID in use
130 static u16 rio_destid_first(struct rio_net *net)
133 struct rio_id_table *idtab = &net->destid_table;
135 spin_lock(&idtab->lock);
136 destid = find_first_bit(idtab->table, idtab->max);
137 if (destid >= idtab->max)
138 destid = RIO_INVALID_DESTID;
140 destid += idtab->start;
141 spin_unlock(&idtab->lock);
146 * rio_destid_next - return next destID in use
148 * from: destination ID from which search shall continue
150 static u16 rio_destid_next(struct rio_net *net, u16 from)
153 struct rio_id_table *idtab = &net->destid_table;
155 spin_lock(&idtab->lock);
156 destid = find_next_bit(idtab->table, idtab->max, from);
157 if (destid >= idtab->max)
158 destid = RIO_INVALID_DESTID;
160 destid += idtab->start;
161 spin_unlock(&idtab->lock);
166 * rio_get_device_id - Get the base/extended device id for a device
167 * @port: RIO master port
168 * @destid: Destination ID of device
169 * @hopcount: Hopcount to device
171 * Reads the base/extended device id from a device. Returns the
172 * 8/16-bit device ID.
174 static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
178 rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
180 return RIO_GET_DID(port->sys_size, result);
184 * rio_set_device_id - Set the base/extended device id for a device
185 * @port: RIO master port
186 * @destid: Destination ID of device
187 * @hopcount: Hopcount to device
188 * @did: Device ID value to be written
190 * Writes the base/extended device id from a device.
192 static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
194 rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
195 RIO_SET_DID(port->sys_size, did));
199 * rio_local_set_device_id - Set the base/extended device id for a port
200 * @port: RIO master port
201 * @did: Device ID value to be written
203 * Writes the base/extended device id from a device.
205 static void rio_local_set_device_id(struct rio_mport *port, u16 did)
207 rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
212 * rio_clear_locks- Release all host locks and signal enumeration complete
213 * @net: RIO network to run on
215 * Marks the component tag CSR on each device with the enumeration
216 * complete flag. When complete, it then release the host locks on
217 * each device. Returns 0 on success or %-EINVAL on failure.
219 static int rio_clear_locks(struct rio_net *net)
221 struct rio_mport *port = net->hport;
222 struct rio_dev *rdev;
226 /* Release host device id locks */
227 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
228 port->host_deviceid);
229 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
230 if ((result & 0xffff) != 0xffff) {
232 "RIO: badness when releasing host lock on master port, result %8.8x\n",
236 list_for_each_entry(rdev, &net->devices, net_list) {
237 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
238 port->host_deviceid);
239 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
240 if ((result & 0xffff) != 0xffff) {
242 "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
243 rdev->vid, rdev->did);
247 /* Mark device as discovered and enable master */
248 rio_read_config_32(rdev,
249 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
251 result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
252 rio_write_config_32(rdev,
253 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
261 * rio_enum_host- Set host lock and initialize host destination ID
262 * @port: Master port to issue transaction
264 * Sets the local host master port lock and destination ID register
265 * with the host device ID value. The host device ID value is provided
266 * by the platform. Returns %0 on success or %-1 on failure.
268 static int rio_enum_host(struct rio_mport *port)
272 /* Set master port host device id lock */
273 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
274 port->host_deviceid);
276 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
277 if ((result & 0xffff) != port->host_deviceid)
280 /* Set master port destid and init destid ctr */
281 rio_local_set_device_id(port, port->host_deviceid);
286 * rio_device_has_destid- Test if a device contains a destination ID register
287 * @port: Master port to issue transaction
288 * @src_ops: RIO device source operations
289 * @dst_ops: RIO device destination operations
291 * Checks the provided @src_ops and @dst_ops for the necessary transaction
292 * capabilities that indicate whether or not a device will implement a
293 * destination ID register. Returns 1 if true or 0 if false.
295 static int rio_device_has_destid(struct rio_mport *port, int src_ops,
298 u32 mask = RIO_OPS_READ | RIO_OPS_WRITE | RIO_OPS_ATOMIC_TST_SWP | RIO_OPS_ATOMIC_INC | RIO_OPS_ATOMIC_DEC | RIO_OPS_ATOMIC_SET | RIO_OPS_ATOMIC_CLR;
300 return !!((src_ops | dst_ops) & mask);
304 * rio_release_dev- Frees a RIO device struct
305 * @dev: LDM device associated with a RIO device struct
307 * Gets the RIO device struct associated a RIO device struct.
308 * The RIO device struct is freed.
310 static void rio_release_dev(struct device *dev)
312 struct rio_dev *rdev;
314 rdev = to_rio_dev(dev);
319 * rio_is_switch- Tests if a RIO device has switch capabilities
322 * Gets the RIO device Processing Element Features register
323 * contents and tests for switch capabilities. Returns 1 if
324 * the device is a switch or 0 if it is not a switch.
325 * The RIO device struct is freed.
327 static int rio_is_switch(struct rio_dev *rdev)
329 if (rdev->pef & RIO_PEF_SWITCH)
335 * rio_switch_init - Sets switch operations for a particular vendor switch
337 * @do_enum: Enumeration/Discovery mode flag
339 * Searches the RIO switch ops table for known switch types. If the vid
340 * and did match a switch table entry, then call switch initialization
341 * routine to setup switch-specific routines.
343 static void rio_switch_init(struct rio_dev *rdev, int do_enum)
345 struct rio_switch_ops *cur = __start_rio_switch_ops;
346 struct rio_switch_ops *end = __end_rio_switch_ops;
349 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
350 pr_debug("RIO: calling init routine for %s\n",
352 cur->init_hook(rdev, do_enum);
358 if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
359 pr_debug("RIO: adding STD routing ops for %s\n",
361 rdev->rswitch->add_entry = rio_std_route_add_entry;
362 rdev->rswitch->get_entry = rio_std_route_get_entry;
363 rdev->rswitch->clr_table = rio_std_route_clr_table;
366 if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
367 printk(KERN_ERR "RIO: missing routing ops for %s\n",
372 * rio_add_device- Adds a RIO device to the device model
375 * Adds the RIO device to the global device list and adds the RIO
376 * device to the RIO device list. Creates the generic sysfs nodes
379 static int __devinit rio_add_device(struct rio_dev *rdev)
383 err = device_add(&rdev->dev);
387 spin_lock(&rio_global_list_lock);
388 list_add_tail(&rdev->global_list, &rio_devices);
389 spin_unlock(&rio_global_list_lock);
391 rio_create_sysfs_dev_files(rdev);
397 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
399 * @port: Master port associated with the RIO network
400 * @local: local=1 select local port otherwise a far device is reached
401 * @destid: Destination ID of the device to check host bit
402 * @hopcount: Number of hops to reach the target
403 * @port_num: Port (-number on switch) to enable on a far end device
405 * Returns 0 or 1 from on General Control Command and Status Register
408 inline int rio_enable_rx_tx_port(struct rio_mport *port,
409 int local, u16 destid,
410 u8 hopcount, u8 port_num) {
411 #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
416 * enable rx input tx output port
418 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
419 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
421 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
424 rio_local_read_config_32(port, ext_ftr_ptr +
425 RIO_PORT_N_CTL_CSR(0),
428 if (rio_mport_read_config_32(port, destid, hopcount,
429 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), ®val) < 0)
433 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
435 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
436 | RIO_PORT_N_CTL_EN_TX_SER;
439 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
440 | RIO_PORT_N_CTL_EN_TX_PAR;
444 rio_local_write_config_32(port, ext_ftr_ptr +
445 RIO_PORT_N_CTL_CSR(0), regval);
447 if (rio_mport_write_config_32(port, destid, hopcount,
448 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
456 * rio_setup_device- Allocates and sets up a RIO device
458 * @port: Master port to send transactions
459 * @destid: Current destination ID
460 * @hopcount: Current hopcount
461 * @do_enum: Enumeration/Discovery mode flag
463 * Allocates a RIO device and configures fields based on configuration
464 * space contents. If device has a destination ID register, a destination
465 * ID is either assigned in enumeration mode or read from configuration
466 * space in discovery mode. If the device has switch capabilities, then
467 * a switch is allocated and configured appropriately. Returns a pointer
468 * to a RIO device on success or NULL on failure.
471 static struct rio_dev __devinit *rio_setup_device(struct rio_net *net,
472 struct rio_mport *port, u16 destid,
473 u8 hopcount, int do_enum)
476 struct rio_dev *rdev;
477 struct rio_switch *rswitch = NULL;
482 size = sizeof(struct rio_dev);
483 if (rio_mport_read_config_32(port, destid, hopcount,
484 RIO_PEF_CAR, &result))
487 if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
488 rio_mport_read_config_32(port, destid, hopcount,
489 RIO_SWP_INFO_CAR, &swpinfo);
490 if (result & RIO_PEF_SWITCH) {
491 size += (RIO_GET_TOTAL_PORTS(swpinfo) *
492 sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
496 rdev = kzalloc(size, GFP_KERNEL);
502 rdev->swpinfo = swpinfo;
503 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
505 rdev->did = result >> 16;
506 rdev->vid = result & 0xffff;
507 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
509 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
511 rdev->asm_did = result >> 16;
512 rdev->asm_vid = result & 0xffff;
513 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
515 rdev->asm_rev = result >> 16;
516 if (rdev->pef & RIO_PEF_EXT_FEATURES) {
517 rdev->efptr = result & 0xffff;
518 rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
521 rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
522 hopcount, RIO_EFB_ERR_MGMNT);
525 rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
527 rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
531 /* Assign component tag to device */
532 if (next_comptag >= 0x10000) {
533 pr_err("RIO: Component Tag Counter Overflow\n");
536 rio_mport_write_config_32(port, destid, hopcount,
537 RIO_COMPONENT_TAG_CSR, next_comptag);
538 rdev->comp_tag = next_comptag++;
540 rio_mport_read_config_32(port, destid, hopcount,
541 RIO_COMPONENT_TAG_CSR,
545 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
547 rio_set_device_id(port, destid, hopcount, next_destid);
548 rdev->destid = next_destid;
549 next_destid = rio_destid_alloc(net);
551 rdev->destid = rio_get_device_id(port, destid, hopcount);
553 rdev->hopcount = 0xff;
555 /* Switch device has an associated destID which
556 * will be adjusted later
558 rdev->destid = destid;
559 rdev->hopcount = hopcount;
562 /* If a PE has both switch and other functions, show it as a switch */
563 if (rio_is_switch(rdev)) {
564 rswitch = rdev->rswitch;
565 rswitch->switchid = rdev->comp_tag & RIO_CTAG_UDEVID;
566 rswitch->port_ok = 0;
567 rswitch->route_table = kzalloc(sizeof(u8)*
568 RIO_MAX_ROUTE_ENTRIES(port->sys_size),
570 if (!rswitch->route_table)
572 /* Initialize switch route table */
573 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
575 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
576 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
578 rio_switch_init(rdev, do_enum);
580 if (do_enum && rswitch->clr_table)
581 rswitch->clr_table(port, destid, hopcount,
584 list_add_tail(&rswitch->node, &net->switches);
588 /*Enable Input Output Port (transmitter reviever)*/
589 rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
591 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
595 rdev->dev.bus = &rio_bus_type;
596 rdev->dev.parent = &rio_bus;
598 device_initialize(&rdev->dev);
599 rdev->dev.release = rio_release_dev;
602 rdev->dma_mask = DMA_BIT_MASK(32);
603 rdev->dev.dma_mask = &rdev->dma_mask;
604 rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
606 if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
607 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
610 ret = rio_add_device(rdev);
618 kfree(rswitch->route_table);
625 * rio_sport_is_active- Tests if a switch port has an active connection.
626 * @port: Master port to send transaction
627 * @destid: Associated destination ID for switch
628 * @hopcount: Hopcount to reach switch
629 * @sport: Switch port number
631 * Reads the port error status CSR for a particular switch port to
632 * determine if the port has an active link. Returns
633 * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
637 rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
642 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
644 while (ext_ftr_ptr) {
645 rio_mport_read_config_32(port, destid, hopcount,
646 ext_ftr_ptr, &result);
647 result = RIO_GET_BLOCK_ID(result);
648 if ((result == RIO_EFB_SER_EP_FREE_ID) ||
649 (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
650 (result == RIO_EFB_SER_EP_FREC_ID))
653 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
658 rio_mport_read_config_32(port, destid, hopcount,
660 RIO_PORT_N_ERR_STS_CSR(sport),
663 return result & RIO_PORT_N_ERR_STS_PORT_OK;
667 * rio_lock_device - Acquires host device lock for specified device
668 * @port: Master port to send transaction
669 * @destid: Destination ID for device/switch
670 * @hopcount: Hopcount to reach switch
671 * @wait_ms: Max wait time in msec (0 = no timeout)
673 * Attepts to acquire host device lock for specified device
674 * Returns 0 if device lock acquired or EINVAL if timeout expires.
677 rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
682 /* Attempt to acquire device lock */
683 rio_mport_write_config_32(port, destid, hopcount,
684 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
685 rio_mport_read_config_32(port, destid, hopcount,
686 RIO_HOST_DID_LOCK_CSR, &result);
688 while (result != port->host_deviceid) {
689 if (wait_ms != 0 && tcnt == wait_ms) {
690 pr_debug("RIO: timeout when locking device %x:%x\n",
698 /* Try to acquire device lock again */
699 rio_mport_write_config_32(port, destid,
701 RIO_HOST_DID_LOCK_CSR,
702 port->host_deviceid);
703 rio_mport_read_config_32(port, destid,
705 RIO_HOST_DID_LOCK_CSR, &result);
712 * rio_unlock_device - Releases host device lock for specified device
713 * @port: Master port to send transaction
714 * @destid: Destination ID for device/switch
715 * @hopcount: Hopcount to reach switch
717 * Returns 0 if device lock released or EINVAL if fails.
720 rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
724 /* Release device lock */
725 rio_mport_write_config_32(port, destid,
727 RIO_HOST_DID_LOCK_CSR,
728 port->host_deviceid);
729 rio_mport_read_config_32(port, destid, hopcount,
730 RIO_HOST_DID_LOCK_CSR, &result);
731 if ((result & 0xffff) != 0xffff) {
732 pr_debug("RIO: badness when releasing device lock %x:%x\n",
741 * rio_route_add_entry- Add a route entry to a switch routing table
743 * @table: Routing table ID
744 * @route_destid: Destination ID to be routed
745 * @route_port: Port number to be routed
746 * @lock: lock switch device flag
748 * Calls the switch specific add_entry() method to add a route entry
749 * on a switch. The route table can be specified using the @table
750 * argument if a switch has per port routing tables or the normal
751 * use is to specific all tables (or the global table) by passing
752 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
756 rio_route_add_entry(struct rio_dev *rdev,
757 u16 table, u16 route_destid, u8 route_port, int lock)
762 rc = rio_lock_device(rdev->net->hport, rdev->destid,
763 rdev->hopcount, 1000);
768 rc = rdev->rswitch->add_entry(rdev->net->hport, rdev->destid,
769 rdev->hopcount, table,
770 route_destid, route_port);
772 rio_unlock_device(rdev->net->hport, rdev->destid,
779 * rio_route_get_entry- Read a route entry in a switch routing table
781 * @table: Routing table ID
782 * @route_destid: Destination ID to be routed
783 * @route_port: Pointer to read port number into
784 * @lock: lock switch device flag
786 * Calls the switch specific get_entry() method to read a route entry
787 * in a switch. The route table can be specified using the @table
788 * argument if a switch has per port routing tables or the normal
789 * use is to specific all tables (or the global table) by passing
790 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
794 rio_route_get_entry(struct rio_dev *rdev, u16 table,
795 u16 route_destid, u8 *route_port, int lock)
800 rc = rio_lock_device(rdev->net->hport, rdev->destid,
801 rdev->hopcount, 1000);
806 rc = rdev->rswitch->get_entry(rdev->net->hport, rdev->destid,
807 rdev->hopcount, table,
808 route_destid, route_port);
810 rio_unlock_device(rdev->net->hport, rdev->destid,
817 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
818 * @port: Master port to send transaction
819 * @hopcount: Number of hops to the device
821 * Used during enumeration to read the Host Device ID Lock CSR on a
822 * RIO device. Returns the value of the lock register.
824 static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
828 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
829 RIO_HOST_DID_LOCK_CSR, &result);
831 return (u16) (result & 0xffff);
835 * rio_enum_peer- Recursively enumerate a RIO network through a master port
836 * @net: RIO network being enumerated
837 * @port: Master port to send transactions
838 * @hopcount: Number of hops into the network
839 * @prev: Previous RIO device connected to the enumerated one
840 * @prev_port: Port on previous RIO device
842 * Recursively enumerates a RIO network. Transactions are sent via the
843 * master port passed in @port.
845 static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
846 u8 hopcount, struct rio_dev *prev, int prev_port)
848 struct rio_dev *rdev;
852 if (rio_mport_chk_dev_access(port,
853 RIO_ANY_DESTID(port->sys_size), hopcount)) {
854 pr_debug("RIO: device access check failed\n");
858 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
859 pr_debug("RIO: PE already discovered by this host\n");
861 * Already discovered by this host. Add it as another
862 * link to the existing device.
864 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
865 hopcount, RIO_COMPONENT_TAG_CSR, ®val);
868 rdev = rio_get_comptag((regval & 0xffff), NULL);
870 if (rdev && prev && rio_is_switch(prev)) {
871 pr_debug("RIO: redundant path to %s\n",
873 prev->rswitch->nextdev[prev_port] = rdev;
880 /* Attempt to acquire device lock */
881 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
883 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
884 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
885 < port->host_deviceid) {
888 /* Attempt to acquire device lock again */
889 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
891 RIO_HOST_DID_LOCK_CSR,
892 port->host_deviceid);
895 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
897 "RIO: PE locked by a higher priority host...retreating\n");
901 /* Setup new RIO device */
902 rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
905 /* Add device to the global and bus/net specific list. */
906 list_add_tail(&rdev->net_list, &net->devices);
908 if (prev && rio_is_switch(prev))
909 prev->rswitch->nextdev[prev_port] = rdev;
913 if (rio_is_switch(rdev)) {
920 sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
921 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
922 port->host_deviceid, sw_inport, 0);
923 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
925 destid = rio_destid_first(net);
926 while (destid != RIO_INVALID_DESTID && destid < next_destid) {
927 if (destid != port->host_deviceid) {
928 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
929 destid, sw_inport, 0);
930 rdev->rswitch->route_table[destid] = sw_inport;
932 destid = rio_destid_next(net, destid + 1);
935 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
936 rio_name(rdev), rdev->vid, rdev->did,
937 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
938 sw_destid = next_destid;
940 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
942 if (sw_inport == port_num) {
943 rio_enable_rx_tx_port(port, 0,
944 RIO_ANY_DESTID(port->sys_size),
946 rdev->rswitch->port_ok |= (1 << port_num);
950 cur_destid = next_destid;
952 if (rio_sport_is_active
953 (port, RIO_ANY_DESTID(port->sys_size), hopcount,
956 "RIO: scanning device on port %d\n",
958 rio_enable_rx_tx_port(port, 0,
959 RIO_ANY_DESTID(port->sys_size),
961 rdev->rswitch->port_ok |= (1 << port_num);
962 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
963 RIO_ANY_DESTID(port->sys_size),
966 if (rio_enum_peer(net, port, hopcount + 1,
970 /* Update routing tables */
971 destid = rio_destid_next(net, cur_destid + 1);
972 if (destid != RIO_INVALID_DESTID) {
973 for (destid = cur_destid;
974 destid < next_destid;) {
975 if (destid != port->host_deviceid) {
976 rio_route_add_entry(rdev,
982 route_table[destid] =
985 destid = rio_destid_next(net,
990 /* If switch supports Error Management,
991 * set PORT_LOCKOUT bit for unused port
994 rio_set_port_lockout(rdev, port_num, 1);
996 rdev->rswitch->port_ok &= ~(1 << port_num);
1000 /* Direct Port-write messages to the enumeratiing host */
1001 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
1003 rio_write_config_32(rdev,
1004 rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
1005 (port->host_deviceid << 16) |
1006 (port->sys_size << 15));
1011 /* Check for empty switch */
1012 if (next_destid == sw_destid)
1013 next_destid = rio_destid_alloc(net);
1015 rdev->destid = sw_destid;
1017 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
1018 rio_name(rdev), rdev->vid, rdev->did);
1024 * rio_enum_complete- Tests if enumeration of a network is complete
1025 * @port: Master port to send transaction
1027 * Tests the PGCCSR discovered bit for non-zero value (enumeration
1028 * complete flag). Return %1 if enumeration is complete or %0 if
1029 * enumeration is incomplete.
1031 static int rio_enum_complete(struct rio_mport *port)
1035 rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
1037 return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
1041 * rio_disc_peer- Recursively discovers a RIO network through a master port
1042 * @net: RIO network being discovered
1043 * @port: Master port to send transactions
1044 * @destid: Current destination ID in network
1045 * @hopcount: Number of hops into the network
1046 * @prev: previous rio_dev
1047 * @prev_port: previous port number
1049 * Recursively discovers a RIO network. Transactions are sent via the
1050 * master port passed in @port.
1052 static int __devinit
1053 rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
1054 u8 hopcount, struct rio_dev *prev, int prev_port)
1056 u8 port_num, route_port;
1057 struct rio_dev *rdev;
1060 /* Setup new RIO device */
1061 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
1062 /* Add device to the global and bus/net specific list. */
1063 list_add_tail(&rdev->net_list, &net->devices);
1065 if (prev && rio_is_switch(prev))
1066 prev->rswitch->nextdev[prev_port] = rdev;
1070 if (rio_is_switch(rdev)) {
1071 /* Associated destid is how we accessed this switch */
1072 rdev->destid = destid;
1075 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
1076 rio_name(rdev), rdev->vid, rdev->did,
1077 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
1079 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
1081 if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
1084 if (rio_sport_is_active
1085 (port, destid, hopcount, port_num)) {
1087 "RIO: scanning device on port %d\n",
1090 rio_lock_device(port, destid, hopcount, 1000);
1093 ndestid < RIO_ANY_DESTID(port->sys_size);
1095 rio_route_get_entry(rdev,
1099 if (route_port == port_num)
1103 if (ndestid == RIO_ANY_DESTID(port->sys_size))
1105 rio_unlock_device(port, destid, hopcount);
1106 if (rio_disc_peer(net, port, ndestid,
1107 hopcount + 1, rdev, port_num) < 0)
1112 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
1113 rio_name(rdev), rdev->vid, rdev->did);
1119 * rio_mport_is_active- Tests if master port link is active
1120 * @port: Master port to test
1122 * Reads the port error status CSR for the master port to
1123 * determine if the port has an active link. Returns
1124 * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
1125 * or %0 if it is inactive.
1127 static int rio_mport_is_active(struct rio_mport *port)
1131 int *entry = rio_mport_phys_table;
1135 rio_mport_get_feature(port, 1, 0, 0, *entry)))
1137 } while (*++entry >= 0);
1140 rio_local_read_config_32(port,
1142 RIO_PORT_N_ERR_STS_CSR(port->index),
1145 return result & RIO_PORT_N_ERR_STS_PORT_OK;
1149 * rio_alloc_net- Allocate and configure a new RIO network
1150 * @port: Master port associated with the RIO network
1151 * @do_enum: Enumeration/Discovery mode flag
1152 * @start: logical minimal start id for new net
1154 * Allocates a RIO network structure, initializes per-network
1155 * list heads, and adds the associated master port to the
1156 * network list of associated master ports. Returns a
1157 * RIO network pointer on success or %NULL on failure.
1159 static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port,
1160 int do_enum, u16 start)
1162 struct rio_net *net;
1164 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
1165 if (net && do_enum) {
1166 net->destid_table.table = kzalloc(
1167 BITS_TO_LONGS(RIO_MAX_ROUTE_ENTRIES(port->sys_size)) *
1171 if (net->destid_table.table == NULL) {
1172 pr_err("RIO: failed to allocate destID table\n");
1176 net->destid_table.start = start;
1177 net->destid_table.next = 0;
1178 net->destid_table.max =
1179 RIO_MAX_ROUTE_ENTRIES(port->sys_size);
1180 spin_lock_init(&net->destid_table.lock);
1185 INIT_LIST_HEAD(&net->node);
1186 INIT_LIST_HEAD(&net->devices);
1187 INIT_LIST_HEAD(&net->switches);
1188 INIT_LIST_HEAD(&net->mports);
1189 list_add_tail(&port->nnode, &net->mports);
1197 * rio_update_route_tables- Updates route tables in switches
1198 * @net: RIO network to run update on
1200 * For each enumerated device, ensure that each switch in a system
1201 * has correct routing entries. Add routes for devices that where
1202 * unknown dirung the first enumeration pass through the switch.
1204 static void rio_update_route_tables(struct rio_net *net)
1206 struct rio_dev *rdev, *swrdev;
1207 struct rio_switch *rswitch;
1211 list_for_each_entry(rdev, &net->devices, net_list) {
1213 destid = rdev->destid;
1215 list_for_each_entry(rswitch, &net->switches, node) {
1217 if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
1220 if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
1221 swrdev = sw_to_rio_dev(rswitch);
1223 /* Skip if destid ends in empty switch*/
1224 if (swrdev->destid == destid)
1227 sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
1229 if (rswitch->add_entry) {
1230 rio_route_add_entry(swrdev,
1231 RIO_GLOBAL_TABLE, destid,
1233 rswitch->route_table[destid] = sport;
1241 * rio_init_em - Initializes RIO Error Management (for switches)
1244 * For each enumerated switch, call device-specific error management
1245 * initialization routine (if supplied by the switch driver).
1247 static void rio_init_em(struct rio_dev *rdev)
1249 if (rio_is_switch(rdev) && (rdev->em_efptr) &&
1250 (rdev->rswitch->em_init)) {
1251 rdev->rswitch->em_init(rdev);
1256 * rio_pw_enable - Enables/disables port-write handling by a master port
1257 * @port: Master port associated with port-write handling
1258 * @enable: 1=enable, 0=disable
1260 static void rio_pw_enable(struct rio_mport *port, int enable)
1262 if (port->ops->pwenable)
1263 port->ops->pwenable(port, enable);
1267 * rio_enum_mport- Start enumeration through a master port
1268 * @mport: Master port to send transactions
1270 * Starts the enumeration process. If somebody has enumerated our
1271 * master port device, then give up. If not and we have an active
1272 * link, then start recursive peer enumeration. Returns %0 if
1273 * enumeration succeeds or %-EBUSY if enumeration fails.
1275 int __devinit rio_enum_mport(struct rio_mport *mport)
1277 struct rio_net *net = NULL;
1280 printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
1282 /* If somebody else enumerated our master port device, bail. */
1283 if (rio_enum_host(mport) < 0) {
1285 "RIO: master port %d device has been enumerated by a remote host\n",
1291 /* If master port has an active link, allocate net and enum peers */
1292 if (rio_mport_is_active(mport)) {
1293 net = rio_alloc_net(mport, 1, 0);
1295 printk(KERN_ERR "RIO: failed to allocate new net\n");
1300 /* reserve mport destID in new net */
1301 rio_destid_reserve(net, mport->host_deviceid);
1303 /* Enable Input Output Port (transmitter reviever) */
1304 rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
1306 /* Set component tag for host */
1307 rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
1310 next_destid = rio_destid_alloc(net);
1312 if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
1313 /* A higher priority host won enumeration, bail. */
1315 "RIO: master port %d device has lost enumeration to a remote host\n",
1317 rio_clear_locks(net);
1321 /* free the last allocated destID (unused) */
1322 rio_destid_free(net, next_destid);
1323 rio_update_route_tables(net);
1324 rio_clear_locks(net);
1325 rio_pw_enable(mport, 1);
1327 printk(KERN_INFO "RIO: master port %d link inactive\n",
1337 * rio_build_route_tables- Generate route tables from switch route entries
1338 * @net: RIO network to run route tables scan on
1340 * For each switch device, generate a route table by copying existing
1341 * route entries from the switch.
1343 static void rio_build_route_tables(struct rio_net *net)
1345 struct rio_switch *rswitch;
1346 struct rio_dev *rdev;
1350 list_for_each_entry(rswitch, &net->switches, node) {
1351 rdev = sw_to_rio_dev(rswitch);
1353 rio_lock_device(net->hport, rdev->destid,
1354 rdev->hopcount, 1000);
1356 i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
1358 if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
1361 rswitch->route_table[i] = sport;
1364 rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
1369 * rio_disc_mport- Start discovery through a master port
1370 * @mport: Master port to send transactions
1372 * Starts the discovery process. If we have an active link,
1373 * then wait for the signal that enumeration is complete.
1374 * When enumeration completion is signaled, start recursive
1375 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1378 int __devinit rio_disc_mport(struct rio_mport *mport)
1380 struct rio_net *net = NULL;
1381 unsigned long to_end;
1383 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1386 /* If master port has an active link, allocate net and discover peers */
1387 if (rio_mport_is_active(mport)) {
1388 pr_debug("RIO: wait for enumeration to complete...\n");
1390 to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1391 while (time_before(jiffies, to_end)) {
1392 if (rio_enum_complete(mport))
1394 schedule_timeout_uninterruptible(msecs_to_jiffies(10));
1397 pr_debug("RIO: discovery timeout on mport %d %s\n",
1398 mport->id, mport->name);
1401 pr_debug("RIO: ... enumeration done\n");
1403 net = rio_alloc_net(mport, 0, 0);
1405 printk(KERN_ERR "RIO: Failed to allocate new net\n");
1409 /* Read DestID assigned by enumerator */
1410 rio_local_read_config_32(mport, RIO_DID_CSR,
1411 &mport->host_deviceid);
1412 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1413 mport->host_deviceid);
1415 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1418 "RIO: master port %d device has failed discovery\n",
1423 rio_build_route_tables(net);