rio-scan.c 36 KB

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