rio.c 64 KB

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