mediatek: add patches for 5.15 and kernel config for mt7622
[openwrt/openwrt.git] / target / linux / mediatek / files / drivers / net / phy / rtk / rtl8367c / rtl8367c_asicdrv_meter.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 : RTL8367C switch high-level API for RTL8367C
14 * Feature : Shared meter related functions
15 *
16 */
17 #include <rtl8367c_asicdrv_meter.h>
18 /* Function Name:
19 * rtl8367c_setAsicShareMeter
20 * Description:
21 * Set meter configuration
22 * Input:
23 * index - hared meter index (0-31)
24 * rate - 17-bits rate of share meter, unit is 8Kpbs
25 * ifg - Including IFG in rate calculation, 1:include 0:exclude
26 * Output:
27 * None
28 * Return:
29 * RT_ERR_OK - Success
30 * RT_ERR_SMI - SMI access error
31 * RT_ERR_FILTER_METER_ID - Invalid meter
32 * Note:
33 * None
34 */
35 ret_t rtl8367c_setAsicShareMeter(rtk_uint32 index, rtk_uint32 rate, rtk_uint32 ifg)
36 {
37 ret_t retVal;
38
39 if(index > RTL8367C_METERMAX)
40 return RT_ERR_FILTER_METER_ID;
41
42 if(index < 32)
43 {
44 /*19-bits Rate*/
45 retVal = rtl8367c_setAsicReg(RTL8367C_METER_RATE_REG(index), rate&0xFFFF);
46 if(retVal != RT_ERR_OK)
47 return retVal;
48
49 retVal = rtl8367c_setAsicReg(RTL8367C_METER_RATE_REG(index) + 1, (rate &0x70000) >> 16);
50 if(retVal != RT_ERR_OK)
51 return retVal;
52
53 retVal = rtl8367c_setAsicRegBit(RTL8367C_METER_IFG_CTRL_REG(index), RTL8367C_METER_IFG_OFFSET(index), ifg);
54 if(retVal != RT_ERR_OK)
55 return retVal;
56 }
57 else
58 {
59 /*19-bits Rate*/
60 retVal = rtl8367c_setAsicReg(RTL8367C_REG_METER32_RATE_CTRL0 + ((index-32) << 1), rate&0xFFFF);
61 if(retVal != RT_ERR_OK)
62 return retVal;
63
64 retVal = rtl8367c_setAsicReg(RTL8367C_REG_METER32_RATE_CTRL0 + ((index-32) << 1) + 1, (rate &0x70000) >> 16);
65 if(retVal != RT_ERR_OK)
66 return retVal;
67
68 retVal = rtl8367c_setAsicRegBit(RTL8367C_REG_METER_IFG_CTRL2 + ((index-32) >> 4), RTL8367C_METER_IFG_OFFSET(index), ifg);
69 if(retVal != RT_ERR_OK)
70 return retVal;
71 }
72
73 return RT_ERR_OK;
74 }
75 /* Function Name:
76 * rtl8367c_getAsicShareMeter
77 * Description:
78 * Get meter configuration
79 * Input:
80 * index - hared meter index (0-31)
81 * pRate - 17-bits rate of share meter, unit is 8Kpbs
82 * pIfg - Including IFG in rate calculation, 1:include 0:exclude
83 * Output:
84 * None
85 * Return:
86 * RT_ERR_OK - Success
87 * RT_ERR_SMI - SMI access error
88 * RT_ERR_FILTER_METER_ID - Invalid meter
89 * Note:
90 * None
91 */
92 ret_t rtl8367c_getAsicShareMeter(rtk_uint32 index, rtk_uint32 *pRate, rtk_uint32 *pIfg)
93 {
94 rtk_uint32 regData;
95 rtk_uint32 regData2;
96 ret_t retVal;
97
98 if(index > RTL8367C_METERMAX)
99 return RT_ERR_FILTER_METER_ID;
100
101 if(index < 32)
102 {
103 /*17-bits Rate*/
104 retVal = rtl8367c_getAsicReg(RTL8367C_METER_RATE_REG(index), &regData);
105 if(retVal != RT_ERR_OK)
106 return retVal;
107
108 retVal = rtl8367c_getAsicReg(RTL8367C_METER_RATE_REG(index) + 1, &regData2);
109 if(retVal != RT_ERR_OK)
110 return retVal;
111
112 *pRate = ((regData2 << 16) & 0x70000) | regData;
113 /*IFG*/
114 retVal = rtl8367c_getAsicRegBit(RTL8367C_METER_IFG_CTRL_REG(index), RTL8367C_METER_IFG_OFFSET(index), pIfg);
115
116 return retVal;
117 }
118 else
119 {
120 /*17-bits Rate*/
121 retVal = rtl8367c_getAsicReg(RTL8367C_REG_METER32_RATE_CTRL0 + ((index-32) << 1), &regData);
122 if(retVal != RT_ERR_OK)
123 return retVal;
124
125 retVal = rtl8367c_getAsicReg(RTL8367C_REG_METER32_RATE_CTRL0 + ((index-32) << 1) + 1, &regData2);
126 if(retVal != RT_ERR_OK)
127 return retVal;
128
129 *pRate = ((regData2 << 16) & 0x70000) | regData;
130 /*IFG*/
131 retVal = rtl8367c_getAsicRegBit(RTL8367C_REG_METER_IFG_CTRL2 + ((index-32) >> 4), RTL8367C_METER_IFG_OFFSET(index), pIfg);
132
133 return retVal;
134 }
135 }
136 /* Function Name:
137 * rtl8367c_setAsicShareMeterBucketSize
138 * Description:
139 * Set meter related leaky bucket threshold
140 * Input:
141 * index - hared meter index (0-31)
142 * lbthreshold - Leaky bucket threshold of meter
143 * Output:
144 * None
145 * Return:
146 * RT_ERR_OK - Success
147 * RT_ERR_SMI - SMI access error
148 * RT_ERR_FILTER_METER_ID - Invalid meter
149 * Note:
150 * None
151 */
152 ret_t rtl8367c_setAsicShareMeterBucketSize(rtk_uint32 index, rtk_uint32 lbthreshold)
153 {
154
155 if(index > RTL8367C_METERMAX)
156 return RT_ERR_FILTER_METER_ID;
157
158 if(index < 32)
159 return rtl8367c_setAsicReg(RTL8367C_METER_BUCKET_SIZE_REG(index), lbthreshold);
160 else
161 return rtl8367c_setAsicReg(RTL8367C_REG_METER32_BUCKET_SIZE + index - 32, lbthreshold);
162 }
163 /* Function Name:
164 * rtl8367c_getAsicShareMeterBucketSize
165 * Description:
166 * Get meter related leaky bucket threshold
167 * Input:
168 * index - hared meter index (0-31)
169 * pLbthreshold - Leaky bucket threshold of meter
170 * Output:
171 * None
172 * Return:
173 * RT_ERR_OK - Success
174 * RT_ERR_SMI - SMI access error
175 * RT_ERR_FILTER_METER_ID - Invalid meter
176 * Note:
177 * None
178 */
179 ret_t rtl8367c_getAsicShareMeterBucketSize(rtk_uint32 index, rtk_uint32 *pLbthreshold)
180 {
181 if(index > RTL8367C_METERMAX)
182 return RT_ERR_FILTER_METER_ID;
183
184 if(index < 32)
185 return rtl8367c_getAsicReg(RTL8367C_METER_BUCKET_SIZE_REG(index), pLbthreshold);
186 else
187 return rtl8367c_getAsicReg(RTL8367C_REG_METER32_BUCKET_SIZE + index - 32, pLbthreshold);
188 }
189
190 /* Function Name:
191 * rtl8367c_setAsicShareMeterType
192 * Description:
193 * Set meter Type
194 * Input:
195 * index - shared meter index (0-31)
196 * Type - 0: kbps, 1: pps
197 * Output:
198 * None
199 * Return:
200 * RT_ERR_OK - Success
201 * RT_ERR_SMI - SMI access error
202 * RT_ERR_FILTER_METER_ID - Invalid meter
203 * Note:
204 * None
205 */
206 ret_t rtl8367c_setAsicShareMeterType(rtk_uint32 index, rtk_uint32 type)
207 {
208 rtk_uint32 reg;
209
210 if(index > RTL8367C_METERMAX)
211 return RT_ERR_FILTER_METER_ID;
212
213 if(index < 32)
214 reg = RTL8367C_REG_METER_MODE_SETTING0 + (index / 16);
215 else
216 reg = RTL8367C_REG_METER_MODE_SETTING2 + ((index - 32) / 16);
217 return rtl8367c_setAsicRegBit(reg, index % 16, type);
218 }
219
220 /* Function Name:
221 * rtl8367c_getAsicShareMeterType
222 * Description:
223 * Get meter Type
224 * Input:
225 * index - shared meter index (0-31)
226 * Output:
227 * pType - 0: kbps, 1: pps
228 * Return:
229 * RT_ERR_OK - Success
230 * RT_ERR_SMI - SMI access error
231 * RT_ERR_FILTER_METER_ID - Invalid meter
232 * Note:
233 * None
234 */
235 ret_t rtl8367c_getAsicShareMeterType(rtk_uint32 index, rtk_uint32 *pType)
236 {
237 rtk_uint32 reg;
238
239 if(index > RTL8367C_METERMAX)
240 return RT_ERR_FILTER_METER_ID;
241
242 if(NULL == pType)
243 return RT_ERR_NULL_POINTER;
244
245 if(index < 32)
246 reg = RTL8367C_REG_METER_MODE_SETTING0 + (index / 16);
247 else
248 reg = RTL8367C_REG_METER_MODE_SETTING2 + ((index - 32) / 16);
249 return rtl8367c_getAsicRegBit(reg, index % 16, pType);
250 }
251
252
253 /* Function Name:
254 * rtl8367c_setAsicMeterExceedStatus
255 * Description:
256 * Clear shared meter status
257 * Input:
258 * index - hared meter index (0-31)
259 * Output:
260 * None
261 * Return:
262 * RT_ERR_OK - Success
263 * RT_ERR_SMI - SMI access error
264 * RT_ERR_FILTER_METER_ID - Invalid meter
265 * Note:
266 * None
267 */
268 ret_t rtl8367c_setAsicMeterExceedStatus(rtk_uint32 index)
269 {
270 if(index > RTL8367C_METERMAX)
271 return RT_ERR_FILTER_METER_ID;
272
273 if(index < 32)
274 return rtl8367c_setAsicRegBit(RTL8367C_METER_OVERRATE_INDICATOR_REG(index), RTL8367C_METER_EXCEED_OFFSET(index), 1);
275 else
276 return rtl8367c_setAsicRegBit(RTL8367C_REG_METER_OVERRATE_INDICATOR2 + ((index - 32) >> 4), RTL8367C_METER_EXCEED_OFFSET(index), 1);
277
278 }
279 /* Function Name:
280 * rtl8367c_getAsicMeterExceedStatus
281 * Description:
282 * Get shared meter status
283 * Input:
284 * index - hared meter index (0-31)
285 * pStatus - 0: rate doesn't exceed 1: rate exceeds
286 * Output:
287 * None
288 * Return:
289 * RT_ERR_OK - Success
290 * RT_ERR_SMI - SMI access error
291 * RT_ERR_FILTER_METER_ID - Invalid meter
292 * Note:
293 * If rate is over rate*8Kbps of a meter, the state bit of this meter is set to 1.
294 */
295 ret_t rtl8367c_getAsicMeterExceedStatus(rtk_uint32 index, rtk_uint32* pStatus)
296 {
297 if(index > RTL8367C_METERMAX)
298 return RT_ERR_FILTER_METER_ID;
299
300 if(index < 32)
301 return rtl8367c_getAsicRegBit(RTL8367C_METER_OVERRATE_INDICATOR_REG(index), RTL8367C_METER_EXCEED_OFFSET(index), pStatus);
302 else
303 return rtl8367c_getAsicRegBit(RTL8367C_REG_METER_OVERRATE_INDICATOR2 + ((index - 32) >> 4), RTL8367C_METER_EXCEED_OFFSET(index), pStatus);
304 }
305