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