1921d1a5af6ba0f2aff739da06f669880f93b77f
[openwrt/staging/stintel.git] / target / linux / mediatek / files / drivers / net / phy / rtk / rtl8367c / mirror.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 Mirror module.
15 *
16 */
17
18 #include <rtk_switch.h>
19 #include <rtk_error.h>
20 #include <mirror.h>
21 #include <string.h>
22 #include <rtl8367c_asicdrv.h>
23 #include <rtl8367c_asicdrv_mirror.h>
24
25 /* Function Name:
26 * rtk_mirror_portBased_set
27 * Description:
28 * Set port mirror function.
29 * Input:
30 * mirroring_port - Monitor port.
31 * pMirrored_rx_portmask - Rx mirror port mask.
32 * pMirrored_tx_portmask - Tx mirror port mask.
33 * Output:
34 * None
35 * Return:
36 * RT_ERR_OK - OK
37 * RT_ERR_FAILED - Failed
38 * RT_ERR_SMI - SMI access error
39 * RT_ERR_PORT_ID - Invalid port number
40 * RT_ERR_PORT_MASK - Invalid portmask.
41 * Note:
42 * The API is to set mirror function of source port and mirror port.
43 * The mirror port can only be set to one port and the TX and RX mirror ports
44 * should be identical.
45 */
46 rtk_api_ret_t rtk_mirror_portBased_set(rtk_port_t mirroring_port, rtk_portmask_t *pMirrored_rx_portmask, rtk_portmask_t *pMirrored_tx_portmask)
47 {
48 rtk_api_ret_t retVal;
49 rtk_enable_t mirRx, mirTx;
50 rtk_uint32 i, pmask;
51 rtk_port_t source_port;
52
53 /* Check initialization state */
54 RTK_CHK_INIT_STATE();
55
56 /* Check port valid */
57 RTK_CHK_PORT_VALID(mirroring_port);
58
59 if(NULL == pMirrored_rx_portmask)
60 return RT_ERR_NULL_POINTER;
61
62 if(NULL == pMirrored_tx_portmask)
63 return RT_ERR_NULL_POINTER;
64
65 RTK_CHK_PORTMASK_VALID(pMirrored_rx_portmask);
66
67 RTK_CHK_PORTMASK_VALID(pMirrored_tx_portmask);
68
69 /*Mirror Sorce Port Mask Check*/
70 if (pMirrored_tx_portmask->bits[0]!=pMirrored_rx_portmask->bits[0]&&pMirrored_tx_portmask->bits[0]!=0&&pMirrored_rx_portmask->bits[0]!=0)
71 return RT_ERR_PORT_MASK;
72
73 /*mirror port != source port*/
74 if(RTK_PORTMASK_IS_PORT_SET((*pMirrored_tx_portmask), mirroring_port) || RTK_PORTMASK_IS_PORT_SET((*pMirrored_rx_portmask), mirroring_port))
75 return RT_ERR_PORT_MASK;
76
77 source_port = rtk_switch_maxLogicalPort_get();
78
79 RTK_SCAN_ALL_LOG_PORT(i)
80 {
81 if (pMirrored_tx_portmask->bits[0]&(1<<i))
82 {
83 source_port = i;
84 break;
85 }
86
87 if (pMirrored_rx_portmask->bits[0]&(1<<i))
88 {
89 source_port = i;
90 break;
91 }
92 }
93
94 if ((retVal = rtl8367c_setAsicPortMirror(rtk_switch_port_L2P_get(source_port), rtk_switch_port_L2P_get(mirroring_port))) != RT_ERR_OK)
95 return retVal;
96 if(pMirrored_rx_portmask->bits[0] != 0)
97 {
98 if ((retVal = rtk_switch_portmask_L2P_get(pMirrored_rx_portmask, &pmask)) != RT_ERR_OK)
99 return retVal;
100 if ((retVal = rtl8367c_setAsicPortMirrorMask(pmask)) != RT_ERR_OK)
101 return retVal;
102 }
103 else
104 {
105 if ((retVal = rtk_switch_portmask_L2P_get(pMirrored_tx_portmask, &pmask)) != RT_ERR_OK)
106 return retVal;
107 if ((retVal = rtl8367c_setAsicPortMirrorMask(pmask)) != RT_ERR_OK)
108 return retVal;
109 }
110
111
112 if (pMirrored_rx_portmask->bits[0])
113 mirRx = ENABLED;
114 else
115 mirRx = DISABLED;
116
117 if ((retVal = rtl8367c_setAsicPortMirrorRxFunction(mirRx)) != RT_ERR_OK)
118 return retVal;
119
120 if (pMirrored_tx_portmask->bits[0])
121 mirTx = ENABLED;
122 else
123 mirTx = DISABLED;
124
125 if ((retVal = rtl8367c_setAsicPortMirrorTxFunction(mirTx)) != RT_ERR_OK)
126 return retVal;
127
128 return RT_ERR_OK;
129
130 }
131
132 /* Function Name:
133 * rtk_mirror_portBased_get
134 * Description:
135 * Get port mirror function.
136 * Input:
137 * None
138 * Output:
139 * pMirroring_port - Monitor port.
140 * pMirrored_rx_portmask - Rx mirror port mask.
141 * pMirrored_tx_portmask - Tx mirror port mask.
142 * Return:
143 * RT_ERR_OK - OK
144 * RT_ERR_FAILED - Failed
145 * RT_ERR_SMI - SMI access error
146 * RT_ERR_INPUT - Invalid input parameters.
147 * Note:
148 * The API is to get mirror function of source port and mirror port.
149 */
150 rtk_api_ret_t rtk_mirror_portBased_get(rtk_port_t *pMirroring_port, rtk_portmask_t *pMirrored_rx_portmask, rtk_portmask_t *pMirrored_tx_portmask)
151 {
152 rtk_api_ret_t retVal;
153 rtk_port_t source_port;
154 rtk_enable_t mirRx, mirTx;
155 rtk_uint32 sport, mport, pmask;
156
157 /* Check initialization state */
158 RTK_CHK_INIT_STATE();
159
160 if(NULL == pMirrored_rx_portmask)
161 return RT_ERR_NULL_POINTER;
162
163 if(NULL == pMirrored_tx_portmask)
164 return RT_ERR_NULL_POINTER;
165
166 if(NULL == pMirroring_port)
167 return RT_ERR_NULL_POINTER;
168
169 if ((retVal = rtl8367c_getAsicPortMirror(&sport, &mport)) != RT_ERR_OK)
170 return retVal;
171 source_port = rtk_switch_port_P2L_get(sport);
172 *pMirroring_port = rtk_switch_port_P2L_get(mport);
173
174 if ((retVal = rtl8367c_getAsicPortMirrorRxFunction((rtk_uint32*)&mirRx)) != RT_ERR_OK)
175 return retVal;
176
177 if ((retVal = rtl8367c_getAsicPortMirrorTxFunction((rtk_uint32*)&mirTx)) != RT_ERR_OK)
178 return retVal;
179
180 if ((retVal = rtl8367c_getAsicPortMirrorMask(&pmask)) != RT_ERR_OK)
181 return retVal;
182
183 if (DISABLED == mirRx)
184 pMirrored_rx_portmask->bits[0]=0;
185 else
186 {
187 if ((retVal = rtk_switch_portmask_P2L_get(pmask, pMirrored_rx_portmask)) != RT_ERR_OK)
188 return retVal;
189 pMirrored_rx_portmask->bits[0] |= 1<<source_port;
190 }
191
192 if (DISABLED == mirTx)
193 pMirrored_tx_portmask->bits[0]=0;
194 else
195 {
196 if ((retVal = rtk_switch_portmask_P2L_get(pmask, pMirrored_tx_portmask)) != RT_ERR_OK)
197 return retVal;
198 pMirrored_tx_portmask->bits[0] |= 1<<source_port;
199 }
200
201 return RT_ERR_OK;
202
203 }
204
205 /* Function Name:
206 * rtk_mirror_portIso_set
207 * Description:
208 * Set mirror port isolation.
209 * Input:
210 * enable |Mirror isolation status.
211 * Output:
212 * None
213 * Return:
214 * RT_ERR_OK - OK
215 * RT_ERR_FAILED - Failed
216 * RT_ERR_SMI - SMI access error
217 * RT_ERR_ENABLE - Invalid enable input
218 * Note:
219 * The API is to set mirror isolation function that prevent normal forwarding packets to miror port.
220 */
221 rtk_api_ret_t rtk_mirror_portIso_set(rtk_enable_t enable)
222 {
223 rtk_api_ret_t retVal;
224
225 /* Check initialization state */
226 RTK_CHK_INIT_STATE();
227
228 if (enable >= RTK_ENABLE_END)
229 return RT_ERR_ENABLE;
230
231 if ((retVal = rtl8367c_setAsicPortMirrorIsolation(enable)) != RT_ERR_OK)
232 return retVal;
233
234 return RT_ERR_OK;
235 }
236
237 /* Function Name:
238 * rtk_mirror_portIso_get
239 * Description:
240 * Get mirror port isolation.
241 * Input:
242 * None
243 * Output:
244 * pEnable |Mirror isolation status.
245 * Return:
246 * RT_ERR_OK - OK
247 * RT_ERR_FAILED - Failed
248 * RT_ERR_SMI - SMI access error
249 * RT_ERR_INPUT - Invalid input parameters.
250 * Note:
251 * The API is to get mirror isolation status.
252 */
253 rtk_api_ret_t rtk_mirror_portIso_get(rtk_enable_t *pEnable)
254 {
255 rtk_api_ret_t retVal;
256
257 /* Check initialization state */
258 RTK_CHK_INIT_STATE();
259
260 if(NULL == pEnable)
261 return RT_ERR_NULL_POINTER;
262
263 if ((retVal = rtl8367c_getAsicPortMirrorIsolation(pEnable)) != RT_ERR_OK)
264 return retVal;
265
266 return RT_ERR_OK;
267 }
268
269 /* Function Name:
270 * rtk_mirror_vlanLeaky_set
271 * Description:
272 * Set mirror VLAN leaky.
273 * Input:
274 * txenable -TX leaky enable.
275 * rxenable - RX leaky enable.
276 * Output:
277 * None
278 * Return:
279 * RT_ERR_OK - OK
280 * RT_ERR_FAILED - Failed
281 * RT_ERR_SMI - SMI access error
282 * RT_ERR_ENABLE - Invalid enable input
283 * Note:
284 * The API is to set mirror VLAN leaky function forwarding packets to miror port.
285 */
286 rtk_api_ret_t rtk_mirror_vlanLeaky_set(rtk_enable_t txenable, rtk_enable_t rxenable)
287 {
288 rtk_api_ret_t retVal;
289
290 /* Check initialization state */
291 RTK_CHK_INIT_STATE();
292
293 if ((txenable >= RTK_ENABLE_END) ||(rxenable >= RTK_ENABLE_END))
294 return RT_ERR_ENABLE;
295
296 if ((retVal = rtl8367c_setAsicPortMirrorVlanTxLeaky(txenable)) != RT_ERR_OK)
297 return retVal;
298
299 if ((retVal = rtl8367c_setAsicPortMirrorVlanRxLeaky(rxenable)) != RT_ERR_OK)
300 return retVal;
301
302 return RT_ERR_OK;
303 }
304
305 /* Function Name:
306 * rtk_mirror_vlanLeaky_get
307 * Description:
308 * Get mirror VLAN leaky.
309 * Input:
310 * None
311 * Output:
312 * pTxenable - TX leaky enable.
313 * pRxenable - RX leaky enable.
314 * Return:
315 * RT_ERR_OK - OK
316 * RT_ERR_FAILED - Failed
317 * RT_ERR_SMI - SMI access error
318 * RT_ERR_INPUT - Invalid input parameters.
319 * Note:
320 * The API is to get mirror VLAN leaky status.
321 */
322 rtk_api_ret_t rtk_mirror_vlanLeaky_get(rtk_enable_t *pTxenable, rtk_enable_t *pRxenable)
323 {
324 rtk_api_ret_t retVal;
325
326 /* Check initialization state */
327 RTK_CHK_INIT_STATE();
328
329 if( (NULL == pTxenable) || (NULL == pRxenable) )
330 return RT_ERR_NULL_POINTER;
331
332 if ((retVal = rtl8367c_getAsicPortMirrorVlanTxLeaky(pTxenable)) != RT_ERR_OK)
333 return retVal;
334
335 if ((retVal = rtl8367c_getAsicPortMirrorVlanRxLeaky(pRxenable)) != RT_ERR_OK)
336 return retVal;
337
338 return RT_ERR_OK;
339 }
340
341 /* Function Name:
342 * rtk_mirror_isolationLeaky_set
343 * Description:
344 * Set mirror Isolation leaky.
345 * Input:
346 * txenable -TX leaky enable.
347 * rxenable - RX leaky enable.
348 * Output:
349 * None
350 * Return:
351 * RT_ERR_OK - OK
352 * RT_ERR_FAILED - Failed
353 * RT_ERR_SMI - SMI access error
354 * RT_ERR_ENABLE - Invalid enable input
355 * Note:
356 * The API is to set mirror VLAN leaky function forwarding packets to miror port.
357 */
358 rtk_api_ret_t rtk_mirror_isolationLeaky_set(rtk_enable_t txenable, rtk_enable_t rxenable)
359 {
360 rtk_api_ret_t retVal;
361
362 /* Check initialization state */
363 RTK_CHK_INIT_STATE();
364
365 if ((txenable >= RTK_ENABLE_END) ||(rxenable >= RTK_ENABLE_END))
366 return RT_ERR_ENABLE;
367
368 if ((retVal = rtl8367c_setAsicPortMirrorIsolationTxLeaky(txenable)) != RT_ERR_OK)
369 return retVal;
370
371 if ((retVal = rtl8367c_setAsicPortMirrorIsolationRxLeaky(rxenable)) != RT_ERR_OK)
372 return retVal;
373
374 return RT_ERR_OK;
375 }
376
377 /* Function Name:
378 * rtk_mirror_isolationLeaky_get
379 * Description:
380 * Get mirror isolation leaky.
381 * Input:
382 * None
383 * Output:
384 * pTxenable - TX leaky enable.
385 * pRxenable - RX leaky enable.
386 * Return:
387 * RT_ERR_OK - OK
388 * RT_ERR_FAILED - Failed
389 * RT_ERR_SMI - SMI access error
390 * RT_ERR_INPUT - Invalid input parameters.
391 * Note:
392 * The API is to get mirror isolation leaky status.
393 */
394 rtk_api_ret_t rtk_mirror_isolationLeaky_get(rtk_enable_t *pTxenable, rtk_enable_t *pRxenable)
395 {
396 rtk_api_ret_t retVal;
397
398 /* Check initialization state */
399 RTK_CHK_INIT_STATE();
400
401 if( (NULL == pTxenable) || (NULL == pRxenable) )
402 return RT_ERR_NULL_POINTER;
403
404 if ((retVal = rtl8367c_getAsicPortMirrorIsolationTxLeaky(pTxenable)) != RT_ERR_OK)
405 return retVal;
406
407 if ((retVal = rtl8367c_getAsicPortMirrorIsolationRxLeaky(pRxenable)) != RT_ERR_OK)
408 return retVal;
409
410 return RT_ERR_OK;
411 }
412
413 /* Function Name:
414 * rtk_mirror_keep_set
415 * Description:
416 * Set mirror packet format keep.
417 * Input:
418 * mode - -mirror keep mode.
419 * Output:
420 * None
421 * Return:
422 * RT_ERR_OK - OK
423 * RT_ERR_FAILED - Failed
424 * RT_ERR_SMI - SMI access error
425 * RT_ERR_ENABLE - Invalid enable input
426 * Note:
427 * The API is to set -mirror keep mode.
428 * The mirror keep mode is as following:
429 * - MIRROR_FOLLOW_VLAN
430 * - MIRROR_KEEP_ORIGINAL
431 * - MIRROR_KEEP_END
432 */
433 rtk_api_ret_t rtk_mirror_keep_set(rtk_mirror_keep_t mode)
434 {
435 rtk_api_ret_t retVal;
436
437 /* Check initialization state */
438 RTK_CHK_INIT_STATE();
439
440 if (mode >= MIRROR_KEEP_END)
441 return RT_ERR_ENABLE;
442
443 if ((retVal = rtl8367c_setAsicPortMirrorRealKeep(mode)) != RT_ERR_OK)
444 return retVal;
445
446 return RT_ERR_OK;
447 }
448
449 /* Function Name:
450 * rtk_mirror_keep_get
451 * Description:
452 * Get mirror packet format keep.
453 * Input:
454 * None
455 * Output:
456 * pMode -mirror keep mode.
457 * Return:
458 * RT_ERR_OK - OK
459 * RT_ERR_FAILED - Failed
460 * RT_ERR_SMI - SMI access error
461 * RT_ERR_INPUT - Invalid input parameters.
462 * Note:
463 * The API is to get mirror keep mode.
464 * The mirror keep mode is as following:
465 * - MIRROR_FOLLOW_VLAN
466 * - MIRROR_KEEP_ORIGINAL
467 * - MIRROR_KEEP_END
468 */
469 rtk_api_ret_t rtk_mirror_keep_get(rtk_mirror_keep_t *pMode)
470 {
471 rtk_api_ret_t retVal;
472
473 /* Check initialization state */
474 RTK_CHK_INIT_STATE();
475
476 if(NULL == pMode)
477 return RT_ERR_NULL_POINTER;
478
479 if ((retVal = rtl8367c_getAsicPortMirrorRealKeep(pMode)) != RT_ERR_OK)
480 return retVal;
481
482 return RT_ERR_OK;
483 }
484
485 /* Function Name:
486 * rtk_mirror_override_set
487 * Description:
488 * Set port mirror override function.
489 * Input:
490 * rxMirror - 1: output mirrored packet, 0: output normal forward packet
491 * txMirror - 1: output mirrored packet, 0: output normal forward packet
492 * aclMirror - 1: output mirrored packet, 0: output normal forward packet
493 * Output:
494 * None
495 * Return:
496 * RT_ERR_OK - OK
497 * RT_ERR_FAILED - Failed
498 * RT_ERR_SMI - SMI access error
499 * Note:
500 * The API is to set mirror override function.
501 * This function control the output format when a port output
502 * normal forward & mirrored packet at the same time.
503 */
504 rtk_api_ret_t rtk_mirror_override_set(rtk_enable_t rxMirror, rtk_enable_t txMirror, rtk_enable_t aclMirror)
505 {
506 rtk_api_ret_t retVal;
507
508 if( (rxMirror >= RTK_ENABLE_END) || (txMirror >= RTK_ENABLE_END) || (aclMirror >= RTK_ENABLE_END))
509 return RT_ERR_ENABLE;
510
511 if ((retVal = rtl8367c_setAsicPortMirrorOverride((rtk_uint32)rxMirror, (rtk_uint32)txMirror, (rtk_uint32)aclMirror)) != RT_ERR_OK)
512 return retVal;
513
514 return RT_ERR_OK;
515 }
516
517 /* Function Name:
518 * rtk_mirror_override_get
519 * Description:
520 * Get port mirror override function.
521 * Input:
522 * None
523 * Output:
524 * pRxMirror - 1: output mirrored packet, 0: output normal forward packet
525 * pTxMirror - 1: output mirrored packet, 0: output normal forward packet
526 * pAclMirror - 1: output mirrored packet, 0: output normal forward packet
527 * Return:
528 * RT_ERR_OK - OK
529 * RT_ERR_FAILED - Failed
530 * RT_ERR_SMI - SMI access error
531 * RT_ERR_NULL_POINTER - Null Pointer
532 * Note:
533 * The API is to Get mirror override function.
534 * This function control the output format when a port output
535 * normal forward & mirrored packet at the same time.
536 */
537 rtk_api_ret_t rtk_mirror_override_get(rtk_enable_t *pRxMirror, rtk_enable_t *pTxMirror, rtk_enable_t *pAclMirror)
538 {
539 rtk_api_ret_t retVal;
540
541 if( (pRxMirror == NULL) || (pTxMirror == NULL) || (pAclMirror == NULL))
542 return RT_ERR_ENABLE;
543
544 if ((retVal = rtl8367c_getAsicPortMirrorOverride((rtk_uint32 *)pRxMirror, (rtk_uint32 *)pTxMirror, (rtk_uint32 *)pAclMirror)) != RT_ERR_OK)
545 return retVal;
546
547 return RT_ERR_OK;
548 }