rio.c 52 KB

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