2a37decaaf29997130bc0f2033b527bafd4b9f77
[dpdk.git] / drivers / net / mlx5 / mlx5_flow_meter.c
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright 2018 Mellanox Technologies, Ltd
4  */
5 #include <math.h>
6
7 #include <rte_tailq.h>
8 #include <rte_malloc.h>
9 #include <rte_mtr.h>
10 #include <rte_mtr_driver.h>
11
12 #include <mlx5_devx_cmds.h>
13 #include <mlx5_malloc.h>
14
15 #include "mlx5.h"
16 #include "mlx5_flow.h"
17
18 /**
19  * Create the meter action.
20  *
21  * @param priv
22  *   Pointer to mlx5_priv.
23  * @param[in] fm
24  *   Pointer to flow meter to be converted.
25  *
26  * @return
27  *   Pointer to the meter action on success, NULL otherwise.
28  */
29 static void *
30 mlx5_flow_meter_action_create(struct mlx5_priv *priv,
31                               struct mlx5_flow_meter *fm)
32 {
33 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
34         struct mlx5dv_dr_flow_meter_attr mtr_init;
35         void *attr = fm->mfts->fmp;
36         struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm =
37                                                      &fm->profile->srtcm_prm;
38
39         fm->mfts->fmp_size = MLX5_ST_SZ_BYTES(flow_meter_parameters);
40         memset(attr, 0, fm->mfts->fmp_size);
41         MLX5_SET(flow_meter_parameters, attr, valid, 1);
42         MLX5_SET(flow_meter_parameters, attr, bucket_overflow, 1);
43         MLX5_SET(flow_meter_parameters, attr,
44                  start_color, MLX5_FLOW_COLOR_GREEN);
45         MLX5_SET(flow_meter_parameters, attr, both_buckets_on_green, 0);
46         MLX5_SET(flow_meter_parameters,
47                  attr, cbs_exponent, srtcm->cbs_exponent);
48         MLX5_SET(flow_meter_parameters,
49                  attr, cbs_mantissa, srtcm->cbs_mantissa);
50         MLX5_SET(flow_meter_parameters,
51                  attr, cir_exponent, srtcm->cir_exponent);
52         MLX5_SET(flow_meter_parameters,
53                  attr, cir_mantissa, srtcm->cir_mantissa);
54         MLX5_SET(flow_meter_parameters,
55                  attr, ebs_exponent, srtcm->ebs_exponent);
56         MLX5_SET(flow_meter_parameters,
57                  attr, ebs_mantissa, srtcm->ebs_mantissa);
58         mtr_init.next_table =
59                 fm->transfer ? fm->mfts->transfer.tbl->obj :
60                     fm->egress ? fm->mfts->egress.tbl->obj :
61                                        fm->mfts->ingress.tbl->obj;
62         mtr_init.reg_c_index = priv->mtr_color_reg - REG_C_0;
63         mtr_init.flow_meter_parameter = fm->mfts->fmp;
64         mtr_init.flow_meter_parameter_sz = fm->mfts->fmp_size;
65         mtr_init.active = fm->active_state;
66         return mlx5_glue->dv_create_flow_action_meter(&mtr_init);
67 #else
68         (void)priv;
69         (void)fm;
70         return NULL;
71 #endif
72 }
73
74 /**
75  * Find meter profile by id.
76  *
77  * @param priv
78  *   Pointer to mlx5_priv.
79  * @param meter_profile_id
80  *   Meter profile id.
81  *
82  * @return
83  *   Pointer to the profile found on success, NULL otherwise.
84  */
85 static struct mlx5_flow_meter_profile *
86 mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
87 {
88         struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
89         struct mlx5_flow_meter_profile *fmp;
90
91         TAILQ_FOREACH(fmp, fmps, next)
92                 if (meter_profile_id == fmp->meter_profile_id)
93                         return fmp;
94         return NULL;
95 }
96
97 /**
98  * Validate the MTR profile.
99  *
100  * @param[in] dev
101  *   Pointer to Ethernet device.
102  * @param[in] meter_profile_id
103  *   Meter profile id.
104  * @param[in] profile
105  *   Pointer to meter profile detail.
106  * @param[out] error
107  *   Pointer to the error structure.
108  *
109  * @return
110  *   0 on success, a negative errno value otherwise and rte_errno is set.
111  */
112 static int
113 mlx5_flow_meter_profile_validate(struct rte_eth_dev *dev,
114                                  uint32_t meter_profile_id,
115                                  struct rte_mtr_meter_profile *profile,
116                                  struct rte_mtr_error *error)
117 {
118         struct mlx5_priv *priv = dev->data->dev_private;
119         struct mlx5_flow_meter_profile *fmp;
120
121         /* Profile must not be NULL. */
122         if (profile == NULL)
123                 return -rte_mtr_error_set(error, EINVAL,
124                                           RTE_MTR_ERROR_TYPE_METER_PROFILE,
125                                           NULL, "Meter profile is null.");
126         /* Meter profile ID must be valid. */
127         if (meter_profile_id == UINT32_MAX)
128                 return -rte_mtr_error_set(error, EINVAL,
129                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
130                                           NULL, "Meter profile id not valid.");
131         /* Meter profile must not exist. */
132         fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
133         if (fmp)
134                 return -rte_mtr_error_set(error, EEXIST,
135                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
136                                           NULL,
137                                           "Meter profile already exists.");
138         if (profile->alg == RTE_MTR_SRTCM_RFC2697) {
139                 if (priv->config.hca_attr.qos.flow_meter_old) {
140                         /* Verify support for flow meter parameters. */
141                         if (profile->srtcm_rfc2697.cir > 0 &&
142                             profile->srtcm_rfc2697.cir <= MLX5_SRTCM_CIR_MAX &&
143                             profile->srtcm_rfc2697.cbs > 0 &&
144                             profile->srtcm_rfc2697.cbs <= MLX5_SRTCM_CBS_MAX &&
145                             profile->srtcm_rfc2697.ebs <= MLX5_SRTCM_EBS_MAX)
146                                 return 0;
147                         else
148                                 return -rte_mtr_error_set
149                                              (error, ENOTSUP,
150                                               RTE_MTR_ERROR_TYPE_MTR_PARAMS,
151                                               NULL,
152                                               profile->srtcm_rfc2697.ebs ?
153                                               "Metering value ebs must be 0." :
154                                               "Invalid metering parameters.");
155                 }
156         }
157         return -rte_mtr_error_set(error, ENOTSUP,
158                                   RTE_MTR_ERROR_TYPE_METER_PROFILE,
159                                   NULL, "Metering algorithm not supported.");
160 }
161
162 /**
163  * Calculate mantissa and exponent for cir.
164  *
165  * @param[in] cir
166  *   Value to be calculated.
167  * @param[out] man
168  *   Pointer to the mantissa.
169  * @param[out] exp
170  *   Pointer to the exp.
171  */
172 static void
173 mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
174 {
175         int64_t _cir;
176         int64_t delta = INT64_MAX;
177         uint8_t _man = 0;
178         uint8_t _exp = 0;
179         uint64_t m, e;
180
181         for (m = 0; m <= 0xFF; m++) { /* man width 8 bit */
182                 for (e = 0; e <= 0x1F; e++) { /* exp width 5bit */
183                         _cir = (1000000000ULL * m) >> e;
184                         if (llabs(cir - _cir) <= delta) {
185                                 delta = llabs(cir - _cir);
186                                 _man = m;
187                                 _exp = e;
188                         }
189                 }
190         }
191         *man = _man;
192         *exp = _exp;
193 }
194
195 /**
196  * Calculate mantissa and exponent for xbs.
197  *
198  * @param[in] xbs
199  *   Value to be calculated.
200  * @param[out] man
201  *   Pointer to the mantissa.
202  * @param[out] exp
203  *   Pointer to the exp.
204  */
205 static void
206 mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
207 {
208         int _exp;
209         double _man;
210
211         /* Special case xbs == 0 ? both exp and matissa are 0. */
212         if (xbs == 0) {
213                 *man = 0;
214                 *exp = 0;
215                 return;
216         }
217         /* xbs = xbs_mantissa * 2^xbs_exponent */
218         _man = frexp(xbs, &_exp);
219         _man = _man * pow(2, MLX5_MAN_WIDTH);
220         _exp = _exp - MLX5_MAN_WIDTH;
221         *man = (uint8_t)ceil(_man);
222         *exp = _exp;
223 }
224
225 /**
226  * Fill the prm meter parameter.
227  *
228  * @param[in,out] fmp
229  *   Pointer to meter profie to be converted.
230  * @param[out] error
231  *   Pointer to the error structure.
232  *
233  * @return
234  *   0 on success, a negative errno value otherwise and rte_errno is set.
235  */
236 static int
237 mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
238                           struct rte_mtr_error *error)
239 {
240         struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
241         uint8_t man, exp;
242
243         if (fmp->profile.alg != RTE_MTR_SRTCM_RFC2697)
244                 return -rte_mtr_error_set(error, ENOTSUP,
245                                 RTE_MTR_ERROR_TYPE_METER_PROFILE,
246                                 NULL, "Metering algorithm not supported.");
247          /* cbs = cbs_mantissa * 2^cbs_exponent */
248         mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.cbs,
249                                     &man, &exp);
250         srtcm->cbs_mantissa = man;
251         srtcm->cbs_exponent = exp;
252         /* Check if cbs mantissa is too large. */
253         if (srtcm->cbs_exponent != exp)
254                 return -rte_mtr_error_set(error, EINVAL,
255                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
256                                           "Metering profile parameter cbs is"
257                                           " invalid.");
258         /* ebs = ebs_mantissa * 2^ebs_exponent */
259         mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.ebs,
260                                     &man, &exp);
261         srtcm->ebs_mantissa = man;
262         srtcm->ebs_exponent = exp;
263         /* Check if ebs mantissa is too large. */
264         if (srtcm->ebs_exponent != exp)
265                 return -rte_mtr_error_set(error, EINVAL,
266                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
267                                           "Metering profile parameter ebs is"
268                                           " invalid.");
269         /* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
270         mlx5_flow_meter_cir_man_exp_calc(fmp->profile.srtcm_rfc2697.cir,
271                                     &man, &exp);
272         srtcm->cir_mantissa = man;
273         srtcm->cir_exponent = exp;
274         /* Check if cir mantissa is too large. */
275         if (srtcm->cir_exponent != exp)
276                 return -rte_mtr_error_set(error, EINVAL,
277                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
278                                           "Metering profile parameter cir is"
279                                           " invalid.");
280         return 0;
281 }
282
283 /**
284  * Callback to get MTR capabilities.
285  *
286  * @param[in] dev
287  *   Pointer to Ethernet device.
288  * @param[out] cap
289  *   Pointer to save MTR capabilities.
290  * @param[out] error
291  *   Pointer to the error structure.
292  *
293  * @return
294  *   0 on success, a negative errno value otherwise and rte_errno is set.
295  */
296 static int
297 mlx5_flow_mtr_cap_get(struct rte_eth_dev *dev,
298                  struct rte_mtr_capabilities *cap,
299                  struct rte_mtr_error *error __rte_unused)
300 {
301         struct mlx5_priv *priv = dev->data->dev_private;
302         struct mlx5_hca_qos_attr *qattr = &priv->config.hca_attr.qos;
303
304         if (!priv->mtr_en)
305                 return -rte_mtr_error_set(error, ENOTSUP,
306                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
307                                           "Meter is not supported");
308         memset(cap, 0, sizeof(*cap));
309         cap->n_max = 1 << qattr->log_max_flow_meter;
310         cap->n_shared_max = cap->n_max;
311         cap->identical = 1;
312         cap->shared_identical = 1;
313         cap->shared_n_flows_per_mtr_max = 4 << 20;
314         /* 2M flows can share the same meter. */
315         cap->chaining_n_mtrs_per_flow_max = 1; /* Chaining is not supported. */
316         cap->meter_srtcm_rfc2697_n_max = qattr->flow_meter_old ? cap->n_max : 0;
317         cap->meter_rate_max = 1ULL << 40; /* 1 Tera tokens per sec. */
318         cap->policer_action_drop_supported = 1;
319         cap->stats_mask = RTE_MTR_STATS_N_BYTES_DROPPED |
320                           RTE_MTR_STATS_N_PKTS_DROPPED;
321         return 0;
322 }
323
324 /**
325  * Callback to add MTR profile.
326  *
327  * @param[in] dev
328  *   Pointer to Ethernet device.
329  * @param[in] meter_profile_id
330  *   Meter profile id.
331  * @param[in] profile
332  *   Pointer to meter profile detail.
333  * @param[out] error
334  *   Pointer to the error structure.
335  *
336  * @return
337  *   0 on success, a negative errno value otherwise and rte_errno is set.
338  */
339 static int
340 mlx5_flow_meter_profile_add(struct rte_eth_dev *dev,
341                        uint32_t meter_profile_id,
342                        struct rte_mtr_meter_profile *profile,
343                        struct rte_mtr_error *error)
344 {
345         struct mlx5_priv *priv = dev->data->dev_private;
346         struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
347         struct mlx5_flow_meter_profile *fmp;
348         int ret;
349
350         if (!priv->mtr_en)
351                 return -rte_mtr_error_set(error, ENOTSUP,
352                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
353                                           "Meter is not supported");
354         /* Check input params. */
355         ret = mlx5_flow_meter_profile_validate(dev, meter_profile_id,
356                                                profile, error);
357         if (ret)
358                 return ret;
359         /* Meter profile memory allocation. */
360         fmp = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_flow_meter_profile),
361                          RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
362         if (fmp == NULL)
363                 return -rte_mtr_error_set(error, ENOMEM,
364                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED,
365                                           NULL, "Meter profile memory "
366                                           "alloc failed.");
367         /* Fill profile info. */
368         fmp->meter_profile_id = meter_profile_id;
369         fmp->profile = *profile;
370         /* Fill the flow meter parameters for the PRM. */
371         ret = mlx5_flow_meter_param_fill(fmp, error);
372         if (ret)
373                 goto error;
374         /* Add to list. */
375         TAILQ_INSERT_TAIL(fmps, fmp, next);
376         return 0;
377 error:
378         mlx5_free(fmp);
379         return ret;
380 }
381
382 /**
383  * Callback to delete MTR profile.
384  *
385  * @param[in] dev
386  *   Pointer to Ethernet device.
387  * @param[in] meter_profile_id
388  *   Meter profile id.
389  * @param[out] error
390  *   Pointer to the error structure.
391  *
392  * @return
393  *   0 on success, a negative errno value otherwise and rte_errno is set.
394  */
395 static int
396 mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
397                           uint32_t meter_profile_id,
398                           struct rte_mtr_error *error)
399 {
400         struct mlx5_priv *priv = dev->data->dev_private;
401         struct mlx5_flow_meter_profile *fmp;
402
403         if (!priv->mtr_en)
404                 return -rte_mtr_error_set(error, ENOTSUP,
405                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
406                                           "Meter is not supported");
407         /* Meter profile must exist. */
408         fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
409         if (fmp == NULL)
410                 return -rte_mtr_error_set(error, ENOENT,
411                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
412                                           &meter_profile_id,
413                                           "Meter profile id is invalid.");
414         /* Check profile is unused. */
415         if (fmp->ref_cnt)
416                 return -rte_mtr_error_set(error, EBUSY,
417                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
418                                           NULL, "Meter profile is in use.");
419         /* Remove from list. */
420         TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
421         mlx5_free(fmp);
422         return 0;
423 }
424
425 /**
426  * Convert wrong color setting action to verbose error.
427  *
428  * @param[in] action
429  *   Policy color action.
430  *
431  * @return
432  *   Verbose meter color error type.
433  */
434 static inline enum rte_mtr_error_type
435 action2error(enum rte_mtr_policer_action action)
436 {
437         switch (action) {
438         case MTR_POLICER_ACTION_COLOR_GREEN:
439                 return RTE_MTR_ERROR_TYPE_POLICER_ACTION_GREEN;
440         case MTR_POLICER_ACTION_COLOR_YELLOW:
441                 return RTE_MTR_ERROR_TYPE_POLICER_ACTION_YELLOW;
442         case MTR_POLICER_ACTION_COLOR_RED:
443                 return RTE_MTR_ERROR_TYPE_POLICER_ACTION_RED;
444         default:
445                 break;
446         }
447         return RTE_MTR_ERROR_TYPE_UNSPECIFIED;
448 }
449
450 /**
451  * Check meter validation.
452  *
453  * @param[in] priv
454  *   Pointer to mlx5 private data structure.
455  * @param[in] meter_id
456  *   Meter id.
457  * @param[in] params
458  *   Pointer to rte meter parameters.
459  * @param[out] error
460  *   Pointer to rte meter error structure.
461  *
462  * @return
463  *   0 on success, a negative errno value otherwise and rte_errno is set.
464  */
465 static int
466 mlx5_flow_meter_validate(struct mlx5_priv *priv, uint32_t meter_id,
467                          struct rte_mtr_params *params,
468                          struct rte_mtr_error *error)
469 {
470         /* Meter must use global drop action. */
471         if (!priv->sh->dr_drop_action)
472                 return -rte_mtr_error_set(error, ENOTSUP,
473                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
474                                           NULL,
475                                           "No drop action ready for meter.");
476         /* Meter params must not be NULL. */
477         if (params == NULL)
478                 return -rte_mtr_error_set(error, EINVAL,
479                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
480                                           NULL, "Meter object params null.");
481         /* Previous meter color is not supported. */
482         if (params->use_prev_mtr_color)
483                 return -rte_mtr_error_set(error, ENOTSUP,
484                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
485                                           NULL,
486                                           "Previous meter color "
487                                           "not supported.");
488         /* Validate policer settings. */
489         if (params->action[RTE_COLOR_RED] != MTR_POLICER_ACTION_DROP)
490                 return -rte_mtr_error_set
491                                 (error, ENOTSUP,
492                                  action2error(params->action[RTE_COLOR_RED]),
493                                  NULL,
494                                  "Red color only supports drop action.");
495         if (params->action[RTE_COLOR_GREEN] != MTR_POLICER_ACTION_COLOR_GREEN)
496                 return -rte_mtr_error_set
497                                 (error, ENOTSUP,
498                                  action2error(params->action[RTE_COLOR_GREEN]),
499                                  NULL,
500                                  "Green color only supports recolor green action.");
501         /* Validate meter id. */
502         if (mlx5_flow_meter_find(priv, meter_id))
503                 return -rte_mtr_error_set(error, EEXIST,
504                                           RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
505                                           "Meter object already exists.");
506         return 0;
507 }
508
509 /**
510  * Modify the flow meter action.
511  *
512  * @param[in] priv
513  *   Pointer to mlx5 private data structure.
514  * @param[in] fm
515  *   Pointer to flow meter to be modified.
516  * @param[in] srtcm
517  *   Pointer to meter srtcm description parameter.
518  * @param[in] modify_bits
519  *   The bit in srtcm to be updated.
520  * @param[in] active_state
521  *   The state to be updated.
522  * @return
523  *   0 on success, o negative value otherwise.
524  */
525 static int
526 mlx5_flow_meter_action_modify(struct mlx5_priv *priv,
527                 struct mlx5_flow_meter *fm,
528                 const struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm,
529                 uint64_t modify_bits, uint32_t active_state)
530 {
531 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
532         uint32_t in[MLX5_ST_SZ_DW(flow_meter_parameters)] = { 0 };
533         uint32_t *attr;
534         struct mlx5dv_dr_flow_meter_attr mod_attr = { 0 };
535         int ret;
536
537         /* Fill command parameters. */
538         mod_attr.reg_c_index = priv->mtr_color_reg - REG_C_0;
539         mod_attr.flow_meter_parameter = in;
540         mod_attr.flow_meter_parameter_sz = fm->mfts->fmp_size;
541         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
542                 mod_attr.active = !!active_state;
543         else
544                 mod_attr.active = 0;
545         attr = in;
546         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
547                 MLX5_SET(flow_meter_parameters,
548                          attr, cbs_exponent, srtcm->cbs_exponent);
549                 MLX5_SET(flow_meter_parameters,
550                          attr, cbs_mantissa, srtcm->cbs_mantissa);
551         }
552         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
553                 MLX5_SET(flow_meter_parameters,
554                          attr, cir_exponent, srtcm->cir_exponent);
555                 MLX5_SET(flow_meter_parameters,
556                          attr, cir_mantissa, srtcm->cir_mantissa);
557         }
558         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
559                 MLX5_SET(flow_meter_parameters,
560                          attr, ebs_exponent, srtcm->ebs_exponent);
561                 MLX5_SET(flow_meter_parameters,
562                          attr, ebs_mantissa, srtcm->ebs_mantissa);
563         }
564         /* Apply modifications to meter only if it was created. */
565         if (fm->mfts->meter_action) {
566                 ret = mlx5_glue->dv_modify_flow_action_meter
567                                         (fm->mfts->meter_action, &mod_attr,
568                                         rte_cpu_to_be_64(modify_bits));
569                 if (ret)
570                         return ret;
571         }
572         /* Update succeedded modify meter parameters. */
573         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
574                 fm->active_state = !!active_state;
575         attr = fm->mfts->fmp;
576         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
577                 MLX5_SET(flow_meter_parameters,
578                          attr, cbs_exponent, srtcm->cbs_exponent);
579                 MLX5_SET(flow_meter_parameters,
580                          attr, cbs_mantissa, srtcm->cbs_mantissa);
581         }
582         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
583                 MLX5_SET(flow_meter_parameters,
584                          attr, cir_exponent, srtcm->cir_exponent);
585                 MLX5_SET(flow_meter_parameters,
586                          attr, cir_mantissa, srtcm->cir_mantissa);
587         }
588         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
589                 MLX5_SET(flow_meter_parameters,
590                          attr, ebs_exponent, srtcm->ebs_exponent);
591                 MLX5_SET(flow_meter_parameters,
592                          attr, ebs_mantissa, srtcm->ebs_mantissa);
593         }
594
595         return 0;
596 #else
597         (void)priv;
598         (void)fm;
599         (void)srtcm;
600         (void)modify_bits;
601         (void)active_state;
602         return -ENOTSUP;
603 #endif
604 }
605
606 static void
607 mlx5_flow_meter_stats_enable_update(struct mlx5_flow_meter *fm,
608                                 uint64_t stats_mask)
609 {
610         fm->green_bytes = (stats_mask & RTE_MTR_STATS_N_BYTES_GREEN) ? 1 : 0;
611         fm->green_pkts = (stats_mask & RTE_MTR_STATS_N_PKTS_GREEN) ? 1 : 0;
612         fm->red_bytes = (stats_mask & RTE_MTR_STATS_N_BYTES_RED) ? 1 : 0;
613         fm->red_pkts = (stats_mask & RTE_MTR_STATS_N_PKTS_RED) ? 1 : 0;
614         fm->bytes_dropped =
615                 (stats_mask & RTE_MTR_STATS_N_BYTES_DROPPED) ? 1 : 0;
616         fm->pkts_dropped = (stats_mask & RTE_MTR_STATS_N_PKTS_DROPPED) ? 1 : 0;
617 }
618
619 /**
620  * Create meter rules.
621  *
622  * @param[in] dev
623  *   Pointer to Ethernet device.
624  * @param[in] meter_id
625  *   Meter id.
626  * @param[in] params
627  *   Pointer to rte meter parameters.
628  * @param[in] shared
629  *   Meter shared with other flow or not.
630  * @param[out] error
631  *   Pointer to rte meter error structure.
632  *
633  * @return
634  *   0 on success, a negative errno value otherwise and rte_errno is set.
635  */
636 static int
637 mlx5_flow_meter_create(struct rte_eth_dev *dev, uint32_t meter_id,
638                        struct rte_mtr_params *params, int shared,
639                        struct rte_mtr_error *error)
640 {
641         struct mlx5_priv *priv = dev->data->dev_private;
642         struct mlx5_flow_meters *fms = &priv->flow_meters;
643         struct mlx5_flow_meter_profile *fmp;
644         struct mlx5_flow_meter *fm;
645         const struct rte_flow_attr attr = {
646                                 .ingress = 1,
647                                 .egress = 1,
648                                 .transfer = priv->config.dv_esw_en ? 1 : 0,
649                         };
650         struct mlx5_indexed_pool_config flow_ipool_cfg = {
651                 .size = 0,
652                 .trunk_size = 64,
653                 .need_lock = 1,
654                 .type = "mlx5_flow_mtr_flow_id_pool",
655         };
656         int ret;
657         uint32_t idx = 0;
658         uint8_t mtr_id_bits;
659         uint8_t mtr_reg_bits = priv->mtr_reg_share ?
660                                 MLX5_MTR_IDLE_BITS_IN_COLOR_REG : MLX5_REG_BITS;
661
662         if (!priv->mtr_en)
663                 return -rte_mtr_error_set(error, ENOTSUP,
664                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
665                                           "Meter is not supported");
666         /* Validate the parameters. */
667         ret = mlx5_flow_meter_validate(priv, meter_id, params, error);
668         if (ret)
669                 return ret;
670         /* Meter profile must exist. */
671         fmp = mlx5_flow_meter_profile_find(priv, params->meter_profile_id);
672         if (fmp == NULL)
673                 return -rte_mtr_error_set(error, ENOENT,
674                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
675                                           NULL, "Meter profile id not valid.");
676         /* Allocate the flow meter memory. */
677         fm = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MTR], &idx);
678         if (fm == NULL)
679                 return -rte_mtr_error_set(error, ENOMEM,
680                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
681                                           "Memory alloc failed for meter.");
682         mtr_id_bits = MLX5_REG_BITS - __builtin_clz(idx);
683         if ((mtr_id_bits + priv->max_mtr_flow_bits) > mtr_reg_bits) {
684                 DRV_LOG(ERR, "Meter number exceeds max limit.");
685                 goto error;
686         }
687         if (mtr_id_bits > priv->max_mtr_bits)
688                 priv->max_mtr_bits = mtr_id_bits;
689         fm->idx = idx;
690         /* Fill the flow meter parameters. */
691         fm->meter_id = meter_id;
692         fm->profile = fmp;
693         memcpy(fm->action, params->action, sizeof(params->action));
694         mlx5_flow_meter_stats_enable_update(fm, params->stats_mask);
695
696         /* Alloc policer counters. */
697         if (fm->green_bytes || fm->green_pkts) {
698                 fm->policer_stats.pass_cnt = mlx5_counter_alloc(dev);
699                 if (!fm->policer_stats.pass_cnt)
700                         goto error;
701         }
702         if (fm->red_bytes || fm->red_pkts ||
703             fm->bytes_dropped || fm->pkts_dropped) {
704                 fm->policer_stats.drop_cnt = mlx5_counter_alloc(dev);
705                 if (!fm->policer_stats.drop_cnt)
706                         goto error;
707         }
708         fm->mfts = mlx5_flow_create_mtr_tbls(dev);
709         if (!fm->mfts)
710                 goto error;
711         ret = mlx5_flow_prepare_policer_rules(dev, fm, &attr);
712         if (ret)
713                 goto error;
714         /* Add to the flow meter list. */
715         TAILQ_INSERT_TAIL(fms, fm, next);
716         fm->active_state = 1; /* Config meter starts as active. */
717         fm->shared = !!shared;
718         fm->profile->ref_cnt++;
719         fm->flow_ipool = mlx5_ipool_create(&flow_ipool_cfg);
720         if (!fm->flow_ipool)
721                 goto error;
722         rte_spinlock_init(&fm->sl);
723         return 0;
724 error:
725         mlx5_flow_destroy_policer_rules(dev, fm, &attr);
726         mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
727         /* Free policer counters. */
728         if (fm->policer_stats.pass_cnt)
729                 mlx5_counter_free(dev, fm->policer_stats.pass_cnt);
730         if (fm->policer_stats.drop_cnt)
731                 mlx5_counter_free(dev, fm->policer_stats.drop_cnt);
732         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], idx);
733         return -rte_mtr_error_set(error, -ret,
734                                   RTE_MTR_ERROR_TYPE_UNSPECIFIED,
735                                   NULL, "Failed to create devx meter.");
736 }
737
738 /**
739  * Destroy meter rules.
740  *
741  * @param[in] dev
742  *   Pointer to Ethernet device.
743  * @param[in] meter_id
744  *   Meter id.
745  * @param[out] error
746  *   Pointer to rte meter error structure.
747  *
748  * @return
749  *   0 on success, a negative errno value otherwise and rte_errno is set.
750  */
751 static int
752 mlx5_flow_meter_destroy(struct rte_eth_dev *dev, uint32_t meter_id,
753                         struct rte_mtr_error *error)
754 {
755         struct mlx5_priv *priv = dev->data->dev_private;
756         struct mlx5_flow_meters *fms = &priv->flow_meters;
757         struct mlx5_flow_meter_profile *fmp;
758         struct mlx5_flow_meter *fm;
759         const struct rte_flow_attr attr = {
760                                 .ingress = 1,
761                                 .egress = 1,
762                                 .transfer = priv->config.dv_esw_en ? 1 : 0,
763                         };
764
765         if (!priv->mtr_en)
766                 return -rte_mtr_error_set(error, ENOTSUP,
767                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
768                                           "Meter is not supported");
769         /* Meter object must exist. */
770         fm = mlx5_flow_meter_find(priv, meter_id);
771         if (fm == NULL)
772                 return -rte_mtr_error_set(error, ENOENT,
773                                           RTE_MTR_ERROR_TYPE_MTR_ID,
774                                           NULL, "Meter object id not valid.");
775         /* Meter object must not have any owner. */
776         if (fm->ref_cnt > 0)
777                 return -rte_mtr_error_set(error, EBUSY,
778                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED,
779                                           NULL, "Meter object is being used.");
780         /* Get the meter profile. */
781         fmp = fm->profile;
782         MLX5_ASSERT(fmp);
783         /* Update dependencies. */
784         fmp->ref_cnt--;
785         /* Remove from the flow meter list. */
786         TAILQ_REMOVE(fms, fm, next);
787         /* Free policer counters. */
788         if (fm->policer_stats.pass_cnt)
789                 mlx5_counter_free(dev, fm->policer_stats.pass_cnt);
790         if (fm->policer_stats.drop_cnt)
791                 mlx5_counter_free(dev, fm->policer_stats.drop_cnt);
792         /* Free meter flow table */
793         if (fm->flow_ipool)
794                 mlx5_ipool_destroy(fm->flow_ipool);
795         mlx5_flow_destroy_policer_rules(dev, fm, &attr);
796         mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
797         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
798         return 0;
799 }
800
801 /**
802  * Modify meter state.
803  *
804  * @param[in] priv
805  *   Pointer to mlx5 private data structure.
806  * @param[in] fm
807  *   Pointer to flow meter.
808  * @param[in] new_state
809  *   New state to update.
810  * @param[out] error
811  *   Pointer to rte meter error structure.
812  *
813  * @return
814  *   0 on success, a negative errno value otherwise and rte_errno is set.
815  */
816 static int
817 mlx5_flow_meter_modify_state(struct mlx5_priv *priv,
818                              struct mlx5_flow_meter *fm,
819                              uint32_t new_state,
820                              struct rte_mtr_error *error)
821 {
822         static const struct mlx5_flow_meter_srtcm_rfc2697_prm srtcm = {
823                 .cbs_exponent = 20,
824                 .cbs_mantissa = 191,
825                 .cir_exponent = 0,
826                 .cir_mantissa = 200,
827                 .ebs_exponent = 0,
828                 .ebs_mantissa = 0,
829         };
830         uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
831                                MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
832         int ret;
833
834         if (new_state == MLX5_FLOW_METER_DISABLE)
835                 ret = mlx5_flow_meter_action_modify(priv, fm, &srtcm,
836                                                     modify_bits, 0);
837         else
838                 ret = mlx5_flow_meter_action_modify(priv, fm,
839                                                    &fm->profile->srtcm_prm,
840                                                     modify_bits, 0);
841         if (ret)
842                 return -rte_mtr_error_set(error, -ret,
843                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
844                                           NULL,
845                                           new_state ?
846                                           "Failed to enable meter." :
847                                           "Failed to disable meter.");
848         return 0;
849 }
850
851 /**
852  * Callback to enable flow meter.
853  *
854  * @param[in] dev
855  *   Pointer to Ethernet device.
856  * @param[in] meter_id
857  *   Meter id.
858  * @param[out] error
859  *   Pointer to rte meter error structure.
860  *
861  * @return
862  *   0 on success, a negative errno value otherwise and rte_errno is set.
863  */
864 static int
865 mlx5_flow_meter_enable(struct rte_eth_dev *dev,
866                        uint32_t meter_id,
867                        struct rte_mtr_error *error)
868 {
869         struct mlx5_priv *priv = dev->data->dev_private;
870         struct mlx5_flow_meter *fm;
871         int ret;
872
873         if (!priv->mtr_en)
874                 return -rte_mtr_error_set(error, ENOTSUP,
875                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
876                                           "Meter is not supported");
877         /* Meter object must exist. */
878         fm = mlx5_flow_meter_find(priv, meter_id);
879         if (fm == NULL)
880                 return -rte_mtr_error_set(error, ENOENT,
881                                           RTE_MTR_ERROR_TYPE_MTR_ID,
882                                           NULL, "Meter not found.");
883         if (fm->active_state == MLX5_FLOW_METER_ENABLE)
884                 return 0;
885         ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_ENABLE,
886                                            error);
887         if (!ret)
888                 fm->active_state = MLX5_FLOW_METER_ENABLE;
889         return ret;
890 }
891
892 /**
893  * Callback to disable flow meter.
894  *
895  * @param[in] dev
896  *   Pointer to Ethernet device.
897  * @param[in] meter_id
898  *   Meter id.
899  * @param[out] error
900  *   Pointer to rte meter error structure.
901  *
902  * @return
903  *   0 on success, a negative errno value otherwise and rte_errno is set.
904  */
905 static int
906 mlx5_flow_meter_disable(struct rte_eth_dev *dev,
907                         uint32_t meter_id,
908                         struct rte_mtr_error *error)
909 {
910         struct mlx5_priv *priv = dev->data->dev_private;
911         struct mlx5_flow_meter *fm;
912         int ret;
913
914         if (!priv->mtr_en)
915                 return -rte_mtr_error_set(error, ENOTSUP,
916                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
917                                           "Meter is not supported");
918         /* Meter object must exist. */
919         fm = mlx5_flow_meter_find(priv, meter_id);
920         if (fm == NULL)
921                 return -rte_mtr_error_set(error, ENOENT,
922                                           RTE_MTR_ERROR_TYPE_MTR_ID,
923                                           NULL, "Meter not found.");
924         if (fm->active_state == MLX5_FLOW_METER_DISABLE)
925                 return 0;
926         ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_DISABLE,
927                                            error);
928         if (!ret)
929                 fm->active_state = MLX5_FLOW_METER_DISABLE;
930         return ret;
931 }
932
933 /**
934  * Callback to update meter profile.
935  *
936  * @param[in] dev
937  *   Pointer to Ethernet device.
938  * @param[in] meter_id
939  *   Meter id.
940  * @param[in] meter_profile_id
941  *   To be updated meter profile id.
942  * @param[out] error
943  *   Pointer to rte meter error structure.
944  *
945  * @return
946  *   0 on success, a negative errno value otherwise and rte_errno is set.
947  */
948 static int
949 mlx5_flow_meter_profile_update(struct rte_eth_dev *dev,
950                                uint32_t meter_id,
951                                uint32_t meter_profile_id,
952                                struct rte_mtr_error *error)
953 {
954         struct mlx5_priv *priv = dev->data->dev_private;
955         struct mlx5_flow_meter_profile *fmp;
956         struct mlx5_flow_meter_profile *old_fmp;
957         struct mlx5_flow_meter *fm;
958         uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
959                                MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
960         int ret;
961
962         if (!priv->mtr_en)
963                 return -rte_mtr_error_set(error, ENOTSUP,
964                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
965                                           "Meter is not supported");
966         /* Meter profile must exist. */
967         fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
968         if (fmp == NULL)
969                 return -rte_mtr_error_set(error, ENOENT,
970                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
971                                           NULL, "Meter profile not found.");
972         /* Meter object must exist. */
973         fm = mlx5_flow_meter_find(priv, meter_id);
974         if (fm == NULL)
975                 return -rte_mtr_error_set(error, ENOENT,
976                                           RTE_MTR_ERROR_TYPE_MTR_ID,
977                                           NULL, "Meter not found.");
978         /* MTR object already set to meter profile id. */
979         old_fmp = fm->profile;
980         if (fmp == old_fmp)
981                 return 0;
982         /* Update the profile. */
983         fm->profile = fmp;
984         /* Update meter params in HW (if not disabled). */
985         if (fm->active_state == MLX5_FLOW_METER_DISABLE)
986                 return 0;
987         ret = mlx5_flow_meter_action_modify(priv, fm, &fm->profile->srtcm_prm,
988                                               modify_bits, fm->active_state);
989         if (ret) {
990                 fm->profile = old_fmp;
991                 return -rte_mtr_error_set(error, -ret,
992                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
993                                           NULL, "Failed to update meter"
994                                           " parmeters in hardware.");
995         }
996         old_fmp->ref_cnt--;
997         fmp->ref_cnt++;
998         return 0;
999 }
1000
1001 /**
1002  * Callback to update meter stats mask.
1003  *
1004  * @param[in] dev
1005  *   Pointer to Ethernet device.
1006  * @param[in] meter_id
1007  *   Meter id.
1008  * @param[in] stats_mask
1009  *   To be updated stats_mask.
1010  * @param[out] error
1011  *   Pointer to rte meter error structure.
1012  *
1013  * @return
1014  *   0 on success, a negative errno value otherwise and rte_errno is set.
1015  */
1016 static int
1017 mlx5_flow_meter_stats_update(struct rte_eth_dev *dev,
1018                              uint32_t meter_id,
1019                              uint64_t stats_mask,
1020                              struct rte_mtr_error *error)
1021 {
1022         struct mlx5_priv *priv = dev->data->dev_private;
1023         struct mlx5_flow_meter *fm;
1024         const struct rte_flow_attr attr = {
1025                                 .ingress = 1,
1026                                 .egress = 1,
1027                                 .transfer = priv->config.dv_esw_en ? 1 : 0,
1028                         };
1029         bool need_updated = false;
1030         struct mlx5_flow_policer_stats old_policer_stats;
1031
1032         if (!priv->mtr_en)
1033                 return -rte_mtr_error_set(error, ENOTSUP,
1034                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1035                                           "Meter is not supported");
1036         /* Meter object must exist. */
1037         fm = mlx5_flow_meter_find(priv, meter_id);
1038         if (fm == NULL)
1039                 return -rte_mtr_error_set(error, ENOENT,
1040                                           RTE_MTR_ERROR_TYPE_MTR_ID,
1041                                           NULL, "Meter object id not valid.");
1042         old_policer_stats.pass_cnt = 0;
1043         old_policer_stats.drop_cnt = 0;
1044         if (!!((RTE_MTR_STATS_N_PKTS_GREEN |
1045                                 RTE_MTR_STATS_N_BYTES_GREEN) & stats_mask) !=
1046                 !!fm->policer_stats.pass_cnt) {
1047                 need_updated = true;
1048                 if (fm->policer_stats.pass_cnt) {
1049                         old_policer_stats.pass_cnt = fm->policer_stats.pass_cnt;
1050                         fm->policer_stats.pass_cnt = 0;
1051                 } else {
1052                         fm->policer_stats.pass_cnt =
1053                                 mlx5_counter_alloc(dev);
1054                         if (!fm->policer_stats.pass_cnt)
1055                                 return -rte_mtr_error_set(error, ENOMEM,
1056                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1057                                           "Counter alloc failed for meter.");
1058                 }
1059         }
1060         if (!!((RTE_MTR_STATS_N_PKTS_RED | RTE_MTR_STATS_N_BYTES_RED |
1061                 RTE_MTR_STATS_N_PKTS_DROPPED | RTE_MTR_STATS_N_BYTES_DROPPED) &
1062                 stats_mask) !=
1063                 !!fm->policer_stats.drop_cnt) {
1064                 need_updated = true;
1065                 if (fm->policer_stats.drop_cnt) {
1066                         old_policer_stats.drop_cnt = fm->policer_stats.drop_cnt;
1067                         fm->policer_stats.drop_cnt = 0;
1068                 } else {
1069                         fm->policer_stats.drop_cnt =
1070                                 mlx5_counter_alloc(dev);
1071                         if (!fm->policer_stats.drop_cnt)
1072                                 return -rte_mtr_error_set(error, ENOMEM,
1073                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1074                                           "Counter alloc failed for meter.");
1075                 }
1076         }
1077         if (need_updated) {
1078                 if (mlx5_flow_prepare_policer_rules(dev, fm, &attr)) {
1079                         if (fm->policer_stats.pass_cnt &&
1080                                 fm->policer_stats.pass_cnt !=
1081                                 old_policer_stats.pass_cnt)
1082                                 mlx5_counter_free(dev,
1083                                         fm->policer_stats.pass_cnt);
1084                         fm->policer_stats.pass_cnt =
1085                                         old_policer_stats.pass_cnt;
1086                         if (fm->policer_stats.drop_cnt &&
1087                                 fm->policer_stats.drop_cnt !=
1088                                 old_policer_stats.drop_cnt)
1089                                 mlx5_counter_free(dev,
1090                                         fm->policer_stats.drop_cnt);
1091                         fm->policer_stats.pass_cnt =
1092                                         old_policer_stats.pass_cnt;
1093                         return -rte_mtr_error_set(error, ENOTSUP,
1094                                 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
1095                                 NULL, "Failed to create meter policer rules.");
1096                 }
1097                 /* Free old policer counters. */
1098                 if (old_policer_stats.pass_cnt)
1099                         mlx5_counter_free(dev,
1100                                 old_policer_stats.pass_cnt);
1101                 if (old_policer_stats.drop_cnt)
1102                         mlx5_counter_free(dev,
1103                                 old_policer_stats.drop_cnt);
1104         }
1105         mlx5_flow_meter_stats_enable_update(fm, stats_mask);
1106         return 0;
1107 }
1108
1109 /**
1110  * Callback to read meter statistics.
1111  *
1112  * @param[in] dev
1113  *   Pointer to Ethernet device.
1114  * @param[in] meter_id
1115  *   Meter id.
1116  * @param[out] stats
1117  *   Pointer to store the statistics.
1118  * @param[out] stats_mask
1119  *   Pointer to store the stats_mask.
1120  * @param[in] clear
1121  *   Statistic to be cleared after read or not.
1122  * @param[out] error
1123  *   Pointer to rte meter error structure.
1124  *
1125  * @return
1126  *   0 on success, a negative errno value otherwise and rte_errno is set.
1127  */
1128 static int
1129 mlx5_flow_meter_stats_read(struct rte_eth_dev *dev,
1130                            uint32_t meter_id,
1131                            struct rte_mtr_stats *stats,
1132                            uint64_t *stats_mask,
1133                            int clear,
1134                            struct rte_mtr_error *error)
1135 {
1136         struct mlx5_priv *priv = dev->data->dev_private;
1137         struct mlx5_flow_meter *fm;
1138         struct mlx5_flow_policer_stats *ps;
1139         uint64_t pkts;
1140         uint64_t bytes;
1141         int ret = 0;
1142
1143         if (!priv->mtr_en)
1144                 return -rte_mtr_error_set(error, ENOTSUP,
1145                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1146                                           "Meter is not supported");
1147         /* Meter object must exist. */
1148         fm = mlx5_flow_meter_find(priv, meter_id);
1149         if (fm == NULL)
1150                 return -rte_mtr_error_set(error, ENOENT,
1151                                           RTE_MTR_ERROR_TYPE_MTR_ID,
1152                                           NULL, "Meter object id not valid.");
1153         ps = &fm->policer_stats;
1154         *stats_mask = 0;
1155         if (fm->green_bytes)
1156                 *stats_mask |= RTE_MTR_STATS_N_BYTES_GREEN;
1157         if (fm->green_pkts)
1158                 *stats_mask |= RTE_MTR_STATS_N_PKTS_GREEN;
1159         if (fm->red_bytes)
1160                 *stats_mask |= RTE_MTR_STATS_N_BYTES_RED;
1161         if (fm->red_pkts)
1162                 *stats_mask |= RTE_MTR_STATS_N_PKTS_RED;
1163         if (fm->bytes_dropped)
1164                 *stats_mask |= RTE_MTR_STATS_N_BYTES_DROPPED;
1165         if (fm->pkts_dropped)
1166                 *stats_mask |= RTE_MTR_STATS_N_PKTS_DROPPED;
1167         memset(stats, 0, sizeof(*stats));
1168         if (ps->pass_cnt) {
1169                 ret = mlx5_counter_query(dev, ps->pass_cnt, clear, &pkts,
1170                                                  &bytes);
1171                 if (ret)
1172                         goto error;
1173                 /* If need to read the packets, set it. */
1174                 if (fm->green_pkts)
1175                         stats->n_pkts[RTE_COLOR_GREEN] = pkts;
1176                 /* If need to read the bytes, set it. */
1177                 if (fm->green_bytes)
1178                         stats->n_bytes[RTE_COLOR_GREEN] = bytes;
1179         }
1180         if (ps->drop_cnt) {
1181                 ret = mlx5_counter_query(dev, ps->drop_cnt, clear, &pkts,
1182                                                  &bytes);
1183                 if (ret)
1184                         goto error;
1185                 /* If need to read the packets, set it. */
1186                 if (fm->pkts_dropped)
1187                         stats->n_pkts_dropped = pkts;
1188                 /* If need to read the bytes, set it. */
1189                 if (fm->bytes_dropped)
1190                         stats->n_bytes_dropped = bytes;
1191         }
1192         return 0;
1193 error:
1194         return -rte_mtr_error_set(error, ret, RTE_MTR_ERROR_TYPE_STATS, NULL,
1195                                  "Failed to read policer counters.");
1196 }
1197
1198 static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
1199         .capabilities_get = mlx5_flow_mtr_cap_get,
1200         .meter_profile_add = mlx5_flow_meter_profile_add,
1201         .meter_profile_delete = mlx5_flow_meter_profile_delete,
1202         .create = mlx5_flow_meter_create,
1203         .destroy = mlx5_flow_meter_destroy,
1204         .meter_enable = mlx5_flow_meter_enable,
1205         .meter_disable = mlx5_flow_meter_disable,
1206         .meter_profile_update = mlx5_flow_meter_profile_update,
1207         .meter_dscp_table_update = NULL,
1208         .policer_actions_update = NULL,
1209         .stats_update = mlx5_flow_meter_stats_update,
1210         .stats_read = mlx5_flow_meter_stats_read,
1211 };
1212
1213 /**
1214  * Get meter operations.
1215  *
1216  * @param dev
1217  *   Pointer to Ethernet device structure.
1218  * @param arg
1219  *   Pointer to set the mtr operations.
1220  *
1221  * @return
1222  *   Always 0.
1223  */
1224 int
1225 mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
1226 {
1227         *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;
1228         return 0;
1229 }
1230
1231 /**
1232  * Find meter by id.
1233  *
1234  * @param priv
1235  *   Pointer to mlx5_priv.
1236  * @param meter_id
1237  *   Meter id.
1238  *
1239  * @return
1240  *   Pointer to the profile found on success, NULL otherwise.
1241  */
1242 struct mlx5_flow_meter *
1243 mlx5_flow_meter_find(struct mlx5_priv *priv, uint32_t meter_id)
1244 {
1245         struct mlx5_flow_meters *fms = &priv->flow_meters;
1246         struct mlx5_flow_meter *fm;
1247
1248         TAILQ_FOREACH(fm, fms, next)
1249                 if (meter_id == fm->meter_id)
1250                         return fm;
1251         return NULL;
1252 }
1253
1254 /**
1255  * Attach meter to flow.
1256  * Unidirectional Meter creation can only be done
1257  * when flow direction is known, i.e. when calling meter_attach.
1258  *
1259  * @param [in] priv
1260  *  Pointer to mlx5 private data.
1261  * @param[in] fm
1262  *   Pointer to flow meter.
1263  * @param [in] attr
1264  *  Pointer to flow attributes.
1265  * @param [out] error
1266  *  Pointer to error structure.
1267  *
1268  * @return
1269  *   0 on success, a negative errno value otherwise and rte_errno is set.
1270  */
1271 int
1272 mlx5_flow_meter_attach(struct mlx5_priv *priv,
1273                        struct mlx5_flow_meter *fm,
1274                        const struct rte_flow_attr *attr,
1275                        struct rte_flow_error *error)
1276 {
1277         int ret = 0;
1278
1279         rte_spinlock_lock(&fm->sl);
1280         if (fm->mfts->meter_action) {
1281                 if (fm->shared &&
1282                     attr->transfer == fm->transfer &&
1283                     attr->ingress == fm->ingress &&
1284                     attr->egress == fm->egress)
1285                         fm->ref_cnt++;
1286                 else
1287                         ret = -1;
1288         } else {
1289                 fm->ingress = attr->ingress;
1290                 fm->egress = attr->egress;
1291                 fm->transfer = attr->transfer;
1292                  fm->ref_cnt = 1;
1293                 /* This also creates the meter object. */
1294                 fm->mfts->meter_action = mlx5_flow_meter_action_create(priv,
1295                                                                        fm);
1296                 if (!fm->mfts->meter_action) {
1297                         fm->ref_cnt = 0;
1298                         fm->ingress = 0;
1299                         fm->egress = 0;
1300                         fm->transfer = 0;
1301                         ret = -1;
1302                         DRV_LOG(ERR, "Meter action create failed.");
1303                 }
1304         }
1305         rte_spinlock_unlock(&fm->sl);
1306         if (ret)
1307                 rte_flow_error_set(error, EINVAL,
1308                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1309                                    fm->mfts->meter_action ?
1310                                    "Meter attr not match" :
1311                                    "Meter action create failed");
1312         return ret ? -rte_errno : 0;
1313 }
1314
1315 /**
1316  * Detach meter from flow.
1317  *
1318  * @param [in] fm
1319  *  Pointer to flow meter.
1320  */
1321 void
1322 mlx5_flow_meter_detach(struct mlx5_flow_meter *fm)
1323 {
1324 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
1325         rte_spinlock_lock(&fm->sl);
1326         MLX5_ASSERT(fm->ref_cnt);
1327         if (--fm->ref_cnt == 0) {
1328                 mlx5_glue->destroy_flow_action(fm->mfts->meter_action);
1329                 fm->mfts->meter_action = NULL;
1330                 fm->ingress = 0;
1331                 fm->egress = 0;
1332                 fm->transfer = 0;
1333         }
1334         rte_spinlock_unlock(&fm->sl);
1335 #else
1336         (void)fm;
1337 #endif
1338 }
1339
1340 /**
1341  * Flush meter configuration.
1342  *
1343  * @param[in] dev
1344  *   Pointer to Ethernet device.
1345  * @param[out] error
1346  *   Pointer to rte meter error structure.
1347  *
1348  * @return
1349  *   0 on success, a negative errno value otherwise and rte_errno is set.
1350  */
1351 int
1352 mlx5_flow_meter_flush(struct rte_eth_dev *dev, struct rte_mtr_error *error)
1353 {
1354         struct mlx5_priv *priv = dev->data->dev_private;
1355         struct mlx5_flow_meters *fms = &priv->flow_meters;
1356         struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
1357         struct mlx5_flow_meter_profile *fmp;
1358         struct mlx5_flow_meter *fm;
1359         const struct rte_flow_attr attr = {
1360                                 .ingress = 1,
1361                                 .egress = 1,
1362                                 .transfer = priv->config.dv_esw_en ? 1 : 0,
1363                         };
1364         void *tmp;
1365
1366         TAILQ_FOREACH_SAFE(fm, fms, next, tmp) {
1367                 /* Meter object must not have any owner. */
1368                 MLX5_ASSERT(!fm->ref_cnt);
1369                 /* Get meter profile. */
1370                 fmp = fm->profile;
1371                 if (fmp == NULL)
1372                         return -rte_mtr_error_set(error, EINVAL,
1373                                 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1374                                 NULL, "MTR object meter profile invalid.");
1375                 /* Update dependencies. */
1376                 fmp->ref_cnt--;
1377                 /* Remove from list. */
1378                 TAILQ_REMOVE(fms, fm, next);
1379                 /* Free policer counters. */
1380                 if (fm->policer_stats.pass_cnt)
1381                         mlx5_counter_free(dev, fm->policer_stats.pass_cnt);
1382                 if (fm->policer_stats.drop_cnt)
1383                         mlx5_counter_free(dev, fm->policer_stats.drop_cnt);
1384                 /* Free meter flow table. */
1385                 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
1386                 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
1387                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
1388         }
1389         TAILQ_FOREACH_SAFE(fmp, fmps, next, tmp) {
1390                 /* Check unused. */
1391                 MLX5_ASSERT(!fmp->ref_cnt);
1392                 /* Remove from list. */
1393                 TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
1394                 mlx5_free(fmp);
1395         }
1396         return 0;
1397 }