rio.c 33 KB

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