rio.c 60 KB

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