IB/IPoIB: Support acceleration options callbacks
[sfrench/cifs-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
35 #include <linux/skbuff.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/moduleparam.h>
38 #include <linux/ip.h>
39 #include <linux/in.h>
40 #include <linux/igmp.h>
41 #include <linux/inetdevice.h>
42 #include <linux/delay.h>
43 #include <linux/completion.h>
44 #include <linux/slab.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 struct ipoib_mcast_iter {
59         struct net_device *dev;
60         union ib_gid       mgid;
61         unsigned long      created;
62         unsigned int       queuelen;
63         unsigned int       complete;
64         unsigned int       send_only;
65 };
66
67 /* join state that allows creating mcg with sendonly member request */
68 #define SENDONLY_FULLMEMBER_JOIN        8
69
70 /*
71  * This should be called with the priv->lock held
72  */
73 static void __ipoib_mcast_schedule_join_thread(struct ipoib_dev_priv *priv,
74                                                struct ipoib_mcast *mcast,
75                                                bool delay)
76 {
77         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
78                 return;
79
80         /*
81          * We will be scheduling *something*, so cancel whatever is
82          * currently scheduled first
83          */
84         cancel_delayed_work(&priv->mcast_task);
85         if (mcast && delay) {
86                 /*
87                  * We had a failure and want to schedule a retry later
88                  */
89                 mcast->backoff *= 2;
90                 if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
91                         mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
92                 mcast->delay_until = jiffies + (mcast->backoff * HZ);
93                 /*
94                  * Mark this mcast for its delay, but restart the
95                  * task immediately.  The join task will make sure to
96                  * clear out all entries without delays, and then
97                  * schedule itself to run again when the earliest
98                  * delay expires
99                  */
100                 queue_delayed_work(priv->wq, &priv->mcast_task, 0);
101         } else if (delay) {
102                 /*
103                  * Special case of retrying after a failure to
104                  * allocate the broadcast multicast group, wait
105                  * 1 second and try again
106                  */
107                 queue_delayed_work(priv->wq, &priv->mcast_task, HZ);
108         } else
109                 queue_delayed_work(priv->wq, &priv->mcast_task, 0);
110 }
111
112 static void ipoib_mcast_free(struct ipoib_mcast *mcast)
113 {
114         struct net_device *dev = mcast->dev;
115         int tx_dropped = 0;
116
117         ipoib_dbg_mcast(ipoib_priv(dev), "deleting multicast group %pI6\n",
118                         mcast->mcmember.mgid.raw);
119
120         /* remove all neigh connected to this mcast */
121         ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
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         netif_tx_lock_bh(dev);
132         dev->stats.tx_dropped += tx_dropped;
133         netif_tx_unlock_bh(dev);
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->delay_until = jiffies;
150         mcast->backoff = 1;
151
152         INIT_LIST_HEAD(&mcast->list);
153         INIT_LIST_HEAD(&mcast->neigh_list);
154         skb_queue_head_init(&mcast->pkt_queue);
155
156         return mcast;
157 }
158
159 static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
160 {
161         struct ipoib_dev_priv *priv = ipoib_priv(dev);
162         struct rb_node *n = priv->multicast_tree.rb_node;
163
164         while (n) {
165                 struct ipoib_mcast *mcast;
166                 int ret;
167
168                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
169
170                 ret = memcmp(mgid, mcast->mcmember.mgid.raw,
171                              sizeof (union ib_gid));
172                 if (ret < 0)
173                         n = n->rb_left;
174                 else if (ret > 0)
175                         n = n->rb_right;
176                 else
177                         return mcast;
178         }
179
180         return NULL;
181 }
182
183 static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
184 {
185         struct ipoib_dev_priv *priv = ipoib_priv(dev);
186         struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
187
188         while (*n) {
189                 struct ipoib_mcast *tmcast;
190                 int ret;
191
192                 pn = *n;
193                 tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
194
195                 ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
196                              sizeof (union ib_gid));
197                 if (ret < 0)
198                         n = &pn->rb_left;
199                 else if (ret > 0)
200                         n = &pn->rb_right;
201                 else
202                         return -EEXIST;
203         }
204
205         rb_link_node(&mcast->rb_node, pn, n);
206         rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
207
208         return 0;
209 }
210
211 static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
212                                    struct ib_sa_mcmember_rec *mcmember)
213 {
214         struct net_device *dev = mcast->dev;
215         struct ipoib_dev_priv *priv = ipoib_priv(dev);
216         struct rdma_netdev *rn = netdev_priv(dev);
217         struct ipoib_ah *ah;
218         int ret;
219         int set_qkey = 0;
220
221         mcast->mcmember = *mcmember;
222
223         /* Set the multicast MTU and cached Q_Key before we attach if it's
224          * the broadcast group.
225          */
226         if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
227                     sizeof (union ib_gid))) {
228                 spin_lock_irq(&priv->lock);
229                 if (!priv->broadcast) {
230                         spin_unlock_irq(&priv->lock);
231                         return -EAGAIN;
232                 }
233                 /*update priv member according to the new mcast*/
234                 priv->broadcast->mcmember.qkey = mcmember->qkey;
235                 priv->broadcast->mcmember.mtu = mcmember->mtu;
236                 priv->broadcast->mcmember.traffic_class = mcmember->traffic_class;
237                 priv->broadcast->mcmember.rate = mcmember->rate;
238                 priv->broadcast->mcmember.sl = mcmember->sl;
239                 priv->broadcast->mcmember.flow_label = mcmember->flow_label;
240                 priv->broadcast->mcmember.hop_limit = mcmember->hop_limit;
241                 /* assume if the admin and the mcast are the same both can be changed */
242                 if (priv->mcast_mtu == priv->admin_mtu)
243                         priv->admin_mtu =
244                         priv->mcast_mtu =
245                         IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
246                 else
247                         priv->mcast_mtu =
248                         IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
249
250                 priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
251                 spin_unlock_irq(&priv->lock);
252                 priv->tx_wr.remote_qkey = priv->qkey;
253                 set_qkey = 1;
254         }
255
256         if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
257                 if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
258                         ipoib_warn(priv, "multicast group %pI6 already attached\n",
259                                    mcast->mcmember.mgid.raw);
260
261                         return 0;
262                 }
263
264                 ret = rn->attach_mcast(dev, priv->ca, &mcast->mcmember.mgid,
265                                        be16_to_cpu(mcast->mcmember.mlid),
266                                        set_qkey, priv->qkey);
267                 if (ret < 0) {
268                         ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
269                                    mcast->mcmember.mgid.raw);
270
271                         clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
272                         return ret;
273                 }
274         }
275
276         {
277                 struct ib_ah_attr av = {
278                         .dlid          = be16_to_cpu(mcast->mcmember.mlid),
279                         .port_num      = priv->port,
280                         .sl            = mcast->mcmember.sl,
281                         .ah_flags      = IB_AH_GRH,
282                         .static_rate   = mcast->mcmember.rate,
283                         .grh           = {
284                                 .flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
285                                 .hop_limit     = mcast->mcmember.hop_limit,
286                                 .sgid_index    = 0,
287                                 .traffic_class = mcast->mcmember.traffic_class
288                         }
289                 };
290                 av.grh.dgid = mcast->mcmember.mgid;
291
292                 ah = ipoib_create_ah(dev, priv->pd, &av);
293                 if (IS_ERR(ah)) {
294                         ipoib_warn(priv, "ib_address_create failed %ld\n",
295                                 -PTR_ERR(ah));
296                         /* use original error */
297                         return PTR_ERR(ah);
298                 } else {
299                         spin_lock_irq(&priv->lock);
300                         mcast->ah = ah;
301                         spin_unlock_irq(&priv->lock);
302
303                         ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
304                                         mcast->mcmember.mgid.raw,
305                                         mcast->ah->ah,
306                                         be16_to_cpu(mcast->mcmember.mlid),
307                                         mcast->mcmember.sl);
308                 }
309         }
310
311         /* actually send any queued packets */
312         netif_tx_lock_bh(dev);
313         while (!skb_queue_empty(&mcast->pkt_queue)) {
314                 struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
315
316                 netif_tx_unlock_bh(dev);
317
318                 skb->dev = dev;
319
320                 ret = dev_queue_xmit(skb);
321                 if (ret)
322                         ipoib_warn(priv, "%s:dev_queue_xmit failed to re-queue packet, ret:%d\n",
323                                    __func__, ret);
324                 netif_tx_lock_bh(dev);
325         }
326         netif_tx_unlock_bh(dev);
327
328         return 0;
329 }
330
331 void ipoib_mcast_carrier_on_task(struct work_struct *work)
332 {
333         struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
334                                                    carrier_on_task);
335         struct ib_port_attr attr;
336         int ret;
337
338         if (ib_query_port(priv->ca, priv->port, &attr) ||
339             attr.state != IB_PORT_ACTIVE) {
340                 ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
341                 return;
342         }
343         /*
344          * Check if can send sendonly MCG's with sendonly-fullmember join state.
345          * It done here after the successfully join to the broadcast group,
346          * because the broadcast group must always be joined first and is always
347          * re-joined if the SM changes substantially.
348          */
349         ret = ipoib_check_sm_sendonly_fullmember_support(priv);
350         if (ret < 0)
351                 pr_debug("%s failed query sm support for sendonly-fullmember (ret: %d)\n",
352                          priv->dev->name, ret);
353
354         /*
355          * Take rtnl_lock to avoid racing with ipoib_stop() and
356          * turning the carrier back on while a device is being
357          * removed.  However, ipoib_stop() will attempt to flush
358          * the workqueue while holding the rtnl lock, so loop
359          * on trylock until either we get the lock or we see
360          * FLAG_OPER_UP go away as that signals that we are bailing
361          * and can safely ignore the carrier on work.
362          */
363         while (!rtnl_trylock()) {
364                 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
365                         return;
366                 else
367                         msleep(20);
368         }
369         if (!ipoib_cm_admin_enabled(priv->dev))
370                 dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
371         netif_carrier_on(priv->dev);
372         rtnl_unlock();
373 }
374
375 static int ipoib_mcast_join_complete(int status,
376                                      struct ib_sa_multicast *multicast)
377 {
378         struct ipoib_mcast *mcast = multicast->context;
379         struct net_device *dev = mcast->dev;
380         struct ipoib_dev_priv *priv = ipoib_priv(dev);
381
382         ipoib_dbg_mcast(priv, "%sjoin completion for %pI6 (status %d)\n",
383                         test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ?
384                         "sendonly " : "",
385                         mcast->mcmember.mgid.raw, status);
386
387         /* We trap for port events ourselves. */
388         if (status == -ENETRESET) {
389                 status = 0;
390                 goto out;
391         }
392
393         if (!status)
394                 status = ipoib_mcast_join_finish(mcast, &multicast->rec);
395
396         if (!status) {
397                 mcast->backoff = 1;
398                 mcast->delay_until = jiffies;
399
400                 /*
401                  * Defer carrier on work to priv->wq to avoid a
402                  * deadlock on rtnl_lock here.  Requeue our multicast
403                  * work too, which will end up happening right after
404                  * our carrier on task work and will allow us to
405                  * send out all of the non-broadcast joins
406                  */
407                 if (mcast == priv->broadcast) {
408                         spin_lock_irq(&priv->lock);
409                         queue_work(priv->wq, &priv->carrier_on_task);
410                         __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
411                         goto out_locked;
412                 }
413         } else {
414                 bool silent_fail =
415                     test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
416                     status == -EINVAL;
417
418                 if (mcast->logcount < 20) {
419                         if (status == -ETIMEDOUT || status == -EAGAIN ||
420                             silent_fail) {
421                                 ipoib_dbg_mcast(priv, "%smulticast join failed for %pI6, status %d\n",
422                                                 test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
423                                                 mcast->mcmember.mgid.raw, status);
424                         } else {
425                                 ipoib_warn(priv, "%smulticast join failed for %pI6, status %d\n",
426                                                 test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
427                                            mcast->mcmember.mgid.raw, status);
428                         }
429
430                         if (!silent_fail)
431                                 mcast->logcount++;
432                 }
433
434                 if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
435                     mcast->backoff >= 2) {
436                         /*
437                          * We only retry sendonly joins once before we drop
438                          * the packet and quit trying to deal with the
439                          * group.  However, we leave the group in the
440                          * mcast list as an unjoined group.  If we want to
441                          * try joining again, we simply queue up a packet
442                          * and restart the join thread.  The empty queue
443                          * is why the join thread ignores this group.
444                          */
445                         mcast->backoff = 1;
446                         netif_tx_lock_bh(dev);
447                         while (!skb_queue_empty(&mcast->pkt_queue)) {
448                                 ++dev->stats.tx_dropped;
449                                 dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
450                         }
451                         netif_tx_unlock_bh(dev);
452                 } else {
453                         spin_lock_irq(&priv->lock);
454                         /* Requeue this join task with a backoff delay */
455                         __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
456                         goto out_locked;
457                 }
458         }
459 out:
460         spin_lock_irq(&priv->lock);
461 out_locked:
462         /*
463          * Make sure to set mcast->mc before we clear the busy flag to avoid
464          * racing with code that checks for BUSY before checking mcast->mc
465          */
466         if (status)
467                 mcast->mc = NULL;
468         else
469                 mcast->mc = multicast;
470         clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
471         spin_unlock_irq(&priv->lock);
472         complete(&mcast->done);
473
474         return status;
475 }
476
477 /*
478  * Caller must hold 'priv->lock'
479  */
480 static int ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast)
481 {
482         struct ipoib_dev_priv *priv = ipoib_priv(dev);
483         struct ib_sa_multicast *multicast;
484         struct ib_sa_mcmember_rec rec = {
485                 .join_state = 1
486         };
487         ib_sa_comp_mask comp_mask;
488         int ret = 0;
489
490         if (!priv->broadcast ||
491             !test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
492                 return -EINVAL;
493
494         ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
495
496         rec.mgid     = mcast->mcmember.mgid;
497         rec.port_gid = priv->local_gid;
498         rec.pkey     = cpu_to_be16(priv->pkey);
499
500         comp_mask =
501                 IB_SA_MCMEMBER_REC_MGID         |
502                 IB_SA_MCMEMBER_REC_PORT_GID     |
503                 IB_SA_MCMEMBER_REC_PKEY         |
504                 IB_SA_MCMEMBER_REC_JOIN_STATE;
505
506         if (mcast != priv->broadcast) {
507                 /*
508                  * RFC 4391:
509                  *  The MGID MUST use the same P_Key, Q_Key, SL, MTU,
510                  *  and HopLimit as those used in the broadcast-GID.  The rest
511                  *  of attributes SHOULD follow the values used in the
512                  *  broadcast-GID as well.
513                  */
514                 comp_mask |=
515                         IB_SA_MCMEMBER_REC_QKEY                 |
516                         IB_SA_MCMEMBER_REC_MTU_SELECTOR         |
517                         IB_SA_MCMEMBER_REC_MTU                  |
518                         IB_SA_MCMEMBER_REC_TRAFFIC_CLASS        |
519                         IB_SA_MCMEMBER_REC_RATE_SELECTOR        |
520                         IB_SA_MCMEMBER_REC_RATE                 |
521                         IB_SA_MCMEMBER_REC_SL                   |
522                         IB_SA_MCMEMBER_REC_FLOW_LABEL           |
523                         IB_SA_MCMEMBER_REC_HOP_LIMIT;
524
525                 rec.qkey          = priv->broadcast->mcmember.qkey;
526                 rec.mtu_selector  = IB_SA_EQ;
527                 rec.mtu           = priv->broadcast->mcmember.mtu;
528                 rec.traffic_class = priv->broadcast->mcmember.traffic_class;
529                 rec.rate_selector = IB_SA_EQ;
530                 rec.rate          = priv->broadcast->mcmember.rate;
531                 rec.sl            = priv->broadcast->mcmember.sl;
532                 rec.flow_label    = priv->broadcast->mcmember.flow_label;
533                 rec.hop_limit     = priv->broadcast->mcmember.hop_limit;
534
535                 /*
536                  * Send-only IB Multicast joins work at the core IB layer but
537                  * require specific SM support.
538                  * We can use such joins here only if the current SM supports that feature.
539                  * However, if not, we emulate an Ethernet multicast send,
540                  * which does not require a multicast subscription and will
541                  * still send properly. The most appropriate thing to
542                  * do is to create the group if it doesn't exist as that
543                  * most closely emulates the behavior, from a user space
544                  * application perspective, of Ethernet multicast operation.
545                  */
546                 if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
547                     priv->sm_fullmember_sendonly_support)
548                         /* SM supports sendonly-fullmember, otherwise fallback to full-member */
549                         rec.join_state = SENDONLY_FULLMEMBER_JOIN;
550         }
551         spin_unlock_irq(&priv->lock);
552
553         multicast = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
554                                          &rec, comp_mask, GFP_KERNEL,
555                                          ipoib_mcast_join_complete, mcast);
556         spin_lock_irq(&priv->lock);
557         if (IS_ERR(multicast)) {
558                 ret = PTR_ERR(multicast);
559                 ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
560                 /* Requeue this join task with a backoff delay */
561                 __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
562                 clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
563                 spin_unlock_irq(&priv->lock);
564                 complete(&mcast->done);
565                 spin_lock_irq(&priv->lock);
566         }
567         return 0;
568 }
569
570 void ipoib_mcast_join_task(struct work_struct *work)
571 {
572         struct ipoib_dev_priv *priv =
573                 container_of(work, struct ipoib_dev_priv, mcast_task.work);
574         struct net_device *dev = priv->dev;
575         struct ib_port_attr port_attr;
576         unsigned long delay_until = 0;
577         struct ipoib_mcast *mcast = NULL;
578
579         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
580                 return;
581
582         if (ib_query_port(priv->ca, priv->port, &port_attr)) {
583                 ipoib_dbg(priv, "ib_query_port() failed\n");
584                 return;
585         }
586         if (port_attr.state != IB_PORT_ACTIVE) {
587                 ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
588                           port_attr.state);
589                 return;
590         }
591         priv->local_lid = port_attr.lid;
592         netif_addr_lock_bh(dev);
593
594         if (!test_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags)) {
595                 netif_addr_unlock_bh(dev);
596                 return;
597         }
598         netif_addr_unlock_bh(dev);
599
600         spin_lock_irq(&priv->lock);
601         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
602                 goto out;
603
604         if (!priv->broadcast) {
605                 struct ipoib_mcast *broadcast;
606
607                 broadcast = ipoib_mcast_alloc(dev, 0);
608                 if (!broadcast) {
609                         ipoib_warn(priv, "failed to allocate broadcast group\n");
610                         /*
611                          * Restart us after a 1 second delay to retry
612                          * creating our broadcast group and attaching to
613                          * it.  Until this succeeds, this ipoib dev is
614                          * completely stalled (multicast wise).
615                          */
616                         __ipoib_mcast_schedule_join_thread(priv, NULL, 1);
617                         goto out;
618                 }
619
620                 memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
621                        sizeof (union ib_gid));
622                 priv->broadcast = broadcast;
623
624                 __ipoib_mcast_add(dev, priv->broadcast);
625         }
626
627         if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
628                 if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
629                     !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags)) {
630                         mcast = priv->broadcast;
631                         if (mcast->backoff > 1 &&
632                             time_before(jiffies, mcast->delay_until)) {
633                                 delay_until = mcast->delay_until;
634                                 mcast = NULL;
635                         }
636                 }
637                 goto out;
638         }
639
640         /*
641          * We'll never get here until the broadcast group is both allocated
642          * and attached
643          */
644         list_for_each_entry(mcast, &priv->multicast_list, list) {
645                 if (IS_ERR_OR_NULL(mcast->mc) &&
646                     !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
647                     (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ||
648                      !skb_queue_empty(&mcast->pkt_queue))) {
649                         if (mcast->backoff == 1 ||
650                             time_after_eq(jiffies, mcast->delay_until)) {
651                                 /* Found the next unjoined group */
652                                 init_completion(&mcast->done);
653                                 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
654                                 if (ipoib_mcast_join(dev, mcast)) {
655                                         spin_unlock_irq(&priv->lock);
656                                         return;
657                                 }
658                         } else if (!delay_until ||
659                                  time_before(mcast->delay_until, delay_until))
660                                 delay_until = mcast->delay_until;
661                 }
662         }
663
664         mcast = NULL;
665         ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
666
667 out:
668         if (delay_until) {
669                 cancel_delayed_work(&priv->mcast_task);
670                 queue_delayed_work(priv->wq, &priv->mcast_task,
671                                    delay_until - jiffies);
672         }
673         if (mcast) {
674                 init_completion(&mcast->done);
675                 set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
676                 ipoib_mcast_join(dev, mcast);
677         }
678         spin_unlock_irq(&priv->lock);
679 }
680
681 void ipoib_mcast_start_thread(struct net_device *dev)
682 {
683         struct ipoib_dev_priv *priv = ipoib_priv(dev);
684         unsigned long flags;
685
686         ipoib_dbg_mcast(priv, "starting multicast thread\n");
687
688         spin_lock_irqsave(&priv->lock, flags);
689         __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
690         spin_unlock_irqrestore(&priv->lock, flags);
691 }
692
693 int ipoib_mcast_stop_thread(struct net_device *dev)
694 {
695         struct ipoib_dev_priv *priv = ipoib_priv(dev);
696         unsigned long flags;
697
698         ipoib_dbg_mcast(priv, "stopping multicast thread\n");
699
700         spin_lock_irqsave(&priv->lock, flags);
701         cancel_delayed_work(&priv->mcast_task);
702         spin_unlock_irqrestore(&priv->lock, flags);
703
704         flush_workqueue(priv->wq);
705
706         return 0;
707 }
708
709 static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
710 {
711         struct ipoib_dev_priv *priv = ipoib_priv(dev);
712         struct rdma_netdev *rn = netdev_priv(dev);
713         int ret = 0;
714
715         if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
716                 ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
717
718         if (!IS_ERR_OR_NULL(mcast->mc))
719                 ib_sa_free_multicast(mcast->mc);
720
721         if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
722                 ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
723                                 mcast->mcmember.mgid.raw);
724
725                 /* Remove ourselves from the multicast group */
726                 ret = rn->detach_mcast(dev, priv->ca, &mcast->mcmember.mgid,
727                                        be16_to_cpu(mcast->mcmember.mlid));
728                 if (ret)
729                         ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
730         } else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
731                 ipoib_dbg(priv, "leaving with no mcmember but not a "
732                           "SENDONLY join\n");
733
734         return 0;
735 }
736
737 /*
738  * Check if the multicast group is sendonly. If so remove it from the maps
739  * and add to the remove list
740  */
741 void ipoib_check_and_add_mcast_sendonly(struct ipoib_dev_priv *priv, u8 *mgid,
742                                 struct list_head *remove_list)
743 {
744         /* Is this multicast ? */
745         if (*mgid == 0xff) {
746                 struct ipoib_mcast *mcast = __ipoib_mcast_find(priv->dev, mgid);
747
748                 if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
749                         list_del(&mcast->list);
750                         rb_erase(&mcast->rb_node, &priv->multicast_tree);
751                         list_add_tail(&mcast->list, remove_list);
752                 }
753         }
754 }
755
756 void ipoib_mcast_remove_list(struct list_head *remove_list)
757 {
758         struct ipoib_mcast *mcast, *tmcast;
759
760         list_for_each_entry_safe(mcast, tmcast, remove_list, list) {
761                 ipoib_mcast_leave(mcast->dev, mcast);
762                 ipoib_mcast_free(mcast);
763         }
764 }
765
766 void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
767 {
768         struct ipoib_dev_priv *priv = ipoib_priv(dev);
769         struct rdma_netdev *rn = netdev_priv(dev);
770         struct ipoib_mcast *mcast;
771         unsigned long flags;
772         void *mgid = daddr + 4;
773
774         spin_lock_irqsave(&priv->lock, flags);
775
776         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)         ||
777             !priv->broadcast                                    ||
778             !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
779                 ++dev->stats.tx_dropped;
780                 dev_kfree_skb_any(skb);
781                 goto unlock;
782         }
783
784         mcast = __ipoib_mcast_find(dev, mgid);
785         if (!mcast || !mcast->ah) {
786                 if (!mcast) {
787                         /* Let's create a new send only group now */
788                         ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
789                                         mgid);
790
791                         mcast = ipoib_mcast_alloc(dev, 0);
792                         if (!mcast) {
793                                 ipoib_warn(priv, "unable to allocate memory "
794                                            "for multicast structure\n");
795                                 ++dev->stats.tx_dropped;
796                                 dev_kfree_skb_any(skb);
797                                 goto unlock;
798                         }
799
800                         set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
801                         memcpy(mcast->mcmember.mgid.raw, mgid,
802                                sizeof (union ib_gid));
803                         __ipoib_mcast_add(dev, mcast);
804                         list_add_tail(&mcast->list, &priv->multicast_list);
805                 }
806                 if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE) {
807                         /* put pseudoheader back on for next time */
808                         skb_push(skb, sizeof(struct ipoib_pseudo_header));
809                         skb_queue_tail(&mcast->pkt_queue, skb);
810                 } else {
811                         ++dev->stats.tx_dropped;
812                         dev_kfree_skb_any(skb);
813                 }
814                 if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
815                         __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
816                 }
817         } else {
818                 struct ipoib_neigh *neigh;
819
820                 spin_unlock_irqrestore(&priv->lock, flags);
821                 neigh = ipoib_neigh_get(dev, daddr);
822                 spin_lock_irqsave(&priv->lock, flags);
823                 if (!neigh) {
824                         neigh = ipoib_neigh_alloc(daddr, dev);
825                         if (neigh) {
826                                 kref_get(&mcast->ah->ref);
827                                 neigh->ah       = mcast->ah;
828                                 list_add_tail(&neigh->list, &mcast->neigh_list);
829                         }
830                 }
831                 spin_unlock_irqrestore(&priv->lock, flags);
832                 mcast->ah->last_send = rn->send(dev, skb, mcast->ah->ah,
833                                                 IB_MULTICAST_QPN);
834                 if (neigh)
835                         ipoib_neigh_put(neigh);
836                 return;
837         }
838
839 unlock:
840         spin_unlock_irqrestore(&priv->lock, flags);
841 }
842
843 void ipoib_mcast_dev_flush(struct net_device *dev)
844 {
845         struct ipoib_dev_priv *priv = ipoib_priv(dev);
846         LIST_HEAD(remove_list);
847         struct ipoib_mcast *mcast, *tmcast;
848         unsigned long flags;
849
850         ipoib_dbg_mcast(priv, "flushing multicast list\n");
851
852         spin_lock_irqsave(&priv->lock, flags);
853
854         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
855                 list_del(&mcast->list);
856                 rb_erase(&mcast->rb_node, &priv->multicast_tree);
857                 list_add_tail(&mcast->list, &remove_list);
858         }
859
860         if (priv->broadcast) {
861                 rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
862                 list_add_tail(&priv->broadcast->list, &remove_list);
863                 priv->broadcast = NULL;
864         }
865
866         spin_unlock_irqrestore(&priv->lock, flags);
867
868         /*
869          * make sure the in-flight joins have finished before we attempt
870          * to leave
871          */
872         list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
873                 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
874                         wait_for_completion(&mcast->done);
875
876         ipoib_mcast_remove_list(&remove_list);
877 }
878
879 static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
880 {
881         /* reserved QPN, prefix, scope */
882         if (memcmp(addr, broadcast, 6))
883                 return 0;
884         /* signature lower, pkey */
885         if (memcmp(addr + 7, broadcast + 7, 3))
886                 return 0;
887         return 1;
888 }
889
890 void ipoib_mcast_restart_task(struct work_struct *work)
891 {
892         struct ipoib_dev_priv *priv =
893                 container_of(work, struct ipoib_dev_priv, restart_task);
894         struct net_device *dev = priv->dev;
895         struct netdev_hw_addr *ha;
896         struct ipoib_mcast *mcast, *tmcast;
897         LIST_HEAD(remove_list);
898         unsigned long flags;
899         struct ib_sa_mcmember_rec rec;
900
901         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
902                 /*
903                  * shortcut...on shutdown flush is called next, just
904                  * let it do all the work
905                  */
906                 return;
907
908         ipoib_dbg_mcast(priv, "restarting multicast task\n");
909
910         local_irq_save(flags);
911         netif_addr_lock(dev);
912         spin_lock(&priv->lock);
913
914         /*
915          * Unfortunately, the networking core only gives us a list of all of
916          * the multicast hardware addresses. We need to figure out which ones
917          * are new and which ones have been removed
918          */
919
920         /* Clear out the found flag */
921         list_for_each_entry(mcast, &priv->multicast_list, list)
922                 clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
923
924         /* Mark all of the entries that are found or don't exist */
925         netdev_for_each_mc_addr(ha, dev) {
926                 union ib_gid mgid;
927
928                 if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
929                         continue;
930
931                 memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
932
933                 mcast = __ipoib_mcast_find(dev, &mgid);
934                 if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
935                         struct ipoib_mcast *nmcast;
936
937                         /* ignore group which is directly joined by userspace */
938                         if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
939                             !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
940                                 ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
941                                                 mgid.raw);
942                                 continue;
943                         }
944
945                         /* Not found or send-only group, let's add a new entry */
946                         ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
947                                         mgid.raw);
948
949                         nmcast = ipoib_mcast_alloc(dev, 0);
950                         if (!nmcast) {
951                                 ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
952                                 continue;
953                         }
954
955                         set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
956
957                         nmcast->mcmember.mgid = mgid;
958
959                         if (mcast) {
960                                 /* Destroy the send only entry */
961                                 list_move_tail(&mcast->list, &remove_list);
962
963                                 rb_replace_node(&mcast->rb_node,
964                                                 &nmcast->rb_node,
965                                                 &priv->multicast_tree);
966                         } else
967                                 __ipoib_mcast_add(dev, nmcast);
968
969                         list_add_tail(&nmcast->list, &priv->multicast_list);
970                 }
971
972                 if (mcast)
973                         set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
974         }
975
976         /* Remove all of the entries don't exist anymore */
977         list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
978                 if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
979                     !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
980                         ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
981                                         mcast->mcmember.mgid.raw);
982
983                         rb_erase(&mcast->rb_node, &priv->multicast_tree);
984
985                         /* Move to the remove list */
986                         list_move_tail(&mcast->list, &remove_list);
987                 }
988         }
989
990         spin_unlock(&priv->lock);
991         netif_addr_unlock(dev);
992         local_irq_restore(flags);
993
994         /*
995          * make sure the in-flight joins have finished before we attempt
996          * to leave
997          */
998         list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
999                 if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
1000                         wait_for_completion(&mcast->done);
1001
1002         ipoib_mcast_remove_list(&remove_list);
1003
1004         /*
1005          * Double check that we are still up
1006          */
1007         if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1008                 spin_lock_irqsave(&priv->lock, flags);
1009                 __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
1010                 spin_unlock_irqrestore(&priv->lock, flags);
1011         }
1012 }
1013
1014 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
1015
1016 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
1017 {
1018         struct ipoib_mcast_iter *iter;
1019
1020         iter = kmalloc(sizeof *iter, GFP_KERNEL);
1021         if (!iter)
1022                 return NULL;
1023
1024         iter->dev = dev;
1025         memset(iter->mgid.raw, 0, 16);
1026
1027         if (ipoib_mcast_iter_next(iter)) {
1028                 kfree(iter);
1029                 return NULL;
1030         }
1031
1032         return iter;
1033 }
1034
1035 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
1036 {
1037         struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
1038         struct rb_node *n;
1039         struct ipoib_mcast *mcast;
1040         int ret = 1;
1041
1042         spin_lock_irq(&priv->lock);
1043
1044         n = rb_first(&priv->multicast_tree);
1045
1046         while (n) {
1047                 mcast = rb_entry(n, struct ipoib_mcast, rb_node);
1048
1049                 if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
1050                            sizeof (union ib_gid)) < 0) {
1051                         iter->mgid      = mcast->mcmember.mgid;
1052                         iter->created   = mcast->created;
1053                         iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
1054                         iter->complete  = !!mcast->ah;
1055                         iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
1056
1057                         ret = 0;
1058
1059                         break;
1060                 }
1061
1062                 n = rb_next(n);
1063         }
1064
1065         spin_unlock_irq(&priv->lock);
1066
1067         return ret;
1068 }
1069
1070 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
1071                            union ib_gid *mgid,
1072                            unsigned long *created,
1073                            unsigned int *queuelen,
1074                            unsigned int *complete,
1075                            unsigned int *send_only)
1076 {
1077         *mgid      = iter->mgid;
1078         *created   = iter->created;
1079         *queuelen  = iter->queuelen;
1080         *complete  = iter->complete;
1081         *send_only = iter->send_only;
1082 }
1083
1084 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */