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 
551da177e4SLinus Torvalds MODULE_AUTHOR("Roland Dreier");
561da177e4SLinus Torvalds MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
571da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL");
581da177e4SLinus Torvalds 
590f485251SShirley Ma int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
600f485251SShirley Ma int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
610f485251SShirley Ma 
620f485251SShirley Ma module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
630f485251SShirley Ma MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
640f485251SShirley Ma module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
650f485251SShirley Ma MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
660f485251SShirley Ma 
671da177e4SLinus Torvalds #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
681da177e4SLinus Torvalds int ipoib_debug_level;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds module_param_named(debug_level, ipoib_debug_level, int, 0644);
711da177e4SLinus Torvalds MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
721da177e4SLinus Torvalds #endif
731da177e4SLinus Torvalds 
741732b0efSRoland Dreier struct ipoib_path_iter {
751732b0efSRoland Dreier 	struct net_device *dev;
761732b0efSRoland Dreier 	struct ipoib_path  path;
771732b0efSRoland Dreier };
781732b0efSRoland Dreier 
791da177e4SLinus Torvalds static const u8 ipv4_bcast_addr[] = {
801da177e4SLinus Torvalds 	0x00, 0xff, 0xff, 0xff,
811da177e4SLinus Torvalds 	0xff, 0x12, 0x40, 0x1b,	0x00, 0x00, 0x00, 0x00,
821da177e4SLinus Torvalds 	0x00, 0x00, 0x00, 0x00,	0xff, 0xff, 0xff, 0xff
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds struct workqueue_struct *ipoib_workqueue;
861da177e4SLinus Torvalds 
87c1a0b23bSMichael S. Tsirkin struct ib_sa_client ipoib_sa_client;
88c1a0b23bSMichael S. Tsirkin 
8911a0ae4cSJason Gunthorpe static int ipoib_add_one(struct ib_device *device);
907c1eb45aSHaggai Eran static void ipoib_remove_one(struct ib_device *device, void *client_data);
91b63b70d8SShlomo Pongratz static void ipoib_neigh_reclaim(struct rcu_head *rp);
92ddde896eSGuy Shapiro static struct net_device *ipoib_get_net_dev_by_params(
931fb7f897SMark Bloch 		struct ib_device *dev, u32 port, u16 pkey,
94ddde896eSGuy Shapiro 		const union ib_gid *gid, const struct sockaddr *addr,
95ddde896eSGuy Shapiro 		void *client_data);
96492a7e67SMark Bloch static int ipoib_set_mac(struct net_device *dev, void *addr);
9731a82362SFeras Daoud static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
9831a82362SFeras Daoud 		       int cmd);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static struct ib_client ipoib_client = {
1011da177e4SLinus Torvalds 	.name   = "ipoib",
1021da177e4SLinus Torvalds 	.add    = ipoib_add_one,
103ddde896eSGuy Shapiro 	.remove = ipoib_remove_one,
104ddde896eSGuy Shapiro 	.get_net_dev_by_params = ipoib_get_net_dev_by_params,
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
107771a5258SShamir Rabinovitch #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
ipoib_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)108771a5258SShamir Rabinovitch static int ipoib_netdev_event(struct notifier_block *this,
109771a5258SShamir Rabinovitch 			      unsigned long event, void *ptr)
110771a5258SShamir Rabinovitch {
111771a5258SShamir Rabinovitch 	struct netdev_notifier_info *ni = ptr;
112771a5258SShamir Rabinovitch 	struct net_device *dev = ni->dev;
113771a5258SShamir Rabinovitch 
114771a5258SShamir Rabinovitch 	if (dev->netdev_ops->ndo_open != ipoib_open)
115771a5258SShamir Rabinovitch 		return NOTIFY_DONE;
116771a5258SShamir Rabinovitch 
117771a5258SShamir Rabinovitch 	switch (event) {
118771a5258SShamir Rabinovitch 	case NETDEV_REGISTER:
119771a5258SShamir Rabinovitch 		ipoib_create_debug_files(dev);
120771a5258SShamir Rabinovitch 		break;
121771a5258SShamir Rabinovitch 	case NETDEV_CHANGENAME:
122771a5258SShamir Rabinovitch 		ipoib_delete_debug_files(dev);
123771a5258SShamir Rabinovitch 		ipoib_create_debug_files(dev);
124771a5258SShamir Rabinovitch 		break;
125771a5258SShamir Rabinovitch 	case NETDEV_UNREGISTER:
126771a5258SShamir Rabinovitch 		ipoib_delete_debug_files(dev);
127771a5258SShamir Rabinovitch 		break;
128771a5258SShamir Rabinovitch 	}
129771a5258SShamir Rabinovitch 
130771a5258SShamir Rabinovitch 	return NOTIFY_DONE;
131771a5258SShamir Rabinovitch }
132771a5258SShamir Rabinovitch #endif
133771a5258SShamir Rabinovitch 
ipoib_open(struct net_device * dev)1341da177e4SLinus Torvalds int ipoib_open(struct net_device *dev)
1351da177e4SLinus Torvalds {
136c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	ipoib_dbg(priv, "bringing up interface\n");
1391da177e4SLinus Torvalds 
140437708c4SMichal Schmidt 	netif_carrier_off(dev);
141437708c4SMichal Schmidt 
142e028cc55SYossi Etigin 	set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
1431da177e4SLinus Torvalds 
144efc82eeeSDoug Ledford 	if (ipoib_ib_dev_open(dev)) {
145db84f880SErez Shitrit 		if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1461da177e4SLinus Torvalds 			return 0;
147b8a1b1ceSRoland Dreier 		goto err_disable;
148dd57c930SAlex Estrin 	}
149fe25c561SYossi Etigin 
1505c37077fSZhu Yanjun 	ipoib_ib_dev_up(dev);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1531da177e4SLinus Torvalds 		struct ipoib_dev_priv *cpriv;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 		/* Bring up any child interfaces too */
156f47944ccSErez Shitrit 		down_read(&priv->vlan_rwsem);
1571da177e4SLinus Torvalds 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
1581da177e4SLinus Torvalds 			int flags;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 			flags = cpriv->dev->flags;
1611da177e4SLinus Torvalds 			if (flags & IFF_UP)
1621da177e4SLinus Torvalds 				continue;
1631da177e4SLinus Torvalds 
164567c5e13SPetr Machata 			dev_change_flags(cpriv->dev, flags | IFF_UP, NULL);
1651da177e4SLinus Torvalds 		}
166f47944ccSErez Shitrit 		up_read(&priv->vlan_rwsem);
1673ccbd933SJack Wang 	} else if (priv->parent) {
1683ccbd933SJack Wang 		struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
1691da177e4SLinus Torvalds 
1703ccbd933SJack Wang 		if (!test_bit(IPOIB_FLAG_ADMIN_UP, &ppriv->flags))
1713ccbd933SJack Wang 			ipoib_dbg(priv, "parent device %s is not up, so child device may be not functioning.\n",
1723ccbd933SJack Wang 				  ppriv->dev->name);
1733ccbd933SJack Wang 	}
1741da177e4SLinus Torvalds 	netif_start_queue(dev);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	return 0;
177b8a1b1ceSRoland Dreier 
178b8a1b1ceSRoland Dreier err_disable:
179b8a1b1ceSRoland Dreier 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
180b8a1b1ceSRoland Dreier 
181b8a1b1ceSRoland Dreier 	return -EINVAL;
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
ipoib_stop(struct net_device * dev)1841da177e4SLinus Torvalds static int ipoib_stop(struct net_device *dev)
1851da177e4SLinus Torvalds {
186c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	ipoib_dbg(priv, "stopping interface\n");
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	netif_stop_queue(dev);
1931da177e4SLinus Torvalds 
194efc82eeeSDoug Ledford 	ipoib_ib_dev_down(dev);
195cd565b4bSErez Shitrit 	ipoib_ib_dev_stop(dev);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1981da177e4SLinus Torvalds 		struct ipoib_dev_priv *cpriv;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 		/* Bring down any child interfaces too */
201f47944ccSErez Shitrit 		down_read(&priv->vlan_rwsem);
2021da177e4SLinus Torvalds 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
2031da177e4SLinus Torvalds 			int flags;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 			flags = cpriv->dev->flags;
2061da177e4SLinus Torvalds 			if (!(flags & IFF_UP))
2071da177e4SLinus Torvalds 				continue;
2081da177e4SLinus Torvalds 
209567c5e13SPetr Machata 			dev_change_flags(cpriv->dev, flags & ~IFF_UP, NULL);
2101da177e4SLinus Torvalds 		}
211f47944ccSErez Shitrit 		up_read(&priv->vlan_rwsem);
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	return 0;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
ipoib_fix_features(struct net_device * dev,netdev_features_t features)2179ca36f7dSDavid S. Miller static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
2183d96c74dSMichał Mirosław {
219c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
2203d96c74dSMichał Mirosław 
2213d96c74dSMichał Mirosław 	if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
222c4268778SYuval Shaia 		features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
2233d96c74dSMichał Mirosław 
2243d96c74dSMichał Mirosław 	return features;
2253d96c74dSMichał Mirosław }
2263d96c74dSMichał Mirosław 
ipoib_change_mtu(struct net_device * dev,int new_mtu)2271da177e4SLinus Torvalds static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
2281da177e4SLinus Torvalds {
229c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
230ed7b521dSErez Shitrit 	int ret = 0;
2311da177e4SLinus Torvalds 
232839fcabaSMichael S. Tsirkin 	/* dev->mtu > 2K ==> connected mode */
233586a6934SPradeep Satyanarayana 	if (ipoib_cm_admin_enabled(dev)) {
234586a6934SPradeep Satyanarayana 		if (new_mtu > ipoib_cm_max_mtu(dev))
235586a6934SPradeep Satyanarayana 			return -EINVAL;
236586a6934SPradeep Satyanarayana 
237839fcabaSMichael S. Tsirkin 		if (new_mtu > priv->mcast_mtu)
238839fcabaSMichael S. Tsirkin 			ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
239839fcabaSMichael S. Tsirkin 				   priv->mcast_mtu);
240586a6934SPradeep Satyanarayana 
241839fcabaSMichael S. Tsirkin 		dev->mtu = new_mtu;
242839fcabaSMichael S. Tsirkin 		return 0;
243839fcabaSMichael S. Tsirkin 	}
244839fcabaSMichael S. Tsirkin 
245142a9c28SMuhammad Sammar 	if (new_mtu < (ETH_MIN_MTU + IPOIB_ENCAP_LEN) ||
246142a9c28SMuhammad Sammar 	    new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
2471da177e4SLinus Torvalds 		return -EINVAL;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	priv->admin_mtu = new_mtu;
2501da177e4SLinus Torvalds 
25129da686dSFeras Daoud 	if (priv->mcast_mtu < priv->admin_mtu)
25229da686dSFeras Daoud 		ipoib_dbg(priv, "MTU must be smaller than the underlying "
25329da686dSFeras Daoud 				"link layer MTU - 4 (%u)\n", priv->mcast_mtu);
25429da686dSFeras Daoud 
255ed7b521dSErez Shitrit 	new_mtu = min(priv->mcast_mtu, priv->admin_mtu);
2561da177e4SLinus Torvalds 
257ed7b521dSErez Shitrit 	if (priv->rn_ops->ndo_change_mtu) {
258ed7b521dSErez Shitrit 		bool carrier_status = netif_carrier_ok(dev);
259ed7b521dSErez Shitrit 
260ed7b521dSErez Shitrit 		netif_carrier_off(dev);
261ed7b521dSErez Shitrit 
262ed7b521dSErez Shitrit 		/* notify lower level on the real mtu */
263ed7b521dSErez Shitrit 		ret = priv->rn_ops->ndo_change_mtu(dev, new_mtu);
264ed7b521dSErez Shitrit 
265ed7b521dSErez Shitrit 		if (carrier_status)
266ed7b521dSErez Shitrit 			netif_carrier_on(dev);
267ed7b521dSErez Shitrit 	} else {
268ed7b521dSErez Shitrit 		dev->mtu = new_mtu;
269ed7b521dSErez Shitrit 	}
270ed7b521dSErez Shitrit 
271ed7b521dSErez Shitrit 	return ret;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
ipoib_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)274b6c871e5SErez Shitrit static void ipoib_get_stats(struct net_device *dev,
275b6c871e5SErez Shitrit 			    struct rtnl_link_stats64 *stats)
276b6c871e5SErez Shitrit {
277b6c871e5SErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
278b6c871e5SErez Shitrit 
279b6c871e5SErez Shitrit 	if (priv->rn_ops->ndo_get_stats64)
280b6c871e5SErez Shitrit 		priv->rn_ops->ndo_get_stats64(dev, stats);
281b6c871e5SErez Shitrit 	else
282b6c871e5SErez Shitrit 		netdev_stats_to_stats64(stats, &dev->stats);
283b6c871e5SErez Shitrit }
284b6c871e5SErez Shitrit 
285ddde896eSGuy Shapiro /* Called with an RCU read lock taken */
ipoib_is_dev_match_addr_rcu(const struct sockaddr * addr,struct net_device * dev)286ddde896eSGuy Shapiro static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
287ddde896eSGuy Shapiro 					struct net_device *dev)
288ddde896eSGuy Shapiro {
289ddde896eSGuy Shapiro 	struct net *net = dev_net(dev);
290ddde896eSGuy Shapiro 	struct in_device *in_dev;
291ddde896eSGuy Shapiro 	struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
292ddde896eSGuy Shapiro 	struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
293ddde896eSGuy Shapiro 	__be32 ret_addr;
294ddde896eSGuy Shapiro 
295ddde896eSGuy Shapiro 	switch (addr->sa_family) {
296ddde896eSGuy Shapiro 	case AF_INET:
297ddde896eSGuy Shapiro 		in_dev = in_dev_get(dev);
298ddde896eSGuy Shapiro 		if (!in_dev)
299ddde896eSGuy Shapiro 			return false;
300ddde896eSGuy Shapiro 
301ddde896eSGuy Shapiro 		ret_addr = inet_confirm_addr(net, in_dev, 0,
302ddde896eSGuy Shapiro 					     addr_in->sin_addr.s_addr,
303ddde896eSGuy Shapiro 					     RT_SCOPE_HOST);
304ddde896eSGuy Shapiro 		in_dev_put(in_dev);
305ddde896eSGuy Shapiro 		if (ret_addr)
306ddde896eSGuy Shapiro 			return true;
307ddde896eSGuy Shapiro 
308ddde896eSGuy Shapiro 		break;
309ddde896eSGuy Shapiro 	case AF_INET6:
310ddde896eSGuy Shapiro 		if (IS_ENABLED(CONFIG_IPV6) &&
311ddde896eSGuy Shapiro 		    ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
312ddde896eSGuy Shapiro 			return true;
313ddde896eSGuy Shapiro 
314ddde896eSGuy Shapiro 		break;
315ddde896eSGuy Shapiro 	}
316ddde896eSGuy Shapiro 	return false;
317ddde896eSGuy Shapiro }
318ddde896eSGuy Shapiro 
319bf194997SLeon Romanovsky /*
320ddde896eSGuy Shapiro  * Find the master net_device on top of the given net_device.
321ddde896eSGuy Shapiro  * @dev: base IPoIB net_device
322ddde896eSGuy Shapiro  *
323ddde896eSGuy Shapiro  * Returns the master net_device with a reference held, or the same net_device
324ddde896eSGuy Shapiro  * if no master exists.
325ddde896eSGuy Shapiro  */
ipoib_get_master_net_dev(struct net_device * dev)326ddde896eSGuy Shapiro static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
327ddde896eSGuy Shapiro {
328ddde896eSGuy Shapiro 	struct net_device *master;
329ddde896eSGuy Shapiro 
330ddde896eSGuy Shapiro 	rcu_read_lock();
331ddde896eSGuy Shapiro 	master = netdev_master_upper_dev_get_rcu(dev);
332ddde896eSGuy Shapiro 	if (master)
333ddde896eSGuy Shapiro 		dev_hold(master);
334ddde896eSGuy Shapiro 	rcu_read_unlock();
335ddde896eSGuy Shapiro 
336ddde896eSGuy Shapiro 	if (master)
337ddde896eSGuy Shapiro 		return master;
338ddde896eSGuy Shapiro 
339ddde896eSGuy Shapiro 	dev_hold(dev);
340ddde896eSGuy Shapiro 	return dev;
341ddde896eSGuy Shapiro }
342ddde896eSGuy Shapiro 
343e0e79c8eSDavid Ahern struct ipoib_walk_data {
344e0e79c8eSDavid Ahern 	const struct sockaddr *addr;
345e0e79c8eSDavid Ahern 	struct net_device *result;
346e0e79c8eSDavid Ahern };
347e0e79c8eSDavid Ahern 
ipoib_upper_walk(struct net_device * upper,struct netdev_nested_priv * priv)348eff74233STaehee Yoo static int ipoib_upper_walk(struct net_device *upper,
349eff74233STaehee Yoo 			    struct netdev_nested_priv *priv)
350e0e79c8eSDavid Ahern {
351eff74233STaehee Yoo 	struct ipoib_walk_data *data = (struct ipoib_walk_data *)priv->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 /**
364bf194997SLeon Romanovsky  * ipoib_get_net_dev_match_addr - Find a net_device matching
365bf194997SLeon Romanovsky  * the given address, which is an upper device of the given net_device.
366bf194997SLeon Romanovsky  *
367ddde896eSGuy Shapiro  * @addr: IP address to look for.
368ddde896eSGuy Shapiro  * @dev: base IPoIB net_device
369ddde896eSGuy Shapiro  *
370ddde896eSGuy Shapiro  * If found, returns the net_device with a reference held. Otherwise return
371ddde896eSGuy Shapiro  * NULL.
372ddde896eSGuy Shapiro  */
ipoib_get_net_dev_match_addr(const struct sockaddr * addr,struct net_device * dev)373ddde896eSGuy Shapiro static struct net_device *ipoib_get_net_dev_match_addr(
374ddde896eSGuy Shapiro 		const struct sockaddr *addr, struct net_device *dev)
375ddde896eSGuy Shapiro {
376eff74233STaehee Yoo 	struct netdev_nested_priv priv;
377e0e79c8eSDavid Ahern 	struct ipoib_walk_data data = {
378e0e79c8eSDavid Ahern 		.addr = addr,
379e0e79c8eSDavid Ahern 	};
380ddde896eSGuy Shapiro 
381eff74233STaehee Yoo 	priv.data = (void *)&data;
382ddde896eSGuy Shapiro 	rcu_read_lock();
383ddde896eSGuy Shapiro 	if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
384ddde896eSGuy Shapiro 		dev_hold(dev);
385e0e79c8eSDavid Ahern 		data.result = dev;
386ddde896eSGuy Shapiro 		goto out;
387ddde896eSGuy Shapiro 	}
388ddde896eSGuy Shapiro 
389eff74233STaehee Yoo 	netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &priv);
390ddde896eSGuy Shapiro out:
391ddde896eSGuy Shapiro 	rcu_read_unlock();
392e0e79c8eSDavid Ahern 	return data.result;
393ddde896eSGuy Shapiro }
394ddde896eSGuy Shapiro 
395ddde896eSGuy Shapiro /* returns the number of IPoIB netdevs on top a given ipoib device matching a
396ddde896eSGuy Shapiro  * pkey_index and address, if one exists.
397ddde896eSGuy Shapiro  *
398ddde896eSGuy Shapiro  * @found_net_dev: contains a matching net_device if the return value >= 1,
399ddde896eSGuy Shapiro  * with a reference held. */
ipoib_match_gid_pkey_addr(struct ipoib_dev_priv * priv,const union ib_gid * gid,u16 pkey_index,const struct sockaddr * addr,int nesting,struct net_device ** found_net_dev)400ddde896eSGuy Shapiro static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
401ddde896eSGuy Shapiro 				     const union ib_gid *gid,
402ddde896eSGuy Shapiro 				     u16 pkey_index,
403ddde896eSGuy Shapiro 				     const struct sockaddr *addr,
404ddde896eSGuy Shapiro 				     int nesting,
405ddde896eSGuy Shapiro 				     struct net_device **found_net_dev)
406ddde896eSGuy Shapiro {
407ddde896eSGuy Shapiro 	struct ipoib_dev_priv *child_priv;
408ddde896eSGuy Shapiro 	struct net_device *net_dev = NULL;
409ddde896eSGuy Shapiro 	int matches = 0;
410ddde896eSGuy Shapiro 
411ddde896eSGuy Shapiro 	if (priv->pkey_index == pkey_index &&
412ddde896eSGuy Shapiro 	    (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
413ddde896eSGuy Shapiro 		if (!addr) {
414ddde896eSGuy Shapiro 			net_dev = ipoib_get_master_net_dev(priv->dev);
415ddde896eSGuy Shapiro 		} else {
416ddde896eSGuy Shapiro 			/* Verify the net_device matches the IP address, as
417ddde896eSGuy Shapiro 			 * IPoIB child devices currently share a GID. */
418ddde896eSGuy Shapiro 			net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
419ddde896eSGuy Shapiro 		}
420ddde896eSGuy Shapiro 		if (net_dev) {
421ddde896eSGuy Shapiro 			if (!*found_net_dev)
422ddde896eSGuy Shapiro 				*found_net_dev = net_dev;
423ddde896eSGuy Shapiro 			else
424ddde896eSGuy Shapiro 				dev_put(net_dev);
425ddde896eSGuy Shapiro 			++matches;
426ddde896eSGuy Shapiro 		}
427ddde896eSGuy Shapiro 	}
428ddde896eSGuy Shapiro 
429ddde896eSGuy Shapiro 	/* Check child interfaces */
430ddde896eSGuy Shapiro 	down_read_nested(&priv->vlan_rwsem, nesting);
431ddde896eSGuy Shapiro 	list_for_each_entry(child_priv, &priv->child_intfs, list) {
432ddde896eSGuy Shapiro 		matches += ipoib_match_gid_pkey_addr(child_priv, gid,
433ddde896eSGuy Shapiro 						    pkey_index, addr,
434ddde896eSGuy Shapiro 						    nesting + 1,
435ddde896eSGuy Shapiro 						    found_net_dev);
436ddde896eSGuy Shapiro 		if (matches > 1)
437ddde896eSGuy Shapiro 			break;
438ddde896eSGuy Shapiro 	}
439ddde896eSGuy Shapiro 	up_read(&priv->vlan_rwsem);
440ddde896eSGuy Shapiro 
441ddde896eSGuy Shapiro 	return matches;
442ddde896eSGuy Shapiro }
443ddde896eSGuy Shapiro 
444ddde896eSGuy Shapiro /* Returns the number of matching net_devs found (between 0 and 2). Also
445ddde896eSGuy Shapiro  * return the matching net_device in the @net_dev parameter, holding a
446ddde896eSGuy Shapiro  * reference to the net_device, if the number of matches >= 1 */
__ipoib_get_net_dev_by_params(struct list_head * dev_list,u32 port,u16 pkey_index,const union ib_gid * gid,const struct sockaddr * addr,struct net_device ** net_dev)4471fb7f897SMark Bloch static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u32 port,
448ddde896eSGuy Shapiro 					 u16 pkey_index,
449ddde896eSGuy Shapiro 					 const union ib_gid *gid,
450ddde896eSGuy Shapiro 					 const struct sockaddr *addr,
451ddde896eSGuy Shapiro 					 struct net_device **net_dev)
452ddde896eSGuy Shapiro {
453ddde896eSGuy Shapiro 	struct ipoib_dev_priv *priv;
454ddde896eSGuy Shapiro 	int matches = 0;
455ddde896eSGuy Shapiro 
456ddde896eSGuy Shapiro 	*net_dev = NULL;
457ddde896eSGuy Shapiro 
458ddde896eSGuy Shapiro 	list_for_each_entry(priv, dev_list, list) {
459ddde896eSGuy Shapiro 		if (priv->port != port)
460ddde896eSGuy Shapiro 			continue;
461ddde896eSGuy Shapiro 
462ddde896eSGuy Shapiro 		matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
463ddde896eSGuy Shapiro 						     addr, 0, net_dev);
464ddde896eSGuy Shapiro 		if (matches > 1)
465ddde896eSGuy Shapiro 			break;
466ddde896eSGuy Shapiro 	}
467ddde896eSGuy Shapiro 
468ddde896eSGuy Shapiro 	return matches;
469ddde896eSGuy Shapiro }
470ddde896eSGuy Shapiro 
ipoib_get_net_dev_by_params(struct ib_device * dev,u32 port,u16 pkey,const union ib_gid * gid,const struct sockaddr * addr,void * client_data)471ddde896eSGuy Shapiro static struct net_device *ipoib_get_net_dev_by_params(
4721fb7f897SMark Bloch 		struct ib_device *dev, u32 port, u16 pkey,
473ddde896eSGuy Shapiro 		const union ib_gid *gid, const struct sockaddr *addr,
474ddde896eSGuy Shapiro 		void *client_data)
475ddde896eSGuy Shapiro {
476ddde896eSGuy Shapiro 	struct net_device *net_dev;
477ddde896eSGuy Shapiro 	struct list_head *dev_list = client_data;
478ddde896eSGuy Shapiro 	u16 pkey_index;
479ddde896eSGuy Shapiro 	int matches;
480ddde896eSGuy Shapiro 	int ret;
481ddde896eSGuy Shapiro 
482ddde896eSGuy Shapiro 	if (!rdma_protocol_ib(dev, port))
483ddde896eSGuy Shapiro 		return NULL;
484ddde896eSGuy Shapiro 
485ddde896eSGuy Shapiro 	ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
486ddde896eSGuy Shapiro 	if (ret)
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");
512df561f66SGustavo A. R. Silva 		fallthrough;
513ddde896eSGuy Shapiro 	case 1:
514ddde896eSGuy Shapiro 		return net_dev;
515ddde896eSGuy Shapiro 	}
516ddde896eSGuy Shapiro }
517ddde896eSGuy Shapiro 
ipoib_set_mode(struct net_device * dev,const char * buf)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));
5368f149b68SGary Leshner 		netif_set_real_num_tx_queues(dev, 1);
53771d9c5f9SRoland Dreier 		rtnl_unlock();
538e622f2f4SChristoph Hellwig 		priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
53971d9c5f9SRoland Dreier 
54071d9c5f9SRoland Dreier 		ipoib_flush_paths(dev);
5410a0007f2SFeras Daoud 		return (!rtnl_trylock()) ? -EBUSY : 0;
54271d9c5f9SRoland Dreier 	}
54371d9c5f9SRoland Dreier 
54471d9c5f9SRoland Dreier 	if (!strcmp(buf, "datagram\n")) {
54571d9c5f9SRoland Dreier 		clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
54671d9c5f9SRoland Dreier 		netdev_update_features(dev);
54771d9c5f9SRoland Dreier 		dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
5488f149b68SGary Leshner 		netif_set_real_num_tx_queues(dev, dev->num_tx_queues);
54971d9c5f9SRoland Dreier 		rtnl_unlock();
55071d9c5f9SRoland Dreier 		ipoib_flush_paths(dev);
5510a0007f2SFeras Daoud 		return (!rtnl_trylock()) ? -EBUSY : 0;
55271d9c5f9SRoland Dreier 	}
55371d9c5f9SRoland Dreier 
55471d9c5f9SRoland Dreier 	return -EINVAL;
55571d9c5f9SRoland Dreier }
55671d9c5f9SRoland Dreier 
__path_find(struct net_device * dev,void * gid)557546481c2SErez Shitrit struct ipoib_path *__path_find(struct net_device *dev, void *gid)
5581da177e4SLinus Torvalds {
559c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
5601da177e4SLinus Torvalds 	struct rb_node *n = priv->path_tree.rb_node;
5611da177e4SLinus Torvalds 	struct ipoib_path *path;
5621da177e4SLinus Torvalds 	int ret;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	while (n) {
5651da177e4SLinus Torvalds 		path = rb_entry(n, struct ipoib_path, rb_node);
5661da177e4SLinus Torvalds 
56737c22a77SJack Morgenstein 		ret = memcmp(gid, path->pathrec.dgid.raw,
5681da177e4SLinus Torvalds 			     sizeof (union ib_gid));
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 		if (ret < 0)
5711da177e4SLinus Torvalds 			n = n->rb_left;
5721da177e4SLinus Torvalds 		else if (ret > 0)
5731da177e4SLinus Torvalds 			n = n->rb_right;
5741da177e4SLinus Torvalds 		else
5751da177e4SLinus Torvalds 			return path;
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	return NULL;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
__path_add(struct net_device * dev,struct ipoib_path * path)5811da177e4SLinus Torvalds static int __path_add(struct net_device *dev, struct ipoib_path *path)
5821da177e4SLinus Torvalds {
583c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
5841da177e4SLinus Torvalds 	struct rb_node **n = &priv->path_tree.rb_node;
5851da177e4SLinus Torvalds 	struct rb_node *pn = NULL;
5861da177e4SLinus Torvalds 	struct ipoib_path *tpath;
5871da177e4SLinus Torvalds 	int ret;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	while (*n) {
5901da177e4SLinus Torvalds 		pn = *n;
5911da177e4SLinus Torvalds 		tpath = rb_entry(pn, struct ipoib_path, rb_node);
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 		ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
5941da177e4SLinus Torvalds 			     sizeof (union ib_gid));
5951da177e4SLinus Torvalds 		if (ret < 0)
5961da177e4SLinus Torvalds 			n = &pn->rb_left;
5971da177e4SLinus Torvalds 		else if (ret > 0)
5981da177e4SLinus Torvalds 			n = &pn->rb_right;
5991da177e4SLinus Torvalds 		else
6001da177e4SLinus Torvalds 			return -EEXIST;
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	rb_link_node(&path->rb_node, pn, n);
6041da177e4SLinus Torvalds 	rb_insert_color(&path->rb_node, &priv->path_tree);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	list_add_tail(&path->list, &priv->path_list);
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	return 0;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
path_free(struct net_device * dev,struct ipoib_path * path)6111da177e4SLinus Torvalds static void path_free(struct net_device *dev, struct ipoib_path *path)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	struct sk_buff *skb;
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&path->queue)))
6161da177e4SLinus Torvalds 		dev_kfree_skb_irq(skb);
6171da177e4SLinus Torvalds 
6180dd9ce18SErez Alfasi 	ipoib_dbg(ipoib_priv(dev), "%s\n", __func__);
6191da177e4SLinus Torvalds 
620b63b70d8SShlomo Pongratz 	/* remove all neigh connected to this path */
621b63b70d8SShlomo Pongratz 	ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	if (path->ah)
6241da177e4SLinus Torvalds 		ipoib_put_ah(path->ah);
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	kfree(path);
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291732b0efSRoland Dreier #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
6301732b0efSRoland Dreier 
ipoib_path_iter_init(struct net_device * dev)6311732b0efSRoland Dreier struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
6321732b0efSRoland Dreier {
6331732b0efSRoland Dreier 	struct ipoib_path_iter *iter;
6341732b0efSRoland Dreier 
635b1b63970SKamal Heib 	iter = kmalloc(sizeof(*iter), GFP_KERNEL);
6361732b0efSRoland Dreier 	if (!iter)
6371732b0efSRoland Dreier 		return NULL;
6381732b0efSRoland Dreier 
6391732b0efSRoland Dreier 	iter->dev = dev;
6401732b0efSRoland Dreier 	memset(iter->path.pathrec.dgid.raw, 0, 16);
6411732b0efSRoland Dreier 
6421732b0efSRoland Dreier 	if (ipoib_path_iter_next(iter)) {
6431732b0efSRoland Dreier 		kfree(iter);
6441732b0efSRoland Dreier 		return NULL;
6451732b0efSRoland Dreier 	}
6461732b0efSRoland Dreier 
6471732b0efSRoland Dreier 	return iter;
6481732b0efSRoland Dreier }
6491732b0efSRoland Dreier 
ipoib_path_iter_next(struct ipoib_path_iter * iter)6501732b0efSRoland Dreier int ipoib_path_iter_next(struct ipoib_path_iter *iter)
6511732b0efSRoland Dreier {
652c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
6531732b0efSRoland Dreier 	struct rb_node *n;
6541732b0efSRoland Dreier 	struct ipoib_path *path;
6551732b0efSRoland Dreier 	int ret = 1;
6561732b0efSRoland Dreier 
6571732b0efSRoland Dreier 	spin_lock_irq(&priv->lock);
6581732b0efSRoland Dreier 
6591732b0efSRoland Dreier 	n = rb_first(&priv->path_tree);
6601732b0efSRoland Dreier 
6611732b0efSRoland Dreier 	while (n) {
6621732b0efSRoland Dreier 		path = rb_entry(n, struct ipoib_path, rb_node);
6631732b0efSRoland Dreier 
6641732b0efSRoland Dreier 		if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
6651732b0efSRoland Dreier 			   sizeof (union ib_gid)) < 0) {
6661732b0efSRoland Dreier 			iter->path = *path;
6671732b0efSRoland Dreier 			ret = 0;
6681732b0efSRoland Dreier 			break;
6691732b0efSRoland Dreier 		}
6701732b0efSRoland Dreier 
6711732b0efSRoland Dreier 		n = rb_next(n);
6721732b0efSRoland Dreier 	}
6731732b0efSRoland Dreier 
6741732b0efSRoland Dreier 	spin_unlock_irq(&priv->lock);
6751732b0efSRoland Dreier 
6761732b0efSRoland Dreier 	return ret;
6771732b0efSRoland Dreier }
6781732b0efSRoland Dreier 
ipoib_path_iter_read(struct ipoib_path_iter * iter,struct ipoib_path * path)6791732b0efSRoland Dreier void ipoib_path_iter_read(struct ipoib_path_iter *iter,
6801732b0efSRoland Dreier 			  struct ipoib_path *path)
6811732b0efSRoland Dreier {
6821732b0efSRoland Dreier 	*path = iter->path;
6831732b0efSRoland Dreier }
6841732b0efSRoland Dreier 
6851732b0efSRoland Dreier #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
6861732b0efSRoland Dreier 
ipoib_mark_paths_invalid(struct net_device * dev)687ee1e2c82SMoni Shoua void ipoib_mark_paths_invalid(struct net_device *dev)
688ee1e2c82SMoni Shoua {
689c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
690ee1e2c82SMoni Shoua 	struct ipoib_path *path, *tp;
691ee1e2c82SMoni Shoua 
692ee1e2c82SMoni Shoua 	spin_lock_irq(&priv->lock);
693ee1e2c82SMoni Shoua 
694ee1e2c82SMoni Shoua 	list_for_each_entry_safe(path, tp, &priv->path_list, list) {
69557520751SDasaratharaman Chandramouli 		ipoib_dbg(priv, "mark path LID 0x%08x GID %pI6 invalid\n",
69657520751SDasaratharaman Chandramouli 			  be32_to_cpu(sa_path_get_dlid(&path->pathrec)),
697fcace2feSHarvey Harrison 			  path->pathrec.dgid.raw);
698fa9391dbSDoug Ledford 		if (path->ah)
699fa9391dbSDoug Ledford 			path->ah->valid = 0;
700ee1e2c82SMoni Shoua 	}
701ee1e2c82SMoni Shoua 
702ee1e2c82SMoni Shoua 	spin_unlock_irq(&priv->lock);
703ee1e2c82SMoni Shoua }
704ee1e2c82SMoni Shoua 
push_pseudo_header(struct sk_buff * skb,const char * daddr)7052b084176SErez Shitrit static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
7062b084176SErez Shitrit {
7072b084176SErez Shitrit 	struct ipoib_pseudo_header *phdr;
7082b084176SErez Shitrit 
709d58ff351SJohannes Berg 	phdr = skb_push(skb, sizeof(*phdr));
7102b084176SErez Shitrit 	memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
7112b084176SErez Shitrit }
7122b084176SErez Shitrit 
ipoib_flush_paths(struct net_device * dev)7131da177e4SLinus Torvalds void ipoib_flush_paths(struct net_device *dev)
7141da177e4SLinus Torvalds {
715c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
7161da177e4SLinus Torvalds 	struct ipoib_path *path, *tp;
7171da177e4SLinus Torvalds 	LIST_HEAD(remove_list);
718943c246eSRoland Dreier 	unsigned long flags;
7191da177e4SLinus Torvalds 
720943c246eSRoland Dreier 	netif_tx_lock_bh(dev);
721943c246eSRoland Dreier 	spin_lock_irqsave(&priv->lock, flags);
7221da177e4SLinus Torvalds 
723157de229SRobert P. J. Day 	list_splice_init(&priv->path_list, &remove_list);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	list_for_each_entry(path, &remove_list, list)
7261da177e4SLinus Torvalds 		rb_erase(&path->rb_node, &priv->path_tree);
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	list_for_each_entry_safe(path, tp, &remove_list, list) {
7291da177e4SLinus Torvalds 		if (path->query)
7301da177e4SLinus Torvalds 			ib_sa_cancel_query(path->query_id, path->query);
731943c246eSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
732943c246eSRoland Dreier 		netif_tx_unlock_bh(dev);
7331da177e4SLinus Torvalds 		wait_for_completion(&path->done);
7341da177e4SLinus Torvalds 		path_free(dev, path);
735943c246eSRoland Dreier 		netif_tx_lock_bh(dev);
736943c246eSRoland Dreier 		spin_lock_irqsave(&priv->lock, flags);
7371da177e4SLinus Torvalds 	}
738943c246eSRoland Dreier 
739943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
740943c246eSRoland Dreier 	netif_tx_unlock_bh(dev);
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
path_rec_completion(int status,struct sa_path_rec * pathrec,unsigned int num_prs,void * path_ptr)7431da177e4SLinus Torvalds static void path_rec_completion(int status,
744c2f8fc4eSDasaratharaman Chandramouli 				struct sa_path_rec *pathrec,
7455a374949SMark Zhang 				unsigned int num_prs, void *path_ptr)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	struct ipoib_path *path = path_ptr;
7481da177e4SLinus Torvalds 	struct net_device *dev = path->dev;
749c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
7501da177e4SLinus Torvalds 	struct ipoib_ah *ah = NULL;
751c9da4badSRoland Dreier 	struct ipoib_ah *old_ah = NULL;
752d04d01b1SMichael S. Tsirkin 	struct ipoib_neigh *neigh, *tn;
7531da177e4SLinus Torvalds 	struct sk_buff_head skqueue;
7541da177e4SLinus Torvalds 	struct sk_buff *skb;
7551da177e4SLinus Torvalds 	unsigned long flags;
7561da177e4SLinus Torvalds 
757843613b0SRoland Dreier 	if (!status)
7585b095d98SHarvey Harrison 		ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
75957520751SDasaratharaman Chandramouli 			  be32_to_cpu(sa_path_get_dlid(pathrec)),
7609fdca4daSDasaratharaman Chandramouli 			  pathrec->dgid.raw);
7611da177e4SLinus Torvalds 	else
7625b095d98SHarvey Harrison 		ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
763fcace2feSHarvey Harrison 			  status, path->pathrec.dgid.raw);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	skb_queue_head_init(&skqueue);
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	if (!status) {
76890898850SDasaratharaman Chandramouli 		struct rdma_ah_attr av;
7691da177e4SLinus Torvalds 
7704ad6a024SParav Pandit 		if (!ib_init_ah_attr_from_path(priv->ca, priv->port,
77139839107SParav Pandit 					       pathrec, &av, NULL)) {
7721da177e4SLinus Torvalds 			ah = ipoib_create_ah(dev, priv->pd, &av);
773aa74f487SParav Pandit 			rdma_destroy_ah_attr(&av);
774aa74f487SParav Pandit 		}
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
7781da177e4SLinus Torvalds 
7793874397cSMike Marciniszyn 	if (!IS_ERR_OR_NULL(ah)) {
78043900089SErez Shitrit 		/*
78143900089SErez Shitrit 		 * pathrec.dgid is used as the database key from the LLADDR,
78243900089SErez Shitrit 		 * it must remain unchanged even if the SA returns a different
78343900089SErez Shitrit 		 * GID to use in the AH.
78443900089SErez Shitrit 		 */
78543900089SErez Shitrit 		if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw,
78643900089SErez Shitrit 			   sizeof(union ib_gid))) {
78743900089SErez Shitrit 			ipoib_dbg(
78843900089SErez Shitrit 				priv,
78943900089SErez Shitrit 				"%s got PathRec for gid %pI6 while asked for %pI6\n",
79043900089SErez Shitrit 				dev->name, pathrec->dgid.raw,
79143900089SErez Shitrit 				path->pathrec.dgid.raw);
79243900089SErez Shitrit 			memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw,
79343900089SErez Shitrit 			       sizeof(union ib_gid));
79443900089SErez Shitrit 		}
79543900089SErez Shitrit 
7961da177e4SLinus Torvalds 		path->pathrec = *pathrec;
7971da177e4SLinus Torvalds 
798c9da4badSRoland Dreier 		old_ah   = path->ah;
799c9da4badSRoland Dreier 		path->ah = ah;
800c9da4badSRoland Dreier 
8011da177e4SLinus Torvalds 		ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
80257520751SDasaratharaman Chandramouli 			  ah, be32_to_cpu(sa_path_get_dlid(pathrec)),
8039fdca4daSDasaratharaman Chandramouli 			  pathrec->sl);
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 		while ((skb = __skb_dequeue(&path->queue)))
8061da177e4SLinus Torvalds 			__skb_queue_tail(&skqueue, skb);
8071da177e4SLinus Torvalds 
808d04d01b1SMichael S. Tsirkin 		list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
809ee1e2c82SMoni Shoua 			if (neigh->ah) {
810ee1e2c82SMoni Shoua 				WARN_ON(neigh->ah != old_ah);
811ee1e2c82SMoni Shoua 				/*
812ee1e2c82SMoni Shoua 				 * Dropping the ah reference inside
813ee1e2c82SMoni Shoua 				 * priv->lock is safe here, because we
814ee1e2c82SMoni Shoua 				 * will hold one more reference from
815ee1e2c82SMoni Shoua 				 * the original value of path->ah (ie
816ee1e2c82SMoni Shoua 				 * old_ah).
817ee1e2c82SMoni Shoua 				 */
818ee1e2c82SMoni Shoua 				ipoib_put_ah(neigh->ah);
819ee1e2c82SMoni Shoua 			}
8201da177e4SLinus Torvalds 			kref_get(&path->ah->ref);
8211da177e4SLinus Torvalds 			neigh->ah = path->ah;
8221da177e4SLinus Torvalds 
823b63b70d8SShlomo Pongratz 			if (ipoib_cm_enabled(dev, neigh->daddr)) {
824839fcabaSMichael S. Tsirkin 				if (!ipoib_cm_get(neigh))
825839fcabaSMichael S. Tsirkin 					ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
826839fcabaSMichael S. Tsirkin 									       path,
827839fcabaSMichael S. Tsirkin 									       neigh));
828839fcabaSMichael S. Tsirkin 				if (!ipoib_cm_get(neigh)) {
829b63b70d8SShlomo Pongratz 					ipoib_neigh_free(neigh);
830839fcabaSMichael S. Tsirkin 					continue;
831839fcabaSMichael S. Tsirkin 				}
832839fcabaSMichael S. Tsirkin 			}
833839fcabaSMichael S. Tsirkin 
8341da177e4SLinus Torvalds 			while ((skb = __skb_dequeue(&neigh->queue)))
8351da177e4SLinus Torvalds 				__skb_queue_tail(&skqueue, skb);
8361da177e4SLinus Torvalds 		}
837fa9391dbSDoug Ledford 		path->ah->valid = 1;
8385872a9fcSRoland Dreier 	}
8391da177e4SLinus Torvalds 
8405872a9fcSRoland Dreier 	path->query = NULL;
8411da177e4SLinus Torvalds 	complete(&path->done);
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
8441da177e4SLinus Torvalds 
845f72dd566SRoland Dreier 	if (IS_ERR_OR_NULL(ah))
846f72dd566SRoland Dreier 		ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
847f72dd566SRoland Dreier 
848ee1e2c82SMoni Shoua 	if (old_ah)
849ee1e2c82SMoni Shoua 		ipoib_put_ah(old_ah);
850ee1e2c82SMoni Shoua 
8511da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&skqueue))) {
852d32b9a81SFeras Daoud 		int ret;
8531da177e4SLinus Torvalds 		skb->dev = dev;
854d32b9a81SFeras Daoud 		ret = dev_queue_xmit(skb);
855d32b9a81SFeras Daoud 		if (ret)
856d32b9a81SFeras Daoud 			ipoib_warn(priv, "%s: dev_queue_xmit failed to re-queue packet, ret:%d\n",
857d32b9a81SFeras Daoud 				   __func__, ret);
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
init_path_rec(struct ipoib_dev_priv * priv,struct ipoib_path * path,void * gid)86198aebc55SErez Shitrit static void init_path_rec(struct ipoib_dev_priv *priv, struct ipoib_path *path,
86298aebc55SErez Shitrit 			  void *gid)
86398aebc55SErez Shitrit {
86498aebc55SErez Shitrit 	path->dev = priv->dev;
86598aebc55SErez Shitrit 
86698aebc55SErez Shitrit 	if (rdma_cap_opa_ah(priv->ca, priv->port))
86798aebc55SErez Shitrit 		path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA;
86898aebc55SErez Shitrit 	else
86998aebc55SErez Shitrit 		path->pathrec.rec_type = SA_PATH_REC_TYPE_IB;
87098aebc55SErez Shitrit 
87198aebc55SErez Shitrit 	memcpy(path->pathrec.dgid.raw, gid, sizeof(union ib_gid));
87298aebc55SErez Shitrit 	path->pathrec.sgid	    = priv->local_gid;
87398aebc55SErez Shitrit 	path->pathrec.pkey	    = cpu_to_be16(priv->pkey);
87498aebc55SErez Shitrit 	path->pathrec.numb_path     = 1;
87598aebc55SErez Shitrit 	path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
87698aebc55SErez Shitrit }
87798aebc55SErez Shitrit 
path_rec_create(struct net_device * dev,void * gid)87837c22a77SJack Morgenstein static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
8791da177e4SLinus Torvalds {
880c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
8811da177e4SLinus Torvalds 	struct ipoib_path *path;
8821da177e4SLinus Torvalds 
8831401b53aSJack Morgenstein 	if (!priv->broadcast)
8841401b53aSJack Morgenstein 		return NULL;
8851401b53aSJack Morgenstein 
886b1b63970SKamal Heib 	path = kzalloc(sizeof(*path), GFP_ATOMIC);
8871da177e4SLinus Torvalds 	if (!path)
8881da177e4SLinus Torvalds 		return NULL;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	skb_queue_head_init(&path->queue);
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	INIT_LIST_HEAD(&path->neigh_list);
8931da177e4SLinus Torvalds 
89498aebc55SErez Shitrit 	init_path_rec(priv, path, gid);
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	return path;
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
path_rec_start(struct net_device * dev,struct ipoib_path * path)8991da177e4SLinus Torvalds static int path_rec_start(struct net_device *dev,
9001da177e4SLinus Torvalds 			  struct ipoib_path *path)
9011da177e4SLinus Torvalds {
902c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
9031da177e4SLinus Torvalds 
9045b095d98SHarvey Harrison 	ipoib_dbg(priv, "Start path record lookup for %pI6\n",
905fcace2feSHarvey Harrison 		  path->pathrec.dgid.raw);
9061da177e4SLinus Torvalds 
90765c7eddaSRoland Dreier 	init_completion(&path->done);
90865c7eddaSRoland Dreier 
9091da177e4SLinus Torvalds 	path->query_id =
910c1a0b23bSMichael S. Tsirkin 		ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
9111da177e4SLinus Torvalds 				   &path->pathrec,
9121da177e4SLinus Torvalds 				   IB_SA_PATH_REC_DGID		|
9131da177e4SLinus Torvalds 				   IB_SA_PATH_REC_SGID		|
9141da177e4SLinus Torvalds 				   IB_SA_PATH_REC_NUMB_PATH	|
91581668838SSean Hefty 				   IB_SA_PATH_REC_TRAFFIC_CLASS |
9161da177e4SLinus Torvalds 				   IB_SA_PATH_REC_PKEY,
9171da177e4SLinus Torvalds 				   1000, GFP_ATOMIC,
9181da177e4SLinus Torvalds 				   path_rec_completion,
9191da177e4SLinus Torvalds 				   path, &path->query);
9201da177e4SLinus Torvalds 	if (path->query_id < 0) {
92101b3fc8bSOr Gerlitz 		ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
9221da177e4SLinus Torvalds 		path->query = NULL;
92393a3ab93SYossi Etigin 		complete(&path->done);
9241da177e4SLinus Torvalds 		return path->query_id;
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	return 0;
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
neigh_refresh_path(struct ipoib_neigh * neigh,u8 * daddr,struct net_device * dev)930fa9391dbSDoug Ledford static void neigh_refresh_path(struct ipoib_neigh *neigh, u8 *daddr,
931fa9391dbSDoug Ledford 			       struct net_device *dev)
932fa9391dbSDoug Ledford {
933fa9391dbSDoug Ledford 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
934fa9391dbSDoug Ledford 	struct ipoib_path *path;
935fa9391dbSDoug Ledford 	unsigned long flags;
936fa9391dbSDoug Ledford 
937fa9391dbSDoug Ledford 	spin_lock_irqsave(&priv->lock, flags);
938fa9391dbSDoug Ledford 
939fa9391dbSDoug Ledford 	path = __path_find(dev, daddr + 4);
940fa9391dbSDoug Ledford 	if (!path)
941fa9391dbSDoug Ledford 		goto out;
942fa9391dbSDoug Ledford 	if (!path->query)
943fa9391dbSDoug Ledford 		path_rec_start(dev, path);
944fa9391dbSDoug Ledford out:
945fa9391dbSDoug Ledford 	spin_unlock_irqrestore(&priv->lock, flags);
946fa9391dbSDoug Ledford }
947fa9391dbSDoug Ledford 
neigh_add_path(struct sk_buff * skb,u8 * daddr,struct net_device * dev)94816ba3defSErez Shitrit static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr,
949b63b70d8SShlomo Pongratz 					  struct net_device *dev)
9501da177e4SLinus Torvalds {
951c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
952cd565b4bSErez Shitrit 	struct rdma_netdev *rn = netdev_priv(dev);
9531da177e4SLinus Torvalds 	struct ipoib_path *path;
9541da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
955943c246eSRoland Dreier 	unsigned long flags;
9561da177e4SLinus Torvalds 
957b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
958b63b70d8SShlomo Pongratz 	neigh = ipoib_neigh_alloc(daddr, dev);
9591da177e4SLinus Torvalds 	if (!neigh) {
960b5120a6eSShlomo Pongratz 		spin_unlock_irqrestore(&priv->lock, flags);
961de903512SRoland Dreier 		++dev->stats.tx_dropped;
9621da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
96316ba3defSErez Shitrit 		return NULL;
96416ba3defSErez Shitrit 	}
96516ba3defSErez Shitrit 
96616ba3defSErez Shitrit 	/* To avoid race condition, make sure that the
96716ba3defSErez Shitrit 	 * neigh will be added only once.
96816ba3defSErez Shitrit 	 */
96916ba3defSErez Shitrit 	if (unlikely(!list_empty(&neigh->list))) {
97016ba3defSErez Shitrit 		spin_unlock_irqrestore(&priv->lock, flags);
97116ba3defSErez Shitrit 		return neigh;
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 
974b63b70d8SShlomo Pongratz 	path = __path_find(dev, daddr + 4);
9751da177e4SLinus Torvalds 	if (!path) {
976b63b70d8SShlomo Pongratz 		path = path_rec_create(dev, daddr + 4);
9771da177e4SLinus Torvalds 		if (!path)
978d2e0655eSMichael S. Tsirkin 			goto err_path;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 		__path_add(dev, path);
9811da177e4SLinus Torvalds 	}
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	list_add_tail(&neigh->list, &path->neigh_list);
9841da177e4SLinus Torvalds 
985fa9391dbSDoug Ledford 	if (path->ah && path->ah->valid) {
9861da177e4SLinus Torvalds 		kref_get(&path->ah->ref);
9871da177e4SLinus Torvalds 		neigh->ah = path->ah;
9881da177e4SLinus Torvalds 
989b63b70d8SShlomo Pongratz 		if (ipoib_cm_enabled(dev, neigh->daddr)) {
990839fcabaSMichael S. Tsirkin 			if (!ipoib_cm_get(neigh))
991839fcabaSMichael S. Tsirkin 				ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
992839fcabaSMichael S. Tsirkin 			if (!ipoib_cm_get(neigh)) {
993b63b70d8SShlomo Pongratz 				ipoib_neigh_free(neigh);
994839fcabaSMichael S. Tsirkin 				goto err_drop;
995839fcabaSMichael S. Tsirkin 			}
996fc791b63SPaolo Abeni 			if (skb_queue_len(&neigh->queue) <
997fc791b63SPaolo Abeni 			    IPOIB_MAX_PATH_REC_QUEUE) {
9982b084176SErez Shitrit 				push_pseudo_header(skb, neigh->daddr);
999839fcabaSMichael S. Tsirkin 				__skb_queue_tail(&neigh->queue, skb);
1000fc791b63SPaolo Abeni 			} else {
1001839fcabaSMichael S. Tsirkin 				ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
1002839fcabaSMichael S. Tsirkin 					   skb_queue_len(&neigh->queue));
1003839fcabaSMichael S. Tsirkin 				goto err_drop;
1004839fcabaSMichael S. Tsirkin 			}
1005721d67cdSRoland Dreier 		} else {
1006721d67cdSRoland Dreier 			spin_unlock_irqrestore(&priv->lock, flags);
1007cd565b4bSErez Shitrit 			path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1008cd565b4bSErez Shitrit 						       IPOIB_QPN(daddr));
1009b63b70d8SShlomo Pongratz 			ipoib_neigh_put(neigh);
101016ba3defSErez Shitrit 			return NULL;
1011721d67cdSRoland Dreier 		}
10121da177e4SLinus Torvalds 	} else {
10131da177e4SLinus Torvalds 		neigh->ah  = NULL;
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 		if (!path->query && path_rec_start(dev, path))
101649b8e744SJim Foraker 			goto err_path;
10172b084176SErez Shitrit 		if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
10182b084176SErez Shitrit 			push_pseudo_header(skb, neigh->daddr);
10192745b5b7SMichael S. Tsirkin 			__skb_queue_tail(&neigh->queue, skb);
10202b084176SErez Shitrit 		} else {
10211e85b806SErez Shitrit 			goto err_drop;
10221da177e4SLinus Torvalds 		}
10232b084176SErez Shitrit 	}
10241da177e4SLinus Torvalds 
1025943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
1026b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
102716ba3defSErez Shitrit 	return NULL;
10281da177e4SLinus Torvalds 
1029d2e0655eSMichael S. Tsirkin err_path:
1030b63b70d8SShlomo Pongratz 	ipoib_neigh_free(neigh);
1031839fcabaSMichael S. Tsirkin err_drop:
1032de903512SRoland Dreier 	++dev->stats.tx_dropped;
10331da177e4SLinus Torvalds 	dev_kfree_skb_any(skb);
10341da177e4SLinus Torvalds 
1035943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
1036b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
103716ba3defSErez Shitrit 
103816ba3defSErez Shitrit 	return NULL;
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
unicast_arp_send(struct sk_buff * skb,struct net_device * dev,struct ipoib_pseudo_header * phdr)10411da177e4SLinus Torvalds static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
1042fc791b63SPaolo Abeni 			     struct ipoib_pseudo_header *phdr)
10431da177e4SLinus Torvalds {
1044c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1045cd565b4bSErez Shitrit 	struct rdma_netdev *rn = netdev_priv(dev);
10461da177e4SLinus Torvalds 	struct ipoib_path *path;
1047943c246eSRoland Dreier 	unsigned long flags;
10481da177e4SLinus Torvalds 
1049943c246eSRoland Dreier 	spin_lock_irqsave(&priv->lock, flags);
10501da177e4SLinus Torvalds 
105198aebc55SErez Shitrit 	/* no broadcast means that all paths are (going to be) not valid */
105298aebc55SErez Shitrit 	if (!priv->broadcast)
105398aebc55SErez Shitrit 		goto drop_and_unlock;
105498aebc55SErez Shitrit 
1055fc791b63SPaolo Abeni 	path = __path_find(dev, phdr->hwaddr + 4);
1056fa9391dbSDoug Ledford 	if (!path || !path->ah || !path->ah->valid) {
105771d98b46SJack Morgenstein 		if (!path) {
1058fc791b63SPaolo Abeni 			path = path_rec_create(dev, phdr->hwaddr + 4);
105915517080SEvgenii Smirnov 			if (!path)
106015517080SEvgenii Smirnov 				goto drop_and_unlock;
106115517080SEvgenii Smirnov 			__path_add(dev, path);
106215517080SEvgenii Smirnov 		} else {
106315517080SEvgenii Smirnov 			/*
106415517080SEvgenii Smirnov 			 * make sure there are no changes in the existing
106515517080SEvgenii Smirnov 			 * path record
106615517080SEvgenii Smirnov 			 */
106798aebc55SErez Shitrit 			init_path_rec(priv, path, phdr->hwaddr + 4);
106815517080SEvgenii Smirnov 		}
106915517080SEvgenii Smirnov 		if (!path->query && path_rec_start(dev, path)) {
107015517080SEvgenii Smirnov 			goto drop_and_unlock;
107115517080SEvgenii Smirnov 		}
107298aebc55SErez Shitrit 
10731e85b806SErez Shitrit 		if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
10742b084176SErez Shitrit 			push_pseudo_header(skb, phdr->hwaddr);
10751da177e4SLinus Torvalds 			__skb_queue_tail(&path->queue, skb);
107615517080SEvgenii Smirnov 			goto unlock;
10771da177e4SLinus Torvalds 		} else {
107898aebc55SErez Shitrit 			goto drop_and_unlock;
10791da177e4SLinus Torvalds 		}
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 
108215517080SEvgenii Smirnov 	spin_unlock_irqrestore(&priv->lock, flags);
108357520751SDasaratharaman Chandramouli 	ipoib_dbg(priv, "Send unicast ARP to %08x\n",
108457520751SDasaratharaman Chandramouli 		  be32_to_cpu(sa_path_get_dlid(&path->pathrec)));
1085cd565b4bSErez Shitrit 	path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1086cd565b4bSErez Shitrit 				       IPOIB_QPN(phdr->hwaddr));
1087721d67cdSRoland Dreier 	return;
108898aebc55SErez Shitrit 
108998aebc55SErez Shitrit drop_and_unlock:
109098aebc55SErez Shitrit 	++dev->stats.tx_dropped;
109198aebc55SErez Shitrit 	dev_kfree_skb_any(skb);
109215517080SEvgenii Smirnov unlock:
109398aebc55SErez Shitrit 	spin_unlock_irqrestore(&priv->lock, flags);
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
ipoib_start_xmit(struct sk_buff * skb,struct net_device * dev)109647a3968aSLuc Van Oostenryck static netdev_tx_t ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
10971da177e4SLinus Torvalds {
1098c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1099cd565b4bSErez Shitrit 	struct rdma_netdev *rn = netdev_priv(dev);
11001da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
1101fc791b63SPaolo Abeni 	struct ipoib_pseudo_header *phdr;
1102b63b70d8SShlomo Pongratz 	struct ipoib_header *header;
11031da177e4SLinus Torvalds 	unsigned long flags;
11041da177e4SLinus Torvalds 
1105fc791b63SPaolo Abeni 	phdr = (struct ipoib_pseudo_header *) skb->data;
1106fc791b63SPaolo Abeni 	skb_pull(skb, sizeof(*phdr));
1107b63b70d8SShlomo Pongratz 	header = (struct ipoib_header *) skb->data;
1108b63b70d8SShlomo Pongratz 
1109fc791b63SPaolo Abeni 	if (unlikely(phdr->hwaddr[4] == 0xff)) {
1110b63b70d8SShlomo Pongratz 		/* multicast, arrange "if" according to probability */
1111b63b70d8SShlomo Pongratz 		if ((header->proto != htons(ETH_P_IP)) &&
1112b63b70d8SShlomo Pongratz 		    (header->proto != htons(ETH_P_IPV6)) &&
1113b63b70d8SShlomo Pongratz 		    (header->proto != htons(ETH_P_ARP)) &&
1114dc850b0eSPatrick McHardy 		    (header->proto != htons(ETH_P_RARP)) &&
1115dc850b0eSPatrick McHardy 		    (header->proto != htons(ETH_P_TIPC))) {
1116b63b70d8SShlomo Pongratz 			/* ethertype not supported by IPoIB */
111717e6abeeSDavid Miller 			++dev->stats.tx_dropped;
111817e6abeeSDavid Miller 			dev_kfree_skb_any(skb);
1119b63b70d8SShlomo Pongratz 			return NETDEV_TX_OK;
112017e6abeeSDavid Miller 		}
1121b63b70d8SShlomo Pongratz 		/* Add in the P_Key for multicast*/
1122fc791b63SPaolo Abeni 		phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1123fc791b63SPaolo Abeni 		phdr->hwaddr[9] = priv->pkey & 0xff;
1124b63b70d8SShlomo Pongratz 
1125fc791b63SPaolo Abeni 		neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1126b63b70d8SShlomo Pongratz 		if (likely(neigh))
1127b63b70d8SShlomo Pongratz 			goto send_using_neigh;
1128fc791b63SPaolo Abeni 		ipoib_mcast_send(dev, phdr->hwaddr, skb);
1129b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 
1132b63b70d8SShlomo Pongratz 	/* unicast, arrange "switch" according to probability */
1133b63b70d8SShlomo Pongratz 	switch (header->proto) {
1134b63b70d8SShlomo Pongratz 	case htons(ETH_P_IP):
1135b63b70d8SShlomo Pongratz 	case htons(ETH_P_IPV6):
1136dc850b0eSPatrick McHardy 	case htons(ETH_P_TIPC):
1137fc791b63SPaolo Abeni 		neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1138b63b70d8SShlomo Pongratz 		if (unlikely(!neigh)) {
113916ba3defSErez Shitrit 			neigh = neigh_add_path(skb, phdr->hwaddr, dev);
114016ba3defSErez Shitrit 			if (likely(!neigh))
1141b63b70d8SShlomo Pongratz 				return NETDEV_TX_OK;
1142b63b70d8SShlomo Pongratz 		}
1143b63b70d8SShlomo Pongratz 		break;
1144b63b70d8SShlomo Pongratz 	case htons(ETH_P_ARP):
1145b63b70d8SShlomo Pongratz 	case htons(ETH_P_RARP):
1146b63b70d8SShlomo Pongratz 		/* for unicast ARP and RARP should always perform path find */
1147fc791b63SPaolo Abeni 		unicast_arp_send(skb, dev, phdr);
1148b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
1149b63b70d8SShlomo Pongratz 	default:
1150b63b70d8SShlomo Pongratz 		/* ethertype not supported by IPoIB */
1151b63b70d8SShlomo Pongratz 		++dev->stats.tx_dropped;
1152b63b70d8SShlomo Pongratz 		dev_kfree_skb_any(skb);
1153b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
11548a7f7521SMichael S. Tsirkin 	}
11558a7f7521SMichael S. Tsirkin 
1156b63b70d8SShlomo Pongratz send_using_neigh:
1157b63b70d8SShlomo Pongratz 	/* note we now hold a ref to neigh */
1158bafff974SOr Gerlitz 	if (ipoib_cm_get(neigh)) {
1159bafff974SOr Gerlitz 		if (ipoib_cm_up(neigh)) {
1160bafff974SOr Gerlitz 			ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1161b63b70d8SShlomo Pongratz 			goto unref;
1162bafff974SOr Gerlitz 		}
1163fa9391dbSDoug Ledford 	} else if (neigh->ah && neigh->ah->valid) {
1164cd565b4bSErez Shitrit 		neigh->ah->last_send = rn->send(dev, skb, neigh->ah->ah,
1165cd565b4bSErez Shitrit 						IPOIB_QPN(phdr->hwaddr));
1166b63b70d8SShlomo Pongratz 		goto unref;
1167fa9391dbSDoug Ledford 	} else if (neigh->ah) {
1168fa9391dbSDoug Ledford 		neigh_refresh_path(neigh, phdr->hwaddr, dev);
11691da177e4SLinus Torvalds 	}
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
11722b084176SErez Shitrit 		push_pseudo_header(skb, phdr->hwaddr);
1173943c246eSRoland Dreier 		spin_lock_irqsave(&priv->lock, flags);
11741da177e4SLinus Torvalds 		__skb_queue_tail(&neigh->queue, skb);
1175943c246eSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
11761da177e4SLinus Torvalds 	} else {
1177de903512SRoland Dreier 		++dev->stats.tx_dropped;
11781da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
11791da177e4SLinus Torvalds 	}
11801da177e4SLinus Torvalds 
1181b63b70d8SShlomo Pongratz unref:
1182b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	return NETDEV_TX_OK;
11851da177e4SLinus Torvalds }
11861da177e4SLinus Torvalds 
ipoib_timeout(struct net_device * dev,unsigned int txqueue)11870290bd29SMichael S. Tsirkin static void ipoib_timeout(struct net_device *dev, unsigned int txqueue)
11881da177e4SLinus Torvalds {
1189c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1190042a00f9SMike Marciniszyn 	struct rdma_netdev *rn = netdev_priv(dev);
11911da177e4SLinus Torvalds 
1192042a00f9SMike Marciniszyn 	if (rn->tx_timeout) {
1193042a00f9SMike Marciniszyn 		rn->tx_timeout(dev, txqueue);
1194042a00f9SMike Marciniszyn 		return;
1195042a00f9SMike Marciniszyn 	}
11964b2d319bSRoland Dreier 	ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
11974d0e9657SFlorian Westphal 		   jiffies_to_msecs(jiffies - dev_trans_start(dev)));
11981acba6a8SValentine Fatiev 	ipoib_warn(priv,
11991acba6a8SValentine Fatiev 		   "queue stopped %d, tx_head %u, tx_tail %u, global_tx_head %u, global_tx_tail %u\n",
12001acba6a8SValentine Fatiev 		   netif_queue_stopped(dev), priv->tx_head, priv->tx_tail,
12011acba6a8SValentine Fatiev 		   priv->global_tx_head, priv->global_tx_tail);
12021acba6a8SValentine Fatiev 
12031da177e4SLinus Torvalds 	/* XXX reset QP, etc. */
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
ipoib_hard_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned int len)12061da177e4SLinus Torvalds static int ipoib_hard_header(struct sk_buff *skb,
12071da177e4SLinus Torvalds 			     struct net_device *dev,
12081da177e4SLinus Torvalds 			     unsigned short type,
12090578cdadSKamal Heib 			     const void *daddr,
12100578cdadSKamal Heib 			     const void *saddr,
12110578cdadSKamal Heib 			     unsigned int len)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	struct ipoib_header *header;
12141da177e4SLinus Torvalds 
1215b1b63970SKamal Heib 	header = skb_push(skb, sizeof(*header));
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	header->proto = htons(type);
12181da177e4SLinus Torvalds 	header->reserved = 0;
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	/*
1221b63b70d8SShlomo Pongratz 	 * we don't rely on dst_entry structure,  always stuff the
1222fc791b63SPaolo Abeni 	 * destination address into skb hard header so we can figure out where
1223936d7de3SRoland Dreier 	 * to send the packet later.
12241da177e4SLinus Torvalds 	 */
12252b084176SErez Shitrit 	push_pseudo_header(skb, daddr);
12261da177e4SLinus Torvalds 
1227fc791b63SPaolo Abeni 	return IPOIB_HARD_LEN;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
ipoib_set_mcast_list(struct net_device * dev)12301da177e4SLinus Torvalds static void ipoib_set_mcast_list(struct net_device *dev)
12311da177e4SLinus Torvalds {
1232c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
12331da177e4SLinus Torvalds 
12347a343d4cSLeonid Arsh 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
12357a343d4cSLeonid Arsh 		ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
12367a343d4cSLeonid Arsh 		return;
12377a343d4cSLeonid Arsh 	}
12387a343d4cSLeonid Arsh 
12390b39578bSDoug Ledford 	queue_work(priv->wq, &priv->restart_task);
12401da177e4SLinus Torvalds }
12411da177e4SLinus Torvalds 
ipoib_get_iflink(const struct net_device * dev)12425aa7add8SNicolas Dichtel static int ipoib_get_iflink(const struct net_device *dev)
12435aa7add8SNicolas Dichtel {
1244c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
12455aa7add8SNicolas Dichtel 
12462c153959SErez Shitrit 	/* parent interface */
12472c153959SErez Shitrit 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
12482c153959SErez Shitrit 		return dev->ifindex;
12492c153959SErez Shitrit 
12502c153959SErez Shitrit 	/* child/vlan interface */
12515aa7add8SNicolas Dichtel 	return priv->parent->ifindex;
12525aa7add8SNicolas Dichtel }
12535aa7add8SNicolas Dichtel 
ipoib_addr_hash(struct ipoib_neigh_hash * htbl,u8 * daddr)1254b63b70d8SShlomo Pongratz static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
12551da177e4SLinus Torvalds {
1256b63b70d8SShlomo Pongratz 	/*
1257b63b70d8SShlomo Pongratz 	 * Use only the address parts that contributes to spreading
1258b63b70d8SShlomo Pongratz 	 * The subnet prefix is not used as one can not connect to
1259b63b70d8SShlomo Pongratz 	 * same remote port (GUID) using the same remote QPN via two
1260b63b70d8SShlomo Pongratz 	 * different subnets.
1261b63b70d8SShlomo Pongratz 	 */
1262b63b70d8SShlomo Pongratz 	 /* qpn octets[1:4) & port GUID octets[12:20) */
12639d1ad66eSShlomo Pongratz 	u32 *d32 = (u32 *) daddr;
1264b63b70d8SShlomo Pongratz 	u32 hv;
12651da177e4SLinus Torvalds 
12669d1ad66eSShlomo Pongratz 	hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
1267b63b70d8SShlomo Pongratz 	return hv & htbl->mask;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
ipoib_neigh_get(struct net_device * dev,u8 * daddr)1270b63b70d8SShlomo Pongratz struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1271b63b70d8SShlomo Pongratz {
1272c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1273b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1274b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1275b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh = NULL;
1276b63b70d8SShlomo Pongratz 	u32 hash_val;
1277b63b70d8SShlomo Pongratz 
1278b63b70d8SShlomo Pongratz 	rcu_read_lock_bh();
1279b63b70d8SShlomo Pongratz 
1280b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_bh(ntbl->htbl);
1281b63b70d8SShlomo Pongratz 
1282b63b70d8SShlomo Pongratz 	if (!htbl)
1283b63b70d8SShlomo Pongratz 		goto out_unlock;
1284b63b70d8SShlomo Pongratz 
1285b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, daddr);
1286b63b70d8SShlomo Pongratz 	for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1287b63b70d8SShlomo Pongratz 	     neigh != NULL;
1288b63b70d8SShlomo Pongratz 	     neigh = rcu_dereference_bh(neigh->hnext)) {
1289b63b70d8SShlomo Pongratz 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1290b63b70d8SShlomo Pongratz 			/* found, take one ref on behalf of the caller */
1291a5e27fb6SWeihang Li 			if (!refcount_inc_not_zero(&neigh->refcnt)) {
1292b63b70d8SShlomo Pongratz 				/* deleted */
1293b63b70d8SShlomo Pongratz 				neigh = NULL;
1294b63b70d8SShlomo Pongratz 				goto out_unlock;
1295b63b70d8SShlomo Pongratz 			}
129661c78eeaSErez Shitrit 
129761c78eeaSErez Shitrit 			if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1298b63b70d8SShlomo Pongratz 				neigh->alive = jiffies;
1299b63b70d8SShlomo Pongratz 			goto out_unlock;
1300b63b70d8SShlomo Pongratz 		}
1301b63b70d8SShlomo Pongratz 	}
1302b63b70d8SShlomo Pongratz 
1303b63b70d8SShlomo Pongratz out_unlock:
1304b63b70d8SShlomo Pongratz 	rcu_read_unlock_bh();
1305b63b70d8SShlomo Pongratz 	return neigh;
1306b63b70d8SShlomo Pongratz }
1307b63b70d8SShlomo Pongratz 
__ipoib_reap_neigh(struct ipoib_dev_priv * priv)1308b63b70d8SShlomo Pongratz static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1309b63b70d8SShlomo Pongratz {
1310b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1311b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1312b63b70d8SShlomo Pongratz 	unsigned long neigh_obsolete;
1313b63b70d8SShlomo Pongratz 	unsigned long dt;
1314b63b70d8SShlomo Pongratz 	unsigned long flags;
1315b63b70d8SShlomo Pongratz 	int i;
1316bd99b2e0SChristoph Lameter 	LIST_HEAD(remove_list);
1317b63b70d8SShlomo Pongratz 
1318b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1319b63b70d8SShlomo Pongratz 
1320b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1321b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
1322b63b70d8SShlomo Pongratz 
1323b63b70d8SShlomo Pongratz 	if (!htbl)
1324b63b70d8SShlomo Pongratz 		goto out_unlock;
1325b63b70d8SShlomo Pongratz 
1326b63b70d8SShlomo Pongratz 	/* neigh is obsolete if it was idle for two GC periods */
1327b63b70d8SShlomo Pongratz 	dt = 2 * arp_tbl.gc_interval;
1328b63b70d8SShlomo Pongratz 	neigh_obsolete = jiffies - dt;
1329b63b70d8SShlomo Pongratz 
1330b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1331b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1332b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1333b63b70d8SShlomo Pongratz 
1334b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1335b5120a6eSShlomo Pongratz 							  lockdep_is_held(&priv->lock))) != NULL) {
1336b63b70d8SShlomo Pongratz 			/* was the neigh idle for two GC periods */
1337b63b70d8SShlomo Pongratz 			if (time_after(neigh_obsolete, neigh->alive)) {
1338bd99b2e0SChristoph Lameter 
1339432c55ffSChristoph Lameter 				ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
1340bd99b2e0SChristoph Lameter 
1341b63b70d8SShlomo Pongratz 				rcu_assign_pointer(*np,
1342b63b70d8SShlomo Pongratz 						   rcu_dereference_protected(neigh->hnext,
1343b5120a6eSShlomo Pongratz 									     lockdep_is_held(&priv->lock)));
1344b63b70d8SShlomo Pongratz 				/* remove from path/mc list */
1345c586071dSFeras Daoud 				list_del_init(&neigh->list);
1346b63b70d8SShlomo Pongratz 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1347b63b70d8SShlomo Pongratz 			} else {
1348b63b70d8SShlomo Pongratz 				np = &neigh->hnext;
1349b63b70d8SShlomo Pongratz 			}
1350b63b70d8SShlomo Pongratz 
1351b63b70d8SShlomo Pongratz 		}
1352b63b70d8SShlomo Pongratz 	}
1353b63b70d8SShlomo Pongratz 
1354b63b70d8SShlomo Pongratz out_unlock:
1355b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
135650be28deSErez Shitrit 	ipoib_mcast_remove_list(&remove_list);
1357b63b70d8SShlomo Pongratz }
1358b63b70d8SShlomo Pongratz 
ipoib_reap_neigh(struct work_struct * work)1359b63b70d8SShlomo Pongratz static void ipoib_reap_neigh(struct work_struct *work)
1360b63b70d8SShlomo Pongratz {
1361b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv =
1362b63b70d8SShlomo Pongratz 		container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1363b63b70d8SShlomo Pongratz 
1364b63b70d8SShlomo Pongratz 	__ipoib_reap_neigh(priv);
1365b63b70d8SShlomo Pongratz 
13660b39578bSDoug Ledford 	queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1367b63b70d8SShlomo Pongratz 			   arp_tbl.gc_interval);
1368b63b70d8SShlomo Pongratz }
1369b63b70d8SShlomo Pongratz 
1370b63b70d8SShlomo Pongratz 
ipoib_neigh_ctor(u8 * daddr,struct net_device * dev)1371b63b70d8SShlomo Pongratz static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
1372732a2170SMoni Shoua 				      struct net_device *dev)
1373d2e0655eSMichael S. Tsirkin {
1374d2e0655eSMichael S. Tsirkin 	struct ipoib_neigh *neigh;
1375d2e0655eSMichael S. Tsirkin 
1376b1b63970SKamal Heib 	neigh = kzalloc(sizeof(*neigh), GFP_ATOMIC);
1377d2e0655eSMichael S. Tsirkin 	if (!neigh)
1378d2e0655eSMichael S. Tsirkin 		return NULL;
1379d2e0655eSMichael S. Tsirkin 
1380732a2170SMoni Shoua 	neigh->dev = dev;
1381b63b70d8SShlomo Pongratz 	memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
138282b39913SRoland Dreier 	skb_queue_head_init(&neigh->queue);
1383b63b70d8SShlomo Pongratz 	INIT_LIST_HEAD(&neigh->list);
1384839fcabaSMichael S. Tsirkin 	ipoib_cm_set(neigh, NULL);
1385b63b70d8SShlomo Pongratz 	/* one ref on behalf of the caller */
1386a5e27fb6SWeihang Li 	refcount_set(&neigh->refcnt, 1);
1387d2e0655eSMichael S. Tsirkin 
1388d2e0655eSMichael S. Tsirkin 	return neigh;
1389d2e0655eSMichael S. Tsirkin }
1390d2e0655eSMichael S. Tsirkin 
ipoib_neigh_alloc(u8 * daddr,struct net_device * dev)1391b63b70d8SShlomo Pongratz struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1392b63b70d8SShlomo Pongratz 				      struct net_device *dev)
1393d2e0655eSMichael S. Tsirkin {
1394c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1395b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1396b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1397b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh;
1398b63b70d8SShlomo Pongratz 	u32 hash_val;
1399b63b70d8SShlomo Pongratz 
1400b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1401b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
1402b63b70d8SShlomo Pongratz 	if (!htbl) {
1403b63b70d8SShlomo Pongratz 		neigh = NULL;
1404b63b70d8SShlomo Pongratz 		goto out_unlock;
1405b63b70d8SShlomo Pongratz 	}
1406b63b70d8SShlomo Pongratz 
1407b63b70d8SShlomo Pongratz 	/* need to add a new neigh, but maybe some other thread succeeded?
1408b63b70d8SShlomo Pongratz 	 * recalc hash, maybe hash resize took place so we do a search
1409b63b70d8SShlomo Pongratz 	 */
1410b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, daddr);
1411b63b70d8SShlomo Pongratz 	for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1412b5120a6eSShlomo Pongratz 					       lockdep_is_held(&priv->lock));
1413b63b70d8SShlomo Pongratz 	     neigh != NULL;
1414b63b70d8SShlomo Pongratz 	     neigh = rcu_dereference_protected(neigh->hnext,
1415b5120a6eSShlomo Pongratz 					       lockdep_is_held(&priv->lock))) {
1416b63b70d8SShlomo Pongratz 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1417b63b70d8SShlomo Pongratz 			/* found, take one ref on behalf of the caller */
1418a5e27fb6SWeihang Li 			if (!refcount_inc_not_zero(&neigh->refcnt)) {
1419b63b70d8SShlomo Pongratz 				/* deleted */
1420b63b70d8SShlomo Pongratz 				neigh = NULL;
1421b63b70d8SShlomo Pongratz 				break;
1422b63b70d8SShlomo Pongratz 			}
1423b63b70d8SShlomo Pongratz 			neigh->alive = jiffies;
1424b63b70d8SShlomo Pongratz 			goto out_unlock;
1425b63b70d8SShlomo Pongratz 		}
1426b63b70d8SShlomo Pongratz 	}
1427b63b70d8SShlomo Pongratz 
1428b63b70d8SShlomo Pongratz 	neigh = ipoib_neigh_ctor(daddr, dev);
1429b63b70d8SShlomo Pongratz 	if (!neigh)
1430b63b70d8SShlomo Pongratz 		goto out_unlock;
1431b63b70d8SShlomo Pongratz 
1432b63b70d8SShlomo Pongratz 	/* one ref on behalf of the hash table */
1433a5e27fb6SWeihang Li 	refcount_inc(&neigh->refcnt);
1434b63b70d8SShlomo Pongratz 	neigh->alive = jiffies;
1435b63b70d8SShlomo Pongratz 	/* put in hash */
1436b63b70d8SShlomo Pongratz 	rcu_assign_pointer(neigh->hnext,
1437b63b70d8SShlomo Pongratz 			   rcu_dereference_protected(htbl->buckets[hash_val],
1438b5120a6eSShlomo Pongratz 						     lockdep_is_held(&priv->lock)));
1439b63b70d8SShlomo Pongratz 	rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1440b63b70d8SShlomo Pongratz 	atomic_inc(&ntbl->entries);
1441b63b70d8SShlomo Pongratz 
1442b63b70d8SShlomo Pongratz out_unlock:
1443b63b70d8SShlomo Pongratz 
1444b63b70d8SShlomo Pongratz 	return neigh;
1445b63b70d8SShlomo Pongratz }
1446b63b70d8SShlomo Pongratz 
ipoib_neigh_dtor(struct ipoib_neigh * neigh)1447b63b70d8SShlomo Pongratz void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1448b63b70d8SShlomo Pongratz {
1449b63b70d8SShlomo Pongratz 	/* neigh reference count was dropprd to zero */
1450b63b70d8SShlomo Pongratz 	struct net_device *dev = neigh->dev;
1451c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
14522745b5b7SMichael S. Tsirkin 	struct sk_buff *skb;
1453b63b70d8SShlomo Pongratz 	if (neigh->ah)
1454b63b70d8SShlomo Pongratz 		ipoib_put_ah(neigh->ah);
14552745b5b7SMichael S. Tsirkin 	while ((skb = __skb_dequeue(&neigh->queue))) {
1456de903512SRoland Dreier 		++dev->stats.tx_dropped;
14572745b5b7SMichael S. Tsirkin 		dev_kfree_skb_any(skb);
14582745b5b7SMichael S. Tsirkin 	}
1459839fcabaSMichael S. Tsirkin 	if (ipoib_cm_get(neigh))
1460839fcabaSMichael S. Tsirkin 		ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1461c1048affSErez Shitrit 	ipoib_dbg(ipoib_priv(dev),
1462b63b70d8SShlomo Pongratz 		  "neigh free for %06x %pI6\n",
1463b63b70d8SShlomo Pongratz 		  IPOIB_QPN(neigh->daddr),
1464b63b70d8SShlomo Pongratz 		  neigh->daddr + 4);
1465d2e0655eSMichael S. Tsirkin 	kfree(neigh);
1466b63b70d8SShlomo Pongratz 	if (atomic_dec_and_test(&priv->ntbl.entries)) {
1467b63b70d8SShlomo Pongratz 		if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1468b63b70d8SShlomo Pongratz 			complete(&priv->ntbl.flushed);
1469b63b70d8SShlomo Pongratz 	}
1470d2e0655eSMichael S. Tsirkin }
1471d2e0655eSMichael S. Tsirkin 
ipoib_neigh_reclaim(struct rcu_head * rp)1472b63b70d8SShlomo Pongratz static void ipoib_neigh_reclaim(struct rcu_head *rp)
14731da177e4SLinus Torvalds {
1474b63b70d8SShlomo Pongratz 	/* Called as a result of removal from hash table */
1475b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1476b63b70d8SShlomo Pongratz 	/* note TX context may hold another ref */
1477b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
1478b63b70d8SShlomo Pongratz }
1479b63b70d8SShlomo Pongratz 
ipoib_neigh_free(struct ipoib_neigh * neigh)1480b63b70d8SShlomo Pongratz void ipoib_neigh_free(struct ipoib_neigh *neigh)
1481b63b70d8SShlomo Pongratz {
1482b63b70d8SShlomo Pongratz 	struct net_device *dev = neigh->dev;
1483c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1484b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1485b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1486b63b70d8SShlomo Pongratz 	struct ipoib_neigh __rcu **np;
1487b63b70d8SShlomo Pongratz 	struct ipoib_neigh *n;
1488b63b70d8SShlomo Pongratz 	u32 hash_val;
1489b63b70d8SShlomo Pongratz 
1490b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1491b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock));
1492b63b70d8SShlomo Pongratz 	if (!htbl)
1493b5120a6eSShlomo Pongratz 		return;
1494b63b70d8SShlomo Pongratz 
1495b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1496b63b70d8SShlomo Pongratz 	np = &htbl->buckets[hash_val];
1497b63b70d8SShlomo Pongratz 	for (n = rcu_dereference_protected(*np,
1498b5120a6eSShlomo Pongratz 					    lockdep_is_held(&priv->lock));
1499b63b70d8SShlomo Pongratz 	     n != NULL;
15006c723a68SShlomo Pongratz 	     n = rcu_dereference_protected(*np,
1501b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock))) {
1502b63b70d8SShlomo Pongratz 		if (n == neigh) {
1503b63b70d8SShlomo Pongratz 			/* found */
1504b63b70d8SShlomo Pongratz 			rcu_assign_pointer(*np,
1505b63b70d8SShlomo Pongratz 					   rcu_dereference_protected(neigh->hnext,
1506b5120a6eSShlomo Pongratz 								     lockdep_is_held(&priv->lock)));
150749b8e744SJim Foraker 			/* remove from parent list */
1508c586071dSFeras Daoud 			list_del_init(&neigh->list);
1509b63b70d8SShlomo Pongratz 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1510b5120a6eSShlomo Pongratz 			return;
1511b63b70d8SShlomo Pongratz 		} else {
1512b63b70d8SShlomo Pongratz 			np = &n->hnext;
1513b63b70d8SShlomo Pongratz 		}
1514b63b70d8SShlomo Pongratz 	}
1515b63b70d8SShlomo Pongratz }
1516b63b70d8SShlomo Pongratz 
ipoib_neigh_hash_init(struct ipoib_dev_priv * priv)1517b63b70d8SShlomo Pongratz static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1518b63b70d8SShlomo Pongratz {
1519b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1520b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
152152374967SBart Van Assche 	struct ipoib_neigh __rcu **buckets;
1522b63b70d8SShlomo Pongratz 	u32 size;
1523b63b70d8SShlomo Pongratz 
1524b63b70d8SShlomo Pongratz 	clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1525b63b70d8SShlomo Pongratz 	ntbl->htbl = NULL;
1526b63b70d8SShlomo Pongratz 	htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1527b63b70d8SShlomo Pongratz 	if (!htbl)
1528b63b70d8SShlomo Pongratz 		return -ENOMEM;
1529b63b70d8SShlomo Pongratz 	size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1530259e1914SJan Dakinevich 	buckets = kvcalloc(size, sizeof(*buckets), GFP_KERNEL);
1531b63b70d8SShlomo Pongratz 	if (!buckets) {
1532b63b70d8SShlomo Pongratz 		kfree(htbl);
1533b63b70d8SShlomo Pongratz 		return -ENOMEM;
1534b63b70d8SShlomo Pongratz 	}
1535b63b70d8SShlomo Pongratz 	htbl->size = size;
1536b63b70d8SShlomo Pongratz 	htbl->mask = (size - 1);
1537b63b70d8SShlomo Pongratz 	htbl->buckets = buckets;
153852374967SBart Van Assche 	RCU_INIT_POINTER(ntbl->htbl, htbl);
153966172c09SShlomo Pongratz 	htbl->ntbl = ntbl;
1540b63b70d8SShlomo Pongratz 	atomic_set(&ntbl->entries, 0);
1541b63b70d8SShlomo Pongratz 
1542b63b70d8SShlomo Pongratz 	/* start garbage collection */
15430b39578bSDoug Ledford 	queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1544b63b70d8SShlomo Pongratz 			   arp_tbl.gc_interval);
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	return 0;
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
neigh_hash_free_rcu(struct rcu_head * head)1549b63b70d8SShlomo Pongratz static void neigh_hash_free_rcu(struct rcu_head *head)
1550b63b70d8SShlomo Pongratz {
1551b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl = container_of(head,
1552b63b70d8SShlomo Pongratz 						    struct ipoib_neigh_hash,
1553b63b70d8SShlomo Pongratz 						    rcu);
1554b63b70d8SShlomo Pongratz 	struct ipoib_neigh __rcu **buckets = htbl->buckets;
155566172c09SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = htbl->ntbl;
1556b63b70d8SShlomo Pongratz 
1557259e1914SJan Dakinevich 	kvfree(buckets);
1558b63b70d8SShlomo Pongratz 	kfree(htbl);
155966172c09SShlomo Pongratz 	complete(&ntbl->deleted);
1560b63b70d8SShlomo Pongratz }
1561b63b70d8SShlomo Pongratz 
ipoib_del_neighs_by_gid(struct net_device * dev,u8 * gid)1562b63b70d8SShlomo Pongratz void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1563b63b70d8SShlomo Pongratz {
1564c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1565b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1566b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1567b63b70d8SShlomo Pongratz 	unsigned long flags;
1568b63b70d8SShlomo Pongratz 	int i;
1569b63b70d8SShlomo Pongratz 
1570b63b70d8SShlomo Pongratz 	/* remove all neigh connected to a given path or mcast */
1571b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1572b63b70d8SShlomo Pongratz 
1573b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1574b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
1575b63b70d8SShlomo Pongratz 
1576b63b70d8SShlomo Pongratz 	if (!htbl)
1577b63b70d8SShlomo Pongratz 		goto out_unlock;
1578b63b70d8SShlomo Pongratz 
1579b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1580b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1581b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1582b63b70d8SShlomo Pongratz 
1583b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1584b5120a6eSShlomo Pongratz 							  lockdep_is_held(&priv->lock))) != NULL) {
1585b63b70d8SShlomo Pongratz 			/* delete neighs belong to this parent */
1586b63b70d8SShlomo Pongratz 			if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1587b63b70d8SShlomo Pongratz 				rcu_assign_pointer(*np,
1588b63b70d8SShlomo Pongratz 						   rcu_dereference_protected(neigh->hnext,
1589b5120a6eSShlomo Pongratz 									     lockdep_is_held(&priv->lock)));
1590b63b70d8SShlomo Pongratz 				/* remove from parent list */
1591c586071dSFeras Daoud 				list_del_init(&neigh->list);
1592b63b70d8SShlomo Pongratz 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1593b63b70d8SShlomo Pongratz 			} else {
1594b63b70d8SShlomo Pongratz 				np = &neigh->hnext;
1595b63b70d8SShlomo Pongratz 			}
1596b63b70d8SShlomo Pongratz 
1597b63b70d8SShlomo Pongratz 		}
1598b63b70d8SShlomo Pongratz 	}
1599b63b70d8SShlomo Pongratz out_unlock:
1600b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
1601b63b70d8SShlomo Pongratz }
1602b63b70d8SShlomo Pongratz 
ipoib_flush_neighs(struct ipoib_dev_priv * priv)1603b63b70d8SShlomo Pongratz static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1604b63b70d8SShlomo Pongratz {
1605b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1606b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1607b63b70d8SShlomo Pongratz 	unsigned long flags;
160866172c09SShlomo Pongratz 	int i, wait_flushed = 0;
1609b63b70d8SShlomo Pongratz 
161066172c09SShlomo Pongratz 	init_completion(&priv->ntbl.flushed);
1611d2e46fccSFeras Daoud 	set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1612b63b70d8SShlomo Pongratz 
1613b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1614b63b70d8SShlomo Pongratz 
1615b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1616b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock));
1617b63b70d8SShlomo Pongratz 	if (!htbl)
1618b63b70d8SShlomo Pongratz 		goto out_unlock;
1619b63b70d8SShlomo Pongratz 
162066172c09SShlomo Pongratz 	wait_flushed = atomic_read(&priv->ntbl.entries);
162166172c09SShlomo Pongratz 	if (!wait_flushed)
162266172c09SShlomo Pongratz 		goto free_htbl;
162366172c09SShlomo Pongratz 
1624b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1625b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1626b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1627b63b70d8SShlomo Pongratz 
1628b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1629b5120a6eSShlomo Pongratz 				       lockdep_is_held(&priv->lock))) != NULL) {
1630b63b70d8SShlomo Pongratz 			rcu_assign_pointer(*np,
1631b63b70d8SShlomo Pongratz 					   rcu_dereference_protected(neigh->hnext,
1632b5120a6eSShlomo Pongratz 								     lockdep_is_held(&priv->lock)));
1633b63b70d8SShlomo Pongratz 			/* remove from path/mc list */
1634c586071dSFeras Daoud 			list_del_init(&neigh->list);
1635b63b70d8SShlomo Pongratz 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1636b63b70d8SShlomo Pongratz 		}
1637b63b70d8SShlomo Pongratz 	}
1638b63b70d8SShlomo Pongratz 
163966172c09SShlomo Pongratz free_htbl:
1640b63b70d8SShlomo Pongratz 	rcu_assign_pointer(ntbl->htbl, NULL);
1641b63b70d8SShlomo Pongratz 	call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1642b63b70d8SShlomo Pongratz 
1643b63b70d8SShlomo Pongratz out_unlock:
1644b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
164566172c09SShlomo Pongratz 	if (wait_flushed)
164666172c09SShlomo Pongratz 		wait_for_completion(&priv->ntbl.flushed);
1647b63b70d8SShlomo Pongratz }
1648b63b70d8SShlomo Pongratz 
ipoib_neigh_hash_uninit(struct net_device * dev)1649b63b70d8SShlomo Pongratz static void ipoib_neigh_hash_uninit(struct net_device *dev)
1650b63b70d8SShlomo Pongratz {
1651c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1652b63b70d8SShlomo Pongratz 
16530dd9ce18SErez Alfasi 	ipoib_dbg(priv, "%s\n", __func__);
165466172c09SShlomo Pongratz 	init_completion(&priv->ntbl.deleted);
1655b63b70d8SShlomo Pongratz 
1656cda8daf1SErez Shitrit 	cancel_delayed_work_sync(&priv->neigh_reap_task);
1657b63b70d8SShlomo Pongratz 
1658b63b70d8SShlomo Pongratz 	ipoib_flush_neighs(priv);
165966172c09SShlomo Pongratz 
166066172c09SShlomo Pongratz 	wait_for_completion(&priv->ntbl.deleted);
1661b63b70d8SShlomo Pongratz }
1662b63b70d8SShlomo Pongratz 
ipoib_napi_add(struct net_device * dev)16638966e28dSErez Shitrit static void ipoib_napi_add(struct net_device *dev)
16648966e28dSErez Shitrit {
16658966e28dSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
16668966e28dSErez Shitrit 
16672157f5caSJakub Kicinski 	netif_napi_add_weight(dev, &priv->recv_napi, ipoib_rx_poll,
16682157f5caSJakub Kicinski 			      IPOIB_NUM_WC);
16692157f5caSJakub Kicinski 	netif_napi_add_weight(dev, &priv->send_napi, ipoib_tx_poll,
16702157f5caSJakub Kicinski 			      MAX_SEND_CQE);
16718966e28dSErez Shitrit }
16728966e28dSErez Shitrit 
ipoib_napi_del(struct net_device * dev)16738966e28dSErez Shitrit static void ipoib_napi_del(struct net_device *dev)
16748966e28dSErez Shitrit {
16758966e28dSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
16768966e28dSErez Shitrit 
16778966e28dSErez Shitrit 	netif_napi_del(&priv->recv_napi);
16788966e28dSErez Shitrit 	netif_napi_del(&priv->send_napi);
16798966e28dSErez Shitrit }
16808966e28dSErez Shitrit 
ipoib_dev_uninit_default(struct net_device * dev)16810a1a9726SLeon Romanovsky static void ipoib_dev_uninit_default(struct net_device *dev)
1682515ed4f3SErez Shitrit {
1683c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1684b63b70d8SShlomo Pongratz 
1685515ed4f3SErez Shitrit 	ipoib_transport_dev_cleanup(dev);
1686515ed4f3SErez Shitrit 
16878966e28dSErez Shitrit 	ipoib_napi_del(dev);
1688b53d4566SAlex Vesker 
1689515ed4f3SErez Shitrit 	ipoib_cm_dev_cleanup(dev);
1690515ed4f3SErez Shitrit 
1691515ed4f3SErez Shitrit 	kfree(priv->rx_ring);
1692515ed4f3SErez Shitrit 	vfree(priv->tx_ring);
1693515ed4f3SErez Shitrit 
1694515ed4f3SErez Shitrit 	priv->rx_ring = NULL;
1695515ed4f3SErez Shitrit 	priv->tx_ring = NULL;
1696515ed4f3SErez Shitrit }
1697515ed4f3SErez Shitrit 
ipoib_dev_init_default(struct net_device * dev)1698cd565b4bSErez Shitrit static int ipoib_dev_init_default(struct net_device *dev)
16991da177e4SLinus Torvalds {
1700c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
170110f7b9bcSJakub Kicinski 	u8 addr_mod[3];
17021da177e4SLinus Torvalds 
17038966e28dSErez Shitrit 	ipoib_napi_add(dev);
1704cd565b4bSErez Shitrit 
17051da177e4SLinus Torvalds 	/* Allocate RX/TX "rings" to hold queued skbs */
17066396bb22SKees Cook 	priv->rx_ring =	kcalloc(ipoib_recvq_size,
17076396bb22SKees Cook 				       sizeof(*priv->rx_ring),
17081da177e4SLinus Torvalds 				       GFP_KERNEL);
170974226649SLeon Romanovsky 	if (!priv->rx_ring)
1710be7aa663SDoug Ledford 		goto out;
17111da177e4SLinus Torvalds 
1712fad953ceSKees Cook 	priv->tx_ring = vzalloc(array_size(ipoib_sendq_size,
1713fad953ceSKees Cook 					   sizeof(*priv->tx_ring)));
17141da177e4SLinus Torvalds 	if (!priv->tx_ring) {
1715c55359a2SYuval Shaia 		pr_warn("%s: failed to allocate TX ring (%d entries)\n",
1716cd565b4bSErez Shitrit 			priv->ca->name, ipoib_sendq_size);
17171da177e4SLinus Torvalds 		goto out_rx_ring_cleanup;
17181da177e4SLinus Torvalds 	}
17191da177e4SLinus Torvalds 
17201acba6a8SValentine Fatiev 	/* priv->tx_head, tx_tail and global_tx_tail/head are already 0 */
17211da177e4SLinus Torvalds 
1722cd565b4bSErez Shitrit 	if (ipoib_transport_dev_init(dev, priv->ca)) {
1723cd565b4bSErez Shitrit 		pr_warn("%s: ipoib_transport_dev_init failed\n",
1724cd565b4bSErez Shitrit 			priv->ca->name);
17251da177e4SLinus Torvalds 		goto out_tx_ring_cleanup;
1726515ed4f3SErez Shitrit 	}
17271da177e4SLinus Torvalds 
1728cd565b4bSErez Shitrit 	/* after qp created set dev address */
172910f7b9bcSJakub Kicinski 	addr_mod[0] = (priv->qp->qp_num >> 16) & 0xff;
173010f7b9bcSJakub Kicinski 	addr_mod[1] = (priv->qp->qp_num >>  8) & 0xff;
173110f7b9bcSJakub Kicinski 	addr_mod[2] = (priv->qp->qp_num) & 0xff;
173210f7b9bcSJakub Kicinski 	dev_addr_mod(priv->dev, 1, addr_mod, sizeof(addr_mod));
1733cd565b4bSErez Shitrit 
17341da177e4SLinus Torvalds 	return 0;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds out_tx_ring_cleanup:
173710313cbbSRoland Dreier 	vfree(priv->tx_ring);
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds out_rx_ring_cleanup:
17401da177e4SLinus Torvalds 	kfree(priv->rx_ring);
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds out:
17438966e28dSErez Shitrit 	ipoib_napi_del(dev);
17441da177e4SLinus Torvalds 	return -ENOMEM;
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
ipoib_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)174731a82362SFeras Daoud static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
174831a82362SFeras Daoud 		       int cmd)
174931a82362SFeras Daoud {
175031a82362SFeras Daoud 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
175131a82362SFeras Daoud 
1752a7605370SArnd Bergmann 	if (!priv->rn_ops->ndo_eth_ioctl)
175331a82362SFeras Daoud 		return -EOPNOTSUPP;
175431a82362SFeras Daoud 
1755a7605370SArnd Bergmann 	return priv->rn_ops->ndo_eth_ioctl(dev, ifr, cmd);
175631a82362SFeras Daoud }
175731a82362SFeras Daoud 
ipoib_dev_init(struct net_device * dev)1758eaeb3984SJason Gunthorpe static int ipoib_dev_init(struct net_device *dev)
1759515ed4f3SErez Shitrit {
1760c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1761515ed4f3SErez Shitrit 	int ret = -ENOMEM;
1762515ed4f3SErez Shitrit 
1763515ed4f3SErez Shitrit 	priv->qp = NULL;
1764515ed4f3SErez Shitrit 
1765515ed4f3SErez Shitrit 	/*
1766515ed4f3SErez Shitrit 	 * the various IPoIB tasks assume they will never race against
1767515ed4f3SErez Shitrit 	 * themselves, so always use a single thread workqueue
1768515ed4f3SErez Shitrit 	 */
1769515ed4f3SErez Shitrit 	priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM);
1770515ed4f3SErez Shitrit 	if (!priv->wq) {
1771515ed4f3SErez Shitrit 		pr_warn("%s: failed to allocate device WQ\n", dev->name);
1772515ed4f3SErez Shitrit 		goto out;
1773515ed4f3SErez Shitrit 	}
1774515ed4f3SErez Shitrit 
1775515ed4f3SErez Shitrit 	/* create pd, which used both for control and datapath*/
1776515ed4f3SErez Shitrit 	priv->pd = ib_alloc_pd(priv->ca, 0);
1777515ed4f3SErez Shitrit 	if (IS_ERR(priv->pd)) {
1778eaeb3984SJason Gunthorpe 		pr_warn("%s: failed to allocate PD\n", priv->ca->name);
1779515ed4f3SErez Shitrit 		goto clean_wq;
1780515ed4f3SErez Shitrit 	}
1781515ed4f3SErez Shitrit 
1782cd565b4bSErez Shitrit 	ret = priv->rn_ops->ndo_init(dev);
1783515ed4f3SErez Shitrit 	if (ret) {
1784515ed4f3SErez Shitrit 		pr_warn("%s failed to init HW resource\n", dev->name);
1785515ed4f3SErez Shitrit 		goto out_free_pd;
1786515ed4f3SErez Shitrit 	}
1787515ed4f3SErez Shitrit 
178899a7e2bfSWei Yongjun 	ret = ipoib_neigh_hash_init(priv);
178999a7e2bfSWei Yongjun 	if (ret) {
1790515ed4f3SErez Shitrit 		pr_warn("%s failed to init neigh hash\n", dev->name);
1791515ed4f3SErez Shitrit 		goto out_dev_uninit;
1792515ed4f3SErez Shitrit 	}
1793515ed4f3SErez Shitrit 
1794515ed4f3SErez Shitrit 	if (dev->flags & IFF_UP) {
1795515ed4f3SErez Shitrit 		if (ipoib_ib_dev_open(dev)) {
1796515ed4f3SErez Shitrit 			pr_warn("%s failed to open device\n", dev->name);
1797515ed4f3SErez Shitrit 			ret = -ENODEV;
1798cda8daf1SErez Shitrit 			goto out_hash_uninit;
1799515ed4f3SErez Shitrit 		}
1800515ed4f3SErez Shitrit 	}
1801515ed4f3SErez Shitrit 
1802515ed4f3SErez Shitrit 	return 0;
1803515ed4f3SErez Shitrit 
1804cda8daf1SErez Shitrit out_hash_uninit:
1805cda8daf1SErez Shitrit 	ipoib_neigh_hash_uninit(dev);
1806cda8daf1SErez Shitrit 
1807515ed4f3SErez Shitrit out_dev_uninit:
1808515ed4f3SErez Shitrit 	ipoib_ib_dev_cleanup(dev);
1809515ed4f3SErez Shitrit 
1810515ed4f3SErez Shitrit out_free_pd:
1811515ed4f3SErez Shitrit 	if (priv->pd) {
1812515ed4f3SErez Shitrit 		ib_dealloc_pd(priv->pd);
1813515ed4f3SErez Shitrit 		priv->pd = NULL;
1814515ed4f3SErez Shitrit 	}
1815515ed4f3SErez Shitrit 
1816515ed4f3SErez Shitrit clean_wq:
1817515ed4f3SErez Shitrit 	if (priv->wq) {
1818515ed4f3SErez Shitrit 		destroy_workqueue(priv->wq);
1819515ed4f3SErez Shitrit 		priv->wq = NULL;
1820515ed4f3SErez Shitrit 	}
1821515ed4f3SErez Shitrit 
1822515ed4f3SErez Shitrit out:
1823515ed4f3SErez Shitrit 	return ret;
1824515ed4f3SErez Shitrit }
1825515ed4f3SErez Shitrit 
18267cbee87cSJason Gunthorpe /*
18277cbee87cSJason Gunthorpe  * This must be called before doing an unregister_netdev on a parent device to
18287cbee87cSJason Gunthorpe  * shutdown the IB event handler.
18297cbee87cSJason Gunthorpe  */
ipoib_parent_unregister_pre(struct net_device * ndev)18307cbee87cSJason Gunthorpe static void ipoib_parent_unregister_pre(struct net_device *ndev)
18317cbee87cSJason Gunthorpe {
18327cbee87cSJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
18337cbee87cSJason Gunthorpe 
18347cbee87cSJason Gunthorpe 	/*
18357cbee87cSJason Gunthorpe 	 * ipoib_set_mac checks netif_running before pushing work, clearing
18367cbee87cSJason Gunthorpe 	 * running ensures the it will not add more work.
18377cbee87cSJason Gunthorpe 	 */
18387cbee87cSJason Gunthorpe 	rtnl_lock();
1839567c5e13SPetr Machata 	dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP, NULL);
18407cbee87cSJason Gunthorpe 	rtnl_unlock();
18417cbee87cSJason Gunthorpe 
18427cbee87cSJason Gunthorpe 	/* ipoib_event() cannot be running once this returns */
18437cbee87cSJason Gunthorpe 	ib_unregister_event_handler(&priv->event_handler);
18447cbee87cSJason Gunthorpe 
18457cbee87cSJason Gunthorpe 	/*
18467cbee87cSJason Gunthorpe 	 * Work on the queue grabs the rtnl lock, so this cannot be done while
18477cbee87cSJason Gunthorpe 	 * also holding it.
18487cbee87cSJason Gunthorpe 	 */
18497cbee87cSJason Gunthorpe 	flush_workqueue(ipoib_workqueue);
18507cbee87cSJason Gunthorpe }
18517cbee87cSJason Gunthorpe 
ipoib_set_dev_features(struct ipoib_dev_priv * priv)1852eaeb3984SJason Gunthorpe static void ipoib_set_dev_features(struct ipoib_dev_priv *priv)
1853eaeb3984SJason Gunthorpe {
1854eaeb3984SJason Gunthorpe 	priv->hca_caps = priv->ca->attrs.device_cap_flags;
1855e945c653SJason Gunthorpe 	priv->kernel_caps = priv->ca->attrs.kernel_cap_flags;
1856eaeb3984SJason Gunthorpe 
1857eaeb3984SJason Gunthorpe 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1858eaeb3984SJason Gunthorpe 		priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1859eaeb3984SJason Gunthorpe 
1860e945c653SJason Gunthorpe 		if (priv->kernel_caps & IBK_UD_TSO)
1861eaeb3984SJason Gunthorpe 			priv->dev->hw_features |= NETIF_F_TSO;
1862eaeb3984SJason Gunthorpe 
1863eaeb3984SJason Gunthorpe 		priv->dev->features |= priv->dev->hw_features;
1864eaeb3984SJason Gunthorpe 	}
1865eaeb3984SJason Gunthorpe }
1866eaeb3984SJason Gunthorpe 
ipoib_parent_init(struct net_device * ndev)1867eaeb3984SJason Gunthorpe static int ipoib_parent_init(struct net_device *ndev)
1868eaeb3984SJason Gunthorpe {
1869eaeb3984SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1870eaeb3984SJason Gunthorpe 	struct ib_port_attr attr;
1871eaeb3984SJason Gunthorpe 	int result;
1872eaeb3984SJason Gunthorpe 
1873eaeb3984SJason Gunthorpe 	result = ib_query_port(priv->ca, priv->port, &attr);
1874eaeb3984SJason Gunthorpe 	if (result) {
1875eaeb3984SJason Gunthorpe 		pr_warn("%s: ib_query_port %d failed\n", priv->ca->name,
1876eaeb3984SJason Gunthorpe 			priv->port);
1877eaeb3984SJason Gunthorpe 		return result;
1878eaeb3984SJason Gunthorpe 	}
18796d72344cSKaike Wan 	priv->max_ib_mtu = rdma_mtu_from_attr(priv->ca, priv->port, &attr);
1880eaeb3984SJason Gunthorpe 
1881eaeb3984SJason Gunthorpe 	result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey);
1882eaeb3984SJason Gunthorpe 	if (result) {
1883eaeb3984SJason Gunthorpe 		pr_warn("%s: ib_query_pkey port %d failed (ret = %d)\n",
1884eaeb3984SJason Gunthorpe 			priv->ca->name, priv->port, result);
1885eaeb3984SJason Gunthorpe 		return result;
1886eaeb3984SJason Gunthorpe 	}
1887eaeb3984SJason Gunthorpe 
1888eaeb3984SJason Gunthorpe 	result = rdma_query_gid(priv->ca, priv->port, 0, &priv->local_gid);
1889eaeb3984SJason Gunthorpe 	if (result) {
1890eaeb3984SJason Gunthorpe 		pr_warn("%s: rdma_query_gid port %d failed (ret = %d)\n",
1891eaeb3984SJason Gunthorpe 			priv->ca->name, priv->port, result);
1892eaeb3984SJason Gunthorpe 		return result;
1893eaeb3984SJason Gunthorpe 	}
189410f7b9bcSJakub Kicinski 	dev_addr_mod(priv->dev, 4, priv->local_gid.raw, sizeof(union ib_gid));
1895eaeb3984SJason Gunthorpe 
1896eaeb3984SJason Gunthorpe 	SET_NETDEV_DEV(priv->dev, priv->ca->dev.parent);
18979b8b2a32SArseny Maslennikov 	priv->dev->dev_port = priv->port - 1;
18989b8b2a32SArseny Maslennikov 	/* Let's set this one too for backwards compatibility. */
1899eaeb3984SJason Gunthorpe 	priv->dev->dev_id = priv->port - 1;
1900eaeb3984SJason Gunthorpe 
1901eaeb3984SJason Gunthorpe 	return 0;
1902eaeb3984SJason Gunthorpe }
1903eaeb3984SJason Gunthorpe 
ipoib_child_init(struct net_device * ndev)1904eaeb3984SJason Gunthorpe static void ipoib_child_init(struct net_device *ndev)
1905eaeb3984SJason Gunthorpe {
1906eaeb3984SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1907eaeb3984SJason Gunthorpe 	struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
1908eaeb3984SJason Gunthorpe 
1909eaeb3984SJason Gunthorpe 	priv->max_ib_mtu = ppriv->max_ib_mtu;
1910eaeb3984SJason Gunthorpe 	set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
191187fb5c1cSMichael Guralnik 	if (memchr_inv(priv->dev->dev_addr, 0, INFINIBAND_ALEN))
191287fb5c1cSMichael Guralnik 		memcpy(&priv->local_gid, priv->dev->dev_addr + 4,
191387fb5c1cSMichael Guralnik 		       sizeof(priv->local_gid));
191487fb5c1cSMichael Guralnik 	else {
191510f7b9bcSJakub Kicinski 		__dev_addr_set(priv->dev, ppriv->dev->dev_addr,
191687fb5c1cSMichael Guralnik 			       INFINIBAND_ALEN);
191787fb5c1cSMichael Guralnik 		memcpy(&priv->local_gid, &ppriv->local_gid,
191887fb5c1cSMichael Guralnik 		       sizeof(priv->local_gid));
191987fb5c1cSMichael Guralnik 	}
1920eaeb3984SJason Gunthorpe }
1921eaeb3984SJason Gunthorpe 
ipoib_ndo_init(struct net_device * ndev)1922eaeb3984SJason Gunthorpe static int ipoib_ndo_init(struct net_device *ndev)
1923eaeb3984SJason Gunthorpe {
1924eaeb3984SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1925eaeb3984SJason Gunthorpe 	int rc;
1926b7e159ebSGary Leshner 	struct rdma_netdev *rn = netdev_priv(ndev);
1927eaeb3984SJason Gunthorpe 
1928eaeb3984SJason Gunthorpe 	if (priv->parent) {
1929eaeb3984SJason Gunthorpe 		ipoib_child_init(ndev);
1930eaeb3984SJason Gunthorpe 	} else {
1931eaeb3984SJason Gunthorpe 		rc = ipoib_parent_init(ndev);
1932eaeb3984SJason Gunthorpe 		if (rc)
1933eaeb3984SJason Gunthorpe 			return rc;
1934eaeb3984SJason Gunthorpe 	}
1935eaeb3984SJason Gunthorpe 
1936eaeb3984SJason Gunthorpe 	/* MTU will be reset when mcast join happens */
1937eaeb3984SJason Gunthorpe 	ndev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1938eaeb3984SJason Gunthorpe 	priv->mcast_mtu = priv->admin_mtu = ndev->mtu;
1939b7e159ebSGary Leshner 	rn->mtu = priv->mcast_mtu;
1940eaeb3984SJason Gunthorpe 	ndev->max_mtu = IPOIB_CM_MTU;
1941eaeb3984SJason Gunthorpe 
1942eaeb3984SJason Gunthorpe 	ndev->neigh_priv_len = sizeof(struct ipoib_neigh);
1943eaeb3984SJason Gunthorpe 
1944eaeb3984SJason Gunthorpe 	/*
1945eaeb3984SJason Gunthorpe 	 * Set the full membership bit, so that we join the right
1946eaeb3984SJason Gunthorpe 	 * broadcast group, etc.
1947eaeb3984SJason Gunthorpe 	 */
1948eaeb3984SJason Gunthorpe 	priv->pkey |= 0x8000;
1949eaeb3984SJason Gunthorpe 
1950eaeb3984SJason Gunthorpe 	ndev->broadcast[8] = priv->pkey >> 8;
1951eaeb3984SJason Gunthorpe 	ndev->broadcast[9] = priv->pkey & 0xff;
1952eaeb3984SJason Gunthorpe 	set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
1953eaeb3984SJason Gunthorpe 
1954eaeb3984SJason Gunthorpe 	ipoib_set_dev_features(priv);
1955eaeb3984SJason Gunthorpe 
1956eaeb3984SJason Gunthorpe 	rc = ipoib_dev_init(ndev);
1957eaeb3984SJason Gunthorpe 	if (rc) {
1958eaeb3984SJason Gunthorpe 		pr_warn("%s: failed to initialize device: %s port %d (ret = %d)\n",
1959eaeb3984SJason Gunthorpe 			priv->ca->name, priv->dev->name, priv->port, rc);
196091b01061SValentine Fatiev 		return rc;
196191b01061SValentine Fatiev 	}
196291b01061SValentine Fatiev 
196391b01061SValentine Fatiev 	if (priv->parent) {
196491b01061SValentine Fatiev 		struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
196591b01061SValentine Fatiev 
196691b01061SValentine Fatiev 		dev_hold(priv->parent);
196791b01061SValentine Fatiev 
196891b01061SValentine Fatiev 		down_write(&ppriv->vlan_rwsem);
196991b01061SValentine Fatiev 		list_add_tail(&priv->list, &ppriv->child_intfs);
197091b01061SValentine Fatiev 		up_write(&ppriv->vlan_rwsem);
1971eaeb3984SJason Gunthorpe 	}
1972eaeb3984SJason Gunthorpe 
1973eaeb3984SJason Gunthorpe 	return 0;
1974eaeb3984SJason Gunthorpe }
1975eaeb3984SJason Gunthorpe 
ipoib_ndo_uninit(struct net_device * dev)19767cbee87cSJason Gunthorpe static void ipoib_ndo_uninit(struct net_device *dev)
19771da177e4SLinus Torvalds {
197825405d98SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
19799baa0b03SOr Gerlitz 
19809baa0b03SOr Gerlitz 	ASSERT_RTNL();
19811da177e4SLinus Torvalds 
198225405d98SJason Gunthorpe 	/*
198325405d98SJason Gunthorpe 	 * ipoib_remove_one guarantees the children are removed before the
198425405d98SJason Gunthorpe 	 * parent, and that is the only place where a parent can be removed.
198525405d98SJason Gunthorpe 	 */
198625405d98SJason Gunthorpe 	WARN_ON(!list_empty(&priv->child_intfs));
19871da177e4SLinus Torvalds 
198891b01061SValentine Fatiev 	if (priv->parent) {
198991b01061SValentine Fatiev 		struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
199091b01061SValentine Fatiev 
199191b01061SValentine Fatiev 		down_write(&ppriv->vlan_rwsem);
199291b01061SValentine Fatiev 		list_del(&priv->list);
199391b01061SValentine Fatiev 		up_write(&ppriv->vlan_rwsem);
199491b01061SValentine Fatiev 	}
199591b01061SValentine Fatiev 
1996be7aa663SDoug Ledford 	ipoib_neigh_hash_uninit(dev);
1997be7aa663SDoug Ledford 
19981da177e4SLinus Torvalds 	ipoib_ib_dev_cleanup(dev);
19991da177e4SLinus Torvalds 
2000515ed4f3SErez Shitrit 	/* no more works over the priv->wq */
2001515ed4f3SErez Shitrit 	if (priv->wq) {
200265936bf2SJason Gunthorpe 		/* See ipoib_mcast_carrier_on_task() */
200365936bf2SJason Gunthorpe 		WARN_ON(test_bit(IPOIB_FLAG_OPER_UP, &priv->flags));
2004515ed4f3SErez Shitrit 		destroy_workqueue(priv->wq);
2005515ed4f3SErez Shitrit 		priv->wq = NULL;
2006515ed4f3SErez Shitrit 	}
200713476d35SJason Gunthorpe 
200891b01061SValentine Fatiev 	if (priv->parent)
200913476d35SJason Gunthorpe 		dev_put(priv->parent);
201013476d35SJason Gunthorpe }
20111da177e4SLinus Torvalds 
ipoib_set_vf_link_state(struct net_device * dev,int vf,int link_state)20129c3c5f8eSEli Cohen static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
20139c3c5f8eSEli Cohen {
2014c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
20159c3c5f8eSEli Cohen 
20169c3c5f8eSEli Cohen 	return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
20179c3c5f8eSEli Cohen }
20189c3c5f8eSEli Cohen 
ipoib_get_vf_config(struct net_device * dev,int vf,struct ifla_vf_info * ivf)20199c3c5f8eSEli Cohen static int ipoib_get_vf_config(struct net_device *dev, int vf,
20209c3c5f8eSEli Cohen 			       struct ifla_vf_info *ivf)
20219c3c5f8eSEli Cohen {
2022c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
20239c3c5f8eSEli Cohen 	int err;
20249c3c5f8eSEli Cohen 
20259c3c5f8eSEli Cohen 	err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
20269c3c5f8eSEli Cohen 	if (err)
20279c3c5f8eSEli Cohen 		return err;
20289c3c5f8eSEli Cohen 
20299c3c5f8eSEli Cohen 	ivf->vf = vf;
203064d701c6SDenis Kirjanov 	memcpy(ivf->mac, dev->dev_addr, dev->addr_len);
20319c3c5f8eSEli Cohen 
20329c3c5f8eSEli Cohen 	return 0;
20339c3c5f8eSEli Cohen }
20349c3c5f8eSEli Cohen 
ipoib_set_vf_guid(struct net_device * dev,int vf,u64 guid,int type)20359c3c5f8eSEli Cohen static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
20369c3c5f8eSEli Cohen {
2037c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
20389c3c5f8eSEli Cohen 
20399c3c5f8eSEli Cohen 	if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
20409c3c5f8eSEli Cohen 		return -EINVAL;
20419c3c5f8eSEli Cohen 
20429c3c5f8eSEli Cohen 	return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
20439c3c5f8eSEli Cohen }
20449c3c5f8eSEli Cohen 
ipoib_get_vf_guid(struct net_device * dev,int vf,struct ifla_vf_guid * node_guid,struct ifla_vf_guid * port_guid)20452446887eSDanit Goldberg static int ipoib_get_vf_guid(struct net_device *dev, int vf,
20462446887eSDanit Goldberg 			     struct ifla_vf_guid *node_guid,
20472446887eSDanit Goldberg 			     struct ifla_vf_guid *port_guid)
20482446887eSDanit Goldberg {
20492446887eSDanit Goldberg 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
20502446887eSDanit Goldberg 
20512446887eSDanit Goldberg 	return ib_get_vf_guid(priv->ca, vf, priv->port, node_guid, port_guid);
20522446887eSDanit Goldberg }
20532446887eSDanit Goldberg 
ipoib_get_vf_stats(struct net_device * dev,int vf,struct ifla_vf_stats * vf_stats)20549c3c5f8eSEli Cohen static int ipoib_get_vf_stats(struct net_device *dev, int vf,
20559c3c5f8eSEli Cohen 			      struct ifla_vf_stats *vf_stats)
20569c3c5f8eSEli Cohen {
2057c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
20589c3c5f8eSEli Cohen 
20599c3c5f8eSEli Cohen 	return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
20609c3c5f8eSEli Cohen }
20619c3c5f8eSEli Cohen 
20623b04dddeSStephen Hemminger static const struct header_ops ipoib_header_ops = {
20633b04dddeSStephen Hemminger 	.create	= ipoib_hard_header,
20643b04dddeSStephen Hemminger };
20653b04dddeSStephen Hemminger 
20669c3c5f8eSEli Cohen static const struct net_device_ops ipoib_netdev_ops_pf = {
2067eaeb3984SJason Gunthorpe 	.ndo_init		 = ipoib_ndo_init,
20687cbee87cSJason Gunthorpe 	.ndo_uninit		 = ipoib_ndo_uninit,
20699c3c5f8eSEli Cohen 	.ndo_open		 = ipoib_open,
20709c3c5f8eSEli Cohen 	.ndo_stop		 = ipoib_stop,
20719c3c5f8eSEli Cohen 	.ndo_change_mtu		 = ipoib_change_mtu,
20729c3c5f8eSEli Cohen 	.ndo_fix_features	 = ipoib_fix_features,
20739c3c5f8eSEli Cohen 	.ndo_start_xmit		 = ipoib_start_xmit,
20749c3c5f8eSEli Cohen 	.ndo_tx_timeout		 = ipoib_timeout,
20759c3c5f8eSEli Cohen 	.ndo_set_rx_mode	 = ipoib_set_mcast_list,
20769c3c5f8eSEli Cohen 	.ndo_get_iflink		 = ipoib_get_iflink,
20779c3c5f8eSEli Cohen 	.ndo_set_vf_link_state	 = ipoib_set_vf_link_state,
20789c3c5f8eSEli Cohen 	.ndo_get_vf_config	 = ipoib_get_vf_config,
20799c3c5f8eSEli Cohen 	.ndo_get_vf_stats	 = ipoib_get_vf_stats,
20802446887eSDanit Goldberg 	.ndo_get_vf_guid	 = ipoib_get_vf_guid,
20819c3c5f8eSEli Cohen 	.ndo_set_vf_guid	 = ipoib_set_vf_guid,
2082492a7e67SMark Bloch 	.ndo_set_mac_address	 = ipoib_set_mac,
2083b6c871e5SErez Shitrit 	.ndo_get_stats64	 = ipoib_get_stats,
2084a7605370SArnd Bergmann 	.ndo_eth_ioctl		 = ipoib_ioctl,
20859c3c5f8eSEli Cohen };
20869c3c5f8eSEli Cohen 
20879c3c5f8eSEli Cohen static const struct net_device_ops ipoib_netdev_ops_vf = {
2088eaeb3984SJason Gunthorpe 	.ndo_init		 = ipoib_ndo_init,
20897cbee87cSJason Gunthorpe 	.ndo_uninit		 = ipoib_ndo_uninit,
2090fe8114e8SStephen Hemminger 	.ndo_open		 = ipoib_open,
2091fe8114e8SStephen Hemminger 	.ndo_stop		 = ipoib_stop,
2092fe8114e8SStephen Hemminger 	.ndo_change_mtu		 = ipoib_change_mtu,
20933d96c74dSMichał Mirosław 	.ndo_fix_features	 = ipoib_fix_features,
2094fe8114e8SStephen Hemminger 	.ndo_start_xmit	 	 = ipoib_start_xmit,
2095fe8114e8SStephen Hemminger 	.ndo_tx_timeout		 = ipoib_timeout,
2096afc4b13dSJiri Pirko 	.ndo_set_rx_mode	 = ipoib_set_mcast_list,
20975aa7add8SNicolas Dichtel 	.ndo_get_iflink		 = ipoib_get_iflink,
2098eb54714dSFeras Daoud 	.ndo_get_stats64	 = ipoib_get_stats,
2099a7605370SArnd Bergmann 	.ndo_eth_ioctl		 = ipoib_ioctl,
2100fe8114e8SStephen Hemminger };
2101fe8114e8SStephen Hemminger 
21028f149b68SGary Leshner static const struct net_device_ops ipoib_netdev_default_pf = {
21038f149b68SGary Leshner 	.ndo_init		 = ipoib_dev_init_default,
21048f149b68SGary Leshner 	.ndo_uninit		 = ipoib_dev_uninit_default,
21058f149b68SGary Leshner 	.ndo_open		 = ipoib_ib_dev_open_default,
21068f149b68SGary Leshner 	.ndo_stop		 = ipoib_ib_dev_stop_default,
21078f149b68SGary Leshner };
21088f149b68SGary Leshner 
ipoib_setup_common(struct net_device * dev)2109cd565b4bSErez Shitrit void ipoib_setup_common(struct net_device *dev)
21101da177e4SLinus Torvalds {
21113b04dddeSStephen Hemminger 	dev->header_ops		 = &ipoib_header_ops;
21128f149b68SGary Leshner 	dev->netdev_ops          = &ipoib_netdev_default_pf;
2113bea3348eSStephen Hemminger 
211482c24c18SEli Cohen 	ipoib_set_ethtool_ops(dev);
211582c24c18SEli Cohen 
21161da177e4SLinus Torvalds 	dev->watchdog_timeo	 = HZ;
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	dev->flags		|= IFF_BROADCAST | IFF_MULTICAST;
21191da177e4SLinus Torvalds 
2120fc791b63SPaolo Abeni 	dev->hard_header_len	 = IPOIB_HARD_LEN;
21211da177e4SLinus Torvalds 	dev->addr_len		 = INFINIBAND_ALEN;
21221da177e4SLinus Torvalds 	dev->type		 = ARPHRD_INFINIBAND;
21230f485251SShirley Ma 	dev->tx_queue_len	 = ipoib_sendq_size * 2;
2124eb14032fSEli Cohen 	dev->features		 = (NETIF_F_VLAN_CHALLENGED	|
2125eb14032fSEli Cohen 				    NETIF_F_HIGHDMA);
212602875878SEric Dumazet 	netif_keep_dst(dev);
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds 	memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
21299f49a5b5SJason Gunthorpe 
21309f49a5b5SJason Gunthorpe 	/*
21319f49a5b5SJason Gunthorpe 	 * unregister_netdev always frees the netdev, we use this mode
21329f49a5b5SJason Gunthorpe 	 * consistently to unify all the various unregister paths, including
21339f49a5b5SJason Gunthorpe 	 * those connected to rtnl_link_ops which require it.
21349f49a5b5SJason Gunthorpe 	 */
21359f49a5b5SJason Gunthorpe 	dev->needs_free_netdev = true;
2136cd565b4bSErez Shitrit }
2137cd565b4bSErez Shitrit 
ipoib_build_priv(struct net_device * dev)2138cd565b4bSErez Shitrit static void ipoib_build_priv(struct net_device *dev)
2139cd565b4bSErez Shitrit {
2140cd565b4bSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds 	priv->dev = dev;
21431da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
2144f47944ccSErez Shitrit 	init_rwsem(&priv->vlan_rwsem);
2145edf3f301SFeras Daoud 	mutex_init(&priv->mcast_mutex);
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->path_list);
21481da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->child_intfs);
21491da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->dead_ahs);
21501da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->multicast_list);
21511da177e4SLinus Torvalds 
2152c4028958SDavid Howells 	INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
2153e8224e4bSYossi Etigin 	INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
2154ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
2155ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
2156ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
2157c4028958SDavid Howells 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
2158c4028958SDavid Howells 	INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
2159b63b70d8SShlomo Pongratz 	INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
21601da177e4SLinus Torvalds }
21611da177e4SLinus Torvalds 
ipoib_alloc_netdev(struct ib_device * hca,u32 port,const char * name)21621fb7f897SMark Bloch static struct net_device *ipoib_alloc_netdev(struct ib_device *hca, u32 port,
2163cd565b4bSErez Shitrit 					     const char *name)
2164cd565b4bSErez Shitrit {
2165cd565b4bSErez Shitrit 	struct net_device *dev;
2166cd565b4bSErez Shitrit 
2167f6a8a19bSDenis Drozdov 	dev = rdma_alloc_netdev(hca, port, RDMA_NETDEV_IPOIB, name,
2168f6a8a19bSDenis Drozdov 				NET_NAME_UNKNOWN, ipoib_setup_common);
21695d6b0cb3SDenis Drozdov 	if (!IS_ERR(dev) || PTR_ERR(dev) != -EOPNOTSUPP)
2170cd565b4bSErez Shitrit 		return dev;
2171f6a8a19bSDenis Drozdov 
21725d6b0cb3SDenis Drozdov 	dev = alloc_netdev(sizeof(struct rdma_netdev), name, NET_NAME_UNKNOWN,
2173f6a8a19bSDenis Drozdov 			   ipoib_setup_common);
21745d6b0cb3SDenis Drozdov 	if (!dev)
21755d6b0cb3SDenis Drozdov 		return ERR_PTR(-ENOMEM);
21765d6b0cb3SDenis Drozdov 	return dev;
2177cd565b4bSErez Shitrit }
2178cd565b4bSErez Shitrit 
ipoib_intf_init(struct ib_device * hca,u32 port,const char * name,struct net_device * dev)21791fb7f897SMark Bloch int ipoib_intf_init(struct ib_device *hca, u32 port, const char *name,
21805d6b0cb3SDenis Drozdov 		    struct net_device *dev)
2181cd565b4bSErez Shitrit {
21825d6b0cb3SDenis Drozdov 	struct rdma_netdev *rn = netdev_priv(dev);
2183cd565b4bSErez Shitrit 	struct ipoib_dev_priv *priv;
21845d6b0cb3SDenis Drozdov 	int rc;
2185cd565b4bSErez Shitrit 
2186cd565b4bSErez Shitrit 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2187cd565b4bSErez Shitrit 	if (!priv)
21885d6b0cb3SDenis Drozdov 		return -ENOMEM;
2189cd565b4bSErez Shitrit 
2190eaeb3984SJason Gunthorpe 	priv->ca = hca;
2191eaeb3984SJason Gunthorpe 	priv->port = port;
2192eaeb3984SJason Gunthorpe 
21935d6b0cb3SDenis Drozdov 	rc = rdma_init_netdev(hca, port, RDMA_NETDEV_IPOIB, name,
21945d6b0cb3SDenis Drozdov 			      NET_NAME_UNKNOWN, ipoib_setup_common, dev);
21955d6b0cb3SDenis Drozdov 	if (rc) {
21965d6b0cb3SDenis Drozdov 		if (rc != -EOPNOTSUPP)
21975d6b0cb3SDenis Drozdov 			goto out;
21985d6b0cb3SDenis Drozdov 
21995d6b0cb3SDenis Drozdov 		rn->send = ipoib_send;
22005d6b0cb3SDenis Drozdov 		rn->attach_mcast = ipoib_mcast_attach;
22015d6b0cb3SDenis Drozdov 		rn->detach_mcast = ipoib_mcast_detach;
22025d6b0cb3SDenis Drozdov 		rn->hca = hca;
2203*e632291aSDragos Tatulea 
2204*e632291aSDragos Tatulea 		rc = netif_set_real_num_tx_queues(dev, 1);
2205*e632291aSDragos Tatulea 		if (rc)
2206*e632291aSDragos Tatulea 			goto out;
2207*e632291aSDragos Tatulea 
2208*e632291aSDragos Tatulea 		rc = netif_set_real_num_rx_queues(dev, 1);
2209*e632291aSDragos Tatulea 		if (rc)
2210*e632291aSDragos Tatulea 			goto out;
22115d6b0cb3SDenis Drozdov 	}
2212cd565b4bSErez Shitrit 
2213cd565b4bSErez Shitrit 	priv->rn_ops = dev->netdev_ops;
2214cd565b4bSErez Shitrit 
2215e945c653SJason Gunthorpe 	if (hca->attrs.kernel_cap_flags & IBK_VIRTUAL_FUNCTION)
2216cd565b4bSErez Shitrit 		dev->netdev_ops	= &ipoib_netdev_ops_vf;
2217cd565b4bSErez Shitrit 	else
2218cd565b4bSErez Shitrit 		dev->netdev_ops	= &ipoib_netdev_ops_pf;
2219cd565b4bSErez Shitrit 
2220cd565b4bSErez Shitrit 	rn->clnt_priv = priv;
22219f49a5b5SJason Gunthorpe 	/*
22229f49a5b5SJason Gunthorpe 	 * Only the child register_netdev flows can handle priv_destructor
22239f49a5b5SJason Gunthorpe 	 * being set, so we force it to NULL here and handle manually until it
22249f49a5b5SJason Gunthorpe 	 * is safe to turn on.
22259f49a5b5SJason Gunthorpe 	 */
22269f49a5b5SJason Gunthorpe 	priv->next_priv_destructor = dev->priv_destructor;
22279f49a5b5SJason Gunthorpe 	dev->priv_destructor = NULL;
22289f49a5b5SJason Gunthorpe 
2229cd565b4bSErez Shitrit 	ipoib_build_priv(dev);
2230cd565b4bSErez Shitrit 
22315d6b0cb3SDenis Drozdov 	return 0;
22325d6b0cb3SDenis Drozdov 
22335d6b0cb3SDenis Drozdov out:
2234cd565b4bSErez Shitrit 	kfree(priv);
22355d6b0cb3SDenis Drozdov 	return rc;
22365d6b0cb3SDenis Drozdov }
22375d6b0cb3SDenis Drozdov 
ipoib_intf_alloc(struct ib_device * hca,u32 port,const char * name)22381fb7f897SMark Bloch struct net_device *ipoib_intf_alloc(struct ib_device *hca, u32 port,
22395d6b0cb3SDenis Drozdov 				    const char *name)
22405d6b0cb3SDenis Drozdov {
22415d6b0cb3SDenis Drozdov 	struct net_device *dev;
22425d6b0cb3SDenis Drozdov 	int rc;
22435d6b0cb3SDenis Drozdov 
22445d6b0cb3SDenis Drozdov 	dev = ipoib_alloc_netdev(hca, port, name);
22455d6b0cb3SDenis Drozdov 	if (IS_ERR(dev))
22465d6b0cb3SDenis Drozdov 		return dev;
22475d6b0cb3SDenis Drozdov 
22485d6b0cb3SDenis Drozdov 	rc = ipoib_intf_init(hca, port, name, dev);
22495d6b0cb3SDenis Drozdov 	if (rc) {
22505d6b0cb3SDenis Drozdov 		free_netdev(dev);
22515d6b0cb3SDenis Drozdov 		return ERR_PTR(rc);
22525d6b0cb3SDenis Drozdov 	}
22535d6b0cb3SDenis Drozdov 
22545d6b0cb3SDenis Drozdov 	/*
22555d6b0cb3SDenis Drozdov 	 * Upon success the caller must ensure ipoib_intf_free is called or
22565d6b0cb3SDenis Drozdov 	 * register_netdevice succeed'd and priv_destructor is set to
22575d6b0cb3SDenis Drozdov 	 * ipoib_intf_free.
22585d6b0cb3SDenis Drozdov 	 */
22595d6b0cb3SDenis Drozdov 	return dev;
22601da177e4SLinus Torvalds }
22611da177e4SLinus Torvalds 
ipoib_intf_free(struct net_device * dev)22629f49a5b5SJason Gunthorpe void ipoib_intf_free(struct net_device *dev)
22639f49a5b5SJason Gunthorpe {
22649f49a5b5SJason Gunthorpe 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
22659f49a5b5SJason Gunthorpe 	struct rdma_netdev *rn = netdev_priv(dev);
22669f49a5b5SJason Gunthorpe 
22679f49a5b5SJason Gunthorpe 	dev->priv_destructor = priv->next_priv_destructor;
22689f49a5b5SJason Gunthorpe 	if (dev->priv_destructor)
22699f49a5b5SJason Gunthorpe 		dev->priv_destructor(dev);
22709f49a5b5SJason Gunthorpe 
22719f49a5b5SJason Gunthorpe 	/*
22729f49a5b5SJason Gunthorpe 	 * There are some error flows around register_netdev failing that may
22739f49a5b5SJason Gunthorpe 	 * attempt to call priv_destructor twice, prevent that from happening.
22749f49a5b5SJason Gunthorpe 	 */
22759f49a5b5SJason Gunthorpe 	dev->priv_destructor = NULL;
22769f49a5b5SJason Gunthorpe 
22779f49a5b5SJason Gunthorpe 	/* unregister/destroy is very complicated. Make bugs more obvious. */
22789f49a5b5SJason Gunthorpe 	rn->clnt_priv = NULL;
22799f49a5b5SJason Gunthorpe 
22809f49a5b5SJason Gunthorpe 	kfree(priv);
22819f49a5b5SJason Gunthorpe }
22829f49a5b5SJason Gunthorpe 
pkey_show(struct device * dev,struct device_attribute * attr,char * buf)22831f8f60f3SYueHaibing static ssize_t pkey_show(struct device *dev, struct device_attribute *attr,
22841f8f60f3SYueHaibing 			 char *buf)
22851da177e4SLinus Torvalds {
2286c1048affSErez Shitrit 	struct net_device *ndev = to_net_dev(dev);
2287c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
22881da177e4SLinus Torvalds 
22891c7fd726SJoe Perches 	return sysfs_emit(buf, "0x%04x\n", priv->pkey);
22901da177e4SLinus Torvalds }
22911f8f60f3SYueHaibing static DEVICE_ATTR_RO(pkey);
22921da177e4SLinus Torvalds 
umcast_show(struct device * dev,struct device_attribute * attr,char * buf)22931f8f60f3SYueHaibing static ssize_t umcast_show(struct device *dev, struct device_attribute *attr,
22941f8f60f3SYueHaibing 			   char *buf)
2295335a64a5SOr Gerlitz {
2296c1048affSErez Shitrit 	struct net_device *ndev = to_net_dev(dev);
2297c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2298335a64a5SOr Gerlitz 
22991c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n",
23001c7fd726SJoe Perches 			  test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
2301335a64a5SOr Gerlitz }
2302335a64a5SOr Gerlitz 
ipoib_set_umcast(struct net_device * ndev,int umcast_val)2303862096a8SOr Gerlitz void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
2304335a64a5SOr Gerlitz {
2305c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2306335a64a5SOr Gerlitz 
2307335a64a5SOr Gerlitz 	if (umcast_val > 0) {
2308335a64a5SOr Gerlitz 		set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2309335a64a5SOr Gerlitz 		ipoib_warn(priv, "ignoring multicast groups joined directly "
2310335a64a5SOr Gerlitz 				"by userspace\n");
2311335a64a5SOr Gerlitz 	} else
2312335a64a5SOr Gerlitz 		clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2313862096a8SOr Gerlitz }
2314862096a8SOr Gerlitz 
umcast_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)23151f8f60f3SYueHaibing static ssize_t umcast_store(struct device *dev, struct device_attribute *attr,
2316862096a8SOr Gerlitz 			    const char *buf, size_t count)
2317862096a8SOr Gerlitz {
2318862096a8SOr Gerlitz 	unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
2319862096a8SOr Gerlitz 
2320862096a8SOr Gerlitz 	ipoib_set_umcast(to_net_dev(dev), umcast_val);
2321335a64a5SOr Gerlitz 
2322335a64a5SOr Gerlitz 	return count;
2323335a64a5SOr Gerlitz }
23241f8f60f3SYueHaibing static DEVICE_ATTR_RW(umcast);
2325335a64a5SOr Gerlitz 
ipoib_add_umcast_attr(struct net_device * dev)2326335a64a5SOr Gerlitz int ipoib_add_umcast_attr(struct net_device *dev)
2327335a64a5SOr Gerlitz {
2328335a64a5SOr Gerlitz 	return device_create_file(&dev->dev, &dev_attr_umcast);
2329335a64a5SOr Gerlitz }
2330335a64a5SOr Gerlitz 
set_base_guid(struct ipoib_dev_priv * priv,union ib_gid * gid)2331492a7e67SMark Bloch static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
2332492a7e67SMark Bloch {
2333492a7e67SMark Bloch 	struct ipoib_dev_priv *child_priv;
2334492a7e67SMark Bloch 	struct net_device *netdev = priv->dev;
2335492a7e67SMark Bloch 
23369b29953bSMark Bloch 	netif_addr_lock_bh(netdev);
2337492a7e67SMark Bloch 
2338492a7e67SMark Bloch 	memcpy(&priv->local_gid.global.interface_id,
2339492a7e67SMark Bloch 	       &gid->global.interface_id,
2340492a7e67SMark Bloch 	       sizeof(gid->global.interface_id));
234110f7b9bcSJakub Kicinski 	dev_addr_mod(netdev, 4, (u8 *)&priv->local_gid, sizeof(priv->local_gid));
2342492a7e67SMark Bloch 	clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2343492a7e67SMark Bloch 
23449b29953bSMark Bloch 	netif_addr_unlock_bh(netdev);
2345492a7e67SMark Bloch 
2346492a7e67SMark Bloch 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
2347492a7e67SMark Bloch 		down_read(&priv->vlan_rwsem);
2348492a7e67SMark Bloch 		list_for_each_entry(child_priv, &priv->child_intfs, list)
2349492a7e67SMark Bloch 			set_base_guid(child_priv, gid);
2350492a7e67SMark Bloch 		up_read(&priv->vlan_rwsem);
2351492a7e67SMark Bloch 	}
2352492a7e67SMark Bloch }
2353492a7e67SMark Bloch 
ipoib_check_lladdr(struct net_device * dev,struct sockaddr_storage * ss)2354492a7e67SMark Bloch static int ipoib_check_lladdr(struct net_device *dev,
2355492a7e67SMark Bloch 			      struct sockaddr_storage *ss)
2356492a7e67SMark Bloch {
2357492a7e67SMark Bloch 	union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
2358492a7e67SMark Bloch 	int ret = 0;
2359492a7e67SMark Bloch 
23609b29953bSMark Bloch 	netif_addr_lock_bh(dev);
2361492a7e67SMark Bloch 
2362492a7e67SMark Bloch 	/* Make sure the QPN, reserved and subnet prefix match the current
2363492a7e67SMark Bloch 	 * lladdr, it also makes sure the lladdr is unicast.
2364492a7e67SMark Bloch 	 */
2365492a7e67SMark Bloch 	if (memcmp(dev->dev_addr, ss->__data,
2366492a7e67SMark Bloch 		   4 + sizeof(gid->global.subnet_prefix)) ||
2367492a7e67SMark Bloch 	    gid->global.interface_id == 0)
2368492a7e67SMark Bloch 		ret = -EINVAL;
2369492a7e67SMark Bloch 
23709b29953bSMark Bloch 	netif_addr_unlock_bh(dev);
2371492a7e67SMark Bloch 
2372492a7e67SMark Bloch 	return ret;
2373492a7e67SMark Bloch }
2374492a7e67SMark Bloch 
ipoib_set_mac(struct net_device * dev,void * addr)2375492a7e67SMark Bloch static int ipoib_set_mac(struct net_device *dev, void *addr)
2376492a7e67SMark Bloch {
2377c1048affSErez Shitrit 	struct ipoib_dev_priv *priv = ipoib_priv(dev);
2378492a7e67SMark Bloch 	struct sockaddr_storage *ss = addr;
2379492a7e67SMark Bloch 	int ret;
2380492a7e67SMark Bloch 
2381492a7e67SMark Bloch 	if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
2382492a7e67SMark Bloch 		return -EBUSY;
2383492a7e67SMark Bloch 
2384492a7e67SMark Bloch 	ret = ipoib_check_lladdr(dev, ss);
2385492a7e67SMark Bloch 	if (ret)
2386492a7e67SMark Bloch 		return ret;
2387492a7e67SMark Bloch 
2388492a7e67SMark Bloch 	set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
2389492a7e67SMark Bloch 
2390492a7e67SMark Bloch 	queue_work(ipoib_workqueue, &priv->flush_light);
2391492a7e67SMark Bloch 
2392492a7e67SMark Bloch 	return 0;
2393492a7e67SMark Bloch }
2394492a7e67SMark Bloch 
create_child_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)23951f8f60f3SYueHaibing static ssize_t create_child_store(struct device *dev,
239643cb76d9SGreg Kroah-Hartman 				  struct device_attribute *attr,
23971da177e4SLinus Torvalds 				  const char *buf, size_t count)
23981da177e4SLinus Torvalds {
23991da177e4SLinus Torvalds 	int pkey;
24001da177e4SLinus Torvalds 	int ret;
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
24031da177e4SLinus Torvalds 		return -EINVAL;
24041da177e4SLinus Torvalds 
24053d790a4cSOr Gerlitz 	if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
24061da177e4SLinus Torvalds 		return -EINVAL;
24071da177e4SLinus Torvalds 
240843cb76d9SGreg Kroah-Hartman 	ret = ipoib_vlan_add(to_net_dev(dev), pkey);
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 	return ret ? ret : count;
24111da177e4SLinus Torvalds }
24121f8f60f3SYueHaibing static DEVICE_ATTR_WO(create_child);
24131da177e4SLinus Torvalds 
delete_child_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)24141f8f60f3SYueHaibing static ssize_t delete_child_store(struct device *dev,
241543cb76d9SGreg Kroah-Hartman 				  struct device_attribute *attr,
24161da177e4SLinus Torvalds 				  const char *buf, size_t count)
24171da177e4SLinus Torvalds {
24181da177e4SLinus Torvalds 	int pkey;
24191da177e4SLinus Torvalds 	int ret;
24201da177e4SLinus Torvalds 
24211da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
24221da177e4SLinus Torvalds 		return -EINVAL;
24231da177e4SLinus Torvalds 
24241da177e4SLinus Torvalds 	if (pkey < 0 || pkey > 0xffff)
24251da177e4SLinus Torvalds 		return -EINVAL;
24261da177e4SLinus Torvalds 
242743cb76d9SGreg Kroah-Hartman 	ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds 	return ret ? ret : count;
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds }
24321f8f60f3SYueHaibing static DEVICE_ATTR_WO(delete_child);
24331da177e4SLinus Torvalds 
ipoib_add_pkey_attr(struct net_device * dev)24341da177e4SLinus Torvalds int ipoib_add_pkey_attr(struct net_device *dev)
24351da177e4SLinus Torvalds {
243643cb76d9SGreg Kroah-Hartman 	return device_create_file(&dev->dev, &dev_attr_pkey);
24371da177e4SLinus Torvalds }
24381da177e4SLinus Torvalds 
2439f6350da4SArseny Maslennikov /*
2440f6350da4SArseny Maslennikov  * We erroneously exposed the iface's port number in the dev_id
2441f6350da4SArseny Maslennikov  * sysfs field long after dev_port was introduced for that purpose[1],
2442f6350da4SArseny Maslennikov  * and we need to stop everyone from relying on that.
2443f6350da4SArseny Maslennikov  * Let's overload the shower routine for the dev_id file here
2444f6350da4SArseny Maslennikov  * to gently bring the issue up.
2445f6350da4SArseny Maslennikov  *
2446f6350da4SArseny Maslennikov  * [1] https://www.spinics.net/lists/netdev/msg272123.html
2447f6350da4SArseny Maslennikov  */
dev_id_show(struct device * dev,struct device_attribute * attr,char * buf)2448f6350da4SArseny Maslennikov static ssize_t dev_id_show(struct device *dev,
2449f6350da4SArseny Maslennikov 			   struct device_attribute *attr, char *buf)
2450f6350da4SArseny Maslennikov {
2451f6350da4SArseny Maslennikov 	struct net_device *ndev = to_net_dev(dev);
2452f6350da4SArseny Maslennikov 
2453b79656edSLeon Romanovsky 	/*
2454b79656edSLeon Romanovsky 	 * ndev->dev_port will be equal to 0 in old kernel prior to commit
2455b79656edSLeon Romanovsky 	 * 9b8b2a323008 ("IB/ipoib: Use dev_port to expose network interface
2456b79656edSLeon Romanovsky 	 * port numbers") Zero was chosen as special case for user space
2457b79656edSLeon Romanovsky 	 * applications to fallback and query dev_id to check if it has
2458b79656edSLeon Romanovsky 	 * different value or not.
2459b79656edSLeon Romanovsky 	 *
2460b79656edSLeon Romanovsky 	 * Don't print warning in such scenario.
2461b79656edSLeon Romanovsky 	 *
2462b79656edSLeon Romanovsky 	 * https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L358
2463b79656edSLeon Romanovsky 	 */
2464b79656edSLeon Romanovsky 	if (ndev->dev_port && ndev->dev_id == ndev->dev_port)
2465f6350da4SArseny Maslennikov 		netdev_info_once(ndev,
2466f6350da4SArseny 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",
2467f6350da4SArseny Maslennikov 			current->comm);
2468f6350da4SArseny Maslennikov 
24691c7fd726SJoe Perches 	return sysfs_emit(buf, "%#x\n", ndev->dev_id);
2470f6350da4SArseny Maslennikov }
2471f6350da4SArseny Maslennikov static DEVICE_ATTR_RO(dev_id);
2472f6350da4SArseny Maslennikov 
ipoib_intercept_dev_id_attr(struct net_device * dev)2473ed0bc265SKamal Heib static int ipoib_intercept_dev_id_attr(struct net_device *dev)
2474f6350da4SArseny Maslennikov {
2475f6350da4SArseny Maslennikov 	device_remove_file(&dev->dev, &dev_attr_dev_id);
2476f6350da4SArseny Maslennikov 	return device_create_file(&dev->dev, &dev_attr_dev_id);
2477f6350da4SArseny Maslennikov }
2478f6350da4SArseny Maslennikov 
ipoib_add_port(const char * format,struct ib_device * hca,u32 port)24791da177e4SLinus Torvalds static struct net_device *ipoib_add_port(const char *format,
24801fb7f897SMark Bloch 					 struct ib_device *hca, u32 port)
24811da177e4SLinus Torvalds {
24825d6b0cb3SDenis Drozdov 	struct rtnl_link_ops *ops = ipoib_get_link_ops();
24835d6b0cb3SDenis Drozdov 	struct rdma_netdev_alloc_params params;
24841da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
24859f49a5b5SJason Gunthorpe 	struct net_device *ndev;
2486eaeb3984SJason Gunthorpe 	int result;
24871da177e4SLinus Torvalds 
24885d6b0cb3SDenis Drozdov 	ndev = ipoib_intf_alloc(hca, port, format);
24895d6b0cb3SDenis Drozdov 	if (IS_ERR(ndev)) {
24905d6b0cb3SDenis Drozdov 		pr_warn("%s, %d: ipoib_intf_alloc failed %ld\n", hca->name, port,
24915d6b0cb3SDenis Drozdov 			PTR_ERR(ndev));
24925d6b0cb3SDenis Drozdov 		return ndev;
24931da177e4SLinus Torvalds 	}
24945d6b0cb3SDenis Drozdov 	priv = ipoib_priv(ndev);
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
24971da177e4SLinus Torvalds 			      priv->ca, ipoib_event);
2498dcc9881eSLeon Romanovsky 	ib_register_event_handler(&priv->event_handler);
24991da177e4SLinus Torvalds 
250010293610SAlex Estrin 	/* call event handler to ensure pkey in sync */
250110293610SAlex Estrin 	queue_work(ipoib_workqueue, &priv->flush_heavy);
250210293610SAlex Estrin 
25035ce2dcedSKamal Heib 	ndev->rtnl_link_ops = ipoib_get_link_ops();
25045ce2dcedSKamal Heib 
25059f49a5b5SJason Gunthorpe 	result = register_netdev(ndev);
25061da177e4SLinus Torvalds 	if (result) {
2507c55359a2SYuval Shaia 		pr_warn("%s: couldn't register ipoib port %d; error %d\n",
25081da177e4SLinus Torvalds 			hca->name, port, result);
25091da177e4SLinus Torvalds 
25109f49a5b5SJason Gunthorpe 		ipoib_parent_unregister_pre(ndev);
25119f49a5b5SJason Gunthorpe 		ipoib_intf_free(ndev);
25129f49a5b5SJason Gunthorpe 		free_netdev(ndev);
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds 		return ERR_PTR(result);
25151da177e4SLinus Torvalds 	}
25161da177e4SLinus Torvalds 
25173023a1e9SKamal Heib 	if (hca->ops.rdma_netdev_get_params) {
25183023a1e9SKamal Heib 		int rc = hca->ops.rdma_netdev_get_params(hca, port,
25195d6b0cb3SDenis Drozdov 						     RDMA_NETDEV_IPOIB,
25205d6b0cb3SDenis Drozdov 						     &params);
25215d6b0cb3SDenis Drozdov 
25225d6b0cb3SDenis Drozdov 		if (!rc && ops->priv_size < params.sizeof_priv)
25235d6b0cb3SDenis Drozdov 			ops->priv_size = params.sizeof_priv;
25245d6b0cb3SDenis Drozdov 	}
25259f49a5b5SJason Gunthorpe 	/*
25269f49a5b5SJason Gunthorpe 	 * We cannot set priv_destructor before register_netdev because we
25279f49a5b5SJason Gunthorpe 	 * need priv to be always valid during the error flow to execute
25289f49a5b5SJason Gunthorpe 	 * ipoib_parent_unregister_pre(). Instead handle it manually and only
25299f49a5b5SJason Gunthorpe 	 * enter priv_destructor mode once we are completely registered.
25309f49a5b5SJason Gunthorpe 	 */
25319f49a5b5SJason Gunthorpe 	ndev->priv_destructor = ipoib_intf_free;
25329f49a5b5SJason Gunthorpe 
2533f6350da4SArseny Maslennikov 	if (ipoib_intercept_dev_id_attr(ndev))
2534f6350da4SArseny Maslennikov 		goto sysfs_failed;
25359f49a5b5SJason Gunthorpe 	if (ipoib_cm_add_mode_attr(ndev))
25369f49a5b5SJason Gunthorpe 		goto sysfs_failed;
25379f49a5b5SJason Gunthorpe 	if (ipoib_add_pkey_attr(ndev))
25389f49a5b5SJason Gunthorpe 		goto sysfs_failed;
25399f49a5b5SJason Gunthorpe 	if (ipoib_add_umcast_attr(ndev))
25409f49a5b5SJason Gunthorpe 		goto sysfs_failed;
25419f49a5b5SJason Gunthorpe 	if (device_create_file(&ndev->dev, &dev_attr_create_child))
25429f49a5b5SJason Gunthorpe 		goto sysfs_failed;
25439f49a5b5SJason Gunthorpe 	if (device_create_file(&ndev->dev, &dev_attr_delete_child))
25449f49a5b5SJason Gunthorpe 		goto sysfs_failed;
25459f49a5b5SJason Gunthorpe 
25469f49a5b5SJason Gunthorpe 	return ndev;
25479f49a5b5SJason Gunthorpe 
25489f49a5b5SJason Gunthorpe sysfs_failed:
25499f49a5b5SJason Gunthorpe 	ipoib_parent_unregister_pre(ndev);
25509f49a5b5SJason Gunthorpe 	unregister_netdev(ndev);
25519f49a5b5SJason Gunthorpe 	return ERR_PTR(-ENOMEM);
25529f49a5b5SJason Gunthorpe }
25539f49a5b5SJason Gunthorpe 
ipoib_add_one(struct ib_device * device)255411a0ae4cSJason Gunthorpe static int ipoib_add_one(struct ib_device *device)
25551da177e4SLinus Torvalds {
25561da177e4SLinus Torvalds 	struct list_head *dev_list;
25571da177e4SLinus Torvalds 	struct net_device *dev;
25581da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
2559ea1075edSJason Gunthorpe 	unsigned int p;
25608e37ab68SMichael Wang 	int count = 0;
256107ebafbaSTom Tucker 
2562b1b63970SKamal Heib 	dev_list = kmalloc(sizeof(*dev_list), GFP_KERNEL);
25631da177e4SLinus Torvalds 	if (!dev_list)
256411a0ae4cSJason Gunthorpe 		return -ENOMEM;
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	INIT_LIST_HEAD(dev_list);
25671da177e4SLinus Torvalds 
2568ea1075edSJason Gunthorpe 	rdma_for_each_port (device, p) {
25698e37ab68SMichael Wang 		if (!rdma_protocol_ib(device, p))
25707b4c8769SEli Cohen 			continue;
25711da177e4SLinus Torvalds 		dev = ipoib_add_port("ib%d", device, p);
25721da177e4SLinus Torvalds 		if (!IS_ERR(dev)) {
2573c1048affSErez Shitrit 			priv = ipoib_priv(dev);
25741da177e4SLinus Torvalds 			list_add_tail(&priv->list, dev_list);
25758e37ab68SMichael Wang 			count++;
25761da177e4SLinus Torvalds 		}
25771da177e4SLinus Torvalds 	}
25781da177e4SLinus Torvalds 
25798e37ab68SMichael Wang 	if (!count) {
2580ac6dbf7fSYuval Shaia 		kfree(dev_list);
258111a0ae4cSJason Gunthorpe 		return -EOPNOTSUPP;
25828e37ab68SMichael Wang 	}
25838e37ab68SMichael Wang 
25841da177e4SLinus Torvalds 	ib_set_client_data(device, &ipoib_client, dev_list);
258511a0ae4cSJason Gunthorpe 	return 0;
25861da177e4SLinus Torvalds }
25871da177e4SLinus Torvalds 
ipoib_remove_one(struct ib_device * device,void * client_data)25887c1eb45aSHaggai Eran static void ipoib_remove_one(struct ib_device *device, void *client_data)
25891da177e4SLinus Torvalds {
259025405d98SJason Gunthorpe 	struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv;
25917c1eb45aSHaggai Eran 	struct list_head *dev_list = client_data;
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
259425405d98SJason Gunthorpe 		LIST_HEAD(head);
25957cbee87cSJason Gunthorpe 		ipoib_parent_unregister_pre(priv->dev);
25961da177e4SLinus Torvalds 
259725405d98SJason Gunthorpe 		rtnl_lock();
259825405d98SJason Gunthorpe 
259925405d98SJason Gunthorpe 		list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs,
260025405d98SJason Gunthorpe 					 list)
260125405d98SJason Gunthorpe 			unregister_netdevice_queue(cpriv->dev, &head);
260225405d98SJason Gunthorpe 		unregister_netdevice_queue(priv->dev, &head);
260325405d98SJason Gunthorpe 		unregister_netdevice_many(&head);
260425405d98SJason Gunthorpe 
260525405d98SJason Gunthorpe 		rtnl_unlock();
26061da177e4SLinus Torvalds 	}
260706c56e44SMichael S. Tsirkin 
260806c56e44SMichael S. Tsirkin 	kfree(dev_list);
26091da177e4SLinus Torvalds }
26101da177e4SLinus Torvalds 
2611771a5258SShamir Rabinovitch #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2612771a5258SShamir Rabinovitch static struct notifier_block ipoib_netdev_notifier = {
2613771a5258SShamir Rabinovitch 	.notifier_call = ipoib_netdev_event,
2614771a5258SShamir Rabinovitch };
2615771a5258SShamir Rabinovitch #endif
2616771a5258SShamir Rabinovitch 
ipoib_init_module(void)26171da177e4SLinus Torvalds static int __init ipoib_init_module(void)
26181da177e4SLinus Torvalds {
26191da177e4SLinus Torvalds 	int ret;
26201da177e4SLinus Torvalds 
26210f485251SShirley Ma 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
26220f485251SShirley Ma 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
26230f485251SShirley Ma 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
26240f485251SShirley Ma 
26250f485251SShirley Ma 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
26260f485251SShirley Ma 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
2627732eacc0SHagen Paul Pfeifer 	ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
262868e995a2SPradeep Satyanarayana #ifdef CONFIG_INFINIBAND_IPOIB_CM
262968e995a2SPradeep Satyanarayana 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
263011f74b40SAlex Vesker 	ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0);
263168e995a2SPradeep Satyanarayana #endif
26320f485251SShirley Ma 
2633f89271daSEli Cohen 	/*
2634f89271daSEli Cohen 	 * When copying small received packets, we only copy from the
2635f89271daSEli Cohen 	 * linear data part of the SKB, so we rely on this condition.
2636f89271daSEli Cohen 	 */
2637f89271daSEli Cohen 	BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2638f89271daSEli Cohen 
26392e061c69SGreg Kroah-Hartman 	ipoib_register_debugfs();
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 	/*
26420b39578bSDoug Ledford 	 * We create a global workqueue here that is used for all flush
26430b39578bSDoug Ledford 	 * operations.  However, if you attempt to flush a workqueue
26440b39578bSDoug Ledford 	 * from a task on that same workqueue, it deadlocks the system.
26450b39578bSDoug Ledford 	 * We want to be able to flush the tasks associated with a
26460b39578bSDoug Ledford 	 * specific net device, so we also create a workqueue for each
26470b39578bSDoug Ledford 	 * netdevice.  We queue up the tasks for that device only on
26480b39578bSDoug Ledford 	 * its private workqueue, and we only queue up flush events
26490b39578bSDoug Ledford 	 * on our global flush workqueue.  This avoids the deadlocks.
26501da177e4SLinus Torvalds 	 */
2651ee190ab7SJason Gunthorpe 	ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush", 0);
26521da177e4SLinus Torvalds 	if (!ipoib_workqueue) {
26531da177e4SLinus Torvalds 		ret = -ENOMEM;
26541da177e4SLinus Torvalds 		goto err_fs;
26551da177e4SLinus Torvalds 	}
26561da177e4SLinus Torvalds 
2657c1a0b23bSMichael S. Tsirkin 	ib_sa_register_client(&ipoib_sa_client);
2658c1a0b23bSMichael S. Tsirkin 
26591da177e4SLinus Torvalds 	ret = ib_register_client(&ipoib_client);
26601da177e4SLinus Torvalds 	if (ret)
2661c1a0b23bSMichael S. Tsirkin 		goto err_sa;
26621da177e4SLinus Torvalds 
26639baa0b03SOr Gerlitz 	ret = ipoib_netlink_init();
26649baa0b03SOr Gerlitz 	if (ret)
26659baa0b03SOr Gerlitz 		goto err_client;
26669baa0b03SOr Gerlitz 
2667771a5258SShamir Rabinovitch #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2668771a5258SShamir Rabinovitch 	register_netdevice_notifier(&ipoib_netdev_notifier);
2669771a5258SShamir Rabinovitch #endif
26701da177e4SLinus Torvalds 	return 0;
26711da177e4SLinus Torvalds 
26729baa0b03SOr Gerlitz err_client:
26739baa0b03SOr Gerlitz 	ib_unregister_client(&ipoib_client);
26749baa0b03SOr Gerlitz 
2675c1a0b23bSMichael S. Tsirkin err_sa:
2676c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&ipoib_sa_client);
26771da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
26781da177e4SLinus Torvalds 
26799adec1a8SRoland Dreier err_fs:
26809adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
26819adec1a8SRoland Dreier 
26821da177e4SLinus Torvalds 	return ret;
26831da177e4SLinus Torvalds }
26841da177e4SLinus Torvalds 
ipoib_cleanup_module(void)26851da177e4SLinus Torvalds static void __exit ipoib_cleanup_module(void)
26861da177e4SLinus Torvalds {
2687771a5258SShamir Rabinovitch #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2688771a5258SShamir Rabinovitch 	unregister_netdevice_notifier(&ipoib_netdev_notifier);
2689771a5258SShamir Rabinovitch #endif
26909baa0b03SOr Gerlitz 	ipoib_netlink_fini();
26911da177e4SLinus Torvalds 	ib_unregister_client(&ipoib_client);
2692c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&ipoib_sa_client);
26939adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
26941da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
26951da177e4SLinus Torvalds }
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds module_init(ipoib_init_module);
26981da177e4SLinus Torvalds module_exit(ipoib_cleanup_module);
2699