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