rio.c 63 KB

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