blob: 94e30fe4b8f3242b17f8adc5c1623669d2a513b6 [file] [log] [blame]
Matt Portereb188d02005-11-07 01:00:17 -08001/*
2 * RapidIO enumeration and discovery support
3 *
4 * Copyright 2005 MontaVista Software, Inc.
5 * Matt Porter <mporter@kernel.crashing.org>
6 *
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/config.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
16
17#include <linux/delay.h>
Matt Porterfa78cc52005-11-07 01:00:18 -080018#include <linux/dma-mapping.h>
Matt Portereb188d02005-11-07 01:00:17 -080019#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>
Tim Schmielaude259682006-01-08 01:02:05 -080027#include <linux/jiffies.h>
28#include <linux/slab.h>
Matt Portereb188d02005-11-07 01:00:17 -080029
30#include "rio.h"
31
32LIST_HEAD(rio_devices);
33static LIST_HEAD(rio_switches);
34
35#define RIO_ENUM_CMPL_MAGIC 0xdeadbeef
36
37static void rio_enum_timeout(unsigned long);
38
Matt Porterfa78cc52005-11-07 01:00:18 -080039DEFINE_SPINLOCK(rio_global_list_lock);
40
Matt Portereb188d02005-11-07 01:00:17 -080041static int next_destid = 0;
42static int next_switchid = 0;
43static int next_net = 0;
44
45static struct timer_list rio_enum_timer =
46TIMER_INITIALIZER(rio_enum_timeout, 0, 0);
47
48static 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,
54};
55
56static int rio_sport_phys_table[] = {
57 RIO_EFB_PAR_EP_FREE_ID,
58 RIO_EFB_SER_EP_FREE_ID,
59 -1,
60};
61
Matt Portereb188d02005-11-07 01:00:17 -080062/**
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(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 */
Matt Porterfa78cc52005-11-07 01:00:18 -080089static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
Matt Portereb188d02005-11-07 01:00:17 -080090{
91 rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
92 RIO_SET_DID(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(did));
105}
106
107/**
108 * rio_clear_locks- Release all host locks and signal enumeration complete
109 * @port: Master port to issue transaction
110 *
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.
114 */
115static int rio_clear_locks(struct rio_mport *port)
116{
117 struct rio_dev *rdev;
118 u32 result;
119 int ret = 0;
120
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);
127
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;
137 }
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;
147 }
148 }
149
150 return ret;
151}
152
153/**
154 * rio_enum_host- Set host lock and initialize host destination ID
155 * @port: Master port to issue transaction
156 *
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.
160 */
161static int rio_enum_host(struct rio_mport *port)
162{
163 u32 result;
164
165 /* Set master port host device id lock */
166 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
167 port->host_deviceid);
168
169 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
170 if ((result & 0xffff) != port->host_deviceid)
171 return -1;
172
173 /* Set master port destid and init destid ctr */
174 rio_local_set_device_id(port, port->host_deviceid);
175
176 if (next_destid == port->host_deviceid)
177 next_destid++;
178
179 return 0;
180}
181
182/**
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
187 *
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.
191 */
192static int rio_device_has_destid(struct rio_mport *port, int src_ops,
193 int dst_ops)
194{
Matt Porterfa78cc52005-11-07 01:00:18 -0800195 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;
196
197 return !!((src_ops | dst_ops) & mask);
Matt Portereb188d02005-11-07 01:00:17 -0800198}
199
200/**
201 * rio_release_dev- Frees a RIO device struct
202 * @dev: LDM device associated with a RIO device struct
203 *
204 * Gets the RIO device struct associated a RIO device struct.
205 * The RIO device struct is freed.
206 */
207static void rio_release_dev(struct device *dev)
208{
209 struct rio_dev *rdev;
210
211 rdev = to_rio_dev(dev);
212 kfree(rdev);
213}
214
215/**
216 * rio_is_switch- Tests if a RIO device has switch capabilities
217 * @rdev: RIO device
218 *
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.
223 */
224static int rio_is_switch(struct rio_dev *rdev)
225{
226 if (rdev->pef & RIO_PEF_SWITCH)
227 return 1;
228 return 0;
229}
230
231/**
232 * rio_route_set_ops- Sets routing operations for a particular vendor switch
233 * @rdev: RIO device
234 *
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.
238 */
239static void rio_route_set_ops(struct rio_dev *rdev)
240{
241 struct rio_route_ops *cur = __start_rio_route_ops;
242 struct rio_route_ops *end = __end_rio_route_ops;
243
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;
249 }
250 cur++;
251 }
252
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));
256}
257
258/**
259 * rio_add_device- Adds a RIO device to the device model
260 * @rdev: RIO device
261 *
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.
265 */
266static void __devinit rio_add_device(struct rio_dev *rdev)
267{
268 device_add(&rdev->dev);
269
270 spin_lock(&rio_global_list_lock);
271 list_add_tail(&rdev->global_list, &rio_devices);
272 spin_unlock(&rio_global_list_lock);
273
274 rio_create_sysfs_dev_files(rdev);
275}
276
277/**
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
284 *
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.
291 *
292 */
293static struct rio_dev *rio_setup_device(struct rio_net *net,
294 struct rio_mport *port, u16 destid,
295 u8 hopcount, int do_enum)
296{
297 struct rio_dev *rdev;
298 struct rio_switch *rswitch;
299 int result, rdid;
300
301 rdev = kmalloc(sizeof(struct rio_dev), GFP_KERNEL);
302 if (!rdev)
303 goto out;
304
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;
324
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);
329
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);
338
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;
348 }
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);
359
360 list_add_tail(&rswitch->node, &rio_switches);
361
362 } else
363 sprintf(rio_name(rdev), "%02x:e:%04x", rdev->net->id,
364 rdev->destid);
365
366 rdev->dev.bus = &rio_bus_type;
367
368 device_initialize(&rdev->dev);
369 rdev->dev.release = rio_release_dev;
370 rio_dev_get(rdev);
371
Matt Porterfa78cc52005-11-07 01:00:18 -0800372 rdev->dma_mask = DMA_32BIT_MASK;
373 rdev->dev.dma_mask = &rdev->dma_mask;
374 rdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
Matt Portereb188d02005-11-07 01:00:17 -0800375
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);
380
381 rio_add_device(rdev);
382
383 out:
384 return rdev;
385}
386
387/**
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
393 *
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.
398 */
399static int
400rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
401{
402 u32 result;
403 u32 ext_ftr_ptr;
404
405 int *entry = rio_sport_phys_table;
406
407 do {
408 if ((ext_ftr_ptr =
409 rio_mport_get_feature(port, 0, destid, hopcount, *entry)))
410
411 break;
412 } while (*++entry >= 0);
413
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);
419
420 return (result & PORT_N_ERR_STS_PORT_OK);
421}
422
423/**
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
430 *
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.
437 */
438static int rio_route_add_entry(struct rio_mport *mport, struct rio_dev *rdev,
439 u16 table, u16 route_destid, u8 route_port)
440{
441 return rdev->rswitch->add_entry(mport, rdev->rswitch->destid,
442 rdev->rswitch->hopcount, table,
443 route_destid, route_port);
444}
445
446/**
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
453 *
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.
460 */
461static int
462rio_route_get_entry(struct rio_mport *mport, struct rio_dev *rdev, u16 table,
463 u16 route_destid, u8 * route_port)
464{
465 return rdev->rswitch->get_entry(mport, rdev->rswitch->destid,
466 rdev->rswitch->hopcount, table,
467 route_destid, route_port);
468}
469
470/**
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
474 *
475 * Used during enumeration to read the Host Device ID Lock CSR on a
476 * RIO device. Returns the value of the lock register.
477 */
478static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
479{
480 u32 result;
481
482 rio_mport_read_config_32(port, RIO_ANY_DESTID, hopcount,
483 RIO_HOST_DID_LOCK_CSR, &result);
484
485 return (u16) (result & 0xffff);
486}
487
488/**
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
493 *
494 * Returns port number being used to access the switch device.
495 */
496static u8
497rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
498{
499 u32 result;
500
501 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
502 &result);
503
504 return (u8) (result & 0xff);
505}
506
507/**
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
512 *
513 * Returns total numbers of ports implemented by the switch device.
514 */
515static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
516 u8 hopcount)
517{
518 u32 result;
519
520 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
521 &result);
522
523 return RIO_GET_TOTAL_PORTS(result);
524}
525
526/**
527 * rio_net_add_mport- Add a master port to a RIO network
528 * @net: RIO network
529 * @port: Master port to add
530 *
531 * Adds a master port to the network list of associated master
532 * ports..
533 */
534static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
535{
536 spin_lock(&rio_global_list_lock);
537 list_add_tail(&port->nnode, &net->mports);
538 spin_unlock(&rio_global_list_lock);
539}
540
541/**
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
546 *
547 * Recursively enumerates a RIO network. Transactions are sent via the
548 * master port passed in @port.
549 */
550static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
551 u8 hopcount)
552{
553 int port_num;
554 int num_ports;
555 int cur_destid;
556 struct rio_dev *rdev;
557 u16 destid;
558 int tmp;
559
560 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
561 pr_debug("RIO: PE already discovered by this host\n");
562 /*
563 * Already discovered by this host. Add it as another
564 * master port for the current network.
565 */
566 rio_net_add_mport(net, port);
567 return 0;
568 }
569
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);
581 }
582
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;
587 }
588
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;
595
596 if (rio_is_switch(rdev)) {
597 next_switchid++;
598
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);
607 }
608
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;
618
619 cur_destid = next_destid;
620
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);
629
630 if (rio_enum_peer(net, port, hopcount + 1) < 0)
631 return -1;
632
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;
644 }
645 rdev->rswitch->destid = cur_destid;
646 }
647 }
648 }
649 } else
650 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
651 rio_name(rdev), rdev->vid, rdev->did);
652
653 return 0;
654}
655
656/**
657 * rio_enum_complete- Tests if enumeration of a network is complete
658 * @port: Master port to send transaction
659 *
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.
663 */
664static int rio_enum_complete(struct rio_mport *port)
665{
666 u32 tag_csr;
667 int ret = 0;
668
669 rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
670
671 if (tag_csr == RIO_ENUM_CMPL_MAGIC)
672 ret = 1;
673
674 return ret;
675}
676
677/**
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
683 *
684 * Recursively discovers a RIO network. Transactions are sent via the
685 * master port passed in @port.
686 */
687static int
688rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
689 u8 hopcount)
690{
691 u8 port_num, route_port;
692 int num_ports;
693 struct rio_dev *rdev;
694 u16 ndestid;
695
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;
702
703 if (rio_is_switch(rdev)) {
704 next_switchid++;
705
706 /* Associated destid is how we accessed this switch */
707 rdev->rswitch->destid = destid;
708
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;
717
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;
731 }
732
733 if (rio_disc_peer
734 (net, port, ndestid, hopcount + 1) < 0)
735 return -1;
736 }
737 }
738 } else
739 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
740 rio_name(rdev), rdev->vid, rdev->did);
741
742 return 0;
743}
744
745/**
746 * rio_mport_is_active- Tests if master port link is active
747 * @port: Master port to test
748 *
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.
753 */
754static int rio_mport_is_active(struct rio_mport *port)
755{
756 u32 result = 0;
757 u32 ext_ftr_ptr;
758 int *entry = rio_mport_phys_table;
759
760 do {
761 if ((ext_ftr_ptr =
762 rio_mport_get_feature(port, 1, 0, 0, *entry)))
763 break;
764 } while (*++entry >= 0);
765
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);
771
772 return (result & PORT_N_ERR_STS_PORT_OK);
773}
774
775/**
776 * rio_alloc_net- Allocate and configure a new RIO network
777 * @port: Master port associated with the RIO network
778 *
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.
783 */
784static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
785{
786 struct rio_net *net;
787
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++;
797 }
798 return net;
799}
800
801/**
802 * rio_enum_mport- Start enumeration through a master port
803 * @mport: Master port to send transactions
804 *
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.
809 */
810int rio_enum_mport(struct rio_mport *mport)
811{
812 struct rio_net *net = NULL;
813 int rc = 0;
814
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;
824 }
825
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;
832 }
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;
841 }
842 rio_clear_locks(mport);
843 } else {
844 printk(KERN_INFO "RIO: master port %d link inactive\n",
845 mport->id);
846 rc = -EINVAL;
847 }
848
849 out:
850 return rc;
851}
852
853/**
854 * rio_build_route_tables- Generate route tables from switch route entries
855 *
856 * For each switch device, generate a route table by copying existing
857 * route entries from the switch.
858 */
859static void rio_build_route_tables(void)
860{
861 struct rio_dev *rdev;
862 int i;
863 u8 sport;
864
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;
873 }
874}
875
876/**
877 * rio_enum_timeout- Signal that enumeration timed out
878 * @data: Address of timeout flag.
879 *
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.
883 */
884static void rio_enum_timeout(unsigned long data)
885{
886 /* Enumeration timed out, set flag */
887 *(int *)data = 1;
888}
889
890/**
891 * rio_disc_mport- Start discovery through a master port
892 * @mport: Master port to send transactions
893 *
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.
899 */
900int rio_disc_mport(struct rio_mport *mport)
901{
902 struct rio_net *net = NULL;
903 int enum_timeout_flag = 0;
904
905 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
906 mport->name);
907
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;
913 }
914
915 pr_debug("RIO: wait for enumeration complete...");
916
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;
926 }
927 }
928 del_timer_sync(&rio_enum_timer);
929
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;
936 }
937
938 rio_build_route_tables();
939 }
940
941 return 0;
942
943 timeout:
944 pr_debug("timeout\n");
945 bail:
946 return -EBUSY;
947}