1b7d3a0feSSunil Kumar Kori /* SPDX-License-Identifier: BSD-3-Clause
2b7d3a0feSSunil Kumar Kori * Copyright(C) 2022 Marvell International Ltd.
3b7d3a0feSSunil Kumar Kori */
4b7d3a0feSSunil Kumar Kori
5b7d3a0feSSunil Kumar Kori #include "cnxk_ethdev.h"
6b7d3a0feSSunil Kumar Kori
7b7d3a0feSSunil Kumar Kori #define CNXK_NIX_CMAN_RED_MIN_THRESH 75
8b7d3a0feSSunil Kumar Kori #define CNXK_NIX_CMAN_RED_MAX_THRESH 95
9b7d3a0feSSunil Kumar Kori
10b7d3a0feSSunil Kumar Kori int
cnxk_nix_cman_info_get(struct rte_eth_dev * dev,struct rte_eth_cman_info * info)11b7d3a0feSSunil Kumar Kori cnxk_nix_cman_info_get(struct rte_eth_dev *dev, struct rte_eth_cman_info *info)
12b7d3a0feSSunil Kumar Kori {
13b7d3a0feSSunil Kumar Kori RTE_SET_USED(dev);
14b7d3a0feSSunil Kumar Kori
15b7d3a0feSSunil Kumar Kori info->modes_supported = RTE_CMAN_RED;
16b7d3a0feSSunil Kumar Kori info->objs_supported = RTE_ETH_CMAN_OBJ_RX_QUEUE | RTE_ETH_CMAN_OBJ_RX_QUEUE_MEMPOOL;
17b7d3a0feSSunil Kumar Kori
18b7d3a0feSSunil Kumar Kori return 0;
19b7d3a0feSSunil Kumar Kori }
20b7d3a0feSSunil Kumar Kori
21b7d3a0feSSunil Kumar Kori int
cnxk_nix_cman_config_init(struct rte_eth_dev * dev,struct rte_eth_cman_config * config)22b7d3a0feSSunil Kumar Kori cnxk_nix_cman_config_init(struct rte_eth_dev *dev, struct rte_eth_cman_config *config)
23b7d3a0feSSunil Kumar Kori {
24b7d3a0feSSunil Kumar Kori RTE_SET_USED(dev);
25b7d3a0feSSunil Kumar Kori
26b7d3a0feSSunil Kumar Kori memset(config, 0, sizeof(struct rte_eth_cman_config));
27b7d3a0feSSunil Kumar Kori
28b7d3a0feSSunil Kumar Kori config->obj = RTE_ETH_CMAN_OBJ_RX_QUEUE;
29b7d3a0feSSunil Kumar Kori config->mode = RTE_CMAN_RED;
30b7d3a0feSSunil Kumar Kori config->mode_param.red.min_th = CNXK_NIX_CMAN_RED_MIN_THRESH;
31b7d3a0feSSunil Kumar Kori config->mode_param.red.max_th = CNXK_NIX_CMAN_RED_MAX_THRESH;
32b7d3a0feSSunil Kumar Kori return 0;
33b7d3a0feSSunil Kumar Kori }
34b7d3a0feSSunil Kumar Kori
35b7d3a0feSSunil Kumar Kori static int
nix_cman_config_validate(struct rte_eth_dev * eth_dev,const struct rte_eth_cman_config * config)36b7d3a0feSSunil Kumar Kori nix_cman_config_validate(struct rte_eth_dev *eth_dev, const struct rte_eth_cman_config *config)
37b7d3a0feSSunil Kumar Kori {
38b7d3a0feSSunil Kumar Kori struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
39b7d3a0feSSunil Kumar Kori struct rte_eth_cman_info info;
40b7d3a0feSSunil Kumar Kori
41b7d3a0feSSunil Kumar Kori memset(&info, 0, sizeof(struct rte_eth_cman_info));
42b7d3a0feSSunil Kumar Kori cnxk_nix_cman_info_get(eth_dev, &info);
43b7d3a0feSSunil Kumar Kori
44b7d3a0feSSunil Kumar Kori if (!(config->obj & info.objs_supported)) {
45b7d3a0feSSunil Kumar Kori plt_err("Invalid object");
46b7d3a0feSSunil Kumar Kori return -EINVAL;
47b7d3a0feSSunil Kumar Kori }
48b7d3a0feSSunil Kumar Kori
49b7d3a0feSSunil Kumar Kori if (!(config->mode & info.modes_supported)) {
50b7d3a0feSSunil Kumar Kori plt_err("Invalid mode");
51b7d3a0feSSunil Kumar Kori return -EINVAL;
52b7d3a0feSSunil Kumar Kori }
53b7d3a0feSSunil Kumar Kori
54b7d3a0feSSunil Kumar Kori if (config->obj_param.rx_queue >= dev->nb_rxq) {
55b7d3a0feSSunil Kumar Kori plt_err("Invalid queue ID. Queue = %u", config->obj_param.rx_queue);
56b7d3a0feSSunil Kumar Kori return -EINVAL;
57b7d3a0feSSunil Kumar Kori }
58b7d3a0feSSunil Kumar Kori
59b7d3a0feSSunil Kumar Kori if (config->mode_param.red.min_th > CNXK_NIX_CMAN_RED_MAX_THRESH) {
60b7d3a0feSSunil Kumar Kori plt_err("Invalid RED minimum threshold. min_th = %u",
61b7d3a0feSSunil Kumar Kori config->mode_param.red.min_th);
62b7d3a0feSSunil Kumar Kori return -EINVAL;
63b7d3a0feSSunil Kumar Kori }
64b7d3a0feSSunil Kumar Kori
65b7d3a0feSSunil Kumar Kori if (config->mode_param.red.max_th > CNXK_NIX_CMAN_RED_MAX_THRESH) {
66b7d3a0feSSunil Kumar Kori plt_err("Invalid RED maximum threshold. max_th = %u",
67b7d3a0feSSunil Kumar Kori config->mode_param.red.max_th);
68b7d3a0feSSunil Kumar Kori return -EINVAL;
69b7d3a0feSSunil Kumar Kori }
70b7d3a0feSSunil Kumar Kori
71*9fd66d79SSunil Kumar Kori if (config->mode_param.red.min_th > config->mode_param.red.max_th) {
72*9fd66d79SSunil Kumar Kori plt_err("RED minimum threshold must be less or equal to maximum threshold");
73*9fd66d79SSunil Kumar Kori return -EINVAL;
74*9fd66d79SSunil Kumar Kori }
75*9fd66d79SSunil Kumar Kori
76b7d3a0feSSunil Kumar Kori return 0;
77b7d3a0feSSunil Kumar Kori }
78b7d3a0feSSunil Kumar Kori
79b7d3a0feSSunil Kumar Kori int
cnxk_nix_cman_config_set(struct rte_eth_dev * eth_dev,const struct rte_eth_cman_config * config)80b7d3a0feSSunil Kumar Kori cnxk_nix_cman_config_set(struct rte_eth_dev *eth_dev, const struct rte_eth_cman_config *config)
81b7d3a0feSSunil Kumar Kori {
82b7d3a0feSSunil Kumar Kori struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
83b7d3a0feSSunil Kumar Kori struct roc_nix *nix = &dev->nix;
84b7d3a0feSSunil Kumar Kori uint8_t drop, pass, shift;
85b7d3a0feSSunil Kumar Kori uint8_t min_th, max_th;
86b7d3a0feSSunil Kumar Kori struct roc_nix_cq *cq;
87b7d3a0feSSunil Kumar Kori struct roc_nix_rq *rq;
88b7d3a0feSSunil Kumar Kori bool is_mempool;
89b7d3a0feSSunil Kumar Kori uint64_t buf_cnt;
90b7d3a0feSSunil Kumar Kori int rc;
91b7d3a0feSSunil Kumar Kori
92b7d3a0feSSunil Kumar Kori rc = nix_cman_config_validate(eth_dev, config);
93b7d3a0feSSunil Kumar Kori if (rc)
94b7d3a0feSSunil Kumar Kori return rc;
95b7d3a0feSSunil Kumar Kori
96b7d3a0feSSunil Kumar Kori cq = &dev->cqs[config->obj_param.rx_queue];
97b7d3a0feSSunil Kumar Kori rq = &dev->rqs[config->obj_param.rx_queue];
98b7d3a0feSSunil Kumar Kori is_mempool = config->obj & RTE_ETH_CMAN_OBJ_RX_QUEUE_MEMPOOL ? true : false;
99b7d3a0feSSunil Kumar Kori min_th = config->mode_param.red.min_th;
100b7d3a0feSSunil Kumar Kori max_th = config->mode_param.red.max_th;
101b7d3a0feSSunil Kumar Kori
102b7d3a0feSSunil Kumar Kori if (is_mempool) {
103b7d3a0feSSunil Kumar Kori buf_cnt = roc_npa_aura_op_limit_get(rq->aura_handle);
104b7d3a0feSSunil Kumar Kori shift = plt_log2_u32(buf_cnt);
105b7d3a0feSSunil Kumar Kori shift = shift < 8 ? 0 : shift - 8;
106b7d3a0feSSunil Kumar Kori pass = (buf_cnt >> shift) - ((buf_cnt * min_th / 100) >> shift);
107b7d3a0feSSunil Kumar Kori drop = (buf_cnt >> shift) - ((buf_cnt * max_th / 100) >> shift);
108b7d3a0feSSunil Kumar Kori rq->red_pass = pass;
109b7d3a0feSSunil Kumar Kori rq->red_drop = drop;
110b7d3a0feSSunil Kumar Kori
111b7d3a0feSSunil Kumar Kori if (rq->spb_ena) {
112b7d3a0feSSunil Kumar Kori buf_cnt = roc_npa_aura_op_limit_get(rq->spb_aura_handle);
113b7d3a0feSSunil Kumar Kori shift = plt_log2_u32(buf_cnt);
114b7d3a0feSSunil Kumar Kori shift = shift < 8 ? 0 : shift - 8;
115b7d3a0feSSunil Kumar Kori pass = (buf_cnt >> shift) - ((buf_cnt * min_th / 100) >> shift);
116b7d3a0feSSunil Kumar Kori drop = (buf_cnt >> shift) - ((buf_cnt * max_th / 100) >> shift);
117b7d3a0feSSunil Kumar Kori rq->spb_red_pass = pass;
118b7d3a0feSSunil Kumar Kori rq->spb_red_drop = drop;
119b7d3a0feSSunil Kumar Kori }
120b7d3a0feSSunil Kumar Kori } else {
121b7d3a0feSSunil Kumar Kori shift = plt_log2_u32(cq->nb_desc);
122b7d3a0feSSunil Kumar Kori shift = shift < 8 ? 0 : shift - 8;
123b7d3a0feSSunil Kumar Kori pass = 256 - ((cq->nb_desc * min_th / 100) >> shift);
124b7d3a0feSSunil Kumar Kori drop = 256 - ((cq->nb_desc * max_th / 100) >> shift);
125b7d3a0feSSunil Kumar Kori
126b7d3a0feSSunil Kumar Kori rq->xqe_red_pass = pass;
127b7d3a0feSSunil Kumar Kori rq->xqe_red_drop = drop;
128b7d3a0feSSunil Kumar Kori }
129b7d3a0feSSunil Kumar Kori
130b7d3a0feSSunil Kumar Kori rc = roc_nix_rq_cman_config(nix, rq);
131b7d3a0feSSunil Kumar Kori if (rc)
132b7d3a0feSSunil Kumar Kori return rc;
133b7d3a0feSSunil Kumar Kori
134b7d3a0feSSunil Kumar Kori memcpy(&dev->cman_cfg, config, sizeof(struct rte_eth_cman_config));
135b7d3a0feSSunil Kumar Kori return 0;
136b7d3a0feSSunil Kumar Kori }
137b7d3a0feSSunil Kumar Kori
138b7d3a0feSSunil Kumar Kori int
cnxk_nix_cman_config_get(struct rte_eth_dev * eth_dev,struct rte_eth_cman_config * config)139b7d3a0feSSunil Kumar Kori cnxk_nix_cman_config_get(struct rte_eth_dev *eth_dev, struct rte_eth_cman_config *config)
140b7d3a0feSSunil Kumar Kori {
141b7d3a0feSSunil Kumar Kori struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
142b7d3a0feSSunil Kumar Kori
143b7d3a0feSSunil Kumar Kori memcpy(config, &dev->cman_cfg, sizeof(struct rte_eth_cman_config));
144b7d3a0feSSunil Kumar Kori return 0;
145b7d3a0feSSunil Kumar Kori }
146