1// SPDX-License-Identifier: GPL-2.0-only 2 3#include <linux/ethtool.h> 4 5#include "netlink.h" 6#include "common.h" 7#include "bitset.h" 8 9struct module_req_info { 10 struct ethnl_req_info base; 11}; 12 13struct module_reply_data { 14 struct ethnl_reply_data base; 15 struct ethtool_module_power_mode_params power; 16}; 17 18#define MODULE_REPDATA(__reply_base) \ 19 container_of(__reply_base, struct module_reply_data, base) 20 21/* MODULE_GET */ 22 23const struct nla_policy ethnl_module_get_policy[ETHTOOL_A_MODULE_HEADER + 1] = { 24 [ETHTOOL_A_MODULE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy), 25}; 26 27static int module_get_power_mode(struct net_device *dev, 28 struct module_reply_data *data, 29 struct netlink_ext_ack *extack) 30{ 31 const struct ethtool_ops *ops = dev->ethtool_ops; 32 33 if (!ops->get_module_power_mode) 34 return 0; 35 36 return ops->get_module_power_mode(dev, &data->power, extack); 37} 38 39static int module_prepare_data(const struct ethnl_req_info *req_base, 40 struct ethnl_reply_data *reply_base, 41 const struct genl_info *info) 42{ 43 struct module_reply_data *data = MODULE_REPDATA(reply_base); 44 struct net_device *dev = reply_base->dev; 45 int ret; 46 47 ret = ethnl_ops_begin(dev); 48 if (ret < 0) 49 return ret; 50 51 ret = module_get_power_mode(dev, data, info->extack); 52 if (ret < 0) 53 goto out_complete; 54 55out_complete: 56 ethnl_ops_complete(dev); 57 return ret; 58} 59 60static int module_reply_size(const struct ethnl_req_info *req_base, 61 const struct ethnl_reply_data *reply_base) 62{ 63 struct module_reply_data *data = MODULE_REPDATA(reply_base); 64 int len = 0; 65 66 if (data->power.policy) 67 len += nla_total_size(sizeof(u8)); /* _MODULE_POWER_MODE_POLICY */ 68 69 if (data->power.mode) 70 len += nla_total_size(sizeof(u8)); /* _MODULE_POWER_MODE */ 71 72 return len; 73} 74 75static int module_fill_reply(struct sk_buff *skb, 76 const struct ethnl_req_info *req_base, 77 const struct ethnl_reply_data *reply_base) 78{ 79 const struct module_reply_data *data = MODULE_REPDATA(reply_base); 80 81 if (data->power.policy && 82 nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE_POLICY, 83 data->power.policy)) 84 return -EMSGSIZE; 85 86 if (data->power.mode && 87 nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE, data->power.mode)) 88 return -EMSGSIZE; 89 90 return 0; 91} 92 93/* MODULE_SET */ 94 95const struct nla_policy ethnl_module_set_policy[ETHTOOL_A_MODULE_POWER_MODE_POLICY + 1] = { 96 [ETHTOOL_A_MODULE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy), 97 [ETHTOOL_A_MODULE_POWER_MODE_POLICY] = 98 NLA_POLICY_RANGE(NLA_U8, ETHTOOL_MODULE_POWER_MODE_POLICY_HIGH, 99 ETHTOOL_MODULE_POWER_MODE_POLICY_AUTO), 100}; 101 102static int 103ethnl_set_module_validate(struct ethnl_req_info *req_info, 104 struct genl_info *info) 105{ 106 const struct ethtool_ops *ops = req_info->dev->ethtool_ops; 107 struct nlattr **tb = info->attrs; 108 109 if (!tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY]) 110 return 0; 111 112 if (!ops->get_module_power_mode || !ops->set_module_power_mode) { 113 NL_SET_ERR_MSG_ATTR(info->extack, 114 tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY], 115 "Setting power mode policy is not supported by this device"); 116 return -EOPNOTSUPP; 117 } 118 119 return 1; 120} 121 122static int 123ethnl_set_module(struct ethnl_req_info *req_info, struct genl_info *info) 124{ 125 struct ethtool_module_power_mode_params power = {}; 126 struct ethtool_module_power_mode_params power_new; 127 const struct ethtool_ops *ops; 128 struct net_device *dev = req_info->dev; 129 struct nlattr **tb = info->attrs; 130 int ret; 131 132 ops = dev->ethtool_ops; 133 134 power_new.policy = nla_get_u8(tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY]); 135 ret = ops->get_module_power_mode(dev, &power, info->extack); 136 if (ret < 0) 137 return ret; 138 139 if (power_new.policy == power.policy) 140 return 0; 141 142 ret = ops->set_module_power_mode(dev, &power_new, info->extack); 143 return ret < 0 ? ret : 1; 144} 145 146const struct ethnl_request_ops ethnl_module_request_ops = { 147 .request_cmd = ETHTOOL_MSG_MODULE_GET, 148 .reply_cmd = ETHTOOL_MSG_MODULE_GET_REPLY, 149 .hdr_attr = ETHTOOL_A_MODULE_HEADER, 150 .req_info_size = sizeof(struct module_req_info), 151 .reply_data_size = sizeof(struct module_reply_data), 152 153 .prepare_data = module_prepare_data, 154 .reply_size = module_reply_size, 155 .fill_reply = module_fill_reply, 156 157 .set_validate = ethnl_set_module_validate, 158 .set = ethnl_set_module, 159 .set_ntf_cmd = ETHTOOL_MSG_MODULE_NTF, 160}; 161