rio-scan.c 28 KB

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