rapidio: add Port-Write handling for EM
[linux/fpc-iii.git] / drivers / rapidio / rio-scan.c
blob74633cc6b2eb19e82d1a4d5e9f35bcf5f2d54b73
1 /*
2 * RapidIO enumeration and discovery support
4 * Copyright 2005 MontaVista Software, Inc.
5 * Matt Porter <mporter@kernel.crashing.org>
7 * Copyright 2009 Integrated Device Technology, Inc.
8 * Alex Bounine <alexandre.bounine@idt.com>
9 * - Added Port-Write/Error Management initialization and handling
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/types.h>
18 #include <linux/kernel.h>
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/jiffies.h>
31 #include <linux/slab.h>
33 #include "rio.h"
35 LIST_HEAD(rio_devices);
36 static LIST_HEAD(rio_switches);
38 static void rio_enum_timeout(unsigned long);
40 static void rio_init_em(struct rio_dev *rdev);
42 DEFINE_SPINLOCK(rio_global_list_lock);
44 static int next_destid = 0;
45 static int next_switchid = 0;
46 static int next_net = 0;
47 static int next_comptag;
49 static struct timer_list rio_enum_timer =
50 TIMER_INITIALIZER(rio_enum_timeout, 0, 0);
52 static int rio_mport_phys_table[] = {
53 RIO_EFB_PAR_EP_ID,
54 RIO_EFB_PAR_EP_REC_ID,
55 RIO_EFB_SER_EP_ID,
56 RIO_EFB_SER_EP_REC_ID,
57 -1,
60 /**
61 * rio_get_device_id - Get the base/extended device id for a device
62 * @port: RIO master port
63 * @destid: Destination ID of device
64 * @hopcount: Hopcount to device
66 * Reads the base/extended device id from a device. Returns the
67 * 8/16-bit device ID.
69 static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
71 u32 result;
73 rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
75 return RIO_GET_DID(port->sys_size, result);
78 /**
79 * rio_set_device_id - Set the base/extended device id for a device
80 * @port: RIO master port
81 * @destid: Destination ID of device
82 * @hopcount: Hopcount to device
83 * @did: Device ID value to be written
85 * Writes the base/extended device id from a device.
87 static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
89 rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
90 RIO_SET_DID(port->sys_size, did));
93 /**
94 * rio_local_set_device_id - Set the base/extended device id for a port
95 * @port: RIO master port
96 * @did: Device ID value to be written
98 * Writes the base/extended device id from a device.
100 static void rio_local_set_device_id(struct rio_mport *port, u16 did)
102 rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
103 did));
107 * rio_clear_locks- Release all host locks and signal enumeration complete
108 * @port: Master port to issue transaction
110 * Marks the component tag CSR on each device with the enumeration
111 * complete flag. When complete, it then release the host locks on
112 * each device. Returns 0 on success or %-EINVAL on failure.
114 static int rio_clear_locks(struct rio_mport *port)
116 struct rio_dev *rdev;
117 u32 result;
118 int ret = 0;
120 /* Assign component tag to all devices */
121 next_comptag = 1;
122 rio_local_write_config_32(port, RIO_COMPONENT_TAG_CSR, next_comptag++);
124 list_for_each_entry(rdev, &rio_devices, global_list) {
125 /* Mark device as discovered */
126 rio_read_config_32(rdev,
127 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
128 &result);
129 rio_write_config_32(rdev,
130 rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
131 result | RIO_PORT_GEN_DISCOVERED);
133 rio_write_config_32(rdev, RIO_COMPONENT_TAG_CSR, next_comptag);
134 rdev->comp_tag = next_comptag++;
135 if (next_comptag >= 0x10000) {
136 pr_err("RIO: Component Tag Counter Overflow\n");
137 break;
141 /* Release host device id locks */
142 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
143 port->host_deviceid);
144 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
145 if ((result & 0xffff) != 0xffff) {
146 printk(KERN_INFO
147 "RIO: badness when releasing host lock on master port, result %8.8x\n",
148 result);
149 ret = -EINVAL;
151 list_for_each_entry(rdev, &rio_devices, global_list) {
152 rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
153 port->host_deviceid);
154 rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
155 if ((result & 0xffff) != 0xffff) {
156 printk(KERN_INFO
157 "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
158 rdev->vid, rdev->did);
159 ret = -EINVAL;
163 return ret;
167 * rio_enum_host- Set host lock and initialize host destination ID
168 * @port: Master port to issue transaction
170 * Sets the local host master port lock and destination ID register
171 * with the host device ID value. The host device ID value is provided
172 * by the platform. Returns %0 on success or %-1 on failure.
174 static int rio_enum_host(struct rio_mport *port)
176 u32 result;
178 /* Set master port host device id lock */
179 rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
180 port->host_deviceid);
182 rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
183 if ((result & 0xffff) != port->host_deviceid)
184 return -1;
186 /* Set master port destid and init destid ctr */
187 rio_local_set_device_id(port, port->host_deviceid);
189 if (next_destid == port->host_deviceid)
190 next_destid++;
192 return 0;
196 * rio_device_has_destid- Test if a device contains a destination ID register
197 * @port: Master port to issue transaction
198 * @src_ops: RIO device source operations
199 * @dst_ops: RIO device destination operations
201 * Checks the provided @src_ops and @dst_ops for the necessary transaction
202 * capabilities that indicate whether or not a device will implement a
203 * destination ID register. Returns 1 if true or 0 if false.
205 static int rio_device_has_destid(struct rio_mport *port, int src_ops,
206 int dst_ops)
208 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;
210 return !!((src_ops | dst_ops) & mask);
214 * rio_release_dev- Frees a RIO device struct
215 * @dev: LDM device associated with a RIO device struct
217 * Gets the RIO device struct associated a RIO device struct.
218 * The RIO device struct is freed.
220 static void rio_release_dev(struct device *dev)
222 struct rio_dev *rdev;
224 rdev = to_rio_dev(dev);
225 kfree(rdev);
229 * rio_is_switch- Tests if a RIO device has switch capabilities
230 * @rdev: RIO device
232 * Gets the RIO device Processing Element Features register
233 * contents and tests for switch capabilities. Returns 1 if
234 * the device is a switch or 0 if it is not a switch.
235 * The RIO device struct is freed.
237 static int rio_is_switch(struct rio_dev *rdev)
239 if (rdev->pef & RIO_PEF_SWITCH)
240 return 1;
241 return 0;
245 * rio_route_set_ops- Sets routing operations for a particular vendor switch
246 * @rdev: RIO device
248 * Searches the RIO route ops table for known switch types. If the vid
249 * and did match a switch table entry, then set the add_entry() and
250 * get_entry() ops to the table entry values.
252 static void rio_route_set_ops(struct rio_dev *rdev)
254 struct rio_route_ops *cur = __start_rio_route_ops;
255 struct rio_route_ops *end = __end_rio_route_ops;
257 while (cur < end) {
258 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
259 pr_debug("RIO: adding routing ops for %s\n", rio_name(rdev));
260 rdev->rswitch->add_entry = cur->add_hook;
261 rdev->rswitch->get_entry = cur->get_hook;
262 rdev->rswitch->clr_table = cur->clr_hook;
263 break;
265 cur++;
268 if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
269 pr_debug("RIO: adding STD routing ops for %s\n",
270 rio_name(rdev));
271 rdev->rswitch->add_entry = rio_std_route_add_entry;
272 rdev->rswitch->get_entry = rio_std_route_get_entry;
273 rdev->rswitch->clr_table = rio_std_route_clr_table;
276 if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
277 printk(KERN_ERR "RIO: missing routing ops for %s\n",
278 rio_name(rdev));
282 * rio_em_set_ops- Sets Error Managment operations for a particular vendor switch
283 * @rdev: RIO device
285 * Searches the RIO EM ops table for known switch types. If the vid
286 * and did match a switch table entry, then set the em_init() and
287 * em_handle() ops to the table entry values.
289 static void rio_em_set_ops(struct rio_dev *rdev)
291 struct rio_em_ops *cur = __start_rio_em_ops;
292 struct rio_em_ops *end = __end_rio_em_ops;
294 while (cur < end) {
295 if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
296 pr_debug("RIO: adding EM ops for %s\n", rio_name(rdev));
297 rdev->rswitch->em_init = cur->init_hook;
298 rdev->rswitch->em_handle = cur->handler_hook;
299 break;
301 cur++;
306 * rio_add_device- Adds a RIO device to the device model
307 * @rdev: RIO device
309 * Adds the RIO device to the global device list and adds the RIO
310 * device to the RIO device list. Creates the generic sysfs nodes
311 * for an RIO device.
313 static int __devinit rio_add_device(struct rio_dev *rdev)
315 int err;
317 err = device_add(&rdev->dev);
318 if (err)
319 return err;
321 spin_lock(&rio_global_list_lock);
322 list_add_tail(&rdev->global_list, &rio_devices);
323 spin_unlock(&rio_global_list_lock);
325 rio_create_sysfs_dev_files(rdev);
327 return 0;
331 * rio_setup_device- Allocates and sets up a RIO device
332 * @net: RIO network
333 * @port: Master port to send transactions
334 * @destid: Current destination ID
335 * @hopcount: Current hopcount
336 * @do_enum: Enumeration/Discovery mode flag
338 * Allocates a RIO device and configures fields based on configuration
339 * space contents. If device has a destination ID register, a destination
340 * ID is either assigned in enumeration mode or read from configuration
341 * space in discovery mode. If the device has switch capabilities, then
342 * a switch is allocated and configured appropriately. Returns a pointer
343 * to a RIO device on success or NULL on failure.
346 static struct rio_dev __devinit *rio_setup_device(struct rio_net *net,
347 struct rio_mport *port, u16 destid,
348 u8 hopcount, int do_enum)
350 int ret = 0;
351 struct rio_dev *rdev;
352 struct rio_switch *rswitch = NULL;
353 int result, rdid;
355 rdev = kzalloc(sizeof(struct rio_dev), GFP_KERNEL);
356 if (!rdev)
357 return NULL;
359 rdev->net = net;
360 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
361 &result);
362 rdev->did = result >> 16;
363 rdev->vid = result & 0xffff;
364 rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
365 &rdev->device_rev);
366 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
367 &result);
368 rdev->asm_did = result >> 16;
369 rdev->asm_vid = result & 0xffff;
370 rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
371 &result);
372 rdev->asm_rev = result >> 16;
373 rio_mport_read_config_32(port, destid, hopcount, RIO_PEF_CAR,
374 &rdev->pef);
375 if (rdev->pef & RIO_PEF_EXT_FEATURES) {
376 rdev->efptr = result & 0xffff;
377 rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
378 hopcount);
380 rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
381 hopcount, RIO_EFB_ERR_MGMNT);
384 rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
385 &rdev->src_ops);
386 rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
387 &rdev->dst_ops);
389 if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
390 if (do_enum) {
391 rio_set_device_id(port, destid, hopcount, next_destid);
392 rdev->destid = next_destid++;
393 if (next_destid == port->host_deviceid)
394 next_destid++;
395 } else
396 rdev->destid = rio_get_device_id(port, destid, hopcount);
397 } else
398 /* Switch device has an associated destID */
399 rdev->destid = RIO_INVALID_DESTID;
401 /* If a PE has both switch and other functions, show it as a switch */
402 if (rio_is_switch(rdev)) {
403 rio_mport_read_config_32(port, destid, hopcount,
404 RIO_SWP_INFO_CAR, &rdev->swpinfo);
405 rswitch = kzalloc(sizeof(struct rio_switch), GFP_KERNEL);
406 if (!rswitch)
407 goto cleanup;
408 rswitch->switchid = next_switchid;
409 rswitch->hopcount = hopcount;
410 rswitch->destid = destid;
411 rswitch->port_ok = 0;
412 rswitch->route_table = kzalloc(sizeof(u8)*
413 RIO_MAX_ROUTE_ENTRIES(port->sys_size),
414 GFP_KERNEL);
415 if (!rswitch->route_table)
416 goto cleanup;
417 /* Initialize switch route table */
418 for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
419 rdid++)
420 rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
421 rdev->rswitch = rswitch;
422 dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
423 rdev->rswitch->switchid);
424 rio_route_set_ops(rdev);
425 rio_em_set_ops(rdev);
427 if (do_enum && rdev->rswitch->clr_table)
428 rdev->rswitch->clr_table(port, destid, hopcount,
429 RIO_GLOBAL_TABLE);
431 list_add_tail(&rswitch->node, &rio_switches);
433 } else
434 dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
435 rdev->destid);
437 rdev->dev.bus = &rio_bus_type;
439 device_initialize(&rdev->dev);
440 rdev->dev.release = rio_release_dev;
441 rio_dev_get(rdev);
443 rdev->dma_mask = DMA_BIT_MASK(32);
444 rdev->dev.dma_mask = &rdev->dma_mask;
445 rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
447 if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
448 (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
449 rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
450 0, 0xffff);
452 ret = rio_add_device(rdev);
453 if (ret)
454 goto cleanup;
456 return rdev;
458 cleanup:
459 if (rswitch) {
460 kfree(rswitch->route_table);
461 kfree(rswitch);
463 kfree(rdev);
464 return NULL;
468 * rio_sport_is_active- Tests if a switch port has an active connection.
469 * @port: Master port to send transaction
470 * @destid: Associated destination ID for switch
471 * @hopcount: Hopcount to reach switch
472 * @sport: Switch port number
474 * Reads the port error status CSR for a particular switch port to
475 * determine if the port has an active link. Returns
476 * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
477 * inactive.
479 static int
480 rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
482 u32 result = 0;
483 u32 ext_ftr_ptr;
485 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
487 while (ext_ftr_ptr) {
488 rio_mport_read_config_32(port, destid, hopcount,
489 ext_ftr_ptr, &result);
490 result = RIO_GET_BLOCK_ID(result);
491 if ((result == RIO_EFB_SER_EP_FREE_ID) ||
492 (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
493 (result == RIO_EFB_SER_EP_FREC_ID))
494 break;
496 ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
497 ext_ftr_ptr);
500 if (ext_ftr_ptr)
501 rio_mport_read_config_32(port, destid, hopcount,
502 ext_ftr_ptr +
503 RIO_PORT_N_ERR_STS_CSR(sport),
504 &result);
506 return result & RIO_PORT_N_ERR_STS_PORT_OK;
510 * rio_lock_device - Acquires host device lock for specified device
511 * @port: Master port to send transaction
512 * @destid: Destination ID for device/switch
513 * @hopcount: Hopcount to reach switch
514 * @wait_ms: Max wait time in msec (0 = no timeout)
516 * Attepts to acquire host device lock for specified device
517 * Returns 0 if device lock acquired or EINVAL if timeout expires.
519 static int
520 rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
522 u32 result;
523 int tcnt = 0;
525 /* Attempt to acquire device lock */
526 rio_mport_write_config_32(port, destid, hopcount,
527 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
528 rio_mport_read_config_32(port, destid, hopcount,
529 RIO_HOST_DID_LOCK_CSR, &result);
531 while (result != port->host_deviceid) {
532 if (wait_ms != 0 && tcnt == wait_ms) {
533 pr_debug("RIO: timeout when locking device %x:%x\n",
534 destid, hopcount);
535 return -EINVAL;
538 /* Delay a bit */
539 mdelay(1);
540 tcnt++;
541 /* Try to acquire device lock again */
542 rio_mport_write_config_32(port, destid,
543 hopcount,
544 RIO_HOST_DID_LOCK_CSR,
545 port->host_deviceid);
546 rio_mport_read_config_32(port, destid,
547 hopcount,
548 RIO_HOST_DID_LOCK_CSR, &result);
551 return 0;
555 * rio_unlock_device - Releases host device lock for specified device
556 * @port: Master port to send transaction
557 * @destid: Destination ID for device/switch
558 * @hopcount: Hopcount to reach switch
560 * Returns 0 if device lock released or EINVAL if fails.
562 static int
563 rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
565 u32 result;
567 /* Release device lock */
568 rio_mport_write_config_32(port, destid,
569 hopcount,
570 RIO_HOST_DID_LOCK_CSR,
571 port->host_deviceid);
572 rio_mport_read_config_32(port, destid, hopcount,
573 RIO_HOST_DID_LOCK_CSR, &result);
574 if ((result & 0xffff) != 0xffff) {
575 pr_debug("RIO: badness when releasing device lock %x:%x\n",
576 destid, hopcount);
577 return -EINVAL;
580 return 0;
584 * rio_route_add_entry- Add a route entry to a switch routing table
585 * @mport: Master port to send transaction
586 * @rswitch: Switch device
587 * @table: Routing table ID
588 * @route_destid: Destination ID to be routed
589 * @route_port: Port number to be routed
590 * @lock: lock switch device flag
592 * Calls the switch specific add_entry() method to add a route entry
593 * on a switch. The route table can be specified using the @table
594 * argument if a switch has per port routing tables or the normal
595 * use is to specific all tables (or the global table) by passing
596 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
597 * on failure.
599 static int
600 rio_route_add_entry(struct rio_mport *mport, struct rio_switch *rswitch,
601 u16 table, u16 route_destid, u8 route_port, int lock)
603 int rc;
605 if (lock) {
606 rc = rio_lock_device(mport, rswitch->destid,
607 rswitch->hopcount, 1000);
608 if (rc)
609 return rc;
612 rc = rswitch->add_entry(mport, rswitch->destid,
613 rswitch->hopcount, table,
614 route_destid, route_port);
615 if (lock)
616 rio_unlock_device(mport, rswitch->destid, rswitch->hopcount);
618 return rc;
622 * rio_route_get_entry- Read a route entry in a switch routing table
623 * @mport: Master port to send transaction
624 * @rswitch: Switch device
625 * @table: Routing table ID
626 * @route_destid: Destination ID to be routed
627 * @route_port: Pointer to read port number into
628 * @lock: lock switch device flag
630 * Calls the switch specific get_entry() method to read a route entry
631 * in a switch. The route table can be specified using the @table
632 * argument if a switch has per port routing tables or the normal
633 * use is to specific all tables (or the global table) by passing
634 * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
635 * on failure.
637 static int
638 rio_route_get_entry(struct rio_mport *mport, struct rio_switch *rswitch, u16 table,
639 u16 route_destid, u8 *route_port, int lock)
641 int rc;
643 if (lock) {
644 rc = rio_lock_device(mport, rswitch->destid,
645 rswitch->hopcount, 1000);
646 if (rc)
647 return rc;
650 rc = rswitch->get_entry(mport, rswitch->destid,
651 rswitch->hopcount, table,
652 route_destid, route_port);
653 if (lock)
654 rio_unlock_device(mport, rswitch->destid, rswitch->hopcount);
656 return rc;
660 * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
661 * @port: Master port to send transaction
662 * @hopcount: Number of hops to the device
664 * Used during enumeration to read the Host Device ID Lock CSR on a
665 * RIO device. Returns the value of the lock register.
667 static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
669 u32 result;
671 rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
672 RIO_HOST_DID_LOCK_CSR, &result);
674 return (u16) (result & 0xffff);
678 * rio_get_swpinfo_inport- Gets the ingress port number
679 * @mport: Master port to send transaction
680 * @destid: Destination ID associated with the switch
681 * @hopcount: Number of hops to the device
683 * Returns port number being used to access the switch device.
685 static u8
686 rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
688 u32 result;
690 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
691 &result);
693 return (u8) (result & 0xff);
697 * rio_get_swpinfo_tports- Gets total number of ports on the switch
698 * @mport: Master port to send transaction
699 * @destid: Destination ID associated with the switch
700 * @hopcount: Number of hops to the device
702 * Returns total numbers of ports implemented by the switch device.
704 static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
705 u8 hopcount)
707 u32 result;
709 rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
710 &result);
712 return RIO_GET_TOTAL_PORTS(result);
716 * rio_net_add_mport- Add a master port to a RIO network
717 * @net: RIO network
718 * @port: Master port to add
720 * Adds a master port to the network list of associated master
721 * ports..
723 static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
725 spin_lock(&rio_global_list_lock);
726 list_add_tail(&port->nnode, &net->mports);
727 spin_unlock(&rio_global_list_lock);
731 * rio_enum_peer- Recursively enumerate a RIO network through a master port
732 * @net: RIO network being enumerated
733 * @port: Master port to send transactions
734 * @hopcount: Number of hops into the network
736 * Recursively enumerates a RIO network. Transactions are sent via the
737 * master port passed in @port.
739 static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
740 u8 hopcount)
742 int port_num;
743 int num_ports;
744 int cur_destid;
745 int sw_destid;
746 int sw_inport;
747 struct rio_dev *rdev;
748 u16 destid;
749 int tmp;
751 if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
752 pr_debug("RIO: PE already discovered by this host\n");
754 * Already discovered by this host. Add it as another
755 * master port for the current network.
757 rio_net_add_mport(net, port);
758 return 0;
761 /* Attempt to acquire device lock */
762 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
763 hopcount,
764 RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
765 while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
766 < port->host_deviceid) {
767 /* Delay a bit */
768 mdelay(1);
769 /* Attempt to acquire device lock again */
770 rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
771 hopcount,
772 RIO_HOST_DID_LOCK_CSR,
773 port->host_deviceid);
776 if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
777 pr_debug(
778 "RIO: PE locked by a higher priority host...retreating\n");
779 return -1;
782 /* Setup new RIO device */
783 rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
784 hopcount, 1);
785 if (rdev) {
786 /* Add device to the global and bus/net specific list. */
787 list_add_tail(&rdev->net_list, &net->devices);
788 } else
789 return -1;
791 if (rio_is_switch(rdev)) {
792 next_switchid++;
793 sw_inport = rio_get_swpinfo_inport(port,
794 RIO_ANY_DESTID(port->sys_size), hopcount);
795 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
796 port->host_deviceid, sw_inport, 0);
797 rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
799 for (destid = 0; destid < next_destid; destid++) {
800 if (destid == port->host_deviceid)
801 continue;
802 rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
803 destid, sw_inport, 0);
804 rdev->rswitch->route_table[destid] = sw_inport;
807 num_ports =
808 rio_get_swpinfo_tports(port, RIO_ANY_DESTID(port->sys_size),
809 hopcount);
810 pr_debug(
811 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
812 rio_name(rdev), rdev->vid, rdev->did, num_ports);
813 sw_destid = next_destid;
814 for (port_num = 0; port_num < num_ports; port_num++) {
815 if (sw_inport == port_num) {
816 rdev->rswitch->port_ok |= (1 << port_num);
817 continue;
820 cur_destid = next_destid;
822 if (rio_sport_is_active
823 (port, RIO_ANY_DESTID(port->sys_size), hopcount,
824 port_num)) {
825 pr_debug(
826 "RIO: scanning device on port %d\n",
827 port_num);
828 rdev->rswitch->port_ok |= (1 << port_num);
829 rio_route_add_entry(port, rdev->rswitch,
830 RIO_GLOBAL_TABLE,
831 RIO_ANY_DESTID(port->sys_size),
832 port_num, 0);
834 if (rio_enum_peer(net, port, hopcount + 1) < 0)
835 return -1;
837 /* Update routing tables */
838 if (next_destid > cur_destid) {
839 for (destid = cur_destid;
840 destid < next_destid; destid++) {
841 if (destid == port->host_deviceid)
842 continue;
843 rio_route_add_entry(port, rdev->rswitch,
844 RIO_GLOBAL_TABLE,
845 destid,
846 port_num,
848 rdev->rswitch->
849 route_table[destid] =
850 port_num;
853 } else {
854 /* If switch supports Error Management,
855 * set PORT_LOCKOUT bit for unused port
857 if (rdev->em_efptr)
858 rio_set_port_lockout(rdev, port_num, 1);
860 rdev->rswitch->port_ok &= ~(1 << port_num);
864 /* Direct Port-write messages to the enumeratiing host */
865 if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
866 (rdev->em_efptr)) {
867 rio_write_config_32(rdev,
868 rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
869 (port->host_deviceid << 16) |
870 (port->sys_size << 15));
873 rio_init_em(rdev);
875 /* Check for empty switch */
876 if (next_destid == sw_destid) {
877 next_destid++;
878 if (next_destid == port->host_deviceid)
879 next_destid++;
882 rdev->rswitch->destid = sw_destid;
883 } else
884 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
885 rio_name(rdev), rdev->vid, rdev->did);
887 return 0;
891 * rio_enum_complete- Tests if enumeration of a network is complete
892 * @port: Master port to send transaction
894 * Tests the Component Tag CSR for non-zero value (enumeration
895 * complete flag). Return %1 if enumeration is complete or %0 if
896 * enumeration is incomplete.
898 static int rio_enum_complete(struct rio_mport *port)
900 u32 tag_csr;
902 rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
903 return (tag_csr & 0xffff) ? 1 : 0;
907 * rio_disc_peer- Recursively discovers a RIO network through a master port
908 * @net: RIO network being discovered
909 * @port: Master port to send transactions
910 * @destid: Current destination ID in network
911 * @hopcount: Number of hops into the network
913 * Recursively discovers a RIO network. Transactions are sent via the
914 * master port passed in @port.
916 static int __devinit
917 rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
918 u8 hopcount)
920 u8 port_num, route_port;
921 int num_ports;
922 struct rio_dev *rdev;
923 u16 ndestid;
925 /* Setup new RIO device */
926 if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
927 /* Add device to the global and bus/net specific list. */
928 list_add_tail(&rdev->net_list, &net->devices);
929 } else
930 return -1;
932 if (rio_is_switch(rdev)) {
933 next_switchid++;
935 /* Associated destid is how we accessed this switch */
936 rdev->rswitch->destid = destid;
938 num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
939 pr_debug(
940 "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
941 rio_name(rdev), rdev->vid, rdev->did, num_ports);
942 for (port_num = 0; port_num < num_ports; port_num++) {
943 if (rio_get_swpinfo_inport(port, destid, hopcount) ==
944 port_num)
945 continue;
947 if (rio_sport_is_active
948 (port, destid, hopcount, port_num)) {
949 pr_debug(
950 "RIO: scanning device on port %d\n",
951 port_num);
953 rio_lock_device(port, destid, hopcount, 1000);
955 for (ndestid = 0;
956 ndestid < RIO_ANY_DESTID(port->sys_size);
957 ndestid++) {
958 rio_route_get_entry(port, rdev->rswitch,
959 RIO_GLOBAL_TABLE,
960 ndestid,
961 &route_port, 0);
962 if (route_port == port_num)
963 break;
966 rio_unlock_device(port, destid, hopcount);
967 if (rio_disc_peer
968 (net, port, ndestid, hopcount + 1) < 0)
969 return -1;
972 } else
973 pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
974 rio_name(rdev), rdev->vid, rdev->did);
976 return 0;
980 * rio_mport_is_active- Tests if master port link is active
981 * @port: Master port to test
983 * Reads the port error status CSR for the master port to
984 * determine if the port has an active link. Returns
985 * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
986 * or %0 if it is inactive.
988 static int rio_mport_is_active(struct rio_mport *port)
990 u32 result = 0;
991 u32 ext_ftr_ptr;
992 int *entry = rio_mport_phys_table;
994 do {
995 if ((ext_ftr_ptr =
996 rio_mport_get_feature(port, 1, 0, 0, *entry)))
997 break;
998 } while (*++entry >= 0);
1000 if (ext_ftr_ptr)
1001 rio_local_read_config_32(port,
1002 ext_ftr_ptr +
1003 RIO_PORT_N_ERR_STS_CSR(port->index),
1004 &result);
1006 return result & RIO_PORT_N_ERR_STS_PORT_OK;
1010 * rio_alloc_net- Allocate and configure a new RIO network
1011 * @port: Master port associated with the RIO network
1013 * Allocates a RIO network structure, initializes per-network
1014 * list heads, and adds the associated master port to the
1015 * network list of associated master ports. Returns a
1016 * RIO network pointer on success or %NULL on failure.
1018 static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
1020 struct rio_net *net;
1022 net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
1023 if (net) {
1024 INIT_LIST_HEAD(&net->node);
1025 INIT_LIST_HEAD(&net->devices);
1026 INIT_LIST_HEAD(&net->mports);
1027 list_add_tail(&port->nnode, &net->mports);
1028 net->hport = port;
1029 net->id = next_net++;
1031 return net;
1035 * rio_update_route_tables- Updates route tables in switches
1036 * @port: Master port associated with the RIO network
1038 * For each enumerated device, ensure that each switch in a system
1039 * has correct routing entries. Add routes for devices that where
1040 * unknown dirung the first enumeration pass through the switch.
1042 static void rio_update_route_tables(struct rio_mport *port)
1044 struct rio_dev *rdev;
1045 struct rio_switch *rswitch;
1046 u8 sport;
1047 u16 destid;
1049 list_for_each_entry(rdev, &rio_devices, global_list) {
1051 destid = (rio_is_switch(rdev))?rdev->rswitch->destid:rdev->destid;
1053 list_for_each_entry(rswitch, &rio_switches, node) {
1055 if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
1056 continue;
1058 if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
1059 /* Skip if destid ends in empty switch*/
1060 if (rswitch->destid == destid)
1061 continue;
1063 sport = rio_get_swpinfo_inport(port,
1064 rswitch->destid, rswitch->hopcount);
1066 if (rswitch->add_entry) {
1067 rio_route_add_entry(port, rswitch,
1068 RIO_GLOBAL_TABLE, destid,
1069 sport, 0);
1070 rswitch->route_table[destid] = sport;
1078 * rio_init_em - Initializes RIO Error Management (for switches)
1079 * @port: Master port associated with the RIO network
1081 * For each enumerated switch, call device-specific error management
1082 * initialization routine (if supplied by the switch driver).
1084 static void rio_init_em(struct rio_dev *rdev)
1086 if (rio_is_switch(rdev) && (rdev->em_efptr) &&
1087 (rdev->rswitch->em_init)) {
1088 rdev->rswitch->em_init(rdev);
1093 * rio_pw_enable - Enables/disables port-write handling by a master port
1094 * @port: Master port associated with port-write handling
1095 * @enable: 1=enable, 0=disable
1097 static void rio_pw_enable(struct rio_mport *port, int enable)
1099 if (port->ops->pwenable)
1100 port->ops->pwenable(port, enable);
1104 * rio_enum_mport- Start enumeration through a master port
1105 * @mport: Master port to send transactions
1107 * Starts the enumeration process. If somebody has enumerated our
1108 * master port device, then give up. If not and we have an active
1109 * link, then start recursive peer enumeration. Returns %0 if
1110 * enumeration succeeds or %-EBUSY if enumeration fails.
1112 int __devinit rio_enum_mport(struct rio_mport *mport)
1114 struct rio_net *net = NULL;
1115 int rc = 0;
1117 printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
1118 mport->name);
1119 /* If somebody else enumerated our master port device, bail. */
1120 if (rio_enum_host(mport) < 0) {
1121 printk(KERN_INFO
1122 "RIO: master port %d device has been enumerated by a remote host\n",
1123 mport->id);
1124 rc = -EBUSY;
1125 goto out;
1128 /* If master port has an active link, allocate net and enum peers */
1129 if (rio_mport_is_active(mport)) {
1130 if (!(net = rio_alloc_net(mport))) {
1131 printk(KERN_ERR "RIO: failed to allocate new net\n");
1132 rc = -ENOMEM;
1133 goto out;
1135 if (rio_enum_peer(net, mport, 0) < 0) {
1136 /* A higher priority host won enumeration, bail. */
1137 printk(KERN_INFO
1138 "RIO: master port %d device has lost enumeration to a remote host\n",
1139 mport->id);
1140 rio_clear_locks(mport);
1141 rc = -EBUSY;
1142 goto out;
1144 rio_update_route_tables(mport);
1145 rio_clear_locks(mport);
1146 rio_pw_enable(mport, 1);
1147 } else {
1148 printk(KERN_INFO "RIO: master port %d link inactive\n",
1149 mport->id);
1150 rc = -EINVAL;
1153 out:
1154 return rc;
1158 * rio_build_route_tables- Generate route tables from switch route entries
1160 * For each switch device, generate a route table by copying existing
1161 * route entries from the switch.
1163 static void rio_build_route_tables(void)
1165 struct rio_dev *rdev;
1166 int i;
1167 u8 sport;
1169 list_for_each_entry(rdev, &rio_devices, global_list)
1170 if (rio_is_switch(rdev)) {
1171 rio_lock_device(rdev->net->hport, rdev->rswitch->destid,
1172 rdev->rswitch->hopcount, 1000);
1173 for (i = 0;
1174 i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
1175 i++) {
1176 if (rio_route_get_entry
1177 (rdev->net->hport, rdev->rswitch,
1178 RIO_GLOBAL_TABLE, i, &sport, 0) < 0)
1179 continue;
1180 rdev->rswitch->route_table[i] = sport;
1183 rio_unlock_device(rdev->net->hport,
1184 rdev->rswitch->destid,
1185 rdev->rswitch->hopcount);
1190 * rio_enum_timeout- Signal that enumeration timed out
1191 * @data: Address of timeout flag.
1193 * When the enumeration complete timer expires, set a flag that
1194 * signals to the discovery process that enumeration did not
1195 * complete in a sane amount of time.
1197 static void rio_enum_timeout(unsigned long data)
1199 /* Enumeration timed out, set flag */
1200 *(int *)data = 1;
1204 * rio_disc_mport- Start discovery through a master port
1205 * @mport: Master port to send transactions
1207 * Starts the discovery process. If we have an active link,
1208 * then wait for the signal that enumeration is complete.
1209 * When enumeration completion is signaled, start recursive
1210 * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
1211 * on failure.
1213 int __devinit rio_disc_mport(struct rio_mport *mport)
1215 struct rio_net *net = NULL;
1216 int enum_timeout_flag = 0;
1218 printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
1219 mport->name);
1221 /* If master port has an active link, allocate net and discover peers */
1222 if (rio_mport_is_active(mport)) {
1223 if (!(net = rio_alloc_net(mport))) {
1224 printk(KERN_ERR "RIO: Failed to allocate new net\n");
1225 goto bail;
1228 pr_debug("RIO: wait for enumeration complete...");
1230 rio_enum_timer.expires =
1231 jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
1232 rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
1233 add_timer(&rio_enum_timer);
1234 while (!rio_enum_complete(mport)) {
1235 mdelay(1);
1236 if (enum_timeout_flag) {
1237 del_timer_sync(&rio_enum_timer);
1238 goto timeout;
1241 del_timer_sync(&rio_enum_timer);
1243 pr_debug("done\n");
1245 /* Read DestID assigned by enumerator */
1246 rio_local_read_config_32(mport, RIO_DID_CSR,
1247 &mport->host_deviceid);
1248 mport->host_deviceid = RIO_GET_DID(mport->sys_size,
1249 mport->host_deviceid);
1251 if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
1252 0) < 0) {
1253 printk(KERN_INFO
1254 "RIO: master port %d device has failed discovery\n",
1255 mport->id);
1256 goto bail;
1259 rio_build_route_tables();
1262 return 0;
1264 timeout:
1265 pr_debug("timeout\n");
1266 bail:
1267 return -EBUSY;