rio-scan.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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. goto cleanup;
  408. rio_dev_get(rdev);
  409. return rdev;
  410. cleanup:
  411. if (rswitch)
  412. kfree(rswitch->route_table);
  413. kfree(rdev);
  414. return NULL;
  415. }
  416. /**
  417. * rio_sport_is_active- Tests if a switch port has an active connection.
  418. * @rdev: RapidIO device object
  419. * @sp: Switch port number
  420. *
  421. * Reads the port error status CSR for a particular switch port to
  422. * determine if the port has an active link. Returns
  423. * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
  424. * inactive.
  425. */
  426. static int
  427. rio_sport_is_active(struct rio_dev *rdev, int sp)
  428. {
  429. u32 result = 0;
  430. rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, sp),
  431. &result);
  432. return result & RIO_PORT_N_ERR_STS_PORT_OK;
  433. }
  434. /**
  435. * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
  436. * @port: Master port to send transaction
  437. * @hopcount: Number of hops to the device
  438. *
  439. * Used during enumeration to read the Host Device ID Lock CSR on a
  440. * RIO device. Returns the value of the lock register.
  441. */
  442. static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
  443. {
  444. u32 result;
  445. rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
  446. RIO_HOST_DID_LOCK_CSR, &result);
  447. return (u16) (result & 0xffff);
  448. }
  449. /**
  450. * rio_enum_peer- Recursively enumerate a RIO network through a master port
  451. * @net: RIO network being enumerated
  452. * @port: Master port to send transactions
  453. * @hopcount: Number of hops into the network
  454. * @prev: Previous RIO device connected to the enumerated one
  455. * @prev_port: Port on previous RIO device
  456. *
  457. * Recursively enumerates a RIO network. Transactions are sent via the
  458. * master port passed in @port.
  459. */
  460. static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
  461. u8 hopcount, struct rio_dev *prev, int prev_port)
  462. {
  463. struct rio_dev *rdev;
  464. u32 regval;
  465. int tmp;
  466. if (rio_mport_chk_dev_access(port,
  467. RIO_ANY_DESTID(port->sys_size), hopcount)) {
  468. pr_debug("RIO: device access check failed\n");
  469. return -1;
  470. }
  471. if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
  472. pr_debug("RIO: PE already discovered by this host\n");
  473. /*
  474. * Already discovered by this host. Add it as another
  475. * link to the existing device.
  476. */
  477. rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
  478. hopcount, RIO_COMPONENT_TAG_CSR, &regval);
  479. if (regval) {
  480. rdev = rio_get_comptag((regval & 0xffff), NULL);
  481. if (rdev && prev && rio_is_switch(prev)) {
  482. pr_debug("RIO: redundant path to %s\n",
  483. rio_name(rdev));
  484. prev->rswitch->nextdev[prev_port] = rdev;
  485. }
  486. }
  487. return 0;
  488. }
  489. /* Attempt to acquire device lock */
  490. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  491. hopcount,
  492. RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  493. while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
  494. < port->host_deviceid) {
  495. /* Delay a bit */
  496. mdelay(1);
  497. /* Attempt to acquire device lock again */
  498. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  499. hopcount,
  500. RIO_HOST_DID_LOCK_CSR,
  501. port->host_deviceid);
  502. }
  503. if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
  504. pr_debug(
  505. "RIO: PE locked by a higher priority host...retreating\n");
  506. return -1;
  507. }
  508. /* Setup new RIO device */
  509. rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
  510. hopcount, 1);
  511. if (rdev) {
  512. rdev->prev = prev;
  513. if (prev && rio_is_switch(prev))
  514. prev->rswitch->nextdev[prev_port] = rdev;
  515. } else
  516. return -1;
  517. if (rio_is_switch(rdev)) {
  518. int sw_destid;
  519. int cur_destid;
  520. int sw_inport;
  521. u16 destid;
  522. int port_num;
  523. sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
  524. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  525. port->host_deviceid, sw_inport, 0);
  526. rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
  527. destid = rio_destid_first(net);
  528. while (destid != RIO_INVALID_DESTID && destid < next_destid) {
  529. if (destid != port->host_deviceid) {
  530. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  531. destid, sw_inport, 0);
  532. rdev->rswitch->route_table[destid] = sw_inport;
  533. }
  534. destid = rio_destid_next(net, destid + 1);
  535. }
  536. pr_debug(
  537. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  538. rio_name(rdev), rdev->vid, rdev->did,
  539. RIO_GET_TOTAL_PORTS(rdev->swpinfo));
  540. sw_destid = next_destid;
  541. for (port_num = 0;
  542. port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  543. port_num++) {
  544. if (sw_inport == port_num) {
  545. rio_enable_rx_tx_port(port, 0,
  546. RIO_ANY_DESTID(port->sys_size),
  547. hopcount, port_num);
  548. rdev->rswitch->port_ok |= (1 << port_num);
  549. continue;
  550. }
  551. cur_destid = next_destid;
  552. if (rio_sport_is_active(rdev, port_num)) {
  553. pr_debug(
  554. "RIO: scanning device on port %d\n",
  555. port_num);
  556. rio_enable_rx_tx_port(port, 0,
  557. RIO_ANY_DESTID(port->sys_size),
  558. hopcount, port_num);
  559. rdev->rswitch->port_ok |= (1 << port_num);
  560. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  561. RIO_ANY_DESTID(port->sys_size),
  562. port_num, 0);
  563. if (rio_enum_peer(net, port, hopcount + 1,
  564. rdev, port_num) < 0)
  565. return -1;
  566. /* Update routing tables */
  567. destid = rio_destid_next(net, cur_destid + 1);
  568. if (destid != RIO_INVALID_DESTID) {
  569. for (destid = cur_destid;
  570. destid < next_destid;) {
  571. if (destid != port->host_deviceid) {
  572. rio_route_add_entry(rdev,
  573. RIO_GLOBAL_TABLE,
  574. destid,
  575. port_num,
  576. 0);
  577. rdev->rswitch->
  578. route_table[destid] =
  579. port_num;
  580. }
  581. destid = rio_destid_next(net,
  582. destid + 1);
  583. }
  584. }
  585. } else {
  586. /* If switch supports Error Management,
  587. * set PORT_LOCKOUT bit for unused port
  588. */
  589. if (rdev->em_efptr)
  590. rio_set_port_lockout(rdev, port_num, 1);
  591. rdev->rswitch->port_ok &= ~(1 << port_num);
  592. }
  593. }
  594. /* Direct Port-write messages to the enumeratiing host */
  595. if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
  596. (rdev->em_efptr)) {
  597. rio_write_config_32(rdev,
  598. rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
  599. (port->host_deviceid << 16) |
  600. (port->sys_size << 15));
  601. }
  602. rio_init_em(rdev);
  603. /* Check for empty switch */
  604. if (next_destid == sw_destid)
  605. next_destid = rio_destid_alloc(net);
  606. rdev->destid = sw_destid;
  607. } else
  608. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  609. rio_name(rdev), rdev->vid, rdev->did);
  610. return 0;
  611. }
  612. /**
  613. * rio_enum_complete- Tests if enumeration of a network is complete
  614. * @port: Master port to send transaction
  615. *
  616. * Tests the PGCCSR discovered bit for non-zero value (enumeration
  617. * complete flag). Return %1 if enumeration is complete or %0 if
  618. * enumeration is incomplete.
  619. */
  620. static int rio_enum_complete(struct rio_mport *port)
  621. {
  622. u32 regval;
  623. rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  624. &regval);
  625. return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
  626. }
  627. /**
  628. * rio_disc_peer- Recursively discovers a RIO network through a master port
  629. * @net: RIO network being discovered
  630. * @port: Master port to send transactions
  631. * @destid: Current destination ID in network
  632. * @hopcount: Number of hops into the network
  633. * @prev: previous rio_dev
  634. * @prev_port: previous port number
  635. *
  636. * Recursively discovers a RIO network. Transactions are sent via the
  637. * master port passed in @port.
  638. */
  639. static int
  640. rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
  641. u8 hopcount, struct rio_dev *prev, int prev_port)
  642. {
  643. u8 port_num, route_port;
  644. struct rio_dev *rdev;
  645. u16 ndestid;
  646. /* Setup new RIO device */
  647. if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
  648. rdev->prev = prev;
  649. if (prev && rio_is_switch(prev))
  650. prev->rswitch->nextdev[prev_port] = rdev;
  651. } else
  652. return -1;
  653. if (rio_is_switch(rdev)) {
  654. /* Associated destid is how we accessed this switch */
  655. rdev->destid = destid;
  656. pr_debug(
  657. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  658. rio_name(rdev), rdev->vid, rdev->did,
  659. RIO_GET_TOTAL_PORTS(rdev->swpinfo));
  660. for (port_num = 0;
  661. port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  662. port_num++) {
  663. if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
  664. continue;
  665. if (rio_sport_is_active(rdev, port_num)) {
  666. pr_debug(
  667. "RIO: scanning device on port %d\n",
  668. port_num);
  669. rio_lock_device(port, destid, hopcount, 1000);
  670. for (ndestid = 0;
  671. ndestid < RIO_ANY_DESTID(port->sys_size);
  672. ndestid++) {
  673. rio_route_get_entry(rdev,
  674. RIO_GLOBAL_TABLE,
  675. ndestid,
  676. &route_port, 0);
  677. if (route_port == port_num)
  678. break;
  679. }
  680. if (ndestid == RIO_ANY_DESTID(port->sys_size))
  681. continue;
  682. rio_unlock_device(port, destid, hopcount);
  683. if (rio_disc_peer(net, port, ndestid,
  684. hopcount + 1, rdev, port_num) < 0)
  685. return -1;
  686. }
  687. }
  688. } else
  689. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  690. rio_name(rdev), rdev->vid, rdev->did);
  691. return 0;
  692. }
  693. /**
  694. * rio_mport_is_active- Tests if master port link is active
  695. * @port: Master port to test
  696. *
  697. * Reads the port error status CSR for the master port to
  698. * determine if the port has an active link. Returns
  699. * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
  700. * or %0 if it is inactive.
  701. */
  702. static int rio_mport_is_active(struct rio_mport *port)
  703. {
  704. u32 result = 0;
  705. rio_local_read_config_32(port,
  706. port->phys_efptr +
  707. RIO_PORT_N_ERR_STS_CSR(port->index, port->phys_rmap),
  708. &result);
  709. return result & RIO_PORT_N_ERR_STS_PORT_OK;
  710. }
  711. static void rio_scan_release_net(struct rio_net *net)
  712. {
  713. pr_debug("RIO-SCAN: %s: net_%d\n", __func__, net->id);
  714. kfree(net->enum_data);
  715. }
  716. static void rio_scan_release_dev(struct device *dev)
  717. {
  718. struct rio_net *net;
  719. net = to_rio_net(dev);
  720. pr_debug("RIO-SCAN: %s: net_%d\n", __func__, net->id);
  721. kfree(net);
  722. }
  723. /*
  724. * rio_scan_alloc_net - Allocate and configure a new RIO network
  725. * @mport: Master port associated with the RIO network
  726. * @do_enum: Enumeration/Discovery mode flag
  727. * @start: logical minimal start id for new net
  728. *
  729. * Allocates a new RIO network structure and initializes enumerator-specific
  730. * part of it (if required).
  731. * Returns a RIO network pointer on success or %NULL on failure.
  732. */
  733. static struct rio_net *rio_scan_alloc_net(struct rio_mport *mport,
  734. int do_enum, u16 start)
  735. {
  736. struct rio_net *net;
  737. net = rio_alloc_net(mport);
  738. if (net && do_enum) {
  739. struct rio_id_table *idtab;
  740. size_t size;
  741. size = sizeof(struct rio_id_table) +
  742. BITS_TO_LONGS(
  743. RIO_MAX_ROUTE_ENTRIES(mport->sys_size)
  744. ) * sizeof(long);
  745. idtab = kzalloc(size, GFP_KERNEL);
  746. if (idtab == NULL) {
  747. pr_err("RIO: failed to allocate destID table\n");
  748. rio_free_net(net);
  749. net = NULL;
  750. } else {
  751. net->enum_data = idtab;
  752. net->release = rio_scan_release_net;
  753. idtab->start = start;
  754. idtab->max = RIO_MAX_ROUTE_ENTRIES(mport->sys_size);
  755. spin_lock_init(&idtab->lock);
  756. }
  757. }
  758. if (net) {
  759. net->id = mport->id;
  760. net->hport = mport;
  761. dev_set_name(&net->dev, "rnet_%d", net->id);
  762. net->dev.parent = &mport->dev;
  763. net->dev.release = rio_scan_release_dev;
  764. rio_add_net(net);
  765. }
  766. return net;
  767. }
  768. /**
  769. * rio_update_route_tables- Updates route tables in switches
  770. * @net: RIO network to run update on
  771. *
  772. * For each enumerated device, ensure that each switch in a system
  773. * has correct routing entries. Add routes for devices that where
  774. * unknown during the first enumeration pass through the switch.
  775. */
  776. static void rio_update_route_tables(struct rio_net *net)
  777. {
  778. struct rio_dev *rdev, *swrdev;
  779. struct rio_switch *rswitch;
  780. u8 sport;
  781. u16 destid;
  782. list_for_each_entry(rdev, &net->devices, net_list) {
  783. destid = rdev->destid;
  784. list_for_each_entry(rswitch, &net->switches, node) {
  785. if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
  786. continue;
  787. if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
  788. swrdev = sw_to_rio_dev(rswitch);
  789. /* Skip if destid ends in empty switch*/
  790. if (swrdev->destid == destid)
  791. continue;
  792. sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
  793. rio_route_add_entry(swrdev, RIO_GLOBAL_TABLE,
  794. destid, sport, 0);
  795. rswitch->route_table[destid] = sport;
  796. }
  797. }
  798. }
  799. }
  800. /**
  801. * rio_init_em - Initializes RIO Error Management (for switches)
  802. * @rdev: RIO device
  803. *
  804. * For each enumerated switch, call device-specific error management
  805. * initialization routine (if supplied by the switch driver).
  806. */
  807. static void rio_init_em(struct rio_dev *rdev)
  808. {
  809. if (rio_is_switch(rdev) && (rdev->em_efptr) &&
  810. rdev->rswitch->ops && rdev->rswitch->ops->em_init) {
  811. rdev->rswitch->ops->em_init(rdev);
  812. }
  813. }
  814. /**
  815. * rio_enum_mport- Start enumeration through a master port
  816. * @mport: Master port to send transactions
  817. * @flags: Enumeration control flags
  818. *
  819. * Starts the enumeration process. If somebody has enumerated our
  820. * master port device, then give up. If not and we have an active
  821. * link, then start recursive peer enumeration. Returns %0 if
  822. * enumeration succeeds or %-EBUSY if enumeration fails.
  823. */
  824. static int rio_enum_mport(struct rio_mport *mport, u32 flags)
  825. {
  826. struct rio_net *net = NULL;
  827. int rc = 0;
  828. printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
  829. mport->name);
  830. /*
  831. * To avoid multiple start requests (repeat enumeration is not supported
  832. * by this method) check if enumeration/discovery was performed for this
  833. * mport: if mport was added into the list of mports for a net exit
  834. * with error.
  835. */
  836. if (mport->nnode.next || mport->nnode.prev)
  837. return -EBUSY;
  838. /* If somebody else enumerated our master port device, bail. */
  839. if (rio_enum_host(mport) < 0) {
  840. printk(KERN_INFO
  841. "RIO: master port %d device has been enumerated by a remote host\n",
  842. mport->id);
  843. rc = -EBUSY;
  844. goto out;
  845. }
  846. /* If master port has an active link, allocate net and enum peers */
  847. if (rio_mport_is_active(mport)) {
  848. net = rio_scan_alloc_net(mport, 1, 0);
  849. if (!net) {
  850. printk(KERN_ERR "RIO: failed to allocate new net\n");
  851. rc = -ENOMEM;
  852. goto out;
  853. }
  854. /* reserve mport destID in new net */
  855. rio_destid_reserve(net, mport->host_deviceid);
  856. /* Enable Input Output Port (transmitter receiver) */
  857. rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
  858. /* Set component tag for host */
  859. rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
  860. next_comptag++);
  861. next_destid = rio_destid_alloc(net);
  862. if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
  863. /* A higher priority host won enumeration, bail. */
  864. printk(KERN_INFO
  865. "RIO: master port %d device has lost enumeration to a remote host\n",
  866. mport->id);
  867. rio_clear_locks(net);
  868. rc = -EBUSY;
  869. goto out;
  870. }
  871. /* free the last allocated destID (unused) */
  872. rio_destid_free(net, next_destid);
  873. rio_update_route_tables(net);
  874. rio_clear_locks(net);
  875. rio_pw_enable(mport, 1);
  876. } else {
  877. printk(KERN_INFO "RIO: master port %d link inactive\n",
  878. mport->id);
  879. rc = -EINVAL;
  880. }
  881. out:
  882. return rc;
  883. }
  884. /**
  885. * rio_build_route_tables- Generate route tables from switch route entries
  886. * @net: RIO network to run route tables scan on
  887. *
  888. * For each switch device, generate a route table by copying existing
  889. * route entries from the switch.
  890. */
  891. static void rio_build_route_tables(struct rio_net *net)
  892. {
  893. struct rio_switch *rswitch;
  894. struct rio_dev *rdev;
  895. int i;
  896. u8 sport;
  897. list_for_each_entry(rswitch, &net->switches, node) {
  898. rdev = sw_to_rio_dev(rswitch);
  899. rio_lock_device(net->hport, rdev->destid,
  900. rdev->hopcount, 1000);
  901. for (i = 0;
  902. i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
  903. i++) {
  904. if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
  905. i, &sport, 0) < 0)
  906. continue;
  907. rswitch->route_table[i] = sport;
  908. }
  909. rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
  910. }
  911. }
  912. /**
  913. * rio_disc_mport- Start discovery through a master port
  914. * @mport: Master port to send transactions
  915. * @flags: discovery control flags
  916. *
  917. * Starts the discovery process. If we have an active link,
  918. * then wait for the signal that enumeration is complete (if wait
  919. * is allowed).
  920. * When enumeration completion is signaled, start recursive
  921. * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
  922. * on failure.
  923. */
  924. static int rio_disc_mport(struct rio_mport *mport, u32 flags)
  925. {
  926. struct rio_net *net = NULL;
  927. unsigned long to_end;
  928. printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
  929. mport->name);
  930. /* If master port has an active link, allocate net and discover peers */
  931. if (rio_mport_is_active(mport)) {
  932. if (rio_enum_complete(mport))
  933. goto enum_done;
  934. else if (flags & RIO_SCAN_ENUM_NO_WAIT)
  935. return -EAGAIN;
  936. pr_debug("RIO: wait for enumeration to complete...\n");
  937. to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
  938. while (time_before(jiffies, to_end)) {
  939. if (rio_enum_complete(mport))
  940. goto enum_done;
  941. msleep(10);
  942. }
  943. pr_debug("RIO: discovery timeout on mport %d %s\n",
  944. mport->id, mport->name);
  945. goto bail;
  946. enum_done:
  947. pr_debug("RIO: ... enumeration done\n");
  948. net = rio_scan_alloc_net(mport, 0, 0);
  949. if (!net) {
  950. printk(KERN_ERR "RIO: Failed to allocate new net\n");
  951. goto bail;
  952. }
  953. /* Read DestID assigned by enumerator */
  954. rio_local_read_config_32(mport, RIO_DID_CSR,
  955. &mport->host_deviceid);
  956. mport->host_deviceid = RIO_GET_DID(mport->sys_size,
  957. mport->host_deviceid);
  958. if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
  959. 0, NULL, 0) < 0) {
  960. printk(KERN_INFO
  961. "RIO: master port %d device has failed discovery\n",
  962. mport->id);
  963. goto bail;
  964. }
  965. rio_build_route_tables(net);
  966. }
  967. return 0;
  968. bail:
  969. return -EBUSY;
  970. }
  971. static struct rio_scan rio_scan_ops = {
  972. .owner = THIS_MODULE,
  973. .enumerate = rio_enum_mport,
  974. .discover = rio_disc_mport,
  975. };
  976. static bool scan;
  977. module_param(scan, bool, 0);
  978. MODULE_PARM_DESC(scan, "Start RapidIO network enumeration/discovery "
  979. "(default = 0)");
  980. /**
  981. * rio_basic_attach:
  982. *
  983. * When this enumeration/discovery method is loaded as a module this function
  984. * registers its specific enumeration and discover routines for all available
  985. * RapidIO mport devices. The "scan" command line parameter controls ability of
  986. * the module to start RapidIO enumeration/discovery automatically.
  987. *
  988. * Returns 0 for success or -EIO if unable to register itself.
  989. *
  990. * This enumeration/discovery method cannot be unloaded and therefore does not
  991. * provide a matching cleanup_module routine.
  992. */
  993. static int __init rio_basic_attach(void)
  994. {
  995. if (rio_register_scan(RIO_MPORT_ANY, &rio_scan_ops))
  996. return -EIO;
  997. if (scan)
  998. rio_init_mports();
  999. return 0;
  1000. }
  1001. late_initcall(rio_basic_attach);
  1002. MODULE_DESCRIPTION("Basic RapidIO enumeration/discovery");
  1003. MODULE_LICENSE("GPL");