xref: /openbmc/linux/net/dcb/dcbnl.c (revision bb1dfefd)
12f90b865SAlexander Duyck /*
2698e1d23SMark Rustad  * Copyright (c) 2008-2011, Intel Corporation.
32f90b865SAlexander Duyck  *
42f90b865SAlexander Duyck  * This program is free software; you can redistribute it and/or modify it
52f90b865SAlexander Duyck  * under the terms and conditions of the GNU General Public License,
62f90b865SAlexander Duyck  * version 2, as published by the Free Software Foundation.
72f90b865SAlexander Duyck  *
82f90b865SAlexander Duyck  * This program is distributed in the hope it will be useful, but WITHOUT
92f90b865SAlexander Duyck  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
102f90b865SAlexander Duyck  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
112f90b865SAlexander Duyck  * more details.
122f90b865SAlexander Duyck  *
132f90b865SAlexander Duyck  * You should have received a copy of the GNU General Public License along with
142f90b865SAlexander Duyck  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
152f90b865SAlexander Duyck  * Place - Suite 330, Boston, MA 02111-1307 USA.
162f90b865SAlexander Duyck  *
172f90b865SAlexander Duyck  * Author: Lucy Liu <lucy.liu@intel.com>
182f90b865SAlexander Duyck  */
192f90b865SAlexander Duyck 
202f90b865SAlexander Duyck #include <linux/netdevice.h>
212f90b865SAlexander Duyck #include <linux/netlink.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
232f90b865SAlexander Duyck #include <net/netlink.h>
242f90b865SAlexander Duyck #include <net/rtnetlink.h>
252f90b865SAlexander Duyck #include <linux/dcbnl.h>
2696b99684SJohn Fastabend #include <net/dcbevent.h>
272f90b865SAlexander Duyck #include <linux/rtnetlink.h>
283a9a231dSPaul Gortmaker #include <linux/module.h>
292f90b865SAlexander Duyck #include <net/sock.h>
302f90b865SAlexander Duyck 
312f90b865SAlexander Duyck /**
322f90b865SAlexander Duyck  * Data Center Bridging (DCB) is a collection of Ethernet enhancements
332f90b865SAlexander Duyck  * intended to allow network traffic with differing requirements
342f90b865SAlexander Duyck  * (highly reliable, no drops vs. best effort vs. low latency) to operate
352f90b865SAlexander Duyck  * and co-exist on Ethernet.  Current DCB features are:
362f90b865SAlexander Duyck  *
372f90b865SAlexander Duyck  * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
382f90b865SAlexander Duyck  *   framework for assigning bandwidth guarantees to traffic classes.
392f90b865SAlexander Duyck  *
402f90b865SAlexander Duyck  * Priority-based Flow Control (PFC) - provides a flow control mechanism which
412f90b865SAlexander Duyck  *   can work independently for each 802.1p priority.
422f90b865SAlexander Duyck  *
432f90b865SAlexander Duyck  * Congestion Notification - provides a mechanism for end-to-end congestion
442f90b865SAlexander Duyck  *   control for protocols which do not have built-in congestion management.
452f90b865SAlexander Duyck  *
462f90b865SAlexander Duyck  * More information about the emerging standards for these Ethernet features
472f90b865SAlexander Duyck  * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
482f90b865SAlexander Duyck  *
492f90b865SAlexander Duyck  * This file implements an rtnetlink interface to allow configuration of DCB
502f90b865SAlexander Duyck  * features for capable devices.
512f90b865SAlexander Duyck  */
522f90b865SAlexander Duyck 
532f90b865SAlexander Duyck MODULE_AUTHOR("Lucy Liu, <lucy.liu@intel.com>");
547a6b6f51SJeff Kirsher MODULE_DESCRIPTION("Data Center Bridging netlink interface");
552f90b865SAlexander Duyck MODULE_LICENSE("GPL");
562f90b865SAlexander Duyck 
572f90b865SAlexander Duyck /**************** DCB attribute policies *************************************/
582f90b865SAlexander Duyck 
592f90b865SAlexander Duyck /* DCB netlink attributes policy */
60b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
61859ee3c4SAlexander Duyck 	[DCB_ATTR_IFNAME]      = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
622f90b865SAlexander Duyck 	[DCB_ATTR_STATE]       = {.type = NLA_U8},
632f90b865SAlexander Duyck 	[DCB_ATTR_PFC_CFG]     = {.type = NLA_NESTED},
642f90b865SAlexander Duyck 	[DCB_ATTR_PG_CFG]      = {.type = NLA_NESTED},
652f90b865SAlexander Duyck 	[DCB_ATTR_SET_ALL]     = {.type = NLA_U8},
662f90b865SAlexander Duyck 	[DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
6746132188SAlexander Duyck 	[DCB_ATTR_CAP]         = {.type = NLA_NESTED},
680eb3aa9bSAlexander Duyck 	[DCB_ATTR_PFC_STATE]   = {.type = NLA_U8},
69859ee3c4SAlexander Duyck 	[DCB_ATTR_BCN]         = {.type = NLA_NESTED},
706fa382afSYi Zou 	[DCB_ATTR_APP]         = {.type = NLA_NESTED},
713e29027aSJohn Fastabend 	[DCB_ATTR_IEEE]	       = {.type = NLA_NESTED},
726241b625SShmulik Ravid 	[DCB_ATTR_DCBX]        = {.type = NLA_U8},
73ea45fe4eSShmulik Ravid 	[DCB_ATTR_FEATCFG]     = {.type = NLA_NESTED},
742f90b865SAlexander Duyck };
752f90b865SAlexander Duyck 
762f90b865SAlexander Duyck /* DCB priority flow control to User Priority nested attributes */
77b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
782f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_0]   = {.type = NLA_U8},
792f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_1]   = {.type = NLA_U8},
802f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_2]   = {.type = NLA_U8},
812f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_3]   = {.type = NLA_U8},
822f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_4]   = {.type = NLA_U8},
832f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_5]   = {.type = NLA_U8},
842f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_6]   = {.type = NLA_U8},
852f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_7]   = {.type = NLA_U8},
862f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
872f90b865SAlexander Duyck };
882f90b865SAlexander Duyck 
892f90b865SAlexander Duyck /* DCB priority grouping nested attributes */
90b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
912f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_0]      = {.type = NLA_NESTED},
922f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_1]      = {.type = NLA_NESTED},
932f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_2]      = {.type = NLA_NESTED},
942f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_3]      = {.type = NLA_NESTED},
952f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_4]      = {.type = NLA_NESTED},
962f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_5]      = {.type = NLA_NESTED},
972f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_6]      = {.type = NLA_NESTED},
982f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_7]      = {.type = NLA_NESTED},
992f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_ALL]    = {.type = NLA_NESTED},
1002f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_0]   = {.type = NLA_U8},
1012f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_1]   = {.type = NLA_U8},
1022f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_2]   = {.type = NLA_U8},
1032f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_3]   = {.type = NLA_U8},
1042f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_4]   = {.type = NLA_U8},
1052f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_5]   = {.type = NLA_U8},
1062f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_6]   = {.type = NLA_U8},
1072f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_7]   = {.type = NLA_U8},
1082f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
1092f90b865SAlexander Duyck };
1102f90b865SAlexander Duyck 
1112f90b865SAlexander Duyck /* DCB traffic class nested attributes. */
112b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
1132f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_PGID]            = {.type = NLA_U8},
1142f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_UP_MAPPING]      = {.type = NLA_U8},
1152f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_STRICT_PRIO]     = {.type = NLA_U8},
1162f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_BW_PCT]          = {.type = NLA_U8},
1172f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_ALL]             = {.type = NLA_FLAG},
1182f90b865SAlexander Duyck };
1192f90b865SAlexander Duyck 
12046132188SAlexander Duyck /* DCB capabilities nested attributes. */
121b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
12246132188SAlexander Duyck 	[DCB_CAP_ATTR_ALL]     = {.type = NLA_FLAG},
12346132188SAlexander Duyck 	[DCB_CAP_ATTR_PG]      = {.type = NLA_U8},
12446132188SAlexander Duyck 	[DCB_CAP_ATTR_PFC]     = {.type = NLA_U8},
12546132188SAlexander Duyck 	[DCB_CAP_ATTR_UP2TC]   = {.type = NLA_U8},
12646132188SAlexander Duyck 	[DCB_CAP_ATTR_PG_TCS]  = {.type = NLA_U8},
12746132188SAlexander Duyck 	[DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
12846132188SAlexander Duyck 	[DCB_CAP_ATTR_GSP]     = {.type = NLA_U8},
12946132188SAlexander Duyck 	[DCB_CAP_ATTR_BCN]     = {.type = NLA_U8},
1306241b625SShmulik Ravid 	[DCB_CAP_ATTR_DCBX]    = {.type = NLA_U8},
13146132188SAlexander Duyck };
1322f90b865SAlexander Duyck 
13333dbabc4SAlexander Duyck /* DCB capabilities nested attributes. */
134b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
13533dbabc4SAlexander Duyck 	[DCB_NUMTCS_ATTR_ALL]     = {.type = NLA_FLAG},
13633dbabc4SAlexander Duyck 	[DCB_NUMTCS_ATTR_PG]      = {.type = NLA_U8},
13733dbabc4SAlexander Duyck 	[DCB_NUMTCS_ATTR_PFC]     = {.type = NLA_U8},
13833dbabc4SAlexander Duyck };
13933dbabc4SAlexander Duyck 
140859ee3c4SAlexander Duyck /* DCB BCN nested attributes. */
141b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
142859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_0]         = {.type = NLA_U8},
143859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_1]         = {.type = NLA_U8},
144859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_2]         = {.type = NLA_U8},
145859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_3]         = {.type = NLA_U8},
146859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_4]         = {.type = NLA_U8},
147859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_5]         = {.type = NLA_U8},
148859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_6]         = {.type = NLA_U8},
149859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_7]         = {.type = NLA_U8},
150859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_ALL]       = {.type = NLA_FLAG},
151f4314e81SDon Skidmore 	[DCB_BCN_ATTR_BCNA_0]       = {.type = NLA_U32},
152f4314e81SDon Skidmore 	[DCB_BCN_ATTR_BCNA_1]       = {.type = NLA_U32},
153859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_ALPHA]        = {.type = NLA_U32},
154859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_BETA]         = {.type = NLA_U32},
155859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_GD]           = {.type = NLA_U32},
156859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_GI]           = {.type = NLA_U32},
157859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_TMAX]         = {.type = NLA_U32},
158859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_TD]           = {.type = NLA_U32},
159859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RMIN]         = {.type = NLA_U32},
160859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_W]            = {.type = NLA_U32},
161859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RD]           = {.type = NLA_U32},
162859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RU]           = {.type = NLA_U32},
163859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_WRTT]         = {.type = NLA_U32},
164859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RI]           = {.type = NLA_U32},
165859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_C]            = {.type = NLA_U32},
166859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_ALL]          = {.type = NLA_FLAG},
167859ee3c4SAlexander Duyck };
168859ee3c4SAlexander Duyck 
1696fa382afSYi Zou /* DCB APP nested attributes. */
170b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
1716fa382afSYi Zou 	[DCB_APP_ATTR_IDTYPE]       = {.type = NLA_U8},
1726fa382afSYi Zou 	[DCB_APP_ATTR_ID]           = {.type = NLA_U16},
1736fa382afSYi Zou 	[DCB_APP_ATTR_PRIORITY]     = {.type = NLA_U8},
1746fa382afSYi Zou };
1756fa382afSYi Zou 
1763e29027aSJohn Fastabend /* IEEE 802.1Qaz nested attributes. */
1773e29027aSJohn Fastabend static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
1783e29027aSJohn Fastabend 	[DCB_ATTR_IEEE_ETS]	    = {.len = sizeof(struct ieee_ets)},
1793e29027aSJohn Fastabend 	[DCB_ATTR_IEEE_PFC]	    = {.len = sizeof(struct ieee_pfc)},
1803e29027aSJohn Fastabend 	[DCB_ATTR_IEEE_APP_TABLE]   = {.type = NLA_NESTED},
18108f10affSAmir Vadai 	[DCB_ATTR_IEEE_MAXRATE]   = {.len = sizeof(struct ieee_maxrate)},
1823e29027aSJohn Fastabend };
1833e29027aSJohn Fastabend 
1843e29027aSJohn Fastabend static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = {
1853e29027aSJohn Fastabend 	[DCB_ATTR_IEEE_APP]	    = {.len = sizeof(struct dcb_app)},
1863e29027aSJohn Fastabend };
1873e29027aSJohn Fastabend 
188ea45fe4eSShmulik Ravid /* DCB number of traffic classes nested attributes. */
189ea45fe4eSShmulik Ravid static const struct nla_policy dcbnl_featcfg_nest[DCB_FEATCFG_ATTR_MAX + 1] = {
190ea45fe4eSShmulik Ravid 	[DCB_FEATCFG_ATTR_ALL]      = {.type = NLA_FLAG},
191ea45fe4eSShmulik Ravid 	[DCB_FEATCFG_ATTR_PG]       = {.type = NLA_U8},
192ea45fe4eSShmulik Ravid 	[DCB_FEATCFG_ATTR_PFC]      = {.type = NLA_U8},
193ea45fe4eSShmulik Ravid 	[DCB_FEATCFG_ATTR_APP]      = {.type = NLA_U8},
194ea45fe4eSShmulik Ravid };
195ea45fe4eSShmulik Ravid 
1969ab933abSJohn Fastabend static LIST_HEAD(dcb_app_list);
1979ab933abSJohn Fastabend static DEFINE_SPINLOCK(dcb_lock);
1989ab933abSJohn Fastabend 
19933a03aadSThomas Graf static struct sk_buff *dcbnl_newmsg(int type, u8 cmd, u32 port, u32 seq,
20033a03aadSThomas Graf 				    u32 flags, struct nlmsghdr **nlhp)
20133a03aadSThomas Graf {
20233a03aadSThomas Graf 	struct sk_buff *skb;
20333a03aadSThomas Graf 	struct dcbmsg *dcb;
20433a03aadSThomas Graf 	struct nlmsghdr *nlh;
20533a03aadSThomas Graf 
20633a03aadSThomas Graf 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
20733a03aadSThomas Graf 	if (!skb)
20833a03aadSThomas Graf 		return NULL;
20933a03aadSThomas Graf 
21033a03aadSThomas Graf 	nlh = nlmsg_put(skb, port, seq, type, sizeof(*dcb), flags);
211b3908e22SThomas Graf 	BUG_ON(!nlh);
21233a03aadSThomas Graf 
21333a03aadSThomas Graf 	dcb = nlmsg_data(nlh);
21433a03aadSThomas Graf 	dcb->dcb_family = AF_UNSPEC;
21533a03aadSThomas Graf 	dcb->cmd = cmd;
21633a03aadSThomas Graf 	dcb->dcb_pad = 0;
21733a03aadSThomas Graf 
21833a03aadSThomas Graf 	if (nlhp)
21933a03aadSThomas Graf 		*nlhp = nlh;
22033a03aadSThomas Graf 
22133a03aadSThomas Graf 	return skb;
22233a03aadSThomas Graf }
22333a03aadSThomas Graf 
2247be99413SThomas Graf static int dcbnl_getstate(struct net_device *netdev, struct nlmsghdr *nlh,
2257be99413SThomas Graf 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
2262f90b865SAlexander Duyck {
2272f90b865SAlexander Duyck 	/* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
2282f90b865SAlexander Duyck 	if (!netdev->dcbnl_ops->getstate)
2293d1f4869SThomas Graf 		return -EOPNOTSUPP;
2302f90b865SAlexander Duyck 
2317be99413SThomas Graf 	return nla_put_u8(skb, DCB_ATTR_STATE,
2327be99413SThomas Graf 			  netdev->dcbnl_ops->getstate(netdev));
2332f90b865SAlexander Duyck }
2342f90b865SAlexander Duyck 
2357be99413SThomas Graf static int dcbnl_getpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
2367be99413SThomas Graf 			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
2372f90b865SAlexander Duyck {
2382f90b865SAlexander Duyck 	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
2392f90b865SAlexander Duyck 	u8 value;
2403d1f4869SThomas Graf 	int ret;
2412f90b865SAlexander Duyck 	int i;
2422f90b865SAlexander Duyck 	int getall = 0;
2432f90b865SAlexander Duyck 
2443d1f4869SThomas Graf 	if (!tb[DCB_ATTR_PFC_CFG])
2453d1f4869SThomas Graf 		return -EINVAL;
2463d1f4869SThomas Graf 
2473d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->getpfccfg)
2483d1f4869SThomas Graf 		return -EOPNOTSUPP;
2492f90b865SAlexander Duyck 
2502f90b865SAlexander Duyck 	ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
2512f90b865SAlexander Duyck 	                       tb[DCB_ATTR_PFC_CFG],
2522f90b865SAlexander Duyck 	                       dcbnl_pfc_up_nest);
2532f90b865SAlexander Duyck 	if (ret)
2543d1f4869SThomas Graf 		return ret;
2552f90b865SAlexander Duyck 
2567be99413SThomas Graf 	nest = nla_nest_start(skb, DCB_ATTR_PFC_CFG);
2572f90b865SAlexander Duyck 	if (!nest)
2583d1f4869SThomas Graf 		return -EMSGSIZE;
2592f90b865SAlexander Duyck 
2602f90b865SAlexander Duyck 	if (data[DCB_PFC_UP_ATTR_ALL])
2612f90b865SAlexander Duyck 		getall = 1;
2622f90b865SAlexander Duyck 
2632f90b865SAlexander Duyck 	for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
2642f90b865SAlexander Duyck 		if (!getall && !data[i])
2652f90b865SAlexander Duyck 			continue;
2662f90b865SAlexander Duyck 
2672f90b865SAlexander Duyck 		netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
2682f90b865SAlexander Duyck 		                             &value);
2697be99413SThomas Graf 		ret = nla_put_u8(skb, i, value);
2702f90b865SAlexander Duyck 		if (ret) {
2717be99413SThomas Graf 			nla_nest_cancel(skb, nest);
2723d1f4869SThomas Graf 			return ret;
2732f90b865SAlexander Duyck 		}
2742f90b865SAlexander Duyck 	}
2757be99413SThomas Graf 	nla_nest_end(skb, nest);
2762f90b865SAlexander Duyck 
2772f90b865SAlexander Duyck 	return 0;
2782f90b865SAlexander Duyck }
2792f90b865SAlexander Duyck 
2807be99413SThomas Graf static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlmsghdr *nlh,
2817be99413SThomas Graf 				u32 seq, struct nlattr **tb, struct sk_buff *skb)
2822f90b865SAlexander Duyck {
2832f90b865SAlexander Duyck 	u8 perm_addr[MAX_ADDR_LEN];
2842f90b865SAlexander Duyck 
2852f90b865SAlexander Duyck 	if (!netdev->dcbnl_ops->getpermhwaddr)
2863d1f4869SThomas Graf 		return -EOPNOTSUPP;
2872f90b865SAlexander Duyck 
2882f90b865SAlexander Duyck 	netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
2892f90b865SAlexander Duyck 
2907be99413SThomas Graf 	return nla_put(skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr), perm_addr);
2912f90b865SAlexander Duyck }
2922f90b865SAlexander Duyck 
2937be99413SThomas Graf static int dcbnl_getcap(struct net_device *netdev, struct nlmsghdr *nlh,
2947be99413SThomas Graf 			u32 seq, struct nlattr **tb, struct sk_buff *skb)
29546132188SAlexander Duyck {
29646132188SAlexander Duyck 	struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
29746132188SAlexander Duyck 	u8 value;
2983d1f4869SThomas Graf 	int ret;
29946132188SAlexander Duyck 	int i;
30046132188SAlexander Duyck 	int getall = 0;
30146132188SAlexander Duyck 
3023d1f4869SThomas Graf 	if (!tb[DCB_ATTR_CAP])
3033d1f4869SThomas Graf 		return -EINVAL;
3043d1f4869SThomas Graf 
3053d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->getcap)
3063d1f4869SThomas Graf 		return -EOPNOTSUPP;
30746132188SAlexander Duyck 
30846132188SAlexander Duyck 	ret = nla_parse_nested(data, DCB_CAP_ATTR_MAX, tb[DCB_ATTR_CAP],
30946132188SAlexander Duyck 	                       dcbnl_cap_nest);
31046132188SAlexander Duyck 	if (ret)
3113d1f4869SThomas Graf 		return ret;
31246132188SAlexander Duyck 
3137be99413SThomas Graf 	nest = nla_nest_start(skb, DCB_ATTR_CAP);
31446132188SAlexander Duyck 	if (!nest)
3153d1f4869SThomas Graf 		return -EMSGSIZE;
31646132188SAlexander Duyck 
31746132188SAlexander Duyck 	if (data[DCB_CAP_ATTR_ALL])
31846132188SAlexander Duyck 		getall = 1;
31946132188SAlexander Duyck 
32046132188SAlexander Duyck 	for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
32146132188SAlexander Duyck 		if (!getall && !data[i])
32246132188SAlexander Duyck 			continue;
32346132188SAlexander Duyck 
32446132188SAlexander Duyck 		if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
3257be99413SThomas Graf 			ret = nla_put_u8(skb, i, value);
32646132188SAlexander Duyck 			if (ret) {
3277be99413SThomas Graf 				nla_nest_cancel(skb, nest);
3283d1f4869SThomas Graf 				return ret;
3297be99413SThomas Graf 			}
3307be99413SThomas Graf 		}
3317be99413SThomas Graf 	}
3327be99413SThomas Graf 	nla_nest_end(skb, nest);
33346132188SAlexander Duyck 
33446132188SAlexander Duyck 	return 0;
33546132188SAlexander Duyck }
33646132188SAlexander Duyck 
3377be99413SThomas Graf static int dcbnl_getnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
3387be99413SThomas Graf 			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
33933dbabc4SAlexander Duyck {
34033dbabc4SAlexander Duyck 	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
34133dbabc4SAlexander Duyck 	u8 value;
3423d1f4869SThomas Graf 	int ret;
34333dbabc4SAlexander Duyck 	int i;
34433dbabc4SAlexander Duyck 	int getall = 0;
34533dbabc4SAlexander Duyck 
3463d1f4869SThomas Graf 	if (!tb[DCB_ATTR_NUMTCS])
3473d1f4869SThomas Graf 		return -EINVAL;
3483d1f4869SThomas Graf 
3493d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->getnumtcs)
3503d1f4869SThomas Graf 		return -EOPNOTSUPP;
35133dbabc4SAlexander Duyck 
35233dbabc4SAlexander Duyck 	ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
35333dbabc4SAlexander Duyck 	                       dcbnl_numtcs_nest);
3543d1f4869SThomas Graf 	if (ret)
3553d1f4869SThomas Graf 		return ret;
35633dbabc4SAlexander Duyck 
3577be99413SThomas Graf 	nest = nla_nest_start(skb, DCB_ATTR_NUMTCS);
3583d1f4869SThomas Graf 	if (!nest)
3593d1f4869SThomas Graf 		return -EMSGSIZE;
36033dbabc4SAlexander Duyck 
36133dbabc4SAlexander Duyck 	if (data[DCB_NUMTCS_ATTR_ALL])
36233dbabc4SAlexander Duyck 		getall = 1;
36333dbabc4SAlexander Duyck 
36433dbabc4SAlexander Duyck 	for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
36533dbabc4SAlexander Duyck 		if (!getall && !data[i])
36633dbabc4SAlexander Duyck 			continue;
36733dbabc4SAlexander Duyck 
36833dbabc4SAlexander Duyck 		ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
36933dbabc4SAlexander Duyck 		if (!ret) {
3707be99413SThomas Graf 			ret = nla_put_u8(skb, i, value);
37133dbabc4SAlexander Duyck 			if (ret) {
3727be99413SThomas Graf 				nla_nest_cancel(skb, nest);
3733d1f4869SThomas Graf 				return ret;
37433dbabc4SAlexander Duyck 			}
3753d1f4869SThomas Graf 		} else
3763d1f4869SThomas Graf 			return -EINVAL;
3777be99413SThomas Graf 	}
3787be99413SThomas Graf 	nla_nest_end(skb, nest);
37933dbabc4SAlexander Duyck 
38033dbabc4SAlexander Duyck 	return 0;
38133dbabc4SAlexander Duyck }
38233dbabc4SAlexander Duyck 
3837be99413SThomas Graf static int dcbnl_setnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
3847be99413SThomas Graf 			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
38533dbabc4SAlexander Duyck {
38633dbabc4SAlexander Duyck 	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
3873d1f4869SThomas Graf 	int ret;
38833dbabc4SAlexander Duyck 	u8 value;
38933dbabc4SAlexander Duyck 	int i;
39033dbabc4SAlexander Duyck 
3913d1f4869SThomas Graf 	if (!tb[DCB_ATTR_NUMTCS])
3923d1f4869SThomas Graf 		return -EINVAL;
3933d1f4869SThomas Graf 
3943d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->setnumtcs)
3953d1f4869SThomas Graf 		return -EOPNOTSUPP;
39633dbabc4SAlexander Duyck 
39733dbabc4SAlexander Duyck 	ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
39833dbabc4SAlexander Duyck 	                       dcbnl_numtcs_nest);
3997be99413SThomas Graf 	if (ret)
4003d1f4869SThomas Graf 		return ret;
40133dbabc4SAlexander Duyck 
40233dbabc4SAlexander Duyck 	for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
40333dbabc4SAlexander Duyck 		if (data[i] == NULL)
40433dbabc4SAlexander Duyck 			continue;
40533dbabc4SAlexander Duyck 
40633dbabc4SAlexander Duyck 		value = nla_get_u8(data[i]);
40733dbabc4SAlexander Duyck 
40833dbabc4SAlexander Duyck 		ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
40933dbabc4SAlexander Duyck 		if (ret)
4107be99413SThomas Graf 			break;
41133dbabc4SAlexander Duyck 	}
41233dbabc4SAlexander Duyck 
4137be99413SThomas Graf 	return nla_put_u8(skb, DCB_ATTR_NUMTCS, !!ret);
41433dbabc4SAlexander Duyck }
41533dbabc4SAlexander Duyck 
4167be99413SThomas Graf static int dcbnl_getpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
4177be99413SThomas Graf 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
4180eb3aa9bSAlexander Duyck {
4190eb3aa9bSAlexander Duyck 	if (!netdev->dcbnl_ops->getpfcstate)
4203d1f4869SThomas Graf 		return -EOPNOTSUPP;
4210eb3aa9bSAlexander Duyck 
4227be99413SThomas Graf 	return nla_put_u8(skb, DCB_ATTR_PFC_STATE,
4237be99413SThomas Graf 			  netdev->dcbnl_ops->getpfcstate(netdev));
4240eb3aa9bSAlexander Duyck }
4250eb3aa9bSAlexander Duyck 
4267be99413SThomas Graf static int dcbnl_setpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
4277be99413SThomas Graf 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
4280eb3aa9bSAlexander Duyck {
4290eb3aa9bSAlexander Duyck 	u8 value;
4300eb3aa9bSAlexander Duyck 
4313d1f4869SThomas Graf 	if (!tb[DCB_ATTR_PFC_STATE])
4327be99413SThomas Graf 		return -EINVAL;
4330eb3aa9bSAlexander Duyck 
4343d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->setpfcstate)
4353d1f4869SThomas Graf 		return -EOPNOTSUPP;
4363d1f4869SThomas Graf 
4370eb3aa9bSAlexander Duyck 	value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
4380eb3aa9bSAlexander Duyck 
4390eb3aa9bSAlexander Duyck 	netdev->dcbnl_ops->setpfcstate(netdev, value);
4400eb3aa9bSAlexander Duyck 
4417be99413SThomas Graf 	return nla_put_u8(skb, DCB_ATTR_PFC_STATE, 0);
4420eb3aa9bSAlexander Duyck }
4430eb3aa9bSAlexander Duyck 
4447be99413SThomas Graf static int dcbnl_getapp(struct net_device *netdev, struct nlmsghdr *nlh,
4457be99413SThomas Graf 			u32 seq, struct nlattr **tb, struct sk_buff *skb)
44657949686SYi Zou {
44757949686SYi Zou 	struct nlattr *app_nest;
44857949686SYi Zou 	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
44957949686SYi Zou 	u16 id;
45057949686SYi Zou 	u8 up, idtype;
4513d1f4869SThomas Graf 	int ret;
45257949686SYi Zou 
4533dce38a0SJohn Fastabend 	if (!tb[DCB_ATTR_APP])
4543d1f4869SThomas Graf 		return -EINVAL;
45557949686SYi Zou 
45657949686SYi Zou 	ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
45757949686SYi Zou 	                       dcbnl_app_nest);
45857949686SYi Zou 	if (ret)
4593d1f4869SThomas Graf 		return ret;
46057949686SYi Zou 
46157949686SYi Zou 	/* all must be non-null */
46257949686SYi Zou 	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
46357949686SYi Zou 	    (!app_tb[DCB_APP_ATTR_ID]))
4643d1f4869SThomas Graf 		return -EINVAL;
46557949686SYi Zou 
46657949686SYi Zou 	/* either by eth type or by socket number */
46757949686SYi Zou 	idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
46857949686SYi Zou 	if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
46957949686SYi Zou 	    (idtype != DCB_APP_IDTYPE_PORTNUM))
4703d1f4869SThomas Graf 		return -EINVAL;
47157949686SYi Zou 
47257949686SYi Zou 	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
4733dce38a0SJohn Fastabend 
4743dce38a0SJohn Fastabend 	if (netdev->dcbnl_ops->getapp) {
47557949686SYi Zou 		up = netdev->dcbnl_ops->getapp(netdev, idtype, id);
4763dce38a0SJohn Fastabend 	} else {
4773dce38a0SJohn Fastabend 		struct dcb_app app = {
4783dce38a0SJohn Fastabend 					.selector = idtype,
4793dce38a0SJohn Fastabend 					.protocol = id,
4803dce38a0SJohn Fastabend 				     };
4813dce38a0SJohn Fastabend 		up = dcb_getapp(netdev, &app);
4823dce38a0SJohn Fastabend 	}
48357949686SYi Zou 
4847be99413SThomas Graf 	app_nest = nla_nest_start(skb, DCB_ATTR_APP);
4857be99413SThomas Graf 	if (!app_nest)
4863d1f4869SThomas Graf 		return -EMSGSIZE;
48757949686SYi Zou 
4887be99413SThomas Graf 	ret = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE, idtype);
48957949686SYi Zou 	if (ret)
49057949686SYi Zou 		goto out_cancel;
49157949686SYi Zou 
4927be99413SThomas Graf 	ret = nla_put_u16(skb, DCB_APP_ATTR_ID, id);
49357949686SYi Zou 	if (ret)
49457949686SYi Zou 		goto out_cancel;
49557949686SYi Zou 
4967be99413SThomas Graf 	ret = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY, up);
49757949686SYi Zou 	if (ret)
49857949686SYi Zou 		goto out_cancel;
49957949686SYi Zou 
5007be99413SThomas Graf 	nla_nest_end(skb, app_nest);
50157949686SYi Zou 
5023d1f4869SThomas Graf 	return 0;
50357949686SYi Zou 
50457949686SYi Zou out_cancel:
5057be99413SThomas Graf 	nla_nest_cancel(skb, app_nest);
50657949686SYi Zou 	return ret;
50757949686SYi Zou }
50857949686SYi Zou 
5097be99413SThomas Graf static int dcbnl_setapp(struct net_device *netdev, struct nlmsghdr *nlh,
5107be99413SThomas Graf 			u32 seq, struct nlattr **tb, struct sk_buff *skb)
51157949686SYi Zou {
5123d1f4869SThomas Graf 	int ret;
51357949686SYi Zou 	u16 id;
51457949686SYi Zou 	u8 up, idtype;
51557949686SYi Zou 	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
51657949686SYi Zou 
5179ab933abSJohn Fastabend 	if (!tb[DCB_ATTR_APP])
5183d1f4869SThomas Graf 		return -EINVAL;
51957949686SYi Zou 
52057949686SYi Zou 	ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
52157949686SYi Zou 	                       dcbnl_app_nest);
52257949686SYi Zou 	if (ret)
5233d1f4869SThomas Graf 		return ret;
52457949686SYi Zou 
52557949686SYi Zou 	/* all must be non-null */
52657949686SYi Zou 	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
52757949686SYi Zou 	    (!app_tb[DCB_APP_ATTR_ID]) ||
52857949686SYi Zou 	    (!app_tb[DCB_APP_ATTR_PRIORITY]))
5293d1f4869SThomas Graf 		return -EINVAL;
53057949686SYi Zou 
53157949686SYi Zou 	/* either by eth type or by socket number */
53257949686SYi Zou 	idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
53357949686SYi Zou 	if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
53457949686SYi Zou 	    (idtype != DCB_APP_IDTYPE_PORTNUM))
5353d1f4869SThomas Graf 		return -EINVAL;
53657949686SYi Zou 
53757949686SYi Zou 	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
53857949686SYi Zou 	up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
53957949686SYi Zou 
5409ab933abSJohn Fastabend 	if (netdev->dcbnl_ops->setapp) {
5413d1f4869SThomas Graf 		ret = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
5429ab933abSJohn Fastabend 	} else {
5439ab933abSJohn Fastabend 		struct dcb_app app;
5449ab933abSJohn Fastabend 		app.selector = idtype;
5459ab933abSJohn Fastabend 		app.protocol = id;
5469ab933abSJohn Fastabend 		app.priority = up;
5473d1f4869SThomas Graf 		ret = dcb_setapp(netdev, &app);
5489ab933abSJohn Fastabend 	}
5499ab933abSJohn Fastabend 
5507be99413SThomas Graf 	ret = nla_put_u8(skb, DCB_ATTR_APP, ret);
55108157984SJohn Fastabend 	dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SAPP, seq, 0);
5523d1f4869SThomas Graf 
55357949686SYi Zou 	return ret;
55457949686SYi Zou }
55557949686SYi Zou 
5567be99413SThomas Graf static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
5577be99413SThomas Graf 			     struct nlattr **tb, struct sk_buff *skb, int dir)
5582f90b865SAlexander Duyck {
5592f90b865SAlexander Duyck 	struct nlattr *pg_nest, *param_nest, *data;
5602f90b865SAlexander Duyck 	struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
5612f90b865SAlexander Duyck 	struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
5622f90b865SAlexander Duyck 	u8 prio, pgid, tc_pct, up_map;
5633d1f4869SThomas Graf 	int ret;
5642f90b865SAlexander Duyck 	int getall = 0;
5652f90b865SAlexander Duyck 	int i;
5662f90b865SAlexander Duyck 
5673d1f4869SThomas Graf 	if (!tb[DCB_ATTR_PG_CFG])
5683d1f4869SThomas Graf 		return -EINVAL;
5693d1f4869SThomas Graf 
5703d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->getpgtccfgtx ||
5712f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->getpgtccfgrx ||
5722f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->getpgbwgcfgtx ||
5732f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->getpgbwgcfgrx)
5743d1f4869SThomas Graf 		return -EOPNOTSUPP;
5752f90b865SAlexander Duyck 
5762f90b865SAlexander Duyck 	ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
5772f90b865SAlexander Duyck 	                       tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
5782f90b865SAlexander Duyck 	if (ret)
5793d1f4869SThomas Graf 		return ret;
5802f90b865SAlexander Duyck 
5817be99413SThomas Graf 	pg_nest = nla_nest_start(skb, DCB_ATTR_PG_CFG);
5822f90b865SAlexander Duyck 	if (!pg_nest)
5833d1f4869SThomas Graf 		return -EMSGSIZE;
5842f90b865SAlexander Duyck 
5852f90b865SAlexander Duyck 	if (pg_tb[DCB_PG_ATTR_TC_ALL])
5862f90b865SAlexander Duyck 		getall = 1;
5872f90b865SAlexander Duyck 
5882f90b865SAlexander Duyck 	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
5892f90b865SAlexander Duyck 		if (!getall && !pg_tb[i])
5902f90b865SAlexander Duyck 			continue;
5912f90b865SAlexander Duyck 
5922f90b865SAlexander Duyck 		if (pg_tb[DCB_PG_ATTR_TC_ALL])
5932f90b865SAlexander Duyck 			data = pg_tb[DCB_PG_ATTR_TC_ALL];
5942f90b865SAlexander Duyck 		else
5952f90b865SAlexander Duyck 			data = pg_tb[i];
5962f90b865SAlexander Duyck 		ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
5972f90b865SAlexander Duyck 				       data, dcbnl_tc_param_nest);
5982f90b865SAlexander Duyck 		if (ret)
5992f90b865SAlexander Duyck 			goto err_pg;
6002f90b865SAlexander Duyck 
6017be99413SThomas Graf 		param_nest = nla_nest_start(skb, i);
6022f90b865SAlexander Duyck 		if (!param_nest)
6032f90b865SAlexander Duyck 			goto err_pg;
6042f90b865SAlexander Duyck 
6052f90b865SAlexander Duyck 		pgid = DCB_ATTR_VALUE_UNDEFINED;
6062f90b865SAlexander Duyck 		prio = DCB_ATTR_VALUE_UNDEFINED;
6072f90b865SAlexander Duyck 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
6082f90b865SAlexander Duyck 		up_map = DCB_ATTR_VALUE_UNDEFINED;
6092f90b865SAlexander Duyck 
6102f90b865SAlexander Duyck 		if (dir) {
6112f90b865SAlexander Duyck 			/* Rx */
6122f90b865SAlexander Duyck 			netdev->dcbnl_ops->getpgtccfgrx(netdev,
6132f90b865SAlexander Duyck 						i - DCB_PG_ATTR_TC_0, &prio,
6142f90b865SAlexander Duyck 						&pgid, &tc_pct, &up_map);
6152f90b865SAlexander Duyck 		} else {
6162f90b865SAlexander Duyck 			/* Tx */
6172f90b865SAlexander Duyck 			netdev->dcbnl_ops->getpgtccfgtx(netdev,
6182f90b865SAlexander Duyck 						i - DCB_PG_ATTR_TC_0, &prio,
6192f90b865SAlexander Duyck 						&pgid, &tc_pct, &up_map);
6202f90b865SAlexander Duyck 		}
6212f90b865SAlexander Duyck 
6222f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
6232f90b865SAlexander Duyck 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
6247be99413SThomas Graf 			ret = nla_put_u8(skb,
6252f90b865SAlexander Duyck 			                 DCB_TC_ATTR_PARAM_PGID, pgid);
6262f90b865SAlexander Duyck 			if (ret)
6272f90b865SAlexander Duyck 				goto err_param;
6282f90b865SAlexander Duyck 		}
6292f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
6302f90b865SAlexander Duyck 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
6317be99413SThomas Graf 			ret = nla_put_u8(skb,
6322f90b865SAlexander Duyck 			                 DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
6332f90b865SAlexander Duyck 			if (ret)
6342f90b865SAlexander Duyck 				goto err_param;
6352f90b865SAlexander Duyck 		}
6362f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
6372f90b865SAlexander Duyck 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
6387be99413SThomas Graf 			ret = nla_put_u8(skb,
6392f90b865SAlexander Duyck 			                 DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
6402f90b865SAlexander Duyck 			if (ret)
6412f90b865SAlexander Duyck 				goto err_param;
6422f90b865SAlexander Duyck 		}
6432f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
6442f90b865SAlexander Duyck 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
6457be99413SThomas Graf 			ret = nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT,
6462f90b865SAlexander Duyck 			                 tc_pct);
6472f90b865SAlexander Duyck 			if (ret)
6482f90b865SAlexander Duyck 				goto err_param;
6492f90b865SAlexander Duyck 		}
6507be99413SThomas Graf 		nla_nest_end(skb, param_nest);
6512f90b865SAlexander Duyck 	}
6522f90b865SAlexander Duyck 
6532f90b865SAlexander Duyck 	if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
6542f90b865SAlexander Duyck 		getall = 1;
6552f90b865SAlexander Duyck 	else
6562f90b865SAlexander Duyck 		getall = 0;
6572f90b865SAlexander Duyck 
6582f90b865SAlexander Duyck 	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
6592f90b865SAlexander Duyck 		if (!getall && !pg_tb[i])
6602f90b865SAlexander Duyck 			continue;
6612f90b865SAlexander Duyck 
6622f90b865SAlexander Duyck 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
6632f90b865SAlexander Duyck 
6642f90b865SAlexander Duyck 		if (dir) {
6652f90b865SAlexander Duyck 			/* Rx */
6662f90b865SAlexander Duyck 			netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
6672f90b865SAlexander Duyck 					i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
6682f90b865SAlexander Duyck 		} else {
6692f90b865SAlexander Duyck 			/* Tx */
6702f90b865SAlexander Duyck 			netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
6712f90b865SAlexander Duyck 					i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
6722f90b865SAlexander Duyck 		}
6737be99413SThomas Graf 		ret = nla_put_u8(skb, i, tc_pct);
6742f90b865SAlexander Duyck 		if (ret)
6752f90b865SAlexander Duyck 			goto err_pg;
6762f90b865SAlexander Duyck 	}
6772f90b865SAlexander Duyck 
6787be99413SThomas Graf 	nla_nest_end(skb, pg_nest);
6792f90b865SAlexander Duyck 
6802f90b865SAlexander Duyck 	return 0;
6812f90b865SAlexander Duyck 
6822f90b865SAlexander Duyck err_param:
6837be99413SThomas Graf 	nla_nest_cancel(skb, param_nest);
6842f90b865SAlexander Duyck err_pg:
6857be99413SThomas Graf 	nla_nest_cancel(skb, pg_nest);
6863d1f4869SThomas Graf 
6873d1f4869SThomas Graf 	return -EMSGSIZE;
6882f90b865SAlexander Duyck }
6892f90b865SAlexander Duyck 
6907be99413SThomas Graf static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
6917be99413SThomas Graf 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
6922f90b865SAlexander Duyck {
6937be99413SThomas Graf 	return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 0);
6942f90b865SAlexander Duyck }
6952f90b865SAlexander Duyck 
6967be99413SThomas Graf static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
6977be99413SThomas Graf 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
6982f90b865SAlexander Duyck {
6997be99413SThomas Graf 	return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 1);
7002f90b865SAlexander Duyck }
7012f90b865SAlexander Duyck 
7027be99413SThomas Graf static int dcbnl_setstate(struct net_device *netdev, struct nlmsghdr *nlh,
7037be99413SThomas Graf 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
7042f90b865SAlexander Duyck {
7052f90b865SAlexander Duyck 	u8 value;
7062f90b865SAlexander Duyck 
7073d1f4869SThomas Graf 	if (!tb[DCB_ATTR_STATE])
7087be99413SThomas Graf 		return -EINVAL;
7092f90b865SAlexander Duyck 
7103d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->setstate)
7113d1f4869SThomas Graf 		return -EOPNOTSUPP;
7123d1f4869SThomas Graf 
7132f90b865SAlexander Duyck 	value = nla_get_u8(tb[DCB_ATTR_STATE]);
7142f90b865SAlexander Duyck 
7157be99413SThomas Graf 	return nla_put_u8(skb, DCB_ATTR_STATE,
7167be99413SThomas Graf 			  netdev->dcbnl_ops->setstate(netdev, value));
7172f90b865SAlexander Duyck }
7182f90b865SAlexander Duyck 
7197be99413SThomas Graf static int dcbnl_setpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
7207be99413SThomas Graf 			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
7212f90b865SAlexander Duyck {
7222f90b865SAlexander Duyck 	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
7232f90b865SAlexander Duyck 	int i;
7243d1f4869SThomas Graf 	int ret;
7252f90b865SAlexander Duyck 	u8 value;
7262f90b865SAlexander Duyck 
7273d1f4869SThomas Graf 	if (!tb[DCB_ATTR_PFC_CFG])
7283d1f4869SThomas Graf 		return -EINVAL;
7293d1f4869SThomas Graf 
7303d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->setpfccfg)
7313d1f4869SThomas Graf 		return -EOPNOTSUPP;
7322f90b865SAlexander Duyck 
7332f90b865SAlexander Duyck 	ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
7342f90b865SAlexander Duyck 	                       tb[DCB_ATTR_PFC_CFG],
7352f90b865SAlexander Duyck 	                       dcbnl_pfc_up_nest);
7362f90b865SAlexander Duyck 	if (ret)
7373d1f4869SThomas Graf 		return ret;
7382f90b865SAlexander Duyck 
7392f90b865SAlexander Duyck 	for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
7402f90b865SAlexander Duyck 		if (data[i] == NULL)
7412f90b865SAlexander Duyck 			continue;
7422f90b865SAlexander Duyck 		value = nla_get_u8(data[i]);
7432f90b865SAlexander Duyck 		netdev->dcbnl_ops->setpfccfg(netdev,
7442f90b865SAlexander Duyck 			data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
7452f90b865SAlexander Duyck 	}
7462f90b865SAlexander Duyck 
7477be99413SThomas Graf 	return nla_put_u8(skb, DCB_ATTR_PFC_CFG, 0);
7482f90b865SAlexander Duyck }
7492f90b865SAlexander Duyck 
7507be99413SThomas Graf static int dcbnl_setall(struct net_device *netdev, struct nlmsghdr *nlh,
7517be99413SThomas Graf 			u32 seq, struct nlattr **tb, struct sk_buff *skb)
7522f90b865SAlexander Duyck {
7533d1f4869SThomas Graf 	int ret;
7542f90b865SAlexander Duyck 
7553d1f4869SThomas Graf 	if (!tb[DCB_ATTR_SET_ALL])
7563d1f4869SThomas Graf 		return -EINVAL;
7573d1f4869SThomas Graf 
7583d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->setall)
7593d1f4869SThomas Graf 		return -EOPNOTSUPP;
7602f90b865SAlexander Duyck 
7617be99413SThomas Graf 	ret = nla_put_u8(skb, DCB_ATTR_SET_ALL,
7627be99413SThomas Graf 			 netdev->dcbnl_ops->setall(netdev));
76308157984SJohn Fastabend 	dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SET_ALL, seq, 0);
7642f90b865SAlexander Duyck 
7652f90b865SAlexander Duyck 	return ret;
7662f90b865SAlexander Duyck }
7672f90b865SAlexander Duyck 
7687be99413SThomas Graf static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
7697be99413SThomas Graf 			     u32 seq, struct nlattr **tb, struct sk_buff *skb,
7707be99413SThomas Graf 			     int dir)
7712f90b865SAlexander Duyck {
7722f90b865SAlexander Duyck 	struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
7732f90b865SAlexander Duyck 	struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
7743d1f4869SThomas Graf 	int ret;
7752f90b865SAlexander Duyck 	int i;
7762f90b865SAlexander Duyck 	u8 pgid;
7772f90b865SAlexander Duyck 	u8 up_map;
7782f90b865SAlexander Duyck 	u8 prio;
7792f90b865SAlexander Duyck 	u8 tc_pct;
7802f90b865SAlexander Duyck 
7813d1f4869SThomas Graf 	if (!tb[DCB_ATTR_PG_CFG])
7823d1f4869SThomas Graf 		return -EINVAL;
7833d1f4869SThomas Graf 
7843d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->setpgtccfgtx ||
7852f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->setpgtccfgrx ||
7862f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->setpgbwgcfgtx ||
7872f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->setpgbwgcfgrx)
7883d1f4869SThomas Graf 		return -EOPNOTSUPP;
7892f90b865SAlexander Duyck 
7902f90b865SAlexander Duyck 	ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
7912f90b865SAlexander Duyck 	                       tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
7922f90b865SAlexander Duyck 	if (ret)
7933d1f4869SThomas Graf 		return ret;
7942f90b865SAlexander Duyck 
7952f90b865SAlexander Duyck 	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
7962f90b865SAlexander Duyck 		if (!pg_tb[i])
7972f90b865SAlexander Duyck 			continue;
7982f90b865SAlexander Duyck 
7992f90b865SAlexander Duyck 		ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
8002f90b865SAlexander Duyck 		                       pg_tb[i], dcbnl_tc_param_nest);
8012f90b865SAlexander Duyck 		if (ret)
8023d1f4869SThomas Graf 			return ret;
8032f90b865SAlexander Duyck 
8042f90b865SAlexander Duyck 		pgid = DCB_ATTR_VALUE_UNDEFINED;
8052f90b865SAlexander Duyck 		prio = DCB_ATTR_VALUE_UNDEFINED;
8062f90b865SAlexander Duyck 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
8072f90b865SAlexander Duyck 		up_map = DCB_ATTR_VALUE_UNDEFINED;
8082f90b865SAlexander Duyck 
8092f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
8102f90b865SAlexander Duyck 			prio =
8112f90b865SAlexander Duyck 			    nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
8122f90b865SAlexander Duyck 
8132f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_PGID])
8142f90b865SAlexander Duyck 			pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
8152f90b865SAlexander Duyck 
8162f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
8172f90b865SAlexander Duyck 			tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
8182f90b865SAlexander Duyck 
8192f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
8202f90b865SAlexander Duyck 			up_map =
8212f90b865SAlexander Duyck 			     nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
8222f90b865SAlexander Duyck 
8232f90b865SAlexander Duyck 		/* dir: Tx = 0, Rx = 1 */
8242f90b865SAlexander Duyck 		if (dir) {
8252f90b865SAlexander Duyck 			/* Rx */
8262f90b865SAlexander Duyck 			netdev->dcbnl_ops->setpgtccfgrx(netdev,
8272f90b865SAlexander Duyck 				i - DCB_PG_ATTR_TC_0,
8282f90b865SAlexander Duyck 				prio, pgid, tc_pct, up_map);
8292f90b865SAlexander Duyck 		} else {
8302f90b865SAlexander Duyck 			/* Tx */
8312f90b865SAlexander Duyck 			netdev->dcbnl_ops->setpgtccfgtx(netdev,
8322f90b865SAlexander Duyck 				i - DCB_PG_ATTR_TC_0,
8332f90b865SAlexander Duyck 				prio, pgid, tc_pct, up_map);
8342f90b865SAlexander Duyck 		}
8352f90b865SAlexander Duyck 	}
8362f90b865SAlexander Duyck 
8372f90b865SAlexander Duyck 	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
8382f90b865SAlexander Duyck 		if (!pg_tb[i])
8392f90b865SAlexander Duyck 			continue;
8402f90b865SAlexander Duyck 
8412f90b865SAlexander Duyck 		tc_pct = nla_get_u8(pg_tb[i]);
8422f90b865SAlexander Duyck 
8432f90b865SAlexander Duyck 		/* dir: Tx = 0, Rx = 1 */
8442f90b865SAlexander Duyck 		if (dir) {
8452f90b865SAlexander Duyck 			/* Rx */
8462f90b865SAlexander Duyck 			netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
8472f90b865SAlexander Duyck 					 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
8482f90b865SAlexander Duyck 		} else {
8492f90b865SAlexander Duyck 			/* Tx */
8502f90b865SAlexander Duyck 			netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
8512f90b865SAlexander Duyck 					 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
8522f90b865SAlexander Duyck 		}
8532f90b865SAlexander Duyck 	}
8542f90b865SAlexander Duyck 
855bb1dfefdSJohn Fastabend 	return nla_put_u8(skb, DCB_ATTR_PG_CFG, 0);
8562f90b865SAlexander Duyck }
8572f90b865SAlexander Duyck 
8587be99413SThomas Graf static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
8597be99413SThomas Graf 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
8602f90b865SAlexander Duyck {
8617be99413SThomas Graf 	return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 0);
8622f90b865SAlexander Duyck }
8632f90b865SAlexander Duyck 
8647be99413SThomas Graf static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
8657be99413SThomas Graf 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
8662f90b865SAlexander Duyck {
8677be99413SThomas Graf 	return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 1);
8682f90b865SAlexander Duyck }
8692f90b865SAlexander Duyck 
8707be99413SThomas Graf static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
8717be99413SThomas Graf 			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
872859ee3c4SAlexander Duyck {
873859ee3c4SAlexander Duyck 	struct nlattr *bcn_nest;
874859ee3c4SAlexander Duyck 	struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
875859ee3c4SAlexander Duyck 	u8 value_byte;
876859ee3c4SAlexander Duyck 	u32 value_integer;
8773d1f4869SThomas Graf 	int ret;
878859ee3c4SAlexander Duyck 	bool getall = false;
879859ee3c4SAlexander Duyck 	int i;
880859ee3c4SAlexander Duyck 
8813d1f4869SThomas Graf 	if (!tb[DCB_ATTR_BCN])
8823d1f4869SThomas Graf 		return -EINVAL;
8833d1f4869SThomas Graf 
8843d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->getbcnrp ||
885859ee3c4SAlexander Duyck 	    !netdev->dcbnl_ops->getbcncfg)
8863d1f4869SThomas Graf 		return -EOPNOTSUPP;
887859ee3c4SAlexander Duyck 
888859ee3c4SAlexander Duyck 	ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
889859ee3c4SAlexander Duyck 	                       tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
890859ee3c4SAlexander Duyck 	if (ret)
8913d1f4869SThomas Graf 		return ret;
892859ee3c4SAlexander Duyck 
8937be99413SThomas Graf 	bcn_nest = nla_nest_start(skb, DCB_ATTR_BCN);
894859ee3c4SAlexander Duyck 	if (!bcn_nest)
8953d1f4869SThomas Graf 		return -EMSGSIZE;
896859ee3c4SAlexander Duyck 
897859ee3c4SAlexander Duyck 	if (bcn_tb[DCB_BCN_ATTR_ALL])
898859ee3c4SAlexander Duyck 		getall = true;
899859ee3c4SAlexander Duyck 
900859ee3c4SAlexander Duyck 	for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
901859ee3c4SAlexander Duyck 		if (!getall && !bcn_tb[i])
902859ee3c4SAlexander Duyck 			continue;
903859ee3c4SAlexander Duyck 
904859ee3c4SAlexander Duyck 		netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
905859ee3c4SAlexander Duyck 		                            &value_byte);
9067be99413SThomas Graf 		ret = nla_put_u8(skb, i, value_byte);
907859ee3c4SAlexander Duyck 		if (ret)
908859ee3c4SAlexander Duyck 			goto err_bcn;
909859ee3c4SAlexander Duyck 	}
910859ee3c4SAlexander Duyck 
911f4314e81SDon Skidmore 	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
912859ee3c4SAlexander Duyck 		if (!getall && !bcn_tb[i])
913859ee3c4SAlexander Duyck 			continue;
914859ee3c4SAlexander Duyck 
915859ee3c4SAlexander Duyck 		netdev->dcbnl_ops->getbcncfg(netdev, i,
916859ee3c4SAlexander Duyck 		                             &value_integer);
9177be99413SThomas Graf 		ret = nla_put_u32(skb, i, value_integer);
918859ee3c4SAlexander Duyck 		if (ret)
919859ee3c4SAlexander Duyck 			goto err_bcn;
920859ee3c4SAlexander Duyck 	}
921859ee3c4SAlexander Duyck 
9227be99413SThomas Graf 	nla_nest_end(skb, bcn_nest);
923859ee3c4SAlexander Duyck 
924859ee3c4SAlexander Duyck 	return 0;
925859ee3c4SAlexander Duyck 
926859ee3c4SAlexander Duyck err_bcn:
9277be99413SThomas Graf 	nla_nest_cancel(skb, bcn_nest);
928859ee3c4SAlexander Duyck 	return ret;
929859ee3c4SAlexander Duyck }
930859ee3c4SAlexander Duyck 
9317be99413SThomas Graf static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
9327be99413SThomas Graf 			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
933859ee3c4SAlexander Duyck {
934859ee3c4SAlexander Duyck 	struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
935859ee3c4SAlexander Duyck 	int i;
9363d1f4869SThomas Graf 	int ret;
937859ee3c4SAlexander Duyck 	u8 value_byte;
938859ee3c4SAlexander Duyck 	u32 value_int;
939859ee3c4SAlexander Duyck 
9403d1f4869SThomas Graf 	if (!tb[DCB_ATTR_BCN])
9413d1f4869SThomas Graf 		return -EINVAL;
9423d1f4869SThomas Graf 
9433d1f4869SThomas Graf 	if (!netdev->dcbnl_ops->setbcncfg ||
944f64f9e71SJoe Perches 	    !netdev->dcbnl_ops->setbcnrp)
9453d1f4869SThomas Graf 		return -EOPNOTSUPP;
946859ee3c4SAlexander Duyck 
947859ee3c4SAlexander Duyck 	ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
948859ee3c4SAlexander Duyck 	                       tb[DCB_ATTR_BCN],
949859ee3c4SAlexander Duyck 	                       dcbnl_pfc_up_nest);
950859ee3c4SAlexander Duyck 	if (ret)
9513d1f4869SThomas Graf 		return ret;
952859ee3c4SAlexander Duyck 
953859ee3c4SAlexander Duyck 	for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
954859ee3c4SAlexander Duyck 		if (data[i] == NULL)
955859ee3c4SAlexander Duyck 			continue;
956859ee3c4SAlexander Duyck 		value_byte = nla_get_u8(data[i]);
957859ee3c4SAlexander Duyck 		netdev->dcbnl_ops->setbcnrp(netdev,
958859ee3c4SAlexander Duyck 			data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
959859ee3c4SAlexander Duyck 	}
960859ee3c4SAlexander Duyck 
961f4314e81SDon Skidmore 	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
962859ee3c4SAlexander Duyck 		if (data[i] == NULL)
963859ee3c4SAlexander Duyck 			continue;
964859ee3c4SAlexander Duyck 		value_int = nla_get_u32(data[i]);
965859ee3c4SAlexander Duyck 		netdev->dcbnl_ops->setbcncfg(netdev,
966859ee3c4SAlexander Duyck 	                                     i, value_int);
967859ee3c4SAlexander Duyck 	}
968859ee3c4SAlexander Duyck 
9693d1f4869SThomas Graf 	return nla_put_u8(skb, DCB_ATTR_BCN, 0);
970859ee3c4SAlexander Duyck }
971859ee3c4SAlexander Duyck 
972dc6ed1dfSShmulik Ravid static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,
973dc6ed1dfSShmulik Ravid 				int app_nested_type, int app_info_type,
974dc6ed1dfSShmulik Ravid 				int app_entry_type)
975eed84713SShmulik Ravid {
976eed84713SShmulik Ravid 	struct dcb_peer_app_info info;
977eed84713SShmulik Ravid 	struct dcb_app *table = NULL;
978eed84713SShmulik Ravid 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
979eed84713SShmulik Ravid 	u16 app_count;
980eed84713SShmulik Ravid 	int err;
981eed84713SShmulik Ravid 
982eed84713SShmulik Ravid 
983eed84713SShmulik Ravid 	/**
984eed84713SShmulik Ravid 	 * retrieve the peer app configuration form the driver. If the driver
985eed84713SShmulik Ravid 	 * handlers fail exit without doing anything
986eed84713SShmulik Ravid 	 */
987eed84713SShmulik Ravid 	err = ops->peer_getappinfo(netdev, &info, &app_count);
988eed84713SShmulik Ravid 	if (!err && app_count) {
989eed84713SShmulik Ravid 		table = kmalloc(sizeof(struct dcb_app) * app_count, GFP_KERNEL);
990eed84713SShmulik Ravid 		if (!table)
991eed84713SShmulik Ravid 			return -ENOMEM;
992eed84713SShmulik Ravid 
993eed84713SShmulik Ravid 		err = ops->peer_getapptable(netdev, table);
994eed84713SShmulik Ravid 	}
995eed84713SShmulik Ravid 
996eed84713SShmulik Ravid 	if (!err) {
997eed84713SShmulik Ravid 		u16 i;
998eed84713SShmulik Ravid 		struct nlattr *app;
999eed84713SShmulik Ravid 
1000eed84713SShmulik Ravid 		/**
1001eed84713SShmulik Ravid 		 * build the message, from here on the only possible failure
1002eed84713SShmulik Ravid 		 * is due to the skb size
1003eed84713SShmulik Ravid 		 */
1004eed84713SShmulik Ravid 		err = -EMSGSIZE;
1005eed84713SShmulik Ravid 
1006dc6ed1dfSShmulik Ravid 		app = nla_nest_start(skb, app_nested_type);
1007eed84713SShmulik Ravid 		if (!app)
1008eed84713SShmulik Ravid 			goto nla_put_failure;
1009eed84713SShmulik Ravid 
10101eb4c977SDavid S. Miller 		if (app_info_type &&
10111eb4c977SDavid S. Miller 		    nla_put(skb, app_info_type, sizeof(info), &info))
10121eb4c977SDavid S. Miller 			goto nla_put_failure;
1013dc6ed1dfSShmulik Ravid 
10141eb4c977SDavid S. Miller 		for (i = 0; i < app_count; i++) {
10151eb4c977SDavid S. Miller 			if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
10161eb4c977SDavid S. Miller 				    &table[i]))
10171eb4c977SDavid S. Miller 				goto nla_put_failure;
10181eb4c977SDavid S. Miller 		}
1019eed84713SShmulik Ravid 		nla_nest_end(skb, app);
1020eed84713SShmulik Ravid 	}
1021eed84713SShmulik Ravid 	err = 0;
1022eed84713SShmulik Ravid 
1023eed84713SShmulik Ravid nla_put_failure:
1024eed84713SShmulik Ravid 	kfree(table);
1025eed84713SShmulik Ravid 	return err;
1026eed84713SShmulik Ravid }
10273e29027aSJohn Fastabend 
10283e29027aSJohn Fastabend /* Handle IEEE 802.1Qaz GET commands. */
1029314b4778SJohn Fastabend static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
10303e29027aSJohn Fastabend {
10319ab933abSJohn Fastabend 	struct nlattr *ieee, *app;
10329ab933abSJohn Fastabend 	struct dcb_app_type *itr;
10333e29027aSJohn Fastabend 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1034c7797bafSJohn Fastabend 	int dcbx;
10353d1f4869SThomas Graf 	int err;
10363e29027aSJohn Fastabend 
10371eb4c977SDavid S. Miller 	if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
10383d1f4869SThomas Graf 		return -EMSGSIZE;
10393d1f4869SThomas Graf 
10403e29027aSJohn Fastabend 	ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
10413e29027aSJohn Fastabend 	if (!ieee)
10423d1f4869SThomas Graf 		return -EMSGSIZE;
10433e29027aSJohn Fastabend 
10443e29027aSJohn Fastabend 	if (ops->ieee_getets) {
10453e29027aSJohn Fastabend 		struct ieee_ets ets;
10463e29027aSJohn Fastabend 		err = ops->ieee_getets(netdev, &ets);
10471eb4c977SDavid S. Miller 		if (!err &&
10481eb4c977SDavid S. Miller 		    nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
10493d1f4869SThomas Graf 			return -EMSGSIZE;
10503e29027aSJohn Fastabend 	}
10513e29027aSJohn Fastabend 
105208f10affSAmir Vadai 	if (ops->ieee_getmaxrate) {
105308f10affSAmir Vadai 		struct ieee_maxrate maxrate;
105408f10affSAmir Vadai 		err = ops->ieee_getmaxrate(netdev, &maxrate);
105508f10affSAmir Vadai 		if (!err) {
105608f10affSAmir Vadai 			err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
105708f10affSAmir Vadai 				      sizeof(maxrate), &maxrate);
105808f10affSAmir Vadai 			if (err)
10593d1f4869SThomas Graf 				return -EMSGSIZE;
106008f10affSAmir Vadai 		}
106108f10affSAmir Vadai 	}
106208f10affSAmir Vadai 
10633e29027aSJohn Fastabend 	if (ops->ieee_getpfc) {
10643e29027aSJohn Fastabend 		struct ieee_pfc pfc;
10653e29027aSJohn Fastabend 		err = ops->ieee_getpfc(netdev, &pfc);
10661eb4c977SDavid S. Miller 		if (!err &&
10671eb4c977SDavid S. Miller 		    nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
10683d1f4869SThomas Graf 			return -EMSGSIZE;
10693e29027aSJohn Fastabend 	}
10703e29027aSJohn Fastabend 
10719ab933abSJohn Fastabend 	app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
10729ab933abSJohn Fastabend 	if (!app)
10733d1f4869SThomas Graf 		return -EMSGSIZE;
10749ab933abSJohn Fastabend 
10759ab933abSJohn Fastabend 	spin_lock(&dcb_lock);
10769ab933abSJohn Fastabend 	list_for_each_entry(itr, &dcb_app_list, list) {
1077e290ed81SMark Rustad 		if (itr->ifindex == netdev->ifindex) {
107870bfa2d2SDan Carpenter 			err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
107970bfa2d2SDan Carpenter 					 &itr->app);
108070bfa2d2SDan Carpenter 			if (err) {
108170bfa2d2SDan Carpenter 				spin_unlock(&dcb_lock);
10823d1f4869SThomas Graf 				return -EMSGSIZE;
108370bfa2d2SDan Carpenter 			}
108470bfa2d2SDan Carpenter 		}
10859ab933abSJohn Fastabend 	}
1086c7797bafSJohn Fastabend 
1087c7797bafSJohn Fastabend 	if (netdev->dcbnl_ops->getdcbx)
1088c7797bafSJohn Fastabend 		dcbx = netdev->dcbnl_ops->getdcbx(netdev);
1089c7797bafSJohn Fastabend 	else
1090c7797bafSJohn Fastabend 		dcbx = -EOPNOTSUPP;
1091c7797bafSJohn Fastabend 
10929ab933abSJohn Fastabend 	spin_unlock(&dcb_lock);
10939ab933abSJohn Fastabend 	nla_nest_end(skb, app);
10949ab933abSJohn Fastabend 
1095eed84713SShmulik Ravid 	/* get peer info if available */
1096eed84713SShmulik Ravid 	if (ops->ieee_peer_getets) {
1097eed84713SShmulik Ravid 		struct ieee_ets ets;
1098eed84713SShmulik Ravid 		err = ops->ieee_peer_getets(netdev, &ets);
10991eb4c977SDavid S. Miller 		if (!err &&
11001eb4c977SDavid S. Miller 		    nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
11013d1f4869SThomas Graf 			return -EMSGSIZE;
1102eed84713SShmulik Ravid 	}
1103eed84713SShmulik Ravid 
1104eed84713SShmulik Ravid 	if (ops->ieee_peer_getpfc) {
1105eed84713SShmulik Ravid 		struct ieee_pfc pfc;
1106eed84713SShmulik Ravid 		err = ops->ieee_peer_getpfc(netdev, &pfc);
11071eb4c977SDavid S. Miller 		if (!err &&
11081eb4c977SDavid S. Miller 		    nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
11093d1f4869SThomas Graf 			return -EMSGSIZE;
1110eed84713SShmulik Ravid 	}
1111eed84713SShmulik Ravid 
1112eed84713SShmulik Ravid 	if (ops->peer_getappinfo && ops->peer_getapptable) {
1113dc6ed1dfSShmulik Ravid 		err = dcbnl_build_peer_app(netdev, skb,
1114dc6ed1dfSShmulik Ravid 					   DCB_ATTR_IEEE_PEER_APP,
1115dc6ed1dfSShmulik Ravid 					   DCB_ATTR_IEEE_APP_UNSPEC,
1116dc6ed1dfSShmulik Ravid 					   DCB_ATTR_IEEE_APP);
1117eed84713SShmulik Ravid 		if (err)
11183d1f4869SThomas Graf 			return -EMSGSIZE;
1119eed84713SShmulik Ravid 	}
1120eed84713SShmulik Ravid 
11213e29027aSJohn Fastabend 	nla_nest_end(skb, ieee);
1122c7797bafSJohn Fastabend 	if (dcbx >= 0) {
1123c7797bafSJohn Fastabend 		err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
1124c7797bafSJohn Fastabend 		if (err)
11253d1f4869SThomas Graf 			return -EMSGSIZE;
1126c7797bafSJohn Fastabend 	}
11273e29027aSJohn Fastabend 
1128314b4778SJohn Fastabend 	return 0;
11293e29027aSJohn Fastabend }
11303e29027aSJohn Fastabend 
11315b7f7626SShmulik Ravid static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
11325b7f7626SShmulik Ravid 			     int dir)
11335b7f7626SShmulik Ravid {
11345b7f7626SShmulik Ravid 	u8 pgid, up_map, prio, tc_pct;
11355b7f7626SShmulik Ravid 	const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
11365b7f7626SShmulik Ravid 	int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
11375b7f7626SShmulik Ravid 	struct nlattr *pg = nla_nest_start(skb, i);
11385b7f7626SShmulik Ravid 
11395b7f7626SShmulik Ravid 	if (!pg)
11403d1f4869SThomas Graf 		return -EMSGSIZE;
11415b7f7626SShmulik Ravid 
11425b7f7626SShmulik Ravid 	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
11435b7f7626SShmulik Ravid 		struct nlattr *tc_nest = nla_nest_start(skb, i);
11445b7f7626SShmulik Ravid 
11455b7f7626SShmulik Ravid 		if (!tc_nest)
11463d1f4869SThomas Graf 			return -EMSGSIZE;
11475b7f7626SShmulik Ravid 
11485b7f7626SShmulik Ravid 		pgid = DCB_ATTR_VALUE_UNDEFINED;
11495b7f7626SShmulik Ravid 		prio = DCB_ATTR_VALUE_UNDEFINED;
11505b7f7626SShmulik Ravid 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
11515b7f7626SShmulik Ravid 		up_map = DCB_ATTR_VALUE_UNDEFINED;
11525b7f7626SShmulik Ravid 
11535b7f7626SShmulik Ravid 		if (!dir)
11545b7f7626SShmulik Ravid 			ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
11555b7f7626SShmulik Ravid 					  &prio, &pgid, &tc_pct, &up_map);
11565b7f7626SShmulik Ravid 		else
11575b7f7626SShmulik Ravid 			ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
11585b7f7626SShmulik Ravid 					  &prio, &pgid, &tc_pct, &up_map);
11595b7f7626SShmulik Ravid 
11601eb4c977SDavid S. Miller 		if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
11611eb4c977SDavid S. Miller 		    nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
11621eb4c977SDavid S. Miller 		    nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
11631eb4c977SDavid S. Miller 		    nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
11643d1f4869SThomas Graf 			return -EMSGSIZE;
11655b7f7626SShmulik Ravid 		nla_nest_end(skb, tc_nest);
11665b7f7626SShmulik Ravid 	}
11675b7f7626SShmulik Ravid 
11685b7f7626SShmulik Ravid 	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
11695b7f7626SShmulik Ravid 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
11705b7f7626SShmulik Ravid 
11715b7f7626SShmulik Ravid 		if (!dir)
11725b7f7626SShmulik Ravid 			ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
11735b7f7626SShmulik Ravid 					   &tc_pct);
11745b7f7626SShmulik Ravid 		else
11755b7f7626SShmulik Ravid 			ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
11765b7f7626SShmulik Ravid 					   &tc_pct);
11771eb4c977SDavid S. Miller 		if (nla_put_u8(skb, i, tc_pct))
11783d1f4869SThomas Graf 			return -EMSGSIZE;
11795b7f7626SShmulik Ravid 	}
11805b7f7626SShmulik Ravid 	nla_nest_end(skb, pg);
11815b7f7626SShmulik Ravid 	return 0;
11825b7f7626SShmulik Ravid }
11835b7f7626SShmulik Ravid 
11845b7f7626SShmulik Ravid static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
11855b7f7626SShmulik Ravid {
11865b7f7626SShmulik Ravid 	struct nlattr *cee, *app;
11875b7f7626SShmulik Ravid 	struct dcb_app_type *itr;
11885b7f7626SShmulik Ravid 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
11895b7f7626SShmulik Ravid 	int dcbx, i, err = -EMSGSIZE;
11905b7f7626SShmulik Ravid 	u8 value;
11915b7f7626SShmulik Ravid 
11921eb4c977SDavid S. Miller 	if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
11931eb4c977SDavid S. Miller 		goto nla_put_failure;
11945b7f7626SShmulik Ravid 	cee = nla_nest_start(skb, DCB_ATTR_CEE);
11955b7f7626SShmulik Ravid 	if (!cee)
11965b7f7626SShmulik Ravid 		goto nla_put_failure;
11975b7f7626SShmulik Ravid 
11985b7f7626SShmulik Ravid 	/* local pg */
11995b7f7626SShmulik Ravid 	if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
12005b7f7626SShmulik Ravid 		err = dcbnl_cee_pg_fill(skb, netdev, 1);
12015b7f7626SShmulik Ravid 		if (err)
12025b7f7626SShmulik Ravid 			goto nla_put_failure;
12035b7f7626SShmulik Ravid 	}
12045b7f7626SShmulik Ravid 
12055b7f7626SShmulik Ravid 	if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
12065b7f7626SShmulik Ravid 		err = dcbnl_cee_pg_fill(skb, netdev, 0);
12075b7f7626SShmulik Ravid 		if (err)
12085b7f7626SShmulik Ravid 			goto nla_put_failure;
12095b7f7626SShmulik Ravid 	}
12105b7f7626SShmulik Ravid 
12115b7f7626SShmulik Ravid 	/* local pfc */
12125b7f7626SShmulik Ravid 	if (ops->getpfccfg) {
12135b7f7626SShmulik Ravid 		struct nlattr *pfc_nest = nla_nest_start(skb, DCB_ATTR_CEE_PFC);
12145b7f7626SShmulik Ravid 
12155b7f7626SShmulik Ravid 		if (!pfc_nest)
12165b7f7626SShmulik Ravid 			goto nla_put_failure;
12175b7f7626SShmulik Ravid 
12185b7f7626SShmulik Ravid 		for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
12195b7f7626SShmulik Ravid 			ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
12201eb4c977SDavid S. Miller 			if (nla_put_u8(skb, i, value))
12211eb4c977SDavid S. Miller 				goto nla_put_failure;
12225b7f7626SShmulik Ravid 		}
12235b7f7626SShmulik Ravid 		nla_nest_end(skb, pfc_nest);
12245b7f7626SShmulik Ravid 	}
12255b7f7626SShmulik Ravid 
12265b7f7626SShmulik Ravid 	/* local app */
12275b7f7626SShmulik Ravid 	spin_lock(&dcb_lock);
12285b7f7626SShmulik Ravid 	app = nla_nest_start(skb, DCB_ATTR_CEE_APP_TABLE);
12295b7f7626SShmulik Ravid 	if (!app)
123040f5d72aSDan Carpenter 		goto dcb_unlock;
12315b7f7626SShmulik Ravid 
12325b7f7626SShmulik Ravid 	list_for_each_entry(itr, &dcb_app_list, list) {
1233e290ed81SMark Rustad 		if (itr->ifindex == netdev->ifindex) {
12345b7f7626SShmulik Ravid 			struct nlattr *app_nest = nla_nest_start(skb,
12355b7f7626SShmulik Ravid 								 DCB_ATTR_APP);
12365b7f7626SShmulik Ravid 			if (!app_nest)
12375b7f7626SShmulik Ravid 				goto dcb_unlock;
12385b7f7626SShmulik Ravid 
12395b7f7626SShmulik Ravid 			err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
12405b7f7626SShmulik Ravid 					 itr->app.selector);
12415b7f7626SShmulik Ravid 			if (err)
12425b7f7626SShmulik Ravid 				goto dcb_unlock;
12435b7f7626SShmulik Ravid 
12445b7f7626SShmulik Ravid 			err = nla_put_u16(skb, DCB_APP_ATTR_ID,
12455b7f7626SShmulik Ravid 					  itr->app.protocol);
12465b7f7626SShmulik Ravid 			if (err)
12475b7f7626SShmulik Ravid 				goto dcb_unlock;
12485b7f7626SShmulik Ravid 
12495b7f7626SShmulik Ravid 			err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
12505b7f7626SShmulik Ravid 					 itr->app.priority);
12515b7f7626SShmulik Ravid 			if (err)
12525b7f7626SShmulik Ravid 				goto dcb_unlock;
12535b7f7626SShmulik Ravid 
12545b7f7626SShmulik Ravid 			nla_nest_end(skb, app_nest);
12555b7f7626SShmulik Ravid 		}
12565b7f7626SShmulik Ravid 	}
12575b7f7626SShmulik Ravid 	nla_nest_end(skb, app);
12585b7f7626SShmulik Ravid 
12595b7f7626SShmulik Ravid 	if (netdev->dcbnl_ops->getdcbx)
12605b7f7626SShmulik Ravid 		dcbx = netdev->dcbnl_ops->getdcbx(netdev);
12615b7f7626SShmulik Ravid 	else
12625b7f7626SShmulik Ravid 		dcbx = -EOPNOTSUPP;
12635b7f7626SShmulik Ravid 
12645b7f7626SShmulik Ravid 	spin_unlock(&dcb_lock);
12655b7f7626SShmulik Ravid 
12665b7f7626SShmulik Ravid 	/* features flags */
12675b7f7626SShmulik Ravid 	if (ops->getfeatcfg) {
12685b7f7626SShmulik Ravid 		struct nlattr *feat = nla_nest_start(skb, DCB_ATTR_CEE_FEAT);
12695b7f7626SShmulik Ravid 		if (!feat)
12705b7f7626SShmulik Ravid 			goto nla_put_failure;
12715b7f7626SShmulik Ravid 
12725b7f7626SShmulik Ravid 		for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
12735b7f7626SShmulik Ravid 		     i++)
12741eb4c977SDavid S. Miller 			if (!ops->getfeatcfg(netdev, i, &value) &&
12751eb4c977SDavid S. Miller 			    nla_put_u8(skb, i, value))
12761eb4c977SDavid S. Miller 				goto nla_put_failure;
12775b7f7626SShmulik Ravid 
12785b7f7626SShmulik Ravid 		nla_nest_end(skb, feat);
12795b7f7626SShmulik Ravid 	}
12805b7f7626SShmulik Ravid 
12815b7f7626SShmulik Ravid 	/* peer info if available */
12825b7f7626SShmulik Ravid 	if (ops->cee_peer_getpg) {
12835b7f7626SShmulik Ravid 		struct cee_pg pg;
12845b7f7626SShmulik Ravid 		err = ops->cee_peer_getpg(netdev, &pg);
12851eb4c977SDavid S. Miller 		if (!err &&
12861eb4c977SDavid S. Miller 		    nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
12871eb4c977SDavid S. Miller 			goto nla_put_failure;
12885b7f7626SShmulik Ravid 	}
12895b7f7626SShmulik Ravid 
12905b7f7626SShmulik Ravid 	if (ops->cee_peer_getpfc) {
12915b7f7626SShmulik Ravid 		struct cee_pfc pfc;
12925b7f7626SShmulik Ravid 		err = ops->cee_peer_getpfc(netdev, &pfc);
12931eb4c977SDavid S. Miller 		if (!err &&
12941eb4c977SDavid S. Miller 		    nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
12951eb4c977SDavid S. Miller 			goto nla_put_failure;
12965b7f7626SShmulik Ravid 	}
12975b7f7626SShmulik Ravid 
12985b7f7626SShmulik Ravid 	if (ops->peer_getappinfo && ops->peer_getapptable) {
12995b7f7626SShmulik Ravid 		err = dcbnl_build_peer_app(netdev, skb,
13005b7f7626SShmulik Ravid 					   DCB_ATTR_CEE_PEER_APP_TABLE,
13015b7f7626SShmulik Ravid 					   DCB_ATTR_CEE_PEER_APP_INFO,
13025b7f7626SShmulik Ravid 					   DCB_ATTR_CEE_PEER_APP);
13035b7f7626SShmulik Ravid 		if (err)
13045b7f7626SShmulik Ravid 			goto nla_put_failure;
13055b7f7626SShmulik Ravid 	}
13065b7f7626SShmulik Ravid 	nla_nest_end(skb, cee);
13075b7f7626SShmulik Ravid 
13085b7f7626SShmulik Ravid 	/* DCBX state */
13095b7f7626SShmulik Ravid 	if (dcbx >= 0) {
13105b7f7626SShmulik Ravid 		err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
13115b7f7626SShmulik Ravid 		if (err)
13125b7f7626SShmulik Ravid 			goto nla_put_failure;
13135b7f7626SShmulik Ravid 	}
13145b7f7626SShmulik Ravid 	return 0;
13155b7f7626SShmulik Ravid 
13165b7f7626SShmulik Ravid dcb_unlock:
13175b7f7626SShmulik Ravid 	spin_unlock(&dcb_lock);
13185b7f7626SShmulik Ravid nla_put_failure:
13195b7f7626SShmulik Ravid 	return err;
13205b7f7626SShmulik Ravid }
13215b7f7626SShmulik Ravid 
13225b7f7626SShmulik Ravid static int dcbnl_notify(struct net_device *dev, int event, int cmd,
13235b7f7626SShmulik Ravid 			u32 seq, u32 pid, int dcbx_ver)
1324314b4778SJohn Fastabend {
1325314b4778SJohn Fastabend 	struct net *net = dev_net(dev);
1326314b4778SJohn Fastabend 	struct sk_buff *skb;
1327314b4778SJohn Fastabend 	struct nlmsghdr *nlh;
1328314b4778SJohn Fastabend 	const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
1329314b4778SJohn Fastabend 	int err;
1330314b4778SJohn Fastabend 
1331314b4778SJohn Fastabend 	if (!ops)
1332314b4778SJohn Fastabend 		return -EOPNOTSUPP;
1333314b4778SJohn Fastabend 
1334ab6d4707SThomas Graf 	skb = dcbnl_newmsg(event, cmd, pid, seq, 0, &nlh);
1335314b4778SJohn Fastabend 	if (!skb)
1336314b4778SJohn Fastabend 		return -ENOBUFS;
1337314b4778SJohn Fastabend 
13385b7f7626SShmulik Ravid 	if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
1339314b4778SJohn Fastabend 		err = dcbnl_ieee_fill(skb, dev);
13405b7f7626SShmulik Ravid 	else
13415b7f7626SShmulik Ravid 		err = dcbnl_cee_fill(skb, dev);
13425b7f7626SShmulik Ravid 
1343314b4778SJohn Fastabend 	if (err < 0) {
1344314b4778SJohn Fastabend 		/* Report error to broadcast listeners */
1345ab6d4707SThomas Graf 		nlmsg_free(skb);
1346314b4778SJohn Fastabend 		rtnl_set_sk_err(net, RTNLGRP_DCB, err);
1347314b4778SJohn Fastabend 	} else {
1348314b4778SJohn Fastabend 		/* End nlmsg and notify broadcast listeners */
1349314b4778SJohn Fastabend 		nlmsg_end(skb, nlh);
1350314b4778SJohn Fastabend 		rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
1351314b4778SJohn Fastabend 	}
1352314b4778SJohn Fastabend 
1353314b4778SJohn Fastabend 	return err;
1354314b4778SJohn Fastabend }
13555b7f7626SShmulik Ravid 
13565b7f7626SShmulik Ravid int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
13575b7f7626SShmulik Ravid 		      u32 seq, u32 pid)
13585b7f7626SShmulik Ravid {
13595b7f7626SShmulik Ravid 	return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_IEEE);
13605b7f7626SShmulik Ravid }
13615b7f7626SShmulik Ravid EXPORT_SYMBOL(dcbnl_ieee_notify);
13625b7f7626SShmulik Ravid 
13635b7f7626SShmulik Ravid int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
13645b7f7626SShmulik Ravid 		     u32 seq, u32 pid)
13655b7f7626SShmulik Ravid {
13665b7f7626SShmulik Ravid 	return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_CEE);
13675b7f7626SShmulik Ravid }
13685b7f7626SShmulik Ravid EXPORT_SYMBOL(dcbnl_cee_notify);
1369314b4778SJohn Fastabend 
1370314b4778SJohn Fastabend /* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
1371314b4778SJohn Fastabend  * be completed the entire msg is aborted and error value is returned.
1372314b4778SJohn Fastabend  * No attempt is made to reconcile the case where only part of the
1373314b4778SJohn Fastabend  * cmd can be completed.
1374314b4778SJohn Fastabend  */
13757be99413SThomas Graf static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
13767be99413SThomas Graf 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
1377314b4778SJohn Fastabend {
1378314b4778SJohn Fastabend 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1379314b4778SJohn Fastabend 	struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
13803d1f4869SThomas Graf 	int err;
1381314b4778SJohn Fastabend 
1382314b4778SJohn Fastabend 	if (!ops)
13833d1f4869SThomas Graf 		return -EOPNOTSUPP;
1384314b4778SJohn Fastabend 
13854003b658SJohn Fastabend 	if (!tb[DCB_ATTR_IEEE])
13864003b658SJohn Fastabend 		return -EINVAL;
13874003b658SJohn Fastabend 
1388314b4778SJohn Fastabend 	err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
1389314b4778SJohn Fastabend 			       tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
1390314b4778SJohn Fastabend 	if (err)
1391314b4778SJohn Fastabend 		return err;
1392314b4778SJohn Fastabend 
1393314b4778SJohn Fastabend 	if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
1394314b4778SJohn Fastabend 		struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
1395314b4778SJohn Fastabend 		err = ops->ieee_setets(netdev, ets);
1396314b4778SJohn Fastabend 		if (err)
1397314b4778SJohn Fastabend 			goto err;
1398314b4778SJohn Fastabend 	}
1399314b4778SJohn Fastabend 
140008f10affSAmir Vadai 	if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
140108f10affSAmir Vadai 		struct ieee_maxrate *maxrate =
140208f10affSAmir Vadai 			nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
140308f10affSAmir Vadai 		err = ops->ieee_setmaxrate(netdev, maxrate);
140408f10affSAmir Vadai 		if (err)
140508f10affSAmir Vadai 			goto err;
140608f10affSAmir Vadai 	}
140708f10affSAmir Vadai 
1408314b4778SJohn Fastabend 	if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
1409314b4778SJohn Fastabend 		struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
1410314b4778SJohn Fastabend 		err = ops->ieee_setpfc(netdev, pfc);
1411314b4778SJohn Fastabend 		if (err)
1412314b4778SJohn Fastabend 			goto err;
1413314b4778SJohn Fastabend 	}
1414314b4778SJohn Fastabend 
1415314b4778SJohn Fastabend 	if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1416314b4778SJohn Fastabend 		struct nlattr *attr;
1417314b4778SJohn Fastabend 		int rem;
1418314b4778SJohn Fastabend 
1419314b4778SJohn Fastabend 		nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
1420314b4778SJohn Fastabend 			struct dcb_app *app_data;
1421314b4778SJohn Fastabend 			if (nla_type(attr) != DCB_ATTR_IEEE_APP)
1422314b4778SJohn Fastabend 				continue;
1423314b4778SJohn Fastabend 			app_data = nla_data(attr);
1424314b4778SJohn Fastabend 			if (ops->ieee_setapp)
1425314b4778SJohn Fastabend 				err = ops->ieee_setapp(netdev, app_data);
1426314b4778SJohn Fastabend 			else
1427b6db2174SJohn Fastabend 				err = dcb_ieee_setapp(netdev, app_data);
1428314b4778SJohn Fastabend 			if (err)
1429314b4778SJohn Fastabend 				goto err;
1430314b4778SJohn Fastabend 		}
1431314b4778SJohn Fastabend 	}
1432314b4778SJohn Fastabend 
1433314b4778SJohn Fastabend err:
14347be99413SThomas Graf 	err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
14355b7f7626SShmulik Ravid 	dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
1436314b4778SJohn Fastabend 	return err;
1437314b4778SJohn Fastabend }
1438314b4778SJohn Fastabend 
14397be99413SThomas Graf static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
14407be99413SThomas Graf 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
1441314b4778SJohn Fastabend {
1442314b4778SJohn Fastabend 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1443314b4778SJohn Fastabend 
1444314b4778SJohn Fastabend 	if (!ops)
1445314b4778SJohn Fastabend 		return -EOPNOTSUPP;
1446314b4778SJohn Fastabend 
14477be99413SThomas Graf 	return dcbnl_ieee_fill(skb, netdev);
1448314b4778SJohn Fastabend }
1449314b4778SJohn Fastabend 
14507be99413SThomas Graf static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
14517be99413SThomas Graf 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
1452f9ae7e4bSJohn Fastabend {
1453f9ae7e4bSJohn Fastabend 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1454f9ae7e4bSJohn Fastabend 	struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
14553d1f4869SThomas Graf 	int err;
1456f9ae7e4bSJohn Fastabend 
1457f9ae7e4bSJohn Fastabend 	if (!ops)
1458f9ae7e4bSJohn Fastabend 		return -EOPNOTSUPP;
1459f9ae7e4bSJohn Fastabend 
1460f9ae7e4bSJohn Fastabend 	if (!tb[DCB_ATTR_IEEE])
1461f9ae7e4bSJohn Fastabend 		return -EINVAL;
1462f9ae7e4bSJohn Fastabend 
1463f9ae7e4bSJohn Fastabend 	err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
1464f9ae7e4bSJohn Fastabend 			       tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
1465f9ae7e4bSJohn Fastabend 	if (err)
1466f9ae7e4bSJohn Fastabend 		return err;
1467f9ae7e4bSJohn Fastabend 
1468f9ae7e4bSJohn Fastabend 	if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1469f9ae7e4bSJohn Fastabend 		struct nlattr *attr;
1470f9ae7e4bSJohn Fastabend 		int rem;
1471f9ae7e4bSJohn Fastabend 
1472f9ae7e4bSJohn Fastabend 		nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
1473f9ae7e4bSJohn Fastabend 			struct dcb_app *app_data;
1474f9ae7e4bSJohn Fastabend 
1475f9ae7e4bSJohn Fastabend 			if (nla_type(attr) != DCB_ATTR_IEEE_APP)
1476f9ae7e4bSJohn Fastabend 				continue;
1477f9ae7e4bSJohn Fastabend 			app_data = nla_data(attr);
1478f9ae7e4bSJohn Fastabend 			if (ops->ieee_delapp)
1479f9ae7e4bSJohn Fastabend 				err = ops->ieee_delapp(netdev, app_data);
1480f9ae7e4bSJohn Fastabend 			else
1481f9ae7e4bSJohn Fastabend 				err = dcb_ieee_delapp(netdev, app_data);
1482f9ae7e4bSJohn Fastabend 			if (err)
1483f9ae7e4bSJohn Fastabend 				goto err;
1484f9ae7e4bSJohn Fastabend 		}
1485f9ae7e4bSJohn Fastabend 	}
1486f9ae7e4bSJohn Fastabend 
1487f9ae7e4bSJohn Fastabend err:
14887be99413SThomas Graf 	err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
14895b7f7626SShmulik Ravid 	dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
1490f9ae7e4bSJohn Fastabend 	return err;
1491f9ae7e4bSJohn Fastabend }
1492f9ae7e4bSJohn Fastabend 
1493f9ae7e4bSJohn Fastabend 
14946241b625SShmulik Ravid /* DCBX configuration */
14957be99413SThomas Graf static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
14967be99413SThomas Graf 			 u32 seq, struct nlattr **tb, struct sk_buff *skb)
14976241b625SShmulik Ravid {
14986241b625SShmulik Ravid 	if (!netdev->dcbnl_ops->getdcbx)
14997f891cf1SShmulik Ravid 		return -EOPNOTSUPP;
15006241b625SShmulik Ravid 
15017be99413SThomas Graf 	return nla_put_u8(skb, DCB_ATTR_DCBX,
15027be99413SThomas Graf 			  netdev->dcbnl_ops->getdcbx(netdev));
15036241b625SShmulik Ravid }
15046241b625SShmulik Ravid 
15057be99413SThomas Graf static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
15067be99413SThomas Graf 			 u32 seq, struct nlattr **tb, struct sk_buff *skb)
15076241b625SShmulik Ravid {
15086241b625SShmulik Ravid 	u8 value;
15096241b625SShmulik Ravid 
15107f891cf1SShmulik Ravid 	if (!netdev->dcbnl_ops->setdcbx)
15117f891cf1SShmulik Ravid 		return -EOPNOTSUPP;
15127f891cf1SShmulik Ravid 
15137f891cf1SShmulik Ravid 	if (!tb[DCB_ATTR_DCBX])
15147f891cf1SShmulik Ravid 		return -EINVAL;
15156241b625SShmulik Ravid 
15166241b625SShmulik Ravid 	value = nla_get_u8(tb[DCB_ATTR_DCBX]);
15176241b625SShmulik Ravid 
15187be99413SThomas Graf 	return nla_put_u8(skb, DCB_ATTR_DCBX,
15197be99413SThomas Graf 			  netdev->dcbnl_ops->setdcbx(netdev, value));
15206241b625SShmulik Ravid }
15216241b625SShmulik Ravid 
15227be99413SThomas Graf static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
15237be99413SThomas Graf 			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
1524ea45fe4eSShmulik Ravid {
1525ea45fe4eSShmulik Ravid 	struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
1526ea45fe4eSShmulik Ravid 	u8 value;
15277f891cf1SShmulik Ravid 	int ret, i;
1528ea45fe4eSShmulik Ravid 	int getall = 0;
1529ea45fe4eSShmulik Ravid 
15307f891cf1SShmulik Ravid 	if (!netdev->dcbnl_ops->getfeatcfg)
15317f891cf1SShmulik Ravid 		return -EOPNOTSUPP;
15327f891cf1SShmulik Ravid 
15337f891cf1SShmulik Ravid 	if (!tb[DCB_ATTR_FEATCFG])
15347f891cf1SShmulik Ravid 		return -EINVAL;
1535ea45fe4eSShmulik Ravid 
1536ea45fe4eSShmulik Ravid 	ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
1537ea45fe4eSShmulik Ravid 			       dcbnl_featcfg_nest);
15387f891cf1SShmulik Ravid 	if (ret)
15397be99413SThomas Graf 		return ret;
1540ea45fe4eSShmulik Ravid 
15417be99413SThomas Graf 	nest = nla_nest_start(skb, DCB_ATTR_FEATCFG);
15427be99413SThomas Graf 	if (!nest)
15437be99413SThomas Graf 		return -EMSGSIZE;
1544ea45fe4eSShmulik Ravid 
1545ea45fe4eSShmulik Ravid 	if (data[DCB_FEATCFG_ATTR_ALL])
1546ea45fe4eSShmulik Ravid 		getall = 1;
1547ea45fe4eSShmulik Ravid 
1548ea45fe4eSShmulik Ravid 	for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1549ea45fe4eSShmulik Ravid 		if (!getall && !data[i])
1550ea45fe4eSShmulik Ravid 			continue;
1551ea45fe4eSShmulik Ravid 
1552ea45fe4eSShmulik Ravid 		ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
15537f891cf1SShmulik Ravid 		if (!ret)
15547be99413SThomas Graf 			ret = nla_put_u8(skb, i, value);
1555ea45fe4eSShmulik Ravid 
1556ea45fe4eSShmulik Ravid 		if (ret) {
15577be99413SThomas Graf 			nla_nest_cancel(skb, nest);
15587f891cf1SShmulik Ravid 			goto nla_put_failure;
1559ea45fe4eSShmulik Ravid 		}
1560ea45fe4eSShmulik Ravid 	}
15617be99413SThomas Graf 	nla_nest_end(skb, nest);
1562ea45fe4eSShmulik Ravid 
15637f891cf1SShmulik Ravid nla_put_failure:
1564ea45fe4eSShmulik Ravid 	return ret;
1565ea45fe4eSShmulik Ravid }
1566ea45fe4eSShmulik Ravid 
15677be99413SThomas Graf static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
15687be99413SThomas Graf 			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
1569ea45fe4eSShmulik Ravid {
1570ea45fe4eSShmulik Ravid 	struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
15717f891cf1SShmulik Ravid 	int ret, i;
1572ea45fe4eSShmulik Ravid 	u8 value;
1573ea45fe4eSShmulik Ravid 
15747f891cf1SShmulik Ravid 	if (!netdev->dcbnl_ops->setfeatcfg)
15757f891cf1SShmulik Ravid 		return -ENOTSUPP;
15767f891cf1SShmulik Ravid 
15777f891cf1SShmulik Ravid 	if (!tb[DCB_ATTR_FEATCFG])
15787f891cf1SShmulik Ravid 		return -EINVAL;
1579ea45fe4eSShmulik Ravid 
1580ea45fe4eSShmulik Ravid 	ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
1581ea45fe4eSShmulik Ravid 			       dcbnl_featcfg_nest);
1582ea45fe4eSShmulik Ravid 
15837f891cf1SShmulik Ravid 	if (ret)
1584ea45fe4eSShmulik Ravid 		goto err;
1585ea45fe4eSShmulik Ravid 
1586ea45fe4eSShmulik Ravid 	for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1587ea45fe4eSShmulik Ravid 		if (data[i] == NULL)
1588ea45fe4eSShmulik Ravid 			continue;
1589ea45fe4eSShmulik Ravid 
1590ea45fe4eSShmulik Ravid 		value = nla_get_u8(data[i]);
1591ea45fe4eSShmulik Ravid 
1592ea45fe4eSShmulik Ravid 		ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
1593ea45fe4eSShmulik Ravid 
1594ea45fe4eSShmulik Ravid 		if (ret)
15957f891cf1SShmulik Ravid 			goto err;
1596ea45fe4eSShmulik Ravid 	}
1597ea45fe4eSShmulik Ravid err:
15987be99413SThomas Graf 	ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
15997f891cf1SShmulik Ravid 
1600ea45fe4eSShmulik Ravid 	return ret;
1601ea45fe4eSShmulik Ravid }
1602ea45fe4eSShmulik Ravid 
1603dc6ed1dfSShmulik Ravid /* Handle CEE DCBX GET commands. */
16047be99413SThomas Graf static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
16057be99413SThomas Graf 			 u32 seq, struct nlattr **tb, struct sk_buff *skb)
1606dc6ed1dfSShmulik Ravid {
1607dc6ed1dfSShmulik Ravid 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1608dc6ed1dfSShmulik Ravid 
1609dc6ed1dfSShmulik Ravid 	if (!ops)
1610dc6ed1dfSShmulik Ravid 		return -EOPNOTSUPP;
1611dc6ed1dfSShmulik Ravid 
16127be99413SThomas Graf 	return dcbnl_cee_fill(skb, netdev);
1613dc6ed1dfSShmulik Ravid }
1614dc6ed1dfSShmulik Ravid 
161533a03aadSThomas Graf struct reply_func {
161633a03aadSThomas Graf 	/* reply netlink message type */
161733a03aadSThomas Graf 	int	type;
161833a03aadSThomas Graf 
161933a03aadSThomas Graf 	/* function to fill message contents */
162033a03aadSThomas Graf 	int   (*cb)(struct net_device *, struct nlmsghdr *, u32,
162133a03aadSThomas Graf 		    struct nlattr **, struct sk_buff *);
162233a03aadSThomas Graf };
162333a03aadSThomas Graf 
162433a03aadSThomas Graf static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
16257be99413SThomas Graf 	[DCB_CMD_GSTATE]	= { RTM_GETDCB, dcbnl_getstate },
16267be99413SThomas Graf 	[DCB_CMD_SSTATE]	= { RTM_SETDCB, dcbnl_setstate },
16277be99413SThomas Graf 	[DCB_CMD_PFC_GCFG]	= { RTM_GETDCB, dcbnl_getpfccfg },
16287be99413SThomas Graf 	[DCB_CMD_PFC_SCFG]	= { RTM_SETDCB, dcbnl_setpfccfg },
16297be99413SThomas Graf 	[DCB_CMD_GPERM_HWADDR]	= { RTM_GETDCB, dcbnl_getperm_hwaddr },
16307be99413SThomas Graf 	[DCB_CMD_GCAP]		= { RTM_GETDCB, dcbnl_getcap },
16317be99413SThomas Graf 	[DCB_CMD_GNUMTCS]	= { RTM_GETDCB, dcbnl_getnumtcs },
16327be99413SThomas Graf 	[DCB_CMD_SNUMTCS]	= { RTM_SETDCB, dcbnl_setnumtcs },
16337be99413SThomas Graf 	[DCB_CMD_PFC_GSTATE]	= { RTM_GETDCB, dcbnl_getpfcstate },
16347be99413SThomas Graf 	[DCB_CMD_PFC_SSTATE]	= { RTM_SETDCB, dcbnl_setpfcstate },
16357be99413SThomas Graf 	[DCB_CMD_GAPP]		= { RTM_GETDCB, dcbnl_getapp },
16367be99413SThomas Graf 	[DCB_CMD_SAPP]		= { RTM_SETDCB, dcbnl_setapp },
16377be99413SThomas Graf 	[DCB_CMD_PGTX_GCFG]	= { RTM_GETDCB, dcbnl_pgtx_getcfg },
16387be99413SThomas Graf 	[DCB_CMD_PGTX_SCFG]	= { RTM_SETDCB, dcbnl_pgtx_setcfg },
16397be99413SThomas Graf 	[DCB_CMD_PGRX_GCFG]	= { RTM_GETDCB, dcbnl_pgrx_getcfg },
16407be99413SThomas Graf 	[DCB_CMD_PGRX_SCFG]	= { RTM_SETDCB, dcbnl_pgrx_setcfg },
16417be99413SThomas Graf 	[DCB_CMD_SET_ALL]	= { RTM_SETDCB, dcbnl_setall },
16427be99413SThomas Graf 	[DCB_CMD_BCN_GCFG]	= { RTM_GETDCB, dcbnl_bcn_getcfg },
16437be99413SThomas Graf 	[DCB_CMD_BCN_SCFG]	= { RTM_SETDCB, dcbnl_bcn_setcfg },
16447be99413SThomas Graf 	[DCB_CMD_IEEE_GET]	= { RTM_GETDCB, dcbnl_ieee_get },
16457be99413SThomas Graf 	[DCB_CMD_IEEE_SET]	= { RTM_SETDCB, dcbnl_ieee_set },
16467be99413SThomas Graf 	[DCB_CMD_IEEE_DEL]	= { RTM_SETDCB, dcbnl_ieee_del },
16477be99413SThomas Graf 	[DCB_CMD_GDCBX]		= { RTM_GETDCB, dcbnl_getdcbx },
16487be99413SThomas Graf 	[DCB_CMD_SDCBX]		= { RTM_SETDCB, dcbnl_setdcbx },
16497be99413SThomas Graf 	[DCB_CMD_GFEATCFG]	= { RTM_GETDCB, dcbnl_getfeatcfg },
16507be99413SThomas Graf 	[DCB_CMD_SFEATCFG]	= { RTM_SETDCB, dcbnl_setfeatcfg },
16517be99413SThomas Graf 	[DCB_CMD_CEE_GET]	= { RTM_GETDCB, dcbnl_cee_get },
165233a03aadSThomas Graf };
165333a03aadSThomas Graf 
16542f90b865SAlexander Duyck static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
16552f90b865SAlexander Duyck {
16562f90b865SAlexander Duyck 	struct net *net = sock_net(skb->sk);
16572f90b865SAlexander Duyck 	struct net_device *netdev;
16587a282bc3SThomas Graf 	struct dcbmsg *dcb = nlmsg_data(nlh);
16592f90b865SAlexander Duyck 	struct nlattr *tb[DCB_ATTR_MAX + 1];
16602f90b865SAlexander Duyck 	u32 pid = skb ? NETLINK_CB(skb).pid : 0;
16612f90b865SAlexander Duyck 	int ret = -EINVAL;
166233a03aadSThomas Graf 	struct sk_buff *reply_skb;
166339912f9cSThomas Graf 	struct nlmsghdr *reply_nlh = NULL;
166433a03aadSThomas Graf 	const struct reply_func *fn;
16652f90b865SAlexander Duyck 
166609ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
16672f90b865SAlexander Duyck 		return -EINVAL;
16682f90b865SAlexander Duyck 
16692f90b865SAlexander Duyck 	ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
16702f90b865SAlexander Duyck 			  dcbnl_rtnl_policy);
16712f90b865SAlexander Duyck 	if (ret < 0)
16722f90b865SAlexander Duyck 		return ret;
16732f90b865SAlexander Duyck 
167433a03aadSThomas Graf 	if (dcb->cmd > DCB_CMD_MAX)
167533a03aadSThomas Graf 		return -EINVAL;
167633a03aadSThomas Graf 
167733a03aadSThomas Graf 	/* check if a reply function has been defined for the command */
167833a03aadSThomas Graf 	fn = &reply_funcs[dcb->cmd];
167933a03aadSThomas Graf 	if (!fn->cb)
168033a03aadSThomas Graf 		return -EOPNOTSUPP;
168133a03aadSThomas Graf 
16822f90b865SAlexander Duyck 	if (!tb[DCB_ATTR_IFNAME])
16832f90b865SAlexander Duyck 		return -EINVAL;
16842f90b865SAlexander Duyck 
16852f90b865SAlexander Duyck 	netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
16862f90b865SAlexander Duyck 	if (!netdev)
16873d1f4869SThomas Graf 		return -ENODEV;
16882f90b865SAlexander Duyck 
16893d1f4869SThomas Graf 	if (!netdev->dcbnl_ops) {
16903d1f4869SThomas Graf 		ret = -EOPNOTSUPP;
16913d1f4869SThomas Graf 		goto out;
16923d1f4869SThomas Graf 	}
16932f90b865SAlexander Duyck 
169433a03aadSThomas Graf 	reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, pid, nlh->nlmsg_seq,
169533a03aadSThomas Graf 				 nlh->nlmsg_flags, &reply_nlh);
169633a03aadSThomas Graf 	if (!reply_skb) {
169733a03aadSThomas Graf 		ret = -ENOBUFS;
169833a03aadSThomas Graf 		goto out;
169933a03aadSThomas Graf 	}
170033a03aadSThomas Graf 
170133a03aadSThomas Graf 	ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
170233a03aadSThomas Graf 	if (ret < 0) {
170333a03aadSThomas Graf 		nlmsg_free(reply_skb);
170433a03aadSThomas Graf 		goto out;
170533a03aadSThomas Graf 	}
170633a03aadSThomas Graf 
170733a03aadSThomas Graf 	nlmsg_end(reply_skb, reply_nlh);
170833a03aadSThomas Graf 
170933a03aadSThomas Graf 	ret = rtnl_unicast(reply_skb, &init_net, pid);
17102f90b865SAlexander Duyck out:
17112f90b865SAlexander Duyck 	dev_put(netdev);
17122f90b865SAlexander Duyck 	return ret;
17132f90b865SAlexander Duyck }
17142f90b865SAlexander Duyck 
1715716b31abSThomas Graf static struct dcb_app_type *dcb_app_lookup(const struct dcb_app *app,
1716716b31abSThomas Graf 					   int ifindex, int prio)
1717716b31abSThomas Graf {
1718716b31abSThomas Graf 	struct dcb_app_type *itr;
1719716b31abSThomas Graf 
1720716b31abSThomas Graf 	list_for_each_entry(itr, &dcb_app_list, list) {
1721716b31abSThomas Graf 		if (itr->app.selector == app->selector &&
1722716b31abSThomas Graf 		    itr->app.protocol == app->protocol &&
1723716b31abSThomas Graf 		    itr->ifindex == ifindex &&
1724716b31abSThomas Graf 		    (!prio || itr->app.priority == prio))
1725716b31abSThomas Graf 			return itr;
1726716b31abSThomas Graf 	}
1727716b31abSThomas Graf 
1728716b31abSThomas Graf 	return NULL;
1729716b31abSThomas Graf }
1730716b31abSThomas Graf 
17314e4f2f69SThomas Graf static int dcb_app_add(const struct dcb_app *app, int ifindex)
17324e4f2f69SThomas Graf {
17334e4f2f69SThomas Graf 	struct dcb_app_type *entry;
17344e4f2f69SThomas Graf 
17354e4f2f69SThomas Graf 	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
17364e4f2f69SThomas Graf 	if (!entry)
17374e4f2f69SThomas Graf 		return -ENOMEM;
17384e4f2f69SThomas Graf 
17394e4f2f69SThomas Graf 	memcpy(&entry->app, app, sizeof(*app));
17404e4f2f69SThomas Graf 	entry->ifindex = ifindex;
17414e4f2f69SThomas Graf 	list_add(&entry->list, &dcb_app_list);
17424e4f2f69SThomas Graf 
17434e4f2f69SThomas Graf 	return 0;
17444e4f2f69SThomas Graf }
17454e4f2f69SThomas Graf 
17469ab933abSJohn Fastabend /**
17479ab933abSJohn Fastabend  * dcb_getapp - retrieve the DCBX application user priority
17489ab933abSJohn Fastabend  *
17499ab933abSJohn Fastabend  * On success returns a non-zero 802.1p user priority bitmap
17509ab933abSJohn Fastabend  * otherwise returns 0 as the invalid user priority bitmap to
17519ab933abSJohn Fastabend  * indicate an error.
17529ab933abSJohn Fastabend  */
17539ab933abSJohn Fastabend u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
17549ab933abSJohn Fastabend {
17559ab933abSJohn Fastabend 	struct dcb_app_type *itr;
17569ab933abSJohn Fastabend 	u8 prio = 0;
17579ab933abSJohn Fastabend 
17589ab933abSJohn Fastabend 	spin_lock(&dcb_lock);
1759716b31abSThomas Graf 	if ((itr = dcb_app_lookup(app, dev->ifindex, 0)))
17609ab933abSJohn Fastabend 		prio = itr->app.priority;
17619ab933abSJohn Fastabend 	spin_unlock(&dcb_lock);
17629ab933abSJohn Fastabend 
17639ab933abSJohn Fastabend 	return prio;
17649ab933abSJohn Fastabend }
17659ab933abSJohn Fastabend EXPORT_SYMBOL(dcb_getapp);
17669ab933abSJohn Fastabend 
17679ab933abSJohn Fastabend /**
1768b6db2174SJohn Fastabend  * dcb_setapp - add CEE dcb application data to app list
17699ab933abSJohn Fastabend  *
1770b6db2174SJohn Fastabend  * Priority 0 is an invalid priority in CEE spec. This routine
1771b6db2174SJohn Fastabend  * removes applications from the app list if the priority is
1772b6db2174SJohn Fastabend  * set to zero.
17739ab933abSJohn Fastabend  */
1774ab6baf98SJohn Fastabend int dcb_setapp(struct net_device *dev, struct dcb_app *new)
17759ab933abSJohn Fastabend {
17769ab933abSJohn Fastabend 	struct dcb_app_type *itr;
17777ec79270SJohn Fastabend 	struct dcb_app_type event;
17784e4f2f69SThomas Graf 	int err = 0;
17797ec79270SJohn Fastabend 
1780e290ed81SMark Rustad 	event.ifindex = dev->ifindex;
17817ec79270SJohn Fastabend 	memcpy(&event.app, new, sizeof(event.app));
17826bd0e1cbSJohn Fastabend 	if (dev->dcbnl_ops->getdcbx)
17836bd0e1cbSJohn Fastabend 		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
17849ab933abSJohn Fastabend 
17859ab933abSJohn Fastabend 	spin_lock(&dcb_lock);
17869ab933abSJohn Fastabend 	/* Search for existing match and replace */
1787716b31abSThomas Graf 	if ((itr = dcb_app_lookup(new, dev->ifindex, 0))) {
17889ab933abSJohn Fastabend 		if (new->priority)
17899ab933abSJohn Fastabend 			itr->app.priority = new->priority;
17909ab933abSJohn Fastabend 		else {
17919ab933abSJohn Fastabend 			list_del(&itr->list);
17929ab933abSJohn Fastabend 			kfree(itr);
17939ab933abSJohn Fastabend 		}
17949ab933abSJohn Fastabend 		goto out;
17959ab933abSJohn Fastabend 	}
17969ab933abSJohn Fastabend 	/* App type does not exist add new application type */
17974e4f2f69SThomas Graf 	if (new->priority)
17984e4f2f69SThomas Graf 		err = dcb_app_add(new, dev->ifindex);
17999ab933abSJohn Fastabend out:
18009ab933abSJohn Fastabend 	spin_unlock(&dcb_lock);
18014e4f2f69SThomas Graf 	if (!err)
18027ec79270SJohn Fastabend 		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
18034e4f2f69SThomas Graf 	return err;
18049ab933abSJohn Fastabend }
18059ab933abSJohn Fastabend EXPORT_SYMBOL(dcb_setapp);
18069ab933abSJohn Fastabend 
1807b6db2174SJohn Fastabend /**
1808a364c8cfSJohn Fastabend  * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
1809a364c8cfSJohn Fastabend  *
1810a364c8cfSJohn Fastabend  * Helper routine which on success returns a non-zero 802.1Qaz user
1811a364c8cfSJohn Fastabend  * priority bitmap otherwise returns 0 to indicate the dcb_app was
1812a364c8cfSJohn Fastabend  * not found in APP list.
1813a364c8cfSJohn Fastabend  */
1814a364c8cfSJohn Fastabend u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
1815a364c8cfSJohn Fastabend {
1816a364c8cfSJohn Fastabend 	struct dcb_app_type *itr;
1817a364c8cfSJohn Fastabend 	u8 prio = 0;
1818a364c8cfSJohn Fastabend 
1819a364c8cfSJohn Fastabend 	spin_lock(&dcb_lock);
1820716b31abSThomas Graf 	if ((itr = dcb_app_lookup(app, dev->ifindex, 0)))
1821a364c8cfSJohn Fastabend 		prio |= 1 << itr->app.priority;
1822a364c8cfSJohn Fastabend 	spin_unlock(&dcb_lock);
1823a364c8cfSJohn Fastabend 
1824a364c8cfSJohn Fastabend 	return prio;
1825a364c8cfSJohn Fastabend }
1826a364c8cfSJohn Fastabend EXPORT_SYMBOL(dcb_ieee_getapp_mask);
1827a364c8cfSJohn Fastabend 
1828a364c8cfSJohn Fastabend /**
1829b6db2174SJohn Fastabend  * dcb_ieee_setapp - add IEEE dcb application data to app list
1830b6db2174SJohn Fastabend  *
1831b6db2174SJohn Fastabend  * This adds Application data to the list. Multiple application
1832b6db2174SJohn Fastabend  * entries may exists for the same selector and protocol as long
1833b6db2174SJohn Fastabend  * as the priorities are different.
1834b6db2174SJohn Fastabend  */
1835b6db2174SJohn Fastabend int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
1836b6db2174SJohn Fastabend {
1837b6db2174SJohn Fastabend 	struct dcb_app_type event;
1838b6db2174SJohn Fastabend 	int err = 0;
1839b6db2174SJohn Fastabend 
1840e290ed81SMark Rustad 	event.ifindex = dev->ifindex;
1841b6db2174SJohn Fastabend 	memcpy(&event.app, new, sizeof(event.app));
18426bd0e1cbSJohn Fastabend 	if (dev->dcbnl_ops->getdcbx)
18436bd0e1cbSJohn Fastabend 		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
1844b6db2174SJohn Fastabend 
1845b6db2174SJohn Fastabend 	spin_lock(&dcb_lock);
1846b6db2174SJohn Fastabend 	/* Search for existing match and abort if found */
1847716b31abSThomas Graf 	if (dcb_app_lookup(new, dev->ifindex, new->priority)) {
1848b6db2174SJohn Fastabend 		err = -EEXIST;
1849b6db2174SJohn Fastabend 		goto out;
1850b6db2174SJohn Fastabend 	}
1851b6db2174SJohn Fastabend 
18524e4f2f69SThomas Graf 	err = dcb_app_add(new, dev->ifindex);
1853b6db2174SJohn Fastabend out:
1854b6db2174SJohn Fastabend 	spin_unlock(&dcb_lock);
1855b6db2174SJohn Fastabend 	if (!err)
1856b6db2174SJohn Fastabend 		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
1857b6db2174SJohn Fastabend 	return err;
1858b6db2174SJohn Fastabend }
1859b6db2174SJohn Fastabend EXPORT_SYMBOL(dcb_ieee_setapp);
1860b6db2174SJohn Fastabend 
1861f9ae7e4bSJohn Fastabend /**
1862f9ae7e4bSJohn Fastabend  * dcb_ieee_delapp - delete IEEE dcb application data from list
1863f9ae7e4bSJohn Fastabend  *
1864f9ae7e4bSJohn Fastabend  * This removes a matching APP data from the APP list
1865f9ae7e4bSJohn Fastabend  */
1866f9ae7e4bSJohn Fastabend int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
1867f9ae7e4bSJohn Fastabend {
1868f9ae7e4bSJohn Fastabend 	struct dcb_app_type *itr;
1869f9ae7e4bSJohn Fastabend 	struct dcb_app_type event;
1870f9ae7e4bSJohn Fastabend 	int err = -ENOENT;
1871f9ae7e4bSJohn Fastabend 
1872e290ed81SMark Rustad 	event.ifindex = dev->ifindex;
1873f9ae7e4bSJohn Fastabend 	memcpy(&event.app, del, sizeof(event.app));
18746bd0e1cbSJohn Fastabend 	if (dev->dcbnl_ops->getdcbx)
18756bd0e1cbSJohn Fastabend 		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
1876f9ae7e4bSJohn Fastabend 
1877f9ae7e4bSJohn Fastabend 	spin_lock(&dcb_lock);
1878f9ae7e4bSJohn Fastabend 	/* Search for existing match and remove it. */
1879716b31abSThomas Graf 	if ((itr = dcb_app_lookup(del, dev->ifindex, del->priority))) {
1880f9ae7e4bSJohn Fastabend 		list_del(&itr->list);
1881f9ae7e4bSJohn Fastabend 		kfree(itr);
1882f9ae7e4bSJohn Fastabend 		err = 0;
1883f9ae7e4bSJohn Fastabend 	}
1884f9ae7e4bSJohn Fastabend 
1885f9ae7e4bSJohn Fastabend 	spin_unlock(&dcb_lock);
1886f9ae7e4bSJohn Fastabend 	if (!err)
1887f9ae7e4bSJohn Fastabend 		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
1888f9ae7e4bSJohn Fastabend 	return err;
1889f9ae7e4bSJohn Fastabend }
1890f9ae7e4bSJohn Fastabend EXPORT_SYMBOL(dcb_ieee_delapp);
1891f9ae7e4bSJohn Fastabend 
18927c14c3f1SShmulik Ravid static void dcb_flushapp(void)
18939ab933abSJohn Fastabend {
18949ab933abSJohn Fastabend 	struct dcb_app_type *app;
18952a8fe003SDan Carpenter 	struct dcb_app_type *tmp;
18969ab933abSJohn Fastabend 
18979ab933abSJohn Fastabend 	spin_lock(&dcb_lock);
18982a8fe003SDan Carpenter 	list_for_each_entry_safe(app, tmp, &dcb_app_list, list) {
18999ab933abSJohn Fastabend 		list_del(&app->list);
19009ab933abSJohn Fastabend 		kfree(app);
19019ab933abSJohn Fastabend 	}
19029ab933abSJohn Fastabend 	spin_unlock(&dcb_lock);
19039ab933abSJohn Fastabend }
19049ab933abSJohn Fastabend 
19052f90b865SAlexander Duyck static int __init dcbnl_init(void)
19062f90b865SAlexander Duyck {
19079ab933abSJohn Fastabend 	INIT_LIST_HEAD(&dcb_app_list);
19089ab933abSJohn Fastabend 
1909c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, NULL);
1910c7ac8679SGreg Rose 	rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, NULL);
19112f90b865SAlexander Duyck 
19122f90b865SAlexander Duyck 	return 0;
19132f90b865SAlexander Duyck }
19142f90b865SAlexander Duyck module_init(dcbnl_init);
19152f90b865SAlexander Duyck 
19162f90b865SAlexander Duyck static void __exit dcbnl_exit(void)
19172f90b865SAlexander Duyck {
19182f90b865SAlexander Duyck 	rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
19192f90b865SAlexander Duyck 	rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
19209ab933abSJohn Fastabend 	dcb_flushapp();
19212f90b865SAlexander Duyck }
19222f90b865SAlexander Duyck module_exit(dcbnl_exit);
1923