1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/rculist.h>
35
36#include "qib.h"
37
38
39
40
41
42static struct qib_mcast_qp *qib_mcast_qp_alloc(struct qib_qp *qp)
43{
44 struct qib_mcast_qp *mqp;
45
46 mqp = kmalloc(sizeof *mqp, GFP_KERNEL);
47 if (!mqp)
48 goto bail;
49
50 mqp->qp = qp;
51 atomic_inc(&qp->refcount);
52
53bail:
54 return mqp;
55}
56
57static void qib_mcast_qp_free(struct qib_mcast_qp *mqp)
58{
59 struct qib_qp *qp = mqp->qp;
60
61
62 if (atomic_dec_and_test(&qp->refcount))
63 wake_up(&qp->wait);
64
65 kfree(mqp);
66}
67
68
69
70
71
72
73
74static struct qib_mcast *qib_mcast_alloc(union ib_gid *mgid)
75{
76 struct qib_mcast *mcast;
77
78 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
79 if (!mcast)
80 goto bail;
81
82 mcast->mgid = *mgid;
83 INIT_LIST_HEAD(&mcast->qp_list);
84 init_waitqueue_head(&mcast->wait);
85 atomic_set(&mcast->refcount, 0);
86 mcast->n_attached = 0;
87
88bail:
89 return mcast;
90}
91
92static void qib_mcast_free(struct qib_mcast *mcast)
93{
94 struct qib_mcast_qp *p, *tmp;
95
96 list_for_each_entry_safe(p, tmp, &mcast->qp_list, list)
97 qib_mcast_qp_free(p);
98
99 kfree(mcast);
100}
101
102
103
104
105
106
107
108
109
110
111struct qib_mcast *qib_mcast_find(struct qib_ibport *ibp, union ib_gid *mgid)
112{
113 struct rb_node *n;
114 unsigned long flags;
115 struct qib_mcast *mcast;
116
117 spin_lock_irqsave(&ibp->lock, flags);
118 n = ibp->mcast_tree.rb_node;
119 while (n) {
120 int ret;
121
122 mcast = rb_entry(n, struct qib_mcast, rb_node);
123
124 ret = memcmp(mgid->raw, mcast->mgid.raw,
125 sizeof(union ib_gid));
126 if (ret < 0)
127 n = n->rb_left;
128 else if (ret > 0)
129 n = n->rb_right;
130 else {
131 atomic_inc(&mcast->refcount);
132 spin_unlock_irqrestore(&ibp->lock, flags);
133 goto bail;
134 }
135 }
136 spin_unlock_irqrestore(&ibp->lock, flags);
137
138 mcast = NULL;
139
140bail:
141 return mcast;
142}
143
144
145
146
147
148
149
150
151
152
153static int qib_mcast_add(struct qib_ibdev *dev, struct qib_ibport *ibp,
154 struct qib_mcast *mcast, struct qib_mcast_qp *mqp)
155{
156 struct rb_node **n = &ibp->mcast_tree.rb_node;
157 struct rb_node *pn = NULL;
158 int ret;
159
160 spin_lock_irq(&ibp->lock);
161
162 while (*n) {
163 struct qib_mcast *tmcast;
164 struct qib_mcast_qp *p;
165
166 pn = *n;
167 tmcast = rb_entry(pn, struct qib_mcast, rb_node);
168
169 ret = memcmp(mcast->mgid.raw, tmcast->mgid.raw,
170 sizeof(union ib_gid));
171 if (ret < 0) {
172 n = &pn->rb_left;
173 continue;
174 }
175 if (ret > 0) {
176 n = &pn->rb_right;
177 continue;
178 }
179
180
181 list_for_each_entry_rcu(p, &tmcast->qp_list, list) {
182 if (p->qp == mqp->qp) {
183 ret = ESRCH;
184 goto bail;
185 }
186 }
187 if (tmcast->n_attached == ib_qib_max_mcast_qp_attached) {
188 ret = ENOMEM;
189 goto bail;
190 }
191
192 tmcast->n_attached++;
193
194 list_add_tail_rcu(&mqp->list, &tmcast->qp_list);
195 ret = EEXIST;
196 goto bail;
197 }
198
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);
202 ret = ENOMEM;
203 goto bail;
204 }
205
206 dev->n_mcast_grps_allocated++;
207 spin_unlock(&dev->n_mcast_grps_lock);
208
209 mcast->n_attached++;
210
211 list_add_tail_rcu(&mqp->list, &mcast->qp_list);
212
213 atomic_inc(&mcast->refcount);
214 rb_link_node(&mcast->rb_node, pn, n);
215 rb_insert_color(&mcast->rb_node, &ibp->mcast_tree);
216
217 ret = 0;
218
219bail:
220 spin_unlock_irq(&ibp->lock);
221
222 return ret;
223}
224
225int qib_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
226{
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;
232 int ret;
233
234 if (ibqp->qp_num <= 1 || qp->state == IB_QPS_RESET) {
235 ret = -EINVAL;
236 goto bail;
237 }
238
239
240
241
242
243 mcast = qib_mcast_alloc(gid);
244 if (mcast == NULL) {
245 ret = -ENOMEM;
246 goto bail;
247 }
248 mqp = qib_mcast_qp_alloc(qp);
249 if (mqp == NULL) {
250 qib_mcast_free(mcast);
251 ret = -ENOMEM;
252 goto bail;
253 }
254 ibp = to_iport(ibqp->device, qp->port_num);
255 switch (qib_mcast_add(dev, ibp, mcast, mqp)) {
256 case ESRCH:
257
258 qib_mcast_qp_free(mqp);
259 qib_mcast_free(mcast);
260 break;
261
262 case EEXIST:
263 qib_mcast_free(mcast);
264 break;
265
266 case ENOMEM:
267
268 qib_mcast_qp_free(mqp);
269 qib_mcast_free(mcast);
270 ret = -ENOMEM;
271 goto bail;
272
273 default:
274 break;
275 }
276
277 ret = 0;
278
279bail:
280 return ret;
281}
282
283int qib_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
284{
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;
290 struct rb_node *n;
291 int last = 0;
292 int ret;
293
294 if (ibqp->qp_num <= 1 || qp->state == IB_QPS_RESET) {
295 ret = -EINVAL;
296 goto bail;
297 }
298
299 spin_lock_irq(&ibp->lock);
300
301
302 n = ibp->mcast_tree.rb_node;
303 while (1) {
304 if (n == NULL) {
305 spin_unlock_irq(&ibp->lock);
306 ret = -EINVAL;
307 goto bail;
308 }
309
310 mcast = rb_entry(n, struct qib_mcast, rb_node);
311 ret = memcmp(gid->raw, mcast->mgid.raw,
312 sizeof(union ib_gid));
313 if (ret < 0)
314 n = n->rb_left;
315 else if (ret > 0)
316 n = n->rb_right;
317 else
318 break;
319 }
320
321
322 list_for_each_entry_safe(p, tmp, &mcast->qp_list, list) {
323 if (p->qp != qp)
324 continue;
325
326
327
328
329 list_del_rcu(&p->list);
330 mcast->n_attached--;
331
332
333 if (list_empty(&mcast->qp_list)) {
334 rb_erase(&mcast->rb_node, &ibp->mcast_tree);
335 last = 1;
336 }
337 break;
338 }
339
340 spin_unlock_irq(&ibp->lock);
341
342 if (p) {
343
344
345
346
347 wait_event(mcast->wait, atomic_read(&mcast->refcount) <= 1);
348 qib_mcast_qp_free(p);
349 }
350 if (last) {
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);
357 }
358
359 ret = 0;
360
361bail:
362 return ret;
363}
364
365int qib_mcast_tree_empty(struct qib_ibport *ibp)
366{
367 return ibp->mcast_tree.rb_node == NULL;
368}
369