rio.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  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 <mporter@kernel.crashing.org>
  8. *
  9. * Copyright 2009 - 2013 Integrated Device Technology, Inc.
  10. * Alex Bounine <alexandre.bounine@idt.com>
  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 <mporter@kernel.crashing.org>");
  39. MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
  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_inb_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_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
  1239. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  1240. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  1241. * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
  1242. * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
  1243. * @from: Previous RIO device found in search, or %NULL for new search
  1244. *
  1245. * Iterates through the list of known RIO devices. If a RIO device is
  1246. * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
  1247. * count to the device is incrememted and a pointer to its device
  1248. * structure is returned. Otherwise, %NULL is returned. A new search
  1249. * is initiated by passing %NULL to the @from argument. Otherwise, if
  1250. * @from is not %NULL, searches continue from next device on the global
  1251. * list. The reference count for @from is always decremented if it is
  1252. * not %NULL.
  1253. */
  1254. struct rio_dev *rio_get_asm(u16 vid, u16 did,
  1255. u16 asm_vid, u16 asm_did, struct rio_dev *from)
  1256. {
  1257. struct list_head *n;
  1258. struct rio_dev *rdev;
  1259. WARN_ON(in_interrupt());
  1260. spin_lock(&rio_global_list_lock);
  1261. n = from ? from->global_list.next : rio_devices.next;
  1262. while (n && (n != &rio_devices)) {
  1263. rdev = rio_dev_g(n);
  1264. if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
  1265. (did == RIO_ANY_ID || rdev->did == did) &&
  1266. (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
  1267. (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
  1268. goto exit;
  1269. n = n->next;
  1270. }
  1271. rdev = NULL;
  1272. exit:
  1273. rio_dev_put(from);
  1274. rdev = rio_dev_get(rdev);
  1275. spin_unlock(&rio_global_list_lock);
  1276. return rdev;
  1277. }
  1278. EXPORT_SYMBOL_GPL(rio_get_asm);
  1279. /**
  1280. * rio_get_device - Begin or continue searching for a RIO device by vid/did
  1281. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  1282. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  1283. * @from: Previous RIO device found in search, or %NULL for new search
  1284. *
  1285. * Iterates through the list of known RIO devices. If a RIO device is
  1286. * found with a matching @vid and @did, the reference count to the
  1287. * device is incrememted and a pointer to its device structure is returned.
  1288. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  1289. * to the @from argument. Otherwise, if @from is not %NULL, searches
  1290. * continue from next device on the global list. The reference count for
  1291. * @from is always decremented if it is not %NULL.
  1292. */
  1293. struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
  1294. {
  1295. return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
  1296. }
  1297. EXPORT_SYMBOL_GPL(rio_get_device);
  1298. /**
  1299. * rio_std_route_add_entry - Add switch route table entry using standard
  1300. * registers defined in RIO specification rev.1.3
  1301. * @mport: Master port to issue transaction
  1302. * @destid: Destination ID of the device
  1303. * @hopcount: Number of switch hops to the device
  1304. * @table: routing table ID (global or port-specific)
  1305. * @route_destid: destID entry in the RT
  1306. * @route_port: destination port for specified destID
  1307. */
  1308. static int
  1309. rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  1310. u16 table, u16 route_destid, u8 route_port)
  1311. {
  1312. if (table == RIO_GLOBAL_TABLE) {
  1313. rio_mport_write_config_32(mport, destid, hopcount,
  1314. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  1315. (u32)route_destid);
  1316. rio_mport_write_config_32(mport, destid, hopcount,
  1317. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  1318. (u32)route_port);
  1319. }
  1320. udelay(10);
  1321. return 0;
  1322. }
  1323. /**
  1324. * rio_std_route_get_entry - Read switch route table entry (port number)
  1325. * associated with specified destID using standard registers defined in RIO
  1326. * specification rev.1.3
  1327. * @mport: Master port to issue transaction
  1328. * @destid: Destination ID of the device
  1329. * @hopcount: Number of switch hops to the device
  1330. * @table: routing table ID (global or port-specific)
  1331. * @route_destid: destID entry in the RT
  1332. * @route_port: returned destination port for specified destID
  1333. */
  1334. static int
  1335. rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  1336. u16 table, u16 route_destid, u8 *route_port)
  1337. {
  1338. u32 result;
  1339. if (table == RIO_GLOBAL_TABLE) {
  1340. rio_mport_write_config_32(mport, destid, hopcount,
  1341. RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
  1342. rio_mport_read_config_32(mport, destid, hopcount,
  1343. RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
  1344. *route_port = (u8)result;
  1345. }
  1346. return 0;
  1347. }
  1348. /**
  1349. * rio_std_route_clr_table - Clear swotch route table using standard registers
  1350. * defined in RIO specification rev.1.3.
  1351. * @mport: Master port to issue transaction
  1352. * @destid: Destination ID of the device
  1353. * @hopcount: Number of switch hops to the device
  1354. * @table: routing table ID (global or port-specific)
  1355. */
  1356. static int
  1357. rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
  1358. u16 table)
  1359. {
  1360. u32 max_destid = 0xff;
  1361. u32 i, pef, id_inc = 1, ext_cfg = 0;
  1362. u32 port_sel = RIO_INVALID_ROUTE;
  1363. if (table == RIO_GLOBAL_TABLE) {
  1364. rio_mport_read_config_32(mport, destid, hopcount,
  1365. RIO_PEF_CAR, &pef);
  1366. if (mport->sys_size) {
  1367. rio_mport_read_config_32(mport, destid, hopcount,
  1368. RIO_SWITCH_RT_LIMIT,
  1369. &max_destid);
  1370. max_destid &= RIO_RT_MAX_DESTID;
  1371. }
  1372. if (pef & RIO_PEF_EXT_RT) {
  1373. ext_cfg = 0x80000000;
  1374. id_inc = 4;
  1375. port_sel = (RIO_INVALID_ROUTE << 24) |
  1376. (RIO_INVALID_ROUTE << 16) |
  1377. (RIO_INVALID_ROUTE << 8) |
  1378. RIO_INVALID_ROUTE;
  1379. }
  1380. for (i = 0; i <= max_destid;) {
  1381. rio_mport_write_config_32(mport, destid, hopcount,
  1382. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  1383. ext_cfg | i);
  1384. rio_mport_write_config_32(mport, destid, hopcount,
  1385. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  1386. port_sel);
  1387. i += id_inc;
  1388. }
  1389. }
  1390. udelay(10);
  1391. return 0;
  1392. }
  1393. /**
  1394. * rio_lock_device - Acquires host device lock for specified device
  1395. * @port: Master port to send transaction
  1396. * @destid: Destination ID for device/switch
  1397. * @hopcount: Hopcount to reach switch
  1398. * @wait_ms: Max wait time in msec (0 = no timeout)
  1399. *
  1400. * Attepts to acquire host device lock for specified device
  1401. * Returns 0 if device lock acquired or EINVAL if timeout expires.
  1402. */
  1403. int rio_lock_device(struct rio_mport *port, u16 destid,
  1404. u8 hopcount, int wait_ms)
  1405. {
  1406. u32 result;
  1407. int tcnt = 0;
  1408. /* Attempt to acquire device lock */
  1409. rio_mport_write_config_32(port, destid, hopcount,
  1410. RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  1411. rio_mport_read_config_32(port, destid, hopcount,
  1412. RIO_HOST_DID_LOCK_CSR, &result);
  1413. while (result != port->host_deviceid) {
  1414. if (wait_ms != 0 && tcnt == wait_ms) {
  1415. pr_debug("RIO: timeout when locking device %x:%x\n",
  1416. destid, hopcount);
  1417. return -EINVAL;
  1418. }
  1419. /* Delay a bit */
  1420. mdelay(1);
  1421. tcnt++;
  1422. /* Try to acquire device lock again */
  1423. rio_mport_write_config_32(port, destid,
  1424. hopcount,
  1425. RIO_HOST_DID_LOCK_CSR,
  1426. port->host_deviceid);
  1427. rio_mport_read_config_32(port, destid,
  1428. hopcount,
  1429. RIO_HOST_DID_LOCK_CSR, &result);
  1430. }
  1431. return 0;
  1432. }
  1433. EXPORT_SYMBOL_GPL(rio_lock_device);
  1434. /**
  1435. * rio_unlock_device - Releases host device lock for specified device
  1436. * @port: Master port to send transaction
  1437. * @destid: Destination ID for device/switch
  1438. * @hopcount: Hopcount to reach switch
  1439. *
  1440. * Returns 0 if device lock released or EINVAL if fails.
  1441. */
  1442. int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
  1443. {
  1444. u32 result;
  1445. /* Release device lock */
  1446. rio_mport_write_config_32(port, destid,
  1447. hopcount,
  1448. RIO_HOST_DID_LOCK_CSR,
  1449. port->host_deviceid);
  1450. rio_mport_read_config_32(port, destid, hopcount,
  1451. RIO_HOST_DID_LOCK_CSR, &result);
  1452. if ((result & 0xffff) != 0xffff) {
  1453. pr_debug("RIO: badness when releasing device lock %x:%x\n",
  1454. destid, hopcount);
  1455. return -EINVAL;
  1456. }
  1457. return 0;
  1458. }
  1459. EXPORT_SYMBOL_GPL(rio_unlock_device);
  1460. /**
  1461. * rio_route_add_entry- Add a route entry to a switch routing table
  1462. * @rdev: RIO device
  1463. * @table: Routing table ID
  1464. * @route_destid: Destination ID to be routed
  1465. * @route_port: Port number to be routed
  1466. * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
  1467. *
  1468. * If available calls the switch specific add_entry() method to add a route
  1469. * entry into a switch routing table. Otherwise uses standard RT update method
  1470. * as defined by RapidIO specification. A specific routing table can be selected
  1471. * using the @table argument if a switch has per port routing tables or
  1472. * the standard (or global) table may be used by passing
  1473. * %RIO_GLOBAL_TABLE in @table.
  1474. *
  1475. * Returns %0 on success or %-EINVAL on failure.
  1476. */
  1477. int rio_route_add_entry(struct rio_dev *rdev,
  1478. u16 table, u16 route_destid, u8 route_port, int lock)
  1479. {
  1480. int rc = -EINVAL;
  1481. struct rio_switch_ops *ops = rdev->rswitch->ops;
  1482. if (lock) {
  1483. rc = rio_lock_device(rdev->net->hport, rdev->destid,
  1484. rdev->hopcount, 1000);
  1485. if (rc)
  1486. return rc;
  1487. }
  1488. spin_lock(&rdev->rswitch->lock);
  1489. if (!ops || !ops->add_entry) {
  1490. rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
  1491. rdev->hopcount, table,
  1492. route_destid, route_port);
  1493. } else if (try_module_get(ops->owner)) {
  1494. rc = ops->add_entry(rdev->net->hport, rdev->destid,
  1495. rdev->hopcount, table, route_destid,
  1496. route_port);
  1497. module_put(ops->owner);
  1498. }
  1499. spin_unlock(&rdev->rswitch->lock);
  1500. if (lock)
  1501. rio_unlock_device(rdev->net->hport, rdev->destid,
  1502. rdev->hopcount);
  1503. return rc;
  1504. }
  1505. EXPORT_SYMBOL_GPL(rio_route_add_entry);
  1506. /**
  1507. * rio_route_get_entry- Read an entry from a switch routing table
  1508. * @rdev: RIO device
  1509. * @table: Routing table ID
  1510. * @route_destid: Destination ID to be routed
  1511. * @route_port: Pointer to read port number into
  1512. * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
  1513. *
  1514. * If available calls the switch specific get_entry() method to fetch a route
  1515. * entry from a switch routing table. Otherwise uses standard RT read method
  1516. * as defined by RapidIO specification. A specific routing table can be selected
  1517. * using the @table argument if a switch has per port routing tables or
  1518. * the standard (or global) table may be used by passing
  1519. * %RIO_GLOBAL_TABLE in @table.
  1520. *
  1521. * Returns %0 on success or %-EINVAL on failure.
  1522. */
  1523. int rio_route_get_entry(struct rio_dev *rdev, u16 table,
  1524. u16 route_destid, u8 *route_port, int lock)
  1525. {
  1526. int rc = -EINVAL;
  1527. struct rio_switch_ops *ops = rdev->rswitch->ops;
  1528. if (lock) {
  1529. rc = rio_lock_device(rdev->net->hport, rdev->destid,
  1530. rdev->hopcount, 1000);
  1531. if (rc)
  1532. return rc;
  1533. }
  1534. spin_lock(&rdev->rswitch->lock);
  1535. if (!ops || !ops->get_entry) {
  1536. rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
  1537. rdev->hopcount, table,
  1538. route_destid, route_port);
  1539. } else if (try_module_get(ops->owner)) {
  1540. rc = ops->get_entry(rdev->net->hport, rdev->destid,
  1541. rdev->hopcount, table, route_destid,
  1542. route_port);
  1543. module_put(ops->owner);
  1544. }
  1545. spin_unlock(&rdev->rswitch->lock);
  1546. if (lock)
  1547. rio_unlock_device(rdev->net->hport, rdev->destid,
  1548. rdev->hopcount);
  1549. return rc;
  1550. }
  1551. EXPORT_SYMBOL_GPL(rio_route_get_entry);
  1552. /**
  1553. * rio_route_clr_table - Clear a switch routing table
  1554. * @rdev: RIO device
  1555. * @table: Routing table ID
  1556. * @lock: apply a hardware lock on switch device flag (1=lock, 0=no_lock)
  1557. *
  1558. * If available calls the switch specific clr_table() method to clear a switch
  1559. * routing table. Otherwise uses standard RT write method as defined by RapidIO
  1560. * specification. A specific routing table can be selected using the @table
  1561. * argument if a switch has per port routing tables or the standard (or global)
  1562. * table may be used by passing %RIO_GLOBAL_TABLE in @table.
  1563. *
  1564. * Returns %0 on success or %-EINVAL on failure.
  1565. */
  1566. int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
  1567. {
  1568. int rc = -EINVAL;
  1569. struct rio_switch_ops *ops = rdev->rswitch->ops;
  1570. if (lock) {
  1571. rc = rio_lock_device(rdev->net->hport, rdev->destid,
  1572. rdev->hopcount, 1000);
  1573. if (rc)
  1574. return rc;
  1575. }
  1576. spin_lock(&rdev->rswitch->lock);
  1577. if (!ops || !ops->clr_table) {
  1578. rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
  1579. rdev->hopcount, table);
  1580. } else if (try_module_get(ops->owner)) {
  1581. rc = ops->clr_table(rdev->net->hport, rdev->destid,
  1582. rdev->hopcount, table);
  1583. module_put(ops->owner);
  1584. }
  1585. spin_unlock(&rdev->rswitch->lock);
  1586. if (lock)
  1587. rio_unlock_device(rdev->net->hport, rdev->destid,
  1588. rdev->hopcount);
  1589. return rc;
  1590. }
  1591. EXPORT_SYMBOL_GPL(rio_route_clr_table);
  1592. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  1593. static bool rio_chan_filter(struct dma_chan *chan, void *arg)
  1594. {
  1595. struct rio_mport *mport = arg;
  1596. /* Check that DMA device belongs to the right MPORT */
  1597. return mport == container_of(chan->device, struct rio_mport, dma);
  1598. }
  1599. /**
  1600. * rio_request_mport_dma - request RapidIO capable DMA channel associated
  1601. * with specified local RapidIO mport device.
  1602. * @mport: RIO mport to perform DMA data transfers
  1603. *
  1604. * Returns pointer to allocated DMA channel or NULL if failed.
  1605. */
  1606. struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
  1607. {
  1608. dma_cap_mask_t mask;
  1609. dma_cap_zero(mask);
  1610. dma_cap_set(DMA_SLAVE, mask);
  1611. return dma_request_channel(mask, rio_chan_filter, mport);
  1612. }
  1613. EXPORT_SYMBOL_GPL(rio_request_mport_dma);
  1614. /**
  1615. * rio_request_dma - request RapidIO capable DMA channel that supports
  1616. * specified target RapidIO device.
  1617. * @rdev: RIO device associated with DMA transfer
  1618. *
  1619. * Returns pointer to allocated DMA channel or NULL if failed.
  1620. */
  1621. struct dma_chan *rio_request_dma(struct rio_dev *rdev)
  1622. {
  1623. return rio_request_mport_dma(rdev->net->hport);
  1624. }
  1625. EXPORT_SYMBOL_GPL(rio_request_dma);
  1626. /**
  1627. * rio_release_dma - release specified DMA channel
  1628. * @dchan: DMA channel to release
  1629. */
  1630. void rio_release_dma(struct dma_chan *dchan)
  1631. {
  1632. dma_release_channel(dchan);
  1633. }
  1634. EXPORT_SYMBOL_GPL(rio_release_dma);
  1635. /**
  1636. * rio_dma_prep_xfer - RapidIO specific wrapper
  1637. * for device_prep_slave_sg callback defined by DMAENGINE.
  1638. * @dchan: DMA channel to configure
  1639. * @destid: target RapidIO device destination ID
  1640. * @data: RIO specific data descriptor
  1641. * @direction: DMA data transfer direction (TO or FROM the device)
  1642. * @flags: dmaengine defined flags
  1643. *
  1644. * Initializes RapidIO capable DMA channel for the specified data transfer.
  1645. * Uses DMA channel private extension to pass information related to remote
  1646. * target RIO device.
  1647. *
  1648. * Returns: pointer to DMA transaction descriptor if successful,
  1649. * error-valued pointer or NULL if failed.
  1650. */
  1651. struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
  1652. u16 destid, struct rio_dma_data *data,
  1653. enum dma_transfer_direction direction, unsigned long flags)
  1654. {
  1655. struct rio_dma_ext rio_ext;
  1656. if (!dchan->device->device_prep_slave_sg) {
  1657. pr_err("%s: prep_rio_sg == NULL\n", __func__);
  1658. return NULL;
  1659. }
  1660. rio_ext.destid = destid;
  1661. rio_ext.rio_addr_u = data->rio_addr_u;
  1662. rio_ext.rio_addr = data->rio_addr;
  1663. rio_ext.wr_type = data->wr_type;
  1664. return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
  1665. direction, flags, &rio_ext);
  1666. }
  1667. EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
  1668. /**
  1669. * rio_dma_prep_slave_sg - RapidIO specific wrapper
  1670. * for device_prep_slave_sg callback defined by DMAENGINE.
  1671. * @rdev: RIO device control structure
  1672. * @dchan: DMA channel to configure
  1673. * @data: RIO specific data descriptor
  1674. * @direction: DMA data transfer direction (TO or FROM the device)
  1675. * @flags: dmaengine defined flags
  1676. *
  1677. * Initializes RapidIO capable DMA channel for the specified data transfer.
  1678. * Uses DMA channel private extension to pass information related to remote
  1679. * target RIO device.
  1680. *
  1681. * Returns: pointer to DMA transaction descriptor if successful,
  1682. * error-valued pointer or NULL if failed.
  1683. */
  1684. struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
  1685. struct dma_chan *dchan, struct rio_dma_data *data,
  1686. enum dma_transfer_direction direction, unsigned long flags)
  1687. {
  1688. return rio_dma_prep_xfer(dchan, rdev->destid, data, direction, flags);
  1689. }
  1690. EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
  1691. #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
  1692. /**
  1693. * rio_find_mport - find RIO mport by its ID
  1694. * @mport_id: number (ID) of mport device
  1695. *
  1696. * Given a RIO mport number, the desired mport is located
  1697. * in the global list of mports. If the mport is found, a pointer to its
  1698. * data structure is returned. If no mport is found, %NULL is returned.
  1699. */
  1700. struct rio_mport *rio_find_mport(int mport_id)
  1701. {
  1702. struct rio_mport *port;
  1703. mutex_lock(&rio_mport_list_lock);
  1704. list_for_each_entry(port, &rio_mports, node) {
  1705. if (port->id == mport_id)
  1706. goto found;
  1707. }
  1708. port = NULL;
  1709. found:
  1710. mutex_unlock(&rio_mport_list_lock);
  1711. return port;
  1712. }
  1713. /**
  1714. * rio_register_scan - enumeration/discovery method registration interface
  1715. * @mport_id: mport device ID for which fabric scan routine has to be set
  1716. * (RIO_MPORT_ANY = set for all available mports)
  1717. * @scan_ops: enumeration/discovery operations structure
  1718. *
  1719. * Registers enumeration/discovery operations with RapidIO subsystem and
  1720. * attaches it to the specified mport device (or all available mports
  1721. * if RIO_MPORT_ANY is specified).
  1722. *
  1723. * Returns error if the mport already has an enumerator attached to it.
  1724. * In case of RIO_MPORT_ANY skips mports with valid scan routines (no error).
  1725. */
  1726. int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
  1727. {
  1728. struct rio_mport *port;
  1729. struct rio_scan_node *scan;
  1730. int rc = 0;
  1731. pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
  1732. if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
  1733. !scan_ops)
  1734. return -EINVAL;
  1735. mutex_lock(&rio_mport_list_lock);
  1736. /*
  1737. * Check if there is another enumerator already registered for
  1738. * the same mport ID (including RIO_MPORT_ANY). Multiple enumerators
  1739. * for the same mport ID are not supported.
  1740. */
  1741. list_for_each_entry(scan, &rio_scans, node) {
  1742. if (scan->mport_id == mport_id) {
  1743. rc = -EBUSY;
  1744. goto err_out;
  1745. }
  1746. }
  1747. /*
  1748. * Allocate and initialize new scan registration node.
  1749. */
  1750. scan = kzalloc(sizeof(*scan), GFP_KERNEL);
  1751. if (!scan) {
  1752. rc = -ENOMEM;
  1753. goto err_out;
  1754. }
  1755. scan->mport_id = mport_id;
  1756. scan->ops = scan_ops;
  1757. /*
  1758. * Traverse the list of registered mports to attach this new scan.
  1759. *
  1760. * The new scan with matching mport ID overrides any previously attached
  1761. * scan assuming that old scan (if any) is the default one (based on the
  1762. * enumerator registration check above).
  1763. * If the new scan is the global one, it will be attached only to mports
  1764. * that do not have their own individual operations already attached.
  1765. */
  1766. list_for_each_entry(port, &rio_mports, node) {
  1767. if (port->id == mport_id) {
  1768. port->nscan = scan_ops;
  1769. break;
  1770. } else if (mport_id == RIO_MPORT_ANY && !port->nscan)
  1771. port->nscan = scan_ops;
  1772. }
  1773. list_add_tail(&scan->node, &rio_scans);
  1774. err_out:
  1775. mutex_unlock(&rio_mport_list_lock);
  1776. return rc;
  1777. }
  1778. EXPORT_SYMBOL_GPL(rio_register_scan);
  1779. /**
  1780. * rio_unregister_scan - removes enumeration/discovery method from mport
  1781. * @mport_id: mport device ID for which fabric scan routine has to be
  1782. * unregistered (RIO_MPORT_ANY = apply to all mports that use
  1783. * the specified scan_ops)
  1784. * @scan_ops: enumeration/discovery operations structure
  1785. *
  1786. * Removes enumeration or discovery method assigned to the specified mport
  1787. * device. If RIO_MPORT_ANY is specified, removes the specified operations from
  1788. * all mports that have them attached.
  1789. */
  1790. int rio_unregister_scan(int mport_id, struct rio_scan *scan_ops)
  1791. {
  1792. struct rio_mport *port;
  1793. struct rio_scan_node *scan;
  1794. pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
  1795. if (mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS)
  1796. return -EINVAL;
  1797. mutex_lock(&rio_mport_list_lock);
  1798. list_for_each_entry(port, &rio_mports, node)
  1799. if (port->id == mport_id ||
  1800. (mport_id == RIO_MPORT_ANY && port->nscan == scan_ops))
  1801. port->nscan = NULL;
  1802. list_for_each_entry(scan, &rio_scans, node) {
  1803. if (scan->mport_id == mport_id) {
  1804. list_del(&scan->node);
  1805. kfree(scan);
  1806. break;
  1807. }
  1808. }
  1809. mutex_unlock(&rio_mport_list_lock);
  1810. return 0;
  1811. }
  1812. EXPORT_SYMBOL_GPL(rio_unregister_scan);
  1813. /**
  1814. * rio_mport_scan - execute enumeration/discovery on the specified mport
  1815. * @mport_id: number (ID) of mport device
  1816. */
  1817. int rio_mport_scan(int mport_id)
  1818. {
  1819. struct rio_mport *port = NULL;
  1820. int rc;
  1821. mutex_lock(&rio_mport_list_lock);
  1822. list_for_each_entry(port, &rio_mports, node) {
  1823. if (port->id == mport_id)
  1824. goto found;
  1825. }
  1826. mutex_unlock(&rio_mport_list_lock);
  1827. return -ENODEV;
  1828. found:
  1829. if (!port->nscan) {
  1830. mutex_unlock(&rio_mport_list_lock);
  1831. return -EINVAL;
  1832. }
  1833. if (!try_module_get(port->nscan->owner)) {
  1834. mutex_unlock(&rio_mport_list_lock);
  1835. return -ENODEV;
  1836. }
  1837. mutex_unlock(&rio_mport_list_lock);
  1838. if (port->host_deviceid >= 0)
  1839. rc = port->nscan->enumerate(port, 0);
  1840. else
  1841. rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
  1842. module_put(port->nscan->owner);
  1843. return rc;
  1844. }
  1845. static void rio_fixup_device(struct rio_dev *dev)
  1846. {
  1847. }
  1848. static int rio_init(void)
  1849. {
  1850. struct rio_dev *dev = NULL;
  1851. while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
  1852. rio_fixup_device(dev);
  1853. }
  1854. return 0;
  1855. }
  1856. static struct workqueue_struct *rio_wq;
  1857. struct rio_disc_work {
  1858. struct work_struct work;
  1859. struct rio_mport *mport;
  1860. };
  1861. static void disc_work_handler(struct work_struct *_work)
  1862. {
  1863. struct rio_disc_work *work;
  1864. work = container_of(_work, struct rio_disc_work, work);
  1865. pr_debug("RIO: discovery work for mport %d %s\n",
  1866. work->mport->id, work->mport->name);
  1867. if (try_module_get(work->mport->nscan->owner)) {
  1868. work->mport->nscan->discover(work->mport, 0);
  1869. module_put(work->mport->nscan->owner);
  1870. }
  1871. }
  1872. int rio_init_mports(void)
  1873. {
  1874. struct rio_mport *port;
  1875. struct rio_disc_work *work;
  1876. int n = 0;
  1877. if (!next_portid)
  1878. return -ENODEV;
  1879. /*
  1880. * First, run enumerations and check if we need to perform discovery
  1881. * on any of the registered mports.
  1882. */
  1883. mutex_lock(&rio_mport_list_lock);
  1884. list_for_each_entry(port, &rio_mports, node) {
  1885. if (port->host_deviceid >= 0) {
  1886. if (port->nscan && try_module_get(port->nscan->owner)) {
  1887. port->nscan->enumerate(port, 0);
  1888. module_put(port->nscan->owner);
  1889. }
  1890. } else
  1891. n++;
  1892. }
  1893. mutex_unlock(&rio_mport_list_lock);
  1894. if (!n)
  1895. goto no_disc;
  1896. /*
  1897. * If we have mports that require discovery schedule a discovery work
  1898. * for each of them. If the code below fails to allocate needed
  1899. * resources, exit without error to keep results of enumeration
  1900. * process (if any).
  1901. * TODO: Implement restart of discovery process for all or
  1902. * individual discovering mports.
  1903. */
  1904. rio_wq = alloc_workqueue("riodisc", 0, 0);
  1905. if (!rio_wq) {
  1906. pr_err("RIO: unable allocate rio_wq\n");
  1907. goto no_disc;
  1908. }
  1909. work = kcalloc(n, sizeof *work, GFP_KERNEL);
  1910. if (!work) {
  1911. destroy_workqueue(rio_wq);
  1912. goto no_disc;
  1913. }
  1914. n = 0;
  1915. mutex_lock(&rio_mport_list_lock);
  1916. list_for_each_entry(port, &rio_mports, node) {
  1917. if (port->host_deviceid < 0 && port->nscan) {
  1918. work[n].mport = port;
  1919. INIT_WORK(&work[n].work, disc_work_handler);
  1920. queue_work(rio_wq, &work[n].work);
  1921. n++;
  1922. }
  1923. }
  1924. flush_workqueue(rio_wq);
  1925. mutex_unlock(&rio_mport_list_lock);
  1926. pr_debug("RIO: destroy discovery workqueue\n");
  1927. destroy_workqueue(rio_wq);
  1928. kfree(work);
  1929. no_disc:
  1930. rio_init();
  1931. return 0;
  1932. }
  1933. EXPORT_SYMBOL_GPL(rio_init_mports);
  1934. static int rio_get_hdid(int index)
  1935. {
  1936. if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
  1937. return -1;
  1938. return hdid[index];
  1939. }
  1940. int rio_mport_initialize(struct rio_mport *mport)
  1941. {
  1942. if (next_portid >= RIO_MAX_MPORTS) {
  1943. pr_err("RIO: reached specified max number of mports\n");
  1944. return -ENODEV;
  1945. }
  1946. atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
  1947. mport->id = next_portid++;
  1948. mport->host_deviceid = rio_get_hdid(mport->id);
  1949. mport->nscan = NULL;
  1950. mutex_init(&mport->lock);
  1951. mport->pwe_refcnt = 0;
  1952. INIT_LIST_HEAD(&mport->pwrites);
  1953. return 0;
  1954. }
  1955. EXPORT_SYMBOL_GPL(rio_mport_initialize);
  1956. int rio_register_mport(struct rio_mport *port)
  1957. {
  1958. struct rio_scan_node *scan = NULL;
  1959. int res = 0;
  1960. mutex_lock(&rio_mport_list_lock);
  1961. /*
  1962. * Check if there are any registered enumeration/discovery operations
  1963. * that have to be attached to the added mport.
  1964. */
  1965. list_for_each_entry(scan, &rio_scans, node) {
  1966. if (port->id == scan->mport_id ||
  1967. scan->mport_id == RIO_MPORT_ANY) {
  1968. port->nscan = scan->ops;
  1969. if (port->id == scan->mport_id)
  1970. break;
  1971. }
  1972. }
  1973. list_add_tail(&port->node, &rio_mports);
  1974. mutex_unlock(&rio_mport_list_lock);
  1975. dev_set_name(&port->dev, "rapidio%d", port->id);
  1976. port->dev.class = &rio_mport_class;
  1977. atomic_set(&port->state, RIO_DEVICE_RUNNING);
  1978. res = device_register(&port->dev);
  1979. if (res)
  1980. dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
  1981. port->id, res);
  1982. else
  1983. dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
  1984. return res;
  1985. }
  1986. EXPORT_SYMBOL_GPL(rio_register_mport);
  1987. static int rio_mport_cleanup_callback(struct device *dev, void *data)
  1988. {
  1989. struct rio_dev *rdev = to_rio_dev(dev);
  1990. if (dev->bus == &rio_bus_type)
  1991. rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
  1992. return 0;
  1993. }
  1994. static int rio_net_remove_children(struct rio_net *net)
  1995. {
  1996. /*
  1997. * Unregister all RapidIO devices residing on this net (this will
  1998. * invoke notification of registered subsystem interfaces as well).
  1999. */
  2000. device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
  2001. return 0;
  2002. }
  2003. int rio_unregister_mport(struct rio_mport *port)
  2004. {
  2005. pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
  2006. /* Transition mport to the SHUTDOWN state */
  2007. if (atomic_cmpxchg(&port->state,
  2008. RIO_DEVICE_RUNNING,
  2009. RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
  2010. pr_err("RIO: %s unexpected state transition for mport %s\n",
  2011. __func__, port->name);
  2012. }
  2013. if (port->net && port->net->hport == port) {
  2014. rio_net_remove_children(port->net);
  2015. rio_free_net(port->net);
  2016. }
  2017. /*
  2018. * Unregister all RapidIO devices attached to this mport (this will
  2019. * invoke notification of registered subsystem interfaces as well).
  2020. */
  2021. mutex_lock(&rio_mport_list_lock);
  2022. list_del(&port->node);
  2023. mutex_unlock(&rio_mport_list_lock);
  2024. device_unregister(&port->dev);
  2025. return 0;
  2026. }
  2027. EXPORT_SYMBOL_GPL(rio_unregister_mport);