rio-scan.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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_route_add_entry- Add a route entry to a switch routing table
  398. * @mport: Master port to send transaction
  399. * @rswitch: Switch device
  400. * @table: Routing table ID
  401. * @route_destid: Destination ID to be routed
  402. * @route_port: Port number to be routed
  403. *
  404. * Calls the switch specific add_entry() method to add a route entry
  405. * on a switch. The route table can be specified using the @table
  406. * argument if a switch has per port routing tables or the normal
  407. * use is to specific all tables (or the global table) by passing
  408. * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
  409. * on failure.
  410. */
  411. static int rio_route_add_entry(struct rio_mport *mport, struct rio_switch *rswitch,
  412. u16 table, u16 route_destid, u8 route_port)
  413. {
  414. return rswitch->add_entry(mport, rswitch->destid,
  415. rswitch->hopcount, table,
  416. route_destid, route_port);
  417. }
  418. /**
  419. * rio_route_get_entry- Read a route entry in a switch routing table
  420. * @mport: Master port to send transaction
  421. * @rswitch: Switch device
  422. * @table: Routing table ID
  423. * @route_destid: Destination ID to be routed
  424. * @route_port: Pointer to read port number into
  425. *
  426. * Calls the switch specific get_entry() method to read a route entry
  427. * in a switch. The route table can be specified using the @table
  428. * argument if a switch has per port routing tables or the normal
  429. * use is to specific all tables (or the global table) by passing
  430. * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
  431. * on failure.
  432. */
  433. static int
  434. rio_route_get_entry(struct rio_mport *mport, struct rio_switch *rswitch, u16 table,
  435. u16 route_destid, u8 * route_port)
  436. {
  437. return rswitch->get_entry(mport, rswitch->destid,
  438. rswitch->hopcount, table,
  439. route_destid, route_port);
  440. }
  441. /**
  442. * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
  443. * @port: Master port to send transaction
  444. * @hopcount: Number of hops to the device
  445. *
  446. * Used during enumeration to read the Host Device ID Lock CSR on a
  447. * RIO device. Returns the value of the lock register.
  448. */
  449. static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
  450. {
  451. u32 result;
  452. rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
  453. RIO_HOST_DID_LOCK_CSR, &result);
  454. return (u16) (result & 0xffff);
  455. }
  456. /**
  457. * rio_get_swpinfo_inport- Gets the ingress port number
  458. * @mport: Master port to send transaction
  459. * @destid: Destination ID associated with the switch
  460. * @hopcount: Number of hops to the device
  461. *
  462. * Returns port number being used to access the switch device.
  463. */
  464. static u8
  465. rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
  466. {
  467. u32 result;
  468. rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
  469. &result);
  470. return (u8) (result & 0xff);
  471. }
  472. /**
  473. * rio_get_swpinfo_tports- Gets total number of ports on the switch
  474. * @mport: Master port to send transaction
  475. * @destid: Destination ID associated with the switch
  476. * @hopcount: Number of hops to the device
  477. *
  478. * Returns total numbers of ports implemented by the switch device.
  479. */
  480. static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
  481. u8 hopcount)
  482. {
  483. u32 result;
  484. rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
  485. &result);
  486. return RIO_GET_TOTAL_PORTS(result);
  487. }
  488. /**
  489. * rio_net_add_mport- Add a master port to a RIO network
  490. * @net: RIO network
  491. * @port: Master port to add
  492. *
  493. * Adds a master port to the network list of associated master
  494. * ports..
  495. */
  496. static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
  497. {
  498. spin_lock(&rio_global_list_lock);
  499. list_add_tail(&port->nnode, &net->mports);
  500. spin_unlock(&rio_global_list_lock);
  501. }
  502. /**
  503. * rio_enum_peer- Recursively enumerate a RIO network through a master port
  504. * @net: RIO network being enumerated
  505. * @port: Master port to send transactions
  506. * @hopcount: Number of hops into the network
  507. *
  508. * Recursively enumerates a RIO network. Transactions are sent via the
  509. * master port passed in @port.
  510. */
  511. static int __devinit rio_enum_peer(struct rio_net *net, struct rio_mport *port,
  512. u8 hopcount)
  513. {
  514. int port_num;
  515. int num_ports;
  516. int cur_destid;
  517. int sw_destid;
  518. int sw_inport;
  519. struct rio_dev *rdev;
  520. u16 destid;
  521. int tmp;
  522. if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
  523. pr_debug("RIO: PE already discovered by this host\n");
  524. /*
  525. * Already discovered by this host. Add it as another
  526. * master port for the current network.
  527. */
  528. rio_net_add_mport(net, port);
  529. return 0;
  530. }
  531. /* Attempt to acquire device lock */
  532. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  533. hopcount,
  534. RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  535. while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
  536. < port->host_deviceid) {
  537. /* Delay a bit */
  538. mdelay(1);
  539. /* Attempt to acquire device lock again */
  540. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  541. hopcount,
  542. RIO_HOST_DID_LOCK_CSR,
  543. port->host_deviceid);
  544. }
  545. if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
  546. pr_debug(
  547. "RIO: PE locked by a higher priority host...retreating\n");
  548. return -1;
  549. }
  550. /* Setup new RIO device */
  551. rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
  552. hopcount, 1);
  553. if (rdev) {
  554. /* Add device to the global and bus/net specific list. */
  555. list_add_tail(&rdev->net_list, &net->devices);
  556. } else
  557. return -1;
  558. if (rio_is_switch(rdev)) {
  559. next_switchid++;
  560. sw_inport = rio_get_swpinfo_inport(port,
  561. RIO_ANY_DESTID(port->sys_size), hopcount);
  562. rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
  563. port->host_deviceid, sw_inport);
  564. rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
  565. for (destid = 0; destid < next_destid; destid++) {
  566. if (destid == port->host_deviceid)
  567. continue;
  568. rio_route_add_entry(port, rdev->rswitch, RIO_GLOBAL_TABLE,
  569. destid, sw_inport);
  570. rdev->rswitch->route_table[destid] = sw_inport;
  571. }
  572. num_ports =
  573. rio_get_swpinfo_tports(port, RIO_ANY_DESTID(port->sys_size),
  574. hopcount);
  575. pr_debug(
  576. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  577. rio_name(rdev), rdev->vid, rdev->did, num_ports);
  578. sw_destid = next_destid;
  579. for (port_num = 0; port_num < num_ports; port_num++) {
  580. if (sw_inport == port_num)
  581. continue;
  582. cur_destid = next_destid;
  583. if (rio_sport_is_active
  584. (port, RIO_ANY_DESTID(port->sys_size), hopcount,
  585. port_num)) {
  586. pr_debug(
  587. "RIO: scanning device on port %d\n",
  588. port_num);
  589. rio_route_add_entry(port, rdev->rswitch,
  590. RIO_GLOBAL_TABLE,
  591. RIO_ANY_DESTID(port->sys_size),
  592. port_num);
  593. if (rio_enum_peer(net, port, hopcount + 1) < 0)
  594. return -1;
  595. /* Update routing tables */
  596. if (next_destid > cur_destid) {
  597. for (destid = cur_destid;
  598. destid < next_destid; destid++) {
  599. if (destid == port->host_deviceid)
  600. continue;
  601. rio_route_add_entry(port, rdev->rswitch,
  602. RIO_GLOBAL_TABLE,
  603. destid,
  604. port_num);
  605. rdev->rswitch->
  606. route_table[destid] =
  607. port_num;
  608. }
  609. }
  610. }
  611. }
  612. /* Check for empty switch */
  613. if (next_destid == sw_destid) {
  614. next_destid++;
  615. if (next_destid == port->host_deviceid)
  616. next_destid++;
  617. }
  618. rdev->rswitch->destid = sw_destid;
  619. } else
  620. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  621. rio_name(rdev), rdev->vid, rdev->did);
  622. return 0;
  623. }
  624. /**
  625. * rio_enum_complete- Tests if enumeration of a network is complete
  626. * @port: Master port to send transaction
  627. *
  628. * Tests the Component Tag CSR for presence of the magic enumeration
  629. * complete flag. Return %1 if enumeration is complete or %0 if
  630. * enumeration is incomplete.
  631. */
  632. static int rio_enum_complete(struct rio_mport *port)
  633. {
  634. u32 tag_csr;
  635. int ret = 0;
  636. rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
  637. if (tag_csr == RIO_ENUM_CMPL_MAGIC)
  638. ret = 1;
  639. return ret;
  640. }
  641. /**
  642. * rio_disc_peer- Recursively discovers a RIO network through a master port
  643. * @net: RIO network being discovered
  644. * @port: Master port to send transactions
  645. * @destid: Current destination ID in network
  646. * @hopcount: Number of hops into the network
  647. *
  648. * Recursively discovers a RIO network. Transactions are sent via the
  649. * master port passed in @port.
  650. */
  651. static int __devinit
  652. rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
  653. u8 hopcount)
  654. {
  655. u8 port_num, route_port;
  656. int num_ports;
  657. struct rio_dev *rdev;
  658. u16 ndestid;
  659. /* Setup new RIO device */
  660. if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
  661. /* Add device to the global and bus/net specific list. */
  662. list_add_tail(&rdev->net_list, &net->devices);
  663. } else
  664. return -1;
  665. if (rio_is_switch(rdev)) {
  666. next_switchid++;
  667. /* Associated destid is how we accessed this switch */
  668. rdev->rswitch->destid = destid;
  669. num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
  670. pr_debug(
  671. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  672. rio_name(rdev), rdev->vid, rdev->did, num_ports);
  673. for (port_num = 0; port_num < num_ports; port_num++) {
  674. if (rio_get_swpinfo_inport(port, destid, hopcount) ==
  675. port_num)
  676. continue;
  677. if (rio_sport_is_active
  678. (port, destid, hopcount, port_num)) {
  679. pr_debug(
  680. "RIO: scanning device on port %d\n",
  681. port_num);
  682. for (ndestid = 0;
  683. ndestid < RIO_ANY_DESTID(port->sys_size);
  684. ndestid++) {
  685. rio_route_get_entry(port, rdev->rswitch,
  686. RIO_GLOBAL_TABLE,
  687. ndestid,
  688. &route_port);
  689. if (route_port == port_num)
  690. break;
  691. }
  692. if (rio_disc_peer
  693. (net, port, ndestid, hopcount + 1) < 0)
  694. return -1;
  695. }
  696. }
  697. } else
  698. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  699. rio_name(rdev), rdev->vid, rdev->did);
  700. return 0;
  701. }
  702. /**
  703. * rio_mport_is_active- Tests if master port link is active
  704. * @port: Master port to test
  705. *
  706. * Reads the port error status CSR for the master port to
  707. * determine if the port has an active link. Returns
  708. * %PORT_N_ERR_STS_PORT_OK if the master port is active
  709. * or %0 if it is inactive.
  710. */
  711. static int rio_mport_is_active(struct rio_mport *port)
  712. {
  713. u32 result = 0;
  714. u32 ext_ftr_ptr;
  715. int *entry = rio_mport_phys_table;
  716. do {
  717. if ((ext_ftr_ptr =
  718. rio_mport_get_feature(port, 1, 0, 0, *entry)))
  719. break;
  720. } while (*++entry >= 0);
  721. if (ext_ftr_ptr)
  722. rio_local_read_config_32(port,
  723. ext_ftr_ptr +
  724. RIO_PORT_N_ERR_STS_CSR(port->index),
  725. &result);
  726. return (result & PORT_N_ERR_STS_PORT_OK);
  727. }
  728. /**
  729. * rio_alloc_net- Allocate and configure a new RIO network
  730. * @port: Master port associated with the RIO network
  731. *
  732. * Allocates a RIO network structure, initializes per-network
  733. * list heads, and adds the associated master port to the
  734. * network list of associated master ports. Returns a
  735. * RIO network pointer on success or %NULL on failure.
  736. */
  737. static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
  738. {
  739. struct rio_net *net;
  740. net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
  741. if (net) {
  742. INIT_LIST_HEAD(&net->node);
  743. INIT_LIST_HEAD(&net->devices);
  744. INIT_LIST_HEAD(&net->mports);
  745. list_add_tail(&port->nnode, &net->mports);
  746. net->hport = port;
  747. net->id = next_net++;
  748. }
  749. return net;
  750. }
  751. /**
  752. * rio_update_route_tables- Updates route tables in switches
  753. * @port: Master port associated with the RIO network
  754. *
  755. * For each enumerated device, ensure that each switch in a system
  756. * has correct routing entries. Add routes for devices that where
  757. * unknown dirung the first enumeration pass through the switch.
  758. */
  759. static void rio_update_route_tables(struct rio_mport *port)
  760. {
  761. struct rio_dev *rdev;
  762. struct rio_switch *rswitch;
  763. u8 sport;
  764. u16 destid;
  765. list_for_each_entry(rdev, &rio_devices, global_list) {
  766. destid = (rio_is_switch(rdev))?rdev->rswitch->destid:rdev->destid;
  767. list_for_each_entry(rswitch, &rio_switches, node) {
  768. if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
  769. continue;
  770. if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
  771. /* Skip if destid ends in empty switch*/
  772. if (rswitch->destid == destid)
  773. continue;
  774. sport = rio_get_swpinfo_inport(port,
  775. rswitch->destid, rswitch->hopcount);
  776. if (rswitch->add_entry) {
  777. rio_route_add_entry(port, rswitch, RIO_GLOBAL_TABLE, destid, sport);
  778. rswitch->route_table[destid] = sport;
  779. }
  780. }
  781. }
  782. }
  783. }
  784. /**
  785. * rio_enum_mport- Start enumeration through a master port
  786. * @mport: Master port to send transactions
  787. *
  788. * Starts the enumeration process. If somebody has enumerated our
  789. * master port device, then give up. If not and we have an active
  790. * link, then start recursive peer enumeration. Returns %0 if
  791. * enumeration succeeds or %-EBUSY if enumeration fails.
  792. */
  793. int __devinit rio_enum_mport(struct rio_mport *mport)
  794. {
  795. struct rio_net *net = NULL;
  796. int rc = 0;
  797. printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
  798. mport->name);
  799. /* If somebody else enumerated our master port device, bail. */
  800. if (rio_enum_host(mport) < 0) {
  801. printk(KERN_INFO
  802. "RIO: master port %d device has been enumerated by a remote host\n",
  803. mport->id);
  804. rc = -EBUSY;
  805. goto out;
  806. }
  807. /* If master port has an active link, allocate net and enum peers */
  808. if (rio_mport_is_active(mport)) {
  809. if (!(net = rio_alloc_net(mport))) {
  810. printk(KERN_ERR "RIO: failed to allocate new net\n");
  811. rc = -ENOMEM;
  812. goto out;
  813. }
  814. if (rio_enum_peer(net, mport, 0) < 0) {
  815. /* A higher priority host won enumeration, bail. */
  816. printk(KERN_INFO
  817. "RIO: master port %d device has lost enumeration to a remote host\n",
  818. mport->id);
  819. rio_clear_locks(mport);
  820. rc = -EBUSY;
  821. goto out;
  822. }
  823. rio_update_route_tables(mport);
  824. rio_clear_locks(mport);
  825. } else {
  826. printk(KERN_INFO "RIO: master port %d link inactive\n",
  827. mport->id);
  828. rc = -EINVAL;
  829. }
  830. out:
  831. return rc;
  832. }
  833. /**
  834. * rio_build_route_tables- Generate route tables from switch route entries
  835. *
  836. * For each switch device, generate a route table by copying existing
  837. * route entries from the switch.
  838. */
  839. static void rio_build_route_tables(void)
  840. {
  841. struct rio_dev *rdev;
  842. int i;
  843. u8 sport;
  844. list_for_each_entry(rdev, &rio_devices, global_list)
  845. if (rio_is_switch(rdev))
  846. for (i = 0;
  847. i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
  848. i++) {
  849. if (rio_route_get_entry
  850. (rdev->net->hport, rdev->rswitch, RIO_GLOBAL_TABLE,
  851. i, &sport) < 0)
  852. continue;
  853. rdev->rswitch->route_table[i] = sport;
  854. }
  855. }
  856. /**
  857. * rio_enum_timeout- Signal that enumeration timed out
  858. * @data: Address of timeout flag.
  859. *
  860. * When the enumeration complete timer expires, set a flag that
  861. * signals to the discovery process that enumeration did not
  862. * complete in a sane amount of time.
  863. */
  864. static void rio_enum_timeout(unsigned long data)
  865. {
  866. /* Enumeration timed out, set flag */
  867. *(int *)data = 1;
  868. }
  869. /**
  870. * rio_disc_mport- Start discovery through a master port
  871. * @mport: Master port to send transactions
  872. *
  873. * Starts the discovery process. If we have an active link,
  874. * then wait for the signal that enumeration is complete.
  875. * When enumeration completion is signaled, start recursive
  876. * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
  877. * on failure.
  878. */
  879. int __devinit rio_disc_mport(struct rio_mport *mport)
  880. {
  881. struct rio_net *net = NULL;
  882. int enum_timeout_flag = 0;
  883. printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
  884. mport->name);
  885. /* If master port has an active link, allocate net and discover peers */
  886. if (rio_mport_is_active(mport)) {
  887. if (!(net = rio_alloc_net(mport))) {
  888. printk(KERN_ERR "RIO: Failed to allocate new net\n");
  889. goto bail;
  890. }
  891. pr_debug("RIO: wait for enumeration complete...");
  892. rio_enum_timer.expires =
  893. jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
  894. rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
  895. add_timer(&rio_enum_timer);
  896. while (!rio_enum_complete(mport)) {
  897. mdelay(1);
  898. if (enum_timeout_flag) {
  899. del_timer_sync(&rio_enum_timer);
  900. goto timeout;
  901. }
  902. }
  903. del_timer_sync(&rio_enum_timer);
  904. pr_debug("done\n");
  905. if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
  906. 0) < 0) {
  907. printk(KERN_INFO
  908. "RIO: master port %d device has failed discovery\n",
  909. mport->id);
  910. goto bail;
  911. }
  912. rio_build_route_tables();
  913. }
  914. return 0;
  915. timeout:
  916. pr_debug("timeout\n");
  917. bail:
  918. return -EBUSY;
  919. }