en_dcb_nl.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2011 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/dcbnl.h>
  34. #include <linux/math64.h>
  35. #include "mlx4_en.h"
  36. static int mlx4_en_dcbnl_ieee_getets(struct net_device *dev,
  37. struct ieee_ets *ets)
  38. {
  39. struct mlx4_en_priv *priv = netdev_priv(dev);
  40. struct ieee_ets *my_ets = &priv->ets;
  41. /* No IEEE PFC settings available */
  42. if (!my_ets)
  43. return -EINVAL;
  44. ets->ets_cap = IEEE_8021QAZ_MAX_TCS;
  45. ets->cbs = my_ets->cbs;
  46. memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
  47. memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
  48. memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
  49. return 0;
  50. }
  51. static int mlx4_en_ets_validate(struct mlx4_en_priv *priv, struct ieee_ets *ets)
  52. {
  53. int i;
  54. int total_ets_bw = 0;
  55. int has_ets_tc = 0;
  56. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  57. if (ets->prio_tc[i] > MLX4_EN_NUM_UP) {
  58. en_err(priv, "Bad priority in UP <=> TC mapping. TC: %d, UP: %d\n",
  59. i, ets->prio_tc[i]);
  60. return -EINVAL;
  61. }
  62. switch (ets->tc_tsa[i]) {
  63. case IEEE_8021QAZ_TSA_STRICT:
  64. break;
  65. case IEEE_8021QAZ_TSA_ETS:
  66. has_ets_tc = 1;
  67. total_ets_bw += ets->tc_tx_bw[i];
  68. break;
  69. default:
  70. en_err(priv, "TC[%d]: Not supported TSA: %d\n",
  71. i, ets->tc_tsa[i]);
  72. return -ENOTSUPP;
  73. }
  74. }
  75. if (has_ets_tc && total_ets_bw != MLX4_EN_BW_MAX) {
  76. en_err(priv, "Bad ETS BW sum: %d. Should be exactly 100%%\n",
  77. total_ets_bw);
  78. return -EINVAL;
  79. }
  80. return 0;
  81. }
  82. static int mlx4_en_config_port_scheduler(struct mlx4_en_priv *priv,
  83. struct ieee_ets *ets, u16 *ratelimit)
  84. {
  85. struct mlx4_en_dev *mdev = priv->mdev;
  86. int num_strict = 0;
  87. int i;
  88. __u8 tc_tx_bw[IEEE_8021QAZ_MAX_TCS] = { 0 };
  89. __u8 pg[IEEE_8021QAZ_MAX_TCS] = { 0 };
  90. ets = ets ?: &priv->ets;
  91. ratelimit = ratelimit ?: priv->maxrate;
  92. /* higher TC means higher priority => lower pg */
  93. for (i = IEEE_8021QAZ_MAX_TCS - 1; i >= 0; i--) {
  94. switch (ets->tc_tsa[i]) {
  95. case IEEE_8021QAZ_TSA_STRICT:
  96. pg[i] = num_strict++;
  97. tc_tx_bw[i] = MLX4_EN_BW_MAX;
  98. break;
  99. case IEEE_8021QAZ_TSA_ETS:
  100. pg[i] = MLX4_EN_TC_ETS;
  101. tc_tx_bw[i] = ets->tc_tx_bw[i] ?: MLX4_EN_BW_MIN;
  102. break;
  103. }
  104. }
  105. return mlx4_SET_PORT_SCHEDULER(mdev->dev, priv->port, tc_tx_bw, pg,
  106. ratelimit);
  107. }
  108. static int
  109. mlx4_en_dcbnl_ieee_setets(struct net_device *dev, struct ieee_ets *ets)
  110. {
  111. struct mlx4_en_priv *priv = netdev_priv(dev);
  112. struct mlx4_en_dev *mdev = priv->mdev;
  113. int err;
  114. err = mlx4_en_ets_validate(priv, ets);
  115. if (err)
  116. return err;
  117. err = mlx4_SET_PORT_PRIO2TC(mdev->dev, priv->port, ets->prio_tc);
  118. if (err)
  119. return err;
  120. err = mlx4_en_config_port_scheduler(priv, ets, NULL);
  121. if (err)
  122. return err;
  123. memcpy(&priv->ets, ets, sizeof(priv->ets));
  124. return 0;
  125. }
  126. static int mlx4_en_dcbnl_ieee_getpfc(struct net_device *dev,
  127. struct ieee_pfc *pfc)
  128. {
  129. struct mlx4_en_priv *priv = netdev_priv(dev);
  130. pfc->pfc_cap = IEEE_8021QAZ_MAX_TCS;
  131. pfc->pfc_en = priv->prof->tx_ppp;
  132. return 0;
  133. }
  134. static int mlx4_en_dcbnl_ieee_setpfc(struct net_device *dev,
  135. struct ieee_pfc *pfc)
  136. {
  137. struct mlx4_en_priv *priv = netdev_priv(dev);
  138. struct mlx4_en_dev *mdev = priv->mdev;
  139. int err;
  140. en_dbg(DRV, priv, "cap: 0x%x en: 0x%x mbc: 0x%x delay: %d\n",
  141. pfc->pfc_cap,
  142. pfc->pfc_en,
  143. pfc->mbc,
  144. pfc->delay);
  145. priv->prof->rx_pause = priv->prof->tx_pause = !!pfc->pfc_en;
  146. priv->prof->rx_ppp = priv->prof->tx_ppp = pfc->pfc_en;
  147. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  148. priv->rx_skb_size + ETH_FCS_LEN,
  149. priv->prof->tx_pause,
  150. priv->prof->tx_ppp,
  151. priv->prof->rx_pause,
  152. priv->prof->rx_ppp);
  153. if (err)
  154. en_err(priv, "Failed setting pause params\n");
  155. return err;
  156. }
  157. static u8 mlx4_en_dcbnl_getdcbx(struct net_device *dev)
  158. {
  159. return DCB_CAP_DCBX_VER_IEEE;
  160. }
  161. static u8 mlx4_en_dcbnl_setdcbx(struct net_device *dev, u8 mode)
  162. {
  163. if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
  164. (mode & DCB_CAP_DCBX_VER_CEE) ||
  165. !(mode & DCB_CAP_DCBX_VER_IEEE) ||
  166. !(mode & DCB_CAP_DCBX_HOST))
  167. return 1;
  168. return 0;
  169. }
  170. #define MLX4_RATELIMIT_UNITS_IN_KB 100000 /* rate-limit HW unit in Kbps */
  171. static int mlx4_en_dcbnl_ieee_getmaxrate(struct net_device *dev,
  172. struct ieee_maxrate *maxrate)
  173. {
  174. struct mlx4_en_priv *priv = netdev_priv(dev);
  175. int i;
  176. if (!priv->maxrate)
  177. return -EINVAL;
  178. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  179. maxrate->tc_maxrate[i] =
  180. priv->maxrate[i] * MLX4_RATELIMIT_UNITS_IN_KB;
  181. return 0;
  182. }
  183. static int mlx4_en_dcbnl_ieee_setmaxrate(struct net_device *dev,
  184. struct ieee_maxrate *maxrate)
  185. {
  186. struct mlx4_en_priv *priv = netdev_priv(dev);
  187. u16 tmp[IEEE_8021QAZ_MAX_TCS];
  188. int i, err;
  189. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  190. /* Convert from Kbps into HW units, rounding result up.
  191. * Setting to 0, means unlimited BW.
  192. */
  193. tmp[i] = div_u64(maxrate->tc_maxrate[i] +
  194. MLX4_RATELIMIT_UNITS_IN_KB - 1,
  195. MLX4_RATELIMIT_UNITS_IN_KB);
  196. }
  197. err = mlx4_en_config_port_scheduler(priv, NULL, tmp);
  198. if (err)
  199. return err;
  200. memcpy(priv->maxrate, tmp, sizeof(priv->maxrate));
  201. return 0;
  202. }
  203. const struct dcbnl_rtnl_ops mlx4_en_dcbnl_ops = {
  204. .ieee_getets = mlx4_en_dcbnl_ieee_getets,
  205. .ieee_setets = mlx4_en_dcbnl_ieee_setets,
  206. .ieee_getmaxrate = mlx4_en_dcbnl_ieee_getmaxrate,
  207. .ieee_setmaxrate = mlx4_en_dcbnl_ieee_setmaxrate,
  208. .ieee_getpfc = mlx4_en_dcbnl_ieee_getpfc,
  209. .ieee_setpfc = mlx4_en_dcbnl_ieee_setpfc,
  210. .getdcbx = mlx4_en_dcbnl_getdcbx,
  211. .setdcbx = mlx4_en_dcbnl_setdcbx,
  212. };