rio-scan.c 31 KB

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