rio-scan.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /*
  2. * RapidIO enumeration and discovery support
  3. *
  4. * Copyright 2005 MontaVista Software, Inc.
  5. * Matt Porter <mporter@kernel.crashing.org>
  6. *
  7. * Copyright 2009 Integrated Device Technology, Inc.
  8. * Alex Bounine <alexandre.bounine@idt.com>
  9. * - Added Port-Write/Error Management initialization and handling
  10. *
  11. * Copyright 2009 Sysgo AG
  12. * Thomas Moll <thomas.moll@sysgo.com>
  13. * - Added Input- Output- enable functionality, to allow full communication
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. */
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/init.h>
  25. #include <linux/rio.h>
  26. #include <linux/rio_drv.h>
  27. #include <linux/rio_ids.h>
  28. #include <linux/rio_regs.h>
  29. #include <linux/module.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/timer.h>
  32. #include <linux/sched.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/slab.h>
  35. #include "rio.h"
  36. static void rio_init_em(struct rio_dev *rdev);
  37. static int next_destid = 0;
  38. static int next_comptag = 1;
  39. static int rio_mport_phys_table[] = {
  40. RIO_EFB_PAR_EP_ID,
  41. RIO_EFB_PAR_EP_REC_ID,
  42. RIO_EFB_SER_EP_ID,
  43. RIO_EFB_SER_EP_REC_ID,
  44. -1,
  45. };
  46. /**
  47. * rio_destid_alloc - Allocate next available destID for given network
  48. * @net: RIO network
  49. *
  50. * Returns next available device destination ID for the specified RIO network.
  51. * Marks allocated ID as one in use.
  52. * Returns RIO_INVALID_DESTID if new destID is not available.
  53. */
  54. static u16 rio_destid_alloc(struct rio_net *net)
  55. {
  56. int destid;
  57. struct rio_id_table *idtab = &net->destid_table;
  58. spin_lock(&idtab->lock);
  59. destid = find_first_zero_bit(idtab->table, idtab->max);
  60. if (destid < idtab->max) {
  61. set_bit(destid, idtab->table);
  62. destid += idtab->start;
  63. } else
  64. destid = RIO_INVALID_DESTID;
  65. spin_unlock(&idtab->lock);
  66. return (u16)destid;
  67. }
  68. /**
  69. * rio_destid_reserve - Reserve the specivied destID
  70. * @net: RIO network
  71. * @destid: destID to reserve
  72. *
  73. * Tries to reserve the specified destID.
  74. * Returns 0 if successful.
  75. */
  76. static int rio_destid_reserve(struct rio_net *net, u16 destid)
  77. {
  78. int oldbit;
  79. struct rio_id_table *idtab = &net->destid_table;
  80. destid -= idtab->start;
  81. spin_lock(&idtab->lock);
  82. oldbit = test_and_set_bit(destid, idtab->table);
  83. spin_unlock(&idtab->lock);
  84. return oldbit;
  85. }
  86. /**
  87. * rio_destid_free - free a previously allocated destID
  88. * @net: RIO network
  89. * @destid: destID to free
  90. *
  91. * Makes the specified destID available for use.
  92. */
  93. static void rio_destid_free(struct rio_net *net, u16 destid)
  94. {
  95. struct rio_id_table *idtab = &net->destid_table;
  96. destid -= idtab->start;
  97. spin_lock(&idtab->lock);
  98. clear_bit(destid, idtab->table);
  99. spin_unlock(&idtab->lock);
  100. }
  101. /**
  102. * rio_destid_first - return first destID in use
  103. * @net: RIO network
  104. */
  105. static u16 rio_destid_first(struct rio_net *net)
  106. {
  107. int destid;
  108. struct rio_id_table *idtab = &net->destid_table;
  109. spin_lock(&idtab->lock);
  110. destid = find_first_bit(idtab->table, idtab->max);
  111. if (destid >= idtab->max)
  112. destid = RIO_INVALID_DESTID;
  113. else
  114. destid += idtab->start;
  115. spin_unlock(&idtab->lock);
  116. return (u16)destid;
  117. }
  118. /**
  119. * rio_destid_next - return next destID in use
  120. * @net: RIO network
  121. * @from: destination ID from which search shall continue
  122. */
  123. static u16 rio_destid_next(struct rio_net *net, u16 from)
  124. {
  125. int destid;
  126. struct rio_id_table *idtab = &net->destid_table;
  127. spin_lock(&idtab->lock);
  128. destid = find_next_bit(idtab->table, idtab->max, from);
  129. if (destid >= idtab->max)
  130. destid = RIO_INVALID_DESTID;
  131. else
  132. destid += idtab->start;
  133. spin_unlock(&idtab->lock);
  134. return (u16)destid;
  135. }
  136. /**
  137. * rio_get_device_id - Get the base/extended device id for a device
  138. * @port: RIO master port
  139. * @destid: Destination ID of device
  140. * @hopcount: Hopcount to device
  141. *
  142. * Reads the base/extended device id from a device. Returns the
  143. * 8/16-bit device ID.
  144. */
  145. static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
  146. {
  147. u32 result;
  148. rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
  149. return RIO_GET_DID(port->sys_size, result);
  150. }
  151. /**
  152. * rio_set_device_id - Set the base/extended device id for a device
  153. * @port: RIO master port
  154. * @destid: Destination ID of device
  155. * @hopcount: Hopcount to device
  156. * @did: Device ID value to be written
  157. *
  158. * Writes the base/extended device id from a device.
  159. */
  160. static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
  161. {
  162. rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
  163. RIO_SET_DID(port->sys_size, did));
  164. }
  165. /**
  166. * rio_local_set_device_id - Set the base/extended device id for a port
  167. * @port: RIO master port
  168. * @did: Device ID value to be written
  169. *
  170. * Writes the base/extended device id from a device.
  171. */
  172. static void rio_local_set_device_id(struct rio_mport *port, u16 did)
  173. {
  174. rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
  175. did));
  176. }
  177. /**
  178. * rio_clear_locks- Release all host locks and signal enumeration complete
  179. * @net: RIO network to run on
  180. *
  181. * Marks the component tag CSR on each device with the enumeration
  182. * complete flag. When complete, it then release the host locks on
  183. * each device. Returns 0 on success or %-EINVAL on failure.
  184. */
  185. static int rio_clear_locks(struct rio_net *net)
  186. {
  187. struct rio_mport *port = net->hport;
  188. struct rio_dev *rdev;
  189. u32 result;
  190. int ret = 0;
  191. /* Release host device id locks */
  192. rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
  193. port->host_deviceid);
  194. rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
  195. if ((result & 0xffff) != 0xffff) {
  196. printk(KERN_INFO
  197. "RIO: badness when releasing host lock on master port, result %8.8x\n",
  198. result);
  199. ret = -EINVAL;
  200. }
  201. list_for_each_entry(rdev, &net->devices, net_list) {
  202. rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
  203. port->host_deviceid);
  204. rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
  205. if ((result & 0xffff) != 0xffff) {
  206. printk(KERN_INFO
  207. "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
  208. rdev->vid, rdev->did);
  209. ret = -EINVAL;
  210. }
  211. /* Mark device as discovered and enable master */
  212. rio_read_config_32(rdev,
  213. rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  214. &result);
  215. result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
  216. rio_write_config_32(rdev,
  217. rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  218. result);
  219. }
  220. return ret;
  221. }
  222. /**
  223. * rio_enum_host- Set host lock and initialize host destination ID
  224. * @port: Master port to issue transaction
  225. *
  226. * Sets the local host master port lock and destination ID register
  227. * with the host device ID value. The host device ID value is provided
  228. * by the platform. Returns %0 on success or %-1 on failure.
  229. */
  230. static int rio_enum_host(struct rio_mport *port)
  231. {
  232. u32 result;
  233. /* Set master port host device id lock */
  234. rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
  235. port->host_deviceid);
  236. rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
  237. if ((result & 0xffff) != port->host_deviceid)
  238. return -1;
  239. /* Set master port destid and init destid ctr */
  240. rio_local_set_device_id(port, port->host_deviceid);
  241. return 0;
  242. }
  243. /**
  244. * rio_device_has_destid- Test if a device contains a destination ID register
  245. * @port: Master port to issue transaction
  246. * @src_ops: RIO device source operations
  247. * @dst_ops: RIO device destination operations
  248. *
  249. * Checks the provided @src_ops and @dst_ops for the necessary transaction
  250. * capabilities that indicate whether or not a device will implement a
  251. * destination ID register. Returns 1 if true or 0 if false.
  252. */
  253. static int rio_device_has_destid(struct rio_mport *port, int src_ops,
  254. int dst_ops)
  255. {
  256. 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;
  257. return !!((src_ops | dst_ops) & mask);
  258. }
  259. /**
  260. * rio_release_dev- Frees a RIO device struct
  261. * @dev: LDM device associated with a RIO device struct
  262. *
  263. * Gets the RIO device struct associated a RIO device struct.
  264. * The RIO device struct is freed.
  265. */
  266. static void rio_release_dev(struct device *dev)
  267. {
  268. struct rio_dev *rdev;
  269. rdev = to_rio_dev(dev);
  270. kfree(rdev);
  271. }
  272. /**
  273. * rio_is_switch- Tests if a RIO device has switch capabilities
  274. * @rdev: RIO device
  275. *
  276. * Gets the RIO device Processing Element Features register
  277. * contents and tests for switch capabilities. Returns 1 if
  278. * the device is a switch or 0 if it is not a switch.
  279. * The RIO device struct is freed.
  280. */
  281. static int rio_is_switch(struct rio_dev *rdev)
  282. {
  283. if (rdev->pef & RIO_PEF_SWITCH)
  284. return 1;
  285. return 0;
  286. }
  287. /**
  288. * rio_setup_device- Allocates and sets up a RIO device
  289. * @net: RIO network
  290. * @port: Master port to send transactions
  291. * @destid: Current destination ID
  292. * @hopcount: Current hopcount
  293. * @do_enum: Enumeration/Discovery mode flag
  294. *
  295. * Allocates a RIO device and configures fields based on configuration
  296. * space contents. If device has a destination ID register, a destination
  297. * ID is either assigned in enumeration mode or read from configuration
  298. * space in discovery mode. If the device has switch capabilities, then
  299. * a switch is allocated and configured appropriately. Returns a pointer
  300. * to a RIO device on success or NULL on failure.
  301. *
  302. */
  303. static struct rio_dev *rio_setup_device(struct rio_net *net,
  304. struct rio_mport *port, u16 destid,
  305. u8 hopcount, int do_enum)
  306. {
  307. int ret = 0;
  308. struct rio_dev *rdev;
  309. struct rio_switch *rswitch = NULL;
  310. int result, rdid;
  311. size_t size;
  312. u32 swpinfo = 0;
  313. size = sizeof(struct rio_dev);
  314. if (rio_mport_read_config_32(port, destid, hopcount,
  315. RIO_PEF_CAR, &result))
  316. return NULL;
  317. if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
  318. rio_mport_read_config_32(port, destid, hopcount,
  319. RIO_SWP_INFO_CAR, &swpinfo);
  320. if (result & RIO_PEF_SWITCH) {
  321. size += (RIO_GET_TOTAL_PORTS(swpinfo) *
  322. sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
  323. }
  324. }
  325. rdev = kzalloc(size, GFP_KERNEL);
  326. if (!rdev)
  327. return NULL;
  328. rdev->net = net;
  329. rdev->pef = result;
  330. rdev->swpinfo = swpinfo;
  331. rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
  332. &result);
  333. rdev->did = result >> 16;
  334. rdev->vid = result & 0xffff;
  335. rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
  336. &rdev->device_rev);
  337. rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
  338. &result);
  339. rdev->asm_did = result >> 16;
  340. rdev->asm_vid = result & 0xffff;
  341. rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
  342. &result);
  343. rdev->asm_rev = result >> 16;
  344. if (rdev->pef & RIO_PEF_EXT_FEATURES) {
  345. rdev->efptr = result & 0xffff;
  346. rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
  347. hopcount);
  348. rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
  349. hopcount, RIO_EFB_ERR_MGMNT);
  350. }
  351. rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
  352. &rdev->src_ops);
  353. rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
  354. &rdev->dst_ops);
  355. if (do_enum) {
  356. /* Assign component tag to device */
  357. if (next_comptag >= 0x10000) {
  358. pr_err("RIO: Component Tag Counter Overflow\n");
  359. goto cleanup;
  360. }
  361. rio_mport_write_config_32(port, destid, hopcount,
  362. RIO_COMPONENT_TAG_CSR, next_comptag);
  363. rdev->comp_tag = next_comptag++;
  364. rdev->do_enum = true;
  365. } else {
  366. rio_mport_read_config_32(port, destid, hopcount,
  367. RIO_COMPONENT_TAG_CSR,
  368. &rdev->comp_tag);
  369. }
  370. if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
  371. if (do_enum) {
  372. rio_set_device_id(port, destid, hopcount, next_destid);
  373. rdev->destid = next_destid;
  374. next_destid = rio_destid_alloc(net);
  375. } else
  376. rdev->destid = rio_get_device_id(port, destid, hopcount);
  377. rdev->hopcount = 0xff;
  378. } else {
  379. /* Switch device has an associated destID which
  380. * will be adjusted later
  381. */
  382. rdev->destid = destid;
  383. rdev->hopcount = hopcount;
  384. }
  385. /* If a PE has both switch and other functions, show it as a switch */
  386. if (rio_is_switch(rdev)) {
  387. rswitch = rdev->rswitch;
  388. rswitch->port_ok = 0;
  389. spin_lock_init(&rswitch->lock);
  390. rswitch->route_table = kzalloc(sizeof(u8)*
  391. RIO_MAX_ROUTE_ENTRIES(port->sys_size),
  392. GFP_KERNEL);
  393. if (!rswitch->route_table)
  394. goto cleanup;
  395. /* Initialize switch route table */
  396. for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
  397. rdid++)
  398. rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
  399. dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
  400. rdev->comp_tag & RIO_CTAG_UDEVID);
  401. if (do_enum)
  402. rio_route_clr_table(rdev, RIO_GLOBAL_TABLE, 0);
  403. list_add_tail(&rswitch->node, &net->switches);
  404. } else {
  405. if (do_enum)
  406. /*Enable Input Output Port (transmitter reviever)*/
  407. rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
  408. dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
  409. rdev->comp_tag & RIO_CTAG_UDEVID);
  410. }
  411. rdev->dev.parent = &port->dev;
  412. rio_attach_device(rdev);
  413. device_initialize(&rdev->dev);
  414. rdev->dev.release = rio_release_dev;
  415. rio_dev_get(rdev);
  416. rdev->dma_mask = DMA_BIT_MASK(32);
  417. rdev->dev.dma_mask = &rdev->dma_mask;
  418. rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  419. if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
  420. rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
  421. 0, 0xffff);
  422. ret = rio_add_device(rdev);
  423. if (ret)
  424. goto cleanup;
  425. return rdev;
  426. cleanup:
  427. if (rswitch)
  428. kfree(rswitch->route_table);
  429. kfree(rdev);
  430. return NULL;
  431. }
  432. /**
  433. * rio_sport_is_active- Tests if a switch port has an active connection.
  434. * @port: Master port to send transaction
  435. * @destid: Associated destination ID for switch
  436. * @hopcount: Hopcount to reach switch
  437. * @sport: Switch port number
  438. *
  439. * Reads the port error status CSR for a particular switch port to
  440. * determine if the port has an active link. Returns
  441. * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
  442. * inactive.
  443. */
  444. static int
  445. rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
  446. {
  447. u32 result = 0;
  448. u32 ext_ftr_ptr;
  449. ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
  450. while (ext_ftr_ptr) {
  451. rio_mport_read_config_32(port, destid, hopcount,
  452. ext_ftr_ptr, &result);
  453. result = RIO_GET_BLOCK_ID(result);
  454. if ((result == RIO_EFB_SER_EP_FREE_ID) ||
  455. (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
  456. (result == RIO_EFB_SER_EP_FREC_ID))
  457. break;
  458. ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
  459. ext_ftr_ptr);
  460. }
  461. if (ext_ftr_ptr)
  462. rio_mport_read_config_32(port, destid, hopcount,
  463. ext_ftr_ptr +
  464. RIO_PORT_N_ERR_STS_CSR(sport),
  465. &result);
  466. return result & RIO_PORT_N_ERR_STS_PORT_OK;
  467. }
  468. /**
  469. * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
  470. * @port: Master port to send transaction
  471. * @hopcount: Number of hops to the device
  472. *
  473. * Used during enumeration to read the Host Device ID Lock CSR on a
  474. * RIO device. Returns the value of the lock register.
  475. */
  476. static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
  477. {
  478. u32 result;
  479. rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
  480. RIO_HOST_DID_LOCK_CSR, &result);
  481. return (u16) (result & 0xffff);
  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. * @prev: Previous RIO device connected to the enumerated one
  489. * @prev_port: Port on previous RIO device
  490. *
  491. * Recursively enumerates a RIO network. Transactions are sent via the
  492. * master port passed in @port.
  493. */
  494. static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
  495. u8 hopcount, struct rio_dev *prev, int prev_port)
  496. {
  497. struct rio_dev *rdev;
  498. u32 regval;
  499. int tmp;
  500. if (rio_mport_chk_dev_access(port,
  501. RIO_ANY_DESTID(port->sys_size), hopcount)) {
  502. pr_debug("RIO: device access check failed\n");
  503. return -1;
  504. }
  505. if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
  506. pr_debug("RIO: PE already discovered by this host\n");
  507. /*
  508. * Already discovered by this host. Add it as another
  509. * link to the existing device.
  510. */
  511. rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
  512. hopcount, RIO_COMPONENT_TAG_CSR, &regval);
  513. if (regval) {
  514. rdev = rio_get_comptag((regval & 0xffff), NULL);
  515. if (rdev && prev && rio_is_switch(prev)) {
  516. pr_debug("RIO: redundant path to %s\n",
  517. rio_name(rdev));
  518. prev->rswitch->nextdev[prev_port] = rdev;
  519. }
  520. }
  521. return 0;
  522. }
  523. /* Attempt to acquire device lock */
  524. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  525. hopcount,
  526. RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  527. while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
  528. < port->host_deviceid) {
  529. /* Delay a bit */
  530. mdelay(1);
  531. /* Attempt to acquire device lock again */
  532. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  533. hopcount,
  534. RIO_HOST_DID_LOCK_CSR,
  535. port->host_deviceid);
  536. }
  537. if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
  538. pr_debug(
  539. "RIO: PE locked by a higher priority host...retreating\n");
  540. return -1;
  541. }
  542. /* Setup new RIO device */
  543. rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
  544. hopcount, 1);
  545. if (rdev) {
  546. /* Add device to the global and bus/net specific list. */
  547. list_add_tail(&rdev->net_list, &net->devices);
  548. rdev->prev = prev;
  549. if (prev && rio_is_switch(prev))
  550. prev->rswitch->nextdev[prev_port] = rdev;
  551. } else
  552. return -1;
  553. if (rio_is_switch(rdev)) {
  554. int sw_destid;
  555. int cur_destid;
  556. int sw_inport;
  557. u16 destid;
  558. int port_num;
  559. sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
  560. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  561. port->host_deviceid, sw_inport, 0);
  562. rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
  563. destid = rio_destid_first(net);
  564. while (destid != RIO_INVALID_DESTID && destid < next_destid) {
  565. if (destid != port->host_deviceid) {
  566. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  567. destid, sw_inport, 0);
  568. rdev->rswitch->route_table[destid] = sw_inport;
  569. }
  570. destid = rio_destid_next(net, destid + 1);
  571. }
  572. pr_debug(
  573. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  574. rio_name(rdev), rdev->vid, rdev->did,
  575. RIO_GET_TOTAL_PORTS(rdev->swpinfo));
  576. sw_destid = next_destid;
  577. for (port_num = 0;
  578. port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  579. port_num++) {
  580. if (sw_inport == port_num) {
  581. rio_enable_rx_tx_port(port, 0,
  582. RIO_ANY_DESTID(port->sys_size),
  583. hopcount, port_num);
  584. rdev->rswitch->port_ok |= (1 << port_num);
  585. continue;
  586. }
  587. cur_destid = next_destid;
  588. if (rio_sport_is_active
  589. (port, RIO_ANY_DESTID(port->sys_size), hopcount,
  590. port_num)) {
  591. pr_debug(
  592. "RIO: scanning device on port %d\n",
  593. port_num);
  594. rio_enable_rx_tx_port(port, 0,
  595. RIO_ANY_DESTID(port->sys_size),
  596. hopcount, port_num);
  597. rdev->rswitch->port_ok |= (1 << port_num);
  598. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  599. RIO_ANY_DESTID(port->sys_size),
  600. port_num, 0);
  601. if (rio_enum_peer(net, port, hopcount + 1,
  602. rdev, port_num) < 0)
  603. return -1;
  604. /* Update routing tables */
  605. destid = rio_destid_next(net, cur_destid + 1);
  606. if (destid != RIO_INVALID_DESTID) {
  607. for (destid = cur_destid;
  608. destid < next_destid;) {
  609. if (destid != port->host_deviceid) {
  610. rio_route_add_entry(rdev,
  611. RIO_GLOBAL_TABLE,
  612. destid,
  613. port_num,
  614. 0);
  615. rdev->rswitch->
  616. route_table[destid] =
  617. port_num;
  618. }
  619. destid = rio_destid_next(net,
  620. destid + 1);
  621. }
  622. }
  623. } else {
  624. /* If switch supports Error Management,
  625. * set PORT_LOCKOUT bit for unused port
  626. */
  627. if (rdev->em_efptr)
  628. rio_set_port_lockout(rdev, port_num, 1);
  629. rdev->rswitch->port_ok &= ~(1 << port_num);
  630. }
  631. }
  632. /* Direct Port-write messages to the enumeratiing host */
  633. if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
  634. (rdev->em_efptr)) {
  635. rio_write_config_32(rdev,
  636. rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
  637. (port->host_deviceid << 16) |
  638. (port->sys_size << 15));
  639. }
  640. rio_init_em(rdev);
  641. /* Check for empty switch */
  642. if (next_destid == sw_destid)
  643. next_destid = rio_destid_alloc(net);
  644. rdev->destid = sw_destid;
  645. } else
  646. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  647. rio_name(rdev), rdev->vid, rdev->did);
  648. return 0;
  649. }
  650. /**
  651. * rio_enum_complete- Tests if enumeration of a network is complete
  652. * @port: Master port to send transaction
  653. *
  654. * Tests the PGCCSR discovered bit for non-zero value (enumeration
  655. * complete flag). Return %1 if enumeration is complete or %0 if
  656. * enumeration is incomplete.
  657. */
  658. static int rio_enum_complete(struct rio_mport *port)
  659. {
  660. u32 regval;
  661. rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  662. &regval);
  663. return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
  664. }
  665. /**
  666. * rio_disc_peer- Recursively discovers a RIO network through a master port
  667. * @net: RIO network being discovered
  668. * @port: Master port to send transactions
  669. * @destid: Current destination ID in network
  670. * @hopcount: Number of hops into the network
  671. * @prev: previous rio_dev
  672. * @prev_port: previous port number
  673. *
  674. * Recursively discovers a RIO network. Transactions are sent via the
  675. * master port passed in @port.
  676. */
  677. static int
  678. rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
  679. u8 hopcount, struct rio_dev *prev, int prev_port)
  680. {
  681. u8 port_num, route_port;
  682. struct rio_dev *rdev;
  683. u16 ndestid;
  684. /* Setup new RIO device */
  685. if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
  686. /* Add device to the global and bus/net specific list. */
  687. list_add_tail(&rdev->net_list, &net->devices);
  688. rdev->prev = prev;
  689. if (prev && rio_is_switch(prev))
  690. prev->rswitch->nextdev[prev_port] = rdev;
  691. } else
  692. return -1;
  693. if (rio_is_switch(rdev)) {
  694. /* Associated destid is how we accessed this switch */
  695. rdev->destid = destid;
  696. pr_debug(
  697. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  698. rio_name(rdev), rdev->vid, rdev->did,
  699. RIO_GET_TOTAL_PORTS(rdev->swpinfo));
  700. for (port_num = 0;
  701. port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  702. port_num++) {
  703. if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
  704. continue;
  705. if (rio_sport_is_active
  706. (port, destid, hopcount, port_num)) {
  707. pr_debug(
  708. "RIO: scanning device on port %d\n",
  709. port_num);
  710. rio_lock_device(port, destid, hopcount, 1000);
  711. for (ndestid = 0;
  712. ndestid < RIO_ANY_DESTID(port->sys_size);
  713. ndestid++) {
  714. rio_route_get_entry(rdev,
  715. RIO_GLOBAL_TABLE,
  716. ndestid,
  717. &route_port, 0);
  718. if (route_port == port_num)
  719. break;
  720. }
  721. if (ndestid == RIO_ANY_DESTID(port->sys_size))
  722. continue;
  723. rio_unlock_device(port, destid, hopcount);
  724. if (rio_disc_peer(net, port, ndestid,
  725. hopcount + 1, rdev, port_num) < 0)
  726. return -1;
  727. }
  728. }
  729. } else
  730. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  731. rio_name(rdev), rdev->vid, rdev->did);
  732. return 0;
  733. }
  734. /**
  735. * rio_mport_is_active- Tests if master port link is active
  736. * @port: Master port to test
  737. *
  738. * Reads the port error status CSR for the master port to
  739. * determine if the port has an active link. Returns
  740. * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
  741. * or %0 if it is inactive.
  742. */
  743. static int rio_mport_is_active(struct rio_mport *port)
  744. {
  745. u32 result = 0;
  746. u32 ext_ftr_ptr;
  747. int *entry = rio_mport_phys_table;
  748. do {
  749. if ((ext_ftr_ptr =
  750. rio_mport_get_feature(port, 1, 0, 0, *entry)))
  751. break;
  752. } while (*++entry >= 0);
  753. if (ext_ftr_ptr)
  754. rio_local_read_config_32(port,
  755. ext_ftr_ptr +
  756. RIO_PORT_N_ERR_STS_CSR(port->index),
  757. &result);
  758. return result & RIO_PORT_N_ERR_STS_PORT_OK;
  759. }
  760. /**
  761. * rio_alloc_net- Allocate and configure a new RIO network
  762. * @port: Master port associated with the RIO network
  763. * @do_enum: Enumeration/Discovery mode flag
  764. * @start: logical minimal start id for new net
  765. *
  766. * Allocates a RIO network structure, initializes per-network
  767. * list heads, and adds the associated master port to the
  768. * network list of associated master ports. Returns a
  769. * RIO network pointer on success or %NULL on failure.
  770. */
  771. static struct rio_net *rio_alloc_net(struct rio_mport *port,
  772. int do_enum, u16 start)
  773. {
  774. struct rio_net *net;
  775. net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
  776. if (net && do_enum) {
  777. net->destid_table.table = kcalloc(
  778. BITS_TO_LONGS(RIO_MAX_ROUTE_ENTRIES(port->sys_size)),
  779. sizeof(long),
  780. GFP_KERNEL);
  781. if (net->destid_table.table == NULL) {
  782. pr_err("RIO: failed to allocate destID table\n");
  783. kfree(net);
  784. net = NULL;
  785. } else {
  786. net->destid_table.start = start;
  787. net->destid_table.max =
  788. RIO_MAX_ROUTE_ENTRIES(port->sys_size);
  789. spin_lock_init(&net->destid_table.lock);
  790. }
  791. }
  792. if (net) {
  793. INIT_LIST_HEAD(&net->node);
  794. INIT_LIST_HEAD(&net->devices);
  795. INIT_LIST_HEAD(&net->switches);
  796. INIT_LIST_HEAD(&net->mports);
  797. list_add_tail(&port->nnode, &net->mports);
  798. net->hport = port;
  799. net->id = port->id;
  800. }
  801. return net;
  802. }
  803. /**
  804. * rio_update_route_tables- Updates route tables in switches
  805. * @net: RIO network to run update on
  806. *
  807. * For each enumerated device, ensure that each switch in a system
  808. * has correct routing entries. Add routes for devices that where
  809. * unknown dirung the first enumeration pass through the switch.
  810. */
  811. static void rio_update_route_tables(struct rio_net *net)
  812. {
  813. struct rio_dev *rdev, *swrdev;
  814. struct rio_switch *rswitch;
  815. u8 sport;
  816. u16 destid;
  817. list_for_each_entry(rdev, &net->devices, net_list) {
  818. destid = rdev->destid;
  819. list_for_each_entry(rswitch, &net->switches, node) {
  820. if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
  821. continue;
  822. if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
  823. swrdev = sw_to_rio_dev(rswitch);
  824. /* Skip if destid ends in empty switch*/
  825. if (swrdev->destid == destid)
  826. continue;
  827. sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
  828. rio_route_add_entry(swrdev, RIO_GLOBAL_TABLE,
  829. destid, sport, 0);
  830. rswitch->route_table[destid] = sport;
  831. }
  832. }
  833. }
  834. }
  835. /**
  836. * rio_init_em - Initializes RIO Error Management (for switches)
  837. * @rdev: RIO device
  838. *
  839. * For each enumerated switch, call device-specific error management
  840. * initialization routine (if supplied by the switch driver).
  841. */
  842. static void rio_init_em(struct rio_dev *rdev)
  843. {
  844. if (rio_is_switch(rdev) && (rdev->em_efptr) &&
  845. rdev->rswitch->ops && rdev->rswitch->ops->em_init) {
  846. rdev->rswitch->ops->em_init(rdev);
  847. }
  848. }
  849. /**
  850. * rio_pw_enable - Enables/disables port-write handling by a master port
  851. * @port: Master port associated with port-write handling
  852. * @enable: 1=enable, 0=disable
  853. */
  854. static void rio_pw_enable(struct rio_mport *port, int enable)
  855. {
  856. if (port->ops->pwenable)
  857. port->ops->pwenable(port, enable);
  858. }
  859. /**
  860. * rio_enum_mport- Start enumeration through a master port
  861. * @mport: Master port to send transactions
  862. * @flags: Enumeration control flags
  863. *
  864. * Starts the enumeration process. If somebody has enumerated our
  865. * master port device, then give up. If not and we have an active
  866. * link, then start recursive peer enumeration. Returns %0 if
  867. * enumeration succeeds or %-EBUSY if enumeration fails.
  868. */
  869. static int rio_enum_mport(struct rio_mport *mport, u32 flags)
  870. {
  871. struct rio_net *net = NULL;
  872. int rc = 0;
  873. printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
  874. mport->name);
  875. /*
  876. * To avoid multiple start requests (repeat enumeration is not supported
  877. * by this method) check if enumeration/discovery was performed for this
  878. * mport: if mport was added into the list of mports for a net exit
  879. * with error.
  880. */
  881. if (mport->nnode.next || mport->nnode.prev)
  882. return -EBUSY;
  883. /* If somebody else enumerated our master port device, bail. */
  884. if (rio_enum_host(mport) < 0) {
  885. printk(KERN_INFO
  886. "RIO: master port %d device has been enumerated by a remote host\n",
  887. mport->id);
  888. rc = -EBUSY;
  889. goto out;
  890. }
  891. /* If master port has an active link, allocate net and enum peers */
  892. if (rio_mport_is_active(mport)) {
  893. net = rio_alloc_net(mport, 1, 0);
  894. if (!net) {
  895. printk(KERN_ERR "RIO: failed to allocate new net\n");
  896. rc = -ENOMEM;
  897. goto out;
  898. }
  899. /* reserve mport destID in new net */
  900. rio_destid_reserve(net, mport->host_deviceid);
  901. /* Enable Input Output Port (transmitter reviever) */
  902. rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
  903. /* Set component tag for host */
  904. rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
  905. next_comptag++);
  906. next_destid = rio_destid_alloc(net);
  907. if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
  908. /* A higher priority host won enumeration, bail. */
  909. printk(KERN_INFO
  910. "RIO: master port %d device has lost enumeration to a remote host\n",
  911. mport->id);
  912. rio_clear_locks(net);
  913. rc = -EBUSY;
  914. goto out;
  915. }
  916. /* free the last allocated destID (unused) */
  917. rio_destid_free(net, next_destid);
  918. rio_update_route_tables(net);
  919. rio_clear_locks(net);
  920. rio_pw_enable(mport, 1);
  921. } else {
  922. printk(KERN_INFO "RIO: master port %d link inactive\n",
  923. mport->id);
  924. rc = -EINVAL;
  925. }
  926. out:
  927. return rc;
  928. }
  929. /**
  930. * rio_build_route_tables- Generate route tables from switch route entries
  931. * @net: RIO network to run route tables scan on
  932. *
  933. * For each switch device, generate a route table by copying existing
  934. * route entries from the switch.
  935. */
  936. static void rio_build_route_tables(struct rio_net *net)
  937. {
  938. struct rio_switch *rswitch;
  939. struct rio_dev *rdev;
  940. int i;
  941. u8 sport;
  942. list_for_each_entry(rswitch, &net->switches, node) {
  943. rdev = sw_to_rio_dev(rswitch);
  944. rio_lock_device(net->hport, rdev->destid,
  945. rdev->hopcount, 1000);
  946. for (i = 0;
  947. i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
  948. i++) {
  949. if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
  950. i, &sport, 0) < 0)
  951. continue;
  952. rswitch->route_table[i] = sport;
  953. }
  954. rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
  955. }
  956. }
  957. /**
  958. * rio_disc_mport- Start discovery through a master port
  959. * @mport: Master port to send transactions
  960. * @flags: discovery control flags
  961. *
  962. * Starts the discovery process. If we have an active link,
  963. * then wait for the signal that enumeration is complete (if wait
  964. * is allowed).
  965. * When enumeration completion is signaled, start recursive
  966. * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
  967. * on failure.
  968. */
  969. static int rio_disc_mport(struct rio_mport *mport, u32 flags)
  970. {
  971. struct rio_net *net = NULL;
  972. unsigned long to_end;
  973. printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
  974. mport->name);
  975. /* If master port has an active link, allocate net and discover peers */
  976. if (rio_mport_is_active(mport)) {
  977. if (rio_enum_complete(mport))
  978. goto enum_done;
  979. else if (flags & RIO_SCAN_ENUM_NO_WAIT)
  980. return -EAGAIN;
  981. pr_debug("RIO: wait for enumeration to complete...\n");
  982. to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
  983. while (time_before(jiffies, to_end)) {
  984. if (rio_enum_complete(mport))
  985. goto enum_done;
  986. msleep(10);
  987. }
  988. pr_debug("RIO: discovery timeout on mport %d %s\n",
  989. mport->id, mport->name);
  990. goto bail;
  991. enum_done:
  992. pr_debug("RIO: ... enumeration done\n");
  993. net = rio_alloc_net(mport, 0, 0);
  994. if (!net) {
  995. printk(KERN_ERR "RIO: Failed to allocate new net\n");
  996. goto bail;
  997. }
  998. /* Read DestID assigned by enumerator */
  999. rio_local_read_config_32(mport, RIO_DID_CSR,
  1000. &mport->host_deviceid);
  1001. mport->host_deviceid = RIO_GET_DID(mport->sys_size,
  1002. mport->host_deviceid);
  1003. if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
  1004. 0, NULL, 0) < 0) {
  1005. printk(KERN_INFO
  1006. "RIO: master port %d device has failed discovery\n",
  1007. mport->id);
  1008. goto bail;
  1009. }
  1010. rio_build_route_tables(net);
  1011. }
  1012. return 0;
  1013. bail:
  1014. return -EBUSY;
  1015. }
  1016. static struct rio_scan rio_scan_ops = {
  1017. .owner = THIS_MODULE,
  1018. .enumerate = rio_enum_mport,
  1019. .discover = rio_disc_mport,
  1020. };
  1021. static bool scan;
  1022. module_param(scan, bool, 0);
  1023. MODULE_PARM_DESC(scan, "Start RapidIO network enumeration/discovery "
  1024. "(default = 0)");
  1025. /**
  1026. * rio_basic_attach:
  1027. *
  1028. * When this enumeration/discovery method is loaded as a module this function
  1029. * registers its specific enumeration and discover routines for all available
  1030. * RapidIO mport devices. The "scan" command line parameter controls ability of
  1031. * the module to start RapidIO enumeration/discovery automatically.
  1032. *
  1033. * Returns 0 for success or -EIO if unable to register itself.
  1034. *
  1035. * This enumeration/discovery method cannot be unloaded and therefore does not
  1036. * provide a matching cleanup_module routine.
  1037. */
  1038. static int __init rio_basic_attach(void)
  1039. {
  1040. if (rio_register_scan(RIO_MPORT_ANY, &rio_scan_ops))
  1041. return -EIO;
  1042. if (scan)
  1043. rio_init_mports();
  1044. return 0;
  1045. }
  1046. late_initcall(rio_basic_attach);
  1047. MODULE_DESCRIPTION("Basic RapidIO enumeration/discovery");
  1048. MODULE_LICENSE("GPL");