Merge branch 'fix/pcm-hwptr' into for-linus
[linux/fpc-iii.git] / drivers / infiniband / hw / ipath / ipath_verbs_mcast.c
blobd73e3223287930c344784cea458e7eacf2b820ad
1 /*
2 * Copyright (c) 2006, 2007 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
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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
31 * SOFTWARE.
34 #include <linux/rculist.h>
36 #include "ipath_verbs.h"
39 * Global table of GID to attached QPs.
40 * The table is global to all ipath devices since a send from one QP/device
41 * needs to be locally routed to any locally attached QPs on the same
42 * or different device.
44 static struct rb_root mcast_tree;
45 static DEFINE_SPINLOCK(mcast_lock);
47 /**
48 * ipath_mcast_qp_alloc - alloc a struct to link a QP to mcast GID struct
49 * @qp: the QP to link
51 static struct ipath_mcast_qp *ipath_mcast_qp_alloc(struct ipath_qp *qp)
53 struct ipath_mcast_qp *mqp;
55 mqp = kmalloc(sizeof *mqp, GFP_KERNEL);
56 if (!mqp)
57 goto bail;
59 mqp->qp = qp;
60 atomic_inc(&qp->refcount);
62 bail:
63 return mqp;
66 static void ipath_mcast_qp_free(struct ipath_mcast_qp *mqp)
68 struct ipath_qp *qp = mqp->qp;
70 /* Notify ipath_destroy_qp() if it is waiting. */
71 if (atomic_dec_and_test(&qp->refcount))
72 wake_up(&qp->wait);
74 kfree(mqp);
77 /**
78 * ipath_mcast_alloc - allocate the multicast GID structure
79 * @mgid: the multicast GID
81 * A list of QPs will be attached to this structure.
83 static struct ipath_mcast *ipath_mcast_alloc(union ib_gid *mgid)
85 struct ipath_mcast *mcast;
87 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
88 if (!mcast)
89 goto bail;
91 mcast->mgid = *mgid;
92 INIT_LIST_HEAD(&mcast->qp_list);
93 init_waitqueue_head(&mcast->wait);
94 atomic_set(&mcast->refcount, 0);
95 mcast->n_attached = 0;
97 bail:
98 return mcast;
101 static void ipath_mcast_free(struct ipath_mcast *mcast)
103 struct ipath_mcast_qp *p, *tmp;
105 list_for_each_entry_safe(p, tmp, &mcast->qp_list, list)
106 ipath_mcast_qp_free(p);
108 kfree(mcast);
112 * ipath_mcast_find - search the global table for the given multicast GID
113 * @mgid: the multicast GID to search for
115 * Returns NULL if not found.
117 * The caller is responsible for decrementing the reference count if found.
119 struct ipath_mcast *ipath_mcast_find(union ib_gid *mgid)
121 struct rb_node *n;
122 unsigned long flags;
123 struct ipath_mcast *mcast;
125 spin_lock_irqsave(&mcast_lock, flags);
126 n = mcast_tree.rb_node;
127 while (n) {
128 int ret;
130 mcast = rb_entry(n, struct ipath_mcast, rb_node);
132 ret = memcmp(mgid->raw, mcast->mgid.raw,
133 sizeof(union ib_gid));
134 if (ret < 0)
135 n = n->rb_left;
136 else if (ret > 0)
137 n = n->rb_right;
138 else {
139 atomic_inc(&mcast->refcount);
140 spin_unlock_irqrestore(&mcast_lock, flags);
141 goto bail;
144 spin_unlock_irqrestore(&mcast_lock, flags);
146 mcast = NULL;
148 bail:
149 return mcast;
153 * ipath_mcast_add - insert mcast GID into table and attach QP struct
154 * @mcast: the mcast GID table
155 * @mqp: the QP to attach
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.
161 static int ipath_mcast_add(struct ipath_ibdev *dev,
162 struct ipath_mcast *mcast,
163 struct ipath_mcast_qp *mqp)
165 struct rb_node **n = &mcast_tree.rb_node;
166 struct rb_node *pn = NULL;
167 int ret;
169 spin_lock_irq(&mcast_lock);
171 while (*n) {
172 struct ipath_mcast *tmcast;
173 struct ipath_mcast_qp *p;
175 pn = *n;
176 tmcast = rb_entry(pn, struct ipath_mcast, rb_node);
178 ret = memcmp(mcast->mgid.raw, tmcast->mgid.raw,
179 sizeof(union ib_gid));
180 if (ret < 0) {
181 n = &pn->rb_left;
182 continue;
184 if (ret > 0) {
185 n = &pn->rb_right;
186 continue;
189 /* Search the QP list to see if this is already there. */
190 list_for_each_entry_rcu(p, &tmcast->qp_list, list) {
191 if (p->qp == mqp->qp) {
192 ret = ESRCH;
193 goto bail;
196 if (tmcast->n_attached == ib_ipath_max_mcast_qp_attached) {
197 ret = ENOMEM;
198 goto bail;
201 tmcast->n_attached++;
203 list_add_tail_rcu(&mqp->list, &tmcast->qp_list);
204 ret = EEXIST;
205 goto bail;
208 spin_lock(&dev->n_mcast_grps_lock);
209 if (dev->n_mcast_grps_allocated == ib_ipath_max_mcast_grps) {
210 spin_unlock(&dev->n_mcast_grps_lock);
211 ret = ENOMEM;
212 goto bail;
215 dev->n_mcast_grps_allocated++;
216 spin_unlock(&dev->n_mcast_grps_lock);
218 mcast->n_attached++;
220 list_add_tail_rcu(&mqp->list, &mcast->qp_list);
222 atomic_inc(&mcast->refcount);
223 rb_link_node(&mcast->rb_node, pn, n);
224 rb_insert_color(&mcast->rb_node, &mcast_tree);
226 ret = 0;
228 bail:
229 spin_unlock_irq(&mcast_lock);
231 return ret;
234 int ipath_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
236 struct ipath_qp *qp = to_iqp(ibqp);
237 struct ipath_ibdev *dev = to_idev(ibqp->device);
238 struct ipath_mcast *mcast;
239 struct ipath_mcast_qp *mqp;
240 int ret;
243 * Allocate data structures since its better to do this outside of
244 * spin locks and it will most likely be needed.
246 mcast = ipath_mcast_alloc(gid);
247 if (mcast == NULL) {
248 ret = -ENOMEM;
249 goto bail;
251 mqp = ipath_mcast_qp_alloc(qp);
252 if (mqp == NULL) {
253 ipath_mcast_free(mcast);
254 ret = -ENOMEM;
255 goto bail;
257 switch (ipath_mcast_add(dev, mcast, mqp)) {
258 case ESRCH:
259 /* Neither was used: can't attach the same QP twice. */
260 ipath_mcast_qp_free(mqp);
261 ipath_mcast_free(mcast);
262 ret = -EINVAL;
263 goto bail;
264 case EEXIST: /* The mcast wasn't used */
265 ipath_mcast_free(mcast);
266 break;
267 case ENOMEM:
268 /* Exceeded the maximum number of mcast groups. */
269 ipath_mcast_qp_free(mqp);
270 ipath_mcast_free(mcast);
271 ret = -ENOMEM;
272 goto bail;
273 default:
274 break;
277 ret = 0;
279 bail:
280 return ret;
283 int ipath_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
285 struct ipath_qp *qp = to_iqp(ibqp);
286 struct ipath_ibdev *dev = to_idev(ibqp->device);
287 struct ipath_mcast *mcast = NULL;
288 struct ipath_mcast_qp *p, *tmp;
289 struct rb_node *n;
290 int last = 0;
291 int ret;
293 spin_lock_irq(&mcast_lock);
295 /* Find the GID in the mcast table. */
296 n = mcast_tree.rb_node;
297 while (1) {
298 if (n == NULL) {
299 spin_unlock_irq(&mcast_lock);
300 ret = -EINVAL;
301 goto bail;
304 mcast = rb_entry(n, struct ipath_mcast, rb_node);
305 ret = memcmp(gid->raw, mcast->mgid.raw,
306 sizeof(union ib_gid));
307 if (ret < 0)
308 n = n->rb_left;
309 else if (ret > 0)
310 n = n->rb_right;
311 else
312 break;
315 /* Search the QP list. */
316 list_for_each_entry_safe(p, tmp, &mcast->qp_list, list) {
317 if (p->qp != qp)
318 continue;
320 * We found it, so remove it, but don't poison the forward
321 * link until we are sure there are no list walkers.
323 list_del_rcu(&p->list);
324 mcast->n_attached--;
326 /* If this was the last attached QP, remove the GID too. */
327 if (list_empty(&mcast->qp_list)) {
328 rb_erase(&mcast->rb_node, &mcast_tree);
329 last = 1;
331 break;
334 spin_unlock_irq(&mcast_lock);
336 if (p) {
338 * Wait for any list walkers to finish before freeing the
339 * list element.
341 wait_event(mcast->wait, atomic_read(&mcast->refcount) <= 1);
342 ipath_mcast_qp_free(p);
344 if (last) {
345 atomic_dec(&mcast->refcount);
346 wait_event(mcast->wait, !atomic_read(&mcast->refcount));
347 ipath_mcast_free(mcast);
348 spin_lock_irq(&dev->n_mcast_grps_lock);
349 dev->n_mcast_grps_allocated--;
350 spin_unlock_irq(&dev->n_mcast_grps_lock);
353 ret = 0;
355 bail:
356 return ret;
359 int ipath_mcast_tree_empty(void)
361 return mcast_tree.rb_node == NULL;