rxe_srq.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_srq_chk_attr(struct rxe_dev *rxe, struct rxe_srq *srq,
  37. struct ib_srq_attr *attr, enum ib_srq_attr_mask mask)
  38. {
  39. if (srq && srq->error) {
  40. pr_warn("srq in error state\n");
  41. goto err1;
  42. }
  43. if (mask & IB_SRQ_MAX_WR) {
  44. if (attr->max_wr > rxe->attr.max_srq_wr) {
  45. pr_warn("max_wr(%d) > max_srq_wr(%d)\n",
  46. attr->max_wr, rxe->attr.max_srq_wr);
  47. goto err1;
  48. }
  49. if (attr->max_wr <= 0) {
  50. pr_warn("max_wr(%d) <= 0\n", attr->max_wr);
  51. goto err1;
  52. }
  53. if (srq && srq->limit && (attr->max_wr < srq->limit)) {
  54. pr_warn("max_wr (%d) < srq->limit (%d)\n",
  55. attr->max_wr, srq->limit);
  56. goto err1;
  57. }
  58. if (attr->max_wr < RXE_MIN_SRQ_WR)
  59. attr->max_wr = RXE_MIN_SRQ_WR;
  60. }
  61. if (mask & IB_SRQ_LIMIT) {
  62. if (attr->srq_limit > rxe->attr.max_srq_wr) {
  63. pr_warn("srq_limit(%d) > max_srq_wr(%d)\n",
  64. attr->srq_limit, rxe->attr.max_srq_wr);
  65. goto err1;
  66. }
  67. if (srq && (attr->srq_limit > srq->rq.queue->buf->index_mask)) {
  68. pr_warn("srq_limit (%d) > cur limit(%d)\n",
  69. attr->srq_limit,
  70. srq->rq.queue->buf->index_mask);
  71. goto err1;
  72. }
  73. }
  74. if (mask == IB_SRQ_INIT_MASK) {
  75. if (attr->max_sge > rxe->attr.max_srq_sge) {
  76. pr_warn("max_sge(%d) > max_srq_sge(%d)\n",
  77. attr->max_sge, rxe->attr.max_srq_sge);
  78. goto err1;
  79. }
  80. if (attr->max_sge < RXE_MIN_SRQ_SGE)
  81. attr->max_sge = RXE_MIN_SRQ_SGE;
  82. }
  83. return 0;
  84. err1:
  85. return -EINVAL;
  86. }
  87. int rxe_srq_from_init(struct rxe_dev *rxe, struct rxe_srq *srq,
  88. struct ib_srq_init_attr *init,
  89. struct ib_ucontext *context, struct ib_udata *udata)
  90. {
  91. int err;
  92. int srq_wqe_size;
  93. struct rxe_queue *q;
  94. srq->ibsrq.event_handler = init->event_handler;
  95. srq->ibsrq.srq_context = init->srq_context;
  96. srq->limit = init->attr.srq_limit;
  97. srq->srq_num = srq->pelem.index;
  98. srq->rq.max_wr = init->attr.max_wr;
  99. srq->rq.max_sge = init->attr.max_sge;
  100. srq_wqe_size = rcv_wqe_size(srq->rq.max_sge);
  101. spin_lock_init(&srq->rq.producer_lock);
  102. spin_lock_init(&srq->rq.consumer_lock);
  103. q = rxe_queue_init(rxe, &srq->rq.max_wr,
  104. srq_wqe_size);
  105. if (!q) {
  106. pr_warn("unable to allocate queue for srq\n");
  107. return -ENOMEM;
  108. }
  109. srq->rq.queue = q;
  110. err = do_mmap_info(rxe, udata, false, context, q->buf,
  111. q->buf_size, &q->ip);
  112. if (err)
  113. return err;
  114. if (udata && udata->outlen >= sizeof(struct mminfo) + sizeof(u32)) {
  115. if (copy_to_user(udata->outbuf + sizeof(struct mminfo),
  116. &srq->srq_num, sizeof(u32)))
  117. return -EFAULT;
  118. }
  119. return 0;
  120. }
  121. int rxe_srq_from_attr(struct rxe_dev *rxe, struct rxe_srq *srq,
  122. struct ib_srq_attr *attr, enum ib_srq_attr_mask mask,
  123. struct ib_udata *udata)
  124. {
  125. int err;
  126. struct rxe_queue *q = srq->rq.queue;
  127. struct mminfo mi = { .offset = 1, .size = 0};
  128. if (mask & IB_SRQ_MAX_WR) {
  129. /* Check that we can write the mminfo struct to user space */
  130. if (udata && udata->inlen >= sizeof(__u64)) {
  131. __u64 mi_addr;
  132. /* Get address of user space mminfo struct */
  133. err = ib_copy_from_udata(&mi_addr, udata,
  134. sizeof(mi_addr));
  135. if (err)
  136. goto err1;
  137. udata->outbuf = (void __user *)(unsigned long)mi_addr;
  138. udata->outlen = sizeof(mi);
  139. if (!access_ok(VERIFY_WRITE,
  140. (void __user *)udata->outbuf,
  141. udata->outlen)) {
  142. err = -EFAULT;
  143. goto err1;
  144. }
  145. }
  146. err = rxe_queue_resize(q, (unsigned int *)&attr->max_wr,
  147. rcv_wqe_size(srq->rq.max_sge),
  148. srq->rq.queue->ip ?
  149. srq->rq.queue->ip->context :
  150. NULL,
  151. udata, &srq->rq.producer_lock,
  152. &srq->rq.consumer_lock);
  153. if (err)
  154. goto err2;
  155. }
  156. if (mask & IB_SRQ_LIMIT)
  157. srq->limit = attr->srq_limit;
  158. return 0;
  159. err2:
  160. rxe_queue_cleanup(q);
  161. srq->rq.queue = NULL;
  162. err1:
  163. return err;
  164. }