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  * $Id: ipoib_main.c 1377 2004-12-23 19:57:12Z roland $
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include "ipoib.h"
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <linux/module.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/init.h>
421da177e4SLinus Torvalds #include <linux/slab.h>
431da177e4SLinus Torvalds #include <linux/vmalloc.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <linux/if_arp.h>	/* For ARPHRD_xxx */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <linux/ip.h>
481da177e4SLinus Torvalds #include <linux/in.h>
491da177e4SLinus Torvalds 
5014c85021SArnaldo Carvalho de Melo #include <net/dst.h>
5114c85021SArnaldo Carvalho de Melo 
521da177e4SLinus Torvalds MODULE_AUTHOR("Roland Dreier");
531da177e4SLinus Torvalds MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
541da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL");
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
571da177e4SLinus Torvalds int ipoib_debug_level;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds module_param_named(debug_level, ipoib_debug_level, int, 0644);
601da177e4SLinus Torvalds MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
611da177e4SLinus Torvalds #endif
621da177e4SLinus Torvalds 
631732b0efSRoland Dreier struct ipoib_path_iter {
641732b0efSRoland Dreier 	struct net_device *dev;
651732b0efSRoland Dreier 	struct ipoib_path  path;
661732b0efSRoland Dreier };
671732b0efSRoland Dreier 
681da177e4SLinus Torvalds static const u8 ipv4_bcast_addr[] = {
691da177e4SLinus Torvalds 	0x00, 0xff, 0xff, 0xff,
701da177e4SLinus Torvalds 	0xff, 0x12, 0x40, 0x1b,	0x00, 0x00, 0x00, 0x00,
711da177e4SLinus Torvalds 	0x00, 0x00, 0x00, 0x00,	0xff, 0xff, 0xff, 0xff
721da177e4SLinus Torvalds };
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds struct workqueue_struct *ipoib_workqueue;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static void ipoib_add_one(struct ib_device *device);
771da177e4SLinus Torvalds static void ipoib_remove_one(struct ib_device *device);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static struct ib_client ipoib_client = {
801da177e4SLinus Torvalds 	.name   = "ipoib",
811da177e4SLinus Torvalds 	.add    = ipoib_add_one,
821da177e4SLinus Torvalds 	.remove = ipoib_remove_one
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds int ipoib_open(struct net_device *dev)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	ipoib_dbg(priv, "bringing up interface\n");
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	if (ipoib_pkey_dev_delay_open(dev))
941da177e4SLinus Torvalds 		return 0;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	if (ipoib_ib_dev_open(dev))
971da177e4SLinus Torvalds 		return -EINVAL;
981da177e4SLinus Torvalds 
99267ee88eSRoland Dreier 	if (ipoib_ib_dev_up(dev)) {
100267ee88eSRoland Dreier 		ipoib_ib_dev_stop(dev);
1011da177e4SLinus Torvalds 		return -EINVAL;
102267ee88eSRoland Dreier 	}
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1051da177e4SLinus Torvalds 		struct ipoib_dev_priv *cpriv;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 		/* Bring up any child interfaces too */
10895ed644fSIngo Molnar 		mutex_lock(&priv->vlan_mutex);
1091da177e4SLinus Torvalds 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
1101da177e4SLinus Torvalds 			int flags;
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 			flags = cpriv->dev->flags;
1131da177e4SLinus Torvalds 			if (flags & IFF_UP)
1141da177e4SLinus Torvalds 				continue;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 			dev_change_flags(cpriv->dev, flags | IFF_UP);
1171da177e4SLinus Torvalds 		}
11895ed644fSIngo Molnar 		mutex_unlock(&priv->vlan_mutex);
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	netif_start_queue(dev);
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	return 0;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static int ipoib_stop(struct net_device *dev)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	ipoib_dbg(priv, "stopping interface\n");
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	netif_stop_queue(dev);
1351da177e4SLinus Torvalds 
1360b3ea082SJack Morgenstein 	/*
1370b3ea082SJack Morgenstein 	 * Now flush workqueue to make sure a scheduled task doesn't
1380b3ea082SJack Morgenstein 	 * bring our internal state back up.
1390b3ea082SJack Morgenstein 	 */
1400b3ea082SJack Morgenstein 	flush_workqueue(ipoib_workqueue);
1410b3ea082SJack Morgenstein 
1420b3ea082SJack Morgenstein 	ipoib_ib_dev_down(dev, 1);
1431da177e4SLinus Torvalds 	ipoib_ib_dev_stop(dev);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1461da177e4SLinus Torvalds 		struct ipoib_dev_priv *cpriv;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		/* Bring down any child interfaces too */
14995ed644fSIngo Molnar 		mutex_lock(&priv->vlan_mutex);
1501da177e4SLinus Torvalds 		list_for_each_entry(cpriv, &priv->child_intfs, list) {
1511da177e4SLinus Torvalds 			int flags;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 			flags = cpriv->dev->flags;
1541da177e4SLinus Torvalds 			if (!(flags & IFF_UP))
1551da177e4SLinus Torvalds 				continue;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 			dev_change_flags(cpriv->dev, flags & ~IFF_UP);
1581da177e4SLinus Torvalds 		}
15995ed644fSIngo Molnar 		mutex_unlock(&priv->vlan_mutex);
1601da177e4SLinus Torvalds 	}
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	return 0;
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	if (new_mtu > IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN)
1701da177e4SLinus Torvalds 		return -EINVAL;
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	priv->admin_mtu = new_mtu;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	return 0;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds static struct ipoib_path *__path_find(struct net_device *dev,
1801da177e4SLinus Torvalds 				      union ib_gid *gid)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
1831da177e4SLinus Torvalds 	struct rb_node *n = priv->path_tree.rb_node;
1841da177e4SLinus Torvalds 	struct ipoib_path *path;
1851da177e4SLinus Torvalds 	int ret;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	while (n) {
1881da177e4SLinus Torvalds 		path = rb_entry(n, struct ipoib_path, rb_node);
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 		ret = memcmp(gid->raw, path->pathrec.dgid.raw,
1911da177e4SLinus Torvalds 			     sizeof (union ib_gid));
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 		if (ret < 0)
1941da177e4SLinus Torvalds 			n = n->rb_left;
1951da177e4SLinus Torvalds 		else if (ret > 0)
1961da177e4SLinus Torvalds 			n = n->rb_right;
1971da177e4SLinus Torvalds 		else
1981da177e4SLinus Torvalds 			return path;
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	return NULL;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static int __path_add(struct net_device *dev, struct ipoib_path *path)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
2071da177e4SLinus Torvalds 	struct rb_node **n = &priv->path_tree.rb_node;
2081da177e4SLinus Torvalds 	struct rb_node *pn = NULL;
2091da177e4SLinus Torvalds 	struct ipoib_path *tpath;
2101da177e4SLinus Torvalds 	int ret;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	while (*n) {
2131da177e4SLinus Torvalds 		pn = *n;
2141da177e4SLinus Torvalds 		tpath = rb_entry(pn, struct ipoib_path, rb_node);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 		ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
2171da177e4SLinus Torvalds 			     sizeof (union ib_gid));
2181da177e4SLinus Torvalds 		if (ret < 0)
2191da177e4SLinus Torvalds 			n = &pn->rb_left;
2201da177e4SLinus Torvalds 		else if (ret > 0)
2211da177e4SLinus Torvalds 			n = &pn->rb_right;
2221da177e4SLinus Torvalds 		else
2231da177e4SLinus Torvalds 			return -EEXIST;
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	rb_link_node(&path->rb_node, pn, n);
2271da177e4SLinus Torvalds 	rb_insert_color(&path->rb_node, &priv->path_tree);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	list_add_tail(&path->list, &priv->path_list);
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	return 0;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static void path_free(struct net_device *dev, struct ipoib_path *path)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
2371da177e4SLinus Torvalds 	struct ipoib_neigh *neigh, *tn;
2381da177e4SLinus Torvalds 	struct sk_buff *skb;
2391da177e4SLinus Torvalds 	unsigned long flags;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&path->queue)))
2421da177e4SLinus Torvalds 		dev_kfree_skb_irq(skb);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
2471da177e4SLinus Torvalds 		/*
2481da177e4SLinus Torvalds 		 * It's safe to call ipoib_put_ah() inside priv->lock
2491da177e4SLinus Torvalds 		 * here, because we know that path->ah will always
2501da177e4SLinus Torvalds 		 * hold one more reference, so ipoib_put_ah() will
2511da177e4SLinus Torvalds 		 * never do more than decrement the ref count.
2521da177e4SLinus Torvalds 		 */
2531da177e4SLinus Torvalds 		if (neigh->ah)
2541da177e4SLinus Torvalds 			ipoib_put_ah(neigh->ah);
2551da177e4SLinus Torvalds 		*to_ipoib_neigh(neigh->neighbour) = NULL;
2561da177e4SLinus Torvalds 		neigh->neighbour->ops->destructor = NULL;
2571da177e4SLinus Torvalds 		kfree(neigh);
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	if (path->ah)
2631da177e4SLinus Torvalds 		ipoib_put_ah(path->ah);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	kfree(path);
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681732b0efSRoland Dreier #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2691732b0efSRoland Dreier 
2701732b0efSRoland Dreier struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
2711732b0efSRoland Dreier {
2721732b0efSRoland Dreier 	struct ipoib_path_iter *iter;
2731732b0efSRoland Dreier 
2741732b0efSRoland Dreier 	iter = kmalloc(sizeof *iter, GFP_KERNEL);
2751732b0efSRoland Dreier 	if (!iter)
2761732b0efSRoland Dreier 		return NULL;
2771732b0efSRoland Dreier 
2781732b0efSRoland Dreier 	iter->dev = dev;
2791732b0efSRoland Dreier 	memset(iter->path.pathrec.dgid.raw, 0, 16);
2801732b0efSRoland Dreier 
2811732b0efSRoland Dreier 	if (ipoib_path_iter_next(iter)) {
2821732b0efSRoland Dreier 		kfree(iter);
2831732b0efSRoland Dreier 		return NULL;
2841732b0efSRoland Dreier 	}
2851732b0efSRoland Dreier 
2861732b0efSRoland Dreier 	return iter;
2871732b0efSRoland Dreier }
2881732b0efSRoland Dreier 
2891732b0efSRoland Dreier int ipoib_path_iter_next(struct ipoib_path_iter *iter)
2901732b0efSRoland Dreier {
2911732b0efSRoland Dreier 	struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
2921732b0efSRoland Dreier 	struct rb_node *n;
2931732b0efSRoland Dreier 	struct ipoib_path *path;
2941732b0efSRoland Dreier 	int ret = 1;
2951732b0efSRoland Dreier 
2961732b0efSRoland Dreier 	spin_lock_irq(&priv->lock);
2971732b0efSRoland Dreier 
2981732b0efSRoland Dreier 	n = rb_first(&priv->path_tree);
2991732b0efSRoland Dreier 
3001732b0efSRoland Dreier 	while (n) {
3011732b0efSRoland Dreier 		path = rb_entry(n, struct ipoib_path, rb_node);
3021732b0efSRoland Dreier 
3031732b0efSRoland Dreier 		if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
3041732b0efSRoland Dreier 			   sizeof (union ib_gid)) < 0) {
3051732b0efSRoland Dreier 			iter->path = *path;
3061732b0efSRoland Dreier 			ret = 0;
3071732b0efSRoland Dreier 			break;
3081732b0efSRoland Dreier 		}
3091732b0efSRoland Dreier 
3101732b0efSRoland Dreier 		n = rb_next(n);
3111732b0efSRoland Dreier 	}
3121732b0efSRoland Dreier 
3131732b0efSRoland Dreier 	spin_unlock_irq(&priv->lock);
3141732b0efSRoland Dreier 
3151732b0efSRoland Dreier 	return ret;
3161732b0efSRoland Dreier }
3171732b0efSRoland Dreier 
3181732b0efSRoland Dreier void ipoib_path_iter_read(struct ipoib_path_iter *iter,
3191732b0efSRoland Dreier 			  struct ipoib_path *path)
3201732b0efSRoland Dreier {
3211732b0efSRoland Dreier 	*path = iter->path;
3221732b0efSRoland Dreier }
3231732b0efSRoland Dreier 
3241732b0efSRoland Dreier #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
3251732b0efSRoland Dreier 
3261da177e4SLinus Torvalds void ipoib_flush_paths(struct net_device *dev)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
3291da177e4SLinus Torvalds 	struct ipoib_path *path, *tp;
3301da177e4SLinus Torvalds 	LIST_HEAD(remove_list);
3311da177e4SLinus Torvalds 	unsigned long flags;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	list_splice(&priv->path_list, &remove_list);
3361da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->path_list);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	list_for_each_entry(path, &remove_list, list)
3391da177e4SLinus Torvalds 		rb_erase(&path->rb_node, &priv->path_tree);
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	list_for_each_entry_safe(path, tp, &remove_list, list) {
3441da177e4SLinus Torvalds 		if (path->query)
3451da177e4SLinus Torvalds 			ib_sa_cancel_query(path->query_id, path->query);
3461da177e4SLinus Torvalds 		wait_for_completion(&path->done);
3471da177e4SLinus Torvalds 		path_free(dev, path);
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds static void path_rec_completion(int status,
3521da177e4SLinus Torvalds 				struct ib_sa_path_rec *pathrec,
3531da177e4SLinus Torvalds 				void *path_ptr)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	struct ipoib_path *path = path_ptr;
3561da177e4SLinus Torvalds 	struct net_device *dev = path->dev;
3571da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
3581da177e4SLinus Torvalds 	struct ipoib_ah *ah = NULL;
3591da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
3601da177e4SLinus Torvalds 	struct sk_buff_head skqueue;
3611da177e4SLinus Torvalds 	struct sk_buff *skb;
3621da177e4SLinus Torvalds 	unsigned long flags;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	if (pathrec)
3651da177e4SLinus Torvalds 		ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
3661da177e4SLinus Torvalds 			  be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
3671da177e4SLinus Torvalds 	else
3681da177e4SLinus Torvalds 		ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
3691da177e4SLinus Torvalds 			  status, IPOIB_GID_ARG(path->pathrec.dgid));
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	skb_queue_head_init(&skqueue);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	if (!status) {
3741da177e4SLinus Torvalds 		struct ib_ah_attr av = {
3751da177e4SLinus Torvalds 			.dlid 	       = be16_to_cpu(pathrec->dlid),
3761da177e4SLinus Torvalds 			.sl 	       = pathrec->sl,
3771da177e4SLinus Torvalds 			.port_num      = priv->port
3781da177e4SLinus Torvalds 		};
379e6ded99cSRoland Dreier 		int path_rate = ib_sa_rate_enum_to_int(pathrec->rate);
3801da177e4SLinus Torvalds 
381e6ded99cSRoland Dreier 		if (path_rate > 0 && priv->local_rate > path_rate)
382e6ded99cSRoland Dreier 			av.static_rate = (priv->local_rate - 1) / path_rate;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 		ipoib_dbg(priv, "static_rate %d for local port %dX, path %dX\n",
3851da177e4SLinus Torvalds 			  av.static_rate, priv->local_rate,
3861da177e4SLinus Torvalds 			  ib_sa_rate_enum_to_int(pathrec->rate));
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 		ah = ipoib_create_ah(dev, priv->pd, &av);
3891da177e4SLinus Torvalds 	}
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	path->ah = ah;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	if (ah) {
3961da177e4SLinus Torvalds 		path->pathrec = *pathrec;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 		ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
3991da177e4SLinus Torvalds 			  ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 		while ((skb = __skb_dequeue(&path->queue)))
4021da177e4SLinus Torvalds 			__skb_queue_tail(&skqueue, skb);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 		list_for_each_entry(neigh, &path->neigh_list, list) {
4051da177e4SLinus Torvalds 			kref_get(&path->ah->ref);
4061da177e4SLinus Torvalds 			neigh->ah = path->ah;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 			while ((skb = __skb_dequeue(&neigh->queue)))
4091da177e4SLinus Torvalds 				__skb_queue_tail(&skqueue, skb);
4101da177e4SLinus Torvalds 		}
4115872a9fcSRoland Dreier 	}
4121da177e4SLinus Torvalds 
4135872a9fcSRoland Dreier 	path->query = NULL;
4141da177e4SLinus Torvalds 	complete(&path->done);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	while ((skb = __skb_dequeue(&skqueue))) {
4191da177e4SLinus Torvalds 		skb->dev = dev;
4201da177e4SLinus Torvalds 		if (dev_queue_xmit(skb))
4211da177e4SLinus Torvalds 			ipoib_warn(priv, "dev_queue_xmit failed "
4221da177e4SLinus Torvalds 				   "to requeue packet\n");
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds static struct ipoib_path *path_rec_create(struct net_device *dev,
4271da177e4SLinus Torvalds 					  union ib_gid *gid)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
4301da177e4SLinus Torvalds 	struct ipoib_path *path;
4311da177e4SLinus Torvalds 
43221a38489SRoland Dreier 	path = kzalloc(sizeof *path, GFP_ATOMIC);
4331da177e4SLinus Torvalds 	if (!path)
4341da177e4SLinus Torvalds 		return NULL;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	path->dev = dev;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	skb_queue_head_init(&path->queue);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	INIT_LIST_HEAD(&path->neigh_list);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	memcpy(path->pathrec.dgid.raw, gid->raw, sizeof (union ib_gid));
4431da177e4SLinus Torvalds 	path->pathrec.sgid      = priv->local_gid;
4441da177e4SLinus Torvalds 	path->pathrec.pkey      = cpu_to_be16(priv->pkey);
4451da177e4SLinus Torvalds 	path->pathrec.numb_path = 1;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	return path;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static int path_rec_start(struct net_device *dev,
4511da177e4SLinus Torvalds 			  struct ipoib_path *path)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
4561da177e4SLinus Torvalds 		  IPOIB_GID_ARG(path->pathrec.dgid));
4571da177e4SLinus Torvalds 
45865c7eddaSRoland Dreier 	init_completion(&path->done);
45965c7eddaSRoland Dreier 
4601da177e4SLinus Torvalds 	path->query_id =
4611da177e4SLinus Torvalds 		ib_sa_path_rec_get(priv->ca, priv->port,
4621da177e4SLinus Torvalds 				   &path->pathrec,
4631da177e4SLinus Torvalds 				   IB_SA_PATH_REC_DGID		|
4641da177e4SLinus Torvalds 				   IB_SA_PATH_REC_SGID		|
4651da177e4SLinus Torvalds 				   IB_SA_PATH_REC_NUMB_PATH	|
4661da177e4SLinus Torvalds 				   IB_SA_PATH_REC_PKEY,
4671da177e4SLinus Torvalds 				   1000, GFP_ATOMIC,
4681da177e4SLinus Torvalds 				   path_rec_completion,
4691da177e4SLinus Torvalds 				   path, &path->query);
4701da177e4SLinus Torvalds 	if (path->query_id < 0) {
4711da177e4SLinus Torvalds 		ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
4721da177e4SLinus Torvalds 		path->query = NULL;
4731da177e4SLinus Torvalds 		return path->query_id;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	return 0;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
4821da177e4SLinus Torvalds 	struct ipoib_path *path;
4831da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
4861da177e4SLinus Torvalds 	if (!neigh) {
4871da177e4SLinus Torvalds 		++priv->stats.tx_dropped;
4881da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
4891da177e4SLinus Torvalds 		return;
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	skb_queue_head_init(&neigh->queue);
4931da177e4SLinus Torvalds 	neigh->neighbour = skb->dst->neighbour;
4941da177e4SLinus Torvalds 	*to_ipoib_neigh(skb->dst->neighbour) = neigh;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	/*
4971da177e4SLinus Torvalds 	 * We can only be called from ipoib_start_xmit, so we're
4981da177e4SLinus Torvalds 	 * inside tx_lock -- no need to save/restore flags.
4991da177e4SLinus Torvalds 	 */
5001da177e4SLinus Torvalds 	spin_lock(&priv->lock);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	path = __path_find(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4));
5031da177e4SLinus Torvalds 	if (!path) {
5041da177e4SLinus Torvalds 		path = path_rec_create(dev,
5051da177e4SLinus Torvalds 				       (union ib_gid *) (skb->dst->neighbour->ha + 4));
5061da177e4SLinus Torvalds 		if (!path)
5071da177e4SLinus Torvalds 			goto err;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 		__path_add(dev, path);
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	list_add_tail(&neigh->list, &path->neigh_list);
5131da177e4SLinus Torvalds 
51447f7a071SMichael S. Tsirkin 	if (path->ah) {
5151da177e4SLinus Torvalds 		kref_get(&path->ah->ref);
5161da177e4SLinus Torvalds 		neigh->ah = path->ah;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 		ipoib_send(dev, skb, path->ah,
5191da177e4SLinus Torvalds 			   be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
5201da177e4SLinus Torvalds 	} else {
5211da177e4SLinus Torvalds 		neigh->ah  = NULL;
5221da177e4SLinus Torvalds 		if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
5231da177e4SLinus Torvalds 			__skb_queue_tail(&neigh->queue, skb);
5241da177e4SLinus Torvalds 		} else {
5251da177e4SLinus Torvalds 			++priv->stats.tx_dropped;
5261da177e4SLinus Torvalds 			dev_kfree_skb_any(skb);
5271da177e4SLinus Torvalds 		}
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 		if (!path->query && path_rec_start(dev, path))
5301da177e4SLinus Torvalds 			goto err;
5311da177e4SLinus Torvalds 	}
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	spin_unlock(&priv->lock);
5341da177e4SLinus Torvalds 	return;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds err:
5371da177e4SLinus Torvalds 	*to_ipoib_neigh(skb->dst->neighbour) = NULL;
5381da177e4SLinus Torvalds 	list_del(&neigh->list);
5391da177e4SLinus Torvalds 	neigh->neighbour->ops->destructor = NULL;
5401da177e4SLinus Torvalds 	kfree(neigh);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	++priv->stats.tx_dropped;
5431da177e4SLinus Torvalds 	dev_kfree_skb_any(skb);
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	spin_unlock(&priv->lock);
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
548d70ed607SRoland Dreier static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	/* Look up path record for unicasts */
5531da177e4SLinus Torvalds 	if (skb->dst->neighbour->ha[4] != 0xff) {
5541da177e4SLinus Torvalds 		neigh_add_path(skb, dev);
5551da177e4SLinus Torvalds 		return;
5561da177e4SLinus Torvalds 	}
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	/* Add in the P_Key for multicasts */
5591da177e4SLinus Torvalds 	skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
5601da177e4SLinus Torvalds 	skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
5611da177e4SLinus Torvalds 	ipoib_mcast_send(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4), skb);
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
5651da177e4SLinus Torvalds 			     struct ipoib_pseudoheader *phdr)
5661da177e4SLinus Torvalds {
5671da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
5681da177e4SLinus Torvalds 	struct ipoib_path *path;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	/*
5711da177e4SLinus Torvalds 	 * We can only be called from ipoib_start_xmit, so we're
5721da177e4SLinus Torvalds 	 * inside tx_lock -- no need to save/restore flags.
5731da177e4SLinus Torvalds 	 */
5741da177e4SLinus Torvalds 	spin_lock(&priv->lock);
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	path = __path_find(dev, (union ib_gid *) (phdr->hwaddr + 4));
5771da177e4SLinus Torvalds 	if (!path) {
5781da177e4SLinus Torvalds 		path = path_rec_create(dev,
5791da177e4SLinus Torvalds 				       (union ib_gid *) (phdr->hwaddr + 4));
5801da177e4SLinus Torvalds 		if (path) {
5811da177e4SLinus Torvalds 			/* put pseudoheader back on for next time */
5821da177e4SLinus Torvalds 			skb_push(skb, sizeof *phdr);
5831da177e4SLinus Torvalds 			__skb_queue_tail(&path->queue, skb);
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 			if (path_rec_start(dev, path)) {
5861da177e4SLinus Torvalds 				spin_unlock(&priv->lock);
5871da177e4SLinus Torvalds 				path_free(dev, path);
5881da177e4SLinus Torvalds 				return;
5891da177e4SLinus Torvalds 			} else
5901da177e4SLinus Torvalds 				__path_add(dev, path);
5911da177e4SLinus Torvalds 		} else {
5921da177e4SLinus Torvalds 			++priv->stats.tx_dropped;
5931da177e4SLinus Torvalds 			dev_kfree_skb_any(skb);
5941da177e4SLinus Torvalds 		}
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 		spin_unlock(&priv->lock);
5971da177e4SLinus Torvalds 		return;
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
60047f7a071SMichael S. Tsirkin 	if (path->ah) {
6011da177e4SLinus Torvalds 		ipoib_dbg(priv, "Send unicast ARP to %04x\n",
6021da177e4SLinus Torvalds 			  be16_to_cpu(path->pathrec.dlid));
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 		ipoib_send(dev, skb, path->ah,
6051da177e4SLinus Torvalds 			   be32_to_cpup((__be32 *) phdr->hwaddr));
6061da177e4SLinus Torvalds 	} else if ((path->query || !path_rec_start(dev, path)) &&
6071da177e4SLinus Torvalds 		   skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
6081da177e4SLinus Torvalds 		/* put pseudoheader back on for next time */
6091da177e4SLinus Torvalds 		skb_push(skb, sizeof *phdr);
6101da177e4SLinus Torvalds 		__skb_queue_tail(&path->queue, skb);
6111da177e4SLinus Torvalds 	} else {
6121da177e4SLinus Torvalds 		++priv->stats.tx_dropped;
6131da177e4SLinus Torvalds 		dev_kfree_skb_any(skb);
6141da177e4SLinus Torvalds 	}
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	spin_unlock(&priv->lock);
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
6201da177e4SLinus Torvalds {
6211da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
6221da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
6231da177e4SLinus Torvalds 	unsigned long flags;
6241da177e4SLinus Torvalds 
625a20583a7SRoland Dreier 	if (!spin_trylock_irqsave(&priv->tx_lock, flags))
6261da177e4SLinus Torvalds 		return NETDEV_TX_LOCKED;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	/*
6291da177e4SLinus Torvalds 	 * Check if our queue is stopped.  Since we have the LLTX bit
6301da177e4SLinus Torvalds 	 * set, we can't rely on netif_stop_queue() preventing our
6311da177e4SLinus Torvalds 	 * xmit function from being called with a full queue.
6321da177e4SLinus Torvalds 	 */
6331da177e4SLinus Torvalds 	if (unlikely(netif_queue_stopped(dev))) {
6341da177e4SLinus Torvalds 		spin_unlock_irqrestore(&priv->tx_lock, flags);
6351da177e4SLinus Torvalds 		return NETDEV_TX_BUSY;
6361da177e4SLinus Torvalds 	}
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (skb->dst && skb->dst->neighbour) {
6391da177e4SLinus Torvalds 		if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
640d70ed607SRoland Dreier 			ipoib_path_lookup(skb, dev);
6411da177e4SLinus Torvalds 			goto out;
6421da177e4SLinus Torvalds 		}
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 		neigh = *to_ipoib_neigh(skb->dst->neighbour);
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 		if (likely(neigh->ah)) {
6471da177e4SLinus Torvalds 			ipoib_send(dev, skb, neigh->ah,
6481da177e4SLinus Torvalds 				   be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
6491da177e4SLinus Torvalds 			goto out;
6501da177e4SLinus Torvalds 		}
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 		if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
6531da177e4SLinus Torvalds 			spin_lock(&priv->lock);
6541da177e4SLinus Torvalds 			__skb_queue_tail(&neigh->queue, skb);
6551da177e4SLinus Torvalds 			spin_unlock(&priv->lock);
6561da177e4SLinus Torvalds 		} else {
6571da177e4SLinus Torvalds 			++priv->stats.tx_dropped;
6581da177e4SLinus Torvalds 			dev_kfree_skb_any(skb);
6591da177e4SLinus Torvalds 		}
6601da177e4SLinus Torvalds 	} else {
6611da177e4SLinus Torvalds 		struct ipoib_pseudoheader *phdr =
6621da177e4SLinus Torvalds 			(struct ipoib_pseudoheader *) skb->data;
6631da177e4SLinus Torvalds 		skb_pull(skb, sizeof *phdr);
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 		if (phdr->hwaddr[4] == 0xff) {
6661da177e4SLinus Torvalds 			/* Add in the P_Key for multicast*/
6671da177e4SLinus Torvalds 			phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
6681da177e4SLinus Torvalds 			phdr->hwaddr[9] = priv->pkey & 0xff;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 			ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
6711da177e4SLinus Torvalds 		} else {
6720dca0f7bSHal Rosenstock 			/* unicast GID -- should be ARP or RARP reply */
6731da177e4SLinus Torvalds 
6740dca0f7bSHal Rosenstock 			if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
6750dca0f7bSHal Rosenstock 			    (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
6761da177e4SLinus Torvalds 				ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
6771da177e4SLinus Torvalds 					   IPOIB_GID_FMT "\n",
6781da177e4SLinus Torvalds 					   skb->dst ? "neigh" : "dst",
67997f52eb4SSean Hefty 					   be16_to_cpup((__be16 *) skb->data),
68097f52eb4SSean Hefty 					   be32_to_cpup((__be32 *) phdr->hwaddr),
6811da177e4SLinus Torvalds 					   IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
6821da177e4SLinus Torvalds 				dev_kfree_skb_any(skb);
6831da177e4SLinus Torvalds 				++priv->stats.tx_dropped;
6841da177e4SLinus Torvalds 				goto out;
6851da177e4SLinus Torvalds 			}
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 			unicast_arp_send(skb, dev, phdr);
6881da177e4SLinus Torvalds 		}
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds out:
6921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->tx_lock, flags);
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	return NETDEV_TX_OK;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	return &priv->stats;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds static void ipoib_timeout(struct net_device *dev)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
7071da177e4SLinus Torvalds 
7084b2d319bSRoland Dreier 	ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
7094b2d319bSRoland Dreier 		   jiffies_to_msecs(jiffies - dev->trans_start));
7104b2d319bSRoland Dreier 	ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
7114b2d319bSRoland Dreier 		   netif_queue_stopped(dev),
7124b2d319bSRoland Dreier 		   priv->tx_head, priv->tx_tail);
7131da177e4SLinus Torvalds 	/* XXX reset QP, etc. */
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds static int ipoib_hard_header(struct sk_buff *skb,
7171da177e4SLinus Torvalds 			     struct net_device *dev,
7181da177e4SLinus Torvalds 			     unsigned short type,
7191da177e4SLinus Torvalds 			     void *daddr, void *saddr, unsigned len)
7201da177e4SLinus Torvalds {
7211da177e4SLinus Torvalds 	struct ipoib_header *header;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	header = (struct ipoib_header *) skb_push(skb, sizeof *header);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	header->proto = htons(type);
7261da177e4SLinus Torvalds 	header->reserved = 0;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	/*
7291da177e4SLinus Torvalds 	 * If we don't have a neighbour structure, stuff the
7301da177e4SLinus Torvalds 	 * destination address onto the front of the skb so we can
7311da177e4SLinus Torvalds 	 * figure out where to send the packet later.
7321da177e4SLinus Torvalds 	 */
7331da177e4SLinus Torvalds 	if (!skb->dst || !skb->dst->neighbour) {
7341da177e4SLinus Torvalds 		struct ipoib_pseudoheader *phdr =
7351da177e4SLinus Torvalds 			(struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
7361da177e4SLinus Torvalds 		memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	return 0;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds static void ipoib_set_mcast_list(struct net_device *dev)
7431da177e4SLinus Torvalds {
7441da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
7451da177e4SLinus Torvalds 
7461ad62a19SMichael S. Tsirkin 	queue_work(ipoib_workqueue, &priv->restart_task);
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds static void ipoib_neigh_destructor(struct neighbour *n)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	struct ipoib_neigh *neigh;
7521da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(n->dev);
7531da177e4SLinus Torvalds 	unsigned long flags;
7541da177e4SLinus Torvalds 	struct ipoib_ah *ah = NULL;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	ipoib_dbg(priv,
7571da177e4SLinus Torvalds 		  "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
7581da177e4SLinus Torvalds 		  be32_to_cpup((__be32 *) n->ha),
7591da177e4SLinus Torvalds 		  IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	neigh = *to_ipoib_neigh(n);
7641da177e4SLinus Torvalds 	if (neigh) {
7651da177e4SLinus Torvalds 		if (neigh->ah)
7661da177e4SLinus Torvalds 			ah = neigh->ah;
7671da177e4SLinus Torvalds 		list_del(&neigh->list);
7681da177e4SLinus Torvalds 		*to_ipoib_neigh(n) = NULL;
7691da177e4SLinus Torvalds 		kfree(neigh);
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	if (ah)
7751da177e4SLinus Torvalds 		ipoib_put_ah(ah);
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds static int ipoib_neigh_setup(struct neighbour *neigh)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	/*
7811da177e4SLinus Torvalds 	 * Is this kosher?  I can't find anybody in the kernel that
7821da177e4SLinus Torvalds 	 * sets neigh->destructor, so we should be able to set it here
7831da177e4SLinus Torvalds 	 * without trouble.
7841da177e4SLinus Torvalds 	 */
7851da177e4SLinus Torvalds 	neigh->ops->destructor = ipoib_neigh_destructor;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	return 0;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	parms->neigh_setup = ipoib_neigh_setup;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	return 0;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
7981da177e4SLinus Torvalds {
7991da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	/* Allocate RX/TX "rings" to hold queued skbs */
8021da177e4SLinus Torvalds 
803de6eb66bSRoland Dreier 	priv->rx_ring =	kzalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_rx_buf),
8041da177e4SLinus Torvalds 				GFP_KERNEL);
8051da177e4SLinus Torvalds 	if (!priv->rx_ring) {
8061da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
8071da177e4SLinus Torvalds 		       ca->name, IPOIB_RX_RING_SIZE);
8081da177e4SLinus Torvalds 		goto out;
8091da177e4SLinus Torvalds 	}
8101da177e4SLinus Torvalds 
811de6eb66bSRoland Dreier 	priv->tx_ring = kzalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_tx_buf),
8121da177e4SLinus Torvalds 				GFP_KERNEL);
8131da177e4SLinus Torvalds 	if (!priv->tx_ring) {
8141da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
8151da177e4SLinus Torvalds 		       ca->name, IPOIB_TX_RING_SIZE);
8161da177e4SLinus Torvalds 		goto out_rx_ring_cleanup;
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	/* priv->tx_head & tx_tail are already 0 */
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	if (ipoib_ib_dev_init(dev, ca, port))
8221da177e4SLinus Torvalds 		goto out_tx_ring_cleanup;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	return 0;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds out_tx_ring_cleanup:
8271da177e4SLinus Torvalds 	kfree(priv->tx_ring);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds out_rx_ring_cleanup:
8301da177e4SLinus Torvalds 	kfree(priv->rx_ring);
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds out:
8331da177e4SLinus Torvalds 	return -ENOMEM;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds void ipoib_dev_cleanup(struct net_device *dev)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
8391da177e4SLinus Torvalds 
8401732b0efSRoland Dreier 	ipoib_delete_debug_files(dev);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	/* Delete any child interfaces first */
8431da177e4SLinus Torvalds 	list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
8441da177e4SLinus Torvalds 		unregister_netdev(cpriv->dev);
8451da177e4SLinus Torvalds 		ipoib_dev_cleanup(cpriv->dev);
8461da177e4SLinus Torvalds 		free_netdev(cpriv->dev);
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	ipoib_ib_dev_cleanup(dev);
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	kfree(priv->rx_ring);
8521da177e4SLinus Torvalds 	kfree(priv->tx_ring);
85392a6b34bSHal Rosenstock 
85492a6b34bSHal Rosenstock 	priv->rx_ring = NULL;
8551da177e4SLinus Torvalds 	priv->tx_ring = NULL;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds static void ipoib_setup(struct net_device *dev)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv = netdev_priv(dev);
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	dev->open 		 = ipoib_open;
8631da177e4SLinus Torvalds 	dev->stop 		 = ipoib_stop;
8641da177e4SLinus Torvalds 	dev->change_mtu 	 = ipoib_change_mtu;
8651da177e4SLinus Torvalds 	dev->hard_start_xmit 	 = ipoib_start_xmit;
8661da177e4SLinus Torvalds 	dev->get_stats 		 = ipoib_get_stats;
8671da177e4SLinus Torvalds 	dev->tx_timeout 	 = ipoib_timeout;
8681da177e4SLinus Torvalds 	dev->hard_header 	 = ipoib_hard_header;
8691da177e4SLinus Torvalds 	dev->set_multicast_list  = ipoib_set_mcast_list;
8701da177e4SLinus Torvalds 	dev->neigh_setup         = ipoib_neigh_setup_dev;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	dev->watchdog_timeo 	 = HZ;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	dev->flags              |= IFF_BROADCAST | IFF_MULTICAST;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	/*
8771da177e4SLinus Torvalds 	 * We add in INFINIBAND_ALEN to allow for the destination
8781da177e4SLinus Torvalds 	 * address "pseudoheader" for skbs without neighbour struct.
8791da177e4SLinus Torvalds 	 */
8801da177e4SLinus Torvalds 	dev->hard_header_len 	 = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
8811da177e4SLinus Torvalds 	dev->addr_len 		 = INFINIBAND_ALEN;
8821da177e4SLinus Torvalds 	dev->type 		 = ARPHRD_INFINIBAND;
8831da177e4SLinus Torvalds 	dev->tx_queue_len 	 = IPOIB_TX_RING_SIZE * 2;
8841da177e4SLinus Torvalds 	dev->features            = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	/* MTU will be reset when mcast join happens */
8871da177e4SLinus Torvalds 	dev->mtu 		 = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
8881da177e4SLinus Torvalds 	priv->mcast_mtu 	 = priv->admin_mtu = dev->mtu;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	netif_carrier_off(dev);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	SET_MODULE_OWNER(dev);
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	priv->dev = dev;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
8991da177e4SLinus Torvalds 	spin_lock_init(&priv->tx_lock);
9001da177e4SLinus Torvalds 
90195ed644fSIngo Molnar 	mutex_init(&priv->mcast_mutex);
90295ed644fSIngo Molnar 	mutex_init(&priv->vlan_mutex);
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->path_list);
9051da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->child_intfs);
9061da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->dead_ahs);
9071da177e4SLinus Torvalds 	INIT_LIST_HEAD(&priv->multicast_list);
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	INIT_WORK(&priv->pkey_task,    ipoib_pkey_poll,          priv->dev);
9101da177e4SLinus Torvalds 	INIT_WORK(&priv->mcast_task,   ipoib_mcast_join_task,    priv->dev);
9111da177e4SLinus Torvalds 	INIT_WORK(&priv->flush_task,   ipoib_ib_dev_flush,       priv->dev);
9121da177e4SLinus Torvalds 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
9131da177e4SLinus Torvalds 	INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah,            priv->dev);
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
9171da177e4SLinus Torvalds {
9181da177e4SLinus Torvalds 	struct net_device *dev;
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
9211da177e4SLinus Torvalds 			   ipoib_setup);
9221da177e4SLinus Torvalds 	if (!dev)
9231da177e4SLinus Torvalds 		return NULL;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	return netdev_priv(dev);
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds static ssize_t show_pkey(struct class_device *cdev, char *buf)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv =
9311da177e4SLinus Torvalds 		netdev_priv(container_of(cdev, struct net_device, class_dev));
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	return sprintf(buf, "0x%04x\n", priv->pkey);
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds static ssize_t create_child(struct class_device *cdev,
9381da177e4SLinus Torvalds 			    const char *buf, size_t count)
9391da177e4SLinus Torvalds {
9401da177e4SLinus Torvalds 	int pkey;
9411da177e4SLinus Torvalds 	int ret;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
9441da177e4SLinus Torvalds 		return -EINVAL;
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	if (pkey < 0 || pkey > 0xffff)
9471da177e4SLinus Torvalds 		return -EINVAL;
9481da177e4SLinus Torvalds 
9494ce05937SRoland Dreier 	/*
9504ce05937SRoland Dreier 	 * Set the full membership bit, so that we join the right
9514ce05937SRoland Dreier 	 * broadcast group, etc.
9524ce05937SRoland Dreier 	 */
9534ce05937SRoland Dreier 	pkey |= 0x8000;
9544ce05937SRoland Dreier 
9551da177e4SLinus Torvalds 	ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
9561da177e4SLinus Torvalds 			     pkey);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	return ret ? ret : count;
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds static ssize_t delete_child(struct class_device *cdev,
9631da177e4SLinus Torvalds 			    const char *buf, size_t count)
9641da177e4SLinus Torvalds {
9651da177e4SLinus Torvalds 	int pkey;
9661da177e4SLinus Torvalds 	int ret;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	if (sscanf(buf, "%i", &pkey) != 1)
9691da177e4SLinus Torvalds 		return -EINVAL;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	if (pkey < 0 || pkey > 0xffff)
9721da177e4SLinus Torvalds 		return -EINVAL;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
9751da177e4SLinus Torvalds 				pkey);
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	return ret ? ret : count;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds int ipoib_add_pkey_attr(struct net_device *dev)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	return class_device_create_file(&dev->class_dev,
9851da177e4SLinus Torvalds 					&class_device_attr_pkey);
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds static struct net_device *ipoib_add_port(const char *format,
9891da177e4SLinus Torvalds 					 struct ib_device *hca, u8 port)
9901da177e4SLinus Torvalds {
9911da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
9921da177e4SLinus Torvalds 	int result = -ENOMEM;
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	priv = ipoib_intf_alloc(format);
9951da177e4SLinus Torvalds 	if (!priv)
9961da177e4SLinus Torvalds 		goto alloc_mem_failed;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	SET_NETDEV_DEV(priv->dev, hca->dma_device);
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	result = ib_query_pkey(hca, port, 0, &priv->pkey);
10011da177e4SLinus Torvalds 	if (result) {
10021da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
10031da177e4SLinus Torvalds 		       hca->name, port, result);
10041da177e4SLinus Torvalds 		goto alloc_mem_failed;
10051da177e4SLinus Torvalds 	}
10061da177e4SLinus Torvalds 
10074ce05937SRoland Dreier 	/*
10084ce05937SRoland Dreier 	 * Set the full membership bit, so that we join the right
10094ce05937SRoland Dreier 	 * broadcast group, etc.
10104ce05937SRoland Dreier 	 */
10114ce05937SRoland Dreier 	priv->pkey |= 0x8000;
10124ce05937SRoland Dreier 
10131da177e4SLinus Torvalds 	priv->dev->broadcast[8] = priv->pkey >> 8;
10141da177e4SLinus Torvalds 	priv->dev->broadcast[9] = priv->pkey & 0xff;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	result = ib_query_gid(hca, port, 0, &priv->local_gid);
10171da177e4SLinus Torvalds 	if (result) {
10181da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
10191da177e4SLinus Torvalds 		       hca->name, port, result);
10201da177e4SLinus Torvalds 		goto alloc_mem_failed;
10211da177e4SLinus Torvalds 	} else
10221da177e4SLinus Torvalds 		memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	result = ipoib_dev_init(priv->dev, hca, port);
10261da177e4SLinus Torvalds 	if (result < 0) {
10271da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
10281da177e4SLinus Torvalds 		       hca->name, port, result);
10291da177e4SLinus Torvalds 		goto device_init_failed;
10301da177e4SLinus Torvalds 	}
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
10331da177e4SLinus Torvalds 			      priv->ca, ipoib_event);
10341da177e4SLinus Torvalds 	result = ib_register_event_handler(&priv->event_handler);
10351da177e4SLinus Torvalds 	if (result < 0) {
10361da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: ib_register_event_handler failed for "
10371da177e4SLinus Torvalds 		       "port %d (ret = %d)\n",
10381da177e4SLinus Torvalds 		       hca->name, port, result);
10391da177e4SLinus Torvalds 		goto event_failed;
10401da177e4SLinus Torvalds 	}
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	result = register_netdev(priv->dev);
10431da177e4SLinus Torvalds 	if (result) {
10441da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
10451da177e4SLinus Torvalds 		       hca->name, port, result);
10461da177e4SLinus Torvalds 		goto register_failed;
10471da177e4SLinus Torvalds 	}
10481da177e4SLinus Torvalds 
10491732b0efSRoland Dreier 	ipoib_create_debug_files(priv->dev);
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	if (ipoib_add_pkey_attr(priv->dev))
10521da177e4SLinus Torvalds 		goto sysfs_failed;
10531da177e4SLinus Torvalds 	if (class_device_create_file(&priv->dev->class_dev,
10541da177e4SLinus Torvalds 				     &class_device_attr_create_child))
10551da177e4SLinus Torvalds 		goto sysfs_failed;
10561da177e4SLinus Torvalds 	if (class_device_create_file(&priv->dev->class_dev,
10571da177e4SLinus Torvalds 				     &class_device_attr_delete_child))
10581da177e4SLinus Torvalds 		goto sysfs_failed;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	return priv->dev;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds sysfs_failed:
10631732b0efSRoland Dreier 	ipoib_delete_debug_files(priv->dev);
10641da177e4SLinus Torvalds 	unregister_netdev(priv->dev);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds register_failed:
10671da177e4SLinus Torvalds 	ib_unregister_event_handler(&priv->event_handler);
106851574e03SMichael S. Tsirkin 	flush_scheduled_work();
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds event_failed:
10711da177e4SLinus Torvalds 	ipoib_dev_cleanup(priv->dev);
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds device_init_failed:
10741da177e4SLinus Torvalds 	free_netdev(priv->dev);
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds alloc_mem_failed:
10771da177e4SLinus Torvalds 	return ERR_PTR(result);
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds static void ipoib_add_one(struct ib_device *device)
10811da177e4SLinus Torvalds {
10821da177e4SLinus Torvalds 	struct list_head *dev_list;
10831da177e4SLinus Torvalds 	struct net_device *dev;
10841da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv;
10851da177e4SLinus Torvalds 	int s, e, p;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
10881da177e4SLinus Torvalds 	if (!dev_list)
10891da177e4SLinus Torvalds 		return;
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	INIT_LIST_HEAD(dev_list);
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	if (device->node_type == IB_NODE_SWITCH) {
10941da177e4SLinus Torvalds 		s = 0;
10951da177e4SLinus Torvalds 		e = 0;
10961da177e4SLinus Torvalds 	} else {
10971da177e4SLinus Torvalds 		s = 1;
10981da177e4SLinus Torvalds 		e = device->phys_port_cnt;
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	for (p = s; p <= e; ++p) {
11021da177e4SLinus Torvalds 		dev = ipoib_add_port("ib%d", device, p);
11031da177e4SLinus Torvalds 		if (!IS_ERR(dev)) {
11041da177e4SLinus Torvalds 			priv = netdev_priv(dev);
11051da177e4SLinus Torvalds 			list_add_tail(&priv->list, dev_list);
11061da177e4SLinus Torvalds 		}
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	ib_set_client_data(device, &ipoib_client, dev_list);
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds static void ipoib_remove_one(struct ib_device *device)
11131da177e4SLinus Torvalds {
11141da177e4SLinus Torvalds 	struct ipoib_dev_priv *priv, *tmp;
11151da177e4SLinus Torvalds 	struct list_head *dev_list;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	dev_list = ib_get_client_data(device, &ipoib_client);
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
11201da177e4SLinus Torvalds 		ib_unregister_event_handler(&priv->event_handler);
112151574e03SMichael S. Tsirkin 		flush_scheduled_work();
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 		unregister_netdev(priv->dev);
11241da177e4SLinus Torvalds 		ipoib_dev_cleanup(priv->dev);
11251da177e4SLinus Torvalds 		free_netdev(priv->dev);
11261da177e4SLinus Torvalds 	}
112706c56e44SMichael S. Tsirkin 
112806c56e44SMichael S. Tsirkin 	kfree(dev_list);
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds static int __init ipoib_init_module(void)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds 	int ret;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	ret = ipoib_register_debugfs();
11361da177e4SLinus Torvalds 	if (ret)
11371da177e4SLinus Torvalds 		return ret;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	/*
11401da177e4SLinus Torvalds 	 * We create our own workqueue mainly because we want to be
11411da177e4SLinus Torvalds 	 * able to flush it when devices are being removed.  We can't
11421da177e4SLinus Torvalds 	 * use schedule_work()/flush_scheduled_work() because both
11431da177e4SLinus Torvalds 	 * unregister_netdev() and linkwatch_event take the rtnl lock,
11441da177e4SLinus Torvalds 	 * so flush_scheduled_work() can deadlock during device
11451da177e4SLinus Torvalds 	 * removal.
11461da177e4SLinus Torvalds 	 */
11471da177e4SLinus Torvalds 	ipoib_workqueue = create_singlethread_workqueue("ipoib");
11481da177e4SLinus Torvalds 	if (!ipoib_workqueue) {
11491da177e4SLinus Torvalds 		ret = -ENOMEM;
11501da177e4SLinus Torvalds 		goto err_fs;
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	ret = ib_register_client(&ipoib_client);
11541da177e4SLinus Torvalds 	if (ret)
11551da177e4SLinus Torvalds 		goto err_wq;
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	return 0;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds err_wq:
11601da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
11611da177e4SLinus Torvalds 
11629adec1a8SRoland Dreier err_fs:
11639adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
11649adec1a8SRoland Dreier 
11651da177e4SLinus Torvalds 	return ret;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds static void __exit ipoib_cleanup_module(void)
11691da177e4SLinus Torvalds {
11701da177e4SLinus Torvalds 	ib_unregister_client(&ipoib_client);
11719adec1a8SRoland Dreier 	ipoib_unregister_debugfs();
11721da177e4SLinus Torvalds 	destroy_workqueue(ipoib_workqueue);
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds module_init(ipoib_init_module);
11761da177e4SLinus Torvalds module_exit(ipoib_cleanup_module);
1177