rio-scan.c 36 KB

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