i40e_devlink.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2023 Intel Corporation. */
  3. #include <net/devlink.h>
  4. #include "i40e.h"
  5. #include "i40e_devlink.h"
  6. static void i40e_info_get_dsn(struct i40e_pf *pf, char *buf, size_t len)
  7. {
  8. u8 dsn[8];
  9. put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
  10. snprintf(buf, len, "%8phD", dsn);
  11. }
  12. static void i40e_info_fw_mgmt(struct i40e_hw *hw, char *buf, size_t len)
  13. {
  14. struct i40e_adminq_info *aq = &hw->aq;
  15. snprintf(buf, len, "%u.%u", aq->fw_maj_ver, aq->fw_min_ver);
  16. }
  17. static void i40e_info_fw_mgmt_build(struct i40e_hw *hw, char *buf, size_t len)
  18. {
  19. struct i40e_adminq_info *aq = &hw->aq;
  20. snprintf(buf, len, "%05d", aq->fw_build);
  21. }
  22. static void i40e_info_fw_api(struct i40e_hw *hw, char *buf, size_t len)
  23. {
  24. struct i40e_adminq_info *aq = &hw->aq;
  25. snprintf(buf, len, "%u.%u", aq->api_maj_ver, aq->api_min_ver);
  26. }
  27. static void i40e_info_pba(struct i40e_hw *hw, char *buf, size_t len)
  28. {
  29. buf[0] = '\0';
  30. if (hw->pba_id)
  31. strscpy(buf, hw->pba_id, len);
  32. }
  33. enum i40e_devlink_version_type {
  34. I40E_DL_VERSION_FIXED,
  35. I40E_DL_VERSION_RUNNING,
  36. };
  37. static int i40e_devlink_info_put(struct devlink_info_req *req,
  38. enum i40e_devlink_version_type type,
  39. const char *key, const char *value)
  40. {
  41. if (!strlen(value))
  42. return 0;
  43. switch (type) {
  44. case I40E_DL_VERSION_FIXED:
  45. return devlink_info_version_fixed_put(req, key, value);
  46. case I40E_DL_VERSION_RUNNING:
  47. return devlink_info_version_running_put(req, key, value);
  48. }
  49. return 0;
  50. }
  51. static int i40e_devlink_info_get(struct devlink *dl,
  52. struct devlink_info_req *req,
  53. struct netlink_ext_ack *extack)
  54. {
  55. struct i40e_pf *pf = devlink_priv(dl);
  56. struct i40e_hw *hw = &pf->hw;
  57. char buf[32];
  58. int err;
  59. i40e_info_get_dsn(pf, buf, sizeof(buf));
  60. err = devlink_info_serial_number_put(req, buf);
  61. if (err)
  62. return err;
  63. i40e_info_fw_mgmt(hw, buf, sizeof(buf));
  64. err = i40e_devlink_info_put(req, I40E_DL_VERSION_RUNNING,
  65. DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, buf);
  66. if (err)
  67. return err;
  68. i40e_info_fw_mgmt_build(hw, buf, sizeof(buf));
  69. err = i40e_devlink_info_put(req, I40E_DL_VERSION_RUNNING,
  70. "fw.mgmt.build", buf);
  71. if (err)
  72. return err;
  73. i40e_info_fw_api(hw, buf, sizeof(buf));
  74. err = i40e_devlink_info_put(req, I40E_DL_VERSION_RUNNING,
  75. DEVLINK_INFO_VERSION_GENERIC_FW_MGMT_API,
  76. buf);
  77. if (err)
  78. return err;
  79. i40e_info_nvm_ver(hw, buf, sizeof(buf));
  80. err = i40e_devlink_info_put(req, I40E_DL_VERSION_RUNNING,
  81. "fw.psid.api", buf);
  82. if (err)
  83. return err;
  84. i40e_info_eetrack(hw, buf, sizeof(buf));
  85. err = i40e_devlink_info_put(req, I40E_DL_VERSION_RUNNING,
  86. DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID,
  87. buf);
  88. if (err)
  89. return err;
  90. i40e_info_civd_ver(hw, buf, sizeof(buf));
  91. err = i40e_devlink_info_put(req, I40E_DL_VERSION_RUNNING,
  92. DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, buf);
  93. if (err)
  94. return err;
  95. i40e_info_pba(hw, buf, sizeof(buf));
  96. err = i40e_devlink_info_put(req, I40E_DL_VERSION_FIXED,
  97. DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, buf);
  98. return err;
  99. }
  100. static const struct devlink_ops i40e_devlink_ops = {
  101. .info_get = i40e_devlink_info_get,
  102. };
  103. /**
  104. * i40e_alloc_pf - Allocate devlink and return i40e_pf structure pointer
  105. * @dev: the device to allocate for
  106. *
  107. * Allocate a devlink instance for this device and return the private
  108. * area as the i40e_pf structure.
  109. **/
  110. struct i40e_pf *i40e_alloc_pf(struct device *dev)
  111. {
  112. struct devlink *devlink;
  113. devlink = devlink_alloc(&i40e_devlink_ops, sizeof(struct i40e_pf), dev);
  114. if (!devlink)
  115. return NULL;
  116. return devlink_priv(devlink);
  117. }
  118. /**
  119. * i40e_free_pf - Free i40e_pf structure and associated devlink
  120. * @pf: the PF structure
  121. *
  122. * Free i40e_pf structure and devlink allocated by devlink_alloc.
  123. **/
  124. void i40e_free_pf(struct i40e_pf *pf)
  125. {
  126. struct devlink *devlink = priv_to_devlink(pf);
  127. devlink_free(devlink);
  128. }
  129. /**
  130. * i40e_devlink_register - Register devlink interface for this PF
  131. * @pf: the PF to register the devlink for.
  132. *
  133. * Register the devlink instance associated with this physical function.
  134. **/
  135. void i40e_devlink_register(struct i40e_pf *pf)
  136. {
  137. devlink_register(priv_to_devlink(pf));
  138. }
  139. /**
  140. * i40e_devlink_unregister - Unregister devlink resources for this PF.
  141. * @pf: the PF structure to cleanup
  142. *
  143. * Releases resources used by devlink and cleans up associated memory.
  144. **/
  145. void i40e_devlink_unregister(struct i40e_pf *pf)
  146. {
  147. devlink_unregister(priv_to_devlink(pf));
  148. }
  149. /**
  150. * i40e_devlink_set_switch_id - Set unique switch id based on pci dsn
  151. * @pf: the PF to create a devlink port for
  152. * @ppid: struct with switch id information
  153. */
  154. static void i40e_devlink_set_switch_id(struct i40e_pf *pf,
  155. struct netdev_phys_item_id *ppid)
  156. {
  157. u64 id = pci_get_dsn(pf->pdev);
  158. ppid->id_len = sizeof(id);
  159. put_unaligned_be64(id, &ppid->id);
  160. }
  161. /**
  162. * i40e_devlink_create_port - Create a devlink port for this PF
  163. * @pf: the PF to create a port for
  164. *
  165. * Create and register a devlink_port for this PF. Note that although each
  166. * physical function is connected to a separate devlink instance, the port
  167. * will still be numbered according to the physical function id.
  168. *
  169. * Return: zero on success or an error code on failure.
  170. **/
  171. int i40e_devlink_create_port(struct i40e_pf *pf)
  172. {
  173. struct devlink *devlink = priv_to_devlink(pf);
  174. struct devlink_port_attrs attrs = {};
  175. struct device *dev = &pf->pdev->dev;
  176. int err;
  177. attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
  178. attrs.phys.port_number = pf->hw.pf_id;
  179. i40e_devlink_set_switch_id(pf, &attrs.switch_id);
  180. devlink_port_attrs_set(&pf->devlink_port, &attrs);
  181. err = devlink_port_register(devlink, &pf->devlink_port, pf->hw.pf_id);
  182. if (err) {
  183. dev_err(dev, "devlink_port_register failed: %d\n", err);
  184. return err;
  185. }
  186. return 0;
  187. }
  188. /**
  189. * i40e_devlink_destroy_port - Destroy the devlink_port for this PF
  190. * @pf: the PF to cleanup
  191. *
  192. * Unregisters the devlink_port structure associated with this PF.
  193. **/
  194. void i40e_devlink_destroy_port(struct i40e_pf *pf)
  195. {
  196. devlink_port_unregister(&pf->devlink_port);
  197. }