rio-scan.c 37 KB

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