dpaa2-eth-dcb.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /* Copyright 2020 NXP */
  3. #include "dpaa2-eth.h"
  4. static int dpaa2_eth_dcbnl_ieee_getpfc(struct net_device *net_dev,
  5. struct ieee_pfc *pfc)
  6. {
  7. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  8. if (!(priv->link_state.options & DPNI_LINK_OPT_PFC_PAUSE))
  9. return 0;
  10. memcpy(pfc, &priv->pfc, sizeof(priv->pfc));
  11. pfc->pfc_cap = dpaa2_eth_tc_count(priv);
  12. return 0;
  13. }
  14. static inline bool dpaa2_eth_is_prio_enabled(u8 pfc_en, u8 tc)
  15. {
  16. return !!(pfc_en & (1 << tc));
  17. }
  18. static int dpaa2_eth_set_pfc_cn(struct dpaa2_eth_priv *priv, u8 pfc_en)
  19. {
  20. struct dpni_congestion_notification_cfg cfg = {0};
  21. int i, err;
  22. cfg.notification_mode = DPNI_CONG_OPT_FLOW_CONTROL;
  23. cfg.units = DPNI_CONGESTION_UNIT_FRAMES;
  24. cfg.message_iova = 0ULL;
  25. cfg.message_ctx = 0ULL;
  26. for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
  27. if (dpaa2_eth_is_prio_enabled(pfc_en, i)) {
  28. cfg.threshold_entry = DPAA2_ETH_CN_THRESH_ENTRY(priv);
  29. cfg.threshold_exit = DPAA2_ETH_CN_THRESH_EXIT(priv);
  30. } else {
  31. /* For priorities not set in the pfc_en mask, we leave
  32. * the congestion thresholds at zero, which effectively
  33. * disables generation of PFC frames for them
  34. */
  35. cfg.threshold_entry = 0;
  36. cfg.threshold_exit = 0;
  37. }
  38. err = dpni_set_congestion_notification(priv->mc_io, 0,
  39. priv->mc_token,
  40. DPNI_QUEUE_RX, i, &cfg);
  41. if (err) {
  42. netdev_err(priv->net_dev,
  43. "dpni_set_congestion_notification failed\n");
  44. return err;
  45. }
  46. }
  47. return 0;
  48. }
  49. static int dpaa2_eth_dcbnl_ieee_setpfc(struct net_device *net_dev,
  50. struct ieee_pfc *pfc)
  51. {
  52. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  53. struct dpni_link_cfg link_cfg = {0};
  54. bool tx_pause;
  55. int err;
  56. if (pfc->mbc || pfc->delay)
  57. return -EOPNOTSUPP;
  58. /* If same PFC enabled mask, nothing to do */
  59. if (priv->pfc.pfc_en == pfc->pfc_en)
  60. return 0;
  61. /* We allow PFC configuration even if it won't have any effect until
  62. * general pause frames are enabled
  63. */
  64. tx_pause = dpaa2_eth_tx_pause_enabled(priv->link_state.options);
  65. if (!dpaa2_eth_rx_pause_enabled(priv->link_state.options) || !tx_pause)
  66. netdev_warn(net_dev, "Pause support must be enabled in order for PFC to work!\n");
  67. link_cfg.rate = priv->link_state.rate;
  68. link_cfg.options = priv->link_state.options;
  69. if (pfc->pfc_en)
  70. link_cfg.options |= DPNI_LINK_OPT_PFC_PAUSE;
  71. else
  72. link_cfg.options &= ~DPNI_LINK_OPT_PFC_PAUSE;
  73. err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
  74. if (err) {
  75. netdev_err(net_dev, "dpni_set_link_cfg failed\n");
  76. return err;
  77. }
  78. /* Configure congestion notifications for the enabled priorities */
  79. err = dpaa2_eth_set_pfc_cn(priv, pfc->pfc_en);
  80. if (err)
  81. return err;
  82. memcpy(&priv->pfc, pfc, sizeof(priv->pfc));
  83. priv->pfc_enabled = !!pfc->pfc_en;
  84. dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
  85. return 0;
  86. }
  87. static u8 dpaa2_eth_dcbnl_getdcbx(struct net_device *net_dev)
  88. {
  89. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  90. return priv->dcbx_mode;
  91. }
  92. static u8 dpaa2_eth_dcbnl_setdcbx(struct net_device *net_dev, u8 mode)
  93. {
  94. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  95. return (mode != (priv->dcbx_mode)) ? 1 : 0;
  96. }
  97. static u8 dpaa2_eth_dcbnl_getcap(struct net_device *net_dev, int capid, u8 *cap)
  98. {
  99. struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
  100. switch (capid) {
  101. case DCB_CAP_ATTR_PFC:
  102. *cap = true;
  103. break;
  104. case DCB_CAP_ATTR_PFC_TCS:
  105. *cap = 1 << (dpaa2_eth_tc_count(priv) - 1);
  106. break;
  107. case DCB_CAP_ATTR_DCBX:
  108. *cap = priv->dcbx_mode;
  109. break;
  110. default:
  111. *cap = false;
  112. break;
  113. }
  114. return 0;
  115. }
  116. const struct dcbnl_rtnl_ops dpaa2_eth_dcbnl_ops = {
  117. .ieee_getpfc = dpaa2_eth_dcbnl_ieee_getpfc,
  118. .ieee_setpfc = dpaa2_eth_dcbnl_ieee_setpfc,
  119. .getdcbx = dpaa2_eth_dcbnl_getdcbx,
  120. .setdcbx = dpaa2_eth_dcbnl_setdcbx,
  121. .getcap = dpaa2_eth_dcbnl_getcap,
  122. };