Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018-2021 HiSilicon Limited.
3 : : */
4 : :
5 : : #include <rte_io.h>
6 : : #include <rte_ethdev.h>
7 : :
8 : : #include "hns3_logs.h"
9 : : #include "hns3_ethdev.h"
10 : : #include "hns3_dcb.h"
11 : :
12 : : #define HNS3_SHAPER_BS_U_DEF 5
13 : : #define HNS3_SHAPER_BS_S_DEF 20
14 : : #define BW_MAX_PERCENT 100
15 : :
16 : : /*
17 : : * hns3_shaper_para_calc: calculate ir parameter for the shaper
18 : : * @ir: Rate to be config, its unit is Mbps
19 : : * @shaper_level: the shaper level. eg: port, pg, priority, queueset
20 : : * @shaper_para: shaper parameter of IR shaper
21 : : *
22 : : * the formula:
23 : : *
24 : : * IR_b * (2 ^ IR_u) * 8
25 : : * IR(Mbps) = ------------------------- * CLOCK(1000Mbps)
26 : : * Tick * (2 ^ IR_s)
27 : : *
28 : : * @return: 0: calculate successful, negative: fail
29 : : */
30 : : static int
31 : 0 : hns3_shaper_para_calc(struct hns3_hw *hw, uint32_t ir, uint8_t shaper_level,
32 : : struct hns3_shaper_parameter *shaper_para)
33 : : {
34 : : #define SHAPER_DEFAULT_IR_B 126
35 : : #define DIVISOR_CLK (1000 * 8)
36 : : #define DIVISOR_IR_B_126 (126 * DIVISOR_CLK)
37 : :
38 : 0 : const uint16_t tick_array[HNS3_SHAPER_LVL_CNT] = {
39 : : 6 * 256, /* Priority level */
40 : : 6 * 32, /* Priority group level */
41 : : 6 * 8, /* Port level */
42 : : 6 * 256 /* Qset level */
43 : : };
44 : : uint8_t ir_u_calc = 0;
45 : : uint8_t ir_s_calc = 0;
46 : : uint32_t denominator;
47 : : uint32_t ir_calc;
48 : : uint32_t tick;
49 : :
50 : : /* Calc tick */
51 [ # # ]: 0 : if (shaper_level >= HNS3_SHAPER_LVL_CNT) {
52 : 0 : hns3_err(hw,
53 : : "shaper_level(%u) is greater than HNS3_SHAPER_LVL_CNT(%d)",
54 : : shaper_level, HNS3_SHAPER_LVL_CNT);
55 : 0 : return -EINVAL;
56 : : }
57 : :
58 [ # # ]: 0 : if (ir > hw->max_tm_rate) {
59 : 0 : hns3_err(hw, "rate(%u) exceeds the max rate(%u) driver "
60 : : "supported.", ir, hw->max_tm_rate);
61 : 0 : return -EINVAL;
62 : : }
63 : :
64 : 0 : tick = tick_array[shaper_level];
65 : :
66 : : /*
67 : : * Calc the speed if ir_b = 126, ir_u = 0 and ir_s = 0
68 : : * the formula is changed to:
69 : : * 126 * 1 * 8
70 : : * ir_calc = ---------------- * 1000
71 : : * tick * 1
72 : : */
73 : 0 : ir_calc = (DIVISOR_IR_B_126 + (tick >> 1) - 1) / tick;
74 : :
75 [ # # ]: 0 : if (ir_calc == ir) {
76 : 0 : shaper_para->ir_b = SHAPER_DEFAULT_IR_B;
77 [ # # ]: 0 : } else if (ir_calc > ir) {
78 : : /* Increasing the denominator to select ir_s value */
79 [ # # ]: 0 : while (ir_calc >= ir && ir) {
80 : 0 : ir_s_calc++;
81 : 0 : ir_calc = DIVISOR_IR_B_126 / (tick * (1 << ir_s_calc));
82 : : }
83 : :
84 : 0 : shaper_para->ir_b = (ir * tick * (1 << ir_s_calc) +
85 : 0 : (DIVISOR_CLK >> 1)) / DIVISOR_CLK;
86 : : } else {
87 : : /*
88 : : * Increasing the numerator to select ir_u value. ir_u_calc will
89 : : * get maximum value when ir_calc is minimum and ir is maximum.
90 : : * ir_calc gets minimum value when tick is the maximum value.
91 : : * At the same time, value of ir_u_calc can only be increased up
92 : : * to eight after the while loop if the value of ir is equal
93 : : * to hw->max_tm_rate.
94 : : */
95 : : uint32_t numerator;
96 : : do {
97 : 0 : ir_u_calc++;
98 : 0 : numerator = DIVISOR_IR_B_126 * (1 << ir_u_calc);
99 : 0 : ir_calc = (numerator + (tick >> 1)) / tick;
100 [ # # ]: 0 : } while (ir_calc < ir);
101 : :
102 [ # # ]: 0 : if (ir_calc == ir) {
103 : 0 : shaper_para->ir_b = SHAPER_DEFAULT_IR_B;
104 : : } else {
105 : : --ir_u_calc;
106 : :
107 : : /*
108 : : * The maximum value of ir_u_calc in this branch is
109 : : * seven in all cases. Thus, value of denominator can
110 : : * not be zero here.
111 : : */
112 : 0 : denominator = DIVISOR_CLK * (1 << ir_u_calc);
113 : 0 : shaper_para->ir_b =
114 : 0 : (ir * tick + (denominator >> 1)) / denominator;
115 : : }
116 : : }
117 : :
118 : 0 : shaper_para->ir_u = ir_u_calc;
119 : 0 : shaper_para->ir_s = ir_s_calc;
120 : :
121 : 0 : return 0;
122 : : }
123 : :
124 : : static int
125 : : hns3_fill_pri_array(struct hns3_hw *hw, uint8_t *pri, uint8_t pri_id)
126 : : {
127 : : #define HNS3_HALF_BYTE_BIT_OFFSET 4
128 : 0 : uint8_t tc = hw->dcb_info.prio_tc[pri_id];
129 : :
130 : 0 : if (tc >= hw->dcb_info.num_tc)
131 : : return -EINVAL;
132 : :
133 : : /*
134 : : * The register for priority has four bytes, the first bytes includes
135 : : * priority0 and priority1, the higher 4bit stands for priority1
136 : : * while the lower 4bit stands for priority0, as below:
137 : : * first byte: | pri_1 | pri_0 |
138 : : * second byte: | pri_3 | pri_2 |
139 : : * third byte: | pri_5 | pri_4 |
140 : : * fourth byte: | pri_7 | pri_6 |
141 : : */
142 : 0 : pri[pri_id >> 1] |= tc << ((pri_id & 1) * HNS3_HALF_BYTE_BIT_OFFSET);
143 : :
144 : : return 0;
145 : : }
146 : :
147 : : static int
148 : 0 : hns3_up_to_tc_map(struct hns3_hw *hw)
149 : : {
150 : : struct hns3_cmd_desc desc;
151 : : uint8_t *pri = (uint8_t *)desc.data;
152 : : uint8_t pri_id;
153 : : int ret;
154 : :
155 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PRI_TO_TC_MAPPING, false);
156 : :
157 [ # # ]: 0 : for (pri_id = 0; pri_id < HNS3_MAX_USER_PRIO; pri_id++) {
158 [ # # ]: 0 : ret = hns3_fill_pri_array(hw, pri, pri_id);
159 : : if (ret)
160 : : return ret;
161 : : }
162 : :
163 : 0 : return hns3_cmd_send(hw, &desc, 1);
164 : : }
165 : :
166 : : static int
167 : 0 : hns3_pg_to_pri_map_cfg(struct hns3_hw *hw, uint8_t pg_id, uint8_t pri_bit_map)
168 : : {
169 : : struct hns3_pg_to_pri_link_cmd *map;
170 : : struct hns3_cmd_desc desc;
171 : :
172 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PG_TO_PRI_LINK, false);
173 : :
174 : : map = (struct hns3_pg_to_pri_link_cmd *)desc.data;
175 : :
176 : 0 : map->pg_id = pg_id;
177 : 0 : map->pri_bit_map = pri_bit_map;
178 : :
179 : 0 : return hns3_cmd_send(hw, &desc, 1);
180 : : }
181 : :
182 : : static int
183 : 0 : hns3_pg_to_pri_map(struct hns3_hw *hw)
184 : : {
185 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
186 : : struct hns3_pf *pf = &hns->pf;
187 : : struct hns3_pg_info *pg_info;
188 : : int ret, i;
189 : :
190 [ # # ]: 0 : if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
191 : : return -EINVAL;
192 : :
193 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_pg; i++) {
194 : : /* Cfg pg to priority mapping */
195 : : pg_info = &hw->dcb_info.pg_info[i];
196 : 0 : ret = hns3_pg_to_pri_map_cfg(hw, i, pg_info->tc_bit_map);
197 [ # # ]: 0 : if (ret)
198 : 0 : return ret;
199 : : }
200 : :
201 : : return 0;
202 : : }
203 : :
204 : : static int
205 : 0 : hns3_qs_to_pri_map_cfg(struct hns3_hw *hw, uint16_t qs_id, uint8_t pri)
206 : : {
207 : : struct hns3_qs_to_pri_link_cmd *map;
208 : : struct hns3_cmd_desc desc;
209 : :
210 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_QS_TO_PRI_LINK, false);
211 : :
212 : : map = (struct hns3_qs_to_pri_link_cmd *)desc.data;
213 : :
214 : 0 : map->qs_id = rte_cpu_to_le_16(qs_id);
215 : 0 : map->priority = pri;
216 : 0 : map->link_vld = HNS3_DCB_QS_PRI_LINK_VLD_MSK;
217 : :
218 : 0 : return hns3_cmd_send(hw, &desc, 1);
219 : : }
220 : :
221 : : static int
222 : 0 : hns3_dcb_qs_weight_cfg(struct hns3_hw *hw, uint16_t qs_id, uint8_t dwrr)
223 : : {
224 : : struct hns3_qs_weight_cmd *weight;
225 : : struct hns3_cmd_desc desc;
226 : :
227 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_QS_WEIGHT, false);
228 : :
229 : : weight = (struct hns3_qs_weight_cmd *)desc.data;
230 : :
231 : 0 : weight->qs_id = rte_cpu_to_le_16(qs_id);
232 : 0 : weight->dwrr = dwrr;
233 : :
234 : 0 : return hns3_cmd_send(hw, &desc, 1);
235 : : }
236 : :
237 : : static int
238 : 0 : hns3_dcb_ets_tc_dwrr_cfg(struct hns3_hw *hw)
239 : : {
240 : : #define DEFAULT_TC_OFFSET 14
241 : : struct hns3_ets_tc_weight_cmd *ets_weight;
242 : : struct hns3_pg_info *pg_info;
243 : : struct hns3_cmd_desc desc;
244 : : uint8_t i;
245 : :
246 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_ETS_TC_WEIGHT, false);
247 : : ets_weight = (struct hns3_ets_tc_weight_cmd *)desc.data;
248 : :
249 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
250 : 0 : pg_info = &hw->dcb_info.pg_info[hw->dcb_info.tc_info[i].pgid];
251 : 0 : ets_weight->tc_weight[i] = pg_info->tc_dwrr[i];
252 : : }
253 : :
254 : 0 : ets_weight->weight_offset = DEFAULT_TC_OFFSET;
255 : :
256 : 0 : return hns3_cmd_send(hw, &desc, 1);
257 : : }
258 : :
259 : : static int
260 : 0 : hns3_dcb_pri_weight_cfg(struct hns3_hw *hw, uint8_t pri_id, uint8_t dwrr)
261 : : {
262 : : struct hns3_priority_weight_cmd *weight;
263 : : struct hns3_cmd_desc desc;
264 : :
265 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PRI_WEIGHT, false);
266 : :
267 : : weight = (struct hns3_priority_weight_cmd *)desc.data;
268 : :
269 : 0 : weight->pri_id = pri_id;
270 : 0 : weight->dwrr = dwrr;
271 : :
272 : 0 : return hns3_cmd_send(hw, &desc, 1);
273 : : }
274 : :
275 : : static int
276 : 0 : hns3_dcb_pg_weight_cfg(struct hns3_hw *hw, uint8_t pg_id, uint8_t dwrr)
277 : : {
278 : : struct hns3_pg_weight_cmd *weight;
279 : : struct hns3_cmd_desc desc;
280 : :
281 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PG_WEIGHT, false);
282 : :
283 : : weight = (struct hns3_pg_weight_cmd *)desc.data;
284 : :
285 : 0 : weight->pg_id = pg_id;
286 : 0 : weight->dwrr = dwrr;
287 : :
288 : 0 : return hns3_cmd_send(hw, &desc, 1);
289 : : }
290 : : static int
291 : 0 : hns3_dcb_pg_schd_mode_cfg(struct hns3_hw *hw, uint8_t pg_id)
292 : : {
293 : : struct hns3_cmd_desc desc;
294 : :
295 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PG_SCH_MODE_CFG, false);
296 : :
297 [ # # ]: 0 : if (hw->dcb_info.pg_info[pg_id].pg_sch_mode == HNS3_SCH_MODE_DWRR)
298 : 0 : desc.data[1] = rte_cpu_to_le_32(HNS3_DCB_TX_SCHD_DWRR_MSK);
299 : : else
300 : 0 : desc.data[1] = 0;
301 : :
302 : 0 : desc.data[0] = rte_cpu_to_le_32(pg_id);
303 : :
304 : 0 : return hns3_cmd_send(hw, &desc, 1);
305 : : }
306 : :
307 : : static uint32_t
308 : : hns3_dcb_get_shapping_para(uint8_t ir_b, uint8_t ir_u, uint8_t ir_s,
309 : : uint8_t bs_b, uint8_t bs_s)
310 : : {
311 : : uint32_t shapping_para = 0;
312 : :
313 : : /* If ir_b is zero it means IR is 0Mbps, return zero of shapping_para */
314 : 0 : if (ir_b == 0)
315 : : return shapping_para;
316 : :
317 : : hns3_dcb_set_field(shapping_para, IR_B, ir_b);
318 : 0 : hns3_dcb_set_field(shapping_para, IR_U, ir_u);
319 : 0 : hns3_dcb_set_field(shapping_para, IR_S, ir_s);
320 : : hns3_dcb_set_field(shapping_para, BS_B, bs_b);
321 : 0 : hns3_dcb_set_field(shapping_para, BS_S, bs_s);
322 : :
323 : 0 : return shapping_para;
324 : : }
325 : :
326 : : static int
327 : 0 : hns3_dcb_port_shaper_cfg(struct hns3_hw *hw, uint32_t speed)
328 : : {
329 : : struct hns3_port_shapping_cmd *shap_cfg_cmd;
330 : : struct hns3_shaper_parameter shaper_parameter;
331 : : uint32_t shapping_para;
332 : : uint32_t ir_u, ir_b, ir_s;
333 : : struct hns3_cmd_desc desc;
334 : : int ret;
335 : :
336 : 0 : ret = hns3_shaper_para_calc(hw, speed,
337 : : HNS3_SHAPER_LVL_PORT, &shaper_parameter);
338 [ # # ]: 0 : if (ret) {
339 : 0 : hns3_err(hw, "calculate shaper parameter failed: %d", ret);
340 : 0 : return ret;
341 : : }
342 : :
343 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PORT_SHAPPING, false);
344 : : shap_cfg_cmd = (struct hns3_port_shapping_cmd *)desc.data;
345 : :
346 : 0 : ir_b = shaper_parameter.ir_b;
347 : 0 : ir_u = shaper_parameter.ir_u;
348 : 0 : ir_s = shaper_parameter.ir_s;
349 [ # # ]: 0 : shapping_para = hns3_dcb_get_shapping_para(ir_b, ir_u, ir_s,
350 : : HNS3_SHAPER_BS_U_DEF,
351 : : HNS3_SHAPER_BS_S_DEF);
352 : :
353 : 0 : shap_cfg_cmd->port_shapping_para = rte_cpu_to_le_32(shapping_para);
354 : :
355 : : /*
356 : : * Configure the port_rate and set bit HNS3_TM_RATE_VLD_B of flag
357 : : * field in hns3_port_shapping_cmd to require firmware to recalculate
358 : : * shapping parameters. And whether the parameters are recalculated
359 : : * depends on the firmware version. But driver still needs to
360 : : * calculate it and configure to firmware for better compatibility.
361 : : */
362 : 0 : shap_cfg_cmd->port_rate = rte_cpu_to_le_32(speed);
363 : 0 : hns3_set_bit(shap_cfg_cmd->flag, HNS3_TM_RATE_VLD_B, 1);
364 : :
365 : 0 : return hns3_cmd_send(hw, &desc, 1);
366 : : }
367 : :
368 : : int
369 : 0 : hns3_port_shaper_update(struct hns3_hw *hw, uint32_t speed)
370 : : {
371 : : int ret;
372 : :
373 : 0 : ret = hns3_dcb_port_shaper_cfg(hw, speed);
374 [ # # ]: 0 : if (ret)
375 : 0 : hns3_err(hw, "configure port shappering failed: ret = %d", ret);
376 : :
377 : 0 : return ret;
378 : : }
379 : :
380 : : static int
381 : 0 : hns3_dcb_pg_shapping_cfg(struct hns3_hw *hw, enum hns3_shap_bucket bucket,
382 : : uint8_t pg_id, uint32_t shapping_para, uint32_t rate)
383 : : {
384 : : struct hns3_pg_shapping_cmd *shap_cfg_cmd;
385 : : enum hns3_opcode_type opcode;
386 : : struct hns3_cmd_desc desc;
387 : :
388 [ # # ]: 0 : opcode = bucket ? HNS3_OPC_TM_PG_P_SHAPPING :
389 : : HNS3_OPC_TM_PG_C_SHAPPING;
390 : 0 : hns3_cmd_setup_basic_desc(&desc, opcode, false);
391 : :
392 : : shap_cfg_cmd = (struct hns3_pg_shapping_cmd *)desc.data;
393 : :
394 : 0 : shap_cfg_cmd->pg_id = pg_id;
395 : :
396 : 0 : shap_cfg_cmd->pg_shapping_para = rte_cpu_to_le_32(shapping_para);
397 : :
398 : : /*
399 : : * Configure the pg_rate and set bit HNS3_TM_RATE_VLD_B of flag field in
400 : : * hns3_pg_shapping_cmd to require firmware to recalculate shapping
401 : : * parameters. And whether parameters are recalculated depends on
402 : : * the firmware version. But driver still needs to calculate it and
403 : : * configure to firmware for better compatibility.
404 : : */
405 : 0 : shap_cfg_cmd->pg_rate = rte_cpu_to_le_32(rate);
406 : 0 : hns3_set_bit(shap_cfg_cmd->flag, HNS3_TM_RATE_VLD_B, 1);
407 : :
408 : 0 : return hns3_cmd_send(hw, &desc, 1);
409 : : }
410 : :
411 : : static int
412 : 0 : hns3_pg_shaper_rate_cfg(struct hns3_hw *hw, uint8_t pg_id, uint32_t rate)
413 : : {
414 : : struct hns3_shaper_parameter shaper_parameter;
415 : : uint32_t ir_u, ir_b, ir_s;
416 : : uint32_t shaper_para;
417 : : int ret;
418 : :
419 : : /* Calc shaper para */
420 : 0 : ret = hns3_shaper_para_calc(hw, rate, HNS3_SHAPER_LVL_PG,
421 : : &shaper_parameter);
422 [ # # ]: 0 : if (ret) {
423 : 0 : hns3_err(hw, "calculate shaper parameter fail, ret = %d.",
424 : : ret);
425 : 0 : return ret;
426 : : }
427 : :
428 : : shaper_para = hns3_dcb_get_shapping_para(0, 0, 0,
429 : : HNS3_SHAPER_BS_U_DEF,
430 : : HNS3_SHAPER_BS_S_DEF);
431 : :
432 : 0 : ret = hns3_dcb_pg_shapping_cfg(hw, HNS3_DCB_SHAP_C_BUCKET, pg_id,
433 : : shaper_para, rate);
434 [ # # ]: 0 : if (ret) {
435 : 0 : hns3_err(hw, "config PG CIR shaper parameter fail, ret = %d.",
436 : : ret);
437 : 0 : return ret;
438 : : }
439 : :
440 : 0 : ir_b = shaper_parameter.ir_b;
441 : 0 : ir_u = shaper_parameter.ir_u;
442 : 0 : ir_s = shaper_parameter.ir_s;
443 [ # # ]: 0 : shaper_para = hns3_dcb_get_shapping_para(ir_b, ir_u, ir_s,
444 : : HNS3_SHAPER_BS_U_DEF,
445 : : HNS3_SHAPER_BS_S_DEF);
446 : :
447 : 0 : ret = hns3_dcb_pg_shapping_cfg(hw, HNS3_DCB_SHAP_P_BUCKET, pg_id,
448 : : shaper_para, rate);
449 [ # # ]: 0 : if (ret) {
450 : 0 : hns3_err(hw, "config PG PIR shaper parameter fail, ret = %d.",
451 : : ret);
452 : 0 : return ret;
453 : : }
454 : :
455 : : return 0;
456 : : }
457 : :
458 : : static int
459 : 0 : hns3_dcb_pg_shaper_cfg(struct hns3_hw *hw)
460 : : {
461 : : struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
462 : : uint32_t rate;
463 : : uint8_t i;
464 : : int ret;
465 : :
466 : : /* Cfg pg schd */
467 [ # # ]: 0 : if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
468 : : return -EINVAL;
469 : :
470 : : /* Pg to pri */
471 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_pg; i++) {
472 : 0 : rate = hw->dcb_info.pg_info[i].bw_limit;
473 : 0 : ret = hns3_pg_shaper_rate_cfg(hw, i, rate);
474 [ # # ]: 0 : if (ret)
475 : 0 : return ret;
476 : : }
477 : :
478 : : return 0;
479 : : }
480 : :
481 : : static int
482 : 0 : hns3_dcb_qs_schd_mode_cfg(struct hns3_hw *hw, uint16_t qs_id, uint8_t mode)
483 : : {
484 : : struct hns3_cmd_desc desc;
485 : :
486 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_QS_SCH_MODE_CFG, false);
487 : :
488 [ # # ]: 0 : if (mode == HNS3_SCH_MODE_DWRR)
489 : 0 : desc.data[1] = rte_cpu_to_le_32(HNS3_DCB_TX_SCHD_DWRR_MSK);
490 : : else
491 : 0 : desc.data[1] = 0;
492 : :
493 : 0 : desc.data[0] = rte_cpu_to_le_32(qs_id);
494 : :
495 : 0 : return hns3_cmd_send(hw, &desc, 1);
496 : : }
497 : :
498 : : static int
499 : 0 : hns3_dcb_pri_schd_mode_cfg(struct hns3_hw *hw, uint8_t pri_id)
500 : : {
501 : : struct hns3_cmd_desc desc;
502 : :
503 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_PRI_SCH_MODE_CFG, false);
504 : :
505 [ # # ]: 0 : if (hw->dcb_info.tc_info[pri_id].tc_sch_mode == HNS3_SCH_MODE_DWRR)
506 : 0 : desc.data[1] = rte_cpu_to_le_32(HNS3_DCB_TX_SCHD_DWRR_MSK);
507 : : else
508 : 0 : desc.data[1] = 0;
509 : :
510 : 0 : desc.data[0] = rte_cpu_to_le_32(pri_id);
511 : :
512 : 0 : return hns3_cmd_send(hw, &desc, 1);
513 : : }
514 : :
515 : : static int
516 : 0 : hns3_dcb_pri_shapping_cfg(struct hns3_hw *hw, enum hns3_shap_bucket bucket,
517 : : uint8_t pri_id, uint32_t shapping_para, uint32_t rate)
518 : : {
519 : : struct hns3_pri_shapping_cmd *shap_cfg_cmd;
520 : : enum hns3_opcode_type opcode;
521 : : struct hns3_cmd_desc desc;
522 : :
523 [ # # ]: 0 : opcode = bucket ? HNS3_OPC_TM_PRI_P_SHAPPING :
524 : : HNS3_OPC_TM_PRI_C_SHAPPING;
525 : :
526 : 0 : hns3_cmd_setup_basic_desc(&desc, opcode, false);
527 : :
528 : : shap_cfg_cmd = (struct hns3_pri_shapping_cmd *)desc.data;
529 : :
530 : 0 : shap_cfg_cmd->pri_id = pri_id;
531 : :
532 : 0 : shap_cfg_cmd->pri_shapping_para = rte_cpu_to_le_32(shapping_para);
533 : :
534 : : /*
535 : : * Configure the pri_rate and set bit HNS3_TM_RATE_VLD_B of flag
536 : : * field in hns3_pri_shapping_cmd to require firmware to recalculate
537 : : * shapping parameters. And whether the parameters are recalculated
538 : : * depends on the firmware version. But driver still needs to
539 : : * calculate it and configure to firmware for better compatibility.
540 : : */
541 : 0 : shap_cfg_cmd->pri_rate = rte_cpu_to_le_32(rate);
542 : 0 : hns3_set_bit(shap_cfg_cmd->flag, HNS3_TM_RATE_VLD_B, 1);
543 : :
544 : 0 : return hns3_cmd_send(hw, &desc, 1);
545 : : }
546 : :
547 : : static int
548 : 0 : hns3_pri_shaper_rate_cfg(struct hns3_hw *hw, uint8_t tc_no, uint32_t rate)
549 : : {
550 : : struct hns3_shaper_parameter shaper_parameter;
551 : : uint32_t ir_u, ir_b, ir_s;
552 : : uint32_t shaper_para;
553 : : int ret;
554 : :
555 : 0 : ret = hns3_shaper_para_calc(hw, rate, HNS3_SHAPER_LVL_PRI,
556 : : &shaper_parameter);
557 [ # # ]: 0 : if (ret) {
558 : 0 : hns3_err(hw, "calculate shaper parameter failed: %d.",
559 : : ret);
560 : 0 : return ret;
561 : : }
562 : :
563 : : shaper_para = hns3_dcb_get_shapping_para(0, 0, 0,
564 : : HNS3_SHAPER_BS_U_DEF,
565 : : HNS3_SHAPER_BS_S_DEF);
566 : :
567 : 0 : ret = hns3_dcb_pri_shapping_cfg(hw, HNS3_DCB_SHAP_C_BUCKET, tc_no,
568 : : shaper_para, rate);
569 [ # # ]: 0 : if (ret) {
570 : 0 : hns3_err(hw,
571 : : "config priority CIR shaper parameter failed: %d.",
572 : : ret);
573 : 0 : return ret;
574 : : }
575 : :
576 : 0 : ir_b = shaper_parameter.ir_b;
577 : 0 : ir_u = shaper_parameter.ir_u;
578 : 0 : ir_s = shaper_parameter.ir_s;
579 [ # # ]: 0 : shaper_para = hns3_dcb_get_shapping_para(ir_b, ir_u, ir_s,
580 : : HNS3_SHAPER_BS_U_DEF,
581 : : HNS3_SHAPER_BS_S_DEF);
582 : :
583 : 0 : ret = hns3_dcb_pri_shapping_cfg(hw, HNS3_DCB_SHAP_P_BUCKET, tc_no,
584 : : shaper_para, rate);
585 [ # # ]: 0 : if (ret) {
586 : 0 : hns3_err(hw,
587 : : "config priority PIR shaper parameter failed: %d.",
588 : : ret);
589 : 0 : return ret;
590 : : }
591 : :
592 : : return 0;
593 : : }
594 : :
595 : : static int
596 : 0 : hns3_dcb_pri_shaper_cfg(struct hns3_hw *hw)
597 : : {
598 : : struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
599 : : uint32_t rate;
600 : : uint8_t i;
601 : : int ret;
602 : :
603 [ # # ]: 0 : if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
604 : : return -EINVAL;
605 : :
606 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
607 : 0 : rate = hw->dcb_info.tc_info[i].bw_limit;
608 : 0 : ret = hns3_pri_shaper_rate_cfg(hw, i, rate);
609 [ # # ]: 0 : if (ret) {
610 : 0 : hns3_err(hw, "config pri shaper failed: %d.", ret);
611 : 0 : return ret;
612 : : }
613 : : }
614 : :
615 : : return 0;
616 : : }
617 : :
618 : : static int
619 : 0 : hns3_set_rss_size(struct hns3_hw *hw, uint16_t nb_rx_q)
620 : : {
621 : : struct hns3_rss_conf *rss_cfg = &hw->rss_info;
622 : : uint16_t rx_qnum_per_tc;
623 : : uint16_t used_rx_queues;
624 : : uint16_t i;
625 : :
626 : 0 : rx_qnum_per_tc = nb_rx_q / hw->dcb_info.num_tc;
627 [ # # ]: 0 : if (rx_qnum_per_tc > hw->rss_size_max) {
628 : 0 : hns3_err(hw, "rx queue number of per tc (%u) is greater than "
629 : : "value (%u) hardware supported.",
630 : : rx_qnum_per_tc, hw->rss_size_max);
631 : 0 : return -EINVAL;
632 : : }
633 : :
634 : 0 : used_rx_queues = hw->dcb_info.num_tc * rx_qnum_per_tc;
635 [ # # ]: 0 : if (used_rx_queues != nb_rx_q) {
636 : 0 : hns3_err(hw, "rx queue number (%u) configured must be an "
637 : : "integral multiple of valid tc number (%u).",
638 : : nb_rx_q, hw->dcb_info.num_tc);
639 : 0 : return -EINVAL;
640 : : }
641 : 0 : hw->alloc_rss_size = rx_qnum_per_tc;
642 : 0 : hw->used_rx_queues = used_rx_queues;
643 : :
644 : : /*
645 : : * When rss size is changed, we need to update rss redirection table
646 : : * maintained by driver. Besides, during the entire reset process, we
647 : : * need to ensure that the rss table information are not overwritten
648 : : * and configured directly to the hardware in the RESET_STAGE_RESTORE
649 : : * stage of the reset process.
650 : : */
651 [ # # ]: 0 : if (rte_atomic_load_explicit(&hw->reset.resetting, rte_memory_order_relaxed) == 0) {
652 [ # # ]: 0 : for (i = 0; i < hw->rss_ind_tbl_size; i++)
653 : 0 : rss_cfg->rss_indirection_tbl[i] =
654 : 0 : i % hw->alloc_rss_size;
655 : : }
656 : :
657 : : return 0;
658 : : }
659 : :
660 : : static int
661 : 0 : hns3_tc_queue_mapping_cfg(struct hns3_hw *hw, uint16_t nb_tx_q)
662 : : {
663 : : struct hns3_tc_queue_info *tc_queue;
664 : : uint16_t used_tx_queues;
665 : : uint16_t tx_qnum_per_tc;
666 : : uint8_t i;
667 : :
668 : 0 : tx_qnum_per_tc = nb_tx_q / hw->dcb_info.num_tc;
669 : 0 : used_tx_queues = hw->dcb_info.num_tc * tx_qnum_per_tc;
670 [ # # ]: 0 : if (used_tx_queues != nb_tx_q) {
671 : 0 : hns3_err(hw, "tx queue number (%u) configured must be an "
672 : : "integral multiple of valid tc number (%u).",
673 : : nb_tx_q, hw->dcb_info.num_tc);
674 : 0 : return -EINVAL;
675 : : }
676 : :
677 : 0 : hw->used_tx_queues = used_tx_queues;
678 : 0 : hw->tx_qnum_per_tc = tx_qnum_per_tc;
679 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
680 : 0 : tc_queue = &hw->tc_queue[i];
681 [ # # # # ]: 0 : if (hw->dcb_info.hw_tc_map & BIT(i) && i < hw->dcb_info.num_tc) {
682 : 0 : tc_queue->enable = true;
683 : 0 : tc_queue->tqp_offset = i * hw->tx_qnum_per_tc;
684 : 0 : tc_queue->tqp_count = hw->tx_qnum_per_tc;
685 : 0 : tc_queue->tc = i;
686 : : } else {
687 : : /* Set to default queue if TC is disable */
688 : 0 : tc_queue->enable = false;
689 : 0 : tc_queue->tqp_offset = 0;
690 : 0 : tc_queue->tqp_count = 0;
691 : 0 : tc_queue->tc = 0;
692 : : }
693 : : }
694 : :
695 : : return 0;
696 : : }
697 : :
698 : : uint8_t
699 : 0 : hns3_txq_mapped_tc_get(struct hns3_hw *hw, uint16_t txq_no)
700 : : {
701 : : struct hns3_tc_queue_info *tc_queue;
702 : : uint8_t i;
703 : :
704 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
705 : 0 : tc_queue = &hw->tc_queue[i];
706 [ # # ]: 0 : if (!tc_queue->enable)
707 : 0 : continue;
708 : :
709 [ # # ]: 0 : if (txq_no >= tc_queue->tqp_offset &&
710 [ # # ]: 0 : txq_no < tc_queue->tqp_offset + tc_queue->tqp_count)
711 : 0 : return i;
712 : : }
713 : :
714 : : /* return TC0 in default case */
715 : : return 0;
716 : : }
717 : :
718 : : int
719 : 0 : hns3_queue_to_tc_mapping(struct hns3_hw *hw, uint16_t nb_rx_q, uint16_t nb_tx_q)
720 : : {
721 : : int ret;
722 : :
723 [ # # ]: 0 : if (nb_rx_q < hw->dcb_info.num_tc) {
724 : 0 : hns3_err(hw, "number of Rx queues(%u) is less than number of TC(%u).",
725 : : nb_rx_q, hw->dcb_info.num_tc);
726 : 0 : return -EINVAL;
727 : : }
728 : :
729 [ # # ]: 0 : if (nb_tx_q < hw->dcb_info.num_tc) {
730 : 0 : hns3_err(hw, "number of Tx queues(%u) is less than number of TC(%u).",
731 : : nb_tx_q, hw->dcb_info.num_tc);
732 : 0 : return -EINVAL;
733 : : }
734 : :
735 : 0 : ret = hns3_set_rss_size(hw, nb_rx_q);
736 [ # # ]: 0 : if (ret)
737 : : return ret;
738 : :
739 : 0 : return hns3_tc_queue_mapping_cfg(hw, nb_tx_q);
740 : : }
741 : :
742 : : int
743 : 0 : hns3_dcb_info_init(struct hns3_hw *hw)
744 : : {
745 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
746 : : struct hns3_pf *pf = &hns->pf;
747 : : int i, k;
748 : :
749 [ # # ]: 0 : if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE &&
750 [ # # ]: 0 : hw->dcb_info.num_pg != 1)
751 : : return -EINVAL;
752 : :
753 : : /* Initializing PG information */
754 : 0 : memset(hw->dcb_info.pg_info, 0,
755 : : sizeof(struct hns3_pg_info) * HNS3_PG_NUM);
756 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_pg; i++) {
757 [ # # ]: 0 : hw->dcb_info.pg_dwrr[i] = i ? 0 : BW_MAX_PERCENT;
758 : 0 : hw->dcb_info.pg_info[i].pg_id = i;
759 : 0 : hw->dcb_info.pg_info[i].pg_sch_mode = HNS3_SCH_MODE_DWRR;
760 : 0 : hw->dcb_info.pg_info[i].bw_limit = hw->max_tm_rate;
761 : :
762 [ # # ]: 0 : if (i != 0)
763 : 0 : continue;
764 : :
765 : 0 : hw->dcb_info.pg_info[i].tc_bit_map = hw->dcb_info.hw_tc_map;
766 [ # # ]: 0 : for (k = 0; k < hw->dcb_info.num_tc; k++)
767 : 0 : hw->dcb_info.pg_info[i].tc_dwrr[k] = BW_MAX_PERCENT;
768 : : }
769 : :
770 : : /* All UPs mapping to TC0 */
771 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
772 : 0 : hw->dcb_info.prio_tc[i] = 0;
773 : :
774 : : /* Initializing tc information */
775 : 0 : memset(hw->dcb_info.tc_info, 0,
776 : : sizeof(struct hns3_tc_info) * HNS3_MAX_TC_NUM);
777 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
778 : 0 : hw->dcb_info.tc_info[i].tc_id = i;
779 : 0 : hw->dcb_info.tc_info[i].tc_sch_mode = HNS3_SCH_MODE_DWRR;
780 : 0 : hw->dcb_info.tc_info[i].pgid = 0;
781 : 0 : hw->dcb_info.tc_info[i].bw_limit =
782 : 0 : hw->dcb_info.pg_info[0].bw_limit;
783 : : }
784 : :
785 : : return 0;
786 : : }
787 : :
788 : : static int
789 : 0 : hns3_dcb_lvl2_schd_mode_cfg(struct hns3_hw *hw)
790 : : {
791 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
792 : : struct hns3_pf *pf = &hns->pf;
793 : : int ret, i;
794 : :
795 : : /* Only being config on TC-Based scheduler mode */
796 [ # # ]: 0 : if (pf->tx_sch_mode == HNS3_FLAG_VNET_BASE_SCH_MODE)
797 : : return -EINVAL;
798 : :
799 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_pg; i++) {
800 : 0 : ret = hns3_dcb_pg_schd_mode_cfg(hw, i);
801 [ # # ]: 0 : if (ret)
802 : 0 : return ret;
803 : : }
804 : :
805 : : return 0;
806 : : }
807 : :
808 : : static int
809 : 0 : hns3_dcb_lvl34_schd_mode_cfg(struct hns3_hw *hw)
810 : : {
811 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
812 : : struct hns3_pf *pf = &hns->pf;
813 : : uint8_t i;
814 : : int ret;
815 : :
816 [ # # ]: 0 : if (pf->tx_sch_mode == HNS3_FLAG_TC_BASE_SCH_MODE) {
817 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
818 : 0 : ret = hns3_dcb_pri_schd_mode_cfg(hw, i);
819 [ # # ]: 0 : if (ret)
820 : 0 : return ret;
821 : :
822 : 0 : ret = hns3_dcb_qs_schd_mode_cfg(hw, i,
823 : : HNS3_SCH_MODE_DWRR);
824 [ # # ]: 0 : if (ret)
825 : 0 : return ret;
826 : : }
827 : : }
828 : :
829 : : return 0;
830 : : }
831 : :
832 : : static int
833 : 0 : hns3_dcb_schd_mode_cfg(struct hns3_hw *hw)
834 : : {
835 : : int ret;
836 : :
837 : 0 : ret = hns3_dcb_lvl2_schd_mode_cfg(hw);
838 [ # # ]: 0 : if (ret) {
839 : 0 : hns3_err(hw, "config lvl2_schd_mode failed: %d", ret);
840 : 0 : return ret;
841 : : }
842 : :
843 : 0 : ret = hns3_dcb_lvl34_schd_mode_cfg(hw);
844 [ # # ]: 0 : if (ret)
845 : 0 : hns3_err(hw, "config lvl34_schd_mode failed: %d", ret);
846 : :
847 : : return ret;
848 : : }
849 : :
850 : : static int
851 : 0 : hns3_dcb_pri_tc_base_dwrr_cfg(struct hns3_hw *hw)
852 : : {
853 : : struct hns3_pg_info *pg_info;
854 : : uint8_t dwrr;
855 : : int ret, i;
856 : :
857 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
858 : 0 : pg_info = &hw->dcb_info.pg_info[hw->dcb_info.tc_info[i].pgid];
859 : 0 : dwrr = pg_info->tc_dwrr[i];
860 : :
861 : 0 : ret = hns3_dcb_pri_weight_cfg(hw, i, dwrr);
862 [ # # ]: 0 : if (ret) {
863 : 0 : hns3_err(hw, "fail to send priority weight cmd: %d, ret = %d",
864 : : i, ret);
865 : 0 : return ret;
866 : : }
867 : :
868 : 0 : ret = hns3_dcb_qs_weight_cfg(hw, i, BW_MAX_PERCENT);
869 [ # # ]: 0 : if (ret) {
870 : 0 : hns3_err(hw, "fail to send qs_weight cmd: %d, ret = %d",
871 : : i, ret);
872 : 0 : return ret;
873 : : }
874 : : }
875 : :
876 : : return 0;
877 : : }
878 : :
879 : : static int
880 : 0 : hns3_dcb_pri_dwrr_cfg(struct hns3_hw *hw)
881 : : {
882 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
883 : : struct hns3_pf *pf = &hns->pf;
884 : : uint32_t version;
885 : : int ret;
886 : :
887 [ # # ]: 0 : if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
888 : : return -EINVAL;
889 : :
890 : 0 : ret = hns3_dcb_pri_tc_base_dwrr_cfg(hw);
891 [ # # ]: 0 : if (ret)
892 : : return ret;
893 : :
894 [ # # ]: 0 : if (!hns3_dev_get_support(hw, DCB))
895 : : return 0;
896 : :
897 : 0 : ret = hns3_dcb_ets_tc_dwrr_cfg(hw);
898 [ # # ]: 0 : if (ret == -EOPNOTSUPP) {
899 : 0 : version = hw->fw_version;
900 : 0 : hns3_warn(hw,
901 : : "fw %lu.%lu.%lu.%lu doesn't support ets tc weight cmd",
902 : : hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
903 : : HNS3_FW_VERSION_BYTE3_S),
904 : : hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
905 : : HNS3_FW_VERSION_BYTE2_S),
906 : : hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
907 : : HNS3_FW_VERSION_BYTE1_S),
908 : : hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
909 : : HNS3_FW_VERSION_BYTE0_S));
910 : : ret = 0;
911 : : }
912 : :
913 : : return ret;
914 : : }
915 : :
916 : : static int
917 : 0 : hns3_dcb_pg_dwrr_cfg(struct hns3_hw *hw)
918 : : {
919 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
920 : : struct hns3_pf *pf = &hns->pf;
921 : : int ret, i;
922 : :
923 : : /* Cfg pg schd */
924 [ # # ]: 0 : if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
925 : : return -EINVAL;
926 : :
927 : : /* Cfg pg to prio */
928 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_pg; i++) {
929 : : /* Cfg dwrr */
930 : 0 : ret = hns3_dcb_pg_weight_cfg(hw, i, hw->dcb_info.pg_dwrr[i]);
931 [ # # ]: 0 : if (ret)
932 : 0 : return ret;
933 : : }
934 : :
935 : : return 0;
936 : : }
937 : :
938 : : static int
939 : 0 : hns3_dcb_dwrr_cfg(struct hns3_hw *hw)
940 : : {
941 : : int ret;
942 : :
943 : 0 : ret = hns3_dcb_pg_dwrr_cfg(hw);
944 [ # # ]: 0 : if (ret) {
945 : 0 : hns3_err(hw, "config pg_dwrr failed: %d", ret);
946 : 0 : return ret;
947 : : }
948 : :
949 : 0 : ret = hns3_dcb_pri_dwrr_cfg(hw);
950 [ # # ]: 0 : if (ret)
951 : 0 : hns3_err(hw, "config pri_dwrr failed: %d", ret);
952 : :
953 : : return ret;
954 : : }
955 : :
956 : : static int
957 : 0 : hns3_dcb_shaper_cfg(struct hns3_hw *hw)
958 : : {
959 : : int ret;
960 : :
961 : 0 : ret = hns3_dcb_port_shaper_cfg(hw, hw->mac.link_speed);
962 [ # # ]: 0 : if (ret) {
963 : 0 : hns3_err(hw, "config port shaper failed: %d", ret);
964 : 0 : return ret;
965 : : }
966 : :
967 : 0 : ret = hns3_dcb_pg_shaper_cfg(hw);
968 [ # # ]: 0 : if (ret) {
969 : 0 : hns3_err(hw, "config pg shaper failed: %d", ret);
970 : 0 : return ret;
971 : : }
972 : :
973 : 0 : return hns3_dcb_pri_shaper_cfg(hw);
974 : : }
975 : :
976 : : static int
977 : 0 : hns3_q_to_qs_map_cfg(struct hns3_hw *hw, uint16_t q_id, uint16_t qs_id)
978 : : {
979 : : struct hns3_nq_to_qs_link_cmd *map;
980 : : struct hns3_cmd_desc desc;
981 : : uint16_t tmp_qs_id = 0;
982 : : uint16_t qs_id_l;
983 : : uint16_t qs_id_h;
984 : :
985 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_NQ_TO_QS_LINK, false);
986 : :
987 : : map = (struct hns3_nq_to_qs_link_cmd *)desc.data;
988 : :
989 : 0 : map->nq_id = rte_cpu_to_le_16(q_id);
990 : :
991 : : /*
992 : : * Network engine with revision_id 0x21 uses 0~9 bit of qs_id to
993 : : * configure qset_id. So we need to convert qs_id to the follow
994 : : * format to support qset_id > 1024.
995 : : * qs_id: | 15 | 14 ~ 10 | 9 ~ 0 |
996 : : * / / \ \
997 : : * / / \ \
998 : : * qset_id: | 15 ~ 11 | 10 | 9 ~ 0 |
999 : : * | qs_id_h | vld | qs_id_l |
1000 : : */
1001 : 0 : qs_id_l = hns3_get_field(qs_id, HNS3_DCB_QS_ID_L_MSK,
1002 : : HNS3_DCB_QS_ID_L_S);
1003 : 0 : qs_id_h = hns3_get_field(qs_id, HNS3_DCB_QS_ID_H_MSK,
1004 : : HNS3_DCB_QS_ID_H_S);
1005 : : hns3_set_field(tmp_qs_id, HNS3_DCB_QS_ID_L_MSK, HNS3_DCB_QS_ID_L_S,
1006 : : qs_id_l);
1007 : 0 : hns3_set_field(tmp_qs_id, HNS3_DCB_QS_ID_H_EXT_MSK,
1008 : : HNS3_DCB_QS_ID_H_EXT_S, qs_id_h);
1009 : 0 : map->qset_id = rte_cpu_to_le_16(tmp_qs_id | HNS3_DCB_Q_QS_LINK_VLD_MSK);
1010 : :
1011 : 0 : return hns3_cmd_send(hw, &desc, 1);
1012 : : }
1013 : :
1014 : : static int
1015 : 0 : hns3_q_to_qs_map(struct hns3_hw *hw)
1016 : : {
1017 : : struct hns3_tc_queue_info *tc_queue;
1018 : : uint16_t q_id;
1019 : : uint32_t i, j;
1020 : : int ret;
1021 : :
1022 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
1023 : : tc_queue = &hw->tc_queue[i];
1024 [ # # ]: 0 : for (j = 0; j < tc_queue->tqp_count; j++) {
1025 : 0 : q_id = tc_queue->tqp_offset + j;
1026 : 0 : ret = hns3_q_to_qs_map_cfg(hw, q_id, i);
1027 [ # # ]: 0 : if (ret)
1028 : 0 : return ret;
1029 : : }
1030 : : }
1031 : :
1032 : : return 0;
1033 : : }
1034 : :
1035 : : static int
1036 : 0 : hns3_pri_q_qs_cfg(struct hns3_hw *hw)
1037 : : {
1038 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1039 : : struct hns3_pf *pf = &hns->pf;
1040 : : uint32_t i;
1041 : : int ret;
1042 : :
1043 [ # # ]: 0 : if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE)
1044 : : return -EINVAL;
1045 : :
1046 : : /* Cfg qs -> pri mapping */
1047 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
1048 : 0 : ret = hns3_qs_to_pri_map_cfg(hw, i, i);
1049 [ # # ]: 0 : if (ret) {
1050 : 0 : hns3_err(hw, "qs_to_pri mapping fail: %d", ret);
1051 : 0 : return ret;
1052 : : }
1053 : : }
1054 : :
1055 : : /* Cfg q -> qs mapping */
1056 : 0 : ret = hns3_q_to_qs_map(hw);
1057 [ # # ]: 0 : if (ret)
1058 : 0 : hns3_err(hw, "nq_to_qs mapping fail: %d", ret);
1059 : :
1060 : : return ret;
1061 : : }
1062 : :
1063 : : static int
1064 : 0 : hns3_dcb_map_cfg(struct hns3_hw *hw)
1065 : : {
1066 : : int ret;
1067 : :
1068 : 0 : ret = hns3_up_to_tc_map(hw);
1069 [ # # ]: 0 : if (ret) {
1070 : 0 : hns3_err(hw, "up_to_tc mapping fail: %d", ret);
1071 : 0 : return ret;
1072 : : }
1073 : :
1074 : 0 : ret = hns3_pg_to_pri_map(hw);
1075 [ # # ]: 0 : if (ret) {
1076 : 0 : hns3_err(hw, "pg_to_pri mapping fail: %d", ret);
1077 : 0 : return ret;
1078 : : }
1079 : :
1080 : 0 : return hns3_pri_q_qs_cfg(hw);
1081 : : }
1082 : :
1083 : : static int
1084 : 0 : hns3_dcb_schd_setup_hw(struct hns3_hw *hw)
1085 : : {
1086 : : int ret;
1087 : :
1088 : : /* Cfg dcb mapping */
1089 : 0 : ret = hns3_dcb_map_cfg(hw);
1090 [ # # ]: 0 : if (ret)
1091 : : return ret;
1092 : :
1093 : : /* Cfg dcb shaper */
1094 : 0 : ret = hns3_dcb_shaper_cfg(hw);
1095 [ # # ]: 0 : if (ret)
1096 : : return ret;
1097 : :
1098 : : /* Cfg dwrr */
1099 : 0 : ret = hns3_dcb_dwrr_cfg(hw);
1100 [ # # ]: 0 : if (ret)
1101 : : return ret;
1102 : :
1103 : : /* Cfg schd mode for each level schd */
1104 : 0 : return hns3_dcb_schd_mode_cfg(hw);
1105 : : }
1106 : :
1107 : : static int
1108 : 0 : hns3_pause_param_cfg(struct hns3_hw *hw, const uint8_t *addr,
1109 : : uint8_t pause_trans_gap, uint16_t pause_trans_time)
1110 : : {
1111 : : struct hns3_cfg_pause_param_cmd *pause_param;
1112 : : struct hns3_cmd_desc desc;
1113 : :
1114 : : pause_param = (struct hns3_cfg_pause_param_cmd *)desc.data;
1115 : :
1116 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_MAC_PARA, false);
1117 : :
1118 : : memcpy(pause_param->mac_addr, addr, RTE_ETHER_ADDR_LEN);
1119 : : memcpy(pause_param->mac_addr_extra, addr, RTE_ETHER_ADDR_LEN);
1120 : 0 : pause_param->pause_trans_gap = pause_trans_gap;
1121 : 0 : pause_param->pause_trans_time = rte_cpu_to_le_16(pause_trans_time);
1122 : :
1123 : 0 : return hns3_cmd_send(hw, &desc, 1);
1124 : : }
1125 : :
1126 : : int
1127 : 0 : hns3_pause_addr_cfg(struct hns3_hw *hw, const uint8_t *mac_addr)
1128 : : {
1129 : : struct hns3_cfg_pause_param_cmd *pause_param;
1130 : : struct hns3_cmd_desc desc;
1131 : : uint16_t trans_time;
1132 : : uint8_t trans_gap;
1133 : : int ret;
1134 : :
1135 : : pause_param = (struct hns3_cfg_pause_param_cmd *)desc.data;
1136 : :
1137 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_MAC_PARA, true);
1138 : :
1139 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
1140 [ # # ]: 0 : if (ret)
1141 : : return ret;
1142 : :
1143 : 0 : trans_gap = pause_param->pause_trans_gap;
1144 : 0 : trans_time = rte_le_to_cpu_16(pause_param->pause_trans_time);
1145 : :
1146 : 0 : return hns3_pause_param_cfg(hw, mac_addr, trans_gap, trans_time);
1147 : : }
1148 : :
1149 : : static int
1150 : 0 : hns3_pause_param_setup_hw(struct hns3_hw *hw, uint16_t pause_time)
1151 : : {
1152 : : #define PAUSE_TIME_DIV_BY 2
1153 : : #define PAUSE_TIME_MIN_VALUE 0x4
1154 : :
1155 : : struct hns3_mac *mac = &hw->mac;
1156 : : uint8_t pause_trans_gap;
1157 : :
1158 : : /*
1159 : : * Pause transmit gap must be less than "pause_time / 2", otherwise
1160 : : * the behavior of MAC is undefined.
1161 : : */
1162 [ # # ]: 0 : if (pause_time > PAUSE_TIME_DIV_BY * HNS3_DEFAULT_PAUSE_TRANS_GAP)
1163 : : pause_trans_gap = HNS3_DEFAULT_PAUSE_TRANS_GAP;
1164 [ # # ]: 0 : else if (pause_time >= PAUSE_TIME_MIN_VALUE &&
1165 : : pause_time <= PAUSE_TIME_DIV_BY * HNS3_DEFAULT_PAUSE_TRANS_GAP)
1166 : 0 : pause_trans_gap = pause_time / PAUSE_TIME_DIV_BY - 1;
1167 : : else {
1168 : 0 : hns3_warn(hw, "pause_time(%u) is adjusted to 4", pause_time);
1169 : : pause_time = PAUSE_TIME_MIN_VALUE;
1170 : : pause_trans_gap = pause_time / PAUSE_TIME_DIV_BY - 1;
1171 : : }
1172 : :
1173 : 0 : return hns3_pause_param_cfg(hw, mac->mac_addr,
1174 : : pause_trans_gap, pause_time);
1175 : : }
1176 : :
1177 : : static int
1178 : 0 : hns3_mac_pause_en_cfg(struct hns3_hw *hw, bool tx, bool rx)
1179 : : {
1180 : : struct hns3_cmd_desc desc;
1181 : :
1182 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_MAC_PAUSE_EN, false);
1183 : :
1184 [ # # # # ]: 0 : desc.data[0] = rte_cpu_to_le_32((tx ? HNS3_TX_MAC_PAUSE_EN_MSK : 0) |
1185 : : (rx ? HNS3_RX_MAC_PAUSE_EN_MSK : 0));
1186 : :
1187 : 0 : return hns3_cmd_send(hw, &desc, 1);
1188 : : }
1189 : :
1190 : : static int
1191 : 0 : hns3_pfc_pause_en_cfg(struct hns3_hw *hw, uint8_t pfc_bitmap, bool tx, bool rx)
1192 : : {
1193 : : struct hns3_cmd_desc desc;
1194 : : struct hns3_pfc_en_cmd *pfc = (struct hns3_pfc_en_cmd *)desc.data;
1195 : :
1196 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PFC_PAUSE_EN, false);
1197 : :
1198 [ # # # # ]: 0 : pfc->tx_rx_en_bitmap = (uint8_t)((tx ? HNS3_TX_MAC_PAUSE_EN_MSK : 0) |
1199 : : (rx ? HNS3_RX_MAC_PAUSE_EN_MSK : 0));
1200 : :
1201 : 0 : pfc->pri_en_bitmap = pfc_bitmap;
1202 : :
1203 : 0 : return hns3_cmd_send(hw, &desc, 1);
1204 : : }
1205 : :
1206 : : static int
1207 : 0 : hns3_qs_bp_cfg(struct hns3_hw *hw, uint8_t tc, uint8_t grp_id, uint32_t bit_map)
1208 : : {
1209 : : struct hns3_bp_to_qs_map_cmd *bp_to_qs_map_cmd;
1210 : : struct hns3_cmd_desc desc;
1211 : :
1212 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TM_BP_TO_QSET_MAPPING, false);
1213 : :
1214 : : bp_to_qs_map_cmd = (struct hns3_bp_to_qs_map_cmd *)desc.data;
1215 : :
1216 : 0 : bp_to_qs_map_cmd->tc_id = tc;
1217 : 0 : bp_to_qs_map_cmd->qs_group_id = grp_id;
1218 : 0 : bp_to_qs_map_cmd->qs_bit_map = rte_cpu_to_le_32(bit_map);
1219 : :
1220 : 0 : return hns3_cmd_send(hw, &desc, 1);
1221 : : }
1222 : :
1223 : : static void
1224 : : hns3_get_rx_tx_en_status(struct hns3_hw *hw, bool *tx_en, bool *rx_en)
1225 : : {
1226 [ # # # # ]: 0 : switch (hw->requested_fc_mode) {
1227 : : case HNS3_FC_NONE:
1228 : : *tx_en = false;
1229 : : *rx_en = false;
1230 : : break;
1231 : : case HNS3_FC_RX_PAUSE:
1232 : : *tx_en = false;
1233 : : *rx_en = true;
1234 : : break;
1235 : : case HNS3_FC_TX_PAUSE:
1236 : : *tx_en = true;
1237 : : *rx_en = false;
1238 : : break;
1239 : : case HNS3_FC_FULL:
1240 : : *tx_en = true;
1241 : : *rx_en = true;
1242 : : break;
1243 : : default:
1244 : : *tx_en = false;
1245 : : *rx_en = false;
1246 : : break;
1247 : : }
1248 : : }
1249 : :
1250 : : static int
1251 : 0 : hns3_mac_pause_setup_hw(struct hns3_hw *hw)
1252 : : {
1253 : : bool tx_en, rx_en;
1254 : :
1255 [ # # ]: 0 : if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)
1256 : : hns3_get_rx_tx_en_status(hw, &tx_en, &rx_en);
1257 : : else {
1258 : : tx_en = false;
1259 : : rx_en = false;
1260 : : }
1261 : :
1262 : 0 : return hns3_mac_pause_en_cfg(hw, tx_en, rx_en);
1263 : : }
1264 : :
1265 : : static int
1266 : 0 : hns3_pfc_setup_hw(struct hns3_hw *hw)
1267 : : {
1268 : : bool tx_en, rx_en;
1269 : :
1270 [ # # ]: 0 : if (hw->current_fc_status == HNS3_FC_STATUS_PFC)
1271 : : hns3_get_rx_tx_en_status(hw, &tx_en, &rx_en);
1272 : : else {
1273 : : tx_en = false;
1274 : : rx_en = false;
1275 : : }
1276 : :
1277 : 0 : return hns3_pfc_pause_en_cfg(hw, hw->dcb_info.pfc_en, tx_en, rx_en);
1278 : : }
1279 : :
1280 : : /*
1281 : : * Each Tc has a 1024 queue sets to backpress, it divides to
1282 : : * 32 group, each group contains 32 queue sets, which can be
1283 : : * represented by uint32_t bitmap.
1284 : : */
1285 : : static int
1286 : 0 : hns3_bp_setup_hw(struct hns3_hw *hw, uint8_t tc)
1287 : : {
1288 : : uint32_t qs_bitmap;
1289 : : int ret;
1290 : : int i;
1291 : :
1292 [ # # ]: 0 : for (i = 0; i < HNS3_BP_GRP_NUM; i++) {
1293 : : uint8_t grp, sub_grp;
1294 : : qs_bitmap = 0;
1295 : :
1296 : 0 : grp = hns3_get_field(tc, HNS3_BP_GRP_ID_M, HNS3_BP_GRP_ID_S);
1297 : 0 : sub_grp = hns3_get_field(tc, HNS3_BP_SUB_GRP_ID_M,
1298 : : HNS3_BP_SUB_GRP_ID_S);
1299 [ # # ]: 0 : if (i == grp)
1300 : 0 : qs_bitmap |= (1 << sub_grp);
1301 : :
1302 : 0 : ret = hns3_qs_bp_cfg(hw, tc, i, qs_bitmap);
1303 [ # # ]: 0 : if (ret)
1304 : 0 : return ret;
1305 : : }
1306 : :
1307 : : return 0;
1308 : : }
1309 : :
1310 : : static int
1311 : : hns3_dcb_bp_setup(struct hns3_hw *hw)
1312 : : {
1313 : : int ret, i;
1314 : :
1315 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
1316 : 0 : ret = hns3_bp_setup_hw(hw, i);
1317 [ # # ]: 0 : if (ret)
1318 : : return ret;
1319 : : }
1320 : :
1321 : : return 0;
1322 : : }
1323 : :
1324 : : static int
1325 : 0 : hns3_dcb_pause_setup_hw(struct hns3_hw *hw)
1326 : : {
1327 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1328 : : struct hns3_pf *pf = &hns->pf;
1329 : : int ret;
1330 : :
1331 : 0 : ret = hns3_pause_param_setup_hw(hw, pf->pause_time);
1332 [ # # ]: 0 : if (ret) {
1333 : 0 : hns3_err(hw, "Fail to set pause parameter. ret = %d", ret);
1334 : 0 : return ret;
1335 : : }
1336 : :
1337 : 0 : ret = hns3_mac_pause_setup_hw(hw);
1338 [ # # ]: 0 : if (ret) {
1339 : 0 : hns3_err(hw, "Fail to setup MAC pause. ret = %d", ret);
1340 : 0 : return ret;
1341 : : }
1342 : :
1343 : : /* Only DCB-supported dev supports qset back pressure and pfc cmd */
1344 [ # # ]: 0 : if (!hns3_dev_get_support(hw, DCB))
1345 : : return 0;
1346 : :
1347 : 0 : ret = hns3_pfc_setup_hw(hw);
1348 [ # # ]: 0 : if (ret) {
1349 : 0 : hns3_err(hw, "config pfc failed! ret = %d", ret);
1350 : 0 : return ret;
1351 : : }
1352 : :
1353 : : return hns3_dcb_bp_setup(hw);
1354 : : }
1355 : :
1356 : : static uint8_t
1357 : : hns3_dcb_undrop_tc_map(struct hns3_hw *hw, uint8_t pfc_en)
1358 : : {
1359 : : uint8_t pfc_map = 0;
1360 : : uint8_t *prio_tc;
1361 : : uint8_t i, j;
1362 : :
1363 : 0 : prio_tc = hw->dcb_info.prio_tc;
1364 [ # # # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
1365 [ # # # # ]: 0 : for (j = 0; j < HNS3_MAX_USER_PRIO; j++) {
1366 [ # # # # : 0 : if (prio_tc[j] == i && pfc_en & BIT(j)) {
# # # # ]
1367 : 0 : pfc_map |= BIT(i);
1368 : 0 : break;
1369 : : }
1370 : : }
1371 : : }
1372 : :
1373 : : return pfc_map;
1374 : : }
1375 : :
1376 : : static uint8_t
1377 : : hns3_dcb_parse_num_tc(struct hns3_adapter *hns)
1378 : : {
1379 : : struct rte_eth_dcb_rx_conf *dcb_rx_conf;
1380 : : struct hns3_hw *hw = &hns->hw;
1381 : : uint8_t max_tc_id = 0;
1382 : : int i;
1383 : :
1384 : 0 : dcb_rx_conf = &hw->data->dev_conf.rx_adv_conf.dcb_rx_conf;
1385 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
1386 : 0 : if (dcb_rx_conf->dcb_tc[i] > max_tc_id)
1387 : : max_tc_id = dcb_rx_conf->dcb_tc[i];
1388 : : }
1389 : :
1390 : : /* Number of TC is equal to max_tc_id plus 1. */
1391 : 0 : return max_tc_id + 1;
1392 : : }
1393 : :
1394 : : static int
1395 : 0 : hns3_dcb_info_cfg(struct hns3_adapter *hns)
1396 : : {
1397 : : struct rte_eth_dcb_rx_conf *dcb_rx_conf;
1398 : 0 : struct hns3_hw *hw = &hns->hw;
1399 : : uint8_t tc_bw, bw_rest;
1400 : : uint8_t i, j;
1401 : : int ret;
1402 : :
1403 : 0 : dcb_rx_conf = &hw->data->dev_conf.rx_adv_conf.dcb_rx_conf;
1404 : 0 : hw->dcb_info.local_max_tc = (uint8_t)dcb_rx_conf->nb_tcs;
1405 : 0 : hw->dcb_info.pfc_max = (uint8_t)dcb_rx_conf->nb_tcs;
1406 : :
1407 : : /* Config pg0 */
1408 : 0 : memset(hw->dcb_info.pg_info, 0,
1409 : : sizeof(struct hns3_pg_info) * HNS3_PG_NUM);
1410 : 0 : hw->dcb_info.pg_dwrr[0] = BW_MAX_PERCENT;
1411 : : hw->dcb_info.pg_info[0].pg_id = 0;
1412 : 0 : hw->dcb_info.pg_info[0].pg_sch_mode = HNS3_SCH_MODE_DWRR;
1413 : 0 : hw->dcb_info.pg_info[0].bw_limit = hw->max_tm_rate;
1414 : 0 : hw->dcb_info.pg_info[0].tc_bit_map = hw->dcb_info.hw_tc_map;
1415 : :
1416 : : /* Each tc has same bw for valid tc by default */
1417 : 0 : tc_bw = BW_MAX_PERCENT / hw->dcb_info.num_tc;
1418 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++)
1419 : 0 : hw->dcb_info.pg_info[0].tc_dwrr[i] = tc_bw;
1420 : : /* To ensure the sum of tc_dwrr is equal to 100 */
1421 : 0 : bw_rest = BW_MAX_PERCENT % hw->dcb_info.num_tc;
1422 [ # # ]: 0 : for (j = 0; j < bw_rest; j++)
1423 : 0 : hw->dcb_info.pg_info[0].tc_dwrr[j]++;
1424 [ # # ]: 0 : for (; i < dcb_rx_conf->nb_tcs; i++)
1425 : 0 : hw->dcb_info.pg_info[0].tc_dwrr[i] = 0;
1426 : :
1427 : : /* All tcs map to pg0 */
1428 : 0 : memset(hw->dcb_info.tc_info, 0,
1429 : : sizeof(struct hns3_tc_info) * HNS3_MAX_TC_NUM);
1430 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
1431 : 0 : hw->dcb_info.tc_info[i].tc_id = i;
1432 : 0 : hw->dcb_info.tc_info[i].tc_sch_mode = HNS3_SCH_MODE_DWRR;
1433 : 0 : hw->dcb_info.tc_info[i].pgid = 0;
1434 : 0 : hw->dcb_info.tc_info[i].bw_limit =
1435 : : hw->dcb_info.pg_info[0].bw_limit;
1436 : : }
1437 : :
1438 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
1439 : 0 : hw->dcb_info.prio_tc[i] = dcb_rx_conf->dcb_tc[i];
1440 : :
1441 : 0 : ret = hns3_queue_to_tc_mapping(hw, hw->data->nb_rx_queues,
1442 : 0 : hw->data->nb_tx_queues);
1443 [ # # ]: 0 : if (ret)
1444 : 0 : hns3_err(hw, "update tc queue mapping failed, ret = %d.", ret);
1445 : :
1446 : 0 : return ret;
1447 : : }
1448 : :
1449 : : static int
1450 : 0 : hns3_dcb_info_update(struct hns3_adapter *hns, uint8_t num_tc)
1451 : : {
1452 : : struct hns3_pf *pf = &hns->pf;
1453 : : struct hns3_hw *hw = &hns->hw;
1454 : 0 : uint16_t nb_rx_q = hw->data->nb_rx_queues;
1455 : 0 : uint16_t nb_tx_q = hw->data->nb_tx_queues;
1456 : : uint8_t bit_map = 0;
1457 : : uint8_t i;
1458 : :
1459 [ # # ]: 0 : if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE &&
1460 [ # # ]: 0 : hw->dcb_info.num_pg != 1)
1461 : : return -EINVAL;
1462 : :
1463 [ # # ]: 0 : if (nb_rx_q < num_tc) {
1464 : 0 : hns3_err(hw, "number of Rx queues(%u) is less than tcs(%u).",
1465 : : nb_rx_q, num_tc);
1466 : 0 : return -EINVAL;
1467 : : }
1468 : :
1469 [ # # ]: 0 : if (nb_tx_q < num_tc) {
1470 : 0 : hns3_err(hw, "number of Tx queues(%u) is less than tcs(%u).",
1471 : : nb_tx_q, num_tc);
1472 : 0 : return -EINVAL;
1473 : : }
1474 : :
1475 : : /* Currently not support uncontinuous tc */
1476 : 0 : hw->dcb_info.num_tc = num_tc;
1477 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++)
1478 : 0 : bit_map |= BIT(i);
1479 : :
1480 [ # # ]: 0 : if (!bit_map) {
1481 : : bit_map = 1;
1482 : 0 : hw->dcb_info.num_tc = 1;
1483 : : }
1484 : 0 : hw->dcb_info.hw_tc_map = bit_map;
1485 : :
1486 : 0 : return hns3_dcb_info_cfg(hns);
1487 : : }
1488 : :
1489 : : static int
1490 : 0 : hns3_dcb_hw_configure(struct hns3_adapter *hns)
1491 : : {
1492 : : struct hns3_pf *pf = &hns->pf;
1493 : 0 : struct hns3_hw *hw = &hns->hw;
1494 : 0 : enum hns3_fc_status fc_status = hw->current_fc_status;
1495 : 0 : enum hns3_fc_mode requested_fc_mode = hw->requested_fc_mode;
1496 : 0 : uint8_t hw_pfc_map = hw->dcb_info.hw_pfc_map;
1497 : 0 : uint8_t pfc_en = hw->dcb_info.pfc_en;
1498 : : int ret;
1499 : :
1500 [ # # ]: 0 : if (pf->tx_sch_mode != HNS3_FLAG_TC_BASE_SCH_MODE &&
1501 : : pf->tx_sch_mode != HNS3_FLAG_VNET_BASE_SCH_MODE)
1502 : : return -ENOTSUP;
1503 : :
1504 : 0 : ret = hns3_dcb_schd_setup_hw(hw);
1505 [ # # ]: 0 : if (ret) {
1506 : 0 : hns3_err(hw, "dcb schedule configure failed! ret = %d", ret);
1507 : 0 : return ret;
1508 : : }
1509 : :
1510 [ # # ]: 0 : if (hw->data->dev_conf.dcb_capability_en & RTE_ETH_DCB_PFC_SUPPORT) {
1511 : 0 : hw->dcb_info.pfc_en =
1512 : : RTE_LEN2MASK((uint8_t)HNS3_MAX_USER_PRIO, uint8_t);
1513 : :
1514 : 0 : hw->dcb_info.hw_pfc_map =
1515 : : hns3_dcb_undrop_tc_map(hw, hw->dcb_info.pfc_en);
1516 : :
1517 : 0 : hw->current_fc_status = HNS3_FC_STATUS_PFC;
1518 : 0 : hw->requested_fc_mode = HNS3_FC_FULL;
1519 : : } else {
1520 : 0 : hw->current_fc_status = HNS3_FC_STATUS_NONE;
1521 : 0 : hw->requested_fc_mode = HNS3_FC_NONE;
1522 : 0 : hw->dcb_info.pfc_en = 0;
1523 : 0 : hw->dcb_info.hw_pfc_map = 0;
1524 : : }
1525 : :
1526 : 0 : ret = hns3_buffer_alloc(hw);
1527 [ # # ]: 0 : if (ret)
1528 : 0 : goto cfg_fail;
1529 : :
1530 : 0 : ret = hns3_dcb_pause_setup_hw(hw);
1531 [ # # ]: 0 : if (ret) {
1532 : 0 : hns3_err(hw, "setup pfc failed! ret = %d", ret);
1533 : 0 : goto cfg_fail;
1534 : : }
1535 : :
1536 : : return 0;
1537 : :
1538 : 0 : cfg_fail:
1539 : 0 : hw->requested_fc_mode = requested_fc_mode;
1540 : 0 : hw->current_fc_status = fc_status;
1541 : 0 : hw->dcb_info.pfc_en = pfc_en;
1542 : 0 : hw->dcb_info.hw_pfc_map = hw_pfc_map;
1543 : :
1544 : 0 : return ret;
1545 : : }
1546 : :
1547 : : /*
1548 : : * hns3_dcb_configure - setup dcb related config
1549 : : * @hns: pointer to hns3 adapter
1550 : : * Returns 0 on success, negative value on failure.
1551 : : */
1552 : : int
1553 : 0 : hns3_dcb_configure(struct hns3_adapter *hns)
1554 : : {
1555 : : struct hns3_hw *hw = &hns->hw;
1556 : : uint8_t num_tc;
1557 : : int ret;
1558 : :
1559 : : num_tc = hns3_dcb_parse_num_tc(hns);
1560 : 0 : ret = hns3_dcb_info_update(hns, num_tc);
1561 [ # # ]: 0 : if (ret) {
1562 : 0 : hns3_err(hw, "dcb info update failed: %d", ret);
1563 : 0 : return ret;
1564 : : }
1565 : :
1566 : 0 : ret = hns3_dcb_hw_configure(hns);
1567 [ # # ]: 0 : if (ret) {
1568 : 0 : hns3_err(hw, "dcb sw configure failed: %d", ret);
1569 : 0 : return ret;
1570 : : }
1571 : :
1572 : : return 0;
1573 : : }
1574 : :
1575 : : int
1576 : 0 : hns3_dcb_init_hw(struct hns3_hw *hw)
1577 : : {
1578 : : int ret;
1579 : :
1580 : 0 : ret = hns3_dcb_schd_setup_hw(hw);
1581 [ # # ]: 0 : if (ret) {
1582 : 0 : hns3_err(hw, "dcb schedule setup failed: %d", ret);
1583 : 0 : return ret;
1584 : : }
1585 : :
1586 : 0 : ret = hns3_dcb_pause_setup_hw(hw);
1587 [ # # ]: 0 : if (ret)
1588 : 0 : hns3_err(hw, "PAUSE setup failed: %d", ret);
1589 : :
1590 : : return ret;
1591 : : }
1592 : :
1593 : : int
1594 : 0 : hns3_dcb_init(struct hns3_hw *hw)
1595 : : {
1596 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1597 : : struct hns3_pf *pf = &hns->pf;
1598 : : uint16_t default_tqp_num;
1599 : : int ret;
1600 : :
1601 : 0 : PMD_INIT_FUNC_TRACE();
1602 : :
1603 : : /*
1604 : : * According to the 'adapter_state' identifier, the following branch
1605 : : * is only executed to initialize default configurations of dcb during
1606 : : * the initializing driver process. Due to driver saving dcb-related
1607 : : * information before reset triggered, the reinit dev stage of the
1608 : : * reset process can not access to the branch, or those information
1609 : : * will be changed.
1610 : : */
1611 [ # # ]: 0 : if (hw->adapter_state == HNS3_NIC_UNINITIALIZED) {
1612 : 0 : hw->requested_fc_mode = HNS3_FC_NONE;
1613 : 0 : pf->pause_time = HNS3_DEFAULT_PAUSE_TRANS_TIME;
1614 : 0 : hw->current_fc_status = HNS3_FC_STATUS_NONE;
1615 : :
1616 : 0 : ret = hns3_dcb_info_init(hw);
1617 [ # # ]: 0 : if (ret) {
1618 : 0 : hns3_err(hw, "dcb info init failed, ret = %d.", ret);
1619 : 0 : return ret;
1620 : : }
1621 : :
1622 : : /*
1623 : : * The number of queues configured by default cannot exceed
1624 : : * the maximum number of queues for a single TC.
1625 : : */
1626 : 0 : default_tqp_num = RTE_MIN(hw->rss_size_max,
1627 : : hw->tqps_num / hw->dcb_info.num_tc);
1628 : 0 : ret = hns3_queue_to_tc_mapping(hw, default_tqp_num, default_tqp_num);
1629 [ # # ]: 0 : if (ret) {
1630 : 0 : hns3_err(hw,
1631 : : "update tc queue mapping failed, ret = %d.",
1632 : : ret);
1633 : 0 : return ret;
1634 : : }
1635 : : }
1636 : :
1637 : : /*
1638 : : * DCB hardware will be configured by following the function during
1639 : : * the initializing driver process and the reset process. However,
1640 : : * driver will restore directly configurations of dcb hardware based
1641 : : * on dcb-related information soft maintained when driver
1642 : : * initialization has finished and reset is coming.
1643 : : */
1644 : 0 : ret = hns3_dcb_init_hw(hw);
1645 [ # # ]: 0 : if (ret) {
1646 : 0 : hns3_err(hw, "dcb init hardware failed, ret = %d.", ret);
1647 : 0 : return ret;
1648 : : }
1649 : :
1650 : : return 0;
1651 : : }
1652 : :
1653 : : int
1654 : 0 : hns3_update_queue_map_configure(struct hns3_adapter *hns)
1655 : : {
1656 : 0 : struct hns3_hw *hw = &hns->hw;
1657 : 0 : enum rte_eth_rx_mq_mode mq_mode = hw->data->dev_conf.rxmode.mq_mode;
1658 : 0 : uint16_t nb_rx_q = hw->data->nb_rx_queues;
1659 : 0 : uint16_t nb_tx_q = hw->data->nb_tx_queues;
1660 : : int ret;
1661 : :
1662 [ # # ]: 0 : if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG)
1663 : : return 0;
1664 : :
1665 : 0 : ret = hns3_queue_to_tc_mapping(hw, nb_rx_q, nb_tx_q);
1666 [ # # ]: 0 : if (ret) {
1667 : 0 : hns3_err(hw, "failed to update tc queue mapping, ret = %d.",
1668 : : ret);
1669 : 0 : return ret;
1670 : : }
1671 : 0 : ret = hns3_q_to_qs_map(hw);
1672 [ # # ]: 0 : if (ret)
1673 : 0 : hns3_err(hw, "failed to map nq to qs, ret = %d.", ret);
1674 : :
1675 : : return ret;
1676 : : }
1677 : :
1678 : : static void
1679 : 0 : hns3_get_fc_mode(struct hns3_hw *hw, enum rte_eth_fc_mode mode)
1680 : : {
1681 [ # # # # : 0 : switch (mode) {
# ]
1682 : 0 : case RTE_ETH_FC_NONE:
1683 : 0 : hw->requested_fc_mode = HNS3_FC_NONE;
1684 : 0 : break;
1685 : 0 : case RTE_ETH_FC_RX_PAUSE:
1686 : 0 : hw->requested_fc_mode = HNS3_FC_RX_PAUSE;
1687 : 0 : break;
1688 : 0 : case RTE_ETH_FC_TX_PAUSE:
1689 : 0 : hw->requested_fc_mode = HNS3_FC_TX_PAUSE;
1690 : 0 : break;
1691 : 0 : case RTE_ETH_FC_FULL:
1692 : 0 : hw->requested_fc_mode = HNS3_FC_FULL;
1693 : 0 : break;
1694 : 0 : default:
1695 : 0 : hw->requested_fc_mode = HNS3_FC_NONE;
1696 : 0 : hns3_warn(hw, "fc_mode(%u) exceeds member scope and is "
1697 : : "configured to RTE_ETH_FC_NONE", mode);
1698 : 0 : break;
1699 : : }
1700 : 0 : }
1701 : :
1702 : : /*
1703 : : * hns3_dcb_pfc_enable - Enable priority flow control
1704 : : * @dev: pointer to ethernet device
1705 : : *
1706 : : * Configures the pfc settings for one priority.
1707 : : */
1708 : : int
1709 : 0 : hns3_dcb_pfc_enable(struct rte_eth_dev *dev, struct rte_eth_pfc_conf *pfc_conf)
1710 : : {
1711 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1712 : : struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1713 : 0 : enum hns3_fc_status fc_status = hw->current_fc_status;
1714 : 0 : enum hns3_fc_mode old_fc_mode = hw->requested_fc_mode;
1715 : 0 : uint8_t hw_pfc_map = hw->dcb_info.hw_pfc_map;
1716 : 0 : uint8_t pfc_en = hw->dcb_info.pfc_en;
1717 : 0 : uint8_t priority = pfc_conf->priority;
1718 : 0 : uint16_t pause_time = pf->pause_time;
1719 : : int ret;
1720 : :
1721 : 0 : hw->dcb_info.pfc_en |= BIT(priority);
1722 : 0 : hw->dcb_info.hw_pfc_map =
1723 : : hns3_dcb_undrop_tc_map(hw, hw->dcb_info.pfc_en);
1724 : 0 : ret = hns3_buffer_alloc(hw);
1725 [ # # ]: 0 : if (ret) {
1726 : 0 : hns3_err(hw, "update packet buffer failed, ret = %d", ret);
1727 : 0 : goto buffer_alloc_fail;
1728 : : }
1729 : :
1730 : 0 : pf->pause_time = pfc_conf->fc.pause_time;
1731 : 0 : hns3_get_fc_mode(hw, pfc_conf->fc.mode);
1732 [ # # ]: 0 : if (hw->requested_fc_mode == HNS3_FC_NONE)
1733 : 0 : hw->current_fc_status = HNS3_FC_STATUS_NONE;
1734 : : else
1735 : 0 : hw->current_fc_status = HNS3_FC_STATUS_PFC;
1736 : :
1737 : : /*
1738 : : * The flow control mode of all UPs will be changed based on
1739 : : * requested_fc_mode coming from user.
1740 : : */
1741 : 0 : ret = hns3_dcb_pause_setup_hw(hw);
1742 [ # # ]: 0 : if (ret) {
1743 : 0 : hns3_err(hw, "enable pfc failed! ret = %d", ret);
1744 : 0 : goto pfc_setup_fail;
1745 : : }
1746 : :
1747 : : return 0;
1748 : :
1749 : : pfc_setup_fail:
1750 : 0 : hw->requested_fc_mode = old_fc_mode;
1751 : 0 : hw->current_fc_status = fc_status;
1752 : 0 : pf->pause_time = pause_time;
1753 : 0 : buffer_alloc_fail:
1754 : 0 : hw->dcb_info.pfc_en = pfc_en;
1755 : 0 : hw->dcb_info.hw_pfc_map = hw_pfc_map;
1756 : :
1757 : 0 : return ret;
1758 : : }
1759 : :
1760 : : /*
1761 : : * hns3_fc_enable - Enable MAC pause
1762 : : * @dev: pointer to ethernet device
1763 : : *
1764 : : * Configures the MAC pause settings.
1765 : : */
1766 : : int
1767 : 0 : hns3_fc_enable(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1768 : : {
1769 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1770 : : struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1771 : 0 : enum hns3_fc_mode old_fc_mode = hw->requested_fc_mode;
1772 : 0 : enum hns3_fc_status fc_status = hw->current_fc_status;
1773 : 0 : uint16_t pause_time = pf->pause_time;
1774 : : int ret;
1775 : :
1776 : 0 : pf->pause_time = fc_conf->pause_time;
1777 : 0 : hns3_get_fc_mode(hw, fc_conf->mode);
1778 : :
1779 : : /*
1780 : : * In fact, current_fc_status is HNS3_FC_STATUS_NONE when mode
1781 : : * of flow control is configured to be HNS3_FC_NONE.
1782 : : */
1783 [ # # ]: 0 : if (hw->requested_fc_mode == HNS3_FC_NONE)
1784 : 0 : hw->current_fc_status = HNS3_FC_STATUS_NONE;
1785 : : else
1786 : 0 : hw->current_fc_status = HNS3_FC_STATUS_MAC_PAUSE;
1787 : :
1788 : 0 : ret = hns3_dcb_pause_setup_hw(hw);
1789 [ # # ]: 0 : if (ret) {
1790 : 0 : hns3_err(hw, "enable MAC Pause failed! ret = %d", ret);
1791 : 0 : goto setup_fc_fail;
1792 : : }
1793 : :
1794 : : return 0;
1795 : :
1796 : : setup_fc_fail:
1797 : 0 : hw->requested_fc_mode = old_fc_mode;
1798 : 0 : hw->current_fc_status = fc_status;
1799 : 0 : pf->pause_time = pause_time;
1800 : :
1801 : 0 : return ret;
1802 : : }
1803 : :
1804 : : int
1805 : 0 : hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
1806 : : {
1807 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1808 : 0 : enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
1809 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1810 : : int i;
1811 : :
1812 [ # # # # ]: 0 : if (hns->is_vf && !hns3_dev_get_support(hw, VF_MULTI_TCS))
1813 : : return -ENOTSUP;
1814 : :
1815 : 0 : rte_spinlock_lock(&hw->lock);
1816 [ # # ]: 0 : if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG)
1817 : 0 : dcb_info->nb_tcs = hw->dcb_info.local_max_tc;
1818 : : else
1819 : 0 : dcb_info->nb_tcs = 1;
1820 : :
1821 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
1822 : 0 : dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
1823 [ # # ]: 0 : for (i = 0; i < dcb_info->nb_tcs; i++)
1824 : 0 : dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
1825 : :
1826 [ # # ]: 0 : for (i = 0; i < hw->dcb_info.num_tc; i++) {
1827 : 0 : dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i;
1828 : 0 : dcb_info->tc_queue.tc_txq[0][i].base =
1829 : 0 : hw->tc_queue[i].tqp_offset;
1830 : 0 : dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size;
1831 : 0 : dcb_info->tc_queue.tc_txq[0][i].nb_queue =
1832 : 0 : hw->tc_queue[i].tqp_count;
1833 : : }
1834 : : rte_spinlock_unlock(&hw->lock);
1835 : :
1836 : 0 : return 0;
1837 : : }
1838 : :
1839 : : int
1840 : 0 : hns3_check_dev_mq_mode(struct rte_eth_dev *dev)
1841 : : {
1842 : 0 : enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
1843 : 0 : enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
1844 : 0 : struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1845 : : struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1846 : : struct rte_eth_dcb_rx_conf *dcb_rx_conf;
1847 : : struct rte_eth_dcb_tx_conf *dcb_tx_conf;
1848 : : uint8_t num_tc;
1849 : : int max_tc = 0;
1850 : : int i;
1851 : :
1852 [ # # ]: 0 : if (((uint32_t)rx_mq_mode & RTE_ETH_MQ_RX_VMDQ_FLAG) ||
1853 [ # # ]: 0 : (tx_mq_mode == RTE_ETH_MQ_TX_VMDQ_DCB ||
1854 : : tx_mq_mode == RTE_ETH_MQ_TX_VMDQ_ONLY)) {
1855 : 0 : hns3_err(hw, "VMDQ is not supported, rx_mq_mode = %d, tx_mq_mode = %d.",
1856 : : rx_mq_mode, tx_mq_mode);
1857 : 0 : return -EOPNOTSUPP;
1858 : : }
1859 : :
1860 : : dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
1861 : : dcb_tx_conf = &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf;
1862 [ # # ]: 0 : if ((uint32_t)rx_mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) {
1863 [ # # ]: 0 : if (dcb_rx_conf->nb_tcs > hw->dcb_info.tc_max) {
1864 : 0 : hns3_err(hw, "nb_tcs(%u) > max_tc(%u) driver supported.",
1865 : : dcb_rx_conf->nb_tcs, hw->dcb_info.tc_max);
1866 : 0 : return -EINVAL;
1867 : : }
1868 : :
1869 : : /*
1870 : : * The PF driver supports only four or eight TCs. But the
1871 : : * number of TCs supported by the VF driver is flexible,
1872 : : * therefore, only the number of TCs in the PF is verified.
1873 : : */
1874 [ # # # # ]: 0 : if (!hns->is_vf && !(dcb_rx_conf->nb_tcs == HNS3_4_TCS ||
1875 : : dcb_rx_conf->nb_tcs == HNS3_8_TCS)) {
1876 : 0 : hns3_err(hw, "on RTE_ETH_MQ_RX_DCB_RSS mode, "
1877 : : "nb_tcs(%d) != %d or %d in rx direction.",
1878 : : dcb_rx_conf->nb_tcs, HNS3_4_TCS, HNS3_8_TCS);
1879 : 0 : return -EINVAL;
1880 : : }
1881 : :
1882 [ # # ]: 0 : if (dcb_rx_conf->nb_tcs != dcb_tx_conf->nb_tcs) {
1883 : 0 : hns3_err(hw, "num_tcs(%d) of tx is not equal to rx(%d)",
1884 : : dcb_tx_conf->nb_tcs, dcb_rx_conf->nb_tcs);
1885 : 0 : return -EINVAL;
1886 : : }
1887 : :
1888 [ # # ]: 0 : for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
1889 [ # # ]: 0 : if (dcb_rx_conf->dcb_tc[i] != dcb_tx_conf->dcb_tc[i]) {
1890 : 0 : hns3_err(hw, "dcb_tc[%d] = %u in rx direction, "
1891 : : "is not equal to one in tx direction.",
1892 : : i, dcb_rx_conf->dcb_tc[i]);
1893 : 0 : return -EINVAL;
1894 : : }
1895 : 0 : if (dcb_rx_conf->dcb_tc[i] > max_tc)
1896 : : max_tc = dcb_rx_conf->dcb_tc[i];
1897 : : }
1898 : :
1899 : 0 : num_tc = max_tc + 1;
1900 [ # # ]: 0 : if (num_tc > dcb_rx_conf->nb_tcs) {
1901 : 0 : hns3_err(hw, "max num_tc(%u) mapped > nb_tcs(%u)",
1902 : : num_tc, dcb_rx_conf->nb_tcs);
1903 : 0 : return -EINVAL;
1904 : : }
1905 : : }
1906 : :
1907 : : return 0;
1908 : : }
|