3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2015 Intel Corporation.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
21 * Copyright(c) 2015 Intel Corporation.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include <linux/rculist.h>
56 * mcast_qp_alloc - alloc a struct to link a QP to mcast GID struct
59 static struct hfi1_mcast_qp
*mcast_qp_alloc(struct hfi1_qp
*qp
)
61 struct hfi1_mcast_qp
*mqp
;
63 mqp
= kmalloc(sizeof(*mqp
), GFP_KERNEL
);
68 atomic_inc(&qp
->refcount
);
74 static void mcast_qp_free(struct hfi1_mcast_qp
*mqp
)
76 struct hfi1_qp
*qp
= mqp
->qp
;
78 /* Notify hfi1_destroy_qp() if it is waiting. */
79 if (atomic_dec_and_test(&qp
->refcount
))
86 * mcast_alloc - allocate the multicast GID structure
87 * @mgid: the multicast GID
89 * A list of QPs will be attached to this structure.
91 static struct hfi1_mcast
*mcast_alloc(union ib_gid
*mgid
)
93 struct hfi1_mcast
*mcast
;
95 mcast
= kmalloc(sizeof(*mcast
), GFP_KERNEL
);
100 INIT_LIST_HEAD(&mcast
->qp_list
);
101 init_waitqueue_head(&mcast
->wait
);
102 atomic_set(&mcast
->refcount
, 0);
103 mcast
->n_attached
= 0;
109 static void mcast_free(struct hfi1_mcast
*mcast
)
111 struct hfi1_mcast_qp
*p
, *tmp
;
113 list_for_each_entry_safe(p
, tmp
, &mcast
->qp_list
, list
)
120 * hfi1_mcast_find - search the global table for the given multicast GID
121 * @ibp: the IB port structure
122 * @mgid: the multicast GID to search for
124 * Returns NULL if not found.
126 * The caller is responsible for decrementing the reference count if found.
128 struct hfi1_mcast
*hfi1_mcast_find(struct hfi1_ibport
*ibp
, union ib_gid
*mgid
)
132 struct hfi1_mcast
*mcast
;
134 spin_lock_irqsave(&ibp
->lock
, flags
);
135 n
= ibp
->mcast_tree
.rb_node
;
139 mcast
= rb_entry(n
, struct hfi1_mcast
, rb_node
);
141 ret
= memcmp(mgid
->raw
, mcast
->mgid
.raw
,
142 sizeof(union ib_gid
));
148 atomic_inc(&mcast
->refcount
);
149 spin_unlock_irqrestore(&ibp
->lock
, flags
);
153 spin_unlock_irqrestore(&ibp
->lock
, flags
);
162 * mcast_add - insert mcast GID into table and attach QP struct
163 * @mcast: the mcast GID table
164 * @mqp: the QP to attach
166 * Return zero if both were added. Return EEXIST if the GID was already in
167 * the table but the QP was added. Return ESRCH if the QP was already
168 * attached and neither structure was added.
170 static int mcast_add(struct hfi1_ibdev
*dev
, struct hfi1_ibport
*ibp
,
171 struct hfi1_mcast
*mcast
, struct hfi1_mcast_qp
*mqp
)
173 struct rb_node
**n
= &ibp
->mcast_tree
.rb_node
;
174 struct rb_node
*pn
= NULL
;
177 spin_lock_irq(&ibp
->lock
);
180 struct hfi1_mcast
*tmcast
;
181 struct hfi1_mcast_qp
*p
;
184 tmcast
= rb_entry(pn
, struct hfi1_mcast
, rb_node
);
186 ret
= memcmp(mcast
->mgid
.raw
, tmcast
->mgid
.raw
,
187 sizeof(union ib_gid
));
197 /* Search the QP list to see if this is already there. */
198 list_for_each_entry_rcu(p
, &tmcast
->qp_list
, list
) {
199 if (p
->qp
== mqp
->qp
) {
204 if (tmcast
->n_attached
== hfi1_max_mcast_qp_attached
) {
209 tmcast
->n_attached
++;
211 list_add_tail_rcu(&mqp
->list
, &tmcast
->qp_list
);
216 spin_lock(&dev
->n_mcast_grps_lock
);
217 if (dev
->n_mcast_grps_allocated
== hfi1_max_mcast_grps
) {
218 spin_unlock(&dev
->n_mcast_grps_lock
);
223 dev
->n_mcast_grps_allocated
++;
224 spin_unlock(&dev
->n_mcast_grps_lock
);
228 list_add_tail_rcu(&mqp
->list
, &mcast
->qp_list
);
230 atomic_inc(&mcast
->refcount
);
231 rb_link_node(&mcast
->rb_node
, pn
, n
);
232 rb_insert_color(&mcast
->rb_node
, &ibp
->mcast_tree
);
237 spin_unlock_irq(&ibp
->lock
);
242 int hfi1_multicast_attach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
244 struct hfi1_qp
*qp
= to_iqp(ibqp
);
245 struct hfi1_ibdev
*dev
= to_idev(ibqp
->device
);
246 struct hfi1_ibport
*ibp
;
247 struct hfi1_mcast
*mcast
;
248 struct hfi1_mcast_qp
*mqp
;
251 if (ibqp
->qp_num
<= 1 || qp
->state
== IB_QPS_RESET
) {
257 * Allocate data structures since its better to do this outside of
258 * spin locks and it will most likely be needed.
260 mcast
= mcast_alloc(gid
);
265 mqp
= mcast_qp_alloc(qp
);
271 ibp
= to_iport(ibqp
->device
, qp
->port_num
);
272 switch (mcast_add(dev
, ibp
, mcast
, mqp
)) {
274 /* Neither was used: OK to attach the same QP twice. */
279 case EEXIST
: /* The mcast wasn't used */
284 /* Exceeded the maximum number of mcast groups. */
300 int hfi1_multicast_detach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
302 struct hfi1_qp
*qp
= to_iqp(ibqp
);
303 struct hfi1_ibdev
*dev
= to_idev(ibqp
->device
);
304 struct hfi1_ibport
*ibp
= to_iport(ibqp
->device
, qp
->port_num
);
305 struct hfi1_mcast
*mcast
= NULL
;
306 struct hfi1_mcast_qp
*p
, *tmp
;
311 if (ibqp
->qp_num
<= 1 || qp
->state
== IB_QPS_RESET
) {
316 spin_lock_irq(&ibp
->lock
);
318 /* Find the GID in the mcast table. */
319 n
= ibp
->mcast_tree
.rb_node
;
322 spin_unlock_irq(&ibp
->lock
);
327 mcast
= rb_entry(n
, struct hfi1_mcast
, rb_node
);
328 ret
= memcmp(gid
->raw
, mcast
->mgid
.raw
,
329 sizeof(union ib_gid
));
338 /* Search the QP list. */
339 list_for_each_entry_safe(p
, tmp
, &mcast
->qp_list
, list
) {
343 * We found it, so remove it, but don't poison the forward
344 * link until we are sure there are no list walkers.
346 list_del_rcu(&p
->list
);
349 /* If this was the last attached QP, remove the GID too. */
350 if (list_empty(&mcast
->qp_list
)) {
351 rb_erase(&mcast
->rb_node
, &ibp
->mcast_tree
);
357 spin_unlock_irq(&ibp
->lock
);
361 * Wait for any list walkers to finish before freeing the
364 wait_event(mcast
->wait
, atomic_read(&mcast
->refcount
) <= 1);
368 atomic_dec(&mcast
->refcount
);
369 wait_event(mcast
->wait
, !atomic_read(&mcast
->refcount
));
371 spin_lock_irq(&dev
->n_mcast_grps_lock
);
372 dev
->n_mcast_grps_allocated
--;
373 spin_unlock_irq(&dev
->n_mcast_grps_lock
);
382 int hfi1_mcast_tree_empty(struct hfi1_ibport
*ibp
)
384 return ibp
->mcast_tree
.rb_node
== NULL
;