xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt_devlink.c (revision 56fb34d86e875dbb0d3e6a81c5d3d035db373031)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2017 Broadcom Limited
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/pci.h>
11 #include <linux/netdevice.h>
12 #include <net/devlink.h>
13 #include "bnxt_hsi.h"
14 #include "bnxt.h"
15 #include "bnxt_vfr.h"
16 #include "bnxt_devlink.h"
17 
18 static int bnxt_fw_reporter_diagnose(struct devlink_health_reporter *reporter,
19 				     struct devlink_fmsg *fmsg)
20 {
21 	struct bnxt *bp = devlink_health_reporter_priv(reporter);
22 	struct bnxt_fw_health *health = bp->fw_health;
23 	u32 val, health_status;
24 	int rc;
25 
26 	if (!health || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
27 		return 0;
28 
29 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
30 	health_status = val & 0xffff;
31 
32 	if (health_status == BNXT_FW_STATUS_HEALTHY) {
33 		rc = devlink_fmsg_string_pair_put(fmsg, "FW status",
34 						  "Healthy;");
35 		if (rc)
36 			return rc;
37 	} else if (health_status < BNXT_FW_STATUS_HEALTHY) {
38 		rc = devlink_fmsg_string_pair_put(fmsg, "FW status",
39 						  "Not yet completed initialization;");
40 		if (rc)
41 			return rc;
42 	} else if (health_status > BNXT_FW_STATUS_HEALTHY) {
43 		rc = devlink_fmsg_string_pair_put(fmsg, "FW status",
44 						  "Encountered fatal error and cannot recover;");
45 		if (rc)
46 			return rc;
47 	}
48 
49 	if (val >> 16) {
50 		rc = devlink_fmsg_u32_pair_put(fmsg, "Error", val >> 16);
51 		if (rc)
52 			return rc;
53 	}
54 
55 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
56 	rc = devlink_fmsg_u32_pair_put(fmsg, "Reset count", val);
57 	if (rc)
58 		return rc;
59 
60 	return 0;
61 }
62 
63 static const struct devlink_health_reporter_ops bnxt_dl_fw_reporter_ops = {
64 	.name = "fw",
65 	.diagnose = bnxt_fw_reporter_diagnose,
66 };
67 
68 static int bnxt_fw_reset_recover(struct devlink_health_reporter *reporter,
69 				 void *priv_ctx)
70 {
71 	struct bnxt *bp = devlink_health_reporter_priv(reporter);
72 
73 	if (!priv_ctx)
74 		return -EOPNOTSUPP;
75 
76 	bnxt_fw_reset(bp);
77 	return 0;
78 }
79 
80 static const
81 struct devlink_health_reporter_ops bnxt_dl_fw_reset_reporter_ops = {
82 	.name = "fw_reset",
83 	.recover = bnxt_fw_reset_recover,
84 };
85 
86 static int bnxt_fw_fatal_recover(struct devlink_health_reporter *reporter,
87 				 void *priv_ctx)
88 {
89 	struct bnxt *bp = devlink_health_reporter_priv(reporter);
90 	struct bnxt_fw_reporter_ctx *fw_reporter_ctx = priv_ctx;
91 	unsigned long event;
92 
93 	if (!priv_ctx)
94 		return -EOPNOTSUPP;
95 
96 	event = fw_reporter_ctx->sp_event;
97 	if (event == BNXT_FW_RESET_NOTIFY_SP_EVENT)
98 		bnxt_fw_reset(bp);
99 	else if (event == BNXT_FW_EXCEPTION_SP_EVENT)
100 		bnxt_fw_exception(bp);
101 
102 	return 0;
103 }
104 
105 static const
106 struct devlink_health_reporter_ops bnxt_dl_fw_fatal_reporter_ops = {
107 	.name = "fw_fatal",
108 	.recover = bnxt_fw_fatal_recover,
109 };
110 
111 static void bnxt_dl_fw_reporters_create(struct bnxt *bp)
112 {
113 	struct bnxt_fw_health *health = bp->fw_health;
114 
115 	if (!health)
116 		return;
117 
118 	health->fw_reporter =
119 		devlink_health_reporter_create(bp->dl, &bnxt_dl_fw_reporter_ops,
120 					       0, false, bp);
121 	if (IS_ERR(health->fw_reporter)) {
122 		netdev_warn(bp->dev, "Failed to create FW health reporter, rc = %ld\n",
123 			    PTR_ERR(health->fw_reporter));
124 		health->fw_reporter = NULL;
125 	}
126 
127 	health->fw_reset_reporter =
128 		devlink_health_reporter_create(bp->dl,
129 					       &bnxt_dl_fw_reset_reporter_ops,
130 					       0, true, bp);
131 	if (IS_ERR(health->fw_reset_reporter)) {
132 		netdev_warn(bp->dev, "Failed to create FW fatal health reporter, rc = %ld\n",
133 			    PTR_ERR(health->fw_reset_reporter));
134 		health->fw_reset_reporter = NULL;
135 	}
136 
137 	health->fw_fatal_reporter =
138 		devlink_health_reporter_create(bp->dl,
139 					       &bnxt_dl_fw_fatal_reporter_ops,
140 					       0, true, bp);
141 	if (IS_ERR(health->fw_fatal_reporter)) {
142 		netdev_warn(bp->dev, "Failed to create FW fatal health reporter, rc = %ld\n",
143 			    PTR_ERR(health->fw_fatal_reporter));
144 		health->fw_fatal_reporter = NULL;
145 	}
146 }
147 
148 static void bnxt_dl_fw_reporters_destroy(struct bnxt *bp)
149 {
150 	struct bnxt_fw_health *health = bp->fw_health;
151 
152 	if (!health)
153 		return;
154 
155 	if (health->fw_reporter)
156 		devlink_health_reporter_destroy(health->fw_reporter);
157 
158 	if (health->fw_reset_reporter)
159 		devlink_health_reporter_destroy(health->fw_reset_reporter);
160 
161 	if (health->fw_fatal_reporter)
162 		devlink_health_reporter_destroy(health->fw_fatal_reporter);
163 }
164 
165 void bnxt_devlink_health_report(struct bnxt *bp, unsigned long event)
166 {
167 	struct bnxt_fw_health *fw_health = bp->fw_health;
168 	struct bnxt_fw_reporter_ctx fw_reporter_ctx;
169 
170 	if (!fw_health)
171 		return;
172 
173 	fw_reporter_ctx.sp_event = event;
174 	switch (event) {
175 	case BNXT_FW_RESET_NOTIFY_SP_EVENT:
176 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
177 			if (!fw_health->fw_fatal_reporter)
178 				return;
179 
180 			devlink_health_report(fw_health->fw_fatal_reporter,
181 					      "FW fatal async event received",
182 					      &fw_reporter_ctx);
183 			return;
184 		}
185 		if (!fw_health->fw_reset_reporter)
186 			return;
187 
188 		devlink_health_report(fw_health->fw_reset_reporter,
189 				      "FW non-fatal reset event received",
190 				      &fw_reporter_ctx);
191 		return;
192 
193 	case BNXT_FW_EXCEPTION_SP_EVENT:
194 		if (!fw_health->fw_fatal_reporter)
195 			return;
196 
197 		devlink_health_report(fw_health->fw_fatal_reporter,
198 				      "FW fatal error reported",
199 				      &fw_reporter_ctx);
200 		return;
201 	}
202 }
203 
204 static const struct devlink_ops bnxt_dl_ops = {
205 #ifdef CONFIG_BNXT_SRIOV
206 	.eswitch_mode_set = bnxt_dl_eswitch_mode_set,
207 	.eswitch_mode_get = bnxt_dl_eswitch_mode_get,
208 #endif /* CONFIG_BNXT_SRIOV */
209 };
210 
211 enum bnxt_dl_param_id {
212 	BNXT_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
213 	BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK,
214 };
215 
216 static const struct bnxt_dl_nvm_param nvm_params[] = {
217 	{DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV, NVM_OFF_ENABLE_SRIOV,
218 	 BNXT_NVM_SHARED_CFG, 1},
219 	{DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI, NVM_OFF_IGNORE_ARI,
220 	 BNXT_NVM_SHARED_CFG, 1},
221 	{DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
222 	 NVM_OFF_MSIX_VEC_PER_PF_MAX, BNXT_NVM_SHARED_CFG, 10},
223 	{DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
224 	 NVM_OFF_MSIX_VEC_PER_PF_MIN, BNXT_NVM_SHARED_CFG, 7},
225 	{BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK, NVM_OFF_DIS_GRE_VER_CHECK,
226 	 BNXT_NVM_SHARED_CFG, 1},
227 };
228 
229 static int bnxt_hwrm_nvm_req(struct bnxt *bp, u32 param_id, void *msg,
230 			     int msg_len, union devlink_param_value *val)
231 {
232 	struct hwrm_nvm_get_variable_input *req = msg;
233 	void *data_addr = NULL, *buf = NULL;
234 	struct bnxt_dl_nvm_param nvm_param;
235 	int bytesize, idx = 0, rc, i;
236 	dma_addr_t data_dma_addr;
237 
238 	/* Get/Set NVM CFG parameter is supported only on PFs */
239 	if (BNXT_VF(bp))
240 		return -EPERM;
241 
242 	for (i = 0; i < ARRAY_SIZE(nvm_params); i++) {
243 		if (nvm_params[i].id == param_id) {
244 			nvm_param = nvm_params[i];
245 			break;
246 		}
247 	}
248 
249 	if (i == ARRAY_SIZE(nvm_params))
250 		return -EOPNOTSUPP;
251 
252 	if (nvm_param.dir_type == BNXT_NVM_PORT_CFG)
253 		idx = bp->pf.port_id;
254 	else if (nvm_param.dir_type == BNXT_NVM_FUNC_CFG)
255 		idx = bp->pf.fw_fid - BNXT_FIRST_PF_FID;
256 
257 	bytesize = roundup(nvm_param.num_bits, BITS_PER_BYTE) / BITS_PER_BYTE;
258 	switch (bytesize) {
259 	case 1:
260 		if (nvm_param.num_bits == 1)
261 			buf = &val->vbool;
262 		else
263 			buf = &val->vu8;
264 		break;
265 	case 2:
266 		buf = &val->vu16;
267 		break;
268 	case 4:
269 		buf = &val->vu32;
270 		break;
271 	default:
272 		return -EFAULT;
273 	}
274 
275 	data_addr = dma_alloc_coherent(&bp->pdev->dev, bytesize,
276 				       &data_dma_addr, GFP_KERNEL);
277 	if (!data_addr)
278 		return -ENOMEM;
279 
280 	req->dest_data_addr = cpu_to_le64(data_dma_addr);
281 	req->data_len = cpu_to_le16(nvm_param.num_bits);
282 	req->option_num = cpu_to_le16(nvm_param.offset);
283 	req->index_0 = cpu_to_le16(idx);
284 	if (idx)
285 		req->dimensions = cpu_to_le16(1);
286 
287 	if (req->req_type == cpu_to_le16(HWRM_NVM_SET_VARIABLE)) {
288 		memcpy(data_addr, buf, bytesize);
289 		rc = hwrm_send_message(bp, msg, msg_len, HWRM_CMD_TIMEOUT);
290 	} else {
291 		rc = hwrm_send_message_silent(bp, msg, msg_len,
292 					      HWRM_CMD_TIMEOUT);
293 	}
294 	if (!rc && req->req_type == cpu_to_le16(HWRM_NVM_GET_VARIABLE))
295 		memcpy(buf, data_addr, bytesize);
296 
297 	dma_free_coherent(&bp->pdev->dev, bytesize, data_addr, data_dma_addr);
298 	if (rc == -EACCES)
299 		netdev_err(bp->dev, "PF does not have admin privileges to modify NVM config\n");
300 	return rc;
301 }
302 
303 static int bnxt_dl_nvm_param_get(struct devlink *dl, u32 id,
304 				 struct devlink_param_gset_ctx *ctx)
305 {
306 	struct hwrm_nvm_get_variable_input req = {0};
307 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
308 	int rc;
309 
310 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_VARIABLE, -1, -1);
311 	rc = bnxt_hwrm_nvm_req(bp, id, &req, sizeof(req), &ctx->val);
312 	if (!rc)
313 		if (id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
314 			ctx->val.vbool = !ctx->val.vbool;
315 
316 	return rc;
317 }
318 
319 static int bnxt_dl_nvm_param_set(struct devlink *dl, u32 id,
320 				 struct devlink_param_gset_ctx *ctx)
321 {
322 	struct hwrm_nvm_set_variable_input req = {0};
323 	struct bnxt *bp = bnxt_get_bp_from_dl(dl);
324 
325 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_SET_VARIABLE, -1, -1);
326 
327 	if (id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
328 		ctx->val.vbool = !ctx->val.vbool;
329 
330 	return bnxt_hwrm_nvm_req(bp, id, &req, sizeof(req), &ctx->val);
331 }
332 
333 static int bnxt_dl_msix_validate(struct devlink *dl, u32 id,
334 				 union devlink_param_value val,
335 				 struct netlink_ext_ack *extack)
336 {
337 	int max_val = -1;
338 
339 	if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX)
340 		max_val = BNXT_MSIX_VEC_MAX;
341 
342 	if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN)
343 		max_val = BNXT_MSIX_VEC_MIN_MAX;
344 
345 	if (val.vu32 > max_val) {
346 		NL_SET_ERR_MSG_MOD(extack, "MSIX value is exceeding the range");
347 		return -EINVAL;
348 	}
349 
350 	return 0;
351 }
352 
353 static const struct devlink_param bnxt_dl_params[] = {
354 	DEVLINK_PARAM_GENERIC(ENABLE_SRIOV,
355 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
356 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
357 			      NULL),
358 	DEVLINK_PARAM_GENERIC(IGNORE_ARI,
359 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
360 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
361 			      NULL),
362 	DEVLINK_PARAM_GENERIC(MSIX_VEC_PER_PF_MAX,
363 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
364 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
365 			      bnxt_dl_msix_validate),
366 	DEVLINK_PARAM_GENERIC(MSIX_VEC_PER_PF_MIN,
367 			      BIT(DEVLINK_PARAM_CMODE_PERMANENT),
368 			      bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
369 			      bnxt_dl_msix_validate),
370 	DEVLINK_PARAM_DRIVER(BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK,
371 			     "gre_ver_check", DEVLINK_PARAM_TYPE_BOOL,
372 			     BIT(DEVLINK_PARAM_CMODE_PERMANENT),
373 			     bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
374 			     NULL),
375 };
376 
377 static const struct devlink_param bnxt_dl_port_params[] = {
378 };
379 
380 int bnxt_dl_register(struct bnxt *bp)
381 {
382 	struct devlink *dl;
383 	int rc;
384 
385 	if (bp->hwrm_spec_code < 0x10600) {
386 		netdev_warn(bp->dev, "Firmware does not support NVM params");
387 		return -ENOTSUPP;
388 	}
389 
390 	dl = devlink_alloc(&bnxt_dl_ops, sizeof(struct bnxt_dl));
391 	if (!dl) {
392 		netdev_warn(bp->dev, "devlink_alloc failed");
393 		return -ENOMEM;
394 	}
395 
396 	bnxt_link_bp_to_dl(bp, dl);
397 
398 	/* Add switchdev eswitch mode setting, if SRIOV supported */
399 	if (pci_find_ext_capability(bp->pdev, PCI_EXT_CAP_ID_SRIOV) &&
400 	    bp->hwrm_spec_code > 0x10803)
401 		bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
402 
403 	rc = devlink_register(dl, &bp->pdev->dev);
404 	if (rc) {
405 		netdev_warn(bp->dev, "devlink_register failed. rc=%d", rc);
406 		goto err_dl_free;
407 	}
408 
409 	rc = devlink_params_register(dl, bnxt_dl_params,
410 				     ARRAY_SIZE(bnxt_dl_params));
411 	if (rc) {
412 		netdev_warn(bp->dev, "devlink_params_register failed. rc=%d",
413 			    rc);
414 		goto err_dl_unreg;
415 	}
416 
417 	devlink_port_attrs_set(&bp->dl_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
418 			       bp->pf.port_id, false, 0,
419 			       bp->switch_id, sizeof(bp->switch_id));
420 	rc = devlink_port_register(dl, &bp->dl_port, bp->pf.port_id);
421 	if (rc) {
422 		netdev_err(bp->dev, "devlink_port_register failed");
423 		goto err_dl_param_unreg;
424 	}
425 	devlink_port_type_eth_set(&bp->dl_port, bp->dev);
426 
427 	rc = devlink_port_params_register(&bp->dl_port, bnxt_dl_port_params,
428 					  ARRAY_SIZE(bnxt_dl_port_params));
429 	if (rc) {
430 		netdev_err(bp->dev, "devlink_port_params_register failed");
431 		goto err_dl_port_unreg;
432 	}
433 
434 	devlink_params_publish(dl);
435 
436 	bnxt_dl_fw_reporters_create(bp);
437 
438 	return 0;
439 
440 err_dl_port_unreg:
441 	devlink_port_unregister(&bp->dl_port);
442 err_dl_param_unreg:
443 	devlink_params_unregister(dl, bnxt_dl_params,
444 				  ARRAY_SIZE(bnxt_dl_params));
445 err_dl_unreg:
446 	devlink_unregister(dl);
447 err_dl_free:
448 	bnxt_link_bp_to_dl(bp, NULL);
449 	devlink_free(dl);
450 	return rc;
451 }
452 
453 void bnxt_dl_unregister(struct bnxt *bp)
454 {
455 	struct devlink *dl = bp->dl;
456 
457 	if (!dl)
458 		return;
459 
460 	bnxt_dl_fw_reporters_destroy(bp);
461 	devlink_port_params_unregister(&bp->dl_port, bnxt_dl_port_params,
462 				       ARRAY_SIZE(bnxt_dl_port_params));
463 	devlink_port_unregister(&bp->dl_port);
464 	devlink_params_unregister(dl, bnxt_dl_params,
465 				  ARRAY_SIZE(bnxt_dl_params));
466 	devlink_unregister(dl);
467 	devlink_free(dl);
468 }
469