rc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright(c) 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <rdma/rdma_vt.h>
  48. #include <rdma/ib_hdrs.h>
  49. /*
  50. * Convert the AETH credit code into the number of credits.
  51. */
  52. static const u16 credit_table[31] = {
  53. 0, /* 0 */
  54. 1, /* 1 */
  55. 2, /* 2 */
  56. 3, /* 3 */
  57. 4, /* 4 */
  58. 6, /* 5 */
  59. 8, /* 6 */
  60. 12, /* 7 */
  61. 16, /* 8 */
  62. 24, /* 9 */
  63. 32, /* A */
  64. 48, /* B */
  65. 64, /* C */
  66. 96, /* D */
  67. 128, /* E */
  68. 192, /* F */
  69. 256, /* 10 */
  70. 384, /* 11 */
  71. 512, /* 12 */
  72. 768, /* 13 */
  73. 1024, /* 14 */
  74. 1536, /* 15 */
  75. 2048, /* 16 */
  76. 3072, /* 17 */
  77. 4096, /* 18 */
  78. 6144, /* 19 */
  79. 8192, /* 1A */
  80. 12288, /* 1B */
  81. 16384, /* 1C */
  82. 24576, /* 1D */
  83. 32768 /* 1E */
  84. };
  85. /**
  86. * rvt_compute_aeth - compute the AETH (syndrome + MSN)
  87. * @qp: the queue pair to compute the AETH for
  88. *
  89. * Returns the AETH.
  90. */
  91. __be32 rvt_compute_aeth(struct rvt_qp *qp)
  92. {
  93. u32 aeth = qp->r_msn & IB_MSN_MASK;
  94. if (qp->ibqp.srq) {
  95. /*
  96. * Shared receive queues don't generate credits.
  97. * Set the credit field to the invalid value.
  98. */
  99. aeth |= IB_AETH_CREDIT_INVAL << IB_AETH_CREDIT_SHIFT;
  100. } else {
  101. u32 min, max, x;
  102. u32 credits;
  103. struct rvt_rwq *wq = qp->r_rq.wq;
  104. u32 head;
  105. u32 tail;
  106. /* sanity check pointers before trusting them */
  107. head = wq->head;
  108. if (head >= qp->r_rq.size)
  109. head = 0;
  110. tail = wq->tail;
  111. if (tail >= qp->r_rq.size)
  112. tail = 0;
  113. /*
  114. * Compute the number of credits available (RWQEs).
  115. * There is a small chance that the pair of reads are
  116. * not atomic, which is OK, since the fuzziness is
  117. * resolved as further ACKs go out.
  118. */
  119. credits = head - tail;
  120. if ((int)credits < 0)
  121. credits += qp->r_rq.size;
  122. /*
  123. * Binary search the credit table to find the code to
  124. * use.
  125. */
  126. min = 0;
  127. max = 31;
  128. for (;;) {
  129. x = (min + max) / 2;
  130. if (credit_table[x] == credits)
  131. break;
  132. if (credit_table[x] > credits) {
  133. max = x;
  134. } else {
  135. if (min == x)
  136. break;
  137. min = x;
  138. }
  139. }
  140. aeth |= x << IB_AETH_CREDIT_SHIFT;
  141. }
  142. return cpu_to_be32(aeth);
  143. }
  144. EXPORT_SYMBOL(rvt_compute_aeth);
  145. /**
  146. * rvt_get_credit - flush the send work queue of a QP
  147. * @qp: the qp who's send work queue to flush
  148. * @aeth: the Acknowledge Extended Transport Header
  149. *
  150. * The QP s_lock should be held.
  151. */
  152. void rvt_get_credit(struct rvt_qp *qp, u32 aeth)
  153. {
  154. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  155. u32 credit = (aeth >> IB_AETH_CREDIT_SHIFT) & IB_AETH_CREDIT_MASK;
  156. lockdep_assert_held(&qp->s_lock);
  157. /*
  158. * If the credit is invalid, we can send
  159. * as many packets as we like. Otherwise, we have to
  160. * honor the credit field.
  161. */
  162. if (credit == IB_AETH_CREDIT_INVAL) {
  163. if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
  164. qp->s_flags |= RVT_S_UNLIMITED_CREDIT;
  165. if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
  166. qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
  167. rdi->driver_f.schedule_send(qp);
  168. }
  169. }
  170. } else if (!(qp->s_flags & RVT_S_UNLIMITED_CREDIT)) {
  171. /* Compute new LSN (i.e., MSN + credit) */
  172. credit = (aeth + credit_table[credit]) & IB_MSN_MASK;
  173. if (rvt_cmp_msn(credit, qp->s_lsn) > 0) {
  174. qp->s_lsn = credit;
  175. if (qp->s_flags & RVT_S_WAIT_SSN_CREDIT) {
  176. qp->s_flags &= ~RVT_S_WAIT_SSN_CREDIT;
  177. rdi->driver_f.schedule_send(qp);
  178. }
  179. }
  180. }
  181. }
  182. EXPORT_SYMBOL(rvt_get_credit);