rxe_cq.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, 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 "rxe.h"
  34. #include "rxe_loc.h"
  35. #include "rxe_queue.h"
  36. int rxe_cq_chk_attr(struct rxe_dev *rxe, struct rxe_cq *cq,
  37. int cqe, int comp_vector)
  38. {
  39. int count;
  40. if (cqe <= 0) {
  41. pr_warn("cqe(%d) <= 0\n", cqe);
  42. goto err1;
  43. }
  44. if (cqe > rxe->attr.max_cqe) {
  45. pr_warn("cqe(%d) > max_cqe(%d)\n",
  46. cqe, rxe->attr.max_cqe);
  47. goto err1;
  48. }
  49. if (cq) {
  50. count = queue_count(cq->queue);
  51. if (cqe < count) {
  52. pr_warn("cqe(%d) < current # elements in queue (%d)",
  53. cqe, count);
  54. goto err1;
  55. }
  56. }
  57. return 0;
  58. err1:
  59. return -EINVAL;
  60. }
  61. static void rxe_send_complete(unsigned long data)
  62. {
  63. struct rxe_cq *cq = (struct rxe_cq *)data;
  64. unsigned long flags;
  65. spin_lock_irqsave(&cq->cq_lock, flags);
  66. if (cq->is_dying) {
  67. spin_unlock_irqrestore(&cq->cq_lock, flags);
  68. return;
  69. }
  70. spin_unlock_irqrestore(&cq->cq_lock, flags);
  71. cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
  72. }
  73. int rxe_cq_from_init(struct rxe_dev *rxe, struct rxe_cq *cq, int cqe,
  74. int comp_vector, struct ib_ucontext *context,
  75. struct rxe_create_cq_resp __user *uresp)
  76. {
  77. int err;
  78. cq->queue = rxe_queue_init(rxe, &cqe,
  79. sizeof(struct rxe_cqe));
  80. if (!cq->queue) {
  81. pr_warn("unable to create cq\n");
  82. return -ENOMEM;
  83. }
  84. err = do_mmap_info(rxe, uresp ? &uresp->mi : NULL, context,
  85. cq->queue->buf, cq->queue->buf_size, &cq->queue->ip);
  86. if (err) {
  87. kvfree(cq->queue->buf);
  88. kfree(cq->queue);
  89. return err;
  90. }
  91. if (uresp)
  92. cq->is_user = 1;
  93. cq->is_dying = false;
  94. tasklet_init(&cq->comp_task, rxe_send_complete, (unsigned long)cq);
  95. spin_lock_init(&cq->cq_lock);
  96. cq->ibcq.cqe = cqe;
  97. return 0;
  98. }
  99. int rxe_cq_resize_queue(struct rxe_cq *cq, int cqe,
  100. struct rxe_resize_cq_resp __user *uresp)
  101. {
  102. int err;
  103. err = rxe_queue_resize(cq->queue, (unsigned int *)&cqe,
  104. sizeof(struct rxe_cqe),
  105. cq->queue->ip ? cq->queue->ip->context : NULL,
  106. uresp ? &uresp->mi : NULL, NULL, &cq->cq_lock);
  107. if (!err)
  108. cq->ibcq.cqe = cqe;
  109. return err;
  110. }
  111. int rxe_cq_post(struct rxe_cq *cq, struct rxe_cqe *cqe, int solicited)
  112. {
  113. struct ib_event ev;
  114. unsigned long flags;
  115. spin_lock_irqsave(&cq->cq_lock, flags);
  116. if (unlikely(queue_full(cq->queue))) {
  117. spin_unlock_irqrestore(&cq->cq_lock, flags);
  118. if (cq->ibcq.event_handler) {
  119. ev.device = cq->ibcq.device;
  120. ev.element.cq = &cq->ibcq;
  121. ev.event = IB_EVENT_CQ_ERR;
  122. cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
  123. }
  124. return -EBUSY;
  125. }
  126. memcpy(producer_addr(cq->queue), cqe, sizeof(*cqe));
  127. /* make sure all changes to the CQ are written before we update the
  128. * producer pointer
  129. */
  130. smp_wmb();
  131. advance_producer(cq->queue);
  132. spin_unlock_irqrestore(&cq->cq_lock, flags);
  133. if ((cq->notify == IB_CQ_NEXT_COMP) ||
  134. (cq->notify == IB_CQ_SOLICITED && solicited)) {
  135. cq->notify = 0;
  136. tasklet_schedule(&cq->comp_task);
  137. }
  138. return 0;
  139. }
  140. void rxe_cq_disable(struct rxe_cq *cq)
  141. {
  142. unsigned long flags;
  143. spin_lock_irqsave(&cq->cq_lock, flags);
  144. cq->is_dying = true;
  145. spin_unlock_irqrestore(&cq->cq_lock, flags);
  146. }
  147. void rxe_cq_cleanup(struct rxe_pool_entry *arg)
  148. {
  149. struct rxe_cq *cq = container_of(arg, typeof(*cq), pelem);
  150. if (cq->queue)
  151. rxe_queue_cleanup(cq->queue);
  152. }