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