IPoIB: Fix address handle refcounting for multicast groups
[safe/jmp/linux-2.6] / drivers / infiniband / ulp / ipoib / ipoib_multicast.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4  * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $
35  */
36
37 #include <linux/skbuff.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/ip.h>
40 #include <linux/in.h>
41 #include <linux/igmp.h>
42 #include <linux/inetdevice.h>
43 #include <linux/delay.h>
44 #include <linux/completion.h>
45
46 #include <net/dst.h>
47
48 #include "ipoib.h"
49
50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
51 static int mcast_debug_level;
52
53 module_param(mcast_debug_level, int, 0644);
54 MODULE_PARM_DESC(mcast_debug_level,
55                  "Enable multicast debug tracing if > 0");
56 #endif
57
58 static DECLARE_MUTEX(mcast_mutex);
59
60 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
61 struct ipoib_mcast {
62         struct ib_sa_mcmember_rec mcmember;
63         struct ipoib_ah          *ah;
64
65         struct rb_node    rb_node;
66         struct list_head  list;
67         struct completion done;
68
69         int                 query_id;
70         struct ib_sa_query *query;
71
72         unsigned long created;
73         unsigned long backoff;
74
75         unsigned long flags;
76         unsigned char logcount;
77
78         struct list_head  neigh_list;
79
80         struct sk_buff_head pkt_queue;
81
82         struct net_device *dev;
83 };
84
85 struct ipoib_mcast_iter {
86         struct net_device *dev;
87         union ib_gid       mgid;
88         unsigned long      created;
89         unsigned int       queuelen;
90         unsigned int       complete;
91         unsigned int       send_only;
92 };
93
94 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
95 {
96         struct net_device *dev = mcast->dev;
97         struct ipoib_dev_priv *priv = netdev_priv(dev);
98         struct ipoib_neigh *neigh, *tmp;
99         unsigned long flags;
100
101         ipoib_dbg_mcast(netdev_priv(dev),
102                         "deleting multicast group " IPOIB_GID_FMT "\n",
103                         IPOIB_GID_ARG(mcast->mcmember.mgid));
104
105         spin_lock_irqsave(&priv->lock, flags);
106
107         list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
108                 /*
109                  * It's safe to call ipoib_put_ah() inside priv->lock
110                  * here, because we know that mcast->ah will always
111                  * hold one more reference, so ipoib_put_ah() will
112                  * never do more than decrement the ref count.
113                  */
114                 if (neigh->ah)
115                         ipoib_put_ah(neigh->ah);
116                 *to_ipoib_neigh(neigh->neighbour) = NULL;
117                 neigh->neighbour->ops->destructor = NULL;
118                 kfree(neigh);
119         }
120
121         spin_unlock_irqrestore(&priv->lock, flags);
122
123         if (mcast->ah)
124                 ipoib_put_ah(mcast->ah);
125
126         while (!skb_queue_empty(&mcast->pkt_queue))
127                 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
128
129         kfree(mcast);
130 }
131
132 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
133                                              int can_sleep)
134 {
135         struct ipoib_mcast *mcast;
136
137         mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
138         if (!mcast)
139                 return NULL;
140
141         mcast->dev = dev;
142         mcast->created = jiffies;
143         mcast->backoff = 1;
144
145         INIT_LIST_HEAD(&mcast->list);
146         INIT_LIST_HEAD(&mcast->neigh_list);
147         skb_queue_head_init(&mcast->pkt_queue);
148
149         return mcast;
150 }
151
152 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
153 {
154         struct ipoib_dev_priv *priv = netdev_priv(dev);
155         struct rb_node *n = priv->multicast_tree.rb_node;
156
157         while (n) {
158                 struct ipoib_mcast *mcast;
159                 int ret;
160
161                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
162
163                 ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
164                              sizeof (union ib_gid));
165                 if (ret < 0)
166                         n = n->rb_left;
167                 else if (ret > 0)
168                         n = n->rb_right;
169                 else
170                         return mcast;
171         }
172
173         return NULL;
174 }
175
176 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
177 {
178         struct ipoib_dev_priv *priv = netdev_priv(dev);
179         struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
180
181         while (*n) {
182                 struct ipoib_mcast *tmcast;
183                 int ret;
184
185                 pn = *n;
186                 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
187
188                 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
189                              sizeof (union ib_gid));
190                 if (ret < 0)
191                         n = &pn->rb_left;
192                 else if (ret > 0)
193                         n = &pn->rb_right;
194                 else
195                         return -EEXIST;
196         }
197
198         rb_link_node(&mcast->rb_node, pn, n);
199         rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
200
201         return 0;
202 }
203
204 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
205                                    struct ib_sa_mcmember_rec *mcmember)
206 {
207         struct net_device *dev = mcast->dev;
208         struct ipoib_dev_priv *priv = netdev_priv(dev);
209         int ret;
210
211         mcast->mcmember = *mcmember;
212
213         /* Set the cached Q_Key before we attach if it's the broadcast group */
214         if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
215                     sizeof (union ib_gid))) {
216                 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
217                 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
218         }
219
220         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
221                 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
222                         ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
223                                    " already attached\n",
224                                    IPOIB_GID_ARG(mcast->mcmember.mgid));
225
226                         return 0;
227                 }
228
229                 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
230                                          &mcast->mcmember.mgid);
231                 if (ret < 0) {
232                         ipoib_warn(priv, "couldn't attach QP to multicast group "
233                                    IPOIB_GID_FMT "\n",
234                                    IPOIB_GID_ARG(mcast->mcmember.mgid));
235
236                         clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
237                         return ret;
238                 }
239         }
240
241         {
242                 struct ib_ah_attr av = {
243                         .dlid          = be16_to_cpu(mcast->mcmember.mlid),
244                         .port_num      = priv->port,
245                         .sl            = mcast->mcmember.sl,
246                         .ah_flags      = IB_AH_GRH,
247                         .grh           = {
248                                 .flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
249                                 .hop_limit     = mcast->mcmember.hop_limit,
250                                 .sgid_index    = 0,
251                                 .traffic_class = mcast->mcmember.traffic_class
252                         }
253                 };
254                 int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate);
255
256                 av.grh.dgid = mcast->mcmember.mgid;
257
258                 if (path_rate > 0 && priv->local_rate > path_rate)
259                         av.static_rate = (priv->local_rate - 1) / path_rate;
260
261                 ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
262                                 av.static_rate, priv->local_rate,
263                                 ib_sa_rate_enum_to_int(mcast->mcmember.rate));
264
265                 mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
266                 if (!mcast->ah) {
267                         ipoib_warn(priv, "ib_address_create failed\n");
268                 } else {
269                         ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
270                                         " AV %p, LID 0x%04x, SL %d\n",
271                                         IPOIB_GID_ARG(mcast->mcmember.mgid),
272                                         mcast->ah->ah,
273                                         be16_to_cpu(mcast->mcmember.mlid),
274                                         mcast->mcmember.sl);
275                 }
276         }
277
278         /* actually send any queued packets */
279         while (!skb_queue_empty(&mcast->pkt_queue)) {
280                 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
281
282                 skb->dev = dev;
283
284                 if (!skb->dst || !skb->dst->neighbour) {
285                         /* put pseudoheader back on for next time */
286                         skb_push(skb, sizeof (struct ipoib_pseudoheader));
287                 }
288
289                 if (dev_queue_xmit(skb))
290                         ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
291         }
292
293         return 0;
294 }
295
296 static void
297 ipoib_mcast_sendonly_join_complete(int status,
298                                    struct ib_sa_mcmember_rec *mcmember,
299                                    void *mcast_ptr)
300 {
301         struct ipoib_mcast *mcast = mcast_ptr;
302         struct net_device *dev = mcast->dev;
303
304         if (!status)
305                 ipoib_mcast_join_finish(mcast, mcmember);
306         else {
307                 if (mcast->logcount++ < 20)
308                         ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
309                                         IPOIB_GID_FMT ", status %d\n",
310                                         IPOIB_GID_ARG(mcast->mcmember.mgid), status);
311
312                 /* Flush out any queued packets */
313                 while (!skb_queue_empty(&mcast->pkt_queue))
314                         dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
315
316                 /* Clear the busy flag so we try again */
317                 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
318         }
319
320         complete(&mcast->done);
321 }
322
323 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
324 {
325         struct net_device *dev = mcast->dev;
326         struct ipoib_dev_priv *priv = netdev_priv(dev);
327         struct ib_sa_mcmember_rec rec = {
328 #if 0                           /* Some SMs don't support send-only yet */
329                 .join_state = 4
330 #else
331                 .join_state = 1
332 #endif
333         };
334         int ret = 0;
335
336         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
337                 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
338                 return -ENODEV;
339         }
340
341         if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
342                 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
343                 return -EBUSY;
344         }
345
346         rec.mgid     = mcast->mcmember.mgid;
347         rec.port_gid = priv->local_gid;
348         rec.pkey     = cpu_to_be16(priv->pkey);
349
350         init_completion(&mcast->done);
351
352         ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
353                                      IB_SA_MCMEMBER_REC_MGID            |
354                                      IB_SA_MCMEMBER_REC_PORT_GID        |
355                                      IB_SA_MCMEMBER_REC_PKEY            |
356                                      IB_SA_MCMEMBER_REC_JOIN_STATE,
357                                      1000, GFP_ATOMIC,
358                                      ipoib_mcast_sendonly_join_complete,
359                                      mcast, &mcast->query);
360         if (ret < 0) {
361                 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
362                            ret);
363         } else {
364                 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
365                                 ", starting join\n",
366                                 IPOIB_GID_ARG(mcast->mcmember.mgid));
367
368                 mcast->query_id = ret;
369         }
370
371         return ret;
372 }
373
374 static void ipoib_mcast_join_complete(int status,
375                                       struct ib_sa_mcmember_rec *mcmember,
376                                       void *mcast_ptr)
377 {
378         struct ipoib_mcast *mcast = mcast_ptr;
379         struct net_device *dev = mcast->dev;
380         struct ipoib_dev_priv *priv = netdev_priv(dev);
381
382         ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
383                         " (status %d)\n",
384                         IPOIB_GID_ARG(mcast->mcmember.mgid), status);
385
386         if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
387                 mcast->backoff = 1;
388                 down(&mcast_mutex);
389                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
390                         queue_work(ipoib_workqueue, &priv->mcast_task);
391                 up(&mcast_mutex);
392                 complete(&mcast->done);
393                 return;
394         }
395
396         if (status == -EINTR) {
397                 complete(&mcast->done);
398                 return;
399         }
400
401         if (status && mcast->logcount++ < 20) {
402                 if (status == -ETIMEDOUT || status == -EINTR) {
403                         ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
404                                         ", status %d\n",
405                                         IPOIB_GID_ARG(mcast->mcmember.mgid),
406                                         status);
407                 } else {
408                         ipoib_warn(priv, "multicast join failed for "
409                                    IPOIB_GID_FMT ", status %d\n",
410                                    IPOIB_GID_ARG(mcast->mcmember.mgid),
411                                    status);
412                 }
413         }
414
415         mcast->backoff *= 2;
416         if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
417                 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
418
419         mcast->query = NULL;
420
421         down(&mcast_mutex);
422         if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
423                 if (status == -ETIMEDOUT)
424                         queue_work(ipoib_workqueue, &priv->mcast_task);
425                 else
426                         queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
427                                            mcast->backoff * HZ);
428         } else
429                 complete(&mcast->done);
430         up(&mcast_mutex);
431
432         return;
433 }
434
435 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
436                              int create)
437 {
438         struct ipoib_dev_priv *priv = netdev_priv(dev);
439         struct ib_sa_mcmember_rec rec = {
440                 .join_state = 1
441         };
442         ib_sa_comp_mask comp_mask;
443         int ret = 0;
444
445         ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
446                         IPOIB_GID_ARG(mcast->mcmember.mgid));
447
448         rec.mgid     = mcast->mcmember.mgid;
449         rec.port_gid = priv->local_gid;
450         rec.pkey     = cpu_to_be16(priv->pkey);
451
452         comp_mask =
453                 IB_SA_MCMEMBER_REC_MGID         |
454                 IB_SA_MCMEMBER_REC_PORT_GID     |
455                 IB_SA_MCMEMBER_REC_PKEY         |
456                 IB_SA_MCMEMBER_REC_JOIN_STATE;
457
458         if (create) {
459                 comp_mask |=
460                         IB_SA_MCMEMBER_REC_QKEY         |
461                         IB_SA_MCMEMBER_REC_SL           |
462                         IB_SA_MCMEMBER_REC_FLOW_LABEL   |
463                         IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
464
465                 rec.qkey          = priv->broadcast->mcmember.qkey;
466                 rec.sl            = priv->broadcast->mcmember.sl;
467                 rec.flow_label    = priv->broadcast->mcmember.flow_label;
468                 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
469         }
470
471         init_completion(&mcast->done);
472
473         ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
474                                      mcast->backoff * 1000, GFP_ATOMIC,
475                                      ipoib_mcast_join_complete,
476                                      mcast, &mcast->query);
477
478         if (ret < 0) {
479                 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
480
481                 mcast->backoff *= 2;
482                 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
483                         mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
484
485                 down(&mcast_mutex);
486                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
487                         queue_delayed_work(ipoib_workqueue,
488                                            &priv->mcast_task,
489                                            mcast->backoff * HZ);
490                 up(&mcast_mutex);
491         } else
492                 mcast->query_id = ret;
493 }
494
495 void ipoib_mcast_join_task(void *dev_ptr)
496 {
497         struct net_device *dev = dev_ptr;
498         struct ipoib_dev_priv *priv = netdev_priv(dev);
499
500         if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
501                 return;
502
503         if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
504                 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
505         else
506                 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
507
508         {
509                 struct ib_port_attr attr;
510
511                 if (!ib_query_port(priv->ca, priv->port, &attr)) {
512                         priv->local_lid  = attr.lid;
513                         priv->local_rate = attr.active_speed *
514                                 ib_width_enum_to_int(attr.active_width);
515                 } else
516                         ipoib_warn(priv, "ib_query_port failed\n");
517         }
518
519         if (!priv->broadcast) {
520                 priv->broadcast = ipoib_mcast_alloc(dev, 1);
521                 if (!priv->broadcast) {
522                         ipoib_warn(priv, "failed to allocate broadcast group\n");
523                         down(&mcast_mutex);
524                         if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
525                                 queue_delayed_work(ipoib_workqueue,
526                                                    &priv->mcast_task, HZ);
527                         up(&mcast_mutex);
528                         return;
529                 }
530
531                 memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
532                        sizeof (union ib_gid));
533
534                 spin_lock_irq(&priv->lock);
535                 __ipoib_mcast_add(dev, priv->broadcast);
536                 spin_unlock_irq(&priv->lock);
537         }
538
539         if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
540                 ipoib_mcast_join(dev, priv->broadcast, 0);
541                 return;
542         }
543
544         while (1) {
545                 struct ipoib_mcast *mcast = NULL;
546
547                 spin_lock_irq(&priv->lock);
548                 list_for_each_entry(mcast, &priv->multicast_list, list) {
549                         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
550                             && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
551                             && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
552                                 /* Found the next unjoined group */
553                                 break;
554                         }
555                 }
556                 spin_unlock_irq(&priv->lock);
557
558                 if (&mcast->list == &priv->multicast_list) {
559                         /* All done */
560                         break;
561                 }
562
563                 ipoib_mcast_join(dev, mcast, 1);
564                 return;
565         }
566
567         priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
568                 IPOIB_ENCAP_LEN;
569         dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
570
571         ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
572
573         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
574         netif_carrier_on(dev);
575 }
576
577 int ipoib_mcast_start_thread(struct net_device *dev)
578 {
579         struct ipoib_dev_priv *priv = netdev_priv(dev);
580
581         ipoib_dbg_mcast(priv, "starting multicast thread\n");
582
583         down(&mcast_mutex);
584         if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
585                 queue_work(ipoib_workqueue, &priv->mcast_task);
586         up(&mcast_mutex);
587
588         return 0;
589 }
590
591 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
592 {
593         struct ipoib_dev_priv *priv = netdev_priv(dev);
594         struct ipoib_mcast *mcast;
595
596         ipoib_dbg_mcast(priv, "stopping multicast thread\n");
597
598         down(&mcast_mutex);
599         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
600         cancel_delayed_work(&priv->mcast_task);
601         up(&mcast_mutex);
602
603         if (flush)
604                 flush_workqueue(ipoib_workqueue);
605
606         if (priv->broadcast && priv->broadcast->query) {
607                 ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
608                 priv->broadcast->query = NULL;
609                 ipoib_dbg_mcast(priv, "waiting for bcast\n");
610                 wait_for_completion(&priv->broadcast->done);
611         }
612
613         list_for_each_entry(mcast, &priv->multicast_list, list) {
614                 if (mcast->query) {
615                         ib_sa_cancel_query(mcast->query_id, mcast->query);
616                         mcast->query = NULL;
617                         ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
618                                         IPOIB_GID_ARG(mcast->mcmember.mgid));
619                         wait_for_completion(&mcast->done);
620                 }
621         }
622
623         return 0;
624 }
625
626 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
627 {
628         struct ipoib_dev_priv *priv = netdev_priv(dev);
629         struct ib_sa_mcmember_rec rec = {
630                 .join_state = 1
631         };
632         int ret = 0;
633
634         if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
635                 return 0;
636
637         ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
638                         IPOIB_GID_ARG(mcast->mcmember.mgid));
639
640         rec.mgid     = mcast->mcmember.mgid;
641         rec.port_gid = priv->local_gid;
642         rec.pkey     = cpu_to_be16(priv->pkey);
643
644         /* Remove ourselves from the multicast group */
645         ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
646                                  &mcast->mcmember.mgid);
647         if (ret)
648                 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
649
650         /*
651          * Just make one shot at leaving and don't wait for a reply;
652          * if we fail, too bad.
653          */
654         ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
655                                         IB_SA_MCMEMBER_REC_MGID         |
656                                         IB_SA_MCMEMBER_REC_PORT_GID     |
657                                         IB_SA_MCMEMBER_REC_PKEY         |
658                                         IB_SA_MCMEMBER_REC_JOIN_STATE,
659                                         0, GFP_ATOMIC, NULL,
660                                         mcast, &mcast->query);
661         if (ret < 0)
662                 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
663                            "for leave (result = %d)\n", ret);
664
665         return 0;
666 }
667
668 void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
669                       struct sk_buff *skb)
670 {
671         struct ipoib_dev_priv *priv = netdev_priv(dev);
672         struct ipoib_mcast *mcast;
673
674         /*
675          * We can only be called from ipoib_start_xmit, so we're
676          * inside tx_lock -- no need to save/restore flags.
677          */
678         spin_lock(&priv->lock);
679
680         mcast = __ipoib_mcast_find(dev, mgid);
681         if (!mcast) {
682                 /* Let's create a new send only group now */
683                 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
684                                 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
685
686                 mcast = ipoib_mcast_alloc(dev, 0);
687                 if (!mcast) {
688                         ipoib_warn(priv, "unable to allocate memory for "
689                                    "multicast structure\n");
690                         dev_kfree_skb_any(skb);
691                         goto out;
692                 }
693
694                 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
695                 mcast->mcmember.mgid = *mgid;
696                 __ipoib_mcast_add(dev, mcast);
697                 list_add_tail(&mcast->list, &priv->multicast_list);
698         }
699
700         if (!mcast->ah) {
701                 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
702                         skb_queue_tail(&mcast->pkt_queue, skb);
703                 else
704                         dev_kfree_skb_any(skb);
705
706                 if (mcast->query)
707                         ipoib_dbg_mcast(priv, "no address vector, "
708                                         "but multicast join already started\n");
709                 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
710                         ipoib_mcast_sendonly_join(mcast);
711
712                 /*
713                  * If lookup completes between here and out:, don't
714                  * want to send packet twice.
715                  */
716                 mcast = NULL;
717         }
718
719 out:
720         if (mcast && mcast->ah) {
721                 if (skb->dst            &&
722                     skb->dst->neighbour &&
723                     !*to_ipoib_neigh(skb->dst->neighbour)) {
724                         struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
725
726                         if (neigh) {
727                                 kref_get(&mcast->ah->ref);
728                                 neigh->ah       = mcast->ah;
729                                 neigh->neighbour = skb->dst->neighbour;
730                                 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
731                                 list_add_tail(&neigh->list, &mcast->neigh_list);
732                         }
733                 }
734
735                 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
736         }
737
738         spin_unlock(&priv->lock);
739 }
740
741 void ipoib_mcast_dev_flush(struct net_device *dev)
742 {
743         struct ipoib_dev_priv *priv = netdev_priv(dev);
744         LIST_HEAD(remove_list);
745         struct ipoib_mcast *mcast, *tmcast, *nmcast;
746         unsigned long flags;
747
748         ipoib_dbg_mcast(priv, "flushing multicast list\n");
749
750         spin_lock_irqsave(&priv->lock, flags);
751         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
752                 nmcast = ipoib_mcast_alloc(dev, 0);
753                 if (nmcast) {
754                         nmcast->flags =
755                                 mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY);
756
757                         nmcast->mcmember.mgid = mcast->mcmember.mgid;
758
759                         /* Add the new group in before the to-be-destroyed group */
760                         list_add_tail(&nmcast->list, &mcast->list);
761                         list_del_init(&mcast->list);
762
763                         rb_replace_node(&mcast->rb_node, &nmcast->rb_node,
764                                         &priv->multicast_tree);
765
766                         list_add_tail(&mcast->list, &remove_list);
767                 } else {
768                         ipoib_warn(priv, "could not reallocate multicast group "
769                                    IPOIB_GID_FMT "\n",
770                                    IPOIB_GID_ARG(mcast->mcmember.mgid));
771                 }
772         }
773
774         if (priv->broadcast) {
775                 nmcast = ipoib_mcast_alloc(dev, 0);
776                 if (nmcast) {
777                         nmcast->mcmember.mgid = priv->broadcast->mcmember.mgid;
778
779                         rb_replace_node(&priv->broadcast->rb_node,
780                                         &nmcast->rb_node,
781                                         &priv->multicast_tree);
782
783                         list_add_tail(&priv->broadcast->list, &remove_list);
784                         priv->broadcast = nmcast;
785                 } else
786                         ipoib_warn(priv, "could not reallocate broadcast group "
787                                           IPOIB_GID_FMT "\n",
788                                           IPOIB_GID_ARG(priv->broadcast->mcmember.mgid));
789         }
790
791         spin_unlock_irqrestore(&priv->lock, flags);
792
793         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
794                 ipoib_mcast_leave(dev, mcast);
795                 ipoib_mcast_free(mcast);
796         }
797 }
798
799 void ipoib_mcast_dev_down(struct net_device *dev)
800 {
801         struct ipoib_dev_priv *priv = netdev_priv(dev);
802         unsigned long flags;
803
804         /* Delete broadcast since it will be recreated */
805         if (priv->broadcast) {
806                 ipoib_dbg_mcast(priv, "deleting broadcast group\n");
807
808                 spin_lock_irqsave(&priv->lock, flags);
809                 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
810                 spin_unlock_irqrestore(&priv->lock, flags);
811                 ipoib_mcast_leave(dev, priv->broadcast);
812                 ipoib_mcast_free(priv->broadcast);
813                 priv->broadcast = NULL;
814         }
815 }
816
817 void ipoib_mcast_restart_task(void *dev_ptr)
818 {
819         struct net_device *dev = dev_ptr;
820         struct ipoib_dev_priv *priv = netdev_priv(dev);
821         struct dev_mc_list *mclist;
822         struct ipoib_mcast *mcast, *tmcast;
823         LIST_HEAD(remove_list);
824         unsigned long flags;
825
826         ipoib_dbg_mcast(priv, "restarting multicast task\n");
827
828         ipoib_mcast_stop_thread(dev, 0);
829
830         spin_lock_irqsave(&priv->lock, flags);
831
832         /*
833          * Unfortunately, the networking core only gives us a list of all of
834          * the multicast hardware addresses. We need to figure out which ones
835          * are new and which ones have been removed
836          */
837
838         /* Clear out the found flag */
839         list_for_each_entry(mcast, &priv->multicast_list, list)
840                 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
841
842         /* Mark all of the entries that are found or don't exist */
843         for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
844                 union ib_gid mgid;
845
846                 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
847
848                 /* Add in the P_Key */
849                 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
850                 mgid.raw[5] = priv->pkey & 0xff;
851
852                 mcast = __ipoib_mcast_find(dev, &mgid);
853                 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
854                         struct ipoib_mcast *nmcast;
855
856                         /* Not found or send-only group, let's add a new entry */
857                         ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
858                                         IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
859
860                         nmcast = ipoib_mcast_alloc(dev, 0);
861                         if (!nmcast) {
862                                 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
863                                 continue;
864                         }
865
866                         set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
867
868                         nmcast->mcmember.mgid = mgid;
869
870                         if (mcast) {
871                                 /* Destroy the send only entry */
872                                 list_del(&mcast->list);
873                                 list_add_tail(&mcast->list, &remove_list);
874
875                                 rb_replace_node(&mcast->rb_node,
876                                                 &nmcast->rb_node,
877                                                 &priv->multicast_tree);
878                         } else
879                                 __ipoib_mcast_add(dev, nmcast);
880
881                         list_add_tail(&nmcast->list, &priv->multicast_list);
882                 }
883
884                 if (mcast)
885                         set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
886         }
887
888         /* Remove all of the entries don't exist anymore */
889         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
890                 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
891                     !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
892                         ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
893                                         IPOIB_GID_ARG(mcast->mcmember.mgid));
894
895                         rb_erase(&mcast->rb_node, &priv->multicast_tree);
896
897                         /* Move to the remove list */
898                         list_del(&mcast->list);
899                         list_add_tail(&mcast->list, &remove_list);
900                 }
901         }
902         spin_unlock_irqrestore(&priv->lock, flags);
903
904         /* We have to cancel outside of the spinlock */
905         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
906                 ipoib_mcast_leave(mcast->dev, mcast);
907                 ipoib_mcast_free(mcast);
908         }
909
910         if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
911                 ipoib_mcast_start_thread(dev);
912 }
913
914 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
915
916 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
917 {
918         struct ipoib_mcast_iter *iter;
919
920         iter = kmalloc(sizeof *iter, GFP_KERNEL);
921         if (!iter)
922                 return NULL;
923
924         iter->dev = dev;
925         memset(iter->mgid.raw, 0, 16);
926
927         if (ipoib_mcast_iter_next(iter)) {
928                 kfree(iter);
929                 return NULL;
930         }
931
932         return iter;
933 }
934
935 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
936 {
937         struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
938         struct rb_node *n;
939         struct ipoib_mcast *mcast;
940         int ret = 1;
941
942         spin_lock_irq(&priv->lock);
943
944         n = rb_first(&priv->multicast_tree);
945
946         while (n) {
947                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
948
949                 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
950                            sizeof (union ib_gid)) < 0) {
951                         iter->mgid      = mcast->mcmember.mgid;
952                         iter->created   = mcast->created;
953                         iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
954                         iter->complete  = !!mcast->ah;
955                         iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
956
957                         ret = 0;
958
959                         break;
960                 }
961
962                 n = rb_next(n);
963         }
964
965         spin_unlock_irq(&priv->lock);
966
967         return ret;
968 }
969
970 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
971                            union ib_gid *mgid,
972                            unsigned long *created,
973                            unsigned int *queuelen,
974                            unsigned int *complete,
975                            unsigned int *send_only)
976 {
977         *mgid      = iter->mgid;
978         *created   = iter->created;
979         *queuelen  = iter->queuelen;
980         *complete  = iter->complete;
981         *send_only = iter->send_only;
982 }
983
984 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */