rio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/config.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.h>
  18. #include <linux/rio.h>
  19. #include <linux/rio_drv.h>
  20. #include <linux/rio_ids.h>
  21. #include <linux/rio_regs.h>
  22. #include <linux/module.h>
  23. #include <linux/spinlock.h>
  24. #include "rio.h"
  25. static LIST_HEAD(rio_mports);
  26. /**
  27. * rio_local_get_device_id - Get the base/extended device id for a port
  28. * @port: RIO master port from which to get the deviceid
  29. *
  30. * Reads the base/extended device id from the local device
  31. * implementing the master port. Returns the 8/16-bit device
  32. * id.
  33. */
  34. u16 rio_local_get_device_id(struct rio_mport *port)
  35. {
  36. u32 result;
  37. rio_local_read_config_32(port, RIO_DID_CSR, &result);
  38. return (RIO_GET_DID(result));
  39. }
  40. /**
  41. * rio_request_inb_mbox - request inbound mailbox service
  42. * @mport: RIO master port from which to allocate the mailbox resource
  43. * @mbox: Mailbox number to claim
  44. * @entries: Number of entries in inbound mailbox queue
  45. * @minb: Callback to execute when inbound message is received
  46. *
  47. * Requests ownership of an inbound mailbox resource and binds
  48. * a callback function to the resource. Returns %0 on success.
  49. */
  50. int rio_request_inb_mbox(struct rio_mport *mport,
  51. int mbox,
  52. int entries,
  53. void (*minb) (struct rio_mport * mport, int mbox,
  54. int slot))
  55. {
  56. int rc = 0;
  57. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  58. if (res) {
  59. rio_init_mbox_res(res, mbox, mbox);
  60. /* Make sure this mailbox isn't in use */
  61. if ((rc =
  62. request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
  63. res)) < 0) {
  64. kfree(res);
  65. goto out;
  66. }
  67. mport->inb_msg[mbox].res = res;
  68. /* Hook the inbound message callback */
  69. mport->inb_msg[mbox].mcback = minb;
  70. rc = rio_open_inb_mbox(mport, mbox, entries);
  71. } else
  72. rc = -ENOMEM;
  73. out:
  74. return rc;
  75. }
  76. /**
  77. * rio_release_inb_mbox - release inbound mailbox message service
  78. * @mport: RIO master port from which to release the mailbox resource
  79. * @mbox: Mailbox number to release
  80. *
  81. * Releases ownership of an inbound mailbox resource. Returns 0
  82. * if the request has been satisfied.
  83. */
  84. int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
  85. {
  86. rio_close_inb_mbox(mport, mbox);
  87. /* Release the mailbox resource */
  88. return release_resource(mport->inb_msg[mbox].res);
  89. }
  90. /**
  91. * rio_request_outb_mbox - request outbound mailbox service
  92. * @mport: RIO master port from which to allocate the mailbox resource
  93. * @mbox: Mailbox number to claim
  94. * @entries: Number of entries in outbound mailbox queue
  95. * @moutb: Callback to execute when outbound message is sent
  96. *
  97. * Requests ownership of an outbound mailbox resource and binds
  98. * a callback function to the resource. Returns 0 on success.
  99. */
  100. int rio_request_outb_mbox(struct rio_mport *mport,
  101. int mbox,
  102. int entries,
  103. void (*moutb) (struct rio_mport * mport, int mbox,
  104. int slot))
  105. {
  106. int rc = 0;
  107. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  108. if (res) {
  109. rio_init_mbox_res(res, mbox, mbox);
  110. /* Make sure this outbound mailbox isn't in use */
  111. if ((rc =
  112. request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
  113. res)) < 0) {
  114. kfree(res);
  115. goto out;
  116. }
  117. mport->outb_msg[mbox].res = res;
  118. /* Hook the inbound message callback */
  119. mport->outb_msg[mbox].mcback = moutb;
  120. rc = rio_open_outb_mbox(mport, mbox, entries);
  121. } else
  122. rc = -ENOMEM;
  123. out:
  124. return rc;
  125. }
  126. /**
  127. * rio_release_outb_mbox - release outbound mailbox message service
  128. * @mport: RIO master port from which to release the mailbox resource
  129. * @mbox: Mailbox number to release
  130. *
  131. * Releases ownership of an inbound mailbox resource. Returns 0
  132. * if the request has been satisfied.
  133. */
  134. int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
  135. {
  136. rio_close_outb_mbox(mport, mbox);
  137. /* Release the mailbox resource */
  138. return release_resource(mport->outb_msg[mbox].res);
  139. }
  140. /**
  141. * rio_setup_inb_dbell - bind inbound doorbell callback
  142. * @mport: RIO master port to bind the doorbell callback
  143. * @res: Doorbell message resource
  144. * @dinb: Callback to execute when doorbell is received
  145. *
  146. * Adds a doorbell resource/callback pair into a port's
  147. * doorbell event list. Returns 0 if the request has been
  148. * satisfied.
  149. */
  150. static int
  151. rio_setup_inb_dbell(struct rio_mport *mport, struct resource *res,
  152. void (*dinb) (struct rio_mport * mport, u16 src, u16 dst,
  153. u16 info))
  154. {
  155. int rc = 0;
  156. struct rio_dbell *dbell;
  157. if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
  158. rc = -ENOMEM;
  159. goto out;
  160. }
  161. dbell->res = res;
  162. dbell->dinb = dinb;
  163. list_add_tail(&dbell->node, &mport->dbells);
  164. out:
  165. return rc;
  166. }
  167. /**
  168. * rio_request_inb_dbell - request inbound doorbell message service
  169. * @mport: RIO master port from which to allocate the doorbell resource
  170. * @start: Doorbell info range start
  171. * @end: Doorbell info range end
  172. * @dinb: Callback to execute when doorbell is received
  173. *
  174. * Requests ownership of an inbound doorbell resource and binds
  175. * a callback function to the resource. Returns 0 if the request
  176. * has been satisfied.
  177. */
  178. int rio_request_inb_dbell(struct rio_mport *mport,
  179. u16 start,
  180. u16 end,
  181. void (*dinb) (struct rio_mport * mport, u16 src,
  182. u16 dst, u16 info))
  183. {
  184. int rc = 0;
  185. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  186. if (res) {
  187. rio_init_dbell_res(res, start, end);
  188. /* Make sure these doorbells aren't in use */
  189. if ((rc =
  190. request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
  191. res)) < 0) {
  192. kfree(res);
  193. goto out;
  194. }
  195. /* Hook the doorbell callback */
  196. rc = rio_setup_inb_dbell(mport, res, dinb);
  197. } else
  198. rc = -ENOMEM;
  199. out:
  200. return rc;
  201. }
  202. /**
  203. * rio_release_inb_dbell - release inbound doorbell message service
  204. * @mport: RIO master port from which to release the doorbell resource
  205. * @start: Doorbell info range start
  206. * @end: Doorbell info range end
  207. *
  208. * Releases ownership of an inbound doorbell resource and removes
  209. * callback from the doorbell event list. Returns 0 if the request
  210. * has been satisfied.
  211. */
  212. int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
  213. {
  214. int rc = 0, found = 0;
  215. struct rio_dbell *dbell;
  216. list_for_each_entry(dbell, &mport->dbells, node) {
  217. if ((dbell->res->start == start) && (dbell->res->end == end)) {
  218. found = 1;
  219. break;
  220. }
  221. }
  222. /* If we can't find an exact match, fail */
  223. if (!found) {
  224. rc = -EINVAL;
  225. goto out;
  226. }
  227. /* Delete from list */
  228. list_del(&dbell->node);
  229. /* Release the doorbell resource */
  230. rc = release_resource(dbell->res);
  231. /* Free the doorbell event */
  232. kfree(dbell);
  233. out:
  234. return rc;
  235. }
  236. /**
  237. * rio_request_outb_dbell - request outbound doorbell message range
  238. * @rdev: RIO device from which to allocate the doorbell resource
  239. * @start: Doorbell message range start
  240. * @end: Doorbell message range end
  241. *
  242. * Requests ownership of a doorbell message range. Returns a resource
  243. * if the request has been satisfied or %NULL on failure.
  244. */
  245. struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
  246. u16 end)
  247. {
  248. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  249. if (res) {
  250. rio_init_dbell_res(res, start, end);
  251. /* Make sure these doorbells aren't in use */
  252. if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
  253. < 0) {
  254. kfree(res);
  255. res = NULL;
  256. }
  257. }
  258. return res;
  259. }
  260. /**
  261. * rio_release_outb_dbell - release outbound doorbell message range
  262. * @rdev: RIO device from which to release the doorbell resource
  263. * @res: Doorbell resource to be freed
  264. *
  265. * Releases ownership of a doorbell message range. Returns 0 if the
  266. * request has been satisfied.
  267. */
  268. int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
  269. {
  270. int rc = release_resource(res);
  271. kfree(res);
  272. return rc;
  273. }
  274. /**
  275. * rio_mport_get_feature - query for devices' extended features
  276. * @port: Master port to issue transaction
  277. * @local: Indicate a local master port or remote device access
  278. * @destid: Destination ID of the device
  279. * @hopcount: Number of switch hops to the device
  280. * @ftr: Extended feature code
  281. *
  282. * Tell if a device supports a given RapidIO capability.
  283. * Returns the offset of the requested extended feature
  284. * block within the device's RIO configuration space or
  285. * 0 in case the device does not support it. Possible
  286. * values for @ftr:
  287. *
  288. * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
  289. *
  290. * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
  291. *
  292. * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
  293. *
  294. * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
  295. *
  296. * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
  297. *
  298. * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
  299. */
  300. u32
  301. rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
  302. u8 hopcount, int ftr)
  303. {
  304. u32 asm_info, ext_ftr_ptr, ftr_header;
  305. if (local)
  306. rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
  307. else
  308. rio_mport_read_config_32(port, destid, hopcount,
  309. RIO_ASM_INFO_CAR, &asm_info);
  310. ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
  311. while (ext_ftr_ptr) {
  312. if (local)
  313. rio_local_read_config_32(port, ext_ftr_ptr,
  314. &ftr_header);
  315. else
  316. rio_mport_read_config_32(port, destid, hopcount,
  317. ext_ftr_ptr, &ftr_header);
  318. if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
  319. return ext_ftr_ptr;
  320. if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
  321. break;
  322. }
  323. return 0;
  324. }
  325. /**
  326. * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
  327. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  328. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  329. * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
  330. * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
  331. * @from: Previous RIO device found in search, or %NULL for new search
  332. *
  333. * Iterates through the list of known RIO devices. If a RIO device is
  334. * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
  335. * count to the device is incrememted and a pointer to its device
  336. * structure is returned. Otherwise, %NULL is returned. A new search
  337. * is initiated by passing %NULL to the @from argument. Otherwise, if
  338. * @from is not %NULL, searches continue from next device on the global
  339. * list. The reference count for @from is always decremented if it is
  340. * not %NULL.
  341. */
  342. struct rio_dev *rio_get_asm(u16 vid, u16 did,
  343. u16 asm_vid, u16 asm_did, struct rio_dev *from)
  344. {
  345. struct list_head *n;
  346. struct rio_dev *rdev;
  347. WARN_ON(in_interrupt());
  348. spin_lock(&rio_global_list_lock);
  349. n = from ? from->global_list.next : rio_devices.next;
  350. while (n && (n != &rio_devices)) {
  351. rdev = rio_dev_g(n);
  352. if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
  353. (did == RIO_ANY_ID || rdev->did == did) &&
  354. (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
  355. (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
  356. goto exit;
  357. n = n->next;
  358. }
  359. rdev = NULL;
  360. exit:
  361. rio_dev_put(from);
  362. rdev = rio_dev_get(rdev);
  363. spin_unlock(&rio_global_list_lock);
  364. return rdev;
  365. }
  366. /**
  367. * rio_get_device - Begin or continue searching for a RIO device by vid/did
  368. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  369. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  370. * @from: Previous RIO device found in search, or %NULL for new search
  371. *
  372. * Iterates through the list of known RIO devices. If a RIO device is
  373. * found with a matching @vid and @did, the reference count to the
  374. * device is incrememted and a pointer to its device structure is returned.
  375. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  376. * to the @from argument. Otherwise, if @from is not %NULL, searches
  377. * continue from next device on the global list. The reference count for
  378. * @from is always decremented if it is not %NULL.
  379. */
  380. struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
  381. {
  382. return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
  383. }
  384. static void rio_fixup_device(struct rio_dev *dev)
  385. {
  386. }
  387. static int __devinit rio_init(void)
  388. {
  389. struct rio_dev *dev = NULL;
  390. while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
  391. rio_fixup_device(dev);
  392. }
  393. return 0;
  394. }
  395. device_initcall(rio_init);
  396. int rio_init_mports(void)
  397. {
  398. int rc = 0;
  399. struct rio_mport *port;
  400. list_for_each_entry(port, &rio_mports, node) {
  401. if (!request_mem_region(port->iores.start,
  402. port->iores.end - port->iores.start,
  403. port->name)) {
  404. printk(KERN_ERR
  405. "RIO: Error requesting master port region %8.8lx-%8.8lx\n",
  406. port->iores.start, port->iores.end - 1);
  407. rc = -ENOMEM;
  408. goto out;
  409. }
  410. if (port->host_deviceid >= 0)
  411. rio_enum_mport(port);
  412. else
  413. rio_disc_mport(port);
  414. }
  415. out:
  416. return rc;
  417. }
  418. void rio_register_mport(struct rio_mport *port)
  419. {
  420. list_add_tail(&port->node, &rio_mports);
  421. }
  422. EXPORT_SYMBOL_GPL(rio_local_get_device_id);
  423. EXPORT_SYMBOL_GPL(rio_get_device);
  424. EXPORT_SYMBOL_GPL(rio_get_asm);
  425. EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
  426. EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
  427. EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
  428. EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
  429. EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
  430. EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
  431. EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
  432. EXPORT_SYMBOL_GPL(rio_release_outb_mbox);