Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / infiniband / hw / ipath / ipath_verbs_mcast.c
1 /*
2  * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3  * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4  *
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:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
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.
23  *
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.
32  */
33
34 #include <linux/list.h>
35 #include <linux/rcupdate.h>
36
37 #include "ipath_verbs.h"
38
39 /*
40  * Global table of GID to attached QPs.
41  * The table is global to all ipath devices since a send from one QP/device
42  * needs to be locally routed to any locally attached QPs on the same
43  * or different device.
44  */
45 static struct rb_root mcast_tree;
46 static DEFINE_SPINLOCK(mcast_lock);
47
48 /**
49  * ipath_mcast_qp_alloc - alloc a struct to link a QP to mcast GID struct
50  * @qp: the QP to link
51  */
52 static struct ipath_mcast_qp *ipath_mcast_qp_alloc(struct ipath_qp *qp)
53 {
54         struct ipath_mcast_qp *mqp;
55
56         mqp = kmalloc(sizeof *mqp, GFP_KERNEL);
57         if (!mqp)
58                 goto bail;
59
60         mqp->qp = qp;
61         atomic_inc(&qp->refcount);
62
63 bail:
64         return mqp;
65 }
66
67 static void ipath_mcast_qp_free(struct ipath_mcast_qp *mqp)
68 {
69         struct ipath_qp *qp = mqp->qp;
70
71         /* Notify ipath_destroy_qp() if it is waiting. */
72         if (atomic_dec_and_test(&qp->refcount))
73                 wake_up(&qp->wait);
74
75         kfree(mqp);
76 }
77
78 /**
79  * ipath_mcast_alloc - allocate the multicast GID structure
80  * @mgid: the multicast GID
81  *
82  * A list of QPs will be attached to this structure.
83  */
84 static struct ipath_mcast *ipath_mcast_alloc(union ib_gid *mgid)
85 {
86         struct ipath_mcast *mcast;
87
88         mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
89         if (!mcast)
90                 goto bail;
91
92         mcast->mgid = *mgid;
93         INIT_LIST_HEAD(&mcast->qp_list);
94         init_waitqueue_head(&mcast->wait);
95         atomic_set(&mcast->refcount, 0);
96         mcast->n_attached = 0;
97
98 bail:
99         return mcast;
100 }
101
102 static void ipath_mcast_free(struct ipath_mcast *mcast)
103 {
104         struct ipath_mcast_qp *p, *tmp;
105
106         list_for_each_entry_safe(p, tmp, &mcast->qp_list, list)
107                 ipath_mcast_qp_free(p);
108
109         kfree(mcast);
110 }
111
112 /**
113  * ipath_mcast_find - search the global table for the given multicast GID
114  * @mgid: the multicast GID to search for
115  *
116  * Returns NULL if not found.
117  *
118  * The caller is responsible for decrementing the reference count if found.
119  */
120 struct ipath_mcast *ipath_mcast_find(union ib_gid *mgid)
121 {
122         struct rb_node *n;
123         unsigned long flags;
124         struct ipath_mcast *mcast;
125
126         spin_lock_irqsave(&mcast_lock, flags);
127         n = mcast_tree.rb_node;
128         while (n) {
129                 int ret;
130
131                 mcast = rb_entry(n, struct ipath_mcast, rb_node);
132
133                 ret = memcmp(mgid->raw, mcast->mgid.raw,
134                              sizeof(union ib_gid));
135                 if (ret < 0)
136                         n = n->rb_left;
137                 else if (ret > 0)
138                         n = n->rb_right;
139                 else {
140                         atomic_inc(&mcast->refcount);
141                         spin_unlock_irqrestore(&mcast_lock, flags);
142                         goto bail;
143                 }
144         }
145         spin_unlock_irqrestore(&mcast_lock, flags);
146
147         mcast = NULL;
148
149 bail:
150         return mcast;
151 }
152
153 /**
154  * ipath_mcast_add - insert mcast GID into table and attach QP struct
155  * @mcast: the mcast GID table
156  * @mqp: the QP to attach
157  *
158  * Return zero if both were added.  Return EEXIST if the GID was already in
159  * the table but the QP was added.  Return ESRCH if the QP was already
160  * attached and neither structure was added.
161  */
162 static int ipath_mcast_add(struct ipath_ibdev *dev,
163                            struct ipath_mcast *mcast,
164                            struct ipath_mcast_qp *mqp)
165 {
166         struct rb_node **n = &mcast_tree.rb_node;
167         struct rb_node *pn = NULL;
168         unsigned long flags;
169         int ret;
170
171         spin_lock_irqsave(&mcast_lock, flags);
172
173         while (*n) {
174                 struct ipath_mcast *tmcast;
175                 struct ipath_mcast_qp *p;
176
177                 pn = *n;
178                 tmcast = rb_entry(pn, struct ipath_mcast, rb_node);
179
180                 ret = memcmp(mcast->mgid.raw, tmcast->mgid.raw,
181                              sizeof(union ib_gid));
182                 if (ret < 0) {
183                         n = &pn->rb_left;
184                         continue;
185                 }
186                 if (ret > 0) {
187                         n = &pn->rb_right;
188                         continue;
189                 }
190
191                 /* Search the QP list to see if this is already there. */
192                 list_for_each_entry_rcu(p, &tmcast->qp_list, list) {
193                         if (p->qp == mqp->qp) {
194                                 ret = ESRCH;
195                                 goto bail;
196                         }
197                 }
198                 if (tmcast->n_attached == ib_ipath_max_mcast_qp_attached) {
199                         ret = ENOMEM;
200                         goto bail;
201                 }
202
203                 tmcast->n_attached++;
204
205                 list_add_tail_rcu(&mqp->list, &tmcast->qp_list);
206                 ret = EEXIST;
207                 goto bail;
208         }
209
210         if (dev->n_mcast_grps_allocated == ib_ipath_max_mcast_grps) {
211                 ret = ENOMEM;
212                 goto bail;
213         }
214
215         dev->n_mcast_grps_allocated++;
216
217         list_add_tail_rcu(&mqp->list, &mcast->qp_list);
218
219         atomic_inc(&mcast->refcount);
220         rb_link_node(&mcast->rb_node, pn, n);
221         rb_insert_color(&mcast->rb_node, &mcast_tree);
222
223         ret = 0;
224
225 bail:
226         spin_unlock_irqrestore(&mcast_lock, flags);
227
228         return ret;
229 }
230
231 int ipath_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
232 {
233         struct ipath_qp *qp = to_iqp(ibqp);
234         struct ipath_ibdev *dev = to_idev(ibqp->device);
235         struct ipath_mcast *mcast;
236         struct ipath_mcast_qp *mqp;
237         int ret;
238
239         /*
240          * Allocate data structures since its better to do this outside of
241          * spin locks and it will most likely be needed.
242          */
243         mcast = ipath_mcast_alloc(gid);
244         if (mcast == NULL) {
245                 ret = -ENOMEM;
246                 goto bail;
247         }
248         mqp = ipath_mcast_qp_alloc(qp);
249         if (mqp == NULL) {
250                 ipath_mcast_free(mcast);
251                 ret = -ENOMEM;
252                 goto bail;
253         }
254         switch (ipath_mcast_add(dev, mcast, mqp)) {
255         case ESRCH:
256                 /* Neither was used: can't attach the same QP twice. */
257                 ipath_mcast_qp_free(mqp);
258                 ipath_mcast_free(mcast);
259                 ret = -EINVAL;
260                 goto bail;
261         case EEXIST:            /* The mcast wasn't used */
262                 ipath_mcast_free(mcast);
263                 break;
264         case ENOMEM:
265                 /* Exceeded the maximum number of mcast groups. */
266                 ipath_mcast_qp_free(mqp);
267                 ipath_mcast_free(mcast);
268                 ret = -ENOMEM;
269                 goto bail;
270         default:
271                 break;
272         }
273
274         ret = 0;
275
276 bail:
277         return ret;
278 }
279
280 int ipath_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
281 {
282         struct ipath_qp *qp = to_iqp(ibqp);
283         struct ipath_ibdev *dev = to_idev(ibqp->device);
284         struct ipath_mcast *mcast = NULL;
285         struct ipath_mcast_qp *p, *tmp;
286         struct rb_node *n;
287         unsigned long flags;
288         int last = 0;
289         int ret;
290
291         spin_lock_irqsave(&mcast_lock, flags);
292
293         /* Find the GID in the mcast table. */
294         n = mcast_tree.rb_node;
295         while (1) {
296                 if (n == NULL) {
297                         spin_unlock_irqrestore(&mcast_lock, flags);
298                         ret = -EINVAL;
299                         goto bail;
300                 }
301
302                 mcast = rb_entry(n, struct ipath_mcast, rb_node);
303                 ret = memcmp(gid->raw, mcast->mgid.raw,
304                              sizeof(union ib_gid));
305                 if (ret < 0)
306                         n = n->rb_left;
307                 else if (ret > 0)
308                         n = n->rb_right;
309                 else
310                         break;
311         }
312
313         /* Search the QP list. */
314         list_for_each_entry_safe(p, tmp, &mcast->qp_list, list) {
315                 if (p->qp != qp)
316                         continue;
317                 /*
318                  * We found it, so remove it, but don't poison the forward
319                  * link until we are sure there are no list walkers.
320                  */
321                 list_del_rcu(&p->list);
322                 mcast->n_attached--;
323
324                 /* If this was the last attached QP, remove the GID too. */
325                 if (list_empty(&mcast->qp_list)) {
326                         rb_erase(&mcast->rb_node, &mcast_tree);
327                         last = 1;
328                 }
329                 break;
330         }
331
332         spin_unlock_irqrestore(&mcast_lock, flags);
333
334         if (p) {
335                 /*
336                  * Wait for any list walkers to finish before freeing the
337                  * list element.
338                  */
339                 wait_event(mcast->wait, atomic_read(&mcast->refcount) <= 1);
340                 ipath_mcast_qp_free(p);
341         }
342         if (last) {
343                 atomic_dec(&mcast->refcount);
344                 wait_event(mcast->wait, !atomic_read(&mcast->refcount));
345                 ipath_mcast_free(mcast);
346                 dev->n_mcast_grps_allocated--;
347         }
348
349         ret = 0;
350
351 bail:
352         return ret;
353 }
354
355 int ipath_mcast_tree_empty(void)
356 {
357         return mcast_tree.rb_node == NULL;
358 }