This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / infiniband / ulp / ipoib / ipoib_multicast.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $
33  */
34
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/ip.h>
38 #include <linux/in.h>
39 #include <linux/igmp.h>
40 #include <linux/inetdevice.h>
41 #include <linux/delay.h>
42 #include <linux/completion.h>
43
44 #include "ipoib.h"
45
46 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
47 static int mcast_debug_level;
48
49 module_param(mcast_debug_level, int, 0644);
50 MODULE_PARM_DESC(mcast_debug_level,
51                  "Enable multicast debug tracing if > 0");
52 #endif
53
54 static DECLARE_MUTEX(mcast_mutex);
55
56 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
57 struct ipoib_mcast {
58         struct ib_sa_mcmember_rec mcmember;
59         struct ipoib_ah          *ah;
60
61         struct rb_node    rb_node;
62         struct list_head  list;
63         struct completion done;
64
65         int                 query_id;
66         struct ib_sa_query *query;
67
68         unsigned long created;
69         unsigned long backoff;
70
71         unsigned long flags;
72         unsigned char logcount;
73
74         struct list_head  neigh_list;
75
76         struct sk_buff_head pkt_queue;
77
78         struct net_device *dev;
79 };
80
81 struct ipoib_mcast_iter {
82         struct net_device *dev;
83         union ib_gid       mgid;
84         unsigned long      created;
85         unsigned int       queuelen;
86         unsigned int       complete;
87         unsigned int       send_only;
88 };
89
90 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
91 {
92         struct net_device *dev = mcast->dev;
93         struct ipoib_dev_priv *priv = netdev_priv(dev);
94         struct ipoib_neigh *neigh, *tmp;
95         unsigned long flags;
96
97         ipoib_dbg_mcast(netdev_priv(dev),
98                         "deleting multicast group " IPOIB_GID_FMT "\n",
99                         IPOIB_GID_ARG(mcast->mcmember.mgid));
100
101         spin_lock_irqsave(&priv->lock, flags);
102
103         list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
104                 ipoib_put_ah(neigh->ah);
105                 *to_ipoib_neigh(neigh->neighbour) = NULL;
106                 neigh->neighbour->ops->destructor = NULL;
107                 kfree(neigh);
108         }
109
110         spin_unlock_irqrestore(&priv->lock, flags);
111
112         if (mcast->ah)
113                 ipoib_put_ah(mcast->ah);
114
115         while (!skb_queue_empty(&mcast->pkt_queue)) {
116                 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
117
118                 skb->dev = dev;
119                 dev_kfree_skb_any(skb);
120         }
121
122         kfree(mcast);
123 }
124
125 static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
126                                              int can_sleep)
127 {
128         struct ipoib_mcast *mcast;
129
130         mcast = kmalloc(sizeof (*mcast), can_sleep ? GFP_KERNEL : GFP_ATOMIC);
131         if (!mcast)
132                 return NULL;
133
134         memset(mcast, 0, sizeof (*mcast));
135
136         init_completion(&mcast->done);
137
138         mcast->dev = dev;
139         mcast->created = jiffies;
140         mcast->backoff = HZ;
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
256                 av.grh.dgid = mcast->mcmember.mgid;
257
258                 if (ib_sa_rate_enum_to_int(mcast->mcmember.rate) > 0)
259                         av.static_rate = (2 * priv->local_rate -
260                                           ib_sa_rate_enum_to_int(mcast->mcmember.rate) - 1) /
261                                 (priv->local_rate ? priv->local_rate : 1);
262
263                 ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
264                                 av.static_rate, priv->local_rate,
265                                 ib_sa_rate_enum_to_int(mcast->mcmember.rate));
266
267                 mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
268                 if (!mcast->ah) {
269                         ipoib_warn(priv, "ib_address_create failed\n");
270                 } else {
271                         ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
272                                         " AV %p, LID 0x%04x, SL %d\n",
273                                         IPOIB_GID_ARG(mcast->mcmember.mgid),
274                                         mcast->ah->ah,
275                                         be16_to_cpu(mcast->mcmember.mlid),
276                                         mcast->mcmember.sl);
277                 }
278         }
279
280         /* actually send any queued packets */
281         while (!skb_queue_empty(&mcast->pkt_queue)) {
282                 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
283
284                 skb->dev = dev;
285
286                 if (!skb->dst || !skb->dst->neighbour) {
287                         /* put pseudoheader back on for next time */
288                         skb_push(skb, sizeof (struct ipoib_pseudoheader));
289                 }
290
291                 if (dev_queue_xmit(skb))
292                         ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
293         }
294
295         return 0;
296 }
297
298 static void
299 ipoib_mcast_sendonly_join_complete(int status,
300                                    struct ib_sa_mcmember_rec *mcmember,
301                                    void *mcast_ptr)
302 {
303         struct ipoib_mcast *mcast = mcast_ptr;
304         struct net_device *dev = mcast->dev;
305
306         if (!status)
307                 ipoib_mcast_join_finish(mcast, mcmember);
308         else {
309                 if (mcast->logcount++ < 20)
310                         ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
311                                         IPOIB_GID_FMT ", status %d\n",
312                                         IPOIB_GID_ARG(mcast->mcmember.mgid), status);
313
314                 /* Flush out any queued packets */
315                 while (!skb_queue_empty(&mcast->pkt_queue)) {
316                         struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
317
318                         skb->dev = dev;
319
320                         dev_kfree_skb_any(skb);
321                 }
322
323                 /* Clear the busy flag so we try again */
324                 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
325         }
326
327         complete(&mcast->done);
328 }
329
330 static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
331 {
332         struct net_device *dev = mcast->dev;
333         struct ipoib_dev_priv *priv = netdev_priv(dev);
334         struct ib_sa_mcmember_rec rec = {
335 #if 0                           /* Some SMs don't support send-only yet */
336                 .join_state = 4
337 #else
338                 .join_state = 1
339 #endif
340         };
341         int ret = 0;
342
343         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
344                 ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
345                 return -ENODEV;
346         }
347
348         if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
349                 ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
350                 return -EBUSY;
351         }
352
353         rec.mgid     = mcast->mcmember.mgid;
354         rec.port_gid = priv->local_gid;
355         rec.pkey     = be16_to_cpu(priv->pkey);
356
357         ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
358                                      IB_SA_MCMEMBER_REC_MGID            |
359                                      IB_SA_MCMEMBER_REC_PORT_GID        |
360                                      IB_SA_MCMEMBER_REC_PKEY            |
361                                      IB_SA_MCMEMBER_REC_JOIN_STATE,
362                                      1000, GFP_ATOMIC,
363                                      ipoib_mcast_sendonly_join_complete,
364                                      mcast, &mcast->query);
365         if (ret < 0) {
366                 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
367                            ret);
368         } else {
369                 ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
370                                 ", starting join\n",
371                                 IPOIB_GID_ARG(mcast->mcmember.mgid));
372
373                 mcast->query_id = ret;
374         }
375
376         return ret;
377 }
378
379 static void ipoib_mcast_join_complete(int status,
380                                       struct ib_sa_mcmember_rec *mcmember,
381                                       void *mcast_ptr)
382 {
383         struct ipoib_mcast *mcast = mcast_ptr;
384         struct net_device *dev = mcast->dev;
385         struct ipoib_dev_priv *priv = netdev_priv(dev);
386
387         ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
388                         " (status %d)\n",
389                         IPOIB_GID_ARG(mcast->mcmember.mgid), status);
390
391         if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
392                 mcast->backoff = HZ;
393                 down(&mcast_mutex);
394                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
395                         queue_work(ipoib_workqueue, &priv->mcast_task);
396                 up(&mcast_mutex);
397                 complete(&mcast->done);
398                 return;
399         }
400
401         if (status == -EINTR) {
402                 complete(&mcast->done);
403                 return;
404         }
405
406         if (status && mcast->logcount++ < 20) {
407                 if (status == -ETIMEDOUT || status == -EINTR) {
408                         ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
409                                         ", status %d\n",
410                                         IPOIB_GID_ARG(mcast->mcmember.mgid),
411                                         status);
412                 } else {
413                         ipoib_warn(priv, "multicast join failed for "
414                                    IPOIB_GID_FMT ", status %d\n",
415                                    IPOIB_GID_ARG(mcast->mcmember.mgid),
416                                    status);
417                 }
418         }
419
420         mcast->backoff *= 2;
421         if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
422                 mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
423
424         mcast->query = NULL;
425
426         down(&mcast_mutex);
427         if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
428                 if (status == -ETIMEDOUT)
429                         queue_work(ipoib_workqueue, &priv->mcast_task);
430                 else
431                         queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
432                                            mcast->backoff * HZ);
433         } else
434                 complete(&mcast->done);
435         up(&mcast_mutex);
436
437         return;
438 }
439
440 static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
441                              int create)
442 {
443         struct ipoib_dev_priv *priv = netdev_priv(dev);
444         struct ib_sa_mcmember_rec rec = {
445                 .join_state = 1
446         };
447         ib_sa_comp_mask comp_mask;
448         int ret = 0;
449
450         ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
451                         IPOIB_GID_ARG(mcast->mcmember.mgid));
452
453         rec.mgid     = mcast->mcmember.mgid;
454         rec.port_gid = priv->local_gid;
455         rec.pkey     = be16_to_cpu(priv->pkey);
456
457         comp_mask =
458                 IB_SA_MCMEMBER_REC_MGID         |
459                 IB_SA_MCMEMBER_REC_PORT_GID     |
460                 IB_SA_MCMEMBER_REC_PKEY         |
461                 IB_SA_MCMEMBER_REC_JOIN_STATE;
462
463         if (create) {
464                 comp_mask |=
465                         IB_SA_MCMEMBER_REC_QKEY         |
466                         IB_SA_MCMEMBER_REC_SL           |
467                         IB_SA_MCMEMBER_REC_FLOW_LABEL   |
468                         IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
469
470                 rec.qkey          = priv->broadcast->mcmember.qkey;
471                 rec.sl            = priv->broadcast->mcmember.sl;
472                 rec.flow_label    = priv->broadcast->mcmember.flow_label;
473                 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
474         }
475
476         ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
477                                      mcast->backoff * 1000, GFP_ATOMIC,
478                                      ipoib_mcast_join_complete,
479                                      mcast, &mcast->query);
480
481         if (ret < 0) {
482                 ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
483
484                 mcast->backoff *= 2;
485                 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
486                         mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
487
488                 down(&mcast_mutex);
489                 if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
490                         queue_delayed_work(ipoib_workqueue,
491                                            &priv->mcast_task,
492                                            mcast->backoff);
493                 up(&mcast_mutex);
494         } else
495                 mcast->query_id = ret;
496 }
497
498 void ipoib_mcast_join_task(void *dev_ptr)
499 {
500         struct net_device *dev = dev_ptr;
501         struct ipoib_dev_priv *priv = netdev_priv(dev);
502
503         if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
504                 return;
505
506         if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
507                 ipoib_warn(priv, "ib_gid_entry_get() failed\n");
508         else
509                 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
510
511         {
512                 struct ib_port_attr attr;
513
514                 if (!ib_query_port(priv->ca, priv->port, &attr)) {
515                         priv->local_lid  = attr.lid;
516                         priv->local_rate = attr.active_speed *
517                                 ib_width_enum_to_int(attr.active_width);
518                 } else
519                         ipoib_warn(priv, "ib_query_port failed\n");
520         }
521
522         if (!priv->broadcast) {
523                 priv->broadcast = ipoib_mcast_alloc(dev, 1);
524                 if (!priv->broadcast) {
525                         ipoib_warn(priv, "failed to allocate broadcast group\n");
526                         down(&mcast_mutex);
527                         if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
528                                 queue_delayed_work(ipoib_workqueue,
529                                                    &priv->mcast_task, HZ);
530                         up(&mcast_mutex);
531                         return;
532                 }
533
534                 memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
535                        sizeof (union ib_gid));
536
537                 spin_lock_irq(&priv->lock);
538                 __ipoib_mcast_add(dev, priv->broadcast);
539                 spin_unlock_irq(&priv->lock);
540         }
541
542         if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
543                 ipoib_mcast_join(dev, priv->broadcast, 0);
544                 return;
545         }
546
547         while (1) {
548                 struct ipoib_mcast *mcast = NULL;
549
550                 spin_lock_irq(&priv->lock);
551                 list_for_each_entry(mcast, &priv->multicast_list, list) {
552                         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
553                             && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
554                             && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
555                                 /* Found the next unjoined group */
556                                 break;
557                         }
558                 }
559                 spin_unlock_irq(&priv->lock);
560
561                 if (&mcast->list == &priv->multicast_list) {
562                         /* All done */
563                         break;
564                 }
565
566                 ipoib_mcast_join(dev, mcast, 1);
567                 return;
568         }
569
570         priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
571                 IPOIB_ENCAP_LEN;
572         dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
573
574         ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
575
576         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
577         netif_carrier_on(dev);
578 }
579
580 int ipoib_mcast_start_thread(struct net_device *dev)
581 {
582         struct ipoib_dev_priv *priv = netdev_priv(dev);
583
584         ipoib_dbg_mcast(priv, "starting multicast thread\n");
585
586         down(&mcast_mutex);
587         if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
588                 queue_work(ipoib_workqueue, &priv->mcast_task);
589         up(&mcast_mutex);
590
591         return 0;
592 }
593
594 int ipoib_mcast_stop_thread(struct net_device *dev)
595 {
596         struct ipoib_dev_priv *priv = netdev_priv(dev);
597         struct ipoib_mcast *mcast;
598
599         ipoib_dbg_mcast(priv, "stopping multicast thread\n");
600
601         down(&mcast_mutex);
602         clear_bit(IPOIB_MCAST_RUN, &priv->flags);
603         cancel_delayed_work(&priv->mcast_task);
604         up(&mcast_mutex);
605
606         flush_workqueue(ipoib_workqueue);
607
608         if (priv->broadcast && priv->broadcast->query) {
609                 ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
610                 priv->broadcast->query = NULL;
611                 ipoib_dbg_mcast(priv, "waiting for bcast\n");
612                 wait_for_completion(&priv->broadcast->done);
613         }
614
615         list_for_each_entry(mcast, &priv->multicast_list, list) {
616                 if (mcast->query) {
617                         ib_sa_cancel_query(mcast->query_id, mcast->query);
618                         mcast->query = NULL;
619                         ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
620                                         IPOIB_GID_ARG(mcast->mcmember.mgid));
621                         wait_for_completion(&mcast->done);
622                 }
623         }
624
625         return 0;
626 }
627
628 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
629 {
630         struct ipoib_dev_priv *priv = netdev_priv(dev);
631         struct ib_sa_mcmember_rec rec = {
632                 .join_state = 1
633         };
634         int ret = 0;
635
636         if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
637                 return 0;
638
639         ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
640                         IPOIB_GID_ARG(mcast->mcmember.mgid));
641
642         rec.mgid     = mcast->mcmember.mgid;
643         rec.port_gid = priv->local_gid;
644         rec.pkey     = be16_to_cpu(priv->pkey);
645
646         /* Remove ourselves from the multicast group */
647         ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
648                                  &mcast->mcmember.mgid);
649         if (ret)
650                 ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
651
652         /*
653          * Just make one shot at leaving and don't wait for a reply;
654          * if we fail, too bad.
655          */
656         ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
657                                         IB_SA_MCMEMBER_REC_MGID         |
658                                         IB_SA_MCMEMBER_REC_PORT_GID     |
659                                         IB_SA_MCMEMBER_REC_PKEY         |
660                                         IB_SA_MCMEMBER_REC_JOIN_STATE,
661                                         0, GFP_ATOMIC, NULL,
662                                         mcast, &mcast->query);
663         if (ret < 0)
664                 ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
665                            "for leave (result = %d)\n", ret);
666
667         return 0;
668 }
669
670 void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
671                       struct sk_buff *skb)
672 {
673         struct ipoib_dev_priv *priv = netdev_priv(dev);
674         struct ipoib_mcast *mcast;
675
676         /*
677          * We can only be called from ipoib_start_xmit, so we're
678          * inside tx_lock -- no need to save/restore flags.
679          */
680         spin_lock(&priv->lock);
681
682         mcast = __ipoib_mcast_find(dev, mgid);
683         if (!mcast) {
684                 /* Let's create a new send only group now */
685                 ipoib_dbg_mcast(priv, "setting up send only multicast group for "
686                                 IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
687
688                 mcast = ipoib_mcast_alloc(dev, 0);
689                 if (!mcast) {
690                         ipoib_warn(priv, "unable to allocate memory for "
691                                    "multicast structure\n");
692                         dev_kfree_skb_any(skb);
693                         goto out;
694                 }
695
696                 set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
697                 mcast->mcmember.mgid = *mgid;
698                 __ipoib_mcast_add(dev, mcast);
699                 list_add_tail(&mcast->list, &priv->multicast_list);
700         }
701
702         if (!mcast->ah) {
703                 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
704                         skb_queue_tail(&mcast->pkt_queue, skb);
705                 else
706                         dev_kfree_skb_any(skb);
707
708                 if (mcast->query)
709                         ipoib_dbg_mcast(priv, "no address vector, "
710                                         "but multicast join already started\n");
711                 else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
712                         ipoib_mcast_sendonly_join(mcast);
713
714                 /*
715                  * If lookup completes between here and out:, don't
716                  * want to send packet twice.
717                  */
718                 mcast = NULL;
719         }
720
721 out:
722         if (mcast && mcast->ah) {
723                 if (skb->dst            &&
724                     skb->dst->neighbour &&
725                     !*to_ipoib_neigh(skb->dst->neighbour)) {
726                         struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
727
728                         if (neigh) {
729                                 kref_get(&mcast->ah->ref);
730                                 neigh->ah       = mcast->ah;
731                                 neigh->neighbour = skb->dst->neighbour;
732                                 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
733                                 list_add_tail(&neigh->list, &mcast->neigh_list);
734                         }
735                 }
736
737                 ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
738         }
739
740         spin_unlock(&priv->lock);
741 }
742
743 void ipoib_mcast_dev_flush(struct net_device *dev)
744 {
745         struct ipoib_dev_priv *priv = netdev_priv(dev);
746         LIST_HEAD(remove_list);
747         struct ipoib_mcast *mcast, *tmcast, *nmcast;
748         unsigned long flags;
749
750         ipoib_dbg_mcast(priv, "flushing multicast list\n");
751
752         spin_lock_irqsave(&priv->lock, flags);
753         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
754                 nmcast = ipoib_mcast_alloc(dev, 0);
755                 if (nmcast) {
756                         nmcast->flags =
757                                 mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY);
758
759                         nmcast->mcmember.mgid = mcast->mcmember.mgid;
760
761                         /* Add the new group in before the to-be-destroyed group */
762                         list_add_tail(&nmcast->list, &mcast->list);
763                         list_del_init(&mcast->list);
764
765                         rb_replace_node(&mcast->rb_node, &nmcast->rb_node,
766                                         &priv->multicast_tree);
767
768                         list_add_tail(&mcast->list, &remove_list);
769                 } else {
770                         ipoib_warn(priv, "could not reallocate multicast group "
771                                    IPOIB_GID_FMT "\n",
772                                    IPOIB_GID_ARG(mcast->mcmember.mgid));
773                 }
774         }
775
776         if (priv->broadcast) {
777                 nmcast = ipoib_mcast_alloc(dev, 0);
778                 if (nmcast) {
779                         nmcast->mcmember.mgid = priv->broadcast->mcmember.mgid;
780
781                         rb_replace_node(&priv->broadcast->rb_node,
782                                         &nmcast->rb_node,
783                                         &priv->multicast_tree);
784
785                         list_add_tail(&priv->broadcast->list, &remove_list);
786                 }
787
788                 priv->broadcast = nmcast;
789         }
790
791         spin_unlock_irqrestore(&priv->lock, flags);
792
793         list_for_each_entry(mcast, &remove_list, list) {
794                 ipoib_mcast_leave(dev, mcast);
795                 ipoib_mcast_free(mcast);
796         }
797 }
798
799 void ipoib_mcast_dev_down(struct net_device *dev)
800 {
801         struct ipoib_dev_priv *priv = netdev_priv(dev);
802         unsigned long flags;
803
804         /* Delete broadcast since it will be recreated */
805         if (priv->broadcast) {
806                 ipoib_dbg_mcast(priv, "deleting broadcast group\n");
807
808                 spin_lock_irqsave(&priv->lock, flags);
809                 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
810                 spin_unlock_irqrestore(&priv->lock, flags);
811                 ipoib_mcast_leave(dev, priv->broadcast);
812                 ipoib_mcast_free(priv->broadcast);
813                 priv->broadcast = NULL;
814         }
815 }
816
817 void ipoib_mcast_restart_task(void *dev_ptr)
818 {
819         struct net_device *dev = dev_ptr;
820         struct ipoib_dev_priv *priv = netdev_priv(dev);
821         struct dev_mc_list *mclist;
822         struct ipoib_mcast *mcast, *tmcast;
823         LIST_HEAD(remove_list);
824         unsigned long flags;
825
826         ipoib_dbg_mcast(priv, "restarting multicast task\n");
827
828         ipoib_mcast_stop_thread(dev);
829
830         spin_lock_irqsave(&priv->lock, flags);
831
832         /*
833          * Unfortunately, the networking core only gives us a list of all of
834          * the multicast hardware addresses. We need to figure out which ones
835          * are new and which ones have been removed
836          */
837
838         /* Clear out the found flag */
839         list_for_each_entry(mcast, &priv->multicast_list, list)
840                 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
841
842         /* Mark all of the entries that are found or don't exist */
843         for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
844                 union ib_gid mgid;
845
846                 memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
847
848                 /* Add in the P_Key */
849                 mgid.raw[4] = (priv->pkey >> 8) & 0xff;
850                 mgid.raw[5] = priv->pkey & 0xff;
851
852                 mcast = __ipoib_mcast_find(dev, &mgid);
853                 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
854                         struct ipoib_mcast *nmcast;
855
856                         /* Not found or send-only group, let's add a new entry */
857                         ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
858                                         IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
859
860                         nmcast = ipoib_mcast_alloc(dev, 0);
861                         if (!nmcast) {
862                                 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
863                                 continue;
864                         }
865
866                         set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
867
868                         nmcast->mcmember.mgid = mgid;
869
870                         if (mcast) {
871                                 /* Destroy the send only entry */
872                                 list_del(&mcast->list);
873                                 list_add_tail(&mcast->list, &remove_list);
874
875                                 rb_replace_node(&mcast->rb_node,
876                                                 &nmcast->rb_node,
877                                                 &priv->multicast_tree);
878                         } else
879                                 __ipoib_mcast_add(dev, nmcast);
880
881                         list_add_tail(&nmcast->list, &priv->multicast_list);
882                 }
883
884                 if (mcast)
885                         set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
886         }
887
888         /* Remove all of the entries don't exist anymore */
889         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
890                 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
891                     !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
892                         ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
893                                         IPOIB_GID_ARG(mcast->mcmember.mgid));
894
895                         rb_erase(&mcast->rb_node, &priv->multicast_tree);
896
897                         /* Move to the remove list */
898                         list_del(&mcast->list);
899                         list_add_tail(&mcast->list, &remove_list);
900                 }
901         }
902         spin_unlock_irqrestore(&priv->lock, flags);
903
904         /* We have to cancel outside of the spinlock */
905         list_for_each_entry(mcast, &remove_list, list) {
906                 ipoib_mcast_leave(mcast->dev, mcast);
907                 ipoib_mcast_free(mcast);
908         }
909
910         if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
911                 ipoib_mcast_start_thread(dev);
912 }
913
914 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
915 {
916         struct ipoib_mcast_iter *iter;
917
918         iter = kmalloc(sizeof *iter, GFP_KERNEL);
919         if (!iter)
920                 return NULL;
921
922         iter->dev = dev;
923         memset(iter->mgid.raw, 0, sizeof iter->mgid);
924
925         if (ipoib_mcast_iter_next(iter)) {
926                 ipoib_mcast_iter_free(iter);
927                 return NULL;
928         }
929
930         return iter;
931 }
932
933 void ipoib_mcast_iter_free(struct ipoib_mcast_iter *iter)
934 {
935         kfree(iter);
936 }
937
938 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
939 {
940         struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
941         struct rb_node *n;
942         struct ipoib_mcast *mcast;
943         int ret = 1;
944
945         spin_lock_irq(&priv->lock);
946
947         n = rb_first(&priv->multicast_tree);
948
949         while (n) {
950                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
951
952                 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
953                            sizeof (union ib_gid)) < 0) {
954                         iter->mgid      = mcast->mcmember.mgid;
955                         iter->created   = mcast->created;
956                         iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
957                         iter->complete  = !!mcast->ah;
958                         iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
959
960                         ret = 0;
961
962                         break;
963                 }
964
965                 n = rb_next(n);
966         }
967
968         spin_unlock_irq(&priv->lock);
969
970         return ret;
971 }
972
973 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
974                            union ib_gid *mgid,
975                            unsigned long *created,
976                            unsigned int *queuelen,
977                            unsigned int *complete,
978                            unsigned int *send_only)
979 {
980         *mgid      = iter->mgid;
981         *created   = iter->created;
982         *queuelen  = iter->queuelen;
983         *complete  = iter->complete;
984         *send_only = iter->send_only;
985 }