1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <linux/ethtool.h>
4 #include <linux/firmware.h>
5 #include <linux/sfp.h>
6 #include <net/devlink.h>
7 #include <net/netdev_lock.h>
8
9 #include "bitset.h"
10 #include "common.h"
11 #include "module_fw.h"
12 #include "netlink.h"
13
14 struct module_req_info {
15 struct ethnl_req_info base;
16 };
17
18 struct module_reply_data {
19 struct ethnl_reply_data base;
20 struct ethtool_module_power_mode_params power;
21 };
22
23 #define MODULE_REPDATA(__reply_base) \
24 container_of(__reply_base, struct module_reply_data, base)
25
26 /* MODULE_GET */
27
28 const struct nla_policy ethnl_module_get_policy[ETHTOOL_A_MODULE_HEADER + 1] = {
29 [ETHTOOL_A_MODULE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
30 };
31
module_get_power_mode(struct net_device * dev,struct module_reply_data * data,struct netlink_ext_ack * extack)32 static int module_get_power_mode(struct net_device *dev,
33 struct module_reply_data *data,
34 struct netlink_ext_ack *extack)
35 {
36 const struct ethtool_ops *ops = dev->ethtool_ops;
37
38 if (!ops->get_module_power_mode)
39 return 0;
40
41 if (dev->ethtool->module_fw_flash_in_progress) {
42 NL_SET_ERR_MSG(extack,
43 "Module firmware flashing is in progress");
44 return -EBUSY;
45 }
46
47 return ops->get_module_power_mode(dev, &data->power, extack);
48 }
49
module_prepare_data(const struct ethnl_req_info * req_base,struct ethnl_reply_data * reply_base,const struct genl_info * info)50 static int module_prepare_data(const struct ethnl_req_info *req_base,
51 struct ethnl_reply_data *reply_base,
52 const struct genl_info *info)
53 {
54 struct module_reply_data *data = MODULE_REPDATA(reply_base);
55 struct net_device *dev = reply_base->dev;
56 int ret;
57
58 ret = ethnl_ops_begin(dev);
59 if (ret < 0)
60 return ret;
61
62 ret = module_get_power_mode(dev, data, info->extack);
63 if (ret < 0)
64 goto out_complete;
65
66 out_complete:
67 ethnl_ops_complete(dev);
68 return ret;
69 }
70
module_reply_size(const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)71 static int module_reply_size(const struct ethnl_req_info *req_base,
72 const struct ethnl_reply_data *reply_base)
73 {
74 struct module_reply_data *data = MODULE_REPDATA(reply_base);
75 int len = 0;
76
77 if (data->power.policy)
78 len += nla_total_size(sizeof(u8)); /* _MODULE_POWER_MODE_POLICY */
79
80 if (data->power.mode)
81 len += nla_total_size(sizeof(u8)); /* _MODULE_POWER_MODE */
82
83 return len;
84 }
85
module_fill_reply(struct sk_buff * skb,const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)86 static int module_fill_reply(struct sk_buff *skb,
87 const struct ethnl_req_info *req_base,
88 const struct ethnl_reply_data *reply_base)
89 {
90 const struct module_reply_data *data = MODULE_REPDATA(reply_base);
91
92 if (data->power.policy &&
93 nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE_POLICY,
94 data->power.policy))
95 return -EMSGSIZE;
96
97 if (data->power.mode &&
98 nla_put_u8(skb, ETHTOOL_A_MODULE_POWER_MODE, data->power.mode))
99 return -EMSGSIZE;
100
101 return 0;
102 }
103
104 /* MODULE_SET */
105
106 const struct nla_policy ethnl_module_set_policy[ETHTOOL_A_MODULE_POWER_MODE_POLICY + 1] = {
107 [ETHTOOL_A_MODULE_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
108 [ETHTOOL_A_MODULE_POWER_MODE_POLICY] =
109 NLA_POLICY_RANGE(NLA_U8, ETHTOOL_MODULE_POWER_MODE_POLICY_HIGH,
110 ETHTOOL_MODULE_POWER_MODE_POLICY_AUTO),
111 };
112
113 static int
ethnl_set_module_validate(struct ethnl_req_info * req_info,struct genl_info * info)114 ethnl_set_module_validate(struct ethnl_req_info *req_info,
115 struct genl_info *info)
116 {
117 const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
118 struct nlattr **tb = info->attrs;
119
120 if (!tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY])
121 return 0;
122
123 if (!ops->get_module_power_mode || !ops->set_module_power_mode) {
124 NL_SET_ERR_MSG_ATTR(info->extack,
125 tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY],
126 "Setting power mode policy is not supported by this device");
127 return -EOPNOTSUPP;
128 }
129
130 return 1;
131 }
132
133 static int
ethnl_set_module(struct ethnl_req_info * req_info,struct genl_info * info)134 ethnl_set_module(struct ethnl_req_info *req_info, struct genl_info *info)
135 {
136 struct ethtool_module_power_mode_params power = {};
137 struct ethtool_module_power_mode_params power_new;
138 const struct ethtool_ops *ops;
139 struct net_device *dev = req_info->dev;
140 struct nlattr **tb = info->attrs;
141 int ret;
142
143 ops = dev->ethtool_ops;
144
145 if (dev->ethtool->module_fw_flash_in_progress) {
146 NL_SET_ERR_MSG(info->extack,
147 "Module firmware flashing is in progress");
148 return -EBUSY;
149 }
150
151 power_new.policy = nla_get_u8(tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY]);
152 ret = ops->get_module_power_mode(dev, &power, info->extack);
153 if (ret < 0)
154 return ret;
155
156 if (power_new.policy == power.policy)
157 return 0;
158
159 ret = ops->set_module_power_mode(dev, &power_new, info->extack);
160 return ret < 0 ? ret : 1;
161 }
162
163 const struct ethnl_request_ops ethnl_module_request_ops = {
164 .request_cmd = ETHTOOL_MSG_MODULE_GET,
165 .reply_cmd = ETHTOOL_MSG_MODULE_GET_REPLY,
166 .hdr_attr = ETHTOOL_A_MODULE_HEADER,
167 .req_info_size = sizeof(struct module_req_info),
168 .reply_data_size = sizeof(struct module_reply_data),
169
170 .prepare_data = module_prepare_data,
171 .reply_size = module_reply_size,
172 .fill_reply = module_fill_reply,
173
174 .set_validate = ethnl_set_module_validate,
175 .set = ethnl_set_module,
176 .set_ntf_cmd = ETHTOOL_MSG_MODULE_NTF,
177 };
178
179 /* MODULE_FW_FLASH_ACT */
180
181 const struct nla_policy
182 ethnl_module_fw_flash_act_policy[ETHTOOL_A_MODULE_FW_FLASH_PASSWORD + 1] = {
183 [ETHTOOL_A_MODULE_FW_FLASH_HEADER] =
184 NLA_POLICY_NESTED(ethnl_header_policy),
185 [ETHTOOL_A_MODULE_FW_FLASH_FILE_NAME] = { .type = NLA_NUL_STRING },
186 [ETHTOOL_A_MODULE_FW_FLASH_PASSWORD] = { .type = NLA_U32 },
187 };
188
189 static LIST_HEAD(module_fw_flash_work_list);
190 static DEFINE_SPINLOCK(module_fw_flash_work_list_lock);
191
192 static int
module_flash_fw_work_list_add(struct ethtool_module_fw_flash * module_fw,struct genl_info * info)193 module_flash_fw_work_list_add(struct ethtool_module_fw_flash *module_fw,
194 struct genl_info *info)
195 {
196 struct ethtool_module_fw_flash *work;
197
198 /* First, check if already registered. */
199 spin_lock(&module_fw_flash_work_list_lock);
200 list_for_each_entry(work, &module_fw_flash_work_list, list) {
201 if (work->fw_update.ntf_params.portid == info->snd_portid &&
202 work->fw_update.dev == module_fw->fw_update.dev) {
203 spin_unlock(&module_fw_flash_work_list_lock);
204 return -EALREADY;
205 }
206 }
207
208 list_add_tail(&module_fw->list, &module_fw_flash_work_list);
209 spin_unlock(&module_fw_flash_work_list_lock);
210
211 return 0;
212 }
213
module_flash_fw_work_list_del(struct list_head * list)214 static void module_flash_fw_work_list_del(struct list_head *list)
215 {
216 spin_lock(&module_fw_flash_work_list_lock);
217 list_del(list);
218 spin_unlock(&module_fw_flash_work_list_lock);
219 }
220
module_flash_fw_work(struct work_struct * work)221 static void module_flash_fw_work(struct work_struct *work)
222 {
223 struct ethtool_module_fw_flash *module_fw;
224 struct net_device *dev;
225
226 module_fw = container_of(work, struct ethtool_module_fw_flash, work);
227 dev = module_fw->fw_update.dev;
228
229 ethtool_cmis_fw_update(&module_fw->fw_update);
230
231 module_flash_fw_work_list_del(&module_fw->list);
232
233 rtnl_lock();
234 netdev_lock_ops(dev);
235 dev->ethtool->module_fw_flash_in_progress = false;
236 netdev_unlock_ops(dev);
237 rtnl_unlock();
238
239 netdev_put(dev, &module_fw->dev_tracker);
240 release_firmware(module_fw->fw_update.fw);
241 kfree(module_fw);
242 }
243
244 #define MODULE_EEPROM_PHYS_ID_PAGE 0
245 #define MODULE_EEPROM_PHYS_ID_I2C_ADDR 0x50
246
module_flash_fw_work_init(struct ethtool_module_fw_flash * module_fw,struct net_device * dev,struct netlink_ext_ack * extack)247 static int module_flash_fw_work_init(struct ethtool_module_fw_flash *module_fw,
248 struct net_device *dev,
249 struct netlink_ext_ack *extack)
250 {
251 const struct ethtool_ops *ops = dev->ethtool_ops;
252 struct ethtool_module_eeprom page_data = {};
253 u8 phys_id;
254 int err;
255
256 /* Fetch the SFF-8024 Identifier Value. For all supported standards, it
257 * is located at I2C address 0x50, byte 0. See section 4.1 in SFF-8024,
258 * revision 4.9.
259 */
260 page_data.page = MODULE_EEPROM_PHYS_ID_PAGE;
261 page_data.offset = SFP_PHYS_ID;
262 page_data.length = sizeof(phys_id);
263 page_data.i2c_address = MODULE_EEPROM_PHYS_ID_I2C_ADDR;
264 page_data.data = &phys_id;
265
266 err = ops->get_module_eeprom_by_page(dev, &page_data, extack);
267 if (err < 0)
268 return err;
269
270 switch (phys_id) {
271 case SFF8024_ID_QSFP_DD:
272 case SFF8024_ID_OSFP:
273 case SFF8024_ID_DSFP:
274 case SFF8024_ID_QSFP_PLUS_CMIS:
275 case SFF8024_ID_SFP_DD_CMIS:
276 case SFF8024_ID_SFP_PLUS_CMIS:
277 INIT_WORK(&module_fw->work, module_flash_fw_work);
278 break;
279 default:
280 NL_SET_ERR_MSG(extack,
281 "Module type does not support firmware flashing");
282 return -EOPNOTSUPP;
283 }
284
285 return 0;
286 }
287
ethnl_module_fw_flash_sock_destroy(struct ethnl_sock_priv * sk_priv)288 void ethnl_module_fw_flash_sock_destroy(struct ethnl_sock_priv *sk_priv)
289 {
290 struct ethtool_module_fw_flash *work;
291
292 spin_lock(&module_fw_flash_work_list_lock);
293 list_for_each_entry(work, &module_fw_flash_work_list, list) {
294 if (work->fw_update.ntf_params.portid == sk_priv->portid &&
295 dev_net(work->fw_update.dev) == sk_priv->net)
296 work->fw_update.ntf_params.closed_sock = true;
297 }
298 spin_unlock(&module_fw_flash_work_list_lock);
299 }
300
301 static int
module_flash_fw_schedule(struct net_device * dev,const char * file_name,struct ethtool_module_fw_flash_params * params,struct sk_buff * skb,struct genl_info * info)302 module_flash_fw_schedule(struct net_device *dev, const char *file_name,
303 struct ethtool_module_fw_flash_params *params,
304 struct sk_buff *skb, struct genl_info *info)
305 {
306 struct ethtool_cmis_fw_update_params *fw_update;
307 struct ethtool_module_fw_flash *module_fw;
308 int err;
309
310 module_fw = kzalloc_obj(*module_fw);
311 if (!module_fw)
312 return -ENOMEM;
313
314 fw_update = &module_fw->fw_update;
315 fw_update->params = *params;
316 err = request_firmware_direct(&fw_update->fw,
317 file_name, &dev->dev);
318 if (err) {
319 NL_SET_ERR_MSG(info->extack,
320 "Failed to request module firmware image");
321 goto err_free;
322 }
323
324 err = module_flash_fw_work_init(module_fw, dev, info->extack);
325 if (err < 0)
326 goto err_release_firmware;
327
328 fw_update->dev = dev;
329 fw_update->ntf_params.portid = info->snd_portid;
330 fw_update->ntf_params.seq = info->snd_seq;
331 fw_update->ntf_params.closed_sock = false;
332
333 err = ethnl_sock_priv_set(skb, dev_net(dev),
334 fw_update->ntf_params.portid,
335 ETHTOOL_SOCK_TYPE_MODULE_FW_FLASH);
336 if (err < 0)
337 goto err_release_firmware;
338
339 err = module_flash_fw_work_list_add(module_fw, info);
340 if (err < 0)
341 goto err_release_firmware;
342
343 dev->ethtool->module_fw_flash_in_progress = true;
344 netdev_hold(dev, &module_fw->dev_tracker, GFP_KERNEL);
345
346 schedule_work(&module_fw->work);
347
348 return 0;
349
350 err_release_firmware:
351 release_firmware(fw_update->fw);
352 err_free:
353 kfree(module_fw);
354 return err;
355 }
356
module_flash_fw(struct net_device * dev,struct nlattr ** tb,struct sk_buff * skb,struct genl_info * info)357 static int module_flash_fw(struct net_device *dev, struct nlattr **tb,
358 struct sk_buff *skb, struct genl_info *info)
359 {
360 struct ethtool_module_fw_flash_params params = {};
361 const char *file_name;
362 struct nlattr *attr;
363
364 if (GENL_REQ_ATTR_CHECK(info, ETHTOOL_A_MODULE_FW_FLASH_FILE_NAME))
365 return -EINVAL;
366
367 file_name = nla_data(tb[ETHTOOL_A_MODULE_FW_FLASH_FILE_NAME]);
368
369 attr = tb[ETHTOOL_A_MODULE_FW_FLASH_PASSWORD];
370 if (attr) {
371 params.password = cpu_to_be32(nla_get_u32(attr));
372 params.password_valid = true;
373 }
374
375 return module_flash_fw_schedule(dev, file_name, ¶ms, skb, info);
376 }
377
ethnl_module_fw_flash_validate(struct net_device * dev,struct netlink_ext_ack * extack)378 static int ethnl_module_fw_flash_validate(struct net_device *dev,
379 struct netlink_ext_ack *extack)
380 {
381 struct devlink_port *devlink_port = dev->devlink_port;
382 const struct ethtool_ops *ops = dev->ethtool_ops;
383
384 if (!ops->set_module_eeprom_by_page ||
385 !ops->get_module_eeprom_by_page) {
386 NL_SET_ERR_MSG(extack,
387 "Flashing module firmware is not supported by this device");
388 return -EOPNOTSUPP;
389 }
390
391 if (!ops->reset) {
392 NL_SET_ERR_MSG(extack,
393 "Reset module is not supported by this device, so flashing is not permitted");
394 return -EOPNOTSUPP;
395 }
396
397 if (dev->ethtool->module_fw_flash_in_progress) {
398 NL_SET_ERR_MSG(extack, "Module firmware flashing already in progress");
399 return -EBUSY;
400 }
401
402 if (dev->flags & IFF_UP) {
403 NL_SET_ERR_MSG(extack, "Netdevice is up, so flashing is not permitted");
404 return -EBUSY;
405 }
406
407 if (devlink_port && devlink_port->attrs.split) {
408 NL_SET_ERR_MSG(extack, "Can't perform firmware flashing on a split port");
409 return -EOPNOTSUPP;
410 }
411
412 return 0;
413 }
414
ethnl_act_module_fw_flash(struct sk_buff * skb,struct genl_info * info)415 int ethnl_act_module_fw_flash(struct sk_buff *skb, struct genl_info *info)
416 {
417 struct ethnl_req_info req_info = {};
418 struct nlattr **tb = info->attrs;
419 struct net_device *dev;
420 int ret;
421
422 ret = ethnl_parse_header_dev_get(&req_info,
423 tb[ETHTOOL_A_MODULE_FW_FLASH_HEADER],
424 genl_info_net(info), info->extack,
425 true);
426 if (ret < 0)
427 return ret;
428 dev = req_info.dev;
429
430 rtnl_lock();
431 netdev_lock_ops(dev);
432 ret = ethnl_ops_begin(dev);
433 if (ret < 0)
434 goto out_unlock;
435
436 ret = ethnl_module_fw_flash_validate(dev, info->extack);
437 if (ret < 0)
438 goto out_complete;
439
440 ret = module_flash_fw(dev, tb, skb, info);
441
442 out_complete:
443 ethnl_ops_complete(dev);
444
445 out_unlock:
446 netdev_unlock_ops(dev);
447 rtnl_unlock();
448 ethnl_parse_header_dev_put(&req_info);
449 return ret;
450 }
451
452 /* MODULE_FW_FLASH_NTF */
453
454 static int
ethnl_module_fw_flash_ntf_put_err(struct sk_buff * skb,char * err_msg,char * sub_err_msg)455 ethnl_module_fw_flash_ntf_put_err(struct sk_buff *skb, char *err_msg,
456 char *sub_err_msg)
457 {
458 int err_msg_len, sub_err_msg_len, total_len;
459 struct nlattr *attr;
460
461 if (!err_msg)
462 return 0;
463
464 err_msg_len = strlen(err_msg);
465 total_len = err_msg_len + 2; /* For period and NUL. */
466
467 if (sub_err_msg) {
468 sub_err_msg_len = strlen(sub_err_msg);
469 total_len += sub_err_msg_len + 2; /* For ", ". */
470 }
471
472 attr = nla_reserve(skb, ETHTOOL_A_MODULE_FW_FLASH_STATUS_MSG,
473 total_len);
474 if (!attr)
475 return -ENOMEM;
476
477 if (sub_err_msg)
478 sprintf(nla_data(attr), "%s, %s.", err_msg, sub_err_msg);
479 else
480 sprintf(nla_data(attr), "%s.", err_msg);
481
482 return 0;
483 }
484
485 static void
ethnl_module_fw_flash_ntf(struct net_device * dev,enum ethtool_module_fw_flash_status status,struct ethnl_module_fw_flash_ntf_params * ntf_params,char * err_msg,char * sub_err_msg,u64 done,u64 total)486 ethnl_module_fw_flash_ntf(struct net_device *dev,
487 enum ethtool_module_fw_flash_status status,
488 struct ethnl_module_fw_flash_ntf_params *ntf_params,
489 char *err_msg, char *sub_err_msg,
490 u64 done, u64 total)
491 {
492 struct sk_buff *skb;
493 void *hdr;
494 int ret;
495
496 if (ntf_params->closed_sock)
497 return;
498
499 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
500 if (!skb)
501 return;
502
503 hdr = ethnl_unicast_put(skb, ntf_params->portid, ++ntf_params->seq,
504 ETHTOOL_MSG_MODULE_FW_FLASH_NTF);
505 if (!hdr)
506 goto err_skb;
507
508 ret = ethnl_fill_reply_header(skb, dev,
509 ETHTOOL_A_MODULE_FW_FLASH_HEADER);
510 if (ret < 0)
511 goto err_skb;
512
513 if (nla_put_u32(skb, ETHTOOL_A_MODULE_FW_FLASH_STATUS, status))
514 goto err_skb;
515
516 ret = ethnl_module_fw_flash_ntf_put_err(skb, err_msg, sub_err_msg);
517 if (ret < 0)
518 goto err_skb;
519
520 if (nla_put_uint(skb, ETHTOOL_A_MODULE_FW_FLASH_DONE, done))
521 goto err_skb;
522
523 if (nla_put_uint(skb, ETHTOOL_A_MODULE_FW_FLASH_TOTAL, total))
524 goto err_skb;
525
526 genlmsg_end(skb, hdr);
527 genlmsg_unicast(dev_net(dev), skb, ntf_params->portid);
528 return;
529
530 err_skb:
531 nlmsg_free(skb);
532 }
533
ethnl_module_fw_flash_ntf_err(struct net_device * dev,struct ethnl_module_fw_flash_ntf_params * params,char * err_msg,char * sub_err_msg)534 void ethnl_module_fw_flash_ntf_err(struct net_device *dev,
535 struct ethnl_module_fw_flash_ntf_params *params,
536 char *err_msg, char *sub_err_msg)
537 {
538 ethnl_module_fw_flash_ntf(dev, ETHTOOL_MODULE_FW_FLASH_STATUS_ERROR,
539 params, err_msg, sub_err_msg, 0, 0);
540 }
541
542 void
ethnl_module_fw_flash_ntf_start(struct net_device * dev,struct ethnl_module_fw_flash_ntf_params * params)543 ethnl_module_fw_flash_ntf_start(struct net_device *dev,
544 struct ethnl_module_fw_flash_ntf_params *params)
545 {
546 ethnl_module_fw_flash_ntf(dev, ETHTOOL_MODULE_FW_FLASH_STATUS_STARTED,
547 params, NULL, NULL, 0, 0);
548 }
549
550 void
ethnl_module_fw_flash_ntf_complete(struct net_device * dev,struct ethnl_module_fw_flash_ntf_params * params)551 ethnl_module_fw_flash_ntf_complete(struct net_device *dev,
552 struct ethnl_module_fw_flash_ntf_params *params)
553 {
554 ethnl_module_fw_flash_ntf(dev, ETHTOOL_MODULE_FW_FLASH_STATUS_COMPLETED,
555 params, NULL, NULL, 0, 0);
556 }
557
558 void
ethnl_module_fw_flash_ntf_in_progress(struct net_device * dev,struct ethnl_module_fw_flash_ntf_params * params,u64 done,u64 total)559 ethnl_module_fw_flash_ntf_in_progress(struct net_device *dev,
560 struct ethnl_module_fw_flash_ntf_params *params,
561 u64 done, u64 total)
562 {
563 ethnl_module_fw_flash_ntf(dev,
564 ETHTOOL_MODULE_FW_FLASH_STATUS_IN_PROGRESS,
565 params, NULL, NULL, done, total);
566 }
567