rio.c 14 KB

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