bnxt_dcb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /* Broadcom NetXtreme-C/E network driver.
  2. *
  3. * Copyright (c) 2014-2016 Broadcom Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/netdevice.h>
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/pci.h>
  15. #include <linux/etherdevice.h>
  16. #include "bnxt_hsi.h"
  17. #include "bnxt.h"
  18. #include "bnxt_dcb.h"
  19. #ifdef CONFIG_BNXT_DCB
  20. static int bnxt_hwrm_queue_pri2cos_cfg(struct bnxt *bp, struct ieee_ets *ets)
  21. {
  22. struct hwrm_queue_pri2cos_cfg_input req = {0};
  23. int rc = 0, i;
  24. u8 *pri2cos;
  25. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_PRI2COS_CFG, -1, -1);
  26. req.flags = cpu_to_le32(QUEUE_PRI2COS_CFG_REQ_FLAGS_PATH_BIDIR |
  27. QUEUE_PRI2COS_CFG_REQ_FLAGS_IVLAN);
  28. pri2cos = &req.pri0_cos_queue_id;
  29. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  30. req.enables |= cpu_to_le32(
  31. QUEUE_PRI2COS_CFG_REQ_ENABLES_PRI0_COS_QUEUE_ID << i);
  32. pri2cos[i] = bp->q_info[ets->prio_tc[i]].queue_id;
  33. }
  34. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  35. return rc;
  36. }
  37. static int bnxt_hwrm_queue_pri2cos_qcfg(struct bnxt *bp, struct ieee_ets *ets)
  38. {
  39. struct hwrm_queue_pri2cos_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  40. struct hwrm_queue_pri2cos_qcfg_input req = {0};
  41. int rc = 0;
  42. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
  43. req.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
  44. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  45. if (!rc) {
  46. u8 *pri2cos = &resp->pri0_cos_queue_id;
  47. int i, j;
  48. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  49. u8 queue_id = pri2cos[i];
  50. for (j = 0; j < bp->max_tc; j++) {
  51. if (bp->q_info[j].queue_id == queue_id) {
  52. ets->prio_tc[i] = j;
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. return rc;
  59. }
  60. static int bnxt_hwrm_queue_cos2bw_cfg(struct bnxt *bp, struct ieee_ets *ets,
  61. u8 max_tc)
  62. {
  63. struct hwrm_queue_cos2bw_cfg_input req = {0};
  64. struct bnxt_cos2bw_cfg cos2bw;
  65. int rc = 0, i;
  66. void *data;
  67. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_COS2BW_CFG, -1, -1);
  68. data = &req.unused_0;
  69. for (i = 0; i < max_tc; i++, data += sizeof(cos2bw) - 4) {
  70. req.enables |= cpu_to_le32(
  71. QUEUE_COS2BW_CFG_REQ_ENABLES_COS_QUEUE_ID0_VALID << i);
  72. memset(&cos2bw, 0, sizeof(cos2bw));
  73. cos2bw.queue_id = bp->q_info[i].queue_id;
  74. if (ets->tc_tsa[i] == IEEE_8021QAZ_TSA_STRICT) {
  75. cos2bw.tsa =
  76. QUEUE_COS2BW_QCFG_RESP_QUEUE_ID0_TSA_ASSIGN_SP;
  77. cos2bw.pri_lvl = i;
  78. } else {
  79. cos2bw.tsa =
  80. QUEUE_COS2BW_QCFG_RESP_QUEUE_ID0_TSA_ASSIGN_ETS;
  81. cos2bw.bw_weight = ets->tc_tx_bw[i];
  82. }
  83. memcpy(data, &cos2bw.queue_id, sizeof(cos2bw) - 4);
  84. if (i == 0) {
  85. req.queue_id0 = cos2bw.queue_id;
  86. req.unused_0 = 0;
  87. }
  88. }
  89. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  90. return rc;
  91. }
  92. static int bnxt_hwrm_queue_cos2bw_qcfg(struct bnxt *bp, struct ieee_ets *ets)
  93. {
  94. struct hwrm_queue_cos2bw_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  95. struct hwrm_queue_cos2bw_qcfg_input req = {0};
  96. struct bnxt_cos2bw_cfg cos2bw;
  97. void *data;
  98. int rc, i;
  99. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_COS2BW_QCFG, -1, -1);
  100. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  101. if (rc)
  102. return rc;
  103. data = &resp->queue_id0 + offsetof(struct bnxt_cos2bw_cfg, queue_id);
  104. for (i = 0; i < bp->max_tc; i++, data += sizeof(cos2bw) - 4) {
  105. int j;
  106. memcpy(&cos2bw.queue_id, data, sizeof(cos2bw) - 4);
  107. if (i == 0)
  108. cos2bw.queue_id = resp->queue_id0;
  109. for (j = 0; j < bp->max_tc; j++) {
  110. if (bp->q_info[j].queue_id != cos2bw.queue_id)
  111. continue;
  112. if (cos2bw.tsa ==
  113. QUEUE_COS2BW_QCFG_RESP_QUEUE_ID0_TSA_ASSIGN_SP) {
  114. ets->tc_tsa[j] = IEEE_8021QAZ_TSA_STRICT;
  115. } else {
  116. ets->tc_tsa[j] = IEEE_8021QAZ_TSA_ETS;
  117. ets->tc_tx_bw[j] = cos2bw.bw_weight;
  118. }
  119. }
  120. }
  121. return 0;
  122. }
  123. static int bnxt_hwrm_queue_cfg(struct bnxt *bp, unsigned int lltc_mask)
  124. {
  125. struct hwrm_queue_cfg_input req = {0};
  126. int i;
  127. if (netif_running(bp->dev))
  128. bnxt_tx_disable(bp);
  129. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_CFG, -1, -1);
  130. req.flags = cpu_to_le32(QUEUE_CFG_REQ_FLAGS_PATH_BIDIR);
  131. req.enables = cpu_to_le32(QUEUE_CFG_REQ_ENABLES_SERVICE_PROFILE);
  132. /* Configure lossless queues to lossy first */
  133. req.service_profile = QUEUE_CFG_REQ_SERVICE_PROFILE_LOSSY;
  134. for (i = 0; i < bp->max_tc; i++) {
  135. if (BNXT_LLQ(bp->q_info[i].queue_profile)) {
  136. req.queue_id = cpu_to_le32(bp->q_info[i].queue_id);
  137. hwrm_send_message(bp, &req, sizeof(req),
  138. HWRM_CMD_TIMEOUT);
  139. bp->q_info[i].queue_profile =
  140. QUEUE_CFG_REQ_SERVICE_PROFILE_LOSSY;
  141. }
  142. }
  143. /* Now configure desired queues to lossless */
  144. req.service_profile = QUEUE_CFG_REQ_SERVICE_PROFILE_LOSSLESS;
  145. for (i = 0; i < bp->max_tc; i++) {
  146. if (lltc_mask & (1 << i)) {
  147. req.queue_id = cpu_to_le32(bp->q_info[i].queue_id);
  148. hwrm_send_message(bp, &req, sizeof(req),
  149. HWRM_CMD_TIMEOUT);
  150. bp->q_info[i].queue_profile =
  151. QUEUE_CFG_REQ_SERVICE_PROFILE_LOSSLESS;
  152. }
  153. }
  154. if (netif_running(bp->dev))
  155. bnxt_tx_enable(bp);
  156. return 0;
  157. }
  158. static int bnxt_hwrm_queue_pfc_cfg(struct bnxt *bp, struct ieee_pfc *pfc)
  159. {
  160. struct hwrm_queue_pfcenable_cfg_input req = {0};
  161. struct ieee_ets *my_ets = bp->ieee_ets;
  162. unsigned int tc_mask = 0, pri_mask = 0;
  163. u8 i, pri, lltc_count = 0;
  164. bool need_q_recfg = false;
  165. int rc;
  166. if (!my_ets)
  167. return -EINVAL;
  168. for (i = 0; i < bp->max_tc; i++) {
  169. for (pri = 0; pri < IEEE_8021QAZ_MAX_TCS; pri++) {
  170. if ((pfc->pfc_en & (1 << pri)) &&
  171. (my_ets->prio_tc[pri] == i)) {
  172. pri_mask |= 1 << pri;
  173. tc_mask |= 1 << i;
  174. }
  175. }
  176. if (tc_mask & (1 << i))
  177. lltc_count++;
  178. }
  179. if (lltc_count > bp->max_lltc)
  180. return -EINVAL;
  181. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_PFCENABLE_CFG, -1, -1);
  182. req.flags = cpu_to_le32(pri_mask);
  183. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  184. if (rc)
  185. return rc;
  186. for (i = 0; i < bp->max_tc; i++) {
  187. if (tc_mask & (1 << i)) {
  188. if (!BNXT_LLQ(bp->q_info[i].queue_profile))
  189. need_q_recfg = true;
  190. }
  191. }
  192. if (need_q_recfg)
  193. rc = bnxt_hwrm_queue_cfg(bp, tc_mask);
  194. return rc;
  195. }
  196. static int bnxt_hwrm_queue_pfc_qcfg(struct bnxt *bp, struct ieee_pfc *pfc)
  197. {
  198. struct hwrm_queue_pfcenable_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  199. struct hwrm_queue_pfcenable_qcfg_input req = {0};
  200. u8 pri_mask;
  201. int rc;
  202. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_PFCENABLE_QCFG, -1, -1);
  203. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  204. if (rc)
  205. return rc;
  206. pri_mask = le32_to_cpu(resp->flags);
  207. pfc->pfc_en = pri_mask;
  208. return 0;
  209. }
  210. static int bnxt_ets_validate(struct bnxt *bp, struct ieee_ets *ets, u8 *tc)
  211. {
  212. int total_ets_bw = 0;
  213. u8 max_tc = 0;
  214. int i;
  215. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
  216. if (ets->prio_tc[i] > bp->max_tc) {
  217. netdev_err(bp->dev, "priority to TC mapping exceeds TC count %d\n",
  218. ets->prio_tc[i]);
  219. return -EINVAL;
  220. }
  221. if (ets->prio_tc[i] > max_tc)
  222. max_tc = ets->prio_tc[i];
  223. if ((ets->tc_tx_bw[i] || ets->tc_tsa[i]) && i > bp->max_tc)
  224. return -EINVAL;
  225. switch (ets->tc_tsa[i]) {
  226. case IEEE_8021QAZ_TSA_STRICT:
  227. break;
  228. case IEEE_8021QAZ_TSA_ETS:
  229. total_ets_bw += ets->tc_tx_bw[i];
  230. break;
  231. default:
  232. return -ENOTSUPP;
  233. }
  234. }
  235. if (total_ets_bw > 100)
  236. return -EINVAL;
  237. *tc = max_tc + 1;
  238. return 0;
  239. }
  240. static int bnxt_dcbnl_ieee_getets(struct net_device *dev, struct ieee_ets *ets)
  241. {
  242. struct bnxt *bp = netdev_priv(dev);
  243. struct ieee_ets *my_ets = bp->ieee_ets;
  244. ets->ets_cap = bp->max_tc;
  245. if (!my_ets) {
  246. int rc;
  247. if (bp->dcbx_cap & DCB_CAP_DCBX_HOST)
  248. return 0;
  249. my_ets = kzalloc(sizeof(*my_ets), GFP_KERNEL);
  250. if (!my_ets)
  251. return 0;
  252. rc = bnxt_hwrm_queue_cos2bw_qcfg(bp, my_ets);
  253. if (rc)
  254. return 0;
  255. rc = bnxt_hwrm_queue_pri2cos_qcfg(bp, my_ets);
  256. if (rc)
  257. return 0;
  258. }
  259. ets->cbs = my_ets->cbs;
  260. memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
  261. memcpy(ets->tc_rx_bw, my_ets->tc_rx_bw, sizeof(ets->tc_rx_bw));
  262. memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
  263. memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
  264. return 0;
  265. }
  266. static int bnxt_dcbnl_ieee_setets(struct net_device *dev, struct ieee_ets *ets)
  267. {
  268. struct bnxt *bp = netdev_priv(dev);
  269. struct ieee_ets *my_ets = bp->ieee_ets;
  270. u8 max_tc = 0;
  271. int rc, i;
  272. if (!(bp->dcbx_cap & DCB_CAP_DCBX_VER_IEEE) ||
  273. !(bp->dcbx_cap & DCB_CAP_DCBX_HOST))
  274. return -EINVAL;
  275. rc = bnxt_ets_validate(bp, ets, &max_tc);
  276. if (!rc) {
  277. if (!my_ets) {
  278. my_ets = kzalloc(sizeof(*my_ets), GFP_KERNEL);
  279. if (!my_ets)
  280. return -ENOMEM;
  281. /* initialize PRI2TC mappings to invalid value */
  282. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
  283. my_ets->prio_tc[i] = IEEE_8021QAZ_MAX_TCS;
  284. bp->ieee_ets = my_ets;
  285. }
  286. rc = bnxt_setup_mq_tc(dev, max_tc);
  287. if (rc)
  288. return rc;
  289. rc = bnxt_hwrm_queue_cos2bw_cfg(bp, ets, max_tc);
  290. if (rc)
  291. return rc;
  292. rc = bnxt_hwrm_queue_pri2cos_cfg(bp, ets);
  293. if (rc)
  294. return rc;
  295. memcpy(my_ets, ets, sizeof(*my_ets));
  296. }
  297. return rc;
  298. }
  299. static int bnxt_dcbnl_ieee_getpfc(struct net_device *dev, struct ieee_pfc *pfc)
  300. {
  301. struct bnxt *bp = netdev_priv(dev);
  302. __le64 *stats = (__le64 *)bp->hw_rx_port_stats;
  303. struct ieee_pfc *my_pfc = bp->ieee_pfc;
  304. long rx_off, tx_off;
  305. int i, rc;
  306. pfc->pfc_cap = bp->max_lltc;
  307. if (!my_pfc) {
  308. if (bp->dcbx_cap & DCB_CAP_DCBX_HOST)
  309. return 0;
  310. my_pfc = kzalloc(sizeof(*my_pfc), GFP_KERNEL);
  311. if (!my_pfc)
  312. return 0;
  313. bp->ieee_pfc = my_pfc;
  314. rc = bnxt_hwrm_queue_pfc_qcfg(bp, my_pfc);
  315. if (rc)
  316. return 0;
  317. }
  318. pfc->pfc_en = my_pfc->pfc_en;
  319. pfc->mbc = my_pfc->mbc;
  320. pfc->delay = my_pfc->delay;
  321. if (!stats)
  322. return 0;
  323. rx_off = BNXT_RX_STATS_OFFSET(rx_pfc_ena_frames_pri0);
  324. tx_off = BNXT_TX_STATS_OFFSET(tx_pfc_ena_frames_pri0);
  325. for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++, rx_off++, tx_off++) {
  326. pfc->requests[i] = le64_to_cpu(*(stats + tx_off));
  327. pfc->indications[i] = le64_to_cpu(*(stats + rx_off));
  328. }
  329. return 0;
  330. }
  331. static int bnxt_dcbnl_ieee_setpfc(struct net_device *dev, struct ieee_pfc *pfc)
  332. {
  333. struct bnxt *bp = netdev_priv(dev);
  334. struct ieee_pfc *my_pfc = bp->ieee_pfc;
  335. int rc;
  336. if (!(bp->dcbx_cap & DCB_CAP_DCBX_VER_IEEE) ||
  337. !(bp->dcbx_cap & DCB_CAP_DCBX_HOST))
  338. return -EINVAL;
  339. if (!my_pfc) {
  340. my_pfc = kzalloc(sizeof(*my_pfc), GFP_KERNEL);
  341. if (!my_pfc)
  342. return -ENOMEM;
  343. bp->ieee_pfc = my_pfc;
  344. }
  345. rc = bnxt_hwrm_queue_pfc_cfg(bp, pfc);
  346. if (!rc)
  347. memcpy(my_pfc, pfc, sizeof(*my_pfc));
  348. return rc;
  349. }
  350. static int bnxt_dcbnl_ieee_setapp(struct net_device *dev, struct dcb_app *app)
  351. {
  352. struct bnxt *bp = netdev_priv(dev);
  353. int rc = -EINVAL;
  354. if (!(bp->dcbx_cap & DCB_CAP_DCBX_VER_IEEE) ||
  355. !(bp->dcbx_cap & DCB_CAP_DCBX_HOST))
  356. return -EINVAL;
  357. rc = dcb_ieee_setapp(dev, app);
  358. return rc;
  359. }
  360. static int bnxt_dcbnl_ieee_delapp(struct net_device *dev, struct dcb_app *app)
  361. {
  362. struct bnxt *bp = netdev_priv(dev);
  363. int rc;
  364. if (!(bp->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
  365. return -EINVAL;
  366. rc = dcb_ieee_delapp(dev, app);
  367. return rc;
  368. }
  369. static u8 bnxt_dcbnl_getdcbx(struct net_device *dev)
  370. {
  371. struct bnxt *bp = netdev_priv(dev);
  372. return bp->dcbx_cap;
  373. }
  374. static u8 bnxt_dcbnl_setdcbx(struct net_device *dev, u8 mode)
  375. {
  376. struct bnxt *bp = netdev_priv(dev);
  377. /* only support IEEE */
  378. if ((mode & DCB_CAP_DCBX_VER_CEE) || !(mode & DCB_CAP_DCBX_VER_IEEE))
  379. return 1;
  380. if ((mode & DCB_CAP_DCBX_HOST) && BNXT_VF(bp))
  381. return 1;
  382. if (mode == bp->dcbx_cap)
  383. return 0;
  384. bp->dcbx_cap = mode;
  385. return 0;
  386. }
  387. static const struct dcbnl_rtnl_ops dcbnl_ops = {
  388. .ieee_getets = bnxt_dcbnl_ieee_getets,
  389. .ieee_setets = bnxt_dcbnl_ieee_setets,
  390. .ieee_getpfc = bnxt_dcbnl_ieee_getpfc,
  391. .ieee_setpfc = bnxt_dcbnl_ieee_setpfc,
  392. .ieee_setapp = bnxt_dcbnl_ieee_setapp,
  393. .ieee_delapp = bnxt_dcbnl_ieee_delapp,
  394. .getdcbx = bnxt_dcbnl_getdcbx,
  395. .setdcbx = bnxt_dcbnl_setdcbx,
  396. };
  397. void bnxt_dcb_init(struct bnxt *bp)
  398. {
  399. if (bp->hwrm_spec_code < 0x10501)
  400. return;
  401. bp->dcbx_cap = DCB_CAP_DCBX_VER_IEEE;
  402. if (BNXT_PF(bp))
  403. bp->dcbx_cap |= DCB_CAP_DCBX_HOST;
  404. else
  405. bp->dcbx_cap |= DCB_CAP_DCBX_LLD_MANAGED;
  406. bp->dev->dcbnl_ops = &dcbnl_ops;
  407. }
  408. void bnxt_dcb_free(struct bnxt *bp)
  409. {
  410. kfree(bp->ieee_pfc);
  411. kfree(bp->ieee_ets);
  412. bp->ieee_pfc = NULL;
  413. bp->ieee_ets = NULL;
  414. }
  415. #else
  416. void bnxt_dcb_init(struct bnxt *bp)
  417. {
  418. }
  419. void bnxt_dcb_free(struct bnxt *bp)
  420. {
  421. }
  422. #endif