1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2017 Broadcom Limited
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.
10 #include <linux/pci.h>
11 #include <linux/netdevice.h>
12 #include <net/devlink.h>
16 #include "bnxt_devlink.h"
17 #include "bnxt_ethtool.h"
20 bnxt_dl_flash_update(struct devlink *dl, const char *filename,
21 const char *region, struct netlink_ext_ack *extack)
23 struct bnxt *bp = bnxt_get_bp_from_dl(dl);
29 NL_SET_ERR_MSG_MOD(extack,
30 "flash update not supported from a VF");
34 return bnxt_flash_package_from_file(bp->dev, filename, 0);
37 static int bnxt_fw_reporter_diagnose(struct devlink_health_reporter *reporter,
38 struct devlink_fmsg *fmsg,
39 struct netlink_ext_ack *extack)
41 struct bnxt *bp = devlink_health_reporter_priv(reporter);
42 struct bnxt_fw_health *health = bp->fw_health;
43 u32 val, health_status;
46 if (!health || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
49 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
50 health_status = val & 0xffff;
52 if (health_status < BNXT_FW_STATUS_HEALTHY) {
53 rc = devlink_fmsg_string_pair_put(fmsg, "Description",
54 "Not yet completed initialization");
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");
65 rc = devlink_fmsg_u32_pair_put(fmsg, "Error code", val >> 16);
70 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
71 rc = devlink_fmsg_u32_pair_put(fmsg, "Reset count", val);
78 static const struct devlink_health_reporter_ops bnxt_dl_fw_reporter_ops = {
80 .diagnose = bnxt_fw_reporter_diagnose,
83 static int bnxt_fw_reset_recover(struct devlink_health_reporter *reporter,
85 struct netlink_ext_ack *extack)
87 struct bnxt *bp = devlink_health_reporter_priv(reporter);
97 struct devlink_health_reporter_ops bnxt_dl_fw_reset_reporter_ops = {
99 .recover = bnxt_fw_reset_recover,
102 static int bnxt_fw_fatal_recover(struct devlink_health_reporter *reporter,
104 struct netlink_ext_ack *extack)
106 struct bnxt *bp = devlink_health_reporter_priv(reporter);
107 struct bnxt_fw_reporter_ctx *fw_reporter_ctx = priv_ctx;
113 bp->fw_health->fatal = true;
114 event = fw_reporter_ctx->sp_event;
115 if (event == BNXT_FW_RESET_NOTIFY_SP_EVENT)
117 else if (event == BNXT_FW_EXCEPTION_SP_EVENT)
118 bnxt_fw_exception(bp);
124 struct devlink_health_reporter_ops bnxt_dl_fw_fatal_reporter_ops = {
126 .recover = bnxt_fw_fatal_recover,
129 static void bnxt_dl_fw_reporters_create(struct bnxt *bp)
131 struct bnxt_fw_health *health = bp->fw_health;
136 health->fw_reporter =
137 devlink_health_reporter_create(bp->dl, &bnxt_dl_fw_reporter_ops,
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;
145 health->fw_reset_reporter =
146 devlink_health_reporter_create(bp->dl,
147 &bnxt_dl_fw_reset_reporter_ops,
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;
155 health->fw_fatal_reporter =
156 devlink_health_reporter_create(bp->dl,
157 &bnxt_dl_fw_fatal_reporter_ops,
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;
166 static void bnxt_dl_fw_reporters_destroy(struct bnxt *bp)
168 struct bnxt_fw_health *health = bp->fw_health;
173 if (health->fw_reporter)
174 devlink_health_reporter_destroy(health->fw_reporter);
176 if (health->fw_reset_reporter)
177 devlink_health_reporter_destroy(health->fw_reset_reporter);
179 if (health->fw_fatal_reporter)
180 devlink_health_reporter_destroy(health->fw_fatal_reporter);
183 void bnxt_devlink_health_report(struct bnxt *bp, unsigned long event)
185 struct bnxt_fw_health *fw_health = bp->fw_health;
186 struct bnxt_fw_reporter_ctx fw_reporter_ctx;
191 fw_reporter_ctx.sp_event = 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)
198 devlink_health_report(fw_health->fw_fatal_reporter,
199 "FW fatal async event received",
203 if (!fw_health->fw_reset_reporter)
206 devlink_health_report(fw_health->fw_reset_reporter,
207 "FW non-fatal reset event received",
211 case BNXT_FW_EXCEPTION_SP_EVENT:
212 if (!fw_health->fw_fatal_reporter)
215 devlink_health_report(fw_health->fw_fatal_reporter,
216 "FW fatal error reported",
222 void bnxt_dl_health_status_update(struct bnxt *bp, bool healthy)
224 struct bnxt_fw_health *health = bp->fw_health;
228 state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
230 state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
233 devlink_health_reporter_state_update(health->fw_fatal_reporter,
236 devlink_health_reporter_state_update(health->fw_reset_reporter,
239 health->fatal = false;
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,
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,
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},
268 union bnxt_nvm_data {
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)
279 if (nvm_num_bits == 1) {
280 dst->val8 = src->vbool;
283 if (dl_num_bytes == 4)
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);
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)
298 if (nvm_num_bits == 1) {
299 dst->vbool = src->val8;
302 val32 = le32_to_cpu(src->val32);
303 if (dl_num_bytes == 4)
305 else if (dl_num_bytes == 2)
306 dst->vu16 = (u16)val32;
307 else if (dl_num_bytes == 1)
308 dst->vu8 = (u8)val32;
311 static int bnxt_hwrm_nvm_req(struct bnxt *bp, u32 param_id, void *msg,
312 int msg_len, union devlink_param_value *val)
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;
320 /* Get/Set NVM CFG parameter is supported only on PFs */
324 for (i = 0; i < ARRAY_SIZE(nvm_params); i++) {
325 if (nvm_params[i].id == param_id) {
326 nvm_param = nvm_params[i];
331 if (i == ARRAY_SIZE(nvm_params))
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;
339 data = dma_alloc_coherent(&bp->pdev->dev, sizeof(*data),
340 &data_dma_addr, GFP_KERNEL);
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);
349 req->dimensions = cpu_to_le16(1);
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);
356 rc = hwrm_send_message_silent(bp, msg, msg_len,
359 bnxt_copy_from_nvm_data(val, data,
360 nvm_param.nvm_num_bits,
361 nvm_param.dl_num_bytes);
363 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
366 NVM_GET_VARIABLE_CMD_ERR_CODE_VAR_NOT_EXIST)
370 dma_free_coherent(&bp->pdev->dev, sizeof(*data), data, data_dma_addr);
372 netdev_err(bp->dev, "PF does not have admin privileges to modify NVM config\n");
376 static int bnxt_dl_nvm_param_get(struct devlink *dl, u32 id,
377 struct devlink_param_gset_ctx *ctx)
379 struct hwrm_nvm_get_variable_input req = {0};
380 struct bnxt *bp = bnxt_get_bp_from_dl(dl);
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);
386 if (id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
387 ctx->val.vbool = !ctx->val.vbool;
392 static int bnxt_dl_nvm_param_set(struct devlink *dl, u32 id,
393 struct devlink_param_gset_ctx *ctx)
395 struct hwrm_nvm_set_variable_input req = {0};
396 struct bnxt *bp = bnxt_get_bp_from_dl(dl);
398 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_SET_VARIABLE, -1, -1);
400 if (id == BNXT_DEVLINK_PARAM_ID_GRE_VER_CHECK)
401 ctx->val.vbool = !ctx->val.vbool;
403 return bnxt_hwrm_nvm_req(bp, id, &req, sizeof(req), &ctx->val);
406 static int bnxt_dl_msix_validate(struct devlink *dl, u32 id,
407 union devlink_param_value val,
408 struct netlink_ext_ack *extack)
412 if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX)
413 max_val = BNXT_MSIX_VEC_MAX;
415 if (id == DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN)
416 max_val = BNXT_MSIX_VEC_MIN_MAX;
418 if (val.vu32 > max_val) {
419 NL_SET_ERR_MSG_MOD(extack, "MSIX value is exceeding the range");
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,
431 DEVLINK_PARAM_GENERIC(IGNORE_ARI,
432 BIT(DEVLINK_PARAM_CMODE_PERMANENT),
433 bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
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,
450 static const struct devlink_param bnxt_dl_port_params[] = {
453 int bnxt_dl_register(struct bnxt *bp)
458 if (bp->hwrm_spec_code < 0x10600) {
459 netdev_warn(bp->dev, "Firmware does not support NVM params");
463 dl = devlink_alloc(&bnxt_dl_ops, sizeof(struct bnxt_dl));
465 netdev_warn(bp->dev, "devlink_alloc failed");
469 bnxt_link_bp_to_dl(bp, dl);
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;
476 rc = devlink_register(dl, &bp->pdev->dev);
478 netdev_warn(bp->dev, "devlink_register failed. rc=%d", rc);
482 rc = devlink_params_register(dl, bnxt_dl_params,
483 ARRAY_SIZE(bnxt_dl_params));
485 netdev_warn(bp->dev, "devlink_params_register failed. rc=%d",
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);
495 netdev_err(bp->dev, "devlink_port_register failed");
496 goto err_dl_param_unreg;
498 devlink_port_type_eth_set(&bp->dl_port, bp->dev);
500 rc = devlink_port_params_register(&bp->dl_port, bnxt_dl_port_params,
501 ARRAY_SIZE(bnxt_dl_port_params));
503 netdev_err(bp->dev, "devlink_port_params_register failed");
504 goto err_dl_port_unreg;
507 devlink_params_publish(dl);
509 bnxt_dl_fw_reporters_create(bp);
514 devlink_port_unregister(&bp->dl_port);
516 devlink_params_unregister(dl, bnxt_dl_params,
517 ARRAY_SIZE(bnxt_dl_params));
519 devlink_unregister(dl);
521 bnxt_link_bp_to_dl(bp, NULL);
526 void bnxt_dl_unregister(struct bnxt *bp)
528 struct devlink *dl = bp->dl;
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);