18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2012 Mellanox Technologies. -  All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
348c2ecf20Sopenharmony_ci#include <linux/if_arp.h>      /* For ARPHRD_xxx */
358c2ecf20Sopenharmony_ci#include <linux/module.h>
368c2ecf20Sopenharmony_ci#include <net/rtnetlink.h>
378c2ecf20Sopenharmony_ci#include "ipoib.h"
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic const struct nla_policy ipoib_policy[IFLA_IPOIB_MAX + 1] = {
408c2ecf20Sopenharmony_ci	[IFLA_IPOIB_PKEY]	= { .type = NLA_U16 },
418c2ecf20Sopenharmony_ci	[IFLA_IPOIB_MODE]	= { .type = NLA_U16 },
428c2ecf20Sopenharmony_ci	[IFLA_IPOIB_UMCAST]	= { .type = NLA_U16 },
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic unsigned int ipoib_get_max_num_queues(void)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	return min_t(unsigned int, num_possible_cpus(), 128);
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int ipoib_fill_info(struct sk_buff *skb, const struct net_device *dev)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
538c2ecf20Sopenharmony_ci	u16 val;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	if (nla_put_u16(skb, IFLA_IPOIB_PKEY, priv->pkey))
568c2ecf20Sopenharmony_ci		goto nla_put_failure;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	val = test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
598c2ecf20Sopenharmony_ci	if (nla_put_u16(skb, IFLA_IPOIB_MODE, val))
608c2ecf20Sopenharmony_ci		goto nla_put_failure;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	val = test_bit(IPOIB_FLAG_UMCAST, &priv->flags);
638c2ecf20Sopenharmony_ci	if (nla_put_u16(skb, IFLA_IPOIB_UMCAST, val))
648c2ecf20Sopenharmony_ci		goto nla_put_failure;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	return 0;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cinla_put_failure:
698c2ecf20Sopenharmony_ci	return -EMSGSIZE;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic int ipoib_changelink(struct net_device *dev, struct nlattr *tb[],
738c2ecf20Sopenharmony_ci			    struct nlattr *data[],
748c2ecf20Sopenharmony_ci			    struct netlink_ext_ack *extack)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	u16 mode, umcast;
778c2ecf20Sopenharmony_ci	int ret = 0;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (data[IFLA_IPOIB_MODE]) {
808c2ecf20Sopenharmony_ci		mode  = nla_get_u16(data[IFLA_IPOIB_MODE]);
818c2ecf20Sopenharmony_ci		if (mode == IPOIB_MODE_DATAGRAM)
828c2ecf20Sopenharmony_ci			ret = ipoib_set_mode(dev, "datagram\n");
838c2ecf20Sopenharmony_ci		else if (mode == IPOIB_MODE_CONNECTED)
848c2ecf20Sopenharmony_ci			ret = ipoib_set_mode(dev, "connected\n");
858c2ecf20Sopenharmony_ci		else
868c2ecf20Sopenharmony_ci			ret = -EINVAL;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		if (ret < 0)
898c2ecf20Sopenharmony_ci			goto out_err;
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (data[IFLA_IPOIB_UMCAST]) {
938c2ecf20Sopenharmony_ci		umcast = nla_get_u16(data[IFLA_IPOIB_UMCAST]);
948c2ecf20Sopenharmony_ci		ipoib_set_umcast(dev, umcast);
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ciout_err:
988c2ecf20Sopenharmony_ci	return ret;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic int ipoib_new_child_link(struct net *src_net, struct net_device *dev,
1028c2ecf20Sopenharmony_ci				struct nlattr *tb[], struct nlattr *data[],
1038c2ecf20Sopenharmony_ci				struct netlink_ext_ack *extack)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct net_device *pdev;
1068c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *ppriv;
1078c2ecf20Sopenharmony_ci	u16 child_pkey;
1088c2ecf20Sopenharmony_ci	int err;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (!tb[IFLA_LINK])
1118c2ecf20Sopenharmony_ci		return -EINVAL;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	pdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1148c2ecf20Sopenharmony_ci	if (!pdev || pdev->type != ARPHRD_INFINIBAND)
1158c2ecf20Sopenharmony_ci		return -ENODEV;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	ppriv = ipoib_priv(pdev);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (test_bit(IPOIB_FLAG_SUBINTERFACE, &ppriv->flags)) {
1208c2ecf20Sopenharmony_ci		ipoib_warn(ppriv, "child creation disallowed for child devices\n");
1218c2ecf20Sopenharmony_ci		return -EINVAL;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (!data || !data[IFLA_IPOIB_PKEY]) {
1258c2ecf20Sopenharmony_ci		ipoib_dbg(ppriv, "no pkey specified, using parent pkey\n");
1268c2ecf20Sopenharmony_ci		child_pkey  = ppriv->pkey;
1278c2ecf20Sopenharmony_ci	} else
1288c2ecf20Sopenharmony_ci		child_pkey  = nla_get_u16(data[IFLA_IPOIB_PKEY]);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	err = ipoib_intf_init(ppriv->ca, ppriv->port, dev->name, dev);
1318c2ecf20Sopenharmony_ci	if (err) {
1328c2ecf20Sopenharmony_ci		ipoib_warn(ppriv, "failed to initialize pkey device\n");
1338c2ecf20Sopenharmony_ci		return err;
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	err = __ipoib_vlan_add(ppriv, ipoib_priv(dev),
1378c2ecf20Sopenharmony_ci			       child_pkey, IPOIB_RTNL_CHILD);
1388c2ecf20Sopenharmony_ci	if (err)
1398c2ecf20Sopenharmony_ci		return err;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (data) {
1428c2ecf20Sopenharmony_ci		err = ipoib_changelink(dev, tb, data, extack);
1438c2ecf20Sopenharmony_ci		if (err) {
1448c2ecf20Sopenharmony_ci			unregister_netdevice(dev);
1458c2ecf20Sopenharmony_ci			return err;
1468c2ecf20Sopenharmony_ci		}
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return 0;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic void ipoib_del_child_link(struct net_device *dev, struct list_head *head)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (!priv->parent)
1578c2ecf20Sopenharmony_ci		return;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	unregister_netdevice_queue(dev, head);
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic size_t ipoib_get_size(const struct net_device *dev)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	return nla_total_size(2) +	/* IFLA_IPOIB_PKEY   */
1658c2ecf20Sopenharmony_ci		nla_total_size(2) +	/* IFLA_IPOIB_MODE   */
1668c2ecf20Sopenharmony_ci		nla_total_size(2);	/* IFLA_IPOIB_UMCAST */
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic struct rtnl_link_ops ipoib_link_ops __read_mostly = {
1708c2ecf20Sopenharmony_ci	.kind		= "ipoib",
1718c2ecf20Sopenharmony_ci	.netns_refund   = true,
1728c2ecf20Sopenharmony_ci	.maxtype	= IFLA_IPOIB_MAX,
1738c2ecf20Sopenharmony_ci	.policy		= ipoib_policy,
1748c2ecf20Sopenharmony_ci	.priv_size	= sizeof(struct ipoib_dev_priv),
1758c2ecf20Sopenharmony_ci	.setup		= ipoib_setup_common,
1768c2ecf20Sopenharmony_ci	.newlink	= ipoib_new_child_link,
1778c2ecf20Sopenharmony_ci	.dellink	= ipoib_del_child_link,
1788c2ecf20Sopenharmony_ci	.changelink	= ipoib_changelink,
1798c2ecf20Sopenharmony_ci	.get_size	= ipoib_get_size,
1808c2ecf20Sopenharmony_ci	.fill_info	= ipoib_fill_info,
1818c2ecf20Sopenharmony_ci	.get_num_rx_queues = ipoib_get_max_num_queues,
1828c2ecf20Sopenharmony_ci	.get_num_tx_queues = ipoib_get_max_num_queues,
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistruct rtnl_link_ops *ipoib_get_link_ops(void)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	return &ipoib_link_ops;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ciint __init ipoib_netlink_init(void)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	return rtnl_link_register(&ipoib_link_ops);
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_civoid __exit ipoib_netlink_fini(void)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	rtnl_link_unregister(&ipoib_link_ops);
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ciMODULE_ALIAS_RTNL_LINK("ipoib");
201