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