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>
5114c85021SArnaldo Carvalho de Melo 
524b48680bSYan Burman #define DRV_VERSION "1.0.0"
534b48680bSYan Burman 
544b48680bSYan Burman const char ipoib_driver_version[] = DRV_VERSION;
554b48680bSYan Burman 
561da177e4SLinus Torvalds MODULE_AUTHOR("Roland Dreier");
571da177e4SLinus Torvalds MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
581da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL");
594b48680bSYan Burman MODULE_VERSION(DRV_VERSION);
601da177e4SLinus Torvalds 
610f485251SShirley Ma int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
620f485251SShirley Ma int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
630f485251SShirley Ma 
640f485251SShirley Ma module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
650f485251SShirley Ma MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
660f485251SShirley Ma module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
670f485251SShirley Ma MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
680f485251SShirley Ma 
691da177e4SLinus Torvalds #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
701da177e4SLinus Torvalds int ipoib_debug_level;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds module_param_named(debug_level, ipoib_debug_level, int, 0644);
731da177e4SLinus Torvalds MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
741da177e4SLinus Torvalds #endif
751da177e4SLinus Torvalds 
761732b0efSRoland Dreier struct ipoib_path_iter {
771732b0efSRoland Dreier 	struct net_device *dev;
781732b0efSRoland Dreier 	struct ipoib_path  path;
791732b0efSRoland Dreier };
801732b0efSRoland Dreier 
811da177e4SLinus Torvalds static const u8 ipv4_bcast_addr[] = {
821da177e4SLinus Torvalds 	0x00, 0xff, 0xff, 0xff,
831da177e4SLinus Torvalds 	0xff, 0x12, 0x40, 0x1b,	0x00, 0x00, 0x00, 0x00,
841da177e4SLinus Torvalds 	0x00, 0x00, 0x00, 0x00,	0xff, 0xff, 0xff, 0xff
851da177e4SLinus Torvalds };
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds struct workqueue_struct *ipoib_workqueue;
881da177e4SLinus Torvalds 
89c1a0b23bSMichael S. Tsirkin struct ib_sa_client ipoib_sa_client;
90c1a0b23bSMichael S. Tsirkin 
911da177e4SLinus Torvalds static void ipoib_add_one(struct ib_device *device);
921da177e4SLinus Torvalds static void ipoib_remove_one(struct ib_device *device);
93b63b70d8SShlomo Pongratz static void ipoib_neigh_reclaim(struct rcu_head *rp);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static struct ib_client ipoib_client = {
961da177e4SLinus Torvalds 	.name   = "ipoib",
971da177e4SLinus Torvalds 	.add    = ipoib_add_one,
981da177e4SLinus Torvalds 	.remove = ipoib_remove_one
991da177e4SLinus Torvalds };
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds int ipoib_open(struct net_device *dev)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	ipoib_dbg(priv, "bringing up interface\n");
1061da177e4SLinus Torvalds 
107e028cc55SYossi Etigin 	set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	if (ipoib_pkey_dev_delay_open(dev))
1101da177e4SLinus Torvalds 		return 0;
1111da177e4SLinus Torvalds 
112b8a1b1ceSRoland Dreier 	if (ipoib_ib_dev_open(dev))
113b8a1b1ceSRoland Dreier 		goto err_disable;
114fe25c561SYossi Etigin 
115b8a1b1ceSRoland Dreier 	if (ipoib_ib_dev_up(dev))
116b8a1b1ceSRoland Dreier 		goto err_stop;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1191da177e4SLinus Torvalds 		struct ipoib_dev_priv *cpriv;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 		/* Bring up any child interfaces too */
12295ed644fSIngo Molnar 		mutex_lock(&priv->vlan_mutex);
1231da177e4SLinus Torvalds 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
1241da177e4SLinus Torvalds 			int flags;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 			flags = cpriv->dev->flags;
1271da177e4SLinus Torvalds 			if (flags & IFF_UP)
1281da177e4SLinus Torvalds 				continue;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 			dev_change_flags(cpriv->dev, flags | IFF_UP);
1311da177e4SLinus Torvalds 		}
13295ed644fSIngo Molnar 		mutex_unlock(&priv->vlan_mutex);
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	netif_start_queue(dev);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	return 0;
138b8a1b1ceSRoland Dreier 
139b8a1b1ceSRoland Dreier err_stop:
140b8a1b1ceSRoland Dreier 	ipoib_ib_dev_stop(dev, 1);
141b8a1b1ceSRoland Dreier 
142b8a1b1ceSRoland Dreier err_disable:
143b8a1b1ceSRoland Dreier 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
144b8a1b1ceSRoland Dreier 
145b8a1b1ceSRoland Dreier 	return -EINVAL;
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds static int ipoib_stop(struct net_device *dev)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	ipoib_dbg(priv, "stopping interface\n");
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	netif_stop_queue(dev);
1571da177e4SLinus Torvalds 
158bea1e22dSPatrick McHardy 	ipoib_ib_dev_down(dev, 1);
159a77a57a1SRoland Dreier 	ipoib_ib_dev_stop(dev, 0);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1621da177e4SLinus Torvalds 		struct ipoib_dev_priv *cpriv;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 		/* Bring down any child interfaces too */
16595ed644fSIngo Molnar 		mutex_lock(&priv->vlan_mutex);
1661da177e4SLinus Torvalds 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
1671da177e4SLinus Torvalds 			int flags;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 			flags = cpriv->dev->flags;
1701da177e4SLinus Torvalds 			if (!(flags & IFF_UP))
1711da177e4SLinus Torvalds 				continue;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 			dev_change_flags(cpriv->dev, flags & ~IFF_UP);
1741da177e4SLinus Torvalds 		}
17595ed644fSIngo Molnar 		mutex_unlock(&priv->vlan_mutex);
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	return 0;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1819baa0b03SOr Gerlitz static void ipoib_uninit(struct net_device *dev)
1829baa0b03SOr Gerlitz {
1839baa0b03SOr Gerlitz 	ipoib_dev_cleanup(dev);
1849baa0b03SOr Gerlitz }
1859baa0b03SOr Gerlitz 
1869ca36f7dSDavid S. Miller static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
1873d96c74dSMichał Mirosław {
1883d96c74dSMichał Mirosław 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1893d96c74dSMichał Mirosław 
1903d96c74dSMichał Mirosław 	if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1913d96c74dSMichał Mirosław 		features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1923d96c74dSMichał Mirosław 
1933d96c74dSMichał Mirosław 	return features;
1943d96c74dSMichał Mirosław }
1953d96c74dSMichał Mirosław 
1961da177e4SLinus Torvalds static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1991da177e4SLinus Torvalds 
200839fcabaSMichael S. Tsirkin 	/* dev->mtu > 2K ==> connected mode */
201586a6934SPradeep Satyanarayana 	if (ipoib_cm_admin_enabled(dev)) {
202586a6934SPradeep Satyanarayana 		if (new_mtu > ipoib_cm_max_mtu(dev))
203586a6934SPradeep Satyanarayana 			return -EINVAL;
204586a6934SPradeep Satyanarayana 
205839fcabaSMichael S. Tsirkin 		if (new_mtu > priv->mcast_mtu)
206839fcabaSMichael S. Tsirkin 			ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
207839fcabaSMichael S. Tsirkin 				   priv->mcast_mtu);
208586a6934SPradeep Satyanarayana 
209839fcabaSMichael S. Tsirkin 		dev->mtu = new_mtu;
210839fcabaSMichael S. Tsirkin 		return 0;
211839fcabaSMichael S. Tsirkin 	}
212839fcabaSMichael S. Tsirkin 
213bc7b3a36SShirley Ma 	if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
2141da177e4SLinus Torvalds 		return -EINVAL;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	priv->admin_mtu = new_mtu;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	return 0;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
22371d9c5f9SRoland Dreier int ipoib_set_mode(struct net_device *dev, const char *buf)
22471d9c5f9SRoland Dreier {
22571d9c5f9SRoland Dreier 	struct ipoib_dev_priv *priv = netdev_priv(dev);
22671d9c5f9SRoland Dreier 
22771d9c5f9SRoland Dreier 	/* flush paths if we switch modes so that connections are restarted */
22871d9c5f9SRoland Dreier 	if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
22971d9c5f9SRoland Dreier 		set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
23071d9c5f9SRoland Dreier 		ipoib_warn(priv, "enabling connected mode "
23171d9c5f9SRoland Dreier 			   "will cause multicast packet drops\n");
23271d9c5f9SRoland Dreier 		netdev_update_features(dev);
23371d9c5f9SRoland Dreier 		rtnl_unlock();
23471d9c5f9SRoland Dreier 		priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
23571d9c5f9SRoland Dreier 
23671d9c5f9SRoland Dreier 		ipoib_flush_paths(dev);
23771d9c5f9SRoland Dreier 		rtnl_lock();
23871d9c5f9SRoland Dreier 		return 0;
23971d9c5f9SRoland Dreier 	}
24071d9c5f9SRoland Dreier 
24171d9c5f9SRoland Dreier 	if (!strcmp(buf, "datagram\n")) {
24271d9c5f9SRoland Dreier 		clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
24371d9c5f9SRoland Dreier 		netdev_update_features(dev);
24471d9c5f9SRoland Dreier 		dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
24571d9c5f9SRoland Dreier 		rtnl_unlock();
24671d9c5f9SRoland Dreier 		ipoib_flush_paths(dev);
24771d9c5f9SRoland Dreier 		rtnl_lock();
24871d9c5f9SRoland Dreier 		return 0;
24971d9c5f9SRoland Dreier 	}
25071d9c5f9SRoland Dreier 
25171d9c5f9SRoland Dreier 	return -EINVAL;
25271d9c5f9SRoland Dreier }
25371d9c5f9SRoland Dreier 
25437c22a77SJack Morgenstein static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
2571da177e4SLinus Torvalds 	struct rb_node *n = priv->path_tree.rb_node;
2581da177e4SLinus Torvalds 	struct ipoib_path *path;
2591da177e4SLinus Torvalds 	int ret;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	while (n) {
2621da177e4SLinus Torvalds 		path = rb_entry(n, struct ipoib_path, rb_node);
2631da177e4SLinus Torvalds 
26437c22a77SJack Morgenstein 		ret = memcmp(gid, path->pathrec.dgid.raw,
2651da177e4SLinus Torvalds 			     sizeof (union ib_gid));
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		if (ret < 0)
2681da177e4SLinus Torvalds 			n = n->rb_left;
2691da177e4SLinus Torvalds 		else if (ret > 0)
2701da177e4SLinus Torvalds 			n = n->rb_right;
2711da177e4SLinus Torvalds 		else
2721da177e4SLinus Torvalds 			return path;
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	return NULL;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static int __path_add(struct net_device *dev, struct ipoib_path *path)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
2811da177e4SLinus Torvalds 	struct rb_node **n = &priv->path_tree.rb_node;
2821da177e4SLinus Torvalds 	struct rb_node *pn = NULL;
2831da177e4SLinus Torvalds 	struct ipoib_path *tpath;
2841da177e4SLinus Torvalds 	int ret;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	while (*n) {
2871da177e4SLinus Torvalds 		pn = *n;
2881da177e4SLinus Torvalds 		tpath = rb_entry(pn, struct ipoib_path, rb_node);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 		ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
2911da177e4SLinus Torvalds 			     sizeof (union ib_gid));
2921da177e4SLinus Torvalds 		if (ret < 0)
2931da177e4SLinus Torvalds 			n = &pn->rb_left;
2941da177e4SLinus Torvalds 		else if (ret > 0)
2951da177e4SLinus Torvalds 			n = &pn->rb_right;
2961da177e4SLinus Torvalds 		else
2971da177e4SLinus Torvalds 			return -EEXIST;
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	rb_link_node(&path->rb_node, pn, n);
3011da177e4SLinus Torvalds 	rb_insert_color(&path->rb_node, &priv->path_tree);
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	list_add_tail(&path->list, &priv->path_list);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	return 0;
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds static void path_free(struct net_device *dev, struct ipoib_path *path)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	struct sk_buff *skb;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&path->queue)))
3131da177e4SLinus Torvalds 		dev_kfree_skb_irq(skb);
3141da177e4SLinus Torvalds 
315b63b70d8SShlomo Pongratz 	ipoib_dbg(netdev_priv(dev), "path_free\n");
3161da177e4SLinus Torvalds 
317b63b70d8SShlomo Pongratz 	/* remove all neigh connected to this path */
318b63b70d8SShlomo Pongratz 	ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	if (path->ah)
3211da177e4SLinus Torvalds 		ipoib_put_ah(path->ah);
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	kfree(path);
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
3261732b0efSRoland Dreier #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
3271732b0efSRoland Dreier 
3281732b0efSRoland Dreier struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
3291732b0efSRoland Dreier {
3301732b0efSRoland Dreier 	struct ipoib_path_iter *iter;
3311732b0efSRoland Dreier 
3321732b0efSRoland Dreier 	iter = kmalloc(sizeof *iter, GFP_KERNEL);
3331732b0efSRoland Dreier 	if (!iter)
3341732b0efSRoland Dreier 		return NULL;
3351732b0efSRoland Dreier 
3361732b0efSRoland Dreier 	iter->dev = dev;
3371732b0efSRoland Dreier 	memset(iter->path.pathrec.dgid.raw, 0, 16);
3381732b0efSRoland Dreier 
3391732b0efSRoland Dreier 	if (ipoib_path_iter_next(iter)) {
3401732b0efSRoland Dreier 		kfree(iter);
3411732b0efSRoland Dreier 		return NULL;
3421732b0efSRoland Dreier 	}
3431732b0efSRoland Dreier 
3441732b0efSRoland Dreier 	return iter;
3451732b0efSRoland Dreier }
3461732b0efSRoland Dreier 
3471732b0efSRoland Dreier int ipoib_path_iter_next(struct ipoib_path_iter *iter)
3481732b0efSRoland Dreier {
3491732b0efSRoland Dreier 	struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
3501732b0efSRoland Dreier 	struct rb_node *n;
3511732b0efSRoland Dreier 	struct ipoib_path *path;
3521732b0efSRoland Dreier 	int ret = 1;
3531732b0efSRoland Dreier 
3541732b0efSRoland Dreier 	spin_lock_irq(&priv->lock);
3551732b0efSRoland Dreier 
3561732b0efSRoland Dreier 	n = rb_first(&priv->path_tree);
3571732b0efSRoland Dreier 
3581732b0efSRoland Dreier 	while (n) {
3591732b0efSRoland Dreier 		path = rb_entry(n, struct ipoib_path, rb_node);
3601732b0efSRoland Dreier 
3611732b0efSRoland Dreier 		if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
3621732b0efSRoland Dreier 			   sizeof (union ib_gid)) < 0) {
3631732b0efSRoland Dreier 			iter->path = *path;
3641732b0efSRoland Dreier 			ret = 0;
3651732b0efSRoland Dreier 			break;
3661732b0efSRoland Dreier 		}
3671732b0efSRoland Dreier 
3681732b0efSRoland Dreier 		n = rb_next(n);
3691732b0efSRoland Dreier 	}
3701732b0efSRoland Dreier 
3711732b0efSRoland Dreier 	spin_unlock_irq(&priv->lock);
3721732b0efSRoland Dreier 
3731732b0efSRoland Dreier 	return ret;
3741732b0efSRoland Dreier }
3751732b0efSRoland Dreier 
3761732b0efSRoland Dreier void ipoib_path_iter_read(struct ipoib_path_iter *iter,
3771732b0efSRoland Dreier 			  struct ipoib_path *path)
3781732b0efSRoland Dreier {
3791732b0efSRoland Dreier 	*path = iter->path;
3801732b0efSRoland Dreier }
3811732b0efSRoland Dreier 
3821732b0efSRoland Dreier #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
3831732b0efSRoland Dreier 
384ee1e2c82SMoni Shoua void ipoib_mark_paths_invalid(struct net_device *dev)
385ee1e2c82SMoni Shoua {
386ee1e2c82SMoni Shoua 	struct ipoib_dev_priv *priv = netdev_priv(dev);
387ee1e2c82SMoni Shoua 	struct ipoib_path *path, *tp;
388ee1e2c82SMoni Shoua 
389ee1e2c82SMoni Shoua 	spin_lock_irq(&priv->lock);
390ee1e2c82SMoni Shoua 
391ee1e2c82SMoni Shoua 	list_for_each_entry_safe(path, tp, &priv->path_list, list) {
3925b095d98SHarvey Harrison 		ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
393ee1e2c82SMoni Shoua 			be16_to_cpu(path->pathrec.dlid),
394fcace2feSHarvey Harrison 			path->pathrec.dgid.raw);
395ee1e2c82SMoni Shoua 		path->valid =  0;
396ee1e2c82SMoni Shoua 	}
397ee1e2c82SMoni Shoua 
398ee1e2c82SMoni Shoua 	spin_unlock_irq(&priv->lock);
399ee1e2c82SMoni Shoua }
400ee1e2c82SMoni Shoua 
4011da177e4SLinus Torvalds void ipoib_flush_paths(struct net_device *dev)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
4041da177e4SLinus Torvalds 	struct ipoib_path *path, *tp;
4051da177e4SLinus Torvalds 	LIST_HEAD(remove_list);
406943c246eSRoland Dreier 	unsigned long flags;
4071da177e4SLinus Torvalds 
408943c246eSRoland Dreier 	netif_tx_lock_bh(dev);
409943c246eSRoland Dreier 	spin_lock_irqsave(&priv->lock, flags);
4101da177e4SLinus Torvalds 
411157de229SRobert P. J. Day 	list_splice_init(&priv->path_list, &remove_list);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	list_for_each_entry(path, &remove_list, list)
4141da177e4SLinus Torvalds 		rb_erase(&path->rb_node, &priv->path_tree);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	list_for_each_entry_safe(path, tp, &remove_list, list) {
4171da177e4SLinus Torvalds 		if (path->query)
4181da177e4SLinus Torvalds 			ib_sa_cancel_query(path->query_id, path->query);
419943c246eSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
420943c246eSRoland Dreier 		netif_tx_unlock_bh(dev);
4211da177e4SLinus Torvalds 		wait_for_completion(&path->done);
4221da177e4SLinus Torvalds 		path_free(dev, path);
423943c246eSRoland Dreier 		netif_tx_lock_bh(dev);
424943c246eSRoland Dreier 		spin_lock_irqsave(&priv->lock, flags);
4251da177e4SLinus Torvalds 	}
426943c246eSRoland Dreier 
427943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
428943c246eSRoland Dreier 	netif_tx_unlock_bh(dev);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds static void path_rec_completion(int status,
4321da177e4SLinus Torvalds 				struct ib_sa_path_rec *pathrec,
4331da177e4SLinus Torvalds 				void *path_ptr)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	struct ipoib_path *path = path_ptr;
4361da177e4SLinus Torvalds 	struct net_device *dev = path->dev;
4371da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
4381da177e4SLinus Torvalds 	struct ipoib_ah *ah = NULL;
439c9da4badSRoland Dreier 	struct ipoib_ah *old_ah = NULL;
440d04d01b1SMichael S. Tsirkin 	struct ipoib_neigh *neigh, *tn;
4411da177e4SLinus Torvalds 	struct sk_buff_head skqueue;
4421da177e4SLinus Torvalds 	struct sk_buff *skb;
4431da177e4SLinus Torvalds 	unsigned long flags;
4441da177e4SLinus Torvalds 
445843613b0SRoland Dreier 	if (!status)
4465b095d98SHarvey Harrison 		ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
447fcace2feSHarvey Harrison 			  be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
4481da177e4SLinus Torvalds 	else
4495b095d98SHarvey Harrison 		ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
450fcace2feSHarvey Harrison 			  status, path->pathrec.dgid.raw);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	skb_queue_head_init(&skqueue);
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	if (!status) {
45546f1b3d7SSean Hefty 		struct ib_ah_attr av;
4561da177e4SLinus Torvalds 
45746f1b3d7SSean Hefty 		if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
4581da177e4SLinus Torvalds 			ah = ipoib_create_ah(dev, priv->pd, &av);
4591da177e4SLinus Torvalds 	}
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
4621da177e4SLinus Torvalds 
4633874397cSMike Marciniszyn 	if (!IS_ERR_OR_NULL(ah)) {
4641da177e4SLinus Torvalds 		path->pathrec = *pathrec;
4651da177e4SLinus Torvalds 
466c9da4badSRoland Dreier 		old_ah   = path->ah;
467c9da4badSRoland Dreier 		path->ah = ah;
468c9da4badSRoland Dreier 
4691da177e4SLinus Torvalds 		ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
4701da177e4SLinus Torvalds 			  ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 		while ((skb = __skb_dequeue(&path->queue)))
4731da177e4SLinus Torvalds 			__skb_queue_tail(&skqueue, skb);
4741da177e4SLinus Torvalds 
475d04d01b1SMichael S. Tsirkin 		list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
476ee1e2c82SMoni Shoua 			if (neigh->ah) {
477ee1e2c82SMoni Shoua 				WARN_ON(neigh->ah != old_ah);
478ee1e2c82SMoni Shoua 				/*
479ee1e2c82SMoni Shoua 				 * Dropping the ah reference inside
480ee1e2c82SMoni Shoua 				 * priv->lock is safe here, because we
481ee1e2c82SMoni Shoua 				 * will hold one more reference from
482ee1e2c82SMoni Shoua 				 * the original value of path->ah (ie
483ee1e2c82SMoni Shoua 				 * old_ah).
484ee1e2c82SMoni Shoua 				 */
485ee1e2c82SMoni Shoua 				ipoib_put_ah(neigh->ah);
486ee1e2c82SMoni Shoua 			}
4871da177e4SLinus Torvalds 			kref_get(&path->ah->ref);
4881da177e4SLinus Torvalds 			neigh->ah = path->ah;
4891da177e4SLinus Torvalds 
490b63b70d8SShlomo Pongratz 			if (ipoib_cm_enabled(dev, neigh->daddr)) {
491839fcabaSMichael S. Tsirkin 				if (!ipoib_cm_get(neigh))
492839fcabaSMichael S. Tsirkin 					ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
493839fcabaSMichael S. Tsirkin 									       path,
494839fcabaSMichael S. Tsirkin 									       neigh));
495839fcabaSMichael S. Tsirkin 				if (!ipoib_cm_get(neigh)) {
496b63b70d8SShlomo Pongratz 					ipoib_neigh_free(neigh);
497839fcabaSMichael S. Tsirkin 					continue;
498839fcabaSMichael S. Tsirkin 				}
499839fcabaSMichael S. Tsirkin 			}
500839fcabaSMichael S. Tsirkin 
5011da177e4SLinus Torvalds 			while ((skb = __skb_dequeue(&neigh->queue)))
5021da177e4SLinus Torvalds 				__skb_queue_tail(&skqueue, skb);
5031da177e4SLinus Torvalds 		}
504ee1e2c82SMoni Shoua 		path->valid = 1;
5055872a9fcSRoland Dreier 	}
5061da177e4SLinus Torvalds 
5075872a9fcSRoland Dreier 	path->query = NULL;
5081da177e4SLinus Torvalds 	complete(&path->done);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
5111da177e4SLinus Torvalds 
512f72dd566SRoland Dreier 	if (IS_ERR_OR_NULL(ah))
513f72dd566SRoland Dreier 		ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
514f72dd566SRoland Dreier 
515ee1e2c82SMoni Shoua 	if (old_ah)
516ee1e2c82SMoni Shoua 		ipoib_put_ah(old_ah);
517ee1e2c82SMoni Shoua 
5181da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&skqueue))) {
5191da177e4SLinus Torvalds 		skb->dev = dev;
5201da177e4SLinus Torvalds 		if (dev_queue_xmit(skb))
5211da177e4SLinus Torvalds 			ipoib_warn(priv, "dev_queue_xmit failed "
5221da177e4SLinus Torvalds 				   "to requeue packet\n");
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
52637c22a77SJack Morgenstein static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
5291da177e4SLinus Torvalds 	struct ipoib_path *path;
5301da177e4SLinus Torvalds 
5311401b53aSJack Morgenstein 	if (!priv->broadcast)
5321401b53aSJack Morgenstein 		return NULL;
5331401b53aSJack Morgenstein 
53421a38489SRoland Dreier 	path = kzalloc(sizeof *path, GFP_ATOMIC);
5351da177e4SLinus Torvalds 	if (!path)
5361da177e4SLinus Torvalds 		return NULL;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	path->dev = dev;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	skb_queue_head_init(&path->queue);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	INIT_LIST_HEAD(&path->neigh_list);
5431da177e4SLinus Torvalds 
54437c22a77SJack Morgenstein 	memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
5451da177e4SLinus Torvalds 	path->pathrec.sgid	    = priv->local_gid;
5461da177e4SLinus Torvalds 	path->pathrec.pkey	    = cpu_to_be16(priv->pkey);
5471da177e4SLinus Torvalds 	path->pathrec.numb_path     = 1;
54881668838SSean Hefty 	path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	return path;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds static int path_rec_start(struct net_device *dev,
5541da177e4SLinus Torvalds 			  struct ipoib_path *path)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
5571da177e4SLinus Torvalds 
5585b095d98SHarvey Harrison 	ipoib_dbg(priv, "Start path record lookup for %pI6\n",
559fcace2feSHarvey Harrison 		  path->pathrec.dgid.raw);
5601da177e4SLinus Torvalds 
56165c7eddaSRoland Dreier 	init_completion(&path->done);
56265c7eddaSRoland Dreier 
5631da177e4SLinus Torvalds 	path->query_id =
564c1a0b23bSMichael S. Tsirkin 		ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
5651da177e4SLinus Torvalds 				   &path->pathrec,
5661da177e4SLinus Torvalds 				   IB_SA_PATH_REC_DGID		|
5671da177e4SLinus Torvalds 				   IB_SA_PATH_REC_SGID		|
5681da177e4SLinus Torvalds 				   IB_SA_PATH_REC_NUMB_PATH	|
56981668838SSean Hefty 				   IB_SA_PATH_REC_TRAFFIC_CLASS |
5701da177e4SLinus Torvalds 				   IB_SA_PATH_REC_PKEY,
5711da177e4SLinus Torvalds 				   1000, GFP_ATOMIC,
5721da177e4SLinus Torvalds 				   path_rec_completion,
5731da177e4SLinus Torvalds 				   path, &path->query);
5741da177e4SLinus Torvalds 	if (path->query_id < 0) {
57501b3fc8bSOr Gerlitz 		ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
5761da177e4SLinus Torvalds 		path->query = NULL;
57793a3ab93SYossi Etigin 		complete(&path->done);
5781da177e4SLinus Torvalds 		return path->query_id;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	return 0;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
584b63b70d8SShlomo Pongratz static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
585b63b70d8SShlomo Pongratz 			   struct net_device *dev)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
5881da177e4SLinus Torvalds 	struct ipoib_path *path;
5891da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
590943c246eSRoland Dreier 	unsigned long flags;
5911da177e4SLinus Torvalds 
592b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
593b63b70d8SShlomo Pongratz 	neigh = ipoib_neigh_alloc(daddr, dev);
5941da177e4SLinus Torvalds 	if (!neigh) {
595b5120a6eSShlomo Pongratz 		spin_unlock_irqrestore(&priv->lock, flags);
596de903512SRoland Dreier 		++dev->stats.tx_dropped;
5971da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
5981da177e4SLinus Torvalds 		return;
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds 
601b63b70d8SShlomo Pongratz 	path = __path_find(dev, daddr + 4);
6021da177e4SLinus Torvalds 	if (!path) {
603b63b70d8SShlomo Pongratz 		path = path_rec_create(dev, daddr + 4);
6041da177e4SLinus Torvalds 		if (!path)
605d2e0655eSMichael S. Tsirkin 			goto err_path;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 		__path_add(dev, path);
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	list_add_tail(&neigh->list, &path->neigh_list);
6111da177e4SLinus Torvalds 
61247f7a071SMichael S. Tsirkin 	if (path->ah) {
6131da177e4SLinus Torvalds 		kref_get(&path->ah->ref);
6141da177e4SLinus Torvalds 		neigh->ah = path->ah;
6151da177e4SLinus Torvalds 
616b63b70d8SShlomo Pongratz 		if (ipoib_cm_enabled(dev, neigh->daddr)) {
617839fcabaSMichael S. Tsirkin 			if (!ipoib_cm_get(neigh))
618839fcabaSMichael S. Tsirkin 				ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
619839fcabaSMichael S. Tsirkin 			if (!ipoib_cm_get(neigh)) {
620b63b70d8SShlomo Pongratz 				ipoib_neigh_free(neigh);
621839fcabaSMichael S. Tsirkin 				goto err_drop;
622839fcabaSMichael S. Tsirkin 			}
623839fcabaSMichael S. Tsirkin 			if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
624839fcabaSMichael S. Tsirkin 				__skb_queue_tail(&neigh->queue, skb);
625839fcabaSMichael S. Tsirkin 			else {
626839fcabaSMichael S. Tsirkin 				ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
627839fcabaSMichael S. Tsirkin 					   skb_queue_len(&neigh->queue));
628839fcabaSMichael S. Tsirkin 				goto err_drop;
629839fcabaSMichael S. Tsirkin 			}
630721d67cdSRoland Dreier 		} else {
631721d67cdSRoland Dreier 			spin_unlock_irqrestore(&priv->lock, flags);
632b63b70d8SShlomo Pongratz 			ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
633b63b70d8SShlomo Pongratz 			ipoib_neigh_put(neigh);
634721d67cdSRoland Dreier 			return;
635721d67cdSRoland Dreier 		}
6361da177e4SLinus Torvalds 	} else {
6371da177e4SLinus Torvalds 		neigh->ah  = NULL;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 		if (!path->query && path_rec_start(dev, path))
64049b8e744SJim Foraker 			goto err_path;
6412745b5b7SMichael S. Tsirkin 
6422745b5b7SMichael S. Tsirkin 		__skb_queue_tail(&neigh->queue, skb);
6431da177e4SLinus Torvalds 	}
6441da177e4SLinus Torvalds 
645943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
646b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
6471da177e4SLinus Torvalds 	return;
6481da177e4SLinus Torvalds 
649d2e0655eSMichael S. Tsirkin err_path:
650b63b70d8SShlomo Pongratz 	ipoib_neigh_free(neigh);
651839fcabaSMichael S. Tsirkin err_drop:
652de903512SRoland Dreier 	++dev->stats.tx_dropped;
6531da177e4SLinus Torvalds 	dev_kfree_skb_any(skb);
6541da177e4SLinus Torvalds 
655943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
656b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
660936d7de3SRoland Dreier 			     struct ipoib_cb *cb)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
6631da177e4SLinus Torvalds 	struct ipoib_path *path;
664943c246eSRoland Dreier 	unsigned long flags;
6651da177e4SLinus Torvalds 
666943c246eSRoland Dreier 	spin_lock_irqsave(&priv->lock, flags);
6671da177e4SLinus Torvalds 
668936d7de3SRoland Dreier 	path = __path_find(dev, cb->hwaddr + 4);
669ee1e2c82SMoni Shoua 	if (!path || !path->valid) {
67071d98b46SJack Morgenstein 		int new_path = 0;
67171d98b46SJack Morgenstein 
67271d98b46SJack Morgenstein 		if (!path) {
673936d7de3SRoland Dreier 			path = path_rec_create(dev, cb->hwaddr + 4);
67471d98b46SJack Morgenstein 			new_path = 1;
67571d98b46SJack Morgenstein 		}
6761da177e4SLinus Torvalds 		if (path) {
6771da177e4SLinus Torvalds 			__skb_queue_tail(&path->queue, skb);
6781da177e4SLinus Torvalds 
679ff79ae80SYossi Etigin 			if (!path->query && path_rec_start(dev, path)) {
680943c246eSRoland Dreier 				spin_unlock_irqrestore(&priv->lock, flags);
68171d98b46SJack Morgenstein 				if (new_path)
6821da177e4SLinus Torvalds 					path_free(dev, path);
6831da177e4SLinus Torvalds 				return;
6841da177e4SLinus Torvalds 			} else
6851da177e4SLinus Torvalds 				__path_add(dev, path);
6861da177e4SLinus Torvalds 		} else {
687de903512SRoland Dreier 			++dev->stats.tx_dropped;
6881da177e4SLinus Torvalds 			dev_kfree_skb_any(skb);
6891da177e4SLinus Torvalds 		}
6901da177e4SLinus Torvalds 
691943c246eSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
6921da177e4SLinus Torvalds 		return;
6931da177e4SLinus Torvalds 	}
6941da177e4SLinus Torvalds 
69547f7a071SMichael S. Tsirkin 	if (path->ah) {
6961da177e4SLinus Torvalds 		ipoib_dbg(priv, "Send unicast ARP to %04x\n",
6971da177e4SLinus Torvalds 			  be16_to_cpu(path->pathrec.dlid));
6981da177e4SLinus Torvalds 
699721d67cdSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
700936d7de3SRoland Dreier 		ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
701721d67cdSRoland Dreier 		return;
7021da177e4SLinus Torvalds 	} else if ((path->query || !path_rec_start(dev, path)) &&
7031da177e4SLinus Torvalds 		   skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
7041da177e4SLinus Torvalds 		__skb_queue_tail(&path->queue, skb);
7051da177e4SLinus Torvalds 	} else {
706de903512SRoland Dreier 		++dev->stats.tx_dropped;
7071da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 
710943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
7161da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
717b63b70d8SShlomo Pongratz 	struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
718b63b70d8SShlomo Pongratz 	struct ipoib_header *header;
7191da177e4SLinus Torvalds 	unsigned long flags;
7201da177e4SLinus Torvalds 
721b63b70d8SShlomo Pongratz 	header = (struct ipoib_header *) skb->data;
722b63b70d8SShlomo Pongratz 
723b63b70d8SShlomo Pongratz 	if (unlikely(cb->hwaddr[4] == 0xff)) {
724b63b70d8SShlomo Pongratz 		/* multicast, arrange "if" according to probability */
725b63b70d8SShlomo Pongratz 		if ((header->proto != htons(ETH_P_IP)) &&
726b63b70d8SShlomo Pongratz 		    (header->proto != htons(ETH_P_IPV6)) &&
727b63b70d8SShlomo Pongratz 		    (header->proto != htons(ETH_P_ARP)) &&
728dc850b0eSPatrick McHardy 		    (header->proto != htons(ETH_P_RARP)) &&
729dc850b0eSPatrick McHardy 		    (header->proto != htons(ETH_P_TIPC))) {
730b63b70d8SShlomo Pongratz 			/* ethertype not supported by IPoIB */
73117e6abeeSDavid Miller 			++dev->stats.tx_dropped;
73217e6abeeSDavid Miller 			dev_kfree_skb_any(skb);
733b63b70d8SShlomo Pongratz 			return NETDEV_TX_OK;
73417e6abeeSDavid Miller 		}
735b63b70d8SShlomo Pongratz 		/* Add in the P_Key for multicast*/
736b63b70d8SShlomo Pongratz 		cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
737b63b70d8SShlomo Pongratz 		cb->hwaddr[9] = priv->pkey & 0xff;
738b63b70d8SShlomo Pongratz 
739b63b70d8SShlomo Pongratz 		neigh = ipoib_neigh_get(dev, cb->hwaddr);
740b63b70d8SShlomo Pongratz 		if (likely(neigh))
741b63b70d8SShlomo Pongratz 			goto send_using_neigh;
742b63b70d8SShlomo Pongratz 		ipoib_mcast_send(dev, cb->hwaddr, skb);
743b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
746b63b70d8SShlomo Pongratz 	/* unicast, arrange "switch" according to probability */
747b63b70d8SShlomo Pongratz 	switch (header->proto) {
748b63b70d8SShlomo Pongratz 	case htons(ETH_P_IP):
749b63b70d8SShlomo Pongratz 	case htons(ETH_P_IPV6):
750dc850b0eSPatrick McHardy 	case htons(ETH_P_TIPC):
751b63b70d8SShlomo Pongratz 		neigh = ipoib_neigh_get(dev, cb->hwaddr);
752b63b70d8SShlomo Pongratz 		if (unlikely(!neigh)) {
753b63b70d8SShlomo Pongratz 			neigh_add_path(skb, cb->hwaddr, dev);
754b63b70d8SShlomo Pongratz 			return NETDEV_TX_OK;
755b63b70d8SShlomo Pongratz 		}
756b63b70d8SShlomo Pongratz 		break;
757b63b70d8SShlomo Pongratz 	case htons(ETH_P_ARP):
758b63b70d8SShlomo Pongratz 	case htons(ETH_P_RARP):
759b63b70d8SShlomo Pongratz 		/* for unicast ARP and RARP should always perform path find */
760b63b70d8SShlomo Pongratz 		unicast_arp_send(skb, dev, cb);
761b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
762b63b70d8SShlomo Pongratz 	default:
763b63b70d8SShlomo Pongratz 		/* ethertype not supported by IPoIB */
764b63b70d8SShlomo Pongratz 		++dev->stats.tx_dropped;
765b63b70d8SShlomo Pongratz 		dev_kfree_skb_any(skb);
766b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
7678a7f7521SMichael S. Tsirkin 	}
7688a7f7521SMichael S. Tsirkin 
769b63b70d8SShlomo Pongratz send_using_neigh:
770b63b70d8SShlomo Pongratz 	/* note we now hold a ref to neigh */
771bafff974SOr Gerlitz 	if (ipoib_cm_get(neigh)) {
772bafff974SOr Gerlitz 		if (ipoib_cm_up(neigh)) {
773bafff974SOr Gerlitz 			ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
774b63b70d8SShlomo Pongratz 			goto unref;
775bafff974SOr Gerlitz 		}
776bafff974SOr Gerlitz 	} else if (neigh->ah) {
777b63b70d8SShlomo Pongratz 		ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
778b63b70d8SShlomo Pongratz 		goto unref;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
782943c246eSRoland Dreier 		spin_lock_irqsave(&priv->lock, flags);
7831da177e4SLinus Torvalds 		__skb_queue_tail(&neigh->queue, skb);
784943c246eSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
7851da177e4SLinus Torvalds 	} else {
786de903512SRoland Dreier 		++dev->stats.tx_dropped;
7871da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 
790b63b70d8SShlomo Pongratz unref:
791b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	return NETDEV_TX_OK;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds static void ipoib_timeout(struct net_device *dev)
7971da177e4SLinus Torvalds {
7981da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
7991da177e4SLinus Torvalds 
8004b2d319bSRoland Dreier 	ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
8014b2d319bSRoland Dreier 		   jiffies_to_msecs(jiffies - dev->trans_start));
8024b2d319bSRoland Dreier 	ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
8034b2d319bSRoland Dreier 		   netif_queue_stopped(dev),
8044b2d319bSRoland Dreier 		   priv->tx_head, priv->tx_tail);
8051da177e4SLinus Torvalds 	/* XXX reset QP, etc. */
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds static int ipoib_hard_header(struct sk_buff *skb,
8091da177e4SLinus Torvalds 			     struct net_device *dev,
8101da177e4SLinus Torvalds 			     unsigned short type,
8113b04dddeSStephen Hemminger 			     const void *daddr, const void *saddr, unsigned len)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	struct ipoib_header *header;
814b63b70d8SShlomo Pongratz 	struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	header = (struct ipoib_header *) skb_push(skb, sizeof *header);
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	header->proto = htons(type);
8191da177e4SLinus Torvalds 	header->reserved = 0;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	/*
822b63b70d8SShlomo Pongratz 	 * we don't rely on dst_entry structure,  always stuff the
823936d7de3SRoland Dreier 	 * destination address into skb->cb so we can figure out where
824936d7de3SRoland Dreier 	 * to send the packet later.
8251da177e4SLinus Torvalds 	 */
826936d7de3SRoland Dreier 	memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
8271da177e4SLinus Torvalds 
82883bdd3b9SDoug Ledford 	return sizeof *header;
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds static void ipoib_set_mcast_list(struct net_device *dev)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
8341da177e4SLinus Torvalds 
8357a343d4cSLeonid Arsh 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
8367a343d4cSLeonid Arsh 		ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
8377a343d4cSLeonid Arsh 		return;
8387a343d4cSLeonid Arsh 	}
8397a343d4cSLeonid Arsh 
8401ad62a19SMichael S. Tsirkin 	queue_work(ipoib_workqueue, &priv->restart_task);
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
843b63b70d8SShlomo Pongratz static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
8441da177e4SLinus Torvalds {
845b63b70d8SShlomo Pongratz 	/*
846b63b70d8SShlomo Pongratz 	 * Use only the address parts that contributes to spreading
847b63b70d8SShlomo Pongratz 	 * The subnet prefix is not used as one can not connect to
848b63b70d8SShlomo Pongratz 	 * same remote port (GUID) using the same remote QPN via two
849b63b70d8SShlomo Pongratz 	 * different subnets.
850b63b70d8SShlomo Pongratz 	 */
851b63b70d8SShlomo Pongratz 	 /* qpn octets[1:4) & port GUID octets[12:20) */
8529d1ad66eSShlomo Pongratz 	u32 *d32 = (u32 *) daddr;
853b63b70d8SShlomo Pongratz 	u32 hv;
8541da177e4SLinus Torvalds 
8559d1ad66eSShlomo Pongratz 	hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
856b63b70d8SShlomo Pongratz 	return hv & htbl->mask;
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
859b63b70d8SShlomo Pongratz struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
860b63b70d8SShlomo Pongratz {
861b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
862b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
863b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
864b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh = NULL;
865b63b70d8SShlomo Pongratz 	u32 hash_val;
866b63b70d8SShlomo Pongratz 
867b63b70d8SShlomo Pongratz 	rcu_read_lock_bh();
868b63b70d8SShlomo Pongratz 
869b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_bh(ntbl->htbl);
870b63b70d8SShlomo Pongratz 
871b63b70d8SShlomo Pongratz 	if (!htbl)
872b63b70d8SShlomo Pongratz 		goto out_unlock;
873b63b70d8SShlomo Pongratz 
874b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, daddr);
875b63b70d8SShlomo Pongratz 	for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
876b63b70d8SShlomo Pongratz 	     neigh != NULL;
877b63b70d8SShlomo Pongratz 	     neigh = rcu_dereference_bh(neigh->hnext)) {
878b63b70d8SShlomo Pongratz 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
879b63b70d8SShlomo Pongratz 			/* found, take one ref on behalf of the caller */
880b63b70d8SShlomo Pongratz 			if (!atomic_inc_not_zero(&neigh->refcnt)) {
881b63b70d8SShlomo Pongratz 				/* deleted */
882b63b70d8SShlomo Pongratz 				neigh = NULL;
883b63b70d8SShlomo Pongratz 				goto out_unlock;
884b63b70d8SShlomo Pongratz 			}
885b63b70d8SShlomo Pongratz 			neigh->alive = jiffies;
886b63b70d8SShlomo Pongratz 			goto out_unlock;
887b63b70d8SShlomo Pongratz 		}
888b63b70d8SShlomo Pongratz 	}
889b63b70d8SShlomo Pongratz 
890b63b70d8SShlomo Pongratz out_unlock:
891b63b70d8SShlomo Pongratz 	rcu_read_unlock_bh();
892b63b70d8SShlomo Pongratz 	return neigh;
893b63b70d8SShlomo Pongratz }
894b63b70d8SShlomo Pongratz 
895b63b70d8SShlomo Pongratz static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
896b63b70d8SShlomo Pongratz {
897b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
898b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
899b63b70d8SShlomo Pongratz 	unsigned long neigh_obsolete;
900b63b70d8SShlomo Pongratz 	unsigned long dt;
901b63b70d8SShlomo Pongratz 	unsigned long flags;
902b63b70d8SShlomo Pongratz 	int i;
903b63b70d8SShlomo Pongratz 
904b63b70d8SShlomo Pongratz 	if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
905b63b70d8SShlomo Pongratz 		return;
906b63b70d8SShlomo Pongratz 
907b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
908b63b70d8SShlomo Pongratz 
909b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
910b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
911b63b70d8SShlomo Pongratz 
912b63b70d8SShlomo Pongratz 	if (!htbl)
913b63b70d8SShlomo Pongratz 		goto out_unlock;
914b63b70d8SShlomo Pongratz 
915b63b70d8SShlomo Pongratz 	/* neigh is obsolete if it was idle for two GC periods */
916b63b70d8SShlomo Pongratz 	dt = 2 * arp_tbl.gc_interval;
917b63b70d8SShlomo Pongratz 	neigh_obsolete = jiffies - dt;
918b63b70d8SShlomo Pongratz 	/* handle possible race condition */
919b63b70d8SShlomo Pongratz 	if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
920b63b70d8SShlomo Pongratz 		goto out_unlock;
921b63b70d8SShlomo Pongratz 
922b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
923b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
924b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
925b63b70d8SShlomo Pongratz 
926b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
927b5120a6eSShlomo Pongratz 							  lockdep_is_held(&priv->lock))) != NULL) {
928b63b70d8SShlomo Pongratz 			/* was the neigh idle for two GC periods */
929b63b70d8SShlomo Pongratz 			if (time_after(neigh_obsolete, neigh->alive)) {
930b63b70d8SShlomo Pongratz 				rcu_assign_pointer(*np,
931b63b70d8SShlomo Pongratz 						   rcu_dereference_protected(neigh->hnext,
932b5120a6eSShlomo Pongratz 									     lockdep_is_held(&priv->lock)));
933b63b70d8SShlomo Pongratz 				/* remove from path/mc list */
934b63b70d8SShlomo Pongratz 				list_del(&neigh->list);
935b63b70d8SShlomo Pongratz 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
936b63b70d8SShlomo Pongratz 			} else {
937b63b70d8SShlomo Pongratz 				np = &neigh->hnext;
938b63b70d8SShlomo Pongratz 			}
939b63b70d8SShlomo Pongratz 
940b63b70d8SShlomo Pongratz 		}
941b63b70d8SShlomo Pongratz 	}
942b63b70d8SShlomo Pongratz 
943b63b70d8SShlomo Pongratz out_unlock:
944b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
945b63b70d8SShlomo Pongratz }
946b63b70d8SShlomo Pongratz 
947b63b70d8SShlomo Pongratz static void ipoib_reap_neigh(struct work_struct *work)
948b63b70d8SShlomo Pongratz {
949b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv =
950b63b70d8SShlomo Pongratz 		container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
951b63b70d8SShlomo Pongratz 
952b63b70d8SShlomo Pongratz 	__ipoib_reap_neigh(priv);
953b63b70d8SShlomo Pongratz 
954b63b70d8SShlomo Pongratz 	if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
955b63b70d8SShlomo Pongratz 		queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
956b63b70d8SShlomo Pongratz 				   arp_tbl.gc_interval);
957b63b70d8SShlomo Pongratz }
958b63b70d8SShlomo Pongratz 
959b63b70d8SShlomo Pongratz 
960b63b70d8SShlomo Pongratz static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
961732a2170SMoni Shoua 				      struct net_device *dev)
962d2e0655eSMichael S. Tsirkin {
963d2e0655eSMichael S. Tsirkin 	struct ipoib_neigh *neigh;
964d2e0655eSMichael S. Tsirkin 
965b63b70d8SShlomo Pongratz 	neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
966d2e0655eSMichael S. Tsirkin 	if (!neigh)
967d2e0655eSMichael S. Tsirkin 		return NULL;
968d2e0655eSMichael S. Tsirkin 
969732a2170SMoni Shoua 	neigh->dev = dev;
970b63b70d8SShlomo Pongratz 	memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
97182b39913SRoland Dreier 	skb_queue_head_init(&neigh->queue);
972b63b70d8SShlomo Pongratz 	INIT_LIST_HEAD(&neigh->list);
973839fcabaSMichael S. Tsirkin 	ipoib_cm_set(neigh, NULL);
974b63b70d8SShlomo Pongratz 	/* one ref on behalf of the caller */
975b63b70d8SShlomo Pongratz 	atomic_set(&neigh->refcnt, 1);
976d2e0655eSMichael S. Tsirkin 
977d2e0655eSMichael S. Tsirkin 	return neigh;
978d2e0655eSMichael S. Tsirkin }
979d2e0655eSMichael S. Tsirkin 
980b63b70d8SShlomo Pongratz struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
981b63b70d8SShlomo Pongratz 				      struct net_device *dev)
982d2e0655eSMichael S. Tsirkin {
983b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
984b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
985b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
986b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh;
987b63b70d8SShlomo Pongratz 	u32 hash_val;
988b63b70d8SShlomo Pongratz 
989b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
990b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
991b63b70d8SShlomo Pongratz 	if (!htbl) {
992b63b70d8SShlomo Pongratz 		neigh = NULL;
993b63b70d8SShlomo Pongratz 		goto out_unlock;
994b63b70d8SShlomo Pongratz 	}
995b63b70d8SShlomo Pongratz 
996b63b70d8SShlomo Pongratz 	/* need to add a new neigh, but maybe some other thread succeeded?
997b63b70d8SShlomo Pongratz 	 * recalc hash, maybe hash resize took place so we do a search
998b63b70d8SShlomo Pongratz 	 */
999b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, daddr);
1000b63b70d8SShlomo Pongratz 	for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1001b5120a6eSShlomo Pongratz 					       lockdep_is_held(&priv->lock));
1002b63b70d8SShlomo Pongratz 	     neigh != NULL;
1003b63b70d8SShlomo Pongratz 	     neigh = rcu_dereference_protected(neigh->hnext,
1004b5120a6eSShlomo Pongratz 					       lockdep_is_held(&priv->lock))) {
1005b63b70d8SShlomo Pongratz 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1006b63b70d8SShlomo Pongratz 			/* found, take one ref on behalf of the caller */
1007b63b70d8SShlomo Pongratz 			if (!atomic_inc_not_zero(&neigh->refcnt)) {
1008b63b70d8SShlomo Pongratz 				/* deleted */
1009b63b70d8SShlomo Pongratz 				neigh = NULL;
1010b63b70d8SShlomo Pongratz 				break;
1011b63b70d8SShlomo Pongratz 			}
1012b63b70d8SShlomo Pongratz 			neigh->alive = jiffies;
1013b63b70d8SShlomo Pongratz 			goto out_unlock;
1014b63b70d8SShlomo Pongratz 		}
1015b63b70d8SShlomo Pongratz 	}
1016b63b70d8SShlomo Pongratz 
1017b63b70d8SShlomo Pongratz 	neigh = ipoib_neigh_ctor(daddr, dev);
1018b63b70d8SShlomo Pongratz 	if (!neigh)
1019b63b70d8SShlomo Pongratz 		goto out_unlock;
1020b63b70d8SShlomo Pongratz 
1021b63b70d8SShlomo Pongratz 	/* one ref on behalf of the hash table */
1022b63b70d8SShlomo Pongratz 	atomic_inc(&neigh->refcnt);
1023b63b70d8SShlomo Pongratz 	neigh->alive = jiffies;
1024b63b70d8SShlomo Pongratz 	/* put in hash */
1025b63b70d8SShlomo Pongratz 	rcu_assign_pointer(neigh->hnext,
1026b63b70d8SShlomo Pongratz 			   rcu_dereference_protected(htbl->buckets[hash_val],
1027b5120a6eSShlomo Pongratz 						     lockdep_is_held(&priv->lock)));
1028b63b70d8SShlomo Pongratz 	rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1029b63b70d8SShlomo Pongratz 	atomic_inc(&ntbl->entries);
1030b63b70d8SShlomo Pongratz 
1031b63b70d8SShlomo Pongratz out_unlock:
1032b63b70d8SShlomo Pongratz 
1033b63b70d8SShlomo Pongratz 	return neigh;
1034b63b70d8SShlomo Pongratz }
1035b63b70d8SShlomo Pongratz 
1036b63b70d8SShlomo Pongratz void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1037b63b70d8SShlomo Pongratz {
1038b63b70d8SShlomo Pongratz 	/* neigh reference count was dropprd to zero */
1039b63b70d8SShlomo Pongratz 	struct net_device *dev = neigh->dev;
1040b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
10412745b5b7SMichael S. Tsirkin 	struct sk_buff *skb;
1042b63b70d8SShlomo Pongratz 	if (neigh->ah)
1043b63b70d8SShlomo Pongratz 		ipoib_put_ah(neigh->ah);
10442745b5b7SMichael S. Tsirkin 	while ((skb = __skb_dequeue(&neigh->queue))) {
1045de903512SRoland Dreier 		++dev->stats.tx_dropped;
10462745b5b7SMichael S. Tsirkin 		dev_kfree_skb_any(skb);
10472745b5b7SMichael S. Tsirkin 	}
1048839fcabaSMichael S. Tsirkin 	if (ipoib_cm_get(neigh))
1049839fcabaSMichael S. Tsirkin 		ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1050b63b70d8SShlomo Pongratz 	ipoib_dbg(netdev_priv(dev),
1051b63b70d8SShlomo Pongratz 		  "neigh free for %06x %pI6\n",
1052b63b70d8SShlomo Pongratz 		  IPOIB_QPN(neigh->daddr),
1053b63b70d8SShlomo Pongratz 		  neigh->daddr + 4);
1054d2e0655eSMichael S. Tsirkin 	kfree(neigh);
1055b63b70d8SShlomo Pongratz 	if (atomic_dec_and_test(&priv->ntbl.entries)) {
1056b63b70d8SShlomo Pongratz 		if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1057b63b70d8SShlomo Pongratz 			complete(&priv->ntbl.flushed);
1058b63b70d8SShlomo Pongratz 	}
1059d2e0655eSMichael S. Tsirkin }
1060d2e0655eSMichael S. Tsirkin 
1061b63b70d8SShlomo Pongratz static void ipoib_neigh_reclaim(struct rcu_head *rp)
10621da177e4SLinus Torvalds {
1063b63b70d8SShlomo Pongratz 	/* Called as a result of removal from hash table */
1064b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1065b63b70d8SShlomo Pongratz 	/* note TX context may hold another ref */
1066b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
1067b63b70d8SShlomo Pongratz }
1068b63b70d8SShlomo Pongratz 
1069b63b70d8SShlomo Pongratz void ipoib_neigh_free(struct ipoib_neigh *neigh)
1070b63b70d8SShlomo Pongratz {
1071b63b70d8SShlomo Pongratz 	struct net_device *dev = neigh->dev;
1072b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1073b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1074b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1075b63b70d8SShlomo Pongratz 	struct ipoib_neigh __rcu **np;
1076b63b70d8SShlomo Pongratz 	struct ipoib_neigh *n;
1077b63b70d8SShlomo Pongratz 	u32 hash_val;
1078b63b70d8SShlomo Pongratz 
1079b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1080b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock));
1081b63b70d8SShlomo Pongratz 	if (!htbl)
1082b5120a6eSShlomo Pongratz 		return;
1083b63b70d8SShlomo Pongratz 
1084b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1085b63b70d8SShlomo Pongratz 	np = &htbl->buckets[hash_val];
1086b63b70d8SShlomo Pongratz 	for (n = rcu_dereference_protected(*np,
1087b5120a6eSShlomo Pongratz 					    lockdep_is_held(&priv->lock));
1088b63b70d8SShlomo Pongratz 	     n != NULL;
10896c723a68SShlomo Pongratz 	     n = rcu_dereference_protected(*np,
1090b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock))) {
1091b63b70d8SShlomo Pongratz 		if (n == neigh) {
1092b63b70d8SShlomo Pongratz 			/* found */
1093b63b70d8SShlomo Pongratz 			rcu_assign_pointer(*np,
1094b63b70d8SShlomo Pongratz 					   rcu_dereference_protected(neigh->hnext,
1095b5120a6eSShlomo Pongratz 								     lockdep_is_held(&priv->lock)));
109649b8e744SJim Foraker 			/* remove from parent list */
109749b8e744SJim Foraker 			list_del(&neigh->list);
1098b63b70d8SShlomo Pongratz 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1099b5120a6eSShlomo Pongratz 			return;
1100b63b70d8SShlomo Pongratz 		} else {
1101b63b70d8SShlomo Pongratz 			np = &n->hnext;
1102b63b70d8SShlomo Pongratz 		}
1103b63b70d8SShlomo Pongratz 	}
1104b63b70d8SShlomo Pongratz }
1105b63b70d8SShlomo Pongratz 
1106b63b70d8SShlomo Pongratz static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1107b63b70d8SShlomo Pongratz {
1108b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1109b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1110b63b70d8SShlomo Pongratz 	struct ipoib_neigh **buckets;
1111b63b70d8SShlomo Pongratz 	u32 size;
1112b63b70d8SShlomo Pongratz 
1113b63b70d8SShlomo Pongratz 	clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1114b63b70d8SShlomo Pongratz 	ntbl->htbl = NULL;
1115b63b70d8SShlomo Pongratz 	htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1116b63b70d8SShlomo Pongratz 	if (!htbl)
1117b63b70d8SShlomo Pongratz 		return -ENOMEM;
1118b63b70d8SShlomo Pongratz 	set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1119b63b70d8SShlomo Pongratz 	size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1120b63b70d8SShlomo Pongratz 	buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1121b63b70d8SShlomo Pongratz 	if (!buckets) {
1122b63b70d8SShlomo Pongratz 		kfree(htbl);
1123b63b70d8SShlomo Pongratz 		return -ENOMEM;
1124b63b70d8SShlomo Pongratz 	}
1125b63b70d8SShlomo Pongratz 	htbl->size = size;
1126b63b70d8SShlomo Pongratz 	htbl->mask = (size - 1);
1127b63b70d8SShlomo Pongratz 	htbl->buckets = buckets;
1128b63b70d8SShlomo Pongratz 	ntbl->htbl = htbl;
112966172c09SShlomo Pongratz 	htbl->ntbl = ntbl;
1130b63b70d8SShlomo Pongratz 	atomic_set(&ntbl->entries, 0);
1131b63b70d8SShlomo Pongratz 
1132b63b70d8SShlomo Pongratz 	/* start garbage collection */
1133b63b70d8SShlomo Pongratz 	clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1134b63b70d8SShlomo Pongratz 	queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
1135b63b70d8SShlomo Pongratz 			   arp_tbl.gc_interval);
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	return 0;
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds 
1140b63b70d8SShlomo Pongratz static void neigh_hash_free_rcu(struct rcu_head *head)
1141b63b70d8SShlomo Pongratz {
1142b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl = container_of(head,
1143b63b70d8SShlomo Pongratz 						    struct ipoib_neigh_hash,
1144b63b70d8SShlomo Pongratz 						    rcu);
1145b63b70d8SShlomo Pongratz 	struct ipoib_neigh __rcu **buckets = htbl->buckets;
114666172c09SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = htbl->ntbl;
1147b63b70d8SShlomo Pongratz 
1148b63b70d8SShlomo Pongratz 	kfree(buckets);
1149b63b70d8SShlomo Pongratz 	kfree(htbl);
115066172c09SShlomo Pongratz 	complete(&ntbl->deleted);
1151b63b70d8SShlomo Pongratz }
1152b63b70d8SShlomo Pongratz 
1153b63b70d8SShlomo Pongratz void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1154b63b70d8SShlomo Pongratz {
1155b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1156b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1157b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1158b63b70d8SShlomo Pongratz 	unsigned long flags;
1159b63b70d8SShlomo Pongratz 	int i;
1160b63b70d8SShlomo Pongratz 
1161b63b70d8SShlomo Pongratz 	/* remove all neigh connected to a given path or mcast */
1162b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1163b63b70d8SShlomo Pongratz 
1164b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1165b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
1166b63b70d8SShlomo Pongratz 
1167b63b70d8SShlomo Pongratz 	if (!htbl)
1168b63b70d8SShlomo Pongratz 		goto out_unlock;
1169b63b70d8SShlomo Pongratz 
1170b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1171b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1172b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1173b63b70d8SShlomo Pongratz 
1174b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1175b5120a6eSShlomo Pongratz 							  lockdep_is_held(&priv->lock))) != NULL) {
1176b63b70d8SShlomo Pongratz 			/* delete neighs belong to this parent */
1177b63b70d8SShlomo Pongratz 			if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1178b63b70d8SShlomo Pongratz 				rcu_assign_pointer(*np,
1179b63b70d8SShlomo Pongratz 						   rcu_dereference_protected(neigh->hnext,
1180b5120a6eSShlomo Pongratz 									     lockdep_is_held(&priv->lock)));
1181b63b70d8SShlomo Pongratz 				/* remove from parent list */
1182b63b70d8SShlomo Pongratz 				list_del(&neigh->list);
1183b63b70d8SShlomo Pongratz 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1184b63b70d8SShlomo Pongratz 			} else {
1185b63b70d8SShlomo Pongratz 				np = &neigh->hnext;
1186b63b70d8SShlomo Pongratz 			}
1187b63b70d8SShlomo Pongratz 
1188b63b70d8SShlomo Pongratz 		}
1189b63b70d8SShlomo Pongratz 	}
1190b63b70d8SShlomo Pongratz out_unlock:
1191b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
1192b63b70d8SShlomo Pongratz }
1193b63b70d8SShlomo Pongratz 
1194b63b70d8SShlomo Pongratz static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1195b63b70d8SShlomo Pongratz {
1196b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1197b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1198b63b70d8SShlomo Pongratz 	unsigned long flags;
119966172c09SShlomo Pongratz 	int i, wait_flushed = 0;
1200b63b70d8SShlomo Pongratz 
120166172c09SShlomo Pongratz 	init_completion(&priv->ntbl.flushed);
1202b63b70d8SShlomo Pongratz 
1203b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1204b63b70d8SShlomo Pongratz 
1205b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1206b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock));
1207b63b70d8SShlomo Pongratz 	if (!htbl)
1208b63b70d8SShlomo Pongratz 		goto out_unlock;
1209b63b70d8SShlomo Pongratz 
121066172c09SShlomo Pongratz 	wait_flushed = atomic_read(&priv->ntbl.entries);
121166172c09SShlomo Pongratz 	if (!wait_flushed)
121266172c09SShlomo Pongratz 		goto free_htbl;
121366172c09SShlomo Pongratz 
1214b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1215b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1216b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1217b63b70d8SShlomo Pongratz 
1218b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1219b5120a6eSShlomo Pongratz 				       lockdep_is_held(&priv->lock))) != NULL) {
1220b63b70d8SShlomo Pongratz 			rcu_assign_pointer(*np,
1221b63b70d8SShlomo Pongratz 					   rcu_dereference_protected(neigh->hnext,
1222b5120a6eSShlomo Pongratz 								     lockdep_is_held(&priv->lock)));
1223b63b70d8SShlomo Pongratz 			/* remove from path/mc list */
1224b63b70d8SShlomo Pongratz 			list_del(&neigh->list);
1225b63b70d8SShlomo Pongratz 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1226b63b70d8SShlomo Pongratz 		}
1227b63b70d8SShlomo Pongratz 	}
1228b63b70d8SShlomo Pongratz 
122966172c09SShlomo Pongratz free_htbl:
1230b63b70d8SShlomo Pongratz 	rcu_assign_pointer(ntbl->htbl, NULL);
1231b63b70d8SShlomo Pongratz 	call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1232b63b70d8SShlomo Pongratz 
1233b63b70d8SShlomo Pongratz out_unlock:
1234b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
123566172c09SShlomo Pongratz 	if (wait_flushed)
123666172c09SShlomo Pongratz 		wait_for_completion(&priv->ntbl.flushed);
1237b63b70d8SShlomo Pongratz }
1238b63b70d8SShlomo Pongratz 
1239b63b70d8SShlomo Pongratz static void ipoib_neigh_hash_uninit(struct net_device *dev)
1240b63b70d8SShlomo Pongratz {
1241b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1242b63b70d8SShlomo Pongratz 	int stopped;
1243b63b70d8SShlomo Pongratz 
1244b63b70d8SShlomo Pongratz 	ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
124566172c09SShlomo Pongratz 	init_completion(&priv->ntbl.deleted);
1246b63b70d8SShlomo Pongratz 	set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1247b63b70d8SShlomo Pongratz 
1248b63b70d8SShlomo Pongratz 	/* Stop GC if called at init fail need to cancel work */
1249b63b70d8SShlomo Pongratz 	stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1250b63b70d8SShlomo Pongratz 	if (!stopped)
1251b63b70d8SShlomo Pongratz 		cancel_delayed_work(&priv->neigh_reap_task);
1252b63b70d8SShlomo Pongratz 
1253b63b70d8SShlomo Pongratz 	ipoib_flush_neighs(priv);
125466172c09SShlomo Pongratz 
125566172c09SShlomo Pongratz 	wait_for_completion(&priv->ntbl.deleted);
1256b63b70d8SShlomo Pongratz }
1257b63b70d8SShlomo Pongratz 
1258b63b70d8SShlomo Pongratz 
12591da177e4SLinus Torvalds int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
12601da177e4SLinus Torvalds {
12611da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
12621da177e4SLinus Torvalds 
1263b63b70d8SShlomo Pongratz 	if (ipoib_neigh_hash_init(priv) < 0)
1264b63b70d8SShlomo Pongratz 		goto out;
12651da177e4SLinus Torvalds 	/* Allocate RX/TX "rings" to hold queued skbs */
12660f485251SShirley Ma 	priv->rx_ring =	kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
12671da177e4SLinus Torvalds 				GFP_KERNEL);
12681da177e4SLinus Torvalds 	if (!priv->rx_ring) {
12691da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
12700f485251SShirley Ma 		       ca->name, ipoib_recvq_size);
1271b63b70d8SShlomo Pongratz 		goto out_neigh_hash_cleanup;
12721da177e4SLinus Torvalds 	}
12731da177e4SLinus Torvalds 
1274948579cdSJoe Perches 	priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
12751da177e4SLinus Torvalds 	if (!priv->tx_ring) {
12761da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
12770f485251SShirley Ma 		       ca->name, ipoib_sendq_size);
12781da177e4SLinus Torvalds 		goto out_rx_ring_cleanup;
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds 
12811b524963SMichael S. Tsirkin 	/* priv->tx_head, tx_tail & tx_outstanding are already 0 */
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	if (ipoib_ib_dev_init(dev, ca, port))
12841da177e4SLinus Torvalds 		goto out_tx_ring_cleanup;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	return 0;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds out_tx_ring_cleanup:
128910313cbbSRoland Dreier 	vfree(priv->tx_ring);
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds out_rx_ring_cleanup:
12921da177e4SLinus Torvalds 	kfree(priv->rx_ring);
12931da177e4SLinus Torvalds 
1294b63b70d8SShlomo Pongratz out_neigh_hash_cleanup:
1295b63b70d8SShlomo Pongratz 	ipoib_neigh_hash_uninit(dev);
12961da177e4SLinus Torvalds out:
12971da177e4SLinus Torvalds 	return -ENOMEM;
12981da177e4SLinus Torvalds }
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds void ipoib_dev_cleanup(struct net_device *dev)
13011da177e4SLinus Torvalds {
13021da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
13039baa0b03SOr Gerlitz 	LIST_HEAD(head);
13049baa0b03SOr Gerlitz 
13059baa0b03SOr Gerlitz 	ASSERT_RTNL();
13061da177e4SLinus Torvalds 
13071732b0efSRoland Dreier 	ipoib_delete_debug_files(dev);
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	/* Delete any child interfaces first */
13101da177e4SLinus Torvalds 	list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1311b63b70d8SShlomo Pongratz 		/* Stop GC on child */
1312b63b70d8SShlomo Pongratz 		set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1313b63b70d8SShlomo Pongratz 		cancel_delayed_work(&cpriv->neigh_reap_task);
13149baa0b03SOr Gerlitz 		unregister_netdevice_queue(cpriv->dev, &head);
13151da177e4SLinus Torvalds 	}
13169baa0b03SOr Gerlitz 	unregister_netdevice_many(&head);
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	ipoib_ib_dev_cleanup(dev);
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	kfree(priv->rx_ring);
132110313cbbSRoland Dreier 	vfree(priv->tx_ring);
132292a6b34bSHal Rosenstock 
132392a6b34bSHal Rosenstock 	priv->rx_ring = NULL;
13241da177e4SLinus Torvalds 	priv->tx_ring = NULL;
1325b63b70d8SShlomo Pongratz 
1326b63b70d8SShlomo Pongratz 	ipoib_neigh_hash_uninit(dev);
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
13293b04dddeSStephen Hemminger static const struct header_ops ipoib_header_ops = {
13303b04dddeSStephen Hemminger 	.create	= ipoib_hard_header,
13313b04dddeSStephen Hemminger };
13323b04dddeSStephen Hemminger 
1333fe8114e8SStephen Hemminger static const struct net_device_ops ipoib_netdev_ops = {
13349baa0b03SOr Gerlitz 	.ndo_uninit		 = ipoib_uninit,
1335fe8114e8SStephen Hemminger 	.ndo_open		 = ipoib_open,
1336fe8114e8SStephen Hemminger 	.ndo_stop		 = ipoib_stop,
1337fe8114e8SStephen Hemminger 	.ndo_change_mtu		 = ipoib_change_mtu,
13383d96c74dSMichał Mirosław 	.ndo_fix_features	 = ipoib_fix_features,
1339fe8114e8SStephen Hemminger 	.ndo_start_xmit	 	 = ipoib_start_xmit,
1340fe8114e8SStephen Hemminger 	.ndo_tx_timeout		 = ipoib_timeout,
1341afc4b13dSJiri Pirko 	.ndo_set_rx_mode	 = ipoib_set_mcast_list,
1342fe8114e8SStephen Hemminger };
1343fe8114e8SStephen Hemminger 
13449baa0b03SOr Gerlitz void ipoib_setup(struct net_device *dev)
13451da177e4SLinus Torvalds {
13461da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
13471da177e4SLinus Torvalds 
1348fe8114e8SStephen Hemminger 	dev->netdev_ops		 = &ipoib_netdev_ops;
13493b04dddeSStephen Hemminger 	dev->header_ops		 = &ipoib_header_ops;
1350bea3348eSStephen Hemminger 
135182c24c18SEli Cohen 	ipoib_set_ethtool_ops(dev);
135282c24c18SEli Cohen 
1353bea3348eSStephen Hemminger 	netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	dev->watchdog_timeo	 = HZ;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	dev->flags		|= IFF_BROADCAST | IFF_MULTICAST;
13581da177e4SLinus Torvalds 
1359936d7de3SRoland Dreier 	dev->hard_header_len	 = IPOIB_ENCAP_LEN;
13601da177e4SLinus Torvalds 	dev->addr_len		 = INFINIBAND_ALEN;
13611da177e4SLinus Torvalds 	dev->type		 = ARPHRD_INFINIBAND;
13620f485251SShirley Ma 	dev->tx_queue_len	 = ipoib_sendq_size * 2;
1363eb14032fSEli Cohen 	dev->features		 = (NETIF_F_VLAN_CHALLENGED	|
1364eb14032fSEli Cohen 				    NETIF_F_HIGHDMA);
136586d15cd8SEric Dumazet 	dev->priv_flags		&= ~IFF_XMIT_DST_RELEASE;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	netif_carrier_off(dev);
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	priv->dev = dev;
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
13741da177e4SLinus Torvalds 
137595ed644fSIngo Molnar 	mutex_init(&priv->vlan_mutex);
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->path_list);
13781da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->child_intfs);
13791da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->dead_ahs);
13801da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->multicast_list);
13811da177e4SLinus Torvalds 
138226bbf13cSYosef Etigin 	INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
1383c4028958SDavid Howells 	INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
1384e8224e4bSYossi Etigin 	INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1385ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
1386ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
1387ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
1388c4028958SDavid Howells 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1389c4028958SDavid Howells 	INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1390b63b70d8SShlomo Pongratz 	INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
13941da177e4SLinus Torvalds {
13951da177e4SLinus Torvalds 	struct net_device *dev;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 	dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
13981da177e4SLinus Torvalds 			   ipoib_setup);
13991da177e4SLinus Torvalds 	if (!dev)
14001da177e4SLinus Torvalds 		return NULL;
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	return netdev_priv(dev);
14031da177e4SLinus Torvalds }
14041da177e4SLinus Torvalds 
140543cb76d9SGreg Kroah-Hartman static ssize_t show_pkey(struct device *dev,
140643cb76d9SGreg Kroah-Hartman 			 struct device_attribute *attr, char *buf)
14071da177e4SLinus Torvalds {
140843cb76d9SGreg Kroah-Hartman 	struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	return sprintf(buf, "0x%04x\n", priv->pkey);
14111da177e4SLinus Torvalds }
141243cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
14131da177e4SLinus Torvalds 
1414335a64a5SOr Gerlitz static ssize_t show_umcast(struct device *dev,
1415335a64a5SOr Gerlitz 			   struct device_attribute *attr, char *buf)
1416335a64a5SOr Gerlitz {
1417335a64a5SOr Gerlitz 	struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1418335a64a5SOr Gerlitz 
1419335a64a5SOr Gerlitz 	return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1420335a64a5SOr Gerlitz }
1421335a64a5SOr Gerlitz 
1422862096a8SOr Gerlitz void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
1423335a64a5SOr Gerlitz {
1424862096a8SOr Gerlitz 	struct ipoib_dev_priv *priv = netdev_priv(ndev);
1425335a64a5SOr Gerlitz 
1426335a64a5SOr Gerlitz 	if (umcast_val > 0) {
1427335a64a5SOr Gerlitz 		set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1428335a64a5SOr Gerlitz 		ipoib_warn(priv, "ignoring multicast groups joined directly "
1429335a64a5SOr Gerlitz 				"by userspace\n");
1430335a64a5SOr Gerlitz 	} else
1431335a64a5SOr Gerlitz 		clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1432862096a8SOr Gerlitz }
1433862096a8SOr Gerlitz 
1434862096a8SOr Gerlitz static ssize_t set_umcast(struct device *dev,
1435862096a8SOr Gerlitz 			  struct device_attribute *attr,
1436862096a8SOr Gerlitz 			  const char *buf, size_t count)
1437862096a8SOr Gerlitz {
1438862096a8SOr Gerlitz 	unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1439862096a8SOr Gerlitz 
1440862096a8SOr Gerlitz 	ipoib_set_umcast(to_net_dev(dev), umcast_val);
1441335a64a5SOr Gerlitz 
1442335a64a5SOr Gerlitz 	return count;
1443335a64a5SOr Gerlitz }
1444335a64a5SOr Gerlitz static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1445335a64a5SOr Gerlitz 
1446335a64a5SOr Gerlitz int ipoib_add_umcast_attr(struct net_device *dev)
1447335a64a5SOr Gerlitz {
1448335a64a5SOr Gerlitz 	return device_create_file(&dev->dev, &dev_attr_umcast);
1449335a64a5SOr Gerlitz }
1450335a64a5SOr Gerlitz 
145143cb76d9SGreg Kroah-Hartman static ssize_t create_child(struct device *dev,
145243cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr,
14531da177e4SLinus Torvalds 			    const char *buf, size_t count)
14541da177e4SLinus Torvalds {
14551da177e4SLinus Torvalds 	int pkey;
14561da177e4SLinus Torvalds 	int ret;
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
14591da177e4SLinus Torvalds 		return -EINVAL;
14601da177e4SLinus Torvalds 
14613d790a4cSOr Gerlitz 	if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
14621da177e4SLinus Torvalds 		return -EINVAL;
14631da177e4SLinus Torvalds 
14644ce05937SRoland Dreier 	/*
14654ce05937SRoland Dreier 	 * Set the full membership bit, so that we join the right
14664ce05937SRoland Dreier 	 * broadcast group, etc.
14674ce05937SRoland Dreier 	 */
14684ce05937SRoland Dreier 	pkey |= 0x8000;
14694ce05937SRoland Dreier 
147043cb76d9SGreg Kroah-Hartman 	ret = ipoib_vlan_add(to_net_dev(dev), pkey);
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	return ret ? ret : count;
14731da177e4SLinus Torvalds }
14747a52b34bSOr Gerlitz static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
14751da177e4SLinus Torvalds 
147643cb76d9SGreg Kroah-Hartman static ssize_t delete_child(struct device *dev,
147743cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr,
14781da177e4SLinus Torvalds 			    const char *buf, size_t count)
14791da177e4SLinus Torvalds {
14801da177e4SLinus Torvalds 	int pkey;
14811da177e4SLinus Torvalds 	int ret;
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
14841da177e4SLinus Torvalds 		return -EINVAL;
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 	if (pkey < 0 || pkey > 0xffff)
14871da177e4SLinus Torvalds 		return -EINVAL;
14881da177e4SLinus Torvalds 
148943cb76d9SGreg Kroah-Hartman 	ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	return ret ? ret : count;
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds }
14947a52b34bSOr Gerlitz static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds int ipoib_add_pkey_attr(struct net_device *dev)
14971da177e4SLinus Torvalds {
149843cb76d9SGreg Kroah-Hartman 	return device_create_file(&dev->dev, &dev_attr_pkey);
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
150183bb63f6SOr Gerlitz int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
150283bb63f6SOr Gerlitz {
150383bb63f6SOr Gerlitz 	struct ib_device_attr *device_attr;
150483bb63f6SOr Gerlitz 	int result = -ENOMEM;
150583bb63f6SOr Gerlitz 
150683bb63f6SOr Gerlitz 	device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
150783bb63f6SOr Gerlitz 	if (!device_attr) {
150883bb63f6SOr Gerlitz 		printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
150983bb63f6SOr Gerlitz 		       hca->name, sizeof *device_attr);
151083bb63f6SOr Gerlitz 		return result;
151183bb63f6SOr Gerlitz 	}
151283bb63f6SOr Gerlitz 
151383bb63f6SOr Gerlitz 	result = ib_query_device(hca, device_attr);
151483bb63f6SOr Gerlitz 	if (result) {
151583bb63f6SOr Gerlitz 		printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
151683bb63f6SOr Gerlitz 		       hca->name, result);
151783bb63f6SOr Gerlitz 		kfree(device_attr);
151883bb63f6SOr Gerlitz 		return result;
151983bb63f6SOr Gerlitz 	}
152083bb63f6SOr Gerlitz 	priv->hca_caps = device_attr->device_cap_flags;
152183bb63f6SOr Gerlitz 
152283bb63f6SOr Gerlitz 	kfree(device_attr);
152383bb63f6SOr Gerlitz 
152483bb63f6SOr Gerlitz 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
15253d96c74dSMichał Mirosław 		priv->dev->hw_features = NETIF_F_SG |
15263d96c74dSMichał Mirosław 			NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15273d96c74dSMichał Mirosław 
15283d96c74dSMichał Mirosław 		if (priv->hca_caps & IB_DEVICE_UD_TSO)
15293d96c74dSMichał Mirosław 			priv->dev->hw_features |= NETIF_F_TSO;
15303d96c74dSMichał Mirosław 
15313d96c74dSMichał Mirosław 		priv->dev->features |= priv->dev->hw_features;
153283bb63f6SOr Gerlitz 	}
153383bb63f6SOr Gerlitz 
153483bb63f6SOr Gerlitz 	return 0;
153583bb63f6SOr Gerlitz }
153683bb63f6SOr Gerlitz 
15371da177e4SLinus Torvalds static struct net_device *ipoib_add_port(const char *format,
15381da177e4SLinus Torvalds 					 struct ib_device *hca, u8 port)
15391da177e4SLinus Torvalds {
15401da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
1541bc7b3a36SShirley Ma 	struct ib_port_attr attr;
15421da177e4SLinus Torvalds 	int result = -ENOMEM;
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	priv = ipoib_intf_alloc(format);
15451da177e4SLinus Torvalds 	if (!priv)
15461da177e4SLinus Torvalds 		goto alloc_mem_failed;
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	SET_NETDEV_DEV(priv->dev, hca->dma_device);
1549c3aa9b18SEli Cohen 	priv->dev->dev_id = port - 1;
15501da177e4SLinus Torvalds 
1551bc7b3a36SShirley Ma 	if (!ib_query_port(hca, port, &attr))
1552bc7b3a36SShirley Ma 		priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1553bc7b3a36SShirley Ma 	else {
1554bc7b3a36SShirley Ma 		printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1555bc7b3a36SShirley Ma 		       hca->name, port);
1556bc7b3a36SShirley Ma 		goto device_init_failed;
1557bc7b3a36SShirley Ma 	}
1558bc7b3a36SShirley Ma 
1559bc7b3a36SShirley Ma 	/* MTU will be reset when mcast join happens */
1560bc7b3a36SShirley Ma 	priv->dev->mtu  = IPOIB_UD_MTU(priv->max_ib_mtu);
1561bc7b3a36SShirley Ma 	priv->mcast_mtu  = priv->admin_mtu = priv->dev->mtu;
1562bc7b3a36SShirley Ma 
1563596b9b68SDavid Miller 	priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1564596b9b68SDavid Miller 
15651da177e4SLinus Torvalds 	result = ib_query_pkey(hca, port, 0, &priv->pkey);
15661da177e4SLinus Torvalds 	if (result) {
15671da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
15681da177e4SLinus Torvalds 		       hca->name, port, result);
1569ca6de177SEli Cohen 		goto device_init_failed;
15701da177e4SLinus Torvalds 	}
15711da177e4SLinus Torvalds 
157283bb63f6SOr Gerlitz 	if (ipoib_set_dev_features(priv, hca))
15736046136cSEli Cohen 		goto device_init_failed;
1574af40da89SVladimir Sokolovsky 
15754ce05937SRoland Dreier 	/*
15764ce05937SRoland Dreier 	 * Set the full membership bit, so that we join the right
15774ce05937SRoland Dreier 	 * broadcast group, etc.
15784ce05937SRoland Dreier 	 */
15794ce05937SRoland Dreier 	priv->pkey |= 0x8000;
15804ce05937SRoland Dreier 
15811da177e4SLinus Torvalds 	priv->dev->broadcast[8] = priv->pkey >> 8;
15821da177e4SLinus Torvalds 	priv->dev->broadcast[9] = priv->pkey & 0xff;
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	result = ib_query_gid(hca, port, 0, &priv->local_gid);
15851da177e4SLinus Torvalds 	if (result) {
15861da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
15871da177e4SLinus Torvalds 		       hca->name, port, result);
1588ca6de177SEli Cohen 		goto device_init_failed;
15891da177e4SLinus Torvalds 	} else
15901da177e4SLinus Torvalds 		memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 	result = ipoib_dev_init(priv->dev, hca, port);
15931da177e4SLinus Torvalds 	if (result < 0) {
15941da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
15951da177e4SLinus Torvalds 		       hca->name, port, result);
15961da177e4SLinus Torvalds 		goto device_init_failed;
15971da177e4SLinus Torvalds 	}
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
16001da177e4SLinus Torvalds 			      priv->ca, ipoib_event);
16011da177e4SLinus Torvalds 	result = ib_register_event_handler(&priv->event_handler);
16021da177e4SLinus Torvalds 	if (result < 0) {
16031da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: ib_register_event_handler failed for "
16041da177e4SLinus Torvalds 		       "port %d (ret = %d)\n",
16051da177e4SLinus Torvalds 		       hca->name, port, result);
16061da177e4SLinus Torvalds 		goto event_failed;
16071da177e4SLinus Torvalds 	}
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	result = register_netdev(priv->dev);
16101da177e4SLinus Torvalds 	if (result) {
16111da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
16121da177e4SLinus Torvalds 		       hca->name, port, result);
16131da177e4SLinus Torvalds 		goto register_failed;
16141da177e4SLinus Torvalds 	}
16151da177e4SLinus Torvalds 
16161732b0efSRoland Dreier 	ipoib_create_debug_files(priv->dev);
16171da177e4SLinus Torvalds 
1618839fcabaSMichael S. Tsirkin 	if (ipoib_cm_add_mode_attr(priv->dev))
1619839fcabaSMichael S. Tsirkin 		goto sysfs_failed;
16201da177e4SLinus Torvalds 	if (ipoib_add_pkey_attr(priv->dev))
16211da177e4SLinus Torvalds 		goto sysfs_failed;
1622335a64a5SOr Gerlitz 	if (ipoib_add_umcast_attr(priv->dev))
1623335a64a5SOr Gerlitz 		goto sysfs_failed;
162443cb76d9SGreg Kroah-Hartman 	if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
16251da177e4SLinus Torvalds 		goto sysfs_failed;
162643cb76d9SGreg Kroah-Hartman 	if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
16271da177e4SLinus Torvalds 		goto sysfs_failed;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	return priv->dev;
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds sysfs_failed:
16321732b0efSRoland Dreier 	ipoib_delete_debug_files(priv->dev);
16331da177e4SLinus Torvalds 	unregister_netdev(priv->dev);
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds register_failed:
16361da177e4SLinus Torvalds 	ib_unregister_event_handler(&priv->event_handler);
1637b63b70d8SShlomo Pongratz 	/* Stop GC if started before flush */
1638b63b70d8SShlomo Pongratz 	set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1639b63b70d8SShlomo Pongratz 	cancel_delayed_work(&priv->neigh_reap_task);
1640a77a57a1SRoland Dreier 	flush_workqueue(ipoib_workqueue);
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds event_failed:
16431da177e4SLinus Torvalds 	ipoib_dev_cleanup(priv->dev);
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds device_init_failed:
16461da177e4SLinus Torvalds 	free_netdev(priv->dev);
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds alloc_mem_failed:
16491da177e4SLinus Torvalds 	return ERR_PTR(result);
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds static void ipoib_add_one(struct ib_device *device)
16531da177e4SLinus Torvalds {
16541da177e4SLinus Torvalds 	struct list_head *dev_list;
16551da177e4SLinus Torvalds 	struct net_device *dev;
16561da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
16571da177e4SLinus Torvalds 	int s, e, p;
16581da177e4SLinus Torvalds 
165907ebafbaSTom Tucker 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
166007ebafbaSTom Tucker 		return;
166107ebafbaSTom Tucker 
16621da177e4SLinus Torvalds 	dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
16631da177e4SLinus Torvalds 	if (!dev_list)
16641da177e4SLinus Torvalds 		return;
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	INIT_LIST_HEAD(dev_list);
16671da177e4SLinus Torvalds 
166807ebafbaSTom Tucker 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
16691da177e4SLinus Torvalds 		s = 0;
16701da177e4SLinus Torvalds 		e = 0;
16711da177e4SLinus Torvalds 	} else {
16721da177e4SLinus Torvalds 		s = 1;
16731da177e4SLinus Torvalds 		e = device->phys_port_cnt;
16741da177e4SLinus Torvalds 	}
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	for (p = s; p <= e; ++p) {
16777b4c8769SEli Cohen 		if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
16787b4c8769SEli Cohen 			continue;
16791da177e4SLinus Torvalds 		dev = ipoib_add_port("ib%d", device, p);
16801da177e4SLinus Torvalds 		if (!IS_ERR(dev)) {
16811da177e4SLinus Torvalds 			priv = netdev_priv(dev);
16821da177e4SLinus Torvalds 			list_add_tail(&priv->list, dev_list);
16831da177e4SLinus Torvalds 		}
16841da177e4SLinus Torvalds 	}
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	ib_set_client_data(device, &ipoib_client, dev_list);
16871da177e4SLinus Torvalds }
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds static void ipoib_remove_one(struct ib_device *device)
16901da177e4SLinus Torvalds {
16911da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv, *tmp;
16921da177e4SLinus Torvalds 	struct list_head *dev_list;
16931da177e4SLinus Torvalds 
169407ebafbaSTom Tucker 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
169507ebafbaSTom Tucker 		return;
169607ebafbaSTom Tucker 
16971da177e4SLinus Torvalds 	dev_list = ib_get_client_data(device, &ipoib_client);
16985a2815f0SItai Garbi 	if (!dev_list)
16995a2815f0SItai Garbi 		return;
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
17021da177e4SLinus Torvalds 		ib_unregister_event_handler(&priv->event_handler);
1703a77a57a1SRoland Dreier 
1704a77a57a1SRoland Dreier 		rtnl_lock();
1705a77a57a1SRoland Dreier 		dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1706a77a57a1SRoland Dreier 		rtnl_unlock();
1707a77a57a1SRoland Dreier 
1708b63b70d8SShlomo Pongratz 		/* Stop GC */
1709b63b70d8SShlomo Pongratz 		set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1710b63b70d8SShlomo Pongratz 		cancel_delayed_work(&priv->neigh_reap_task);
1711a77a57a1SRoland Dreier 		flush_workqueue(ipoib_workqueue);
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 		unregister_netdev(priv->dev);
17141da177e4SLinus Torvalds 		free_netdev(priv->dev);
17151da177e4SLinus Torvalds 	}
171606c56e44SMichael S. Tsirkin 
171706c56e44SMichael S. Tsirkin 	kfree(dev_list);
17181da177e4SLinus Torvalds }
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds static int __init ipoib_init_module(void)
17211da177e4SLinus Torvalds {
17221da177e4SLinus Torvalds 	int ret;
17231da177e4SLinus Torvalds 
17240f485251SShirley Ma 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
17250f485251SShirley Ma 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
17260f485251SShirley Ma 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
17270f485251SShirley Ma 
17280f485251SShirley Ma 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
17290f485251SShirley Ma 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1730732eacc0SHagen Paul Pfeifer 	ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
173168e995a2SPradeep Satyanarayana #ifdef CONFIG_INFINIBAND_IPOIB_CM
173268e995a2SPradeep Satyanarayana 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
173368e995a2SPradeep Satyanarayana #endif
17340f485251SShirley Ma 
1735f89271daSEli Cohen 	/*
1736f89271daSEli Cohen 	 * When copying small received packets, we only copy from the
1737f89271daSEli Cohen 	 * linear data part of the SKB, so we rely on this condition.
1738f89271daSEli Cohen 	 */
1739f89271daSEli Cohen 	BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1740f89271daSEli Cohen 
17411da177e4SLinus Torvalds 	ret = ipoib_register_debugfs();
17421da177e4SLinus Torvalds 	if (ret)
17431da177e4SLinus Torvalds 		return ret;
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 	/*
17461da177e4SLinus Torvalds 	 * We create our own workqueue mainly because we want to be
17471da177e4SLinus Torvalds 	 * able to flush it when devices are being removed.  We can't
17481da177e4SLinus Torvalds 	 * use schedule_work()/flush_scheduled_work() because both
17491da177e4SLinus Torvalds 	 * unregister_netdev() and linkwatch_event take the rtnl lock,
17501da177e4SLinus Torvalds 	 * so flush_scheduled_work() can deadlock during device
17511da177e4SLinus Torvalds 	 * removal.
17521da177e4SLinus Torvalds 	 */
17531da177e4SLinus Torvalds 	ipoib_workqueue = create_singlethread_workqueue("ipoib");
17541da177e4SLinus Torvalds 	if (!ipoib_workqueue) {
17551da177e4SLinus Torvalds 		ret = -ENOMEM;
17561da177e4SLinus Torvalds 		goto err_fs;
17571da177e4SLinus Torvalds 	}
17581da177e4SLinus Torvalds 
1759c1a0b23bSMichael S. Tsirkin 	ib_sa_register_client(&ipoib_sa_client);
1760c1a0b23bSMichael S. Tsirkin 
17611da177e4SLinus Torvalds 	ret = ib_register_client(&ipoib_client);
17621da177e4SLinus Torvalds 	if (ret)
1763c1a0b23bSMichael S. Tsirkin 		goto err_sa;
17641da177e4SLinus Torvalds 
17659baa0b03SOr Gerlitz 	ret = ipoib_netlink_init();
17669baa0b03SOr Gerlitz 	if (ret)
17679baa0b03SOr Gerlitz 		goto err_client;
17689baa0b03SOr Gerlitz 
17691da177e4SLinus Torvalds 	return 0;
17701da177e4SLinus Torvalds 
17719baa0b03SOr Gerlitz err_client:
17729baa0b03SOr Gerlitz 	ib_unregister_client(&ipoib_client);
17739baa0b03SOr Gerlitz 
1774c1a0b23bSMichael S. Tsirkin err_sa:
1775c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&ipoib_sa_client);
17761da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
17771da177e4SLinus Torvalds 
17789adec1a8SRoland Dreier err_fs:
17799adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
17809adec1a8SRoland Dreier 
17811da177e4SLinus Torvalds 	return ret;
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds static void __exit ipoib_cleanup_module(void)
17851da177e4SLinus Torvalds {
17869baa0b03SOr Gerlitz 	ipoib_netlink_fini();
17871da177e4SLinus Torvalds 	ib_unregister_client(&ipoib_client);
1788c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&ipoib_sa_client);
17899adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
17901da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
17911da177e4SLinus Torvalds }
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds module_init(ipoib_init_module);
17941da177e4SLinus Torvalds module_exit(ipoib_cleanup_module);
1795