netdev: Add netdev->addr_list_lock protection.
[linux-2.6.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 struct ipoib_mcast_iter {
61         struct net_device *dev;
62         union ib_gid       mgid;
63         unsigned long      created;
64         unsigned int       queuelen;
65         unsigned int       complete;
66         unsigned int       send_only;
67 };
68
69 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
70 {
71         struct net_device *dev = mcast->dev;
72         struct ipoib_dev_priv *priv = netdev_priv(dev);
73         struct ipoib_neigh *neigh, *tmp;
74         unsigned long flags;
75         int tx_dropped = 0;
76
77         ipoib_dbg_mcast(netdev_priv(dev),
78                         "deleting multicast group " IPOIB_GID_FMT "\n",
79                         IPOIB_GID_ARG(mcast->mcmember.mgid));
80
81         spin_lock_irqsave(&priv->lock, flags);
82
83         list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
84                 /*
85                  * It's safe to call ipoib_put_ah() inside priv->lock
86                  * here, because we know that mcast->ah will always
87                  * hold one more reference, so ipoib_put_ah() will
88                  * never do more than decrement the ref count.
89                  */
90                 if (neigh->ah)
91                         ipoib_put_ah(neigh->ah);
92                 ipoib_neigh_free(dev, neigh);
93         }
94
95         spin_unlock_irqrestore(&priv->lock, flags);
96
97         if (mcast->ah)
98                 ipoib_put_ah(mcast->ah);
99
100         while (!skb_queue_empty(&mcast->pkt_queue)) {
101                 ++tx_dropped;
102                 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
103         }
104
105         spin_lock_irqsave(&priv->tx_lock, flags);
106         dev->stats.tx_dropped += tx_dropped;
107         spin_unlock_irqrestore(&priv->tx_lock, flags);
108
109         kfree(mcast);
110 }
111
112 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
113                                              int can_sleep)
114 {
115         struct ipoib_mcast *mcast;
116
117         mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
118         if (!mcast)
119                 return NULL;
120
121         mcast->dev = dev;
122         mcast->created = jiffies;
123         mcast->backoff = 1;
124
125         INIT_LIST_HEAD(&mcast->list);
126         INIT_LIST_HEAD(&mcast->neigh_list);
127         skb_queue_head_init(&mcast->pkt_queue);
128
129         return mcast;
130 }
131
132 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
133 {
134         struct ipoib_dev_priv *priv = netdev_priv(dev);
135         struct rb_node *n = priv->multicast_tree.rb_node;
136
137         while (n) {
138                 struct ipoib_mcast *mcast;
139                 int ret;
140
141                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
142
143                 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
144                              sizeof (union ib_gid));
145                 if (ret < 0)
146                         n = n->rb_left;
147                 else if (ret > 0)
148                         n = n->rb_right;
149                 else
150                         return mcast;
151         }
152
153         return NULL;
154 }
155
156 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
157 {
158         struct ipoib_dev_priv *priv = netdev_priv(dev);
159         struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
160
161         while (*n) {
162                 struct ipoib_mcast *tmcast;
163                 int ret;
164
165                 pn = *n;
166                 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
167
168                 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
169                              sizeof (union ib_gid));
170                 if (ret < 0)
171                         n = &pn->rb_left;
172                 else if (ret > 0)
173                         n = &pn->rb_right;
174                 else
175                         return -EEXIST;
176         }
177
178         rb_link_node(&mcast->rb_node, pn, n);
179         rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
180
181         return 0;
182 }
183
184 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
185                                    struct ib_sa_mcmember_rec *mcmember)
186 {
187         struct net_device *dev = mcast->dev;
188         struct ipoib_dev_priv *priv = netdev_priv(dev);
189         struct ipoib_ah *ah;
190         int ret;
191
192         mcast->mcmember = *mcmember;
193
194         /* Set the cached Q_Key before we attach if it's the broadcast group */
195         if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
196                     sizeof (union ib_gid))) {
197                 spin_lock_irq(&priv->lock);
198                 if (!priv->broadcast) {
199                         spin_unlock_irq(&priv->lock);
200                         return -EAGAIN;
201                 }
202                 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
203                 spin_unlock_irq(&priv->lock);
204                 priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
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);
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 static int ipoib_mcast_join_complete(int status,
370                                      struct ib_sa_multicast *multicast)
371 {
372         struct ipoib_mcast *mcast = multicast->context;
373         struct net_device *dev = mcast->dev;
374         struct ipoib_dev_priv *priv = netdev_priv(dev);
375
376         ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
377                         " (status %d)\n",
378                         IPOIB_GID_ARG(mcast->mcmember.mgid), status);
379
380         /* We trap for port events ourselves. */
381         if (status == -ENETRESET)
382                 return 0;
383
384         if (!status)
385                 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
386
387         if (!status) {
388                 mcast->backoff = 1;
389                 mutex_lock(&mcast_mutex);
390                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
391                         queue_delayed_work(ipoib_workqueue,
392                                            &priv->mcast_task, 0);
393                 mutex_unlock(&mcast_mutex);
394
395                 if (mcast == priv->broadcast)
396                         netif_carrier_on(dev);
397
398                 return 0;
399         }
400
401         if (mcast->logcount++ < 20) {
402                 if (status == -ETIMEDOUT) {
403                         ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
404                                         ", status %d\n",
405                                         IPOIB_GID_ARG(mcast->mcmember.mgid),
406                                         status);
407                 } else {
408                         ipoib_warn(priv, "multicast join failed for "
409                                    IPOIB_GID_FMT ", status %d\n",
410                                    IPOIB_GID_ARG(mcast->mcmember.mgid),
411                                    status);
412                 }
413         }
414
415         mcast->backoff *= 2;
416         if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
417                 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
418
419         /* Clear the busy flag so we try again */
420         status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
421
422         mutex_lock(&mcast_mutex);
423         spin_lock_irq(&priv->lock);
424         if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
425                 queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
426                                    mcast->backoff * HZ);
427         spin_unlock_irq(&priv->lock);
428         mutex_unlock(&mcast_mutex);
429
430         return status;
431 }
432
433 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
434                              int create)
435 {
436         struct ipoib_dev_priv *priv = netdev_priv(dev);
437         struct ib_sa_mcmember_rec rec = {
438                 .join_state = 1
439         };
440         ib_sa_comp_mask comp_mask;
441         int ret = 0;
442
443         ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
444                         IPOIB_GID_ARG(mcast->mcmember.mgid));
445
446         rec.mgid     = mcast->mcmember.mgid;
447         rec.port_gid = priv->local_gid;
448         rec.pkey     = cpu_to_be16(priv->pkey);
449
450         comp_mask =
451                 IB_SA_MCMEMBER_REC_MGID         |
452                 IB_SA_MCMEMBER_REC_PORT_GID     |
453                 IB_SA_MCMEMBER_REC_PKEY         |
454                 IB_SA_MCMEMBER_REC_JOIN_STATE;
455
456         if (create) {
457                 comp_mask |=
458                         IB_SA_MCMEMBER_REC_QKEY                 |
459                         IB_SA_MCMEMBER_REC_MTU_SELECTOR         |
460                         IB_SA_MCMEMBER_REC_MTU                  |
461                         IB_SA_MCMEMBER_REC_TRAFFIC_CLASS        |
462                         IB_SA_MCMEMBER_REC_RATE_SELECTOR        |
463                         IB_SA_MCMEMBER_REC_RATE                 |
464                         IB_SA_MCMEMBER_REC_SL                   |
465                         IB_SA_MCMEMBER_REC_FLOW_LABEL           |
466                         IB_SA_MCMEMBER_REC_HOP_LIMIT;
467
468                 rec.qkey          = priv->broadcast->mcmember.qkey;
469                 rec.mtu_selector  = IB_SA_EQ;
470                 rec.mtu           = priv->broadcast->mcmember.mtu;
471                 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
472                 rec.rate_selector = IB_SA_EQ;
473                 rec.rate          = priv->broadcast->mcmember.rate;
474                 rec.sl            = priv->broadcast->mcmember.sl;
475                 rec.flow_label    = priv->broadcast->mcmember.flow_label;
476                 rec.hop_limit     = priv->broadcast->mcmember.hop_limit;
477         }
478
479         set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
480         mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
481                                          &rec, comp_mask, GFP_KERNEL,
482                                          ipoib_mcast_join_complete, mcast);
483         if (IS_ERR(mcast->mc)) {
484                 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
485                 ret = PTR_ERR(mcast->mc);
486                 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
487
488                 mcast->backoff *= 2;
489                 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
490                         mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
491
492                 mutex_lock(&mcast_mutex);
493                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
494                         queue_delayed_work(ipoib_workqueue,
495                                            &priv->mcast_task,
496                                            mcast->backoff * HZ);
497                 mutex_unlock(&mcast_mutex);
498         }
499 }
500
501 void ipoib_mcast_join_task(struct work_struct *work)
502 {
503         struct ipoib_dev_priv *priv =
504                 container_of(work, struct ipoib_dev_priv, mcast_task.work);
505         struct net_device *dev = priv->dev;
506
507         if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
508                 return;
509
510         if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
511                 ipoib_warn(priv, "ib_query_gid() failed\n");
512         else
513                 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
514
515         {
516                 struct ib_port_attr attr;
517
518                 if (!ib_query_port(priv->ca, priv->port, &attr))
519                         priv->local_lid = attr.lid;
520                 else
521                         ipoib_warn(priv, "ib_query_port failed\n");
522         }
523
524         if (!priv->broadcast) {
525                 struct ipoib_mcast *broadcast;
526
527                 broadcast = ipoib_mcast_alloc(dev, 1);
528                 if (!broadcast) {
529                         ipoib_warn(priv, "failed to allocate broadcast group\n");
530                         mutex_lock(&mcast_mutex);
531                         if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
532                                 queue_delayed_work(ipoib_workqueue,
533                                                    &priv->mcast_task, HZ);
534                         mutex_unlock(&mcast_mutex);
535                         return;
536                 }
537
538                 spin_lock_irq(&priv->lock);
539                 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
540                        sizeof (union ib_gid));
541                 priv->broadcast = broadcast;
542
543                 __ipoib_mcast_add(dev, priv->broadcast);
544                 spin_unlock_irq(&priv->lock);
545         }
546
547         if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
548                 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
549                         ipoib_mcast_join(dev, priv->broadcast, 0);
550                 return;
551         }
552
553         while (1) {
554                 struct ipoib_mcast *mcast = NULL;
555
556                 spin_lock_irq(&priv->lock);
557                 list_for_each_entry(mcast, &priv->multicast_list, list) {
558                         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
559                             && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
560                             && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
561                                 /* Found the next unjoined group */
562                                 break;
563                         }
564                 }
565                 spin_unlock_irq(&priv->lock);
566
567                 if (&mcast->list == &priv->multicast_list) {
568                         /* All done */
569                         break;
570                 }
571
572                 ipoib_mcast_join(dev, mcast, 1);
573                 return;
574         }
575
576         priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
577
578         if (!ipoib_cm_admin_enabled(dev))
579                 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
580
581         ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
582
583         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
584 }
585
586 int ipoib_mcast_start_thread(struct net_device *dev)
587 {
588         struct ipoib_dev_priv *priv = netdev_priv(dev);
589
590         ipoib_dbg_mcast(priv, "starting multicast thread\n");
591
592         mutex_lock(&mcast_mutex);
593         if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
594                 queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
595         mutex_unlock(&mcast_mutex);
596
597         spin_lock_irq(&priv->lock);
598         set_bit(IPOIB_MCAST_STARTED, &priv->flags);
599         spin_unlock_irq(&priv->lock);
600
601         return 0;
602 }
603
604 int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
605 {
606         struct ipoib_dev_priv *priv = netdev_priv(dev);
607
608         ipoib_dbg_mcast(priv, "stopping multicast thread\n");
609
610         spin_lock_irq(&priv->lock);
611         clear_bit(IPOIB_MCAST_STARTED, &priv->flags);
612         spin_unlock_irq(&priv->lock);
613
614         mutex_lock(&mcast_mutex);
615         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
616         cancel_delayed_work(&priv->mcast_task);
617         mutex_unlock(&mcast_mutex);
618
619         if (flush)
620                 flush_workqueue(ipoib_workqueue);
621
622         return 0;
623 }
624
625 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
626 {
627         struct ipoib_dev_priv *priv = netdev_priv(dev);
628         int ret = 0;
629
630         if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
631                 ib_sa_free_multicast(mcast->mc);
632
633         if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
634                 ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
635                                 IPOIB_GID_ARG(mcast->mcmember.mgid));
636
637                 /* Remove ourselves from the multicast group */
638                 ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
639                                          &mcast->mcmember.mgid);
640                 if (ret)
641                         ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
642         }
643
644         return 0;
645 }
646
647 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
648 {
649         struct ipoib_dev_priv *priv = netdev_priv(dev);
650         struct ipoib_mcast *mcast;
651
652         /*
653          * We can only be called from ipoib_start_xmit, so we're
654          * inside tx_lock -- no need to save/restore flags.
655          */
656         spin_lock(&priv->lock);
657
658         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)         ||
659             !priv->broadcast                                    ||
660             !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
661                 ++dev->stats.tx_dropped;
662                 dev_kfree_skb_any(skb);
663                 goto unlock;
664         }
665
666         mcast = __ipoib_mcast_find(dev, mgid);
667         if (!mcast) {
668                 /* Let's create a new send only group now */
669                 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
670                                 IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
671
672                 mcast = ipoib_mcast_alloc(dev, 0);
673                 if (!mcast) {
674                         ipoib_warn(priv, "unable to allocate memory for "
675                                    "multicast structure\n");
676                         ++dev->stats.tx_dropped;
677                         dev_kfree_skb_any(skb);
678                         goto out;
679                 }
680
681                 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
682                 memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
683                 __ipoib_mcast_add(dev, mcast);
684                 list_add_tail(&mcast->list, &priv->multicast_list);
685         }
686
687         if (!mcast->ah) {
688                 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
689                         skb_queue_tail(&mcast->pkt_queue, skb);
690                 else {
691                         ++dev->stats.tx_dropped;
692                         dev_kfree_skb_any(skb);
693                 }
694
695                 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
696                         ipoib_dbg_mcast(priv, "no address vector, "
697                                         "but multicast join already started\n");
698                 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
699                         ipoib_mcast_sendonly_join(mcast);
700
701                 /*
702                  * If lookup completes between here and out:, don't
703                  * want to send packet twice.
704                  */
705                 mcast = NULL;
706         }
707
708 out:
709         if (mcast && mcast->ah) {
710                 if (skb->dst            &&
711                     skb->dst->neighbour &&
712                     !*to_ipoib_neigh(skb->dst->neighbour)) {
713                         struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
714                                                                         skb->dev);
715
716                         if (neigh) {
717                                 kref_get(&mcast->ah->ref);
718                                 neigh->ah       = mcast->ah;
719                                 list_add_tail(&neigh->list, &mcast->neigh_list);
720                         }
721                 }
722
723                 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
724         }
725
726 unlock:
727         spin_unlock(&priv->lock);
728 }
729
730 void ipoib_mcast_dev_flush(struct net_device *dev)
731 {
732         struct ipoib_dev_priv *priv = netdev_priv(dev);
733         LIST_HEAD(remove_list);
734         struct ipoib_mcast *mcast, *tmcast;
735         unsigned long flags;
736
737         ipoib_dbg_mcast(priv, "flushing multicast list\n");
738
739         spin_lock_irqsave(&priv->lock, flags);
740
741         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
742                 list_del(&mcast->list);
743                 rb_erase(&mcast->rb_node, &priv->multicast_tree);
744                 list_add_tail(&mcast->list, &remove_list);
745         }
746
747         if (priv->broadcast) {
748                 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
749                 list_add_tail(&priv->broadcast->list, &remove_list);
750                 priv->broadcast = NULL;
751         }
752
753         spin_unlock_irqrestore(&priv->lock, flags);
754
755         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
756                 ipoib_mcast_leave(dev, mcast);
757                 ipoib_mcast_free(mcast);
758         }
759 }
760
761 void ipoib_mcast_restart_task(struct work_struct *work)
762 {
763         struct ipoib_dev_priv *priv =
764                 container_of(work, struct ipoib_dev_priv, restart_task);
765         struct net_device *dev = priv->dev;
766         struct dev_mc_list *mclist;
767         struct ipoib_mcast *mcast, *tmcast;
768         LIST_HEAD(remove_list);
769         unsigned long flags;
770         struct ib_sa_mcmember_rec rec;
771
772         ipoib_dbg_mcast(priv, "restarting multicast task\n");
773
774         ipoib_mcast_stop_thread(dev, 0);
775
776         local_irq_save(flags);
777         netif_tx_lock(dev);
778         netif_addr_lock(dev);
779         spin_lock(&priv->lock);
780
781         /*
782          * Unfortunately, the networking core only gives us a list of all of
783          * the multicast hardware addresses. We need to figure out which ones
784          * are new and which ones have been removed
785          */
786
787         /* Clear out the found flag */
788         list_for_each_entry(mcast, &priv->multicast_list, list)
789                 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
790
791         /* Mark all of the entries that are found or don't exist */
792         for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
793                 union ib_gid mgid;
794
795                 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
796
797                 mcast = __ipoib_mcast_find(dev, &mgid);
798                 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
799                         struct ipoib_mcast *nmcast;
800
801                         /* ignore group which is directly joined by userspace */
802                         if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
803                             !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
804                                 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
805                                                 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
806                                 continue;
807                         }
808
809                         /* Not found or send-only group, let's add a new entry */
810                         ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
811                                         IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
812
813                         nmcast = ipoib_mcast_alloc(dev, 0);
814                         if (!nmcast) {
815                                 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
816                                 continue;
817                         }
818
819                         set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
820
821                         nmcast->mcmember.mgid = mgid;
822
823                         if (mcast) {
824                                 /* Destroy the send only entry */
825                                 list_move_tail(&mcast->list, &remove_list);
826
827                                 rb_replace_node(&mcast->rb_node,
828                                                 &nmcast->rb_node,
829                                                 &priv->multicast_tree);
830                         } else
831                                 __ipoib_mcast_add(dev, nmcast);
832
833                         list_add_tail(&nmcast->list, &priv->multicast_list);
834                 }
835
836                 if (mcast)
837                         set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
838         }
839
840         /* Remove all of the entries don't exist anymore */
841         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
842                 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
843                     !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
844                         ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
845                                         IPOIB_GID_ARG(mcast->mcmember.mgid));
846
847                         rb_erase(&mcast->rb_node, &priv->multicast_tree);
848
849                         /* Move to the remove list */
850                         list_move_tail(&mcast->list, &remove_list);
851                 }
852         }
853
854         spin_unlock(&priv->lock);
855         netif_addr_unlock(dev);
856         netif_tx_unlock(dev);
857         local_irq_restore(flags);
858
859         /* We have to cancel outside of the spinlock */
860         list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
861                 ipoib_mcast_leave(mcast->dev, mcast);
862                 ipoib_mcast_free(mcast);
863         }
864
865         if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
866                 ipoib_mcast_start_thread(dev);
867 }
868
869 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
870
871 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
872 {
873         struct ipoib_mcast_iter *iter;
874
875         iter = kmalloc(sizeof *iter, GFP_KERNEL);
876         if (!iter)
877                 return NULL;
878
879         iter->dev = dev;
880         memset(iter->mgid.raw, 0, 16);
881
882         if (ipoib_mcast_iter_next(iter)) {
883                 kfree(iter);
884                 return NULL;
885         }
886
887         return iter;
888 }
889
890 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
891 {
892         struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
893         struct rb_node *n;
894         struct ipoib_mcast *mcast;
895         int ret = 1;
896
897         spin_lock_irq(&priv->lock);
898
899         n = rb_first(&priv->multicast_tree);
900
901         while (n) {
902                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
903
904                 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
905                            sizeof (union ib_gid)) < 0) {
906                         iter->mgid      = mcast->mcmember.mgid;
907                         iter->created   = mcast->created;
908                         iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
909                         iter->complete  = !!mcast->ah;
910                         iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
911
912                         ret = 0;
913
914                         break;
915                 }
916
917                 n = rb_next(n);
918         }
919
920         spin_unlock_irq(&priv->lock);
921
922         return ret;
923 }
924
925 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
926                            union ib_gid *mgid,
927                            unsigned long *created,
928                            unsigned int *queuelen,
929                            unsigned int *complete,
930                            unsigned int *send_only)
931 {
932         *mgid      = iter->mgid;
933         *created   = iter->created;
934         *queuelen  = iter->queuelen;
935         *complete  = iter->complete;
936         *send_only = iter->send_only;
937 }
938
939 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */