blob: f935c1f71a583db4dff6d14937e972096717f0b7 [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
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800329 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
330 if (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);
Matt Portereb188d02005-11-07 01:00:17 -0800337 } else
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800338 /* Switch device has an associated destID */
339 rdev->destid = RIO_INVALID_DESTID;
Matt Portereb188d02005-11-07 01:00:17 -0800340
341 /* If a PE has both switch and other functions, show it as a switch */
342 if (rio_is_switch(rdev)) {
343 rio_mport_read_config_32(port, destid, hopcount,
344 RIO_SWP_INFO_CAR, &rdev->swpinfo);
345 rswitch = kmalloc(sizeof(struct rio_switch), GFP_KERNEL);
346 if (!rswitch) {
347 kfree(rdev);
348 rdev = NULL;
349 goto out;
350 }
351 rswitch->switchid = next_switchid;
352 rswitch->hopcount = hopcount;
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800353 rswitch->destid = destid;
Matt Portereb188d02005-11-07 01:00:17 -0800354 /* Initialize switch route table */
355 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES; rdid++)
356 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
357 rdev->rswitch = rswitch;
358 sprintf(rio_name(rdev), "%02x:s:%04x", rdev->net->id,
359 rdev->rswitch->switchid);
360 rio_route_set_ops(rdev);
361
362 list_add_tail(&rswitch->node, &rio_switches);
363
364 } else
365 sprintf(rio_name(rdev), "%02x:e:%04x", rdev->net->id,
366 rdev->destid);
367
368 rdev->dev.bus = &rio_bus_type;
369
370 device_initialize(&rdev->dev);
371 rdev->dev.release = rio_release_dev;
372 rio_dev_get(rdev);
373
Matt Porterfa78cc52005-11-07 01:00:18 -0800374 rdev->dma_mask = DMA_32BIT_MASK;
375 rdev->dev.dma_mask = &rdev->dma_mask;
376 rdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
Matt Portereb188d02005-11-07 01:00:17 -0800377
378 if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
379 (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
380 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
381 0, 0xffff);
382
383 rio_add_device(rdev);
384
385 out:
386 return rdev;
387}
388
389/**
390 * rio_sport_is_active- Tests if a switch port has an active connection.
391 * @port: Master port to send transaction
392 * @destid: Associated destination ID for switch
393 * @hopcount: Hopcount to reach switch
394 * @sport: Switch port number
395 *
396 * Reads the port error status CSR for a particular switch port to
397 * determine if the port has an active link. Returns
398 * %PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
399 * inactive.
400 */
401static int
402rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
403{
404 u32 result;
405 u32 ext_ftr_ptr;
406
407 int *entry = rio_sport_phys_table;
408
409 do {
410 if ((ext_ftr_ptr =
411 rio_mport_get_feature(port, 0, destid, hopcount, *entry)))
412
413 break;
414 } while (*++entry >= 0);
415
416 if (ext_ftr_ptr)
417 rio_mport_read_config_32(port, destid, hopcount,
418 ext_ftr_ptr +
419 RIO_PORT_N_ERR_STS_CSR(sport),
420 &result);
421
422 return (result & PORT_N_ERR_STS_PORT_OK);
423}
424
425/**
426 * rio_route_add_entry- Add a route entry to a switch routing table
427 * @mport: Master port to send transaction
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800428 * @rswitch: Switch device
Matt Portereb188d02005-11-07 01:00:17 -0800429 * @table: Routing table ID
430 * @route_destid: Destination ID to be routed
431 * @route_port: Port number to be routed
432 *
433 * Calls the switch specific add_entry() method to add a route entry
434 * on a switch. The route table can be specified using the @table
435 * argument if a switch has per port routing tables or the normal
436 * use is to specific all tables (or the global table) by passing
437 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
438 * on failure.
439 */
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800440static int rio_route_add_entry(struct rio_mport *mport, struct rio_switch *rswitch,
Matt Portereb188d02005-11-07 01:00:17 -0800441 u16 table, u16 route_destid, u8 route_port)
442{
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800443 return rswitch->add_entry(mport, rswitch->destid,
444 rswitch->hopcount, table,
Matt Portereb188d02005-11-07 01:00:17 -0800445 route_destid, route_port);
446}
447
448/**
449 * rio_route_get_entry- Read a route entry in a switch routing table
450 * @mport: Master port to send transaction
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800451 * @rswitch: Switch device
Matt Portereb188d02005-11-07 01:00:17 -0800452 * @table: Routing table ID
453 * @route_destid: Destination ID to be routed
454 * @route_port: Pointer to read port number into
455 *
456 * Calls the switch specific get_entry() method to read a route entry
457 * in a switch. The route table can be specified using the @table
458 * argument if a switch has per port routing tables or the normal
459 * use is to specific all tables (or the global table) by passing
460 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
461 * on failure.
462 */
463static int
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800464rio_route_get_entry(struct rio_mport *mport, struct rio_switch *rswitch, u16 table,
Matt Portereb188d02005-11-07 01:00:17 -0800465 u16 route_destid, u8 * route_port)
466{
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800467 return rswitch->get_entry(mport, rswitch->destid,
468 rswitch->hopcount, table,
Matt Portereb188d02005-11-07 01:00:17 -0800469 route_destid, route_port);
470}
471
472/**
473 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
474 * @port: Master port to send transaction
475 * @hopcount: Number of hops to the device
476 *
477 * Used during enumeration to read the Host Device ID Lock CSR on a
478 * RIO device. Returns the value of the lock register.
479 */
480static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
481{
482 u32 result;
483
484 rio_mport_read_config_32(port, RIO_ANY_DESTID, hopcount,
485 RIO_HOST_DID_LOCK_CSR, &result);
486
487 return (u16) (result & 0xffff);
488}
489
490/**
491 * rio_get_swpinfo_inport- Gets the ingress port number
492 * @mport: Master port to send transaction
493 * @destid: Destination ID associated with the switch
494 * @hopcount: Number of hops to the device
495 *
496 * Returns port number being used to access the switch device.
497 */
498static u8
499rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
500{
501 u32 result;
502
503 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
504 &result);
505
506 return (u8) (result & 0xff);
507}
508
509/**
510 * rio_get_swpinfo_tports- Gets total number of ports on the switch
511 * @mport: Master port to send transaction
512 * @destid: Destination ID associated with the switch
513 * @hopcount: Number of hops to the device
514 *
515 * Returns total numbers of ports implemented by the switch device.
516 */
517static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
518 u8 hopcount)
519{
520 u32 result;
521
522 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
523 &result);
524
525 return RIO_GET_TOTAL_PORTS(result);
526}
527
528/**
529 * rio_net_add_mport- Add a master port to a RIO network
530 * @net: RIO network
531 * @port: Master port to add
532 *
533 * Adds a master port to the network list of associated master
534 * ports..
535 */
536static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
537{
538 spin_lock(&rio_global_list_lock);
539 list_add_tail(&port->nnode, &net->mports);
540 spin_unlock(&rio_global_list_lock);
541}
542
543/**
544 * rio_enum_peer- Recursively enumerate a RIO network through a master port
545 * @net: RIO network being enumerated
546 * @port: Master port to send transactions
547 * @hopcount: Number of hops into the network
548 *
549 * Recursively enumerates a RIO network. Transactions are sent via the
550 * master port passed in @port.
551 */
552static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
553 u8 hopcount)
554{
555 int port_num;
556 int num_ports;
557 int cur_destid;
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800558 int sw_destid;
559 int sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800560 struct rio_dev *rdev;
561 u16 destid;
562 int tmp;
563
564 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
565 pr_debug("RIO: PE already discovered by this host\n");
566 /*
567 * Already discovered by this host. Add it as another
568 * master port for the current network.
569 */
570 rio_net_add_mport(net, port);
571 return 0;
572 }
573
574 /* Attempt to acquire device lock */
575 rio_mport_write_config_32(port, RIO_ANY_DESTID, hopcount,
576 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
577 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
578 < port->host_deviceid) {
579 /* Delay a bit */
580 mdelay(1);
581 /* Attempt to acquire device lock again */
582 rio_mport_write_config_32(port, RIO_ANY_DESTID, hopcount,
583 RIO_HOST_DID_LOCK_CSR,
584 port->host_deviceid);
585 }
586
587 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
588 pr_debug(
589 "RIO: PE locked by a higher priority host...retreating\n");
590 return -1;
591 }
592
593 /* Setup new RIO device */
594 if ((rdev = rio_setup_device(net, port, RIO_ANY_DESTID, hopcount, 1))) {
595 /* Add device to the global and bus/net specific list. */
596 list_add_tail(&rdev->net_list, &net->devices);
597 } else
598 return -1;
599
600 if (rio_is_switch(rdev)) {
601 next_switchid++;
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800602 sw_inport = rio_get_swpinfo_inport(port, RIO_ANY_DESTID, hopcount);
603 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
604 port->host_deviceid, sw_inport);
605 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800606
607 for (destid = 0; destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800608 if (destid == port->host_deviceid)
609 continue;
610 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
611 destid, sw_inport);
612 rdev->rswitch->route_table[destid] = sw_inport;
Matt Portereb188d02005-11-07 01:00:17 -0800613 }
614
615 num_ports =
616 rio_get_swpinfo_tports(port, RIO_ANY_DESTID, hopcount);
617 pr_debug(
618 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
619 rio_name(rdev), rdev->vid, rdev->did, num_ports);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800620 sw_destid = next_destid;
Matt Portereb188d02005-11-07 01:00:17 -0800621 for (port_num = 0; port_num < num_ports; port_num++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800622 if (sw_inport == port_num)
Matt Portereb188d02005-11-07 01:00:17 -0800623 continue;
624
625 cur_destid = next_destid;
626
627 if (rio_sport_is_active
628 (port, RIO_ANY_DESTID, hopcount, port_num)) {
629 pr_debug(
630 "RIO: scanning device on port %d\n",
631 port_num);
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800632 rio_route_add_entry(port, rdev->rswitch,
Matt Portereb188d02005-11-07 01:00:17 -0800633 RIO_GLOBAL_TABLE,
634 RIO_ANY_DESTID, port_num);
635
636 if (rio_enum_peer(net, port, hopcount + 1) < 0)
637 return -1;
638
639 /* Update routing tables */
640 if (next_destid > cur_destid) {
641 for (destid = cur_destid;
642 destid < next_destid; destid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800643 if (destid == port->host_deviceid)
644 continue;
645 rio_route_add_entry(port, rdev->rswitch,
Matt Portereb188d02005-11-07 01:00:17 -0800646 RIO_GLOBAL_TABLE,
647 destid,
648 port_num);
649 rdev->rswitch->
650 route_table[destid] =
651 port_num;
652 }
Matt Portereb188d02005-11-07 01:00:17 -0800653 }
654 }
655 }
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800656
657 /* Check for empty switch */
658 if (next_destid == sw_destid) {
659 next_destid++;
660 if (next_destid == port->host_deviceid)
661 next_destid++;
662 }
663
664 rdev->rswitch->destid = sw_destid;
Matt Portereb188d02005-11-07 01:00:17 -0800665 } else
666 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
667 rio_name(rdev), rdev->vid, rdev->did);
668
669 return 0;
670}
671
672/**
673 * rio_enum_complete- Tests if enumeration of a network is complete
674 * @port: Master port to send transaction
675 *
676 * Tests the Component Tag CSR for presence of the magic enumeration
677 * complete flag. Return %1 if enumeration is complete or %0 if
678 * enumeration is incomplete.
679 */
680static int rio_enum_complete(struct rio_mport *port)
681{
682 u32 tag_csr;
683 int ret = 0;
684
685 rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
686
687 if (tag_csr == RIO_ENUM_CMPL_MAGIC)
688 ret = 1;
689
690 return ret;
691}
692
693/**
694 * rio_disc_peer- Recursively discovers a RIO network through a master port
695 * @net: RIO network being discovered
696 * @port: Master port to send transactions
697 * @destid: Current destination ID in network
698 * @hopcount: Number of hops into the network
699 *
700 * Recursively discovers a RIO network. Transactions are sent via the
701 * master port passed in @port.
702 */
703static int
704rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
705 u8 hopcount)
706{
707 u8 port_num, route_port;
708 int num_ports;
709 struct rio_dev *rdev;
710 u16 ndestid;
711
712 /* Setup new RIO device */
713 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
714 /* Add device to the global and bus/net specific list. */
715 list_add_tail(&rdev->net_list, &net->devices);
716 } else
717 return -1;
718
719 if (rio_is_switch(rdev)) {
720 next_switchid++;
721
722 /* Associated destid is how we accessed this switch */
723 rdev->rswitch->destid = destid;
724
725 num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
726 pr_debug(
727 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
728 rio_name(rdev), rdev->vid, rdev->did, num_ports);
729 for (port_num = 0; port_num < num_ports; port_num++) {
730 if (rio_get_swpinfo_inport(port, destid, hopcount) ==
731 port_num)
732 continue;
733
734 if (rio_sport_is_active
735 (port, destid, hopcount, port_num)) {
736 pr_debug(
737 "RIO: scanning device on port %d\n",
738 port_num);
739 for (ndestid = 0; ndestid < RIO_ANY_DESTID;
740 ndestid++) {
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800741 rio_route_get_entry(port, rdev->rswitch,
Matt Portereb188d02005-11-07 01:00:17 -0800742 RIO_GLOBAL_TABLE,
743 ndestid,
744 &route_port);
745 if (route_port == port_num)
746 break;
747 }
748
749 if (rio_disc_peer
750 (net, port, ndestid, hopcount + 1) < 0)
751 return -1;
752 }
753 }
754 } else
755 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
756 rio_name(rdev), rdev->vid, rdev->did);
757
758 return 0;
759}
760
761/**
762 * rio_mport_is_active- Tests if master port link is active
763 * @port: Master port to test
764 *
765 * Reads the port error status CSR for the master port to
766 * determine if the port has an active link. Returns
767 * %PORT_N_ERR_STS_PORT_OK if the master port is active
768 * or %0 if it is inactive.
769 */
770static int rio_mport_is_active(struct rio_mport *port)
771{
772 u32 result = 0;
773 u32 ext_ftr_ptr;
774 int *entry = rio_mport_phys_table;
775
776 do {
777 if ((ext_ftr_ptr =
778 rio_mport_get_feature(port, 1, 0, 0, *entry)))
779 break;
780 } while (*++entry >= 0);
781
782 if (ext_ftr_ptr)
783 rio_local_read_config_32(port,
784 ext_ftr_ptr +
785 RIO_PORT_N_ERR_STS_CSR(port->index),
786 &result);
787
788 return (result & PORT_N_ERR_STS_PORT_OK);
789}
790
791/**
792 * rio_alloc_net- Allocate and configure a new RIO network
793 * @port: Master port associated with the RIO network
794 *
795 * Allocates a RIO network structure, initializes per-network
796 * list heads, and adds the associated master port to the
797 * network list of associated master ports. Returns a
798 * RIO network pointer on success or %NULL on failure.
799 */
800static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
801{
802 struct rio_net *net;
803
804 net = kmalloc(sizeof(struct rio_net), GFP_KERNEL);
805 if (net) {
806 memset(net, 0, sizeof(struct rio_net));
807 INIT_LIST_HEAD(&net->node);
808 INIT_LIST_HEAD(&net->devices);
809 INIT_LIST_HEAD(&net->mports);
810 list_add_tail(&port->nnode, &net->mports);
811 net->hport = port;
812 net->id = next_net++;
813 }
814 return net;
815}
816
817/**
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800818 * rio_update_route_tables- Updates route tables in switches
819 * @port: Master port associated with the RIO network
820 *
821 * For each enumerated device, ensure that each switch in a system
822 * has correct routing entries. Add routes for devices that where
823 * unknown dirung the first enumeration pass through the switch.
824 */
825static void rio_update_route_tables(struct rio_mport *port)
826{
827 struct rio_dev *rdev;
828 struct rio_switch *rswitch;
829 u8 sport;
830 u16 destid;
831
832 list_for_each_entry(rdev, &rio_devices, global_list) {
833
834 destid = (rio_is_switch(rdev))?rdev->rswitch->destid:rdev->destid;
835
836 list_for_each_entry(rswitch, &rio_switches, node) {
837
838 if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
839 continue;
840
841 if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
842
843 sport = rio_get_swpinfo_inport(port,
844 rswitch->destid, rswitch->hopcount);
845
846 if (rswitch->add_entry) {
847 rio_route_add_entry(port, rswitch, RIO_GLOBAL_TABLE, destid, sport);
848 rswitch->route_table[destid] = sport;
849 }
850 }
851 }
852 }
853}
854
855/**
Matt Portereb188d02005-11-07 01:00:17 -0800856 * rio_enum_mport- Start enumeration through a master port
857 * @mport: Master port to send transactions
858 *
859 * Starts the enumeration process. If somebody has enumerated our
860 * master port device, then give up. If not and we have an active
861 * link, then start recursive peer enumeration. Returns %0 if
862 * enumeration succeeds or %-EBUSY if enumeration fails.
863 */
864int rio_enum_mport(struct rio_mport *mport)
865{
866 struct rio_net *net = NULL;
867 int rc = 0;
868
869 printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
870 mport->name);
871 /* If somebody else enumerated our master port device, bail. */
872 if (rio_enum_host(mport) < 0) {
873 printk(KERN_INFO
874 "RIO: master port %d device has been enumerated by a remote host\n",
875 mport->id);
876 rc = -EBUSY;
877 goto out;
878 }
879
880 /* If master port has an active link, allocate net and enum peers */
881 if (rio_mport_is_active(mport)) {
882 if (!(net = rio_alloc_net(mport))) {
883 printk(KERN_ERR "RIO: failed to allocate new net\n");
884 rc = -ENOMEM;
885 goto out;
886 }
887 if (rio_enum_peer(net, mport, 0) < 0) {
888 /* A higher priority host won enumeration, bail. */
889 printk(KERN_INFO
890 "RIO: master port %d device has lost enumeration to a remote host\n",
891 mport->id);
892 rio_clear_locks(mport);
893 rc = -EBUSY;
894 goto out;
895 }
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800896 rio_update_route_tables(mport);
Matt Portereb188d02005-11-07 01:00:17 -0800897 rio_clear_locks(mport);
898 } else {
899 printk(KERN_INFO "RIO: master port %d link inactive\n",
900 mport->id);
901 rc = -EINVAL;
902 }
903
904 out:
905 return rc;
906}
907
908/**
909 * rio_build_route_tables- Generate route tables from switch route entries
910 *
911 * For each switch device, generate a route table by copying existing
912 * route entries from the switch.
913 */
914static void rio_build_route_tables(void)
915{
916 struct rio_dev *rdev;
917 int i;
918 u8 sport;
919
920 list_for_each_entry(rdev, &rio_devices, global_list)
921 if (rio_is_switch(rdev))
922 for (i = 0; i < RIO_MAX_ROUTE_ENTRIES; i++) {
923 if (rio_route_get_entry
Alexandre Bouninec70555b2007-02-10 01:46:47 -0800924 (rdev->net->hport, rdev->rswitch, RIO_GLOBAL_TABLE,
925 i, &sport) < 0)
Matt Portereb188d02005-11-07 01:00:17 -0800926 continue;
927 rdev->rswitch->route_table[i] = sport;
928 }
929}
930
931/**
932 * rio_enum_timeout- Signal that enumeration timed out
933 * @data: Address of timeout flag.
934 *
935 * When the enumeration complete timer expires, set a flag that
936 * signals to the discovery process that enumeration did not
937 * complete in a sane amount of time.
938 */
939static void rio_enum_timeout(unsigned long data)
940{
941 /* Enumeration timed out, set flag */
942 *(int *)data = 1;
943}
944
945/**
946 * rio_disc_mport- Start discovery through a master port
947 * @mport: Master port to send transactions
948 *
949 * Starts the discovery process. If we have an active link,
950 * then wait for the signal that enumeration is complete.
951 * When enumeration completion is signaled, start recursive
952 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
953 * on failure.
954 */
955int rio_disc_mport(struct rio_mport *mport)
956{
957 struct rio_net *net = NULL;
958 int enum_timeout_flag = 0;
959
960 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
961 mport->name);
962
963 /* If master port has an active link, allocate net and discover peers */
964 if (rio_mport_is_active(mport)) {
965 if (!(net = rio_alloc_net(mport))) {
966 printk(KERN_ERR "RIO: Failed to allocate new net\n");
967 goto bail;
968 }
969
970 pr_debug("RIO: wait for enumeration complete...");
971
972 rio_enum_timer.expires =
973 jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
974 rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
975 add_timer(&rio_enum_timer);
976 while (!rio_enum_complete(mport)) {
977 mdelay(1);
978 if (enum_timeout_flag) {
979 del_timer_sync(&rio_enum_timer);
980 goto timeout;
981 }
982 }
983 del_timer_sync(&rio_enum_timer);
984
985 pr_debug("done\n");
986 if (rio_disc_peer(net, mport, RIO_ANY_DESTID, 0) < 0) {
987 printk(KERN_INFO
988 "RIO: master port %d device has failed discovery\n",
989 mport->id);
990 goto bail;
991 }
992
993 rio_build_route_tables();
994 }
995
996 return 0;
997
998 timeout:
999 pr_debug("timeout\n");
1000 bail:
1001 return -EBUSY;
1002}