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