xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/broadcom/bnxt/bnxt_devlink.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* Broadcom NetXtreme-C/E network driver.
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Copyright (c) 2017 Broadcom Limited
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
6*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
7*4882a593Smuzhiyun  * the Free Software Foundation.
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/pci.h>
11*4882a593Smuzhiyun #include <linux/netdevice.h>
12*4882a593Smuzhiyun #include <net/devlink.h>
13*4882a593Smuzhiyun #include "bnxt_hsi.h"
14*4882a593Smuzhiyun #include "bnxt.h"
15*4882a593Smuzhiyun #include "bnxt_vfr.h"
16*4882a593Smuzhiyun #include "bnxt_devlink.h"
17*4882a593Smuzhiyun #include "bnxt_ethtool.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun static int
bnxt_dl_flash_update(struct devlink * dl,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack)20*4882a593Smuzhiyun bnxt_dl_flash_update(struct devlink *dl,
21*4882a593Smuzhiyun 		     struct devlink_flash_update_params *params,
22*4882a593Smuzhiyun 		     struct netlink_ext_ack *extack)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
25*4882a593Smuzhiyun 	int rc;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	if (!BNXT_PF(bp)) {
28*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(extack,
29*4882a593Smuzhiyun 				   "flash update not supported from a VF");
30*4882a593Smuzhiyun 		return -EPERM;
31*4882a593Smuzhiyun 	}
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	devlink_flash_update_begin_notify(dl);
34*4882a593Smuzhiyun 	devlink_flash_update_status_notify(dl, "Preparing to flash", NULL, 0, 0);
35*4882a593Smuzhiyun 	rc = bnxt_flash_package_from_file(bp->dev, params->file_name, 0);
36*4882a593Smuzhiyun 	if (!rc)
37*4882a593Smuzhiyun 		devlink_flash_update_status_notify(dl, "Flashing done", NULL, 0, 0);
38*4882a593Smuzhiyun 	else
39*4882a593Smuzhiyun 		devlink_flash_update_status_notify(dl, "Flashing failed", NULL, 0, 0);
40*4882a593Smuzhiyun 	devlink_flash_update_end_notify(dl);
41*4882a593Smuzhiyun 	return rc;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
bnxt_fw_reporter_diagnose(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,struct netlink_ext_ack * extack)44*4882a593Smuzhiyun static int bnxt_fw_reporter_diagnose(struct devlink_health_reporter *reporter,
45*4882a593Smuzhiyun 				     struct devlink_fmsg *fmsg,
46*4882a593Smuzhiyun 				     struct netlink_ext_ack *extack)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	struct bnxt *bp = devlink_health_reporter_priv(reporter);
49*4882a593Smuzhiyun 	u32 val, health_status;
50*4882a593Smuzhiyun 	int rc;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
53*4882a593Smuzhiyun 		return 0;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
56*4882a593Smuzhiyun 	health_status = val & 0xffff;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (health_status < BNXT_FW_STATUS_HEALTHY) {
59*4882a593Smuzhiyun 		rc = devlink_fmsg_string_pair_put(fmsg, "Description",
60*4882a593Smuzhiyun 						  "Not yet completed initialization");
61*4882a593Smuzhiyun 		if (rc)
62*4882a593Smuzhiyun 			return rc;
63*4882a593Smuzhiyun 	} else if (health_status > BNXT_FW_STATUS_HEALTHY) {
64*4882a593Smuzhiyun 		rc = devlink_fmsg_string_pair_put(fmsg, "Description",
65*4882a593Smuzhiyun 						  "Encountered fatal error and cannot recover");
66*4882a593Smuzhiyun 		if (rc)
67*4882a593Smuzhiyun 			return rc;
68*4882a593Smuzhiyun 	}
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	if (val >> 16) {
71*4882a593Smuzhiyun 		rc = devlink_fmsg_u32_pair_put(fmsg, "Error code", val >> 16);
72*4882a593Smuzhiyun 		if (rc)
73*4882a593Smuzhiyun 			return rc;
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
77*4882a593Smuzhiyun 	rc = devlink_fmsg_u32_pair_put(fmsg, "Reset count", val);
78*4882a593Smuzhiyun 	if (rc)
79*4882a593Smuzhiyun 		return rc;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	return 0;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun static const struct devlink_health_reporter_ops bnxt_dl_fw_reporter_ops = {
85*4882a593Smuzhiyun 	.name = "fw",
86*4882a593Smuzhiyun 	.diagnose = bnxt_fw_reporter_diagnose,
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun 
bnxt_fw_reset_recover(struct devlink_health_reporter * reporter,void * priv_ctx,struct netlink_ext_ack * extack)89*4882a593Smuzhiyun static int bnxt_fw_reset_recover(struct devlink_health_reporter *reporter,
90*4882a593Smuzhiyun 				 void *priv_ctx,
91*4882a593Smuzhiyun 				 struct netlink_ext_ack *extack)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	struct bnxt *bp = devlink_health_reporter_priv(reporter);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (!priv_ctx)
96*4882a593Smuzhiyun 		return -EOPNOTSUPP;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	bnxt_fw_reset(bp);
99*4882a593Smuzhiyun 	return -EINPROGRESS;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun static const
103*4882a593Smuzhiyun struct devlink_health_reporter_ops bnxt_dl_fw_reset_reporter_ops = {
104*4882a593Smuzhiyun 	.name = "fw_reset",
105*4882a593Smuzhiyun 	.recover = bnxt_fw_reset_recover,
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun 
bnxt_fw_fatal_recover(struct devlink_health_reporter * reporter,void * priv_ctx,struct netlink_ext_ack * extack)108*4882a593Smuzhiyun static int bnxt_fw_fatal_recover(struct devlink_health_reporter *reporter,
109*4882a593Smuzhiyun 				 void *priv_ctx,
110*4882a593Smuzhiyun 				 struct netlink_ext_ack *extack)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	struct bnxt *bp = devlink_health_reporter_priv(reporter);
113*4882a593Smuzhiyun 	struct bnxt_fw_reporter_ctx *fw_reporter_ctx = priv_ctx;
114*4882a593Smuzhiyun 	unsigned long event;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (!priv_ctx)
117*4882a593Smuzhiyun 		return -EOPNOTSUPP;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	bp->fw_health->fatal = true;
120*4882a593Smuzhiyun 	event = fw_reporter_ctx->sp_event;
121*4882a593Smuzhiyun 	if (event == BNXT_FW_RESET_NOTIFY_SP_EVENT)
122*4882a593Smuzhiyun 		bnxt_fw_reset(bp);
123*4882a593Smuzhiyun 	else if (event == BNXT_FW_EXCEPTION_SP_EVENT)
124*4882a593Smuzhiyun 		bnxt_fw_exception(bp);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	return -EINPROGRESS;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun static const
130*4882a593Smuzhiyun struct devlink_health_reporter_ops bnxt_dl_fw_fatal_reporter_ops = {
131*4882a593Smuzhiyun 	.name = "fw_fatal",
132*4882a593Smuzhiyun 	.recover = bnxt_fw_fatal_recover,
133*4882a593Smuzhiyun };
134*4882a593Smuzhiyun 
bnxt_dl_fw_reporters_create(struct bnxt * bp)135*4882a593Smuzhiyun void bnxt_dl_fw_reporters_create(struct bnxt *bp)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	struct bnxt_fw_health *health = bp->fw_health;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	if (!bp->dl || !health)
140*4882a593Smuzhiyun 		return;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) || health->fw_reset_reporter)
143*4882a593Smuzhiyun 		goto err_recovery;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	health->fw_reset_reporter =
146*4882a593Smuzhiyun 		devlink_health_reporter_create(bp->dl,
147*4882a593Smuzhiyun 					       &bnxt_dl_fw_reset_reporter_ops,
148*4882a593Smuzhiyun 					       0, bp);
149*4882a593Smuzhiyun 	if (IS_ERR(health->fw_reset_reporter)) {
150*4882a593Smuzhiyun 		netdev_warn(bp->dev, "Failed to create FW fatal health reporter, rc = %ld\n",
151*4882a593Smuzhiyun 			    PTR_ERR(health->fw_reset_reporter));
152*4882a593Smuzhiyun 		health->fw_reset_reporter = NULL;
153*4882a593Smuzhiyun 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun err_recovery:
157*4882a593Smuzhiyun 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
158*4882a593Smuzhiyun 		return;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	if (!health->fw_reporter) {
161*4882a593Smuzhiyun 		health->fw_reporter =
162*4882a593Smuzhiyun 			devlink_health_reporter_create(bp->dl,
163*4882a593Smuzhiyun 						       &bnxt_dl_fw_reporter_ops,
164*4882a593Smuzhiyun 						       0, bp);
165*4882a593Smuzhiyun 		if (IS_ERR(health->fw_reporter)) {
166*4882a593Smuzhiyun 			netdev_warn(bp->dev, "Failed to create FW health reporter, rc = %ld\n",
167*4882a593Smuzhiyun 				    PTR_ERR(health->fw_reporter));
168*4882a593Smuzhiyun 			health->fw_reporter = NULL;
169*4882a593Smuzhiyun 			bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
170*4882a593Smuzhiyun 			return;
171*4882a593Smuzhiyun 		}
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	if (health->fw_fatal_reporter)
175*4882a593Smuzhiyun 		return;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	health->fw_fatal_reporter =
178*4882a593Smuzhiyun 		devlink_health_reporter_create(bp->dl,
179*4882a593Smuzhiyun 					       &bnxt_dl_fw_fatal_reporter_ops,
180*4882a593Smuzhiyun 					       0, bp);
181*4882a593Smuzhiyun 	if (IS_ERR(health->fw_fatal_reporter)) {
182*4882a593Smuzhiyun 		netdev_warn(bp->dev, "Failed to create FW fatal health reporter, rc = %ld\n",
183*4882a593Smuzhiyun 			    PTR_ERR(health->fw_fatal_reporter));
184*4882a593Smuzhiyun 		health->fw_fatal_reporter = NULL;
185*4882a593Smuzhiyun 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
bnxt_dl_fw_reporters_destroy(struct bnxt * bp,bool all)189*4882a593Smuzhiyun void bnxt_dl_fw_reporters_destroy(struct bnxt *bp, bool all)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	struct bnxt_fw_health *health = bp->fw_health;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	if (!bp->dl || !health)
194*4882a593Smuzhiyun 		return;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	if ((all || !(bp->fw_cap & BNXT_FW_CAP_HOT_RESET)) &&
197*4882a593Smuzhiyun 	    health->fw_reset_reporter) {
198*4882a593Smuzhiyun 		devlink_health_reporter_destroy(health->fw_reset_reporter);
199*4882a593Smuzhiyun 		health->fw_reset_reporter = NULL;
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) && !all)
203*4882a593Smuzhiyun 		return;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	if (health->fw_reporter) {
206*4882a593Smuzhiyun 		devlink_health_reporter_destroy(health->fw_reporter);
207*4882a593Smuzhiyun 		health->fw_reporter = NULL;
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	if (health->fw_fatal_reporter) {
211*4882a593Smuzhiyun 		devlink_health_reporter_destroy(health->fw_fatal_reporter);
212*4882a593Smuzhiyun 		health->fw_fatal_reporter = NULL;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
bnxt_devlink_health_report(struct bnxt * bp,unsigned long event)216*4882a593Smuzhiyun void bnxt_devlink_health_report(struct bnxt *bp, unsigned long event)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	struct bnxt_fw_health *fw_health = bp->fw_health;
219*4882a593Smuzhiyun 	struct bnxt_fw_reporter_ctx fw_reporter_ctx;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	fw_reporter_ctx.sp_event = event;
222*4882a593Smuzhiyun 	switch (event) {
223*4882a593Smuzhiyun 	case BNXT_FW_RESET_NOTIFY_SP_EVENT:
224*4882a593Smuzhiyun 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
225*4882a593Smuzhiyun 			if (!fw_health->fw_fatal_reporter)
226*4882a593Smuzhiyun 				return;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 			devlink_health_report(fw_health->fw_fatal_reporter,
229*4882a593Smuzhiyun 					      "FW fatal async event received",
230*4882a593Smuzhiyun 					      &fw_reporter_ctx);
231*4882a593Smuzhiyun 			return;
232*4882a593Smuzhiyun 		}
233*4882a593Smuzhiyun 		if (!fw_health->fw_reset_reporter)
234*4882a593Smuzhiyun 			return;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 		devlink_health_report(fw_health->fw_reset_reporter,
237*4882a593Smuzhiyun 				      "FW non-fatal reset event received",
238*4882a593Smuzhiyun 				      &fw_reporter_ctx);
239*4882a593Smuzhiyun 		return;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	case BNXT_FW_EXCEPTION_SP_EVENT:
242*4882a593Smuzhiyun 		if (!fw_health->fw_fatal_reporter)
243*4882a593Smuzhiyun 			return;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 		devlink_health_report(fw_health->fw_fatal_reporter,
246*4882a593Smuzhiyun 				      "FW fatal error reported",
247*4882a593Smuzhiyun 				      &fw_reporter_ctx);
248*4882a593Smuzhiyun 		return;
249*4882a593Smuzhiyun 	}
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
bnxt_dl_health_status_update(struct bnxt * bp,bool healthy)252*4882a593Smuzhiyun void bnxt_dl_health_status_update(struct bnxt *bp, bool healthy)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	struct bnxt_fw_health *health = bp->fw_health;
255*4882a593Smuzhiyun 	u8 state;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	if (healthy)
258*4882a593Smuzhiyun 		state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
259*4882a593Smuzhiyun 	else
260*4882a593Smuzhiyun 		state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	if (health->fatal)
263*4882a593Smuzhiyun 		devlink_health_reporter_state_update(health->fw_fatal_reporter,
264*4882a593Smuzhiyun 						     state);
265*4882a593Smuzhiyun 	else
266*4882a593Smuzhiyun 		devlink_health_reporter_state_update(health->fw_reset_reporter,
267*4882a593Smuzhiyun 						     state);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	health->fatal = false;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
bnxt_dl_health_recovery_done(struct bnxt * bp)272*4882a593Smuzhiyun void bnxt_dl_health_recovery_done(struct bnxt *bp)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	struct bnxt_fw_health *hlth = bp->fw_health;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	if (hlth->fatal)
277*4882a593Smuzhiyun 		devlink_health_reporter_recovery_done(hlth->fw_fatal_reporter);
278*4882a593Smuzhiyun 	else
279*4882a593Smuzhiyun 		devlink_health_reporter_recovery_done(hlth->fw_reset_reporter);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun static int bnxt_dl_info_get(struct devlink *dl, struct devlink_info_req *req,
283*4882a593Smuzhiyun 			    struct netlink_ext_ack *extack);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun static const struct devlink_ops bnxt_dl_ops = {
286*4882a593Smuzhiyun #ifdef CONFIG_BNXT_SRIOV
287*4882a593Smuzhiyun 	.eswitch_mode_set = bnxt_dl_eswitch_mode_set,
288*4882a593Smuzhiyun 	.eswitch_mode_get = bnxt_dl_eswitch_mode_get,
289*4882a593Smuzhiyun #endif /* CONFIG_BNXT_SRIOV */
290*4882a593Smuzhiyun 	.info_get	  = bnxt_dl_info_get,
291*4882a593Smuzhiyun 	.flash_update	  = bnxt_dl_flash_update,
292*4882a593Smuzhiyun };
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun static const struct devlink_ops bnxt_vf_dl_ops;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun enum bnxt_dl_param_id {
297*4882a593Smuzhiyun 	BNXT_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
298*4882a593Smuzhiyun 	BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK,
299*4882a593Smuzhiyun };
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun static const struct bnxt_dl_nvm_param nvm_params[] = {
302*4882a593Smuzhiyun 	{DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV, NVM_OFF_ENABLE_SRIOV,
303*4882a593Smuzhiyun 	 BNXT_NVM_SHARED_CFG, 1, 1},
304*4882a593Smuzhiyun 	{DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI, NVM_OFF_IGNORE_ARI,
305*4882a593Smuzhiyun 	 BNXT_NVM_SHARED_CFG, 1, 1},
306*4882a593Smuzhiyun 	{DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
307*4882a593Smuzhiyun 	 NVM_OFF_MSIX_VEC_PER_PF_MAX, BNXT_NVM_SHARED_CFG, 10, 4},
308*4882a593Smuzhiyun 	{DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
309*4882a593Smuzhiyun 	 NVM_OFF_MSIX_VEC_PER_PF_MIN, BNXT_NVM_SHARED_CFG, 7, 4},
310*4882a593Smuzhiyun 	{BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK, NVM_OFF_DIS_GRE_VER_CHECK,
311*4882a593Smuzhiyun 	 BNXT_NVM_SHARED_CFG, 1, 1},
312*4882a593Smuzhiyun };
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun union bnxt_nvm_data {
315*4882a593Smuzhiyun 	u8	val8;
316*4882a593Smuzhiyun 	__le32	val32;
317*4882a593Smuzhiyun };
318*4882a593Smuzhiyun 
bnxt_copy_to_nvm_data(union bnxt_nvm_data * dst,union devlink_param_value * src,int nvm_num_bits,int dl_num_bytes)319*4882a593Smuzhiyun static void bnxt_copy_to_nvm_data(union bnxt_nvm_data *dst,
320*4882a593Smuzhiyun 				  union devlink_param_value *src,
321*4882a593Smuzhiyun 				  int nvm_num_bits, int dl_num_bytes)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun 	u32 val32 = 0;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (nvm_num_bits == 1) {
326*4882a593Smuzhiyun 		dst->val8 = src->vbool;
327*4882a593Smuzhiyun 		return;
328*4882a593Smuzhiyun 	}
329*4882a593Smuzhiyun 	if (dl_num_bytes == 4)
330*4882a593Smuzhiyun 		val32 = src->vu32;
331*4882a593Smuzhiyun 	else if (dl_num_bytes == 2)
332*4882a593Smuzhiyun 		val32 = (u32)src->vu16;
333*4882a593Smuzhiyun 	else if (dl_num_bytes == 1)
334*4882a593Smuzhiyun 		val32 = (u32)src->vu8;
335*4882a593Smuzhiyun 	dst->val32 = cpu_to_le32(val32);
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
bnxt_copy_from_nvm_data(union devlink_param_value * dst,union bnxt_nvm_data * src,int nvm_num_bits,int dl_num_bytes)338*4882a593Smuzhiyun static void bnxt_copy_from_nvm_data(union devlink_param_value *dst,
339*4882a593Smuzhiyun 				    union bnxt_nvm_data *src,
340*4882a593Smuzhiyun 				    int nvm_num_bits, int dl_num_bytes)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	u32 val32;
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	if (nvm_num_bits == 1) {
345*4882a593Smuzhiyun 		dst->vbool = src->val8;
346*4882a593Smuzhiyun 		return;
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 	val32 = le32_to_cpu(src->val32);
349*4882a593Smuzhiyun 	if (dl_num_bytes == 4)
350*4882a593Smuzhiyun 		dst->vu32 = val32;
351*4882a593Smuzhiyun 	else if (dl_num_bytes == 2)
352*4882a593Smuzhiyun 		dst->vu16 = (u16)val32;
353*4882a593Smuzhiyun 	else if (dl_num_bytes == 1)
354*4882a593Smuzhiyun 		dst->vu8 = (u8)val32;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
bnxt_hwrm_get_nvm_cfg_ver(struct bnxt * bp,union devlink_param_value * nvm_cfg_ver)357*4882a593Smuzhiyun static int bnxt_hwrm_get_nvm_cfg_ver(struct bnxt *bp,
358*4882a593Smuzhiyun 				     union devlink_param_value *nvm_cfg_ver)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	struct hwrm_nvm_get_variable_input req = {0};
361*4882a593Smuzhiyun 	union bnxt_nvm_data *data;
362*4882a593Smuzhiyun 	dma_addr_t data_dma_addr;
363*4882a593Smuzhiyun 	int rc;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_VARIABLE, -1, -1);
366*4882a593Smuzhiyun 	data = dma_alloc_coherent(&bp->pdev->dev, sizeof(*data),
367*4882a593Smuzhiyun 				  &data_dma_addr, GFP_KERNEL);
368*4882a593Smuzhiyun 	if (!data)
369*4882a593Smuzhiyun 		return -ENOMEM;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	req.dest_data_addr = cpu_to_le64(data_dma_addr);
372*4882a593Smuzhiyun 	req.data_len = cpu_to_le16(BNXT_NVM_CFG_VER_BITS);
373*4882a593Smuzhiyun 	req.option_num = cpu_to_le16(NVM_OFF_NVM_CFG_VER);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
376*4882a593Smuzhiyun 	if (!rc)
377*4882a593Smuzhiyun 		bnxt_copy_from_nvm_data(nvm_cfg_ver, data,
378*4882a593Smuzhiyun 					BNXT_NVM_CFG_VER_BITS,
379*4882a593Smuzhiyun 					BNXT_NVM_CFG_VER_BYTES);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	dma_free_coherent(&bp->pdev->dev, sizeof(*data), data, data_dma_addr);
382*4882a593Smuzhiyun 	return rc;
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun 
bnxt_dl_info_put(struct bnxt * bp,struct devlink_info_req * req,enum bnxt_dl_version_type type,const char * key,char * buf)385*4882a593Smuzhiyun static int bnxt_dl_info_put(struct bnxt *bp, struct devlink_info_req *req,
386*4882a593Smuzhiyun 			    enum bnxt_dl_version_type type, const char *key,
387*4882a593Smuzhiyun 			    char *buf)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	if (!strlen(buf))
390*4882a593Smuzhiyun 		return 0;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	if ((bp->flags & BNXT_FLAG_CHIP_P5) &&
393*4882a593Smuzhiyun 	    (!strcmp(key, DEVLINK_INFO_VERSION_GENERIC_FW_NCSI) ||
394*4882a593Smuzhiyun 	     !strcmp(key, DEVLINK_INFO_VERSION_GENERIC_FW_ROCE)))
395*4882a593Smuzhiyun 		return 0;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	switch (type) {
398*4882a593Smuzhiyun 	case BNXT_VERSION_FIXED:
399*4882a593Smuzhiyun 		return devlink_info_version_fixed_put(req, key, buf);
400*4882a593Smuzhiyun 	case BNXT_VERSION_RUNNING:
401*4882a593Smuzhiyun 		return devlink_info_version_running_put(req, key, buf);
402*4882a593Smuzhiyun 	case BNXT_VERSION_STORED:
403*4882a593Smuzhiyun 		return devlink_info_version_stored_put(req, key, buf);
404*4882a593Smuzhiyun 	}
405*4882a593Smuzhiyun 	return 0;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun #define HWRM_FW_VER_STR_LEN	16
409*4882a593Smuzhiyun 
bnxt_dl_info_get(struct devlink * dl,struct devlink_info_req * req,struct netlink_ext_ack * extack)410*4882a593Smuzhiyun static int bnxt_dl_info_get(struct devlink *dl, struct devlink_info_req *req,
411*4882a593Smuzhiyun 			    struct netlink_ext_ack *extack)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun 	struct hwrm_nvm_get_dev_info_output nvm_dev_info;
414*4882a593Smuzhiyun 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
415*4882a593Smuzhiyun 	union devlink_param_value nvm_cfg_ver;
416*4882a593Smuzhiyun 	struct hwrm_ver_get_output *ver_resp;
417*4882a593Smuzhiyun 	char mgmt_ver[FW_VER_STR_LEN];
418*4882a593Smuzhiyun 	char roce_ver[FW_VER_STR_LEN];
419*4882a593Smuzhiyun 	char ncsi_ver[FW_VER_STR_LEN];
420*4882a593Smuzhiyun 	char buf[32];
421*4882a593Smuzhiyun 	int rc;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	rc = devlink_info_driver_name_put(req, DRV_MODULE_NAME);
424*4882a593Smuzhiyun 	if (rc)
425*4882a593Smuzhiyun 		return rc;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	if (BNXT_PF(bp) && (bp->flags & BNXT_FLAG_DSN_VALID)) {
428*4882a593Smuzhiyun 		sprintf(buf, "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X",
429*4882a593Smuzhiyun 			bp->dsn[7], bp->dsn[6], bp->dsn[5], bp->dsn[4],
430*4882a593Smuzhiyun 			bp->dsn[3], bp->dsn[2], bp->dsn[1], bp->dsn[0]);
431*4882a593Smuzhiyun 		rc = devlink_info_serial_number_put(req, buf);
432*4882a593Smuzhiyun 		if (rc)
433*4882a593Smuzhiyun 			return rc;
434*4882a593Smuzhiyun 	}
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	if (strlen(bp->board_serialno)) {
437*4882a593Smuzhiyun 		rc = devlink_info_board_serial_number_put(req, bp->board_serialno);
438*4882a593Smuzhiyun 		if (rc)
439*4882a593Smuzhiyun 			return rc;
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_FIXED,
443*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
444*4882a593Smuzhiyun 			      bp->board_partno);
445*4882a593Smuzhiyun 	if (rc)
446*4882a593Smuzhiyun 		return rc;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	sprintf(buf, "%X", bp->chip_num);
449*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_FIXED,
450*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_ASIC_ID, buf);
451*4882a593Smuzhiyun 	if (rc)
452*4882a593Smuzhiyun 		return rc;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	ver_resp = &bp->ver_resp;
455*4882a593Smuzhiyun 	sprintf(buf, "%c%d", 'A' + ver_resp->chip_rev, ver_resp->chip_metal);
456*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_FIXED,
457*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_ASIC_REV, buf);
458*4882a593Smuzhiyun 	if (rc)
459*4882a593Smuzhiyun 		return rc;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
462*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_FW_PSID,
463*4882a593Smuzhiyun 			      bp->nvm_cfg_ver);
464*4882a593Smuzhiyun 	if (rc)
465*4882a593Smuzhiyun 		return rc;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	buf[0] = 0;
468*4882a593Smuzhiyun 	strncat(buf, ver_resp->active_pkg_name, HWRM_FW_VER_STR_LEN);
469*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
470*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_FW, buf);
471*4882a593Smuzhiyun 	if (rc)
472*4882a593Smuzhiyun 		return rc;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	if (BNXT_PF(bp) && !bnxt_hwrm_get_nvm_cfg_ver(bp, &nvm_cfg_ver)) {
475*4882a593Smuzhiyun 		u32 ver = nvm_cfg_ver.vu32;
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 		sprintf(buf, "%d.%d.%d", (ver >> 16) & 0xff, (ver >> 8) & 0xff,
478*4882a593Smuzhiyun 			ver & 0xff);
479*4882a593Smuzhiyun 		rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
480*4882a593Smuzhiyun 				      DEVLINK_INFO_VERSION_GENERIC_FW_PSID,
481*4882a593Smuzhiyun 				      buf);
482*4882a593Smuzhiyun 		if (rc)
483*4882a593Smuzhiyun 			return rc;
484*4882a593Smuzhiyun 	}
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	if (ver_resp->flags & VER_GET_RESP_FLAGS_EXT_VER_AVAIL) {
487*4882a593Smuzhiyun 		snprintf(mgmt_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
488*4882a593Smuzhiyun 			 ver_resp->hwrm_fw_major, ver_resp->hwrm_fw_minor,
489*4882a593Smuzhiyun 			 ver_resp->hwrm_fw_build, ver_resp->hwrm_fw_patch);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 		snprintf(ncsi_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
492*4882a593Smuzhiyun 			 ver_resp->mgmt_fw_major, ver_resp->mgmt_fw_minor,
493*4882a593Smuzhiyun 			 ver_resp->mgmt_fw_build, ver_resp->mgmt_fw_patch);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		snprintf(roce_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
496*4882a593Smuzhiyun 			 ver_resp->roce_fw_major, ver_resp->roce_fw_minor,
497*4882a593Smuzhiyun 			 ver_resp->roce_fw_build, ver_resp->roce_fw_patch);
498*4882a593Smuzhiyun 	} else {
499*4882a593Smuzhiyun 		snprintf(mgmt_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
500*4882a593Smuzhiyun 			 ver_resp->hwrm_fw_maj_8b, ver_resp->hwrm_fw_min_8b,
501*4882a593Smuzhiyun 			 ver_resp->hwrm_fw_bld_8b, ver_resp->hwrm_fw_rsvd_8b);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 		snprintf(ncsi_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
504*4882a593Smuzhiyun 			 ver_resp->mgmt_fw_maj_8b, ver_resp->mgmt_fw_min_8b,
505*4882a593Smuzhiyun 			 ver_resp->mgmt_fw_bld_8b, ver_resp->mgmt_fw_rsvd_8b);
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 		snprintf(roce_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
508*4882a593Smuzhiyun 			 ver_resp->roce_fw_maj_8b, ver_resp->roce_fw_min_8b,
509*4882a593Smuzhiyun 			 ver_resp->roce_fw_bld_8b, ver_resp->roce_fw_rsvd_8b);
510*4882a593Smuzhiyun 	}
511*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
512*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, mgmt_ver);
513*4882a593Smuzhiyun 	if (rc)
514*4882a593Smuzhiyun 		return rc;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
517*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_FW_MGMT_API,
518*4882a593Smuzhiyun 			      bp->hwrm_ver_supp);
519*4882a593Smuzhiyun 	if (rc)
520*4882a593Smuzhiyun 		return rc;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
523*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_FW_NCSI, ncsi_ver);
524*4882a593Smuzhiyun 	if (rc)
525*4882a593Smuzhiyun 		return rc;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_RUNNING,
528*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_FW_ROCE, roce_ver);
529*4882a593Smuzhiyun 	if (rc)
530*4882a593Smuzhiyun 		return rc;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	rc = bnxt_hwrm_nvm_get_dev_info(bp, &nvm_dev_info);
533*4882a593Smuzhiyun 	if (rc ||
534*4882a593Smuzhiyun 	    !(nvm_dev_info.flags & NVM_GET_DEV_INFO_RESP_FLAGS_FW_VER_VALID))
535*4882a593Smuzhiyun 		return 0;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	buf[0] = 0;
538*4882a593Smuzhiyun 	strncat(buf, nvm_dev_info.pkg_name, HWRM_FW_VER_STR_LEN);
539*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
540*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_FW, buf);
541*4882a593Smuzhiyun 	if (rc)
542*4882a593Smuzhiyun 		return rc;
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	snprintf(mgmt_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
545*4882a593Smuzhiyun 		 nvm_dev_info.hwrm_fw_major, nvm_dev_info.hwrm_fw_minor,
546*4882a593Smuzhiyun 		 nvm_dev_info.hwrm_fw_build, nvm_dev_info.hwrm_fw_patch);
547*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
548*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, mgmt_ver);
549*4882a593Smuzhiyun 	if (rc)
550*4882a593Smuzhiyun 		return rc;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	snprintf(ncsi_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
553*4882a593Smuzhiyun 		 nvm_dev_info.mgmt_fw_major, nvm_dev_info.mgmt_fw_minor,
554*4882a593Smuzhiyun 		 nvm_dev_info.mgmt_fw_build, nvm_dev_info.mgmt_fw_patch);
555*4882a593Smuzhiyun 	rc = bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
556*4882a593Smuzhiyun 			      DEVLINK_INFO_VERSION_GENERIC_FW_NCSI, ncsi_ver);
557*4882a593Smuzhiyun 	if (rc)
558*4882a593Smuzhiyun 		return rc;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	snprintf(roce_ver, FW_VER_STR_LEN, "%d.%d.%d.%d",
561*4882a593Smuzhiyun 		 nvm_dev_info.roce_fw_major, nvm_dev_info.roce_fw_minor,
562*4882a593Smuzhiyun 		 nvm_dev_info.roce_fw_build, nvm_dev_info.roce_fw_patch);
563*4882a593Smuzhiyun 	return bnxt_dl_info_put(bp, req, BNXT_VERSION_STORED,
564*4882a593Smuzhiyun 				DEVLINK_INFO_VERSION_GENERIC_FW_ROCE, roce_ver);
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun 
bnxt_hwrm_nvm_req(struct bnxt * bp,u32 param_id,void * msg,int msg_len,union devlink_param_value * val)567*4882a593Smuzhiyun static int bnxt_hwrm_nvm_req(struct bnxt *bp, u32 param_id, void *msg,
568*4882a593Smuzhiyun 			     int msg_len, union devlink_param_value *val)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	struct hwrm_nvm_get_variable_input *req = msg;
571*4882a593Smuzhiyun 	struct bnxt_dl_nvm_param nvm_param;
572*4882a593Smuzhiyun 	union bnxt_nvm_data *data;
573*4882a593Smuzhiyun 	dma_addr_t data_dma_addr;
574*4882a593Smuzhiyun 	int idx = 0, rc, i;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	/* Get/Set NVM CFG parameter is supported only on PFs */
577*4882a593Smuzhiyun 	if (BNXT_VF(bp))
578*4882a593Smuzhiyun 		return -EPERM;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(nvm_params); i++) {
581*4882a593Smuzhiyun 		if (nvm_params[i].id == param_id) {
582*4882a593Smuzhiyun 			nvm_param = nvm_params[i];
583*4882a593Smuzhiyun 			break;
584*4882a593Smuzhiyun 		}
585*4882a593Smuzhiyun 	}
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	if (i == ARRAY_SIZE(nvm_params))
588*4882a593Smuzhiyun 		return -EOPNOTSUPP;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	if (nvm_param.dir_type == BNXT_NVM_PORT_CFG)
591*4882a593Smuzhiyun 		idx = bp->pf.port_id;
592*4882a593Smuzhiyun 	else if (nvm_param.dir_type == BNXT_NVM_FUNC_CFG)
593*4882a593Smuzhiyun 		idx = bp->pf.fw_fid - BNXT_FIRST_PF_FID;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	data = dma_alloc_coherent(&bp->pdev->dev, sizeof(*data),
596*4882a593Smuzhiyun 				  &data_dma_addr, GFP_KERNEL);
597*4882a593Smuzhiyun 	if (!data)
598*4882a593Smuzhiyun 		return -ENOMEM;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	req->dest_data_addr = cpu_to_le64(data_dma_addr);
601*4882a593Smuzhiyun 	req->data_len = cpu_to_le16(nvm_param.nvm_num_bits);
602*4882a593Smuzhiyun 	req->option_num = cpu_to_le16(nvm_param.offset);
603*4882a593Smuzhiyun 	req->index_0 = cpu_to_le16(idx);
604*4882a593Smuzhiyun 	if (idx)
605*4882a593Smuzhiyun 		req->dimensions = cpu_to_le16(1);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	if (req->req_type == cpu_to_le16(HWRM_NVM_SET_VARIABLE)) {
608*4882a593Smuzhiyun 		bnxt_copy_to_nvm_data(data, val, nvm_param.nvm_num_bits,
609*4882a593Smuzhiyun 				      nvm_param.dl_num_bytes);
610*4882a593Smuzhiyun 		rc = hwrm_send_message(bp, msg, msg_len, HWRM_CMD_TIMEOUT);
611*4882a593Smuzhiyun 	} else {
612*4882a593Smuzhiyun 		rc = hwrm_send_message_silent(bp, msg, msg_len,
613*4882a593Smuzhiyun 					      HWRM_CMD_TIMEOUT);
614*4882a593Smuzhiyun 		if (!rc) {
615*4882a593Smuzhiyun 			bnxt_copy_from_nvm_data(val, data,
616*4882a593Smuzhiyun 						nvm_param.nvm_num_bits,
617*4882a593Smuzhiyun 						nvm_param.dl_num_bytes);
618*4882a593Smuzhiyun 		} else {
619*4882a593Smuzhiyun 			struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 			if (resp->cmd_err ==
622*4882a593Smuzhiyun 				NVM_GET_VARIABLE_CMD_ERR_CODE_VAR_NOT_EXIST)
623*4882a593Smuzhiyun 				rc = -EOPNOTSUPP;
624*4882a593Smuzhiyun 		}
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 	dma_free_coherent(&bp->pdev->dev, sizeof(*data), data, data_dma_addr);
627*4882a593Smuzhiyun 	if (rc == -EACCES)
628*4882a593Smuzhiyun 		netdev_err(bp->dev, "PF does not have admin privileges to modify NVM config\n");
629*4882a593Smuzhiyun 	return rc;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun 
bnxt_dl_nvm_param_get(struct devlink * dl,u32 id,struct devlink_param_gset_ctx * ctx)632*4882a593Smuzhiyun static int bnxt_dl_nvm_param_get(struct devlink *dl, u32 id,
633*4882a593Smuzhiyun 				 struct devlink_param_gset_ctx *ctx)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun 	struct hwrm_nvm_get_variable_input req = {0};
636*4882a593Smuzhiyun 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
637*4882a593Smuzhiyun 	int rc;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_VARIABLE, -1, -1);
640*4882a593Smuzhiyun 	rc = bnxt_hwrm_nvm_req(bp, id, &req, sizeof(req), &ctx->val);
641*4882a593Smuzhiyun 	if (!rc)
642*4882a593Smuzhiyun 		if (id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
643*4882a593Smuzhiyun 			ctx->val.vbool = !ctx->val.vbool;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	return rc;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun 
bnxt_dl_nvm_param_set(struct devlink * dl,u32 id,struct devlink_param_gset_ctx * ctx)648*4882a593Smuzhiyun static int bnxt_dl_nvm_param_set(struct devlink *dl, u32 id,
649*4882a593Smuzhiyun 				 struct devlink_param_gset_ctx *ctx)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun 	struct hwrm_nvm_set_variable_input req = {0};
652*4882a593Smuzhiyun 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_SET_VARIABLE, -1, -1);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
657*4882a593Smuzhiyun 		ctx->val.vbool = !ctx->val.vbool;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	return bnxt_hwrm_nvm_req(bp, id, &req, sizeof(req), &ctx->val);
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun 
bnxt_dl_msix_validate(struct devlink * dl,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)662*4882a593Smuzhiyun static int bnxt_dl_msix_validate(struct devlink *dl, u32 id,
663*4882a593Smuzhiyun 				 union devlink_param_value val,
664*4882a593Smuzhiyun 				 struct netlink_ext_ack *extack)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun 	int max_val = -1;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX)
669*4882a593Smuzhiyun 		max_val = BNXT_MSIX_VEC_MAX;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN)
672*4882a593Smuzhiyun 		max_val = BNXT_MSIX_VEC_MIN_MAX;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	if (val.vu32 > max_val) {
675*4882a593Smuzhiyun 		NL_SET_ERR_MSG_MOD(extack, "MSIX value is exceeding the range");
676*4882a593Smuzhiyun 		return -EINVAL;
677*4882a593Smuzhiyun 	}
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	return 0;
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun static const struct devlink_param bnxt_dl_params[] = {
683*4882a593Smuzhiyun 	DEVLINK_PARAM_GENERIC(ENABLE_SRIOV,
684*4882a593Smuzhiyun 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
685*4882a593Smuzhiyun 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
686*4882a593Smuzhiyun 			      NULL),
687*4882a593Smuzhiyun 	DEVLINK_PARAM_GENERIC(IGNORE_ARI,
688*4882a593Smuzhiyun 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
689*4882a593Smuzhiyun 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
690*4882a593Smuzhiyun 			      NULL),
691*4882a593Smuzhiyun 	DEVLINK_PARAM_GENERIC(MSIX_VEC_PER_PF_MAX,
692*4882a593Smuzhiyun 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
693*4882a593Smuzhiyun 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
694*4882a593Smuzhiyun 			      bnxt_dl_msix_validate),
695*4882a593Smuzhiyun 	DEVLINK_PARAM_GENERIC(MSIX_VEC_PER_PF_MIN,
696*4882a593Smuzhiyun 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
697*4882a593Smuzhiyun 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
698*4882a593Smuzhiyun 			      bnxt_dl_msix_validate),
699*4882a593Smuzhiyun 	DEVLINK_PARAM_DRIVER(BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK,
700*4882a593Smuzhiyun 			     "gre_ver_check", DEVLINK_PARAM_TYPE_BOOL,
701*4882a593Smuzhiyun 			     BIT(DEVLINK_PARAM_CMODE_PERMANENT),
702*4882a593Smuzhiyun 			     bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
703*4882a593Smuzhiyun 			     NULL),
704*4882a593Smuzhiyun };
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun static const struct devlink_param bnxt_dl_port_params[] = {
707*4882a593Smuzhiyun };
708*4882a593Smuzhiyun 
bnxt_dl_params_register(struct bnxt * bp)709*4882a593Smuzhiyun static int bnxt_dl_params_register(struct bnxt *bp)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun 	int rc;
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	if (bp->hwrm_spec_code < 0x10600)
714*4882a593Smuzhiyun 		return 0;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	rc = devlink_params_register(bp->dl, bnxt_dl_params,
717*4882a593Smuzhiyun 				     ARRAY_SIZE(bnxt_dl_params));
718*4882a593Smuzhiyun 	if (rc) {
719*4882a593Smuzhiyun 		netdev_warn(bp->dev, "devlink_params_register failed. rc=%d\n",
720*4882a593Smuzhiyun 			    rc);
721*4882a593Smuzhiyun 		return rc;
722*4882a593Smuzhiyun 	}
723*4882a593Smuzhiyun 	rc = devlink_port_params_register(&bp->dl_port, bnxt_dl_port_params,
724*4882a593Smuzhiyun 					  ARRAY_SIZE(bnxt_dl_port_params));
725*4882a593Smuzhiyun 	if (rc) {
726*4882a593Smuzhiyun 		netdev_err(bp->dev, "devlink_port_params_register failed\n");
727*4882a593Smuzhiyun 		devlink_params_unregister(bp->dl, bnxt_dl_params,
728*4882a593Smuzhiyun 					  ARRAY_SIZE(bnxt_dl_params));
729*4882a593Smuzhiyun 		return rc;
730*4882a593Smuzhiyun 	}
731*4882a593Smuzhiyun 	devlink_params_publish(bp->dl);
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	return 0;
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun 
bnxt_dl_params_unregister(struct bnxt * bp)736*4882a593Smuzhiyun static void bnxt_dl_params_unregister(struct bnxt *bp)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun 	if (bp->hwrm_spec_code < 0x10600)
739*4882a593Smuzhiyun 		return;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	devlink_params_unregister(bp->dl, bnxt_dl_params,
742*4882a593Smuzhiyun 				  ARRAY_SIZE(bnxt_dl_params));
743*4882a593Smuzhiyun 	devlink_port_params_unregister(&bp->dl_port, bnxt_dl_port_params,
744*4882a593Smuzhiyun 				       ARRAY_SIZE(bnxt_dl_port_params));
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun 
bnxt_dl_register(struct bnxt * bp)747*4882a593Smuzhiyun int bnxt_dl_register(struct bnxt *bp)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun 	struct devlink_port_attrs attrs = {};
750*4882a593Smuzhiyun 	struct devlink *dl;
751*4882a593Smuzhiyun 	int rc;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	if (BNXT_PF(bp))
754*4882a593Smuzhiyun 		dl = devlink_alloc(&bnxt_dl_ops, sizeof(struct bnxt_dl));
755*4882a593Smuzhiyun 	else
756*4882a593Smuzhiyun 		dl = devlink_alloc(&bnxt_vf_dl_ops, sizeof(struct bnxt_dl));
757*4882a593Smuzhiyun 	if (!dl) {
758*4882a593Smuzhiyun 		netdev_warn(bp->dev, "devlink_alloc failed\n");
759*4882a593Smuzhiyun 		return -ENOMEM;
760*4882a593Smuzhiyun 	}
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	bnxt_link_bp_to_dl(bp, dl);
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	/* Add switchdev eswitch mode setting, if SRIOV supported */
765*4882a593Smuzhiyun 	if (pci_find_ext_capability(bp->pdev, PCI_EXT_CAP_ID_SRIOV) &&
766*4882a593Smuzhiyun 	    bp->hwrm_spec_code > 0x10803)
767*4882a593Smuzhiyun 		bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	rc = devlink_register(dl, &bp->pdev->dev);
770*4882a593Smuzhiyun 	if (rc) {
771*4882a593Smuzhiyun 		netdev_warn(bp->dev, "devlink_register failed. rc=%d\n", rc);
772*4882a593Smuzhiyun 		goto err_dl_free;
773*4882a593Smuzhiyun 	}
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	if (!BNXT_PF(bp))
776*4882a593Smuzhiyun 		return 0;
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
779*4882a593Smuzhiyun 	attrs.phys.port_number = bp->pf.port_id;
780*4882a593Smuzhiyun 	memcpy(attrs.switch_id.id, bp->dsn, sizeof(bp->dsn));
781*4882a593Smuzhiyun 	attrs.switch_id.id_len = sizeof(bp->dsn);
782*4882a593Smuzhiyun 	devlink_port_attrs_set(&bp->dl_port, &attrs);
783*4882a593Smuzhiyun 	rc = devlink_port_register(dl, &bp->dl_port, bp->pf.port_id);
784*4882a593Smuzhiyun 	if (rc) {
785*4882a593Smuzhiyun 		netdev_err(bp->dev, "devlink_port_register failed\n");
786*4882a593Smuzhiyun 		goto err_dl_unreg;
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	rc = bnxt_dl_params_register(bp);
790*4882a593Smuzhiyun 	if (rc)
791*4882a593Smuzhiyun 		goto err_dl_port_unreg;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	return 0;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun err_dl_port_unreg:
796*4882a593Smuzhiyun 	devlink_port_unregister(&bp->dl_port);
797*4882a593Smuzhiyun err_dl_unreg:
798*4882a593Smuzhiyun 	devlink_unregister(dl);
799*4882a593Smuzhiyun err_dl_free:
800*4882a593Smuzhiyun 	bnxt_link_bp_to_dl(bp, NULL);
801*4882a593Smuzhiyun 	devlink_free(dl);
802*4882a593Smuzhiyun 	return rc;
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun 
bnxt_dl_unregister(struct bnxt * bp)805*4882a593Smuzhiyun void bnxt_dl_unregister(struct bnxt *bp)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun 	struct devlink *dl = bp->dl;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	if (!dl)
810*4882a593Smuzhiyun 		return;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	if (BNXT_PF(bp)) {
813*4882a593Smuzhiyun 		bnxt_dl_params_unregister(bp);
814*4882a593Smuzhiyun 		devlink_port_unregister(&bp->dl_port);
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 	devlink_unregister(dl);
817*4882a593Smuzhiyun 	devlink_free(dl);
818*4882a593Smuzhiyun }
819