common/cnxk: handle packet mode shaper limits
[dpdk.git] / drivers / common / cnxk / roc_nix_tm_utils.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include "roc_api.h"
6 #include "roc_priv.h"
7
8 static inline uint64_t
9 nix_tm_shaper2regval(struct nix_tm_shaper_data *shaper)
10 {
11         uint64_t regval;
12
13         if (roc_model_is_cn9k()) {
14                 regval = (shaper->burst_exponent << 37);
15                 regval |= (shaper->burst_mantissa << 29);
16                 regval |= (shaper->div_exp << 13);
17                 regval |= (shaper->exponent << 9);
18                 regval |= (shaper->mantissa << 1);
19                 return regval;
20         }
21
22         regval = (shaper->burst_exponent << 44);
23         regval |= (shaper->burst_mantissa << 29);
24         regval |= (shaper->div_exp << 13);
25         regval |= (shaper->exponent << 9);
26         regval |= (shaper->mantissa << 1);
27         return regval;
28 }
29
30 uint16_t
31 nix_tm_lvl2nix_tl1_root(uint32_t lvl)
32 {
33         switch (lvl) {
34         case ROC_TM_LVL_ROOT:
35                 return NIX_TXSCH_LVL_TL1;
36         case ROC_TM_LVL_SCH1:
37                 return NIX_TXSCH_LVL_TL2;
38         case ROC_TM_LVL_SCH2:
39                 return NIX_TXSCH_LVL_TL3;
40         case ROC_TM_LVL_SCH3:
41                 return NIX_TXSCH_LVL_TL4;
42         case ROC_TM_LVL_SCH4:
43                 return NIX_TXSCH_LVL_SMQ;
44         default:
45                 return NIX_TXSCH_LVL_CNT;
46         }
47 }
48
49 uint16_t
50 nix_tm_lvl2nix_tl2_root(uint32_t lvl)
51 {
52         switch (lvl) {
53         case ROC_TM_LVL_ROOT:
54                 return NIX_TXSCH_LVL_TL2;
55         case ROC_TM_LVL_SCH1:
56                 return NIX_TXSCH_LVL_TL3;
57         case ROC_TM_LVL_SCH2:
58                 return NIX_TXSCH_LVL_TL4;
59         case ROC_TM_LVL_SCH3:
60                 return NIX_TXSCH_LVL_SMQ;
61         default:
62                 return NIX_TXSCH_LVL_CNT;
63         }
64 }
65
66 uint16_t
67 nix_tm_lvl2nix(struct nix *nix, uint32_t lvl)
68 {
69         if (nix_tm_have_tl1_access(nix))
70                 return nix_tm_lvl2nix_tl1_root(lvl);
71         else
72                 return nix_tm_lvl2nix_tl2_root(lvl);
73 }
74
75 static uint8_t
76 nix_tm_relchan_get(struct nix *nix)
77 {
78         return nix->tx_chan_base & 0xff;
79 }
80
81 static int
82 nix_tm_find_prio_anchor(struct nix *nix, uint32_t node_id,
83                         enum roc_nix_tm_tree tree)
84 {
85         struct nix_tm_node *child_node;
86         struct nix_tm_node_list *list;
87
88         list = nix_tm_node_list(nix, tree);
89
90         TAILQ_FOREACH(child_node, list, node) {
91                 if (!child_node->parent)
92                         continue;
93                 if (!(child_node->parent->id == node_id))
94                         continue;
95                 if (child_node->priority == child_node->parent->rr_prio)
96                         continue;
97                 return child_node->hw_id - child_node->priority;
98         }
99         return 0;
100 }
101
102 struct nix_tm_shaper_profile *
103 nix_tm_shaper_profile_search(struct nix *nix, uint32_t id)
104 {
105         struct nix_tm_shaper_profile *profile;
106
107         TAILQ_FOREACH(profile, &nix->shaper_profile_list, shaper) {
108                 if (profile->id == id)
109                         return profile;
110         }
111         return NULL;
112 }
113
114 struct nix_tm_node *
115 nix_tm_node_search(struct nix *nix, uint32_t node_id, enum roc_nix_tm_tree tree)
116 {
117         struct nix_tm_node_list *list;
118         struct nix_tm_node *node;
119
120         list = nix_tm_node_list(nix, tree);
121         TAILQ_FOREACH(node, list, node) {
122                 if (node->id == node_id)
123                         return node;
124         }
125         return NULL;
126 }
127
128 uint64_t
129 nix_tm_shaper_rate_conv(uint64_t value, uint64_t *exponent_p,
130                         uint64_t *mantissa_p, uint64_t *div_exp_p)
131 {
132         uint64_t div_exp, exponent, mantissa;
133
134         /* Boundary checks */
135         if (value < NIX_TM_MIN_SHAPER_RATE || value > NIX_TM_MAX_SHAPER_RATE)
136                 return 0;
137
138         if (value <= NIX_TM_SHAPER_RATE(0, 0, 0)) {
139                 /* Calculate rate div_exp and mantissa using
140                  * the following formula:
141                  *
142                  * value = (2E6 * (256 + mantissa)
143                  *              / ((1 << div_exp) * 256))
144                  */
145                 div_exp = 0;
146                 exponent = 0;
147                 mantissa = NIX_TM_MAX_RATE_MANTISSA;
148
149                 while (value < (NIX_TM_SHAPER_RATE_CONST / (1 << div_exp)))
150                         div_exp += 1;
151
152                 while (value < ((NIX_TM_SHAPER_RATE_CONST * (256 + mantissa)) /
153                                 ((1 << div_exp) * 256)))
154                         mantissa -= 1;
155         } else {
156                 /* Calculate rate exponent and mantissa using
157                  * the following formula:
158                  *
159                  * value = (2E6 * ((256 + mantissa) << exponent)) / 256
160                  *
161                  */
162                 div_exp = 0;
163                 exponent = NIX_TM_MAX_RATE_EXPONENT;
164                 mantissa = NIX_TM_MAX_RATE_MANTISSA;
165
166                 while (value < (NIX_TM_SHAPER_RATE_CONST * (1 << exponent)))
167                         exponent -= 1;
168
169                 while (value < ((NIX_TM_SHAPER_RATE_CONST *
170                                  ((256 + mantissa) << exponent)) /
171                                 256))
172                         mantissa -= 1;
173         }
174
175         if (div_exp > NIX_TM_MAX_RATE_DIV_EXP ||
176             exponent > NIX_TM_MAX_RATE_EXPONENT ||
177             mantissa > NIX_TM_MAX_RATE_MANTISSA)
178                 return 0;
179
180         if (div_exp_p)
181                 *div_exp_p = div_exp;
182         if (exponent_p)
183                 *exponent_p = exponent;
184         if (mantissa_p)
185                 *mantissa_p = mantissa;
186
187         /* Calculate real rate value */
188         return NIX_TM_SHAPER_RATE(exponent, mantissa, div_exp);
189 }
190
191 uint64_t
192 nix_tm_shaper_burst_conv(uint64_t value, uint64_t *exponent_p,
193                          uint64_t *mantissa_p)
194 {
195         uint64_t min_burst, max_burst;
196         uint64_t exponent, mantissa;
197         uint32_t max_mantissa;
198
199         min_burst = NIX_TM_MIN_SHAPER_BURST;
200         max_burst = roc_nix_tm_max_shaper_burst_get();
201
202         if (value < min_burst || value > max_burst)
203                 return 0;
204
205         max_mantissa = (roc_model_is_cn9k() ? NIX_CN9K_TM_MAX_BURST_MANTISSA :
206                                               NIX_TM_MAX_BURST_MANTISSA);
207         /* Calculate burst exponent and mantissa using
208          * the following formula:
209          *
210          * value = (((256 + mantissa) << (exponent + 1) / 256)
211          *
212          */
213         exponent = NIX_TM_MAX_BURST_EXPONENT;
214         mantissa = max_mantissa;
215
216         while (value < (1ull << (exponent + 1)))
217                 exponent -= 1;
218
219         while (value < ((256 + mantissa) << (exponent + 1)) / 256)
220                 mantissa -= 1;
221
222         if (exponent > NIX_TM_MAX_BURST_EXPONENT || mantissa > max_mantissa)
223                 return 0;
224
225         if (exponent_p)
226                 *exponent_p = exponent;
227         if (mantissa_p)
228                 *mantissa_p = mantissa;
229
230         return NIX_TM_SHAPER_BURST(exponent, mantissa);
231 }
232
233 static void
234 nix_tm_shaper_conf_get(struct nix_tm_shaper_profile *profile,
235                        struct nix_tm_shaper_data *cir,
236                        struct nix_tm_shaper_data *pir)
237 {
238         if (!profile)
239                 return;
240
241         /* Calculate CIR exponent and mantissa */
242         if (profile->commit.rate)
243                 cir->rate = nix_tm_shaper_rate_conv(
244                         profile->commit.rate, &cir->exponent, &cir->mantissa,
245                         &cir->div_exp);
246
247         /* Calculate PIR exponent and mantissa */
248         if (profile->peak.rate)
249                 pir->rate = nix_tm_shaper_rate_conv(
250                         profile->peak.rate, &pir->exponent, &pir->mantissa,
251                         &pir->div_exp);
252
253         /* Calculate CIR burst exponent and mantissa */
254         if (profile->commit.size)
255                 cir->burst = nix_tm_shaper_burst_conv(profile->commit.size,
256                                                       &cir->burst_exponent,
257                                                       &cir->burst_mantissa);
258
259         /* Calculate PIR burst exponent and mantissa */
260         if (profile->peak.size)
261                 pir->burst = nix_tm_shaper_burst_conv(profile->peak.size,
262                                                       &pir->burst_exponent,
263                                                       &pir->burst_mantissa);
264 }
265
266 uint32_t
267 nix_tm_check_rr(struct nix *nix, uint32_t parent_id, enum roc_nix_tm_tree tree,
268                 uint32_t *rr_prio, uint32_t *max_prio)
269 {
270         uint32_t node_cnt[NIX_TM_TLX_SP_PRIO_MAX];
271         struct nix_tm_node_list *list;
272         struct nix_tm_node *node;
273         uint32_t rr_num = 0, i;
274         uint32_t children = 0;
275         uint32_t priority;
276
277         memset(node_cnt, 0, sizeof(node_cnt));
278         *rr_prio = 0xF;
279         *max_prio = UINT32_MAX;
280
281         list = nix_tm_node_list(nix, tree);
282         TAILQ_FOREACH(node, list, node) {
283                 if (!node->parent)
284                         continue;
285
286                 if (!(node->parent->id == parent_id))
287                         continue;
288
289                 priority = node->priority;
290                 node_cnt[priority]++;
291                 children++;
292         }
293
294         for (i = 0; i < NIX_TM_TLX_SP_PRIO_MAX; i++) {
295                 if (!node_cnt[i])
296                         break;
297
298                 if (node_cnt[i] > rr_num) {
299                         *rr_prio = i;
300                         rr_num = node_cnt[i];
301                 }
302         }
303
304         /* RR group of single RR child is considered as SP */
305         if (rr_num == 1) {
306                 *rr_prio = 0xF;
307                 rr_num = 0;
308         }
309
310         /* Max prio will be returned only when we have non zero prio
311          * or if a parent has single child.
312          */
313         if (i > 1 || (children == 1))
314                 *max_prio = i - 1;
315         return rr_num;
316 }
317
318 static uint16_t
319 nix_tm_max_prio(struct nix *nix, uint16_t hw_lvl)
320 {
321         if (hw_lvl >= NIX_TXSCH_LVL_CNT)
322                 return 0;
323
324         /* MDQ does not support SP */
325         if (hw_lvl == NIX_TXSCH_LVL_MDQ)
326                 return 0;
327
328         /* PF's TL1 with VF's enabled does not support SP */
329         if (hw_lvl == NIX_TXSCH_LVL_TL1 && (!nix_tm_have_tl1_access(nix) ||
330                                             (nix->tm_flags & NIX_TM_TL1_NO_SP)))
331                 return 0;
332
333         return NIX_TM_TLX_SP_PRIO_MAX - 1;
334 }
335
336 int
337 nix_tm_validate_prio(struct nix *nix, uint32_t lvl, uint32_t parent_id,
338                      uint32_t priority, enum roc_nix_tm_tree tree)
339 {
340         uint8_t priorities[NIX_TM_TLX_SP_PRIO_MAX];
341         struct nix_tm_node_list *list;
342         struct nix_tm_node *node;
343         uint32_t rr_num = 0;
344         int i;
345
346         list = nix_tm_node_list(nix, tree);
347         /* Validate priority against max */
348         if (priority > nix_tm_max_prio(nix, nix_tm_lvl2nix(nix, lvl - 1)))
349                 return NIX_ERR_TM_PRIO_EXCEEDED;
350
351         if (parent_id == ROC_NIX_TM_NODE_ID_INVALID)
352                 return 0;
353
354         memset(priorities, 0, sizeof(priorities));
355         priorities[priority] = 1;
356
357         TAILQ_FOREACH(node, list, node) {
358                 if (!node->parent)
359                         continue;
360
361                 if (node->parent->id != parent_id)
362                         continue;
363
364                 priorities[node->priority]++;
365         }
366
367         for (i = 0; i < NIX_TM_TLX_SP_PRIO_MAX; i++)
368                 if (priorities[i] > 1)
369                         rr_num++;
370
371         /* At max, one rr groups per parent */
372         if (rr_num > 1)
373                 return NIX_ERR_TM_MULTIPLE_RR_GROUPS;
374
375         /* Check for previous priority to avoid holes in priorities */
376         if (priority && !priorities[priority - 1])
377                 return NIX_ERR_TM_PRIO_ORDER;
378
379         return 0;
380 }
381
382 bool
383 nix_tm_child_res_valid(struct nix_tm_node_list *list,
384                        struct nix_tm_node *parent)
385 {
386         struct nix_tm_node *child;
387
388         TAILQ_FOREACH(child, list, node) {
389                 if (child->parent != parent)
390                         continue;
391                 if (!(child->flags & NIX_TM_NODE_HWRES))
392                         return false;
393         }
394         return true;
395 }
396
397 uint8_t
398 nix_tm_tl1_default_prep(uint32_t schq, volatile uint64_t *reg,
399                         volatile uint64_t *regval)
400 {
401         uint8_t k = 0;
402
403         /*
404          * Default config for TL1.
405          * For VF this is always ignored.
406          */
407         plt_tm_dbg("Default config for main root %s(%u)",
408                    nix_tm_hwlvl2str(NIX_TXSCH_LVL_TL1), schq);
409
410         /* Set DWRR quantum */
411         reg[k] = NIX_AF_TL1X_SCHEDULE(schq);
412         regval[k] = NIX_TM_TL1_DFLT_RR_QTM;
413         k++;
414
415         reg[k] = NIX_AF_TL1X_TOPOLOGY(schq);
416         regval[k] = (NIX_TM_TL1_DFLT_RR_PRIO << 1);
417         k++;
418
419         reg[k] = NIX_AF_TL1X_CIR(schq);
420         regval[k] = 0;
421         k++;
422
423         return k;
424 }
425
426 uint8_t
427 nix_tm_topology_reg_prep(struct nix *nix, struct nix_tm_node *node,
428                          volatile uint64_t *reg, volatile uint64_t *regval,
429                          volatile uint64_t *regval_mask)
430 {
431         struct roc_nix *roc_nix = nix_priv_to_roc_nix(nix);
432         uint8_t k = 0, hw_lvl, parent_lvl;
433         uint64_t parent = 0, child = 0;
434         enum roc_nix_tm_tree tree;
435         uint32_t rr_prio, schq;
436         uint16_t link, relchan;
437
438         tree = node->tree;
439         schq = node->hw_id;
440         hw_lvl = node->hw_lvl;
441         parent_lvl = hw_lvl + 1;
442         rr_prio = node->rr_prio;
443
444         /* Root node will not have a parent node */
445         if (hw_lvl == nix->tm_root_lvl)
446                 parent = node->parent_hw_id;
447         else
448                 parent = node->parent->hw_id;
449
450         link = nix->tx_link;
451         relchan = nix_tm_relchan_get(nix);
452
453         if (hw_lvl != NIX_TXSCH_LVL_SMQ)
454                 child = nix_tm_find_prio_anchor(nix, node->id, tree);
455
456         /* Override default rr_prio when TL1
457          * Static Priority is disabled
458          */
459         if (hw_lvl == NIX_TXSCH_LVL_TL1 && nix->tm_flags & NIX_TM_TL1_NO_SP) {
460                 rr_prio = NIX_TM_TL1_DFLT_RR_PRIO;
461                 child = 0;
462         }
463
464         plt_tm_dbg("Topology config node %s(%u)->%s(%" PRIu64 ") lvl %u, id %u"
465                    " prio_anchor %" PRIu64 " rr_prio %u (%p)",
466                    nix_tm_hwlvl2str(hw_lvl), schq, nix_tm_hwlvl2str(parent_lvl),
467                    parent, node->lvl, node->id, child, rr_prio, node);
468
469         /* Prepare Topology and Link config */
470         switch (hw_lvl) {
471         case NIX_TXSCH_LVL_SMQ:
472
473                 /* Set xoff which will be cleared later */
474                 reg[k] = NIX_AF_SMQX_CFG(schq);
475                 regval[k] = (BIT_ULL(50) | NIX_MIN_HW_FRS |
476                              ((nix->mtu & 0xFFFF) << 8));
477                 /* Maximum Vtag insertion size as a multiple of four bytes */
478                 if (roc_nix->hw_vlan_ins)
479                         regval[k] |= (0x2ULL << 36);
480                 regval_mask[k] = ~(BIT_ULL(50) | GENMASK_ULL(6, 0) |
481                                    GENMASK_ULL(23, 8) | GENMASK_ULL(38, 36));
482                 k++;
483
484                 /* Parent and schedule conf */
485                 reg[k] = NIX_AF_MDQX_PARENT(schq);
486                 regval[k] = parent << 16;
487                 k++;
488
489                 break;
490         case NIX_TXSCH_LVL_TL4:
491                 /* Parent and schedule conf */
492                 reg[k] = NIX_AF_TL4X_PARENT(schq);
493                 regval[k] = parent << 16;
494                 k++;
495
496                 reg[k] = NIX_AF_TL4X_TOPOLOGY(schq);
497                 regval[k] = (child << 32) | (rr_prio << 1);
498                 k++;
499
500                 /* Configure TL4 to send to SDP channel instead of CGX/LBK */
501                 if (nix->sdp_link) {
502                         reg[k] = NIX_AF_TL4X_SDP_LINK_CFG(schq);
503                         regval[k] = BIT_ULL(12);
504                         k++;
505                 }
506                 break;
507         case NIX_TXSCH_LVL_TL3:
508                 /* Parent and schedule conf */
509                 reg[k] = NIX_AF_TL3X_PARENT(schq);
510                 regval[k] = parent << 16;
511                 k++;
512
513                 reg[k] = NIX_AF_TL3X_TOPOLOGY(schq);
514                 regval[k] = (child << 32) | (rr_prio << 1);
515                 k++;
516
517                 /* Link configuration */
518                 if (!nix->sdp_link &&
519                     nix->tm_link_cfg_lvl == NIX_TXSCH_LVL_TL3) {
520                         reg[k] = NIX_AF_TL3_TL2X_LINKX_CFG(schq, link);
521                         regval[k] = BIT_ULL(12) | relchan;
522                         k++;
523                 }
524
525                 break;
526         case NIX_TXSCH_LVL_TL2:
527                 /* Parent and schedule conf */
528                 reg[k] = NIX_AF_TL2X_PARENT(schq);
529                 regval[k] = parent << 16;
530                 k++;
531
532                 reg[k] = NIX_AF_TL2X_TOPOLOGY(schq);
533                 regval[k] = (child << 32) | (rr_prio << 1);
534                 k++;
535
536                 /* Link configuration */
537                 if (!nix->sdp_link &&
538                     nix->tm_link_cfg_lvl == NIX_TXSCH_LVL_TL2) {
539                         reg[k] = NIX_AF_TL3_TL2X_LINKX_CFG(schq, link);
540                         regval[k] = BIT_ULL(12) | relchan;
541                         k++;
542                 }
543
544                 break;
545         case NIX_TXSCH_LVL_TL1:
546                 reg[k] = NIX_AF_TL1X_TOPOLOGY(schq);
547                 regval[k] = (child << 32) | (rr_prio << 1 /*RR_PRIO*/);
548                 k++;
549
550                 break;
551         }
552
553         return k;
554 }
555
556 uint8_t
557 nix_tm_sched_reg_prep(struct nix *nix, struct nix_tm_node *node,
558                       volatile uint64_t *reg, volatile uint64_t *regval)
559 {
560         uint64_t strict_prio = node->priority;
561         uint32_t hw_lvl = node->hw_lvl;
562         uint32_t schq = node->hw_id;
563         uint64_t rr_quantum;
564         uint8_t k = 0;
565
566         /* For CN9K, weight needs to be converted to quantum */
567         rr_quantum = nix_tm_weight_to_rr_quantum(node->weight);
568
569         /* For children to root, strict prio is default if either
570          * device root is TL2 or TL1 Static Priority is disabled.
571          */
572         if (hw_lvl == NIX_TXSCH_LVL_TL2 &&
573             (!nix_tm_have_tl1_access(nix) || nix->tm_flags & NIX_TM_TL1_NO_SP))
574                 strict_prio = NIX_TM_TL1_DFLT_RR_PRIO;
575
576         plt_tm_dbg("Schedule config node %s(%u) lvl %u id %u, "
577                    "prio 0x%" PRIx64 ", rr_quantum/rr_wt 0x%" PRIx64 " (%p)",
578                    nix_tm_hwlvl2str(node->hw_lvl), schq, node->lvl, node->id,
579                    strict_prio, rr_quantum, node);
580
581         switch (hw_lvl) {
582         case NIX_TXSCH_LVL_SMQ:
583                 reg[k] = NIX_AF_MDQX_SCHEDULE(schq);
584                 regval[k] = (strict_prio << 24) | rr_quantum;
585                 k++;
586
587                 break;
588         case NIX_TXSCH_LVL_TL4:
589                 reg[k] = NIX_AF_TL4X_SCHEDULE(schq);
590                 regval[k] = (strict_prio << 24) | rr_quantum;
591                 k++;
592
593                 break;
594         case NIX_TXSCH_LVL_TL3:
595                 reg[k] = NIX_AF_TL3X_SCHEDULE(schq);
596                 regval[k] = (strict_prio << 24) | rr_quantum;
597                 k++;
598
599                 break;
600         case NIX_TXSCH_LVL_TL2:
601                 reg[k] = NIX_AF_TL2X_SCHEDULE(schq);
602                 regval[k] = (strict_prio << 24) | rr_quantum;
603                 k++;
604
605                 break;
606         case NIX_TXSCH_LVL_TL1:
607                 reg[k] = NIX_AF_TL1X_SCHEDULE(schq);
608                 regval[k] = rr_quantum;
609                 k++;
610
611                 break;
612         }
613
614         return k;
615 }
616
617 uint8_t
618 nix_tm_shaper_reg_prep(struct nix_tm_node *node,
619                        struct nix_tm_shaper_profile *profile,
620                        volatile uint64_t *reg, volatile uint64_t *regval)
621 {
622         struct nix_tm_shaper_data cir, pir;
623         uint32_t schq = node->hw_id;
624         uint64_t adjust = 0;
625         uint8_t k = 0;
626
627         memset(&cir, 0, sizeof(cir));
628         memset(&pir, 0, sizeof(pir));
629         nix_tm_shaper_conf_get(profile, &cir, &pir);
630
631         if (profile && node->pkt_mode)
632                 adjust = profile->pkt_mode_adj;
633         else if (profile)
634                 adjust = profile->pkt_len_adj;
635
636         plt_tm_dbg("Shaper config node %s(%u) lvl %u id %u, "
637                    "pir %" PRIu64 "(%" PRIu64 "B),"
638                    " cir %" PRIu64 "(%" PRIu64 "B)"
639                    "adjust 0x%" PRIx64 "(pktmode %u) (%p)",
640                    nix_tm_hwlvl2str(node->hw_lvl), schq, node->lvl, node->id,
641                    pir.rate, pir.burst, cir.rate, cir.burst, adjust,
642                    node->pkt_mode, node);
643
644         switch (node->hw_lvl) {
645         case NIX_TXSCH_LVL_SMQ:
646                 /* Configure PIR, CIR */
647                 reg[k] = NIX_AF_MDQX_PIR(schq);
648                 regval[k] = (pir.rate && pir.burst) ?
649                                           (nix_tm_shaper2regval(&pir) | 1) :
650                                           0;
651                 k++;
652
653                 reg[k] = NIX_AF_MDQX_CIR(schq);
654                 regval[k] = (cir.rate && cir.burst) ?
655                                           (nix_tm_shaper2regval(&cir) | 1) :
656                                           0;
657                 k++;
658
659                 /* Configure RED ALG */
660                 reg[k] = NIX_AF_MDQX_SHAPE(schq);
661                 regval[k] = (adjust | (uint64_t)node->red_algo << 9 |
662                              (uint64_t)node->pkt_mode << 24);
663                 k++;
664                 break;
665         case NIX_TXSCH_LVL_TL4:
666                 /* Configure PIR, CIR */
667                 reg[k] = NIX_AF_TL4X_PIR(schq);
668                 regval[k] = (pir.rate && pir.burst) ?
669                                           (nix_tm_shaper2regval(&pir) | 1) :
670                                           0;
671                 k++;
672
673                 reg[k] = NIX_AF_TL4X_CIR(schq);
674                 regval[k] = (cir.rate && cir.burst) ?
675                                           (nix_tm_shaper2regval(&cir) | 1) :
676                                           0;
677                 k++;
678
679                 /* Configure RED algo */
680                 reg[k] = NIX_AF_TL4X_SHAPE(schq);
681                 regval[k] = (adjust | (uint64_t)node->red_algo << 9 |
682                              (uint64_t)node->pkt_mode << 24);
683                 k++;
684                 break;
685         case NIX_TXSCH_LVL_TL3:
686                 /* Configure PIR, CIR */
687                 reg[k] = NIX_AF_TL3X_PIR(schq);
688                 regval[k] = (pir.rate && pir.burst) ?
689                                           (nix_tm_shaper2regval(&pir) | 1) :
690                                           0;
691                 k++;
692
693                 reg[k] = NIX_AF_TL3X_CIR(schq);
694                 regval[k] = (cir.rate && cir.burst) ?
695                                           (nix_tm_shaper2regval(&cir) | 1) :
696                                           0;
697                 k++;
698
699                 /* Configure RED algo */
700                 reg[k] = NIX_AF_TL3X_SHAPE(schq);
701                 regval[k] = (adjust | (uint64_t)node->red_algo << 9 |
702                              (uint64_t)node->pkt_mode);
703                 k++;
704
705                 break;
706         case NIX_TXSCH_LVL_TL2:
707                 /* Configure PIR, CIR */
708                 reg[k] = NIX_AF_TL2X_PIR(schq);
709                 regval[k] = (pir.rate && pir.burst) ?
710                                           (nix_tm_shaper2regval(&pir) | 1) :
711                                           0;
712                 k++;
713
714                 reg[k] = NIX_AF_TL2X_CIR(schq);
715                 regval[k] = (cir.rate && cir.burst) ?
716                                           (nix_tm_shaper2regval(&cir) | 1) :
717                                           0;
718                 k++;
719
720                 /* Configure RED algo */
721                 reg[k] = NIX_AF_TL2X_SHAPE(schq);
722                 regval[k] = (adjust | (uint64_t)node->red_algo << 9 |
723                              (uint64_t)node->pkt_mode << 24);
724                 k++;
725
726                 break;
727         case NIX_TXSCH_LVL_TL1:
728                 /* Configure CIR */
729                 reg[k] = NIX_AF_TL1X_CIR(schq);
730                 regval[k] = (cir.rate && cir.burst) ?
731                                           (nix_tm_shaper2regval(&cir) | 1) :
732                                           0;
733                 k++;
734
735                 /* Configure length disable and adjust */
736                 reg[k] = NIX_AF_TL1X_SHAPE(schq);
737                 regval[k] = (adjust | (uint64_t)node->pkt_mode << 24);
738                 k++;
739                 break;
740         }
741
742         return k;
743 }
744
745 uint8_t
746 nix_tm_sw_xoff_prep(struct nix_tm_node *node, bool enable,
747                     volatile uint64_t *reg, volatile uint64_t *regval)
748 {
749         uint32_t hw_lvl = node->hw_lvl;
750         uint32_t schq = node->hw_id;
751         uint8_t k = 0;
752
753         plt_tm_dbg("sw xoff config node %s(%u) lvl %u id %u, enable %u (%p)",
754                    nix_tm_hwlvl2str(hw_lvl), schq, node->lvl, node->id, enable,
755                    node);
756
757         regval[k] = enable;
758
759         switch (hw_lvl) {
760         case NIX_TXSCH_LVL_MDQ:
761                 reg[k] = NIX_AF_MDQX_SW_XOFF(schq);
762                 k++;
763                 break;
764         case NIX_TXSCH_LVL_TL4:
765                 reg[k] = NIX_AF_TL4X_SW_XOFF(schq);
766                 k++;
767                 break;
768         case NIX_TXSCH_LVL_TL3:
769                 reg[k] = NIX_AF_TL3X_SW_XOFF(schq);
770                 k++;
771                 break;
772         case NIX_TXSCH_LVL_TL2:
773                 reg[k] = NIX_AF_TL2X_SW_XOFF(schq);
774                 k++;
775                 break;
776         case NIX_TXSCH_LVL_TL1:
777                 reg[k] = NIX_AF_TL1X_SW_XOFF(schq);
778                 k++;
779                 break;
780         default:
781                 break;
782         }
783
784         return k;
785 }
786
787 /* Search for min rate in topology */
788 uint64_t
789 nix_tm_shaper_profile_rate_min(struct nix *nix)
790 {
791         struct nix_tm_shaper_profile *profile;
792         uint64_t rate_min = 1E9; /* 1 Gbps */
793
794         TAILQ_FOREACH(profile, &nix->shaper_profile_list, shaper) {
795                 if (profile->peak.rate && profile->peak.rate < rate_min)
796                         rate_min = profile->peak.rate;
797
798                 if (profile->commit.rate && profile->commit.rate < rate_min)
799                         rate_min = profile->commit.rate;
800         }
801         return rate_min;
802 }
803
804 uint16_t
805 nix_tm_resource_avail(struct nix *nix, uint8_t hw_lvl, bool contig)
806 {
807         uint32_t pos = 0, start_pos = 0;
808         struct plt_bitmap *bmp;
809         uint16_t count = 0;
810         uint64_t slab = 0;
811
812         bmp = contig ? nix->schq_contig_bmp[hw_lvl] : nix->schq_bmp[hw_lvl];
813         plt_bitmap_scan_init(bmp);
814
815         if (!plt_bitmap_scan(bmp, &pos, &slab))
816                 return count;
817
818         /* Count bit set */
819         start_pos = pos;
820         do {
821                 count += __builtin_popcountll(slab);
822                 if (!plt_bitmap_scan(bmp, &pos, &slab))
823                         break;
824         } while (pos != start_pos);
825
826         return count;
827 }
828
829 uint16_t
830 nix_tm_resource_estimate(struct nix *nix, uint16_t *schq_contig, uint16_t *schq,
831                          enum roc_nix_tm_tree tree)
832 {
833         struct nix_tm_node_list *list;
834         uint8_t contig_cnt, hw_lvl;
835         struct nix_tm_node *parent;
836         uint16_t cnt = 0, avail;
837
838         list = nix_tm_node_list(nix, tree);
839         /* Walk through parents from TL1..TL4 */
840         for (hw_lvl = NIX_TXSCH_LVL_TL1; hw_lvl > 0; hw_lvl--) {
841                 TAILQ_FOREACH(parent, list, node) {
842                         if (hw_lvl != parent->hw_lvl)
843                                 continue;
844
845                         /* Skip accounting for children whose
846                          * parent does not indicate so.
847                          */
848                         if (!parent->child_realloc)
849                                 continue;
850
851                         /* Count children needed */
852                         schq[hw_lvl - 1] += parent->rr_num;
853                         if (parent->max_prio != UINT32_MAX) {
854                                 contig_cnt = parent->max_prio + 1;
855                                 schq_contig[hw_lvl - 1] += contig_cnt;
856                                 /* When we have SP + DWRR at a parent,
857                                  * we will always have a spare schq at rr prio
858                                  * location in contiguous queues. Hence reduce
859                                  * discontiguous count by 1.
860                                  */
861                                 if (parent->max_prio > 0 && parent->rr_num)
862                                         schq[hw_lvl - 1] -= 1;
863                         }
864                 }
865         }
866
867         schq[nix->tm_root_lvl] = 1;
868         if (!nix_tm_have_tl1_access(nix))
869                 schq[NIX_TXSCH_LVL_TL1] = 1;
870
871         /* Now check for existing resources */
872         for (hw_lvl = 0; hw_lvl < NIX_TXSCH_LVL_CNT; hw_lvl++) {
873                 avail = nix_tm_resource_avail(nix, hw_lvl, false);
874                 if (schq[hw_lvl] <= avail)
875                         schq[hw_lvl] = 0;
876                 else
877                         schq[hw_lvl] -= avail;
878
879                 /* For contiguous queues, realloc everything */
880                 avail = nix_tm_resource_avail(nix, hw_lvl, true);
881                 if (schq_contig[hw_lvl] <= avail)
882                         schq_contig[hw_lvl] = 0;
883
884                 cnt += schq[hw_lvl];
885                 cnt += schq_contig[hw_lvl];
886
887                 plt_tm_dbg("Estimate resources needed for %s: dis %u cont %u",
888                            nix_tm_hwlvl2str(hw_lvl), schq[hw_lvl],
889                            schq_contig[hw_lvl]);
890         }
891
892         return cnt;
893 }
894
895 uint16_t
896 roc_nix_tm_leaf_cnt(struct roc_nix *roc_nix)
897 {
898         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
899         struct nix_tm_node_list *list;
900         struct nix_tm_node *node;
901         uint16_t leaf_cnt = 0;
902
903         /* Count leafs only in user list */
904         list = nix_tm_node_list(nix, ROC_NIX_TM_USER);
905         TAILQ_FOREACH(node, list, node) {
906                 if (node->id < nix->nb_tx_queues)
907                         leaf_cnt++;
908         }
909
910         return leaf_cnt;
911 }
912
913 int
914 roc_nix_tm_node_lvl(struct roc_nix *roc_nix, uint32_t node_id)
915 {
916         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
917         struct nix_tm_node *node;
918
919         node = nix_tm_node_search(nix, node_id, ROC_NIX_TM_USER);
920         if (!node)
921                 return NIX_ERR_TM_INVALID_NODE;
922
923         return node->lvl;
924 }
925
926 struct roc_nix_tm_node *
927 roc_nix_tm_node_get(struct roc_nix *roc_nix, uint32_t node_id)
928 {
929         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
930         struct nix_tm_node *node;
931
932         node = nix_tm_node_search(nix, node_id, ROC_NIX_TM_USER);
933         return (struct roc_nix_tm_node *)node;
934 }
935
936 struct roc_nix_tm_node *
937 roc_nix_tm_node_next(struct roc_nix *roc_nix, struct roc_nix_tm_node *__prev)
938 {
939         struct nix_tm_node *prev = (struct nix_tm_node *)__prev;
940         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
941         struct nix_tm_node_list *list;
942
943         list = nix_tm_node_list(nix, ROC_NIX_TM_USER);
944
945         /* HEAD of the list */
946         if (!prev)
947                 return (struct roc_nix_tm_node *)TAILQ_FIRST(list);
948
949         /* Next entry */
950         if (prev->tree != ROC_NIX_TM_USER)
951                 return NULL;
952
953         return (struct roc_nix_tm_node *)TAILQ_NEXT(prev, node);
954 }
955
956 struct roc_nix_tm_shaper_profile *
957 roc_nix_tm_shaper_profile_get(struct roc_nix *roc_nix, uint32_t profile_id)
958 {
959         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
960         struct nix_tm_shaper_profile *profile;
961
962         profile = nix_tm_shaper_profile_search(nix, profile_id);
963         return (struct roc_nix_tm_shaper_profile *)profile;
964 }
965
966 struct roc_nix_tm_shaper_profile *
967 roc_nix_tm_shaper_profile_next(struct roc_nix *roc_nix,
968                                struct roc_nix_tm_shaper_profile *__prev)
969 {
970         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
971         struct nix_tm_shaper_profile_list *list;
972         struct nix_tm_shaper_profile *prev;
973
974         prev = (struct nix_tm_shaper_profile *)__prev;
975         list = &nix->shaper_profile_list;
976
977         /* HEAD of the list */
978         if (!prev)
979                 return (struct roc_nix_tm_shaper_profile *)TAILQ_FIRST(list);
980
981         return (struct roc_nix_tm_shaper_profile *)TAILQ_NEXT(prev, shaper);
982 }
983
984 struct nix_tm_node *
985 nix_tm_node_alloc(void)
986 {
987         struct nix_tm_node *node;
988
989         node = plt_zmalloc(sizeof(struct nix_tm_node), 0);
990         if (!node)
991                 return NULL;
992
993         node->free_fn = plt_free;
994         return node;
995 }
996
997 void
998 nix_tm_node_free(struct nix_tm_node *node)
999 {
1000         if (!node || node->free_fn == NULL)
1001                 return;
1002
1003         (node->free_fn)(node);
1004 }
1005
1006 struct nix_tm_shaper_profile *
1007 nix_tm_shaper_profile_alloc(void)
1008 {
1009         struct nix_tm_shaper_profile *profile;
1010
1011         profile = plt_zmalloc(sizeof(struct nix_tm_shaper_profile), 0);
1012         if (!profile)
1013                 return NULL;
1014
1015         profile->free_fn = plt_free;
1016         return profile;
1017 }
1018
1019 void
1020 nix_tm_shaper_profile_free(struct nix_tm_shaper_profile *profile)
1021 {
1022         if (!profile || !profile->free_fn)
1023                 return;
1024
1025         (profile->free_fn)(profile);
1026 }
1027
1028 int
1029 roc_nix_tm_node_stats_get(struct roc_nix *roc_nix, uint32_t node_id, bool clear,
1030                           struct roc_nix_tm_node_stats *n_stats)
1031 {
1032         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
1033         struct mbox *mbox = (&nix->dev)->mbox;
1034         struct nix_txschq_config *req, *rsp;
1035         struct nix_tm_node *node;
1036         uint32_t schq;
1037         int rc, i;
1038
1039         node = nix_tm_node_search(nix, node_id, ROC_NIX_TM_USER);
1040         if (!node)
1041                 return NIX_ERR_TM_INVALID_NODE;
1042
1043         if (node->hw_lvl != NIX_TXSCH_LVL_TL1)
1044                 return NIX_ERR_OP_NOTSUP;
1045
1046         schq = node->hw_id;
1047         /* Skip fetch if not requested */
1048         if (!n_stats)
1049                 goto clear_stats;
1050
1051         memset(n_stats, 0, sizeof(struct roc_nix_tm_node_stats));
1052         /* Check if node has HW resource */
1053         if (!(node->flags & NIX_TM_NODE_HWRES))
1054                 return 0;
1055
1056         req = mbox_alloc_msg_nix_txschq_cfg(mbox);
1057         req->read = 1;
1058         req->lvl = NIX_TXSCH_LVL_TL1;
1059
1060         i = 0;
1061         req->reg[i++] = NIX_AF_TL1X_DROPPED_PACKETS(schq);
1062         req->reg[i++] = NIX_AF_TL1X_DROPPED_BYTES(schq);
1063         req->reg[i++] = NIX_AF_TL1X_GREEN_PACKETS(schq);
1064         req->reg[i++] = NIX_AF_TL1X_GREEN_BYTES(schq);
1065         req->reg[i++] = NIX_AF_TL1X_YELLOW_PACKETS(schq);
1066         req->reg[i++] = NIX_AF_TL1X_YELLOW_BYTES(schq);
1067         req->reg[i++] = NIX_AF_TL1X_RED_PACKETS(schq);
1068         req->reg[i++] = NIX_AF_TL1X_RED_BYTES(schq);
1069         req->num_regs = i;
1070
1071         rc = mbox_process_msg(mbox, (void **)&rsp);
1072         if (rc)
1073                 return rc;
1074
1075         /* Return stats */
1076         n_stats->stats[ROC_NIX_TM_NODE_PKTS_DROPPED] = rsp->regval[0];
1077         n_stats->stats[ROC_NIX_TM_NODE_BYTES_DROPPED] = rsp->regval[1];
1078         n_stats->stats[ROC_NIX_TM_NODE_GREEN_PKTS] = rsp->regval[2];
1079         n_stats->stats[ROC_NIX_TM_NODE_GREEN_BYTES] = rsp->regval[3];
1080         n_stats->stats[ROC_NIX_TM_NODE_YELLOW_PKTS] = rsp->regval[4];
1081         n_stats->stats[ROC_NIX_TM_NODE_YELLOW_BYTES] = rsp->regval[5];
1082         n_stats->stats[ROC_NIX_TM_NODE_RED_PKTS] = rsp->regval[6];
1083         n_stats->stats[ROC_NIX_TM_NODE_RED_BYTES] = rsp->regval[7];
1084
1085 clear_stats:
1086         if (!clear)
1087                 return 0;
1088
1089         /* Clear all the stats */
1090         req = mbox_alloc_msg_nix_txschq_cfg(mbox);
1091         req->lvl = NIX_TXSCH_LVL_TL1;
1092         i = 0;
1093         req->reg[i++] = NIX_AF_TL1X_DROPPED_PACKETS(schq);
1094         req->reg[i++] = NIX_AF_TL1X_DROPPED_BYTES(schq);
1095         req->reg[i++] = NIX_AF_TL1X_GREEN_PACKETS(schq);
1096         req->reg[i++] = NIX_AF_TL1X_GREEN_BYTES(schq);
1097         req->reg[i++] = NIX_AF_TL1X_YELLOW_PACKETS(schq);
1098         req->reg[i++] = NIX_AF_TL1X_YELLOW_BYTES(schq);
1099         req->reg[i++] = NIX_AF_TL1X_RED_PACKETS(schq);
1100         req->reg[i++] = NIX_AF_TL1X_RED_BYTES(schq);
1101         req->num_regs = i;
1102
1103         return mbox_process_msg(mbox, (void **)&rsp);
1104 }