xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_devlink.c (revision 47327e198d42c77322dbe175817499d2d7ddc26a)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_lib.h"
6 #include "ice_devlink.h"
7 #include "ice_eswitch.h"
8 #include "ice_fw_update.h"
9 
10 /* context for devlink info version reporting */
11 struct ice_info_ctx {
12 	char buf[128];
13 	struct ice_orom_info pending_orom;
14 	struct ice_nvm_info pending_nvm;
15 	struct ice_netlist_info pending_netlist;
16 	struct ice_hw_dev_caps dev_caps;
17 };
18 
19 /* The following functions are used to format specific strings for various
20  * devlink info versions. The ctx parameter is used to provide the storage
21  * buffer, as well as any ancillary information calculated when the info
22  * request was made.
23  *
24  * If a version does not exist, for example when attempting to get the
25  * inactive version of flash when there is no pending update, the function
26  * should leave the buffer in the ctx structure empty.
27  */
28 
29 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
30 {
31 	u8 dsn[8];
32 
33 	/* Copy the DSN into an array in Big Endian format */
34 	put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
35 
36 	snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
37 }
38 
39 static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
40 {
41 	struct ice_hw *hw = &pf->hw;
42 	enum ice_status status;
43 
44 	status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
45 	if (status)
46 		/* We failed to locate the PBA, so just skip this entry */
47 		dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %s\n",
48 			ice_stat_str(status));
49 }
50 
51 static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
52 {
53 	struct ice_hw *hw = &pf->hw;
54 
55 	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
56 		 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
57 }
58 
59 static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
60 {
61 	struct ice_hw *hw = &pf->hw;
62 
63 	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
64 		 hw->api_min_ver, hw->api_patch);
65 }
66 
67 static void ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
68 {
69 	struct ice_hw *hw = &pf->hw;
70 
71 	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
72 }
73 
74 static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
75 {
76 	struct ice_orom_info *orom = &pf->hw.flash.orom;
77 
78 	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
79 		 orom->major, orom->build, orom->patch);
80 }
81 
82 static void
83 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
84 			  struct ice_info_ctx *ctx)
85 {
86 	struct ice_orom_info *orom = &ctx->pending_orom;
87 
88 	if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
89 		snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
90 			 orom->major, orom->build, orom->patch);
91 }
92 
93 static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
94 {
95 	struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
96 
97 	snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
98 }
99 
100 static void
101 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
102 			 struct ice_info_ctx *ctx)
103 {
104 	struct ice_nvm_info *nvm = &ctx->pending_nvm;
105 
106 	if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
107 		snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
108 			 nvm->major, nvm->minor);
109 }
110 
111 static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
112 {
113 	struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
114 
115 	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
116 }
117 
118 static void
119 ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
120 {
121 	struct ice_nvm_info *nvm = &ctx->pending_nvm;
122 
123 	if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
124 		snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
125 }
126 
127 static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
128 {
129 	struct ice_hw *hw = &pf->hw;
130 
131 	snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
132 }
133 
134 static void
135 ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
136 {
137 	struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
138 
139 	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
140 		 pkg->major, pkg->minor, pkg->update, pkg->draft);
141 }
142 
143 static void
144 ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
145 {
146 	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
147 }
148 
149 static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
150 {
151 	struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
152 
153 	/* The netlist version fields are BCD formatted */
154 	snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
155 		 netlist->major, netlist->minor,
156 		 netlist->type >> 16, netlist->type & 0xFFFF,
157 		 netlist->rev, netlist->cust_ver);
158 }
159 
160 static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
161 {
162 	struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
163 
164 	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
165 }
166 
167 static void
168 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
169 			     struct ice_info_ctx *ctx)
170 {
171 	struct ice_netlist_info *netlist = &ctx->pending_netlist;
172 
173 	/* The netlist version fields are BCD formatted */
174 	if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
175 		snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
176 			 netlist->major, netlist->minor,
177 			 netlist->type >> 16, netlist->type & 0xFFFF,
178 			 netlist->rev, netlist->cust_ver);
179 }
180 
181 static void
182 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
183 			       struct ice_info_ctx *ctx)
184 {
185 	struct ice_netlist_info *netlist = &ctx->pending_netlist;
186 
187 	if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
188 		snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
189 }
190 
191 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
192 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
193 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
194 
195 /* The combined() macro inserts both the running entry as well as a stored
196  * entry. The running entry will always report the version from the active
197  * handler. The stored entry will first try the pending handler, and fallback
198  * to the active handler if the pending function does not report a version.
199  * The pending handler should check the status of a pending update for the
200  * relevant flash component. It should only fill in the buffer in the case
201  * where a valid pending version is available. This ensures that the related
202  * stored and running versions remain in sync, and that stored versions are
203  * correctly reported as expected.
204  */
205 #define combined(key, active, pending) \
206 	running(key, active), \
207 	stored(key, pending, active)
208 
209 enum ice_version_type {
210 	ICE_VERSION_FIXED,
211 	ICE_VERSION_RUNNING,
212 	ICE_VERSION_STORED,
213 };
214 
215 static const struct ice_devlink_version {
216 	enum ice_version_type type;
217 	const char *key;
218 	void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
219 	void (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
220 } ice_devlink_versions[] = {
221 	fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
222 	running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
223 	running("fw.mgmt.api", ice_info_fw_api),
224 	running("fw.mgmt.build", ice_info_fw_build),
225 	combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
226 	combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
227 	combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
228 	running("fw.app.name", ice_info_ddp_pkg_name),
229 	running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
230 	running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
231 	combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
232 	combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
233 };
234 
235 /**
236  * ice_devlink_info_get - .info_get devlink handler
237  * @devlink: devlink instance structure
238  * @req: the devlink info request
239  * @extack: extended netdev ack structure
240  *
241  * Callback for the devlink .info_get operation. Reports information about the
242  * device.
243  *
244  * Return: zero on success or an error code on failure.
245  */
246 static int ice_devlink_info_get(struct devlink *devlink,
247 				struct devlink_info_req *req,
248 				struct netlink_ext_ack *extack)
249 {
250 	struct ice_pf *pf = devlink_priv(devlink);
251 	struct device *dev = ice_pf_to_dev(pf);
252 	struct ice_hw *hw = &pf->hw;
253 	struct ice_info_ctx *ctx;
254 	enum ice_status status;
255 	size_t i;
256 	int err;
257 
258 	err = ice_wait_for_reset(pf, 10 * HZ);
259 	if (err) {
260 		NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
261 		return err;
262 	}
263 
264 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
265 	if (!ctx)
266 		return -ENOMEM;
267 
268 	/* discover capabilities first */
269 	status = ice_discover_dev_caps(hw, &ctx->dev_caps);
270 	if (status) {
271 		dev_dbg(dev, "Failed to discover device capabilities, status %s aq_err %s\n",
272 			ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
273 		NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
274 		err = -EIO;
275 		goto out_free_ctx;
276 	}
277 
278 	if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
279 		status = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
280 		if (status) {
281 			dev_dbg(dev, "Unable to read inactive Option ROM version data, status %s aq_err %s\n",
282 				ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
283 
284 			/* disable display of pending Option ROM */
285 			ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
286 		}
287 	}
288 
289 	if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
290 		status = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
291 		if (status) {
292 			dev_dbg(dev, "Unable to read inactive NVM version data, status %s aq_err %s\n",
293 				ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
294 
295 			/* disable display of pending Option ROM */
296 			ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
297 		}
298 	}
299 
300 	if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
301 		status = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
302 		if (status) {
303 			dev_dbg(dev, "Unable to read inactive Netlist version data, status %s aq_err %s\n",
304 				ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
305 
306 			/* disable display of pending Option ROM */
307 			ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
308 		}
309 	}
310 
311 	err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
312 	if (err) {
313 		NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
314 		goto out_free_ctx;
315 	}
316 
317 	ice_info_get_dsn(pf, ctx);
318 
319 	err = devlink_info_serial_number_put(req, ctx->buf);
320 	if (err) {
321 		NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
322 		goto out_free_ctx;
323 	}
324 
325 	for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
326 		enum ice_version_type type = ice_devlink_versions[i].type;
327 		const char *key = ice_devlink_versions[i].key;
328 
329 		memset(ctx->buf, 0, sizeof(ctx->buf));
330 
331 		ice_devlink_versions[i].getter(pf, ctx);
332 
333 		/* If the default getter doesn't report a version, use the
334 		 * fallback function. This is primarily useful in the case of
335 		 * "stored" versions that want to report the same value as the
336 		 * running version in the normal case of no pending update.
337 		 */
338 		if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
339 			ice_devlink_versions[i].fallback(pf, ctx);
340 
341 		/* Do not report missing versions */
342 		if (ctx->buf[0] == '\0')
343 			continue;
344 
345 		switch (type) {
346 		case ICE_VERSION_FIXED:
347 			err = devlink_info_version_fixed_put(req, key, ctx->buf);
348 			if (err) {
349 				NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
350 				goto out_free_ctx;
351 			}
352 			break;
353 		case ICE_VERSION_RUNNING:
354 			err = devlink_info_version_running_put(req, key, ctx->buf);
355 			if (err) {
356 				NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
357 				goto out_free_ctx;
358 			}
359 			break;
360 		case ICE_VERSION_STORED:
361 			err = devlink_info_version_stored_put(req, key, ctx->buf);
362 			if (err) {
363 				NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
364 				goto out_free_ctx;
365 			}
366 			break;
367 		}
368 	}
369 
370 out_free_ctx:
371 	kfree(ctx);
372 	return err;
373 }
374 
375 /**
376  * ice_devlink_flash_update - Update firmware stored in flash on the device
377  * @devlink: pointer to devlink associated with device to update
378  * @params: flash update parameters
379  * @extack: netlink extended ACK structure
380  *
381  * Perform a device flash update. The bulk of the update logic is contained
382  * within the ice_flash_pldm_image function.
383  *
384  * Returns: zero on success, or an error code on failure.
385  */
386 static int
387 ice_devlink_flash_update(struct devlink *devlink,
388 			 struct devlink_flash_update_params *params,
389 			 struct netlink_ext_ack *extack)
390 {
391 	struct ice_pf *pf = devlink_priv(devlink);
392 	struct ice_hw *hw = &pf->hw;
393 	u8 preservation;
394 	int err;
395 
396 	if (!params->overwrite_mask) {
397 		/* preserve all settings and identifiers */
398 		preservation = ICE_AQC_NVM_PRESERVE_ALL;
399 	} else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
400 		/* overwrite settings, but preserve the vital device identifiers */
401 		preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
402 	} else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
403 					      DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
404 		/* overwrite both settings and identifiers, preserve nothing */
405 		preservation = ICE_AQC_NVM_NO_PRESERVATION;
406 	} else {
407 		NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
408 		return -EOPNOTSUPP;
409 	}
410 
411 	if (!hw->dev_caps.common_cap.nvm_unified_update) {
412 		NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
413 		return -EOPNOTSUPP;
414 	}
415 
416 	err = ice_check_for_pending_update(pf, NULL, extack);
417 	if (err)
418 		return err;
419 
420 	devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
421 
422 	return ice_flash_pldm_image(pf, params->fw, preservation, extack);
423 }
424 
425 static const struct devlink_ops ice_devlink_ops = {
426 	.supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
427 	.eswitch_mode_get = ice_eswitch_mode_get,
428 	.eswitch_mode_set = ice_eswitch_mode_set,
429 	.info_get = ice_devlink_info_get,
430 	.flash_update = ice_devlink_flash_update,
431 };
432 
433 static int
434 ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
435 			    struct devlink_param_gset_ctx *ctx)
436 {
437 	struct ice_pf *pf = devlink_priv(devlink);
438 
439 	ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2;
440 
441 	return 0;
442 }
443 
444 static int
445 ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
446 			    struct devlink_param_gset_ctx *ctx)
447 {
448 	struct ice_pf *pf = devlink_priv(devlink);
449 	bool roce_ena = ctx->val.vbool;
450 	int ret;
451 
452 	if (!roce_ena) {
453 		ice_unplug_aux_dev(pf);
454 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
455 		return 0;
456 	}
457 
458 	pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
459 	ret = ice_plug_aux_dev(pf);
460 	if (ret)
461 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
462 
463 	return ret;
464 }
465 
466 static int
467 ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
468 				 union devlink_param_value val,
469 				 struct netlink_ext_ack *extack)
470 {
471 	struct ice_pf *pf = devlink_priv(devlink);
472 
473 	if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
474 		return -EOPNOTSUPP;
475 
476 	if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
477 		NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
478 		return -EOPNOTSUPP;
479 	}
480 
481 	return 0;
482 }
483 
484 static int
485 ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
486 			  struct devlink_param_gset_ctx *ctx)
487 {
488 	struct ice_pf *pf = devlink_priv(devlink);
489 
490 	ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
491 
492 	return 0;
493 }
494 
495 static int
496 ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
497 			  struct devlink_param_gset_ctx *ctx)
498 {
499 	struct ice_pf *pf = devlink_priv(devlink);
500 	bool iw_ena = ctx->val.vbool;
501 	int ret;
502 
503 	if (!iw_ena) {
504 		ice_unplug_aux_dev(pf);
505 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
506 		return 0;
507 	}
508 
509 	pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
510 	ret = ice_plug_aux_dev(pf);
511 	if (ret)
512 		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
513 
514 	return ret;
515 }
516 
517 static int
518 ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
519 			       union devlink_param_value val,
520 			       struct netlink_ext_ack *extack)
521 {
522 	struct ice_pf *pf = devlink_priv(devlink);
523 
524 	if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
525 		return -EOPNOTSUPP;
526 
527 	if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
528 		NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
529 		return -EOPNOTSUPP;
530 	}
531 
532 	return 0;
533 }
534 
535 static const struct devlink_param ice_devlink_params[] = {
536 	DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
537 			      ice_devlink_enable_roce_get,
538 			      ice_devlink_enable_roce_set,
539 			      ice_devlink_enable_roce_validate),
540 	DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
541 			      ice_devlink_enable_iw_get,
542 			      ice_devlink_enable_iw_set,
543 			      ice_devlink_enable_iw_validate),
544 
545 };
546 
547 static void ice_devlink_free(void *devlink_ptr)
548 {
549 	devlink_free((struct devlink *)devlink_ptr);
550 }
551 
552 /**
553  * ice_allocate_pf - Allocate devlink and return PF structure pointer
554  * @dev: the device to allocate for
555  *
556  * Allocate a devlink instance for this device and return the private area as
557  * the PF structure. The devlink memory is kept track of through devres by
558  * adding an action to remove it when unwinding.
559  */
560 struct ice_pf *ice_allocate_pf(struct device *dev)
561 {
562 	struct devlink *devlink;
563 
564 	devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
565 	if (!devlink)
566 		return NULL;
567 
568 	/* Add an action to teardown the devlink when unwinding the driver */
569 	if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
570 		return NULL;
571 
572 	return devlink_priv(devlink);
573 }
574 
575 /**
576  * ice_devlink_register - Register devlink interface for this PF
577  * @pf: the PF to register the devlink for.
578  *
579  * Register the devlink instance associated with this physical function.
580  *
581  * Return: zero on success or an error code on failure.
582  */
583 void ice_devlink_register(struct ice_pf *pf)
584 {
585 	struct devlink *devlink = priv_to_devlink(pf);
586 
587 	devlink_register(devlink);
588 }
589 
590 /**
591  * ice_devlink_unregister - Unregister devlink resources for this PF.
592  * @pf: the PF structure to cleanup
593  *
594  * Releases resources used by devlink and cleans up associated memory.
595  */
596 void ice_devlink_unregister(struct ice_pf *pf)
597 {
598 	devlink_unregister(priv_to_devlink(pf));
599 }
600 
601 int ice_devlink_register_params(struct ice_pf *pf)
602 {
603 	struct devlink *devlink = priv_to_devlink(pf);
604 	union devlink_param_value value;
605 	int err;
606 
607 	err = devlink_params_register(devlink, ice_devlink_params,
608 				      ARRAY_SIZE(ice_devlink_params));
609 	if (err)
610 		return err;
611 
612 	value.vbool = false;
613 	devlink_param_driverinit_value_set(devlink,
614 					   DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
615 					   value);
616 
617 	value.vbool = test_bit(ICE_FLAG_RDMA_ENA, pf->flags) ? true : false;
618 	devlink_param_driverinit_value_set(devlink,
619 					   DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
620 					   value);
621 
622 	return 0;
623 }
624 
625 void ice_devlink_unregister_params(struct ice_pf *pf)
626 {
627 	devlink_params_unregister(priv_to_devlink(pf), ice_devlink_params,
628 				  ARRAY_SIZE(ice_devlink_params));
629 }
630 
631 /**
632  * ice_devlink_create_pf_port - Create a devlink port for this PF
633  * @pf: the PF to create a devlink port for
634  *
635  * Create and register a devlink_port for this PF.
636  *
637  * Return: zero on success or an error code on failure.
638  */
639 int ice_devlink_create_pf_port(struct ice_pf *pf)
640 {
641 	struct devlink_port_attrs attrs = {};
642 	struct devlink_port *devlink_port;
643 	struct devlink *devlink;
644 	struct ice_vsi *vsi;
645 	struct device *dev;
646 	int err;
647 
648 	dev = ice_pf_to_dev(pf);
649 
650 	devlink_port = &pf->devlink_port;
651 
652 	vsi = ice_get_main_vsi(pf);
653 	if (!vsi)
654 		return -EIO;
655 
656 	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
657 	attrs.phys.port_number = pf->hw.bus.func;
658 	devlink_port_attrs_set(devlink_port, &attrs);
659 	devlink = priv_to_devlink(pf);
660 
661 	err = devlink_port_register(devlink, devlink_port, vsi->idx);
662 	if (err) {
663 		dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
664 			pf->hw.pf_id, err);
665 		return err;
666 	}
667 
668 	return 0;
669 }
670 
671 /**
672  * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
673  * @pf: the PF to cleanup
674  *
675  * Unregisters the devlink_port structure associated with this PF.
676  */
677 void ice_devlink_destroy_pf_port(struct ice_pf *pf)
678 {
679 	struct devlink_port *devlink_port;
680 
681 	devlink_port = &pf->devlink_port;
682 
683 	devlink_port_type_clear(devlink_port);
684 	devlink_port_unregister(devlink_port);
685 }
686 
687 /**
688  * ice_devlink_create_vf_port - Create a devlink port for this VF
689  * @vf: the VF to create a port for
690  *
691  * Create and register a devlink_port for this VF.
692  *
693  * Return: zero on success or an error code on failure.
694  */
695 int ice_devlink_create_vf_port(struct ice_vf *vf)
696 {
697 	struct devlink_port_attrs attrs = {};
698 	struct devlink_port *devlink_port;
699 	struct devlink *devlink;
700 	struct ice_vsi *vsi;
701 	struct device *dev;
702 	struct ice_pf *pf;
703 	int err;
704 
705 	pf = vf->pf;
706 	dev = ice_pf_to_dev(pf);
707 	vsi = ice_get_vf_vsi(vf);
708 	devlink_port = &vf->devlink_port;
709 
710 	attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
711 	attrs.pci_vf.pf = pf->hw.bus.func;
712 	attrs.pci_vf.vf = vf->vf_id;
713 
714 	devlink_port_attrs_set(devlink_port, &attrs);
715 	devlink = priv_to_devlink(pf);
716 
717 	err = devlink_port_register(devlink, devlink_port, vsi->idx);
718 	if (err) {
719 		dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
720 			vf->vf_id, err);
721 		return err;
722 	}
723 
724 	return 0;
725 }
726 
727 /**
728  * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
729  * @vf: the VF to cleanup
730  *
731  * Unregisters the devlink_port structure associated with this VF.
732  */
733 void ice_devlink_destroy_vf_port(struct ice_vf *vf)
734 {
735 	struct devlink_port *devlink_port;
736 
737 	devlink_port = &vf->devlink_port;
738 
739 	devlink_port_type_clear(devlink_port);
740 	devlink_port_unregister(devlink_port);
741 }
742 
743 /**
744  * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
745  * @devlink: the devlink instance
746  * @ops: the devlink region being snapshotted
747  * @extack: extended ACK response structure
748  * @data: on exit points to snapshot data buffer
749  *
750  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
751  * the shadow-ram devlink region. It captures a snapshot of the shadow ram
752  * contents. This snapshot can later be viewed via the devlink-region
753  * interface.
754  *
755  * @returns zero on success, and updates the data pointer. Returns a non-zero
756  * error code on failure.
757  */
758 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
759 				    const struct devlink_region_ops *ops,
760 				    struct netlink_ext_ack *extack, u8 **data)
761 {
762 	struct ice_pf *pf = devlink_priv(devlink);
763 	struct device *dev = ice_pf_to_dev(pf);
764 	struct ice_hw *hw = &pf->hw;
765 	enum ice_status status;
766 	void *nvm_data;
767 	u32 nvm_size;
768 
769 	nvm_size = hw->flash.flash_size;
770 	nvm_data = vzalloc(nvm_size);
771 	if (!nvm_data)
772 		return -ENOMEM;
773 
774 	status = ice_acquire_nvm(hw, ICE_RES_READ);
775 	if (status) {
776 		dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
777 			status, hw->adminq.sq_last_status);
778 		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
779 		vfree(nvm_data);
780 		return -EIO;
781 	}
782 
783 	status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
784 	if (status) {
785 		dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
786 			nvm_size, status, hw->adminq.sq_last_status);
787 		NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
788 		ice_release_nvm(hw);
789 		vfree(nvm_data);
790 		return -EIO;
791 	}
792 
793 	ice_release_nvm(hw);
794 
795 	*data = nvm_data;
796 
797 	return 0;
798 }
799 
800 /**
801  * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
802  * @devlink: the devlink instance
803  * @ops: the devlink region being snapshotted
804  * @extack: extended ACK response structure
805  * @data: on exit points to snapshot data buffer
806  *
807  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
808  * the device-caps devlink region. It captures a snapshot of the device
809  * capabilities reported by firmware.
810  *
811  * @returns zero on success, and updates the data pointer. Returns a non-zero
812  * error code on failure.
813  */
814 static int
815 ice_devlink_devcaps_snapshot(struct devlink *devlink,
816 			     const struct devlink_region_ops *ops,
817 			     struct netlink_ext_ack *extack, u8 **data)
818 {
819 	struct ice_pf *pf = devlink_priv(devlink);
820 	struct device *dev = ice_pf_to_dev(pf);
821 	struct ice_hw *hw = &pf->hw;
822 	enum ice_status status;
823 	void *devcaps;
824 
825 	devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
826 	if (!devcaps)
827 		return -ENOMEM;
828 
829 	status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
830 				  ice_aqc_opc_list_dev_caps, NULL);
831 	if (status) {
832 		dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
833 			status, hw->adminq.sq_last_status);
834 		NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
835 		vfree(devcaps);
836 		return -EIO;
837 	}
838 
839 	*data = (u8 *)devcaps;
840 
841 	return 0;
842 }
843 
844 static const struct devlink_region_ops ice_nvm_region_ops = {
845 	.name = "nvm-flash",
846 	.destructor = vfree,
847 	.snapshot = ice_devlink_nvm_snapshot,
848 };
849 
850 static const struct devlink_region_ops ice_devcaps_region_ops = {
851 	.name = "device-caps",
852 	.destructor = vfree,
853 	.snapshot = ice_devlink_devcaps_snapshot,
854 };
855 
856 /**
857  * ice_devlink_init_regions - Initialize devlink regions
858  * @pf: the PF device structure
859  *
860  * Create devlink regions used to enable access to dump the contents of the
861  * flash memory on the device.
862  */
863 void ice_devlink_init_regions(struct ice_pf *pf)
864 {
865 	struct devlink *devlink = priv_to_devlink(pf);
866 	struct device *dev = ice_pf_to_dev(pf);
867 	u64 nvm_size;
868 
869 	nvm_size = pf->hw.flash.flash_size;
870 	pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
871 					       nvm_size);
872 	if (IS_ERR(pf->nvm_region)) {
873 		dev_err(dev, "failed to create NVM devlink region, err %ld\n",
874 			PTR_ERR(pf->nvm_region));
875 		pf->nvm_region = NULL;
876 	}
877 
878 	pf->devcaps_region = devlink_region_create(devlink,
879 						   &ice_devcaps_region_ops, 10,
880 						   ICE_AQ_MAX_BUF_LEN);
881 	if (IS_ERR(pf->devcaps_region)) {
882 		dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
883 			PTR_ERR(pf->devcaps_region));
884 		pf->devcaps_region = NULL;
885 	}
886 }
887 
888 /**
889  * ice_devlink_destroy_regions - Destroy devlink regions
890  * @pf: the PF device structure
891  *
892  * Remove previously created regions for this PF.
893  */
894 void ice_devlink_destroy_regions(struct ice_pf *pf)
895 {
896 	if (pf->nvm_region)
897 		devlink_region_destroy(pf->nvm_region);
898 	if (pf->devcaps_region)
899 		devlink_region_destroy(pf->devcaps_region);
900 }
901