1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include "netlink.h" 4 #include "common.h" 5 #include "bitset.h" 6 7 #define EEE_MODES_COUNT \ 8 (sizeof_field(struct ethtool_keee, supported_u32) * BITS_PER_BYTE) 9 10 struct eee_req_info { 11 struct ethnl_req_info base; 12 }; 13 14 struct eee_reply_data { 15 struct ethnl_reply_data base; 16 struct ethtool_keee eee; 17 }; 18 19 #define EEE_REPDATA(__reply_base) \ 20 container_of(__reply_base, struct eee_reply_data, base) 21 22 const struct nla_policy ethnl_eee_get_policy[] = { 23 [ETHTOOL_A_EEE_HEADER] = 24 NLA_POLICY_NESTED(ethnl_header_policy), 25 }; 26 27 static int eee_prepare_data(const struct ethnl_req_info *req_base, 28 struct ethnl_reply_data *reply_base, 29 const struct genl_info *info) 30 { 31 struct eee_reply_data *data = EEE_REPDATA(reply_base); 32 struct net_device *dev = reply_base->dev; 33 struct ethtool_keee *eee = &data->eee; 34 int ret; 35 36 if (!dev->ethtool_ops->get_eee) 37 return -EOPNOTSUPP; 38 ret = ethnl_ops_begin(dev); 39 if (ret < 0) 40 return ret; 41 ret = dev->ethtool_ops->get_eee(dev, eee); 42 ethnl_ops_complete(dev); 43 44 if (!ret && !ethtool_eee_use_linkmodes(eee)) { 45 ethtool_convert_legacy_u32_to_link_mode(eee->supported, 46 eee->supported_u32); 47 ethtool_convert_legacy_u32_to_link_mode(eee->advertised, 48 eee->advertised_u32); 49 ethtool_convert_legacy_u32_to_link_mode(eee->lp_advertised, 50 eee->lp_advertised_u32); 51 } 52 53 return ret; 54 } 55 56 static int eee_reply_size(const struct ethnl_req_info *req_base, 57 const struct ethnl_reply_data *reply_base) 58 { 59 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS; 60 const struct eee_reply_data *data = EEE_REPDATA(reply_base); 61 const struct ethtool_keee *eee = &data->eee; 62 int len = 0; 63 int ret; 64 65 BUILD_BUG_ON(sizeof(eee->advertised_u32) * BITS_PER_BYTE != 66 EEE_MODES_COUNT); 67 BUILD_BUG_ON(sizeof(eee->lp_advertised_u32) * BITS_PER_BYTE != 68 EEE_MODES_COUNT); 69 70 /* MODES_OURS */ 71 ret = ethnl_bitset_size(eee->advertised, eee->supported, 72 __ETHTOOL_LINK_MODE_MASK_NBITS, 73 link_mode_names, compact); 74 if (ret < 0) 75 return ret; 76 len += ret; 77 /* MODES_PEERS */ 78 ret = ethnl_bitset_size(eee->lp_advertised, NULL, 79 __ETHTOOL_LINK_MODE_MASK_NBITS, 80 link_mode_names, compact); 81 if (ret < 0) 82 return ret; 83 len += ret; 84 85 len += nla_total_size(sizeof(u8)) + /* _EEE_ACTIVE */ 86 nla_total_size(sizeof(u8)) + /* _EEE_ENABLED */ 87 nla_total_size(sizeof(u8)) + /* _EEE_TX_LPI_ENABLED */ 88 nla_total_size(sizeof(u32)); /* _EEE_TX_LPI_TIMER */ 89 90 return len; 91 } 92 93 static int eee_fill_reply(struct sk_buff *skb, 94 const struct ethnl_req_info *req_base, 95 const struct ethnl_reply_data *reply_base) 96 { 97 bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS; 98 const struct eee_reply_data *data = EEE_REPDATA(reply_base); 99 const struct ethtool_keee *eee = &data->eee; 100 int ret; 101 102 ret = ethnl_put_bitset(skb, ETHTOOL_A_EEE_MODES_OURS, 103 eee->advertised, eee->supported, 104 __ETHTOOL_LINK_MODE_MASK_NBITS, 105 link_mode_names, compact); 106 if (ret < 0) 107 return ret; 108 ret = ethnl_put_bitset(skb, ETHTOOL_A_EEE_MODES_PEER, 109 eee->lp_advertised, NULL, 110 __ETHTOOL_LINK_MODE_MASK_NBITS, 111 link_mode_names, compact); 112 if (ret < 0) 113 return ret; 114 115 if (nla_put_u8(skb, ETHTOOL_A_EEE_ACTIVE, eee->eee_active) || 116 nla_put_u8(skb, ETHTOOL_A_EEE_ENABLED, eee->eee_enabled) || 117 nla_put_u8(skb, ETHTOOL_A_EEE_TX_LPI_ENABLED, 118 eee->tx_lpi_enabled) || 119 nla_put_u32(skb, ETHTOOL_A_EEE_TX_LPI_TIMER, eee->tx_lpi_timer)) 120 return -EMSGSIZE; 121 122 return 0; 123 } 124 125 /* EEE_SET */ 126 127 const struct nla_policy ethnl_eee_set_policy[] = { 128 [ETHTOOL_A_EEE_HEADER] = 129 NLA_POLICY_NESTED(ethnl_header_policy), 130 [ETHTOOL_A_EEE_MODES_OURS] = { .type = NLA_NESTED }, 131 [ETHTOOL_A_EEE_ENABLED] = { .type = NLA_U8 }, 132 [ETHTOOL_A_EEE_TX_LPI_ENABLED] = { .type = NLA_U8 }, 133 [ETHTOOL_A_EEE_TX_LPI_TIMER] = { .type = NLA_U32 }, 134 }; 135 136 static int 137 ethnl_set_eee_validate(struct ethnl_req_info *req_info, struct genl_info *info) 138 { 139 const struct ethtool_ops *ops = req_info->dev->ethtool_ops; 140 141 return ops->get_eee && ops->set_eee ? 1 : -EOPNOTSUPP; 142 } 143 144 static int 145 ethnl_set_eee(struct ethnl_req_info *req_info, struct genl_info *info) 146 { 147 struct net_device *dev = req_info->dev; 148 struct nlattr **tb = info->attrs; 149 struct ethtool_keee eee = {}; 150 bool mod = false; 151 int ret; 152 153 ret = dev->ethtool_ops->get_eee(dev, &eee); 154 if (ret < 0) 155 return ret; 156 157 if (ethtool_eee_use_linkmodes(&eee)) { 158 ret = ethnl_update_bitset(eee.advertised, 159 __ETHTOOL_LINK_MODE_MASK_NBITS, 160 tb[ETHTOOL_A_EEE_MODES_OURS], 161 link_mode_names, info->extack, &mod); 162 } else { 163 ret = ethnl_update_bitset32(&eee.advertised_u32, EEE_MODES_COUNT, 164 tb[ETHTOOL_A_EEE_MODES_OURS], 165 link_mode_names, info->extack, &mod); 166 } 167 if (ret < 0) 168 return ret; 169 ethnl_update_bool(&eee.eee_enabled, tb[ETHTOOL_A_EEE_ENABLED], &mod); 170 ethnl_update_bool(&eee.tx_lpi_enabled, tb[ETHTOOL_A_EEE_TX_LPI_ENABLED], 171 &mod); 172 ethnl_update_u32(&eee.tx_lpi_timer, tb[ETHTOOL_A_EEE_TX_LPI_TIMER], 173 &mod); 174 if (!mod) 175 return 0; 176 177 ret = dev->ethtool_ops->set_eee(dev, &eee); 178 return ret < 0 ? ret : 1; 179 } 180 181 const struct ethnl_request_ops ethnl_eee_request_ops = { 182 .request_cmd = ETHTOOL_MSG_EEE_GET, 183 .reply_cmd = ETHTOOL_MSG_EEE_GET_REPLY, 184 .hdr_attr = ETHTOOL_A_EEE_HEADER, 185 .req_info_size = sizeof(struct eee_req_info), 186 .reply_data_size = sizeof(struct eee_reply_data), 187 188 .prepare_data = eee_prepare_data, 189 .reply_size = eee_reply_size, 190 .fill_reply = eee_fill_reply, 191 192 .set_validate = ethnl_set_eee_validate, 193 .set = ethnl_set_eee, 194 .set_ntf_cmd = ETHTOOL_MSG_EEE_NTF, 195 }; 196