xref: /dpdk/lib/sched/rte_sched.c (revision 8f1d23ece06adff5eae9f1b4365bdbbd3abee2b2)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include <rte_common.h>
9 #include <rte_log.h>
10 #include <rte_malloc.h>
11 #include <rte_cycles.h>
12 #include <rte_prefetch.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_mbuf.h>
15 #include <rte_bitmap.h>
16 #include <rte_reciprocal.h>
17 
18 #include "rte_sched.h"
19 #include "rte_sched_common.h"
20 #include "rte_approx.h"
21 
22 #ifdef __INTEL_COMPILER
23 #pragma warning(disable:2259) /* conversion may lose significant bits */
24 #endif
25 
26 #ifndef RTE_SCHED_PORT_N_GRINDERS
27 #define RTE_SCHED_PORT_N_GRINDERS 8
28 #endif
29 
30 #define RTE_SCHED_TB_RATE_CONFIG_ERR          (1e-7)
31 #define RTE_SCHED_WRR_SHIFT                   3
32 #define RTE_SCHED_MAX_QUEUES_PER_TC           RTE_SCHED_BE_QUEUES_PER_PIPE
33 #define RTE_SCHED_GRINDER_PCACHE_SIZE         (64 / RTE_SCHED_QUEUES_PER_PIPE)
34 #define RTE_SCHED_PIPE_INVALID                UINT32_MAX
35 #define RTE_SCHED_BMP_POS_INVALID             UINT32_MAX
36 
37 /* Scaling for cycles_per_byte calculation
38  * Chosen so that minimum rate is 480 bit/sec
39  */
40 #define RTE_SCHED_TIME_SHIFT		      8
41 
42 struct rte_sched_pipe_profile {
43 	/* Token bucket (TB) */
44 	uint64_t tb_period;
45 	uint64_t tb_credits_per_period;
46 	uint64_t tb_size;
47 
48 	/* Pipe traffic classes */
49 	uint64_t tc_period;
50 	uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
51 	uint8_t tc_ov_weight;
52 
53 	/* Pipe best-effort traffic class queues */
54 	uint8_t  wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
55 };
56 
57 struct rte_sched_pipe {
58 	/* Token bucket (TB) */
59 	uint64_t tb_time; /* time of last update */
60 	uint64_t tb_credits;
61 
62 	/* Pipe profile and flags */
63 	uint32_t profile;
64 
65 	/* Traffic classes (TCs) */
66 	uint64_t tc_time; /* time of next update */
67 	uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
68 
69 	/* Weighted Round Robin (WRR) */
70 	uint8_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
71 
72 	/* TC oversubscription */
73 	uint64_t tc_ov_credits;
74 	uint8_t tc_ov_period_id;
75 } __rte_cache_aligned;
76 
77 struct rte_sched_queue {
78 	uint16_t qw;
79 	uint16_t qr;
80 };
81 
82 struct rte_sched_queue_extra {
83 	struct rte_sched_queue_stats stats;
84 	RTE_STD_C11
85 	union {
86 		struct rte_red red;
87 		struct rte_pie pie;
88 	};
89 };
90 
91 enum grinder_state {
92 	e_GRINDER_PREFETCH_PIPE = 0,
93 	e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
94 	e_GRINDER_PREFETCH_MBUF,
95 	e_GRINDER_READ_MBUF
96 };
97 
98 struct rte_sched_subport_profile {
99 	/* Token bucket (TB) */
100 	uint64_t tb_period;
101 	uint64_t tb_credits_per_period;
102 	uint64_t tb_size;
103 
104 	uint64_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
105 	uint64_t tc_period;
106 };
107 
108 struct rte_sched_grinder {
109 	/* Pipe cache */
110 	uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
111 	uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
112 	uint32_t pcache_w;
113 	uint32_t pcache_r;
114 
115 	/* Current pipe */
116 	enum grinder_state state;
117 	uint32_t productive;
118 	uint32_t pindex;
119 	struct rte_sched_subport *subport;
120 	struct rte_sched_subport_profile *subport_params;
121 	struct rte_sched_pipe *pipe;
122 	struct rte_sched_pipe_profile *pipe_params;
123 
124 	/* TC cache */
125 	uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
126 	uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
127 	uint32_t tccache_w;
128 	uint32_t tccache_r;
129 
130 	/* Current TC */
131 	uint32_t tc_index;
132 	struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
133 	struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
134 	uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
135 	uint16_t qsize;
136 	uint32_t qmask;
137 	uint32_t qpos;
138 	struct rte_mbuf *pkt;
139 
140 	/* WRR */
141 	uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
142 	uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
143 	uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
144 };
145 
146 struct rte_sched_subport {
147 	/* Token bucket (TB) */
148 	uint64_t tb_time; /* time of last update */
149 	uint64_t tb_credits;
150 
151 	/* Traffic classes (TCs) */
152 	uint64_t tc_time; /* time of next update */
153 	uint64_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
154 
155 	/* TC oversubscription */
156 	uint64_t tc_ov_wm;
157 	uint64_t tc_ov_wm_min;
158 	uint64_t tc_ov_wm_max;
159 	uint8_t tc_ov_period_id;
160 	uint8_t tc_ov;
161 	uint32_t tc_ov_n;
162 	double tc_ov_rate;
163 
164 	/* Statistics */
165 	struct rte_sched_subport_stats stats __rte_cache_aligned;
166 
167 	/* subport profile */
168 	uint32_t profile;
169 	/* Subport pipes */
170 	uint32_t n_pipes_per_subport_enabled;
171 	uint32_t n_pipe_profiles;
172 	uint32_t n_max_pipe_profiles;
173 
174 	/* Pipe best-effort TC rate */
175 	uint64_t pipe_tc_be_rate_max;
176 
177 	/* Pipe queues size */
178 	uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
179 
180 	bool cman_enabled;
181 	enum rte_sched_cman_mode cman;
182 
183 	RTE_STD_C11
184 	union {
185 		struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
186 		struct rte_pie_config pie_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
187 	};
188 
189 	/* Scheduling loop detection */
190 	uint32_t pipe_loop;
191 	uint32_t pipe_exhaustion;
192 
193 	/* Bitmap */
194 	struct rte_bitmap *bmp;
195 	uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
196 
197 	/* Grinders */
198 	struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
199 	uint32_t busy_grinders;
200 
201 	/* Queue base calculation */
202 	uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
203 	uint32_t qsize_sum;
204 
205 	struct rte_sched_pipe *pipe;
206 	struct rte_sched_queue *queue;
207 	struct rte_sched_queue_extra *queue_extra;
208 	struct rte_sched_pipe_profile *pipe_profiles;
209 	uint8_t *bmp_array;
210 	struct rte_mbuf **queue_array;
211 	uint8_t memory[0] __rte_cache_aligned;
212 
213 	/* TC oversubscription activation */
214 	int tc_ov_enabled;
215 } __rte_cache_aligned;
216 
217 struct rte_sched_port {
218 	/* User parameters */
219 	uint32_t n_subports_per_port;
220 	uint32_t n_pipes_per_subport;
221 	uint32_t n_pipes_per_subport_log2;
222 	uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
223 	uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
224 	uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
225 	uint32_t n_subport_profiles;
226 	uint32_t n_max_subport_profiles;
227 	uint64_t rate;
228 	uint32_t mtu;
229 	uint32_t frame_overhead;
230 	int socket;
231 
232 	/* Timing */
233 	uint64_t time_cpu_cycles;     /* Current CPU time measured in CPU cycles */
234 	uint64_t time_cpu_bytes;      /* Current CPU time measured in bytes */
235 	uint64_t time;                /* Current NIC TX time measured in bytes */
236 	struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
237 	uint64_t cycles_per_byte;
238 
239 	/* Grinders */
240 	struct rte_mbuf **pkts_out;
241 	uint32_t n_pkts_out;
242 	uint32_t subport_id;
243 
244 	/* Large data structures */
245 	struct rte_sched_subport_profile *subport_profiles;
246 	struct rte_sched_subport *subports[0] __rte_cache_aligned;
247 } __rte_cache_aligned;
248 
249 enum rte_sched_subport_array {
250 	e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
251 	e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
252 	e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
253 	e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
254 	e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
255 	e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
256 	e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
257 };
258 
259 static inline uint32_t
260 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
261 {
262 	return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
263 }
264 
265 static inline struct rte_mbuf **
266 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
267 {
268 	uint32_t pindex = qindex >> 4;
269 	uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
270 
271 	return (subport->queue_array + pindex *
272 		subport->qsize_sum + subport->qsize_add[qpos]);
273 }
274 
275 static inline uint16_t
276 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
277 struct rte_sched_subport *subport, uint32_t qindex)
278 {
279 	uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
280 
281 	return subport->qsize[tc];
282 }
283 
284 static inline uint32_t
285 rte_sched_port_queues_per_port(struct rte_sched_port *port)
286 {
287 	uint32_t n_queues = 0, i;
288 
289 	for (i = 0; i < port->n_subports_per_port; i++)
290 		n_queues += rte_sched_subport_pipe_queues(port->subports[i]);
291 
292 	return n_queues;
293 }
294 
295 static inline uint16_t
296 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
297 {
298 	uint16_t pipe_queue = port->pipe_queue[traffic_class];
299 
300 	return pipe_queue;
301 }
302 
303 static inline uint8_t
304 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
305 {
306 	uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
307 
308 	return pipe_tc;
309 }
310 
311 static inline uint8_t
312 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
313 {
314 	uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
315 
316 	return tc_queue;
317 }
318 
319 static int
320 pipe_profile_check(struct rte_sched_pipe_params *params,
321 	uint64_t rate, uint16_t *qsize)
322 {
323 	uint32_t i;
324 
325 	/* Pipe parameters */
326 	if (params == NULL) {
327 		RTE_LOG(ERR, SCHED,
328 			"%s: Incorrect value for parameter params\n", __func__);
329 		return -EINVAL;
330 	}
331 
332 	/* TB rate: non-zero, not greater than port rate */
333 	if (params->tb_rate == 0 ||
334 		params->tb_rate > rate) {
335 		RTE_LOG(ERR, SCHED,
336 			"%s: Incorrect value for tb rate\n", __func__);
337 		return -EINVAL;
338 	}
339 
340 	/* TB size: non-zero */
341 	if (params->tb_size == 0) {
342 		RTE_LOG(ERR, SCHED,
343 			"%s: Incorrect value for tb size\n", __func__);
344 		return -EINVAL;
345 	}
346 
347 	/* TC rate: non-zero if qsize non-zero, less than pipe rate */
348 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
349 		if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
350 			(qsize[i] != 0 && (params->tc_rate[i] == 0 ||
351 			params->tc_rate[i] > params->tb_rate))) {
352 			RTE_LOG(ERR, SCHED,
353 				"%s: Incorrect value for qsize or tc_rate\n", __func__);
354 			return -EINVAL;
355 		}
356 	}
357 
358 	if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
359 		qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
360 		RTE_LOG(ERR, SCHED,
361 			"%s: Incorrect value for be traffic class rate\n", __func__);
362 		return -EINVAL;
363 	}
364 
365 	/* TC period: non-zero */
366 	if (params->tc_period == 0) {
367 		RTE_LOG(ERR, SCHED,
368 			"%s: Incorrect value for tc period\n", __func__);
369 		return -EINVAL;
370 	}
371 
372 	/*  Best effort tc oversubscription weight: non-zero */
373 	if (params->tc_ov_weight == 0) {
374 		RTE_LOG(ERR, SCHED,
375 			"%s: Incorrect value for tc ov weight\n", __func__);
376 		return -EINVAL;
377 	}
378 
379 	/* Queue WRR weights: non-zero */
380 	for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
381 		if (params->wrr_weights[i] == 0) {
382 			RTE_LOG(ERR, SCHED,
383 				"%s: Incorrect value for wrr weight\n", __func__);
384 			return -EINVAL;
385 		}
386 	}
387 
388 	return 0;
389 }
390 
391 static int
392 subport_profile_check(struct rte_sched_subport_profile_params *params,
393 	uint64_t rate)
394 {
395 	uint32_t i;
396 
397 	/* Check user parameters */
398 	if (params == NULL) {
399 		RTE_LOG(ERR, SCHED, "%s: "
400 		"Incorrect value for parameter params\n", __func__);
401 		return -EINVAL;
402 	}
403 
404 	if (params->tb_rate == 0 || params->tb_rate > rate) {
405 		RTE_LOG(ERR, SCHED, "%s: "
406 		"Incorrect value for tb rate\n", __func__);
407 		return -EINVAL;
408 	}
409 
410 	if (params->tb_size == 0) {
411 		RTE_LOG(ERR, SCHED, "%s: "
412 		"Incorrect value for tb size\n", __func__);
413 		return -EINVAL;
414 	}
415 
416 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
417 		uint64_t tc_rate = params->tc_rate[i];
418 
419 		if (tc_rate == 0 || (tc_rate > params->tb_rate)) {
420 			RTE_LOG(ERR, SCHED, "%s: "
421 			"Incorrect value for tc rate\n", __func__);
422 			return -EINVAL;
423 		}
424 	}
425 
426 	if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
427 		RTE_LOG(ERR, SCHED, "%s: "
428 		"Incorrect tc rate(best effort)\n", __func__);
429 		return -EINVAL;
430 	}
431 
432 	if (params->tc_period == 0) {
433 		RTE_LOG(ERR, SCHED, "%s: "
434 		"Incorrect value for tc period\n", __func__);
435 		return -EINVAL;
436 	}
437 
438 	return 0;
439 }
440 
441 static int
442 rte_sched_port_check_params(struct rte_sched_port_params *params)
443 {
444 	uint32_t i;
445 
446 	if (params == NULL) {
447 		RTE_LOG(ERR, SCHED,
448 			"%s: Incorrect value for parameter params\n", __func__);
449 		return -EINVAL;
450 	}
451 
452 	/* socket */
453 	if (params->socket < 0) {
454 		RTE_LOG(ERR, SCHED,
455 			"%s: Incorrect value for socket id\n", __func__);
456 		return -EINVAL;
457 	}
458 
459 	/* rate */
460 	if (params->rate == 0) {
461 		RTE_LOG(ERR, SCHED,
462 			"%s: Incorrect value for rate\n", __func__);
463 		return -EINVAL;
464 	}
465 
466 	/* mtu */
467 	if (params->mtu == 0) {
468 		RTE_LOG(ERR, SCHED,
469 			"%s: Incorrect value for mtu\n", __func__);
470 		return -EINVAL;
471 	}
472 
473 	/* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
474 	if (params->n_subports_per_port == 0 ||
475 	    params->n_subports_per_port > 1u << 16 ||
476 	    !rte_is_power_of_2(params->n_subports_per_port)) {
477 		RTE_LOG(ERR, SCHED,
478 			"%s: Incorrect value for number of subports\n", __func__);
479 		return -EINVAL;
480 	}
481 
482 	if (params->subport_profiles == NULL ||
483 		params->n_subport_profiles == 0 ||
484 		params->n_max_subport_profiles == 0 ||
485 		params->n_subport_profiles > params->n_max_subport_profiles) {
486 		RTE_LOG(ERR, SCHED,
487 		"%s: Incorrect value for subport profiles\n", __func__);
488 		return -EINVAL;
489 	}
490 
491 	for (i = 0; i < params->n_subport_profiles; i++) {
492 		struct rte_sched_subport_profile_params *p =
493 						params->subport_profiles + i;
494 		int status;
495 
496 		status = subport_profile_check(p, params->rate);
497 		if (status != 0) {
498 			RTE_LOG(ERR, SCHED,
499 			"%s: subport profile check failed(%d)\n",
500 			__func__, status);
501 			return -EINVAL;
502 		}
503 	}
504 
505 	/* n_pipes_per_subport: non-zero, power of 2 */
506 	if (params->n_pipes_per_subport == 0 ||
507 	    !rte_is_power_of_2(params->n_pipes_per_subport)) {
508 		RTE_LOG(ERR, SCHED,
509 			"%s: Incorrect value for maximum pipes number\n", __func__);
510 		return -EINVAL;
511 	}
512 
513 	return 0;
514 }
515 
516 static uint32_t
517 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
518 	enum rte_sched_subport_array array)
519 {
520 	uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
521 	uint32_t n_subport_pipe_queues =
522 		RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
523 
524 	uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
525 	uint32_t size_queue =
526 		n_subport_pipe_queues * sizeof(struct rte_sched_queue);
527 	uint32_t size_queue_extra
528 		= n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
529 	uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
530 		sizeof(struct rte_sched_pipe_profile);
531 	uint32_t size_bmp_array =
532 		rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
533 	uint32_t size_per_pipe_queue_array, size_queue_array;
534 
535 	uint32_t base, i;
536 
537 	size_per_pipe_queue_array = 0;
538 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
539 		if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
540 			size_per_pipe_queue_array +=
541 				params->qsize[i] * sizeof(struct rte_mbuf *);
542 		else
543 			size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
544 				params->qsize[i] * sizeof(struct rte_mbuf *);
545 	}
546 	size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
547 
548 	base = 0;
549 
550 	if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
551 		return base;
552 	base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
553 
554 	if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
555 		return base;
556 	base += RTE_CACHE_LINE_ROUNDUP(size_queue);
557 
558 	if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
559 		return base;
560 	base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
561 
562 	if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
563 		return base;
564 	base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
565 
566 	if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
567 		return base;
568 	base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
569 
570 	if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
571 		return base;
572 	base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
573 
574 	return base;
575 }
576 
577 static void
578 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
579 {
580 	uint32_t i;
581 
582 	subport->qsize_add[0] = 0;
583 
584 	/* Strict priority traffic class */
585 	for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
586 		subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
587 
588 	/* Best-effort traffic class */
589 	subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
590 		subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
591 		subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
592 	subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
593 		subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
594 		subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
595 	subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
596 		subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
597 		subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
598 
599 	subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
600 		subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
601 }
602 
603 static void
604 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
605 {
606 	struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
607 
608 	RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
609 		"	Token bucket: period = %"PRIu64", credits per period = %"PRIu64", size = %"PRIu64"\n"
610 		"	Traffic classes: period = %"PRIu64",\n"
611 		"	credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
612 		", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
613 		", %"PRIu64", %"PRIu64", %"PRIu64"]\n"
614 		"	Best-effort traffic class oversubscription: weight = %hhu\n"
615 		"	WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
616 		i,
617 
618 		/* Token bucket */
619 		p->tb_period,
620 		p->tb_credits_per_period,
621 		p->tb_size,
622 
623 		/* Traffic classes */
624 		p->tc_period,
625 		p->tc_credits_per_period[0],
626 		p->tc_credits_per_period[1],
627 		p->tc_credits_per_period[2],
628 		p->tc_credits_per_period[3],
629 		p->tc_credits_per_period[4],
630 		p->tc_credits_per_period[5],
631 		p->tc_credits_per_period[6],
632 		p->tc_credits_per_period[7],
633 		p->tc_credits_per_period[8],
634 		p->tc_credits_per_period[9],
635 		p->tc_credits_per_period[10],
636 		p->tc_credits_per_period[11],
637 		p->tc_credits_per_period[12],
638 
639 		/* Best-effort traffic class oversubscription */
640 		p->tc_ov_weight,
641 
642 		/* WRR */
643 		p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
644 }
645 
646 static void
647 rte_sched_port_log_subport_profile(struct rte_sched_port *port, uint32_t i)
648 {
649 	struct rte_sched_subport_profile *p = port->subport_profiles + i;
650 
651 	RTE_LOG(DEBUG, SCHED, "Low level config for subport profile %u:\n"
652 	"Token bucket: period = %"PRIu64", credits per period = %"PRIu64","
653 	"size = %"PRIu64"\n"
654 	"Traffic classes: period = %"PRIu64",\n"
655 	"credits per period = [%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
656 	" %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64
657 	" %"PRIu64", %"PRIu64", %"PRIu64"]\n",
658 	i,
659 
660 	/* Token bucket */
661 	p->tb_period,
662 	p->tb_credits_per_period,
663 	p->tb_size,
664 
665 	/* Traffic classes */
666 	p->tc_period,
667 	p->tc_credits_per_period[0],
668 	p->tc_credits_per_period[1],
669 	p->tc_credits_per_period[2],
670 	p->tc_credits_per_period[3],
671 	p->tc_credits_per_period[4],
672 	p->tc_credits_per_period[5],
673 	p->tc_credits_per_period[6],
674 	p->tc_credits_per_period[7],
675 	p->tc_credits_per_period[8],
676 	p->tc_credits_per_period[9],
677 	p->tc_credits_per_period[10],
678 	p->tc_credits_per_period[11],
679 	p->tc_credits_per_period[12]);
680 }
681 
682 static inline uint64_t
683 rte_sched_time_ms_to_bytes(uint64_t time_ms, uint64_t rate)
684 {
685 	uint64_t time = time_ms;
686 
687 	time = (time * rate) / 1000;
688 
689 	return time;
690 }
691 
692 static void
693 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
694 	struct rte_sched_pipe_params *src,
695 	struct rte_sched_pipe_profile *dst,
696 	uint64_t rate)
697 {
698 	uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
699 	uint32_t lcd1, lcd2, lcd;
700 	uint32_t i;
701 
702 	/* Token Bucket */
703 	if (src->tb_rate == rate) {
704 		dst->tb_credits_per_period = 1;
705 		dst->tb_period = 1;
706 	} else {
707 		double tb_rate = (double) src->tb_rate
708 				/ (double) rate;
709 		double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
710 
711 		rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
712 			&dst->tb_period);
713 	}
714 
715 	dst->tb_size = src->tb_size;
716 
717 	/* Traffic Classes */
718 	dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
719 						rate);
720 
721 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
722 		if (subport->qsize[i])
723 			dst->tc_credits_per_period[i]
724 				= rte_sched_time_ms_to_bytes(src->tc_period,
725 					src->tc_rate[i]);
726 
727 	dst->tc_ov_weight = src->tc_ov_weight;
728 
729 	/* WRR queues */
730 	wrr_cost[0] = src->wrr_weights[0];
731 	wrr_cost[1] = src->wrr_weights[1];
732 	wrr_cost[2] = src->wrr_weights[2];
733 	wrr_cost[3] = src->wrr_weights[3];
734 
735 	lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
736 	lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
737 	lcd = rte_get_lcd(lcd1, lcd2);
738 
739 	wrr_cost[0] = lcd / wrr_cost[0];
740 	wrr_cost[1] = lcd / wrr_cost[1];
741 	wrr_cost[2] = lcd / wrr_cost[2];
742 	wrr_cost[3] = lcd / wrr_cost[3];
743 
744 	dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
745 	dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
746 	dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
747 	dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
748 }
749 
750 static void
751 rte_sched_subport_profile_convert(struct rte_sched_subport_profile_params *src,
752 	struct rte_sched_subport_profile *dst,
753 	uint64_t rate)
754 {
755 	uint32_t i;
756 
757 	/* Token Bucket */
758 	if (src->tb_rate == rate) {
759 		dst->tb_credits_per_period = 1;
760 		dst->tb_period = 1;
761 	} else {
762 		double tb_rate = (double) src->tb_rate
763 				/ (double) rate;
764 		double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
765 
766 		rte_approx_64(tb_rate, d, &dst->tb_credits_per_period,
767 			&dst->tb_period);
768 	}
769 
770 	dst->tb_size = src->tb_size;
771 
772 	/* Traffic Classes */
773 	dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period, rate);
774 
775 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
776 		dst->tc_credits_per_period[i]
777 			= rte_sched_time_ms_to_bytes(src->tc_period,
778 				src->tc_rate[i]);
779 }
780 
781 static void
782 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
783 	struct rte_sched_subport_params *params, uint64_t rate)
784 {
785 	uint32_t i;
786 
787 	for (i = 0; i < subport->n_pipe_profiles; i++) {
788 		struct rte_sched_pipe_params *src = params->pipe_profiles + i;
789 		struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
790 
791 		rte_sched_pipe_profile_convert(subport, src, dst, rate);
792 		rte_sched_port_log_pipe_profile(subport, i);
793 	}
794 
795 	subport->pipe_tc_be_rate_max = 0;
796 	for (i = 0; i < subport->n_pipe_profiles; i++) {
797 		struct rte_sched_pipe_params *src = params->pipe_profiles + i;
798 		uint64_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
799 
800 		if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
801 			subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
802 	}
803 }
804 
805 static void
806 rte_sched_port_config_subport_profile_table(struct rte_sched_port *port,
807 	struct rte_sched_port_params *params,
808 	uint64_t rate)
809 {
810 	uint32_t i;
811 
812 	for (i = 0; i < port->n_subport_profiles; i++) {
813 		struct rte_sched_subport_profile_params *src
814 				= params->subport_profiles + i;
815 		struct rte_sched_subport_profile *dst
816 				= port->subport_profiles + i;
817 
818 		rte_sched_subport_profile_convert(src, dst, rate);
819 		rte_sched_port_log_subport_profile(port, i);
820 	}
821 }
822 
823 static int
824 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
825 	uint32_t n_max_pipes_per_subport,
826 	uint64_t rate)
827 {
828 	uint32_t i;
829 
830 	/* Check user parameters */
831 	if (params == NULL) {
832 		RTE_LOG(ERR, SCHED,
833 			"%s: Incorrect value for parameter params\n", __func__);
834 		return -EINVAL;
835 	}
836 
837 	/* qsize: if non-zero, power of 2,
838 	 * no bigger than 32K (due to 16-bit read/write pointers)
839 	 */
840 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
841 		uint16_t qsize = params->qsize[i];
842 
843 		if (qsize != 0 && !rte_is_power_of_2(qsize)) {
844 			RTE_LOG(ERR, SCHED,
845 				"%s: Incorrect value for qsize\n", __func__);
846 			return -EINVAL;
847 		}
848 	}
849 
850 	if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
851 		RTE_LOG(ERR, SCHED, "%s: Incorrect qsize\n", __func__);
852 		return -EINVAL;
853 	}
854 
855 	/* n_pipes_per_subport: non-zero, power of 2 */
856 	if (params->n_pipes_per_subport_enabled == 0 ||
857 		params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
858 	    !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
859 		RTE_LOG(ERR, SCHED,
860 			"%s: Incorrect value for pipes number\n", __func__);
861 		return -EINVAL;
862 	}
863 
864 	/* pipe_profiles and n_pipe_profiles */
865 	if (params->pipe_profiles == NULL ||
866 	    params->n_pipe_profiles == 0 ||
867 		params->n_max_pipe_profiles == 0 ||
868 		params->n_pipe_profiles > params->n_max_pipe_profiles) {
869 		RTE_LOG(ERR, SCHED,
870 			"%s: Incorrect value for pipe profiles\n", __func__);
871 		return -EINVAL;
872 	}
873 
874 	for (i = 0; i < params->n_pipe_profiles; i++) {
875 		struct rte_sched_pipe_params *p = params->pipe_profiles + i;
876 		int status;
877 
878 		status = pipe_profile_check(p, rate, &params->qsize[0]);
879 		if (status != 0) {
880 			RTE_LOG(ERR, SCHED,
881 				"%s: Pipe profile check failed(%d)\n", __func__, status);
882 			return -EINVAL;
883 		}
884 	}
885 
886 	return 0;
887 }
888 
889 uint32_t
890 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *port_params,
891 	struct rte_sched_subport_params **subport_params)
892 {
893 	uint32_t size0 = 0, size1 = 0, i;
894 	int status;
895 
896 	status = rte_sched_port_check_params(port_params);
897 	if (status != 0) {
898 		RTE_LOG(ERR, SCHED,
899 			"%s: Port scheduler port params check failed (%d)\n",
900 			__func__, status);
901 
902 		return 0;
903 	}
904 
905 	for (i = 0; i < port_params->n_subports_per_port; i++) {
906 		struct rte_sched_subport_params *sp = subport_params[i];
907 
908 		status = rte_sched_subport_check_params(sp,
909 				port_params->n_pipes_per_subport,
910 				port_params->rate);
911 		if (status != 0) {
912 			RTE_LOG(ERR, SCHED,
913 				"%s: Port scheduler subport params check failed (%d)\n",
914 				__func__, status);
915 
916 			return 0;
917 		}
918 	}
919 
920 	size0 = sizeof(struct rte_sched_port);
921 
922 	for (i = 0; i < port_params->n_subports_per_port; i++) {
923 		struct rte_sched_subport_params *sp = subport_params[i];
924 
925 		size1 += rte_sched_subport_get_array_base(sp,
926 					e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
927 	}
928 
929 	return size0 + size1;
930 }
931 
932 struct rte_sched_port *
933 rte_sched_port_config(struct rte_sched_port_params *params)
934 {
935 	struct rte_sched_port *port = NULL;
936 	uint32_t size0, size1, size2;
937 	uint32_t cycles_per_byte;
938 	uint32_t i, j;
939 	int status;
940 
941 	status = rte_sched_port_check_params(params);
942 	if (status != 0) {
943 		RTE_LOG(ERR, SCHED,
944 			"%s: Port scheduler params check failed (%d)\n",
945 			__func__, status);
946 		return NULL;
947 	}
948 
949 	size0 = sizeof(struct rte_sched_port);
950 	size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
951 	size2 = params->n_max_subport_profiles *
952 		sizeof(struct rte_sched_subport_profile);
953 
954 	/* Allocate memory to store the data structures */
955 	port = rte_zmalloc_socket("qos_params", size0 + size1,
956 				 RTE_CACHE_LINE_SIZE, params->socket);
957 	if (port == NULL) {
958 		RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
959 
960 		return NULL;
961 	}
962 
963 	/* Allocate memory to store the subport profile */
964 	port->subport_profiles  = rte_zmalloc_socket("subport_profile", size2,
965 					RTE_CACHE_LINE_SIZE, params->socket);
966 	if (port->subport_profiles == NULL) {
967 		RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
968 		rte_free(port);
969 		return NULL;
970 	}
971 
972 	/* User parameters */
973 	port->n_subports_per_port = params->n_subports_per_port;
974 	port->n_subport_profiles = params->n_subport_profiles;
975 	port->n_max_subport_profiles = params->n_max_subport_profiles;
976 	port->n_pipes_per_subport = params->n_pipes_per_subport;
977 	port->n_pipes_per_subport_log2 =
978 			__builtin_ctz(params->n_pipes_per_subport);
979 	port->socket = params->socket;
980 
981 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
982 		port->pipe_queue[i] = i;
983 
984 	for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
985 		port->pipe_tc[i] = j;
986 
987 		if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
988 			j++;
989 	}
990 
991 	for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
992 		port->tc_queue[i] = j;
993 
994 		if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
995 			j++;
996 	}
997 	port->rate = params->rate;
998 	port->mtu = params->mtu + params->frame_overhead;
999 	port->frame_overhead = params->frame_overhead;
1000 
1001 	/* Timing */
1002 	port->time_cpu_cycles = rte_get_tsc_cycles();
1003 	port->time_cpu_bytes = 0;
1004 	port->time = 0;
1005 
1006 	/* Subport profile table */
1007 	rte_sched_port_config_subport_profile_table(port, params, port->rate);
1008 
1009 	cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
1010 		/ params->rate;
1011 	port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
1012 	port->cycles_per_byte = cycles_per_byte;
1013 
1014 	/* Grinders */
1015 	port->pkts_out = NULL;
1016 	port->n_pkts_out = 0;
1017 	port->subport_id = 0;
1018 
1019 	return port;
1020 }
1021 
1022 static inline void
1023 rte_sched_subport_free(struct rte_sched_port *port,
1024 	struct rte_sched_subport *subport)
1025 {
1026 	uint32_t n_subport_pipe_queues;
1027 	uint32_t qindex;
1028 
1029 	if (subport == NULL)
1030 		return;
1031 
1032 	n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
1033 
1034 	/* Free enqueued mbufs */
1035 	for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
1036 		struct rte_mbuf **mbufs =
1037 			rte_sched_subport_pipe_qbase(subport, qindex);
1038 		uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1039 		if (qsize != 0) {
1040 			struct rte_sched_queue *queue = subport->queue + qindex;
1041 			uint16_t qr = queue->qr & (qsize - 1);
1042 			uint16_t qw = queue->qw & (qsize - 1);
1043 
1044 			for (; qr != qw; qr = (qr + 1) & (qsize - 1))
1045 				rte_pktmbuf_free(mbufs[qr]);
1046 		}
1047 	}
1048 
1049 	rte_free(subport);
1050 }
1051 
1052 void
1053 rte_sched_port_free(struct rte_sched_port *port)
1054 {
1055 	uint32_t i;
1056 
1057 	/* Check user parameters */
1058 	if (port == NULL)
1059 		return;
1060 
1061 	for (i = 0; i < port->n_subports_per_port; i++)
1062 		rte_sched_subport_free(port, port->subports[i]);
1063 
1064 	rte_free(port->subport_profiles);
1065 	rte_free(port);
1066 }
1067 
1068 static void
1069 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1070 {
1071 	uint32_t i;
1072 
1073 	for (i = 0; i < n_subports; i++) {
1074 		struct rte_sched_subport *subport = port->subports[i];
1075 
1076 		rte_sched_subport_free(port, subport);
1077 	}
1078 
1079 	rte_free(port->subport_profiles);
1080 	rte_free(port);
1081 }
1082 
1083 static int
1084 rte_sched_red_config(struct rte_sched_port *port,
1085 	struct rte_sched_subport *s,
1086 	struct rte_sched_subport_params *params,
1087 	uint32_t n_subports)
1088 {
1089 	uint32_t i;
1090 
1091 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1092 
1093 		uint32_t j;
1094 
1095 		for (j = 0; j < RTE_COLORS; j++) {
1096 			/* if min/max are both zero, then RED is disabled */
1097 			if ((params->cman_params->red_params[i][j].min_th |
1098 				 params->cman_params->red_params[i][j].max_th) == 0) {
1099 				continue;
1100 			}
1101 
1102 			if (rte_red_config_init(&s->red_config[i][j],
1103 				params->cman_params->red_params[i][j].wq_log2,
1104 				params->cman_params->red_params[i][j].min_th,
1105 				params->cman_params->red_params[i][j].max_th,
1106 				params->cman_params->red_params[i][j].maxp_inv) != 0) {
1107 				rte_sched_free_memory(port, n_subports);
1108 
1109 				RTE_LOG(NOTICE, SCHED,
1110 				"%s: RED configuration init fails\n", __func__);
1111 				return -EINVAL;
1112 			}
1113 		}
1114 	}
1115 	s->cman = RTE_SCHED_CMAN_RED;
1116 	return 0;
1117 }
1118 
1119 static int
1120 rte_sched_pie_config(struct rte_sched_port *port,
1121 	struct rte_sched_subport *s,
1122 	struct rte_sched_subport_params *params,
1123 	uint32_t n_subports)
1124 {
1125 	uint32_t i;
1126 
1127 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1128 		if (params->cman_params->pie_params[i].tailq_th > params->qsize[i]) {
1129 			RTE_LOG(NOTICE, SCHED,
1130 			"%s: PIE tailq threshold incorrect\n", __func__);
1131 			return -EINVAL;
1132 		}
1133 
1134 		if (rte_pie_config_init(&s->pie_config[i],
1135 			params->cman_params->pie_params[i].qdelay_ref,
1136 			params->cman_params->pie_params[i].dp_update_interval,
1137 			params->cman_params->pie_params[i].max_burst,
1138 			params->cman_params->pie_params[i].tailq_th) != 0) {
1139 			rte_sched_free_memory(port, n_subports);
1140 
1141 			RTE_LOG(NOTICE, SCHED,
1142 			"%s: PIE configuration init fails\n", __func__);
1143 			return -EINVAL;
1144 			}
1145 	}
1146 	s->cman = RTE_SCHED_CMAN_PIE;
1147 	return 0;
1148 }
1149 
1150 static int
1151 rte_sched_cman_config(struct rte_sched_port *port,
1152 	struct rte_sched_subport *s,
1153 	struct rte_sched_subport_params *params,
1154 	uint32_t n_subports)
1155 {
1156 	if (params->cman_params->cman_mode == RTE_SCHED_CMAN_RED)
1157 		return rte_sched_red_config(port, s, params, n_subports);
1158 
1159 	else if (params->cman_params->cman_mode == RTE_SCHED_CMAN_PIE)
1160 		return rte_sched_pie_config(port, s, params, n_subports);
1161 
1162 	return -EINVAL;
1163 }
1164 
1165 int
1166 rte_sched_subport_tc_ov_config(struct rte_sched_port *port,
1167 	uint32_t subport_id,
1168 	bool tc_ov_enable)
1169 {
1170 	struct rte_sched_subport *s;
1171 
1172 	if (port == NULL) {
1173 		RTE_LOG(ERR, SCHED,
1174 			"%s: Incorrect value for parameter port\n", __func__);
1175 		return -EINVAL;
1176 	}
1177 
1178 	if (subport_id >= port->n_subports_per_port) {
1179 		RTE_LOG(ERR, SCHED,
1180 			"%s: Incorrect value for parameter subport id\n", __func__);
1181 		return  -EINVAL;
1182 	}
1183 
1184 	s = port->subports[subport_id];
1185 	s->tc_ov_enabled = tc_ov_enable ? 1 : 0;
1186 
1187 	return 0;
1188 }
1189 
1190 int
1191 rte_sched_subport_config(struct rte_sched_port *port,
1192 	uint32_t subport_id,
1193 	struct rte_sched_subport_params *params,
1194 	uint32_t subport_profile_id)
1195 {
1196 	struct rte_sched_subport *s = NULL;
1197 	uint32_t n_subports = subport_id;
1198 	struct rte_sched_subport_profile *profile;
1199 	uint32_t n_subport_pipe_queues, i;
1200 	uint32_t size0, size1, bmp_mem_size;
1201 	int status;
1202 	int ret;
1203 
1204 	/* Check user parameters */
1205 	if (port == NULL) {
1206 		RTE_LOG(ERR, SCHED,
1207 			"%s: Incorrect value for parameter port\n", __func__);
1208 		return 0;
1209 	}
1210 
1211 	if (subport_id >= port->n_subports_per_port) {
1212 		RTE_LOG(ERR, SCHED,
1213 			"%s: Incorrect value for subport id\n", __func__);
1214 		ret = -EINVAL;
1215 		goto out;
1216 	}
1217 
1218 	if (subport_profile_id >= port->n_max_subport_profiles) {
1219 		RTE_LOG(ERR, SCHED, "%s: "
1220 			"Number of subport profile exceeds the max limit\n",
1221 			__func__);
1222 		ret = -EINVAL;
1223 		goto out;
1224 	}
1225 
1226 	/** Memory is allocated only on first invocation of the api for a
1227 	 * given subport. Subsequent invocation on same subport will just
1228 	 * update subport bandwidth parameter.
1229 	 **/
1230 	if (port->subports[subport_id] == NULL) {
1231 
1232 		status = rte_sched_subport_check_params(params,
1233 			port->n_pipes_per_subport,
1234 			port->rate);
1235 		if (status != 0) {
1236 			RTE_LOG(NOTICE, SCHED,
1237 				"%s: Port scheduler params check failed (%d)\n",
1238 				__func__, status);
1239 			ret = -EINVAL;
1240 			goto out;
1241 		}
1242 
1243 		/* Determine the amount of memory to allocate */
1244 		size0 = sizeof(struct rte_sched_subport);
1245 		size1 = rte_sched_subport_get_array_base(params,
1246 					e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1247 
1248 		/* Allocate memory to store the data structures */
1249 		s = rte_zmalloc_socket("subport_params", size0 + size1,
1250 			RTE_CACHE_LINE_SIZE, port->socket);
1251 		if (s == NULL) {
1252 			RTE_LOG(ERR, SCHED,
1253 				"%s: Memory allocation fails\n", __func__);
1254 			ret = -ENOMEM;
1255 			goto out;
1256 		}
1257 
1258 		n_subports++;
1259 
1260 		subport_profile_id = 0;
1261 
1262 		/* Port */
1263 		port->subports[subport_id] = s;
1264 
1265 		s->tb_time = port->time;
1266 
1267 		/* compile time checks */
1268 		RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1269 		RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1270 			(RTE_SCHED_PORT_N_GRINDERS - 1));
1271 
1272 		/* User parameters */
1273 		s->n_pipes_per_subport_enabled =
1274 				params->n_pipes_per_subport_enabled;
1275 		memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1276 		s->n_pipe_profiles = params->n_pipe_profiles;
1277 		s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1278 
1279 		/* TC oversubscription is enabled by default */
1280 		s->tc_ov_enabled = 1;
1281 
1282 		if (params->cman_params != NULL) {
1283 			s->cman_enabled = true;
1284 			status = rte_sched_cman_config(port, s, params, n_subports);
1285 			if (status) {
1286 				RTE_LOG(NOTICE, SCHED,
1287 					"%s: CMAN configuration fails\n", __func__);
1288 				return status;
1289 			}
1290 		} else {
1291 			s->cman_enabled = false;
1292 		}
1293 
1294 		/* Scheduling loop detection */
1295 		s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1296 		s->pipe_exhaustion = 0;
1297 
1298 		/* Grinders */
1299 		s->busy_grinders = 0;
1300 
1301 		/* Queue base calculation */
1302 		rte_sched_subport_config_qsize(s);
1303 
1304 		/* Large data structures */
1305 		s->pipe = (struct rte_sched_pipe *)
1306 			(s->memory + rte_sched_subport_get_array_base(params,
1307 			e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1308 		s->queue = (struct rte_sched_queue *)
1309 			(s->memory + rte_sched_subport_get_array_base(params,
1310 			e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1311 		s->queue_extra = (struct rte_sched_queue_extra *)
1312 			(s->memory + rte_sched_subport_get_array_base(params,
1313 			e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1314 		s->pipe_profiles = (struct rte_sched_pipe_profile *)
1315 			(s->memory + rte_sched_subport_get_array_base(params,
1316 			e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1317 		s->bmp_array =  s->memory + rte_sched_subport_get_array_base(
1318 				params, e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1319 		s->queue_array = (struct rte_mbuf **)
1320 			(s->memory + rte_sched_subport_get_array_base(params,
1321 			e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1322 
1323 		/* Pipe profile table */
1324 		rte_sched_subport_config_pipe_profile_table(s, params,
1325 							    port->rate);
1326 
1327 		/* Bitmap */
1328 		n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1329 		bmp_mem_size = rte_bitmap_get_memory_footprint(
1330 						n_subport_pipe_queues);
1331 		s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1332 					bmp_mem_size);
1333 		if (s->bmp == NULL) {
1334 			RTE_LOG(ERR, SCHED,
1335 				"%s: Subport bitmap init error\n", __func__);
1336 			ret = -EINVAL;
1337 			goto out;
1338 		}
1339 
1340 		for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1341 			s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1342 
1343 		/* TC oversubscription */
1344 		s->tc_ov_wm_min = port->mtu;
1345 		s->tc_ov_period_id = 0;
1346 		s->tc_ov = 0;
1347 		s->tc_ov_n = 0;
1348 		s->tc_ov_rate = 0;
1349 	}
1350 
1351 	{
1352 	/* update subport parameters from subport profile table*/
1353 		profile = port->subport_profiles + subport_profile_id;
1354 
1355 		s = port->subports[subport_id];
1356 
1357 		s->tb_credits = profile->tb_size / 2;
1358 
1359 		s->tc_time = port->time + profile->tc_period;
1360 
1361 		for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1362 			if (s->qsize[i])
1363 				s->tc_credits[i] =
1364 					profile->tc_credits_per_period[i];
1365 			else
1366 				profile->tc_credits_per_period[i] = 0;
1367 
1368 		s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(profile->tc_period,
1369 							s->pipe_tc_be_rate_max);
1370 		s->tc_ov_wm = s->tc_ov_wm_max;
1371 		s->profile = subport_profile_id;
1372 
1373 	}
1374 
1375 	rte_sched_port_log_subport_profile(port, subport_profile_id);
1376 
1377 	return 0;
1378 
1379 out:
1380 	rte_sched_free_memory(port, n_subports);
1381 
1382 	return ret;
1383 }
1384 
1385 int
1386 rte_sched_pipe_config(struct rte_sched_port *port,
1387 	uint32_t subport_id,
1388 	uint32_t pipe_id,
1389 	int32_t pipe_profile)
1390 {
1391 	struct rte_sched_subport *s;
1392 	struct rte_sched_subport_profile *sp;
1393 	struct rte_sched_pipe *p;
1394 	struct rte_sched_pipe_profile *params;
1395 	uint32_t n_subports = subport_id + 1;
1396 	uint32_t deactivate, profile, i;
1397 	int ret;
1398 
1399 	/* Check user parameters */
1400 	profile = (uint32_t) pipe_profile;
1401 	deactivate = (pipe_profile < 0);
1402 
1403 	if (port == NULL) {
1404 		RTE_LOG(ERR, SCHED,
1405 			"%s: Incorrect value for parameter port\n", __func__);
1406 		return -EINVAL;
1407 	}
1408 
1409 	if (subport_id >= port->n_subports_per_port) {
1410 		RTE_LOG(ERR, SCHED,
1411 			"%s: Incorrect value for parameter subport id\n", __func__);
1412 		ret = -EINVAL;
1413 		goto out;
1414 	}
1415 
1416 	s = port->subports[subport_id];
1417 	if (pipe_id >= s->n_pipes_per_subport_enabled) {
1418 		RTE_LOG(ERR, SCHED,
1419 			"%s: Incorrect value for parameter pipe id\n", __func__);
1420 		ret = -EINVAL;
1421 		goto out;
1422 	}
1423 
1424 	if (!deactivate && profile >= s->n_pipe_profiles) {
1425 		RTE_LOG(ERR, SCHED,
1426 			"%s: Incorrect value for parameter pipe profile\n", __func__);
1427 		ret = -EINVAL;
1428 		goto out;
1429 	}
1430 
1431 	sp = port->subport_profiles + s->profile;
1432 	/* Handle the case when pipe already has a valid configuration */
1433 	p = s->pipe + pipe_id;
1434 	if (p->tb_time) {
1435 		params = s->pipe_profiles + p->profile;
1436 
1437 		double subport_tc_be_rate =
1438 		(double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1439 			/ (double) sp->tc_period;
1440 		double pipe_tc_be_rate =
1441 			(double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1442 			/ (double) params->tc_period;
1443 		uint32_t tc_be_ov = s->tc_ov;
1444 
1445 		/* Unplug pipe from its subport */
1446 		s->tc_ov_n -= params->tc_ov_weight;
1447 		s->tc_ov_rate -= pipe_tc_be_rate;
1448 		s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1449 
1450 		if (s->tc_ov != tc_be_ov) {
1451 			RTE_LOG(DEBUG, SCHED,
1452 				"Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1453 				subport_id, subport_tc_be_rate, s->tc_ov_rate);
1454 		}
1455 
1456 		/* Reset the pipe */
1457 		memset(p, 0, sizeof(struct rte_sched_pipe));
1458 	}
1459 
1460 	if (deactivate)
1461 		return 0;
1462 
1463 	/* Apply the new pipe configuration */
1464 	p->profile = profile;
1465 	params = s->pipe_profiles + p->profile;
1466 
1467 	/* Token Bucket (TB) */
1468 	p->tb_time = port->time;
1469 	p->tb_credits = params->tb_size / 2;
1470 
1471 	/* Traffic Classes (TCs) */
1472 	p->tc_time = port->time + params->tc_period;
1473 
1474 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1475 		if (s->qsize[i])
1476 			p->tc_credits[i] = params->tc_credits_per_period[i];
1477 
1478 	{
1479 		/* Subport best effort tc oversubscription */
1480 		double subport_tc_be_rate =
1481 		(double)sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1482 			/ (double) sp->tc_period;
1483 		double pipe_tc_be_rate =
1484 			(double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1485 			/ (double) params->tc_period;
1486 		uint32_t tc_be_ov = s->tc_ov;
1487 
1488 		s->tc_ov_n += params->tc_ov_weight;
1489 		s->tc_ov_rate += pipe_tc_be_rate;
1490 		s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1491 
1492 		if (s->tc_ov != tc_be_ov) {
1493 			RTE_LOG(DEBUG, SCHED,
1494 				"Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1495 				subport_id, subport_tc_be_rate, s->tc_ov_rate);
1496 		}
1497 		p->tc_ov_period_id = s->tc_ov_period_id;
1498 		p->tc_ov_credits = s->tc_ov_wm;
1499 	}
1500 
1501 	return 0;
1502 
1503 out:
1504 	rte_sched_free_memory(port, n_subports);
1505 
1506 	return ret;
1507 }
1508 
1509 int
1510 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1511 	uint32_t subport_id,
1512 	struct rte_sched_pipe_params *params,
1513 	uint32_t *pipe_profile_id)
1514 {
1515 	struct rte_sched_subport *s;
1516 	struct rte_sched_pipe_profile *pp;
1517 	uint32_t i;
1518 	int status;
1519 
1520 	/* Port */
1521 	if (port == NULL) {
1522 		RTE_LOG(ERR, SCHED,
1523 			"%s: Incorrect value for parameter port\n", __func__);
1524 		return -EINVAL;
1525 	}
1526 
1527 	/* Subport id not exceeds the max limit */
1528 	if (subport_id > port->n_subports_per_port) {
1529 		RTE_LOG(ERR, SCHED,
1530 			"%s: Incorrect value for subport id\n", __func__);
1531 		return -EINVAL;
1532 	}
1533 
1534 	s = port->subports[subport_id];
1535 
1536 	/* Pipe profiles exceeds the max limit */
1537 	if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1538 		RTE_LOG(ERR, SCHED,
1539 			"%s: Number of pipe profiles exceeds the max limit\n", __func__);
1540 		return -EINVAL;
1541 	}
1542 
1543 	/* Pipe params */
1544 	status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1545 	if (status != 0) {
1546 		RTE_LOG(ERR, SCHED,
1547 			"%s: Pipe profile check failed(%d)\n", __func__, status);
1548 		return -EINVAL;
1549 	}
1550 
1551 	pp = &s->pipe_profiles[s->n_pipe_profiles];
1552 	rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1553 
1554 	/* Pipe profile should not exists */
1555 	for (i = 0; i < s->n_pipe_profiles; i++)
1556 		if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1557 			RTE_LOG(ERR, SCHED,
1558 				"%s: Pipe profile exists\n", __func__);
1559 			return -EINVAL;
1560 		}
1561 
1562 	/* Pipe profile commit */
1563 	*pipe_profile_id = s->n_pipe_profiles;
1564 	s->n_pipe_profiles++;
1565 
1566 	if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1567 		s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1568 
1569 	rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1570 
1571 	return 0;
1572 }
1573 
1574 int
1575 rte_sched_port_subport_profile_add(struct rte_sched_port *port,
1576 	struct rte_sched_subport_profile_params *params,
1577 	uint32_t *subport_profile_id)
1578 {
1579 	int status;
1580 	uint32_t i;
1581 	struct rte_sched_subport_profile *dst;
1582 
1583 	/* Port */
1584 	if (port == NULL) {
1585 		RTE_LOG(ERR, SCHED, "%s: "
1586 		"Incorrect value for parameter port\n", __func__);
1587 		return -EINVAL;
1588 	}
1589 
1590 	if (params == NULL) {
1591 		RTE_LOG(ERR, SCHED, "%s: "
1592 		"Incorrect value for parameter profile\n", __func__);
1593 		return -EINVAL;
1594 	}
1595 
1596 	if (subport_profile_id == NULL) {
1597 		RTE_LOG(ERR, SCHED, "%s: "
1598 		"Incorrect value for parameter subport_profile_id\n",
1599 		__func__);
1600 		return -EINVAL;
1601 	}
1602 
1603 	dst = port->subport_profiles + port->n_subport_profiles;
1604 
1605 	/* Subport profiles exceeds the max limit */
1606 	if (port->n_subport_profiles >= port->n_max_subport_profiles) {
1607 		RTE_LOG(ERR, SCHED, "%s: "
1608 		"Number of subport profiles exceeds the max limit\n",
1609 		 __func__);
1610 		return -EINVAL;
1611 	}
1612 
1613 	status = subport_profile_check(params, port->rate);
1614 	if (status != 0) {
1615 		RTE_LOG(ERR, SCHED,
1616 		"%s: subport profile check failed(%d)\n", __func__, status);
1617 		return -EINVAL;
1618 	}
1619 
1620 	rte_sched_subport_profile_convert(params, dst, port->rate);
1621 
1622 	/* Subport profile should not exists */
1623 	for (i = 0; i < port->n_subport_profiles; i++)
1624 		if (memcmp(port->subport_profiles + i,
1625 		    dst, sizeof(*dst)) == 0) {
1626 			RTE_LOG(ERR, SCHED,
1627 			"%s: subport profile exists\n", __func__);
1628 			return -EINVAL;
1629 		}
1630 
1631 	/* Subport profile commit */
1632 	*subport_profile_id = port->n_subport_profiles;
1633 	port->n_subport_profiles++;
1634 
1635 	rte_sched_port_log_subport_profile(port, *subport_profile_id);
1636 
1637 	return 0;
1638 }
1639 
1640 static inline uint32_t
1641 rte_sched_port_qindex(struct rte_sched_port *port,
1642 	uint32_t subport,
1643 	uint32_t pipe,
1644 	uint32_t traffic_class,
1645 	uint32_t queue)
1646 {
1647 	return ((subport & (port->n_subports_per_port - 1)) <<
1648 		(port->n_pipes_per_subport_log2 + 4)) |
1649 		((pipe &
1650 		(port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1651 		((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1652 		(RTE_SCHED_QUEUES_PER_PIPE - 1));
1653 }
1654 
1655 void
1656 rte_sched_port_pkt_write(struct rte_sched_port *port,
1657 			 struct rte_mbuf *pkt,
1658 			 uint32_t subport, uint32_t pipe,
1659 			 uint32_t traffic_class,
1660 			 uint32_t queue, enum rte_color color)
1661 {
1662 	uint32_t queue_id =
1663 		rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1664 
1665 	rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1666 }
1667 
1668 void
1669 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1670 				  const struct rte_mbuf *pkt,
1671 				  uint32_t *subport, uint32_t *pipe,
1672 				  uint32_t *traffic_class, uint32_t *queue)
1673 {
1674 	uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1675 
1676 	*subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1677 	*pipe = (queue_id >> 4) &
1678 		(port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1679 	*traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1680 	*queue = rte_sched_port_tc_queue(port, queue_id);
1681 }
1682 
1683 enum rte_color
1684 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1685 {
1686 	return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1687 }
1688 
1689 int
1690 rte_sched_subport_read_stats(struct rte_sched_port *port,
1691 			     uint32_t subport_id,
1692 			     struct rte_sched_subport_stats *stats,
1693 			     uint32_t *tc_ov)
1694 {
1695 	struct rte_sched_subport *s;
1696 
1697 	/* Check user parameters */
1698 	if (port == NULL) {
1699 		RTE_LOG(ERR, SCHED,
1700 			"%s: Incorrect value for parameter port\n", __func__);
1701 		return -EINVAL;
1702 	}
1703 
1704 	if (subport_id >= port->n_subports_per_port) {
1705 		RTE_LOG(ERR, SCHED,
1706 			"%s: Incorrect value for subport id\n", __func__);
1707 		return -EINVAL;
1708 	}
1709 
1710 	if (stats == NULL) {
1711 		RTE_LOG(ERR, SCHED,
1712 			"%s: Incorrect value for parameter stats\n", __func__);
1713 		return -EINVAL;
1714 	}
1715 
1716 	if (tc_ov == NULL) {
1717 		RTE_LOG(ERR, SCHED,
1718 			"%s: Incorrect value for tc_ov\n", __func__);
1719 		return -EINVAL;
1720 	}
1721 
1722 	s = port->subports[subport_id];
1723 
1724 	/* Copy subport stats and clear */
1725 	memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1726 	memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1727 
1728 	/* Subport TC oversubscription status */
1729 	*tc_ov = s->tc_ov;
1730 
1731 	return 0;
1732 }
1733 
1734 int
1735 rte_sched_queue_read_stats(struct rte_sched_port *port,
1736 	uint32_t queue_id,
1737 	struct rte_sched_queue_stats *stats,
1738 	uint16_t *qlen)
1739 {
1740 	struct rte_sched_subport *s;
1741 	struct rte_sched_queue *q;
1742 	struct rte_sched_queue_extra *qe;
1743 	uint32_t subport_id, subport_qmask, subport_qindex;
1744 
1745 	/* Check user parameters */
1746 	if (port == NULL) {
1747 		RTE_LOG(ERR, SCHED,
1748 			"%s: Incorrect value for parameter port\n", __func__);
1749 		return -EINVAL;
1750 	}
1751 
1752 	if (queue_id >= rte_sched_port_queues_per_port(port)) {
1753 		RTE_LOG(ERR, SCHED,
1754 			"%s: Incorrect value for queue id\n", __func__);
1755 		return -EINVAL;
1756 	}
1757 
1758 	if (stats == NULL) {
1759 		RTE_LOG(ERR, SCHED,
1760 			"%s: Incorrect value for parameter stats\n", __func__);
1761 		return -EINVAL;
1762 	}
1763 
1764 	if (qlen == NULL) {
1765 		RTE_LOG(ERR, SCHED,
1766 			"%s: Incorrect value for parameter qlen\n", __func__);
1767 		return -EINVAL;
1768 	}
1769 	subport_qmask = port->n_pipes_per_subport_log2 + 4;
1770 	subport_id = (queue_id >> subport_qmask) & (port->n_subports_per_port - 1);
1771 
1772 	s = port->subports[subport_id];
1773 	subport_qindex = ((1 << subport_qmask) - 1) & queue_id;
1774 	q = s->queue + subport_qindex;
1775 	qe = s->queue_extra + subport_qindex;
1776 
1777 	/* Copy queue stats and clear */
1778 	memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1779 	memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1780 
1781 	/* Queue length */
1782 	*qlen = q->qw - q->qr;
1783 
1784 	return 0;
1785 }
1786 
1787 #ifdef RTE_SCHED_DEBUG
1788 
1789 static inline int
1790 rte_sched_port_queue_is_empty(struct rte_sched_subport *subport,
1791 	uint32_t qindex)
1792 {
1793 	struct rte_sched_queue *queue = subport->queue + qindex;
1794 
1795 	return queue->qr == queue->qw;
1796 }
1797 
1798 #endif /* RTE_SCHED_DEBUG */
1799 
1800 static inline void
1801 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1802 	struct rte_sched_subport *subport,
1803 	uint32_t qindex,
1804 	struct rte_mbuf *pkt)
1805 {
1806 	uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1807 	uint32_t pkt_len = pkt->pkt_len;
1808 
1809 	subport->stats.n_pkts_tc[tc_index] += 1;
1810 	subport->stats.n_bytes_tc[tc_index] += pkt_len;
1811 }
1812 
1813 static inline void
1814 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1815 	struct rte_sched_subport *subport,
1816 	uint32_t qindex,
1817 	struct rte_mbuf *pkt,
1818 	uint32_t n_pkts_cman_dropped)
1819 {
1820 	uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1821 	uint32_t pkt_len = pkt->pkt_len;
1822 
1823 	subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1824 	subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1825 	subport->stats.n_pkts_cman_dropped[tc_index] += n_pkts_cman_dropped;
1826 }
1827 
1828 static inline void
1829 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1830 	uint32_t qindex,
1831 	struct rte_mbuf *pkt)
1832 {
1833 	struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1834 	uint32_t pkt_len = pkt->pkt_len;
1835 
1836 	qe->stats.n_pkts += 1;
1837 	qe->stats.n_bytes += pkt_len;
1838 }
1839 
1840 static inline void
1841 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1842 	uint32_t qindex,
1843 	struct rte_mbuf *pkt,
1844 	uint32_t n_pkts_cman_dropped)
1845 {
1846 	struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1847 	uint32_t pkt_len = pkt->pkt_len;
1848 
1849 	qe->stats.n_pkts_dropped += 1;
1850 	qe->stats.n_bytes_dropped += pkt_len;
1851 	if (subport->cman_enabled)
1852 		qe->stats.n_pkts_cman_dropped += n_pkts_cman_dropped;
1853 }
1854 
1855 static inline int
1856 rte_sched_port_cman_drop(struct rte_sched_port *port,
1857 	struct rte_sched_subport *subport,
1858 	struct rte_mbuf *pkt,
1859 	uint32_t qindex,
1860 	uint16_t qlen)
1861 {
1862 	if (!subport->cman_enabled)
1863 		return 0;
1864 
1865 	struct rte_sched_queue_extra *qe;
1866 	uint32_t tc_index;
1867 
1868 	tc_index = rte_sched_port_pipe_tc(port, qindex);
1869 	qe = subport->queue_extra + qindex;
1870 
1871 	/* RED */
1872 	if (subport->cman == RTE_SCHED_CMAN_RED) {
1873 		struct rte_red_config *red_cfg;
1874 		struct rte_red *red;
1875 		enum rte_color color;
1876 
1877 		color = rte_sched_port_pkt_read_color(pkt);
1878 		red_cfg = &subport->red_config[tc_index][color];
1879 
1880 		if ((red_cfg->min_th | red_cfg->max_th) == 0)
1881 			return 0;
1882 
1883 		red = &qe->red;
1884 
1885 		return rte_red_enqueue(red_cfg, red, qlen, port->time);
1886 	}
1887 
1888 	/* PIE */
1889 	struct rte_pie_config *pie_cfg = &subport->pie_config[tc_index];
1890 	struct rte_pie *pie = &qe->pie;
1891 
1892 	return rte_pie_enqueue(pie_cfg, pie, qlen, pkt->pkt_len, port->time_cpu_cycles);
1893 }
1894 
1895 static inline void
1896 rte_sched_port_red_set_queue_empty_timestamp(struct rte_sched_port *port,
1897 	struct rte_sched_subport *subport, uint32_t qindex)
1898 {
1899 	if (subport->cman_enabled && subport->cman == RTE_SCHED_CMAN_RED) {
1900 		struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1901 		struct rte_red *red = &qe->red;
1902 
1903 		rte_red_mark_queue_empty(red, port->time);
1904 	}
1905 }
1906 
1907 static inline void
1908 rte_sched_port_pie_dequeue(struct rte_sched_subport *subport,
1909 uint32_t qindex, uint32_t pkt_len, uint64_t time) {
1910 	if (subport->cman_enabled && subport->cman == RTE_SCHED_CMAN_PIE) {
1911 		struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1912 		struct rte_pie *pie = &qe->pie;
1913 
1914 		/* Update queue length */
1915 		pie->qlen -= 1;
1916 		pie->qlen_bytes -= pkt_len;
1917 
1918 		rte_pie_dequeue(pie, pkt_len, time);
1919 	}
1920 }
1921 
1922 #ifdef RTE_SCHED_DEBUG
1923 
1924 static inline void
1925 debug_check_queue_slab(struct rte_sched_subport *subport, uint32_t bmp_pos,
1926 		       uint64_t bmp_slab)
1927 {
1928 	uint64_t mask;
1929 	uint32_t i, panic;
1930 
1931 	if (bmp_slab == 0)
1932 		rte_panic("Empty slab at position %u\n", bmp_pos);
1933 
1934 	panic = 0;
1935 	for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1936 		if (mask & bmp_slab) {
1937 			if (rte_sched_port_queue_is_empty(subport, bmp_pos + i)) {
1938 				printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1939 				panic = 1;
1940 			}
1941 		}
1942 	}
1943 
1944 	if (panic)
1945 		rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1946 			bmp_slab, bmp_pos);
1947 }
1948 
1949 #endif /* RTE_SCHED_DEBUG */
1950 
1951 static inline struct rte_sched_subport *
1952 rte_sched_port_subport(struct rte_sched_port *port,
1953 	struct rte_mbuf *pkt)
1954 {
1955 	uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1956 	uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1957 
1958 	return port->subports[subport_id];
1959 }
1960 
1961 static inline uint32_t
1962 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1963 	struct rte_mbuf *pkt, uint32_t subport_qmask)
1964 {
1965 	struct rte_sched_queue *q;
1966 	struct rte_sched_queue_extra *qe;
1967 	uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1968 	uint32_t subport_queue_id = subport_qmask & qindex;
1969 
1970 	q = subport->queue + subport_queue_id;
1971 	rte_prefetch0(q);
1972 	qe = subport->queue_extra + subport_queue_id;
1973 	rte_prefetch0(qe);
1974 
1975 	return subport_queue_id;
1976 }
1977 
1978 static inline void
1979 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1980 	struct rte_sched_subport *subport,
1981 	uint32_t qindex,
1982 	struct rte_mbuf **qbase)
1983 {
1984 	struct rte_sched_queue *q;
1985 	struct rte_mbuf **q_qw;
1986 	uint16_t qsize;
1987 
1988 	q = subport->queue + qindex;
1989 	qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1990 	q_qw = qbase + (q->qw & (qsize - 1));
1991 
1992 	rte_prefetch0(q_qw);
1993 	rte_bitmap_prefetch0(subport->bmp, qindex);
1994 }
1995 
1996 static inline int
1997 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
1998 	struct rte_sched_subport *subport,
1999 	uint32_t qindex,
2000 	struct rte_mbuf **qbase,
2001 	struct rte_mbuf *pkt)
2002 {
2003 	struct rte_sched_queue *q;
2004 	uint16_t qsize;
2005 	uint16_t qlen;
2006 
2007 	q = subport->queue + qindex;
2008 	qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2009 	qlen = q->qw - q->qr;
2010 
2011 	/* Drop the packet (and update drop stats) when queue is full */
2012 	if (unlikely(rte_sched_port_cman_drop(port, subport, pkt, qindex, qlen) ||
2013 		     (qlen >= qsize))) {
2014 		rte_pktmbuf_free(pkt);
2015 		rte_sched_port_update_subport_stats_on_drop(port, subport,
2016 			qindex, pkt, qlen < qsize);
2017 		rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
2018 			qlen < qsize);
2019 		return 0;
2020 	}
2021 
2022 	/* Enqueue packet */
2023 	qbase[q->qw & (qsize - 1)] = pkt;
2024 	q->qw++;
2025 
2026 	/* Activate queue in the subport bitmap */
2027 	rte_bitmap_set(subport->bmp, qindex);
2028 
2029 	/* Statistics */
2030 	rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
2031 	rte_sched_port_update_queue_stats(subport, qindex, pkt);
2032 
2033 	return 1;
2034 }
2035 
2036 
2037 /*
2038  * The enqueue function implements a 4-level pipeline with each stage
2039  * processing two different packets. The purpose of using a pipeline
2040  * is to hide the latency of prefetching the data structures. The
2041  * naming convention is presented in the diagram below:
2042  *
2043  *   p00  _______   p10  _______   p20  _______   p30  _______
2044  * ----->|       |----->|       |----->|       |----->|       |----->
2045  *       |   0   |      |   1   |      |   2   |      |   3   |
2046  * ----->|_______|----->|_______|----->|_______|----->|_______|----->
2047  *   p01            p11            p21            p31
2048  *
2049  */
2050 int
2051 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
2052 		       uint32_t n_pkts)
2053 {
2054 	struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
2055 		*pkt30, *pkt31, *pkt_last;
2056 	struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
2057 		**q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
2058 	struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
2059 		*subport20, *subport21, *subport30, *subport31, *subport_last;
2060 	uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
2061 	uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
2062 	uint32_t subport_qmask;
2063 	uint32_t result, i;
2064 
2065 	result = 0;
2066 	subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
2067 
2068 	/*
2069 	 * Less then 6 input packets available, which is not enough to
2070 	 * feed the pipeline
2071 	 */
2072 	if (unlikely(n_pkts < 6)) {
2073 		struct rte_sched_subport *subports[5];
2074 		struct rte_mbuf **q_base[5];
2075 		uint32_t q[5];
2076 
2077 		/* Prefetch the mbuf structure of each packet */
2078 		for (i = 0; i < n_pkts; i++)
2079 			rte_prefetch0(pkts[i]);
2080 
2081 		/* Prefetch the subport structure for each packet */
2082 		for (i = 0; i < n_pkts; i++)
2083 			subports[i] = rte_sched_port_subport(port, pkts[i]);
2084 
2085 		/* Prefetch the queue structure for each queue */
2086 		for (i = 0; i < n_pkts; i++)
2087 			q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
2088 					pkts[i], subport_qmask);
2089 
2090 		/* Prefetch the write pointer location of each queue */
2091 		for (i = 0; i < n_pkts; i++) {
2092 			q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
2093 			rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
2094 				q[i], q_base[i]);
2095 		}
2096 
2097 		/* Write each packet to its queue */
2098 		for (i = 0; i < n_pkts; i++)
2099 			result += rte_sched_port_enqueue_qwa(port, subports[i],
2100 						q[i], q_base[i], pkts[i]);
2101 
2102 		return result;
2103 	}
2104 
2105 	/* Feed the first 3 stages of the pipeline (6 packets needed) */
2106 	pkt20 = pkts[0];
2107 	pkt21 = pkts[1];
2108 	rte_prefetch0(pkt20);
2109 	rte_prefetch0(pkt21);
2110 
2111 	pkt10 = pkts[2];
2112 	pkt11 = pkts[3];
2113 	rte_prefetch0(pkt10);
2114 	rte_prefetch0(pkt11);
2115 
2116 	subport20 = rte_sched_port_subport(port, pkt20);
2117 	subport21 = rte_sched_port_subport(port, pkt21);
2118 	q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
2119 			pkt20, subport_qmask);
2120 	q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
2121 			pkt21, subport_qmask);
2122 
2123 	pkt00 = pkts[4];
2124 	pkt01 = pkts[5];
2125 	rte_prefetch0(pkt00);
2126 	rte_prefetch0(pkt01);
2127 
2128 	subport10 = rte_sched_port_subport(port, pkt10);
2129 	subport11 = rte_sched_port_subport(port, pkt11);
2130 	q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2131 			pkt10, subport_qmask);
2132 	q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2133 			pkt11, subport_qmask);
2134 
2135 	q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2136 	q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2137 	rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2138 	rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2139 
2140 	/* Run the pipeline */
2141 	for (i = 6; i < (n_pkts & (~1)); i += 2) {
2142 		/* Propagate stage inputs */
2143 		pkt30 = pkt20;
2144 		pkt31 = pkt21;
2145 		pkt20 = pkt10;
2146 		pkt21 = pkt11;
2147 		pkt10 = pkt00;
2148 		pkt11 = pkt01;
2149 		q30 = q20;
2150 		q31 = q21;
2151 		q20 = q10;
2152 		q21 = q11;
2153 		subport30 = subport20;
2154 		subport31 = subport21;
2155 		subport20 = subport10;
2156 		subport21 = subport11;
2157 		q30_base = q20_base;
2158 		q31_base = q21_base;
2159 
2160 		/* Stage 0: Get packets in */
2161 		pkt00 = pkts[i];
2162 		pkt01 = pkts[i + 1];
2163 		rte_prefetch0(pkt00);
2164 		rte_prefetch0(pkt01);
2165 
2166 		/* Stage 1: Prefetch subport and queue structure storing queue pointers */
2167 		subport10 = rte_sched_port_subport(port, pkt10);
2168 		subport11 = rte_sched_port_subport(port, pkt11);
2169 		q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
2170 				pkt10, subport_qmask);
2171 		q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
2172 				pkt11, subport_qmask);
2173 
2174 		/* Stage 2: Prefetch queue write location */
2175 		q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
2176 		q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
2177 		rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
2178 		rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
2179 
2180 		/* Stage 3: Write packet to queue and activate queue */
2181 		r30 = rte_sched_port_enqueue_qwa(port, subport30,
2182 				q30, q30_base, pkt30);
2183 		r31 = rte_sched_port_enqueue_qwa(port, subport31,
2184 				q31, q31_base, pkt31);
2185 		result += r30 + r31;
2186 	}
2187 
2188 	/*
2189 	 * Drain the pipeline (exactly 6 packets).
2190 	 * Handle the last packet in the case
2191 	 * of an odd number of input packets.
2192 	 */
2193 	pkt_last = pkts[n_pkts - 1];
2194 	rte_prefetch0(pkt_last);
2195 
2196 	subport00 = rte_sched_port_subport(port, pkt00);
2197 	subport01 = rte_sched_port_subport(port, pkt01);
2198 	q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
2199 			pkt00, subport_qmask);
2200 	q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
2201 			pkt01, subport_qmask);
2202 
2203 	q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
2204 	q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
2205 	rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
2206 	rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
2207 
2208 	r20 = rte_sched_port_enqueue_qwa(port, subport20,
2209 			q20, q20_base, pkt20);
2210 	r21 = rte_sched_port_enqueue_qwa(port, subport21,
2211 			q21, q21_base, pkt21);
2212 	result += r20 + r21;
2213 
2214 	subport_last = rte_sched_port_subport(port, pkt_last);
2215 	q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
2216 				pkt_last, subport_qmask);
2217 
2218 	q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
2219 	q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
2220 	rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
2221 	rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
2222 
2223 	r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
2224 			q10_base, pkt10);
2225 	r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
2226 			q11_base, pkt11);
2227 	result += r10 + r11;
2228 
2229 	q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
2230 	rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
2231 		q_last, q_last_base);
2232 
2233 	r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
2234 			q00_base, pkt00);
2235 	r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
2236 			q01_base, pkt01);
2237 	result += r00 + r01;
2238 
2239 	if (n_pkts & 1) {
2240 		r_last = rte_sched_port_enqueue_qwa(port, subport_last,
2241 					q_last,	q_last_base, pkt_last);
2242 		result += r_last;
2243 	}
2244 
2245 	return result;
2246 }
2247 
2248 static inline uint64_t
2249 grinder_tc_ov_credits_update(struct rte_sched_port *port,
2250 	struct rte_sched_subport *subport, uint32_t pos)
2251 {
2252 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2253 	struct rte_sched_subport_profile *sp = grinder->subport_params;
2254 	uint64_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2255 	uint64_t tc_consumption = 0, tc_ov_consumption_max;
2256 	uint64_t tc_ov_wm = subport->tc_ov_wm;
2257 	uint32_t i;
2258 
2259 	if (subport->tc_ov == 0)
2260 		return subport->tc_ov_wm_max;
2261 
2262 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2263 		tc_ov_consumption[i] = sp->tc_credits_per_period[i]
2264 					-  subport->tc_credits[i];
2265 		tc_consumption += tc_ov_consumption[i];
2266 	}
2267 
2268 	tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2269 	sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2270 		subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2271 
2272 	tc_ov_consumption_max =
2273 	sp->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2274 			tc_consumption;
2275 
2276 	if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2277 		(tc_ov_consumption_max - port->mtu)) {
2278 		tc_ov_wm  -= tc_ov_wm >> 7;
2279 		if (tc_ov_wm < subport->tc_ov_wm_min)
2280 			tc_ov_wm = subport->tc_ov_wm_min;
2281 
2282 		return tc_ov_wm;
2283 	}
2284 
2285 	tc_ov_wm += (tc_ov_wm >> 7) + 1;
2286 	if (tc_ov_wm > subport->tc_ov_wm_max)
2287 		tc_ov_wm = subport->tc_ov_wm_max;
2288 
2289 	return tc_ov_wm;
2290 }
2291 
2292 static inline void
2293 grinder_credits_update(struct rte_sched_port *port,
2294 	struct rte_sched_subport *subport, uint32_t pos)
2295 {
2296 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2297 	struct rte_sched_pipe *pipe = grinder->pipe;
2298 	struct rte_sched_pipe_profile *params = grinder->pipe_params;
2299 	struct rte_sched_subport_profile *sp = grinder->subport_params;
2300 	uint64_t n_periods;
2301 	uint32_t i;
2302 
2303 	/* Subport TB */
2304 	n_periods = (port->time - subport->tb_time) / sp->tb_period;
2305 	subport->tb_credits += n_periods * sp->tb_credits_per_period;
2306 	subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2307 	subport->tb_time += n_periods * sp->tb_period;
2308 
2309 	/* Pipe TB */
2310 	n_periods = (port->time - pipe->tb_time) / params->tb_period;
2311 	pipe->tb_credits += n_periods * params->tb_credits_per_period;
2312 	pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2313 	pipe->tb_time += n_periods * params->tb_period;
2314 
2315 	/* Subport TCs */
2316 	if (unlikely(port->time >= subport->tc_time)) {
2317 		for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2318 			subport->tc_credits[i] = sp->tc_credits_per_period[i];
2319 
2320 		subport->tc_time = port->time + sp->tc_period;
2321 	}
2322 
2323 	/* Pipe TCs */
2324 	if (unlikely(port->time >= pipe->tc_time)) {
2325 		for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2326 			pipe->tc_credits[i] = params->tc_credits_per_period[i];
2327 		pipe->tc_time = port->time + params->tc_period;
2328 	}
2329 }
2330 
2331 static inline void
2332 grinder_credits_update_with_tc_ov(struct rte_sched_port *port,
2333 	struct rte_sched_subport *subport, uint32_t pos)
2334 {
2335 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2336 	struct rte_sched_pipe *pipe = grinder->pipe;
2337 	struct rte_sched_pipe_profile *params = grinder->pipe_params;
2338 	struct rte_sched_subport_profile *sp = grinder->subport_params;
2339 	uint64_t n_periods;
2340 	uint32_t i;
2341 
2342 	/* Subport TB */
2343 	n_periods = (port->time - subport->tb_time) / sp->tb_period;
2344 	subport->tb_credits += n_periods * sp->tb_credits_per_period;
2345 	subport->tb_credits = RTE_MIN(subport->tb_credits, sp->tb_size);
2346 	subport->tb_time += n_periods * sp->tb_period;
2347 
2348 	/* Pipe TB */
2349 	n_periods = (port->time - pipe->tb_time) / params->tb_period;
2350 	pipe->tb_credits += n_periods * params->tb_credits_per_period;
2351 	pipe->tb_credits = RTE_MIN(pipe->tb_credits, params->tb_size);
2352 	pipe->tb_time += n_periods * params->tb_period;
2353 
2354 	/* Subport TCs */
2355 	if (unlikely(port->time >= subport->tc_time)) {
2356 		subport->tc_ov_wm =
2357 			grinder_tc_ov_credits_update(port, subport, pos);
2358 
2359 		for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2360 			subport->tc_credits[i] = sp->tc_credits_per_period[i];
2361 
2362 		subport->tc_time = port->time + sp->tc_period;
2363 		subport->tc_ov_period_id++;
2364 	}
2365 
2366 	/* Pipe TCs */
2367 	if (unlikely(port->time >= pipe->tc_time)) {
2368 		for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2369 			pipe->tc_credits[i] = params->tc_credits_per_period[i];
2370 		pipe->tc_time = port->time + params->tc_period;
2371 	}
2372 
2373 	/* Pipe TCs - Oversubscription */
2374 	if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2375 		pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2376 
2377 		pipe->tc_ov_period_id = subport->tc_ov_period_id;
2378 	}
2379 }
2380 
2381 static inline int
2382 grinder_credits_check(struct rte_sched_port *port,
2383 	struct rte_sched_subport *subport, uint32_t pos)
2384 {
2385 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2386 	struct rte_sched_pipe *pipe = grinder->pipe;
2387 	struct rte_mbuf *pkt = grinder->pkt;
2388 	uint32_t tc_index = grinder->tc_index;
2389 	uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2390 	uint64_t subport_tb_credits = subport->tb_credits;
2391 	uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2392 	uint64_t pipe_tb_credits = pipe->tb_credits;
2393 	uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2394 	int enough_credits;
2395 
2396 	/* Check pipe and subport credits */
2397 	enough_credits = (pkt_len <= subport_tb_credits) &&
2398 		(pkt_len <= subport_tc_credits) &&
2399 		(pkt_len <= pipe_tb_credits) &&
2400 		(pkt_len <= pipe_tc_credits);
2401 
2402 	if (!enough_credits)
2403 		return 0;
2404 
2405 	/* Update pipe and subport credits */
2406 	subport->tb_credits -= pkt_len;
2407 	subport->tc_credits[tc_index] -= pkt_len;
2408 	pipe->tb_credits -= pkt_len;
2409 	pipe->tc_credits[tc_index] -= pkt_len;
2410 
2411 	return 1;
2412 }
2413 
2414 static inline int
2415 grinder_credits_check_with_tc_ov(struct rte_sched_port *port,
2416 	struct rte_sched_subport *subport, uint32_t pos)
2417 {
2418 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2419 	struct rte_sched_pipe *pipe = grinder->pipe;
2420 	struct rte_mbuf *pkt = grinder->pkt;
2421 	uint32_t tc_index = grinder->tc_index;
2422 	uint64_t pkt_len = pkt->pkt_len + port->frame_overhead;
2423 	uint64_t subport_tb_credits = subport->tb_credits;
2424 	uint64_t subport_tc_credits = subport->tc_credits[tc_index];
2425 	uint64_t pipe_tb_credits = pipe->tb_credits;
2426 	uint64_t pipe_tc_credits = pipe->tc_credits[tc_index];
2427 	uint64_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2428 	uint64_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2429 	uint64_t pipe_tc_ov_credits;
2430 	uint32_t i;
2431 	int enough_credits;
2432 
2433 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2434 		pipe_tc_ov_mask1[i] = ~0LLU;
2435 
2436 	pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2437 	pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = ~0LLU;
2438 	pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2439 
2440 	/* Check pipe and subport credits */
2441 	enough_credits = (pkt_len <= subport_tb_credits) &&
2442 		(pkt_len <= subport_tc_credits) &&
2443 		(pkt_len <= pipe_tb_credits) &&
2444 		(pkt_len <= pipe_tc_credits) &&
2445 		(pkt_len <= pipe_tc_ov_credits);
2446 
2447 	if (!enough_credits)
2448 		return 0;
2449 
2450 	/* Update pipe and subport credits */
2451 	subport->tb_credits -= pkt_len;
2452 	subport->tc_credits[tc_index] -= pkt_len;
2453 	pipe->tb_credits -= pkt_len;
2454 	pipe->tc_credits[tc_index] -= pkt_len;
2455 	pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2456 
2457 	return 1;
2458 }
2459 
2460 
2461 static inline int
2462 grinder_schedule(struct rte_sched_port *port,
2463 	struct rte_sched_subport *subport, uint32_t pos)
2464 {
2465 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2466 	struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2467 	uint32_t qindex = grinder->qindex[grinder->qpos];
2468 	struct rte_mbuf *pkt = grinder->pkt;
2469 	uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2470 	uint32_t be_tc_active;
2471 
2472 	if (subport->tc_ov_enabled) {
2473 		if (!grinder_credits_check_with_tc_ov(port, subport, pos))
2474 			return 0;
2475 	} else {
2476 		if (!grinder_credits_check(port, subport, pos))
2477 			return 0;
2478 	}
2479 
2480 	/* Advance port time */
2481 	port->time += pkt_len;
2482 
2483 	/* Send packet */
2484 	port->pkts_out[port->n_pkts_out++] = pkt;
2485 	queue->qr++;
2486 
2487 	be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2488 	grinder->wrr_tokens[grinder->qpos] +=
2489 		(pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2490 
2491 	if (queue->qr == queue->qw) {
2492 		rte_bitmap_clear(subport->bmp, qindex);
2493 		grinder->qmask &= ~(1 << grinder->qpos);
2494 		if (be_tc_active)
2495 			grinder->wrr_mask[grinder->qpos] = 0;
2496 
2497 		rte_sched_port_red_set_queue_empty_timestamp(port, subport, qindex);
2498 	}
2499 
2500 	rte_sched_port_pie_dequeue(subport, qindex, pkt_len, port->time_cpu_cycles);
2501 
2502 	/* Reset pipe loop detection */
2503 	subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2504 	grinder->productive = 1;
2505 
2506 	return 1;
2507 }
2508 
2509 static inline int
2510 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2511 {
2512 	uint32_t i;
2513 
2514 	for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2515 		if (subport->grinder_base_bmp_pos[i] == base_pipe)
2516 			return 1;
2517 	}
2518 
2519 	return 0;
2520 }
2521 
2522 static inline void
2523 grinder_pcache_populate(struct rte_sched_subport *subport,
2524 	uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2525 {
2526 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2527 	uint16_t w[4];
2528 
2529 	grinder->pcache_w = 0;
2530 	grinder->pcache_r = 0;
2531 
2532 	w[0] = (uint16_t) bmp_slab;
2533 	w[1] = (uint16_t) (bmp_slab >> 16);
2534 	w[2] = (uint16_t) (bmp_slab >> 32);
2535 	w[3] = (uint16_t) (bmp_slab >> 48);
2536 
2537 	grinder->pcache_qmask[grinder->pcache_w] = w[0];
2538 	grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2539 	grinder->pcache_w += (w[0] != 0);
2540 
2541 	grinder->pcache_qmask[grinder->pcache_w] = w[1];
2542 	grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2543 	grinder->pcache_w += (w[1] != 0);
2544 
2545 	grinder->pcache_qmask[grinder->pcache_w] = w[2];
2546 	grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2547 	grinder->pcache_w += (w[2] != 0);
2548 
2549 	grinder->pcache_qmask[grinder->pcache_w] = w[3];
2550 	grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2551 	grinder->pcache_w += (w[3] != 0);
2552 }
2553 
2554 static inline void
2555 grinder_tccache_populate(struct rte_sched_subport *subport,
2556 	uint32_t pos, uint32_t qindex, uint16_t qmask)
2557 {
2558 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2559 	uint8_t b, i;
2560 
2561 	grinder->tccache_w = 0;
2562 	grinder->tccache_r = 0;
2563 
2564 	for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2565 		b = (uint8_t) ((qmask >> i) & 0x1);
2566 		grinder->tccache_qmask[grinder->tccache_w] = b;
2567 		grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2568 		grinder->tccache_w += (b != 0);
2569 	}
2570 
2571 	b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2572 	grinder->tccache_qmask[grinder->tccache_w] = b;
2573 	grinder->tccache_qindex[grinder->tccache_w] = qindex +
2574 		RTE_SCHED_TRAFFIC_CLASS_BE;
2575 	grinder->tccache_w += (b != 0);
2576 }
2577 
2578 static inline int
2579 grinder_next_tc(struct rte_sched_port *port,
2580 	struct rte_sched_subport *subport, uint32_t pos)
2581 {
2582 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2583 	struct rte_mbuf **qbase;
2584 	uint32_t qindex;
2585 	uint16_t qsize;
2586 
2587 	if (grinder->tccache_r == grinder->tccache_w)
2588 		return 0;
2589 
2590 	qindex = grinder->tccache_qindex[grinder->tccache_r];
2591 	qbase = rte_sched_subport_pipe_qbase(subport, qindex);
2592 	qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2593 
2594 	grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2595 	grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2596 	grinder->qsize = qsize;
2597 
2598 	if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2599 		grinder->queue[0] = subport->queue + qindex;
2600 		grinder->qbase[0] = qbase;
2601 		grinder->qindex[0] = qindex;
2602 		grinder->tccache_r++;
2603 
2604 		return 1;
2605 	}
2606 
2607 	grinder->queue[0] = subport->queue + qindex;
2608 	grinder->queue[1] = subport->queue + qindex + 1;
2609 	grinder->queue[2] = subport->queue + qindex + 2;
2610 	grinder->queue[3] = subport->queue + qindex + 3;
2611 
2612 	grinder->qbase[0] = qbase;
2613 	grinder->qbase[1] = qbase + qsize;
2614 	grinder->qbase[2] = qbase + 2 * qsize;
2615 	grinder->qbase[3] = qbase + 3 * qsize;
2616 
2617 	grinder->qindex[0] = qindex;
2618 	grinder->qindex[1] = qindex + 1;
2619 	grinder->qindex[2] = qindex + 2;
2620 	grinder->qindex[3] = qindex + 3;
2621 
2622 	grinder->tccache_r++;
2623 	return 1;
2624 }
2625 
2626 static inline int
2627 grinder_next_pipe(struct rte_sched_port *port,
2628 	struct rte_sched_subport *subport, uint32_t pos)
2629 {
2630 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2631 	uint32_t pipe_qindex;
2632 	uint16_t pipe_qmask;
2633 
2634 	if (grinder->pcache_r < grinder->pcache_w) {
2635 		pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2636 		pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2637 		grinder->pcache_r++;
2638 	} else {
2639 		uint64_t bmp_slab = 0;
2640 		uint32_t bmp_pos = 0;
2641 
2642 		/* Get another non-empty pipe group */
2643 		if (unlikely(rte_bitmap_scan(subport->bmp, &bmp_pos, &bmp_slab) <= 0))
2644 			return 0;
2645 
2646 #ifdef RTE_SCHED_DEBUG
2647 		debug_check_queue_slab(subport, bmp_pos, bmp_slab);
2648 #endif
2649 
2650 		/* Return if pipe group already in one of the other grinders */
2651 		subport->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2652 		if (unlikely(grinder_pipe_exists(subport, bmp_pos)))
2653 			return 0;
2654 
2655 		subport->grinder_base_bmp_pos[pos] = bmp_pos;
2656 
2657 		/* Install new pipe group into grinder's pipe cache */
2658 		grinder_pcache_populate(subport, pos, bmp_pos, bmp_slab);
2659 
2660 		pipe_qmask = grinder->pcache_qmask[0];
2661 		pipe_qindex = grinder->pcache_qindex[0];
2662 		grinder->pcache_r = 1;
2663 	}
2664 
2665 	/* Install new pipe in the grinder */
2666 	grinder->pindex = pipe_qindex >> 4;
2667 	grinder->subport = subport;
2668 	grinder->pipe = subport->pipe + grinder->pindex;
2669 	grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2670 	grinder->productive = 0;
2671 
2672 	grinder_tccache_populate(subport, pos, pipe_qindex, pipe_qmask);
2673 	grinder_next_tc(port, subport, pos);
2674 
2675 	/* Check for pipe exhaustion */
2676 	if (grinder->pindex == subport->pipe_loop) {
2677 		subport->pipe_exhaustion = 1;
2678 		subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2679 	}
2680 
2681 	return 1;
2682 }
2683 
2684 
2685 static inline void
2686 grinder_wrr_load(struct rte_sched_subport *subport, uint32_t pos)
2687 {
2688 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2689 	struct rte_sched_pipe *pipe = grinder->pipe;
2690 	struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2691 	uint32_t qmask = grinder->qmask;
2692 
2693 	grinder->wrr_tokens[0] =
2694 		((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2695 	grinder->wrr_tokens[1] =
2696 		((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2697 	grinder->wrr_tokens[2] =
2698 		((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2699 	grinder->wrr_tokens[3] =
2700 		((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2701 
2702 	grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2703 	grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2704 	grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2705 	grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2706 
2707 	grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2708 	grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2709 	grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2710 	grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2711 }
2712 
2713 static inline void
2714 grinder_wrr_store(struct rte_sched_subport *subport, uint32_t pos)
2715 {
2716 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2717 	struct rte_sched_pipe *pipe = grinder->pipe;
2718 
2719 	pipe->wrr_tokens[0] =
2720 			(grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2721 				RTE_SCHED_WRR_SHIFT;
2722 	pipe->wrr_tokens[1] =
2723 			(grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2724 				RTE_SCHED_WRR_SHIFT;
2725 	pipe->wrr_tokens[2] =
2726 			(grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2727 				RTE_SCHED_WRR_SHIFT;
2728 	pipe->wrr_tokens[3] =
2729 			(grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2730 				RTE_SCHED_WRR_SHIFT;
2731 }
2732 
2733 static inline void
2734 grinder_wrr(struct rte_sched_subport *subport, uint32_t pos)
2735 {
2736 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2737 	uint16_t wrr_tokens_min;
2738 
2739 	grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2740 	grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2741 	grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2742 	grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2743 
2744 	grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2745 	wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2746 
2747 	grinder->wrr_tokens[0] -= wrr_tokens_min;
2748 	grinder->wrr_tokens[1] -= wrr_tokens_min;
2749 	grinder->wrr_tokens[2] -= wrr_tokens_min;
2750 	grinder->wrr_tokens[3] -= wrr_tokens_min;
2751 }
2752 
2753 
2754 #define grinder_evict(subport, pos)
2755 
2756 static inline void
2757 grinder_prefetch_pipe(struct rte_sched_subport *subport, uint32_t pos)
2758 {
2759 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2760 
2761 	rte_prefetch0(grinder->pipe);
2762 	rte_prefetch0(grinder->queue[0]);
2763 }
2764 
2765 static inline void
2766 grinder_prefetch_tc_queue_arrays(struct rte_sched_subport *subport, uint32_t pos)
2767 {
2768 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2769 	uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2770 
2771 	qsize = grinder->qsize;
2772 	grinder->qpos = 0;
2773 
2774 	if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2775 		qr[0] = grinder->queue[0]->qr & (qsize - 1);
2776 
2777 		rte_prefetch0(grinder->qbase[0] + qr[0]);
2778 		return;
2779 	}
2780 
2781 	qr[0] = grinder->queue[0]->qr & (qsize - 1);
2782 	qr[1] = grinder->queue[1]->qr & (qsize - 1);
2783 	qr[2] = grinder->queue[2]->qr & (qsize - 1);
2784 	qr[3] = grinder->queue[3]->qr & (qsize - 1);
2785 
2786 	rte_prefetch0(grinder->qbase[0] + qr[0]);
2787 	rte_prefetch0(grinder->qbase[1] + qr[1]);
2788 
2789 	grinder_wrr_load(subport, pos);
2790 	grinder_wrr(subport, pos);
2791 
2792 	rte_prefetch0(grinder->qbase[2] + qr[2]);
2793 	rte_prefetch0(grinder->qbase[3] + qr[3]);
2794 }
2795 
2796 static inline void
2797 grinder_prefetch_mbuf(struct rte_sched_subport *subport, uint32_t pos)
2798 {
2799 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2800 	uint32_t qpos = grinder->qpos;
2801 	struct rte_mbuf **qbase = grinder->qbase[qpos];
2802 	uint16_t qsize = grinder->qsize;
2803 	uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2804 
2805 	grinder->pkt = qbase[qr];
2806 	rte_prefetch0(grinder->pkt);
2807 
2808 	if (unlikely((qr & 0x7) == 7)) {
2809 		uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2810 
2811 		rte_prefetch0(qbase + qr_next);
2812 	}
2813 }
2814 
2815 static inline uint32_t
2816 grinder_handle(struct rte_sched_port *port,
2817 	struct rte_sched_subport *subport, uint32_t pos)
2818 {
2819 	struct rte_sched_grinder *grinder = subport->grinder + pos;
2820 
2821 	switch (grinder->state) {
2822 	case e_GRINDER_PREFETCH_PIPE:
2823 	{
2824 		if (grinder_next_pipe(port, subport, pos)) {
2825 			grinder_prefetch_pipe(subport, pos);
2826 			subport->busy_grinders++;
2827 
2828 			grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2829 			return 0;
2830 		}
2831 
2832 		return 0;
2833 	}
2834 
2835 	case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2836 	{
2837 		struct rte_sched_pipe *pipe = grinder->pipe;
2838 
2839 		grinder->pipe_params = subport->pipe_profiles + pipe->profile;
2840 		grinder->subport_params = port->subport_profiles +
2841 						subport->profile;
2842 
2843 		grinder_prefetch_tc_queue_arrays(subport, pos);
2844 
2845 		if (subport->tc_ov_enabled)
2846 			grinder_credits_update_with_tc_ov(port, subport, pos);
2847 		else
2848 			grinder_credits_update(port, subport, pos);
2849 
2850 		grinder->state = e_GRINDER_PREFETCH_MBUF;
2851 		return 0;
2852 	}
2853 
2854 	case e_GRINDER_PREFETCH_MBUF:
2855 	{
2856 		grinder_prefetch_mbuf(subport, pos);
2857 
2858 		grinder->state = e_GRINDER_READ_MBUF;
2859 		return 0;
2860 	}
2861 
2862 	case e_GRINDER_READ_MBUF:
2863 	{
2864 		uint32_t wrr_active, result = 0;
2865 
2866 		result = grinder_schedule(port, subport, pos);
2867 
2868 		wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2869 
2870 		/* Look for next packet within the same TC */
2871 		if (result && grinder->qmask) {
2872 			if (wrr_active)
2873 				grinder_wrr(subport, pos);
2874 
2875 			grinder_prefetch_mbuf(subport, pos);
2876 
2877 			return 1;
2878 		}
2879 
2880 		if (wrr_active)
2881 			grinder_wrr_store(subport, pos);
2882 
2883 		/* Look for another active TC within same pipe */
2884 		if (grinder_next_tc(port, subport, pos)) {
2885 			grinder_prefetch_tc_queue_arrays(subport, pos);
2886 
2887 			grinder->state = e_GRINDER_PREFETCH_MBUF;
2888 			return result;
2889 		}
2890 
2891 		if (grinder->productive == 0 &&
2892 		    subport->pipe_loop == RTE_SCHED_PIPE_INVALID)
2893 			subport->pipe_loop = grinder->pindex;
2894 
2895 		grinder_evict(subport, pos);
2896 
2897 		/* Look for another active pipe */
2898 		if (grinder_next_pipe(port, subport, pos)) {
2899 			grinder_prefetch_pipe(subport, pos);
2900 
2901 			grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2902 			return result;
2903 		}
2904 
2905 		/* No active pipe found */
2906 		subport->busy_grinders--;
2907 
2908 		grinder->state = e_GRINDER_PREFETCH_PIPE;
2909 		return result;
2910 	}
2911 
2912 	default:
2913 		rte_panic("Algorithmic error (invalid state)\n");
2914 		return 0;
2915 	}
2916 }
2917 
2918 static inline void
2919 rte_sched_port_time_resync(struct rte_sched_port *port)
2920 {
2921 	uint64_t cycles = rte_get_tsc_cycles();
2922 	uint64_t cycles_diff;
2923 	uint64_t bytes_diff;
2924 	uint32_t i;
2925 
2926 	if (cycles < port->time_cpu_cycles)
2927 		port->time_cpu_cycles = 0;
2928 
2929 	cycles_diff = cycles - port->time_cpu_cycles;
2930 	/* Compute elapsed time in bytes */
2931 	bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2932 					   port->inv_cycles_per_byte);
2933 
2934 	/* Advance port time */
2935 	port->time_cpu_cycles +=
2936 		(bytes_diff * port->cycles_per_byte) >> RTE_SCHED_TIME_SHIFT;
2937 	port->time_cpu_bytes += bytes_diff;
2938 	if (port->time < port->time_cpu_bytes)
2939 		port->time = port->time_cpu_bytes;
2940 
2941 	/* Reset pipe loop detection */
2942 	for (i = 0; i < port->n_subports_per_port; i++)
2943 		port->subports[i]->pipe_loop = RTE_SCHED_PIPE_INVALID;
2944 }
2945 
2946 static inline int
2947 rte_sched_port_exceptions(struct rte_sched_subport *subport, int second_pass)
2948 {
2949 	int exceptions;
2950 
2951 	/* Check if any exception flag is set */
2952 	exceptions = (second_pass && subport->busy_grinders == 0) ||
2953 		(subport->pipe_exhaustion == 1);
2954 
2955 	/* Clear exception flags */
2956 	subport->pipe_exhaustion = 0;
2957 
2958 	return exceptions;
2959 }
2960 
2961 int
2962 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2963 {
2964 	struct rte_sched_subport *subport;
2965 	uint32_t subport_id = port->subport_id;
2966 	uint32_t i, n_subports = 0, count;
2967 
2968 	port->pkts_out = pkts;
2969 	port->n_pkts_out = 0;
2970 
2971 	rte_sched_port_time_resync(port);
2972 
2973 	/* Take each queue in the grinder one step further */
2974 	for (i = 0, count = 0; ; i++)  {
2975 		subport = port->subports[subport_id];
2976 
2977 		count += grinder_handle(port, subport,
2978 				i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2979 
2980 		if (count == n_pkts) {
2981 			subport_id++;
2982 
2983 			if (subport_id == port->n_subports_per_port)
2984 				subport_id = 0;
2985 
2986 			port->subport_id = subport_id;
2987 			break;
2988 		}
2989 
2990 		if (rte_sched_port_exceptions(subport, i >= RTE_SCHED_PORT_N_GRINDERS)) {
2991 			i = 0;
2992 			subport_id++;
2993 			n_subports++;
2994 		}
2995 
2996 		if (subport_id == port->n_subports_per_port)
2997 			subport_id = 0;
2998 
2999 		if (n_subports == port->n_subports_per_port) {
3000 			port->subport_id = subport_id;
3001 			break;
3002 		}
3003 	}
3004 
3005 	return count;
3006 }
3007