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)) {
496839fcabaSMichael S. Tsirkin 					list_del(&neigh->list);
497b63b70d8SShlomo Pongratz 					ipoib_neigh_free(neigh);
498839fcabaSMichael S. Tsirkin 					continue;
499839fcabaSMichael S. Tsirkin 				}
500839fcabaSMichael S. Tsirkin 			}
501839fcabaSMichael S. Tsirkin 
5021da177e4SLinus Torvalds 			while ((skb = __skb_dequeue(&neigh->queue)))
5031da177e4SLinus Torvalds 				__skb_queue_tail(&skqueue, skb);
5041da177e4SLinus Torvalds 		}
505ee1e2c82SMoni Shoua 		path->valid = 1;
5065872a9fcSRoland Dreier 	}
5071da177e4SLinus Torvalds 
5085872a9fcSRoland Dreier 	path->query = NULL;
5091da177e4SLinus Torvalds 	complete(&path->done);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
5121da177e4SLinus Torvalds 
513f72dd566SRoland Dreier 	if (IS_ERR_OR_NULL(ah))
514f72dd566SRoland Dreier 		ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
515f72dd566SRoland Dreier 
516ee1e2c82SMoni Shoua 	if (old_ah)
517ee1e2c82SMoni Shoua 		ipoib_put_ah(old_ah);
518ee1e2c82SMoni Shoua 
5191da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&skqueue))) {
5201da177e4SLinus Torvalds 		skb->dev = dev;
5211da177e4SLinus Torvalds 		if (dev_queue_xmit(skb))
5221da177e4SLinus Torvalds 			ipoib_warn(priv, "dev_queue_xmit failed "
5231da177e4SLinus Torvalds 				   "to requeue packet\n");
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
52737c22a77SJack Morgenstein static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
5301da177e4SLinus Torvalds 	struct ipoib_path *path;
5311da177e4SLinus Torvalds 
5321401b53aSJack Morgenstein 	if (!priv->broadcast)
5331401b53aSJack Morgenstein 		return NULL;
5341401b53aSJack Morgenstein 
53521a38489SRoland Dreier 	path = kzalloc(sizeof *path, GFP_ATOMIC);
5361da177e4SLinus Torvalds 	if (!path)
5371da177e4SLinus Torvalds 		return NULL;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	path->dev = dev;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	skb_queue_head_init(&path->queue);
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	INIT_LIST_HEAD(&path->neigh_list);
5441da177e4SLinus Torvalds 
54537c22a77SJack Morgenstein 	memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
5461da177e4SLinus Torvalds 	path->pathrec.sgid	    = priv->local_gid;
5471da177e4SLinus Torvalds 	path->pathrec.pkey	    = cpu_to_be16(priv->pkey);
5481da177e4SLinus Torvalds 	path->pathrec.numb_path     = 1;
54981668838SSean Hefty 	path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	return path;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds static int path_rec_start(struct net_device *dev,
5551da177e4SLinus Torvalds 			  struct ipoib_path *path)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
5581da177e4SLinus Torvalds 
5595b095d98SHarvey Harrison 	ipoib_dbg(priv, "Start path record lookup for %pI6\n",
560fcace2feSHarvey Harrison 		  path->pathrec.dgid.raw);
5611da177e4SLinus Torvalds 
56265c7eddaSRoland Dreier 	init_completion(&path->done);
56365c7eddaSRoland Dreier 
5641da177e4SLinus Torvalds 	path->query_id =
565c1a0b23bSMichael S. Tsirkin 		ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
5661da177e4SLinus Torvalds 				   &path->pathrec,
5671da177e4SLinus Torvalds 				   IB_SA_PATH_REC_DGID		|
5681da177e4SLinus Torvalds 				   IB_SA_PATH_REC_SGID		|
5691da177e4SLinus Torvalds 				   IB_SA_PATH_REC_NUMB_PATH	|
57081668838SSean Hefty 				   IB_SA_PATH_REC_TRAFFIC_CLASS |
5711da177e4SLinus Torvalds 				   IB_SA_PATH_REC_PKEY,
5721da177e4SLinus Torvalds 				   1000, GFP_ATOMIC,
5731da177e4SLinus Torvalds 				   path_rec_completion,
5741da177e4SLinus Torvalds 				   path, &path->query);
5751da177e4SLinus Torvalds 	if (path->query_id < 0) {
57601b3fc8bSOr Gerlitz 		ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
5771da177e4SLinus Torvalds 		path->query = NULL;
57893a3ab93SYossi Etigin 		complete(&path->done);
5791da177e4SLinus Torvalds 		return path->query_id;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	return 0;
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
585b63b70d8SShlomo Pongratz static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
586b63b70d8SShlomo Pongratz 			   struct net_device *dev)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
5891da177e4SLinus Torvalds 	struct ipoib_path *path;
5901da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
591943c246eSRoland Dreier 	unsigned long flags;
5921da177e4SLinus Torvalds 
593b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
594b63b70d8SShlomo Pongratz 	neigh = ipoib_neigh_alloc(daddr, dev);
5951da177e4SLinus Torvalds 	if (!neigh) {
596b5120a6eSShlomo Pongratz 		spin_unlock_irqrestore(&priv->lock, flags);
597de903512SRoland Dreier 		++dev->stats.tx_dropped;
5981da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
5991da177e4SLinus Torvalds 		return;
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds 
602b63b70d8SShlomo Pongratz 	path = __path_find(dev, daddr + 4);
6031da177e4SLinus Torvalds 	if (!path) {
604b63b70d8SShlomo Pongratz 		path = path_rec_create(dev, daddr + 4);
6051da177e4SLinus Torvalds 		if (!path)
606d2e0655eSMichael S. Tsirkin 			goto err_path;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 		__path_add(dev, path);
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	list_add_tail(&neigh->list, &path->neigh_list);
6121da177e4SLinus Torvalds 
61347f7a071SMichael S. Tsirkin 	if (path->ah) {
6141da177e4SLinus Torvalds 		kref_get(&path->ah->ref);
6151da177e4SLinus Torvalds 		neigh->ah = path->ah;
6161da177e4SLinus Torvalds 
617b63b70d8SShlomo Pongratz 		if (ipoib_cm_enabled(dev, neigh->daddr)) {
618839fcabaSMichael S. Tsirkin 			if (!ipoib_cm_get(neigh))
619839fcabaSMichael S. Tsirkin 				ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
620839fcabaSMichael S. Tsirkin 			if (!ipoib_cm_get(neigh)) {
621839fcabaSMichael S. Tsirkin 				list_del(&neigh->list);
622b63b70d8SShlomo Pongratz 				ipoib_neigh_free(neigh);
623839fcabaSMichael S. Tsirkin 				goto err_drop;
624839fcabaSMichael S. Tsirkin 			}
625839fcabaSMichael S. Tsirkin 			if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
626839fcabaSMichael S. Tsirkin 				__skb_queue_tail(&neigh->queue, skb);
627839fcabaSMichael S. Tsirkin 			else {
628839fcabaSMichael S. Tsirkin 				ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
629839fcabaSMichael S. Tsirkin 					   skb_queue_len(&neigh->queue));
630839fcabaSMichael S. Tsirkin 				goto err_drop;
631839fcabaSMichael S. Tsirkin 			}
632721d67cdSRoland Dreier 		} else {
633721d67cdSRoland Dreier 			spin_unlock_irqrestore(&priv->lock, flags);
634b63b70d8SShlomo Pongratz 			ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
635b63b70d8SShlomo Pongratz 			ipoib_neigh_put(neigh);
636721d67cdSRoland Dreier 			return;
637721d67cdSRoland Dreier 		}
6381da177e4SLinus Torvalds 	} else {
6391da177e4SLinus Torvalds 		neigh->ah  = NULL;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 		if (!path->query && path_rec_start(dev, path))
642d2e0655eSMichael S. Tsirkin 			goto err_list;
6432745b5b7SMichael S. Tsirkin 
6442745b5b7SMichael S. Tsirkin 		__skb_queue_tail(&neigh->queue, skb);
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds 
647943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
648b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
6491da177e4SLinus Torvalds 	return;
6501da177e4SLinus Torvalds 
651d2e0655eSMichael S. Tsirkin err_list:
6521da177e4SLinus Torvalds 	list_del(&neigh->list);
6531da177e4SLinus Torvalds 
654d2e0655eSMichael S. Tsirkin err_path:
655b63b70d8SShlomo Pongratz 	ipoib_neigh_free(neigh);
656839fcabaSMichael S. Tsirkin err_drop:
657de903512SRoland Dreier 	++dev->stats.tx_dropped;
6581da177e4SLinus Torvalds 	dev_kfree_skb_any(skb);
6591da177e4SLinus Torvalds 
660943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
661b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
665936d7de3SRoland Dreier 			     struct ipoib_cb *cb)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
6681da177e4SLinus Torvalds 	struct ipoib_path *path;
669943c246eSRoland Dreier 	unsigned long flags;
6701da177e4SLinus Torvalds 
671943c246eSRoland Dreier 	spin_lock_irqsave(&priv->lock, flags);
6721da177e4SLinus Torvalds 
673936d7de3SRoland Dreier 	path = __path_find(dev, cb->hwaddr + 4);
674ee1e2c82SMoni Shoua 	if (!path || !path->valid) {
67571d98b46SJack Morgenstein 		int new_path = 0;
67671d98b46SJack Morgenstein 
67771d98b46SJack Morgenstein 		if (!path) {
678936d7de3SRoland Dreier 			path = path_rec_create(dev, cb->hwaddr + 4);
67971d98b46SJack Morgenstein 			new_path = 1;
68071d98b46SJack Morgenstein 		}
6811da177e4SLinus Torvalds 		if (path) {
6821da177e4SLinus Torvalds 			__skb_queue_tail(&path->queue, skb);
6831da177e4SLinus Torvalds 
684ff79ae80SYossi Etigin 			if (!path->query && path_rec_start(dev, path)) {
685943c246eSRoland Dreier 				spin_unlock_irqrestore(&priv->lock, flags);
68671d98b46SJack Morgenstein 				if (new_path)
6871da177e4SLinus Torvalds 					path_free(dev, path);
6881da177e4SLinus Torvalds 				return;
6891da177e4SLinus Torvalds 			} else
6901da177e4SLinus Torvalds 				__path_add(dev, path);
6911da177e4SLinus Torvalds 		} else {
692de903512SRoland Dreier 			++dev->stats.tx_dropped;
6931da177e4SLinus Torvalds 			dev_kfree_skb_any(skb);
6941da177e4SLinus Torvalds 		}
6951da177e4SLinus Torvalds 
696943c246eSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
6971da177e4SLinus Torvalds 		return;
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 
70047f7a071SMichael S. Tsirkin 	if (path->ah) {
7011da177e4SLinus Torvalds 		ipoib_dbg(priv, "Send unicast ARP to %04x\n",
7021da177e4SLinus Torvalds 			  be16_to_cpu(path->pathrec.dlid));
7031da177e4SLinus Torvalds 
704721d67cdSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
705936d7de3SRoland Dreier 		ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
706721d67cdSRoland Dreier 		return;
7071da177e4SLinus Torvalds 	} else if ((path->query || !path_rec_start(dev, path)) &&
7081da177e4SLinus Torvalds 		   skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
7091da177e4SLinus Torvalds 		__skb_queue_tail(&path->queue, skb);
7101da177e4SLinus Torvalds 	} else {
711de903512SRoland Dreier 		++dev->stats.tx_dropped;
7121da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 
715943c246eSRoland Dreier 	spin_unlock_irqrestore(&priv->lock, flags);
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
7211da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
722b63b70d8SShlomo Pongratz 	struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
723b63b70d8SShlomo Pongratz 	struct ipoib_header *header;
7241da177e4SLinus Torvalds 	unsigned long flags;
7251da177e4SLinus Torvalds 
726b63b70d8SShlomo Pongratz 	header = (struct ipoib_header *) skb->data;
727b63b70d8SShlomo Pongratz 
728b63b70d8SShlomo Pongratz 	if (unlikely(cb->hwaddr[4] == 0xff)) {
729b63b70d8SShlomo Pongratz 		/* multicast, arrange "if" according to probability */
730b63b70d8SShlomo Pongratz 		if ((header->proto != htons(ETH_P_IP)) &&
731b63b70d8SShlomo Pongratz 		    (header->proto != htons(ETH_P_IPV6)) &&
732b63b70d8SShlomo Pongratz 		    (header->proto != htons(ETH_P_ARP)) &&
733b63b70d8SShlomo Pongratz 		    (header->proto != htons(ETH_P_RARP))) {
734b63b70d8SShlomo Pongratz 			/* ethertype not supported by IPoIB */
73517e6abeeSDavid Miller 			++dev->stats.tx_dropped;
73617e6abeeSDavid Miller 			dev_kfree_skb_any(skb);
737b63b70d8SShlomo Pongratz 			return NETDEV_TX_OK;
73817e6abeeSDavid Miller 		}
739b63b70d8SShlomo Pongratz 		/* Add in the P_Key for multicast*/
740b63b70d8SShlomo Pongratz 		cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
741b63b70d8SShlomo Pongratz 		cb->hwaddr[9] = priv->pkey & 0xff;
742b63b70d8SShlomo Pongratz 
743b63b70d8SShlomo Pongratz 		neigh = ipoib_neigh_get(dev, cb->hwaddr);
744b63b70d8SShlomo Pongratz 		if (likely(neigh))
745b63b70d8SShlomo Pongratz 			goto send_using_neigh;
746b63b70d8SShlomo Pongratz 		ipoib_mcast_send(dev, cb->hwaddr, skb);
747b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 
750b63b70d8SShlomo Pongratz 	/* unicast, arrange "switch" according to probability */
751b63b70d8SShlomo Pongratz 	switch (header->proto) {
752b63b70d8SShlomo Pongratz 	case htons(ETH_P_IP):
753b63b70d8SShlomo Pongratz 	case htons(ETH_P_IPV6):
754b63b70d8SShlomo Pongratz 		neigh = ipoib_neigh_get(dev, cb->hwaddr);
755b63b70d8SShlomo Pongratz 		if (unlikely(!neigh)) {
756b63b70d8SShlomo Pongratz 			neigh_add_path(skb, cb->hwaddr, dev);
757b63b70d8SShlomo Pongratz 			return NETDEV_TX_OK;
758b63b70d8SShlomo Pongratz 		}
759b63b70d8SShlomo Pongratz 		break;
760b63b70d8SShlomo Pongratz 	case htons(ETH_P_ARP):
761b63b70d8SShlomo Pongratz 	case htons(ETH_P_RARP):
762b63b70d8SShlomo Pongratz 		/* for unicast ARP and RARP should always perform path find */
763b63b70d8SShlomo Pongratz 		unicast_arp_send(skb, dev, cb);
764b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
765b63b70d8SShlomo Pongratz 	default:
766b63b70d8SShlomo Pongratz 		/* ethertype not supported by IPoIB */
767b63b70d8SShlomo Pongratz 		++dev->stats.tx_dropped;
768b63b70d8SShlomo Pongratz 		dev_kfree_skb_any(skb);
769b63b70d8SShlomo Pongratz 		return NETDEV_TX_OK;
7708a7f7521SMichael S. Tsirkin 	}
7718a7f7521SMichael S. Tsirkin 
772b63b70d8SShlomo Pongratz send_using_neigh:
773b63b70d8SShlomo Pongratz 	/* note we now hold a ref to neigh */
774bafff974SOr Gerlitz 	if (ipoib_cm_get(neigh)) {
775bafff974SOr Gerlitz 		if (ipoib_cm_up(neigh)) {
776bafff974SOr Gerlitz 			ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
777b63b70d8SShlomo Pongratz 			goto unref;
778bafff974SOr Gerlitz 		}
779bafff974SOr Gerlitz 	} else if (neigh->ah) {
780b63b70d8SShlomo Pongratz 		ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
781b63b70d8SShlomo Pongratz 		goto unref;
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
785943c246eSRoland Dreier 		spin_lock_irqsave(&priv->lock, flags);
7861da177e4SLinus Torvalds 		__skb_queue_tail(&neigh->queue, skb);
787943c246eSRoland Dreier 		spin_unlock_irqrestore(&priv->lock, flags);
7881da177e4SLinus Torvalds 	} else {
789de903512SRoland Dreier 		++dev->stats.tx_dropped;
7901da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
7911da177e4SLinus Torvalds 	}
7921da177e4SLinus Torvalds 
793b63b70d8SShlomo Pongratz unref:
794b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	return NETDEV_TX_OK;
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds static void ipoib_timeout(struct net_device *dev)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
8021da177e4SLinus Torvalds 
8034b2d319bSRoland Dreier 	ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
8044b2d319bSRoland Dreier 		   jiffies_to_msecs(jiffies - dev->trans_start));
8054b2d319bSRoland Dreier 	ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
8064b2d319bSRoland Dreier 		   netif_queue_stopped(dev),
8074b2d319bSRoland Dreier 		   priv->tx_head, priv->tx_tail);
8081da177e4SLinus Torvalds 	/* XXX reset QP, etc. */
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds static int ipoib_hard_header(struct sk_buff *skb,
8121da177e4SLinus Torvalds 			     struct net_device *dev,
8131da177e4SLinus Torvalds 			     unsigned short type,
8143b04dddeSStephen Hemminger 			     const void *daddr, const void *saddr, unsigned len)
8151da177e4SLinus Torvalds {
8161da177e4SLinus Torvalds 	struct ipoib_header *header;
817b63b70d8SShlomo Pongratz 	struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	header = (struct ipoib_header *) skb_push(skb, sizeof *header);
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	header->proto = htons(type);
8221da177e4SLinus Torvalds 	header->reserved = 0;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	/*
825b63b70d8SShlomo Pongratz 	 * we don't rely on dst_entry structure,  always stuff the
826936d7de3SRoland Dreier 	 * destination address into skb->cb so we can figure out where
827936d7de3SRoland Dreier 	 * to send the packet later.
8281da177e4SLinus Torvalds 	 */
829936d7de3SRoland Dreier 	memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
8301da177e4SLinus Torvalds 
83183bdd3b9SDoug Ledford 	return sizeof *header;
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds static void ipoib_set_mcast_list(struct net_device *dev)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
8371da177e4SLinus Torvalds 
8387a343d4cSLeonid Arsh 	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
8397a343d4cSLeonid Arsh 		ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
8407a343d4cSLeonid Arsh 		return;
8417a343d4cSLeonid Arsh 	}
8427a343d4cSLeonid Arsh 
8431ad62a19SMichael S. Tsirkin 	queue_work(ipoib_workqueue, &priv->restart_task);
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
846b63b70d8SShlomo Pongratz static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
8471da177e4SLinus Torvalds {
848b63b70d8SShlomo Pongratz 	/*
849b63b70d8SShlomo Pongratz 	 * Use only the address parts that contributes to spreading
850b63b70d8SShlomo Pongratz 	 * The subnet prefix is not used as one can not connect to
851b63b70d8SShlomo Pongratz 	 * same remote port (GUID) using the same remote QPN via two
852b63b70d8SShlomo Pongratz 	 * different subnets.
853b63b70d8SShlomo Pongratz 	 */
854b63b70d8SShlomo Pongratz 	 /* qpn octets[1:4) & port GUID octets[12:20) */
8559d1ad66eSShlomo Pongratz 	u32 *d32 = (u32 *) daddr;
856b63b70d8SShlomo Pongratz 	u32 hv;
8571da177e4SLinus Torvalds 
8589d1ad66eSShlomo Pongratz 	hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
859b63b70d8SShlomo Pongratz 	return hv & htbl->mask;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
862b63b70d8SShlomo Pongratz struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
863b63b70d8SShlomo Pongratz {
864b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
865b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
866b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
867b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh = NULL;
868b63b70d8SShlomo Pongratz 	u32 hash_val;
869b63b70d8SShlomo Pongratz 
870b63b70d8SShlomo Pongratz 	rcu_read_lock_bh();
871b63b70d8SShlomo Pongratz 
872b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_bh(ntbl->htbl);
873b63b70d8SShlomo Pongratz 
874b63b70d8SShlomo Pongratz 	if (!htbl)
875b63b70d8SShlomo Pongratz 		goto out_unlock;
876b63b70d8SShlomo Pongratz 
877b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, daddr);
878b63b70d8SShlomo Pongratz 	for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
879b63b70d8SShlomo Pongratz 	     neigh != NULL;
880b63b70d8SShlomo Pongratz 	     neigh = rcu_dereference_bh(neigh->hnext)) {
881b63b70d8SShlomo Pongratz 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
882b63b70d8SShlomo Pongratz 			/* found, take one ref on behalf of the caller */
883b63b70d8SShlomo Pongratz 			if (!atomic_inc_not_zero(&neigh->refcnt)) {
884b63b70d8SShlomo Pongratz 				/* deleted */
885b63b70d8SShlomo Pongratz 				neigh = NULL;
886b63b70d8SShlomo Pongratz 				goto out_unlock;
887b63b70d8SShlomo Pongratz 			}
888b63b70d8SShlomo Pongratz 			neigh->alive = jiffies;
889b63b70d8SShlomo Pongratz 			goto out_unlock;
890b63b70d8SShlomo Pongratz 		}
891b63b70d8SShlomo Pongratz 	}
892b63b70d8SShlomo Pongratz 
893b63b70d8SShlomo Pongratz out_unlock:
894b63b70d8SShlomo Pongratz 	rcu_read_unlock_bh();
895b63b70d8SShlomo Pongratz 	return neigh;
896b63b70d8SShlomo Pongratz }
897b63b70d8SShlomo Pongratz 
898b63b70d8SShlomo Pongratz static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
899b63b70d8SShlomo Pongratz {
900b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
901b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
902b63b70d8SShlomo Pongratz 	unsigned long neigh_obsolete;
903b63b70d8SShlomo Pongratz 	unsigned long dt;
904b63b70d8SShlomo Pongratz 	unsigned long flags;
905b63b70d8SShlomo Pongratz 	int i;
906b63b70d8SShlomo Pongratz 
907b63b70d8SShlomo Pongratz 	if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
908b63b70d8SShlomo Pongratz 		return;
909b63b70d8SShlomo Pongratz 
910b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
911b63b70d8SShlomo Pongratz 
912b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
913b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
914b63b70d8SShlomo Pongratz 
915b63b70d8SShlomo Pongratz 	if (!htbl)
916b63b70d8SShlomo Pongratz 		goto out_unlock;
917b63b70d8SShlomo Pongratz 
918b63b70d8SShlomo Pongratz 	/* neigh is obsolete if it was idle for two GC periods */
919b63b70d8SShlomo Pongratz 	dt = 2 * arp_tbl.gc_interval;
920b63b70d8SShlomo Pongratz 	neigh_obsolete = jiffies - dt;
921b63b70d8SShlomo Pongratz 	/* handle possible race condition */
922b63b70d8SShlomo Pongratz 	if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
923b63b70d8SShlomo Pongratz 		goto out_unlock;
924b63b70d8SShlomo Pongratz 
925b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
926b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
927b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
928b63b70d8SShlomo Pongratz 
929b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
930b5120a6eSShlomo Pongratz 							  lockdep_is_held(&priv->lock))) != NULL) {
931b63b70d8SShlomo Pongratz 			/* was the neigh idle for two GC periods */
932b63b70d8SShlomo Pongratz 			if (time_after(neigh_obsolete, neigh->alive)) {
933b63b70d8SShlomo Pongratz 				rcu_assign_pointer(*np,
934b63b70d8SShlomo Pongratz 						   rcu_dereference_protected(neigh->hnext,
935b5120a6eSShlomo Pongratz 									     lockdep_is_held(&priv->lock)));
936b63b70d8SShlomo Pongratz 				/* remove from path/mc list */
937b63b70d8SShlomo Pongratz 				list_del(&neigh->list);
938b63b70d8SShlomo Pongratz 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
939b63b70d8SShlomo Pongratz 			} else {
940b63b70d8SShlomo Pongratz 				np = &neigh->hnext;
941b63b70d8SShlomo Pongratz 			}
942b63b70d8SShlomo Pongratz 
943b63b70d8SShlomo Pongratz 		}
944b63b70d8SShlomo Pongratz 	}
945b63b70d8SShlomo Pongratz 
946b63b70d8SShlomo Pongratz out_unlock:
947b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
948b63b70d8SShlomo Pongratz }
949b63b70d8SShlomo Pongratz 
950b63b70d8SShlomo Pongratz static void ipoib_reap_neigh(struct work_struct *work)
951b63b70d8SShlomo Pongratz {
952b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv =
953b63b70d8SShlomo Pongratz 		container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
954b63b70d8SShlomo Pongratz 
955b63b70d8SShlomo Pongratz 	__ipoib_reap_neigh(priv);
956b63b70d8SShlomo Pongratz 
957b63b70d8SShlomo Pongratz 	if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
958b63b70d8SShlomo Pongratz 		queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
959b63b70d8SShlomo Pongratz 				   arp_tbl.gc_interval);
960b63b70d8SShlomo Pongratz }
961b63b70d8SShlomo Pongratz 
962b63b70d8SShlomo Pongratz 
963b63b70d8SShlomo Pongratz static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
964732a2170SMoni Shoua 				      struct net_device *dev)
965d2e0655eSMichael S. Tsirkin {
966d2e0655eSMichael S. Tsirkin 	struct ipoib_neigh *neigh;
967d2e0655eSMichael S. Tsirkin 
968b63b70d8SShlomo Pongratz 	neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
969d2e0655eSMichael S. Tsirkin 	if (!neigh)
970d2e0655eSMichael S. Tsirkin 		return NULL;
971d2e0655eSMichael S. Tsirkin 
972732a2170SMoni Shoua 	neigh->dev = dev;
973b63b70d8SShlomo Pongratz 	memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
97482b39913SRoland Dreier 	skb_queue_head_init(&neigh->queue);
975b63b70d8SShlomo Pongratz 	INIT_LIST_HEAD(&neigh->list);
976839fcabaSMichael S. Tsirkin 	ipoib_cm_set(neigh, NULL);
977b63b70d8SShlomo Pongratz 	/* one ref on behalf of the caller */
978b63b70d8SShlomo Pongratz 	atomic_set(&neigh->refcnt, 1);
979d2e0655eSMichael S. Tsirkin 
980d2e0655eSMichael S. Tsirkin 	return neigh;
981d2e0655eSMichael S. Tsirkin }
982d2e0655eSMichael S. Tsirkin 
983b63b70d8SShlomo Pongratz struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
984b63b70d8SShlomo Pongratz 				      struct net_device *dev)
985d2e0655eSMichael S. Tsirkin {
986b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
987b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
988b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
989b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh;
990b63b70d8SShlomo Pongratz 	u32 hash_val;
991b63b70d8SShlomo Pongratz 
992b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
993b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
994b63b70d8SShlomo Pongratz 	if (!htbl) {
995b63b70d8SShlomo Pongratz 		neigh = NULL;
996b63b70d8SShlomo Pongratz 		goto out_unlock;
997b63b70d8SShlomo Pongratz 	}
998b63b70d8SShlomo Pongratz 
999b63b70d8SShlomo Pongratz 	/* need to add a new neigh, but maybe some other thread succeeded?
1000b63b70d8SShlomo Pongratz 	 * recalc hash, maybe hash resize took place so we do a search
1001b63b70d8SShlomo Pongratz 	 */
1002b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, daddr);
1003b63b70d8SShlomo Pongratz 	for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1004b5120a6eSShlomo Pongratz 					       lockdep_is_held(&priv->lock));
1005b63b70d8SShlomo Pongratz 	     neigh != NULL;
1006b63b70d8SShlomo Pongratz 	     neigh = rcu_dereference_protected(neigh->hnext,
1007b5120a6eSShlomo Pongratz 					       lockdep_is_held(&priv->lock))) {
1008b63b70d8SShlomo Pongratz 		if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1009b63b70d8SShlomo Pongratz 			/* found, take one ref on behalf of the caller */
1010b63b70d8SShlomo Pongratz 			if (!atomic_inc_not_zero(&neigh->refcnt)) {
1011b63b70d8SShlomo Pongratz 				/* deleted */
1012b63b70d8SShlomo Pongratz 				neigh = NULL;
1013b63b70d8SShlomo Pongratz 				break;
1014b63b70d8SShlomo Pongratz 			}
1015b63b70d8SShlomo Pongratz 			neigh->alive = jiffies;
1016b63b70d8SShlomo Pongratz 			goto out_unlock;
1017b63b70d8SShlomo Pongratz 		}
1018b63b70d8SShlomo Pongratz 	}
1019b63b70d8SShlomo Pongratz 
1020b63b70d8SShlomo Pongratz 	neigh = ipoib_neigh_ctor(daddr, dev);
1021b63b70d8SShlomo Pongratz 	if (!neigh)
1022b63b70d8SShlomo Pongratz 		goto out_unlock;
1023b63b70d8SShlomo Pongratz 
1024b63b70d8SShlomo Pongratz 	/* one ref on behalf of the hash table */
1025b63b70d8SShlomo Pongratz 	atomic_inc(&neigh->refcnt);
1026b63b70d8SShlomo Pongratz 	neigh->alive = jiffies;
1027b63b70d8SShlomo Pongratz 	/* put in hash */
1028b63b70d8SShlomo Pongratz 	rcu_assign_pointer(neigh->hnext,
1029b63b70d8SShlomo Pongratz 			   rcu_dereference_protected(htbl->buckets[hash_val],
1030b5120a6eSShlomo Pongratz 						     lockdep_is_held(&priv->lock)));
1031b63b70d8SShlomo Pongratz 	rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1032b63b70d8SShlomo Pongratz 	atomic_inc(&ntbl->entries);
1033b63b70d8SShlomo Pongratz 
1034b63b70d8SShlomo Pongratz out_unlock:
1035b63b70d8SShlomo Pongratz 
1036b63b70d8SShlomo Pongratz 	return neigh;
1037b63b70d8SShlomo Pongratz }
1038b63b70d8SShlomo Pongratz 
1039b63b70d8SShlomo Pongratz void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1040b63b70d8SShlomo Pongratz {
1041b63b70d8SShlomo Pongratz 	/* neigh reference count was dropprd to zero */
1042b63b70d8SShlomo Pongratz 	struct net_device *dev = neigh->dev;
1043b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
10442745b5b7SMichael S. Tsirkin 	struct sk_buff *skb;
1045b63b70d8SShlomo Pongratz 	if (neigh->ah)
1046b63b70d8SShlomo Pongratz 		ipoib_put_ah(neigh->ah);
10472745b5b7SMichael S. Tsirkin 	while ((skb = __skb_dequeue(&neigh->queue))) {
1048de903512SRoland Dreier 		++dev->stats.tx_dropped;
10492745b5b7SMichael S. Tsirkin 		dev_kfree_skb_any(skb);
10502745b5b7SMichael S. Tsirkin 	}
1051839fcabaSMichael S. Tsirkin 	if (ipoib_cm_get(neigh))
1052839fcabaSMichael S. Tsirkin 		ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1053b63b70d8SShlomo Pongratz 	ipoib_dbg(netdev_priv(dev),
1054b63b70d8SShlomo Pongratz 		  "neigh free for %06x %pI6\n",
1055b63b70d8SShlomo Pongratz 		  IPOIB_QPN(neigh->daddr),
1056b63b70d8SShlomo Pongratz 		  neigh->daddr + 4);
1057d2e0655eSMichael S. Tsirkin 	kfree(neigh);
1058b63b70d8SShlomo Pongratz 	if (atomic_dec_and_test(&priv->ntbl.entries)) {
1059b63b70d8SShlomo Pongratz 		if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1060b63b70d8SShlomo Pongratz 			complete(&priv->ntbl.flushed);
1061b63b70d8SShlomo Pongratz 	}
1062d2e0655eSMichael S. Tsirkin }
1063d2e0655eSMichael S. Tsirkin 
1064b63b70d8SShlomo Pongratz static void ipoib_neigh_reclaim(struct rcu_head *rp)
10651da177e4SLinus Torvalds {
1066b63b70d8SShlomo Pongratz 	/* Called as a result of removal from hash table */
1067b63b70d8SShlomo Pongratz 	struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1068b63b70d8SShlomo Pongratz 	/* note TX context may hold another ref */
1069b63b70d8SShlomo Pongratz 	ipoib_neigh_put(neigh);
1070b63b70d8SShlomo Pongratz }
1071b63b70d8SShlomo Pongratz 
1072b63b70d8SShlomo Pongratz void ipoib_neigh_free(struct ipoib_neigh *neigh)
1073b63b70d8SShlomo Pongratz {
1074b63b70d8SShlomo Pongratz 	struct net_device *dev = neigh->dev;
1075b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1076b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1077b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1078b63b70d8SShlomo Pongratz 	struct ipoib_neigh __rcu **np;
1079b63b70d8SShlomo Pongratz 	struct ipoib_neigh *n;
1080b63b70d8SShlomo Pongratz 	u32 hash_val;
1081b63b70d8SShlomo Pongratz 
1082b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1083b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock));
1084b63b70d8SShlomo Pongratz 	if (!htbl)
1085b5120a6eSShlomo Pongratz 		return;
1086b63b70d8SShlomo Pongratz 
1087b63b70d8SShlomo Pongratz 	hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1088b63b70d8SShlomo Pongratz 	np = &htbl->buckets[hash_val];
1089b63b70d8SShlomo Pongratz 	for (n = rcu_dereference_protected(*np,
1090b5120a6eSShlomo Pongratz 					    lockdep_is_held(&priv->lock));
1091b63b70d8SShlomo Pongratz 	     n != NULL;
10926c723a68SShlomo Pongratz 	     n = rcu_dereference_protected(*np,
1093b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock))) {
1094b63b70d8SShlomo Pongratz 		if (n == neigh) {
1095b63b70d8SShlomo Pongratz 			/* found */
1096b63b70d8SShlomo Pongratz 			rcu_assign_pointer(*np,
1097b63b70d8SShlomo Pongratz 					   rcu_dereference_protected(neigh->hnext,
1098b5120a6eSShlomo Pongratz 								     lockdep_is_held(&priv->lock)));
1099b63b70d8SShlomo Pongratz 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1100b5120a6eSShlomo Pongratz 			return;
1101b63b70d8SShlomo Pongratz 		} else {
1102b63b70d8SShlomo Pongratz 			np = &n->hnext;
1103b63b70d8SShlomo Pongratz 		}
1104b63b70d8SShlomo Pongratz 	}
1105b63b70d8SShlomo Pongratz }
1106b63b70d8SShlomo Pongratz 
1107b63b70d8SShlomo Pongratz static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1108b63b70d8SShlomo Pongratz {
1109b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1110b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1111b63b70d8SShlomo Pongratz 	struct ipoib_neigh **buckets;
1112b63b70d8SShlomo Pongratz 	u32 size;
1113b63b70d8SShlomo Pongratz 
1114b63b70d8SShlomo Pongratz 	clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1115b63b70d8SShlomo Pongratz 	ntbl->htbl = NULL;
1116b63b70d8SShlomo Pongratz 	htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1117b63b70d8SShlomo Pongratz 	if (!htbl)
1118b63b70d8SShlomo Pongratz 		return -ENOMEM;
1119b63b70d8SShlomo Pongratz 	set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1120b63b70d8SShlomo Pongratz 	size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1121b63b70d8SShlomo Pongratz 	buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1122b63b70d8SShlomo Pongratz 	if (!buckets) {
1123b63b70d8SShlomo Pongratz 		kfree(htbl);
1124b63b70d8SShlomo Pongratz 		return -ENOMEM;
1125b63b70d8SShlomo Pongratz 	}
1126b63b70d8SShlomo Pongratz 	htbl->size = size;
1127b63b70d8SShlomo Pongratz 	htbl->mask = (size - 1);
1128b63b70d8SShlomo Pongratz 	htbl->buckets = buckets;
1129b63b70d8SShlomo Pongratz 	ntbl->htbl = htbl;
113066172c09SShlomo Pongratz 	htbl->ntbl = ntbl;
1131b63b70d8SShlomo Pongratz 	atomic_set(&ntbl->entries, 0);
1132b63b70d8SShlomo Pongratz 
1133b63b70d8SShlomo Pongratz 	/* start garbage collection */
1134b63b70d8SShlomo Pongratz 	clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1135b63b70d8SShlomo Pongratz 	queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
1136b63b70d8SShlomo Pongratz 			   arp_tbl.gc_interval);
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	return 0;
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
1141b63b70d8SShlomo Pongratz static void neigh_hash_free_rcu(struct rcu_head *head)
1142b63b70d8SShlomo Pongratz {
1143b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl = container_of(head,
1144b63b70d8SShlomo Pongratz 						    struct ipoib_neigh_hash,
1145b63b70d8SShlomo Pongratz 						    rcu);
1146b63b70d8SShlomo Pongratz 	struct ipoib_neigh __rcu **buckets = htbl->buckets;
114766172c09SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = htbl->ntbl;
1148b63b70d8SShlomo Pongratz 
1149b63b70d8SShlomo Pongratz 	kfree(buckets);
1150b63b70d8SShlomo Pongratz 	kfree(htbl);
115166172c09SShlomo Pongratz 	complete(&ntbl->deleted);
1152b63b70d8SShlomo Pongratz }
1153b63b70d8SShlomo Pongratz 
1154b63b70d8SShlomo Pongratz void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1155b63b70d8SShlomo Pongratz {
1156b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1157b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1158b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1159b63b70d8SShlomo Pongratz 	unsigned long flags;
1160b63b70d8SShlomo Pongratz 	int i;
1161b63b70d8SShlomo Pongratz 
1162b63b70d8SShlomo Pongratz 	/* remove all neigh connected to a given path or mcast */
1163b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1164b63b70d8SShlomo Pongratz 
1165b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1166b5120a6eSShlomo Pongratz 					 lockdep_is_held(&priv->lock));
1167b63b70d8SShlomo Pongratz 
1168b63b70d8SShlomo Pongratz 	if (!htbl)
1169b63b70d8SShlomo Pongratz 		goto out_unlock;
1170b63b70d8SShlomo Pongratz 
1171b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1172b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1173b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1174b63b70d8SShlomo Pongratz 
1175b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1176b5120a6eSShlomo Pongratz 							  lockdep_is_held(&priv->lock))) != NULL) {
1177b63b70d8SShlomo Pongratz 			/* delete neighs belong to this parent */
1178b63b70d8SShlomo Pongratz 			if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1179b63b70d8SShlomo Pongratz 				rcu_assign_pointer(*np,
1180b63b70d8SShlomo Pongratz 						   rcu_dereference_protected(neigh->hnext,
1181b5120a6eSShlomo Pongratz 									     lockdep_is_held(&priv->lock)));
1182b63b70d8SShlomo Pongratz 				/* remove from parent list */
1183b63b70d8SShlomo Pongratz 				list_del(&neigh->list);
1184b63b70d8SShlomo Pongratz 				call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1185b63b70d8SShlomo Pongratz 			} else {
1186b63b70d8SShlomo Pongratz 				np = &neigh->hnext;
1187b63b70d8SShlomo Pongratz 			}
1188b63b70d8SShlomo Pongratz 
1189b63b70d8SShlomo Pongratz 		}
1190b63b70d8SShlomo Pongratz 	}
1191b63b70d8SShlomo Pongratz out_unlock:
1192b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
1193b63b70d8SShlomo Pongratz }
1194b63b70d8SShlomo Pongratz 
1195b63b70d8SShlomo Pongratz static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1196b63b70d8SShlomo Pongratz {
1197b63b70d8SShlomo Pongratz 	struct ipoib_neigh_table *ntbl = &priv->ntbl;
1198b63b70d8SShlomo Pongratz 	struct ipoib_neigh_hash *htbl;
1199b63b70d8SShlomo Pongratz 	unsigned long flags;
120066172c09SShlomo Pongratz 	int i, wait_flushed = 0;
1201b63b70d8SShlomo Pongratz 
120266172c09SShlomo Pongratz 	init_completion(&priv->ntbl.flushed);
1203b63b70d8SShlomo Pongratz 
1204b5120a6eSShlomo Pongratz 	spin_lock_irqsave(&priv->lock, flags);
1205b63b70d8SShlomo Pongratz 
1206b63b70d8SShlomo Pongratz 	htbl = rcu_dereference_protected(ntbl->htbl,
1207b5120a6eSShlomo Pongratz 					lockdep_is_held(&priv->lock));
1208b63b70d8SShlomo Pongratz 	if (!htbl)
1209b63b70d8SShlomo Pongratz 		goto out_unlock;
1210b63b70d8SShlomo Pongratz 
121166172c09SShlomo Pongratz 	wait_flushed = atomic_read(&priv->ntbl.entries);
121266172c09SShlomo Pongratz 	if (!wait_flushed)
121366172c09SShlomo Pongratz 		goto free_htbl;
121466172c09SShlomo Pongratz 
1215b63b70d8SShlomo Pongratz 	for (i = 0; i < htbl->size; i++) {
1216b63b70d8SShlomo Pongratz 		struct ipoib_neigh *neigh;
1217b63b70d8SShlomo Pongratz 		struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1218b63b70d8SShlomo Pongratz 
1219b63b70d8SShlomo Pongratz 		while ((neigh = rcu_dereference_protected(*np,
1220b5120a6eSShlomo Pongratz 				       lockdep_is_held(&priv->lock))) != NULL) {
1221b63b70d8SShlomo Pongratz 			rcu_assign_pointer(*np,
1222b63b70d8SShlomo Pongratz 					   rcu_dereference_protected(neigh->hnext,
1223b5120a6eSShlomo Pongratz 								     lockdep_is_held(&priv->lock)));
1224b63b70d8SShlomo Pongratz 			/* remove from path/mc list */
1225b63b70d8SShlomo Pongratz 			list_del(&neigh->list);
1226b63b70d8SShlomo Pongratz 			call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1227b63b70d8SShlomo Pongratz 		}
1228b63b70d8SShlomo Pongratz 	}
1229b63b70d8SShlomo Pongratz 
123066172c09SShlomo Pongratz free_htbl:
1231b63b70d8SShlomo Pongratz 	rcu_assign_pointer(ntbl->htbl, NULL);
1232b63b70d8SShlomo Pongratz 	call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1233b63b70d8SShlomo Pongratz 
1234b63b70d8SShlomo Pongratz out_unlock:
1235b5120a6eSShlomo Pongratz 	spin_unlock_irqrestore(&priv->lock, flags);
123666172c09SShlomo Pongratz 	if (wait_flushed)
123766172c09SShlomo Pongratz 		wait_for_completion(&priv->ntbl.flushed);
1238b63b70d8SShlomo Pongratz }
1239b63b70d8SShlomo Pongratz 
1240b63b70d8SShlomo Pongratz static void ipoib_neigh_hash_uninit(struct net_device *dev)
1241b63b70d8SShlomo Pongratz {
1242b63b70d8SShlomo Pongratz 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1243b63b70d8SShlomo Pongratz 	int stopped;
1244b63b70d8SShlomo Pongratz 
1245b63b70d8SShlomo Pongratz 	ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
124666172c09SShlomo Pongratz 	init_completion(&priv->ntbl.deleted);
1247b63b70d8SShlomo Pongratz 	set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1248b63b70d8SShlomo Pongratz 
1249b63b70d8SShlomo Pongratz 	/* Stop GC if called at init fail need to cancel work */
1250b63b70d8SShlomo Pongratz 	stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1251b63b70d8SShlomo Pongratz 	if (!stopped)
1252b63b70d8SShlomo Pongratz 		cancel_delayed_work(&priv->neigh_reap_task);
1253b63b70d8SShlomo Pongratz 
1254b63b70d8SShlomo Pongratz 	ipoib_flush_neighs(priv);
125566172c09SShlomo Pongratz 
125666172c09SShlomo Pongratz 	wait_for_completion(&priv->ntbl.deleted);
1257b63b70d8SShlomo Pongratz }
1258b63b70d8SShlomo Pongratz 
1259b63b70d8SShlomo Pongratz 
12601da177e4SLinus Torvalds int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
12611da177e4SLinus Torvalds {
12621da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
12631da177e4SLinus Torvalds 
1264b63b70d8SShlomo Pongratz 	if (ipoib_neigh_hash_init(priv) < 0)
1265b63b70d8SShlomo Pongratz 		goto out;
12661da177e4SLinus Torvalds 	/* Allocate RX/TX "rings" to hold queued skbs */
12670f485251SShirley Ma 	priv->rx_ring =	kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
12681da177e4SLinus Torvalds 				GFP_KERNEL);
12691da177e4SLinus Torvalds 	if (!priv->rx_ring) {
12701da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
12710f485251SShirley Ma 		       ca->name, ipoib_recvq_size);
1272b63b70d8SShlomo Pongratz 		goto out_neigh_hash_cleanup;
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds 
1275948579cdSJoe Perches 	priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
12761da177e4SLinus Torvalds 	if (!priv->tx_ring) {
12771da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
12780f485251SShirley Ma 		       ca->name, ipoib_sendq_size);
12791da177e4SLinus Torvalds 		goto out_rx_ring_cleanup;
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 
12821b524963SMichael S. Tsirkin 	/* priv->tx_head, tx_tail & tx_outstanding are already 0 */
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	if (ipoib_ib_dev_init(dev, ca, port))
12851da177e4SLinus Torvalds 		goto out_tx_ring_cleanup;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	return 0;
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds out_tx_ring_cleanup:
129010313cbbSRoland Dreier 	vfree(priv->tx_ring);
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds out_rx_ring_cleanup:
12931da177e4SLinus Torvalds 	kfree(priv->rx_ring);
12941da177e4SLinus Torvalds 
1295b63b70d8SShlomo Pongratz out_neigh_hash_cleanup:
1296b63b70d8SShlomo Pongratz 	ipoib_neigh_hash_uninit(dev);
12971da177e4SLinus Torvalds out:
12981da177e4SLinus Torvalds 	return -ENOMEM;
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds void ipoib_dev_cleanup(struct net_device *dev)
13021da177e4SLinus Torvalds {
13031da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
13049baa0b03SOr Gerlitz 	LIST_HEAD(head);
13059baa0b03SOr Gerlitz 
13069baa0b03SOr Gerlitz 	ASSERT_RTNL();
13071da177e4SLinus Torvalds 
13081732b0efSRoland Dreier 	ipoib_delete_debug_files(dev);
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	/* Delete any child interfaces first */
13111da177e4SLinus Torvalds 	list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1312b63b70d8SShlomo Pongratz 		/* Stop GC on child */
1313b63b70d8SShlomo Pongratz 		set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1314b63b70d8SShlomo Pongratz 		cancel_delayed_work(&cpriv->neigh_reap_task);
13159baa0b03SOr Gerlitz 		unregister_netdevice_queue(cpriv->dev, &head);
13161da177e4SLinus Torvalds 	}
13179baa0b03SOr Gerlitz 	unregister_netdevice_many(&head);
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	ipoib_ib_dev_cleanup(dev);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	kfree(priv->rx_ring);
132210313cbbSRoland Dreier 	vfree(priv->tx_ring);
132392a6b34bSHal Rosenstock 
132492a6b34bSHal Rosenstock 	priv->rx_ring = NULL;
13251da177e4SLinus Torvalds 	priv->tx_ring = NULL;
1326b63b70d8SShlomo Pongratz 
1327b63b70d8SShlomo Pongratz 	ipoib_neigh_hash_uninit(dev);
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
13303b04dddeSStephen Hemminger static const struct header_ops ipoib_header_ops = {
13313b04dddeSStephen Hemminger 	.create	= ipoib_hard_header,
13323b04dddeSStephen Hemminger };
13333b04dddeSStephen Hemminger 
1334fe8114e8SStephen Hemminger static const struct net_device_ops ipoib_netdev_ops = {
13359baa0b03SOr Gerlitz 	.ndo_uninit		 = ipoib_uninit,
1336fe8114e8SStephen Hemminger 	.ndo_open		 = ipoib_open,
1337fe8114e8SStephen Hemminger 	.ndo_stop		 = ipoib_stop,
1338fe8114e8SStephen Hemminger 	.ndo_change_mtu		 = ipoib_change_mtu,
13393d96c74dSMichał Mirosław 	.ndo_fix_features	 = ipoib_fix_features,
1340fe8114e8SStephen Hemminger 	.ndo_start_xmit	 	 = ipoib_start_xmit,
1341fe8114e8SStephen Hemminger 	.ndo_tx_timeout		 = ipoib_timeout,
1342afc4b13dSJiri Pirko 	.ndo_set_rx_mode	 = ipoib_set_mcast_list,
1343fe8114e8SStephen Hemminger };
1344fe8114e8SStephen Hemminger 
13459baa0b03SOr Gerlitz void ipoib_setup(struct net_device *dev)
13461da177e4SLinus Torvalds {
13471da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
13481da177e4SLinus Torvalds 
1349fe8114e8SStephen Hemminger 	dev->netdev_ops		 = &ipoib_netdev_ops;
13503b04dddeSStephen Hemminger 	dev->header_ops		 = &ipoib_header_ops;
1351bea3348eSStephen Hemminger 
135282c24c18SEli Cohen 	ipoib_set_ethtool_ops(dev);
135382c24c18SEli Cohen 
1354bea3348eSStephen Hemminger 	netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	dev->watchdog_timeo	 = HZ;
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	dev->flags		|= IFF_BROADCAST | IFF_MULTICAST;
13591da177e4SLinus Torvalds 
1360936d7de3SRoland Dreier 	dev->hard_header_len	 = IPOIB_ENCAP_LEN;
13611da177e4SLinus Torvalds 	dev->addr_len		 = INFINIBAND_ALEN;
13621da177e4SLinus Torvalds 	dev->type		 = ARPHRD_INFINIBAND;
13630f485251SShirley Ma 	dev->tx_queue_len	 = ipoib_sendq_size * 2;
1364eb14032fSEli Cohen 	dev->features		 = (NETIF_F_VLAN_CHALLENGED	|
1365eb14032fSEli Cohen 				    NETIF_F_HIGHDMA);
136686d15cd8SEric Dumazet 	dev->priv_flags		&= ~IFF_XMIT_DST_RELEASE;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	netif_carrier_off(dev);
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	priv->dev = dev;
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
13751da177e4SLinus Torvalds 
137695ed644fSIngo Molnar 	mutex_init(&priv->vlan_mutex);
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->path_list);
13791da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->child_intfs);
13801da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->dead_ahs);
13811da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->multicast_list);
13821da177e4SLinus Torvalds 
138326bbf13cSYosef Etigin 	INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
1384c4028958SDavid Howells 	INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
1385e8224e4bSYossi Etigin 	INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1386ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
1387ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
1388ee1e2c82SMoni Shoua 	INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
1389c4028958SDavid Howells 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1390c4028958SDavid Howells 	INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1391b63b70d8SShlomo Pongratz 	INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	struct net_device *dev;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
13991da177e4SLinus Torvalds 			   ipoib_setup);
14001da177e4SLinus Torvalds 	if (!dev)
14011da177e4SLinus Torvalds 		return NULL;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	return netdev_priv(dev);
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
140643cb76d9SGreg Kroah-Hartman static ssize_t show_pkey(struct device *dev,
140743cb76d9SGreg Kroah-Hartman 			 struct device_attribute *attr, char *buf)
14081da177e4SLinus Torvalds {
140943cb76d9SGreg Kroah-Hartman 	struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	return sprintf(buf, "0x%04x\n", priv->pkey);
14121da177e4SLinus Torvalds }
141343cb76d9SGreg Kroah-Hartman static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
14141da177e4SLinus Torvalds 
1415335a64a5SOr Gerlitz static ssize_t show_umcast(struct device *dev,
1416335a64a5SOr Gerlitz 			   struct device_attribute *attr, char *buf)
1417335a64a5SOr Gerlitz {
1418335a64a5SOr Gerlitz 	struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1419335a64a5SOr Gerlitz 
1420335a64a5SOr Gerlitz 	return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1421335a64a5SOr Gerlitz }
1422335a64a5SOr Gerlitz 
1423862096a8SOr Gerlitz void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
1424335a64a5SOr Gerlitz {
1425862096a8SOr Gerlitz 	struct ipoib_dev_priv *priv = netdev_priv(ndev);
1426335a64a5SOr Gerlitz 
1427335a64a5SOr Gerlitz 	if (umcast_val > 0) {
1428335a64a5SOr Gerlitz 		set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1429335a64a5SOr Gerlitz 		ipoib_warn(priv, "ignoring multicast groups joined directly "
1430335a64a5SOr Gerlitz 				"by userspace\n");
1431335a64a5SOr Gerlitz 	} else
1432335a64a5SOr Gerlitz 		clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1433862096a8SOr Gerlitz }
1434862096a8SOr Gerlitz 
1435862096a8SOr Gerlitz static ssize_t set_umcast(struct device *dev,
1436862096a8SOr Gerlitz 			  struct device_attribute *attr,
1437862096a8SOr Gerlitz 			  const char *buf, size_t count)
1438862096a8SOr Gerlitz {
1439862096a8SOr Gerlitz 	unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1440862096a8SOr Gerlitz 
1441862096a8SOr Gerlitz 	ipoib_set_umcast(to_net_dev(dev), umcast_val);
1442335a64a5SOr Gerlitz 
1443335a64a5SOr Gerlitz 	return count;
1444335a64a5SOr Gerlitz }
1445335a64a5SOr Gerlitz static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1446335a64a5SOr Gerlitz 
1447335a64a5SOr Gerlitz int ipoib_add_umcast_attr(struct net_device *dev)
1448335a64a5SOr Gerlitz {
1449335a64a5SOr Gerlitz 	return device_create_file(&dev->dev, &dev_attr_umcast);
1450335a64a5SOr Gerlitz }
1451335a64a5SOr Gerlitz 
145243cb76d9SGreg Kroah-Hartman static ssize_t create_child(struct device *dev,
145343cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr,
14541da177e4SLinus Torvalds 			    const char *buf, size_t count)
14551da177e4SLinus Torvalds {
14561da177e4SLinus Torvalds 	int pkey;
14571da177e4SLinus Torvalds 	int ret;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
14601da177e4SLinus Torvalds 		return -EINVAL;
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	if (pkey < 0 || pkey > 0xffff)
14631da177e4SLinus Torvalds 		return -EINVAL;
14641da177e4SLinus Torvalds 
14654ce05937SRoland Dreier 	/*
14664ce05937SRoland Dreier 	 * Set the full membership bit, so that we join the right
14674ce05937SRoland Dreier 	 * broadcast group, etc.
14684ce05937SRoland Dreier 	 */
14694ce05937SRoland Dreier 	pkey |= 0x8000;
14704ce05937SRoland Dreier 
147143cb76d9SGreg Kroah-Hartman 	ret = ipoib_vlan_add(to_net_dev(dev), pkey);
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 	return ret ? ret : count;
14741da177e4SLinus Torvalds }
14757a52b34bSOr Gerlitz static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
14761da177e4SLinus Torvalds 
147743cb76d9SGreg Kroah-Hartman static ssize_t delete_child(struct device *dev,
147843cb76d9SGreg Kroah-Hartman 			    struct device_attribute *attr,
14791da177e4SLinus Torvalds 			    const char *buf, size_t count)
14801da177e4SLinus Torvalds {
14811da177e4SLinus Torvalds 	int pkey;
14821da177e4SLinus Torvalds 	int ret;
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
14851da177e4SLinus Torvalds 		return -EINVAL;
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 	if (pkey < 0 || pkey > 0xffff)
14881da177e4SLinus Torvalds 		return -EINVAL;
14891da177e4SLinus Torvalds 
149043cb76d9SGreg Kroah-Hartman 	ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	return ret ? ret : count;
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds }
14957a52b34bSOr Gerlitz static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds int ipoib_add_pkey_attr(struct net_device *dev)
14981da177e4SLinus Torvalds {
149943cb76d9SGreg Kroah-Hartman 	return device_create_file(&dev->dev, &dev_attr_pkey);
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
150283bb63f6SOr Gerlitz int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
150383bb63f6SOr Gerlitz {
150483bb63f6SOr Gerlitz 	struct ib_device_attr *device_attr;
150583bb63f6SOr Gerlitz 	int result = -ENOMEM;
150683bb63f6SOr Gerlitz 
150783bb63f6SOr Gerlitz 	device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
150883bb63f6SOr Gerlitz 	if (!device_attr) {
150983bb63f6SOr Gerlitz 		printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
151083bb63f6SOr Gerlitz 		       hca->name, sizeof *device_attr);
151183bb63f6SOr Gerlitz 		return result;
151283bb63f6SOr Gerlitz 	}
151383bb63f6SOr Gerlitz 
151483bb63f6SOr Gerlitz 	result = ib_query_device(hca, device_attr);
151583bb63f6SOr Gerlitz 	if (result) {
151683bb63f6SOr Gerlitz 		printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
151783bb63f6SOr Gerlitz 		       hca->name, result);
151883bb63f6SOr Gerlitz 		kfree(device_attr);
151983bb63f6SOr Gerlitz 		return result;
152083bb63f6SOr Gerlitz 	}
152183bb63f6SOr Gerlitz 	priv->hca_caps = device_attr->device_cap_flags;
152283bb63f6SOr Gerlitz 
152383bb63f6SOr Gerlitz 	kfree(device_attr);
152483bb63f6SOr Gerlitz 
152583bb63f6SOr Gerlitz 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
15263d96c74dSMichał Mirosław 		priv->dev->hw_features = NETIF_F_SG |
15273d96c74dSMichał Mirosław 			NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15283d96c74dSMichał Mirosław 
15293d96c74dSMichał Mirosław 		if (priv->hca_caps & IB_DEVICE_UD_TSO)
15303d96c74dSMichał Mirosław 			priv->dev->hw_features |= NETIF_F_TSO;
15313d96c74dSMichał Mirosław 
15323d96c74dSMichał Mirosław 		priv->dev->features |= priv->dev->hw_features;
153383bb63f6SOr Gerlitz 	}
153483bb63f6SOr Gerlitz 
153583bb63f6SOr Gerlitz 	return 0;
153683bb63f6SOr Gerlitz }
153783bb63f6SOr Gerlitz 
15381da177e4SLinus Torvalds static struct net_device *ipoib_add_port(const char *format,
15391da177e4SLinus Torvalds 					 struct ib_device *hca, u8 port)
15401da177e4SLinus Torvalds {
15411da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
1542bc7b3a36SShirley Ma 	struct ib_port_attr attr;
15431da177e4SLinus Torvalds 	int result = -ENOMEM;
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	priv = ipoib_intf_alloc(format);
15461da177e4SLinus Torvalds 	if (!priv)
15471da177e4SLinus Torvalds 		goto alloc_mem_failed;
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	SET_NETDEV_DEV(priv->dev, hca->dma_device);
1550c3aa9b18SEli Cohen 	priv->dev->dev_id = port - 1;
15511da177e4SLinus Torvalds 
1552bc7b3a36SShirley Ma 	if (!ib_query_port(hca, port, &attr))
1553bc7b3a36SShirley Ma 		priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1554bc7b3a36SShirley Ma 	else {
1555bc7b3a36SShirley Ma 		printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1556bc7b3a36SShirley Ma 		       hca->name, port);
1557bc7b3a36SShirley Ma 		goto device_init_failed;
1558bc7b3a36SShirley Ma 	}
1559bc7b3a36SShirley Ma 
1560bc7b3a36SShirley Ma 	/* MTU will be reset when mcast join happens */
1561bc7b3a36SShirley Ma 	priv->dev->mtu  = IPOIB_UD_MTU(priv->max_ib_mtu);
1562bc7b3a36SShirley Ma 	priv->mcast_mtu  = priv->admin_mtu = priv->dev->mtu;
1563bc7b3a36SShirley Ma 
1564596b9b68SDavid Miller 	priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1565596b9b68SDavid Miller 
15661da177e4SLinus Torvalds 	result = ib_query_pkey(hca, port, 0, &priv->pkey);
15671da177e4SLinus Torvalds 	if (result) {
15681da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
15691da177e4SLinus Torvalds 		       hca->name, port, result);
1570ca6de177SEli Cohen 		goto device_init_failed;
15711da177e4SLinus Torvalds 	}
15721da177e4SLinus Torvalds 
157383bb63f6SOr Gerlitz 	if (ipoib_set_dev_features(priv, hca))
15746046136cSEli Cohen 		goto device_init_failed;
1575af40da89SVladimir Sokolovsky 
15764ce05937SRoland Dreier 	/*
15774ce05937SRoland Dreier 	 * Set the full membership bit, so that we join the right
15784ce05937SRoland Dreier 	 * broadcast group, etc.
15794ce05937SRoland Dreier 	 */
15804ce05937SRoland Dreier 	priv->pkey |= 0x8000;
15814ce05937SRoland Dreier 
15821da177e4SLinus Torvalds 	priv->dev->broadcast[8] = priv->pkey >> 8;
15831da177e4SLinus Torvalds 	priv->dev->broadcast[9] = priv->pkey & 0xff;
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	result = ib_query_gid(hca, port, 0, &priv->local_gid);
15861da177e4SLinus Torvalds 	if (result) {
15871da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
15881da177e4SLinus Torvalds 		       hca->name, port, result);
1589ca6de177SEli Cohen 		goto device_init_failed;
15901da177e4SLinus Torvalds 	} else
15911da177e4SLinus Torvalds 		memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	result = ipoib_dev_init(priv->dev, hca, port);
15941da177e4SLinus Torvalds 	if (result < 0) {
15951da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
15961da177e4SLinus Torvalds 		       hca->name, port, result);
15971da177e4SLinus Torvalds 		goto device_init_failed;
15981da177e4SLinus Torvalds 	}
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
16011da177e4SLinus Torvalds 			      priv->ca, ipoib_event);
16021da177e4SLinus Torvalds 	result = ib_register_event_handler(&priv->event_handler);
16031da177e4SLinus Torvalds 	if (result < 0) {
16041da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: ib_register_event_handler failed for "
16051da177e4SLinus Torvalds 		       "port %d (ret = %d)\n",
16061da177e4SLinus Torvalds 		       hca->name, port, result);
16071da177e4SLinus Torvalds 		goto event_failed;
16081da177e4SLinus Torvalds 	}
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	result = register_netdev(priv->dev);
16111da177e4SLinus Torvalds 	if (result) {
16121da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
16131da177e4SLinus Torvalds 		       hca->name, port, result);
16141da177e4SLinus Torvalds 		goto register_failed;
16151da177e4SLinus Torvalds 	}
16161da177e4SLinus Torvalds 
16171732b0efSRoland Dreier 	ipoib_create_debug_files(priv->dev);
16181da177e4SLinus Torvalds 
1619839fcabaSMichael S. Tsirkin 	if (ipoib_cm_add_mode_attr(priv->dev))
1620839fcabaSMichael S. Tsirkin 		goto sysfs_failed;
16211da177e4SLinus Torvalds 	if (ipoib_add_pkey_attr(priv->dev))
16221da177e4SLinus Torvalds 		goto sysfs_failed;
1623335a64a5SOr Gerlitz 	if (ipoib_add_umcast_attr(priv->dev))
1624335a64a5SOr Gerlitz 		goto sysfs_failed;
162543cb76d9SGreg Kroah-Hartman 	if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
16261da177e4SLinus Torvalds 		goto sysfs_failed;
162743cb76d9SGreg Kroah-Hartman 	if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
16281da177e4SLinus Torvalds 		goto sysfs_failed;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	return priv->dev;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds sysfs_failed:
16331732b0efSRoland Dreier 	ipoib_delete_debug_files(priv->dev);
16341da177e4SLinus Torvalds 	unregister_netdev(priv->dev);
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds register_failed:
16371da177e4SLinus Torvalds 	ib_unregister_event_handler(&priv->event_handler);
1638b63b70d8SShlomo Pongratz 	/* Stop GC if started before flush */
1639b63b70d8SShlomo Pongratz 	set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1640b63b70d8SShlomo Pongratz 	cancel_delayed_work(&priv->neigh_reap_task);
1641a77a57a1SRoland Dreier 	flush_workqueue(ipoib_workqueue);
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds event_failed:
16441da177e4SLinus Torvalds 	ipoib_dev_cleanup(priv->dev);
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds device_init_failed:
16471da177e4SLinus Torvalds 	free_netdev(priv->dev);
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds alloc_mem_failed:
16501da177e4SLinus Torvalds 	return ERR_PTR(result);
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds static void ipoib_add_one(struct ib_device *device)
16541da177e4SLinus Torvalds {
16551da177e4SLinus Torvalds 	struct list_head *dev_list;
16561da177e4SLinus Torvalds 	struct net_device *dev;
16571da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
16581da177e4SLinus Torvalds 	int s, e, p;
16591da177e4SLinus Torvalds 
166007ebafbaSTom Tucker 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
166107ebafbaSTom Tucker 		return;
166207ebafbaSTom Tucker 
16631da177e4SLinus Torvalds 	dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
16641da177e4SLinus Torvalds 	if (!dev_list)
16651da177e4SLinus Torvalds 		return;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	INIT_LIST_HEAD(dev_list);
16681da177e4SLinus Torvalds 
166907ebafbaSTom Tucker 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
16701da177e4SLinus Torvalds 		s = 0;
16711da177e4SLinus Torvalds 		e = 0;
16721da177e4SLinus Torvalds 	} else {
16731da177e4SLinus Torvalds 		s = 1;
16741da177e4SLinus Torvalds 		e = device->phys_port_cnt;
16751da177e4SLinus Torvalds 	}
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	for (p = s; p <= e; ++p) {
16787b4c8769SEli Cohen 		if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
16797b4c8769SEli Cohen 			continue;
16801da177e4SLinus Torvalds 		dev = ipoib_add_port("ib%d", device, p);
16811da177e4SLinus Torvalds 		if (!IS_ERR(dev)) {
16821da177e4SLinus Torvalds 			priv = netdev_priv(dev);
16831da177e4SLinus Torvalds 			list_add_tail(&priv->list, dev_list);
16841da177e4SLinus Torvalds 		}
16851da177e4SLinus Torvalds 	}
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 	ib_set_client_data(device, &ipoib_client, dev_list);
16881da177e4SLinus Torvalds }
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds static void ipoib_remove_one(struct ib_device *device)
16911da177e4SLinus Torvalds {
16921da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv, *tmp;
16931da177e4SLinus Torvalds 	struct list_head *dev_list;
16941da177e4SLinus Torvalds 
169507ebafbaSTom Tucker 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
169607ebafbaSTom Tucker 		return;
169707ebafbaSTom Tucker 
16981da177e4SLinus Torvalds 	dev_list = ib_get_client_data(device, &ipoib_client);
16995a2815f0SItai Garbi 	if (!dev_list)
17005a2815f0SItai Garbi 		return;
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
17031da177e4SLinus Torvalds 		ib_unregister_event_handler(&priv->event_handler);
1704a77a57a1SRoland Dreier 
1705a77a57a1SRoland Dreier 		rtnl_lock();
1706a77a57a1SRoland Dreier 		dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1707a77a57a1SRoland Dreier 		rtnl_unlock();
1708a77a57a1SRoland Dreier 
1709b63b70d8SShlomo Pongratz 		/* Stop GC */
1710b63b70d8SShlomo Pongratz 		set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1711b63b70d8SShlomo Pongratz 		cancel_delayed_work(&priv->neigh_reap_task);
1712a77a57a1SRoland Dreier 		flush_workqueue(ipoib_workqueue);
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 		unregister_netdev(priv->dev);
17151da177e4SLinus Torvalds 		free_netdev(priv->dev);
17161da177e4SLinus Torvalds 	}
171706c56e44SMichael S. Tsirkin 
171806c56e44SMichael S. Tsirkin 	kfree(dev_list);
17191da177e4SLinus Torvalds }
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds static int __init ipoib_init_module(void)
17221da177e4SLinus Torvalds {
17231da177e4SLinus Torvalds 	int ret;
17241da177e4SLinus Torvalds 
17250f485251SShirley Ma 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
17260f485251SShirley Ma 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
17270f485251SShirley Ma 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
17280f485251SShirley Ma 
17290f485251SShirley Ma 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
17300f485251SShirley Ma 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1731732eacc0SHagen Paul Pfeifer 	ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
173268e995a2SPradeep Satyanarayana #ifdef CONFIG_INFINIBAND_IPOIB_CM
173368e995a2SPradeep Satyanarayana 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
173468e995a2SPradeep Satyanarayana #endif
17350f485251SShirley Ma 
1736f89271daSEli Cohen 	/*
1737f89271daSEli Cohen 	 * When copying small received packets, we only copy from the
1738f89271daSEli Cohen 	 * linear data part of the SKB, so we rely on this condition.
1739f89271daSEli Cohen 	 */
1740f89271daSEli Cohen 	BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1741f89271daSEli Cohen 
17421da177e4SLinus Torvalds 	ret = ipoib_register_debugfs();
17431da177e4SLinus Torvalds 	if (ret)
17441da177e4SLinus Torvalds 		return ret;
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 	/*
17471da177e4SLinus Torvalds 	 * We create our own workqueue mainly because we want to be
17481da177e4SLinus Torvalds 	 * able to flush it when devices are being removed.  We can't
17491da177e4SLinus Torvalds 	 * use schedule_work()/flush_scheduled_work() because both
17501da177e4SLinus Torvalds 	 * unregister_netdev() and linkwatch_event take the rtnl lock,
17511da177e4SLinus Torvalds 	 * so flush_scheduled_work() can deadlock during device
17521da177e4SLinus Torvalds 	 * removal.
17531da177e4SLinus Torvalds 	 */
17541da177e4SLinus Torvalds 	ipoib_workqueue = create_singlethread_workqueue("ipoib");
17551da177e4SLinus Torvalds 	if (!ipoib_workqueue) {
17561da177e4SLinus Torvalds 		ret = -ENOMEM;
17571da177e4SLinus Torvalds 		goto err_fs;
17581da177e4SLinus Torvalds 	}
17591da177e4SLinus Torvalds 
1760c1a0b23bSMichael S. Tsirkin 	ib_sa_register_client(&ipoib_sa_client);
1761c1a0b23bSMichael S. Tsirkin 
17621da177e4SLinus Torvalds 	ret = ib_register_client(&ipoib_client);
17631da177e4SLinus Torvalds 	if (ret)
1764c1a0b23bSMichael S. Tsirkin 		goto err_sa;
17651da177e4SLinus Torvalds 
17669baa0b03SOr Gerlitz 	ret = ipoib_netlink_init();
17679baa0b03SOr Gerlitz 	if (ret)
17689baa0b03SOr Gerlitz 		goto err_client;
17699baa0b03SOr Gerlitz 
17701da177e4SLinus Torvalds 	return 0;
17711da177e4SLinus Torvalds 
17729baa0b03SOr Gerlitz err_client:
17739baa0b03SOr Gerlitz 	ib_unregister_client(&ipoib_client);
17749baa0b03SOr Gerlitz 
1775c1a0b23bSMichael S. Tsirkin err_sa:
1776c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&ipoib_sa_client);
17771da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
17781da177e4SLinus Torvalds 
17799adec1a8SRoland Dreier err_fs:
17809adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
17819adec1a8SRoland Dreier 
17821da177e4SLinus Torvalds 	return ret;
17831da177e4SLinus Torvalds }
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds static void __exit ipoib_cleanup_module(void)
17861da177e4SLinus Torvalds {
17879baa0b03SOr Gerlitz 	ipoib_netlink_fini();
17881da177e4SLinus Torvalds 	ib_unregister_client(&ipoib_client);
1789c1a0b23bSMichael S. Tsirkin 	ib_sa_unregister_client(&ipoib_sa_client);
17909adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
17911da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
17921da177e4SLinus Torvalds }
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds module_init(ipoib_init_module);
17951da177e4SLinus Torvalds module_exit(ipoib_cleanup_module);
1796