spectrum_dcb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxsw/spectrum_dcb.c
  3. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2016 Ido Schimmel <idosch@mellanox.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/netdevice.h>
  35. #include <linux/string.h>
  36. #include <linux/bitops.h>
  37. #include <net/dcbnl.h>
  38. #include "spectrum.h"
  39. #include "reg.h"
  40. static u8 mlxsw_sp_dcbnl_getdcbx(struct net_device __always_unused *dev)
  41. {
  42. return DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
  43. }
  44. static u8 mlxsw_sp_dcbnl_setdcbx(struct net_device __always_unused *dev,
  45. u8 mode)
  46. {
  47. return (mode != (DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE)) ? 1 : 0;
  48. }
  49. static int mlxsw_sp_dcbnl_ieee_getets(struct net_device *dev,
  50. struct ieee_ets *ets)
  51. {
  52. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  53. memcpy(ets, mlxsw_sp_port->dcb.ets, sizeof(*ets));
  54. return 0;
  55. }
  56. static int mlxsw_sp_port_ets_validate(struct mlxsw_sp_port *mlxsw_sp_port,
  57. struct ieee_ets *ets)
  58. {
  59. struct net_device *dev = mlxsw_sp_port->dev;
  60. bool has_ets_tc = false;
  61. int i, tx_bw_sum = 0;
  62. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  63. switch (ets->tc_tsa[i]) {
  64. case IEEE_8021QAZ_TSA_STRICT:
  65. break;
  66. case IEEE_8021QAZ_TSA_ETS:
  67. has_ets_tc = true;
  68. tx_bw_sum += ets->tc_tx_bw[i];
  69. break;
  70. default:
  71. netdev_err(dev, "Only strict priority and ETS are supported\n");
  72. return -EINVAL;
  73. }
  74. if (ets->prio_tc[i] >= IEEE_8021QAZ_MAX_TCS) {
  75. netdev_err(dev, "Invalid TC\n");
  76. return -EINVAL;
  77. }
  78. }
  79. if (has_ets_tc && tx_bw_sum != 100) {
  80. netdev_err(dev, "Total ETS bandwidth should equal 100\n");
  81. return -EINVAL;
  82. }
  83. return 0;
  84. }
  85. static int mlxsw_sp_port_pg_prio_map(struct mlxsw_sp_port *mlxsw_sp_port,
  86. u8 *prio_tc)
  87. {
  88. char pptb_pl[MLXSW_REG_PPTB_LEN];
  89. int i;
  90. mlxsw_reg_pptb_pack(pptb_pl, mlxsw_sp_port->local_port);
  91. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  92. mlxsw_reg_pptb_prio_to_buff_pack(pptb_pl, i, prio_tc[i]);
  93. return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pptb),
  94. pptb_pl);
  95. }
  96. static bool mlxsw_sp_ets_has_pg(u8 *prio_tc, u8 pg)
  97. {
  98. int i;
  99. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  100. if (prio_tc[i] == pg)
  101. return true;
  102. return false;
  103. }
  104. static int mlxsw_sp_port_pg_destroy(struct mlxsw_sp_port *mlxsw_sp_port,
  105. u8 *old_prio_tc, u8 *new_prio_tc)
  106. {
  107. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  108. char pbmc_pl[MLXSW_REG_PBMC_LEN];
  109. int err, i;
  110. mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0);
  111. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
  112. if (err)
  113. return err;
  114. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  115. u8 pg = old_prio_tc[i];
  116. if (!mlxsw_sp_ets_has_pg(new_prio_tc, pg))
  117. mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, pg, 0);
  118. }
  119. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
  120. }
  121. static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port,
  122. struct ieee_ets *ets)
  123. {
  124. bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
  125. struct ieee_ets *my_ets = mlxsw_sp_port->dcb.ets;
  126. struct net_device *dev = mlxsw_sp_port->dev;
  127. int err;
  128. /* Create the required PGs, but don't destroy existing ones, as
  129. * traffic is still directed to them.
  130. */
  131. err = __mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu,
  132. ets->prio_tc, pause_en,
  133. mlxsw_sp_port->dcb.pfc);
  134. if (err) {
  135. netdev_err(dev, "Failed to configure port's headroom\n");
  136. return err;
  137. }
  138. err = mlxsw_sp_port_pg_prio_map(mlxsw_sp_port, ets->prio_tc);
  139. if (err) {
  140. netdev_err(dev, "Failed to set PG-priority mapping\n");
  141. goto err_port_prio_pg_map;
  142. }
  143. err = mlxsw_sp_port_pg_destroy(mlxsw_sp_port, my_ets->prio_tc,
  144. ets->prio_tc);
  145. if (err)
  146. netdev_warn(dev, "Failed to remove ununsed PGs\n");
  147. return 0;
  148. err_port_prio_pg_map:
  149. mlxsw_sp_port_pg_destroy(mlxsw_sp_port, ets->prio_tc, my_ets->prio_tc);
  150. return err;
  151. }
  152. static int __mlxsw_sp_dcbnl_ieee_setets(struct mlxsw_sp_port *mlxsw_sp_port,
  153. struct ieee_ets *ets)
  154. {
  155. struct ieee_ets *my_ets = mlxsw_sp_port->dcb.ets;
  156. struct net_device *dev = mlxsw_sp_port->dev;
  157. int i, err;
  158. /* Egress configuration. */
  159. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  160. bool dwrr = ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS;
  161. u8 weight = ets->tc_tx_bw[i];
  162. err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
  163. MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
  164. 0, dwrr, weight);
  165. if (err) {
  166. netdev_err(dev, "Failed to link subgroup ETS element %d to group\n",
  167. i);
  168. goto err_port_ets_set;
  169. }
  170. }
  171. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  172. err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i,
  173. ets->prio_tc[i]);
  174. if (err) {
  175. netdev_err(dev, "Failed to map prio %d to TC %d\n", i,
  176. ets->prio_tc[i]);
  177. goto err_port_prio_tc_set;
  178. }
  179. }
  180. /* Ingress configuration. */
  181. err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, ets);
  182. if (err)
  183. goto err_port_headroom_set;
  184. return 0;
  185. err_port_headroom_set:
  186. i = IEEE_8021QAZ_MAX_TCS;
  187. err_port_prio_tc_set:
  188. for (i--; i >= 0; i--)
  189. mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, my_ets->prio_tc[i]);
  190. i = IEEE_8021QAZ_MAX_TCS;
  191. err_port_ets_set:
  192. for (i--; i >= 0; i--) {
  193. bool dwrr = my_ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS;
  194. u8 weight = my_ets->tc_tx_bw[i];
  195. err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
  196. MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
  197. 0, dwrr, weight);
  198. }
  199. return err;
  200. }
  201. static int mlxsw_sp_dcbnl_ieee_setets(struct net_device *dev,
  202. struct ieee_ets *ets)
  203. {
  204. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  205. int err;
  206. err = mlxsw_sp_port_ets_validate(mlxsw_sp_port, ets);
  207. if (err)
  208. return err;
  209. err = __mlxsw_sp_dcbnl_ieee_setets(mlxsw_sp_port, ets);
  210. if (err)
  211. return err;
  212. memcpy(mlxsw_sp_port->dcb.ets, ets, sizeof(*ets));
  213. mlxsw_sp_port->dcb.ets->ets_cap = IEEE_8021QAZ_MAX_TCS;
  214. return 0;
  215. }
  216. static int mlxsw_sp_dcbnl_ieee_getmaxrate(struct net_device *dev,
  217. struct ieee_maxrate *maxrate)
  218. {
  219. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  220. memcpy(maxrate, mlxsw_sp_port->dcb.maxrate, sizeof(*maxrate));
  221. return 0;
  222. }
  223. static int mlxsw_sp_dcbnl_ieee_setmaxrate(struct net_device *dev,
  224. struct ieee_maxrate *maxrate)
  225. {
  226. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  227. struct ieee_maxrate *my_maxrate = mlxsw_sp_port->dcb.maxrate;
  228. int err, i;
  229. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  230. err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
  231. MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
  232. i, 0,
  233. maxrate->tc_maxrate[i]);
  234. if (err) {
  235. netdev_err(dev, "Failed to set maxrate for TC %d\n", i);
  236. goto err_port_ets_maxrate_set;
  237. }
  238. }
  239. memcpy(mlxsw_sp_port->dcb.maxrate, maxrate, sizeof(*maxrate));
  240. return 0;
  241. err_port_ets_maxrate_set:
  242. for (i--; i >= 0; i--)
  243. mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
  244. MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
  245. i, 0, my_maxrate->tc_maxrate[i]);
  246. return err;
  247. }
  248. static int mlxsw_sp_port_pfc_cnt_get(struct mlxsw_sp_port *mlxsw_sp_port,
  249. u8 prio)
  250. {
  251. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  252. struct ieee_pfc *my_pfc = mlxsw_sp_port->dcb.pfc;
  253. char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
  254. int err;
  255. mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port,
  256. MLXSW_REG_PPCNT_PRIO_CNT, prio);
  257. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
  258. if (err)
  259. return err;
  260. my_pfc->requests[prio] = mlxsw_reg_ppcnt_tx_pause_get(ppcnt_pl);
  261. my_pfc->indications[prio] = mlxsw_reg_ppcnt_rx_pause_get(ppcnt_pl);
  262. return 0;
  263. }
  264. static int mlxsw_sp_dcbnl_ieee_getpfc(struct net_device *dev,
  265. struct ieee_pfc *pfc)
  266. {
  267. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  268. int err, i;
  269. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  270. err = mlxsw_sp_port_pfc_cnt_get(mlxsw_sp_port, i);
  271. if (err) {
  272. netdev_err(dev, "Failed to get PFC count for priority %d\n",
  273. i);
  274. return err;
  275. }
  276. }
  277. memcpy(pfc, mlxsw_sp_port->dcb.pfc, sizeof(*pfc));
  278. return 0;
  279. }
  280. static int mlxsw_sp_port_pfc_set(struct mlxsw_sp_port *mlxsw_sp_port,
  281. struct ieee_pfc *pfc)
  282. {
  283. char pfcc_pl[MLXSW_REG_PFCC_LEN];
  284. mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port);
  285. mlxsw_reg_pfcc_pprx_set(pfcc_pl, mlxsw_sp_port->link.rx_pause);
  286. mlxsw_reg_pfcc_pptx_set(pfcc_pl, mlxsw_sp_port->link.tx_pause);
  287. mlxsw_reg_pfcc_prio_pack(pfcc_pl, pfc->pfc_en);
  288. return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc),
  289. pfcc_pl);
  290. }
  291. static int mlxsw_sp_dcbnl_ieee_setpfc(struct net_device *dev,
  292. struct ieee_pfc *pfc)
  293. {
  294. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  295. bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
  296. int err;
  297. if (pause_en && pfc->pfc_en) {
  298. netdev_err(dev, "PAUSE frames already enabled on port\n");
  299. return -EINVAL;
  300. }
  301. err = __mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu,
  302. mlxsw_sp_port->dcb.ets->prio_tc,
  303. pause_en, pfc);
  304. if (err) {
  305. netdev_err(dev, "Failed to configure port's headroom for PFC\n");
  306. return err;
  307. }
  308. err = mlxsw_sp_port_pfc_set(mlxsw_sp_port, pfc);
  309. if (err) {
  310. netdev_err(dev, "Failed to configure PFC\n");
  311. goto err_port_pfc_set;
  312. }
  313. memcpy(mlxsw_sp_port->dcb.pfc, pfc, sizeof(*pfc));
  314. mlxsw_sp_port->dcb.pfc->pfc_cap = IEEE_8021QAZ_MAX_TCS;
  315. return 0;
  316. err_port_pfc_set:
  317. __mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu,
  318. mlxsw_sp_port->dcb.ets->prio_tc, pause_en,
  319. mlxsw_sp_port->dcb.pfc);
  320. return err;
  321. }
  322. static const struct dcbnl_rtnl_ops mlxsw_sp_dcbnl_ops = {
  323. .ieee_getets = mlxsw_sp_dcbnl_ieee_getets,
  324. .ieee_setets = mlxsw_sp_dcbnl_ieee_setets,
  325. .ieee_getmaxrate = mlxsw_sp_dcbnl_ieee_getmaxrate,
  326. .ieee_setmaxrate = mlxsw_sp_dcbnl_ieee_setmaxrate,
  327. .ieee_getpfc = mlxsw_sp_dcbnl_ieee_getpfc,
  328. .ieee_setpfc = mlxsw_sp_dcbnl_ieee_setpfc,
  329. .getdcbx = mlxsw_sp_dcbnl_getdcbx,
  330. .setdcbx = mlxsw_sp_dcbnl_setdcbx,
  331. };
  332. static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
  333. {
  334. mlxsw_sp_port->dcb.ets = kzalloc(sizeof(*mlxsw_sp_port->dcb.ets),
  335. GFP_KERNEL);
  336. if (!mlxsw_sp_port->dcb.ets)
  337. return -ENOMEM;
  338. mlxsw_sp_port->dcb.ets->ets_cap = IEEE_8021QAZ_MAX_TCS;
  339. return 0;
  340. }
  341. static void mlxsw_sp_port_ets_fini(struct mlxsw_sp_port *mlxsw_sp_port)
  342. {
  343. kfree(mlxsw_sp_port->dcb.ets);
  344. }
  345. static int mlxsw_sp_port_maxrate_init(struct mlxsw_sp_port *mlxsw_sp_port)
  346. {
  347. int i;
  348. mlxsw_sp_port->dcb.maxrate = kmalloc(sizeof(*mlxsw_sp_port->dcb.maxrate),
  349. GFP_KERNEL);
  350. if (!mlxsw_sp_port->dcb.maxrate)
  351. return -ENOMEM;
  352. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  353. mlxsw_sp_port->dcb.maxrate->tc_maxrate[i] = MLXSW_REG_QEEC_MAS_DIS;
  354. return 0;
  355. }
  356. static void mlxsw_sp_port_maxrate_fini(struct mlxsw_sp_port *mlxsw_sp_port)
  357. {
  358. kfree(mlxsw_sp_port->dcb.maxrate);
  359. }
  360. static int mlxsw_sp_port_pfc_init(struct mlxsw_sp_port *mlxsw_sp_port)
  361. {
  362. mlxsw_sp_port->dcb.pfc = kzalloc(sizeof(*mlxsw_sp_port->dcb.pfc),
  363. GFP_KERNEL);
  364. if (!mlxsw_sp_port->dcb.pfc)
  365. return -ENOMEM;
  366. mlxsw_sp_port->dcb.pfc->pfc_cap = IEEE_8021QAZ_MAX_TCS;
  367. return 0;
  368. }
  369. static void mlxsw_sp_port_pfc_fini(struct mlxsw_sp_port *mlxsw_sp_port)
  370. {
  371. kfree(mlxsw_sp_port->dcb.pfc);
  372. }
  373. int mlxsw_sp_port_dcb_init(struct mlxsw_sp_port *mlxsw_sp_port)
  374. {
  375. int err;
  376. err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
  377. if (err)
  378. return err;
  379. err = mlxsw_sp_port_maxrate_init(mlxsw_sp_port);
  380. if (err)
  381. goto err_port_maxrate_init;
  382. err = mlxsw_sp_port_pfc_init(mlxsw_sp_port);
  383. if (err)
  384. goto err_port_pfc_init;
  385. mlxsw_sp_port->dev->dcbnl_ops = &mlxsw_sp_dcbnl_ops;
  386. return 0;
  387. err_port_pfc_init:
  388. mlxsw_sp_port_maxrate_fini(mlxsw_sp_port);
  389. err_port_maxrate_init:
  390. mlxsw_sp_port_ets_fini(mlxsw_sp_port);
  391. return err;
  392. }
  393. void mlxsw_sp_port_dcb_fini(struct mlxsw_sp_port *mlxsw_sp_port)
  394. {
  395. mlxsw_sp_port_pfc_fini(mlxsw_sp_port);
  396. mlxsw_sp_port_maxrate_fini(mlxsw_sp_port);
  397. mlxsw_sp_port_ets_fini(mlxsw_sp_port);
  398. }