blob: 7bf7b2c88245358e023b1ddc8d97a19999b1a047 [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
Matt Portereb188d02005-11-07 01:00:17 -080013#include <linux/types.h>
14#include <linux/kernel.h>
15
16#include <linux/delay.h>
Matt Porterfa78cc52005-11-07 01:00:18 -080017#include <linux/dma-mapping.h>
Matt Portereb188d02005-11-07 01:00:17 -080018#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>
Tim Schmielaude259682006-01-08 01:02:05 -080026#include <linux/jiffies.h>
27#include <linux/slab.h>
Matt Portereb188d02005-11-07 01:00:17 -080028
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
Matt Porterfa78cc52005-11-07 01:00:18 -080038DEFINE_SPINLOCK(rio_global_list_lock);
39
Matt Portereb188d02005-11-07 01:00:17 -080040static 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 -1,
59};
60
Matt Portereb188d02005-11-07 01:00:17 -080061/**
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 */
70static 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 */
Matt Porterfa78cc52005-11-07 01:00:18 -080088static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
Matt Portereb188d02005-11-07 01:00:17 -080089{
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 */
101static 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 */
114static 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 */
160static 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 */
191static int rio_device_has_destid(struct rio_mport *port, int src_ops,
192 int dst_ops)
193{
Matt Porterfa78cc52005-11-07 01:00:18 -0800194 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);
Matt Portereb188d02005-11-07 01:00:17 -0800197}
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 */
206static 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 */
223static 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 */
238static 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 */
265static 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 */
292static 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 = kmalloc(sizeof(struct rio_dev), GFP_KERNEL);
301 if (!rdev)
302 goto out;
303
304 memset(rdev, 0, sizeof(struct rio_dev));
305 rdev->net = net;
306 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
307 &result);
308 rdev->did = result >> 16;
309 rdev->vid = result & 0xffff;
310 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
311 &rdev->device_rev);
312 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
313 &result);
314 rdev->asm_did = result >> 16;
315 rdev->asm_vid = result & 0xffff;
316 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
317 &result);
318 rdev->asm_rev = result >> 16;
319 rio_mport_read_config_32(port, destid, hopcount, RIO_PEF_CAR,
320 &rdev->pef);
321 if (rdev->pef & RIO_PEF_EXT_FEATURES)
322 rdev->efptr = result & 0xffff;
323
324 rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
325 &rdev->src_ops);
326 rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
327 &rdev->dst_ops);
328
329 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)
330 && do_enum) {
331 rio_set_device_id(port, destid, hopcount, next_destid);
332 rdev->destid = next_destid++;
333 if (next_destid == port->host_deviceid)
334 next_destid++;
335 } else
336 rdev->destid = rio_get_device_id(port, destid, hopcount);
337
338 /* If a PE has both switch and other functions, show it as a switch */
339 if (rio_is_switch(rdev)) {
340 rio_mport_read_config_32(port, destid, hopcount,
341 RIO_SWP_INFO_CAR, &rdev->swpinfo);
342 rswitch = kmalloc(sizeof(struct rio_switch), GFP_KERNEL);
343 if (!rswitch) {
344 kfree(rdev);
345 rdev = NULL;
346 goto out;
347 }
348 rswitch->switchid = next_switchid;
349 rswitch->hopcount = hopcount;
350 rswitch->destid = 0xffff;
351 /* Initialize switch route table */
352 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES; rdid++)
353 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
354 rdev->rswitch = rswitch;
355 sprintf(rio_name(rdev), "%02x:s:%04x", rdev->net->id,
356 rdev->rswitch->switchid);
357 rio_route_set_ops(rdev);
358
359 list_add_tail(&rswitch->node, &rio_switches);
360
361 } else
362 sprintf(rio_name(rdev), "%02x:e:%04x", rdev->net->id,
363 rdev->destid);
364
365 rdev->dev.bus = &rio_bus_type;
366
367 device_initialize(&rdev->dev);
368 rdev->dev.release = rio_release_dev;
369 rio_dev_get(rdev);
370
Matt Porterfa78cc52005-11-07 01:00:18 -0800371 rdev->dma_mask = DMA_32BIT_MASK;
372 rdev->dev.dma_mask = &rdev->dma_mask;
373 rdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
Matt Portereb188d02005-11-07 01:00:17 -0800374
375 if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
376 (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
377 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
378 0, 0xffff);
379
380 rio_add_device(rdev);
381
382 out:
383 return rdev;
384}
385
386/**
387 * rio_sport_is_active- Tests if a switch port has an active connection.
388 * @port: Master port to send transaction
389 * @destid: Associated destination ID for switch
390 * @hopcount: Hopcount to reach switch
391 * @sport: Switch port number
392 *
393 * Reads the port error status CSR for a particular switch port to
394 * determine if the port has an active link. Returns
395 * %PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
396 * inactive.
397 */
398static int
399rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
400{
401 u32 result;
402 u32 ext_ftr_ptr;
403
404 int *entry = rio_sport_phys_table;
405
406 do {
407 if ((ext_ftr_ptr =
408 rio_mport_get_feature(port, 0, destid, hopcount, *entry)))
409
410 break;
411 } while (*++entry >= 0);
412
413 if (ext_ftr_ptr)
414 rio_mport_read_config_32(port, destid, hopcount,
415 ext_ftr_ptr +
416 RIO_PORT_N_ERR_STS_CSR(sport),
417 &result);
418
419 return (result & PORT_N_ERR_STS_PORT_OK);
420}
421
422/**
423 * rio_route_add_entry- Add a route entry to a switch routing table
424 * @mport: Master port to send transaction
425 * @rdev: Switch device
426 * @table: Routing table ID
427 * @route_destid: Destination ID to be routed
428 * @route_port: Port number to be routed
429 *
430 * Calls the switch specific add_entry() method to add a route entry
431 * on a switch. The route table can be specified using the @table
432 * argument if a switch has per port routing tables or the normal
433 * use is to specific all tables (or the global table) by passing
434 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
435 * on failure.
436 */
437static int rio_route_add_entry(struct rio_mport *mport, struct rio_dev *rdev,
438 u16 table, u16 route_destid, u8 route_port)
439{
440 return rdev->rswitch->add_entry(mport, rdev->rswitch->destid,
441 rdev->rswitch->hopcount, table,
442 route_destid, route_port);
443}
444
445/**
446 * rio_route_get_entry- Read a route entry in a switch routing table
447 * @mport: Master port to send transaction
448 * @rdev: Switch device
449 * @table: Routing table ID
450 * @route_destid: Destination ID to be routed
451 * @route_port: Pointer to read port number into
452 *
453 * Calls the switch specific get_entry() method to read a route entry
454 * in a switch. The route table can be specified using the @table
455 * argument if a switch has per port routing tables or the normal
456 * use is to specific all tables (or the global table) by passing
457 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
458 * on failure.
459 */
460static int
461rio_route_get_entry(struct rio_mport *mport, struct rio_dev *rdev, u16 table,
462 u16 route_destid, u8 * route_port)
463{
464 return rdev->rswitch->get_entry(mport, rdev->rswitch->destid,
465 rdev->rswitch->hopcount, table,
466 route_destid, route_port);
467}
468
469/**
470 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
471 * @port: Master port to send transaction
472 * @hopcount: Number of hops to the device
473 *
474 * Used during enumeration to read the Host Device ID Lock CSR on a
475 * RIO device. Returns the value of the lock register.
476 */
477static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
478{
479 u32 result;
480
481 rio_mport_read_config_32(port, RIO_ANY_DESTID, hopcount,
482 RIO_HOST_DID_LOCK_CSR, &result);
483
484 return (u16) (result & 0xffff);
485}
486
487/**
488 * rio_get_swpinfo_inport- Gets the ingress port number
489 * @mport: Master port to send transaction
490 * @destid: Destination ID associated with the switch
491 * @hopcount: Number of hops to the device
492 *
493 * Returns port number being used to access the switch device.
494 */
495static u8
496rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
497{
498 u32 result;
499
500 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
501 &result);
502
503 return (u8) (result & 0xff);
504}
505
506/**
507 * rio_get_swpinfo_tports- Gets total number of ports on the switch
508 * @mport: Master port to send transaction
509 * @destid: Destination ID associated with the switch
510 * @hopcount: Number of hops to the device
511 *
512 * Returns total numbers of ports implemented by the switch device.
513 */
514static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
515 u8 hopcount)
516{
517 u32 result;
518
519 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
520 &result);
521
522 return RIO_GET_TOTAL_PORTS(result);
523}
524
525/**
526 * rio_net_add_mport- Add a master port to a RIO network
527 * @net: RIO network
528 * @port: Master port to add
529 *
530 * Adds a master port to the network list of associated master
531 * ports..
532 */
533static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
534{
535 spin_lock(&rio_global_list_lock);
536 list_add_tail(&port->nnode, &net->mports);
537 spin_unlock(&rio_global_list_lock);
538}
539
540/**
541 * rio_enum_peer- Recursively enumerate a RIO network through a master port
542 * @net: RIO network being enumerated
543 * @port: Master port to send transactions
544 * @hopcount: Number of hops into the network
545 *
546 * Recursively enumerates a RIO network. Transactions are sent via the
547 * master port passed in @port.
548 */
549static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
550 u8 hopcount)
551{
552 int port_num;
553 int num_ports;
554 int cur_destid;
555 struct rio_dev *rdev;
556 u16 destid;
557 int tmp;
558
559 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
560 pr_debug("RIO: PE already discovered by this host\n");
561 /*
562 * Already discovered by this host. Add it as another
563 * master port for the current network.
564 */
565 rio_net_add_mport(net, port);
566 return 0;
567 }
568
569 /* Attempt to acquire device lock */
570 rio_mport_write_config_32(port, RIO_ANY_DESTID, hopcount,
571 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
572 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
573 < port->host_deviceid) {
574 /* Delay a bit */
575 mdelay(1);
576 /* Attempt to acquire device lock again */
577 rio_mport_write_config_32(port, RIO_ANY_DESTID, hopcount,
578 RIO_HOST_DID_LOCK_CSR,
579 port->host_deviceid);
580 }
581
582 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
583 pr_debug(
584 "RIO: PE locked by a higher priority host...retreating\n");
585 return -1;
586 }
587
588 /* Setup new RIO device */
589 if ((rdev = rio_setup_device(net, port, RIO_ANY_DESTID, hopcount, 1))) {
590 /* Add device to the global and bus/net specific list. */
591 list_add_tail(&rdev->net_list, &net->devices);
592 } else
593 return -1;
594
595 if (rio_is_switch(rdev)) {
596 next_switchid++;
597
598 for (destid = 0; destid < next_destid; destid++) {
599 rio_route_add_entry(port, rdev, RIO_GLOBAL_TABLE,
600 destid, rio_get_swpinfo_inport(port,
601 RIO_ANY_DESTID,
602 hopcount));
603 rdev->rswitch->route_table[destid] =
604 rio_get_swpinfo_inport(port, RIO_ANY_DESTID,
605 hopcount);
606 }
607
608 num_ports =
609 rio_get_swpinfo_tports(port, RIO_ANY_DESTID, hopcount);
610 pr_debug(
611 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
612 rio_name(rdev), rdev->vid, rdev->did, num_ports);
613 for (port_num = 0; port_num < num_ports; port_num++) {
614 if (rio_get_swpinfo_inport
615 (port, RIO_ANY_DESTID, hopcount) == port_num)
616 continue;
617
618 cur_destid = next_destid;
619
620 if (rio_sport_is_active
621 (port, RIO_ANY_DESTID, hopcount, port_num)) {
622 pr_debug(
623 "RIO: scanning device on port %d\n",
624 port_num);
625 rio_route_add_entry(port, rdev,
626 RIO_GLOBAL_TABLE,
627 RIO_ANY_DESTID, port_num);
628
629 if (rio_enum_peer(net, port, hopcount + 1) < 0)
630 return -1;
631
632 /* Update routing tables */
633 if (next_destid > cur_destid) {
634 for (destid = cur_destid;
635 destid < next_destid; destid++) {
636 rio_route_add_entry(port, rdev,
637 RIO_GLOBAL_TABLE,
638 destid,
639 port_num);
640 rdev->rswitch->
641 route_table[destid] =
642 port_num;
643 }
644 rdev->rswitch->destid = cur_destid;
645 }
646 }
647 }
648 } else
649 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
650 rio_name(rdev), rdev->vid, rdev->did);
651
652 return 0;
653}
654
655/**
656 * rio_enum_complete- Tests if enumeration of a network is complete
657 * @port: Master port to send transaction
658 *
659 * Tests the Component Tag CSR for presence of the magic enumeration
660 * complete flag. Return %1 if enumeration is complete or %0 if
661 * enumeration is incomplete.
662 */
663static int rio_enum_complete(struct rio_mport *port)
664{
665 u32 tag_csr;
666 int ret = 0;
667
668 rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
669
670 if (tag_csr == RIO_ENUM_CMPL_MAGIC)
671 ret = 1;
672
673 return ret;
674}
675
676/**
677 * rio_disc_peer- Recursively discovers a RIO network through a master port
678 * @net: RIO network being discovered
679 * @port: Master port to send transactions
680 * @destid: Current destination ID in network
681 * @hopcount: Number of hops into the network
682 *
683 * Recursively discovers a RIO network. Transactions are sent via the
684 * master port passed in @port.
685 */
686static int
687rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
688 u8 hopcount)
689{
690 u8 port_num, route_port;
691 int num_ports;
692 struct rio_dev *rdev;
693 u16 ndestid;
694
695 /* Setup new RIO device */
696 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
697 /* Add device to the global and bus/net specific list. */
698 list_add_tail(&rdev->net_list, &net->devices);
699 } else
700 return -1;
701
702 if (rio_is_switch(rdev)) {
703 next_switchid++;
704
705 /* Associated destid is how we accessed this switch */
706 rdev->rswitch->destid = destid;
707
708 num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
709 pr_debug(
710 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
711 rio_name(rdev), rdev->vid, rdev->did, num_ports);
712 for (port_num = 0; port_num < num_ports; port_num++) {
713 if (rio_get_swpinfo_inport(port, destid, hopcount) ==
714 port_num)
715 continue;
716
717 if (rio_sport_is_active
718 (port, destid, hopcount, port_num)) {
719 pr_debug(
720 "RIO: scanning device on port %d\n",
721 port_num);
722 for (ndestid = 0; ndestid < RIO_ANY_DESTID;
723 ndestid++) {
724 rio_route_get_entry(port, rdev,
725 RIO_GLOBAL_TABLE,
726 ndestid,
727 &route_port);
728 if (route_port == port_num)
729 break;
730 }
731
732 if (rio_disc_peer
733 (net, port, ndestid, hopcount + 1) < 0)
734 return -1;
735 }
736 }
737 } else
738 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
739 rio_name(rdev), rdev->vid, rdev->did);
740
741 return 0;
742}
743
744/**
745 * rio_mport_is_active- Tests if master port link is active
746 * @port: Master port to test
747 *
748 * Reads the port error status CSR for the master port to
749 * determine if the port has an active link. Returns
750 * %PORT_N_ERR_STS_PORT_OK if the master port is active
751 * or %0 if it is inactive.
752 */
753static int rio_mport_is_active(struct rio_mport *port)
754{
755 u32 result = 0;
756 u32 ext_ftr_ptr;
757 int *entry = rio_mport_phys_table;
758
759 do {
760 if ((ext_ftr_ptr =
761 rio_mport_get_feature(port, 1, 0, 0, *entry)))
762 break;
763 } while (*++entry >= 0);
764
765 if (ext_ftr_ptr)
766 rio_local_read_config_32(port,
767 ext_ftr_ptr +
768 RIO_PORT_N_ERR_STS_CSR(port->index),
769 &result);
770
771 return (result & PORT_N_ERR_STS_PORT_OK);
772}
773
774/**
775 * rio_alloc_net- Allocate and configure a new RIO network
776 * @port: Master port associated with the RIO network
777 *
778 * Allocates a RIO network structure, initializes per-network
779 * list heads, and adds the associated master port to the
780 * network list of associated master ports. Returns a
781 * RIO network pointer on success or %NULL on failure.
782 */
783static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
784{
785 struct rio_net *net;
786
787 net = kmalloc(sizeof(struct rio_net), GFP_KERNEL);
788 if (net) {
789 memset(net, 0, sizeof(struct rio_net));
790 INIT_LIST_HEAD(&net->node);
791 INIT_LIST_HEAD(&net->devices);
792 INIT_LIST_HEAD(&net->mports);
793 list_add_tail(&port->nnode, &net->mports);
794 net->hport = port;
795 net->id = next_net++;
796 }
797 return net;
798}
799
800/**
801 * rio_enum_mport- Start enumeration through a master port
802 * @mport: Master port to send transactions
803 *
804 * Starts the enumeration process. If somebody has enumerated our
805 * master port device, then give up. If not and we have an active
806 * link, then start recursive peer enumeration. Returns %0 if
807 * enumeration succeeds or %-EBUSY if enumeration fails.
808 */
809int rio_enum_mport(struct rio_mport *mport)
810{
811 struct rio_net *net = NULL;
812 int rc = 0;
813
814 printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
815 mport->name);
816 /* If somebody else enumerated our master port device, bail. */
817 if (rio_enum_host(mport) < 0) {
818 printk(KERN_INFO
819 "RIO: master port %d device has been enumerated by a remote host\n",
820 mport->id);
821 rc = -EBUSY;
822 goto out;
823 }
824
825 /* If master port has an active link, allocate net and enum peers */
826 if (rio_mport_is_active(mport)) {
827 if (!(net = rio_alloc_net(mport))) {
828 printk(KERN_ERR "RIO: failed to allocate new net\n");
829 rc = -ENOMEM;
830 goto out;
831 }
832 if (rio_enum_peer(net, mport, 0) < 0) {
833 /* A higher priority host won enumeration, bail. */
834 printk(KERN_INFO
835 "RIO: master port %d device has lost enumeration to a remote host\n",
836 mport->id);
837 rio_clear_locks(mport);
838 rc = -EBUSY;
839 goto out;
840 }
841 rio_clear_locks(mport);
842 } else {
843 printk(KERN_INFO "RIO: master port %d link inactive\n",
844 mport->id);
845 rc = -EINVAL;
846 }
847
848 out:
849 return rc;
850}
851
852/**
853 * rio_build_route_tables- Generate route tables from switch route entries
854 *
855 * For each switch device, generate a route table by copying existing
856 * route entries from the switch.
857 */
858static void rio_build_route_tables(void)
859{
860 struct rio_dev *rdev;
861 int i;
862 u8 sport;
863
864 list_for_each_entry(rdev, &rio_devices, global_list)
865 if (rio_is_switch(rdev))
866 for (i = 0; i < RIO_MAX_ROUTE_ENTRIES; i++) {
867 if (rio_route_get_entry
868 (rdev->net->hport, rdev, RIO_GLOBAL_TABLE, i,
869 &sport) < 0)
870 continue;
871 rdev->rswitch->route_table[i] = sport;
872 }
873}
874
875/**
876 * rio_enum_timeout- Signal that enumeration timed out
877 * @data: Address of timeout flag.
878 *
879 * When the enumeration complete timer expires, set a flag that
880 * signals to the discovery process that enumeration did not
881 * complete in a sane amount of time.
882 */
883static void rio_enum_timeout(unsigned long data)
884{
885 /* Enumeration timed out, set flag */
886 *(int *)data = 1;
887}
888
889/**
890 * rio_disc_mport- Start discovery through a master port
891 * @mport: Master port to send transactions
892 *
893 * Starts the discovery process. If we have an active link,
894 * then wait for the signal that enumeration is complete.
895 * When enumeration completion is signaled, start recursive
896 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
897 * on failure.
898 */
899int rio_disc_mport(struct rio_mport *mport)
900{
901 struct rio_net *net = NULL;
902 int enum_timeout_flag = 0;
903
904 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
905 mport->name);
906
907 /* If master port has an active link, allocate net and discover peers */
908 if (rio_mport_is_active(mport)) {
909 if (!(net = rio_alloc_net(mport))) {
910 printk(KERN_ERR "RIO: Failed to allocate new net\n");
911 goto bail;
912 }
913
914 pr_debug("RIO: wait for enumeration complete...");
915
916 rio_enum_timer.expires =
917 jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
918 rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
919 add_timer(&rio_enum_timer);
920 while (!rio_enum_complete(mport)) {
921 mdelay(1);
922 if (enum_timeout_flag) {
923 del_timer_sync(&rio_enum_timer);
924 goto timeout;
925 }
926 }
927 del_timer_sync(&rio_enum_timer);
928
929 pr_debug("done\n");
930 if (rio_disc_peer(net, mport, RIO_ANY_DESTID, 0) < 0) {
931 printk(KERN_INFO
932 "RIO: master port %d device has failed discovery\n",
933 mport->id);
934 goto bail;
935 }
936
937 rio_build_route_tables();
938 }
939
940 return 0;
941
942 timeout:
943 pr_debug("timeout\n");
944 bail:
945 return -EBUSY;
946}