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