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