rio-scan.c 31 KB

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