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