en_dcb_nl.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "mlx4_en.h"
  35. static int mlx4_en_dcbnl_ieee_getets(struct net_device *dev,
  36. struct ieee_ets *ets)
  37. {
  38. struct mlx4_en_priv *priv = netdev_priv(dev);
  39. struct ieee_ets *my_ets = &priv->ets;
  40. /* No IEEE PFC settings available */
  41. if (!my_ets)
  42. return -EINVAL;
  43. ets->ets_cap = IEEE_8021QAZ_MAX_TCS;
  44. ets->cbs = my_ets->cbs;
  45. memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
  46. memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
  47. memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
  48. return 0;
  49. }
  50. static int mlx4_en_ets_validate(struct mlx4_en_priv *priv, struct ieee_ets *ets)
  51. {
  52. int i;
  53. int total_ets_bw = 0;
  54. int has_ets_tc = 0;
  55. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  56. if (ets->prio_tc[i] > MLX4_EN_NUM_UP) {
  57. en_err(priv, "Bad priority in UP <=> TC mapping. TC: %d, UP: %d\n",
  58. i, ets->prio_tc[i]);
  59. return -EINVAL;
  60. }
  61. switch (ets->tc_tsa[i]) {
  62. case IEEE_8021QAZ_TSA_STRICT:
  63. break;
  64. case IEEE_8021QAZ_TSA_ETS:
  65. has_ets_tc = 1;
  66. total_ets_bw += ets->tc_tx_bw[i];
  67. break;
  68. default:
  69. en_err(priv, "TC[%d]: Not supported TSA: %d\n",
  70. i, ets->tc_tsa[i]);
  71. return -ENOTSUPP;
  72. }
  73. }
  74. if (has_ets_tc && total_ets_bw != MLX4_EN_BW_MAX) {
  75. en_err(priv, "Bad ETS BW sum: %d. Should be exactly 100%%\n",
  76. total_ets_bw);
  77. return -EINVAL;
  78. }
  79. return 0;
  80. }
  81. static int mlx4_en_config_port_scheduler(struct mlx4_en_priv *priv,
  82. struct ieee_ets *ets, u16 *ratelimit)
  83. {
  84. struct mlx4_en_dev *mdev = priv->mdev;
  85. int num_strict = 0;
  86. int i;
  87. __u8 tc_tx_bw[IEEE_8021QAZ_MAX_TCS] = { 0 };
  88. __u8 pg[IEEE_8021QAZ_MAX_TCS] = { 0 };
  89. ets = ets ?: &priv->ets;
  90. ratelimit = ratelimit ?: priv->maxrate;
  91. /* higher TC means higher priority => lower pg */
  92. for (i = IEEE_8021QAZ_MAX_TCS - 1; i >= 0; i--) {
  93. switch (ets->tc_tsa[i]) {
  94. case IEEE_8021QAZ_TSA_STRICT:
  95. pg[i] = num_strict++;
  96. tc_tx_bw[i] = MLX4_EN_BW_MAX;
  97. break;
  98. case IEEE_8021QAZ_TSA_ETS:
  99. pg[i] = MLX4_EN_TC_ETS;
  100. tc_tx_bw[i] = ets->tc_tx_bw[i] ?: MLX4_EN_BW_MIN;
  101. break;
  102. }
  103. }
  104. return mlx4_SET_PORT_SCHEDULER(mdev->dev, priv->port, tc_tx_bw, pg,
  105. ratelimit);
  106. }
  107. static int
  108. mlx4_en_dcbnl_ieee_setets(struct net_device *dev, struct ieee_ets *ets)
  109. {
  110. struct mlx4_en_priv *priv = netdev_priv(dev);
  111. struct mlx4_en_dev *mdev = priv->mdev;
  112. int err;
  113. err = mlx4_en_ets_validate(priv, ets);
  114. if (err)
  115. return err;
  116. err = mlx4_SET_PORT_PRIO2TC(mdev->dev, priv->port, ets->prio_tc);
  117. if (err)
  118. return err;
  119. err = mlx4_en_config_port_scheduler(priv, ets, NULL);
  120. if (err)
  121. return err;
  122. memcpy(&priv->ets, ets, sizeof(priv->ets));
  123. return 0;
  124. }
  125. static int mlx4_en_dcbnl_ieee_getpfc(struct net_device *dev,
  126. struct ieee_pfc *pfc)
  127. {
  128. struct mlx4_en_priv *priv = netdev_priv(dev);
  129. pfc->pfc_cap = IEEE_8021QAZ_MAX_TCS;
  130. pfc->pfc_en = priv->prof->tx_ppp;
  131. return 0;
  132. }
  133. static int mlx4_en_dcbnl_ieee_setpfc(struct net_device *dev,
  134. struct ieee_pfc *pfc)
  135. {
  136. struct mlx4_en_priv *priv = netdev_priv(dev);
  137. struct mlx4_en_dev *mdev = priv->mdev;
  138. int err;
  139. en_dbg(DRV, priv, "cap: 0x%x en: 0x%x mbc: 0x%x delay: %d\n",
  140. pfc->pfc_cap,
  141. pfc->pfc_en,
  142. pfc->mbc,
  143. pfc->delay);
  144. priv->prof->rx_pause = priv->prof->tx_pause = !!pfc->pfc_en;
  145. priv->prof->rx_ppp = priv->prof->tx_ppp = pfc->pfc_en;
  146. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  147. priv->rx_skb_size + ETH_FCS_LEN,
  148. priv->prof->tx_pause,
  149. priv->prof->tx_ppp,
  150. priv->prof->rx_pause,
  151. priv->prof->rx_ppp);
  152. if (err)
  153. en_err(priv, "Failed setting pause params\n");
  154. return err;
  155. }
  156. static u8 mlx4_en_dcbnl_getdcbx(struct net_device *dev)
  157. {
  158. return DCB_CAP_DCBX_VER_IEEE;
  159. }
  160. static u8 mlx4_en_dcbnl_setdcbx(struct net_device *dev, u8 mode)
  161. {
  162. if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
  163. (mode & DCB_CAP_DCBX_VER_CEE) ||
  164. !(mode & DCB_CAP_DCBX_VER_IEEE) ||
  165. !(mode & DCB_CAP_DCBX_HOST))
  166. return 1;
  167. return 0;
  168. }
  169. #define MLX4_RATELIMIT_UNITS_IN_KB 100000 /* rate-limit HW unit in Kbps */
  170. static int mlx4_en_dcbnl_ieee_getmaxrate(struct net_device *dev,
  171. struct ieee_maxrate *maxrate)
  172. {
  173. struct mlx4_en_priv *priv = netdev_priv(dev);
  174. int i;
  175. if (!priv->maxrate)
  176. return -EINVAL;
  177. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  178. maxrate->tc_maxrate[i] =
  179. priv->maxrate[i] * MLX4_RATELIMIT_UNITS_IN_KB;
  180. return 0;
  181. }
  182. static int mlx4_en_dcbnl_ieee_setmaxrate(struct net_device *dev,
  183. struct ieee_maxrate *maxrate)
  184. {
  185. struct mlx4_en_priv *priv = netdev_priv(dev);
  186. u16 tmp[IEEE_8021QAZ_MAX_TCS];
  187. int i, err;
  188. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  189. /* Convert from Kbps into HW units, rounding result up.
  190. * Setting to 0, means unlimited BW.
  191. */
  192. tmp[i] =
  193. (maxrate->tc_maxrate[i] + MLX4_RATELIMIT_UNITS_IN_KB -
  194. 1) / MLX4_RATELIMIT_UNITS_IN_KB;
  195. }
  196. err = mlx4_en_config_port_scheduler(priv, NULL, tmp);
  197. if (err)
  198. return err;
  199. memcpy(priv->maxrate, tmp, sizeof(*priv->maxrate));
  200. return 0;
  201. }
  202. const struct dcbnl_rtnl_ops mlx4_en_dcbnl_ops = {
  203. .ieee_getets = mlx4_en_dcbnl_ieee_getets,
  204. .ieee_setets = mlx4_en_dcbnl_ieee_setets,
  205. .ieee_getmaxrate = mlx4_en_dcbnl_ieee_getmaxrate,
  206. .ieee_setmaxrate = mlx4_en_dcbnl_ieee_setmaxrate,
  207. .ieee_getpfc = mlx4_en_dcbnl_ieee_getpfc,
  208. .ieee_setpfc = mlx4_en_dcbnl_ieee_setpfc,
  209. .getdcbx = mlx4_en_dcbnl_getdcbx,
  210. .setdcbx = mlx4_en_dcbnl_setdcbx,
  211. };