blob: d2ea01872d6a915d3e4dfb6777ef598ce5f46e90 [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 Bounine058f88d62010-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 Bounine058f88d62010-05-26 14:44:03 -0700251 * @do_enum: Enumeration/Discovery mode flag
Matt Portereb188d02005-11-07 01:00:17 -0800252 *
Alexandre Bounine058f88d62010-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 Bounine058f88d62010-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 Bounine058f88d62010-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 Bounine058f88d62010-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
Alexandre Bounineae05cbd2010-10-27 15:34:29 -0700423 if (rdev->pef & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
424 rio_mport_read_config_32(port, destid, hopcount,
425 RIO_SWP_INFO_CAR, &rdev->swpinfo);
426 }
427
Matt Portereb188d02005-11-07 01:00:17 -0800428 rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
429 &rdev->src_ops);
430 rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
431 &rdev->dst_ops);
432
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800433 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
434 if (do_enum) {
435 rio_set_device_id(port, destid, hopcount, next_destid);
436 rdev->destid = next_destid++;
437 if (next_destid == port->host_deviceid)
438 next_destid++;
439 } else
440 rdev->destid = rio_get_device_id(port, destid, hopcount);
Matt Portereb188d02005-11-07 01:00:17 -0800441 } else
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800442 /* Switch device has an associated destID */
443 rdev->destid = RIO_INVALID_DESTID;
Matt Portereb188d02005-11-07 01:00:17 -0800444
445 /* If a PE has both switch and other functions, show it as a switch */
446 if (rio_is_switch(rdev)) {
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700447 rswitch = kzalloc(sizeof(*rswitch) +
448 RIO_GET_TOTAL_PORTS(rdev->swpinfo) *
449 sizeof(rswitch->nextdev[0]),
450 GFP_KERNEL);
Yang Li5f28c522009-05-11 22:36:02 +0000451 if (!rswitch)
452 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800453 rswitch->switchid = next_switchid;
454 rswitch->hopcount = hopcount;
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800455 rswitch->destid = destid;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700456 rswitch->port_ok = 0;
Zhang Weie0423232008-04-18 13:33:42 -0700457 rswitch->route_table = kzalloc(sizeof(u8)*
458 RIO_MAX_ROUTE_ENTRIES(port->sys_size),
459 GFP_KERNEL);
Yang Li5f28c522009-05-11 22:36:02 +0000460 if (!rswitch->route_table)
461 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800462 /* Initialize switch route table */
Zhang Weie0423232008-04-18 13:33:42 -0700463 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
464 rdid++)
Matt Portereb188d02005-11-07 01:00:17 -0800465 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
466 rdev->rswitch = rswitch;
Alexandre Bounineae05cbd2010-10-27 15:34:29 -0700467 rswitch->rdev = rdev;
Kay Sieversb53c75832008-12-04 10:01:52 -0800468 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
469 rdev->rswitch->switchid);
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700470 rio_switch_init(rdev, do_enum);
Matt Portereb188d02005-11-07 01:00:17 -0800471
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700472 if (do_enum && rdev->rswitch->clr_table)
473 rdev->rswitch->clr_table(port, destid, hopcount,
474 RIO_GLOBAL_TABLE);
475
Matt Portereb188d02005-11-07 01:00:17 -0800476 list_add_tail(&rswitch->node, &rio_switches);
477
Thomas Moll933af4a2010-05-26 14:44:01 -0700478 } else {
479 if (do_enum)
480 /*Enable Input Output Port (transmitter reviever)*/
481 rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
482
Kay Sieversb53c75832008-12-04 10:01:52 -0800483 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
484 rdev->destid);
Thomas Moll933af4a2010-05-26 14:44:01 -0700485 }
Matt Portereb188d02005-11-07 01:00:17 -0800486
487 rdev->dev.bus = &rio_bus_type;
Alexandre Bounine2c70f022010-10-27 15:34:26 -0700488 rdev->dev.parent = &rio_bus;
Matt Portereb188d02005-11-07 01:00:17 -0800489
490 device_initialize(&rdev->dev);
491 rdev->dev.release = rio_release_dev;
492 rio_dev_get(rdev);
493
Yang Hongyang284901a2009-04-06 19:01:15 -0700494 rdev->dma_mask = DMA_BIT_MASK(32);
Matt Porterfa78cc52005-11-07 01:00:18 -0800495 rdev->dev.dma_mask = &rdev->dma_mask;
Yang Hongyang284901a2009-04-06 19:01:15 -0700496 rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Matt Portereb188d02005-11-07 01:00:17 -0800497
498 if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
499 (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
500 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
501 0, 0xffff);
502
Yang Li5f28c522009-05-11 22:36:02 +0000503 ret = rio_add_device(rdev);
504 if (ret)
505 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800506
Matt Portereb188d02005-11-07 01:00:17 -0800507 return rdev;
Yang Li5f28c522009-05-11 22:36:02 +0000508
509cleanup:
510 if (rswitch) {
511 kfree(rswitch->route_table);
512 kfree(rswitch);
513 }
514 kfree(rdev);
515 return NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800516}
517
518/**
519 * rio_sport_is_active- Tests if a switch port has an active connection.
520 * @port: Master port to send transaction
521 * @destid: Associated destination ID for switch
522 * @hopcount: Hopcount to reach switch
523 * @sport: Switch port number
524 *
525 * Reads the port error status CSR for a particular switch port to
526 * determine if the port has an active link. Returns
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700527 * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
Matt Portereb188d02005-11-07 01:00:17 -0800528 * inactive.
529 */
530static int
531rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
532{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700533 u32 result = 0;
Matt Portereb188d02005-11-07 01:00:17 -0800534 u32 ext_ftr_ptr;
535
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700536 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800537
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700538 while (ext_ftr_ptr) {
539 rio_mport_read_config_32(port, destid, hopcount,
540 ext_ftr_ptr, &result);
541 result = RIO_GET_BLOCK_ID(result);
542 if ((result == RIO_EFB_SER_EP_FREE_ID) ||
543 (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
544 (result == RIO_EFB_SER_EP_FREC_ID))
Matt Portereb188d02005-11-07 01:00:17 -0800545 break;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700546
547 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
548 ext_ftr_ptr);
549 }
Matt Portereb188d02005-11-07 01:00:17 -0800550
551 if (ext_ftr_ptr)
552 rio_mport_read_config_32(port, destid, hopcount,
553 ext_ftr_ptr +
554 RIO_PORT_N_ERR_STS_CSR(sport),
555 &result);
556
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700557 return result & RIO_PORT_N_ERR_STS_PORT_OK;
Matt Portereb188d02005-11-07 01:00:17 -0800558}
559
560/**
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700561 * rio_lock_device - Acquires host device lock for specified device
562 * @port: Master port to send transaction
563 * @destid: Destination ID for device/switch
564 * @hopcount: Hopcount to reach switch
565 * @wait_ms: Max wait time in msec (0 = no timeout)
566 *
567 * Attepts to acquire host device lock for specified device
568 * Returns 0 if device lock acquired or EINVAL if timeout expires.
569 */
570static int
571rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
572{
573 u32 result;
574 int tcnt = 0;
575
576 /* Attempt to acquire device lock */
577 rio_mport_write_config_32(port, destid, hopcount,
578 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
579 rio_mport_read_config_32(port, destid, hopcount,
580 RIO_HOST_DID_LOCK_CSR, &result);
581
582 while (result != port->host_deviceid) {
583 if (wait_ms != 0 && tcnt == wait_ms) {
584 pr_debug("RIO: timeout when locking device %x:%x\n",
585 destid, hopcount);
586 return -EINVAL;
587 }
588
589 /* Delay a bit */
590 mdelay(1);
591 tcnt++;
592 /* Try to acquire device lock again */
593 rio_mport_write_config_32(port, destid,
594 hopcount,
595 RIO_HOST_DID_LOCK_CSR,
596 port->host_deviceid);
597 rio_mport_read_config_32(port, destid,
598 hopcount,
599 RIO_HOST_DID_LOCK_CSR, &result);
600 }
601
602 return 0;
603}
604
605/**
606 * rio_unlock_device - Releases host device lock for specified device
607 * @port: Master port to send transaction
608 * @destid: Destination ID for device/switch
609 * @hopcount: Hopcount to reach switch
610 *
611 * Returns 0 if device lock released or EINVAL if fails.
612 */
613static int
614rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
615{
616 u32 result;
617
618 /* Release device lock */
619 rio_mport_write_config_32(port, destid,
620 hopcount,
621 RIO_HOST_DID_LOCK_CSR,
622 port->host_deviceid);
623 rio_mport_read_config_32(port, destid, hopcount,
624 RIO_HOST_DID_LOCK_CSR, &result);
625 if ((result & 0xffff) != 0xffff) {
626 pr_debug("RIO: badness when releasing device lock %x:%x\n",
627 destid, hopcount);
628 return -EINVAL;
629 }
630
631 return 0;
632}
633
634/**
Matt Portereb188d02005-11-07 01:00:17 -0800635 * rio_route_add_entry- Add a route entry to a switch routing table
636 * @mport: Master port to send transaction
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800637 * @rswitch: Switch device
Matt Portereb188d02005-11-07 01:00:17 -0800638 * @table: Routing table ID
639 * @route_destid: Destination ID to be routed
640 * @route_port: Port number to be routed
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700641 * @lock: lock switch device flag
Matt Portereb188d02005-11-07 01:00:17 -0800642 *
643 * Calls the switch specific add_entry() method to add a route entry
644 * on a switch. The route table can be specified using the @table
645 * argument if a switch has per port routing tables or the normal
646 * use is to specific all tables (or the global table) by passing
647 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
648 * on failure.
649 */
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700650static int
651rio_route_add_entry(struct rio_mport *mport, struct rio_switch *rswitch,
652 u16 table, u16 route_destid, u8 route_port, int lock)
Matt Portereb188d02005-11-07 01:00:17 -0800653{
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700654 int rc;
655
656 if (lock) {
657 rc = rio_lock_device(mport, rswitch->destid,
658 rswitch->hopcount, 1000);
659 if (rc)
660 return rc;
661 }
662
663 rc = rswitch->add_entry(mport, rswitch->destid,
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800664 rswitch->hopcount, table,
Matt Portereb188d02005-11-07 01:00:17 -0800665 route_destid, route_port);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700666 if (lock)
667 rio_unlock_device(mport, rswitch->destid, rswitch->hopcount);
668
669 return rc;
Matt Portereb188d02005-11-07 01:00:17 -0800670}
671
672/**
673 * rio_route_get_entry- Read a route entry in a switch routing table
674 * @mport: Master port to send transaction
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800675 * @rswitch: Switch device
Matt Portereb188d02005-11-07 01:00:17 -0800676 * @table: Routing table ID
677 * @route_destid: Destination ID to be routed
678 * @route_port: Pointer to read port number into
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700679 * @lock: lock switch device flag
Matt Portereb188d02005-11-07 01:00:17 -0800680 *
681 * Calls the switch specific get_entry() method to read a route entry
682 * in a switch. The route table can be specified using the @table
683 * argument if a switch has per port routing tables or the normal
684 * use is to specific all tables (or the global table) by passing
685 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
686 * on failure.
687 */
688static int
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800689rio_route_get_entry(struct rio_mport *mport, struct rio_switch *rswitch, u16 table,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700690 u16 route_destid, u8 *route_port, int lock)
Matt Portereb188d02005-11-07 01:00:17 -0800691{
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700692 int rc;
693
694 if (lock) {
695 rc = rio_lock_device(mport, rswitch->destid,
696 rswitch->hopcount, 1000);
697 if (rc)
698 return rc;
699 }
700
701 rc = rswitch->get_entry(mport, rswitch->destid,
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800702 rswitch->hopcount, table,
Matt Portereb188d02005-11-07 01:00:17 -0800703 route_destid, route_port);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700704 if (lock)
705 rio_unlock_device(mport, rswitch->destid, rswitch->hopcount);
706
707 return rc;
Matt Portereb188d02005-11-07 01:00:17 -0800708}
709
710/**
711 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
712 * @port: Master port to send transaction
713 * @hopcount: Number of hops to the device
714 *
715 * Used during enumeration to read the Host Device ID Lock CSR on a
716 * RIO device. Returns the value of the lock register.
717 */
718static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
719{
720 u32 result;
721
Zhang Weie0423232008-04-18 13:33:42 -0700722 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800723 RIO_HOST_DID_LOCK_CSR, &result);
724
725 return (u16) (result & 0xffff);
726}
727
728/**
Matt Portereb188d02005-11-07 01:00:17 -0800729 * rio_net_add_mport- Add a master port to a RIO network
730 * @net: RIO network
731 * @port: Master port to add
732 *
733 * Adds a master port to the network list of associated master
734 * ports..
735 */
736static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
737{
738 spin_lock(&rio_global_list_lock);
739 list_add_tail(&port->nnode, &net->mports);
740 spin_unlock(&rio_global_list_lock);
741}
742
743/**
744 * rio_enum_peer- Recursively enumerate a RIO network through a master port
745 * @net: RIO network being enumerated
746 * @port: Master port to send transactions
747 * @hopcount: Number of hops into the network
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700748 * @prev: Previous RIO device connected to the enumerated one
749 * @prev_port: Port on previous RIO device
Matt Portereb188d02005-11-07 01:00:17 -0800750 *
751 * Recursively enumerates a RIO network. Transactions are sent via the
752 * master port passed in @port.
753 */
Li Yang181a6ff2009-05-12 16:36:03 +0800754static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700755 u8 hopcount, struct rio_dev *prev, int prev_port)
Matt Portereb188d02005-11-07 01:00:17 -0800756{
757 int port_num;
Matt Portereb188d02005-11-07 01:00:17 -0800758 int cur_destid;
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800759 int sw_destid;
760 int sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800761 struct rio_dev *rdev;
762 u16 destid;
763 int tmp;
764
765 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
766 pr_debug("RIO: PE already discovered by this host\n");
767 /*
768 * Already discovered by this host. Add it as another
769 * master port for the current network.
770 */
771 rio_net_add_mport(net, port);
772 return 0;
773 }
774
775 /* Attempt to acquire device lock */
Zhang Weie0423232008-04-18 13:33:42 -0700776 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
777 hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800778 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
779 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
780 < port->host_deviceid) {
781 /* Delay a bit */
782 mdelay(1);
783 /* Attempt to acquire device lock again */
Zhang Weie0423232008-04-18 13:33:42 -0700784 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
785 hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800786 RIO_HOST_DID_LOCK_CSR,
787 port->host_deviceid);
788 }
789
790 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
791 pr_debug(
792 "RIO: PE locked by a higher priority host...retreating\n");
793 return -1;
794 }
795
796 /* Setup new RIO device */
Zhang Weie0423232008-04-18 13:33:42 -0700797 rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
798 hopcount, 1);
799 if (rdev) {
Matt Portereb188d02005-11-07 01:00:17 -0800800 /* Add device to the global and bus/net specific list. */
801 list_add_tail(&rdev->net_list, &net->devices);
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700802 rdev->prev = prev;
803 if (prev && rio_is_switch(prev))
804 prev->rswitch->nextdev[prev_port] = rdev;
Matt Portereb188d02005-11-07 01:00:17 -0800805 } else
806 return -1;
807
808 if (rio_is_switch(rdev)) {
809 next_switchid++;
Alexandre Bounineae05cbd2010-10-27 15:34:29 -0700810 sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800811 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700812 port->host_deviceid, sw_inport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800813 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800814
815 for (destid = 0; destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800816 if (destid == port->host_deviceid)
817 continue;
818 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700819 destid, sw_inport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800820 rdev->rswitch->route_table[destid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800821 }
822
Matt Portereb188d02005-11-07 01:00:17 -0800823 pr_debug(
824 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700825 rio_name(rdev), rdev->vid, rdev->did,
826 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800827 sw_destid = next_destid;
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700828 for (port_num = 0;
829 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
830 port_num++) {
Thomas Moll933af4a2010-05-26 14:44:01 -0700831 /*Enable Input Output Port (transmitter reviever)*/
832 rio_enable_rx_tx_port(port, 0,
833 RIO_ANY_DESTID(port->sys_size),
834 hopcount, port_num);
835
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700836 if (sw_inport == port_num) {
837 rdev->rswitch->port_ok |= (1 << port_num);
Matt Portereb188d02005-11-07 01:00:17 -0800838 continue;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700839 }
Matt Portereb188d02005-11-07 01:00:17 -0800840
841 cur_destid = next_destid;
842
843 if (rio_sport_is_active
Zhang Weie0423232008-04-18 13:33:42 -0700844 (port, RIO_ANY_DESTID(port->sys_size), hopcount,
845 port_num)) {
Matt Portereb188d02005-11-07 01:00:17 -0800846 pr_debug(
847 "RIO: scanning device on port %d\n",
848 port_num);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700849 rdev->rswitch->port_ok |= (1 << port_num);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800850 rio_route_add_entry(port, rdev->rswitch,
Zhang Weie0423232008-04-18 13:33:42 -0700851 RIO_GLOBAL_TABLE,
852 RIO_ANY_DESTID(port->sys_size),
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700853 port_num, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800854
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700855 if (rio_enum_peer(net, port, hopcount + 1,
856 rdev, port_num) < 0)
Matt Portereb188d02005-11-07 01:00:17 -0800857 return -1;
858
859 /* Update routing tables */
860 if (next_destid > cur_destid) {
861 for (destid = cur_destid;
862 destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800863 if (destid == port->host_deviceid)
864 continue;
865 rio_route_add_entry(port, rdev->rswitch,
Matt Portereb188d02005-11-07 01:00:17 -0800866 RIO_GLOBAL_TABLE,
867 destid,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700868 port_num,
869 0);
Matt Portereb188d02005-11-07 01:00:17 -0800870 rdev->rswitch->
871 route_table[destid] =
872 port_num;
873 }
Matt Portereb188d02005-11-07 01:00:17 -0800874 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700875 } else {
876 /* If switch supports Error Management,
877 * set PORT_LOCKOUT bit for unused port
878 */
879 if (rdev->em_efptr)
880 rio_set_port_lockout(rdev, port_num, 1);
881
882 rdev->rswitch->port_ok &= ~(1 << port_num);
Matt Portereb188d02005-11-07 01:00:17 -0800883 }
884 }
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800885
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700886 /* Direct Port-write messages to the enumeratiing host */
887 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
888 (rdev->em_efptr)) {
889 rio_write_config_32(rdev,
890 rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
891 (port->host_deviceid << 16) |
892 (port->sys_size << 15));
893 }
894
895 rio_init_em(rdev);
896
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800897 /* Check for empty switch */
898 if (next_destid == sw_destid) {
899 next_destid++;
900 if (next_destid == port->host_deviceid)
901 next_destid++;
902 }
903
904 rdev->rswitch->destid = sw_destid;
Matt Portereb188d02005-11-07 01:00:17 -0800905 } else
906 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
907 rio_name(rdev), rdev->vid, rdev->did);
908
909 return 0;
910}
911
912/**
913 * rio_enum_complete- Tests if enumeration of a network is complete
914 * @port: Master port to send transaction
915 *
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700916 * Tests the Component Tag CSR for non-zero value (enumeration
917 * complete flag). Return %1 if enumeration is complete or %0 if
Matt Portereb188d02005-11-07 01:00:17 -0800918 * enumeration is incomplete.
919 */
920static int rio_enum_complete(struct rio_mport *port)
921{
922 u32 tag_csr;
Matt Portereb188d02005-11-07 01:00:17 -0800923
924 rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700925 return (tag_csr & 0xffff) ? 1 : 0;
Matt Portereb188d02005-11-07 01:00:17 -0800926}
927
928/**
929 * rio_disc_peer- Recursively discovers a RIO network through a master port
930 * @net: RIO network being discovered
931 * @port: Master port to send transactions
932 * @destid: Current destination ID in network
933 * @hopcount: Number of hops into the network
934 *
935 * Recursively discovers a RIO network. Transactions are sent via the
936 * master port passed in @port.
937 */
Li Yang181a6ff2009-05-12 16:36:03 +0800938static int __devinit
Matt Portereb188d02005-11-07 01:00:17 -0800939rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
940 u8 hopcount)
941{
942 u8 port_num, route_port;
Matt Portereb188d02005-11-07 01:00:17 -0800943 struct rio_dev *rdev;
944 u16 ndestid;
945
946 /* Setup new RIO device */
947 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
948 /* Add device to the global and bus/net specific list. */
949 list_add_tail(&rdev->net_list, &net->devices);
950 } else
951 return -1;
952
953 if (rio_is_switch(rdev)) {
954 next_switchid++;
955
956 /* Associated destid is how we accessed this switch */
957 rdev->rswitch->destid = destid;
958
Matt Portereb188d02005-11-07 01:00:17 -0800959 pr_debug(
960 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700961 rio_name(rdev), rdev->vid, rdev->did,
962 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
963 for (port_num = 0;
964 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
965 port_num++) {
Alexandre Bounineae05cbd2010-10-27 15:34:29 -0700966 if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
Matt Portereb188d02005-11-07 01:00:17 -0800967 continue;
968
969 if (rio_sport_is_active
970 (port, destid, hopcount, port_num)) {
971 pr_debug(
972 "RIO: scanning device on port %d\n",
973 port_num);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700974
975 rio_lock_device(port, destid, hopcount, 1000);
976
Zhang Weie0423232008-04-18 13:33:42 -0700977 for (ndestid = 0;
978 ndestid < RIO_ANY_DESTID(port->sys_size);
Matt Portereb188d02005-11-07 01:00:17 -0800979 ndestid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800980 rio_route_get_entry(port, rdev->rswitch,
Matt Portereb188d02005-11-07 01:00:17 -0800981 RIO_GLOBAL_TABLE,
982 ndestid,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700983 &route_port, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800984 if (route_port == port_num)
985 break;
986 }
987
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700988 rio_unlock_device(port, destid, hopcount);
Matt Portereb188d02005-11-07 01:00:17 -0800989 if (rio_disc_peer
990 (net, port, ndestid, hopcount + 1) < 0)
991 return -1;
992 }
993 }
994 } else
995 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
996 rio_name(rdev), rdev->vid, rdev->did);
997
998 return 0;
999}
1000
1001/**
1002 * rio_mport_is_active- Tests if master port link is active
1003 * @port: Master port to test
1004 *
1005 * Reads the port error status CSR for the master port to
1006 * determine if the port has an active link. Returns
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001007 * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
Matt Portereb188d02005-11-07 01:00:17 -08001008 * or %0 if it is inactive.
1009 */
1010static int rio_mport_is_active(struct rio_mport *port)
1011{
1012 u32 result = 0;
1013 u32 ext_ftr_ptr;
1014 int *entry = rio_mport_phys_table;
1015
1016 do {
1017 if ((ext_ftr_ptr =
1018 rio_mport_get_feature(port, 1, 0, 0, *entry)))
1019 break;
1020 } while (*++entry >= 0);
1021
1022 if (ext_ftr_ptr)
1023 rio_local_read_config_32(port,
1024 ext_ftr_ptr +
1025 RIO_PORT_N_ERR_STS_CSR(port->index),
1026 &result);
1027
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001028 return result & RIO_PORT_N_ERR_STS_PORT_OK;
Matt Portereb188d02005-11-07 01:00:17 -08001029}
1030
1031/**
1032 * rio_alloc_net- Allocate and configure a new RIO network
1033 * @port: Master port associated with the RIO network
1034 *
1035 * Allocates a RIO network structure, initializes per-network
1036 * list heads, and adds the associated master port to the
1037 * network list of associated master ports. Returns a
1038 * RIO network pointer on success or %NULL on failure.
1039 */
1040static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
1041{
1042 struct rio_net *net;
1043
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001044 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
Matt Portereb188d02005-11-07 01:00:17 -08001045 if (net) {
Matt Portereb188d02005-11-07 01:00:17 -08001046 INIT_LIST_HEAD(&net->node);
1047 INIT_LIST_HEAD(&net->devices);
1048 INIT_LIST_HEAD(&net->mports);
1049 list_add_tail(&port->nnode, &net->mports);
1050 net->hport = port;
1051 net->id = next_net++;
1052 }
1053 return net;
1054}
1055
1056/**
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001057 * rio_update_route_tables- Updates route tables in switches
1058 * @port: Master port associated with the RIO network
1059 *
1060 * For each enumerated device, ensure that each switch in a system
1061 * has correct routing entries. Add routes for devices that where
1062 * unknown dirung the first enumeration pass through the switch.
1063 */
1064static void rio_update_route_tables(struct rio_mport *port)
1065{
1066 struct rio_dev *rdev;
1067 struct rio_switch *rswitch;
1068 u8 sport;
1069 u16 destid;
1070
1071 list_for_each_entry(rdev, &rio_devices, global_list) {
1072
1073 destid = (rio_is_switch(rdev))?rdev->rswitch->destid:rdev->destid;
1074
1075 list_for_each_entry(rswitch, &rio_switches, node) {
1076
1077 if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
1078 continue;
1079
1080 if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001081 /* Skip if destid ends in empty switch*/
1082 if (rswitch->destid == destid)
1083 continue;
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001084
Alexandre Bounineae05cbd2010-10-27 15:34:29 -07001085 sport = RIO_GET_PORT_NUM(rswitch->rdev->swpinfo);
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001086
1087 if (rswitch->add_entry) {
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001088 rio_route_add_entry(port, rswitch,
1089 RIO_GLOBAL_TABLE, destid,
1090 sport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001091 rswitch->route_table[destid] = sport;
1092 }
1093 }
1094 }
1095 }
1096}
1097
1098/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001099 * rio_init_em - Initializes RIO Error Management (for switches)
Randy Dunlap97ef6f72010-05-28 15:08:08 -07001100 * @rdev: RIO device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001101 *
1102 * For each enumerated switch, call device-specific error management
1103 * initialization routine (if supplied by the switch driver).
1104 */
1105static void rio_init_em(struct rio_dev *rdev)
1106{
1107 if (rio_is_switch(rdev) && (rdev->em_efptr) &&
1108 (rdev->rswitch->em_init)) {
1109 rdev->rswitch->em_init(rdev);
1110 }
1111}
1112
1113/**
1114 * rio_pw_enable - Enables/disables port-write handling by a master port
1115 * @port: Master port associated with port-write handling
1116 * @enable: 1=enable, 0=disable
1117 */
1118static void rio_pw_enable(struct rio_mport *port, int enable)
1119{
1120 if (port->ops->pwenable)
1121 port->ops->pwenable(port, enable);
1122}
1123
1124/**
Matt Portereb188d02005-11-07 01:00:17 -08001125 * rio_enum_mport- Start enumeration through a master port
1126 * @mport: Master port to send transactions
1127 *
1128 * Starts the enumeration process. If somebody has enumerated our
1129 * master port device, then give up. If not and we have an active
1130 * link, then start recursive peer enumeration. Returns %0 if
1131 * enumeration succeeds or %-EBUSY if enumeration fails.
1132 */
Al Viro37d33d12008-11-22 17:36:24 +00001133int __devinit rio_enum_mport(struct rio_mport *mport)
Matt Portereb188d02005-11-07 01:00:17 -08001134{
1135 struct rio_net *net = NULL;
1136 int rc = 0;
1137
1138 printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
1139 mport->name);
1140 /* If somebody else enumerated our master port device, bail. */
1141 if (rio_enum_host(mport) < 0) {
1142 printk(KERN_INFO
1143 "RIO: master port %d device has been enumerated by a remote host\n",
1144 mport->id);
1145 rc = -EBUSY;
1146 goto out;
1147 }
1148
1149 /* If master port has an active link, allocate net and enum peers */
1150 if (rio_mport_is_active(mport)) {
1151 if (!(net = rio_alloc_net(mport))) {
1152 printk(KERN_ERR "RIO: failed to allocate new net\n");
1153 rc = -ENOMEM;
1154 goto out;
1155 }
Thomas Moll933af4a2010-05-26 14:44:01 -07001156
1157 /* Enable Input Output Port (transmitter reviever) */
1158 rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
1159
Alexandre Bounine68fe4df2010-10-27 15:34:29 -07001160 if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
Matt Portereb188d02005-11-07 01:00:17 -08001161 /* A higher priority host won enumeration, bail. */
1162 printk(KERN_INFO
1163 "RIO: master port %d device has lost enumeration to a remote host\n",
1164 mport->id);
1165 rio_clear_locks(mport);
1166 rc = -EBUSY;
1167 goto out;
1168 }
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001169 rio_update_route_tables(mport);
Matt Portereb188d02005-11-07 01:00:17 -08001170 rio_clear_locks(mport);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001171 rio_pw_enable(mport, 1);
Matt Portereb188d02005-11-07 01:00:17 -08001172 } else {
1173 printk(KERN_INFO "RIO: master port %d link inactive\n",
1174 mport->id);
1175 rc = -EINVAL;
1176 }
1177
1178 out:
1179 return rc;
1180}
1181
1182/**
1183 * rio_build_route_tables- Generate route tables from switch route entries
1184 *
1185 * For each switch device, generate a route table by copying existing
1186 * route entries from the switch.
1187 */
1188static void rio_build_route_tables(void)
1189{
1190 struct rio_dev *rdev;
1191 int i;
1192 u8 sport;
1193
1194 list_for_each_entry(rdev, &rio_devices, global_list)
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001195 if (rio_is_switch(rdev)) {
1196 rio_lock_device(rdev->net->hport, rdev->rswitch->destid,
1197 rdev->rswitch->hopcount, 1000);
1198 for (i = 0;
1199 i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
1200 i++) {
1201 if (rio_route_get_entry
1202 (rdev->net->hport, rdev->rswitch,
1203 RIO_GLOBAL_TABLE, i, &sport, 0) < 0)
1204 continue;
1205 rdev->rswitch->route_table[i] = sport;
1206 }
1207
1208 rio_unlock_device(rdev->net->hport,
1209 rdev->rswitch->destid,
1210 rdev->rswitch->hopcount);
Matt Portereb188d02005-11-07 01:00:17 -08001211 }
1212}
1213
1214/**
1215 * rio_enum_timeout- Signal that enumeration timed out
1216 * @data: Address of timeout flag.
1217 *
1218 * When the enumeration complete timer expires, set a flag that
1219 * signals to the discovery process that enumeration did not
1220 * complete in a sane amount of time.
1221 */
1222static void rio_enum_timeout(unsigned long data)
1223{
1224 /* Enumeration timed out, set flag */
1225 *(int *)data = 1;
1226}
1227
1228/**
1229 * rio_disc_mport- Start discovery through a master port
1230 * @mport: Master port to send transactions
1231 *
1232 * Starts the discovery process. If we have an active link,
1233 * then wait for the signal that enumeration is complete.
1234 * When enumeration completion is signaled, start recursive
1235 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1236 * on failure.
1237 */
Al Viro37d33d12008-11-22 17:36:24 +00001238int __devinit rio_disc_mport(struct rio_mport *mport)
Matt Portereb188d02005-11-07 01:00:17 -08001239{
1240 struct rio_net *net = NULL;
1241 int enum_timeout_flag = 0;
1242
1243 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1244 mport->name);
1245
1246 /* If master port has an active link, allocate net and discover peers */
1247 if (rio_mport_is_active(mport)) {
1248 if (!(net = rio_alloc_net(mport))) {
1249 printk(KERN_ERR "RIO: Failed to allocate new net\n");
1250 goto bail;
1251 }
1252
1253 pr_debug("RIO: wait for enumeration complete...");
1254
1255 rio_enum_timer.expires =
1256 jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1257 rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
1258 add_timer(&rio_enum_timer);
1259 while (!rio_enum_complete(mport)) {
1260 mdelay(1);
1261 if (enum_timeout_flag) {
1262 del_timer_sync(&rio_enum_timer);
1263 goto timeout;
1264 }
1265 }
1266 del_timer_sync(&rio_enum_timer);
1267
1268 pr_debug("done\n");
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001269
1270 /* Read DestID assigned by enumerator */
1271 rio_local_read_config_32(mport, RIO_DID_CSR,
1272 &mport->host_deviceid);
1273 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1274 mport->host_deviceid);
1275
Zhang Weie0423232008-04-18 13:33:42 -07001276 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1277 0) < 0) {
Matt Portereb188d02005-11-07 01:00:17 -08001278 printk(KERN_INFO
1279 "RIO: master port %d device has failed discovery\n",
1280 mport->id);
1281 goto bail;
1282 }
1283
1284 rio_build_route_tables();
1285 }
1286
1287 return 0;
1288
1289 timeout:
1290 pr_debug("timeout\n");
1291 bail:
1292 return -EBUSY;
1293}