xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt_devlink.c (revision 2aceb896ee18ae35b21b14c978d8c2ef8c7b439d)
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 		kfree(bp->ctx);
473 		bp->ctx = NULL;
474 		break;
475 	}
476 	case DEVLINK_RELOAD_ACTION_FW_ACTIVATE: {
477 		if (limit == DEVLINK_RELOAD_LIMIT_NO_RESET)
478 			return bnxt_dl_livepatch_activate(bp, extack);
479 		if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET) {
480 			NL_SET_ERR_MSG_MOD(extack, "Device not capable, requires reboot");
481 			return -EOPNOTSUPP;
482 		}
483 		if (!bnxt_hwrm_reset_permitted(bp)) {
484 			NL_SET_ERR_MSG_MOD(extack,
485 					   "Reset denied by firmware, it may be inhibited by remote driver");
486 			return -EPERM;
487 		}
488 		rtnl_lock();
489 		if (bp->dev->reg_state == NETREG_UNREGISTERED) {
490 			rtnl_unlock();
491 			return -ENODEV;
492 		}
493 		if (netif_running(bp->dev))
494 			set_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
495 		rc = bnxt_hwrm_firmware_reset(bp->dev,
496 					      FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP,
497 					      FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP,
498 					      FW_RESET_REQ_FLAGS_RESET_GRACEFUL |
499 					      FW_RESET_REQ_FLAGS_FW_ACTIVATION);
500 		if (rc) {
501 			NL_SET_ERR_MSG_MOD(extack, "Failed to activate firmware");
502 			clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
503 			rtnl_unlock();
504 		}
505 		break;
506 	}
507 	default:
508 		rc = -EOPNOTSUPP;
509 	}
510 
511 	return rc;
512 }
513 
514 static int bnxt_dl_reload_up(struct devlink *dl, enum devlink_reload_action action,
515 			     enum devlink_reload_limit limit, u32 *actions_performed,
516 			     struct netlink_ext_ack *extack)
517 {
518 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
519 	int rc = 0;
520 
521 	*actions_performed = 0;
522 	switch (action) {
523 	case DEVLINK_RELOAD_ACTION_DRIVER_REINIT: {
524 		bnxt_fw_init_one(bp);
525 		bnxt_vf_reps_alloc(bp);
526 		if (netif_running(bp->dev))
527 			rc = bnxt_open_nic(bp, true, true);
528 		bnxt_ulp_start(bp, rc);
529 		if (!rc) {
530 			bnxt_reenable_sriov(bp);
531 			bnxt_ptp_reapply_pps(bp);
532 		}
533 		break;
534 	}
535 	case DEVLINK_RELOAD_ACTION_FW_ACTIVATE: {
536 		unsigned long start = jiffies;
537 		unsigned long timeout = start + BNXT_DFLT_FW_RST_MAX_DSECS * HZ / 10;
538 
539 		if (limit == DEVLINK_RELOAD_LIMIT_NO_RESET)
540 			break;
541 		if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
542 			timeout = start + bp->fw_health->normal_func_wait_dsecs * HZ / 10;
543 		if (!netif_running(bp->dev))
544 			NL_SET_ERR_MSG_MOD(extack,
545 					   "Device is closed, not waiting for reset notice that will never come");
546 		rtnl_unlock();
547 		while (test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state)) {
548 			if (time_after(jiffies, timeout)) {
549 				NL_SET_ERR_MSG_MOD(extack, "Activation incomplete");
550 				rc = -ETIMEDOUT;
551 				break;
552 			}
553 			if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
554 				NL_SET_ERR_MSG_MOD(extack, "Activation aborted");
555 				rc = -ENODEV;
556 				break;
557 			}
558 			msleep(50);
559 		}
560 		rtnl_lock();
561 		if (!rc)
562 			*actions_performed |= BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
563 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
564 		break;
565 	}
566 	default:
567 		return -EOPNOTSUPP;
568 	}
569 
570 	if (!rc) {
571 		bnxt_print_device_info(bp);
572 		if (netif_running(bp->dev)) {
573 			mutex_lock(&bp->link_lock);
574 			bnxt_report_link(bp);
575 			mutex_unlock(&bp->link_lock);
576 		}
577 		*actions_performed |= BIT(action);
578 	} else if (netif_running(bp->dev)) {
579 		dev_close(bp->dev);
580 	}
581 	rtnl_unlock();
582 	return rc;
583 }
584 
585 static bool bnxt_nvm_test(struct bnxt *bp, struct netlink_ext_ack *extack)
586 {
587 	bool rc = false;
588 	u32 datalen;
589 	u16 index;
590 	u8 *buf;
591 
592 	if (bnxt_find_nvram_item(bp->dev, BNX_DIR_TYPE_VPD,
593 				 BNX_DIR_ORDINAL_FIRST, BNX_DIR_EXT_NONE,
594 				 &index, NULL, &datalen) || !datalen) {
595 		NL_SET_ERR_MSG_MOD(extack, "nvm test vpd entry error");
596 		return false;
597 	}
598 
599 	buf = kzalloc(datalen, GFP_KERNEL);
600 	if (!buf) {
601 		NL_SET_ERR_MSG_MOD(extack, "insufficient memory for nvm test");
602 		return false;
603 	}
604 
605 	if (bnxt_get_nvram_item(bp->dev, index, 0, datalen, buf)) {
606 		NL_SET_ERR_MSG_MOD(extack, "nvm test vpd read error");
607 		goto done;
608 	}
609 
610 	if (bnxt_flash_nvram(bp->dev, BNX_DIR_TYPE_VPD, BNX_DIR_ORDINAL_FIRST,
611 			     BNX_DIR_EXT_NONE, 0, 0, buf, datalen)) {
612 		NL_SET_ERR_MSG_MOD(extack, "nvm test vpd write error");
613 		goto done;
614 	}
615 
616 	rc = true;
617 
618 done:
619 	kfree(buf);
620 	return rc;
621 }
622 
623 static bool bnxt_dl_selftest_check(struct devlink *dl, unsigned int id,
624 				   struct netlink_ext_ack *extack)
625 {
626 	return id == DEVLINK_ATTR_SELFTEST_ID_FLASH;
627 }
628 
629 static enum devlink_selftest_status bnxt_dl_selftest_run(struct devlink *dl,
630 							 unsigned int id,
631 							 struct netlink_ext_ack *extack)
632 {
633 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
634 
635 	if (id == DEVLINK_ATTR_SELFTEST_ID_FLASH)
636 		return bnxt_nvm_test(bp, extack) ?
637 				DEVLINK_SELFTEST_STATUS_PASS :
638 				DEVLINK_SELFTEST_STATUS_FAIL;
639 
640 	return DEVLINK_SELFTEST_STATUS_SKIP;
641 }
642 
643 static const struct devlink_ops bnxt_dl_ops = {
644 #ifdef CONFIG_BNXT_SRIOV
645 	.eswitch_mode_set = bnxt_dl_eswitch_mode_set,
646 	.eswitch_mode_get = bnxt_dl_eswitch_mode_get,
647 #endif /* CONFIG_BNXT_SRIOV */
648 	.info_get	  = bnxt_dl_info_get,
649 	.flash_update	  = bnxt_dl_flash_update,
650 	.reload_actions	  = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
651 			    BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
652 	.reload_limits	  = BIT(DEVLINK_RELOAD_LIMIT_NO_RESET),
653 	.reload_down	  = bnxt_dl_reload_down,
654 	.reload_up	  = bnxt_dl_reload_up,
655 	.selftest_check	  = bnxt_dl_selftest_check,
656 	.selftest_run	  = bnxt_dl_selftest_run,
657 };
658 
659 static const struct devlink_ops bnxt_vf_dl_ops;
660 
661 enum bnxt_dl_param_id {
662 	BNXT_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
663 	BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK,
664 };
665 
666 static const struct bnxt_dl_nvm_param nvm_params[] = {
667 	{DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV, NVM_OFF_ENABLE_SRIOV,
668 	 BNXT_NVM_SHARED_CFG, 1, 1},
669 	{DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI, NVM_OFF_IGNORE_ARI,
670 	 BNXT_NVM_SHARED_CFG, 1, 1},
671 	{DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
672 	 NVM_OFF_MSIX_VEC_PER_PF_MAX, BNXT_NVM_SHARED_CFG, 10, 4},
673 	{DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
674 	 NVM_OFF_MSIX_VEC_PER_PF_MIN, BNXT_NVM_SHARED_CFG, 7, 4},
675 	{BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK, NVM_OFF_DIS_GRE_VER_CHECK,
676 	 BNXT_NVM_SHARED_CFG, 1, 1},
677 };
678 
679 union bnxt_nvm_data {
680 	u8	val8;
681 	__le32	val32;
682 };
683 
684 static void bnxt_copy_to_nvm_data(union bnxt_nvm_data *dst,
685 				  union devlink_param_value *src,
686 				  int nvm_num_bits, int dl_num_bytes)
687 {
688 	u32 val32 = 0;
689 
690 	if (nvm_num_bits == 1) {
691 		dst->val8 = src->vbool;
692 		return;
693 	}
694 	if (dl_num_bytes == 4)
695 		val32 = src->vu32;
696 	else if (dl_num_bytes == 2)
697 		val32 = (u32)src->vu16;
698 	else if (dl_num_bytes == 1)
699 		val32 = (u32)src->vu8;
700 	dst->val32 = cpu_to_le32(val32);
701 }
702 
703 static void bnxt_copy_from_nvm_data(union devlink_param_value *dst,
704 				    union bnxt_nvm_data *src,
705 				    int nvm_num_bits, int dl_num_bytes)
706 {
707 	u32 val32;
708 
709 	if (nvm_num_bits == 1) {
710 		dst->vbool = src->val8;
711 		return;
712 	}
713 	val32 = le32_to_cpu(src->val32);
714 	if (dl_num_bytes == 4)
715 		dst->vu32 = val32;
716 	else if (dl_num_bytes == 2)
717 		dst->vu16 = (u16)val32;
718 	else if (dl_num_bytes == 1)
719 		dst->vu8 = (u8)val32;
720 }
721 
722 static int bnxt_hwrm_get_nvm_cfg_ver(struct bnxt *bp, u32 *nvm_cfg_ver)
723 {
724 	struct hwrm_nvm_get_variable_input *req;
725 	u16 bytes = BNXT_NVM_CFG_VER_BYTES;
726 	u16 bits = BNXT_NVM_CFG_VER_BITS;
727 	union devlink_param_value ver;
728 	union bnxt_nvm_data *data;
729 	dma_addr_t data_dma_addr;
730 	int rc, i = 2;
731 	u16 dim = 1;
732 
733 	rc = hwrm_req_init(bp, req, HWRM_NVM_GET_VARIABLE);
734 	if (rc)
735 		return rc;
736 
737 	data = hwrm_req_dma_slice(bp, req, sizeof(*data), &data_dma_addr);
738 	if (!data) {
739 		rc = -ENOMEM;
740 		goto exit;
741 	}
742 
743 	/* earlier devices present as an array of raw bytes */
744 	if (!BNXT_CHIP_P5(bp)) {
745 		dim = 0;
746 		i = 0;
747 		bits *= 3;  /* array of 3 version components */
748 		bytes *= 4; /* copy whole word */
749 	}
750 
751 	hwrm_req_hold(bp, req);
752 	req->dest_data_addr = cpu_to_le64(data_dma_addr);
753 	req->data_len = cpu_to_le16(bits);
754 	req->option_num = cpu_to_le16(NVM_OFF_NVM_CFG_VER);
755 	req->dimensions = cpu_to_le16(dim);
756 
757 	while (i >= 0) {
758 		req->index_0 = cpu_to_le16(i--);
759 		rc = hwrm_req_send_silent(bp, req);
760 		if (rc)
761 			goto exit;
762 		bnxt_copy_from_nvm_data(&ver, data, bits, bytes);
763 
764 		if (BNXT_CHIP_P5(bp)) {
765 			*nvm_cfg_ver <<= 8;
766 			*nvm_cfg_ver |= ver.vu8;
767 		} else {
768 			*nvm_cfg_ver = ver.vu32;
769 		}
770 	}
771 
772 exit:
773 	hwrm_req_drop(bp, req);
774 	return rc;
775 }
776 
777 static int bnxt_dl_info_put(struct bnxt *bp, struct devlink_info_req *req,
778 			    enum bnxt_dl_version_type type, const char *key,
779 			    char *buf)
780 {
781 	if (!strlen(buf))
782 		return 0;
783 
784 	if ((bp->flags & BNXT_FLAG_CHIP_P5) &&
785 	    (!strcmp(key, DEVLINK_INFO_VERSION_GENERIC_FW_NCSI) ||
786 	     !strcmp(key, DEVLINK_INFO_VERSION_GENERIC_FW_ROCE)))
787 		return 0;
788 
789 	switch (type) {
790 	case BNXT_VERSION_FIXED:
791 		return devlink_info_version_fixed_put(req, key, buf);
792 	case BNXT_VERSION_RUNNING:
793 		return devlink_info_version_running_put(req, key, buf);
794 	case BNXT_VERSION_STORED:
795 		return devlink_info_version_stored_put(req, key, buf);
796 	}
797 	return 0;
798 }
799 
800 #define BNXT_FW_SRT_PATCH	"fw.srt.patch"
801 #define BNXT_FW_CRT_PATCH	"fw.crt.patch"
802 
803 static int bnxt_dl_livepatch_info_put(struct bnxt *bp,
804 				      struct devlink_info_req *req,
805 				      const char *key)
806 {
807 	struct hwrm_fw_livepatch_query_input *query;
808 	struct hwrm_fw_livepatch_query_output *resp;
809 	u16 flags;
810 	int rc;
811 
812 	if (~bp->fw_cap & BNXT_FW_CAP_LIVEPATCH)
813 		return 0;
814 
815 	rc = hwrm_req_init(bp, query, HWRM_FW_LIVEPATCH_QUERY);
816 	if (rc)
817 		return rc;
818 
819 	if (!strcmp(key, BNXT_FW_SRT_PATCH))
820 		query->fw_target = FW_LIVEPATCH_QUERY_REQ_FW_TARGET_SECURE_FW;
821 	else if (!strcmp(key, BNXT_FW_CRT_PATCH))
822 		query->fw_target = FW_LIVEPATCH_QUERY_REQ_FW_TARGET_COMMON_FW;
823 	else
824 		goto exit;
825 
826 	resp = hwrm_req_hold(bp, query);
827 	rc = hwrm_req_send(bp, query);
828 	if (rc)
829 		goto exit;
830 
831 	flags = le16_to_cpu(resp->status_flags);
832 	if (flags & FW_LIVEPATCH_QUERY_RESP_STATUS_FLAGS_ACTIVE) {
833 		resp->active_ver[sizeof(resp->active_ver) - 1] = '\0';
834 		rc = devlink_info_version_running_put(req, key, resp->active_ver);
835 		if (rc)
836 			goto exit;
837 	}
838 
839 	if (flags & FW_LIVEPATCH_QUERY_RESP_STATUS_FLAGS_INSTALL) {
840 		resp->install_ver[sizeof(resp->install_ver) - 1] = '\0';
841 		rc = devlink_info_version_stored_put(req, key, resp->install_ver);
842 		if (rc)
843 			goto exit;
844 	}
845 
846 exit:
847 	hwrm_req_drop(bp, query);
848 	return rc;
849 }
850 
851 #define HWRM_FW_VER_STR_LEN	16
852 
853 static int bnxt_dl_info_get(struct devlink *dl, struct devlink_info_req *req,
854 			    struct netlink_ext_ack *extack)
855 {
856 	struct hwrm_nvm_get_dev_info_output nvm_dev_info;
857 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
858 	struct hwrm_ver_get_output *ver_resp;
859 	char mgmt_ver[FW_VER_STR_LEN];
860 	char roce_ver[FW_VER_STR_LEN];
861 	char ncsi_ver[FW_VER_STR_LEN];
862 	char buf[32];
863 	u32 ver = 0;
864 	int rc;
865 
866 	if (BNXT_PF(bp) && (bp->flags & BNXT_FLAG_DSN_VALID)) {
867 		sprintf(buf, "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X",
868 			bp->dsn[7], bp->dsn[6], bp->dsn[5], bp->dsn[4],
869 			bp->dsn[3], bp->dsn[2], bp->dsn[1], bp->dsn[0]);
870 		rc = devlink_info_serial_number_put(req, buf);
871 		if (rc)
872 			return rc;
873 	}
874 
875 	if (strlen(bp->board_serialno)) {
876 		rc = devlink_info_board_serial_number_put(req, bp->board_serialno);
877 		if (rc)
878 			return rc;
879 	}
880 
881 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_FIXED,
882 			      DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
883 			      bp->board_partno);
884 	if (rc)
885 		return rc;
886 
887 	sprintf(buf, "%X", bp->chip_num);
888 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_FIXED,
889 			      DEVLINK_INFO_VERSION_GENERIC_ASIC_ID, buf);
890 	if (rc)
891 		return rc;
892 
893 	ver_resp = &bp->ver_resp;
894 	sprintf(buf, "%c%d", 'A' + ver_resp->chip_rev, ver_resp->chip_metal);
895 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_FIXED,
896 			      DEVLINK_INFO_VERSION_GENERIC_ASIC_REV, buf);
897 	if (rc)
898 		return rc;
899 
900 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
901 			      DEVLINK_INFO_VERSION_GENERIC_FW_PSID,
902 			      bp->nvm_cfg_ver);
903 	if (rc)
904 		return rc;
905 
906 	buf[0] = 0;
907 	strncat(buf, ver_resp->active_pkg_name, HWRM_FW_VER_STR_LEN);
908 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
909 			      DEVLINK_INFO_VERSION_GENERIC_FW, buf);
910 	if (rc)
911 		return rc;
912 
913 	if (BNXT_PF(bp) && !bnxt_hwrm_get_nvm_cfg_ver(bp, &ver)) {
914 		sprintf(buf, "%d.%d.%d", (ver >> 16) & 0xff, (ver >> 8) & 0xff,
915 			ver & 0xff);
916 		rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
917 				      DEVLINK_INFO_VERSION_GENERIC_FW_PSID,
918 				      buf);
919 		if (rc)
920 			return rc;
921 	}
922 
923 	if (ver_resp->flags & VER_GET_RESP_FLAGS_EXT_VER_AVAIL) {
924 		snprintf(mgmt_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
925 			 ver_resp->hwrm_fw_major, ver_resp->hwrm_fw_minor,
926 			 ver_resp->hwrm_fw_build, ver_resp->hwrm_fw_patch);
927 
928 		snprintf(ncsi_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
929 			 ver_resp->mgmt_fw_major, ver_resp->mgmt_fw_minor,
930 			 ver_resp->mgmt_fw_build, ver_resp->mgmt_fw_patch);
931 
932 		snprintf(roce_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
933 			 ver_resp->roce_fw_major, ver_resp->roce_fw_minor,
934 			 ver_resp->roce_fw_build, ver_resp->roce_fw_patch);
935 	} else {
936 		snprintf(mgmt_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
937 			 ver_resp->hwrm_fw_maj_8b, ver_resp->hwrm_fw_min_8b,
938 			 ver_resp->hwrm_fw_bld_8b, ver_resp->hwrm_fw_rsvd_8b);
939 
940 		snprintf(ncsi_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
941 			 ver_resp->mgmt_fw_maj_8b, ver_resp->mgmt_fw_min_8b,
942 			 ver_resp->mgmt_fw_bld_8b, ver_resp->mgmt_fw_rsvd_8b);
943 
944 		snprintf(roce_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
945 			 ver_resp->roce_fw_maj_8b, ver_resp->roce_fw_min_8b,
946 			 ver_resp->roce_fw_bld_8b, ver_resp->roce_fw_rsvd_8b);
947 	}
948 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
949 			      DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, mgmt_ver);
950 	if (rc)
951 		return rc;
952 
953 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
954 			      DEVLINK_INFO_VERSION_GENERIC_FW_MGMT_API,
955 			      bp->hwrm_ver_supp);
956 	if (rc)
957 		return rc;
958 
959 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
960 			      DEVLINK_INFO_VERSION_GENERIC_FW_NCSI, ncsi_ver);
961 	if (rc)
962 		return rc;
963 
964 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
965 			      DEVLINK_INFO_VERSION_GENERIC_FW_ROCE, roce_ver);
966 	if (rc)
967 		return rc;
968 
969 	rc = bnxt_hwrm_nvm_get_dev_info(bp, &nvm_dev_info);
970 	if (rc ||
971 	    !(nvm_dev_info.flags & NVM_GET_DEV_INFO_RESP_FLAGS_FW_VER_VALID)) {
972 		if (!bnxt_get_pkginfo(bp->dev, buf, sizeof(buf)))
973 			return bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
974 						DEVLINK_INFO_VERSION_GENERIC_FW,
975 						buf);
976 		return 0;
977 	}
978 
979 	buf[0] = 0;
980 	strncat(buf, nvm_dev_info.pkg_name, HWRM_FW_VER_STR_LEN);
981 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
982 			      DEVLINK_INFO_VERSION_GENERIC_FW, buf);
983 	if (rc)
984 		return rc;
985 
986 	snprintf(mgmt_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
987 		 nvm_dev_info.hwrm_fw_major, nvm_dev_info.hwrm_fw_minor,
988 		 nvm_dev_info.hwrm_fw_build, nvm_dev_info.hwrm_fw_patch);
989 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
990 			      DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, mgmt_ver);
991 	if (rc)
992 		return rc;
993 
994 	snprintf(ncsi_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
995 		 nvm_dev_info.mgmt_fw_major, nvm_dev_info.mgmt_fw_minor,
996 		 nvm_dev_info.mgmt_fw_build, nvm_dev_info.mgmt_fw_patch);
997 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
998 			      DEVLINK_INFO_VERSION_GENERIC_FW_NCSI, ncsi_ver);
999 	if (rc)
1000 		return rc;
1001 
1002 	snprintf(roce_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
1003 		 nvm_dev_info.roce_fw_major, nvm_dev_info.roce_fw_minor,
1004 		 nvm_dev_info.roce_fw_build, nvm_dev_info.roce_fw_patch);
1005 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
1006 			      DEVLINK_INFO_VERSION_GENERIC_FW_ROCE, roce_ver);
1007 	if (rc)
1008 		return rc;
1009 
1010 	if (BNXT_CHIP_P5(bp)) {
1011 		rc = bnxt_dl_livepatch_info_put(bp, req, BNXT_FW_SRT_PATCH);
1012 		if (rc)
1013 			return rc;
1014 	}
1015 	return bnxt_dl_livepatch_info_put(bp, req, BNXT_FW_CRT_PATCH);
1016 
1017 }
1018 
1019 static int bnxt_hwrm_nvm_req(struct bnxt *bp, u32 param_id, void *msg,
1020 			     union devlink_param_value *val)
1021 {
1022 	struct hwrm_nvm_get_variable_input *req = msg;
1023 	struct bnxt_dl_nvm_param nvm_param;
1024 	struct hwrm_err_output *resp;
1025 	union bnxt_nvm_data *data;
1026 	dma_addr_t data_dma_addr;
1027 	int idx = 0, rc, i;
1028 
1029 	/* Get/Set NVM CFG parameter is supported only on PFs */
1030 	if (BNXT_VF(bp)) {
1031 		hwrm_req_drop(bp, req);
1032 		return -EPERM;
1033 	}
1034 
1035 	for (i = 0; i < ARRAY_SIZE(nvm_params); i++) {
1036 		if (nvm_params[i].id == param_id) {
1037 			nvm_param = nvm_params[i];
1038 			break;
1039 		}
1040 	}
1041 
1042 	if (i == ARRAY_SIZE(nvm_params)) {
1043 		hwrm_req_drop(bp, req);
1044 		return -EOPNOTSUPP;
1045 	}
1046 
1047 	if (nvm_param.dir_type == BNXT_NVM_PORT_CFG)
1048 		idx = bp->pf.port_id;
1049 	else if (nvm_param.dir_type == BNXT_NVM_FUNC_CFG)
1050 		idx = bp->pf.fw_fid - BNXT_FIRST_PF_FID;
1051 
1052 	data = hwrm_req_dma_slice(bp, req, sizeof(*data), &data_dma_addr);
1053 
1054 	if (!data) {
1055 		hwrm_req_drop(bp, req);
1056 		return -ENOMEM;
1057 	}
1058 
1059 	req->dest_data_addr = cpu_to_le64(data_dma_addr);
1060 	req->data_len = cpu_to_le16(nvm_param.nvm_num_bits);
1061 	req->option_num = cpu_to_le16(nvm_param.offset);
1062 	req->index_0 = cpu_to_le16(idx);
1063 	if (idx)
1064 		req->dimensions = cpu_to_le16(1);
1065 
1066 	resp = hwrm_req_hold(bp, req);
1067 	if (req->req_type == cpu_to_le16(HWRM_NVM_SET_VARIABLE)) {
1068 		bnxt_copy_to_nvm_data(data, val, nvm_param.nvm_num_bits,
1069 				      nvm_param.dl_num_bytes);
1070 		rc = hwrm_req_send(bp, msg);
1071 	} else {
1072 		rc = hwrm_req_send_silent(bp, msg);
1073 		if (!rc) {
1074 			bnxt_copy_from_nvm_data(val, data,
1075 						nvm_param.nvm_num_bits,
1076 						nvm_param.dl_num_bytes);
1077 		} else {
1078 			if (resp->cmd_err ==
1079 				NVM_GET_VARIABLE_CMD_ERR_CODE_VAR_NOT_EXIST)
1080 				rc = -EOPNOTSUPP;
1081 		}
1082 	}
1083 	hwrm_req_drop(bp, req);
1084 	if (rc == -EACCES)
1085 		netdev_err(bp->dev, "PF does not have admin privileges to modify NVM config\n");
1086 	return rc;
1087 }
1088 
1089 static int bnxt_dl_nvm_param_get(struct devlink *dl, u32 id,
1090 				 struct devlink_param_gset_ctx *ctx)
1091 {
1092 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
1093 	struct hwrm_nvm_get_variable_input *req;
1094 	int rc;
1095 
1096 	rc = hwrm_req_init(bp, req, HWRM_NVM_GET_VARIABLE);
1097 	if (rc)
1098 		return rc;
1099 
1100 	rc = bnxt_hwrm_nvm_req(bp, id, req, &ctx->val);
1101 	if (!rc && id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
1102 		ctx->val.vbool = !ctx->val.vbool;
1103 
1104 	return rc;
1105 }
1106 
1107 static int bnxt_dl_nvm_param_set(struct devlink *dl, u32 id,
1108 				 struct devlink_param_gset_ctx *ctx)
1109 {
1110 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
1111 	struct hwrm_nvm_set_variable_input *req;
1112 	int rc;
1113 
1114 	rc = hwrm_req_init(bp, req, HWRM_NVM_SET_VARIABLE);
1115 	if (rc)
1116 		return rc;
1117 
1118 	if (id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
1119 		ctx->val.vbool = !ctx->val.vbool;
1120 
1121 	return bnxt_hwrm_nvm_req(bp, id, req, &ctx->val);
1122 }
1123 
1124 static int bnxt_dl_msix_validate(struct devlink *dl, u32 id,
1125 				 union devlink_param_value val,
1126 				 struct netlink_ext_ack *extack)
1127 {
1128 	int max_val = -1;
1129 
1130 	if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX)
1131 		max_val = BNXT_MSIX_VEC_MAX;
1132 
1133 	if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN)
1134 		max_val = BNXT_MSIX_VEC_MIN_MAX;
1135 
1136 	if (val.vu32 > max_val) {
1137 		NL_SET_ERR_MSG_MOD(extack, "MSIX value is exceeding the range");
1138 		return -EINVAL;
1139 	}
1140 
1141 	return 0;
1142 }
1143 
1144 static int bnxt_remote_dev_reset_get(struct devlink *dl, u32 id,
1145 				     struct devlink_param_gset_ctx *ctx)
1146 {
1147 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
1148 
1149 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
1150 		return -EOPNOTSUPP;
1151 
1152 	ctx->val.vbool = bnxt_dl_get_remote_reset(dl);
1153 	return 0;
1154 }
1155 
1156 static int bnxt_remote_dev_reset_set(struct devlink *dl, u32 id,
1157 				     struct devlink_param_gset_ctx *ctx)
1158 {
1159 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
1160 	int rc;
1161 
1162 	rc = bnxt_hwrm_remote_dev_reset_set(bp, ctx->val.vbool);
1163 	if (rc)
1164 		return rc;
1165 
1166 	bnxt_dl_set_remote_reset(dl, ctx->val.vbool);
1167 	return rc;
1168 }
1169 
1170 static const struct devlink_param bnxt_dl_params[] = {
1171 	DEVLINK_PARAM_GENERIC(ENABLE_SRIOV,
1172 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1173 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1174 			      NULL),
1175 	DEVLINK_PARAM_GENERIC(IGNORE_ARI,
1176 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1177 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1178 			      NULL),
1179 	DEVLINK_PARAM_GENERIC(MSIX_VEC_PER_PF_MAX,
1180 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1181 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1182 			      bnxt_dl_msix_validate),
1183 	DEVLINK_PARAM_GENERIC(MSIX_VEC_PER_PF_MIN,
1184 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1185 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1186 			      bnxt_dl_msix_validate),
1187 	DEVLINK_PARAM_DRIVER(BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK,
1188 			     "gre_ver_check", DEVLINK_PARAM_TYPE_BOOL,
1189 			     BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1190 			     bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
1191 			     NULL),
1192 	/* keep REMOTE_DEV_RESET last, it is excluded based on caps */
1193 	DEVLINK_PARAM_GENERIC(ENABLE_REMOTE_DEV_RESET,
1194 			      BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1195 			      bnxt_remote_dev_reset_get,
1196 			      bnxt_remote_dev_reset_set, NULL),
1197 };
1198 
1199 static int bnxt_dl_params_register(struct bnxt *bp)
1200 {
1201 	int num_params = ARRAY_SIZE(bnxt_dl_params);
1202 	int rc;
1203 
1204 	if (bp->hwrm_spec_code < 0x10600)
1205 		return 0;
1206 
1207 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
1208 		num_params--;
1209 
1210 	rc = devlink_params_register(bp->dl, bnxt_dl_params, num_params);
1211 	if (rc)
1212 		netdev_warn(bp->dev, "devlink_params_register failed. rc=%d\n",
1213 			    rc);
1214 	return rc;
1215 }
1216 
1217 static void bnxt_dl_params_unregister(struct bnxt *bp)
1218 {
1219 	int num_params = ARRAY_SIZE(bnxt_dl_params);
1220 
1221 	if (bp->hwrm_spec_code < 0x10600)
1222 		return;
1223 
1224 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
1225 		num_params--;
1226 
1227 	devlink_params_unregister(bp->dl, bnxt_dl_params, num_params);
1228 }
1229 
1230 int bnxt_dl_register(struct bnxt *bp)
1231 {
1232 	const struct devlink_ops *devlink_ops;
1233 	struct devlink_port_attrs attrs = {};
1234 	struct bnxt_dl *bp_dl;
1235 	struct devlink *dl;
1236 	int rc;
1237 
1238 	if (BNXT_PF(bp))
1239 		devlink_ops = &bnxt_dl_ops;
1240 	else
1241 		devlink_ops = &bnxt_vf_dl_ops;
1242 
1243 	dl = devlink_alloc(devlink_ops, sizeof(struct bnxt_dl), &bp->pdev->dev);
1244 	if (!dl) {
1245 		netdev_warn(bp->dev, "devlink_alloc failed\n");
1246 		return -ENOMEM;
1247 	}
1248 
1249 	bp->dl = dl;
1250 	bp_dl = devlink_priv(dl);
1251 	bp_dl->bp = bp;
1252 	bnxt_dl_set_remote_reset(dl, true);
1253 
1254 	/* Add switchdev eswitch mode setting, if SRIOV supported */
1255 	if (pci_find_ext_capability(bp->pdev, PCI_EXT_CAP_ID_SRIOV) &&
1256 	    bp->hwrm_spec_code > 0x10803)
1257 		bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
1258 
1259 	if (!BNXT_PF(bp))
1260 		goto out;
1261 
1262 	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
1263 	attrs.phys.port_number = bp->pf.port_id;
1264 	memcpy(attrs.switch_id.id, bp->dsn, sizeof(bp->dsn));
1265 	attrs.switch_id.id_len = sizeof(bp->dsn);
1266 	devlink_port_attrs_set(&bp->dl_port, &attrs);
1267 	rc = devlink_port_register(dl, &bp->dl_port, bp->pf.port_id);
1268 	if (rc) {
1269 		netdev_err(bp->dev, "devlink_port_register failed\n");
1270 		goto err_dl_free;
1271 	}
1272 
1273 	rc = bnxt_dl_params_register(bp);
1274 	if (rc)
1275 		goto err_dl_port_unreg;
1276 
1277 out:
1278 	devlink_register(dl);
1279 	return 0;
1280 
1281 err_dl_port_unreg:
1282 	devlink_port_unregister(&bp->dl_port);
1283 err_dl_free:
1284 	devlink_free(dl);
1285 	return rc;
1286 }
1287 
1288 void bnxt_dl_unregister(struct bnxt *bp)
1289 {
1290 	struct devlink *dl = bp->dl;
1291 
1292 	devlink_unregister(dl);
1293 	if (BNXT_PF(bp)) {
1294 		bnxt_dl_params_unregister(bp);
1295 		devlink_port_unregister(&bp->dl_port);
1296 	}
1297 	devlink_free(dl);
1298 }
1299