• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/src/linux/linux-2.6/drivers/infiniband/ulp/ipoib/
1/*
2 * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses.  You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 *     Redistribution and use in source and binary forms, with or
13 *     without modification, are permitted provided that the following
14 *     conditions are met:
15 *
16 *      - Redistributions of source code must retain the above
17 *        copyright notice, this list of conditions and the following
18 *        disclaimer.
19 *
20 *      - Redistributions in binary form must reproduce the above
21 *        copyright notice, this list of conditions and the following
22 *        disclaimer in the documentation and/or other materials
23 *        provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 *
34 * $Id: ipoib_multicast.c,v 1.1.1.1 2007/08/03 18:52:32 Exp $
35 */
36
37#include <linux/skbuff.h>
38#include <linux/rtnetlink.h>
39#include <linux/ip.h>
40#include <linux/in.h>
41#include <linux/igmp.h>
42#include <linux/inetdevice.h>
43#include <linux/delay.h>
44#include <linux/completion.h>
45
46#include <net/dst.h>
47
48#include "ipoib.h"
49
50#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
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
58static DEFINE_MUTEX(mcast_mutex);
59
60/* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
61struct ipoib_mcast {
62	struct ib_sa_mcmember_rec mcmember;
63	struct ib_sa_multicast	 *mc;
64	struct ipoib_ah          *ah;
65
66	struct rb_node    rb_node;
67	struct list_head  list;
68
69	unsigned long created;
70	unsigned long backoff;
71
72	unsigned long flags;
73	unsigned char logcount;
74
75	struct list_head  neigh_list;
76
77	struct sk_buff_head pkt_queue;
78
79	struct net_device *dev;
80};
81
82struct ipoib_mcast_iter {
83	struct net_device *dev;
84	union ib_gid       mgid;
85	unsigned long      created;
86	unsigned int       queuelen;
87	unsigned int       complete;
88	unsigned int       send_only;
89};
90
91static void ipoib_mcast_free(struct ipoib_mcast *mcast)
92{
93	struct net_device *dev = mcast->dev;
94	struct ipoib_dev_priv *priv = netdev_priv(dev);
95	struct ipoib_neigh *neigh, *tmp;
96	unsigned long flags;
97	int tx_dropped = 0;
98
99	ipoib_dbg_mcast(netdev_priv(dev),
100			"deleting multicast group " IPOIB_GID_FMT "\n",
101			IPOIB_GID_ARG(mcast->mcmember.mgid));
102
103	spin_lock_irqsave(&priv->lock, flags);
104
105	list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
106		/*
107		 * It's safe to call ipoib_put_ah() inside priv->lock
108		 * here, because we know that mcast->ah will always
109		 * hold one more reference, so ipoib_put_ah() will
110		 * never do more than decrement the ref count.
111		 */
112		if (neigh->ah)
113			ipoib_put_ah(neigh->ah);
114		ipoib_neigh_free(dev, neigh);
115	}
116
117	spin_unlock_irqrestore(&priv->lock, flags);
118
119	if (mcast->ah)
120		ipoib_put_ah(mcast->ah);
121
122	while (!skb_queue_empty(&mcast->pkt_queue)) {
123		++tx_dropped;
124		dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
125	}
126
127	spin_lock_irqsave(&priv->tx_lock, flags);
128	priv->stats.tx_dropped += tx_dropped;
129	spin_unlock_irqrestore(&priv->tx_lock, flags);
130
131	kfree(mcast);
132}
133
134static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
135					     int can_sleep)
136{
137	struct ipoib_mcast *mcast;
138
139	mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
140	if (!mcast)
141		return NULL;
142
143	mcast->dev = dev;
144	mcast->created = jiffies;
145	mcast->backoff = 1;
146
147	INIT_LIST_HEAD(&mcast->list);
148	INIT_LIST_HEAD(&mcast->neigh_list);
149	skb_queue_head_init(&mcast->pkt_queue);
150
151	return mcast;
152}
153
154static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
155{
156	struct ipoib_dev_priv *priv = netdev_priv(dev);
157	struct rb_node *n = priv->multicast_tree.rb_node;
158
159	while (n) {
160		struct ipoib_mcast *mcast;
161		int ret;
162
163		mcast = rb_entry(n, struct ipoib_mcast, rb_node);
164
165		ret = memcmp(mgid, mcast->mcmember.mgid.raw,
166			     sizeof (union ib_gid));
167		if (ret < 0)
168			n = n->rb_left;
169		else if (ret > 0)
170			n = n->rb_right;
171		else
172			return mcast;
173	}
174
175	return NULL;
176}
177
178static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
179{
180	struct ipoib_dev_priv *priv = netdev_priv(dev);
181	struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
182
183	while (*n) {
184		struct ipoib_mcast *tmcast;
185		int ret;
186
187		pn = *n;
188		tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
189
190		ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
191			     sizeof (union ib_gid));
192		if (ret < 0)
193			n = &pn->rb_left;
194		else if (ret > 0)
195			n = &pn->rb_right;
196		else
197			return -EEXIST;
198	}
199
200	rb_link_node(&mcast->rb_node, pn, n);
201	rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
202
203	return 0;
204}
205
206static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
207				   struct ib_sa_mcmember_rec *mcmember)
208{
209	struct net_device *dev = mcast->dev;
210	struct ipoib_dev_priv *priv = netdev_priv(dev);
211	struct ipoib_ah *ah;
212	int ret;
213
214	mcast->mcmember = *mcmember;
215
216	/* Set the cached Q_Key before we attach if it's the broadcast group */
217	if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
218		    sizeof (union ib_gid))) {
219		priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
220		priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
221	}
222
223	if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
224		if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
225			ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
226				   " already attached\n",
227				   IPOIB_GID_ARG(mcast->mcmember.mgid));
228
229			return 0;
230		}
231
232		ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
233					 &mcast->mcmember.mgid);
234		if (ret < 0) {
235			ipoib_warn(priv, "couldn't attach QP to multicast group "
236				   IPOIB_GID_FMT "\n",
237				   IPOIB_GID_ARG(mcast->mcmember.mgid));
238
239			clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
240			return ret;
241		}
242	}
243
244	{
245		struct ib_ah_attr av = {
246			.dlid	       = be16_to_cpu(mcast->mcmember.mlid),
247			.port_num      = priv->port,
248			.sl	       = mcast->mcmember.sl,
249			.ah_flags      = IB_AH_GRH,
250			.static_rate   = mcast->mcmember.rate,
251			.grh	       = {
252				.flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
253				.hop_limit     = mcast->mcmember.hop_limit,
254				.sgid_index    = 0,
255				.traffic_class = mcast->mcmember.traffic_class
256			}
257		};
258		av.grh.dgid = mcast->mcmember.mgid;
259
260		ah = ipoib_create_ah(dev, priv->pd, &av);
261		if (!ah) {
262			ipoib_warn(priv, "ib_address_create failed\n");
263		} else {
264			spin_lock_irq(&priv->lock);
265			mcast->ah = ah;
266			spin_unlock_irq(&priv->lock);
267
268			ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
269					" AV %p, LID 0x%04x, SL %d\n",
270					IPOIB_GID_ARG(mcast->mcmember.mgid),
271					mcast->ah->ah,
272					be16_to_cpu(mcast->mcmember.mlid),
273					mcast->mcmember.sl);
274		}
275	}
276
277	/* actually send any queued packets */
278	spin_lock_irq(&priv->tx_lock);
279	while (!skb_queue_empty(&mcast->pkt_queue)) {
280		struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
281		spin_unlock_irq(&priv->tx_lock);
282
283		skb->dev = dev;
284
285		if (!skb->dst || !skb->dst->neighbour) {
286			/* put pseudoheader back on for next time */
287			skb_push(skb, sizeof (struct ipoib_pseudoheader));
288		}
289
290		if (dev_queue_xmit(skb))
291			ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
292		spin_lock_irq(&priv->tx_lock);
293	}
294	spin_unlock_irq(&priv->tx_lock);
295
296	return 0;
297}
298
299static int
300ipoib_mcast_sendonly_join_complete(int status,
301				   struct ib_sa_multicast *multicast)
302{
303	struct ipoib_mcast *mcast = multicast->context;
304	struct net_device *dev = mcast->dev;
305	struct ipoib_dev_priv *priv = netdev_priv(dev);
306
307	/* We trap for port events ourselves. */
308	if (status == -ENETRESET)
309		return 0;
310
311	if (!status)
312		status = ipoib_mcast_join_finish(mcast, &multicast->rec);
313
314	if (status) {
315		if (mcast->logcount++ < 20)
316			ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
317					IPOIB_GID_FMT ", status %d\n",
318					IPOIB_GID_ARG(mcast->mcmember.mgid), status);
319
320		/* Flush out any queued packets */
321		spin_lock_irq(&priv->tx_lock);
322		while (!skb_queue_empty(&mcast->pkt_queue)) {
323			++priv->stats.tx_dropped;
324			dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
325		}
326		spin_unlock_irq(&priv->tx_lock);
327
328		/* Clear the busy flag so we try again */
329		status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
330					    &mcast->flags);
331	}
332	return status;
333}
334
335static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
336{
337	struct net_device *dev = mcast->dev;
338	struct ipoib_dev_priv *priv = netdev_priv(dev);
339	struct ib_sa_mcmember_rec rec = {
340		.join_state = 1
341	};
342	int ret = 0;
343
344	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
345		ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
346		return -ENODEV;
347	}
348
349	if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
350		ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
351		return -EBUSY;
352	}
353
354	rec.mgid     = mcast->mcmember.mgid;
355	rec.port_gid = priv->local_gid;
356	rec.pkey     = cpu_to_be16(priv->pkey);
357
358	mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
359					 priv->port, &rec,
360					 IB_SA_MCMEMBER_REC_MGID	|
361					 IB_SA_MCMEMBER_REC_PORT_GID	|
362					 IB_SA_MCMEMBER_REC_PKEY	|
363					 IB_SA_MCMEMBER_REC_JOIN_STATE,
364					 GFP_ATOMIC,
365					 ipoib_mcast_sendonly_join_complete,
366					 mcast);
367	if (IS_ERR(mcast->mc)) {
368		ret = PTR_ERR(mcast->mc);
369		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
370		ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
371			   ret);
372	} else {
373		ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
374				", starting join\n",
375				IPOIB_GID_ARG(mcast->mcmember.mgid));
376	}
377
378	return ret;
379}
380
381static int ipoib_mcast_join_complete(int status,
382				     struct ib_sa_multicast *multicast)
383{
384	struct ipoib_mcast *mcast = multicast->context;
385	struct net_device *dev = mcast->dev;
386	struct ipoib_dev_priv *priv = netdev_priv(dev);
387
388	ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
389			" (status %d)\n",
390			IPOIB_GID_ARG(mcast->mcmember.mgid), status);
391
392	/* We trap for port events ourselves. */
393	if (status == -ENETRESET)
394		return 0;
395
396	if (!status)
397		status = ipoib_mcast_join_finish(mcast, &multicast->rec);
398
399	if (!status) {
400		mcast->backoff = 1;
401		mutex_lock(&mcast_mutex);
402		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
403			queue_delayed_work(ipoib_workqueue,
404					   &priv->mcast_task, 0);
405		mutex_unlock(&mcast_mutex);
406
407		if (mcast == priv->broadcast)
408			netif_carrier_on(dev);
409
410		return 0;
411	}
412
413	if (mcast->logcount++ < 20) {
414		if (status == -ETIMEDOUT) {
415			ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
416					", status %d\n",
417					IPOIB_GID_ARG(mcast->mcmember.mgid),
418					status);
419		} else {
420			ipoib_warn(priv, "multicast join failed for "
421				   IPOIB_GID_FMT ", status %d\n",
422				   IPOIB_GID_ARG(mcast->mcmember.mgid),
423				   status);
424		}
425	}
426
427	mcast->backoff *= 2;
428	if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
429		mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
430
431	/* Clear the busy flag so we try again */
432	status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
433
434	mutex_lock(&mcast_mutex);
435	spin_lock_irq(&priv->lock);
436	if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
437		queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
438				   mcast->backoff * HZ);
439	spin_unlock_irq(&priv->lock);
440	mutex_unlock(&mcast_mutex);
441
442	return status;
443}
444
445static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
446			     int create)
447{
448	struct ipoib_dev_priv *priv = netdev_priv(dev);
449	struct ib_sa_mcmember_rec rec = {
450		.join_state = 1
451	};
452	ib_sa_comp_mask comp_mask;
453	int ret = 0;
454
455	ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
456			IPOIB_GID_ARG(mcast->mcmember.mgid));
457
458	rec.mgid     = mcast->mcmember.mgid;
459	rec.port_gid = priv->local_gid;
460	rec.pkey     = cpu_to_be16(priv->pkey);
461
462	comp_mask =
463		IB_SA_MCMEMBER_REC_MGID		|
464		IB_SA_MCMEMBER_REC_PORT_GID	|
465		IB_SA_MCMEMBER_REC_PKEY		|
466		IB_SA_MCMEMBER_REC_JOIN_STATE;
467
468	if (create) {
469		comp_mask |=
470			IB_SA_MCMEMBER_REC_QKEY			|
471			IB_SA_MCMEMBER_REC_MTU_SELECTOR		|
472			IB_SA_MCMEMBER_REC_MTU			|
473			IB_SA_MCMEMBER_REC_TRAFFIC_CLASS	|
474			IB_SA_MCMEMBER_REC_RATE_SELECTOR	|
475			IB_SA_MCMEMBER_REC_RATE			|
476			IB_SA_MCMEMBER_REC_SL			|
477			IB_SA_MCMEMBER_REC_FLOW_LABEL		|
478			IB_SA_MCMEMBER_REC_HOP_LIMIT;
479
480		rec.qkey	  = priv->broadcast->mcmember.qkey;
481		rec.mtu_selector  = IB_SA_EQ;
482		rec.mtu		  = priv->broadcast->mcmember.mtu;
483		rec.traffic_class = priv->broadcast->mcmember.traffic_class;
484		rec.rate_selector = IB_SA_EQ;
485		rec.rate	  = priv->broadcast->mcmember.rate;
486		rec.sl		  = priv->broadcast->mcmember.sl;
487		rec.flow_label	  = priv->broadcast->mcmember.flow_label;
488		rec.hop_limit	  = priv->broadcast->mcmember.hop_limit;
489	}
490
491	set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
492	mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
493					 &rec, comp_mask, GFP_KERNEL,
494					 ipoib_mcast_join_complete, mcast);
495	if (IS_ERR(mcast->mc)) {
496		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
497		ret = PTR_ERR(mcast->mc);
498		ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
499
500		mcast->backoff *= 2;
501		if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
502			mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
503
504		mutex_lock(&mcast_mutex);
505		if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
506			queue_delayed_work(ipoib_workqueue,
507					   &priv->mcast_task,
508					   mcast->backoff * HZ);
509		mutex_unlock(&mcast_mutex);
510	}
511}
512
513void ipoib_mcast_join_task(struct work_struct *work)
514{
515	struct ipoib_dev_priv *priv =
516		container_of(work, struct ipoib_dev_priv, mcast_task.work);
517	struct net_device *dev = priv->dev;
518
519	if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
520		return;
521
522	if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
523		ipoib_warn(priv, "ib_query_gid() failed\n");
524	else
525		memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
526
527	{
528		struct ib_port_attr attr;
529
530		if (!ib_query_port(priv->ca, priv->port, &attr))
531			priv->local_lid = attr.lid;
532		else
533			ipoib_warn(priv, "ib_query_port failed\n");
534	}
535
536	if (!priv->broadcast) {
537		struct ipoib_mcast *broadcast;
538
539		broadcast = ipoib_mcast_alloc(dev, 1);
540		if (!broadcast) {
541			ipoib_warn(priv, "failed to allocate broadcast group\n");
542			mutex_lock(&mcast_mutex);
543			if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
544				queue_delayed_work(ipoib_workqueue,
545						   &priv->mcast_task, HZ);
546			mutex_unlock(&mcast_mutex);
547			return;
548		}
549
550		spin_lock_irq(&priv->lock);
551		memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
552		       sizeof (union ib_gid));
553		priv->broadcast = broadcast;
554
555		__ipoib_mcast_add(dev, priv->broadcast);
556		spin_unlock_irq(&priv->lock);
557	}
558
559	if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
560		if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
561			ipoib_mcast_join(dev, priv->broadcast, 0);
562		return;
563	}
564
565	while (1) {
566		struct ipoib_mcast *mcast = NULL;
567
568		spin_lock_irq(&priv->lock);
569		list_for_each_entry(mcast, &priv->multicast_list, list) {
570			if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
571			    && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
572			    && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
573				/* Found the next unjoined group */
574				break;
575			}
576		}
577		spin_unlock_irq(&priv->lock);
578
579		if (&mcast->list == &priv->multicast_list) {
580			/* All done */
581			break;
582		}
583
584		ipoib_mcast_join(dev, mcast, 1);
585		return;
586	}
587
588	priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
589		IPOIB_ENCAP_LEN;
590
591	if (!ipoib_cm_admin_enabled(dev))
592		dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
593
594	ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
595
596	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
597}
598
599int ipoib_mcast_start_thread(struct net_device *dev)
600{
601	struct ipoib_dev_priv *priv = netdev_priv(dev);
602
603	ipoib_dbg_mcast(priv, "starting multicast thread\n");
604
605	mutex_lock(&mcast_mutex);
606	if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
607		queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
608	mutex_unlock(&mcast_mutex);
609
610	spin_lock_irq(&priv->lock);
611	set_bit(IPOIB_MCAST_STARTED, &priv->flags);
612	spin_unlock_irq(&priv->lock);
613
614	return 0;
615}
616
617int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
618{
619	struct ipoib_dev_priv *priv = netdev_priv(dev);
620
621	ipoib_dbg_mcast(priv, "stopping multicast thread\n");
622
623	spin_lock_irq(&priv->lock);
624	clear_bit(IPOIB_MCAST_STARTED, &priv->flags);
625	spin_unlock_irq(&priv->lock);
626
627	mutex_lock(&mcast_mutex);
628	clear_bit(IPOIB_MCAST_RUN, &priv->flags);
629	cancel_delayed_work(&priv->mcast_task);
630	mutex_unlock(&mcast_mutex);
631
632	if (flush)
633		flush_workqueue(ipoib_workqueue);
634
635	return 0;
636}
637
638static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
639{
640	struct ipoib_dev_priv *priv = netdev_priv(dev);
641	int ret = 0;
642
643	if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
644		ib_sa_free_multicast(mcast->mc);
645
646	if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
647		ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
648				IPOIB_GID_ARG(mcast->mcmember.mgid));
649
650		/* Remove ourselves from the multicast group */
651		ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
652					 &mcast->mcmember.mgid);
653		if (ret)
654			ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
655	}
656
657	return 0;
658}
659
660void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
661{
662	struct ipoib_dev_priv *priv = netdev_priv(dev);
663	struct ipoib_mcast *mcast;
664
665	/*
666	 * We can only be called from ipoib_start_xmit, so we're
667	 * inside tx_lock -- no need to save/restore flags.
668	 */
669	spin_lock(&priv->lock);
670
671	if (!test_bit(IPOIB_MCAST_STARTED, &priv->flags)	||
672	    !priv->broadcast					||
673	    !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
674		++priv->stats.tx_dropped;
675		dev_kfree_skb_any(skb);
676		goto unlock;
677	}
678
679	mcast = __ipoib_mcast_find(dev, mgid);
680	if (!mcast) {
681		/* Let's create a new send only group now */
682		ipoib_dbg_mcast(priv, "setting up send only multicast group for "
683				IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
684
685		mcast = ipoib_mcast_alloc(dev, 0);
686		if (!mcast) {
687			ipoib_warn(priv, "unable to allocate memory for "
688				   "multicast structure\n");
689			++priv->stats.tx_dropped;
690			dev_kfree_skb_any(skb);
691			goto out;
692		}
693
694		set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
695		memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
696		__ipoib_mcast_add(dev, mcast);
697		list_add_tail(&mcast->list, &priv->multicast_list);
698	}
699
700	if (!mcast->ah) {
701		if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
702			skb_queue_tail(&mcast->pkt_queue, skb);
703		else {
704			++priv->stats.tx_dropped;
705			dev_kfree_skb_any(skb);
706		}
707
708		if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
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
721out:
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 = ipoib_neigh_alloc(skb->dst->neighbour);
727
728			if (neigh) {
729				kref_get(&mcast->ah->ref);
730				neigh->ah  	= mcast->ah;
731				list_add_tail(&neigh->list, &mcast->neigh_list);
732			}
733		}
734
735		ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
736	}
737
738unlock:
739	spin_unlock(&priv->lock);
740}
741
742void ipoib_mcast_dev_flush(struct net_device *dev)
743{
744	struct ipoib_dev_priv *priv = netdev_priv(dev);
745	LIST_HEAD(remove_list);
746	struct ipoib_mcast *mcast, *tmcast;
747	unsigned long flags;
748
749	ipoib_dbg_mcast(priv, "flushing multicast list\n");
750
751	spin_lock_irqsave(&priv->lock, flags);
752
753	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
754		list_del(&mcast->list);
755		rb_erase(&mcast->rb_node, &priv->multicast_tree);
756		list_add_tail(&mcast->list, &remove_list);
757	}
758
759	if (priv->broadcast) {
760		rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
761		list_add_tail(&priv->broadcast->list, &remove_list);
762		priv->broadcast = NULL;
763	}
764
765	spin_unlock_irqrestore(&priv->lock, flags);
766
767	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
768		ipoib_mcast_leave(dev, mcast);
769		ipoib_mcast_free(mcast);
770	}
771}
772
773void ipoib_mcast_restart_task(struct work_struct *work)
774{
775	struct ipoib_dev_priv *priv =
776		container_of(work, struct ipoib_dev_priv, restart_task);
777	struct net_device *dev = priv->dev;
778	struct dev_mc_list *mclist;
779	struct ipoib_mcast *mcast, *tmcast;
780	LIST_HEAD(remove_list);
781	unsigned long flags;
782
783	ipoib_dbg_mcast(priv, "restarting multicast task\n");
784
785	ipoib_mcast_stop_thread(dev, 0);
786
787	local_irq_save(flags);
788	netif_tx_lock(dev);
789	spin_lock(&priv->lock);
790
791	/*
792	 * Unfortunately, the networking core only gives us a list of all of
793	 * the multicast hardware addresses. We need to figure out which ones
794	 * are new and which ones have been removed
795	 */
796
797	/* Clear out the found flag */
798	list_for_each_entry(mcast, &priv->multicast_list, list)
799		clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
800
801	/* Mark all of the entries that are found or don't exist */
802	for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
803		union ib_gid mgid;
804
805		memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
806
807		/* Add in the P_Key */
808		mgid.raw[4] = (priv->pkey >> 8) & 0xff;
809		mgid.raw[5] = priv->pkey & 0xff;
810
811		mcast = __ipoib_mcast_find(dev, &mgid);
812		if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
813			struct ipoib_mcast *nmcast;
814
815			/* Not found or send-only group, let's add a new entry */
816			ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
817					IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
818
819			nmcast = ipoib_mcast_alloc(dev, 0);
820			if (!nmcast) {
821				ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
822				continue;
823			}
824
825			set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
826
827			nmcast->mcmember.mgid = mgid;
828
829			if (mcast) {
830				/* Destroy the send only entry */
831				list_move_tail(&mcast->list, &remove_list);
832
833				rb_replace_node(&mcast->rb_node,
834						&nmcast->rb_node,
835						&priv->multicast_tree);
836			} else
837				__ipoib_mcast_add(dev, nmcast);
838
839			list_add_tail(&nmcast->list, &priv->multicast_list);
840		}
841
842		if (mcast)
843			set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
844	}
845
846	/* Remove all of the entries don't exist anymore */
847	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
848		if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
849		    !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
850			ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
851					IPOIB_GID_ARG(mcast->mcmember.mgid));
852
853			rb_erase(&mcast->rb_node, &priv->multicast_tree);
854
855			/* Move to the remove list */
856			list_move_tail(&mcast->list, &remove_list);
857		}
858	}
859
860	spin_unlock(&priv->lock);
861	netif_tx_unlock(dev);
862	local_irq_restore(flags);
863
864	/* We have to cancel outside of the spinlock */
865	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
866		ipoib_mcast_leave(mcast->dev, mcast);
867		ipoib_mcast_free(mcast);
868	}
869
870	if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
871		ipoib_mcast_start_thread(dev);
872}
873
874#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
875
876struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
877{
878	struct ipoib_mcast_iter *iter;
879
880	iter = kmalloc(sizeof *iter, GFP_KERNEL);
881	if (!iter)
882		return NULL;
883
884	iter->dev = dev;
885	memset(iter->mgid.raw, 0, 16);
886
887	if (ipoib_mcast_iter_next(iter)) {
888		kfree(iter);
889		return NULL;
890	}
891
892	return iter;
893}
894
895int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
896{
897	struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
898	struct rb_node *n;
899	struct ipoib_mcast *mcast;
900	int ret = 1;
901
902	spin_lock_irq(&priv->lock);
903
904	n = rb_first(&priv->multicast_tree);
905
906	while (n) {
907		mcast = rb_entry(n, struct ipoib_mcast, rb_node);
908
909		if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
910			   sizeof (union ib_gid)) < 0) {
911			iter->mgid      = mcast->mcmember.mgid;
912			iter->created   = mcast->created;
913			iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
914			iter->complete  = !!mcast->ah;
915			iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
916
917			ret = 0;
918
919			break;
920		}
921
922		n = rb_next(n);
923	}
924
925	spin_unlock_irq(&priv->lock);
926
927	return ret;
928}
929
930void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
931			   union ib_gid *mgid,
932			   unsigned long *created,
933			   unsigned int *queuelen,
934			   unsigned int *complete,
935			   unsigned int *send_only)
936{
937	*mgid      = iter->mgid;
938	*created   = iter->created;
939	*queuelen  = iter->queuelen;
940	*complete  = iter->complete;
941	*send_only = iter->send_only;
942}
943
944#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
945