en_dcb_nl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. #include "fw_qos.h"
  37. /* Definitions for QCN
  38. */
  39. struct mlx4_congestion_control_mb_prio_802_1_qau_params {
  40. __be32 modify_enable_high;
  41. __be32 modify_enable_low;
  42. __be32 reserved1;
  43. __be32 extended_enable;
  44. __be32 rppp_max_rps;
  45. __be32 rpg_time_reset;
  46. __be32 rpg_byte_reset;
  47. __be32 rpg_threshold;
  48. __be32 rpg_max_rate;
  49. __be32 rpg_ai_rate;
  50. __be32 rpg_hai_rate;
  51. __be32 rpg_gd;
  52. __be32 rpg_min_dec_fac;
  53. __be32 rpg_min_rate;
  54. __be32 max_time_rise;
  55. __be32 max_byte_rise;
  56. __be32 max_qdelta;
  57. __be32 min_qoffset;
  58. __be32 gd_coefficient;
  59. __be32 reserved2[5];
  60. __be32 cp_sample_base;
  61. __be32 reserved3[39];
  62. };
  63. struct mlx4_congestion_control_mb_prio_802_1_qau_statistics {
  64. __be64 rppp_rp_centiseconds;
  65. __be32 reserved1;
  66. __be32 ignored_cnm;
  67. __be32 rppp_created_rps;
  68. __be32 estimated_total_rate;
  69. __be32 max_active_rate_limiter_index;
  70. __be32 dropped_cnms_busy_fw;
  71. __be32 reserved2;
  72. __be32 cnms_handled_successfully;
  73. __be32 min_total_limiters_rate;
  74. __be32 max_total_limiters_rate;
  75. __be32 reserved3[4];
  76. };
  77. static int mlx4_en_dcbnl_ieee_getets(struct net_device *dev,
  78. struct ieee_ets *ets)
  79. {
  80. struct mlx4_en_priv *priv = netdev_priv(dev);
  81. struct ieee_ets *my_ets = &priv->ets;
  82. /* No IEEE PFC settings available */
  83. if (!my_ets)
  84. return -EINVAL;
  85. ets->ets_cap = IEEE_8021QAZ_MAX_TCS;
  86. ets->cbs = my_ets->cbs;
  87. memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
  88. memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
  89. memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
  90. return 0;
  91. }
  92. static int mlx4_en_ets_validate(struct mlx4_en_priv *priv, struct ieee_ets *ets)
  93. {
  94. int i;
  95. int total_ets_bw = 0;
  96. int has_ets_tc = 0;
  97. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  98. if (ets->prio_tc[i] >= MLX4_EN_NUM_UP) {
  99. en_err(priv, "Bad priority in UP <=> TC mapping. TC: %d, UP: %d\n",
  100. i, ets->prio_tc[i]);
  101. return -EINVAL;
  102. }
  103. switch (ets->tc_tsa[i]) {
  104. case IEEE_8021QAZ_TSA_STRICT:
  105. break;
  106. case IEEE_8021QAZ_TSA_ETS:
  107. has_ets_tc = 1;
  108. total_ets_bw += ets->tc_tx_bw[i];
  109. break;
  110. default:
  111. en_err(priv, "TC[%d]: Not supported TSA: %d\n",
  112. i, ets->tc_tsa[i]);
  113. return -ENOTSUPP;
  114. }
  115. }
  116. if (has_ets_tc && total_ets_bw != MLX4_EN_BW_MAX) {
  117. en_err(priv, "Bad ETS BW sum: %d. Should be exactly 100%%\n",
  118. total_ets_bw);
  119. return -EINVAL;
  120. }
  121. return 0;
  122. }
  123. static int mlx4_en_config_port_scheduler(struct mlx4_en_priv *priv,
  124. struct ieee_ets *ets, u16 *ratelimit)
  125. {
  126. struct mlx4_en_dev *mdev = priv->mdev;
  127. int num_strict = 0;
  128. int i;
  129. __u8 tc_tx_bw[IEEE_8021QAZ_MAX_TCS] = { 0 };
  130. __u8 pg[IEEE_8021QAZ_MAX_TCS] = { 0 };
  131. ets = ets ?: &priv->ets;
  132. ratelimit = ratelimit ?: priv->maxrate;
  133. /* higher TC means higher priority => lower pg */
  134. for (i = IEEE_8021QAZ_MAX_TCS - 1; i >= 0; i--) {
  135. switch (ets->tc_tsa[i]) {
  136. case IEEE_8021QAZ_TSA_STRICT:
  137. pg[i] = num_strict++;
  138. tc_tx_bw[i] = MLX4_EN_BW_MAX;
  139. break;
  140. case IEEE_8021QAZ_TSA_ETS:
  141. pg[i] = MLX4_EN_TC_ETS;
  142. tc_tx_bw[i] = ets->tc_tx_bw[i] ?: MLX4_EN_BW_MIN;
  143. break;
  144. }
  145. }
  146. return mlx4_SET_PORT_SCHEDULER(mdev->dev, priv->port, tc_tx_bw, pg,
  147. ratelimit);
  148. }
  149. static int
  150. mlx4_en_dcbnl_ieee_setets(struct net_device *dev, struct ieee_ets *ets)
  151. {
  152. struct mlx4_en_priv *priv = netdev_priv(dev);
  153. struct mlx4_en_dev *mdev = priv->mdev;
  154. int err;
  155. err = mlx4_en_ets_validate(priv, ets);
  156. if (err)
  157. return err;
  158. err = mlx4_SET_PORT_PRIO2TC(mdev->dev, priv->port, ets->prio_tc);
  159. if (err)
  160. return err;
  161. err = mlx4_en_config_port_scheduler(priv, ets, NULL);
  162. if (err)
  163. return err;
  164. memcpy(&priv->ets, ets, sizeof(priv->ets));
  165. return 0;
  166. }
  167. static int mlx4_en_dcbnl_ieee_getpfc(struct net_device *dev,
  168. struct ieee_pfc *pfc)
  169. {
  170. struct mlx4_en_priv *priv = netdev_priv(dev);
  171. pfc->pfc_cap = IEEE_8021QAZ_MAX_TCS;
  172. pfc->pfc_en = priv->prof->tx_ppp;
  173. return 0;
  174. }
  175. static int mlx4_en_dcbnl_ieee_setpfc(struct net_device *dev,
  176. struct ieee_pfc *pfc)
  177. {
  178. struct mlx4_en_priv *priv = netdev_priv(dev);
  179. struct mlx4_en_port_profile *prof = priv->prof;
  180. struct mlx4_en_dev *mdev = priv->mdev;
  181. int err;
  182. en_dbg(DRV, priv, "cap: 0x%x en: 0x%x mbc: 0x%x delay: %d\n",
  183. pfc->pfc_cap,
  184. pfc->pfc_en,
  185. pfc->mbc,
  186. pfc->delay);
  187. prof->rx_pause = !pfc->pfc_en;
  188. prof->tx_pause = !pfc->pfc_en;
  189. prof->rx_ppp = pfc->pfc_en;
  190. prof->tx_ppp = pfc->pfc_en;
  191. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  192. priv->rx_skb_size + ETH_FCS_LEN,
  193. prof->tx_pause,
  194. prof->tx_ppp,
  195. prof->rx_pause,
  196. prof->rx_ppp);
  197. if (err)
  198. en_err(priv, "Failed setting pause params\n");
  199. else
  200. mlx4_en_update_pfc_stats_bitmap(mdev->dev, &priv->stats_bitmap,
  201. prof->rx_ppp, prof->rx_pause,
  202. prof->tx_ppp, prof->tx_pause);
  203. return err;
  204. }
  205. static u8 mlx4_en_dcbnl_getdcbx(struct net_device *dev)
  206. {
  207. return DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
  208. }
  209. static u8 mlx4_en_dcbnl_setdcbx(struct net_device *dev, u8 mode)
  210. {
  211. if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
  212. (mode & DCB_CAP_DCBX_VER_CEE) ||
  213. !(mode & DCB_CAP_DCBX_VER_IEEE) ||
  214. !(mode & DCB_CAP_DCBX_HOST))
  215. return 1;
  216. return 0;
  217. }
  218. #define MLX4_RATELIMIT_UNITS_IN_KB 100000 /* rate-limit HW unit in Kbps */
  219. static int mlx4_en_dcbnl_ieee_getmaxrate(struct net_device *dev,
  220. struct ieee_maxrate *maxrate)
  221. {
  222. struct mlx4_en_priv *priv = netdev_priv(dev);
  223. int i;
  224. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  225. maxrate->tc_maxrate[i] =
  226. priv->maxrate[i] * MLX4_RATELIMIT_UNITS_IN_KB;
  227. return 0;
  228. }
  229. static int mlx4_en_dcbnl_ieee_setmaxrate(struct net_device *dev,
  230. struct ieee_maxrate *maxrate)
  231. {
  232. struct mlx4_en_priv *priv = netdev_priv(dev);
  233. u16 tmp[IEEE_8021QAZ_MAX_TCS];
  234. int i, err;
  235. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  236. /* Convert from Kbps into HW units, rounding result up.
  237. * Setting to 0, means unlimited BW.
  238. */
  239. tmp[i] = div_u64(maxrate->tc_maxrate[i] +
  240. MLX4_RATELIMIT_UNITS_IN_KB - 1,
  241. MLX4_RATELIMIT_UNITS_IN_KB);
  242. }
  243. err = mlx4_en_config_port_scheduler(priv, NULL, tmp);
  244. if (err)
  245. return err;
  246. memcpy(priv->maxrate, tmp, sizeof(priv->maxrate));
  247. return 0;
  248. }
  249. #define RPG_ENABLE_BIT 31
  250. #define CN_TAG_BIT 30
  251. static int mlx4_en_dcbnl_ieee_getqcn(struct net_device *dev,
  252. struct ieee_qcn *qcn)
  253. {
  254. struct mlx4_en_priv *priv = netdev_priv(dev);
  255. struct mlx4_congestion_control_mb_prio_802_1_qau_params *hw_qcn;
  256. struct mlx4_cmd_mailbox *mailbox_out = NULL;
  257. u64 mailbox_in_dma = 0;
  258. u32 inmod = 0;
  259. int i, err;
  260. if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QCN))
  261. return -EOPNOTSUPP;
  262. mailbox_out = mlx4_alloc_cmd_mailbox(priv->mdev->dev);
  263. if (IS_ERR(mailbox_out))
  264. return -ENOMEM;
  265. hw_qcn =
  266. (struct mlx4_congestion_control_mb_prio_802_1_qau_params *)
  267. mailbox_out->buf;
  268. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  269. inmod = priv->port | ((1 << i) << 8) |
  270. (MLX4_CTRL_ALGO_802_1_QAU_REACTION_POINT << 16);
  271. err = mlx4_cmd_box(priv->mdev->dev, mailbox_in_dma,
  272. mailbox_out->dma,
  273. inmod, MLX4_CONGESTION_CONTROL_GET_PARAMS,
  274. MLX4_CMD_CONGESTION_CTRL_OPCODE,
  275. MLX4_CMD_TIME_CLASS_C,
  276. MLX4_CMD_NATIVE);
  277. if (err) {
  278. mlx4_free_cmd_mailbox(priv->mdev->dev, mailbox_out);
  279. return err;
  280. }
  281. qcn->rpg_enable[i] =
  282. be32_to_cpu(hw_qcn->extended_enable) >> RPG_ENABLE_BIT;
  283. qcn->rppp_max_rps[i] =
  284. be32_to_cpu(hw_qcn->rppp_max_rps);
  285. qcn->rpg_time_reset[i] =
  286. be32_to_cpu(hw_qcn->rpg_time_reset);
  287. qcn->rpg_byte_reset[i] =
  288. be32_to_cpu(hw_qcn->rpg_byte_reset);
  289. qcn->rpg_threshold[i] =
  290. be32_to_cpu(hw_qcn->rpg_threshold);
  291. qcn->rpg_max_rate[i] =
  292. be32_to_cpu(hw_qcn->rpg_max_rate);
  293. qcn->rpg_ai_rate[i] =
  294. be32_to_cpu(hw_qcn->rpg_ai_rate);
  295. qcn->rpg_hai_rate[i] =
  296. be32_to_cpu(hw_qcn->rpg_hai_rate);
  297. qcn->rpg_gd[i] =
  298. be32_to_cpu(hw_qcn->rpg_gd);
  299. qcn->rpg_min_dec_fac[i] =
  300. be32_to_cpu(hw_qcn->rpg_min_dec_fac);
  301. qcn->rpg_min_rate[i] =
  302. be32_to_cpu(hw_qcn->rpg_min_rate);
  303. qcn->cndd_state_machine[i] =
  304. priv->cndd_state[i];
  305. }
  306. mlx4_free_cmd_mailbox(priv->mdev->dev, mailbox_out);
  307. return 0;
  308. }
  309. static int mlx4_en_dcbnl_ieee_setqcn(struct net_device *dev,
  310. struct ieee_qcn *qcn)
  311. {
  312. struct mlx4_en_priv *priv = netdev_priv(dev);
  313. struct mlx4_congestion_control_mb_prio_802_1_qau_params *hw_qcn;
  314. struct mlx4_cmd_mailbox *mailbox_in = NULL;
  315. u64 mailbox_in_dma = 0;
  316. u32 inmod = 0;
  317. int i, err;
  318. #define MODIFY_ENABLE_HIGH_MASK 0xc0000000
  319. #define MODIFY_ENABLE_LOW_MASK 0xffc00000
  320. if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QCN))
  321. return -EOPNOTSUPP;
  322. mailbox_in = mlx4_alloc_cmd_mailbox(priv->mdev->dev);
  323. if (IS_ERR(mailbox_in))
  324. return -ENOMEM;
  325. mailbox_in_dma = mailbox_in->dma;
  326. hw_qcn =
  327. (struct mlx4_congestion_control_mb_prio_802_1_qau_params *)mailbox_in->buf;
  328. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  329. inmod = priv->port | ((1 << i) << 8) |
  330. (MLX4_CTRL_ALGO_802_1_QAU_REACTION_POINT << 16);
  331. /* Before updating QCN parameter,
  332. * need to set it's modify enable bit to 1
  333. */
  334. hw_qcn->modify_enable_high = cpu_to_be32(
  335. MODIFY_ENABLE_HIGH_MASK);
  336. hw_qcn->modify_enable_low = cpu_to_be32(MODIFY_ENABLE_LOW_MASK);
  337. hw_qcn->extended_enable = cpu_to_be32(qcn->rpg_enable[i] << RPG_ENABLE_BIT);
  338. hw_qcn->rppp_max_rps = cpu_to_be32(qcn->rppp_max_rps[i]);
  339. hw_qcn->rpg_time_reset = cpu_to_be32(qcn->rpg_time_reset[i]);
  340. hw_qcn->rpg_byte_reset = cpu_to_be32(qcn->rpg_byte_reset[i]);
  341. hw_qcn->rpg_threshold = cpu_to_be32(qcn->rpg_threshold[i]);
  342. hw_qcn->rpg_max_rate = cpu_to_be32(qcn->rpg_max_rate[i]);
  343. hw_qcn->rpg_ai_rate = cpu_to_be32(qcn->rpg_ai_rate[i]);
  344. hw_qcn->rpg_hai_rate = cpu_to_be32(qcn->rpg_hai_rate[i]);
  345. hw_qcn->rpg_gd = cpu_to_be32(qcn->rpg_gd[i]);
  346. hw_qcn->rpg_min_dec_fac = cpu_to_be32(qcn->rpg_min_dec_fac[i]);
  347. hw_qcn->rpg_min_rate = cpu_to_be32(qcn->rpg_min_rate[i]);
  348. priv->cndd_state[i] = qcn->cndd_state_machine[i];
  349. if (qcn->cndd_state_machine[i] == DCB_CNDD_INTERIOR_READY)
  350. hw_qcn->extended_enable |= cpu_to_be32(1 << CN_TAG_BIT);
  351. err = mlx4_cmd(priv->mdev->dev, mailbox_in_dma, inmod,
  352. MLX4_CONGESTION_CONTROL_SET_PARAMS,
  353. MLX4_CMD_CONGESTION_CTRL_OPCODE,
  354. MLX4_CMD_TIME_CLASS_C,
  355. MLX4_CMD_NATIVE);
  356. if (err) {
  357. mlx4_free_cmd_mailbox(priv->mdev->dev, mailbox_in);
  358. return err;
  359. }
  360. }
  361. mlx4_free_cmd_mailbox(priv->mdev->dev, mailbox_in);
  362. return 0;
  363. }
  364. static int mlx4_en_dcbnl_ieee_getqcnstats(struct net_device *dev,
  365. struct ieee_qcn_stats *qcn_stats)
  366. {
  367. struct mlx4_en_priv *priv = netdev_priv(dev);
  368. struct mlx4_congestion_control_mb_prio_802_1_qau_statistics *hw_qcn_stats;
  369. struct mlx4_cmd_mailbox *mailbox_out = NULL;
  370. u64 mailbox_in_dma = 0;
  371. u32 inmod = 0;
  372. int i, err;
  373. if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QCN))
  374. return -EOPNOTSUPP;
  375. mailbox_out = mlx4_alloc_cmd_mailbox(priv->mdev->dev);
  376. if (IS_ERR(mailbox_out))
  377. return -ENOMEM;
  378. hw_qcn_stats =
  379. (struct mlx4_congestion_control_mb_prio_802_1_qau_statistics *)
  380. mailbox_out->buf;
  381. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  382. inmod = priv->port | ((1 << i) << 8) |
  383. (MLX4_CTRL_ALGO_802_1_QAU_REACTION_POINT << 16);
  384. err = mlx4_cmd_box(priv->mdev->dev, mailbox_in_dma,
  385. mailbox_out->dma, inmod,
  386. MLX4_CONGESTION_CONTROL_GET_STATISTICS,
  387. MLX4_CMD_CONGESTION_CTRL_OPCODE,
  388. MLX4_CMD_TIME_CLASS_C,
  389. MLX4_CMD_NATIVE);
  390. if (err) {
  391. mlx4_free_cmd_mailbox(priv->mdev->dev, mailbox_out);
  392. return err;
  393. }
  394. qcn_stats->rppp_rp_centiseconds[i] =
  395. be64_to_cpu(hw_qcn_stats->rppp_rp_centiseconds);
  396. qcn_stats->rppp_created_rps[i] =
  397. be32_to_cpu(hw_qcn_stats->rppp_created_rps);
  398. }
  399. mlx4_free_cmd_mailbox(priv->mdev->dev, mailbox_out);
  400. return 0;
  401. }
  402. const struct dcbnl_rtnl_ops mlx4_en_dcbnl_ops = {
  403. .ieee_getets = mlx4_en_dcbnl_ieee_getets,
  404. .ieee_setets = mlx4_en_dcbnl_ieee_setets,
  405. .ieee_getmaxrate = mlx4_en_dcbnl_ieee_getmaxrate,
  406. .ieee_setmaxrate = mlx4_en_dcbnl_ieee_setmaxrate,
  407. .ieee_getpfc = mlx4_en_dcbnl_ieee_getpfc,
  408. .ieee_setpfc = mlx4_en_dcbnl_ieee_setpfc,
  409. .getdcbx = mlx4_en_dcbnl_getdcbx,
  410. .setdcbx = mlx4_en_dcbnl_setdcbx,
  411. .ieee_getqcn = mlx4_en_dcbnl_ieee_getqcn,
  412. .ieee_setqcn = mlx4_en_dcbnl_ieee_setqcn,
  413. .ieee_getqcnstats = mlx4_en_dcbnl_ieee_getqcnstats,
  414. };
  415. const struct dcbnl_rtnl_ops mlx4_en_dcbnl_pfc_ops = {
  416. .ieee_getpfc = mlx4_en_dcbnl_ieee_getpfc,
  417. .ieee_setpfc = mlx4_en_dcbnl_ieee_setpfc,
  418. .getdcbx = mlx4_en_dcbnl_getdcbx,
  419. .setdcbx = mlx4_en_dcbnl_setdcbx,
  420. };