perf probe: Fix memory leak when synthesizing SDT probes
[linux-2.6-microblaze.git] / drivers / rapidio / rio-scan.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * RapidIO enumeration and discovery support
4  *
5  * Copyright 2005 MontaVista Software, Inc.
6  * Matt Porter <mporter@kernel.crashing.org>
7  *
8  * Copyright 2009 Integrated Device Technology, Inc.
9  * Alex Bounine <alexandre.bounine@idt.com>
10  * - Added Port-Write/Error Management initialization and handling
11  *
12  * Copyright 2009 Sysgo AG
13  * Thomas Moll <thomas.moll@sysgo.com>
14  * - Added Input- Output- enable functionality, to allow full communication
15  */
16
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19
20 #include <linux/delay.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/init.h>
23 #include <linux/rio.h>
24 #include <linux/rio_drv.h>
25 #include <linux/rio_ids.h>
26 #include <linux/rio_regs.h>
27 #include <linux/module.h>
28 #include <linux/spinlock.h>
29 #include <linux/timer.h>
30 #include <linux/sched.h>
31 #include <linux/jiffies.h>
32 #include <linux/slab.h>
33
34 #include "rio.h"
35
36 static void rio_init_em(struct rio_dev *rdev);
37
38 struct rio_id_table {
39         u16 start;      /* logical minimal id */
40         u32 max;        /* max number of IDs in table */
41         spinlock_t lock;
42         unsigned long table[];
43 };
44
45 static int next_destid = 0;
46 static int next_comptag = 1;
47
48 /**
49  * rio_destid_alloc - Allocate next available destID for given network
50  * @net: RIO network
51  *
52  * Returns next available device destination ID for the specified RIO network.
53  * Marks allocated ID as one in use.
54  * Returns RIO_INVALID_DESTID if new destID is not available.
55  */
56 static u16 rio_destid_alloc(struct rio_net *net)
57 {
58         int destid;
59         struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
60
61         spin_lock(&idtab->lock);
62         destid = find_first_zero_bit(idtab->table, idtab->max);
63
64         if (destid < idtab->max) {
65                 set_bit(destid, idtab->table);
66                 destid += idtab->start;
67         } else
68                 destid = RIO_INVALID_DESTID;
69
70         spin_unlock(&idtab->lock);
71         return (u16)destid;
72 }
73
74 /**
75  * rio_destid_reserve - Reserve the specified destID
76  * @net: RIO network
77  * @destid: destID to reserve
78  *
79  * Tries to reserve the specified destID.
80  * Returns 0 if successful.
81  */
82 static int rio_destid_reserve(struct rio_net *net, u16 destid)
83 {
84         int oldbit;
85         struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
86
87         destid -= idtab->start;
88         spin_lock(&idtab->lock);
89         oldbit = test_and_set_bit(destid, idtab->table);
90         spin_unlock(&idtab->lock);
91         return oldbit;
92 }
93
94 /**
95  * rio_destid_free - free a previously allocated destID
96  * @net: RIO network
97  * @destid: destID to free
98  *
99  * Makes the specified destID available for use.
100  */
101 static void rio_destid_free(struct rio_net *net, u16 destid)
102 {
103         struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
104
105         destid -= idtab->start;
106         spin_lock(&idtab->lock);
107         clear_bit(destid, idtab->table);
108         spin_unlock(&idtab->lock);
109 }
110
111 /**
112  * rio_destid_first - return first destID in use
113  * @net: RIO network
114  */
115 static u16 rio_destid_first(struct rio_net *net)
116 {
117         int destid;
118         struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
119
120         spin_lock(&idtab->lock);
121         destid = find_first_bit(idtab->table, idtab->max);
122         if (destid >= idtab->max)
123                 destid = RIO_INVALID_DESTID;
124         else
125                 destid += idtab->start;
126         spin_unlock(&idtab->lock);
127         return (u16)destid;
128 }
129
130 /**
131  * rio_destid_next - return next destID in use
132  * @net: RIO network
133  * @from: destination ID from which search shall continue
134  */
135 static u16 rio_destid_next(struct rio_net *net, u16 from)
136 {
137         int destid;
138         struct rio_id_table *idtab = (struct rio_id_table *)net->enum_data;
139
140         spin_lock(&idtab->lock);
141         destid = find_next_bit(idtab->table, idtab->max, from);
142         if (destid >= idtab->max)
143                 destid = RIO_INVALID_DESTID;
144         else
145                 destid += idtab->start;
146         spin_unlock(&idtab->lock);
147         return (u16)destid;
148 }
149
150 /**
151  * rio_get_device_id - Get the base/extended device id for a device
152  * @port: RIO master port
153  * @destid: Destination ID of device
154  * @hopcount: Hopcount to device
155  *
156  * Reads the base/extended device id from a device. Returns the
157  * 8/16-bit device ID.
158  */
159 static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
160 {
161         u32 result;
162
163         rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
164
165         return RIO_GET_DID(port->sys_size, result);
166 }
167
168 /**
169  * rio_set_device_id - Set the base/extended device id for a device
170  * @port: RIO master port
171  * @destid: Destination ID of device
172  * @hopcount: Hopcount to device
173  * @did: Device ID value to be written
174  *
175  * Writes the base/extended device id from a device.
176  */
177 static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
178 {
179         rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
180                                   RIO_SET_DID(port->sys_size, did));
181 }
182
183 /**
184  * rio_clear_locks- Release all host locks and signal enumeration complete
185  * @net: RIO network to run on
186  *
187  * Marks the component tag CSR on each device with the enumeration
188  * complete flag. When complete, it then release the host locks on
189  * each device. Returns 0 on success or %-EINVAL on failure.
190  */
191 static int rio_clear_locks(struct rio_net *net)
192 {
193         struct rio_mport *port = net->hport;
194         struct rio_dev *rdev;
195         u32 result;
196         int ret = 0;
197
198         /* Release host device id locks */
199         rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
200                                   port->host_deviceid);
201         rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
202         if ((result & 0xffff) != 0xffff) {
203                 printk(KERN_INFO
204                        "RIO: badness when releasing host lock on master port, result %8.8x\n",
205                        result);
206                 ret = -EINVAL;
207         }
208         list_for_each_entry(rdev, &net->devices, net_list) {
209                 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
210                                     port->host_deviceid);
211                 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
212                 if ((result & 0xffff) != 0xffff) {
213                         printk(KERN_INFO
214                                "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
215                                rdev->vid, rdev->did);
216                         ret = -EINVAL;
217                 }
218
219                 /* Mark device as discovered and enable master */
220                 rio_read_config_32(rdev,
221                                    rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
222                                    &result);
223                 result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
224                 rio_write_config_32(rdev,
225                                     rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
226                                     result);
227         }
228
229         return ret;
230 }
231
232 /**
233  * rio_enum_host- Set host lock and initialize host destination ID
234  * @port: Master port to issue transaction
235  *
236  * Sets the local host master port lock and destination ID register
237  * with the host device ID value. The host device ID value is provided
238  * by the platform. Returns %0 on success or %-1 on failure.
239  */
240 static int rio_enum_host(struct rio_mport *port)
241 {
242         u32 result;
243
244         /* Set master port host device id lock */
245         rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
246                                   port->host_deviceid);
247
248         rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
249         if ((result & 0xffff) != port->host_deviceid)
250                 return -1;
251
252         /* Set master port destid and init destid ctr */
253         rio_local_set_device_id(port, port->host_deviceid);
254         return 0;
255 }
256
257 /**
258  * rio_device_has_destid- Test if a device contains a destination ID register
259  * @port: Master port to issue transaction
260  * @src_ops: RIO device source operations
261  * @dst_ops: RIO device destination operations
262  *
263  * Checks the provided @src_ops and @dst_ops for the necessary transaction
264  * capabilities that indicate whether or not a device will implement a
265  * destination ID register. Returns 1 if true or 0 if false.
266  */
267 static int rio_device_has_destid(struct rio_mport *port, int src_ops,
268                                  int dst_ops)
269 {
270         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;
271
272         return !!((src_ops | dst_ops) & mask);
273 }
274
275 /**
276  * rio_release_dev- Frees a RIO device struct
277  * @dev: LDM device associated with a RIO device struct
278  *
279  * Gets the RIO device struct associated a RIO device struct.
280  * The RIO device struct is freed.
281  */
282 static void rio_release_dev(struct device *dev)
283 {
284         struct rio_dev *rdev;
285
286         rdev = to_rio_dev(dev);
287         kfree(rdev);
288 }
289
290 /**
291  * rio_is_switch- Tests if a RIO device has switch capabilities
292  * @rdev: RIO device
293  *
294  * Gets the RIO device Processing Element Features register
295  * contents and tests for switch capabilities. Returns 1 if
296  * the device is a switch or 0 if it is not a switch.
297  * The RIO device struct is freed.
298  */
299 static int rio_is_switch(struct rio_dev *rdev)
300 {
301         if (rdev->pef & RIO_PEF_SWITCH)
302                 return 1;
303         return 0;
304 }
305
306 /**
307  * rio_setup_device- Allocates and sets up a RIO device
308  * @net: RIO network
309  * @port: Master port to send transactions
310  * @destid: Current destination ID
311  * @hopcount: Current hopcount
312  * @do_enum: Enumeration/Discovery mode flag
313  *
314  * Allocates a RIO device and configures fields based on configuration
315  * space contents. If device has a destination ID register, a destination
316  * ID is either assigned in enumeration mode or read from configuration
317  * space in discovery mode.  If the device has switch capabilities, then
318  * a switch is allocated and configured appropriately. Returns a pointer
319  * to a RIO device on success or NULL on failure.
320  *
321  */
322 static struct rio_dev *rio_setup_device(struct rio_net *net,
323                                         struct rio_mport *port, u16 destid,
324                                         u8 hopcount, int do_enum)
325 {
326         int ret = 0;
327         struct rio_dev *rdev;
328         struct rio_switch *rswitch = NULL;
329         int result, rdid;
330         size_t size;
331         u32 swpinfo = 0;
332
333         size = sizeof(*rdev);
334         if (rio_mport_read_config_32(port, destid, hopcount,
335                                      RIO_PEF_CAR, &result))
336                 return NULL;
337
338         if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
339                 rio_mport_read_config_32(port, destid, hopcount,
340                                          RIO_SWP_INFO_CAR, &swpinfo);
341                 if (result & RIO_PEF_SWITCH)
342                         size += struct_size(rswitch, nextdev, RIO_GET_TOTAL_PORTS(swpinfo));
343         }
344
345         rdev = kzalloc(size, GFP_KERNEL);
346         if (!rdev)
347                 return NULL;
348
349         rdev->net = net;
350         rdev->pef = result;
351         rdev->swpinfo = swpinfo;
352         rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
353                                  &result);
354         rdev->did = result >> 16;
355         rdev->vid = result & 0xffff;
356         rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
357                                  &rdev->device_rev);
358         rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
359                                  &result);
360         rdev->asm_did = result >> 16;
361         rdev->asm_vid = result & 0xffff;
362         rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
363                                  &result);
364         rdev->asm_rev = result >> 16;
365         if (rdev->pef & RIO_PEF_EXT_FEATURES) {
366                 rdev->efptr = result & 0xffff;
367                 rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
368                                                 hopcount, &rdev->phys_rmap);
369                 pr_debug("RIO: %s Register Map %d device\n",
370                          __func__, rdev->phys_rmap);
371
372                 rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
373                                                 hopcount, RIO_EFB_ERR_MGMNT);
374                 if (!rdev->em_efptr)
375                         rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
376                                                 hopcount, RIO_EFB_ERR_MGMNT_HS);
377         }
378
379         rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
380                                  &rdev->src_ops);
381         rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
382                                  &rdev->dst_ops);
383
384         if (do_enum) {
385                 /* Assign component tag to device */
386                 if (next_comptag >= 0x10000) {
387                         pr_err("RIO: Component Tag Counter Overflow\n");
388                         goto cleanup;
389                 }
390                 rio_mport_write_config_32(port, destid, hopcount,
391                                           RIO_COMPONENT_TAG_CSR, next_comptag);
392                 rdev->comp_tag = next_comptag++;
393                 rdev->do_enum = true;
394         }  else {
395                 rio_mport_read_config_32(port, destid, hopcount,
396                                          RIO_COMPONENT_TAG_CSR,
397                                          &rdev->comp_tag);
398         }
399
400         if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
401                 if (do_enum) {
402                         rio_set_device_id(port, destid, hopcount, next_destid);
403                         rdev->destid = next_destid;
404                         next_destid = rio_destid_alloc(net);
405                 } else
406                         rdev->destid = rio_get_device_id(port, destid, hopcount);
407
408                 rdev->hopcount = 0xff;
409         } else {
410                 /* Switch device has an associated destID which
411                  * will be adjusted later
412                  */
413                 rdev->destid = destid;
414                 rdev->hopcount = hopcount;
415         }
416
417         /* If a PE has both switch and other functions, show it as a switch */
418         if (rio_is_switch(rdev)) {
419                 rswitch = rdev->rswitch;
420                 rswitch->port_ok = 0;
421                 spin_lock_init(&rswitch->lock);
422                 rswitch->route_table =
423                         kzalloc(RIO_MAX_ROUTE_ENTRIES(port->sys_size),
424                                 GFP_KERNEL);
425                 if (!rswitch->route_table)
426                         goto cleanup;
427                 /* Initialize switch route table */
428                 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
429                                 rdid++)
430                         rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
431                 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
432                              rdev->comp_tag & RIO_CTAG_UDEVID);
433
434                 if (do_enum)
435                         rio_route_clr_table(rdev, RIO_GLOBAL_TABLE, 0);
436         } else {
437                 if (do_enum)
438                         /*Enable Input Output Port (transmitter receiver)*/
439                         rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
440
441                 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
442                              rdev->comp_tag & RIO_CTAG_UDEVID);
443         }
444
445         rdev->dev.parent = &net->dev;
446         rio_attach_device(rdev);
447         rdev->dev.release = rio_release_dev;
448         rdev->dma_mask = DMA_BIT_MASK(32);
449         rdev->dev.dma_mask = &rdev->dma_mask;
450         rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
451
452         if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
453                 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
454                                    0, 0xffff);
455
456         ret = rio_add_device(rdev);
457         if (ret)
458                 goto cleanup;
459
460         rio_dev_get(rdev);
461
462         return rdev;
463
464 cleanup:
465         if (rswitch)
466                 kfree(rswitch->route_table);
467
468         kfree(rdev);
469         return NULL;
470 }
471
472 /**
473  * rio_sport_is_active- Tests if a switch port has an active connection.
474  * @rdev: RapidIO device object
475  * @sp: Switch port number
476  *
477  * Reads the port error status CSR for a particular switch port to
478  * determine if the port has an active link.  Returns
479  * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
480  * inactive.
481  */
482 static int
483 rio_sport_is_active(struct rio_dev *rdev, int sp)
484 {
485         u32 result = 0;
486
487         rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, sp),
488                            &result);
489
490         return result & RIO_PORT_N_ERR_STS_PORT_OK;
491 }
492
493 /**
494  * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
495  * @port: Master port to send transaction
496  * @hopcount: Number of hops to the device
497  *
498  * Used during enumeration to read the Host Device ID Lock CSR on a
499  * RIO device. Returns the value of the lock register.
500  */
501 static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
502 {
503         u32 result;
504
505         rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
506                                  RIO_HOST_DID_LOCK_CSR, &result);
507
508         return (u16) (result & 0xffff);
509 }
510
511 /**
512  * rio_enum_peer- Recursively enumerate a RIO network through a master port
513  * @net: RIO network being enumerated
514  * @port: Master port to send transactions
515  * @hopcount: Number of hops into the network
516  * @prev: Previous RIO device connected to the enumerated one
517  * @prev_port: Port on previous RIO device
518  *
519  * Recursively enumerates a RIO network.  Transactions are sent via the
520  * master port passed in @port.
521  */
522 static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
523                          u8 hopcount, struct rio_dev *prev, int prev_port)
524 {
525         struct rio_dev *rdev;
526         u32 regval;
527         int tmp;
528
529         if (rio_mport_chk_dev_access(port,
530                         RIO_ANY_DESTID(port->sys_size), hopcount)) {
531                 pr_debug("RIO: device access check failed\n");
532                 return -1;
533         }
534
535         if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
536                 pr_debug("RIO: PE already discovered by this host\n");
537                 /*
538                  * Already discovered by this host. Add it as another
539                  * link to the existing device.
540                  */
541                 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
542                                 hopcount, RIO_COMPONENT_TAG_CSR, &regval);
543
544                 if (regval) {
545                         rdev = rio_get_comptag((regval & 0xffff), NULL);
546
547                         if (rdev && prev && rio_is_switch(prev)) {
548                                 pr_debug("RIO: redundant path to %s\n",
549                                          rio_name(rdev));
550                                 prev->rswitch->nextdev[prev_port] = rdev;
551                         }
552                 }
553
554                 return 0;
555         }
556
557         /* Attempt to acquire device lock */
558         rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
559                                   hopcount,
560                                   RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
561         while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
562                < port->host_deviceid) {
563                 /* Delay a bit */
564                 mdelay(1);
565                 /* Attempt to acquire device lock again */
566                 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
567                                           hopcount,
568                                           RIO_HOST_DID_LOCK_CSR,
569                                           port->host_deviceid);
570         }
571
572         if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
573                 pr_debug(
574                     "RIO: PE locked by a higher priority host...retreating\n");
575                 return -1;
576         }
577
578         /* Setup new RIO device */
579         rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
580                                         hopcount, 1);
581         if (rdev) {
582                 rdev->prev = prev;
583                 if (prev && rio_is_switch(prev))
584                         prev->rswitch->nextdev[prev_port] = rdev;
585         } else
586                 return -1;
587
588         if (rio_is_switch(rdev)) {
589                 int sw_destid;
590                 int cur_destid;
591                 int sw_inport;
592                 u16 destid;
593                 int port_num;
594
595                 sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
596                 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
597                                     port->host_deviceid, sw_inport, 0);
598                 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
599
600                 destid = rio_destid_first(net);
601                 while (destid != RIO_INVALID_DESTID && destid < next_destid) {
602                         if (destid != port->host_deviceid) {
603                                 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
604                                                     destid, sw_inport, 0);
605                                 rdev->rswitch->route_table[destid] = sw_inport;
606                         }
607                         destid = rio_destid_next(net, destid + 1);
608                 }
609                 pr_debug(
610                     "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
611                     rio_name(rdev), rdev->vid, rdev->did,
612                     RIO_GET_TOTAL_PORTS(rdev->swpinfo));
613                 sw_destid = next_destid;
614                 for (port_num = 0;
615                      port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
616                      port_num++) {
617                         if (sw_inport == port_num) {
618                                 rio_enable_rx_tx_port(port, 0,
619                                               RIO_ANY_DESTID(port->sys_size),
620                                               hopcount, port_num);
621                                 rdev->rswitch->port_ok |= (1 << port_num);
622                                 continue;
623                         }
624
625                         cur_destid = next_destid;
626
627                         if (rio_sport_is_active(rdev, port_num)) {
628                                 pr_debug(
629                                     "RIO: scanning device on port %d\n",
630                                     port_num);
631                                 rio_enable_rx_tx_port(port, 0,
632                                               RIO_ANY_DESTID(port->sys_size),
633                                               hopcount, port_num);
634                                 rdev->rswitch->port_ok |= (1 << port_num);
635                                 rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
636                                                 RIO_ANY_DESTID(port->sys_size),
637                                                 port_num, 0);
638
639                                 if (rio_enum_peer(net, port, hopcount + 1,
640                                                   rdev, port_num) < 0)
641                                         return -1;
642
643                                 /* Update routing tables */
644                                 destid = rio_destid_next(net, cur_destid + 1);
645                                 if (destid != RIO_INVALID_DESTID) {
646                                         for (destid = cur_destid;
647                                              destid < next_destid;) {
648                                                 if (destid != port->host_deviceid) {
649                                                         rio_route_add_entry(rdev,
650                                                                     RIO_GLOBAL_TABLE,
651                                                                     destid,
652                                                                     port_num,
653                                                                     0);
654                                                         rdev->rswitch->
655                                                                 route_table[destid] =
656                                                                 port_num;
657                                                 }
658                                                 destid = rio_destid_next(net,
659                                                                 destid + 1);
660                                         }
661                                 }
662                         } else {
663                                 /* If switch supports Error Management,
664                                  * set PORT_LOCKOUT bit for unused port
665                                  */
666                                 if (rdev->em_efptr)
667                                         rio_set_port_lockout(rdev, port_num, 1);
668
669                                 rdev->rswitch->port_ok &= ~(1 << port_num);
670                         }
671                 }
672
673                 /* Direct Port-write messages to the enumeratiing host */
674                 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
675                     (rdev->em_efptr)) {
676                         rio_write_config_32(rdev,
677                                         rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
678                                         (port->host_deviceid << 16) |
679                                         (port->sys_size << 15));
680                 }
681
682                 rio_init_em(rdev);
683
684                 /* Check for empty switch */
685                 if (next_destid == sw_destid)
686                         next_destid = rio_destid_alloc(net);
687
688                 rdev->destid = sw_destid;
689         } else
690                 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
691                     rio_name(rdev), rdev->vid, rdev->did);
692
693         return 0;
694 }
695
696 /**
697  * rio_enum_complete- Tests if enumeration of a network is complete
698  * @port: Master port to send transaction
699  *
700  * Tests the PGCCSR discovered bit for non-zero value (enumeration
701  * complete flag). Return %1 if enumeration is complete or %0 if
702  * enumeration is incomplete.
703  */
704 static int rio_enum_complete(struct rio_mport *port)
705 {
706         u32 regval;
707
708         rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
709                                  &regval);
710         return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
711 }
712
713 /**
714  * rio_disc_peer- Recursively discovers a RIO network through a master port
715  * @net: RIO network being discovered
716  * @port: Master port to send transactions
717  * @destid: Current destination ID in network
718  * @hopcount: Number of hops into the network
719  * @prev: previous rio_dev
720  * @prev_port: previous port number
721  *
722  * Recursively discovers a RIO network.  Transactions are sent via the
723  * master port passed in @port.
724  */
725 static int
726 rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
727               u8 hopcount, struct rio_dev *prev, int prev_port)
728 {
729         u8 port_num, route_port;
730         struct rio_dev *rdev;
731         u16 ndestid;
732
733         /* Setup new RIO device */
734         if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
735                 rdev->prev = prev;
736                 if (prev && rio_is_switch(prev))
737                         prev->rswitch->nextdev[prev_port] = rdev;
738         } else
739                 return -1;
740
741         if (rio_is_switch(rdev)) {
742                 /* Associated destid is how we accessed this switch */
743                 rdev->destid = destid;
744
745                 pr_debug(
746                     "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
747                     rio_name(rdev), rdev->vid, rdev->did,
748                     RIO_GET_TOTAL_PORTS(rdev->swpinfo));
749                 for (port_num = 0;
750                      port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
751                      port_num++) {
752                         if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
753                                 continue;
754
755                         if (rio_sport_is_active(rdev, port_num)) {
756                                 pr_debug(
757                                     "RIO: scanning device on port %d\n",
758                                     port_num);
759
760                                 rio_lock_device(port, destid, hopcount, 1000);
761
762                                 for (ndestid = 0;
763                                      ndestid < RIO_ANY_DESTID(port->sys_size);
764                                      ndestid++) {
765                                         rio_route_get_entry(rdev,
766                                                             RIO_GLOBAL_TABLE,
767                                                             ndestid,
768                                                             &route_port, 0);
769                                         if (route_port == port_num)
770                                                 break;
771                                 }
772
773                                 if (ndestid == RIO_ANY_DESTID(port->sys_size))
774                                         continue;
775                                 rio_unlock_device(port, destid, hopcount);
776                                 if (rio_disc_peer(net, port, ndestid,
777                                         hopcount + 1, rdev, port_num) < 0)
778                                         return -1;
779                         }
780                 }
781         } else
782                 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
783                     rio_name(rdev), rdev->vid, rdev->did);
784
785         return 0;
786 }
787
788 /**
789  * rio_mport_is_active- Tests if master port link is active
790  * @port: Master port to test
791  *
792  * Reads the port error status CSR for the master port to
793  * determine if the port has an active link.  Returns
794  * %RIO_PORT_N_ERR_STS_PORT_OK if the  master port is active
795  * or %0 if it is inactive.
796  */
797 static int rio_mport_is_active(struct rio_mport *port)
798 {
799         u32 result = 0;
800
801         rio_local_read_config_32(port,
802                 port->phys_efptr +
803                         RIO_PORT_N_ERR_STS_CSR(port->index, port->phys_rmap),
804                 &result);
805         return result & RIO_PORT_N_ERR_STS_PORT_OK;
806 }
807
808 static void rio_scan_release_net(struct rio_net *net)
809 {
810         pr_debug("RIO-SCAN: %s: net_%d\n", __func__, net->id);
811         kfree(net->enum_data);
812 }
813
814 static void rio_scan_release_dev(struct device *dev)
815 {
816         struct rio_net *net;
817
818         net = to_rio_net(dev);
819         pr_debug("RIO-SCAN: %s: net_%d\n", __func__, net->id);
820         kfree(net);
821 }
822
823 /*
824  * rio_scan_alloc_net - Allocate and configure a new RIO network
825  * @mport: Master port associated with the RIO network
826  * @do_enum: Enumeration/Discovery mode flag
827  * @start: logical minimal start id for new net
828  *
829  * Allocates a new RIO network structure and initializes enumerator-specific
830  * part of it (if required).
831  * Returns a RIO network pointer on success or %NULL on failure.
832  */
833 static struct rio_net *rio_scan_alloc_net(struct rio_mport *mport,
834                                           int do_enum, u16 start)
835 {
836         struct rio_net *net;
837
838         net = rio_alloc_net(mport);
839
840         if (net && do_enum) {
841                 struct rio_id_table *idtab;
842                 size_t size;
843
844                 size = sizeof(struct rio_id_table) +
845                                 BITS_TO_LONGS(
846                                         RIO_MAX_ROUTE_ENTRIES(mport->sys_size)
847                                         ) * sizeof(long);
848
849                 idtab = kzalloc(size, GFP_KERNEL);
850
851                 if (idtab == NULL) {
852                         pr_err("RIO: failed to allocate destID table\n");
853                         rio_free_net(net);
854                         net = NULL;
855                 } else {
856                         net->enum_data = idtab;
857                         net->release = rio_scan_release_net;
858                         idtab->start = start;
859                         idtab->max = RIO_MAX_ROUTE_ENTRIES(mport->sys_size);
860                         spin_lock_init(&idtab->lock);
861                 }
862         }
863
864         if (net) {
865                 net->id = mport->id;
866                 net->hport = mport;
867                 dev_set_name(&net->dev, "rnet_%d", net->id);
868                 net->dev.parent = &mport->dev;
869                 net->dev.release = rio_scan_release_dev;
870                 rio_add_net(net);
871         }
872
873         return net;
874 }
875
876 /**
877  * rio_update_route_tables- Updates route tables in switches
878  * @net: RIO network to run update on
879  *
880  * For each enumerated device, ensure that each switch in a system
881  * has correct routing entries. Add routes for devices that where
882  * unknown during the first enumeration pass through the switch.
883  */
884 static void rio_update_route_tables(struct rio_net *net)
885 {
886         struct rio_dev *rdev, *swrdev;
887         struct rio_switch *rswitch;
888         u8 sport;
889         u16 destid;
890
891         list_for_each_entry(rdev, &net->devices, net_list) {
892
893                 destid = rdev->destid;
894
895                 list_for_each_entry(rswitch, &net->switches, node) {
896
897                         if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
898                                 continue;
899
900                         if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
901                                 swrdev = sw_to_rio_dev(rswitch);
902
903                                 /* Skip if destid ends in empty switch*/
904                                 if (swrdev->destid == destid)
905                                         continue;
906
907                                 sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
908
909                                 rio_route_add_entry(swrdev, RIO_GLOBAL_TABLE,
910                                                     destid, sport, 0);
911                                 rswitch->route_table[destid] = sport;
912                         }
913                 }
914         }
915 }
916
917 /**
918  * rio_init_em - Initializes RIO Error Management (for switches)
919  * @rdev: RIO device
920  *
921  * For each enumerated switch, call device-specific error management
922  * initialization routine (if supplied by the switch driver).
923  */
924 static void rio_init_em(struct rio_dev *rdev)
925 {
926         if (rio_is_switch(rdev) && (rdev->em_efptr) &&
927             rdev->rswitch->ops && rdev->rswitch->ops->em_init) {
928                 rdev->rswitch->ops->em_init(rdev);
929         }
930 }
931
932 /**
933  * rio_enum_mport- Start enumeration through a master port
934  * @mport: Master port to send transactions
935  * @flags: Enumeration control flags
936  *
937  * Starts the enumeration process. If somebody has enumerated our
938  * master port device, then give up. If not and we have an active
939  * link, then start recursive peer enumeration. Returns %0 if
940  * enumeration succeeds or %-EBUSY if enumeration fails.
941  */
942 static int rio_enum_mport(struct rio_mport *mport, u32 flags)
943 {
944         struct rio_net *net = NULL;
945         int rc = 0;
946
947         printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
948                mport->name);
949
950         /*
951          * To avoid multiple start requests (repeat enumeration is not supported
952          * by this method) check if enumeration/discovery was performed for this
953          * mport: if mport was added into the list of mports for a net exit
954          * with error.
955          */
956         if (mport->nnode.next || mport->nnode.prev)
957                 return -EBUSY;
958
959         /* If somebody else enumerated our master port device, bail. */
960         if (rio_enum_host(mport) < 0) {
961                 printk(KERN_INFO
962                        "RIO: master port %d device has been enumerated by a remote host\n",
963                        mport->id);
964                 rc = -EBUSY;
965                 goto out;
966         }
967
968         /* If master port has an active link, allocate net and enum peers */
969         if (rio_mport_is_active(mport)) {
970                 net = rio_scan_alloc_net(mport, 1, 0);
971                 if (!net) {
972                         printk(KERN_ERR "RIO: failed to allocate new net\n");
973                         rc = -ENOMEM;
974                         goto out;
975                 }
976
977                 /* reserve mport destID in new net */
978                 rio_destid_reserve(net, mport->host_deviceid);
979
980                 /* Enable Input Output Port (transmitter receiver) */
981                 rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
982
983                 /* Set component tag for host */
984                 rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
985                                           next_comptag++);
986
987                 next_destid = rio_destid_alloc(net);
988
989                 if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
990                         /* A higher priority host won enumeration, bail. */
991                         printk(KERN_INFO
992                                "RIO: master port %d device has lost enumeration to a remote host\n",
993                                mport->id);
994                         rio_clear_locks(net);
995                         rc = -EBUSY;
996                         goto out;
997                 }
998                 /* free the last allocated destID (unused) */
999                 rio_destid_free(net, next_destid);
1000                 rio_update_route_tables(net);
1001                 rio_clear_locks(net);
1002                 rio_pw_enable(mport, 1);
1003         } else {
1004                 printk(KERN_INFO "RIO: master port %d link inactive\n",
1005                        mport->id);
1006                 rc = -EINVAL;
1007         }
1008
1009       out:
1010         return rc;
1011 }
1012
1013 /**
1014  * rio_build_route_tables- Generate route tables from switch route entries
1015  * @net: RIO network to run route tables scan on
1016  *
1017  * For each switch device, generate a route table by copying existing
1018  * route entries from the switch.
1019  */
1020 static void rio_build_route_tables(struct rio_net *net)
1021 {
1022         struct rio_switch *rswitch;
1023         struct rio_dev *rdev;
1024         int i;
1025         u8 sport;
1026
1027         list_for_each_entry(rswitch, &net->switches, node) {
1028                 rdev = sw_to_rio_dev(rswitch);
1029
1030                 rio_lock_device(net->hport, rdev->destid,
1031                                 rdev->hopcount, 1000);
1032                 for (i = 0;
1033                      i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
1034                      i++) {
1035                         if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
1036                                                 i, &sport, 0) < 0)
1037                                 continue;
1038                         rswitch->route_table[i] = sport;
1039                 }
1040
1041                 rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
1042         }
1043 }
1044
1045 /**
1046  * rio_disc_mport- Start discovery through a master port
1047  * @mport: Master port to send transactions
1048  * @flags: discovery control flags
1049  *
1050  * Starts the discovery process. If we have an active link,
1051  * then wait for the signal that enumeration is complete (if wait
1052  * is allowed).
1053  * When enumeration completion is signaled, start recursive
1054  * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1055  * on failure.
1056  */
1057 static int rio_disc_mport(struct rio_mport *mport, u32 flags)
1058 {
1059         struct rio_net *net = NULL;
1060         unsigned long to_end;
1061
1062         printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1063                mport->name);
1064
1065         /* If master port has an active link, allocate net and discover peers */
1066         if (rio_mport_is_active(mport)) {
1067                 if (rio_enum_complete(mport))
1068                         goto enum_done;
1069                 else if (flags & RIO_SCAN_ENUM_NO_WAIT)
1070                         return -EAGAIN;
1071
1072                 pr_debug("RIO: wait for enumeration to complete...\n");
1073
1074                 to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1075                 while (time_before(jiffies, to_end)) {
1076                         if (rio_enum_complete(mport))
1077                                 goto enum_done;
1078                         msleep(10);
1079                 }
1080
1081                 pr_debug("RIO: discovery timeout on mport %d %s\n",
1082                          mport->id, mport->name);
1083                 goto bail;
1084 enum_done:
1085                 pr_debug("RIO: ... enumeration done\n");
1086
1087                 net = rio_scan_alloc_net(mport, 0, 0);
1088                 if (!net) {
1089                         printk(KERN_ERR "RIO: Failed to allocate new net\n");
1090                         goto bail;
1091                 }
1092
1093                 /* Read DestID assigned by enumerator */
1094                 rio_local_read_config_32(mport, RIO_DID_CSR,
1095                                          &mport->host_deviceid);
1096                 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1097                                                    mport->host_deviceid);
1098
1099                 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1100                                         0, NULL, 0) < 0) {
1101                         printk(KERN_INFO
1102                                "RIO: master port %d device has failed discovery\n",
1103                                mport->id);
1104                         goto bail;
1105                 }
1106
1107                 rio_build_route_tables(net);
1108         }
1109
1110         return 0;
1111 bail:
1112         return -EBUSY;
1113 }
1114
1115 static struct rio_scan rio_scan_ops = {
1116         .owner = THIS_MODULE,
1117         .enumerate = rio_enum_mport,
1118         .discover = rio_disc_mport,
1119 };
1120
1121 static bool scan;
1122 module_param(scan, bool, 0);
1123 MODULE_PARM_DESC(scan, "Start RapidIO network enumeration/discovery "
1124                         "(default = 0)");
1125
1126 /**
1127  * rio_basic_attach:
1128  *
1129  * When this enumeration/discovery method is loaded as a module this function
1130  * registers its specific enumeration and discover routines for all available
1131  * RapidIO mport devices. The "scan" command line parameter controls ability of
1132  * the module to start RapidIO enumeration/discovery automatically.
1133  *
1134  * Returns 0 for success or -EIO if unable to register itself.
1135  *
1136  * This enumeration/discovery method cannot be unloaded and therefore does not
1137  * provide a matching cleanup_module routine.
1138  */
1139
1140 static int __init rio_basic_attach(void)
1141 {
1142         if (rio_register_scan(RIO_MPORT_ANY, &rio_scan_ops))
1143                 return -EIO;
1144         if (scan)
1145                 rio_init_mports();
1146         return 0;
1147 }
1148
1149 late_initcall(rio_basic_attach);
1150
1151 MODULE_DESCRIPTION("Basic RapidIO enumeration/discovery");
1152 MODULE_LICENSE("GPL");