rxe_mcast.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. int rxe_mcast_get_grp(struct rxe_dev *rxe, union ib_gid *mgid,
  36. struct rxe_mc_grp **grp_p)
  37. {
  38. int err;
  39. struct rxe_mc_grp *grp;
  40. if (rxe->attr.max_mcast_qp_attach == 0) {
  41. err = -EINVAL;
  42. goto err1;
  43. }
  44. grp = rxe_pool_get_key(&rxe->mc_grp_pool, mgid);
  45. if (grp)
  46. goto done;
  47. grp = rxe_alloc(&rxe->mc_grp_pool);
  48. if (!grp) {
  49. err = -ENOMEM;
  50. goto err1;
  51. }
  52. INIT_LIST_HEAD(&grp->qp_list);
  53. spin_lock_init(&grp->mcg_lock);
  54. grp->rxe = rxe;
  55. rxe_add_key(grp, mgid);
  56. err = rxe->ifc_ops->mcast_add(rxe, mgid);
  57. if (err)
  58. goto err2;
  59. done:
  60. *grp_p = grp;
  61. return 0;
  62. err2:
  63. rxe_drop_ref(grp);
  64. err1:
  65. return err;
  66. }
  67. int rxe_mcast_add_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp,
  68. struct rxe_mc_grp *grp)
  69. {
  70. int err;
  71. struct rxe_mc_elem *elem;
  72. /* check to see of the qp is already a member of the group */
  73. spin_lock_bh(&qp->grp_lock);
  74. spin_lock_bh(&grp->mcg_lock);
  75. list_for_each_entry(elem, &grp->qp_list, qp_list) {
  76. if (elem->qp == qp) {
  77. err = 0;
  78. goto out;
  79. }
  80. }
  81. if (grp->num_qp >= rxe->attr.max_mcast_qp_attach) {
  82. err = -ENOMEM;
  83. goto out;
  84. }
  85. elem = rxe_alloc(&rxe->mc_elem_pool);
  86. if (!elem) {
  87. err = -ENOMEM;
  88. goto out;
  89. }
  90. /* each qp holds a ref on the grp */
  91. rxe_add_ref(grp);
  92. grp->num_qp++;
  93. elem->qp = qp;
  94. elem->grp = grp;
  95. list_add(&elem->qp_list, &grp->qp_list);
  96. list_add(&elem->grp_list, &qp->grp_list);
  97. err = 0;
  98. out:
  99. spin_unlock_bh(&grp->mcg_lock);
  100. spin_unlock_bh(&qp->grp_lock);
  101. return err;
  102. }
  103. int rxe_mcast_drop_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp,
  104. union ib_gid *mgid)
  105. {
  106. struct rxe_mc_grp *grp;
  107. struct rxe_mc_elem *elem, *tmp;
  108. grp = rxe_pool_get_key(&rxe->mc_grp_pool, mgid);
  109. if (!grp)
  110. goto err1;
  111. spin_lock_bh(&qp->grp_lock);
  112. spin_lock_bh(&grp->mcg_lock);
  113. list_for_each_entry_safe(elem, tmp, &grp->qp_list, qp_list) {
  114. if (elem->qp == qp) {
  115. list_del(&elem->qp_list);
  116. list_del(&elem->grp_list);
  117. grp->num_qp--;
  118. spin_unlock_bh(&grp->mcg_lock);
  119. spin_unlock_bh(&qp->grp_lock);
  120. rxe_drop_ref(elem);
  121. rxe_drop_ref(grp); /* ref held by QP */
  122. rxe_drop_ref(grp); /* ref from get_key */
  123. return 0;
  124. }
  125. }
  126. spin_unlock_bh(&grp->mcg_lock);
  127. spin_unlock_bh(&qp->grp_lock);
  128. rxe_drop_ref(grp); /* ref from get_key */
  129. err1:
  130. return -EINVAL;
  131. }
  132. void rxe_drop_all_mcast_groups(struct rxe_qp *qp)
  133. {
  134. struct rxe_mc_grp *grp;
  135. struct rxe_mc_elem *elem;
  136. while (1) {
  137. spin_lock_bh(&qp->grp_lock);
  138. if (list_empty(&qp->grp_list)) {
  139. spin_unlock_bh(&qp->grp_lock);
  140. break;
  141. }
  142. elem = list_first_entry(&qp->grp_list, struct rxe_mc_elem,
  143. grp_list);
  144. list_del(&elem->grp_list);
  145. spin_unlock_bh(&qp->grp_lock);
  146. grp = elem->grp;
  147. spin_lock_bh(&grp->mcg_lock);
  148. list_del(&elem->qp_list);
  149. grp->num_qp--;
  150. spin_unlock_bh(&grp->mcg_lock);
  151. rxe_drop_ref(grp);
  152. rxe_drop_ref(elem);
  153. }
  154. }
  155. void rxe_mc_cleanup(void *arg)
  156. {
  157. struct rxe_mc_grp *grp = arg;
  158. struct rxe_dev *rxe = grp->rxe;
  159. rxe_drop_key(grp);
  160. rxe->ifc_ops->mcast_delete(rxe, &grp->mgid);
  161. }