Initial commit of vpp code.
[vpp.git] / vnet / vnet / policer / xlate.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <string.h>
16 #include <stddef.h>
17 #include <stdio.h>
18 #include <assert.h>
19 #include <math.h>
20
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23
24 #include <vnet/policer/xlate.h>
25 #include <vnet/policer/police.h>
26
27 #define INTERNAL_SS 1
28
29 /* debugs */
30 #define SSE2_QOS_DEBUG_ERROR(msg, args...) \
31     fformat(stderr, msg "\n", ##args);
32
33 #define SSE2_QOS_DEBUG_INFO(msg, args...) \
34     fformat(stderr, msg "\n", ##args);
35
36
37 #define SSE2_QOS_TR_ERR(TpParms...)
38 // {
39 // }
40
41 #define SSE2_QOS_TR_INFO(TpParms...)
42
43 #ifndef MIN
44 #define MIN(x,y)            (((x)<(y))?(x):(y))
45 #endif
46
47 #ifndef MAX
48 #define MAX(x,y)            (((x)>(y))?(x):(y))
49 #endif
50
51 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AH_OFFSET                   0
52 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AH_MASK                     8
53 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AH_SHIFT                   24
54
55 #define IPE_POLICER_FULL_WRITE_REQUEST_TYPE_OFFSET                    2
56 #define IPE_POLICER_FULL_WRITE_REQUEST_TYPE_MASK                      2
57 #define IPE_POLICER_FULL_WRITE_REQUEST_TYPE_SHIFT                    10
58
59 #define IPE_POLICER_FULL_WRITE_REQUEST_CMD_OFFSET                     3
60 #define IPE_POLICER_FULL_WRITE_REQUEST_CMD_MASK                       2
61 #define IPE_POLICER_FULL_WRITE_REQUEST_CMD_SHIFT                      0
62
63 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AL_OFFSET                   4
64 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AL_MASK                    32
65 #define IPE_POLICER_FULL_WRITE_REQUEST_M40AL_SHIFT                    0
66
67 #define IPE_POLICER_FULL_WRITE_REQUEST_RFC_OFFSET                     8
68 #define IPE_POLICER_FULL_WRITE_REQUEST_RFC_MASK                       2
69 #define IPE_POLICER_FULL_WRITE_REQUEST_RFC_SHIFT                     30
70
71 #define IPE_POLICER_FULL_WRITE_REQUEST_AN_OFFSET                      8
72 #define IPE_POLICER_FULL_WRITE_REQUEST_AN_MASK                        1
73 #define IPE_POLICER_FULL_WRITE_REQUEST_AN_SHIFT                      29
74
75 #define IPE_POLICER_FULL_WRITE_REQUEST_REXP_OFFSET                    8
76 #define IPE_POLICER_FULL_WRITE_REQUEST_REXP_MASK                      4
77 #define IPE_POLICER_FULL_WRITE_REQUEST_REXP_SHIFT                    22
78
79 #define IPE_POLICER_FULL_WRITE_REQUEST_ARM_OFFSET                     9
80 #define IPE_POLICER_FULL_WRITE_REQUEST_ARM_MASK                      11
81 #define IPE_POLICER_FULL_WRITE_REQUEST_ARM_SHIFT                     11
82
83 #define IPE_POLICER_FULL_WRITE_REQUEST_PRM_OFFSET                    10
84 #define IPE_POLICER_FULL_WRITE_REQUEST_PRM_MASK                      11
85 #define IPE_POLICER_FULL_WRITE_REQUEST_PRM_SHIFT                      0
86
87 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLE_OFFSET                   12
88 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLE_MASK                      5
89 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLE_SHIFT                    27
90
91 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLM_OFFSET                   12
92 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLM_MASK                      7
93 #define IPE_POLICER_FULL_WRITE_REQUEST_CBLM_SHIFT                    20
94
95 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLE_OFFSET                   13
96 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLE_MASK                      5
97 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLE_SHIFT                    15
98
99 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLM_OFFSET                   14
100 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLM_MASK                      7
101 #define IPE_POLICER_FULL_WRITE_REQUEST_EBLM_SHIFT                     8
102
103 #define IPE_POLICER_FULL_WRITE_REQUEST_CB_OFFSET                     16
104 #define IPE_POLICER_FULL_WRITE_REQUEST_CB_MASK                       31
105 #define IPE_POLICER_FULL_WRITE_REQUEST_CB_SHIFT                       0
106
107 #define IPE_POLICER_FULL_WRITE_REQUEST_EB_OFFSET                     20
108 #define IPE_POLICER_FULL_WRITE_REQUEST_EB_MASK                       31
109 #define IPE_POLICER_FULL_WRITE_REQUEST_EB_SHIFT                       0
110
111 #define IPE_RFC_RFC2697           0x00000000
112 #define IPE_RFC_RFC2698           0x00000001
113 #define IPE_RFC_RFC4115           0x00000002
114 #define IPE_RFC_MEF5CF1           0x00000003
115
116 /* End of constants copied from sse_ipe_desc_fmt.h */
117
118 /* Misc Policer specific definitions */
119 #define SSE2_QOS_POLICER_FIXED_PKT_SIZE    256
120
121 // TODO check what can be provided by hw macro based on ASIC
122 #define SSE2_QOS_POL_TICKS_PER_SEC     1000LL /* 1 tick = 1 ms */
123
124 /*
125  * Default burst, in ms (byte format)
126  */
127 #define SSE2_QOS_POL_DEF_BURST_BYTE    100
128
129 /*
130  * Minimum burst needs to be such that the largest packet size is accomodated
131  */
132 // Do we need to get it from some lib?
133 #define SSE2_QOS_POL_MIN_BURST_BYTE    9*1024
134
135
136 /*
137  * Flag to indicate if AN is employed or not
138  * 1 - TRUE, 0 - FALSE
139  */
140 #define SSE2_QOS_POL_ALLOW_NEGATIVE    1
141
142 // Various Macros to take care of policer calculations
143
144 #define SSE2_QOS_POL_COMM_BKT_MAX \
145                             (1<<IPE_POLICER_FULL_WRITE_REQUEST_CB_MASK)
146 #define SSE2_QOS_POL_EXTD_BKT_MAX \
147                             (1<<IPE_POLICER_FULL_WRITE_REQUEST_EB_MASK)
148
149 #define SSE2_QOS_POL_RATE_EXP_SIZE \
150                             (IPE_POLICER_FULL_WRITE_REQUEST_REXP_MASK)
151 #define SSE2_QOS_POL_RATE_EXP_MAX  ((1<<SSE2_QOS_POL_RATE_EXP_SIZE) - 1)
152 #define SSE2_QOS_POL_AVG_RATE_MANT_SIZE \
153                             (IPE_POLICER_FULL_WRITE_REQUEST_ARM_MASK)
154 #define SSE2_QOS_POL_AVG_RATE_MANT_MAX    \
155                             ((1<< SSE2_QOS_POL_AVG_RATE_MANT_SIZE) - 1) 
156 #define SSE2_QOS_POL_AVG_RATE_MAX \
157                             (SSE2_QOS_POL_AVG_RATE_MANT_MAX << \
158                              SSE2_QOS_POL_RATE_EXP_MAX)
159
160 #define SSE2_QOS_POL_PEAK_RATE_MANT_SIZE   \
161                             (IPE_POLICER_FULL_WRITE_REQUEST_PRM_MASK)
162 #define SSE2_QOS_POL_PEAK_RATE_MANT_MAX    \
163                             ((1<<SSE2_QOS_POL_PEAK_RATE_MANT_SIZE) - 1) 
164 #define SSE2_QOS_POL_PEAK_RATE_MAX \
165                             (SSE2_QOS_POL_PEAK_RATE_MANT_MAX << \
166                              SSE2_QOS_POL_RATE_EXP_MAX)
167
168 #define SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_SIZE   \
169                         (IPE_POLICER_FULL_WRITE_REQUEST_CBLM_MASK)
170 #define SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_MAX   \
171                         ((1<<SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_SIZE) - 1)
172 #define SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_SIZE   \
173                         (IPE_POLICER_FULL_WRITE_REQUEST_CBLE_MASK)
174 #define SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_MAX   \
175                         ((1<<SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_SIZE) - 1)
176 #define SSE2_QOS_POL_COMM_BKT_LIMIT_MAX \
177                         ((uint64_t)SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_MAX << \
178                          (uint64_t)SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_MAX)
179
180 #define SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_SIZE   \
181                         (IPE_POLICER_FULL_WRITE_REQUEST_EBLM_MASK)
182 #define SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_MAX   \
183                         ((1<<SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_SIZE) - 1)
184 #define SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_SIZE   \
185                         (IPE_POLICER_FULL_WRITE_REQUEST_EBLE_MASK)
186 #define SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_MAX   \
187                         ((1<<SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_SIZE) - 1)
188 #define SSE2_QOS_POL_EXT_BKT_LIMIT_MAX \
189                         ((uint64_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_MAX << \
190                          (uint64_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_MAX)
191
192 /*
193  * Rates determine the units of the bucket
194  *    256.114688 Gbps < Rate                      8 byte units
195  *    128.057344 Gbps < Rate <= 256.114688 Gbps   4 byte units
196  *     64.028672 Gbps < Rate <= 128.057344 Gbps   2 byte units
197  *                      Rate <=  64.028672 Gbps   1 byte units
198  *
199  * The code uses bytes per tick as oppose to Gigabits per second.
200  */
201 #define RATE256 (256114688000LL / 8LL / SSE2_QOS_POL_TICKS_PER_SEC)
202 #define RATE128 (128057344000LL / 8LL / SSE2_QOS_POL_TICKS_PER_SEC)
203 #define RATE64  ( 64028672000LL / 8LL / SSE2_QOS_POL_TICKS_PER_SEC)
204
205 #define RATE_OVER256_UNIT  8LL
206 #define RATE_128TO256_UNIT 4LL
207 #define RATE_64TO128_UNIT  2LL
208
209 static cerrno
210 sse2_qos_pol_round (uint64_t numerator,
211                       uint64_t denominator,
212                       uint64_t *rounded_value,
213                       sse2_qos_round_type_en round_type)
214 {
215     cerrno rc = EOK;
216
217     if (denominator == 0) {
218         SSE2_QOS_DEBUG_ERROR("Illegal denominator");
219         SSE2_QOS_TR_ERR(SSE2_QOSRM_TP_ERR_59);
220         return(EINVAL);
221     }
222
223     switch (round_type) {
224     case SSE2_QOS_ROUND_TO_CLOSEST:
225         *rounded_value = ((numerator + (denominator >> 1)) / denominator);
226         break;
227
228     case SSE2_QOS_ROUND_TO_UP:
229         *rounded_value = (numerator / denominator);
230         if ((*rounded_value * denominator) < numerator) {
231             *rounded_value += 1;
232         }
233         break;
234
235     case SSE2_QOS_ROUND_TO_DOWN:
236         *rounded_value = (numerator / denominator);
237         break;
238
239     case SSE2_QOS_ROUND_INVALID:
240     default:
241         SSE2_QOS_DEBUG_ERROR("Illegal round type");
242         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_60, round_type);
243         rc = EINVAL;
244         break;
245     }
246     return(rc);
247 }
248
249
250 static cerrno
251 sse2_pol_validate_cfg_params (sse2_qos_pol_cfg_params_st *cfg)
252 {
253     uint64_t numer, denom, rnd_value;
254     uint32_t cir_hw, eir_hw;
255     cerrno rc = EOK;
256
257     if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698) &&
258         (cfg->rb.kbps.eir_kbps < cfg->rb.kbps.cir_kbps)) {
259         SSE2_QOS_DEBUG_ERROR("CIR (%u kbps) is greater than PIR (%u kbps)",
260                               cfg->rb.kbps.cir_kbps, cfg->rb.kbps.eir_kbps);
261         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_39, cfg->rb.kbps.cir_kbps,
262                         cfg->rb.kbps.eir_kbps);
263         return(EINVAL);
264     }
265
266     /*
267      * convert rates to bytes-per-tick
268      */
269     numer = (uint64_t)(cfg->rb.kbps.cir_kbps);
270     denom = (uint64_t)(8 * SSE2_QOS_POL_TICKS_PER_SEC) / 1000;
271     rc = sse2_qos_pol_round(numer, denom, &rnd_value,
272                             (sse2_qos_round_type_en) cfg->rnd_type);
273     if (CERR_IS_NOTOK(rc)) {
274         SSE2_QOS_DEBUG_ERROR("Unable to convert CIR to bytes/tick format");
275         // Error traced
276         return(rc);
277     }
278     cir_hw = (uint32_t)rnd_value;
279
280     numer = (uint64_t)(cfg->rb.kbps.eir_kbps);
281     rc = sse2_qos_pol_round(numer, denom, &rnd_value,
282                             (sse2_qos_round_type_en) cfg->rnd_type);
283     if (CERR_IS_NOTOK(rc)) {
284         SSE2_QOS_DEBUG_ERROR("Unable to convert EIR to bytes/tick format");
285         // Error traced
286         return(rc);
287     }
288     eir_hw = (uint32_t)rnd_value;
289
290     if (cir_hw > SSE2_QOS_POL_AVG_RATE_MAX) {
291         SSE2_QOS_DEBUG_ERROR("hw cir (%u bytes/tick) is greater than the "
292                               "max supported value (%u)", cir_hw,
293                               SSE2_QOS_POL_AVG_RATE_MAX);
294         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_84, cir_hw, 
295                          SSE2_QOS_POL_AVG_RATE_MAX);
296         return(EINVAL);
297     }
298
299     if (eir_hw > SSE2_QOS_POL_PEAK_RATE_MAX) {
300         SSE2_QOS_DEBUG_ERROR("hw eir (%u bytes/tick) is greater than the "
301                  "max supported value (%u). Capping it to the max. "
302                  "supported value", eir_hw, SSE2_QOS_POL_PEAK_RATE_MAX);
303         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_85, 
304                  eir_hw, SSE2_QOS_POL_PEAK_RATE_MAX);
305         return(EINVAL);
306     }
307     /*
308      * CIR = 0, with bc != 0 is not allowed
309      */
310     if ((cfg->rb.kbps.cir_kbps == 0) && cfg->rb.kbps.cb_bytes) {
311         SSE2_QOS_DEBUG_ERROR("CIR = 0 with bc != 0");
312         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_55);
313         return(EINVAL);
314     }
315
316     if ((cfg->rb.kbps.eir_kbps == 0) &&
317         (cfg->rfc > SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)) {
318         SSE2_QOS_DEBUG_ERROR("EIR = 0 for a 2R3C policer (rfc: %u)", cfg->rfc);
319         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_23, cfg->rb.kbps.eir_kbps, cfg->rfc);
320         return(EINVAL);
321     }
322
323     if (cfg->rb.kbps.eir_kbps &&
324        (cfg->rfc < SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698)) {
325         SSE2_QOS_DEBUG_ERROR("EIR: %u kbps for a 1-rate policer (rfc: %u)",
326                             cfg->rb.kbps.eir_kbps, cfg->rfc);
327         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_23, cfg->rb.kbps.eir_kbps, cfg->rfc);
328         return(EINVAL);
329     }
330
331     if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes) {
332         SSE2_QOS_DEBUG_ERROR("For a 1R1B policer, EB burst cannot be > 0");
333         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_56);
334         return(EINVAL);
335     }
336
337     return(EOK);
338 }
339
340 static void
341 sse2_qos_convert_value_to_exp_mant_fmt (uint64_t       value,
342                                         uint16_t       max_exp_value,
343                                         uint16_t       max_mant_value,
344                                         sse2_qos_round_type_en type,
345                                         uint8_t        *exp,
346                                         uint32_t       *mant)
347 {
348     uint64_t rnd_value;
349     uint64_t temp_mant;
350     uint8_t temp_exp;
351
352     /*
353      * Select the lowest possible exp, and the largest possible mant
354      */
355     temp_exp = 0;
356     temp_mant = value;
357     while (temp_exp <= max_exp_value) {
358         if (temp_mant <= max_mant_value) {
359             break;
360         }
361
362         temp_exp++;
363         rnd_value = 0;
364         (void)sse2_qos_pol_round((uint64_t)value, (uint64_t)(1 << temp_exp),
365                                &rnd_value, type);
366         temp_mant = rnd_value;
367     }
368
369     if (temp_exp > max_exp_value) {
370         /*
371          * CAP mant to its max value, and decrement exp
372          */
373         temp_exp--;
374         temp_mant = max_mant_value;
375     }
376
377     *exp = temp_exp;
378     *mant = (uint32_t)temp_mant;
379
380     SSE2_QOS_DEBUG_INFO("value: 0x%llx, mant: %u, exp: %u", value, *mant, *exp);
381     return;
382 }
383
384 static cerrno
385 sse2_pol_convert_cfg_rates_to_hw (sse2_qos_pol_cfg_params_st *cfg,
386                                   sse2_qos_pol_hw_params_st  *hw)
387 {
388     cerrno rc = EOK;
389     uint32_t cir_hw, eir_hw, hi_mant, hi_rate, cir_rnded, eir_rnded, eir_kbps;
390     uint64_t numer, denom, rnd_value;
391     uint8_t exp;
392
393     /*
394      * convert rates to bytes-per-tick (tick is 1ms)
395      * For rate conversion, the denominator is gonna be the same
396      */
397     denom = (uint64_t)((SSE2_QOS_POL_TICKS_PER_SEC * 8) / 1000);
398     numer = (uint64_t)(cfg->rb.kbps.cir_kbps);
399     rc = sse2_qos_pol_round(numer, denom, &rnd_value,
400                             (sse2_qos_round_type_en) cfg->rnd_type);
401     if (CERR_IS_NOTOK(rc)) {
402         SSE2_QOS_DEBUG_ERROR("Rounding error, rate: %d kbps, rounding_type: %d",
403                             cfg->rb.kbps.cir_kbps, cfg->rnd_type);
404         // Error is traced
405         return(rc);
406     }
407     cir_hw = (uint32_t)rnd_value;
408
409     if (cfg->rb.kbps.cir_kbps && (cir_hw == 0)) {
410         /*
411          * After rounding, cir_hw = 0. Bump it up
412          */
413         cir_hw = 1;
414     }
415
416     if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) {
417         eir_kbps = 0;
418     } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697) {
419         eir_kbps = cfg->rb.kbps.cir_kbps;
420     } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115) {
421         eir_kbps = cfg->rb.kbps.eir_kbps - cfg->rb.kbps.cir_kbps;
422     } else {
423         eir_kbps = cfg->rb.kbps.eir_kbps;
424     }
425
426     numer = (uint64_t)eir_kbps;
427     rc = sse2_qos_pol_round(numer, denom, &rnd_value,
428                             (sse2_qos_round_type_en) cfg->rnd_type);
429     if (CERR_IS_NOTOK(rc)) {
430         SSE2_QOS_DEBUG_ERROR("Rounding error, rate: %d kbps, rounding_type: %d",
431                               eir_kbps, cfg->rnd_type);
432         // Error is traced
433         return(rc);
434     }
435     eir_hw = (uint32_t)rnd_value;
436
437     if (eir_kbps && (eir_hw == 0)) {
438         /*
439          * After rounding, eir_hw = 0. Bump it up
440          */
441         eir_hw = 1;
442     }
443
444     SSE2_QOS_DEBUG_INFO("cir_hw: %u bytes/tick, eir_hw: %u bytes/tick", cir_hw,
445                          eir_hw);
446
447     if (cir_hw > eir_hw) {
448         hi_rate = cir_hw;
449     } else {
450         hi_rate = eir_hw;
451     }
452
453     if ((cir_hw == 0) && (eir_hw == 0)) {
454         /*
455          * Both the rates are 0. Use exp = 15, and set the RFC to 4115. Also
456          * set AN = 0
457          */
458         exp = (uint8_t)SSE2_QOS_POL_RATE_EXP_MAX;
459         hi_mant = 0;
460         hw->rfc = IPE_RFC_RFC4115;
461         hw->allow_negative = 0;
462     } else {
463         sse2_qos_convert_value_to_exp_mant_fmt(hi_rate, 
464                             (uint16_t)SSE2_QOS_POL_RATE_EXP_MAX,
465                             (uint16_t)SSE2_QOS_POL_AVG_RATE_MANT_MAX,
466                             (sse2_qos_round_type_en) cfg->rnd_type,
467                             &exp, &hi_mant);
468     }
469
470     denom = (1 << exp);
471     if (hi_rate == eir_hw) {
472         hw->peak_rate_man = (uint16_t)hi_mant;
473         rc = sse2_qos_pol_round((uint64_t)cir_hw, denom, &rnd_value,
474                                 (sse2_qos_round_type_en) cfg->rnd_type);
475         hw->avg_rate_man = (uint16_t)rnd_value;
476     } else {
477         hw->avg_rate_man = (uint16_t)hi_mant;
478         rc = sse2_qos_pol_round((uint64_t)eir_hw, denom, &rnd_value,
479                                 (sse2_qos_round_type_en) cfg->rnd_type);
480         hw->peak_rate_man = (uint16_t)rnd_value;
481     }
482     if (CERR_IS_NOTOK(rc)) {
483         SSE2_QOS_DEBUG_ERROR("Rounding error");
484         // Error is traced
485         return(rc);
486     }
487     hw->rate_exp = exp;
488
489     if ((hw->avg_rate_man == 0) && (cfg->rb.kbps.cir_kbps)) {
490         /*
491          * cir was reduced to 0 during rounding. Bump it up
492          */
493         hw->avg_rate_man = 1;
494         SSE2_QOS_DEBUG_INFO("CIR = 0 during rounding. Bump it up to %u "
495                            "bytes/tick", (hw->avg_rate_man << hw->rate_exp));
496     }
497
498     if ((hw->peak_rate_man == 0) && eir_kbps) {
499         /*
500          * eir was reduced to 0 during rounding. Bump it up
501          */
502         hw->peak_rate_man = 1;
503         SSE2_QOS_DEBUG_INFO("EIR = 0 during rounding. Bump it up to %u "
504                            "bytes/tick", (hw->peak_rate_man << hw->rate_exp));
505     }
506
507     cir_rnded = (hw->avg_rate_man << hw->rate_exp);
508     eir_rnded = (hw->peak_rate_man << hw->rate_exp);
509
510     SSE2_QOS_DEBUG_INFO("Configured(rounded) values, cir: %u "
511                        "kbps (mant: %u, exp: %u, rate: %u bytes/tick)",
512                        cfg->rb.kbps.cir_kbps, hw->avg_rate_man, 
513                        hw->rate_exp, cir_rnded);
514
515     SSE2_QOS_DEBUG_INFO("Configured(rounded) values, eir: %u "
516                        "kbps (mant: %u, exp: %u, rate: %u bytes/tick)",
517                        cfg->rb.kbps.eir_kbps, hw->peak_rate_man, 
518                        hw->rate_exp, eir_rnded);
519
520     return(rc);
521 }
522
523 /*****
524  * NAME
525  *   sse2_pol_get_bkt_max
526  *
527  * PARAMETERS
528  *  rate_hw    - either the averate rate or peak rate
529  *  bkt_max    - bit width in the current bucket or extended bucket
530  *
531  * RETURNS
532  *  uint64_t   - maximum token bytes for the current or extended bucket
533  *
534  * DESCRIPTION
535  *  The current bucket or extended bucket fields are in units of either
536  *  1,2,4,8 bytes based on the average or peak rate respective to current
537  *  or extended bucket.
538  *
539  *  To get the actual maximum number of bytes that can be stored in the
540  *  field, the value must be multiplied by the units of either 1,2,4,8
541  *  bytes based on the rate.
542  *****/
543 uint64_t
544 sse2_pol_get_bkt_max (uint64_t rate_hw, uint64_t bkt_max)
545 {
546     if (rate_hw <= RATE64) {
547         return (bkt_max - 1);
548     } else if (rate_hw <= RATE128) {
549         return ((bkt_max * RATE_64TO128_UNIT) - RATE_64TO128_UNIT);
550     } else if (rate_hw <= RATE256) {
551         return ((bkt_max * RATE_128TO256_UNIT) - RATE_128TO256_UNIT);
552     }
553     /* rate must be over 256 */
554     return ((bkt_max * RATE_OVER256_UNIT) - RATE_OVER256_UNIT);
555 }
556
557 /*****
558  * NAME
559  *   sse2_pol_get_bkt_value
560  *
561  * PARAMETERS
562  *  rate_hw    - either the averate rate or peak rate
563  *  byte_value - bytes for this token bucket
564  *
565  * RETURNS
566  *  uint64_t   - unit value for the current or extended bucket field
567  *
568  * DESCRIPTION
569  *  The current bucket or extended bucket fields are in units of either
570  *  1,2,4,8 bytes based on the average or peak rate respective to current
571  *  or extended bucket.
572  *
573  *  To get the units that can be stored in the field, the byte value must
574  *  be divided by the units of either 1,2,4,8 bytes based on the rate.
575  *****/
576 uint64_t
577 sse2_pol_get_bkt_value (uint64_t rate_hw, uint64_t byte_value)
578 {
579     if (rate_hw <= RATE64) {
580         return (byte_value);
581     } else if (rate_hw <= RATE128) {
582         return (byte_value / RATE_64TO128_UNIT);
583     } else if (rate_hw <= RATE256) {
584         return (byte_value / RATE_128TO256_UNIT);
585     }
586     /* rate must be over 256 */
587     return (byte_value / RATE_OVER256_UNIT);
588 }
589
590 static void
591 sse2_pol_rnd_burst_byte_fmt (uint64_t cfg_burst,
592                              uint16_t max_exp_value,
593                              uint16_t max_mant_value,
594                              uint32_t max_bkt_value,
595                              uint32_t rate_hw,
596                              uint8_t *exp,
597                              uint32_t *mant,
598                              uint32_t *bkt_value)
599 {
600     uint64_t    bkt_max=max_bkt_value;
601     uint64_t    bkt_limit_max;
602     uint64_t    rnd_burst;
603     uint64_t    temp_bkt_value;
604
605     bkt_limit_max = ((uint64_t)max_mant_value<<(uint64_t)max_exp_value);
606     bkt_max = sse2_pol_get_bkt_max(rate_hw, bkt_max);
607     bkt_max=MIN(bkt_max, bkt_limit_max);
608     if (!cfg_burst) {
609         /*
610          * If configured burst = 0, compute the burst to be 100ms at a given
611          * rate. Note that for rate_hw = 0, exp = mant = 0.
612          */
613         cfg_burst = (uint64_t)rate_hw * (uint64_t)SSE2_QOS_POL_DEF_BURST_BYTE;
614     }
615
616     if (cfg_burst > bkt_max) {
617         SSE2_QOS_DEBUG_ERROR("burst 0x%llx bytes is greater than the max. "
618                             "supported value 0x%llx bytes. Capping it to the "
619                             "max", cfg_burst, bkt_max);
620         SSE2_QOS_TR_INFO(SSE2_QOS_TP_INFO_38, 
621                           (uint)cfg_burst, (uint)bkt_max);
622         cfg_burst = bkt_max;
623     }
624
625     if (cfg_burst < SSE2_QOS_POL_MIN_BURST_BYTE) {
626         /*
627          * Bump up the burst value ONLY if the cfg_burst is non-zero AND
628          * less than the min. supported value
629          */
630         SSE2_QOS_DEBUG_INFO("burst 0x%llx bytes is less than the min "
631                        "supported value %u bytes. Rounding it up to "
632                        "the min", cfg_burst, SSE2_QOS_POL_MIN_BURST_BYTE);
633         SSE2_QOS_TR_INFO(SSE2_QOS_TP_INFO_39, (uint)cfg_burst,
634                         SSE2_QOS_POL_MIN_BURST_BYTE);
635         cfg_burst = SSE2_QOS_POL_MIN_BURST_BYTE;
636     }
637
638     sse2_qos_convert_value_to_exp_mant_fmt(cfg_burst,
639                                              max_exp_value,
640                                              max_mant_value, 
641                                              SSE2_QOS_ROUND_TO_DOWN,
642                                              exp,
643                                              mant);
644
645     /* Bucket value is based on rate. */
646     rnd_burst = ((uint64_t)(*mant) << (uint64_t)(*exp));
647     temp_bkt_value = sse2_pol_get_bkt_value(rate_hw, rnd_burst);
648     *bkt_value = (uint32_t)temp_bkt_value;
649 }
650
651 static cerrno
652 sse2_pol_convert_cfg_burst_to_hw (sse2_qos_pol_cfg_params_st *cfg,
653                                   sse2_qos_pol_hw_params_st  *hw)
654 {
655     uint8_t  temp_exp;
656     uint32_t temp_mant, rate_hw; 
657     uint64_t eb_bytes;
658     uint32_t bkt_value;
659
660     /*
661      * compute Committed Burst
662      */
663     SSE2_QOS_DEBUG_INFO("Compute commit burst ...");
664     rate_hw = (hw->avg_rate_man) << (hw->rate_exp);
665     sse2_pol_rnd_burst_byte_fmt(cfg->rb.kbps.cb_bytes, 
666                            (uint16_t)SSE2_QOS_POL_COMM_BKT_LIMIT_EXP_MAX,
667                            (uint16_t)SSE2_QOS_POL_COMM_BKT_LIMIT_MANT_MAX,
668                            (uint32_t)SSE2_QOS_POL_COMM_BKT_MAX,
669                            rate_hw, &temp_exp, &temp_mant, &bkt_value);
670     SSE2_QOS_DEBUG_INFO("Committed burst, burst_limit: 0x%llx mant : %u, "
671                        "exp: %u, rnded: 0x%llx cb:%u bytes",
672                        cfg->rb.kbps.cb_bytes, temp_mant, temp_exp, 
673                        ((uint64_t)temp_mant << (uint64_t)temp_exp), bkt_value);
674
675     hw->comm_bkt_limit_exp = temp_exp;
676     hw->comm_bkt_limit_man = (uint8_t)temp_mant;
677     hw->comm_bkt = bkt_value;
678
679     /*
680      * compute Exceed Burst
681      */
682     SSE2_QOS_DEBUG_INFO("Compute exceed burst ...");
683
684     if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) {
685         /*
686          * For 1R2C, hw uses 2R3C (RFC-4115). As such, the Exceed Bucket
687          * params are set to 0. Recommendation is to use EB_exp = max_exp (=15)
688          * and EB_mant = 0
689          */
690         hw->extd_bkt_limit_exp = (uint8_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_MAX;
691         hw->extd_bkt_limit_man = 0;
692         SSE2_QOS_DEBUG_INFO("Excess burst, burst: 0x%llx mant: %u, "
693                "exp: %u, rnded: 0x%llx bytes", 
694                cfg->rb.kbps.eb_bytes, hw->extd_bkt_limit_man,
695                hw->extd_bkt_limit_exp,
696                ((uint64_t)hw->extd_bkt_limit_man <<
697                 (uint64_t)hw->extd_bkt_limit_exp));
698         SSE2_QOS_TR_INFO(SSE2_QOS_TP_INFO_20, (uint)cfg->rb.kbps.eb_bytes, 
699                             hw->extd_bkt_limit_man, hw->extd_bkt_limit_exp);
700         return(EOK);
701     }
702
703     if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697) {
704         eb_bytes = cfg->rb.kbps.cb_bytes + cfg->rb.kbps.eb_bytes;
705     } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115) {
706         eb_bytes = cfg->rb.kbps.eb_bytes - cfg->rb.kbps.cb_bytes;
707     } else {
708         eb_bytes = cfg->rb.kbps.eb_bytes;
709     }
710
711     rate_hw = (hw->peak_rate_man) << (hw->rate_exp);
712     sse2_pol_rnd_burst_byte_fmt(eb_bytes, 
713                            (uint16_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_EXP_MAX,
714                            (uint16_t)SSE2_QOS_POL_EXTD_BKT_LIMIT_MANT_MAX,
715                            (uint32_t)SSE2_QOS_POL_EXTD_BKT_MAX,
716                            rate_hw, &temp_exp, &temp_mant, &bkt_value);
717
718     SSE2_QOS_DEBUG_INFO("Excess burst, burst_limit: 0x%llx mant: %u, "
719                        "exp: %u, rnded: 0x%llx eb:%u bytes", 
720                        cfg->rb.kbps.eb_bytes, temp_mant, temp_exp,
721                        ((uint64_t)temp_mant << (uint64_t)temp_exp), bkt_value);
722
723     hw->extd_bkt_limit_exp = (uint8_t)temp_exp;
724     hw->extd_bkt_limit_man = (uint8_t)temp_mant;
725     hw->extd_bkt = bkt_value;
726
727     return(EOK);
728 }
729
730
731 /*
732  * Input: configured parameter values in 'cfg'.
733  * Output: h/w programmable parameter values in 'hw'.
734  * Return: success or failure code.
735  */
736 static cerrno
737 sse2_pol_convert_cfg_to_hw_params (sse2_qos_pol_cfg_params_st *cfg,
738                                    sse2_qos_pol_hw_params_st  *hw)
739 {
740     cerrno rc = EOK;
741
742     /*
743      * clear the hw_params
744      */
745     memset(hw, 0, sizeof(sse2_qos_pol_hw_params_st));
746
747     hw->allow_negative = SSE2_QOS_POL_ALLOW_NEGATIVE;
748
749     if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) ||
750         (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)) {
751         hw->rfc = IPE_RFC_RFC4115;
752     } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697) {
753         hw->rfc = IPE_RFC_RFC2697;
754     } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698) {
755         hw->rfc = IPE_RFC_RFC2698;
756     } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1) {
757         hw->rfc = IPE_RFC_MEF5CF1;
758     } else {
759         SSE2_QOS_DEBUG_ERROR("Invalid RFC type %d\n", cfg->rfc);
760         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_61, cfg->rfc);
761         return(EINVAL);
762     }
763
764     rc = sse2_pol_convert_cfg_rates_to_hw(cfg, hw);
765     if (CERR_IS_NOTOK(rc)) {
766         SSE2_QOS_DEBUG_ERROR("Unable to convert config rates to hw. Error: %d",
767                               rc);
768         // Error is traced
769         return(rc);
770     }
771
772     rc = sse2_pol_convert_cfg_burst_to_hw(cfg, hw);
773     if (CERR_IS_NOTOK(rc)) {
774         SSE2_QOS_DEBUG_ERROR("Unable to convert config burst to hw. Error: %d",
775                               rc);
776         // Error is traced
777         return(rc);
778     }
779
780     return OK_pushHW;
781 }
782
783
784 uint32_t
785 sse2_qos_convert_pps_to_kbps (uint32_t rate_pps)
786 {
787     // sse2_qos_ship_inc_counter(SSE2_QOS_SHIP_COUNTER_TYPE_API_CNT,
788     //                            SSE2_QOS_SHIP_CNT_POL_CONV_PPS_TO_KBPS);
789
790     uint64_t numer, rnd_value = 0;
791
792     numer = (uint64_t)((uint64_t)rate_pps *
793             (uint64_t)SSE2_QOS_POLICER_FIXED_PKT_SIZE * 8LL);
794     (void)sse2_qos_pol_round(numer, 1000LL, &rnd_value,
795                               SSE2_QOS_ROUND_TO_CLOSEST);
796
797     return ((uint32_t)rnd_value);
798 }
799
800 uint32_t
801 sse2_qos_convert_burst_ms_to_bytes (uint32_t burst_ms,
802                                      uint32_t rate_kbps)
803 {
804     uint64_t numer, rnd_value = 0;
805
806     //sse2_qos_ship_inc_counter(SSE2_QOS_SHIP_COUNTER_TYPE_API_CNT,
807     //                          SSE2_QOS_SHIP_CNT_POL_CONV_BURST_MS_TO_BYTES);
808
809     numer = (uint64_t)((uint64_t)burst_ms * (uint64_t)rate_kbps);
810
811     (void)sse2_qos_pol_round(numer, 8LL, &rnd_value,
812                              SSE2_QOS_ROUND_TO_CLOSEST);
813
814     return ((uint32_t)rnd_value);
815 }
816
817
818 /*
819  * Input: configured parameters in 'cfg'.
820  * Output: h/w parameters are returned in 'hw',
821  * Return: Status, success or failure code.
822  */
823 trans_layer_rc
824 sse2_pol_compute_hw_params (sse2_qos_pol_cfg_params_st *cfg,
825                             sse2_qos_pol_hw_params_st  *hw)
826 {
827     cerrno rc = EOK;
828
829     if (!cfg || !hw) {
830         SSE2_QOS_DEBUG_ERROR("Illegal parameters");
831         return(Not_OK);
832     }
833
834     /*
835      * Validate the police config params being presented to RM
836      */
837     rc = sse2_pol_validate_cfg_params(cfg);
838     if (CERR_IS_NOTOK(rc)) {
839         SSE2_QOS_DEBUG_ERROR("Config parameter validation failed. Error: %d",
840                             rc);
841         // Error is traced
842         return(Not_OK);
843     }
844
845     /*
846      * first round configured values to h/w supported values. This func
847      * also determines whether 'tick' or 'byte' format
848      */
849     rc = sse2_pol_convert_cfg_to_hw_params(cfg, hw);
850     if (CERR_IS_NOTOK(rc)) {
851         SSE2_QOS_DEBUG_ERROR("Unable to convert config params to hw params. "
852                             "Error: %d", rc);
853         SSE2_QOS_TR_ERR(SSE2_QOS_TP_ERR_53, rc);
854         return(Not_OK);
855     }
856
857     return OK_pushHW;
858 }
859
860
861 #if defined (INTERNAL_SS) || defined (X86)
862
863 // For initializing the x86 policer format
864
865 /*
866  * Return the number of hardware TSC timer ticks per second for the dataplane.
867  * This is approximately, but not exactly, the clock speed.
868  */
869 static uint64_t get_tsc_hz(void) 
870 {
871     f64 cpu_freq;
872
873     cpu_freq = os_cpu_clock_frequency();
874     return (uint64_t) cpu_freq;
875 }
876
877 /*
878  * Convert rates into bytes_per_period and scale.
879  * Return 0 if ok or 1 if error.
880  */
881 static int 
882 compute_policer_params (uint64_t hz,                 // CPU speed in clocks per second
883                         uint64_t cir_rate,           // in bytes per second
884                         uint64_t pir_rate,           // in bytes per second
885                         uint32_t *current_limit,     // in bytes, output may scale the input
886                         uint32_t *extended_limit,    // in bytes, output may scale the input
887                         uint32_t *cir_bytes_per_period,
888                         uint32_t *pir_bytes_per_period,
889                         uint32_t *scale)
890 {
891     double period;
892     double internal_cir_bytes_per_period;
893     double internal_pir_bytes_per_period;
894     uint32_t max;
895     uint32_t scale_shift;
896     uint32_t scale_amount;
897     uint32_t orig_current_limit = *current_limit;
898
899     // Compute period. For 1Ghz-to-8Ghz CPUs, the period will be in 
900     // the range of 16 to 116 usec.
901     period = ((double) hz) / ((double) POLICER_TICKS_PER_PERIOD);
902
903     // Determine bytes per period for each rate
904     internal_cir_bytes_per_period = (double)cir_rate / period;
905     internal_pir_bytes_per_period = (double)pir_rate / period;
906
907     // Scale if possible. Scaling helps rate accuracy, but is contrained 
908     // by the scaled rates and limits fitting in 32-bits.
909     // In addition, we need to insure the scaled rate is no larger than
910     // 2^22 tokens per period. This allows the dataplane to ignore overflow
911     // in the tokens-per-period multiplication since it could only
912     // happen if the policer were idle for more than a year.
913     // This is not really a constraint because 100Gbps at 1Ghz is only
914     // 1.6M tokens per period.
915 #define MAX_RATE_SHIFT 10
916     max = MAX(*current_limit, *extended_limit);
917     max = MAX(max, (uint32_t)internal_cir_bytes_per_period << MAX_RATE_SHIFT);
918     max = MAX(max, (uint32_t)internal_pir_bytes_per_period << MAX_RATE_SHIFT);
919     scale_shift = __builtin_clz(max);
920     
921     scale_amount = 1 << scale_shift;
922     *scale = scale_shift;
923     
924     // Scale the limits
925     *current_limit = *current_limit << scale_shift;
926     *extended_limit = *extended_limit << scale_shift;
927
928     // Scale the rates
929     internal_cir_bytes_per_period = internal_cir_bytes_per_period * ((double)scale_amount);
930     internal_pir_bytes_per_period = internal_pir_bytes_per_period * ((double)scale_amount);
931
932     // Make sure the new rates are reasonable
933     // Only needed for very low rates with large bursts
934     if (internal_cir_bytes_per_period < 1.0) {
935         internal_cir_bytes_per_period = 1.0;
936     }
937     if (internal_pir_bytes_per_period < 1.0) {
938         internal_pir_bytes_per_period = 1.0;
939     }
940
941     *cir_bytes_per_period = (uint32_t)internal_cir_bytes_per_period;
942     *pir_bytes_per_period = (uint32_t)internal_pir_bytes_per_period;
943
944 // #define PRINT_X86_POLICE_PARAMS 
945 #ifdef PRINT_X86_POLICE_PARAMS
946     {
947         uint64_t effective_BPS;
948
949         // This value actually slightly conservative because it doesn't take into account
950         // the partial period at the end of a second. This really matters only for very low
951         // rates.
952         effective_BPS = (((uint64_t) (*cir_bytes_per_period * (uint64_t)period)) >> *scale ); 
953
954         printf("hz=%llu, cir_rate=%llu, limit=%u => "
955                "periods-per-sec=%d usec-per-period=%d => "
956                "scale=%d cir_BPP=%u, scaled_limit=%u => "
957                "effective BPS=%llu, accuracy=%f\n",
958           // input values
959             (unsigned long long)hz,
960             (unsigned long long)cir_rate,
961             orig_current_limit,
962           // computed values
963             (uint32_t)(period),                   // periods per second 
964             (uint32_t)(1000.0 * 1000.0 / period), // in usec
965             *scale,
966             *cir_bytes_per_period,
967             *current_limit,
968           // accuracy
969             (unsigned long long)effective_BPS,
970             (double)cir_rate / (double)effective_BPS);
971     }
972 #else
973     orig_current_limit = orig_current_limit;  // Make compiler happy
974 #endif
975
976     return 0; // ok
977 }
978
979
980 /*
981  * Input: configured parameters in 'cfg'.
982  * Output: h/w parameters are returned in 'hw',
983  * Return: Status, success or failure code.
984  */
985 trans_layer_rc
986 x86_pol_compute_hw_params (sse2_qos_pol_cfg_params_st *cfg,
987                            policer_read_response_type_st *hw)
988 {
989     const int BYTES_PER_KBIT = (1000 / 8);
990     uint64_t hz;
991     uint32_t cap;
992
993     if (!cfg || !hw) {
994         SSE2_QOS_DEBUG_ERROR("Illegal parameters");
995         return(Not_OK);
996     }
997
998     hz = get_tsc_hz();
999     hw->last_update_time = 0;
1000
1001     // Cap the bursts to 32-bits. This allows up to almost one second of
1002     // burst on a 40GE interface, which should be fine for x86.
1003     cap = (cfg->rb.kbps.cb_bytes > 0xFFFFFFFF) ? 0xFFFFFFFF : cfg->rb.kbps.cb_bytes;
1004     hw->current_limit = cap;
1005     cap = (cfg->rb.kbps.eb_bytes > 0xFFFFFFFF) ? 0xFFFFFFFF : cfg->rb.kbps.eb_bytes;
1006     hw->extended_limit = cap;
1007
1008     if ((cfg->rb.kbps.cir_kbps == 0) && (cfg->rb.kbps.cb_bytes == 0) && (cfg->rb.kbps.eb_bytes == 0)) {
1009         // This is a uninitialized, always-violate policer
1010         hw->single_rate = 1;
1011         hw->cir_tokens_per_period = 0;
1012         return OK_pushHW;
1013     }
1014
1015     if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) ||
1016         (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697)) {
1017         // Single-rate policer
1018
1019         hw->single_rate = 1;
1020
1021         if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_1R2C) && cfg->rb.kbps.eb_bytes) {
1022             SSE2_QOS_DEBUG_ERROR("Policer parameter validation failed -- 1R2C.");
1023             return(Not_OK);
1024         }
1025
1026         if ((cfg->rb.kbps.cir_kbps == 0) ||
1027             (cfg->rb.kbps.eir_kbps != 0) ||
1028             ((cfg->rb.kbps.cb_bytes == 0) && (cfg->rb.kbps.eb_bytes == 0))) {
1029             SSE2_QOS_DEBUG_ERROR("Policer parameter validation failed -- 1R.");
1030             return(Not_OK);
1031         }
1032
1033         if (compute_policer_params(hz, 
1034                                    (uint64_t)cfg->rb.kbps.cir_kbps * BYTES_PER_KBIT, 
1035                                    0,
1036                                    &hw->current_limit, 
1037                                    &hw->extended_limit,
1038                                    &hw->cir_tokens_per_period,
1039                                    &hw->pir_tokens_per_period, 
1040                                    &hw->scale)) {
1041             SSE2_QOS_DEBUG_ERROR("Policer parameter computation failed.");
1042             return(Not_OK);
1043         }
1044
1045     } else if ((cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698) ||
1046                (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115)) {
1047         // Two-rate policer
1048
1049         if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115) {
1050             hw->color_aware = 1;
1051         }
1052
1053         if ((cfg->rb.kbps.cir_kbps == 0) || (cfg->rb.kbps.eir_kbps == 0) || (cfg->rb.kbps.eir_kbps < cfg->rb.kbps.cir_kbps) ||
1054                 (cfg->rb.kbps.cb_bytes == 0) || (cfg->rb.kbps.eb_bytes == 0)) {
1055             SSE2_QOS_DEBUG_ERROR("Config parameter validation failed.");
1056             return(Not_OK);
1057         }
1058
1059         if (compute_policer_params(hz, 
1060                                    (uint64_t)cfg->rb.kbps.cir_kbps * BYTES_PER_KBIT, 
1061                                    (uint64_t)cfg->rb.kbps.eir_kbps * BYTES_PER_KBIT, 
1062                                    &hw->current_limit, 
1063                                    &hw->extended_limit,
1064                                    &hw->cir_tokens_per_period,
1065                                    &hw->pir_tokens_per_period, 
1066                                    &hw->scale)) {
1067             SSE2_QOS_DEBUG_ERROR("Policer parameter computation failed.");
1068             return(Not_OK);
1069         }
1070
1071     } else {
1072         SSE2_QOS_DEBUG_ERROR("Config parameter validation failed. RFC not supported");
1073         return(Not_OK);
1074     }
1075
1076     hw->current_bucket = hw->current_limit;
1077     hw->extended_bucket = hw->extended_limit;
1078
1079     return OK_pushHW;
1080 }
1081 #endif
1082
1083
1084 /*
1085  * Input: configured parameters in 'cfg'.
1086  * Output: physical structure is returned in 'phys',
1087  * Return: Status, success or failure code.
1088  */
1089 trans_layer_rc
1090 sse2_pol_logical_2_physical (sse2_qos_pol_cfg_params_st    *cfg,
1091                              policer_read_response_type_st *phys)
1092 {
1093     trans_layer_rc rc;
1094     sse2_qos_pol_hw_params_st pol_hw;
1095     sse2_qos_pol_cfg_params_st kbps_cfg;
1096
1097     memset(phys, 0, sizeof(policer_read_response_type_st));
1098     memset(&kbps_cfg, 0, sizeof(sse2_qos_pol_cfg_params_st));
1099
1100     if (!cfg) {
1101         SSE2_QOS_DEBUG_ERROR("Illegal parameters");
1102         return(Not_OK);
1103     }
1104
1105     switch (cfg->rate_type) {
1106     case SSE2_QOS_RATE_KBPS:
1107         /* copy all the data into kbps_cfg */
1108         kbps_cfg.rb.kbps.cir_kbps = cfg->rb.kbps.cir_kbps;
1109         kbps_cfg.rb.kbps.eir_kbps = cfg->rb.kbps.eir_kbps;
1110         kbps_cfg.rb.kbps.cb_bytes = cfg->rb.kbps.cb_bytes;
1111         kbps_cfg.rb.kbps.eb_bytes = cfg->rb.kbps.eb_bytes;
1112         break;
1113     case SSE2_QOS_RATE_PPS:
1114         kbps_cfg.rb.kbps.cir_kbps =
1115             sse2_qos_convert_pps_to_kbps(cfg->rb.pps.cir_pps);
1116         kbps_cfg.rb.kbps.eir_kbps =
1117             sse2_qos_convert_pps_to_kbps(cfg->rb.pps.eir_pps);
1118         kbps_cfg.rb.kbps.cb_bytes = sse2_qos_convert_burst_ms_to_bytes(
1119             (uint32_t) cfg->rb.pps.cb_ms, kbps_cfg.rb.kbps.cir_kbps);
1120         kbps_cfg.rb.kbps.eb_bytes = sse2_qos_convert_burst_ms_to_bytes(
1121             (uint32_t) cfg->rb.pps.eb_ms, kbps_cfg.rb.kbps.eir_kbps);
1122         break;
1123     default:
1124         SSE2_QOS_DEBUG_ERROR("Illegal rate type");
1125         return(Not_OK);
1126     }
1127
1128     /* rate type is now converted to kbps */
1129     kbps_cfg.rate_type = SSE2_QOS_RATE_KBPS;
1130     kbps_cfg.rnd_type  = cfg->rnd_type;
1131     kbps_cfg.rfc       = cfg->rfc;
1132
1133 #if !defined (INTERNAL_SS) && !defined (X86)
1134     // convert logical into hw params which involves qos calculations
1135     rc = sse2_pol_compute_hw_params(&kbps_cfg, &pol_hw);
1136     if (rc == Not_OK) {
1137         SSE2_QOS_DEBUG_ERROR("Unable to compute hw param. Error: %d", rc);
1138         return (rc);
1139     }
1140
1141     // convert hw params into the physical
1142     phys->rfc  = pol_hw.rfc;
1143     phys->an   = pol_hw.allow_negative;
1144     phys->rexp = pol_hw.rate_exp;
1145     phys->arm  = pol_hw.avg_rate_man;
1146     phys->prm  = pol_hw.peak_rate_man;
1147     phys->cble = pol_hw.comm_bkt_limit_exp;
1148     phys->cblm = pol_hw.comm_bkt_limit_man;
1149     phys->eble = pol_hw.extd_bkt_limit_exp;
1150     phys->eblm = pol_hw.extd_bkt_limit_man;
1151     phys->cb   = pol_hw.comm_bkt;
1152     phys->eb   = pol_hw.extd_bkt;
1153
1154     /* for debugging purposes, the bucket token values can be overwritten */
1155     if (cfg->overwrite_bucket) {
1156         phys->cb = cfg->current_bucket;
1157         phys->eb = cfg->extended_bucket;
1158     }
1159 #else
1160     // convert logical into hw params which involves qos calculations
1161     rc = x86_pol_compute_hw_params(&kbps_cfg, phys);
1162     if (rc == Not_OK) {
1163         SSE2_QOS_DEBUG_ERROR("Unable to compute hw param. Error: %d", rc);
1164         return (rc);
1165     }
1166
1167     /* for debugging purposes, the bucket token values can be overwritten */
1168     if (cfg->overwrite_bucket) {
1169         phys->current_bucket = cfg->current_bucket;
1170         phys->extended_bucket = cfg->extended_bucket;
1171     }
1172
1173     // Touch to avoid compiler warning for X86
1174     pol_hw.allow_negative = pol_hw.allow_negative;
1175
1176 #endif // if !defined (INTERNAL_SS) && !defined (X86)
1177
1178     return OK_pushHW;
1179 }
1180
1181
1182 static void
1183 sse2_qos_convert_pol_bucket_to_hw_fmt (
1184     policer_read_response_type_st *bkt,
1185     sse2_qos_pol_hw_params_st *hw_fmt)
1186 {
1187     memset(hw_fmt, 0, sizeof(sse2_qos_pol_hw_params_st));
1188 #if !defined (INTERNAL_SS) && !defined (X86)
1189     hw_fmt->rfc = (uint8_t)bkt->rfc;
1190     hw_fmt->allow_negative = (uint8_t)bkt->an;
1191     hw_fmt->rate_exp = (uint8_t)bkt->rexp;
1192     hw_fmt->avg_rate_man = (uint16_t)bkt->arm;
1193     hw_fmt->peak_rate_man = (uint16_t)bkt->prm;
1194     hw_fmt->comm_bkt_limit_man = (uint8_t)bkt->cblm;
1195     hw_fmt->comm_bkt_limit_exp = (uint8_t)bkt->cble;
1196     hw_fmt->extd_bkt_limit_man = (uint8_t)bkt->eblm;
1197     hw_fmt->extd_bkt_limit_exp = (uint8_t)bkt->eble;
1198     hw_fmt->extd_bkt = bkt->eb;
1199     hw_fmt->comm_bkt = bkt->cb;
1200 #endif // if !defined (INTERNAL_SS) && !defined (X86)
1201 }
1202
1203 /*
1204  * Input: h/w programmable parameter values in 'hw'
1205  * Output: configured parameter values in 'cfg'
1206  * Return: Status, success or failure code.
1207  */
1208 static cerrno
1209 sse2_pol_convert_hw_to_cfg_params (sse2_qos_pol_hw_params_st  *hw,
1210                                    sse2_qos_pol_cfg_params_st *cfg)
1211 {
1212     uint64_t temp_rate;
1213
1214     if ((hw == NULL) || (cfg == NULL)) {
1215         return EINVAL;
1216     }
1217
1218     if ((hw->rfc == IPE_RFC_RFC4115) &&
1219         !(hw->peak_rate_man << hw->rate_exp) &&
1220         !(hw->extd_bkt_limit_man)) {
1221         /*
1222          * For a 1R2C, we set EIR = 0, EB = 0
1223          */
1224         cfg->rfc = SSE2_QOS_POLICER_TYPE_1R2C;
1225     } else if (hw->rfc == IPE_RFC_RFC2697) {
1226         cfg->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
1227     } else if (hw->rfc == IPE_RFC_RFC2698) {
1228         cfg->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
1229     } else if (hw->rfc == IPE_RFC_RFC4115) {
1230         cfg->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
1231     } else if (hw->rfc == IPE_RFC_MEF5CF1) {
1232         cfg->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
1233     } else {
1234         return EINVAL;
1235     }
1236
1237     temp_rate = ((hw->avg_rate_man << hw->rate_exp) * 8LL *
1238                 SSE2_QOS_POL_TICKS_PER_SEC)/1000;
1239     cfg->rb.kbps.cir_kbps = (uint32_t)temp_rate;
1240
1241     temp_rate = ((hw->peak_rate_man << hw->rate_exp) * 8LL *
1242                 SSE2_QOS_POL_TICKS_PER_SEC)/1000;
1243     cfg->rb.kbps.eir_kbps = (uint32_t)temp_rate;
1244
1245     cfg->rb.kbps.cb_bytes = ((uint64_t)hw->comm_bkt_limit_man << 
1246                             (uint64_t)hw->comm_bkt_limit_exp);
1247     cfg->rb.kbps.eb_bytes = ((uint64_t)hw->extd_bkt_limit_man << 
1248                             (uint64_t)hw->extd_bkt_limit_exp);
1249
1250     if (cfg->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697) {
1251        /*
1252         * For 1R3C in the hardware, EB = sum(CB, EB). Also, EIR = CIR. Restore
1253         * values such that the configured params don't reflect this adjustment
1254         */
1255         cfg->rb.kbps.eb_bytes = (cfg->rb.kbps.eb_bytes -
1256                                  cfg->rb.kbps.cb_bytes);
1257         cfg->rb.kbps.eir_kbps = 0;
1258     } else if (cfg->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115) {
1259        /*
1260         * For 4115 in the hardware is excess rate and burst, but EA provides
1261         * peak-rate, so adjust it to be eir
1262         */
1263         cfg->rb.kbps.eir_kbps += cfg->rb.kbps.cir_kbps;
1264         cfg->rb.kbps.eb_bytes += cfg->rb.kbps.cb_bytes;
1265     }
1266     /* h/w conversion to cfg is in kbps */
1267     cfg->rate_type        = SSE2_QOS_RATE_KBPS;
1268     cfg->overwrite_bucket = 0;
1269     cfg->current_bucket   = hw->comm_bkt;
1270     cfg->extended_bucket  = hw->extd_bkt;
1271
1272     SSE2_QOS_DEBUG_INFO("configured params, cir: %u kbps, eir: %u kbps, cb "
1273                      "burst: 0x%llx bytes, eb burst: 0x%llx bytes",
1274                         cfg->rb.kbps.cir_kbps, cfg->rb.kbps.eir_kbps,
1275                         cfg->rb.kbps.cb_bytes, cfg->rb.kbps.eb_bytes);
1276     SSE2_QOS_TR_INFO(SSE2_QOS_TP_INFO_22, cfg->rb.kbps.cir_kbps,
1277                    cfg->rb.kbps.eir_kbps,
1278                    (uint)cfg->rb.kbps.cb_bytes, (uint)cfg->rb.kbps.eb_bytes);
1279
1280     return EOK;
1281 }
1282
1283 uint32_t
1284 sse2_qos_convert_kbps_to_pps (uint32_t rate_kbps)
1285 {
1286     uint64_t numer, denom, rnd_value = 0;
1287
1288     // sse_qosrm_ship_inc_counter(SSE2_QOS_SHIP_COUNTER_TYPE_API_CNT,
1289     //                            SSE2_QOS_SHIP_CNT_POL_CONV_KBPS_TO_PPS);
1290
1291     numer = (uint64_t)((uint64_t)rate_kbps * 1000LL);
1292     denom = (uint64_t)((uint64_t)SSE2_QOS_POLICER_FIXED_PKT_SIZE * 8LL);
1293
1294     (void)sse2_qos_pol_round(numer, denom, &rnd_value,
1295                              SSE2_QOS_ROUND_TO_CLOSEST);
1296
1297     return((uint32_t)rnd_value);
1298 }
1299
1300 uint32_t
1301 sse2_qos_convert_burst_bytes_to_ms (uint64_t burst_bytes,
1302                                     uint32_t rate_kbps)
1303 {
1304     uint64_t numer, denom, rnd_value = 0;
1305
1306     //sse_qosrm_ship_inc_counter(SSE2_QOS_SHIP_COUNTER_TYPE_API_CNT,
1307     //                         SSE2_QOS_SHIP_CNT_POL_CONV_BYTES_TO_BURST_MS);
1308
1309     numer = burst_bytes * 8LL;
1310     denom = (uint64_t)rate_kbps;
1311
1312     (void)sse2_qos_pol_round(numer, denom, &rnd_value,
1313                              SSE2_QOS_ROUND_TO_CLOSEST);
1314
1315     return((uint32_t)rnd_value);
1316 }
1317
1318 /*
1319  * Input: physical structure in 'phys', rate_type in cfg
1320  * Output: configured parameters in 'cfg'.
1321  * Return: Status, success or failure code.
1322  */
1323 trans_layer_rc
1324 sse2_pol_physical_2_logical (policer_read_response_type_st *phys,
1325                              sse2_qos_pol_cfg_params_st    *cfg)
1326 {
1327     cerrno rc;
1328     sse2_qos_pol_hw_params_st pol_hw;
1329     sse2_qos_pol_cfg_params_st kbps_cfg;
1330
1331     memset(&pol_hw, 0, sizeof(sse2_qos_pol_hw_params_st));
1332     memset(&kbps_cfg, 0, sizeof(sse2_qos_pol_cfg_params_st));
1333
1334     if (!phys) {
1335         SSE2_QOS_DEBUG_ERROR("Illegal parameters");
1336         return(Not_OK);
1337     }
1338
1339     sse2_qos_convert_pol_bucket_to_hw_fmt (phys, &pol_hw);
1340
1341     rc = sse2_pol_convert_hw_to_cfg_params(&pol_hw, &kbps_cfg);
1342     if (CERR_IS_NOTOK(rc)) {
1343         SSE2_QOS_DEBUG_ERROR("Unable to convert hw params to config params. "
1344                             "Error: %d", rc);
1345         return(Not_OK);
1346     }
1347
1348     /* check what rate type is required */
1349     switch (cfg->rate_type) {
1350     case SSE2_QOS_RATE_KBPS:
1351         /* copy all the data into kbps_cfg */
1352         cfg->rb.kbps.cir_kbps = kbps_cfg.rb.kbps.cir_kbps;
1353         cfg->rb.kbps.eir_kbps = kbps_cfg.rb.kbps.eir_kbps;
1354         cfg->rb.kbps.cb_bytes = kbps_cfg.rb.kbps.cb_bytes;
1355         cfg->rb.kbps.eb_bytes = kbps_cfg.rb.kbps.eb_bytes;
1356         break;
1357     case SSE2_QOS_RATE_PPS:
1358         cfg->rb.pps.cir_pps =
1359             sse2_qos_convert_kbps_to_pps(kbps_cfg.rb.kbps.cir_kbps);
1360         cfg->rb.pps.eir_pps =
1361             sse2_qos_convert_kbps_to_pps(kbps_cfg.rb.kbps.eir_kbps);
1362         cfg->rb.pps.cb_ms = sse2_qos_convert_burst_bytes_to_ms(
1363             kbps_cfg.rb.kbps.cb_bytes, kbps_cfg.rb.kbps.cir_kbps);
1364         cfg->rb.pps.eb_ms = sse2_qos_convert_burst_bytes_to_ms(
1365             kbps_cfg.rb.kbps.eb_bytes, kbps_cfg.rb.kbps.eir_kbps);
1366         break;
1367     default:
1368         SSE2_QOS_DEBUG_ERROR("Illegal rate type");
1369         return(Not_OK);
1370     }
1371
1372     /* cfg->rate_type remains what it was */
1373     cfg->rnd_type         = kbps_cfg.rnd_type;
1374     cfg->rfc              = kbps_cfg.rfc;
1375     cfg->overwrite_bucket = kbps_cfg.overwrite_bucket;
1376     cfg->current_bucket   = kbps_cfg.current_bucket;
1377     cfg->extended_bucket  = kbps_cfg.extended_bucket;
1378
1379     return OK_pushHW;
1380 }