2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/rculist.h>
39 * qib_mcast_qp_alloc - alloc a struct to link a QP to mcast GID struct
42 static struct qib_mcast_qp
*qib_mcast_qp_alloc(struct qib_qp
*qp
)
44 struct qib_mcast_qp
*mqp
;
46 mqp
= kmalloc(sizeof *mqp
, GFP_KERNEL
);
51 atomic_inc(&qp
->refcount
);
57 static void qib_mcast_qp_free(struct qib_mcast_qp
*mqp
)
59 struct qib_qp
*qp
= mqp
->qp
;
61 /* Notify qib_destroy_qp() if it is waiting. */
62 if (atomic_dec_and_test(&qp
->refcount
))
69 * qib_mcast_alloc - allocate the multicast GID structure
70 * @mgid: the multicast GID
72 * A list of QPs will be attached to this structure.
74 static struct qib_mcast
*qib_mcast_alloc(union ib_gid
*mgid
)
76 struct qib_mcast
*mcast
;
78 mcast
= kmalloc(sizeof *mcast
, GFP_KERNEL
);
83 INIT_LIST_HEAD(&mcast
->qp_list
);
84 init_waitqueue_head(&mcast
->wait
);
85 atomic_set(&mcast
->refcount
, 0);
86 mcast
->n_attached
= 0;
92 static void qib_mcast_free(struct qib_mcast
*mcast
)
94 struct qib_mcast_qp
*p
, *tmp
;
96 list_for_each_entry_safe(p
, tmp
, &mcast
->qp_list
, list
)
103 * qib_mcast_find - search the global table for the given multicast GID
104 * @ibp: the IB port structure
105 * @mgid: the multicast GID to search for
107 * Returns NULL if not found.
109 * The caller is responsible for decrementing the reference count if found.
111 struct qib_mcast
*qib_mcast_find(struct qib_ibport
*ibp
, union ib_gid
*mgid
)
115 struct qib_mcast
*mcast
;
117 spin_lock_irqsave(&ibp
->lock
, flags
);
118 n
= ibp
->mcast_tree
.rb_node
;
122 mcast
= rb_entry(n
, struct qib_mcast
, rb_node
);
124 ret
= memcmp(mgid
->raw
, mcast
->mgid
.raw
,
125 sizeof(union ib_gid
));
131 atomic_inc(&mcast
->refcount
);
132 spin_unlock_irqrestore(&ibp
->lock
, flags
);
136 spin_unlock_irqrestore(&ibp
->lock
, flags
);
145 * qib_mcast_add - insert mcast GID into table and attach QP struct
146 * @mcast: the mcast GID table
147 * @mqp: the QP to attach
149 * Return zero if both were added. Return EEXIST if the GID was already in
150 * the table but the QP was added. Return ESRCH if the QP was already
151 * attached and neither structure was added.
153 static int qib_mcast_add(struct qib_ibdev
*dev
, struct qib_ibport
*ibp
,
154 struct qib_mcast
*mcast
, struct qib_mcast_qp
*mqp
)
156 struct rb_node
**n
= &ibp
->mcast_tree
.rb_node
;
157 struct rb_node
*pn
= NULL
;
160 spin_lock_irq(&ibp
->lock
);
163 struct qib_mcast
*tmcast
;
164 struct qib_mcast_qp
*p
;
167 tmcast
= rb_entry(pn
, struct qib_mcast
, rb_node
);
169 ret
= memcmp(mcast
->mgid
.raw
, tmcast
->mgid
.raw
,
170 sizeof(union ib_gid
));
180 /* Search the QP list to see if this is already there. */
181 list_for_each_entry_rcu(p
, &tmcast
->qp_list
, list
) {
182 if (p
->qp
== mqp
->qp
) {
187 if (tmcast
->n_attached
== ib_qib_max_mcast_qp_attached
) {
192 tmcast
->n_attached
++;
194 list_add_tail_rcu(&mqp
->list
, &tmcast
->qp_list
);
199 spin_lock(&dev
->n_mcast_grps_lock
);
200 if (dev
->n_mcast_grps_allocated
== ib_qib_max_mcast_grps
) {
201 spin_unlock(&dev
->n_mcast_grps_lock
);
206 dev
->n_mcast_grps_allocated
++;
207 spin_unlock(&dev
->n_mcast_grps_lock
);
211 list_add_tail_rcu(&mqp
->list
, &mcast
->qp_list
);
213 atomic_inc(&mcast
->refcount
);
214 rb_link_node(&mcast
->rb_node
, pn
, n
);
215 rb_insert_color(&mcast
->rb_node
, &ibp
->mcast_tree
);
220 spin_unlock_irq(&ibp
->lock
);
225 int qib_multicast_attach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
227 struct qib_qp
*qp
= to_iqp(ibqp
);
228 struct qib_ibdev
*dev
= to_idev(ibqp
->device
);
229 struct qib_ibport
*ibp
;
230 struct qib_mcast
*mcast
;
231 struct qib_mcast_qp
*mqp
;
234 if (ibqp
->qp_num
<= 1 || qp
->state
== IB_QPS_RESET
) {
240 * Allocate data structures since its better to do this outside of
241 * spin locks and it will most likely be needed.
243 mcast
= qib_mcast_alloc(gid
);
248 mqp
= qib_mcast_qp_alloc(qp
);
250 qib_mcast_free(mcast
);
254 ibp
= to_iport(ibqp
->device
, qp
->port_num
);
255 switch (qib_mcast_add(dev
, ibp
, mcast
, mqp
)) {
257 /* Neither was used: OK to attach the same QP twice. */
258 qib_mcast_qp_free(mqp
);
259 qib_mcast_free(mcast
);
262 case EEXIST
: /* The mcast wasn't used */
263 qib_mcast_free(mcast
);
267 /* Exceeded the maximum number of mcast groups. */
268 qib_mcast_qp_free(mqp
);
269 qib_mcast_free(mcast
);
283 int qib_multicast_detach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
285 struct qib_qp
*qp
= to_iqp(ibqp
);
286 struct qib_ibdev
*dev
= to_idev(ibqp
->device
);
287 struct qib_ibport
*ibp
= to_iport(ibqp
->device
, qp
->port_num
);
288 struct qib_mcast
*mcast
= NULL
;
289 struct qib_mcast_qp
*p
, *tmp
;
294 if (ibqp
->qp_num
<= 1 || qp
->state
== IB_QPS_RESET
) {
299 spin_lock_irq(&ibp
->lock
);
301 /* Find the GID in the mcast table. */
302 n
= ibp
->mcast_tree
.rb_node
;
305 spin_unlock_irq(&ibp
->lock
);
310 mcast
= rb_entry(n
, struct qib_mcast
, rb_node
);
311 ret
= memcmp(gid
->raw
, mcast
->mgid
.raw
,
312 sizeof(union ib_gid
));
321 /* Search the QP list. */
322 list_for_each_entry_safe(p
, tmp
, &mcast
->qp_list
, list
) {
326 * We found it, so remove it, but don't poison the forward
327 * link until we are sure there are no list walkers.
329 list_del_rcu(&p
->list
);
332 /* If this was the last attached QP, remove the GID too. */
333 if (list_empty(&mcast
->qp_list
)) {
334 rb_erase(&mcast
->rb_node
, &ibp
->mcast_tree
);
340 spin_unlock_irq(&ibp
->lock
);
344 * Wait for any list walkers to finish before freeing the
347 wait_event(mcast
->wait
, atomic_read(&mcast
->refcount
) <= 1);
348 qib_mcast_qp_free(p
);
351 atomic_dec(&mcast
->refcount
);
352 wait_event(mcast
->wait
, !atomic_read(&mcast
->refcount
));
353 qib_mcast_free(mcast
);
354 spin_lock_irq(&dev
->n_mcast_grps_lock
);
355 dev
->n_mcast_grps_allocated
--;
356 spin_unlock_irq(&dev
->n_mcast_grps_lock
);
365 int qib_mcast_tree_empty(struct qib_ibport
*ibp
)
367 return ibp
->mcast_tree
.rb_node
== NULL
;