mcast.c 9.9 KB

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