rio-scan.c 39 KB

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