rio-scan.c 31 KB

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