blob: a97548ee6a97a097cdcc802997f27e9620ccc66c [file] [log] [blame]
Bryan O'Sullivan65221082006-03-29 15:23:38 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
Bryan O'Sullivan65221082006-03-29 15:23:38 -08003 * 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 */
45static struct rb_root mcast_tree;
46static 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 */
52static 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
63bail:
64 return mqp;
65}
66
67static 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 */
84static 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
97bail:
98 return mcast;
99}
100
101static void ipath_mcast_free(struct ipath_mcast *mcast)
102{
103 struct ipath_mcast_qp *p, *tmp;
104
105 list_for_each_entry_safe(p, tmp, &mcast->qp_list, list)
106 ipath_mcast_qp_free(p);
107
108 kfree(mcast);
109}
110
111/**
112 * ipath_mcast_find - search the global table for the given multicast GID
113 * @mgid: the multicast GID to search for
114 *
115 * Returns NULL if not found.
116 *
117 * The caller is responsible for decrementing the reference count if found.
118 */
119struct ipath_mcast *ipath_mcast_find(union ib_gid *mgid)
120{
121 struct rb_node *n;
122 unsigned long flags;
123 struct ipath_mcast *mcast;
124
125 spin_lock_irqsave(&mcast_lock, flags);
126 n = mcast_tree.rb_node;
127 while (n) {
128 int ret;
129
130 mcast = rb_entry(n, struct ipath_mcast, rb_node);
131
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;
142 }
143 }
144 spin_unlock_irqrestore(&mcast_lock, flags);
145
146 mcast = NULL;
147
148bail:
149 return mcast;
150}
151
152/**
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
156 *
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.
160 */
161static int ipath_mcast_add(struct ipath_mcast *mcast,
162 struct ipath_mcast_qp *mqp)
163{
164 struct rb_node **n = &mcast_tree.rb_node;
165 struct rb_node *pn = NULL;
166 unsigned long flags;
167 int ret;
168
169 spin_lock_irqsave(&mcast_lock, flags);
170
171 while (*n) {
172 struct ipath_mcast *tmcast;
173 struct ipath_mcast_qp *p;
174
175 pn = *n;
176 tmcast = rb_entry(pn, struct ipath_mcast, rb_node);
177
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;
183 }
184 if (ret > 0) {
185 n = &pn->rb_right;
186 continue;
187 }
188
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 spin_unlock_irqrestore(&mcast_lock, flags);
193 ret = ESRCH;
194 goto bail;
195 }
196 }
197 list_add_tail_rcu(&mqp->list, &tmcast->qp_list);
198 spin_unlock_irqrestore(&mcast_lock, flags);
199 ret = EEXIST;
200 goto bail;
201 }
202
203 list_add_tail_rcu(&mqp->list, &mcast->qp_list);
204
205 atomic_inc(&mcast->refcount);
206 rb_link_node(&mcast->rb_node, pn, n);
207 rb_insert_color(&mcast->rb_node, &mcast_tree);
208
209 spin_unlock_irqrestore(&mcast_lock, flags);
210
211 ret = 0;
212
213bail:
214 return ret;
215}
216
217int ipath_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
218{
219 struct ipath_qp *qp = to_iqp(ibqp);
220 struct ipath_mcast *mcast;
221 struct ipath_mcast_qp *mqp;
222 int ret;
223
224 /*
225 * Allocate data structures since its better to do this outside of
226 * spin locks and it will most likely be needed.
227 */
228 mcast = ipath_mcast_alloc(gid);
229 if (mcast == NULL) {
230 ret = -ENOMEM;
231 goto bail;
232 }
233 mqp = ipath_mcast_qp_alloc(qp);
234 if (mqp == NULL) {
235 ipath_mcast_free(mcast);
236 ret = -ENOMEM;
237 goto bail;
238 }
239 switch (ipath_mcast_add(mcast, mqp)) {
240 case ESRCH:
241 /* Neither was used: can't attach the same QP twice. */
242 ipath_mcast_qp_free(mqp);
243 ipath_mcast_free(mcast);
244 ret = -EINVAL;
245 goto bail;
246 case EEXIST: /* The mcast wasn't used */
247 ipath_mcast_free(mcast);
248 break;
249 default:
250 break;
251 }
252
253 ret = 0;
254
255bail:
256 return ret;
257}
258
259int ipath_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
260{
261 struct ipath_qp *qp = to_iqp(ibqp);
262 struct ipath_mcast *mcast = NULL;
263 struct ipath_mcast_qp *p, *tmp;
264 struct rb_node *n;
265 unsigned long flags;
266 int last = 0;
267 int ret;
268
269 spin_lock_irqsave(&mcast_lock, flags);
270
271 /* Find the GID in the mcast table. */
272 n = mcast_tree.rb_node;
273 while (1) {
274 if (n == NULL) {
275 spin_unlock_irqrestore(&mcast_lock, flags);
276 ret = 0;
277 goto bail;
278 }
279
280 mcast = rb_entry(n, struct ipath_mcast, rb_node);
281 ret = memcmp(gid->raw, mcast->mgid.raw,
282 sizeof(union ib_gid));
283 if (ret < 0)
284 n = n->rb_left;
285 else if (ret > 0)
286 n = n->rb_right;
287 else
288 break;
289 }
290
291 /* Search the QP list. */
292 list_for_each_entry_safe(p, tmp, &mcast->qp_list, list) {
293 if (p->qp != qp)
294 continue;
295 /*
296 * We found it, so remove it, but don't poison the forward
297 * link until we are sure there are no list walkers.
298 */
299 list_del_rcu(&p->list);
300
301 /* If this was the last attached QP, remove the GID too. */
302 if (list_empty(&mcast->qp_list)) {
303 rb_erase(&mcast->rb_node, &mcast_tree);
304 last = 1;
305 }
306 break;
307 }
308
309 spin_unlock_irqrestore(&mcast_lock, flags);
310
311 if (p) {
312 /*
313 * Wait for any list walkers to finish before freeing the
314 * list element.
315 */
316 wait_event(mcast->wait, atomic_read(&mcast->refcount) <= 1);
317 ipath_mcast_qp_free(p);
318 }
319 if (last) {
320 atomic_dec(&mcast->refcount);
321 wait_event(mcast->wait, !atomic_read(&mcast->refcount));
322 ipath_mcast_free(mcast);
323 }
324
325 ret = 0;
326
327bail:
328 return ret;
329}
330
331int ipath_mcast_tree_empty(void)
332{
333 return mcast_tree.rb_node == NULL;
334}