rio.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /*
  2. * RapidIO interconnect services
  3. * (RapidIO Interconnect Specification, http://www.rapidio.org)
  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. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/delay.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/slab.h>
  28. #include <linux/interrupt.h>
  29. #include "rio.h"
  30. static LIST_HEAD(rio_mports);
  31. static unsigned char next_portid;
  32. /**
  33. * rio_local_get_device_id - Get the base/extended device id for a port
  34. * @port: RIO master port from which to get the deviceid
  35. *
  36. * Reads the base/extended device id from the local device
  37. * implementing the master port. Returns the 8/16-bit device
  38. * id.
  39. */
  40. u16 rio_local_get_device_id(struct rio_mport *port)
  41. {
  42. u32 result;
  43. rio_local_read_config_32(port, RIO_DID_CSR, &result);
  44. return (RIO_GET_DID(port->sys_size, result));
  45. }
  46. /**
  47. * rio_request_inb_mbox - request inbound mailbox service
  48. * @mport: RIO master port from which to allocate the mailbox resource
  49. * @dev_id: Device specific pointer to pass on event
  50. * @mbox: Mailbox number to claim
  51. * @entries: Number of entries in inbound mailbox queue
  52. * @minb: Callback to execute when inbound message is received
  53. *
  54. * Requests ownership of an inbound mailbox resource and binds
  55. * a callback function to the resource. Returns %0 on success.
  56. */
  57. int rio_request_inb_mbox(struct rio_mport *mport,
  58. void *dev_id,
  59. int mbox,
  60. int entries,
  61. void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
  62. int slot))
  63. {
  64. int rc = -ENOSYS;
  65. struct resource *res;
  66. if (mport->ops->open_inb_mbox == NULL)
  67. goto out;
  68. res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  69. if (res) {
  70. rio_init_mbox_res(res, mbox, mbox);
  71. /* Make sure this mailbox isn't in use */
  72. if ((rc =
  73. request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
  74. res)) < 0) {
  75. kfree(res);
  76. goto out;
  77. }
  78. mport->inb_msg[mbox].res = res;
  79. /* Hook the inbound message callback */
  80. mport->inb_msg[mbox].mcback = minb;
  81. rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
  82. } else
  83. rc = -ENOMEM;
  84. out:
  85. return rc;
  86. }
  87. /**
  88. * rio_release_inb_mbox - release inbound mailbox message service
  89. * @mport: RIO master port from which to release the mailbox resource
  90. * @mbox: Mailbox number to release
  91. *
  92. * Releases ownership of an inbound mailbox resource. Returns 0
  93. * if the request has been satisfied.
  94. */
  95. int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
  96. {
  97. if (mport->ops->close_inb_mbox) {
  98. mport->ops->close_inb_mbox(mport, mbox);
  99. /* Release the mailbox resource */
  100. return release_resource(mport->inb_msg[mbox].res);
  101. } else
  102. return -ENOSYS;
  103. }
  104. /**
  105. * rio_request_outb_mbox - request outbound mailbox service
  106. * @mport: RIO master port from which to allocate the mailbox resource
  107. * @dev_id: Device specific pointer to pass on event
  108. * @mbox: Mailbox number to claim
  109. * @entries: Number of entries in outbound mailbox queue
  110. * @moutb: Callback to execute when outbound message is sent
  111. *
  112. * Requests ownership of an outbound mailbox resource and binds
  113. * a callback function to the resource. Returns 0 on success.
  114. */
  115. int rio_request_outb_mbox(struct rio_mport *mport,
  116. void *dev_id,
  117. int mbox,
  118. int entries,
  119. void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
  120. {
  121. int rc = -ENOSYS;
  122. struct resource *res;
  123. if (mport->ops->open_outb_mbox == NULL)
  124. goto out;
  125. res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  126. if (res) {
  127. rio_init_mbox_res(res, mbox, mbox);
  128. /* Make sure this outbound mailbox isn't in use */
  129. if ((rc =
  130. request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
  131. res)) < 0) {
  132. kfree(res);
  133. goto out;
  134. }
  135. mport->outb_msg[mbox].res = res;
  136. /* Hook the inbound message callback */
  137. mport->outb_msg[mbox].mcback = moutb;
  138. rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
  139. } else
  140. rc = -ENOMEM;
  141. out:
  142. return rc;
  143. }
  144. /**
  145. * rio_release_outb_mbox - release outbound mailbox message service
  146. * @mport: RIO master port from which to release the mailbox resource
  147. * @mbox: Mailbox number to release
  148. *
  149. * Releases ownership of an inbound mailbox resource. Returns 0
  150. * if the request has been satisfied.
  151. */
  152. int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
  153. {
  154. if (mport->ops->close_outb_mbox) {
  155. mport->ops->close_outb_mbox(mport, mbox);
  156. /* Release the mailbox resource */
  157. return release_resource(mport->outb_msg[mbox].res);
  158. } else
  159. return -ENOSYS;
  160. }
  161. /**
  162. * rio_setup_inb_dbell - bind inbound doorbell callback
  163. * @mport: RIO master port to bind the doorbell callback
  164. * @dev_id: Device specific pointer to pass on event
  165. * @res: Doorbell message resource
  166. * @dinb: Callback to execute when doorbell is received
  167. *
  168. * Adds a doorbell resource/callback pair into a port's
  169. * doorbell event list. Returns 0 if the request has been
  170. * satisfied.
  171. */
  172. static int
  173. rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
  174. void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
  175. u16 info))
  176. {
  177. int rc = 0;
  178. struct rio_dbell *dbell;
  179. if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
  180. rc = -ENOMEM;
  181. goto out;
  182. }
  183. dbell->res = res;
  184. dbell->dinb = dinb;
  185. dbell->dev_id = dev_id;
  186. list_add_tail(&dbell->node, &mport->dbells);
  187. out:
  188. return rc;
  189. }
  190. /**
  191. * rio_request_inb_dbell - request inbound doorbell message service
  192. * @mport: RIO master port from which to allocate the doorbell resource
  193. * @dev_id: Device specific pointer to pass on event
  194. * @start: Doorbell info range start
  195. * @end: Doorbell info range end
  196. * @dinb: Callback to execute when doorbell is received
  197. *
  198. * Requests ownership of an inbound doorbell resource and binds
  199. * a callback function to the resource. Returns 0 if the request
  200. * has been satisfied.
  201. */
  202. int rio_request_inb_dbell(struct rio_mport *mport,
  203. void *dev_id,
  204. u16 start,
  205. u16 end,
  206. void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
  207. u16 dst, u16 info))
  208. {
  209. int rc = 0;
  210. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  211. if (res) {
  212. rio_init_dbell_res(res, start, end);
  213. /* Make sure these doorbells aren't in use */
  214. if ((rc =
  215. request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
  216. res)) < 0) {
  217. kfree(res);
  218. goto out;
  219. }
  220. /* Hook the doorbell callback */
  221. rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
  222. } else
  223. rc = -ENOMEM;
  224. out:
  225. return rc;
  226. }
  227. /**
  228. * rio_release_inb_dbell - release inbound doorbell message service
  229. * @mport: RIO master port from which to release the doorbell resource
  230. * @start: Doorbell info range start
  231. * @end: Doorbell info range end
  232. *
  233. * Releases ownership of an inbound doorbell resource and removes
  234. * callback from the doorbell event list. Returns 0 if the request
  235. * has been satisfied.
  236. */
  237. int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
  238. {
  239. int rc = 0, found = 0;
  240. struct rio_dbell *dbell;
  241. list_for_each_entry(dbell, &mport->dbells, node) {
  242. if ((dbell->res->start == start) && (dbell->res->end == end)) {
  243. found = 1;
  244. break;
  245. }
  246. }
  247. /* If we can't find an exact match, fail */
  248. if (!found) {
  249. rc = -EINVAL;
  250. goto out;
  251. }
  252. /* Delete from list */
  253. list_del(&dbell->node);
  254. /* Release the doorbell resource */
  255. rc = release_resource(dbell->res);
  256. /* Free the doorbell event */
  257. kfree(dbell);
  258. out:
  259. return rc;
  260. }
  261. /**
  262. * rio_request_outb_dbell - request outbound doorbell message range
  263. * @rdev: RIO device from which to allocate the doorbell resource
  264. * @start: Doorbell message range start
  265. * @end: Doorbell message range end
  266. *
  267. * Requests ownership of a doorbell message range. Returns a resource
  268. * if the request has been satisfied or %NULL on failure.
  269. */
  270. struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
  271. u16 end)
  272. {
  273. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  274. if (res) {
  275. rio_init_dbell_res(res, start, end);
  276. /* Make sure these doorbells aren't in use */
  277. if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
  278. < 0) {
  279. kfree(res);
  280. res = NULL;
  281. }
  282. }
  283. return res;
  284. }
  285. /**
  286. * rio_release_outb_dbell - release outbound doorbell message range
  287. * @rdev: RIO device from which to release the doorbell resource
  288. * @res: Doorbell resource to be freed
  289. *
  290. * Releases ownership of a doorbell message range. Returns 0 if the
  291. * request has been satisfied.
  292. */
  293. int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
  294. {
  295. int rc = release_resource(res);
  296. kfree(res);
  297. return rc;
  298. }
  299. /**
  300. * rio_request_inb_pwrite - request inbound port-write message service
  301. * @rdev: RIO device to which register inbound port-write callback routine
  302. * @pwcback: Callback routine to execute when port-write is received
  303. *
  304. * Binds a port-write callback function to the RapidIO device.
  305. * Returns 0 if the request has been satisfied.
  306. */
  307. int rio_request_inb_pwrite(struct rio_dev *rdev,
  308. int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
  309. {
  310. int rc = 0;
  311. spin_lock(&rio_global_list_lock);
  312. if (rdev->pwcback != NULL)
  313. rc = -ENOMEM;
  314. else
  315. rdev->pwcback = pwcback;
  316. spin_unlock(&rio_global_list_lock);
  317. return rc;
  318. }
  319. EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
  320. /**
  321. * rio_release_inb_pwrite - release inbound port-write message service
  322. * @rdev: RIO device which registered for inbound port-write callback
  323. *
  324. * Removes callback from the rio_dev structure. Returns 0 if the request
  325. * has been satisfied.
  326. */
  327. int rio_release_inb_pwrite(struct rio_dev *rdev)
  328. {
  329. int rc = -ENOMEM;
  330. spin_lock(&rio_global_list_lock);
  331. if (rdev->pwcback) {
  332. rdev->pwcback = NULL;
  333. rc = 0;
  334. }
  335. spin_unlock(&rio_global_list_lock);
  336. return rc;
  337. }
  338. EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
  339. /**
  340. * rio_mport_get_physefb - Helper function that returns register offset
  341. * for Physical Layer Extended Features Block.
  342. * @port: Master port to issue transaction
  343. * @local: Indicate a local master port or remote device access
  344. * @destid: Destination ID of the device
  345. * @hopcount: Number of switch hops to the device
  346. */
  347. u32
  348. rio_mport_get_physefb(struct rio_mport *port, int local,
  349. u16 destid, u8 hopcount)
  350. {
  351. u32 ext_ftr_ptr;
  352. u32 ftr_header;
  353. ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
  354. while (ext_ftr_ptr) {
  355. if (local)
  356. rio_local_read_config_32(port, ext_ftr_ptr,
  357. &ftr_header);
  358. else
  359. rio_mport_read_config_32(port, destid, hopcount,
  360. ext_ftr_ptr, &ftr_header);
  361. ftr_header = RIO_GET_BLOCK_ID(ftr_header);
  362. switch (ftr_header) {
  363. case RIO_EFB_SER_EP_ID_V13P:
  364. case RIO_EFB_SER_EP_REC_ID_V13P:
  365. case RIO_EFB_SER_EP_FREE_ID_V13P:
  366. case RIO_EFB_SER_EP_ID:
  367. case RIO_EFB_SER_EP_REC_ID:
  368. case RIO_EFB_SER_EP_FREE_ID:
  369. case RIO_EFB_SER_EP_FREC_ID:
  370. return ext_ftr_ptr;
  371. default:
  372. break;
  373. }
  374. ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
  375. hopcount, ext_ftr_ptr);
  376. }
  377. return ext_ftr_ptr;
  378. }
  379. /**
  380. * rio_get_comptag - Begin or continue searching for a RIO device by component tag
  381. * @comp_tag: RIO component tag to match
  382. * @from: Previous RIO device found in search, or %NULL for new search
  383. *
  384. * Iterates through the list of known RIO devices. If a RIO device is
  385. * found with a matching @comp_tag, a pointer to its device
  386. * structure is returned. Otherwise, %NULL is returned. A new search
  387. * is initiated by passing %NULL to the @from argument. Otherwise, if
  388. * @from is not %NULL, searches continue from next device on the global
  389. * list.
  390. */
  391. struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
  392. {
  393. struct list_head *n;
  394. struct rio_dev *rdev;
  395. spin_lock(&rio_global_list_lock);
  396. n = from ? from->global_list.next : rio_devices.next;
  397. while (n && (n != &rio_devices)) {
  398. rdev = rio_dev_g(n);
  399. if (rdev->comp_tag == comp_tag)
  400. goto exit;
  401. n = n->next;
  402. }
  403. rdev = NULL;
  404. exit:
  405. spin_unlock(&rio_global_list_lock);
  406. return rdev;
  407. }
  408. /**
  409. * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
  410. * @rdev: Pointer to RIO device control structure
  411. * @pnum: Switch port number to set LOCKOUT bit
  412. * @lock: Operation : set (=1) or clear (=0)
  413. */
  414. int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
  415. {
  416. u32 regval;
  417. rio_read_config_32(rdev,
  418. rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
  419. &regval);
  420. if (lock)
  421. regval |= RIO_PORT_N_CTL_LOCKOUT;
  422. else
  423. regval &= ~RIO_PORT_N_CTL_LOCKOUT;
  424. rio_write_config_32(rdev,
  425. rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
  426. regval);
  427. return 0;
  428. }
  429. /**
  430. * rio_chk_dev_route - Validate route to the specified device.
  431. * @rdev: RIO device failed to respond
  432. * @nrdev: Last active device on the route to rdev
  433. * @npnum: nrdev's port number on the route to rdev
  434. *
  435. * Follows a route to the specified RIO device to determine the last available
  436. * device (and corresponding RIO port) on the route.
  437. */
  438. static int
  439. rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
  440. {
  441. u32 result;
  442. int p_port, rc = -EIO;
  443. struct rio_dev *prev = NULL;
  444. /* Find switch with failed RIO link */
  445. while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
  446. if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
  447. prev = rdev->prev;
  448. break;
  449. }
  450. rdev = rdev->prev;
  451. }
  452. if (prev == NULL)
  453. goto err_out;
  454. p_port = prev->rswitch->route_table[rdev->destid];
  455. if (p_port != RIO_INVALID_ROUTE) {
  456. pr_debug("RIO: link failed on [%s]-P%d\n",
  457. rio_name(prev), p_port);
  458. *nrdev = prev;
  459. *npnum = p_port;
  460. rc = 0;
  461. } else
  462. pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
  463. err_out:
  464. return rc;
  465. }
  466. /**
  467. * rio_mport_chk_dev_access - Validate access to the specified device.
  468. * @mport: Master port to send transactions
  469. * @destid: Device destination ID in network
  470. * @hopcount: Number of hops into the network
  471. */
  472. int
  473. rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
  474. {
  475. int i = 0;
  476. u32 tmp;
  477. while (rio_mport_read_config_32(mport, destid, hopcount,
  478. RIO_DEV_ID_CAR, &tmp)) {
  479. i++;
  480. if (i == RIO_MAX_CHK_RETRY)
  481. return -EIO;
  482. mdelay(1);
  483. }
  484. return 0;
  485. }
  486. /**
  487. * rio_chk_dev_access - Validate access to the specified device.
  488. * @rdev: Pointer to RIO device control structure
  489. */
  490. static int rio_chk_dev_access(struct rio_dev *rdev)
  491. {
  492. return rio_mport_chk_dev_access(rdev->net->hport,
  493. rdev->destid, rdev->hopcount);
  494. }
  495. /**
  496. * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
  497. * returns link-response (if requested).
  498. * @rdev: RIO devive to issue Input-status command
  499. * @pnum: Device port number to issue the command
  500. * @lnkresp: Response from a link partner
  501. */
  502. static int
  503. rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
  504. {
  505. u32 regval;
  506. int checkcount;
  507. if (lnkresp) {
  508. /* Read from link maintenance response register
  509. * to clear valid bit */
  510. rio_read_config_32(rdev,
  511. rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
  512. &regval);
  513. udelay(50);
  514. }
  515. /* Issue Input-status command */
  516. rio_write_config_32(rdev,
  517. rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
  518. RIO_MNT_REQ_CMD_IS);
  519. /* Exit if the response is not expected */
  520. if (lnkresp == NULL)
  521. return 0;
  522. checkcount = 3;
  523. while (checkcount--) {
  524. udelay(50);
  525. rio_read_config_32(rdev,
  526. rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
  527. &regval);
  528. if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
  529. *lnkresp = regval;
  530. return 0;
  531. }
  532. }
  533. return -EIO;
  534. }
  535. /**
  536. * rio_clr_err_stopped - Clears port Error-stopped states.
  537. * @rdev: Pointer to RIO device control structure
  538. * @pnum: Switch port number to clear errors
  539. * @err_status: port error status (if 0 reads register from device)
  540. */
  541. static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
  542. {
  543. struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
  544. u32 regval;
  545. u32 far_ackid, far_linkstat, near_ackid;
  546. if (err_status == 0)
  547. rio_read_config_32(rdev,
  548. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  549. &err_status);
  550. if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
  551. pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
  552. /*
  553. * Send a Link-Request/Input-Status control symbol
  554. */
  555. if (rio_get_input_status(rdev, pnum, &regval)) {
  556. pr_debug("RIO_EM: Input-status response timeout\n");
  557. goto rd_err;
  558. }
  559. pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
  560. pnum, regval);
  561. far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
  562. far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
  563. rio_read_config_32(rdev,
  564. rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
  565. &regval);
  566. pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
  567. near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
  568. pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
  569. " near_ackID=0x%02x\n",
  570. pnum, far_ackid, far_linkstat, near_ackid);
  571. /*
  572. * If required, synchronize ackIDs of near and
  573. * far sides.
  574. */
  575. if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
  576. (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
  577. /* Align near outstanding/outbound ackIDs with
  578. * far inbound.
  579. */
  580. rio_write_config_32(rdev,
  581. rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
  582. (near_ackid << 24) |
  583. (far_ackid << 8) | far_ackid);
  584. /* Align far outstanding/outbound ackIDs with
  585. * near inbound.
  586. */
  587. far_ackid++;
  588. if (nextdev)
  589. rio_write_config_32(nextdev,
  590. nextdev->phys_efptr +
  591. RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
  592. (far_ackid << 24) |
  593. (near_ackid << 8) | near_ackid);
  594. else
  595. pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
  596. }
  597. rd_err:
  598. rio_read_config_32(rdev,
  599. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  600. &err_status);
  601. pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
  602. }
  603. if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
  604. pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
  605. rio_get_input_status(nextdev,
  606. RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
  607. udelay(50);
  608. rio_read_config_32(rdev,
  609. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  610. &err_status);
  611. pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
  612. }
  613. return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
  614. RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
  615. }
  616. /**
  617. * rio_inb_pwrite_handler - process inbound port-write message
  618. * @pw_msg: pointer to inbound port-write message
  619. *
  620. * Processes an inbound port-write message. Returns 0 if the request
  621. * has been satisfied.
  622. */
  623. int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
  624. {
  625. struct rio_dev *rdev;
  626. u32 err_status, em_perrdet, em_ltlerrdet;
  627. int rc, portnum;
  628. rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
  629. if (rdev == NULL) {
  630. /* Device removed or enumeration error */
  631. pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
  632. __func__, pw_msg->em.comptag);
  633. return -EIO;
  634. }
  635. pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
  636. #ifdef DEBUG_PW
  637. {
  638. u32 i;
  639. for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
  640. pr_debug("0x%02x: %08x %08x %08x %08x\n",
  641. i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
  642. pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
  643. i += 4;
  644. }
  645. }
  646. #endif
  647. /* Call an external service function (if such is registered
  648. * for this device). This may be the service for endpoints that send
  649. * device-specific port-write messages. End-point messages expected
  650. * to be handled completely by EP specific device driver.
  651. * For switches rc==0 signals that no standard processing required.
  652. */
  653. if (rdev->pwcback != NULL) {
  654. rc = rdev->pwcback(rdev, pw_msg, 0);
  655. if (rc == 0)
  656. return 0;
  657. }
  658. portnum = pw_msg->em.is_port & 0xFF;
  659. /* Check if device and route to it are functional:
  660. * Sometimes devices may send PW message(s) just before being
  661. * powered down (or link being lost).
  662. */
  663. if (rio_chk_dev_access(rdev)) {
  664. pr_debug("RIO: device access failed - get link partner\n");
  665. /* Scan route to the device and identify failed link.
  666. * This will replace device and port reported in PW message.
  667. * PW message should not be used after this point.
  668. */
  669. if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
  670. pr_err("RIO: Route trace for %s failed\n",
  671. rio_name(rdev));
  672. return -EIO;
  673. }
  674. pw_msg = NULL;
  675. }
  676. /* For End-point devices processing stops here */
  677. if (!(rdev->pef & RIO_PEF_SWITCH))
  678. return 0;
  679. if (rdev->phys_efptr == 0) {
  680. pr_err("RIO_PW: Bad switch initialization for %s\n",
  681. rio_name(rdev));
  682. return 0;
  683. }
  684. /*
  685. * Process the port-write notification from switch
  686. */
  687. if (rdev->rswitch->em_handle)
  688. rdev->rswitch->em_handle(rdev, portnum);
  689. rio_read_config_32(rdev,
  690. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
  691. &err_status);
  692. pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
  693. if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
  694. if (!(rdev->rswitch->port_ok & (1 << portnum))) {
  695. rdev->rswitch->port_ok |= (1 << portnum);
  696. rio_set_port_lockout(rdev, portnum, 0);
  697. /* Schedule Insertion Service */
  698. pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
  699. rio_name(rdev), portnum);
  700. }
  701. /* Clear error-stopped states (if reported).
  702. * Depending on the link partner state, two attempts
  703. * may be needed for successful recovery.
  704. */
  705. if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
  706. RIO_PORT_N_ERR_STS_PW_INP_ES)) {
  707. if (rio_clr_err_stopped(rdev, portnum, err_status))
  708. rio_clr_err_stopped(rdev, portnum, 0);
  709. }
  710. } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
  711. if (rdev->rswitch->port_ok & (1 << portnum)) {
  712. rdev->rswitch->port_ok &= ~(1 << portnum);
  713. rio_set_port_lockout(rdev, portnum, 1);
  714. rio_write_config_32(rdev,
  715. rdev->phys_efptr +
  716. RIO_PORT_N_ACK_STS_CSR(portnum),
  717. RIO_PORT_N_ACK_CLEAR);
  718. /* Schedule Extraction Service */
  719. pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
  720. rio_name(rdev), portnum);
  721. }
  722. }
  723. rio_read_config_32(rdev,
  724. rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
  725. if (em_perrdet) {
  726. pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
  727. portnum, em_perrdet);
  728. /* Clear EM Port N Error Detect CSR */
  729. rio_write_config_32(rdev,
  730. rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
  731. }
  732. rio_read_config_32(rdev,
  733. rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
  734. if (em_ltlerrdet) {
  735. pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
  736. em_ltlerrdet);
  737. /* Clear EM L/T Layer Error Detect CSR */
  738. rio_write_config_32(rdev,
  739. rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
  740. }
  741. /* Clear remaining error bits and Port-Write Pending bit */
  742. rio_write_config_32(rdev,
  743. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
  744. err_status);
  745. return 0;
  746. }
  747. EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
  748. /**
  749. * rio_mport_get_efb - get pointer to next extended features block
  750. * @port: Master port to issue transaction
  751. * @local: Indicate a local master port or remote device access
  752. * @destid: Destination ID of the device
  753. * @hopcount: Number of switch hops to the device
  754. * @from: Offset of current Extended Feature block header (if 0 starts
  755. * from ExtFeaturePtr)
  756. */
  757. u32
  758. rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
  759. u8 hopcount, u32 from)
  760. {
  761. u32 reg_val;
  762. if (from == 0) {
  763. if (local)
  764. rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
  765. &reg_val);
  766. else
  767. rio_mport_read_config_32(port, destid, hopcount,
  768. RIO_ASM_INFO_CAR, &reg_val);
  769. return reg_val & RIO_EXT_FTR_PTR_MASK;
  770. } else {
  771. if (local)
  772. rio_local_read_config_32(port, from, &reg_val);
  773. else
  774. rio_mport_read_config_32(port, destid, hopcount,
  775. from, &reg_val);
  776. return RIO_GET_BLOCK_ID(reg_val);
  777. }
  778. }
  779. /**
  780. * rio_mport_get_feature - query for devices' extended features
  781. * @port: Master port to issue transaction
  782. * @local: Indicate a local master port or remote device access
  783. * @destid: Destination ID of the device
  784. * @hopcount: Number of switch hops to the device
  785. * @ftr: Extended feature code
  786. *
  787. * Tell if a device supports a given RapidIO capability.
  788. * Returns the offset of the requested extended feature
  789. * block within the device's RIO configuration space or
  790. * 0 in case the device does not support it. Possible
  791. * values for @ftr:
  792. *
  793. * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
  794. *
  795. * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
  796. *
  797. * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
  798. *
  799. * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
  800. *
  801. * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
  802. *
  803. * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
  804. */
  805. u32
  806. rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
  807. u8 hopcount, int ftr)
  808. {
  809. u32 asm_info, ext_ftr_ptr, ftr_header;
  810. if (local)
  811. rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
  812. else
  813. rio_mport_read_config_32(port, destid, hopcount,
  814. RIO_ASM_INFO_CAR, &asm_info);
  815. ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
  816. while (ext_ftr_ptr) {
  817. if (local)
  818. rio_local_read_config_32(port, ext_ftr_ptr,
  819. &ftr_header);
  820. else
  821. rio_mport_read_config_32(port, destid, hopcount,
  822. ext_ftr_ptr, &ftr_header);
  823. if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
  824. return ext_ftr_ptr;
  825. if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
  826. break;
  827. }
  828. return 0;
  829. }
  830. /**
  831. * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
  832. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  833. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  834. * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
  835. * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
  836. * @from: Previous RIO device found in search, or %NULL for new search
  837. *
  838. * Iterates through the list of known RIO devices. If a RIO device is
  839. * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
  840. * count to the device is incrememted and a pointer to its device
  841. * structure is returned. Otherwise, %NULL is returned. A new search
  842. * is initiated by passing %NULL to the @from argument. Otherwise, if
  843. * @from is not %NULL, searches continue from next device on the global
  844. * list. The reference count for @from is always decremented if it is
  845. * not %NULL.
  846. */
  847. struct rio_dev *rio_get_asm(u16 vid, u16 did,
  848. u16 asm_vid, u16 asm_did, struct rio_dev *from)
  849. {
  850. struct list_head *n;
  851. struct rio_dev *rdev;
  852. WARN_ON(in_interrupt());
  853. spin_lock(&rio_global_list_lock);
  854. n = from ? from->global_list.next : rio_devices.next;
  855. while (n && (n != &rio_devices)) {
  856. rdev = rio_dev_g(n);
  857. if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
  858. (did == RIO_ANY_ID || rdev->did == did) &&
  859. (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
  860. (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
  861. goto exit;
  862. n = n->next;
  863. }
  864. rdev = NULL;
  865. exit:
  866. rio_dev_put(from);
  867. rdev = rio_dev_get(rdev);
  868. spin_unlock(&rio_global_list_lock);
  869. return rdev;
  870. }
  871. /**
  872. * rio_get_device - Begin or continue searching for a RIO device by vid/did
  873. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  874. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  875. * @from: Previous RIO device found in search, or %NULL for new search
  876. *
  877. * Iterates through the list of known RIO devices. If a RIO device is
  878. * found with a matching @vid and @did, the reference count to the
  879. * device is incrememted and a pointer to its device structure is returned.
  880. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  881. * to the @from argument. Otherwise, if @from is not %NULL, searches
  882. * continue from next device on the global list. The reference count for
  883. * @from is always decremented if it is not %NULL.
  884. */
  885. struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
  886. {
  887. return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
  888. }
  889. /**
  890. * rio_std_route_add_entry - Add switch route table entry using standard
  891. * registers defined in RIO specification rev.1.3
  892. * @mport: Master port to issue transaction
  893. * @destid: Destination ID of the device
  894. * @hopcount: Number of switch hops to the device
  895. * @table: routing table ID (global or port-specific)
  896. * @route_destid: destID entry in the RT
  897. * @route_port: destination port for specified destID
  898. */
  899. int rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  900. u16 table, u16 route_destid, u8 route_port)
  901. {
  902. if (table == RIO_GLOBAL_TABLE) {
  903. rio_mport_write_config_32(mport, destid, hopcount,
  904. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  905. (u32)route_destid);
  906. rio_mport_write_config_32(mport, destid, hopcount,
  907. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  908. (u32)route_port);
  909. }
  910. udelay(10);
  911. return 0;
  912. }
  913. /**
  914. * rio_std_route_get_entry - Read switch route table entry (port number)
  915. * associated with specified destID using standard registers defined in RIO
  916. * specification rev.1.3
  917. * @mport: Master port to issue transaction
  918. * @destid: Destination ID of the device
  919. * @hopcount: Number of switch hops to the device
  920. * @table: routing table ID (global or port-specific)
  921. * @route_destid: destID entry in the RT
  922. * @route_port: returned destination port for specified destID
  923. */
  924. int rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  925. u16 table, u16 route_destid, u8 *route_port)
  926. {
  927. u32 result;
  928. if (table == RIO_GLOBAL_TABLE) {
  929. rio_mport_write_config_32(mport, destid, hopcount,
  930. RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
  931. rio_mport_read_config_32(mport, destid, hopcount,
  932. RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
  933. *route_port = (u8)result;
  934. }
  935. return 0;
  936. }
  937. /**
  938. * rio_std_route_clr_table - Clear swotch route table using standard registers
  939. * defined in RIO specification rev.1.3.
  940. * @mport: Master port to issue transaction
  941. * @destid: Destination ID of the device
  942. * @hopcount: Number of switch hops to the device
  943. * @table: routing table ID (global or port-specific)
  944. */
  945. int rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
  946. u16 table)
  947. {
  948. u32 max_destid = 0xff;
  949. u32 i, pef, id_inc = 1, ext_cfg = 0;
  950. u32 port_sel = RIO_INVALID_ROUTE;
  951. if (table == RIO_GLOBAL_TABLE) {
  952. rio_mport_read_config_32(mport, destid, hopcount,
  953. RIO_PEF_CAR, &pef);
  954. if (mport->sys_size) {
  955. rio_mport_read_config_32(mport, destid, hopcount,
  956. RIO_SWITCH_RT_LIMIT,
  957. &max_destid);
  958. max_destid &= RIO_RT_MAX_DESTID;
  959. }
  960. if (pef & RIO_PEF_EXT_RT) {
  961. ext_cfg = 0x80000000;
  962. id_inc = 4;
  963. port_sel = (RIO_INVALID_ROUTE << 24) |
  964. (RIO_INVALID_ROUTE << 16) |
  965. (RIO_INVALID_ROUTE << 8) |
  966. RIO_INVALID_ROUTE;
  967. }
  968. for (i = 0; i <= max_destid;) {
  969. rio_mport_write_config_32(mport, destid, hopcount,
  970. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  971. ext_cfg | i);
  972. rio_mport_write_config_32(mport, destid, hopcount,
  973. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  974. port_sel);
  975. i += id_inc;
  976. }
  977. }
  978. udelay(10);
  979. return 0;
  980. }
  981. static void rio_fixup_device(struct rio_dev *dev)
  982. {
  983. }
  984. static int __devinit rio_init(void)
  985. {
  986. struct rio_dev *dev = NULL;
  987. while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
  988. rio_fixup_device(dev);
  989. }
  990. return 0;
  991. }
  992. int __devinit rio_init_mports(void)
  993. {
  994. struct rio_mport *port;
  995. list_for_each_entry(port, &rio_mports, node) {
  996. if (port->host_deviceid >= 0)
  997. rio_enum_mport(port);
  998. else
  999. rio_disc_mport(port);
  1000. }
  1001. rio_init();
  1002. return 0;
  1003. }
  1004. device_initcall_sync(rio_init_mports);
  1005. static int hdids[RIO_MAX_MPORTS + 1];
  1006. static int rio_get_hdid(int index)
  1007. {
  1008. if (!hdids[0] || hdids[0] <= index || index >= RIO_MAX_MPORTS)
  1009. return -1;
  1010. return hdids[index + 1];
  1011. }
  1012. static int rio_hdid_setup(char *str)
  1013. {
  1014. (void)get_options(str, ARRAY_SIZE(hdids), hdids);
  1015. return 1;
  1016. }
  1017. __setup("riohdid=", rio_hdid_setup);
  1018. int rio_register_mport(struct rio_mport *port)
  1019. {
  1020. if (next_portid >= RIO_MAX_MPORTS) {
  1021. pr_err("RIO: reached specified max number of mports\n");
  1022. return 1;
  1023. }
  1024. port->id = next_portid++;
  1025. port->host_deviceid = rio_get_hdid(port->id);
  1026. list_add_tail(&port->node, &rio_mports);
  1027. return 0;
  1028. }
  1029. EXPORT_SYMBOL_GPL(rio_local_get_device_id);
  1030. EXPORT_SYMBOL_GPL(rio_get_device);
  1031. EXPORT_SYMBOL_GPL(rio_get_asm);
  1032. EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
  1033. EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
  1034. EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
  1035. EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
  1036. EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
  1037. EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
  1038. EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
  1039. EXPORT_SYMBOL_GPL(rio_release_outb_mbox);