c2_vq.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2005 Ammasso, Inc. All rights reserved.
  3. * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include "c2_vq.h"
  36. #include "c2_provider.h"
  37. /*
  38. * Verbs Request Objects:
  39. *
  40. * VQ Request Objects are allocated by the kernel verbs handlers.
  41. * They contain a wait object, a refcnt, an atomic bool indicating that the
  42. * adapter has replied, and a copy of the verb reply work request.
  43. * A pointer to the VQ Request Object is passed down in the context
  44. * field of the work request message, and reflected back by the adapter
  45. * in the verbs reply message. The function handle_vq() in the interrupt
  46. * path will use this pointer to:
  47. * 1) append a copy of the verbs reply message
  48. * 2) mark that the reply is ready
  49. * 3) wake up the kernel verbs handler blocked awaiting the reply.
  50. *
  51. *
  52. * The kernel verbs handlers do a "get" to put a 2nd reference on the
  53. * VQ Request object. If the kernel verbs handler exits before the adapter
  54. * can respond, this extra reference will keep the VQ Request object around
  55. * until the adapter's reply can be processed. The reason we need this is
  56. * because a pointer to this object is stuffed into the context field of
  57. * the verbs work request message, and reflected back in the reply message.
  58. * It is used in the interrupt handler (handle_vq()) to wake up the appropriate
  59. * kernel verb handler that is blocked awaiting the verb reply.
  60. * So handle_vq() will do a "put" on the object when it's done accessing it.
  61. * NOTE: If we guarantee that the kernel verb handler will never bail before
  62. * getting the reply, then we don't need these refcnts.
  63. *
  64. *
  65. * VQ Request objects are freed by the kernel verbs handlers only
  66. * after the verb has been processed, or when the adapter fails and
  67. * does not reply.
  68. *
  69. *
  70. * Verbs Reply Buffers:
  71. *
  72. * VQ Reply bufs are local host memory copies of a
  73. * outstanding Verb Request reply
  74. * message. The are always allocated by the kernel verbs handlers, and _may_ be
  75. * freed by either the kernel verbs handler -or- the interrupt handler. The
  76. * kernel verbs handler _must_ free the repbuf, then free the vq request object
  77. * in that order.
  78. */
  79. int vq_init(struct c2_dev *c2dev)
  80. {
  81. sprintf(c2dev->vq_cache_name, "c2-vq:dev%c",
  82. (char) ('0' + c2dev->devnum));
  83. c2dev->host_msg_cache =
  84. kmem_cache_create(c2dev->vq_cache_name, c2dev->rep_vq.msg_size, 0,
  85. SLAB_HWCACHE_ALIGN, NULL);
  86. if (c2dev->host_msg_cache == NULL) {
  87. return -ENOMEM;
  88. }
  89. return 0;
  90. }
  91. void vq_term(struct c2_dev *c2dev)
  92. {
  93. kmem_cache_destroy(c2dev->host_msg_cache);
  94. }
  95. /* vq_req_alloc - allocate a VQ Request Object and initialize it.
  96. * The refcnt is set to 1.
  97. */
  98. struct c2_vq_req *vq_req_alloc(struct c2_dev *c2dev)
  99. {
  100. struct c2_vq_req *r;
  101. r = kmalloc(sizeof(struct c2_vq_req), GFP_KERNEL);
  102. if (r) {
  103. init_waitqueue_head(&r->wait_object);
  104. r->reply_msg = 0;
  105. r->event = 0;
  106. r->cm_id = NULL;
  107. r->qp = NULL;
  108. atomic_set(&r->refcnt, 1);
  109. atomic_set(&r->reply_ready, 0);
  110. }
  111. return r;
  112. }
  113. /* vq_req_free - free the VQ Request Object. It is assumed the verbs handler
  114. * has already free the VQ Reply Buffer if it existed.
  115. */
  116. void vq_req_free(struct c2_dev *c2dev, struct c2_vq_req *r)
  117. {
  118. r->reply_msg = 0;
  119. if (atomic_dec_and_test(&r->refcnt)) {
  120. kfree(r);
  121. }
  122. }
  123. /* vq_req_get - reference a VQ Request Object. Done
  124. * only in the kernel verbs handlers.
  125. */
  126. void vq_req_get(struct c2_dev *c2dev, struct c2_vq_req *r)
  127. {
  128. atomic_inc(&r->refcnt);
  129. }
  130. /* vq_req_put - dereference and potentially free a VQ Request Object.
  131. *
  132. * This is only called by handle_vq() on the
  133. * interrupt when it is done processing
  134. * a verb reply message. If the associated
  135. * kernel verbs handler has already bailed,
  136. * then this put will actually free the VQ
  137. * Request object _and_ the VQ Reply Buffer
  138. * if it exists.
  139. */
  140. void vq_req_put(struct c2_dev *c2dev, struct c2_vq_req *r)
  141. {
  142. if (atomic_dec_and_test(&r->refcnt)) {
  143. if (r->reply_msg != 0)
  144. vq_repbuf_free(c2dev,
  145. (void *) (unsigned long) r->reply_msg);
  146. kfree(r);
  147. }
  148. }
  149. /*
  150. * vq_repbuf_alloc - allocate a VQ Reply Buffer.
  151. */
  152. void *vq_repbuf_alloc(struct c2_dev *c2dev)
  153. {
  154. return kmem_cache_alloc(c2dev->host_msg_cache, GFP_ATOMIC);
  155. }
  156. /*
  157. * vq_send_wr - post a verbs request message to the Verbs Request Queue.
  158. * If a message is not available in the MQ, then block until one is available.
  159. * NOTE: handle_mq() on the interrupt context will wake up threads blocked here.
  160. * When the adapter drains the Verbs Request Queue,
  161. * it inserts MQ index 0 in to the
  162. * adapter->host activity fifo and interrupts the host.
  163. */
  164. int vq_send_wr(struct c2_dev *c2dev, union c2wr *wr)
  165. {
  166. void *msg;
  167. wait_queue_t __wait;
  168. /*
  169. * grab adapter vq lock
  170. */
  171. spin_lock(&c2dev->vqlock);
  172. /*
  173. * allocate msg
  174. */
  175. msg = c2_mq_alloc(&c2dev->req_vq);
  176. /*
  177. * If we cannot get a msg, then we'll wait
  178. * When a messages are available, the int handler will wake_up()
  179. * any waiters.
  180. */
  181. while (msg == NULL) {
  182. pr_debug("%s:%d no available msg in VQ, waiting...\n",
  183. __func__, __LINE__);
  184. init_waitqueue_entry(&__wait, current);
  185. add_wait_queue(&c2dev->req_vq_wo, &__wait);
  186. spin_unlock(&c2dev->vqlock);
  187. for (;;) {
  188. set_current_state(TASK_INTERRUPTIBLE);
  189. if (!c2_mq_full(&c2dev->req_vq)) {
  190. break;
  191. }
  192. if (!signal_pending(current)) {
  193. schedule_timeout(1 * HZ); /* 1 second... */
  194. continue;
  195. }
  196. set_current_state(TASK_RUNNING);
  197. remove_wait_queue(&c2dev->req_vq_wo, &__wait);
  198. return -EINTR;
  199. }
  200. set_current_state(TASK_RUNNING);
  201. remove_wait_queue(&c2dev->req_vq_wo, &__wait);
  202. spin_lock(&c2dev->vqlock);
  203. msg = c2_mq_alloc(&c2dev->req_vq);
  204. }
  205. /*
  206. * copy wr into adapter msg
  207. */
  208. memcpy(msg, wr, c2dev->req_vq.msg_size);
  209. /*
  210. * post msg
  211. */
  212. c2_mq_produce(&c2dev->req_vq);
  213. /*
  214. * release adapter vq lock
  215. */
  216. spin_unlock(&c2dev->vqlock);
  217. return 0;
  218. }
  219. /*
  220. * vq_wait_for_reply - block until the adapter posts a Verb Reply Message.
  221. */
  222. int vq_wait_for_reply(struct c2_dev *c2dev, struct c2_vq_req *req)
  223. {
  224. if (!wait_event_timeout(req->wait_object,
  225. atomic_read(&req->reply_ready),
  226. 60*HZ))
  227. return -ETIMEDOUT;
  228. return 0;
  229. }
  230. /*
  231. * vq_repbuf_free - Free a Verbs Reply Buffer.
  232. */
  233. void vq_repbuf_free(struct c2_dev *c2dev, void *reply)
  234. {
  235. kmem_cache_free(c2dev->host_msg_cache, reply);
  236. }