rio.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. u32 regval;
  404. rio_read_config_32(rdev,
  405. rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
  406. &regval);
  407. if (lock)
  408. regval |= RIO_PORT_N_CTL_LOCKOUT;
  409. else
  410. regval &= ~RIO_PORT_N_CTL_LOCKOUT;
  411. rio_write_config_32(rdev,
  412. rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
  413. regval);
  414. return 0;
  415. }
  416. /**
  417. * rio_chk_dev_route - Validate route to the specified device.
  418. * @rdev: RIO device failed to respond
  419. * @nrdev: Last active device on the route to rdev
  420. * @npnum: nrdev's port number on the route to rdev
  421. *
  422. * Follows a route to the specified RIO device to determine the last available
  423. * device (and corresponding RIO port) on the route.
  424. */
  425. static int
  426. rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
  427. {
  428. u32 result;
  429. int p_port, rc = -EIO;
  430. struct rio_dev *prev = NULL;
  431. /* Find switch with failed RIO link */
  432. while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
  433. if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
  434. prev = rdev->prev;
  435. break;
  436. }
  437. rdev = rdev->prev;
  438. }
  439. if (prev == NULL)
  440. goto err_out;
  441. p_port = prev->rswitch->route_table[rdev->destid];
  442. if (p_port != RIO_INVALID_ROUTE) {
  443. pr_debug("RIO: link failed on [%s]-P%d\n",
  444. rio_name(prev), p_port);
  445. *nrdev = prev;
  446. *npnum = p_port;
  447. rc = 0;
  448. } else
  449. pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
  450. err_out:
  451. return rc;
  452. }
  453. /**
  454. * rio_mport_chk_dev_access - Validate access to the specified device.
  455. * @mport: Master port to send transactions
  456. * @destid: Device destination ID in network
  457. * @hopcount: Number of hops into the network
  458. */
  459. int
  460. rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
  461. {
  462. int i = 0;
  463. u32 tmp;
  464. while (rio_mport_read_config_32(mport, destid, hopcount,
  465. RIO_DEV_ID_CAR, &tmp)) {
  466. i++;
  467. if (i == RIO_MAX_CHK_RETRY)
  468. return -EIO;
  469. mdelay(1);
  470. }
  471. return 0;
  472. }
  473. /**
  474. * rio_chk_dev_access - Validate access to the specified device.
  475. * @rdev: Pointer to RIO device control structure
  476. */
  477. static int rio_chk_dev_access(struct rio_dev *rdev)
  478. {
  479. return rio_mport_chk_dev_access(rdev->net->hport,
  480. rdev->destid, rdev->hopcount);
  481. }
  482. /**
  483. * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
  484. * returns link-response (if requested).
  485. * @rdev: RIO devive to issue Input-status command
  486. * @pnum: Device port number to issue the command
  487. * @lnkresp: Response from a link partner
  488. */
  489. static int
  490. rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
  491. {
  492. u32 regval;
  493. int checkcount;
  494. if (lnkresp) {
  495. /* Read from link maintenance response register
  496. * to clear valid bit */
  497. rio_read_config_32(rdev,
  498. rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
  499. &regval);
  500. udelay(50);
  501. }
  502. /* Issue Input-status command */
  503. rio_write_config_32(rdev,
  504. rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
  505. RIO_MNT_REQ_CMD_IS);
  506. /* Exit if the response is not expected */
  507. if (lnkresp == NULL)
  508. return 0;
  509. checkcount = 3;
  510. while (checkcount--) {
  511. udelay(50);
  512. rio_read_config_32(rdev,
  513. rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
  514. &regval);
  515. if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
  516. *lnkresp = regval;
  517. return 0;
  518. }
  519. }
  520. return -EIO;
  521. }
  522. /**
  523. * rio_clr_err_stopped - Clears port Error-stopped states.
  524. * @rdev: Pointer to RIO device control structure
  525. * @pnum: Switch port number to clear errors
  526. * @err_status: port error status (if 0 reads register from device)
  527. */
  528. static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
  529. {
  530. struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
  531. u32 regval;
  532. u32 far_ackid, far_linkstat, near_ackid;
  533. if (err_status == 0)
  534. rio_read_config_32(rdev,
  535. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  536. &err_status);
  537. if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
  538. pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
  539. /*
  540. * Send a Link-Request/Input-Status control symbol
  541. */
  542. if (rio_get_input_status(rdev, pnum, &regval)) {
  543. pr_debug("RIO_EM: Input-status response timeout\n");
  544. goto rd_err;
  545. }
  546. pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
  547. pnum, regval);
  548. far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
  549. far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
  550. rio_read_config_32(rdev,
  551. rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
  552. &regval);
  553. pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
  554. near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
  555. pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
  556. " near_ackID=0x%02x\n",
  557. pnum, far_ackid, far_linkstat, near_ackid);
  558. /*
  559. * If required, synchronize ackIDs of near and
  560. * far sides.
  561. */
  562. if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
  563. (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
  564. /* Align near outstanding/outbound ackIDs with
  565. * far inbound.
  566. */
  567. rio_write_config_32(rdev,
  568. rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
  569. (near_ackid << 24) |
  570. (far_ackid << 8) | far_ackid);
  571. /* Align far outstanding/outbound ackIDs with
  572. * near inbound.
  573. */
  574. far_ackid++;
  575. if (nextdev)
  576. rio_write_config_32(nextdev,
  577. nextdev->phys_efptr +
  578. RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
  579. (far_ackid << 24) |
  580. (near_ackid << 8) | near_ackid);
  581. else
  582. pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
  583. }
  584. rd_err:
  585. rio_read_config_32(rdev,
  586. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  587. &err_status);
  588. pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
  589. }
  590. if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
  591. pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
  592. rio_get_input_status(nextdev,
  593. RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
  594. udelay(50);
  595. rio_read_config_32(rdev,
  596. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  597. &err_status);
  598. pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
  599. }
  600. return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
  601. RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
  602. }
  603. /**
  604. * rio_inb_pwrite_handler - process inbound port-write message
  605. * @pw_msg: pointer to inbound port-write message
  606. *
  607. * Processes an inbound port-write message. Returns 0 if the request
  608. * has been satisfied.
  609. */
  610. int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
  611. {
  612. struct rio_dev *rdev;
  613. u32 err_status, em_perrdet, em_ltlerrdet;
  614. int rc, portnum;
  615. rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
  616. if (rdev == NULL) {
  617. /* Device removed or enumeration error */
  618. pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
  619. __func__, pw_msg->em.comptag);
  620. return -EIO;
  621. }
  622. pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
  623. #ifdef DEBUG_PW
  624. {
  625. u32 i;
  626. for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
  627. pr_debug("0x%02x: %08x %08x %08x %08x\n",
  628. i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
  629. pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
  630. i += 4;
  631. }
  632. }
  633. #endif
  634. /* Call an external service function (if such is registered
  635. * for this device). This may be the service for endpoints that send
  636. * device-specific port-write messages. End-point messages expected
  637. * to be handled completely by EP specific device driver.
  638. * For switches rc==0 signals that no standard processing required.
  639. */
  640. if (rdev->pwcback != NULL) {
  641. rc = rdev->pwcback(rdev, pw_msg, 0);
  642. if (rc == 0)
  643. return 0;
  644. }
  645. portnum = pw_msg->em.is_port & 0xFF;
  646. /* Check if device and route to it are functional:
  647. * Sometimes devices may send PW message(s) just before being
  648. * powered down (or link being lost).
  649. */
  650. if (rio_chk_dev_access(rdev)) {
  651. pr_debug("RIO: device access failed - get link partner\n");
  652. /* Scan route to the device and identify failed link.
  653. * This will replace device and port reported in PW message.
  654. * PW message should not be used after this point.
  655. */
  656. if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
  657. pr_err("RIO: Route trace for %s failed\n",
  658. rio_name(rdev));
  659. return -EIO;
  660. }
  661. pw_msg = NULL;
  662. }
  663. /* For End-point devices processing stops here */
  664. if (!(rdev->pef & RIO_PEF_SWITCH))
  665. return 0;
  666. if (rdev->phys_efptr == 0) {
  667. pr_err("RIO_PW: Bad switch initialization for %s\n",
  668. rio_name(rdev));
  669. return 0;
  670. }
  671. /*
  672. * Process the port-write notification from switch
  673. */
  674. if (rdev->rswitch->em_handle)
  675. rdev->rswitch->em_handle(rdev, portnum);
  676. rio_read_config_32(rdev,
  677. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
  678. &err_status);
  679. pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
  680. if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
  681. if (!(rdev->rswitch->port_ok & (1 << portnum))) {
  682. rdev->rswitch->port_ok |= (1 << portnum);
  683. rio_set_port_lockout(rdev, portnum, 0);
  684. /* Schedule Insertion Service */
  685. pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
  686. rio_name(rdev), portnum);
  687. }
  688. /* Clear error-stopped states (if reported).
  689. * Depending on the link partner state, two attempts
  690. * may be needed for successful recovery.
  691. */
  692. if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
  693. RIO_PORT_N_ERR_STS_PW_INP_ES)) {
  694. if (rio_clr_err_stopped(rdev, portnum, err_status))
  695. rio_clr_err_stopped(rdev, portnum, 0);
  696. }
  697. } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
  698. if (rdev->rswitch->port_ok & (1 << portnum)) {
  699. rdev->rswitch->port_ok &= ~(1 << portnum);
  700. rio_set_port_lockout(rdev, portnum, 1);
  701. rio_write_config_32(rdev,
  702. rdev->phys_efptr +
  703. RIO_PORT_N_ACK_STS_CSR(portnum),
  704. RIO_PORT_N_ACK_CLEAR);
  705. /* Schedule Extraction Service */
  706. pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
  707. rio_name(rdev), portnum);
  708. }
  709. }
  710. rio_read_config_32(rdev,
  711. rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
  712. if (em_perrdet) {
  713. pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
  714. portnum, em_perrdet);
  715. /* Clear EM Port N Error Detect CSR */
  716. rio_write_config_32(rdev,
  717. rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
  718. }
  719. rio_read_config_32(rdev,
  720. rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
  721. if (em_ltlerrdet) {
  722. pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
  723. em_ltlerrdet);
  724. /* Clear EM L/T Layer Error Detect CSR */
  725. rio_write_config_32(rdev,
  726. rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
  727. }
  728. /* Clear remaining error bits and Port-Write Pending bit */
  729. rio_write_config_32(rdev,
  730. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
  731. err_status);
  732. return 0;
  733. }
  734. EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
  735. /**
  736. * rio_mport_get_efb - get pointer to next extended features block
  737. * @port: Master port to issue transaction
  738. * @local: Indicate a local master port or remote device access
  739. * @destid: Destination ID of the device
  740. * @hopcount: Number of switch hops to the device
  741. * @from: Offset of current Extended Feature block header (if 0 starts
  742. * from ExtFeaturePtr)
  743. */
  744. u32
  745. rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
  746. u8 hopcount, u32 from)
  747. {
  748. u32 reg_val;
  749. if (from == 0) {
  750. if (local)
  751. rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
  752. &reg_val);
  753. else
  754. rio_mport_read_config_32(port, destid, hopcount,
  755. RIO_ASM_INFO_CAR, &reg_val);
  756. return reg_val & RIO_EXT_FTR_PTR_MASK;
  757. } else {
  758. if (local)
  759. rio_local_read_config_32(port, from, &reg_val);
  760. else
  761. rio_mport_read_config_32(port, destid, hopcount,
  762. from, &reg_val);
  763. return RIO_GET_BLOCK_ID(reg_val);
  764. }
  765. }
  766. /**
  767. * rio_mport_get_feature - query for devices' extended features
  768. * @port: Master port to issue transaction
  769. * @local: Indicate a local master port or remote device access
  770. * @destid: Destination ID of the device
  771. * @hopcount: Number of switch hops to the device
  772. * @ftr: Extended feature code
  773. *
  774. * Tell if a device supports a given RapidIO capability.
  775. * Returns the offset of the requested extended feature
  776. * block within the device's RIO configuration space or
  777. * 0 in case the device does not support it. Possible
  778. * values for @ftr:
  779. *
  780. * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
  781. *
  782. * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
  783. *
  784. * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
  785. *
  786. * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
  787. *
  788. * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
  789. *
  790. * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
  791. */
  792. u32
  793. rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
  794. u8 hopcount, int ftr)
  795. {
  796. u32 asm_info, ext_ftr_ptr, ftr_header;
  797. if (local)
  798. rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
  799. else
  800. rio_mport_read_config_32(port, destid, hopcount,
  801. RIO_ASM_INFO_CAR, &asm_info);
  802. ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
  803. while (ext_ftr_ptr) {
  804. if (local)
  805. rio_local_read_config_32(port, ext_ftr_ptr,
  806. &ftr_header);
  807. else
  808. rio_mport_read_config_32(port, destid, hopcount,
  809. ext_ftr_ptr, &ftr_header);
  810. if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
  811. return ext_ftr_ptr;
  812. if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
  813. break;
  814. }
  815. return 0;
  816. }
  817. /**
  818. * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
  819. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  820. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  821. * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
  822. * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
  823. * @from: Previous RIO device found in search, or %NULL for new search
  824. *
  825. * Iterates through the list of known RIO devices. If a RIO device is
  826. * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
  827. * count to the device is incrememted and a pointer to its device
  828. * structure is returned. Otherwise, %NULL is returned. A new search
  829. * is initiated by passing %NULL to the @from argument. Otherwise, if
  830. * @from is not %NULL, searches continue from next device on the global
  831. * list. The reference count for @from is always decremented if it is
  832. * not %NULL.
  833. */
  834. struct rio_dev *rio_get_asm(u16 vid, u16 did,
  835. u16 asm_vid, u16 asm_did, struct rio_dev *from)
  836. {
  837. struct list_head *n;
  838. struct rio_dev *rdev;
  839. WARN_ON(in_interrupt());
  840. spin_lock(&rio_global_list_lock);
  841. n = from ? from->global_list.next : rio_devices.next;
  842. while (n && (n != &rio_devices)) {
  843. rdev = rio_dev_g(n);
  844. if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
  845. (did == RIO_ANY_ID || rdev->did == did) &&
  846. (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
  847. (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
  848. goto exit;
  849. n = n->next;
  850. }
  851. rdev = NULL;
  852. exit:
  853. rio_dev_put(from);
  854. rdev = rio_dev_get(rdev);
  855. spin_unlock(&rio_global_list_lock);
  856. return rdev;
  857. }
  858. /**
  859. * rio_get_device - Begin or continue searching for a RIO device by vid/did
  860. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  861. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  862. * @from: Previous RIO device found in search, or %NULL for new search
  863. *
  864. * Iterates through the list of known RIO devices. If a RIO device is
  865. * found with a matching @vid and @did, the reference count to the
  866. * device is incrememted and a pointer to its device structure is returned.
  867. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  868. * to the @from argument. Otherwise, if @from is not %NULL, searches
  869. * continue from next device on the global list. The reference count for
  870. * @from is always decremented if it is not %NULL.
  871. */
  872. struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
  873. {
  874. return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
  875. }
  876. /**
  877. * rio_std_route_add_entry - Add switch route table entry using standard
  878. * registers defined in RIO specification rev.1.3
  879. * @mport: Master port to issue transaction
  880. * @destid: Destination ID of the device
  881. * @hopcount: Number of switch hops to the device
  882. * @table: routing table ID (global or port-specific)
  883. * @route_destid: destID entry in the RT
  884. * @route_port: destination port for specified destID
  885. */
  886. int rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  887. u16 table, u16 route_destid, u8 route_port)
  888. {
  889. if (table == RIO_GLOBAL_TABLE) {
  890. rio_mport_write_config_32(mport, destid, hopcount,
  891. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  892. (u32)route_destid);
  893. rio_mport_write_config_32(mport, destid, hopcount,
  894. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  895. (u32)route_port);
  896. }
  897. udelay(10);
  898. return 0;
  899. }
  900. /**
  901. * rio_std_route_get_entry - Read switch route table entry (port number)
  902. * associated with specified destID using standard registers defined in RIO
  903. * specification rev.1.3
  904. * @mport: Master port to issue transaction
  905. * @destid: Destination ID of the device
  906. * @hopcount: Number of switch hops to the device
  907. * @table: routing table ID (global or port-specific)
  908. * @route_destid: destID entry in the RT
  909. * @route_port: returned destination port for specified destID
  910. */
  911. int rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  912. u16 table, u16 route_destid, u8 *route_port)
  913. {
  914. u32 result;
  915. if (table == RIO_GLOBAL_TABLE) {
  916. rio_mport_write_config_32(mport, destid, hopcount,
  917. RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
  918. rio_mport_read_config_32(mport, destid, hopcount,
  919. RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
  920. *route_port = (u8)result;
  921. }
  922. return 0;
  923. }
  924. /**
  925. * rio_std_route_clr_table - Clear swotch route table using standard registers
  926. * defined in RIO specification rev.1.3.
  927. * @mport: Master port to issue transaction
  928. * @destid: Destination ID of the device
  929. * @hopcount: Number of switch hops to the device
  930. * @table: routing table ID (global or port-specific)
  931. */
  932. int rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
  933. u16 table)
  934. {
  935. u32 max_destid = 0xff;
  936. u32 i, pef, id_inc = 1, ext_cfg = 0;
  937. u32 port_sel = RIO_INVALID_ROUTE;
  938. if (table == RIO_GLOBAL_TABLE) {
  939. rio_mport_read_config_32(mport, destid, hopcount,
  940. RIO_PEF_CAR, &pef);
  941. if (mport->sys_size) {
  942. rio_mport_read_config_32(mport, destid, hopcount,
  943. RIO_SWITCH_RT_LIMIT,
  944. &max_destid);
  945. max_destid &= RIO_RT_MAX_DESTID;
  946. }
  947. if (pef & RIO_PEF_EXT_RT) {
  948. ext_cfg = 0x80000000;
  949. id_inc = 4;
  950. port_sel = (RIO_INVALID_ROUTE << 24) |
  951. (RIO_INVALID_ROUTE << 16) |
  952. (RIO_INVALID_ROUTE << 8) |
  953. RIO_INVALID_ROUTE;
  954. }
  955. for (i = 0; i <= max_destid;) {
  956. rio_mport_write_config_32(mport, destid, hopcount,
  957. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  958. ext_cfg | i);
  959. rio_mport_write_config_32(mport, destid, hopcount,
  960. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  961. port_sel);
  962. i += id_inc;
  963. }
  964. }
  965. udelay(10);
  966. return 0;
  967. }
  968. static void rio_fixup_device(struct rio_dev *dev)
  969. {
  970. }
  971. static int __devinit rio_init(void)
  972. {
  973. struct rio_dev *dev = NULL;
  974. while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
  975. rio_fixup_device(dev);
  976. }
  977. return 0;
  978. }
  979. device_initcall(rio_init);
  980. int __devinit rio_init_mports(void)
  981. {
  982. int rc = 0;
  983. struct rio_mport *port;
  984. list_for_each_entry(port, &rio_mports, node) {
  985. if (!request_mem_region(port->iores.start,
  986. resource_size(&port->iores),
  987. port->name)) {
  988. printk(KERN_ERR
  989. "RIO: Error requesting master port region 0x%016llx-0x%016llx\n",
  990. (u64)port->iores.start, (u64)port->iores.end);
  991. rc = -ENOMEM;
  992. goto out;
  993. }
  994. if (port->host_deviceid >= 0)
  995. rio_enum_mport(port);
  996. else
  997. rio_disc_mport(port);
  998. }
  999. out:
  1000. return rc;
  1001. }
  1002. void rio_register_mport(struct rio_mport *port)
  1003. {
  1004. list_add_tail(&port->node, &rio_mports);
  1005. }
  1006. EXPORT_SYMBOL_GPL(rio_local_get_device_id);
  1007. EXPORT_SYMBOL_GPL(rio_get_device);
  1008. EXPORT_SYMBOL_GPL(rio_get_asm);
  1009. EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
  1010. EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
  1011. EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
  1012. EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
  1013. EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
  1014. EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
  1015. EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
  1016. EXPORT_SYMBOL_GPL(rio_release_outb_mbox);