• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3 
4 #include <linux/etherdevice.h>
5 
6 #include "hclge_cmd.h"
7 #include "hclge_main.h"
8 #include "hclge_tm.h"
9 
10 enum hclge_shaper_level {
11 	HCLGE_SHAPER_LVL_PRI	= 0,
12 	HCLGE_SHAPER_LVL_PG	= 1,
13 	HCLGE_SHAPER_LVL_PORT	= 2,
14 	HCLGE_SHAPER_LVL_QSET	= 3,
15 	HCLGE_SHAPER_LVL_CNT	= 4,
16 	HCLGE_SHAPER_LVL_VF	= 0,
17 	HCLGE_SHAPER_LVL_PF	= 1,
18 };
19 
20 #define HCLGE_TM_PFC_PKT_GET_CMD_NUM	3
21 #define HCLGE_TM_PFC_NUM_GET_PER_CMD	3
22 
23 #define HCLGE_SHAPER_BS_U_DEF	5
24 #define HCLGE_SHAPER_BS_S_DEF	20
25 
26 /* hclge_shaper_para_calc: calculate ir parameter for the shaper
27  * @ir: Rate to be config, its unit is Mbps
28  * @shaper_level: the shaper level. eg: port, pg, priority, queueset
29  * @ir_para: parameters of IR shaper
30  * @max_tm_rate: max tm rate is available to config
31  *
32  * the formula:
33  *
34  *		IR_b * (2 ^ IR_u) * 8
35  * IR(Mbps) = -------------------------  *  CLOCK(1000Mbps)
36  *		Tick * (2 ^ IR_s)
37  *
38  * @return: 0: calculate sucessful, negative: fail
39  */
hclge_shaper_para_calc(u32 ir,u8 shaper_level,struct hclge_shaper_ir_para * ir_para,u32 max_tm_rate)40 static int hclge_shaper_para_calc(u32 ir, u8 shaper_level,
41 				  struct hclge_shaper_ir_para *ir_para,
42 				  u32 max_tm_rate)
43 {
44 #define DIVISOR_CLK		(1000 * 8)
45 #define DIVISOR_IR_B_126	(126 * DIVISOR_CLK)
46 
47 	static const u16 tick_array[HCLGE_SHAPER_LVL_CNT] = {
48 		6 * 256,        /* Prioriy level */
49 		6 * 32,         /* Prioriy group level */
50 		6 * 8,          /* Port level */
51 		6 * 256         /* Qset level */
52 	};
53 	u8 ir_u_calc = 0;
54 	u8 ir_s_calc = 0;
55 	u32 ir_calc;
56 	u32 tick;
57 
58 	/* Calc tick */
59 	if (shaper_level >= HCLGE_SHAPER_LVL_CNT ||
60 	    ir > max_tm_rate)
61 		return -EINVAL;
62 
63 	tick = tick_array[shaper_level];
64 
65 	/**
66 	 * Calc the speed if ir_b = 126, ir_u = 0 and ir_s = 0
67 	 * the formula is changed to:
68 	 *		126 * 1 * 8
69 	 * ir_calc = ---------------- * 1000
70 	 *		tick * 1
71 	 */
72 	ir_calc = (DIVISOR_IR_B_126 + (tick >> 1) - 1) / tick;
73 
74 	if (ir_calc == ir) {
75 		ir_para->ir_b = 126;
76 		ir_para->ir_u = 0;
77 		ir_para->ir_s = 0;
78 
79 		return 0;
80 	} else if (ir_calc > ir) {
81 		/* Increasing the denominator to select ir_s value */
82 		while (ir_calc >= ir && ir) {
83 			ir_s_calc++;
84 			ir_calc = DIVISOR_IR_B_126 / (tick * (1 << ir_s_calc));
85 		}
86 
87 		ir_para->ir_b = (ir * tick * (1 << ir_s_calc) +
88 				(DIVISOR_CLK >> 1)) / DIVISOR_CLK;
89 	} else {
90 		/* Increasing the numerator to select ir_u value */
91 		u32 numerator;
92 
93 		while (ir_calc < ir) {
94 			ir_u_calc++;
95 			numerator = DIVISOR_IR_B_126 * (1 << ir_u_calc);
96 			ir_calc = (numerator + (tick >> 1)) / tick;
97 		}
98 
99 		if (ir_calc == ir) {
100 			ir_para->ir_b = 126;
101 		} else {
102 			u32 denominator = DIVISOR_CLK * (1 << --ir_u_calc);
103 			ir_para->ir_b = (ir * tick + (denominator >> 1)) /
104 					denominator;
105 		}
106 	}
107 
108 	ir_para->ir_u = ir_u_calc;
109 	ir_para->ir_s = ir_s_calc;
110 
111 	return 0;
112 }
113 
hclge_pfc_stats_get(struct hclge_dev * hdev,enum hclge_opcode_type opcode,u64 * stats)114 static int hclge_pfc_stats_get(struct hclge_dev *hdev,
115 			       enum hclge_opcode_type opcode, u64 *stats)
116 {
117 	struct hclge_desc desc[HCLGE_TM_PFC_PKT_GET_CMD_NUM];
118 	int ret, i, j;
119 
120 	if (!(opcode == HCLGE_OPC_QUERY_PFC_RX_PKT_CNT ||
121 	      opcode == HCLGE_OPC_QUERY_PFC_TX_PKT_CNT))
122 		return -EINVAL;
123 
124 	for (i = 0; i < HCLGE_TM_PFC_PKT_GET_CMD_NUM - 1; i++) {
125 		hclge_cmd_setup_basic_desc(&desc[i], opcode, true);
126 		desc[i].flag |= cpu_to_le16(HCLGE_CMD_FLAG_NEXT);
127 	}
128 
129 	hclge_cmd_setup_basic_desc(&desc[i], opcode, true);
130 
131 	ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_TM_PFC_PKT_GET_CMD_NUM);
132 	if (ret)
133 		return ret;
134 
135 	for (i = 0; i < HCLGE_TM_PFC_PKT_GET_CMD_NUM; i++) {
136 		struct hclge_pfc_stats_cmd *pfc_stats =
137 				(struct hclge_pfc_stats_cmd *)desc[i].data;
138 
139 		for (j = 0; j < HCLGE_TM_PFC_NUM_GET_PER_CMD; j++) {
140 			u32 index = i * HCLGE_TM_PFC_PKT_GET_CMD_NUM + j;
141 
142 			if (index < HCLGE_MAX_TC_NUM)
143 				stats[index] =
144 					le64_to_cpu(pfc_stats->pkt_num[j]);
145 		}
146 	}
147 	return 0;
148 }
149 
hclge_pfc_rx_stats_get(struct hclge_dev * hdev,u64 * stats)150 int hclge_pfc_rx_stats_get(struct hclge_dev *hdev, u64 *stats)
151 {
152 	return hclge_pfc_stats_get(hdev, HCLGE_OPC_QUERY_PFC_RX_PKT_CNT, stats);
153 }
154 
hclge_pfc_tx_stats_get(struct hclge_dev * hdev,u64 * stats)155 int hclge_pfc_tx_stats_get(struct hclge_dev *hdev, u64 *stats)
156 {
157 	return hclge_pfc_stats_get(hdev, HCLGE_OPC_QUERY_PFC_TX_PKT_CNT, stats);
158 }
159 
hclge_mac_pause_en_cfg(struct hclge_dev * hdev,bool tx,bool rx)160 int hclge_mac_pause_en_cfg(struct hclge_dev *hdev, bool tx, bool rx)
161 {
162 	struct hclge_desc desc;
163 
164 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_MAC_PAUSE_EN, false);
165 
166 	desc.data[0] = cpu_to_le32((tx ? HCLGE_TX_MAC_PAUSE_EN_MSK : 0) |
167 		(rx ? HCLGE_RX_MAC_PAUSE_EN_MSK : 0));
168 
169 	return hclge_cmd_send(&hdev->hw, &desc, 1);
170 }
171 
hclge_pfc_pause_en_cfg(struct hclge_dev * hdev,u8 tx_rx_bitmap,u8 pfc_bitmap)172 static int hclge_pfc_pause_en_cfg(struct hclge_dev *hdev, u8 tx_rx_bitmap,
173 				  u8 pfc_bitmap)
174 {
175 	struct hclge_desc desc;
176 	struct hclge_pfc_en_cmd *pfc = (struct hclge_pfc_en_cmd *)desc.data;
177 
178 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_PFC_PAUSE_EN, false);
179 
180 	pfc->tx_rx_en_bitmap = tx_rx_bitmap;
181 	pfc->pri_en_bitmap = pfc_bitmap;
182 
183 	return hclge_cmd_send(&hdev->hw, &desc, 1);
184 }
185 
hclge_pause_param_cfg(struct hclge_dev * hdev,const u8 * addr,u8 pause_trans_gap,u16 pause_trans_time)186 static int hclge_pause_param_cfg(struct hclge_dev *hdev, const u8 *addr,
187 				 u8 pause_trans_gap, u16 pause_trans_time)
188 {
189 	struct hclge_cfg_pause_param_cmd *pause_param;
190 	struct hclge_desc desc;
191 
192 	pause_param = (struct hclge_cfg_pause_param_cmd *)desc.data;
193 
194 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_MAC_PARA, false);
195 
196 	ether_addr_copy(pause_param->mac_addr, addr);
197 	ether_addr_copy(pause_param->mac_addr_extra, addr);
198 	pause_param->pause_trans_gap = pause_trans_gap;
199 	pause_param->pause_trans_time = cpu_to_le16(pause_trans_time);
200 
201 	return hclge_cmd_send(&hdev->hw, &desc, 1);
202 }
203 
hclge_pause_addr_cfg(struct hclge_dev * hdev,const u8 * mac_addr)204 int hclge_pause_addr_cfg(struct hclge_dev *hdev, const u8 *mac_addr)
205 {
206 	struct hclge_cfg_pause_param_cmd *pause_param;
207 	struct hclge_desc desc;
208 	u16 trans_time;
209 	u8 trans_gap;
210 	int ret;
211 
212 	pause_param = (struct hclge_cfg_pause_param_cmd *)desc.data;
213 
214 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_MAC_PARA, true);
215 
216 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
217 	if (ret)
218 		return ret;
219 
220 	trans_gap = pause_param->pause_trans_gap;
221 	trans_time = le16_to_cpu(pause_param->pause_trans_time);
222 
223 	return hclge_pause_param_cfg(hdev, mac_addr, trans_gap, trans_time);
224 }
225 
hclge_fill_pri_array(struct hclge_dev * hdev,u8 * pri,u8 pri_id)226 static int hclge_fill_pri_array(struct hclge_dev *hdev, u8 *pri, u8 pri_id)
227 {
228 	u8 tc;
229 
230 	tc = hdev->tm_info.prio_tc[pri_id];
231 
232 	if (tc >= hdev->tm_info.num_tc)
233 		return -EINVAL;
234 
235 	/**
236 	 * the register for priority has four bytes, the first bytes includes
237 	 *  priority0 and priority1, the higher 4bit stands for priority1
238 	 *  while the lower 4bit stands for priority0, as below:
239 	 * first byte:	| pri_1 | pri_0 |
240 	 * second byte:	| pri_3 | pri_2 |
241 	 * third byte:	| pri_5 | pri_4 |
242 	 * fourth byte:	| pri_7 | pri_6 |
243 	 */
244 	pri[pri_id >> 1] |= tc << ((pri_id & 1) * 4);
245 
246 	return 0;
247 }
248 
hclge_up_to_tc_map(struct hclge_dev * hdev)249 static int hclge_up_to_tc_map(struct hclge_dev *hdev)
250 {
251 	struct hclge_desc desc;
252 	u8 *pri = (u8 *)desc.data;
253 	u8 pri_id;
254 	int ret;
255 
256 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_PRI_TO_TC_MAPPING, false);
257 
258 	for (pri_id = 0; pri_id < HNAE3_MAX_USER_PRIO; pri_id++) {
259 		ret = hclge_fill_pri_array(hdev, pri, pri_id);
260 		if (ret)
261 			return ret;
262 	}
263 
264 	return hclge_cmd_send(&hdev->hw, &desc, 1);
265 }
266 
hclge_tm_pg_to_pri_map_cfg(struct hclge_dev * hdev,u8 pg_id,u8 pri_bit_map)267 static int hclge_tm_pg_to_pri_map_cfg(struct hclge_dev *hdev,
268 				      u8 pg_id, u8 pri_bit_map)
269 {
270 	struct hclge_pg_to_pri_link_cmd *map;
271 	struct hclge_desc desc;
272 
273 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_PG_TO_PRI_LINK, false);
274 
275 	map = (struct hclge_pg_to_pri_link_cmd *)desc.data;
276 
277 	map->pg_id = pg_id;
278 	map->pri_bit_map = pri_bit_map;
279 
280 	return hclge_cmd_send(&hdev->hw, &desc, 1);
281 }
282 
hclge_tm_qs_to_pri_map_cfg(struct hclge_dev * hdev,u16 qs_id,u8 pri)283 static int hclge_tm_qs_to_pri_map_cfg(struct hclge_dev *hdev,
284 				      u16 qs_id, u8 pri)
285 {
286 	struct hclge_qs_to_pri_link_cmd *map;
287 	struct hclge_desc desc;
288 
289 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_QS_TO_PRI_LINK, false);
290 
291 	map = (struct hclge_qs_to_pri_link_cmd *)desc.data;
292 
293 	map->qs_id = cpu_to_le16(qs_id);
294 	map->priority = pri;
295 	map->link_vld = HCLGE_TM_QS_PRI_LINK_VLD_MSK;
296 
297 	return hclge_cmd_send(&hdev->hw, &desc, 1);
298 }
299 
hclge_tm_q_to_qs_map_cfg(struct hclge_dev * hdev,u16 q_id,u16 qs_id)300 static int hclge_tm_q_to_qs_map_cfg(struct hclge_dev *hdev,
301 				    u16 q_id, u16 qs_id)
302 {
303 	struct hclge_nq_to_qs_link_cmd *map;
304 	struct hclge_desc desc;
305 
306 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_NQ_TO_QS_LINK, false);
307 
308 	map = (struct hclge_nq_to_qs_link_cmd *)desc.data;
309 
310 	map->nq_id = cpu_to_le16(q_id);
311 	map->qset_id = cpu_to_le16(qs_id | HCLGE_TM_Q_QS_LINK_VLD_MSK);
312 
313 	return hclge_cmd_send(&hdev->hw, &desc, 1);
314 }
315 
hclge_tm_pg_weight_cfg(struct hclge_dev * hdev,u8 pg_id,u8 dwrr)316 static int hclge_tm_pg_weight_cfg(struct hclge_dev *hdev, u8 pg_id,
317 				  u8 dwrr)
318 {
319 	struct hclge_pg_weight_cmd *weight;
320 	struct hclge_desc desc;
321 
322 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_PG_WEIGHT, false);
323 
324 	weight = (struct hclge_pg_weight_cmd *)desc.data;
325 
326 	weight->pg_id = pg_id;
327 	weight->dwrr = dwrr;
328 
329 	return hclge_cmd_send(&hdev->hw, &desc, 1);
330 }
331 
hclge_tm_pri_weight_cfg(struct hclge_dev * hdev,u8 pri_id,u8 dwrr)332 static int hclge_tm_pri_weight_cfg(struct hclge_dev *hdev, u8 pri_id,
333 				   u8 dwrr)
334 {
335 	struct hclge_priority_weight_cmd *weight;
336 	struct hclge_desc desc;
337 
338 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_PRI_WEIGHT, false);
339 
340 	weight = (struct hclge_priority_weight_cmd *)desc.data;
341 
342 	weight->pri_id = pri_id;
343 	weight->dwrr = dwrr;
344 
345 	return hclge_cmd_send(&hdev->hw, &desc, 1);
346 }
347 
hclge_tm_qs_weight_cfg(struct hclge_dev * hdev,u16 qs_id,u8 dwrr)348 static int hclge_tm_qs_weight_cfg(struct hclge_dev *hdev, u16 qs_id,
349 				  u8 dwrr)
350 {
351 	struct hclge_qs_weight_cmd *weight;
352 	struct hclge_desc desc;
353 
354 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_QS_WEIGHT, false);
355 
356 	weight = (struct hclge_qs_weight_cmd *)desc.data;
357 
358 	weight->qs_id = cpu_to_le16(qs_id);
359 	weight->dwrr = dwrr;
360 
361 	return hclge_cmd_send(&hdev->hw, &desc, 1);
362 }
363 
hclge_tm_get_shapping_para(u8 ir_b,u8 ir_u,u8 ir_s,u8 bs_b,u8 bs_s)364 static u32 hclge_tm_get_shapping_para(u8 ir_b, u8 ir_u, u8 ir_s,
365 				      u8 bs_b, u8 bs_s)
366 {
367 	u32 shapping_para = 0;
368 
369 	hclge_tm_set_field(shapping_para, IR_B, ir_b);
370 	hclge_tm_set_field(shapping_para, IR_U, ir_u);
371 	hclge_tm_set_field(shapping_para, IR_S, ir_s);
372 	hclge_tm_set_field(shapping_para, BS_B, bs_b);
373 	hclge_tm_set_field(shapping_para, BS_S, bs_s);
374 
375 	return shapping_para;
376 }
377 
hclge_tm_pg_shapping_cfg(struct hclge_dev * hdev,enum hclge_shap_bucket bucket,u8 pg_id,u32 shapping_para)378 static int hclge_tm_pg_shapping_cfg(struct hclge_dev *hdev,
379 				    enum hclge_shap_bucket bucket, u8 pg_id,
380 				    u32 shapping_para)
381 {
382 	struct hclge_pg_shapping_cmd *shap_cfg_cmd;
383 	enum hclge_opcode_type opcode;
384 	struct hclge_desc desc;
385 
386 	opcode = bucket ? HCLGE_OPC_TM_PG_P_SHAPPING :
387 		 HCLGE_OPC_TM_PG_C_SHAPPING;
388 	hclge_cmd_setup_basic_desc(&desc, opcode, false);
389 
390 	shap_cfg_cmd = (struct hclge_pg_shapping_cmd *)desc.data;
391 
392 	shap_cfg_cmd->pg_id = pg_id;
393 
394 	shap_cfg_cmd->pg_shapping_para = cpu_to_le32(shapping_para);
395 
396 	return hclge_cmd_send(&hdev->hw, &desc, 1);
397 }
398 
hclge_tm_port_shaper_cfg(struct hclge_dev * hdev)399 static int hclge_tm_port_shaper_cfg(struct hclge_dev *hdev)
400 {
401 	struct hclge_port_shapping_cmd *shap_cfg_cmd;
402 	struct hclge_shaper_ir_para ir_para;
403 	struct hclge_desc desc;
404 	u32 shapping_para;
405 	int ret;
406 
407 	ret = hclge_shaper_para_calc(hdev->hw.mac.speed, HCLGE_SHAPER_LVL_PORT,
408 				     &ir_para,
409 				     hdev->ae_dev->dev_specs.max_tm_rate);
410 	if (ret)
411 		return ret;
412 
413 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_PORT_SHAPPING, false);
414 	shap_cfg_cmd = (struct hclge_port_shapping_cmd *)desc.data;
415 
416 	shapping_para = hclge_tm_get_shapping_para(ir_para.ir_b, ir_para.ir_u,
417 						   ir_para.ir_s,
418 						   HCLGE_SHAPER_BS_U_DEF,
419 						   HCLGE_SHAPER_BS_S_DEF);
420 
421 	shap_cfg_cmd->port_shapping_para = cpu_to_le32(shapping_para);
422 
423 	return hclge_cmd_send(&hdev->hw, &desc, 1);
424 }
425 
hclge_tm_pri_shapping_cfg(struct hclge_dev * hdev,enum hclge_shap_bucket bucket,u8 pri_id,u32 shapping_para)426 static int hclge_tm_pri_shapping_cfg(struct hclge_dev *hdev,
427 				     enum hclge_shap_bucket bucket, u8 pri_id,
428 				     u32 shapping_para)
429 {
430 	struct hclge_pri_shapping_cmd *shap_cfg_cmd;
431 	enum hclge_opcode_type opcode;
432 	struct hclge_desc desc;
433 
434 	opcode = bucket ? HCLGE_OPC_TM_PRI_P_SHAPPING :
435 		 HCLGE_OPC_TM_PRI_C_SHAPPING;
436 
437 	hclge_cmd_setup_basic_desc(&desc, opcode, false);
438 
439 	shap_cfg_cmd = (struct hclge_pri_shapping_cmd *)desc.data;
440 
441 	shap_cfg_cmd->pri_id = pri_id;
442 
443 	shap_cfg_cmd->pri_shapping_para = cpu_to_le32(shapping_para);
444 
445 	return hclge_cmd_send(&hdev->hw, &desc, 1);
446 }
447 
hclge_tm_pg_schd_mode_cfg(struct hclge_dev * hdev,u8 pg_id)448 static int hclge_tm_pg_schd_mode_cfg(struct hclge_dev *hdev, u8 pg_id)
449 {
450 	struct hclge_desc desc;
451 
452 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_PG_SCH_MODE_CFG, false);
453 
454 	if (hdev->tm_info.pg_info[pg_id].pg_sch_mode == HCLGE_SCH_MODE_DWRR)
455 		desc.data[1] = cpu_to_le32(HCLGE_TM_TX_SCHD_DWRR_MSK);
456 	else
457 		desc.data[1] = 0;
458 
459 	desc.data[0] = cpu_to_le32(pg_id);
460 
461 	return hclge_cmd_send(&hdev->hw, &desc, 1);
462 }
463 
hclge_tm_pri_schd_mode_cfg(struct hclge_dev * hdev,u8 pri_id)464 static int hclge_tm_pri_schd_mode_cfg(struct hclge_dev *hdev, u8 pri_id)
465 {
466 	struct hclge_desc desc;
467 
468 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_PRI_SCH_MODE_CFG, false);
469 
470 	if (hdev->tm_info.tc_info[pri_id].tc_sch_mode == HCLGE_SCH_MODE_DWRR)
471 		desc.data[1] = cpu_to_le32(HCLGE_TM_TX_SCHD_DWRR_MSK);
472 	else
473 		desc.data[1] = 0;
474 
475 	desc.data[0] = cpu_to_le32(pri_id);
476 
477 	return hclge_cmd_send(&hdev->hw, &desc, 1);
478 }
479 
hclge_tm_qs_schd_mode_cfg(struct hclge_dev * hdev,u16 qs_id,u8 mode)480 static int hclge_tm_qs_schd_mode_cfg(struct hclge_dev *hdev, u16 qs_id, u8 mode)
481 {
482 	struct hclge_desc desc;
483 
484 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_QS_SCH_MODE_CFG, false);
485 
486 	if (mode == HCLGE_SCH_MODE_DWRR)
487 		desc.data[1] = cpu_to_le32(HCLGE_TM_TX_SCHD_DWRR_MSK);
488 	else
489 		desc.data[1] = 0;
490 
491 	desc.data[0] = cpu_to_le32(qs_id);
492 
493 	return hclge_cmd_send(&hdev->hw, &desc, 1);
494 }
495 
hclge_tm_qs_bp_cfg(struct hclge_dev * hdev,u8 tc,u8 grp_id,u32 bit_map)496 static int hclge_tm_qs_bp_cfg(struct hclge_dev *hdev, u8 tc, u8 grp_id,
497 			      u32 bit_map)
498 {
499 	struct hclge_bp_to_qs_map_cmd *bp_to_qs_map_cmd;
500 	struct hclge_desc desc;
501 
502 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TM_BP_TO_QSET_MAPPING,
503 				   false);
504 
505 	bp_to_qs_map_cmd = (struct hclge_bp_to_qs_map_cmd *)desc.data;
506 
507 	bp_to_qs_map_cmd->tc_id = tc;
508 	bp_to_qs_map_cmd->qs_group_id = grp_id;
509 	bp_to_qs_map_cmd->qs_bit_map = cpu_to_le32(bit_map);
510 
511 	return hclge_cmd_send(&hdev->hw, &desc, 1);
512 }
513 
hclge_tm_qs_shaper_cfg(struct hclge_vport * vport,int max_tx_rate)514 int hclge_tm_qs_shaper_cfg(struct hclge_vport *vport, int max_tx_rate)
515 {
516 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
517 	struct hclge_qs_shapping_cmd *shap_cfg_cmd;
518 	struct hclge_shaper_ir_para ir_para;
519 	struct hclge_dev *hdev = vport->back;
520 	struct hclge_desc desc;
521 	u32 shaper_para;
522 	int ret, i;
523 
524 	if (!max_tx_rate)
525 		max_tx_rate = hdev->ae_dev->dev_specs.max_tm_rate;
526 
527 	ret = hclge_shaper_para_calc(max_tx_rate, HCLGE_SHAPER_LVL_QSET,
528 				     &ir_para,
529 				     hdev->ae_dev->dev_specs.max_tm_rate);
530 	if (ret)
531 		return ret;
532 
533 	shaper_para = hclge_tm_get_shapping_para(ir_para.ir_b, ir_para.ir_u,
534 						 ir_para.ir_s,
535 						 HCLGE_SHAPER_BS_U_DEF,
536 						 HCLGE_SHAPER_BS_S_DEF);
537 
538 	for (i = 0; i < kinfo->num_tc; i++) {
539 		hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QCN_SHAPPING_CFG,
540 					   false);
541 
542 		shap_cfg_cmd = (struct hclge_qs_shapping_cmd *)desc.data;
543 		shap_cfg_cmd->qs_id = cpu_to_le16(vport->qs_offset + i);
544 		shap_cfg_cmd->qs_shapping_para = cpu_to_le32(shaper_para);
545 
546 		ret = hclge_cmd_send(&hdev->hw, &desc, 1);
547 		if (ret) {
548 			dev_err(&hdev->pdev->dev,
549 				"vf%u, qs%u failed to set tx_rate:%d, ret=%d\n",
550 				vport->vport_id, shap_cfg_cmd->qs_id,
551 				max_tx_rate, ret);
552 			return ret;
553 		}
554 	}
555 
556 	return 0;
557 }
558 
hclge_tm_vport_tc_info_update(struct hclge_vport * vport)559 static void hclge_tm_vport_tc_info_update(struct hclge_vport *vport)
560 {
561 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
562 	struct hclge_dev *hdev = vport->back;
563 	u16 max_rss_size;
564 	u8 i;
565 
566 	/* TC configuration is shared by PF/VF in one port, only allow
567 	 * one tc for VF for simplicity. VF's vport_id is non zero.
568 	 */
569 	kinfo->num_tc = vport->vport_id ? 1 :
570 			min_t(u16, vport->alloc_tqps, hdev->tm_info.num_tc);
571 	vport->qs_offset = (vport->vport_id ? HNAE3_MAX_TC : 0) +
572 				(vport->vport_id ? (vport->vport_id - 1) : 0);
573 
574 	max_rss_size = min_t(u16, hdev->rss_size_max,
575 			     vport->alloc_tqps / kinfo->num_tc);
576 
577 	/* Set to user value, no larger than max_rss_size. */
578 	if (kinfo->req_rss_size != kinfo->rss_size && kinfo->req_rss_size &&
579 	    kinfo->req_rss_size <= max_rss_size) {
580 		dev_info(&hdev->pdev->dev, "rss changes from %u to %u\n",
581 			 kinfo->rss_size, kinfo->req_rss_size);
582 		kinfo->rss_size = kinfo->req_rss_size;
583 	} else if (kinfo->rss_size > max_rss_size ||
584 		   (!kinfo->req_rss_size && kinfo->rss_size < max_rss_size)) {
585 		/* if user not set rss, the rss_size should compare with the
586 		 * valid msi numbers to ensure one to one map between tqp and
587 		 * irq as default.
588 		 */
589 		if (!kinfo->req_rss_size)
590 			max_rss_size = min_t(u16, max_rss_size,
591 					     (hdev->num_nic_msi - 1) /
592 					     kinfo->num_tc);
593 
594 		/* Set to the maximum specification value (max_rss_size). */
595 		kinfo->rss_size = max_rss_size;
596 	}
597 
598 	kinfo->num_tqps = kinfo->num_tc * kinfo->rss_size;
599 	vport->dwrr = 100;  /* 100 percent as init */
600 	vport->alloc_rss_size = kinfo->rss_size;
601 	vport->bw_limit = hdev->tm_info.pg_info[0].bw_limit;
602 
603 	for (i = 0; i < HNAE3_MAX_TC; i++) {
604 		if (hdev->hw_tc_map & BIT(i) && i < kinfo->num_tc) {
605 			kinfo->tc_info[i].enable = true;
606 			kinfo->tc_info[i].tqp_offset = i * kinfo->rss_size;
607 			kinfo->tc_info[i].tqp_count = kinfo->rss_size;
608 			kinfo->tc_info[i].tc = i;
609 		} else {
610 			/* Set to default queue if TC is disable */
611 			kinfo->tc_info[i].enable = false;
612 			kinfo->tc_info[i].tqp_offset = 0;
613 			kinfo->tc_info[i].tqp_count = 1;
614 			kinfo->tc_info[i].tc = 0;
615 		}
616 	}
617 
618 	memcpy(kinfo->prio_tc, hdev->tm_info.prio_tc,
619 	       sizeof_field(struct hnae3_knic_private_info, prio_tc));
620 }
621 
hclge_tm_vport_info_update(struct hclge_dev * hdev)622 static void hclge_tm_vport_info_update(struct hclge_dev *hdev)
623 {
624 	struct hclge_vport *vport = hdev->vport;
625 	u32 i;
626 
627 	for (i = 0; i < hdev->num_alloc_vport; i++) {
628 		hclge_tm_vport_tc_info_update(vport);
629 
630 		vport++;
631 	}
632 }
633 
hclge_tm_tc_info_init(struct hclge_dev * hdev)634 static void hclge_tm_tc_info_init(struct hclge_dev *hdev)
635 {
636 	u8 i;
637 
638 	for (i = 0; i < hdev->tm_info.num_tc; i++) {
639 		hdev->tm_info.tc_info[i].tc_id = i;
640 		hdev->tm_info.tc_info[i].tc_sch_mode = HCLGE_SCH_MODE_DWRR;
641 		hdev->tm_info.tc_info[i].pgid = 0;
642 		hdev->tm_info.tc_info[i].bw_limit =
643 			hdev->tm_info.pg_info[0].bw_limit;
644 	}
645 
646 	for (i = 0; i < HNAE3_MAX_USER_PRIO; i++)
647 		hdev->tm_info.prio_tc[i] =
648 			(i >= hdev->tm_info.num_tc) ? 0 : i;
649 }
650 
hclge_tm_pg_info_init(struct hclge_dev * hdev)651 static void hclge_tm_pg_info_init(struct hclge_dev *hdev)
652 {
653 #define BW_PERCENT	100
654 
655 	u8 i;
656 
657 	for (i = 0; i < hdev->tm_info.num_pg; i++) {
658 		int k;
659 
660 		hdev->tm_info.pg_dwrr[i] = i ? 0 : BW_PERCENT;
661 
662 		hdev->tm_info.pg_info[i].pg_id = i;
663 		hdev->tm_info.pg_info[i].pg_sch_mode = HCLGE_SCH_MODE_DWRR;
664 
665 		hdev->tm_info.pg_info[i].bw_limit =
666 					hdev->ae_dev->dev_specs.max_tm_rate;
667 
668 		if (i != 0)
669 			continue;
670 
671 		hdev->tm_info.pg_info[i].tc_bit_map = hdev->hw_tc_map;
672 		for (k = 0; k < hdev->tm_info.num_tc; k++)
673 			hdev->tm_info.pg_info[i].tc_dwrr[k] = BW_PERCENT;
674 		for (; k < HNAE3_MAX_TC; k++)
675 			hdev->tm_info.pg_info[i].tc_dwrr[k] = 0;
676 	}
677 }
678 
hclge_update_fc_mode_by_dcb_flag(struct hclge_dev * hdev)679 static void hclge_update_fc_mode_by_dcb_flag(struct hclge_dev *hdev)
680 {
681 	if (hdev->tm_info.num_tc == 1 && !hdev->tm_info.pfc_en) {
682 		if (hdev->fc_mode_last_time == HCLGE_FC_PFC)
683 			dev_warn(&hdev->pdev->dev,
684 				 "Only 1 tc used, but last mode is FC_PFC\n");
685 
686 		hdev->tm_info.fc_mode = hdev->fc_mode_last_time;
687 	} else if (hdev->tm_info.fc_mode != HCLGE_FC_PFC) {
688 		/* fc_mode_last_time record the last fc_mode when
689 		 * DCB is enabled, so that fc_mode can be set to
690 		 * the correct value when DCB is disabled.
691 		 */
692 		hdev->fc_mode_last_time = hdev->tm_info.fc_mode;
693 		hdev->tm_info.fc_mode = HCLGE_FC_PFC;
694 	}
695 }
696 
hclge_update_fc_mode(struct hclge_dev * hdev)697 static void hclge_update_fc_mode(struct hclge_dev *hdev)
698 {
699 	if (!hdev->tm_info.pfc_en) {
700 		hdev->tm_info.fc_mode = hdev->fc_mode_last_time;
701 		return;
702 	}
703 
704 	if (hdev->tm_info.fc_mode != HCLGE_FC_PFC) {
705 		hdev->fc_mode_last_time = hdev->tm_info.fc_mode;
706 		hdev->tm_info.fc_mode = HCLGE_FC_PFC;
707 	}
708 }
709 
hclge_tm_pfc_info_update(struct hclge_dev * hdev)710 void hclge_tm_pfc_info_update(struct hclge_dev *hdev)
711 {
712 	if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
713 		hclge_update_fc_mode(hdev);
714 	else
715 		hclge_update_fc_mode_by_dcb_flag(hdev);
716 }
717 
hclge_tm_schd_info_init(struct hclge_dev * hdev)718 static void hclge_tm_schd_info_init(struct hclge_dev *hdev)
719 {
720 	hclge_tm_pg_info_init(hdev);
721 
722 	hclge_tm_tc_info_init(hdev);
723 
724 	hclge_tm_vport_info_update(hdev);
725 
726 	hclge_tm_pfc_info_update(hdev);
727 }
728 
hclge_tm_pg_to_pri_map(struct hclge_dev * hdev)729 static int hclge_tm_pg_to_pri_map(struct hclge_dev *hdev)
730 {
731 	int ret;
732 	u32 i;
733 
734 	if (hdev->tx_sch_mode != HCLGE_FLAG_TC_BASE_SCH_MODE)
735 		return 0;
736 
737 	for (i = 0; i < hdev->tm_info.num_pg; i++) {
738 		/* Cfg mapping */
739 		ret = hclge_tm_pg_to_pri_map_cfg(
740 			hdev, i, hdev->tm_info.pg_info[i].tc_bit_map);
741 		if (ret)
742 			return ret;
743 	}
744 
745 	return 0;
746 }
747 
hclge_tm_pg_shaper_cfg(struct hclge_dev * hdev)748 static int hclge_tm_pg_shaper_cfg(struct hclge_dev *hdev)
749 {
750 	u32 max_tm_rate = hdev->ae_dev->dev_specs.max_tm_rate;
751 	struct hclge_shaper_ir_para ir_para;
752 	u32 shaper_para;
753 	int ret;
754 	u32 i;
755 
756 	/* Cfg pg schd */
757 	if (hdev->tx_sch_mode != HCLGE_FLAG_TC_BASE_SCH_MODE)
758 		return 0;
759 
760 	/* Pg to pri */
761 	for (i = 0; i < hdev->tm_info.num_pg; i++) {
762 		/* Calc shaper para */
763 		ret = hclge_shaper_para_calc(hdev->tm_info.pg_info[i].bw_limit,
764 					     HCLGE_SHAPER_LVL_PG,
765 					     &ir_para, max_tm_rate);
766 		if (ret)
767 			return ret;
768 
769 		shaper_para = hclge_tm_get_shapping_para(0, 0, 0,
770 							 HCLGE_SHAPER_BS_U_DEF,
771 							 HCLGE_SHAPER_BS_S_DEF);
772 		ret = hclge_tm_pg_shapping_cfg(hdev,
773 					       HCLGE_TM_SHAP_C_BUCKET, i,
774 					       shaper_para);
775 		if (ret)
776 			return ret;
777 
778 		shaper_para = hclge_tm_get_shapping_para(ir_para.ir_b,
779 							 ir_para.ir_u,
780 							 ir_para.ir_s,
781 							 HCLGE_SHAPER_BS_U_DEF,
782 							 HCLGE_SHAPER_BS_S_DEF);
783 		ret = hclge_tm_pg_shapping_cfg(hdev,
784 					       HCLGE_TM_SHAP_P_BUCKET, i,
785 					       shaper_para);
786 		if (ret)
787 			return ret;
788 	}
789 
790 	return 0;
791 }
792 
hclge_tm_pg_dwrr_cfg(struct hclge_dev * hdev)793 static int hclge_tm_pg_dwrr_cfg(struct hclge_dev *hdev)
794 {
795 	int ret;
796 	u32 i;
797 
798 	/* cfg pg schd */
799 	if (hdev->tx_sch_mode != HCLGE_FLAG_TC_BASE_SCH_MODE)
800 		return 0;
801 
802 	/* pg to prio */
803 	for (i = 0; i < hdev->tm_info.num_pg; i++) {
804 		/* Cfg dwrr */
805 		ret = hclge_tm_pg_weight_cfg(hdev, i, hdev->tm_info.pg_dwrr[i]);
806 		if (ret)
807 			return ret;
808 	}
809 
810 	return 0;
811 }
812 
hclge_vport_q_to_qs_map(struct hclge_dev * hdev,struct hclge_vport * vport)813 static int hclge_vport_q_to_qs_map(struct hclge_dev *hdev,
814 				   struct hclge_vport *vport)
815 {
816 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
817 	struct hnae3_queue **tqp = kinfo->tqp;
818 	struct hnae3_tc_info *v_tc_info;
819 	u32 i, j;
820 	int ret;
821 
822 	for (i = 0; i < kinfo->num_tc; i++) {
823 		v_tc_info = &kinfo->tc_info[i];
824 		for (j = 0; j < v_tc_info->tqp_count; j++) {
825 			struct hnae3_queue *q = tqp[v_tc_info->tqp_offset + j];
826 
827 			ret = hclge_tm_q_to_qs_map_cfg(hdev,
828 						       hclge_get_queue_id(q),
829 						       vport->qs_offset + i);
830 			if (ret)
831 				return ret;
832 		}
833 	}
834 
835 	return 0;
836 }
837 
hclge_tm_pri_q_qs_cfg(struct hclge_dev * hdev)838 static int hclge_tm_pri_q_qs_cfg(struct hclge_dev *hdev)
839 {
840 	struct hclge_vport *vport = hdev->vport;
841 	int ret;
842 	u32 i, k;
843 
844 	if (hdev->tx_sch_mode == HCLGE_FLAG_TC_BASE_SCH_MODE) {
845 		/* Cfg qs -> pri mapping, one by one mapping */
846 		for (k = 0; k < hdev->num_alloc_vport; k++) {
847 			struct hnae3_knic_private_info *kinfo =
848 				&vport[k].nic.kinfo;
849 
850 			for (i = 0; i < kinfo->num_tc; i++) {
851 				ret = hclge_tm_qs_to_pri_map_cfg(
852 					hdev, vport[k].qs_offset + i, i);
853 				if (ret)
854 					return ret;
855 			}
856 		}
857 	} else if (hdev->tx_sch_mode == HCLGE_FLAG_VNET_BASE_SCH_MODE) {
858 		/* Cfg qs -> pri mapping,  qs = tc, pri = vf, 8 qs -> 1 pri */
859 		for (k = 0; k < hdev->num_alloc_vport; k++)
860 			for (i = 0; i < HNAE3_MAX_TC; i++) {
861 				ret = hclge_tm_qs_to_pri_map_cfg(
862 					hdev, vport[k].qs_offset + i, k);
863 				if (ret)
864 					return ret;
865 			}
866 	} else {
867 		return -EINVAL;
868 	}
869 
870 	/* Cfg q -> qs mapping */
871 	for (i = 0; i < hdev->num_alloc_vport; i++) {
872 		ret = hclge_vport_q_to_qs_map(hdev, vport);
873 		if (ret)
874 			return ret;
875 
876 		vport++;
877 	}
878 
879 	return 0;
880 }
881 
hclge_tm_pri_tc_base_shaper_cfg(struct hclge_dev * hdev)882 static int hclge_tm_pri_tc_base_shaper_cfg(struct hclge_dev *hdev)
883 {
884 	u32 max_tm_rate = hdev->ae_dev->dev_specs.max_tm_rate;
885 	struct hclge_shaper_ir_para ir_para;
886 	u32 shaper_para;
887 	int ret;
888 	u32 i;
889 
890 	for (i = 0; i < hdev->tm_info.num_tc; i++) {
891 		ret = hclge_shaper_para_calc(hdev->tm_info.tc_info[i].bw_limit,
892 					     HCLGE_SHAPER_LVL_PRI,
893 					     &ir_para, max_tm_rate);
894 		if (ret)
895 			return ret;
896 
897 		shaper_para = hclge_tm_get_shapping_para(0, 0, 0,
898 							 HCLGE_SHAPER_BS_U_DEF,
899 							 HCLGE_SHAPER_BS_S_DEF);
900 		ret = hclge_tm_pri_shapping_cfg(hdev, HCLGE_TM_SHAP_C_BUCKET, i,
901 						shaper_para);
902 		if (ret)
903 			return ret;
904 
905 		shaper_para = hclge_tm_get_shapping_para(ir_para.ir_b,
906 							 ir_para.ir_u,
907 							 ir_para.ir_s,
908 							 HCLGE_SHAPER_BS_U_DEF,
909 							 HCLGE_SHAPER_BS_S_DEF);
910 		ret = hclge_tm_pri_shapping_cfg(hdev, HCLGE_TM_SHAP_P_BUCKET, i,
911 						shaper_para);
912 		if (ret)
913 			return ret;
914 	}
915 
916 	return 0;
917 }
918 
hclge_tm_pri_vnet_base_shaper_pri_cfg(struct hclge_vport * vport)919 static int hclge_tm_pri_vnet_base_shaper_pri_cfg(struct hclge_vport *vport)
920 {
921 	struct hclge_dev *hdev = vport->back;
922 	struct hclge_shaper_ir_para ir_para;
923 	u32 shaper_para;
924 	int ret;
925 
926 	ret = hclge_shaper_para_calc(vport->bw_limit, HCLGE_SHAPER_LVL_VF,
927 				     &ir_para,
928 				     hdev->ae_dev->dev_specs.max_tm_rate);
929 	if (ret)
930 		return ret;
931 
932 	shaper_para = hclge_tm_get_shapping_para(0, 0, 0,
933 						 HCLGE_SHAPER_BS_U_DEF,
934 						 HCLGE_SHAPER_BS_S_DEF);
935 	ret = hclge_tm_pri_shapping_cfg(hdev, HCLGE_TM_SHAP_C_BUCKET,
936 					vport->vport_id, shaper_para);
937 	if (ret)
938 		return ret;
939 
940 	shaper_para = hclge_tm_get_shapping_para(ir_para.ir_b, ir_para.ir_u,
941 						 ir_para.ir_s,
942 						 HCLGE_SHAPER_BS_U_DEF,
943 						 HCLGE_SHAPER_BS_S_DEF);
944 	ret = hclge_tm_pri_shapping_cfg(hdev, HCLGE_TM_SHAP_P_BUCKET,
945 					vport->vport_id, shaper_para);
946 	if (ret)
947 		return ret;
948 
949 	return 0;
950 }
951 
hclge_tm_pri_vnet_base_shaper_qs_cfg(struct hclge_vport * vport)952 static int hclge_tm_pri_vnet_base_shaper_qs_cfg(struct hclge_vport *vport)
953 {
954 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
955 	struct hclge_dev *hdev = vport->back;
956 	u32 max_tm_rate = hdev->ae_dev->dev_specs.max_tm_rate;
957 	struct hclge_shaper_ir_para ir_para;
958 	u32 i;
959 	int ret;
960 
961 	for (i = 0; i < kinfo->num_tc; i++) {
962 		ret = hclge_shaper_para_calc(hdev->tm_info.tc_info[i].bw_limit,
963 					     HCLGE_SHAPER_LVL_QSET,
964 					     &ir_para, max_tm_rate);
965 		if (ret)
966 			return ret;
967 	}
968 
969 	return 0;
970 }
971 
hclge_tm_pri_vnet_base_shaper_cfg(struct hclge_dev * hdev)972 static int hclge_tm_pri_vnet_base_shaper_cfg(struct hclge_dev *hdev)
973 {
974 	struct hclge_vport *vport = hdev->vport;
975 	int ret;
976 	u32 i;
977 
978 	/* Need config vport shaper */
979 	for (i = 0; i < hdev->num_alloc_vport; i++) {
980 		ret = hclge_tm_pri_vnet_base_shaper_pri_cfg(vport);
981 		if (ret)
982 			return ret;
983 
984 		ret = hclge_tm_pri_vnet_base_shaper_qs_cfg(vport);
985 		if (ret)
986 			return ret;
987 
988 		vport++;
989 	}
990 
991 	return 0;
992 }
993 
hclge_tm_pri_shaper_cfg(struct hclge_dev * hdev)994 static int hclge_tm_pri_shaper_cfg(struct hclge_dev *hdev)
995 {
996 	int ret;
997 
998 	if (hdev->tx_sch_mode == HCLGE_FLAG_TC_BASE_SCH_MODE) {
999 		ret = hclge_tm_pri_tc_base_shaper_cfg(hdev);
1000 		if (ret)
1001 			return ret;
1002 	} else {
1003 		ret = hclge_tm_pri_vnet_base_shaper_cfg(hdev);
1004 		if (ret)
1005 			return ret;
1006 	}
1007 
1008 	return 0;
1009 }
1010 
hclge_tm_pri_tc_base_dwrr_cfg(struct hclge_dev * hdev)1011 static int hclge_tm_pri_tc_base_dwrr_cfg(struct hclge_dev *hdev)
1012 {
1013 	struct hclge_vport *vport = hdev->vport;
1014 	struct hclge_pg_info *pg_info;
1015 	u8 dwrr;
1016 	int ret;
1017 	u32 i, k;
1018 
1019 	for (i = 0; i < hdev->tm_info.num_tc; i++) {
1020 		pg_info =
1021 			&hdev->tm_info.pg_info[hdev->tm_info.tc_info[i].pgid];
1022 		dwrr = pg_info->tc_dwrr[i];
1023 
1024 		ret = hclge_tm_pri_weight_cfg(hdev, i, dwrr);
1025 		if (ret)
1026 			return ret;
1027 
1028 		for (k = 0; k < hdev->num_alloc_vport; k++) {
1029 			ret = hclge_tm_qs_weight_cfg(
1030 				hdev, vport[k].qs_offset + i,
1031 				vport[k].dwrr);
1032 			if (ret)
1033 				return ret;
1034 		}
1035 	}
1036 
1037 	return 0;
1038 }
1039 
hclge_tm_ets_tc_dwrr_cfg(struct hclge_dev * hdev)1040 static int hclge_tm_ets_tc_dwrr_cfg(struct hclge_dev *hdev)
1041 {
1042 #define DEFAULT_TC_OFFSET	14
1043 
1044 	struct hclge_ets_tc_weight_cmd *ets_weight;
1045 	struct hclge_desc desc;
1046 	unsigned int i;
1047 
1048 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_ETS_TC_WEIGHT, false);
1049 	ets_weight = (struct hclge_ets_tc_weight_cmd *)desc.data;
1050 
1051 	for (i = 0; i < HNAE3_MAX_TC; i++) {
1052 		struct hclge_pg_info *pg_info;
1053 
1054 		pg_info = &hdev->tm_info.pg_info[hdev->tm_info.tc_info[i].pgid];
1055 		ets_weight->tc_weight[i] = pg_info->tc_dwrr[i];
1056 	}
1057 
1058 	ets_weight->weight_offset = DEFAULT_TC_OFFSET;
1059 
1060 	return hclge_cmd_send(&hdev->hw, &desc, 1);
1061 }
1062 
hclge_tm_pri_vnet_base_dwrr_pri_cfg(struct hclge_vport * vport)1063 static int hclge_tm_pri_vnet_base_dwrr_pri_cfg(struct hclge_vport *vport)
1064 {
1065 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
1066 	struct hclge_dev *hdev = vport->back;
1067 	int ret;
1068 	u8 i;
1069 
1070 	/* Vf dwrr */
1071 	ret = hclge_tm_pri_weight_cfg(hdev, vport->vport_id, vport->dwrr);
1072 	if (ret)
1073 		return ret;
1074 
1075 	/* Qset dwrr */
1076 	for (i = 0; i < kinfo->num_tc; i++) {
1077 		ret = hclge_tm_qs_weight_cfg(
1078 			hdev, vport->qs_offset + i,
1079 			hdev->tm_info.pg_info[0].tc_dwrr[i]);
1080 		if (ret)
1081 			return ret;
1082 	}
1083 
1084 	return 0;
1085 }
1086 
hclge_tm_pri_vnet_base_dwrr_cfg(struct hclge_dev * hdev)1087 static int hclge_tm_pri_vnet_base_dwrr_cfg(struct hclge_dev *hdev)
1088 {
1089 	struct hclge_vport *vport = hdev->vport;
1090 	int ret;
1091 	u32 i;
1092 
1093 	for (i = 0; i < hdev->num_alloc_vport; i++) {
1094 		ret = hclge_tm_pri_vnet_base_dwrr_pri_cfg(vport);
1095 		if (ret)
1096 			return ret;
1097 
1098 		vport++;
1099 	}
1100 
1101 	return 0;
1102 }
1103 
hclge_tm_pri_dwrr_cfg(struct hclge_dev * hdev)1104 static int hclge_tm_pri_dwrr_cfg(struct hclge_dev *hdev)
1105 {
1106 	int ret;
1107 
1108 	if (hdev->tx_sch_mode == HCLGE_FLAG_TC_BASE_SCH_MODE) {
1109 		ret = hclge_tm_pri_tc_base_dwrr_cfg(hdev);
1110 		if (ret)
1111 			return ret;
1112 
1113 		if (!hnae3_dev_dcb_supported(hdev))
1114 			return 0;
1115 
1116 		ret = hclge_tm_ets_tc_dwrr_cfg(hdev);
1117 		if (ret == -EOPNOTSUPP) {
1118 			dev_warn(&hdev->pdev->dev,
1119 				 "fw %08x does't support ets tc weight cmd\n",
1120 				 hdev->fw_version);
1121 			ret = 0;
1122 		}
1123 
1124 		return ret;
1125 	} else {
1126 		ret = hclge_tm_pri_vnet_base_dwrr_cfg(hdev);
1127 		if (ret)
1128 			return ret;
1129 	}
1130 
1131 	return 0;
1132 }
1133 
hclge_tm_map_cfg(struct hclge_dev * hdev)1134 static int hclge_tm_map_cfg(struct hclge_dev *hdev)
1135 {
1136 	int ret;
1137 
1138 	ret = hclge_up_to_tc_map(hdev);
1139 	if (ret)
1140 		return ret;
1141 
1142 	ret = hclge_tm_pg_to_pri_map(hdev);
1143 	if (ret)
1144 		return ret;
1145 
1146 	return hclge_tm_pri_q_qs_cfg(hdev);
1147 }
1148 
hclge_tm_shaper_cfg(struct hclge_dev * hdev)1149 static int hclge_tm_shaper_cfg(struct hclge_dev *hdev)
1150 {
1151 	int ret;
1152 
1153 	ret = hclge_tm_port_shaper_cfg(hdev);
1154 	if (ret)
1155 		return ret;
1156 
1157 	ret = hclge_tm_pg_shaper_cfg(hdev);
1158 	if (ret)
1159 		return ret;
1160 
1161 	return hclge_tm_pri_shaper_cfg(hdev);
1162 }
1163 
hclge_tm_dwrr_cfg(struct hclge_dev * hdev)1164 int hclge_tm_dwrr_cfg(struct hclge_dev *hdev)
1165 {
1166 	int ret;
1167 
1168 	ret = hclge_tm_pg_dwrr_cfg(hdev);
1169 	if (ret)
1170 		return ret;
1171 
1172 	return hclge_tm_pri_dwrr_cfg(hdev);
1173 }
1174 
hclge_tm_lvl2_schd_mode_cfg(struct hclge_dev * hdev)1175 static int hclge_tm_lvl2_schd_mode_cfg(struct hclge_dev *hdev)
1176 {
1177 	int ret;
1178 	u8 i;
1179 
1180 	/* Only being config on TC-Based scheduler mode */
1181 	if (hdev->tx_sch_mode == HCLGE_FLAG_VNET_BASE_SCH_MODE)
1182 		return 0;
1183 
1184 	for (i = 0; i < hdev->tm_info.num_pg; i++) {
1185 		ret = hclge_tm_pg_schd_mode_cfg(hdev, i);
1186 		if (ret)
1187 			return ret;
1188 	}
1189 
1190 	return 0;
1191 }
1192 
hclge_tm_schd_mode_vnet_base_cfg(struct hclge_vport * vport)1193 static int hclge_tm_schd_mode_vnet_base_cfg(struct hclge_vport *vport)
1194 {
1195 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
1196 	struct hclge_dev *hdev = vport->back;
1197 	int ret;
1198 	u8 i;
1199 
1200 	if (vport->vport_id >= HNAE3_MAX_TC)
1201 		return -EINVAL;
1202 
1203 	ret = hclge_tm_pri_schd_mode_cfg(hdev, vport->vport_id);
1204 	if (ret)
1205 		return ret;
1206 
1207 	for (i = 0; i < kinfo->num_tc; i++) {
1208 		u8 sch_mode = hdev->tm_info.tc_info[i].tc_sch_mode;
1209 
1210 		ret = hclge_tm_qs_schd_mode_cfg(hdev, vport->qs_offset + i,
1211 						sch_mode);
1212 		if (ret)
1213 			return ret;
1214 	}
1215 
1216 	return 0;
1217 }
1218 
hclge_tm_lvl34_schd_mode_cfg(struct hclge_dev * hdev)1219 static int hclge_tm_lvl34_schd_mode_cfg(struct hclge_dev *hdev)
1220 {
1221 	struct hclge_vport *vport = hdev->vport;
1222 	int ret;
1223 	u8 i, k;
1224 
1225 	if (hdev->tx_sch_mode == HCLGE_FLAG_TC_BASE_SCH_MODE) {
1226 		for (i = 0; i < hdev->tm_info.num_tc; i++) {
1227 			ret = hclge_tm_pri_schd_mode_cfg(hdev, i);
1228 			if (ret)
1229 				return ret;
1230 
1231 			for (k = 0; k < hdev->num_alloc_vport; k++) {
1232 				ret = hclge_tm_qs_schd_mode_cfg(
1233 					hdev, vport[k].qs_offset + i,
1234 					HCLGE_SCH_MODE_DWRR);
1235 				if (ret)
1236 					return ret;
1237 			}
1238 		}
1239 	} else {
1240 		for (i = 0; i < hdev->num_alloc_vport; i++) {
1241 			ret = hclge_tm_schd_mode_vnet_base_cfg(vport);
1242 			if (ret)
1243 				return ret;
1244 
1245 			vport++;
1246 		}
1247 	}
1248 
1249 	return 0;
1250 }
1251 
hclge_tm_schd_mode_hw(struct hclge_dev * hdev)1252 static int hclge_tm_schd_mode_hw(struct hclge_dev *hdev)
1253 {
1254 	int ret;
1255 
1256 	ret = hclge_tm_lvl2_schd_mode_cfg(hdev);
1257 	if (ret)
1258 		return ret;
1259 
1260 	return hclge_tm_lvl34_schd_mode_cfg(hdev);
1261 }
1262 
hclge_tm_schd_setup_hw(struct hclge_dev * hdev)1263 int hclge_tm_schd_setup_hw(struct hclge_dev *hdev)
1264 {
1265 	int ret;
1266 
1267 	/* Cfg tm mapping  */
1268 	ret = hclge_tm_map_cfg(hdev);
1269 	if (ret)
1270 		return ret;
1271 
1272 	/* Cfg tm shaper */
1273 	ret = hclge_tm_shaper_cfg(hdev);
1274 	if (ret)
1275 		return ret;
1276 
1277 	/* Cfg dwrr */
1278 	ret = hclge_tm_dwrr_cfg(hdev);
1279 	if (ret)
1280 		return ret;
1281 
1282 	/* Cfg schd mode for each level schd */
1283 	return hclge_tm_schd_mode_hw(hdev);
1284 }
1285 
hclge_pause_param_setup_hw(struct hclge_dev * hdev)1286 static int hclge_pause_param_setup_hw(struct hclge_dev *hdev)
1287 {
1288 	struct hclge_mac *mac = &hdev->hw.mac;
1289 
1290 	return hclge_pause_param_cfg(hdev, mac->mac_addr,
1291 				     HCLGE_DEFAULT_PAUSE_TRANS_GAP,
1292 				     HCLGE_DEFAULT_PAUSE_TRANS_TIME);
1293 }
1294 
hclge_pfc_setup_hw(struct hclge_dev * hdev)1295 static int hclge_pfc_setup_hw(struct hclge_dev *hdev)
1296 {
1297 	u8 enable_bitmap = 0;
1298 
1299 	if (hdev->tm_info.fc_mode == HCLGE_FC_PFC)
1300 		enable_bitmap = HCLGE_TX_MAC_PAUSE_EN_MSK |
1301 				HCLGE_RX_MAC_PAUSE_EN_MSK;
1302 
1303 	return hclge_pfc_pause_en_cfg(hdev, enable_bitmap,
1304 				      hdev->tm_info.pfc_en);
1305 }
1306 
1307 /* Each Tc has a 1024 queue sets to backpress, it divides to
1308  * 32 group, each group contains 32 queue sets, which can be
1309  * represented by u32 bitmap.
1310  */
hclge_bp_setup_hw(struct hclge_dev * hdev,u8 tc)1311 static int hclge_bp_setup_hw(struct hclge_dev *hdev, u8 tc)
1312 {
1313 	int i;
1314 
1315 	for (i = 0; i < HCLGE_BP_GRP_NUM; i++) {
1316 		u32 qs_bitmap = 0;
1317 		int k, ret;
1318 
1319 		for (k = 0; k < hdev->num_alloc_vport; k++) {
1320 			struct hclge_vport *vport = &hdev->vport[k];
1321 			u16 qs_id = vport->qs_offset + tc;
1322 			u8 grp, sub_grp;
1323 
1324 			grp = hnae3_get_field(qs_id, HCLGE_BP_GRP_ID_M,
1325 					      HCLGE_BP_GRP_ID_S);
1326 			sub_grp = hnae3_get_field(qs_id, HCLGE_BP_SUB_GRP_ID_M,
1327 						  HCLGE_BP_SUB_GRP_ID_S);
1328 			if (i == grp)
1329 				qs_bitmap |= (1 << sub_grp);
1330 		}
1331 
1332 		ret = hclge_tm_qs_bp_cfg(hdev, tc, i, qs_bitmap);
1333 		if (ret)
1334 			return ret;
1335 	}
1336 
1337 	return 0;
1338 }
1339 
hclge_mac_pause_setup_hw(struct hclge_dev * hdev)1340 static int hclge_mac_pause_setup_hw(struct hclge_dev *hdev)
1341 {
1342 	bool tx_en, rx_en;
1343 
1344 	switch (hdev->tm_info.fc_mode) {
1345 	case HCLGE_FC_NONE:
1346 		tx_en = false;
1347 		rx_en = false;
1348 		break;
1349 	case HCLGE_FC_RX_PAUSE:
1350 		tx_en = false;
1351 		rx_en = true;
1352 		break;
1353 	case HCLGE_FC_TX_PAUSE:
1354 		tx_en = true;
1355 		rx_en = false;
1356 		break;
1357 	case HCLGE_FC_FULL:
1358 		tx_en = true;
1359 		rx_en = true;
1360 		break;
1361 	case HCLGE_FC_PFC:
1362 		tx_en = false;
1363 		rx_en = false;
1364 		break;
1365 	default:
1366 		tx_en = true;
1367 		rx_en = true;
1368 	}
1369 
1370 	return hclge_mac_pause_en_cfg(hdev, tx_en, rx_en);
1371 }
1372 
hclge_tm_bp_setup(struct hclge_dev * hdev)1373 static int hclge_tm_bp_setup(struct hclge_dev *hdev)
1374 {
1375 	int ret;
1376 	int i;
1377 
1378 	for (i = 0; i < hdev->tm_info.num_tc; i++) {
1379 		ret = hclge_bp_setup_hw(hdev, i);
1380 		if (ret)
1381 			return ret;
1382 	}
1383 
1384 	return 0;
1385 }
1386 
hclge_pause_setup_hw(struct hclge_dev * hdev,bool init)1387 int hclge_pause_setup_hw(struct hclge_dev *hdev, bool init)
1388 {
1389 	int ret;
1390 
1391 	ret = hclge_pause_param_setup_hw(hdev);
1392 	if (ret)
1393 		return ret;
1394 
1395 	ret = hclge_mac_pause_setup_hw(hdev);
1396 	if (ret)
1397 		return ret;
1398 
1399 	/* Only DCB-supported dev supports qset back pressure and pfc cmd */
1400 	if (!hnae3_dev_dcb_supported(hdev))
1401 		return 0;
1402 
1403 	/* GE MAC does not support PFC, when driver is initializing and MAC
1404 	 * is in GE Mode, ignore the error here, otherwise initialization
1405 	 * will fail.
1406 	 */
1407 	ret = hclge_pfc_setup_hw(hdev);
1408 	if (init && ret == -EOPNOTSUPP)
1409 		dev_warn(&hdev->pdev->dev, "GE MAC does not support pfc\n");
1410 	else if (ret) {
1411 		dev_err(&hdev->pdev->dev, "config pfc failed! ret = %d\n",
1412 			ret);
1413 		return ret;
1414 	}
1415 
1416 	return hclge_tm_bp_setup(hdev);
1417 }
1418 
hclge_tm_prio_tc_info_update(struct hclge_dev * hdev,u8 * prio_tc)1419 void hclge_tm_prio_tc_info_update(struct hclge_dev *hdev, u8 *prio_tc)
1420 {
1421 	struct hclge_vport *vport = hdev->vport;
1422 	struct hnae3_knic_private_info *kinfo;
1423 	u32 i, k;
1424 
1425 	for (i = 0; i < HNAE3_MAX_USER_PRIO; i++) {
1426 		hdev->tm_info.prio_tc[i] = prio_tc[i];
1427 
1428 		for (k = 0;  k < hdev->num_alloc_vport; k++) {
1429 			kinfo = &vport[k].nic.kinfo;
1430 			kinfo->prio_tc[i] = prio_tc[i];
1431 		}
1432 	}
1433 }
1434 
hclge_tm_schd_info_update(struct hclge_dev * hdev,u8 num_tc)1435 void hclge_tm_schd_info_update(struct hclge_dev *hdev, u8 num_tc)
1436 {
1437 	u8 bit_map = 0;
1438 	u8 i;
1439 
1440 	hdev->tm_info.num_tc = num_tc;
1441 
1442 	for (i = 0; i < hdev->tm_info.num_tc; i++)
1443 		bit_map |= BIT(i);
1444 
1445 	if (!bit_map) {
1446 		bit_map = 1;
1447 		hdev->tm_info.num_tc = 1;
1448 	}
1449 
1450 	hdev->hw_tc_map = bit_map;
1451 
1452 	hclge_tm_schd_info_init(hdev);
1453 }
1454 
hclge_tm_init_hw(struct hclge_dev * hdev,bool init)1455 int hclge_tm_init_hw(struct hclge_dev *hdev, bool init)
1456 {
1457 	int ret;
1458 
1459 	if ((hdev->tx_sch_mode != HCLGE_FLAG_TC_BASE_SCH_MODE) &&
1460 	    (hdev->tx_sch_mode != HCLGE_FLAG_VNET_BASE_SCH_MODE))
1461 		return -ENOTSUPP;
1462 
1463 	ret = hclge_tm_schd_setup_hw(hdev);
1464 	if (ret)
1465 		return ret;
1466 
1467 	ret = hclge_pause_setup_hw(hdev, init);
1468 	if (ret)
1469 		return ret;
1470 
1471 	return 0;
1472 }
1473 
hclge_tm_schd_init(struct hclge_dev * hdev)1474 int hclge_tm_schd_init(struct hclge_dev *hdev)
1475 {
1476 	/* fc_mode is HCLGE_FC_FULL on reset */
1477 	hdev->tm_info.fc_mode = HCLGE_FC_FULL;
1478 	hdev->fc_mode_last_time = hdev->tm_info.fc_mode;
1479 
1480 	if (hdev->tx_sch_mode != HCLGE_FLAG_TC_BASE_SCH_MODE &&
1481 	    hdev->tm_info.num_pg != 1)
1482 		return -EINVAL;
1483 
1484 	hclge_tm_schd_info_init(hdev);
1485 
1486 	return hclge_tm_init_hw(hdev, true);
1487 }
1488 
hclge_tm_vport_map_update(struct hclge_dev * hdev)1489 int hclge_tm_vport_map_update(struct hclge_dev *hdev)
1490 {
1491 	struct hclge_vport *vport = hdev->vport;
1492 	int ret;
1493 
1494 	hclge_tm_vport_tc_info_update(vport);
1495 
1496 	ret = hclge_vport_q_to_qs_map(hdev, vport);
1497 	if (ret)
1498 		return ret;
1499 
1500 	if (hdev->tm_info.num_tc == 1 && !hdev->tm_info.pfc_en)
1501 		return 0;
1502 
1503 	return hclge_tm_bp_setup(hdev);
1504 }
1505