1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/ethtool.h>
8 struct eeprom_req_info {
9 struct ethnl_req_info base;
17 struct eeprom_reply_data {
18 struct ethnl_reply_data base;
23 #define MODULE_EEPROM_REQINFO(__req_base) \
24 container_of(__req_base, struct eeprom_req_info, base)
26 #define MODULE_EEPROM_REPDATA(__reply_base) \
27 container_of(__reply_base, struct eeprom_reply_data, base)
29 static int fallback_set_params(struct eeprom_req_info *request,
30 struct ethtool_modinfo *modinfo,
31 struct ethtool_eeprom *eeprom)
33 u32 offset = request->offset;
34 u32 length = request->length;
37 offset = request->page * ETH_MODULE_EEPROM_PAGE_LEN + offset;
39 if (modinfo->type == ETH_MODULE_SFF_8472 &&
40 request->i2c_address == 0x51)
41 offset += ETH_MODULE_EEPROM_PAGE_LEN * 2;
43 if (offset >= modinfo->eeprom_len)
46 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
48 eeprom->offset = offset;
53 static int eeprom_fallback(struct eeprom_req_info *request,
54 struct eeprom_reply_data *reply,
55 struct genl_info *info)
57 struct net_device *dev = reply->base.dev;
58 struct ethtool_modinfo modinfo = {0};
59 struct ethtool_eeprom eeprom = {0};
63 modinfo.cmd = ETHTOOL_GMODULEINFO;
64 err = ethtool_get_module_info_call(dev, &modinfo);
68 err = fallback_set_params(request, &modinfo, &eeprom);
72 data = kmalloc(eeprom.len, GFP_KERNEL);
75 err = ethtool_get_module_eeprom_call(dev, &eeprom, data);
80 reply->length = eeprom.len;
89 static int get_module_eeprom_by_page(struct net_device *dev,
90 struct ethtool_module_eeprom *page_data,
91 struct netlink_ext_ack *extack)
93 const struct ethtool_ops *ops = dev->ethtool_ops;
96 return sfp_get_module_eeprom_by_page(dev->sfp_bus, page_data, extack);
98 if (ops->get_module_eeprom_by_page)
99 return ops->get_module_eeprom_by_page(dev, page_data, extack);
104 static int eeprom_prepare_data(const struct ethnl_req_info *req_base,
105 struct ethnl_reply_data *reply_base,
106 struct genl_info *info)
108 struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base);
109 struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_base);
110 struct ethtool_module_eeprom page_data = {0};
111 struct net_device *dev = reply_base->dev;
114 page_data.offset = request->offset;
115 page_data.length = request->length;
116 page_data.i2c_address = request->i2c_address;
117 page_data.page = request->page;
118 page_data.bank = request->bank;
119 page_data.data = kmalloc(page_data.length, GFP_KERNEL);
123 ret = ethnl_ops_begin(dev);
127 ret = get_module_eeprom_by_page(dev, &page_data, info->extack);
132 reply->data = page_data.data;
134 ethnl_ops_complete(dev);
138 ethnl_ops_complete(dev);
140 kfree(page_data.data);
142 if (ret == -EOPNOTSUPP)
143 return eeprom_fallback(request, reply, info);
147 static int eeprom_parse_request(struct ethnl_req_info *req_info, struct nlattr **tb,
148 struct netlink_ext_ack *extack)
150 struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_info);
152 if (!tb[ETHTOOL_A_MODULE_EEPROM_OFFSET] ||
153 !tb[ETHTOOL_A_MODULE_EEPROM_LENGTH] ||
154 !tb[ETHTOOL_A_MODULE_EEPROM_PAGE] ||
155 !tb[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS])
158 request->i2c_address = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS]);
159 request->offset = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_OFFSET]);
160 request->length = nla_get_u32(tb[ETHTOOL_A_MODULE_EEPROM_LENGTH]);
162 /* The following set of conditions limit the API to only dump 1/2
163 * EEPROM page without crossing low page boundary located at offset 128.
164 * This means user may only request dumps of length limited to 128 from
165 * either low 128 bytes or high 128 bytes.
166 * For pages higher than 0 only high 128 bytes are accessible.
168 request->page = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_PAGE]);
169 if (request->page && request->offset < ETH_MODULE_EEPROM_PAGE_LEN) {
170 NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_PAGE],
171 "reading from lower half page is allowed for page 0 only");
175 if (request->offset < ETH_MODULE_EEPROM_PAGE_LEN &&
176 request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN) {
177 NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH],
178 "reading cross half page boundary is illegal");
180 } else if (request->offset + request->length > ETH_MODULE_EEPROM_PAGE_LEN * 2) {
181 NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_MODULE_EEPROM_LENGTH],
182 "reading cross page boundary is illegal");
186 if (tb[ETHTOOL_A_MODULE_EEPROM_BANK])
187 request->bank = nla_get_u8(tb[ETHTOOL_A_MODULE_EEPROM_BANK]);
192 static int eeprom_reply_size(const struct ethnl_req_info *req_base,
193 const struct ethnl_reply_data *reply_base)
195 const struct eeprom_req_info *request = MODULE_EEPROM_REQINFO(req_base);
197 return nla_total_size(sizeof(u8) * request->length); /* _EEPROM_DATA */
200 static int eeprom_fill_reply(struct sk_buff *skb,
201 const struct ethnl_req_info *req_base,
202 const struct ethnl_reply_data *reply_base)
204 struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base);
206 return nla_put(skb, ETHTOOL_A_MODULE_EEPROM_DATA, reply->length, reply->data);
209 static void eeprom_cleanup_data(struct ethnl_reply_data *reply_base)
211 struct eeprom_reply_data *reply = MODULE_EEPROM_REPDATA(reply_base);
216 const struct ethnl_request_ops ethnl_module_eeprom_request_ops = {
217 .request_cmd = ETHTOOL_MSG_MODULE_EEPROM_GET,
218 .reply_cmd = ETHTOOL_MSG_MODULE_EEPROM_GET_REPLY,
219 .hdr_attr = ETHTOOL_A_MODULE_EEPROM_HEADER,
220 .req_info_size = sizeof(struct eeprom_req_info),
221 .reply_data_size = sizeof(struct eeprom_reply_data),
223 .parse_request = eeprom_parse_request,
224 .prepare_data = eeprom_prepare_data,
225 .reply_size = eeprom_reply_size,
226 .fill_reply = eeprom_fill_reply,
227 .cleanup_data = eeprom_cleanup_data,
230 const struct nla_policy ethnl_module_eeprom_get_policy[] = {
231 [ETHTOOL_A_MODULE_EEPROM_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
232 [ETHTOOL_A_MODULE_EEPROM_OFFSET] =
233 NLA_POLICY_MAX(NLA_U32, ETH_MODULE_EEPROM_PAGE_LEN * 2 - 1),
234 [ETHTOOL_A_MODULE_EEPROM_LENGTH] =
235 NLA_POLICY_RANGE(NLA_U32, 1, ETH_MODULE_EEPROM_PAGE_LEN),
236 [ETHTOOL_A_MODULE_EEPROM_PAGE] = { .type = NLA_U8 },
237 [ETHTOOL_A_MODULE_EEPROM_BANK] = { .type = NLA_U8 },
238 [ETHTOOL_A_MODULE_EEPROM_I2C_ADDRESS] =
239 NLA_POLICY_RANGE(NLA_U8, 0, ETH_MODULE_MAX_I2C_ADDRESS),