Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-drm-fsl-dcu.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  * $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 DEFINE_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         int tx_dropped = 0;
101
102         ipoib_dbg_mcast(netdev_priv(dev),
103                         "deleting multicast group " IPOIB_GID_FMT "\n",
104                         IPOIB_GID_ARG(mcast->mcmember.mgid));
105
106         spin_lock_irqsave(&priv->lock, flags);
107
108         list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
109                 /*
110                  * It's safe to call ipoib_put_ah() inside priv->lock
111                  * here, because we know that mcast->ah will always
112                  * hold one more reference, so ipoib_put_ah() will
113                  * never do more than decrement the ref count.
114                  */
115                 if (neigh->ah)
116                         ipoib_put_ah(neigh->ah);
117                 ipoib_neigh_free(dev, neigh);
118         }
119
120         spin_unlock_irqrestore(&priv->lock, flags);
121
122         if (mcast->ah)
123                 ipoib_put_ah(mcast->ah);
124
125         while (!skb_queue_empty(&mcast->pkt_queue)) {
126                 ++tx_dropped;
127                 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
128         }
129
130         spin_lock_irqsave(&priv->tx_lock, flags);
131         priv->stats.tx_dropped += tx_dropped;
132         spin_unlock_irqrestore(&priv->tx_lock, flags);
133
134         kfree(mcast);
135 }
136
137 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
138                                              int can_sleep)
139 {
140         struct ipoib_mcast *mcast;
141
142         mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
143         if (!mcast)
144                 return NULL;
145
146         mcast->dev = dev;
147         mcast->created = jiffies;
148         mcast->backoff = 1;
149
150         INIT_LIST_HEAD(&mcast->list);
151         INIT_LIST_HEAD(&mcast->neigh_list);
152         skb_queue_head_init(&mcast->pkt_queue);
153
154         return mcast;
155 }
156
157 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
158 {
159         struct ipoib_dev_priv *priv = netdev_priv(dev);
160         struct rb_node *n = priv->multicast_tree.rb_node;
161
162         while (n) {
163                 struct ipoib_mcast *mcast;
164                 int ret;
165
166                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
167
168                 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
169                              sizeof (union ib_gid));
170                 if (ret < 0)
171                         n = n->rb_left;
172                 else if (ret > 0)
173                         n = n->rb_right;
174                 else
175                         return mcast;
176         }
177
178         return NULL;
179 }
180
181 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
182 {
183         struct ipoib_dev_priv *priv = netdev_priv(dev);
184         struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
185
186         while (*n) {
187                 struct ipoib_mcast *tmcast;
188                 int ret;
189
190                 pn = *n;
191                 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
192
193                 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
194                              sizeof (union ib_gid));
195                 if (ret < 0)
196                         n = &pn->rb_left;
197                 else if (ret > 0)
198                         n = &pn->rb_right;
199                 else
200                         return -EEXIST;
201         }
202
203         rb_link_node(&mcast->rb_node, pn, n);
204         rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
205
206         return 0;
207 }
208
209 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
210                                    struct ib_sa_mcmember_rec *mcmember)
211 {
212         struct net_device *dev = mcast->dev;
213         struct ipoib_dev_priv *priv = netdev_priv(dev);
214         struct ipoib_ah *ah;
215         int ret;
216
217         mcast->mcmember = *mcmember;
218
219         /* Set the cached Q_Key before we attach if it's the broadcast group */
220         if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
221                     sizeof (union ib_gid))) {
222                 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
223                 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
224         }
225
226         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
227                 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
228                         ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
229                                    " already attached\n",
230                                    IPOIB_GID_ARG(mcast->mcmember.mgid));
231
232                         return 0;
233                 }
234
235                 ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
236                                          &mcast->mcmember.mgid);
237                 if (ret < 0) {
238                         ipoib_warn(priv, "couldn't attach QP to multicast group "
239                                    IPOIB_GID_FMT "\n",
240                                    IPOIB_GID_ARG(mcast->mcmember.mgid));
241
242                         clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
243                         return ret;
244                 }
245         }
246
247         {
248                 struct ib_ah_attr av = {
249                         .dlid          = be16_to_cpu(mcast->mcmember.mlid),
250                         .port_num      = priv->port,
251                         .sl            = mcast->mcmember.sl,
252                         .ah_flags      = IB_AH_GRH,
253                         .static_rate   = mcast->mcmember.rate,
254                         .grh           = {
255                                 .flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
256                                 .hop_limit     = mcast->mcmember.hop_limit,
257                                 .sgid_index    = 0,
258                                 .traffic_class = mcast->mcmember.traffic_class
259                         }
260                 };
261                 av.grh.dgid = mcast->mcmember.mgid;
262
263                 ah = ipoib_create_ah(dev, priv->pd, &av);
264                 if (!ah) {
265                         ipoib_warn(priv, "ib_address_create failed\n");
266                 } else {
267                         spin_lock_irq(&priv->lock);
268                         mcast->ah = ah;
269                         spin_unlock_irq(&priv->lock);
270
271                         ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
272                                         " AV %p, LID 0x%04x, SL %d\n",
273                                         IPOIB_GID_ARG(mcast->mcmember.mgid),
274                                         mcast->ah->ah,
275                                         be16_to_cpu(mcast->mcmember.mlid),
276                                         mcast->mcmember.sl);
277                 }
278         }
279
280         /* actually send any queued packets */
281         spin_lock_irq(&priv->tx_lock);
282         while (!skb_queue_empty(&mcast->pkt_queue)) {
283                 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
284                 spin_unlock_irq(&priv->tx_lock);
285
286                 skb->dev = dev;
287
288                 if (!skb->dst || !skb->dst->neighbour) {
289                         /* put pseudoheader back on for next time */
290                         skb_push(skb, sizeof (struct ipoib_pseudoheader));
291                 }
292
293                 if (dev_queue_xmit(skb))
294                         ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
295                 spin_lock_irq(&priv->tx_lock);
296         }
297         spin_unlock_irq(&priv->tx_lock);
298
299         return 0;
300 }
301
302 static void
303 ipoib_mcast_sendonly_join_complete(int status,
304                                    struct ib_sa_mcmember_rec *mcmember,
305                                    void *mcast_ptr)
306 {
307         struct ipoib_mcast *mcast = mcast_ptr;
308         struct net_device *dev = mcast->dev;
309         struct ipoib_dev_priv *priv = netdev_priv(dev);
310
311         if (!status)
312                 ipoib_mcast_join_finish(mcast, mcmember);
313         else {
314                 if (mcast->logcount++ < 20)
315                         ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
316                                         IPOIB_GID_FMT ", status %d\n",
317                                         IPOIB_GID_ARG(mcast->mcmember.mgid), status);
318
319                 /* Flush out any queued packets */
320                 spin_lock_irq(&priv->tx_lock);
321                 while (!skb_queue_empty(&mcast->pkt_queue)) {
322                         ++priv->stats.tx_dropped;
323                         dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
324                 }
325                 spin_unlock_irq(&priv->tx_lock);
326
327                 /* Clear the busy flag so we try again */
328                 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
329                 mcast->query = NULL;
330         }
331
332         complete(&mcast->done);
333 }
334
335 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
336 {
337         struct net_device *dev = mcast->dev;
338         struct ipoib_dev_priv *priv = netdev_priv(dev);
339         struct ib_sa_mcmember_rec rec = {
340 #if 0                           /* Some SMs don't support send-only yet */
341                 .join_state = 4
342 #else
343                 .join_state = 1
344 #endif
345         };
346         int ret = 0;
347
348         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
349                 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
350                 return -ENODEV;
351         }
352
353         if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
354                 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
355                 return -EBUSY;
356         }
357
358         rec.mgid     = mcast->mcmember.mgid;
359         rec.port_gid = priv->local_gid;
360         rec.pkey     = cpu_to_be16(priv->pkey);
361
362         init_completion(&mcast->done);
363
364         ret = ib_sa_mcmember_rec_set(&ipoib_sa_client, priv->ca, priv->port, &rec,
365                                      IB_SA_MCMEMBER_REC_MGID            |
366                                      IB_SA_MCMEMBER_REC_PORT_GID        |
367                                      IB_SA_MCMEMBER_REC_PKEY            |
368                                      IB_SA_MCMEMBER_REC_JOIN_STATE,
369                                      1000, GFP_ATOMIC,
370                                      ipoib_mcast_sendonly_join_complete,
371                                      mcast, &mcast->query);
372         if (ret < 0) {
373                 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
374                            ret);
375         } else {
376                 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
377                                 ", starting join\n",
378                                 IPOIB_GID_ARG(mcast->mcmember.mgid));
379
380                 mcast->query_id = ret;
381         }
382
383         return ret;
384 }
385
386 static void ipoib_mcast_join_complete(int status,
387                                       struct ib_sa_mcmember_rec *mcmember,
388                                       void *mcast_ptr)
389 {
390         struct ipoib_mcast *mcast = mcast_ptr;
391         struct net_device *dev = mcast->dev;
392         struct ipoib_dev_priv *priv = netdev_priv(dev);
393
394         ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
395                         " (status %d)\n",
396                         IPOIB_GID_ARG(mcast->mcmember.mgid), status);
397
398         if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
399                 mcast->backoff = 1;
400                 mutex_lock(&mcast_mutex);
401                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
402                         queue_delayed_work(ipoib_workqueue,
403                                            &priv->mcast_task, 0);
404                 mutex_unlock(&mcast_mutex);
405                 complete(&mcast->done);
406                 return;
407         }
408
409         if (status == -EINTR) {
410                 complete(&mcast->done);
411                 return;
412         }
413
414         if (status && mcast->logcount++ < 20) {
415                 if (status == -ETIMEDOUT || status == -EINTR) {
416                         ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
417                                         ", status %d\n",
418                                         IPOIB_GID_ARG(mcast->mcmember.mgid),
419                                         status);
420                 } else {
421                         ipoib_warn(priv, "multicast join failed for "
422                                    IPOIB_GID_FMT ", status %d\n",
423                                    IPOIB_GID_ARG(mcast->mcmember.mgid),
424                                    status);
425                 }
426         }
427
428         mcast->backoff *= 2;
429         if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
430                 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
431
432         mutex_lock(&mcast_mutex);
433
434         spin_lock_irq(&priv->lock);
435         mcast->query = NULL;
436
437         if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
438                 if (status == -ETIMEDOUT)
439                         queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
440                                            0);
441                 else
442                         queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
443                                            mcast->backoff * HZ);
444         } else
445                 complete(&mcast->done);
446         spin_unlock_irq(&priv->lock);
447         mutex_unlock(&mcast_mutex);
448
449         return;
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         init_completion(&mcast->done);
499
500         ret = ib_sa_mcmember_rec_set(&ipoib_sa_client, priv->ca, priv->port,
501                                      &rec, comp_mask, mcast->backoff * 1000,
502                                      GFP_ATOMIC, ipoib_mcast_join_complete,
503                                      mcast, &mcast->query);
504
505         if (ret < 0) {
506                 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
507
508                 mcast->backoff *= 2;
509                 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
510                         mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
511
512                 mutex_lock(&mcast_mutex);
513                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
514                         queue_delayed_work(ipoib_workqueue,
515                                            &priv->mcast_task,
516                                            mcast->backoff * HZ);
517                 mutex_unlock(&mcast_mutex);
518         } else
519                 mcast->query_id = ret;
520 }
521
522 void ipoib_mcast_join_task(struct work_struct *work)
523 {
524         struct ipoib_dev_priv *priv =
525                 container_of(work, struct ipoib_dev_priv, mcast_task.work);
526         struct net_device *dev = priv->dev;
527
528         if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
529                 return;
530
531         if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
532                 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
533         else
534                 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
535
536         {
537                 struct ib_port_attr attr;
538
539                 if (!ib_query_port(priv->ca, priv->port, &attr)) {
540                         priv->local_lid  = attr.lid;
541                         priv->local_rate = attr.active_speed *
542                                 ib_width_enum_to_int(attr.active_width);
543                 } else
544                 ipoib_warn(priv, "ib_query_port failed\n");
545         }
546
547         if (!priv->broadcast) {
548                 struct ipoib_mcast *broadcast;
549
550                 broadcast = ipoib_mcast_alloc(dev, 1);
551                 if (!broadcast) {
552                         ipoib_warn(priv, "failed to allocate broadcast group\n");
553                         mutex_lock(&mcast_mutex);
554                         if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
555                                 queue_delayed_work(ipoib_workqueue,
556                                                    &priv->mcast_task, HZ);
557                         mutex_unlock(&mcast_mutex);
558                         return;
559                 }
560
561                 spin_lock_irq(&priv->lock);
562                 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
563                        sizeof (union ib_gid));
564                 priv->broadcast = broadcast;
565
566                 __ipoib_mcast_add(dev, priv->broadcast);
567                 spin_unlock_irq(&priv->lock);
568         }
569
570         if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
571                 ipoib_mcast_join(dev, priv->broadcast, 0);
572                 return;
573         }
574
575         while (1) {
576                 struct ipoib_mcast *mcast = NULL;
577
578                 spin_lock_irq(&priv->lock);
579                 list_for_each_entry(mcast, &priv->multicast_list, list) {
580                         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
581                             && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
582                             && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
583                                 /* Found the next unjoined group */
584                                 break;
585                         }
586                 }
587                 spin_unlock_irq(&priv->lock);
588
589                 if (&mcast->list == &priv->multicast_list) {
590                         /* All done */
591                         break;
592                 }
593
594                 ipoib_mcast_join(dev, mcast, 1);
595                 return;
596         }
597
598         priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
599                 IPOIB_ENCAP_LEN;
600         dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
601
602         ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
603
604         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
605         netif_carrier_on(dev);
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         spin_lock_irq(&priv->lock);
620         set_bit(IPOIB_MCAST_STARTED, &priv->flags);
621         spin_unlock_irq(&priv->lock);
622
623         return 0;
624 }
625
626 static void wait_for_mcast_join(struct ipoib_dev_priv *priv,
627                                 struct ipoib_mcast *mcast)
628 {
629         spin_lock_irq(&priv->lock);
630         if (mcast && mcast->query) {
631                 ib_sa_cancel_query(mcast->query_id, mcast->query);
632                 mcast->query = NULL;
633                 spin_unlock_irq(&priv->lock);
634                 ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
635                                 IPOIB_GID_ARG(mcast->mcmember.mgid));
636                 wait_for_completion(&mcast->done);
637         }
638         else
639                 spin_unlock_irq(&priv->lock);
640 }
641
642 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
643 {
644         struct ipoib_dev_priv *priv = netdev_priv(dev);
645         struct ipoib_mcast *mcast;
646
647         ipoib_dbg_mcast(priv, "stopping multicast thread\n");
648
649         spin_lock_irq(&priv->lock);
650         clear_bit(IPOIB_MCAST_STARTED, &priv->flags);
651         spin_unlock_irq(&priv->lock);
652
653         mutex_lock(&mcast_mutex);
654         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
655         cancel_delayed_work(&priv->mcast_task);
656         mutex_unlock(&mcast_mutex);
657
658         if (flush)
659                 flush_workqueue(ipoib_workqueue);
660
661         wait_for_mcast_join(priv, priv->broadcast);
662
663         list_for_each_entry(mcast, &priv->multicast_list, list)
664                 wait_for_mcast_join(priv, mcast);
665
666         return 0;
667 }
668
669 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
670 {
671         struct ipoib_dev_priv *priv = netdev_priv(dev);
672         struct ib_sa_mcmember_rec rec = {
673                 .join_state = 1
674         };
675         int ret = 0;
676
677         if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
678                 return 0;
679
680         ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
681                         IPOIB_GID_ARG(mcast->mcmember.mgid));
682
683         rec.mgid     = mcast->mcmember.mgid;
684         rec.port_gid = priv->local_gid;
685         rec.pkey     = cpu_to_be16(priv->pkey);
686
687         /* Remove ourselves from the multicast group */
688         ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
689                                  &mcast->mcmember.mgid);
690         if (ret)
691                 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
692
693         /*
694          * Just make one shot at leaving and don't wait for a reply;
695          * if we fail, too bad.
696          */
697         ret = ib_sa_mcmember_rec_delete(&ipoib_sa_client, priv->ca, priv->port, &rec,
698                                         IB_SA_MCMEMBER_REC_MGID         |
699                                         IB_SA_MCMEMBER_REC_PORT_GID     |
700                                         IB_SA_MCMEMBER_REC_PKEY         |
701                                         IB_SA_MCMEMBER_REC_JOIN_STATE,
702                                         0, GFP_ATOMIC, NULL,
703                                         mcast, &mcast->query);
704         if (ret < 0)
705                 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
706                            "for leave (result = %d)\n", ret);
707
708         return 0;
709 }
710
711 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
712 {
713         struct ipoib_dev_priv *priv = netdev_priv(dev);
714         struct ipoib_mcast *mcast;
715
716         /*
717          * We can only be called from ipoib_start_xmit, so we're
718          * inside tx_lock -- no need to save/restore flags.
719          */
720         spin_lock(&priv->lock);
721
722         if (!test_bit(IPOIB_MCAST_STARTED, &priv->flags)        ||
723             !priv->broadcast                                    ||
724             !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
725                 ++priv->stats.tx_dropped;
726                 dev_kfree_skb_any(skb);
727                 goto unlock;
728         }
729
730         mcast = __ipoib_mcast_find(dev, mgid);
731         if (!mcast) {
732                 /* Let's create a new send only group now */
733                 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
734                                 IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
735
736                 mcast = ipoib_mcast_alloc(dev, 0);
737                 if (!mcast) {
738                         ipoib_warn(priv, "unable to allocate memory for "
739                                    "multicast structure\n");
740                         ++priv->stats.tx_dropped;
741                         dev_kfree_skb_any(skb);
742                         goto out;
743                 }
744
745                 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
746                 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
747                 __ipoib_mcast_add(dev, mcast);
748                 list_add_tail(&mcast->list, &priv->multicast_list);
749         }
750
751         if (!mcast->ah) {
752                 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
753                         skb_queue_tail(&mcast->pkt_queue, skb);
754                 else {
755                         ++priv->stats.tx_dropped;
756                         dev_kfree_skb_any(skb);
757                 }
758
759                 if (mcast->query)
760                         ipoib_dbg_mcast(priv, "no address vector, "
761                                         "but multicast join already started\n");
762                 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
763                         ipoib_mcast_sendonly_join(mcast);
764
765                 /*
766                  * If lookup completes between here and out:, don't
767                  * want to send packet twice.
768                  */
769                 mcast = NULL;
770         }
771
772 out:
773         if (mcast && mcast->ah) {
774                 if (skb->dst            &&
775                     skb->dst->neighbour &&
776                     !*to_ipoib_neigh(skb->dst->neighbour)) {
777                         struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour);
778
779                         if (neigh) {
780                                 kref_get(&mcast->ah->ref);
781                                 neigh->ah       = mcast->ah;
782                                 list_add_tail(&neigh->list, &mcast->neigh_list);
783                         }
784                 }
785
786                 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
787         }
788
789 unlock:
790         spin_unlock(&priv->lock);
791 }
792
793 void ipoib_mcast_dev_flush(struct net_device *dev)
794 {
795         struct ipoib_dev_priv *priv = netdev_priv(dev);
796         LIST_HEAD(remove_list);
797         struct ipoib_mcast *mcast, *tmcast;
798         unsigned long flags;
799
800         ipoib_dbg_mcast(priv, "flushing multicast list\n");
801
802         spin_lock_irqsave(&priv->lock, flags);
803
804         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
805                 list_del(&mcast->list);
806                 rb_erase(&mcast->rb_node, &priv->multicast_tree);
807                 list_add_tail(&mcast->list, &remove_list);
808         }
809
810         if (priv->broadcast) {
811                 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
812                 list_add_tail(&priv->broadcast->list, &remove_list);
813                 priv->broadcast = NULL;
814         }
815
816         spin_unlock_irqrestore(&priv->lock, flags);
817
818         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
819                 ipoib_mcast_leave(dev, mcast);
820                 ipoib_mcast_free(mcast);
821         }
822 }
823
824 void ipoib_mcast_restart_task(struct work_struct *work)
825 {
826         struct ipoib_dev_priv *priv =
827                 container_of(work, struct ipoib_dev_priv, restart_task);
828         struct net_device *dev = priv->dev;
829         struct dev_mc_list *mclist;
830         struct ipoib_mcast *mcast, *tmcast;
831         LIST_HEAD(remove_list);
832         unsigned long flags;
833
834         ipoib_dbg_mcast(priv, "restarting multicast task\n");
835
836         ipoib_mcast_stop_thread(dev, 0);
837
838         local_irq_save(flags);
839         netif_tx_lock(dev);
840         spin_lock(&priv->lock);
841
842         /*
843          * Unfortunately, the networking core only gives us a list of all of
844          * the multicast hardware addresses. We need to figure out which ones
845          * are new and which ones have been removed
846          */
847
848         /* Clear out the found flag */
849         list_for_each_entry(mcast, &priv->multicast_list, list)
850                 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
851
852         /* Mark all of the entries that are found or don't exist */
853         for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
854                 union ib_gid mgid;
855
856                 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
857
858                 /* Add in the P_Key */
859                 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
860                 mgid.raw[5] = priv->pkey & 0xff;
861
862                 mcast = __ipoib_mcast_find(dev, &mgid);
863                 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
864                         struct ipoib_mcast *nmcast;
865
866                         /* Not found or send-only group, let's add a new entry */
867                         ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
868                                         IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
869
870                         nmcast = ipoib_mcast_alloc(dev, 0);
871                         if (!nmcast) {
872                                 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
873                                 continue;
874                         }
875
876                         set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
877
878                         nmcast->mcmember.mgid = mgid;
879
880                         if (mcast) {
881                                 /* Destroy the send only entry */
882                                 list_move_tail(&mcast->list, &remove_list);
883
884                                 rb_replace_node(&mcast->rb_node,
885                                                 &nmcast->rb_node,
886                                                 &priv->multicast_tree);
887                         } else
888                                 __ipoib_mcast_add(dev, nmcast);
889
890                         list_add_tail(&nmcast->list, &priv->multicast_list);
891                 }
892
893                 if (mcast)
894                         set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
895         }
896
897         /* Remove all of the entries don't exist anymore */
898         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
899                 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
900                     !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
901                         ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
902                                         IPOIB_GID_ARG(mcast->mcmember.mgid));
903
904                         rb_erase(&mcast->rb_node, &priv->multicast_tree);
905
906                         /* Move to the remove list */
907                         list_move_tail(&mcast->list, &remove_list);
908                 }
909         }
910
911         spin_unlock(&priv->lock);
912         netif_tx_unlock(dev);
913         local_irq_restore(flags);
914
915         /* We have to cancel outside of the spinlock */
916         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
917                 wait_for_mcast_join(priv, mcast);
918                 ipoib_mcast_leave(mcast->dev, mcast);
919                 ipoib_mcast_free(mcast);
920         }
921
922         if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
923                 ipoib_mcast_start_thread(dev);
924 }
925
926 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
927
928 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
929 {
930         struct ipoib_mcast_iter *iter;
931
932         iter = kmalloc(sizeof *iter, GFP_KERNEL);
933         if (!iter)
934                 return NULL;
935
936         iter->dev = dev;
937         memset(iter->mgid.raw, 0, 16);
938
939         if (ipoib_mcast_iter_next(iter)) {
940                 kfree(iter);
941                 return NULL;
942         }
943
944         return iter;
945 }
946
947 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
948 {
949         struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
950         struct rb_node *n;
951         struct ipoib_mcast *mcast;
952         int ret = 1;
953
954         spin_lock_irq(&priv->lock);
955
956         n = rb_first(&priv->multicast_tree);
957
958         while (n) {
959                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
960
961                 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
962                            sizeof (union ib_gid)) < 0) {
963                         iter->mgid      = mcast->mcmember.mgid;
964                         iter->created   = mcast->created;
965                         iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
966                         iter->complete  = !!mcast->ah;
967                         iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
968
969                         ret = 0;
970
971                         break;
972                 }
973
974                 n = rb_next(n);
975         }
976
977         spin_unlock_irq(&priv->lock);
978
979         return ret;
980 }
981
982 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
983                            union ib_gid *mgid,
984                            unsigned long *created,
985                            unsigned int *queuelen,
986                            unsigned int *complete,
987                            unsigned int *send_only)
988 {
989         *mgid      = iter->mgid;
990         *created   = iter->created;
991         *queuelen  = iter->queuelen;
992         *complete  = iter->complete;
993         *send_only = iter->send_only;
994 }
995
996 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */