rio-scan.c 28 KB

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