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