rio-scan.c revision 07590ff03935a2efbc03bc7861f20c059576a479
1/*
2 * RapidIO enumeration and discovery support
3 *
4 * Copyright 2005 MontaVista Software, Inc.
5 * Matt Porter <mporter@kernel.crashing.org>
6 *
7 * This program is free software; you can redistribute  it and/or modify it
8 * under  the terms of  the GNU General  Public License as published by the
9 * Free Software Foundation;  either version 2 of the  License, or (at your
10 * option) any later version.
11 */
12
13#include <linux/types.h>
14#include <linux/kernel.h>
15
16#include <linux/delay.h>
17#include <linux/dma-mapping.h>
18#include <linux/init.h>
19#include <linux/rio.h>
20#include <linux/rio_drv.h>
21#include <linux/rio_ids.h>
22#include <linux/rio_regs.h>
23#include <linux/module.h>
24#include <linux/spinlock.h>
25#include <linux/timer.h>
26#include <linux/jiffies.h>
27#include <linux/slab.h>
28
29#include "rio.h"
30
31LIST_HEAD(rio_devices);
32static LIST_HEAD(rio_switches);
33
34#define RIO_ENUM_CMPL_MAGIC	0xdeadbeef
35
36static void rio_enum_timeout(unsigned long);
37
38DEFINE_SPINLOCK(rio_global_list_lock);
39
40static int next_destid = 0;
41static int next_switchid = 0;
42static int next_net = 0;
43
44static struct timer_list rio_enum_timer =
45TIMER_INITIALIZER(rio_enum_timeout, 0, 0);
46
47static int rio_mport_phys_table[] = {
48	RIO_EFB_PAR_EP_ID,
49	RIO_EFB_PAR_EP_REC_ID,
50	RIO_EFB_SER_EP_ID,
51	RIO_EFB_SER_EP_REC_ID,
52	-1,
53};
54
55static int rio_sport_phys_table[] = {
56	RIO_EFB_PAR_EP_FREE_ID,
57	RIO_EFB_SER_EP_FREE_ID,
58	RIO_EFB_SER_EP_FREC_ID,
59	-1,
60};
61
62/**
63 * rio_get_device_id - Get the base/extended device id for a device
64 * @port: RIO master port
65 * @destid: Destination ID of device
66 * @hopcount: Hopcount to device
67 *
68 * Reads the base/extended device id from a device. Returns the
69 * 8/16-bit device ID.
70 */
71static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
72{
73	u32 result;
74
75	rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
76
77	return RIO_GET_DID(port->sys_size, result);
78}
79
80/**
81 * rio_set_device_id - Set the base/extended device id for a device
82 * @port: RIO master port
83 * @destid: Destination ID of device
84 * @hopcount: Hopcount to device
85 * @did: Device ID value to be written
86 *
87 * Writes the base/extended device id from a device.
88 */
89static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
90{
91	rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
92				  RIO_SET_DID(port->sys_size, did));
93}
94
95/**
96 * rio_local_set_device_id - Set the base/extended device id for a port
97 * @port: RIO master port
98 * @did: Device ID value to be written
99 *
100 * Writes the base/extended device id from a device.
101 */
102static void rio_local_set_device_id(struct rio_mport *port, u16 did)
103{
104	rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
105				did));
106}
107
108/**
109 * rio_clear_locks- Release all host locks and signal enumeration complete
110 * @port: Master port to issue transaction
111 *
112 * Marks the component tag CSR on each device with the enumeration
113 * complete flag. When complete, it then release the host locks on
114 * each device. Returns 0 on success or %-EINVAL on failure.
115 */
116static int rio_clear_locks(struct rio_mport *port)
117{
118	struct rio_dev *rdev;
119	u32 result;
120	int ret = 0;
121
122	/* Write component tag CSR magic complete value */
123	rio_local_write_config_32(port, RIO_COMPONENT_TAG_CSR,
124				  RIO_ENUM_CMPL_MAGIC);
125	list_for_each_entry(rdev, &rio_devices, global_list)
126	    rio_write_config_32(rdev, RIO_COMPONENT_TAG_CSR,
127				RIO_ENUM_CMPL_MAGIC);
128
129	/* Release host device id locks */
130	rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
131				  port->host_deviceid);
132	rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
133	if ((result & 0xffff) != 0xffff) {
134		printk(KERN_INFO
135		       "RIO: badness when releasing host lock on master port, result %8.8x\n",
136		       result);
137		ret = -EINVAL;
138	}
139	list_for_each_entry(rdev, &rio_devices, global_list) {
140		rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
141				    port->host_deviceid);
142		rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
143		if ((result & 0xffff) != 0xffff) {
144			printk(KERN_INFO
145			       "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
146			       rdev->vid, rdev->did);
147			ret = -EINVAL;
148		}
149	}
150
151	return ret;
152}
153
154/**
155 * rio_enum_host- Set host lock and initialize host destination ID
156 * @port: Master port to issue transaction
157 *
158 * Sets the local host master port lock and destination ID register
159 * with the host device ID value. The host device ID value is provided
160 * by the platform. Returns %0 on success or %-1 on failure.
161 */
162static int rio_enum_host(struct rio_mport *port)
163{
164	u32 result;
165
166	/* Set master port host device id lock */
167	rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
168				  port->host_deviceid);
169
170	rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
171	if ((result & 0xffff) != port->host_deviceid)
172		return -1;
173
174	/* Set master port destid and init destid ctr */
175	rio_local_set_device_id(port, port->host_deviceid);
176
177	if (next_destid == port->host_deviceid)
178		next_destid++;
179
180	return 0;
181}
182
183/**
184 * rio_device_has_destid- Test if a device contains a destination ID register
185 * @port: Master port to issue transaction
186 * @src_ops: RIO device source operations
187 * @dst_ops: RIO device destination operations
188 *
189 * Checks the provided @src_ops and @dst_ops for the necessary transaction
190 * capabilities that indicate whether or not a device will implement a
191 * destination ID register. Returns 1 if true or 0 if false.
192 */
193static int rio_device_has_destid(struct rio_mport *port, int src_ops,
194				 int dst_ops)
195{
196	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;
197
198	return !!((src_ops | dst_ops) & mask);
199}
200
201/**
202 * rio_release_dev- Frees a RIO device struct
203 * @dev: LDM device associated with a RIO device struct
204 *
205 * Gets the RIO device struct associated a RIO device struct.
206 * The RIO device struct is freed.
207 */
208static void rio_release_dev(struct device *dev)
209{
210	struct rio_dev *rdev;
211
212	rdev = to_rio_dev(dev);
213	kfree(rdev);
214}
215
216/**
217 * rio_is_switch- Tests if a RIO device has switch capabilities
218 * @rdev: RIO device
219 *
220 * Gets the RIO device Processing Element Features register
221 * contents and tests for switch capabilities. Returns 1 if
222 * the device is a switch or 0 if it is not a switch.
223 * The RIO device struct is freed.
224 */
225static int rio_is_switch(struct rio_dev *rdev)
226{
227	if (rdev->pef & RIO_PEF_SWITCH)
228		return 1;
229	return 0;
230}
231
232/**
233 * rio_route_set_ops- Sets routing operations for a particular vendor switch
234 * @rdev: RIO device
235 *
236 * Searches the RIO route ops table for known switch types. If the vid
237 * and did match a switch table entry, then set the add_entry() and
238 * get_entry() ops to the table entry values.
239 */
240static void rio_route_set_ops(struct rio_dev *rdev)
241{
242	struct rio_route_ops *cur = __start_rio_route_ops;
243	struct rio_route_ops *end = __end_rio_route_ops;
244
245	while (cur < end) {
246		if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
247			pr_debug("RIO: adding routing ops for %s\n", rio_name(rdev));
248			rdev->rswitch->add_entry = cur->add_hook;
249			rdev->rswitch->get_entry = cur->get_hook;
250			rdev->rswitch->clr_table = cur->clr_hook;
251			break;
252		}
253		cur++;
254	}
255
256	if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
257		pr_debug("RIO: adding STD routing ops for %s\n",
258			rio_name(rdev));
259		rdev->rswitch->add_entry = rio_std_route_add_entry;
260		rdev->rswitch->get_entry = rio_std_route_get_entry;
261		rdev->rswitch->clr_table = rio_std_route_clr_table;
262	}
263
264	if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
265		printk(KERN_ERR "RIO: missing routing ops for %s\n",
266		       rio_name(rdev));
267}
268
269/**
270 * rio_add_device- Adds a RIO device to the device model
271 * @rdev: RIO device
272 *
273 * Adds the RIO device to the global device list and adds the RIO
274 * device to the RIO device list.  Creates the generic sysfs nodes
275 * for an RIO device.
276 */
277static int __devinit rio_add_device(struct rio_dev *rdev)
278{
279	int err;
280
281	err = device_add(&rdev->dev);
282	if (err)
283		return err;
284
285	spin_lock(&rio_global_list_lock);
286	list_add_tail(&rdev->global_list, &rio_devices);
287	spin_unlock(&rio_global_list_lock);
288
289	rio_create_sysfs_dev_files(rdev);
290
291	return 0;
292}
293
294/**
295 * rio_setup_device- Allocates and sets up a RIO device
296 * @net: RIO network
297 * @port: Master port to send transactions
298 * @destid: Current destination ID
299 * @hopcount: Current hopcount
300 * @do_enum: Enumeration/Discovery mode flag
301 *
302 * Allocates a RIO device and configures fields based on configuration
303 * space contents. If device has a destination ID register, a destination
304 * ID is either assigned in enumeration mode or read from configuration
305 * space in discovery mode.  If the device has switch capabilities, then
306 * a switch is allocated and configured appropriately. Returns a pointer
307 * to a RIO device on success or NULL on failure.
308 *
309 */
310static struct rio_dev __devinit *rio_setup_device(struct rio_net *net,
311					struct rio_mport *port, u16 destid,
312					u8 hopcount, int do_enum)
313{
314	int ret = 0;
315	struct rio_dev *rdev;
316	struct rio_switch *rswitch = NULL;
317	int result, rdid;
318
319	rdev = kzalloc(sizeof(struct rio_dev), GFP_KERNEL);
320	if (!rdev)
321		return NULL;
322
323	rdev->net = net;
324	rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
325				 &result);
326	rdev->did = result >> 16;
327	rdev->vid = result & 0xffff;
328	rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
329				 &rdev->device_rev);
330	rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
331				 &result);
332	rdev->asm_did = result >> 16;
333	rdev->asm_vid = result & 0xffff;
334	rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
335				 &result);
336	rdev->asm_rev = result >> 16;
337	rio_mport_read_config_32(port, destid, hopcount, RIO_PEF_CAR,
338				 &rdev->pef);
339	if (rdev->pef & RIO_PEF_EXT_FEATURES)
340		rdev->efptr = result & 0xffff;
341
342	rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
343				 &rdev->src_ops);
344	rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
345				 &rdev->dst_ops);
346
347	if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
348		if (do_enum) {
349			rio_set_device_id(port, destid, hopcount, next_destid);
350			rdev->destid = next_destid++;
351			if (next_destid == port->host_deviceid)
352				next_destid++;
353		} else
354			rdev->destid = rio_get_device_id(port, destid, hopcount);
355	} else
356		/* Switch device has an associated destID */
357		rdev->destid = RIO_INVALID_DESTID;
358
359	/* If a PE has both switch and other functions, show it as a switch */
360	if (rio_is_switch(rdev)) {
361		rio_mport_read_config_32(port, destid, hopcount,
362					 RIO_SWP_INFO_CAR, &rdev->swpinfo);
363		rswitch = kzalloc(sizeof(struct rio_switch), GFP_KERNEL);
364		if (!rswitch)
365			goto cleanup;
366		rswitch->switchid = next_switchid;
367		rswitch->hopcount = hopcount;
368		rswitch->destid = destid;
369		rswitch->route_table = kzalloc(sizeof(u8)*
370					RIO_MAX_ROUTE_ENTRIES(port->sys_size),
371					GFP_KERNEL);
372		if (!rswitch->route_table)
373			goto cleanup;
374		/* Initialize switch route table */
375		for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
376				rdid++)
377			rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
378		rdev->rswitch = rswitch;
379		dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
380			     rdev->rswitch->switchid);
381		rio_route_set_ops(rdev);
382
383		if (do_enum && rdev->rswitch->clr_table)
384			rdev->rswitch->clr_table(port, destid, hopcount,
385						 RIO_GLOBAL_TABLE);
386
387		list_add_tail(&rswitch->node, &rio_switches);
388
389	} else
390		dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
391			     rdev->destid);
392
393	rdev->dev.bus = &rio_bus_type;
394
395	device_initialize(&rdev->dev);
396	rdev->dev.release = rio_release_dev;
397	rio_dev_get(rdev);
398
399	rdev->dma_mask = DMA_BIT_MASK(32);
400	rdev->dev.dma_mask = &rdev->dma_mask;
401	rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
402
403	if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
404	    (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
405		rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
406				   0, 0xffff);
407
408	ret = rio_add_device(rdev);
409	if (ret)
410		goto cleanup;
411
412	return rdev;
413
414cleanup:
415	if (rswitch) {
416		kfree(rswitch->route_table);
417		kfree(rswitch);
418	}
419	kfree(rdev);
420	return NULL;
421}
422
423/**
424 * rio_sport_is_active- Tests if a switch port has an active connection.
425 * @port: Master port to send transaction
426 * @destid: Associated destination ID for switch
427 * @hopcount: Hopcount to reach switch
428 * @sport: Switch port number
429 *
430 * Reads the port error status CSR for a particular switch port to
431 * determine if the port has an active link.  Returns
432 * %PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
433 * inactive.
434 */
435static int
436rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
437{
438	u32 result;
439	u32 ext_ftr_ptr;
440
441	int *entry = rio_sport_phys_table;
442
443	do {
444		if ((ext_ftr_ptr =
445		     rio_mport_get_feature(port, 0, destid, hopcount, *entry)))
446
447			break;
448	} while (*++entry >= 0);
449
450	if (ext_ftr_ptr)
451		rio_mport_read_config_32(port, destid, hopcount,
452					 ext_ftr_ptr +
453					 RIO_PORT_N_ERR_STS_CSR(sport),
454					 &result);
455
456	return (result & PORT_N_ERR_STS_PORT_OK);
457}
458
459/**
460 * rio_route_add_entry- Add a route entry to a switch routing table
461 * @mport: Master port to send transaction
462 * @rswitch: Switch device
463 * @table: Routing table ID
464 * @route_destid: Destination ID to be routed
465 * @route_port: Port number to be routed
466 *
467 * Calls the switch specific add_entry() method to add a route entry
468 * on a switch. The route table can be specified using the @table
469 * argument if a switch has per port routing tables or the normal
470 * use is to specific all tables (or the global table) by passing
471 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
472 * on failure.
473 */
474static int rio_route_add_entry(struct rio_mport *mport, struct rio_switch *rswitch,
475			       u16 table, u16 route_destid, u8 route_port)
476{
477	return rswitch->add_entry(mport, rswitch->destid,
478					rswitch->hopcount, table,
479					route_destid, route_port);
480}
481
482/**
483 * rio_route_get_entry- Read a route entry in a switch routing table
484 * @mport: Master port to send transaction
485 * @rswitch: Switch device
486 * @table: Routing table ID
487 * @route_destid: Destination ID to be routed
488 * @route_port: Pointer to read port number into
489 *
490 * Calls the switch specific get_entry() method to read a route entry
491 * in a switch. The route table can be specified using the @table
492 * argument if a switch has per port routing tables or the normal
493 * use is to specific all tables (or the global table) by passing
494 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
495 * on failure.
496 */
497static int
498rio_route_get_entry(struct rio_mport *mport, struct rio_switch *rswitch, u16 table,
499		    u16 route_destid, u8 * route_port)
500{
501	return rswitch->get_entry(mport, rswitch->destid,
502					rswitch->hopcount, table,
503					route_destid, route_port);
504}
505
506/**
507 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
508 * @port: Master port to send transaction
509 * @hopcount: Number of hops to the device
510 *
511 * Used during enumeration to read the Host Device ID Lock CSR on a
512 * RIO device. Returns the value of the lock register.
513 */
514static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
515{
516	u32 result;
517
518	rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
519				 RIO_HOST_DID_LOCK_CSR, &result);
520
521	return (u16) (result & 0xffff);
522}
523
524/**
525 * rio_get_swpinfo_inport- Gets the ingress port number
526 * @mport: Master port to send transaction
527 * @destid: Destination ID associated with the switch
528 * @hopcount: Number of hops to the device
529 *
530 * Returns port number being used to access the switch device.
531 */
532static u8
533rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
534{
535	u32 result;
536
537	rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
538				 &result);
539
540	return (u8) (result & 0xff);
541}
542
543/**
544 * rio_get_swpinfo_tports- Gets total number of ports on the switch
545 * @mport: Master port to send transaction
546 * @destid: Destination ID associated with the switch
547 * @hopcount: Number of hops to the device
548 *
549 * Returns total numbers of ports implemented by the switch device.
550 */
551static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
552				 u8 hopcount)
553{
554	u32 result;
555
556	rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
557				 &result);
558
559	return RIO_GET_TOTAL_PORTS(result);
560}
561
562/**
563 * rio_net_add_mport- Add a master port to a RIO network
564 * @net: RIO network
565 * @port: Master port to add
566 *
567 * Adds a master port to the network list of associated master
568 * ports..
569 */
570static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
571{
572	spin_lock(&rio_global_list_lock);
573	list_add_tail(&port->nnode, &net->mports);
574	spin_unlock(&rio_global_list_lock);
575}
576
577/**
578 * rio_enum_peer- Recursively enumerate a RIO network through a master port
579 * @net: RIO network being enumerated
580 * @port: Master port to send transactions
581 * @hopcount: Number of hops into the network
582 *
583 * Recursively enumerates a RIO network.  Transactions are sent via the
584 * master port passed in @port.
585 */
586static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
587			 u8 hopcount)
588{
589	int port_num;
590	int num_ports;
591	int cur_destid;
592	int sw_destid;
593	int sw_inport;
594	struct rio_dev *rdev;
595	u16 destid;
596	int tmp;
597
598	if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
599		pr_debug("RIO: PE already discovered by this host\n");
600		/*
601		 * Already discovered by this host. Add it as another
602		 * master port for the current network.
603		 */
604		rio_net_add_mport(net, port);
605		return 0;
606	}
607
608	/* Attempt to acquire device lock */
609	rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
610				  hopcount,
611				  RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
612	while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
613	       < port->host_deviceid) {
614		/* Delay a bit */
615		mdelay(1);
616		/* Attempt to acquire device lock again */
617		rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
618					  hopcount,
619					  RIO_HOST_DID_LOCK_CSR,
620					  port->host_deviceid);
621	}
622
623	if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
624		pr_debug(
625		    "RIO: PE locked by a higher priority host...retreating\n");
626		return -1;
627	}
628
629	/* Setup new RIO device */
630	rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
631					hopcount, 1);
632	if (rdev) {
633		/* Add device to the global and bus/net specific list. */
634		list_add_tail(&rdev->net_list, &net->devices);
635	} else
636		return -1;
637
638	if (rio_is_switch(rdev)) {
639		next_switchid++;
640		sw_inport = rio_get_swpinfo_inport(port,
641				RIO_ANY_DESTID(port->sys_size), hopcount);
642		rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
643				    port->host_deviceid, sw_inport);
644		rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
645
646		for (destid = 0; destid < next_destid; destid++) {
647			if (destid == port->host_deviceid)
648				continue;
649			rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
650					    destid, sw_inport);
651			rdev->rswitch->route_table[destid] = sw_inport;
652		}
653
654		num_ports =
655		    rio_get_swpinfo_tports(port, RIO_ANY_DESTID(port->sys_size),
656						hopcount);
657		pr_debug(
658		    "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
659		    rio_name(rdev), rdev->vid, rdev->did, num_ports);
660		sw_destid = next_destid;
661		for (port_num = 0; port_num < num_ports; port_num++) {
662			if (sw_inport == port_num)
663				continue;
664
665			cur_destid = next_destid;
666
667			if (rio_sport_is_active
668			    (port, RIO_ANY_DESTID(port->sys_size), hopcount,
669			     port_num)) {
670				pr_debug(
671				    "RIO: scanning device on port %d\n",
672				    port_num);
673				rio_route_add_entry(port, rdev->rswitch,
674						RIO_GLOBAL_TABLE,
675						RIO_ANY_DESTID(port->sys_size),
676						port_num);
677
678				if (rio_enum_peer(net, port, hopcount + 1) < 0)
679					return -1;
680
681				/* Update routing tables */
682				if (next_destid > cur_destid) {
683					for (destid = cur_destid;
684					     destid < next_destid; destid++) {
685						if (destid == port->host_deviceid)
686							continue;
687						rio_route_add_entry(port, rdev->rswitch,
688								    RIO_GLOBAL_TABLE,
689								    destid,
690								    port_num);
691						rdev->rswitch->
692						    route_table[destid] =
693						    port_num;
694					}
695				}
696			}
697		}
698
699		/* Check for empty switch */
700		if (next_destid == sw_destid) {
701			next_destid++;
702			if (next_destid == port->host_deviceid)
703				next_destid++;
704		}
705
706		rdev->rswitch->destid = sw_destid;
707	} else
708		pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
709		    rio_name(rdev), rdev->vid, rdev->did);
710
711	return 0;
712}
713
714/**
715 * rio_enum_complete- Tests if enumeration of a network is complete
716 * @port: Master port to send transaction
717 *
718 * Tests the Component Tag CSR for presence of the magic enumeration
719 * complete flag. Return %1 if enumeration is complete or %0 if
720 * enumeration is incomplete.
721 */
722static int rio_enum_complete(struct rio_mport *port)
723{
724	u32 tag_csr;
725	int ret = 0;
726
727	rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
728
729	if (tag_csr == RIO_ENUM_CMPL_MAGIC)
730		ret = 1;
731
732	return ret;
733}
734
735/**
736 * rio_disc_peer- Recursively discovers a RIO network through a master port
737 * @net: RIO network being discovered
738 * @port: Master port to send transactions
739 * @destid: Current destination ID in network
740 * @hopcount: Number of hops into the network
741 *
742 * Recursively discovers a RIO network.  Transactions are sent via the
743 * master port passed in @port.
744 */
745static int __devinit
746rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
747	      u8 hopcount)
748{
749	u8 port_num, route_port;
750	int num_ports;
751	struct rio_dev *rdev;
752	u16 ndestid;
753
754	/* Setup new RIO device */
755	if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
756		/* Add device to the global and bus/net specific list. */
757		list_add_tail(&rdev->net_list, &net->devices);
758	} else
759		return -1;
760
761	if (rio_is_switch(rdev)) {
762		next_switchid++;
763
764		/* Associated destid is how we accessed this switch */
765		rdev->rswitch->destid = destid;
766
767		num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
768		pr_debug(
769		    "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
770		    rio_name(rdev), rdev->vid, rdev->did, num_ports);
771		for (port_num = 0; port_num < num_ports; port_num++) {
772			if (rio_get_swpinfo_inport(port, destid, hopcount) ==
773			    port_num)
774				continue;
775
776			if (rio_sport_is_active
777			    (port, destid, hopcount, port_num)) {
778				pr_debug(
779				    "RIO: scanning device on port %d\n",
780				    port_num);
781				for (ndestid = 0;
782				     ndestid < RIO_ANY_DESTID(port->sys_size);
783				     ndestid++) {
784					rio_route_get_entry(port, rdev->rswitch,
785							    RIO_GLOBAL_TABLE,
786							    ndestid,
787							    &route_port);
788					if (route_port == port_num)
789						break;
790				}
791
792				if (rio_disc_peer
793				    (net, port, ndestid, hopcount + 1) < 0)
794					return -1;
795			}
796		}
797	} else
798		pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
799		    rio_name(rdev), rdev->vid, rdev->did);
800
801	return 0;
802}
803
804/**
805 * rio_mport_is_active- Tests if master port link is active
806 * @port: Master port to test
807 *
808 * Reads the port error status CSR for the master port to
809 * determine if the port has an active link.  Returns
810 * %PORT_N_ERR_STS_PORT_OK if the  master port is active
811 * or %0 if it is inactive.
812 */
813static int rio_mport_is_active(struct rio_mport *port)
814{
815	u32 result = 0;
816	u32 ext_ftr_ptr;
817	int *entry = rio_mport_phys_table;
818
819	do {
820		if ((ext_ftr_ptr =
821		     rio_mport_get_feature(port, 1, 0, 0, *entry)))
822			break;
823	} while (*++entry >= 0);
824
825	if (ext_ftr_ptr)
826		rio_local_read_config_32(port,
827					 ext_ftr_ptr +
828					 RIO_PORT_N_ERR_STS_CSR(port->index),
829					 &result);
830
831	return (result & PORT_N_ERR_STS_PORT_OK);
832}
833
834/**
835 * rio_alloc_net- Allocate and configure a new RIO network
836 * @port: Master port associated with the RIO network
837 *
838 * Allocates a RIO network structure, initializes per-network
839 * list heads, and adds the associated master port to the
840 * network list of associated master ports. Returns a
841 * RIO network pointer on success or %NULL on failure.
842 */
843static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
844{
845	struct rio_net *net;
846
847	net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
848	if (net) {
849		INIT_LIST_HEAD(&net->node);
850		INIT_LIST_HEAD(&net->devices);
851		INIT_LIST_HEAD(&net->mports);
852		list_add_tail(&port->nnode, &net->mports);
853		net->hport = port;
854		net->id = next_net++;
855	}
856	return net;
857}
858
859/**
860 * rio_update_route_tables- Updates route tables in switches
861 * @port: Master port associated with the RIO network
862 *
863 * For each enumerated device, ensure that each switch in a system
864 * has correct routing entries. Add routes for devices that where
865 * unknown dirung the first enumeration pass through the switch.
866 */
867static void rio_update_route_tables(struct rio_mport *port)
868{
869	struct rio_dev *rdev;
870	struct rio_switch *rswitch;
871	u8 sport;
872	u16 destid;
873
874	list_for_each_entry(rdev, &rio_devices, global_list) {
875
876		destid = (rio_is_switch(rdev))?rdev->rswitch->destid:rdev->destid;
877
878		list_for_each_entry(rswitch, &rio_switches, node) {
879
880			if (rio_is_switch(rdev)	&& (rdev->rswitch == rswitch))
881				continue;
882
883			if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
884				/* Skip if destid ends in empty switch*/
885				if (rswitch->destid == destid)
886					continue;
887
888				sport = rio_get_swpinfo_inport(port,
889						rswitch->destid, rswitch->hopcount);
890
891				if (rswitch->add_entry)	{
892					rio_route_add_entry(port, rswitch, RIO_GLOBAL_TABLE, destid, sport);
893					rswitch->route_table[destid] = sport;
894				}
895			}
896		}
897	}
898}
899
900/**
901 * rio_enum_mport- Start enumeration through a master port
902 * @mport: Master port to send transactions
903 *
904 * Starts the enumeration process. If somebody has enumerated our
905 * master port device, then give up. If not and we have an active
906 * link, then start recursive peer enumeration. Returns %0 if
907 * enumeration succeeds or %-EBUSY if enumeration fails.
908 */
909int __devinit rio_enum_mport(struct rio_mport *mport)
910{
911	struct rio_net *net = NULL;
912	int rc = 0;
913
914	printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
915	       mport->name);
916	/* If somebody else enumerated our master port device, bail. */
917	if (rio_enum_host(mport) < 0) {
918		printk(KERN_INFO
919		       "RIO: master port %d device has been enumerated by a remote host\n",
920		       mport->id);
921		rc = -EBUSY;
922		goto out;
923	}
924
925	/* If master port has an active link, allocate net and enum peers */
926	if (rio_mport_is_active(mport)) {
927		if (!(net = rio_alloc_net(mport))) {
928			printk(KERN_ERR "RIO: failed to allocate new net\n");
929			rc = -ENOMEM;
930			goto out;
931		}
932		if (rio_enum_peer(net, mport, 0) < 0) {
933			/* A higher priority host won enumeration, bail. */
934			printk(KERN_INFO
935			       "RIO: master port %d device has lost enumeration to a remote host\n",
936			       mport->id);
937			rio_clear_locks(mport);
938			rc = -EBUSY;
939			goto out;
940		}
941		rio_update_route_tables(mport);
942		rio_clear_locks(mport);
943	} else {
944		printk(KERN_INFO "RIO: master port %d link inactive\n",
945		       mport->id);
946		rc = -EINVAL;
947	}
948
949      out:
950	return rc;
951}
952
953/**
954 * rio_build_route_tables- Generate route tables from switch route entries
955 *
956 * For each switch device, generate a route table by copying existing
957 * route entries from the switch.
958 */
959static void rio_build_route_tables(void)
960{
961	struct rio_dev *rdev;
962	int i;
963	u8 sport;
964
965	list_for_each_entry(rdev, &rio_devices, global_list)
966	    if (rio_is_switch(rdev))
967		for (i = 0;
968		     i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
969		     i++) {
970			if (rio_route_get_entry
971			    (rdev->net->hport, rdev->rswitch, RIO_GLOBAL_TABLE,
972			     i, &sport) < 0)
973				continue;
974			rdev->rswitch->route_table[i] = sport;
975		}
976}
977
978/**
979 * rio_enum_timeout- Signal that enumeration timed out
980 * @data: Address of timeout flag.
981 *
982 * When the enumeration complete timer expires, set a flag that
983 * signals to the discovery process that enumeration did not
984 * complete in a sane amount of time.
985 */
986static void rio_enum_timeout(unsigned long data)
987{
988	/* Enumeration timed out, set flag */
989	*(int *)data = 1;
990}
991
992/**
993 * rio_disc_mport- Start discovery through a master port
994 * @mport: Master port to send transactions
995 *
996 * Starts the discovery process. If we have an active link,
997 * then wait for the signal that enumeration is complete.
998 * When enumeration completion is signaled, start recursive
999 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1000 * on failure.
1001 */
1002int __devinit rio_disc_mport(struct rio_mport *mport)
1003{
1004	struct rio_net *net = NULL;
1005	int enum_timeout_flag = 0;
1006
1007	printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1008	       mport->name);
1009
1010	/* If master port has an active link, allocate net and discover peers */
1011	if (rio_mport_is_active(mport)) {
1012		if (!(net = rio_alloc_net(mport))) {
1013			printk(KERN_ERR "RIO: Failed to allocate new net\n");
1014			goto bail;
1015		}
1016
1017		pr_debug("RIO: wait for enumeration complete...");
1018
1019		rio_enum_timer.expires =
1020		    jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1021		rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
1022		add_timer(&rio_enum_timer);
1023		while (!rio_enum_complete(mport)) {
1024			mdelay(1);
1025			if (enum_timeout_flag) {
1026				del_timer_sync(&rio_enum_timer);
1027				goto timeout;
1028			}
1029		}
1030		del_timer_sync(&rio_enum_timer);
1031
1032		pr_debug("done\n");
1033		if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1034					0) < 0) {
1035			printk(KERN_INFO
1036			       "RIO: master port %d device has failed discovery\n",
1037			       mport->id);
1038			goto bail;
1039		}
1040
1041		rio_build_route_tables();
1042	}
1043
1044	return 0;
1045
1046      timeout:
1047	pr_debug("timeout\n");
1048      bail:
1049	return -EBUSY;
1050}
1051