xref: /linux/drivers/net/ethernet/intel/ice/devlink/devlink.c (revision 860a9bed265146b10311bcadbbcef59c3af4454d)
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_tear_down_devlink_rate_tree - removes devlink-rate exported tree
528  * @pf: pf struct
529  *
530  * This function tears down tree exported during VF's creation.
531  */
532 void ice_tear_down_devlink_rate_tree(struct ice_pf *pf)
533 {
534 	struct devlink *devlink;
535 	struct ice_vf *vf;
536 	unsigned int bkt;
537 
538 	devlink = priv_to_devlink(pf);
539 
540 	devl_lock(devlink);
541 	mutex_lock(&pf->vfs.table_lock);
542 	ice_for_each_vf(pf, bkt, vf) {
543 		if (vf->devlink_port.devlink_rate)
544 			devl_rate_leaf_destroy(&vf->devlink_port);
545 	}
546 	mutex_unlock(&pf->vfs.table_lock);
547 
548 	devl_rate_nodes_destroy(devlink);
549 	devl_unlock(devlink);
550 }
551 
552 /**
553  * ice_enable_custom_tx - try to enable custom Tx feature
554  * @pf: pf struct
555  *
556  * This function tries to enable custom Tx feature,
557  * it's not possible to enable it, if DCB or ADQ is active.
558  */
559 static bool ice_enable_custom_tx(struct ice_pf *pf)
560 {
561 	struct ice_port_info *pi = ice_get_main_vsi(pf)->port_info;
562 	struct device *dev = ice_pf_to_dev(pf);
563 
564 	if (pi->is_custom_tx_enabled)
565 		/* already enabled, return true */
566 		return true;
567 
568 	if (ice_is_adq_active(pf)) {
569 		dev_err(dev, "ADQ active, can't modify Tx scheduler tree\n");
570 		return false;
571 	}
572 
573 	if (ice_is_dcb_active(pf)) {
574 		dev_err(dev, "DCB active, can't modify Tx scheduler tree\n");
575 		return false;
576 	}
577 
578 	pi->is_custom_tx_enabled = true;
579 
580 	return true;
581 }
582 
583 /**
584  * ice_traverse_tx_tree - traverse Tx scheduler tree
585  * @devlink: devlink struct
586  * @node: current node, used for recursion
587  * @tc_node: tc_node struct, that is treated as a root
588  * @pf: pf struct
589  *
590  * This function traverses Tx scheduler tree and exports
591  * entire structure to the devlink-rate.
592  */
593 static void ice_traverse_tx_tree(struct devlink *devlink, struct ice_sched_node *node,
594 				 struct ice_sched_node *tc_node, struct ice_pf *pf)
595 {
596 	struct devlink_rate *rate_node = NULL;
597 	struct ice_vf *vf;
598 	int i;
599 
600 	if (node->rate_node)
601 		/* already added, skip to the next */
602 		goto traverse_children;
603 
604 	if (node->parent == tc_node) {
605 		/* create root node */
606 		rate_node = devl_rate_node_create(devlink, node, node->name, NULL);
607 	} else if (node->vsi_handle &&
608 		   pf->vsi[node->vsi_handle]->vf) {
609 		vf = pf->vsi[node->vsi_handle]->vf;
610 		if (!vf->devlink_port.devlink_rate)
611 			/* leaf nodes doesn't have children
612 			 * so we don't set rate_node
613 			 */
614 			devl_rate_leaf_create(&vf->devlink_port, node,
615 					      node->parent->rate_node);
616 	} else if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF &&
617 		   node->parent->rate_node) {
618 		rate_node = devl_rate_node_create(devlink, node, node->name,
619 						  node->parent->rate_node);
620 	}
621 
622 	if (rate_node && !IS_ERR(rate_node))
623 		node->rate_node = rate_node;
624 
625 traverse_children:
626 	for (i = 0; i < node->num_children; i++)
627 		ice_traverse_tx_tree(devlink, node->children[i], tc_node, pf);
628 }
629 
630 /**
631  * ice_devlink_rate_init_tx_topology - export Tx scheduler tree to devlink rate
632  * @devlink: devlink struct
633  * @vsi: main vsi struct
634  *
635  * This function finds a root node, then calls ice_traverse_tx tree, which
636  * traverses the tree and exports it's contents to devlink rate.
637  */
638 int ice_devlink_rate_init_tx_topology(struct devlink *devlink, struct ice_vsi *vsi)
639 {
640 	struct ice_port_info *pi = vsi->port_info;
641 	struct ice_sched_node *tc_node;
642 	struct ice_pf *pf = vsi->back;
643 	int i;
644 
645 	tc_node = pi->root->children[0];
646 	mutex_lock(&pi->sched_lock);
647 	devl_lock(devlink);
648 	for (i = 0; i < tc_node->num_children; i++)
649 		ice_traverse_tx_tree(devlink, tc_node->children[i], tc_node, pf);
650 	devl_unlock(devlink);
651 	mutex_unlock(&pi->sched_lock);
652 
653 	return 0;
654 }
655 
656 static void ice_clear_rate_nodes(struct ice_sched_node *node)
657 {
658 	node->rate_node = NULL;
659 
660 	for (int i = 0; i < node->num_children; i++)
661 		ice_clear_rate_nodes(node->children[i]);
662 }
663 
664 /**
665  * ice_devlink_rate_clear_tx_topology - clear node->rate_node
666  * @vsi: main vsi struct
667  *
668  * Clear rate_node to cleanup creation of Tx topology.
669  *
670  */
671 void ice_devlink_rate_clear_tx_topology(struct ice_vsi *vsi)
672 {
673 	struct ice_port_info *pi = vsi->port_info;
674 
675 	mutex_lock(&pi->sched_lock);
676 	ice_clear_rate_nodes(pi->root->children[0]);
677 	mutex_unlock(&pi->sched_lock);
678 }
679 
680 /**
681  * ice_set_object_tx_share - sets node scheduling parameter
682  * @pi: devlink struct instance
683  * @node: node struct instance
684  * @bw: bandwidth in bytes per second
685  * @extack: extended netdev ack structure
686  *
687  * This function sets ICE_MIN_BW scheduling BW limit.
688  */
689 static int ice_set_object_tx_share(struct ice_port_info *pi, struct ice_sched_node *node,
690 				   u64 bw, struct netlink_ext_ack *extack)
691 {
692 	int status;
693 
694 	mutex_lock(&pi->sched_lock);
695 	/* converts bytes per second to kilo bits per second */
696 	node->tx_share = div_u64(bw, 125);
697 	status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW, node->tx_share);
698 	mutex_unlock(&pi->sched_lock);
699 
700 	if (status)
701 		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_share");
702 
703 	return status;
704 }
705 
706 /**
707  * ice_set_object_tx_max - sets node scheduling parameter
708  * @pi: devlink struct instance
709  * @node: node struct instance
710  * @bw: bandwidth in bytes per second
711  * @extack: extended netdev ack structure
712  *
713  * This function sets ICE_MAX_BW scheduling BW limit.
714  */
715 static int ice_set_object_tx_max(struct ice_port_info *pi, struct ice_sched_node *node,
716 				 u64 bw, struct netlink_ext_ack *extack)
717 {
718 	int status;
719 
720 	mutex_lock(&pi->sched_lock);
721 	/* converts bytes per second value to kilo bits per second */
722 	node->tx_max = div_u64(bw, 125);
723 	status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW, node->tx_max);
724 	mutex_unlock(&pi->sched_lock);
725 
726 	if (status)
727 		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_max");
728 
729 	return status;
730 }
731 
732 /**
733  * ice_set_object_tx_priority - sets node scheduling parameter
734  * @pi: devlink struct instance
735  * @node: node struct instance
736  * @priority: value representing priority for strict priority arbitration
737  * @extack: extended netdev ack structure
738  *
739  * This function sets priority of node among siblings.
740  */
741 static int ice_set_object_tx_priority(struct ice_port_info *pi, struct ice_sched_node *node,
742 				      u32 priority, struct netlink_ext_ack *extack)
743 {
744 	int status;
745 
746 	if (priority >= 8) {
747 		NL_SET_ERR_MSG_MOD(extack, "Priority should be less than 8");
748 		return -EINVAL;
749 	}
750 
751 	mutex_lock(&pi->sched_lock);
752 	node->tx_priority = priority;
753 	status = ice_sched_set_node_priority(pi, node, node->tx_priority);
754 	mutex_unlock(&pi->sched_lock);
755 
756 	if (status)
757 		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_priority");
758 
759 	return status;
760 }
761 
762 /**
763  * ice_set_object_tx_weight - sets node scheduling parameter
764  * @pi: devlink struct instance
765  * @node: node struct instance
766  * @weight: value represeting relative weight for WFQ arbitration
767  * @extack: extended netdev ack structure
768  *
769  * This function sets node weight for WFQ algorithm.
770  */
771 static int ice_set_object_tx_weight(struct ice_port_info *pi, struct ice_sched_node *node,
772 				    u32 weight, struct netlink_ext_ack *extack)
773 {
774 	int status;
775 
776 	if (weight > 200 || weight < 1) {
777 		NL_SET_ERR_MSG_MOD(extack, "Weight must be between 1 and 200");
778 		return -EINVAL;
779 	}
780 
781 	mutex_lock(&pi->sched_lock);
782 	node->tx_weight = weight;
783 	status = ice_sched_set_node_weight(pi, node, node->tx_weight);
784 	mutex_unlock(&pi->sched_lock);
785 
786 	if (status)
787 		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_weight");
788 
789 	return status;
790 }
791 
792 /**
793  * ice_get_pi_from_dev_rate - get port info from devlink_rate
794  * @rate_node: devlink struct instance
795  *
796  * This function returns corresponding port_info struct of devlink_rate
797  */
798 static struct ice_port_info *ice_get_pi_from_dev_rate(struct devlink_rate *rate_node)
799 {
800 	struct ice_pf *pf = devlink_priv(rate_node->devlink);
801 
802 	return ice_get_main_vsi(pf)->port_info;
803 }
804 
805 static int ice_devlink_rate_node_new(struct devlink_rate *rate_node, void **priv,
806 				     struct netlink_ext_ack *extack)
807 {
808 	struct ice_sched_node *node;
809 	struct ice_port_info *pi;
810 
811 	pi = ice_get_pi_from_dev_rate(rate_node);
812 
813 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
814 		return -EBUSY;
815 
816 	/* preallocate memory for ice_sched_node */
817 	node = devm_kzalloc(ice_hw_to_dev(pi->hw), sizeof(*node), GFP_KERNEL);
818 	*priv = node;
819 
820 	return 0;
821 }
822 
823 static int ice_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,
824 				     struct netlink_ext_ack *extack)
825 {
826 	struct ice_sched_node *node, *tc_node;
827 	struct ice_port_info *pi;
828 
829 	pi = ice_get_pi_from_dev_rate(rate_node);
830 	tc_node = pi->root->children[0];
831 	node = priv;
832 
833 	if (!rate_node->parent || !node || tc_node == node || !extack)
834 		return 0;
835 
836 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
837 		return -EBUSY;
838 
839 	/* can't allow to delete a node with children */
840 	if (node->num_children)
841 		return -EINVAL;
842 
843 	mutex_lock(&pi->sched_lock);
844 	ice_free_sched_node(pi, node);
845 	mutex_unlock(&pi->sched_lock);
846 
847 	return 0;
848 }
849 
850 static int ice_devlink_rate_leaf_tx_max_set(struct devlink_rate *rate_leaf, void *priv,
851 					    u64 tx_max, struct netlink_ext_ack *extack)
852 {
853 	struct ice_sched_node *node = priv;
854 
855 	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
856 		return -EBUSY;
857 
858 	if (!node)
859 		return 0;
860 
861 	return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_leaf),
862 				     node, tx_max, extack);
863 }
864 
865 static int ice_devlink_rate_leaf_tx_share_set(struct devlink_rate *rate_leaf, void *priv,
866 					      u64 tx_share, struct netlink_ext_ack *extack)
867 {
868 	struct ice_sched_node *node = priv;
869 
870 	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
871 		return -EBUSY;
872 
873 	if (!node)
874 		return 0;
875 
876 	return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_leaf), node,
877 				       tx_share, extack);
878 }
879 
880 static int ice_devlink_rate_leaf_tx_priority_set(struct devlink_rate *rate_leaf, void *priv,
881 						 u32 tx_priority, struct netlink_ext_ack *extack)
882 {
883 	struct ice_sched_node *node = priv;
884 
885 	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
886 		return -EBUSY;
887 
888 	if (!node)
889 		return 0;
890 
891 	return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_leaf), node,
892 					  tx_priority, extack);
893 }
894 
895 static int ice_devlink_rate_leaf_tx_weight_set(struct devlink_rate *rate_leaf, void *priv,
896 					       u32 tx_weight, struct netlink_ext_ack *extack)
897 {
898 	struct ice_sched_node *node = priv;
899 
900 	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
901 		return -EBUSY;
902 
903 	if (!node)
904 		return 0;
905 
906 	return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_leaf), node,
907 					tx_weight, extack);
908 }
909 
910 static int ice_devlink_rate_node_tx_max_set(struct devlink_rate *rate_node, void *priv,
911 					    u64 tx_max, struct netlink_ext_ack *extack)
912 {
913 	struct ice_sched_node *node = priv;
914 
915 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
916 		return -EBUSY;
917 
918 	if (!node)
919 		return 0;
920 
921 	return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_node),
922 				     node, tx_max, extack);
923 }
924 
925 static int ice_devlink_rate_node_tx_share_set(struct devlink_rate *rate_node, void *priv,
926 					      u64 tx_share, struct netlink_ext_ack *extack)
927 {
928 	struct ice_sched_node *node = priv;
929 
930 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
931 		return -EBUSY;
932 
933 	if (!node)
934 		return 0;
935 
936 	return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_node),
937 				       node, tx_share, extack);
938 }
939 
940 static int ice_devlink_rate_node_tx_priority_set(struct devlink_rate *rate_node, void *priv,
941 						 u32 tx_priority, struct netlink_ext_ack *extack)
942 {
943 	struct ice_sched_node *node = priv;
944 
945 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
946 		return -EBUSY;
947 
948 	if (!node)
949 		return 0;
950 
951 	return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_node),
952 					  node, tx_priority, extack);
953 }
954 
955 static int ice_devlink_rate_node_tx_weight_set(struct devlink_rate *rate_node, void *priv,
956 					       u32 tx_weight, struct netlink_ext_ack *extack)
957 {
958 	struct ice_sched_node *node = priv;
959 
960 	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
961 		return -EBUSY;
962 
963 	if (!node)
964 		return 0;
965 
966 	return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_node),
967 					node, tx_weight, extack);
968 }
969 
970 static int ice_devlink_set_parent(struct devlink_rate *devlink_rate,
971 				  struct devlink_rate *parent,
972 				  void *priv, void *parent_priv,
973 				  struct netlink_ext_ack *extack)
974 {
975 	struct ice_port_info *pi = ice_get_pi_from_dev_rate(devlink_rate);
976 	struct ice_sched_node *tc_node, *node, *parent_node;
977 	u16 num_nodes_added;
978 	u32 first_node_teid;
979 	u32 node_teid;
980 	int status;
981 
982 	tc_node = pi->root->children[0];
983 	node = priv;
984 
985 	if (!extack)
986 		return 0;
987 
988 	if (!ice_enable_custom_tx(devlink_priv(devlink_rate->devlink)))
989 		return -EBUSY;
990 
991 	if (!parent) {
992 		if (!node || tc_node == node || node->num_children)
993 			return -EINVAL;
994 
995 		mutex_lock(&pi->sched_lock);
996 		ice_free_sched_node(pi, node);
997 		mutex_unlock(&pi->sched_lock);
998 
999 		return 0;
1000 	}
1001 
1002 	parent_node = parent_priv;
1003 
1004 	/* if the node doesn't exist, create it */
1005 	if (!node->parent) {
1006 		mutex_lock(&pi->sched_lock);
1007 		status = ice_sched_add_elems(pi, tc_node, parent_node,
1008 					     parent_node->tx_sched_layer + 1,
1009 					     1, &num_nodes_added, &first_node_teid,
1010 					     &node);
1011 		mutex_unlock(&pi->sched_lock);
1012 
1013 		if (status) {
1014 			NL_SET_ERR_MSG_MOD(extack, "Can't add a new node");
1015 			return status;
1016 		}
1017 
1018 		if (devlink_rate->tx_share)
1019 			ice_set_object_tx_share(pi, node, devlink_rate->tx_share, extack);
1020 		if (devlink_rate->tx_max)
1021 			ice_set_object_tx_max(pi, node, devlink_rate->tx_max, extack);
1022 		if (devlink_rate->tx_priority)
1023 			ice_set_object_tx_priority(pi, node, devlink_rate->tx_priority, extack);
1024 		if (devlink_rate->tx_weight)
1025 			ice_set_object_tx_weight(pi, node, devlink_rate->tx_weight, extack);
1026 	} else {
1027 		node_teid = le32_to_cpu(node->info.node_teid);
1028 		mutex_lock(&pi->sched_lock);
1029 		status = ice_sched_move_nodes(pi, parent_node, 1, &node_teid);
1030 		mutex_unlock(&pi->sched_lock);
1031 
1032 		if (status)
1033 			NL_SET_ERR_MSG_MOD(extack, "Can't move existing node to a new parent");
1034 	}
1035 
1036 	return status;
1037 }
1038 
1039 /**
1040  * ice_devlink_reinit_up - do reinit of the given PF
1041  * @pf: pointer to the PF struct
1042  */
1043 static int ice_devlink_reinit_up(struct ice_pf *pf)
1044 {
1045 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
1046 	struct ice_vsi_cfg_params params;
1047 	int err;
1048 
1049 	err = ice_init_dev(pf);
1050 	if (err)
1051 		return err;
1052 
1053 	params = ice_vsi_to_params(vsi);
1054 	params.flags = ICE_VSI_FLAG_INIT;
1055 
1056 	rtnl_lock();
1057 	err = ice_vsi_cfg(vsi, &params);
1058 	rtnl_unlock();
1059 	if (err)
1060 		goto err_vsi_cfg;
1061 
1062 	/* No need to take devl_lock, it's already taken by devlink API */
1063 	err = ice_load(pf);
1064 	if (err)
1065 		goto err_load;
1066 
1067 	return 0;
1068 
1069 err_load:
1070 	rtnl_lock();
1071 	ice_vsi_decfg(vsi);
1072 	rtnl_unlock();
1073 err_vsi_cfg:
1074 	ice_deinit_dev(pf);
1075 	return err;
1076 }
1077 
1078 /**
1079  * ice_devlink_reload_up - do reload up after reinit
1080  * @devlink: pointer to the devlink instance reloading
1081  * @action: the action requested
1082  * @limit: limits imposed by userspace, such as not resetting
1083  * @actions_performed: on return, indicate what actions actually performed
1084  * @extack: netlink extended ACK structure
1085  */
1086 static int
1087 ice_devlink_reload_up(struct devlink *devlink,
1088 		      enum devlink_reload_action action,
1089 		      enum devlink_reload_limit limit,
1090 		      u32 *actions_performed,
1091 		      struct netlink_ext_ack *extack)
1092 {
1093 	struct ice_pf *pf = devlink_priv(devlink);
1094 
1095 	switch (action) {
1096 	case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
1097 		*actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
1098 		return ice_devlink_reinit_up(pf);
1099 	case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
1100 		*actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE);
1101 		return ice_devlink_reload_empr_finish(pf, extack);
1102 	default:
1103 		WARN_ON(1);
1104 		return -EOPNOTSUPP;
1105 	}
1106 }
1107 
1108 static const struct devlink_ops ice_devlink_ops = {
1109 	.supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
1110 	.reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
1111 			  BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
1112 	.reload_down = ice_devlink_reload_down,
1113 	.reload_up = ice_devlink_reload_up,
1114 	.eswitch_mode_get = ice_eswitch_mode_get,
1115 	.eswitch_mode_set = ice_eswitch_mode_set,
1116 	.info_get = ice_devlink_info_get,
1117 	.flash_update = ice_devlink_flash_update,
1118 
1119 	.rate_node_new = ice_devlink_rate_node_new,
1120 	.rate_node_del = ice_devlink_rate_node_del,
1121 
1122 	.rate_leaf_tx_max_set = ice_devlink_rate_leaf_tx_max_set,
1123 	.rate_leaf_tx_share_set = ice_devlink_rate_leaf_tx_share_set,
1124 	.rate_leaf_tx_priority_set = ice_devlink_rate_leaf_tx_priority_set,
1125 	.rate_leaf_tx_weight_set = ice_devlink_rate_leaf_tx_weight_set,
1126 
1127 	.rate_node_tx_max_set = ice_devlink_rate_node_tx_max_set,
1128 	.rate_node_tx_share_set = ice_devlink_rate_node_tx_share_set,
1129 	.rate_node_tx_priority_set = ice_devlink_rate_node_tx_priority_set,
1130 	.rate_node_tx_weight_set = ice_devlink_rate_node_tx_weight_set,
1131 
1132 	.rate_leaf_parent_set = ice_devlink_set_parent,
1133 	.rate_node_parent_set = ice_devlink_set_parent,
1134 };
1135 
1136 static int
1137 ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
1138 			    struct devlink_param_gset_ctx *ctx)
1139 {
1140 	struct ice_pf *pf = devlink_priv(devlink);
1141 
1142 	ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false;
1143 
1144 	return 0;
1145 }
1146 
1147 static int
1148 ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
1149 			    struct devlink_param_gset_ctx *ctx)
1150 {
1151 	struct ice_pf *pf = devlink_priv(devlink);
1152 	bool roce_ena = ctx->val.vbool;
1153 	int ret;
1154 
1155 	if (!roce_ena) {
1156 		ice_unplug_aux_dev(pf);
1157 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1158 		return 0;
1159 	}
1160 
1161 	pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
1162 	ret = ice_plug_aux_dev(pf);
1163 	if (ret)
1164 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1165 
1166 	return ret;
1167 }
1168 
1169 static int
1170 ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
1171 				 union devlink_param_value val,
1172 				 struct netlink_ext_ack *extack)
1173 {
1174 	struct ice_pf *pf = devlink_priv(devlink);
1175 
1176 	if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1177 		return -EOPNOTSUPP;
1178 
1179 	if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
1180 		NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1181 		return -EOPNOTSUPP;
1182 	}
1183 
1184 	return 0;
1185 }
1186 
1187 static int
1188 ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
1189 			  struct devlink_param_gset_ctx *ctx)
1190 {
1191 	struct ice_pf *pf = devlink_priv(devlink);
1192 
1193 	ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
1194 
1195 	return 0;
1196 }
1197 
1198 static int
1199 ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
1200 			  struct devlink_param_gset_ctx *ctx)
1201 {
1202 	struct ice_pf *pf = devlink_priv(devlink);
1203 	bool iw_ena = ctx->val.vbool;
1204 	int ret;
1205 
1206 	if (!iw_ena) {
1207 		ice_unplug_aux_dev(pf);
1208 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1209 		return 0;
1210 	}
1211 
1212 	pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
1213 	ret = ice_plug_aux_dev(pf);
1214 	if (ret)
1215 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1216 
1217 	return ret;
1218 }
1219 
1220 static int
1221 ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
1222 			       union devlink_param_value val,
1223 			       struct netlink_ext_ack *extack)
1224 {
1225 	struct ice_pf *pf = devlink_priv(devlink);
1226 
1227 	if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1228 		return -EOPNOTSUPP;
1229 
1230 	if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
1231 		NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1232 		return -EOPNOTSUPP;
1233 	}
1234 
1235 	return 0;
1236 }
1237 
1238 static const struct devlink_param ice_devlink_params[] = {
1239 	DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1240 			      ice_devlink_enable_roce_get,
1241 			      ice_devlink_enable_roce_set,
1242 			      ice_devlink_enable_roce_validate),
1243 	DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1244 			      ice_devlink_enable_iw_get,
1245 			      ice_devlink_enable_iw_set,
1246 			      ice_devlink_enable_iw_validate),
1247 
1248 };
1249 
1250 static void ice_devlink_free(void *devlink_ptr)
1251 {
1252 	devlink_free((struct devlink *)devlink_ptr);
1253 }
1254 
1255 /**
1256  * ice_allocate_pf - Allocate devlink and return PF structure pointer
1257  * @dev: the device to allocate for
1258  *
1259  * Allocate a devlink instance for this device and return the private area as
1260  * the PF structure. The devlink memory is kept track of through devres by
1261  * adding an action to remove it when unwinding.
1262  */
1263 struct ice_pf *ice_allocate_pf(struct device *dev)
1264 {
1265 	struct devlink *devlink;
1266 
1267 	devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
1268 	if (!devlink)
1269 		return NULL;
1270 
1271 	/* Add an action to teardown the devlink when unwinding the driver */
1272 	if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
1273 		return NULL;
1274 
1275 	return devlink_priv(devlink);
1276 }
1277 
1278 /**
1279  * ice_devlink_register - Register devlink interface for this PF
1280  * @pf: the PF to register the devlink for.
1281  *
1282  * Register the devlink instance associated with this physical function.
1283  *
1284  * Return: zero on success or an error code on failure.
1285  */
1286 void ice_devlink_register(struct ice_pf *pf)
1287 {
1288 	struct devlink *devlink = priv_to_devlink(pf);
1289 
1290 	devl_register(devlink);
1291 }
1292 
1293 /**
1294  * ice_devlink_unregister - Unregister devlink resources for this PF.
1295  * @pf: the PF structure to cleanup
1296  *
1297  * Releases resources used by devlink and cleans up associated memory.
1298  */
1299 void ice_devlink_unregister(struct ice_pf *pf)
1300 {
1301 	devl_unregister(priv_to_devlink(pf));
1302 }
1303 
1304 int ice_devlink_register_params(struct ice_pf *pf)
1305 {
1306 	struct devlink *devlink = priv_to_devlink(pf);
1307 
1308 	return devl_params_register(devlink, ice_devlink_params,
1309 				    ARRAY_SIZE(ice_devlink_params));
1310 }
1311 
1312 void ice_devlink_unregister_params(struct ice_pf *pf)
1313 {
1314 	devl_params_unregister(priv_to_devlink(pf), ice_devlink_params,
1315 			       ARRAY_SIZE(ice_devlink_params));
1316 }
1317 
1318 #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
1319 
1320 static const struct devlink_region_ops ice_nvm_region_ops;
1321 static const struct devlink_region_ops ice_sram_region_ops;
1322 
1323 /**
1324  * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
1325  * @devlink: the devlink instance
1326  * @ops: the devlink region to snapshot
1327  * @extack: extended ACK response structure
1328  * @data: on exit points to snapshot data buffer
1329  *
1330  * This function is called in response to a DEVLINK_CMD_REGION_NEW for either
1331  * the nvm-flash or shadow-ram region.
1332  *
1333  * It captures a snapshot of the NVM or Shadow RAM flash contents. This
1334  * snapshot can then later be viewed via the DEVLINK_CMD_REGION_READ netlink
1335  * interface.
1336  *
1337  * @returns zero on success, and updates the data pointer. Returns a non-zero
1338  * error code on failure.
1339  */
1340 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
1341 				    const struct devlink_region_ops *ops,
1342 				    struct netlink_ext_ack *extack, u8 **data)
1343 {
1344 	struct ice_pf *pf = devlink_priv(devlink);
1345 	struct device *dev = ice_pf_to_dev(pf);
1346 	struct ice_hw *hw = &pf->hw;
1347 	bool read_shadow_ram;
1348 	u8 *nvm_data, *tmp, i;
1349 	u32 nvm_size, left;
1350 	s8 num_blks;
1351 	int status;
1352 
1353 	if (ops == &ice_nvm_region_ops) {
1354 		read_shadow_ram = false;
1355 		nvm_size = hw->flash.flash_size;
1356 	} else if (ops == &ice_sram_region_ops) {
1357 		read_shadow_ram = true;
1358 		nvm_size = hw->flash.sr_words * 2u;
1359 	} else {
1360 		NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1361 		return -EOPNOTSUPP;
1362 	}
1363 
1364 	nvm_data = vzalloc(nvm_size);
1365 	if (!nvm_data)
1366 		return -ENOMEM;
1367 
1368 	num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
1369 	tmp = nvm_data;
1370 	left = nvm_size;
1371 
1372 	/* Some systems take longer to read the NVM than others which causes the
1373 	 * FW to reclaim the NVM lock before the entire NVM has been read. Fix
1374 	 * this by breaking the reads of the NVM into smaller chunks that will
1375 	 * probably not take as long. This has some overhead since we are
1376 	 * increasing the number of AQ commands, but it should always work
1377 	 */
1378 	for (i = 0; i < num_blks; i++) {
1379 		u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
1380 
1381 		status = ice_acquire_nvm(hw, ICE_RES_READ);
1382 		if (status) {
1383 			dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1384 				status, hw->adminq.sq_last_status);
1385 			NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1386 			vfree(nvm_data);
1387 			return -EIO;
1388 		}
1389 
1390 		status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
1391 					   &read_sz, tmp, read_shadow_ram);
1392 		if (status) {
1393 			dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1394 				read_sz, status, hw->adminq.sq_last_status);
1395 			NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1396 			ice_release_nvm(hw);
1397 			vfree(nvm_data);
1398 			return -EIO;
1399 		}
1400 		ice_release_nvm(hw);
1401 
1402 		tmp += read_sz;
1403 		left -= read_sz;
1404 	}
1405 
1406 	*data = nvm_data;
1407 
1408 	return 0;
1409 }
1410 
1411 /**
1412  * ice_devlink_nvm_read - Read a portion of NVM flash contents
1413  * @devlink: the devlink instance
1414  * @ops: the devlink region to snapshot
1415  * @extack: extended ACK response structure
1416  * @offset: the offset to start at
1417  * @size: the amount to read
1418  * @data: the data buffer to read into
1419  *
1420  * This function is called in response to DEVLINK_CMD_REGION_READ to directly
1421  * read a section of the NVM contents.
1422  *
1423  * It reads from either the nvm-flash or shadow-ram region contents.
1424  *
1425  * @returns zero on success, and updates the data pointer. Returns a non-zero
1426  * error code on failure.
1427  */
1428 static int ice_devlink_nvm_read(struct devlink *devlink,
1429 				const struct devlink_region_ops *ops,
1430 				struct netlink_ext_ack *extack,
1431 				u64 offset, u32 size, u8 *data)
1432 {
1433 	struct ice_pf *pf = devlink_priv(devlink);
1434 	struct device *dev = ice_pf_to_dev(pf);
1435 	struct ice_hw *hw = &pf->hw;
1436 	bool read_shadow_ram;
1437 	u64 nvm_size;
1438 	int status;
1439 
1440 	if (ops == &ice_nvm_region_ops) {
1441 		read_shadow_ram = false;
1442 		nvm_size = hw->flash.flash_size;
1443 	} else if (ops == &ice_sram_region_ops) {
1444 		read_shadow_ram = true;
1445 		nvm_size = hw->flash.sr_words * 2u;
1446 	} else {
1447 		NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1448 		return -EOPNOTSUPP;
1449 	}
1450 
1451 	if (offset + size >= nvm_size) {
1452 		NL_SET_ERR_MSG_MOD(extack, "Cannot read beyond the region size");
1453 		return -ERANGE;
1454 	}
1455 
1456 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1457 	if (status) {
1458 		dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1459 			status, hw->adminq.sq_last_status);
1460 		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1461 		return -EIO;
1462 	}
1463 
1464 	status = ice_read_flat_nvm(hw, (u32)offset, &size, data,
1465 				   read_shadow_ram);
1466 	if (status) {
1467 		dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1468 			size, status, hw->adminq.sq_last_status);
1469 		NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1470 		ice_release_nvm(hw);
1471 		return -EIO;
1472 	}
1473 	ice_release_nvm(hw);
1474 
1475 	return 0;
1476 }
1477 
1478 /**
1479  * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
1480  * @devlink: the devlink instance
1481  * @ops: the devlink region being snapshotted
1482  * @extack: extended ACK response structure
1483  * @data: on exit points to snapshot data buffer
1484  *
1485  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1486  * the device-caps devlink region. It captures a snapshot of the device
1487  * capabilities reported by firmware.
1488  *
1489  * @returns zero on success, and updates the data pointer. Returns a non-zero
1490  * error code on failure.
1491  */
1492 static int
1493 ice_devlink_devcaps_snapshot(struct devlink *devlink,
1494 			     const struct devlink_region_ops *ops,
1495 			     struct netlink_ext_ack *extack, u8 **data)
1496 {
1497 	struct ice_pf *pf = devlink_priv(devlink);
1498 	struct device *dev = ice_pf_to_dev(pf);
1499 	struct ice_hw *hw = &pf->hw;
1500 	void *devcaps;
1501 	int status;
1502 
1503 	devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
1504 	if (!devcaps)
1505 		return -ENOMEM;
1506 
1507 	status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
1508 				  ice_aqc_opc_list_dev_caps, NULL);
1509 	if (status) {
1510 		dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
1511 			status, hw->adminq.sq_last_status);
1512 		NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
1513 		vfree(devcaps);
1514 		return status;
1515 	}
1516 
1517 	*data = (u8 *)devcaps;
1518 
1519 	return 0;
1520 }
1521 
1522 static const struct devlink_region_ops ice_nvm_region_ops = {
1523 	.name = "nvm-flash",
1524 	.destructor = vfree,
1525 	.snapshot = ice_devlink_nvm_snapshot,
1526 	.read = ice_devlink_nvm_read,
1527 };
1528 
1529 static const struct devlink_region_ops ice_sram_region_ops = {
1530 	.name = "shadow-ram",
1531 	.destructor = vfree,
1532 	.snapshot = ice_devlink_nvm_snapshot,
1533 	.read = ice_devlink_nvm_read,
1534 };
1535 
1536 static const struct devlink_region_ops ice_devcaps_region_ops = {
1537 	.name = "device-caps",
1538 	.destructor = vfree,
1539 	.snapshot = ice_devlink_devcaps_snapshot,
1540 };
1541 
1542 /**
1543  * ice_devlink_init_regions - Initialize devlink regions
1544  * @pf: the PF device structure
1545  *
1546  * Create devlink regions used to enable access to dump the contents of the
1547  * flash memory on the device.
1548  */
1549 void ice_devlink_init_regions(struct ice_pf *pf)
1550 {
1551 	struct devlink *devlink = priv_to_devlink(pf);
1552 	struct device *dev = ice_pf_to_dev(pf);
1553 	u64 nvm_size, sram_size;
1554 
1555 	nvm_size = pf->hw.flash.flash_size;
1556 	pf->nvm_region = devl_region_create(devlink, &ice_nvm_region_ops, 1,
1557 					    nvm_size);
1558 	if (IS_ERR(pf->nvm_region)) {
1559 		dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1560 			PTR_ERR(pf->nvm_region));
1561 		pf->nvm_region = NULL;
1562 	}
1563 
1564 	sram_size = pf->hw.flash.sr_words * 2u;
1565 	pf->sram_region = devl_region_create(devlink, &ice_sram_region_ops,
1566 					     1, sram_size);
1567 	if (IS_ERR(pf->sram_region)) {
1568 		dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1569 			PTR_ERR(pf->sram_region));
1570 		pf->sram_region = NULL;
1571 	}
1572 
1573 	pf->devcaps_region = devl_region_create(devlink,
1574 						&ice_devcaps_region_ops, 10,
1575 						ICE_AQ_MAX_BUF_LEN);
1576 	if (IS_ERR(pf->devcaps_region)) {
1577 		dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
1578 			PTR_ERR(pf->devcaps_region));
1579 		pf->devcaps_region = NULL;
1580 	}
1581 }
1582 
1583 /**
1584  * ice_devlink_destroy_regions - Destroy devlink regions
1585  * @pf: the PF device structure
1586  *
1587  * Remove previously created regions for this PF.
1588  */
1589 void ice_devlink_destroy_regions(struct ice_pf *pf)
1590 {
1591 	if (pf->nvm_region)
1592 		devl_region_destroy(pf->nvm_region);
1593 
1594 	if (pf->sram_region)
1595 		devl_region_destroy(pf->sram_region);
1596 
1597 	if (pf->devcaps_region)
1598 		devl_region_destroy(pf->devcaps_region);
1599 }
1600