xref: /linux/drivers/net/ethernet/intel/ice/devlink/devlink.c (revision a6a6a98094116b60e5523a571d9443c53325f5b1)
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 enum ice_param_id {
1385 	ICE_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
1386 	ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS,
1387 };
1388 
1389 static const struct devlink_param ice_dvl_rdma_params[] = {
1390 	DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1391 			      ice_devlink_enable_roce_get,
1392 			      ice_devlink_enable_roce_set,
1393 			      ice_devlink_enable_roce_validate),
1394 	DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1395 			      ice_devlink_enable_iw_get,
1396 			      ice_devlink_enable_iw_set,
1397 			      ice_devlink_enable_iw_validate),
1398 };
1399 
1400 static const struct devlink_param ice_dvl_sched_params[] = {
1401 	DEVLINK_PARAM_DRIVER(ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS,
1402 			     "tx_scheduling_layers",
1403 			     DEVLINK_PARAM_TYPE_U8,
1404 			     BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1405 			     ice_devlink_tx_sched_layers_get,
1406 			     ice_devlink_tx_sched_layers_set,
1407 			     ice_devlink_tx_sched_layers_validate),
1408 };
1409 
1410 static void ice_devlink_free(void *devlink_ptr)
1411 {
1412 	devlink_free((struct devlink *)devlink_ptr);
1413 }
1414 
1415 /**
1416  * ice_allocate_pf - Allocate devlink and return PF structure pointer
1417  * @dev: the device to allocate for
1418  *
1419  * Allocate a devlink instance for this device and return the private area as
1420  * the PF structure. The devlink memory is kept track of through devres by
1421  * adding an action to remove it when unwinding.
1422  */
1423 struct ice_pf *ice_allocate_pf(struct device *dev)
1424 {
1425 	struct devlink *devlink;
1426 
1427 	devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
1428 	if (!devlink)
1429 		return NULL;
1430 
1431 	/* Add an action to teardown the devlink when unwinding the driver */
1432 	if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
1433 		return NULL;
1434 
1435 	return devlink_priv(devlink);
1436 }
1437 
1438 /**
1439  * ice_devlink_register - Register devlink interface for this PF
1440  * @pf: the PF to register the devlink for.
1441  *
1442  * Register the devlink instance associated with this physical function.
1443  *
1444  * Return: zero on success or an error code on failure.
1445  */
1446 void ice_devlink_register(struct ice_pf *pf)
1447 {
1448 	struct devlink *devlink = priv_to_devlink(pf);
1449 
1450 	devl_register(devlink);
1451 }
1452 
1453 /**
1454  * ice_devlink_unregister - Unregister devlink resources for this PF.
1455  * @pf: the PF structure to cleanup
1456  *
1457  * Releases resources used by devlink and cleans up associated memory.
1458  */
1459 void ice_devlink_unregister(struct ice_pf *pf)
1460 {
1461 	devl_unregister(priv_to_devlink(pf));
1462 }
1463 
1464 int ice_devlink_register_params(struct ice_pf *pf)
1465 {
1466 	struct devlink *devlink = priv_to_devlink(pf);
1467 	struct ice_hw *hw = &pf->hw;
1468 	int status;
1469 
1470 	status = devl_params_register(devlink, ice_dvl_rdma_params,
1471 				      ARRAY_SIZE(ice_dvl_rdma_params));
1472 	if (status)
1473 		return status;
1474 
1475 	if (hw->func_caps.common_cap.tx_sched_topo_comp_mode_en)
1476 		status = devl_params_register(devlink, ice_dvl_sched_params,
1477 					      ARRAY_SIZE(ice_dvl_sched_params));
1478 
1479 	return status;
1480 }
1481 
1482 void ice_devlink_unregister_params(struct ice_pf *pf)
1483 {
1484 	struct devlink *devlink = priv_to_devlink(pf);
1485 	struct ice_hw *hw = &pf->hw;
1486 
1487 	devl_params_unregister(devlink, ice_dvl_rdma_params,
1488 			       ARRAY_SIZE(ice_dvl_rdma_params));
1489 
1490 	if (hw->func_caps.common_cap.tx_sched_topo_comp_mode_en)
1491 		devl_params_unregister(devlink, ice_dvl_sched_params,
1492 				       ARRAY_SIZE(ice_dvl_sched_params));
1493 }
1494 
1495 #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
1496 
1497 static const struct devlink_region_ops ice_nvm_region_ops;
1498 static const struct devlink_region_ops ice_sram_region_ops;
1499 
1500 /**
1501  * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
1502  * @devlink: the devlink instance
1503  * @ops: the devlink region to snapshot
1504  * @extack: extended ACK response structure
1505  * @data: on exit points to snapshot data buffer
1506  *
1507  * This function is called in response to a DEVLINK_CMD_REGION_NEW for either
1508  * the nvm-flash or shadow-ram region.
1509  *
1510  * It captures a snapshot of the NVM or Shadow RAM flash contents. This
1511  * snapshot can then later be viewed via the DEVLINK_CMD_REGION_READ netlink
1512  * interface.
1513  *
1514  * @returns zero on success, and updates the data pointer. Returns a non-zero
1515  * error code on failure.
1516  */
1517 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
1518 				    const struct devlink_region_ops *ops,
1519 				    struct netlink_ext_ack *extack, u8 **data)
1520 {
1521 	struct ice_pf *pf = devlink_priv(devlink);
1522 	struct device *dev = ice_pf_to_dev(pf);
1523 	struct ice_hw *hw = &pf->hw;
1524 	bool read_shadow_ram;
1525 	u8 *nvm_data, *tmp, i;
1526 	u32 nvm_size, left;
1527 	s8 num_blks;
1528 	int status;
1529 
1530 	if (ops == &ice_nvm_region_ops) {
1531 		read_shadow_ram = false;
1532 		nvm_size = hw->flash.flash_size;
1533 	} else if (ops == &ice_sram_region_ops) {
1534 		read_shadow_ram = true;
1535 		nvm_size = hw->flash.sr_words * 2u;
1536 	} else {
1537 		NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1538 		return -EOPNOTSUPP;
1539 	}
1540 
1541 	nvm_data = vzalloc(nvm_size);
1542 	if (!nvm_data)
1543 		return -ENOMEM;
1544 
1545 	num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
1546 	tmp = nvm_data;
1547 	left = nvm_size;
1548 
1549 	/* Some systems take longer to read the NVM than others which causes the
1550 	 * FW to reclaim the NVM lock before the entire NVM has been read. Fix
1551 	 * this by breaking the reads of the NVM into smaller chunks that will
1552 	 * probably not take as long. This has some overhead since we are
1553 	 * increasing the number of AQ commands, but it should always work
1554 	 */
1555 	for (i = 0; i < num_blks; i++) {
1556 		u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
1557 
1558 		status = ice_acquire_nvm(hw, ICE_RES_READ);
1559 		if (status) {
1560 			dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1561 				status, hw->adminq.sq_last_status);
1562 			NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1563 			vfree(nvm_data);
1564 			return -EIO;
1565 		}
1566 
1567 		status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
1568 					   &read_sz, tmp, read_shadow_ram);
1569 		if (status) {
1570 			dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1571 				read_sz, status, hw->adminq.sq_last_status);
1572 			NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1573 			ice_release_nvm(hw);
1574 			vfree(nvm_data);
1575 			return -EIO;
1576 		}
1577 		ice_release_nvm(hw);
1578 
1579 		tmp += read_sz;
1580 		left -= read_sz;
1581 	}
1582 
1583 	*data = nvm_data;
1584 
1585 	return 0;
1586 }
1587 
1588 /**
1589  * ice_devlink_nvm_read - Read a portion of NVM flash contents
1590  * @devlink: the devlink instance
1591  * @ops: the devlink region to snapshot
1592  * @extack: extended ACK response structure
1593  * @offset: the offset to start at
1594  * @size: the amount to read
1595  * @data: the data buffer to read into
1596  *
1597  * This function is called in response to DEVLINK_CMD_REGION_READ to directly
1598  * read a section of the NVM contents.
1599  *
1600  * It reads from either the nvm-flash or shadow-ram region contents.
1601  *
1602  * @returns zero on success, and updates the data pointer. Returns a non-zero
1603  * error code on failure.
1604  */
1605 static int ice_devlink_nvm_read(struct devlink *devlink,
1606 				const struct devlink_region_ops *ops,
1607 				struct netlink_ext_ack *extack,
1608 				u64 offset, u32 size, u8 *data)
1609 {
1610 	struct ice_pf *pf = devlink_priv(devlink);
1611 	struct device *dev = ice_pf_to_dev(pf);
1612 	struct ice_hw *hw = &pf->hw;
1613 	bool read_shadow_ram;
1614 	u64 nvm_size;
1615 	int status;
1616 
1617 	if (ops == &ice_nvm_region_ops) {
1618 		read_shadow_ram = false;
1619 		nvm_size = hw->flash.flash_size;
1620 	} else if (ops == &ice_sram_region_ops) {
1621 		read_shadow_ram = true;
1622 		nvm_size = hw->flash.sr_words * 2u;
1623 	} else {
1624 		NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1625 		return -EOPNOTSUPP;
1626 	}
1627 
1628 	if (offset + size >= nvm_size) {
1629 		NL_SET_ERR_MSG_MOD(extack, "Cannot read beyond the region size");
1630 		return -ERANGE;
1631 	}
1632 
1633 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1634 	if (status) {
1635 		dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1636 			status, hw->adminq.sq_last_status);
1637 		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1638 		return -EIO;
1639 	}
1640 
1641 	status = ice_read_flat_nvm(hw, (u32)offset, &size, data,
1642 				   read_shadow_ram);
1643 	if (status) {
1644 		dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1645 			size, status, hw->adminq.sq_last_status);
1646 		NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1647 		ice_release_nvm(hw);
1648 		return -EIO;
1649 	}
1650 	ice_release_nvm(hw);
1651 
1652 	return 0;
1653 }
1654 
1655 /**
1656  * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
1657  * @devlink: the devlink instance
1658  * @ops: the devlink region being snapshotted
1659  * @extack: extended ACK response structure
1660  * @data: on exit points to snapshot data buffer
1661  *
1662  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1663  * the device-caps devlink region. It captures a snapshot of the device
1664  * capabilities reported by firmware.
1665  *
1666  * @returns zero on success, and updates the data pointer. Returns a non-zero
1667  * error code on failure.
1668  */
1669 static int
1670 ice_devlink_devcaps_snapshot(struct devlink *devlink,
1671 			     const struct devlink_region_ops *ops,
1672 			     struct netlink_ext_ack *extack, u8 **data)
1673 {
1674 	struct ice_pf *pf = devlink_priv(devlink);
1675 	struct device *dev = ice_pf_to_dev(pf);
1676 	struct ice_hw *hw = &pf->hw;
1677 	void *devcaps;
1678 	int status;
1679 
1680 	devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
1681 	if (!devcaps)
1682 		return -ENOMEM;
1683 
1684 	status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
1685 				  ice_aqc_opc_list_dev_caps, NULL);
1686 	if (status) {
1687 		dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
1688 			status, hw->adminq.sq_last_status);
1689 		NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
1690 		vfree(devcaps);
1691 		return status;
1692 	}
1693 
1694 	*data = (u8 *)devcaps;
1695 
1696 	return 0;
1697 }
1698 
1699 static const struct devlink_region_ops ice_nvm_region_ops = {
1700 	.name = "nvm-flash",
1701 	.destructor = vfree,
1702 	.snapshot = ice_devlink_nvm_snapshot,
1703 	.read = ice_devlink_nvm_read,
1704 };
1705 
1706 static const struct devlink_region_ops ice_sram_region_ops = {
1707 	.name = "shadow-ram",
1708 	.destructor = vfree,
1709 	.snapshot = ice_devlink_nvm_snapshot,
1710 	.read = ice_devlink_nvm_read,
1711 };
1712 
1713 static const struct devlink_region_ops ice_devcaps_region_ops = {
1714 	.name = "device-caps",
1715 	.destructor = vfree,
1716 	.snapshot = ice_devlink_devcaps_snapshot,
1717 };
1718 
1719 /**
1720  * ice_devlink_init_regions - Initialize devlink regions
1721  * @pf: the PF device structure
1722  *
1723  * Create devlink regions used to enable access to dump the contents of the
1724  * flash memory on the device.
1725  */
1726 void ice_devlink_init_regions(struct ice_pf *pf)
1727 {
1728 	struct devlink *devlink = priv_to_devlink(pf);
1729 	struct device *dev = ice_pf_to_dev(pf);
1730 	u64 nvm_size, sram_size;
1731 
1732 	nvm_size = pf->hw.flash.flash_size;
1733 	pf->nvm_region = devl_region_create(devlink, &ice_nvm_region_ops, 1,
1734 					    nvm_size);
1735 	if (IS_ERR(pf->nvm_region)) {
1736 		dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1737 			PTR_ERR(pf->nvm_region));
1738 		pf->nvm_region = NULL;
1739 	}
1740 
1741 	sram_size = pf->hw.flash.sr_words * 2u;
1742 	pf->sram_region = devl_region_create(devlink, &ice_sram_region_ops,
1743 					     1, sram_size);
1744 	if (IS_ERR(pf->sram_region)) {
1745 		dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1746 			PTR_ERR(pf->sram_region));
1747 		pf->sram_region = NULL;
1748 	}
1749 
1750 	pf->devcaps_region = devl_region_create(devlink,
1751 						&ice_devcaps_region_ops, 10,
1752 						ICE_AQ_MAX_BUF_LEN);
1753 	if (IS_ERR(pf->devcaps_region)) {
1754 		dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
1755 			PTR_ERR(pf->devcaps_region));
1756 		pf->devcaps_region = NULL;
1757 	}
1758 }
1759 
1760 /**
1761  * ice_devlink_destroy_regions - Destroy devlink regions
1762  * @pf: the PF device structure
1763  *
1764  * Remove previously created regions for this PF.
1765  */
1766 void ice_devlink_destroy_regions(struct ice_pf *pf)
1767 {
1768 	if (pf->nvm_region)
1769 		devl_region_destroy(pf->nvm_region);
1770 
1771 	if (pf->sram_region)
1772 		devl_region_destroy(pf->sram_region);
1773 
1774 	if (pf->devcaps_region)
1775 		devl_region_destroy(pf->devcaps_region);
1776 }
1777