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