rio.c 54 KB

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