rio.c 32 KB

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