rio-scan.c 36 KB

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