rio.c 63 KB

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