c2_mq.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "c2.h"
  34. #include "c2_mq.h"
  35. void *c2_mq_alloc(struct c2_mq *q)
  36. {
  37. BUG_ON(q->magic != C2_MQ_MAGIC);
  38. BUG_ON(q->type != C2_MQ_ADAPTER_TARGET);
  39. if (c2_mq_full(q)) {
  40. return NULL;
  41. } else {
  42. #ifdef DEBUG
  43. struct c2wr_hdr *m =
  44. (struct c2wr_hdr *) (q->msg_pool.host + q->priv * q->msg_size);
  45. #ifdef CCMSGMAGIC
  46. BUG_ON(m->magic != be32_to_cpu(~CCWR_MAGIC));
  47. m->magic = cpu_to_be32(CCWR_MAGIC);
  48. #endif
  49. return m;
  50. #else
  51. return q->msg_pool.host + q->priv * q->msg_size;
  52. #endif
  53. }
  54. }
  55. void c2_mq_produce(struct c2_mq *q)
  56. {
  57. BUG_ON(q->magic != C2_MQ_MAGIC);
  58. BUG_ON(q->type != C2_MQ_ADAPTER_TARGET);
  59. if (!c2_mq_full(q)) {
  60. q->priv = (q->priv + 1) % q->q_size;
  61. q->hint_count++;
  62. /* Update peer's offset. */
  63. __raw_writew((__force u16) cpu_to_be16(q->priv), &q->peer->shared);
  64. }
  65. }
  66. void *c2_mq_consume(struct c2_mq *q)
  67. {
  68. BUG_ON(q->magic != C2_MQ_MAGIC);
  69. BUG_ON(q->type != C2_MQ_HOST_TARGET);
  70. if (c2_mq_empty(q)) {
  71. return NULL;
  72. } else {
  73. #ifdef DEBUG
  74. struct c2wr_hdr *m = (struct c2wr_hdr *)
  75. (q->msg_pool.host + q->priv * q->msg_size);
  76. #ifdef CCMSGMAGIC
  77. BUG_ON(m->magic != be32_to_cpu(CCWR_MAGIC));
  78. #endif
  79. return m;
  80. #else
  81. return q->msg_pool.host + q->priv * q->msg_size;
  82. #endif
  83. }
  84. }
  85. void c2_mq_free(struct c2_mq *q)
  86. {
  87. BUG_ON(q->magic != C2_MQ_MAGIC);
  88. BUG_ON(q->type != C2_MQ_HOST_TARGET);
  89. if (!c2_mq_empty(q)) {
  90. #ifdef CCMSGMAGIC
  91. {
  92. struct c2wr_hdr __iomem *m = (struct c2wr_hdr __iomem *)
  93. (q->msg_pool.adapter + q->priv * q->msg_size);
  94. __raw_writel(cpu_to_be32(~CCWR_MAGIC), &m->magic);
  95. }
  96. #endif
  97. q->priv = (q->priv + 1) % q->q_size;
  98. /* Update peer's offset. */
  99. __raw_writew((__force u16) cpu_to_be16(q->priv), &q->peer->shared);
  100. }
  101. }
  102. void c2_mq_lconsume(struct c2_mq *q, u32 wqe_count)
  103. {
  104. BUG_ON(q->magic != C2_MQ_MAGIC);
  105. BUG_ON(q->type != C2_MQ_ADAPTER_TARGET);
  106. while (wqe_count--) {
  107. BUG_ON(c2_mq_empty(q));
  108. *q->shared = cpu_to_be16((be16_to_cpu(*q->shared)+1) % q->q_size);
  109. }
  110. }
  111. #if 0
  112. u32 c2_mq_count(struct c2_mq *q)
  113. {
  114. s32 count;
  115. if (q->type == C2_MQ_HOST_TARGET)
  116. count = be16_to_cpu(*q->shared) - q->priv;
  117. else
  118. count = q->priv - be16_to_cpu(*q->shared);
  119. if (count < 0)
  120. count += q->q_size;
  121. return (u32) count;
  122. }
  123. #endif /* 0 */
  124. void c2_mq_req_init(struct c2_mq *q, u32 index, u32 q_size, u32 msg_size,
  125. u8 __iomem *pool_start, u16 __iomem *peer, u32 type)
  126. {
  127. BUG_ON(!q->shared);
  128. /* This code assumes the byte swapping has already been done! */
  129. q->index = index;
  130. q->q_size = q_size;
  131. q->msg_size = msg_size;
  132. q->msg_pool.adapter = pool_start;
  133. q->peer = (struct c2_mq_shared __iomem *) peer;
  134. q->magic = C2_MQ_MAGIC;
  135. q->type = type;
  136. q->priv = 0;
  137. q->hint_count = 0;
  138. return;
  139. }
  140. void c2_mq_rep_init(struct c2_mq *q, u32 index, u32 q_size, u32 msg_size,
  141. u8 *pool_start, u16 __iomem *peer, u32 type)
  142. {
  143. BUG_ON(!q->shared);
  144. /* This code assumes the byte swapping has already been done! */
  145. q->index = index;
  146. q->q_size = q_size;
  147. q->msg_size = msg_size;
  148. q->msg_pool.host = pool_start;
  149. q->peer = (struct c2_mq_shared __iomem *) peer;
  150. q->magic = C2_MQ_MAGIC;
  151. q->type = type;
  152. q->priv = 0;
  153. q->hint_count = 0;
  154. return;
  155. }