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