xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt_devlink.c (revision 79ac11393328fb1717d17c12e3c0eef0e9fa0647)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2017 Broadcom Limited
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/pci.h>
11 #include <linux/netdevice.h>
12 #include <linux/vmalloc.h>
13 #include <net/devlink.h>
14 #include "bnxt_hsi.h"
15 #include "bnxt.h"
16 #include "bnxt_hwrm.h"
17 #include "bnxt_vfr.h"
18 #include "bnxt_devlink.h"
19 #include "bnxt_ethtool.h"
20 #include "bnxt_ulp.h"
21 #include "bnxt_ptp.h"
22 #include "bnxt_coredump.h"
23 #include "bnxt_nvm_defs.h"
24 
25 static void __bnxt_fw_recover(struct bnxt *bp)
26 {
27 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
28 	    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
29 		bnxt_fw_reset(bp);
30 	else
31 		bnxt_fw_exception(bp);
32 }
33 
34 static int
35 bnxt_dl_flash_update(struct devlink *dl,
36 		     struct devlink_flash_update_params *params,
37 		     struct netlink_ext_ack *extack)
38 {
39 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
40 	int rc;
41 
42 	if (!BNXT_PF(bp)) {
43 		NL_SET_ERR_MSG_MOD(extack,
44 				   "flash update not supported from a VF");
45 		return -EPERM;
46 	}
47 
48 	devlink_flash_update_status_notify(dl, "Preparing to flash", NULL, 0, 0);
49 	rc = bnxt_flash_package_from_fw_obj(bp->dev, params->fw, 0, extack);
50 	if (!rc)
51 		devlink_flash_update_status_notify(dl, "Flashing done", NULL, 0, 0);
52 	else
53 		devlink_flash_update_status_notify(dl, "Flashing failed", NULL, 0, 0);
54 	return rc;
55 }
56 
57 static int bnxt_hwrm_remote_dev_reset_set(struct bnxt *bp, bool remote_reset)
58 {
59 	struct hwrm_func_cfg_input *req;
60 	int rc;
61 
62 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
63 		return -EOPNOTSUPP;
64 
65 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
66 	if (rc)
67 		return rc;
68 
69 	req->fid = cpu_to_le16(0xffff);
70 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_HOT_RESET_IF_SUPPORT);
71 	if (remote_reset)
72 		req->flags = cpu_to_le32(FUNC_CFG_REQ_FLAGS_HOT_RESET_IF_EN_DIS);
73 
74 	return hwrm_req_send(bp, req);
75 }
76 
77 static char *bnxt_health_severity_str(enum bnxt_health_severity severity)
78 {
79 	switch (severity) {
80 	case SEVERITY_NORMAL: return "normal";
81 	case SEVERITY_WARNING: return "warning";
82 	case SEVERITY_RECOVERABLE: return "recoverable";
83 	case SEVERITY_FATAL: return "fatal";
84 	default: return "unknown";
85 	}
86 }
87 
88 static char *bnxt_health_remedy_str(enum bnxt_health_remedy remedy)
89 {
90 	switch (remedy) {
91 	case REMEDY_DEVLINK_RECOVER: return "devlink recover";
92 	case REMEDY_POWER_CYCLE_DEVICE: return "device power cycle";
93 	case REMEDY_POWER_CYCLE_HOST: return "host power cycle";
94 	case REMEDY_FW_UPDATE: return "update firmware";
95 	case REMEDY_HW_REPLACE: return "replace hardware";
96 	default: return "unknown";
97 	}
98 }
99 
100 static int bnxt_fw_diagnose(struct devlink_health_reporter *reporter,
101 			    struct devlink_fmsg *fmsg,
102 			    struct netlink_ext_ack *extack)
103 {
104 	struct bnxt *bp = devlink_health_reporter_priv(reporter);
105 	struct bnxt_fw_health *h = bp->fw_health;
106 	u32 fw_status, fw_resets;
107 
108 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
109 		devlink_fmsg_string_pair_put(fmsg, "Status", "recovering");
110 		return 0;
111 	}
112 
113 	if (!h->status_reliable) {
114 		devlink_fmsg_string_pair_put(fmsg, "Status", "unknown");
115 		return 0;
116 	}
117 
118 	mutex_lock(&h->lock);
119 	fw_status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
120 	if (BNXT_FW_IS_BOOTING(fw_status)) {
121 		devlink_fmsg_string_pair_put(fmsg, "Status", "initializing");
122 	} else if (h->severity || fw_status != BNXT_FW_STATUS_HEALTHY) {
123 		if (!h->severity) {
124 			h->severity = SEVERITY_FATAL;
125 			h->remedy = REMEDY_POWER_CYCLE_DEVICE;
126 			h->diagnoses++;
127 			devlink_health_report(h->fw_reporter,
128 					      "FW error diagnosed", h);
129 		}
130 		devlink_fmsg_string_pair_put(fmsg, "Status", "error");
131 		devlink_fmsg_u32_pair_put(fmsg, "Syndrome", fw_status);
132 	} else {
133 		devlink_fmsg_string_pair_put(fmsg, "Status", "healthy");
134 	}
135 
136 	devlink_fmsg_string_pair_put(fmsg, "Severity",
137 				     bnxt_health_severity_str(h->severity));
138 
139 	if (h->severity) {
140 		devlink_fmsg_string_pair_put(fmsg, "Remedy",
141 					     bnxt_health_remedy_str(h->remedy));
142 		if (h->remedy == REMEDY_DEVLINK_RECOVER)
143 			devlink_fmsg_string_pair_put(fmsg, "Impact",
144 						     "traffic+ntuple_cfg");
145 	}
146 
147 	mutex_unlock(&h->lock);
148 	if (!h->resets_reliable)
149 		return 0;
150 
151 	fw_resets = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
152 	devlink_fmsg_u32_pair_put(fmsg, "Resets", fw_resets);
153 	devlink_fmsg_u32_pair_put(fmsg, "Arrests", h->arrests);
154 	devlink_fmsg_u32_pair_put(fmsg, "Survivals", h->survivals);
155 	devlink_fmsg_u32_pair_put(fmsg, "Discoveries", h->discoveries);
156 	devlink_fmsg_u32_pair_put(fmsg, "Fatalities", h->fatalities);
157 	devlink_fmsg_u32_pair_put(fmsg, "Diagnoses", h->diagnoses);
158 	return 0;
159 }
160 
161 static int bnxt_fw_dump(struct devlink_health_reporter *reporter,
162 			struct devlink_fmsg *fmsg, void *priv_ctx,
163 			struct netlink_ext_ack *extack)
164 {
165 	struct bnxt *bp = devlink_health_reporter_priv(reporter);
166 	u32 dump_len;
167 	void *data;
168 	int rc;
169 
170 	/* TODO: no firmware dump support in devlink_health_report() context */
171 	if (priv_ctx)
172 		return -EOPNOTSUPP;
173 
174 	dump_len = bnxt_get_coredump_length(bp, BNXT_DUMP_LIVE);
175 	if (!dump_len)
176 		return -EIO;
177 
178 	data = vmalloc(dump_len);
179 	if (!data)
180 		return -ENOMEM;
181 
182 	rc = bnxt_get_coredump(bp, BNXT_DUMP_LIVE, data, &dump_len);
183 	if (!rc) {
184 		devlink_fmsg_pair_nest_start(fmsg, "core");
185 		devlink_fmsg_binary_pair_put(fmsg, "data", data, dump_len);
186 		devlink_fmsg_u32_pair_put(fmsg, "size", dump_len);
187 		devlink_fmsg_pair_nest_end(fmsg);
188 	}
189 
190 	vfree(data);
191 	return rc;
192 }
193 
194 static int bnxt_fw_recover(struct devlink_health_reporter *reporter,
195 			   void *priv_ctx,
196 			   struct netlink_ext_ack *extack)
197 {
198 	struct bnxt *bp = devlink_health_reporter_priv(reporter);
199 
200 	if (bp->fw_health->severity == SEVERITY_FATAL)
201 		return -ENODEV;
202 
203 	set_bit(BNXT_STATE_RECOVER, &bp->state);
204 	__bnxt_fw_recover(bp);
205 
206 	return -EINPROGRESS;
207 }
208 
209 static const struct devlink_health_reporter_ops bnxt_dl_fw_reporter_ops = {
210 	.name = "fw",
211 	.diagnose = bnxt_fw_diagnose,
212 	.dump = bnxt_fw_dump,
213 	.recover = bnxt_fw_recover,
214 };
215 
216 static struct devlink_health_reporter *
217 __bnxt_dl_reporter_create(struct bnxt *bp,
218 			  const struct devlink_health_reporter_ops *ops)
219 {
220 	struct devlink_health_reporter *reporter;
221 
222 	reporter = devlink_health_reporter_create(bp->dl, ops, 0, bp);
223 	if (IS_ERR(reporter)) {
224 		netdev_warn(bp->dev, "Failed to create %s health reporter, rc = %ld\n",
225 			    ops->name, PTR_ERR(reporter));
226 		return NULL;
227 	}
228 
229 	return reporter;
230 }
231 
232 void bnxt_dl_fw_reporters_create(struct bnxt *bp)
233 {
234 	struct bnxt_fw_health *fw_health = bp->fw_health;
235 
236 	if (fw_health && !fw_health->fw_reporter)
237 		fw_health->fw_reporter = __bnxt_dl_reporter_create(bp, &bnxt_dl_fw_reporter_ops);
238 }
239 
240 void bnxt_dl_fw_reporters_destroy(struct bnxt *bp)
241 {
242 	struct bnxt_fw_health *fw_health = bp->fw_health;
243 
244 	if (fw_health && fw_health->fw_reporter) {
245 		devlink_health_reporter_destroy(fw_health->fw_reporter);
246 		fw_health->fw_reporter = NULL;
247 	}
248 }
249 
250 void bnxt_devlink_health_fw_report(struct bnxt *bp)
251 {
252 	struct bnxt_fw_health *fw_health = bp->fw_health;
253 	int rc;
254 
255 	if (!fw_health)
256 		return;
257 
258 	if (!fw_health->fw_reporter) {
259 		__bnxt_fw_recover(bp);
260 		return;
261 	}
262 
263 	mutex_lock(&fw_health->lock);
264 	fw_health->severity = SEVERITY_RECOVERABLE;
265 	fw_health->remedy = REMEDY_DEVLINK_RECOVER;
266 	mutex_unlock(&fw_health->lock);
267 	rc = devlink_health_report(fw_health->fw_reporter, "FW error reported",
268 				   fw_health);
269 	if (rc == -ECANCELED)
270 		__bnxt_fw_recover(bp);
271 }
272 
273 void bnxt_dl_health_fw_status_update(struct bnxt *bp, bool healthy)
274 {
275 	struct bnxt_fw_health *fw_health = bp->fw_health;
276 	u8 state;
277 
278 	mutex_lock(&fw_health->lock);
279 	if (healthy) {
280 		fw_health->severity = SEVERITY_NORMAL;
281 		state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
282 	} else {
283 		fw_health->severity = SEVERITY_FATAL;
284 		fw_health->remedy = REMEDY_POWER_CYCLE_DEVICE;
285 		state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
286 	}
287 	mutex_unlock(&fw_health->lock);
288 	devlink_health_reporter_state_update(fw_health->fw_reporter, state);
289 }
290 
291 void bnxt_dl_health_fw_recovery_done(struct bnxt *bp)
292 {
293 	struct bnxt_dl *dl = devlink_priv(bp->dl);
294 
295 	devlink_health_reporter_recovery_done(bp->fw_health->fw_reporter);
296 	bnxt_hwrm_remote_dev_reset_set(bp, dl->remote_reset);
297 }
298 
299 static int bnxt_dl_info_get(struct devlink *dl, struct devlink_info_req *req,
300 			    struct netlink_ext_ack *extack);
301 
302 static void
303 bnxt_dl_livepatch_report_err(struct bnxt *bp, struct netlink_ext_ack *extack,
304 			     struct hwrm_fw_livepatch_output *resp)
305 {
306 	int err = ((struct hwrm_err_output *)resp)->cmd_err;
307 
308 	switch (err) {
309 	case FW_LIVEPATCH_CMD_ERR_CODE_INVALID_OPCODE:
310 		netdev_err(bp->dev, "Illegal live patch opcode");
311 		NL_SET_ERR_MSG_MOD(extack, "Invalid opcode");
312 		break;
313 	case FW_LIVEPATCH_CMD_ERR_CODE_NOT_SUPPORTED:
314 		NL_SET_ERR_MSG_MOD(extack, "Live patch operation not supported");
315 		break;
316 	case FW_LIVEPATCH_CMD_ERR_CODE_NOT_INSTALLED:
317 		NL_SET_ERR_MSG_MOD(extack, "Live patch not found");
318 		break;
319 	case FW_LIVEPATCH_CMD_ERR_CODE_NOT_PATCHED:
320 		NL_SET_ERR_MSG_MOD(extack,
321 				   "Live patch deactivation failed. Firmware not patched.");
322 		break;
323 	case FW_LIVEPATCH_CMD_ERR_CODE_AUTH_FAIL:
324 		NL_SET_ERR_MSG_MOD(extack, "Live patch not authenticated");
325 		break;
326 	case FW_LIVEPATCH_CMD_ERR_CODE_INVALID_HEADER:
327 		NL_SET_ERR_MSG_MOD(extack, "Incompatible live patch");
328 		break;
329 	case FW_LIVEPATCH_CMD_ERR_CODE_INVALID_SIZE:
330 		NL_SET_ERR_MSG_MOD(extack, "Live patch has invalid size");
331 		break;
332 	case FW_LIVEPATCH_CMD_ERR_CODE_ALREADY_PATCHED:
333 		NL_SET_ERR_MSG_MOD(extack, "Live patch already applied");
334 		break;
335 	default:
336 		netdev_err(bp->dev, "Unexpected live patch error: %d\n", err);
337 		NL_SET_ERR_MSG_MOD(extack, "Failed to activate live patch");
338 		break;
339 	}
340 }
341 
342 /* Live patch status in NVM */
343 #define BNXT_LIVEPATCH_NOT_INSTALLED	0
344 #define BNXT_LIVEPATCH_INSTALLED	FW_LIVEPATCH_QUERY_RESP_STATUS_FLAGS_INSTALL
345 #define BNXT_LIVEPATCH_REMOVED		FW_LIVEPATCH_QUERY_RESP_STATUS_FLAGS_ACTIVE
346 #define BNXT_LIVEPATCH_MASK		(FW_LIVEPATCH_QUERY_RESP_STATUS_FLAGS_INSTALL | \
347 					 FW_LIVEPATCH_QUERY_RESP_STATUS_FLAGS_ACTIVE)
348 #define BNXT_LIVEPATCH_ACTIVATED	BNXT_LIVEPATCH_MASK
349 
350 #define BNXT_LIVEPATCH_STATE(flags)	((flags) & BNXT_LIVEPATCH_MASK)
351 
352 static int
353 bnxt_dl_livepatch_activate(struct bnxt *bp, struct netlink_ext_ack *extack)
354 {
355 	struct hwrm_fw_livepatch_query_output *query_resp;
356 	struct hwrm_fw_livepatch_query_input *query_req;
357 	struct hwrm_fw_livepatch_output *patch_resp;
358 	struct hwrm_fw_livepatch_input *patch_req;
359 	u16 flags, live_patch_state;
360 	bool activated = false;
361 	u32 installed = 0;
362 	u8 target;
363 	int rc;
364 
365 	if (~bp->fw_cap & BNXT_FW_CAP_LIVEPATCH) {
366 		NL_SET_ERR_MSG_MOD(extack, "Device does not support live patch");
367 		return -EOPNOTSUPP;
368 	}
369 
370 	rc = hwrm_req_init(bp, query_req, HWRM_FW_LIVEPATCH_QUERY);
371 	if (rc)
372 		return rc;
373 	query_resp = hwrm_req_hold(bp, query_req);
374 
375 	rc = hwrm_req_init(bp, patch_req, HWRM_FW_LIVEPATCH);
376 	if (rc) {
377 		hwrm_req_drop(bp, query_req);
378 		return rc;
379 	}
380 	patch_req->loadtype = FW_LIVEPATCH_REQ_LOADTYPE_NVM_INSTALL;
381 	patch_resp = hwrm_req_hold(bp, patch_req);
382 
383 	for (target = 1; target <= FW_LIVEPATCH_REQ_FW_TARGET_LAST; target++) {
384 		query_req->fw_target = target;
385 		rc = hwrm_req_send(bp, query_req);
386 		if (rc) {
387 			NL_SET_ERR_MSG_MOD(extack, "Failed to query packages");
388 			break;
389 		}
390 
391 		flags = le16_to_cpu(query_resp->status_flags);
392 		live_patch_state = BNXT_LIVEPATCH_STATE(flags);
393 
394 		if (live_patch_state == BNXT_LIVEPATCH_NOT_INSTALLED)
395 			continue;
396 
397 		if (live_patch_state == BNXT_LIVEPATCH_ACTIVATED) {
398 			activated = true;
399 			continue;
400 		}
401 
402 		if (live_patch_state == BNXT_LIVEPATCH_INSTALLED)
403 			patch_req->opcode = FW_LIVEPATCH_REQ_OPCODE_ACTIVATE;
404 		else if (live_patch_state == BNXT_LIVEPATCH_REMOVED)
405 			patch_req->opcode = FW_LIVEPATCH_REQ_OPCODE_DEACTIVATE;
406 
407 		patch_req->fw_target = target;
408 		rc = hwrm_req_send(bp, patch_req);
409 		if (rc) {
410 			bnxt_dl_livepatch_report_err(bp, extack, patch_resp);
411 			break;
412 		}
413 		installed++;
414 	}
415 
416 	if (!rc && !installed) {
417 		if (activated) {
418 			NL_SET_ERR_MSG_MOD(extack, "Live patch already activated");
419 			rc = -EEXIST;
420 		} else {
421 			NL_SET_ERR_MSG_MOD(extack, "No live patches found");
422 			rc = -ENOENT;
423 		}
424 	}
425 	hwrm_req_drop(bp, query_req);
426 	hwrm_req_drop(bp, patch_req);
427 	return rc;
428 }
429 
430 static int bnxt_dl_reload_down(struct devlink *dl, bool netns_change,
431 			       enum devlink_reload_action action,
432 			       enum devlink_reload_limit limit,
433 			       struct netlink_ext_ack *extack)
434 {
435 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
436 	int rc = 0;
437 
438 	switch (action) {
439 	case DEVLINK_RELOAD_ACTION_DRIVER_REINIT: {
440 		rtnl_lock();
441 		if (bnxt_sriov_cfg(bp)) {
442 			NL_SET_ERR_MSG_MOD(extack,
443 					   "reload is unsupported while VFs are allocated or being configured");
444 			rtnl_unlock();
445 			return -EOPNOTSUPP;
446 		}
447 		if (bp->dev->reg_state == NETREG_UNREGISTERED) {
448 			rtnl_unlock();
449 			return -ENODEV;
450 		}
451 		bnxt_ulp_stop(bp);
452 		if (netif_running(bp->dev)) {
453 			rc = bnxt_close_nic(bp, true, true);
454 			if (rc) {
455 				NL_SET_ERR_MSG_MOD(extack, "Failed to close");
456 				dev_close(bp->dev);
457 				rtnl_unlock();
458 				break;
459 			}
460 		}
461 		bnxt_vf_reps_free(bp);
462 		rc = bnxt_hwrm_func_drv_unrgtr(bp);
463 		if (rc) {
464 			NL_SET_ERR_MSG_MOD(extack, "Failed to deregister");
465 			if (netif_running(bp->dev))
466 				dev_close(bp->dev);
467 			rtnl_unlock();
468 			break;
469 		}
470 		bnxt_cancel_reservations(bp, false);
471 		bnxt_free_ctx_mem(bp);
472 		break;
473 	}
474 	case DEVLINK_RELOAD_ACTION_FW_ACTIVATE: {
475 		if (limit == DEVLINK_RELOAD_LIMIT_NO_RESET)
476 			return bnxt_dl_livepatch_activate(bp, extack);
477 		if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET) {
478 			NL_SET_ERR_MSG_MOD(extack, "Device not capable, requires reboot");
479 			return -EOPNOTSUPP;
480 		}
481 		if (!bnxt_hwrm_reset_permitted(bp)) {
482 			NL_SET_ERR_MSG_MOD(extack,
483 					   "Reset denied by firmware, it may be inhibited by remote driver");
484 			return -EPERM;
485 		}
486 		rtnl_lock();
487 		if (bp->dev->reg_state == NETREG_UNREGISTERED) {
488 			rtnl_unlock();
489 			return -ENODEV;
490 		}
491 		if (netif_running(bp->dev))
492 			set_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
493 		rc = bnxt_hwrm_firmware_reset(bp->dev,
494 					      FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP,
495 					      FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP,
496 					      FW_RESET_REQ_FLAGS_RESET_GRACEFUL |
497 					      FW_RESET_REQ_FLAGS_FW_ACTIVATION);
498 		if (rc) {
499 			NL_SET_ERR_MSG_MOD(extack, "Failed to activate firmware");
500 			clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
501 			rtnl_unlock();
502 		}
503 		break;
504 	}
505 	default:
506 		rc = -EOPNOTSUPP;
507 	}
508 
509 	return rc;
510 }
511 
512 static int bnxt_dl_reload_up(struct devlink *dl, enum devlink_reload_action action,
513 			     enum devlink_reload_limit limit, u32 *actions_performed,
514 			     struct netlink_ext_ack *extack)
515 {
516 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
517 	int rc = 0;
518 
519 	*actions_performed = 0;
520 	switch (action) {
521 	case DEVLINK_RELOAD_ACTION_DRIVER_REINIT: {
522 		bnxt_fw_init_one(bp);
523 		bnxt_vf_reps_alloc(bp);
524 		if (netif_running(bp->dev))
525 			rc = bnxt_open_nic(bp, true, true);
526 		bnxt_ulp_start(bp, rc);
527 		if (!rc) {
528 			bnxt_reenable_sriov(bp);
529 			bnxt_ptp_reapply_pps(bp);
530 		}
531 		break;
532 	}
533 	case DEVLINK_RELOAD_ACTION_FW_ACTIVATE: {
534 		unsigned long start = jiffies;
535 		unsigned long timeout = start + BNXT_DFLT_FW_RST_MAX_DSECS * HZ / 10;
536 
537 		if (limit == DEVLINK_RELOAD_LIMIT_NO_RESET)
538 			break;
539 		if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
540 			timeout = start + bp->fw_health->normal_func_wait_dsecs * HZ / 10;
541 		if (!netif_running(bp->dev))
542 			NL_SET_ERR_MSG_MOD(extack,
543 					   "Device is closed, not waiting for reset notice that will never come");
544 		rtnl_unlock();
545 		while (test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) {
546 			if (time_after(jiffies, timeout)) {
547 				NL_SET_ERR_MSG_MOD(extack, "Activation incomplete");
548 				rc = -ETIMEDOUT;
549 				break;
550 			}
551 			if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
552 				NL_SET_ERR_MSG_MOD(extack, "Activation aborted");
553 				rc = -ENODEV;
554 				break;
555 			}
556 			msleep(50);
557 		}
558 		rtnl_lock();
559 		if (!rc)
560 			*actions_performed |= BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
561 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
562 		break;
563 	}
564 	default:
565 		return -EOPNOTSUPP;
566 	}
567 
568 	if (!rc) {
569 		bnxt_print_device_info(bp);
570 		if (netif_running(bp->dev)) {
571 			mutex_lock(&bp->link_lock);
572 			bnxt_report_link(bp);
573 			mutex_unlock(&bp->link_lock);
574 		}
575 		*actions_performed |= BIT(action);
576 	} else if (netif_running(bp->dev)) {
577 		dev_close(bp->dev);
578 	}
579 	rtnl_unlock();
580 	return rc;
581 }
582 
583 static bool bnxt_nvm_test(struct bnxt *bp, struct netlink_ext_ack *extack)
584 {
585 	bool rc = false;
586 	u32 datalen;
587 	u16 index;
588 	u8 *buf;
589 
590 	if (bnxt_find_nvram_item(bp->dev, BNX_DIR_TYPE_VPD,
591 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
592 				 &index, NULL, &datalen) || !datalen) {
593 		NL_SET_ERR_MSG_MOD(extack, "nvm test vpd entry error");
594 		return false;
595 	}
596 
597 	buf = kzalloc(datalen, GFP_KERNEL);
598 	if (!buf) {
599 		NL_SET_ERR_MSG_MOD(extack, "insufficient memory for nvm test");
600 		return false;
601 	}
602 
603 	if (bnxt_get_nvram_item(bp->dev, index, 0, datalen, buf)) {
604 		NL_SET_ERR_MSG_MOD(extack, "nvm test vpd read error");
605 		goto done;
606 	}
607 
608 	if (bnxt_flash_nvram(bp->dev, BNX_DIR_TYPE_VPD, BNX_DIR_ORDINAL_FIRST,
609 			     BNX_DIR_EXT_NONE, 0, 0, buf, datalen)) {
610 		NL_SET_ERR_MSG_MOD(extack, "nvm test vpd write error");
611 		goto done;
612 	}
613 
614 	rc = true;
615 
616 done:
617 	kfree(buf);
618 	return rc;
619 }
620 
621 static bool bnxt_dl_selftest_check(struct devlink *dl, unsigned int id,
622 				   struct netlink_ext_ack *extack)
623 {
624 	return id == DEVLINK_ATTR_SELFTEST_ID_FLASH;
625 }
626 
627 static enum devlink_selftest_status bnxt_dl_selftest_run(struct devlink *dl,
628 							 unsigned int id,
629 							 struct netlink_ext_ack *extack)
630 {
631 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
632 
633 	if (id == DEVLINK_ATTR_SELFTEST_ID_FLASH)
634 		return bnxt_nvm_test(bp, extack) ?
635 				DEVLINK_SELFTEST_STATUS_PASS :
636 				DEVLINK_SELFTEST_STATUS_FAIL;
637 
638 	return DEVLINK_SELFTEST_STATUS_SKIP;
639 }
640 
641 static const struct devlink_ops bnxt_dl_ops = {
642 #ifdef CONFIG_BNXT_SRIOV
643 	.eswitch_mode_set = bnxt_dl_eswitch_mode_set,
644 	.eswitch_mode_get = bnxt_dl_eswitch_mode_get,
645 #endif /* CONFIG_BNXT_SRIOV */
646 	.info_get	  = bnxt_dl_info_get,
647 	.flash_update	  = bnxt_dl_flash_update,
648 	.reload_actions	  = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
649 			    BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
650 	.reload_limits	  = BIT(DEVLINK_RELOAD_LIMIT_NO_RESET),
651 	.reload_down	  = bnxt_dl_reload_down,
652 	.reload_up	  = bnxt_dl_reload_up,
653 	.selftest_check	  = bnxt_dl_selftest_check,
654 	.selftest_run	  = bnxt_dl_selftest_run,
655 };
656 
657 static const struct devlink_ops bnxt_vf_dl_ops;
658 
659 enum bnxt_dl_param_id {
660 	BNXT_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
661 	BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK,
662 };
663 
664 static const struct bnxt_dl_nvm_param nvm_params[] = {
665 	{DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV, NVM_OFF_ENABLE_SRIOV,
666 	 BNXT_NVM_SHARED_CFG, 1, 1},
667 	{DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI, NVM_OFF_IGNORE_ARI,
668 	 BNXT_NVM_SHARED_CFG, 1, 1},
669 	{DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
670 	 NVM_OFF_MSIX_VEC_PER_PF_MAX, BNXT_NVM_SHARED_CFG, 10, 4},
671 	{DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
672 	 NVM_OFF_MSIX_VEC_PER_PF_MIN, BNXT_NVM_SHARED_CFG, 7, 4},
673 	{BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK, NVM_OFF_DIS_GRE_VER_CHECK,
674 	 BNXT_NVM_SHARED_CFG, 1, 1},
675 };
676 
677 union bnxt_nvm_data {
678 	u8	val8;
679 	__le32	val32;
680 };
681 
682 static void bnxt_copy_to_nvm_data(union bnxt_nvm_data *dst,
683 				  union devlink_param_value *src,
684 				  int nvm_num_bits, int dl_num_bytes)
685 {
686 	u32 val32 = 0;
687 
688 	if (nvm_num_bits == 1) {
689 		dst->val8 = src->vbool;
690 		return;
691 	}
692 	if (dl_num_bytes == 4)
693 		val32 = src->vu32;
694 	else if (dl_num_bytes == 2)
695 		val32 = (u32)src->vu16;
696 	else if (dl_num_bytes == 1)
697 		val32 = (u32)src->vu8;
698 	dst->val32 = cpu_to_le32(val32);
699 }
700 
701 static void bnxt_copy_from_nvm_data(union devlink_param_value *dst,
702 				    union bnxt_nvm_data *src,
703 				    int nvm_num_bits, int dl_num_bytes)
704 {
705 	u32 val32;
706 
707 	if (nvm_num_bits == 1) {
708 		dst->vbool = src->val8;
709 		return;
710 	}
711 	val32 = le32_to_cpu(src->val32);
712 	if (dl_num_bytes == 4)
713 		dst->vu32 = val32;
714 	else if (dl_num_bytes == 2)
715 		dst->vu16 = (u16)val32;
716 	else if (dl_num_bytes == 1)
717 		dst->vu8 = (u8)val32;
718 }
719 
720 static int bnxt_hwrm_get_nvm_cfg_ver(struct bnxt *bp, u32 *nvm_cfg_ver)
721 {
722 	struct hwrm_nvm_get_variable_input *req;
723 	u16 bytes = BNXT_NVM_CFG_VER_BYTES;
724 	u16 bits = BNXT_NVM_CFG_VER_BITS;
725 	union devlink_param_value ver;
726 	union bnxt_nvm_data *data;
727 	dma_addr_t data_dma_addr;
728 	int rc, i = 2;
729 	u16 dim = 1;
730 
731 	rc = hwrm_req_init(bp, req, HWRM_NVM_GET_VARIABLE);
732 	if (rc)
733 		return rc;
734 
735 	data = hwrm_req_dma_slice(bp, req, sizeof(*data), &data_dma_addr);
736 	if (!data) {
737 		rc = -ENOMEM;
738 		goto exit;
739 	}
740 
741 	/* earlier devices present as an array of raw bytes */
742 	if (!BNXT_CHIP_P5_PLUS(bp)) {
743 		dim = 0;
744 		i = 0;
745 		bits *= 3;  /* array of 3 version components */
746 		bytes *= 4; /* copy whole word */
747 	}
748 
749 	hwrm_req_hold(bp, req);
750 	req->dest_data_addr = cpu_to_le64(data_dma_addr);
751 	req->data_len = cpu_to_le16(bits);
752 	req->option_num = cpu_to_le16(NVM_OFF_NVM_CFG_VER);
753 	req->dimensions = cpu_to_le16(dim);
754 
755 	while (i >= 0) {
756 		req->index_0 = cpu_to_le16(i--);
757 		rc = hwrm_req_send_silent(bp, req);
758 		if (rc)
759 			goto exit;
760 		bnxt_copy_from_nvm_data(&ver, data, bits, bytes);
761 
762 		if (BNXT_CHIP_P5_PLUS(bp)) {
763 			*nvm_cfg_ver <<= 8;
764 			*nvm_cfg_ver |= ver.vu8;
765 		} else {
766 			*nvm_cfg_ver = ver.vu32;
767 		}
768 	}
769 
770 exit:
771 	hwrm_req_drop(bp, req);
772 	return rc;
773 }
774 
775 static int bnxt_dl_info_put(struct bnxt *bp, struct devlink_info_req *req,
776 			    enum bnxt_dl_version_type type, const char *key,
777 			    char *buf)
778 {
779 	if (!strlen(buf))
780 		return 0;
781 
782 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
783 	    (!strcmp(key, DEVLINK_INFO_VERSION_GENERIC_FW_NCSI) ||
784 	     !strcmp(key, DEVLINK_INFO_VERSION_GENERIC_FW_ROCE)))
785 		return 0;
786 
787 	switch (type) {
788 	case BNXT_VERSION_FIXED:
789 		return devlink_info_version_fixed_put(req, key, buf);
790 	case BNXT_VERSION_RUNNING:
791 		return devlink_info_version_running_put(req, key, buf);
792 	case BNXT_VERSION_STORED:
793 		return devlink_info_version_stored_put(req, key, buf);
794 	}
795 	return 0;
796 }
797 
798 #define BNXT_FW_SRT_PATCH	"fw.srt.patch"
799 #define BNXT_FW_CRT_PATCH	"fw.crt.patch"
800 
801 static int bnxt_dl_livepatch_info_put(struct bnxt *bp,
802 				      struct devlink_info_req *req,
803 				      const char *key)
804 {
805 	struct hwrm_fw_livepatch_query_input *query;
806 	struct hwrm_fw_livepatch_query_output *resp;
807 	u16 flags;
808 	int rc;
809 
810 	if (~bp->fw_cap & BNXT_FW_CAP_LIVEPATCH)
811 		return 0;
812 
813 	rc = hwrm_req_init(bp, query, HWRM_FW_LIVEPATCH_QUERY);
814 	if (rc)
815 		return rc;
816 
817 	if (!strcmp(key, BNXT_FW_SRT_PATCH))
818 		query->fw_target = FW_LIVEPATCH_QUERY_REQ_FW_TARGET_SECURE_FW;
819 	else if (!strcmp(key, BNXT_FW_CRT_PATCH))
820 		query->fw_target = FW_LIVEPATCH_QUERY_REQ_FW_TARGET_COMMON_FW;
821 	else
822 		goto exit;
823 
824 	resp = hwrm_req_hold(bp, query);
825 	rc = hwrm_req_send(bp, query);
826 	if (rc)
827 		goto exit;
828 
829 	flags = le16_to_cpu(resp->status_flags);
830 	if (flags & FW_LIVEPATCH_QUERY_RESP_STATUS_FLAGS_ACTIVE) {
831 		resp->active_ver[sizeof(resp->active_ver) - 1] = '\0';
832 		rc = devlink_info_version_running_put(req, key, resp->active_ver);
833 		if (rc)
834 			goto exit;
835 	}
836 
837 	if (flags & FW_LIVEPATCH_QUERY_RESP_STATUS_FLAGS_INSTALL) {
838 		resp->install_ver[sizeof(resp->install_ver) - 1] = '\0';
839 		rc = devlink_info_version_stored_put(req, key, resp->install_ver);
840 		if (rc)
841 			goto exit;
842 	}
843 
844 exit:
845 	hwrm_req_drop(bp, query);
846 	return rc;
847 }
848 
849 #define HWRM_FW_VER_STR_LEN	16
850 
851 static int bnxt_dl_info_get(struct devlink *dl, struct devlink_info_req *req,
852 			    struct netlink_ext_ack *extack)
853 {
854 	struct hwrm_nvm_get_dev_info_output nvm_dev_info;
855 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
856 	struct hwrm_ver_get_output *ver_resp;
857 	char mgmt_ver[FW_VER_STR_LEN];
858 	char roce_ver[FW_VER_STR_LEN];
859 	char ncsi_ver[FW_VER_STR_LEN];
860 	char buf[32];
861 	u32 ver = 0;
862 	int rc;
863 
864 	if (BNXT_PF(bp) && (bp->flags & BNXT_FLAG_DSN_VALID)) {
865 		sprintf(buf, "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X",
866 			bp->dsn[7], bp->dsn[6], bp->dsn[5], bp->dsn[4],
867 			bp->dsn[3], bp->dsn[2], bp->dsn[1], bp->dsn[0]);
868 		rc = devlink_info_serial_number_put(req, buf);
869 		if (rc)
870 			return rc;
871 	}
872 
873 	if (strlen(bp->board_serialno)) {
874 		rc = devlink_info_board_serial_number_put(req, bp->board_serialno);
875 		if (rc)
876 			return rc;
877 	}
878 
879 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_FIXED,
880 			      DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
881 			      bp->board_partno);
882 	if (rc)
883 		return rc;
884 
885 	sprintf(buf, "%X", bp->chip_num);
886 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_FIXED,
887 			      DEVLINK_INFO_VERSION_GENERIC_ASIC_ID, buf);
888 	if (rc)
889 		return rc;
890 
891 	ver_resp = &bp->ver_resp;
892 	sprintf(buf, "%c%d", 'A' + ver_resp->chip_rev, ver_resp->chip_metal);
893 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_FIXED,
894 			      DEVLINK_INFO_VERSION_GENERIC_ASIC_REV, buf);
895 	if (rc)
896 		return rc;
897 
898 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
899 			      DEVLINK_INFO_VERSION_GENERIC_FW_PSID,
900 			      bp->nvm_cfg_ver);
901 	if (rc)
902 		return rc;
903 
904 	buf[0] = 0;
905 	strncat(buf, ver_resp->active_pkg_name, HWRM_FW_VER_STR_LEN);
906 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
907 			      DEVLINK_INFO_VERSION_GENERIC_FW, buf);
908 	if (rc)
909 		return rc;
910 
911 	if (BNXT_PF(bp) && !bnxt_hwrm_get_nvm_cfg_ver(bp, &ver)) {
912 		sprintf(buf, "%d.%d.%d", (ver >> 16) & 0xff, (ver >> 8) & 0xff,
913 			ver & 0xff);
914 		rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
915 				      DEVLINK_INFO_VERSION_GENERIC_FW_PSID,
916 				      buf);
917 		if (rc)
918 			return rc;
919 	}
920 
921 	if (ver_resp->flags & VER_GET_RESP_FLAGS_EXT_VER_AVAIL) {
922 		snprintf(mgmt_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
923 			 ver_resp->hwrm_fw_major, ver_resp->hwrm_fw_minor,
924 			 ver_resp->hwrm_fw_build, ver_resp->hwrm_fw_patch);
925 
926 		snprintf(ncsi_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
927 			 ver_resp->mgmt_fw_major, ver_resp->mgmt_fw_minor,
928 			 ver_resp->mgmt_fw_build, ver_resp->mgmt_fw_patch);
929 
930 		snprintf(roce_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
931 			 ver_resp->roce_fw_major, ver_resp->roce_fw_minor,
932 			 ver_resp->roce_fw_build, ver_resp->roce_fw_patch);
933 	} else {
934 		snprintf(mgmt_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
935 			 ver_resp->hwrm_fw_maj_8b, ver_resp->hwrm_fw_min_8b,
936 			 ver_resp->hwrm_fw_bld_8b, ver_resp->hwrm_fw_rsvd_8b);
937 
938 		snprintf(ncsi_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
939 			 ver_resp->mgmt_fw_maj_8b, ver_resp->mgmt_fw_min_8b,
940 			 ver_resp->mgmt_fw_bld_8b, ver_resp->mgmt_fw_rsvd_8b);
941 
942 		snprintf(roce_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
943 			 ver_resp->roce_fw_maj_8b, ver_resp->roce_fw_min_8b,
944 			 ver_resp->roce_fw_bld_8b, ver_resp->roce_fw_rsvd_8b);
945 	}
946 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
947 			      DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, mgmt_ver);
948 	if (rc)
949 		return rc;
950 
951 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
952 			      DEVLINK_INFO_VERSION_GENERIC_FW_MGMT_API,
953 			      bp->hwrm_ver_supp);
954 	if (rc)
955 		return rc;
956 
957 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
958 			      DEVLINK_INFO_VERSION_GENERIC_FW_NCSI, ncsi_ver);
959 	if (rc)
960 		return rc;
961 
962 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
963 			      DEVLINK_INFO_VERSION_GENERIC_FW_ROCE, roce_ver);
964 	if (rc)
965 		return rc;
966 
967 	rc = bnxt_hwrm_nvm_get_dev_info(bp, &nvm_dev_info);
968 	if (rc ||
969 	    !(nvm_dev_info.flags & NVM_GET_DEV_INFO_RESP_FLAGS_FW_VER_VALID)) {
970 		if (!bnxt_get_pkginfo(bp->dev, buf, sizeof(buf)))
971 			return bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
972 						DEVLINK_INFO_VERSION_GENERIC_FW,
973 						buf);
974 		return 0;
975 	}
976 
977 	buf[0] = 0;
978 	strncat(buf, nvm_dev_info.pkg_name, HWRM_FW_VER_STR_LEN);
979 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
980 			      DEVLINK_INFO_VERSION_GENERIC_FW, buf);
981 	if (rc)
982 		return rc;
983 
984 	snprintf(mgmt_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
985 		 nvm_dev_info.hwrm_fw_major, nvm_dev_info.hwrm_fw_minor,
986 		 nvm_dev_info.hwrm_fw_build, nvm_dev_info.hwrm_fw_patch);
987 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
988 			      DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, mgmt_ver);
989 	if (rc)
990 		return rc;
991 
992 	snprintf(ncsi_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
993 		 nvm_dev_info.mgmt_fw_major, nvm_dev_info.mgmt_fw_minor,
994 		 nvm_dev_info.mgmt_fw_build, nvm_dev_info.mgmt_fw_patch);
995 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
996 			      DEVLINK_INFO_VERSION_GENERIC_FW_NCSI, ncsi_ver);
997 	if (rc)
998 		return rc;
999 
1000 	snprintf(roce_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
1001 		 nvm_dev_info.roce_fw_major, nvm_dev_info.roce_fw_minor,
1002 		 nvm_dev_info.roce_fw_build, nvm_dev_info.roce_fw_patch);
1003 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
1004 			      DEVLINK_INFO_VERSION_GENERIC_FW_ROCE, roce_ver);
1005 	if (rc)
1006 		return rc;
1007 
1008 	if (BNXT_CHIP_P5_PLUS(bp)) {
1009 		rc = bnxt_dl_livepatch_info_put(bp, req, BNXT_FW_SRT_PATCH);
1010 		if (rc)
1011 			return rc;
1012 	}
1013 	return bnxt_dl_livepatch_info_put(bp, req, BNXT_FW_CRT_PATCH);
1014 
1015 }
1016 
1017 static int bnxt_hwrm_nvm_req(struct bnxt *bp, u32 param_id, void *msg,
1018 			     union devlink_param_value *val)
1019 {
1020 	struct hwrm_nvm_get_variable_input *req = msg;
1021 	struct bnxt_dl_nvm_param nvm_param;
1022 	struct hwrm_err_output *resp;
1023 	union bnxt_nvm_data *data;
1024 	dma_addr_t data_dma_addr;
1025 	int idx = 0, rc, i;
1026 
1027 	/* Get/Set NVM CFG parameter is supported only on PFs */
1028 	if (BNXT_VF(bp)) {
1029 		hwrm_req_drop(bp, req);
1030 		return -EPERM;
1031 	}
1032 
1033 	for (i = 0; i < ARRAY_SIZE(nvm_params); i++) {
1034 		if (nvm_params[i].id == param_id) {
1035 			nvm_param = nvm_params[i];
1036 			break;
1037 		}
1038 	}
1039 
1040 	if (i == ARRAY_SIZE(nvm_params)) {
1041 		hwrm_req_drop(bp, req);
1042 		return -EOPNOTSUPP;
1043 	}
1044 
1045 	if (nvm_param.dir_type == BNXT_NVM_PORT_CFG)
1046 		idx = bp->pf.port_id;
1047 	else if (nvm_param.dir_type == BNXT_NVM_FUNC_CFG)
1048 		idx = bp->pf.fw_fid - BNXT_FIRST_PF_FID;
1049 
1050 	data = hwrm_req_dma_slice(bp, req, sizeof(*data), &data_dma_addr);
1051 
1052 	if (!data) {
1053 		hwrm_req_drop(bp, req);
1054 		return -ENOMEM;
1055 	}
1056 
1057 	req->dest_data_addr = cpu_to_le64(data_dma_addr);
1058 	req->data_len = cpu_to_le16(nvm_param.nvm_num_bits);
1059 	req->option_num = cpu_to_le16(nvm_param.offset);
1060 	req->index_0 = cpu_to_le16(idx);
1061 	if (idx)
1062 		req->dimensions = cpu_to_le16(1);
1063 
1064 	resp = hwrm_req_hold(bp, req);
1065 	if (req->req_type == cpu_to_le16(HWRM_NVM_SET_VARIABLE)) {
1066 		bnxt_copy_to_nvm_data(data, val, nvm_param.nvm_num_bits,
1067 				      nvm_param.dl_num_bytes);
1068 		rc = hwrm_req_send(bp, msg);
1069 	} else {
1070 		rc = hwrm_req_send_silent(bp, msg);
1071 		if (!rc) {
1072 			bnxt_copy_from_nvm_data(val, data,
1073 						nvm_param.nvm_num_bits,
1074 						nvm_param.dl_num_bytes);
1075 		} else {
1076 			if (resp->cmd_err ==
1077 				NVM_GET_VARIABLE_CMD_ERR_CODE_VAR_NOT_EXIST)
1078 				rc = -EOPNOTSUPP;
1079 		}
1080 	}
1081 	hwrm_req_drop(bp, req);
1082 	if (rc == -EACCES)
1083 		netdev_err(bp->dev, "PF does not have admin privileges to modify NVM config\n");
1084 	return rc;
1085 }
1086 
1087 static int bnxt_dl_nvm_param_get(struct devlink *dl, u32 id,
1088 				 struct devlink_param_gset_ctx *ctx)
1089 {
1090 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
1091 	struct hwrm_nvm_get_variable_input *req;
1092 	int rc;
1093 
1094 	rc = hwrm_req_init(bp, req, HWRM_NVM_GET_VARIABLE);
1095 	if (rc)
1096 		return rc;
1097 
1098 	rc = bnxt_hwrm_nvm_req(bp, id, req, &ctx->val);
1099 	if (!rc && id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
1100 		ctx->val.vbool = !ctx->val.vbool;
1101 
1102 	return rc;
1103 }
1104 
1105 static int bnxt_dl_nvm_param_set(struct devlink *dl, u32 id,
1106 				 struct devlink_param_gset_ctx *ctx)
1107 {
1108 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
1109 	struct hwrm_nvm_set_variable_input *req;
1110 	int rc;
1111 
1112 	rc = hwrm_req_init(bp, req, HWRM_NVM_SET_VARIABLE);
1113 	if (rc)
1114 		return rc;
1115 
1116 	if (id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
1117 		ctx->val.vbool = !ctx->val.vbool;
1118 
1119 	return bnxt_hwrm_nvm_req(bp, id, req, &ctx->val);
1120 }
1121 
1122 static int bnxt_dl_msix_validate(struct devlink *dl, u32 id,
1123 				 union devlink_param_value val,
1124 				 struct netlink_ext_ack *extack)
1125 {
1126 	int max_val = -1;
1127 
1128 	if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX)
1129 		max_val = BNXT_MSIX_VEC_MAX;
1130 
1131 	if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN)
1132 		max_val = BNXT_MSIX_VEC_MIN_MAX;
1133 
1134 	if (val.vu32 > max_val) {
1135 		NL_SET_ERR_MSG_MOD(extack, "MSIX value is exceeding the range");
1136 		return -EINVAL;
1137 	}
1138 
1139 	return 0;
1140 }
1141 
1142 static int bnxt_remote_dev_reset_get(struct devlink *dl, u32 id,
1143 				     struct devlink_param_gset_ctx *ctx)
1144 {
1145 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
1146 
1147 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
1148 		return -EOPNOTSUPP;
1149 
1150 	ctx->val.vbool = bnxt_dl_get_remote_reset(dl);
1151 	return 0;
1152 }
1153 
1154 static int bnxt_remote_dev_reset_set(struct devlink *dl, u32 id,
1155 				     struct devlink_param_gset_ctx *ctx)
1156 {
1157 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
1158 	int rc;
1159 
1160 	rc = bnxt_hwrm_remote_dev_reset_set(bp, ctx->val.vbool);
1161 	if (rc)
1162 		return rc;
1163 
1164 	bnxt_dl_set_remote_reset(dl, ctx->val.vbool);
1165 	return rc;
1166 }
1167 
1168 static const struct devlink_param bnxt_dl_params[] = {
1169 	DEVLINK_PARAM_GENERIC(ENABLE_SRIOV,
1170 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1171 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1172 			      NULL),
1173 	DEVLINK_PARAM_GENERIC(IGNORE_ARI,
1174 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1175 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1176 			      NULL),
1177 	DEVLINK_PARAM_GENERIC(MSIX_VEC_PER_PF_MAX,
1178 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1179 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1180 			      bnxt_dl_msix_validate),
1181 	DEVLINK_PARAM_GENERIC(MSIX_VEC_PER_PF_MIN,
1182 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1183 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1184 			      bnxt_dl_msix_validate),
1185 	DEVLINK_PARAM_DRIVER(BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK,
1186 			     "gre_ver_check", DEVLINK_PARAM_TYPE_BOOL,
1187 			     BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1188 			     bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1189 			     NULL),
1190 	/* keep REMOTE_DEV_RESET last, it is excluded based on caps */
1191 	DEVLINK_PARAM_GENERIC(ENABLE_REMOTE_DEV_RESET,
1192 			      BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1193 			      bnxt_remote_dev_reset_get,
1194 			      bnxt_remote_dev_reset_set, NULL),
1195 };
1196 
1197 static int bnxt_dl_params_register(struct bnxt *bp)
1198 {
1199 	int num_params = ARRAY_SIZE(bnxt_dl_params);
1200 	int rc;
1201 
1202 	if (bp->hwrm_spec_code < 0x10600)
1203 		return 0;
1204 
1205 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
1206 		num_params--;
1207 
1208 	rc = devlink_params_register(bp->dl, bnxt_dl_params, num_params);
1209 	if (rc)
1210 		netdev_warn(bp->dev, "devlink_params_register failed. rc=%d\n",
1211 			    rc);
1212 	return rc;
1213 }
1214 
1215 static void bnxt_dl_params_unregister(struct bnxt *bp)
1216 {
1217 	int num_params = ARRAY_SIZE(bnxt_dl_params);
1218 
1219 	if (bp->hwrm_spec_code < 0x10600)
1220 		return;
1221 
1222 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
1223 		num_params--;
1224 
1225 	devlink_params_unregister(bp->dl, bnxt_dl_params, num_params);
1226 }
1227 
1228 int bnxt_dl_register(struct bnxt *bp)
1229 {
1230 	const struct devlink_ops *devlink_ops;
1231 	struct devlink_port_attrs attrs = {};
1232 	struct bnxt_dl *bp_dl;
1233 	struct devlink *dl;
1234 	int rc;
1235 
1236 	if (BNXT_PF(bp))
1237 		devlink_ops = &bnxt_dl_ops;
1238 	else
1239 		devlink_ops = &bnxt_vf_dl_ops;
1240 
1241 	dl = devlink_alloc(devlink_ops, sizeof(struct bnxt_dl), &bp->pdev->dev);
1242 	if (!dl) {
1243 		netdev_warn(bp->dev, "devlink_alloc failed\n");
1244 		return -ENOMEM;
1245 	}
1246 
1247 	bp->dl = dl;
1248 	bp_dl = devlink_priv(dl);
1249 	bp_dl->bp = bp;
1250 	bnxt_dl_set_remote_reset(dl, true);
1251 
1252 	/* Add switchdev eswitch mode setting, if SRIOV supported */
1253 	if (pci_find_ext_capability(bp->pdev, PCI_EXT_CAP_ID_SRIOV) &&
1254 	    bp->hwrm_spec_code > 0x10803)
1255 		bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
1256 
1257 	if (!BNXT_PF(bp))
1258 		goto out;
1259 
1260 	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
1261 	attrs.phys.port_number = bp->pf.port_id;
1262 	memcpy(attrs.switch_id.id, bp->dsn, sizeof(bp->dsn));
1263 	attrs.switch_id.id_len = sizeof(bp->dsn);
1264 	devlink_port_attrs_set(&bp->dl_port, &attrs);
1265 	rc = devlink_port_register(dl, &bp->dl_port, bp->pf.port_id);
1266 	if (rc) {
1267 		netdev_err(bp->dev, "devlink_port_register failed\n");
1268 		goto err_dl_free;
1269 	}
1270 
1271 	rc = bnxt_dl_params_register(bp);
1272 	if (rc)
1273 		goto err_dl_port_unreg;
1274 
1275 out:
1276 	devlink_register(dl);
1277 	return 0;
1278 
1279 err_dl_port_unreg:
1280 	devlink_port_unregister(&bp->dl_port);
1281 err_dl_free:
1282 	devlink_free(dl);
1283 	return rc;
1284 }
1285 
1286 void bnxt_dl_unregister(struct bnxt *bp)
1287 {
1288 	struct devlink *dl = bp->dl;
1289 
1290 	devlink_unregister(dl);
1291 	if (BNXT_PF(bp)) {
1292 		bnxt_dl_params_unregister(bp);
1293 		devlink_port_unregister(&bp->dl_port);
1294 	}
1295 	devlink_free(dl);
1296 }
1297