rio-scan.c 28 KB

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