projects/linux·blob master · refs

251 lines · 7.1 KiB

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