rio-scan.c 36 KB

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