rio-scan.c 28 KB

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