rio-scan.c 35 KB

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