xref: /linux/drivers/net/ethernet/intel/ice/devlink/devlink.c (revision 6af91e3d2cfc8bb579b1aa2d22cd91f8c34acdf6)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
3 
4 #include <linux/vmalloc.h>
5 
6 #include "ice.h"
7 #include "ice_lib.h"
8 #include "devlink.h"
9 #include "ice_eswitch.h"
10 #include "ice_fw_update.h"
11 #include "ice_dcb_lib.h"
12 
13 /* context for devlink info version reporting */
14 struct ice_info_ctx {
15 	char buf[128];
16 	struct ice_orom_info pending_orom;
17 	struct ice_nvm_info pending_nvm;
18 	struct ice_netlist_info pending_netlist;
19 	struct ice_hw_dev_caps dev_caps;
20 };
21 
22 /* The following functions are used to format specific strings for various
23  * devlink info versions. The ctx parameter is used to provide the storage
24  * buffer, as well as any ancillary information calculated when the info
25  * request was made.
26  *
27  * If a version does not exist, for example when attempting to get the
28  * inactive version of flash when there is no pending update, the function
29  * should leave the buffer in the ctx structure empty.
30  */
31 
32 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
33 {
34 	u8 dsn[8];
35 
36 	/* Copy the DSN into an array in Big Endian format */
37 	put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
38 
39 	snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
40 }
41 
42 static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
43 {
44 	struct ice_hw *hw = &pf->hw;
45 	int status;
46 
47 	status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
48 	if (status)
49 		/* We failed to locate the PBA, so just skip this entry */
50 		dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %d\n",
51 			status);
52 }
53 
54 static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
55 {
56 	struct ice_hw *hw = &pf->hw;
57 
58 	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
59 		 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
60 }
61 
62 static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
63 {
64 	struct ice_hw *hw = &pf->hw;
65 
66 	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
67 		 hw->api_min_ver, hw->api_patch);
68 }
69 
70 static void ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
71 {
72 	struct ice_hw *hw = &pf->hw;
73 
74 	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
75 }
76 
77 static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
78 {
79 	struct ice_orom_info *orom = &pf->hw.flash.orom;
80 
81 	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
82 		 orom->major, orom->build, orom->patch);
83 }
84 
85 static void
86 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
87 			  struct ice_info_ctx *ctx)
88 {
89 	struct ice_orom_info *orom = &ctx->pending_orom;
90 
91 	if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
92 		snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
93 			 orom->major, orom->build, orom->patch);
94 }
95 
96 static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
97 {
98 	struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
99 
100 	snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
101 }
102 
103 static void
104 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
105 			 struct ice_info_ctx *ctx)
106 {
107 	struct ice_nvm_info *nvm = &ctx->pending_nvm;
108 
109 	if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
110 		snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
111 			 nvm->major, nvm->minor);
112 }
113 
114 static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
115 {
116 	struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
117 
118 	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
119 }
120 
121 static void
122 ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
123 {
124 	struct ice_nvm_info *nvm = &ctx->pending_nvm;
125 
126 	if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
127 		snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
128 }
129 
130 static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
131 {
132 	struct ice_hw *hw = &pf->hw;
133 
134 	snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
135 }
136 
137 static void
138 ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
139 {
140 	struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
141 
142 	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
143 		 pkg->major, pkg->minor, pkg->update, pkg->draft);
144 }
145 
146 static void
147 ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
148 {
149 	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
150 }
151 
152 static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
153 {
154 	struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
155 
156 	/* The netlist version fields are BCD formatted */
157 	snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
158 		 netlist->major, netlist->minor,
159 		 netlist->type >> 16, netlist->type & 0xFFFF,
160 		 netlist->rev, netlist->cust_ver);
161 }
162 
163 static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
164 {
165 	struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
166 
167 	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
168 }
169 
170 static void
171 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
172 			     struct ice_info_ctx *ctx)
173 {
174 	struct ice_netlist_info *netlist = &ctx->pending_netlist;
175 
176 	/* The netlist version fields are BCD formatted */
177 	if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
178 		snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
179 			 netlist->major, netlist->minor,
180 			 netlist->type >> 16, netlist->type & 0xFFFF,
181 			 netlist->rev, netlist->cust_ver);
182 }
183 
184 static void
185 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
186 			       struct ice_info_ctx *ctx)
187 {
188 	struct ice_netlist_info *netlist = &ctx->pending_netlist;
189 
190 	if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
191 		snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
192 }
193 
194 static void ice_info_cgu_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
195 {
196 	u32 id, cfg_ver, fw_ver;
197 
198 	if (!ice_is_feature_supported(pf, ICE_F_CGU))
199 		return;
200 	if (ice_aq_get_cgu_info(&pf->hw, &id, &cfg_ver, &fw_ver))
201 		return;
202 	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", id, cfg_ver, fw_ver);
203 }
204 
205 static void ice_info_cgu_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
206 {
207 	if (!ice_is_feature_supported(pf, ICE_F_CGU))
208 		return;
209 	snprintf(ctx->buf, sizeof(ctx->buf), "%u", pf->hw.cgu_part_number);
210 }
211 
212 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
213 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
214 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
215 
216 /* The combined() macro inserts both the running entry as well as a stored
217  * entry. The running entry will always report the version from the active
218  * handler. The stored entry will first try the pending handler, and fallback
219  * to the active handler if the pending function does not report a version.
220  * The pending handler should check the status of a pending update for the
221  * relevant flash component. It should only fill in the buffer in the case
222  * where a valid pending version is available. This ensures that the related
223  * stored and running versions remain in sync, and that stored versions are
224  * correctly reported as expected.
225  */
226 #define combined(key, active, pending) \
227 	running(key, active), \
228 	stored(key, pending, active)
229 
230 enum ice_version_type {
231 	ICE_VERSION_FIXED,
232 	ICE_VERSION_RUNNING,
233 	ICE_VERSION_STORED,
234 };
235 
236 static const struct ice_devlink_version {
237 	enum ice_version_type type;
238 	const char *key;
239 	void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
240 	void (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
241 } ice_devlink_versions[] = {
242 	fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
243 	running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
244 	running("fw.mgmt.api", ice_info_fw_api),
245 	running("fw.mgmt.build", ice_info_fw_build),
246 	combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
247 	combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
248 	combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
249 	running("fw.app.name", ice_info_ddp_pkg_name),
250 	running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
251 	running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
252 	combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
253 	combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
254 	fixed("cgu.id", ice_info_cgu_id),
255 	running("fw.cgu", ice_info_cgu_fw_build),
256 };
257 
258 /**
259  * ice_devlink_info_get - .info_get devlink handler
260  * @devlink: devlink instance structure
261  * @req: the devlink info request
262  * @extack: extended netdev ack structure
263  *
264  * Callback for the devlink .info_get operation. Reports information about the
265  * device.
266  *
267  * Return: zero on success or an error code on failure.
268  */
269 static int ice_devlink_info_get(struct devlink *devlink,
270 				struct devlink_info_req *req,
271 				struct netlink_ext_ack *extack)
272 {
273 	struct ice_pf *pf = devlink_priv(devlink);
274 	struct device *dev = ice_pf_to_dev(pf);
275 	struct ice_hw *hw = &pf->hw;
276 	struct ice_info_ctx *ctx;
277 	size_t i;
278 	int err;
279 
280 	err = ice_wait_for_reset(pf, 10 * HZ);
281 	if (err) {
282 		NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
283 		return err;
284 	}
285 
286 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
287 	if (!ctx)
288 		return -ENOMEM;
289 
290 	/* discover capabilities first */
291 	err = ice_discover_dev_caps(hw, &ctx->dev_caps);
292 	if (err) {
293 		dev_dbg(dev, "Failed to discover device capabilities, status %d aq_err %s\n",
294 			err, ice_aq_str(hw->adminq.sq_last_status));
295 		NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
296 		goto out_free_ctx;
297 	}
298 
299 	if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
300 		err = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
301 		if (err) {
302 			dev_dbg(dev, "Unable to read inactive Option ROM version data, status %d aq_err %s\n",
303 				err, ice_aq_str(hw->adminq.sq_last_status));
304 
305 			/* disable display of pending Option ROM */
306 			ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
307 		}
308 	}
309 
310 	if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
311 		err = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
312 		if (err) {
313 			dev_dbg(dev, "Unable to read inactive NVM version data, status %d aq_err %s\n",
314 				err, ice_aq_str(hw->adminq.sq_last_status));
315 
316 			/* disable display of pending Option ROM */
317 			ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
318 		}
319 	}
320 
321 	if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
322 		err = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
323 		if (err) {
324 			dev_dbg(dev, "Unable to read inactive Netlist version data, status %d aq_err %s\n",
325 				err, ice_aq_str(hw->adminq.sq_last_status));
326 
327 			/* disable display of pending Option ROM */
328 			ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
329 		}
330 	}
331 
332 	ice_info_get_dsn(pf, ctx);
333 
334 	err = devlink_info_serial_number_put(req, ctx->buf);
335 	if (err) {
336 		NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
337 		goto out_free_ctx;
338 	}
339 
340 	for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
341 		enum ice_version_type type = ice_devlink_versions[i].type;
342 		const char *key = ice_devlink_versions[i].key;
343 
344 		memset(ctx->buf, 0, sizeof(ctx->buf));
345 
346 		ice_devlink_versions[i].getter(pf, ctx);
347 
348 		/* If the default getter doesn't report a version, use the
349 		 * fallback function. This is primarily useful in the case of
350 		 * "stored" versions that want to report the same value as the
351 		 * running version in the normal case of no pending update.
352 		 */
353 		if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
354 			ice_devlink_versions[i].fallback(pf, ctx);
355 
356 		/* Do not report missing versions */
357 		if (ctx->buf[0] == '\0')
358 			continue;
359 
360 		switch (type) {
361 		case ICE_VERSION_FIXED:
362 			err = devlink_info_version_fixed_put(req, key, ctx->buf);
363 			if (err) {
364 				NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
365 				goto out_free_ctx;
366 			}
367 			break;
368 		case ICE_VERSION_RUNNING:
369 			err = devlink_info_version_running_put(req, key, ctx->buf);
370 			if (err) {
371 				NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
372 				goto out_free_ctx;
373 			}
374 			break;
375 		case ICE_VERSION_STORED:
376 			err = devlink_info_version_stored_put(req, key, ctx->buf);
377 			if (err) {
378 				NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
379 				goto out_free_ctx;
380 			}
381 			break;
382 		}
383 	}
384 
385 out_free_ctx:
386 	kfree(ctx);
387 	return err;
388 }
389 
390 /**
391  * ice_devlink_reload_empr_start - Start EMP reset to activate new firmware
392  * @pf: pointer to the pf instance
393  * @extack: netlink extended ACK structure
394  *
395  * Allow user to activate new Embedded Management Processor firmware by
396  * issuing device specific EMP reset. Called in response to
397  * a DEVLINK_CMD_RELOAD with the DEVLINK_RELOAD_ACTION_FW_ACTIVATE.
398  *
399  * Note that teardown and rebuild of the driver state happens automatically as
400  * part of an interrupt and watchdog task. This is because all physical
401  * functions on the device must be able to reset when an EMP reset occurs from
402  * any source.
403  */
404 static int
405 ice_devlink_reload_empr_start(struct ice_pf *pf,
406 			      struct netlink_ext_ack *extack)
407 {
408 	struct device *dev = ice_pf_to_dev(pf);
409 	struct ice_hw *hw = &pf->hw;
410 	u8 pending;
411 	int err;
412 
413 	err = ice_get_pending_updates(pf, &pending, extack);
414 	if (err)
415 		return err;
416 
417 	/* pending is a bitmask of which flash banks have a pending update,
418 	 * including the main NVM bank, the Option ROM bank, and the netlist
419 	 * bank. If any of these bits are set, then there is a pending update
420 	 * waiting to be activated.
421 	 */
422 	if (!pending) {
423 		NL_SET_ERR_MSG_MOD(extack, "No pending firmware update");
424 		return -ECANCELED;
425 	}
426 
427 	if (pf->fw_emp_reset_disabled) {
428 		NL_SET_ERR_MSG_MOD(extack, "EMP reset is not available. To activate firmware, a reboot or power cycle is needed");
429 		return -ECANCELED;
430 	}
431 
432 	dev_dbg(dev, "Issuing device EMP reset to activate firmware\n");
433 
434 	err = ice_aq_nvm_update_empr(hw);
435 	if (err) {
436 		dev_err(dev, "Failed to trigger EMP device reset to reload firmware, err %d aq_err %s\n",
437 			err, ice_aq_str(hw->adminq.sq_last_status));
438 		NL_SET_ERR_MSG_MOD(extack, "Failed to trigger EMP device reset to reload firmware");
439 		return err;
440 	}
441 
442 	return 0;
443 }
444 
445 /**
446  * ice_devlink_reinit_down - unload given PF
447  * @pf: pointer to the PF struct
448  */
449 static void ice_devlink_reinit_down(struct ice_pf *pf)
450 {
451 	/* No need to take devl_lock, it's already taken by devlink API */
452 	ice_unload(pf);
453 	rtnl_lock();
454 	ice_vsi_decfg(ice_get_main_vsi(pf));
455 	rtnl_unlock();
456 	ice_deinit_dev(pf);
457 }
458 
459 /**
460  * ice_devlink_reload_down - prepare for reload
461  * @devlink: pointer to the devlink instance to reload
462  * @netns_change: if true, the network namespace is changing
463  * @action: the action to perform
464  * @limit: limits on what reload should do, such as not resetting
465  * @extack: netlink extended ACK structure
466  */
467 static int
468 ice_devlink_reload_down(struct devlink *devlink, bool netns_change,
469 			enum devlink_reload_action action,
470 			enum devlink_reload_limit limit,
471 			struct netlink_ext_ack *extack)
472 {
473 	struct ice_pf *pf = devlink_priv(devlink);
474 
475 	switch (action) {
476 	case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
477 		if (ice_is_eswitch_mode_switchdev(pf)) {
478 			NL_SET_ERR_MSG_MOD(extack,
479 					   "Go to legacy mode before doing reinit");
480 			return -EOPNOTSUPP;
481 		}
482 		if (ice_is_adq_active(pf)) {
483 			NL_SET_ERR_MSG_MOD(extack,
484 					   "Turn off ADQ before doing reinit");
485 			return -EOPNOTSUPP;
486 		}
487 		if (ice_has_vfs(pf)) {
488 			NL_SET_ERR_MSG_MOD(extack,
489 					   "Remove all VFs before doing reinit");
490 			return -EOPNOTSUPP;
491 		}
492 		ice_devlink_reinit_down(pf);
493 		return 0;
494 	case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
495 		return ice_devlink_reload_empr_start(pf, extack);
496 	default:
497 		WARN_ON(1);
498 		return -EOPNOTSUPP;
499 	}
500 }
501 
502 /**
503  * ice_devlink_reload_empr_finish - Wait for EMP reset to finish
504  * @pf: pointer to the pf instance
505  * @extack: netlink extended ACK structure
506  *
507  * Wait for driver to finish rebuilding after EMP reset is completed. This
508  * includes time to wait for both the actual device reset as well as the time
509  * for the driver's rebuild to complete.
510  */
511 static int
512 ice_devlink_reload_empr_finish(struct ice_pf *pf,
513 			       struct netlink_ext_ack *extack)
514 {
515 	int err;
516 
517 	err = ice_wait_for_reset(pf, 60 * HZ);
518 	if (err) {
519 		NL_SET_ERR_MSG_MOD(extack, "Device still resetting after 1 minute");
520 		return err;
521 	}
522 
523 	return 0;
524 }
525 
526 /**
527  * ice_get_tx_topo_user_sel - Read user's choice from flash
528  * @pf: pointer to pf structure
529  * @layers: value read from flash will be saved here
530  *
531  * Reads user's preference for Tx Scheduler Topology Tree from PFA TLV.
532  *
533  * Return: zero when read was successful, negative values otherwise.
534  */
535 static int ice_get_tx_topo_user_sel(struct ice_pf *pf, uint8_t *layers)
536 {
537 	struct ice_aqc_nvm_tx_topo_user_sel usr_sel = {};
538 	struct ice_hw *hw = &pf->hw;
539 	int err;
540 
541 	err = ice_acquire_nvm(hw, ICE_RES_READ);
542 	if (err)
543 		return err;
544 
545 	err = ice_aq_read_nvm(hw, ICE_AQC_NVM_TX_TOPO_MOD_ID, 0,
546 			      sizeof(usr_sel), &usr_sel, true, true, NULL);
547 	if (err)
548 		goto exit_release_res;
549 
550 	if (usr_sel.data & ICE_AQC_NVM_TX_TOPO_USER_SEL)
551 		*layers = ICE_SCHED_5_LAYERS;
552 	else
553 		*layers = ICE_SCHED_9_LAYERS;
554 
555 exit_release_res:
556 	ice_release_nvm(hw);
557 
558 	return err;
559 }
560 
561 /**
562  * ice_update_tx_topo_user_sel - Save user's preference in flash
563  * @pf: pointer to pf structure
564  * @layers: value to be saved in flash
565  *
566  * Variable "layers" defines user's preference about number of layers in Tx
567  * Scheduler Topology Tree. This choice should be stored in PFA TLV field
568  * and be picked up by driver, next time during init.
569  *
570  * Return: zero when save was successful, negative values otherwise.
571  */
572 static int ice_update_tx_topo_user_sel(struct ice_pf *pf, int layers)
573 {
574 	struct ice_aqc_nvm_tx_topo_user_sel usr_sel = {};
575 	struct ice_hw *hw = &pf->hw;
576 	int err;
577 
578 	err = ice_acquire_nvm(hw, ICE_RES_WRITE);
579 	if (err)
580 		return err;
581 
582 	err = ice_aq_read_nvm(hw, ICE_AQC_NVM_TX_TOPO_MOD_ID, 0,
583 			      sizeof(usr_sel), &usr_sel, true, true, NULL);
584 	if (err)
585 		goto exit_release_res;
586 
587 	if (layers == ICE_SCHED_5_LAYERS)
588 		usr_sel.data |= ICE_AQC_NVM_TX_TOPO_USER_SEL;
589 	else
590 		usr_sel.data &= ~ICE_AQC_NVM_TX_TOPO_USER_SEL;
591 
592 	err = ice_write_one_nvm_block(pf, ICE_AQC_NVM_TX_TOPO_MOD_ID, 2,
593 				      sizeof(usr_sel.data), &usr_sel.data,
594 				      true, NULL, NULL);
595 exit_release_res:
596 	ice_release_nvm(hw);
597 
598 	return err;
599 }
600 
601 /**
602  * ice_devlink_tx_sched_layers_get - Get tx_scheduling_layers parameter
603  * @devlink: pointer to the devlink instance
604  * @id: the parameter ID to set
605  * @ctx: context to store the parameter value
606  *
607  * Return: zero on success and negative value on failure.
608  */
609 static int ice_devlink_tx_sched_layers_get(struct devlink *devlink, u32 id,
610 					   struct devlink_param_gset_ctx *ctx)
611 {
612 	struct ice_pf *pf = devlink_priv(devlink);
613 	int err;
614 
615 	err = ice_get_tx_topo_user_sel(pf, &ctx->val.vu8);
616 	if (err)
617 		return err;
618 
619 	return 0;
620 }
621 
622 /**
623  * ice_devlink_tx_sched_layers_set - Set tx_scheduling_layers parameter
624  * @devlink: pointer to the devlink instance
625  * @id: the parameter ID to set
626  * @ctx: context to get the parameter value
627  * @extack: netlink extended ACK structure
628  *
629  * Return: zero on success and negative value on failure.
630  */
631 static int ice_devlink_tx_sched_layers_set(struct devlink *devlink, u32 id,
632 					   struct devlink_param_gset_ctx *ctx,
633 					   struct netlink_ext_ack *extack)
634 {
635 	struct ice_pf *pf = devlink_priv(devlink);
636 	int err;
637 
638 	err = ice_update_tx_topo_user_sel(pf, ctx->val.vu8);
639 	if (err)
640 		return err;
641 
642 	NL_SET_ERR_MSG_MOD(extack,
643 			   "Tx scheduling layers have been changed on this device. You must do the PCI slot powercycle for the change to take effect.");
644 
645 	return 0;
646 }
647 
648 /**
649  * ice_devlink_tx_sched_layers_validate - Validate passed tx_scheduling_layers
650  *                                        parameter value
651  * @devlink: unused pointer to devlink instance
652  * @id: the parameter ID to validate
653  * @val: value to validate
654  * @extack: netlink extended ACK structure
655  *
656  * Supported values are:
657  * - 5 - five layers Tx Scheduler Topology Tree
658  * - 9 - nine layers Tx Scheduler Topology Tree
659  *
660  * Return: zero when passed parameter value is supported. Negative value on
661  * error.
662  */
663 static int ice_devlink_tx_sched_layers_validate(struct devlink *devlink, u32 id,
664 						union devlink_param_value val,
665 						struct netlink_ext_ack *extack)
666 {
667 	if (val.vu8 != ICE_SCHED_5_LAYERS && val.vu8 != ICE_SCHED_9_LAYERS) {
668 		NL_SET_ERR_MSG_MOD(extack,
669 				   "Wrong number of tx scheduler layers provided.");
670 		return -EINVAL;
671 	}
672 
673 	return 0;
674 }
675 
676 /**
677  * ice_tear_down_devlink_rate_tree - removes devlink-rate exported tree
678  * @pf: pf struct
679  *
680  * This function tears down tree exported during VF's creation.
681  */
682 void ice_tear_down_devlink_rate_tree(struct ice_pf *pf)
683 {
684 	struct devlink *devlink;
685 	struct ice_vf *vf;
686 	unsigned int bkt;
687 
688 	devlink = priv_to_devlink(pf);
689 
690 	devl_lock(devlink);
691 	mutex_lock(&pf->vfs.table_lock);
692 	ice_for_each_vf(pf, bkt, vf) {
693 		if (vf->devlink_port.devlink_rate)
694 			devl_rate_leaf_destroy(&vf->devlink_port);
695 	}
696 	mutex_unlock(&pf->vfs.table_lock);
697 
698 	devl_rate_nodes_destroy(devlink);
699 	devl_unlock(devlink);
700 }
701 
702 /**
703  * ice_enable_custom_tx - try to enable custom Tx feature
704  * @pf: pf struct
705  *
706  * This function tries to enable custom Tx feature,
707  * it's not possible to enable it, if DCB or ADQ is active.
708  */
709 static bool ice_enable_custom_tx(struct ice_pf *pf)
710 {
711 	struct ice_port_info *pi = ice_get_main_vsi(pf)->port_info;
712 	struct device *dev = ice_pf_to_dev(pf);
713 
714 	if (pi->is_custom_tx_enabled)
715 		/* already enabled, return true */
716 		return true;
717 
718 	if (ice_is_adq_active(pf)) {
719 		dev_err(dev, "ADQ active, can't modify Tx scheduler tree\n");
720 		return false;
721 	}
722 
723 	if (ice_is_dcb_active(pf)) {
724 		dev_err(dev, "DCB active, can't modify Tx scheduler tree\n");
725 		return false;
726 	}
727 
728 	pi->is_custom_tx_enabled = true;
729 
730 	return true;
731 }
732 
733 /**
734  * ice_traverse_tx_tree - traverse Tx scheduler tree
735  * @devlink: devlink struct
736  * @node: current node, used for recursion
737  * @tc_node: tc_node struct, that is treated as a root
738  * @pf: pf struct
739  *
740  * This function traverses Tx scheduler tree and exports
741  * entire structure to the devlink-rate.
742  */
743 static void ice_traverse_tx_tree(struct devlink *devlink, struct ice_sched_node *node,
744 				 struct ice_sched_node *tc_node, struct ice_pf *pf)
745 {
746 	struct devlink_rate *rate_node = NULL;
747 	struct ice_vf *vf;
748 	int i;
749 
750 	if (node->rate_node)
751 		/* already added, skip to the next */
752 		goto traverse_children;
753 
754 	if (node->parent == tc_node) {
755 		/* create root node */
756 		rate_node = devl_rate_node_create(devlink, node, node->name, NULL);
757 	} else if (node->vsi_handle &&
758 		   pf->vsi[node->vsi_handle]->vf) {
759 		vf = pf->vsi[node->vsi_handle]->vf;
760 		if (!vf->devlink_port.devlink_rate)
761 			/* leaf nodes doesn't have children
762 			 * so we don't set rate_node
763 			 */
764 			devl_rate_leaf_create(&vf->devlink_port, node,
765 					      node->parent->rate_node);
766 	} else if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF &&
767 		   node->parent->rate_node) {
768 		rate_node = devl_rate_node_create(devlink, node, node->name,
769 						  node->parent->rate_node);
770 	}
771 
772 	if (rate_node && !IS_ERR(rate_node))
773 		node->rate_node = rate_node;
774 
775 traverse_children:
776 	for (i = 0; i < node->num_children; i++)
777 		ice_traverse_tx_tree(devlink, node->children[i], tc_node, pf);
778 }
779 
780 /**
781  * ice_devlink_rate_init_tx_topology - export Tx scheduler tree to devlink rate
782  * @devlink: devlink struct
783  * @vsi: main vsi struct
784  *
785  * This function finds a root node, then calls ice_traverse_tx tree, which
786  * traverses the tree and exports it's contents to devlink rate.
787  */
788 int ice_devlink_rate_init_tx_topology(struct devlink *devlink, struct ice_vsi *vsi)
789 {
790 	struct ice_port_info *pi = vsi->port_info;
791 	struct ice_sched_node *tc_node;
792 	struct ice_pf *pf = vsi->back;
793 	int i;
794 
795 	tc_node = pi->root->children[0];
796 	mutex_lock(&pi->sched_lock);
797 	for (i = 0; i < tc_node->num_children; i++)
798 		ice_traverse_tx_tree(devlink, tc_node->children[i], tc_node, pf);
799 	mutex_unlock(&pi->sched_lock);
800 
801 	return 0;
802 }
803 
804 static void ice_clear_rate_nodes(struct ice_sched_node *node)
805 {
806 	node->rate_node = NULL;
807 
808 	for (int i = 0; i < node->num_children; i++)
809 		ice_clear_rate_nodes(node->children[i]);
810 }
811 
812 /**
813  * ice_devlink_rate_clear_tx_topology - clear node->rate_node
814  * @vsi: main vsi struct
815  *
816  * Clear rate_node to cleanup creation of Tx topology.
817  *
818  */
819 void ice_devlink_rate_clear_tx_topology(struct ice_vsi *vsi)
820 {
821 	struct ice_port_info *pi = vsi->port_info;
822 
823 	mutex_lock(&pi->sched_lock);
824 	ice_clear_rate_nodes(pi->root->children[0]);
825 	mutex_unlock(&pi->sched_lock);
826 }
827 
828 /**
829  * ice_set_object_tx_share - sets node scheduling parameter
830  * @pi: devlink struct instance
831  * @node: node struct instance
832  * @bw: bandwidth in bytes per second
833  * @extack: extended netdev ack structure
834  *
835  * This function sets ICE_MIN_BW scheduling BW limit.
836  */
837 static int ice_set_object_tx_share(struct ice_port_info *pi, struct ice_sched_node *node,
838 				   u64 bw, struct netlink_ext_ack *extack)
839 {
840 	int status;
841 
842 	mutex_lock(&pi->sched_lock);
843 	/* converts bytes per second to kilo bits per second */
844 	node->tx_share = div_u64(bw, 125);
845 	status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW, node->tx_share);
846 	mutex_unlock(&pi->sched_lock);
847 
848 	if (status)
849 		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_share");
850 
851 	return status;
852 }
853 
854 /**
855  * ice_set_object_tx_max - sets node scheduling parameter
856  * @pi: devlink struct instance
857  * @node: node struct instance
858  * @bw: bandwidth in bytes per second
859  * @extack: extended netdev ack structure
860  *
861  * This function sets ICE_MAX_BW scheduling BW limit.
862  */
863 static int ice_set_object_tx_max(struct ice_port_info *pi, struct ice_sched_node *node,
864 				 u64 bw, struct netlink_ext_ack *extack)
865 {
866 	int status;
867 
868 	mutex_lock(&pi->sched_lock);
869 	/* converts bytes per second value to kilo bits per second */
870 	node->tx_max = div_u64(bw, 125);
871 	status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW, node->tx_max);
872 	mutex_unlock(&pi->sched_lock);
873 
874 	if (status)
875 		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_max");
876 
877 	return status;
878 }
879 
880 /**
881  * ice_set_object_tx_priority - sets node scheduling parameter
882  * @pi: devlink struct instance
883  * @node: node struct instance
884  * @priority: value representing priority for strict priority arbitration
885  * @extack: extended netdev ack structure
886  *
887  * This function sets priority of node among siblings.
888  */
889 static int ice_set_object_tx_priority(struct ice_port_info *pi, struct ice_sched_node *node,
890 				      u32 priority, struct netlink_ext_ack *extack)
891 {
892 	int status;
893 
894 	if (priority >= 8) {
895 		NL_SET_ERR_MSG_MOD(extack, "Priority should be less than 8");
896 		return -EINVAL;
897 	}
898 
899 	mutex_lock(&pi->sched_lock);
900 	node->tx_priority = priority;
901 	status = ice_sched_set_node_priority(pi, node, node->tx_priority);
902 	mutex_unlock(&pi->sched_lock);
903 
904 	if (status)
905 		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_priority");
906 
907 	return status;
908 }
909 
910 /**
911  * ice_set_object_tx_weight - sets node scheduling parameter
912  * @pi: devlink struct instance
913  * @node: node struct instance
914  * @weight: value represeting relative weight for WFQ arbitration
915  * @extack: extended netdev ack structure
916  *
917  * This function sets node weight for WFQ algorithm.
918  */
919 static int ice_set_object_tx_weight(struct ice_port_info *pi, struct ice_sched_node *node,
920 				    u32 weight, struct netlink_ext_ack *extack)
921 {
922 	int status;
923 
924 	if (weight > 200 || weight < 1) {
925 		NL_SET_ERR_MSG_MOD(extack, "Weight must be between 1 and 200");
926 		return -EINVAL;
927 	}
928 
929 	mutex_lock(&pi->sched_lock);
930 	node->tx_weight = weight;
931 	status = ice_sched_set_node_weight(pi, node, node->tx_weight);
932 	mutex_unlock(&pi->sched_lock);
933 
934 	if (status)
935 		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_weight");
936 
937 	return status;
938 }
939 
940 /**
941  * ice_get_pi_from_dev_rate - get port info from devlink_rate
942  * @rate_node: devlink struct instance
943  *
944  * This function returns corresponding port_info struct of devlink_rate
945  */
946 static struct ice_port_info *ice_get_pi_from_dev_rate(struct devlink_rate *rate_node)
947 {
948 	struct ice_pf *pf = devlink_priv(rate_node->devlink);
949 
950 	return ice_get_main_vsi(pf)->port_info;
951 }
952 
953 static int ice_devlink_rate_node_new(struct devlink_rate *rate_node, void **priv,
954 				     struct netlink_ext_ack *extack)
955 {
956 	struct ice_sched_node *node;
957 	struct ice_port_info *pi;
958 
959 	pi = ice_get_pi_from_dev_rate(rate_node);
960 
961 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
962 		return -EBUSY;
963 
964 	/* preallocate memory for ice_sched_node */
965 	node = devm_kzalloc(ice_hw_to_dev(pi->hw), sizeof(*node), GFP_KERNEL);
966 	*priv = node;
967 
968 	return 0;
969 }
970 
971 static int ice_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,
972 				     struct netlink_ext_ack *extack)
973 {
974 	struct ice_sched_node *node, *tc_node;
975 	struct ice_port_info *pi;
976 
977 	pi = ice_get_pi_from_dev_rate(rate_node);
978 	tc_node = pi->root->children[0];
979 	node = priv;
980 
981 	if (!rate_node->parent || !node || tc_node == node || !extack)
982 		return 0;
983 
984 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
985 		return -EBUSY;
986 
987 	/* can't allow to delete a node with children */
988 	if (node->num_children)
989 		return -EINVAL;
990 
991 	mutex_lock(&pi->sched_lock);
992 	ice_free_sched_node(pi, node);
993 	mutex_unlock(&pi->sched_lock);
994 
995 	return 0;
996 }
997 
998 static int ice_devlink_rate_leaf_tx_max_set(struct devlink_rate *rate_leaf, void *priv,
999 					    u64 tx_max, struct netlink_ext_ack *extack)
1000 {
1001 	struct ice_sched_node *node = priv;
1002 
1003 	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1004 		return -EBUSY;
1005 
1006 	if (!node)
1007 		return 0;
1008 
1009 	return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_leaf),
1010 				     node, tx_max, extack);
1011 }
1012 
1013 static int ice_devlink_rate_leaf_tx_share_set(struct devlink_rate *rate_leaf, void *priv,
1014 					      u64 tx_share, struct netlink_ext_ack *extack)
1015 {
1016 	struct ice_sched_node *node = priv;
1017 
1018 	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1019 		return -EBUSY;
1020 
1021 	if (!node)
1022 		return 0;
1023 
1024 	return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_leaf), node,
1025 				       tx_share, extack);
1026 }
1027 
1028 static int ice_devlink_rate_leaf_tx_priority_set(struct devlink_rate *rate_leaf, void *priv,
1029 						 u32 tx_priority, struct netlink_ext_ack *extack)
1030 {
1031 	struct ice_sched_node *node = priv;
1032 
1033 	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1034 		return -EBUSY;
1035 
1036 	if (!node)
1037 		return 0;
1038 
1039 	return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_leaf), node,
1040 					  tx_priority, extack);
1041 }
1042 
1043 static int ice_devlink_rate_leaf_tx_weight_set(struct devlink_rate *rate_leaf, void *priv,
1044 					       u32 tx_weight, struct netlink_ext_ack *extack)
1045 {
1046 	struct ice_sched_node *node = priv;
1047 
1048 	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1049 		return -EBUSY;
1050 
1051 	if (!node)
1052 		return 0;
1053 
1054 	return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_leaf), node,
1055 					tx_weight, extack);
1056 }
1057 
1058 static int ice_devlink_rate_node_tx_max_set(struct devlink_rate *rate_node, void *priv,
1059 					    u64 tx_max, struct netlink_ext_ack *extack)
1060 {
1061 	struct ice_sched_node *node = priv;
1062 
1063 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1064 		return -EBUSY;
1065 
1066 	if (!node)
1067 		return 0;
1068 
1069 	return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_node),
1070 				     node, tx_max, extack);
1071 }
1072 
1073 static int ice_devlink_rate_node_tx_share_set(struct devlink_rate *rate_node, void *priv,
1074 					      u64 tx_share, struct netlink_ext_ack *extack)
1075 {
1076 	struct ice_sched_node *node = priv;
1077 
1078 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1079 		return -EBUSY;
1080 
1081 	if (!node)
1082 		return 0;
1083 
1084 	return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_node),
1085 				       node, tx_share, extack);
1086 }
1087 
1088 static int ice_devlink_rate_node_tx_priority_set(struct devlink_rate *rate_node, void *priv,
1089 						 u32 tx_priority, struct netlink_ext_ack *extack)
1090 {
1091 	struct ice_sched_node *node = priv;
1092 
1093 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1094 		return -EBUSY;
1095 
1096 	if (!node)
1097 		return 0;
1098 
1099 	return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_node),
1100 					  node, tx_priority, extack);
1101 }
1102 
1103 static int ice_devlink_rate_node_tx_weight_set(struct devlink_rate *rate_node, void *priv,
1104 					       u32 tx_weight, struct netlink_ext_ack *extack)
1105 {
1106 	struct ice_sched_node *node = priv;
1107 
1108 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1109 		return -EBUSY;
1110 
1111 	if (!node)
1112 		return 0;
1113 
1114 	return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_node),
1115 					node, tx_weight, extack);
1116 }
1117 
1118 static int ice_devlink_set_parent(struct devlink_rate *devlink_rate,
1119 				  struct devlink_rate *parent,
1120 				  void *priv, void *parent_priv,
1121 				  struct netlink_ext_ack *extack)
1122 {
1123 	struct ice_port_info *pi = ice_get_pi_from_dev_rate(devlink_rate);
1124 	struct ice_sched_node *tc_node, *node, *parent_node;
1125 	u16 num_nodes_added;
1126 	u32 first_node_teid;
1127 	u32 node_teid;
1128 	int status;
1129 
1130 	tc_node = pi->root->children[0];
1131 	node = priv;
1132 
1133 	if (!extack)
1134 		return 0;
1135 
1136 	if (!ice_enable_custom_tx(devlink_priv(devlink_rate->devlink)))
1137 		return -EBUSY;
1138 
1139 	if (!parent) {
1140 		if (!node || tc_node == node || node->num_children)
1141 			return -EINVAL;
1142 
1143 		mutex_lock(&pi->sched_lock);
1144 		ice_free_sched_node(pi, node);
1145 		mutex_unlock(&pi->sched_lock);
1146 
1147 		return 0;
1148 	}
1149 
1150 	parent_node = parent_priv;
1151 
1152 	/* if the node doesn't exist, create it */
1153 	if (!node->parent) {
1154 		mutex_lock(&pi->sched_lock);
1155 		status = ice_sched_add_elems(pi, tc_node, parent_node,
1156 					     parent_node->tx_sched_layer + 1,
1157 					     1, &num_nodes_added, &first_node_teid,
1158 					     &node);
1159 		mutex_unlock(&pi->sched_lock);
1160 
1161 		if (status) {
1162 			NL_SET_ERR_MSG_MOD(extack, "Can't add a new node");
1163 			return status;
1164 		}
1165 
1166 		if (devlink_rate->tx_share)
1167 			ice_set_object_tx_share(pi, node, devlink_rate->tx_share, extack);
1168 		if (devlink_rate->tx_max)
1169 			ice_set_object_tx_max(pi, node, devlink_rate->tx_max, extack);
1170 		if (devlink_rate->tx_priority)
1171 			ice_set_object_tx_priority(pi, node, devlink_rate->tx_priority, extack);
1172 		if (devlink_rate->tx_weight)
1173 			ice_set_object_tx_weight(pi, node, devlink_rate->tx_weight, extack);
1174 	} else {
1175 		node_teid = le32_to_cpu(node->info.node_teid);
1176 		mutex_lock(&pi->sched_lock);
1177 		status = ice_sched_move_nodes(pi, parent_node, 1, &node_teid);
1178 		mutex_unlock(&pi->sched_lock);
1179 
1180 		if (status)
1181 			NL_SET_ERR_MSG_MOD(extack, "Can't move existing node to a new parent");
1182 	}
1183 
1184 	return status;
1185 }
1186 
1187 /**
1188  * ice_devlink_reinit_up - do reinit of the given PF
1189  * @pf: pointer to the PF struct
1190  */
1191 static int ice_devlink_reinit_up(struct ice_pf *pf)
1192 {
1193 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
1194 	int err;
1195 
1196 	err = ice_init_dev(pf);
1197 	if (err)
1198 		return err;
1199 
1200 	vsi->flags = ICE_VSI_FLAG_INIT;
1201 
1202 	rtnl_lock();
1203 	err = ice_vsi_cfg(vsi);
1204 	rtnl_unlock();
1205 	if (err)
1206 		goto err_vsi_cfg;
1207 
1208 	/* No need to take devl_lock, it's already taken by devlink API */
1209 	err = ice_load(pf);
1210 	if (err)
1211 		goto err_load;
1212 
1213 	return 0;
1214 
1215 err_load:
1216 	rtnl_lock();
1217 	ice_vsi_decfg(vsi);
1218 	rtnl_unlock();
1219 err_vsi_cfg:
1220 	ice_deinit_dev(pf);
1221 	return err;
1222 }
1223 
1224 /**
1225  * ice_devlink_reload_up - do reload up after reinit
1226  * @devlink: pointer to the devlink instance reloading
1227  * @action: the action requested
1228  * @limit: limits imposed by userspace, such as not resetting
1229  * @actions_performed: on return, indicate what actions actually performed
1230  * @extack: netlink extended ACK structure
1231  */
1232 static int
1233 ice_devlink_reload_up(struct devlink *devlink,
1234 		      enum devlink_reload_action action,
1235 		      enum devlink_reload_limit limit,
1236 		      u32 *actions_performed,
1237 		      struct netlink_ext_ack *extack)
1238 {
1239 	struct ice_pf *pf = devlink_priv(devlink);
1240 
1241 	switch (action) {
1242 	case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
1243 		*actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
1244 		return ice_devlink_reinit_up(pf);
1245 	case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
1246 		*actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE);
1247 		return ice_devlink_reload_empr_finish(pf, extack);
1248 	default:
1249 		WARN_ON(1);
1250 		return -EOPNOTSUPP;
1251 	}
1252 }
1253 
1254 static const struct devlink_ops ice_devlink_ops = {
1255 	.supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
1256 	.reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
1257 			  BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
1258 	.reload_down = ice_devlink_reload_down,
1259 	.reload_up = ice_devlink_reload_up,
1260 	.eswitch_mode_get = ice_eswitch_mode_get,
1261 	.eswitch_mode_set = ice_eswitch_mode_set,
1262 	.info_get = ice_devlink_info_get,
1263 	.flash_update = ice_devlink_flash_update,
1264 
1265 	.rate_node_new = ice_devlink_rate_node_new,
1266 	.rate_node_del = ice_devlink_rate_node_del,
1267 
1268 	.rate_leaf_tx_max_set = ice_devlink_rate_leaf_tx_max_set,
1269 	.rate_leaf_tx_share_set = ice_devlink_rate_leaf_tx_share_set,
1270 	.rate_leaf_tx_priority_set = ice_devlink_rate_leaf_tx_priority_set,
1271 	.rate_leaf_tx_weight_set = ice_devlink_rate_leaf_tx_weight_set,
1272 
1273 	.rate_node_tx_max_set = ice_devlink_rate_node_tx_max_set,
1274 	.rate_node_tx_share_set = ice_devlink_rate_node_tx_share_set,
1275 	.rate_node_tx_priority_set = ice_devlink_rate_node_tx_priority_set,
1276 	.rate_node_tx_weight_set = ice_devlink_rate_node_tx_weight_set,
1277 
1278 	.rate_leaf_parent_set = ice_devlink_set_parent,
1279 	.rate_node_parent_set = ice_devlink_set_parent,
1280 };
1281 
1282 static int
1283 ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
1284 			    struct devlink_param_gset_ctx *ctx)
1285 {
1286 	struct ice_pf *pf = devlink_priv(devlink);
1287 
1288 	ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false;
1289 
1290 	return 0;
1291 }
1292 
1293 static int ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
1294 				       struct devlink_param_gset_ctx *ctx,
1295 				       struct netlink_ext_ack *extack)
1296 {
1297 	struct ice_pf *pf = devlink_priv(devlink);
1298 	bool roce_ena = ctx->val.vbool;
1299 	int ret;
1300 
1301 	if (!roce_ena) {
1302 		ice_unplug_aux_dev(pf);
1303 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1304 		return 0;
1305 	}
1306 
1307 	pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
1308 	ret = ice_plug_aux_dev(pf);
1309 	if (ret)
1310 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1311 
1312 	return ret;
1313 }
1314 
1315 static int
1316 ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
1317 				 union devlink_param_value val,
1318 				 struct netlink_ext_ack *extack)
1319 {
1320 	struct ice_pf *pf = devlink_priv(devlink);
1321 
1322 	if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1323 		return -EOPNOTSUPP;
1324 
1325 	if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
1326 		NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1327 		return -EOPNOTSUPP;
1328 	}
1329 
1330 	return 0;
1331 }
1332 
1333 static int
1334 ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
1335 			  struct devlink_param_gset_ctx *ctx)
1336 {
1337 	struct ice_pf *pf = devlink_priv(devlink);
1338 
1339 	ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
1340 
1341 	return 0;
1342 }
1343 
1344 static int ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
1345 				     struct devlink_param_gset_ctx *ctx,
1346 				     struct netlink_ext_ack *extack)
1347 {
1348 	struct ice_pf *pf = devlink_priv(devlink);
1349 	bool iw_ena = ctx->val.vbool;
1350 	int ret;
1351 
1352 	if (!iw_ena) {
1353 		ice_unplug_aux_dev(pf);
1354 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1355 		return 0;
1356 	}
1357 
1358 	pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
1359 	ret = ice_plug_aux_dev(pf);
1360 	if (ret)
1361 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1362 
1363 	return ret;
1364 }
1365 
1366 static int
1367 ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
1368 			       union devlink_param_value val,
1369 			       struct netlink_ext_ack *extack)
1370 {
1371 	struct ice_pf *pf = devlink_priv(devlink);
1372 
1373 	if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1374 		return -EOPNOTSUPP;
1375 
1376 	if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
1377 		NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1378 		return -EOPNOTSUPP;
1379 	}
1380 
1381 	return 0;
1382 }
1383 
1384 #define DEVLINK_LOCAL_FWD_DISABLED_STR "disabled"
1385 #define DEVLINK_LOCAL_FWD_ENABLED_STR "enabled"
1386 #define DEVLINK_LOCAL_FWD_PRIORITIZED_STR "prioritized"
1387 
1388 /**
1389  * ice_devlink_local_fwd_mode_to_str - Get string for local_fwd mode.
1390  * @mode: local forwarding for mode used in port_info struct.
1391  *
1392  * Return: Mode respective string or "Invalid".
1393  */
1394 static const char *
1395 ice_devlink_local_fwd_mode_to_str(enum ice_local_fwd_mode mode)
1396 {
1397 	switch (mode) {
1398 	case ICE_LOCAL_FWD_MODE_ENABLED:
1399 		return DEVLINK_LOCAL_FWD_ENABLED_STR;
1400 	case ICE_LOCAL_FWD_MODE_PRIORITIZED:
1401 		return DEVLINK_LOCAL_FWD_PRIORITIZED_STR;
1402 	case ICE_LOCAL_FWD_MODE_DISABLED:
1403 		return DEVLINK_LOCAL_FWD_DISABLED_STR;
1404 	}
1405 
1406 	return "Invalid";
1407 }
1408 
1409 /**
1410  * ice_devlink_local_fwd_str_to_mode - Get local_fwd mode from string name.
1411  * @mode_str: local forwarding mode string.
1412  *
1413  * Return: Mode value or negative number if invalid.
1414  */
1415 static int ice_devlink_local_fwd_str_to_mode(const char *mode_str)
1416 {
1417 	if (!strcmp(mode_str, DEVLINK_LOCAL_FWD_ENABLED_STR))
1418 		return ICE_LOCAL_FWD_MODE_ENABLED;
1419 	else if (!strcmp(mode_str, DEVLINK_LOCAL_FWD_PRIORITIZED_STR))
1420 		return ICE_LOCAL_FWD_MODE_PRIORITIZED;
1421 	else if (!strcmp(mode_str, DEVLINK_LOCAL_FWD_DISABLED_STR))
1422 		return ICE_LOCAL_FWD_MODE_DISABLED;
1423 
1424 	return -EINVAL;
1425 }
1426 
1427 /**
1428  * ice_devlink_local_fwd_get - Get local_fwd parameter.
1429  * @devlink: Pointer to the devlink instance.
1430  * @id: The parameter ID to set.
1431  * @ctx: Context to store the parameter value.
1432  *
1433  * Return: Zero.
1434  */
1435 static int ice_devlink_local_fwd_get(struct devlink *devlink, u32 id,
1436 				     struct devlink_param_gset_ctx *ctx)
1437 {
1438 	struct ice_pf *pf = devlink_priv(devlink);
1439 	struct ice_port_info *pi;
1440 	const char *mode_str;
1441 
1442 	pi = pf->hw.port_info;
1443 	mode_str = ice_devlink_local_fwd_mode_to_str(pi->local_fwd_mode);
1444 	snprintf(ctx->val.vstr, sizeof(ctx->val.vstr), "%s", mode_str);
1445 
1446 	return 0;
1447 }
1448 
1449 /**
1450  * ice_devlink_local_fwd_set - Set local_fwd parameter.
1451  * @devlink: Pointer to the devlink instance.
1452  * @id: The parameter ID to set.
1453  * @ctx: Context to get the parameter value.
1454  * @extack: Netlink extended ACK structure.
1455  *
1456  * Return: Zero.
1457  */
1458 static int ice_devlink_local_fwd_set(struct devlink *devlink, u32 id,
1459 				     struct devlink_param_gset_ctx *ctx,
1460 				     struct netlink_ext_ack *extack)
1461 {
1462 	int new_local_fwd_mode = ice_devlink_local_fwd_str_to_mode(ctx->val.vstr);
1463 	struct ice_pf *pf = devlink_priv(devlink);
1464 	struct device *dev = ice_pf_to_dev(pf);
1465 	struct ice_port_info *pi;
1466 
1467 	pi = pf->hw.port_info;
1468 	if (pi->local_fwd_mode != new_local_fwd_mode) {
1469 		pi->local_fwd_mode = new_local_fwd_mode;
1470 		dev_info(dev, "Setting local_fwd to %s\n", ctx->val.vstr);
1471 		ice_schedule_reset(pf, ICE_RESET_CORER);
1472 	}
1473 
1474 	return 0;
1475 }
1476 
1477 /**
1478  * ice_devlink_local_fwd_validate - Validate passed local_fwd parameter value.
1479  * @devlink: Unused pointer to devlink instance.
1480  * @id: The parameter ID to validate.
1481  * @val: Value to validate.
1482  * @extack: Netlink extended ACK structure.
1483  *
1484  * Supported values are:
1485  * "enabled" - local_fwd is enabled, "disabled" - local_fwd is disabled
1486  * "prioritized" - local_fwd traffic is prioritized in scheduling.
1487  *
1488  * Return: Zero when passed parameter value is supported. Negative value on
1489  * error.
1490  */
1491 static int ice_devlink_local_fwd_validate(struct devlink *devlink, u32 id,
1492 					  union devlink_param_value val,
1493 					  struct netlink_ext_ack *extack)
1494 {
1495 	if (ice_devlink_local_fwd_str_to_mode(val.vstr) < 0) {
1496 		NL_SET_ERR_MSG_MOD(extack, "Error: Requested value is not supported.");
1497 		return -EINVAL;
1498 	}
1499 
1500 	return 0;
1501 }
1502 
1503 enum ice_param_id {
1504 	ICE_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
1505 	ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS,
1506 	ICE_DEVLINK_PARAM_ID_LOCAL_FWD,
1507 };
1508 
1509 static const struct devlink_param ice_dvl_rdma_params[] = {
1510 	DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1511 			      ice_devlink_enable_roce_get,
1512 			      ice_devlink_enable_roce_set,
1513 			      ice_devlink_enable_roce_validate),
1514 	DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1515 			      ice_devlink_enable_iw_get,
1516 			      ice_devlink_enable_iw_set,
1517 			      ice_devlink_enable_iw_validate),
1518 };
1519 
1520 static const struct devlink_param ice_dvl_sched_params[] = {
1521 	DEVLINK_PARAM_DRIVER(ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS,
1522 			     "tx_scheduling_layers",
1523 			     DEVLINK_PARAM_TYPE_U8,
1524 			     BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1525 			     ice_devlink_tx_sched_layers_get,
1526 			     ice_devlink_tx_sched_layers_set,
1527 			     ice_devlink_tx_sched_layers_validate),
1528 	DEVLINK_PARAM_DRIVER(ICE_DEVLINK_PARAM_ID_LOCAL_FWD,
1529 			     "local_forwarding", DEVLINK_PARAM_TYPE_STRING,
1530 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1531 			     ice_devlink_local_fwd_get,
1532 			     ice_devlink_local_fwd_set,
1533 			     ice_devlink_local_fwd_validate),
1534 };
1535 
1536 static void ice_devlink_free(void *devlink_ptr)
1537 {
1538 	devlink_free((struct devlink *)devlink_ptr);
1539 }
1540 
1541 /**
1542  * ice_allocate_pf - Allocate devlink and return PF structure pointer
1543  * @dev: the device to allocate for
1544  *
1545  * Allocate a devlink instance for this device and return the private area as
1546  * the PF structure. The devlink memory is kept track of through devres by
1547  * adding an action to remove it when unwinding.
1548  */
1549 struct ice_pf *ice_allocate_pf(struct device *dev)
1550 {
1551 	struct devlink *devlink;
1552 
1553 	devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
1554 	if (!devlink)
1555 		return NULL;
1556 
1557 	/* Add an action to teardown the devlink when unwinding the driver */
1558 	if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
1559 		return NULL;
1560 
1561 	return devlink_priv(devlink);
1562 }
1563 
1564 /**
1565  * ice_devlink_register - Register devlink interface for this PF
1566  * @pf: the PF to register the devlink for.
1567  *
1568  * Register the devlink instance associated with this physical function.
1569  *
1570  * Return: zero on success or an error code on failure.
1571  */
1572 void ice_devlink_register(struct ice_pf *pf)
1573 {
1574 	struct devlink *devlink = priv_to_devlink(pf);
1575 
1576 	devl_register(devlink);
1577 }
1578 
1579 /**
1580  * ice_devlink_unregister - Unregister devlink resources for this PF.
1581  * @pf: the PF structure to cleanup
1582  *
1583  * Releases resources used by devlink and cleans up associated memory.
1584  */
1585 void ice_devlink_unregister(struct ice_pf *pf)
1586 {
1587 	devl_unregister(priv_to_devlink(pf));
1588 }
1589 
1590 int ice_devlink_register_params(struct ice_pf *pf)
1591 {
1592 	struct devlink *devlink = priv_to_devlink(pf);
1593 	struct ice_hw *hw = &pf->hw;
1594 	int status;
1595 
1596 	status = devl_params_register(devlink, ice_dvl_rdma_params,
1597 				      ARRAY_SIZE(ice_dvl_rdma_params));
1598 	if (status)
1599 		return status;
1600 
1601 	if (hw->func_caps.common_cap.tx_sched_topo_comp_mode_en)
1602 		status = devl_params_register(devlink, ice_dvl_sched_params,
1603 					      ARRAY_SIZE(ice_dvl_sched_params));
1604 
1605 	return status;
1606 }
1607 
1608 void ice_devlink_unregister_params(struct ice_pf *pf)
1609 {
1610 	struct devlink *devlink = priv_to_devlink(pf);
1611 	struct ice_hw *hw = &pf->hw;
1612 
1613 	devl_params_unregister(devlink, ice_dvl_rdma_params,
1614 			       ARRAY_SIZE(ice_dvl_rdma_params));
1615 
1616 	if (hw->func_caps.common_cap.tx_sched_topo_comp_mode_en)
1617 		devl_params_unregister(devlink, ice_dvl_sched_params,
1618 				       ARRAY_SIZE(ice_dvl_sched_params));
1619 }
1620 
1621 #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
1622 
1623 static const struct devlink_region_ops ice_nvm_region_ops;
1624 static const struct devlink_region_ops ice_sram_region_ops;
1625 
1626 /**
1627  * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
1628  * @devlink: the devlink instance
1629  * @ops: the devlink region to snapshot
1630  * @extack: extended ACK response structure
1631  * @data: on exit points to snapshot data buffer
1632  *
1633  * This function is called in response to a DEVLINK_CMD_REGION_NEW for either
1634  * the nvm-flash or shadow-ram region.
1635  *
1636  * It captures a snapshot of the NVM or Shadow RAM flash contents. This
1637  * snapshot can then later be viewed via the DEVLINK_CMD_REGION_READ netlink
1638  * interface.
1639  *
1640  * @returns zero on success, and updates the data pointer. Returns a non-zero
1641  * error code on failure.
1642  */
1643 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
1644 				    const struct devlink_region_ops *ops,
1645 				    struct netlink_ext_ack *extack, u8 **data)
1646 {
1647 	struct ice_pf *pf = devlink_priv(devlink);
1648 	struct device *dev = ice_pf_to_dev(pf);
1649 	struct ice_hw *hw = &pf->hw;
1650 	bool read_shadow_ram;
1651 	u8 *nvm_data, *tmp, i;
1652 	u32 nvm_size, left;
1653 	s8 num_blks;
1654 	int status;
1655 
1656 	if (ops == &ice_nvm_region_ops) {
1657 		read_shadow_ram = false;
1658 		nvm_size = hw->flash.flash_size;
1659 	} else if (ops == &ice_sram_region_ops) {
1660 		read_shadow_ram = true;
1661 		nvm_size = hw->flash.sr_words * 2u;
1662 	} else {
1663 		NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1664 		return -EOPNOTSUPP;
1665 	}
1666 
1667 	nvm_data = vzalloc(nvm_size);
1668 	if (!nvm_data)
1669 		return -ENOMEM;
1670 
1671 	num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
1672 	tmp = nvm_data;
1673 	left = nvm_size;
1674 
1675 	/* Some systems take longer to read the NVM than others which causes the
1676 	 * FW to reclaim the NVM lock before the entire NVM has been read. Fix
1677 	 * this by breaking the reads of the NVM into smaller chunks that will
1678 	 * probably not take as long. This has some overhead since we are
1679 	 * increasing the number of AQ commands, but it should always work
1680 	 */
1681 	for (i = 0; i < num_blks; i++) {
1682 		u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
1683 
1684 		status = ice_acquire_nvm(hw, ICE_RES_READ);
1685 		if (status) {
1686 			dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1687 				status, hw->adminq.sq_last_status);
1688 			NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1689 			vfree(nvm_data);
1690 			return -EIO;
1691 		}
1692 
1693 		status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
1694 					   &read_sz, tmp, read_shadow_ram);
1695 		if (status) {
1696 			dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1697 				read_sz, status, hw->adminq.sq_last_status);
1698 			NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1699 			ice_release_nvm(hw);
1700 			vfree(nvm_data);
1701 			return -EIO;
1702 		}
1703 		ice_release_nvm(hw);
1704 
1705 		tmp += read_sz;
1706 		left -= read_sz;
1707 	}
1708 
1709 	*data = nvm_data;
1710 
1711 	return 0;
1712 }
1713 
1714 /**
1715  * ice_devlink_nvm_read - Read a portion of NVM flash contents
1716  * @devlink: the devlink instance
1717  * @ops: the devlink region to snapshot
1718  * @extack: extended ACK response structure
1719  * @offset: the offset to start at
1720  * @size: the amount to read
1721  * @data: the data buffer to read into
1722  *
1723  * This function is called in response to DEVLINK_CMD_REGION_READ to directly
1724  * read a section of the NVM contents.
1725  *
1726  * It reads from either the nvm-flash or shadow-ram region contents.
1727  *
1728  * @returns zero on success, and updates the data pointer. Returns a non-zero
1729  * error code on failure.
1730  */
1731 static int ice_devlink_nvm_read(struct devlink *devlink,
1732 				const struct devlink_region_ops *ops,
1733 				struct netlink_ext_ack *extack,
1734 				u64 offset, u32 size, u8 *data)
1735 {
1736 	struct ice_pf *pf = devlink_priv(devlink);
1737 	struct device *dev = ice_pf_to_dev(pf);
1738 	struct ice_hw *hw = &pf->hw;
1739 	bool read_shadow_ram;
1740 	u64 nvm_size;
1741 	int status;
1742 
1743 	if (ops == &ice_nvm_region_ops) {
1744 		read_shadow_ram = false;
1745 		nvm_size = hw->flash.flash_size;
1746 	} else if (ops == &ice_sram_region_ops) {
1747 		read_shadow_ram = true;
1748 		nvm_size = hw->flash.sr_words * 2u;
1749 	} else {
1750 		NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1751 		return -EOPNOTSUPP;
1752 	}
1753 
1754 	if (offset + size >= nvm_size) {
1755 		NL_SET_ERR_MSG_MOD(extack, "Cannot read beyond the region size");
1756 		return -ERANGE;
1757 	}
1758 
1759 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1760 	if (status) {
1761 		dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1762 			status, hw->adminq.sq_last_status);
1763 		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1764 		return -EIO;
1765 	}
1766 
1767 	status = ice_read_flat_nvm(hw, (u32)offset, &size, data,
1768 				   read_shadow_ram);
1769 	if (status) {
1770 		dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1771 			size, status, hw->adminq.sq_last_status);
1772 		NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1773 		ice_release_nvm(hw);
1774 		return -EIO;
1775 	}
1776 	ice_release_nvm(hw);
1777 
1778 	return 0;
1779 }
1780 
1781 /**
1782  * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
1783  * @devlink: the devlink instance
1784  * @ops: the devlink region being snapshotted
1785  * @extack: extended ACK response structure
1786  * @data: on exit points to snapshot data buffer
1787  *
1788  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1789  * the device-caps devlink region. It captures a snapshot of the device
1790  * capabilities reported by firmware.
1791  *
1792  * @returns zero on success, and updates the data pointer. Returns a non-zero
1793  * error code on failure.
1794  */
1795 static int
1796 ice_devlink_devcaps_snapshot(struct devlink *devlink,
1797 			     const struct devlink_region_ops *ops,
1798 			     struct netlink_ext_ack *extack, u8 **data)
1799 {
1800 	struct ice_pf *pf = devlink_priv(devlink);
1801 	struct device *dev = ice_pf_to_dev(pf);
1802 	struct ice_hw *hw = &pf->hw;
1803 	void *devcaps;
1804 	int status;
1805 
1806 	devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
1807 	if (!devcaps)
1808 		return -ENOMEM;
1809 
1810 	status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
1811 				  ice_aqc_opc_list_dev_caps, NULL);
1812 	if (status) {
1813 		dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
1814 			status, hw->adminq.sq_last_status);
1815 		NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
1816 		vfree(devcaps);
1817 		return status;
1818 	}
1819 
1820 	*data = (u8 *)devcaps;
1821 
1822 	return 0;
1823 }
1824 
1825 static const struct devlink_region_ops ice_nvm_region_ops = {
1826 	.name = "nvm-flash",
1827 	.destructor = vfree,
1828 	.snapshot = ice_devlink_nvm_snapshot,
1829 	.read = ice_devlink_nvm_read,
1830 };
1831 
1832 static const struct devlink_region_ops ice_sram_region_ops = {
1833 	.name = "shadow-ram",
1834 	.destructor = vfree,
1835 	.snapshot = ice_devlink_nvm_snapshot,
1836 	.read = ice_devlink_nvm_read,
1837 };
1838 
1839 static const struct devlink_region_ops ice_devcaps_region_ops = {
1840 	.name = "device-caps",
1841 	.destructor = vfree,
1842 	.snapshot = ice_devlink_devcaps_snapshot,
1843 };
1844 
1845 /**
1846  * ice_devlink_init_regions - Initialize devlink regions
1847  * @pf: the PF device structure
1848  *
1849  * Create devlink regions used to enable access to dump the contents of the
1850  * flash memory on the device.
1851  */
1852 void ice_devlink_init_regions(struct ice_pf *pf)
1853 {
1854 	struct devlink *devlink = priv_to_devlink(pf);
1855 	struct device *dev = ice_pf_to_dev(pf);
1856 	u64 nvm_size, sram_size;
1857 
1858 	nvm_size = pf->hw.flash.flash_size;
1859 	pf->nvm_region = devl_region_create(devlink, &ice_nvm_region_ops, 1,
1860 					    nvm_size);
1861 	if (IS_ERR(pf->nvm_region)) {
1862 		dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1863 			PTR_ERR(pf->nvm_region));
1864 		pf->nvm_region = NULL;
1865 	}
1866 
1867 	sram_size = pf->hw.flash.sr_words * 2u;
1868 	pf->sram_region = devl_region_create(devlink, &ice_sram_region_ops,
1869 					     1, sram_size);
1870 	if (IS_ERR(pf->sram_region)) {
1871 		dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1872 			PTR_ERR(pf->sram_region));
1873 		pf->sram_region = NULL;
1874 	}
1875 
1876 	pf->devcaps_region = devl_region_create(devlink,
1877 						&ice_devcaps_region_ops, 10,
1878 						ICE_AQ_MAX_BUF_LEN);
1879 	if (IS_ERR(pf->devcaps_region)) {
1880 		dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
1881 			PTR_ERR(pf->devcaps_region));
1882 		pf->devcaps_region = NULL;
1883 	}
1884 }
1885 
1886 /**
1887  * ice_devlink_destroy_regions - Destroy devlink regions
1888  * @pf: the PF device structure
1889  *
1890  * Remove previously created regions for this PF.
1891  */
1892 void ice_devlink_destroy_regions(struct ice_pf *pf)
1893 {
1894 	if (pf->nvm_region)
1895 		devl_region_destroy(pf->nvm_region);
1896 
1897 	if (pf->sram_region)
1898 		devl_region_destroy(pf->sram_region);
1899 
1900 	if (pf->devcaps_region)
1901 		devl_region_destroy(pf->devcaps_region);
1902 }
1903