rio-scan.c 36 KB

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