summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/mellanox/mlxsw/spectrum_trap.c
blob: 26d01adbedad3c0fbed2234c6e915a1329b60a59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/* Copyright (c) 2019 Mellanox Technologies. All rights reserved */

#include <linux/bitops.h>
#include <linux/kernel.h>
#include <linux/netlink.h>
#include <net/devlink.h>
#include <uapi/linux/devlink.h>

#include "core.h"
#include "reg.h"
#include "spectrum.h"
#include "spectrum_trap.h"

struct mlxsw_sp_trap_policer_item {
	struct devlink_trap_policer policer;
	u16 hw_id;
};

struct mlxsw_sp_trap_group_item {
	struct devlink_trap_group group;
	u16 hw_group_id;
	u8 priority;
	u8 fixed_policer:1; /* Whether policer binding can change */
};

#define MLXSW_SP_TRAP_LISTENERS_MAX 3

struct mlxsw_sp_trap_item {
	struct devlink_trap trap;
	struct mlxsw_listener listeners_arr[MLXSW_SP_TRAP_LISTENERS_MAX];
	u8 is_source:1;
};

/* All driver-specific traps must be documented in
 * Documentation/networking/devlink/mlxsw.rst
 */
enum {
	DEVLINK_MLXSW_TRAP_ID_BASE = DEVLINK_TRAP_GENERIC_ID_MAX,
	DEVLINK_MLXSW_TRAP_ID_IRIF_DISABLED,
	DEVLINK_MLXSW_TRAP_ID_ERIF_DISABLED,
};

#define DEVLINK_MLXSW_TRAP_NAME_IRIF_DISABLED \
	"irif_disabled"
#define DEVLINK_MLXSW_TRAP_NAME_ERIF_DISABLED \
	"erif_disabled"

#define MLXSW_SP_TRAP_METADATA DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT

enum {
	/* Packet was mirrored from ingress. */
	MLXSW_SP_MIRROR_REASON_INGRESS = 1,
	/* Packet was mirrored from policy engine. */
	MLXSW_SP_MIRROR_REASON_POLICY_ENGINE = 2,
	/* Packet was early dropped. */
	MLXSW_SP_MIRROR_REASON_INGRESS_WRED = 9,
	/* Packet was mirrored from egress. */
	MLXSW_SP_MIRROR_REASON_EGRESS = 14,
};

static int mlxsw_sp_rx_listener(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
				u8 local_port,
				struct mlxsw_sp_port *mlxsw_sp_port)
{
	struct mlxsw_sp_port_pcpu_stats *pcpu_stats;

	if (unlikely(!mlxsw_sp_port)) {
		dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
				     local_port);
		kfree_skb(skb);
		return -EINVAL;
	}

	skb->dev = mlxsw_sp_port->dev;

	pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
	u64_stats_update_begin(&pcpu_stats->syncp);
	pcpu_stats->rx_packets++;
	pcpu_stats->rx_bytes += skb->len;
	u64_stats_update_end(&pcpu_stats->syncp);

	skb->protocol = eth_type_trans(skb, skb->dev);

	return 0;
}

static void mlxsw_sp_rx_drop_listener(struct sk_buff *skb, u8 local_port,
				      void *trap_ctx)
{
	struct devlink_port *in_devlink_port;
	struct mlxsw_sp_port *mlxsw_sp_port;
	struct mlxsw_sp *mlxsw_sp;
	struct devlink *devlink;
	int err;

	mlxsw_sp = devlink_trap_ctx_priv(trap_ctx);
	mlxsw_sp_port = mlxsw_sp->ports[local_port];

	err = mlxsw_sp_rx_listener(mlxsw_sp, skb, local_port, mlxsw_sp_port);
	if (err)
		return;

	devlink = priv_to_devlink(mlxsw_sp->core);
	in_devlink_port = mlxsw_core_port_devlink_port_get(mlxsw_sp->core,
							   local_port);
	skb_push(skb, ETH_HLEN);
	devlink_trap_report(devlink, skb, trap_ctx, in_devlink_port, NULL);
	consume_skb(skb);
}

static void mlxsw_sp_rx_acl_drop_listener(struct sk_buff *skb, u8 local_port,
					  void *trap_ctx)
{
	u32 cookie_index = mlxsw_skb_cb(skb)->rx_md_info.cookie_index;
	const struct flow_action_cookie *fa_cookie;
	struct devlink_port *in_devlink_port;
	struct mlxsw_sp_port *mlxsw_sp_port;
	struct mlxsw_sp *mlxsw_sp;
	struct devlink *devlink;
	int err;

	mlxsw_sp = devlink_trap_ctx_priv(trap_ctx);
	mlxsw_sp_port = mlxsw_sp->ports[local_port];

	err = mlxsw_sp_rx_listener(mlxsw_sp, skb, local_port, mlxsw_sp_port);
	if (err)
		return;

	devlink = priv_to_devlink(mlxsw_sp->core);
	in_devlink_port = mlxsw_core_port_devlink_port_get(mlxsw_sp->core,
							   local_port);
	skb_push(skb, ETH_HLEN);
	rcu_read_lock();
	fa_cookie = mlxsw_sp_acl_act_cookie_lookup(mlxsw_sp, cookie_index);
	devlink_trap_report(devlink, skb, trap_ctx, in_devlink_port, fa_cookie);
	rcu_read_unlock();
	consume_skb(skb);
}

static int __mlxsw_sp_rx_no_mark_listener(struct sk_buff *skb, u8 local_port,
					  void *trap_ctx)
{
	struct devlink_port *in_devlink_port;
	struct mlxsw_sp_port *mlxsw_sp_port;
	struct mlxsw_sp *mlxsw_sp;
	struct devlink *devlink;
	int err;

	mlxsw_sp = devlink_trap_ctx_priv(trap_ctx);
	mlxsw_sp_port = mlxsw_sp->ports[local_port];

	err = mlxsw_sp_rx_listener(mlxsw_sp, skb, local_port, mlxsw_sp_port);
	if (err)
		return err;

	devlink = priv_to_devlink(mlxsw_sp->core);
	in_devlink_port = mlxsw_core_port_devlink_port_get(mlxsw_sp->core,
							   local_port);
	skb_push(skb, ETH_HLEN);
	devlink_trap_report(devlink, skb, trap_ctx, in_devlink_port, NULL);
	skb_pull(skb, ETH_HLEN);

	return 0;
}

static void mlxsw_sp_rx_no_mark_listener(struct sk_buff *skb, u8 local_port,
					 void *trap_ctx)
{
	int err;

	err = __mlxsw_sp_rx_no_mark_listener(skb, local_port, trap_ctx);
	if (err)
		return;

	netif_receive_skb(skb);
}

static void mlxsw_sp_rx_mark_listener(struct sk_buff *skb, u8 local_port,
				      void *trap_ctx)
{
	skb->offload_fwd_mark = 1;
	mlxsw_sp_rx_no_mark_listener(skb, local_port, trap_ctx);
}

static void mlxsw_sp_rx_l3_mark_listener(struct sk_buff *skb, u8 local_port,
					 void *trap_ctx)
{
	skb->offload_l3_fwd_mark = 1;
	skb->offload_fwd_mark = 1;
	mlxsw_sp_rx_no_mark_listener(skb, local_port, trap_ctx);
}

static void mlxsw_sp_rx_ptp_listener(struct sk_buff *skb, u8 local_port,
				     void *trap_ctx)
{
	struct mlxsw_sp *mlxsw_sp = devlink_trap_ctx_priv(trap_ctx);
	int err;

	err = __mlxsw_sp_rx_no_mark_listener(skb, local_port, trap_ctx);
	if (err)
		return;

	/* The PTP handler expects skb->data to point to the start of the
	 * Ethernet header.
	 */
	skb_push(skb, ETH_HLEN);
	mlxsw_sp_ptp_receive(mlxsw_sp, skb, local_port);
}

static struct mlxsw_sp_port *
mlxsw_sp_sample_tx_port_get(struct mlxsw_sp *mlxsw_sp,
			    const struct mlxsw_rx_md_info *rx_md_info)
{
	u8 local_port;

	if (!rx_md_info->tx_port_valid)
		return NULL;

	if (rx_md_info->tx_port_is_lag)
		local_port = mlxsw_core_lag_mapping_get(mlxsw_sp->core,
							rx_md_info->tx_lag_id,
							rx_md_info->tx_lag_port_index);
	else
		local_port = rx_md_info->tx_sys_port;

	if (local_port >= mlxsw_core_max_ports(mlxsw_sp->core))
		return NULL;

	return mlxsw_sp->ports[local_port];
}

/* The latency units are determined according to MOGCR.mirror_latency_units. It
 * defaults to 64 nanoseconds.
 */
#define MLXSW_SP_MIRROR_LATENCY_SHIFT	6

static void mlxsw_sp_psample_md_init(struct mlxsw_sp *mlxsw_sp,
				     struct psample_metadata *md,
				     struct sk_buff *skb, int in_ifindex,
				     bool truncate, u32 trunc_size)
{
	struct mlxsw_rx_md_info *rx_md_info = &mlxsw_skb_cb(skb)->rx_md_info;
	struct mlxsw_sp_port *mlxsw_sp_port;

	md->trunc_size = truncate ? trunc_size : skb->len;
	md->in_ifindex = in_ifindex;
	mlxsw_sp_port = mlxsw_sp_sample_tx_port_get(mlxsw_sp, rx_md_info);
	md->out_ifindex = mlxsw_sp_port && mlxsw_sp_port->dev ?
			  mlxsw_sp_port->dev->ifindex : 0;
	md->out_tc_valid = rx_md_info->tx_tc_valid;
	md->out_tc = rx_md_info->tx_tc;
	md->out_tc_occ_valid = rx_md_info->tx_congestion_valid;
	md->out_tc_occ = rx_md_info->tx_congestion;
	md->latency_valid = rx_md_info->latency_valid;
	md->latency = rx_md_info->latency;
	md->latency <<= MLXSW_SP_MIRROR_LATENCY_SHIFT;
}

static void mlxsw_sp_rx_sample_listener(struct sk_buff *skb, u8 local_port,
					void *trap_ctx)
{
	struct mlxsw_sp *mlxsw_sp = devlink_trap_ctx_priv(trap_ctx);
	struct mlxsw_sp_sample_trigger trigger;
	struct mlxsw_sp_sample_params *params;
	struct mlxsw_sp_port *mlxsw_sp_port;
	struct psample_metadata md = {};
	int err;

	err = __mlxsw_sp_rx_no_mark_listener(skb, local_port, trap_ctx);
	if (err)
		return;

	mlxsw_sp_port = mlxsw_sp->ports[local_port];
	if (!mlxsw_sp_port)
		goto out;

	trigger.type = MLXSW_SP_SAMPLE_TRIGGER_TYPE_INGRESS;
	trigger.local_port = local_port;
	params = mlxsw_sp_sample_trigger_params_lookup(mlxsw_sp, &trigger);
	if (!params)
		goto out;

	/* The psample module expects skb->data to point to the start of the
	 * Ethernet header.
	 */
	skb_push(skb, ETH_HLEN);
	mlxsw_sp_psample_md_init(mlxsw_sp, &md, skb,
				 mlxsw_sp_port->dev->ifindex, params->truncate,
				 params->trunc_size);
	psample_sample_packet(params->psample_group, skb, params->rate, &md);
out:
	consume_skb(skb);
}

static void mlxsw_sp_rx_sample_tx_listener(struct sk_buff *skb, u8 local_port,
					   void *trap_ctx)
{
	struct mlxsw_rx_md_info *rx_md_info = &mlxsw_skb_cb(skb)->rx_md_info;
	struct mlxsw_sp *mlxsw_sp = devlink_trap_ctx_priv(trap_ctx);
	struct mlxsw_sp_port *mlxsw_sp_port, *mlxsw_sp_port_tx;
	struct mlxsw_sp_sample_trigger trigger;
	struct mlxsw_sp_sample_params *params;
	struct psample_metadata md = {};
	int err;

	/* Locally generated packets are not reported from the policy engine
	 * trigger, so do not report them from the egress trigger as well.
	 */
	if (local_port == MLXSW_PORT_CPU_PORT)
		goto out;

	err = __mlxsw_sp_rx_no_mark_listener(skb, local_port, trap_ctx);
	if (err)
		return;

	mlxsw_sp_port = mlxsw_sp->ports[local_port];
	if (!mlxsw_sp_port)
		goto out;

	/* Packet was sampled from Tx, so we need to retrieve the sample
	 * parameters based on the Tx port and not the Rx port.
	 */
	mlxsw_sp_port_tx = mlxsw_sp_sample_tx_port_get(mlxsw_sp, rx_md_info);
	if (!mlxsw_sp_port_tx)
		goto out;

	trigger.type = MLXSW_SP_SAMPLE_TRIGGER_TYPE_EGRESS;
	trigger.local_port = mlxsw_sp_port_tx->local_port;
	params = mlxsw_sp_sample_trigger_params_lookup(mlxsw_sp, &trigger);
	if (!params)
		goto out;

	/* The psample module expects skb->data to point to the start of the
	 * Ethernet header.
	 */
	skb_push(skb, ETH_HLEN);
	mlxsw_sp_psample_md_init(mlxsw_sp, &md, skb,
				 mlxsw_sp_port->dev->ifindex, params->truncate,
				 params->trunc_size);
	psample_sample_packet(params->psample_group, skb, params->rate, &md);
out:
	consume_skb(skb);
}

static void mlxsw_sp_rx_sample_acl_listener(struct sk_buff *skb, u8 local_port,
					    void *trap_ctx)
{
	struct mlxsw_sp *mlxsw_sp = devlink_trap_ctx_priv(trap_ctx);
	struct mlxsw_sp_sample_trigger trigger = {
		.type = MLXSW_SP_SAMPLE_TRIGGER_TYPE_POLICY_ENGINE,
	};
	struct mlxsw_sp_sample_params *params;
	struct mlxsw_sp_port *mlxsw_sp_port;
	struct psample_metadata md = {};
	int err;

	err = __mlxsw_sp_rx_no_mark_listener(skb, local_port, trap_ctx);
	if (err)
		return;

	mlxsw_sp_port = mlxsw_sp->ports[local_port];
	if (!mlxsw_sp_port)
		goto out;

	params = mlxsw_sp_sample_trigger_params_lookup(mlxsw_sp, &trigger);
	if (!params)
		goto out;

	/* The psample module expects skb->data to point to the start of the
	 * Ethernet header.
	 */
	skb_push(skb, ETH_HLEN);
	mlxsw_sp_psample_md_init(mlxsw_sp, &md, skb,
				 mlxsw_sp_port->dev->ifindex, params->truncate,
				 params->trunc_size);
	psample_sample_packet(params->psample_group, skb, params->rate, &md);
out:
	consume_skb(skb);
}

#define MLXSW_SP_TRAP_DROP(_id, _group_id)				      \
	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
			     MLXSW_SP_TRAP_METADATA)

#define MLXSW_SP_TRAP_DROP_EXT(_id, _group_id, _metadata)		      \
	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
			     MLXSW_SP_TRAP_METADATA | (_metadata))

#define MLXSW_SP_TRAP_BUFFER_DROP(_id)					      \
	DEVLINK_TRAP_GENERIC(DROP, TRAP, _id,				      \
			     DEVLINK_TRAP_GROUP_GENERIC_ID_BUFFER_DROPS,      \
			     MLXSW_SP_TRAP_METADATA)

#define MLXSW_SP_TRAP_DRIVER_DROP(_id, _group_id)			      \
	DEVLINK_TRAP_DRIVER(DROP, DROP, DEVLINK_MLXSW_TRAP_ID_##_id,	      \
			    DEVLINK_MLXSW_TRAP_NAME_##_id,		      \
			    DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
			    MLXSW_SP_TRAP_METADATA)

#define MLXSW_SP_TRAP_EXCEPTION(_id, _group_id)		      \
	DEVLINK_TRAP_GENERIC(EXCEPTION, TRAP, _id,			      \
			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
			     MLXSW_SP_TRAP_METADATA)

#define MLXSW_SP_TRAP_CONTROL(_id, _group_id, _action)			      \
	DEVLINK_TRAP_GENERIC(CONTROL, _action, _id,			      \
			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
			     MLXSW_SP_TRAP_METADATA)

#define MLXSW_SP_RXL_DISCARD(_id, _group_id)				      \
	MLXSW_RXL_DIS(mlxsw_sp_rx_drop_listener, DISCARD_##_id,		      \
		      TRAP_EXCEPTION_TO_CPU, false, SP_##_group_id,	      \
		      SET_FW_DEFAULT, SP_##_group_id)

#define MLXSW_SP_RXL_ACL_DISCARD(_id, _en_group_id, _dis_group_id)	      \
	MLXSW_RXL_DIS(mlxsw_sp_rx_acl_drop_listener, DISCARD_##_id,	      \
		      TRAP_EXCEPTION_TO_CPU, false, SP_##_en_group_id,	      \
		      SET_FW_DEFAULT, SP_##_dis_group_id)

#define MLXSW_SP_RXL_BUFFER_DISCARD(_mirror_reason)			      \
	MLXSW_RXL_MIRROR(mlxsw_sp_rx_drop_listener, 0, SP_BUFFER_DISCARDS,    \
			 MLXSW_SP_MIRROR_REASON_##_mirror_reason)

#define MLXSW_SP_RXL_EXCEPTION(_id, _group_id, _action)			      \
	MLXSW_RXL(mlxsw_sp_rx_mark_listener, _id,			      \
		   _action, false, SP_##_group_id, SET_FW_DEFAULT)

#define MLXSW_SP_RXL_NO_MARK(_id, _group_id, _action, _is_ctrl)		      \
	MLXSW_RXL(mlxsw_sp_rx_no_mark_listener, _id, _action,		      \
		  _is_ctrl, SP_##_group_id, DISCARD)

#define MLXSW_SP_RXL_MARK(_id, _group_id, _action, _is_ctrl)		      \
	MLXSW_RXL(mlxsw_sp_rx_mark_listener, _id, _action, _is_ctrl,	      \
		  SP_##_group_id, DISCARD)

#define MLXSW_SP_RXL_L3_MARK(_id, _group_id, _action, _is_ctrl)		      \
	MLXSW_RXL(mlxsw_sp_rx_l3_mark_listener, _id, _action, _is_ctrl,	      \
		  SP_##_group_id, DISCARD)

#define MLXSW_SP_TRAP_POLICER(_id, _rate, _burst)			      \
	DEVLINK_TRAP_POLICER(_id, _rate, _burst,			      \
			     MLXSW_REG_QPCR_HIGHEST_CIR,		      \
			     MLXSW_REG_QPCR_LOWEST_CIR,			      \
			     1 << MLXSW_REG_QPCR_HIGHEST_CBS,		      \
			     1 << MLXSW_REG_QPCR_LOWEST_CBS)

/* Ordered by policer identifier */
static const struct mlxsw_sp_trap_policer_item
mlxsw_sp_trap_policer_items_arr[] = {
	{
		.policer = MLXSW_SP_TRAP_POLICER(1, 10 * 1024, 4096),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(2, 128, 128),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(3, 128, 128),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(4, 128, 128),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(5, 16 * 1024, 8192),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(6, 128, 128),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(7, 1024, 512),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(8, 20 * 1024, 8192),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(9, 128, 128),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(10, 1024, 512),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(11, 256, 128),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(12, 128, 128),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(13, 128, 128),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(14, 1024, 512),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(15, 1024, 512),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(16, 24 * 1024, 16384),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(17, 19 * 1024, 8192),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(18, 1024, 512),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(19, 1024, 512),
	},
	{
		.policer = MLXSW_SP_TRAP_POLICER(20, 10240, 4096),
	},
};

static const struct mlxsw_sp_trap_group_item mlxsw_sp_trap_group_items_arr[] = {
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(L2_DROPS, 1),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_L2_DISCARDS,
		.priority = 0,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(L3_DROPS, 1),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_L3_DISCARDS,
		.priority = 0,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(L3_EXCEPTIONS, 1),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_L3_EXCEPTIONS,
		.priority = 2,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(TUNNEL_DROPS, 1),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_TUNNEL_DISCARDS,
		.priority = 0,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(ACL_DROPS, 1),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_ACL_DISCARDS,
		.priority = 0,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(STP, 2),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_STP,
		.priority = 5,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(LACP, 3),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP,
		.priority = 5,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(LLDP, 4),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP,
		.priority = 5,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(MC_SNOOPING, 5),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_MC_SNOOPING,
		.priority = 3,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(DHCP, 6),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP,
		.priority = 2,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(NEIGH_DISCOVERY, 7),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_NEIGH_DISCOVERY,
		.priority = 2,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(BFD, 8),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_BFD,
		.priority = 5,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(OSPF, 9),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF,
		.priority = 5,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(BGP, 10),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP,
		.priority = 4,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(VRRP, 11),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_VRRP,
		.priority = 5,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(PIM, 12),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM,
		.priority = 5,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(UC_LB, 13),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_LBERROR,
		.priority = 0,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(LOCAL_DELIVERY, 14),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME,
		.priority = 2,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(EXTERNAL_DELIVERY, 19),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_EXTERNAL_ROUTE,
		.priority = 1,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(IPV6, 15),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6,
		.priority = 2,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(PTP_EVENT, 16),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP0,
		.priority = 5,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(PTP_GENERAL, 17),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_PTP1,
		.priority = 2,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(ACL_TRAP, 18),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_FLOW_LOGGING,
		.priority = 4,
	},
};

static const struct mlxsw_sp_trap_item mlxsw_sp_trap_items_arr[] = {
	{
		.trap = MLXSW_SP_TRAP_DROP(SMAC_MC, L2_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_PACKET_SMAC_MC, L2_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(VLAN_TAG_MISMATCH, L2_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_SWITCH_VTAG_ALLOW,
					     L2_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(INGRESS_VLAN_FILTER, L2_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_SWITCH_VLAN, L2_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(INGRESS_STP_FILTER, L2_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_SWITCH_STP, L2_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(EMPTY_TX_LIST, L2_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(LOOKUP_SWITCH_UC, L2_DISCARDS),
			MLXSW_SP_RXL_DISCARD(LOOKUP_SWITCH_MC_NULL, L2_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(PORT_LOOPBACK_FILTER, L2_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(LOOKUP_SWITCH_LB, L2_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(BLACKHOLE_ROUTE, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ROUTER2, L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(NON_IP_PACKET, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_ROUTER_NON_IP_PACKET,
					     L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(UC_DIP_MC_DMAC, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_ROUTER_UC_DIP_MC_DMAC,
					     L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(DIP_LB, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_ROUTER_DIP_LB, L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(SIP_MC, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_ROUTER_SIP_MC, L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(SIP_LB, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_ROUTER_SIP_LB, L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(CORRUPTED_IP_HDR, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_ROUTER_CORRUPTED_IP_HDR,
					     L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(IPV4_SIP_BC, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ING_ROUTER_IPV4_SIP_BC,
					     L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(IPV6_MC_DIP_RESERVED_SCOPE,
					   L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(IPV6_MC_DIP_RESERVED_SCOPE,
					     L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE,
					   L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE,
					     L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_EXCEPTION(MTU_ERROR, L3_EXCEPTIONS),
		.listeners_arr = {
			MLXSW_SP_RXL_EXCEPTION(MTUERROR, L3_EXCEPTIONS,
					       TRAP_TO_CPU),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_EXCEPTION(TTL_ERROR, L3_EXCEPTIONS),
		.listeners_arr = {
			MLXSW_SP_RXL_EXCEPTION(TTLERROR, L3_EXCEPTIONS,
					       TRAP_TO_CPU),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_EXCEPTION(RPF, L3_EXCEPTIONS),
		.listeners_arr = {
			MLXSW_SP_RXL_EXCEPTION(RPF, L3_EXCEPTIONS, TRAP_TO_CPU),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_EXCEPTION(REJECT_ROUTE, L3_EXCEPTIONS),
		.listeners_arr = {
			MLXSW_SP_RXL_EXCEPTION(RTR_INGRESS1, L3_EXCEPTIONS,
					       TRAP_TO_CPU),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_EXCEPTION(UNRESOLVED_NEIGH,
						L3_EXCEPTIONS),
		.listeners_arr = {
			MLXSW_SP_RXL_EXCEPTION(HOST_MISS_IPV4, L3_EXCEPTIONS,
					       TRAP_TO_CPU),
			MLXSW_SP_RXL_EXCEPTION(HOST_MISS_IPV6, L3_EXCEPTIONS,
					       TRAP_TO_CPU),
			MLXSW_SP_RXL_EXCEPTION(RTR_EGRESS0, L3_EXCEPTIONS,
					       TRAP_EXCEPTION_TO_CPU),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_EXCEPTION(IPV4_LPM_UNICAST_MISS,
						L3_EXCEPTIONS),
		.listeners_arr = {
			MLXSW_SP_RXL_EXCEPTION(DISCARD_ROUTER_LPM4,
					       L3_EXCEPTIONS,
					       TRAP_EXCEPTION_TO_CPU),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_EXCEPTION(IPV6_LPM_UNICAST_MISS,
						L3_EXCEPTIONS),
		.listeners_arr = {
			MLXSW_SP_RXL_EXCEPTION(DISCARD_ROUTER_LPM6,
					       L3_EXCEPTIONS,
					       TRAP_EXCEPTION_TO_CPU),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DRIVER_DROP(IRIF_DISABLED, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ROUTER_IRIF_EN, L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DRIVER_DROP(ERIF_DISABLED, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ROUTER_ERIF_EN, L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(NON_ROUTABLE, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(NON_ROUTABLE, L3_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_EXCEPTION(DECAP_ERROR, TUNNEL_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_EXCEPTION(DECAP_ECN0, TUNNEL_DISCARDS,
					       TRAP_EXCEPTION_TO_CPU),
			MLXSW_SP_RXL_EXCEPTION(IPIP_DECAP_ERROR,
					       TUNNEL_DISCARDS,
					       TRAP_EXCEPTION_TO_CPU),
			MLXSW_SP_RXL_EXCEPTION(DISCARD_DEC_PKT, TUNNEL_DISCARDS,
					       TRAP_EXCEPTION_TO_CPU),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(OVERLAY_SMAC_MC, TUNNEL_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(OVERLAY_SMAC_MC, TUNNEL_DISCARDS),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP_EXT(INGRESS_FLOW_ACTION_DROP,
					       ACL_DROPS,
					       DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
		.listeners_arr = {
			MLXSW_SP_RXL_ACL_DISCARD(INGRESS_ACL, ACL_DISCARDS,
						 DUMMY),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP_EXT(EGRESS_FLOW_ACTION_DROP,
					       ACL_DROPS,
					       DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
		.listeners_arr = {
			MLXSW_SP_RXL_ACL_DISCARD(EGRESS_ACL, ACL_DISCARDS,
						 DUMMY),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(STP, STP, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(STP, STP, TRAP_TO_CPU, true),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(LACP, LACP, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(LACP, LACP, TRAP_TO_CPU, true),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(LLDP, LLDP, TRAP),
		.listeners_arr = {
			MLXSW_RXL(mlxsw_sp_rx_ptp_listener, LLDP, TRAP_TO_CPU,
				  false, SP_LLDP, DISCARD),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IGMP_QUERY, MC_SNOOPING, MIRROR),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IGMP_QUERY, MC_SNOOPING,
					  MIRROR_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IGMP_V1_REPORT, MC_SNOOPING,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(IGMP_V1_REPORT, MC_SNOOPING,
					     TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IGMP_V2_REPORT, MC_SNOOPING,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(IGMP_V2_REPORT, MC_SNOOPING,
					     TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IGMP_V3_REPORT, MC_SNOOPING,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(IGMP_V3_REPORT, MC_SNOOPING,
					     TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IGMP_V2_LEAVE, MC_SNOOPING,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(IGMP_V2_LEAVE, MC_SNOOPING,
					     TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(MLD_QUERY, MC_SNOOPING, MIRROR),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_MLDV12_LISTENER_QUERY,
					  MC_SNOOPING, MIRROR_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(MLD_V1_REPORT, MC_SNOOPING,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_REPORT,
					     MC_SNOOPING, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(MLD_V2_REPORT, MC_SNOOPING,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(IPV6_MLDV2_LISTENER_REPORT,
					     MC_SNOOPING, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(MLD_V1_DONE, MC_SNOOPING,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_DONE,
					     MC_SNOOPING, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV4_DHCP, DHCP, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV4_DHCP, DHCP, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_DHCP, DHCP, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_DHCP, DHCP, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(ARP_REQUEST, NEIGH_DISCOVERY,
					      MIRROR),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(ARPBC, NEIGH_DISCOVERY, MIRROR_TO_CPU,
					  false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(ARP_RESPONSE, NEIGH_DISCOVERY,
					      MIRROR),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(ARPUC, NEIGH_DISCOVERY, MIRROR_TO_CPU,
					  false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(ARP_OVERLAY, NEIGH_DISCOVERY,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(NVE_DECAP_ARP, NEIGH_DISCOVERY,
					     TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_NEIGH_SOLICIT,
					      NEIGH_DISCOVERY, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_SOLICITATION,
					  NEIGH_DISCOVERY, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_NEIGH_ADVERT,
					      NEIGH_DISCOVERY, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_ADVERTISEMENT,
					  NEIGH_DISCOVERY, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV4_BFD, BFD, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV4_BFD, BFD, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_BFD, BFD, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_BFD, BFD, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV4_OSPF, OSPF, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV4_OSPF, OSPF, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_OSPF, OSPF, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_OSPF, OSPF, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV4_BGP, BGP, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV4_BGP, BGP, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_BGP, BGP, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_BGP, BGP, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV4_VRRP, VRRP, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV4_VRRP, VRRP, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_VRRP, VRRP, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_VRRP, VRRP, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV4_PIM, PIM, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV4_PIM, PIM, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_PIM, PIM, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_PIM, PIM, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(UC_LB, UC_LB, MIRROR),
		.listeners_arr = {
			MLXSW_SP_RXL_L3_MARK(LBERROR, LBERROR, MIRROR_TO_CPU,
					     false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(LOCAL_ROUTE, LOCAL_DELIVERY,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IP2ME, IP2ME, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(EXTERNAL_ROUTE, EXTERNAL_DELIVERY,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(RTR_INGRESS0, EXTERNAL_ROUTE,
					  TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_UC_DIP_LINK_LOCAL_SCOPE,
					      LOCAL_DELIVERY, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_DEST, IP2ME,
					  TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV4_ROUTER_ALERT, LOCAL_DELIVERY,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV4, IP2ME, TRAP_TO_CPU,
					  false),
		},
	},
	{
		/* IPV6_ROUTER_ALERT is defined in uAPI as 22, but it is not
		 * used in this file, so undefine it.
		 */
		#undef IPV6_ROUTER_ALERT
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_ROUTER_ALERT, LOCAL_DELIVERY,
					      TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV6, IP2ME, TRAP_TO_CPU,
					  false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_DIP_ALL_NODES, IPV6, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_ALL_NODES_LINK, IPV6,
					  TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_DIP_ALL_ROUTERS, IPV6, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(IPV6_ALL_ROUTERS_LINK, IPV6,
					  TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_ROUTER_SOLICIT, IPV6, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_SOLICITATION, IPV6,
					  TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_ROUTER_ADVERT, IPV6, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_ADVERTISEMENT, IPV6,
					  TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(IPV6_REDIRECT, IPV6, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_MARK(L3_IPV6_REDIRECTION, IPV6,
					  TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(PTP_EVENT, PTP_EVENT, TRAP),
		.listeners_arr = {
			MLXSW_RXL(mlxsw_sp_rx_ptp_listener, PTP0, TRAP_TO_CPU,
				  false, SP_PTP0, DISCARD),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(PTP_GENERAL, PTP_GENERAL, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(PTP1, PTP1, TRAP_TO_CPU, false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(FLOW_ACTION_TRAP, ACL_TRAP, TRAP),
		.listeners_arr = {
			MLXSW_SP_RXL_NO_MARK(ACL0, FLOW_LOGGING, TRAP_TO_CPU,
					     false),
		},
	},
	{
		.trap = MLXSW_SP_TRAP_DROP(BLACKHOLE_NEXTHOP, L3_DROPS),
		.listeners_arr = {
			MLXSW_SP_RXL_DISCARD(ROUTER3, L3_DISCARDS),
		},
	},
};

static struct mlxsw_sp_trap_policer_item *
mlxsw_sp_trap_policer_item_lookup(struct mlxsw_sp *mlxsw_sp, u32 id)
{
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	int i;

	for (i = 0; i < trap->policers_count; i++) {
		if (trap->policer_items_arr[i].policer.id == id)
			return &trap->policer_items_arr[i];
	}

	return NULL;
}

static struct mlxsw_sp_trap_group_item *
mlxsw_sp_trap_group_item_lookup(struct mlxsw_sp *mlxsw_sp, u16 id)
{
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	int i;

	for (i = 0; i < trap->groups_count; i++) {
		if (trap->group_items_arr[i].group.id == id)
			return &trap->group_items_arr[i];
	}

	return NULL;
}

static struct mlxsw_sp_trap_item *
mlxsw_sp_trap_item_lookup(struct mlxsw_sp *mlxsw_sp, u16 id)
{
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	int i;

	for (i = 0; i < trap->traps_count; i++) {
		if (trap->trap_items_arr[i].trap.id == id)
			return &trap->trap_items_arr[i];
	}

	return NULL;
}

static int mlxsw_sp_trap_cpu_policers_set(struct mlxsw_sp *mlxsw_sp)
{
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	char qpcr_pl[MLXSW_REG_QPCR_LEN];
	u16 hw_id;

	/* The purpose of "thin" policer is to drop as many packets
	 * as possible. The dummy group is using it.
	 */
	hw_id = find_first_zero_bit(trap->policers_usage, trap->max_policers);
	if (WARN_ON(hw_id == trap->max_policers))
		return -ENOBUFS;

	__set_bit(hw_id, trap->policers_usage);
	trap->thin_policer_hw_id = hw_id;
	mlxsw_reg_qpcr_pack(qpcr_pl, hw_id, MLXSW_REG_QPCR_IR_UNITS_M,
			    false, 1, 4);
	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qpcr), qpcr_pl);
}

static int mlxsw_sp_trap_dummy_group_init(struct mlxsw_sp *mlxsw_sp)
{
	char htgt_pl[MLXSW_REG_HTGT_LEN];

	mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_SP_DUMMY,
			    mlxsw_sp->trap->thin_policer_hw_id, 0, 1);
	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(htgt), htgt_pl);
}

static int mlxsw_sp_trap_policer_items_arr_init(struct mlxsw_sp *mlxsw_sp)
{
	size_t arr_size = ARRAY_SIZE(mlxsw_sp_trap_policer_items_arr);
	size_t elem_size = sizeof(struct mlxsw_sp_trap_policer_item);
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	size_t free_policers = 0;
	u32 last_id;
	int i;

	for_each_clear_bit(i, trap->policers_usage, trap->max_policers)
		free_policers++;

	if (arr_size > free_policers) {
		dev_err(mlxsw_sp->bus_info->dev, "Exceeded number of supported packet trap policers\n");
		return -ENOBUFS;
	}

	trap->policer_items_arr = kcalloc(free_policers, elem_size, GFP_KERNEL);
	if (!trap->policer_items_arr)
		return -ENOMEM;

	trap->policers_count = free_policers;

	/* Initialize policer items array with pre-defined policers. */
	memcpy(trap->policer_items_arr, mlxsw_sp_trap_policer_items_arr,
	       elem_size * arr_size);

	/* Initialize policer items array with the rest of the available
	 * policers.
	 */
	last_id = mlxsw_sp_trap_policer_items_arr[arr_size - 1].policer.id;
	for (i = arr_size; i < trap->policers_count; i++) {
		const struct mlxsw_sp_trap_policer_item *policer_item;

		/* Use parameters set for first policer and override
		 * relevant ones.
		 */
		policer_item = &mlxsw_sp_trap_policer_items_arr[0];
		trap->policer_items_arr[i] = *policer_item;
		trap->policer_items_arr[i].policer.id = ++last_id;
		trap->policer_items_arr[i].policer.init_rate = 1;
		trap->policer_items_arr[i].policer.init_burst = 16;
	}

	return 0;
}

static void mlxsw_sp_trap_policer_items_arr_fini(struct mlxsw_sp *mlxsw_sp)
{
	kfree(mlxsw_sp->trap->policer_items_arr);
}

static int mlxsw_sp_trap_policers_init(struct mlxsw_sp *mlxsw_sp)
{
	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
	const struct mlxsw_sp_trap_policer_item *policer_item;
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	int err, i;

	err = mlxsw_sp_trap_policer_items_arr_init(mlxsw_sp);
	if (err)
		return err;

	for (i = 0; i < trap->policers_count; i++) {
		policer_item = &trap->policer_items_arr[i];
		err = devlink_trap_policers_register(devlink,
						     &policer_item->policer, 1);
		if (err)
			goto err_trap_policer_register;
	}

	return 0;

err_trap_policer_register:
	for (i--; i >= 0; i--) {
		policer_item = &trap->policer_items_arr[i];
		devlink_trap_policers_unregister(devlink,
						 &policer_item->policer, 1);
	}
	mlxsw_sp_trap_policer_items_arr_fini(mlxsw_sp);
	return err;
}

static void mlxsw_sp_trap_policers_fini(struct mlxsw_sp *mlxsw_sp)
{
	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
	const struct mlxsw_sp_trap_policer_item *policer_item;
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	int i;

	for (i = trap->policers_count - 1; i >= 0; i--) {
		policer_item = &trap->policer_items_arr[i];
		devlink_trap_policers_unregister(devlink,
						 &policer_item->policer, 1);
	}
	mlxsw_sp_trap_policer_items_arr_fini(mlxsw_sp);
}

static int mlxsw_sp_trap_group_items_arr_init(struct mlxsw_sp *mlxsw_sp)
{
	size_t common_groups_count = ARRAY_SIZE(mlxsw_sp_trap_group_items_arr);
	const struct mlxsw_sp_trap_group_item *spec_group_items_arr;
	size_t elem_size = sizeof(struct mlxsw_sp_trap_group_item);
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	size_t groups_count, spec_groups_count;
	int err;

	err = mlxsw_sp->trap_ops->groups_init(mlxsw_sp, &spec_group_items_arr,
					      &spec_groups_count);
	if (err)
		return err;

	/* The group items array is created by concatenating the common trap
	 * group items and the ASIC-specific trap group items.
	 */
	groups_count = common_groups_count + spec_groups_count;
	trap->group_items_arr = kcalloc(groups_count, elem_size, GFP_KERNEL);
	if (!trap->group_items_arr)
		return -ENOMEM;

	memcpy(trap->group_items_arr, mlxsw_sp_trap_group_items_arr,
	       elem_size * common_groups_count);
	memcpy(trap->group_items_arr + common_groups_count,
	       spec_group_items_arr, elem_size * spec_groups_count);

	trap->groups_count = groups_count;

	return 0;
}

static void mlxsw_sp_trap_group_items_arr_fini(struct mlxsw_sp *mlxsw_sp)
{
	kfree(mlxsw_sp->trap->group_items_arr);
}

static int mlxsw_sp_trap_groups_init(struct mlxsw_sp *mlxsw_sp)
{
	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
	const struct mlxsw_sp_trap_group_item *group_item;
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	int err, i;

	err = mlxsw_sp_trap_group_items_arr_init(mlxsw_sp);
	if (err)
		return err;

	for (i = 0; i < trap->groups_count; i++) {
		group_item = &trap->group_items_arr[i];
		err = devlink_trap_groups_register(devlink, &group_item->group,
						   1);
		if (err)
			goto err_trap_group_register;
	}

	return 0;

err_trap_group_register:
	for (i--; i >= 0; i--) {
		group_item = &trap->group_items_arr[i];
		devlink_trap_groups_unregister(devlink, &group_item->group, 1);
	}
	mlxsw_sp_trap_group_items_arr_fini(mlxsw_sp);
	return err;
}

static void mlxsw_sp_trap_groups_fini(struct mlxsw_sp *mlxsw_sp)
{
	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	int i;

	for (i = trap->groups_count - 1; i >= 0; i--) {
		const struct mlxsw_sp_trap_group_item *group_item;

		group_item = &trap->group_items_arr[i];
		devlink_trap_groups_unregister(devlink, &group_item->group, 1);
	}
	mlxsw_sp_trap_group_items_arr_fini(mlxsw_sp);
}

static bool
mlxsw_sp_trap_listener_is_valid(const struct mlxsw_listener *listener)
{
	return listener->trap_id != 0;
}

static int mlxsw_sp_trap_items_arr_init(struct mlxsw_sp *mlxsw_sp)
{
	size_t common_traps_count = ARRAY_SIZE(mlxsw_sp_trap_items_arr);
	const struct mlxsw_sp_trap_item *spec_trap_items_arr;
	size_t elem_size = sizeof(struct mlxsw_sp_trap_item);
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	size_t traps_count, spec_traps_count;
	int err;

	err = mlxsw_sp->trap_ops->traps_init(mlxsw_sp, &spec_trap_items_arr,
					     &spec_traps_count);
	if (err)
		return err;

	/* The trap items array is created by concatenating the common trap
	 * items and the ASIC-specific trap items.
	 */
	traps_count = common_traps_count + spec_traps_count;
	trap->trap_items_arr = kcalloc(traps_count, elem_size, GFP_KERNEL);
	if (!trap->trap_items_arr)
		return -ENOMEM;

	memcpy(trap->trap_items_arr, mlxsw_sp_trap_items_arr,
	       elem_size * common_traps_count);
	memcpy(trap->trap_items_arr + common_traps_count,
	       spec_trap_items_arr, elem_size * spec_traps_count);

	trap->traps_count = traps_count;

	return 0;
}

static void mlxsw_sp_trap_items_arr_fini(struct mlxsw_sp *mlxsw_sp)
{
	kfree(mlxsw_sp->trap->trap_items_arr);
}

static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
{
	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	const struct mlxsw_sp_trap_item *trap_item;
	int err, i;

	err = mlxsw_sp_trap_items_arr_init(mlxsw_sp);
	if (err)
		return err;

	for (i = 0; i < trap->traps_count; i++) {
		trap_item = &trap->trap_items_arr[i];
		err = devlink_traps_register(devlink, &trap_item->trap, 1,
					     mlxsw_sp);
		if (err)
			goto err_trap_register;
	}

	return 0;

err_trap_register:
	for (i--; i >= 0; i--) {
		trap_item = &trap->trap_items_arr[i];
		devlink_traps_unregister(devlink, &trap_item->trap, 1);
	}
	mlxsw_sp_trap_items_arr_fini(mlxsw_sp);
	return err;
}

static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
{
	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	int i;

	for (i = trap->traps_count - 1; i >= 0; i--) {
		const struct mlxsw_sp_trap_item *trap_item;

		trap_item = &trap->trap_items_arr[i];
		devlink_traps_unregister(devlink, &trap_item->trap, 1);
	}
	mlxsw_sp_trap_items_arr_fini(mlxsw_sp);
}

int mlxsw_sp_devlink_traps_init(struct mlxsw_sp *mlxsw_sp)
{
	int err;

	err = mlxsw_sp_trap_cpu_policers_set(mlxsw_sp);
	if (err)
		return err;

	err = mlxsw_sp_trap_dummy_group_init(mlxsw_sp);
	if (err)
		return err;

	err = mlxsw_sp_trap_policers_init(mlxsw_sp);
	if (err)
		return err;

	err = mlxsw_sp_trap_groups_init(mlxsw_sp);
	if (err)
		goto err_trap_groups_init;

	err = mlxsw_sp_traps_init(mlxsw_sp);
	if (err)
		goto err_traps_init;

	return 0;

err_traps_init:
	mlxsw_sp_trap_groups_fini(mlxsw_sp);
err_trap_groups_init:
	mlxsw_sp_trap_policers_fini(mlxsw_sp);
	return err;
}

void mlxsw_sp_devlink_traps_fini(struct mlxsw_sp *mlxsw_sp)
{
	mlxsw_sp_traps_fini(mlxsw_sp);
	mlxsw_sp_trap_groups_fini(mlxsw_sp);
	mlxsw_sp_trap_policers_fini(mlxsw_sp);
}

int mlxsw_sp_trap_init(struct mlxsw_core *mlxsw_core,
		       const struct devlink_trap *trap, void *trap_ctx)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	const struct mlxsw_sp_trap_item *trap_item;
	int i;

	trap_item = mlxsw_sp_trap_item_lookup(mlxsw_sp, trap->id);
	if (WARN_ON(!trap_item))
		return -EINVAL;

	for (i = 0; i < MLXSW_SP_TRAP_LISTENERS_MAX; i++) {
		const struct mlxsw_listener *listener;
		int err;

		listener = &trap_item->listeners_arr[i];
		if (!mlxsw_sp_trap_listener_is_valid(listener))
			continue;
		err = mlxsw_core_trap_register(mlxsw_core, listener, trap_ctx);
		if (err)
			return err;
	}

	return 0;
}

void mlxsw_sp_trap_fini(struct mlxsw_core *mlxsw_core,
			const struct devlink_trap *trap, void *trap_ctx)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	const struct mlxsw_sp_trap_item *trap_item;
	int i;

	trap_item = mlxsw_sp_trap_item_lookup(mlxsw_sp, trap->id);
	if (WARN_ON(!trap_item))
		return;

	for (i = MLXSW_SP_TRAP_LISTENERS_MAX - 1; i >= 0; i--) {
		const struct mlxsw_listener *listener;

		listener = &trap_item->listeners_arr[i];
		if (!mlxsw_sp_trap_listener_is_valid(listener))
			continue;
		mlxsw_core_trap_unregister(mlxsw_core, listener, trap_ctx);
	}
}

int mlxsw_sp_trap_action_set(struct mlxsw_core *mlxsw_core,
			     const struct devlink_trap *trap,
			     enum devlink_trap_action action,
			     struct netlink_ext_ack *extack)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	const struct mlxsw_sp_trap_item *trap_item;
	int i;

	trap_item = mlxsw_sp_trap_item_lookup(mlxsw_sp, trap->id);
	if (WARN_ON(!trap_item))
		return -EINVAL;

	if (trap_item->is_source) {
		NL_SET_ERR_MSG_MOD(extack, "Changing the action of source traps is not supported");
		return -EOPNOTSUPP;
	}

	for (i = 0; i < MLXSW_SP_TRAP_LISTENERS_MAX; i++) {
		const struct mlxsw_listener *listener;
		bool enabled;
		int err;

		listener = &trap_item->listeners_arr[i];
		if (!mlxsw_sp_trap_listener_is_valid(listener))
			continue;

		switch (action) {
		case DEVLINK_TRAP_ACTION_DROP:
			enabled = false;
			break;
		case DEVLINK_TRAP_ACTION_TRAP:
			enabled = true;
			break;
		default:
			return -EINVAL;
		}
		err = mlxsw_core_trap_state_set(mlxsw_core, listener, enabled);
		if (err)
			return err;
	}

	return 0;
}

static int
__mlxsw_sp_trap_group_init(struct mlxsw_core *mlxsw_core,
			   const struct devlink_trap_group *group,
			   u32 policer_id, struct netlink_ext_ack *extack)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	u16 hw_policer_id = MLXSW_REG_HTGT_INVALID_POLICER;
	const struct mlxsw_sp_trap_group_item *group_item;
	char htgt_pl[MLXSW_REG_HTGT_LEN];

	group_item = mlxsw_sp_trap_group_item_lookup(mlxsw_sp, group->id);
	if (WARN_ON(!group_item))
		return -EINVAL;

	if (group_item->fixed_policer && policer_id != group->init_policer_id) {
		NL_SET_ERR_MSG_MOD(extack, "Changing the policer binding of this group is not supported");
		return -EOPNOTSUPP;
	}

	if (policer_id) {
		struct mlxsw_sp_trap_policer_item *policer_item;

		policer_item = mlxsw_sp_trap_policer_item_lookup(mlxsw_sp,
								 policer_id);
		if (WARN_ON(!policer_item))
			return -EINVAL;
		hw_policer_id = policer_item->hw_id;
	}

	mlxsw_reg_htgt_pack(htgt_pl, group_item->hw_group_id, hw_policer_id,
			    group_item->priority, group_item->priority);
	return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
}

int mlxsw_sp_trap_group_init(struct mlxsw_core *mlxsw_core,
			     const struct devlink_trap_group *group)
{
	return __mlxsw_sp_trap_group_init(mlxsw_core, group,
					  group->init_policer_id, NULL);
}

int mlxsw_sp_trap_group_set(struct mlxsw_core *mlxsw_core,
			    const struct devlink_trap_group *group,
			    const struct devlink_trap_policer *policer,
			    struct netlink_ext_ack *extack)
{
	u32 policer_id = policer ? policer->id : 0;

	return __mlxsw_sp_trap_group_init(mlxsw_core, group, policer_id,
					  extack);
}

static int
mlxsw_sp_trap_policer_item_init(struct mlxsw_sp *mlxsw_sp,
				struct mlxsw_sp_trap_policer_item *policer_item)
{
	struct mlxsw_sp_trap *trap = mlxsw_sp->trap;
	u16 hw_id;

	/* We should be able to allocate a policer because the number of
	 * policers we registered with devlink is in according with the number
	 * of available policers.
	 */
	hw_id = find_first_zero_bit(trap->policers_usage, trap->max_policers);
	if (WARN_ON(hw_id == trap->max_policers))
		return -ENOBUFS;

	__set_bit(hw_id, trap->policers_usage);
	policer_item->hw_id = hw_id;

	return 0;
}

static void
mlxsw_sp_trap_policer_item_fini(struct mlxsw_sp *mlxsw_sp,
				struct mlxsw_sp_trap_policer_item *policer_item)
{
	__clear_bit(policer_item->hw_id, mlxsw_sp->trap->policers_usage);
}

static int mlxsw_sp_trap_policer_bs(u64 burst, u8 *p_burst_size,
				    struct netlink_ext_ack *extack)
{
	int bs = fls64(burst) - 1;

	if (burst != (BIT_ULL(bs))) {
		NL_SET_ERR_MSG_MOD(extack, "Policer burst size is not power of two");
		return -EINVAL;
	}

	*p_burst_size = bs;

	return 0;
}

static int __mlxsw_sp_trap_policer_set(struct mlxsw_sp *mlxsw_sp, u16 hw_id,
				       u64 rate, u64 burst, bool clear_counter,
				       struct netlink_ext_ack *extack)
{
	char qpcr_pl[MLXSW_REG_QPCR_LEN];
	u8 burst_size;
	int err;

	err = mlxsw_sp_trap_policer_bs(burst, &burst_size, extack);
	if (err)
		return err;

	mlxsw_reg_qpcr_pack(qpcr_pl, hw_id, MLXSW_REG_QPCR_IR_UNITS_M, false,
			    rate, burst_size);
	mlxsw_reg_qpcr_clear_counter_set(qpcr_pl, clear_counter);
	return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qpcr), qpcr_pl);
}

int mlxsw_sp_trap_policer_init(struct mlxsw_core *mlxsw_core,
			       const struct devlink_trap_policer *policer)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	struct mlxsw_sp_trap_policer_item *policer_item;
	int err;

	policer_item = mlxsw_sp_trap_policer_item_lookup(mlxsw_sp, policer->id);
	if (WARN_ON(!policer_item))
		return -EINVAL;

	err = mlxsw_sp_trap_policer_item_init(mlxsw_sp, policer_item);
	if (err)
		return err;

	err = __mlxsw_sp_trap_policer_set(mlxsw_sp, policer_item->hw_id,
					  policer->init_rate,
					  policer->init_burst, true, NULL);
	if (err)
		goto err_trap_policer_set;

	return 0;

err_trap_policer_set:
	mlxsw_sp_trap_policer_item_fini(mlxsw_sp, policer_item);
	return err;
}

void mlxsw_sp_trap_policer_fini(struct mlxsw_core *mlxsw_core,
				const struct devlink_trap_policer *policer)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	struct mlxsw_sp_trap_policer_item *policer_item;

	policer_item = mlxsw_sp_trap_policer_item_lookup(mlxsw_sp, policer->id);
	if (WARN_ON(!policer_item))
		return;

	mlxsw_sp_trap_policer_item_fini(mlxsw_sp, policer_item);
}

int mlxsw_sp_trap_policer_set(struct mlxsw_core *mlxsw_core,
			      const struct devlink_trap_policer *policer,
			      u64 rate, u64 burst,
			      struct netlink_ext_ack *extack)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	struct mlxsw_sp_trap_policer_item *policer_item;

	policer_item = mlxsw_sp_trap_policer_item_lookup(mlxsw_sp, policer->id);
	if (WARN_ON(!policer_item))
		return -EINVAL;

	return __mlxsw_sp_trap_policer_set(mlxsw_sp, policer_item->hw_id,
					   rate, burst, false, extack);
}

int
mlxsw_sp_trap_policer_counter_get(struct mlxsw_core *mlxsw_core,
				  const struct devlink_trap_policer *policer,
				  u64 *p_drops)
{
	struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
	struct mlxsw_sp_trap_policer_item *policer_item;
	char qpcr_pl[MLXSW_REG_QPCR_LEN];
	int err;

	policer_item = mlxsw_sp_trap_policer_item_lookup(mlxsw_sp, policer->id);
	if (WARN_ON(!policer_item))
		return -EINVAL;

	mlxsw_reg_qpcr_pack(qpcr_pl, policer_item->hw_id,
			    MLXSW_REG_QPCR_IR_UNITS_M, false, 0, 0);
	err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(qpcr), qpcr_pl);
	if (err)
		return err;

	*p_drops = mlxsw_reg_qpcr_violate_count_get(qpcr_pl);

	return 0;
}

int mlxsw_sp_trap_group_policer_hw_id_get(struct mlxsw_sp *mlxsw_sp, u16 id,
					  bool *p_enabled, u16 *p_hw_id)
{
	struct mlxsw_sp_trap_policer_item *pol_item;
	struct mlxsw_sp_trap_group_item *gr_item;
	u32 pol_id;

	gr_item = mlxsw_sp_trap_group_item_lookup(mlxsw_sp, id);
	if (!gr_item)
		return -ENOENT;

	pol_id = gr_item->group.init_policer_id;
	if (!pol_id) {
		*p_enabled = false;
		return 0;
	}

	pol_item = mlxsw_sp_trap_policer_item_lookup(mlxsw_sp, pol_id);
	if (WARN_ON(!pol_item))
		return -ENOENT;

	*p_enabled = true;
	*p_hw_id = pol_item->hw_id;
	return 0;
}

static const struct mlxsw_sp_trap_group_item
mlxsw_sp1_trap_group_items_arr[] = {
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(ACL_SAMPLE, 0),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_PKT_SAMPLE,
		.priority = 0,
	},
};

static const struct mlxsw_sp_trap_item
mlxsw_sp1_trap_items_arr[] = {
	{
		.trap = MLXSW_SP_TRAP_CONTROL(FLOW_ACTION_SAMPLE, ACL_SAMPLE,
					      MIRROR),
		.listeners_arr = {
			MLXSW_RXL(mlxsw_sp_rx_sample_listener, PKT_SAMPLE,
				  MIRROR_TO_CPU, false, SP_PKT_SAMPLE, DISCARD),
		},
	},
};

static int
mlxsw_sp1_trap_groups_init(struct mlxsw_sp *mlxsw_sp,
			   const struct mlxsw_sp_trap_group_item **arr,
			   size_t *p_groups_count)
{
	*arr = mlxsw_sp1_trap_group_items_arr;
	*p_groups_count = ARRAY_SIZE(mlxsw_sp1_trap_group_items_arr);

	return 0;
}

static int mlxsw_sp1_traps_init(struct mlxsw_sp *mlxsw_sp,
				const struct mlxsw_sp_trap_item **arr,
				size_t *p_traps_count)
{
	*arr = mlxsw_sp1_trap_items_arr;
	*p_traps_count = ARRAY_SIZE(mlxsw_sp1_trap_items_arr);

	return 0;
}

const struct mlxsw_sp_trap_ops mlxsw_sp1_trap_ops = {
	.groups_init = mlxsw_sp1_trap_groups_init,
	.traps_init = mlxsw_sp1_traps_init,
};

static const struct mlxsw_sp_trap_group_item
mlxsw_sp2_trap_group_items_arr[] = {
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(BUFFER_DROPS, 20),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_BUFFER_DISCARDS,
		.priority = 0,
		.fixed_policer = true,
	},
	{
		.group = DEVLINK_TRAP_GROUP_GENERIC(ACL_SAMPLE, 0),
		.hw_group_id = MLXSW_REG_HTGT_TRAP_GROUP_SP_PKT_SAMPLE,
		.priority = 0,
		.fixed_policer = true,
	},
};

static const struct mlxsw_sp_trap_item
mlxsw_sp2_trap_items_arr[] = {
	{
		.trap = MLXSW_SP_TRAP_BUFFER_DROP(EARLY_DROP),
		.listeners_arr = {
			MLXSW_SP_RXL_BUFFER_DISCARD(INGRESS_WRED),
		},
		.is_source = true,
	},
	{
		.trap = MLXSW_SP_TRAP_CONTROL(FLOW_ACTION_SAMPLE, ACL_SAMPLE,
					      MIRROR),
		.listeners_arr = {
			MLXSW_RXL_MIRROR(mlxsw_sp_rx_sample_listener, 1,
					 SP_PKT_SAMPLE,
					 MLXSW_SP_MIRROR_REASON_INGRESS),
			MLXSW_RXL_MIRROR(mlxsw_sp_rx_sample_tx_listener, 1,
					 SP_PKT_SAMPLE,
					 MLXSW_SP_MIRROR_REASON_EGRESS),
			MLXSW_RXL_MIRROR(mlxsw_sp_rx_sample_acl_listener, 1,
					 SP_PKT_SAMPLE,
					 MLXSW_SP_MIRROR_REASON_POLICY_ENGINE),
		},
	},
};

static int
mlxsw_sp2_trap_groups_init(struct mlxsw_sp *mlxsw_sp,
			   const struct mlxsw_sp_trap_group_item **arr,
			   size_t *p_groups_count)
{
	*arr = mlxsw_sp2_trap_group_items_arr;
	*p_groups_count = ARRAY_SIZE(mlxsw_sp2_trap_group_items_arr);

	return 0;
}

static int mlxsw_sp2_traps_init(struct mlxsw_sp *mlxsw_sp,
				const struct mlxsw_sp_trap_item **arr,
				size_t *p_traps_count)
{
	*arr = mlxsw_sp2_trap_items_arr;
	*p_traps_count = ARRAY_SIZE(mlxsw_sp2_trap_items_arr);

	return 0;
}

const struct mlxsw_sp_trap_ops mlxsw_sp2_trap_ops = {
	.groups_init = mlxsw_sp2_trap_groups_init,
	.traps_init = mlxsw_sp2_traps_init,
};