rio-scan.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. LIST_HEAD(rio_devices);
  37. static void rio_init_em(struct rio_dev *rdev);
  38. DEFINE_SPINLOCK(rio_global_list_lock);
  39. static int next_destid = 0;
  40. static int next_comptag = 1;
  41. static int rio_mport_phys_table[] = {
  42. RIO_EFB_PAR_EP_ID,
  43. RIO_EFB_PAR_EP_REC_ID,
  44. RIO_EFB_SER_EP_ID,
  45. RIO_EFB_SER_EP_REC_ID,
  46. -1,
  47. };
  48. /**
  49. * rio_destid_alloc - Allocate next available destID for given network
  50. * @net: RIO network
  51. *
  52. * Returns next available device destination ID for the specified RIO network.
  53. * Marks allocated ID as one in use.
  54. * Returns RIO_INVALID_DESTID if new destID is not available.
  55. */
  56. static u16 rio_destid_alloc(struct rio_net *net)
  57. {
  58. int destid;
  59. struct rio_id_table *idtab = &net->destid_table;
  60. spin_lock(&idtab->lock);
  61. destid = find_first_zero_bit(idtab->table, idtab->max);
  62. if (destid < idtab->max) {
  63. set_bit(destid, idtab->table);
  64. destid += idtab->start;
  65. } else
  66. destid = RIO_INVALID_DESTID;
  67. spin_unlock(&idtab->lock);
  68. return (u16)destid;
  69. }
  70. /**
  71. * rio_destid_reserve - Reserve the specivied destID
  72. * @net: RIO network
  73. * @destid: destID to reserve
  74. *
  75. * Tries to reserve the specified destID.
  76. * Returns 0 if successfull.
  77. */
  78. static int rio_destid_reserve(struct rio_net *net, u16 destid)
  79. {
  80. int oldbit;
  81. struct rio_id_table *idtab = &net->destid_table;
  82. destid -= idtab->start;
  83. spin_lock(&idtab->lock);
  84. oldbit = test_and_set_bit(destid, idtab->table);
  85. spin_unlock(&idtab->lock);
  86. return oldbit;
  87. }
  88. /**
  89. * rio_destid_free - free a previously allocated destID
  90. * @net: RIO network
  91. * @destid: destID to free
  92. *
  93. * Makes the specified destID available for use.
  94. */
  95. static void rio_destid_free(struct rio_net *net, u16 destid)
  96. {
  97. struct rio_id_table *idtab = &net->destid_table;
  98. destid -= idtab->start;
  99. spin_lock(&idtab->lock);
  100. clear_bit(destid, idtab->table);
  101. spin_unlock(&idtab->lock);
  102. }
  103. /**
  104. * rio_destid_first - return first destID in use
  105. * @net: RIO network
  106. */
  107. static u16 rio_destid_first(struct rio_net *net)
  108. {
  109. int destid;
  110. struct rio_id_table *idtab = &net->destid_table;
  111. spin_lock(&idtab->lock);
  112. destid = find_first_bit(idtab->table, idtab->max);
  113. if (destid >= idtab->max)
  114. destid = RIO_INVALID_DESTID;
  115. else
  116. destid += idtab->start;
  117. spin_unlock(&idtab->lock);
  118. return (u16)destid;
  119. }
  120. /**
  121. * rio_destid_next - return next destID in use
  122. * @net: RIO network
  123. * @from: destination ID from which search shall continue
  124. */
  125. static u16 rio_destid_next(struct rio_net *net, u16 from)
  126. {
  127. int destid;
  128. struct rio_id_table *idtab = &net->destid_table;
  129. spin_lock(&idtab->lock);
  130. destid = find_next_bit(idtab->table, idtab->max, from);
  131. if (destid >= idtab->max)
  132. destid = RIO_INVALID_DESTID;
  133. else
  134. destid += idtab->start;
  135. spin_unlock(&idtab->lock);
  136. return (u16)destid;
  137. }
  138. /**
  139. * rio_get_device_id - Get the base/extended device id for a device
  140. * @port: RIO master port
  141. * @destid: Destination ID of device
  142. * @hopcount: Hopcount to device
  143. *
  144. * Reads the base/extended device id from a device. Returns the
  145. * 8/16-bit device ID.
  146. */
  147. static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
  148. {
  149. u32 result;
  150. rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
  151. return RIO_GET_DID(port->sys_size, result);
  152. }
  153. /**
  154. * rio_set_device_id - Set the base/extended device id for a device
  155. * @port: RIO master port
  156. * @destid: Destination ID of device
  157. * @hopcount: Hopcount to device
  158. * @did: Device ID value to be written
  159. *
  160. * Writes the base/extended device id from a device.
  161. */
  162. static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
  163. {
  164. rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
  165. RIO_SET_DID(port->sys_size, did));
  166. }
  167. /**
  168. * rio_local_set_device_id - Set the base/extended device id for a port
  169. * @port: RIO master port
  170. * @did: Device ID value to be written
  171. *
  172. * Writes the base/extended device id from a device.
  173. */
  174. static void rio_local_set_device_id(struct rio_mport *port, u16 did)
  175. {
  176. rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
  177. did));
  178. }
  179. /**
  180. * rio_clear_locks- Release all host locks and signal enumeration complete
  181. * @net: RIO network to run on
  182. *
  183. * Marks the component tag CSR on each device with the enumeration
  184. * complete flag. When complete, it then release the host locks on
  185. * each device. Returns 0 on success or %-EINVAL on failure.
  186. */
  187. static int rio_clear_locks(struct rio_net *net)
  188. {
  189. struct rio_mport *port = net->hport;
  190. struct rio_dev *rdev;
  191. u32 result;
  192. int ret = 0;
  193. /* Release host device id locks */
  194. rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
  195. port->host_deviceid);
  196. rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
  197. if ((result & 0xffff) != 0xffff) {
  198. printk(KERN_INFO
  199. "RIO: badness when releasing host lock on master port, result %8.8x\n",
  200. result);
  201. ret = -EINVAL;
  202. }
  203. list_for_each_entry(rdev, &net->devices, net_list) {
  204. rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
  205. port->host_deviceid);
  206. rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
  207. if ((result & 0xffff) != 0xffff) {
  208. printk(KERN_INFO
  209. "RIO: badness when releasing host lock on vid %4.4x did %4.4x\n",
  210. rdev->vid, rdev->did);
  211. ret = -EINVAL;
  212. }
  213. /* Mark device as discovered and enable master */
  214. rio_read_config_32(rdev,
  215. rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  216. &result);
  217. result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
  218. rio_write_config_32(rdev,
  219. rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  220. result);
  221. }
  222. return ret;
  223. }
  224. /**
  225. * rio_enum_host- Set host lock and initialize host destination ID
  226. * @port: Master port to issue transaction
  227. *
  228. * Sets the local host master port lock and destination ID register
  229. * with the host device ID value. The host device ID value is provided
  230. * by the platform. Returns %0 on success or %-1 on failure.
  231. */
  232. static int rio_enum_host(struct rio_mport *port)
  233. {
  234. u32 result;
  235. /* Set master port host device id lock */
  236. rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
  237. port->host_deviceid);
  238. rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
  239. if ((result & 0xffff) != port->host_deviceid)
  240. return -1;
  241. /* Set master port destid and init destid ctr */
  242. rio_local_set_device_id(port, port->host_deviceid);
  243. return 0;
  244. }
  245. /**
  246. * rio_device_has_destid- Test if a device contains a destination ID register
  247. * @port: Master port to issue transaction
  248. * @src_ops: RIO device source operations
  249. * @dst_ops: RIO device destination operations
  250. *
  251. * Checks the provided @src_ops and @dst_ops for the necessary transaction
  252. * capabilities that indicate whether or not a device will implement a
  253. * destination ID register. Returns 1 if true or 0 if false.
  254. */
  255. static int rio_device_has_destid(struct rio_mport *port, int src_ops,
  256. int dst_ops)
  257. {
  258. 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;
  259. return !!((src_ops | dst_ops) & mask);
  260. }
  261. /**
  262. * rio_release_dev- Frees a RIO device struct
  263. * @dev: LDM device associated with a RIO device struct
  264. *
  265. * Gets the RIO device struct associated a RIO device struct.
  266. * The RIO device struct is freed.
  267. */
  268. static void rio_release_dev(struct device *dev)
  269. {
  270. struct rio_dev *rdev;
  271. rdev = to_rio_dev(dev);
  272. kfree(rdev);
  273. }
  274. /**
  275. * rio_is_switch- Tests if a RIO device has switch capabilities
  276. * @rdev: RIO device
  277. *
  278. * Gets the RIO device Processing Element Features register
  279. * contents and tests for switch capabilities. Returns 1 if
  280. * the device is a switch or 0 if it is not a switch.
  281. * The RIO device struct is freed.
  282. */
  283. static int rio_is_switch(struct rio_dev *rdev)
  284. {
  285. if (rdev->pef & RIO_PEF_SWITCH)
  286. return 1;
  287. return 0;
  288. }
  289. /**
  290. * rio_switch_init - Sets switch operations for a particular vendor switch
  291. * @rdev: RIO device
  292. * @do_enum: Enumeration/Discovery mode flag
  293. *
  294. * Searches the RIO switch ops table for known switch types. If the vid
  295. * and did match a switch table entry, then call switch initialization
  296. * routine to setup switch-specific routines.
  297. */
  298. static void rio_switch_init(struct rio_dev *rdev, int do_enum)
  299. {
  300. struct rio_switch_ops *cur = __start_rio_switch_ops;
  301. struct rio_switch_ops *end = __end_rio_switch_ops;
  302. while (cur < end) {
  303. if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
  304. pr_debug("RIO: calling init routine for %s\n",
  305. rio_name(rdev));
  306. cur->init_hook(rdev, do_enum);
  307. break;
  308. }
  309. cur++;
  310. }
  311. if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
  312. pr_debug("RIO: adding STD routing ops for %s\n",
  313. rio_name(rdev));
  314. rdev->rswitch->add_entry = rio_std_route_add_entry;
  315. rdev->rswitch->get_entry = rio_std_route_get_entry;
  316. rdev->rswitch->clr_table = rio_std_route_clr_table;
  317. }
  318. if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
  319. printk(KERN_ERR "RIO: missing routing ops for %s\n",
  320. rio_name(rdev));
  321. }
  322. /**
  323. * rio_add_device- Adds a RIO device to the device model
  324. * @rdev: RIO device
  325. *
  326. * Adds the RIO device to the global device list and adds the RIO
  327. * device to the RIO device list. Creates the generic sysfs nodes
  328. * for an RIO device.
  329. */
  330. static int rio_add_device(struct rio_dev *rdev)
  331. {
  332. int err;
  333. err = device_add(&rdev->dev);
  334. if (err)
  335. return err;
  336. spin_lock(&rio_global_list_lock);
  337. list_add_tail(&rdev->global_list, &rio_devices);
  338. spin_unlock(&rio_global_list_lock);
  339. rio_create_sysfs_dev_files(rdev);
  340. return 0;
  341. }
  342. /**
  343. * rio_enable_rx_tx_port - enable input receiver and output transmitter of
  344. * given port
  345. * @port: Master port associated with the RIO network
  346. * @local: local=1 select local port otherwise a far device is reached
  347. * @destid: Destination ID of the device to check host bit
  348. * @hopcount: Number of hops to reach the target
  349. * @port_num: Port (-number on switch) to enable on a far end device
  350. *
  351. * Returns 0 or 1 from on General Control Command and Status Register
  352. * (EXT_PTR+0x3C)
  353. */
  354. inline int rio_enable_rx_tx_port(struct rio_mport *port,
  355. int local, u16 destid,
  356. u8 hopcount, u8 port_num) {
  357. #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
  358. u32 regval;
  359. u32 ext_ftr_ptr;
  360. /*
  361. * enable rx input tx output port
  362. */
  363. pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
  364. "%d, port_num = %d)\n", local, destid, hopcount, port_num);
  365. ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
  366. if (local) {
  367. rio_local_read_config_32(port, ext_ftr_ptr +
  368. RIO_PORT_N_CTL_CSR(0),
  369. &regval);
  370. } else {
  371. if (rio_mport_read_config_32(port, destid, hopcount,
  372. ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
  373. return -EIO;
  374. }
  375. if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
  376. /* serial */
  377. regval = regval | RIO_PORT_N_CTL_EN_RX_SER
  378. | RIO_PORT_N_CTL_EN_TX_SER;
  379. } else {
  380. /* parallel */
  381. regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
  382. | RIO_PORT_N_CTL_EN_TX_PAR;
  383. }
  384. if (local) {
  385. rio_local_write_config_32(port, ext_ftr_ptr +
  386. RIO_PORT_N_CTL_CSR(0), regval);
  387. } else {
  388. if (rio_mport_write_config_32(port, destid, hopcount,
  389. ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
  390. return -EIO;
  391. }
  392. #endif
  393. return 0;
  394. }
  395. /**
  396. * rio_setup_device- Allocates and sets up a RIO device
  397. * @net: RIO network
  398. * @port: Master port to send transactions
  399. * @destid: Current destination ID
  400. * @hopcount: Current hopcount
  401. * @do_enum: Enumeration/Discovery mode flag
  402. *
  403. * Allocates a RIO device and configures fields based on configuration
  404. * space contents. If device has a destination ID register, a destination
  405. * ID is either assigned in enumeration mode or read from configuration
  406. * space in discovery mode. If the device has switch capabilities, then
  407. * a switch is allocated and configured appropriately. Returns a pointer
  408. * to a RIO device on success or NULL on failure.
  409. *
  410. */
  411. static struct rio_dev *rio_setup_device(struct rio_net *net,
  412. struct rio_mport *port, u16 destid,
  413. u8 hopcount, int do_enum)
  414. {
  415. int ret = 0;
  416. struct rio_dev *rdev;
  417. struct rio_switch *rswitch = NULL;
  418. int result, rdid;
  419. size_t size;
  420. u32 swpinfo = 0;
  421. size = sizeof(struct rio_dev);
  422. if (rio_mport_read_config_32(port, destid, hopcount,
  423. RIO_PEF_CAR, &result))
  424. return NULL;
  425. if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
  426. rio_mport_read_config_32(port, destid, hopcount,
  427. RIO_SWP_INFO_CAR, &swpinfo);
  428. if (result & RIO_PEF_SWITCH) {
  429. size += (RIO_GET_TOTAL_PORTS(swpinfo) *
  430. sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
  431. }
  432. }
  433. rdev = kzalloc(size, GFP_KERNEL);
  434. if (!rdev)
  435. return NULL;
  436. rdev->net = net;
  437. rdev->pef = result;
  438. rdev->swpinfo = swpinfo;
  439. rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
  440. &result);
  441. rdev->did = result >> 16;
  442. rdev->vid = result & 0xffff;
  443. rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
  444. &rdev->device_rev);
  445. rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
  446. &result);
  447. rdev->asm_did = result >> 16;
  448. rdev->asm_vid = result & 0xffff;
  449. rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
  450. &result);
  451. rdev->asm_rev = result >> 16;
  452. if (rdev->pef & RIO_PEF_EXT_FEATURES) {
  453. rdev->efptr = result & 0xffff;
  454. rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
  455. hopcount);
  456. rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
  457. hopcount, RIO_EFB_ERR_MGMNT);
  458. }
  459. rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
  460. &rdev->src_ops);
  461. rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
  462. &rdev->dst_ops);
  463. if (do_enum) {
  464. /* Assign component tag to device */
  465. if (next_comptag >= 0x10000) {
  466. pr_err("RIO: Component Tag Counter Overflow\n");
  467. goto cleanup;
  468. }
  469. rio_mport_write_config_32(port, destid, hopcount,
  470. RIO_COMPONENT_TAG_CSR, next_comptag);
  471. rdev->comp_tag = next_comptag++;
  472. } else {
  473. rio_mport_read_config_32(port, destid, hopcount,
  474. RIO_COMPONENT_TAG_CSR,
  475. &rdev->comp_tag);
  476. }
  477. if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
  478. if (do_enum) {
  479. rio_set_device_id(port, destid, hopcount, next_destid);
  480. rdev->destid = next_destid;
  481. next_destid = rio_destid_alloc(net);
  482. } else
  483. rdev->destid = rio_get_device_id(port, destid, hopcount);
  484. rdev->hopcount = 0xff;
  485. } else {
  486. /* Switch device has an associated destID which
  487. * will be adjusted later
  488. */
  489. rdev->destid = destid;
  490. rdev->hopcount = hopcount;
  491. }
  492. /* If a PE has both switch and other functions, show it as a switch */
  493. if (rio_is_switch(rdev)) {
  494. rswitch = rdev->rswitch;
  495. rswitch->switchid = rdev->comp_tag & RIO_CTAG_UDEVID;
  496. rswitch->port_ok = 0;
  497. rswitch->route_table = kzalloc(sizeof(u8)*
  498. RIO_MAX_ROUTE_ENTRIES(port->sys_size),
  499. GFP_KERNEL);
  500. if (!rswitch->route_table)
  501. goto cleanup;
  502. /* Initialize switch route table */
  503. for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
  504. rdid++)
  505. rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
  506. dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
  507. rswitch->switchid);
  508. rio_switch_init(rdev, do_enum);
  509. if (do_enum && rswitch->clr_table)
  510. rswitch->clr_table(port, destid, hopcount,
  511. RIO_GLOBAL_TABLE);
  512. list_add_tail(&rswitch->node, &net->switches);
  513. } else {
  514. if (do_enum)
  515. /*Enable Input Output Port (transmitter reviever)*/
  516. rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
  517. dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
  518. rdev->destid);
  519. }
  520. rdev->dev.bus = &rio_bus_type;
  521. rdev->dev.parent = &rio_bus;
  522. device_initialize(&rdev->dev);
  523. rdev->dev.release = rio_release_dev;
  524. rio_dev_get(rdev);
  525. rdev->dma_mask = DMA_BIT_MASK(32);
  526. rdev->dev.dma_mask = &rdev->dma_mask;
  527. rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  528. if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
  529. rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
  530. 0, 0xffff);
  531. ret = rio_add_device(rdev);
  532. if (ret)
  533. goto cleanup;
  534. return rdev;
  535. cleanup:
  536. if (rswitch)
  537. kfree(rswitch->route_table);
  538. kfree(rdev);
  539. return NULL;
  540. }
  541. /**
  542. * rio_sport_is_active- Tests if a switch port has an active connection.
  543. * @port: Master port to send transaction
  544. * @destid: Associated destination ID for switch
  545. * @hopcount: Hopcount to reach switch
  546. * @sport: Switch port number
  547. *
  548. * Reads the port error status CSR for a particular switch port to
  549. * determine if the port has an active link. Returns
  550. * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
  551. * inactive.
  552. */
  553. static int
  554. rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
  555. {
  556. u32 result = 0;
  557. u32 ext_ftr_ptr;
  558. ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
  559. while (ext_ftr_ptr) {
  560. rio_mport_read_config_32(port, destid, hopcount,
  561. ext_ftr_ptr, &result);
  562. result = RIO_GET_BLOCK_ID(result);
  563. if ((result == RIO_EFB_SER_EP_FREE_ID) ||
  564. (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
  565. (result == RIO_EFB_SER_EP_FREC_ID))
  566. break;
  567. ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
  568. ext_ftr_ptr);
  569. }
  570. if (ext_ftr_ptr)
  571. rio_mport_read_config_32(port, destid, hopcount,
  572. ext_ftr_ptr +
  573. RIO_PORT_N_ERR_STS_CSR(sport),
  574. &result);
  575. return result & RIO_PORT_N_ERR_STS_PORT_OK;
  576. }
  577. /**
  578. * rio_lock_device - Acquires host device lock for specified device
  579. * @port: Master port to send transaction
  580. * @destid: Destination ID for device/switch
  581. * @hopcount: Hopcount to reach switch
  582. * @wait_ms: Max wait time in msec (0 = no timeout)
  583. *
  584. * Attepts to acquire host device lock for specified device
  585. * Returns 0 if device lock acquired or EINVAL if timeout expires.
  586. */
  587. static int
  588. rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
  589. {
  590. u32 result;
  591. int tcnt = 0;
  592. /* Attempt to acquire device lock */
  593. rio_mport_write_config_32(port, destid, hopcount,
  594. RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  595. rio_mport_read_config_32(port, destid, hopcount,
  596. RIO_HOST_DID_LOCK_CSR, &result);
  597. while (result != port->host_deviceid) {
  598. if (wait_ms != 0 && tcnt == wait_ms) {
  599. pr_debug("RIO: timeout when locking device %x:%x\n",
  600. destid, hopcount);
  601. return -EINVAL;
  602. }
  603. /* Delay a bit */
  604. mdelay(1);
  605. tcnt++;
  606. /* Try to acquire device lock again */
  607. rio_mport_write_config_32(port, destid,
  608. hopcount,
  609. RIO_HOST_DID_LOCK_CSR,
  610. port->host_deviceid);
  611. rio_mport_read_config_32(port, destid,
  612. hopcount,
  613. RIO_HOST_DID_LOCK_CSR, &result);
  614. }
  615. return 0;
  616. }
  617. /**
  618. * rio_unlock_device - Releases host device lock for specified device
  619. * @port: Master port to send transaction
  620. * @destid: Destination ID for device/switch
  621. * @hopcount: Hopcount to reach switch
  622. *
  623. * Returns 0 if device lock released or EINVAL if fails.
  624. */
  625. static int
  626. rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
  627. {
  628. u32 result;
  629. /* Release device lock */
  630. rio_mport_write_config_32(port, destid,
  631. hopcount,
  632. RIO_HOST_DID_LOCK_CSR,
  633. port->host_deviceid);
  634. rio_mport_read_config_32(port, destid, hopcount,
  635. RIO_HOST_DID_LOCK_CSR, &result);
  636. if ((result & 0xffff) != 0xffff) {
  637. pr_debug("RIO: badness when releasing device lock %x:%x\n",
  638. destid, hopcount);
  639. return -EINVAL;
  640. }
  641. return 0;
  642. }
  643. /**
  644. * rio_route_add_entry- Add a route entry to a switch routing table
  645. * @rdev: RIO device
  646. * @table: Routing table ID
  647. * @route_destid: Destination ID to be routed
  648. * @route_port: Port number to be routed
  649. * @lock: lock switch device flag
  650. *
  651. * Calls the switch specific add_entry() method to add a route entry
  652. * on a switch. The route table can be specified using the @table
  653. * argument if a switch has per port routing tables or the normal
  654. * use is to specific all tables (or the global table) by passing
  655. * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
  656. * on failure.
  657. */
  658. static int
  659. rio_route_add_entry(struct rio_dev *rdev,
  660. u16 table, u16 route_destid, u8 route_port, int lock)
  661. {
  662. int rc;
  663. if (lock) {
  664. rc = rio_lock_device(rdev->net->hport, rdev->destid,
  665. rdev->hopcount, 1000);
  666. if (rc)
  667. return rc;
  668. }
  669. rc = rdev->rswitch->add_entry(rdev->net->hport, rdev->destid,
  670. rdev->hopcount, table,
  671. route_destid, route_port);
  672. if (lock)
  673. rio_unlock_device(rdev->net->hport, rdev->destid,
  674. rdev->hopcount);
  675. return rc;
  676. }
  677. /**
  678. * rio_route_get_entry- Read a route entry in a switch routing table
  679. * @rdev: RIO device
  680. * @table: Routing table ID
  681. * @route_destid: Destination ID to be routed
  682. * @route_port: Pointer to read port number into
  683. * @lock: lock switch device flag
  684. *
  685. * Calls the switch specific get_entry() method to read a route entry
  686. * in a switch. The route table can be specified using the @table
  687. * argument if a switch has per port routing tables or the normal
  688. * use is to specific all tables (or the global table) by passing
  689. * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
  690. * on failure.
  691. */
  692. static int
  693. rio_route_get_entry(struct rio_dev *rdev, u16 table,
  694. u16 route_destid, u8 *route_port, int lock)
  695. {
  696. int rc;
  697. if (lock) {
  698. rc = rio_lock_device(rdev->net->hport, rdev->destid,
  699. rdev->hopcount, 1000);
  700. if (rc)
  701. return rc;
  702. }
  703. rc = rdev->rswitch->get_entry(rdev->net->hport, rdev->destid,
  704. rdev->hopcount, table,
  705. route_destid, route_port);
  706. if (lock)
  707. rio_unlock_device(rdev->net->hport, rdev->destid,
  708. rdev->hopcount);
  709. return rc;
  710. }
  711. /**
  712. * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
  713. * @port: Master port to send transaction
  714. * @hopcount: Number of hops to the device
  715. *
  716. * Used during enumeration to read the Host Device ID Lock CSR on a
  717. * RIO device. Returns the value of the lock register.
  718. */
  719. static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
  720. {
  721. u32 result;
  722. rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
  723. RIO_HOST_DID_LOCK_CSR, &result);
  724. return (u16) (result & 0xffff);
  725. }
  726. /**
  727. * rio_enum_peer- Recursively enumerate a RIO network through a master port
  728. * @net: RIO network being enumerated
  729. * @port: Master port to send transactions
  730. * @hopcount: Number of hops into the network
  731. * @prev: Previous RIO device connected to the enumerated one
  732. * @prev_port: Port on previous RIO device
  733. *
  734. * Recursively enumerates a RIO network. Transactions are sent via the
  735. * master port passed in @port.
  736. */
  737. static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
  738. u8 hopcount, struct rio_dev *prev, int prev_port)
  739. {
  740. struct rio_dev *rdev;
  741. u32 regval;
  742. int tmp;
  743. if (rio_mport_chk_dev_access(port,
  744. RIO_ANY_DESTID(port->sys_size), hopcount)) {
  745. pr_debug("RIO: device access check failed\n");
  746. return -1;
  747. }
  748. if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
  749. pr_debug("RIO: PE already discovered by this host\n");
  750. /*
  751. * Already discovered by this host. Add it as another
  752. * link to the existing device.
  753. */
  754. rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
  755. hopcount, RIO_COMPONENT_TAG_CSR, &regval);
  756. if (regval) {
  757. rdev = rio_get_comptag((regval & 0xffff), NULL);
  758. if (rdev && prev && rio_is_switch(prev)) {
  759. pr_debug("RIO: redundant path to %s\n",
  760. rio_name(rdev));
  761. prev->rswitch->nextdev[prev_port] = rdev;
  762. }
  763. }
  764. return 0;
  765. }
  766. /* Attempt to acquire device lock */
  767. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  768. hopcount,
  769. RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  770. while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
  771. < port->host_deviceid) {
  772. /* Delay a bit */
  773. mdelay(1);
  774. /* Attempt to acquire device lock again */
  775. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  776. hopcount,
  777. RIO_HOST_DID_LOCK_CSR,
  778. port->host_deviceid);
  779. }
  780. if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
  781. pr_debug(
  782. "RIO: PE locked by a higher priority host...retreating\n");
  783. return -1;
  784. }
  785. /* Setup new RIO device */
  786. rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
  787. hopcount, 1);
  788. if (rdev) {
  789. /* Add device to the global and bus/net specific list. */
  790. list_add_tail(&rdev->net_list, &net->devices);
  791. rdev->prev = prev;
  792. if (prev && rio_is_switch(prev))
  793. prev->rswitch->nextdev[prev_port] = rdev;
  794. } else
  795. return -1;
  796. if (rio_is_switch(rdev)) {
  797. int sw_destid;
  798. int cur_destid;
  799. int sw_inport;
  800. u16 destid;
  801. int port_num;
  802. sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
  803. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  804. port->host_deviceid, sw_inport, 0);
  805. rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
  806. destid = rio_destid_first(net);
  807. while (destid != RIO_INVALID_DESTID && destid < next_destid) {
  808. if (destid != port->host_deviceid) {
  809. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  810. destid, sw_inport, 0);
  811. rdev->rswitch->route_table[destid] = sw_inport;
  812. }
  813. destid = rio_destid_next(net, destid + 1);
  814. }
  815. pr_debug(
  816. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  817. rio_name(rdev), rdev->vid, rdev->did,
  818. RIO_GET_TOTAL_PORTS(rdev->swpinfo));
  819. sw_destid = next_destid;
  820. for (port_num = 0;
  821. port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  822. port_num++) {
  823. if (sw_inport == port_num) {
  824. rio_enable_rx_tx_port(port, 0,
  825. RIO_ANY_DESTID(port->sys_size),
  826. hopcount, port_num);
  827. rdev->rswitch->port_ok |= (1 << port_num);
  828. continue;
  829. }
  830. cur_destid = next_destid;
  831. if (rio_sport_is_active
  832. (port, RIO_ANY_DESTID(port->sys_size), hopcount,
  833. port_num)) {
  834. pr_debug(
  835. "RIO: scanning device on port %d\n",
  836. port_num);
  837. rio_enable_rx_tx_port(port, 0,
  838. RIO_ANY_DESTID(port->sys_size),
  839. hopcount, port_num);
  840. rdev->rswitch->port_ok |= (1 << port_num);
  841. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  842. RIO_ANY_DESTID(port->sys_size),
  843. port_num, 0);
  844. if (rio_enum_peer(net, port, hopcount + 1,
  845. rdev, port_num) < 0)
  846. return -1;
  847. /* Update routing tables */
  848. destid = rio_destid_next(net, cur_destid + 1);
  849. if (destid != RIO_INVALID_DESTID) {
  850. for (destid = cur_destid;
  851. destid < next_destid;) {
  852. if (destid != port->host_deviceid) {
  853. rio_route_add_entry(rdev,
  854. RIO_GLOBAL_TABLE,
  855. destid,
  856. port_num,
  857. 0);
  858. rdev->rswitch->
  859. route_table[destid] =
  860. port_num;
  861. }
  862. destid = rio_destid_next(net,
  863. destid + 1);
  864. }
  865. }
  866. } else {
  867. /* If switch supports Error Management,
  868. * set PORT_LOCKOUT bit for unused port
  869. */
  870. if (rdev->em_efptr)
  871. rio_set_port_lockout(rdev, port_num, 1);
  872. rdev->rswitch->port_ok &= ~(1 << port_num);
  873. }
  874. }
  875. /* Direct Port-write messages to the enumeratiing host */
  876. if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
  877. (rdev->em_efptr)) {
  878. rio_write_config_32(rdev,
  879. rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
  880. (port->host_deviceid << 16) |
  881. (port->sys_size << 15));
  882. }
  883. rio_init_em(rdev);
  884. /* Check for empty switch */
  885. if (next_destid == sw_destid)
  886. next_destid = rio_destid_alloc(net);
  887. rdev->destid = sw_destid;
  888. } else
  889. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  890. rio_name(rdev), rdev->vid, rdev->did);
  891. return 0;
  892. }
  893. /**
  894. * rio_enum_complete- Tests if enumeration of a network is complete
  895. * @port: Master port to send transaction
  896. *
  897. * Tests the PGCCSR discovered bit for non-zero value (enumeration
  898. * complete flag). Return %1 if enumeration is complete or %0 if
  899. * enumeration is incomplete.
  900. */
  901. static int rio_enum_complete(struct rio_mport *port)
  902. {
  903. u32 regval;
  904. rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  905. &regval);
  906. return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
  907. }
  908. /**
  909. * rio_disc_peer- Recursively discovers a RIO network through a master port
  910. * @net: RIO network being discovered
  911. * @port: Master port to send transactions
  912. * @destid: Current destination ID in network
  913. * @hopcount: Number of hops into the network
  914. * @prev: previous rio_dev
  915. * @prev_port: previous port number
  916. *
  917. * Recursively discovers a RIO network. Transactions are sent via the
  918. * master port passed in @port.
  919. */
  920. static int
  921. rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
  922. u8 hopcount, struct rio_dev *prev, int prev_port)
  923. {
  924. u8 port_num, route_port;
  925. struct rio_dev *rdev;
  926. u16 ndestid;
  927. /* Setup new RIO device */
  928. if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
  929. /* Add device to the global and bus/net specific list. */
  930. list_add_tail(&rdev->net_list, &net->devices);
  931. rdev->prev = prev;
  932. if (prev && rio_is_switch(prev))
  933. prev->rswitch->nextdev[prev_port] = rdev;
  934. } else
  935. return -1;
  936. if (rio_is_switch(rdev)) {
  937. /* Associated destid is how we accessed this switch */
  938. rdev->destid = destid;
  939. pr_debug(
  940. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  941. rio_name(rdev), rdev->vid, rdev->did,
  942. RIO_GET_TOTAL_PORTS(rdev->swpinfo));
  943. for (port_num = 0;
  944. port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  945. port_num++) {
  946. if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
  947. continue;
  948. if (rio_sport_is_active
  949. (port, destid, hopcount, port_num)) {
  950. pr_debug(
  951. "RIO: scanning device on port %d\n",
  952. port_num);
  953. rio_lock_device(port, destid, hopcount, 1000);
  954. for (ndestid = 0;
  955. ndestid < RIO_ANY_DESTID(port->sys_size);
  956. ndestid++) {
  957. rio_route_get_entry(rdev,
  958. RIO_GLOBAL_TABLE,
  959. ndestid,
  960. &route_port, 0);
  961. if (route_port == port_num)
  962. break;
  963. }
  964. if (ndestid == RIO_ANY_DESTID(port->sys_size))
  965. continue;
  966. rio_unlock_device(port, destid, hopcount);
  967. if (rio_disc_peer(net, port, ndestid,
  968. hopcount + 1, rdev, port_num) < 0)
  969. return -1;
  970. }
  971. }
  972. } else
  973. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  974. rio_name(rdev), rdev->vid, rdev->did);
  975. return 0;
  976. }
  977. /**
  978. * rio_mport_is_active- Tests if master port link is active
  979. * @port: Master port to test
  980. *
  981. * Reads the port error status CSR for the master port to
  982. * determine if the port has an active link. Returns
  983. * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
  984. * or %0 if it is inactive.
  985. */
  986. static int rio_mport_is_active(struct rio_mport *port)
  987. {
  988. u32 result = 0;
  989. u32 ext_ftr_ptr;
  990. int *entry = rio_mport_phys_table;
  991. do {
  992. if ((ext_ftr_ptr =
  993. rio_mport_get_feature(port, 1, 0, 0, *entry)))
  994. break;
  995. } while (*++entry >= 0);
  996. if (ext_ftr_ptr)
  997. rio_local_read_config_32(port,
  998. ext_ftr_ptr +
  999. RIO_PORT_N_ERR_STS_CSR(port->index),
  1000. &result);
  1001. return result & RIO_PORT_N_ERR_STS_PORT_OK;
  1002. }
  1003. /**
  1004. * rio_alloc_net- Allocate and configure a new RIO network
  1005. * @port: Master port associated with the RIO network
  1006. * @do_enum: Enumeration/Discovery mode flag
  1007. * @start: logical minimal start id for new net
  1008. *
  1009. * Allocates a RIO network structure, initializes per-network
  1010. * list heads, and adds the associated master port to the
  1011. * network list of associated master ports. Returns a
  1012. * RIO network pointer on success or %NULL on failure.
  1013. */
  1014. static struct rio_net *rio_alloc_net(struct rio_mport *port,
  1015. int do_enum, u16 start)
  1016. {
  1017. struct rio_net *net;
  1018. net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
  1019. if (net && do_enum) {
  1020. net->destid_table.table = kcalloc(
  1021. BITS_TO_LONGS(RIO_MAX_ROUTE_ENTRIES(port->sys_size)),
  1022. sizeof(long),
  1023. GFP_KERNEL);
  1024. if (net->destid_table.table == NULL) {
  1025. pr_err("RIO: failed to allocate destID table\n");
  1026. kfree(net);
  1027. net = NULL;
  1028. } else {
  1029. net->destid_table.start = start;
  1030. net->destid_table.max =
  1031. RIO_MAX_ROUTE_ENTRIES(port->sys_size);
  1032. spin_lock_init(&net->destid_table.lock);
  1033. }
  1034. }
  1035. if (net) {
  1036. INIT_LIST_HEAD(&net->node);
  1037. INIT_LIST_HEAD(&net->devices);
  1038. INIT_LIST_HEAD(&net->switches);
  1039. INIT_LIST_HEAD(&net->mports);
  1040. list_add_tail(&port->nnode, &net->mports);
  1041. net->hport = port;
  1042. net->id = port->id;
  1043. }
  1044. return net;
  1045. }
  1046. /**
  1047. * rio_update_route_tables- Updates route tables in switches
  1048. * @net: RIO network to run update on
  1049. *
  1050. * For each enumerated device, ensure that each switch in a system
  1051. * has correct routing entries. Add routes for devices that where
  1052. * unknown dirung the first enumeration pass through the switch.
  1053. */
  1054. static void rio_update_route_tables(struct rio_net *net)
  1055. {
  1056. struct rio_dev *rdev, *swrdev;
  1057. struct rio_switch *rswitch;
  1058. u8 sport;
  1059. u16 destid;
  1060. list_for_each_entry(rdev, &net->devices, net_list) {
  1061. destid = rdev->destid;
  1062. list_for_each_entry(rswitch, &net->switches, node) {
  1063. if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
  1064. continue;
  1065. if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
  1066. swrdev = sw_to_rio_dev(rswitch);
  1067. /* Skip if destid ends in empty switch*/
  1068. if (swrdev->destid == destid)
  1069. continue;
  1070. sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
  1071. if (rswitch->add_entry) {
  1072. rio_route_add_entry(swrdev,
  1073. RIO_GLOBAL_TABLE, destid,
  1074. sport, 0);
  1075. rswitch->route_table[destid] = sport;
  1076. }
  1077. }
  1078. }
  1079. }
  1080. }
  1081. /**
  1082. * rio_init_em - Initializes RIO Error Management (for switches)
  1083. * @rdev: RIO device
  1084. *
  1085. * For each enumerated switch, call device-specific error management
  1086. * initialization routine (if supplied by the switch driver).
  1087. */
  1088. static void rio_init_em(struct rio_dev *rdev)
  1089. {
  1090. if (rio_is_switch(rdev) && (rdev->em_efptr) &&
  1091. (rdev->rswitch->em_init)) {
  1092. rdev->rswitch->em_init(rdev);
  1093. }
  1094. }
  1095. /**
  1096. * rio_pw_enable - Enables/disables port-write handling by a master port
  1097. * @port: Master port associated with port-write handling
  1098. * @enable: 1=enable, 0=disable
  1099. */
  1100. static void rio_pw_enable(struct rio_mport *port, int enable)
  1101. {
  1102. if (port->ops->pwenable)
  1103. port->ops->pwenable(port, enable);
  1104. }
  1105. /**
  1106. * rio_enum_mport- Start enumeration through a master port
  1107. * @mport: Master port to send transactions
  1108. *
  1109. * Starts the enumeration process. If somebody has enumerated our
  1110. * master port device, then give up. If not and we have an active
  1111. * link, then start recursive peer enumeration. Returns %0 if
  1112. * enumeration succeeds or %-EBUSY if enumeration fails.
  1113. */
  1114. int rio_enum_mport(struct rio_mport *mport)
  1115. {
  1116. struct rio_net *net = NULL;
  1117. int rc = 0;
  1118. printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
  1119. mport->name);
  1120. /* If somebody else enumerated our master port device, bail. */
  1121. if (rio_enum_host(mport) < 0) {
  1122. printk(KERN_INFO
  1123. "RIO: master port %d device has been enumerated by a remote host\n",
  1124. mport->id);
  1125. rc = -EBUSY;
  1126. goto out;
  1127. }
  1128. /* If master port has an active link, allocate net and enum peers */
  1129. if (rio_mport_is_active(mport)) {
  1130. net = rio_alloc_net(mport, 1, 0);
  1131. if (!net) {
  1132. printk(KERN_ERR "RIO: failed to allocate new net\n");
  1133. rc = -ENOMEM;
  1134. goto out;
  1135. }
  1136. /* reserve mport destID in new net */
  1137. rio_destid_reserve(net, mport->host_deviceid);
  1138. /* Enable Input Output Port (transmitter reviever) */
  1139. rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
  1140. /* Set component tag for host */
  1141. rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
  1142. next_comptag++);
  1143. next_destid = rio_destid_alloc(net);
  1144. if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
  1145. /* A higher priority host won enumeration, bail. */
  1146. printk(KERN_INFO
  1147. "RIO: master port %d device has lost enumeration to a remote host\n",
  1148. mport->id);
  1149. rio_clear_locks(net);
  1150. rc = -EBUSY;
  1151. goto out;
  1152. }
  1153. /* free the last allocated destID (unused) */
  1154. rio_destid_free(net, next_destid);
  1155. rio_update_route_tables(net);
  1156. rio_clear_locks(net);
  1157. rio_pw_enable(mport, 1);
  1158. } else {
  1159. printk(KERN_INFO "RIO: master port %d link inactive\n",
  1160. mport->id);
  1161. rc = -EINVAL;
  1162. }
  1163. out:
  1164. return rc;
  1165. }
  1166. /**
  1167. * rio_build_route_tables- Generate route tables from switch route entries
  1168. * @net: RIO network to run route tables scan on
  1169. *
  1170. * For each switch device, generate a route table by copying existing
  1171. * route entries from the switch.
  1172. */
  1173. static void rio_build_route_tables(struct rio_net *net)
  1174. {
  1175. struct rio_switch *rswitch;
  1176. struct rio_dev *rdev;
  1177. int i;
  1178. u8 sport;
  1179. list_for_each_entry(rswitch, &net->switches, node) {
  1180. rdev = sw_to_rio_dev(rswitch);
  1181. rio_lock_device(net->hport, rdev->destid,
  1182. rdev->hopcount, 1000);
  1183. for (i = 0;
  1184. i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
  1185. i++) {
  1186. if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
  1187. i, &sport, 0) < 0)
  1188. continue;
  1189. rswitch->route_table[i] = sport;
  1190. }
  1191. rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
  1192. }
  1193. }
  1194. /**
  1195. * rio_disc_mport- Start discovery through a master port
  1196. * @mport: Master port to send transactions
  1197. *
  1198. * Starts the discovery process. If we have an active link,
  1199. * then wait for the signal that enumeration is complete.
  1200. * When enumeration completion is signaled, start recursive
  1201. * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
  1202. * on failure.
  1203. */
  1204. int rio_disc_mport(struct rio_mport *mport)
  1205. {
  1206. struct rio_net *net = NULL;
  1207. unsigned long to_end;
  1208. printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
  1209. mport->name);
  1210. /* If master port has an active link, allocate net and discover peers */
  1211. if (rio_mport_is_active(mport)) {
  1212. pr_debug("RIO: wait for enumeration to complete...\n");
  1213. to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
  1214. while (time_before(jiffies, to_end)) {
  1215. if (rio_enum_complete(mport))
  1216. goto enum_done;
  1217. msleep(10);
  1218. }
  1219. pr_debug("RIO: discovery timeout on mport %d %s\n",
  1220. mport->id, mport->name);
  1221. goto bail;
  1222. enum_done:
  1223. pr_debug("RIO: ... enumeration done\n");
  1224. net = rio_alloc_net(mport, 0, 0);
  1225. if (!net) {
  1226. printk(KERN_ERR "RIO: Failed to allocate new net\n");
  1227. goto bail;
  1228. }
  1229. /* Read DestID assigned by enumerator */
  1230. rio_local_read_config_32(mport, RIO_DID_CSR,
  1231. &mport->host_deviceid);
  1232. mport->host_deviceid = RIO_GET_DID(mport->sys_size,
  1233. mport->host_deviceid);
  1234. if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
  1235. 0, NULL, 0) < 0) {
  1236. printk(KERN_INFO
  1237. "RIO: master port %d device has failed discovery\n",
  1238. mport->id);
  1239. goto bail;
  1240. }
  1241. rio_build_route_tables(net);
  1242. }
  1243. return 0;
  1244. bail:
  1245. return -EBUSY;
  1246. }