1 /*
2  * Copyright (c) 2004 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 "ipoib.h"
36 
37 #include <linux/module.h>
38 
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/kernel.h>
42 #include <linux/vmalloc.h>
43 
44 #include <linux/if_arp.h>	/* For ARPHRD_xxx */
45 
46 #include <linux/ip.h>
47 #include <linux/in.h>
48 
49 #include <linux/jhash.h>
50 #include <net/arp.h>
51 #include <net/addrconf.h>
52 #include <linux/inetdevice.h>
53 #include <rdma/ib_cache.h>
54 #include <linux/pci.h>
55 
56 #define DRV_VERSION "1.0.0"
57 
58 const char ipoib_driver_version[] = DRV_VERSION;
59 
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
62 MODULE_LICENSE("Dual BSD/GPL");
63 MODULE_VERSION(DRV_VERSION);
64 
65 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
66 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
67 
68 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
69 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
70 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
71 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
72 
73 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
74 int ipoib_debug_level;
75 
76 module_param_named(debug_level, ipoib_debug_level, int, 0644);
77 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
78 #endif
79 
80 struct ipoib_path_iter {
81 	struct net_device *dev;
82 	struct ipoib_path  path;
83 };
84 
85 static const u8 ipv4_bcast_addr[] = {
86 	0x00, 0xff, 0xff, 0xff,
87 	0xff, 0x12, 0x40, 0x1b,	0x00, 0x00, 0x00, 0x00,
88 	0x00, 0x00, 0x00, 0x00,	0xff, 0xff, 0xff, 0xff
89 };
90 
91 struct workqueue_struct *ipoib_workqueue;
92 
93 struct ib_sa_client ipoib_sa_client;
94 
95 static void ipoib_add_one(struct ib_device *device);
96 static void ipoib_remove_one(struct ib_device *device, void *client_data);
97 static void ipoib_neigh_reclaim(struct rcu_head *rp);
98 static struct net_device *ipoib_get_net_dev_by_params(
99 		struct ib_device *dev, u8 port, u16 pkey,
100 		const union ib_gid *gid, const struct sockaddr *addr,
101 		void *client_data);
102 static int ipoib_set_mac(struct net_device *dev, void *addr);
103 
104 static struct ib_client ipoib_client = {
105 	.name   = "ipoib",
106 	.add    = ipoib_add_one,
107 	.remove = ipoib_remove_one,
108 	.get_net_dev_by_params = ipoib_get_net_dev_by_params,
109 };
110 
111 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
112 static int ipoib_netdev_event(struct notifier_block *this,
113 			      unsigned long event, void *ptr)
114 {
115 	struct netdev_notifier_info *ni = ptr;
116 	struct net_device *dev = ni->dev;
117 
118 	if (dev->netdev_ops->ndo_open != ipoib_open)
119 		return NOTIFY_DONE;
120 
121 	switch (event) {
122 	case NETDEV_REGISTER:
123 		ipoib_create_debug_files(dev);
124 		break;
125 	case NETDEV_CHANGENAME:
126 		ipoib_delete_debug_files(dev);
127 		ipoib_create_debug_files(dev);
128 		break;
129 	case NETDEV_UNREGISTER:
130 		ipoib_delete_debug_files(dev);
131 		break;
132 	}
133 
134 	return NOTIFY_DONE;
135 }
136 #endif
137 
138 int ipoib_open(struct net_device *dev)
139 {
140 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
141 
142 	ipoib_dbg(priv, "bringing up interface\n");
143 
144 	netif_carrier_off(dev);
145 
146 	set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
147 
148 	priv->sm_fullmember_sendonly_support = false;
149 
150 	if (ipoib_ib_dev_open(dev)) {
151 		if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
152 			return 0;
153 		goto err_disable;
154 	}
155 
156 	ipoib_ib_dev_up(dev);
157 
158 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
159 		struct ipoib_dev_priv *cpriv;
160 
161 		/* Bring up any child interfaces too */
162 		down_read(&priv->vlan_rwsem);
163 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
164 			int flags;
165 
166 			flags = cpriv->dev->flags;
167 			if (flags & IFF_UP)
168 				continue;
169 
170 			dev_change_flags(cpriv->dev, flags | IFF_UP);
171 		}
172 		up_read(&priv->vlan_rwsem);
173 	}
174 
175 	netif_start_queue(dev);
176 
177 	return 0;
178 
179 err_disable:
180 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
181 
182 	return -EINVAL;
183 }
184 
185 static int ipoib_stop(struct net_device *dev)
186 {
187 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
188 
189 	ipoib_dbg(priv, "stopping interface\n");
190 
191 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
192 
193 	netif_stop_queue(dev);
194 
195 	ipoib_ib_dev_down(dev);
196 	ipoib_ib_dev_stop(dev);
197 
198 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
199 		struct ipoib_dev_priv *cpriv;
200 
201 		/* Bring down any child interfaces too */
202 		down_read(&priv->vlan_rwsem);
203 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
204 			int flags;
205 
206 			flags = cpriv->dev->flags;
207 			if (!(flags & IFF_UP))
208 				continue;
209 
210 			dev_change_flags(cpriv->dev, flags & ~IFF_UP);
211 		}
212 		up_read(&priv->vlan_rwsem);
213 	}
214 
215 	return 0;
216 }
217 
218 static void ipoib_uninit(struct net_device *dev)
219 {
220 	ipoib_dev_cleanup(dev);
221 }
222 
223 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
224 {
225 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
226 
227 	if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
228 		features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
229 
230 	return features;
231 }
232 
233 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
234 {
235 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
236 	int ret = 0;
237 
238 	/* dev->mtu > 2K ==> connected mode */
239 	if (ipoib_cm_admin_enabled(dev)) {
240 		if (new_mtu > ipoib_cm_max_mtu(dev))
241 			return -EINVAL;
242 
243 		if (new_mtu > priv->mcast_mtu)
244 			ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
245 				   priv->mcast_mtu);
246 
247 		dev->mtu = new_mtu;
248 		return 0;
249 	}
250 
251 	if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
252 		return -EINVAL;
253 
254 	priv->admin_mtu = new_mtu;
255 
256 	if (priv->mcast_mtu < priv->admin_mtu)
257 		ipoib_dbg(priv, "MTU must be smaller than the underlying "
258 				"link layer MTU - 4 (%u)\n", priv->mcast_mtu);
259 
260 	new_mtu = min(priv->mcast_mtu, priv->admin_mtu);
261 
262 	if (priv->rn_ops->ndo_change_mtu) {
263 		bool carrier_status = netif_carrier_ok(dev);
264 
265 		netif_carrier_off(dev);
266 
267 		/* notify lower level on the real mtu */
268 		ret = priv->rn_ops->ndo_change_mtu(dev, new_mtu);
269 
270 		if (carrier_status)
271 			netif_carrier_on(dev);
272 	} else {
273 		dev->mtu = new_mtu;
274 	}
275 
276 	return ret;
277 }
278 
279 static void ipoib_get_stats(struct net_device *dev,
280 			    struct rtnl_link_stats64 *stats)
281 {
282 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
283 
284 	if (priv->rn_ops->ndo_get_stats64)
285 		priv->rn_ops->ndo_get_stats64(dev, stats);
286 	else
287 		netdev_stats_to_stats64(stats, &dev->stats);
288 }
289 
290 /* Called with an RCU read lock taken */
291 static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
292 					struct net_device *dev)
293 {
294 	struct net *net = dev_net(dev);
295 	struct in_device *in_dev;
296 	struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
297 	struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
298 	__be32 ret_addr;
299 
300 	switch (addr->sa_family) {
301 	case AF_INET:
302 		in_dev = in_dev_get(dev);
303 		if (!in_dev)
304 			return false;
305 
306 		ret_addr = inet_confirm_addr(net, in_dev, 0,
307 					     addr_in->sin_addr.s_addr,
308 					     RT_SCOPE_HOST);
309 		in_dev_put(in_dev);
310 		if (ret_addr)
311 			return true;
312 
313 		break;
314 	case AF_INET6:
315 		if (IS_ENABLED(CONFIG_IPV6) &&
316 		    ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
317 			return true;
318 
319 		break;
320 	}
321 	return false;
322 }
323 
324 /**
325  * Find the master net_device on top of the given net_device.
326  * @dev: base IPoIB net_device
327  *
328  * Returns the master net_device with a reference held, or the same net_device
329  * if no master exists.
330  */
331 static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
332 {
333 	struct net_device *master;
334 
335 	rcu_read_lock();
336 	master = netdev_master_upper_dev_get_rcu(dev);
337 	if (master)
338 		dev_hold(master);
339 	rcu_read_unlock();
340 
341 	if (master)
342 		return master;
343 
344 	dev_hold(dev);
345 	return dev;
346 }
347 
348 struct ipoib_walk_data {
349 	const struct sockaddr *addr;
350 	struct net_device *result;
351 };
352 
353 static int ipoib_upper_walk(struct net_device *upper, void *_data)
354 {
355 	struct ipoib_walk_data *data = _data;
356 	int ret = 0;
357 
358 	if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) {
359 		dev_hold(upper);
360 		data->result = upper;
361 		ret = 1;
362 	}
363 
364 	return ret;
365 }
366 
367 /**
368  * Find a net_device matching the given address, which is an upper device of
369  * the given net_device.
370  * @addr: IP address to look for.
371  * @dev: base IPoIB net_device
372  *
373  * If found, returns the net_device with a reference held. Otherwise return
374  * NULL.
375  */
376 static struct net_device *ipoib_get_net_dev_match_addr(
377 		const struct sockaddr *addr, struct net_device *dev)
378 {
379 	struct ipoib_walk_data data = {
380 		.addr = addr,
381 	};
382 
383 	rcu_read_lock();
384 	if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
385 		dev_hold(dev);
386 		data.result = dev;
387 		goto out;
388 	}
389 
390 	netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &data);
391 out:
392 	rcu_read_unlock();
393 	return data.result;
394 }
395 
396 /* returns the number of IPoIB netdevs on top a given ipoib device matching a
397  * pkey_index and address, if one exists.
398  *
399  * @found_net_dev: contains a matching net_device if the return value >= 1,
400  * with a reference held. */
401 static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
402 				     const union ib_gid *gid,
403 				     u16 pkey_index,
404 				     const struct sockaddr *addr,
405 				     int nesting,
406 				     struct net_device **found_net_dev)
407 {
408 	struct ipoib_dev_priv *child_priv;
409 	struct net_device *net_dev = NULL;
410 	int matches = 0;
411 
412 	if (priv->pkey_index == pkey_index &&
413 	    (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
414 		if (!addr) {
415 			net_dev = ipoib_get_master_net_dev(priv->dev);
416 		} else {
417 			/* Verify the net_device matches the IP address, as
418 			 * IPoIB child devices currently share a GID. */
419 			net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
420 		}
421 		if (net_dev) {
422 			if (!*found_net_dev)
423 				*found_net_dev = net_dev;
424 			else
425 				dev_put(net_dev);
426 			++matches;
427 		}
428 	}
429 
430 	/* Check child interfaces */
431 	down_read_nested(&priv->vlan_rwsem, nesting);
432 	list_for_each_entry(child_priv, &priv->child_intfs, list) {
433 		matches += ipoib_match_gid_pkey_addr(child_priv, gid,
434 						    pkey_index, addr,
435 						    nesting + 1,
436 						    found_net_dev);
437 		if (matches > 1)
438 			break;
439 	}
440 	up_read(&priv->vlan_rwsem);
441 
442 	return matches;
443 }
444 
445 /* Returns the number of matching net_devs found (between 0 and 2). Also
446  * return the matching net_device in the @net_dev parameter, holding a
447  * reference to the net_device, if the number of matches >= 1 */
448 static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
449 					 u16 pkey_index,
450 					 const union ib_gid *gid,
451 					 const struct sockaddr *addr,
452 					 struct net_device **net_dev)
453 {
454 	struct ipoib_dev_priv *priv;
455 	int matches = 0;
456 
457 	*net_dev = NULL;
458 
459 	list_for_each_entry(priv, dev_list, list) {
460 		if (priv->port != port)
461 			continue;
462 
463 		matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
464 						     addr, 0, net_dev);
465 		if (matches > 1)
466 			break;
467 	}
468 
469 	return matches;
470 }
471 
472 static struct net_device *ipoib_get_net_dev_by_params(
473 		struct ib_device *dev, u8 port, u16 pkey,
474 		const union ib_gid *gid, const struct sockaddr *addr,
475 		void *client_data)
476 {
477 	struct net_device *net_dev;
478 	struct list_head *dev_list = client_data;
479 	u16 pkey_index;
480 	int matches;
481 	int ret;
482 
483 	if (!rdma_protocol_ib(dev, port))
484 		return NULL;
485 
486 	ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
487 	if (ret)
488 		return NULL;
489 
490 	if (!dev_list)
491 		return NULL;
492 
493 	/* See if we can find a unique device matching the L2 parameters */
494 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
495 						gid, NULL, &net_dev);
496 
497 	switch (matches) {
498 	case 0:
499 		return NULL;
500 	case 1:
501 		return net_dev;
502 	}
503 
504 	dev_put(net_dev);
505 
506 	/* Couldn't find a unique device with L2 parameters only. Use L3
507 	 * address to uniquely match the net device */
508 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
509 						gid, addr, &net_dev);
510 	switch (matches) {
511 	case 0:
512 		return NULL;
513 	default:
514 		dev_warn_ratelimited(&dev->dev,
515 				     "duplicate IP address detected\n");
516 		/* Fall through */
517 	case 1:
518 		return net_dev;
519 	}
520 }
521 
522 int ipoib_set_mode(struct net_device *dev, const char *buf)
523 {
524 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
525 
526 	if ((test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
527 	     !strcmp(buf, "connected\n")) ||
528 	     (!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
529 	     !strcmp(buf, "datagram\n"))) {
530 		return 0;
531 	}
532 
533 	/* flush paths if we switch modes so that connections are restarted */
534 	if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
535 		set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
536 		ipoib_warn(priv, "enabling connected mode "
537 			   "will cause multicast packet drops\n");
538 		netdev_update_features(dev);
539 		dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
540 		rtnl_unlock();
541 		priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
542 
543 		ipoib_flush_paths(dev);
544 		return (!rtnl_trylock()) ? -EBUSY : 0;
545 	}
546 
547 	if (!strcmp(buf, "datagram\n")) {
548 		clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
549 		netdev_update_features(dev);
550 		dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
551 		rtnl_unlock();
552 		ipoib_flush_paths(dev);
553 		return (!rtnl_trylock()) ? -EBUSY : 0;
554 	}
555 
556 	return -EINVAL;
557 }
558 
559 struct ipoib_path *__path_find(struct net_device *dev, void *gid)
560 {
561 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
562 	struct rb_node *n = priv->path_tree.rb_node;
563 	struct ipoib_path *path;
564 	int ret;
565 
566 	while (n) {
567 		path = rb_entry(n, struct ipoib_path, rb_node);
568 
569 		ret = memcmp(gid, path->pathrec.dgid.raw,
570 			     sizeof (union ib_gid));
571 
572 		if (ret < 0)
573 			n = n->rb_left;
574 		else if (ret > 0)
575 			n = n->rb_right;
576 		else
577 			return path;
578 	}
579 
580 	return NULL;
581 }
582 
583 static int __path_add(struct net_device *dev, struct ipoib_path *path)
584 {
585 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
586 	struct rb_node **n = &priv->path_tree.rb_node;
587 	struct rb_node *pn = NULL;
588 	struct ipoib_path *tpath;
589 	int ret;
590 
591 	while (*n) {
592 		pn = *n;
593 		tpath = rb_entry(pn, struct ipoib_path, rb_node);
594 
595 		ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
596 			     sizeof (union ib_gid));
597 		if (ret < 0)
598 			n = &pn->rb_left;
599 		else if (ret > 0)
600 			n = &pn->rb_right;
601 		else
602 			return -EEXIST;
603 	}
604 
605 	rb_link_node(&path->rb_node, pn, n);
606 	rb_insert_color(&path->rb_node, &priv->path_tree);
607 
608 	list_add_tail(&path->list, &priv->path_list);
609 
610 	return 0;
611 }
612 
613 static void path_free(struct net_device *dev, struct ipoib_path *path)
614 {
615 	struct sk_buff *skb;
616 
617 	while ((skb = __skb_dequeue(&path->queue)))
618 		dev_kfree_skb_irq(skb);
619 
620 	ipoib_dbg(ipoib_priv(dev), "path_free\n");
621 
622 	/* remove all neigh connected to this path */
623 	ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
624 
625 	if (path->ah)
626 		ipoib_put_ah(path->ah);
627 
628 	kfree(path);
629 }
630 
631 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
632 
633 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
634 {
635 	struct ipoib_path_iter *iter;
636 
637 	iter = kmalloc(sizeof *iter, GFP_KERNEL);
638 	if (!iter)
639 		return NULL;
640 
641 	iter->dev = dev;
642 	memset(iter->path.pathrec.dgid.raw, 0, 16);
643 
644 	if (ipoib_path_iter_next(iter)) {
645 		kfree(iter);
646 		return NULL;
647 	}
648 
649 	return iter;
650 }
651 
652 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
653 {
654 	struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
655 	struct rb_node *n;
656 	struct ipoib_path *path;
657 	int ret = 1;
658 
659 	spin_lock_irq(&priv->lock);
660 
661 	n = rb_first(&priv->path_tree);
662 
663 	while (n) {
664 		path = rb_entry(n, struct ipoib_path, rb_node);
665 
666 		if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
667 			   sizeof (union ib_gid)) < 0) {
668 			iter->path = *path;
669 			ret = 0;
670 			break;
671 		}
672 
673 		n = rb_next(n);
674 	}
675 
676 	spin_unlock_irq(&priv->lock);
677 
678 	return ret;
679 }
680 
681 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
682 			  struct ipoib_path *path)
683 {
684 	*path = iter->path;
685 }
686 
687 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
688 
689 void ipoib_mark_paths_invalid(struct net_device *dev)
690 {
691 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
692 	struct ipoib_path *path, *tp;
693 
694 	spin_lock_irq(&priv->lock);
695 
696 	list_for_each_entry_safe(path, tp, &priv->path_list, list) {
697 		ipoib_dbg(priv, "mark path LID 0x%08x GID %pI6 invalid\n",
698 			  be32_to_cpu(sa_path_get_dlid(&path->pathrec)),
699 			  path->pathrec.dgid.raw);
700 		path->valid =  0;
701 	}
702 
703 	spin_unlock_irq(&priv->lock);
704 }
705 
706 static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
707 {
708 	struct ipoib_pseudo_header *phdr;
709 
710 	phdr = skb_push(skb, sizeof(*phdr));
711 	memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
712 }
713 
714 void ipoib_flush_paths(struct net_device *dev)
715 {
716 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
717 	struct ipoib_path *path, *tp;
718 	LIST_HEAD(remove_list);
719 	unsigned long flags;
720 
721 	netif_tx_lock_bh(dev);
722 	spin_lock_irqsave(&priv->lock, flags);
723 
724 	list_splice_init(&priv->path_list, &remove_list);
725 
726 	list_for_each_entry(path, &remove_list, list)
727 		rb_erase(&path->rb_node, &priv->path_tree);
728 
729 	list_for_each_entry_safe(path, tp, &remove_list, list) {
730 		if (path->query)
731 			ib_sa_cancel_query(path->query_id, path->query);
732 		spin_unlock_irqrestore(&priv->lock, flags);
733 		netif_tx_unlock_bh(dev);
734 		wait_for_completion(&path->done);
735 		path_free(dev, path);
736 		netif_tx_lock_bh(dev);
737 		spin_lock_irqsave(&priv->lock, flags);
738 	}
739 
740 	spin_unlock_irqrestore(&priv->lock, flags);
741 	netif_tx_unlock_bh(dev);
742 }
743 
744 static void path_rec_completion(int status,
745 				struct sa_path_rec *pathrec,
746 				void *path_ptr)
747 {
748 	struct ipoib_path *path = path_ptr;
749 	struct net_device *dev = path->dev;
750 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
751 	struct ipoib_ah *ah = NULL;
752 	struct ipoib_ah *old_ah = NULL;
753 	struct ipoib_neigh *neigh, *tn;
754 	struct sk_buff_head skqueue;
755 	struct sk_buff *skb;
756 	unsigned long flags;
757 
758 	if (!status)
759 		ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
760 			  be32_to_cpu(sa_path_get_dlid(pathrec)),
761 			  pathrec->dgid.raw);
762 	else
763 		ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
764 			  status, path->pathrec.dgid.raw);
765 
766 	skb_queue_head_init(&skqueue);
767 
768 	if (!status) {
769 		struct rdma_ah_attr av;
770 
771 		if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
772 			ah = ipoib_create_ah(dev, priv->pd, &av);
773 	}
774 
775 	spin_lock_irqsave(&priv->lock, flags);
776 
777 	if (!IS_ERR_OR_NULL(ah)) {
778 		path->pathrec = *pathrec;
779 
780 		old_ah   = path->ah;
781 		path->ah = ah;
782 
783 		ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
784 			  ah, be32_to_cpu(sa_path_get_dlid(pathrec)),
785 			  pathrec->sl);
786 
787 		while ((skb = __skb_dequeue(&path->queue)))
788 			__skb_queue_tail(&skqueue, skb);
789 
790 		list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
791 			if (neigh->ah) {
792 				WARN_ON(neigh->ah != old_ah);
793 				/*
794 				 * Dropping the ah reference inside
795 				 * priv->lock is safe here, because we
796 				 * will hold one more reference from
797 				 * the original value of path->ah (ie
798 				 * old_ah).
799 				 */
800 				ipoib_put_ah(neigh->ah);
801 			}
802 			kref_get(&path->ah->ref);
803 			neigh->ah = path->ah;
804 
805 			if (ipoib_cm_enabled(dev, neigh->daddr)) {
806 				if (!ipoib_cm_get(neigh))
807 					ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
808 									       path,
809 									       neigh));
810 				if (!ipoib_cm_get(neigh)) {
811 					ipoib_neigh_free(neigh);
812 					continue;
813 				}
814 			}
815 
816 			while ((skb = __skb_dequeue(&neigh->queue)))
817 				__skb_queue_tail(&skqueue, skb);
818 		}
819 		path->valid = 1;
820 	}
821 
822 	path->query = NULL;
823 	complete(&path->done);
824 
825 	spin_unlock_irqrestore(&priv->lock, flags);
826 
827 	if (IS_ERR_OR_NULL(ah))
828 		ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
829 
830 	if (old_ah)
831 		ipoib_put_ah(old_ah);
832 
833 	while ((skb = __skb_dequeue(&skqueue))) {
834 		int ret;
835 		skb->dev = dev;
836 		ret = dev_queue_xmit(skb);
837 		if (ret)
838 			ipoib_warn(priv, "%s: dev_queue_xmit failed to re-queue packet, ret:%d\n",
839 				   __func__, ret);
840 	}
841 }
842 
843 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
844 {
845 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
846 	struct ipoib_path *path;
847 
848 	if (!priv->broadcast)
849 		return NULL;
850 
851 	path = kzalloc(sizeof *path, GFP_ATOMIC);
852 	if (!path)
853 		return NULL;
854 
855 	path->dev = dev;
856 
857 	skb_queue_head_init(&path->queue);
858 
859 	INIT_LIST_HEAD(&path->neigh_list);
860 
861 	if (rdma_cap_opa_ah(priv->ca, priv->port))
862 		path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA;
863 	else
864 		path->pathrec.rec_type = SA_PATH_REC_TYPE_IB;
865 	memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
866 	path->pathrec.sgid	    = priv->local_gid;
867 	path->pathrec.pkey	    = cpu_to_be16(priv->pkey);
868 	path->pathrec.numb_path     = 1;
869 	path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
870 
871 	return path;
872 }
873 
874 static int path_rec_start(struct net_device *dev,
875 			  struct ipoib_path *path)
876 {
877 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
878 
879 	ipoib_dbg(priv, "Start path record lookup for %pI6\n",
880 		  path->pathrec.dgid.raw);
881 
882 	init_completion(&path->done);
883 
884 	path->query_id =
885 		ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
886 				   &path->pathrec,
887 				   IB_SA_PATH_REC_DGID		|
888 				   IB_SA_PATH_REC_SGID		|
889 				   IB_SA_PATH_REC_NUMB_PATH	|
890 				   IB_SA_PATH_REC_TRAFFIC_CLASS |
891 				   IB_SA_PATH_REC_PKEY,
892 				   1000, GFP_ATOMIC,
893 				   path_rec_completion,
894 				   path, &path->query);
895 	if (path->query_id < 0) {
896 		ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
897 		path->query = NULL;
898 		complete(&path->done);
899 		return path->query_id;
900 	}
901 
902 	return 0;
903 }
904 
905 static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
906 			   struct net_device *dev)
907 {
908 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
909 	struct rdma_netdev *rn = netdev_priv(dev);
910 	struct ipoib_path *path;
911 	struct ipoib_neigh *neigh;
912 	unsigned long flags;
913 
914 	spin_lock_irqsave(&priv->lock, flags);
915 	neigh = ipoib_neigh_alloc(daddr, dev);
916 	if (!neigh) {
917 		spin_unlock_irqrestore(&priv->lock, flags);
918 		++dev->stats.tx_dropped;
919 		dev_kfree_skb_any(skb);
920 		return;
921 	}
922 
923 	path = __path_find(dev, daddr + 4);
924 	if (!path) {
925 		path = path_rec_create(dev, daddr + 4);
926 		if (!path)
927 			goto err_path;
928 
929 		__path_add(dev, path);
930 	}
931 
932 	list_add_tail(&neigh->list, &path->neigh_list);
933 
934 	if (path->ah) {
935 		kref_get(&path->ah->ref);
936 		neigh->ah = path->ah;
937 
938 		if (ipoib_cm_enabled(dev, neigh->daddr)) {
939 			if (!ipoib_cm_get(neigh))
940 				ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
941 			if (!ipoib_cm_get(neigh)) {
942 				ipoib_neigh_free(neigh);
943 				goto err_drop;
944 			}
945 			if (skb_queue_len(&neigh->queue) <
946 			    IPOIB_MAX_PATH_REC_QUEUE) {
947 				push_pseudo_header(skb, neigh->daddr);
948 				__skb_queue_tail(&neigh->queue, skb);
949 			} else {
950 				ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
951 					   skb_queue_len(&neigh->queue));
952 				goto err_drop;
953 			}
954 		} else {
955 			spin_unlock_irqrestore(&priv->lock, flags);
956 			path->ah->last_send = rn->send(dev, skb, path->ah->ah,
957 						       IPOIB_QPN(daddr));
958 			ipoib_neigh_put(neigh);
959 			return;
960 		}
961 	} else {
962 		neigh->ah  = NULL;
963 
964 		if (!path->query && path_rec_start(dev, path))
965 			goto err_path;
966 		if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
967 			push_pseudo_header(skb, neigh->daddr);
968 			__skb_queue_tail(&neigh->queue, skb);
969 		} else {
970 			goto err_drop;
971 		}
972 	}
973 
974 	spin_unlock_irqrestore(&priv->lock, flags);
975 	ipoib_neigh_put(neigh);
976 	return;
977 
978 err_path:
979 	ipoib_neigh_free(neigh);
980 err_drop:
981 	++dev->stats.tx_dropped;
982 	dev_kfree_skb_any(skb);
983 
984 	spin_unlock_irqrestore(&priv->lock, flags);
985 	ipoib_neigh_put(neigh);
986 }
987 
988 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
989 			     struct ipoib_pseudo_header *phdr)
990 {
991 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
992 	struct rdma_netdev *rn = netdev_priv(dev);
993 	struct ipoib_path *path;
994 	unsigned long flags;
995 
996 	spin_lock_irqsave(&priv->lock, flags);
997 
998 	path = __path_find(dev, phdr->hwaddr + 4);
999 	if (!path || !path->valid) {
1000 		int new_path = 0;
1001 
1002 		if (!path) {
1003 			path = path_rec_create(dev, phdr->hwaddr + 4);
1004 			new_path = 1;
1005 		}
1006 		if (path) {
1007 			if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1008 				push_pseudo_header(skb, phdr->hwaddr);
1009 				__skb_queue_tail(&path->queue, skb);
1010 			} else {
1011 				++dev->stats.tx_dropped;
1012 				dev_kfree_skb_any(skb);
1013 			}
1014 
1015 			if (!path->query && path_rec_start(dev, path)) {
1016 				spin_unlock_irqrestore(&priv->lock, flags);
1017 				if (new_path)
1018 					path_free(dev, path);
1019 				return;
1020 			} else
1021 				__path_add(dev, path);
1022 		} else {
1023 			++dev->stats.tx_dropped;
1024 			dev_kfree_skb_any(skb);
1025 		}
1026 
1027 		spin_unlock_irqrestore(&priv->lock, flags);
1028 		return;
1029 	}
1030 
1031 	if (path->ah) {
1032 		ipoib_dbg(priv, "Send unicast ARP to %08x\n",
1033 			  be32_to_cpu(sa_path_get_dlid(&path->pathrec)));
1034 
1035 		spin_unlock_irqrestore(&priv->lock, flags);
1036 		path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1037 					       IPOIB_QPN(phdr->hwaddr));
1038 		return;
1039 	} else if ((path->query || !path_rec_start(dev, path)) &&
1040 		   skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1041 		push_pseudo_header(skb, phdr->hwaddr);
1042 		__skb_queue_tail(&path->queue, skb);
1043 	} else {
1044 		++dev->stats.tx_dropped;
1045 		dev_kfree_skb_any(skb);
1046 	}
1047 
1048 	spin_unlock_irqrestore(&priv->lock, flags);
1049 }
1050 
1051 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1052 {
1053 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1054 	struct rdma_netdev *rn = netdev_priv(dev);
1055 	struct ipoib_neigh *neigh;
1056 	struct ipoib_pseudo_header *phdr;
1057 	struct ipoib_header *header;
1058 	unsigned long flags;
1059 
1060 	phdr = (struct ipoib_pseudo_header *) skb->data;
1061 	skb_pull(skb, sizeof(*phdr));
1062 	header = (struct ipoib_header *) skb->data;
1063 
1064 	if (unlikely(phdr->hwaddr[4] == 0xff)) {
1065 		/* multicast, arrange "if" according to probability */
1066 		if ((header->proto != htons(ETH_P_IP)) &&
1067 		    (header->proto != htons(ETH_P_IPV6)) &&
1068 		    (header->proto != htons(ETH_P_ARP)) &&
1069 		    (header->proto != htons(ETH_P_RARP)) &&
1070 		    (header->proto != htons(ETH_P_TIPC))) {
1071 			/* ethertype not supported by IPoIB */
1072 			++dev->stats.tx_dropped;
1073 			dev_kfree_skb_any(skb);
1074 			return NETDEV_TX_OK;
1075 		}
1076 		/* Add in the P_Key for multicast*/
1077 		phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1078 		phdr->hwaddr[9] = priv->pkey & 0xff;
1079 
1080 		neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1081 		if (likely(neigh))
1082 			goto send_using_neigh;
1083 		ipoib_mcast_send(dev, phdr->hwaddr, skb);
1084 		return NETDEV_TX_OK;
1085 	}
1086 
1087 	/* unicast, arrange "switch" according to probability */
1088 	switch (header->proto) {
1089 	case htons(ETH_P_IP):
1090 	case htons(ETH_P_IPV6):
1091 	case htons(ETH_P_TIPC):
1092 		neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1093 		if (unlikely(!neigh)) {
1094 			neigh_add_path(skb, phdr->hwaddr, dev);
1095 			return NETDEV_TX_OK;
1096 		}
1097 		break;
1098 	case htons(ETH_P_ARP):
1099 	case htons(ETH_P_RARP):
1100 		/* for unicast ARP and RARP should always perform path find */
1101 		unicast_arp_send(skb, dev, phdr);
1102 		return NETDEV_TX_OK;
1103 	default:
1104 		/* ethertype not supported by IPoIB */
1105 		++dev->stats.tx_dropped;
1106 		dev_kfree_skb_any(skb);
1107 		return NETDEV_TX_OK;
1108 	}
1109 
1110 send_using_neigh:
1111 	/* note we now hold a ref to neigh */
1112 	if (ipoib_cm_get(neigh)) {
1113 		if (ipoib_cm_up(neigh)) {
1114 			ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1115 			goto unref;
1116 		}
1117 	} else if (neigh->ah) {
1118 		neigh->ah->last_send = rn->send(dev, skb, neigh->ah->ah,
1119 						IPOIB_QPN(phdr->hwaddr));
1120 		goto unref;
1121 	}
1122 
1123 	if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1124 		push_pseudo_header(skb, phdr->hwaddr);
1125 		spin_lock_irqsave(&priv->lock, flags);
1126 		__skb_queue_tail(&neigh->queue, skb);
1127 		spin_unlock_irqrestore(&priv->lock, flags);
1128 	} else {
1129 		++dev->stats.tx_dropped;
1130 		dev_kfree_skb_any(skb);
1131 	}
1132 
1133 unref:
1134 	ipoib_neigh_put(neigh);
1135 
1136 	return NETDEV_TX_OK;
1137 }
1138 
1139 static void ipoib_timeout(struct net_device *dev)
1140 {
1141 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1142 
1143 	ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
1144 		   jiffies_to_msecs(jiffies - dev_trans_start(dev)));
1145 	ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1146 		   netif_queue_stopped(dev),
1147 		   priv->tx_head, priv->tx_tail);
1148 	/* XXX reset QP, etc. */
1149 }
1150 
1151 static int ipoib_hard_header(struct sk_buff *skb,
1152 			     struct net_device *dev,
1153 			     unsigned short type,
1154 			     const void *daddr, const void *saddr, unsigned len)
1155 {
1156 	struct ipoib_header *header;
1157 
1158 	header = skb_push(skb, sizeof *header);
1159 
1160 	header->proto = htons(type);
1161 	header->reserved = 0;
1162 
1163 	/*
1164 	 * we don't rely on dst_entry structure,  always stuff the
1165 	 * destination address into skb hard header so we can figure out where
1166 	 * to send the packet later.
1167 	 */
1168 	push_pseudo_header(skb, daddr);
1169 
1170 	return IPOIB_HARD_LEN;
1171 }
1172 
1173 static void ipoib_set_mcast_list(struct net_device *dev)
1174 {
1175 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1176 
1177 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1178 		ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1179 		return;
1180 	}
1181 
1182 	queue_work(priv->wq, &priv->restart_task);
1183 }
1184 
1185 static int ipoib_get_iflink(const struct net_device *dev)
1186 {
1187 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1188 
1189 	/* parent interface */
1190 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1191 		return dev->ifindex;
1192 
1193 	/* child/vlan interface */
1194 	return priv->parent->ifindex;
1195 }
1196 
1197 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
1198 {
1199 	/*
1200 	 * Use only the address parts that contributes to spreading
1201 	 * The subnet prefix is not used as one can not connect to
1202 	 * same remote port (GUID) using the same remote QPN via two
1203 	 * different subnets.
1204 	 */
1205 	 /* qpn octets[1:4) & port GUID octets[12:20) */
1206 	u32 *d32 = (u32 *) daddr;
1207 	u32 hv;
1208 
1209 	hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
1210 	return hv & htbl->mask;
1211 }
1212 
1213 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1214 {
1215 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1216 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1217 	struct ipoib_neigh_hash *htbl;
1218 	struct ipoib_neigh *neigh = NULL;
1219 	u32 hash_val;
1220 
1221 	rcu_read_lock_bh();
1222 
1223 	htbl = rcu_dereference_bh(ntbl->htbl);
1224 
1225 	if (!htbl)
1226 		goto out_unlock;
1227 
1228 	hash_val = ipoib_addr_hash(htbl, daddr);
1229 	for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1230 	     neigh != NULL;
1231 	     neigh = rcu_dereference_bh(neigh->hnext)) {
1232 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1233 			/* found, take one ref on behalf of the caller */
1234 			if (!atomic_inc_not_zero(&neigh->refcnt)) {
1235 				/* deleted */
1236 				neigh = NULL;
1237 				goto out_unlock;
1238 			}
1239 
1240 			if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1241 				neigh->alive = jiffies;
1242 			goto out_unlock;
1243 		}
1244 	}
1245 
1246 out_unlock:
1247 	rcu_read_unlock_bh();
1248 	return neigh;
1249 }
1250 
1251 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1252 {
1253 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1254 	struct ipoib_neigh_hash *htbl;
1255 	unsigned long neigh_obsolete;
1256 	unsigned long dt;
1257 	unsigned long flags;
1258 	int i;
1259 	LIST_HEAD(remove_list);
1260 
1261 	if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1262 		return;
1263 
1264 	spin_lock_irqsave(&priv->lock, flags);
1265 
1266 	htbl = rcu_dereference_protected(ntbl->htbl,
1267 					 lockdep_is_held(&priv->lock));
1268 
1269 	if (!htbl)
1270 		goto out_unlock;
1271 
1272 	/* neigh is obsolete if it was idle for two GC periods */
1273 	dt = 2 * arp_tbl.gc_interval;
1274 	neigh_obsolete = jiffies - dt;
1275 	/* handle possible race condition */
1276 	if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1277 		goto out_unlock;
1278 
1279 	for (i = 0; i < htbl->size; i++) {
1280 		struct ipoib_neigh *neigh;
1281 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1282 
1283 		while ((neigh = rcu_dereference_protected(*np,
1284 							  lockdep_is_held(&priv->lock))) != NULL) {
1285 			/* was the neigh idle for two GC periods */
1286 			if (time_after(neigh_obsolete, neigh->alive)) {
1287 
1288 				ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
1289 
1290 				rcu_assign_pointer(*np,
1291 						   rcu_dereference_protected(neigh->hnext,
1292 									     lockdep_is_held(&priv->lock)));
1293 				/* remove from path/mc list */
1294 				list_del_init(&neigh->list);
1295 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1296 			} else {
1297 				np = &neigh->hnext;
1298 			}
1299 
1300 		}
1301 	}
1302 
1303 out_unlock:
1304 	spin_unlock_irqrestore(&priv->lock, flags);
1305 	ipoib_mcast_remove_list(&remove_list);
1306 }
1307 
1308 static void ipoib_reap_neigh(struct work_struct *work)
1309 {
1310 	struct ipoib_dev_priv *priv =
1311 		container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1312 
1313 	__ipoib_reap_neigh(priv);
1314 
1315 	if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1316 		queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1317 				   arp_tbl.gc_interval);
1318 }
1319 
1320 
1321 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
1322 				      struct net_device *dev)
1323 {
1324 	struct ipoib_neigh *neigh;
1325 
1326 	neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
1327 	if (!neigh)
1328 		return NULL;
1329 
1330 	neigh->dev = dev;
1331 	memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
1332 	skb_queue_head_init(&neigh->queue);
1333 	INIT_LIST_HEAD(&neigh->list);
1334 	ipoib_cm_set(neigh, NULL);
1335 	/* one ref on behalf of the caller */
1336 	atomic_set(&neigh->refcnt, 1);
1337 
1338 	return neigh;
1339 }
1340 
1341 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1342 				      struct net_device *dev)
1343 {
1344 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1345 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1346 	struct ipoib_neigh_hash *htbl;
1347 	struct ipoib_neigh *neigh;
1348 	u32 hash_val;
1349 
1350 	htbl = rcu_dereference_protected(ntbl->htbl,
1351 					 lockdep_is_held(&priv->lock));
1352 	if (!htbl) {
1353 		neigh = NULL;
1354 		goto out_unlock;
1355 	}
1356 
1357 	/* need to add a new neigh, but maybe some other thread succeeded?
1358 	 * recalc hash, maybe hash resize took place so we do a search
1359 	 */
1360 	hash_val = ipoib_addr_hash(htbl, daddr);
1361 	for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1362 					       lockdep_is_held(&priv->lock));
1363 	     neigh != NULL;
1364 	     neigh = rcu_dereference_protected(neigh->hnext,
1365 					       lockdep_is_held(&priv->lock))) {
1366 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1367 			/* found, take one ref on behalf of the caller */
1368 			if (!atomic_inc_not_zero(&neigh->refcnt)) {
1369 				/* deleted */
1370 				neigh = NULL;
1371 				break;
1372 			}
1373 			neigh->alive = jiffies;
1374 			goto out_unlock;
1375 		}
1376 	}
1377 
1378 	neigh = ipoib_neigh_ctor(daddr, dev);
1379 	if (!neigh)
1380 		goto out_unlock;
1381 
1382 	/* one ref on behalf of the hash table */
1383 	atomic_inc(&neigh->refcnt);
1384 	neigh->alive = jiffies;
1385 	/* put in hash */
1386 	rcu_assign_pointer(neigh->hnext,
1387 			   rcu_dereference_protected(htbl->buckets[hash_val],
1388 						     lockdep_is_held(&priv->lock)));
1389 	rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1390 	atomic_inc(&ntbl->entries);
1391 
1392 out_unlock:
1393 
1394 	return neigh;
1395 }
1396 
1397 void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1398 {
1399 	/* neigh reference count was dropprd to zero */
1400 	struct net_device *dev = neigh->dev;
1401 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1402 	struct sk_buff *skb;
1403 	if (neigh->ah)
1404 		ipoib_put_ah(neigh->ah);
1405 	while ((skb = __skb_dequeue(&neigh->queue))) {
1406 		++dev->stats.tx_dropped;
1407 		dev_kfree_skb_any(skb);
1408 	}
1409 	if (ipoib_cm_get(neigh))
1410 		ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1411 	ipoib_dbg(ipoib_priv(dev),
1412 		  "neigh free for %06x %pI6\n",
1413 		  IPOIB_QPN(neigh->daddr),
1414 		  neigh->daddr + 4);
1415 	kfree(neigh);
1416 	if (atomic_dec_and_test(&priv->ntbl.entries)) {
1417 		if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1418 			complete(&priv->ntbl.flushed);
1419 	}
1420 }
1421 
1422 static void ipoib_neigh_reclaim(struct rcu_head *rp)
1423 {
1424 	/* Called as a result of removal from hash table */
1425 	struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1426 	/* note TX context may hold another ref */
1427 	ipoib_neigh_put(neigh);
1428 }
1429 
1430 void ipoib_neigh_free(struct ipoib_neigh *neigh)
1431 {
1432 	struct net_device *dev = neigh->dev;
1433 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1434 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1435 	struct ipoib_neigh_hash *htbl;
1436 	struct ipoib_neigh __rcu **np;
1437 	struct ipoib_neigh *n;
1438 	u32 hash_val;
1439 
1440 	htbl = rcu_dereference_protected(ntbl->htbl,
1441 					lockdep_is_held(&priv->lock));
1442 	if (!htbl)
1443 		return;
1444 
1445 	hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1446 	np = &htbl->buckets[hash_val];
1447 	for (n = rcu_dereference_protected(*np,
1448 					    lockdep_is_held(&priv->lock));
1449 	     n != NULL;
1450 	     n = rcu_dereference_protected(*np,
1451 					lockdep_is_held(&priv->lock))) {
1452 		if (n == neigh) {
1453 			/* found */
1454 			rcu_assign_pointer(*np,
1455 					   rcu_dereference_protected(neigh->hnext,
1456 								     lockdep_is_held(&priv->lock)));
1457 			/* remove from parent list */
1458 			list_del_init(&neigh->list);
1459 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1460 			return;
1461 		} else {
1462 			np = &n->hnext;
1463 		}
1464 	}
1465 }
1466 
1467 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1468 {
1469 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1470 	struct ipoib_neigh_hash *htbl;
1471 	struct ipoib_neigh __rcu **buckets;
1472 	u32 size;
1473 
1474 	clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1475 	ntbl->htbl = NULL;
1476 	htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1477 	if (!htbl)
1478 		return -ENOMEM;
1479 	set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1480 	size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1481 	buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1482 	if (!buckets) {
1483 		kfree(htbl);
1484 		return -ENOMEM;
1485 	}
1486 	htbl->size = size;
1487 	htbl->mask = (size - 1);
1488 	htbl->buckets = buckets;
1489 	RCU_INIT_POINTER(ntbl->htbl, htbl);
1490 	htbl->ntbl = ntbl;
1491 	atomic_set(&ntbl->entries, 0);
1492 
1493 	/* start garbage collection */
1494 	clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1495 	queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1496 			   arp_tbl.gc_interval);
1497 
1498 	return 0;
1499 }
1500 
1501 static void neigh_hash_free_rcu(struct rcu_head *head)
1502 {
1503 	struct ipoib_neigh_hash *htbl = container_of(head,
1504 						    struct ipoib_neigh_hash,
1505 						    rcu);
1506 	struct ipoib_neigh __rcu **buckets = htbl->buckets;
1507 	struct ipoib_neigh_table *ntbl = htbl->ntbl;
1508 
1509 	kfree(buckets);
1510 	kfree(htbl);
1511 	complete(&ntbl->deleted);
1512 }
1513 
1514 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1515 {
1516 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1517 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1518 	struct ipoib_neigh_hash *htbl;
1519 	unsigned long flags;
1520 	int i;
1521 
1522 	/* remove all neigh connected to a given path or mcast */
1523 	spin_lock_irqsave(&priv->lock, flags);
1524 
1525 	htbl = rcu_dereference_protected(ntbl->htbl,
1526 					 lockdep_is_held(&priv->lock));
1527 
1528 	if (!htbl)
1529 		goto out_unlock;
1530 
1531 	for (i = 0; i < htbl->size; i++) {
1532 		struct ipoib_neigh *neigh;
1533 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1534 
1535 		while ((neigh = rcu_dereference_protected(*np,
1536 							  lockdep_is_held(&priv->lock))) != NULL) {
1537 			/* delete neighs belong to this parent */
1538 			if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1539 				rcu_assign_pointer(*np,
1540 						   rcu_dereference_protected(neigh->hnext,
1541 									     lockdep_is_held(&priv->lock)));
1542 				/* remove from parent list */
1543 				list_del_init(&neigh->list);
1544 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1545 			} else {
1546 				np = &neigh->hnext;
1547 			}
1548 
1549 		}
1550 	}
1551 out_unlock:
1552 	spin_unlock_irqrestore(&priv->lock, flags);
1553 }
1554 
1555 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1556 {
1557 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1558 	struct ipoib_neigh_hash *htbl;
1559 	unsigned long flags;
1560 	int i, wait_flushed = 0;
1561 
1562 	init_completion(&priv->ntbl.flushed);
1563 	set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1564 
1565 	spin_lock_irqsave(&priv->lock, flags);
1566 
1567 	htbl = rcu_dereference_protected(ntbl->htbl,
1568 					lockdep_is_held(&priv->lock));
1569 	if (!htbl)
1570 		goto out_unlock;
1571 
1572 	wait_flushed = atomic_read(&priv->ntbl.entries);
1573 	if (!wait_flushed)
1574 		goto free_htbl;
1575 
1576 	for (i = 0; i < htbl->size; i++) {
1577 		struct ipoib_neigh *neigh;
1578 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1579 
1580 		while ((neigh = rcu_dereference_protected(*np,
1581 				       lockdep_is_held(&priv->lock))) != NULL) {
1582 			rcu_assign_pointer(*np,
1583 					   rcu_dereference_protected(neigh->hnext,
1584 								     lockdep_is_held(&priv->lock)));
1585 			/* remove from path/mc list */
1586 			list_del_init(&neigh->list);
1587 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1588 		}
1589 	}
1590 
1591 free_htbl:
1592 	rcu_assign_pointer(ntbl->htbl, NULL);
1593 	call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1594 
1595 out_unlock:
1596 	spin_unlock_irqrestore(&priv->lock, flags);
1597 	if (wait_flushed)
1598 		wait_for_completion(&priv->ntbl.flushed);
1599 }
1600 
1601 static void ipoib_neigh_hash_uninit(struct net_device *dev)
1602 {
1603 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1604 	int stopped;
1605 
1606 	ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1607 	init_completion(&priv->ntbl.deleted);
1608 
1609 	/* Stop GC if called at init fail need to cancel work */
1610 	stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1611 	if (!stopped)
1612 		cancel_delayed_work(&priv->neigh_reap_task);
1613 
1614 	ipoib_flush_neighs(priv);
1615 
1616 	wait_for_completion(&priv->ntbl.deleted);
1617 }
1618 
1619 static void ipoib_dev_uninit_default(struct net_device *dev)
1620 {
1621 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1622 
1623 	ipoib_transport_dev_cleanup(dev);
1624 
1625 	netif_napi_del(&priv->napi);
1626 
1627 	ipoib_cm_dev_cleanup(dev);
1628 
1629 	kfree(priv->rx_ring);
1630 	vfree(priv->tx_ring);
1631 
1632 	priv->rx_ring = NULL;
1633 	priv->tx_ring = NULL;
1634 }
1635 
1636 static int ipoib_dev_init_default(struct net_device *dev)
1637 {
1638 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1639 
1640 	netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
1641 
1642 	/* Allocate RX/TX "rings" to hold queued skbs */
1643 	priv->rx_ring =	kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1644 				GFP_KERNEL);
1645 	if (!priv->rx_ring)
1646 		goto out;
1647 
1648 	priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1649 	if (!priv->tx_ring) {
1650 		printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
1651 		       priv->ca->name, ipoib_sendq_size);
1652 		goto out_rx_ring_cleanup;
1653 	}
1654 
1655 	/* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1656 
1657 	if (ipoib_transport_dev_init(dev, priv->ca)) {
1658 		pr_warn("%s: ipoib_transport_dev_init failed\n",
1659 			priv->ca->name);
1660 		goto out_tx_ring_cleanup;
1661 	}
1662 
1663 	/* after qp created set dev address */
1664 	priv->dev->dev_addr[1] = (priv->qp->qp_num >> 16) & 0xff;
1665 	priv->dev->dev_addr[2] = (priv->qp->qp_num >>  8) & 0xff;
1666 	priv->dev->dev_addr[3] = (priv->qp->qp_num) & 0xff;
1667 
1668 	setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
1669 		    (unsigned long)dev);
1670 
1671 	return 0;
1672 
1673 out_tx_ring_cleanup:
1674 	vfree(priv->tx_ring);
1675 
1676 out_rx_ring_cleanup:
1677 	kfree(priv->rx_ring);
1678 
1679 out:
1680 	netif_napi_del(&priv->napi);
1681 	return -ENOMEM;
1682 }
1683 
1684 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1685 {
1686 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1687 	int ret = -ENOMEM;
1688 
1689 	priv->ca = ca;
1690 	priv->port = port;
1691 	priv->qp = NULL;
1692 
1693 	/*
1694 	 * the various IPoIB tasks assume they will never race against
1695 	 * themselves, so always use a single thread workqueue
1696 	 */
1697 	priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM);
1698 	if (!priv->wq) {
1699 		pr_warn("%s: failed to allocate device WQ\n", dev->name);
1700 		goto out;
1701 	}
1702 
1703 	/* create pd, which used both for control and datapath*/
1704 	priv->pd = ib_alloc_pd(priv->ca, 0);
1705 	if (IS_ERR(priv->pd)) {
1706 		pr_warn("%s: failed to allocate PD\n", ca->name);
1707 		goto clean_wq;
1708 	}
1709 
1710 	ret = priv->rn_ops->ndo_init(dev);
1711 	if (ret) {
1712 		pr_warn("%s failed to init HW resource\n", dev->name);
1713 		goto out_free_pd;
1714 	}
1715 
1716 	if (ipoib_neigh_hash_init(priv) < 0) {
1717 		pr_warn("%s failed to init neigh hash\n", dev->name);
1718 		goto out_dev_uninit;
1719 	}
1720 
1721 	if (dev->flags & IFF_UP) {
1722 		if (ipoib_ib_dev_open(dev)) {
1723 			pr_warn("%s failed to open device\n", dev->name);
1724 			ret = -ENODEV;
1725 			goto out_dev_uninit;
1726 		}
1727 	}
1728 
1729 	return 0;
1730 
1731 out_dev_uninit:
1732 	ipoib_ib_dev_cleanup(dev);
1733 
1734 out_free_pd:
1735 	if (priv->pd) {
1736 		ib_dealloc_pd(priv->pd);
1737 		priv->pd = NULL;
1738 	}
1739 
1740 clean_wq:
1741 	if (priv->wq) {
1742 		destroy_workqueue(priv->wq);
1743 		priv->wq = NULL;
1744 	}
1745 
1746 out:
1747 	return ret;
1748 }
1749 
1750 void ipoib_dev_cleanup(struct net_device *dev)
1751 {
1752 	struct ipoib_dev_priv *priv = ipoib_priv(dev), *cpriv, *tcpriv;
1753 	LIST_HEAD(head);
1754 
1755 	ASSERT_RTNL();
1756 
1757 	/* Delete any child interfaces first */
1758 	list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1759 		/* Stop GC on child */
1760 		set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1761 		cancel_delayed_work(&cpriv->neigh_reap_task);
1762 		unregister_netdevice_queue(cpriv->dev, &head);
1763 	}
1764 	unregister_netdevice_many(&head);
1765 
1766 	ipoib_neigh_hash_uninit(dev);
1767 
1768 	ipoib_ib_dev_cleanup(dev);
1769 
1770 	/* no more works over the priv->wq */
1771 	if (priv->wq) {
1772 		flush_workqueue(priv->wq);
1773 		destroy_workqueue(priv->wq);
1774 		priv->wq = NULL;
1775 	}
1776 }
1777 
1778 static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
1779 {
1780 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1781 
1782 	return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
1783 }
1784 
1785 static int ipoib_get_vf_config(struct net_device *dev, int vf,
1786 			       struct ifla_vf_info *ivf)
1787 {
1788 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1789 	int err;
1790 
1791 	err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
1792 	if (err)
1793 		return err;
1794 
1795 	ivf->vf = vf;
1796 
1797 	return 0;
1798 }
1799 
1800 static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
1801 {
1802 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1803 
1804 	if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
1805 		return -EINVAL;
1806 
1807 	return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
1808 }
1809 
1810 static int ipoib_get_vf_stats(struct net_device *dev, int vf,
1811 			      struct ifla_vf_stats *vf_stats)
1812 {
1813 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1814 
1815 	return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
1816 }
1817 
1818 static const struct header_ops ipoib_header_ops = {
1819 	.create	= ipoib_hard_header,
1820 };
1821 
1822 static const struct net_device_ops ipoib_netdev_ops_pf = {
1823 	.ndo_uninit		 = ipoib_uninit,
1824 	.ndo_open		 = ipoib_open,
1825 	.ndo_stop		 = ipoib_stop,
1826 	.ndo_change_mtu		 = ipoib_change_mtu,
1827 	.ndo_fix_features	 = ipoib_fix_features,
1828 	.ndo_start_xmit		 = ipoib_start_xmit,
1829 	.ndo_tx_timeout		 = ipoib_timeout,
1830 	.ndo_set_rx_mode	 = ipoib_set_mcast_list,
1831 	.ndo_get_iflink		 = ipoib_get_iflink,
1832 	.ndo_set_vf_link_state	 = ipoib_set_vf_link_state,
1833 	.ndo_get_vf_config	 = ipoib_get_vf_config,
1834 	.ndo_get_vf_stats	 = ipoib_get_vf_stats,
1835 	.ndo_set_vf_guid	 = ipoib_set_vf_guid,
1836 	.ndo_set_mac_address	 = ipoib_set_mac,
1837 	.ndo_get_stats64	 = ipoib_get_stats,
1838 };
1839 
1840 static const struct net_device_ops ipoib_netdev_ops_vf = {
1841 	.ndo_uninit		 = ipoib_uninit,
1842 	.ndo_open		 = ipoib_open,
1843 	.ndo_stop		 = ipoib_stop,
1844 	.ndo_change_mtu		 = ipoib_change_mtu,
1845 	.ndo_fix_features	 = ipoib_fix_features,
1846 	.ndo_start_xmit	 	 = ipoib_start_xmit,
1847 	.ndo_tx_timeout		 = ipoib_timeout,
1848 	.ndo_set_rx_mode	 = ipoib_set_mcast_list,
1849 	.ndo_get_iflink		 = ipoib_get_iflink,
1850 	.ndo_get_stats64	 = ipoib_get_stats,
1851 };
1852 
1853 void ipoib_setup_common(struct net_device *dev)
1854 {
1855 	dev->header_ops		 = &ipoib_header_ops;
1856 
1857 	ipoib_set_ethtool_ops(dev);
1858 
1859 	dev->watchdog_timeo	 = HZ;
1860 
1861 	dev->flags		|= IFF_BROADCAST | IFF_MULTICAST;
1862 
1863 	dev->hard_header_len	 = IPOIB_HARD_LEN;
1864 	dev->addr_len		 = INFINIBAND_ALEN;
1865 	dev->type		 = ARPHRD_INFINIBAND;
1866 	dev->tx_queue_len	 = ipoib_sendq_size * 2;
1867 	dev->features		 = (NETIF_F_VLAN_CHALLENGED	|
1868 				    NETIF_F_HIGHDMA);
1869 	netif_keep_dst(dev);
1870 
1871 	memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1872 }
1873 
1874 static void ipoib_build_priv(struct net_device *dev)
1875 {
1876 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1877 
1878 	priv->dev = dev;
1879 	spin_lock_init(&priv->lock);
1880 	init_rwsem(&priv->vlan_rwsem);
1881 	mutex_init(&priv->mcast_mutex);
1882 
1883 	INIT_LIST_HEAD(&priv->path_list);
1884 	INIT_LIST_HEAD(&priv->child_intfs);
1885 	INIT_LIST_HEAD(&priv->dead_ahs);
1886 	INIT_LIST_HEAD(&priv->multicast_list);
1887 
1888 	INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
1889 	INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1890 	INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
1891 	INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
1892 	INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
1893 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1894 	INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1895 	INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1896 }
1897 
1898 static const struct net_device_ops ipoib_netdev_default_pf = {
1899 	.ndo_init		 = ipoib_dev_init_default,
1900 	.ndo_uninit		 = ipoib_dev_uninit_default,
1901 	.ndo_open		 = ipoib_ib_dev_open_default,
1902 	.ndo_stop		 = ipoib_ib_dev_stop_default,
1903 };
1904 
1905 static struct net_device
1906 *ipoib_create_netdev_default(struct ib_device *hca,
1907 			     const char *name,
1908 			     unsigned char name_assign_type,
1909 			     void (*setup)(struct net_device *))
1910 {
1911 	struct net_device *dev;
1912 	struct rdma_netdev *rn;
1913 
1914 	dev = alloc_netdev((int)sizeof(struct rdma_netdev),
1915 			   name,
1916 			   name_assign_type, setup);
1917 	if (!dev)
1918 		return NULL;
1919 
1920 	rn = netdev_priv(dev);
1921 
1922 	rn->send = ipoib_send;
1923 	rn->attach_mcast = ipoib_mcast_attach;
1924 	rn->detach_mcast = ipoib_mcast_detach;
1925 	rn->free_rdma_netdev = free_netdev;
1926 	rn->hca = hca;
1927 
1928 	dev->netdev_ops = &ipoib_netdev_default_pf;
1929 
1930 	return dev;
1931 }
1932 
1933 static struct net_device *ipoib_get_netdev(struct ib_device *hca, u8 port,
1934 					   const char *name)
1935 {
1936 	struct net_device *dev;
1937 
1938 	if (hca->alloc_rdma_netdev) {
1939 		dev = hca->alloc_rdma_netdev(hca, port,
1940 					     RDMA_NETDEV_IPOIB, name,
1941 					     NET_NAME_UNKNOWN,
1942 					     ipoib_setup_common);
1943 		if (IS_ERR_OR_NULL(dev) && PTR_ERR(dev) != -EOPNOTSUPP)
1944 			return NULL;
1945 	}
1946 
1947 	if (!hca->alloc_rdma_netdev || PTR_ERR(dev) == -EOPNOTSUPP)
1948 		dev = ipoib_create_netdev_default(hca, name, NET_NAME_UNKNOWN,
1949 						  ipoib_setup_common);
1950 
1951 	return dev;
1952 }
1953 
1954 struct ipoib_dev_priv *ipoib_intf_alloc(struct ib_device *hca, u8 port,
1955 					const char *name)
1956 {
1957 	struct net_device *dev;
1958 	struct ipoib_dev_priv *priv;
1959 	struct rdma_netdev *rn;
1960 
1961 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1962 	if (!priv)
1963 		return NULL;
1964 
1965 	dev = ipoib_get_netdev(hca, port, name);
1966 	if (!dev)
1967 		goto free_priv;
1968 
1969 	priv->rn_ops = dev->netdev_ops;
1970 
1971 	/* fixme : should be after the query_cap */
1972 	if (priv->hca_caps & IB_DEVICE_VIRTUAL_FUNCTION)
1973 		dev->netdev_ops	= &ipoib_netdev_ops_vf;
1974 	else
1975 		dev->netdev_ops	= &ipoib_netdev_ops_pf;
1976 
1977 	rn = netdev_priv(dev);
1978 	rn->clnt_priv = priv;
1979 	ipoib_build_priv(dev);
1980 
1981 	return priv;
1982 free_priv:
1983 	kfree(priv);
1984 	return NULL;
1985 }
1986 
1987 static ssize_t show_pkey(struct device *dev,
1988 			 struct device_attribute *attr, char *buf)
1989 {
1990 	struct net_device *ndev = to_net_dev(dev);
1991 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1992 
1993 	return sprintf(buf, "0x%04x\n", priv->pkey);
1994 }
1995 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1996 
1997 static ssize_t show_umcast(struct device *dev,
1998 			   struct device_attribute *attr, char *buf)
1999 {
2000 	struct net_device *ndev = to_net_dev(dev);
2001 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2002 
2003 	return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
2004 }
2005 
2006 void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
2007 {
2008 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2009 
2010 	if (umcast_val > 0) {
2011 		set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2012 		ipoib_warn(priv, "ignoring multicast groups joined directly "
2013 				"by userspace\n");
2014 	} else
2015 		clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2016 }
2017 
2018 static ssize_t set_umcast(struct device *dev,
2019 			  struct device_attribute *attr,
2020 			  const char *buf, size_t count)
2021 {
2022 	unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
2023 
2024 	ipoib_set_umcast(to_net_dev(dev), umcast_val);
2025 
2026 	return count;
2027 }
2028 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
2029 
2030 int ipoib_add_umcast_attr(struct net_device *dev)
2031 {
2032 	return device_create_file(&dev->dev, &dev_attr_umcast);
2033 }
2034 
2035 static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
2036 {
2037 	struct ipoib_dev_priv *child_priv;
2038 	struct net_device *netdev = priv->dev;
2039 
2040 	netif_addr_lock_bh(netdev);
2041 
2042 	memcpy(&priv->local_gid.global.interface_id,
2043 	       &gid->global.interface_id,
2044 	       sizeof(gid->global.interface_id));
2045 	memcpy(netdev->dev_addr + 4, &priv->local_gid, sizeof(priv->local_gid));
2046 	clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2047 
2048 	netif_addr_unlock_bh(netdev);
2049 
2050 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
2051 		down_read(&priv->vlan_rwsem);
2052 		list_for_each_entry(child_priv, &priv->child_intfs, list)
2053 			set_base_guid(child_priv, gid);
2054 		up_read(&priv->vlan_rwsem);
2055 	}
2056 }
2057 
2058 static int ipoib_check_lladdr(struct net_device *dev,
2059 			      struct sockaddr_storage *ss)
2060 {
2061 	union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
2062 	int ret = 0;
2063 
2064 	netif_addr_lock_bh(dev);
2065 
2066 	/* Make sure the QPN, reserved and subnet prefix match the current
2067 	 * lladdr, it also makes sure the lladdr is unicast.
2068 	 */
2069 	if (memcmp(dev->dev_addr, ss->__data,
2070 		   4 + sizeof(gid->global.subnet_prefix)) ||
2071 	    gid->global.interface_id == 0)
2072 		ret = -EINVAL;
2073 
2074 	netif_addr_unlock_bh(dev);
2075 
2076 	return ret;
2077 }
2078 
2079 static int ipoib_set_mac(struct net_device *dev, void *addr)
2080 {
2081 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
2082 	struct sockaddr_storage *ss = addr;
2083 	int ret;
2084 
2085 	if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
2086 		return -EBUSY;
2087 
2088 	ret = ipoib_check_lladdr(dev, ss);
2089 	if (ret)
2090 		return ret;
2091 
2092 	set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
2093 
2094 	queue_work(ipoib_workqueue, &priv->flush_light);
2095 
2096 	return 0;
2097 }
2098 
2099 static ssize_t create_child(struct device *dev,
2100 			    struct device_attribute *attr,
2101 			    const char *buf, size_t count)
2102 {
2103 	int pkey;
2104 	int ret;
2105 
2106 	if (sscanf(buf, "%i", &pkey) != 1)
2107 		return -EINVAL;
2108 
2109 	if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
2110 		return -EINVAL;
2111 
2112 	/*
2113 	 * Set the full membership bit, so that we join the right
2114 	 * broadcast group, etc.
2115 	 */
2116 	pkey |= 0x8000;
2117 
2118 	ret = ipoib_vlan_add(to_net_dev(dev), pkey);
2119 
2120 	return ret ? ret : count;
2121 }
2122 static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
2123 
2124 static ssize_t delete_child(struct device *dev,
2125 			    struct device_attribute *attr,
2126 			    const char *buf, size_t count)
2127 {
2128 	int pkey;
2129 	int ret;
2130 
2131 	if (sscanf(buf, "%i", &pkey) != 1)
2132 		return -EINVAL;
2133 
2134 	if (pkey < 0 || pkey > 0xffff)
2135 		return -EINVAL;
2136 
2137 	ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
2138 
2139 	return ret ? ret : count;
2140 
2141 }
2142 static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
2143 
2144 int ipoib_add_pkey_attr(struct net_device *dev)
2145 {
2146 	return device_create_file(&dev->dev, &dev_attr_pkey);
2147 }
2148 
2149 void ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
2150 {
2151 	priv->hca_caps = hca->attrs.device_cap_flags;
2152 
2153 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
2154 		priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
2155 
2156 		if (priv->hca_caps & IB_DEVICE_UD_TSO)
2157 			priv->dev->hw_features |= NETIF_F_TSO;
2158 
2159 		priv->dev->features |= priv->dev->hw_features;
2160 	}
2161 }
2162 
2163 static struct net_device *ipoib_add_port(const char *format,
2164 					 struct ib_device *hca, u8 port)
2165 {
2166 	struct ipoib_dev_priv *priv;
2167 	struct ib_port_attr attr;
2168 	int result = -ENOMEM;
2169 
2170 	priv = ipoib_intf_alloc(hca, port, format);
2171 	if (!priv)
2172 		goto alloc_mem_failed;
2173 
2174 	SET_NETDEV_DEV(priv->dev, hca->dev.parent);
2175 	priv->dev->dev_id = port - 1;
2176 
2177 	result = ib_query_port(hca, port, &attr);
2178 	if (result) {
2179 		printk(KERN_WARNING "%s: ib_query_port %d failed\n",
2180 		       hca->name, port);
2181 		goto device_init_failed;
2182 	}
2183 
2184 	priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
2185 
2186 	/* MTU will be reset when mcast join happens */
2187 	priv->dev->mtu  = IPOIB_UD_MTU(priv->max_ib_mtu);
2188 	priv->mcast_mtu  = priv->admin_mtu = priv->dev->mtu;
2189 	priv->dev->max_mtu = IPOIB_CM_MTU;
2190 
2191 	priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
2192 
2193 	result = ib_query_pkey(hca, port, 0, &priv->pkey);
2194 	if (result) {
2195 		printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
2196 		       hca->name, port, result);
2197 		goto device_init_failed;
2198 	}
2199 
2200 	ipoib_set_dev_features(priv, hca);
2201 
2202 	/*
2203 	 * Set the full membership bit, so that we join the right
2204 	 * broadcast group, etc.
2205 	 */
2206 	priv->pkey |= 0x8000;
2207 
2208 	priv->dev->broadcast[8] = priv->pkey >> 8;
2209 	priv->dev->broadcast[9] = priv->pkey & 0xff;
2210 
2211 	result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
2212 	if (result) {
2213 		printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
2214 		       hca->name, port, result);
2215 		goto device_init_failed;
2216 	}
2217 
2218 	memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw,
2219 	       sizeof(union ib_gid));
2220 	set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2221 
2222 	result = ipoib_dev_init(priv->dev, hca, port);
2223 	if (result) {
2224 		printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
2225 		       hca->name, port, result);
2226 		goto device_init_failed;
2227 	}
2228 
2229 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
2230 			      priv->ca, ipoib_event);
2231 	result = ib_register_event_handler(&priv->event_handler);
2232 	if (result < 0) {
2233 		printk(KERN_WARNING "%s: ib_register_event_handler failed for "
2234 		       "port %d (ret = %d)\n",
2235 		       hca->name, port, result);
2236 		goto event_failed;
2237 	}
2238 
2239 	result = register_netdev(priv->dev);
2240 	if (result) {
2241 		printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
2242 		       hca->name, port, result);
2243 		goto register_failed;
2244 	}
2245 
2246 	result = -ENOMEM;
2247 	if (ipoib_cm_add_mode_attr(priv->dev))
2248 		goto sysfs_failed;
2249 	if (ipoib_add_pkey_attr(priv->dev))
2250 		goto sysfs_failed;
2251 	if (ipoib_add_umcast_attr(priv->dev))
2252 		goto sysfs_failed;
2253 	if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
2254 		goto sysfs_failed;
2255 	if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
2256 		goto sysfs_failed;
2257 
2258 	return priv->dev;
2259 
2260 sysfs_failed:
2261 	unregister_netdev(priv->dev);
2262 
2263 register_failed:
2264 	ib_unregister_event_handler(&priv->event_handler);
2265 	flush_workqueue(ipoib_workqueue);
2266 	/* Stop GC if started before flush */
2267 	set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2268 	cancel_delayed_work(&priv->neigh_reap_task);
2269 	flush_workqueue(priv->wq);
2270 
2271 event_failed:
2272 	ipoib_dev_cleanup(priv->dev);
2273 
2274 device_init_failed:
2275 	free_netdev(priv->dev);
2276 	kfree(priv);
2277 
2278 alloc_mem_failed:
2279 	return ERR_PTR(result);
2280 }
2281 
2282 static void ipoib_add_one(struct ib_device *device)
2283 {
2284 	struct list_head *dev_list;
2285 	struct net_device *dev;
2286 	struct ipoib_dev_priv *priv;
2287 	int p;
2288 	int count = 0;
2289 
2290 	dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
2291 	if (!dev_list)
2292 		return;
2293 
2294 	INIT_LIST_HEAD(dev_list);
2295 
2296 	for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
2297 		if (!rdma_protocol_ib(device, p))
2298 			continue;
2299 		dev = ipoib_add_port("ib%d", device, p);
2300 		if (!IS_ERR(dev)) {
2301 			priv = ipoib_priv(dev);
2302 			list_add_tail(&priv->list, dev_list);
2303 			count++;
2304 		}
2305 	}
2306 
2307 	if (!count) {
2308 		kfree(dev_list);
2309 		return;
2310 	}
2311 
2312 	ib_set_client_data(device, &ipoib_client, dev_list);
2313 }
2314 
2315 static void ipoib_remove_one(struct ib_device *device, void *client_data)
2316 {
2317 	struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv;
2318 	struct list_head *dev_list = client_data;
2319 
2320 	if (!dev_list)
2321 		return;
2322 
2323 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
2324 		struct rdma_netdev *rn = netdev_priv(priv->dev);
2325 
2326 		ib_unregister_event_handler(&priv->event_handler);
2327 		flush_workqueue(ipoib_workqueue);
2328 
2329 		/* mark interface in the middle of destruction */
2330 		set_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags);
2331 
2332 		rtnl_lock();
2333 		dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
2334 		rtnl_unlock();
2335 
2336 		/* Stop GC */
2337 		set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2338 		cancel_delayed_work(&priv->neigh_reap_task);
2339 		flush_workqueue(priv->wq);
2340 
2341 		unregister_netdev(priv->dev);
2342 		rn->free_rdma_netdev(priv->dev);
2343 
2344 		list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list)
2345 			kfree(cpriv);
2346 
2347 		kfree(priv);
2348 	}
2349 
2350 	kfree(dev_list);
2351 }
2352 
2353 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2354 static struct notifier_block ipoib_netdev_notifier = {
2355 	.notifier_call = ipoib_netdev_event,
2356 };
2357 #endif
2358 
2359 static int __init ipoib_init_module(void)
2360 {
2361 	int ret;
2362 
2363 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2364 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2365 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2366 
2367 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2368 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
2369 	ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
2370 #ifdef CONFIG_INFINIBAND_IPOIB_CM
2371 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2372 	ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0);
2373 #endif
2374 
2375 	/*
2376 	 * When copying small received packets, we only copy from the
2377 	 * linear data part of the SKB, so we rely on this condition.
2378 	 */
2379 	BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2380 
2381 	ret = ipoib_register_debugfs();
2382 	if (ret)
2383 		return ret;
2384 
2385 	/*
2386 	 * We create a global workqueue here that is used for all flush
2387 	 * operations.  However, if you attempt to flush a workqueue
2388 	 * from a task on that same workqueue, it deadlocks the system.
2389 	 * We want to be able to flush the tasks associated with a
2390 	 * specific net device, so we also create a workqueue for each
2391 	 * netdevice.  We queue up the tasks for that device only on
2392 	 * its private workqueue, and we only queue up flush events
2393 	 * on our global flush workqueue.  This avoids the deadlocks.
2394 	 */
2395 	ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush",
2396 						  WQ_MEM_RECLAIM);
2397 	if (!ipoib_workqueue) {
2398 		ret = -ENOMEM;
2399 		goto err_fs;
2400 	}
2401 
2402 	ib_sa_register_client(&ipoib_sa_client);
2403 
2404 	ret = ib_register_client(&ipoib_client);
2405 	if (ret)
2406 		goto err_sa;
2407 
2408 	ret = ipoib_netlink_init();
2409 	if (ret)
2410 		goto err_client;
2411 
2412 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2413 	register_netdevice_notifier(&ipoib_netdev_notifier);
2414 #endif
2415 	return 0;
2416 
2417 err_client:
2418 	ib_unregister_client(&ipoib_client);
2419 
2420 err_sa:
2421 	ib_sa_unregister_client(&ipoib_sa_client);
2422 	destroy_workqueue(ipoib_workqueue);
2423 
2424 err_fs:
2425 	ipoib_unregister_debugfs();
2426 
2427 	return ret;
2428 }
2429 
2430 static void __exit ipoib_cleanup_module(void)
2431 {
2432 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2433 	unregister_netdevice_notifier(&ipoib_netdev_notifier);
2434 #endif
2435 	ipoib_netlink_fini();
2436 	ib_unregister_client(&ipoib_client);
2437 	ib_sa_unregister_client(&ipoib_sa_client);
2438 	ipoib_unregister_debugfs();
2439 	destroy_workqueue(ipoib_workqueue);
2440 }
2441 
2442 module_init(ipoib_init_module);
2443 module_exit(ipoib_cleanup_module);
2444