rio-scan.c 32 KB

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