|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | + |
| 3 | +#include <linux/ethtool.h> |
| 4 | +#include "netlink.h" |
| 5 | +#include "common.h" |
| 6 | + |
| 7 | +struct eeprom_req_info { |
| 8 | + struct ethnl_req_info base; |
| 9 | + u32 offset; |
| 10 | + u32 length; |
| 11 | + u8 page; |
| 12 | + u8 bank; |
| 13 | + u8 i2c_address; |
| 14 | +}; |
| 15 | + |
| 16 | +struct eeprom_reply_data { |
| 17 | + struct ethnl_reply_data base; |
| 18 | + u32 length; |
| 19 | + u8 *data; |
| 20 | +}; |
| 21 | + |
| 22 | +#define MODULE_EEPROM_REQINFO(__req_base) \ |
| 23 | + container_of(__req_base, struct eeprom_req_info, base) |
| 24 | + |
| 25 | +#define MODULE_EEPROM_REPDATA(__reply_base) \ |
| 26 | + container_of(__reply_base, struct eeprom_reply_data, base) |
| 27 | + |
| 28 | +static int eeprom_prepare_data(const struct ethnl_req_info *req_base, |
| 29 | + struct ethnl_reply_data *reply_base, |
| 30 | + struct genl_info *info) |
| 31 | +{ |
| 32 | + struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base); |
| 33 | + struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_base); |
| 34 | + struct ethtool_module_eeprom page_data = {0}; |
| 35 | + struct net_device *dev = reply_base->dev; |
| 36 | + int ret; |
| 37 | + |
| 38 | + if (!dev->ethtool_ops->get_module_eeprom_by_page) |
| 39 | + return -EOPNOTSUPP; |
| 40 | + |
| 41 | + page_data.offset = request->offset; |
| 42 | + page_data.length = request->length; |
| 43 | + page_data.i2c_address = request->i2c_address; |
| 44 | + page_data.page = request->page; |
| 45 | + page_data.bank = request->bank; |
| 46 | + page_data.data = kmalloc(page_data.length, GFP_KERNEL); |
| 47 | + if (!page_data.data) |
| 48 | + return -ENOMEM; |
| 49 | + |
| 50 | + ret = ethnl_ops_begin(dev); |
| 51 | + if (ret) |
| 52 | + goto err_free; |
| 53 | + |
| 54 | + ret = dev->ethtool_ops->get_module_eeprom_by_page(dev, &page_data, |
| 55 | + info->extack); |
| 56 | + if (ret < 0) |
| 57 | + goto err_ops; |
| 58 | + |
| 59 | + reply->length = ret; |
| 60 | + reply->data = page_data.data; |
| 61 | + |
| 62 | + ethnl_ops_complete(dev); |
| 63 | + return 0; |
| 64 | + |
| 65 | +err_ops: |
| 66 | + ethnl_ops_complete(dev); |
| 67 | +err_free: |
| 68 | + kfree(page_data.data); |
| 69 | + return ret; |
| 70 | +} |
| 71 | + |
| 72 | +static int eeprom_parse_request(struct ethnl_req_info *req_info, struct nlattr **tb, |
| 73 | + struct netlink_ext_ack *extack) |
| 74 | +{ |
| 75 | + struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_info); |
| 76 | + |
| 77 | + if (!tb[ETHTOOL_A_MODULE_EEPROM_OFFSET] || |
| 78 | + !tb[ETHTOOL_A_MODULE_EEPROM_LENGTH] || |
| 79 | + !tb[ETHTOOL_A_MODULE_EEPROM_PAGE] || |
| 80 | + !tb[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS]) |
| 81 | + return -EINVAL; |
| 82 | + |
| 83 | + request->i2c_address = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS]); |
| 84 | + request->offset = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_OFFSET]); |
| 85 | + request->length = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_LENGTH]); |
| 86 | + |
| 87 | + if (!request->length) |
| 88 | + return -EINVAL; |
| 89 | + |
| 90 | + /* The following set of conditions limit the API to only dump 1/2 |
| 91 | + * EEPROM page without crossing low page boundary located at offset 128. |
| 92 | + * This means user may only request dumps of length limited to 128 from |
| 93 | + * either low 128 bytes or high 128 bytes. |
| 94 | + * For pages higher than 0 only high 128 bytes are accessible. |
| 95 | + */ |
| 96 | + request->page = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_PAGE]); |
| 97 | + if (request->page && request->offset < ETH_MODULE_EEPROM_PAGE_LEN) { |
| 98 | + NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_PAGE], |
| 99 | + "reading from lower half page is allowed for page 0 only"); |
| 100 | + return -EINVAL; |
| 101 | + } |
| 102 | + |
| 103 | + if (request->offset < ETH_MODULE_EEPROM_PAGE_LEN && |
| 104 | + request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN) { |
| 105 | + NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], |
| 106 | + "reading cross half page boundary is illegal"); |
| 107 | + return -EINVAL; |
| 108 | + } else if (request->offset >= ETH_MODULE_EEPROM_PAGE_LEN * 2) { |
| 109 | + NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_OFFSET], |
| 110 | + "offset is out of bounds"); |
| 111 | + return -EINVAL; |
| 112 | + } else if (request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN * 2) { |
| 113 | + NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], |
| 114 | + "reading cross page boundary is illegal"); |
| 115 | + return -EINVAL; |
| 116 | + } |
| 117 | + |
| 118 | + if (tb[ETHTOOL_A_MODULE_EEPROM_BANK]) |
| 119 | + request->bank = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_BANK]); |
| 120 | + |
| 121 | + return 0; |
| 122 | +} |
| 123 | + |
| 124 | +static int eeprom_reply_size(const struct ethnl_req_info *req_base, |
| 125 | + const struct ethnl_reply_data *reply_base) |
| 126 | +{ |
| 127 | + const struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_base); |
| 128 | + |
| 129 | + return nla_total_size(sizeof(u8) * request->length); /* _EEPROM_DATA */ |
| 130 | +} |
| 131 | + |
| 132 | +static int eeprom_fill_reply(struct sk_buff *skb, |
| 133 | + const struct ethnl_req_info *req_base, |
| 134 | + const struct ethnl_reply_data *reply_base) |
| 135 | +{ |
| 136 | + struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base); |
| 137 | + |
| 138 | + return nla_put(skb, ETHTOOL_A_MODULE_EEPROM_DATA, reply->length, reply->data); |
| 139 | +} |
| 140 | + |
| 141 | +static void eeprom_cleanup_data(struct ethnl_reply_data *reply_base) |
| 142 | +{ |
| 143 | + struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base); |
| 144 | + |
| 145 | + kfree(reply->data); |
| 146 | +} |
| 147 | + |
| 148 | +const struct ethnl_request_ops ethnl_module_eeprom_request_ops = { |
| 149 | + .request_cmd = ETHTOOL_MSG_MODULE_EEPROM_GET, |
| 150 | + .reply_cmd = ETHTOOL_MSG_MODULE_EEPROM_GET_REPLY, |
| 151 | + .hdr_attr = ETHTOOL_A_MODULE_EEPROM_HEADER, |
| 152 | + .req_info_size = sizeof(struct eeprom_req_info), |
| 153 | + .reply_data_size = sizeof(struct eeprom_reply_data), |
| 154 | + |
| 155 | + .parse_request = eeprom_parse_request, |
| 156 | + .prepare_data = eeprom_prepare_data, |
| 157 | + .reply_size = eeprom_reply_size, |
| 158 | + .fill_reply = eeprom_fill_reply, |
| 159 | + .cleanup_data = eeprom_cleanup_data, |
| 160 | +}; |
| 161 | + |
| 162 | +const struct nla_policy ethnl_module_eeprom_get_policy[] = { |
| 163 | + [ETHTOOL_A_MODULE_EEPROM_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy), |
| 164 | + [ETHTOOL_A_MODULE_EEPROM_OFFSET] = { .type = NLA_U32 }, |
| 165 | + [ETHTOOL_A_MODULE_EEPROM_LENGTH] = { .type = NLA_U32 }, |
| 166 | + [ETHTOOL_A_MODULE_EEPROM_PAGE] = { .type = NLA_U8 }, |
| 167 | + [ETHTOOL_A_MODULE_EEPROM_BANK] = { .type = NLA_U8 }, |
| 168 | + [ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS] = |
| 169 | + NLA_POLICY_RANGE(NLA_U8, 0, ETH_MODULE_MAX_I2C_ADDRESS), |
| 170 | +}; |
| 171 | + |
0 commit comments