rio.c 14 KB

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