rio-scan.c 27 KB

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