1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
9 bitmap_ctzll(uint64_t slab)
14 return __builtin_ctzll(slab);
18 nix_tm_clear_shaper_profiles(struct nix *nix)
20 struct nix_tm_shaper_profile *shaper_profile;
22 shaper_profile = TAILQ_FIRST(&nix->shaper_profile_list);
23 while (shaper_profile != NULL) {
24 if (shaper_profile->ref_cnt)
25 plt_warn("Shaper profile %u has non zero references",
27 TAILQ_REMOVE(&nix->shaper_profile_list, shaper_profile, shaper);
28 nix_tm_shaper_profile_free(shaper_profile);
29 shaper_profile = TAILQ_FIRST(&nix->shaper_profile_list);
34 nix_tm_node_reg_conf(struct nix *nix, struct nix_tm_node *node)
36 uint64_t regval_mask[MAX_REGS_PER_MBOX_MSG];
37 uint64_t regval[MAX_REGS_PER_MBOX_MSG];
38 struct nix_tm_shaper_profile *profile;
39 uint64_t reg[MAX_REGS_PER_MBOX_MSG];
40 struct mbox *mbox = (&nix->dev)->mbox;
41 struct nix_txschq_config *req;
46 memset(regval, 0, sizeof(regval));
47 memset(regval_mask, 0, sizeof(regval_mask));
49 profile = nix_tm_shaper_profile_search(nix, node->shaper_profile_id);
50 hw_lvl = node->hw_lvl;
52 /* Need this trigger to configure TL1 */
53 if (!nix_tm_have_tl1_access(nix) && hw_lvl == NIX_TXSCH_LVL_TL2) {
54 /* Prepare default conf for TL1 */
55 req = mbox_alloc_msg_nix_txschq_cfg(mbox);
56 req->lvl = NIX_TXSCH_LVL_TL1;
58 k = nix_tm_tl1_default_prep(node->parent_hw_id, req->reg,
61 rc = mbox_process(mbox);
66 /* Prepare topology config */
67 k = nix_tm_topology_reg_prep(nix, node, reg, regval, regval_mask);
69 /* Prepare schedule config */
70 k += nix_tm_sched_reg_prep(nix, node, ®[k], ®val[k]);
72 /* Prepare shaping config */
73 k += nix_tm_shaper_reg_prep(node, profile, ®[k], ®val[k]);
78 /* Copy and send config mbox */
79 req = mbox_alloc_msg_nix_txschq_cfg(mbox);
83 mbox_memcpy(req->reg, reg, sizeof(uint64_t) * k);
84 mbox_memcpy(req->regval, regval, sizeof(uint64_t) * k);
85 mbox_memcpy(req->regval_mask, regval_mask, sizeof(uint64_t) * k);
87 rc = mbox_process(mbox);
93 plt_err("Txschq conf failed for node %p, rc=%d", node, rc);
98 nix_tm_txsch_reg_config(struct nix *nix, enum roc_nix_tm_tree tree)
100 struct nix_tm_node_list *list;
101 struct nix_tm_node *node;
105 list = nix_tm_node_list(nix, tree);
107 for (hw_lvl = 0; hw_lvl <= nix->tm_root_lvl; hw_lvl++) {
108 TAILQ_FOREACH(node, list, node) {
109 if (node->hw_lvl != hw_lvl)
111 rc = nix_tm_node_reg_conf(nix, node);
121 nix_tm_update_parent_info(struct nix *nix, enum roc_nix_tm_tree tree)
123 struct nix_tm_node *child, *parent;
124 struct nix_tm_node_list *list;
125 uint32_t rr_prio, max_prio;
128 list = nix_tm_node_list(nix, tree);
130 /* Release all the node hw resources locally
131 * if parent marked as dirty and resource exists.
133 TAILQ_FOREACH(child, list, node) {
134 /* Release resource only if parent direct hierarchy changed */
135 if (child->flags & NIX_TM_NODE_HWRES && child->parent &&
136 child->parent->child_realloc) {
137 nix_tm_free_node_resource(nix, child);
139 child->max_prio = UINT32_MAX;
142 TAILQ_FOREACH(parent, list, node) {
143 /* Count group of children of same priority i.e are RR */
144 rr_num = nix_tm_check_rr(nix, parent->id, tree, &rr_prio,
147 /* Assuming that multiple RR groups are
148 * not configured based on capability.
150 parent->rr_prio = rr_prio;
151 parent->rr_num = rr_num;
152 parent->max_prio = max_prio;
159 nix_tm_node_add(struct roc_nix *roc_nix, struct nix_tm_node *node)
161 struct nix *nix = roc_nix_to_nix_priv(roc_nix);
162 struct nix_tm_shaper_profile *profile;
163 uint32_t node_id, parent_id, lvl;
164 struct nix_tm_node *parent_node;
165 uint32_t priority, profile_id;
166 uint8_t hw_lvl, exp_next_lvl;
167 enum roc_nix_tm_tree tree;
171 priority = node->priority;
172 parent_id = node->parent_id;
173 profile_id = node->shaper_profile_id;
177 plt_tm_dbg("Add node %s lvl %u id %u, prio 0x%x weight 0x%x "
178 "parent %u profile 0x%x tree %u",
179 nix_tm_hwlvl2str(nix_tm_lvl2nix(nix, lvl)), lvl, node_id,
180 priority, node->weight, parent_id, profile_id, tree);
182 if (tree >= ROC_NIX_TM_TREE_MAX)
183 return NIX_ERR_PARAM;
185 /* Translate sw level id's to nix hw level id's */
186 hw_lvl = nix_tm_lvl2nix(nix, lvl);
187 if (hw_lvl == NIX_TXSCH_LVL_CNT && !nix_tm_is_leaf(nix, lvl))
188 return NIX_ERR_TM_INVALID_LVL;
190 /* Leaf nodes have to be same priority */
191 if (nix_tm_is_leaf(nix, lvl) && priority != 0)
192 return NIX_ERR_TM_INVALID_PRIO;
194 parent_node = nix_tm_node_search(nix, parent_id, tree);
196 if (node_id < nix->nb_tx_queues)
197 exp_next_lvl = NIX_TXSCH_LVL_SMQ;
199 exp_next_lvl = hw_lvl + 1;
201 /* Check if there is no parent node yet */
202 if (hw_lvl != nix->tm_root_lvl &&
203 (!parent_node || parent_node->hw_lvl != exp_next_lvl))
204 return NIX_ERR_TM_INVALID_PARENT;
206 /* Check if a node already exists */
207 if (nix_tm_node_search(nix, node_id, tree))
208 return NIX_ERR_TM_NODE_EXISTS;
210 profile = nix_tm_shaper_profile_search(nix, profile_id);
211 if (!nix_tm_is_leaf(nix, lvl)) {
212 /* Check if shaper profile exists for non leaf node */
213 if (!profile && profile_id != ROC_NIX_TM_SHAPER_PROFILE_NONE)
214 return NIX_ERR_TM_INVALID_SHAPER_PROFILE;
216 /* Packet mode in profile should match with that of tm node */
217 if (profile && profile->pkt_mode != node->pkt_mode)
218 return NIX_ERR_TM_PKT_MODE_MISMATCH;
221 /* Check if there is second DWRR already in siblings or holes in prio */
222 rc = nix_tm_validate_prio(nix, lvl, parent_id, priority, tree);
226 if (node->weight > ROC_NIX_TM_MAX_SCHED_WT)
227 return NIX_ERR_TM_WEIGHT_EXCEED;
229 /* Maintain minimum weight */
233 node->hw_lvl = nix_tm_lvl2nix(nix, lvl);
235 node->max_prio = UINT32_MAX;
236 node->hw_id = NIX_TM_HW_ID_INVALID;
242 node->parent = parent_node;
244 parent_node->child_realloc = true;
245 node->parent_hw_id = NIX_TM_HW_ID_INVALID;
247 TAILQ_INSERT_TAIL(&nix->trees[tree], node, node);
248 plt_tm_dbg("Added node %s lvl %u id %u (%p)",
249 nix_tm_hwlvl2str(node->hw_lvl), lvl, node_id, node);
254 nix_tm_clear_path_xoff(struct nix *nix, struct nix_tm_node *node)
256 struct mbox *mbox = (&nix->dev)->mbox;
257 struct nix_txschq_config *req;
258 struct nix_tm_node *p;
261 /* Enable nodes in path for flush to succeed */
262 if (!nix_tm_is_leaf(nix, node->lvl))
267 if (!(p->flags & NIX_TM_NODE_ENABLED) &&
268 (p->flags & NIX_TM_NODE_HWRES)) {
269 req = mbox_alloc_msg_nix_txschq_cfg(mbox);
270 req->lvl = p->hw_lvl;
271 req->num_regs = nix_tm_sw_xoff_prep(p, false, req->reg,
273 rc = mbox_process(mbox);
277 p->flags |= NIX_TM_NODE_ENABLED;
286 nix_tm_smq_xoff(struct nix *nix, struct nix_tm_node *node, bool enable)
288 struct mbox *mbox = (&nix->dev)->mbox;
289 struct nix_txschq_config *req;
294 plt_tm_dbg("Setting SMQ %u XOFF/FLUSH to %s", smq,
295 enable ? "enable" : "disable");
297 rc = nix_tm_clear_path_xoff(nix, node);
301 req = mbox_alloc_msg_nix_txschq_cfg(mbox);
302 req->lvl = NIX_TXSCH_LVL_SMQ;
305 req->reg[0] = NIX_AF_SMQX_CFG(smq);
306 req->regval[0] = enable ? (BIT_ULL(50) | BIT_ULL(49)) : 0;
307 req->regval_mask[0] =
308 enable ? ~(BIT_ULL(50) | BIT_ULL(49)) : ~BIT_ULL(50);
310 return mbox_process(mbox);
314 nix_tm_leaf_data_get(struct nix *nix, uint16_t sq, uint32_t *rr_quantum,
317 struct nix_tm_node *node;
320 node = nix_tm_node_search(nix, sq, nix->tm_tree);
322 /* Check if we found a valid leaf node */
323 if (!node || !nix_tm_is_leaf(nix, node->lvl) || !node->parent ||
324 node->parent->hw_id == NIX_TM_HW_ID_INVALID) {
328 /* Get SMQ Id of leaf node's parent */
329 *smq = node->parent->hw_id;
330 *rr_quantum = nix_tm_weight_to_rr_quantum(node->weight);
332 rc = nix_tm_smq_xoff(nix, node->parent, false);
335 node->flags |= NIX_TM_NODE_ENABLED;
340 roc_nix_tm_sq_flush_spin(struct roc_nix_sq *sq)
342 struct nix *nix = roc_nix_to_nix_priv(sq->roc_nix);
343 uint16_t sqb_cnt, head_off, tail_off;
344 uint64_t wdata, val, prev;
345 uint16_t qid = sq->qid;
347 uint64_t timeout; /* 10's of usec */
349 /* Wait for enough time based on shaper min rate */
350 timeout = (sq->nb_desc * roc_nix_max_pkt_len(sq->roc_nix) * 8 * 1E5);
351 /* Wait for worst case scenario of this SQ being last priority
352 * and so have to wait for all other SQ's drain out by their own.
354 timeout = timeout * nix->nb_tx_queues;
355 timeout = timeout / nix->tm_rate_min;
359 wdata = ((uint64_t)qid << 32);
360 regaddr = (int64_t *)(nix->base + NIX_LF_SQ_OP_STATUS);
361 val = roc_atomic64_add_nosync(wdata, regaddr);
363 /* Spin multiple iterations as "sq->fc_cache_pkts" can still
364 * have space to send pkts even though fc_mem is disabled
370 val = roc_atomic64_add_nosync(wdata, regaddr);
371 /* Continue on error */
372 if (val & BIT_ULL(63))
378 sqb_cnt = val & 0xFFFF;
379 head_off = (val >> 20) & 0x3F;
380 tail_off = (val >> 28) & 0x3F;
382 /* SQ reached quiescent state */
383 if (sqb_cnt <= 1 && head_off == tail_off &&
384 (*(volatile uint64_t *)sq->fc == sq->nb_sqb_bufs)) {
396 roc_nix_queues_ctx_dump(sq->roc_nix);
400 /* Flush and disable tx queue and its parent SMQ */
402 nix_tm_sq_flush_pre(struct roc_nix_sq *sq)
404 struct roc_nix *roc_nix = sq->roc_nix;
405 struct nix_tm_node *node, *sibling;
406 struct nix_tm_node_list *list;
407 enum roc_nix_tm_tree tree;
413 nix = roc_nix_to_nix_priv(roc_nix);
415 /* Need not do anything if tree is in disabled state */
416 if (!(nix->tm_flags & NIX_TM_HIERARCHY_ENA))
419 mbox = (&nix->dev)->mbox;
423 list = nix_tm_node_list(nix, tree);
425 /* Find the node for this SQ */
426 node = nix_tm_node_search(nix, qid, tree);
427 if (!node || !(node->flags & NIX_TM_NODE_ENABLED)) {
428 plt_err("Invalid node/state for sq %u", qid);
432 /* Enable CGX RXTX to drain pkts */
433 if (!roc_nix->io_enabled) {
434 /* Though it enables both RX MCAM Entries and CGX Link
435 * we assume all the rx queues are stopped way back.
437 mbox_alloc_msg_nix_lf_start_rx(mbox);
438 rc = mbox_process(mbox);
440 plt_err("cgx start failed, rc=%d", rc);
445 /* Disable smq xoff for case it was enabled earlier */
446 rc = nix_tm_smq_xoff(nix, node->parent, false);
448 plt_err("Failed to enable smq %u, rc=%d", node->parent->hw_id,
453 /* As per HRM, to disable an SQ, all other SQ's
454 * that feed to same SMQ must be paused before SMQ flush.
456 TAILQ_FOREACH(sibling, list, node) {
457 if (sibling->parent != node->parent)
459 if (!(sibling->flags & NIX_TM_NODE_ENABLED))
467 rc = roc_nix_tm_sq_aura_fc(sq, false);
469 plt_err("Failed to disable sqb aura fc, rc=%d", rc);
473 /* Wait for sq entries to be flushed */
474 rc = roc_nix_tm_sq_flush_spin(sq);
476 plt_err("Failed to drain sq %u, rc=%d\n", sq->qid, rc);
481 node->flags &= ~NIX_TM_NODE_ENABLED;
483 /* Disable and flush */
484 rc = nix_tm_smq_xoff(nix, node->parent, true);
486 plt_err("Failed to disable smq %u, rc=%d", node->parent->hw_id,
491 /* Restore cgx state */
492 if (!roc_nix->io_enabled) {
493 mbox_alloc_msg_nix_lf_stop_rx(mbox);
494 rc |= mbox_process(mbox);
501 nix_tm_sq_flush_post(struct roc_nix_sq *sq)
503 struct roc_nix *roc_nix = sq->roc_nix;
504 struct nix_tm_node *node, *sibling;
505 struct nix_tm_node_list *list;
506 enum roc_nix_tm_tree tree;
507 struct roc_nix_sq *s_sq;
513 nix = roc_nix_to_nix_priv(roc_nix);
515 /* Need not do anything if tree is in disabled state */
516 if (!(nix->tm_flags & NIX_TM_HIERARCHY_ENA))
521 list = nix_tm_node_list(nix, tree);
523 /* Find the node for this SQ */
524 node = nix_tm_node_search(nix, qid, tree);
526 plt_err("Invalid node for sq %u", qid);
530 /* Enable all the siblings back */
531 TAILQ_FOREACH(sibling, list, node) {
532 if (sibling->parent != node->parent)
535 if (sibling->id == qid)
538 if (!(sibling->flags & NIX_TM_NODE_ENABLED))
542 s_sq = nix->sqs[s_qid];
547 /* Enable back if any SQ is still present */
548 rc = nix_tm_smq_xoff(nix, node->parent, false);
550 plt_err("Failed to enable smq %u, rc=%d",
551 node->parent->hw_id, rc);
557 rc = roc_nix_tm_sq_aura_fc(s_sq, true);
559 plt_err("Failed to enable sqb aura fc, rc=%d", rc);
568 nix_tm_sq_sched_conf(struct nix *nix, struct nix_tm_node *node,
569 bool rr_quantum_only)
571 struct mbox *mbox = (&nix->dev)->mbox;
572 uint16_t qid = node->id, smq;
576 smq = node->parent->hw_id;
577 rr_quantum = nix_tm_weight_to_rr_quantum(node->weight);
580 plt_tm_dbg("Update sq(%u) rr_quantum 0x%" PRIx64, qid,
583 plt_tm_dbg("Enabling sq(%u)->smq(%u), rr_quantum 0x%" PRIx64,
584 qid, smq, rr_quantum);
586 if (qid > nix->nb_tx_queues)
589 if (roc_model_is_cn9k()) {
590 struct nix_aq_enq_req *aq;
592 aq = mbox_alloc_msg_nix_aq_enq(mbox);
594 aq->ctype = NIX_AQ_CTYPE_SQ;
595 aq->op = NIX_AQ_INSTOP_WRITE;
597 /* smq update only when needed */
598 if (!rr_quantum_only) {
600 aq->sq_mask.smq = ~aq->sq_mask.smq;
602 aq->sq.smq_rr_quantum = rr_quantum;
603 aq->sq_mask.smq_rr_quantum = ~aq->sq_mask.smq_rr_quantum;
605 struct nix_cn10k_aq_enq_req *aq;
607 aq = mbox_alloc_msg_nix_cn10k_aq_enq(mbox);
609 aq->ctype = NIX_AQ_CTYPE_SQ;
610 aq->op = NIX_AQ_INSTOP_WRITE;
612 /* smq update only when needed */
613 if (!rr_quantum_only) {
615 aq->sq_mask.smq = ~aq->sq_mask.smq;
617 aq->sq.smq_rr_weight = rr_quantum;
618 aq->sq_mask.smq_rr_weight = ~aq->sq_mask.smq_rr_weight;
621 rc = mbox_process(mbox);
623 plt_err("Failed to set smq, rc=%d", rc);
628 nix_tm_release_resources(struct nix *nix, uint8_t hw_lvl, bool contig,
631 uint16_t avail, thresh, to_free = 0, schq;
632 struct mbox *mbox = (&nix->dev)->mbox;
633 struct nix_txsch_free_req *req;
634 struct plt_bitmap *bmp;
639 bmp = contig ? nix->schq_contig_bmp[hw_lvl] : nix->schq_bmp[hw_lvl];
641 contig ? nix->contig_rsvd[hw_lvl] : nix->discontig_rsvd[hw_lvl];
642 plt_bitmap_scan_init(bmp);
644 avail = nix_tm_resource_avail(nix, hw_lvl, contig);
647 /* Release only above threshold */
649 to_free = avail - thresh;
651 /* Release everything */
655 /* Now release resources to AF */
657 if (!slab && !plt_bitmap_scan(bmp, &pos, &slab))
660 schq = bitmap_ctzll(slab);
661 slab &= ~(1ULL << schq);
665 req = mbox_alloc_msg_nix_txsch_free(mbox);
669 req->schq_lvl = hw_lvl;
671 rc = mbox_process(mbox);
673 plt_err("failed to release hwres %s(%u) rc %d",
674 nix_tm_hwlvl2str(hw_lvl), schq, rc);
678 plt_tm_dbg("Released hwres %s(%u)", nix_tm_hwlvl2str(hw_lvl),
680 plt_bitmap_clear(bmp, schq);
685 plt_err("resource inconsistency for %s(%u)",
686 nix_tm_hwlvl2str(hw_lvl), contig);
693 nix_tm_free_node_resource(struct nix *nix, struct nix_tm_node *node)
695 struct mbox *mbox = (&nix->dev)->mbox;
696 struct nix_txsch_free_req *req;
697 struct plt_bitmap *bmp;
698 uint16_t avail, hw_id;
702 hw_lvl = node->hw_lvl;
704 bmp = nix->schq_bmp[hw_lvl];
705 /* Free specific HW resource */
706 plt_tm_dbg("Free hwres %s(%u) lvl %u id %u (%p)",
707 nix_tm_hwlvl2str(node->hw_lvl), hw_id, node->lvl, node->id,
710 avail = nix_tm_resource_avail(nix, hw_lvl, false);
711 /* Always for now free to discontiguous queue when avail
714 if (nix->discontig_rsvd[hw_lvl] &&
715 avail < nix->discontig_rsvd[hw_lvl]) {
716 PLT_ASSERT(hw_id < NIX_TM_MAX_HW_TXSCHQ);
717 PLT_ASSERT(plt_bitmap_get(bmp, hw_id) == 0);
718 plt_bitmap_set(bmp, hw_id);
719 node->hw_id = NIX_TM_HW_ID_INVALID;
720 node->flags &= ~NIX_TM_NODE_HWRES;
725 req = mbox_alloc_msg_nix_txsch_free(mbox);
729 req->schq_lvl = node->hw_lvl;
731 rc = mbox_process(mbox);
733 plt_err("failed to release hwres %s(%u) rc %d",
734 nix_tm_hwlvl2str(node->hw_lvl), hw_id, rc);
738 /* Mark parent as dirty for reallocing it's children */
740 node->parent->child_realloc = true;
742 node->hw_id = NIX_TM_HW_ID_INVALID;
743 node->flags &= ~NIX_TM_NODE_HWRES;
744 plt_tm_dbg("Released hwres %s(%u) to af",
745 nix_tm_hwlvl2str(node->hw_lvl), hw_id);
750 nix_tm_node_delete(struct roc_nix *roc_nix, uint32_t node_id,
751 enum roc_nix_tm_tree tree, bool free)
753 struct nix *nix = roc_nix_to_nix_priv(roc_nix);
754 struct nix_tm_shaper_profile *profile;
755 struct nix_tm_node *node, *child;
756 struct nix_tm_node_list *list;
760 plt_tm_dbg("Delete node id %u tree %u", node_id, tree);
762 node = nix_tm_node_search(nix, node_id, tree);
764 return NIX_ERR_TM_INVALID_NODE;
766 list = nix_tm_node_list(nix, tree);
767 /* Check for any existing children */
768 TAILQ_FOREACH(child, list, node) {
769 if (child->parent == node)
770 return NIX_ERR_TM_CHILD_EXISTS;
773 /* Remove shaper profile reference */
774 profile_id = node->shaper_profile_id;
775 profile = nix_tm_shaper_profile_search(nix, profile_id);
777 /* Free hw resource locally */
778 if (node->flags & NIX_TM_NODE_HWRES) {
779 rc = nix_tm_free_node_resource(nix, node);
787 TAILQ_REMOVE(list, node, node);
789 plt_tm_dbg("Deleted node %s lvl %u id %u, prio 0x%x weight 0x%x "
790 "parent %u profile 0x%x tree %u (%p)",
791 nix_tm_hwlvl2str(node->hw_lvl), node->lvl, node->id,
792 node->priority, node->weight,
793 node->parent ? node->parent->id : UINT32_MAX,
794 node->shaper_profile_id, tree, node);
795 /* Free only if requested */
797 nix_tm_node_free(node);
802 nix_tm_assign_hw_id(struct nix *nix, struct nix_tm_node *parent,
803 uint16_t *contig_id, int *contig_cnt,
804 struct nix_tm_node_list *list)
806 struct nix_tm_node *child;
807 struct plt_bitmap *bmp;
808 uint8_t child_hw_lvl;
814 child_hw_lvl = parent->hw_lvl - 1;
815 bmp = nix->schq_bmp[child_hw_lvl];
816 plt_bitmap_scan_init(bmp);
819 /* Save spare schq if it is case of RR + SP */
820 if (parent->rr_prio != 0xf && *contig_cnt > 1)
821 spare_schq = *contig_id + parent->rr_prio;
823 TAILQ_FOREACH(child, list, node) {
826 if (child->parent->id != parent->id)
829 /* Resource never expected to be present */
830 if (child->flags & NIX_TM_NODE_HWRES) {
831 plt_err("Resource exists for child (%s)%u, id %u (%p)",
832 nix_tm_hwlvl2str(child->hw_lvl), child->hw_id,
838 plt_bitmap_scan(bmp, &pos, &slab);
840 if (child->priority == parent->rr_prio && spare_schq != -1) {
841 /* Use spare schq first if present */
844 *contig_cnt = *contig_cnt - 1;
846 } else if (child->priority == parent->rr_prio) {
847 /* Assign a discontiguous queue */
849 plt_err("Schq not found for Child %u "
851 child->id, child->lvl, child);
855 schq = bitmap_ctzll(slab);
856 slab &= ~(1ULL << schq);
858 plt_bitmap_clear(bmp, schq);
860 /* Assign a contiguous queue */
861 schq = *contig_id + child->priority;
862 *contig_cnt = *contig_cnt - 1;
865 plt_tm_dbg("Resource %s(%u), for lvl %u id %u(%p)",
866 nix_tm_hwlvl2str(child->hw_lvl), schq, child->lvl,
870 child->parent_hw_id = parent->hw_id;
871 child->flags |= NIX_TM_NODE_HWRES;
878 nix_tm_assign_resources(struct nix *nix, enum roc_nix_tm_tree tree)
880 struct nix_tm_node *parent, *root = NULL;
881 struct plt_bitmap *bmp, *bmp_contig;
882 struct nix_tm_node_list *list;
883 uint8_t child_hw_lvl, hw_lvl;
884 uint16_t contig_id, j;
889 list = nix_tm_node_list(nix, tree);
890 /* Walk from TL1 to TL4 parents */
891 for (hw_lvl = NIX_TXSCH_LVL_TL1; hw_lvl > 0; hw_lvl--) {
892 TAILQ_FOREACH(parent, list, node) {
893 child_hw_lvl = parent->hw_lvl - 1;
894 if (parent->hw_lvl != hw_lvl)
897 /* Remember root for future */
898 if (parent->hw_lvl == nix->tm_root_lvl)
901 if (!parent->child_realloc) {
902 /* Skip when parent is not dirty */
903 if (nix_tm_child_res_valid(list, parent))
905 plt_err("Parent not dirty but invalid "
906 "child res parent id %u(lvl %u)",
907 parent->id, parent->lvl);
911 bmp_contig = nix->schq_contig_bmp[child_hw_lvl];
913 /* Prealloc contiguous indices for a parent */
914 contig_id = NIX_TM_MAX_HW_TXSCHQ;
915 cnt = (int)parent->max_prio + 1;
917 plt_bitmap_scan_init(bmp_contig);
918 if (!plt_bitmap_scan(bmp_contig, &pos, &slab)) {
919 plt_err("Contig schq not found");
922 contig_id = pos + bitmap_ctzll(slab);
924 /* Check if we have enough */
925 for (j = contig_id; j < contig_id + cnt; j++) {
926 if (!plt_bitmap_get(bmp_contig, j))
930 if (j != contig_id + cnt) {
931 plt_err("Contig schq not sufficient");
935 for (j = contig_id; j < contig_id + cnt; j++)
936 plt_bitmap_clear(bmp_contig, j);
939 /* Assign hw id to all children */
940 rc = nix_tm_assign_hw_id(nix, parent, &contig_id, &cnt,
943 plt_err("Unexpected err, contig res alloc, "
944 "parent %u, of %s, rc=%d, cnt=%d",
945 parent->id, nix_tm_hwlvl2str(hw_lvl),
950 /* Clear the dirty bit as children's
951 * resources are reallocated.
953 parent->child_realloc = false;
957 /* Root is always expected to be there */
961 if (root->flags & NIX_TM_NODE_HWRES)
964 /* Process root node */
965 bmp = nix->schq_bmp[nix->tm_root_lvl];
966 plt_bitmap_scan_init(bmp);
967 if (!plt_bitmap_scan(bmp, &pos, &slab)) {
968 plt_err("Resource not allocated for root");
972 root->hw_id = pos + bitmap_ctzll(slab);
973 root->flags |= NIX_TM_NODE_HWRES;
974 plt_bitmap_clear(bmp, root->hw_id);
976 /* Get TL1 id as well when root is not TL1 */
977 if (!nix_tm_have_tl1_access(nix)) {
978 bmp = nix->schq_bmp[NIX_TXSCH_LVL_TL1];
980 plt_bitmap_scan_init(bmp);
981 if (!plt_bitmap_scan(bmp, &pos, &slab)) {
982 plt_err("Resource not found for TL1");
985 root->parent_hw_id = pos + bitmap_ctzll(slab);
986 plt_bitmap_clear(bmp, root->parent_hw_id);
989 plt_tm_dbg("Resource %s(%u) for root(id %u) (%p)",
990 nix_tm_hwlvl2str(root->hw_lvl), root->hw_id, root->id, root);
996 nix_tm_copy_rsp_to_nix(struct nix *nix, struct nix_txsch_alloc_rsp *rsp)
1001 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1002 for (i = 0; i < rsp->schq[lvl]; i++)
1003 plt_bitmap_set(nix->schq_bmp[lvl],
1004 rsp->schq_list[lvl][i]);
1006 for (i = 0; i < rsp->schq_contig[lvl]; i++)
1007 plt_bitmap_set(nix->schq_contig_bmp[lvl],
1008 rsp->schq_contig_list[lvl][i]);
1013 nix_tm_alloc_txschq(struct nix *nix, enum roc_nix_tm_tree tree)
1015 uint16_t schq_contig[NIX_TXSCH_LVL_CNT];
1016 struct mbox *mbox = (&nix->dev)->mbox;
1017 uint16_t schq[NIX_TXSCH_LVL_CNT];
1018 struct nix_txsch_alloc_req *req;
1019 struct nix_txsch_alloc_rsp *rsp;
1024 memset(schq, 0, sizeof(schq));
1025 memset(schq_contig, 0, sizeof(schq_contig));
1027 /* Estimate requirement */
1028 rc = nix_tm_resource_estimate(nix, schq_contig, schq, tree);
1032 /* Release existing contiguous resources when realloc requested
1033 * as there is no way to guarantee continuity of old with new.
1035 for (hw_lvl = 0; hw_lvl < NIX_TXSCH_LVL_CNT; hw_lvl++) {
1036 if (schq_contig[hw_lvl])
1037 nix_tm_release_resources(nix, hw_lvl, true, false);
1040 /* Alloc as needed */
1043 req = mbox_alloc_msg_nix_txsch_alloc(mbox);
1048 mbox_memcpy(req->schq, schq, sizeof(req->schq));
1049 mbox_memcpy(req->schq_contig, schq_contig,
1050 sizeof(req->schq_contig));
1052 /* Each alloc can be at max of MAX_TXSCHQ_PER_FUNC per level.
1053 * So split alloc to multiple requests.
1055 for (i = 0; i < NIX_TXSCH_LVL_CNT; i++) {
1056 if (req->schq[i] > MAX_TXSCHQ_PER_FUNC)
1057 req->schq[i] = MAX_TXSCHQ_PER_FUNC;
1058 schq[i] -= req->schq[i];
1060 if (req->schq_contig[i] > MAX_TXSCHQ_PER_FUNC)
1061 req->schq_contig[i] = MAX_TXSCHQ_PER_FUNC;
1062 schq_contig[i] -= req->schq_contig[i];
1064 if (schq[i] || schq_contig[i])
1068 rc = mbox_process_msg(mbox, (void *)&rsp);
1072 nix_tm_copy_rsp_to_nix(nix, rsp);
1075 nix->tm_link_cfg_lvl = rsp->link_cfg_lvl;
1078 for (i = 0; i < NIX_TXSCH_LVL_CNT; i++) {
1079 if (nix_tm_release_resources(nix, i, true, false))
1080 plt_err("Failed to release contig resources of "
1083 if (nix_tm_release_resources(nix, i, false, false))
1084 plt_err("Failed to release discontig resources of "
1092 nix_tm_prepare_default_tree(struct roc_nix *roc_nix)
1094 struct nix *nix = roc_nix_to_nix_priv(roc_nix);
1095 uint32_t nonleaf_id = nix->nb_tx_queues;
1096 struct nix_tm_node *node = NULL;
1097 uint8_t leaf_lvl, lvl, lvl_end;
1101 /* Add ROOT, SCH1, SCH2, SCH3, [SCH4] nodes */
1102 parent = ROC_NIX_TM_NODE_ID_INVALID;
1103 /* With TL1 access we have an extra level */
1104 lvl_end = (nix_tm_have_tl1_access(nix) ? ROC_TM_LVL_SCH4 :
1107 for (lvl = ROC_TM_LVL_ROOT; lvl <= lvl_end; lvl++) {
1109 node = nix_tm_node_alloc();
1113 node->id = nonleaf_id;
1114 node->parent_id = parent;
1116 node->weight = NIX_TM_DFLT_RR_WT;
1117 node->shaper_profile_id = ROC_NIX_TM_SHAPER_PROFILE_NONE;
1119 node->tree = ROC_NIX_TM_DEFAULT;
1121 rc = nix_tm_node_add(roc_nix, node);
1124 parent = nonleaf_id;
1128 parent = nonleaf_id - 1;
1129 leaf_lvl = (nix_tm_have_tl1_access(nix) ? ROC_TM_LVL_QUEUE :
1132 /* Add leaf nodes */
1133 for (i = 0; i < nix->nb_tx_queues; i++) {
1135 node = nix_tm_node_alloc();
1140 node->parent_id = parent;
1142 node->weight = NIX_TM_DFLT_RR_WT;
1143 node->shaper_profile_id = ROC_NIX_TM_SHAPER_PROFILE_NONE;
1144 node->lvl = leaf_lvl;
1145 node->tree = ROC_NIX_TM_DEFAULT;
1147 rc = nix_tm_node_add(roc_nix, node);
1154 nix_tm_node_free(node);
1159 nix_tm_prepare_rate_limited_tree(struct roc_nix *roc_nix)
1161 struct nix *nix = roc_nix_to_nix_priv(roc_nix);
1162 uint32_t nonleaf_id = nix->nb_tx_queues;
1163 struct nix_tm_node *node = NULL;
1164 uint8_t leaf_lvl, lvl, lvl_end;
1168 /* Add ROOT, SCH1, SCH2 nodes */
1169 parent = ROC_NIX_TM_NODE_ID_INVALID;
1170 lvl_end = (nix_tm_have_tl1_access(nix) ? ROC_TM_LVL_SCH3 :
1173 for (lvl = ROC_TM_LVL_ROOT; lvl <= lvl_end; lvl++) {
1175 node = nix_tm_node_alloc();
1179 node->id = nonleaf_id;
1180 node->parent_id = parent;
1182 node->weight = NIX_TM_DFLT_RR_WT;
1183 node->shaper_profile_id = ROC_NIX_TM_SHAPER_PROFILE_NONE;
1185 node->tree = ROC_NIX_TM_RLIMIT;
1187 rc = nix_tm_node_add(roc_nix, node);
1190 parent = nonleaf_id;
1194 /* SMQ is mapped to SCH4 when we have TL1 access and SCH3 otherwise */
1195 lvl = (nix_tm_have_tl1_access(nix) ? ROC_TM_LVL_SCH4 : ROC_TM_LVL_SCH3);
1197 /* Add per queue SMQ nodes i.e SCH4 / SCH3 */
1198 for (i = 0; i < nix->nb_tx_queues; i++) {
1200 node = nix_tm_node_alloc();
1204 node->id = nonleaf_id + i;
1205 node->parent_id = parent;
1207 node->weight = NIX_TM_DFLT_RR_WT;
1208 node->shaper_profile_id = ROC_NIX_TM_SHAPER_PROFILE_NONE;
1210 node->tree = ROC_NIX_TM_RLIMIT;
1212 rc = nix_tm_node_add(roc_nix, node);
1217 parent = nonleaf_id;
1218 leaf_lvl = (nix_tm_have_tl1_access(nix) ? ROC_TM_LVL_QUEUE :
1221 /* Add leaf nodes */
1222 for (i = 0; i < nix->nb_tx_queues; i++) {
1224 node = nix_tm_node_alloc();
1229 node->parent_id = parent;
1231 node->weight = NIX_TM_DFLT_RR_WT;
1232 node->shaper_profile_id = ROC_NIX_TM_SHAPER_PROFILE_NONE;
1233 node->lvl = leaf_lvl;
1234 node->tree = ROC_NIX_TM_RLIMIT;
1236 rc = nix_tm_node_add(roc_nix, node);
1243 nix_tm_node_free(node);
1248 nix_tm_free_resources(struct roc_nix *roc_nix, uint32_t tree_mask, bool hw_only)
1250 struct nix *nix = roc_nix_to_nix_priv(roc_nix);
1251 struct nix_tm_shaper_profile *profile;
1252 struct nix_tm_node *node, *next_node;
1253 struct nix_tm_node_list *list;
1254 enum roc_nix_tm_tree tree;
1255 uint32_t profile_id;
1258 for (tree = 0; tree < ROC_NIX_TM_TREE_MAX; tree++) {
1259 if (!(tree_mask & BIT(tree)))
1262 plt_tm_dbg("Freeing resources of tree %u", tree);
1264 list = nix_tm_node_list(nix, tree);
1265 next_node = TAILQ_FIRST(list);
1268 next_node = TAILQ_NEXT(node, node);
1270 if (!nix_tm_is_leaf(nix, node->lvl) &&
1271 node->flags & NIX_TM_NODE_HWRES) {
1272 /* Clear xoff in path for flush to succeed */
1273 rc = nix_tm_clear_path_xoff(nix, node);
1276 rc = nix_tm_free_node_resource(nix, node);
1282 /* Leave software elements if needed */
1286 next_node = TAILQ_FIRST(list);
1289 next_node = TAILQ_NEXT(node, node);
1291 plt_tm_dbg("Free node lvl %u id %u (%p)", node->lvl,
1294 profile_id = node->shaper_profile_id;
1295 profile = nix_tm_shaper_profile_search(nix, profile_id);
1299 TAILQ_REMOVE(list, node, node);
1300 nix_tm_node_free(node);
1307 nix_tm_conf_init(struct roc_nix *roc_nix)
1309 struct nix *nix = roc_nix_to_nix_priv(roc_nix);
1310 uint32_t bmp_sz, hw_lvl;
1314 PLT_STATIC_ASSERT(sizeof(struct nix_tm_node) <= ROC_NIX_TM_NODE_SZ);
1315 PLT_STATIC_ASSERT(sizeof(struct nix_tm_shaper_profile) <=
1316 ROC_NIX_TM_SHAPER_PROFILE_SZ);
1319 for (i = 0; i < ROC_NIX_TM_TREE_MAX; i++)
1320 TAILQ_INIT(&nix->trees[i]);
1322 TAILQ_INIT(&nix->shaper_profile_list);
1323 nix->tm_rate_min = 1E9; /* 1Gbps */
1326 bmp_sz = plt_bitmap_get_memory_footprint(NIX_TM_MAX_HW_TXSCHQ);
1327 bmp_mem = plt_zmalloc(bmp_sz * NIX_TXSCH_LVL_CNT * 2, 0);
1330 nix->schq_bmp_mem = bmp_mem;
1332 /* Init contiguous and discontiguous bitmap per lvl */
1334 for (hw_lvl = 0; hw_lvl < NIX_TXSCH_LVL_CNT; hw_lvl++) {
1335 /* Bitmap for discontiguous resource */
1336 nix->schq_bmp[hw_lvl] =
1337 plt_bitmap_init(NIX_TM_MAX_HW_TXSCHQ, bmp_mem, bmp_sz);
1338 if (!nix->schq_bmp[hw_lvl])
1341 bmp_mem = PLT_PTR_ADD(bmp_mem, bmp_sz);
1343 /* Bitmap for contiguous resource */
1344 nix->schq_contig_bmp[hw_lvl] =
1345 plt_bitmap_init(NIX_TM_MAX_HW_TXSCHQ, bmp_mem, bmp_sz);
1346 if (!nix->schq_contig_bmp[hw_lvl])
1349 bmp_mem = PLT_PTR_ADD(bmp_mem, bmp_sz);
1352 /* Disable TL1 Static Priority when VF's are enabled
1353 * as otherwise VF's TL2 reallocation will be needed
1354 * runtime to support a specific topology of PF.
1356 if (nix->pci_dev->max_vfs)
1357 nix->tm_flags |= NIX_TM_TL1_NO_SP;
1359 /* TL1 access is only for PF's */
1360 if (roc_nix_is_pf(roc_nix)) {
1361 nix->tm_flags |= NIX_TM_TL1_ACCESS;
1362 nix->tm_root_lvl = NIX_TXSCH_LVL_TL1;
1364 nix->tm_root_lvl = NIX_TXSCH_LVL_TL2;
1369 nix_tm_conf_fini(roc_nix);
1374 nix_tm_conf_fini(struct roc_nix *roc_nix)
1376 struct nix *nix = roc_nix_to_nix_priv(roc_nix);
1379 for (hw_lvl = 0; hw_lvl < NIX_TXSCH_LVL_CNT; hw_lvl++) {
1380 plt_bitmap_free(nix->schq_bmp[hw_lvl]);
1381 plt_bitmap_free(nix->schq_contig_bmp[hw_lvl]);
1383 plt_free(nix->schq_bmp_mem);