1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
7 #include <linux/device.h>
8 #include <net/genetlink.h>
10 #include "devl_internal.h"
12 struct devlink_info_req {
14 void (*version_cb)(const char *version_name,
15 enum devlink_info_version_type version_type,
16 void *version_cb_priv);
17 void *version_cb_priv;
20 struct devlink_reload_combination {
21 enum devlink_reload_action action;
22 enum devlink_reload_limit limit;
25 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
27 /* can't reinitialize driver with no down time */
28 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
29 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
34 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
35 enum devlink_reload_limit limit)
39 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
40 if (devlink_reload_invalid_combinations[i].action == action &&
41 devlink_reload_invalid_combinations[i].limit == limit)
47 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
49 return test_bit(action, &devlink->ops->reload_actions);
53 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
55 return test_bit(limit, &devlink->ops->reload_limits);
58 static int devlink_reload_stat_put(struct sk_buff *msg,
59 enum devlink_reload_limit limit, u32 value)
61 struct nlattr *reload_stats_entry;
63 reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
64 if (!reload_stats_entry)
67 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
68 nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
70 nla_nest_end(msg, reload_stats_entry);
74 nla_nest_cancel(msg, reload_stats_entry);
79 devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
81 struct nlattr *reload_stats_attr, *act_info, *act_stats;
86 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
88 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
90 if (!reload_stats_attr)
93 for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
95 !devlink_reload_action_is_supported(devlink, i)) ||
96 i == DEVLINK_RELOAD_ACTION_UNSPEC)
98 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
100 goto nla_put_failure;
102 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
103 goto action_info_nest_cancel;
104 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
106 goto action_info_nest_cancel;
108 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
109 /* Remote stats are shown even if not locally supported.
110 * Stats of actions with unspecified limit are shown
111 * though drivers don't need to register unspecified
114 if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
115 !devlink_reload_limit_is_supported(devlink, j)) ||
116 devlink_reload_combination_is_invalid(i, j))
119 stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
121 value = devlink->stats.reload_stats[stat_idx];
123 value = devlink->stats.remote_reload_stats[stat_idx];
124 if (devlink_reload_stat_put(msg, j, value))
125 goto action_stats_nest_cancel;
127 nla_nest_end(msg, act_stats);
128 nla_nest_end(msg, act_info);
130 nla_nest_end(msg, reload_stats_attr);
133 action_stats_nest_cancel:
134 nla_nest_cancel(msg, act_stats);
135 action_info_nest_cancel:
136 nla_nest_cancel(msg, act_info);
138 nla_nest_cancel(msg, reload_stats_attr);
142 static int devlink_nl_nested_fill(struct sk_buff *msg, struct devlink *devlink)
144 unsigned long rel_index;
148 xa_for_each(&devlink->nested_rels, rel_index, unused) {
149 err = devlink_rel_devlink_handle_put(msg, devlink,
151 DEVLINK_ATTR_NESTED_DEVLINK,
159 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
160 enum devlink_command cmd, u32 portid,
163 struct nlattr *dev_stats;
166 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
170 if (devlink_nl_put_handle(msg, devlink))
171 goto nla_put_failure;
172 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
173 goto nla_put_failure;
175 dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
177 goto nla_put_failure;
179 if (devlink_reload_stats_put(msg, devlink, false))
180 goto dev_stats_nest_cancel;
181 if (devlink_reload_stats_put(msg, devlink, true))
182 goto dev_stats_nest_cancel;
184 nla_nest_end(msg, dev_stats);
186 if (devlink_nl_nested_fill(msg, devlink))
187 goto nla_put_failure;
189 genlmsg_end(msg, hdr);
192 dev_stats_nest_cancel:
193 nla_nest_cancel(msg, dev_stats);
195 genlmsg_cancel(msg, hdr);
199 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
204 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
205 WARN_ON(!devl_is_registered(devlink));
207 if (!devlink_nl_notify_need(devlink))
210 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
214 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
220 devlink_nl_notify_send(devlink, msg);
223 int devlink_nl_get_doit(struct sk_buff *skb, struct genl_info *info)
225 struct devlink *devlink = info->user_ptr[0];
229 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
233 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
234 info->snd_portid, info->snd_seq, 0);
240 return genlmsg_reply(msg, info);
244 devlink_nl_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
245 struct netlink_callback *cb, int flags)
247 return devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
248 NETLINK_CB(cb->skb).portid,
249 cb->nlh->nlmsg_seq, flags);
252 int devlink_nl_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
254 return devlink_nl_dumpit(msg, cb, devlink_nl_get_dump_one);
257 static void devlink_rel_notify_cb(struct devlink *devlink, u32 obj_index)
259 devlink_notify(devlink, DEVLINK_CMD_NEW);
262 static void devlink_rel_cleanup_cb(struct devlink *devlink, u32 obj_index,
265 xa_erase(&devlink->nested_rels, rel_index);
268 int devl_nested_devlink_set(struct devlink *devlink,
269 struct devlink *nested_devlink)
274 err = devlink_rel_nested_in_add(&rel_index, devlink->index, 0,
275 devlink_rel_notify_cb,
276 devlink_rel_cleanup_cb,
280 return xa_insert(&devlink->nested_rels, rel_index,
281 xa_mk_value(0), GFP_KERNEL);
283 EXPORT_SYMBOL_GPL(devl_nested_devlink_set);
285 void devlink_notify_register(struct devlink *devlink)
287 devlink_notify(devlink, DEVLINK_CMD_NEW);
288 devlink_linecards_notify_register(devlink);
289 devlink_ports_notify_register(devlink);
290 devlink_trap_policers_notify_register(devlink);
291 devlink_trap_groups_notify_register(devlink);
292 devlink_traps_notify_register(devlink);
293 devlink_rates_notify_register(devlink);
294 devlink_regions_notify_register(devlink);
295 devlink_params_notify_register(devlink);
298 void devlink_notify_unregister(struct devlink *devlink)
300 devlink_params_notify_unregister(devlink);
301 devlink_regions_notify_unregister(devlink);
302 devlink_rates_notify_unregister(devlink);
303 devlink_traps_notify_unregister(devlink);
304 devlink_trap_groups_notify_unregister(devlink);
305 devlink_trap_policers_notify_unregister(devlink);
306 devlink_ports_notify_unregister(devlink);
307 devlink_linecards_notify_unregister(devlink);
308 devlink_notify(devlink, DEVLINK_CMD_DEL);
311 static void devlink_reload_failed_set(struct devlink *devlink,
314 if (devlink->reload_failed == reload_failed)
316 devlink->reload_failed = reload_failed;
317 devlink_notify(devlink, DEVLINK_CMD_NEW);
320 bool devlink_is_reload_failed(const struct devlink *devlink)
322 return devlink->reload_failed;
324 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
327 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
328 enum devlink_reload_limit limit, u32 actions_performed)
330 unsigned long actions = actions_performed;
334 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
335 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
336 reload_stats[stat_idx]++;
338 devlink_notify(devlink, DEVLINK_CMD_NEW);
342 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
343 u32 actions_performed)
345 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
350 * devlink_remote_reload_actions_performed - Update devlink on reload actions
351 * performed which are not a direct result of devlink reload call.
353 * This should be called by a driver after performing reload actions in case it was not
354 * a result of devlink reload call. For example fw_activate was performed as a result
355 * of devlink reload triggered fw_activate on another host.
356 * The motivation for this function is to keep data on reload actions performed on this
357 * function whether it was done due to direct devlink reload call or not.
360 * @limit: reload limit
361 * @actions_performed: bitmask of actions performed
363 void devlink_remote_reload_actions_performed(struct devlink *devlink,
364 enum devlink_reload_limit limit,
365 u32 actions_performed)
367 if (WARN_ON(!actions_performed ||
368 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
369 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
370 limit > DEVLINK_RELOAD_LIMIT_MAX))
373 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
376 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
378 static struct net *devlink_netns_get(struct sk_buff *skb,
379 struct genl_info *info)
381 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
382 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
383 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
386 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
387 NL_SET_ERR_MSG(info->extack, "multiple netns identifying attributes specified");
388 return ERR_PTR(-EINVAL);
391 if (netns_pid_attr) {
392 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
393 } else if (netns_fd_attr) {
394 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
395 } else if (netns_id_attr) {
396 net = get_net_ns_by_id(sock_net(skb->sk),
397 nla_get_u32(netns_id_attr));
399 net = ERR_PTR(-EINVAL);
402 net = ERR_PTR(-EINVAL);
405 NL_SET_ERR_MSG(info->extack, "Unknown network namespace");
406 return ERR_PTR(-EINVAL);
408 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
410 return ERR_PTR(-EPERM);
415 static void devlink_reload_netns_change(struct devlink *devlink,
416 struct net *curr_net,
417 struct net *dest_net)
419 /* Userspace needs to be notified about devlink objects
420 * removed from original and entering new network namespace.
421 * The rest of the devlink objects are re-created during
422 * reload process so the notifications are generated separatelly.
424 devlink_notify_unregister(devlink);
425 write_pnet(&devlink->_net, dest_net);
426 devlink_notify_register(devlink);
427 devlink_rel_nested_in_notify(devlink);
430 static void devlink_reload_reinit_sanity_check(struct devlink *devlink)
432 WARN_ON(!list_empty(&devlink->trap_policer_list));
433 WARN_ON(!list_empty(&devlink->trap_group_list));
434 WARN_ON(!list_empty(&devlink->trap_list));
435 WARN_ON(!list_empty(&devlink->dpipe_table_list));
436 WARN_ON(!list_empty(&devlink->sb_list));
437 WARN_ON(!list_empty(&devlink->rate_list));
438 WARN_ON(!list_empty(&devlink->linecard_list));
439 WARN_ON(!xa_empty(&devlink->ports));
442 int devlink_reload(struct devlink *devlink, struct net *dest_net,
443 enum devlink_reload_action action,
444 enum devlink_reload_limit limit,
445 u32 *actions_performed, struct netlink_ext_ack *extack)
447 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
448 struct net *curr_net;
451 /* Make sure the reload operations are invoked with the device lock
452 * held to allow drivers to trigger functionality that expects it
453 * (e.g., PCI reset) and to close possible races between these
454 * operations and probe/remove.
456 device_lock_assert(devlink->dev);
458 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
459 sizeof(remote_reload_stats));
461 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
465 curr_net = devlink_net(devlink);
466 if (dest_net && !net_eq(dest_net, curr_net))
467 devlink_reload_netns_change(devlink, curr_net, dest_net);
469 if (action == DEVLINK_RELOAD_ACTION_DRIVER_REINIT) {
470 devlink_params_driverinit_load_new(devlink);
471 devlink_reload_reinit_sanity_check(devlink);
474 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
475 devlink_reload_failed_set(devlink, !!err);
479 WARN_ON(!(*actions_performed & BIT(action)));
480 /* Catch driver on updating the remote action within devlink reload */
481 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
482 sizeof(remote_reload_stats)));
483 devlink_reload_stats_update(devlink, limit, *actions_performed);
488 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
489 enum devlink_command cmd, struct genl_info *info)
494 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
498 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
502 if (devlink_nl_put_handle(msg, devlink))
503 goto nla_put_failure;
505 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
507 goto nla_put_failure;
508 genlmsg_end(msg, hdr);
510 return genlmsg_reply(msg, info);
513 genlmsg_cancel(msg, hdr);
519 int devlink_nl_reload_doit(struct sk_buff *skb, struct genl_info *info)
521 struct devlink *devlink = info->user_ptr[0];
522 enum devlink_reload_action action;
523 enum devlink_reload_limit limit;
524 struct net *dest_net = NULL;
525 u32 actions_performed;
528 err = devlink_resources_validate(devlink, NULL, info);
530 NL_SET_ERR_MSG(info->extack, "resources size validation failed");
534 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
535 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
537 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
539 if (!devlink_reload_action_is_supported(devlink, action)) {
540 NL_SET_ERR_MSG(info->extack, "Requested reload action is not supported by the driver");
544 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
545 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
546 struct nla_bitfield32 limits;
549 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
550 limits_selected = limits.value & limits.selector;
551 if (!limits_selected) {
552 NL_SET_ERR_MSG(info->extack, "Invalid limit selected");
555 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
556 if (limits_selected & BIT(limit))
558 /* UAPI enables multiselection, but currently it is not used */
559 if (limits_selected != BIT(limit)) {
560 NL_SET_ERR_MSG(info->extack, "Multiselection of limit is not supported");
563 if (!devlink_reload_limit_is_supported(devlink, limit)) {
564 NL_SET_ERR_MSG(info->extack, "Requested limit is not supported by the driver");
567 if (devlink_reload_combination_is_invalid(action, limit)) {
568 NL_SET_ERR_MSG(info->extack, "Requested limit is invalid for this action");
572 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
573 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
574 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
575 dest_net = devlink_netns_get(skb, info);
576 if (IS_ERR(dest_net))
577 return PTR_ERR(dest_net);
578 if (!net_eq(dest_net, devlink_net(devlink)) &&
579 action != DEVLINK_RELOAD_ACTION_DRIVER_REINIT) {
580 NL_SET_ERR_MSG_MOD(info->extack,
581 "Changing namespace is only supported for reinit action");
586 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
593 /* For backward compatibility generate reply only if attributes used by user */
594 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
597 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
598 DEVLINK_CMD_RELOAD, info);
601 bool devlink_reload_actions_valid(const struct devlink_ops *ops)
603 const struct devlink_reload_combination *comb;
606 if (!devlink_reload_supported(ops)) {
607 if (WARN_ON(ops->reload_actions))
612 if (WARN_ON(!ops->reload_actions ||
613 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
614 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
617 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
618 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
621 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
622 comb = &devlink_reload_invalid_combinations[i];
623 if (ops->reload_actions == BIT(comb->action) &&
624 ops->reload_limits == BIT(comb->limit))
630 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
631 enum devlink_command cmd, u32 portid,
634 const struct devlink_ops *ops = devlink->ops;
635 enum devlink_eswitch_encap_mode encap_mode;
641 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
645 err = devlink_nl_put_handle(msg, devlink);
647 goto nla_put_failure;
649 if (ops->eswitch_mode_get) {
650 err = ops->eswitch_mode_get(devlink, &mode);
652 goto nla_put_failure;
653 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
655 goto nla_put_failure;
658 if (ops->eswitch_inline_mode_get) {
659 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
661 goto nla_put_failure;
662 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
665 goto nla_put_failure;
668 if (ops->eswitch_encap_mode_get) {
669 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
671 goto nla_put_failure;
672 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
674 goto nla_put_failure;
677 genlmsg_end(msg, hdr);
681 genlmsg_cancel(msg, hdr);
685 int devlink_nl_eswitch_get_doit(struct sk_buff *skb, struct genl_info *info)
687 struct devlink *devlink = info->user_ptr[0];
691 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
695 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
696 info->snd_portid, info->snd_seq, 0);
703 return genlmsg_reply(msg, info);
706 int devlink_nl_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info)
708 struct devlink *devlink = info->user_ptr[0];
709 const struct devlink_ops *ops = devlink->ops;
710 enum devlink_eswitch_encap_mode encap_mode;
715 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
716 if (!ops->eswitch_mode_set)
718 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
719 err = devlink_rate_nodes_check(devlink, mode, info->extack);
722 err = ops->eswitch_mode_set(devlink, mode, info->extack);
727 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
728 if (!ops->eswitch_inline_mode_set)
730 inline_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
731 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
737 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
738 if (!ops->eswitch_encap_mode_set)
740 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
741 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
750 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
754 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
756 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
758 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
763 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
766 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
768 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
769 const char *version_name,
770 const char *version_value,
771 enum devlink_info_version_type version_type)
777 req->version_cb(version_name, version_type,
778 req->version_cb_priv);
783 nest = nla_nest_start_noflag(req->msg, attr);
787 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
790 goto nla_put_failure;
792 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
795 goto nla_put_failure;
797 nla_nest_end(req->msg, nest);
802 nla_nest_cancel(req->msg, nest);
806 int devlink_info_version_fixed_put(struct devlink_info_req *req,
807 const char *version_name,
808 const char *version_value)
810 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
811 version_name, version_value,
812 DEVLINK_INFO_VERSION_TYPE_NONE);
814 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
816 int devlink_info_version_stored_put(struct devlink_info_req *req,
817 const char *version_name,
818 const char *version_value)
820 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
821 version_name, version_value,
822 DEVLINK_INFO_VERSION_TYPE_NONE);
824 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
826 int devlink_info_version_stored_put_ext(struct devlink_info_req *req,
827 const char *version_name,
828 const char *version_value,
829 enum devlink_info_version_type version_type)
831 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
832 version_name, version_value,
835 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put_ext);
837 int devlink_info_version_running_put(struct devlink_info_req *req,
838 const char *version_name,
839 const char *version_value)
841 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
842 version_name, version_value,
843 DEVLINK_INFO_VERSION_TYPE_NONE);
845 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
847 int devlink_info_version_running_put_ext(struct devlink_info_req *req,
848 const char *version_name,
849 const char *version_value,
850 enum devlink_info_version_type version_type)
852 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
853 version_name, version_value,
856 EXPORT_SYMBOL_GPL(devlink_info_version_running_put_ext);
858 static int devlink_nl_driver_info_get(struct device_driver *drv,
859 struct devlink_info_req *req)
865 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME,
872 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
873 enum devlink_command cmd, u32 portid,
874 u32 seq, int flags, struct netlink_ext_ack *extack)
876 struct device *dev = devlink_to_dev(devlink);
877 struct devlink_info_req req = {};
881 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
886 if (devlink_nl_put_handle(msg, devlink))
890 if (devlink->ops->info_get) {
891 err = devlink->ops->info_get(devlink, &req, extack);
896 err = devlink_nl_driver_info_get(dev->driver, &req);
900 genlmsg_end(msg, hdr);
904 genlmsg_cancel(msg, hdr);
908 int devlink_nl_info_get_doit(struct sk_buff *skb, struct genl_info *info)
910 struct devlink *devlink = info->user_ptr[0];
914 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
918 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
919 info->snd_portid, info->snd_seq, 0,
926 return genlmsg_reply(msg, info);
930 devlink_nl_info_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
931 struct netlink_callback *cb, int flags)
935 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
936 NETLINK_CB(cb->skb).portid,
937 cb->nlh->nlmsg_seq, flags,
939 if (err == -EOPNOTSUPP)
944 int devlink_nl_info_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
946 return devlink_nl_dumpit(msg, cb, devlink_nl_info_get_dump_one);
949 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
950 struct devlink *devlink,
951 enum devlink_command cmd,
952 struct devlink_flash_notify *params)
956 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
960 if (devlink_nl_put_handle(msg, devlink))
961 goto nla_put_failure;
963 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
966 if (params->status_msg &&
967 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
969 goto nla_put_failure;
970 if (params->component &&
971 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
973 goto nla_put_failure;
974 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
975 params->done, DEVLINK_ATTR_PAD))
976 goto nla_put_failure;
977 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
978 params->total, DEVLINK_ATTR_PAD))
979 goto nla_put_failure;
980 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
981 params->timeout, DEVLINK_ATTR_PAD))
982 goto nla_put_failure;
985 genlmsg_end(msg, hdr);
989 genlmsg_cancel(msg, hdr);
993 static void __devlink_flash_update_notify(struct devlink *devlink,
994 enum devlink_command cmd,
995 struct devlink_flash_notify *params)
1000 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
1001 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
1002 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
1004 if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink))
1007 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1011 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
1015 devlink_nl_notify_send(devlink, msg);
1022 static void devlink_flash_update_begin_notify(struct devlink *devlink)
1024 struct devlink_flash_notify params = {};
1026 __devlink_flash_update_notify(devlink,
1027 DEVLINK_CMD_FLASH_UPDATE,
1031 static void devlink_flash_update_end_notify(struct devlink *devlink)
1033 struct devlink_flash_notify params = {};
1035 __devlink_flash_update_notify(devlink,
1036 DEVLINK_CMD_FLASH_UPDATE_END,
1040 void devlink_flash_update_status_notify(struct devlink *devlink,
1041 const char *status_msg,
1042 const char *component,
1044 unsigned long total)
1046 struct devlink_flash_notify params = {
1047 .status_msg = status_msg,
1048 .component = component,
1053 __devlink_flash_update_notify(devlink,
1054 DEVLINK_CMD_FLASH_UPDATE_STATUS,
1057 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
1059 void devlink_flash_update_timeout_notify(struct devlink *devlink,
1060 const char *status_msg,
1061 const char *component,
1062 unsigned long timeout)
1064 struct devlink_flash_notify params = {
1065 .status_msg = status_msg,
1066 .component = component,
1070 __devlink_flash_update_notify(devlink,
1071 DEVLINK_CMD_FLASH_UPDATE_STATUS,
1074 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
1076 struct devlink_flash_component_lookup_ctx {
1077 const char *lookup_name;
1078 bool lookup_name_found;
1082 devlink_flash_component_lookup_cb(const char *version_name,
1083 enum devlink_info_version_type version_type,
1084 void *version_cb_priv)
1086 struct devlink_flash_component_lookup_ctx *lookup_ctx = version_cb_priv;
1088 if (version_type != DEVLINK_INFO_VERSION_TYPE_COMPONENT ||
1089 lookup_ctx->lookup_name_found)
1092 lookup_ctx->lookup_name_found =
1093 !strcmp(lookup_ctx->lookup_name, version_name);
1096 static int devlink_flash_component_get(struct devlink *devlink,
1097 struct nlattr *nla_component,
1098 const char **p_component,
1099 struct netlink_ext_ack *extack)
1101 struct devlink_flash_component_lookup_ctx lookup_ctx = {};
1102 struct devlink_info_req req = {};
1103 const char *component;
1109 component = nla_data(nla_component);
1111 if (!devlink->ops->info_get) {
1112 NL_SET_ERR_MSG_ATTR(extack, nla_component,
1113 "component update is not supported by this device");
1117 lookup_ctx.lookup_name = component;
1118 req.version_cb = devlink_flash_component_lookup_cb;
1119 req.version_cb_priv = &lookup_ctx;
1121 ret = devlink->ops->info_get(devlink, &req, NULL);
1125 if (!lookup_ctx.lookup_name_found) {
1126 NL_SET_ERR_MSG_ATTR(extack, nla_component,
1127 "selected component is not supported by this device");
1130 *p_component = component;
1134 int devlink_nl_flash_update_doit(struct sk_buff *skb, struct genl_info *info)
1136 struct nlattr *nla_overwrite_mask, *nla_file_name;
1137 struct devlink_flash_update_params params = {};
1138 struct devlink *devlink = info->user_ptr[0];
1139 const char *file_name;
1140 u32 supported_params;
1143 if (!devlink->ops->flash_update)
1146 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME))
1149 ret = devlink_flash_component_get(devlink,
1150 info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT],
1151 ¶ms.component, info->extack);
1155 supported_params = devlink->ops->supported_flash_update_params;
1157 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
1158 if (nla_overwrite_mask) {
1159 struct nla_bitfield32 sections;
1161 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
1162 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
1163 "overwrite settings are not supported by this device");
1166 sections = nla_get_bitfield32(nla_overwrite_mask);
1167 params.overwrite_mask = sections.value & sections.selector;
1170 nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
1171 file_name = nla_data(nla_file_name);
1172 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
1174 NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name,
1175 "failed to locate the requested firmware file");
1179 devlink_flash_update_begin_notify(devlink);
1180 ret = devlink->ops->flash_update(devlink, ¶ms, info->extack);
1181 devlink_flash_update_end_notify(devlink);
1183 release_firmware(params.fw);
1188 static void __devlink_compat_running_version(struct devlink *devlink,
1189 char *buf, size_t len)
1191 struct devlink_info_req req = {};
1192 const struct nlattr *nlattr;
1193 struct sk_buff *msg;
1196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1201 err = devlink->ops->info_get(devlink, &req, NULL);
1205 nla_for_each_attr_type(nlattr, DEVLINK_ATTR_INFO_VERSION_RUNNING,
1206 (void *)msg->data, msg->len, rem) {
1207 const struct nlattr *kv;
1210 nla_for_each_nested_type(kv, DEVLINK_ATTR_INFO_VERSION_VALUE,
1212 strlcat(buf, nla_data(kv), len);
1213 strlcat(buf, " ", len);
1220 void devlink_compat_running_version(struct devlink *devlink,
1221 char *buf, size_t len)
1223 if (!devlink->ops->info_get)
1227 if (devl_is_registered(devlink))
1228 __devlink_compat_running_version(devlink, buf, len);
1229 devl_unlock(devlink);
1232 int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
1234 struct devlink_flash_update_params params = {};
1238 if (!devl_is_registered(devlink)) {
1243 if (!devlink->ops->flash_update) {
1248 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
1252 devlink_flash_update_begin_notify(devlink);
1253 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
1254 devlink_flash_update_end_notify(devlink);
1256 release_firmware(params.fw);
1258 devl_unlock(devlink);
1264 devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,
1265 u32 portid, u32 seq, int flags,
1266 struct netlink_ext_ack *extack)
1268 struct nlattr *selftests;
1273 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags,
1274 DEVLINK_CMD_SELFTESTS_GET);
1279 if (devlink_nl_put_handle(msg, devlink))
1280 goto err_cancel_msg;
1282 selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
1284 goto err_cancel_msg;
1286 for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
1287 i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
1288 if (devlink->ops->selftest_check(devlink, i, extack)) {
1289 err = nla_put_flag(msg, i);
1291 goto err_cancel_msg;
1295 nla_nest_end(msg, selftests);
1296 genlmsg_end(msg, hdr);
1300 genlmsg_cancel(msg, hdr);
1304 int devlink_nl_selftests_get_doit(struct sk_buff *skb, struct genl_info *info)
1306 struct devlink *devlink = info->user_ptr[0];
1307 struct sk_buff *msg;
1310 if (!devlink->ops->selftest_check)
1313 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1317 err = devlink_nl_selftests_fill(msg, devlink, info->snd_portid,
1318 info->snd_seq, 0, info->extack);
1324 return genlmsg_reply(msg, info);
1327 static int devlink_nl_selftests_get_dump_one(struct sk_buff *msg,
1328 struct devlink *devlink,
1329 struct netlink_callback *cb,
1332 if (!devlink->ops->selftest_check)
1335 return devlink_nl_selftests_fill(msg, devlink,
1336 NETLINK_CB(cb->skb).portid,
1337 cb->nlh->nlmsg_seq, flags,
1341 int devlink_nl_selftests_get_dumpit(struct sk_buff *skb,
1342 struct netlink_callback *cb)
1344 return devlink_nl_dumpit(skb, cb, devlink_nl_selftests_get_dump_one);
1347 static int devlink_selftest_result_put(struct sk_buff *skb, unsigned int id,
1348 enum devlink_selftest_status test_status)
1350 struct nlattr *result_attr;
1352 result_attr = nla_nest_start(skb, DEVLINK_ATTR_SELFTEST_RESULT);
1356 if (nla_put_u32(skb, DEVLINK_ATTR_SELFTEST_RESULT_ID, id) ||
1357 nla_put_u8(skb, DEVLINK_ATTR_SELFTEST_RESULT_STATUS,
1359 goto nla_put_failure;
1361 nla_nest_end(skb, result_attr);
1365 nla_nest_cancel(skb, result_attr);
1369 static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_ID_MAX + 1] = {
1370 [DEVLINK_ATTR_SELFTEST_ID_FLASH] = { .type = NLA_FLAG },
1373 int devlink_nl_selftests_run_doit(struct sk_buff *skb, struct genl_info *info)
1375 struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
1376 struct devlink *devlink = info->user_ptr[0];
1377 struct nlattr *attrs, *selftests;
1378 struct sk_buff *msg;
1383 if (!devlink->ops->selftest_run || !devlink->ops->selftest_check)
1386 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SELFTESTS))
1389 attrs = info->attrs[DEVLINK_ATTR_SELFTESTS];
1391 err = nla_parse_nested(tb, DEVLINK_ATTR_SELFTEST_ID_MAX, attrs,
1392 devlink_selftest_nl_policy, info->extack);
1396 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1401 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1402 &devlink_nl_family, 0, DEVLINK_CMD_SELFTESTS_RUN);
1406 if (devlink_nl_put_handle(msg, devlink))
1407 goto genlmsg_cancel;
1409 selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
1411 goto genlmsg_cancel;
1413 for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
1414 i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
1415 enum devlink_selftest_status test_status;
1417 if (nla_get_flag(tb[i])) {
1418 if (!devlink->ops->selftest_check(devlink, i,
1420 if (devlink_selftest_result_put(msg, i,
1421 DEVLINK_SELFTEST_STATUS_SKIP))
1422 goto selftests_nest_cancel;
1426 test_status = devlink->ops->selftest_run(devlink, i,
1428 if (devlink_selftest_result_put(msg, i, test_status))
1429 goto selftests_nest_cancel;
1433 nla_nest_end(msg, selftests);
1434 genlmsg_end(msg, hdr);
1435 return genlmsg_reply(msg, info);
1437 selftests_nest_cancel:
1438 nla_nest_cancel(msg, selftests);
1440 genlmsg_cancel(msg, hdr);