| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
|
| 3 |
#include <linux/ethtool.h> |
| 4 |
#include <linux/sfp.h> |
| 5 |
#include "netlink.h" |
| 6 |
#include "common.h" |
| 7 |
|
| 8 |
struct eeprom_req_info { |
| 9 |
struct ethnl_req_info base; |
| 10 |
u32 offset; |
| 11 |
u32 length; |
| 12 |
u8 page; |
| 13 |
u8 bank; |
| 14 |
u8 i2c_address; |
| 15 |
}; |
| 16 |
|
| 17 |
struct eeprom_reply_data { |
| 18 |
struct ethnl_reply_data base; |
| 19 |
u32 length; |
| 20 |
u8 *data; |
| 21 |
}; |
| 22 |
|
| 23 |
#define MODULE_EEPROM_REQINFO(__req_base) \ |
| 24 |
container_of(__req_base, struct eeprom_req_info, base) |
| 25 |
|
| 26 |
#define MODULE_EEPROM_REPDATA(__reply_base) \ |
| 27 |
container_of(__reply_base, struct eeprom_reply_data, base) |
| 28 |
|
| 29 |
static int fallback_set_params(struct eeprom_req_info *request, |
| 30 |
struct ethtool_modinfo *modinfo, |
| 31 |
struct ethtool_eeprom *eeprom) |
| 32 |
{ |
| 33 |
u32 offset = request->offset; |
| 34 |
u32 length = request->length; |
| 35 |
|
| 36 |
if (request->page) |
| 37 |
offset = request->page * ETH_MODULE_EEPROM_PAGE_LEN + offset; |
| 38 |
|
| 39 |
if (modinfo->type == ETH_MODULE_SFF_8472 && |
| 40 |
request->i2c_address == 0x51) |
| 41 |
offset += ETH_MODULE_EEPROM_PAGE_LEN * 2; |
| 42 |
|
| 43 |
if (offset >= modinfo->eeprom_len) |
| 44 |
return -EINVAL; |
| 45 |
|
| 46 |
if (length > modinfo->eeprom_len - offset) |
| 47 |
return -EINVAL; |
| 48 |
|
| 49 |
eeprom->cmd = ETHTOOL_GMODULEEEPROM; |
| 50 |
eeprom->len = length; |
| 51 |
eeprom->offset = offset; |
| 52 |
|
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
static int eeprom_fallback(struct eeprom_req_info *request, |
| 57 |
struct eeprom_reply_data *reply) |
| 58 |
{ |
| 59 |
struct net_device *dev = reply->base.dev; |
| 60 |
struct ethtool_modinfo modinfo = {0}; |
| 61 |
struct ethtool_eeprom eeprom = {0}; |
| 62 |
u8 *data; |
| 63 |
int err; |
| 64 |
|
| 65 |
modinfo.cmd = ETHTOOL_GMODULEINFO; |
| 66 |
err = ethtool_get_module_info_call(dev, &modinfo); |
| 67 |
if (err < 0) |
| 68 |
return err; |
| 69 |
|
| 70 |
err = fallback_set_params(request, &modinfo, &eeprom); |
| 71 |
if (err < 0) |
| 72 |
return err; |
| 73 |
|
| 74 |
data = kzalloc(eeprom.len, GFP_KERNEL); |
| 75 |
if (!data) |
| 76 |
return -ENOMEM; |
| 77 |
err = ethtool_get_module_eeprom_call(dev, &eeprom, data); |
| 78 |
if (err < 0) |
| 79 |
goto err_out; |
| 80 |
|
| 81 |
reply->data = data; |
| 82 |
reply->length = eeprom.len; |
| 83 |
|
| 84 |
return 0; |
| 85 |
|
| 86 |
err_out: |
| 87 |
kfree(data); |
| 88 |
return err; |
| 89 |
} |
| 90 |
|
| 91 |
static int get_module_eeprom_by_page(struct net_device *dev, |
| 92 |
struct ethtool_module_eeprom *page_data, |
| 93 |
struct netlink_ext_ack *extack) |
| 94 |
{ |
| 95 |
const struct ethtool_ops *ops = dev->ethtool_ops; |
| 96 |
|
| 97 |
if (dev->ethtool->module_fw_flash_in_progress) { |
| 98 |
NL_SET_ERR_MSG(extack, |
| 99 |
"Module firmware flashing is in progress"); |
| 100 |
return -EBUSY; |
| 101 |
} |
| 102 |
|
| 103 |
if (dev->sfp_bus) |
| 104 |
return sfp_get_module_eeprom_by_page(dev->sfp_bus, page_data, extack); |
| 105 |
|
| 106 |
if (ops->get_module_eeprom_by_page) |
| 107 |
return ops->get_module_eeprom_by_page(dev, page_data, extack); |
| 108 |
|
| 109 |
return -EOPNOTSUPP; |
| 110 |
} |
| 111 |
|
| 112 |
static int eeprom_prepare_data(const struct ethnl_req_info *req_base, |
| 113 |
struct ethnl_reply_data *reply_base, |
| 114 |
const struct genl_info *info) |
| 115 |
{ |
| 116 |
struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base); |
| 117 |
struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_base); |
| 118 |
struct ethtool_module_eeprom page_data = {0}; |
| 119 |
struct net_device *dev = reply_base->dev; |
| 120 |
int ret; |
| 121 |
|
| 122 |
page_data.offset = request->offset; |
| 123 |
page_data.length = request->length; |
| 124 |
page_data.i2c_address = request->i2c_address; |
| 125 |
page_data.page = request->page; |
| 126 |
page_data.bank = request->bank; |
| 127 |
page_data.data = kmalloc(page_data.length, GFP_KERNEL); |
| 128 |
if (!page_data.data) |
| 129 |
return -ENOMEM; |
| 130 |
|
| 131 |
ret = ethnl_ops_begin(dev); |
| 132 |
if (ret) |
| 133 |
goto err_free; |
| 134 |
|
| 135 |
ret = get_module_eeprom_by_page(dev, &page_data, info->extack); |
| 136 |
if (ret < 0) |
| 137 |
goto err_ops; |
| 138 |
|
| 139 |
reply->length = ret; |
| 140 |
reply->data = page_data.data; |
| 141 |
|
| 142 |
ethnl_ops_complete(dev); |
| 143 |
return 0; |
| 144 |
|
| 145 |
err_ops: |
| 146 |
if (ret == -EOPNOTSUPP) |
| 147 |
ret = eeprom_fallback(request, reply); |
| 148 |
ethnl_ops_complete(dev); |
| 149 |
err_free: |
| 150 |
kfree(page_data.data); |
| 151 |
return ret; |
| 152 |
} |
| 153 |
|
| 154 |
static int eeprom_parse_request(struct ethnl_req_info *req_info, struct nlattr **tb, |
| 155 |
struct netlink_ext_ack *extack) |
| 156 |
{ |
| 157 |
struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_info); |
| 158 |
|
| 159 |
if (!tb[ETHTOOL_A_MODULE_EEPROM_OFFSET] || |
| 160 |
!tb[ETHTOOL_A_MODULE_EEPROM_LENGTH] || |
| 161 |
!tb[ETHTOOL_A_MODULE_EEPROM_PAGE] || |
| 162 |
!tb[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS]) |
| 163 |
return -EINVAL; |
| 164 |
|
| 165 |
request->i2c_address = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS]); |
| 166 |
request->offset = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_OFFSET]); |
| 167 |
request->length = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_LENGTH]); |
| 168 |
|
| 169 |
/* The following set of conditions limit the API to only dump 1/2 |
| 170 |
* EEPROM page without crossing low page boundary located at offset 128. |
| 171 |
* This means user may only request dumps of length limited to 128 from |
| 172 |
* either low 128 bytes or high 128 bytes. |
| 173 |
* For pages higher than 0 only high 128 bytes are accessible. |
| 174 |
*/ |
| 175 |
request->page = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_PAGE]); |
| 176 |
if (request->page && request->offset < ETH_MODULE_EEPROM_PAGE_LEN) { |
| 177 |
NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_PAGE], |
| 178 |
"reading from lower half page is allowed for page 0 only"); |
| 179 |
return -EINVAL; |
| 180 |
} |
| 181 |
|
| 182 |
if (request->offset < ETH_MODULE_EEPROM_PAGE_LEN && |
| 183 |
request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN) { |
| 184 |
NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], |
| 185 |
"reading cross half page boundary is illegal"); |
| 186 |
return -EINVAL; |
| 187 |
} else if (request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN * 2) { |
| 188 |
NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH], |
| 189 |
"reading cross page boundary is illegal"); |
| 190 |
return -EINVAL; |
| 191 |
} |
| 192 |
|
| 193 |
if (tb[ETHTOOL_A_MODULE_EEPROM_BANK]) |
| 194 |
request->bank = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_BANK]); |
| 195 |
|
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
static int eeprom_reply_size(const struct ethnl_req_info *req_base, |
| 200 |
const struct ethnl_reply_data *reply_base) |
| 201 |
{ |
| 202 |
const struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_base); |
| 203 |
|
| 204 |
return nla_total_size(sizeof(u8) * request->length); /* _EEPROM_DATA */ |
| 205 |
} |
| 206 |
|
| 207 |
static int eeprom_fill_reply(struct sk_buff *skb, |
| 208 |
const struct ethnl_req_info *req_base, |
| 209 |
const struct ethnl_reply_data *reply_base) |
| 210 |
{ |
| 211 |
struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base); |
| 212 |
|
| 213 |
return nla_put(skb, ETHTOOL_A_MODULE_EEPROM_DATA, reply->length, reply->data); |
| 214 |
} |
| 215 |
|
| 216 |
static void eeprom_cleanup_data(struct ethnl_reply_data *reply_base) |
| 217 |
{ |
| 218 |
struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base); |
| 219 |
|
| 220 |
kfree(reply->data); |
| 221 |
} |
| 222 |
|
| 223 |
const struct ethnl_request_ops ethnl_module_eeprom_request_ops = { |
| 224 |
.request_cmd = ETHTOOL_MSG_MODULE_EEPROM_GET, |
| 225 |
.reply_cmd = ETHTOOL_MSG_MODULE_EEPROM_GET_REPLY, |
| 226 |
.hdr_attr = ETHTOOL_A_MODULE_EEPROM_HEADER, |
| 227 |
.req_info_size = sizeof(struct eeprom_req_info), |
| 228 |
.reply_data_size = sizeof(struct eeprom_reply_data), |
| 229 |
|
| 230 |
.parse_request = eeprom_parse_request, |
| 231 |
.prepare_data = eeprom_prepare_data, |
| 232 |
.reply_size = eeprom_reply_size, |
| 233 |
.fill_reply = eeprom_fill_reply, |
| 234 |
.cleanup_data = eeprom_cleanup_data, |
| 235 |
}; |
| 236 |
|
| 237 |
const struct nla_policy ethnl_module_eeprom_get_policy[] = { |
| 238 |
[ETHTOOL_A_MODULE_EEPROM_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy), |
| 239 |
[ETHTOOL_A_MODULE_EEPROM_OFFSET] = |
| 240 |
NLA_POLICY_MAX(NLA_U32, ETH_MODULE_EEPROM_PAGE_LEN * 2 - 1), |
| 241 |
[ETHTOOL_A_MODULE_EEPROM_LENGTH] = |
| 242 |
NLA_POLICY_RANGE(NLA_U32, 1, ETH_MODULE_EEPROM_PAGE_LEN), |
| 243 |
[ETHTOOL_A_MODULE_EEPROM_PAGE] = { .type = NLA_U8 }, |
| 244 |
[ETHTOOL_A_MODULE_EEPROM_BANK] = { .type = NLA_U8 }, |
| 245 |
[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS] = |
| 246 |
NLA_POLICY_RANGE(NLA_U8, 0, ETH_MODULE_MAX_I2C_ADDRESS), |
| 247 |
}; |
| 248 |
|