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