blob: 0a27253c9215dceaacdb77b3c8f0a846887a8a12 [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>
Alexandre Bouninefa3dbaa2012-10-04 17:16:04 -070034#include <linux/sched.h>
Tim Schmielaude259682006-01-08 01:02:05 -080035#include <linux/jiffies.h>
36#include <linux/slab.h>
Matt Portereb188d02005-11-07 01:00:17 -080037
38#include "rio.h"
39
40LIST_HEAD(rio_devices);
41static LIST_HEAD(rio_switches);
42
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -070043static void rio_init_em(struct rio_dev *rdev);
44
Matt Porterfa78cc52005-11-07 01:00:18 -080045DEFINE_SPINLOCK(rio_global_list_lock);
46
Matt Portereb188d02005-11-07 01:00:17 -080047static int next_destid = 0;
Matt Portereb188d02005-11-07 01:00:17 -080048static int next_net = 0;
Alexandre Bounineaf84ca32010-10-27 15:34:34 -070049static int next_comptag = 1;
Matt Portereb188d02005-11-07 01:00:17 -080050
Matt Portereb188d02005-11-07 01:00:17 -080051static int rio_mport_phys_table[] = {
52 RIO_EFB_PAR_EP_ID,
53 RIO_EFB_PAR_EP_REC_ID,
54 RIO_EFB_SER_EP_ID,
55 RIO_EFB_SER_EP_REC_ID,
56 -1,
57};
58
Matt Portereb188d02005-11-07 01:00:17 -080059/**
60 * rio_get_device_id - Get the base/extended device id for a device
61 * @port: RIO master port
62 * @destid: Destination ID of device
63 * @hopcount: Hopcount to device
64 *
65 * Reads the base/extended device id from a device. Returns the
66 * 8/16-bit device ID.
67 */
68static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
69{
70 u32 result;
71
72 rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
73
Zhang Weie0423232008-04-18 13:33:42 -070074 return RIO_GET_DID(port->sys_size, result);
Matt Portereb188d02005-11-07 01:00:17 -080075}
76
77/**
78 * rio_set_device_id - Set the base/extended device id for a device
79 * @port: RIO master port
80 * @destid: Destination ID of device
81 * @hopcount: Hopcount to device
82 * @did: Device ID value to be written
83 *
84 * Writes the base/extended device id from a device.
85 */
Matt Porterfa78cc52005-11-07 01:00:18 -080086static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
Matt Portereb188d02005-11-07 01:00:17 -080087{
88 rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
Zhang Weie0423232008-04-18 13:33:42 -070089 RIO_SET_DID(port->sys_size, did));
Matt Portereb188d02005-11-07 01:00:17 -080090}
91
92/**
93 * rio_local_set_device_id - Set the base/extended device id for a port
94 * @port: RIO master port
95 * @did: Device ID value to be written
96 *
97 * Writes the base/extended device id from a device.
98 */
99static void rio_local_set_device_id(struct rio_mport *port, u16 did)
100{
Zhang Weie0423232008-04-18 13:33:42 -0700101 rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
102 did));
Matt Portereb188d02005-11-07 01:00:17 -0800103}
104
105/**
106 * rio_clear_locks- Release all host locks and signal enumeration complete
107 * @port: Master port to issue transaction
108 *
109 * Marks the component tag CSR on each device with the enumeration
110 * complete flag. When complete, it then release the host locks on
111 * each device. Returns 0 on success or %-EINVAL on failure.
112 */
113static int rio_clear_locks(struct rio_mport *port)
114{
115 struct rio_dev *rdev;
116 u32 result;
117 int ret = 0;
118
Matt Portereb188d02005-11-07 01:00:17 -0800119 /* Release host device id locks */
120 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
121 port->host_deviceid);
122 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
123 if ((result & 0xffff) != 0xffff) {
124 printk(KERN_INFO
125 "RIO: badness when releasing host lock on master port, result %8.8x\n",
126 result);
127 ret = -EINVAL;
128 }
129 list_for_each_entry(rdev, &rio_devices, global_list) {
130 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
131 port->host_deviceid);
132 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
133 if ((result & 0xffff) != 0xffff) {
134 printk(KERN_INFO
135 "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
136 rdev->vid, rdev->did);
137 ret = -EINVAL;
138 }
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700139
140 /* Mark device as discovered and enable master */
141 rio_read_config_32(rdev,
142 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
143 &result);
144 result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
145 rio_write_config_32(rdev,
146 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
147 result);
Matt Portereb188d02005-11-07 01:00:17 -0800148 }
149
150 return ret;
151}
152
153/**
154 * rio_enum_host- Set host lock and initialize host destination ID
155 * @port: Master port to issue transaction
156 *
157 * Sets the local host master port lock and destination ID register
158 * with the host device ID value. The host device ID value is provided
159 * by the platform. Returns %0 on success or %-1 on failure.
160 */
161static int rio_enum_host(struct rio_mport *port)
162{
163 u32 result;
164
165 /* Set master port host device id lock */
166 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
167 port->host_deviceid);
168
169 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
170 if ((result & 0xffff) != port->host_deviceid)
171 return -1;
172
173 /* Set master port destid and init destid ctr */
174 rio_local_set_device_id(port, port->host_deviceid);
175
176 if (next_destid == port->host_deviceid)
177 next_destid++;
178
179 return 0;
180}
181
182/**
183 * rio_device_has_destid- Test if a device contains a destination ID register
184 * @port: Master port to issue transaction
185 * @src_ops: RIO device source operations
186 * @dst_ops: RIO device destination operations
187 *
188 * Checks the provided @src_ops and @dst_ops for the necessary transaction
189 * capabilities that indicate whether or not a device will implement a
190 * destination ID register. Returns 1 if true or 0 if false.
191 */
192static int rio_device_has_destid(struct rio_mport *port, int src_ops,
193 int dst_ops)
194{
Matt Porterfa78cc52005-11-07 01:00:18 -0800195 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;
196
197 return !!((src_ops | dst_ops) & mask);
Matt Portereb188d02005-11-07 01:00:17 -0800198}
199
200/**
201 * rio_release_dev- Frees a RIO device struct
202 * @dev: LDM device associated with a RIO device struct
203 *
204 * Gets the RIO device struct associated a RIO device struct.
205 * The RIO device struct is freed.
206 */
207static void rio_release_dev(struct device *dev)
208{
209 struct rio_dev *rdev;
210
211 rdev = to_rio_dev(dev);
212 kfree(rdev);
213}
214
215/**
216 * rio_is_switch- Tests if a RIO device has switch capabilities
217 * @rdev: RIO device
218 *
219 * Gets the RIO device Processing Element Features register
220 * contents and tests for switch capabilities. Returns 1 if
221 * the device is a switch or 0 if it is not a switch.
222 * The RIO device struct is freed.
223 */
224static int rio_is_switch(struct rio_dev *rdev)
225{
226 if (rdev->pef & RIO_PEF_SWITCH)
227 return 1;
228 return 0;
229}
230
231/**
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700232 * rio_switch_init - Sets switch operations for a particular vendor switch
Matt Portereb188d02005-11-07 01:00:17 -0800233 * @rdev: RIO device
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700234 * @do_enum: Enumeration/Discovery mode flag
Matt Portereb188d02005-11-07 01:00:17 -0800235 *
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700236 * Searches the RIO switch ops table for known switch types. If the vid
237 * and did match a switch table entry, then call switch initialization
238 * routine to setup switch-specific routines.
Matt Portereb188d02005-11-07 01:00:17 -0800239 */
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700240static void rio_switch_init(struct rio_dev *rdev, int do_enum)
Matt Portereb188d02005-11-07 01:00:17 -0800241{
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700242 struct rio_switch_ops *cur = __start_rio_switch_ops;
243 struct rio_switch_ops *end = __end_rio_switch_ops;
Matt Portereb188d02005-11-07 01:00:17 -0800244
245 while (cur < end) {
246 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700247 pr_debug("RIO: calling init routine for %s\n",
248 rio_name(rdev));
249 cur->init_hook(rdev, do_enum);
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700250 break;
Matt Portereb188d02005-11-07 01:00:17 -0800251 }
252 cur++;
253 }
254
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700255 if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
256 pr_debug("RIO: adding STD routing ops for %s\n",
257 rio_name(rdev));
258 rdev->rswitch->add_entry = rio_std_route_add_entry;
259 rdev->rswitch->get_entry = rio_std_route_get_entry;
260 rdev->rswitch->clr_table = rio_std_route_clr_table;
261 }
262
Matt Portereb188d02005-11-07 01:00:17 -0800263 if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
264 printk(KERN_ERR "RIO: missing routing ops for %s\n",
265 rio_name(rdev));
266}
267
268/**
269 * rio_add_device- Adds a RIO device to the device model
270 * @rdev: RIO device
271 *
272 * Adds the RIO device to the global device list and adds the RIO
273 * device to the RIO device list. Creates the generic sysfs nodes
274 * for an RIO device.
275 */
Yang Li5f28c522009-05-11 22:36:02 +0000276static int __devinit rio_add_device(struct rio_dev *rdev)
Matt Portereb188d02005-11-07 01:00:17 -0800277{
Yang Li5f28c522009-05-11 22:36:02 +0000278 int err;
279
280 err = device_add(&rdev->dev);
281 if (err)
282 return err;
Matt Portereb188d02005-11-07 01:00:17 -0800283
284 spin_lock(&rio_global_list_lock);
285 list_add_tail(&rdev->global_list, &rio_devices);
286 spin_unlock(&rio_global_list_lock);
287
288 rio_create_sysfs_dev_files(rdev);
Yang Li5f28c522009-05-11 22:36:02 +0000289
290 return 0;
Matt Portereb188d02005-11-07 01:00:17 -0800291}
292
293/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300294 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
Thomas Moll933af4a2010-05-26 14:44:01 -0700295 * given port
296 * @port: Master port associated with the RIO network
297 * @local: local=1 select local port otherwise a far device is reached
298 * @destid: Destination ID of the device to check host bit
299 * @hopcount: Number of hops to reach the target
300 * @port_num: Port (-number on switch) to enable on a far end device
301 *
302 * Returns 0 or 1 from on General Control Command and Status Register
303 * (EXT_PTR+0x3C)
304 */
305inline int rio_enable_rx_tx_port(struct rio_mport *port,
306 int local, u16 destid,
307 u8 hopcount, u8 port_num) {
308#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
309 u32 regval;
310 u32 ext_ftr_ptr;
311
312 /*
313 * enable rx input tx output port
314 */
315 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
316 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
317
318 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
319
320 if (local) {
321 rio_local_read_config_32(port, ext_ftr_ptr +
322 RIO_PORT_N_CTL_CSR(0),
323 &regval);
324 } else {
325 if (rio_mport_read_config_32(port, destid, hopcount,
326 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
327 return -EIO;
328 }
329
330 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
331 /* serial */
332 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
333 | RIO_PORT_N_CTL_EN_TX_SER;
334 } else {
335 /* parallel */
336 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
337 | RIO_PORT_N_CTL_EN_TX_PAR;
338 }
339
340 if (local) {
341 rio_local_write_config_32(port, ext_ftr_ptr +
342 RIO_PORT_N_CTL_CSR(0), regval);
343 } else {
344 if (rio_mport_write_config_32(port, destid, hopcount,
345 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
346 return -EIO;
347 }
348#endif
349 return 0;
350}
351
352/**
Matt Portereb188d02005-11-07 01:00:17 -0800353 * rio_setup_device- Allocates and sets up a RIO device
354 * @net: RIO network
355 * @port: Master port to send transactions
356 * @destid: Current destination ID
357 * @hopcount: Current hopcount
358 * @do_enum: Enumeration/Discovery mode flag
359 *
360 * Allocates a RIO device and configures fields based on configuration
361 * space contents. If device has a destination ID register, a destination
362 * ID is either assigned in enumeration mode or read from configuration
363 * space in discovery mode. If the device has switch capabilities, then
364 * a switch is allocated and configured appropriately. Returns a pointer
365 * to a RIO device on success or NULL on failure.
366 *
367 */
Li Yang181a6ff2009-05-12 16:36:03 +0800368static struct rio_dev __devinit *rio_setup_device(struct rio_net *net,
Matt Portereb188d02005-11-07 01:00:17 -0800369 struct rio_mport *port, u16 destid,
370 u8 hopcount, int do_enum)
371{
Yang Li5f28c522009-05-11 22:36:02 +0000372 int ret = 0;
Matt Portereb188d02005-11-07 01:00:17 -0800373 struct rio_dev *rdev;
Yang Li5f28c522009-05-11 22:36:02 +0000374 struct rio_switch *rswitch = NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800375 int result, rdid;
Alexandre Bounineded05782011-01-12 17:00:39 -0800376 size_t size;
377 u32 swpinfo = 0;
Matt Portereb188d02005-11-07 01:00:17 -0800378
Alexandre Bounineded05782011-01-12 17:00:39 -0800379 size = sizeof(struct rio_dev);
380 if (rio_mport_read_config_32(port, destid, hopcount,
381 RIO_PEF_CAR, &result))
382 return NULL;
383
384 if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
385 rio_mport_read_config_32(port, destid, hopcount,
386 RIO_SWP_INFO_CAR, &swpinfo);
387 if (result & RIO_PEF_SWITCH) {
388 size += (RIO_GET_TOTAL_PORTS(swpinfo) *
389 sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
390 }
391 }
392
393 rdev = kzalloc(size, GFP_KERNEL);
Matt Portereb188d02005-11-07 01:00:17 -0800394 if (!rdev)
Yang Li5f28c522009-05-11 22:36:02 +0000395 return NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800396
Matt Portereb188d02005-11-07 01:00:17 -0800397 rdev->net = net;
Alexandre Bounineded05782011-01-12 17:00:39 -0800398 rdev->pef = result;
399 rdev->swpinfo = swpinfo;
Matt Portereb188d02005-11-07 01:00:17 -0800400 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
401 &result);
402 rdev->did = result >> 16;
403 rdev->vid = result & 0xffff;
404 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
405 &rdev->device_rev);
406 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
407 &result);
408 rdev->asm_did = result >> 16;
409 rdev->asm_vid = result & 0xffff;
410 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
411 &result);
412 rdev->asm_rev = result >> 16;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700413 if (rdev->pef & RIO_PEF_EXT_FEATURES) {
Matt Portereb188d02005-11-07 01:00:17 -0800414 rdev->efptr = result & 0xffff;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700415 rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
416 hopcount);
417
418 rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
419 hopcount, RIO_EFB_ERR_MGMNT);
420 }
Matt Portereb188d02005-11-07 01:00:17 -0800421
422 rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
423 &rdev->src_ops);
424 rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
425 &rdev->dst_ops);
426
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700427 if (do_enum) {
428 /* Assign component tag to device */
429 if (next_comptag >= 0x10000) {
430 pr_err("RIO: Component Tag Counter Overflow\n");
431 goto cleanup;
432 }
433 rio_mport_write_config_32(port, destid, hopcount,
434 RIO_COMPONENT_TAG_CSR, next_comptag);
435 rdev->comp_tag = next_comptag++;
Alexandre Bounine558bda62011-01-12 17:00:42 -0800436 } else {
437 rio_mport_read_config_32(port, destid, hopcount,
438 RIO_COMPONENT_TAG_CSR,
439 &rdev->comp_tag);
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700440 }
441
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800442 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
443 if (do_enum) {
444 rio_set_device_id(port, destid, hopcount, next_destid);
445 rdev->destid = next_destid++;
446 if (next_destid == port->host_deviceid)
447 next_destid++;
448 } else
449 rdev->destid = rio_get_device_id(port, destid, hopcount);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800450
451 rdev->hopcount = 0xff;
452 } else {
453 /* Switch device has an associated destID which
454 * will be adjusted later
455 */
456 rdev->destid = destid;
457 rdev->hopcount = hopcount;
458 }
Matt Portereb188d02005-11-07 01:00:17 -0800459
460 /* If a PE has both switch and other functions, show it as a switch */
461 if (rio_is_switch(rdev)) {
Alexandre Bounineded05782011-01-12 17:00:39 -0800462 rswitch = rdev->rswitch;
Alexandre Bounine558bda62011-01-12 17:00:42 -0800463 rswitch->switchid = rdev->comp_tag & RIO_CTAG_UDEVID;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700464 rswitch->port_ok = 0;
Zhang Weie0423232008-04-18 13:33:42 -0700465 rswitch->route_table = kzalloc(sizeof(u8)*
466 RIO_MAX_ROUTE_ENTRIES(port->sys_size),
467 GFP_KERNEL);
Yang Li5f28c522009-05-11 22:36:02 +0000468 if (!rswitch->route_table)
469 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800470 /* Initialize switch route table */
Zhang Weie0423232008-04-18 13:33:42 -0700471 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
472 rdid++)
Matt Portereb188d02005-11-07 01:00:17 -0800473 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
Kay Sieversb53c75832008-12-04 10:01:52 -0800474 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
Alexandre Bounineded05782011-01-12 17:00:39 -0800475 rswitch->switchid);
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700476 rio_switch_init(rdev, do_enum);
Matt Portereb188d02005-11-07 01:00:17 -0800477
Alexandre Bounineded05782011-01-12 17:00:39 -0800478 if (do_enum && rswitch->clr_table)
479 rswitch->clr_table(port, destid, hopcount,
480 RIO_GLOBAL_TABLE);
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700481
Matt Portereb188d02005-11-07 01:00:17 -0800482 list_add_tail(&rswitch->node, &rio_switches);
483
Thomas Moll933af4a2010-05-26 14:44:01 -0700484 } else {
485 if (do_enum)
486 /*Enable Input Output Port (transmitter reviever)*/
487 rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
488
Kay Sieversb53c75832008-12-04 10:01:52 -0800489 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
490 rdev->destid);
Thomas Moll933af4a2010-05-26 14:44:01 -0700491 }
Matt Portereb188d02005-11-07 01:00:17 -0800492
493 rdev->dev.bus = &rio_bus_type;
Alexandre Bounine2c70f022010-10-27 15:34:26 -0700494 rdev->dev.parent = &rio_bus;
Matt Portereb188d02005-11-07 01:00:17 -0800495
496 device_initialize(&rdev->dev);
497 rdev->dev.release = rio_release_dev;
498 rio_dev_get(rdev);
499
Yang Hongyang284901a2009-04-06 19:01:15 -0700500 rdev->dma_mask = DMA_BIT_MASK(32);
Matt Porterfa78cc52005-11-07 01:00:18 -0800501 rdev->dev.dma_mask = &rdev->dma_mask;
Yang Hongyang284901a2009-04-06 19:01:15 -0700502 rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Matt Portereb188d02005-11-07 01:00:17 -0800503
Alexandre Bounine284fb682011-08-25 15:59:13 -0700504 if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
Matt Portereb188d02005-11-07 01:00:17 -0800505 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
506 0, 0xffff);
507
Yang Li5f28c522009-05-11 22:36:02 +0000508 ret = rio_add_device(rdev);
509 if (ret)
510 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800511
Matt Portereb188d02005-11-07 01:00:17 -0800512 return rdev;
Yang Li5f28c522009-05-11 22:36:02 +0000513
514cleanup:
Alexandre Bounine166c0502011-11-02 13:39:11 -0700515 if (rswitch)
Yang Li5f28c522009-05-11 22:36:02 +0000516 kfree(rswitch->route_table);
Alexandre Bounineded05782011-01-12 17:00:39 -0800517
Yang Li5f28c522009-05-11 22:36:02 +0000518 kfree(rdev);
519 return NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800520}
521
522/**
523 * rio_sport_is_active- Tests if a switch port has an active connection.
524 * @port: Master port to send transaction
525 * @destid: Associated destination ID for switch
526 * @hopcount: Hopcount to reach switch
527 * @sport: Switch port number
528 *
529 * Reads the port error status CSR for a particular switch port to
530 * determine if the port has an active link. Returns
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700531 * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
Matt Portereb188d02005-11-07 01:00:17 -0800532 * inactive.
533 */
534static int
535rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
536{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700537 u32 result = 0;
Matt Portereb188d02005-11-07 01:00:17 -0800538 u32 ext_ftr_ptr;
539
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700540 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800541
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700542 while (ext_ftr_ptr) {
543 rio_mport_read_config_32(port, destid, hopcount,
544 ext_ftr_ptr, &result);
545 result = RIO_GET_BLOCK_ID(result);
546 if ((result == RIO_EFB_SER_EP_FREE_ID) ||
547 (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
548 (result == RIO_EFB_SER_EP_FREC_ID))
Matt Portereb188d02005-11-07 01:00:17 -0800549 break;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700550
551 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
552 ext_ftr_ptr);
553 }
Matt Portereb188d02005-11-07 01:00:17 -0800554
555 if (ext_ftr_ptr)
556 rio_mport_read_config_32(port, destid, hopcount,
557 ext_ftr_ptr +
558 RIO_PORT_N_ERR_STS_CSR(sport),
559 &result);
560
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700561 return result & RIO_PORT_N_ERR_STS_PORT_OK;
Matt Portereb188d02005-11-07 01:00:17 -0800562}
563
564/**
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700565 * rio_lock_device - Acquires host device lock for specified device
566 * @port: Master port to send transaction
567 * @destid: Destination ID for device/switch
568 * @hopcount: Hopcount to reach switch
569 * @wait_ms: Max wait time in msec (0 = no timeout)
570 *
571 * Attepts to acquire host device lock for specified device
572 * Returns 0 if device lock acquired or EINVAL if timeout expires.
573 */
574static int
575rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
576{
577 u32 result;
578 int tcnt = 0;
579
580 /* Attempt to acquire device lock */
581 rio_mport_write_config_32(port, destid, hopcount,
582 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
583 rio_mport_read_config_32(port, destid, hopcount,
584 RIO_HOST_DID_LOCK_CSR, &result);
585
586 while (result != port->host_deviceid) {
587 if (wait_ms != 0 && tcnt == wait_ms) {
588 pr_debug("RIO: timeout when locking device %x:%x\n",
589 destid, hopcount);
590 return -EINVAL;
591 }
592
593 /* Delay a bit */
594 mdelay(1);
595 tcnt++;
596 /* Try to acquire device lock again */
597 rio_mport_write_config_32(port, destid,
598 hopcount,
599 RIO_HOST_DID_LOCK_CSR,
600 port->host_deviceid);
601 rio_mport_read_config_32(port, destid,
602 hopcount,
603 RIO_HOST_DID_LOCK_CSR, &result);
604 }
605
606 return 0;
607}
608
609/**
610 * rio_unlock_device - Releases host device lock for specified device
611 * @port: Master port to send transaction
612 * @destid: Destination ID for device/switch
613 * @hopcount: Hopcount to reach switch
614 *
615 * Returns 0 if device lock released or EINVAL if fails.
616 */
617static int
618rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
619{
620 u32 result;
621
622 /* Release device lock */
623 rio_mport_write_config_32(port, destid,
624 hopcount,
625 RIO_HOST_DID_LOCK_CSR,
626 port->host_deviceid);
627 rio_mport_read_config_32(port, destid, hopcount,
628 RIO_HOST_DID_LOCK_CSR, &result);
629 if ((result & 0xffff) != 0xffff) {
630 pr_debug("RIO: badness when releasing device lock %x:%x\n",
631 destid, hopcount);
632 return -EINVAL;
633 }
634
635 return 0;
636}
637
638/**
Matt Portereb188d02005-11-07 01:00:17 -0800639 * rio_route_add_entry- Add a route entry to a switch routing table
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800640 * @rdev: RIO device
Matt Portereb188d02005-11-07 01:00:17 -0800641 * @table: Routing table ID
642 * @route_destid: Destination ID to be routed
643 * @route_port: Port number to be routed
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700644 * @lock: lock switch device flag
Matt Portereb188d02005-11-07 01:00:17 -0800645 *
646 * Calls the switch specific add_entry() method to add a route entry
647 * on a switch. The route table can be specified using the @table
648 * argument if a switch has per port routing tables or the normal
649 * use is to specific all tables (or the global table) by passing
650 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
651 * on failure.
652 */
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700653static int
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800654rio_route_add_entry(struct rio_dev *rdev,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700655 u16 table, u16 route_destid, u8 route_port, int lock)
Matt Portereb188d02005-11-07 01:00:17 -0800656{
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700657 int rc;
658
659 if (lock) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800660 rc = rio_lock_device(rdev->net->hport, rdev->destid,
661 rdev->hopcount, 1000);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700662 if (rc)
663 return rc;
664 }
665
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800666 rc = rdev->rswitch->add_entry(rdev->net->hport, rdev->destid,
667 rdev->hopcount, table,
668 route_destid, route_port);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700669 if (lock)
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800670 rio_unlock_device(rdev->net->hport, rdev->destid,
671 rdev->hopcount);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700672
673 return rc;
Matt Portereb188d02005-11-07 01:00:17 -0800674}
675
676/**
677 * rio_route_get_entry- Read a route entry in a switch routing table
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800678 * @rdev: RIO device
Matt Portereb188d02005-11-07 01:00:17 -0800679 * @table: Routing table ID
680 * @route_destid: Destination ID to be routed
681 * @route_port: Pointer to read port number into
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700682 * @lock: lock switch device flag
Matt Portereb188d02005-11-07 01:00:17 -0800683 *
684 * Calls the switch specific get_entry() method to read a route entry
685 * in a switch. The route table can be specified using the @table
686 * argument if a switch has per port routing tables or the normal
687 * use is to specific all tables (or the global table) by passing
688 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
689 * on failure.
690 */
691static int
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800692rio_route_get_entry(struct rio_dev *rdev, u16 table,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700693 u16 route_destid, u8 *route_port, int lock)
Matt Portereb188d02005-11-07 01:00:17 -0800694{
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700695 int rc;
696
697 if (lock) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800698 rc = rio_lock_device(rdev->net->hport, rdev->destid,
699 rdev->hopcount, 1000);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700700 if (rc)
701 return rc;
702 }
703
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800704 rc = rdev->rswitch->get_entry(rdev->net->hport, rdev->destid,
705 rdev->hopcount, table,
706 route_destid, route_port);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700707 if (lock)
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800708 rio_unlock_device(rdev->net->hport, rdev->destid,
709 rdev->hopcount);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700710
711 return rc;
Matt Portereb188d02005-11-07 01:00:17 -0800712}
713
714/**
715 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
716 * @port: Master port to send transaction
717 * @hopcount: Number of hops to the device
718 *
719 * Used during enumeration to read the Host Device ID Lock CSR on a
720 * RIO device. Returns the value of the lock register.
721 */
722static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
723{
724 u32 result;
725
Zhang Weie0423232008-04-18 13:33:42 -0700726 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800727 RIO_HOST_DID_LOCK_CSR, &result);
728
729 return (u16) (result & 0xffff);
730}
731
732/**
Matt Portereb188d02005-11-07 01:00:17 -0800733 * rio_enum_peer- Recursively enumerate a RIO network through a master port
734 * @net: RIO network being enumerated
735 * @port: Master port to send transactions
736 * @hopcount: Number of hops into the network
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700737 * @prev: Previous RIO device connected to the enumerated one
738 * @prev_port: Port on previous RIO device
Matt Portereb188d02005-11-07 01:00:17 -0800739 *
740 * Recursively enumerates a RIO network. Transactions are sent via the
741 * master port passed in @port.
742 */
Li Yang181a6ff2009-05-12 16:36:03 +0800743static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700744 u8 hopcount, struct rio_dev *prev, int prev_port)
Matt Portereb188d02005-11-07 01:00:17 -0800745{
746 int port_num;
Matt Portereb188d02005-11-07 01:00:17 -0800747 int cur_destid;
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800748 int sw_destid;
749 int sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800750 struct rio_dev *rdev;
751 u16 destid;
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700752 u32 regval;
Matt Portereb188d02005-11-07 01:00:17 -0800753 int tmp;
754
Alexandre Bouninee274e0e2010-10-27 15:34:32 -0700755 if (rio_mport_chk_dev_access(port,
756 RIO_ANY_DESTID(port->sys_size), hopcount)) {
757 pr_debug("RIO: device access check failed\n");
758 return -1;
759 }
760
Matt Portereb188d02005-11-07 01:00:17 -0800761 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
762 pr_debug("RIO: PE already discovered by this host\n");
763 /*
764 * Already discovered by this host. Add it as another
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700765 * link to the existing device.
Matt Portereb188d02005-11-07 01:00:17 -0800766 */
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700767 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
768 hopcount, RIO_COMPONENT_TAG_CSR, &regval);
769
770 if (regval) {
771 rdev = rio_get_comptag((regval & 0xffff), NULL);
772
773 if (rdev && prev && rio_is_switch(prev)) {
774 pr_debug("RIO: redundant path to %s\n",
775 rio_name(rdev));
776 prev->rswitch->nextdev[prev_port] = rdev;
777 }
778 }
779
Matt Portereb188d02005-11-07 01:00:17 -0800780 return 0;
781 }
782
783 /* Attempt to acquire device lock */
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, port->host_deviceid);
787 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
788 < port->host_deviceid) {
789 /* Delay a bit */
790 mdelay(1);
791 /* Attempt to acquire device lock again */
Zhang Weie0423232008-04-18 13:33:42 -0700792 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
793 hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800794 RIO_HOST_DID_LOCK_CSR,
795 port->host_deviceid);
796 }
797
798 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
799 pr_debug(
800 "RIO: PE locked by a higher priority host...retreating\n");
801 return -1;
802 }
803
804 /* Setup new RIO device */
Zhang Weie0423232008-04-18 13:33:42 -0700805 rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
806 hopcount, 1);
807 if (rdev) {
Matt Portereb188d02005-11-07 01:00:17 -0800808 /* Add device to the global and bus/net specific list. */
809 list_add_tail(&rdev->net_list, &net->devices);
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700810 rdev->prev = prev;
811 if (prev && rio_is_switch(prev))
812 prev->rswitch->nextdev[prev_port] = rdev;
Matt Portereb188d02005-11-07 01:00:17 -0800813 } else
814 return -1;
815
816 if (rio_is_switch(rdev)) {
Alexandre Bounineae05cbd2010-10-27 15:34:29 -0700817 sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800818 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700819 port->host_deviceid, sw_inport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800820 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800821
822 for (destid = 0; destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800823 if (destid == port->host_deviceid)
824 continue;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800825 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700826 destid, sw_inport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800827 rdev->rswitch->route_table[destid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800828 }
829
Matt Portereb188d02005-11-07 01:00:17 -0800830 pr_debug(
831 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700832 rio_name(rdev), rdev->vid, rdev->did,
833 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800834 sw_destid = next_destid;
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700835 for (port_num = 0;
836 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
837 port_num++) {
Alexandre Bounine8d4630d2012-10-04 17:16:02 -0700838 if (sw_inport == port_num) {
839 rio_enable_rx_tx_port(port, 0,
Thomas Moll933af4a2010-05-26 14:44:01 -0700840 RIO_ANY_DESTID(port->sys_size),
841 hopcount, port_num);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700842 rdev->rswitch->port_ok |= (1 << port_num);
Matt Portereb188d02005-11-07 01:00:17 -0800843 continue;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700844 }
Matt Portereb188d02005-11-07 01:00:17 -0800845
846 cur_destid = next_destid;
847
848 if (rio_sport_is_active
Zhang Weie0423232008-04-18 13:33:42 -0700849 (port, RIO_ANY_DESTID(port->sys_size), hopcount,
850 port_num)) {
Matt Portereb188d02005-11-07 01:00:17 -0800851 pr_debug(
852 "RIO: scanning device on port %d\n",
853 port_num);
Alexandre Bounine8d4630d2012-10-04 17:16:02 -0700854 rio_enable_rx_tx_port(port, 0,
855 RIO_ANY_DESTID(port->sys_size),
856 hopcount, port_num);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700857 rdev->rswitch->port_ok |= (1 << port_num);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800858 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
Zhang Weie0423232008-04-18 13:33:42 -0700859 RIO_ANY_DESTID(port->sys_size),
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700860 port_num, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800861
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700862 if (rio_enum_peer(net, port, hopcount + 1,
863 rdev, port_num) < 0)
Matt Portereb188d02005-11-07 01:00:17 -0800864 return -1;
865
866 /* Update routing tables */
867 if (next_destid > cur_destid) {
868 for (destid = cur_destid;
869 destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800870 if (destid == port->host_deviceid)
871 continue;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800872 rio_route_add_entry(rdev,
Matt Portereb188d02005-11-07 01:00:17 -0800873 RIO_GLOBAL_TABLE,
874 destid,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700875 port_num,
876 0);
Matt Portereb188d02005-11-07 01:00:17 -0800877 rdev->rswitch->
878 route_table[destid] =
879 port_num;
880 }
Matt Portereb188d02005-11-07 01:00:17 -0800881 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700882 } else {
883 /* If switch supports Error Management,
884 * set PORT_LOCKOUT bit for unused port
885 */
886 if (rdev->em_efptr)
887 rio_set_port_lockout(rdev, port_num, 1);
888
889 rdev->rswitch->port_ok &= ~(1 << port_num);
Matt Portereb188d02005-11-07 01:00:17 -0800890 }
891 }
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800892
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700893 /* Direct Port-write messages to the enumeratiing host */
894 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
895 (rdev->em_efptr)) {
896 rio_write_config_32(rdev,
897 rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
898 (port->host_deviceid << 16) |
899 (port->sys_size << 15));
900 }
901
902 rio_init_em(rdev);
903
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800904 /* Check for empty switch */
905 if (next_destid == sw_destid) {
906 next_destid++;
907 if (next_destid == port->host_deviceid)
908 next_destid++;
909 }
910
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800911 rdev->destid = sw_destid;
Matt Portereb188d02005-11-07 01:00:17 -0800912 } else
913 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
914 rio_name(rdev), rdev->vid, rdev->did);
915
916 return 0;
917}
918
919/**
920 * rio_enum_complete- Tests if enumeration of a network is complete
921 * @port: Master port to send transaction
922 *
Liu Ganga5712592011-11-02 13:39:05 -0700923 * Tests the PGCCSR discovered bit for non-zero value (enumeration
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700924 * complete flag). Return %1 if enumeration is complete or %0 if
Matt Portereb188d02005-11-07 01:00:17 -0800925 * enumeration is incomplete.
926 */
927static int rio_enum_complete(struct rio_mport *port)
928{
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700929 u32 regval;
Matt Portereb188d02005-11-07 01:00:17 -0800930
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700931 rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
932 &regval);
Liu Ganga5712592011-11-02 13:39:05 -0700933 return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
Matt Portereb188d02005-11-07 01:00:17 -0800934}
935
936/**
937 * rio_disc_peer- Recursively discovers a RIO network through a master port
938 * @net: RIO network being discovered
939 * @port: Master port to send transactions
940 * @destid: Current destination ID in network
941 * @hopcount: Number of hops into the network
Randy Dunlap9b310ac2011-01-22 20:16:12 -0800942 * @prev: previous rio_dev
943 * @prev_port: previous port number
Matt Portereb188d02005-11-07 01:00:17 -0800944 *
945 * Recursively discovers a RIO network. Transactions are sent via the
946 * master port passed in @port.
947 */
Li Yang181a6ff2009-05-12 16:36:03 +0800948static int __devinit
Matt Portereb188d02005-11-07 01:00:17 -0800949rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
Alexandre Bounine17e96202011-01-12 17:00:41 -0800950 u8 hopcount, struct rio_dev *prev, int prev_port)
Matt Portereb188d02005-11-07 01:00:17 -0800951{
952 u8 port_num, route_port;
Matt Portereb188d02005-11-07 01:00:17 -0800953 struct rio_dev *rdev;
954 u16 ndestid;
955
956 /* Setup new RIO device */
957 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
958 /* Add device to the global and bus/net specific list. */
959 list_add_tail(&rdev->net_list, &net->devices);
Alexandre Bounine17e96202011-01-12 17:00:41 -0800960 rdev->prev = prev;
961 if (prev && rio_is_switch(prev))
962 prev->rswitch->nextdev[prev_port] = rdev;
Matt Portereb188d02005-11-07 01:00:17 -0800963 } else
964 return -1;
965
966 if (rio_is_switch(rdev)) {
Matt Portereb188d02005-11-07 01:00:17 -0800967 /* Associated destid is how we accessed this switch */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800968 rdev->destid = destid;
Matt Portereb188d02005-11-07 01:00:17 -0800969
Matt Portereb188d02005-11-07 01:00:17 -0800970 pr_debug(
971 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700972 rio_name(rdev), rdev->vid, rdev->did,
973 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
974 for (port_num = 0;
975 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
976 port_num++) {
Alexandre Bounineae05cbd2010-10-27 15:34:29 -0700977 if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
Matt Portereb188d02005-11-07 01:00:17 -0800978 continue;
979
980 if (rio_sport_is_active
981 (port, destid, hopcount, port_num)) {
982 pr_debug(
983 "RIO: scanning device on port %d\n",
984 port_num);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700985
986 rio_lock_device(port, destid, hopcount, 1000);
987
Zhang Weie0423232008-04-18 13:33:42 -0700988 for (ndestid = 0;
989 ndestid < RIO_ANY_DESTID(port->sys_size);
Matt Portereb188d02005-11-07 01:00:17 -0800990 ndestid++) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800991 rio_route_get_entry(rdev,
Matt Portereb188d02005-11-07 01:00:17 -0800992 RIO_GLOBAL_TABLE,
993 ndestid,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700994 &route_port, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800995 if (route_port == port_num)
996 break;
997 }
998
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700999 if (ndestid == RIO_ANY_DESTID(port->sys_size))
1000 continue;
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001001 rio_unlock_device(port, destid, hopcount);
Alexandre Bounine17e96202011-01-12 17:00:41 -08001002 if (rio_disc_peer(net, port, ndestid,
1003 hopcount + 1, rdev, port_num) < 0)
Matt Portereb188d02005-11-07 01:00:17 -08001004 return -1;
1005 }
1006 }
1007 } else
1008 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
1009 rio_name(rdev), rdev->vid, rdev->did);
1010
1011 return 0;
1012}
1013
1014/**
1015 * rio_mport_is_active- Tests if master port link is active
1016 * @port: Master port to test
1017 *
1018 * Reads the port error status CSR for the master port to
1019 * determine if the port has an active link. Returns
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001020 * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
Matt Portereb188d02005-11-07 01:00:17 -08001021 * or %0 if it is inactive.
1022 */
1023static int rio_mport_is_active(struct rio_mport *port)
1024{
1025 u32 result = 0;
1026 u32 ext_ftr_ptr;
1027 int *entry = rio_mport_phys_table;
1028
1029 do {
1030 if ((ext_ftr_ptr =
1031 rio_mport_get_feature(port, 1, 0, 0, *entry)))
1032 break;
1033 } while (*++entry >= 0);
1034
1035 if (ext_ftr_ptr)
1036 rio_local_read_config_32(port,
1037 ext_ftr_ptr +
1038 RIO_PORT_N_ERR_STS_CSR(port->index),
1039 &result);
1040
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001041 return result & RIO_PORT_N_ERR_STS_PORT_OK;
Matt Portereb188d02005-11-07 01:00:17 -08001042}
1043
1044/**
1045 * rio_alloc_net- Allocate and configure a new RIO network
1046 * @port: Master port associated with the RIO network
1047 *
1048 * Allocates a RIO network structure, initializes per-network
1049 * list heads, and adds the associated master port to the
1050 * network list of associated master ports. Returns a
1051 * RIO network pointer on success or %NULL on failure.
1052 */
1053static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
1054{
1055 struct rio_net *net;
1056
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001057 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
Matt Portereb188d02005-11-07 01:00:17 -08001058 if (net) {
Matt Portereb188d02005-11-07 01:00:17 -08001059 INIT_LIST_HEAD(&net->node);
1060 INIT_LIST_HEAD(&net->devices);
1061 INIT_LIST_HEAD(&net->mports);
1062 list_add_tail(&port->nnode, &net->mports);
1063 net->hport = port;
1064 net->id = next_net++;
1065 }
1066 return net;
1067}
1068
1069/**
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001070 * rio_update_route_tables- Updates route tables in switches
1071 * @port: Master port associated with the RIO network
1072 *
1073 * For each enumerated device, ensure that each switch in a system
1074 * has correct routing entries. Add routes for devices that where
1075 * unknown dirung the first enumeration pass through the switch.
1076 */
1077static void rio_update_route_tables(struct rio_mport *port)
1078{
Alexandre Bounineded05782011-01-12 17:00:39 -08001079 struct rio_dev *rdev, *swrdev;
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001080 struct rio_switch *rswitch;
1081 u8 sport;
1082 u16 destid;
1083
1084 list_for_each_entry(rdev, &rio_devices, global_list) {
1085
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001086 destid = rdev->destid;
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001087
1088 list_for_each_entry(rswitch, &rio_switches, node) {
1089
1090 if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
1091 continue;
1092
1093 if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
Alexandre Bounineded05782011-01-12 17:00:39 -08001094 swrdev = sw_to_rio_dev(rswitch);
1095
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001096 /* Skip if destid ends in empty switch*/
Alexandre Bounineded05782011-01-12 17:00:39 -08001097 if (swrdev->destid == destid)
Alexandre Bounine07590ff2010-05-26 14:43:57 -07001098 continue;
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001099
Alexandre Bounineded05782011-01-12 17:00:39 -08001100 sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001101
1102 if (rswitch->add_entry) {
Alexandre Bounineded05782011-01-12 17:00:39 -08001103 rio_route_add_entry(swrdev,
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001104 RIO_GLOBAL_TABLE, destid,
1105 sport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001106 rswitch->route_table[destid] = sport;
1107 }
1108 }
1109 }
1110 }
1111}
1112
1113/**
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001114 * rio_init_em - Initializes RIO Error Management (for switches)
Randy Dunlap97ef6f72010-05-28 15:08:08 -07001115 * @rdev: RIO device
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001116 *
1117 * For each enumerated switch, call device-specific error management
1118 * initialization routine (if supplied by the switch driver).
1119 */
1120static void rio_init_em(struct rio_dev *rdev)
1121{
1122 if (rio_is_switch(rdev) && (rdev->em_efptr) &&
1123 (rdev->rswitch->em_init)) {
1124 rdev->rswitch->em_init(rdev);
1125 }
1126}
1127
1128/**
1129 * rio_pw_enable - Enables/disables port-write handling by a master port
1130 * @port: Master port associated with port-write handling
1131 * @enable: 1=enable, 0=disable
1132 */
1133static void rio_pw_enable(struct rio_mport *port, int enable)
1134{
1135 if (port->ops->pwenable)
1136 port->ops->pwenable(port, enable);
1137}
1138
1139/**
Matt Portereb188d02005-11-07 01:00:17 -08001140 * rio_enum_mport- Start enumeration through a master port
1141 * @mport: Master port to send transactions
1142 *
1143 * Starts the enumeration process. If somebody has enumerated our
1144 * master port device, then give up. If not and we have an active
1145 * link, then start recursive peer enumeration. Returns %0 if
1146 * enumeration succeeds or %-EBUSY if enumeration fails.
1147 */
Al Viro37d33d12008-11-22 17:36:24 +00001148int __devinit rio_enum_mport(struct rio_mport *mport)
Matt Portereb188d02005-11-07 01:00:17 -08001149{
1150 struct rio_net *net = NULL;
1151 int rc = 0;
1152
1153 printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
1154 mport->name);
1155 /* If somebody else enumerated our master port device, bail. */
1156 if (rio_enum_host(mport) < 0) {
1157 printk(KERN_INFO
1158 "RIO: master port %d device has been enumerated by a remote host\n",
1159 mport->id);
1160 rc = -EBUSY;
1161 goto out;
1162 }
1163
1164 /* If master port has an active link, allocate net and enum peers */
1165 if (rio_mport_is_active(mport)) {
1166 if (!(net = rio_alloc_net(mport))) {
1167 printk(KERN_ERR "RIO: failed to allocate new net\n");
1168 rc = -ENOMEM;
1169 goto out;
1170 }
Thomas Moll933af4a2010-05-26 14:44:01 -07001171
1172 /* Enable Input Output Port (transmitter reviever) */
1173 rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
1174
Alexandre Bounineaf84ca32010-10-27 15:34:34 -07001175 /* Set component tag for host */
1176 rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
1177 next_comptag++);
1178
Alexandre Bounine68fe4df2010-10-27 15:34:29 -07001179 if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
Matt Portereb188d02005-11-07 01:00:17 -08001180 /* A higher priority host won enumeration, bail. */
1181 printk(KERN_INFO
1182 "RIO: master port %d device has lost enumeration to a remote host\n",
1183 mport->id);
1184 rio_clear_locks(mport);
1185 rc = -EBUSY;
1186 goto out;
1187 }
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001188 rio_update_route_tables(mport);
Matt Portereb188d02005-11-07 01:00:17 -08001189 rio_clear_locks(mport);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001190 rio_pw_enable(mport, 1);
Matt Portereb188d02005-11-07 01:00:17 -08001191 } else {
1192 printk(KERN_INFO "RIO: master port %d link inactive\n",
1193 mport->id);
1194 rc = -EINVAL;
1195 }
1196
1197 out:
1198 return rc;
1199}
1200
1201/**
1202 * rio_build_route_tables- Generate route tables from switch route entries
1203 *
1204 * For each switch device, generate a route table by copying existing
1205 * route entries from the switch.
1206 */
1207static void rio_build_route_tables(void)
1208{
1209 struct rio_dev *rdev;
1210 int i;
1211 u8 sport;
1212
1213 list_for_each_entry(rdev, &rio_devices, global_list)
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001214 if (rio_is_switch(rdev)) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001215 rio_lock_device(rdev->net->hport, rdev->destid,
1216 rdev->hopcount, 1000);
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001217 for (i = 0;
1218 i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
1219 i++) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001220 if (rio_route_get_entry(rdev,
1221 RIO_GLOBAL_TABLE, i, &sport, 0) < 0)
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001222 continue;
1223 rdev->rswitch->route_table[i] = sport;
1224 }
1225
1226 rio_unlock_device(rdev->net->hport,
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001227 rdev->destid,
1228 rdev->hopcount);
Matt Portereb188d02005-11-07 01:00:17 -08001229 }
1230}
1231
1232/**
Matt Portereb188d02005-11-07 01:00:17 -08001233 * rio_disc_mport- Start discovery through a master port
1234 * @mport: Master port to send transactions
1235 *
1236 * Starts the discovery process. If we have an active link,
1237 * then wait for the signal that enumeration is complete.
1238 * When enumeration completion is signaled, start recursive
1239 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1240 * on failure.
1241 */
Al Viro37d33d12008-11-22 17:36:24 +00001242int __devinit rio_disc_mport(struct rio_mport *mport)
Matt Portereb188d02005-11-07 01:00:17 -08001243{
1244 struct rio_net *net = NULL;
Alexandre Bouninefa3dbaa2012-10-04 17:16:04 -07001245 unsigned long to_end;
Matt Portereb188d02005-11-07 01:00:17 -08001246
1247 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1248 mport->name);
1249
1250 /* If master port has an active link, allocate net and discover peers */
1251 if (rio_mport_is_active(mport)) {
Alexandre Bouninefa3dbaa2012-10-04 17:16:04 -07001252 pr_debug("RIO: wait for enumeration to complete...\n");
1253
1254 to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1255 while (time_before(jiffies, to_end)) {
1256 if (rio_enum_complete(mport))
1257 goto enum_done;
1258 schedule_timeout_uninterruptible(msecs_to_jiffies(10));
1259 }
1260
1261 pr_debug("RIO: discovery timeout on mport %d %s\n",
1262 mport->id, mport->name);
1263 goto bail;
1264enum_done:
1265 pr_debug("RIO: ... enumeration done\n");
1266
1267 net = rio_alloc_net(mport);
1268 if (!net) {
Matt Portereb188d02005-11-07 01:00:17 -08001269 printk(KERN_ERR "RIO: Failed to allocate new net\n");
1270 goto bail;
1271 }
1272
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001273 /* Read DestID assigned by enumerator */
1274 rio_local_read_config_32(mport, RIO_DID_CSR,
1275 &mport->host_deviceid);
1276 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1277 mport->host_deviceid);
1278
Zhang Weie0423232008-04-18 13:33:42 -07001279 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
Alexandre Bounine17e96202011-01-12 17:00:41 -08001280 0, NULL, 0) < 0) {
Matt Portereb188d02005-11-07 01:00:17 -08001281 printk(KERN_INFO
1282 "RIO: master port %d device has failed discovery\n",
1283 mport->id);
1284 goto bail;
1285 }
1286
1287 rio_build_route_tables();
1288 }
1289
1290 return 0;
Alexandre Bouninefa3dbaa2012-10-04 17:16:04 -07001291bail:
Matt Portereb188d02005-11-07 01:00:17 -08001292 return -EBUSY;
1293}