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