Merge branch 'master' into next
[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
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/ip.h>
38 #include <linux/in.h>
39 #include <linux/igmp.h>
40 #include <linux/inetdevice.h>
41 #include <linux/delay.h>
42 #include <linux/completion.h>
43
44 #include <net/dst.h>
45
46 #include "ipoib.h"
47
48 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
49 static int mcast_debug_level;
50
51 module_param(mcast_debug_level, int, 0644);
52 MODULE_PARM_DESC(mcast_debug_level,
53                  "Enable multicast debug tracing if > 0");
54 #endif
55
56 static DEFINE_MUTEX(mcast_mutex);
57
58 struct ipoib_mcast_iter {
59         struct net_device *dev;
60         union ib_gid       mgid;
61         unsigned long      created;
62         unsigned int       queuelen;
63         unsigned int       complete;
64         unsigned int       send_only;
65 };
66
67 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
68 {
69         struct net_device *dev = mcast->dev;
70         struct ipoib_dev_priv *priv = netdev_priv(dev);
71         struct ipoib_neigh *neigh, *tmp;
72         unsigned long flags;
73         int tx_dropped = 0;
74
75         ipoib_dbg_mcast(netdev_priv(dev),
76                         "deleting multicast group " IPOIB_GID_FMT "\n",
77                         IPOIB_GID_ARG(mcast->mcmember.mgid));
78
79         spin_lock_irqsave(&priv->lock, flags);
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_irqrestore(&priv->lock, flags);
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         spin_lock_irqsave(&priv->tx_lock, flags);
104         dev->stats.tx_dropped += tx_dropped;
105         spin_unlock_irqrestore(&priv->tx_lock, flags);
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 cached Q_Key before we attach if it's the broadcast group */
194         if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
195                     sizeof (union ib_gid))) {
196                 spin_lock_irq(&priv->lock);
197                 if (!priv->broadcast) {
198                         spin_unlock_irq(&priv->lock);
199                         return -EAGAIN;
200                 }
201                 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
202                 spin_unlock_irq(&priv->lock);
203                 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
204                 set_qkey = 1;
205         }
206
207         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
208                 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
209                         ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
210                                    " already attached\n",
211                                    IPOIB_GID_ARG(mcast->mcmember.mgid));
212
213                         return 0;
214                 }
215
216                 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
217                                          &mcast->mcmember.mgid, set_qkey);
218                 if (ret < 0) {
219                         ipoib_warn(priv, "couldn't attach QP to multicast group "
220                                    IPOIB_GID_FMT "\n",
221                                    IPOIB_GID_ARG(mcast->mcmember.mgid));
222
223                         clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
224                         return ret;
225                 }
226         }
227
228         {
229                 struct ib_ah_attr av = {
230                         .dlid          = be16_to_cpu(mcast->mcmember.mlid),
231                         .port_num      = priv->port,
232                         .sl            = mcast->mcmember.sl,
233                         .ah_flags      = IB_AH_GRH,
234                         .static_rate   = mcast->mcmember.rate,
235                         .grh           = {
236                                 .flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
237                                 .hop_limit     = mcast->mcmember.hop_limit,
238                                 .sgid_index    = 0,
239                                 .traffic_class = mcast->mcmember.traffic_class
240                         }
241                 };
242                 av.grh.dgid = mcast->mcmember.mgid;
243
244                 ah = ipoib_create_ah(dev, priv->pd, &av);
245                 if (!ah) {
246                         ipoib_warn(priv, "ib_address_create failed\n");
247                 } else {
248                         spin_lock_irq(&priv->lock);
249                         mcast->ah = ah;
250                         spin_unlock_irq(&priv->lock);
251
252                         ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
253                                         " AV %p, LID 0x%04x, SL %d\n",
254                                         IPOIB_GID_ARG(mcast->mcmember.mgid),
255                                         mcast->ah->ah,
256                                         be16_to_cpu(mcast->mcmember.mlid),
257                                         mcast->mcmember.sl);
258                 }
259         }
260
261         /* actually send any queued packets */
262         spin_lock_irq(&priv->tx_lock);
263         while (!skb_queue_empty(&mcast->pkt_queue)) {
264                 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
265                 spin_unlock_irq(&priv->tx_lock);
266
267                 skb->dev = dev;
268
269                 if (!skb->dst || !skb->dst->neighbour) {
270                         /* put pseudoheader back on for next time */
271                         skb_push(skb, sizeof (struct ipoib_pseudoheader));
272                 }
273
274                 if (dev_queue_xmit(skb))
275                         ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
276                 spin_lock_irq(&priv->tx_lock);
277         }
278         spin_unlock_irq(&priv->tx_lock);
279
280         return 0;
281 }
282
283 static int
284 ipoib_mcast_sendonly_join_complete(int status,
285                                    struct ib_sa_multicast *multicast)
286 {
287         struct ipoib_mcast *mcast = multicast->context;
288         struct net_device *dev = mcast->dev;
289         struct ipoib_dev_priv *priv = netdev_priv(dev);
290
291         /* We trap for port events ourselves. */
292         if (status == -ENETRESET)
293                 return 0;
294
295         if (!status)
296                 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
297
298         if (status) {
299                 if (mcast->logcount++ < 20)
300                         ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
301                                         IPOIB_GID_FMT ", status %d\n",
302                                         IPOIB_GID_ARG(mcast->mcmember.mgid), status);
303
304                 /* Flush out any queued packets */
305                 spin_lock_irq(&priv->tx_lock);
306                 while (!skb_queue_empty(&mcast->pkt_queue)) {
307                         ++dev->stats.tx_dropped;
308                         dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
309                 }
310                 spin_unlock_irq(&priv->tx_lock);
311
312                 /* Clear the busy flag so we try again */
313                 status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
314                                             &mcast->flags);
315         }
316         return status;
317 }
318
319 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
320 {
321         struct net_device *dev = mcast->dev;
322         struct ipoib_dev_priv *priv = netdev_priv(dev);
323         struct ib_sa_mcmember_rec rec = {
324 #if 0                           /* Some SMs don't support send-only yet */
325                 .join_state = 4
326 #else
327                 .join_state = 1
328 #endif
329         };
330         int ret = 0;
331
332         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
333                 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
334                 return -ENODEV;
335         }
336
337         if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
338                 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
339                 return -EBUSY;
340         }
341
342         rec.mgid     = mcast->mcmember.mgid;
343         rec.port_gid = priv->local_gid;
344         rec.pkey     = cpu_to_be16(priv->pkey);
345
346         mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
347                                          priv->port, &rec,
348                                          IB_SA_MCMEMBER_REC_MGID        |
349                                          IB_SA_MCMEMBER_REC_PORT_GID    |
350                                          IB_SA_MCMEMBER_REC_PKEY        |
351                                          IB_SA_MCMEMBER_REC_JOIN_STATE,
352                                          GFP_ATOMIC,
353                                          ipoib_mcast_sendonly_join_complete,
354                                          mcast);
355         if (IS_ERR(mcast->mc)) {
356                 ret = PTR_ERR(mcast->mc);
357                 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
358                 ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
359                            ret);
360         } else {
361                 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
362                                 ", starting join\n",
363                                 IPOIB_GID_ARG(mcast->mcmember.mgid));
364         }
365
366         return ret;
367 }
368
369 void ipoib_mcast_carrier_on_task(struct work_struct *work)
370 {
371         struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
372                                                    carrier_on_task);
373
374         /*
375          * Take rtnl_lock to avoid racing with ipoib_stop() and
376          * turning the carrier back on while a device is being
377          * removed.
378          */
379         rtnl_lock();
380         netif_carrier_on(priv->dev);
381         rtnl_unlock();
382 }
383
384 static int ipoib_mcast_join_complete(int status,
385                                      struct ib_sa_multicast *multicast)
386 {
387         struct ipoib_mcast *mcast = multicast->context;
388         struct net_device *dev = mcast->dev;
389         struct ipoib_dev_priv *priv = netdev_priv(dev);
390
391         ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
392                         " (status %d)\n",
393                         IPOIB_GID_ARG(mcast->mcmember.mgid), status);
394
395         /* We trap for port events ourselves. */
396         if (status == -ENETRESET)
397                 return 0;
398
399         if (!status)
400                 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
401
402         if (!status) {
403                 mcast->backoff = 1;
404                 mutex_lock(&mcast_mutex);
405                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
406                         queue_delayed_work(ipoib_workqueue,
407                                            &priv->mcast_task, 0);
408                 mutex_unlock(&mcast_mutex);
409
410                 /*
411                  * Defer carrier on work to ipoib_workqueue to avoid a
412                  * deadlock on rtnl_lock here.
413                  */
414                 if (mcast == priv->broadcast)
415                         queue_work(ipoib_workqueue, &priv->carrier_on_task);
416
417                 return 0;
418         }
419
420         if (mcast->logcount++ < 20) {
421                 if (status == -ETIMEDOUT) {
422                         ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
423                                         ", status %d\n",
424                                         IPOIB_GID_ARG(mcast->mcmember.mgid),
425                                         status);
426                 } else {
427                         ipoib_warn(priv, "multicast join failed for "
428                                    IPOIB_GID_FMT ", status %d\n",
429                                    IPOIB_GID_ARG(mcast->mcmember.mgid),
430                                    status);
431                 }
432         }
433
434         mcast->backoff *= 2;
435         if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
436                 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
437
438         /* Clear the busy flag so we try again */
439         status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
440
441         mutex_lock(&mcast_mutex);
442         spin_lock_irq(&priv->lock);
443         if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
444                 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
445                                    mcast->backoff * HZ);
446         spin_unlock_irq(&priv->lock);
447         mutex_unlock(&mcast_mutex);
448
449         return status;
450 }
451
452 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
453                              int create)
454 {
455         struct ipoib_dev_priv *priv = netdev_priv(dev);
456         struct ib_sa_mcmember_rec rec = {
457                 .join_state = 1
458         };
459         ib_sa_comp_mask comp_mask;
460         int ret = 0;
461
462         ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
463                         IPOIB_GID_ARG(mcast->mcmember.mgid));
464
465         rec.mgid     = mcast->mcmember.mgid;
466         rec.port_gid = priv->local_gid;
467         rec.pkey     = cpu_to_be16(priv->pkey);
468
469         comp_mask =
470                 IB_SA_MCMEMBER_REC_MGID         |
471                 IB_SA_MCMEMBER_REC_PORT_GID     |
472                 IB_SA_MCMEMBER_REC_PKEY         |
473                 IB_SA_MCMEMBER_REC_JOIN_STATE;
474
475         if (create) {
476                 comp_mask |=
477                         IB_SA_MCMEMBER_REC_QKEY                 |
478                         IB_SA_MCMEMBER_REC_MTU_SELECTOR         |
479                         IB_SA_MCMEMBER_REC_MTU                  |
480                         IB_SA_MCMEMBER_REC_TRAFFIC_CLASS        |
481                         IB_SA_MCMEMBER_REC_RATE_SELECTOR        |
482                         IB_SA_MCMEMBER_REC_RATE                 |
483                         IB_SA_MCMEMBER_REC_SL                   |
484                         IB_SA_MCMEMBER_REC_FLOW_LABEL           |
485                         IB_SA_MCMEMBER_REC_HOP_LIMIT;
486
487                 rec.qkey          = priv->broadcast->mcmember.qkey;
488                 rec.mtu_selector  = IB_SA_EQ;
489                 rec.mtu           = priv->broadcast->mcmember.mtu;
490                 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
491                 rec.rate_selector = IB_SA_EQ;
492                 rec.rate          = priv->broadcast->mcmember.rate;
493                 rec.sl            = priv->broadcast->mcmember.sl;
494                 rec.flow_label    = priv->broadcast->mcmember.flow_label;
495                 rec.hop_limit     = priv->broadcast->mcmember.hop_limit;
496         }
497
498         set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
499         mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
500                                          &rec, comp_mask, GFP_KERNEL,
501                                          ipoib_mcast_join_complete, mcast);
502         if (IS_ERR(mcast->mc)) {
503                 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
504                 ret = PTR_ERR(mcast->mc);
505                 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
506
507                 mcast->backoff *= 2;
508                 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
509                         mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
510
511                 mutex_lock(&mcast_mutex);
512                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
513                         queue_delayed_work(ipoib_workqueue,
514                                            &priv->mcast_task,
515                                            mcast->backoff * HZ);
516                 mutex_unlock(&mcast_mutex);
517         }
518 }
519
520 void ipoib_mcast_join_task(struct work_struct *work)
521 {
522         struct ipoib_dev_priv *priv =
523                 container_of(work, struct ipoib_dev_priv, mcast_task.work);
524         struct net_device *dev = priv->dev;
525
526         if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
527                 return;
528
529         if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
530                 ipoib_warn(priv, "ib_query_gid() failed\n");
531         else
532                 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
533
534         {
535                 struct ib_port_attr attr;
536
537                 if (!ib_query_port(priv->ca, priv->port, &attr))
538                         priv->local_lid = attr.lid;
539                 else
540                         ipoib_warn(priv, "ib_query_port failed\n");
541         }
542
543         if (!priv->broadcast) {
544                 struct ipoib_mcast *broadcast;
545
546                 broadcast = ipoib_mcast_alloc(dev, 1);
547                 if (!broadcast) {
548                         ipoib_warn(priv, "failed to allocate broadcast group\n");
549                         mutex_lock(&mcast_mutex);
550                         if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
551                                 queue_delayed_work(ipoib_workqueue,
552                                                    &priv->mcast_task, HZ);
553                         mutex_unlock(&mcast_mutex);
554                         return;
555                 }
556
557                 spin_lock_irq(&priv->lock);
558                 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
559                        sizeof (union ib_gid));
560                 priv->broadcast = broadcast;
561
562                 __ipoib_mcast_add(dev, priv->broadcast);
563                 spin_unlock_irq(&priv->lock);
564         }
565
566         if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
567                 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
568                         ipoib_mcast_join(dev, priv->broadcast, 0);
569                 return;
570         }
571
572         while (1) {
573                 struct ipoib_mcast *mcast = NULL;
574
575                 spin_lock_irq(&priv->lock);
576                 list_for_each_entry(mcast, &priv->multicast_list, list) {
577                         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
578                             && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
579                             && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
580                                 /* Found the next unjoined group */
581                                 break;
582                         }
583                 }
584                 spin_unlock_irq(&priv->lock);
585
586                 if (&mcast->list == &priv->multicast_list) {
587                         /* All done */
588                         break;
589                 }
590
591                 ipoib_mcast_join(dev, mcast, 1);
592                 return;
593         }
594
595         priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
596
597         if (!ipoib_cm_admin_enabled(dev)) {
598                 rtnl_lock();
599                 dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
600                 rtnl_unlock();
601         }
602
603         ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
604
605         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
606 }
607
608 int ipoib_mcast_start_thread(struct net_device *dev)
609 {
610         struct ipoib_dev_priv *priv = netdev_priv(dev);
611
612         ipoib_dbg_mcast(priv, "starting multicast thread\n");
613
614         mutex_lock(&mcast_mutex);
615         if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
616                 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
617         mutex_unlock(&mcast_mutex);
618
619         return 0;
620 }
621
622 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
623 {
624         struct ipoib_dev_priv *priv = netdev_priv(dev);
625
626         ipoib_dbg_mcast(priv, "stopping multicast thread\n");
627
628         mutex_lock(&mcast_mutex);
629         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
630         cancel_delayed_work(&priv->mcast_task);
631         mutex_unlock(&mcast_mutex);
632
633         if (flush)
634                 flush_workqueue(ipoib_workqueue);
635
636         return 0;
637 }
638
639 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
640 {
641         struct ipoib_dev_priv *priv = netdev_priv(dev);
642         int ret = 0;
643
644         if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
645                 ib_sa_free_multicast(mcast->mc);
646
647         if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
648                 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
649                                 IPOIB_GID_ARG(mcast->mcmember.mgid));
650
651                 /* Remove ourselves from the multicast group */
652                 ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
653                                       be16_to_cpu(mcast->mcmember.mlid));
654                 if (ret)
655                         ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
656         }
657
658         return 0;
659 }
660
661 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
662 {
663         struct ipoib_dev_priv *priv = netdev_priv(dev);
664         struct ipoib_mcast *mcast;
665
666         /*
667          * We can only be called from ipoib_start_xmit, so we're
668          * inside tx_lock -- no need to save/restore flags.
669          */
670         spin_lock(&priv->lock);
671
672         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)         ||
673             !priv->broadcast                                    ||
674             !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
675                 ++dev->stats.tx_dropped;
676                 dev_kfree_skb_any(skb);
677                 goto unlock;
678         }
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_RAW_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->stats.tx_dropped;
691                         dev_kfree_skb_any(skb);
692                         goto out;
693                 }
694
695                 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
696                 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
697                 __ipoib_mcast_add(dev, mcast);
698                 list_add_tail(&mcast->list, &priv->multicast_list);
699         }
700
701         if (!mcast->ah) {
702                 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
703                         skb_queue_tail(&mcast->pkt_queue, skb);
704                 else {
705                         ++dev->stats.tx_dropped;
706                         dev_kfree_skb_any(skb);
707                 }
708
709                 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
710                         ipoib_dbg_mcast(priv, "no address vector, "
711                                         "but multicast join already started\n");
712                 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
713                         ipoib_mcast_sendonly_join(mcast);
714
715                 /*
716                  * If lookup completes between here and out:, don't
717                  * want to send packet twice.
718                  */
719                 mcast = NULL;
720         }
721
722 out:
723         if (mcast && mcast->ah) {
724                 if (skb->dst            &&
725                     skb->dst->neighbour &&
726                     !*to_ipoib_neigh(skb->dst->neighbour)) {
727                         struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
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
737                 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
738         }
739
740 unlock:
741         spin_unlock(&priv->lock);
742 }
743
744 void ipoib_mcast_dev_flush(struct net_device *dev)
745 {
746         struct ipoib_dev_priv *priv = netdev_priv(dev);
747         LIST_HEAD(remove_list);
748         struct ipoib_mcast *mcast, *tmcast;
749         unsigned long flags;
750
751         ipoib_dbg_mcast(priv, "flushing multicast list\n");
752
753         spin_lock_irqsave(&priv->lock, flags);
754
755         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
756                 list_del(&mcast->list);
757                 rb_erase(&mcast->rb_node, &priv->multicast_tree);
758                 list_add_tail(&mcast->list, &remove_list);
759         }
760
761         if (priv->broadcast) {
762                 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
763                 list_add_tail(&priv->broadcast->list, &remove_list);
764                 priv->broadcast = NULL;
765         }
766
767         spin_unlock_irqrestore(&priv->lock, flags);
768
769         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
770                 ipoib_mcast_leave(dev, mcast);
771                 ipoib_mcast_free(mcast);
772         }
773 }
774
775 void ipoib_mcast_restart_task(struct work_struct *work)
776 {
777         struct ipoib_dev_priv *priv =
778                 container_of(work, struct ipoib_dev_priv, restart_task);
779         struct net_device *dev = priv->dev;
780         struct dev_mc_list *mclist;
781         struct ipoib_mcast *mcast, *tmcast;
782         LIST_HEAD(remove_list);
783         unsigned long flags;
784         struct ib_sa_mcmember_rec rec;
785
786         ipoib_dbg_mcast(priv, "restarting multicast task\n");
787
788         ipoib_mcast_stop_thread(dev, 0);
789
790         local_irq_save(flags);
791         netif_addr_lock(dev);
792         spin_lock(&priv->lock);
793
794         /*
795          * Unfortunately, the networking core only gives us a list of all of
796          * the multicast hardware addresses. We need to figure out which ones
797          * are new and which ones have been removed
798          */
799
800         /* Clear out the found flag */
801         list_for_each_entry(mcast, &priv->multicast_list, list)
802                 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
803
804         /* Mark all of the entries that are found or don't exist */
805         for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
806                 union ib_gid mgid;
807
808                 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
809
810                 mcast = __ipoib_mcast_find(dev, &mgid);
811                 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
812                         struct ipoib_mcast *nmcast;
813
814                         /* ignore group which is directly joined by userspace */
815                         if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
816                             !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
817                                 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
818                                                 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
819                                 continue;
820                         }
821
822                         /* Not found or send-only group, let's add a new entry */
823                         ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
824                                         IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
825
826                         nmcast = ipoib_mcast_alloc(dev, 0);
827                         if (!nmcast) {
828                                 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
829                                 continue;
830                         }
831
832                         set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
833
834                         nmcast->mcmember.mgid = mgid;
835
836                         if (mcast) {
837                                 /* Destroy the send only entry */
838                                 list_move_tail(&mcast->list, &remove_list);
839
840                                 rb_replace_node(&mcast->rb_node,
841                                                 &nmcast->rb_node,
842                                                 &priv->multicast_tree);
843                         } else
844                                 __ipoib_mcast_add(dev, nmcast);
845
846                         list_add_tail(&nmcast->list, &priv->multicast_list);
847                 }
848
849                 if (mcast)
850                         set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
851         }
852
853         /* Remove all of the entries don't exist anymore */
854         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
855                 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
856                     !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
857                         ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
858                                         IPOIB_GID_ARG(mcast->mcmember.mgid));
859
860                         rb_erase(&mcast->rb_node, &priv->multicast_tree);
861
862                         /* Move to the remove list */
863                         list_move_tail(&mcast->list, &remove_list);
864                 }
865         }
866
867         spin_unlock(&priv->lock);
868         netif_addr_unlock(dev);
869         local_irq_restore(flags);
870
871         /* We have to cancel outside of the spinlock */
872         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
873                 ipoib_mcast_leave(mcast->dev, mcast);
874                 ipoib_mcast_free(mcast);
875         }
876
877         if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
878                 ipoib_mcast_start_thread(dev);
879 }
880
881 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
882
883 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
884 {
885         struct ipoib_mcast_iter *iter;
886
887         iter = kmalloc(sizeof *iter, GFP_KERNEL);
888         if (!iter)
889                 return NULL;
890
891         iter->dev = dev;
892         memset(iter->mgid.raw, 0, 16);
893
894         if (ipoib_mcast_iter_next(iter)) {
895                 kfree(iter);
896                 return NULL;
897         }
898
899         return iter;
900 }
901
902 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
903 {
904         struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
905         struct rb_node *n;
906         struct ipoib_mcast *mcast;
907         int ret = 1;
908
909         spin_lock_irq(&priv->lock);
910
911         n = rb_first(&priv->multicast_tree);
912
913         while (n) {
914                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
915
916                 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
917                            sizeof (union ib_gid)) < 0) {
918                         iter->mgid      = mcast->mcmember.mgid;
919                         iter->created   = mcast->created;
920                         iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
921                         iter->complete  = !!mcast->ah;
922                         iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
923
924                         ret = 0;
925
926                         break;
927                 }
928
929                 n = rb_next(n);
930         }
931
932         spin_unlock_irq(&priv->lock);
933
934         return ret;
935 }
936
937 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
938                            union ib_gid *mgid,
939                            unsigned long *created,
940                            unsigned int *queuelen,
941                            unsigned int *complete,
942                            unsigned int *send_only)
943 {
944         *mgid      = iter->mgid;
945         *created   = iter->created;
946         *queuelen  = iter->queuelen;
947         *complete  = iter->complete;
948         *send_only = iter->send_only;
949 }
950
951 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */