mcast.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 <linux/slab.h>
  48. #include <linux/sched.h>
  49. #include <linux/rculist.h>
  50. #include <rdma/rdma_vt.h>
  51. #include <rdma/rdmavt_qp.h>
  52. #include "mcast.h"
  53. /**
  54. * rvt_driver_mcast - init resources for multicast
  55. * @rdi: rvt dev struct
  56. *
  57. * This is per device that registers with rdmavt
  58. */
  59. void rvt_driver_mcast_init(struct rvt_dev_info *rdi)
  60. {
  61. /*
  62. * Anything that needs setup for multicast on a per driver or per rdi
  63. * basis should be done in here.
  64. */
  65. spin_lock_init(&rdi->n_mcast_grps_lock);
  66. }
  67. /**
  68. * mcast_qp_alloc - alloc a struct to link a QP to mcast GID struct
  69. * @qp: the QP to link
  70. */
  71. static struct rvt_mcast_qp *rvt_mcast_qp_alloc(struct rvt_qp *qp)
  72. {
  73. struct rvt_mcast_qp *mqp;
  74. mqp = kmalloc(sizeof(*mqp), GFP_KERNEL);
  75. if (!mqp)
  76. goto bail;
  77. mqp->qp = qp;
  78. rvt_get_qp(qp);
  79. bail:
  80. return mqp;
  81. }
  82. static void rvt_mcast_qp_free(struct rvt_mcast_qp *mqp)
  83. {
  84. struct rvt_qp *qp = mqp->qp;
  85. /* Notify hfi1_destroy_qp() if it is waiting. */
  86. rvt_put_qp(qp);
  87. kfree(mqp);
  88. }
  89. /**
  90. * mcast_alloc - allocate the multicast GID structure
  91. * @mgid: the multicast GID
  92. *
  93. * A list of QPs will be attached to this structure.
  94. */
  95. static struct rvt_mcast *rvt_mcast_alloc(union ib_gid *mgid)
  96. {
  97. struct rvt_mcast *mcast;
  98. mcast = kzalloc(sizeof(*mcast), GFP_KERNEL);
  99. if (!mcast)
  100. goto bail;
  101. mcast->mgid = *mgid;
  102. INIT_LIST_HEAD(&mcast->qp_list);
  103. init_waitqueue_head(&mcast->wait);
  104. atomic_set(&mcast->refcount, 0);
  105. bail:
  106. return mcast;
  107. }
  108. static void rvt_mcast_free(struct rvt_mcast *mcast)
  109. {
  110. struct rvt_mcast_qp *p, *tmp;
  111. list_for_each_entry_safe(p, tmp, &mcast->qp_list, list)
  112. rvt_mcast_qp_free(p);
  113. kfree(mcast);
  114. }
  115. /**
  116. * rvt_mcast_find - search the global table for the given multicast GID
  117. * @ibp: the IB port structure
  118. * @mgid: the multicast GID to search for
  119. *
  120. * The caller is responsible for decrementing the reference count if found.
  121. *
  122. * Return: NULL if not found.
  123. */
  124. struct rvt_mcast *rvt_mcast_find(struct rvt_ibport *ibp, union ib_gid *mgid)
  125. {
  126. struct rb_node *n;
  127. unsigned long flags;
  128. struct rvt_mcast *found = NULL;
  129. spin_lock_irqsave(&ibp->lock, flags);
  130. n = ibp->mcast_tree.rb_node;
  131. while (n) {
  132. int ret;
  133. struct rvt_mcast *mcast;
  134. mcast = rb_entry(n, struct rvt_mcast, rb_node);
  135. ret = memcmp(mgid->raw, mcast->mgid.raw,
  136. sizeof(union ib_gid));
  137. if (ret < 0) {
  138. n = n->rb_left;
  139. } else if (ret > 0) {
  140. n = n->rb_right;
  141. } else {
  142. atomic_inc(&mcast->refcount);
  143. found = mcast;
  144. break;
  145. }
  146. }
  147. spin_unlock_irqrestore(&ibp->lock, flags);
  148. return found;
  149. }
  150. EXPORT_SYMBOL(rvt_mcast_find);
  151. /**
  152. * mcast_add - insert mcast GID into table and attach QP struct
  153. * @mcast: the mcast GID table
  154. * @mqp: the QP to attach
  155. *
  156. * Return: zero if both were added. Return EEXIST if the GID was already in
  157. * the table but the QP was added. Return ESRCH if the QP was already
  158. * attached and neither structure was added.
  159. */
  160. static int rvt_mcast_add(struct rvt_dev_info *rdi, struct rvt_ibport *ibp,
  161. struct rvt_mcast *mcast, struct rvt_mcast_qp *mqp)
  162. {
  163. struct rb_node **n = &ibp->mcast_tree.rb_node;
  164. struct rb_node *pn = NULL;
  165. int ret;
  166. spin_lock_irq(&ibp->lock);
  167. while (*n) {
  168. struct rvt_mcast *tmcast;
  169. struct rvt_mcast_qp *p;
  170. pn = *n;
  171. tmcast = rb_entry(pn, struct rvt_mcast, rb_node);
  172. ret = memcmp(mcast->mgid.raw, tmcast->mgid.raw,
  173. sizeof(union ib_gid));
  174. if (ret < 0) {
  175. n = &pn->rb_left;
  176. continue;
  177. }
  178. if (ret > 0) {
  179. n = &pn->rb_right;
  180. continue;
  181. }
  182. /* Search the QP list to see if this is already there. */
  183. list_for_each_entry_rcu(p, &tmcast->qp_list, list) {
  184. if (p->qp == mqp->qp) {
  185. ret = ESRCH;
  186. goto bail;
  187. }
  188. }
  189. if (tmcast->n_attached ==
  190. rdi->dparms.props.max_mcast_qp_attach) {
  191. ret = ENOMEM;
  192. goto bail;
  193. }
  194. tmcast->n_attached++;
  195. list_add_tail_rcu(&mqp->list, &tmcast->qp_list);
  196. ret = EEXIST;
  197. goto bail;
  198. }
  199. spin_lock(&rdi->n_mcast_grps_lock);
  200. if (rdi->n_mcast_grps_allocated == rdi->dparms.props.max_mcast_grp) {
  201. spin_unlock(&rdi->n_mcast_grps_lock);
  202. ret = ENOMEM;
  203. goto bail;
  204. }
  205. rdi->n_mcast_grps_allocated++;
  206. spin_unlock(&rdi->n_mcast_grps_lock);
  207. mcast->n_attached++;
  208. list_add_tail_rcu(&mqp->list, &mcast->qp_list);
  209. atomic_inc(&mcast->refcount);
  210. rb_link_node(&mcast->rb_node, pn, n);
  211. rb_insert_color(&mcast->rb_node, &ibp->mcast_tree);
  212. ret = 0;
  213. bail:
  214. spin_unlock_irq(&ibp->lock);
  215. return ret;
  216. }
  217. /**
  218. * rvt_attach_mcast - attach a qp to a multicast group
  219. * @ibqp: Infiniband qp
  220. * @igd: multicast guid
  221. * @lid: multicast lid
  222. *
  223. * Return: 0 on success
  224. */
  225. int rvt_attach_mcast(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  226. {
  227. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  228. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  229. struct rvt_ibport *ibp = rdi->ports[qp->port_num - 1];
  230. struct rvt_mcast *mcast;
  231. struct rvt_mcast_qp *mqp;
  232. int ret = -ENOMEM;
  233. if (ibqp->qp_num <= 1 || qp->state == IB_QPS_RESET)
  234. return -EINVAL;
  235. /*
  236. * Allocate data structures since its better to do this outside of
  237. * spin locks and it will most likely be needed.
  238. */
  239. mcast = rvt_mcast_alloc(gid);
  240. if (!mcast)
  241. return -ENOMEM;
  242. mqp = rvt_mcast_qp_alloc(qp);
  243. if (!mqp)
  244. goto bail_mcast;
  245. switch (rvt_mcast_add(rdi, ibp, mcast, mqp)) {
  246. case ESRCH:
  247. /* Neither was used: OK to attach the same QP twice. */
  248. ret = 0;
  249. goto bail_mqp;
  250. case EEXIST: /* The mcast wasn't used */
  251. ret = 0;
  252. goto bail_mcast;
  253. case ENOMEM:
  254. /* Exceeded the maximum number of mcast groups. */
  255. ret = -ENOMEM;
  256. goto bail_mqp;
  257. default:
  258. break;
  259. }
  260. return 0;
  261. bail_mqp:
  262. rvt_mcast_qp_free(mqp);
  263. bail_mcast:
  264. rvt_mcast_free(mcast);
  265. return ret;
  266. }
  267. /**
  268. * rvt_detach_mcast - remove a qp from a multicast group
  269. * @ibqp: Infiniband qp
  270. * @igd: multicast guid
  271. * @lid: multicast lid
  272. *
  273. * Return: 0 on success
  274. */
  275. int rvt_detach_mcast(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  276. {
  277. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  278. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  279. struct rvt_ibport *ibp = rdi->ports[qp->port_num - 1];
  280. struct rvt_mcast *mcast = NULL;
  281. struct rvt_mcast_qp *p, *tmp, *delp = NULL;
  282. struct rb_node *n;
  283. int last = 0;
  284. int ret = 0;
  285. if (ibqp->qp_num <= 1 || qp->state == IB_QPS_RESET)
  286. return -EINVAL;
  287. spin_lock_irq(&ibp->lock);
  288. /* Find the GID in the mcast table. */
  289. n = ibp->mcast_tree.rb_node;
  290. while (1) {
  291. if (!n) {
  292. spin_unlock_irq(&ibp->lock);
  293. return -EINVAL;
  294. }
  295. mcast = rb_entry(n, struct rvt_mcast, rb_node);
  296. ret = memcmp(gid->raw, mcast->mgid.raw,
  297. sizeof(union ib_gid));
  298. if (ret < 0)
  299. n = n->rb_left;
  300. else if (ret > 0)
  301. n = n->rb_right;
  302. else
  303. break;
  304. }
  305. /* Search the QP list. */
  306. list_for_each_entry_safe(p, tmp, &mcast->qp_list, list) {
  307. if (p->qp != qp)
  308. continue;
  309. /*
  310. * We found it, so remove it, but don't poison the forward
  311. * link until we are sure there are no list walkers.
  312. */
  313. list_del_rcu(&p->list);
  314. mcast->n_attached--;
  315. delp = p;
  316. /* If this was the last attached QP, remove the GID too. */
  317. if (list_empty(&mcast->qp_list)) {
  318. rb_erase(&mcast->rb_node, &ibp->mcast_tree);
  319. last = 1;
  320. }
  321. break;
  322. }
  323. spin_unlock_irq(&ibp->lock);
  324. /* QP not attached */
  325. if (!delp)
  326. return -EINVAL;
  327. /*
  328. * Wait for any list walkers to finish before freeing the
  329. * list element.
  330. */
  331. wait_event(mcast->wait, atomic_read(&mcast->refcount) <= 1);
  332. rvt_mcast_qp_free(delp);
  333. if (last) {
  334. atomic_dec(&mcast->refcount);
  335. wait_event(mcast->wait, !atomic_read(&mcast->refcount));
  336. rvt_mcast_free(mcast);
  337. spin_lock_irq(&rdi->n_mcast_grps_lock);
  338. rdi->n_mcast_grps_allocated--;
  339. spin_unlock_irq(&rdi->n_mcast_grps_lock);
  340. }
  341. return 0;
  342. }
  343. /**
  344. *rvt_mast_tree_empty - determine if any qps are attached to any mcast group
  345. *@rdi: rvt dev struct
  346. *
  347. * Return: in use count
  348. */
  349. int rvt_mcast_tree_empty(struct rvt_dev_info *rdi)
  350. {
  351. int i;
  352. int in_use = 0;
  353. for (i = 0; i < rdi->dparms.nports; i++)
  354. if (rdi->ports[i]->mcast_tree.rb_node)
  355. in_use++;
  356. return in_use;
  357. }