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