11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (c) 2004 Topspin Communications.  All rights reserved.
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This software is available to you under a choice of one of two
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * licenses.  You may choose to be licensed under the terms of the GNU
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * General Public License (GPL) Version 2, available from the file
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * COPYING in the main directory of this source tree, or the
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * OpenIB.org BSD license below:
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     Redistribution and use in source and binary forms, with or
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     without modification, are permitted provided that the following
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     conditions are met:
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *      - Redistributions of source code must retain the above
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *        copyright notice, this list of conditions and the following
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *        disclaimer.
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *      - Redistributions in binary form must reproduce the above
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *        copyright notice, this list of conditions and the following
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *        disclaimer in the documentation and/or other materials
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *        provided with the distribution.
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * SOFTWARE.
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/seq_file.h>
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/uaccess.h>
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "ipoib.h"
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4243cb76d91ee85f579a69d42bc8efc08bac560278Greg Kroah-Hartmanstatic ssize_t show_parent(struct device *d, struct device_attribute *attr,
4343cb76d91ee85f579a69d42bc8efc08bac560278Greg Kroah-Hartman			   char *buf)
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4543cb76d91ee85f579a69d42bc8efc08bac560278Greg Kroah-Hartman	struct net_device *dev = to_net_dev(d);
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct ipoib_dev_priv *priv = netdev_priv(dev);
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%s\n", priv->parent->name);
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5043cb76d91ee85f579a69d42bc8efc08bac560278Greg Kroah-Hartmanstatic DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct ipoib_dev_priv *ppriv, *priv;
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char intf_name[IFNAMSIZ];
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int result;
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!capable(CAP_NET_ADMIN))
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EPERM;
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ppriv = netdev_priv(pdev);
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6326574401fef6766f6c3ca25b5c13febe662d2a32Eric W. Biederman	if (!rtnl_trylock())
6426574401fef6766f6c3ca25b5c13febe662d2a32Eric W. Biederman		return restart_syscall();
6595ed644fd12f53c6fc778f3f246974e5fe3a9468Ingo Molnar	mutex_lock(&ppriv->vlan_mutex);
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * First ensure this isn't a duplicate. We check the parent device and
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * then all of the child interfaces to make sure the Pkey doesn't match.
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ppriv->pkey == pkey) {
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		result = -ENOTUNIQ;
73edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier		priv = NULL;
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry(priv, &ppriv->child_intfs, list) {
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (priv->pkey == pkey) {
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			result = -ENOTUNIQ;
80edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier			priv = NULL;
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto err;
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	snprintf(intf_name, sizeof intf_name, "%s.%04x",
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 ppriv->dev->name, pkey);
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv = ipoib_intf_alloc(intf_name);
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!priv) {
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		result = -ENOMEM;
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
93bc7b3a36ba02e4053ca38653e6a753082d9add03Shirley Ma	priv->max_ib_mtu = ppriv->max_ib_mtu;
94b4132efa1a47858d741ecb05b8735e6fcb603bc8Eli Cohen	/* MTU will be reset when mcast join happens */
95b4132efa1a47858d741ecb05b8735e6fcb603bc8Eli Cohen	priv->dev->mtu   = IPOIB_UD_MTU(priv->max_ib_mtu);
96b4132efa1a47858d741ecb05b8735e6fcb603bc8Eli Cohen	priv->mcast_mtu  = priv->admin_mtu = priv->dev->mtu;
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9983bb63f62bda28be88b21216fbb59838a10f2348Or Gerlitz	result = ipoib_set_dev_features(priv, ppriv->ca);
10083bb63f62bda28be88b21216fbb59838a10f2348Or Gerlitz	if (result)
101edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier		goto err;
10283bb63f62bda28be88b21216fbb59838a10f2348Or Gerlitz
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->pkey = pkey;
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(priv->dev->dev_addr, ppriv->dev->dev_addr, INFINIBAND_ALEN);
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dev->broadcast[8] = pkey >> 8;
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dev->broadcast[9] = pkey & 0xff;
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	result = ipoib_dev_init(priv->dev, ppriv->ca, ppriv->port);
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (result < 0) {
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ipoib_warn(ppriv, "failed to initialize subinterface: "
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			   "device %s, port %d",
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			   ppriv->ca->name, ppriv->port);
114edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier		goto err;
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
117cbbe1efa4972350286b52cb48aefaa11e198c0fbRoland Dreier	result = register_netdevice(priv->dev);
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (result) {
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ipoib_warn(priv, "failed to initialize; error %i", result);
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto register_failed;
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->parent = ppriv->dev;
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1251732b0ef3b3a02e3df328086fb3018741c5476daRoland Dreier	ipoib_create_debug_files(priv->dev);
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
127839fcaba355abaffb7b44f0f4504093acb0b11cfMichael S. Tsirkin	if (ipoib_cm_add_mode_attr(priv->dev))
128839fcaba355abaffb7b44f0f4504093acb0b11cfMichael S. Tsirkin		goto sysfs_failed;
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ipoib_add_pkey_attr(priv->dev))
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto sysfs_failed;
131335a64a5a958002bc238c90de695e120c3c8c120Or Gerlitz	if (ipoib_add_umcast_attr(priv->dev))
132335a64a5a958002bc238c90de695e120c3c8c120Or Gerlitz		goto sysfs_failed;
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13443cb76d91ee85f579a69d42bc8efc08bac560278Greg Kroah-Hartman	if (device_create_file(&priv->dev->dev, &dev_attr_parent))
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto sysfs_failed;
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_add_tail(&priv->list, &ppriv->child_intfs);
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13995ed644fd12f53c6fc778f3f246974e5fe3a9468Ingo Molnar	mutex_unlock(&ppriv->vlan_mutex);
140cbbe1efa4972350286b52cb48aefaa11e198c0fbRoland Dreier	rtnl_unlock();
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldssysfs_failed:
1451732b0ef3b3a02e3df328086fb3018741c5476daRoland Dreier	ipoib_delete_debug_files(priv->dev);
146cbbe1efa4972350286b52cb48aefaa11e198c0fbRoland Dreier	unregister_netdevice(priv->dev);
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsregister_failed:
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ipoib_dev_cleanup(priv->dev);
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldserr:
15295ed644fd12f53c6fc778f3f246974e5fe3a9468Ingo Molnar	mutex_unlock(&ppriv->vlan_mutex);
153cbbe1efa4972350286b52cb48aefaa11e198c0fbRoland Dreier	rtnl_unlock();
154edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier	if (priv)
155edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier		free_netdev(priv->dev);
156edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return result;
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct ipoib_dev_priv *ppriv, *priv, *tpriv;
163edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier	struct net_device *dev = NULL;
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!capable(CAP_NET_ADMIN))
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EPERM;
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ppriv = netdev_priv(pdev);
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17026574401fef6766f6c3ca25b5c13febe662d2a32Eric W. Biederman	if (!rtnl_trylock())
17126574401fef6766f6c3ca25b5c13febe662d2a32Eric W. Biederman		return restart_syscall();
17295ed644fd12f53c6fc778f3f246974e5fe3a9468Ingo Molnar	mutex_lock(&ppriv->vlan_mutex);
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (priv->pkey == pkey) {
175cbbe1efa4972350286b52cb48aefaa11e198c0fbRoland Dreier			unregister_netdevice(priv->dev);
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ipoib_dev_cleanup(priv->dev);
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			list_del(&priv->list);
178edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier			dev = priv->dev;
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
18295ed644fd12f53c6fc778f3f246974e5fe3a9468Ingo Molnar	mutex_unlock(&ppriv->vlan_mutex);
183cbbe1efa4972350286b52cb48aefaa11e198c0fbRoland Dreier	rtnl_unlock();
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
185edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier	if (dev) {
186edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier		free_netdev(dev);
187edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier		return 0;
188edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier	}
189edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier
190edb5abb1e2a84fd8802a3577d95eac84fe1405abRoland Dreier	return -ENODEV;
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
192