Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * RapidIO enumeration and discovery support
0004  *
0005  * Copyright 2005 MontaVista Software, Inc.
0006  * Matt Porter <mporter@kernel.crashing.org>
0007  *
0008  * Copyright 2009 Integrated Device Technology, Inc.
0009  * Alex Bounine <alexandre.bounine@idt.com>
0010  * - Added Port-Write/Error Management initialization and handling
0011  *
0012  * Copyright 2009 Sysgo AG
0013  * Thomas Moll <thomas.moll@sysgo.com>
0014  * - Added Input- Output- enable functionality, to allow full communication
0015  */
0016 
0017 #include <linux/types.h>
0018 #include <linux/kernel.h>
0019 
0020 #include <linux/delay.h>
0021 #include <linux/dma-mapping.h>
0022 #include <linux/init.h>
0023 #include <linux/rio.h>
0024 #include <linux/rio_drv.h>
0025 #include <linux/rio_ids.h>
0026 #include <linux/rio_regs.h>
0027 #include <linux/module.h>
0028 #include <linux/spinlock.h>
0029 #include <linux/timer.h>
0030 #include <linux/sched.h>
0031 #include <linux/jiffies.h>
0032 #include <linux/slab.h>
0033 
0034 #include "rio.h"
0035 
0036 static void rio_init_em(struct rio_dev *rdev);
0037 
0038 struct rio_id_table {
0039     u16 start;  /* logical minimal id */
0040     u32 max;    /* max number of IDs in table */
0041     spinlock_t lock;
0042     unsigned long table[];
0043 };
0044 
0045 static int next_destid = 0;
0046 static int next_comptag = 1;
0047 
0048 /**
0049  * rio_destid_alloc - Allocate next available destID for given network
0050  * @net: RIO network
0051  *
0052  * Returns next available device destination ID for the specified RIO network.
0053  * Marks allocated ID as one in use.
0054  * Returns RIO_INVALID_DESTID if new destID is not available.
0055  */
0056 static u16 rio_destid_alloc(struct rio_net *net)
0057 {
0058     int destid;
0059     struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0060 
0061     spin_lock(&idtab->lock);
0062     destid = find_first_zero_bit(idtab->table, idtab->max);
0063 
0064     if (destid < idtab->max) {
0065         set_bit(destid, idtab->table);
0066         destid += idtab->start;
0067     } else
0068         destid = RIO_INVALID_DESTID;
0069 
0070     spin_unlock(&idtab->lock);
0071     return (u16)destid;
0072 }
0073 
0074 /**
0075  * rio_destid_reserve - Reserve the specified destID
0076  * @net: RIO network
0077  * @destid: destID to reserve
0078  *
0079  * Tries to reserve the specified destID.
0080  * Returns 0 if successful.
0081  */
0082 static int rio_destid_reserve(struct rio_net *net, u16 destid)
0083 {
0084     int oldbit;
0085     struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0086 
0087     destid -= idtab->start;
0088     spin_lock(&idtab->lock);
0089     oldbit = test_and_set_bit(destid, idtab->table);
0090     spin_unlock(&idtab->lock);
0091     return oldbit;
0092 }
0093 
0094 /**
0095  * rio_destid_free - free a previously allocated destID
0096  * @net: RIO network
0097  * @destid: destID to free
0098  *
0099  * Makes the specified destID available for use.
0100  */
0101 static void rio_destid_free(struct rio_net *net, u16 destid)
0102 {
0103     struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0104 
0105     destid -= idtab->start;
0106     spin_lock(&idtab->lock);
0107     clear_bit(destid, idtab->table);
0108     spin_unlock(&idtab->lock);
0109 }
0110 
0111 /**
0112  * rio_destid_first - return first destID in use
0113  * @net: RIO network
0114  */
0115 static u16 rio_destid_first(struct rio_net *net)
0116 {
0117     int destid;
0118     struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0119 
0120     spin_lock(&idtab->lock);
0121     destid = find_first_bit(idtab->table, idtab->max);
0122     if (destid >= idtab->max)
0123         destid = RIO_INVALID_DESTID;
0124     else
0125         destid += idtab->start;
0126     spin_unlock(&idtab->lock);
0127     return (u16)destid;
0128 }
0129 
0130 /**
0131  * rio_destid_next - return next destID in use
0132  * @net: RIO network
0133  * @from: destination ID from which search shall continue
0134  */
0135 static u16 rio_destid_next(struct rio_net *net, u16 from)
0136 {
0137     int destid;
0138     struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
0139 
0140     spin_lock(&idtab->lock);
0141     destid = find_next_bit(idtab->table, idtab->max, from);
0142     if (destid >= idtab->max)
0143         destid = RIO_INVALID_DESTID;
0144     else
0145         destid += idtab->start;
0146     spin_unlock(&idtab->lock);
0147     return (u16)destid;
0148 }
0149 
0150 /**
0151  * rio_get_device_id - Get the base/extended device id for a device
0152  * @port: RIO master port
0153  * @destid: Destination ID of device
0154  * @hopcount: Hopcount to device
0155  *
0156  * Reads the base/extended device id from a device. Returns the
0157  * 8/16-bit device ID.
0158  */
0159 static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
0160 {
0161     u32 result;
0162 
0163     rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
0164 
0165     return RIO_GET_DID(port->sys_size, result);
0166 }
0167 
0168 /**
0169  * rio_set_device_id - Set the base/extended device id for a device
0170  * @port: RIO master port
0171  * @destid: Destination ID of device
0172  * @hopcount: Hopcount to device
0173  * @did: Device ID value to be written
0174  *
0175  * Writes the base/extended device id from a device.
0176  */
0177 static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
0178 {
0179     rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
0180                   RIO_SET_DID(port->sys_size, did));
0181 }
0182 
0183 /**
0184  * rio_clear_locks- Release all host locks and signal enumeration complete
0185  * @net: RIO network to run on
0186  *
0187  * Marks the component tag CSR on each device with the enumeration
0188  * complete flag. When complete, it then release the host locks on
0189  * each device. Returns 0 on success or %-EINVAL on failure.
0190  */
0191 static int rio_clear_locks(struct rio_net *net)
0192 {
0193     struct rio_mport *port = net->hport;
0194     struct rio_dev *rdev;
0195     u32 result;
0196     int ret = 0;
0197 
0198     /* Release host device id locks */
0199     rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
0200                   port->host_deviceid);
0201     rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
0202     if ((result & 0xffff) != 0xffff) {
0203         printk(KERN_INFO
0204                "RIO: badness when releasing host lock on master port, result %8.8x\n",
0205                result);
0206         ret = -EINVAL;
0207     }
0208     list_for_each_entry(rdev, &net->devices, net_list) {
0209         rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
0210                     port->host_deviceid);
0211         rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
0212         if ((result & 0xffff) != 0xffff) {
0213             printk(KERN_INFO
0214                    "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
0215                    rdev->vid, rdev->did);
0216             ret = -EINVAL;
0217         }
0218 
0219         /* Mark device as discovered and enable master */
0220         rio_read_config_32(rdev,
0221                    rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
0222                    &result);
0223         result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
0224         rio_write_config_32(rdev,
0225                     rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
0226                     result);
0227     }
0228 
0229     return ret;
0230 }
0231 
0232 /**
0233  * rio_enum_host- Set host lock and initialize host destination ID
0234  * @port: Master port to issue transaction
0235  *
0236  * Sets the local host master port lock and destination ID register
0237  * with the host device ID value. The host device ID value is provided
0238  * by the platform. Returns %0 on success or %-1 on failure.
0239  */
0240 static int rio_enum_host(struct rio_mport *port)
0241 {
0242     u32 result;
0243 
0244     /* Set master port host device id lock */
0245     rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
0246                   port->host_deviceid);
0247 
0248     rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
0249     if ((result & 0xffff) != port->host_deviceid)
0250         return -1;
0251 
0252     /* Set master port destid and init destid ctr */
0253     rio_local_set_device_id(port, port->host_deviceid);
0254     return 0;
0255 }
0256 
0257 /**
0258  * rio_device_has_destid- Test if a device contains a destination ID register
0259  * @port: Master port to issue transaction
0260  * @src_ops: RIO device source operations
0261  * @dst_ops: RIO device destination operations
0262  *
0263  * Checks the provided @src_ops and @dst_ops for the necessary transaction
0264  * capabilities that indicate whether or not a device will implement a
0265  * destination ID register. Returns 1 if true or 0 if false.
0266  */
0267 static int rio_device_has_destid(struct rio_mport *port, int src_ops,
0268                  int dst_ops)
0269 {
0270     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;
0271 
0272     return !!((src_ops | dst_ops) & mask);
0273 }
0274 
0275 /**
0276  * rio_release_dev- Frees a RIO device struct
0277  * @dev: LDM device associated with a RIO device struct
0278  *
0279  * Gets the RIO device struct associated a RIO device struct.
0280  * The RIO device struct is freed.
0281  */
0282 static void rio_release_dev(struct device *dev)
0283 {
0284     struct rio_dev *rdev;
0285 
0286     rdev = to_rio_dev(dev);
0287     kfree(rdev);
0288 }
0289 
0290 /**
0291  * rio_is_switch- Tests if a RIO device has switch capabilities
0292  * @rdev: RIO device
0293  *
0294  * Gets the RIO device Processing Element Features register
0295  * contents and tests for switch capabilities. Returns 1 if
0296  * the device is a switch or 0 if it is not a switch.
0297  * The RIO device struct is freed.
0298  */
0299 static int rio_is_switch(struct rio_dev *rdev)
0300 {
0301     if (rdev->pef & RIO_PEF_SWITCH)
0302         return 1;
0303     return 0;
0304 }
0305 
0306 /**
0307  * rio_setup_device- Allocates and sets up a RIO device
0308  * @net: RIO network
0309  * @port: Master port to send transactions
0310  * @destid: Current destination ID
0311  * @hopcount: Current hopcount
0312  * @do_enum: Enumeration/Discovery mode flag
0313  *
0314  * Allocates a RIO device and configures fields based on configuration
0315  * space contents. If device has a destination ID register, a destination
0316  * ID is either assigned in enumeration mode or read from configuration
0317  * space in discovery mode.  If the device has switch capabilities, then
0318  * a switch is allocated and configured appropriately. Returns a pointer
0319  * to a RIO device on success or NULL on failure.
0320  *
0321  */
0322 static struct rio_dev *rio_setup_device(struct rio_net *net,
0323                     struct rio_mport *port, u16 destid,
0324                     u8 hopcount, int do_enum)
0325 {
0326     int ret = 0;
0327     struct rio_dev *rdev;
0328     struct rio_switch *rswitch = NULL;
0329     int result, rdid;
0330     size_t size;
0331     u32 swpinfo = 0;
0332 
0333     size = sizeof(*rdev);
0334     if (rio_mport_read_config_32(port, destid, hopcount,
0335                      RIO_PEF_CAR, &result))
0336         return NULL;
0337 
0338     if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
0339         rio_mport_read_config_32(port, destid, hopcount,
0340                      RIO_SWP_INFO_CAR, &swpinfo);
0341         if (result & RIO_PEF_SWITCH)
0342             size += struct_size(rswitch, nextdev, RIO_GET_TOTAL_PORTS(swpinfo));
0343     }
0344 
0345     rdev = kzalloc(size, GFP_KERNEL);
0346     if (!rdev)
0347         return NULL;
0348 
0349     rdev->net = net;
0350     rdev->pef = result;
0351     rdev->swpinfo = swpinfo;
0352     rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
0353                  &result);
0354     rdev->did = result >> 16;
0355     rdev->vid = result & 0xffff;
0356     rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
0357                  &rdev->device_rev);
0358     rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
0359                  &result);
0360     rdev->asm_did = result >> 16;
0361     rdev->asm_vid = result & 0xffff;
0362     rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
0363                  &result);
0364     rdev->asm_rev = result >> 16;
0365     if (rdev->pef & RIO_PEF_EXT_FEATURES) {
0366         rdev->efptr = result & 0xffff;
0367         rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
0368                         hopcount, &rdev->phys_rmap);
0369         pr_debug("RIO: %s Register Map %d device\n",
0370              __func__, rdev->phys_rmap);
0371 
0372         rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
0373                         hopcount, RIO_EFB_ERR_MGMNT);
0374         if (!rdev->em_efptr)
0375             rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
0376                         hopcount, RIO_EFB_ERR_MGMNT_HS);
0377     }
0378 
0379     rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
0380                  &rdev->src_ops);
0381     rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
0382                  &rdev->dst_ops);
0383 
0384     if (do_enum) {
0385         /* Assign component tag to device */
0386         if (next_comptag >= 0x10000) {
0387             pr_err("RIO: Component Tag Counter Overflow\n");
0388             goto cleanup;
0389         }
0390         rio_mport_write_config_32(port, destid, hopcount,
0391                       RIO_COMPONENT_TAG_CSR, next_comptag);
0392         rdev->comp_tag = next_comptag++;
0393         rdev->do_enum = true;
0394     }  else {
0395         rio_mport_read_config_32(port, destid, hopcount,
0396                      RIO_COMPONENT_TAG_CSR,
0397                      &rdev->comp_tag);
0398     }
0399 
0400     if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
0401         if (do_enum) {
0402             rio_set_device_id(port, destid, hopcount, next_destid);
0403             rdev->destid = next_destid;
0404             next_destid = rio_destid_alloc(net);
0405         } else
0406             rdev->destid = rio_get_device_id(port, destid, hopcount);
0407 
0408         rdev->hopcount = 0xff;
0409     } else {
0410         /* Switch device has an associated destID which
0411          * will be adjusted later
0412          */
0413         rdev->destid = destid;
0414         rdev->hopcount = hopcount;
0415     }
0416 
0417     /* If a PE has both switch and other functions, show it as a switch */
0418     if (rio_is_switch(rdev)) {
0419         rswitch = rdev->rswitch;
0420         rswitch->port_ok = 0;
0421         spin_lock_init(&rswitch->lock);
0422         rswitch->route_table =
0423             kzalloc(RIO_MAX_ROUTE_ENTRIES(port->sys_size),
0424                 GFP_KERNEL);
0425         if (!rswitch->route_table)
0426             goto cleanup;
0427         /* Initialize switch route table */
0428         for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
0429                 rdid++)
0430             rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
0431         dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
0432                  rdev->comp_tag & RIO_CTAG_UDEVID);
0433 
0434         if (do_enum)
0435             rio_route_clr_table(rdev, RIO_GLOBAL_TABLE, 0);
0436     } else {
0437         if (do_enum)
0438             /*Enable Input Output Port (transmitter receiver)*/
0439             rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
0440 
0441         dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
0442                  rdev->comp_tag & RIO_CTAG_UDEVID);
0443     }
0444 
0445     rdev->dev.parent = &net->dev;
0446     rio_attach_device(rdev);
0447     rdev->dev.release = rio_release_dev;
0448     rdev->dma_mask = DMA_BIT_MASK(32);
0449     rdev->dev.dma_mask = &rdev->dma_mask;
0450     rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
0451 
0452     if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
0453         rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
0454                    0, 0xffff);
0455 
0456     ret = rio_add_device(rdev);
0457     if (ret)
0458         goto cleanup;
0459 
0460     rio_dev_get(rdev);
0461 
0462     return rdev;
0463 
0464 cleanup:
0465     if (rswitch)
0466         kfree(rswitch->route_table);
0467 
0468     kfree(rdev);
0469     return NULL;
0470 }
0471 
0472 /**
0473  * rio_sport_is_active- Tests if a switch port has an active connection.
0474  * @rdev: RapidIO device object
0475  * @sp: Switch port number
0476  *
0477  * Reads the port error status CSR for a particular switch port to
0478  * determine if the port has an active link.  Returns
0479  * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
0480  * inactive.
0481  */
0482 static int
0483 rio_sport_is_active(struct rio_dev *rdev, int sp)
0484 {
0485     u32 result = 0;
0486 
0487     rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, sp),
0488                &result);
0489 
0490     return result & RIO_PORT_N_ERR_STS_PORT_OK;
0491 }
0492 
0493 /**
0494  * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
0495  * @port: Master port to send transaction
0496  * @hopcount: Number of hops to the device
0497  *
0498  * Used during enumeration to read the Host Device ID Lock CSR on a
0499  * RIO device. Returns the value of the lock register.
0500  */
0501 static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
0502 {
0503     u32 result;
0504 
0505     rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
0506                  RIO_HOST_DID_LOCK_CSR, &result);
0507 
0508     return (u16) (result & 0xffff);
0509 }
0510 
0511 /**
0512  * rio_enum_peer- Recursively enumerate a RIO network through a master port
0513  * @net: RIO network being enumerated
0514  * @port: Master port to send transactions
0515  * @hopcount: Number of hops into the network
0516  * @prev: Previous RIO device connected to the enumerated one
0517  * @prev_port: Port on previous RIO device
0518  *
0519  * Recursively enumerates a RIO network.  Transactions are sent via the
0520  * master port passed in @port.
0521  */
0522 static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
0523              u8 hopcount, struct rio_dev *prev, int prev_port)
0524 {
0525     struct rio_dev *rdev;
0526     u32 regval;
0527     int tmp;
0528 
0529     if (rio_mport_chk_dev_access(port,
0530             RIO_ANY_DESTID(port->sys_size), hopcount)) {
0531         pr_debug("RIO: device access check failed\n");
0532         return -1;
0533     }
0534 
0535     if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
0536         pr_debug("RIO: PE already discovered by this host\n");
0537         /*
0538          * Already discovered by this host. Add it as another
0539          * link to the existing device.
0540          */
0541         rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
0542                 hopcount, RIO_COMPONENT_TAG_CSR, &regval);
0543 
0544         if (regval) {
0545             rdev = rio_get_comptag((regval & 0xffff), NULL);
0546 
0547             if (rdev && prev && rio_is_switch(prev)) {
0548                 pr_debug("RIO: redundant path to %s\n",
0549                      rio_name(rdev));
0550                 prev->rswitch->nextdev[prev_port] = rdev;
0551             }
0552         }
0553 
0554         return 0;
0555     }
0556 
0557     /* Attempt to acquire device lock */
0558     rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
0559                   hopcount,
0560                   RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
0561     while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
0562            < port->host_deviceid) {
0563         /* Delay a bit */
0564         mdelay(1);
0565         /* Attempt to acquire device lock again */
0566         rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
0567                       hopcount,
0568                       RIO_HOST_DID_LOCK_CSR,
0569                       port->host_deviceid);
0570     }
0571 
0572     if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
0573         pr_debug(
0574             "RIO: PE locked by a higher priority host...retreating\n");
0575         return -1;
0576     }
0577 
0578     /* Setup new RIO device */
0579     rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
0580                     hopcount, 1);
0581     if (rdev) {
0582         rdev->prev = prev;
0583         if (prev && rio_is_switch(prev))
0584             prev->rswitch->nextdev[prev_port] = rdev;
0585     } else
0586         return -1;
0587 
0588     if (rio_is_switch(rdev)) {
0589         int sw_destid;
0590         int cur_destid;
0591         int sw_inport;
0592         u16 destid;
0593         int port_num;
0594 
0595         sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
0596         rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
0597                     port->host_deviceid, sw_inport, 0);
0598         rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
0599 
0600         destid = rio_destid_first(net);
0601         while (destid != RIO_INVALID_DESTID && destid < next_destid) {
0602             if (destid != port->host_deviceid) {
0603                 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
0604                             destid, sw_inport, 0);
0605                 rdev->rswitch->route_table[destid] = sw_inport;
0606             }
0607             destid = rio_destid_next(net, destid + 1);
0608         }
0609         pr_debug(
0610             "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
0611             rio_name(rdev), rdev->vid, rdev->did,
0612             RIO_GET_TOTAL_PORTS(rdev->swpinfo));
0613         sw_destid = next_destid;
0614         for (port_num = 0;
0615              port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
0616              port_num++) {
0617             if (sw_inport == port_num) {
0618                 rio_enable_rx_tx_port(port, 0,
0619                           RIO_ANY_DESTID(port->sys_size),
0620                           hopcount, port_num);
0621                 rdev->rswitch->port_ok |= (1 << port_num);
0622                 continue;
0623             }
0624 
0625             cur_destid = next_destid;
0626 
0627             if (rio_sport_is_active(rdev, port_num)) {
0628                 pr_debug(
0629                     "RIO: scanning device on port %d\n",
0630                     port_num);
0631                 rio_enable_rx_tx_port(port, 0,
0632                           RIO_ANY_DESTID(port->sys_size),
0633                           hopcount, port_num);
0634                 rdev->rswitch->port_ok |= (1 << port_num);
0635                 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
0636                         RIO_ANY_DESTID(port->sys_size),
0637                         port_num, 0);
0638 
0639                 if (rio_enum_peer(net, port, hopcount + 1,
0640                           rdev, port_num) < 0)
0641                     return -1;
0642 
0643                 /* Update routing tables */
0644                 destid = rio_destid_next(net, cur_destid + 1);
0645                 if (destid != RIO_INVALID_DESTID) {
0646                     for (destid = cur_destid;
0647                          destid < next_destid;) {
0648                         if (destid != port->host_deviceid) {
0649                             rio_route_add_entry(rdev,
0650                                     RIO_GLOBAL_TABLE,
0651                                     destid,
0652                                     port_num,
0653                                     0);
0654                             rdev->rswitch->
0655                                 route_table[destid] =
0656                                 port_num;
0657                         }
0658                         destid = rio_destid_next(net,
0659                                 destid + 1);
0660                     }
0661                 }
0662             } else {
0663                 /* If switch supports Error Management,
0664                  * set PORT_LOCKOUT bit for unused port
0665                  */
0666                 if (rdev->em_efptr)
0667                     rio_set_port_lockout(rdev, port_num, 1);
0668 
0669                 rdev->rswitch->port_ok &= ~(1 << port_num);
0670             }
0671         }
0672 
0673         /* Direct Port-write messages to the enumeratiing host */
0674         if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
0675             (rdev->em_efptr)) {
0676             rio_write_config_32(rdev,
0677                     rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
0678                     (port->host_deviceid << 16) |
0679                     (port->sys_size << 15));
0680         }
0681 
0682         rio_init_em(rdev);
0683 
0684         /* Check for empty switch */
0685         if (next_destid == sw_destid)
0686             next_destid = rio_destid_alloc(net);
0687 
0688         rdev->destid = sw_destid;
0689     } else
0690         pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
0691             rio_name(rdev), rdev->vid, rdev->did);
0692 
0693     return 0;
0694 }
0695 
0696 /**
0697  * rio_enum_complete- Tests if enumeration of a network is complete
0698  * @port: Master port to send transaction
0699  *
0700  * Tests the PGCCSR discovered bit for non-zero value (enumeration
0701  * complete flag). Return %1 if enumeration is complete or %0 if
0702  * enumeration is incomplete.
0703  */
0704 static int rio_enum_complete(struct rio_mport *port)
0705 {
0706     u32 regval;
0707 
0708     rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
0709                  &regval);
0710     return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
0711 }
0712 
0713 /**
0714  * rio_disc_peer- Recursively discovers a RIO network through a master port
0715  * @net: RIO network being discovered
0716  * @port: Master port to send transactions
0717  * @destid: Current destination ID in network
0718  * @hopcount: Number of hops into the network
0719  * @prev: previous rio_dev
0720  * @prev_port: previous port number
0721  *
0722  * Recursively discovers a RIO network.  Transactions are sent via the
0723  * master port passed in @port.
0724  */
0725 static int
0726 rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
0727           u8 hopcount, struct rio_dev *prev, int prev_port)
0728 {
0729     u8 port_num, route_port;
0730     struct rio_dev *rdev;
0731     u16 ndestid;
0732 
0733     /* Setup new RIO device */
0734     if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
0735         rdev->prev = prev;
0736         if (prev && rio_is_switch(prev))
0737             prev->rswitch->nextdev[prev_port] = rdev;
0738     } else
0739         return -1;
0740 
0741     if (rio_is_switch(rdev)) {
0742         /* Associated destid is how we accessed this switch */
0743         rdev->destid = destid;
0744 
0745         pr_debug(
0746             "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
0747             rio_name(rdev), rdev->vid, rdev->did,
0748             RIO_GET_TOTAL_PORTS(rdev->swpinfo));
0749         for (port_num = 0;
0750              port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
0751              port_num++) {
0752             if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
0753                 continue;
0754 
0755             if (rio_sport_is_active(rdev, port_num)) {
0756                 pr_debug(
0757                     "RIO: scanning device on port %d\n",
0758                     port_num);
0759 
0760                 rio_lock_device(port, destid, hopcount, 1000);
0761 
0762                 for (ndestid = 0;
0763                      ndestid < RIO_ANY_DESTID(port->sys_size);
0764                      ndestid++) {
0765                     rio_route_get_entry(rdev,
0766                                 RIO_GLOBAL_TABLE,
0767                                 ndestid,
0768                                 &route_port, 0);
0769                     if (route_port == port_num)
0770                         break;
0771                 }
0772 
0773                 if (ndestid == RIO_ANY_DESTID(port->sys_size))
0774                     continue;
0775                 rio_unlock_device(port, destid, hopcount);
0776                 if (rio_disc_peer(net, port, ndestid,
0777                     hopcount + 1, rdev, port_num) < 0)
0778                     return -1;
0779             }
0780         }
0781     } else
0782         pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
0783             rio_name(rdev), rdev->vid, rdev->did);
0784 
0785     return 0;
0786 }
0787 
0788 /**
0789  * rio_mport_is_active- Tests if master port link is active
0790  * @port: Master port to test
0791  *
0792  * Reads the port error status CSR for the master port to
0793  * determine if the port has an active link.  Returns
0794  * %RIO_PORT_N_ERR_STS_PORT_OK if the  master port is active
0795  * or %0 if it is inactive.
0796  */
0797 static int rio_mport_is_active(struct rio_mport *port)
0798 {
0799     u32 result = 0;
0800 
0801     rio_local_read_config_32(port,
0802         port->phys_efptr +
0803             RIO_PORT_N_ERR_STS_CSR(port->index, port->phys_rmap),
0804         &result);
0805     return result & RIO_PORT_N_ERR_STS_PORT_OK;
0806 }
0807 
0808 static void rio_scan_release_net(struct rio_net *net)
0809 {
0810     pr_debug("RIO-SCAN: %s: net_%d\n", __func__, net->id);
0811     kfree(net->enum_data);
0812 }
0813 
0814 static void rio_scan_release_dev(struct device *dev)
0815 {
0816     struct rio_net *net;
0817 
0818     net = to_rio_net(dev);
0819     pr_debug("RIO-SCAN: %s: net_%d\n", __func__, net->id);
0820     kfree(net);
0821 }
0822 
0823 /*
0824  * rio_scan_alloc_net - Allocate and configure a new RIO network
0825  * @mport: Master port associated with the RIO network
0826  * @do_enum: Enumeration/Discovery mode flag
0827  * @start: logical minimal start id for new net
0828  *
0829  * Allocates a new RIO network structure and initializes enumerator-specific
0830  * part of it (if required).
0831  * Returns a RIO network pointer on success or %NULL on failure.
0832  */
0833 static struct rio_net *rio_scan_alloc_net(struct rio_mport *mport,
0834                       int do_enum, u16 start)
0835 {
0836     struct rio_net *net;
0837 
0838     net = rio_alloc_net(mport);
0839 
0840     if (net && do_enum) {
0841         struct rio_id_table *idtab;
0842         size_t size;
0843 
0844         size = sizeof(struct rio_id_table) +
0845                 BITS_TO_LONGS(
0846                     RIO_MAX_ROUTE_ENTRIES(mport->sys_size)
0847                     ) * sizeof(long);
0848 
0849         idtab = kzalloc(size, GFP_KERNEL);
0850 
0851         if (idtab == NULL) {
0852             pr_err("RIO: failed to allocate destID table\n");
0853             rio_free_net(net);
0854             net = NULL;
0855         } else {
0856             net->enum_data = idtab;
0857             net->release = rio_scan_release_net;
0858             idtab->start = start;
0859             idtab->max = RIO_MAX_ROUTE_ENTRIES(mport->sys_size);
0860             spin_lock_init(&idtab->lock);
0861         }
0862     }
0863 
0864     if (net) {
0865         net->id = mport->id;
0866         net->hport = mport;
0867         dev_set_name(&net->dev, "rnet_%d", net->id);
0868         net->dev.parent = &mport->dev;
0869         net->dev.release = rio_scan_release_dev;
0870         rio_add_net(net);
0871     }
0872 
0873     return net;
0874 }
0875 
0876 /**
0877  * rio_update_route_tables- Updates route tables in switches
0878  * @net: RIO network to run update on
0879  *
0880  * For each enumerated device, ensure that each switch in a system
0881  * has correct routing entries. Add routes for devices that where
0882  * unknown during the first enumeration pass through the switch.
0883  */
0884 static void rio_update_route_tables(struct rio_net *net)
0885 {
0886     struct rio_dev *rdev, *swrdev;
0887     struct rio_switch *rswitch;
0888     u8 sport;
0889     u16 destid;
0890 
0891     list_for_each_entry(rdev, &net->devices, net_list) {
0892 
0893         destid = rdev->destid;
0894 
0895         list_for_each_entry(rswitch, &net->switches, node) {
0896 
0897             if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
0898                 continue;
0899 
0900             if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
0901                 swrdev = sw_to_rio_dev(rswitch);
0902 
0903                 /* Skip if destid ends in empty switch*/
0904                 if (swrdev->destid == destid)
0905                     continue;
0906 
0907                 sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
0908 
0909                 rio_route_add_entry(swrdev, RIO_GLOBAL_TABLE,
0910                             destid, sport, 0);
0911                 rswitch->route_table[destid] = sport;
0912             }
0913         }
0914     }
0915 }
0916 
0917 /**
0918  * rio_init_em - Initializes RIO Error Management (for switches)
0919  * @rdev: RIO device
0920  *
0921  * For each enumerated switch, call device-specific error management
0922  * initialization routine (if supplied by the switch driver).
0923  */
0924 static void rio_init_em(struct rio_dev *rdev)
0925 {
0926     if (rio_is_switch(rdev) && (rdev->em_efptr) &&
0927         rdev->rswitch->ops && rdev->rswitch->ops->em_init) {
0928         rdev->rswitch->ops->em_init(rdev);
0929     }
0930 }
0931 
0932 /**
0933  * rio_enum_mport- Start enumeration through a master port
0934  * @mport: Master port to send transactions
0935  * @flags: Enumeration control flags
0936  *
0937  * Starts the enumeration process. If somebody has enumerated our
0938  * master port device, then give up. If not and we have an active
0939  * link, then start recursive peer enumeration. Returns %0 if
0940  * enumeration succeeds or %-EBUSY if enumeration fails.
0941  */
0942 static int rio_enum_mport(struct rio_mport *mport, u32 flags)
0943 {
0944     struct rio_net *net = NULL;
0945     int rc = 0;
0946 
0947     printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
0948            mport->name);
0949 
0950     /*
0951      * To avoid multiple start requests (repeat enumeration is not supported
0952      * by this method) check if enumeration/discovery was performed for this
0953      * mport: if mport was added into the list of mports for a net exit
0954      * with error.
0955      */
0956     if (mport->nnode.next || mport->nnode.prev)
0957         return -EBUSY;
0958 
0959     /* If somebody else enumerated our master port device, bail. */
0960     if (rio_enum_host(mport) < 0) {
0961         printk(KERN_INFO
0962                "RIO: master port %d device has been enumerated by a remote host\n",
0963                mport->id);
0964         rc = -EBUSY;
0965         goto out;
0966     }
0967 
0968     /* If master port has an active link, allocate net and enum peers */
0969     if (rio_mport_is_active(mport)) {
0970         net = rio_scan_alloc_net(mport, 1, 0);
0971         if (!net) {
0972             printk(KERN_ERR "RIO: failed to allocate new net\n");
0973             rc = -ENOMEM;
0974             goto out;
0975         }
0976 
0977         /* reserve mport destID in new net */
0978         rio_destid_reserve(net, mport->host_deviceid);
0979 
0980         /* Enable Input Output Port (transmitter receiver) */
0981         rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
0982 
0983         /* Set component tag for host */
0984         rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
0985                       next_comptag++);
0986 
0987         next_destid = rio_destid_alloc(net);
0988 
0989         if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
0990             /* A higher priority host won enumeration, bail. */
0991             printk(KERN_INFO
0992                    "RIO: master port %d device has lost enumeration to a remote host\n",
0993                    mport->id);
0994             rio_clear_locks(net);
0995             rc = -EBUSY;
0996             goto out;
0997         }
0998         /* free the last allocated destID (unused) */
0999         rio_destid_free(net, next_destid);
1000         rio_update_route_tables(net);
1001         rio_clear_locks(net);
1002         rio_pw_enable(mport, 1);
1003     } else {
1004         printk(KERN_INFO "RIO: master port %d link inactive\n",
1005                mport->id);
1006         rc = -EINVAL;
1007     }
1008 
1009       out:
1010     return rc;
1011 }
1012 
1013 /**
1014  * rio_build_route_tables- Generate route tables from switch route entries
1015  * @net: RIO network to run route tables scan on
1016  *
1017  * For each switch device, generate a route table by copying existing
1018  * route entries from the switch.
1019  */
1020 static void rio_build_route_tables(struct rio_net *net)
1021 {
1022     struct rio_switch *rswitch;
1023     struct rio_dev *rdev;
1024     int i;
1025     u8 sport;
1026 
1027     list_for_each_entry(rswitch, &net->switches, node) {
1028         rdev = sw_to_rio_dev(rswitch);
1029 
1030         rio_lock_device(net->hport, rdev->destid,
1031                 rdev->hopcount, 1000);
1032         for (i = 0;
1033              i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
1034              i++) {
1035             if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
1036                         i, &sport, 0) < 0)
1037                 continue;
1038             rswitch->route_table[i] = sport;
1039         }
1040 
1041         rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
1042     }
1043 }
1044 
1045 /**
1046  * rio_disc_mport- Start discovery through a master port
1047  * @mport: Master port to send transactions
1048  * @flags: discovery control flags
1049  *
1050  * Starts the discovery process. If we have an active link,
1051  * then wait for the signal that enumeration is complete (if wait
1052  * is allowed).
1053  * When enumeration completion is signaled, start recursive
1054  * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1055  * on failure.
1056  */
1057 static int rio_disc_mport(struct rio_mport *mport, u32 flags)
1058 {
1059     struct rio_net *net = NULL;
1060     unsigned long to_end;
1061 
1062     printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1063            mport->name);
1064 
1065     /* If master port has an active link, allocate net and discover peers */
1066     if (rio_mport_is_active(mport)) {
1067         if (rio_enum_complete(mport))
1068             goto enum_done;
1069         else if (flags & RIO_SCAN_ENUM_NO_WAIT)
1070             return -EAGAIN;
1071 
1072         pr_debug("RIO: wait for enumeration to complete...\n");
1073 
1074         to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1075         while (time_before(jiffies, to_end)) {
1076             if (rio_enum_complete(mport))
1077                 goto enum_done;
1078             msleep(10);
1079         }
1080 
1081         pr_debug("RIO: discovery timeout on mport %d %s\n",
1082              mport->id, mport->name);
1083         goto bail;
1084 enum_done:
1085         pr_debug("RIO: ... enumeration done\n");
1086 
1087         net = rio_scan_alloc_net(mport, 0, 0);
1088         if (!net) {
1089             printk(KERN_ERR "RIO: Failed to allocate new net\n");
1090             goto bail;
1091         }
1092 
1093         /* Read DestID assigned by enumerator */
1094         rio_local_read_config_32(mport, RIO_DID_CSR,
1095                      &mport->host_deviceid);
1096         mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1097                            mport->host_deviceid);
1098 
1099         if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1100                     0, NULL, 0) < 0) {
1101             printk(KERN_INFO
1102                    "RIO: master port %d device has failed discovery\n",
1103                    mport->id);
1104             goto bail;
1105         }
1106 
1107         rio_build_route_tables(net);
1108     }
1109 
1110     return 0;
1111 bail:
1112     return -EBUSY;
1113 }
1114 
1115 static struct rio_scan rio_scan_ops = {
1116     .owner = THIS_MODULE,
1117     .enumerate = rio_enum_mport,
1118     .discover = rio_disc_mport,
1119 };
1120 
1121 static bool scan;
1122 module_param(scan, bool, 0);
1123 MODULE_PARM_DESC(scan, "Start RapidIO network enumeration/discovery "
1124             "(default = 0)");
1125 
1126 /**
1127  * rio_basic_attach:
1128  *
1129  * When this enumeration/discovery method is loaded as a module this function
1130  * registers its specific enumeration and discover routines for all available
1131  * RapidIO mport devices. The "scan" command line parameter controls ability of
1132  * the module to start RapidIO enumeration/discovery automatically.
1133  *
1134  * Returns 0 for success or -EIO if unable to register itself.
1135  *
1136  * This enumeration/discovery method cannot be unloaded and therefore does not
1137  * provide a matching cleanup_module routine.
1138  */
1139 
1140 static int __init rio_basic_attach(void)
1141 {
1142     if (rio_register_scan(RIO_MPORT_ANY, &rio_scan_ops))
1143         return -EIO;
1144     if (scan)
1145         rio_init_mports();
1146     return 0;
1147 }
1148 
1149 late_initcall(rio_basic_attach);
1150 
1151 MODULE_DESCRIPTION("Basic RapidIO enumeration/discovery");
1152 MODULE_LICENSE("GPL");