11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
32a1d9b7fSRoland Dreier  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
42a1d9b7fSRoland Dreier  * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This software is available to you under a choice of one of two
71da177e4SLinus Torvalds  * licenses.  You may choose to be licensed under the terms of the GNU
81da177e4SLinus Torvalds  * General Public License (GPL) Version 2, available from the file
91da177e4SLinus Torvalds  * COPYING in the main directory of this source tree, or the
101da177e4SLinus Torvalds  * OpenIB.org BSD license below:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *     Redistribution and use in source and binary forms, with or
131da177e4SLinus Torvalds  *     without modification, are permitted provided that the following
141da177e4SLinus Torvalds  *     conditions are met:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *      - Redistributions of source code must retain the above
171da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
181da177e4SLinus Torvalds  *        disclaimer.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *      - Redistributions in binary form must reproduce the above
211da177e4SLinus Torvalds  *        copyright notice, this list of conditions and the following
221da177e4SLinus Torvalds  *        disclaimer in the documentation and/or other materials
231da177e4SLinus Torvalds  *        provided with the distribution.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
261da177e4SLinus Torvalds  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
271da177e4SLinus Torvalds  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
281da177e4SLinus Torvalds  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
291da177e4SLinus Torvalds  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
301da177e4SLinus Torvalds  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
311da177e4SLinus Torvalds  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
321da177e4SLinus Torvalds  * SOFTWARE.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include "ipoib.h"
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include <linux/module.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <linux/init.h>
401da177e4SLinus Torvalds #include <linux/slab.h>
410f485251SShirley Ma #include <linux/kernel.h>
4210313cbbSRoland Dreier #include <linux/vmalloc.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #include <linux/if_arp.h>	/* For ARPHRD_xxx */
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <linux/ip.h>
471da177e4SLinus Torvalds #include <linux/in.h>
481da177e4SLinus Torvalds 
49b63b70d8SShlomo Pongratz #include <linux/jhash.h>
50b63b70d8SShlomo Pongratz #include <net/arp.h>
51ddde896eSGuy Shapiro #include <net/addrconf.h>
52ddde896eSGuy Shapiro #include <linux/inetdevice.h>
53ddde896eSGuy Shapiro #include <rdma/ib_cache.h>
5414c85021SArnaldo Carvalho de Melo 
554b48680bSYan Burman #define DRV_VERSION "1.0.0"
564b48680bSYan Burman 
574b48680bSYan Burman const char ipoib_driver_version[] = DRV_VERSION;
584b48680bSYan Burman 
591da177e4SLinus Torvalds MODULE_AUTHOR("Roland Dreier");
601da177e4SLinus Torvalds MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
611da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL");
621da177e4SLinus Torvalds 
630f485251SShirley Ma int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
640f485251SShirley Ma int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
650f485251SShirley Ma 
660f485251SShirley Ma module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
670f485251SShirley Ma MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
680f485251SShirley Ma module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
690f485251SShirley Ma MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
700f485251SShirley Ma 
711da177e4SLinus Torvalds #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
721da177e4SLinus Torvalds int ipoib_debug_level;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds module_param_named(debug_level, ipoib_debug_level, int, 0644);
751da177e4SLinus Torvalds MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
761da177e4SLinus Torvalds #endif
771da177e4SLinus Torvalds 
781732b0efSRoland Dreier struct ipoib_path_iter {
791732b0efSRoland Dreier 	struct net_device *dev;
801732b0efSRoland Dreier 	struct ipoib_path  path;
811732b0efSRoland Dreier };
821732b0efSRoland Dreier 
831da177e4SLinus Torvalds static const u8 ipv4_bcast_addr[] = {
841da177e4SLinus Torvalds 	0x00, 0xff, 0xff, 0xff,
851da177e4SLinus Torvalds 	0xff, 0x12, 0x40, 0x1b,	0x00, 0x00, 0x00, 0x00,
861da177e4SLinus Torvalds 	0x00, 0x00, 0x00, 0x00,	0xff, 0xff, 0xff, 0xff
871da177e4SLinus Torvalds };
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds struct workqueue_struct *ipoib_workqueue;
901da177e4SLinus Torvalds 
91c1a0b23bSMichael S. Tsirkin struct ib_sa_client ipoib_sa_client;
92c1a0b23bSMichael S. Tsirkin 
931da177e4SLinus Torvalds static void ipoib_add_one(struct ib_device *device);
947c1eb45aSHaggai Eran static void ipoib_remove_one(struct ib_device *device, void *client_data);
95b63b70d8SShlomo Pongratz static void ipoib_neigh_reclaim(struct rcu_head *rp);
96ddde896eSGuy Shapiro static struct net_device *ipoib_get_net_dev_by_params(
97ddde896eSGuy Shapiro 		struct ib_device *dev, u8 port, u16 pkey,
98ddde896eSGuy Shapiro 		const union ib_gid *gid, const struct sockaddr *addr,
99ddde896eSGuy Shapiro 		void *client_data);
100492a7e67SMark Bloch static int ipoib_set_mac(struct net_device *dev, void *addr);
10131a82362SFeras Daoud static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
10231a82362SFeras Daoud 		       int cmd);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static struct ib_client ipoib_client = {
1051da177e4SLinus Torvalds 	.name   = "ipoib",
1061da177e4SLinus Torvalds 	.add    = ipoib_add_one,
107ddde896eSGuy Shapiro 	.remove = ipoib_remove_one,
108ddde896eSGuy Shapiro 	.get_net_dev_by_params = ipoib_get_net_dev_by_params,
1091da177e4SLinus Torvalds };
1101da177e4SLinus Torvalds 
111771a5258SShamir Rabinovitch #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
112771a5258SShamir Rabinovitch static int ipoib_netdev_event(struct notifier_block *this,
113771a5258SShamir Rabinovitch 			      unsigned long event, void *ptr)
114771a5258SShamir Rabinovitch {
115771a5258SShamir Rabinovitch 	struct netdev_notifier_info *ni = ptr;
116771a5258SShamir Rabinovitch 	struct net_device *dev = ni->dev;
117771a5258SShamir Rabinovitch 
118771a5258SShamir Rabinovitch 	if (dev->netdev_ops->ndo_open != ipoib_open)
119771a5258SShamir Rabinovitch 		return NOTIFY_DONE;
120771a5258SShamir Rabinovitch 
121771a5258SShamir Rabinovitch 	switch (event) {
122771a5258SShamir Rabinovitch 	case NETDEV_REGISTER:
123771a5258SShamir Rabinovitch 		ipoib_create_debug_files(dev);
124771a5258SShamir Rabinovitch 		break;
125771a5258SShamir Rabinovitch 	case NETDEV_CHANGENAME:
126771a5258SShamir Rabinovitch 		ipoib_delete_debug_files(dev);
127771a5258SShamir Rabinovitch 		ipoib_create_debug_files(dev);
128771a5258SShamir Rabinovitch 		break;
129771a5258SShamir Rabinovitch 	case NETDEV_UNREGISTER:
130771a5258SShamir Rabinovitch 		ipoib_delete_debug_files(dev);
131771a5258SShamir Rabinovitch 		break;
132771a5258SShamir Rabinovitch 	}
133771a5258SShamir Rabinovitch 
134771a5258SShamir Rabinovitch 	return NOTIFY_DONE;
135771a5258SShamir Rabinovitch }
136771a5258SShamir Rabinovitch #endif
137771a5258SShamir Rabinovitch 
1381da177e4SLinus Torvalds int ipoib_open(struct net_device *dev)
1391da177e4SLinus Torvalds {
140c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	ipoib_dbg(priv, "bringing up interface\n");
1431da177e4SLinus Torvalds 
144437708c4SMichal Schmidt 	netif_carrier_off(dev);
145437708c4SMichal Schmidt 
146e028cc55SYossi Etigin 	set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
1471da177e4SLinus Torvalds 
1483b561130SErez Shitrit 	priv->sm_fullmember_sendonly_support = false;
1493b561130SErez Shitrit 
150efc82eeeSDoug Ledford 	if (ipoib_ib_dev_open(dev)) {
151db84f880SErez Shitrit 		if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1521da177e4SLinus Torvalds 			return 0;
153b8a1b1ceSRoland Dreier 		goto err_disable;
154dd57c930SAlex Estrin 	}
155fe25c561SYossi Etigin 
1565c37077fSZhu Yanjun 	ipoib_ib_dev_up(dev);
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1591da177e4SLinus Torvalds 		struct ipoib_dev_priv *cpriv;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 		/* Bring up any child interfaces too */
162f47944ccSErez Shitrit 		down_read(&priv->vlan_rwsem);
1631da177e4SLinus Torvalds 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
1641da177e4SLinus Torvalds 			int flags;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 			flags = cpriv->dev->flags;
1671da177e4SLinus Torvalds 			if (flags & IFF_UP)
1681da177e4SLinus Torvalds 				continue;
1691da177e4SLinus Torvalds 
170567c5e13SPetr Machata 			dev_change_flags(cpriv->dev, flags | IFF_UP, NULL);
1711da177e4SLinus Torvalds 		}
172f47944ccSErez Shitrit 		up_read(&priv->vlan_rwsem);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	netif_start_queue(dev);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	return 0;
178b8a1b1ceSRoland Dreier 
179b8a1b1ceSRoland Dreier err_disable:
180b8a1b1ceSRoland Dreier 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
181b8a1b1ceSRoland Dreier 
182b8a1b1ceSRoland Dreier 	return -EINVAL;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds static int ipoib_stop(struct net_device *dev)
1861da177e4SLinus Torvalds {
187c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	ipoib_dbg(priv, "stopping interface\n");
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	netif_stop_queue(dev);
1941da177e4SLinus Torvalds 
195efc82eeeSDoug Ledford 	ipoib_ib_dev_down(dev);
196cd565b4bSErez Shitrit 	ipoib_ib_dev_stop(dev);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1991da177e4SLinus Torvalds 		struct ipoib_dev_priv *cpriv;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 		/* Bring down any child interfaces too */
202f47944ccSErez Shitrit 		down_read(&priv->vlan_rwsem);
2031da177e4SLinus Torvalds 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
2041da177e4SLinus Torvalds 			int flags;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 			flags = cpriv->dev->flags;
2071da177e4SLinus Torvalds 			if (!(flags & IFF_UP))
2081da177e4SLinus Torvalds 				continue;
2091da177e4SLinus Torvalds 
210567c5e13SPetr Machata 			dev_change_flags(cpriv->dev, flags & ~IFF_UP, NULL);
2111da177e4SLinus Torvalds 		}
212f47944ccSErez Shitrit 		up_read(&priv->vlan_rwsem);
2131da177e4SLinus Torvalds 	}
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	return 0;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2189ca36f7dSDavid S. Miller static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
2193d96c74dSMichał Mirosław {
220c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
2213d96c74dSMichał Mirosław 
2223d96c74dSMichał Mirosław 	if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
223c4268778SYuval Shaia 		features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
2243d96c74dSMichał Mirosław 
2253d96c74dSMichał Mirosław 	return features;
2263d96c74dSMichał Mirosław }
2273d96c74dSMichał Mirosław 
2281da177e4SLinus Torvalds static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
2291da177e4SLinus Torvalds {
230c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
231ed7b521dSErez Shitrit 	int ret = 0;
2321da177e4SLinus Torvalds 
233839fcabaSMichael S. Tsirkin 	/* dev->mtu > 2K ==> connected mode */
234586a6934SPradeep Satyanarayana 	if (ipoib_cm_admin_enabled(dev)) {
235586a6934SPradeep Satyanarayana 		if (new_mtu > ipoib_cm_max_mtu(dev))
236586a6934SPradeep Satyanarayana 			return -EINVAL;
237586a6934SPradeep Satyanarayana 
238839fcabaSMichael S. Tsirkin 		if (new_mtu > priv->mcast_mtu)
239839fcabaSMichael S. Tsirkin 			ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
240839fcabaSMichael S. Tsirkin 				   priv->mcast_mtu);
241586a6934SPradeep Satyanarayana 
242839fcabaSMichael S. Tsirkin 		dev->mtu = new_mtu;
243839fcabaSMichael S. Tsirkin 		return 0;
244839fcabaSMichael S. Tsirkin 	}
245839fcabaSMichael S. Tsirkin 
246142a9c28SMuhammad Sammar 	if (new_mtu < (ETH_MIN_MTU + IPOIB_ENCAP_LEN) ||
247142a9c28SMuhammad Sammar 	    new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
2481da177e4SLinus Torvalds 		return -EINVAL;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	priv->admin_mtu = new_mtu;
2511da177e4SLinus Torvalds 
25229da686dSFeras Daoud 	if (priv->mcast_mtu < priv->admin_mtu)
25329da686dSFeras Daoud 		ipoib_dbg(priv, "MTU must be smaller than the underlying "
25429da686dSFeras Daoud 				"link layer MTU - 4 (%u)\n", priv->mcast_mtu);
25529da686dSFeras Daoud 
256ed7b521dSErez Shitrit 	new_mtu = min(priv->mcast_mtu, priv->admin_mtu);
2571da177e4SLinus Torvalds 
258ed7b521dSErez Shitrit 	if (priv->rn_ops->ndo_change_mtu) {
259ed7b521dSErez Shitrit 		bool carrier_status = netif_carrier_ok(dev);
260ed7b521dSErez Shitrit 
261ed7b521dSErez Shitrit 		netif_carrier_off(dev);
262ed7b521dSErez Shitrit 
263ed7b521dSErez Shitrit 		/* notify lower level on the real mtu */
264ed7b521dSErez Shitrit 		ret = priv->rn_ops->ndo_change_mtu(dev, new_mtu);
265ed7b521dSErez Shitrit 
266ed7b521dSErez Shitrit 		if (carrier_status)
267ed7b521dSErez Shitrit 			netif_carrier_on(dev);
268ed7b521dSErez Shitrit 	} else {
269ed7b521dSErez Shitrit 		dev->mtu = new_mtu;
270ed7b521dSErez Shitrit 	}
271ed7b521dSErez Shitrit 
272ed7b521dSErez Shitrit 	return ret;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
275b6c871e5SErez Shitrit static void ipoib_get_stats(struct net_device *dev,
276b6c871e5SErez Shitrit 			    struct rtnl_link_stats64 *stats)
277b6c871e5SErez Shitrit {
278b6c871e5SErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
279b6c871e5SErez Shitrit 
280b6c871e5SErez Shitrit 	if (priv->rn_ops->ndo_get_stats64)
281b6c871e5SErez Shitrit 		priv->rn_ops->ndo_get_stats64(dev, stats);
282b6c871e5SErez Shitrit 	else
283b6c871e5SErez Shitrit 		netdev_stats_to_stats64(stats, &dev->stats);
284b6c871e5SErez Shitrit }
285b6c871e5SErez Shitrit 
286ddde896eSGuy Shapiro /* Called with an RCU read lock taken */
287ddde896eSGuy Shapiro static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
288ddde896eSGuy Shapiro 					struct net_device *dev)
289ddde896eSGuy Shapiro {
290ddde896eSGuy Shapiro 	struct net *net = dev_net(dev);
291ddde896eSGuy Shapiro 	struct in_device *in_dev;
292ddde896eSGuy Shapiro 	struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
293ddde896eSGuy Shapiro 	struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
294ddde896eSGuy Shapiro 	__be32 ret_addr;
295ddde896eSGuy Shapiro 
296ddde896eSGuy Shapiro 	switch (addr->sa_family) {
297ddde896eSGuy Shapiro 	case AF_INET:
298ddde896eSGuy Shapiro 		in_dev = in_dev_get(dev);
299ddde896eSGuy Shapiro 		if (!in_dev)
300ddde896eSGuy Shapiro 			return false;
301ddde896eSGuy Shapiro 
302ddde896eSGuy Shapiro 		ret_addr = inet_confirm_addr(net, in_dev, 0,
303ddde896eSGuy Shapiro 					     addr_in->sin_addr.s_addr,
304ddde896eSGuy Shapiro 					     RT_SCOPE_HOST);
305ddde896eSGuy Shapiro 		in_dev_put(in_dev);
306ddde896eSGuy Shapiro 		if (ret_addr)
307ddde896eSGuy Shapiro 			return true;
308ddde896eSGuy Shapiro 
309ddde896eSGuy Shapiro 		break;
310ddde896eSGuy Shapiro 	case AF_INET6:
311ddde896eSGuy Shapiro 		if (IS_ENABLED(CONFIG_IPV6) &&
312ddde896eSGuy Shapiro 		    ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
313ddde896eSGuy Shapiro 			return true;
314ddde896eSGuy Shapiro 
315ddde896eSGuy Shapiro 		break;
316ddde896eSGuy Shapiro 	}
317ddde896eSGuy Shapiro 	return false;
318ddde896eSGuy Shapiro }
319ddde896eSGuy Shapiro 
320ddde896eSGuy Shapiro /**
321ddde896eSGuy Shapiro  * Find the master net_device on top of the given net_device.
322ddde896eSGuy Shapiro  * @dev: base IPoIB net_device
323ddde896eSGuy Shapiro  *
324ddde896eSGuy Shapiro  * Returns the master net_device with a reference held, or the same net_device
325ddde896eSGuy Shapiro  * if no master exists.
326ddde896eSGuy Shapiro  */
327ddde896eSGuy Shapiro static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
328ddde896eSGuy Shapiro {
329ddde896eSGuy Shapiro 	struct net_device *master;
330ddde896eSGuy Shapiro 
331ddde896eSGuy Shapiro 	rcu_read_lock();
332ddde896eSGuy Shapiro 	master = netdev_master_upper_dev_get_rcu(dev);
333ddde896eSGuy Shapiro 	if (master)
334ddde896eSGuy Shapiro 		dev_hold(master);
335ddde896eSGuy Shapiro 	rcu_read_unlock();
336ddde896eSGuy Shapiro 
337ddde896eSGuy Shapiro 	if (master)
338ddde896eSGuy Shapiro 		return master;
339ddde896eSGuy Shapiro 
340ddde896eSGuy Shapiro 	dev_hold(dev);
341ddde896eSGuy Shapiro 	return dev;
342ddde896eSGuy Shapiro }
343ddde896eSGuy Shapiro 
344e0e79c8eSDavid Ahern struct ipoib_walk_data {
345e0e79c8eSDavid Ahern 	const struct sockaddr *addr;
346e0e79c8eSDavid Ahern 	struct net_device *result;
347e0e79c8eSDavid Ahern };
348e0e79c8eSDavid Ahern 
349e0e79c8eSDavid Ahern static int ipoib_upper_walk(struct net_device *upper, void *_data)
350e0e79c8eSDavid Ahern {
351e0e79c8eSDavid Ahern 	struct ipoib_walk_data *data = _data;
352e0e79c8eSDavid Ahern 	int ret = 0;
353e0e79c8eSDavid Ahern 
354e0e79c8eSDavid Ahern 	if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) {
355e0e79c8eSDavid Ahern 		dev_hold(upper);
356e0e79c8eSDavid Ahern 		data->result = upper;
357e0e79c8eSDavid Ahern 		ret = 1;
358e0e79c8eSDavid Ahern 	}
359e0e79c8eSDavid Ahern 
360e0e79c8eSDavid Ahern 	return ret;
361e0e79c8eSDavid Ahern }
362e0e79c8eSDavid Ahern 
363ddde896eSGuy Shapiro /**
364ddde896eSGuy Shapiro  * Find a net_device matching the given address, which is an upper device of
365ddde896eSGuy Shapiro  * the given net_device.
366ddde896eSGuy Shapiro  * @addr: IP address to look for.
367ddde896eSGuy Shapiro  * @dev: base IPoIB net_device
368ddde896eSGuy Shapiro  *
369ddde896eSGuy Shapiro  * If found, returns the net_device with a reference held. Otherwise return
370ddde896eSGuy Shapiro  * NULL.
371ddde896eSGuy Shapiro  */
372ddde896eSGuy Shapiro static struct net_device *ipoib_get_net_dev_match_addr(
373ddde896eSGuy Shapiro 		const struct sockaddr *addr, struct net_device *dev)
374ddde896eSGuy Shapiro {
375e0e79c8eSDavid Ahern 	struct ipoib_walk_data data = {
376e0e79c8eSDavid Ahern 		.addr = addr,
377e0e79c8eSDavid Ahern 	};
378ddde896eSGuy Shapiro 
379ddde896eSGuy Shapiro 	rcu_read_lock();
380ddde896eSGuy Shapiro 	if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
381ddde896eSGuy Shapiro 		dev_hold(dev);
382e0e79c8eSDavid Ahern 		data.result = dev;
383ddde896eSGuy Shapiro 		goto out;
384ddde896eSGuy Shapiro 	}
385ddde896eSGuy Shapiro 
386e0e79c8eSDavid Ahern 	netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &data);
387ddde896eSGuy Shapiro out:
388ddde896eSGuy Shapiro 	rcu_read_unlock();
389e0e79c8eSDavid Ahern 	return data.result;
390ddde896eSGuy Shapiro }
391ddde896eSGuy Shapiro 
392ddde896eSGuy Shapiro /* returns the number of IPoIB netdevs on top a given ipoib device matching a
393ddde896eSGuy Shapiro  * pkey_index and address, if one exists.
394ddde896eSGuy Shapiro  *
395ddde896eSGuy Shapiro  * @found_net_dev: contains a matching net_device if the return value >= 1,
396ddde896eSGuy Shapiro  * with a reference held. */
397ddde896eSGuy Shapiro static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
398ddde896eSGuy Shapiro 				     const union ib_gid *gid,
399ddde896eSGuy Shapiro 				     u16 pkey_index,
400ddde896eSGuy Shapiro 				     const struct sockaddr *addr,
401ddde896eSGuy Shapiro 				     int nesting,
402ddde896eSGuy Shapiro 				     struct net_device **found_net_dev)
403ddde896eSGuy Shapiro {
404ddde896eSGuy Shapiro 	struct ipoib_dev_priv *child_priv;
405ddde896eSGuy Shapiro 	struct net_device *net_dev = NULL;
406ddde896eSGuy Shapiro 	int matches = 0;
407ddde896eSGuy Shapiro 
408ddde896eSGuy Shapiro 	if (priv->pkey_index == pkey_index &&
409ddde896eSGuy Shapiro 	    (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
410ddde896eSGuy Shapiro 		if (!addr) {
411ddde896eSGuy Shapiro 			net_dev = ipoib_get_master_net_dev(priv->dev);
412ddde896eSGuy Shapiro 		} else {
413ddde896eSGuy Shapiro 			/* Verify the net_device matches the IP address, as
414ddde896eSGuy Shapiro 			 * IPoIB child devices currently share a GID. */
415ddde896eSGuy Shapiro 			net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
416ddde896eSGuy Shapiro 		}
417ddde896eSGuy Shapiro 		if (net_dev) {
418ddde896eSGuy Shapiro 			if (!*found_net_dev)
419ddde896eSGuy Shapiro 				*found_net_dev = net_dev;
420ddde896eSGuy Shapiro 			else
421ddde896eSGuy Shapiro 				dev_put(net_dev);
422ddde896eSGuy Shapiro 			++matches;
423ddde896eSGuy Shapiro 		}
424ddde896eSGuy Shapiro 	}
425ddde896eSGuy Shapiro 
426ddde896eSGuy Shapiro 	/* Check child interfaces */
427ddde896eSGuy Shapiro 	down_read_nested(&priv->vlan_rwsem, nesting);
428ddde896eSGuy Shapiro 	list_for_each_entry(child_priv, &priv->child_intfs, list) {
429ddde896eSGuy Shapiro 		matches += ipoib_match_gid_pkey_addr(child_priv, gid,
430ddde896eSGuy Shapiro 						    pkey_index, addr,
431ddde896eSGuy Shapiro 						    nesting + 1,
432ddde896eSGuy Shapiro 						    found_net_dev);
433ddde896eSGuy Shapiro 		if (matches > 1)
434ddde896eSGuy Shapiro 			break;
435ddde896eSGuy Shapiro 	}
436ddde896eSGuy Shapiro 	up_read(&priv->vlan_rwsem);
437ddde896eSGuy Shapiro 
438ddde896eSGuy Shapiro 	return matches;
439ddde896eSGuy Shapiro }
440ddde896eSGuy Shapiro 
441ddde896eSGuy Shapiro /* Returns the number of matching net_devs found (between 0 and 2). Also
442ddde896eSGuy Shapiro  * return the matching net_device in the @net_dev parameter, holding a
443ddde896eSGuy Shapiro  * reference to the net_device, if the number of matches >= 1 */
444ddde896eSGuy Shapiro static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
445ddde896eSGuy Shapiro 					 u16 pkey_index,
446ddde896eSGuy Shapiro 					 const union ib_gid *gid,
447ddde896eSGuy Shapiro 					 const struct sockaddr *addr,
448ddde896eSGuy Shapiro 					 struct net_device **net_dev)
449ddde896eSGuy Shapiro {
450ddde896eSGuy Shapiro 	struct ipoib_dev_priv *priv;
451ddde896eSGuy Shapiro 	int matches = 0;
452ddde896eSGuy Shapiro 
453ddde896eSGuy Shapiro 	*net_dev = NULL;
454ddde896eSGuy Shapiro 
455ddde896eSGuy Shapiro 	list_for_each_entry(priv, dev_list, list) {
456ddde896eSGuy Shapiro 		if (priv->port != port)
457ddde896eSGuy Shapiro 			continue;
458ddde896eSGuy Shapiro 
459ddde896eSGuy Shapiro 		matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
460ddde896eSGuy Shapiro 						     addr, 0, net_dev);
461ddde896eSGuy Shapiro 		if (matches > 1)
462ddde896eSGuy Shapiro 			break;
463ddde896eSGuy Shapiro 	}
464ddde896eSGuy Shapiro 
465ddde896eSGuy Shapiro 	return matches;
466ddde896eSGuy Shapiro }
467ddde896eSGuy Shapiro 
468ddde896eSGuy Shapiro static struct net_device *ipoib_get_net_dev_by_params(
469ddde896eSGuy Shapiro 		struct ib_device *dev, u8 port, u16 pkey,
470ddde896eSGuy Shapiro 		const union ib_gid *gid, const struct sockaddr *addr,
471ddde896eSGuy Shapiro 		void *client_data)
472ddde896eSGuy Shapiro {
473ddde896eSGuy Shapiro 	struct net_device *net_dev;
474ddde896eSGuy Shapiro 	struct list_head *dev_list = client_data;
475ddde896eSGuy Shapiro 	u16 pkey_index;
476ddde896eSGuy Shapiro 	int matches;
477ddde896eSGuy Shapiro 	int ret;
478ddde896eSGuy Shapiro 
479ddde896eSGuy Shapiro 	if (!rdma_protocol_ib(dev, port))
480ddde896eSGuy Shapiro 		return NULL;
481ddde896eSGuy Shapiro 
482ddde896eSGuy Shapiro 	ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
483ddde896eSGuy Shapiro 	if (ret)
484ddde896eSGuy Shapiro 		return NULL;
485ddde896eSGuy Shapiro 
486ddde896eSGuy Shapiro 	if (!dev_list)
487ddde896eSGuy Shapiro 		return NULL;
488ddde896eSGuy Shapiro 
489ddde896eSGuy Shapiro 	/* See if we can find a unique device matching the L2 parameters */
490ddde896eSGuy Shapiro 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
491ddde896eSGuy Shapiro 						gid, NULL, &net_dev);
492ddde896eSGuy Shapiro 
493ddde896eSGuy Shapiro 	switch (matches) {
494ddde896eSGuy Shapiro 	case 0:
495ddde896eSGuy Shapiro 		return NULL;
496ddde896eSGuy Shapiro 	case 1:
497ddde896eSGuy Shapiro 		return net_dev;
498ddde896eSGuy Shapiro 	}
499ddde896eSGuy Shapiro 
500ddde896eSGuy Shapiro 	dev_put(net_dev);
501ddde896eSGuy Shapiro 
502ddde896eSGuy Shapiro 	/* Couldn't find a unique device with L2 parameters only. Use L3
503ddde896eSGuy Shapiro 	 * address to uniquely match the net device */
504ddde896eSGuy Shapiro 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
505ddde896eSGuy Shapiro 						gid, addr, &net_dev);
506ddde896eSGuy Shapiro 	switch (matches) {
507ddde896eSGuy Shapiro 	case 0:
508ddde896eSGuy Shapiro 		return NULL;
509ddde896eSGuy Shapiro 	default:
510ddde896eSGuy Shapiro 		dev_warn_ratelimited(&dev->dev,
511ddde896eSGuy Shapiro 				     "duplicate IP address detected\n");
512ddde896eSGuy Shapiro 		/* Fall through */
513ddde896eSGuy Shapiro 	case 1:
514ddde896eSGuy Shapiro 		return net_dev;
515ddde896eSGuy Shapiro 	}
516ddde896eSGuy Shapiro }
517ddde896eSGuy Shapiro 
51871d9c5f9SRoland Dreier int ipoib_set_mode(struct net_device *dev, const char *buf)
51971d9c5f9SRoland Dreier {
520c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
52171d9c5f9SRoland Dreier 
52280b5b35aSFeras Daoud 	if ((test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
52380b5b35aSFeras Daoud 	     !strcmp(buf, "connected\n")) ||
52480b5b35aSFeras Daoud 	     (!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
52580b5b35aSFeras Daoud 	     !strcmp(buf, "datagram\n"))) {
52680b5b35aSFeras Daoud 		return 0;
52780b5b35aSFeras Daoud 	}
52880b5b35aSFeras Daoud 
52971d9c5f9SRoland Dreier 	/* flush paths if we switch modes so that connections are restarted */
53071d9c5f9SRoland Dreier 	if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
53171d9c5f9SRoland Dreier 		set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
53271d9c5f9SRoland Dreier 		ipoib_warn(priv, "enabling connected mode "
53371d9c5f9SRoland Dreier 			   "will cause multicast packet drops\n");
53471d9c5f9SRoland Dreier 		netdev_update_features(dev);
535edcd2a74SErez Shitrit 		dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
53671d9c5f9SRoland Dreier 		rtnl_unlock();
537e622f2f4SChristoph Hellwig 		priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
53871d9c5f9SRoland Dreier 
53971d9c5f9SRoland Dreier 		ipoib_flush_paths(dev);
5400a0007f2SFeras Daoud 		return (!rtnl_trylock()) ? -EBUSY : 0;
54171d9c5f9SRoland Dreier 	}
54271d9c5f9SRoland Dreier 
54371d9c5f9SRoland Dreier 	if (!strcmp(buf, "datagram\n")) {
54471d9c5f9SRoland Dreier 		clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
54571d9c5f9SRoland Dreier 		netdev_update_features(dev);
54671d9c5f9SRoland Dreier 		dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
54771d9c5f9SRoland Dreier 		rtnl_unlock();
54871d9c5f9SRoland Dreier 		ipoib_flush_paths(dev);
5490a0007f2SFeras Daoud 		return (!rtnl_trylock()) ? -EBUSY : 0;
55071d9c5f9SRoland Dreier 	}
55171d9c5f9SRoland Dreier 
55271d9c5f9SRoland Dreier 	return -EINVAL;
55371d9c5f9SRoland Dreier }
55471d9c5f9SRoland Dreier 
555546481c2SErez Shitrit struct ipoib_path *__path_find(struct net_device *dev, void *gid)
5561da177e4SLinus Torvalds {
557c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
5581da177e4SLinus Torvalds 	struct rb_node *n = priv->path_tree.rb_node;
5591da177e4SLinus Torvalds 	struct ipoib_path *path;
5601da177e4SLinus Torvalds 	int ret;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	while (n) {
5631da177e4SLinus Torvalds 		path = rb_entry(n, struct ipoib_path, rb_node);
5641da177e4SLinus Torvalds 
56537c22a77SJack Morgenstein 		ret = memcmp(gid, path->pathrec.dgid.raw,
5661da177e4SLinus Torvalds 			     sizeof (union ib_gid));
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 		if (ret < 0)
5691da177e4SLinus Torvalds 			n = n->rb_left;
5701da177e4SLinus Torvalds 		else if (ret > 0)
5711da177e4SLinus Torvalds 			n = n->rb_right;
5721da177e4SLinus Torvalds 		else
5731da177e4SLinus Torvalds 			return path;
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	return NULL;
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds static int __path_add(struct net_device *dev, struct ipoib_path *path)
5801da177e4SLinus Torvalds {
581c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
5821da177e4SLinus Torvalds 	struct rb_node **n = &priv->path_tree.rb_node;
5831da177e4SLinus Torvalds 	struct rb_node *pn = NULL;
5841da177e4SLinus Torvalds 	struct ipoib_path *tpath;
5851da177e4SLinus Torvalds 	int ret;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	while (*n) {
5881da177e4SLinus Torvalds 		pn = *n;
5891da177e4SLinus Torvalds 		tpath = rb_entry(pn, struct ipoib_path, rb_node);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 		ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
5921da177e4SLinus Torvalds 			     sizeof (union ib_gid));
5931da177e4SLinus Torvalds 		if (ret < 0)
5941da177e4SLinus Torvalds 			n = &pn->rb_left;
5951da177e4SLinus Torvalds 		else if (ret > 0)
5961da177e4SLinus Torvalds 			n = &pn->rb_right;
5971da177e4SLinus Torvalds 		else
5981da177e4SLinus Torvalds 			return -EEXIST;
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	rb_link_node(&path->rb_node, pn, n);
6021da177e4SLinus Torvalds 	rb_insert_color(&path->rb_node, &priv->path_tree);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	list_add_tail(&path->list, &priv->path_list);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	return 0;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds static void path_free(struct net_device *dev, struct ipoib_path *path)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds 	struct sk_buff *skb;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&path->queue)))
6141da177e4SLinus Torvalds 		dev_kfree_skb_irq(skb);
6151da177e4SLinus Torvalds 
616c1048affSErez Shitrit 	ipoib_dbg(ipoib_priv(dev), "path_free\n");
6171da177e4SLinus Torvalds 
618b63b70d8SShlomo Pongratz 	/* remove all neigh connected to this path */
619b63b70d8SShlomo Pongratz 	ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	if (path->ah)
6221da177e4SLinus Torvalds 		ipoib_put_ah(path->ah);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	kfree(path);
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
6271732b0efSRoland Dreier #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
6281732b0efSRoland Dreier 
6291732b0efSRoland Dreier struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
6301732b0efSRoland Dreier {
6311732b0efSRoland Dreier 	struct ipoib_path_iter *iter;
6321732b0efSRoland Dreier 
633b1b63970SKamal Heib 	iter = kmalloc(sizeof(*iter), GFP_KERNEL);
6341732b0efSRoland Dreier 	if (!iter)
6351732b0efSRoland Dreier 		return NULL;
6361732b0efSRoland Dreier 
6371732b0efSRoland Dreier 	iter->dev = dev;
6381732b0efSRoland Dreier 	memset(iter->path.pathrec.dgid.raw, 0, 16);
6391732b0efSRoland Dreier 
6401732b0efSRoland Dreier 	if (ipoib_path_iter_next(iter)) {
6411732b0efSRoland Dreier 		kfree(iter);
6421732b0efSRoland Dreier 		return NULL;
6431732b0efSRoland Dreier 	}
6441732b0efSRoland Dreier 
6451732b0efSRoland Dreier 	return iter;
6461732b0efSRoland Dreier }
6471732b0efSRoland Dreier 
6481732b0efSRoland Dreier int ipoib_path_iter_next(struct ipoib_path_iter *iter)
6491732b0efSRoland Dreier {
650c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
6511732b0efSRoland Dreier 	struct rb_node *n;
6521732b0efSRoland Dreier 	struct ipoib_path *path;
6531732b0efSRoland Dreier 	int ret = 1;
6541732b0efSRoland Dreier 
6551732b0efSRoland Dreier 	spin_lock_irq(&priv->lock);
6561732b0efSRoland Dreier 
6571732b0efSRoland Dreier 	n = rb_first(&priv->path_tree);
6581732b0efSRoland Dreier 
6591732b0efSRoland Dreier 	while (n) {
6601732b0efSRoland Dreier 		path = rb_entry(n, struct ipoib_path, rb_node);
6611732b0efSRoland Dreier 
6621732b0efSRoland Dreier 		if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
6631732b0efSRoland Dreier 			   sizeof (union ib_gid)) < 0) {
6641732b0efSRoland Dreier 			iter->path = *path;
6651732b0efSRoland Dreier 			ret = 0;
6661732b0efSRoland Dreier 			break;
6671732b0efSRoland Dreier 		}
6681732b0efSRoland Dreier 
6691732b0efSRoland Dreier 		n = rb_next(n);
6701732b0efSRoland Dreier 	}
6711732b0efSRoland Dreier 
6721732b0efSRoland Dreier 	spin_unlock_irq(&priv->lock);
6731732b0efSRoland Dreier 
6741732b0efSRoland Dreier 	return ret;
6751732b0efSRoland Dreier }
6761732b0efSRoland Dreier 
6771732b0efSRoland Dreier void ipoib_path_iter_read(struct ipoib_path_iter *iter,
6781732b0efSRoland Dreier 			  struct ipoib_path *path)
6791732b0efSRoland Dreier {
6801732b0efSRoland Dreier 	*path = iter->path;
6811732b0efSRoland Dreier }
6821732b0efSRoland Dreier 
6831732b0efSRoland Dreier #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
6841732b0efSRoland Dreier 
685ee1e2c82SMoni Shoua void ipoib_mark_paths_invalid(struct net_device *dev)
686ee1e2c82SMoni Shoua {
687c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
688ee1e2c82SMoni Shoua 	struct ipoib_path *path, *tp;
689ee1e2c82SMoni Shoua 
690ee1e2c82SMoni Shoua 	spin_lock_irq(&priv->lock);
691ee1e2c82SMoni Shoua 
692ee1e2c82SMoni Shoua 	list_for_each_entry_safe(path, tp, &priv->path_list, list) {
69357520751SDasaratharaman Chandramouli 		ipoib_dbg(priv, "mark path LID 0x%08x GID %pI6 invalid\n",
69457520751SDasaratharaman Chandramouli 			  be32_to_cpu(sa_path_get_dlid(&path->pathrec)),
695fcace2feSHarvey Harrison 			  path->pathrec.dgid.raw);
696fa9391dbSDoug Ledford 		if (path->ah)
697fa9391dbSDoug Ledford 			path->ah->valid = 0;
698ee1e2c82SMoni Shoua 	}
699ee1e2c82SMoni Shoua 
700ee1e2c82SMoni Shoua 	spin_unlock_irq(&priv->lock);
701ee1e2c82SMoni Shoua }
702ee1e2c82SMoni Shoua 
7032b084176SErez Shitrit static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
7042b084176SErez Shitrit {
7052b084176SErez Shitrit 	struct ipoib_pseudo_header *phdr;
7062b084176SErez Shitrit 
707d58ff351SJohannes Berg 	phdr = skb_push(skb, sizeof(*phdr));
7082b084176SErez Shitrit 	memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
7092b084176SErez Shitrit }
7102b084176SErez Shitrit 
7111da177e4SLinus Torvalds void ipoib_flush_paths(struct net_device *dev)
7121da177e4SLinus Torvalds {
713c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
7141da177e4SLinus Torvalds 	struct ipoib_path *path, *tp;
7151da177e4SLinus Torvalds 	LIST_HEAD(remove_list);
716943c246eSRoland Dreier 	unsigned long flags;
7171da177e4SLinus Torvalds 
718943c246eSRoland Dreier 	netif_tx_lock_bh(dev);
719943c246eSRoland Dreier 	spin_lock_irqsave(&priv->lock, flags);
7201da177e4SLinus Torvalds 
721157de229SRobert P. J. Day 	list_splice_init(&priv->path_list, &remove_list);
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	list_for_each_entry(path, &remove_list, list)
7241da177e4SLinus Torvalds 		rb_erase(&path->rb_node, &priv->path_tree);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	list_for_each_entry_safe(path, tp, &remove_list, list) {
7271da177e4SLinus Torvalds 		if (path->query)
7281da177e4SLinus Torvalds 			ib_sa_cancel_query(path->query_id, path->query);
729943c246eSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
730943c246eSRoland Dreier 		netif_tx_unlock_bh(dev);
7311da177e4SLinus Torvalds 		wait_for_completion(&path->done);
7321da177e4SLinus Torvalds 		path_free(dev, path);
733943c246eSRoland Dreier 		netif_tx_lock_bh(dev);
734943c246eSRoland Dreier 		spin_lock_irqsave(&priv->lock, flags);
7351da177e4SLinus Torvalds 	}
736943c246eSRoland Dreier 
737943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
738943c246eSRoland Dreier 	netif_tx_unlock_bh(dev);
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds static void path_rec_completion(int status,
742c2f8fc4eSDasaratharaman Chandramouli 				struct sa_path_rec *pathrec,
7431da177e4SLinus Torvalds 				void *path_ptr)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	struct ipoib_path *path = path_ptr;
7461da177e4SLinus Torvalds 	struct net_device *dev = path->dev;
747c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
7481da177e4SLinus Torvalds 	struct ipoib_ah *ah = NULL;
749c9da4badSRoland Dreier 	struct ipoib_ah *old_ah = NULL;
750d04d01b1SMichael S. Tsirkin 	struct ipoib_neigh *neigh, *tn;
7511da177e4SLinus Torvalds 	struct sk_buff_head skqueue;
7521da177e4SLinus Torvalds 	struct sk_buff *skb;
7531da177e4SLinus Torvalds 	unsigned long flags;
7541da177e4SLinus Torvalds 
755843613b0SRoland Dreier 	if (!status)
7565b095d98SHarvey Harrison 		ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
75757520751SDasaratharaman Chandramouli 			  be32_to_cpu(sa_path_get_dlid(pathrec)),
7589fdca4daSDasaratharaman Chandramouli 			  pathrec->dgid.raw);
7591da177e4SLinus Torvalds 	else
7605b095d98SHarvey Harrison 		ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
761fcace2feSHarvey Harrison 			  status, path->pathrec.dgid.raw);
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	skb_queue_head_init(&skqueue);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	if (!status) {
76690898850SDasaratharaman Chandramouli 		struct rdma_ah_attr av;
7671da177e4SLinus Torvalds 
7684ad6a024SParav Pandit 		if (!ib_init_ah_attr_from_path(priv->ca, priv->port,
76939839107SParav Pandit 					       pathrec, &av, NULL)) {
7701da177e4SLinus Torvalds 			ah = ipoib_create_ah(dev, priv->pd, &av);
771aa74f487SParav Pandit 			rdma_destroy_ah_attr(&av);
772aa74f487SParav Pandit 		}
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
7761da177e4SLinus Torvalds 
7773874397cSMike Marciniszyn 	if (!IS_ERR_OR_NULL(ah)) {
77843900089SErez Shitrit 		/*
77943900089SErez Shitrit 		 * pathrec.dgid is used as the database key from the LLADDR,
78043900089SErez Shitrit 		 * it must remain unchanged even if the SA returns a different
78143900089SErez Shitrit 		 * GID to use in the AH.
78243900089SErez Shitrit 		 */
78343900089SErez Shitrit 		if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw,
78443900089SErez Shitrit 			   sizeof(union ib_gid))) {
78543900089SErez Shitrit 			ipoib_dbg(
78643900089SErez Shitrit 				priv,
78743900089SErez Shitrit 				"%s got PathRec for gid %pI6 while asked for %pI6\n",
78843900089SErez Shitrit 				dev->name, pathrec->dgid.raw,
78943900089SErez Shitrit 				path->pathrec.dgid.raw);
79043900089SErez Shitrit 			memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw,
79143900089SErez Shitrit 			       sizeof(union ib_gid));
79243900089SErez Shitrit 		}
79343900089SErez Shitrit 
7941da177e4SLinus Torvalds 		path->pathrec = *pathrec;
7951da177e4SLinus Torvalds 
796c9da4badSRoland Dreier 		old_ah   = path->ah;
797c9da4badSRoland Dreier 		path->ah = ah;
798c9da4badSRoland Dreier 
7991da177e4SLinus Torvalds 		ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
80057520751SDasaratharaman Chandramouli 			  ah, be32_to_cpu(sa_path_get_dlid(pathrec)),
8019fdca4daSDasaratharaman Chandramouli 			  pathrec->sl);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 		while ((skb = __skb_dequeue(&path->queue)))
8041da177e4SLinus Torvalds 			__skb_queue_tail(&skqueue, skb);
8051da177e4SLinus Torvalds 
806d04d01b1SMichael S. Tsirkin 		list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
807ee1e2c82SMoni Shoua 			if (neigh->ah) {
808ee1e2c82SMoni Shoua 				WARN_ON(neigh->ah != old_ah);
809ee1e2c82SMoni Shoua 				/*
810ee1e2c82SMoni Shoua 				 * Dropping the ah reference inside
811ee1e2c82SMoni Shoua 				 * priv->lock is safe here, because we
812ee1e2c82SMoni Shoua 				 * will hold one more reference from
813ee1e2c82SMoni Shoua 				 * the original value of path->ah (ie
814ee1e2c82SMoni Shoua 				 * old_ah).
815ee1e2c82SMoni Shoua 				 */
816ee1e2c82SMoni Shoua 				ipoib_put_ah(neigh->ah);
817ee1e2c82SMoni Shoua 			}
8181da177e4SLinus Torvalds 			kref_get(&path->ah->ref);
8191da177e4SLinus Torvalds 			neigh->ah = path->ah;
8201da177e4SLinus Torvalds 
821b63b70d8SShlomo Pongratz 			if (ipoib_cm_enabled(dev, neigh->daddr)) {
822839fcabaSMichael S. Tsirkin 				if (!ipoib_cm_get(neigh))
823839fcabaSMichael S. Tsirkin 					ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
824839fcabaSMichael S. Tsirkin 									       path,
825839fcabaSMichael S. Tsirkin 									       neigh));
826839fcabaSMichael S. Tsirkin 				if (!ipoib_cm_get(neigh)) {
827b63b70d8SShlomo Pongratz 					ipoib_neigh_free(neigh);
828839fcabaSMichael S. Tsirkin 					continue;
829839fcabaSMichael S. Tsirkin 				}
830839fcabaSMichael S. Tsirkin 			}
831839fcabaSMichael S. Tsirkin 
8321da177e4SLinus Torvalds 			while ((skb = __skb_dequeue(&neigh->queue)))
8331da177e4SLinus Torvalds 				__skb_queue_tail(&skqueue, skb);
8341da177e4SLinus Torvalds 		}
835fa9391dbSDoug Ledford 		path->ah->valid = 1;
8365872a9fcSRoland Dreier 	}
8371da177e4SLinus Torvalds 
8385872a9fcSRoland Dreier 	path->query = NULL;
8391da177e4SLinus Torvalds 	complete(&path->done);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
8421da177e4SLinus Torvalds 
843f72dd566SRoland Dreier 	if (IS_ERR_OR_NULL(ah))
844f72dd566SRoland Dreier 		ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
845f72dd566SRoland Dreier 
846ee1e2c82SMoni Shoua 	if (old_ah)
847ee1e2c82SMoni Shoua 		ipoib_put_ah(old_ah);
848ee1e2c82SMoni Shoua 
8491da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&skqueue))) {
850d32b9a81SFeras Daoud 		int ret;
8511da177e4SLinus Torvalds 		skb->dev = dev;
852d32b9a81SFeras Daoud 		ret = dev_queue_xmit(skb);
853d32b9a81SFeras Daoud 		if (ret)
854d32b9a81SFeras Daoud 			ipoib_warn(priv, "%s: dev_queue_xmit failed to re-queue packet, ret:%d\n",
855d32b9a81SFeras Daoud 				   __func__, ret);
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
85998aebc55SErez Shitrit static void init_path_rec(struct ipoib_dev_priv *priv, struct ipoib_path *path,
86098aebc55SErez Shitrit 			  void *gid)
86198aebc55SErez Shitrit {
86298aebc55SErez Shitrit 	path->dev = priv->dev;
86398aebc55SErez Shitrit 
86498aebc55SErez Shitrit 	if (rdma_cap_opa_ah(priv->ca, priv->port))
86598aebc55SErez Shitrit 		path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA;
86698aebc55SErez Shitrit 	else
86798aebc55SErez Shitrit 		path->pathrec.rec_type = SA_PATH_REC_TYPE_IB;
86898aebc55SErez Shitrit 
86998aebc55SErez Shitrit 	memcpy(path->pathrec.dgid.raw, gid, sizeof(union ib_gid));
87098aebc55SErez Shitrit 	path->pathrec.sgid	    = priv->local_gid;
87198aebc55SErez Shitrit 	path->pathrec.pkey	    = cpu_to_be16(priv->pkey);
87298aebc55SErez Shitrit 	path->pathrec.numb_path     = 1;
87398aebc55SErez Shitrit 	path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
87498aebc55SErez Shitrit }
87598aebc55SErez Shitrit 
87637c22a77SJack Morgenstein static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
8771da177e4SLinus Torvalds {
878c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
8791da177e4SLinus Torvalds 	struct ipoib_path *path;
8801da177e4SLinus Torvalds 
8811401b53aSJack Morgenstein 	if (!priv->broadcast)
8821401b53aSJack Morgenstein 		return NULL;
8831401b53aSJack Morgenstein 
884b1b63970SKamal Heib 	path = kzalloc(sizeof(*path), GFP_ATOMIC);
8851da177e4SLinus Torvalds 	if (!path)
8861da177e4SLinus Torvalds 		return NULL;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	skb_queue_head_init(&path->queue);
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	INIT_LIST_HEAD(&path->neigh_list);
8911da177e4SLinus Torvalds 
89298aebc55SErez Shitrit 	init_path_rec(priv, path, gid);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	return path;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds static int path_rec_start(struct net_device *dev,
8981da177e4SLinus Torvalds 			  struct ipoib_path *path)
8991da177e4SLinus Torvalds {
900c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
9011da177e4SLinus Torvalds 
9025b095d98SHarvey Harrison 	ipoib_dbg(priv, "Start path record lookup for %pI6\n",
903fcace2feSHarvey Harrison 		  path->pathrec.dgid.raw);
9041da177e4SLinus Torvalds 
90565c7eddaSRoland Dreier 	init_completion(&path->done);
90665c7eddaSRoland Dreier 
9071da177e4SLinus Torvalds 	path->query_id =
908c1a0b23bSMichael S. Tsirkin 		ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
9091da177e4SLinus Torvalds 				   &path->pathrec,
9101da177e4SLinus Torvalds 				   IB_SA_PATH_REC_DGID		|
9111da177e4SLinus Torvalds 				   IB_SA_PATH_REC_SGID		|
9121da177e4SLinus Torvalds 				   IB_SA_PATH_REC_NUMB_PATH	|
91381668838SSean Hefty 				   IB_SA_PATH_REC_TRAFFIC_CLASS |
9141da177e4SLinus Torvalds 				   IB_SA_PATH_REC_PKEY,
9151da177e4SLinus Torvalds 				   1000, GFP_ATOMIC,
9161da177e4SLinus Torvalds 				   path_rec_completion,
9171da177e4SLinus Torvalds 				   path, &path->query);
9181da177e4SLinus Torvalds 	if (path->query_id < 0) {
91901b3fc8bSOr Gerlitz 		ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
9201da177e4SLinus Torvalds 		path->query = NULL;
92193a3ab93SYossi Etigin 		complete(&path->done);
9221da177e4SLinus Torvalds 		return path->query_id;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	return 0;
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
928fa9391dbSDoug Ledford static void neigh_refresh_path(struct ipoib_neigh *neigh, u8 *daddr,
929fa9391dbSDoug Ledford 			       struct net_device *dev)
930fa9391dbSDoug Ledford {
931fa9391dbSDoug Ledford 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
932fa9391dbSDoug Ledford 	struct ipoib_path *path;
933fa9391dbSDoug Ledford 	unsigned long flags;
934fa9391dbSDoug Ledford 
935fa9391dbSDoug Ledford 	spin_lock_irqsave(&priv->lock, flags);
936fa9391dbSDoug Ledford 
937fa9391dbSDoug Ledford 	path = __path_find(dev, daddr + 4);
938fa9391dbSDoug Ledford 	if (!path)
939fa9391dbSDoug Ledford 		goto out;
940fa9391dbSDoug Ledford 	if (!path->query)
941fa9391dbSDoug Ledford 		path_rec_start(dev, path);
942fa9391dbSDoug Ledford out:
943fa9391dbSDoug Ledford 	spin_unlock_irqrestore(&priv->lock, flags);
944fa9391dbSDoug Ledford }
945fa9391dbSDoug Ledford 
94616ba3defSErez Shitrit static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr,
947b63b70d8SShlomo Pongratz 					  struct net_device *dev)
9481da177e4SLinus Torvalds {
949c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
950cd565b4bSErez Shitrit 	struct rdma_netdev *rn = netdev_priv(dev);
9511da177e4SLinus Torvalds 	struct ipoib_path *path;
9521da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
953943c246eSRoland Dreier 	unsigned long flags;
9541da177e4SLinus Torvalds 
955b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
956b63b70d8SShlomo Pongratz 	neigh = ipoib_neigh_alloc(daddr, dev);
9571da177e4SLinus Torvalds 	if (!neigh) {
958b5120a6eSShlomo Pongratz 		spin_unlock_irqrestore(&priv->lock, flags);
959de903512SRoland Dreier 		++dev->stats.tx_dropped;
9601da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
96116ba3defSErez Shitrit 		return NULL;
96216ba3defSErez Shitrit 	}
96316ba3defSErez Shitrit 
96416ba3defSErez Shitrit 	/* To avoid race condition, make sure that the
96516ba3defSErez Shitrit 	 * neigh will be added only once.
96616ba3defSErez Shitrit 	 */
96716ba3defSErez Shitrit 	if (unlikely(!list_empty(&neigh->list))) {
96816ba3defSErez Shitrit 		spin_unlock_irqrestore(&priv->lock, flags);
96916ba3defSErez Shitrit 		return neigh;
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds 
972b63b70d8SShlomo Pongratz 	path = __path_find(dev, daddr + 4);
9731da177e4SLinus Torvalds 	if (!path) {
974b63b70d8SShlomo Pongratz 		path = path_rec_create(dev, daddr + 4);
9751da177e4SLinus Torvalds 		if (!path)
976d2e0655eSMichael S. Tsirkin 			goto err_path;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 		__path_add(dev, path);
9791da177e4SLinus Torvalds 	}
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	list_add_tail(&neigh->list, &path->neigh_list);
9821da177e4SLinus Torvalds 
983fa9391dbSDoug Ledford 	if (path->ah && path->ah->valid) {
9841da177e4SLinus Torvalds 		kref_get(&path->ah->ref);
9851da177e4SLinus Torvalds 		neigh->ah = path->ah;
9861da177e4SLinus Torvalds 
987b63b70d8SShlomo Pongratz 		if (ipoib_cm_enabled(dev, neigh->daddr)) {
988839fcabaSMichael S. Tsirkin 			if (!ipoib_cm_get(neigh))
989839fcabaSMichael S. Tsirkin 				ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
990839fcabaSMichael S. Tsirkin 			if (!ipoib_cm_get(neigh)) {
991b63b70d8SShlomo Pongratz 				ipoib_neigh_free(neigh);
992839fcabaSMichael S. Tsirkin 				goto err_drop;
993839fcabaSMichael S. Tsirkin 			}
994fc791b63SPaolo Abeni 			if (skb_queue_len(&neigh->queue) <
995fc791b63SPaolo Abeni 			    IPOIB_MAX_PATH_REC_QUEUE) {
9962b084176SErez Shitrit 				push_pseudo_header(skb, neigh->daddr);
997839fcabaSMichael S. Tsirkin 				__skb_queue_tail(&neigh->queue, skb);
998fc791b63SPaolo Abeni 			} else {
999839fcabaSMichael S. Tsirkin 				ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
1000839fcabaSMichael S. Tsirkin 					   skb_queue_len(&neigh->queue));
1001839fcabaSMichael S. Tsirkin 				goto err_drop;
1002839fcabaSMichael S. Tsirkin 			}
1003721d67cdSRoland Dreier 		} else {
1004721d67cdSRoland Dreier 			spin_unlock_irqrestore(&priv->lock, flags);
1005cd565b4bSErez Shitrit 			path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1006cd565b4bSErez Shitrit 						       IPOIB_QPN(daddr));
1007b63b70d8SShlomo Pongratz 			ipoib_neigh_put(neigh);
100816ba3defSErez Shitrit 			return NULL;
1009721d67cdSRoland Dreier 		}
10101da177e4SLinus Torvalds 	} else {
10111da177e4SLinus Torvalds 		neigh->ah  = NULL;
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 		if (!path->query && path_rec_start(dev, path))
101449b8e744SJim Foraker 			goto err_path;
10152b084176SErez Shitrit 		if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
10162b084176SErez Shitrit 			push_pseudo_header(skb, neigh->daddr);
10172745b5b7SMichael S. Tsirkin 			__skb_queue_tail(&neigh->queue, skb);
10182b084176SErez Shitrit 		} else {
10191e85b806SErez Shitrit 			goto err_drop;
10201da177e4SLinus Torvalds 		}
10212b084176SErez Shitrit 	}
10221da177e4SLinus Torvalds 
1023943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
1024b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
102516ba3defSErez Shitrit 	return NULL;
10261da177e4SLinus Torvalds 
1027d2e0655eSMichael S. Tsirkin err_path:
1028b63b70d8SShlomo Pongratz 	ipoib_neigh_free(neigh);
1029839fcabaSMichael S. Tsirkin err_drop:
1030de903512SRoland Dreier 	++dev->stats.tx_dropped;
10311da177e4SLinus Torvalds 	dev_kfree_skb_any(skb);
10321da177e4SLinus Torvalds 
1033943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
1034b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
103516ba3defSErez Shitrit 
103616ba3defSErez Shitrit 	return NULL;
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
1040fc791b63SPaolo Abeni 			     struct ipoib_pseudo_header *phdr)
10411da177e4SLinus Torvalds {
1042c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1043cd565b4bSErez Shitrit 	struct rdma_netdev *rn = netdev_priv(dev);
10441da177e4SLinus Torvalds 	struct ipoib_path *path;
1045943c246eSRoland Dreier 	unsigned long flags;
10461da177e4SLinus Torvalds 
1047943c246eSRoland Dreier 	spin_lock_irqsave(&priv->lock, flags);
10481da177e4SLinus Torvalds 
104998aebc55SErez Shitrit 	/* no broadcast means that all paths are (going to be) not valid */
105098aebc55SErez Shitrit 	if (!priv->broadcast)
105198aebc55SErez Shitrit 		goto drop_and_unlock;
105298aebc55SErez Shitrit 
1053fc791b63SPaolo Abeni 	path = __path_find(dev, phdr->hwaddr + 4);
1054fa9391dbSDoug Ledford 	if (!path || !path->ah || !path->ah->valid) {
105571d98b46SJack Morgenstein 		if (!path) {
1056fc791b63SPaolo Abeni 			path = path_rec_create(dev, phdr->hwaddr + 4);
105715517080SEvgenii Smirnov 			if (!path)
105815517080SEvgenii Smirnov 				goto drop_and_unlock;
105915517080SEvgenii Smirnov 			__path_add(dev, path);
106015517080SEvgenii Smirnov 		} else {
106115517080SEvgenii Smirnov 			/*
106215517080SEvgenii Smirnov 			 * make sure there are no changes in the existing
106315517080SEvgenii Smirnov 			 * path record
106415517080SEvgenii Smirnov 			 */
106598aebc55SErez Shitrit 			init_path_rec(priv, path, phdr->hwaddr + 4);
106615517080SEvgenii Smirnov 		}
106715517080SEvgenii Smirnov 		if (!path->query && path_rec_start(dev, path)) {
106815517080SEvgenii Smirnov 			goto drop_and_unlock;
106915517080SEvgenii Smirnov 		}
107098aebc55SErez Shitrit 
10711e85b806SErez Shitrit 		if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
10722b084176SErez Shitrit 			push_pseudo_header(skb, phdr->hwaddr);
10731da177e4SLinus Torvalds 			__skb_queue_tail(&path->queue, skb);
107415517080SEvgenii Smirnov 			goto unlock;
10751da177e4SLinus Torvalds 		} else {
107698aebc55SErez Shitrit 			goto drop_and_unlock;
10771da177e4SLinus Torvalds 		}
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 
108015517080SEvgenii Smirnov 	spin_unlock_irqrestore(&priv->lock, flags);
108157520751SDasaratharaman Chandramouli 	ipoib_dbg(priv, "Send unicast ARP to %08x\n",
108257520751SDasaratharaman Chandramouli 		  be32_to_cpu(sa_path_get_dlid(&path->pathrec)));
1083cd565b4bSErez Shitrit 	path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1084cd565b4bSErez Shitrit 				       IPOIB_QPN(phdr->hwaddr));
1085721d67cdSRoland Dreier 	return;
108698aebc55SErez Shitrit 
108798aebc55SErez Shitrit drop_and_unlock:
108898aebc55SErez Shitrit 	++dev->stats.tx_dropped;
108998aebc55SErez Shitrit 	dev_kfree_skb_any(skb);
109015517080SEvgenii Smirnov unlock:
109198aebc55SErez Shitrit 	spin_unlock_irqrestore(&priv->lock, flags);
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
109447a3968aSLuc Van Oostenryck static netdev_tx_t ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
10951da177e4SLinus Torvalds {
1096c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1097cd565b4bSErez Shitrit 	struct rdma_netdev *rn = netdev_priv(dev);
10981da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
1099fc791b63SPaolo Abeni 	struct ipoib_pseudo_header *phdr;
1100b63b70d8SShlomo Pongratz 	struct ipoib_header *header;
11011da177e4SLinus Torvalds 	unsigned long flags;
11021da177e4SLinus Torvalds 
1103fc791b63SPaolo Abeni 	phdr = (struct ipoib_pseudo_header *) skb->data;
1104fc791b63SPaolo Abeni 	skb_pull(skb, sizeof(*phdr));
1105b63b70d8SShlomo Pongratz 	header = (struct ipoib_header *) skb->data;
1106b63b70d8SShlomo Pongratz 
1107fc791b63SPaolo Abeni 	if (unlikely(phdr->hwaddr[4] == 0xff)) {
1108b63b70d8SShlomo Pongratz 		/* multicast, arrange "if" according to probability */
1109b63b70d8SShlomo Pongratz 		if ((header->proto != htons(ETH_P_IP)) &&
1110b63b70d8SShlomo Pongratz 		    (header->proto != htons(ETH_P_IPV6)) &&
1111b63b70d8SShlomo Pongratz 		    (header->proto != htons(ETH_P_ARP)) &&
1112dc850b0eSPatrick McHardy 		    (header->proto != htons(ETH_P_RARP)) &&
1113dc850b0eSPatrick McHardy 		    (header->proto != htons(ETH_P_TIPC))) {
1114b63b70d8SShlomo Pongratz 			/* ethertype not supported by IPoIB */
111517e6abeeSDavid Miller 			++dev->stats.tx_dropped;
111617e6abeeSDavid Miller 			dev_kfree_skb_any(skb);
1117b63b70d8SShlomo Pongratz 			return NETDEV_TX_OK;
111817e6abeeSDavid Miller 		}
1119b63b70d8SShlomo Pongratz 		/* Add in the P_Key for multicast*/
1120fc791b63SPaolo Abeni 		phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1121fc791b63SPaolo Abeni 		phdr->hwaddr[9] = priv->pkey & 0xff;
1122b63b70d8SShlomo Pongratz 
1123fc791b63SPaolo Abeni 		neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1124b63b70d8SShlomo Pongratz 		if (likely(neigh))
1125b63b70d8SShlomo Pongratz 			goto send_using_neigh;
1126fc791b63SPaolo Abeni 		ipoib_mcast_send(dev, phdr->hwaddr, skb);
1127b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 
1130b63b70d8SShlomo Pongratz 	/* unicast, arrange "switch" according to probability */
1131b63b70d8SShlomo Pongratz 	switch (header->proto) {
1132b63b70d8SShlomo Pongratz 	case htons(ETH_P_IP):
1133b63b70d8SShlomo Pongratz 	case htons(ETH_P_IPV6):
1134dc850b0eSPatrick McHardy 	case htons(ETH_P_TIPC):
1135fc791b63SPaolo Abeni 		neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1136b63b70d8SShlomo Pongratz 		if (unlikely(!neigh)) {
113716ba3defSErez Shitrit 			neigh = neigh_add_path(skb, phdr->hwaddr, dev);
113816ba3defSErez Shitrit 			if (likely(!neigh))
1139b63b70d8SShlomo Pongratz 				return NETDEV_TX_OK;
1140b63b70d8SShlomo Pongratz 		}
1141b63b70d8SShlomo Pongratz 		break;
1142b63b70d8SShlomo Pongratz 	case htons(ETH_P_ARP):
1143b63b70d8SShlomo Pongratz 	case htons(ETH_P_RARP):
1144b63b70d8SShlomo Pongratz 		/* for unicast ARP and RARP should always perform path find */
1145fc791b63SPaolo Abeni 		unicast_arp_send(skb, dev, phdr);
1146b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
1147b63b70d8SShlomo Pongratz 	default:
1148b63b70d8SShlomo Pongratz 		/* ethertype not supported by IPoIB */
1149b63b70d8SShlomo Pongratz 		++dev->stats.tx_dropped;
1150b63b70d8SShlomo Pongratz 		dev_kfree_skb_any(skb);
1151b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
11528a7f7521SMichael S. Tsirkin 	}
11538a7f7521SMichael S. Tsirkin 
1154b63b70d8SShlomo Pongratz send_using_neigh:
1155b63b70d8SShlomo Pongratz 	/* note we now hold a ref to neigh */
1156bafff974SOr Gerlitz 	if (ipoib_cm_get(neigh)) {
1157bafff974SOr Gerlitz 		if (ipoib_cm_up(neigh)) {
1158bafff974SOr Gerlitz 			ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1159b63b70d8SShlomo Pongratz 			goto unref;
1160bafff974SOr Gerlitz 		}
1161fa9391dbSDoug Ledford 	} else if (neigh->ah && neigh->ah->valid) {
1162cd565b4bSErez Shitrit 		neigh->ah->last_send = rn->send(dev, skb, neigh->ah->ah,
1163cd565b4bSErez Shitrit 						IPOIB_QPN(phdr->hwaddr));
1164b63b70d8SShlomo Pongratz 		goto unref;
1165fa9391dbSDoug Ledford 	} else if (neigh->ah) {
1166fa9391dbSDoug Ledford 		neigh_refresh_path(neigh, phdr->hwaddr, dev);
11671da177e4SLinus Torvalds 	}
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
11702b084176SErez Shitrit 		push_pseudo_header(skb, phdr->hwaddr);
1171943c246eSRoland Dreier 		spin_lock_irqsave(&priv->lock, flags);
11721da177e4SLinus Torvalds 		__skb_queue_tail(&neigh->queue, skb);
1173943c246eSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
11741da177e4SLinus Torvalds 	} else {
1175de903512SRoland Dreier 		++dev->stats.tx_dropped;
11761da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
11771da177e4SLinus Torvalds 	}
11781da177e4SLinus Torvalds 
1179b63b70d8SShlomo Pongratz unref:
1180b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	return NETDEV_TX_OK;
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds static void ipoib_timeout(struct net_device *dev)
11861da177e4SLinus Torvalds {
1187c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
11881da177e4SLinus Torvalds 
11894b2d319bSRoland Dreier 	ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
11904d0e9657SFlorian Westphal 		   jiffies_to_msecs(jiffies - dev_trans_start(dev)));
11914b2d319bSRoland Dreier 	ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
11924b2d319bSRoland Dreier 		   netif_queue_stopped(dev),
11934b2d319bSRoland Dreier 		   priv->tx_head, priv->tx_tail);
11941da177e4SLinus Torvalds 	/* XXX reset QP, etc. */
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds static int ipoib_hard_header(struct sk_buff *skb,
11981da177e4SLinus Torvalds 			     struct net_device *dev,
11991da177e4SLinus Torvalds 			     unsigned short type,
12000578cdadSKamal Heib 			     const void *daddr,
12010578cdadSKamal Heib 			     const void *saddr,
12020578cdadSKamal Heib 			     unsigned int len)
12031da177e4SLinus Torvalds {
12041da177e4SLinus Torvalds 	struct ipoib_header *header;
12051da177e4SLinus Torvalds 
1206b1b63970SKamal Heib 	header = skb_push(skb, sizeof(*header));
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	header->proto = htons(type);
12091da177e4SLinus Torvalds 	header->reserved = 0;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	/*
1212b63b70d8SShlomo Pongratz 	 * we don't rely on dst_entry structure,  always stuff the
1213fc791b63SPaolo Abeni 	 * destination address into skb hard header so we can figure out where
1214936d7de3SRoland Dreier 	 * to send the packet later.
12151da177e4SLinus Torvalds 	 */
12162b084176SErez Shitrit 	push_pseudo_header(skb, daddr);
12171da177e4SLinus Torvalds 
1218fc791b63SPaolo Abeni 	return IPOIB_HARD_LEN;
12191da177e4SLinus Torvalds }
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds static void ipoib_set_mcast_list(struct net_device *dev)
12221da177e4SLinus Torvalds {
1223c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
12241da177e4SLinus Torvalds 
12257a343d4cSLeonid Arsh 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
12267a343d4cSLeonid Arsh 		ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
12277a343d4cSLeonid Arsh 		return;
12287a343d4cSLeonid Arsh 	}
12297a343d4cSLeonid Arsh 
12300b39578bSDoug Ledford 	queue_work(priv->wq, &priv->restart_task);
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12335aa7add8SNicolas Dichtel static int ipoib_get_iflink(const struct net_device *dev)
12345aa7add8SNicolas Dichtel {
1235c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
12365aa7add8SNicolas Dichtel 
12372c153959SErez Shitrit 	/* parent interface */
12382c153959SErez Shitrit 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
12392c153959SErez Shitrit 		return dev->ifindex;
12402c153959SErez Shitrit 
12412c153959SErez Shitrit 	/* child/vlan interface */
12425aa7add8SNicolas Dichtel 	return priv->parent->ifindex;
12435aa7add8SNicolas Dichtel }
12445aa7add8SNicolas Dichtel 
1245b63b70d8SShlomo Pongratz static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
12461da177e4SLinus Torvalds {
1247b63b70d8SShlomo Pongratz 	/*
1248b63b70d8SShlomo Pongratz 	 * Use only the address parts that contributes to spreading
1249b63b70d8SShlomo Pongratz 	 * The subnet prefix is not used as one can not connect to
1250b63b70d8SShlomo Pongratz 	 * same remote port (GUID) using the same remote QPN via two
1251b63b70d8SShlomo Pongratz 	 * different subnets.
1252b63b70d8SShlomo Pongratz 	 */
1253b63b70d8SShlomo Pongratz 	 /* qpn octets[1:4) & port GUID octets[12:20) */
12549d1ad66eSShlomo Pongratz 	u32 *d32 = (u32 *) daddr;
1255b63b70d8SShlomo Pongratz 	u32 hv;
12561da177e4SLinus Torvalds 
12579d1ad66eSShlomo Pongratz 	hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
1258b63b70d8SShlomo Pongratz 	return hv & htbl->mask;
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds 
1261b63b70d8SShlomo Pongratz struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1262b63b70d8SShlomo Pongratz {
1263c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1264b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1265b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1266b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh = NULL;
1267b63b70d8SShlomo Pongratz 	u32 hash_val;
1268b63b70d8SShlomo Pongratz 
1269b63b70d8SShlomo Pongratz 	rcu_read_lock_bh();
1270b63b70d8SShlomo Pongratz 
1271b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_bh(ntbl->htbl);
1272b63b70d8SShlomo Pongratz 
1273b63b70d8SShlomo Pongratz 	if (!htbl)
1274b63b70d8SShlomo Pongratz 		goto out_unlock;
1275b63b70d8SShlomo Pongratz 
1276b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, daddr);
1277b63b70d8SShlomo Pongratz 	for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1278b63b70d8SShlomo Pongratz 	     neigh != NULL;
1279b63b70d8SShlomo Pongratz 	     neigh = rcu_dereference_bh(neigh->hnext)) {
1280b63b70d8SShlomo Pongratz 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1281b63b70d8SShlomo Pongratz 			/* found, take one ref on behalf of the caller */
1282b63b70d8SShlomo Pongratz 			if (!atomic_inc_not_zero(&neigh->refcnt)) {
1283b63b70d8SShlomo Pongratz 				/* deleted */
1284b63b70d8SShlomo Pongratz 				neigh = NULL;
1285b63b70d8SShlomo Pongratz 				goto out_unlock;
1286b63b70d8SShlomo Pongratz 			}
128761c78eeaSErez Shitrit 
128861c78eeaSErez Shitrit 			if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1289b63b70d8SShlomo Pongratz 				neigh->alive = jiffies;
1290b63b70d8SShlomo Pongratz 			goto out_unlock;
1291b63b70d8SShlomo Pongratz 		}
1292b63b70d8SShlomo Pongratz 	}
1293b63b70d8SShlomo Pongratz 
1294b63b70d8SShlomo Pongratz out_unlock:
1295b63b70d8SShlomo Pongratz 	rcu_read_unlock_bh();
1296b63b70d8SShlomo Pongratz 	return neigh;
1297b63b70d8SShlomo Pongratz }
1298b63b70d8SShlomo Pongratz 
1299b63b70d8SShlomo Pongratz static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1300b63b70d8SShlomo Pongratz {
1301b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1302b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1303b63b70d8SShlomo Pongratz 	unsigned long neigh_obsolete;
1304b63b70d8SShlomo Pongratz 	unsigned long dt;
1305b63b70d8SShlomo Pongratz 	unsigned long flags;
1306b63b70d8SShlomo Pongratz 	int i;
1307bd99b2e0SChristoph Lameter 	LIST_HEAD(remove_list);
1308b63b70d8SShlomo Pongratz 
1309b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1310b63b70d8SShlomo Pongratz 
1311b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1312b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
1313b63b70d8SShlomo Pongratz 
1314b63b70d8SShlomo Pongratz 	if (!htbl)
1315b63b70d8SShlomo Pongratz 		goto out_unlock;
1316b63b70d8SShlomo Pongratz 
1317b63b70d8SShlomo Pongratz 	/* neigh is obsolete if it was idle for two GC periods */
1318b63b70d8SShlomo Pongratz 	dt = 2 * arp_tbl.gc_interval;
1319b63b70d8SShlomo Pongratz 	neigh_obsolete = jiffies - dt;
1320b63b70d8SShlomo Pongratz 
1321b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1322b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1323b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1324b63b70d8SShlomo Pongratz 
1325b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1326b5120a6eSShlomo Pongratz 							  lockdep_is_held(&priv->lock))) != NULL) {
1327b63b70d8SShlomo Pongratz 			/* was the neigh idle for two GC periods */
1328b63b70d8SShlomo Pongratz 			if (time_after(neigh_obsolete, neigh->alive)) {
1329bd99b2e0SChristoph Lameter 
1330432c55ffSChristoph Lameter 				ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
1331bd99b2e0SChristoph Lameter 
1332b63b70d8SShlomo Pongratz 				rcu_assign_pointer(*np,
1333b63b70d8SShlomo Pongratz 						   rcu_dereference_protected(neigh->hnext,
1334b5120a6eSShlomo Pongratz 									     lockdep_is_held(&priv->lock)));
1335b63b70d8SShlomo Pongratz 				/* remove from path/mc list */
1336c586071dSFeras Daoud 				list_del_init(&neigh->list);
1337b63b70d8SShlomo Pongratz 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1338b63b70d8SShlomo Pongratz 			} else {
1339b63b70d8SShlomo Pongratz 				np = &neigh->hnext;
1340b63b70d8SShlomo Pongratz 			}
1341b63b70d8SShlomo Pongratz 
1342b63b70d8SShlomo Pongratz 		}
1343b63b70d8SShlomo Pongratz 	}
1344b63b70d8SShlomo Pongratz 
1345b63b70d8SShlomo Pongratz out_unlock:
1346b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
134750be28deSErez Shitrit 	ipoib_mcast_remove_list(&remove_list);
1348b63b70d8SShlomo Pongratz }
1349b63b70d8SShlomo Pongratz 
1350b63b70d8SShlomo Pongratz static void ipoib_reap_neigh(struct work_struct *work)
1351b63b70d8SShlomo Pongratz {
1352b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv =
1353b63b70d8SShlomo Pongratz 		container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1354b63b70d8SShlomo Pongratz 
1355b63b70d8SShlomo Pongratz 	__ipoib_reap_neigh(priv);
1356b63b70d8SShlomo Pongratz 
13570b39578bSDoug Ledford 	queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1358b63b70d8SShlomo Pongratz 			   arp_tbl.gc_interval);
1359b63b70d8SShlomo Pongratz }
1360b63b70d8SShlomo Pongratz 
1361b63b70d8SShlomo Pongratz 
1362b63b70d8SShlomo Pongratz static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
1363732a2170SMoni Shoua 				      struct net_device *dev)
1364d2e0655eSMichael S. Tsirkin {
1365d2e0655eSMichael S. Tsirkin 	struct ipoib_neigh *neigh;
1366d2e0655eSMichael S. Tsirkin 
1367b1b63970SKamal Heib 	neigh = kzalloc(sizeof(*neigh), GFP_ATOMIC);
1368d2e0655eSMichael S. Tsirkin 	if (!neigh)
1369d2e0655eSMichael S. Tsirkin 		return NULL;
1370d2e0655eSMichael S. Tsirkin 
1371732a2170SMoni Shoua 	neigh->dev = dev;
1372b63b70d8SShlomo Pongratz 	memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
137382b39913SRoland Dreier 	skb_queue_head_init(&neigh->queue);
1374b63b70d8SShlomo Pongratz 	INIT_LIST_HEAD(&neigh->list);
1375839fcabaSMichael S. Tsirkin 	ipoib_cm_set(neigh, NULL);
1376b63b70d8SShlomo Pongratz 	/* one ref on behalf of the caller */
1377b63b70d8SShlomo Pongratz 	atomic_set(&neigh->refcnt, 1);
1378d2e0655eSMichael S. Tsirkin 
1379d2e0655eSMichael S. Tsirkin 	return neigh;
1380d2e0655eSMichael S. Tsirkin }
1381d2e0655eSMichael S. Tsirkin 
1382b63b70d8SShlomo Pongratz struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1383b63b70d8SShlomo Pongratz 				      struct net_device *dev)
1384d2e0655eSMichael S. Tsirkin {
1385c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1386b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1387b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1388b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh;
1389b63b70d8SShlomo Pongratz 	u32 hash_val;
1390b63b70d8SShlomo Pongratz 
1391b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1392b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
1393b63b70d8SShlomo Pongratz 	if (!htbl) {
1394b63b70d8SShlomo Pongratz 		neigh = NULL;
1395b63b70d8SShlomo Pongratz 		goto out_unlock;
1396b63b70d8SShlomo Pongratz 	}
1397b63b70d8SShlomo Pongratz 
1398b63b70d8SShlomo Pongratz 	/* need to add a new neigh, but maybe some other thread succeeded?
1399b63b70d8SShlomo Pongratz 	 * recalc hash, maybe hash resize took place so we do a search
1400b63b70d8SShlomo Pongratz 	 */
1401b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, daddr);
1402b63b70d8SShlomo Pongratz 	for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1403b5120a6eSShlomo Pongratz 					       lockdep_is_held(&priv->lock));
1404b63b70d8SShlomo Pongratz 	     neigh != NULL;
1405b63b70d8SShlomo Pongratz 	     neigh = rcu_dereference_protected(neigh->hnext,
1406b5120a6eSShlomo Pongratz 					       lockdep_is_held(&priv->lock))) {
1407b63b70d8SShlomo Pongratz 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1408b63b70d8SShlomo Pongratz 			/* found, take one ref on behalf of the caller */
1409b63b70d8SShlomo Pongratz 			if (!atomic_inc_not_zero(&neigh->refcnt)) {
1410b63b70d8SShlomo Pongratz 				/* deleted */
1411b63b70d8SShlomo Pongratz 				neigh = NULL;
1412b63b70d8SShlomo Pongratz 				break;
1413b63b70d8SShlomo Pongratz 			}
1414b63b70d8SShlomo Pongratz 			neigh->alive = jiffies;
1415b63b70d8SShlomo Pongratz 			goto out_unlock;
1416b63b70d8SShlomo Pongratz 		}
1417b63b70d8SShlomo Pongratz 	}
1418b63b70d8SShlomo Pongratz 
1419b63b70d8SShlomo Pongratz 	neigh = ipoib_neigh_ctor(daddr, dev);
1420b63b70d8SShlomo Pongratz 	if (!neigh)
1421b63b70d8SShlomo Pongratz 		goto out_unlock;
1422b63b70d8SShlomo Pongratz 
1423b63b70d8SShlomo Pongratz 	/* one ref on behalf of the hash table */
1424b63b70d8SShlomo Pongratz 	atomic_inc(&neigh->refcnt);
1425b63b70d8SShlomo Pongratz 	neigh->alive = jiffies;
1426b63b70d8SShlomo Pongratz 	/* put in hash */
1427b63b70d8SShlomo Pongratz 	rcu_assign_pointer(neigh->hnext,
1428b63b70d8SShlomo Pongratz 			   rcu_dereference_protected(htbl->buckets[hash_val],
1429b5120a6eSShlomo Pongratz 						     lockdep_is_held(&priv->lock)));
1430b63b70d8SShlomo Pongratz 	rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1431b63b70d8SShlomo Pongratz 	atomic_inc(&ntbl->entries);
1432b63b70d8SShlomo Pongratz 
1433b63b70d8SShlomo Pongratz out_unlock:
1434b63b70d8SShlomo Pongratz 
1435b63b70d8SShlomo Pongratz 	return neigh;
1436b63b70d8SShlomo Pongratz }
1437b63b70d8SShlomo Pongratz 
1438b63b70d8SShlomo Pongratz void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1439b63b70d8SShlomo Pongratz {
1440b63b70d8SShlomo Pongratz 	/* neigh reference count was dropprd to zero */
1441b63b70d8SShlomo Pongratz 	struct net_device *dev = neigh->dev;
1442c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
14432745b5b7SMichael S. Tsirkin 	struct sk_buff *skb;
1444b63b70d8SShlomo Pongratz 	if (neigh->ah)
1445b63b70d8SShlomo Pongratz 		ipoib_put_ah(neigh->ah);
14462745b5b7SMichael S. Tsirkin 	while ((skb = __skb_dequeue(&neigh->queue))) {
1447de903512SRoland Dreier 		++dev->stats.tx_dropped;
14482745b5b7SMichael S. Tsirkin 		dev_kfree_skb_any(skb);
14492745b5b7SMichael S. Tsirkin 	}
1450839fcabaSMichael S. Tsirkin 	if (ipoib_cm_get(neigh))
1451839fcabaSMichael S. Tsirkin 		ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1452c1048affSErez Shitrit 	ipoib_dbg(ipoib_priv(dev),
1453b63b70d8SShlomo Pongratz 		  "neigh free for %06x %pI6\n",
1454b63b70d8SShlomo Pongratz 		  IPOIB_QPN(neigh->daddr),
1455b63b70d8SShlomo Pongratz 		  neigh->daddr + 4);
1456d2e0655eSMichael S. Tsirkin 	kfree(neigh);
1457b63b70d8SShlomo Pongratz 	if (atomic_dec_and_test(&priv->ntbl.entries)) {
1458b63b70d8SShlomo Pongratz 		if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1459b63b70d8SShlomo Pongratz 			complete(&priv->ntbl.flushed);
1460b63b70d8SShlomo Pongratz 	}
1461d2e0655eSMichael S. Tsirkin }
1462d2e0655eSMichael S. Tsirkin 
1463b63b70d8SShlomo Pongratz static void ipoib_neigh_reclaim(struct rcu_head *rp)
14641da177e4SLinus Torvalds {
1465b63b70d8SShlomo Pongratz 	/* Called as a result of removal from hash table */
1466b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1467b63b70d8SShlomo Pongratz 	/* note TX context may hold another ref */
1468b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
1469b63b70d8SShlomo Pongratz }
1470b63b70d8SShlomo Pongratz 
1471b63b70d8SShlomo Pongratz void ipoib_neigh_free(struct ipoib_neigh *neigh)
1472b63b70d8SShlomo Pongratz {
1473b63b70d8SShlomo Pongratz 	struct net_device *dev = neigh->dev;
1474c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1475b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1476b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1477b63b70d8SShlomo Pongratz 	struct ipoib_neigh __rcu **np;
1478b63b70d8SShlomo Pongratz 	struct ipoib_neigh *n;
1479b63b70d8SShlomo Pongratz 	u32 hash_val;
1480b63b70d8SShlomo Pongratz 
1481b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1482b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock));
1483b63b70d8SShlomo Pongratz 	if (!htbl)
1484b5120a6eSShlomo Pongratz 		return;
1485b63b70d8SShlomo Pongratz 
1486b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1487b63b70d8SShlomo Pongratz 	np = &htbl->buckets[hash_val];
1488b63b70d8SShlomo Pongratz 	for (n = rcu_dereference_protected(*np,
1489b5120a6eSShlomo Pongratz 					    lockdep_is_held(&priv->lock));
1490b63b70d8SShlomo Pongratz 	     n != NULL;
14916c723a68SShlomo Pongratz 	     n = rcu_dereference_protected(*np,
1492b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock))) {
1493b63b70d8SShlomo Pongratz 		if (n == neigh) {
1494b63b70d8SShlomo Pongratz 			/* found */
1495b63b70d8SShlomo Pongratz 			rcu_assign_pointer(*np,
1496b63b70d8SShlomo Pongratz 					   rcu_dereference_protected(neigh->hnext,
1497b5120a6eSShlomo Pongratz 								     lockdep_is_held(&priv->lock)));
149849b8e744SJim Foraker 			/* remove from parent list */
1499c586071dSFeras Daoud 			list_del_init(&neigh->list);
1500b63b70d8SShlomo Pongratz 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1501b5120a6eSShlomo Pongratz 			return;
1502b63b70d8SShlomo Pongratz 		} else {
1503b63b70d8SShlomo Pongratz 			np = &n->hnext;
1504b63b70d8SShlomo Pongratz 		}
1505b63b70d8SShlomo Pongratz 	}
1506b63b70d8SShlomo Pongratz }
1507b63b70d8SShlomo Pongratz 
1508b63b70d8SShlomo Pongratz static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1509b63b70d8SShlomo Pongratz {
1510b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1511b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
151252374967SBart Van Assche 	struct ipoib_neigh __rcu **buckets;
1513b63b70d8SShlomo Pongratz 	u32 size;
1514b63b70d8SShlomo Pongratz 
1515b63b70d8SShlomo Pongratz 	clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1516b63b70d8SShlomo Pongratz 	ntbl->htbl = NULL;
1517b63b70d8SShlomo Pongratz 	htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1518b63b70d8SShlomo Pongratz 	if (!htbl)
1519b63b70d8SShlomo Pongratz 		return -ENOMEM;
1520b63b70d8SShlomo Pongratz 	size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1521259e1914SJan Dakinevich 	buckets = kvcalloc(size, sizeof(*buckets), GFP_KERNEL);
1522b63b70d8SShlomo Pongratz 	if (!buckets) {
1523b63b70d8SShlomo Pongratz 		kfree(htbl);
1524b63b70d8SShlomo Pongratz 		return -ENOMEM;
1525b63b70d8SShlomo Pongratz 	}
1526b63b70d8SShlomo Pongratz 	htbl->size = size;
1527b63b70d8SShlomo Pongratz 	htbl->mask = (size - 1);
1528b63b70d8SShlomo Pongratz 	htbl->buckets = buckets;
152952374967SBart Van Assche 	RCU_INIT_POINTER(ntbl->htbl, htbl);
153066172c09SShlomo Pongratz 	htbl->ntbl = ntbl;
1531b63b70d8SShlomo Pongratz 	atomic_set(&ntbl->entries, 0);
1532b63b70d8SShlomo Pongratz 
1533b63b70d8SShlomo Pongratz 	/* start garbage collection */
15340b39578bSDoug Ledford 	queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1535b63b70d8SShlomo Pongratz 			   arp_tbl.gc_interval);
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	return 0;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
1540b63b70d8SShlomo Pongratz static void neigh_hash_free_rcu(struct rcu_head *head)
1541b63b70d8SShlomo Pongratz {
1542b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl = container_of(head,
1543b63b70d8SShlomo Pongratz 						    struct ipoib_neigh_hash,
1544b63b70d8SShlomo Pongratz 						    rcu);
1545b63b70d8SShlomo Pongratz 	struct ipoib_neigh __rcu **buckets = htbl->buckets;
154666172c09SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = htbl->ntbl;
1547b63b70d8SShlomo Pongratz 
1548259e1914SJan Dakinevich 	kvfree(buckets);
1549b63b70d8SShlomo Pongratz 	kfree(htbl);
155066172c09SShlomo Pongratz 	complete(&ntbl->deleted);
1551b63b70d8SShlomo Pongratz }
1552b63b70d8SShlomo Pongratz 
1553b63b70d8SShlomo Pongratz void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1554b63b70d8SShlomo Pongratz {
1555c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1556b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1557b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1558b63b70d8SShlomo Pongratz 	unsigned long flags;
1559b63b70d8SShlomo Pongratz 	int i;
1560b63b70d8SShlomo Pongratz 
1561b63b70d8SShlomo Pongratz 	/* remove all neigh connected to a given path or mcast */
1562b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1563b63b70d8SShlomo Pongratz 
1564b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1565b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
1566b63b70d8SShlomo Pongratz 
1567b63b70d8SShlomo Pongratz 	if (!htbl)
1568b63b70d8SShlomo Pongratz 		goto out_unlock;
1569b63b70d8SShlomo Pongratz 
1570b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1571b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1572b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1573b63b70d8SShlomo Pongratz 
1574b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1575b5120a6eSShlomo Pongratz 							  lockdep_is_held(&priv->lock))) != NULL) {
1576b63b70d8SShlomo Pongratz 			/* delete neighs belong to this parent */
1577b63b70d8SShlomo Pongratz 			if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1578b63b70d8SShlomo Pongratz 				rcu_assign_pointer(*np,
1579b63b70d8SShlomo Pongratz 						   rcu_dereference_protected(neigh->hnext,
1580b5120a6eSShlomo Pongratz 									     lockdep_is_held(&priv->lock)));
1581b63b70d8SShlomo Pongratz 				/* remove from parent list */
1582c586071dSFeras Daoud 				list_del_init(&neigh->list);
1583b63b70d8SShlomo Pongratz 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1584b63b70d8SShlomo Pongratz 			} else {
1585b63b70d8SShlomo Pongratz 				np = &neigh->hnext;
1586b63b70d8SShlomo Pongratz 			}
1587b63b70d8SShlomo Pongratz 
1588b63b70d8SShlomo Pongratz 		}
1589b63b70d8SShlomo Pongratz 	}
1590b63b70d8SShlomo Pongratz out_unlock:
1591b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
1592b63b70d8SShlomo Pongratz }
1593b63b70d8SShlomo Pongratz 
1594b63b70d8SShlomo Pongratz static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1595b63b70d8SShlomo Pongratz {
1596b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1597b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1598b63b70d8SShlomo Pongratz 	unsigned long flags;
159966172c09SShlomo Pongratz 	int i, wait_flushed = 0;
1600b63b70d8SShlomo Pongratz 
160166172c09SShlomo Pongratz 	init_completion(&priv->ntbl.flushed);
1602d2e46fccSFeras Daoud 	set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1603b63b70d8SShlomo Pongratz 
1604b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1605b63b70d8SShlomo Pongratz 
1606b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1607b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock));
1608b63b70d8SShlomo Pongratz 	if (!htbl)
1609b63b70d8SShlomo Pongratz 		goto out_unlock;
1610b63b70d8SShlomo Pongratz 
161166172c09SShlomo Pongratz 	wait_flushed = atomic_read(&priv->ntbl.entries);
161266172c09SShlomo Pongratz 	if (!wait_flushed)
161366172c09SShlomo Pongratz 		goto free_htbl;
161466172c09SShlomo Pongratz 
1615b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1616b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1617b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1618b63b70d8SShlomo Pongratz 
1619b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1620b5120a6eSShlomo Pongratz 				       lockdep_is_held(&priv->lock))) != NULL) {
1621b63b70d8SShlomo Pongratz 			rcu_assign_pointer(*np,
1622b63b70d8SShlomo Pongratz 					   rcu_dereference_protected(neigh->hnext,
1623b5120a6eSShlomo Pongratz 								     lockdep_is_held(&priv->lock)));
1624b63b70d8SShlomo Pongratz 			/* remove from path/mc list */
1625c586071dSFeras Daoud 			list_del_init(&neigh->list);
1626b63b70d8SShlomo Pongratz 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1627b63b70d8SShlomo Pongratz 		}
1628b63b70d8SShlomo Pongratz 	}
1629b63b70d8SShlomo Pongratz 
163066172c09SShlomo Pongratz free_htbl:
1631b63b70d8SShlomo Pongratz 	rcu_assign_pointer(ntbl->htbl, NULL);
1632b63b70d8SShlomo Pongratz 	call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1633b63b70d8SShlomo Pongratz 
1634b63b70d8SShlomo Pongratz out_unlock:
1635b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
163666172c09SShlomo Pongratz 	if (wait_flushed)
163766172c09SShlomo Pongratz 		wait_for_completion(&priv->ntbl.flushed);
1638b63b70d8SShlomo Pongratz }
1639b63b70d8SShlomo Pongratz 
1640b63b70d8SShlomo Pongratz static void ipoib_neigh_hash_uninit(struct net_device *dev)
1641b63b70d8SShlomo Pongratz {
1642c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1643b63b70d8SShlomo Pongratz 
1644b63b70d8SShlomo Pongratz 	ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
164566172c09SShlomo Pongratz 	init_completion(&priv->ntbl.deleted);
1646b63b70d8SShlomo Pongratz 
1647cda8daf1SErez Shitrit 	cancel_delayed_work_sync(&priv->neigh_reap_task);
1648b63b70d8SShlomo Pongratz 
1649b63b70d8SShlomo Pongratz 	ipoib_flush_neighs(priv);
165066172c09SShlomo Pongratz 
165166172c09SShlomo Pongratz 	wait_for_completion(&priv->ntbl.deleted);
1652b63b70d8SShlomo Pongratz }
1653b63b70d8SShlomo Pongratz 
16548966e28dSErez Shitrit static void ipoib_napi_add(struct net_device *dev)
16558966e28dSErez Shitrit {
16568966e28dSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
16578966e28dSErez Shitrit 
16588966e28dSErez Shitrit 	netif_napi_add(dev, &priv->recv_napi, ipoib_rx_poll, IPOIB_NUM_WC);
16598966e28dSErez Shitrit 	netif_napi_add(dev, &priv->send_napi, ipoib_tx_poll, MAX_SEND_CQE);
16608966e28dSErez Shitrit }
16618966e28dSErez Shitrit 
16628966e28dSErez Shitrit static void ipoib_napi_del(struct net_device *dev)
16638966e28dSErez Shitrit {
16648966e28dSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
16658966e28dSErez Shitrit 
16668966e28dSErez Shitrit 	netif_napi_del(&priv->recv_napi);
16678966e28dSErez Shitrit 	netif_napi_del(&priv->send_napi);
16688966e28dSErez Shitrit }
16698966e28dSErez Shitrit 
16700a1a9726SLeon Romanovsky static void ipoib_dev_uninit_default(struct net_device *dev)
1671515ed4f3SErez Shitrit {
1672c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1673b63b70d8SShlomo Pongratz 
1674515ed4f3SErez Shitrit 	ipoib_transport_dev_cleanup(dev);
1675515ed4f3SErez Shitrit 
16768966e28dSErez Shitrit 	ipoib_napi_del(dev);
1677b53d4566SAlex Vesker 
1678515ed4f3SErez Shitrit 	ipoib_cm_dev_cleanup(dev);
1679515ed4f3SErez Shitrit 
1680515ed4f3SErez Shitrit 	kfree(priv->rx_ring);
1681515ed4f3SErez Shitrit 	vfree(priv->tx_ring);
1682515ed4f3SErez Shitrit 
1683515ed4f3SErez Shitrit 	priv->rx_ring = NULL;
1684515ed4f3SErez Shitrit 	priv->tx_ring = NULL;
1685515ed4f3SErez Shitrit }
1686515ed4f3SErez Shitrit 
1687cd565b4bSErez Shitrit static int ipoib_dev_init_default(struct net_device *dev)
16881da177e4SLinus Torvalds {
1689c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
16901da177e4SLinus Torvalds 
16918966e28dSErez Shitrit 	ipoib_napi_add(dev);
1692cd565b4bSErez Shitrit 
16931da177e4SLinus Torvalds 	/* Allocate RX/TX "rings" to hold queued skbs */
16946396bb22SKees Cook 	priv->rx_ring =	kcalloc(ipoib_recvq_size,
16956396bb22SKees Cook 				       sizeof(*priv->rx_ring),
16961da177e4SLinus Torvalds 				       GFP_KERNEL);
169774226649SLeon Romanovsky 	if (!priv->rx_ring)
1698be7aa663SDoug Ledford 		goto out;
16991da177e4SLinus Torvalds 
1700fad953ceSKees Cook 	priv->tx_ring = vzalloc(array_size(ipoib_sendq_size,
1701fad953ceSKees Cook 					   sizeof(*priv->tx_ring)));
17021da177e4SLinus Torvalds 	if (!priv->tx_ring) {
1703c55359a2SYuval Shaia 		pr_warn("%s: failed to allocate TX ring (%d entries)\n",
1704cd565b4bSErez Shitrit 			priv->ca->name, ipoib_sendq_size);
17051da177e4SLinus Torvalds 		goto out_rx_ring_cleanup;
17061da177e4SLinus Torvalds 	}
17071da177e4SLinus Torvalds 
17081b524963SMichael S. Tsirkin 	/* priv->tx_head, tx_tail & tx_outstanding are already 0 */
17091da177e4SLinus Torvalds 
1710cd565b4bSErez Shitrit 	if (ipoib_transport_dev_init(dev, priv->ca)) {
1711cd565b4bSErez Shitrit 		pr_warn("%s: ipoib_transport_dev_init failed\n",
1712cd565b4bSErez Shitrit 			priv->ca->name);
17131da177e4SLinus Torvalds 		goto out_tx_ring_cleanup;
1714515ed4f3SErez Shitrit 	}
17151da177e4SLinus Torvalds 
1716cd565b4bSErez Shitrit 	/* after qp created set dev address */
1717cd565b4bSErez Shitrit 	priv->dev->dev_addr[1] = (priv->qp->qp_num >> 16) & 0xff;
1718cd565b4bSErez Shitrit 	priv->dev->dev_addr[2] = (priv->qp->qp_num >>  8) & 0xff;
1719cd565b4bSErez Shitrit 	priv->dev->dev_addr[3] = (priv->qp->qp_num) & 0xff;
1720cd565b4bSErez Shitrit 
17211da177e4SLinus Torvalds 	return 0;
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds out_tx_ring_cleanup:
172410313cbbSRoland Dreier 	vfree(priv->tx_ring);
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds out_rx_ring_cleanup:
17271da177e4SLinus Torvalds 	kfree(priv->rx_ring);
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds out:
17308966e28dSErez Shitrit 	ipoib_napi_del(dev);
17311da177e4SLinus Torvalds 	return -ENOMEM;
17321da177e4SLinus Torvalds }
17331da177e4SLinus Torvalds 
173431a82362SFeras Daoud static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
173531a82362SFeras Daoud 		       int cmd)
173631a82362SFeras Daoud {
173731a82362SFeras Daoud 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
173831a82362SFeras Daoud 
173931a82362SFeras Daoud 	if (!priv->rn_ops->ndo_do_ioctl)
174031a82362SFeras Daoud 		return -EOPNOTSUPP;
174131a82362SFeras Daoud 
174231a82362SFeras Daoud 	return priv->rn_ops->ndo_do_ioctl(dev, ifr, cmd);
174331a82362SFeras Daoud }
174431a82362SFeras Daoud 
1745eaeb3984SJason Gunthorpe static int ipoib_dev_init(struct net_device *dev)
1746515ed4f3SErez Shitrit {
1747c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1748515ed4f3SErez Shitrit 	int ret = -ENOMEM;
1749515ed4f3SErez Shitrit 
1750515ed4f3SErez Shitrit 	priv->qp = NULL;
1751515ed4f3SErez Shitrit 
1752515ed4f3SErez Shitrit 	/*
1753515ed4f3SErez Shitrit 	 * the various IPoIB tasks assume they will never race against
1754515ed4f3SErez Shitrit 	 * themselves, so always use a single thread workqueue
1755515ed4f3SErez Shitrit 	 */
1756515ed4f3SErez Shitrit 	priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM);
1757515ed4f3SErez Shitrit 	if (!priv->wq) {
1758515ed4f3SErez Shitrit 		pr_warn("%s: failed to allocate device WQ\n", dev->name);
1759515ed4f3SErez Shitrit 		goto out;
1760515ed4f3SErez Shitrit 	}
1761515ed4f3SErez Shitrit 
1762515ed4f3SErez Shitrit 	/* create pd, which used both for control and datapath*/
1763515ed4f3SErez Shitrit 	priv->pd = ib_alloc_pd(priv->ca, 0);
1764515ed4f3SErez Shitrit 	if (IS_ERR(priv->pd)) {
1765eaeb3984SJason Gunthorpe 		pr_warn("%s: failed to allocate PD\n", priv->ca->name);
1766515ed4f3SErez Shitrit 		goto clean_wq;
1767515ed4f3SErez Shitrit 	}
1768515ed4f3SErez Shitrit 
1769cd565b4bSErez Shitrit 	ret = priv->rn_ops->ndo_init(dev);
1770515ed4f3SErez Shitrit 	if (ret) {
1771515ed4f3SErez Shitrit 		pr_warn("%s failed to init HW resource\n", dev->name);
1772515ed4f3SErez Shitrit 		goto out_free_pd;
1773515ed4f3SErez Shitrit 	}
1774515ed4f3SErez Shitrit 
177599a7e2bfSWei Yongjun 	ret = ipoib_neigh_hash_init(priv);
177699a7e2bfSWei Yongjun 	if (ret) {
1777515ed4f3SErez Shitrit 		pr_warn("%s failed to init neigh hash\n", dev->name);
1778515ed4f3SErez Shitrit 		goto out_dev_uninit;
1779515ed4f3SErez Shitrit 	}
1780515ed4f3SErez Shitrit 
1781515ed4f3SErez Shitrit 	if (dev->flags & IFF_UP) {
1782515ed4f3SErez Shitrit 		if (ipoib_ib_dev_open(dev)) {
1783515ed4f3SErez Shitrit 			pr_warn("%s failed to open device\n", dev->name);
1784515ed4f3SErez Shitrit 			ret = -ENODEV;
1785cda8daf1SErez Shitrit 			goto out_hash_uninit;
1786515ed4f3SErez Shitrit 		}
1787515ed4f3SErez Shitrit 	}
1788515ed4f3SErez Shitrit 
1789515ed4f3SErez Shitrit 	return 0;
1790515ed4f3SErez Shitrit 
1791cda8daf1SErez Shitrit out_hash_uninit:
1792cda8daf1SErez Shitrit 	ipoib_neigh_hash_uninit(dev);
1793cda8daf1SErez Shitrit 
1794515ed4f3SErez Shitrit out_dev_uninit:
1795515ed4f3SErez Shitrit 	ipoib_ib_dev_cleanup(dev);
1796515ed4f3SErez Shitrit 
1797515ed4f3SErez Shitrit out_free_pd:
1798515ed4f3SErez Shitrit 	if (priv->pd) {
1799515ed4f3SErez Shitrit 		ib_dealloc_pd(priv->pd);
1800515ed4f3SErez Shitrit 		priv->pd = NULL;
1801515ed4f3SErez Shitrit 	}
1802515ed4f3SErez Shitrit 
1803515ed4f3SErez Shitrit clean_wq:
1804515ed4f3SErez Shitrit 	if (priv->wq) {
1805515ed4f3SErez Shitrit 		destroy_workqueue(priv->wq);
1806515ed4f3SErez Shitrit 		priv->wq = NULL;
1807515ed4f3SErez Shitrit 	}
1808515ed4f3SErez Shitrit 
1809515ed4f3SErez Shitrit out:
1810515ed4f3SErez Shitrit 	return ret;
1811515ed4f3SErez Shitrit }
1812515ed4f3SErez Shitrit 
18137cbee87cSJason Gunthorpe /*
18147cbee87cSJason Gunthorpe  * This must be called before doing an unregister_netdev on a parent device to
18157cbee87cSJason Gunthorpe  * shutdown the IB event handler.
18167cbee87cSJason Gunthorpe  */
18177cbee87cSJason Gunthorpe static void ipoib_parent_unregister_pre(struct net_device *ndev)
18187cbee87cSJason Gunthorpe {
18197cbee87cSJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
18207cbee87cSJason Gunthorpe 
18217cbee87cSJason Gunthorpe 	/*
18227cbee87cSJason Gunthorpe 	 * ipoib_set_mac checks netif_running before pushing work, clearing
18237cbee87cSJason Gunthorpe 	 * running ensures the it will not add more work.
18247cbee87cSJason Gunthorpe 	 */
18257cbee87cSJason Gunthorpe 	rtnl_lock();
1826567c5e13SPetr Machata 	dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP, NULL);
18277cbee87cSJason Gunthorpe 	rtnl_unlock();
18287cbee87cSJason Gunthorpe 
18297cbee87cSJason Gunthorpe 	/* ipoib_event() cannot be running once this returns */
18307cbee87cSJason Gunthorpe 	ib_unregister_event_handler(&priv->event_handler);
18317cbee87cSJason Gunthorpe 
18327cbee87cSJason Gunthorpe 	/*
18337cbee87cSJason Gunthorpe 	 * Work on the queue grabs the rtnl lock, so this cannot be done while
18347cbee87cSJason Gunthorpe 	 * also holding it.
18357cbee87cSJason Gunthorpe 	 */
18367cbee87cSJason Gunthorpe 	flush_workqueue(ipoib_workqueue);
18377cbee87cSJason Gunthorpe }
18387cbee87cSJason Gunthorpe 
1839eaeb3984SJason Gunthorpe static void ipoib_set_dev_features(struct ipoib_dev_priv *priv)
1840eaeb3984SJason Gunthorpe {
1841eaeb3984SJason Gunthorpe 	priv->hca_caps = priv->ca->attrs.device_cap_flags;
1842eaeb3984SJason Gunthorpe 
1843eaeb3984SJason Gunthorpe 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1844eaeb3984SJason Gunthorpe 		priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1845eaeb3984SJason Gunthorpe 
1846eaeb3984SJason Gunthorpe 		if (priv->hca_caps & IB_DEVICE_UD_TSO)
1847eaeb3984SJason Gunthorpe 			priv->dev->hw_features |= NETIF_F_TSO;
1848eaeb3984SJason Gunthorpe 
1849eaeb3984SJason Gunthorpe 		priv->dev->features |= priv->dev->hw_features;
1850eaeb3984SJason Gunthorpe 	}
1851eaeb3984SJason Gunthorpe }
1852eaeb3984SJason Gunthorpe 
1853eaeb3984SJason Gunthorpe static int ipoib_parent_init(struct net_device *ndev)
1854eaeb3984SJason Gunthorpe {
1855eaeb3984SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1856eaeb3984SJason Gunthorpe 	struct ib_port_attr attr;
1857eaeb3984SJason Gunthorpe 	int result;
1858eaeb3984SJason Gunthorpe 
1859eaeb3984SJason Gunthorpe 	result = ib_query_port(priv->ca, priv->port, &attr);
1860eaeb3984SJason Gunthorpe 	if (result) {
1861eaeb3984SJason Gunthorpe 		pr_warn("%s: ib_query_port %d failed\n", priv->ca->name,
1862eaeb3984SJason Gunthorpe 			priv->port);
1863eaeb3984SJason Gunthorpe 		return result;
1864eaeb3984SJason Gunthorpe 	}
1865eaeb3984SJason Gunthorpe 	priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1866eaeb3984SJason Gunthorpe 
1867eaeb3984SJason Gunthorpe 	result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey);
1868eaeb3984SJason Gunthorpe 	if (result) {
1869eaeb3984SJason Gunthorpe 		pr_warn("%s: ib_query_pkey port %d failed (ret = %d)\n",
1870eaeb3984SJason Gunthorpe 			priv->ca->name, priv->port, result);
1871eaeb3984SJason Gunthorpe 		return result;
1872eaeb3984SJason Gunthorpe 	}
1873eaeb3984SJason Gunthorpe 
1874eaeb3984SJason Gunthorpe 	result = rdma_query_gid(priv->ca, priv->port, 0, &priv->local_gid);
1875eaeb3984SJason Gunthorpe 	if (result) {
1876eaeb3984SJason Gunthorpe 		pr_warn("%s: rdma_query_gid port %d failed (ret = %d)\n",
1877eaeb3984SJason Gunthorpe 			priv->ca->name, priv->port, result);
1878eaeb3984SJason Gunthorpe 		return result;
1879eaeb3984SJason Gunthorpe 	}
1880eaeb3984SJason Gunthorpe 	memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw,
1881eaeb3984SJason Gunthorpe 	       sizeof(union ib_gid));
1882eaeb3984SJason Gunthorpe 
1883eaeb3984SJason Gunthorpe 	SET_NETDEV_DEV(priv->dev, priv->ca->dev.parent);
18849b8b2a32SArseny Maslennikov 	priv->dev->dev_port = priv->port - 1;
18859b8b2a32SArseny Maslennikov 	/* Let's set this one too for backwards compatibility. */
1886eaeb3984SJason Gunthorpe 	priv->dev->dev_id = priv->port - 1;
1887eaeb3984SJason Gunthorpe 
1888eaeb3984SJason Gunthorpe 	return 0;
1889eaeb3984SJason Gunthorpe }
1890eaeb3984SJason Gunthorpe 
1891eaeb3984SJason Gunthorpe static void ipoib_child_init(struct net_device *ndev)
1892eaeb3984SJason Gunthorpe {
1893eaeb3984SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1894eaeb3984SJason Gunthorpe 	struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
1895eaeb3984SJason Gunthorpe 
189613476d35SJason Gunthorpe 	dev_hold(priv->parent);
189713476d35SJason Gunthorpe 
189813476d35SJason Gunthorpe 	down_write(&ppriv->vlan_rwsem);
189913476d35SJason Gunthorpe 	list_add_tail(&priv->list, &ppriv->child_intfs);
190013476d35SJason Gunthorpe 	up_write(&ppriv->vlan_rwsem);
190113476d35SJason Gunthorpe 
1902eaeb3984SJason Gunthorpe 	priv->max_ib_mtu = ppriv->max_ib_mtu;
1903eaeb3984SJason Gunthorpe 	set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1904eaeb3984SJason Gunthorpe 	memcpy(priv->dev->dev_addr, ppriv->dev->dev_addr, INFINIBAND_ALEN);
1905eaeb3984SJason Gunthorpe 	memcpy(&priv->local_gid, &ppriv->local_gid, sizeof(priv->local_gid));
1906eaeb3984SJason Gunthorpe }
1907eaeb3984SJason Gunthorpe 
1908eaeb3984SJason Gunthorpe static int ipoib_ndo_init(struct net_device *ndev)
1909eaeb3984SJason Gunthorpe {
1910eaeb3984SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1911eaeb3984SJason Gunthorpe 	int rc;
1912eaeb3984SJason Gunthorpe 
1913eaeb3984SJason Gunthorpe 	if (priv->parent) {
1914eaeb3984SJason Gunthorpe 		ipoib_child_init(ndev);
1915eaeb3984SJason Gunthorpe 	} else {
1916eaeb3984SJason Gunthorpe 		rc = ipoib_parent_init(ndev);
1917eaeb3984SJason Gunthorpe 		if (rc)
1918eaeb3984SJason Gunthorpe 			return rc;
1919eaeb3984SJason Gunthorpe 	}
1920eaeb3984SJason Gunthorpe 
1921eaeb3984SJason Gunthorpe 	/* MTU will be reset when mcast join happens */
1922eaeb3984SJason Gunthorpe 	ndev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1923eaeb3984SJason Gunthorpe 	priv->mcast_mtu = priv->admin_mtu = ndev->mtu;
1924eaeb3984SJason Gunthorpe 	ndev->max_mtu = IPOIB_CM_MTU;
1925eaeb3984SJason Gunthorpe 
1926eaeb3984SJason Gunthorpe 	ndev->neigh_priv_len = sizeof(struct ipoib_neigh);
1927eaeb3984SJason Gunthorpe 
1928eaeb3984SJason Gunthorpe 	/*
1929eaeb3984SJason Gunthorpe 	 * Set the full membership bit, so that we join the right
1930eaeb3984SJason Gunthorpe 	 * broadcast group, etc.
1931eaeb3984SJason Gunthorpe 	 */
1932eaeb3984SJason Gunthorpe 	priv->pkey |= 0x8000;
1933eaeb3984SJason Gunthorpe 
1934eaeb3984SJason Gunthorpe 	ndev->broadcast[8] = priv->pkey >> 8;
1935eaeb3984SJason Gunthorpe 	ndev->broadcast[9] = priv->pkey & 0xff;
1936eaeb3984SJason Gunthorpe 	set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
1937eaeb3984SJason Gunthorpe 
1938eaeb3984SJason Gunthorpe 	ipoib_set_dev_features(priv);
1939eaeb3984SJason Gunthorpe 
1940eaeb3984SJason Gunthorpe 	rc = ipoib_dev_init(ndev);
1941eaeb3984SJason Gunthorpe 	if (rc) {
1942eaeb3984SJason Gunthorpe 		pr_warn("%s: failed to initialize device: %s port %d (ret = %d)\n",
1943eaeb3984SJason Gunthorpe 			priv->ca->name, priv->dev->name, priv->port, rc);
1944eaeb3984SJason Gunthorpe 	}
1945eaeb3984SJason Gunthorpe 
1946eaeb3984SJason Gunthorpe 	return 0;
1947eaeb3984SJason Gunthorpe }
1948eaeb3984SJason Gunthorpe 
19497cbee87cSJason Gunthorpe static void ipoib_ndo_uninit(struct net_device *dev)
19501da177e4SLinus Torvalds {
195125405d98SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
19529baa0b03SOr Gerlitz 
19539baa0b03SOr Gerlitz 	ASSERT_RTNL();
19541da177e4SLinus Torvalds 
195525405d98SJason Gunthorpe 	/*
195625405d98SJason Gunthorpe 	 * ipoib_remove_one guarantees the children are removed before the
195725405d98SJason Gunthorpe 	 * parent, and that is the only place where a parent can be removed.
195825405d98SJason Gunthorpe 	 */
195925405d98SJason Gunthorpe 	WARN_ON(!list_empty(&priv->child_intfs));
19601da177e4SLinus Torvalds 
1961be7aa663SDoug Ledford 	ipoib_neigh_hash_uninit(dev);
1962be7aa663SDoug Ledford 
19631da177e4SLinus Torvalds 	ipoib_ib_dev_cleanup(dev);
19641da177e4SLinus Torvalds 
1965515ed4f3SErez Shitrit 	/* no more works over the priv->wq */
1966515ed4f3SErez Shitrit 	if (priv->wq) {
1967515ed4f3SErez Shitrit 		flush_workqueue(priv->wq);
1968515ed4f3SErez Shitrit 		destroy_workqueue(priv->wq);
1969515ed4f3SErez Shitrit 		priv->wq = NULL;
1970515ed4f3SErez Shitrit 	}
197113476d35SJason Gunthorpe 
197213476d35SJason Gunthorpe 	if (priv->parent) {
197313476d35SJason Gunthorpe 		struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
197413476d35SJason Gunthorpe 
197513476d35SJason Gunthorpe 		down_write(&ppriv->vlan_rwsem);
197613476d35SJason Gunthorpe 		list_del(&priv->list);
197713476d35SJason Gunthorpe 		up_write(&ppriv->vlan_rwsem);
197813476d35SJason Gunthorpe 
197913476d35SJason Gunthorpe 		dev_put(priv->parent);
198013476d35SJason Gunthorpe 	}
19811da177e4SLinus Torvalds }
19821da177e4SLinus Torvalds 
19839c3c5f8eSEli Cohen static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
19849c3c5f8eSEli Cohen {
1985c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
19869c3c5f8eSEli Cohen 
19879c3c5f8eSEli Cohen 	return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
19889c3c5f8eSEli Cohen }
19899c3c5f8eSEli Cohen 
19909c3c5f8eSEli Cohen static int ipoib_get_vf_config(struct net_device *dev, int vf,
19919c3c5f8eSEli Cohen 			       struct ifla_vf_info *ivf)
19929c3c5f8eSEli Cohen {
1993c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
19949c3c5f8eSEli Cohen 	int err;
19959c3c5f8eSEli Cohen 
19969c3c5f8eSEli Cohen 	err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
19979c3c5f8eSEli Cohen 	if (err)
19989c3c5f8eSEli Cohen 		return err;
19999c3c5f8eSEli Cohen 
20009c3c5f8eSEli Cohen 	ivf->vf = vf;
20019c3c5f8eSEli Cohen 
20029c3c5f8eSEli Cohen 	return 0;
20039c3c5f8eSEli Cohen }
20049c3c5f8eSEli Cohen 
20059c3c5f8eSEli Cohen static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
20069c3c5f8eSEli Cohen {
2007c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
20089c3c5f8eSEli Cohen 
20099c3c5f8eSEli Cohen 	if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
20109c3c5f8eSEli Cohen 		return -EINVAL;
20119c3c5f8eSEli Cohen 
20129c3c5f8eSEli Cohen 	return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
20139c3c5f8eSEli Cohen }
20149c3c5f8eSEli Cohen 
20159c3c5f8eSEli Cohen static int ipoib_get_vf_stats(struct net_device *dev, int vf,
20169c3c5f8eSEli Cohen 			      struct ifla_vf_stats *vf_stats)
20179c3c5f8eSEli Cohen {
2018c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
20199c3c5f8eSEli Cohen 
20209c3c5f8eSEli Cohen 	return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
20219c3c5f8eSEli Cohen }
20229c3c5f8eSEli Cohen 
20233b04dddeSStephen Hemminger static const struct header_ops ipoib_header_ops = {
20243b04dddeSStephen Hemminger 	.create	= ipoib_hard_header,
20253b04dddeSStephen Hemminger };
20263b04dddeSStephen Hemminger 
20279c3c5f8eSEli Cohen static const struct net_device_ops ipoib_netdev_ops_pf = {
2028eaeb3984SJason Gunthorpe 	.ndo_init		 = ipoib_ndo_init,
20297cbee87cSJason Gunthorpe 	.ndo_uninit		 = ipoib_ndo_uninit,
20309c3c5f8eSEli Cohen 	.ndo_open		 = ipoib_open,
20319c3c5f8eSEli Cohen 	.ndo_stop		 = ipoib_stop,
20329c3c5f8eSEli Cohen 	.ndo_change_mtu		 = ipoib_change_mtu,
20339c3c5f8eSEli Cohen 	.ndo_fix_features	 = ipoib_fix_features,
20349c3c5f8eSEli Cohen 	.ndo_start_xmit		 = ipoib_start_xmit,
20359c3c5f8eSEli Cohen 	.ndo_tx_timeout		 = ipoib_timeout,
20369c3c5f8eSEli Cohen 	.ndo_set_rx_mode	 = ipoib_set_mcast_list,
20379c3c5f8eSEli Cohen 	.ndo_get_iflink		 = ipoib_get_iflink,
20389c3c5f8eSEli Cohen 	.ndo_set_vf_link_state	 = ipoib_set_vf_link_state,
20399c3c5f8eSEli Cohen 	.ndo_get_vf_config	 = ipoib_get_vf_config,
20409c3c5f8eSEli Cohen 	.ndo_get_vf_stats	 = ipoib_get_vf_stats,
20419c3c5f8eSEli Cohen 	.ndo_set_vf_guid	 = ipoib_set_vf_guid,
2042492a7e67SMark Bloch 	.ndo_set_mac_address	 = ipoib_set_mac,
2043b6c871e5SErez Shitrit 	.ndo_get_stats64	 = ipoib_get_stats,
204431a82362SFeras Daoud 	.ndo_do_ioctl		 = ipoib_ioctl,
20459c3c5f8eSEli Cohen };
20469c3c5f8eSEli Cohen 
20479c3c5f8eSEli Cohen static const struct net_device_ops ipoib_netdev_ops_vf = {
2048eaeb3984SJason Gunthorpe 	.ndo_init		 = ipoib_ndo_init,
20497cbee87cSJason Gunthorpe 	.ndo_uninit		 = ipoib_ndo_uninit,
2050fe8114e8SStephen Hemminger 	.ndo_open		 = ipoib_open,
2051fe8114e8SStephen Hemminger 	.ndo_stop		 = ipoib_stop,
2052fe8114e8SStephen Hemminger 	.ndo_change_mtu		 = ipoib_change_mtu,
20533d96c74dSMichał Mirosław 	.ndo_fix_features	 = ipoib_fix_features,
2054fe8114e8SStephen Hemminger 	.ndo_start_xmit	 	 = ipoib_start_xmit,
2055fe8114e8SStephen Hemminger 	.ndo_tx_timeout		 = ipoib_timeout,
2056afc4b13dSJiri Pirko 	.ndo_set_rx_mode	 = ipoib_set_mcast_list,
20575aa7add8SNicolas Dichtel 	.ndo_get_iflink		 = ipoib_get_iflink,
2058eb54714dSFeras Daoud 	.ndo_get_stats64	 = ipoib_get_stats,
205931a82362SFeras Daoud 	.ndo_do_ioctl		 = ipoib_ioctl,
2060fe8114e8SStephen Hemminger };
2061fe8114e8SStephen Hemminger 
2062cd565b4bSErez Shitrit void ipoib_setup_common(struct net_device *dev)
20631da177e4SLinus Torvalds {
20643b04dddeSStephen Hemminger 	dev->header_ops		 = &ipoib_header_ops;
2065bea3348eSStephen Hemminger 
206682c24c18SEli Cohen 	ipoib_set_ethtool_ops(dev);
206782c24c18SEli Cohen 
20681da177e4SLinus Torvalds 	dev->watchdog_timeo	 = HZ;
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	dev->flags		|= IFF_BROADCAST | IFF_MULTICAST;
20711da177e4SLinus Torvalds 
2072fc791b63SPaolo Abeni 	dev->hard_header_len	 = IPOIB_HARD_LEN;
20731da177e4SLinus Torvalds 	dev->addr_len		 = INFINIBAND_ALEN;
20741da177e4SLinus Torvalds 	dev->type		 = ARPHRD_INFINIBAND;
20750f485251SShirley Ma 	dev->tx_queue_len	 = ipoib_sendq_size * 2;
2076eb14032fSEli Cohen 	dev->features		 = (NETIF_F_VLAN_CHALLENGED	|
2077eb14032fSEli Cohen 				    NETIF_F_HIGHDMA);
207802875878SEric Dumazet 	netif_keep_dst(dev);
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
20819f49a5b5SJason Gunthorpe 
20829f49a5b5SJason Gunthorpe 	/*
20839f49a5b5SJason Gunthorpe 	 * unregister_netdev always frees the netdev, we use this mode
20849f49a5b5SJason Gunthorpe 	 * consistently to unify all the various unregister paths, including
20859f49a5b5SJason Gunthorpe 	 * those connected to rtnl_link_ops which require it.
20869f49a5b5SJason Gunthorpe 	 */
20879f49a5b5SJason Gunthorpe 	dev->needs_free_netdev = true;
2088cd565b4bSErez Shitrit }
2089cd565b4bSErez Shitrit 
2090cd565b4bSErez Shitrit static void ipoib_build_priv(struct net_device *dev)
2091cd565b4bSErez Shitrit {
2092cd565b4bSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 	priv->dev = dev;
20951da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
2096f47944ccSErez Shitrit 	init_rwsem(&priv->vlan_rwsem);
2097edf3f301SFeras Daoud 	mutex_init(&priv->mcast_mutex);
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->path_list);
21001da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->child_intfs);
21011da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->dead_ahs);
21021da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->multicast_list);
21031da177e4SLinus Torvalds 
2104c4028958SDavid Howells 	INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
2105e8224e4bSYossi Etigin 	INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
2106ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
2107ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
2108ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
2109c4028958SDavid Howells 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
2110c4028958SDavid Howells 	INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
2111b63b70d8SShlomo Pongratz 	INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
2114cd565b4bSErez Shitrit static const struct net_device_ops ipoib_netdev_default_pf = {
2115cd565b4bSErez Shitrit 	.ndo_init		 = ipoib_dev_init_default,
2116cd565b4bSErez Shitrit 	.ndo_uninit		 = ipoib_dev_uninit_default,
2117cd565b4bSErez Shitrit 	.ndo_open		 = ipoib_ib_dev_open_default,
2118cd565b4bSErez Shitrit 	.ndo_stop		 = ipoib_ib_dev_stop_default,
2119cd565b4bSErez Shitrit };
2120cd565b4bSErez Shitrit 
21215d6b0cb3SDenis Drozdov static struct net_device *ipoib_alloc_netdev(struct ib_device *hca, u8 port,
2122cd565b4bSErez Shitrit 					     const char *name)
2123cd565b4bSErez Shitrit {
2124cd565b4bSErez Shitrit 	struct net_device *dev;
2125cd565b4bSErez Shitrit 
2126f6a8a19bSDenis Drozdov 	dev = rdma_alloc_netdev(hca, port, RDMA_NETDEV_IPOIB, name,
2127f6a8a19bSDenis Drozdov 				NET_NAME_UNKNOWN, ipoib_setup_common);
21285d6b0cb3SDenis Drozdov 	if (!IS_ERR(dev) || PTR_ERR(dev) != -EOPNOTSUPP)
2129cd565b4bSErez Shitrit 		return dev;
2130f6a8a19bSDenis Drozdov 
21315d6b0cb3SDenis Drozdov 	dev = alloc_netdev(sizeof(struct rdma_netdev), name, NET_NAME_UNKNOWN,
2132f6a8a19bSDenis Drozdov 			   ipoib_setup_common);
21335d6b0cb3SDenis Drozdov 	if (!dev)
21345d6b0cb3SDenis Drozdov 		return ERR_PTR(-ENOMEM);
21355d6b0cb3SDenis Drozdov 	return dev;
2136cd565b4bSErez Shitrit }
2137cd565b4bSErez Shitrit 
21385d6b0cb3SDenis Drozdov int ipoib_intf_init(struct ib_device *hca, u8 port, const char *name,
21395d6b0cb3SDenis Drozdov 		    struct net_device *dev)
2140cd565b4bSErez Shitrit {
21415d6b0cb3SDenis Drozdov 	struct rdma_netdev *rn = netdev_priv(dev);
2142cd565b4bSErez Shitrit 	struct ipoib_dev_priv *priv;
21435d6b0cb3SDenis Drozdov 	int rc;
2144cd565b4bSErez Shitrit 
2145cd565b4bSErez Shitrit 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2146cd565b4bSErez Shitrit 	if (!priv)
21475d6b0cb3SDenis Drozdov 		return -ENOMEM;
2148cd565b4bSErez Shitrit 
2149eaeb3984SJason Gunthorpe 	priv->ca = hca;
2150eaeb3984SJason Gunthorpe 	priv->port = port;
2151eaeb3984SJason Gunthorpe 
21525d6b0cb3SDenis Drozdov 	rc = rdma_init_netdev(hca, port, RDMA_NETDEV_IPOIB, name,
21535d6b0cb3SDenis Drozdov 			      NET_NAME_UNKNOWN, ipoib_setup_common, dev);
21545d6b0cb3SDenis Drozdov 	if (rc) {
21555d6b0cb3SDenis Drozdov 		if (rc != -EOPNOTSUPP)
21565d6b0cb3SDenis Drozdov 			goto out;
21575d6b0cb3SDenis Drozdov 
21585d6b0cb3SDenis Drozdov 		dev->netdev_ops = &ipoib_netdev_default_pf;
21595d6b0cb3SDenis Drozdov 		rn->send = ipoib_send;
21605d6b0cb3SDenis Drozdov 		rn->attach_mcast = ipoib_mcast_attach;
21615d6b0cb3SDenis Drozdov 		rn->detach_mcast = ipoib_mcast_detach;
21625d6b0cb3SDenis Drozdov 		rn->hca = hca;
21635d6b0cb3SDenis Drozdov 	}
2164cd565b4bSErez Shitrit 
2165cd565b4bSErez Shitrit 	priv->rn_ops = dev->netdev_ops;
2166cd565b4bSErez Shitrit 
21675d6b0cb3SDenis Drozdov 	if (hca->attrs.device_cap_flags & IB_DEVICE_VIRTUAL_FUNCTION)
2168cd565b4bSErez Shitrit 		dev->netdev_ops	= &ipoib_netdev_ops_vf;
2169cd565b4bSErez Shitrit 	else
2170cd565b4bSErez Shitrit 		dev->netdev_ops	= &ipoib_netdev_ops_pf;
2171cd565b4bSErez Shitrit 
2172cd565b4bSErez Shitrit 	rn->clnt_priv = priv;
21739f49a5b5SJason Gunthorpe 	/*
21749f49a5b5SJason Gunthorpe 	 * Only the child register_netdev flows can handle priv_destructor
21759f49a5b5SJason Gunthorpe 	 * being set, so we force it to NULL here and handle manually until it
21769f49a5b5SJason Gunthorpe 	 * is safe to turn on.
21779f49a5b5SJason Gunthorpe 	 */
21789f49a5b5SJason Gunthorpe 	priv->next_priv_destructor = dev->priv_destructor;
21799f49a5b5SJason Gunthorpe 	dev->priv_destructor = NULL;
21809f49a5b5SJason Gunthorpe 
2181cd565b4bSErez Shitrit 	ipoib_build_priv(dev);
2182cd565b4bSErez Shitrit 
21835d6b0cb3SDenis Drozdov 	return 0;
21845d6b0cb3SDenis Drozdov 
21855d6b0cb3SDenis Drozdov out:
2186cd565b4bSErez Shitrit 	kfree(priv);
21875d6b0cb3SDenis Drozdov 	return rc;
21885d6b0cb3SDenis Drozdov }
21895d6b0cb3SDenis Drozdov 
21905d6b0cb3SDenis Drozdov struct net_device *ipoib_intf_alloc(struct ib_device *hca, u8 port,
21915d6b0cb3SDenis Drozdov 				    const char *name)
21925d6b0cb3SDenis Drozdov {
21935d6b0cb3SDenis Drozdov 	struct net_device *dev;
21945d6b0cb3SDenis Drozdov 	int rc;
21955d6b0cb3SDenis Drozdov 
21965d6b0cb3SDenis Drozdov 	dev = ipoib_alloc_netdev(hca, port, name);
21975d6b0cb3SDenis Drozdov 	if (IS_ERR(dev))
21985d6b0cb3SDenis Drozdov 		return dev;
21995d6b0cb3SDenis Drozdov 
22005d6b0cb3SDenis Drozdov 	rc = ipoib_intf_init(hca, port, name, dev);
22015d6b0cb3SDenis Drozdov 	if (rc) {
22025d6b0cb3SDenis Drozdov 		free_netdev(dev);
22035d6b0cb3SDenis Drozdov 		return ERR_PTR(rc);
22045d6b0cb3SDenis Drozdov 	}
22055d6b0cb3SDenis Drozdov 
22065d6b0cb3SDenis Drozdov 	/*
22075d6b0cb3SDenis Drozdov 	 * Upon success the caller must ensure ipoib_intf_free is called or
22085d6b0cb3SDenis Drozdov 	 * register_netdevice succeed'd and priv_destructor is set to
22095d6b0cb3SDenis Drozdov 	 * ipoib_intf_free.
22105d6b0cb3SDenis Drozdov 	 */
22115d6b0cb3SDenis Drozdov 	return dev;
22121da177e4SLinus Torvalds }
22131da177e4SLinus Torvalds 
22149f49a5b5SJason Gunthorpe void ipoib_intf_free(struct net_device *dev)
22159f49a5b5SJason Gunthorpe {
22169f49a5b5SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
22179f49a5b5SJason Gunthorpe 	struct rdma_netdev *rn = netdev_priv(dev);
22189f49a5b5SJason Gunthorpe 
22199f49a5b5SJason Gunthorpe 	dev->priv_destructor = priv->next_priv_destructor;
22209f49a5b5SJason Gunthorpe 	if (dev->priv_destructor)
22219f49a5b5SJason Gunthorpe 		dev->priv_destructor(dev);
22229f49a5b5SJason Gunthorpe 
22239f49a5b5SJason Gunthorpe 	/*
22249f49a5b5SJason Gunthorpe 	 * There are some error flows around register_netdev failing that may
22259f49a5b5SJason Gunthorpe 	 * attempt to call priv_destructor twice, prevent that from happening.
22269f49a5b5SJason Gunthorpe 	 */
22279f49a5b5SJason Gunthorpe 	dev->priv_destructor = NULL;
22289f49a5b5SJason Gunthorpe 
22299f49a5b5SJason Gunthorpe 	/* unregister/destroy is very complicated. Make bugs more obvious. */
22309f49a5b5SJason Gunthorpe 	rn->clnt_priv = NULL;
22319f49a5b5SJason Gunthorpe 
22329f49a5b5SJason Gunthorpe 	kfree(priv);
22339f49a5b5SJason Gunthorpe }
22349f49a5b5SJason Gunthorpe 
223543cb76d9SGreg Kroah-Hartman static ssize_t show_pkey(struct device *dev,
223643cb76d9SGreg Kroah-Hartman 			 struct device_attribute *attr, char *buf)
22371da177e4SLinus Torvalds {
2238c1048affSErez Shitrit 	struct net_device *ndev = to_net_dev(dev);
2239c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds 	return sprintf(buf, "0x%04x\n", priv->pkey);
22421da177e4SLinus Torvalds }
224343cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
22441da177e4SLinus Torvalds 
2245335a64a5SOr Gerlitz static ssize_t show_umcast(struct device *dev,
2246335a64a5SOr Gerlitz 			   struct device_attribute *attr, char *buf)
2247335a64a5SOr Gerlitz {
2248c1048affSErez Shitrit 	struct net_device *ndev = to_net_dev(dev);
2249c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2250335a64a5SOr Gerlitz 
2251335a64a5SOr Gerlitz 	return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
2252335a64a5SOr Gerlitz }
2253335a64a5SOr Gerlitz 
2254862096a8SOr Gerlitz void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
2255335a64a5SOr Gerlitz {
2256c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2257335a64a5SOr Gerlitz 
2258335a64a5SOr Gerlitz 	if (umcast_val > 0) {
2259335a64a5SOr Gerlitz 		set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2260335a64a5SOr Gerlitz 		ipoib_warn(priv, "ignoring multicast groups joined directly "
2261335a64a5SOr Gerlitz 				"by userspace\n");
2262335a64a5SOr Gerlitz 	} else
2263335a64a5SOr Gerlitz 		clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2264862096a8SOr Gerlitz }
2265862096a8SOr Gerlitz 
2266862096a8SOr Gerlitz static ssize_t set_umcast(struct device *dev,
2267862096a8SOr Gerlitz 			  struct device_attribute *attr,
2268862096a8SOr Gerlitz 			  const char *buf, size_t count)
2269862096a8SOr Gerlitz {
2270862096a8SOr Gerlitz 	unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
2271862096a8SOr Gerlitz 
2272862096a8SOr Gerlitz 	ipoib_set_umcast(to_net_dev(dev), umcast_val);
2273335a64a5SOr Gerlitz 
2274335a64a5SOr Gerlitz 	return count;
2275335a64a5SOr Gerlitz }
2276335a64a5SOr Gerlitz static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
2277335a64a5SOr Gerlitz 
2278335a64a5SOr Gerlitz int ipoib_add_umcast_attr(struct net_device *dev)
2279335a64a5SOr Gerlitz {
2280335a64a5SOr Gerlitz 	return device_create_file(&dev->dev, &dev_attr_umcast);
2281335a64a5SOr Gerlitz }
2282335a64a5SOr Gerlitz 
2283492a7e67SMark Bloch static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
2284492a7e67SMark Bloch {
2285492a7e67SMark Bloch 	struct ipoib_dev_priv *child_priv;
2286492a7e67SMark Bloch 	struct net_device *netdev = priv->dev;
2287492a7e67SMark Bloch 
22889b29953bSMark Bloch 	netif_addr_lock_bh(netdev);
2289492a7e67SMark Bloch 
2290492a7e67SMark Bloch 	memcpy(&priv->local_gid.global.interface_id,
2291492a7e67SMark Bloch 	       &gid->global.interface_id,
2292492a7e67SMark Bloch 	       sizeof(gid->global.interface_id));
2293492a7e67SMark Bloch 	memcpy(netdev->dev_addr + 4, &priv->local_gid, sizeof(priv->local_gid));
2294492a7e67SMark Bloch 	clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2295492a7e67SMark Bloch 
22969b29953bSMark Bloch 	netif_addr_unlock_bh(netdev);
2297492a7e67SMark Bloch 
2298492a7e67SMark Bloch 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
2299492a7e67SMark Bloch 		down_read(&priv->vlan_rwsem);
2300492a7e67SMark Bloch 		list_for_each_entry(child_priv, &priv->child_intfs, list)
2301492a7e67SMark Bloch 			set_base_guid(child_priv, gid);
2302492a7e67SMark Bloch 		up_read(&priv->vlan_rwsem);
2303492a7e67SMark Bloch 	}
2304492a7e67SMark Bloch }
2305492a7e67SMark Bloch 
2306492a7e67SMark Bloch static int ipoib_check_lladdr(struct net_device *dev,
2307492a7e67SMark Bloch 			      struct sockaddr_storage *ss)
2308492a7e67SMark Bloch {
2309492a7e67SMark Bloch 	union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
2310492a7e67SMark Bloch 	int ret = 0;
2311492a7e67SMark Bloch 
23129b29953bSMark Bloch 	netif_addr_lock_bh(dev);
2313492a7e67SMark Bloch 
2314492a7e67SMark Bloch 	/* Make sure the QPN, reserved and subnet prefix match the current
2315492a7e67SMark Bloch 	 * lladdr, it also makes sure the lladdr is unicast.
2316492a7e67SMark Bloch 	 */
2317492a7e67SMark Bloch 	if (memcmp(dev->dev_addr, ss->__data,
2318492a7e67SMark Bloch 		   4 + sizeof(gid->global.subnet_prefix)) ||
2319492a7e67SMark Bloch 	    gid->global.interface_id == 0)
2320492a7e67SMark Bloch 		ret = -EINVAL;
2321492a7e67SMark Bloch 
23229b29953bSMark Bloch 	netif_addr_unlock_bh(dev);
2323492a7e67SMark Bloch 
2324492a7e67SMark Bloch 	return ret;
2325492a7e67SMark Bloch }
2326492a7e67SMark Bloch 
2327492a7e67SMark Bloch static int ipoib_set_mac(struct net_device *dev, void *addr)
2328492a7e67SMark Bloch {
2329c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
2330492a7e67SMark Bloch 	struct sockaddr_storage *ss = addr;
2331492a7e67SMark Bloch 	int ret;
2332492a7e67SMark Bloch 
2333492a7e67SMark Bloch 	if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
2334492a7e67SMark Bloch 		return -EBUSY;
2335492a7e67SMark Bloch 
2336492a7e67SMark Bloch 	ret = ipoib_check_lladdr(dev, ss);
2337492a7e67SMark Bloch 	if (ret)
2338492a7e67SMark Bloch 		return ret;
2339492a7e67SMark Bloch 
2340492a7e67SMark Bloch 	set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
2341492a7e67SMark Bloch 
2342492a7e67SMark Bloch 	queue_work(ipoib_workqueue, &priv->flush_light);
2343492a7e67SMark Bloch 
2344492a7e67SMark Bloch 	return 0;
2345492a7e67SMark Bloch }
2346492a7e67SMark Bloch 
234743cb76d9SGreg Kroah-Hartman static ssize_t create_child(struct device *dev,
234843cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr,
23491da177e4SLinus Torvalds 			    const char *buf, size_t count)
23501da177e4SLinus Torvalds {
23511da177e4SLinus Torvalds 	int pkey;
23521da177e4SLinus Torvalds 	int ret;
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
23551da177e4SLinus Torvalds 		return -EINVAL;
23561da177e4SLinus Torvalds 
23573d790a4cSOr Gerlitz 	if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
23581da177e4SLinus Torvalds 		return -EINVAL;
23591da177e4SLinus Torvalds 
236043cb76d9SGreg Kroah-Hartman 	ret = ipoib_vlan_add(to_net_dev(dev), pkey);
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	return ret ? ret : count;
23631da177e4SLinus Torvalds }
23647a52b34bSOr Gerlitz static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
23651da177e4SLinus Torvalds 
236643cb76d9SGreg Kroah-Hartman static ssize_t delete_child(struct device *dev,
236743cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr,
23681da177e4SLinus Torvalds 			    const char *buf, size_t count)
23691da177e4SLinus Torvalds {
23701da177e4SLinus Torvalds 	int pkey;
23711da177e4SLinus Torvalds 	int ret;
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
23741da177e4SLinus Torvalds 		return -EINVAL;
23751da177e4SLinus Torvalds 
23761da177e4SLinus Torvalds 	if (pkey < 0 || pkey > 0xffff)
23771da177e4SLinus Torvalds 		return -EINVAL;
23781da177e4SLinus Torvalds 
237943cb76d9SGreg Kroah-Hartman 	ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	return ret ? ret : count;
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds }
23847a52b34bSOr Gerlitz static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds int ipoib_add_pkey_attr(struct net_device *dev)
23871da177e4SLinus Torvalds {
238843cb76d9SGreg Kroah-Hartman 	return device_create_file(&dev->dev, &dev_attr_pkey);
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds 
2391f6350da4SArseny Maslennikov /*
2392f6350da4SArseny Maslennikov  * We erroneously exposed the iface's port number in the dev_id
2393f6350da4SArseny Maslennikov  * sysfs field long after dev_port was introduced for that purpose[1],
2394f6350da4SArseny Maslennikov  * and we need to stop everyone from relying on that.
2395f6350da4SArseny Maslennikov  * Let's overload the shower routine for the dev_id file here
2396f6350da4SArseny Maslennikov  * to gently bring the issue up.
2397f6350da4SArseny Maslennikov  *
2398f6350da4SArseny Maslennikov  * [1] https://www.spinics.net/lists/netdev/msg272123.html
2399f6350da4SArseny Maslennikov  */
2400f6350da4SArseny Maslennikov static ssize_t dev_id_show(struct device *dev,
2401f6350da4SArseny Maslennikov 			   struct device_attribute *attr, char *buf)
2402f6350da4SArseny Maslennikov {
2403f6350da4SArseny Maslennikov 	struct net_device *ndev = to_net_dev(dev);
2404f6350da4SArseny Maslennikov 
2405f6350da4SArseny Maslennikov 	if (ndev->dev_id == ndev->dev_port)
2406f6350da4SArseny Maslennikov 		netdev_info_once(ndev,
2407f6350da4SArseny Maslennikov 			"\"%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",
2408f6350da4SArseny Maslennikov 			current->comm);
2409f6350da4SArseny Maslennikov 
2410f6350da4SArseny Maslennikov 	return sprintf(buf, "%#x\n", ndev->dev_id);
2411f6350da4SArseny Maslennikov }
2412f6350da4SArseny Maslennikov static DEVICE_ATTR_RO(dev_id);
2413f6350da4SArseny Maslennikov 
2414f6350da4SArseny Maslennikov int ipoib_intercept_dev_id_attr(struct net_device *dev)
2415f6350da4SArseny Maslennikov {
2416f6350da4SArseny Maslennikov 	device_remove_file(&dev->dev, &dev_attr_dev_id);
2417f6350da4SArseny Maslennikov 	return device_create_file(&dev->dev, &dev_attr_dev_id);
2418f6350da4SArseny Maslennikov }
2419f6350da4SArseny Maslennikov 
24201da177e4SLinus Torvalds static struct net_device *ipoib_add_port(const char *format,
24211da177e4SLinus Torvalds 					 struct ib_device *hca, u8 port)
24221da177e4SLinus Torvalds {
24235d6b0cb3SDenis Drozdov 	struct rtnl_link_ops *ops = ipoib_get_link_ops();
24245d6b0cb3SDenis Drozdov 	struct rdma_netdev_alloc_params params;
24251da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
24269f49a5b5SJason Gunthorpe 	struct net_device *ndev;
2427eaeb3984SJason Gunthorpe 	int result;
24281da177e4SLinus Torvalds 
24295d6b0cb3SDenis Drozdov 	ndev = ipoib_intf_alloc(hca, port, format);
24305d6b0cb3SDenis Drozdov 	if (IS_ERR(ndev)) {
24315d6b0cb3SDenis Drozdov 		pr_warn("%s, %d: ipoib_intf_alloc failed %ld\n", hca->name, port,
24325d6b0cb3SDenis Drozdov 			PTR_ERR(ndev));
24335d6b0cb3SDenis Drozdov 		return ndev;
24341da177e4SLinus Torvalds 	}
24355d6b0cb3SDenis Drozdov 	priv = ipoib_priv(ndev);
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
24381da177e4SLinus Torvalds 			      priv->ca, ipoib_event);
2439dcc9881eSLeon Romanovsky 	ib_register_event_handler(&priv->event_handler);
24401da177e4SLinus Torvalds 
244110293610SAlex Estrin 	/* call event handler to ensure pkey in sync */
244210293610SAlex Estrin 	queue_work(ipoib_workqueue, &priv->flush_heavy);
244310293610SAlex Estrin 
24449f49a5b5SJason Gunthorpe 	result = register_netdev(ndev);
24451da177e4SLinus Torvalds 	if (result) {
2446c55359a2SYuval Shaia 		pr_warn("%s: couldn't register ipoib port %d; error %d\n",
24471da177e4SLinus Torvalds 			hca->name, port, result);
24481da177e4SLinus Torvalds 
24499f49a5b5SJason Gunthorpe 		ipoib_parent_unregister_pre(ndev);
24509f49a5b5SJason Gunthorpe 		ipoib_intf_free(ndev);
24519f49a5b5SJason Gunthorpe 		free_netdev(ndev);
24521da177e4SLinus Torvalds 
24531da177e4SLinus Torvalds 		return ERR_PTR(result);
24541da177e4SLinus Torvalds 	}
24551da177e4SLinus Torvalds 
24565d6b0cb3SDenis Drozdov 	if (hca->rdma_netdev_get_params) {
24575d6b0cb3SDenis Drozdov 		int rc = hca->rdma_netdev_get_params(hca, port,
24585d6b0cb3SDenis Drozdov 						     RDMA_NETDEV_IPOIB,
24595d6b0cb3SDenis Drozdov 						     &params);
24605d6b0cb3SDenis Drozdov 
24615d6b0cb3SDenis Drozdov 		if (!rc && ops->priv_size < params.sizeof_priv)
24625d6b0cb3SDenis Drozdov 			ops->priv_size = params.sizeof_priv;
24635d6b0cb3SDenis Drozdov 	}
24649f49a5b5SJason Gunthorpe 	/*
24659f49a5b5SJason Gunthorpe 	 * We cannot set priv_destructor before register_netdev because we
24669f49a5b5SJason Gunthorpe 	 * need priv to be always valid during the error flow to execute
24679f49a5b5SJason Gunthorpe 	 * ipoib_parent_unregister_pre(). Instead handle it manually and only
24689f49a5b5SJason Gunthorpe 	 * enter priv_destructor mode once we are completely registered.
24699f49a5b5SJason Gunthorpe 	 */
24709f49a5b5SJason Gunthorpe 	ndev->priv_destructor = ipoib_intf_free;
24719f49a5b5SJason Gunthorpe 
2472f6350da4SArseny Maslennikov 	if (ipoib_intercept_dev_id_attr(ndev))
2473f6350da4SArseny Maslennikov 		goto sysfs_failed;
24749f49a5b5SJason Gunthorpe 	if (ipoib_cm_add_mode_attr(ndev))
24759f49a5b5SJason Gunthorpe 		goto sysfs_failed;
24769f49a5b5SJason Gunthorpe 	if (ipoib_add_pkey_attr(ndev))
24779f49a5b5SJason Gunthorpe 		goto sysfs_failed;
24789f49a5b5SJason Gunthorpe 	if (ipoib_add_umcast_attr(ndev))
24799f49a5b5SJason Gunthorpe 		goto sysfs_failed;
24809f49a5b5SJason Gunthorpe 	if (device_create_file(&ndev->dev, &dev_attr_create_child))
24819f49a5b5SJason Gunthorpe 		goto sysfs_failed;
24829f49a5b5SJason Gunthorpe 	if (device_create_file(&ndev->dev, &dev_attr_delete_child))
24839f49a5b5SJason Gunthorpe 		goto sysfs_failed;
24849f49a5b5SJason Gunthorpe 
24859f49a5b5SJason Gunthorpe 	return ndev;
24869f49a5b5SJason Gunthorpe 
24879f49a5b5SJason Gunthorpe sysfs_failed:
24889f49a5b5SJason Gunthorpe 	ipoib_parent_unregister_pre(ndev);
24899f49a5b5SJason Gunthorpe 	unregister_netdev(ndev);
24909f49a5b5SJason Gunthorpe 	return ERR_PTR(-ENOMEM);
24919f49a5b5SJason Gunthorpe }
24929f49a5b5SJason Gunthorpe 
24931da177e4SLinus Torvalds static void ipoib_add_one(struct ib_device *device)
24941da177e4SLinus Torvalds {
24951da177e4SLinus Torvalds 	struct list_head *dev_list;
24961da177e4SLinus Torvalds 	struct net_device *dev;
24971da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
24984139032bSHal Rosenstock 	int p;
24998e37ab68SMichael Wang 	int count = 0;
250007ebafbaSTom Tucker 
2501b1b63970SKamal Heib 	dev_list = kmalloc(sizeof(*dev_list), GFP_KERNEL);
25021da177e4SLinus Torvalds 	if (!dev_list)
25031da177e4SLinus Torvalds 		return;
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds 	INIT_LIST_HEAD(dev_list);
25061da177e4SLinus Torvalds 
25074139032bSHal Rosenstock 	for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
25088e37ab68SMichael Wang 		if (!rdma_protocol_ib(device, p))
25097b4c8769SEli Cohen 			continue;
25101da177e4SLinus Torvalds 		dev = ipoib_add_port("ib%d", device, p);
25111da177e4SLinus Torvalds 		if (!IS_ERR(dev)) {
2512c1048affSErez Shitrit 			priv = ipoib_priv(dev);
25131da177e4SLinus Torvalds 			list_add_tail(&priv->list, dev_list);
25148e37ab68SMichael Wang 			count++;
25151da177e4SLinus Torvalds 		}
25161da177e4SLinus Torvalds 	}
25171da177e4SLinus Torvalds 
25188e37ab68SMichael Wang 	if (!count) {
2519ac6dbf7fSYuval Shaia 		kfree(dev_list);
25208e37ab68SMichael Wang 		return;
25218e37ab68SMichael Wang 	}
25228e37ab68SMichael Wang 
25231da177e4SLinus Torvalds 	ib_set_client_data(device, &ipoib_client, dev_list);
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
25267c1eb45aSHaggai Eran static void ipoib_remove_one(struct ib_device *device, void *client_data)
25271da177e4SLinus Torvalds {
252825405d98SJason Gunthorpe 	struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv;
25297c1eb45aSHaggai Eran 	struct list_head *dev_list = client_data;
25301da177e4SLinus Torvalds 
25315a2815f0SItai Garbi 	if (!dev_list)
25325a2815f0SItai Garbi 		return;
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
253525405d98SJason Gunthorpe 		LIST_HEAD(head);
25367cbee87cSJason Gunthorpe 		ipoib_parent_unregister_pre(priv->dev);
25371da177e4SLinus Torvalds 
253825405d98SJason Gunthorpe 		rtnl_lock();
253925405d98SJason Gunthorpe 
254025405d98SJason Gunthorpe 		list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs,
254125405d98SJason Gunthorpe 					 list)
254225405d98SJason Gunthorpe 			unregister_netdevice_queue(cpriv->dev, &head);
254325405d98SJason Gunthorpe 		unregister_netdevice_queue(priv->dev, &head);
254425405d98SJason Gunthorpe 		unregister_netdevice_many(&head);
254525405d98SJason Gunthorpe 
254625405d98SJason Gunthorpe 		rtnl_unlock();
25471da177e4SLinus Torvalds 	}
254806c56e44SMichael S. Tsirkin 
254906c56e44SMichael S. Tsirkin 	kfree(dev_list);
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
2552771a5258SShamir Rabinovitch #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2553771a5258SShamir Rabinovitch static struct notifier_block ipoib_netdev_notifier = {
2554771a5258SShamir Rabinovitch 	.notifier_call = ipoib_netdev_event,
2555771a5258SShamir Rabinovitch };
2556771a5258SShamir Rabinovitch #endif
2557771a5258SShamir Rabinovitch 
25581da177e4SLinus Torvalds static int __init ipoib_init_module(void)
25591da177e4SLinus Torvalds {
25601da177e4SLinus Torvalds 	int ret;
25611da177e4SLinus Torvalds 
25620f485251SShirley Ma 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
25630f485251SShirley Ma 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
25640f485251SShirley Ma 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
25650f485251SShirley Ma 
25660f485251SShirley Ma 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
25670f485251SShirley Ma 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
2568732eacc0SHagen Paul Pfeifer 	ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
256968e995a2SPradeep Satyanarayana #ifdef CONFIG_INFINIBAND_IPOIB_CM
257068e995a2SPradeep Satyanarayana 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
257111f74b40SAlex Vesker 	ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0);
257268e995a2SPradeep Satyanarayana #endif
25730f485251SShirley Ma 
2574f89271daSEli Cohen 	/*
2575f89271daSEli Cohen 	 * When copying small received packets, we only copy from the
2576f89271daSEli Cohen 	 * linear data part of the SKB, so we rely on this condition.
2577f89271daSEli Cohen 	 */
2578f89271daSEli Cohen 	BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2579f89271daSEli Cohen 
25801da177e4SLinus Torvalds 	ret = ipoib_register_debugfs();
25811da177e4SLinus Torvalds 	if (ret)
25821da177e4SLinus Torvalds 		return ret;
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 	/*
25850b39578bSDoug Ledford 	 * We create a global workqueue here that is used for all flush
25860b39578bSDoug Ledford 	 * operations.  However, if you attempt to flush a workqueue
25870b39578bSDoug Ledford 	 * from a task on that same workqueue, it deadlocks the system.
25880b39578bSDoug Ledford 	 * We want to be able to flush the tasks associated with a
25890b39578bSDoug Ledford 	 * specific net device, so we also create a workqueue for each
25900b39578bSDoug Ledford 	 * netdevice.  We queue up the tasks for that device only on
25910b39578bSDoug Ledford 	 * its private workqueue, and we only queue up flush events
25920b39578bSDoug Ledford 	 * on our global flush workqueue.  This avoids the deadlocks.
25931da177e4SLinus Torvalds 	 */
2594ee190ab7SJason Gunthorpe 	ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush", 0);
25951da177e4SLinus Torvalds 	if (!ipoib_workqueue) {
25961da177e4SLinus Torvalds 		ret = -ENOMEM;
25971da177e4SLinus Torvalds 		goto err_fs;
25981da177e4SLinus Torvalds 	}
25991da177e4SLinus Torvalds 
2600c1a0b23bSMichael S. Tsirkin 	ib_sa_register_client(&ipoib_sa_client);
2601c1a0b23bSMichael S. Tsirkin 
26021da177e4SLinus Torvalds 	ret = ib_register_client(&ipoib_client);
26031da177e4SLinus Torvalds 	if (ret)
2604c1a0b23bSMichael S. Tsirkin 		goto err_sa;
26051da177e4SLinus Torvalds 
26069baa0b03SOr Gerlitz 	ret = ipoib_netlink_init();
26079baa0b03SOr Gerlitz 	if (ret)
26089baa0b03SOr Gerlitz 		goto err_client;
26099baa0b03SOr Gerlitz 
2610771a5258SShamir Rabinovitch #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2611771a5258SShamir Rabinovitch 	register_netdevice_notifier(&ipoib_netdev_notifier);
2612771a5258SShamir Rabinovitch #endif
26131da177e4SLinus Torvalds 	return 0;
26141da177e4SLinus Torvalds 
26159baa0b03SOr Gerlitz err_client:
26169baa0b03SOr Gerlitz 	ib_unregister_client(&ipoib_client);
26179baa0b03SOr Gerlitz 
2618c1a0b23bSMichael S. Tsirkin err_sa:
2619c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&ipoib_sa_client);
26201da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
26211da177e4SLinus Torvalds 
26229adec1a8SRoland Dreier err_fs:
26239adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
26249adec1a8SRoland Dreier 
26251da177e4SLinus Torvalds 	return ret;
26261da177e4SLinus Torvalds }
26271da177e4SLinus Torvalds 
26281da177e4SLinus Torvalds static void __exit ipoib_cleanup_module(void)
26291da177e4SLinus Torvalds {
2630771a5258SShamir Rabinovitch #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2631771a5258SShamir Rabinovitch 	unregister_netdevice_notifier(&ipoib_netdev_notifier);
2632771a5258SShamir Rabinovitch #endif
26339baa0b03SOr Gerlitz 	ipoib_netlink_fini();
26341da177e4SLinus Torvalds 	ib_unregister_client(&ipoib_client);
2635c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&ipoib_sa_client);
26369adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
26371da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
26381da177e4SLinus Torvalds }
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds module_init(ipoib_init_module);
26411da177e4SLinus Torvalds module_exit(ipoib_cleanup_module);
2642