rio.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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_devices);
  31. static DEFINE_SPINLOCK(rio_global_list_lock);
  32. static LIST_HEAD(rio_mports);
  33. static DEFINE_MUTEX(rio_mport_list_lock);
  34. static unsigned char next_portid;
  35. static DEFINE_SPINLOCK(rio_mmap_lock);
  36. /**
  37. * rio_local_get_device_id - Get the base/extended device id for a port
  38. * @port: RIO master port from which to get the deviceid
  39. *
  40. * Reads the base/extended device id from the local device
  41. * implementing the master port. Returns the 8/16-bit device
  42. * id.
  43. */
  44. u16 rio_local_get_device_id(struct rio_mport *port)
  45. {
  46. u32 result;
  47. rio_local_read_config_32(port, RIO_DID_CSR, &result);
  48. return (RIO_GET_DID(port->sys_size, result));
  49. }
  50. /**
  51. * rio_add_device- Adds a RIO device to the device model
  52. * @rdev: RIO device
  53. *
  54. * Adds the RIO device to the global device list and adds the RIO
  55. * device to the RIO device list. Creates the generic sysfs nodes
  56. * for an RIO device.
  57. */
  58. int rio_add_device(struct rio_dev *rdev)
  59. {
  60. int err;
  61. err = device_add(&rdev->dev);
  62. if (err)
  63. return err;
  64. spin_lock(&rio_global_list_lock);
  65. list_add_tail(&rdev->global_list, &rio_devices);
  66. spin_unlock(&rio_global_list_lock);
  67. rio_create_sysfs_dev_files(rdev);
  68. return 0;
  69. }
  70. EXPORT_SYMBOL_GPL(rio_add_device);
  71. /**
  72. * rio_request_inb_mbox - request inbound mailbox service
  73. * @mport: RIO master port from which to allocate the mailbox resource
  74. * @dev_id: Device specific pointer to pass on event
  75. * @mbox: Mailbox number to claim
  76. * @entries: Number of entries in inbound mailbox queue
  77. * @minb: Callback to execute when inbound message is received
  78. *
  79. * Requests ownership of an inbound mailbox resource and binds
  80. * a callback function to the resource. Returns %0 on success.
  81. */
  82. int rio_request_inb_mbox(struct rio_mport *mport,
  83. void *dev_id,
  84. int mbox,
  85. int entries,
  86. void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
  87. int slot))
  88. {
  89. int rc = -ENOSYS;
  90. struct resource *res;
  91. if (mport->ops->open_inb_mbox == NULL)
  92. goto out;
  93. res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  94. if (res) {
  95. rio_init_mbox_res(res, mbox, mbox);
  96. /* Make sure this mailbox isn't in use */
  97. if ((rc =
  98. request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
  99. res)) < 0) {
  100. kfree(res);
  101. goto out;
  102. }
  103. mport->inb_msg[mbox].res = res;
  104. /* Hook the inbound message callback */
  105. mport->inb_msg[mbox].mcback = minb;
  106. rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
  107. } else
  108. rc = -ENOMEM;
  109. out:
  110. return rc;
  111. }
  112. /**
  113. * rio_release_inb_mbox - release inbound mailbox message service
  114. * @mport: RIO master port from which to release the mailbox resource
  115. * @mbox: Mailbox number to release
  116. *
  117. * Releases ownership of an inbound mailbox resource. Returns 0
  118. * if the request has been satisfied.
  119. */
  120. int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
  121. {
  122. if (mport->ops->close_inb_mbox) {
  123. mport->ops->close_inb_mbox(mport, mbox);
  124. /* Release the mailbox resource */
  125. return release_resource(mport->inb_msg[mbox].res);
  126. } else
  127. return -ENOSYS;
  128. }
  129. /**
  130. * rio_request_outb_mbox - request outbound mailbox service
  131. * @mport: RIO master port from which to allocate the mailbox resource
  132. * @dev_id: Device specific pointer to pass on event
  133. * @mbox: Mailbox number to claim
  134. * @entries: Number of entries in outbound mailbox queue
  135. * @moutb: Callback to execute when outbound message is sent
  136. *
  137. * Requests ownership of an outbound mailbox resource and binds
  138. * a callback function to the resource. Returns 0 on success.
  139. */
  140. int rio_request_outb_mbox(struct rio_mport *mport,
  141. void *dev_id,
  142. int mbox,
  143. int entries,
  144. void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
  145. {
  146. int rc = -ENOSYS;
  147. struct resource *res;
  148. if (mport->ops->open_outb_mbox == NULL)
  149. goto out;
  150. res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  151. if (res) {
  152. rio_init_mbox_res(res, mbox, mbox);
  153. /* Make sure this outbound mailbox isn't in use */
  154. if ((rc =
  155. request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
  156. res)) < 0) {
  157. kfree(res);
  158. goto out;
  159. }
  160. mport->outb_msg[mbox].res = res;
  161. /* Hook the inbound message callback */
  162. mport->outb_msg[mbox].mcback = moutb;
  163. rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
  164. } else
  165. rc = -ENOMEM;
  166. out:
  167. return rc;
  168. }
  169. /**
  170. * rio_release_outb_mbox - release outbound mailbox message service
  171. * @mport: RIO master port from which to release the mailbox resource
  172. * @mbox: Mailbox number to release
  173. *
  174. * Releases ownership of an inbound mailbox resource. Returns 0
  175. * if the request has been satisfied.
  176. */
  177. int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
  178. {
  179. if (mport->ops->close_outb_mbox) {
  180. mport->ops->close_outb_mbox(mport, mbox);
  181. /* Release the mailbox resource */
  182. return release_resource(mport->outb_msg[mbox].res);
  183. } else
  184. return -ENOSYS;
  185. }
  186. /**
  187. * rio_setup_inb_dbell - bind inbound doorbell callback
  188. * @mport: RIO master port to bind the doorbell callback
  189. * @dev_id: Device specific pointer to pass on event
  190. * @res: Doorbell message resource
  191. * @dinb: Callback to execute when doorbell is received
  192. *
  193. * Adds a doorbell resource/callback pair into a port's
  194. * doorbell event list. Returns 0 if the request has been
  195. * satisfied.
  196. */
  197. static int
  198. rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
  199. void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
  200. u16 info))
  201. {
  202. int rc = 0;
  203. struct rio_dbell *dbell;
  204. if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
  205. rc = -ENOMEM;
  206. goto out;
  207. }
  208. dbell->res = res;
  209. dbell->dinb = dinb;
  210. dbell->dev_id = dev_id;
  211. list_add_tail(&dbell->node, &mport->dbells);
  212. out:
  213. return rc;
  214. }
  215. /**
  216. * rio_request_inb_dbell - request inbound doorbell message service
  217. * @mport: RIO master port from which to allocate the doorbell resource
  218. * @dev_id: Device specific pointer to pass on event
  219. * @start: Doorbell info range start
  220. * @end: Doorbell info range end
  221. * @dinb: Callback to execute when doorbell is received
  222. *
  223. * Requests ownership of an inbound doorbell resource and binds
  224. * a callback function to the resource. Returns 0 if the request
  225. * has been satisfied.
  226. */
  227. int rio_request_inb_dbell(struct rio_mport *mport,
  228. void *dev_id,
  229. u16 start,
  230. u16 end,
  231. void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
  232. u16 dst, u16 info))
  233. {
  234. int rc = 0;
  235. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  236. if (res) {
  237. rio_init_dbell_res(res, start, end);
  238. /* Make sure these doorbells aren't in use */
  239. if ((rc =
  240. request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
  241. res)) < 0) {
  242. kfree(res);
  243. goto out;
  244. }
  245. /* Hook the doorbell callback */
  246. rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
  247. } else
  248. rc = -ENOMEM;
  249. out:
  250. return rc;
  251. }
  252. /**
  253. * rio_release_inb_dbell - release inbound doorbell message service
  254. * @mport: RIO master port from which to release the doorbell resource
  255. * @start: Doorbell info range start
  256. * @end: Doorbell info range end
  257. *
  258. * Releases ownership of an inbound doorbell resource and removes
  259. * callback from the doorbell event list. Returns 0 if the request
  260. * has been satisfied.
  261. */
  262. int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
  263. {
  264. int rc = 0, found = 0;
  265. struct rio_dbell *dbell;
  266. list_for_each_entry(dbell, &mport->dbells, node) {
  267. if ((dbell->res->start == start) && (dbell->res->end == end)) {
  268. found = 1;
  269. break;
  270. }
  271. }
  272. /* If we can't find an exact match, fail */
  273. if (!found) {
  274. rc = -EINVAL;
  275. goto out;
  276. }
  277. /* Delete from list */
  278. list_del(&dbell->node);
  279. /* Release the doorbell resource */
  280. rc = release_resource(dbell->res);
  281. /* Free the doorbell event */
  282. kfree(dbell);
  283. out:
  284. return rc;
  285. }
  286. /**
  287. * rio_request_outb_dbell - request outbound doorbell message range
  288. * @rdev: RIO device from which to allocate the doorbell resource
  289. * @start: Doorbell message range start
  290. * @end: Doorbell message range end
  291. *
  292. * Requests ownership of a doorbell message range. Returns a resource
  293. * if the request has been satisfied or %NULL on failure.
  294. */
  295. struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
  296. u16 end)
  297. {
  298. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  299. if (res) {
  300. rio_init_dbell_res(res, start, end);
  301. /* Make sure these doorbells aren't in use */
  302. if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
  303. < 0) {
  304. kfree(res);
  305. res = NULL;
  306. }
  307. }
  308. return res;
  309. }
  310. /**
  311. * rio_release_outb_dbell - release outbound doorbell message range
  312. * @rdev: RIO device from which to release the doorbell resource
  313. * @res: Doorbell resource to be freed
  314. *
  315. * Releases ownership of a doorbell message range. Returns 0 if the
  316. * request has been satisfied.
  317. */
  318. int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
  319. {
  320. int rc = release_resource(res);
  321. kfree(res);
  322. return rc;
  323. }
  324. /**
  325. * rio_request_inb_pwrite - request inbound port-write message service
  326. * @rdev: RIO device to which register inbound port-write callback routine
  327. * @pwcback: Callback routine to execute when port-write is received
  328. *
  329. * Binds a port-write callback function to the RapidIO device.
  330. * Returns 0 if the request has been satisfied.
  331. */
  332. int rio_request_inb_pwrite(struct rio_dev *rdev,
  333. int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
  334. {
  335. int rc = 0;
  336. spin_lock(&rio_global_list_lock);
  337. if (rdev->pwcback != NULL)
  338. rc = -ENOMEM;
  339. else
  340. rdev->pwcback = pwcback;
  341. spin_unlock(&rio_global_list_lock);
  342. return rc;
  343. }
  344. EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
  345. /**
  346. * rio_release_inb_pwrite - release inbound port-write message service
  347. * @rdev: RIO device which registered for inbound port-write callback
  348. *
  349. * Removes callback from the rio_dev structure. Returns 0 if the request
  350. * has been satisfied.
  351. */
  352. int rio_release_inb_pwrite(struct rio_dev *rdev)
  353. {
  354. int rc = -ENOMEM;
  355. spin_lock(&rio_global_list_lock);
  356. if (rdev->pwcback) {
  357. rdev->pwcback = NULL;
  358. rc = 0;
  359. }
  360. spin_unlock(&rio_global_list_lock);
  361. return rc;
  362. }
  363. EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
  364. /**
  365. * rio_map_inb_region -- Map inbound memory region.
  366. * @mport: Master port.
  367. * @local: physical address of memory region to be mapped
  368. * @rbase: RIO base address assigned to this window
  369. * @size: Size of the memory region
  370. * @rflags: Flags for mapping.
  371. *
  372. * Return: 0 -- Success.
  373. *
  374. * This function will create the mapping from RIO space to local memory.
  375. */
  376. int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
  377. u64 rbase, u32 size, u32 rflags)
  378. {
  379. int rc = 0;
  380. unsigned long flags;
  381. if (!mport->ops->map_inb)
  382. return -1;
  383. spin_lock_irqsave(&rio_mmap_lock, flags);
  384. rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
  385. spin_unlock_irqrestore(&rio_mmap_lock, flags);
  386. return rc;
  387. }
  388. EXPORT_SYMBOL_GPL(rio_map_inb_region);
  389. /**
  390. * rio_unmap_inb_region -- Unmap the inbound memory region
  391. * @mport: Master port
  392. * @lstart: physical address of memory region to be unmapped
  393. */
  394. void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
  395. {
  396. unsigned long flags;
  397. if (!mport->ops->unmap_inb)
  398. return;
  399. spin_lock_irqsave(&rio_mmap_lock, flags);
  400. mport->ops->unmap_inb(mport, lstart);
  401. spin_unlock_irqrestore(&rio_mmap_lock, flags);
  402. }
  403. EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
  404. /**
  405. * rio_mport_get_physefb - Helper function that returns register offset
  406. * for Physical Layer Extended Features Block.
  407. * @port: Master port to issue transaction
  408. * @local: Indicate a local master port or remote device access
  409. * @destid: Destination ID of the device
  410. * @hopcount: Number of switch hops to the device
  411. */
  412. u32
  413. rio_mport_get_physefb(struct rio_mport *port, int local,
  414. u16 destid, u8 hopcount)
  415. {
  416. u32 ext_ftr_ptr;
  417. u32 ftr_header;
  418. ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
  419. while (ext_ftr_ptr) {
  420. if (local)
  421. rio_local_read_config_32(port, ext_ftr_ptr,
  422. &ftr_header);
  423. else
  424. rio_mport_read_config_32(port, destid, hopcount,
  425. ext_ftr_ptr, &ftr_header);
  426. ftr_header = RIO_GET_BLOCK_ID(ftr_header);
  427. switch (ftr_header) {
  428. case RIO_EFB_SER_EP_ID_V13P:
  429. case RIO_EFB_SER_EP_REC_ID_V13P:
  430. case RIO_EFB_SER_EP_FREE_ID_V13P:
  431. case RIO_EFB_SER_EP_ID:
  432. case RIO_EFB_SER_EP_REC_ID:
  433. case RIO_EFB_SER_EP_FREE_ID:
  434. case RIO_EFB_SER_EP_FREC_ID:
  435. return ext_ftr_ptr;
  436. default:
  437. break;
  438. }
  439. ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
  440. hopcount, ext_ftr_ptr);
  441. }
  442. return ext_ftr_ptr;
  443. }
  444. EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
  445. /**
  446. * rio_get_comptag - Begin or continue searching for a RIO device by component tag
  447. * @comp_tag: RIO component tag to match
  448. * @from: Previous RIO device found in search, or %NULL for new search
  449. *
  450. * Iterates through the list of known RIO devices. If a RIO device is
  451. * found with a matching @comp_tag, a pointer to its device
  452. * structure is returned. Otherwise, %NULL is returned. A new search
  453. * is initiated by passing %NULL to the @from argument. Otherwise, if
  454. * @from is not %NULL, searches continue from next device on the global
  455. * list.
  456. */
  457. struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
  458. {
  459. struct list_head *n;
  460. struct rio_dev *rdev;
  461. spin_lock(&rio_global_list_lock);
  462. n = from ? from->global_list.next : rio_devices.next;
  463. while (n && (n != &rio_devices)) {
  464. rdev = rio_dev_g(n);
  465. if (rdev->comp_tag == comp_tag)
  466. goto exit;
  467. n = n->next;
  468. }
  469. rdev = NULL;
  470. exit:
  471. spin_unlock(&rio_global_list_lock);
  472. return rdev;
  473. }
  474. EXPORT_SYMBOL_GPL(rio_get_comptag);
  475. /**
  476. * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
  477. * @rdev: Pointer to RIO device control structure
  478. * @pnum: Switch port number to set LOCKOUT bit
  479. * @lock: Operation : set (=1) or clear (=0)
  480. */
  481. int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
  482. {
  483. u32 regval;
  484. rio_read_config_32(rdev,
  485. rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
  486. &regval);
  487. if (lock)
  488. regval |= RIO_PORT_N_CTL_LOCKOUT;
  489. else
  490. regval &= ~RIO_PORT_N_CTL_LOCKOUT;
  491. rio_write_config_32(rdev,
  492. rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
  493. regval);
  494. return 0;
  495. }
  496. EXPORT_SYMBOL_GPL(rio_set_port_lockout);
  497. /**
  498. * rio_switch_init - Sets switch operations for a particular vendor switch
  499. * @rdev: RIO device
  500. * @do_enum: Enumeration/Discovery mode flag
  501. *
  502. * Searches the RIO switch ops table for known switch types. If the vid
  503. * and did match a switch table entry, then call switch initialization
  504. * routine to setup switch-specific routines.
  505. */
  506. void rio_switch_init(struct rio_dev *rdev, int do_enum)
  507. {
  508. struct rio_switch_ops *cur = __start_rio_switch_ops;
  509. struct rio_switch_ops *end = __end_rio_switch_ops;
  510. while (cur < end) {
  511. if ((cur->vid == rdev->vid) && (cur->did == rdev->did)) {
  512. pr_debug("RIO: calling init routine for %s\n",
  513. rio_name(rdev));
  514. cur->init_hook(rdev, do_enum);
  515. break;
  516. }
  517. cur++;
  518. }
  519. if ((cur >= end) && (rdev->pef & RIO_PEF_STD_RT)) {
  520. pr_debug("RIO: adding STD routing ops for %s\n",
  521. rio_name(rdev));
  522. rdev->rswitch->add_entry = rio_std_route_add_entry;
  523. rdev->rswitch->get_entry = rio_std_route_get_entry;
  524. rdev->rswitch->clr_table = rio_std_route_clr_table;
  525. }
  526. if (!rdev->rswitch->add_entry || !rdev->rswitch->get_entry)
  527. printk(KERN_ERR "RIO: missing routing ops for %s\n",
  528. rio_name(rdev));
  529. }
  530. EXPORT_SYMBOL_GPL(rio_switch_init);
  531. /**
  532. * rio_enable_rx_tx_port - enable input receiver and output transmitter of
  533. * given port
  534. * @port: Master port associated with the RIO network
  535. * @local: local=1 select local port otherwise a far device is reached
  536. * @destid: Destination ID of the device to check host bit
  537. * @hopcount: Number of hops to reach the target
  538. * @port_num: Port (-number on switch) to enable on a far end device
  539. *
  540. * Returns 0 or 1 from on General Control Command and Status Register
  541. * (EXT_PTR+0x3C)
  542. */
  543. int rio_enable_rx_tx_port(struct rio_mport *port,
  544. int local, u16 destid,
  545. u8 hopcount, u8 port_num)
  546. {
  547. #ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
  548. u32 regval;
  549. u32 ext_ftr_ptr;
  550. /*
  551. * enable rx input tx output port
  552. */
  553. pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
  554. "%d, port_num = %d)\n", local, destid, hopcount, port_num);
  555. ext_ftr_ptr = rio_mport_get_physefb(port, local, destid, hopcount);
  556. if (local) {
  557. rio_local_read_config_32(port, ext_ftr_ptr +
  558. RIO_PORT_N_CTL_CSR(0),
  559. &regval);
  560. } else {
  561. if (rio_mport_read_config_32(port, destid, hopcount,
  562. ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), &regval) < 0)
  563. return -EIO;
  564. }
  565. if (regval & RIO_PORT_N_CTL_P_TYP_SER) {
  566. /* serial */
  567. regval = regval | RIO_PORT_N_CTL_EN_RX_SER
  568. | RIO_PORT_N_CTL_EN_TX_SER;
  569. } else {
  570. /* parallel */
  571. regval = regval | RIO_PORT_N_CTL_EN_RX_PAR
  572. | RIO_PORT_N_CTL_EN_TX_PAR;
  573. }
  574. if (local) {
  575. rio_local_write_config_32(port, ext_ftr_ptr +
  576. RIO_PORT_N_CTL_CSR(0), regval);
  577. } else {
  578. if (rio_mport_write_config_32(port, destid, hopcount,
  579. ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num), regval) < 0)
  580. return -EIO;
  581. }
  582. #endif
  583. return 0;
  584. }
  585. EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
  586. /**
  587. * rio_chk_dev_route - Validate route to the specified device.
  588. * @rdev: RIO device failed to respond
  589. * @nrdev: Last active device on the route to rdev
  590. * @npnum: nrdev's port number on the route to rdev
  591. *
  592. * Follows a route to the specified RIO device to determine the last available
  593. * device (and corresponding RIO port) on the route.
  594. */
  595. static int
  596. rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
  597. {
  598. u32 result;
  599. int p_port, rc = -EIO;
  600. struct rio_dev *prev = NULL;
  601. /* Find switch with failed RIO link */
  602. while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
  603. if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
  604. prev = rdev->prev;
  605. break;
  606. }
  607. rdev = rdev->prev;
  608. }
  609. if (prev == NULL)
  610. goto err_out;
  611. p_port = prev->rswitch->route_table[rdev->destid];
  612. if (p_port != RIO_INVALID_ROUTE) {
  613. pr_debug("RIO: link failed on [%s]-P%d\n",
  614. rio_name(prev), p_port);
  615. *nrdev = prev;
  616. *npnum = p_port;
  617. rc = 0;
  618. } else
  619. pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
  620. err_out:
  621. return rc;
  622. }
  623. /**
  624. * rio_mport_chk_dev_access - Validate access to the specified device.
  625. * @mport: Master port to send transactions
  626. * @destid: Device destination ID in network
  627. * @hopcount: Number of hops into the network
  628. */
  629. int
  630. rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
  631. {
  632. int i = 0;
  633. u32 tmp;
  634. while (rio_mport_read_config_32(mport, destid, hopcount,
  635. RIO_DEV_ID_CAR, &tmp)) {
  636. i++;
  637. if (i == RIO_MAX_CHK_RETRY)
  638. return -EIO;
  639. mdelay(1);
  640. }
  641. return 0;
  642. }
  643. EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
  644. /**
  645. * rio_chk_dev_access - Validate access to the specified device.
  646. * @rdev: Pointer to RIO device control structure
  647. */
  648. static int rio_chk_dev_access(struct rio_dev *rdev)
  649. {
  650. return rio_mport_chk_dev_access(rdev->net->hport,
  651. rdev->destid, rdev->hopcount);
  652. }
  653. /**
  654. * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
  655. * returns link-response (if requested).
  656. * @rdev: RIO devive to issue Input-status command
  657. * @pnum: Device port number to issue the command
  658. * @lnkresp: Response from a link partner
  659. */
  660. static int
  661. rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
  662. {
  663. u32 regval;
  664. int checkcount;
  665. if (lnkresp) {
  666. /* Read from link maintenance response register
  667. * to clear valid bit */
  668. rio_read_config_32(rdev,
  669. rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
  670. &regval);
  671. udelay(50);
  672. }
  673. /* Issue Input-status command */
  674. rio_write_config_32(rdev,
  675. rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
  676. RIO_MNT_REQ_CMD_IS);
  677. /* Exit if the response is not expected */
  678. if (lnkresp == NULL)
  679. return 0;
  680. checkcount = 3;
  681. while (checkcount--) {
  682. udelay(50);
  683. rio_read_config_32(rdev,
  684. rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
  685. &regval);
  686. if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
  687. *lnkresp = regval;
  688. return 0;
  689. }
  690. }
  691. return -EIO;
  692. }
  693. /**
  694. * rio_clr_err_stopped - Clears port Error-stopped states.
  695. * @rdev: Pointer to RIO device control structure
  696. * @pnum: Switch port number to clear errors
  697. * @err_status: port error status (if 0 reads register from device)
  698. */
  699. static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
  700. {
  701. struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
  702. u32 regval;
  703. u32 far_ackid, far_linkstat, near_ackid;
  704. if (err_status == 0)
  705. rio_read_config_32(rdev,
  706. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  707. &err_status);
  708. if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
  709. pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
  710. /*
  711. * Send a Link-Request/Input-Status control symbol
  712. */
  713. if (rio_get_input_status(rdev, pnum, &regval)) {
  714. pr_debug("RIO_EM: Input-status response timeout\n");
  715. goto rd_err;
  716. }
  717. pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
  718. pnum, regval);
  719. far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
  720. far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
  721. rio_read_config_32(rdev,
  722. rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
  723. &regval);
  724. pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
  725. near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
  726. pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
  727. " near_ackID=0x%02x\n",
  728. pnum, far_ackid, far_linkstat, near_ackid);
  729. /*
  730. * If required, synchronize ackIDs of near and
  731. * far sides.
  732. */
  733. if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
  734. (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
  735. /* Align near outstanding/outbound ackIDs with
  736. * far inbound.
  737. */
  738. rio_write_config_32(rdev,
  739. rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
  740. (near_ackid << 24) |
  741. (far_ackid << 8) | far_ackid);
  742. /* Align far outstanding/outbound ackIDs with
  743. * near inbound.
  744. */
  745. far_ackid++;
  746. if (nextdev)
  747. rio_write_config_32(nextdev,
  748. nextdev->phys_efptr +
  749. RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
  750. (far_ackid << 24) |
  751. (near_ackid << 8) | near_ackid);
  752. else
  753. pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
  754. }
  755. rd_err:
  756. rio_read_config_32(rdev,
  757. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  758. &err_status);
  759. pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
  760. }
  761. if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
  762. pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
  763. rio_get_input_status(nextdev,
  764. RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
  765. udelay(50);
  766. rio_read_config_32(rdev,
  767. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  768. &err_status);
  769. pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
  770. }
  771. return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
  772. RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
  773. }
  774. /**
  775. * rio_inb_pwrite_handler - process inbound port-write message
  776. * @pw_msg: pointer to inbound port-write message
  777. *
  778. * Processes an inbound port-write message. Returns 0 if the request
  779. * has been satisfied.
  780. */
  781. int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
  782. {
  783. struct rio_dev *rdev;
  784. u32 err_status, em_perrdet, em_ltlerrdet;
  785. int rc, portnum;
  786. rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
  787. if (rdev == NULL) {
  788. /* Device removed or enumeration error */
  789. pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
  790. __func__, pw_msg->em.comptag);
  791. return -EIO;
  792. }
  793. pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
  794. #ifdef DEBUG_PW
  795. {
  796. u32 i;
  797. for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
  798. pr_debug("0x%02x: %08x %08x %08x %08x\n",
  799. i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
  800. pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
  801. i += 4;
  802. }
  803. }
  804. #endif
  805. /* Call an external service function (if such is registered
  806. * for this device). This may be the service for endpoints that send
  807. * device-specific port-write messages. End-point messages expected
  808. * to be handled completely by EP specific device driver.
  809. * For switches rc==0 signals that no standard processing required.
  810. */
  811. if (rdev->pwcback != NULL) {
  812. rc = rdev->pwcback(rdev, pw_msg, 0);
  813. if (rc == 0)
  814. return 0;
  815. }
  816. portnum = pw_msg->em.is_port & 0xFF;
  817. /* Check if device and route to it are functional:
  818. * Sometimes devices may send PW message(s) just before being
  819. * powered down (or link being lost).
  820. */
  821. if (rio_chk_dev_access(rdev)) {
  822. pr_debug("RIO: device access failed - get link partner\n");
  823. /* Scan route to the device and identify failed link.
  824. * This will replace device and port reported in PW message.
  825. * PW message should not be used after this point.
  826. */
  827. if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
  828. pr_err("RIO: Route trace for %s failed\n",
  829. rio_name(rdev));
  830. return -EIO;
  831. }
  832. pw_msg = NULL;
  833. }
  834. /* For End-point devices processing stops here */
  835. if (!(rdev->pef & RIO_PEF_SWITCH))
  836. return 0;
  837. if (rdev->phys_efptr == 0) {
  838. pr_err("RIO_PW: Bad switch initialization for %s\n",
  839. rio_name(rdev));
  840. return 0;
  841. }
  842. /*
  843. * Process the port-write notification from switch
  844. */
  845. if (rdev->rswitch->em_handle)
  846. rdev->rswitch->em_handle(rdev, portnum);
  847. rio_read_config_32(rdev,
  848. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
  849. &err_status);
  850. pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
  851. if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
  852. if (!(rdev->rswitch->port_ok & (1 << portnum))) {
  853. rdev->rswitch->port_ok |= (1 << portnum);
  854. rio_set_port_lockout(rdev, portnum, 0);
  855. /* Schedule Insertion Service */
  856. pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
  857. rio_name(rdev), portnum);
  858. }
  859. /* Clear error-stopped states (if reported).
  860. * Depending on the link partner state, two attempts
  861. * may be needed for successful recovery.
  862. */
  863. if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
  864. RIO_PORT_N_ERR_STS_PW_INP_ES)) {
  865. if (rio_clr_err_stopped(rdev, portnum, err_status))
  866. rio_clr_err_stopped(rdev, portnum, 0);
  867. }
  868. } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
  869. if (rdev->rswitch->port_ok & (1 << portnum)) {
  870. rdev->rswitch->port_ok &= ~(1 << portnum);
  871. rio_set_port_lockout(rdev, portnum, 1);
  872. rio_write_config_32(rdev,
  873. rdev->phys_efptr +
  874. RIO_PORT_N_ACK_STS_CSR(portnum),
  875. RIO_PORT_N_ACK_CLEAR);
  876. /* Schedule Extraction Service */
  877. pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
  878. rio_name(rdev), portnum);
  879. }
  880. }
  881. rio_read_config_32(rdev,
  882. rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
  883. if (em_perrdet) {
  884. pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
  885. portnum, em_perrdet);
  886. /* Clear EM Port N Error Detect CSR */
  887. rio_write_config_32(rdev,
  888. rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
  889. }
  890. rio_read_config_32(rdev,
  891. rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
  892. if (em_ltlerrdet) {
  893. pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
  894. em_ltlerrdet);
  895. /* Clear EM L/T Layer Error Detect CSR */
  896. rio_write_config_32(rdev,
  897. rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
  898. }
  899. /* Clear remaining error bits and Port-Write Pending bit */
  900. rio_write_config_32(rdev,
  901. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
  902. err_status);
  903. return 0;
  904. }
  905. EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
  906. /**
  907. * rio_mport_get_efb - get pointer to next extended features block
  908. * @port: Master port to issue transaction
  909. * @local: Indicate a local master port or remote device access
  910. * @destid: Destination ID of the device
  911. * @hopcount: Number of switch hops to the device
  912. * @from: Offset of current Extended Feature block header (if 0 starts
  913. * from ExtFeaturePtr)
  914. */
  915. u32
  916. rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
  917. u8 hopcount, u32 from)
  918. {
  919. u32 reg_val;
  920. if (from == 0) {
  921. if (local)
  922. rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
  923. &reg_val);
  924. else
  925. rio_mport_read_config_32(port, destid, hopcount,
  926. RIO_ASM_INFO_CAR, &reg_val);
  927. return reg_val & RIO_EXT_FTR_PTR_MASK;
  928. } else {
  929. if (local)
  930. rio_local_read_config_32(port, from, &reg_val);
  931. else
  932. rio_mport_read_config_32(port, destid, hopcount,
  933. from, &reg_val);
  934. return RIO_GET_BLOCK_ID(reg_val);
  935. }
  936. }
  937. EXPORT_SYMBOL_GPL(rio_mport_get_efb);
  938. /**
  939. * rio_mport_get_feature - query for devices' extended features
  940. * @port: Master port to issue transaction
  941. * @local: Indicate a local master port or remote device access
  942. * @destid: Destination ID of the device
  943. * @hopcount: Number of switch hops to the device
  944. * @ftr: Extended feature code
  945. *
  946. * Tell if a device supports a given RapidIO capability.
  947. * Returns the offset of the requested extended feature
  948. * block within the device's RIO configuration space or
  949. * 0 in case the device does not support it. Possible
  950. * values for @ftr:
  951. *
  952. * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
  953. *
  954. * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
  955. *
  956. * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
  957. *
  958. * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
  959. *
  960. * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
  961. *
  962. * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
  963. */
  964. u32
  965. rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
  966. u8 hopcount, int ftr)
  967. {
  968. u32 asm_info, ext_ftr_ptr, ftr_header;
  969. if (local)
  970. rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
  971. else
  972. rio_mport_read_config_32(port, destid, hopcount,
  973. RIO_ASM_INFO_CAR, &asm_info);
  974. ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
  975. while (ext_ftr_ptr) {
  976. if (local)
  977. rio_local_read_config_32(port, ext_ftr_ptr,
  978. &ftr_header);
  979. else
  980. rio_mport_read_config_32(port, destid, hopcount,
  981. ext_ftr_ptr, &ftr_header);
  982. if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
  983. return ext_ftr_ptr;
  984. if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
  985. break;
  986. }
  987. return 0;
  988. }
  989. EXPORT_SYMBOL_GPL(rio_mport_get_feature);
  990. /**
  991. * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
  992. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  993. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  994. * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
  995. * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
  996. * @from: Previous RIO device found in search, or %NULL for new search
  997. *
  998. * Iterates through the list of known RIO devices. If a RIO device is
  999. * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
  1000. * count to the device is incrememted and a pointer to its device
  1001. * structure is returned. Otherwise, %NULL is returned. A new search
  1002. * is initiated by passing %NULL to the @from argument. Otherwise, if
  1003. * @from is not %NULL, searches continue from next device on the global
  1004. * list. The reference count for @from is always decremented if it is
  1005. * not %NULL.
  1006. */
  1007. struct rio_dev *rio_get_asm(u16 vid, u16 did,
  1008. u16 asm_vid, u16 asm_did, struct rio_dev *from)
  1009. {
  1010. struct list_head *n;
  1011. struct rio_dev *rdev;
  1012. WARN_ON(in_interrupt());
  1013. spin_lock(&rio_global_list_lock);
  1014. n = from ? from->global_list.next : rio_devices.next;
  1015. while (n && (n != &rio_devices)) {
  1016. rdev = rio_dev_g(n);
  1017. if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
  1018. (did == RIO_ANY_ID || rdev->did == did) &&
  1019. (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
  1020. (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
  1021. goto exit;
  1022. n = n->next;
  1023. }
  1024. rdev = NULL;
  1025. exit:
  1026. rio_dev_put(from);
  1027. rdev = rio_dev_get(rdev);
  1028. spin_unlock(&rio_global_list_lock);
  1029. return rdev;
  1030. }
  1031. /**
  1032. * rio_get_device - Begin or continue searching for a RIO device by vid/did
  1033. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  1034. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  1035. * @from: Previous RIO device found in search, or %NULL for new search
  1036. *
  1037. * Iterates through the list of known RIO devices. If a RIO device is
  1038. * found with a matching @vid and @did, the reference count to the
  1039. * device is incrememted and a pointer to its device structure is returned.
  1040. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  1041. * to the @from argument. Otherwise, if @from is not %NULL, searches
  1042. * continue from next device on the global list. The reference count for
  1043. * @from is always decremented if it is not %NULL.
  1044. */
  1045. struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
  1046. {
  1047. return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
  1048. }
  1049. /**
  1050. * rio_std_route_add_entry - Add switch route table entry using standard
  1051. * registers defined in RIO specification rev.1.3
  1052. * @mport: Master port to issue transaction
  1053. * @destid: Destination ID of the device
  1054. * @hopcount: Number of switch hops to the device
  1055. * @table: routing table ID (global or port-specific)
  1056. * @route_destid: destID entry in the RT
  1057. * @route_port: destination port for specified destID
  1058. */
  1059. int rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  1060. u16 table, u16 route_destid, u8 route_port)
  1061. {
  1062. if (table == RIO_GLOBAL_TABLE) {
  1063. rio_mport_write_config_32(mport, destid, hopcount,
  1064. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  1065. (u32)route_destid);
  1066. rio_mport_write_config_32(mport, destid, hopcount,
  1067. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  1068. (u32)route_port);
  1069. }
  1070. udelay(10);
  1071. return 0;
  1072. }
  1073. /**
  1074. * rio_std_route_get_entry - Read switch route table entry (port number)
  1075. * associated with specified destID using standard registers defined in RIO
  1076. * specification rev.1.3
  1077. * @mport: Master port to issue transaction
  1078. * @destid: Destination ID of the device
  1079. * @hopcount: Number of switch hops to the device
  1080. * @table: routing table ID (global or port-specific)
  1081. * @route_destid: destID entry in the RT
  1082. * @route_port: returned destination port for specified destID
  1083. */
  1084. int rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  1085. u16 table, u16 route_destid, u8 *route_port)
  1086. {
  1087. u32 result;
  1088. if (table == RIO_GLOBAL_TABLE) {
  1089. rio_mport_write_config_32(mport, destid, hopcount,
  1090. RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
  1091. rio_mport_read_config_32(mport, destid, hopcount,
  1092. RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
  1093. *route_port = (u8)result;
  1094. }
  1095. return 0;
  1096. }
  1097. /**
  1098. * rio_std_route_clr_table - Clear swotch route table using standard registers
  1099. * defined in RIO specification rev.1.3.
  1100. * @mport: Master port to issue transaction
  1101. * @destid: Destination ID of the device
  1102. * @hopcount: Number of switch hops to the device
  1103. * @table: routing table ID (global or port-specific)
  1104. */
  1105. int rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
  1106. u16 table)
  1107. {
  1108. u32 max_destid = 0xff;
  1109. u32 i, pef, id_inc = 1, ext_cfg = 0;
  1110. u32 port_sel = RIO_INVALID_ROUTE;
  1111. if (table == RIO_GLOBAL_TABLE) {
  1112. rio_mport_read_config_32(mport, destid, hopcount,
  1113. RIO_PEF_CAR, &pef);
  1114. if (mport->sys_size) {
  1115. rio_mport_read_config_32(mport, destid, hopcount,
  1116. RIO_SWITCH_RT_LIMIT,
  1117. &max_destid);
  1118. max_destid &= RIO_RT_MAX_DESTID;
  1119. }
  1120. if (pef & RIO_PEF_EXT_RT) {
  1121. ext_cfg = 0x80000000;
  1122. id_inc = 4;
  1123. port_sel = (RIO_INVALID_ROUTE << 24) |
  1124. (RIO_INVALID_ROUTE << 16) |
  1125. (RIO_INVALID_ROUTE << 8) |
  1126. RIO_INVALID_ROUTE;
  1127. }
  1128. for (i = 0; i <= max_destid;) {
  1129. rio_mport_write_config_32(mport, destid, hopcount,
  1130. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  1131. ext_cfg | i);
  1132. rio_mport_write_config_32(mport, destid, hopcount,
  1133. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  1134. port_sel);
  1135. i += id_inc;
  1136. }
  1137. }
  1138. udelay(10);
  1139. return 0;
  1140. }
  1141. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  1142. static bool rio_chan_filter(struct dma_chan *chan, void *arg)
  1143. {
  1144. struct rio_dev *rdev = arg;
  1145. /* Check that DMA device belongs to the right MPORT */
  1146. return (rdev->net->hport ==
  1147. container_of(chan->device, struct rio_mport, dma));
  1148. }
  1149. /**
  1150. * rio_request_dma - request RapidIO capable DMA channel that supports
  1151. * specified target RapidIO device.
  1152. * @rdev: RIO device control structure
  1153. *
  1154. * Returns pointer to allocated DMA channel or NULL if failed.
  1155. */
  1156. struct dma_chan *rio_request_dma(struct rio_dev *rdev)
  1157. {
  1158. dma_cap_mask_t mask;
  1159. struct dma_chan *dchan;
  1160. dma_cap_zero(mask);
  1161. dma_cap_set(DMA_SLAVE, mask);
  1162. dchan = dma_request_channel(mask, rio_chan_filter, rdev);
  1163. return dchan;
  1164. }
  1165. EXPORT_SYMBOL_GPL(rio_request_dma);
  1166. /**
  1167. * rio_release_dma - release specified DMA channel
  1168. * @dchan: DMA channel to release
  1169. */
  1170. void rio_release_dma(struct dma_chan *dchan)
  1171. {
  1172. dma_release_channel(dchan);
  1173. }
  1174. EXPORT_SYMBOL_GPL(rio_release_dma);
  1175. /**
  1176. * rio_dma_prep_slave_sg - RapidIO specific wrapper
  1177. * for device_prep_slave_sg callback defined by DMAENGINE.
  1178. * @rdev: RIO device control structure
  1179. * @dchan: DMA channel to configure
  1180. * @data: RIO specific data descriptor
  1181. * @direction: DMA data transfer direction (TO or FROM the device)
  1182. * @flags: dmaengine defined flags
  1183. *
  1184. * Initializes RapidIO capable DMA channel for the specified data transfer.
  1185. * Uses DMA channel private extension to pass information related to remote
  1186. * target RIO device.
  1187. * Returns pointer to DMA transaction descriptor or NULL if failed.
  1188. */
  1189. struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
  1190. struct dma_chan *dchan, struct rio_dma_data *data,
  1191. enum dma_transfer_direction direction, unsigned long flags)
  1192. {
  1193. struct dma_async_tx_descriptor *txd = NULL;
  1194. struct rio_dma_ext rio_ext;
  1195. if (dchan->device->device_prep_slave_sg == NULL) {
  1196. pr_err("%s: prep_rio_sg == NULL\n", __func__);
  1197. return NULL;
  1198. }
  1199. rio_ext.destid = rdev->destid;
  1200. rio_ext.rio_addr_u = data->rio_addr_u;
  1201. rio_ext.rio_addr = data->rio_addr;
  1202. rio_ext.wr_type = data->wr_type;
  1203. txd = dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
  1204. direction, flags, &rio_ext);
  1205. return txd;
  1206. }
  1207. EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
  1208. #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
  1209. /**
  1210. * rio_register_scan - enumeration/discovery method registration interface
  1211. * @mport_id: mport device ID for which fabric scan routine has to be set
  1212. * (RIO_MPORT_ANY = set for all available mports)
  1213. * @scan_ops: enumeration/discovery control structure
  1214. *
  1215. * Assigns enumeration or discovery method to the specified mport device (or all
  1216. * available mports if RIO_MPORT_ANY is specified).
  1217. * Returns error if the mport already has an enumerator attached to it.
  1218. * In case of RIO_MPORT_ANY ignores ports with valid scan routines and returns
  1219. * an error if was unable to find at least one available mport.
  1220. */
  1221. int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
  1222. {
  1223. struct rio_mport *port;
  1224. int rc = -EBUSY;
  1225. mutex_lock(&rio_mport_list_lock);
  1226. list_for_each_entry(port, &rio_mports, node) {
  1227. if (port->id == mport_id || mport_id == RIO_MPORT_ANY) {
  1228. if (port->nscan && mport_id == RIO_MPORT_ANY)
  1229. continue;
  1230. else if (port->nscan)
  1231. break;
  1232. port->nscan = scan_ops;
  1233. rc = 0;
  1234. if (mport_id != RIO_MPORT_ANY)
  1235. break;
  1236. }
  1237. }
  1238. mutex_unlock(&rio_mport_list_lock);
  1239. return rc;
  1240. }
  1241. EXPORT_SYMBOL_GPL(rio_register_scan);
  1242. /**
  1243. * rio_unregister_scan - removes enumeration/discovery method from mport
  1244. * @mport_id: mport device ID for which fabric scan routine has to be
  1245. * unregistered (RIO_MPORT_ANY = set for all available mports)
  1246. *
  1247. * Removes enumeration or discovery method assigned to the specified mport
  1248. * device (or all available mports if RIO_MPORT_ANY is specified).
  1249. */
  1250. int rio_unregister_scan(int mport_id)
  1251. {
  1252. struct rio_mport *port;
  1253. mutex_lock(&rio_mport_list_lock);
  1254. list_for_each_entry(port, &rio_mports, node) {
  1255. if (port->id == mport_id || mport_id == RIO_MPORT_ANY) {
  1256. if (port->nscan)
  1257. port->nscan = NULL;
  1258. if (mport_id != RIO_MPORT_ANY)
  1259. break;
  1260. }
  1261. }
  1262. mutex_unlock(&rio_mport_list_lock);
  1263. return 0;
  1264. }
  1265. EXPORT_SYMBOL_GPL(rio_unregister_scan);
  1266. static void rio_fixup_device(struct rio_dev *dev)
  1267. {
  1268. }
  1269. static int rio_init(void)
  1270. {
  1271. struct rio_dev *dev = NULL;
  1272. while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
  1273. rio_fixup_device(dev);
  1274. }
  1275. return 0;
  1276. }
  1277. static struct workqueue_struct *rio_wq;
  1278. struct rio_disc_work {
  1279. struct work_struct work;
  1280. struct rio_mport *mport;
  1281. };
  1282. static void disc_work_handler(struct work_struct *_work)
  1283. {
  1284. struct rio_disc_work *work;
  1285. work = container_of(_work, struct rio_disc_work, work);
  1286. pr_debug("RIO: discovery work for mport %d %s\n",
  1287. work->mport->id, work->mport->name);
  1288. work->mport->nscan->discover(work->mport);
  1289. }
  1290. int rio_init_mports(void)
  1291. {
  1292. struct rio_mport *port;
  1293. struct rio_disc_work *work;
  1294. int n = 0;
  1295. if (!next_portid)
  1296. return -ENODEV;
  1297. /*
  1298. * First, run enumerations and check if we need to perform discovery
  1299. * on any of the registered mports.
  1300. */
  1301. mutex_lock(&rio_mport_list_lock);
  1302. list_for_each_entry(port, &rio_mports, node) {
  1303. if (port->host_deviceid >= 0) {
  1304. if (port->nscan)
  1305. port->nscan->enumerate(port);
  1306. } else
  1307. n++;
  1308. }
  1309. mutex_unlock(&rio_mport_list_lock);
  1310. if (!n)
  1311. goto no_disc;
  1312. /*
  1313. * If we have mports that require discovery schedule a discovery work
  1314. * for each of them. If the code below fails to allocate needed
  1315. * resources, exit without error to keep results of enumeration
  1316. * process (if any).
  1317. * TODO: Implement restart of dicovery process for all or
  1318. * individual discovering mports.
  1319. */
  1320. rio_wq = alloc_workqueue("riodisc", 0, 0);
  1321. if (!rio_wq) {
  1322. pr_err("RIO: unable allocate rio_wq\n");
  1323. goto no_disc;
  1324. }
  1325. work = kcalloc(n, sizeof *work, GFP_KERNEL);
  1326. if (!work) {
  1327. pr_err("RIO: no memory for work struct\n");
  1328. destroy_workqueue(rio_wq);
  1329. goto no_disc;
  1330. }
  1331. n = 0;
  1332. mutex_lock(&rio_mport_list_lock);
  1333. list_for_each_entry(port, &rio_mports, node) {
  1334. if (port->host_deviceid < 0 && port->nscan) {
  1335. work[n].mport = port;
  1336. INIT_WORK(&work[n].work, disc_work_handler);
  1337. queue_work(rio_wq, &work[n].work);
  1338. n++;
  1339. }
  1340. }
  1341. mutex_unlock(&rio_mport_list_lock);
  1342. flush_workqueue(rio_wq);
  1343. pr_debug("RIO: destroy discovery workqueue\n");
  1344. destroy_workqueue(rio_wq);
  1345. kfree(work);
  1346. no_disc:
  1347. rio_init();
  1348. return 0;
  1349. }
  1350. static int hdids[RIO_MAX_MPORTS + 1];
  1351. static int rio_get_hdid(int index)
  1352. {
  1353. if (!hdids[0] || hdids[0] <= index || index >= RIO_MAX_MPORTS)
  1354. return -1;
  1355. return hdids[index + 1];
  1356. }
  1357. static int rio_hdid_setup(char *str)
  1358. {
  1359. (void)get_options(str, ARRAY_SIZE(hdids), hdids);
  1360. return 1;
  1361. }
  1362. __setup("riohdid=", rio_hdid_setup);
  1363. int rio_register_mport(struct rio_mport *port)
  1364. {
  1365. if (next_portid >= RIO_MAX_MPORTS) {
  1366. pr_err("RIO: reached specified max number of mports\n");
  1367. return 1;
  1368. }
  1369. port->id = next_portid++;
  1370. port->host_deviceid = rio_get_hdid(port->id);
  1371. port->nscan = NULL;
  1372. mutex_lock(&rio_mport_list_lock);
  1373. list_add_tail(&port->node, &rio_mports);
  1374. mutex_unlock(&rio_mport_list_lock);
  1375. return 0;
  1376. }
  1377. EXPORT_SYMBOL_GPL(rio_local_get_device_id);
  1378. EXPORT_SYMBOL_GPL(rio_get_device);
  1379. EXPORT_SYMBOL_GPL(rio_get_asm);
  1380. EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
  1381. EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
  1382. EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
  1383. EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
  1384. EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
  1385. EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
  1386. EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
  1387. EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
  1388. EXPORT_SYMBOL_GPL(rio_init_mports);