1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
3 * Copyright(c) 2016 Intel Corporation.
6 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/rculist.h>
9 #include <rdma/rdma_vt.h>
10 #include <rdma/rdmavt_qp.h>
15 * rvt_driver_mcast_init - init resources for multicast
16 * @rdi: rvt dev struct
18 * This is per device that registers with rdmavt
20 void rvt_driver_mcast_init(struct rvt_dev_info *rdi)
23 * Anything that needs setup for multicast on a per driver or per rdi
24 * basis should be done in here.
26 spin_lock_init(&rdi->n_mcast_grps_lock);
30 * rvt_mcast_qp_alloc - alloc a struct to link a QP to mcast GID struct
33 static struct rvt_mcast_qp *rvt_mcast_qp_alloc(struct rvt_qp *qp)
35 struct rvt_mcast_qp *mqp;
37 mqp = kmalloc(sizeof(*mqp), GFP_KERNEL);
48 static void rvt_mcast_qp_free(struct rvt_mcast_qp *mqp)
50 struct rvt_qp *qp = mqp->qp;
52 /* Notify hfi1_destroy_qp() if it is waiting. */
59 * rvt_mcast_alloc - allocate the multicast GID structure
60 * @mgid: the multicast GID
61 * @lid: the muilticast LID (host order)
63 * A list of QPs will be attached to this structure.
65 static struct rvt_mcast *rvt_mcast_alloc(union ib_gid *mgid, u16 lid)
67 struct rvt_mcast *mcast;
69 mcast = kzalloc(sizeof(*mcast), GFP_KERNEL);
73 mcast->mcast_addr.mgid = *mgid;
74 mcast->mcast_addr.lid = lid;
76 INIT_LIST_HEAD(&mcast->qp_list);
77 init_waitqueue_head(&mcast->wait);
78 atomic_set(&mcast->refcount, 0);
84 static void rvt_mcast_free(struct rvt_mcast *mcast)
86 struct rvt_mcast_qp *p, *tmp;
88 list_for_each_entry_safe(p, tmp, &mcast->qp_list, list)
95 * rvt_mcast_find - search the global table for the given multicast GID/LID
96 * NOTE: It is valid to have 1 MLID with multiple MGIDs. It is not valid
97 * to have 1 MGID with multiple MLIDs.
98 * @ibp: the IB port structure
99 * @mgid: the multicast GID to search for
100 * @lid: the multicast LID portion of the multicast address (host order)
102 * The caller is responsible for decrementing the reference count if found.
104 * Return: NULL if not found.
106 struct rvt_mcast *rvt_mcast_find(struct rvt_ibport *ibp, union ib_gid *mgid,
111 struct rvt_mcast *found = NULL;
113 spin_lock_irqsave(&ibp->lock, flags);
114 n = ibp->mcast_tree.rb_node;
117 struct rvt_mcast *mcast;
119 mcast = rb_entry(n, struct rvt_mcast, rb_node);
121 ret = memcmp(mgid->raw, mcast->mcast_addr.mgid.raw,
125 } else if (ret > 0) {
128 /* MGID/MLID must match */
129 if (mcast->mcast_addr.lid == lid) {
130 atomic_inc(&mcast->refcount);
136 spin_unlock_irqrestore(&ibp->lock, flags);
139 EXPORT_SYMBOL(rvt_mcast_find);
142 * rvt_mcast_add - insert mcast GID into table and attach QP struct
143 * @mcast: the mcast GID table
144 * @mqp: the QP to attach
146 * Return: zero if both were added. Return EEXIST if the GID was already in
147 * the table but the QP was added. Return ESRCH if the QP was already
148 * attached and neither structure was added. Return EINVAL if the MGID was
149 * found, but the MLID did NOT match.
151 static int rvt_mcast_add(struct rvt_dev_info *rdi, struct rvt_ibport *ibp,
152 struct rvt_mcast *mcast, struct rvt_mcast_qp *mqp)
154 struct rb_node **n = &ibp->mcast_tree.rb_node;
155 struct rb_node *pn = NULL;
158 spin_lock_irq(&ibp->lock);
161 struct rvt_mcast *tmcast;
162 struct rvt_mcast_qp *p;
165 tmcast = rb_entry(pn, struct rvt_mcast, rb_node);
167 ret = memcmp(mcast->mcast_addr.mgid.raw,
168 tmcast->mcast_addr.mgid.raw,
169 sizeof(mcast->mcast_addr.mgid));
179 if (tmcast->mcast_addr.lid != mcast->mcast_addr.lid) {
184 /* Search the QP list to see if this is already there. */
185 list_for_each_entry_rcu(p, &tmcast->qp_list, list) {
186 if (p->qp == mqp->qp) {
191 if (tmcast->n_attached ==
192 rdi->dparms.props.max_mcast_qp_attach) {
197 tmcast->n_attached++;
199 list_add_tail_rcu(&mqp->list, &tmcast->qp_list);
204 spin_lock(&rdi->n_mcast_grps_lock);
205 if (rdi->n_mcast_grps_allocated == rdi->dparms.props.max_mcast_grp) {
206 spin_unlock(&rdi->n_mcast_grps_lock);
211 rdi->n_mcast_grps_allocated++;
212 spin_unlock(&rdi->n_mcast_grps_lock);
216 list_add_tail_rcu(&mqp->list, &mcast->qp_list);
218 atomic_inc(&mcast->refcount);
219 rb_link_node(&mcast->rb_node, pn, n);
220 rb_insert_color(&mcast->rb_node, &ibp->mcast_tree);
225 spin_unlock_irq(&ibp->lock);
231 * rvt_attach_mcast - attach a qp to a multicast group
232 * @ibqp: Infiniband qp
233 * @gid: multicast guid
234 * @lid: multicast lid
236 * Return: 0 on success
238 int rvt_attach_mcast(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
240 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
241 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
242 struct rvt_ibport *ibp = rdi->ports[qp->port_num - 1];
243 struct rvt_mcast *mcast;
244 struct rvt_mcast_qp *mqp;
247 if (ibqp->qp_num <= 1 || qp->state == IB_QPS_RESET)
251 * Allocate data structures since its better to do this outside of
252 * spin locks and it will most likely be needed.
254 mcast = rvt_mcast_alloc(gid, lid);
258 mqp = rvt_mcast_qp_alloc(qp);
262 switch (rvt_mcast_add(rdi, ibp, mcast, mqp)) {
264 /* Neither was used: OK to attach the same QP twice. */
267 case EEXIST: /* The mcast wasn't used */
271 /* Exceeded the maximum number of mcast groups. */
275 /* Invalid MGID/MLID pair */
285 rvt_mcast_qp_free(mqp);
288 rvt_mcast_free(mcast);
294 * rvt_detach_mcast - remove a qp from a multicast group
295 * @ibqp: Infiniband qp
296 * @gid: multicast guid
297 * @lid: multicast lid
299 * Return: 0 on success
301 int rvt_detach_mcast(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
303 struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
304 struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
305 struct rvt_ibport *ibp = rdi->ports[qp->port_num - 1];
306 struct rvt_mcast *mcast = NULL;
307 struct rvt_mcast_qp *p, *tmp, *delp = NULL;
312 if (ibqp->qp_num <= 1)
315 spin_lock_irq(&ibp->lock);
317 /* Find the GID in the mcast table. */
318 n = ibp->mcast_tree.rb_node;
321 spin_unlock_irq(&ibp->lock);
325 mcast = rb_entry(n, struct rvt_mcast, rb_node);
326 ret = memcmp(gid->raw, mcast->mcast_addr.mgid.raw,
330 } else if (ret > 0) {
333 /* MGID/MLID must match */
334 if (mcast->mcast_addr.lid != lid) {
335 spin_unlock_irq(&ibp->lock);
342 /* Search the QP list. */
343 list_for_each_entry_safe(p, tmp, &mcast->qp_list, list) {
347 * We found it, so remove it, but don't poison the forward
348 * link until we are sure there are no list walkers.
350 list_del_rcu(&p->list);
354 /* If this was the last attached QP, remove the GID too. */
355 if (list_empty(&mcast->qp_list)) {
356 rb_erase(&mcast->rb_node, &ibp->mcast_tree);
362 spin_unlock_irq(&ibp->lock);
363 /* QP not attached */
368 * Wait for any list walkers to finish before freeing the
371 wait_event(mcast->wait, atomic_read(&mcast->refcount) <= 1);
372 rvt_mcast_qp_free(delp);
375 atomic_dec(&mcast->refcount);
376 wait_event(mcast->wait, !atomic_read(&mcast->refcount));
377 rvt_mcast_free(mcast);
378 spin_lock_irq(&rdi->n_mcast_grps_lock);
379 rdi->n_mcast_grps_allocated--;
380 spin_unlock_irq(&rdi->n_mcast_grps_lock);
387 * rvt_mcast_tree_empty - determine if any qps are attached to any mcast group
388 * @rdi: rvt dev struct
390 * Return: in use count
392 int rvt_mcast_tree_empty(struct rvt_dev_info *rdi)
397 for (i = 0; i < rdi->dparms.nports; i++)
398 if (rdi->ports[i]->mcast_tree.rb_node)