blob: 8070e074c739f89b0902cfcc1ed9c20e5374c76c [file] [log] [blame]
Matt Portereb188d02005-11-07 01:00:17 -08001/*
2 * RapidIO enumeration and discovery support
3 *
4 * Copyright 2005 MontaVista Software, Inc.
5 * Matt Porter <mporter@kernel.crashing.org>
6 *
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07007 * Copyright 2009 Integrated Device Technology, Inc.
8 * Alex Bounine <alexandre.bounine@idt.com>
9 * - Added Port-Write/Error Management initialization and handling
10 *
Thomas Moll933af4a2010-05-26 14:44:01 -070011 * Copyright 2009 Sysgo AG
12 * Thomas Moll <thomas.moll@sysgo.com>
13 * - Added Input- Output- enable functionality, to allow full communication
14 *
Matt Portereb188d02005-11-07 01:00:17 -080015 * 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 Portereb188d02005-11-07 01:00:17 -080021#include <linux/types.h>
22#include <linux/kernel.h>
23
24#include <linux/delay.h>
Matt Porterfa78cc52005-11-07 01:00:18 -080025#include <linux/dma-mapping.h>
Matt Portereb188d02005-11-07 01:00:17 -080026#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>
Tim Schmielaude259682006-01-08 01:02:05 -080034#include <linux/jiffies.h>
35#include <linux/slab.h>
Matt Portereb188d02005-11-07 01:00:17 -080036
37#include "rio.h"
38
39LIST_HEAD(rio_devices);
40static LIST_HEAD(rio_switches);
41
Matt Portereb188d02005-11-07 01:00:17 -080042static void rio_enum_timeout(unsigned long);
43
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -070044static void rio_init_em(struct rio_dev *rdev);
45
Matt Porterfa78cc52005-11-07 01:00:18 -080046DEFINE_SPINLOCK(rio_global_list_lock);
47
Matt Portereb188d02005-11-07 01:00:17 -080048static int next_destid = 0;
49static int next_switchid = 0;
50static int next_net = 0;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -070051static int next_comptag;
Matt Portereb188d02005-11-07 01:00:17 -080052
53static struct timer_list rio_enum_timer =
54TIMER_INITIALIZER(rio_enum_timeout, 0, 0);
55
56static int rio_mport_phys_table[] = {
57 RIO_EFB_PAR_EP_ID,
58 RIO_EFB_PAR_EP_REC_ID,
59 RIO_EFB_SER_EP_ID,
60 RIO_EFB_SER_EP_REC_ID,
61 -1,
62};
63
Matt Portereb188d02005-11-07 01:00:17 -080064/**
65 * rio_get_device_id - Get the base/extended device id for a device
66 * @port: RIO master port
67 * @destid: Destination ID of device
68 * @hopcount: Hopcount to device
69 *
70 * Reads the base/extended device id from a device. Returns the
71 * 8/16-bit device ID.
72 */
73static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
74{
75 u32 result;
76
77 rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
78
Zhang Weie0423232008-04-18 13:33:42 -070079 return RIO_GET_DID(port->sys_size, result);
Matt Portereb188d02005-11-07 01:00:17 -080080}
81
82/**
83 * rio_set_device_id - Set the base/extended device id for a device
84 * @port: RIO master port
85 * @destid: Destination ID of device
86 * @hopcount: Hopcount to device
87 * @did: Device ID value to be written
88 *
89 * Writes the base/extended device id from a device.
90 */
Matt Porterfa78cc52005-11-07 01:00:18 -080091static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
Matt Portereb188d02005-11-07 01:00:17 -080092{
93 rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
Zhang Weie0423232008-04-18 13:33:42 -070094 RIO_SET_DID(port->sys_size, did));
Matt Portereb188d02005-11-07 01:00:17 -080095}
96
97/**
98 * rio_local_set_device_id - Set the base/extended device id for a port
99 * @port: RIO master port
100 * @did: Device ID value to be written
101 *
102 * Writes the base/extended device id from a device.
103 */
104static void rio_local_set_device_id(struct rio_mport *port, u16 did)
105{
Zhang Weie0423232008-04-18 13:33:42 -0700106 rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
107 did));
Matt Portereb188d02005-11-07 01:00:17 -0800108}
109
110/**
111 * rio_clear_locks- Release all host locks and signal enumeration complete
112 * @port: Master port to issue transaction
113 *
114 * Marks the component tag CSR on each device with the enumeration
115 * complete flag. When complete, it then release the host locks on
116 * each device. Returns 0 on success or %-EINVAL on failure.
117 */
118static int rio_clear_locks(struct rio_mport *port)
119{
120 struct rio_dev *rdev;
121 u32 result;
122 int ret = 0;
123
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700124 /* Assign component tag to all devices */
125 next_comptag = 1;
126 rio_local_write_config_32(port, RIO_COMPONENT_TAG_CSR, next_comptag++);
127
128 list_for_each_entry(rdev, &rio_devices, global_list) {
129 /* Mark device as discovered */
130 rio_read_config_32(rdev,
131 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
132 &result);
133 rio_write_config_32(rdev,
134 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
135 result | RIO_PORT_GEN_DISCOVERED);
136
137 rio_write_config_32(rdev, RIO_COMPONENT_TAG_CSR, next_comptag);
138 rdev->comp_tag = next_comptag++;
139 if (next_comptag >= 0x10000) {
140 pr_err("RIO: Component Tag Counter Overflow\n");
141 break;
142 }
143 }
Matt Portereb188d02005-11-07 01:00:17 -0800144
145 /* Release host device id locks */
146 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
147 port->host_deviceid);
148 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
149 if ((result & 0xffff) != 0xffff) {
150 printk(KERN_INFO
151 "RIO: badness when releasing host lock on master port, result %8.8x\n",
152 result);
153 ret = -EINVAL;
154 }
155 list_for_each_entry(rdev, &rio_devices, global_list) {
156 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
157 port->host_deviceid);
158 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
159 if ((result & 0xffff) != 0xffff) {
160 printk(KERN_INFO
161 "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
162 rdev->vid, rdev->did);
163 ret = -EINVAL;
164 }
165 }
166
167 return ret;
168}
169
170/**
171 * rio_enum_host- Set host lock and initialize host destination ID
172 * @port: Master port to issue transaction
173 *
174 * Sets the local host master port lock and destination ID register
175 * with the host device ID value. The host device ID value is provided
176 * by the platform. Returns %0 on success or %-1 on failure.
177 */
178static int rio_enum_host(struct rio_mport *port)
179{
180 u32 result;
181
182 /* Set master port host device id lock */
183 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
184 port->host_deviceid);
185
186 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
187 if ((result & 0xffff) != port->host_deviceid)
188 return -1;
189
190 /* Set master port destid and init destid ctr */
191 rio_local_set_device_id(port, port->host_deviceid);
192
193 if (next_destid == port->host_deviceid)
194 next_destid++;
195
196 return 0;
197}
198
199/**
200 * rio_device_has_destid- Test if a device contains a destination ID register
201 * @port: Master port to issue transaction
202 * @src_ops: RIO device source operations
203 * @dst_ops: RIO device destination operations
204 *
205 * Checks the provided @src_ops and @dst_ops for the necessary transaction
206 * capabilities that indicate whether or not a device will implement a
207 * destination ID register. Returns 1 if true or 0 if false.
208 */
209static int rio_device_has_destid(struct rio_mport *port, int src_ops,
210 int dst_ops)
211{
Matt Porterfa78cc52005-11-07 01:00:18 -0800212 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;
213
214 return !!((src_ops | dst_ops) & mask);
Matt Portereb188d02005-11-07 01:00:17 -0800215}
216
217/**
218 * rio_release_dev- Frees a RIO device struct
219 * @dev: LDM device associated with a RIO device struct
220 *
221 * Gets the RIO device struct associated a RIO device struct.
222 * The RIO device struct is freed.
223 */
224static void rio_release_dev(struct device *dev)
225{
226 struct rio_dev *rdev;
227
228 rdev = to_rio_dev(dev);
229 kfree(rdev);
230}
231
232/**
233 * rio_is_switch- Tests if a RIO device has switch capabilities
234 * @rdev: RIO device
235 *
236 * Gets the RIO device Processing Element Features register
237 * contents and tests for switch capabilities. Returns 1 if
238 * the device is a switch or 0 if it is not a switch.
239 * The RIO device struct is freed.
240 */
241static int rio_is_switch(struct rio_dev *rdev)
242{
243 if (rdev->pef & RIO_PEF_SWITCH)
244 return 1;
245 return 0;
246}
247
248/**
Alexandre Bounine058f88d2010-05-26 14:44:03 -0700249 * rio_switch_init - Sets switch operations for a particular vendor switch
Matt Portereb188d02005-11-07 01:00:17 -0800250 * @rdev: RIO device
Alexandre Bounine058f88d2010-05-26 14:44:03 -0700251 * @do_enum: Enumeration/Discovery mode flag
Matt Portereb188d02005-11-07 01:00:17 -0800252 *
Alexandre Bounine058f88d2010-05-26 14:44:03 -0700253 * Searches the RIO switch ops table for known switch types. If the vid
254 * and did match a switch table entry, then call switch initialization
255 * routine to setup switch-specific routines.
Matt Portereb188d02005-11-07 01:00:17 -0800256 */
Alexandre Bounine058f88d2010-05-26 14:44:03 -0700257static void rio_switch_init(struct rio_dev *rdev, int do_enum)
Matt Portereb188d02005-11-07 01:00:17 -0800258{
Alexandre Bounine058f88d2010-05-26 14:44:03 -0700259 struct rio_switch_ops *cur = __start_rio_switch_ops;
260 struct rio_switch_ops *end = __end_rio_switch_ops;
Matt Portereb188d02005-11-07 01:00:17 -0800261
262 while (cur < end) {
263 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
Alexandre Bounine058f88d2010-05-26 14:44:03 -0700264 pr_debug("RIO: calling init routine for %s\n",
265 rio_name(rdev));
266 cur->init_hook(rdev, do_enum);
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700267 break;
Matt Portereb188d02005-11-07 01:00:17 -0800268 }
269 cur++;
270 }
271
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700272 if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
273 pr_debug("RIO: adding STD routing ops for %s\n",
274 rio_name(rdev));
275 rdev->rswitch->add_entry = rio_std_route_add_entry;
276 rdev->rswitch->get_entry = rio_std_route_get_entry;
277 rdev->rswitch->clr_table = rio_std_route_clr_table;
278 }
279
Matt Portereb188d02005-11-07 01:00:17 -0800280 if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
281 printk(KERN_ERR "RIO: missing routing ops for %s\n",
282 rio_name(rdev));
283}
284
285/**
286 * rio_add_device- Adds a RIO device to the device model
287 * @rdev: RIO device
288 *
289 * Adds the RIO device to the global device list and adds the RIO
290 * device to the RIO device list. Creates the generic sysfs nodes
291 * for an RIO device.
292 */
Yang Li5f28c522009-05-11 22:36:02 +0000293static int __devinit rio_add_device(struct rio_dev *rdev)
Matt Portereb188d02005-11-07 01:00:17 -0800294{
Yang Li5f28c522009-05-11 22:36:02 +0000295 int err;
296
297 err = device_add(&rdev->dev);
298 if (err)
299 return err;
Matt Portereb188d02005-11-07 01:00:17 -0800300
301 spin_lock(&rio_global_list_lock);
302 list_add_tail(&rdev->global_list, &rio_devices);
303 spin_unlock(&rio_global_list_lock);
304
305 rio_create_sysfs_dev_files(rdev);
Yang Li5f28c522009-05-11 22:36:02 +0000306
307 return 0;
Matt Portereb188d02005-11-07 01:00:17 -0800308}
309
310/**
Thomas Moll933af4a2010-05-26 14:44:01 -0700311 * rio_enable_rx_tx_port - enable input reciever and output transmitter of
312 * given port
313 * @port: Master port associated with the RIO network
314 * @local: local=1 select local port otherwise a far device is reached
315 * @destid: Destination ID of the device to check host bit
316 * @hopcount: Number of hops to reach the target
317 * @port_num: Port (-number on switch) to enable on a far end device
318 *
319 * Returns 0 or 1 from on General Control Command and Status Register
320 * (EXT_PTR+0x3C)
321 */
322inline int rio_enable_rx_tx_port(struct rio_mport *port,
323 int local, u16 destid,
324 u8 hopcount, u8 port_num) {
325#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
326 u32 regval;
327 u32 ext_ftr_ptr;
328
329 /*
330 * enable rx input tx output port
331 */
332 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
333 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
334
335 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
336
337 if (local) {
338 rio_local_read_config_32(port, ext_ftr_ptr +
339 RIO_PORT_N_CTL_CSR(0),
340 &regval);
341 } else {
342 if (rio_mport_read_config_32(port, destid, hopcount,
343 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
344 return -EIO;
345 }
346
347 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
348 /* serial */
349 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
350 | RIO_PORT_N_CTL_EN_TX_SER;
351 } else {
352 /* parallel */
353 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
354 | RIO_PORT_N_CTL_EN_TX_PAR;
355 }
356
357 if (local) {
358 rio_local_write_config_32(port, ext_ftr_ptr +
359 RIO_PORT_N_CTL_CSR(0), regval);
360 } else {
361 if (rio_mport_write_config_32(port, destid, hopcount,
362 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
363 return -EIO;
364 }
365#endif
366 return 0;
367}
368
369/**
Matt Portereb188d02005-11-07 01:00:17 -0800370 * rio_setup_device- Allocates and sets up a RIO device
371 * @net: RIO network
372 * @port: Master port to send transactions
373 * @destid: Current destination ID
374 * @hopcount: Current hopcount
375 * @do_enum: Enumeration/Discovery mode flag
376 *
377 * Allocates a RIO device and configures fields based on configuration
378 * space contents. If device has a destination ID register, a destination
379 * ID is either assigned in enumeration mode or read from configuration
380 * space in discovery mode. If the device has switch capabilities, then
381 * a switch is allocated and configured appropriately. Returns a pointer
382 * to a RIO device on success or NULL on failure.
383 *
384 */
Li Yang181a6ff2009-05-12 16:36:03 +0800385static struct rio_dev __devinit *rio_setup_device(struct rio_net *net,
Matt Portereb188d02005-11-07 01:00:17 -0800386 struct rio_mport *port, u16 destid,
387 u8 hopcount, int do_enum)
388{
Yang Li5f28c522009-05-11 22:36:02 +0000389 int ret = 0;
Matt Portereb188d02005-11-07 01:00:17 -0800390 struct rio_dev *rdev;
Yang Li5f28c522009-05-11 22:36:02 +0000391 struct rio_switch *rswitch = NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800392 int result, rdid;
393
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700394 rdev = kzalloc(sizeof(struct rio_dev), GFP_KERNEL);
Matt Portereb188d02005-11-07 01:00:17 -0800395 if (!rdev)
Yang Li5f28c522009-05-11 22:36:02 +0000396 return NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800397
Matt Portereb188d02005-11-07 01:00:17 -0800398 rdev->net = net;
399 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
400 &result);
401 rdev->did = result >> 16;
402 rdev->vid = result & 0xffff;
403 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
404 &rdev->device_rev);
405 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
406 &result);
407 rdev->asm_did = result >> 16;
408 rdev->asm_vid = result & 0xffff;
409 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
410 &result);
411 rdev->asm_rev = result >> 16;
412 rio_mport_read_config_32(port, destid, hopcount, RIO_PEF_CAR,
413 &rdev->pef);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700414 if (rdev->pef & RIO_PEF_EXT_FEATURES) {
Matt Portereb188d02005-11-07 01:00:17 -0800415 rdev->efptr = result & 0xffff;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700416 rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
417 hopcount);
418
419 rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
420 hopcount, RIO_EFB_ERR_MGMNT);
421 }
Matt Portereb188d02005-11-07 01:00:17 -0800422
423 rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
424 &rdev->src_ops);
425 rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
426 &rdev->dst_ops);
427
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800428 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
429 if (do_enum) {
430 rio_set_device_id(port, destid, hopcount, next_destid);
431 rdev->destid = next_destid++;
432 if (next_destid == port->host_deviceid)
433 next_destid++;
434 } else
435 rdev->destid = rio_get_device_id(port, destid, hopcount);
Matt Portereb188d02005-11-07 01:00:17 -0800436 } else
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800437 /* Switch device has an associated destID */
438 rdev->destid = RIO_INVALID_DESTID;
Matt Portereb188d02005-11-07 01:00:17 -0800439
440 /* If a PE has both switch and other functions, show it as a switch */
441 if (rio_is_switch(rdev)) {
442 rio_mport_read_config_32(port, destid, hopcount,
443 RIO_SWP_INFO_CAR, &rdev->swpinfo);
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700444 rswitch = kzalloc(sizeof(struct rio_switch), GFP_KERNEL);
Yang Li5f28c522009-05-11 22:36:02 +0000445 if (!rswitch)
446 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800447 rswitch->switchid = next_switchid;
448 rswitch->hopcount = hopcount;
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800449 rswitch->destid = destid;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700450 rswitch->port_ok = 0;
Zhang Weie0423232008-04-18 13:33:42 -0700451 rswitch->route_table = kzalloc(sizeof(u8)*
452 RIO_MAX_ROUTE_ENTRIES(port->sys_size),
453 GFP_KERNEL);
Yang Li5f28c522009-05-11 22:36:02 +0000454 if (!rswitch->route_table)
455 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800456 /* Initialize switch route table */
Zhang Weie0423232008-04-18 13:33:42 -0700457 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
458 rdid++)
Matt Portereb188d02005-11-07 01:00:17 -0800459 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
460 rdev->rswitch = rswitch;
Kay Sieversb53c7582008-12-04 10:01:52 -0800461 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
462 rdev->rswitch->switchid);
Alexandre Bounine058f88d2010-05-26 14:44:03 -0700463 rio_switch_init(rdev, do_enum);
Matt Portereb188d02005-11-07 01:00:17 -0800464
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700465 if (do_enum && rdev->rswitch->clr_table)
466 rdev->rswitch->clr_table(port, destid, hopcount,
467 RIO_GLOBAL_TABLE);
468
Matt Portereb188d02005-11-07 01:00:17 -0800469 list_add_tail(&rswitch->node, &rio_switches);
470
Thomas Moll933af4a2010-05-26 14:44:01 -0700471 } else {
472 if (do_enum)
473 /*Enable Input Output Port (transmitter reviever)*/
474 rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
475
Kay Sieversb53c7582008-12-04 10:01:52 -0800476 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
477 rdev->destid);
Thomas Moll933af4a2010-05-26 14:44:01 -0700478 }
Matt Portereb188d02005-11-07 01:00:17 -0800479
480 rdev->dev.bus = &rio_bus_type;
481
482 device_initialize(&rdev->dev);
483 rdev->dev.release = rio_release_dev;
484 rio_dev_get(rdev);
485
Yang Hongyang284901a2009-04-06 19:01:15 -0700486 rdev->dma_mask = DMA_BIT_MASK(32);
Matt Porterfa78cc52005-11-07 01:00:18 -0800487 rdev->dev.dma_mask = &rdev->dma_mask;
Yang Hongyang284901a2009-04-06 19:01:15 -0700488 rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Matt Portereb188d02005-11-07 01:00:17 -0800489
490 if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
491 (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
492 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
493 0, 0xffff);
494
Yang Li5f28c522009-05-11 22:36:02 +0000495 ret = rio_add_device(rdev);
496 if (ret)
497 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800498
Matt Portereb188d02005-11-07 01:00:17 -0800499 return rdev;
Yang Li5f28c522009-05-11 22:36:02 +0000500
501cleanup:
502 if (rswitch) {
503 kfree(rswitch->route_table);
504 kfree(rswitch);
505 }
506 kfree(rdev);
507 return NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800508}
509
510/**
511 * rio_sport_is_active- Tests if a switch port has an active connection.
512 * @port: Master port to send transaction
513 * @destid: Associated destination ID for switch
514 * @hopcount: Hopcount to reach switch
515 * @sport: Switch port number
516 *
517 * Reads the port error status CSR for a particular switch port to
518 * determine if the port has an active link. Returns
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700519 * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
Matt Portereb188d02005-11-07 01:00:17 -0800520 * inactive.
521 */
522static int
523rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
524{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700525 u32 result = 0;
Matt Portereb188d02005-11-07 01:00:17 -0800526 u32 ext_ftr_ptr;
527
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700528 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800529
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700530 while (ext_ftr_ptr) {
531 rio_mport_read_config_32(port, destid, hopcount,
532 ext_ftr_ptr, &result);
533 result = RIO_GET_BLOCK_ID(result);
534 if ((result == RIO_EFB_SER_EP_FREE_ID) ||
535 (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
536 (result == RIO_EFB_SER_EP_FREC_ID))
Matt Portereb188d02005-11-07 01:00:17 -0800537 break;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700538
539 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
540 ext_ftr_ptr);
541 }
Matt Portereb188d02005-11-07 01:00:17 -0800542
543 if (ext_ftr_ptr)
544 rio_mport_read_config_32(port, destid, hopcount,
545 ext_ftr_ptr +
546 RIO_PORT_N_ERR_STS_CSR(sport),
547 &result);
548
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700549 return result & RIO_PORT_N_ERR_STS_PORT_OK;
Matt Portereb188d02005-11-07 01:00:17 -0800550}
551
552/**
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700553 * rio_lock_device - Acquires host device lock for specified device
554 * @port: Master port to send transaction
555 * @destid: Destination ID for device/switch
556 * @hopcount: Hopcount to reach switch
557 * @wait_ms: Max wait time in msec (0 = no timeout)
558 *
559 * Attepts to acquire host device lock for specified device
560 * Returns 0 if device lock acquired or EINVAL if timeout expires.
561 */
562static int
563rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
564{
565 u32 result;
566 int tcnt = 0;
567
568 /* Attempt to acquire device lock */
569 rio_mport_write_config_32(port, destid, hopcount,
570 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
571 rio_mport_read_config_32(port, destid, hopcount,
572 RIO_HOST_DID_LOCK_CSR, &result);
573
574 while (result != port->host_deviceid) {
575 if (wait_ms != 0 && tcnt == wait_ms) {
576 pr_debug("RIO: timeout when locking device %x:%x\n",
577 destid, hopcount);
578 return -EINVAL;
579 }
580
581 /* Delay a bit */
582 mdelay(1);
583 tcnt++;
584 /* Try to acquire device lock again */
585 rio_mport_write_config_32(port, destid,
586 hopcount,
587 RIO_HOST_DID_LOCK_CSR,
588 port->host_deviceid);
589 rio_mport_read_config_32(port, destid,
590 hopcount,
591 RIO_HOST_DID_LOCK_CSR, &result);
592 }
593
594 return 0;
595}
596
597/**
598 * rio_unlock_device - Releases host device lock for specified device
599 * @port: Master port to send transaction
600 * @destid: Destination ID for device/switch
601 * @hopcount: Hopcount to reach switch
602 *
603 * Returns 0 if device lock released or EINVAL if fails.
604 */
605static int
606rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
607{
608 u32 result;
609
610 /* Release device lock */
611 rio_mport_write_config_32(port, destid,
612 hopcount,
613 RIO_HOST_DID_LOCK_CSR,
614 port->host_deviceid);
615 rio_mport_read_config_32(port, destid, hopcount,
616 RIO_HOST_DID_LOCK_CSR, &result);
617 if ((result & 0xffff) != 0xffff) {
618 pr_debug("RIO: badness when releasing device lock %x:%x\n",
619 destid, hopcount);
620 return -EINVAL;
621 }
622
623 return 0;
624}
625
626/**
Matt Portereb188d02005-11-07 01:00:17 -0800627 * rio_route_add_entry- Add a route entry to a switch routing table
628 * @mport: Master port to send transaction
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800629 * @rswitch: Switch device
Matt Portereb188d02005-11-07 01:00:17 -0800630 * @table: Routing table ID
631 * @route_destid: Destination ID to be routed
632 * @route_port: Port number to be routed
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700633 * @lock: lock switch device flag
Matt Portereb188d02005-11-07 01:00:17 -0800634 *
635 * Calls the switch specific add_entry() method to add a route entry
636 * on a switch. The route table can be specified using the @table
637 * argument if a switch has per port routing tables or the normal
638 * use is to specific all tables (or the global table) by passing
639 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
640 * on failure.
641 */
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700642static int
643rio_route_add_entry(struct rio_mport *mport, struct rio_switch *rswitch,
644 u16 table, u16 route_destid, u8 route_port, int lock)
Matt Portereb188d02005-11-07 01:00:17 -0800645{
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700646 int rc;
647
648 if (lock) {
649 rc = rio_lock_device(mport, rswitch->destid,
650 rswitch->hopcount, 1000);
651 if (rc)
652 return rc;
653 }
654
655 rc = rswitch->add_entry(mport, rswitch->destid,
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800656 rswitch->hopcount, table,
Matt Portereb188d02005-11-07 01:00:17 -0800657 route_destid, route_port);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700658 if (lock)
659 rio_unlock_device(mport, rswitch->destid, rswitch->hopcount);
660
661 return rc;
Matt Portereb188d02005-11-07 01:00:17 -0800662}
663
664/**
665 * rio_route_get_entry- Read a route entry in a switch routing table
666 * @mport: Master port to send transaction
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800667 * @rswitch: Switch device
Matt Portereb188d02005-11-07 01:00:17 -0800668 * @table: Routing table ID
669 * @route_destid: Destination ID to be routed
670 * @route_port: Pointer to read port number into
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700671 * @lock: lock switch device flag
Matt Portereb188d02005-11-07 01:00:17 -0800672 *
673 * Calls the switch specific get_entry() method to read a route entry
674 * in a switch. The route table can be specified using the @table
675 * argument if a switch has per port routing tables or the normal
676 * use is to specific all tables (or the global table) by passing
677 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
678 * on failure.
679 */
680static int
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800681rio_route_get_entry(struct rio_mport *mport, struct rio_switch *rswitch, u16 table,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700682 u16 route_destid, u8 *route_port, int lock)
Matt Portereb188d02005-11-07 01:00:17 -0800683{
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700684 int rc;
685
686 if (lock) {
687 rc = rio_lock_device(mport, rswitch->destid,
688 rswitch->hopcount, 1000);
689 if (rc)
690 return rc;
691 }
692
693 rc = rswitch->get_entry(mport, rswitch->destid,
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800694 rswitch->hopcount, table,
Matt Portereb188d02005-11-07 01:00:17 -0800695 route_destid, route_port);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700696 if (lock)
697 rio_unlock_device(mport, rswitch->destid, rswitch->hopcount);
698
699 return rc;
Matt Portereb188d02005-11-07 01:00:17 -0800700}
701
702/**
703 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
704 * @port: Master port to send transaction
705 * @hopcount: Number of hops to the device
706 *
707 * Used during enumeration to read the Host Device ID Lock CSR on a
708 * RIO device. Returns the value of the lock register.
709 */
710static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
711{
712 u32 result;
713
Zhang Weie0423232008-04-18 13:33:42 -0700714 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800715 RIO_HOST_DID_LOCK_CSR, &result);
716
717 return (u16) (result & 0xffff);
718}
719
720/**
721 * rio_get_swpinfo_inport- Gets the ingress port number
722 * @mport: Master port to send transaction
723 * @destid: Destination ID associated with the switch
724 * @hopcount: Number of hops to the device
725 *
726 * Returns port number being used to access the switch device.
727 */
728static u8
729rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
730{
731 u32 result;
732
733 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
734 &result);
735
736 return (u8) (result & 0xff);
737}
738
739/**
740 * rio_get_swpinfo_tports- Gets total number of ports on the switch
741 * @mport: Master port to send transaction
742 * @destid: Destination ID associated with the switch
743 * @hopcount: Number of hops to the device
744 *
745 * Returns total numbers of ports implemented by the switch device.
746 */
747static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
748 u8 hopcount)
749{
750 u32 result;
751
752 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
753 &result);
754
755 return RIO_GET_TOTAL_PORTS(result);
756}
757
758/**
759 * rio_net_add_mport- Add a master port to a RIO network
760 * @net: RIO network
761 * @port: Master port to add
762 *
763 * Adds a master port to the network list of associated master
764 * ports..
765 */
766static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
767{
768 spin_lock(&rio_global_list_lock);
769 list_add_tail(&port->nnode, &net->mports);
770 spin_unlock(&rio_global_list_lock);
771}
772
773/**
774 * rio_enum_peer- Recursively enumerate a RIO network through a master port
775 * @net: RIO network being enumerated
776 * @port: Master port to send transactions
777 * @hopcount: Number of hops into the network
778 *
779 * Recursively enumerates a RIO network. Transactions are sent via the
780 * master port passed in @port.
781 */
Li Yang181a6ff2009-05-12 16:36:03 +0800782static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
Matt Portereb188d02005-11-07 01:00:17 -0800783 u8 hopcount)
784{
785 int port_num;
786 int num_ports;
787 int cur_destid;
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800788 int sw_destid;
789 int sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800790 struct rio_dev *rdev;
791 u16 destid;
792 int tmp;
793
794 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
795 pr_debug("RIO: PE already discovered by this host\n");
796 /*
797 * Already discovered by this host. Add it as another
798 * master port for the current network.
799 */
800 rio_net_add_mport(net, port);
801 return 0;
802 }
803
804 /* Attempt to acquire device lock */
Zhang Weie0423232008-04-18 13:33:42 -0700805 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
806 hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800807 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
808 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
809 < port->host_deviceid) {
810 /* Delay a bit */
811 mdelay(1);
812 /* Attempt to acquire device lock again */
Zhang Weie0423232008-04-18 13:33:42 -0700813 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
814 hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800815 RIO_HOST_DID_LOCK_CSR,
816 port->host_deviceid);
817 }
818
819 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
820 pr_debug(
821 "RIO: PE locked by a higher priority host...retreating\n");
822 return -1;
823 }
824
825 /* Setup new RIO device */
Zhang Weie0423232008-04-18 13:33:42 -0700826 rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
827 hopcount, 1);
828 if (rdev) {
Matt Portereb188d02005-11-07 01:00:17 -0800829 /* Add device to the global and bus/net specific list. */
830 list_add_tail(&rdev->net_list, &net->devices);
831 } else
832 return -1;
833
834 if (rio_is_switch(rdev)) {
835 next_switchid++;
Zhang Weie0423232008-04-18 13:33:42 -0700836 sw_inport = rio_get_swpinfo_inport(port,
837 RIO_ANY_DESTID(port->sys_size), hopcount);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800838 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700839 port->host_deviceid, sw_inport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800840 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800841
842 for (destid = 0; destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800843 if (destid == port->host_deviceid)
844 continue;
845 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700846 destid, sw_inport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800847 rdev->rswitch->route_table[destid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800848 }
849
850 num_ports =
Zhang Weie0423232008-04-18 13:33:42 -0700851 rio_get_swpinfo_tports(port, RIO_ANY_DESTID(port->sys_size),
852 hopcount);
Matt Portereb188d02005-11-07 01:00:17 -0800853 pr_debug(
854 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
855 rio_name(rdev), rdev->vid, rdev->did, num_ports);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800856 sw_destid = next_destid;
Matt Portereb188d02005-11-07 01:00:17 -0800857 for (port_num = 0; port_num < num_ports; port_num++) {
Thomas Moll933af4a2010-05-26 14:44:01 -0700858 /*Enable Input Output Port (transmitter reviever)*/
859 rio_enable_rx_tx_port(port, 0,
860 RIO_ANY_DESTID(port->sys_size),
861 hopcount, port_num);
862
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700863 if (sw_inport == port_num) {
864 rdev->rswitch->port_ok |= (1 << port_num);
Matt Portereb188d02005-11-07 01:00:17 -0800865 continue;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700866 }
Matt Portereb188d02005-11-07 01:00:17 -0800867
868 cur_destid = next_destid;
869
870 if (rio_sport_is_active
Zhang Weie0423232008-04-18 13:33:42 -0700871 (port, RIO_ANY_DESTID(port->sys_size), hopcount,
872 port_num)) {
Matt Portereb188d02005-11-07 01:00:17 -0800873 pr_debug(
874 "RIO: scanning device on port %d\n",
875 port_num);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700876 rdev->rswitch->port_ok |= (1 << port_num);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800877 rio_route_add_entry(port, rdev->rswitch,
Zhang Weie0423232008-04-18 13:33:42 -0700878 RIO_GLOBAL_TABLE,
879 RIO_ANY_DESTID(port->sys_size),
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700880 port_num, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800881
882 if (rio_enum_peer(net, port, hopcount + 1) < 0)
883 return -1;
884
885 /* Update routing tables */
886 if (next_destid > cur_destid) {
887 for (destid = cur_destid;
888 destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800889 if (destid == port->host_deviceid)
890 continue;
891 rio_route_add_entry(port, rdev->rswitch,
Matt Portereb188d02005-11-07 01:00:17 -0800892 RIO_GLOBAL_TABLE,
893 destid,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700894 port_num,
895 0);
Matt Portereb188d02005-11-07 01:00:17 -0800896 rdev->rswitch->
897 route_table[destid] =
898 port_num;
899 }
Matt Portereb188d02005-11-07 01:00:17 -0800900 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700901 } else {
902 /* If switch supports Error Management,
903 * set PORT_LOCKOUT bit for unused port
904 */
905 if (rdev->em_efptr)
906 rio_set_port_lockout(rdev, port_num, 1);
907
908 rdev->rswitch->port_ok &= ~(1 << port_num);
Matt Portereb188d02005-11-07 01:00:17 -0800909 }
910 }
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800911
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700912 /* Direct Port-write messages to the enumeratiing host */
913 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
914 (rdev->em_efptr)) {
915 rio_write_config_32(rdev,
916 rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
917 (port->host_deviceid << 16) |
918 (port->sys_size << 15));
919 }
920
921 rio_init_em(rdev);
922
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800923 /* Check for empty switch */
924 if (next_destid == sw_destid) {
925 next_destid++;
926 if (next_destid == port->host_deviceid)
927 next_destid++;
928 }
929
930 rdev->rswitch->destid = sw_destid;
Matt Portereb188d02005-11-07 01:00:17 -0800931 } else
932 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
933 rio_name(rdev), rdev->vid, rdev->did);
934
935 return 0;
936}
937
938/**
939 * rio_enum_complete- Tests if enumeration of a network is complete
940 * @port: Master port to send transaction
941 *
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700942 * Tests the Component Tag CSR for non-zero value (enumeration
943 * complete flag). Return %1 if enumeration is complete or %0 if
Matt Portereb188d02005-11-07 01:00:17 -0800944 * enumeration is incomplete.
945 */
946static int rio_enum_complete(struct rio_mport *port)
947{
948 u32 tag_csr;
Matt Portereb188d02005-11-07 01:00:17 -0800949
950 rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700951 return (tag_csr & 0xffff) ? 1 : 0;
Matt Portereb188d02005-11-07 01:00:17 -0800952}
953
954/**
955 * rio_disc_peer- Recursively discovers a RIO network through a master port
956 * @net: RIO network being discovered
957 * @port: Master port to send transactions
958 * @destid: Current destination ID in network
959 * @hopcount: Number of hops into the network
960 *
961 * Recursively discovers a RIO network. Transactions are sent via the
962 * master port passed in @port.
963 */
Li Yang181a6ff2009-05-12 16:36:03 +0800964static int __devinit
Matt Portereb188d02005-11-07 01:00:17 -0800965rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
966 u8 hopcount)
967{
968 u8 port_num, route_port;
969 int num_ports;
970 struct rio_dev *rdev;
971 u16 ndestid;
972
973 /* Setup new RIO device */
974 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
975 /* Add device to the global and bus/net specific list. */
976 list_add_tail(&rdev->net_list, &net->devices);
977 } else
978 return -1;
979
980 if (rio_is_switch(rdev)) {
981 next_switchid++;
982
983 /* Associated destid is how we accessed this switch */
984 rdev->rswitch->destid = destid;
985
986 num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
987 pr_debug(
988 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
989 rio_name(rdev), rdev->vid, rdev->did, num_ports);
990 for (port_num = 0; port_num < num_ports; port_num++) {
991 if (rio_get_swpinfo_inport(port, destid, hopcount) ==
992 port_num)
993 continue;
994
995 if (rio_sport_is_active
996 (port, destid, hopcount, port_num)) {
997 pr_debug(
998 "RIO: scanning device on port %d\n",
999 port_num);
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001000
1001 rio_lock_device(port, destid, hopcount, 1000);
1002
Zhang Weie0423232008-04-18 13:33:42 -07001003 for (ndestid = 0;
1004 ndestid < RIO_ANY_DESTID(port->sys_size);
Matt Portereb188d02005-11-07 01:00:17 -08001005 ndestid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001006 rio_route_get_entry(port, rdev->rswitch,
Matt Portereb188d02005-11-07 01:00:17 -08001007 RIO_GLOBAL_TABLE,
1008 ndestid,
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001009 &route_port, 0);
Matt Portereb188d02005-11-07 01:00:17 -08001010 if (route_port == port_num)
1011 break;
1012 }
1013
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001014 rio_unlock_device(port, destid, hopcount);
Matt Portereb188d02005-11-07 01:00:17 -08001015 if (rio_disc_peer
1016 (net, port, ndestid, hopcount + 1) < 0)
1017 return -1;
1018 }
1019 }
1020 } else
1021 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
1022 rio_name(rdev), rdev->vid, rdev->did);
1023
1024 return 0;
1025}
1026
1027/**
1028 * rio_mport_is_active- Tests if master port link is active
1029 * @port: Master port to test
1030 *
1031 * Reads the port error status CSR for the master port to
1032 * determine if the port has an active link. Returns
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001033 * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
Matt Portereb188d02005-11-07 01:00:17 -08001034 * or %0 if it is inactive.
1035 */
1036static int rio_mport_is_active(struct rio_mport *port)
1037{
1038 u32 result = 0;
1039 u32 ext_ftr_ptr;
1040 int *entry = rio_mport_phys_table;
1041
1042 do {
1043 if ((ext_ftr_ptr =
1044 rio_mport_get_feature(port, 1, 0, 0, *entry)))
1045 break;
1046 } while (*++entry >= 0);
1047
1048 if (ext_ftr_ptr)
1049 rio_local_read_config_32(port,
1050 ext_ftr_ptr +
1051 RIO_PORT_N_ERR_STS_CSR(port->index),
1052 &result);
1053
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001054 return result & RIO_PORT_N_ERR_STS_PORT_OK;
Matt Portereb188d02005-11-07 01:00:17 -08001055}
1056
1057/**
1058 * rio_alloc_net- Allocate and configure a new RIO network
1059 * @port: Master port associated with the RIO network
1060 *
1061 * Allocates a RIO network structure, initializes per-network
1062 * list heads, and adds the associated master port to the
1063 * network list of associated master ports. Returns a
1064 * RIO network pointer on success or %NULL on failure.
1065 */
1066static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
1067{
1068 struct rio_net *net;
1069
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001070 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
Matt Portereb188d02005-11-07 01:00:17 -08001071 if (net) {
Matt Portereb188d02005-11-07 01:00:17 -08001072 INIT_LIST_HEAD(&net->node);
1073 INIT_LIST_HEAD(&net->devices);
1074 INIT_LIST_HEAD(&net->mports);
1075 list_add_tail(&port->nnode, &net->mports);
1076 net->hport = port;
1077 net->id = next_net++;
1078 }
1079 return net;
1080}
1081
1082/**
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001083 * rio_update_route_tables- Updates route tables in switches
1084 * @port: Master port associated with the RIO network
1085 *
1086 * For each enumerated device, ensure that each switch in a system
1087 * has correct routing entries. Add routes for devices that where
1088 * unknown dirung the first enumeration pass through the switch.
1089 */
1090static void rio_update_route_tables(struct rio_mport *port)
1091{
1092 struct rio_dev *rdev;
1093 struct rio_switch *rswitch;
1094 u8 sport;
1095 u16 destid;
1096
1097 list_for_each_entry(rdev, &rio_devices, global_list) {
1098
1099 destid = (rio_is_switch(rdev))?rdev->rswitch->destid:rdev->destid;
1100
1101 list_for_each_entry(rswitch, &rio_switches, node) {
1102
1103 if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
1104 continue;
1105
1106 if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001107 /* Skip if destid ends in empty switch*/
1108 if (rswitch->destid == destid)
1109 continue;
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001110
1111 sport = rio_get_swpinfo_inport(port,
1112 rswitch->destid, rswitch->hopcount);
1113
1114 if (rswitch->add_entry) {
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001115 rio_route_add_entry(port, rswitch,
1116 RIO_GLOBAL_TABLE, destid,
1117 sport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001118 rswitch->route_table[destid] = sport;
1119 }
1120 }
1121 }
1122 }
1123}
1124
1125/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001126 * rio_init_em - Initializes RIO Error Management (for switches)
Randy Dunlap97ef6f72010-05-28 15:08:08 -07001127 * @rdev: RIO device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001128 *
1129 * For each enumerated switch, call device-specific error management
1130 * initialization routine (if supplied by the switch driver).
1131 */
1132static void rio_init_em(struct rio_dev *rdev)
1133{
1134 if (rio_is_switch(rdev) && (rdev->em_efptr) &&
1135 (rdev->rswitch->em_init)) {
1136 rdev->rswitch->em_init(rdev);
1137 }
1138}
1139
1140/**
1141 * rio_pw_enable - Enables/disables port-write handling by a master port
1142 * @port: Master port associated with port-write handling
1143 * @enable: 1=enable, 0=disable
1144 */
1145static void rio_pw_enable(struct rio_mport *port, int enable)
1146{
1147 if (port->ops->pwenable)
1148 port->ops->pwenable(port, enable);
1149}
1150
1151/**
Matt Portereb188d02005-11-07 01:00:17 -08001152 * rio_enum_mport- Start enumeration through a master port
1153 * @mport: Master port to send transactions
1154 *
1155 * Starts the enumeration process. If somebody has enumerated our
1156 * master port device, then give up. If not and we have an active
1157 * link, then start recursive peer enumeration. Returns %0 if
1158 * enumeration succeeds or %-EBUSY if enumeration fails.
1159 */
Al Viro37d33d12008-11-22 17:36:24 +00001160int __devinit rio_enum_mport(struct rio_mport *mport)
Matt Portereb188d02005-11-07 01:00:17 -08001161{
1162 struct rio_net *net = NULL;
1163 int rc = 0;
1164
1165 printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
1166 mport->name);
1167 /* If somebody else enumerated our master port device, bail. */
1168 if (rio_enum_host(mport) < 0) {
1169 printk(KERN_INFO
1170 "RIO: master port %d device has been enumerated by a remote host\n",
1171 mport->id);
1172 rc = -EBUSY;
1173 goto out;
1174 }
1175
1176 /* If master port has an active link, allocate net and enum peers */
1177 if (rio_mport_is_active(mport)) {
1178 if (!(net = rio_alloc_net(mport))) {
1179 printk(KERN_ERR "RIO: failed to allocate new net\n");
1180 rc = -ENOMEM;
1181 goto out;
1182 }
Thomas Moll933af4a2010-05-26 14:44:01 -07001183
1184 /* Enable Input Output Port (transmitter reviever) */
1185 rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
1186
Matt Portereb188d02005-11-07 01:00:17 -08001187 if (rio_enum_peer(net, mport, 0) < 0) {
1188 /* A higher priority host won enumeration, bail. */
1189 printk(KERN_INFO
1190 "RIO: master port %d device has lost enumeration to a remote host\n",
1191 mport->id);
1192 rio_clear_locks(mport);
1193 rc = -EBUSY;
1194 goto out;
1195 }
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001196 rio_update_route_tables(mport);
Matt Portereb188d02005-11-07 01:00:17 -08001197 rio_clear_locks(mport);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001198 rio_pw_enable(mport, 1);
Matt Portereb188d02005-11-07 01:00:17 -08001199 } else {
1200 printk(KERN_INFO "RIO: master port %d link inactive\n",
1201 mport->id);
1202 rc = -EINVAL;
1203 }
1204
1205 out:
1206 return rc;
1207}
1208
1209/**
1210 * rio_build_route_tables- Generate route tables from switch route entries
1211 *
1212 * For each switch device, generate a route table by copying existing
1213 * route entries from the switch.
1214 */
1215static void rio_build_route_tables(void)
1216{
1217 struct rio_dev *rdev;
1218 int i;
1219 u8 sport;
1220
1221 list_for_each_entry(rdev, &rio_devices, global_list)
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001222 if (rio_is_switch(rdev)) {
1223 rio_lock_device(rdev->net->hport, rdev->rswitch->destid,
1224 rdev->rswitch->hopcount, 1000);
1225 for (i = 0;
1226 i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
1227 i++) {
1228 if (rio_route_get_entry
1229 (rdev->net->hport, rdev->rswitch,
1230 RIO_GLOBAL_TABLE, i, &sport, 0) < 0)
1231 continue;
1232 rdev->rswitch->route_table[i] = sport;
1233 }
1234
1235 rio_unlock_device(rdev->net->hport,
1236 rdev->rswitch->destid,
1237 rdev->rswitch->hopcount);
Matt Portereb188d02005-11-07 01:00:17 -08001238 }
1239}
1240
1241/**
1242 * rio_enum_timeout- Signal that enumeration timed out
1243 * @data: Address of timeout flag.
1244 *
1245 * When the enumeration complete timer expires, set a flag that
1246 * signals to the discovery process that enumeration did not
1247 * complete in a sane amount of time.
1248 */
1249static void rio_enum_timeout(unsigned long data)
1250{
1251 /* Enumeration timed out, set flag */
1252 *(int *)data = 1;
1253}
1254
1255/**
1256 * rio_disc_mport- Start discovery through a master port
1257 * @mport: Master port to send transactions
1258 *
1259 * Starts the discovery process. If we have an active link,
1260 * then wait for the signal that enumeration is complete.
1261 * When enumeration completion is signaled, start recursive
1262 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1263 * on failure.
1264 */
Al Viro37d33d12008-11-22 17:36:24 +00001265int __devinit rio_disc_mport(struct rio_mport *mport)
Matt Portereb188d02005-11-07 01:00:17 -08001266{
1267 struct rio_net *net = NULL;
1268 int enum_timeout_flag = 0;
1269
1270 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1271 mport->name);
1272
1273 /* If master port has an active link, allocate net and discover peers */
1274 if (rio_mport_is_active(mport)) {
1275 if (!(net = rio_alloc_net(mport))) {
1276 printk(KERN_ERR "RIO: Failed to allocate new net\n");
1277 goto bail;
1278 }
1279
1280 pr_debug("RIO: wait for enumeration complete...");
1281
1282 rio_enum_timer.expires =
1283 jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1284 rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
1285 add_timer(&rio_enum_timer);
1286 while (!rio_enum_complete(mport)) {
1287 mdelay(1);
1288 if (enum_timeout_flag) {
1289 del_timer_sync(&rio_enum_timer);
1290 goto timeout;
1291 }
1292 }
1293 del_timer_sync(&rio_enum_timer);
1294
1295 pr_debug("done\n");
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001296
1297 /* Read DestID assigned by enumerator */
1298 rio_local_read_config_32(mport, RIO_DID_CSR,
1299 &mport->host_deviceid);
1300 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1301 mport->host_deviceid);
1302
Zhang Weie0423232008-04-18 13:33:42 -07001303 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1304 0) < 0) {
Matt Portereb188d02005-11-07 01:00:17 -08001305 printk(KERN_INFO
1306 "RIO: master port %d device has failed discovery\n",
1307 mport->id);
1308 goto bail;
1309 }
1310
1311 rio_build_route_tables();
1312 }
1313
1314 return 0;
1315
1316 timeout:
1317 pr_debug("timeout\n");
1318 bail:
1319 return -EBUSY;
1320}