rio-scan.c 26 KB

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