1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include "netlink.h" 4 #include "common.h" 5 6 struct pause_req_info { 7 struct ethnl_req_info base; 8 }; 9 10 struct pause_reply_data { 11 struct ethnl_reply_data base; 12 struct ethtool_pauseparam pauseparam; 13 struct ethtool_pause_stats pausestat; 14 }; 15 16 #define PAUSE_REPDATA(__reply_base) \ 17 container_of(__reply_base, struct pause_reply_data, base) 18 19 const struct nla_policy ethnl_pause_get_policy[] = { 20 [ETHTOOL_A_PAUSE_HEADER] = { .type = NLA_NESTED }, 21 }; 22 23 static void ethtool_stats_init(u64 *stats, unsigned int n) 24 { 25 while (n--) 26 stats[n] = ETHTOOL_STAT_NOT_SET; 27 } 28 29 static int pause_prepare_data(const struct ethnl_req_info *req_base, 30 struct ethnl_reply_data *reply_base, 31 struct genl_info *info) 32 { 33 struct pause_reply_data *data = PAUSE_REPDATA(reply_base); 34 struct net_device *dev = reply_base->dev; 35 int ret; 36 37 if (!dev->ethtool_ops->get_pauseparam) 38 return -EOPNOTSUPP; 39 40 ret = ethnl_ops_begin(dev); 41 if (ret < 0) 42 return ret; 43 dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam); 44 if (req_base->flags & ETHTOOL_FLAG_STATS && 45 dev->ethtool_ops->get_pause_stats) { 46 ethtool_stats_init((u64 *)&data->pausestat, 47 sizeof(data->pausestat) / 8); 48 dev->ethtool_ops->get_pause_stats(dev, &data->pausestat); 49 } 50 ethnl_ops_complete(dev); 51 52 return 0; 53 } 54 55 static int pause_reply_size(const struct ethnl_req_info *req_base, 56 const struct ethnl_reply_data *reply_base) 57 { 58 int n = nla_total_size(sizeof(u8)) + /* _PAUSE_AUTONEG */ 59 nla_total_size(sizeof(u8)) + /* _PAUSE_RX */ 60 nla_total_size(sizeof(u8)); /* _PAUSE_TX */ 61 62 if (req_base->flags & ETHTOOL_FLAG_STATS) 63 n += nla_total_size(0) + /* _PAUSE_STATS */ 64 nla_total_size_64bit(sizeof(u64)) * 65 (ETHTOOL_A_PAUSE_STAT_MAX - 2); 66 return n; 67 } 68 69 static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype, 70 u16 padtype) 71 { 72 if (val == ETHTOOL_STAT_NOT_SET) 73 return 0; 74 if (nla_put_u64_64bit(skb, attrtype, val, padtype)) 75 return -EMSGSIZE; 76 77 return 0; 78 } 79 80 static int pause_put_stats(struct sk_buff *skb, 81 const struct ethtool_pause_stats *pause_stats) 82 { 83 const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD; 84 struct nlattr *nest; 85 86 nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS); 87 if (!nest) 88 return -EMSGSIZE; 89 90 if (ethtool_put_stat(skb, pause_stats->tx_pause_frames, 91 ETHTOOL_A_PAUSE_STAT_TX_FRAMES, pad) || 92 ethtool_put_stat(skb, pause_stats->rx_pause_frames, 93 ETHTOOL_A_PAUSE_STAT_RX_FRAMES, pad)) 94 goto err_cancel; 95 96 nla_nest_end(skb, nest); 97 return 0; 98 99 err_cancel: 100 nla_nest_cancel(skb, nest); 101 return -EMSGSIZE; 102 } 103 104 static int pause_fill_reply(struct sk_buff *skb, 105 const struct ethnl_req_info *req_base, 106 const struct ethnl_reply_data *reply_base) 107 { 108 const struct pause_reply_data *data = PAUSE_REPDATA(reply_base); 109 const struct ethtool_pauseparam *pauseparam = &data->pauseparam; 110 111 if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) || 112 nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) || 113 nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause)) 114 return -EMSGSIZE; 115 116 if (req_base->flags & ETHTOOL_FLAG_STATS && 117 pause_put_stats(skb, &data->pausestat)) 118 return -EMSGSIZE; 119 120 return 0; 121 } 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 135 /* PAUSE_SET */ 136 137 const struct nla_policy ethnl_pause_set_policy[] = { 138 [ETHTOOL_A_PAUSE_HEADER] = { .type = NLA_NESTED }, 139 [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 }, 140 [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 }, 141 [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 }, 142 }; 143 144 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info) 145 { 146 struct ethtool_pauseparam params = {}; 147 struct ethnl_req_info req_info = {}; 148 struct nlattr **tb = info->attrs; 149 const struct ethtool_ops *ops; 150 struct net_device *dev; 151 bool mod = false; 152 int ret; 153 154 ret = ethnl_parse_header_dev_get(&req_info, 155 tb[ETHTOOL_A_PAUSE_HEADER], 156 genl_info_net(info), info->extack, 157 true); 158 if (ret < 0) 159 return ret; 160 dev = req_info.dev; 161 ops = dev->ethtool_ops; 162 ret = -EOPNOTSUPP; 163 if (!ops->get_pauseparam || !ops->set_pauseparam) 164 goto out_dev; 165 166 rtnl_lock(); 167 ret = ethnl_ops_begin(dev); 168 if (ret < 0) 169 goto out_rtnl; 170 ops->get_pauseparam(dev, ¶ms); 171 172 ethnl_update_bool32(¶ms.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod); 173 ethnl_update_bool32(¶ms.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod); 174 ethnl_update_bool32(¶ms.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod); 175 ret = 0; 176 if (!mod) 177 goto out_ops; 178 179 ret = dev->ethtool_ops->set_pauseparam(dev, ¶ms); 180 if (ret < 0) 181 goto out_ops; 182 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL); 183 184 out_ops: 185 ethnl_ops_complete(dev); 186 out_rtnl: 187 rtnl_unlock(); 188 out_dev: 189 dev_put(dev); 190 return ret; 191 } 192