]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/rapidio/rio-scan.c
rapidio: fix RapidIO sysfs hierarchy
[karo-tx-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         rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
424                                  &rdev->src_ops);
425         rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
426                                  &rdev->dst_ops);
427
428         if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
429                 if (do_enum) {
430                         rio_set_device_id(port, destid, hopcount, next_destid);
431                         rdev->destid = next_destid++;
432                         if (next_destid == port->host_deviceid)
433                                 next_destid++;
434                 } else
435                         rdev->destid = rio_get_device_id(port, destid, hopcount);
436         } else
437                 /* Switch device has an associated destID */
438                 rdev->destid = RIO_INVALID_DESTID;
439
440         /* If a PE has both switch and other functions, show it as a switch */
441         if (rio_is_switch(rdev)) {
442                 rio_mport_read_config_32(port, destid, hopcount,
443                                          RIO_SWP_INFO_CAR, &rdev->swpinfo);
444                 rswitch = kzalloc(sizeof(struct rio_switch), GFP_KERNEL);
445                 if (!rswitch)
446                         goto cleanup;
447                 rswitch->switchid = next_switchid;
448                 rswitch->hopcount = hopcount;
449                 rswitch->destid = destid;
450                 rswitch->port_ok = 0;
451                 rswitch->route_table = kzalloc(sizeof(u8)*
452                                         RIO_MAX_ROUTE_ENTRIES(port->sys_size),
453                                         GFP_KERNEL);
454                 if (!rswitch->route_table)
455                         goto cleanup;
456                 /* Initialize switch route table */
457                 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
458                                 rdid++)
459                         rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
460                 rdev->rswitch = rswitch;
461                 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
462                              rdev->rswitch->switchid);
463                 rio_switch_init(rdev, do_enum);
464
465                 if (do_enum && rdev->rswitch->clr_table)
466                         rdev->rswitch->clr_table(port, destid, hopcount,
467                                                  RIO_GLOBAL_TABLE);
468
469                 list_add_tail(&rswitch->node, &rio_switches);
470
471         } else {
472                 if (do_enum)
473                         /*Enable Input Output Port (transmitter reviever)*/
474                         rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
475
476                 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
477                              rdev->destid);
478         }
479
480         rdev->dev.bus = &rio_bus_type;
481         rdev->dev.parent = &rio_bus;
482
483         device_initialize(&rdev->dev);
484         rdev->dev.release = rio_release_dev;
485         rio_dev_get(rdev);
486
487         rdev->dma_mask = DMA_BIT_MASK(32);
488         rdev->dev.dma_mask = &rdev->dma_mask;
489         rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
490
491         if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
492             (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
493                 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
494                                    0, 0xffff);
495
496         ret = rio_add_device(rdev);
497         if (ret)
498                 goto cleanup;
499
500         return rdev;
501
502 cleanup:
503         if (rswitch) {
504                 kfree(rswitch->route_table);
505                 kfree(rswitch);
506         }
507         kfree(rdev);
508         return NULL;
509 }
510
511 /**
512  * rio_sport_is_active- Tests if a switch port has an active connection.
513  * @port: Master port to send transaction
514  * @destid: Associated destination ID for switch
515  * @hopcount: Hopcount to reach switch
516  * @sport: Switch port number
517  *
518  * Reads the port error status CSR for a particular switch port to
519  * determine if the port has an active link.  Returns
520  * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
521  * inactive.
522  */
523 static int
524 rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
525 {
526         u32 result = 0;
527         u32 ext_ftr_ptr;
528
529         ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
530
531         while (ext_ftr_ptr) {
532                 rio_mport_read_config_32(port, destid, hopcount,
533                                          ext_ftr_ptr, &result);
534                 result = RIO_GET_BLOCK_ID(result);
535                 if ((result == RIO_EFB_SER_EP_FREE_ID) ||
536                     (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
537                     (result == RIO_EFB_SER_EP_FREC_ID))
538                         break;
539
540                 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
541                                                 ext_ftr_ptr);
542         }
543
544         if (ext_ftr_ptr)
545                 rio_mport_read_config_32(port, destid, hopcount,
546                                          ext_ftr_ptr +
547                                          RIO_PORT_N_ERR_STS_CSR(sport),
548                                          &result);
549
550         return result & RIO_PORT_N_ERR_STS_PORT_OK;
551 }
552
553 /**
554  * rio_lock_device - Acquires host device lock for specified device
555  * @port: Master port to send transaction
556  * @destid: Destination ID for device/switch
557  * @hopcount: Hopcount to reach switch
558  * @wait_ms: Max wait time in msec (0 = no timeout)
559  *
560  * Attepts to acquire host device lock for specified device
561  * Returns 0 if device lock acquired or EINVAL if timeout expires.
562  */
563 static int
564 rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
565 {
566         u32 result;
567         int tcnt = 0;
568
569         /* Attempt to acquire device lock */
570         rio_mport_write_config_32(port, destid, hopcount,
571                                   RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
572         rio_mport_read_config_32(port, destid, hopcount,
573                                  RIO_HOST_DID_LOCK_CSR, &result);
574
575         while (result != port->host_deviceid) {
576                 if (wait_ms != 0 && tcnt == wait_ms) {
577                         pr_debug("RIO: timeout when locking device %x:%x\n",
578                                 destid, hopcount);
579                         return -EINVAL;
580                 }
581
582                 /* Delay a bit */
583                 mdelay(1);
584                 tcnt++;
585                 /* Try to acquire device lock again */
586                 rio_mport_write_config_32(port, destid,
587                         hopcount,
588                         RIO_HOST_DID_LOCK_CSR,
589                         port->host_deviceid);
590                 rio_mport_read_config_32(port, destid,
591                         hopcount,
592                         RIO_HOST_DID_LOCK_CSR, &result);
593         }
594
595         return 0;
596 }
597
598 /**
599  * rio_unlock_device - Releases host device lock for specified device
600  * @port: Master port to send transaction
601  * @destid: Destination ID for device/switch
602  * @hopcount: Hopcount to reach switch
603  *
604  * Returns 0 if device lock released or EINVAL if fails.
605  */
606 static int
607 rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
608 {
609         u32 result;
610
611         /* Release device lock */
612         rio_mport_write_config_32(port, destid,
613                                   hopcount,
614                                   RIO_HOST_DID_LOCK_CSR,
615                                   port->host_deviceid);
616         rio_mport_read_config_32(port, destid, hopcount,
617                 RIO_HOST_DID_LOCK_CSR, &result);
618         if ((result & 0xffff) != 0xffff) {
619                 pr_debug("RIO: badness when releasing device lock %x:%x\n",
620                          destid, hopcount);
621                 return -EINVAL;
622         }
623
624         return 0;
625 }
626
627 /**
628  * rio_route_add_entry- Add a route entry to a switch routing table
629  * @mport: Master port to send transaction
630  * @rswitch: Switch device
631  * @table: Routing table ID
632  * @route_destid: Destination ID to be routed
633  * @route_port: Port number to be routed
634  * @lock: lock switch device flag
635  *
636  * Calls the switch specific add_entry() method to add a route entry
637  * on a switch. The route table can be specified using the @table
638  * argument if a switch has per port routing tables or the normal
639  * use is to specific all tables (or the global table) by passing
640  * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
641  * on failure.
642  */
643 static int
644 rio_route_add_entry(struct rio_mport *mport, struct rio_switch *rswitch,
645                     u16 table, u16 route_destid, u8 route_port, int lock)
646 {
647         int rc;
648
649         if (lock) {
650                 rc = rio_lock_device(mport, rswitch->destid,
651                                      rswitch->hopcount, 1000);
652                 if (rc)
653                         return rc;
654         }
655
656         rc = rswitch->add_entry(mport, rswitch->destid,
657                                         rswitch->hopcount, table,
658                                         route_destid, route_port);
659         if (lock)
660                 rio_unlock_device(mport, rswitch->destid, rswitch->hopcount);
661
662         return rc;
663 }
664
665 /**
666  * rio_route_get_entry- Read a route entry in a switch routing table
667  * @mport: Master port to send transaction
668  * @rswitch: Switch device
669  * @table: Routing table ID
670  * @route_destid: Destination ID to be routed
671  * @route_port: Pointer to read port number into
672  * @lock: lock switch device flag
673  *
674  * Calls the switch specific get_entry() method to read a route entry
675  * in a switch. The route table can be specified using the @table
676  * argument if a switch has per port routing tables or the normal
677  * use is to specific all tables (or the global table) by passing
678  * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
679  * on failure.
680  */
681 static int
682 rio_route_get_entry(struct rio_mport *mport, struct rio_switch *rswitch, u16 table,
683                     u16 route_destid, u8 *route_port, int lock)
684 {
685         int rc;
686
687         if (lock) {
688                 rc = rio_lock_device(mport, rswitch->destid,
689                                      rswitch->hopcount, 1000);
690                 if (rc)
691                         return rc;
692         }
693
694         rc = rswitch->get_entry(mport, rswitch->destid,
695                                         rswitch->hopcount, table,
696                                         route_destid, route_port);
697         if (lock)
698                 rio_unlock_device(mport, rswitch->destid, rswitch->hopcount);
699
700         return rc;
701 }
702
703 /**
704  * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
705  * @port: Master port to send transaction
706  * @hopcount: Number of hops to the device
707  *
708  * Used during enumeration to read the Host Device ID Lock CSR on a
709  * RIO device. Returns the value of the lock register.
710  */
711 static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
712 {
713         u32 result;
714
715         rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
716                                  RIO_HOST_DID_LOCK_CSR, &result);
717
718         return (u16) (result & 0xffff);
719 }
720
721 /**
722  * rio_get_swpinfo_inport- Gets the ingress port number
723  * @mport: Master port to send transaction
724  * @destid: Destination ID associated with the switch
725  * @hopcount: Number of hops to the device
726  *
727  * Returns port number being used to access the switch device.
728  */
729 static u8
730 rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
731 {
732         u32 result;
733
734         rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
735                                  &result);
736
737         return (u8) (result & 0xff);
738 }
739
740 /**
741  * rio_get_swpinfo_tports- Gets total number of ports on the switch
742  * @mport: Master port to send transaction
743  * @destid: Destination ID associated with the switch
744  * @hopcount: Number of hops to the device
745  *
746  * Returns total numbers of ports implemented by the switch device.
747  */
748 static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
749                                  u8 hopcount)
750 {
751         u32 result;
752
753         rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
754                                  &result);
755
756         return RIO_GET_TOTAL_PORTS(result);
757 }
758
759 /**
760  * rio_net_add_mport- Add a master port to a RIO network
761  * @net: RIO network
762  * @port: Master port to add
763  *
764  * Adds a master port to the network list of associated master
765  * ports..
766  */
767 static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
768 {
769         spin_lock(&rio_global_list_lock);
770         list_add_tail(&port->nnode, &net->mports);
771         spin_unlock(&rio_global_list_lock);
772 }
773
774 /**
775  * rio_enum_peer- Recursively enumerate a RIO network through a master port
776  * @net: RIO network being enumerated
777  * @port: Master port to send transactions
778  * @hopcount: Number of hops into the network
779  *
780  * Recursively enumerates a RIO network.  Transactions are sent via the
781  * master port passed in @port.
782  */
783 static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
784                          u8 hopcount)
785 {
786         int port_num;
787         int num_ports;
788         int cur_destid;
789         int sw_destid;
790         int sw_inport;
791         struct rio_dev *rdev;
792         u16 destid;
793         int tmp;
794
795         if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
796                 pr_debug("RIO: PE already discovered by this host\n");
797                 /*
798                  * Already discovered by this host. Add it as another
799                  * master port for the current network.
800                  */
801                 rio_net_add_mport(net, port);
802                 return 0;
803         }
804
805         /* Attempt to acquire device lock */
806         rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
807                                   hopcount,
808                                   RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
809         while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
810                < port->host_deviceid) {
811                 /* Delay a bit */
812                 mdelay(1);
813                 /* Attempt to acquire device lock again */
814                 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
815                                           hopcount,
816                                           RIO_HOST_DID_LOCK_CSR,
817                                           port->host_deviceid);
818         }
819
820         if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
821                 pr_debug(
822                     "RIO: PE locked by a higher priority host...retreating\n");
823                 return -1;
824         }
825
826         /* Setup new RIO device */
827         rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
828                                         hopcount, 1);
829         if (rdev) {
830                 /* Add device to the global and bus/net specific list. */
831                 list_add_tail(&rdev->net_list, &net->devices);
832         } else
833                 return -1;
834
835         if (rio_is_switch(rdev)) {
836                 next_switchid++;
837                 sw_inport = rio_get_swpinfo_inport(port,
838                                 RIO_ANY_DESTID(port->sys_size), hopcount);
839                 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
840                                     port->host_deviceid, sw_inport, 0);
841                 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
842
843                 for (destid = 0; destid < next_destid; destid++) {
844                         if (destid == port->host_deviceid)
845                                 continue;
846                         rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
847                                             destid, sw_inport, 0);
848                         rdev->rswitch->route_table[destid] = sw_inport;
849                 }
850
851                 num_ports =
852                     rio_get_swpinfo_tports(port, RIO_ANY_DESTID(port->sys_size),
853                                                 hopcount);
854                 pr_debug(
855                     "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
856                     rio_name(rdev), rdev->vid, rdev->did, num_ports);
857                 sw_destid = next_destid;
858                 for (port_num = 0; port_num < num_ports; port_num++) {
859                         /*Enable Input Output Port (transmitter reviever)*/
860                         rio_enable_rx_tx_port(port, 0,
861                                               RIO_ANY_DESTID(port->sys_size),
862                                               hopcount, port_num);
863
864                         if (sw_inport == port_num) {
865                                 rdev->rswitch->port_ok |= (1 << port_num);
866                                 continue;
867                         }
868
869                         cur_destid = next_destid;
870
871                         if (rio_sport_is_active
872                             (port, RIO_ANY_DESTID(port->sys_size), hopcount,
873                              port_num)) {
874                                 pr_debug(
875                                     "RIO: scanning device on port %d\n",
876                                     port_num);
877                                 rdev->rswitch->port_ok |= (1 << port_num);
878                                 rio_route_add_entry(port, rdev->rswitch,
879                                                 RIO_GLOBAL_TABLE,
880                                                 RIO_ANY_DESTID(port->sys_size),
881                                                 port_num, 0);
882
883                                 if (rio_enum_peer(net, port, hopcount + 1) < 0)
884                                         return -1;
885
886                                 /* Update routing tables */
887                                 if (next_destid > cur_destid) {
888                                         for (destid = cur_destid;
889                                              destid < next_destid; destid++) {
890                                                 if (destid == port->host_deviceid)
891                                                         continue;
892                                                 rio_route_add_entry(port, rdev->rswitch,
893                                                                     RIO_GLOBAL_TABLE,
894                                                                     destid,
895                                                                     port_num,
896                                                                     0);
897                                                 rdev->rswitch->
898                                                     route_table[destid] =
899                                                     port_num;
900                                         }
901                                 }
902                         } else {
903                                 /* If switch supports Error Management,
904                                  * set PORT_LOCKOUT bit for unused port
905                                  */
906                                 if (rdev->em_efptr)
907                                         rio_set_port_lockout(rdev, port_num, 1);
908
909                                 rdev->rswitch->port_ok &= ~(1 << port_num);
910                         }
911                 }
912
913                 /* Direct Port-write messages to the enumeratiing host */
914                 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
915                     (rdev->em_efptr)) {
916                         rio_write_config_32(rdev,
917                                         rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
918                                         (port->host_deviceid << 16) |
919                                         (port->sys_size << 15));
920                 }
921
922                 rio_init_em(rdev);
923
924                 /* Check for empty switch */
925                 if (next_destid == sw_destid) {
926                         next_destid++;
927                         if (next_destid == port->host_deviceid)
928                                 next_destid++;
929                 }
930
931                 rdev->rswitch->destid = sw_destid;
932         } else
933                 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
934                     rio_name(rdev), rdev->vid, rdev->did);
935
936         return 0;
937 }
938
939 /**
940  * rio_enum_complete- Tests if enumeration of a network is complete
941  * @port: Master port to send transaction
942  *
943  * Tests the Component Tag CSR for non-zero value (enumeration
944  * complete flag). Return %1 if enumeration is complete or %0 if
945  * enumeration is incomplete.
946  */
947 static int rio_enum_complete(struct rio_mport *port)
948 {
949         u32 tag_csr;
950
951         rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
952         return (tag_csr & 0xffff) ? 1 : 0;
953 }
954
955 /**
956  * rio_disc_peer- Recursively discovers a RIO network through a master port
957  * @net: RIO network being discovered
958  * @port: Master port to send transactions
959  * @destid: Current destination ID in network
960  * @hopcount: Number of hops into the network
961  *
962  * Recursively discovers a RIO network.  Transactions are sent via the
963  * master port passed in @port.
964  */
965 static int __devinit
966 rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
967               u8 hopcount)
968 {
969         u8 port_num, route_port;
970         int num_ports;
971         struct rio_dev *rdev;
972         u16 ndestid;
973
974         /* Setup new RIO device */
975         if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
976                 /* Add device to the global and bus/net specific list. */
977                 list_add_tail(&rdev->net_list, &net->devices);
978         } else
979                 return -1;
980
981         if (rio_is_switch(rdev)) {
982                 next_switchid++;
983
984                 /* Associated destid is how we accessed this switch */
985                 rdev->rswitch->destid = destid;
986
987                 num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
988                 pr_debug(
989                     "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
990                     rio_name(rdev), rdev->vid, rdev->did, num_ports);
991                 for (port_num = 0; port_num < num_ports; port_num++) {
992                         if (rio_get_swpinfo_inport(port, destid, hopcount) ==
993                             port_num)
994                                 continue;
995
996                         if (rio_sport_is_active
997                             (port, destid, hopcount, port_num)) {
998                                 pr_debug(
999                                     "RIO: scanning device on port %d\n",
1000                                     port_num);
1001
1002                                 rio_lock_device(port, destid, hopcount, 1000);
1003
1004                                 for (ndestid = 0;
1005                                      ndestid < RIO_ANY_DESTID(port->sys_size);
1006                                      ndestid++) {
1007                                         rio_route_get_entry(port, rdev->rswitch,
1008                                                             RIO_GLOBAL_TABLE,
1009                                                             ndestid,
1010                                                             &route_port, 0);
1011                                         if (route_port == port_num)
1012                                                 break;
1013                                 }
1014
1015                                 rio_unlock_device(port, destid, hopcount);
1016                                 if (rio_disc_peer
1017                                     (net, port, ndestid, hopcount + 1) < 0)
1018                                         return -1;
1019                         }
1020                 }
1021         } else
1022                 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
1023                     rio_name(rdev), rdev->vid, rdev->did);
1024
1025         return 0;
1026 }
1027
1028 /**
1029  * rio_mport_is_active- Tests if master port link is active
1030  * @port: Master port to test
1031  *
1032  * Reads the port error status CSR for the master port to
1033  * determine if the port has an active link.  Returns
1034  * %RIO_PORT_N_ERR_STS_PORT_OK if the  master port is active
1035  * or %0 if it is inactive.
1036  */
1037 static int rio_mport_is_active(struct rio_mport *port)
1038 {
1039         u32 result = 0;
1040         u32 ext_ftr_ptr;
1041         int *entry = rio_mport_phys_table;
1042
1043         do {
1044                 if ((ext_ftr_ptr =
1045                      rio_mport_get_feature(port, 1, 0, 0, *entry)))
1046                         break;
1047         } while (*++entry >= 0);
1048
1049         if (ext_ftr_ptr)
1050                 rio_local_read_config_32(port,
1051                                          ext_ftr_ptr +
1052                                          RIO_PORT_N_ERR_STS_CSR(port->index),
1053                                          &result);
1054
1055         return result & RIO_PORT_N_ERR_STS_PORT_OK;
1056 }
1057
1058 /**
1059  * rio_alloc_net- Allocate and configure a new RIO network
1060  * @port: Master port associated with the RIO network
1061  *
1062  * Allocates a RIO network structure, initializes per-network
1063  * list heads, and adds the associated master port to the
1064  * network list of associated master ports. Returns a
1065  * RIO network pointer on success or %NULL on failure.
1066  */
1067 static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
1068 {
1069         struct rio_net *net;
1070
1071         net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
1072         if (net) {
1073                 INIT_LIST_HEAD(&net->node);
1074                 INIT_LIST_HEAD(&net->devices);
1075                 INIT_LIST_HEAD(&net->mports);
1076                 list_add_tail(&port->nnode, &net->mports);
1077                 net->hport = port;
1078                 net->id = next_net++;
1079         }
1080         return net;
1081 }
1082
1083 /**
1084  * rio_update_route_tables- Updates route tables in switches
1085  * @port: Master port associated with the RIO network
1086  *
1087  * For each enumerated device, ensure that each switch in a system
1088  * has correct routing entries. Add routes for devices that where
1089  * unknown dirung the first enumeration pass through the switch.
1090  */
1091 static void rio_update_route_tables(struct rio_mport *port)
1092 {
1093         struct rio_dev *rdev;
1094         struct rio_switch *rswitch;
1095         u8 sport;
1096         u16 destid;
1097
1098         list_for_each_entry(rdev, &rio_devices, global_list) {
1099
1100                 destid = (rio_is_switch(rdev))?rdev->rswitch->destid:rdev->destid;
1101
1102                 list_for_each_entry(rswitch, &rio_switches, node) {
1103
1104                         if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
1105                                 continue;
1106
1107                         if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
1108                                 /* Skip if destid ends in empty switch*/
1109                                 if (rswitch->destid == destid)
1110                                         continue;
1111
1112                                 sport = rio_get_swpinfo_inport(port,
1113                                                 rswitch->destid, rswitch->hopcount);
1114
1115                                 if (rswitch->add_entry) {
1116                                         rio_route_add_entry(port, rswitch,
1117                                                 RIO_GLOBAL_TABLE, destid,
1118                                                 sport, 0);
1119                                         rswitch->route_table[destid] = sport;
1120                                 }
1121                         }
1122                 }
1123         }
1124 }
1125
1126 /**
1127  * rio_init_em - Initializes RIO Error Management (for switches)
1128  * @rdev: RIO device
1129  *
1130  * For each enumerated switch, call device-specific error management
1131  * initialization routine (if supplied by the switch driver).
1132  */
1133 static void rio_init_em(struct rio_dev *rdev)
1134 {
1135         if (rio_is_switch(rdev) && (rdev->em_efptr) &&
1136             (rdev->rswitch->em_init)) {
1137                 rdev->rswitch->em_init(rdev);
1138         }
1139 }
1140
1141 /**
1142  * rio_pw_enable - Enables/disables port-write handling by a master port
1143  * @port: Master port associated with port-write handling
1144  * @enable:  1=enable,  0=disable
1145  */
1146 static void rio_pw_enable(struct rio_mport *port, int enable)
1147 {
1148         if (port->ops->pwenable)
1149                 port->ops->pwenable(port, enable);
1150 }
1151
1152 /**
1153  * rio_enum_mport- Start enumeration through a master port
1154  * @mport: Master port to send transactions
1155  *
1156  * Starts the enumeration process. If somebody has enumerated our
1157  * master port device, then give up. If not and we have an active
1158  * link, then start recursive peer enumeration. Returns %0 if
1159  * enumeration succeeds or %-EBUSY if enumeration fails.
1160  */
1161 int __devinit rio_enum_mport(struct rio_mport *mport)
1162 {
1163         struct rio_net *net = NULL;
1164         int rc = 0;
1165
1166         printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
1167                mport->name);
1168         /* If somebody else enumerated our master port device, bail. */
1169         if (rio_enum_host(mport) < 0) {
1170                 printk(KERN_INFO
1171                        "RIO: master port %d device has been enumerated by a remote host\n",
1172                        mport->id);
1173                 rc = -EBUSY;
1174                 goto out;
1175         }
1176
1177         /* If master port has an active link, allocate net and enum peers */
1178         if (rio_mport_is_active(mport)) {
1179                 if (!(net = rio_alloc_net(mport))) {
1180                         printk(KERN_ERR "RIO: failed to allocate new net\n");
1181                         rc = -ENOMEM;
1182                         goto out;
1183                 }
1184
1185                 /* Enable Input Output Port (transmitter reviever) */
1186                 rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
1187
1188                 if (rio_enum_peer(net, mport, 0) < 0) {
1189                         /* A higher priority host won enumeration, bail. */
1190                         printk(KERN_INFO
1191                                "RIO: master port %d device has lost enumeration to a remote host\n",
1192                                mport->id);
1193                         rio_clear_locks(mport);
1194                         rc = -EBUSY;
1195                         goto out;
1196                 }
1197                 rio_update_route_tables(mport);
1198                 rio_clear_locks(mport);
1199                 rio_pw_enable(mport, 1);
1200         } else {
1201                 printk(KERN_INFO "RIO: master port %d link inactive\n",
1202                        mport->id);
1203                 rc = -EINVAL;
1204         }
1205
1206       out:
1207         return rc;
1208 }
1209
1210 /**
1211  * rio_build_route_tables- Generate route tables from switch route entries
1212  *
1213  * For each switch device, generate a route table by copying existing
1214  * route entries from the switch.
1215  */
1216 static void rio_build_route_tables(void)
1217 {
1218         struct rio_dev *rdev;
1219         int i;
1220         u8 sport;
1221
1222         list_for_each_entry(rdev, &rio_devices, global_list)
1223                 if (rio_is_switch(rdev)) {
1224                         rio_lock_device(rdev->net->hport, rdev->rswitch->destid,
1225                                         rdev->rswitch->hopcount, 1000);
1226                         for (i = 0;
1227                              i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
1228                              i++) {
1229                                 if (rio_route_get_entry
1230                                     (rdev->net->hport, rdev->rswitch,
1231                                      RIO_GLOBAL_TABLE, i, &sport, 0) < 0)
1232                                         continue;
1233                                 rdev->rswitch->route_table[i] = sport;
1234                         }
1235
1236                         rio_unlock_device(rdev->net->hport,
1237                                           rdev->rswitch->destid,
1238                                           rdev->rswitch->hopcount);
1239                 }
1240 }
1241
1242 /**
1243  * rio_enum_timeout- Signal that enumeration timed out
1244  * @data: Address of timeout flag.
1245  *
1246  * When the enumeration complete timer expires, set a flag that
1247  * signals to the discovery process that enumeration did not
1248  * complete in a sane amount of time.
1249  */
1250 static void rio_enum_timeout(unsigned long data)
1251 {
1252         /* Enumeration timed out, set flag */
1253         *(int *)data = 1;
1254 }
1255
1256 /**
1257  * rio_disc_mport- Start discovery through a master port
1258  * @mport: Master port to send transactions
1259  *
1260  * Starts the discovery process. If we have an active link,
1261  * then wait for the signal that enumeration is complete.
1262  * When enumeration completion is signaled, start recursive
1263  * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1264  * on failure.
1265  */
1266 int __devinit rio_disc_mport(struct rio_mport *mport)
1267 {
1268         struct rio_net *net = NULL;
1269         int enum_timeout_flag = 0;
1270
1271         printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1272                mport->name);
1273
1274         /* If master port has an active link, allocate net and discover peers */
1275         if (rio_mport_is_active(mport)) {
1276                 if (!(net = rio_alloc_net(mport))) {
1277                         printk(KERN_ERR "RIO: Failed to allocate new net\n");
1278                         goto bail;
1279                 }
1280
1281                 pr_debug("RIO: wait for enumeration complete...");
1282
1283                 rio_enum_timer.expires =
1284                     jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1285                 rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
1286                 add_timer(&rio_enum_timer);
1287                 while (!rio_enum_complete(mport)) {
1288                         mdelay(1);
1289                         if (enum_timeout_flag) {
1290                                 del_timer_sync(&rio_enum_timer);
1291                                 goto timeout;
1292                         }
1293                 }
1294                 del_timer_sync(&rio_enum_timer);
1295
1296                 pr_debug("done\n");
1297
1298                 /* Read DestID assigned by enumerator */
1299                 rio_local_read_config_32(mport, RIO_DID_CSR,
1300                                          &mport->host_deviceid);
1301                 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1302                                                    mport->host_deviceid);
1303
1304                 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1305                                         0) < 0) {
1306                         printk(KERN_INFO
1307                                "RIO: master port %d device has failed discovery\n",
1308                                mport->id);
1309                         goto bail;
1310                 }
1311
1312                 rio_build_route_tables();
1313         }
1314
1315         return 0;
1316
1317       timeout:
1318         pr_debug("timeout\n");
1319       bail:
1320         return -EBUSY;
1321 }