rio-scan.c 32 KB

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