rio-scan.c 35 KB

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