Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
   4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
   5 */
   6
   7#include <linux/device.h>
   8#include <net/genetlink.h>
   9#include <net/sock.h>
  10#include "devl_internal.h"
  11
  12struct devlink_info_req {
  13	struct sk_buff *msg;
  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;
  18};
  19
  20struct devlink_reload_combination {
  21	enum devlink_reload_action action;
  22	enum devlink_reload_limit limit;
  23};
  24
  25static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
  26	{
  27		/* can't reinitialize driver with no down time */
  28		.action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
  29		.limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
  30	},
  31};
  32
  33static bool
  34devlink_reload_combination_is_invalid(enum devlink_reload_action action,
  35				      enum devlink_reload_limit limit)
  36{
  37	int i;
  38
  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)
  42			return true;
  43	return false;
  44}
  45
  46static bool
  47devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
  48{
  49	return test_bit(action, &devlink->ops->reload_actions);
  50}
  51
  52static bool
  53devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
  54{
  55	return test_bit(limit, &devlink->ops->reload_limits);
  56}
  57
  58static int devlink_reload_stat_put(struct sk_buff *msg,
  59				   enum devlink_reload_limit limit, u32 value)
  60{
  61	struct nlattr *reload_stats_entry;
  62
  63	reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
  64	if (!reload_stats_entry)
  65		return -EMSGSIZE;
  66
  67	if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
  68	    nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
  69		goto nla_put_failure;
  70	nla_nest_end(msg, reload_stats_entry);
  71	return 0;
  72
  73nla_put_failure:
  74	nla_nest_cancel(msg, reload_stats_entry);
  75	return -EMSGSIZE;
  76}
  77
  78static int
  79devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
  80{
  81	struct nlattr *reload_stats_attr, *act_info, *act_stats;
  82	int i, j, stat_idx;
  83	u32 value;
  84
  85	if (!is_remote)
  86		reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
  87	else
  88		reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
  89
  90	if (!reload_stats_attr)
  91		return -EMSGSIZE;
  92
  93	for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
  94		if ((!is_remote &&
  95		     !devlink_reload_action_is_supported(devlink, i)) ||
  96		    i == DEVLINK_RELOAD_ACTION_UNSPEC)
  97			continue;
  98		act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
  99		if (!act_info)
 100			goto nla_put_failure;
 101
 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);
 105		if (!act_stats)
 106			goto action_info_nest_cancel;
 107
 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
 112			 * limit.
 113			 */
 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))
 117				continue;
 118
 119			stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
 120			if (!is_remote)
 121				value = devlink->stats.reload_stats[stat_idx];
 122			else
 123				value = devlink->stats.remote_reload_stats[stat_idx];
 124			if (devlink_reload_stat_put(msg, j, value))
 125				goto action_stats_nest_cancel;
 126		}
 127		nla_nest_end(msg, act_stats);
 128		nla_nest_end(msg, act_info);
 129	}
 130	nla_nest_end(msg, reload_stats_attr);
 131	return 0;
 132
 133action_stats_nest_cancel:
 134	nla_nest_cancel(msg, act_stats);
 135action_info_nest_cancel:
 136	nla_nest_cancel(msg, act_info);
 137nla_put_failure:
 138	nla_nest_cancel(msg, reload_stats_attr);
 139	return -EMSGSIZE;
 140}
 141
 142static int devlink_nl_nested_fill(struct sk_buff *msg, struct devlink *devlink)
 143{
 144	unsigned long rel_index;
 145	void *unused;
 146	int err;
 147
 148	xa_for_each(&devlink->nested_rels, rel_index, unused) {
 149		err = devlink_rel_devlink_handle_put(msg, devlink,
 150						     rel_index,
 151						     DEVLINK_ATTR_NESTED_DEVLINK,
 152						     NULL);
 153		if (err)
 154			return err;
 155	}
 156	return 0;
 157}
 158
 159static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
 160			   enum devlink_command cmd, u32 portid,
 161			   u32 seq, int flags)
 162{
 163	struct nlattr *dev_stats;
 164	void *hdr;
 165
 166	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
 167	if (!hdr)
 168		return -EMSGSIZE;
 169
 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;
 174
 175	dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
 176	if (!dev_stats)
 177		goto nla_put_failure;
 178
 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;
 183
 184	nla_nest_end(msg, dev_stats);
 185
 186	if (devlink_nl_nested_fill(msg, devlink))
 187		goto nla_put_failure;
 188
 189	genlmsg_end(msg, hdr);
 190	return 0;
 191
 192dev_stats_nest_cancel:
 193	nla_nest_cancel(msg, dev_stats);
 194nla_put_failure:
 195	genlmsg_cancel(msg, hdr);
 196	return -EMSGSIZE;
 197}
 198
 199static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
 200{
 201	struct sk_buff *msg;
 202	int err;
 203
 204	WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
 205	WARN_ON(!devl_is_registered(devlink));
 206
 207	if (!devlink_nl_notify_need(devlink))
 208		return;
 209
 210	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 211	if (!msg)
 212		return;
 213
 214	err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
 215	if (err) {
 216		nlmsg_free(msg);
 217		return;
 218	}
 219
 220	devlink_nl_notify_send(devlink, msg);
 221}
 222
 223int devlink_nl_get_doit(struct sk_buff *skb, struct genl_info *info)
 224{
 225	struct devlink *devlink = info->user_ptr[0];
 226	struct sk_buff *msg;
 227	int err;
 228
 229	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 230	if (!msg)
 231		return -ENOMEM;
 232
 233	err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
 234			      info->snd_portid, info->snd_seq, 0);
 235	if (err) {
 236		nlmsg_free(msg);
 237		return err;
 238	}
 239
 240	return genlmsg_reply(msg, info);
 241}
 242
 243static int
 244devlink_nl_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
 245			struct netlink_callback *cb, int flags)
 246{
 247	return devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
 248			       NETLINK_CB(cb->skb).portid,
 249			       cb->nlh->nlmsg_seq, flags);
 250}
 251
 252int devlink_nl_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
 253{
 254	return devlink_nl_dumpit(msg, cb, devlink_nl_get_dump_one);
 255}
 256
 257static void devlink_rel_notify_cb(struct devlink *devlink, u32 obj_index)
 258{
 259	devlink_notify(devlink, DEVLINK_CMD_NEW);
 260}
 261
 262static void devlink_rel_cleanup_cb(struct devlink *devlink, u32 obj_index,
 263				   u32 rel_index)
 264{
 265	xa_erase(&devlink->nested_rels, rel_index);
 266}
 267
 268int devl_nested_devlink_set(struct devlink *devlink,
 269			    struct devlink *nested_devlink)
 270{
 271	u32 rel_index;
 272	int err;
 273
 274	err = devlink_rel_nested_in_add(&rel_index, devlink->index, 0,
 275					devlink_rel_notify_cb,
 276					devlink_rel_cleanup_cb,
 277					nested_devlink);
 278	if (err)
 279		return err;
 280	return xa_insert(&devlink->nested_rels, rel_index,
 281			 xa_mk_value(0), GFP_KERNEL);
 282}
 283EXPORT_SYMBOL_GPL(devl_nested_devlink_set);
 284
 285void devlink_notify_register(struct devlink *devlink)
 286{
 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);
 296}
 297
 298void devlink_notify_unregister(struct devlink *devlink)
 299{
 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);
 309}
 310
 311static void devlink_reload_failed_set(struct devlink *devlink,
 312				      bool reload_failed)
 313{
 314	if (devlink->reload_failed == reload_failed)
 315		return;
 316	devlink->reload_failed = reload_failed;
 317	devlink_notify(devlink, DEVLINK_CMD_NEW);
 318}
 319
 320bool devlink_is_reload_failed(const struct devlink *devlink)
 321{
 322	return devlink->reload_failed;
 323}
 324EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
 325
 326static void
 327__devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
 328			      enum devlink_reload_limit limit, u32 actions_performed)
 329{
 330	unsigned long actions = actions_performed;
 331	int stat_idx;
 332	int action;
 333
 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]++;
 337	}
 338	devlink_notify(devlink, DEVLINK_CMD_NEW);
 339}
 340
 341static void
 342devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
 343			    u32 actions_performed)
 344{
 345	__devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
 346				      actions_performed);
 347}
 348
 349/**
 350 *	devlink_remote_reload_actions_performed - Update devlink on reload actions
 351 *	  performed which are not a direct result of devlink reload call.
 352 *
 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.
 358 *
 359 *	@devlink: devlink
 360 *	@limit: reload limit
 361 *	@actions_performed: bitmask of actions performed
 362 */
 363void devlink_remote_reload_actions_performed(struct devlink *devlink,
 364					     enum devlink_reload_limit limit,
 365					     u32 actions_performed)
 366{
 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))
 371		return;
 372
 373	__devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
 374				      actions_performed);
 375}
 376EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
 377
 378static struct net *devlink_netns_get(struct sk_buff *skb,
 379				     struct genl_info *info)
 380{
 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];
 384	struct net *net;
 385
 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);
 389	}
 390
 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));
 398		if (!net)
 399			net = ERR_PTR(-EINVAL);
 400	} else {
 401		WARN_ON(1);
 402		net = ERR_PTR(-EINVAL);
 403	}
 404	if (IS_ERR(net)) {
 405		NL_SET_ERR_MSG(info->extack, "Unknown network namespace");
 406		return ERR_PTR(-EINVAL);
 407	}
 408	if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
 409		put_net(net);
 410		return ERR_PTR(-EPERM);
 411	}
 412	return net;
 413}
 414
 415static void devlink_reload_netns_change(struct devlink *devlink,
 416					struct net *curr_net,
 417					struct net *dest_net)
 418{
 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.
 423	 */
 424	devlink_notify_unregister(devlink);
 425	write_pnet(&devlink->_net, dest_net);
 426	devlink_notify_register(devlink);
 427	devlink_rel_nested_in_notify(devlink);
 428}
 429
 430static void devlink_reload_reinit_sanity_check(struct devlink *devlink)
 431{
 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));
 440}
 441
 442int 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)
 446{
 447	u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
 448	struct net *curr_net;
 449	int err;
 450
 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.
 455	 */
 456	device_lock_assert(devlink->dev);
 457
 458	memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
 459	       sizeof(remote_reload_stats));
 460
 461	err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
 462	if (err)
 463		return err;
 464
 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);
 468
 469	if (action == DEVLINK_RELOAD_ACTION_DRIVER_REINIT) {
 470		devlink_params_driverinit_load_new(devlink);
 471		devlink_reload_reinit_sanity_check(devlink);
 472	}
 473
 474	err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
 475	devlink_reload_failed_set(devlink, !!err);
 476	if (err)
 477		return err;
 478
 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);
 484	return 0;
 485}
 486
 487static int
 488devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
 489					enum devlink_command cmd, struct genl_info *info)
 490{
 491	struct sk_buff *msg;
 492	void *hdr;
 493
 494	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 495	if (!msg)
 496		return -ENOMEM;
 497
 498	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
 499	if (!hdr)
 500		goto free_msg;
 501
 502	if (devlink_nl_put_handle(msg, devlink))
 503		goto nla_put_failure;
 504
 505	if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
 506			       actions_performed))
 507		goto nla_put_failure;
 508	genlmsg_end(msg, hdr);
 509
 510	return genlmsg_reply(msg, info);
 511
 512nla_put_failure:
 513	genlmsg_cancel(msg, hdr);
 514free_msg:
 515	nlmsg_free(msg);
 516	return -EMSGSIZE;
 517}
 518
 519int devlink_nl_reload_doit(struct sk_buff *skb, struct genl_info *info)
 520{
 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;
 526	int err;
 527
 528	err = devlink_resources_validate(devlink, NULL, info);
 529	if (err) {
 530		NL_SET_ERR_MSG(info->extack, "resources size validation failed");
 531		return err;
 532	}
 533
 534	if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
 535		action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
 536	else
 537		action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
 538
 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");
 541		return -EOPNOTSUPP;
 542	}
 543
 544	limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
 545	if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
 546		struct nla_bitfield32 limits;
 547		u32 limits_selected;
 548
 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");
 553			return -EINVAL;
 554		}
 555		for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
 556			if (limits_selected & BIT(limit))
 557				break;
 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");
 561			return -EOPNOTSUPP;
 562		}
 563		if (!devlink_reload_limit_is_supported(devlink, limit)) {
 564			NL_SET_ERR_MSG(info->extack, "Requested limit is not supported by the driver");
 565			return -EOPNOTSUPP;
 566		}
 567		if (devlink_reload_combination_is_invalid(action, limit)) {
 568			NL_SET_ERR_MSG(info->extack, "Requested limit is invalid for this action");
 569			return -EINVAL;
 570		}
 571	}
 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");
 582			return -EOPNOTSUPP;
 583		}
 584	}
 585
 586	err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
 587
 588	if (dest_net)
 589		put_net(dest_net);
 590
 591	if (err)
 592		return err;
 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])
 595		return 0;
 596
 597	return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
 598						       DEVLINK_CMD_RELOAD, info);
 599}
 600
 601bool devlink_reload_actions_valid(const struct devlink_ops *ops)
 602{
 603	const struct devlink_reload_combination *comb;
 604	int i;
 605
 606	if (!devlink_reload_supported(ops)) {
 607		if (WARN_ON(ops->reload_actions))
 608			return false;
 609		return true;
 610	}
 611
 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)))
 615		return false;
 616
 617	if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
 618		    ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
 619		return false;
 620
 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))
 625			return false;
 626	}
 627	return true;
 628}
 629
 630static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
 631				   enum devlink_command cmd, u32 portid,
 632				   u32 seq, int flags)
 633{
 634	const struct devlink_ops *ops = devlink->ops;
 635	enum devlink_eswitch_encap_mode encap_mode;
 636	u8 inline_mode;
 637	void *hdr;
 638	int err = 0;
 639	u16 mode;
 640
 641	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
 642	if (!hdr)
 643		return -EMSGSIZE;
 644
 645	err = devlink_nl_put_handle(msg, devlink);
 646	if (err)
 647		goto nla_put_failure;
 648
 649	if (ops->eswitch_mode_get) {
 650		err = ops->eswitch_mode_get(devlink, &mode);
 651		if (err)
 652			goto nla_put_failure;
 653		err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
 654		if (err)
 655			goto nla_put_failure;
 656	}
 657
 658	if (ops->eswitch_inline_mode_get) {
 659		err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
 660		if (err)
 661			goto nla_put_failure;
 662		err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
 663				 inline_mode);
 664		if (err)
 665			goto nla_put_failure;
 666	}
 667
 668	if (ops->eswitch_encap_mode_get) {
 669		err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
 670		if (err)
 671			goto nla_put_failure;
 672		err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
 673		if (err)
 674			goto nla_put_failure;
 675	}
 676
 677	genlmsg_end(msg, hdr);
 678	return 0;
 679
 680nla_put_failure:
 681	genlmsg_cancel(msg, hdr);
 682	return err;
 683}
 684
 685int devlink_nl_eswitch_get_doit(struct sk_buff *skb, struct genl_info *info)
 686{
 687	struct devlink *devlink = info->user_ptr[0];
 688	struct sk_buff *msg;
 689	int err;
 690
 691	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 692	if (!msg)
 693		return -ENOMEM;
 694
 695	err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
 696				      info->snd_portid, info->snd_seq, 0);
 697
 698	if (err) {
 699		nlmsg_free(msg);
 700		return err;
 701	}
 702
 703	return genlmsg_reply(msg, info);
 704}
 705
 706int devlink_nl_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info)
 707{
 708	struct devlink *devlink = info->user_ptr[0];
 709	const struct devlink_ops *ops = devlink->ops;
 710	enum devlink_eswitch_encap_mode encap_mode;
 711	u8 inline_mode;
 712	int err = 0;
 713	u16 mode;
 714
 715	if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
 716		if (!ops->eswitch_mode_set)
 717			return -EOPNOTSUPP;
 718		mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
 719		err = devlink_rate_nodes_check(devlink, mode, info->extack);
 720		if (err)
 721			return err;
 722		err = ops->eswitch_mode_set(devlink, mode, info->extack);
 723		if (err)
 724			return err;
 725	}
 726
 727	if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
 728		if (!ops->eswitch_inline_mode_set)
 729			return -EOPNOTSUPP;
 730		inline_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
 731		err = ops->eswitch_inline_mode_set(devlink, inline_mode,
 732						   info->extack);
 733		if (err)
 734			return err;
 735	}
 736
 737	if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
 738		if (!ops->eswitch_encap_mode_set)
 739			return -EOPNOTSUPP;
 740		encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
 741		err = ops->eswitch_encap_mode_set(devlink, encap_mode,
 742						  info->extack);
 743		if (err)
 744			return err;
 745	}
 746
 747	return 0;
 748}
 749
 750int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
 751{
 752	if (!req->msg)
 753		return 0;
 754	return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
 755}
 756EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
 757
 758int devlink_info_board_serial_number_put(struct devlink_info_req *req,
 759					 const char *bsn)
 760{
 761	if (!req->msg)
 762		return 0;
 763	return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
 764			      bsn);
 765}
 766EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
 767
 768static 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)
 772{
 773	struct nlattr *nest;
 774	int err;
 775
 776	if (req->version_cb)
 777		req->version_cb(version_name, version_type,
 778				req->version_cb_priv);
 779
 780	if (!req->msg)
 781		return 0;
 782
 783	nest = nla_nest_start_noflag(req->msg, attr);
 784	if (!nest)
 785		return -EMSGSIZE;
 786
 787	err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
 788			     version_name);
 789	if (err)
 790		goto nla_put_failure;
 791
 792	err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
 793			     version_value);
 794	if (err)
 795		goto nla_put_failure;
 796
 797	nla_nest_end(req->msg, nest);
 798
 799	return 0;
 800
 801nla_put_failure:
 802	nla_nest_cancel(req->msg, nest);
 803	return err;
 804}
 805
 806int devlink_info_version_fixed_put(struct devlink_info_req *req,
 807				   const char *version_name,
 808				   const char *version_value)
 809{
 810	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
 811					version_name, version_value,
 812					DEVLINK_INFO_VERSION_TYPE_NONE);
 813}
 814EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
 815
 816int devlink_info_version_stored_put(struct devlink_info_req *req,
 817				    const char *version_name,
 818				    const char *version_value)
 819{
 820	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
 821					version_name, version_value,
 822					DEVLINK_INFO_VERSION_TYPE_NONE);
 823}
 824EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
 825
 826int 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)
 830{
 831	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
 832					version_name, version_value,
 833					version_type);
 834}
 835EXPORT_SYMBOL_GPL(devlink_info_version_stored_put_ext);
 836
 837int devlink_info_version_running_put(struct devlink_info_req *req,
 838				     const char *version_name,
 839				     const char *version_value)
 840{
 841	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
 842					version_name, version_value,
 843					DEVLINK_INFO_VERSION_TYPE_NONE);
 844}
 845EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
 846
 847int 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)
 851{
 852	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
 853					version_name, version_value,
 854					version_type);
 855}
 856EXPORT_SYMBOL_GPL(devlink_info_version_running_put_ext);
 857
 858static int devlink_nl_driver_info_get(struct device_driver *drv,
 859				      struct devlink_info_req *req)
 860{
 861	if (!drv)
 862		return 0;
 863
 864	if (drv->name[0])
 865		return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME,
 866				      drv->name);
 867
 868	return 0;
 869}
 870
 871static int
 872devlink_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)
 875{
 876	struct device *dev = devlink_to_dev(devlink);
 877	struct devlink_info_req req = {};
 878	void *hdr;
 879	int err;
 880
 881	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
 882	if (!hdr)
 883		return -EMSGSIZE;
 884
 885	err = -EMSGSIZE;
 886	if (devlink_nl_put_handle(msg, devlink))
 887		goto err_cancel_msg;
 888
 889	req.msg = msg;
 890	if (devlink->ops->info_get) {
 891		err = devlink->ops->info_get(devlink, &req, extack);
 892		if (err)
 893			goto err_cancel_msg;
 894	}
 895
 896	err = devlink_nl_driver_info_get(dev->driver, &req);
 897	if (err)
 898		goto err_cancel_msg;
 899
 900	genlmsg_end(msg, hdr);
 901	return 0;
 902
 903err_cancel_msg:
 904	genlmsg_cancel(msg, hdr);
 905	return err;
 906}
 907
 908int devlink_nl_info_get_doit(struct sk_buff *skb, struct genl_info *info)
 909{
 910	struct devlink *devlink = info->user_ptr[0];
 911	struct sk_buff *msg;
 912	int err;
 913
 914	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 915	if (!msg)
 916		return -ENOMEM;
 917
 918	err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
 919				   info->snd_portid, info->snd_seq, 0,
 920				   info->extack);
 921	if (err) {
 922		nlmsg_free(msg);
 923		return err;
 924	}
 925
 926	return genlmsg_reply(msg, info);
 927}
 928
 929static int
 930devlink_nl_info_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
 931			     struct netlink_callback *cb, int flags)
 932{
 933	int err;
 934
 935	err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
 936				   NETLINK_CB(cb->skb).portid,
 937				   cb->nlh->nlmsg_seq, flags,
 938				   cb->extack);
 939	if (err == -EOPNOTSUPP)
 940		err = 0;
 941	return err;
 942}
 943
 944int devlink_nl_info_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
 945{
 946	return devlink_nl_dumpit(msg, cb, devlink_nl_info_get_dump_one);
 947}
 948
 949static 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)
 953{
 954	void *hdr;
 955
 956	hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
 957	if (!hdr)
 958		return -EMSGSIZE;
 959
 960	if (devlink_nl_put_handle(msg, devlink))
 961		goto nla_put_failure;
 962
 963	if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
 964		goto out;
 965
 966	if (params->status_msg &&
 967	    nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
 968			   params->status_msg))
 969		goto nla_put_failure;
 970	if (params->component &&
 971	    nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
 972			   params->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;
 983
 984out:
 985	genlmsg_end(msg, hdr);
 986	return 0;
 987
 988nla_put_failure:
 989	genlmsg_cancel(msg, hdr);
 990	return -EMSGSIZE;
 991}
 992
 993static void __devlink_flash_update_notify(struct devlink *devlink,
 994					  enum devlink_command cmd,
 995					  struct devlink_flash_notify *params)
 996{
 997	struct sk_buff *msg;
 998	int err;
 999
1000	WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
1001		cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
1002		cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
1003
1004	if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink))
1005		return;
1006
1007	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1008	if (!msg)
1009		return;
1010
1011	err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
1012	if (err)
1013		goto out_free_msg;
1014
1015	devlink_nl_notify_send(devlink, msg);
1016	return;
1017
1018out_free_msg:
1019	nlmsg_free(msg);
1020}
1021
1022static void devlink_flash_update_begin_notify(struct devlink *devlink)
1023{
1024	struct devlink_flash_notify params = {};
1025
1026	__devlink_flash_update_notify(devlink,
1027				      DEVLINK_CMD_FLASH_UPDATE,
1028				      &params);
1029}
1030
1031static void devlink_flash_update_end_notify(struct devlink *devlink)
1032{
1033	struct devlink_flash_notify params = {};
1034
1035	__devlink_flash_update_notify(devlink,
1036				      DEVLINK_CMD_FLASH_UPDATE_END,
1037				      &params);
1038}
1039
1040void devlink_flash_update_status_notify(struct devlink *devlink,
1041					const char *status_msg,
1042					const char *component,
1043					unsigned long done,
1044					unsigned long total)
1045{
1046	struct devlink_flash_notify params = {
1047		.status_msg = status_msg,
1048		.component = component,
1049		.done = done,
1050		.total = total,
1051	};
1052
1053	__devlink_flash_update_notify(devlink,
1054				      DEVLINK_CMD_FLASH_UPDATE_STATUS,
1055				      &params);
1056}
1057EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
1058
1059void devlink_flash_update_timeout_notify(struct devlink *devlink,
1060					 const char *status_msg,
1061					 const char *component,
1062					 unsigned long timeout)
1063{
1064	struct devlink_flash_notify params = {
1065		.status_msg = status_msg,
1066		.component = component,
1067		.timeout = timeout,
1068	};
1069
1070	__devlink_flash_update_notify(devlink,
1071				      DEVLINK_CMD_FLASH_UPDATE_STATUS,
1072				      &params);
1073}
1074EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
1075
1076struct devlink_flash_component_lookup_ctx {
1077	const char *lookup_name;
1078	bool lookup_name_found;
1079};
1080
1081static void
1082devlink_flash_component_lookup_cb(const char *version_name,
1083				  enum devlink_info_version_type version_type,
1084				  void *version_cb_priv)
1085{
1086	struct devlink_flash_component_lookup_ctx *lookup_ctx = version_cb_priv;
1087
1088	if (version_type != DEVLINK_INFO_VERSION_TYPE_COMPONENT ||
1089	    lookup_ctx->lookup_name_found)
1090		return;
1091
1092	lookup_ctx->lookup_name_found =
1093		!strcmp(lookup_ctx->lookup_name, version_name);
1094}
1095
1096static int devlink_flash_component_get(struct devlink *devlink,
1097				       struct nlattr *nla_component,
1098				       const char **p_component,
1099				       struct netlink_ext_ack *extack)
1100{
1101	struct devlink_flash_component_lookup_ctx lookup_ctx = {};
1102	struct devlink_info_req req = {};
1103	const char *component;
1104	int ret;
1105
1106	if (!nla_component)
1107		return 0;
1108
1109	component = nla_data(nla_component);
1110
1111	if (!devlink->ops->info_get) {
1112		NL_SET_ERR_MSG_ATTR(extack, nla_component,
1113				    "component update is not supported by this device");
1114		return -EOPNOTSUPP;
1115	}
1116
1117	lookup_ctx.lookup_name = component;
1118	req.version_cb = devlink_flash_component_lookup_cb;
1119	req.version_cb_priv = &lookup_ctx;
1120
1121	ret = devlink->ops->info_get(devlink, &req, NULL);
1122	if (ret)
1123		return ret;
1124
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");
1128		return -EINVAL;
1129	}
1130	*p_component = component;
1131	return 0;
1132}
1133
1134int devlink_nl_flash_update_doit(struct sk_buff *skb, struct genl_info *info)
1135{
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;
1141	int ret;
1142
1143	if (!devlink->ops->flash_update)
1144		return -EOPNOTSUPP;
1145
1146	if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME))
1147		return -EINVAL;
1148
1149	ret = devlink_flash_component_get(devlink,
1150					  info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT],
1151					  &params.component, info->extack);
1152	if (ret)
1153		return ret;
1154
1155	supported_params = devlink->ops->supported_flash_update_params;
1156
1157	nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
1158	if (nla_overwrite_mask) {
1159		struct nla_bitfield32 sections;
1160
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");
1164			return -EOPNOTSUPP;
1165		}
1166		sections = nla_get_bitfield32(nla_overwrite_mask);
1167		params.overwrite_mask = sections.value & sections.selector;
1168	}
1169
1170	nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
1171	file_name = nla_data(nla_file_name);
1172	ret = request_firmware(&params.fw, file_name, devlink->dev);
1173	if (ret) {
1174		NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name,
1175				    "failed to locate the requested firmware file");
1176		return ret;
1177	}
1178
1179	devlink_flash_update_begin_notify(devlink);
1180	ret = devlink->ops->flash_update(devlink, &params, info->extack);
1181	devlink_flash_update_end_notify(devlink);
1182
1183	release_firmware(params.fw);
1184
1185	return ret;
1186}
1187
1188static void __devlink_compat_running_version(struct devlink *devlink,
1189					     char *buf, size_t len)
1190{
1191	struct devlink_info_req req = {};
1192	const struct nlattr *nlattr;
1193	struct sk_buff *msg;
1194	int rem, err;
1195
1196	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1197	if (!msg)
1198		return;
1199
1200	req.msg = msg;
1201	err = devlink->ops->info_get(devlink, &req, NULL);
1202	if (err)
1203		goto free_msg;
1204
1205	nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
 
1206		const struct nlattr *kv;
1207		int rem_kv;
1208
1209		if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
1210			continue;
1211
1212		nla_for_each_nested(kv, nlattr, rem_kv) {
1213			if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
1214				continue;
1215
1216			strlcat(buf, nla_data(kv), len);
1217			strlcat(buf, " ", len);
1218		}
1219	}
1220free_msg:
1221	nlmsg_free(msg);
1222}
1223
1224void devlink_compat_running_version(struct devlink *devlink,
1225				    char *buf, size_t len)
1226{
1227	if (!devlink->ops->info_get)
1228		return;
1229
1230	devl_lock(devlink);
1231	if (devl_is_registered(devlink))
1232		__devlink_compat_running_version(devlink, buf, len);
1233	devl_unlock(devlink);
1234}
1235
1236int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
1237{
1238	struct devlink_flash_update_params params = {};
1239	int ret;
1240
1241	devl_lock(devlink);
1242	if (!devl_is_registered(devlink)) {
1243		ret = -ENODEV;
1244		goto out_unlock;
1245	}
1246
1247	if (!devlink->ops->flash_update) {
1248		ret = -EOPNOTSUPP;
1249		goto out_unlock;
1250	}
1251
1252	ret = request_firmware(&params.fw, file_name, devlink->dev);
1253	if (ret)
1254		goto out_unlock;
1255
1256	devlink_flash_update_begin_notify(devlink);
1257	ret = devlink->ops->flash_update(devlink, &params, NULL);
1258	devlink_flash_update_end_notify(devlink);
1259
1260	release_firmware(params.fw);
1261out_unlock:
1262	devl_unlock(devlink);
1263
1264	return ret;
1265}
1266
1267static int
1268devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,
1269			  u32 portid, u32 seq, int flags,
1270			  struct netlink_ext_ack *extack)
1271{
1272	struct nlattr *selftests;
1273	void *hdr;
1274	int err;
1275	int i;
1276
1277	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags,
1278			  DEVLINK_CMD_SELFTESTS_GET);
1279	if (!hdr)
1280		return -EMSGSIZE;
1281
1282	err = -EMSGSIZE;
1283	if (devlink_nl_put_handle(msg, devlink))
1284		goto err_cancel_msg;
1285
1286	selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
1287	if (!selftests)
1288		goto err_cancel_msg;
1289
1290	for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
1291	     i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
1292		if (devlink->ops->selftest_check(devlink, i, extack)) {
1293			err = nla_put_flag(msg, i);
1294			if (err)
1295				goto err_cancel_msg;
1296		}
1297	}
1298
1299	nla_nest_end(msg, selftests);
1300	genlmsg_end(msg, hdr);
1301	return 0;
1302
1303err_cancel_msg:
1304	genlmsg_cancel(msg, hdr);
1305	return err;
1306}
1307
1308int devlink_nl_selftests_get_doit(struct sk_buff *skb, struct genl_info *info)
1309{
1310	struct devlink *devlink = info->user_ptr[0];
1311	struct sk_buff *msg;
1312	int err;
1313
1314	if (!devlink->ops->selftest_check)
1315		return -EOPNOTSUPP;
1316
1317	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1318	if (!msg)
1319		return -ENOMEM;
1320
1321	err = devlink_nl_selftests_fill(msg, devlink, info->snd_portid,
1322					info->snd_seq, 0, info->extack);
1323	if (err) {
1324		nlmsg_free(msg);
1325		return err;
1326	}
1327
1328	return genlmsg_reply(msg, info);
1329}
1330
1331static int devlink_nl_selftests_get_dump_one(struct sk_buff *msg,
1332					     struct devlink *devlink,
1333					     struct netlink_callback *cb,
1334					     int flags)
1335{
1336	if (!devlink->ops->selftest_check)
1337		return 0;
1338
1339	return devlink_nl_selftests_fill(msg, devlink,
1340					 NETLINK_CB(cb->skb).portid,
1341					 cb->nlh->nlmsg_seq, flags,
1342					 cb->extack);
1343}
1344
1345int devlink_nl_selftests_get_dumpit(struct sk_buff *skb,
1346				    struct netlink_callback *cb)
1347{
1348	return devlink_nl_dumpit(skb, cb, devlink_nl_selftests_get_dump_one);
1349}
1350
1351static int devlink_selftest_result_put(struct sk_buff *skb, unsigned int id,
1352				       enum devlink_selftest_status test_status)
1353{
1354	struct nlattr *result_attr;
1355
1356	result_attr = nla_nest_start(skb, DEVLINK_ATTR_SELFTEST_RESULT);
1357	if (!result_attr)
1358		return -EMSGSIZE;
1359
1360	if (nla_put_u32(skb, DEVLINK_ATTR_SELFTEST_RESULT_ID, id) ||
1361	    nla_put_u8(skb, DEVLINK_ATTR_SELFTEST_RESULT_STATUS,
1362		       test_status))
1363		goto nla_put_failure;
1364
1365	nla_nest_end(skb, result_attr);
1366	return 0;
1367
1368nla_put_failure:
1369	nla_nest_cancel(skb, result_attr);
1370	return -EMSGSIZE;
1371}
1372
1373static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_ID_MAX + 1] = {
1374	[DEVLINK_ATTR_SELFTEST_ID_FLASH] = { .type = NLA_FLAG },
1375};
1376
1377int devlink_nl_selftests_run_doit(struct sk_buff *skb, struct genl_info *info)
1378{
1379	struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
1380	struct devlink *devlink = info->user_ptr[0];
1381	struct nlattr *attrs, *selftests;
1382	struct sk_buff *msg;
1383	void *hdr;
1384	int err;
1385	int i;
1386
1387	if (!devlink->ops->selftest_run || !devlink->ops->selftest_check)
1388		return -EOPNOTSUPP;
1389
1390	if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SELFTESTS))
1391		return -EINVAL;
1392
1393	attrs = info->attrs[DEVLINK_ATTR_SELFTESTS];
1394
1395	err = nla_parse_nested(tb, DEVLINK_ATTR_SELFTEST_ID_MAX, attrs,
1396			       devlink_selftest_nl_policy, info->extack);
1397	if (err < 0)
1398		return err;
1399
1400	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1401	if (!msg)
1402		return -ENOMEM;
1403
1404	err = -EMSGSIZE;
1405	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1406			  &devlink_nl_family, 0, DEVLINK_CMD_SELFTESTS_RUN);
1407	if (!hdr)
1408		goto free_msg;
1409
1410	if (devlink_nl_put_handle(msg, devlink))
1411		goto genlmsg_cancel;
1412
1413	selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
1414	if (!selftests)
1415		goto genlmsg_cancel;
1416
1417	for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
1418	     i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
1419		enum devlink_selftest_status test_status;
1420
1421		if (nla_get_flag(tb[i])) {
1422			if (!devlink->ops->selftest_check(devlink, i,
1423							  info->extack)) {
1424				if (devlink_selftest_result_put(msg, i,
1425								DEVLINK_SELFTEST_STATUS_SKIP))
1426					goto selftests_nest_cancel;
1427				continue;
1428			}
1429
1430			test_status = devlink->ops->selftest_run(devlink, i,
1431								 info->extack);
1432			if (devlink_selftest_result_put(msg, i, test_status))
1433				goto selftests_nest_cancel;
1434		}
1435	}
1436
1437	nla_nest_end(msg, selftests);
1438	genlmsg_end(msg, hdr);
1439	return genlmsg_reply(msg, info);
1440
1441selftests_nest_cancel:
1442	nla_nest_cancel(msg, selftests);
1443genlmsg_cancel:
1444	genlmsg_cancel(msg, hdr);
1445free_msg:
1446	nlmsg_free(msg);
1447	return err;
1448}
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
   4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
   5 */
   6
   7#include <linux/device.h>
   8#include <net/genetlink.h>
   9#include <net/sock.h>
  10#include "devl_internal.h"
  11
  12struct devlink_info_req {
  13	struct sk_buff *msg;
  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;
  18};
  19
  20struct devlink_reload_combination {
  21	enum devlink_reload_action action;
  22	enum devlink_reload_limit limit;
  23};
  24
  25static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
  26	{
  27		/* can't reinitialize driver with no down time */
  28		.action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
  29		.limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
  30	},
  31};
  32
  33static bool
  34devlink_reload_combination_is_invalid(enum devlink_reload_action action,
  35				      enum devlink_reload_limit limit)
  36{
  37	int i;
  38
  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)
  42			return true;
  43	return false;
  44}
  45
  46static bool
  47devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
  48{
  49	return test_bit(action, &devlink->ops->reload_actions);
  50}
  51
  52static bool
  53devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
  54{
  55	return test_bit(limit, &devlink->ops->reload_limits);
  56}
  57
  58static int devlink_reload_stat_put(struct sk_buff *msg,
  59				   enum devlink_reload_limit limit, u32 value)
  60{
  61	struct nlattr *reload_stats_entry;
  62
  63	reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
  64	if (!reload_stats_entry)
  65		return -EMSGSIZE;
  66
  67	if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
  68	    nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
  69		goto nla_put_failure;
  70	nla_nest_end(msg, reload_stats_entry);
  71	return 0;
  72
  73nla_put_failure:
  74	nla_nest_cancel(msg, reload_stats_entry);
  75	return -EMSGSIZE;
  76}
  77
  78static int
  79devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
  80{
  81	struct nlattr *reload_stats_attr, *act_info, *act_stats;
  82	int i, j, stat_idx;
  83	u32 value;
  84
  85	if (!is_remote)
  86		reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
  87	else
  88		reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
  89
  90	if (!reload_stats_attr)
  91		return -EMSGSIZE;
  92
  93	for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
  94		if ((!is_remote &&
  95		     !devlink_reload_action_is_supported(devlink, i)) ||
  96		    i == DEVLINK_RELOAD_ACTION_UNSPEC)
  97			continue;
  98		act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
  99		if (!act_info)
 100			goto nla_put_failure;
 101
 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);
 105		if (!act_stats)
 106			goto action_info_nest_cancel;
 107
 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
 112			 * limit.
 113			 */
 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))
 117				continue;
 118
 119			stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
 120			if (!is_remote)
 121				value = devlink->stats.reload_stats[stat_idx];
 122			else
 123				value = devlink->stats.remote_reload_stats[stat_idx];
 124			if (devlink_reload_stat_put(msg, j, value))
 125				goto action_stats_nest_cancel;
 126		}
 127		nla_nest_end(msg, act_stats);
 128		nla_nest_end(msg, act_info);
 129	}
 130	nla_nest_end(msg, reload_stats_attr);
 131	return 0;
 132
 133action_stats_nest_cancel:
 134	nla_nest_cancel(msg, act_stats);
 135action_info_nest_cancel:
 136	nla_nest_cancel(msg, act_info);
 137nla_put_failure:
 138	nla_nest_cancel(msg, reload_stats_attr);
 139	return -EMSGSIZE;
 140}
 141
 142static int devlink_nl_nested_fill(struct sk_buff *msg, struct devlink *devlink)
 143{
 144	unsigned long rel_index;
 145	void *unused;
 146	int err;
 147
 148	xa_for_each(&devlink->nested_rels, rel_index, unused) {
 149		err = devlink_rel_devlink_handle_put(msg, devlink,
 150						     rel_index,
 151						     DEVLINK_ATTR_NESTED_DEVLINK,
 152						     NULL);
 153		if (err)
 154			return err;
 155	}
 156	return 0;
 157}
 158
 159static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
 160			   enum devlink_command cmd, u32 portid,
 161			   u32 seq, int flags)
 162{
 163	struct nlattr *dev_stats;
 164	void *hdr;
 165
 166	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
 167	if (!hdr)
 168		return -EMSGSIZE;
 169
 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;
 174
 175	dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
 176	if (!dev_stats)
 177		goto nla_put_failure;
 178
 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;
 183
 184	nla_nest_end(msg, dev_stats);
 185
 186	if (devlink_nl_nested_fill(msg, devlink))
 187		goto nla_put_failure;
 188
 189	genlmsg_end(msg, hdr);
 190	return 0;
 191
 192dev_stats_nest_cancel:
 193	nla_nest_cancel(msg, dev_stats);
 194nla_put_failure:
 195	genlmsg_cancel(msg, hdr);
 196	return -EMSGSIZE;
 197}
 198
 199static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
 200{
 201	struct sk_buff *msg;
 202	int err;
 203
 204	WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
 205	WARN_ON(!devl_is_registered(devlink));
 206
 207	if (!devlink_nl_notify_need(devlink))
 208		return;
 209
 210	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 211	if (!msg)
 212		return;
 213
 214	err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
 215	if (err) {
 216		nlmsg_free(msg);
 217		return;
 218	}
 219
 220	devlink_nl_notify_send(devlink, msg);
 221}
 222
 223int devlink_nl_get_doit(struct sk_buff *skb, struct genl_info *info)
 224{
 225	struct devlink *devlink = info->user_ptr[0];
 226	struct sk_buff *msg;
 227	int err;
 228
 229	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 230	if (!msg)
 231		return -ENOMEM;
 232
 233	err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
 234			      info->snd_portid, info->snd_seq, 0);
 235	if (err) {
 236		nlmsg_free(msg);
 237		return err;
 238	}
 239
 240	return genlmsg_reply(msg, info);
 241}
 242
 243static int
 244devlink_nl_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
 245			struct netlink_callback *cb, int flags)
 246{
 247	return devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
 248			       NETLINK_CB(cb->skb).portid,
 249			       cb->nlh->nlmsg_seq, flags);
 250}
 251
 252int devlink_nl_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
 253{
 254	return devlink_nl_dumpit(msg, cb, devlink_nl_get_dump_one);
 255}
 256
 257static void devlink_rel_notify_cb(struct devlink *devlink, u32 obj_index)
 258{
 259	devlink_notify(devlink, DEVLINK_CMD_NEW);
 260}
 261
 262static void devlink_rel_cleanup_cb(struct devlink *devlink, u32 obj_index,
 263				   u32 rel_index)
 264{
 265	xa_erase(&devlink->nested_rels, rel_index);
 266}
 267
 268int devl_nested_devlink_set(struct devlink *devlink,
 269			    struct devlink *nested_devlink)
 270{
 271	u32 rel_index;
 272	int err;
 273
 274	err = devlink_rel_nested_in_add(&rel_index, devlink->index, 0,
 275					devlink_rel_notify_cb,
 276					devlink_rel_cleanup_cb,
 277					nested_devlink);
 278	if (err)
 279		return err;
 280	return xa_insert(&devlink->nested_rels, rel_index,
 281			 xa_mk_value(0), GFP_KERNEL);
 282}
 283EXPORT_SYMBOL_GPL(devl_nested_devlink_set);
 284
 285void devlink_notify_register(struct devlink *devlink)
 286{
 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);
 296}
 297
 298void devlink_notify_unregister(struct devlink *devlink)
 299{
 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);
 309}
 310
 311static void devlink_reload_failed_set(struct devlink *devlink,
 312				      bool reload_failed)
 313{
 314	if (devlink->reload_failed == reload_failed)
 315		return;
 316	devlink->reload_failed = reload_failed;
 317	devlink_notify(devlink, DEVLINK_CMD_NEW);
 318}
 319
 320bool devlink_is_reload_failed(const struct devlink *devlink)
 321{
 322	return devlink->reload_failed;
 323}
 324EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
 325
 326static void
 327__devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
 328			      enum devlink_reload_limit limit, u32 actions_performed)
 329{
 330	unsigned long actions = actions_performed;
 331	int stat_idx;
 332	int action;
 333
 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]++;
 337	}
 338	devlink_notify(devlink, DEVLINK_CMD_NEW);
 339}
 340
 341static void
 342devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
 343			    u32 actions_performed)
 344{
 345	__devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
 346				      actions_performed);
 347}
 348
 349/**
 350 *	devlink_remote_reload_actions_performed - Update devlink on reload actions
 351 *	  performed which are not a direct result of devlink reload call.
 352 *
 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.
 358 *
 359 *	@devlink: devlink
 360 *	@limit: reload limit
 361 *	@actions_performed: bitmask of actions performed
 362 */
 363void devlink_remote_reload_actions_performed(struct devlink *devlink,
 364					     enum devlink_reload_limit limit,
 365					     u32 actions_performed)
 366{
 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))
 371		return;
 372
 373	__devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
 374				      actions_performed);
 375}
 376EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
 377
 378static struct net *devlink_netns_get(struct sk_buff *skb,
 379				     struct genl_info *info)
 380{
 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];
 384	struct net *net;
 385
 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);
 389	}
 390
 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));
 398		if (!net)
 399			net = ERR_PTR(-EINVAL);
 400	} else {
 401		WARN_ON(1);
 402		net = ERR_PTR(-EINVAL);
 403	}
 404	if (IS_ERR(net)) {
 405		NL_SET_ERR_MSG(info->extack, "Unknown network namespace");
 406		return ERR_PTR(-EINVAL);
 407	}
 408	if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
 409		put_net(net);
 410		return ERR_PTR(-EPERM);
 411	}
 412	return net;
 413}
 414
 415static void devlink_reload_netns_change(struct devlink *devlink,
 416					struct net *curr_net,
 417					struct net *dest_net)
 418{
 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.
 423	 */
 424	devlink_notify_unregister(devlink);
 425	write_pnet(&devlink->_net, dest_net);
 426	devlink_notify_register(devlink);
 427	devlink_rel_nested_in_notify(devlink);
 428}
 429
 430static void devlink_reload_reinit_sanity_check(struct devlink *devlink)
 431{
 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));
 440}
 441
 442int 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)
 446{
 447	u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
 448	struct net *curr_net;
 449	int err;
 450
 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.
 455	 */
 456	device_lock_assert(devlink->dev);
 457
 458	memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
 459	       sizeof(remote_reload_stats));
 460
 461	err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
 462	if (err)
 463		return err;
 464
 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);
 468
 469	if (action == DEVLINK_RELOAD_ACTION_DRIVER_REINIT) {
 470		devlink_params_driverinit_load_new(devlink);
 471		devlink_reload_reinit_sanity_check(devlink);
 472	}
 473
 474	err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
 475	devlink_reload_failed_set(devlink, !!err);
 476	if (err)
 477		return err;
 478
 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);
 484	return 0;
 485}
 486
 487static int
 488devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
 489					enum devlink_command cmd, struct genl_info *info)
 490{
 491	struct sk_buff *msg;
 492	void *hdr;
 493
 494	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 495	if (!msg)
 496		return -ENOMEM;
 497
 498	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
 499	if (!hdr)
 500		goto free_msg;
 501
 502	if (devlink_nl_put_handle(msg, devlink))
 503		goto nla_put_failure;
 504
 505	if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
 506			       actions_performed))
 507		goto nla_put_failure;
 508	genlmsg_end(msg, hdr);
 509
 510	return genlmsg_reply(msg, info);
 511
 512nla_put_failure:
 513	genlmsg_cancel(msg, hdr);
 514free_msg:
 515	nlmsg_free(msg);
 516	return -EMSGSIZE;
 517}
 518
 519int devlink_nl_reload_doit(struct sk_buff *skb, struct genl_info *info)
 520{
 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;
 526	int err;
 527
 528	err = devlink_resources_validate(devlink, NULL, info);
 529	if (err) {
 530		NL_SET_ERR_MSG(info->extack, "resources size validation failed");
 531		return err;
 532	}
 533
 534	action = nla_get_u8_default(info->attrs[DEVLINK_ATTR_RELOAD_ACTION],
 535				    DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
 
 
 536
 537	if (!devlink_reload_action_is_supported(devlink, action)) {
 538		NL_SET_ERR_MSG(info->extack, "Requested reload action is not supported by the driver");
 539		return -EOPNOTSUPP;
 540	}
 541
 542	limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
 543	if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
 544		struct nla_bitfield32 limits;
 545		u32 limits_selected;
 546
 547		limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
 548		limits_selected = limits.value & limits.selector;
 549		if (!limits_selected) {
 550			NL_SET_ERR_MSG(info->extack, "Invalid limit selected");
 551			return -EINVAL;
 552		}
 553		for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
 554			if (limits_selected & BIT(limit))
 555				break;
 556		/* UAPI enables multiselection, but currently it is not used */
 557		if (limits_selected != BIT(limit)) {
 558			NL_SET_ERR_MSG(info->extack, "Multiselection of limit is not supported");
 559			return -EOPNOTSUPP;
 560		}
 561		if (!devlink_reload_limit_is_supported(devlink, limit)) {
 562			NL_SET_ERR_MSG(info->extack, "Requested limit is not supported by the driver");
 563			return -EOPNOTSUPP;
 564		}
 565		if (devlink_reload_combination_is_invalid(action, limit)) {
 566			NL_SET_ERR_MSG(info->extack, "Requested limit is invalid for this action");
 567			return -EINVAL;
 568		}
 569	}
 570	if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
 571	    info->attrs[DEVLINK_ATTR_NETNS_FD] ||
 572	    info->attrs[DEVLINK_ATTR_NETNS_ID]) {
 573		dest_net = devlink_netns_get(skb, info);
 574		if (IS_ERR(dest_net))
 575			return PTR_ERR(dest_net);
 576		if (!net_eq(dest_net, devlink_net(devlink)) &&
 577		    action != DEVLINK_RELOAD_ACTION_DRIVER_REINIT) {
 578			NL_SET_ERR_MSG_MOD(info->extack,
 579					   "Changing namespace is only supported for reinit action");
 580			return -EOPNOTSUPP;
 581		}
 582	}
 583
 584	err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
 585
 586	if (dest_net)
 587		put_net(dest_net);
 588
 589	if (err)
 590		return err;
 591	/* For backward compatibility generate reply only if attributes used by user */
 592	if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
 593		return 0;
 594
 595	return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
 596						       DEVLINK_CMD_RELOAD, info);
 597}
 598
 599bool devlink_reload_actions_valid(const struct devlink_ops *ops)
 600{
 601	const struct devlink_reload_combination *comb;
 602	int i;
 603
 604	if (!devlink_reload_supported(ops)) {
 605		if (WARN_ON(ops->reload_actions))
 606			return false;
 607		return true;
 608	}
 609
 610	if (WARN_ON(!ops->reload_actions ||
 611		    ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
 612		    ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
 613		return false;
 614
 615	if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
 616		    ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
 617		return false;
 618
 619	for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)  {
 620		comb = &devlink_reload_invalid_combinations[i];
 621		if (ops->reload_actions == BIT(comb->action) &&
 622		    ops->reload_limits == BIT(comb->limit))
 623			return false;
 624	}
 625	return true;
 626}
 627
 628static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
 629				   enum devlink_command cmd, u32 portid,
 630				   u32 seq, int flags)
 631{
 632	const struct devlink_ops *ops = devlink->ops;
 633	enum devlink_eswitch_encap_mode encap_mode;
 634	u8 inline_mode;
 635	void *hdr;
 636	int err = 0;
 637	u16 mode;
 638
 639	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
 640	if (!hdr)
 641		return -EMSGSIZE;
 642
 643	err = devlink_nl_put_handle(msg, devlink);
 644	if (err)
 645		goto nla_put_failure;
 646
 647	if (ops->eswitch_mode_get) {
 648		err = ops->eswitch_mode_get(devlink, &mode);
 649		if (err)
 650			goto nla_put_failure;
 651		err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
 652		if (err)
 653			goto nla_put_failure;
 654	}
 655
 656	if (ops->eswitch_inline_mode_get) {
 657		err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
 658		if (err)
 659			goto nla_put_failure;
 660		err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
 661				 inline_mode);
 662		if (err)
 663			goto nla_put_failure;
 664	}
 665
 666	if (ops->eswitch_encap_mode_get) {
 667		err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
 668		if (err)
 669			goto nla_put_failure;
 670		err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
 671		if (err)
 672			goto nla_put_failure;
 673	}
 674
 675	genlmsg_end(msg, hdr);
 676	return 0;
 677
 678nla_put_failure:
 679	genlmsg_cancel(msg, hdr);
 680	return err;
 681}
 682
 683int devlink_nl_eswitch_get_doit(struct sk_buff *skb, struct genl_info *info)
 684{
 685	struct devlink *devlink = info->user_ptr[0];
 686	struct sk_buff *msg;
 687	int err;
 688
 689	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 690	if (!msg)
 691		return -ENOMEM;
 692
 693	err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
 694				      info->snd_portid, info->snd_seq, 0);
 695
 696	if (err) {
 697		nlmsg_free(msg);
 698		return err;
 699	}
 700
 701	return genlmsg_reply(msg, info);
 702}
 703
 704int devlink_nl_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info)
 705{
 706	struct devlink *devlink = info->user_ptr[0];
 707	const struct devlink_ops *ops = devlink->ops;
 708	enum devlink_eswitch_encap_mode encap_mode;
 709	u8 inline_mode;
 710	int err = 0;
 711	u16 mode;
 712
 713	if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
 714		if (!ops->eswitch_mode_set)
 715			return -EOPNOTSUPP;
 716		mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
 717		err = devlink_rate_nodes_check(devlink, mode, info->extack);
 718		if (err)
 719			return err;
 720		err = ops->eswitch_mode_set(devlink, mode, info->extack);
 721		if (err)
 722			return err;
 723	}
 724
 725	if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
 726		if (!ops->eswitch_inline_mode_set)
 727			return -EOPNOTSUPP;
 728		inline_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
 729		err = ops->eswitch_inline_mode_set(devlink, inline_mode,
 730						   info->extack);
 731		if (err)
 732			return err;
 733	}
 734
 735	if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
 736		if (!ops->eswitch_encap_mode_set)
 737			return -EOPNOTSUPP;
 738		encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
 739		err = ops->eswitch_encap_mode_set(devlink, encap_mode,
 740						  info->extack);
 741		if (err)
 742			return err;
 743	}
 744
 745	return 0;
 746}
 747
 748int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
 749{
 750	if (!req->msg)
 751		return 0;
 752	return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
 753}
 754EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
 755
 756int devlink_info_board_serial_number_put(struct devlink_info_req *req,
 757					 const char *bsn)
 758{
 759	if (!req->msg)
 760		return 0;
 761	return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
 762			      bsn);
 763}
 764EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
 765
 766static int devlink_info_version_put(struct devlink_info_req *req, int attr,
 767				    const char *version_name,
 768				    const char *version_value,
 769				    enum devlink_info_version_type version_type)
 770{
 771	struct nlattr *nest;
 772	int err;
 773
 774	if (req->version_cb)
 775		req->version_cb(version_name, version_type,
 776				req->version_cb_priv);
 777
 778	if (!req->msg)
 779		return 0;
 780
 781	nest = nla_nest_start_noflag(req->msg, attr);
 782	if (!nest)
 783		return -EMSGSIZE;
 784
 785	err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
 786			     version_name);
 787	if (err)
 788		goto nla_put_failure;
 789
 790	err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
 791			     version_value);
 792	if (err)
 793		goto nla_put_failure;
 794
 795	nla_nest_end(req->msg, nest);
 796
 797	return 0;
 798
 799nla_put_failure:
 800	nla_nest_cancel(req->msg, nest);
 801	return err;
 802}
 803
 804int devlink_info_version_fixed_put(struct devlink_info_req *req,
 805				   const char *version_name,
 806				   const char *version_value)
 807{
 808	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
 809					version_name, version_value,
 810					DEVLINK_INFO_VERSION_TYPE_NONE);
 811}
 812EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
 813
 814int devlink_info_version_stored_put(struct devlink_info_req *req,
 815				    const char *version_name,
 816				    const char *version_value)
 817{
 818	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
 819					version_name, version_value,
 820					DEVLINK_INFO_VERSION_TYPE_NONE);
 821}
 822EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
 823
 824int devlink_info_version_stored_put_ext(struct devlink_info_req *req,
 825					const char *version_name,
 826					const char *version_value,
 827					enum devlink_info_version_type version_type)
 828{
 829	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
 830					version_name, version_value,
 831					version_type);
 832}
 833EXPORT_SYMBOL_GPL(devlink_info_version_stored_put_ext);
 834
 835int devlink_info_version_running_put(struct devlink_info_req *req,
 836				     const char *version_name,
 837				     const char *version_value)
 838{
 839	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
 840					version_name, version_value,
 841					DEVLINK_INFO_VERSION_TYPE_NONE);
 842}
 843EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
 844
 845int devlink_info_version_running_put_ext(struct devlink_info_req *req,
 846					 const char *version_name,
 847					 const char *version_value,
 848					 enum devlink_info_version_type version_type)
 849{
 850	return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
 851					version_name, version_value,
 852					version_type);
 853}
 854EXPORT_SYMBOL_GPL(devlink_info_version_running_put_ext);
 855
 856static int devlink_nl_driver_info_get(struct device_driver *drv,
 857				      struct devlink_info_req *req)
 858{
 859	if (!drv)
 860		return 0;
 861
 862	if (drv->name[0])
 863		return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME,
 864				      drv->name);
 865
 866	return 0;
 867}
 868
 869static int
 870devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
 871		     enum devlink_command cmd, u32 portid,
 872		     u32 seq, int flags, struct netlink_ext_ack *extack)
 873{
 874	struct device *dev = devlink_to_dev(devlink);
 875	struct devlink_info_req req = {};
 876	void *hdr;
 877	int err;
 878
 879	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
 880	if (!hdr)
 881		return -EMSGSIZE;
 882
 883	err = -EMSGSIZE;
 884	if (devlink_nl_put_handle(msg, devlink))
 885		goto err_cancel_msg;
 886
 887	req.msg = msg;
 888	if (devlink->ops->info_get) {
 889		err = devlink->ops->info_get(devlink, &req, extack);
 890		if (err)
 891			goto err_cancel_msg;
 892	}
 893
 894	err = devlink_nl_driver_info_get(dev->driver, &req);
 895	if (err)
 896		goto err_cancel_msg;
 897
 898	genlmsg_end(msg, hdr);
 899	return 0;
 900
 901err_cancel_msg:
 902	genlmsg_cancel(msg, hdr);
 903	return err;
 904}
 905
 906int devlink_nl_info_get_doit(struct sk_buff *skb, struct genl_info *info)
 907{
 908	struct devlink *devlink = info->user_ptr[0];
 909	struct sk_buff *msg;
 910	int err;
 911
 912	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 913	if (!msg)
 914		return -ENOMEM;
 915
 916	err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
 917				   info->snd_portid, info->snd_seq, 0,
 918				   info->extack);
 919	if (err) {
 920		nlmsg_free(msg);
 921		return err;
 922	}
 923
 924	return genlmsg_reply(msg, info);
 925}
 926
 927static int
 928devlink_nl_info_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
 929			     struct netlink_callback *cb, int flags)
 930{
 931	int err;
 932
 933	err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
 934				   NETLINK_CB(cb->skb).portid,
 935				   cb->nlh->nlmsg_seq, flags,
 936				   cb->extack);
 937	if (err == -EOPNOTSUPP)
 938		err = 0;
 939	return err;
 940}
 941
 942int devlink_nl_info_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
 943{
 944	return devlink_nl_dumpit(msg, cb, devlink_nl_info_get_dump_one);
 945}
 946
 947static int devlink_nl_flash_update_fill(struct sk_buff *msg,
 948					struct devlink *devlink,
 949					enum devlink_command cmd,
 950					struct devlink_flash_notify *params)
 951{
 952	void *hdr;
 953
 954	hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
 955	if (!hdr)
 956		return -EMSGSIZE;
 957
 958	if (devlink_nl_put_handle(msg, devlink))
 959		goto nla_put_failure;
 960
 961	if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
 962		goto out;
 963
 964	if (params->status_msg &&
 965	    nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
 966			   params->status_msg))
 967		goto nla_put_failure;
 968	if (params->component &&
 969	    nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
 970			   params->component))
 971		goto nla_put_failure;
 972	if (devlink_nl_put_u64(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
 973			       params->done))
 974		goto nla_put_failure;
 975	if (devlink_nl_put_u64(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
 976			       params->total))
 977		goto nla_put_failure;
 978	if (devlink_nl_put_u64(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
 979			       params->timeout))
 980		goto nla_put_failure;
 981
 982out:
 983	genlmsg_end(msg, hdr);
 984	return 0;
 985
 986nla_put_failure:
 987	genlmsg_cancel(msg, hdr);
 988	return -EMSGSIZE;
 989}
 990
 991static void __devlink_flash_update_notify(struct devlink *devlink,
 992					  enum devlink_command cmd,
 993					  struct devlink_flash_notify *params)
 994{
 995	struct sk_buff *msg;
 996	int err;
 997
 998	WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
 999		cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
1000		cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
1001
1002	if (!devl_is_registered(devlink) || !devlink_nl_notify_need(devlink))
1003		return;
1004
1005	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1006	if (!msg)
1007		return;
1008
1009	err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
1010	if (err)
1011		goto out_free_msg;
1012
1013	devlink_nl_notify_send(devlink, msg);
1014	return;
1015
1016out_free_msg:
1017	nlmsg_free(msg);
1018}
1019
1020static void devlink_flash_update_begin_notify(struct devlink *devlink)
1021{
1022	struct devlink_flash_notify params = {};
1023
1024	__devlink_flash_update_notify(devlink,
1025				      DEVLINK_CMD_FLASH_UPDATE,
1026				      &params);
1027}
1028
1029static void devlink_flash_update_end_notify(struct devlink *devlink)
1030{
1031	struct devlink_flash_notify params = {};
1032
1033	__devlink_flash_update_notify(devlink,
1034				      DEVLINK_CMD_FLASH_UPDATE_END,
1035				      &params);
1036}
1037
1038void devlink_flash_update_status_notify(struct devlink *devlink,
1039					const char *status_msg,
1040					const char *component,
1041					unsigned long done,
1042					unsigned long total)
1043{
1044	struct devlink_flash_notify params = {
1045		.status_msg = status_msg,
1046		.component = component,
1047		.done = done,
1048		.total = total,
1049	};
1050
1051	__devlink_flash_update_notify(devlink,
1052				      DEVLINK_CMD_FLASH_UPDATE_STATUS,
1053				      &params);
1054}
1055EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
1056
1057void devlink_flash_update_timeout_notify(struct devlink *devlink,
1058					 const char *status_msg,
1059					 const char *component,
1060					 unsigned long timeout)
1061{
1062	struct devlink_flash_notify params = {
1063		.status_msg = status_msg,
1064		.component = component,
1065		.timeout = timeout,
1066	};
1067
1068	__devlink_flash_update_notify(devlink,
1069				      DEVLINK_CMD_FLASH_UPDATE_STATUS,
1070				      &params);
1071}
1072EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
1073
1074struct devlink_flash_component_lookup_ctx {
1075	const char *lookup_name;
1076	bool lookup_name_found;
1077};
1078
1079static void
1080devlink_flash_component_lookup_cb(const char *version_name,
1081				  enum devlink_info_version_type version_type,
1082				  void *version_cb_priv)
1083{
1084	struct devlink_flash_component_lookup_ctx *lookup_ctx = version_cb_priv;
1085
1086	if (version_type != DEVLINK_INFO_VERSION_TYPE_COMPONENT ||
1087	    lookup_ctx->lookup_name_found)
1088		return;
1089
1090	lookup_ctx->lookup_name_found =
1091		!strcmp(lookup_ctx->lookup_name, version_name);
1092}
1093
1094static int devlink_flash_component_get(struct devlink *devlink,
1095				       struct nlattr *nla_component,
1096				       const char **p_component,
1097				       struct netlink_ext_ack *extack)
1098{
1099	struct devlink_flash_component_lookup_ctx lookup_ctx = {};
1100	struct devlink_info_req req = {};
1101	const char *component;
1102	int ret;
1103
1104	if (!nla_component)
1105		return 0;
1106
1107	component = nla_data(nla_component);
1108
1109	if (!devlink->ops->info_get) {
1110		NL_SET_ERR_MSG_ATTR(extack, nla_component,
1111				    "component update is not supported by this device");
1112		return -EOPNOTSUPP;
1113	}
1114
1115	lookup_ctx.lookup_name = component;
1116	req.version_cb = devlink_flash_component_lookup_cb;
1117	req.version_cb_priv = &lookup_ctx;
1118
1119	ret = devlink->ops->info_get(devlink, &req, NULL);
1120	if (ret)
1121		return ret;
1122
1123	if (!lookup_ctx.lookup_name_found) {
1124		NL_SET_ERR_MSG_ATTR(extack, nla_component,
1125				    "selected component is not supported by this device");
1126		return -EINVAL;
1127	}
1128	*p_component = component;
1129	return 0;
1130}
1131
1132int devlink_nl_flash_update_doit(struct sk_buff *skb, struct genl_info *info)
1133{
1134	struct nlattr *nla_overwrite_mask, *nla_file_name;
1135	struct devlink_flash_update_params params = {};
1136	struct devlink *devlink = info->user_ptr[0];
1137	const char *file_name;
1138	u32 supported_params;
1139	int ret;
1140
1141	if (!devlink->ops->flash_update)
1142		return -EOPNOTSUPP;
1143
1144	if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME))
1145		return -EINVAL;
1146
1147	ret = devlink_flash_component_get(devlink,
1148					  info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT],
1149					  &params.component, info->extack);
1150	if (ret)
1151		return ret;
1152
1153	supported_params = devlink->ops->supported_flash_update_params;
1154
1155	nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
1156	if (nla_overwrite_mask) {
1157		struct nla_bitfield32 sections;
1158
1159		if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
1160			NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
1161					    "overwrite settings are not supported by this device");
1162			return -EOPNOTSUPP;
1163		}
1164		sections = nla_get_bitfield32(nla_overwrite_mask);
1165		params.overwrite_mask = sections.value & sections.selector;
1166	}
1167
1168	nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
1169	file_name = nla_data(nla_file_name);
1170	ret = request_firmware(&params.fw, file_name, devlink->dev);
1171	if (ret) {
1172		NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name,
1173				    "failed to locate the requested firmware file");
1174		return ret;
1175	}
1176
1177	devlink_flash_update_begin_notify(devlink);
1178	ret = devlink->ops->flash_update(devlink, &params, info->extack);
1179	devlink_flash_update_end_notify(devlink);
1180
1181	release_firmware(params.fw);
1182
1183	return ret;
1184}
1185
1186static void __devlink_compat_running_version(struct devlink *devlink,
1187					     char *buf, size_t len)
1188{
1189	struct devlink_info_req req = {};
1190	const struct nlattr *nlattr;
1191	struct sk_buff *msg;
1192	int rem, err;
1193
1194	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1195	if (!msg)
1196		return;
1197
1198	req.msg = msg;
1199	err = devlink->ops->info_get(devlink, &req, NULL);
1200	if (err)
1201		goto free_msg;
1202
1203	nla_for_each_attr_type(nlattr, DEVLINK_ATTR_INFO_VERSION_RUNNING,
1204			       (void *)msg->data, msg->len, rem) {
1205		const struct nlattr *kv;
1206		int rem_kv;
1207
1208		nla_for_each_nested_type(kv, DEVLINK_ATTR_INFO_VERSION_VALUE,
1209					 nlattr, rem_kv) {
 
 
 
 
 
1210			strlcat(buf, nla_data(kv), len);
1211			strlcat(buf, " ", len);
1212		}
1213	}
1214free_msg:
1215	nlmsg_consume(msg);
1216}
1217
1218void devlink_compat_running_version(struct devlink *devlink,
1219				    char *buf, size_t len)
1220{
1221	if (!devlink->ops->info_get)
1222		return;
1223
1224	devl_lock(devlink);
1225	if (devl_is_registered(devlink))
1226		__devlink_compat_running_version(devlink, buf, len);
1227	devl_unlock(devlink);
1228}
1229
1230int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
1231{
1232	struct devlink_flash_update_params params = {};
1233	int ret;
1234
1235	devl_lock(devlink);
1236	if (!devl_is_registered(devlink)) {
1237		ret = -ENODEV;
1238		goto out_unlock;
1239	}
1240
1241	if (!devlink->ops->flash_update) {
1242		ret = -EOPNOTSUPP;
1243		goto out_unlock;
1244	}
1245
1246	ret = request_firmware(&params.fw, file_name, devlink->dev);
1247	if (ret)
1248		goto out_unlock;
1249
1250	devlink_flash_update_begin_notify(devlink);
1251	ret = devlink->ops->flash_update(devlink, &params, NULL);
1252	devlink_flash_update_end_notify(devlink);
1253
1254	release_firmware(params.fw);
1255out_unlock:
1256	devl_unlock(devlink);
1257
1258	return ret;
1259}
1260
1261static int
1262devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,
1263			  u32 portid, u32 seq, int flags,
1264			  struct netlink_ext_ack *extack)
1265{
1266	struct nlattr *selftests;
1267	void *hdr;
1268	int err;
1269	int i;
1270
1271	hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags,
1272			  DEVLINK_CMD_SELFTESTS_GET);
1273	if (!hdr)
1274		return -EMSGSIZE;
1275
1276	err = -EMSGSIZE;
1277	if (devlink_nl_put_handle(msg, devlink))
1278		goto err_cancel_msg;
1279
1280	selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
1281	if (!selftests)
1282		goto err_cancel_msg;
1283
1284	for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
1285	     i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
1286		if (devlink->ops->selftest_check(devlink, i, extack)) {
1287			err = nla_put_flag(msg, i);
1288			if (err)
1289				goto err_cancel_msg;
1290		}
1291	}
1292
1293	nla_nest_end(msg, selftests);
1294	genlmsg_end(msg, hdr);
1295	return 0;
1296
1297err_cancel_msg:
1298	genlmsg_cancel(msg, hdr);
1299	return err;
1300}
1301
1302int devlink_nl_selftests_get_doit(struct sk_buff *skb, struct genl_info *info)
1303{
1304	struct devlink *devlink = info->user_ptr[0];
1305	struct sk_buff *msg;
1306	int err;
1307
1308	if (!devlink->ops->selftest_check)
1309		return -EOPNOTSUPP;
1310
1311	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1312	if (!msg)
1313		return -ENOMEM;
1314
1315	err = devlink_nl_selftests_fill(msg, devlink, info->snd_portid,
1316					info->snd_seq, 0, info->extack);
1317	if (err) {
1318		nlmsg_free(msg);
1319		return err;
1320	}
1321
1322	return genlmsg_reply(msg, info);
1323}
1324
1325static int devlink_nl_selftests_get_dump_one(struct sk_buff *msg,
1326					     struct devlink *devlink,
1327					     struct netlink_callback *cb,
1328					     int flags)
1329{
1330	if (!devlink->ops->selftest_check)
1331		return 0;
1332
1333	return devlink_nl_selftests_fill(msg, devlink,
1334					 NETLINK_CB(cb->skb).portid,
1335					 cb->nlh->nlmsg_seq, flags,
1336					 cb->extack);
1337}
1338
1339int devlink_nl_selftests_get_dumpit(struct sk_buff *skb,
1340				    struct netlink_callback *cb)
1341{
1342	return devlink_nl_dumpit(skb, cb, devlink_nl_selftests_get_dump_one);
1343}
1344
1345static int devlink_selftest_result_put(struct sk_buff *skb, unsigned int id,
1346				       enum devlink_selftest_status test_status)
1347{
1348	struct nlattr *result_attr;
1349
1350	result_attr = nla_nest_start(skb, DEVLINK_ATTR_SELFTEST_RESULT);
1351	if (!result_attr)
1352		return -EMSGSIZE;
1353
1354	if (nla_put_u32(skb, DEVLINK_ATTR_SELFTEST_RESULT_ID, id) ||
1355	    nla_put_u8(skb, DEVLINK_ATTR_SELFTEST_RESULT_STATUS,
1356		       test_status))
1357		goto nla_put_failure;
1358
1359	nla_nest_end(skb, result_attr);
1360	return 0;
1361
1362nla_put_failure:
1363	nla_nest_cancel(skb, result_attr);
1364	return -EMSGSIZE;
1365}
1366
1367static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_ID_MAX + 1] = {
1368	[DEVLINK_ATTR_SELFTEST_ID_FLASH] = { .type = NLA_FLAG },
1369};
1370
1371int devlink_nl_selftests_run_doit(struct sk_buff *skb, struct genl_info *info)
1372{
1373	struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
1374	struct devlink *devlink = info->user_ptr[0];
1375	struct nlattr *attrs, *selftests;
1376	struct sk_buff *msg;
1377	void *hdr;
1378	int err;
1379	int i;
1380
1381	if (!devlink->ops->selftest_run || !devlink->ops->selftest_check)
1382		return -EOPNOTSUPP;
1383
1384	if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SELFTESTS))
1385		return -EINVAL;
1386
1387	attrs = info->attrs[DEVLINK_ATTR_SELFTESTS];
1388
1389	err = nla_parse_nested(tb, DEVLINK_ATTR_SELFTEST_ID_MAX, attrs,
1390			       devlink_selftest_nl_policy, info->extack);
1391	if (err < 0)
1392		return err;
1393
1394	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1395	if (!msg)
1396		return -ENOMEM;
1397
1398	err = -EMSGSIZE;
1399	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
1400			  &devlink_nl_family, 0, DEVLINK_CMD_SELFTESTS_RUN);
1401	if (!hdr)
1402		goto free_msg;
1403
1404	if (devlink_nl_put_handle(msg, devlink))
1405		goto genlmsg_cancel;
1406
1407	selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
1408	if (!selftests)
1409		goto genlmsg_cancel;
1410
1411	for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
1412	     i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
1413		enum devlink_selftest_status test_status;
1414
1415		if (nla_get_flag(tb[i])) {
1416			if (!devlink->ops->selftest_check(devlink, i,
1417							  info->extack)) {
1418				if (devlink_selftest_result_put(msg, i,
1419								DEVLINK_SELFTEST_STATUS_SKIP))
1420					goto selftests_nest_cancel;
1421				continue;
1422			}
1423
1424			test_status = devlink->ops->selftest_run(devlink, i,
1425								 info->extack);
1426			if (devlink_selftest_result_put(msg, i, test_status))
1427				goto selftests_nest_cancel;
1428		}
1429	}
1430
1431	nla_nest_end(msg, selftests);
1432	genlmsg_end(msg, hdr);
1433	return genlmsg_reply(msg, info);
1434
1435selftests_nest_cancel:
1436	nla_nest_cancel(msg, selftests);
1437genlmsg_cancel:
1438	genlmsg_cancel(msg, hdr);
1439free_msg:
1440	nlmsg_free(msg);
1441	return err;
1442}