rio-scan.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 = kmalloc(sizeof(struct rio_dev), GFP_KERNEL);
  262. if (!rdev)
  263. goto out;
  264. memset(rdev, 0, sizeof(struct rio_dev));
  265. rdev->net = net;
  266. rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
  267. &result);
  268. rdev->did = result >> 16;
  269. rdev->vid = result & 0xffff;
  270. rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
  271. &rdev->device_rev);
  272. rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
  273. &result);
  274. rdev->asm_did = result >> 16;
  275. rdev->asm_vid = result & 0xffff;
  276. rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
  277. &result);
  278. rdev->asm_rev = result >> 16;
  279. rio_mport_read_config_32(port, destid, hopcount, RIO_PEF_CAR,
  280. &rdev->pef);
  281. if (rdev->pef & RIO_PEF_EXT_FEATURES)
  282. rdev->efptr = result & 0xffff;
  283. rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
  284. &rdev->src_ops);
  285. rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
  286. &rdev->dst_ops);
  287. if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)
  288. && do_enum) {
  289. rio_set_device_id(port, destid, hopcount, next_destid);
  290. rdev->destid = next_destid++;
  291. if (next_destid == port->host_deviceid)
  292. next_destid++;
  293. } else
  294. rdev->destid = rio_get_device_id(port, destid, hopcount);
  295. /* If a PE has both switch and other functions, show it as a switch */
  296. if (rio_is_switch(rdev)) {
  297. rio_mport_read_config_32(port, destid, hopcount,
  298. RIO_SWP_INFO_CAR, &rdev->swpinfo);
  299. rswitch = kmalloc(sizeof(struct rio_switch), GFP_KERNEL);
  300. if (!rswitch) {
  301. kfree(rdev);
  302. rdev = NULL;
  303. goto out;
  304. }
  305. rswitch->switchid = next_switchid;
  306. rswitch->hopcount = hopcount;
  307. rswitch->destid = 0xffff;
  308. /* Initialize switch route table */
  309. for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES; rdid++)
  310. rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
  311. rdev->rswitch = rswitch;
  312. sprintf(rio_name(rdev), "%02x:s:%04x", rdev->net->id,
  313. rdev->rswitch->switchid);
  314. rio_route_set_ops(rdev);
  315. list_add_tail(&rswitch->node, &rio_switches);
  316. } else
  317. sprintf(rio_name(rdev), "%02x:e:%04x", rdev->net->id,
  318. rdev->destid);
  319. rdev->dev.bus = &rio_bus_type;
  320. device_initialize(&rdev->dev);
  321. rdev->dev.release = rio_release_dev;
  322. rio_dev_get(rdev);
  323. rdev->dma_mask = DMA_32BIT_MASK;
  324. rdev->dev.dma_mask = &rdev->dma_mask;
  325. rdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
  326. if ((rdev->pef & RIO_PEF_INB_DOORBELL) &&
  327. (rdev->dst_ops & RIO_DST_OPS_DOORBELL))
  328. rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
  329. 0, 0xffff);
  330. rio_add_device(rdev);
  331. out:
  332. return rdev;
  333. }
  334. /**
  335. * rio_sport_is_active- Tests if a switch port has an active connection.
  336. * @port: Master port to send transaction
  337. * @destid: Associated destination ID for switch
  338. * @hopcount: Hopcount to reach switch
  339. * @sport: Switch port number
  340. *
  341. * Reads the port error status CSR for a particular switch port to
  342. * determine if the port has an active link. Returns
  343. * %PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
  344. * inactive.
  345. */
  346. static int
  347. rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
  348. {
  349. u32 result;
  350. u32 ext_ftr_ptr;
  351. int *entry = rio_sport_phys_table;
  352. do {
  353. if ((ext_ftr_ptr =
  354. rio_mport_get_feature(port, 0, destid, hopcount, *entry)))
  355. break;
  356. } while (*++entry >= 0);
  357. if (ext_ftr_ptr)
  358. rio_mport_read_config_32(port, destid, hopcount,
  359. ext_ftr_ptr +
  360. RIO_PORT_N_ERR_STS_CSR(sport),
  361. &result);
  362. return (result & PORT_N_ERR_STS_PORT_OK);
  363. }
  364. /**
  365. * rio_route_add_entry- Add a route entry to a switch routing table
  366. * @mport: Master port to send transaction
  367. * @rdev: Switch device
  368. * @table: Routing table ID
  369. * @route_destid: Destination ID to be routed
  370. * @route_port: Port number to be routed
  371. *
  372. * Calls the switch specific add_entry() method to add a route entry
  373. * on a switch. The route table can be specified using the @table
  374. * argument if a switch has per port routing tables or the normal
  375. * use is to specific all tables (or the global table) by passing
  376. * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
  377. * on failure.
  378. */
  379. static int rio_route_add_entry(struct rio_mport *mport, struct rio_dev *rdev,
  380. u16 table, u16 route_destid, u8 route_port)
  381. {
  382. return rdev->rswitch->add_entry(mport, rdev->rswitch->destid,
  383. rdev->rswitch->hopcount, table,
  384. route_destid, route_port);
  385. }
  386. /**
  387. * rio_route_get_entry- Read a route entry in a switch routing table
  388. * @mport: Master port to send transaction
  389. * @rdev: Switch device
  390. * @table: Routing table ID
  391. * @route_destid: Destination ID to be routed
  392. * @route_port: Pointer to read port number into
  393. *
  394. * Calls the switch specific get_entry() method to read a route entry
  395. * in a switch. The route table can be specified using the @table
  396. * argument if a switch has per port routing tables or the normal
  397. * use is to specific all tables (or the global table) by passing
  398. * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
  399. * on failure.
  400. */
  401. static int
  402. rio_route_get_entry(struct rio_mport *mport, struct rio_dev *rdev, u16 table,
  403. u16 route_destid, u8 * route_port)
  404. {
  405. return rdev->rswitch->get_entry(mport, rdev->rswitch->destid,
  406. rdev->rswitch->hopcount, table,
  407. route_destid, route_port);
  408. }
  409. /**
  410. * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
  411. * @port: Master port to send transaction
  412. * @hopcount: Number of hops to the device
  413. *
  414. * Used during enumeration to read the Host Device ID Lock CSR on a
  415. * RIO device. Returns the value of the lock register.
  416. */
  417. static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
  418. {
  419. u32 result;
  420. rio_mport_read_config_32(port, RIO_ANY_DESTID, hopcount,
  421. RIO_HOST_DID_LOCK_CSR, &result);
  422. return (u16) (result & 0xffff);
  423. }
  424. /**
  425. * rio_get_swpinfo_inport- Gets the ingress port number
  426. * @mport: Master port to send transaction
  427. * @destid: Destination ID associated with the switch
  428. * @hopcount: Number of hops to the device
  429. *
  430. * Returns port number being used to access the switch device.
  431. */
  432. static u8
  433. rio_get_swpinfo_inport(struct rio_mport *mport, u16 destid, u8 hopcount)
  434. {
  435. u32 result;
  436. rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
  437. &result);
  438. return (u8) (result & 0xff);
  439. }
  440. /**
  441. * rio_get_swpinfo_tports- Gets total number of ports on the switch
  442. * @mport: Master port to send transaction
  443. * @destid: Destination ID associated with the switch
  444. * @hopcount: Number of hops to the device
  445. *
  446. * Returns total numbers of ports implemented by the switch device.
  447. */
  448. static u8 rio_get_swpinfo_tports(struct rio_mport *mport, u16 destid,
  449. u8 hopcount)
  450. {
  451. u32 result;
  452. rio_mport_read_config_32(mport, destid, hopcount, RIO_SWP_INFO_CAR,
  453. &result);
  454. return RIO_GET_TOTAL_PORTS(result);
  455. }
  456. /**
  457. * rio_net_add_mport- Add a master port to a RIO network
  458. * @net: RIO network
  459. * @port: Master port to add
  460. *
  461. * Adds a master port to the network list of associated master
  462. * ports..
  463. */
  464. static void rio_net_add_mport(struct rio_net *net, struct rio_mport *port)
  465. {
  466. spin_lock(&rio_global_list_lock);
  467. list_add_tail(&port->nnode, &net->mports);
  468. spin_unlock(&rio_global_list_lock);
  469. }
  470. /**
  471. * rio_enum_peer- Recursively enumerate a RIO network through a master port
  472. * @net: RIO network being enumerated
  473. * @port: Master port to send transactions
  474. * @hopcount: Number of hops into the network
  475. *
  476. * Recursively enumerates a RIO network. Transactions are sent via the
  477. * master port passed in @port.
  478. */
  479. static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
  480. u8 hopcount)
  481. {
  482. int port_num;
  483. int num_ports;
  484. int cur_destid;
  485. struct rio_dev *rdev;
  486. u16 destid;
  487. int tmp;
  488. if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
  489. pr_debug("RIO: PE already discovered by this host\n");
  490. /*
  491. * Already discovered by this host. Add it as another
  492. * master port for the current network.
  493. */
  494. rio_net_add_mport(net, port);
  495. return 0;
  496. }
  497. /* Attempt to acquire device lock */
  498. rio_mport_write_config_32(port, RIO_ANY_DESTID, hopcount,
  499. RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  500. while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
  501. < port->host_deviceid) {
  502. /* Delay a bit */
  503. mdelay(1);
  504. /* Attempt to acquire device lock again */
  505. rio_mport_write_config_32(port, RIO_ANY_DESTID, hopcount,
  506. RIO_HOST_DID_LOCK_CSR,
  507. port->host_deviceid);
  508. }
  509. if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
  510. pr_debug(
  511. "RIO: PE locked by a higher priority host...retreating\n");
  512. return -1;
  513. }
  514. /* Setup new RIO device */
  515. if ((rdev = rio_setup_device(net, port, RIO_ANY_DESTID, hopcount, 1))) {
  516. /* Add device to the global and bus/net specific list. */
  517. list_add_tail(&rdev->net_list, &net->devices);
  518. } else
  519. return -1;
  520. if (rio_is_switch(rdev)) {
  521. next_switchid++;
  522. for (destid = 0; destid < next_destid; destid++) {
  523. rio_route_add_entry(port, rdev, RIO_GLOBAL_TABLE,
  524. destid, rio_get_swpinfo_inport(port,
  525. RIO_ANY_DESTID,
  526. hopcount));
  527. rdev->rswitch->route_table[destid] =
  528. rio_get_swpinfo_inport(port, RIO_ANY_DESTID,
  529. hopcount);
  530. }
  531. num_ports =
  532. rio_get_swpinfo_tports(port, RIO_ANY_DESTID, hopcount);
  533. pr_debug(
  534. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  535. rio_name(rdev), rdev->vid, rdev->did, num_ports);
  536. for (port_num = 0; port_num < num_ports; port_num++) {
  537. if (rio_get_swpinfo_inport
  538. (port, RIO_ANY_DESTID, hopcount) == port_num)
  539. continue;
  540. cur_destid = next_destid;
  541. if (rio_sport_is_active
  542. (port, RIO_ANY_DESTID, hopcount, port_num)) {
  543. pr_debug(
  544. "RIO: scanning device on port %d\n",
  545. port_num);
  546. rio_route_add_entry(port, rdev,
  547. RIO_GLOBAL_TABLE,
  548. RIO_ANY_DESTID, port_num);
  549. if (rio_enum_peer(net, port, hopcount + 1) < 0)
  550. return -1;
  551. /* Update routing tables */
  552. if (next_destid > cur_destid) {
  553. for (destid = cur_destid;
  554. destid < next_destid; destid++) {
  555. rio_route_add_entry(port, rdev,
  556. RIO_GLOBAL_TABLE,
  557. destid,
  558. port_num);
  559. rdev->rswitch->
  560. route_table[destid] =
  561. port_num;
  562. }
  563. rdev->rswitch->destid = cur_destid;
  564. }
  565. }
  566. }
  567. } else
  568. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  569. rio_name(rdev), rdev->vid, rdev->did);
  570. return 0;
  571. }
  572. /**
  573. * rio_enum_complete- Tests if enumeration of a network is complete
  574. * @port: Master port to send transaction
  575. *
  576. * Tests the Component Tag CSR for presence of the magic enumeration
  577. * complete flag. Return %1 if enumeration is complete or %0 if
  578. * enumeration is incomplete.
  579. */
  580. static int rio_enum_complete(struct rio_mport *port)
  581. {
  582. u32 tag_csr;
  583. int ret = 0;
  584. rio_local_read_config_32(port, RIO_COMPONENT_TAG_CSR, &tag_csr);
  585. if (tag_csr == RIO_ENUM_CMPL_MAGIC)
  586. ret = 1;
  587. return ret;
  588. }
  589. /**
  590. * rio_disc_peer- Recursively discovers a RIO network through a master port
  591. * @net: RIO network being discovered
  592. * @port: Master port to send transactions
  593. * @destid: Current destination ID in network
  594. * @hopcount: Number of hops into the network
  595. *
  596. * Recursively discovers a RIO network. Transactions are sent via the
  597. * master port passed in @port.
  598. */
  599. static int
  600. rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
  601. u8 hopcount)
  602. {
  603. u8 port_num, route_port;
  604. int num_ports;
  605. struct rio_dev *rdev;
  606. u16 ndestid;
  607. /* Setup new RIO device */
  608. if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
  609. /* Add device to the global and bus/net specific list. */
  610. list_add_tail(&rdev->net_list, &net->devices);
  611. } else
  612. return -1;
  613. if (rio_is_switch(rdev)) {
  614. next_switchid++;
  615. /* Associated destid is how we accessed this switch */
  616. rdev->rswitch->destid = destid;
  617. num_ports = rio_get_swpinfo_tports(port, destid, hopcount);
  618. pr_debug(
  619. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  620. rio_name(rdev), rdev->vid, rdev->did, num_ports);
  621. for (port_num = 0; port_num < num_ports; port_num++) {
  622. if (rio_get_swpinfo_inport(port, destid, hopcount) ==
  623. port_num)
  624. continue;
  625. if (rio_sport_is_active
  626. (port, destid, hopcount, port_num)) {
  627. pr_debug(
  628. "RIO: scanning device on port %d\n",
  629. port_num);
  630. for (ndestid = 0; ndestid < RIO_ANY_DESTID;
  631. ndestid++) {
  632. rio_route_get_entry(port, rdev,
  633. RIO_GLOBAL_TABLE,
  634. ndestid,
  635. &route_port);
  636. if (route_port == port_num)
  637. break;
  638. }
  639. if (rio_disc_peer
  640. (net, port, ndestid, hopcount + 1) < 0)
  641. return -1;
  642. }
  643. }
  644. } else
  645. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  646. rio_name(rdev), rdev->vid, rdev->did);
  647. return 0;
  648. }
  649. /**
  650. * rio_mport_is_active- Tests if master port link is active
  651. * @port: Master port to test
  652. *
  653. * Reads the port error status CSR for the master port to
  654. * determine if the port has an active link. Returns
  655. * %PORT_N_ERR_STS_PORT_OK if the master port is active
  656. * or %0 if it is inactive.
  657. */
  658. static int rio_mport_is_active(struct rio_mport *port)
  659. {
  660. u32 result = 0;
  661. u32 ext_ftr_ptr;
  662. int *entry = rio_mport_phys_table;
  663. do {
  664. if ((ext_ftr_ptr =
  665. rio_mport_get_feature(port, 1, 0, 0, *entry)))
  666. break;
  667. } while (*++entry >= 0);
  668. if (ext_ftr_ptr)
  669. rio_local_read_config_32(port,
  670. ext_ftr_ptr +
  671. RIO_PORT_N_ERR_STS_CSR(port->index),
  672. &result);
  673. return (result & PORT_N_ERR_STS_PORT_OK);
  674. }
  675. /**
  676. * rio_alloc_net- Allocate and configure a new RIO network
  677. * @port: Master port associated with the RIO network
  678. *
  679. * Allocates a RIO network structure, initializes per-network
  680. * list heads, and adds the associated master port to the
  681. * network list of associated master ports. Returns a
  682. * RIO network pointer on success or %NULL on failure.
  683. */
  684. static struct rio_net __devinit *rio_alloc_net(struct rio_mport *port)
  685. {
  686. struct rio_net *net;
  687. net = kmalloc(sizeof(struct rio_net), GFP_KERNEL);
  688. if (net) {
  689. memset(net, 0, sizeof(struct rio_net));
  690. INIT_LIST_HEAD(&net->node);
  691. INIT_LIST_HEAD(&net->devices);
  692. INIT_LIST_HEAD(&net->mports);
  693. list_add_tail(&port->nnode, &net->mports);
  694. net->hport = port;
  695. net->id = next_net++;
  696. }
  697. return net;
  698. }
  699. /**
  700. * rio_enum_mport- Start enumeration through a master port
  701. * @mport: Master port to send transactions
  702. *
  703. * Starts the enumeration process. If somebody has enumerated our
  704. * master port device, then give up. If not and we have an active
  705. * link, then start recursive peer enumeration. Returns %0 if
  706. * enumeration succeeds or %-EBUSY if enumeration fails.
  707. */
  708. int rio_enum_mport(struct rio_mport *mport)
  709. {
  710. struct rio_net *net = NULL;
  711. int rc = 0;
  712. printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
  713. mport->name);
  714. /* If somebody else enumerated our master port device, bail. */
  715. if (rio_enum_host(mport) < 0) {
  716. printk(KERN_INFO
  717. "RIO: master port %d device has been enumerated by a remote host\n",
  718. mport->id);
  719. rc = -EBUSY;
  720. goto out;
  721. }
  722. /* If master port has an active link, allocate net and enum peers */
  723. if (rio_mport_is_active(mport)) {
  724. if (!(net = rio_alloc_net(mport))) {
  725. printk(KERN_ERR "RIO: failed to allocate new net\n");
  726. rc = -ENOMEM;
  727. goto out;
  728. }
  729. if (rio_enum_peer(net, mport, 0) < 0) {
  730. /* A higher priority host won enumeration, bail. */
  731. printk(KERN_INFO
  732. "RIO: master port %d device has lost enumeration to a remote host\n",
  733. mport->id);
  734. rio_clear_locks(mport);
  735. rc = -EBUSY;
  736. goto out;
  737. }
  738. rio_clear_locks(mport);
  739. } else {
  740. printk(KERN_INFO "RIO: master port %d link inactive\n",
  741. mport->id);
  742. rc = -EINVAL;
  743. }
  744. out:
  745. return rc;
  746. }
  747. /**
  748. * rio_build_route_tables- Generate route tables from switch route entries
  749. *
  750. * For each switch device, generate a route table by copying existing
  751. * route entries from the switch.
  752. */
  753. static void rio_build_route_tables(void)
  754. {
  755. struct rio_dev *rdev;
  756. int i;
  757. u8 sport;
  758. list_for_each_entry(rdev, &rio_devices, global_list)
  759. if (rio_is_switch(rdev))
  760. for (i = 0; i < RIO_MAX_ROUTE_ENTRIES; i++) {
  761. if (rio_route_get_entry
  762. (rdev->net->hport, rdev, RIO_GLOBAL_TABLE, i,
  763. &sport) < 0)
  764. continue;
  765. rdev->rswitch->route_table[i] = sport;
  766. }
  767. }
  768. /**
  769. * rio_enum_timeout- Signal that enumeration timed out
  770. * @data: Address of timeout flag.
  771. *
  772. * When the enumeration complete timer expires, set a flag that
  773. * signals to the discovery process that enumeration did not
  774. * complete in a sane amount of time.
  775. */
  776. static void rio_enum_timeout(unsigned long data)
  777. {
  778. /* Enumeration timed out, set flag */
  779. *(int *)data = 1;
  780. }
  781. /**
  782. * rio_disc_mport- Start discovery through a master port
  783. * @mport: Master port to send transactions
  784. *
  785. * Starts the discovery process. If we have an active link,
  786. * then wait for the signal that enumeration is complete.
  787. * When enumeration completion is signaled, start recursive
  788. * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
  789. * on failure.
  790. */
  791. int rio_disc_mport(struct rio_mport *mport)
  792. {
  793. struct rio_net *net = NULL;
  794. int enum_timeout_flag = 0;
  795. printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
  796. mport->name);
  797. /* If master port has an active link, allocate net and discover peers */
  798. if (rio_mport_is_active(mport)) {
  799. if (!(net = rio_alloc_net(mport))) {
  800. printk(KERN_ERR "RIO: Failed to allocate new net\n");
  801. goto bail;
  802. }
  803. pr_debug("RIO: wait for enumeration complete...");
  804. rio_enum_timer.expires =
  805. jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
  806. rio_enum_timer.data = (unsigned long)&enum_timeout_flag;
  807. add_timer(&rio_enum_timer);
  808. while (!rio_enum_complete(mport)) {
  809. mdelay(1);
  810. if (enum_timeout_flag) {
  811. del_timer_sync(&rio_enum_timer);
  812. goto timeout;
  813. }
  814. }
  815. del_timer_sync(&rio_enum_timer);
  816. pr_debug("done\n");
  817. if (rio_disc_peer(net, mport, RIO_ANY_DESTID, 0) < 0) {
  818. printk(KERN_INFO
  819. "RIO: master port %d device has failed discovery\n",
  820. mport->id);
  821. goto bail;
  822. }
  823. rio_build_route_tables();
  824. }
  825. return 0;
  826. timeout:
  827. pr_debug("timeout\n");
  828. bail:
  829. return -EBUSY;
  830. }