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