1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 2 3 #include "netlink.h" 3 #include "netlink.h" 4 #include "common.h" 4 #include "common.h" 5 5 6 struct pause_req_info { 6 struct pause_req_info { 7 struct ethnl_req_info base; 7 struct ethnl_req_info base; 8 enum ethtool_mac_stats_src src; << 9 }; 8 }; 10 9 11 #define PAUSE_REQINFO(__req_base) \ << 12 container_of(__req_base, struct pause_ << 13 << 14 struct pause_reply_data { 10 struct pause_reply_data { 15 struct ethnl_reply_data base; 11 struct ethnl_reply_data base; 16 struct ethtool_pauseparam pausep 12 struct ethtool_pauseparam pauseparam; 17 struct ethtool_pause_stats pauses 13 struct ethtool_pause_stats pausestat; 18 }; 14 }; 19 15 20 #define PAUSE_REPDATA(__reply_base) \ 16 #define PAUSE_REPDATA(__reply_base) \ 21 container_of(__reply_base, struct paus 17 container_of(__reply_base, struct pause_reply_data, base) 22 18 23 const struct nla_policy ethnl_pause_get_policy 19 const struct nla_policy ethnl_pause_get_policy[] = { 24 [ETHTOOL_A_PAUSE_HEADER] 20 [ETHTOOL_A_PAUSE_HEADER] = 25 NLA_POLICY_NESTED(ethnl_header 21 NLA_POLICY_NESTED(ethnl_header_policy_stats), 26 [ETHTOOL_A_PAUSE_STATS_SRC] << 27 NLA_POLICY_MAX(NLA_U32, ETHTOO << 28 }; 22 }; 29 23 30 static int pause_parse_request(struct ethnl_re !! 24 static void ethtool_stats_init(u64 *stats, unsigned int n) 31 struct nlattr * !! 25 { 32 struct netlink_ !! 26 while (n--) 33 { !! 27 stats[n] = ETHTOOL_STAT_NOT_SET; 34 enum ethtool_mac_stats_src src = ETHTO << 35 struct pause_req_info *req_info = PAUS << 36 << 37 if (tb[ETHTOOL_A_PAUSE_STATS_SRC]) { << 38 if (!(req_base->flags & ETHTOO << 39 NL_SET_ERR_MSG_MOD(ext << 40 "ET << 41 return -EINVAL; << 42 } << 43 << 44 src = nla_get_u32(tb[ETHTOOL_A << 45 } << 46 << 47 req_info->src = src; << 48 << 49 return 0; << 50 } 28 } 51 29 52 static int pause_prepare_data(const struct eth 30 static int pause_prepare_data(const struct ethnl_req_info *req_base, 53 struct ethnl_rep 31 struct ethnl_reply_data *reply_base, 54 const struct gen !! 32 struct genl_info *info) 55 { 33 { 56 const struct pause_req_info *req_info << 57 struct pause_reply_data *data = PAUSE_ 34 struct pause_reply_data *data = PAUSE_REPDATA(reply_base); 58 enum ethtool_mac_stats_src src = req_i << 59 struct net_device *dev = reply_base->d 35 struct net_device *dev = reply_base->dev; 60 int ret; 36 int ret; 61 37 62 if (!dev->ethtool_ops->get_pauseparam) 38 if (!dev->ethtool_ops->get_pauseparam) 63 return -EOPNOTSUPP; 39 return -EOPNOTSUPP; 64 40 65 ethtool_stats_init((u64 *)&data->pause 41 ethtool_stats_init((u64 *)&data->pausestat, 66 sizeof(data->pauses 42 sizeof(data->pausestat) / 8); 67 data->pausestat.src = src; << 68 43 69 ret = ethnl_ops_begin(dev); 44 ret = ethnl_ops_begin(dev); 70 if (ret < 0) 45 if (ret < 0) 71 return ret; 46 return ret; 72 << 73 if ((src == ETHTOOL_MAC_STATS_SRC_EMAC << 74 src == ETHTOOL_MAC_STATS_SRC_PMAC << 75 !__ethtool_dev_mm_supported(dev)) << 76 NL_SET_ERR_MSG_MOD(info->extac << 77 "Device doe << 78 ethnl_ops_complete(dev); << 79 return -EOPNOTSUPP; << 80 } << 81 << 82 dev->ethtool_ops->get_pauseparam(dev, 47 dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam); 83 if (req_base->flags & ETHTOOL_FLAG_STA 48 if (req_base->flags & ETHTOOL_FLAG_STATS && 84 dev->ethtool_ops->get_pause_stats) 49 dev->ethtool_ops->get_pause_stats) 85 dev->ethtool_ops->get_pause_st 50 dev->ethtool_ops->get_pause_stats(dev, &data->pausestat); 86 << 87 ethnl_ops_complete(dev); 51 ethnl_ops_complete(dev); 88 52 89 return 0; 53 return 0; 90 } 54 } 91 55 92 static int pause_reply_size(const struct ethnl 56 static int pause_reply_size(const struct ethnl_req_info *req_base, 93 const struct ethnl 57 const struct ethnl_reply_data *reply_base) 94 { 58 { 95 int n = nla_total_size(sizeof(u8)) + 59 int n = nla_total_size(sizeof(u8)) + /* _PAUSE_AUTONEG */ 96 nla_total_size(sizeof(u8)) + 60 nla_total_size(sizeof(u8)) + /* _PAUSE_RX */ 97 nla_total_size(sizeof(u8)); 61 nla_total_size(sizeof(u8)); /* _PAUSE_TX */ 98 62 99 if (req_base->flags & ETHTOOL_FLAG_STA 63 if (req_base->flags & ETHTOOL_FLAG_STATS) 100 n += nla_total_size(0) + 64 n += nla_total_size(0) + /* _PAUSE_STATS */ 101 nla_total_size(sizeof(u32 << 102 nla_total_size_64bit(size 65 nla_total_size_64bit(sizeof(u64)) * ETHTOOL_PAUSE_STAT_CNT; 103 return n; 66 return n; 104 } 67 } 105 68 106 static int ethtool_put_stat(struct sk_buff *sk 69 static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype, 107 u16 padtype) 70 u16 padtype) 108 { 71 { 109 if (val == ETHTOOL_STAT_NOT_SET) 72 if (val == ETHTOOL_STAT_NOT_SET) 110 return 0; 73 return 0; 111 if (nla_put_u64_64bit(skb, attrtype, v 74 if (nla_put_u64_64bit(skb, attrtype, val, padtype)) 112 return -EMSGSIZE; 75 return -EMSGSIZE; 113 76 114 return 0; 77 return 0; 115 } 78 } 116 79 117 static int pause_put_stats(struct sk_buff *skb 80 static int pause_put_stats(struct sk_buff *skb, 118 const struct ethtoo 81 const struct ethtool_pause_stats *pause_stats) 119 { 82 { 120 const u16 pad = ETHTOOL_A_PAUSE_STAT_P 83 const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD; 121 struct nlattr *nest; 84 struct nlattr *nest; 122 85 123 if (nla_put_u32(skb, ETHTOOL_A_PAUSE_S << 124 return -EMSGSIZE; << 125 << 126 nest = nla_nest_start(skb, ETHTOOL_A_P 86 nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS); 127 if (!nest) 87 if (!nest) 128 return -EMSGSIZE; 88 return -EMSGSIZE; 129 89 130 if (ethtool_put_stat(skb, pause_stats- 90 if (ethtool_put_stat(skb, pause_stats->tx_pause_frames, 131 ETHTOOL_A_PAUSE_S 91 ETHTOOL_A_PAUSE_STAT_TX_FRAMES, pad) || 132 ethtool_put_stat(skb, pause_stats- 92 ethtool_put_stat(skb, pause_stats->rx_pause_frames, 133 ETHTOOL_A_PAUSE_S 93 ETHTOOL_A_PAUSE_STAT_RX_FRAMES, pad)) 134 goto err_cancel; 94 goto err_cancel; 135 95 136 nla_nest_end(skb, nest); 96 nla_nest_end(skb, nest); 137 return 0; 97 return 0; 138 98 139 err_cancel: 99 err_cancel: 140 nla_nest_cancel(skb, nest); 100 nla_nest_cancel(skb, nest); 141 return -EMSGSIZE; 101 return -EMSGSIZE; 142 } 102 } 143 103 144 static int pause_fill_reply(struct sk_buff *sk 104 static int pause_fill_reply(struct sk_buff *skb, 145 const struct ethnl 105 const struct ethnl_req_info *req_base, 146 const struct ethnl 106 const struct ethnl_reply_data *reply_base) 147 { 107 { 148 const struct pause_reply_data *data = 108 const struct pause_reply_data *data = PAUSE_REPDATA(reply_base); 149 const struct ethtool_pauseparam *pause 109 const struct ethtool_pauseparam *pauseparam = &data->pauseparam; 150 110 151 if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AU 111 if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) || 152 nla_put_u8(skb, ETHTOOL_A_PAUSE_RX 112 nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) || 153 nla_put_u8(skb, ETHTOOL_A_PAUSE_TX 113 nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause)) 154 return -EMSGSIZE; 114 return -EMSGSIZE; 155 115 156 if (req_base->flags & ETHTOOL_FLAG_STA 116 if (req_base->flags & ETHTOOL_FLAG_STATS && 157 pause_put_stats(skb, &data->pauses 117 pause_put_stats(skb, &data->pausestat)) 158 return -EMSGSIZE; 118 return -EMSGSIZE; 159 119 160 return 0; 120 return 0; 161 } 121 } 162 122 >> 123 const struct ethnl_request_ops ethnl_pause_request_ops = { >> 124 .request_cmd = ETHTOOL_MSG_PAUSE_GET, >> 125 .reply_cmd = ETHTOOL_MSG_PAUSE_GET_REPLY, >> 126 .hdr_attr = ETHTOOL_A_PAUSE_HEADER, >> 127 .req_info_size = sizeof(struct pause_req_info), >> 128 .reply_data_size = sizeof(struct pause_reply_data), >> 129 >> 130 .prepare_data = pause_prepare_data, >> 131 .reply_size = pause_reply_size, >> 132 .fill_reply = pause_fill_reply, >> 133 }; >> 134 163 /* PAUSE_SET */ 135 /* PAUSE_SET */ 164 136 165 const struct nla_policy ethnl_pause_set_policy 137 const struct nla_policy ethnl_pause_set_policy[] = { 166 [ETHTOOL_A_PAUSE_HEADER] 138 [ETHTOOL_A_PAUSE_HEADER] = 167 NLA_POLICY_NESTED(ethnl_header 139 NLA_POLICY_NESTED(ethnl_header_policy), 168 [ETHTOOL_A_PAUSE_AUTONEG] 140 [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 }, 169 [ETHTOOL_A_PAUSE_RX] 141 [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 }, 170 [ETHTOOL_A_PAUSE_TX] 142 [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 }, 171 }; 143 }; 172 144 173 static int !! 145 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info) 174 ethnl_set_pause_validate(struct ethnl_req_info << 175 struct genl_info *inf << 176 { << 177 const struct ethtool_ops *ops = req_in << 178 << 179 return ops->get_pauseparam && ops->set << 180 } << 181 << 182 static int << 183 ethnl_set_pause(struct ethnl_req_info *req_inf << 184 { 146 { 185 struct net_device *dev = req_info->dev << 186 struct ethtool_pauseparam params = {}; 147 struct ethtool_pauseparam params = {}; >> 148 struct ethnl_req_info req_info = {}; 187 struct nlattr **tb = info->attrs; 149 struct nlattr **tb = info->attrs; >> 150 const struct ethtool_ops *ops; >> 151 struct net_device *dev; 188 bool mod = false; 152 bool mod = false; 189 int ret; 153 int ret; 190 154 191 dev->ethtool_ops->get_pauseparam(dev, !! 155 ret = ethnl_parse_header_dev_get(&req_info, >> 156 tb[ETHTOOL_A_PAUSE_HEADER], >> 157 genl_info_net(info), info->extack, >> 158 true); >> 159 if (ret < 0) >> 160 return ret; >> 161 dev = req_info.dev; >> 162 ops = dev->ethtool_ops; >> 163 ret = -EOPNOTSUPP; >> 164 if (!ops->get_pauseparam || !ops->set_pauseparam) >> 165 goto out_dev; >> 166 >> 167 rtnl_lock(); >> 168 ret = ethnl_ops_begin(dev); >> 169 if (ret < 0) >> 170 goto out_rtnl; >> 171 ops->get_pauseparam(dev, ¶ms); 192 172 193 ethnl_update_bool32(¶ms.autoneg, t 173 ethnl_update_bool32(¶ms.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod); 194 ethnl_update_bool32(¶ms.rx_pause, 174 ethnl_update_bool32(¶ms.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod); 195 ethnl_update_bool32(¶ms.tx_pause, 175 ethnl_update_bool32(¶ms.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod); >> 176 ret = 0; 196 if (!mod) 177 if (!mod) 197 return 0; !! 178 goto out_ops; 198 179 199 ret = dev->ethtool_ops->set_pauseparam 180 ret = dev->ethtool_ops->set_pauseparam(dev, ¶ms); 200 return ret < 0 ? ret : 1; !! 181 if (ret < 0) 201 } !! 182 goto out_ops; 202 !! 183 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL); 203 const struct ethnl_request_ops ethnl_pause_req << 204 .request_cmd = ETHTOOL_MSG_ << 205 .reply_cmd = ETHTOOL_MSG_ << 206 .hdr_attr = ETHTOOL_A_PA << 207 .req_info_size = sizeof(struc << 208 .reply_data_size = sizeof(struc << 209 << 210 .parse_request = pause_parse_ << 211 .prepare_data = pause_prepar << 212 .reply_size = pause_reply_ << 213 .fill_reply = pause_fill_r << 214 184 215 .set_validate = ethnl_set_pa !! 185 out_ops: 216 .set = ethnl_set_pa !! 186 ethnl_ops_complete(dev); 217 .set_ntf_cmd = ETHTOOL_MSG_ !! 187 out_rtnl: 218 }; !! 188 rtnl_unlock(); >> 189 out_dev: >> 190 dev_put(dev); >> 191 return ret; >> 192 } 219 193
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.