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