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