2  * RapidIO enumeration and discovery support
 
   4  * Copyright 2005 MontaVista Software, Inc.
 
   5  * Matt Porter <mporter@kernel.crashing.org>
 
   7  * This program is free software; you can redistribute  it and/or modify it
 
   8  * under  the terms of  the GNU General  Public License as published by the
 
   9  * Free Software Foundation;  either version 2 of the  License, or (at your
 
  10  * option) any later version.
 
  13 #include <linux/types.h>
 
  14 #include <linux/kernel.h>
 
  16 #include <linux/delay.h>
 
  17 #include <linux/dma-mapping.h>
 
  18 #include <linux/init.h>
 
  19 #include <linux/rio.h>
 
  20 #include <linux/rio_drv.h>
 
  21 #include <linux/rio_ids.h>
 
  22 #include <linux/rio_regs.h>
 
  23 #include <linux/module.h>
 
  24 #include <linux/spinlock.h>
 
  25 #include <linux/timer.h>
 
  26 #include <linux/jiffies.h>
 
  27 #include <linux/slab.h>
 
  31 LIST_HEAD(rio_devices);
 
  32 static LIST_HEAD(rio_switches);
 
  34 #define RIO_ENUM_CMPL_MAGIC     0xdeadbeef
 
  36 static void rio_enum_timeout(unsigned long);
 
  38 DEFINE_SPINLOCK(rio_global_list_lock);
 
  40 static int next_destid = 0;
 
  41 static int next_switchid = 0;
 
  42 static int next_net = 0;
 
  44 static struct timer_list rio_enum_timer =
 
  45 TIMER_INITIALIZER(rio_enum_timeout, 0, 0);
 
  47 static int rio_mport_phys_table[] = {
 
  49         RIO_EFB_PAR_EP_REC_ID,
 
  51         RIO_EFB_SER_EP_REC_ID,
 
  55 static int rio_sport_phys_table[] = {
 
  56         RIO_EFB_PAR_EP_FREE_ID,
 
  57         RIO_EFB_SER_EP_FREE_ID,
 
  62  * rio_get_device_id - Get the base/extended device id for a device
 
  63  * @port: RIO master port
 
  64  * @destid: Destination ID of device
 
  65  * @hopcount: Hopcount to device
 
  67  * Reads the base/extended device id from a device. Returns the
 
  70 static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
 
  74         rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
 
  76         return RIO_GET_DID(port->sys_size, result);
 
  80  * rio_set_device_id - Set the base/extended device id for a device
 
  81  * @port: RIO master port
 
  82  * @destid: Destination ID of device
 
  83  * @hopcount: Hopcount to device
 
  84  * @did: Device ID value to be written
 
  86  * Writes the base/extended device id from a device.
 
  88 static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
 
  90         rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
 
  91                                   RIO_SET_DID(port->sys_size, did));
 
  95  * rio_local_set_device_id - Set the base/extended device id for a port
 
  96  * @port: RIO master port
 
  97  * @did: Device ID value to be written
 
  99  * Writes the base/extended device id from a device.
 
 101 static void rio_local_set_device_id(struct rio_mport *port, u16 did)
 
 103         rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
 
 108  * rio_clear_locks- Release all host locks and signal enumeration complete
 
 109  * @port: Master port to issue transaction
 
 111  * Marks the component tag CSR on each device with the enumeration
 
 112  * complete flag. When complete, it then release the host locks on
 
 113  * each device. Returns 0 on success or %-EINVAL on failure.
 
 115 static int rio_clear_locks(struct rio_mport *port)
 
 117         struct rio_dev *rdev;
 
 121         /* Write component tag CSR magic complete value */
 
 122         rio_local_write_config_32(port, RIO_COMPONENT_TAG_CSR,
 
 123                                   RIO_ENUM_CMPL_MAGIC);
 
 124         list_for_each_entry(rdev, &rio_devices, global_list)
 
 125             rio_write_config_32(rdev, RIO_COMPONENT_TAG_CSR,
 
 126                                 RIO_ENUM_CMPL_MAGIC);
 
 128         /* Release host device id locks */
 
 129         rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
 
 130                                   port->host_deviceid);
 
 131         rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
 
 132         if ((result & 0xffff) != 0xffff) {
 
 134                        "RIO: badness when releasing host lock on master port, result %8.8x\n",
 
 138         list_for_each_entry(rdev, &rio_devices, global_list) {
 
 139                 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
 
 140                                     port->host_deviceid);
 
 141                 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
 
 142                 if ((result & 0xffff) != 0xffff) {
 
 144                                "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
 
 145                                rdev->vid, rdev->did);
 
 154  * rio_enum_host- Set host lock and initialize host destination ID
 
 155  * @port: Master port to issue transaction
 
 157  * Sets the local host master port lock and destination ID register
 
 158  * with the host device ID value. The host device ID value is provided
 
 159  * by the platform. Returns %0 on success or %-1 on failure.
 
 161 static int rio_enum_host(struct rio_mport *port)
 
 165         /* Set master port host device id lock */
 
 166         rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
 
 167                                   port->host_deviceid);
 
 169         rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
 
 170         if ((result & 0xffff) != port->host_deviceid)
 
 173         /* Set master port destid and init destid ctr */
 
 174         rio_local_set_device_id(port, port->host_deviceid);
 
 176         if (next_destid == port->host_deviceid)
 
 183  * rio_device_has_destid- Test if a device contains a destination ID register
 
 184  * @port: Master port to issue transaction
 
 185  * @src_ops: RIO device source operations
 
 186  * @dst_ops: RIO device destination operations
 
 188  * Checks the provided @src_ops and @dst_ops for the necessary transaction
 
 189  * capabilities that indicate whether or not a device will implement a
 
 190  * destination ID register. Returns 1 if true or 0 if false.
 
 192 static int rio_device_has_destid(struct rio_mport *port, int src_ops,
 
 195         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;
 
 197         return !!((src_ops | dst_ops) & mask);
 
 201  * rio_release_dev- Frees a RIO device struct
 
 202  * @dev: LDM device associated with a RIO device struct
 
 204  * Gets the RIO device struct associated a RIO device struct.
 
 205  * The RIO device struct is freed.
 
 207 static void rio_release_dev(struct device *dev)
 
 209         struct rio_dev *rdev;
 
 211         rdev = to_rio_dev(dev);
 
 216  * rio_is_switch- Tests if a RIO device has switch capabilities
 
 219  * Gets the RIO device Processing Element Features register
 
 220  * contents and tests for switch capabilities. Returns 1 if
 
 221  * the device is a switch or 0 if it is not a switch.
 
 222  * The RIO device struct is freed.
 
 224 static int rio_is_switch(struct rio_dev *rdev)
 
 226         if (rdev->pef & RIO_PEF_SWITCH)
 
 232  * rio_route_set_ops- Sets routing operations for a particular vendor switch
 
 235  * Searches the RIO route ops table for known switch types. If the vid
 
 236  * and did match a switch table entry, then set the add_entry() and
 
 237  * get_entry() ops to the table entry values.
 
 239 static void rio_route_set_ops(struct rio_dev *rdev)
 
 241         struct rio_route_ops *cur = __start_rio_route_ops;
 
 242         struct rio_route_ops *end = __end_rio_route_ops;
 
 245                 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
 
 246                         pr_debug("RIO: adding routing ops for %s\n", rio_name(rdev));
 
 247                         rdev->rswitch->add_entry = cur->add_hook;
 
 248                         rdev->rswitch->get_entry = cur->get_hook;
 
 253         if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
 
 254                 printk(KERN_ERR "RIO: missing routing ops for %s\n",
 
 259  * rio_add_device- Adds a RIO device to the device model
 
 262  * Adds the RIO device to the global device list and adds the RIO
 
 263  * device to the RIO device list.  Creates the generic sysfs nodes
 
 266 static void __devinit rio_add_device(struct rio_dev *rdev)
 
 268         device_add(&rdev->dev);
 
 270         spin_lock(&rio_global_list_lock);
 
 271         list_add_tail(&rdev->global_list, &rio_devices);
 
 272         spin_unlock(&rio_global_list_lock);
 
 274         rio_create_sysfs_dev_files(rdev);
 
 278  * rio_setup_device- Allocates and sets up a RIO device
 
 280  * @port: Master port to send transactions
 
 281  * @destid: Current destination ID
 
 282  * @hopcount: Current hopcount
 
 283  * @do_enum: Enumeration/Discovery mode flag
 
 285  * Allocates a RIO device and configures fields based on configuration
 
 286  * space contents. If device has a destination ID register, a destination
 
 287  * ID is either assigned in enumeration mode or read from configuration
 
 288  * space in discovery mode.  If the device has switch capabilities, then
 
 289  * a switch is allocated and configured appropriately. Returns a pointer
 
 290  * to a RIO device on success or NULL on failure.
 
 293 static struct rio_dev *rio_setup_device(struct rio_net *net,
 
 294                                         struct rio_mport *port, u16 destid,
 
 295                                         u8 hopcount, int do_enum)
 
 297         struct rio_dev *rdev;
 
 298         struct rio_switch *rswitch;
 
 301         rdev = kzalloc(sizeof(struct rio_dev), GFP_KERNEL);
 
 306         rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
 
 308         rdev->did = result >> 16;
 
 309         rdev->vid = result & 0xffff;
 
 310         rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
 
 312         rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
 
 314         rdev->asm_did = result >> 16;
 
 315         rdev->asm_vid = result & 0xffff;
 
 316         rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
 
 318         rdev->asm_rev = result >> 16;
 
 319         rio_mport_read_config_32(port, destid, hopcount, RIO_PEF_CAR,
 
 321         if (rdev->pef & RIO_PEF_EXT_FEATURES)
 
 322                 rdev->efptr = result & 0xffff;
 
 324         rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
 
 326         rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
 
 329         if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
 
 331                         rio_set_device_id(port, destid, hopcount, next_destid);
 
 332                         rdev->destid = next_destid++;
 
 333                         if (next_destid == port->host_deviceid)
 
 336                         rdev->destid = rio_get_device_id(port, destid, hopcount);
 
 338                 /* Switch device has an associated destID */
 
 339                 rdev->destid = RIO_INVALID_DESTID;
 
 341         /* If a PE has both switch and other functions, show it as a switch */
 
 342         if (rio_is_switch(rdev)) {
 
 343                 rio_mport_read_config_32(port, destid, hopcount,
 
 344                                          RIO_SWP_INFO_CAR, &rdev->swpinfo);
 
 345                 rswitch = kmalloc(sizeof(struct rio_switch), GFP_KERNEL);
 
 351                 rswitch->switchid = next_switchid;
 
 352                 rswitch->hopcount = hopcount;
 
 353                 rswitch->destid = destid;
 
 354                 rswitch->route_table = kzalloc(sizeof(u8)*
 
 355                                         RIO_MAX_ROUTE_ENTRIES(port->sys_size),
 
 357                 if (!rswitch->route_table) {
 
 363                 /* Initialize switch route table */
 
 364                 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
 
 366                         rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
 
 367                 rdev->rswitch = rswitch;
 
 368                 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
 
 369                              rdev->rswitch->switchid);
 
 370                 rio_route_set_ops(rdev);
 
 372                 list_add_tail(&rswitch->node, &rio_switches);
 
 375                 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
 
 378         rdev->dev.bus = &rio_bus_type;
 
 380         device_initialize(&rdev->dev);
 
 381         rdev->dev.release = rio_release_dev;
 
 384         rdev->dma_mask = DMA_BIT_MASK(32);
 
 385         rdev->dev.dma_mask = &rdev->dma_mask;
 
 386         rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
 
 388         if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
 
 389             (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
 
 390                 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
 
 393         rio_add_device(rdev);
 
 400  * rio_sport_is_active- Tests if a switch port has an active connection.
 
 401  * @port: Master port to send transaction
 
 402  * @destid: Associated destination ID for switch
 
 403  * @hopcount: Hopcount to reach switch
 
 404  * @sport: Switch port number
 
 406  * Reads the port error status CSR for a particular switch port to
 
 407  * determine if the port has an active link.  Returns
 
 408  * %PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
 
 412 rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
 
 417         int *entry = rio_sport_phys_table;
 
 421                      rio_mport_get_feature(port, 0, destid, hopcount, *entry)))
 
 424         } while (*++entry >= 0);
 
 427                 rio_mport_read_config_32(port, destid, hopcount,
 
 429                                          RIO_PORT_N_ERR_STS_CSR(sport),
 
 432         return (result & PORT_N_ERR_STS_PORT_OK);
 
 436  * rio_route_add_entry- Add a route entry to a switch routing table
 
 437  * @mport: Master port to send transaction
 
 438  * @rswitch: Switch device
 
 439  * @table: Routing table ID
 
 440  * @route_destid: Destination ID to be routed
 
 441  * @route_port: Port number to be routed
 
 443  * Calls the switch specific add_entry() method to add a route entry
 
 444  * on a switch. The route table can be specified using the @table
 
 445  * argument if a switch has per port routing tables or the normal
 
 446  * use is to specific all tables (or the global table) by passing
 
 447  * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
 
 450 static int rio_route_add_entry(struct rio_mport *mport, struct rio_switch *rswitch,
 
 451                                u16 table, u16 route_destid, u8 route_port)
 
 453         return rswitch->add_entry(mport, rswitch->destid,
 
 454                                         rswitch->hopcount, table,
 
 455                                         route_destid, route_port);
 
 459  * rio_route_get_entry- Read a route entry in a switch routing table
 
 460  * @mport: Master port to send transaction
 
 461  * @rswitch: Switch device
 
 462  * @table: Routing table ID
 
 463  * @route_destid: Destination ID to be routed
 
 464  * @route_port: Pointer to read port number into
 
 466  * Calls the switch specific get_entry() method to read a route entry
 
 467  * in a switch. The route table can be specified using the @table
 
 468  * argument if a switch has per port routing tables or the normal
 
 469  * use is to specific all tables (or the global table) by passing
 
 470  * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
 
 474 rio_route_get_entry(struct rio_mport *mport, struct rio_switch *rswitch, u16 table,
 
 475                     u16 route_destid, u8 * route_port)
 
 477         return rswitch->get_entry(mport, rswitch->destid,
 
 478                                         rswitch->hopcount, table,
 
 479                                         route_destid, route_port);
 
 483  * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
 
 484  * @port: Master port to send transaction
 
 485  * @hopcount: Number of hops to the device
 
 487  * Used during enumeration to read the Host Device ID Lock CSR on a
 
 488  * RIO device. Returns the value of the lock register.
 
 490 static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
 
 494         rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
 
 495                                  RIO_HOST_DID_LOCK_CSR, &result);
 
 497         return (u16) (result & 0xffff);
 
 501  * rio_get_swpinfo_inport- Gets the ingress port number
 
 502  * @mport: Master port to send transaction
 
 503  * @destid: Destination ID associated with the switch
 
 504  * @hopcount: Number of hops to the device
 
 506  * Returns port number being used to access the switch device.
 
 509 rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
 
 513         rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
 
 516         return (u8) (result & 0xff);
 
 520  * rio_get_swpinfo_tports- Gets total number of ports on the switch
 
 521  * @mport: Master port to send transaction
 
 522  * @destid: Destination ID associated with the switch
 
 523  * @hopcount: Number of hops to the device
 
 525  * Returns total numbers of ports implemented by the switch device.
 
 527 static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
 
 532         rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
 
 535         return RIO_GET_TOTAL_PORTS(result);
 
 539  * rio_net_add_mport- Add a master port to a RIO network
 
 541  * @port: Master port to add
 
 543  * Adds a master port to the network list of associated master
 
 546 static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
 
 548         spin_lock(&rio_global_list_lock);
 
 549         list_add_tail(&port->nnode, &net->mports);
 
 550         spin_unlock(&rio_global_list_lock);
 
 554  * rio_enum_peer- Recursively enumerate a RIO network through a master port
 
 555  * @net: RIO network being enumerated
 
 556  * @port: Master port to send transactions
 
 557  * @hopcount: Number of hops into the network
 
 559  * Recursively enumerates a RIO network.  Transactions are sent via the
 
 560  * master port passed in @port.
 
 562 static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
 
 570         struct rio_dev *rdev;
 
 574         if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
 
 575                 pr_debug("RIO: PE already discovered by this host\n");
 
 577                  * Already discovered by this host. Add it as another
 
 578                  * master port for the current network.
 
 580                 rio_net_add_mport(net, port);
 
 584         /* Attempt to acquire device lock */
 
 585         rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
 
 587                                   RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
 
 588         while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
 
 589                < port->host_deviceid) {
 
 592                 /* Attempt to acquire device lock again */
 
 593                 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
 
 595                                           RIO_HOST_DID_LOCK_CSR,
 
 596                                           port->host_deviceid);
 
 599         if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
 
 601                     "RIO: PE locked by a higher priority host...retreating\n");
 
 605         /* Setup new RIO device */
 
 606         rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
 
 609                 /* Add device to the global and bus/net specific list. */
 
 610                 list_add_tail(&rdev->net_list, &net->devices);
 
 614         if (rio_is_switch(rdev)) {
 
 616                 sw_inport = rio_get_swpinfo_inport(port,
 
 617                                 RIO_ANY_DESTID(port->sys_size), hopcount);
 
 618                 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
 
 619                                     port->host_deviceid, sw_inport);
 
 620                 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
 
 622                 for (destid = 0; destid < next_destid; destid++) {
 
 623                         if (destid == port->host_deviceid)
 
 625                         rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
 
 627                         rdev->rswitch->route_table[destid] = sw_inport;
 
 631                     rio_get_swpinfo_tports(port, RIO_ANY_DESTID(port->sys_size),
 
 634                     "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
 
 635                     rio_name(rdev), rdev->vid, rdev->did, num_ports);
 
 636                 sw_destid = next_destid;
 
 637                 for (port_num = 0; port_num < num_ports; port_num++) {
 
 638                         if (sw_inport == port_num)
 
 641                         cur_destid = next_destid;
 
 643                         if (rio_sport_is_active
 
 644                             (port, RIO_ANY_DESTID(port->sys_size), hopcount,
 
 647                                     "RIO: scanning device on port %d\n",
 
 649                                 rio_route_add_entry(port, rdev->rswitch,
 
 651                                                 RIO_ANY_DESTID(port->sys_size),
 
 654                                 if (rio_enum_peer(net, port, hopcount + 1) < 0)
 
 657                                 /* Update routing tables */
 
 658                                 if (next_destid > cur_destid) {
 
 659                                         for (destid = cur_destid;
 
 660                                              destid < next_destid; destid++) {
 
 661                                                 if (destid == port->host_deviceid)
 
 663                                                 rio_route_add_entry(port, rdev->rswitch,
 
 668                                                     route_table[destid] =
 
 675                 /* Check for empty switch */
 
 676                 if (next_destid == sw_destid) {
 
 678                         if (next_destid == port->host_deviceid)
 
 682                 rdev->rswitch->destid = sw_destid;
 
 684                 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
 
 685                     rio_name(rdev), rdev->vid, rdev->did);
 
 691  * rio_enum_complete- Tests if enumeration of a network is complete
 
 692  * @port: Master port to send transaction
 
 694  * Tests the Component Tag CSR for presence of the magic enumeration
 
 695  * complete flag. Return %1 if enumeration is complete or %0 if
 
 696  * enumeration is incomplete.
 
 698 static int rio_enum_complete(struct rio_mport *port)
 
 703         rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
 
 705         if (tag_csr == RIO_ENUM_CMPL_MAGIC)
 
 712  * rio_disc_peer- Recursively discovers a RIO network through a master port
 
 713  * @net: RIO network being discovered
 
 714  * @port: Master port to send transactions
 
 715  * @destid: Current destination ID in network
 
 716  * @hopcount: Number of hops into the network
 
 718  * Recursively discovers a RIO network.  Transactions are sent via the
 
 719  * master port passed in @port.
 
 722 rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
 
 725         u8 port_num, route_port;
 
 727         struct rio_dev *rdev;
 
 730         /* Setup new RIO device */
 
 731         if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
 
 732                 /* Add device to the global and bus/net specific list. */
 
 733                 list_add_tail(&rdev->net_list, &net->devices);
 
 737         if (rio_is_switch(rdev)) {
 
 740                 /* Associated destid is how we accessed this switch */
 
 741                 rdev->rswitch->destid = destid;
 
 743                 num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
 
 745                     "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
 
 746                     rio_name(rdev), rdev->vid, rdev->did, num_ports);
 
 747                 for (port_num = 0; port_num < num_ports; port_num++) {
 
 748                         if (rio_get_swpinfo_inport(port, destid, hopcount) ==
 
 752                         if (rio_sport_is_active
 
 753                             (port, destid, hopcount, port_num)) {
 
 755                                     "RIO: scanning device on port %d\n",
 
 758                                      ndestid < RIO_ANY_DESTID(port->sys_size);
 
 760                                         rio_route_get_entry(port, rdev->rswitch,
 
 764                                         if (route_port == port_num)
 
 769                                     (net, port, ndestid, hopcount + 1) < 0)
 
 774                 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
 
 775                     rio_name(rdev), rdev->vid, rdev->did);
 
 781  * rio_mport_is_active- Tests if master port link is active
 
 782  * @port: Master port to test
 
 784  * Reads the port error status CSR for the master port to
 
 785  * determine if the port has an active link.  Returns
 
 786  * %PORT_N_ERR_STS_PORT_OK if the  master port is active
 
 787  * or %0 if it is inactive.
 
 789 static int rio_mport_is_active(struct rio_mport *port)
 
 793         int *entry = rio_mport_phys_table;
 
 797                      rio_mport_get_feature(port, 1, 0, 0, *entry)))
 
 799         } while (*++entry >= 0);
 
 802                 rio_local_read_config_32(port,
 
 804                                          RIO_PORT_N_ERR_STS_CSR(port->index),
 
 807         return (result & PORT_N_ERR_STS_PORT_OK);
 
 811  * rio_alloc_net- Allocate and configure a new RIO network
 
 812  * @port: Master port associated with the RIO network
 
 814  * Allocates a RIO network structure, initializes per-network
 
 815  * list heads, and adds the associated master port to the
 
 816  * network list of associated master ports. Returns a
 
 817  * RIO network pointer on success or %NULL on failure.
 
 819 static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
 
 823         net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
 
 825                 INIT_LIST_HEAD(&net->node);
 
 826                 INIT_LIST_HEAD(&net->devices);
 
 827                 INIT_LIST_HEAD(&net->mports);
 
 828                 list_add_tail(&port->nnode, &net->mports);
 
 830                 net->id = next_net++;
 
 836  * rio_update_route_tables- Updates route tables in switches
 
 837  * @port: Master port associated with the RIO network
 
 839  * For each enumerated device, ensure that each switch in a system
 
 840  * has correct routing entries. Add routes for devices that where
 
 841  * unknown dirung the first enumeration pass through the switch.
 
 843 static void rio_update_route_tables(struct rio_mport *port)
 
 845         struct rio_dev *rdev;
 
 846         struct rio_switch *rswitch;
 
 850         list_for_each_entry(rdev, &rio_devices, global_list) {
 
 852                 destid = (rio_is_switch(rdev))?rdev->rswitch->destid:rdev->destid;
 
 854                 list_for_each_entry(rswitch, &rio_switches, node) {
 
 856                         if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
 
 859                         if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
 
 861                                 sport = rio_get_swpinfo_inport(port,
 
 862                                                 rswitch->destid, rswitch->hopcount);
 
 864                                 if (rswitch->add_entry) {
 
 865                                         rio_route_add_entry(port, rswitch, RIO_GLOBAL_TABLE, destid, sport);
 
 866                                         rswitch->route_table[destid] = sport;
 
 874  * rio_enum_mport- Start enumeration through a master port
 
 875  * @mport: Master port to send transactions
 
 877  * Starts the enumeration process. If somebody has enumerated our
 
 878  * master port device, then give up. If not and we have an active
 
 879  * link, then start recursive peer enumeration. Returns %0 if
 
 880  * enumeration succeeds or %-EBUSY if enumeration fails.
 
 882 int __devinit rio_enum_mport(struct rio_mport *mport)
 
 884         struct rio_net *net = NULL;
 
 887         printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
 
 889         /* If somebody else enumerated our master port device, bail. */
 
 890         if (rio_enum_host(mport) < 0) {
 
 892                        "RIO: master port %d device has been enumerated by a remote host\n",
 
 898         /* If master port has an active link, allocate net and enum peers */
 
 899         if (rio_mport_is_active(mport)) {
 
 900                 if (!(net = rio_alloc_net(mport))) {
 
 901                         printk(KERN_ERR "RIO: failed to allocate new net\n");
 
 905                 if (rio_enum_peer(net, mport, 0) < 0) {
 
 906                         /* A higher priority host won enumeration, bail. */
 
 908                                "RIO: master port %d device has lost enumeration to a remote host\n",
 
 910                         rio_clear_locks(mport);
 
 914                 rio_update_route_tables(mport);
 
 915                 rio_clear_locks(mport);
 
 917                 printk(KERN_INFO "RIO: master port %d link inactive\n",
 
 927  * rio_build_route_tables- Generate route tables from switch route entries
 
 929  * For each switch device, generate a route table by copying existing
 
 930  * route entries from the switch.
 
 932 static void rio_build_route_tables(void)
 
 934         struct rio_dev *rdev;
 
 938         list_for_each_entry(rdev, &rio_devices, global_list)
 
 939             if (rio_is_switch(rdev))
 
 941                      i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
 
 943                         if (rio_route_get_entry
 
 944                             (rdev->net->hport, rdev->rswitch, RIO_GLOBAL_TABLE,
 
 947                         rdev->rswitch->route_table[i] = sport;
 
 952  * rio_enum_timeout- Signal that enumeration timed out
 
 953  * @data: Address of timeout flag.
 
 955  * When the enumeration complete timer expires, set a flag that
 
 956  * signals to the discovery process that enumeration did not
 
 957  * complete in a sane amount of time.
 
 959 static void rio_enum_timeout(unsigned long data)
 
 961         /* Enumeration timed out, set flag */
 
 966  * rio_disc_mport- Start discovery through a master port
 
 967  * @mport: Master port to send transactions
 
 969  * Starts the discovery process. If we have an active link,
 
 970  * then wait for the signal that enumeration is complete.
 
 971  * When enumeration completion is signaled, start recursive
 
 972  * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
 
 975 int __devinit rio_disc_mport(struct rio_mport *mport)
 
 977         struct rio_net *net = NULL;
 
 978         int enum_timeout_flag = 0;
 
 980         printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
 
 983         /* If master port has an active link, allocate net and discover peers */
 
 984         if (rio_mport_is_active(mport)) {
 
 985                 if (!(net = rio_alloc_net(mport))) {
 
 986                         printk(KERN_ERR "RIO: Failed to allocate new net\n");
 
 990                 pr_debug("RIO: wait for enumeration complete...");
 
 992                 rio_enum_timer.expires =
 
 993                     jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
 
 994                 rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
 
 995                 add_timer(&rio_enum_timer);
 
 996                 while (!rio_enum_complete(mport)) {
 
 998                         if (enum_timeout_flag) {
 
 999                                 del_timer_sync(&rio_enum_timer);
 
1003                 del_timer_sync(&rio_enum_timer);
 
1006                 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
 
1009                                "RIO: master port %d device has failed discovery\n",
 
1014                 rio_build_route_tables();
 
1020         pr_debug("timeout\n");