1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2019 Hisilicon Limited.
11 #include <rte_common.h>
12 #include <rte_ethdev.h>
14 #include "hns3_logs.h"
15 #include "hns3_regs.h"
16 #include "hns3_ethdev.h"
19 #define HNS3_SHAPER_BS_U_DEF 5
20 #define HNS3_SHAPER_BS_S_DEF 20
21 #define BW_MAX_PERCENT 100
24 * hns3_shaper_para_calc: calculate ir parameter for the shaper
25 * @ir: Rate to be config, its unit is Mbps
26 * @shaper_level: the shaper level. eg: port, pg, priority, queueset
27 * @shaper_para: shaper parameter of IR shaper
31 * IR_b * (2 ^ IR_u) * 8
32 * IR(Mbps) = ------------------------- * CLOCK(1000Mbps)
35 * @return: 0: calculate sucessful, negative: fail
38 hns3_shaper_para_calc(struct hns3_hw *hw, uint32_t ir, uint8_t shaper_level,
39 struct hns3_shaper_parameter *shaper_para)
41 #define SHAPER_DEFAULT_IR_B 126
42 #define DIVISOR_CLK (1000 * 8)
43 #define DIVISOR_IR_B_126 (126 * DIVISOR_CLK)
45 const uint16_t tick_array[HNS3_SHAPER_LVL_CNT] = {
46 6 * 256, /* Prioriy level */
47 6 * 32, /* Prioriy group level */
48 6 * 8, /* Port level */
49 6 * 256 /* Qset level */
51 uint8_t ir_u_calc = 0;
52 uint8_t ir_s_calc = 0;
58 if (shaper_level >= HNS3_SHAPER_LVL_CNT) {
60 "shaper_level(%d) is greater than HNS3_SHAPER_LVL_CNT(%d)",
61 shaper_level, HNS3_SHAPER_LVL_CNT);
65 if (ir > hw->max_tm_rate) {
66 hns3_err(hw, "rate(%d) exceeds the max rate(%d) driver "
67 "supported.", ir, hw->max_tm_rate);
71 tick = tick_array[shaper_level];
74 * Calc the speed if ir_b = 126, ir_u = 0 and ir_s = 0
75 * the formula is changed to:
77 * ir_calc = ---------------- * 1000
80 ir_calc = (DIVISOR_IR_B_126 + (tick >> 1) - 1) / tick;
83 shaper_para->ir_b = SHAPER_DEFAULT_IR_B;
84 } else if (ir_calc > ir) {
85 /* Increasing the denominator to select ir_s value */
88 ir_calc = DIVISOR_IR_B_126 / (tick * (1 << ir_s_calc));
89 } while (ir_calc > ir);
92 shaper_para->ir_b = SHAPER_DEFAULT_IR_B;
94 shaper_para->ir_b = (ir * tick * (1 << ir_s_calc) +
95 (DIVISOR_CLK >> 1)) / DIVISOR_CLK;
98 * Increasing the numerator to select ir_u value. ir_u_calc will
99 * get maximum value when ir_calc is minimum and ir is maximum.
100 * ir_calc gets minimum value when tick is the maximum value.
101 * At the same time, value of ir_u_calc can only be increased up
102 * to eight after the while loop if the value of ir is equal
103 * to hw->max_tm_rate.
108 numerator = DIVISOR_IR_B_126 * (1 << ir_u_calc);
109 ir_calc = (numerator + (tick >> 1)) / tick;
110 } while (ir_calc < ir);
113 shaper_para->ir_b = SHAPER_DEFAULT_IR_B;
118 * The maximum value of ir_u_calc in this branch is
119 * seven in all cases. Thus, value of denominator can
122 denominator = DIVISOR_CLK * (1 << ir_u_calc);
124 (ir * tick + (denominator >> 1)) / denominator;
128 shaper_para->ir_u = ir_u_calc;
129 shaper_para->ir_s = ir_s_calc;
135 hns3_fill_pri_array(struct hns3_hw *hw, uint8_t *pri, uint8_t pri_id)
137 #define HNS3_HALF_BYTE_BIT_OFFSET 4
138 uint8_t tc = hw->dcb_info.prio_tc[pri_id];
140 if (tc >= hw->dcb_info.num_tc)
144 * The register for priority has four bytes, the first bytes includes
145 * priority0 and priority1, the higher 4bit stands for priority1
146 * while the lower 4bit stands for priority0, as below:
147 * first byte: | pri_1 | pri_0 |
148 * second byte: | pri_3 | pri_2 |
149 * third byte: | pri_5 | pri_4 |
150 * fourth byte: | pri_7 | pri_6 |
152 pri[pri_id >> 1] |= tc << ((pri_id & 1) * HNS3_HALF_BYTE_BIT_OFFSET);
158 hns3_up_to_tc_map(struct hns3_hw *hw)
160 struct hns3_cmd_desc desc;
161 uint8_t *pri = (uint8_t *)desc.data;
165 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PRI_TO_TC_MAPPING, false);
167 for (pri_id = 0; pri_id < HNS3_MAX_USER_PRIO; pri_id++) {
168 ret = hns3_fill_pri_array(hw, pri, pri_id);
173 return hns3_cmd_send(hw, &desc, 1);
177 hns3_pg_to_pri_map_cfg(struct hns3_hw *hw, uint8_t pg_id, uint8_t pri_bit_map)
179 struct hns3_pg_to_pri_link_cmd *map;
180 struct hns3_cmd_desc desc;
182 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PG_TO_PRI_LINK, false);
184 map = (struct hns3_pg_to_pri_link_cmd *)desc.data;
187 map->pri_bit_map = pri_bit_map;
189 return hns3_cmd_send(hw, &desc, 1);
193 hns3_pg_to_pri_map(struct hns3_hw *hw)
195 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
196 struct hns3_pf *pf = &hns->pf;
197 struct hns3_pg_info *pg_info;
200 if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
203 for (i = 0; i < hw->dcb_info.num_pg; i++) {
204 /* Cfg pg to priority mapping */
205 pg_info = &hw->dcb_info.pg_info[i];
206 ret = hns3_pg_to_pri_map_cfg(hw, i, pg_info->tc_bit_map);
215 hns3_qs_to_pri_map_cfg(struct hns3_hw *hw, uint16_t qs_id, uint8_t pri)
217 struct hns3_qs_to_pri_link_cmd *map;
218 struct hns3_cmd_desc desc;
220 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_QS_TO_PRI_LINK, false);
222 map = (struct hns3_qs_to_pri_link_cmd *)desc.data;
224 map->qs_id = rte_cpu_to_le_16(qs_id);
226 map->link_vld = HNS3_DCB_QS_PRI_LINK_VLD_MSK;
228 return hns3_cmd_send(hw, &desc, 1);
232 hns3_dcb_qs_weight_cfg(struct hns3_hw *hw, uint16_t qs_id, uint8_t dwrr)
234 struct hns3_qs_weight_cmd *weight;
235 struct hns3_cmd_desc desc;
237 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_QS_WEIGHT, false);
239 weight = (struct hns3_qs_weight_cmd *)desc.data;
241 weight->qs_id = rte_cpu_to_le_16(qs_id);
244 return hns3_cmd_send(hw, &desc, 1);
248 hns3_dcb_ets_tc_dwrr_cfg(struct hns3_hw *hw)
250 #define DEFAULT_TC_WEIGHT 1
251 #define DEFAULT_TC_OFFSET 14
252 struct hns3_ets_tc_weight_cmd *ets_weight;
253 struct hns3_cmd_desc desc;
256 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_ETS_TC_WEIGHT, false);
257 ets_weight = (struct hns3_ets_tc_weight_cmd *)desc.data;
259 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
260 struct hns3_pg_info *pg_info;
262 ets_weight->tc_weight[i] = DEFAULT_TC_WEIGHT;
264 if (!(hw->hw_tc_map & BIT(i)))
267 pg_info = &hw->dcb_info.pg_info[hw->dcb_info.tc_info[i].pgid];
268 ets_weight->tc_weight[i] = pg_info->tc_dwrr[i];
271 ets_weight->weight_offset = DEFAULT_TC_OFFSET;
273 return hns3_cmd_send(hw, &desc, 1);
277 hns3_dcb_pri_weight_cfg(struct hns3_hw *hw, uint8_t pri_id, uint8_t dwrr)
279 struct hns3_priority_weight_cmd *weight;
280 struct hns3_cmd_desc desc;
282 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PRI_WEIGHT, false);
284 weight = (struct hns3_priority_weight_cmd *)desc.data;
286 weight->pri_id = pri_id;
289 return hns3_cmd_send(hw, &desc, 1);
293 hns3_dcb_pg_weight_cfg(struct hns3_hw *hw, uint8_t pg_id, uint8_t dwrr)
295 struct hns3_pg_weight_cmd *weight;
296 struct hns3_cmd_desc desc;
298 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PG_WEIGHT, false);
300 weight = (struct hns3_pg_weight_cmd *)desc.data;
302 weight->pg_id = pg_id;
305 return hns3_cmd_send(hw, &desc, 1);
308 hns3_dcb_pg_schd_mode_cfg(struct hns3_hw *hw, uint8_t pg_id)
310 struct hns3_cmd_desc desc;
312 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PG_SCH_MODE_CFG, false);
314 if (hw->dcb_info.pg_info[pg_id].pg_sch_mode == HNS3_SCH_MODE_DWRR)
315 desc.data[1] = rte_cpu_to_le_32(HNS3_DCB_TX_SCHD_DWRR_MSK);
319 desc.data[0] = rte_cpu_to_le_32(pg_id);
321 return hns3_cmd_send(hw, &desc, 1);
325 hns3_dcb_get_shapping_para(uint8_t ir_b, uint8_t ir_u, uint8_t ir_s,
326 uint8_t bs_b, uint8_t bs_s)
328 uint32_t shapping_para = 0;
330 hns3_dcb_set_field(shapping_para, IR_B, ir_b);
331 hns3_dcb_set_field(shapping_para, IR_U, ir_u);
332 hns3_dcb_set_field(shapping_para, IR_S, ir_s);
333 hns3_dcb_set_field(shapping_para, BS_B, bs_b);
334 hns3_dcb_set_field(shapping_para, BS_S, bs_s);
336 return shapping_para;
340 hns3_dcb_port_shaper_cfg(struct hns3_hw *hw)
342 struct hns3_port_shapping_cmd *shap_cfg_cmd;
343 struct hns3_shaper_parameter shaper_parameter;
344 uint32_t shapping_para;
345 uint32_t ir_u, ir_b, ir_s;
346 struct hns3_cmd_desc desc;
349 ret = hns3_shaper_para_calc(hw, hw->mac.link_speed,
350 HNS3_SHAPER_LVL_PORT, &shaper_parameter);
352 hns3_err(hw, "calculate shaper parameter failed: %d", ret);
356 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PORT_SHAPPING, false);
357 shap_cfg_cmd = (struct hns3_port_shapping_cmd *)desc.data;
359 ir_b = shaper_parameter.ir_b;
360 ir_u = shaper_parameter.ir_u;
361 ir_s = shaper_parameter.ir_s;
362 shapping_para = hns3_dcb_get_shapping_para(ir_b, ir_u, ir_s,
363 HNS3_SHAPER_BS_U_DEF,
364 HNS3_SHAPER_BS_S_DEF);
366 shap_cfg_cmd->port_shapping_para = rte_cpu_to_le_32(shapping_para);
369 * Configure the port_rate and set bit HNS3_TM_RATE_VLD_B of flag
370 * field in hns3_port_shapping_cmd to require firmware to recalculate
371 * shapping parameters. And whether the parameters are recalculated
372 * depends on the firmware version. But driver still needs to
373 * calculate it and configure to firmware for better compatibility.
375 shap_cfg_cmd->port_rate = rte_cpu_to_le_32(hw->mac.link_speed);
376 hns3_set_bit(shap_cfg_cmd->flag, HNS3_TM_RATE_VLD_B, 1);
378 return hns3_cmd_send(hw, &desc, 1);
382 hns3_dcb_pg_shapping_cfg(struct hns3_hw *hw, enum hns3_shap_bucket bucket,
383 uint8_t pg_id, uint32_t shapping_para, uint32_t rate)
385 struct hns3_pg_shapping_cmd *shap_cfg_cmd;
386 enum hns3_opcode_type opcode;
387 struct hns3_cmd_desc desc;
389 opcode = bucket ? HNS3_OPC_TM_PG_P_SHAPPING :
390 HNS3_OPC_TM_PG_C_SHAPPING;
391 hns3_cmd_setup_basic_desc(&desc, opcode, false);
393 shap_cfg_cmd = (struct hns3_pg_shapping_cmd *)desc.data;
395 shap_cfg_cmd->pg_id = pg_id;
397 shap_cfg_cmd->pg_shapping_para = rte_cpu_to_le_32(shapping_para);
400 * Configure the pg_rate and set bit HNS3_TM_RATE_VLD_B of flag field in
401 * hns3_pg_shapping_cmd to require firmware to recalculate shapping
402 * parameters. And whether parameters are recalculated depends on
403 * the firmware version. But driver still needs to calculate it and
404 * configure to firmware for better compatibility.
406 shap_cfg_cmd->pg_rate = rte_cpu_to_le_32(rate);
407 hns3_set_bit(shap_cfg_cmd->flag, HNS3_TM_RATE_VLD_B, 1);
409 return hns3_cmd_send(hw, &desc, 1);
413 hns3_dcb_pg_shaper_cfg(struct hns3_hw *hw)
415 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
416 struct hns3_shaper_parameter shaper_parameter;
417 struct hns3_pf *pf = &hns->pf;
418 uint32_t ir_u, ir_b, ir_s;
419 uint32_t shaper_para;
425 if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
429 for (i = 0; i < hw->dcb_info.num_pg; i++) {
430 rate = hw->dcb_info.pg_info[i].bw_limit;
432 /* Calc shaper para */
433 ret = hns3_shaper_para_calc(hw, rate, HNS3_SHAPER_LVL_PG,
436 hns3_err(hw, "calculate shaper parameter failed: %d",
441 shaper_para = hns3_dcb_get_shapping_para(0, 0, 0,
442 HNS3_SHAPER_BS_U_DEF,
443 HNS3_SHAPER_BS_S_DEF);
445 ret = hns3_dcb_pg_shapping_cfg(hw, HNS3_DCB_SHAP_C_BUCKET, i,
449 "config PG CIR shaper parameter failed: %d",
454 ir_b = shaper_parameter.ir_b;
455 ir_u = shaper_parameter.ir_u;
456 ir_s = shaper_parameter.ir_s;
457 shaper_para = hns3_dcb_get_shapping_para(ir_b, ir_u, ir_s,
458 HNS3_SHAPER_BS_U_DEF,
459 HNS3_SHAPER_BS_S_DEF);
461 ret = hns3_dcb_pg_shapping_cfg(hw, HNS3_DCB_SHAP_P_BUCKET, i,
465 "config PG PIR shaper parameter failed: %d",
475 hns3_dcb_qs_schd_mode_cfg(struct hns3_hw *hw, uint16_t qs_id, uint8_t mode)
477 struct hns3_cmd_desc desc;
479 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_QS_SCH_MODE_CFG, false);
481 if (mode == HNS3_SCH_MODE_DWRR)
482 desc.data[1] = rte_cpu_to_le_32(HNS3_DCB_TX_SCHD_DWRR_MSK);
486 desc.data[0] = rte_cpu_to_le_32(qs_id);
488 return hns3_cmd_send(hw, &desc, 1);
492 hns3_dcb_pri_schd_mode_cfg(struct hns3_hw *hw, uint8_t pri_id)
494 struct hns3_cmd_desc desc;
496 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PRI_SCH_MODE_CFG, false);
498 if (hw->dcb_info.tc_info[pri_id].tc_sch_mode == HNS3_SCH_MODE_DWRR)
499 desc.data[1] = rte_cpu_to_le_32(HNS3_DCB_TX_SCHD_DWRR_MSK);
503 desc.data[0] = rte_cpu_to_le_32(pri_id);
505 return hns3_cmd_send(hw, &desc, 1);
509 hns3_dcb_pri_shapping_cfg(struct hns3_hw *hw, enum hns3_shap_bucket bucket,
510 uint8_t pri_id, uint32_t shapping_para, uint32_t rate)
512 struct hns3_pri_shapping_cmd *shap_cfg_cmd;
513 enum hns3_opcode_type opcode;
514 struct hns3_cmd_desc desc;
516 opcode = bucket ? HNS3_OPC_TM_PRI_P_SHAPPING :
517 HNS3_OPC_TM_PRI_C_SHAPPING;
519 hns3_cmd_setup_basic_desc(&desc, opcode, false);
521 shap_cfg_cmd = (struct hns3_pri_shapping_cmd *)desc.data;
523 shap_cfg_cmd->pri_id = pri_id;
525 shap_cfg_cmd->pri_shapping_para = rte_cpu_to_le_32(shapping_para);
528 * Configure the pri_rate and set bit HNS3_TM_RATE_VLD_B of flag
529 * field in hns3_pri_shapping_cmd to require firmware to recalculate
530 * shapping parameters. And whether the parameters are recalculated
531 * depends on the firmware version. But driver still needs to
532 * calculate it and configure to firmware for better compatibility.
534 shap_cfg_cmd->pri_rate = rte_cpu_to_le_32(rate);
535 hns3_set_bit(shap_cfg_cmd->flag, HNS3_TM_RATE_VLD_B, 1);
537 return hns3_cmd_send(hw, &desc, 1);
541 hns3_dcb_pri_tc_base_shaper_cfg(struct hns3_hw *hw)
543 struct hns3_shaper_parameter shaper_parameter;
544 uint32_t ir_u, ir_b, ir_s;
545 uint32_t shaper_para;
549 for (i = 0; i < hw->dcb_info.num_tc; i++) {
550 rate = hw->dcb_info.tc_info[i].bw_limit;
551 ret = hns3_shaper_para_calc(hw, rate, HNS3_SHAPER_LVL_PRI,
554 hns3_err(hw, "calculate shaper parameter failed: %d",
559 shaper_para = hns3_dcb_get_shapping_para(0, 0, 0,
560 HNS3_SHAPER_BS_U_DEF,
561 HNS3_SHAPER_BS_S_DEF);
563 ret = hns3_dcb_pri_shapping_cfg(hw, HNS3_DCB_SHAP_C_BUCKET, i,
567 "config priority CIR shaper parameter failed: %d",
572 ir_b = shaper_parameter.ir_b;
573 ir_u = shaper_parameter.ir_u;
574 ir_s = shaper_parameter.ir_s;
575 shaper_para = hns3_dcb_get_shapping_para(ir_b, ir_u, ir_s,
576 HNS3_SHAPER_BS_U_DEF,
577 HNS3_SHAPER_BS_S_DEF);
579 ret = hns3_dcb_pri_shapping_cfg(hw, HNS3_DCB_SHAP_P_BUCKET, i,
583 "config priority PIR shaper parameter failed: %d",
594 hns3_dcb_pri_shaper_cfg(struct hns3_hw *hw)
596 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
597 struct hns3_pf *pf = &hns->pf;
600 if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
603 ret = hns3_dcb_pri_tc_base_shaper_cfg(hw);
605 hns3_err(hw, "config port shaper failed: %d", ret);
611 hns3_set_rss_size(struct hns3_hw *hw, uint16_t nb_rx_q)
613 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
614 uint16_t rx_qnum_per_tc;
615 uint16_t used_rx_queues;
618 rx_qnum_per_tc = nb_rx_q / hw->num_tc;
619 if (rx_qnum_per_tc > hw->rss_size_max) {
620 hns3_err(hw, "rx queue number of per tc (%u) is greater than "
621 "value (%u) hardware supported.",
622 rx_qnum_per_tc, hw->rss_size_max);
626 used_rx_queues = hw->num_tc * rx_qnum_per_tc;
627 if (used_rx_queues != nb_rx_q) {
628 hns3_err(hw, "rx queue number (%u) configured must be an "
629 "integral multiple of valid tc number (%u).",
630 nb_rx_q, hw->num_tc);
633 hw->alloc_rss_size = rx_qnum_per_tc;
634 hw->used_rx_queues = used_rx_queues;
637 * When rss size is changed, we need to update rss redirection table
638 * maintained by driver. Besides, during the entire reset process, we
639 * need to ensure that the rss table information are not overwritten
640 * and configured directly to the hardware in the RESET_STAGE_RESTORE
641 * stage of the reset process.
643 if (rte_atomic16_read(&hw->reset.resetting) == 0) {
644 for (i = 0; i < HNS3_RSS_IND_TBL_SIZE; i++)
645 rss_cfg->rss_indirection_tbl[i] =
646 i % hw->alloc_rss_size;
653 hns3_tc_queue_mapping_cfg(struct hns3_hw *hw, uint16_t nb_tx_q)
655 struct hns3_tc_queue_info *tc_queue;
656 uint16_t used_tx_queues;
657 uint16_t tx_qnum_per_tc;
660 tx_qnum_per_tc = nb_tx_q / hw->num_tc;
661 used_tx_queues = hw->num_tc * tx_qnum_per_tc;
662 if (used_tx_queues != nb_tx_q) {
663 hns3_err(hw, "tx queue number (%u) configured must be an "
664 "integral multiple of valid tc number (%u).",
665 nb_tx_q, hw->num_tc);
669 hw->used_tx_queues = used_tx_queues;
670 hw->tx_qnum_per_tc = tx_qnum_per_tc;
671 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
672 tc_queue = &hw->tc_queue[i];
673 if (hw->hw_tc_map & BIT(i) && i < hw->num_tc) {
674 tc_queue->enable = true;
675 tc_queue->tqp_offset = i * hw->tx_qnum_per_tc;
676 tc_queue->tqp_count = hw->tx_qnum_per_tc;
679 /* Set to default queue if TC is disable */
680 tc_queue->enable = false;
681 tc_queue->tqp_offset = 0;
682 tc_queue->tqp_count = 0;
691 hns3_queue_to_tc_mapping(struct hns3_hw *hw, uint16_t nb_rx_q, uint16_t nb_tx_q)
695 ret = hns3_set_rss_size(hw, nb_rx_q);
699 return hns3_tc_queue_mapping_cfg(hw, nb_tx_q);
703 hns3_dcb_update_tc_queue_mapping(struct hns3_hw *hw, uint16_t nb_rx_q,
706 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
707 struct hns3_pf *pf = &hns->pf;
710 hw->num_tc = hw->dcb_info.num_tc;
711 ret = hns3_queue_to_tc_mapping(hw, nb_rx_q, nb_tx_q);
716 memcpy(pf->prio_tc, hw->dcb_info.prio_tc, HNS3_MAX_USER_PRIO);
722 hns3_dcb_info_init(struct hns3_hw *hw)
724 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
725 struct hns3_pf *pf = &hns->pf;
728 if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE &&
729 hw->dcb_info.num_pg != 1)
732 /* Initializing PG information */
733 memset(hw->dcb_info.pg_info, 0,
734 sizeof(struct hns3_pg_info) * HNS3_PG_NUM);
735 for (i = 0; i < hw->dcb_info.num_pg; i++) {
736 hw->dcb_info.pg_dwrr[i] = i ? 0 : BW_MAX_PERCENT;
737 hw->dcb_info.pg_info[i].pg_id = i;
738 hw->dcb_info.pg_info[i].pg_sch_mode = HNS3_SCH_MODE_DWRR;
739 hw->dcb_info.pg_info[i].bw_limit = hw->max_tm_rate;
744 hw->dcb_info.pg_info[i].tc_bit_map = hw->hw_tc_map;
745 for (k = 0; k < hw->dcb_info.num_tc; k++)
746 hw->dcb_info.pg_info[i].tc_dwrr[k] = BW_MAX_PERCENT;
749 /* All UPs mapping to TC0 */
750 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
751 hw->dcb_info.prio_tc[i] = 0;
753 /* Initializing tc information */
754 memset(hw->dcb_info.tc_info, 0,
755 sizeof(struct hns3_tc_info) * HNS3_MAX_TC_NUM);
756 for (i = 0; i < hw->dcb_info.num_tc; i++) {
757 hw->dcb_info.tc_info[i].tc_id = i;
758 hw->dcb_info.tc_info[i].tc_sch_mode = HNS3_SCH_MODE_DWRR;
759 hw->dcb_info.tc_info[i].pgid = 0;
760 hw->dcb_info.tc_info[i].bw_limit =
761 hw->dcb_info.pg_info[0].bw_limit;
768 hns3_dcb_lvl2_schd_mode_cfg(struct hns3_hw *hw)
770 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
771 struct hns3_pf *pf = &hns->pf;
774 /* Only being config on TC-Based scheduler mode */
775 if (pf->tx_sch_mode == HNS3_FLAG_VNET_BASE_SCH_MODE)
778 for (i = 0; i < hw->dcb_info.num_pg; i++) {
779 ret = hns3_dcb_pg_schd_mode_cfg(hw, i);
788 hns3_dcb_lvl34_schd_mode_cfg(struct hns3_hw *hw)
790 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
791 struct hns3_pf *pf = &hns->pf;
795 if (pf->tx_sch_mode == HNS3_FLAG_TC_BASE_SCH_MODE) {
796 for (i = 0; i < hw->dcb_info.num_tc; i++) {
797 ret = hns3_dcb_pri_schd_mode_cfg(hw, i);
801 ret = hns3_dcb_qs_schd_mode_cfg(hw, i,
812 hns3_dcb_schd_mode_cfg(struct hns3_hw *hw)
816 ret = hns3_dcb_lvl2_schd_mode_cfg(hw);
818 hns3_err(hw, "config lvl2_schd_mode failed: %d", ret);
822 ret = hns3_dcb_lvl34_schd_mode_cfg(hw);
824 hns3_err(hw, "config lvl34_schd_mode failed: %d", ret);
830 hns3_dcb_pri_tc_base_dwrr_cfg(struct hns3_hw *hw)
832 struct hns3_pg_info *pg_info;
836 for (i = 0; i < hw->dcb_info.num_tc; i++) {
837 pg_info = &hw->dcb_info.pg_info[hw->dcb_info.tc_info[i].pgid];
838 dwrr = pg_info->tc_dwrr[i];
840 ret = hns3_dcb_pri_weight_cfg(hw, i, dwrr);
843 "fail to send priority weight cmd: %d, ret = %d",
848 ret = hns3_dcb_qs_weight_cfg(hw, i, BW_MAX_PERCENT);
850 hns3_err(hw, "fail to send qs_weight cmd: %d, ret = %d",
860 hns3_dcb_pri_dwrr_cfg(struct hns3_hw *hw)
862 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
863 struct hns3_pf *pf = &hns->pf;
867 if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
870 ret = hns3_dcb_pri_tc_base_dwrr_cfg(hw);
874 if (!hns3_dev_dcb_supported(hw))
877 ret = hns3_dcb_ets_tc_dwrr_cfg(hw);
878 if (ret == -EOPNOTSUPP) {
879 version = hw->fw_version;
881 "fw %lu.%lu.%lu.%lu doesn't support ets tc weight cmd",
882 hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
883 HNS3_FW_VERSION_BYTE3_S),
884 hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
885 HNS3_FW_VERSION_BYTE2_S),
886 hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
887 HNS3_FW_VERSION_BYTE1_S),
888 hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
889 HNS3_FW_VERSION_BYTE0_S));
897 hns3_dcb_pg_dwrr_cfg(struct hns3_hw *hw)
899 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
900 struct hns3_pf *pf = &hns->pf;
904 if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
908 for (i = 0; i < hw->dcb_info.num_pg; i++) {
910 ret = hns3_dcb_pg_weight_cfg(hw, i, hw->dcb_info.pg_dwrr[i]);
919 hns3_dcb_dwrr_cfg(struct hns3_hw *hw)
923 ret = hns3_dcb_pg_dwrr_cfg(hw);
925 hns3_err(hw, "config pg_dwrr failed: %d", ret);
929 ret = hns3_dcb_pri_dwrr_cfg(hw);
931 hns3_err(hw, "config pri_dwrr failed: %d", ret);
937 hns3_dcb_shaper_cfg(struct hns3_hw *hw)
941 ret = hns3_dcb_port_shaper_cfg(hw);
943 hns3_err(hw, "config port shaper failed: %d", ret);
947 ret = hns3_dcb_pg_shaper_cfg(hw);
949 hns3_err(hw, "config pg shaper failed: %d", ret);
953 return hns3_dcb_pri_shaper_cfg(hw);
957 hns3_q_to_qs_map_cfg(struct hns3_hw *hw, uint16_t q_id, uint16_t qs_id)
959 struct hns3_nq_to_qs_link_cmd *map;
960 struct hns3_cmd_desc desc;
961 uint16_t tmp_qs_id = 0;
965 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_NQ_TO_QS_LINK, false);
967 map = (struct hns3_nq_to_qs_link_cmd *)desc.data;
969 map->nq_id = rte_cpu_to_le_16(q_id);
972 * Network engine with revision_id 0x21 uses 0~9 bit of qs_id to
973 * configure qset_id. So we need to convert qs_id to the follow
974 * format to support qset_id > 1024.
975 * qs_id: | 15 | 14 ~ 10 | 9 ~ 0 |
978 * qset_id: | 15 ~ 11 | 10 | 9 ~ 0 |
979 * | qs_id_h | vld | qs_id_l |
981 qs_id_l = hns3_get_field(qs_id, HNS3_DCB_QS_ID_L_MSK,
983 qs_id_h = hns3_get_field(qs_id, HNS3_DCB_QS_ID_H_MSK,
985 hns3_set_field(tmp_qs_id, HNS3_DCB_QS_ID_L_MSK, HNS3_DCB_QS_ID_L_S,
987 hns3_set_field(tmp_qs_id, HNS3_DCB_QS_ID_H_EXT_MSK,
988 HNS3_DCB_QS_ID_H_EXT_S, qs_id_h);
989 map->qset_id = rte_cpu_to_le_16(tmp_qs_id | HNS3_DCB_Q_QS_LINK_VLD_MSK);
991 return hns3_cmd_send(hw, &desc, 1);
995 hns3_q_to_qs_map(struct hns3_hw *hw)
997 struct hns3_tc_queue_info *tc_queue;
1002 for (i = 0; i < hw->num_tc; i++) {
1003 tc_queue = &hw->tc_queue[i];
1004 for (j = 0; j < tc_queue->tqp_count; j++) {
1005 q_id = tc_queue->tqp_offset + j;
1006 ret = hns3_q_to_qs_map_cfg(hw, q_id, i);
1016 hns3_pri_q_qs_cfg(struct hns3_hw *hw)
1018 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1019 struct hns3_pf *pf = &hns->pf;
1023 if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
1026 /* Cfg qs -> pri mapping */
1027 for (i = 0; i < hw->num_tc; i++) {
1028 ret = hns3_qs_to_pri_map_cfg(hw, i, i);
1030 hns3_err(hw, "qs_to_pri mapping fail: %d", ret);
1035 /* Cfg q -> qs mapping */
1036 ret = hns3_q_to_qs_map(hw);
1038 hns3_err(hw, "nq_to_qs mapping fail: %d", ret);
1044 hns3_dcb_map_cfg(struct hns3_hw *hw)
1048 ret = hns3_up_to_tc_map(hw);
1050 hns3_err(hw, "up_to_tc mapping fail: %d", ret);
1054 ret = hns3_pg_to_pri_map(hw);
1056 hns3_err(hw, "pri_to_pg mapping fail: %d", ret);
1060 return hns3_pri_q_qs_cfg(hw);
1064 hns3_dcb_schd_setup_hw(struct hns3_hw *hw)
1068 /* Cfg dcb mapping */
1069 ret = hns3_dcb_map_cfg(hw);
1073 /* Cfg dcb shaper */
1074 ret = hns3_dcb_shaper_cfg(hw);
1079 ret = hns3_dcb_dwrr_cfg(hw);
1083 /* Cfg schd mode for each level schd */
1084 return hns3_dcb_schd_mode_cfg(hw);
1088 hns3_pause_param_cfg(struct hns3_hw *hw, const uint8_t *addr,
1089 uint8_t pause_trans_gap, uint16_t pause_trans_time)
1091 struct hns3_cfg_pause_param_cmd *pause_param;
1092 struct hns3_cmd_desc desc;
1094 pause_param = (struct hns3_cfg_pause_param_cmd *)desc.data;
1096 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_MAC_PARA, false);
1098 memcpy(pause_param->mac_addr, addr, RTE_ETHER_ADDR_LEN);
1099 memcpy(pause_param->mac_addr_extra, addr, RTE_ETHER_ADDR_LEN);
1100 pause_param->pause_trans_gap = pause_trans_gap;
1101 pause_param->pause_trans_time = rte_cpu_to_le_16(pause_trans_time);
1103 return hns3_cmd_send(hw, &desc, 1);
1107 hns3_pause_addr_cfg(struct hns3_hw *hw, const uint8_t *mac_addr)
1109 struct hns3_cfg_pause_param_cmd *pause_param;
1110 struct hns3_cmd_desc desc;
1111 uint16_t trans_time;
1115 pause_param = (struct hns3_cfg_pause_param_cmd *)desc.data;
1117 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_MAC_PARA, true);
1119 ret = hns3_cmd_send(hw, &desc, 1);
1123 trans_gap = pause_param->pause_trans_gap;
1124 trans_time = rte_le_to_cpu_16(pause_param->pause_trans_time);
1126 return hns3_pause_param_cfg(hw, mac_addr, trans_gap, trans_time);
1130 hns3_pause_param_setup_hw(struct hns3_hw *hw, uint16_t pause_time)
1132 #define PAUSE_TIME_DIV_BY 2
1133 #define PAUSE_TIME_MIN_VALUE 0x4
1135 struct hns3_mac *mac = &hw->mac;
1136 uint8_t pause_trans_gap;
1139 * Pause transmit gap must be less than "pause_time / 2", otherwise
1140 * the behavior of MAC is undefined.
1142 if (pause_time > PAUSE_TIME_DIV_BY * HNS3_DEFAULT_PAUSE_TRANS_GAP)
1143 pause_trans_gap = HNS3_DEFAULT_PAUSE_TRANS_GAP;
1144 else if (pause_time >= PAUSE_TIME_MIN_VALUE &&
1145 pause_time <= PAUSE_TIME_DIV_BY * HNS3_DEFAULT_PAUSE_TRANS_GAP)
1146 pause_trans_gap = pause_time / PAUSE_TIME_DIV_BY - 1;
1148 hns3_warn(hw, "pause_time(%d) is adjusted to 4", pause_time);
1149 pause_time = PAUSE_TIME_MIN_VALUE;
1150 pause_trans_gap = pause_time / PAUSE_TIME_DIV_BY - 1;
1153 return hns3_pause_param_cfg(hw, mac->mac_addr,
1154 pause_trans_gap, pause_time);
1158 hns3_mac_pause_en_cfg(struct hns3_hw *hw, bool tx, bool rx)
1160 struct hns3_cmd_desc desc;
1162 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_MAC_PAUSE_EN, false);
1164 desc.data[0] = rte_cpu_to_le_32((tx ? HNS3_TX_MAC_PAUSE_EN_MSK : 0) |
1165 (rx ? HNS3_RX_MAC_PAUSE_EN_MSK : 0));
1167 return hns3_cmd_send(hw, &desc, 1);
1171 hns3_pfc_pause_en_cfg(struct hns3_hw *hw, uint8_t pfc_bitmap, bool tx, bool rx)
1173 struct hns3_cmd_desc desc;
1174 struct hns3_pfc_en_cmd *pfc = (struct hns3_pfc_en_cmd *)desc.data;
1176 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PFC_PAUSE_EN, false);
1178 pfc->tx_rx_en_bitmap = (uint8_t)((tx ? HNS3_TX_MAC_PAUSE_EN_MSK : 0) |
1179 (rx ? HNS3_RX_MAC_PAUSE_EN_MSK : 0));
1181 pfc->pri_en_bitmap = pfc_bitmap;
1183 return hns3_cmd_send(hw, &desc, 1);
1187 hns3_qs_bp_cfg(struct hns3_hw *hw, uint8_t tc, uint8_t grp_id, uint32_t bit_map)
1189 struct hns3_bp_to_qs_map_cmd *bp_to_qs_map_cmd;
1190 struct hns3_cmd_desc desc;
1192 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_BP_TO_QSET_MAPPING, false);
1194 bp_to_qs_map_cmd = (struct hns3_bp_to_qs_map_cmd *)desc.data;
1196 bp_to_qs_map_cmd->tc_id = tc;
1197 bp_to_qs_map_cmd->qs_group_id = grp_id;
1198 bp_to_qs_map_cmd->qs_bit_map = rte_cpu_to_le_32(bit_map);
1200 return hns3_cmd_send(hw, &desc, 1);
1204 hns3_get_rx_tx_en_status(struct hns3_hw *hw, bool *tx_en, bool *rx_en)
1206 switch (hw->current_mode) {
1211 case HNS3_FC_RX_PAUSE:
1215 case HNS3_FC_TX_PAUSE:
1231 hns3_mac_pause_setup_hw(struct hns3_hw *hw)
1235 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)
1236 hns3_get_rx_tx_en_status(hw, &tx_en, &rx_en);
1242 return hns3_mac_pause_en_cfg(hw, tx_en, rx_en);
1246 hns3_pfc_setup_hw(struct hns3_hw *hw)
1250 if (hw->current_fc_status == HNS3_FC_STATUS_PFC)
1251 hns3_get_rx_tx_en_status(hw, &tx_en, &rx_en);
1257 return hns3_pfc_pause_en_cfg(hw, hw->dcb_info.pfc_en, tx_en, rx_en);
1261 * Each Tc has a 1024 queue sets to backpress, it divides to
1262 * 32 group, each group contains 32 queue sets, which can be
1263 * represented by uint32_t bitmap.
1266 hns3_bp_setup_hw(struct hns3_hw *hw, uint8_t tc)
1272 for (i = 0; i < HNS3_BP_GRP_NUM; i++) {
1273 uint8_t grp, sub_grp;
1276 grp = hns3_get_field(tc, HNS3_BP_GRP_ID_M, HNS3_BP_GRP_ID_S);
1277 sub_grp = hns3_get_field(tc, HNS3_BP_SUB_GRP_ID_M,
1278 HNS3_BP_SUB_GRP_ID_S);
1280 qs_bitmap |= (1 << sub_grp);
1282 ret = hns3_qs_bp_cfg(hw, tc, i, qs_bitmap);
1291 hns3_dcb_bp_setup(struct hns3_hw *hw)
1295 for (i = 0; i < hw->dcb_info.num_tc; i++) {
1296 ret = hns3_bp_setup_hw(hw, i);
1305 hns3_dcb_pause_setup_hw(struct hns3_hw *hw)
1307 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1308 struct hns3_pf *pf = &hns->pf;
1311 ret = hns3_pause_param_setup_hw(hw, pf->pause_time);
1313 hns3_err(hw, "Fail to set pause parameter. ret = %d", ret);
1317 ret = hns3_mac_pause_setup_hw(hw);
1319 hns3_err(hw, "Fail to setup MAC pause. ret = %d", ret);
1323 /* Only DCB-supported dev supports qset back pressure and pfc cmd */
1324 if (!hns3_dev_dcb_supported(hw))
1327 ret = hns3_pfc_setup_hw(hw);
1329 hns3_err(hw, "config pfc failed! ret = %d", ret);
1333 return hns3_dcb_bp_setup(hw);
1337 hns3_dcb_undrop_tc_map(struct hns3_hw *hw, uint8_t pfc_en)
1339 uint8_t pfc_map = 0;
1343 prio_tc = hw->dcb_info.prio_tc;
1344 for (i = 0; i < hw->dcb_info.num_tc; i++) {
1345 for (j = 0; j < HNS3_MAX_USER_PRIO; j++) {
1346 if (prio_tc[j] == i && pfc_en & BIT(j)) {
1357 hns3_dcb_cfg_validate(struct hns3_adapter *hns, uint8_t *tc, bool *changed)
1359 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
1360 struct hns3_hw *hw = &hns->hw;
1365 dcb_rx_conf = &hw->data->dev_conf.rx_adv_conf.dcb_rx_conf;
1366 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
1367 if (dcb_rx_conf->dcb_tc[i] != hw->dcb_info.prio_tc[i])
1370 if (dcb_rx_conf->dcb_tc[i] > max_tc)
1371 max_tc = dcb_rx_conf->dcb_tc[i];
1374 if (*tc != hw->dcb_info.num_tc)
1378 * We ensure that dcb information can be reconfigured
1379 * after the hns3_priority_flow_ctrl_set function called.
1381 if (hw->current_mode != HNS3_FC_FULL)
1383 pfc_en = RTE_LEN2MASK((uint8_t)dcb_rx_conf->nb_tcs, uint8_t);
1384 if (hw->dcb_info.pfc_en != pfc_en)
1389 hns3_dcb_info_cfg(struct hns3_adapter *hns)
1391 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
1392 struct hns3_pf *pf = &hns->pf;
1393 struct hns3_hw *hw = &hns->hw;
1394 uint8_t tc_bw, bw_rest;
1398 dcb_rx_conf = &hw->data->dev_conf.rx_adv_conf.dcb_rx_conf;
1399 pf->local_max_tc = (uint8_t)dcb_rx_conf->nb_tcs;
1400 pf->pfc_max = (uint8_t)dcb_rx_conf->nb_tcs;
1403 memset(hw->dcb_info.pg_info, 0,
1404 sizeof(struct hns3_pg_info) * HNS3_PG_NUM);
1405 hw->dcb_info.pg_dwrr[0] = BW_MAX_PERCENT;
1406 hw->dcb_info.pg_info[0].pg_id = 0;
1407 hw->dcb_info.pg_info[0].pg_sch_mode = HNS3_SCH_MODE_DWRR;
1408 hw->dcb_info.pg_info[0].bw_limit = hw->max_tm_rate;
1409 hw->dcb_info.pg_info[0].tc_bit_map = hw->hw_tc_map;
1411 /* Each tc has same bw for valid tc by default */
1412 tc_bw = BW_MAX_PERCENT / hw->dcb_info.num_tc;
1413 for (i = 0; i < hw->dcb_info.num_tc; i++)
1414 hw->dcb_info.pg_info[0].tc_dwrr[i] = tc_bw;
1415 /* To ensure the sum of tc_dwrr is equal to 100 */
1416 bw_rest = BW_MAX_PERCENT % hw->dcb_info.num_tc;
1417 for (j = 0; j < bw_rest; j++)
1418 hw->dcb_info.pg_info[0].tc_dwrr[j]++;
1419 for (; i < dcb_rx_conf->nb_tcs; i++)
1420 hw->dcb_info.pg_info[0].tc_dwrr[i] = 0;
1422 /* All tcs map to pg0 */
1423 memset(hw->dcb_info.tc_info, 0,
1424 sizeof(struct hns3_tc_info) * HNS3_MAX_TC_NUM);
1425 for (i = 0; i < hw->dcb_info.num_tc; i++) {
1426 hw->dcb_info.tc_info[i].tc_id = i;
1427 hw->dcb_info.tc_info[i].tc_sch_mode = HNS3_SCH_MODE_DWRR;
1428 hw->dcb_info.tc_info[i].pgid = 0;
1429 hw->dcb_info.tc_info[i].bw_limit =
1430 hw->dcb_info.pg_info[0].bw_limit;
1433 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
1434 hw->dcb_info.prio_tc[i] = dcb_rx_conf->dcb_tc[i];
1436 ret = hns3_dcb_update_tc_queue_mapping(hw, hw->data->nb_rx_queues,
1437 hw->data->nb_tx_queues);
1439 hns3_err(hw, "update tc queue mapping failed, ret = %d.", ret);
1445 hns3_dcb_info_update(struct hns3_adapter *hns, uint8_t num_tc)
1447 struct hns3_pf *pf = &hns->pf;
1448 struct hns3_hw *hw = &hns->hw;
1449 uint16_t nb_rx_q = hw->data->nb_rx_queues;
1450 uint16_t nb_tx_q = hw->data->nb_tx_queues;
1451 uint8_t bit_map = 0;
1454 if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE &&
1455 hw->dcb_info.num_pg != 1)
1458 if (nb_rx_q < num_tc) {
1459 hns3_err(hw, "number of Rx queues(%d) is less than tcs(%d).",
1464 if (nb_tx_q < num_tc) {
1465 hns3_err(hw, "number of Tx queues(%d) is less than tcs(%d).",
1470 /* Currently not support uncontinuous tc */
1471 hw->dcb_info.num_tc = num_tc;
1472 for (i = 0; i < hw->dcb_info.num_tc; i++)
1477 hw->dcb_info.num_tc = 1;
1479 hw->hw_tc_map = bit_map;
1481 return hns3_dcb_info_cfg(hns);
1485 hns3_dcb_hw_configure(struct hns3_adapter *hns)
1487 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
1488 struct hns3_pf *pf = &hns->pf;
1489 struct hns3_hw *hw = &hns->hw;
1490 enum hns3_fc_status fc_status = hw->current_fc_status;
1491 enum hns3_fc_mode current_mode = hw->current_mode;
1492 uint8_t hw_pfc_map = hw->dcb_info.hw_pfc_map;
1495 if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE &&
1496 pf->tx_sch_mode != HNS3_FLAG_VNET_BASE_SCH_MODE)
1499 ret = hns3_dcb_schd_setup_hw(hw);
1501 hns3_err(hw, "dcb schdule configure failed! ret = %d", ret);
1505 if (hw->data->dev_conf.dcb_capability_en & ETH_DCB_PFC_SUPPORT) {
1506 dcb_rx_conf = &hw->data->dev_conf.rx_adv_conf.dcb_rx_conf;
1507 if (dcb_rx_conf->nb_tcs == 0)
1508 hw->dcb_info.pfc_en = 1; /* tc0 only */
1510 hw->dcb_info.pfc_en =
1511 RTE_LEN2MASK((uint8_t)dcb_rx_conf->nb_tcs, uint8_t);
1513 hw->dcb_info.hw_pfc_map =
1514 hns3_dcb_undrop_tc_map(hw, hw->dcb_info.pfc_en);
1516 ret = hns3_buffer_alloc(hw);
1520 hw->current_fc_status = HNS3_FC_STATUS_PFC;
1521 hw->current_mode = HNS3_FC_FULL;
1522 ret = hns3_dcb_pause_setup_hw(hw);
1524 hns3_err(hw, "setup pfc failed! ret = %d", ret);
1525 goto pfc_setup_fail;
1529 * Although dcb_capability_en is lack of ETH_DCB_PFC_SUPPORT
1530 * flag, the DCB information is configured, such as tc numbers.
1531 * Therefore, refreshing the allocation of packet buffer is
1534 ret = hns3_buffer_alloc(hw);
1542 hw->current_mode = current_mode;
1543 hw->current_fc_status = fc_status;
1544 hw->dcb_info.hw_pfc_map = hw_pfc_map;
1545 status = hns3_buffer_alloc(hw);
1547 hns3_err(hw, "recover packet buffer fail! status = %d", status);
1553 * hns3_dcb_configure - setup dcb related config
1554 * @hns: pointer to hns3 adapter
1555 * Returns 0 on success, negative value on failure.
1558 hns3_dcb_configure(struct hns3_adapter *hns)
1560 struct hns3_hw *hw = &hns->hw;
1561 bool map_changed = false;
1565 hns3_dcb_cfg_validate(hns, &num_tc, &map_changed);
1566 if (map_changed || rte_atomic16_read(&hw->reset.resetting)) {
1567 ret = hns3_dcb_info_update(hns, num_tc);
1569 hns3_err(hw, "dcb info update failed: %d", ret);
1573 ret = hns3_dcb_hw_configure(hns);
1575 hns3_err(hw, "dcb sw configure failed: %d", ret);
1584 hns3_dcb_init_hw(struct hns3_hw *hw)
1588 ret = hns3_dcb_schd_setup_hw(hw);
1590 hns3_err(hw, "dcb schedule setup failed: %d", ret);
1594 ret = hns3_dcb_pause_setup_hw(hw);
1596 hns3_err(hw, "PAUSE setup failed: %d", ret);
1602 hns3_dcb_init(struct hns3_hw *hw)
1604 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1605 struct hns3_pf *pf = &hns->pf;
1606 uint16_t default_tqp_num;
1609 PMD_INIT_FUNC_TRACE();
1612 * According to the 'adapter_state' identifier, the following branch
1613 * is only executed to initialize default configurations of dcb during
1614 * the initializing driver process. Due to driver saving dcb-related
1615 * information before reset triggered, the reinit dev stage of the
1616 * reset process can not access to the branch, or those information
1619 if (hw->adapter_state == HNS3_NIC_UNINITIALIZED) {
1620 hw->requested_mode = HNS3_FC_NONE;
1621 hw->current_mode = hw->requested_mode;
1622 pf->pause_time = HNS3_DEFAULT_PAUSE_TRANS_TIME;
1623 hw->current_fc_status = HNS3_FC_STATUS_NONE;
1625 ret = hns3_dcb_info_init(hw);
1627 hns3_err(hw, "dcb info init failed, ret = %d.", ret);
1632 * The number of queues configured by default cannot exceed
1633 * the maximum number of queues for a single TC.
1635 default_tqp_num = RTE_MIN(hw->rss_size_max,
1636 hw->tqps_num / hw->dcb_info.num_tc);
1637 ret = hns3_dcb_update_tc_queue_mapping(hw, default_tqp_num,
1641 "update tc queue mapping failed, ret = %d.",
1648 * DCB hardware will be configured by following the function during
1649 * the initializing driver process and the reset process. However,
1650 * driver will restore directly configurations of dcb hardware based
1651 * on dcb-related information soft maintained when driver
1652 * initialization has finished and reset is coming.
1654 ret = hns3_dcb_init_hw(hw);
1656 hns3_err(hw, "dcb init hardware failed, ret = %d.", ret);
1664 hns3_update_queue_map_configure(struct hns3_adapter *hns)
1666 struct hns3_hw *hw = &hns->hw;
1667 uint16_t nb_rx_q = hw->data->nb_rx_queues;
1668 uint16_t nb_tx_q = hw->data->nb_tx_queues;
1671 ret = hns3_dcb_update_tc_queue_mapping(hw, nb_rx_q, nb_tx_q);
1673 hns3_err(hw, "failed to update tc queue mapping, ret = %d.",
1677 ret = hns3_q_to_qs_map(hw);
1679 hns3_err(hw, "failed to map nq to qs, ret = %d.", ret);
1685 hns3_dcb_cfg_update(struct hns3_adapter *hns)
1687 struct hns3_hw *hw = &hns->hw;
1688 enum rte_eth_rx_mq_mode mq_mode = hw->data->dev_conf.rxmode.mq_mode;
1691 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG) {
1692 ret = hns3_dcb_configure(hns);
1694 hns3_err(hw, "Failed to config dcb: %d", ret);
1697 * Update queue map without PFC configuration,
1698 * due to queues reconfigured by user.
1700 ret = hns3_update_queue_map_configure(hns);
1703 "Failed to update queue mapping configure: %d",
1711 * hns3_dcb_pfc_enable - Enable priority flow control
1712 * @dev: pointer to ethernet device
1714 * Configures the pfc settings for one porority.
1717 hns3_dcb_pfc_enable(struct rte_eth_dev *dev, struct rte_eth_pfc_conf *pfc_conf)
1719 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1720 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1721 enum hns3_fc_status fc_status = hw->current_fc_status;
1722 enum hns3_fc_mode current_mode = hw->current_mode;
1723 uint8_t hw_pfc_map = hw->dcb_info.hw_pfc_map;
1724 uint8_t pfc_en = hw->dcb_info.pfc_en;
1725 uint8_t priority = pfc_conf->priority;
1726 uint16_t pause_time = pf->pause_time;
1729 pf->pause_time = pfc_conf->fc.pause_time;
1730 hw->current_mode = hw->requested_mode;
1731 hw->current_fc_status = HNS3_FC_STATUS_PFC;
1732 hw->dcb_info.pfc_en |= BIT(priority);
1733 hw->dcb_info.hw_pfc_map =
1734 hns3_dcb_undrop_tc_map(hw, hw->dcb_info.pfc_en);
1735 ret = hns3_buffer_alloc(hw);
1737 goto pfc_setup_fail;
1740 * The flow control mode of all UPs will be changed based on
1741 * current_mode coming from user.
1743 ret = hns3_dcb_pause_setup_hw(hw);
1745 hns3_err(hw, "enable pfc failed! ret = %d", ret);
1746 goto pfc_setup_fail;
1752 hw->current_mode = current_mode;
1753 hw->current_fc_status = fc_status;
1754 pf->pause_time = pause_time;
1755 hw->dcb_info.pfc_en = pfc_en;
1756 hw->dcb_info.hw_pfc_map = hw_pfc_map;
1757 status = hns3_buffer_alloc(hw);
1759 hns3_err(hw, "recover packet buffer fail: %d", status);
1765 * hns3_fc_enable - Enable MAC pause
1766 * @dev: pointer to ethernet device
1768 * Configures the MAC pause settings.
1771 hns3_fc_enable(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1773 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1774 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1775 enum hns3_fc_status fc_status = hw->current_fc_status;
1776 enum hns3_fc_mode current_mode = hw->current_mode;
1777 uint16_t pause_time = pf->pause_time;
1780 pf->pause_time = fc_conf->pause_time;
1781 hw->current_mode = hw->requested_mode;
1784 * In fact, current_fc_status is HNS3_FC_STATUS_NONE when mode
1785 * of flow control is configured to be HNS3_FC_NONE.
1787 if (hw->current_mode == HNS3_FC_NONE)
1788 hw->current_fc_status = HNS3_FC_STATUS_NONE;
1790 hw->current_fc_status = HNS3_FC_STATUS_MAC_PAUSE;
1792 ret = hns3_dcb_pause_setup_hw(hw);
1794 hns3_err(hw, "enable MAC Pause failed! ret = %d", ret);
1801 hw->current_mode = current_mode;
1802 hw->current_fc_status = fc_status;
1803 pf->pause_time = pause_time;