rio-scan.c 36 KB

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