spectrum_dcb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /* Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved */
  3. #include <linux/netdevice.h>
  4. #include <linux/string.h>
  5. #include <linux/bitops.h>
  6. #include <net/dcbnl.h>
  7. #include "spectrum.h"
  8. #include "reg.h"
  9. static u8 mlxsw_sp_dcbnl_getdcbx(struct net_device __always_unused *dev)
  10. {
  11. return DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
  12. }
  13. static u8 mlxsw_sp_dcbnl_setdcbx(struct net_device __always_unused *dev,
  14. u8 mode)
  15. {
  16. return (mode != (DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE)) ? 1 : 0;
  17. }
  18. static int mlxsw_sp_dcbnl_ieee_getets(struct net_device *dev,
  19. struct ieee_ets *ets)
  20. {
  21. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  22. memcpy(ets, mlxsw_sp_port->dcb.ets, sizeof(*ets));
  23. return 0;
  24. }
  25. static int mlxsw_sp_port_ets_validate(struct mlxsw_sp_port *mlxsw_sp_port,
  26. struct ieee_ets *ets)
  27. {
  28. struct net_device *dev = mlxsw_sp_port->dev;
  29. bool has_ets_tc = false;
  30. int i, tx_bw_sum = 0;
  31. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  32. switch (ets->tc_tsa[i]) {
  33. case IEEE_8021QAZ_TSA_STRICT:
  34. break;
  35. case IEEE_8021QAZ_TSA_ETS:
  36. has_ets_tc = true;
  37. tx_bw_sum += ets->tc_tx_bw[i];
  38. break;
  39. default:
  40. netdev_err(dev, "Only strict priority and ETS are supported\n");
  41. return -EINVAL;
  42. }
  43. if (ets->prio_tc[i] >= IEEE_8021QAZ_MAX_TCS) {
  44. netdev_err(dev, "Invalid TC\n");
  45. return -EINVAL;
  46. }
  47. }
  48. if (has_ets_tc && tx_bw_sum != 100) {
  49. netdev_err(dev, "Total ETS bandwidth should equal 100\n");
  50. return -EINVAL;
  51. }
  52. return 0;
  53. }
  54. static int mlxsw_sp_port_pg_prio_map(struct mlxsw_sp_port *mlxsw_sp_port,
  55. u8 *prio_tc)
  56. {
  57. char pptb_pl[MLXSW_REG_PPTB_LEN];
  58. int i;
  59. mlxsw_reg_pptb_pack(pptb_pl, mlxsw_sp_port->local_port);
  60. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  61. mlxsw_reg_pptb_prio_to_buff_pack(pptb_pl, i, prio_tc[i]);
  62. return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pptb),
  63. pptb_pl);
  64. }
  65. static bool mlxsw_sp_ets_has_pg(u8 *prio_tc, u8 pg)
  66. {
  67. int i;
  68. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  69. if (prio_tc[i] == pg)
  70. return true;
  71. return false;
  72. }
  73. static int mlxsw_sp_port_pg_destroy(struct mlxsw_sp_port *mlxsw_sp_port,
  74. u8 *old_prio_tc, u8 *new_prio_tc)
  75. {
  76. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  77. char pbmc_pl[MLXSW_REG_PBMC_LEN];
  78. int err, i;
  79. mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0);
  80. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
  81. if (err)
  82. return err;
  83. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  84. u8 pg = old_prio_tc[i];
  85. if (!mlxsw_sp_ets_has_pg(new_prio_tc, pg))
  86. mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, pg, 0);
  87. }
  88. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
  89. }
  90. static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port,
  91. struct ieee_ets *ets)
  92. {
  93. bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
  94. struct ieee_ets *my_ets = mlxsw_sp_port->dcb.ets;
  95. struct net_device *dev = mlxsw_sp_port->dev;
  96. int err;
  97. /* Create the required PGs, but don't destroy existing ones, as
  98. * traffic is still directed to them.
  99. */
  100. err = __mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu,
  101. ets->prio_tc, pause_en,
  102. mlxsw_sp_port->dcb.pfc);
  103. if (err) {
  104. netdev_err(dev, "Failed to configure port's headroom\n");
  105. return err;
  106. }
  107. err = mlxsw_sp_port_pg_prio_map(mlxsw_sp_port, ets->prio_tc);
  108. if (err) {
  109. netdev_err(dev, "Failed to set PG-priority mapping\n");
  110. goto err_port_prio_pg_map;
  111. }
  112. err = mlxsw_sp_port_pg_destroy(mlxsw_sp_port, my_ets->prio_tc,
  113. ets->prio_tc);
  114. if (err)
  115. netdev_warn(dev, "Failed to remove ununsed PGs\n");
  116. return 0;
  117. err_port_prio_pg_map:
  118. mlxsw_sp_port_pg_destroy(mlxsw_sp_port, ets->prio_tc, my_ets->prio_tc);
  119. return err;
  120. }
  121. static int __mlxsw_sp_dcbnl_ieee_setets(struct mlxsw_sp_port *mlxsw_sp_port,
  122. struct ieee_ets *ets)
  123. {
  124. struct ieee_ets *my_ets = mlxsw_sp_port->dcb.ets;
  125. struct net_device *dev = mlxsw_sp_port->dev;
  126. int i, err;
  127. /* Egress configuration. */
  128. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  129. bool dwrr = ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS;
  130. u8 weight = ets->tc_tx_bw[i];
  131. err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
  132. MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
  133. 0, dwrr, weight);
  134. if (err) {
  135. netdev_err(dev, "Failed to link subgroup ETS element %d to group\n",
  136. i);
  137. goto err_port_ets_set;
  138. }
  139. }
  140. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  141. err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i,
  142. ets->prio_tc[i]);
  143. if (err) {
  144. netdev_err(dev, "Failed to map prio %d to TC %d\n", i,
  145. ets->prio_tc[i]);
  146. goto err_port_prio_tc_set;
  147. }
  148. }
  149. /* Ingress configuration. */
  150. err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, ets);
  151. if (err)
  152. goto err_port_headroom_set;
  153. return 0;
  154. err_port_headroom_set:
  155. i = IEEE_8021QAZ_MAX_TCS;
  156. err_port_prio_tc_set:
  157. for (i--; i >= 0; i--)
  158. mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, my_ets->prio_tc[i]);
  159. i = IEEE_8021QAZ_MAX_TCS;
  160. err_port_ets_set:
  161. for (i--; i >= 0; i--) {
  162. bool dwrr = my_ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS;
  163. u8 weight = my_ets->tc_tx_bw[i];
  164. err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
  165. MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
  166. 0, dwrr, weight);
  167. }
  168. return err;
  169. }
  170. static int mlxsw_sp_dcbnl_ieee_setets(struct net_device *dev,
  171. struct ieee_ets *ets)
  172. {
  173. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  174. int err;
  175. err = mlxsw_sp_port_ets_validate(mlxsw_sp_port, ets);
  176. if (err)
  177. return err;
  178. err = __mlxsw_sp_dcbnl_ieee_setets(mlxsw_sp_port, ets);
  179. if (err)
  180. return err;
  181. memcpy(mlxsw_sp_port->dcb.ets, ets, sizeof(*ets));
  182. mlxsw_sp_port->dcb.ets->ets_cap = IEEE_8021QAZ_MAX_TCS;
  183. return 0;
  184. }
  185. static int mlxsw_sp_dcbnl_app_validate(struct net_device *dev,
  186. struct dcb_app *app)
  187. {
  188. int prio;
  189. if (app->priority >= IEEE_8021QAZ_MAX_TCS) {
  190. netdev_err(dev, "APP entry with priority value %u is invalid\n",
  191. app->priority);
  192. return -EINVAL;
  193. }
  194. switch (app->selector) {
  195. case IEEE_8021QAZ_APP_SEL_DSCP:
  196. if (app->protocol >= 64) {
  197. netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
  198. app->protocol);
  199. return -EINVAL;
  200. }
  201. /* Warn about any DSCP APP entries with the same PID. */
  202. prio = fls(dcb_ieee_getapp_mask(dev, app));
  203. if (prio--) {
  204. if (prio < app->priority)
  205. netdev_warn(dev, "Choosing priority %d for DSCP %d in favor of previously-active value of %d\n",
  206. app->priority, app->protocol, prio);
  207. else if (prio > app->priority)
  208. netdev_warn(dev, "Ignoring new priority %d for DSCP %d in favor of current value of %d\n",
  209. app->priority, app->protocol, prio);
  210. }
  211. break;
  212. case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
  213. if (app->protocol) {
  214. netdev_err(dev, "EtherType APP entries with protocol value != 0 not supported\n");
  215. return -EINVAL;
  216. }
  217. break;
  218. default:
  219. netdev_err(dev, "APP entries with selector %u not supported\n",
  220. app->selector);
  221. return -EINVAL;
  222. }
  223. return 0;
  224. }
  225. static u8
  226. mlxsw_sp_port_dcb_app_default_prio(struct mlxsw_sp_port *mlxsw_sp_port)
  227. {
  228. u8 prio_mask;
  229. prio_mask = dcb_ieee_getapp_default_prio_mask(mlxsw_sp_port->dev);
  230. if (prio_mask)
  231. /* Take the highest configured priority. */
  232. return fls(prio_mask) - 1;
  233. return 0;
  234. }
  235. static void
  236. mlxsw_sp_port_dcb_app_dscp_prio_map(struct mlxsw_sp_port *mlxsw_sp_port,
  237. u8 default_prio,
  238. struct dcb_ieee_app_dscp_map *map)
  239. {
  240. int i;
  241. dcb_ieee_getapp_dscp_prio_mask_map(mlxsw_sp_port->dev, map);
  242. for (i = 0; i < ARRAY_SIZE(map->map); ++i) {
  243. if (map->map[i])
  244. map->map[i] = fls(map->map[i]) - 1;
  245. else
  246. map->map[i] = default_prio;
  247. }
  248. }
  249. static bool
  250. mlxsw_sp_port_dcb_app_prio_dscp_map(struct mlxsw_sp_port *mlxsw_sp_port,
  251. struct dcb_ieee_app_prio_map *map)
  252. {
  253. bool have_dscp = false;
  254. int i;
  255. dcb_ieee_getapp_prio_dscp_mask_map(mlxsw_sp_port->dev, map);
  256. for (i = 0; i < ARRAY_SIZE(map->map); ++i) {
  257. if (map->map[i]) {
  258. map->map[i] = fls64(map->map[i]) - 1;
  259. have_dscp = true;
  260. }
  261. }
  262. return have_dscp;
  263. }
  264. static int
  265. mlxsw_sp_port_dcb_app_update_qpts(struct mlxsw_sp_port *mlxsw_sp_port,
  266. enum mlxsw_reg_qpts_trust_state ts)
  267. {
  268. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  269. char qpts_pl[MLXSW_REG_QPTS_LEN];
  270. mlxsw_reg_qpts_pack(qpts_pl, mlxsw_sp_port->local_port, ts);
  271. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qpts), qpts_pl);
  272. }
  273. static int
  274. mlxsw_sp_port_dcb_app_update_qrwe(struct mlxsw_sp_port *mlxsw_sp_port,
  275. bool rewrite_dscp)
  276. {
  277. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  278. char qrwe_pl[MLXSW_REG_QRWE_LEN];
  279. mlxsw_reg_qrwe_pack(qrwe_pl, mlxsw_sp_port->local_port,
  280. false, rewrite_dscp);
  281. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qrwe), qrwe_pl);
  282. }
  283. static int
  284. mlxsw_sp_port_dcb_toggle_trust(struct mlxsw_sp_port *mlxsw_sp_port,
  285. enum mlxsw_reg_qpts_trust_state ts)
  286. {
  287. bool rewrite_dscp = ts == MLXSW_REG_QPTS_TRUST_STATE_DSCP;
  288. int err;
  289. if (mlxsw_sp_port->dcb.trust_state == ts)
  290. return 0;
  291. err = mlxsw_sp_port_dcb_app_update_qpts(mlxsw_sp_port, ts);
  292. if (err)
  293. return err;
  294. err = mlxsw_sp_port_dcb_app_update_qrwe(mlxsw_sp_port, rewrite_dscp);
  295. if (err)
  296. goto err_update_qrwe;
  297. mlxsw_sp_port->dcb.trust_state = ts;
  298. return 0;
  299. err_update_qrwe:
  300. mlxsw_sp_port_dcb_app_update_qpts(mlxsw_sp_port,
  301. mlxsw_sp_port->dcb.trust_state);
  302. return err;
  303. }
  304. static int
  305. mlxsw_sp_port_dcb_app_update_qpdpm(struct mlxsw_sp_port *mlxsw_sp_port,
  306. struct dcb_ieee_app_dscp_map *map)
  307. {
  308. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  309. char qpdpm_pl[MLXSW_REG_QPDPM_LEN];
  310. short int i;
  311. mlxsw_reg_qpdpm_pack(qpdpm_pl, mlxsw_sp_port->local_port);
  312. for (i = 0; i < ARRAY_SIZE(map->map); ++i)
  313. mlxsw_reg_qpdpm_dscp_pack(qpdpm_pl, i, map->map[i]);
  314. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qpdpm), qpdpm_pl);
  315. }
  316. static int
  317. mlxsw_sp_port_dcb_app_update_qpdsm(struct mlxsw_sp_port *mlxsw_sp_port,
  318. struct dcb_ieee_app_prio_map *map)
  319. {
  320. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  321. char qpdsm_pl[MLXSW_REG_QPDSM_LEN];
  322. short int i;
  323. mlxsw_reg_qpdsm_pack(qpdsm_pl, mlxsw_sp_port->local_port);
  324. for (i = 0; i < ARRAY_SIZE(map->map); ++i)
  325. mlxsw_reg_qpdsm_prio_pack(qpdsm_pl, i, map->map[i]);
  326. return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qpdsm), qpdsm_pl);
  327. }
  328. static int mlxsw_sp_port_dcb_app_update(struct mlxsw_sp_port *mlxsw_sp_port)
  329. {
  330. struct dcb_ieee_app_prio_map prio_map;
  331. struct dcb_ieee_app_dscp_map dscp_map;
  332. u8 default_prio;
  333. bool have_dscp;
  334. int err;
  335. default_prio = mlxsw_sp_port_dcb_app_default_prio(mlxsw_sp_port);
  336. have_dscp = mlxsw_sp_port_dcb_app_prio_dscp_map(mlxsw_sp_port,
  337. &prio_map);
  338. mlxsw_sp_port_dcb_app_dscp_prio_map(mlxsw_sp_port, default_prio,
  339. &dscp_map);
  340. err = mlxsw_sp_port_dcb_app_update_qpdpm(mlxsw_sp_port,
  341. &dscp_map);
  342. if (err) {
  343. netdev_err(mlxsw_sp_port->dev, "Couldn't configure priority map\n");
  344. return err;
  345. }
  346. err = mlxsw_sp_port_dcb_app_update_qpdsm(mlxsw_sp_port,
  347. &prio_map);
  348. if (err) {
  349. netdev_err(mlxsw_sp_port->dev, "Couldn't configure DSCP rewrite map\n");
  350. return err;
  351. }
  352. if (!have_dscp) {
  353. err = mlxsw_sp_port_dcb_toggle_trust(mlxsw_sp_port,
  354. MLXSW_REG_QPTS_TRUST_STATE_PCP);
  355. if (err)
  356. netdev_err(mlxsw_sp_port->dev, "Couldn't switch to trust L2\n");
  357. return err;
  358. }
  359. err = mlxsw_sp_port_dcb_toggle_trust(mlxsw_sp_port,
  360. MLXSW_REG_QPTS_TRUST_STATE_DSCP);
  361. if (err) {
  362. /* A failure to set trust DSCP means that the QPDPM and QPDSM
  363. * maps installed above are not in effect. And since we are here
  364. * attempting to set trust DSCP, we couldn't have attempted to
  365. * switch trust to PCP. Thus no cleanup is necessary.
  366. */
  367. netdev_err(mlxsw_sp_port->dev, "Couldn't switch to trust L3\n");
  368. return err;
  369. }
  370. return 0;
  371. }
  372. static int mlxsw_sp_dcbnl_ieee_setapp(struct net_device *dev,
  373. struct dcb_app *app)
  374. {
  375. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  376. int err;
  377. err = mlxsw_sp_dcbnl_app_validate(dev, app);
  378. if (err)
  379. return err;
  380. err = dcb_ieee_setapp(dev, app);
  381. if (err)
  382. return err;
  383. err = mlxsw_sp_port_dcb_app_update(mlxsw_sp_port);
  384. if (err)
  385. goto err_update;
  386. return 0;
  387. err_update:
  388. dcb_ieee_delapp(dev, app);
  389. return err;
  390. }
  391. static int mlxsw_sp_dcbnl_ieee_delapp(struct net_device *dev,
  392. struct dcb_app *app)
  393. {
  394. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  395. int err;
  396. err = dcb_ieee_delapp(dev, app);
  397. if (err)
  398. return err;
  399. err = mlxsw_sp_port_dcb_app_update(mlxsw_sp_port);
  400. if (err)
  401. netdev_err(dev, "Failed to update DCB APP configuration\n");
  402. return 0;
  403. }
  404. static int mlxsw_sp_dcbnl_ieee_getmaxrate(struct net_device *dev,
  405. struct ieee_maxrate *maxrate)
  406. {
  407. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  408. memcpy(maxrate, mlxsw_sp_port->dcb.maxrate, sizeof(*maxrate));
  409. return 0;
  410. }
  411. static int mlxsw_sp_dcbnl_ieee_setmaxrate(struct net_device *dev,
  412. struct ieee_maxrate *maxrate)
  413. {
  414. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  415. struct ieee_maxrate *my_maxrate = mlxsw_sp_port->dcb.maxrate;
  416. int err, i;
  417. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  418. err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
  419. MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
  420. i, 0,
  421. maxrate->tc_maxrate[i]);
  422. if (err) {
  423. netdev_err(dev, "Failed to set maxrate for TC %d\n", i);
  424. goto err_port_ets_maxrate_set;
  425. }
  426. }
  427. memcpy(mlxsw_sp_port->dcb.maxrate, maxrate, sizeof(*maxrate));
  428. return 0;
  429. err_port_ets_maxrate_set:
  430. for (i--; i >= 0; i--)
  431. mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
  432. MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
  433. i, 0, my_maxrate->tc_maxrate[i]);
  434. return err;
  435. }
  436. static int mlxsw_sp_port_pfc_cnt_get(struct mlxsw_sp_port *mlxsw_sp_port,
  437. u8 prio)
  438. {
  439. struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
  440. struct ieee_pfc *my_pfc = mlxsw_sp_port->dcb.pfc;
  441. char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
  442. int err;
  443. mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port,
  444. MLXSW_REG_PPCNT_PRIO_CNT, prio);
  445. err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
  446. if (err)
  447. return err;
  448. my_pfc->requests[prio] = mlxsw_reg_ppcnt_tx_pause_get(ppcnt_pl);
  449. my_pfc->indications[prio] = mlxsw_reg_ppcnt_rx_pause_get(ppcnt_pl);
  450. return 0;
  451. }
  452. static int mlxsw_sp_dcbnl_ieee_getpfc(struct net_device *dev,
  453. struct ieee_pfc *pfc)
  454. {
  455. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  456. int err, i;
  457. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  458. err = mlxsw_sp_port_pfc_cnt_get(mlxsw_sp_port, i);
  459. if (err) {
  460. netdev_err(dev, "Failed to get PFC count for priority %d\n",
  461. i);
  462. return err;
  463. }
  464. }
  465. memcpy(pfc, mlxsw_sp_port->dcb.pfc, sizeof(*pfc));
  466. return 0;
  467. }
  468. static int mlxsw_sp_port_pfc_set(struct mlxsw_sp_port *mlxsw_sp_port,
  469. struct ieee_pfc *pfc)
  470. {
  471. char pfcc_pl[MLXSW_REG_PFCC_LEN];
  472. mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port);
  473. mlxsw_reg_pfcc_pprx_set(pfcc_pl, mlxsw_sp_port->link.rx_pause);
  474. mlxsw_reg_pfcc_pptx_set(pfcc_pl, mlxsw_sp_port->link.tx_pause);
  475. mlxsw_reg_pfcc_prio_pack(pfcc_pl, pfc->pfc_en);
  476. return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc),
  477. pfcc_pl);
  478. }
  479. static int mlxsw_sp_dcbnl_ieee_setpfc(struct net_device *dev,
  480. struct ieee_pfc *pfc)
  481. {
  482. struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
  483. bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
  484. int err;
  485. if (pause_en && pfc->pfc_en) {
  486. netdev_err(dev, "PAUSE frames already enabled on port\n");
  487. return -EINVAL;
  488. }
  489. err = __mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu,
  490. mlxsw_sp_port->dcb.ets->prio_tc,
  491. pause_en, pfc);
  492. if (err) {
  493. netdev_err(dev, "Failed to configure port's headroom for PFC\n");
  494. return err;
  495. }
  496. err = mlxsw_sp_port_pfc_set(mlxsw_sp_port, pfc);
  497. if (err) {
  498. netdev_err(dev, "Failed to configure PFC\n");
  499. goto err_port_pfc_set;
  500. }
  501. memcpy(mlxsw_sp_port->dcb.pfc, pfc, sizeof(*pfc));
  502. mlxsw_sp_port->dcb.pfc->pfc_cap = IEEE_8021QAZ_MAX_TCS;
  503. return 0;
  504. err_port_pfc_set:
  505. __mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu,
  506. mlxsw_sp_port->dcb.ets->prio_tc, pause_en,
  507. mlxsw_sp_port->dcb.pfc);
  508. return err;
  509. }
  510. static const struct dcbnl_rtnl_ops mlxsw_sp_dcbnl_ops = {
  511. .ieee_getets = mlxsw_sp_dcbnl_ieee_getets,
  512. .ieee_setets = mlxsw_sp_dcbnl_ieee_setets,
  513. .ieee_getmaxrate = mlxsw_sp_dcbnl_ieee_getmaxrate,
  514. .ieee_setmaxrate = mlxsw_sp_dcbnl_ieee_setmaxrate,
  515. .ieee_getpfc = mlxsw_sp_dcbnl_ieee_getpfc,
  516. .ieee_setpfc = mlxsw_sp_dcbnl_ieee_setpfc,
  517. .ieee_setapp = mlxsw_sp_dcbnl_ieee_setapp,
  518. .ieee_delapp = mlxsw_sp_dcbnl_ieee_delapp,
  519. .getdcbx = mlxsw_sp_dcbnl_getdcbx,
  520. .setdcbx = mlxsw_sp_dcbnl_setdcbx,
  521. };
  522. static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
  523. {
  524. mlxsw_sp_port->dcb.ets = kzalloc(sizeof(*mlxsw_sp_port->dcb.ets),
  525. GFP_KERNEL);
  526. if (!mlxsw_sp_port->dcb.ets)
  527. return -ENOMEM;
  528. mlxsw_sp_port->dcb.ets->ets_cap = IEEE_8021QAZ_MAX_TCS;
  529. return 0;
  530. }
  531. static void mlxsw_sp_port_ets_fini(struct mlxsw_sp_port *mlxsw_sp_port)
  532. {
  533. kfree(mlxsw_sp_port->dcb.ets);
  534. }
  535. static int mlxsw_sp_port_maxrate_init(struct mlxsw_sp_port *mlxsw_sp_port)
  536. {
  537. int i;
  538. mlxsw_sp_port->dcb.maxrate = kmalloc(sizeof(*mlxsw_sp_port->dcb.maxrate),
  539. GFP_KERNEL);
  540. if (!mlxsw_sp_port->dcb.maxrate)
  541. return -ENOMEM;
  542. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  543. mlxsw_sp_port->dcb.maxrate->tc_maxrate[i] = MLXSW_REG_QEEC_MAS_DIS;
  544. return 0;
  545. }
  546. static void mlxsw_sp_port_maxrate_fini(struct mlxsw_sp_port *mlxsw_sp_port)
  547. {
  548. kfree(mlxsw_sp_port->dcb.maxrate);
  549. }
  550. static int mlxsw_sp_port_pfc_init(struct mlxsw_sp_port *mlxsw_sp_port)
  551. {
  552. mlxsw_sp_port->dcb.pfc = kzalloc(sizeof(*mlxsw_sp_port->dcb.pfc),
  553. GFP_KERNEL);
  554. if (!mlxsw_sp_port->dcb.pfc)
  555. return -ENOMEM;
  556. mlxsw_sp_port->dcb.pfc->pfc_cap = IEEE_8021QAZ_MAX_TCS;
  557. return 0;
  558. }
  559. static void mlxsw_sp_port_pfc_fini(struct mlxsw_sp_port *mlxsw_sp_port)
  560. {
  561. kfree(mlxsw_sp_port->dcb.pfc);
  562. }
  563. int mlxsw_sp_port_dcb_init(struct mlxsw_sp_port *mlxsw_sp_port)
  564. {
  565. int err;
  566. err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
  567. if (err)
  568. return err;
  569. err = mlxsw_sp_port_maxrate_init(mlxsw_sp_port);
  570. if (err)
  571. goto err_port_maxrate_init;
  572. err = mlxsw_sp_port_pfc_init(mlxsw_sp_port);
  573. if (err)
  574. goto err_port_pfc_init;
  575. mlxsw_sp_port->dcb.trust_state = MLXSW_REG_QPTS_TRUST_STATE_PCP;
  576. mlxsw_sp_port->dev->dcbnl_ops = &mlxsw_sp_dcbnl_ops;
  577. return 0;
  578. err_port_pfc_init:
  579. mlxsw_sp_port_maxrate_fini(mlxsw_sp_port);
  580. err_port_maxrate_init:
  581. mlxsw_sp_port_ets_fini(mlxsw_sp_port);
  582. return err;
  583. }
  584. void mlxsw_sp_port_dcb_fini(struct mlxsw_sp_port *mlxsw_sp_port)
  585. {
  586. mlxsw_sp_port_pfc_fini(mlxsw_sp_port);
  587. mlxsw_sp_port_maxrate_fini(mlxsw_sp_port);
  588. mlxsw_sp_port_ets_fini(mlxsw_sp_port);
  589. }