Merge tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[pandora-kernel.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  * Copyright 2009 Integrated Device Technology, Inc.
8  * Alex Bounine <alexandre.bounine@idt.com>
9  * - Added Port-Write/Error Management initialization and handling
10  *
11  * Copyright 2009 Sysgo AG
12  * Thomas Moll <thomas.moll@sysgo.com>
13  * - Added Input- Output- enable functionality, to allow full communication
14  *
15  * This program is free software; you can redistribute  it and/or modify it
16  * under  the terms of  the GNU General  Public License as published by the
17  * Free Software Foundation;  either version 2 of the  License, or (at your
18  * option) any later version.
19  */
20
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/init.h>
27 #include <linux/rio.h>
28 #include <linux/rio_drv.h>
29 #include <linux/rio_ids.h>
30 #include <linux/rio_regs.h>
31 #include <linux/module.h>
32 #include <linux/spinlock.h>
33 #include <linux/timer.h>
34 #include <linux/sched.h>
35 #include <linux/jiffies.h>
36 #include <linux/slab.h>
37
38 #include "rio.h"
39
40 LIST_HEAD(rio_devices);
41
42 static void rio_init_em(struct rio_dev *rdev);
43
44 DEFINE_SPINLOCK(rio_global_list_lock);
45
46 static int next_destid = 0;
47 static int next_comptag = 1;
48
49 static int rio_mport_phys_table[] = {
50         RIO_EFB_PAR_EP_ID,
51         RIO_EFB_PAR_EP_REC_ID,
52         RIO_EFB_SER_EP_ID,
53         RIO_EFB_SER_EP_REC_ID,
54         -1,
55 };
56
57
58 /*
59  * rio_destid_alloc - Allocate next available destID for given network
60  * net: RIO network
61  *
62  * Returns next available device destination ID for the specified RIO network.
63  * Marks allocated ID as one in use.
64  * Returns RIO_INVALID_DESTID if new destID is not available.
65  */
66 static u16 rio_destid_alloc(struct rio_net *net)
67 {
68         int destid;
69         struct rio_id_table *idtab = &net->destid_table;
70
71         spin_lock(&idtab->lock);
72         destid = find_next_zero_bit(idtab->table, idtab->max, idtab->next);
73         if (destid >= idtab->max)
74                 destid = find_first_zero_bit(idtab->table, idtab->max);
75
76         if (destid < idtab->max) {
77                 idtab->next = destid + 1;
78                 if (idtab->next >= idtab->max)
79                         idtab->next = 0;
80                 set_bit(destid, idtab->table);
81                 destid += idtab->start;
82         } else
83                 destid = RIO_INVALID_DESTID;
84
85         spin_unlock(&idtab->lock);
86         return (u16)destid;
87 }
88
89 /*
90  * rio_destid_reserve - Reserve the specivied destID
91  * net: RIO network
92  * destid: destID to reserve
93  *
94  * Tries to reserve the specified destID.
95  * Returns 0 if successfull.
96  */
97 static int rio_destid_reserve(struct rio_net *net, u16 destid)
98 {
99         int oldbit;
100         struct rio_id_table *idtab = &net->destid_table;
101
102         destid -= idtab->start;
103         spin_lock(&idtab->lock);
104         oldbit = test_and_set_bit(destid, idtab->table);
105         spin_unlock(&idtab->lock);
106         return oldbit;
107 }
108
109 /*
110  * rio_destid_free - free a previously allocated destID
111  * net: RIO network
112  * destid: destID to free
113  *
114  * Makes the specified destID available for use.
115  */
116 static void rio_destid_free(struct rio_net *net, u16 destid)
117 {
118         struct rio_id_table *idtab = &net->destid_table;
119
120         destid -= idtab->start;
121         spin_lock(&idtab->lock);
122         clear_bit(destid, idtab->table);
123         spin_unlock(&idtab->lock);
124 }
125
126 /*
127  * rio_destid_first - return first destID in use
128  * net: RIO network
129  */
130 static u16 rio_destid_first(struct rio_net *net)
131 {
132         int destid;
133         struct rio_id_table *idtab = &net->destid_table;
134
135         spin_lock(&idtab->lock);
136         destid = find_first_bit(idtab->table, idtab->max);
137         if (destid >= idtab->max)
138                 destid = RIO_INVALID_DESTID;
139         else
140                 destid += idtab->start;
141         spin_unlock(&idtab->lock);
142         return (u16)destid;
143 }
144
145 /*
146  * rio_destid_next - return next destID in use
147  * net: RIO network
148  * from: destination ID from which search shall continue
149  */
150 static u16 rio_destid_next(struct rio_net *net, u16 from)
151 {
152         int destid;
153         struct rio_id_table *idtab = &net->destid_table;
154
155         spin_lock(&idtab->lock);
156         destid = find_next_bit(idtab->table, idtab->max, from);
157         if (destid >= idtab->max)
158                 destid = RIO_INVALID_DESTID;
159         else
160                 destid += idtab->start;
161         spin_unlock(&idtab->lock);
162         return (u16)destid;
163 }
164
165 /**
166  * rio_get_device_id - Get the base/extended device id for a device
167  * @port: RIO master port
168  * @destid: Destination ID of device
169  * @hopcount: Hopcount to device
170  *
171  * Reads the base/extended device id from a device. Returns the
172  * 8/16-bit device ID.
173  */
174 static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
175 {
176         u32 result;
177
178         rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
179
180         return RIO_GET_DID(port->sys_size, result);
181 }
182
183 /**
184  * rio_set_device_id - Set the base/extended device id for a device
185  * @port: RIO master port
186  * @destid: Destination ID of device
187  * @hopcount: Hopcount to device
188  * @did: Device ID value to be written
189  *
190  * Writes the base/extended device id from a device.
191  */
192 static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
193 {
194         rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
195                                   RIO_SET_DID(port->sys_size, did));
196 }
197
198 /**
199  * rio_local_set_device_id - Set the base/extended device id for a port
200  * @port: RIO master port
201  * @did: Device ID value to be written
202  *
203  * Writes the base/extended device id from a device.
204  */
205 static void rio_local_set_device_id(struct rio_mport *port, u16 did)
206 {
207         rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
208                                 did));
209 }
210
211 /**
212  * rio_clear_locks- Release all host locks and signal enumeration complete
213  * @net: RIO network to run on
214  *
215  * Marks the component tag CSR on each device with the enumeration
216  * complete flag. When complete, it then release the host locks on
217  * each device. Returns 0 on success or %-EINVAL on failure.
218  */
219 static int rio_clear_locks(struct rio_net *net)
220 {
221         struct rio_mport *port = net->hport;
222         struct rio_dev *rdev;
223         u32 result;
224         int ret = 0;
225
226         /* Release host device id locks */
227         rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
228                                   port->host_deviceid);
229         rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
230         if ((result & 0xffff) != 0xffff) {
231                 printk(KERN_INFO
232                        "RIO: badness when releasing host lock on master port, result %8.8x\n",
233                        result);
234                 ret = -EINVAL;
235         }
236         list_for_each_entry(rdev, &net->devices, net_list) {
237                 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
238                                     port->host_deviceid);
239                 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
240                 if ((result & 0xffff) != 0xffff) {
241                         printk(KERN_INFO
242                                "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
243                                rdev->vid, rdev->did);
244                         ret = -EINVAL;
245                 }
246
247                 /* Mark device as discovered and enable master */
248                 rio_read_config_32(rdev,
249                                    rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
250                                    &result);
251                 result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
252                 rio_write_config_32(rdev,
253                                     rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
254                                     result);
255         }
256
257         return ret;
258 }
259
260 /**
261  * rio_enum_host- Set host lock and initialize host destination ID
262  * @port: Master port to issue transaction
263  *
264  * Sets the local host master port lock and destination ID register
265  * with the host device ID value. The host device ID value is provided
266  * by the platform. Returns %0 on success or %-1 on failure.
267  */
268 static int rio_enum_host(struct rio_mport *port)
269 {
270         u32 result;
271
272         /* Set master port host device id lock */
273         rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
274                                   port->host_deviceid);
275
276         rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
277         if ((result & 0xffff) != port->host_deviceid)
278                 return -1;
279
280         /* Set master port destid and init destid ctr */
281         rio_local_set_device_id(port, port->host_deviceid);
282         return 0;
283 }
284
285 /**
286  * rio_device_has_destid- Test if a device contains a destination ID register
287  * @port: Master port to issue transaction
288  * @src_ops: RIO device source operations
289  * @dst_ops: RIO device destination operations
290  *
291  * Checks the provided @src_ops and @dst_ops for the necessary transaction
292  * capabilities that indicate whether or not a device will implement a
293  * destination ID register. Returns 1 if true or 0 if false.
294  */
295 static int rio_device_has_destid(struct rio_mport *port, int src_ops,
296                                  int dst_ops)
297 {
298         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;
299
300         return !!((src_ops | dst_ops) & mask);
301 }
302
303 /**
304  * rio_release_dev- Frees a RIO device struct
305  * @dev: LDM device associated with a RIO device struct
306  *
307  * Gets the RIO device struct associated a RIO device struct.
308  * The RIO device struct is freed.
309  */
310 static void rio_release_dev(struct device *dev)
311 {
312         struct rio_dev *rdev;
313
314         rdev = to_rio_dev(dev);
315         kfree(rdev);
316 }
317
318 /**
319  * rio_is_switch- Tests if a RIO device has switch capabilities
320  * @rdev: RIO device
321  *
322  * Gets the RIO device Processing Element Features register
323  * contents and tests for switch capabilities. Returns 1 if
324  * the device is a switch or 0 if it is not a switch.
325  * The RIO device struct is freed.
326  */
327 static int rio_is_switch(struct rio_dev *rdev)
328 {
329         if (rdev->pef & RIO_PEF_SWITCH)
330                 return 1;
331         return 0;
332 }
333
334 /**
335  * rio_switch_init - Sets switch operations for a particular vendor switch
336  * @rdev: RIO device
337  * @do_enum: Enumeration/Discovery mode flag
338  *
339  * Searches the RIO switch ops table for known switch types. If the vid
340  * and did match a switch table entry, then call switch initialization
341  * routine to setup switch-specific routines.
342  */
343 static void rio_switch_init(struct rio_dev *rdev, int do_enum)
344 {
345         struct rio_switch_ops *cur = __start_rio_switch_ops;
346         struct rio_switch_ops *end = __end_rio_switch_ops;
347
348         while (cur < end) {
349                 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
350                         pr_debug("RIO: calling init routine for %s\n",
351                                  rio_name(rdev));
352                         cur->init_hook(rdev, do_enum);
353                         break;
354                 }
355                 cur++;
356         }
357
358         if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
359                 pr_debug("RIO: adding STD routing ops for %s\n",
360                         rio_name(rdev));
361                 rdev->rswitch->add_entry = rio_std_route_add_entry;
362                 rdev->rswitch->get_entry = rio_std_route_get_entry;
363                 rdev->rswitch->clr_table = rio_std_route_clr_table;
364         }
365
366         if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
367                 printk(KERN_ERR "RIO: missing routing ops for %s\n",
368                        rio_name(rdev));
369 }
370
371 /**
372  * rio_add_device- Adds a RIO device to the device model
373  * @rdev: RIO device
374  *
375  * Adds the RIO device to the global device list and adds the RIO
376  * device to the RIO device list.  Creates the generic sysfs nodes
377  * for an RIO device.
378  */
379 static int __devinit rio_add_device(struct rio_dev *rdev)
380 {
381         int err;
382
383         err = device_add(&rdev->dev);
384         if (err)
385                 return err;
386
387         spin_lock(&rio_global_list_lock);
388         list_add_tail(&rdev->global_list, &rio_devices);
389         spin_unlock(&rio_global_list_lock);
390
391         rio_create_sysfs_dev_files(rdev);
392
393         return 0;
394 }
395
396 /**
397  * rio_enable_rx_tx_port - enable input receiver and output transmitter of
398  * given port
399  * @port: Master port associated with the RIO network
400  * @local: local=1 select local port otherwise a far device is reached
401  * @destid: Destination ID of the device to check host bit
402  * @hopcount: Number of hops to reach the target
403  * @port_num: Port (-number on switch) to enable on a far end device
404  *
405  * Returns 0 or 1 from on General Control Command and Status Register
406  * (EXT_PTR+0x3C)
407  */
408 inline int rio_enable_rx_tx_port(struct rio_mport *port,
409                                  int local, u16 destid,
410                                  u8 hopcount, u8 port_num) {
411 #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
412         u32 regval;
413         u32 ext_ftr_ptr;
414
415         /*
416         * enable rx input tx output port
417         */
418         pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
419                  "%d, port_num = %d)\n", local, destid, hopcount, port_num);
420
421         ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
422
423         if (local) {
424                 rio_local_read_config_32(port, ext_ftr_ptr +
425                                 RIO_PORT_N_CTL_CSR(0),
426                                 &regval);
427         } else {
428                 if (rio_mport_read_config_32(port, destid, hopcount,
429                 ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
430                         return -EIO;
431         }
432
433         if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
434                 /* serial */
435                 regval = regval | RIO_PORT_N_CTL_EN_RX_SER
436                                 | RIO_PORT_N_CTL_EN_TX_SER;
437         } else {
438                 /* parallel */
439                 regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
440                                 | RIO_PORT_N_CTL_EN_TX_PAR;
441         }
442
443         if (local) {
444                 rio_local_write_config_32(port, ext_ftr_ptr +
445                                           RIO_PORT_N_CTL_CSR(0), regval);
446         } else {
447                 if (rio_mport_write_config_32(port, destid, hopcount,
448                     ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
449                         return -EIO;
450         }
451 #endif
452         return 0;
453 }
454
455 /**
456  * rio_setup_device- Allocates and sets up a RIO device
457  * @net: RIO network
458  * @port: Master port to send transactions
459  * @destid: Current destination ID
460  * @hopcount: Current hopcount
461  * @do_enum: Enumeration/Discovery mode flag
462  *
463  * Allocates a RIO device and configures fields based on configuration
464  * space contents. If device has a destination ID register, a destination
465  * ID is either assigned in enumeration mode or read from configuration
466  * space in discovery mode.  If the device has switch capabilities, then
467  * a switch is allocated and configured appropriately. Returns a pointer
468  * to a RIO device on success or NULL on failure.
469  *
470  */
471 static struct rio_dev __devinit *rio_setup_device(struct rio_net *net,
472                                         struct rio_mport *port, u16 destid,
473                                         u8 hopcount, int do_enum)
474 {
475         int ret = 0;
476         struct rio_dev *rdev;
477         struct rio_switch *rswitch = NULL;
478         int result, rdid;
479         size_t size;
480         u32 swpinfo = 0;
481
482         size = sizeof(struct rio_dev);
483         if (rio_mport_read_config_32(port, destid, hopcount,
484                                      RIO_PEF_CAR, &result))
485                 return NULL;
486
487         if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
488                 rio_mport_read_config_32(port, destid, hopcount,
489                                          RIO_SWP_INFO_CAR, &swpinfo);
490                 if (result & RIO_PEF_SWITCH) {
491                         size += (RIO_GET_TOTAL_PORTS(swpinfo) *
492                                 sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
493                 }
494         }
495
496         rdev = kzalloc(size, GFP_KERNEL);
497         if (!rdev)
498                 return NULL;
499
500         rdev->net = net;
501         rdev->pef = result;
502         rdev->swpinfo = swpinfo;
503         rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
504                                  &result);
505         rdev->did = result >> 16;
506         rdev->vid = result & 0xffff;
507         rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
508                                  &rdev->device_rev);
509         rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
510                                  &result);
511         rdev->asm_did = result >> 16;
512         rdev->asm_vid = result & 0xffff;
513         rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
514                                  &result);
515         rdev->asm_rev = result >> 16;
516         if (rdev->pef & RIO_PEF_EXT_FEATURES) {
517                 rdev->efptr = result & 0xffff;
518                 rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
519                                                          hopcount);
520
521                 rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
522                                                 hopcount, RIO_EFB_ERR_MGMNT);
523         }
524
525         rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
526                                  &rdev->src_ops);
527         rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
528                                  &rdev->dst_ops);
529
530         if (do_enum) {
531                 /* Assign component tag to device */
532                 if (next_comptag >= 0x10000) {
533                         pr_err("RIO: Component Tag Counter Overflow\n");
534                         goto cleanup;
535                 }
536                 rio_mport_write_config_32(port, destid, hopcount,
537                                           RIO_COMPONENT_TAG_CSR, next_comptag);
538                 rdev->comp_tag = next_comptag++;
539         }  else {
540                 rio_mport_read_config_32(port, destid, hopcount,
541                                          RIO_COMPONENT_TAG_CSR,
542                                          &rdev->comp_tag);
543         }
544
545         if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
546                 if (do_enum) {
547                         rio_set_device_id(port, destid, hopcount, next_destid);
548                         rdev->destid = next_destid;
549                         next_destid = rio_destid_alloc(net);
550                 } else
551                         rdev->destid = rio_get_device_id(port, destid, hopcount);
552
553                 rdev->hopcount = 0xff;
554         } else {
555                 /* Switch device has an associated destID which
556                  * will be adjusted later
557                  */
558                 rdev->destid = destid;
559                 rdev->hopcount = hopcount;
560         }
561
562         /* If a PE has both switch and other functions, show it as a switch */
563         if (rio_is_switch(rdev)) {
564                 rswitch = rdev->rswitch;
565                 rswitch->switchid = rdev->comp_tag & RIO_CTAG_UDEVID;
566                 rswitch->port_ok = 0;
567                 rswitch->route_table = kzalloc(sizeof(u8)*
568                                         RIO_MAX_ROUTE_ENTRIES(port->sys_size),
569                                         GFP_KERNEL);
570                 if (!rswitch->route_table)
571                         goto cleanup;
572                 /* Initialize switch route table */
573                 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
574                                 rdid++)
575                         rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
576                 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
577                              rswitch->switchid);
578                 rio_switch_init(rdev, do_enum);
579
580                 if (do_enum && rswitch->clr_table)
581                         rswitch->clr_table(port, destid, hopcount,
582                                            RIO_GLOBAL_TABLE);
583
584                 list_add_tail(&rswitch->node, &net->switches);
585
586         } else {
587                 if (do_enum)
588                         /*Enable Input Output Port (transmitter reviever)*/
589                         rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
590
591                 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
592                              rdev->destid);
593         }
594
595         rdev->dev.bus = &rio_bus_type;
596         rdev->dev.parent = &rio_bus;
597
598         device_initialize(&rdev->dev);
599         rdev->dev.release = rio_release_dev;
600         rio_dev_get(rdev);
601
602         rdev->dma_mask = DMA_BIT_MASK(32);
603         rdev->dev.dma_mask = &rdev->dma_mask;
604         rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
605
606         if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
607                 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
608                                    0, 0xffff);
609
610         ret = rio_add_device(rdev);
611         if (ret)
612                 goto cleanup;
613
614         return rdev;
615
616 cleanup:
617         if (rswitch)
618                 kfree(rswitch->route_table);
619
620         kfree(rdev);
621         return NULL;
622 }
623
624 /**
625  * rio_sport_is_active- Tests if a switch port has an active connection.
626  * @port: Master port to send transaction
627  * @destid: Associated destination ID for switch
628  * @hopcount: Hopcount to reach switch
629  * @sport: Switch port number
630  *
631  * Reads the port error status CSR for a particular switch port to
632  * determine if the port has an active link.  Returns
633  * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
634  * inactive.
635  */
636 static int
637 rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
638 {
639         u32 result = 0;
640         u32 ext_ftr_ptr;
641
642         ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
643
644         while (ext_ftr_ptr) {
645                 rio_mport_read_config_32(port, destid, hopcount,
646                                          ext_ftr_ptr, &result);
647                 result = RIO_GET_BLOCK_ID(result);
648                 if ((result == RIO_EFB_SER_EP_FREE_ID) ||
649                     (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
650                     (result == RIO_EFB_SER_EP_FREC_ID))
651                         break;
652
653                 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
654                                                 ext_ftr_ptr);
655         }
656
657         if (ext_ftr_ptr)
658                 rio_mport_read_config_32(port, destid, hopcount,
659                                          ext_ftr_ptr +
660                                          RIO_PORT_N_ERR_STS_CSR(sport),
661                                          &result);
662
663         return result & RIO_PORT_N_ERR_STS_PORT_OK;
664 }
665
666 /**
667  * rio_lock_device - Acquires host device lock for specified device
668  * @port: Master port to send transaction
669  * @destid: Destination ID for device/switch
670  * @hopcount: Hopcount to reach switch
671  * @wait_ms: Max wait time in msec (0 = no timeout)
672  *
673  * Attepts to acquire host device lock for specified device
674  * Returns 0 if device lock acquired or EINVAL if timeout expires.
675  */
676 static int
677 rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
678 {
679         u32 result;
680         int tcnt = 0;
681
682         /* Attempt to acquire device lock */
683         rio_mport_write_config_32(port, destid, hopcount,
684                                   RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
685         rio_mport_read_config_32(port, destid, hopcount,
686                                  RIO_HOST_DID_LOCK_CSR, &result);
687
688         while (result != port->host_deviceid) {
689                 if (wait_ms != 0 && tcnt == wait_ms) {
690                         pr_debug("RIO: timeout when locking device %x:%x\n",
691                                 destid, hopcount);
692                         return -EINVAL;
693                 }
694
695                 /* Delay a bit */
696                 mdelay(1);
697                 tcnt++;
698                 /* Try to acquire device lock again */
699                 rio_mport_write_config_32(port, destid,
700                         hopcount,
701                         RIO_HOST_DID_LOCK_CSR,
702                         port->host_deviceid);
703                 rio_mport_read_config_32(port, destid,
704                         hopcount,
705                         RIO_HOST_DID_LOCK_CSR, &result);
706         }
707
708         return 0;
709 }
710
711 /**
712  * rio_unlock_device - Releases host device lock for specified device
713  * @port: Master port to send transaction
714  * @destid: Destination ID for device/switch
715  * @hopcount: Hopcount to reach switch
716  *
717  * Returns 0 if device lock released or EINVAL if fails.
718  */
719 static int
720 rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
721 {
722         u32 result;
723
724         /* Release device lock */
725         rio_mport_write_config_32(port, destid,
726                                   hopcount,
727                                   RIO_HOST_DID_LOCK_CSR,
728                                   port->host_deviceid);
729         rio_mport_read_config_32(port, destid, hopcount,
730                 RIO_HOST_DID_LOCK_CSR, &result);
731         if ((result & 0xffff) != 0xffff) {
732                 pr_debug("RIO: badness when releasing device lock %x:%x\n",
733                          destid, hopcount);
734                 return -EINVAL;
735         }
736
737         return 0;
738 }
739
740 /**
741  * rio_route_add_entry- Add a route entry to a switch routing table
742  * @rdev: RIO device
743  * @table: Routing table ID
744  * @route_destid: Destination ID to be routed
745  * @route_port: Port number to be routed
746  * @lock: lock switch device flag
747  *
748  * Calls the switch specific add_entry() method to add a route entry
749  * on a switch. The route table can be specified using the @table
750  * argument if a switch has per port routing tables or the normal
751  * use is to specific all tables (or the global table) by passing
752  * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
753  * on failure.
754  */
755 static int
756 rio_route_add_entry(struct rio_dev *rdev,
757                     u16 table, u16 route_destid, u8 route_port, int lock)
758 {
759         int rc;
760
761         if (lock) {
762                 rc = rio_lock_device(rdev->net->hport, rdev->destid,
763                                      rdev->hopcount, 1000);
764                 if (rc)
765                         return rc;
766         }
767
768         rc = rdev->rswitch->add_entry(rdev->net->hport, rdev->destid,
769                                       rdev->hopcount, table,
770                                       route_destid, route_port);
771         if (lock)
772                 rio_unlock_device(rdev->net->hport, rdev->destid,
773                                   rdev->hopcount);
774
775         return rc;
776 }
777
778 /**
779  * rio_route_get_entry- Read a route entry in a switch routing table
780  * @rdev: RIO device
781  * @table: Routing table ID
782  * @route_destid: Destination ID to be routed
783  * @route_port: Pointer to read port number into
784  * @lock: lock switch device flag
785  *
786  * Calls the switch specific get_entry() method to read a route entry
787  * in a switch. The route table can be specified using the @table
788  * argument if a switch has per port routing tables or the normal
789  * use is to specific all tables (or the global table) by passing
790  * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
791  * on failure.
792  */
793 static int
794 rio_route_get_entry(struct rio_dev *rdev, u16 table,
795                     u16 route_destid, u8 *route_port, int lock)
796 {
797         int rc;
798
799         if (lock) {
800                 rc = rio_lock_device(rdev->net->hport, rdev->destid,
801                                      rdev->hopcount, 1000);
802                 if (rc)
803                         return rc;
804         }
805
806         rc = rdev->rswitch->get_entry(rdev->net->hport, rdev->destid,
807                                       rdev->hopcount, table,
808                                       route_destid, route_port);
809         if (lock)
810                 rio_unlock_device(rdev->net->hport, rdev->destid,
811                                   rdev->hopcount);
812
813         return rc;
814 }
815
816 /**
817  * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
818  * @port: Master port to send transaction
819  * @hopcount: Number of hops to the device
820  *
821  * Used during enumeration to read the Host Device ID Lock CSR on a
822  * RIO device. Returns the value of the lock register.
823  */
824 static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
825 {
826         u32 result;
827
828         rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
829                                  RIO_HOST_DID_LOCK_CSR, &result);
830
831         return (u16) (result & 0xffff);
832 }
833
834 /**
835  * rio_enum_peer- Recursively enumerate a RIO network through a master port
836  * @net: RIO network being enumerated
837  * @port: Master port to send transactions
838  * @hopcount: Number of hops into the network
839  * @prev: Previous RIO device connected to the enumerated one
840  * @prev_port: Port on previous RIO device
841  *
842  * Recursively enumerates a RIO network.  Transactions are sent via the
843  * master port passed in @port.
844  */
845 static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
846                          u8 hopcount, struct rio_dev *prev, int prev_port)
847 {
848         struct rio_dev *rdev;
849         u32 regval;
850         int tmp;
851
852         if (rio_mport_chk_dev_access(port,
853                         RIO_ANY_DESTID(port->sys_size), hopcount)) {
854                 pr_debug("RIO: device access check failed\n");
855                 return -1;
856         }
857
858         if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
859                 pr_debug("RIO: PE already discovered by this host\n");
860                 /*
861                  * Already discovered by this host. Add it as another
862                  * link to the existing device.
863                  */
864                 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
865                                 hopcount, RIO_COMPONENT_TAG_CSR, &regval);
866
867                 if (regval) {
868                         rdev = rio_get_comptag((regval & 0xffff), NULL);
869
870                         if (rdev && prev && rio_is_switch(prev)) {
871                                 pr_debug("RIO: redundant path to %s\n",
872                                          rio_name(rdev));
873                                 prev->rswitch->nextdev[prev_port] = rdev;
874                         }
875                 }
876
877                 return 0;
878         }
879
880         /* Attempt to acquire device lock */
881         rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
882                                   hopcount,
883                                   RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
884         while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
885                < port->host_deviceid) {
886                 /* Delay a bit */
887                 mdelay(1);
888                 /* Attempt to acquire device lock again */
889                 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
890                                           hopcount,
891                                           RIO_HOST_DID_LOCK_CSR,
892                                           port->host_deviceid);
893         }
894
895         if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
896                 pr_debug(
897                     "RIO: PE locked by a higher priority host...retreating\n");
898                 return -1;
899         }
900
901         /* Setup new RIO device */
902         rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
903                                         hopcount, 1);
904         if (rdev) {
905                 /* Add device to the global and bus/net specific list. */
906                 list_add_tail(&rdev->net_list, &net->devices);
907                 rdev->prev = prev;
908                 if (prev && rio_is_switch(prev))
909                         prev->rswitch->nextdev[prev_port] = rdev;
910         } else
911                 return -1;
912
913         if (rio_is_switch(rdev)) {
914                 int sw_destid;
915                 int cur_destid;
916                 int sw_inport;
917                 u16 destid;
918                 int port_num;
919
920                 sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
921                 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
922                                     port->host_deviceid, sw_inport, 0);
923                 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
924
925                 destid = rio_destid_first(net);
926                 while (destid != RIO_INVALID_DESTID && destid < next_destid) {
927                         if (destid != port->host_deviceid) {
928                                 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
929                                                     destid, sw_inport, 0);
930                                 rdev->rswitch->route_table[destid] = sw_inport;
931                         }
932                         destid = rio_destid_next(net, destid + 1);
933                 }
934                 pr_debug(
935                     "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
936                     rio_name(rdev), rdev->vid, rdev->did,
937                     RIO_GET_TOTAL_PORTS(rdev->swpinfo));
938                 sw_destid = next_destid;
939                 for (port_num = 0;
940                      port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
941                      port_num++) {
942                         if (sw_inport == port_num) {
943                                 rio_enable_rx_tx_port(port, 0,
944                                               RIO_ANY_DESTID(port->sys_size),
945                                               hopcount, port_num);
946                                 rdev->rswitch->port_ok |= (1 << port_num);
947                                 continue;
948                         }
949
950                         cur_destid = next_destid;
951
952                         if (rio_sport_is_active
953                             (port, RIO_ANY_DESTID(port->sys_size), hopcount,
954                              port_num)) {
955                                 pr_debug(
956                                     "RIO: scanning device on port %d\n",
957                                     port_num);
958                                 rio_enable_rx_tx_port(port, 0,
959                                               RIO_ANY_DESTID(port->sys_size),
960                                               hopcount, port_num);
961                                 rdev->rswitch->port_ok |= (1 << port_num);
962                                 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
963                                                 RIO_ANY_DESTID(port->sys_size),
964                                                 port_num, 0);
965
966                                 if (rio_enum_peer(net, port, hopcount + 1,
967                                                   rdev, port_num) < 0)
968                                         return -1;
969
970                                 /* Update routing tables */
971                                 destid = rio_destid_next(net, cur_destid + 1);
972                                 if (destid != RIO_INVALID_DESTID) {
973                                         for (destid = cur_destid;
974                                              destid < next_destid;) {
975                                                 if (destid != port->host_deviceid) {
976                                                         rio_route_add_entry(rdev,
977                                                                     RIO_GLOBAL_TABLE,
978                                                                     destid,
979                                                                     port_num,
980                                                                     0);
981                                                         rdev->rswitch->
982                                                                 route_table[destid] =
983                                                                 port_num;
984                                                 }
985                                                 destid = rio_destid_next(net,
986                                                                 destid + 1);
987                                         }
988                                 }
989                         } else {
990                                 /* If switch supports Error Management,
991                                  * set PORT_LOCKOUT bit for unused port
992                                  */
993                                 if (rdev->em_efptr)
994                                         rio_set_port_lockout(rdev, port_num, 1);
995
996                                 rdev->rswitch->port_ok &= ~(1 << port_num);
997                         }
998                 }
999
1000                 /* Direct Port-write messages to the enumeratiing host */
1001                 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
1002                     (rdev->em_efptr)) {
1003                         rio_write_config_32(rdev,
1004                                         rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
1005                                         (port->host_deviceid << 16) |
1006                                         (port->sys_size << 15));
1007                 }
1008
1009                 rio_init_em(rdev);
1010
1011                 /* Check for empty switch */
1012                 if (next_destid == sw_destid)
1013                         next_destid = rio_destid_alloc(net);
1014
1015                 rdev->destid = sw_destid;
1016         } else
1017                 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
1018                     rio_name(rdev), rdev->vid, rdev->did);
1019
1020         return 0;
1021 }
1022
1023 /**
1024  * rio_enum_complete- Tests if enumeration of a network is complete
1025  * @port: Master port to send transaction
1026  *
1027  * Tests the PGCCSR discovered bit for non-zero value (enumeration
1028  * complete flag). Return %1 if enumeration is complete or %0 if
1029  * enumeration is incomplete.
1030  */
1031 static int rio_enum_complete(struct rio_mport *port)
1032 {
1033         u32 regval;
1034
1035         rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
1036                                  &regval);
1037         return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
1038 }
1039
1040 /**
1041  * rio_disc_peer- Recursively discovers a RIO network through a master port
1042  * @net: RIO network being discovered
1043  * @port: Master port to send transactions
1044  * @destid: Current destination ID in network
1045  * @hopcount: Number of hops into the network
1046  * @prev: previous rio_dev
1047  * @prev_port: previous port number
1048  *
1049  * Recursively discovers a RIO network.  Transactions are sent via the
1050  * master port passed in @port.
1051  */
1052 static int __devinit
1053 rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
1054               u8 hopcount, struct rio_dev *prev, int prev_port)
1055 {
1056         u8 port_num, route_port;
1057         struct rio_dev *rdev;
1058         u16 ndestid;
1059
1060         /* Setup new RIO device */
1061         if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
1062                 /* Add device to the global and bus/net specific list. */
1063                 list_add_tail(&rdev->net_list, &net->devices);
1064                 rdev->prev = prev;
1065                 if (prev && rio_is_switch(prev))
1066                         prev->rswitch->nextdev[prev_port] = rdev;
1067         } else
1068                 return -1;
1069
1070         if (rio_is_switch(rdev)) {
1071                 /* Associated destid is how we accessed this switch */
1072                 rdev->destid = destid;
1073
1074                 pr_debug(
1075                     "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
1076                     rio_name(rdev), rdev->vid, rdev->did,
1077                     RIO_GET_TOTAL_PORTS(rdev->swpinfo));
1078                 for (port_num = 0;
1079                      port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
1080                      port_num++) {
1081                         if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
1082                                 continue;
1083
1084                         if (rio_sport_is_active
1085                             (port, destid, hopcount, port_num)) {
1086                                 pr_debug(
1087                                     "RIO: scanning device on port %d\n",
1088                                     port_num);
1089
1090                                 rio_lock_device(port, destid, hopcount, 1000);
1091
1092                                 for (ndestid = 0;
1093                                      ndestid < RIO_ANY_DESTID(port->sys_size);
1094                                      ndestid++) {
1095                                         rio_route_get_entry(rdev,
1096                                                             RIO_GLOBAL_TABLE,
1097                                                             ndestid,
1098                                                             &route_port, 0);
1099                                         if (route_port == port_num)
1100                                                 break;
1101                                 }
1102
1103                                 if (ndestid == RIO_ANY_DESTID(port->sys_size))
1104                                         continue;
1105                                 rio_unlock_device(port, destid, hopcount);
1106                                 if (rio_disc_peer(net, port, ndestid,
1107                                         hopcount + 1, rdev, port_num) < 0)
1108                                         return -1;
1109                         }
1110                 }
1111         } else
1112                 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
1113                     rio_name(rdev), rdev->vid, rdev->did);
1114
1115         return 0;
1116 }
1117
1118 /**
1119  * rio_mport_is_active- Tests if master port link is active
1120  * @port: Master port to test
1121  *
1122  * Reads the port error status CSR for the master port to
1123  * determine if the port has an active link.  Returns
1124  * %RIO_PORT_N_ERR_STS_PORT_OK if the  master port is active
1125  * or %0 if it is inactive.
1126  */
1127 static int rio_mport_is_active(struct rio_mport *port)
1128 {
1129         u32 result = 0;
1130         u32 ext_ftr_ptr;
1131         int *entry = rio_mport_phys_table;
1132
1133         do {
1134                 if ((ext_ftr_ptr =
1135                      rio_mport_get_feature(port, 1, 0, 0, *entry)))
1136                         break;
1137         } while (*++entry >= 0);
1138
1139         if (ext_ftr_ptr)
1140                 rio_local_read_config_32(port,
1141                                          ext_ftr_ptr +
1142                                          RIO_PORT_N_ERR_STS_CSR(port->index),
1143                                          &result);
1144
1145         return result & RIO_PORT_N_ERR_STS_PORT_OK;
1146 }
1147
1148 /**
1149  * rio_alloc_net- Allocate and configure a new RIO network
1150  * @port: Master port associated with the RIO network
1151  * @do_enum: Enumeration/Discovery mode flag
1152  * @start: logical minimal start id for new net
1153  *
1154  * Allocates a RIO network structure, initializes per-network
1155  * list heads, and adds the associated master port to the
1156  * network list of associated master ports. Returns a
1157  * RIO network pointer on success or %NULL on failure.
1158  */
1159 static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port,
1160                                                int do_enum, u16 start)
1161 {
1162         struct rio_net *net;
1163
1164         net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
1165         if (net && do_enum) {
1166                 net->destid_table.table = kzalloc(
1167                         BITS_TO_LONGS(RIO_MAX_ROUTE_ENTRIES(port->sys_size)) *
1168                         sizeof(long),
1169                         GFP_KERNEL);
1170
1171                 if (net->destid_table.table == NULL) {
1172                         pr_err("RIO: failed to allocate destID table\n");
1173                         kfree(net);
1174                         net = NULL;
1175                 } else {
1176                         net->destid_table.start = start;
1177                         net->destid_table.next = 0;
1178                         net->destid_table.max =
1179                                         RIO_MAX_ROUTE_ENTRIES(port->sys_size);
1180                         spin_lock_init(&net->destid_table.lock);
1181                 }
1182         }
1183
1184         if (net) {
1185                 INIT_LIST_HEAD(&net->node);
1186                 INIT_LIST_HEAD(&net->devices);
1187                 INIT_LIST_HEAD(&net->switches);
1188                 INIT_LIST_HEAD(&net->mports);
1189                 list_add_tail(&port->nnode, &net->mports);
1190                 net->hport = port;
1191                 net->id = port->id;
1192         }
1193         return net;
1194 }
1195
1196 /**
1197  * rio_update_route_tables- Updates route tables in switches
1198  * @net: RIO network to run update on
1199  *
1200  * For each enumerated device, ensure that each switch in a system
1201  * has correct routing entries. Add routes for devices that where
1202  * unknown dirung the first enumeration pass through the switch.
1203  */
1204 static void rio_update_route_tables(struct rio_net *net)
1205 {
1206         struct rio_dev *rdev, *swrdev;
1207         struct rio_switch *rswitch;
1208         u8 sport;
1209         u16 destid;
1210
1211         list_for_each_entry(rdev, &net->devices, net_list) {
1212
1213                 destid = rdev->destid;
1214
1215                 list_for_each_entry(rswitch, &net->switches, node) {
1216
1217                         if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
1218                                 continue;
1219
1220                         if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
1221                                 swrdev = sw_to_rio_dev(rswitch);
1222
1223                                 /* Skip if destid ends in empty switch*/
1224                                 if (swrdev->destid == destid)
1225                                         continue;
1226
1227                                 sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
1228
1229                                 if (rswitch->add_entry) {
1230                                         rio_route_add_entry(swrdev,
1231                                                 RIO_GLOBAL_TABLE, destid,
1232                                                 sport, 0);
1233                                         rswitch->route_table[destid] = sport;
1234                                 }
1235                         }
1236                 }
1237         }
1238 }
1239
1240 /**
1241  * rio_init_em - Initializes RIO Error Management (for switches)
1242  * @rdev: RIO device
1243  *
1244  * For each enumerated switch, call device-specific error management
1245  * initialization routine (if supplied by the switch driver).
1246  */
1247 static void rio_init_em(struct rio_dev *rdev)
1248 {
1249         if (rio_is_switch(rdev) && (rdev->em_efptr) &&
1250             (rdev->rswitch->em_init)) {
1251                 rdev->rswitch->em_init(rdev);
1252         }
1253 }
1254
1255 /**
1256  * rio_pw_enable - Enables/disables port-write handling by a master port
1257  * @port: Master port associated with port-write handling
1258  * @enable:  1=enable,  0=disable
1259  */
1260 static void rio_pw_enable(struct rio_mport *port, int enable)
1261 {
1262         if (port->ops->pwenable)
1263                 port->ops->pwenable(port, enable);
1264 }
1265
1266 /**
1267  * rio_enum_mport- Start enumeration through a master port
1268  * @mport: Master port to send transactions
1269  *
1270  * Starts the enumeration process. If somebody has enumerated our
1271  * master port device, then give up. If not and we have an active
1272  * link, then start recursive peer enumeration. Returns %0 if
1273  * enumeration succeeds or %-EBUSY if enumeration fails.
1274  */
1275 int __devinit rio_enum_mport(struct rio_mport *mport)
1276 {
1277         struct rio_net *net = NULL;
1278         int rc = 0;
1279
1280         printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
1281                mport->name);
1282         /* If somebody else enumerated our master port device, bail. */
1283         if (rio_enum_host(mport) < 0) {
1284                 printk(KERN_INFO
1285                        "RIO: master port %d device has been enumerated by a remote host\n",
1286                        mport->id);
1287                 rc = -EBUSY;
1288                 goto out;
1289         }
1290
1291         /* If master port has an active link, allocate net and enum peers */
1292         if (rio_mport_is_active(mport)) {
1293                 net = rio_alloc_net(mport, 1, 0);
1294                 if (!net) {
1295                         printk(KERN_ERR "RIO: failed to allocate new net\n");
1296                         rc = -ENOMEM;
1297                         goto out;
1298                 }
1299
1300                 /* reserve mport destID in new net */
1301                 rio_destid_reserve(net, mport->host_deviceid);
1302
1303                 /* Enable Input Output Port (transmitter reviever) */
1304                 rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
1305
1306                 /* Set component tag for host */
1307                 rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
1308                                           next_comptag++);
1309
1310                 next_destid = rio_destid_alloc(net);
1311
1312                 if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
1313                         /* A higher priority host won enumeration, bail. */
1314                         printk(KERN_INFO
1315                                "RIO: master port %d device has lost enumeration to a remote host\n",
1316                                mport->id);
1317                         rio_clear_locks(net);
1318                         rc = -EBUSY;
1319                         goto out;
1320                 }
1321                 /* free the last allocated destID (unused) */
1322                 rio_destid_free(net, next_destid);
1323                 rio_update_route_tables(net);
1324                 rio_clear_locks(net);
1325                 rio_pw_enable(mport, 1);
1326         } else {
1327                 printk(KERN_INFO "RIO: master port %d link inactive\n",
1328                        mport->id);
1329                 rc = -EINVAL;
1330         }
1331
1332       out:
1333         return rc;
1334 }
1335
1336 /**
1337  * rio_build_route_tables- Generate route tables from switch route entries
1338  * @net: RIO network to run route tables scan on
1339  *
1340  * For each switch device, generate a route table by copying existing
1341  * route entries from the switch.
1342  */
1343 static void rio_build_route_tables(struct rio_net *net)
1344 {
1345         struct rio_switch *rswitch;
1346         struct rio_dev *rdev;
1347         int i;
1348         u8 sport;
1349
1350         list_for_each_entry(rswitch, &net->switches, node) {
1351                 rdev = sw_to_rio_dev(rswitch);
1352
1353                 rio_lock_device(net->hport, rdev->destid,
1354                                 rdev->hopcount, 1000);
1355                 for (i = 0;
1356                      i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
1357                      i++) {
1358                         if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
1359                                                 i, &sport, 0) < 0)
1360                                 continue;
1361                         rswitch->route_table[i] = sport;
1362                 }
1363
1364                 rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
1365         }
1366 }
1367
1368 /**
1369  * rio_disc_mport- Start discovery through a master port
1370  * @mport: Master port to send transactions
1371  *
1372  * Starts the discovery process. If we have an active link,
1373  * then wait for the signal that enumeration is complete.
1374  * When enumeration completion is signaled, start recursive
1375  * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1376  * on failure.
1377  */
1378 int __devinit rio_disc_mport(struct rio_mport *mport)
1379 {
1380         struct rio_net *net = NULL;
1381         unsigned long to_end;
1382
1383         printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1384                mport->name);
1385
1386         /* If master port has an active link, allocate net and discover peers */
1387         if (rio_mport_is_active(mport)) {
1388                 pr_debug("RIO: wait for enumeration to complete...\n");
1389
1390                 to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1391                 while (time_before(jiffies, to_end)) {
1392                         if (rio_enum_complete(mport))
1393                                 goto enum_done;
1394                         schedule_timeout_uninterruptible(msecs_to_jiffies(10));
1395                 }
1396
1397                 pr_debug("RIO: discovery timeout on mport %d %s\n",
1398                          mport->id, mport->name);
1399                 goto bail;
1400 enum_done:
1401                 pr_debug("RIO: ... enumeration done\n");
1402
1403                 net = rio_alloc_net(mport, 0, 0);
1404                 if (!net) {
1405                         printk(KERN_ERR "RIO: Failed to allocate new net\n");
1406                         goto bail;
1407                 }
1408
1409                 /* Read DestID assigned by enumerator */
1410                 rio_local_read_config_32(mport, RIO_DID_CSR,
1411                                          &mport->host_deviceid);
1412                 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1413                                                    mport->host_deviceid);
1414
1415                 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1416                                         0, NULL, 0) < 0) {
1417                         printk(KERN_INFO
1418                                "RIO: master port %d device has failed discovery\n",
1419                                mport->id);
1420                         goto bail;
1421                 }
1422
1423                 rio_build_route_tables(net);
1424         }
1425
1426         return 0;
1427 bail:
1428         return -EBUSY;
1429 }