rio-scan.c 32 KB

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