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