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