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