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