[ARM] 3417/1: add support for logicpd pxa270 card engine
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / rapidio / rio-scan.c
blob94e30fe4b8f3242b17f8adc5c1623669d2a513b6
1 /*
2 * RapidIO enumeration and discovery support
4 * Copyright 2005 MontaVista Software, Inc.
5 * Matt Porter <mporter@kernel.crashing.org>
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.
13 #include <linux/config.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/init.h>
20 #include <linux/rio.h>
21 #include <linux/rio_drv.h>
22 #include <linux/rio_ids.h>
23 #include <linux/rio_regs.h>
24 #include <linux/module.h>
25 #include <linux/spinlock.h>
26 #include <linux/timer.h>
27 #include <linux/jiffies.h>
28 #include <linux/slab.h>
30 #include "rio.h"
32 LIST_HEAD(rio_devices);
33 static LIST_HEAD(rio_switches);
35 #define RIO_ENUM_CMPL_MAGIC 0xdeadbeef
37 static void rio_enum_timeout(unsigned long);
39 DEFINE_SPINLOCK(rio_global_list_lock);
41 static int next_destid = 0;
42 static int next_switchid = 0;
43 static int next_net = 0;
45 static struct timer_list rio_enum_timer =
46 TIMER_INITIALIZER(rio_enum_timeout, 0, 0);
48 static int rio_mport_phys_table[] = {
49 RIO_EFB_PAR_EP_ID,
50 RIO_EFB_PAR_EP_REC_ID,
51 RIO_EFB_SER_EP_ID,
52 RIO_EFB_SER_EP_REC_ID,
53 -1,
56 static int rio_sport_phys_table[] = {
57 RIO_EFB_PAR_EP_FREE_ID,
58 RIO_EFB_SER_EP_FREE_ID,
59 -1,
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
68 * Reads the base/extended device id from a device. Returns the
69 * 8/16-bit device ID.
71 static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
73 u32 result;
75 rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
77 return RIO_GET_DID(result);
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
87 * Writes the base/extended device id from a device.
89 static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
91 rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
92 RIO_SET_DID(did));
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
100 * Writes the base/extended device id from a device.
102 static void rio_local_set_device_id(struct rio_mport *port, u16 did)
104 rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(did));
108 * rio_clear_locks- Release all host locks and signal enumeration complete
109 * @port: Master port to issue transaction
111 * Marks the component tag CSR on each device with the enumeration
112 * complete flag. When complete, it then release the host locks on
113 * each device. Returns 0 on success or %-EINVAL on failure.
115 static int rio_clear_locks(struct rio_mport *port)
117 struct rio_dev *rdev;
118 u32 result;
119 int ret = 0;
121 /* Write component tag CSR magic complete value */
122 rio_local_write_config_32(port, RIO_COMPONENT_TAG_CSR,
123 RIO_ENUM_CMPL_MAGIC);
124 list_for_each_entry(rdev, &rio_devices, global_list)
125 rio_write_config_32(rdev, RIO_COMPONENT_TAG_CSR,
126 RIO_ENUM_CMPL_MAGIC);
128 /* Release host device id locks */
129 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
130 port->host_deviceid);
131 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
132 if ((result & 0xffff) != 0xffff) {
133 printk(KERN_INFO
134 "RIO: badness when releasing host lock on master port, result %8.8x\n",
135 result);
136 ret = -EINVAL;
138 list_for_each_entry(rdev, &rio_devices, global_list) {
139 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
140 port->host_deviceid);
141 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
142 if ((result & 0xffff) != 0xffff) {
143 printk(KERN_INFO
144 "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
145 rdev->vid, rdev->did);
146 ret = -EINVAL;
150 return ret;
154 * rio_enum_host- Set host lock and initialize host destination ID
155 * @port: Master port to issue transaction
157 * Sets the local host master port lock and destination ID register
158 * with the host device ID value. The host device ID value is provided
159 * by the platform. Returns %0 on success or %-1 on failure.
161 static int rio_enum_host(struct rio_mport *port)
163 u32 result;
165 /* Set master port host device id lock */
166 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
167 port->host_deviceid);
169 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
170 if ((result & 0xffff) != port->host_deviceid)
171 return -1;
173 /* Set master port destid and init destid ctr */
174 rio_local_set_device_id(port, port->host_deviceid);
176 if (next_destid == port->host_deviceid)
177 next_destid++;
179 return 0;
183 * rio_device_has_destid- Test if a device contains a destination ID register
184 * @port: Master port to issue transaction
185 * @src_ops: RIO device source operations
186 * @dst_ops: RIO device destination operations
188 * Checks the provided @src_ops and @dst_ops for the necessary transaction
189 * capabilities that indicate whether or not a device will implement a
190 * destination ID register. Returns 1 if true or 0 if false.
192 static int rio_device_has_destid(struct rio_mport *port, int src_ops,
193 int dst_ops)
195 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 return !!((src_ops | dst_ops) & mask);
201 * rio_release_dev- Frees a RIO device struct
202 * @dev: LDM device associated with a RIO device struct
204 * Gets the RIO device struct associated a RIO device struct.
205 * The RIO device struct is freed.
207 static void rio_release_dev(struct device *dev)
209 struct rio_dev *rdev;
211 rdev = to_rio_dev(dev);
212 kfree(rdev);
216 * rio_is_switch- Tests if a RIO device has switch capabilities
217 * @rdev: RIO device
219 * Gets the RIO device Processing Element Features register
220 * contents and tests for switch capabilities. Returns 1 if
221 * the device is a switch or 0 if it is not a switch.
222 * The RIO device struct is freed.
224 static int rio_is_switch(struct rio_dev *rdev)
226 if (rdev->pef & RIO_PEF_SWITCH)
227 return 1;
228 return 0;
232 * rio_route_set_ops- Sets routing operations for a particular vendor switch
233 * @rdev: RIO device
235 * Searches the RIO route ops table for known switch types. If the vid
236 * and did match a switch table entry, then set the add_entry() and
237 * get_entry() ops to the table entry values.
239 static void rio_route_set_ops(struct rio_dev *rdev)
241 struct rio_route_ops *cur = __start_rio_route_ops;
242 struct rio_route_ops *end = __end_rio_route_ops;
244 while (cur < end) {
245 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
246 pr_debug("RIO: adding routing ops for %s\n", rio_name(rdev));
247 rdev->rswitch->add_entry = cur->add_hook;
248 rdev->rswitch->get_entry = cur->get_hook;
250 cur++;
253 if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
254 printk(KERN_ERR "RIO: missing routing ops for %s\n",
255 rio_name(rdev));
259 * rio_add_device- Adds a RIO device to the device model
260 * @rdev: RIO device
262 * Adds the RIO device to the global device list and adds the RIO
263 * device to the RIO device list. Creates the generic sysfs nodes
264 * for an RIO device.
266 static void __devinit rio_add_device(struct rio_dev *rdev)
268 device_add(&rdev->dev);
270 spin_lock(&rio_global_list_lock);
271 list_add_tail(&rdev->global_list, &rio_devices);
272 spin_unlock(&rio_global_list_lock);
274 rio_create_sysfs_dev_files(rdev);
278 * rio_setup_device- Allocates and sets up a RIO device
279 * @net: RIO network
280 * @port: Master port to send transactions
281 * @destid: Current destination ID
282 * @hopcount: Current hopcount
283 * @do_enum: Enumeration/Discovery mode flag
285 * Allocates a RIO device and configures fields based on configuration
286 * space contents. If device has a destination ID register, a destination
287 * ID is either assigned in enumeration mode or read from configuration
288 * space in discovery mode. If the device has switch capabilities, then
289 * a switch is allocated and configured appropriately. Returns a pointer
290 * to a RIO device on success or NULL on failure.
293 static struct rio_dev *rio_setup_device(struct rio_net *net,
294 struct rio_mport *port, u16 destid,
295 u8 hopcount, int do_enum)
297 struct rio_dev *rdev;
298 struct rio_switch *rswitch;
299 int result, rdid;
301 rdev = kmalloc(sizeof(struct rio_dev), GFP_KERNEL);
302 if (!rdev)
303 goto out;
305 memset(rdev, 0, sizeof(struct rio_dev));
306 rdev->net = net;
307 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
308 &result);
309 rdev->did = result >> 16;
310 rdev->vid = result & 0xffff;
311 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
312 &rdev->device_rev);
313 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
314 &result);
315 rdev->asm_did = result >> 16;
316 rdev->asm_vid = result & 0xffff;
317 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
318 &result);
319 rdev->asm_rev = result >> 16;
320 rio_mport_read_config_32(port, destid, hopcount, RIO_PEF_CAR,
321 &rdev->pef);
322 if (rdev->pef & RIO_PEF_EXT_FEATURES)
323 rdev->efptr = result & 0xffff;
325 rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
326 &rdev->src_ops);
327 rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
328 &rdev->dst_ops);
330 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)
331 && do_enum) {
332 rio_set_device_id(port, destid, hopcount, next_destid);
333 rdev->destid = next_destid++;
334 if (next_destid == port->host_deviceid)
335 next_destid++;
336 } else
337 rdev->destid = rio_get_device_id(port, destid, hopcount);
339 /* If a PE has both switch and other functions, show it as a switch */
340 if (rio_is_switch(rdev)) {
341 rio_mport_read_config_32(port, destid, hopcount,
342 RIO_SWP_INFO_CAR, &rdev->swpinfo);
343 rswitch = kmalloc(sizeof(struct rio_switch), GFP_KERNEL);
344 if (!rswitch) {
345 kfree(rdev);
346 rdev = NULL;
347 goto out;
349 rswitch->switchid = next_switchid;
350 rswitch->hopcount = hopcount;
351 rswitch->destid = 0xffff;
352 /* Initialize switch route table */
353 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES; rdid++)
354 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
355 rdev->rswitch = rswitch;
356 sprintf(rio_name(rdev), "%02x:s:%04x", rdev->net->id,
357 rdev->rswitch->switchid);
358 rio_route_set_ops(rdev);
360 list_add_tail(&rswitch->node, &rio_switches);
362 } else
363 sprintf(rio_name(rdev), "%02x:e:%04x", rdev->net->id,
364 rdev->destid);
366 rdev->dev.bus = &rio_bus_type;
368 device_initialize(&rdev->dev);
369 rdev->dev.release = rio_release_dev;
370 rio_dev_get(rdev);
372 rdev->dma_mask = DMA_32BIT_MASK;
373 rdev->dev.dma_mask = &rdev->dma_mask;
374 rdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
376 if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
377 (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
378 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
379 0, 0xffff);
381 rio_add_device(rdev);
383 out:
384 return rdev;
388 * rio_sport_is_active- Tests if a switch port has an active connection.
389 * @port: Master port to send transaction
390 * @destid: Associated destination ID for switch
391 * @hopcount: Hopcount to reach switch
392 * @sport: Switch port number
394 * Reads the port error status CSR for a particular switch port to
395 * determine if the port has an active link. Returns
396 * %PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
397 * inactive.
399 static int
400 rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
402 u32 result;
403 u32 ext_ftr_ptr;
405 int *entry = rio_sport_phys_table;
407 do {
408 if ((ext_ftr_ptr =
409 rio_mport_get_feature(port, 0, destid, hopcount, *entry)))
411 break;
412 } while (*++entry >= 0);
414 if (ext_ftr_ptr)
415 rio_mport_read_config_32(port, destid, hopcount,
416 ext_ftr_ptr +
417 RIO_PORT_N_ERR_STS_CSR(sport),
418 &result);
420 return (result & PORT_N_ERR_STS_PORT_OK);
424 * rio_route_add_entry- Add a route entry to a switch routing table
425 * @mport: Master port to send transaction
426 * @rdev: Switch device
427 * @table: Routing table ID
428 * @route_destid: Destination ID to be routed
429 * @route_port: Port number to be routed
431 * Calls the switch specific add_entry() method to add a route entry
432 * on a switch. The route table can be specified using the @table
433 * argument if a switch has per port routing tables or the normal
434 * use is to specific all tables (or the global table) by passing
435 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
436 * on failure.
438 static int rio_route_add_entry(struct rio_mport *mport, struct rio_dev *rdev,
439 u16 table, u16 route_destid, u8 route_port)
441 return rdev->rswitch->add_entry(mport, rdev->rswitch->destid,
442 rdev->rswitch->hopcount, table,
443 route_destid, route_port);
447 * rio_route_get_entry- Read a route entry in a switch routing table
448 * @mport: Master port to send transaction
449 * @rdev: Switch device
450 * @table: Routing table ID
451 * @route_destid: Destination ID to be routed
452 * @route_port: Pointer to read port number into
454 * Calls the switch specific get_entry() method to read a route entry
455 * in a switch. The route table can be specified using the @table
456 * argument if a switch has per port routing tables or the normal
457 * use is to specific all tables (or the global table) by passing
458 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
459 * on failure.
461 static int
462 rio_route_get_entry(struct rio_mport *mport, struct rio_dev *rdev, u16 table,
463 u16 route_destid, u8 * route_port)
465 return rdev->rswitch->get_entry(mport, rdev->rswitch->destid,
466 rdev->rswitch->hopcount, table,
467 route_destid, route_port);
471 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
472 * @port: Master port to send transaction
473 * @hopcount: Number of hops to the device
475 * Used during enumeration to read the Host Device ID Lock CSR on a
476 * RIO device. Returns the value of the lock register.
478 static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
480 u32 result;
482 rio_mport_read_config_32(port, RIO_ANY_DESTID, hopcount,
483 RIO_HOST_DID_LOCK_CSR, &result);
485 return (u16) (result & 0xffff);
489 * rio_get_swpinfo_inport- Gets the ingress port number
490 * @mport: Master port to send transaction
491 * @destid: Destination ID associated with the switch
492 * @hopcount: Number of hops to the device
494 * Returns port number being used to access the switch device.
496 static u8
497 rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
499 u32 result;
501 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
502 &result);
504 return (u8) (result & 0xff);
508 * rio_get_swpinfo_tports- Gets total number of ports on the switch
509 * @mport: Master port to send transaction
510 * @destid: Destination ID associated with the switch
511 * @hopcount: Number of hops to the device
513 * Returns total numbers of ports implemented by the switch device.
515 static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
516 u8 hopcount)
518 u32 result;
520 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
521 &result);
523 return RIO_GET_TOTAL_PORTS(result);
527 * rio_net_add_mport- Add a master port to a RIO network
528 * @net: RIO network
529 * @port: Master port to add
531 * Adds a master port to the network list of associated master
532 * ports..
534 static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
536 spin_lock(&rio_global_list_lock);
537 list_add_tail(&port->nnode, &net->mports);
538 spin_unlock(&rio_global_list_lock);
542 * rio_enum_peer- Recursively enumerate a RIO network through a master port
543 * @net: RIO network being enumerated
544 * @port: Master port to send transactions
545 * @hopcount: Number of hops into the network
547 * Recursively enumerates a RIO network. Transactions are sent via the
548 * master port passed in @port.
550 static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
551 u8 hopcount)
553 int port_num;
554 int num_ports;
555 int cur_destid;
556 struct rio_dev *rdev;
557 u16 destid;
558 int tmp;
560 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
561 pr_debug("RIO: PE already discovered by this host\n");
563 * Already discovered by this host. Add it as another
564 * master port for the current network.
566 rio_net_add_mport(net, port);
567 return 0;
570 /* Attempt to acquire device lock */
571 rio_mport_write_config_32(port, RIO_ANY_DESTID, hopcount,
572 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
573 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
574 < port->host_deviceid) {
575 /* Delay a bit */
576 mdelay(1);
577 /* Attempt to acquire device lock again */
578 rio_mport_write_config_32(port, RIO_ANY_DESTID, hopcount,
579 RIO_HOST_DID_LOCK_CSR,
580 port->host_deviceid);
583 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
584 pr_debug(
585 "RIO: PE locked by a higher priority host...retreating\n");
586 return -1;
589 /* Setup new RIO device */
590 if ((rdev = rio_setup_device(net, port, RIO_ANY_DESTID, hopcount, 1))) {
591 /* Add device to the global and bus/net specific list. */
592 list_add_tail(&rdev->net_list, &net->devices);
593 } else
594 return -1;
596 if (rio_is_switch(rdev)) {
597 next_switchid++;
599 for (destid = 0; destid < next_destid; destid++) {
600 rio_route_add_entry(port, rdev, RIO_GLOBAL_TABLE,
601 destid, rio_get_swpinfo_inport(port,
602 RIO_ANY_DESTID,
603 hopcount));
604 rdev->rswitch->route_table[destid] =
605 rio_get_swpinfo_inport(port, RIO_ANY_DESTID,
606 hopcount);
609 num_ports =
610 rio_get_swpinfo_tports(port, RIO_ANY_DESTID, hopcount);
611 pr_debug(
612 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
613 rio_name(rdev), rdev->vid, rdev->did, num_ports);
614 for (port_num = 0; port_num < num_ports; port_num++) {
615 if (rio_get_swpinfo_inport
616 (port, RIO_ANY_DESTID, hopcount) == port_num)
617 continue;
619 cur_destid = next_destid;
621 if (rio_sport_is_active
622 (port, RIO_ANY_DESTID, hopcount, port_num)) {
623 pr_debug(
624 "RIO: scanning device on port %d\n",
625 port_num);
626 rio_route_add_entry(port, rdev,
627 RIO_GLOBAL_TABLE,
628 RIO_ANY_DESTID, port_num);
630 if (rio_enum_peer(net, port, hopcount + 1) < 0)
631 return -1;
633 /* Update routing tables */
634 if (next_destid > cur_destid) {
635 for (destid = cur_destid;
636 destid < next_destid; destid++) {
637 rio_route_add_entry(port, rdev,
638 RIO_GLOBAL_TABLE,
639 destid,
640 port_num);
641 rdev->rswitch->
642 route_table[destid] =
643 port_num;
645 rdev->rswitch->destid = cur_destid;
649 } else
650 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
651 rio_name(rdev), rdev->vid, rdev->did);
653 return 0;
657 * rio_enum_complete- Tests if enumeration of a network is complete
658 * @port: Master port to send transaction
660 * Tests the Component Tag CSR for presence of the magic enumeration
661 * complete flag. Return %1 if enumeration is complete or %0 if
662 * enumeration is incomplete.
664 static int rio_enum_complete(struct rio_mport *port)
666 u32 tag_csr;
667 int ret = 0;
669 rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
671 if (tag_csr == RIO_ENUM_CMPL_MAGIC)
672 ret = 1;
674 return ret;
678 * rio_disc_peer- Recursively discovers a RIO network through a master port
679 * @net: RIO network being discovered
680 * @port: Master port to send transactions
681 * @destid: Current destination ID in network
682 * @hopcount: Number of hops into the network
684 * Recursively discovers a RIO network. Transactions are sent via the
685 * master port passed in @port.
687 static int
688 rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
689 u8 hopcount)
691 u8 port_num, route_port;
692 int num_ports;
693 struct rio_dev *rdev;
694 u16 ndestid;
696 /* Setup new RIO device */
697 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
698 /* Add device to the global and bus/net specific list. */
699 list_add_tail(&rdev->net_list, &net->devices);
700 } else
701 return -1;
703 if (rio_is_switch(rdev)) {
704 next_switchid++;
706 /* Associated destid is how we accessed this switch */
707 rdev->rswitch->destid = destid;
709 num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
710 pr_debug(
711 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
712 rio_name(rdev), rdev->vid, rdev->did, num_ports);
713 for (port_num = 0; port_num < num_ports; port_num++) {
714 if (rio_get_swpinfo_inport(port, destid, hopcount) ==
715 port_num)
716 continue;
718 if (rio_sport_is_active
719 (port, destid, hopcount, port_num)) {
720 pr_debug(
721 "RIO: scanning device on port %d\n",
722 port_num);
723 for (ndestid = 0; ndestid < RIO_ANY_DESTID;
724 ndestid++) {
725 rio_route_get_entry(port, rdev,
726 RIO_GLOBAL_TABLE,
727 ndestid,
728 &route_port);
729 if (route_port == port_num)
730 break;
733 if (rio_disc_peer
734 (net, port, ndestid, hopcount + 1) < 0)
735 return -1;
738 } else
739 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
740 rio_name(rdev), rdev->vid, rdev->did);
742 return 0;
746 * rio_mport_is_active- Tests if master port link is active
747 * @port: Master port to test
749 * Reads the port error status CSR for the master port to
750 * determine if the port has an active link. Returns
751 * %PORT_N_ERR_STS_PORT_OK if the master port is active
752 * or %0 if it is inactive.
754 static int rio_mport_is_active(struct rio_mport *port)
756 u32 result = 0;
757 u32 ext_ftr_ptr;
758 int *entry = rio_mport_phys_table;
760 do {
761 if ((ext_ftr_ptr =
762 rio_mport_get_feature(port, 1, 0, 0, *entry)))
763 break;
764 } while (*++entry >= 0);
766 if (ext_ftr_ptr)
767 rio_local_read_config_32(port,
768 ext_ftr_ptr +
769 RIO_PORT_N_ERR_STS_CSR(port->index),
770 &result);
772 return (result & PORT_N_ERR_STS_PORT_OK);
776 * rio_alloc_net- Allocate and configure a new RIO network
777 * @port: Master port associated with the RIO network
779 * Allocates a RIO network structure, initializes per-network
780 * list heads, and adds the associated master port to the
781 * network list of associated master ports. Returns a
782 * RIO network pointer on success or %NULL on failure.
784 static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
786 struct rio_net *net;
788 net = kmalloc(sizeof(struct rio_net), GFP_KERNEL);
789 if (net) {
790 memset(net, 0, sizeof(struct rio_net));
791 INIT_LIST_HEAD(&net->node);
792 INIT_LIST_HEAD(&net->devices);
793 INIT_LIST_HEAD(&net->mports);
794 list_add_tail(&port->nnode, &net->mports);
795 net->hport = port;
796 net->id = next_net++;
798 return net;
802 * rio_enum_mport- Start enumeration through a master port
803 * @mport: Master port to send transactions
805 * Starts the enumeration process. If somebody has enumerated our
806 * master port device, then give up. If not and we have an active
807 * link, then start recursive peer enumeration. Returns %0 if
808 * enumeration succeeds or %-EBUSY if enumeration fails.
810 int rio_enum_mport(struct rio_mport *mport)
812 struct rio_net *net = NULL;
813 int rc = 0;
815 printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
816 mport->name);
817 /* If somebody else enumerated our master port device, bail. */
818 if (rio_enum_host(mport) < 0) {
819 printk(KERN_INFO
820 "RIO: master port %d device has been enumerated by a remote host\n",
821 mport->id);
822 rc = -EBUSY;
823 goto out;
826 /* If master port has an active link, allocate net and enum peers */
827 if (rio_mport_is_active(mport)) {
828 if (!(net = rio_alloc_net(mport))) {
829 printk(KERN_ERR "RIO: failed to allocate new net\n");
830 rc = -ENOMEM;
831 goto out;
833 if (rio_enum_peer(net, mport, 0) < 0) {
834 /* A higher priority host won enumeration, bail. */
835 printk(KERN_INFO
836 "RIO: master port %d device has lost enumeration to a remote host\n",
837 mport->id);
838 rio_clear_locks(mport);
839 rc = -EBUSY;
840 goto out;
842 rio_clear_locks(mport);
843 } else {
844 printk(KERN_INFO "RIO: master port %d link inactive\n",
845 mport->id);
846 rc = -EINVAL;
849 out:
850 return rc;
854 * rio_build_route_tables- Generate route tables from switch route entries
856 * For each switch device, generate a route table by copying existing
857 * route entries from the switch.
859 static void rio_build_route_tables(void)
861 struct rio_dev *rdev;
862 int i;
863 u8 sport;
865 list_for_each_entry(rdev, &rio_devices, global_list)
866 if (rio_is_switch(rdev))
867 for (i = 0; i < RIO_MAX_ROUTE_ENTRIES; i++) {
868 if (rio_route_get_entry
869 (rdev->net->hport, rdev, RIO_GLOBAL_TABLE, i,
870 &sport) < 0)
871 continue;
872 rdev->rswitch->route_table[i] = sport;
877 * rio_enum_timeout- Signal that enumeration timed out
878 * @data: Address of timeout flag.
880 * When the enumeration complete timer expires, set a flag that
881 * signals to the discovery process that enumeration did not
882 * complete in a sane amount of time.
884 static void rio_enum_timeout(unsigned long data)
886 /* Enumeration timed out, set flag */
887 *(int *)data = 1;
891 * rio_disc_mport- Start discovery through a master port
892 * @mport: Master port to send transactions
894 * Starts the discovery process. If we have an active link,
895 * then wait for the signal that enumeration is complete.
896 * When enumeration completion is signaled, start recursive
897 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
898 * on failure.
900 int rio_disc_mport(struct rio_mport *mport)
902 struct rio_net *net = NULL;
903 int enum_timeout_flag = 0;
905 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
906 mport->name);
908 /* If master port has an active link, allocate net and discover peers */
909 if (rio_mport_is_active(mport)) {
910 if (!(net = rio_alloc_net(mport))) {
911 printk(KERN_ERR "RIO: Failed to allocate new net\n");
912 goto bail;
915 pr_debug("RIO: wait for enumeration complete...");
917 rio_enum_timer.expires =
918 jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
919 rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
920 add_timer(&rio_enum_timer);
921 while (!rio_enum_complete(mport)) {
922 mdelay(1);
923 if (enum_timeout_flag) {
924 del_timer_sync(&rio_enum_timer);
925 goto timeout;
928 del_timer_sync(&rio_enum_timer);
930 pr_debug("done\n");
931 if (rio_disc_peer(net, mport, RIO_ANY_DESTID, 0) < 0) {
932 printk(KERN_INFO
933 "RIO: master port %d device has failed discovery\n",
934 mport->id);
935 goto bail;
938 rio_build_route_tables();
941 return 0;
943 timeout:
944 pr_debug("timeout\n");
945 bail:
946 return -EBUSY;