bnxt_devlink.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include <linux/pci.h>
  10. #include <linux/netdevice.h>
  11. #include "bnxt_hsi.h"
  12. #include "bnxt.h"
  13. #include "bnxt_vfr.h"
  14. #include "bnxt_devlink.h"
  15. static const struct devlink_ops bnxt_dl_ops = {
  16. #ifdef CONFIG_BNXT_SRIOV
  17. .eswitch_mode_set = bnxt_dl_eswitch_mode_set,
  18. .eswitch_mode_get = bnxt_dl_eswitch_mode_get,
  19. #endif /* CONFIG_BNXT_SRIOV */
  20. };
  21. static const struct bnxt_dl_nvm_param nvm_params[] = {
  22. {DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV, NVM_OFF_ENABLE_SRIOV,
  23. BNXT_NVM_SHARED_CFG, 1},
  24. };
  25. static int bnxt_hwrm_nvm_req(struct bnxt *bp, u32 param_id, void *msg,
  26. int msg_len, union devlink_param_value *val)
  27. {
  28. struct hwrm_nvm_get_variable_input *req = msg;
  29. void *data_addr = NULL, *buf = NULL;
  30. struct bnxt_dl_nvm_param nvm_param;
  31. int bytesize, idx = 0, rc, i;
  32. dma_addr_t data_dma_addr;
  33. /* Get/Set NVM CFG parameter is supported only on PFs */
  34. if (BNXT_VF(bp))
  35. return -EPERM;
  36. for (i = 0; i < ARRAY_SIZE(nvm_params); i++) {
  37. if (nvm_params[i].id == param_id) {
  38. nvm_param = nvm_params[i];
  39. break;
  40. }
  41. }
  42. if (i == ARRAY_SIZE(nvm_params))
  43. return -EOPNOTSUPP;
  44. if (nvm_param.dir_type == BNXT_NVM_PORT_CFG)
  45. idx = bp->pf.port_id;
  46. else if (nvm_param.dir_type == BNXT_NVM_FUNC_CFG)
  47. idx = bp->pf.fw_fid - BNXT_FIRST_PF_FID;
  48. bytesize = roundup(nvm_param.num_bits, BITS_PER_BYTE) / BITS_PER_BYTE;
  49. if (nvm_param.num_bits == 1)
  50. buf = &val->vbool;
  51. data_addr = dma_zalloc_coherent(&bp->pdev->dev, bytesize,
  52. &data_dma_addr, GFP_KERNEL);
  53. if (!data_addr)
  54. return -ENOMEM;
  55. req->dest_data_addr = cpu_to_le64(data_dma_addr);
  56. req->data_len = cpu_to_le16(nvm_param.num_bits);
  57. req->option_num = cpu_to_le16(nvm_param.offset);
  58. req->index_0 = cpu_to_le16(idx);
  59. if (idx)
  60. req->dimensions = cpu_to_le16(1);
  61. if (req->req_type == cpu_to_le16(HWRM_NVM_SET_VARIABLE))
  62. memcpy(data_addr, buf, bytesize);
  63. rc = hwrm_send_message(bp, msg, msg_len, HWRM_CMD_TIMEOUT);
  64. if (!rc && req->req_type == cpu_to_le16(HWRM_NVM_GET_VARIABLE))
  65. memcpy(buf, data_addr, bytesize);
  66. dma_free_coherent(&bp->pdev->dev, bytesize, data_addr, data_dma_addr);
  67. if (rc == HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED) {
  68. netdev_err(bp->dev, "PF does not have admin privileges to modify NVM config\n");
  69. return -EACCES;
  70. } else if (rc) {
  71. return -EIO;
  72. }
  73. return 0;
  74. }
  75. static int bnxt_dl_nvm_param_get(struct devlink *dl, u32 id,
  76. struct devlink_param_gset_ctx *ctx)
  77. {
  78. struct hwrm_nvm_get_variable_input req = {0};
  79. struct bnxt *bp = bnxt_get_bp_from_dl(dl);
  80. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_GET_VARIABLE, -1, -1);
  81. return bnxt_hwrm_nvm_req(bp, id, &req, sizeof(req), &ctx->val);
  82. }
  83. static int bnxt_dl_nvm_param_set(struct devlink *dl, u32 id,
  84. struct devlink_param_gset_ctx *ctx)
  85. {
  86. struct hwrm_nvm_set_variable_input req = {0};
  87. struct bnxt *bp = bnxt_get_bp_from_dl(dl);
  88. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_NVM_SET_VARIABLE, -1, -1);
  89. return bnxt_hwrm_nvm_req(bp, id, &req, sizeof(req), &ctx->val);
  90. }
  91. static const struct devlink_param bnxt_dl_params[] = {
  92. DEVLINK_PARAM_GENERIC(ENABLE_SRIOV,
  93. BIT(DEVLINK_PARAM_CMODE_PERMANENT),
  94. bnxt_dl_nvm_param_get, bnxt_dl_nvm_param_set,
  95. NULL),
  96. };
  97. int bnxt_dl_register(struct bnxt *bp)
  98. {
  99. struct devlink *dl;
  100. int rc;
  101. if (bp->hwrm_spec_code < 0x10600) {
  102. netdev_warn(bp->dev, "Firmware does not support NVM params");
  103. return -ENOTSUPP;
  104. }
  105. dl = devlink_alloc(&bnxt_dl_ops, sizeof(struct bnxt_dl));
  106. if (!dl) {
  107. netdev_warn(bp->dev, "devlink_alloc failed");
  108. return -ENOMEM;
  109. }
  110. bnxt_link_bp_to_dl(bp, dl);
  111. /* Add switchdev eswitch mode setting, if SRIOV supported */
  112. if (pci_find_ext_capability(bp->pdev, PCI_EXT_CAP_ID_SRIOV) &&
  113. bp->hwrm_spec_code > 0x10803)
  114. bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
  115. rc = devlink_register(dl, &bp->pdev->dev);
  116. if (rc) {
  117. netdev_warn(bp->dev, "devlink_register failed. rc=%d", rc);
  118. goto err_dl_free;
  119. }
  120. rc = devlink_params_register(dl, bnxt_dl_params,
  121. ARRAY_SIZE(bnxt_dl_params));
  122. if (rc) {
  123. netdev_warn(bp->dev, "devlink_params_register failed. rc=%d",
  124. rc);
  125. goto err_dl_unreg;
  126. }
  127. return 0;
  128. err_dl_unreg:
  129. devlink_unregister(dl);
  130. err_dl_free:
  131. bnxt_link_bp_to_dl(bp, NULL);
  132. devlink_free(dl);
  133. return rc;
  134. }
  135. void bnxt_dl_unregister(struct bnxt *bp)
  136. {
  137. struct devlink *dl = bp->dl;
  138. if (!dl)
  139. return;
  140. devlink_params_unregister(dl, bnxt_dl_params,
  141. ARRAY_SIZE(bnxt_dl_params));
  142. devlink_unregister(dl);
  143. devlink_free(dl);
  144. }