pandora: defconfig: update
[pandora-kernel.git] / 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
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/moduleparam.h>
38 #include <linux/ip.h>
39 #include <linux/in.h>
40 #include <linux/igmp.h>
41 #include <linux/inetdevice.h>
42 #include <linux/delay.h>
43 #include <linux/completion.h>
44 #include <linux/slab.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 DEFINE_MUTEX(mcast_mutex);
59
60 struct ipoib_mcast_iter {
61         struct net_device *dev;
62         union ib_gid       mgid;
63         unsigned long      created;
64         unsigned int       queuelen;
65         unsigned int       complete;
66         unsigned int       send_only;
67 };
68
69 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
70 {
71         struct net_device *dev = mcast->dev;
72         struct ipoib_dev_priv *priv = netdev_priv(dev);
73         struct ipoib_neigh *neigh, *tmp;
74         int tx_dropped = 0;
75
76         ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
77                         mcast->mcmember.mgid.raw);
78
79         spin_lock_irq(&priv->lock);
80
81         list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
82                 /*
83                  * It's safe to call ipoib_put_ah() inside priv->lock
84                  * here, because we know that mcast->ah will always
85                  * hold one more reference, so ipoib_put_ah() will
86                  * never do more than decrement the ref count.
87                  */
88                 if (neigh->ah)
89                         ipoib_put_ah(neigh->ah);
90                 ipoib_neigh_free(dev, neigh);
91         }
92
93         spin_unlock_irq(&priv->lock);
94
95         if (mcast->ah)
96                 ipoib_put_ah(mcast->ah);
97
98         while (!skb_queue_empty(&mcast->pkt_queue)) {
99                 ++tx_dropped;
100                 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
101         }
102
103         netif_tx_lock_bh(dev);
104         dev->stats.tx_dropped += tx_dropped;
105         netif_tx_unlock_bh(dev);
106
107         kfree(mcast);
108 }
109
110 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
111                                              int can_sleep)
112 {
113         struct ipoib_mcast *mcast;
114
115         mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
116         if (!mcast)
117                 return NULL;
118
119         mcast->dev = dev;
120         mcast->created = jiffies;
121         mcast->backoff = 1;
122
123         INIT_LIST_HEAD(&mcast->list);
124         INIT_LIST_HEAD(&mcast->neigh_list);
125         skb_queue_head_init(&mcast->pkt_queue);
126
127         return mcast;
128 }
129
130 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
131 {
132         struct ipoib_dev_priv *priv = netdev_priv(dev);
133         struct rb_node *n = priv->multicast_tree.rb_node;
134
135         while (n) {
136                 struct ipoib_mcast *mcast;
137                 int ret;
138
139                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
140
141                 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
142                              sizeof (union ib_gid));
143                 if (ret < 0)
144                         n = n->rb_left;
145                 else if (ret > 0)
146                         n = n->rb_right;
147                 else
148                         return mcast;
149         }
150
151         return NULL;
152 }
153
154 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
155 {
156         struct ipoib_dev_priv *priv = netdev_priv(dev);
157         struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
158
159         while (*n) {
160                 struct ipoib_mcast *tmcast;
161                 int ret;
162
163                 pn = *n;
164                 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
165
166                 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
167                              sizeof (union ib_gid));
168                 if (ret < 0)
169                         n = &pn->rb_left;
170                 else if (ret > 0)
171                         n = &pn->rb_right;
172                 else
173                         return -EEXIST;
174         }
175
176         rb_link_node(&mcast->rb_node, pn, n);
177         rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
178
179         return 0;
180 }
181
182 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
183                                    struct ib_sa_mcmember_rec *mcmember)
184 {
185         struct net_device *dev = mcast->dev;
186         struct ipoib_dev_priv *priv = netdev_priv(dev);
187         struct ipoib_ah *ah;
188         int ret;
189         int set_qkey = 0;
190
191         mcast->mcmember = *mcmember;
192
193         /* Set the multicast MTU and cached Q_Key before we attach if it's
194          * the broadcast group.
195          */
196         if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
197                     sizeof (union ib_gid))) {
198                 spin_lock_irq(&priv->lock);
199                 if (!priv->broadcast) {
200                         spin_unlock_irq(&priv->lock);
201                         return -EAGAIN;
202                 }
203                 priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
204                 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
205                 spin_unlock_irq(&priv->lock);
206                 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
207                 set_qkey = 1;
208
209                 if (!ipoib_cm_admin_enabled(dev)) {
210                         rtnl_lock();
211                         dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
212                         rtnl_unlock();
213                 }
214         }
215
216         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
217                 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
218                         ipoib_warn(priv, "multicast group %pI6 already attached\n",
219                                    mcast->mcmember.mgid.raw);
220
221                         return 0;
222                 }
223
224                 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
225                                          &mcast->mcmember.mgid, set_qkey);
226                 if (ret < 0) {
227                         ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
228                                    mcast->mcmember.mgid.raw);
229
230                         clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
231                         return ret;
232                 }
233         }
234
235         {
236                 struct ib_ah_attr av = {
237                         .dlid          = be16_to_cpu(mcast->mcmember.mlid),
238                         .port_num      = priv->port,
239                         .sl            = mcast->mcmember.sl,
240                         .ah_flags      = IB_AH_GRH,
241                         .static_rate   = mcast->mcmember.rate,
242                         .grh           = {
243                                 .flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
244                                 .hop_limit     = mcast->mcmember.hop_limit,
245                                 .sgid_index    = 0,
246                                 .traffic_class = mcast->mcmember.traffic_class
247                         }
248                 };
249                 av.grh.dgid = mcast->mcmember.mgid;
250
251                 ah = ipoib_create_ah(dev, priv->pd, &av);
252                 if (IS_ERR(ah)) {
253                         ipoib_warn(priv, "ib_address_create failed %ld\n",
254                                 -PTR_ERR(ah));
255                         /* use original error */
256                         return PTR_ERR(ah);
257                 } else {
258                         spin_lock_irq(&priv->lock);
259                         mcast->ah = ah;
260                         spin_unlock_irq(&priv->lock);
261
262                         ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
263                                         mcast->mcmember.mgid.raw,
264                                         mcast->ah->ah,
265                                         be16_to_cpu(mcast->mcmember.mlid),
266                                         mcast->mcmember.sl);
267                 }
268         }
269
270         /* actually send any queued packets */
271         netif_tx_lock_bh(dev);
272         while (!skb_queue_empty(&mcast->pkt_queue)) {
273                 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
274
275                 netif_tx_unlock_bh(dev);
276
277                 skb->dev = dev;
278
279                 if (dev_queue_xmit(skb))
280                         ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
281
282                 netif_tx_lock_bh(dev);
283         }
284         netif_tx_unlock_bh(dev);
285
286         return 0;
287 }
288
289 static int
290 ipoib_mcast_sendonly_join_complete(int status,
291                                    struct ib_sa_multicast *multicast)
292 {
293         struct ipoib_mcast *mcast = multicast->context;
294         struct net_device *dev = mcast->dev;
295
296         /* We trap for port events ourselves. */
297         if (status == -ENETRESET)
298                 return 0;
299
300         if (!status)
301                 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
302
303         if (status) {
304                 if (mcast->logcount++ < 20)
305                         ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
306                                         mcast->mcmember.mgid.raw, status);
307
308                 /* Flush out any queued packets */
309                 netif_tx_lock_bh(dev);
310                 while (!skb_queue_empty(&mcast->pkt_queue)) {
311                         ++dev->stats.tx_dropped;
312                         dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
313                 }
314                 netif_tx_unlock_bh(dev);
315
316                 /* Clear the busy flag so we try again */
317                 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
318                                             &mcast->flags);
319         }
320         return status;
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         mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
351                                          priv->port, &rec,
352                                          IB_SA_MCMEMBER_REC_MGID        |
353                                          IB_SA_MCMEMBER_REC_PORT_GID    |
354                                          IB_SA_MCMEMBER_REC_PKEY        |
355                                          IB_SA_MCMEMBER_REC_JOIN_STATE,
356                                          GFP_ATOMIC,
357                                          ipoib_mcast_sendonly_join_complete,
358                                          mcast);
359         if (IS_ERR(mcast->mc)) {
360                 ret = PTR_ERR(mcast->mc);
361                 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
362                 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
363                            ret);
364         } else {
365                 ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
366                                 mcast->mcmember.mgid.raw);
367         }
368
369         return ret;
370 }
371
372 void ipoib_mcast_carrier_on_task(struct work_struct *work)
373 {
374         struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
375                                                    carrier_on_task);
376         struct ib_port_attr attr;
377
378         /*
379          * Take rtnl_lock to avoid racing with ipoib_stop() and
380          * turning the carrier back on while a device is being
381          * removed.
382          */
383         if (ib_query_port(priv->ca, priv->port, &attr) ||
384             attr.state != IB_PORT_ACTIVE) {
385                 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
386                 return;
387         }
388
389         rtnl_lock();
390         netif_carrier_on(priv->dev);
391         rtnl_unlock();
392 }
393
394 static int ipoib_mcast_join_complete(int status,
395                                      struct ib_sa_multicast *multicast)
396 {
397         struct ipoib_mcast *mcast = multicast->context;
398         struct net_device *dev = mcast->dev;
399         struct ipoib_dev_priv *priv = netdev_priv(dev);
400
401         ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
402                         mcast->mcmember.mgid.raw, status);
403
404         /* We trap for port events ourselves. */
405         if (status == -ENETRESET)
406                 return 0;
407
408         if (!status)
409                 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
410
411         if (!status) {
412                 mcast->backoff = 1;
413                 mutex_lock(&mcast_mutex);
414                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
415                         queue_delayed_work(ipoib_workqueue,
416                                            &priv->mcast_task, 0);
417                 mutex_unlock(&mcast_mutex);
418
419                 /*
420                  * Defer carrier on work to ipoib_workqueue to avoid a
421                  * deadlock on rtnl_lock here.
422                  */
423                 if (mcast == priv->broadcast)
424                         queue_work(ipoib_workqueue, &priv->carrier_on_task);
425
426                 return 0;
427         }
428
429         if (mcast->logcount++ < 20) {
430                 if (status == -ETIMEDOUT || status == -EAGAIN) {
431                         ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
432                                         mcast->mcmember.mgid.raw, status);
433                 } else {
434                         ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
435                                    mcast->mcmember.mgid.raw, status);
436                 }
437         }
438
439         mcast->backoff *= 2;
440         if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
441                 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
442
443         /* Clear the busy flag so we try again */
444         status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
445
446         mutex_lock(&mcast_mutex);
447         spin_lock_irq(&priv->lock);
448         if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
449                 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
450                                    mcast->backoff * HZ);
451         spin_unlock_irq(&priv->lock);
452         mutex_unlock(&mcast_mutex);
453
454         return status;
455 }
456
457 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
458                              int create)
459 {
460         struct ipoib_dev_priv *priv = netdev_priv(dev);
461         struct ib_sa_mcmember_rec rec = {
462                 .join_state = 1
463         };
464         ib_sa_comp_mask comp_mask;
465         int ret = 0;
466
467         ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
468
469         rec.mgid     = mcast->mcmember.mgid;
470         rec.port_gid = priv->local_gid;
471         rec.pkey     = cpu_to_be16(priv->pkey);
472
473         comp_mask =
474                 IB_SA_MCMEMBER_REC_MGID         |
475                 IB_SA_MCMEMBER_REC_PORT_GID     |
476                 IB_SA_MCMEMBER_REC_PKEY         |
477                 IB_SA_MCMEMBER_REC_JOIN_STATE;
478
479         if (create) {
480                 comp_mask |=
481                         IB_SA_MCMEMBER_REC_QKEY                 |
482                         IB_SA_MCMEMBER_REC_MTU_SELECTOR         |
483                         IB_SA_MCMEMBER_REC_MTU                  |
484                         IB_SA_MCMEMBER_REC_TRAFFIC_CLASS        |
485                         IB_SA_MCMEMBER_REC_RATE_SELECTOR        |
486                         IB_SA_MCMEMBER_REC_RATE                 |
487                         IB_SA_MCMEMBER_REC_SL                   |
488                         IB_SA_MCMEMBER_REC_FLOW_LABEL           |
489                         IB_SA_MCMEMBER_REC_HOP_LIMIT;
490
491                 rec.qkey          = priv->broadcast->mcmember.qkey;
492                 rec.mtu_selector  = IB_SA_EQ;
493                 rec.mtu           = priv->broadcast->mcmember.mtu;
494                 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
495                 rec.rate_selector = IB_SA_EQ;
496                 rec.rate          = priv->broadcast->mcmember.rate;
497                 rec.sl            = priv->broadcast->mcmember.sl;
498                 rec.flow_label    = priv->broadcast->mcmember.flow_label;
499                 rec.hop_limit     = priv->broadcast->mcmember.hop_limit;
500         }
501
502         set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
503         mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
504                                          &rec, comp_mask, GFP_KERNEL,
505                                          ipoib_mcast_join_complete, mcast);
506         if (IS_ERR(mcast->mc)) {
507                 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
508                 ret = PTR_ERR(mcast->mc);
509                 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
510
511                 mcast->backoff *= 2;
512                 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
513                         mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
514
515                 mutex_lock(&mcast_mutex);
516                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
517                         queue_delayed_work(ipoib_workqueue,
518                                            &priv->mcast_task,
519                                            mcast->backoff * HZ);
520                 mutex_unlock(&mcast_mutex);
521         }
522 }
523
524 void ipoib_mcast_join_task(struct work_struct *work)
525 {
526         struct ipoib_dev_priv *priv =
527                 container_of(work, struct ipoib_dev_priv, mcast_task.work);
528         struct net_device *dev = priv->dev;
529
530         if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
531                 return;
532
533         if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
534                 ipoib_warn(priv, "ib_query_gid() failed\n");
535         else
536                 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
537
538         {
539                 struct ib_port_attr attr;
540
541                 if (!ib_query_port(priv->ca, priv->port, &attr))
542                         priv->local_lid = attr.lid;
543                 else
544                         ipoib_warn(priv, "ib_query_port failed\n");
545         }
546
547         if (!priv->broadcast) {
548                 struct ipoib_mcast *broadcast;
549
550                 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
551                         return;
552
553                 broadcast = ipoib_mcast_alloc(dev, 1);
554                 if (!broadcast) {
555                         ipoib_warn(priv, "failed to allocate broadcast group\n");
556                         mutex_lock(&mcast_mutex);
557                         if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
558                                 queue_delayed_work(ipoib_workqueue,
559                                                    &priv->mcast_task, HZ);
560                         mutex_unlock(&mcast_mutex);
561                         return;
562                 }
563
564                 spin_lock_irq(&priv->lock);
565                 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
566                        sizeof (union ib_gid));
567                 priv->broadcast = broadcast;
568
569                 __ipoib_mcast_add(dev, priv->broadcast);
570                 spin_unlock_irq(&priv->lock);
571         }
572
573         if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
574                 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
575                         ipoib_mcast_join(dev, priv->broadcast, 0);
576                 return;
577         }
578
579         while (1) {
580                 struct ipoib_mcast *mcast = NULL;
581
582                 spin_lock_irq(&priv->lock);
583                 list_for_each_entry(mcast, &priv->multicast_list, list) {
584                         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
585                             && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
586                             && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
587                                 /* Found the next unjoined group */
588                                 break;
589                         }
590                 }
591                 spin_unlock_irq(&priv->lock);
592
593                 if (&mcast->list == &priv->multicast_list) {
594                         /* All done */
595                         break;
596                 }
597
598                 ipoib_mcast_join(dev, mcast, 1);
599                 return;
600         }
601
602         ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
603
604         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
605 }
606
607 int ipoib_mcast_start_thread(struct net_device *dev)
608 {
609         struct ipoib_dev_priv *priv = netdev_priv(dev);
610
611         ipoib_dbg_mcast(priv, "starting multicast thread\n");
612
613         mutex_lock(&mcast_mutex);
614         if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
615                 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
616         mutex_unlock(&mcast_mutex);
617
618         return 0;
619 }
620
621 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
622 {
623         struct ipoib_dev_priv *priv = netdev_priv(dev);
624
625         ipoib_dbg_mcast(priv, "stopping multicast thread\n");
626
627         mutex_lock(&mcast_mutex);
628         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
629         cancel_delayed_work(&priv->mcast_task);
630         mutex_unlock(&mcast_mutex);
631
632         if (flush)
633                 flush_workqueue(ipoib_workqueue);
634
635         return 0;
636 }
637
638 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
639 {
640         struct ipoib_dev_priv *priv = netdev_priv(dev);
641         int ret = 0;
642
643         if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
644                 ib_sa_free_multicast(mcast->mc);
645
646         if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
647                 ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
648                                 mcast->mcmember.mgid.raw);
649
650                 /* Remove ourselves from the multicast group */
651                 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
652                                       be16_to_cpu(mcast->mcmember.mlid));
653                 if (ret)
654                         ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
655         }
656
657         return 0;
658 }
659
660 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
661 {
662         struct ipoib_dev_priv *priv = netdev_priv(dev);
663         struct ipoib_mcast *mcast;
664         unsigned long flags;
665
666         spin_lock_irqsave(&priv->lock, flags);
667
668         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)         ||
669             !priv->broadcast                                    ||
670             !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
671                 ++dev->stats.tx_dropped;
672                 dev_kfree_skb_any(skb);
673                 goto unlock;
674         }
675
676         mcast = __ipoib_mcast_find(dev, mgid);
677         if (!mcast) {
678                 /* Let's create a new send only group now */
679                 ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
680                                 mgid);
681
682                 mcast = ipoib_mcast_alloc(dev, 0);
683                 if (!mcast) {
684                         ipoib_warn(priv, "unable to allocate memory for "
685                                    "multicast structure\n");
686                         ++dev->stats.tx_dropped;
687                         dev_kfree_skb_any(skb);
688                         goto out;
689                 }
690
691                 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
692                 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
693                 __ipoib_mcast_add(dev, mcast);
694                 list_add_tail(&mcast->list, &priv->multicast_list);
695         }
696
697         if (!mcast->ah) {
698                 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
699                         skb_queue_tail(&mcast->pkt_queue, skb);
700                 else {
701                         ++dev->stats.tx_dropped;
702                         dev_kfree_skb_any(skb);
703                 }
704
705                 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
706                         ipoib_dbg_mcast(priv, "no address vector, "
707                                         "but multicast join already started\n");
708                 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
709                         ipoib_mcast_sendonly_join(mcast);
710
711                 /*
712                  * If lookup completes between here and out:, don't
713                  * want to send packet twice.
714                  */
715                 mcast = NULL;
716         }
717
718 out:
719         if (mcast && mcast->ah) {
720                 struct dst_entry *dst = skb_dst(skb);
721                 struct neighbour *n = NULL;
722
723                 rcu_read_lock();
724                 if (dst)
725                         n = dst_get_neighbour(dst);
726                 if (n && !*to_ipoib_neigh(n)) {
727                         struct ipoib_neigh *neigh = ipoib_neigh_alloc(n,
728                                                                       skb->dev);
729
730                         if (neigh) {
731                                 kref_get(&mcast->ah->ref);
732                                 neigh->ah       = mcast->ah;
733                                 list_add_tail(&neigh->list, &mcast->neigh_list);
734                         }
735                 }
736                 rcu_read_unlock();
737                 spin_unlock_irqrestore(&priv->lock, flags);
738                 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
739                 return;
740         }
741
742 unlock:
743         spin_unlock_irqrestore(&priv->lock, flags);
744 }
745
746 void ipoib_mcast_dev_flush(struct net_device *dev)
747 {
748         struct ipoib_dev_priv *priv = netdev_priv(dev);
749         LIST_HEAD(remove_list);
750         struct ipoib_mcast *mcast, *tmcast;
751         unsigned long flags;
752
753         ipoib_dbg_mcast(priv, "flushing multicast list\n");
754
755         spin_lock_irqsave(&priv->lock, flags);
756
757         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
758                 list_del(&mcast->list);
759                 rb_erase(&mcast->rb_node, &priv->multicast_tree);
760                 list_add_tail(&mcast->list, &remove_list);
761         }
762
763         if (priv->broadcast) {
764                 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
765                 list_add_tail(&priv->broadcast->list, &remove_list);
766                 priv->broadcast = NULL;
767         }
768
769         spin_unlock_irqrestore(&priv->lock, flags);
770
771         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
772                 ipoib_mcast_leave(dev, mcast);
773                 ipoib_mcast_free(mcast);
774         }
775 }
776
777 static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
778 {
779         /* reserved QPN, prefix, scope */
780         if (memcmp(addr, broadcast, 6))
781                 return 0;
782         /* signature lower, pkey */
783         if (memcmp(addr + 7, broadcast + 7, 3))
784                 return 0;
785         return 1;
786 }
787
788 void ipoib_mcast_restart_task(struct work_struct *work)
789 {
790         struct ipoib_dev_priv *priv =
791                 container_of(work, struct ipoib_dev_priv, restart_task);
792         struct net_device *dev = priv->dev;
793         struct netdev_hw_addr *ha;
794         struct ipoib_mcast *mcast, *tmcast;
795         LIST_HEAD(remove_list);
796         unsigned long flags;
797         struct ib_sa_mcmember_rec rec;
798
799         ipoib_dbg_mcast(priv, "restarting multicast task\n");
800
801         ipoib_mcast_stop_thread(dev, 0);
802
803         local_irq_save(flags);
804         netif_addr_lock(dev);
805         spin_lock(&priv->lock);
806
807         /*
808          * Unfortunately, the networking core only gives us a list of all of
809          * the multicast hardware addresses. We need to figure out which ones
810          * are new and which ones have been removed
811          */
812
813         /* Clear out the found flag */
814         list_for_each_entry(mcast, &priv->multicast_list, list)
815                 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
816
817         /* Mark all of the entries that are found or don't exist */
818         netdev_for_each_mc_addr(ha, dev) {
819                 union ib_gid mgid;
820
821                 if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
822                         continue;
823
824                 memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
825
826                 mcast = __ipoib_mcast_find(dev, &mgid);
827                 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
828                         struct ipoib_mcast *nmcast;
829
830                         /* ignore group which is directly joined by userspace */
831                         if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
832                             !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
833                                 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
834                                                 mgid.raw);
835                                 continue;
836                         }
837
838                         /* Not found or send-only group, let's add a new entry */
839                         ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
840                                         mgid.raw);
841
842                         nmcast = ipoib_mcast_alloc(dev, 0);
843                         if (!nmcast) {
844                                 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
845                                 continue;
846                         }
847
848                         set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
849
850                         nmcast->mcmember.mgid = mgid;
851
852                         if (mcast) {
853                                 /* Destroy the send only entry */
854                                 list_move_tail(&mcast->list, &remove_list);
855
856                                 rb_replace_node(&mcast->rb_node,
857                                                 &nmcast->rb_node,
858                                                 &priv->multicast_tree);
859                         } else
860                                 __ipoib_mcast_add(dev, nmcast);
861
862                         list_add_tail(&nmcast->list, &priv->multicast_list);
863                 }
864
865                 if (mcast)
866                         set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
867         }
868
869         /* Remove all of the entries don't exist anymore */
870         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
871                 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
872                     !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
873                         ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
874                                         mcast->mcmember.mgid.raw);
875
876                         rb_erase(&mcast->rb_node, &priv->multicast_tree);
877
878                         /* Move to the remove list */
879                         list_move_tail(&mcast->list, &remove_list);
880                 }
881         }
882
883         spin_unlock(&priv->lock);
884         netif_addr_unlock(dev);
885         local_irq_restore(flags);
886
887         /* We have to cancel outside of the spinlock */
888         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
889                 ipoib_mcast_leave(mcast->dev, mcast);
890                 ipoib_mcast_free(mcast);
891         }
892
893         if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
894                 ipoib_mcast_start_thread(dev);
895 }
896
897 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
898
899 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
900 {
901         struct ipoib_mcast_iter *iter;
902
903         iter = kmalloc(sizeof *iter, GFP_KERNEL);
904         if (!iter)
905                 return NULL;
906
907         iter->dev = dev;
908         memset(iter->mgid.raw, 0, 16);
909
910         if (ipoib_mcast_iter_next(iter)) {
911                 kfree(iter);
912                 return NULL;
913         }
914
915         return iter;
916 }
917
918 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
919 {
920         struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
921         struct rb_node *n;
922         struct ipoib_mcast *mcast;
923         int ret = 1;
924
925         spin_lock_irq(&priv->lock);
926
927         n = rb_first(&priv->multicast_tree);
928
929         while (n) {
930                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
931
932                 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
933                            sizeof (union ib_gid)) < 0) {
934                         iter->mgid      = mcast->mcmember.mgid;
935                         iter->created   = mcast->created;
936                         iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
937                         iter->complete  = !!mcast->ah;
938                         iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
939
940                         ret = 0;
941
942                         break;
943                 }
944
945                 n = rb_next(n);
946         }
947
948         spin_unlock_irq(&priv->lock);
949
950         return ret;
951 }
952
953 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
954                            union ib_gid *mgid,
955                            unsigned long *created,
956                            unsigned int *queuelen,
957                            unsigned int *complete,
958                            unsigned int *send_only)
959 {
960         *mgid      = iter->mgid;
961         *created   = iter->created;
962         *queuelen  = iter->queuelen;
963         *complete  = iter->complete;
964         *send_only = iter->send_only;
965 }
966
967 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */