rio-scan.c 26 KB

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