rio-scan.c 35 KB

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