Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * RapidIO enumeration and discovery support |
| 3 | * |
| 4 | * Copyright 2005 MontaVista Software, Inc. |
| 5 | * Matt Porter <mporter@kernel.crashing.org> |
| 6 | * |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 7 | * Copyright 2009 Integrated Device Technology, Inc. |
| 8 | * Alex Bounine <alexandre.bounine@idt.com> |
| 9 | * - Added Port-Write/Error Management initialization and handling |
| 10 | * |
Thomas Moll | 933af4a | 2010-05-26 14:44:01 -0700 | [diff] [blame] | 11 | * Copyright 2009 Sysgo AG |
| 12 | * Thomas Moll <thomas.moll@sysgo.com> |
| 13 | * - Added Input- Output- enable functionality, to allow full communication |
| 14 | * |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 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. |
| 19 | */ |
| 20 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 21 | #include <linux/types.h> |
| 22 | #include <linux/kernel.h> |
| 23 | |
| 24 | #include <linux/delay.h> |
Matt Porter | fa78cc5 | 2005-11-07 01:00:18 -0800 | [diff] [blame] | 25 | #include <linux/dma-mapping.h> |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 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> |
Alexandre Bounine | fa3dbaa | 2012-10-04 17:16:04 -0700 | [diff] [blame] | 34 | #include <linux/sched.h> |
Tim Schmielau | de25968 | 2006-01-08 01:02:05 -0800 | [diff] [blame] | 35 | #include <linux/jiffies.h> |
| 36 | #include <linux/slab.h> |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 37 | |
| 38 | #include "rio.h" |
| 39 | |
| 40 | LIST_HEAD(rio_devices); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 41 | |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 42 | static void rio_init_em(struct rio_dev *rdev); |
| 43 | |
Matt Porter | fa78cc5 | 2005-11-07 01:00:18 -0800 | [diff] [blame] | 44 | DEFINE_SPINLOCK(rio_global_list_lock); |
| 45 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 46 | static int next_destid = 0; |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 47 | static int next_comptag = 1; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 48 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 49 | static int rio_mport_phys_table[] = { |
| 50 | RIO_EFB_PAR_EP_ID, |
| 51 | RIO_EFB_PAR_EP_REC_ID, |
| 52 | RIO_EFB_SER_EP_ID, |
| 53 | RIO_EFB_SER_EP_REC_ID, |
| 54 | -1, |
| 55 | }; |
| 56 | |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 57 | |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 58 | /** |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 59 | * rio_destid_alloc - Allocate next available destID for given network |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 60 | * @net: RIO network |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 61 | * |
| 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. |
| 65 | */ |
| 66 | static u16 rio_destid_alloc(struct rio_net *net) |
| 67 | { |
| 68 | int destid; |
| 69 | struct rio_id_table *idtab = &net->destid_table; |
| 70 | |
| 71 | spin_lock(&idtab->lock); |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 72 | destid = find_first_zero_bit(idtab->table, idtab->max); |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 73 | |
| 74 | if (destid < idtab->max) { |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 75 | set_bit(destid, idtab->table); |
| 76 | destid += idtab->start; |
| 77 | } else |
| 78 | destid = RIO_INVALID_DESTID; |
| 79 | |
| 80 | spin_unlock(&idtab->lock); |
| 81 | return (u16)destid; |
| 82 | } |
| 83 | |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 84 | /** |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 85 | * rio_destid_reserve - Reserve the specivied destID |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 86 | * @net: RIO network |
| 87 | * @destid: destID to reserve |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 88 | * |
| 89 | * Tries to reserve the specified destID. |
| 90 | * Returns 0 if successfull. |
| 91 | */ |
| 92 | static int rio_destid_reserve(struct rio_net *net, u16 destid) |
| 93 | { |
| 94 | int oldbit; |
| 95 | struct rio_id_table *idtab = &net->destid_table; |
| 96 | |
| 97 | destid -= idtab->start; |
| 98 | spin_lock(&idtab->lock); |
| 99 | oldbit = test_and_set_bit(destid, idtab->table); |
| 100 | spin_unlock(&idtab->lock); |
| 101 | return oldbit; |
| 102 | } |
| 103 | |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 104 | /** |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 105 | * rio_destid_free - free a previously allocated destID |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 106 | * @net: RIO network |
| 107 | * @destid: destID to free |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 108 | * |
| 109 | * Makes the specified destID available for use. |
| 110 | */ |
| 111 | static void rio_destid_free(struct rio_net *net, u16 destid) |
| 112 | { |
| 113 | struct rio_id_table *idtab = &net->destid_table; |
| 114 | |
| 115 | destid -= idtab->start; |
| 116 | spin_lock(&idtab->lock); |
| 117 | clear_bit(destid, idtab->table); |
| 118 | spin_unlock(&idtab->lock); |
| 119 | } |
| 120 | |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 121 | /** |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 122 | * rio_destid_first - return first destID in use |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 123 | * @net: RIO network |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 124 | */ |
| 125 | static u16 rio_destid_first(struct rio_net *net) |
| 126 | { |
| 127 | int destid; |
| 128 | struct rio_id_table *idtab = &net->destid_table; |
| 129 | |
| 130 | spin_lock(&idtab->lock); |
| 131 | destid = find_first_bit(idtab->table, idtab->max); |
| 132 | if (destid >= idtab->max) |
| 133 | destid = RIO_INVALID_DESTID; |
| 134 | else |
| 135 | destid += idtab->start; |
| 136 | spin_unlock(&idtab->lock); |
| 137 | return (u16)destid; |
| 138 | } |
| 139 | |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 140 | /** |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 141 | * rio_destid_next - return next destID in use |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 142 | * @net: RIO network |
| 143 | * @from: destination ID from which search shall continue |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 144 | */ |
| 145 | static u16 rio_destid_next(struct rio_net *net, u16 from) |
| 146 | { |
| 147 | int destid; |
| 148 | struct rio_id_table *idtab = &net->destid_table; |
| 149 | |
| 150 | spin_lock(&idtab->lock); |
| 151 | destid = find_next_bit(idtab->table, idtab->max, from); |
| 152 | if (destid >= idtab->max) |
| 153 | destid = RIO_INVALID_DESTID; |
| 154 | else |
| 155 | destid += idtab->start; |
| 156 | spin_unlock(&idtab->lock); |
| 157 | return (u16)destid; |
| 158 | } |
| 159 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 160 | /** |
| 161 | * rio_get_device_id - Get the base/extended device id for a device |
| 162 | * @port: RIO master port |
| 163 | * @destid: Destination ID of device |
| 164 | * @hopcount: Hopcount to device |
| 165 | * |
| 166 | * Reads the base/extended device id from a device. Returns the |
| 167 | * 8/16-bit device ID. |
| 168 | */ |
| 169 | static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount) |
| 170 | { |
| 171 | u32 result; |
| 172 | |
| 173 | rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result); |
| 174 | |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 175 | return RIO_GET_DID(port->sys_size, result); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
| 179 | * rio_set_device_id - Set the base/extended device id for a device |
| 180 | * @port: RIO master port |
| 181 | * @destid: Destination ID of device |
| 182 | * @hopcount: Hopcount to device |
| 183 | * @did: Device ID value to be written |
| 184 | * |
| 185 | * Writes the base/extended device id from a device. |
| 186 | */ |
Matt Porter | fa78cc5 | 2005-11-07 01:00:18 -0800 | [diff] [blame] | 187 | static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 188 | { |
| 189 | rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR, |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 190 | RIO_SET_DID(port->sys_size, did)); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * rio_local_set_device_id - Set the base/extended device id for a port |
| 195 | * @port: RIO master port |
| 196 | * @did: Device ID value to be written |
| 197 | * |
| 198 | * Writes the base/extended device id from a device. |
| 199 | */ |
| 200 | static void rio_local_set_device_id(struct rio_mport *port, u16 did) |
| 201 | { |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 202 | rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size, |
| 203 | did)); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * rio_clear_locks- Release all host locks and signal enumeration complete |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 208 | * @net: RIO network to run on |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 209 | * |
| 210 | * Marks the component tag CSR on each device with the enumeration |
| 211 | * complete flag. When complete, it then release the host locks on |
| 212 | * each device. Returns 0 on success or %-EINVAL on failure. |
| 213 | */ |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 214 | static int rio_clear_locks(struct rio_net *net) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 215 | { |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 216 | struct rio_mport *port = net->hport; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 217 | struct rio_dev *rdev; |
| 218 | u32 result; |
| 219 | int ret = 0; |
| 220 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 221 | /* Release host device id locks */ |
| 222 | rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR, |
| 223 | port->host_deviceid); |
| 224 | rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result); |
| 225 | if ((result & 0xffff) != 0xffff) { |
| 226 | printk(KERN_INFO |
| 227 | "RIO: badness when releasing host lock on master port, result %8.8x\n", |
| 228 | result); |
| 229 | ret = -EINVAL; |
| 230 | } |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 231 | list_for_each_entry(rdev, &net->devices, net_list) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 232 | rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR, |
| 233 | port->host_deviceid); |
| 234 | rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result); |
| 235 | if ((result & 0xffff) != 0xffff) { |
| 236 | printk(KERN_INFO |
| 237 | "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n", |
| 238 | rdev->vid, rdev->did); |
| 239 | ret = -EINVAL; |
| 240 | } |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 241 | |
| 242 | /* Mark device as discovered and enable master */ |
| 243 | rio_read_config_32(rdev, |
| 244 | rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR, |
| 245 | &result); |
| 246 | result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER; |
| 247 | rio_write_config_32(rdev, |
| 248 | rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR, |
| 249 | result); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * rio_enum_host- Set host lock and initialize host destination ID |
| 257 | * @port: Master port to issue transaction |
| 258 | * |
| 259 | * Sets the local host master port lock and destination ID register |
| 260 | * with the host device ID value. The host device ID value is provided |
| 261 | * by the platform. Returns %0 on success or %-1 on failure. |
| 262 | */ |
| 263 | static int rio_enum_host(struct rio_mport *port) |
| 264 | { |
| 265 | u32 result; |
| 266 | |
| 267 | /* Set master port host device id lock */ |
| 268 | rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR, |
| 269 | port->host_deviceid); |
| 270 | |
| 271 | rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result); |
| 272 | if ((result & 0xffff) != port->host_deviceid) |
| 273 | return -1; |
| 274 | |
| 275 | /* Set master port destid and init destid ctr */ |
| 276 | rio_local_set_device_id(port, port->host_deviceid); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * rio_device_has_destid- Test if a device contains a destination ID register |
| 282 | * @port: Master port to issue transaction |
| 283 | * @src_ops: RIO device source operations |
| 284 | * @dst_ops: RIO device destination operations |
| 285 | * |
| 286 | * Checks the provided @src_ops and @dst_ops for the necessary transaction |
| 287 | * capabilities that indicate whether or not a device will implement a |
| 288 | * destination ID register. Returns 1 if true or 0 if false. |
| 289 | */ |
| 290 | static int rio_device_has_destid(struct rio_mport *port, int src_ops, |
| 291 | int dst_ops) |
| 292 | { |
Matt Porter | fa78cc5 | 2005-11-07 01:00:18 -0800 | [diff] [blame] | 293 | 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; |
| 294 | |
| 295 | return !!((src_ops | dst_ops) & mask); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * rio_release_dev- Frees a RIO device struct |
| 300 | * @dev: LDM device associated with a RIO device struct |
| 301 | * |
| 302 | * Gets the RIO device struct associated a RIO device struct. |
| 303 | * The RIO device struct is freed. |
| 304 | */ |
| 305 | static void rio_release_dev(struct device *dev) |
| 306 | { |
| 307 | struct rio_dev *rdev; |
| 308 | |
| 309 | rdev = to_rio_dev(dev); |
| 310 | kfree(rdev); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * rio_is_switch- Tests if a RIO device has switch capabilities |
| 315 | * @rdev: RIO device |
| 316 | * |
| 317 | * Gets the RIO device Processing Element Features register |
| 318 | * contents and tests for switch capabilities. Returns 1 if |
| 319 | * the device is a switch or 0 if it is not a switch. |
| 320 | * The RIO device struct is freed. |
| 321 | */ |
| 322 | static int rio_is_switch(struct rio_dev *rdev) |
| 323 | { |
| 324 | if (rdev->pef & RIO_PEF_SWITCH) |
| 325 | return 1; |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
Alexandre Bounine | 058f88d6 | 2010-05-26 14:44:03 -0700 | [diff] [blame] | 330 | * rio_switch_init - Sets switch operations for a particular vendor switch |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 331 | * @rdev: RIO device |
Alexandre Bounine | 058f88d6 | 2010-05-26 14:44:03 -0700 | [diff] [blame] | 332 | * @do_enum: Enumeration/Discovery mode flag |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 333 | * |
Alexandre Bounine | 058f88d6 | 2010-05-26 14:44:03 -0700 | [diff] [blame] | 334 | * Searches the RIO switch ops table for known switch types. If the vid |
| 335 | * and did match a switch table entry, then call switch initialization |
| 336 | * routine to setup switch-specific routines. |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 337 | */ |
Alexandre Bounine | 058f88d6 | 2010-05-26 14:44:03 -0700 | [diff] [blame] | 338 | static void rio_switch_init(struct rio_dev *rdev, int do_enum) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 339 | { |
Alexandre Bounine | 058f88d6 | 2010-05-26 14:44:03 -0700 | [diff] [blame] | 340 | struct rio_switch_ops *cur = __start_rio_switch_ops; |
| 341 | struct rio_switch_ops *end = __end_rio_switch_ops; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 342 | |
| 343 | while (cur < end) { |
| 344 | if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) { |
Alexandre Bounine | 058f88d6 | 2010-05-26 14:44:03 -0700 | [diff] [blame] | 345 | pr_debug("RIO: calling init routine for %s\n", |
| 346 | rio_name(rdev)); |
| 347 | cur->init_hook(rdev, do_enum); |
Alexandre Bounine | 07590ff | 2010-05-26 14:43:57 -0700 | [diff] [blame] | 348 | break; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 349 | } |
| 350 | cur++; |
| 351 | } |
| 352 | |
Alexandre Bounine | 07590ff | 2010-05-26 14:43:57 -0700 | [diff] [blame] | 353 | if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) { |
| 354 | pr_debug("RIO: adding STD routing ops for %s\n", |
| 355 | rio_name(rdev)); |
| 356 | rdev->rswitch->add_entry = rio_std_route_add_entry; |
| 357 | rdev->rswitch->get_entry = rio_std_route_get_entry; |
| 358 | rdev->rswitch->clr_table = rio_std_route_clr_table; |
| 359 | } |
| 360 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 361 | if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry) |
| 362 | printk(KERN_ERR "RIO: missing routing ops for %s\n", |
| 363 | rio_name(rdev)); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * rio_add_device- Adds a RIO device to the device model |
| 368 | * @rdev: RIO device |
| 369 | * |
| 370 | * Adds the RIO device to the global device list and adds the RIO |
| 371 | * device to the RIO device list. Creates the generic sysfs nodes |
| 372 | * for an RIO device. |
| 373 | */ |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 374 | static int __devinit rio_add_device(struct rio_dev *rdev) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 375 | { |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 376 | int err; |
| 377 | |
| 378 | err = device_add(&rdev->dev); |
| 379 | if (err) |
| 380 | return err; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 381 | |
| 382 | spin_lock(&rio_global_list_lock); |
| 383 | list_add_tail(&rdev->global_list, &rio_devices); |
| 384 | spin_unlock(&rio_global_list_lock); |
| 385 | |
| 386 | rio_create_sysfs_dev_files(rdev); |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 387 | |
| 388 | return 0; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 392 | * rio_enable_rx_tx_port - enable input receiver and output transmitter of |
Thomas Moll | 933af4a | 2010-05-26 14:44:01 -0700 | [diff] [blame] | 393 | * given port |
| 394 | * @port: Master port associated with the RIO network |
| 395 | * @local: local=1 select local port otherwise a far device is reached |
| 396 | * @destid: Destination ID of the device to check host bit |
| 397 | * @hopcount: Number of hops to reach the target |
| 398 | * @port_num: Port (-number on switch) to enable on a far end device |
| 399 | * |
| 400 | * Returns 0 or 1 from on General Control Command and Status Register |
| 401 | * (EXT_PTR+0x3C) |
| 402 | */ |
| 403 | inline int rio_enable_rx_tx_port(struct rio_mport *port, |
| 404 | int local, u16 destid, |
| 405 | u8 hopcount, u8 port_num) { |
| 406 | #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS |
| 407 | u32 regval; |
| 408 | u32 ext_ftr_ptr; |
| 409 | |
| 410 | /* |
| 411 | * enable rx input tx output port |
| 412 | */ |
| 413 | pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = " |
| 414 | "%d, port_num = %d)\n", local, destid, hopcount, port_num); |
| 415 | |
| 416 | ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount); |
| 417 | |
| 418 | if (local) { |
| 419 | rio_local_read_config_32(port, ext_ftr_ptr + |
| 420 | RIO_PORT_N_CTL_CSR(0), |
| 421 | ®val); |
| 422 | } else { |
| 423 | if (rio_mport_read_config_32(port, destid, hopcount, |
| 424 | ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), ®val) < 0) |
| 425 | return -EIO; |
| 426 | } |
| 427 | |
| 428 | if (regval & RIO_PORT_N_CTL_P_TYP_SER) { |
| 429 | /* serial */ |
| 430 | regval = regval | RIO_PORT_N_CTL_EN_RX_SER |
| 431 | | RIO_PORT_N_CTL_EN_TX_SER; |
| 432 | } else { |
| 433 | /* parallel */ |
| 434 | regval = regval | RIO_PORT_N_CTL_EN_RX_PAR |
| 435 | | RIO_PORT_N_CTL_EN_TX_PAR; |
| 436 | } |
| 437 | |
| 438 | if (local) { |
| 439 | rio_local_write_config_32(port, ext_ftr_ptr + |
| 440 | RIO_PORT_N_CTL_CSR(0), regval); |
| 441 | } else { |
| 442 | if (rio_mport_write_config_32(port, destid, hopcount, |
| 443 | ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0) |
| 444 | return -EIO; |
| 445 | } |
| 446 | #endif |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | /** |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 451 | * rio_setup_device- Allocates and sets up a RIO device |
| 452 | * @net: RIO network |
| 453 | * @port: Master port to send transactions |
| 454 | * @destid: Current destination ID |
| 455 | * @hopcount: Current hopcount |
| 456 | * @do_enum: Enumeration/Discovery mode flag |
| 457 | * |
| 458 | * Allocates a RIO device and configures fields based on configuration |
| 459 | * space contents. If device has a destination ID register, a destination |
| 460 | * ID is either assigned in enumeration mode or read from configuration |
| 461 | * space in discovery mode. If the device has switch capabilities, then |
| 462 | * a switch is allocated and configured appropriately. Returns a pointer |
| 463 | * to a RIO device on success or NULL on failure. |
| 464 | * |
| 465 | */ |
Li Yang | 181a6ff | 2009-05-12 16:36:03 +0800 | [diff] [blame] | 466 | static struct rio_dev __devinit *rio_setup_device(struct rio_net *net, |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 467 | struct rio_mport *port, u16 destid, |
| 468 | u8 hopcount, int do_enum) |
| 469 | { |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 470 | int ret = 0; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 471 | struct rio_dev *rdev; |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 472 | struct rio_switch *rswitch = NULL; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 473 | int result, rdid; |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 474 | size_t size; |
| 475 | u32 swpinfo = 0; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 476 | |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 477 | size = sizeof(struct rio_dev); |
| 478 | if (rio_mport_read_config_32(port, destid, hopcount, |
| 479 | RIO_PEF_CAR, &result)) |
| 480 | return NULL; |
| 481 | |
| 482 | if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) { |
| 483 | rio_mport_read_config_32(port, destid, hopcount, |
| 484 | RIO_SWP_INFO_CAR, &swpinfo); |
| 485 | if (result & RIO_PEF_SWITCH) { |
| 486 | size += (RIO_GET_TOTAL_PORTS(swpinfo) * |
| 487 | sizeof(rswitch->nextdev[0])) + sizeof(*rswitch); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | rdev = kzalloc(size, GFP_KERNEL); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 492 | if (!rdev) |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 493 | return NULL; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 494 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 495 | rdev->net = net; |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 496 | rdev->pef = result; |
| 497 | rdev->swpinfo = swpinfo; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 498 | rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR, |
| 499 | &result); |
| 500 | rdev->did = result >> 16; |
| 501 | rdev->vid = result & 0xffff; |
| 502 | rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR, |
| 503 | &rdev->device_rev); |
| 504 | rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR, |
| 505 | &result); |
| 506 | rdev->asm_did = result >> 16; |
| 507 | rdev->asm_vid = result & 0xffff; |
| 508 | rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR, |
| 509 | &result); |
| 510 | rdev->asm_rev = result >> 16; |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 511 | if (rdev->pef & RIO_PEF_EXT_FEATURES) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 512 | rdev->efptr = result & 0xffff; |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 513 | rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid, |
| 514 | hopcount); |
| 515 | |
| 516 | rdev->em_efptr = rio_mport_get_feature(port, 0, destid, |
| 517 | hopcount, RIO_EFB_ERR_MGMNT); |
| 518 | } |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 519 | |
| 520 | rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR, |
| 521 | &rdev->src_ops); |
| 522 | rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR, |
| 523 | &rdev->dst_ops); |
| 524 | |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 525 | if (do_enum) { |
| 526 | /* Assign component tag to device */ |
| 527 | if (next_comptag >= 0x10000) { |
| 528 | pr_err("RIO: Component Tag Counter Overflow\n"); |
| 529 | goto cleanup; |
| 530 | } |
| 531 | rio_mport_write_config_32(port, destid, hopcount, |
| 532 | RIO_COMPONENT_TAG_CSR, next_comptag); |
| 533 | rdev->comp_tag = next_comptag++; |
Alexandre Bounine | 558bda6 | 2011-01-12 17:00:42 -0800 | [diff] [blame] | 534 | } else { |
| 535 | rio_mport_read_config_32(port, destid, hopcount, |
| 536 | RIO_COMPONENT_TAG_CSR, |
| 537 | &rdev->comp_tag); |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 540 | if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) { |
| 541 | if (do_enum) { |
| 542 | rio_set_device_id(port, destid, hopcount, next_destid); |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 543 | rdev->destid = next_destid; |
| 544 | next_destid = rio_destid_alloc(net); |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 545 | } else |
| 546 | rdev->destid = rio_get_device_id(port, destid, hopcount); |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 547 | |
| 548 | rdev->hopcount = 0xff; |
| 549 | } else { |
| 550 | /* Switch device has an associated destID which |
| 551 | * will be adjusted later |
| 552 | */ |
| 553 | rdev->destid = destid; |
| 554 | rdev->hopcount = hopcount; |
| 555 | } |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 556 | |
| 557 | /* If a PE has both switch and other functions, show it as a switch */ |
| 558 | if (rio_is_switch(rdev)) { |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 559 | rswitch = rdev->rswitch; |
Alexandre Bounine | 558bda6 | 2011-01-12 17:00:42 -0800 | [diff] [blame] | 560 | rswitch->switchid = rdev->comp_tag & RIO_CTAG_UDEVID; |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 561 | rswitch->port_ok = 0; |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 562 | rswitch->route_table = kzalloc(sizeof(u8)* |
| 563 | RIO_MAX_ROUTE_ENTRIES(port->sys_size), |
| 564 | GFP_KERNEL); |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 565 | if (!rswitch->route_table) |
| 566 | goto cleanup; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 567 | /* Initialize switch route table */ |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 568 | for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size); |
| 569 | rdid++) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 570 | rswitch->route_table[rdid] = RIO_INVALID_ROUTE; |
Kay Sievers | b53c7583 | 2008-12-04 10:01:52 -0800 | [diff] [blame] | 571 | dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id, |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 572 | rswitch->switchid); |
Alexandre Bounine | 058f88d6 | 2010-05-26 14:44:03 -0700 | [diff] [blame] | 573 | rio_switch_init(rdev, do_enum); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 574 | |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 575 | if (do_enum && rswitch->clr_table) |
| 576 | rswitch->clr_table(port, destid, hopcount, |
| 577 | RIO_GLOBAL_TABLE); |
Alexandre Bounine | 07590ff | 2010-05-26 14:43:57 -0700 | [diff] [blame] | 578 | |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 579 | list_add_tail(&rswitch->node, &net->switches); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 580 | |
Thomas Moll | 933af4a | 2010-05-26 14:44:01 -0700 | [diff] [blame] | 581 | } else { |
| 582 | if (do_enum) |
| 583 | /*Enable Input Output Port (transmitter reviever)*/ |
| 584 | rio_enable_rx_tx_port(port, 0, destid, hopcount, 0); |
| 585 | |
Kay Sievers | b53c7583 | 2008-12-04 10:01:52 -0800 | [diff] [blame] | 586 | dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id, |
| 587 | rdev->destid); |
Thomas Moll | 933af4a | 2010-05-26 14:44:01 -0700 | [diff] [blame] | 588 | } |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 589 | |
| 590 | rdev->dev.bus = &rio_bus_type; |
Alexandre Bounine | 2c70f02 | 2010-10-27 15:34:26 -0700 | [diff] [blame] | 591 | rdev->dev.parent = &rio_bus; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 592 | |
| 593 | device_initialize(&rdev->dev); |
| 594 | rdev->dev.release = rio_release_dev; |
| 595 | rio_dev_get(rdev); |
| 596 | |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 597 | rdev->dma_mask = DMA_BIT_MASK(32); |
Matt Porter | fa78cc5 | 2005-11-07 01:00:18 -0800 | [diff] [blame] | 598 | rdev->dev.dma_mask = &rdev->dma_mask; |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 599 | rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 600 | |
Alexandre Bounine | 284fb68 | 2011-08-25 15:59:13 -0700 | [diff] [blame] | 601 | if (rdev->dst_ops & RIO_DST_OPS_DOORBELL) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 602 | rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE], |
| 603 | 0, 0xffff); |
| 604 | |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 605 | ret = rio_add_device(rdev); |
| 606 | if (ret) |
| 607 | goto cleanup; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 608 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 609 | return rdev; |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 610 | |
| 611 | cleanup: |
Alexandre Bounine | 166c050 | 2011-11-02 13:39:11 -0700 | [diff] [blame] | 612 | if (rswitch) |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 613 | kfree(rswitch->route_table); |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 614 | |
Yang Li | 5f28c52 | 2009-05-11 22:36:02 +0000 | [diff] [blame] | 615 | kfree(rdev); |
| 616 | return NULL; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /** |
| 620 | * rio_sport_is_active- Tests if a switch port has an active connection. |
| 621 | * @port: Master port to send transaction |
| 622 | * @destid: Associated destination ID for switch |
| 623 | * @hopcount: Hopcount to reach switch |
| 624 | * @sport: Switch port number |
| 625 | * |
| 626 | * Reads the port error status CSR for a particular switch port to |
| 627 | * determine if the port has an active link. Returns |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 628 | * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 629 | * inactive. |
| 630 | */ |
| 631 | static int |
| 632 | rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport) |
| 633 | { |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 634 | u32 result = 0; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 635 | u32 ext_ftr_ptr; |
| 636 | |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 637 | ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 638 | |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 639 | while (ext_ftr_ptr) { |
| 640 | rio_mport_read_config_32(port, destid, hopcount, |
| 641 | ext_ftr_ptr, &result); |
| 642 | result = RIO_GET_BLOCK_ID(result); |
| 643 | if ((result == RIO_EFB_SER_EP_FREE_ID) || |
| 644 | (result == RIO_EFB_SER_EP_FREE_ID_V13P) || |
| 645 | (result == RIO_EFB_SER_EP_FREC_ID)) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 646 | break; |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 647 | |
| 648 | ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, |
| 649 | ext_ftr_ptr); |
| 650 | } |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 651 | |
| 652 | if (ext_ftr_ptr) |
| 653 | rio_mport_read_config_32(port, destid, hopcount, |
| 654 | ext_ftr_ptr + |
| 655 | RIO_PORT_N_ERR_STS_CSR(sport), |
| 656 | &result); |
| 657 | |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 658 | return result & RIO_PORT_N_ERR_STS_PORT_OK; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /** |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 662 | * rio_lock_device - Acquires host device lock for specified device |
| 663 | * @port: Master port to send transaction |
| 664 | * @destid: Destination ID for device/switch |
| 665 | * @hopcount: Hopcount to reach switch |
| 666 | * @wait_ms: Max wait time in msec (0 = no timeout) |
| 667 | * |
| 668 | * Attepts to acquire host device lock for specified device |
| 669 | * Returns 0 if device lock acquired or EINVAL if timeout expires. |
| 670 | */ |
| 671 | static int |
| 672 | rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms) |
| 673 | { |
| 674 | u32 result; |
| 675 | int tcnt = 0; |
| 676 | |
| 677 | /* Attempt to acquire device lock */ |
| 678 | rio_mport_write_config_32(port, destid, hopcount, |
| 679 | RIO_HOST_DID_LOCK_CSR, port->host_deviceid); |
| 680 | rio_mport_read_config_32(port, destid, hopcount, |
| 681 | RIO_HOST_DID_LOCK_CSR, &result); |
| 682 | |
| 683 | while (result != port->host_deviceid) { |
| 684 | if (wait_ms != 0 && tcnt == wait_ms) { |
| 685 | pr_debug("RIO: timeout when locking device %x:%x\n", |
| 686 | destid, hopcount); |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | |
| 690 | /* Delay a bit */ |
| 691 | mdelay(1); |
| 692 | tcnt++; |
| 693 | /* Try to acquire device lock again */ |
| 694 | rio_mport_write_config_32(port, destid, |
| 695 | hopcount, |
| 696 | RIO_HOST_DID_LOCK_CSR, |
| 697 | port->host_deviceid); |
| 698 | rio_mport_read_config_32(port, destid, |
| 699 | hopcount, |
| 700 | RIO_HOST_DID_LOCK_CSR, &result); |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * rio_unlock_device - Releases host device lock for specified device |
| 708 | * @port: Master port to send transaction |
| 709 | * @destid: Destination ID for device/switch |
| 710 | * @hopcount: Hopcount to reach switch |
| 711 | * |
| 712 | * Returns 0 if device lock released or EINVAL if fails. |
| 713 | */ |
| 714 | static int |
| 715 | rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount) |
| 716 | { |
| 717 | u32 result; |
| 718 | |
| 719 | /* Release device lock */ |
| 720 | rio_mport_write_config_32(port, destid, |
| 721 | hopcount, |
| 722 | RIO_HOST_DID_LOCK_CSR, |
| 723 | port->host_deviceid); |
| 724 | rio_mport_read_config_32(port, destid, hopcount, |
| 725 | RIO_HOST_DID_LOCK_CSR, &result); |
| 726 | if ((result & 0xffff) != 0xffff) { |
| 727 | pr_debug("RIO: badness when releasing device lock %x:%x\n", |
| 728 | destid, hopcount); |
| 729 | return -EINVAL; |
| 730 | } |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | /** |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 736 | * rio_route_add_entry- Add a route entry to a switch routing table |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 737 | * @rdev: RIO device |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 738 | * @table: Routing table ID |
| 739 | * @route_destid: Destination ID to be routed |
| 740 | * @route_port: Port number to be routed |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 741 | * @lock: lock switch device flag |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 742 | * |
| 743 | * Calls the switch specific add_entry() method to add a route entry |
| 744 | * on a switch. The route table can be specified using the @table |
| 745 | * argument if a switch has per port routing tables or the normal |
| 746 | * use is to specific all tables (or the global table) by passing |
| 747 | * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL |
| 748 | * on failure. |
| 749 | */ |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 750 | static int |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 751 | rio_route_add_entry(struct rio_dev *rdev, |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 752 | u16 table, u16 route_destid, u8 route_port, int lock) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 753 | { |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 754 | int rc; |
| 755 | |
| 756 | if (lock) { |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 757 | rc = rio_lock_device(rdev->net->hport, rdev->destid, |
| 758 | rdev->hopcount, 1000); |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 759 | if (rc) |
| 760 | return rc; |
| 761 | } |
| 762 | |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 763 | rc = rdev->rswitch->add_entry(rdev->net->hport, rdev->destid, |
| 764 | rdev->hopcount, table, |
| 765 | route_destid, route_port); |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 766 | if (lock) |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 767 | rio_unlock_device(rdev->net->hport, rdev->destid, |
| 768 | rdev->hopcount); |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 769 | |
| 770 | return rc; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /** |
| 774 | * rio_route_get_entry- Read a route entry in a switch routing table |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 775 | * @rdev: RIO device |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 776 | * @table: Routing table ID |
| 777 | * @route_destid: Destination ID to be routed |
| 778 | * @route_port: Pointer to read port number into |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 779 | * @lock: lock switch device flag |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 780 | * |
| 781 | * Calls the switch specific get_entry() method to read a route entry |
| 782 | * in a switch. The route table can be specified using the @table |
| 783 | * argument if a switch has per port routing tables or the normal |
| 784 | * use is to specific all tables (or the global table) by passing |
| 785 | * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL |
| 786 | * on failure. |
| 787 | */ |
| 788 | static int |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 789 | rio_route_get_entry(struct rio_dev *rdev, u16 table, |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 790 | u16 route_destid, u8 *route_port, int lock) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 791 | { |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 792 | int rc; |
| 793 | |
| 794 | if (lock) { |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 795 | rc = rio_lock_device(rdev->net->hport, rdev->destid, |
| 796 | rdev->hopcount, 1000); |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 797 | if (rc) |
| 798 | return rc; |
| 799 | } |
| 800 | |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 801 | rc = rdev->rswitch->get_entry(rdev->net->hport, rdev->destid, |
| 802 | rdev->hopcount, table, |
| 803 | route_destid, route_port); |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 804 | if (lock) |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 805 | rio_unlock_device(rdev->net->hport, rdev->destid, |
| 806 | rdev->hopcount); |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 807 | |
| 808 | return rc; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /** |
| 812 | * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device |
| 813 | * @port: Master port to send transaction |
| 814 | * @hopcount: Number of hops to the device |
| 815 | * |
| 816 | * Used during enumeration to read the Host Device ID Lock CSR on a |
| 817 | * RIO device. Returns the value of the lock register. |
| 818 | */ |
| 819 | static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount) |
| 820 | { |
| 821 | u32 result; |
| 822 | |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 823 | rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount, |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 824 | RIO_HOST_DID_LOCK_CSR, &result); |
| 825 | |
| 826 | return (u16) (result & 0xffff); |
| 827 | } |
| 828 | |
| 829 | /** |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 830 | * rio_enum_peer- Recursively enumerate a RIO network through a master port |
| 831 | * @net: RIO network being enumerated |
| 832 | * @port: Master port to send transactions |
| 833 | * @hopcount: Number of hops into the network |
Alexandre Bounine | 68fe4df | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 834 | * @prev: Previous RIO device connected to the enumerated one |
| 835 | * @prev_port: Port on previous RIO device |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 836 | * |
| 837 | * Recursively enumerates a RIO network. Transactions are sent via the |
| 838 | * master port passed in @port. |
| 839 | */ |
Li Yang | 181a6ff | 2009-05-12 16:36:03 +0800 | [diff] [blame] | 840 | static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port, |
Alexandre Bounine | 68fe4df | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 841 | u8 hopcount, struct rio_dev *prev, int prev_port) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 842 | { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 843 | struct rio_dev *rdev; |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 844 | u32 regval; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 845 | int tmp; |
| 846 | |
Alexandre Bounine | e274e0e | 2010-10-27 15:34:32 -0700 | [diff] [blame] | 847 | if (rio_mport_chk_dev_access(port, |
| 848 | RIO_ANY_DESTID(port->sys_size), hopcount)) { |
| 849 | pr_debug("RIO: device access check failed\n"); |
| 850 | return -1; |
| 851 | } |
| 852 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 853 | if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) { |
| 854 | pr_debug("RIO: PE already discovered by this host\n"); |
| 855 | /* |
| 856 | * Already discovered by this host. Add it as another |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 857 | * link to the existing device. |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 858 | */ |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 859 | rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), |
| 860 | hopcount, RIO_COMPONENT_TAG_CSR, ®val); |
| 861 | |
| 862 | if (regval) { |
| 863 | rdev = rio_get_comptag((regval & 0xffff), NULL); |
| 864 | |
| 865 | if (rdev && prev && rio_is_switch(prev)) { |
| 866 | pr_debug("RIO: redundant path to %s\n", |
| 867 | rio_name(rdev)); |
| 868 | prev->rswitch->nextdev[prev_port] = rdev; |
| 869 | } |
| 870 | } |
| 871 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* Attempt to acquire device lock */ |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 876 | rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size), |
| 877 | hopcount, |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 878 | RIO_HOST_DID_LOCK_CSR, port->host_deviceid); |
| 879 | while ((tmp = rio_get_host_deviceid_lock(port, hopcount)) |
| 880 | < port->host_deviceid) { |
| 881 | /* Delay a bit */ |
| 882 | mdelay(1); |
| 883 | /* Attempt to acquire device lock again */ |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 884 | rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size), |
| 885 | hopcount, |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 886 | RIO_HOST_DID_LOCK_CSR, |
| 887 | port->host_deviceid); |
| 888 | } |
| 889 | |
| 890 | if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) { |
| 891 | pr_debug( |
| 892 | "RIO: PE locked by a higher priority host...retreating\n"); |
| 893 | return -1; |
| 894 | } |
| 895 | |
| 896 | /* Setup new RIO device */ |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 897 | rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size), |
| 898 | hopcount, 1); |
| 899 | if (rdev) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 900 | /* Add device to the global and bus/net specific list. */ |
| 901 | list_add_tail(&rdev->net_list, &net->devices); |
Alexandre Bounine | 68fe4df | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 902 | rdev->prev = prev; |
| 903 | if (prev && rio_is_switch(prev)) |
| 904 | prev->rswitch->nextdev[prev_port] = rdev; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 905 | } else |
| 906 | return -1; |
| 907 | |
| 908 | if (rio_is_switch(rdev)) { |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 909 | int sw_destid; |
| 910 | int cur_destid; |
| 911 | int sw_inport; |
| 912 | u16 destid; |
| 913 | int port_num; |
| 914 | |
Alexandre Bounine | ae05cbd | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 915 | sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo); |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 916 | rio_route_add_entry(rdev, RIO_GLOBAL_TABLE, |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 917 | port->host_deviceid, sw_inport, 0); |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 918 | rdev->rswitch->route_table[port->host_deviceid] = sw_inport; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 919 | |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 920 | destid = rio_destid_first(net); |
| 921 | while (destid != RIO_INVALID_DESTID && destid < next_destid) { |
| 922 | if (destid != port->host_deviceid) { |
| 923 | rio_route_add_entry(rdev, RIO_GLOBAL_TABLE, |
| 924 | destid, sw_inport, 0); |
| 925 | rdev->rswitch->route_table[destid] = sw_inport; |
| 926 | } |
| 927 | destid = rio_destid_next(net, destid + 1); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 928 | } |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 929 | pr_debug( |
| 930 | "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n", |
Alexandre Bounine | 68fe4df | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 931 | rio_name(rdev), rdev->vid, rdev->did, |
| 932 | RIO_GET_TOTAL_PORTS(rdev->swpinfo)); |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 933 | sw_destid = next_destid; |
Alexandre Bounine | 68fe4df | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 934 | for (port_num = 0; |
| 935 | port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo); |
| 936 | port_num++) { |
Alexandre Bounine | 8d4630d | 2012-10-04 17:16:02 -0700 | [diff] [blame] | 937 | if (sw_inport == port_num) { |
| 938 | rio_enable_rx_tx_port(port, 0, |
Thomas Moll | 933af4a | 2010-05-26 14:44:01 -0700 | [diff] [blame] | 939 | RIO_ANY_DESTID(port->sys_size), |
| 940 | hopcount, port_num); |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 941 | rdev->rswitch->port_ok |= (1 << port_num); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 942 | continue; |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 943 | } |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 944 | |
| 945 | cur_destid = next_destid; |
| 946 | |
| 947 | if (rio_sport_is_active |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 948 | (port, RIO_ANY_DESTID(port->sys_size), hopcount, |
| 949 | port_num)) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 950 | pr_debug( |
| 951 | "RIO: scanning device on port %d\n", |
| 952 | port_num); |
Alexandre Bounine | 8d4630d | 2012-10-04 17:16:02 -0700 | [diff] [blame] | 953 | rio_enable_rx_tx_port(port, 0, |
| 954 | RIO_ANY_DESTID(port->sys_size), |
| 955 | hopcount, port_num); |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 956 | rdev->rswitch->port_ok |= (1 << port_num); |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 957 | rio_route_add_entry(rdev, RIO_GLOBAL_TABLE, |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 958 | RIO_ANY_DESTID(port->sys_size), |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 959 | port_num, 0); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 960 | |
Alexandre Bounine | 68fe4df | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 961 | if (rio_enum_peer(net, port, hopcount + 1, |
| 962 | rdev, port_num) < 0) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 963 | return -1; |
| 964 | |
| 965 | /* Update routing tables */ |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 966 | destid = rio_destid_next(net, cur_destid + 1); |
| 967 | if (destid != RIO_INVALID_DESTID) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 968 | for (destid = cur_destid; |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 969 | destid < next_destid;) { |
| 970 | if (destid != port->host_deviceid) { |
| 971 | rio_route_add_entry(rdev, |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 972 | RIO_GLOBAL_TABLE, |
| 973 | destid, |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 974 | port_num, |
| 975 | 0); |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 976 | rdev->rswitch-> |
| 977 | route_table[destid] = |
| 978 | port_num; |
| 979 | } |
| 980 | destid = rio_destid_next(net, |
| 981 | destid + 1); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 982 | } |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 983 | } |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 984 | } else { |
| 985 | /* If switch supports Error Management, |
| 986 | * set PORT_LOCKOUT bit for unused port |
| 987 | */ |
| 988 | if (rdev->em_efptr) |
| 989 | rio_set_port_lockout(rdev, port_num, 1); |
| 990 | |
| 991 | rdev->rswitch->port_ok &= ~(1 << port_num); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 992 | } |
| 993 | } |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 994 | |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 995 | /* Direct Port-write messages to the enumeratiing host */ |
| 996 | if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) && |
| 997 | (rdev->em_efptr)) { |
| 998 | rio_write_config_32(rdev, |
| 999 | rdev->em_efptr + RIO_EM_PW_TGT_DEVID, |
| 1000 | (port->host_deviceid << 16) | |
| 1001 | (port->sys_size << 15)); |
| 1002 | } |
| 1003 | |
| 1004 | rio_init_em(rdev); |
| 1005 | |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1006 | /* Check for empty switch */ |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1007 | if (next_destid == sw_destid) |
| 1008 | next_destid = rio_destid_alloc(net); |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1009 | |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 1010 | rdev->destid = sw_destid; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1011 | } else |
| 1012 | pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n", |
| 1013 | rio_name(rdev), rdev->vid, rdev->did); |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * rio_enum_complete- Tests if enumeration of a network is complete |
| 1020 | * @port: Master port to send transaction |
| 1021 | * |
Liu Gang | a571259 | 2011-11-02 13:39:05 -0700 | [diff] [blame] | 1022 | * Tests the PGCCSR discovered bit for non-zero value (enumeration |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 1023 | * complete flag). Return %1 if enumeration is complete or %0 if |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1024 | * enumeration is incomplete. |
| 1025 | */ |
| 1026 | static int rio_enum_complete(struct rio_mport *port) |
| 1027 | { |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 1028 | u32 regval; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1029 | |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 1030 | rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR, |
| 1031 | ®val); |
Liu Gang | a571259 | 2011-11-02 13:39:05 -0700 | [diff] [blame] | 1032 | return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * rio_disc_peer- Recursively discovers a RIO network through a master port |
| 1037 | * @net: RIO network being discovered |
| 1038 | * @port: Master port to send transactions |
| 1039 | * @destid: Current destination ID in network |
| 1040 | * @hopcount: Number of hops into the network |
Randy Dunlap | 9b310ac | 2011-01-22 20:16:12 -0800 | [diff] [blame] | 1041 | * @prev: previous rio_dev |
| 1042 | * @prev_port: previous port number |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1043 | * |
| 1044 | * Recursively discovers a RIO network. Transactions are sent via the |
| 1045 | * master port passed in @port. |
| 1046 | */ |
Li Yang | 181a6ff | 2009-05-12 16:36:03 +0800 | [diff] [blame] | 1047 | static int __devinit |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1048 | rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid, |
Alexandre Bounine | 17e9620 | 2011-01-12 17:00:41 -0800 | [diff] [blame] | 1049 | u8 hopcount, struct rio_dev *prev, int prev_port) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1050 | { |
| 1051 | u8 port_num, route_port; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1052 | struct rio_dev *rdev; |
| 1053 | u16 ndestid; |
| 1054 | |
| 1055 | /* Setup new RIO device */ |
| 1056 | if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) { |
| 1057 | /* Add device to the global and bus/net specific list. */ |
| 1058 | list_add_tail(&rdev->net_list, &net->devices); |
Alexandre Bounine | 17e9620 | 2011-01-12 17:00:41 -0800 | [diff] [blame] | 1059 | rdev->prev = prev; |
| 1060 | if (prev && rio_is_switch(prev)) |
| 1061 | prev->rswitch->nextdev[prev_port] = rdev; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1062 | } else |
| 1063 | return -1; |
| 1064 | |
| 1065 | if (rio_is_switch(rdev)) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1066 | /* Associated destid is how we accessed this switch */ |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 1067 | rdev->destid = destid; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1068 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1069 | pr_debug( |
| 1070 | "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n", |
Alexandre Bounine | 68fe4df | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 1071 | rio_name(rdev), rdev->vid, rdev->did, |
| 1072 | RIO_GET_TOTAL_PORTS(rdev->swpinfo)); |
| 1073 | for (port_num = 0; |
| 1074 | port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo); |
| 1075 | port_num++) { |
Alexandre Bounine | ae05cbd | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 1076 | if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1077 | continue; |
| 1078 | |
| 1079 | if (rio_sport_is_active |
| 1080 | (port, destid, hopcount, port_num)) { |
| 1081 | pr_debug( |
| 1082 | "RIO: scanning device on port %d\n", |
| 1083 | port_num); |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 1084 | |
| 1085 | rio_lock_device(port, destid, hopcount, 1000); |
| 1086 | |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 1087 | for (ndestid = 0; |
| 1088 | ndestid < RIO_ANY_DESTID(port->sys_size); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1089 | ndestid++) { |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 1090 | rio_route_get_entry(rdev, |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1091 | RIO_GLOBAL_TABLE, |
| 1092 | ndestid, |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 1093 | &route_port, 0); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1094 | if (route_port == port_num) |
| 1095 | break; |
| 1096 | } |
| 1097 | |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 1098 | if (ndestid == RIO_ANY_DESTID(port->sys_size)) |
| 1099 | continue; |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 1100 | rio_unlock_device(port, destid, hopcount); |
Alexandre Bounine | 17e9620 | 2011-01-12 17:00:41 -0800 | [diff] [blame] | 1101 | if (rio_disc_peer(net, port, ndestid, |
| 1102 | hopcount + 1, rdev, port_num) < 0) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1103 | return -1; |
| 1104 | } |
| 1105 | } |
| 1106 | } else |
| 1107 | pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n", |
| 1108 | rio_name(rdev), rdev->vid, rdev->did); |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | /** |
| 1114 | * rio_mport_is_active- Tests if master port link is active |
| 1115 | * @port: Master port to test |
| 1116 | * |
| 1117 | * Reads the port error status CSR for the master port to |
| 1118 | * determine if the port has an active link. Returns |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 1119 | * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1120 | * or %0 if it is inactive. |
| 1121 | */ |
| 1122 | static int rio_mport_is_active(struct rio_mport *port) |
| 1123 | { |
| 1124 | u32 result = 0; |
| 1125 | u32 ext_ftr_ptr; |
| 1126 | int *entry = rio_mport_phys_table; |
| 1127 | |
| 1128 | do { |
| 1129 | if ((ext_ftr_ptr = |
| 1130 | rio_mport_get_feature(port, 1, 0, 0, *entry))) |
| 1131 | break; |
| 1132 | } while (*++entry >= 0); |
| 1133 | |
| 1134 | if (ext_ftr_ptr) |
| 1135 | rio_local_read_config_32(port, |
| 1136 | ext_ftr_ptr + |
| 1137 | RIO_PORT_N_ERR_STS_CSR(port->index), |
| 1138 | &result); |
| 1139 | |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 1140 | return result & RIO_PORT_N_ERR_STS_PORT_OK; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /** |
| 1144 | * rio_alloc_net- Allocate and configure a new RIO network |
| 1145 | * @port: Master port associated with the RIO network |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1146 | * @do_enum: Enumeration/Discovery mode flag |
| 1147 | * @start: logical minimal start id for new net |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1148 | * |
| 1149 | * Allocates a RIO network structure, initializes per-network |
| 1150 | * list heads, and adds the associated master port to the |
| 1151 | * network list of associated master ports. Returns a |
| 1152 | * RIO network pointer on success or %NULL on failure. |
| 1153 | */ |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1154 | static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port, |
| 1155 | int do_enum, u16 start) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1156 | { |
| 1157 | struct rio_net *net; |
| 1158 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 1159 | net = kzalloc(sizeof(struct rio_net), GFP_KERNEL); |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1160 | if (net && do_enum) { |
Alexandre Bounine | 4ed134b | 2012-10-10 15:54:01 -0700 | [diff] [blame^] | 1161 | net->destid_table.table = kcalloc( |
| 1162 | BITS_TO_LONGS(RIO_MAX_ROUTE_ENTRIES(port->sys_size)), |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1163 | sizeof(long), |
| 1164 | GFP_KERNEL); |
| 1165 | |
| 1166 | if (net->destid_table.table == NULL) { |
| 1167 | pr_err("RIO: failed to allocate destID table\n"); |
| 1168 | kfree(net); |
| 1169 | net = NULL; |
| 1170 | } else { |
| 1171 | net->destid_table.start = start; |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1172 | net->destid_table.max = |
| 1173 | RIO_MAX_ROUTE_ENTRIES(port->sys_size); |
| 1174 | spin_lock_init(&net->destid_table.lock); |
| 1175 | } |
| 1176 | } |
| 1177 | |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1178 | if (net) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1179 | INIT_LIST_HEAD(&net->node); |
| 1180 | INIT_LIST_HEAD(&net->devices); |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1181 | INIT_LIST_HEAD(&net->switches); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1182 | INIT_LIST_HEAD(&net->mports); |
| 1183 | list_add_tail(&port->nnode, &net->mports); |
| 1184 | net->hport = port; |
Alexandre Bounine | 005842e | 2012-10-04 17:16:08 -0700 | [diff] [blame] | 1185 | net->id = port->id; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1186 | } |
| 1187 | return net; |
| 1188 | } |
| 1189 | |
| 1190 | /** |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1191 | * rio_update_route_tables- Updates route tables in switches |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1192 | * @net: RIO network to run update on |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1193 | * |
| 1194 | * For each enumerated device, ensure that each switch in a system |
| 1195 | * has correct routing entries. Add routes for devices that where |
| 1196 | * unknown dirung the first enumeration pass through the switch. |
| 1197 | */ |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1198 | static void rio_update_route_tables(struct rio_net *net) |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1199 | { |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 1200 | struct rio_dev *rdev, *swrdev; |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1201 | struct rio_switch *rswitch; |
| 1202 | u8 sport; |
| 1203 | u16 destid; |
| 1204 | |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1205 | list_for_each_entry(rdev, &net->devices, net_list) { |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1206 | |
Alexandre Bounine | a93192a | 2011-01-12 17:00:38 -0800 | [diff] [blame] | 1207 | destid = rdev->destid; |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1208 | |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1209 | list_for_each_entry(rswitch, &net->switches, node) { |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1210 | |
| 1211 | if (rio_is_switch(rdev) && (rdev->rswitch == rswitch)) |
| 1212 | continue; |
| 1213 | |
| 1214 | if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) { |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 1215 | swrdev = sw_to_rio_dev(rswitch); |
| 1216 | |
Alexandre Bounine | 07590ff | 2010-05-26 14:43:57 -0700 | [diff] [blame] | 1217 | /* Skip if destid ends in empty switch*/ |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 1218 | if (swrdev->destid == destid) |
Alexandre Bounine | 07590ff | 2010-05-26 14:43:57 -0700 | [diff] [blame] | 1219 | continue; |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1220 | |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 1221 | sport = RIO_GET_PORT_NUM(swrdev->swpinfo); |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1222 | |
| 1223 | if (rswitch->add_entry) { |
Alexandre Bounine | ded0578 | 2011-01-12 17:00:39 -0800 | [diff] [blame] | 1224 | rio_route_add_entry(swrdev, |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 1225 | RIO_GLOBAL_TABLE, destid, |
| 1226 | sport, 0); |
Alexandre Bounine | c70555b | 2007-02-10 01:46:47 -0800 | [diff] [blame] | 1227 | rswitch->route_table[destid] = sport; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /** |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 1235 | * rio_init_em - Initializes RIO Error Management (for switches) |
Randy Dunlap | 97ef6f7 | 2010-05-28 15:08:08 -0700 | [diff] [blame] | 1236 | * @rdev: RIO device |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 1237 | * |
| 1238 | * For each enumerated switch, call device-specific error management |
| 1239 | * initialization routine (if supplied by the switch driver). |
| 1240 | */ |
| 1241 | static void rio_init_em(struct rio_dev *rdev) |
| 1242 | { |
| 1243 | if (rio_is_switch(rdev) && (rdev->em_efptr) && |
| 1244 | (rdev->rswitch->em_init)) { |
| 1245 | rdev->rswitch->em_init(rdev); |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * rio_pw_enable - Enables/disables port-write handling by a master port |
| 1251 | * @port: Master port associated with port-write handling |
| 1252 | * @enable: 1=enable, 0=disable |
| 1253 | */ |
| 1254 | static void rio_pw_enable(struct rio_mport *port, int enable) |
| 1255 | { |
| 1256 | if (port->ops->pwenable) |
| 1257 | port->ops->pwenable(port, enable); |
| 1258 | } |
| 1259 | |
| 1260 | /** |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1261 | * rio_enum_mport- Start enumeration through a master port |
| 1262 | * @mport: Master port to send transactions |
| 1263 | * |
| 1264 | * Starts the enumeration process. If somebody has enumerated our |
| 1265 | * master port device, then give up. If not and we have an active |
| 1266 | * link, then start recursive peer enumeration. Returns %0 if |
| 1267 | * enumeration succeeds or %-EBUSY if enumeration fails. |
| 1268 | */ |
Al Viro | 37d33d1 | 2008-11-22 17:36:24 +0000 | [diff] [blame] | 1269 | int __devinit rio_enum_mport(struct rio_mport *mport) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1270 | { |
| 1271 | struct rio_net *net = NULL; |
| 1272 | int rc = 0; |
| 1273 | |
| 1274 | printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id, |
| 1275 | mport->name); |
| 1276 | /* If somebody else enumerated our master port device, bail. */ |
| 1277 | if (rio_enum_host(mport) < 0) { |
| 1278 | printk(KERN_INFO |
| 1279 | "RIO: master port %d device has been enumerated by a remote host\n", |
| 1280 | mport->id); |
| 1281 | rc = -EBUSY; |
| 1282 | goto out; |
| 1283 | } |
| 1284 | |
| 1285 | /* If master port has an active link, allocate net and enum peers */ |
| 1286 | if (rio_mport_is_active(mport)) { |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1287 | net = rio_alloc_net(mport, 1, 0); |
| 1288 | if (!net) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1289 | printk(KERN_ERR "RIO: failed to allocate new net\n"); |
| 1290 | rc = -ENOMEM; |
| 1291 | goto out; |
| 1292 | } |
Thomas Moll | 933af4a | 2010-05-26 14:44:01 -0700 | [diff] [blame] | 1293 | |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1294 | /* reserve mport destID in new net */ |
| 1295 | rio_destid_reserve(net, mport->host_deviceid); |
| 1296 | |
Thomas Moll | 933af4a | 2010-05-26 14:44:01 -0700 | [diff] [blame] | 1297 | /* Enable Input Output Port (transmitter reviever) */ |
| 1298 | rio_enable_rx_tx_port(mport, 1, 0, 0, 0); |
| 1299 | |
Alexandre Bounine | af84ca3 | 2010-10-27 15:34:34 -0700 | [diff] [blame] | 1300 | /* Set component tag for host */ |
| 1301 | rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR, |
| 1302 | next_comptag++); |
| 1303 | |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1304 | next_destid = rio_destid_alloc(net); |
| 1305 | |
Alexandre Bounine | 68fe4df | 2010-10-27 15:34:29 -0700 | [diff] [blame] | 1306 | if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1307 | /* A higher priority host won enumeration, bail. */ |
| 1308 | printk(KERN_INFO |
| 1309 | "RIO: master port %d device has lost enumeration to a remote host\n", |
| 1310 | mport->id); |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1311 | rio_clear_locks(net); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1312 | rc = -EBUSY; |
| 1313 | goto out; |
| 1314 | } |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1315 | /* free the last allocated destID (unused) */ |
| 1316 | rio_destid_free(net, next_destid); |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1317 | rio_update_route_tables(net); |
| 1318 | rio_clear_locks(net); |
Alexandre Bounine | e5cabeb | 2010-05-26 14:43:59 -0700 | [diff] [blame] | 1319 | rio_pw_enable(mport, 1); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1320 | } else { |
| 1321 | printk(KERN_INFO "RIO: master port %d link inactive\n", |
| 1322 | mport->id); |
| 1323 | rc = -EINVAL; |
| 1324 | } |
| 1325 | |
| 1326 | out: |
| 1327 | return rc; |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * rio_build_route_tables- Generate route tables from switch route entries |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1332 | * @net: RIO network to run route tables scan on |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1333 | * |
| 1334 | * For each switch device, generate a route table by copying existing |
| 1335 | * route entries from the switch. |
| 1336 | */ |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1337 | static void rio_build_route_tables(struct rio_net *net) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1338 | { |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1339 | struct rio_switch *rswitch; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1340 | struct rio_dev *rdev; |
| 1341 | int i; |
| 1342 | u8 sport; |
| 1343 | |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1344 | list_for_each_entry(rswitch, &net->switches, node) { |
| 1345 | rdev = sw_to_rio_dev(rswitch); |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 1346 | |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1347 | rio_lock_device(net->hport, rdev->destid, |
| 1348 | rdev->hopcount, 1000); |
| 1349 | for (i = 0; |
| 1350 | i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size); |
| 1351 | i++) { |
| 1352 | if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE, |
| 1353 | i, &sport, 0) < 0) |
| 1354 | continue; |
| 1355 | rswitch->route_table[i] = sport; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1356 | } |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1357 | |
| 1358 | rio_unlock_device(net->hport, rdev->destid, rdev->hopcount); |
| 1359 | } |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | /** |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1363 | * rio_disc_mport- Start discovery through a master port |
| 1364 | * @mport: Master port to send transactions |
| 1365 | * |
| 1366 | * Starts the discovery process. If we have an active link, |
| 1367 | * then wait for the signal that enumeration is complete. |
| 1368 | * When enumeration completion is signaled, start recursive |
| 1369 | * peer discovery. Returns %0 if discovery succeeds or %-EBUSY |
| 1370 | * on failure. |
| 1371 | */ |
Al Viro | 37d33d1 | 2008-11-22 17:36:24 +0000 | [diff] [blame] | 1372 | int __devinit rio_disc_mport(struct rio_mport *mport) |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1373 | { |
| 1374 | struct rio_net *net = NULL; |
Alexandre Bounine | fa3dbaa | 2012-10-04 17:16:04 -0700 | [diff] [blame] | 1375 | unsigned long to_end; |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1376 | |
| 1377 | printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id, |
| 1378 | mport->name); |
| 1379 | |
| 1380 | /* If master port has an active link, allocate net and discover peers */ |
| 1381 | if (rio_mport_is_active(mport)) { |
Alexandre Bounine | fa3dbaa | 2012-10-04 17:16:04 -0700 | [diff] [blame] | 1382 | pr_debug("RIO: wait for enumeration to complete...\n"); |
| 1383 | |
| 1384 | to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ; |
| 1385 | while (time_before(jiffies, to_end)) { |
| 1386 | if (rio_enum_complete(mport)) |
| 1387 | goto enum_done; |
Alexandre Bounine | f4c9c0e | 2012-10-10 15:53:58 -0700 | [diff] [blame] | 1388 | msleep(10); |
Alexandre Bounine | fa3dbaa | 2012-10-04 17:16:04 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | pr_debug("RIO: discovery timeout on mport %d %s\n", |
| 1392 | mport->id, mport->name); |
| 1393 | goto bail; |
| 1394 | enum_done: |
| 1395 | pr_debug("RIO: ... enumeration done\n"); |
| 1396 | |
Alexandre Bounine | de74e00 | 2012-10-04 17:16:13 -0700 | [diff] [blame] | 1397 | net = rio_alloc_net(mport, 0, 0); |
Alexandre Bounine | fa3dbaa | 2012-10-04 17:16:04 -0700 | [diff] [blame] | 1398 | if (!net) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1399 | printk(KERN_ERR "RIO: Failed to allocate new net\n"); |
| 1400 | goto bail; |
| 1401 | } |
| 1402 | |
Alexandre Bounine | 818a04a | 2010-05-26 14:43:58 -0700 | [diff] [blame] | 1403 | /* Read DestID assigned by enumerator */ |
| 1404 | rio_local_read_config_32(mport, RIO_DID_CSR, |
| 1405 | &mport->host_deviceid); |
| 1406 | mport->host_deviceid = RIO_GET_DID(mport->sys_size, |
| 1407 | mport->host_deviceid); |
| 1408 | |
Zhang Wei | e042323 | 2008-04-18 13:33:42 -0700 | [diff] [blame] | 1409 | if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size), |
Alexandre Bounine | 17e9620 | 2011-01-12 17:00:41 -0800 | [diff] [blame] | 1410 | 0, NULL, 0) < 0) { |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1411 | printk(KERN_INFO |
| 1412 | "RIO: master port %d device has failed discovery\n", |
| 1413 | mport->id); |
| 1414 | goto bail; |
| 1415 | } |
| 1416 | |
Alexandre Bounine | a7071ef | 2012-10-04 17:16:05 -0700 | [diff] [blame] | 1417 | rio_build_route_tables(net); |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | return 0; |
Alexandre Bounine | fa3dbaa | 2012-10-04 17:16:04 -0700 | [diff] [blame] | 1421 | bail: |
Matt Porter | eb188d0 | 2005-11-07 01:00:17 -0800 | [diff] [blame] | 1422 | return -EBUSY; |
| 1423 | } |