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