blob: 745670f535e815870c0e1db5ba1570bb120ec83f [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);
Matt Portereb188d02005-11-07 01:00:17 -080041
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -070042static void rio_init_em(struct rio_dev *rdev);
43
Matt Porterfa78cc52005-11-07 01:00:18 -080044DEFINE_SPINLOCK(rio_global_list_lock);
45
Matt Portereb188d02005-11-07 01:00:17 -080046static int next_destid = 0;
Alexandre Bounineaf84ca32010-10-27 15:34:34 -070047static int next_comptag = 1;
Matt Portereb188d02005-11-07 01:00:17 -080048
Matt Portereb188d02005-11-07 01:00:17 -080049static int rio_mport_phys_table[] = {
50 RIO_EFB_PAR_EP_ID,
51 RIO_EFB_PAR_EP_REC_ID,
52 RIO_EFB_SER_EP_ID,
53 RIO_EFB_SER_EP_REC_ID,
54 -1,
55};
56
Matt Portereb188d02005-11-07 01:00:17 -080057/**
58 * rio_get_device_id - Get the base/extended device id for a device
59 * @port: RIO master port
60 * @destid: Destination ID of device
61 * @hopcount: Hopcount to device
62 *
63 * Reads the base/extended device id from a device. Returns the
64 * 8/16-bit device ID.
65 */
66static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
67{
68 u32 result;
69
70 rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
71
Zhang Weie0423232008-04-18 13:33:42 -070072 return RIO_GET_DID(port->sys_size, result);
Matt Portereb188d02005-11-07 01:00:17 -080073}
74
75/**
76 * rio_set_device_id - Set the base/extended device id for a device
77 * @port: RIO master port
78 * @destid: Destination ID of device
79 * @hopcount: Hopcount to device
80 * @did: Device ID value to be written
81 *
82 * Writes the base/extended device id from a device.
83 */
Matt Porterfa78cc52005-11-07 01:00:18 -080084static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
Matt Portereb188d02005-11-07 01:00:17 -080085{
86 rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
Zhang Weie0423232008-04-18 13:33:42 -070087 RIO_SET_DID(port->sys_size, did));
Matt Portereb188d02005-11-07 01:00:17 -080088}
89
90/**
91 * rio_local_set_device_id - Set the base/extended device id for a port
92 * @port: RIO master port
93 * @did: Device ID value to be written
94 *
95 * Writes the base/extended device id from a device.
96 */
97static void rio_local_set_device_id(struct rio_mport *port, u16 did)
98{
Zhang Weie0423232008-04-18 13:33:42 -070099 rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
100 did));
Matt Portereb188d02005-11-07 01:00:17 -0800101}
102
103/**
104 * rio_clear_locks- Release all host locks and signal enumeration complete
Alexandre Bouninea7071ef2012-10-04 17:16:05 -0700105 * @net: RIO network to run on
Matt Portereb188d02005-11-07 01:00:17 -0800106 *
107 * Marks the component tag CSR on each device with the enumeration
108 * complete flag. When complete, it then release the host locks on
109 * each device. Returns 0 on success or %-EINVAL on failure.
110 */
Alexandre Bouninea7071ef2012-10-04 17:16:05 -0700111static int rio_clear_locks(struct rio_net *net)
Matt Portereb188d02005-11-07 01:00:17 -0800112{
Alexandre Bouninea7071ef2012-10-04 17:16:05 -0700113 struct rio_mport *port = net->hport;
Matt Portereb188d02005-11-07 01:00:17 -0800114 struct rio_dev *rdev;
115 u32 result;
116 int ret = 0;
117
Matt Portereb188d02005-11-07 01:00:17 -0800118 /* Release host device id locks */
119 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
120 port->host_deviceid);
121 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
122 if ((result & 0xffff) != 0xffff) {
123 printk(KERN_INFO
124 "RIO: badness when releasing host lock on master port, result %8.8x\n",
125 result);
126 ret = -EINVAL;
127 }
Alexandre Bouninea7071ef2012-10-04 17:16:05 -0700128 list_for_each_entry(rdev, &net->devices, net_list) {
Matt Portereb188d02005-11-07 01:00:17 -0800129 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
130 port->host_deviceid);
131 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
132 if ((result & 0xffff) != 0xffff) {
133 printk(KERN_INFO
134 "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
135 rdev->vid, rdev->did);
136 ret = -EINVAL;
137 }
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700138
139 /* Mark device as discovered and enable master */
140 rio_read_config_32(rdev,
141 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
142 &result);
143 result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
144 rio_write_config_32(rdev,
145 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
146 result);
Matt Portereb188d02005-11-07 01:00:17 -0800147 }
148
149 return ret;
150}
151
152/**
153 * rio_enum_host- Set host lock and initialize host destination ID
154 * @port: Master port to issue transaction
155 *
156 * Sets the local host master port lock and destination ID register
157 * with the host device ID value. The host device ID value is provided
158 * by the platform. Returns %0 on success or %-1 on failure.
159 */
160static int rio_enum_host(struct rio_mport *port)
161{
162 u32 result;
163
164 /* Set master port host device id lock */
165 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
166 port->host_deviceid);
167
168 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
169 if ((result & 0xffff) != port->host_deviceid)
170 return -1;
171
172 /* Set master port destid and init destid ctr */
173 rio_local_set_device_id(port, port->host_deviceid);
174
175 if (next_destid == port->host_deviceid)
176 next_destid++;
177
178 return 0;
179}
180
181/**
182 * rio_device_has_destid- Test if a device contains a destination ID register
183 * @port: Master port to issue transaction
184 * @src_ops: RIO device source operations
185 * @dst_ops: RIO device destination operations
186 *
187 * Checks the provided @src_ops and @dst_ops for the necessary transaction
188 * capabilities that indicate whether or not a device will implement a
189 * destination ID register. Returns 1 if true or 0 if false.
190 */
191static int rio_device_has_destid(struct rio_mport *port, int src_ops,
192 int dst_ops)
193{
Matt Porterfa78cc52005-11-07 01:00:18 -0800194 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;
195
196 return !!((src_ops | dst_ops) & mask);
Matt Portereb188d02005-11-07 01:00:17 -0800197}
198
199/**
200 * rio_release_dev- Frees a RIO device struct
201 * @dev: LDM device associated with a RIO device struct
202 *
203 * Gets the RIO device struct associated a RIO device struct.
204 * The RIO device struct is freed.
205 */
206static void rio_release_dev(struct device *dev)
207{
208 struct rio_dev *rdev;
209
210 rdev = to_rio_dev(dev);
211 kfree(rdev);
212}
213
214/**
215 * rio_is_switch- Tests if a RIO device has switch capabilities
216 * @rdev: RIO device
217 *
218 * Gets the RIO device Processing Element Features register
219 * contents and tests for switch capabilities. Returns 1 if
220 * the device is a switch or 0 if it is not a switch.
221 * The RIO device struct is freed.
222 */
223static int rio_is_switch(struct rio_dev *rdev)
224{
225 if (rdev->pef & RIO_PEF_SWITCH)
226 return 1;
227 return 0;
228}
229
230/**
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700231 * rio_switch_init - Sets switch operations for a particular vendor switch
Matt Portereb188d02005-11-07 01:00:17 -0800232 * @rdev: RIO device
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700233 * @do_enum: Enumeration/Discovery mode flag
Matt Portereb188d02005-11-07 01:00:17 -0800234 *
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700235 * Searches the RIO switch ops table for known switch types. If the vid
236 * and did match a switch table entry, then call switch initialization
237 * routine to setup switch-specific routines.
Matt Portereb188d02005-11-07 01:00:17 -0800238 */
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700239static void rio_switch_init(struct rio_dev *rdev, int do_enum)
Matt Portereb188d02005-11-07 01:00:17 -0800240{
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700241 struct rio_switch_ops *cur = __start_rio_switch_ops;
242 struct rio_switch_ops *end = __end_rio_switch_ops;
Matt Portereb188d02005-11-07 01:00:17 -0800243
244 while (cur < end) {
245 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700246 pr_debug("RIO: calling init routine for %s\n",
247 rio_name(rdev));
248 cur->init_hook(rdev, do_enum);
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700249 break;
Matt Portereb188d02005-11-07 01:00:17 -0800250 }
251 cur++;
252 }
253
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700254 if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
255 pr_debug("RIO: adding STD routing ops for %s\n",
256 rio_name(rdev));
257 rdev->rswitch->add_entry = rio_std_route_add_entry;
258 rdev->rswitch->get_entry = rio_std_route_get_entry;
259 rdev->rswitch->clr_table = rio_std_route_clr_table;
260 }
261
Matt Portereb188d02005-11-07 01:00:17 -0800262 if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
263 printk(KERN_ERR "RIO: missing routing ops for %s\n",
264 rio_name(rdev));
265}
266
267/**
268 * rio_add_device- Adds a RIO device to the device model
269 * @rdev: RIO device
270 *
271 * Adds the RIO device to the global device list and adds the RIO
272 * device to the RIO device list. Creates the generic sysfs nodes
273 * for an RIO device.
274 */
Yang Li5f28c522009-05-11 22:36:02 +0000275static int __devinit rio_add_device(struct rio_dev *rdev)
Matt Portereb188d02005-11-07 01:00:17 -0800276{
Yang Li5f28c522009-05-11 22:36:02 +0000277 int err;
278
279 err = device_add(&rdev->dev);
280 if (err)
281 return err;
Matt Portereb188d02005-11-07 01:00:17 -0800282
283 spin_lock(&rio_global_list_lock);
284 list_add_tail(&rdev->global_list, &rio_devices);
285 spin_unlock(&rio_global_list_lock);
286
287 rio_create_sysfs_dev_files(rdev);
Yang Li5f28c522009-05-11 22:36:02 +0000288
289 return 0;
Matt Portereb188d02005-11-07 01:00:17 -0800290}
291
292/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300293 * rio_enable_rx_tx_port - enable input receiver and output transmitter of
Thomas Moll933af4a2010-05-26 14:44:01 -0700294 * given port
295 * @port: Master port associated with the RIO network
296 * @local: local=1 select local port otherwise a far device is reached
297 * @destid: Destination ID of the device to check host bit
298 * @hopcount: Number of hops to reach the target
299 * @port_num: Port (-number on switch) to enable on a far end device
300 *
301 * Returns 0 or 1 from on General Control Command and Status Register
302 * (EXT_PTR+0x3C)
303 */
304inline int rio_enable_rx_tx_port(struct rio_mport *port,
305 int local, u16 destid,
306 u8 hopcount, u8 port_num) {
307#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
308 u32 regval;
309 u32 ext_ftr_ptr;
310
311 /*
312 * enable rx input tx output port
313 */
314 pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
315 "%d, port_num = %d)\n", local, destid, hopcount, port_num);
316
317 ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
318
319 if (local) {
320 rio_local_read_config_32(port, ext_ftr_ptr +
321 RIO_PORT_N_CTL_CSR(0),
322 &regval);
323 } else {
324 if (rio_mport_read_config_32(port, destid, hopcount,
325 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
326 return -EIO;
327 }
328
329 if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
330 /* serial */
331 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
332 | RIO_PORT_N_CTL_EN_TX_SER;
333 } else {
334 /* parallel */
335 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
336 | RIO_PORT_N_CTL_EN_TX_PAR;
337 }
338
339 if (local) {
340 rio_local_write_config_32(port, ext_ftr_ptr +
341 RIO_PORT_N_CTL_CSR(0), regval);
342 } else {
343 if (rio_mport_write_config_32(port, destid, hopcount,
344 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
345 return -EIO;
346 }
347#endif
348 return 0;
349}
350
351/**
Matt Portereb188d02005-11-07 01:00:17 -0800352 * rio_setup_device- Allocates and sets up a RIO device
353 * @net: RIO network
354 * @port: Master port to send transactions
355 * @destid: Current destination ID
356 * @hopcount: Current hopcount
357 * @do_enum: Enumeration/Discovery mode flag
358 *
359 * Allocates a RIO device and configures fields based on configuration
360 * space contents. If device has a destination ID register, a destination
361 * ID is either assigned in enumeration mode or read from configuration
362 * space in discovery mode. If the device has switch capabilities, then
363 * a switch is allocated and configured appropriately. Returns a pointer
364 * to a RIO device on success or NULL on failure.
365 *
366 */
Li Yang181a6ff2009-05-12 16:36:03 +0800367static struct rio_dev __devinit *rio_setup_device(struct rio_net *net,
Matt Portereb188d02005-11-07 01:00:17 -0800368 struct rio_mport *port, u16 destid,
369 u8 hopcount, int do_enum)
370{
Yang Li5f28c522009-05-11 22:36:02 +0000371 int ret = 0;
Matt Portereb188d02005-11-07 01:00:17 -0800372 struct rio_dev *rdev;
Yang Li5f28c522009-05-11 22:36:02 +0000373 struct rio_switch *rswitch = NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800374 int result, rdid;
Alexandre Bounineded05782011-01-12 17:00:39 -0800375 size_t size;
376 u32 swpinfo = 0;
Matt Portereb188d02005-11-07 01:00:17 -0800377
Alexandre Bounineded05782011-01-12 17:00:39 -0800378 size = sizeof(struct rio_dev);
379 if (rio_mport_read_config_32(port, destid, hopcount,
380 RIO_PEF_CAR, &result))
381 return NULL;
382
383 if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
384 rio_mport_read_config_32(port, destid, hopcount,
385 RIO_SWP_INFO_CAR, &swpinfo);
386 if (result & RIO_PEF_SWITCH) {
387 size += (RIO_GET_TOTAL_PORTS(swpinfo) *
388 sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
389 }
390 }
391
392 rdev = kzalloc(size, GFP_KERNEL);
Matt Portereb188d02005-11-07 01:00:17 -0800393 if (!rdev)
Yang Li5f28c522009-05-11 22:36:02 +0000394 return NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800395
Matt Portereb188d02005-11-07 01:00:17 -0800396 rdev->net = net;
Alexandre Bounineded05782011-01-12 17:00:39 -0800397 rdev->pef = result;
398 rdev->swpinfo = swpinfo;
Matt Portereb188d02005-11-07 01:00:17 -0800399 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;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700412 if (rdev->pef & RIO_PEF_EXT_FEATURES) {
Matt Portereb188d02005-11-07 01:00:17 -0800413 rdev->efptr = result & 0xffff;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700414 rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
415 hopcount);
416
417 rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
418 hopcount, RIO_EFB_ERR_MGMNT);
419 }
Matt Portereb188d02005-11-07 01:00:17 -0800420
421 rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
422 &rdev->src_ops);
423 rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
424 &rdev->dst_ops);
425
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700426 if (do_enum) {
427 /* Assign component tag to device */
428 if (next_comptag >= 0x10000) {
429 pr_err("RIO: Component Tag Counter Overflow\n");
430 goto cleanup;
431 }
432 rio_mport_write_config_32(port, destid, hopcount,
433 RIO_COMPONENT_TAG_CSR, next_comptag);
434 rdev->comp_tag = next_comptag++;
Alexandre Bounine558bda62011-01-12 17:00:42 -0800435 } else {
436 rio_mport_read_config_32(port, destid, hopcount,
437 RIO_COMPONENT_TAG_CSR,
438 &rdev->comp_tag);
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700439 }
440
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800441 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
442 if (do_enum) {
443 rio_set_device_id(port, destid, hopcount, next_destid);
444 rdev->destid = next_destid++;
445 if (next_destid == port->host_deviceid)
446 next_destid++;
447 } else
448 rdev->destid = rio_get_device_id(port, destid, hopcount);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800449
450 rdev->hopcount = 0xff;
451 } else {
452 /* Switch device has an associated destID which
453 * will be adjusted later
454 */
455 rdev->destid = destid;
456 rdev->hopcount = hopcount;
457 }
Matt Portereb188d02005-11-07 01:00:17 -0800458
459 /* If a PE has both switch and other functions, show it as a switch */
460 if (rio_is_switch(rdev)) {
Alexandre Bounineded05782011-01-12 17:00:39 -0800461 rswitch = rdev->rswitch;
Alexandre Bounine558bda62011-01-12 17:00:42 -0800462 rswitch->switchid = rdev->comp_tag & RIO_CTAG_UDEVID;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700463 rswitch->port_ok = 0;
Zhang Weie0423232008-04-18 13:33:42 -0700464 rswitch->route_table = kzalloc(sizeof(u8)*
465 RIO_MAX_ROUTE_ENTRIES(port->sys_size),
466 GFP_KERNEL);
Yang Li5f28c522009-05-11 22:36:02 +0000467 if (!rswitch->route_table)
468 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800469 /* Initialize switch route table */
Zhang Weie0423232008-04-18 13:33:42 -0700470 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
471 rdid++)
Matt Portereb188d02005-11-07 01:00:17 -0800472 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
Kay Sieversb53c75832008-12-04 10:01:52 -0800473 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
Alexandre Bounineded05782011-01-12 17:00:39 -0800474 rswitch->switchid);
Alexandre Bounine058f88d62010-05-26 14:44:03 -0700475 rio_switch_init(rdev, do_enum);
Matt Portereb188d02005-11-07 01:00:17 -0800476
Alexandre Bounineded05782011-01-12 17:00:39 -0800477 if (do_enum && rswitch->clr_table)
478 rswitch->clr_table(port, destid, hopcount,
479 RIO_GLOBAL_TABLE);
Alexandre Bounine07590ff2010-05-26 14:43:57 -0700480
Alexandre Bouninea7071ef2012-10-04 17:16:05 -0700481 list_add_tail(&rswitch->node, &net->switches);
Matt Portereb188d02005-11-07 01:00:17 -0800482
Thomas Moll933af4a2010-05-26 14:44:01 -0700483 } else {
484 if (do_enum)
485 /*Enable Input Output Port (transmitter reviever)*/
486 rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
487
Kay Sieversb53c75832008-12-04 10:01:52 -0800488 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
489 rdev->destid);
Thomas Moll933af4a2010-05-26 14:44:01 -0700490 }
Matt Portereb188d02005-11-07 01:00:17 -0800491
492 rdev->dev.bus = &rio_bus_type;
Alexandre Bounine2c70f022010-10-27 15:34:26 -0700493 rdev->dev.parent = &rio_bus;
Matt Portereb188d02005-11-07 01:00:17 -0800494
495 device_initialize(&rdev->dev);
496 rdev->dev.release = rio_release_dev;
497 rio_dev_get(rdev);
498
Yang Hongyang284901a2009-04-06 19:01:15 -0700499 rdev->dma_mask = DMA_BIT_MASK(32);
Matt Porterfa78cc52005-11-07 01:00:18 -0800500 rdev->dev.dma_mask = &rdev->dma_mask;
Yang Hongyang284901a2009-04-06 19:01:15 -0700501 rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Matt Portereb188d02005-11-07 01:00:17 -0800502
Alexandre Bounine284fb682011-08-25 15:59:13 -0700503 if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
Matt Portereb188d02005-11-07 01:00:17 -0800504 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
505 0, 0xffff);
506
Yang Li5f28c522009-05-11 22:36:02 +0000507 ret = rio_add_device(rdev);
508 if (ret)
509 goto cleanup;
Matt Portereb188d02005-11-07 01:00:17 -0800510
Matt Portereb188d02005-11-07 01:00:17 -0800511 return rdev;
Yang Li5f28c522009-05-11 22:36:02 +0000512
513cleanup:
Alexandre Bounine166c0502011-11-02 13:39:11 -0700514 if (rswitch)
Yang Li5f28c522009-05-11 22:36:02 +0000515 kfree(rswitch->route_table);
Alexandre Bounineded05782011-01-12 17:00:39 -0800516
Yang Li5f28c522009-05-11 22:36:02 +0000517 kfree(rdev);
518 return NULL;
Matt Portereb188d02005-11-07 01:00:17 -0800519}
520
521/**
522 * rio_sport_is_active- Tests if a switch port has an active connection.
523 * @port: Master port to send transaction
524 * @destid: Associated destination ID for switch
525 * @hopcount: Hopcount to reach switch
526 * @sport: Switch port number
527 *
528 * Reads the port error status CSR for a particular switch port to
529 * determine if the port has an active link. Returns
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700530 * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
Matt Portereb188d02005-11-07 01:00:17 -0800531 * inactive.
532 */
533static int
534rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
535{
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700536 u32 result = 0;
Matt Portereb188d02005-11-07 01:00:17 -0800537 u32 ext_ftr_ptr;
538
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700539 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800540
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700541 while (ext_ftr_ptr) {
542 rio_mport_read_config_32(port, destid, hopcount,
543 ext_ftr_ptr, &result);
544 result = RIO_GET_BLOCK_ID(result);
545 if ((result == RIO_EFB_SER_EP_FREE_ID) ||
546 (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
547 (result == RIO_EFB_SER_EP_FREC_ID))
Matt Portereb188d02005-11-07 01:00:17 -0800548 break;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700549
550 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
551 ext_ftr_ptr);
552 }
Matt Portereb188d02005-11-07 01:00:17 -0800553
554 if (ext_ftr_ptr)
555 rio_mport_read_config_32(port, destid, hopcount,
556 ext_ftr_ptr +
557 RIO_PORT_N_ERR_STS_CSR(sport),
558 &result);
559
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700560 return result & RIO_PORT_N_ERR_STS_PORT_OK;
Matt Portereb188d02005-11-07 01:00:17 -0800561}
562
563/**
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700564 * rio_lock_device - Acquires host device lock for specified device
565 * @port: Master port to send transaction
566 * @destid: Destination ID for device/switch
567 * @hopcount: Hopcount to reach switch
568 * @wait_ms: Max wait time in msec (0 = no timeout)
569 *
570 * Attepts to acquire host device lock for specified device
571 * Returns 0 if device lock acquired or EINVAL if timeout expires.
572 */
573static int
574rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
575{
576 u32 result;
577 int tcnt = 0;
578
579 /* Attempt to acquire device lock */
580 rio_mport_write_config_32(port, destid, hopcount,
581 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
582 rio_mport_read_config_32(port, destid, hopcount,
583 RIO_HOST_DID_LOCK_CSR, &result);
584
585 while (result != port->host_deviceid) {
586 if (wait_ms != 0 && tcnt == wait_ms) {
587 pr_debug("RIO: timeout when locking device %x:%x\n",
588 destid, hopcount);
589 return -EINVAL;
590 }
591
592 /* Delay a bit */
593 mdelay(1);
594 tcnt++;
595 /* Try to acquire device lock again */
596 rio_mport_write_config_32(port, destid,
597 hopcount,
598 RIO_HOST_DID_LOCK_CSR,
599 port->host_deviceid);
600 rio_mport_read_config_32(port, destid,
601 hopcount,
602 RIO_HOST_DID_LOCK_CSR, &result);
603 }
604
605 return 0;
606}
607
608/**
609 * rio_unlock_device - Releases host device lock for specified device
610 * @port: Master port to send transaction
611 * @destid: Destination ID for device/switch
612 * @hopcount: Hopcount to reach switch
613 *
614 * Returns 0 if device lock released or EINVAL if fails.
615 */
616static int
617rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
618{
619 u32 result;
620
621 /* Release device lock */
622 rio_mport_write_config_32(port, destid,
623 hopcount,
624 RIO_HOST_DID_LOCK_CSR,
625 port->host_deviceid);
626 rio_mport_read_config_32(port, destid, hopcount,
627 RIO_HOST_DID_LOCK_CSR, &result);
628 if ((result & 0xffff) != 0xffff) {
629 pr_debug("RIO: badness when releasing device lock %x:%x\n",
630 destid, hopcount);
631 return -EINVAL;
632 }
633
634 return 0;
635}
636
637/**
Matt Portereb188d02005-11-07 01:00:17 -0800638 * rio_route_add_entry- Add a route entry to a switch routing table
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800639 * @rdev: RIO device
Matt Portereb188d02005-11-07 01:00:17 -0800640 * @table: Routing table ID
641 * @route_destid: Destination ID to be routed
642 * @route_port: Port number to be routed
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700643 * @lock: lock switch device flag
Matt Portereb188d02005-11-07 01:00:17 -0800644 *
645 * Calls the switch specific add_entry() method to add a route entry
646 * on a switch. The route table can be specified using the @table
647 * argument if a switch has per port routing tables or the normal
648 * use is to specific all tables (or the global table) by passing
649 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
650 * on failure.
651 */
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700652static int
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800653rio_route_add_entry(struct rio_dev *rdev,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700654 u16 table, u16 route_destid, u8 route_port, int lock)
Matt Portereb188d02005-11-07 01:00:17 -0800655{
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700656 int rc;
657
658 if (lock) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800659 rc = rio_lock_device(rdev->net->hport, rdev->destid,
660 rdev->hopcount, 1000);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700661 if (rc)
662 return rc;
663 }
664
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800665 rc = rdev->rswitch->add_entry(rdev->net->hport, rdev->destid,
666 rdev->hopcount, table,
667 route_destid, route_port);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700668 if (lock)
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800669 rio_unlock_device(rdev->net->hport, rdev->destid,
670 rdev->hopcount);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700671
672 return rc;
Matt Portereb188d02005-11-07 01:00:17 -0800673}
674
675/**
676 * rio_route_get_entry- Read a route entry in a switch routing table
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800677 * @rdev: RIO device
Matt Portereb188d02005-11-07 01:00:17 -0800678 * @table: Routing table ID
679 * @route_destid: Destination ID to be routed
680 * @route_port: Pointer to read port number into
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700681 * @lock: lock switch device flag
Matt Portereb188d02005-11-07 01:00:17 -0800682 *
683 * Calls the switch specific get_entry() method to read a route entry
684 * in a switch. The route table can be specified using the @table
685 * argument if a switch has per port routing tables or the normal
686 * use is to specific all tables (or the global table) by passing
687 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
688 * on failure.
689 */
690static int
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800691rio_route_get_entry(struct rio_dev *rdev, u16 table,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700692 u16 route_destid, u8 *route_port, int lock)
Matt Portereb188d02005-11-07 01:00:17 -0800693{
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700694 int rc;
695
696 if (lock) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800697 rc = rio_lock_device(rdev->net->hport, rdev->destid,
698 rdev->hopcount, 1000);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700699 if (rc)
700 return rc;
701 }
702
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800703 rc = rdev->rswitch->get_entry(rdev->net->hport, rdev->destid,
704 rdev->hopcount, table,
705 route_destid, route_port);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700706 if (lock)
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800707 rio_unlock_device(rdev->net->hport, rdev->destid,
708 rdev->hopcount);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700709
710 return rc;
Matt Portereb188d02005-11-07 01:00:17 -0800711}
712
713/**
714 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
715 * @port: Master port to send transaction
716 * @hopcount: Number of hops to the device
717 *
718 * Used during enumeration to read the Host Device ID Lock CSR on a
719 * RIO device. Returns the value of the lock register.
720 */
721static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
722{
723 u32 result;
724
Zhang Weie0423232008-04-18 13:33:42 -0700725 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800726 RIO_HOST_DID_LOCK_CSR, &result);
727
728 return (u16) (result & 0xffff);
729}
730
731/**
Matt Portereb188d02005-11-07 01:00:17 -0800732 * rio_enum_peer- Recursively enumerate a RIO network through a master port
733 * @net: RIO network being enumerated
734 * @port: Master port to send transactions
735 * @hopcount: Number of hops into the network
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700736 * @prev: Previous RIO device connected to the enumerated one
737 * @prev_port: Port on previous RIO device
Matt Portereb188d02005-11-07 01:00:17 -0800738 *
739 * Recursively enumerates a RIO network. Transactions are sent via the
740 * master port passed in @port.
741 */
Li Yang181a6ff2009-05-12 16:36:03 +0800742static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700743 u8 hopcount, struct rio_dev *prev, int prev_port)
Matt Portereb188d02005-11-07 01:00:17 -0800744{
745 int port_num;
Matt Portereb188d02005-11-07 01:00:17 -0800746 int cur_destid;
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800747 int sw_destid;
748 int sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800749 struct rio_dev *rdev;
750 u16 destid;
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700751 u32 regval;
Matt Portereb188d02005-11-07 01:00:17 -0800752 int tmp;
753
Alexandre Bouninee274e0e2010-10-27 15:34:32 -0700754 if (rio_mport_chk_dev_access(port,
755 RIO_ANY_DESTID(port->sys_size), hopcount)) {
756 pr_debug("RIO: device access check failed\n");
757 return -1;
758 }
759
Matt Portereb188d02005-11-07 01:00:17 -0800760 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
761 pr_debug("RIO: PE already discovered by this host\n");
762 /*
763 * Already discovered by this host. Add it as another
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700764 * link to the existing device.
Matt Portereb188d02005-11-07 01:00:17 -0800765 */
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700766 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
767 hopcount, RIO_COMPONENT_TAG_CSR, &regval);
768
769 if (regval) {
770 rdev = rio_get_comptag((regval & 0xffff), NULL);
771
772 if (rdev && prev && rio_is_switch(prev)) {
773 pr_debug("RIO: redundant path to %s\n",
774 rio_name(rdev));
775 prev->rswitch->nextdev[prev_port] = rdev;
776 }
777 }
778
Matt Portereb188d02005-11-07 01:00:17 -0800779 return 0;
780 }
781
782 /* Attempt to acquire device lock */
Zhang Weie0423232008-04-18 13:33:42 -0700783 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
784 hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800785 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
786 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
787 < port->host_deviceid) {
788 /* Delay a bit */
789 mdelay(1);
790 /* Attempt to acquire device lock again */
Zhang Weie0423232008-04-18 13:33:42 -0700791 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
792 hopcount,
Matt Portereb188d02005-11-07 01:00:17 -0800793 RIO_HOST_DID_LOCK_CSR,
794 port->host_deviceid);
795 }
796
797 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
798 pr_debug(
799 "RIO: PE locked by a higher priority host...retreating\n");
800 return -1;
801 }
802
803 /* Setup new RIO device */
Zhang Weie0423232008-04-18 13:33:42 -0700804 rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
805 hopcount, 1);
806 if (rdev) {
Matt Portereb188d02005-11-07 01:00:17 -0800807 /* Add device to the global and bus/net specific list. */
808 list_add_tail(&rdev->net_list, &net->devices);
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700809 rdev->prev = prev;
810 if (prev && rio_is_switch(prev))
811 prev->rswitch->nextdev[prev_port] = rdev;
Matt Portereb188d02005-11-07 01:00:17 -0800812 } else
813 return -1;
814
815 if (rio_is_switch(rdev)) {
Alexandre Bounineae05cbd2010-10-27 15:34:29 -0700816 sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800817 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700818 port->host_deviceid, sw_inport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800819 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800820
821 for (destid = 0; destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800822 if (destid == port->host_deviceid)
823 continue;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800824 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700825 destid, sw_inport, 0);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800826 rdev->rswitch->route_table[destid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800827 }
828
Matt Portereb188d02005-11-07 01:00:17 -0800829 pr_debug(
830 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700831 rio_name(rdev), rdev->vid, rdev->did,
832 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800833 sw_destid = next_destid;
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700834 for (port_num = 0;
835 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
836 port_num++) {
Alexandre Bounine8d4630d2012-10-04 17:16:02 -0700837 if (sw_inport == port_num) {
838 rio_enable_rx_tx_port(port, 0,
Thomas Moll933af4a2010-05-26 14:44:01 -0700839 RIO_ANY_DESTID(port->sys_size),
840 hopcount, port_num);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700841 rdev->rswitch->port_ok |= (1 << port_num);
Matt Portereb188d02005-11-07 01:00:17 -0800842 continue;
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700843 }
Matt Portereb188d02005-11-07 01:00:17 -0800844
845 cur_destid = next_destid;
846
847 if (rio_sport_is_active
Zhang Weie0423232008-04-18 13:33:42 -0700848 (port, RIO_ANY_DESTID(port->sys_size), hopcount,
849 port_num)) {
Matt Portereb188d02005-11-07 01:00:17 -0800850 pr_debug(
851 "RIO: scanning device on port %d\n",
852 port_num);
Alexandre Bounine8d4630d2012-10-04 17:16:02 -0700853 rio_enable_rx_tx_port(port, 0,
854 RIO_ANY_DESTID(port->sys_size),
855 hopcount, port_num);
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700856 rdev->rswitch->port_ok |= (1 << port_num);
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800857 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
Zhang Weie0423232008-04-18 13:33:42 -0700858 RIO_ANY_DESTID(port->sys_size),
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700859 port_num, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800860
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700861 if (rio_enum_peer(net, port, hopcount + 1,
862 rdev, port_num) < 0)
Matt Portereb188d02005-11-07 01:00:17 -0800863 return -1;
864
865 /* Update routing tables */
866 if (next_destid > cur_destid) {
867 for (destid = cur_destid;
868 destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800869 if (destid == port->host_deviceid)
870 continue;
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800871 rio_route_add_entry(rdev,
Matt Portereb188d02005-11-07 01:00:17 -0800872 RIO_GLOBAL_TABLE,
873 destid,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700874 port_num,
875 0);
Matt Portereb188d02005-11-07 01:00:17 -0800876 rdev->rswitch->
877 route_table[destid] =
878 port_num;
879 }
Matt Portereb188d02005-11-07 01:00:17 -0800880 }
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700881 } else {
882 /* If switch supports Error Management,
883 * set PORT_LOCKOUT bit for unused port
884 */
885 if (rdev->em_efptr)
886 rio_set_port_lockout(rdev, port_num, 1);
887
888 rdev->rswitch->port_ok &= ~(1 << port_num);
Matt Portereb188d02005-11-07 01:00:17 -0800889 }
890 }
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800891
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700892 /* Direct Port-write messages to the enumeratiing host */
893 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
894 (rdev->em_efptr)) {
895 rio_write_config_32(rdev,
896 rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
897 (port->host_deviceid << 16) |
898 (port->sys_size << 15));
899 }
900
901 rio_init_em(rdev);
902
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800903 /* Check for empty switch */
904 if (next_destid == sw_destid) {
905 next_destid++;
906 if (next_destid == port->host_deviceid)
907 next_destid++;
908 }
909
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800910 rdev->destid = sw_destid;
Matt Portereb188d02005-11-07 01:00:17 -0800911 } else
912 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
913 rio_name(rdev), rdev->vid, rdev->did);
914
915 return 0;
916}
917
918/**
919 * rio_enum_complete- Tests if enumeration of a network is complete
920 * @port: Master port to send transaction
921 *
Liu Ganga5712592011-11-02 13:39:05 -0700922 * Tests the PGCCSR discovered bit for non-zero value (enumeration
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -0700923 * complete flag). Return %1 if enumeration is complete or %0 if
Matt Portereb188d02005-11-07 01:00:17 -0800924 * enumeration is incomplete.
925 */
926static int rio_enum_complete(struct rio_mport *port)
927{
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700928 u32 regval;
Matt Portereb188d02005-11-07 01:00:17 -0800929
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700930 rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
931 &regval);
Liu Ganga5712592011-11-02 13:39:05 -0700932 return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
Matt Portereb188d02005-11-07 01:00:17 -0800933}
934
935/**
936 * rio_disc_peer- Recursively discovers a RIO network through a master port
937 * @net: RIO network being discovered
938 * @port: Master port to send transactions
939 * @destid: Current destination ID in network
940 * @hopcount: Number of hops into the network
Randy Dunlap9b310ac2011-01-22 20:16:12 -0800941 * @prev: previous rio_dev
942 * @prev_port: previous port number
Matt Portereb188d02005-11-07 01:00:17 -0800943 *
944 * Recursively discovers a RIO network. Transactions are sent via the
945 * master port passed in @port.
946 */
Li Yang181a6ff2009-05-12 16:36:03 +0800947static int __devinit
Matt Portereb188d02005-11-07 01:00:17 -0800948rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
Alexandre Bounine17e96202011-01-12 17:00:41 -0800949 u8 hopcount, struct rio_dev *prev, int prev_port)
Matt Portereb188d02005-11-07 01:00:17 -0800950{
951 u8 port_num, route_port;
Matt Portereb188d02005-11-07 01:00:17 -0800952 struct rio_dev *rdev;
953 u16 ndestid;
954
955 /* Setup new RIO device */
956 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
957 /* Add device to the global and bus/net specific list. */
958 list_add_tail(&rdev->net_list, &net->devices);
Alexandre Bounine17e96202011-01-12 17:00:41 -0800959 rdev->prev = prev;
960 if (prev && rio_is_switch(prev))
961 prev->rswitch->nextdev[prev_port] = rdev;
Matt Portereb188d02005-11-07 01:00:17 -0800962 } else
963 return -1;
964
965 if (rio_is_switch(rdev)) {
Matt Portereb188d02005-11-07 01:00:17 -0800966 /* Associated destid is how we accessed this switch */
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800967 rdev->destid = destid;
Matt Portereb188d02005-11-07 01:00:17 -0800968
Matt Portereb188d02005-11-07 01:00:17 -0800969 pr_debug(
970 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
Alexandre Bounine68fe4df2010-10-27 15:34:29 -0700971 rio_name(rdev), rdev->vid, rdev->did,
972 RIO_GET_TOTAL_PORTS(rdev->swpinfo));
973 for (port_num = 0;
974 port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
975 port_num++) {
Alexandre Bounineae05cbd2010-10-27 15:34:29 -0700976 if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
Matt Portereb188d02005-11-07 01:00:17 -0800977 continue;
978
979 if (rio_sport_is_active
980 (port, destid, hopcount, port_num)) {
981 pr_debug(
982 "RIO: scanning device on port %d\n",
983 port_num);
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700984
985 rio_lock_device(port, destid, hopcount, 1000);
986
Zhang Weie0423232008-04-18 13:33:42 -0700987 for (ndestid = 0;
988 ndestid < RIO_ANY_DESTID(port->sys_size);
Matt Portereb188d02005-11-07 01:00:17 -0800989 ndestid++) {
Alexandre Bouninea93192a2011-01-12 17:00:38 -0800990 rio_route_get_entry(rdev,
Matt Portereb188d02005-11-07 01:00:17 -0800991 RIO_GLOBAL_TABLE,
992 ndestid,
Alexandre Bounine818a04a2010-05-26 14:43:58 -0700993 &route_port, 0);
Matt Portereb188d02005-11-07 01:00:17 -0800994 if (route_port == port_num)
995 break;
996 }
997
Alexandre Bounineaf84ca32010-10-27 15:34:34 -0700998 if (ndestid == RIO_ANY_DESTID(port->sys_size))
999 continue;
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001000 rio_unlock_device(port, destid, hopcount);
Alexandre Bounine17e96202011-01-12 17:00:41 -08001001 if (rio_disc_peer(net, port, ndestid,
1002 hopcount + 1, rdev, port_num) < 0)
Matt Portereb188d02005-11-07 01:00:17 -08001003 return -1;
1004 }
1005 }
1006 } else
1007 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
1008 rio_name(rdev), rdev->vid, rdev->did);
1009
1010 return 0;
1011}
1012
1013/**
1014 * rio_mport_is_active- Tests if master port link is active
1015 * @port: Master port to test
1016 *
1017 * Reads the port error status CSR for the master port to
1018 * determine if the port has an active link. Returns
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001019 * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
Matt Portereb188d02005-11-07 01:00:17 -08001020 * or %0 if it is inactive.
1021 */
1022static int rio_mport_is_active(struct rio_mport *port)
1023{
1024 u32 result = 0;
1025 u32 ext_ftr_ptr;
1026 int *entry = rio_mport_phys_table;
1027
1028 do {
1029 if ((ext_ftr_ptr =
1030 rio_mport_get_feature(port, 1, 0, 0, *entry)))
1031 break;
1032 } while (*++entry >= 0);
1033
1034 if (ext_ftr_ptr)
1035 rio_local_read_config_32(port,
1036 ext_ftr_ptr +
1037 RIO_PORT_N_ERR_STS_CSR(port->index),
1038 &result);
1039
Alexandre Bouninee5cabeb2010-05-26 14:43:59 -07001040 return result & RIO_PORT_N_ERR_STS_PORT_OK;
Matt Portereb188d02005-11-07 01:00:17 -08001041}
1042
1043/**
1044 * rio_alloc_net- Allocate and configure a new RIO network
1045 * @port: Master port associated with the RIO network
1046 *
1047 * Allocates a RIO network structure, initializes per-network
1048 * list heads, and adds the associated master port to the
1049 * network list of associated master ports. Returns a
1050 * RIO network pointer on success or %NULL on failure.
1051 */
1052static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
1053{
1054 struct rio_net *net;
1055
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001056 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
Matt Portereb188d02005-11-07 01:00:17 -08001057 if (net) {
Matt Portereb188d02005-11-07 01:00:17 -08001058 INIT_LIST_HEAD(&net->node);
1059 INIT_LIST_HEAD(&net->devices);
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001060 INIT_LIST_HEAD(&net->switches);
Matt Portereb188d02005-11-07 01:00:17 -08001061 INIT_LIST_HEAD(&net->mports);
1062 list_add_tail(&port->nnode, &net->mports);
1063 net->hport = port;
Alexandre Bounine005842e2012-10-04 17:16:08 -07001064 net->id = port->id;
Matt Portereb188d02005-11-07 01:00:17 -08001065 }
1066 return net;
1067}
1068
1069/**
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001070 * rio_update_route_tables- Updates route tables in switches
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001071 * @net: RIO network to run update on
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001072 *
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 */
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001077static void rio_update_route_tables(struct rio_net *net)
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001078{
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
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001084 list_for_each_entry(rdev, &net->devices, net_list) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001085
Alexandre Bouninea93192a2011-01-12 17:00:38 -08001086 destid = rdev->destid;
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001087
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001088 list_for_each_entry(rswitch, &net->switches, node) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -08001089
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);
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001184 rio_clear_locks(net);
Matt Portereb188d02005-11-07 01:00:17 -08001185 rc = -EBUSY;
1186 goto out;
1187 }
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001188 rio_update_route_tables(net);
1189 rio_clear_locks(net);
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
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001203 * @net: RIO network to run route tables scan on
Matt Portereb188d02005-11-07 01:00:17 -08001204 *
1205 * For each switch device, generate a route table by copying existing
1206 * route entries from the switch.
1207 */
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001208static void rio_build_route_tables(struct rio_net *net)
Matt Portereb188d02005-11-07 01:00:17 -08001209{
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001210 struct rio_switch *rswitch;
Matt Portereb188d02005-11-07 01:00:17 -08001211 struct rio_dev *rdev;
1212 int i;
1213 u8 sport;
1214
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001215 list_for_each_entry(rswitch, &net->switches, node) {
1216 rdev = sw_to_rio_dev(rswitch);
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001217
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001218 rio_lock_device(net->hport, rdev->destid,
1219 rdev->hopcount, 1000);
1220 for (i = 0;
1221 i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
1222 i++) {
1223 if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
1224 i, &sport, 0) < 0)
1225 continue;
1226 rswitch->route_table[i] = sport;
Matt Portereb188d02005-11-07 01:00:17 -08001227 }
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001228
1229 rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
1230 }
Matt Portereb188d02005-11-07 01:00:17 -08001231}
1232
1233/**
Matt Portereb188d02005-11-07 01:00:17 -08001234 * rio_disc_mport- Start discovery through a master port
1235 * @mport: Master port to send transactions
1236 *
1237 * Starts the discovery process. If we have an active link,
1238 * then wait for the signal that enumeration is complete.
1239 * When enumeration completion is signaled, start recursive
1240 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1241 * on failure.
1242 */
Al Viro37d33d12008-11-22 17:36:24 +00001243int __devinit rio_disc_mport(struct rio_mport *mport)
Matt Portereb188d02005-11-07 01:00:17 -08001244{
1245 struct rio_net *net = NULL;
Alexandre Bouninefa3dbaa2012-10-04 17:16:04 -07001246 unsigned long to_end;
Matt Portereb188d02005-11-07 01:00:17 -08001247
1248 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1249 mport->name);
1250
1251 /* If master port has an active link, allocate net and discover peers */
1252 if (rio_mport_is_active(mport)) {
Alexandre Bouninefa3dbaa2012-10-04 17:16:04 -07001253 pr_debug("RIO: wait for enumeration to complete...\n");
1254
1255 to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1256 while (time_before(jiffies, to_end)) {
1257 if (rio_enum_complete(mport))
1258 goto enum_done;
1259 schedule_timeout_uninterruptible(msecs_to_jiffies(10));
1260 }
1261
1262 pr_debug("RIO: discovery timeout on mport %d %s\n",
1263 mport->id, mport->name);
1264 goto bail;
1265enum_done:
1266 pr_debug("RIO: ... enumeration done\n");
1267
1268 net = rio_alloc_net(mport);
1269 if (!net) {
Matt Portereb188d02005-11-07 01:00:17 -08001270 printk(KERN_ERR "RIO: Failed to allocate new net\n");
1271 goto bail;
1272 }
1273
Alexandre Bounine818a04a2010-05-26 14:43:58 -07001274 /* Read DestID assigned by enumerator */
1275 rio_local_read_config_32(mport, RIO_DID_CSR,
1276 &mport->host_deviceid);
1277 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1278 mport->host_deviceid);
1279
Zhang Weie0423232008-04-18 13:33:42 -07001280 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
Alexandre Bounine17e96202011-01-12 17:00:41 -08001281 0, NULL, 0) < 0) {
Matt Portereb188d02005-11-07 01:00:17 -08001282 printk(KERN_INFO
1283 "RIO: master port %d device has failed discovery\n",
1284 mport->id);
1285 goto bail;
1286 }
1287
Alexandre Bouninea7071ef2012-10-04 17:16:05 -07001288 rio_build_route_tables(net);
Matt Portereb188d02005-11-07 01:00:17 -08001289 }
1290
1291 return 0;
Alexandre Bouninefa3dbaa2012-10-04 17:16:04 -07001292bail:
Matt Portereb188d02005-11-07 01:00:17 -08001293 return -EBUSY;
1294}