rio-scan.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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. } else {
  365. rio_mport_read_config_32(port, destid, hopcount,
  366. RIO_COMPONENT_TAG_CSR,
  367. &rdev->comp_tag);
  368. }
  369. if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
  370. if (do_enum) {
  371. rio_set_device_id(port, destid, hopcount, next_destid);
  372. rdev->destid = next_destid;
  373. next_destid = rio_destid_alloc(net);
  374. } else
  375. rdev->destid = rio_get_device_id(port, destid, hopcount);
  376. rdev->hopcount = 0xff;
  377. } else {
  378. /* Switch device has an associated destID which
  379. * will be adjusted later
  380. */
  381. rdev->destid = destid;
  382. rdev->hopcount = hopcount;
  383. }
  384. /* If a PE has both switch and other functions, show it as a switch */
  385. if (rio_is_switch(rdev)) {
  386. rswitch = rdev->rswitch;
  387. rswitch->switchid = rdev->comp_tag & RIO_CTAG_UDEVID;
  388. rswitch->port_ok = 0;
  389. rswitch->route_table = kzalloc(sizeof(u8)*
  390. RIO_MAX_ROUTE_ENTRIES(port->sys_size),
  391. GFP_KERNEL);
  392. if (!rswitch->route_table)
  393. goto cleanup;
  394. /* Initialize switch route table */
  395. for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
  396. rdid++)
  397. rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
  398. dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
  399. rswitch->switchid);
  400. rio_switch_init(rdev, do_enum);
  401. if (do_enum && rswitch->clr_table)
  402. rswitch->clr_table(port, destid, hopcount,
  403. RIO_GLOBAL_TABLE);
  404. list_add_tail(&rswitch->node, &net->switches);
  405. } else {
  406. if (do_enum)
  407. /*Enable Input Output Port (transmitter reviever)*/
  408. rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
  409. dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
  410. rdev->destid);
  411. }
  412. rio_attach_device(rdev);
  413. device_initialize(&rdev->dev);
  414. rdev->dev.release = rio_release_dev;
  415. rio_dev_get(rdev);
  416. rdev->dma_mask = DMA_BIT_MASK(32);
  417. rdev->dev.dma_mask = &rdev->dma_mask;
  418. rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  419. if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
  420. rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
  421. 0, 0xffff);
  422. ret = rio_add_device(rdev);
  423. if (ret)
  424. goto cleanup;
  425. return rdev;
  426. cleanup:
  427. if (rswitch)
  428. kfree(rswitch->route_table);
  429. kfree(rdev);
  430. return NULL;
  431. }
  432. /**
  433. * rio_sport_is_active- Tests if a switch port has an active connection.
  434. * @port: Master port to send transaction
  435. * @destid: Associated destination ID for switch
  436. * @hopcount: Hopcount to reach switch
  437. * @sport: Switch port number
  438. *
  439. * Reads the port error status CSR for a particular switch port to
  440. * determine if the port has an active link. Returns
  441. * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
  442. * inactive.
  443. */
  444. static int
  445. rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
  446. {
  447. u32 result = 0;
  448. u32 ext_ftr_ptr;
  449. ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
  450. while (ext_ftr_ptr) {
  451. rio_mport_read_config_32(port, destid, hopcount,
  452. ext_ftr_ptr, &result);
  453. result = RIO_GET_BLOCK_ID(result);
  454. if ((result == RIO_EFB_SER_EP_FREE_ID) ||
  455. (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
  456. (result == RIO_EFB_SER_EP_FREC_ID))
  457. break;
  458. ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
  459. ext_ftr_ptr);
  460. }
  461. if (ext_ftr_ptr)
  462. rio_mport_read_config_32(port, destid, hopcount,
  463. ext_ftr_ptr +
  464. RIO_PORT_N_ERR_STS_CSR(sport),
  465. &result);
  466. return result & RIO_PORT_N_ERR_STS_PORT_OK;
  467. }
  468. /**
  469. * rio_lock_device - Acquires host device lock for specified device
  470. * @port: Master port to send transaction
  471. * @destid: Destination ID for device/switch
  472. * @hopcount: Hopcount to reach switch
  473. * @wait_ms: Max wait time in msec (0 = no timeout)
  474. *
  475. * Attepts to acquire host device lock for specified device
  476. * Returns 0 if device lock acquired or EINVAL if timeout expires.
  477. */
  478. static int
  479. rio_lock_device(struct rio_mport *port, u16 destid, u8 hopcount, int wait_ms)
  480. {
  481. u32 result;
  482. int tcnt = 0;
  483. /* Attempt to acquire device lock */
  484. rio_mport_write_config_32(port, destid, hopcount,
  485. RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  486. rio_mport_read_config_32(port, destid, hopcount,
  487. RIO_HOST_DID_LOCK_CSR, &result);
  488. while (result != port->host_deviceid) {
  489. if (wait_ms != 0 && tcnt == wait_ms) {
  490. pr_debug("RIO: timeout when locking device %x:%x\n",
  491. destid, hopcount);
  492. return -EINVAL;
  493. }
  494. /* Delay a bit */
  495. mdelay(1);
  496. tcnt++;
  497. /* Try to acquire device lock again */
  498. rio_mport_write_config_32(port, destid,
  499. hopcount,
  500. RIO_HOST_DID_LOCK_CSR,
  501. port->host_deviceid);
  502. rio_mport_read_config_32(port, destid,
  503. hopcount,
  504. RIO_HOST_DID_LOCK_CSR, &result);
  505. }
  506. return 0;
  507. }
  508. /**
  509. * rio_unlock_device - Releases host device lock for specified device
  510. * @port: Master port to send transaction
  511. * @destid: Destination ID for device/switch
  512. * @hopcount: Hopcount to reach switch
  513. *
  514. * Returns 0 if device lock released or EINVAL if fails.
  515. */
  516. static int
  517. rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
  518. {
  519. u32 result;
  520. /* Release device lock */
  521. rio_mport_write_config_32(port, destid,
  522. hopcount,
  523. RIO_HOST_DID_LOCK_CSR,
  524. port->host_deviceid);
  525. rio_mport_read_config_32(port, destid, hopcount,
  526. RIO_HOST_DID_LOCK_CSR, &result);
  527. if ((result & 0xffff) != 0xffff) {
  528. pr_debug("RIO: badness when releasing device lock %x:%x\n",
  529. destid, hopcount);
  530. return -EINVAL;
  531. }
  532. return 0;
  533. }
  534. /**
  535. * rio_route_add_entry- Add a route entry to a switch routing table
  536. * @rdev: RIO device
  537. * @table: Routing table ID
  538. * @route_destid: Destination ID to be routed
  539. * @route_port: Port number to be routed
  540. * @lock: lock switch device flag
  541. *
  542. * Calls the switch specific add_entry() method to add a route entry
  543. * on a switch. The route table can be specified using the @table
  544. * argument if a switch has per port routing tables or the normal
  545. * use is to specific all tables (or the global table) by passing
  546. * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
  547. * on failure.
  548. */
  549. static int
  550. rio_route_add_entry(struct rio_dev *rdev,
  551. u16 table, u16 route_destid, u8 route_port, int lock)
  552. {
  553. int rc;
  554. if (lock) {
  555. rc = rio_lock_device(rdev->net->hport, rdev->destid,
  556. rdev->hopcount, 1000);
  557. if (rc)
  558. return rc;
  559. }
  560. rc = rdev->rswitch->add_entry(rdev->net->hport, rdev->destid,
  561. rdev->hopcount, table,
  562. route_destid, route_port);
  563. if (lock)
  564. rio_unlock_device(rdev->net->hport, rdev->destid,
  565. rdev->hopcount);
  566. return rc;
  567. }
  568. /**
  569. * rio_route_get_entry- Read a route entry in a switch routing table
  570. * @rdev: RIO device
  571. * @table: Routing table ID
  572. * @route_destid: Destination ID to be routed
  573. * @route_port: Pointer to read port number into
  574. * @lock: lock switch device flag
  575. *
  576. * Calls the switch specific get_entry() method to read a route entry
  577. * in a switch. The route table can be specified using the @table
  578. * argument if a switch has per port routing tables or the normal
  579. * use is to specific all tables (or the global table) by passing
  580. * %RIO_GLOBAL_TABLE in @table. Returns %0 on success or %-EINVAL
  581. * on failure.
  582. */
  583. static int
  584. rio_route_get_entry(struct rio_dev *rdev, u16 table,
  585. u16 route_destid, u8 *route_port, int lock)
  586. {
  587. int rc;
  588. if (lock) {
  589. rc = rio_lock_device(rdev->net->hport, rdev->destid,
  590. rdev->hopcount, 1000);
  591. if (rc)
  592. return rc;
  593. }
  594. rc = rdev->rswitch->get_entry(rdev->net->hport, rdev->destid,
  595. rdev->hopcount, table,
  596. route_destid, route_port);
  597. if (lock)
  598. rio_unlock_device(rdev->net->hport, rdev->destid,
  599. rdev->hopcount);
  600. return rc;
  601. }
  602. /**
  603. * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
  604. * @port: Master port to send transaction
  605. * @hopcount: Number of hops to the device
  606. *
  607. * Used during enumeration to read the Host Device ID Lock CSR on a
  608. * RIO device. Returns the value of the lock register.
  609. */
  610. static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
  611. {
  612. u32 result;
  613. rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
  614. RIO_HOST_DID_LOCK_CSR, &result);
  615. return (u16) (result & 0xffff);
  616. }
  617. /**
  618. * rio_enum_peer- Recursively enumerate a RIO network through a master port
  619. * @net: RIO network being enumerated
  620. * @port: Master port to send transactions
  621. * @hopcount: Number of hops into the network
  622. * @prev: Previous RIO device connected to the enumerated one
  623. * @prev_port: Port on previous RIO device
  624. *
  625. * Recursively enumerates a RIO network. Transactions are sent via the
  626. * master port passed in @port.
  627. */
  628. static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
  629. u8 hopcount, struct rio_dev *prev, int prev_port)
  630. {
  631. struct rio_dev *rdev;
  632. u32 regval;
  633. int tmp;
  634. if (rio_mport_chk_dev_access(port,
  635. RIO_ANY_DESTID(port->sys_size), hopcount)) {
  636. pr_debug("RIO: device access check failed\n");
  637. return -1;
  638. }
  639. if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
  640. pr_debug("RIO: PE already discovered by this host\n");
  641. /*
  642. * Already discovered by this host. Add it as another
  643. * link to the existing device.
  644. */
  645. rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
  646. hopcount, RIO_COMPONENT_TAG_CSR, &regval);
  647. if (regval) {
  648. rdev = rio_get_comptag((regval & 0xffff), NULL);
  649. if (rdev && prev && rio_is_switch(prev)) {
  650. pr_debug("RIO: redundant path to %s\n",
  651. rio_name(rdev));
  652. prev->rswitch->nextdev[prev_port] = rdev;
  653. }
  654. }
  655. return 0;
  656. }
  657. /* Attempt to acquire device lock */
  658. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  659. hopcount,
  660. RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  661. while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
  662. < port->host_deviceid) {
  663. /* Delay a bit */
  664. mdelay(1);
  665. /* Attempt to acquire device lock again */
  666. rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  667. hopcount,
  668. RIO_HOST_DID_LOCK_CSR,
  669. port->host_deviceid);
  670. }
  671. if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
  672. pr_debug(
  673. "RIO: PE locked by a higher priority host...retreating\n");
  674. return -1;
  675. }
  676. /* Setup new RIO device */
  677. rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
  678. hopcount, 1);
  679. if (rdev) {
  680. /* Add device to the global and bus/net specific list. */
  681. list_add_tail(&rdev->net_list, &net->devices);
  682. rdev->prev = prev;
  683. if (prev && rio_is_switch(prev))
  684. prev->rswitch->nextdev[prev_port] = rdev;
  685. } else
  686. return -1;
  687. if (rio_is_switch(rdev)) {
  688. int sw_destid;
  689. int cur_destid;
  690. int sw_inport;
  691. u16 destid;
  692. int port_num;
  693. sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
  694. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  695. port->host_deviceid, sw_inport, 0);
  696. rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
  697. destid = rio_destid_first(net);
  698. while (destid != RIO_INVALID_DESTID && destid < next_destid) {
  699. if (destid != port->host_deviceid) {
  700. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  701. destid, sw_inport, 0);
  702. rdev->rswitch->route_table[destid] = sw_inport;
  703. }
  704. destid = rio_destid_next(net, destid + 1);
  705. }
  706. pr_debug(
  707. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  708. rio_name(rdev), rdev->vid, rdev->did,
  709. RIO_GET_TOTAL_PORTS(rdev->swpinfo));
  710. sw_destid = next_destid;
  711. for (port_num = 0;
  712. port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  713. port_num++) {
  714. if (sw_inport == port_num) {
  715. rio_enable_rx_tx_port(port, 0,
  716. RIO_ANY_DESTID(port->sys_size),
  717. hopcount, port_num);
  718. rdev->rswitch->port_ok |= (1 << port_num);
  719. continue;
  720. }
  721. cur_destid = next_destid;
  722. if (rio_sport_is_active
  723. (port, RIO_ANY_DESTID(port->sys_size), hopcount,
  724. port_num)) {
  725. pr_debug(
  726. "RIO: scanning device on port %d\n",
  727. port_num);
  728. rio_enable_rx_tx_port(port, 0,
  729. RIO_ANY_DESTID(port->sys_size),
  730. hopcount, port_num);
  731. rdev->rswitch->port_ok |= (1 << port_num);
  732. rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  733. RIO_ANY_DESTID(port->sys_size),
  734. port_num, 0);
  735. if (rio_enum_peer(net, port, hopcount + 1,
  736. rdev, port_num) < 0)
  737. return -1;
  738. /* Update routing tables */
  739. destid = rio_destid_next(net, cur_destid + 1);
  740. if (destid != RIO_INVALID_DESTID) {
  741. for (destid = cur_destid;
  742. destid < next_destid;) {
  743. if (destid != port->host_deviceid) {
  744. rio_route_add_entry(rdev,
  745. RIO_GLOBAL_TABLE,
  746. destid,
  747. port_num,
  748. 0);
  749. rdev->rswitch->
  750. route_table[destid] =
  751. port_num;
  752. }
  753. destid = rio_destid_next(net,
  754. destid + 1);
  755. }
  756. }
  757. } else {
  758. /* If switch supports Error Management,
  759. * set PORT_LOCKOUT bit for unused port
  760. */
  761. if (rdev->em_efptr)
  762. rio_set_port_lockout(rdev, port_num, 1);
  763. rdev->rswitch->port_ok &= ~(1 << port_num);
  764. }
  765. }
  766. /* Direct Port-write messages to the enumeratiing host */
  767. if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
  768. (rdev->em_efptr)) {
  769. rio_write_config_32(rdev,
  770. rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
  771. (port->host_deviceid << 16) |
  772. (port->sys_size << 15));
  773. }
  774. rio_init_em(rdev);
  775. /* Check for empty switch */
  776. if (next_destid == sw_destid)
  777. next_destid = rio_destid_alloc(net);
  778. rdev->destid = sw_destid;
  779. } else
  780. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  781. rio_name(rdev), rdev->vid, rdev->did);
  782. return 0;
  783. }
  784. /**
  785. * rio_enum_complete- Tests if enumeration of a network is complete
  786. * @port: Master port to send transaction
  787. *
  788. * Tests the PGCCSR discovered bit for non-zero value (enumeration
  789. * complete flag). Return %1 if enumeration is complete or %0 if
  790. * enumeration is incomplete.
  791. */
  792. static int rio_enum_complete(struct rio_mport *port)
  793. {
  794. u32 regval;
  795. rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  796. &regval);
  797. return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
  798. }
  799. /**
  800. * rio_disc_peer- Recursively discovers a RIO network through a master port
  801. * @net: RIO network being discovered
  802. * @port: Master port to send transactions
  803. * @destid: Current destination ID in network
  804. * @hopcount: Number of hops into the network
  805. * @prev: previous rio_dev
  806. * @prev_port: previous port number
  807. *
  808. * Recursively discovers a RIO network. Transactions are sent via the
  809. * master port passed in @port.
  810. */
  811. static int
  812. rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
  813. u8 hopcount, struct rio_dev *prev, int prev_port)
  814. {
  815. u8 port_num, route_port;
  816. struct rio_dev *rdev;
  817. u16 ndestid;
  818. /* Setup new RIO device */
  819. if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
  820. /* Add device to the global and bus/net specific list. */
  821. list_add_tail(&rdev->net_list, &net->devices);
  822. rdev->prev = prev;
  823. if (prev && rio_is_switch(prev))
  824. prev->rswitch->nextdev[prev_port] = rdev;
  825. } else
  826. return -1;
  827. if (rio_is_switch(rdev)) {
  828. /* Associated destid is how we accessed this switch */
  829. rdev->destid = destid;
  830. pr_debug(
  831. "RIO: found %s (vid %4.4x did %4.4x) with %d ports\n",
  832. rio_name(rdev), rdev->vid, rdev->did,
  833. RIO_GET_TOTAL_PORTS(rdev->swpinfo));
  834. for (port_num = 0;
  835. port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  836. port_num++) {
  837. if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
  838. continue;
  839. if (rio_sport_is_active
  840. (port, destid, hopcount, port_num)) {
  841. pr_debug(
  842. "RIO: scanning device on port %d\n",
  843. port_num);
  844. rio_lock_device(port, destid, hopcount, 1000);
  845. for (ndestid = 0;
  846. ndestid < RIO_ANY_DESTID(port->sys_size);
  847. ndestid++) {
  848. rio_route_get_entry(rdev,
  849. RIO_GLOBAL_TABLE,
  850. ndestid,
  851. &route_port, 0);
  852. if (route_port == port_num)
  853. break;
  854. }
  855. if (ndestid == RIO_ANY_DESTID(port->sys_size))
  856. continue;
  857. rio_unlock_device(port, destid, hopcount);
  858. if (rio_disc_peer(net, port, ndestid,
  859. hopcount + 1, rdev, port_num) < 0)
  860. return -1;
  861. }
  862. }
  863. } else
  864. pr_debug("RIO: found %s (vid %4.4x did %4.4x)\n",
  865. rio_name(rdev), rdev->vid, rdev->did);
  866. return 0;
  867. }
  868. /**
  869. * rio_mport_is_active- Tests if master port link is active
  870. * @port: Master port to test
  871. *
  872. * Reads the port error status CSR for the master port to
  873. * determine if the port has an active link. Returns
  874. * %RIO_PORT_N_ERR_STS_PORT_OK if the master port is active
  875. * or %0 if it is inactive.
  876. */
  877. static int rio_mport_is_active(struct rio_mport *port)
  878. {
  879. u32 result = 0;
  880. u32 ext_ftr_ptr;
  881. int *entry = rio_mport_phys_table;
  882. do {
  883. if ((ext_ftr_ptr =
  884. rio_mport_get_feature(port, 1, 0, 0, *entry)))
  885. break;
  886. } while (*++entry >= 0);
  887. if (ext_ftr_ptr)
  888. rio_local_read_config_32(port,
  889. ext_ftr_ptr +
  890. RIO_PORT_N_ERR_STS_CSR(port->index),
  891. &result);
  892. return result & RIO_PORT_N_ERR_STS_PORT_OK;
  893. }
  894. /**
  895. * rio_alloc_net- Allocate and configure a new RIO network
  896. * @port: Master port associated with the RIO network
  897. * @do_enum: Enumeration/Discovery mode flag
  898. * @start: logical minimal start id for new net
  899. *
  900. * Allocates a RIO network structure, initializes per-network
  901. * list heads, and adds the associated master port to the
  902. * network list of associated master ports. Returns a
  903. * RIO network pointer on success or %NULL on failure.
  904. */
  905. static struct rio_net *rio_alloc_net(struct rio_mport *port,
  906. int do_enum, u16 start)
  907. {
  908. struct rio_net *net;
  909. net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
  910. if (net && do_enum) {
  911. net->destid_table.table = kcalloc(
  912. BITS_TO_LONGS(RIO_MAX_ROUTE_ENTRIES(port->sys_size)),
  913. sizeof(long),
  914. GFP_KERNEL);
  915. if (net->destid_table.table == NULL) {
  916. pr_err("RIO: failed to allocate destID table\n");
  917. kfree(net);
  918. net = NULL;
  919. } else {
  920. net->destid_table.start = start;
  921. net->destid_table.max =
  922. RIO_MAX_ROUTE_ENTRIES(port->sys_size);
  923. spin_lock_init(&net->destid_table.lock);
  924. }
  925. }
  926. if (net) {
  927. INIT_LIST_HEAD(&net->node);
  928. INIT_LIST_HEAD(&net->devices);
  929. INIT_LIST_HEAD(&net->switches);
  930. INIT_LIST_HEAD(&net->mports);
  931. list_add_tail(&port->nnode, &net->mports);
  932. net->hport = port;
  933. net->id = port->id;
  934. }
  935. return net;
  936. }
  937. /**
  938. * rio_update_route_tables- Updates route tables in switches
  939. * @net: RIO network to run update on
  940. *
  941. * For each enumerated device, ensure that each switch in a system
  942. * has correct routing entries. Add routes for devices that where
  943. * unknown dirung the first enumeration pass through the switch.
  944. */
  945. static void rio_update_route_tables(struct rio_net *net)
  946. {
  947. struct rio_dev *rdev, *swrdev;
  948. struct rio_switch *rswitch;
  949. u8 sport;
  950. u16 destid;
  951. list_for_each_entry(rdev, &net->devices, net_list) {
  952. destid = rdev->destid;
  953. list_for_each_entry(rswitch, &net->switches, node) {
  954. if (rio_is_switch(rdev) && (rdev->rswitch == rswitch))
  955. continue;
  956. if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
  957. swrdev = sw_to_rio_dev(rswitch);
  958. /* Skip if destid ends in empty switch*/
  959. if (swrdev->destid == destid)
  960. continue;
  961. sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
  962. if (rswitch->add_entry) {
  963. rio_route_add_entry(swrdev,
  964. RIO_GLOBAL_TABLE, destid,
  965. sport, 0);
  966. rswitch->route_table[destid] = sport;
  967. }
  968. }
  969. }
  970. }
  971. }
  972. /**
  973. * rio_init_em - Initializes RIO Error Management (for switches)
  974. * @rdev: RIO device
  975. *
  976. * For each enumerated switch, call device-specific error management
  977. * initialization routine (if supplied by the switch driver).
  978. */
  979. static void rio_init_em(struct rio_dev *rdev)
  980. {
  981. if (rio_is_switch(rdev) && (rdev->em_efptr) &&
  982. (rdev->rswitch->em_init)) {
  983. rdev->rswitch->em_init(rdev);
  984. }
  985. }
  986. /**
  987. * rio_pw_enable - Enables/disables port-write handling by a master port
  988. * @port: Master port associated with port-write handling
  989. * @enable: 1=enable, 0=disable
  990. */
  991. static void rio_pw_enable(struct rio_mport *port, int enable)
  992. {
  993. if (port->ops->pwenable)
  994. port->ops->pwenable(port, enable);
  995. }
  996. /**
  997. * rio_enum_mport- Start enumeration through a master port
  998. * @mport: Master port to send transactions
  999. *
  1000. * Starts the enumeration process. If somebody has enumerated our
  1001. * master port device, then give up. If not and we have an active
  1002. * link, then start recursive peer enumeration. Returns %0 if
  1003. * enumeration succeeds or %-EBUSY if enumeration fails.
  1004. */
  1005. int rio_enum_mport(struct rio_mport *mport)
  1006. {
  1007. struct rio_net *net = NULL;
  1008. int rc = 0;
  1009. printk(KERN_INFO "RIO: enumerate master port %d, %s\n", mport->id,
  1010. mport->name);
  1011. /* If somebody else enumerated our master port device, bail. */
  1012. if (rio_enum_host(mport) < 0) {
  1013. printk(KERN_INFO
  1014. "RIO: master port %d device has been enumerated by a remote host\n",
  1015. mport->id);
  1016. rc = -EBUSY;
  1017. goto out;
  1018. }
  1019. /* If master port has an active link, allocate net and enum peers */
  1020. if (rio_mport_is_active(mport)) {
  1021. net = rio_alloc_net(mport, 1, 0);
  1022. if (!net) {
  1023. printk(KERN_ERR "RIO: failed to allocate new net\n");
  1024. rc = -ENOMEM;
  1025. goto out;
  1026. }
  1027. /* reserve mport destID in new net */
  1028. rio_destid_reserve(net, mport->host_deviceid);
  1029. /* Enable Input Output Port (transmitter reviever) */
  1030. rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
  1031. /* Set component tag for host */
  1032. rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
  1033. next_comptag++);
  1034. next_destid = rio_destid_alloc(net);
  1035. if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
  1036. /* A higher priority host won enumeration, bail. */
  1037. printk(KERN_INFO
  1038. "RIO: master port %d device has lost enumeration to a remote host\n",
  1039. mport->id);
  1040. rio_clear_locks(net);
  1041. rc = -EBUSY;
  1042. goto out;
  1043. }
  1044. /* free the last allocated destID (unused) */
  1045. rio_destid_free(net, next_destid);
  1046. rio_update_route_tables(net);
  1047. rio_clear_locks(net);
  1048. rio_pw_enable(mport, 1);
  1049. } else {
  1050. printk(KERN_INFO "RIO: master port %d link inactive\n",
  1051. mport->id);
  1052. rc = -EINVAL;
  1053. }
  1054. out:
  1055. return rc;
  1056. }
  1057. /**
  1058. * rio_build_route_tables- Generate route tables from switch route entries
  1059. * @net: RIO network to run route tables scan on
  1060. *
  1061. * For each switch device, generate a route table by copying existing
  1062. * route entries from the switch.
  1063. */
  1064. static void rio_build_route_tables(struct rio_net *net)
  1065. {
  1066. struct rio_switch *rswitch;
  1067. struct rio_dev *rdev;
  1068. int i;
  1069. u8 sport;
  1070. list_for_each_entry(rswitch, &net->switches, node) {
  1071. rdev = sw_to_rio_dev(rswitch);
  1072. rio_lock_device(net->hport, rdev->destid,
  1073. rdev->hopcount, 1000);
  1074. for (i = 0;
  1075. i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
  1076. i++) {
  1077. if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
  1078. i, &sport, 0) < 0)
  1079. continue;
  1080. rswitch->route_table[i] = sport;
  1081. }
  1082. rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
  1083. }
  1084. }
  1085. /**
  1086. * rio_disc_mport- Start discovery through a master port
  1087. * @mport: Master port to send transactions
  1088. *
  1089. * Starts the discovery process. If we have an active link,
  1090. * then wait for the signal that enumeration is complete.
  1091. * When enumeration completion is signaled, start recursive
  1092. * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
  1093. * on failure.
  1094. */
  1095. int rio_disc_mport(struct rio_mport *mport)
  1096. {
  1097. struct rio_net *net = NULL;
  1098. unsigned long to_end;
  1099. printk(KERN_INFO "RIO: discover master port %d, %s\n", mport->id,
  1100. mport->name);
  1101. /* If master port has an active link, allocate net and discover peers */
  1102. if (rio_mport_is_active(mport)) {
  1103. pr_debug("RIO: wait for enumeration to complete...\n");
  1104. to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
  1105. while (time_before(jiffies, to_end)) {
  1106. if (rio_enum_complete(mport))
  1107. goto enum_done;
  1108. msleep(10);
  1109. }
  1110. pr_debug("RIO: discovery timeout on mport %d %s\n",
  1111. mport->id, mport->name);
  1112. goto bail;
  1113. enum_done:
  1114. pr_debug("RIO: ... enumeration done\n");
  1115. net = rio_alloc_net(mport, 0, 0);
  1116. if (!net) {
  1117. printk(KERN_ERR "RIO: Failed to allocate new net\n");
  1118. goto bail;
  1119. }
  1120. /* Read DestID assigned by enumerator */
  1121. rio_local_read_config_32(mport, RIO_DID_CSR,
  1122. &mport->host_deviceid);
  1123. mport->host_deviceid = RIO_GET_DID(mport->sys_size,
  1124. mport->host_deviceid);
  1125. if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
  1126. 0, NULL, 0) < 0) {
  1127. printk(KERN_INFO
  1128. "RIO: master port %d device has failed discovery\n",
  1129. mport->id);
  1130. goto bail;
  1131. }
  1132. rio_build_route_tables(net);
  1133. }
  1134. return 0;
  1135. bail:
  1136. return -EBUSY;
  1137. }
  1138. static struct rio_scan rio_scan_ops = {
  1139. .enumerate = rio_enum_mport,
  1140. .discover = rio_disc_mport,
  1141. };
  1142. static bool scan;
  1143. module_param(scan, bool, 0);
  1144. MODULE_PARM_DESC(scan, "Start RapidIO network enumeration/discovery "
  1145. "(default = 0)");
  1146. /**
  1147. * rio_basic_attach:
  1148. *
  1149. * When this enumeration/discovery method is loaded as a module this function
  1150. * registers its specific enumeration and discover routines for all available
  1151. * RapidIO mport devices. The "scan" command line parameter controls ability of
  1152. * the module to start RapidIO enumeration/discovery automatically.
  1153. *
  1154. * Returns 0 for success or -EIO if unable to register itself.
  1155. *
  1156. * This enumeration/discovery method cannot be unloaded and therefore does not
  1157. * provide a matching cleanup_module routine.
  1158. */
  1159. static int __init rio_basic_attach(void)
  1160. {
  1161. if (rio_register_scan(RIO_MPORT_ANY, &rio_scan_ops))
  1162. return -EIO;
  1163. if (scan)
  1164. rio_init_mports();
  1165. return 0;
  1166. }
  1167. late_initcall(rio_basic_attach);
  1168. MODULE_DESCRIPTION("Basic RapidIO enumeration/discovery");
  1169. MODULE_LICENSE("GPL");