rio.c 33 KB

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