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