a077e0c5cc16b7195aec2fa49be8ba6d3bc30a09
[openwrt/openwrt.git] / target / linux / mediatek / files-5.10 / drivers / net / phy / rtk / rtl8367c / rate.c
1 /*
2 * Copyright (C) 2013 Realtek Semiconductor Corp.
3 * All Rights Reserved.
4 *
5 * Unless you and Realtek execute a separate written software license
6 * agreement governing use of this software, this software is licensed
7 * to you under the terms of the GNU General Public License version 2,
8 * available at https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
9 *
10 * $Revision: 76306 $
11 * $Date: 2017-03-08 15:13:58 +0800 (週三, 08 三月 2017) $
12 *
13 * Purpose : RTK switch high-level API for RTL8367/RTL8367C
14 * Feature : Here is a list of all functions and variables in rate module.
15 *
16 */
17
18 #include <rtk_switch.h>
19 #include <rtk_error.h>
20 #include <rate.h>
21 #include <qos.h>
22 #include <string.h>
23
24 #include <rtl8367c_asicdrv.h>
25 #include <rtl8367c_asicdrv_meter.h>
26 #include <rtl8367c_asicdrv_inbwctrl.h>
27 #include <rtl8367c_asicdrv_scheduling.h>
28
29 /* Function Name:
30 * rtk_rate_shareMeter_set
31 * Description:
32 * Set meter configuration
33 * Input:
34 * index - shared meter index
35 * type - shared meter type
36 * rate - rate of share meter
37 * ifg_include - include IFG or not, ENABLE:include DISABLE:exclude
38 * Output:
39 * None
40 * Return:
41 * RT_ERR_OK - OK
42 * RT_ERR_FAILED - Failed
43 * RT_ERR_SMI - SMI access error
44 * RT_ERR_FILTER_METER_ID - Invalid meter
45 * RT_ERR_RATE - Invalid rate
46 * RT_ERR_INPUT - Invalid input parameters
47 * Note:
48 * The API can set shared meter rate and ifg include for each meter.
49 * The rate unit is 1 kbps and the range is from 8k to 1048568k if type is METER_TYPE_KBPS and
50 * the granularity of rate is 8 kbps.
51 * The rate unit is packets per second and the range is 1 ~ 0x1FFF if type is METER_TYPE_PPS.
52 * The ifg_include parameter is used
53 * for rate calculation with/without inter-frame-gap and preamble.
54 */
55 rtk_api_ret_t rtk_rate_shareMeter_set(rtk_meter_id_t index, rtk_meter_type_t type, rtk_rate_t rate, rtk_enable_t ifg_include)
56 {
57 rtk_api_ret_t retVal;
58
59 /* Check initialization state */
60 RTK_CHK_INIT_STATE();
61
62 if (index > RTK_MAX_METER_ID)
63 return RT_ERR_FILTER_METER_ID;
64
65 if (type >= METER_TYPE_END)
66 return RT_ERR_INPUT;
67
68 if (ifg_include >= RTK_ENABLE_END)
69 return RT_ERR_INPUT;
70
71 switch (type)
72 {
73 case METER_TYPE_KBPS:
74 if (rate > RTL8367C_QOS_RATE_INPUT_MAX_HSG || rate < RTL8367C_QOS_RATE_INPUT_MIN)
75 return RT_ERR_RATE ;
76
77 if ((retVal = rtl8367c_setAsicShareMeter(index, rate >> 3, ifg_include)) != RT_ERR_OK)
78 return retVal;
79
80 break;
81 case METER_TYPE_PPS:
82 if (rate > RTL8367C_QOS_PPS_INPUT_MAX || rate < RTL8367C_QOS_PPS_INPUT_MIN)
83 return RT_ERR_RATE ;
84
85 if ((retVal = rtl8367c_setAsicShareMeter(index, rate, ifg_include)) != RT_ERR_OK)
86 return retVal;
87
88 break;
89 default:
90 return RT_ERR_INPUT;
91 }
92
93 /* Set Type */
94 if ((retVal = rtl8367c_setAsicShareMeterType(index, (rtk_uint32)type)) != RT_ERR_OK)
95 return retVal;
96
97 return RT_ERR_OK;
98 }
99
100 /* Function Name:
101 * rtk_rate_shareMeter_get
102 * Description:
103 * Get meter configuration
104 * Input:
105 * index - shared meter index
106 * Output:
107 * pType - Meter Type
108 * pRate - pointer of rate of share meter
109 * pIfg_include - include IFG or not, ENABLE:include DISABLE:exclude
110 * Return:
111 * RT_ERR_OK - OK
112 * RT_ERR_FAILED - Failed
113 * RT_ERR_SMI - SMI access error
114 * RT_ERR_FILTER_METER_ID - Invalid meter
115 * Note:
116 *
117 */
118 rtk_api_ret_t rtk_rate_shareMeter_get(rtk_meter_id_t index, rtk_meter_type_t *pType, rtk_rate_t *pRate, rtk_enable_t *pIfg_include)
119 {
120 rtk_api_ret_t retVal;
121 rtk_uint32 regData;
122
123 /* Check initialization state */
124 RTK_CHK_INIT_STATE();
125
126 if (index > RTK_MAX_METER_ID)
127 return RT_ERR_FILTER_METER_ID;
128
129 if(NULL == pType)
130 return RT_ERR_NULL_POINTER;
131
132 if(NULL == pRate)
133 return RT_ERR_NULL_POINTER;
134
135 if(NULL == pIfg_include)
136 return RT_ERR_NULL_POINTER;
137
138 if ((retVal = rtl8367c_getAsicShareMeter(index, &regData, pIfg_include)) != RT_ERR_OK)
139 return retVal;
140
141 if ((retVal = rtl8367c_getAsicShareMeterType(index, (rtk_uint32 *)pType)) != RT_ERR_OK)
142 return retVal;
143
144 if(*pType == METER_TYPE_KBPS)
145 *pRate = regData<<3;
146 else
147 *pRate = regData;
148
149 return RT_ERR_OK;
150 }
151
152 /* Function Name:
153 * rtk_rate_shareMeterBucket_set
154 * Description:
155 * Set meter Bucket Size
156 * Input:
157 * index - shared meter index
158 * bucket_size - Bucket Size
159 * Output:
160 * None.
161 * Return:
162 * RT_ERR_OK - OK
163 * RT_ERR_FAILED - Failed
164 * RT_ERR_INPUT - Error Input
165 * RT_ERR_SMI - SMI access error
166 * RT_ERR_FILTER_METER_ID - Invalid meter
167 * Note:
168 * The API can set shared meter bucket size.
169 */
170 rtk_api_ret_t rtk_rate_shareMeterBucket_set(rtk_meter_id_t index, rtk_uint32 bucket_size)
171 {
172 rtk_api_ret_t retVal;
173
174 /* Check initialization state */
175 RTK_CHK_INIT_STATE();
176
177 if (index > RTK_MAX_METER_ID)
178 return RT_ERR_FILTER_METER_ID;
179
180 if(bucket_size > RTL8367C_METERBUCKETSIZEMAX)
181 return RT_ERR_INPUT;
182
183 if ((retVal = rtl8367c_setAsicShareMeterBucketSize(index, bucket_size)) != RT_ERR_OK)
184 return retVal;
185
186 return RT_ERR_OK;
187 }
188
189 /* Function Name:
190 * rtk_rate_shareMeterBucket_get
191 * Description:
192 * Get meter Bucket Size
193 * Input:
194 * index - shared meter index
195 * Output:
196 * pBucket_size - Bucket Size
197 * Return:
198 * RT_ERR_OK - OK
199 * RT_ERR_FAILED - Failed
200 * RT_ERR_SMI - SMI access error
201 * RT_ERR_FILTER_METER_ID - Invalid meter
202 * Note:
203 * The API can get shared meter bucket size.
204 */
205 rtk_api_ret_t rtk_rate_shareMeterBucket_get(rtk_meter_id_t index, rtk_uint32 *pBucket_size)
206 {
207 rtk_api_ret_t retVal;
208
209 /* Check initialization state */
210 RTK_CHK_INIT_STATE();
211
212 if (index > RTK_MAX_METER_ID)
213 return RT_ERR_FILTER_METER_ID;
214
215 if(NULL == pBucket_size)
216 return RT_ERR_NULL_POINTER;
217
218 if ((retVal = rtl8367c_getAsicShareMeterBucketSize(index, pBucket_size)) != RT_ERR_OK)
219 return retVal;
220
221 return RT_ERR_OK;
222 }
223
224 /* Function Name:
225 * rtk_rate_igrBandwidthCtrlRate_set
226 * Description:
227 * Set port ingress bandwidth control
228 * Input:
229 * port - Port id
230 * rate - Rate of share meter
231 * ifg_include - include IFG or not, ENABLE:include DISABLE:exclude
232 * fc_enable - enable flow control or not, ENABLE:use flow control DISABLE:drop
233 * Output:
234 * None
235 * Return:
236 * RT_ERR_OK - OK
237 * RT_ERR_FAILED - Failed
238 * RT_ERR_SMI - SMI access error
239 * RT_ERR_PORT_ID - Invalid port number.
240 * RT_ERR_ENABLE - Invalid IFG parameter.
241 * RT_ERR_INBW_RATE - Invalid ingress rate parameter.
242 * Note:
243 * The rate unit is 1 kbps and the range is from 8k to 1048568k. The granularity of rate is 8 kbps.
244 * The ifg_include parameter is used for rate calculation with/without inter-frame-gap and preamble.
245 */
246 rtk_api_ret_t rtk_rate_igrBandwidthCtrlRate_set(rtk_port_t port, rtk_rate_t rate, rtk_enable_t ifg_include, rtk_enable_t fc_enable)
247 {
248 rtk_api_ret_t retVal;
249
250 /* Check initialization state */
251 RTK_CHK_INIT_STATE();
252
253 /* Check Port Valid */
254 RTK_CHK_PORT_VALID(port);
255
256 if(ifg_include >= RTK_ENABLE_END)
257 return RT_ERR_INPUT;
258
259 if(fc_enable >= RTK_ENABLE_END)
260 return RT_ERR_INPUT;
261
262 if(rtk_switch_isHsgPort(port) == RT_ERR_OK)
263 {
264 if ((rate > RTL8367C_QOS_RATE_INPUT_MAX_HSG) || (rate < RTL8367C_QOS_RATE_INPUT_MIN))
265 return RT_ERR_QOS_EBW_RATE ;
266 }
267 else
268 {
269 if ((rate > RTL8367C_QOS_RATE_INPUT_MAX) || (rate < RTL8367C_QOS_RATE_INPUT_MIN))
270 return RT_ERR_QOS_EBW_RATE ;
271 }
272
273 if ((retVal = rtl8367c_setAsicPortIngressBandwidth(rtk_switch_port_L2P_get(port), rate>>3, ifg_include,fc_enable)) != RT_ERR_OK)
274 return retVal;
275
276 return RT_ERR_OK;
277 }
278
279 /* Function Name:
280 * rtk_rate_igrBandwidthCtrlRate_get
281 * Description:
282 * Get port ingress bandwidth control
283 * Input:
284 * port - Port id
285 * Output:
286 * pRate - Rate of share meter
287 * pIfg_include - Rate's calculation including IFG, ENABLE:include DISABLE:exclude
288 * pFc_enable - enable flow control or not, ENABLE:use flow control DISABLE:drop
289 * Return:
290 * RT_ERR_OK - OK
291 * RT_ERR_FAILED - Failed
292 * RT_ERR_SMI - SMI access error
293 * RT_ERR_PORT_ID - Invalid port number.
294 * RT_ERR_INPUT - Invalid input parameters.
295 * Note:
296 * The rate unit is 1 kbps and the range is from 8k to 1048568k. The granularity of rate is 8 kbps.
297 * The ifg_include parameter is used for rate calculation with/without inter-frame-gap and preamble.
298 */
299 rtk_api_ret_t rtk_rate_igrBandwidthCtrlRate_get(rtk_port_t port, rtk_rate_t *pRate, rtk_enable_t *pIfg_include, rtk_enable_t *pFc_enable)
300 {
301 rtk_api_ret_t retVal;
302 rtk_uint32 regData;
303
304 /* Check initialization state */
305 RTK_CHK_INIT_STATE();
306
307 /* Check Port Valid */
308 RTK_CHK_PORT_VALID(port);
309
310 if(NULL == pIfg_include)
311 return RT_ERR_NULL_POINTER;
312
313 if(NULL == pFc_enable)
314 return RT_ERR_NULL_POINTER;
315
316 if ((retVal = rtl8367c_getAsicPortIngressBandwidth(rtk_switch_port_L2P_get(port), &regData, pIfg_include, pFc_enable)) != RT_ERR_OK)
317 return retVal;
318
319 *pRate = regData<<3;
320
321 return RT_ERR_OK;
322 }
323
324 /* Function Name:
325 * rtk_rate_egrBandwidthCtrlRate_set
326 * Description:
327 * Set port egress bandwidth control
328 * Input:
329 * port - Port id
330 * rate - Rate of egress bandwidth
331 * ifg_include - include IFG or not, ENABLE:include DISABLE:exclude
332 * Output:
333 * None
334 * Return:
335 * RT_ERR_OK - OK
336 * RT_ERR_FAILED - Failed
337 * RT_ERR_SMI - SMI access error
338 * RT_ERR_PORT_ID - Invalid port number.
339 * RT_ERR_INPUT - Invalid input parameters.
340 * RT_ERR_QOS_EBW_RATE - Invalid egress bandwidth/rate
341 * Note:
342 * The rate unit is 1 kbps and the range is from 8k to 1048568k. The granularity of rate is 8 kbps.
343 * The ifg_include parameter is used for rate calculation with/without inter-frame-gap and preamble.
344 */
345 rtk_api_ret_t rtk_rate_egrBandwidthCtrlRate_set( rtk_port_t port, rtk_rate_t rate, rtk_enable_t ifg_include)
346 {
347 rtk_api_ret_t retVal;
348
349 /* Check initialization state */
350 RTK_CHK_INIT_STATE();
351
352 /* Check Port Valid */
353 RTK_CHK_PORT_VALID(port);
354
355 if(rtk_switch_isHsgPort(port) == RT_ERR_OK)
356 {
357 if ((rate > RTL8367C_QOS_RATE_INPUT_MAX_HSG) || (rate < RTL8367C_QOS_RATE_INPUT_MIN))
358 return RT_ERR_QOS_EBW_RATE ;
359 }
360 else
361 {
362 if ((rate > RTL8367C_QOS_RATE_INPUT_MAX) || (rate < RTL8367C_QOS_RATE_INPUT_MIN))
363 return RT_ERR_QOS_EBW_RATE ;
364 }
365
366 if (ifg_include >= RTK_ENABLE_END)
367 return RT_ERR_ENABLE;
368
369 if ((retVal = rtl8367c_setAsicPortEgressRate(rtk_switch_port_L2P_get(port), rate>>3)) != RT_ERR_OK)
370 return retVal;
371
372 if ((retVal = rtl8367c_setAsicPortEgressRateIfg(ifg_include)) != RT_ERR_OK)
373 return retVal;
374
375 return RT_ERR_OK;
376 }
377
378 /* Function Name:
379 * rtk_rate_egrBandwidthCtrlRate_get
380 * Description:
381 * Get port egress bandwidth control
382 * Input:
383 * port - Port id
384 * Output:
385 * pRate - Rate of egress bandwidth
386 * pIfg_include - Rate's calculation including IFG, ENABLE:include DISABLE:exclude
387 * Return:
388 * RT_ERR_OK - OK
389 * RT_ERR_FAILED - Failed
390 * RT_ERR_SMI - SMI access error
391 * RT_ERR_PORT_ID - Invalid port number.
392 * RT_ERR_INPUT - Invalid input parameters.
393 * Note:
394 * The rate unit is 1 kbps and the range is from 8k to 1048568k. The granularity of rate is 8 kbps.
395 * The ifg_include parameter is used for rate calculation with/without inter-frame-gap and preamble.
396 */
397 rtk_api_ret_t rtk_rate_egrBandwidthCtrlRate_get(rtk_port_t port, rtk_rate_t *pRate, rtk_enable_t *pIfg_include)
398 {
399 rtk_api_ret_t retVal;
400 rtk_uint32 regData;
401
402 /* Check initialization state */
403 RTK_CHK_INIT_STATE();
404
405 /* Check Port Valid */
406 RTK_CHK_PORT_VALID(port);
407
408 if(NULL == pRate)
409 return RT_ERR_NULL_POINTER;
410
411 if(NULL == pIfg_include)
412 return RT_ERR_NULL_POINTER;
413
414 if ((retVal = rtl8367c_getAsicPortEgressRate(rtk_switch_port_L2P_get(port), &regData)) != RT_ERR_OK)
415 return retVal;
416
417 *pRate = regData << 3;
418
419 if ((retVal = rtl8367c_getAsicPortEgressRateIfg((rtk_uint32*)pIfg_include)) != RT_ERR_OK)
420 return retVal;
421
422 return RT_ERR_OK;
423 }
424
425
426 /* Function Name:
427 * rtk_rate_egrQueueBwCtrlEnable_get
428 * Description:
429 * Get enable status of egress bandwidth control on specified queue.
430 * Input:
431 * unit - unit id
432 * port - port id
433 * queue - queue id
434 * Output:
435 * pEnable - Pointer to enable status of egress queue bandwidth control
436 * Return:
437 * RT_ERR_OK
438 * RT_ERR_FAILED
439 * RT_ERR_PORT_ID - invalid port id
440 * RT_ERR_QUEUE_ID - invalid queue id
441 * RT_ERR_NULL_POINTER - input parameter may be null pointer
442 * Note:
443 * None
444 */
445 rtk_api_ret_t rtk_rate_egrQueueBwCtrlEnable_get(rtk_port_t port, rtk_qid_t queue, rtk_enable_t *pEnable)
446 {
447 rtk_api_ret_t retVal;
448
449 /* Check initialization state */
450 RTK_CHK_INIT_STATE();
451
452 /* Check Port Valid */
453 RTK_CHK_PORT_VALID(port);
454
455 /*for whole port function, the queue value should be 0xFF*/
456 if (queue != RTK_WHOLE_SYSTEM)
457 return RT_ERR_QUEUE_ID;
458
459 if(NULL == pEnable)
460 return RT_ERR_NULL_POINTER;
461
462 if ((retVal = rtl8367c_getAsicAprEnable(rtk_switch_port_L2P_get(port),pEnable))!=RT_ERR_OK)
463 return retVal;
464
465 return RT_ERR_OK;
466 }
467
468 /* Function Name:
469 * rtk_rate_egrQueueBwCtrlEnable_set
470 * Description:
471 * Set enable status of egress bandwidth control on specified queue.
472 * Input:
473 * port - port id
474 * queue - queue id
475 * enable - enable status of egress queue bandwidth control
476 * Output:
477 * None
478 * Return:
479 * RT_ERR_OK
480 * RT_ERR_FAILED
481 * RT_ERR_PORT_ID - invalid port id
482 * RT_ERR_QUEUE_ID - invalid queue id
483 * RT_ERR_INPUT - invalid input parameter
484 * Note:
485 * None
486 */
487 rtk_api_ret_t rtk_rate_egrQueueBwCtrlEnable_set(rtk_port_t port, rtk_qid_t queue, rtk_enable_t enable)
488 {
489 rtk_api_ret_t retVal;
490
491 /* Check initialization state */
492 RTK_CHK_INIT_STATE();
493
494 /* Check Port Valid */
495 RTK_CHK_PORT_VALID(port);
496
497 /*for whole port function, the queue value should be 0xFF*/
498 if (queue != RTK_WHOLE_SYSTEM)
499 return RT_ERR_QUEUE_ID;
500
501 if (enable>=RTK_ENABLE_END)
502 return RT_ERR_INPUT;
503
504 if ((retVal = rtl8367c_setAsicAprEnable(rtk_switch_port_L2P_get(port), enable))!=RT_ERR_OK)
505 return retVal;
506
507 return RT_ERR_OK;
508 }
509
510 /* Function Name:
511 * rtk_rate_egrQueueBwCtrlRate_get
512 * Description:
513 * Get rate of egress bandwidth control on specified queue.
514 * Input:
515 * port - port id
516 * queue - queue id
517 * pIndex - shared meter index
518 * Output:
519 * pRate - pointer to rate of egress queue bandwidth control
520 * Return:
521 * RT_ERR_OK
522 * RT_ERR_FAILED
523 * RT_ERR_PORT_ID - invalid port id
524 * RT_ERR_QUEUE_ID - invalid queue id
525 * RT_ERR_FILTER_METER_ID - Invalid meter id
526 * Note:
527 * The actual rate control is set in shared meters.
528 * The unit of granularity is 8Kbps.
529 */
530 rtk_api_ret_t rtk_rate_egrQueueBwCtrlRate_get(rtk_port_t port, rtk_qid_t queue, rtk_meter_id_t *pIndex)
531 {
532 rtk_api_ret_t retVal;
533 rtk_uint32 offset_idx;
534 rtk_uint32 phy_port;
535
536 /* Check initialization state */
537 RTK_CHK_INIT_STATE();
538
539 /* Check Port Valid */
540 RTK_CHK_PORT_VALID(port);
541
542 if (queue >= RTK_MAX_NUM_OF_QUEUE)
543 return RT_ERR_QUEUE_ID;
544
545 if(NULL == pIndex)
546 return RT_ERR_NULL_POINTER;
547
548 phy_port = rtk_switch_port_L2P_get(port);
549 if ((retVal=rtl8367c_getAsicAprMeter(phy_port, queue,&offset_idx))!=RT_ERR_OK)
550 return retVal;
551
552 *pIndex = offset_idx + ((phy_port%4)*8);
553
554 return RT_ERR_OK;
555 }
556
557
558 /* Function Name:
559 * rtk_rate_egrQueueBwCtrlRate_set
560 * Description:
561 * Set rate of egress bandwidth control on specified queue.
562 * Input:
563 * port - port id
564 * queue - queue id
565 * index - shared meter index
566 * Output:
567 * None
568 * Return:
569 * RT_ERR_OK
570 * RT_ERR_FAILED
571 * RT_ERR_PORT_ID - invalid port id
572 * RT_ERR_QUEUE_ID - invalid queue id
573 * RT_ERR_FILTER_METER_ID - Invalid meter id
574 * Note:
575 * The actual rate control is set in shared meters.
576 * The unit of granularity is 8Kbps.
577 */
578 rtk_api_ret_t rtk_rate_egrQueueBwCtrlRate_set(rtk_port_t port, rtk_qid_t queue, rtk_meter_id_t index)
579 {
580 rtk_api_ret_t retVal;
581 rtk_uint32 offset_idx;
582 rtk_uint32 phy_port;
583
584 /* Check initialization state */
585 RTK_CHK_INIT_STATE();
586
587 /* Check Port Valid */
588 RTK_CHK_PORT_VALID(port);
589
590 if (queue >= RTK_MAX_NUM_OF_QUEUE)
591 return RT_ERR_QUEUE_ID;
592
593 if (index > RTK_MAX_METER_ID)
594 return RT_ERR_FILTER_METER_ID;
595
596 phy_port = rtk_switch_port_L2P_get(port);
597 if (index < ((phy_port%4)*8) || index > (7 + (phy_port%4)*8))
598 return RT_ERR_FILTER_METER_ID;
599
600 offset_idx = index - ((phy_port%4)*8);
601
602 if ((retVal=rtl8367c_setAsicAprMeter(phy_port,queue,offset_idx))!=RT_ERR_OK)
603 return retVal;
604
605 return RT_ERR_OK;
606 }
607