DPDK logo

Elixir Cross Referencer

   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
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
/* SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright(c) 2019-2020 Xilinx, Inc.
 * Copyright(c) 2007-2019 Solarflare Communications Inc.
 */

#include "efx.h"
#include "efx_impl.h"

#if EFX_OPTS_EF10()

#if EFSYS_OPT_FILTER

#define	EFE_SPEC(eftp, index)	((eftp)->eft_entry[(index)].efe_spec)

static			efx_filter_spec_t *
ef10_filter_entry_spec(
	__in		const ef10_filter_table_t *eftp,
	__in		unsigned int index)
{
	return ((efx_filter_spec_t *)(EFE_SPEC(eftp, index) &
		~(uintptr_t)EFX_EF10_FILTER_FLAGS));
}

static			boolean_t
ef10_filter_entry_is_busy(
	__in		const ef10_filter_table_t *eftp,
	__in		unsigned int index)
{
	if (EFE_SPEC(eftp, index) & EFX_EF10_FILTER_FLAG_BUSY)
		return (B_TRUE);
	else
		return (B_FALSE);
}

static			boolean_t
ef10_filter_entry_is_auto_old(
	__in		const ef10_filter_table_t *eftp,
	__in		unsigned int index)
{
	if (EFE_SPEC(eftp, index) & EFX_EF10_FILTER_FLAG_AUTO_OLD)
		return (B_TRUE);
	else
		return (B_FALSE);
}

static			void
ef10_filter_set_entry(
	__inout		ef10_filter_table_t *eftp,
	__in		unsigned int index,
	__in_opt	const efx_filter_spec_t *efsp)
{
	EFE_SPEC(eftp, index) = (uintptr_t)efsp;
}

static			void
ef10_filter_set_entry_busy(
	__inout		ef10_filter_table_t *eftp,
	__in		unsigned int index)
{
	EFE_SPEC(eftp, index) |= (uintptr_t)EFX_EF10_FILTER_FLAG_BUSY;
}

static			void
ef10_filter_set_entry_not_busy(
	__inout		ef10_filter_table_t *eftp,
	__in		unsigned int index)
{
	EFE_SPEC(eftp, index) &= ~(uintptr_t)EFX_EF10_FILTER_FLAG_BUSY;
}

static			void
ef10_filter_set_entry_auto_old(
	__inout		ef10_filter_table_t *eftp,
	__in		unsigned int index)
{
	EFSYS_ASSERT(ef10_filter_entry_spec(eftp, index) != NULL);
	EFE_SPEC(eftp, index) |= (uintptr_t)EFX_EF10_FILTER_FLAG_AUTO_OLD;
}

static			void
ef10_filter_set_entry_not_auto_old(
	__inout		ef10_filter_table_t *eftp,
	__in		unsigned int index)
{
	EFE_SPEC(eftp, index) &= ~(uintptr_t)EFX_EF10_FILTER_FLAG_AUTO_OLD;
	EFSYS_ASSERT(ef10_filter_entry_spec(eftp, index) != NULL);
}

	__checkReturn	efx_rc_t
ef10_filter_init(
	__in		efx_nic_t *enp)
{
	efx_rc_t rc;
	ef10_filter_table_t *eftp;

	EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));

#define	MATCH_MASK(match) (EFX_MASK32(match) << EFX_LOW_BIT(match))
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_HOST ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_SRC_IP));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_HOST ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_DST_IP));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_MAC ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_SRC_MAC));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_REM_PORT ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_SRC_PORT));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_MAC ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_DST_MAC));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_LOC_PORT ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_DST_PORT));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_ETHER_TYPE ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_ETHER_TYPE));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_INNER_VID ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_INNER_VLAN));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_OUTER_VID ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_OUTER_VLAN));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_IP_PROTO ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_IP_PROTO));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_VNI_OR_VSID ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_VNI_OR_VSID));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_IFRM_LOC_MAC ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_DST_MAC));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_IFRM_UNKNOWN_MCAST_DST ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_UNKNOWN_MCAST_DST));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_IFRM_UNKNOWN_UCAST_DST));
	EFX_STATIC_ASSERT(EFX_FILTER_MATCH_UNKNOWN_MCAST_DST ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_UNKNOWN_MCAST_DST));
	EFX_STATIC_ASSERT((uint32_t)EFX_FILTER_MATCH_UNKNOWN_UCAST_DST ==
	    MATCH_MASK(MC_CMD_FILTER_OP_EXT_IN_MATCH_UNKNOWN_UCAST_DST));
#undef MATCH_MASK

	EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (ef10_filter_table_t), eftp);

	if (!eftp) {
		rc = ENOMEM;
		goto fail1;
	}

	enp->en_filter.ef_ef10_filter_table = eftp;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

			void
ef10_filter_fini(
	__in		efx_nic_t *enp)
{
	EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));

	if (enp->en_filter.ef_ef10_filter_table != NULL) {
		EFSYS_KMEM_FREE(enp->en_esip, sizeof (ef10_filter_table_t),
		    enp->en_filter.ef_ef10_filter_table);
	}
}

static	__checkReturn	efx_rc_t
efx_mcdi_filter_op_add(
	__in		efx_nic_t *enp,
	__in		efx_filter_spec_t *spec,
	__in		unsigned int filter_op,
	__inout		ef10_filter_handle_t *handle)
{
	efx_mcdi_req_t req;
	EFX_MCDI_DECLARE_BUF(payload, MC_CMD_FILTER_OP_V3_IN_LEN,
		MC_CMD_FILTER_OP_EXT_OUT_LEN);
	efx_filter_match_flags_t match_flags;
	efx_rc_t rc;

	req.emr_cmd = MC_CMD_FILTER_OP;
	req.emr_in_buf = payload;
	req.emr_in_length = MC_CMD_FILTER_OP_V3_IN_LEN;
	req.emr_out_buf = payload;
	req.emr_out_length = MC_CMD_FILTER_OP_EXT_OUT_LEN;

	/*
	 * Remove match flag for encapsulated filters that does not correspond
	 * to the MCDI match flags
	 */
	match_flags = spec->efs_match_flags & ~EFX_FILTER_MATCH_ENCAP_TYPE;

	switch (filter_op) {
	case MC_CMD_FILTER_OP_IN_OP_REPLACE:
		MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_HANDLE_LO,
		    handle->efh_lo);
		MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_HANDLE_HI,
		    handle->efh_hi);
		/* Fall through */
	case MC_CMD_FILTER_OP_IN_OP_INSERT:
	case MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE:
		MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_OP, filter_op);
		break;
	default:
		EFSYS_ASSERT(0);
		rc = EINVAL;
		goto fail1;
	}

	MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_PORT_ID, enp->en_vport_id);
	MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_MATCH_FIELDS,
	    match_flags);
	if (spec->efs_dmaq_id == EFX_FILTER_SPEC_RX_DMAQ_ID_DROP) {
		MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_RX_DEST,
		    MC_CMD_FILTER_OP_EXT_IN_RX_DEST_DROP);
	} else {
		MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_RX_DEST,
		    MC_CMD_FILTER_OP_EXT_IN_RX_DEST_HOST);
		MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_RX_QUEUE,
		    spec->efs_dmaq_id);
	}

#if EFSYS_OPT_RX_SCALE
	if (spec->efs_flags & EFX_FILTER_FLAG_RX_RSS) {
		uint32_t rss_context;

		if (spec->efs_rss_context == EFX_RSS_CONTEXT_DEFAULT)
			rss_context = enp->en_rss_context;
		else
			rss_context = spec->efs_rss_context;
		MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_RX_CONTEXT,
		    rss_context);
	}
#endif

	MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_RX_MODE,
	    spec->efs_flags & EFX_FILTER_FLAG_RX_RSS ?
	    MC_CMD_FILTER_OP_EXT_IN_RX_MODE_RSS :
	    MC_CMD_FILTER_OP_EXT_IN_RX_MODE_SIMPLE);
	MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_TX_DEST,
	    MC_CMD_FILTER_OP_EXT_IN_TX_DEST_DEFAULT);

	if (filter_op != MC_CMD_FILTER_OP_IN_OP_REPLACE) {
		/*
		 * NOTE: Unlike most MCDI requests, the filter fields
		 * are presented in network (big endian) byte order.
		 */
		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_SRC_MAC),
		    spec->efs_rem_mac, EFX_MAC_ADDR_LEN);
		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_DST_MAC),
		    spec->efs_loc_mac, EFX_MAC_ADDR_LEN);

		MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_SRC_PORT,
		    __CPU_TO_BE_16(spec->efs_rem_port));
		MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_DST_PORT,
		    __CPU_TO_BE_16(spec->efs_loc_port));

		MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_ETHER_TYPE,
		    __CPU_TO_BE_16(spec->efs_ether_type));

		MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_INNER_VLAN,
		    __CPU_TO_BE_16(spec->efs_inner_vid));
		MCDI_IN_SET_WORD(req, FILTER_OP_EXT_IN_OUTER_VLAN,
		    __CPU_TO_BE_16(spec->efs_outer_vid));

		/* IP protocol (in low byte, high byte is zero) */
		MCDI_IN_SET_BYTE(req, FILTER_OP_EXT_IN_IP_PROTO,
		    spec->efs_ip_proto);

		EFX_STATIC_ASSERT(sizeof (spec->efs_rem_host) ==
		    MC_CMD_FILTER_OP_EXT_IN_SRC_IP_LEN);
		EFX_STATIC_ASSERT(sizeof (spec->efs_loc_host) ==
		    MC_CMD_FILTER_OP_EXT_IN_DST_IP_LEN);

		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_SRC_IP),
		    &spec->efs_rem_host.eo_byte[0],
		    MC_CMD_FILTER_OP_EXT_IN_SRC_IP_LEN);
		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_DST_IP),
		    &spec->efs_loc_host.eo_byte[0],
		    MC_CMD_FILTER_OP_EXT_IN_DST_IP_LEN);

		/*
		 * On Medford, filters for encapsulated packets match based on
		 * the ether type and IP protocol in the outer frame.  In
		 * addition we need to fill in the VNI or VSID type field.
		 */
		switch (spec->efs_encap_type) {
		case EFX_TUNNEL_PROTOCOL_NONE:
			break;
		case EFX_TUNNEL_PROTOCOL_VXLAN:
		case EFX_TUNNEL_PROTOCOL_GENEVE:
			MCDI_IN_POPULATE_DWORD_1(req,
			    FILTER_OP_EXT_IN_VNI_OR_VSID,
			    FILTER_OP_EXT_IN_VNI_TYPE,
			    spec->efs_encap_type == EFX_TUNNEL_PROTOCOL_VXLAN ?
				    MC_CMD_FILTER_OP_EXT_IN_VNI_TYPE_VXLAN :
				    MC_CMD_FILTER_OP_EXT_IN_VNI_TYPE_GENEVE);
			break;
		case EFX_TUNNEL_PROTOCOL_NVGRE:
			MCDI_IN_POPULATE_DWORD_1(req,
			    FILTER_OP_EXT_IN_VNI_OR_VSID,
			    FILTER_OP_EXT_IN_VSID_TYPE,
			    MC_CMD_FILTER_OP_EXT_IN_VSID_TYPE_NVGRE);
			break;
		default:
			EFSYS_ASSERT(0);
			rc = EINVAL;
			goto fail2;
		}

		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_VNI_OR_VSID),
		    spec->efs_vni_or_vsid, EFX_VNI_OR_VSID_LEN);

		memcpy(MCDI_IN2(req, uint8_t, FILTER_OP_EXT_IN_IFRM_DST_MAC),
		    spec->efs_ifrm_loc_mac, EFX_MAC_ADDR_LEN);
	}

	/*
	 * Set the "MARK" or "FLAG" action for all packets matching this filter
	 * if necessary (only useful with equal stride packed stream Rx mode
	 * which provide the information in pseudo-header).
	 * These actions require MC_CMD_FILTER_OP_V3_IN msgrequest.
	 */
	if ((spec->efs_flags & EFX_FILTER_FLAG_ACTION_MARK) &&
	    (spec->efs_flags & EFX_FILTER_FLAG_ACTION_FLAG)) {
		rc = EINVAL;
		goto fail3;
	}
	if (spec->efs_flags & EFX_FILTER_FLAG_ACTION_MARK) {
		MCDI_IN_SET_DWORD(req, FILTER_OP_V3_IN_MATCH_ACTION,
		    MC_CMD_FILTER_OP_V3_IN_MATCH_ACTION_MARK);
		MCDI_IN_SET_DWORD(req, FILTER_OP_V3_IN_MATCH_MARK_VALUE,
		    spec->efs_mark);
	} else if (spec->efs_flags & EFX_FILTER_FLAG_ACTION_FLAG) {
		MCDI_IN_SET_DWORD(req, FILTER_OP_V3_IN_MATCH_ACTION,
		    MC_CMD_FILTER_OP_V3_IN_MATCH_ACTION_FLAG);
	}

	efx_mcdi_execute(enp, &req);

	if (req.emr_rc != 0) {
		rc = req.emr_rc;
		goto fail4;
	}

	if (req.emr_out_length_used < MC_CMD_FILTER_OP_EXT_OUT_LEN) {
		rc = EMSGSIZE;
		goto fail5;
	}

	handle->efh_lo = MCDI_OUT_DWORD(req, FILTER_OP_EXT_OUT_HANDLE_LO);
	handle->efh_hi = MCDI_OUT_DWORD(req, FILTER_OP_EXT_OUT_HANDLE_HI);

	return (0);

fail5:
	EFSYS_PROBE(fail5);
fail4:
	EFSYS_PROBE(fail4);
fail3:
	EFSYS_PROBE(fail3);
fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);

}

static	__checkReturn	efx_rc_t
efx_mcdi_filter_op_delete(
	__in		efx_nic_t *enp,
	__in		unsigned int filter_op,
	__inout		ef10_filter_handle_t *handle)
{
	efx_mcdi_req_t req;
	EFX_MCDI_DECLARE_BUF(payload, MC_CMD_FILTER_OP_EXT_IN_LEN,
		MC_CMD_FILTER_OP_EXT_OUT_LEN);
	efx_rc_t rc;

	req.emr_cmd = MC_CMD_FILTER_OP;
	req.emr_in_buf = payload;
	req.emr_in_length = MC_CMD_FILTER_OP_EXT_IN_LEN;
	req.emr_out_buf = payload;
	req.emr_out_length = MC_CMD_FILTER_OP_EXT_OUT_LEN;

	switch (filter_op) {
	case MC_CMD_FILTER_OP_IN_OP_REMOVE:
		MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_OP,
		    MC_CMD_FILTER_OP_IN_OP_REMOVE);
		break;
	case MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE:
		MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_OP,
		    MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
		break;
	default:
		EFSYS_ASSERT(0);
		rc = EINVAL;
		goto fail1;
	}

	MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_HANDLE_LO, handle->efh_lo);
	MCDI_IN_SET_DWORD(req, FILTER_OP_EXT_IN_HANDLE_HI, handle->efh_hi);

	efx_mcdi_execute_quiet(enp, &req);

	if (req.emr_rc != 0) {
		rc = req.emr_rc;
		goto fail2;
	}

	if (req.emr_out_length_used < MC_CMD_FILTER_OP_EXT_OUT_LEN) {
		rc = EMSGSIZE;
		goto fail3;
	}

	return (0);

fail3:
	EFSYS_PROBE(fail3);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

static	__checkReturn	boolean_t
ef10_filter_equal(
	__in		const efx_filter_spec_t *left,
	__in		const efx_filter_spec_t *right)
{
	/* FIXME: Consider rx vs tx filters (look at efs_flags) */
	if (left->efs_match_flags != right->efs_match_flags)
		return (B_FALSE);
	if (!EFX_OWORD_IS_EQUAL(left->efs_rem_host, right->efs_rem_host))
		return (B_FALSE);
	if (!EFX_OWORD_IS_EQUAL(left->efs_loc_host, right->efs_loc_host))
		return (B_FALSE);
	if (memcmp(left->efs_rem_mac, right->efs_rem_mac, EFX_MAC_ADDR_LEN))
		return (B_FALSE);
	if (memcmp(left->efs_loc_mac, right->efs_loc_mac, EFX_MAC_ADDR_LEN))
		return (B_FALSE);
	if (left->efs_rem_port != right->efs_rem_port)
		return (B_FALSE);
	if (left->efs_loc_port != right->efs_loc_port)
		return (B_FALSE);
	if (left->efs_inner_vid != right->efs_inner_vid)
		return (B_FALSE);
	if (left->efs_outer_vid != right->efs_outer_vid)
		return (B_FALSE);
	if (left->efs_ether_type != right->efs_ether_type)
		return (B_FALSE);
	if (left->efs_ip_proto != right->efs_ip_proto)
		return (B_FALSE);
	if (left->efs_encap_type != right->efs_encap_type)
		return (B_FALSE);
	if (memcmp(left->efs_vni_or_vsid, right->efs_vni_or_vsid,
	    EFX_VNI_OR_VSID_LEN))
		return (B_FALSE);
	if (memcmp(left->efs_ifrm_loc_mac, right->efs_ifrm_loc_mac,
	    EFX_MAC_ADDR_LEN))
		return (B_FALSE);

	return (B_TRUE);

}

static	__checkReturn	boolean_t
ef10_filter_same_dest(
	__in		const efx_filter_spec_t *left,
	__in		const efx_filter_spec_t *right)
{
	if ((left->efs_flags & EFX_FILTER_FLAG_RX_RSS) &&
	    (right->efs_flags & EFX_FILTER_FLAG_RX_RSS)) {
		if (left->efs_rss_context == right->efs_rss_context)
			return (B_TRUE);
	} else if ((~(left->efs_flags) & EFX_FILTER_FLAG_RX_RSS) &&
	    (~(right->efs_flags) & EFX_FILTER_FLAG_RX_RSS)) {
		if (left->efs_dmaq_id == right->efs_dmaq_id)
			return (B_TRUE);
	}
	return (B_FALSE);
}

static	__checkReturn	uint32_t
ef10_filter_hash(
	__in		efx_filter_spec_t *spec)
{
	EFX_STATIC_ASSERT((sizeof (efx_filter_spec_t) % sizeof (uint32_t))
			    == 0);
	EFX_STATIC_ASSERT((EFX_FIELD_OFFSET(efx_filter_spec_t, efs_outer_vid) %
			    sizeof (uint32_t)) == 0);

	/*
	 * As the area of the efx_filter_spec_t we need to hash is DWORD
	 * aligned and an exact number of DWORDs in size we can use the
	 * optimised efx_hash_dwords() rather than efx_hash_bytes()
	 */
	return (efx_hash_dwords((const uint32_t *)&spec->efs_outer_vid,
			(sizeof (efx_filter_spec_t) -
			EFX_FIELD_OFFSET(efx_filter_spec_t, efs_outer_vid)) /
			sizeof (uint32_t), 0));
}

/*
 * Decide whether a filter should be exclusive or else should allow
 * delivery to additional recipients.  Currently we decide that
 * filters for specific local unicast MAC and IP addresses are
 * exclusive.
 */
static	__checkReturn	boolean_t
ef10_filter_is_exclusive(
	__in		efx_filter_spec_t *spec)
{
	if ((spec->efs_match_flags & EFX_FILTER_MATCH_LOC_MAC) &&
	    !EFX_MAC_ADDR_IS_MULTICAST(spec->efs_loc_mac))
		return (B_TRUE);

	if ((spec->efs_match_flags &
		(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
	    (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
		if ((spec->efs_ether_type == EFX_ETHER_TYPE_IPV4) &&
		    ((spec->efs_loc_host.eo_u8[0] & 0xf) != 0xe))
			return (B_TRUE);
		if ((spec->efs_ether_type == EFX_ETHER_TYPE_IPV6) &&
		    (spec->efs_loc_host.eo_u8[0] != 0xff))
			return (B_TRUE);
	}

	return (B_FALSE);
}

	__checkReturn	efx_rc_t
ef10_filter_restore(
	__in		efx_nic_t *enp)
{
	int tbl_id;
	efx_filter_spec_t *spec;
	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
	boolean_t restoring;
	efsys_lock_state_t state;
	efx_rc_t rc;

	EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));

	for (tbl_id = 0; tbl_id < EFX_EF10_FILTER_TBL_ROWS; tbl_id++) {

		EFSYS_LOCK(enp->en_eslp, state);

		spec = ef10_filter_entry_spec(eftp, tbl_id);
		if (spec == NULL) {
			restoring = B_FALSE;
		} else if (ef10_filter_entry_is_busy(eftp, tbl_id)) {
			/* Ignore busy entries. */
			restoring = B_FALSE;
		} else {
			ef10_filter_set_entry_busy(eftp, tbl_id);
			restoring = B_TRUE;
		}

		EFSYS_UNLOCK(enp->en_eslp, state);

		if (restoring == B_FALSE)
			continue;

		if (ef10_filter_is_exclusive(spec)) {
			rc = efx_mcdi_filter_op_add(enp, spec,
			    MC_CMD_FILTER_OP_IN_OP_INSERT,
			    &eftp->eft_entry[tbl_id].efe_handle);
		} else {
			rc = efx_mcdi_filter_op_add(enp, spec,
			    MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE,
			    &eftp->eft_entry[tbl_id].efe_handle);
		}

		if (rc != 0)
			goto fail1;

		EFSYS_LOCK(enp->en_eslp, state);

		ef10_filter_set_entry_not_busy(eftp, tbl_id);

		EFSYS_UNLOCK(enp->en_eslp, state);
	}

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

enum ef10_filter_add_action_e {
	/* Insert a new filter */
	EF10_FILTER_ADD_NEW,
	/*
	 * Replace old filter with a new, overriding the old one
	 * if it has lower priority.
	 */
	EF10_FILTER_ADD_REPLACE,
	/* Store new, lower priority filter as overridden by old filter */
	EF10_FILTER_ADD_STORE,
	/* Special case for AUTO filters, remove AUTO_OLD flag */
	EF10_FILTER_ADD_REFRESH,
};

static	__checkReturn	efx_rc_t
ef10_filter_add_lookup_equal_spec(
	__in		efx_filter_spec_t *spec,
	__in		efx_filter_spec_t *probe_spec,
	__in		efx_filter_replacement_policy_t policy,
	__out		boolean_t *found)
{
	efx_rc_t rc;

	/* Refreshing AUTO filter */
	if (spec->efs_priority == EFX_FILTER_PRI_AUTO &&
	    probe_spec->efs_priority == EFX_FILTER_PRI_AUTO) {
		*found = B_TRUE;
		return (0);
	}

	/*
	 * With exclusive filters, higher priority ones
	 * override lower priority ones, and lower priority
	 * ones are stored in case the higher priority one
	 * is removed.
	 */
	if (ef10_filter_is_exclusive(spec)) {
		switch (policy) {
		case EFX_FILTER_REPLACEMENT_HIGHER_OR_EQUAL_PRIORITY:
			if (spec->efs_priority == probe_spec->efs_priority) {
				*found = B_TRUE;
				break;
			}
			/* Fall-through */
		case EFX_FILTER_REPLACEMENT_HIGHER_PRIORITY:
			if (spec->efs_priority > probe_spec->efs_priority) {
				*found = B_TRUE;
				break;
			}
			/* Fall-through */
		case EFX_FILTER_REPLACEMENT_NEVER:
			/*
			 * Lower priority filter needs to be
			 * stored. It does *not* replace the
			 * old one. That is why EEXIST is not
			 * returned in that case.
			 */
			if (spec->efs_priority < probe_spec->efs_priority) {
				*found = B_TRUE;
				break;
			} else {
				rc = EEXIST;
				goto fail1;
			}
		default:
			EFSYS_ASSERT(0);
			rc = EEXIST;
			goto fail2;
		}
	} else {
		*found = B_FALSE;
	}

	return (0);

fail2:
	EFSYS_PROBE(fail2);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}


static			void
ef10_filter_add_select_action(
	__in		efx_filter_spec_t *saved_spec,
	__in		efx_filter_spec_t *spec,
	__out		enum ef10_filter_add_action_e *action,
	__out		efx_filter_spec_t **overridden_spec)
{
	efx_filter_spec_t *overridden = NULL;

	if (saved_spec == NULL) {
		*action = EF10_FILTER_ADD_NEW;
	} else if (ef10_filter_is_exclusive(spec) == B_FALSE) {
		/*
		 * Non-exclusive filters are always stored in separate entries
		 * in the table. The only case involving a saved spec is
		 * refreshing an AUTO filter.
		 */
		EFSYS_ASSERT(saved_spec->efs_overridden_spec == NULL);
		EFSYS_ASSERT(spec->efs_priority == EFX_FILTER_PRI_AUTO);
		EFSYS_ASSERT(saved_spec->efs_priority == EFX_FILTER_PRI_AUTO);
		*action = EF10_FILTER_ADD_REFRESH;
	} else {
		/* Exclusive filters stored in the same entry */
		if (spec->efs_priority > saved_spec->efs_priority) {
			/*
			 * Insert a high priority filter over a lower priority
			 * one. Only two priority levels are implemented, so
			 * there must not already be an overridden filter.
			 */
			EFX_STATIC_ASSERT(EFX_FILTER_NPRI == 2);
			EFSYS_ASSERT(saved_spec->efs_overridden_spec == NULL);
			overridden = saved_spec;
			*action = EF10_FILTER_ADD_REPLACE;
		} else if (spec->efs_priority == saved_spec->efs_priority) {
			/* Replace in-place or refresh an existing filter */
			if (spec->efs_priority == EFX_FILTER_PRI_AUTO)
				*action = EF10_FILTER_ADD_REFRESH;
			else
				*action = EF10_FILTER_ADD_REPLACE;
		} else {
			/*
			 * Insert a lower priority filter, storing it in case
			 * the higher priority filter is removed.
			 *
			 * Currently there are only two priority levels, so this
			 * must be an AUTO filter.
			 */
			EFX_STATIC_ASSERT(EFX_FILTER_NPRI == 2);
			EFSYS_ASSERT(spec->efs_priority == EFX_FILTER_PRI_AUTO);
			if (saved_spec->efs_overridden_spec != NULL) {
				*action = EF10_FILTER_ADD_REFRESH;
			} else {
				overridden = spec;
				*action = EF10_FILTER_ADD_STORE;
			}
		}
	}

	*overridden_spec = overridden;
}

static	__checkReturn	efx_rc_t
ef10_filter_add_execute_action(
	__in		efx_nic_t *enp,
	__in		efx_filter_spec_t *saved_spec,
	__in		efx_filter_spec_t *spec,
	__in		efx_filter_spec_t *overridden_spec,
	__in		enum ef10_filter_add_action_e action,
	__in		int ins_index)
{
	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
	efsys_lock_state_t state;
	efx_rc_t rc;

	EFSYS_LOCK(enp->en_eslp, state);

	if (action == EF10_FILTER_ADD_REFRESH) {
		ef10_filter_set_entry_not_auto_old(eftp, ins_index);
		goto out_unlock;
	} else if (action == EF10_FILTER_ADD_STORE) {
		EFSYS_ASSERT(overridden_spec != NULL);
		saved_spec->efs_overridden_spec = overridden_spec;
		goto out_unlock;
	}

	EFSYS_UNLOCK(enp->en_eslp, state);

	switch (action) {
	case EF10_FILTER_ADD_REPLACE:
		/*
		 * On replacing the filter handle may change after a
		 * successful replace operation.
		 */
		rc = efx_mcdi_filter_op_add(enp, spec,
		    MC_CMD_FILTER_OP_IN_OP_REPLACE,
		    &eftp->eft_entry[ins_index].efe_handle);
		break;
	case EF10_FILTER_ADD_NEW:
		if (ef10_filter_is_exclusive(spec)) {
			rc = efx_mcdi_filter_op_add(enp, spec,
			    MC_CMD_FILTER_OP_IN_OP_INSERT,
			    &eftp->eft_entry[ins_index].efe_handle);
		} else {
			rc = efx_mcdi_filter_op_add(enp, spec,
			    MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE,
			    &eftp->eft_entry[ins_index].efe_handle);
		}
		break;
	default:
		rc = EINVAL;
		EFSYS_ASSERT(0);
		break;
	}
	if (rc != 0)
		goto fail1;

	EFSYS_LOCK(enp->en_eslp, state);

	if (action == EF10_FILTER_ADD_REPLACE) {
		/* Update the fields that may differ */
		saved_spec->efs_priority = spec->efs_priority;
		saved_spec->efs_flags = spec->efs_flags;
		saved_spec->efs_rss_context = spec->efs_rss_context;
		saved_spec->efs_dmaq_id = spec->efs_dmaq_id;

		if (overridden_spec != NULL)
			saved_spec->efs_overridden_spec = overridden_spec;
	}

out_unlock:
	EFSYS_UNLOCK(enp->en_eslp, state);

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

/*
 * An arbitrary search limit for the software hash table. As per the linux net
 * driver.
 */
#define	EF10_FILTER_SEARCH_LIMIT 200

static	__checkReturn	efx_rc_t
ef10_filter_add_internal(
	__in		efx_nic_t *enp,
	__inout		efx_filter_spec_t *spec,
	__in		efx_filter_replacement_policy_t policy,
	__out_opt	uint32_t *filter_id)
{
	efx_rc_t rc;
	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
	enum ef10_filter_add_action_e action;
	efx_filter_spec_t *overridden_spec = NULL;
	efx_filter_spec_t *saved_spec;
	uint32_t hash;
	unsigned int depth;
	int ins_index;
	efsys_lock_state_t state;
	boolean_t locked = B_FALSE;

	EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));

	EFSYS_ASSERT(spec->efs_overridden_spec == NULL);

	hash = ef10_filter_hash(spec);

	/*
	 * FIXME: Add support for inserting filters of different priorities
	 * and removing lower priority multicast filters (bug 42378)
	 */

	/*
	 * Find any existing filters with the same match tuple or
	 * else a free slot to insert at.  If any of them are busy,
	 * we have to wait and retry.
	 */
retry:
	EFSYS_LOCK(enp->en_eslp, state);
	locked = B_TRUE;

	ins_index = -1;

	for (depth = 1; depth <= EF10_FILTER_SEARCH_LIMIT; depth++) {
		unsigned int probe_index;
		efx_filter_spec_t *probe_spec;

		probe_index = (hash + depth) & (EFX_EF10_FILTER_TBL_ROWS - 1);
		probe_spec = ef10_filter_entry_spec(eftp, probe_index);

		if (probe_spec == NULL) {
			if (ins_index < 0)
				ins_index = probe_index;
		} else if (ef10_filter_equal(spec, probe_spec)) {
			boolean_t found;

			if (ef10_filter_entry_is_busy(eftp, probe_index)) {
				EFSYS_UNLOCK(enp->en_eslp, state);
				locked = B_FALSE;
				goto retry;
			}

			rc = ef10_filter_add_lookup_equal_spec(spec,
			    probe_spec, policy, &found);
			if (rc != 0)
				goto fail1;

			if (found != B_FALSE) {
				ins_index = probe_index;
				break;
			}
		}
	}

	/*
	 * Once we reach the maximum search depth, use the first suitable slot
	 * or return EBUSY if there was none.
	 */
	if (ins_index < 0) {
		rc = EBUSY;
		goto fail2;
	}

	/*
	 * Mark software table entry busy. We might yet fail to insert,
	 * but any attempt to insert a conflicting filter while we're
	 * waiting for the firmware must find the busy entry.
	 */
	ef10_filter_set_entry_busy(eftp, ins_index);

	saved_spec = ef10_filter_entry_spec(eftp, ins_index);
	ef10_filter_add_select_action(saved_spec, spec, &action,
	    &overridden_spec);

	/*
	 * Allocate a new filter if found entry is empty or
	 * a filter should be overridden.
	 */
	if (overridden_spec != NULL || saved_spec == NULL) {
		efx_filter_spec_t *new_spec;

		EFSYS_UNLOCK(enp->en_eslp, state);
		locked = B_FALSE;

		EFSYS_KMEM_ALLOC(enp->en_esip, sizeof (*new_spec), new_spec);
		if (new_spec == NULL) {
			rc = ENOMEM;
			overridden_spec = NULL;
			goto fail3;
		}

		EFSYS_LOCK(enp->en_eslp, state);
		locked = B_TRUE;

		if (saved_spec == NULL) {
			*new_spec = *spec;
			ef10_filter_set_entry(eftp, ins_index, new_spec);
		} else {
			*new_spec = *overridden_spec;
			overridden_spec = new_spec;
		}
	}

	EFSYS_UNLOCK(enp->en_eslp, state);
	locked = B_FALSE;

	rc = ef10_filter_add_execute_action(enp, saved_spec, spec,
	    overridden_spec, action, ins_index);
	if (rc != 0)
		goto fail4;

	if (filter_id)
		*filter_id = ins_index;

	EFSYS_LOCK(enp->en_eslp, state);
	ef10_filter_set_entry_not_busy(eftp, ins_index);
	EFSYS_UNLOCK(enp->en_eslp, state);

	return (0);

fail4:
	EFSYS_PROBE(fail4);

	EFSYS_ASSERT(locked == B_FALSE);
	EFSYS_LOCK(enp->en_eslp, state);

	if (action == EF10_FILTER_ADD_NEW) {
		EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec),
		    ef10_filter_entry_spec(eftp, ins_index));
		ef10_filter_set_entry(eftp, ins_index, NULL);
	}

	EFSYS_UNLOCK(enp->en_eslp, state);

	if (overridden_spec != NULL)
		EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec), overridden_spec);

fail3:
	EFSYS_PROBE(fail3);

	EFSYS_ASSERT(locked == B_FALSE);
	EFSYS_LOCK(enp->en_eslp, state);

	ef10_filter_set_entry_not_busy(eftp, ins_index);

	EFSYS_UNLOCK(enp->en_eslp, state);

fail2:
	EFSYS_PROBE(fail2);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	if (locked)
		EFSYS_UNLOCK(enp->en_eslp, state);

	return (rc);
}

	__checkReturn	efx_rc_t
ef10_filter_add(
	__in		efx_nic_t *enp,
	__inout		efx_filter_spec_t *spec,
	__in		enum efx_filter_replacement_policy_e policy)
{
	efx_rc_t rc;

	rc = ef10_filter_add_internal(enp, spec, policy, NULL);
	if (rc != 0)
		goto fail1;

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

/*
 * Delete a filter by index from the filter table with priority
 * that is not higher than specified.
 */
static	__checkReturn	efx_rc_t
ef10_filter_delete_internal(
	__in		efx_nic_t *enp,
	__in		uint32_t filter_id,
	__in		efx_filter_priority_t priority)
{
	efx_rc_t rc;
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	efx_filter_spec_t *spec;
	efsys_lock_state_t state;
	uint32_t filter_idx = filter_id % EFX_EF10_FILTER_TBL_ROWS;

	/*
	 * Find the software table entry and mark it busy.  Don't
	 * remove it yet; any attempt to update while we're waiting
	 * for the firmware must find the busy entry.
	 *
	 * FIXME: What if the busy flag is never cleared?
	 */
	EFSYS_LOCK(enp->en_eslp, state);
	while (ef10_filter_entry_is_busy(table, filter_idx)) {
		EFSYS_UNLOCK(enp->en_eslp, state);
		EFSYS_SPIN(1);
		EFSYS_LOCK(enp->en_eslp, state);
	}
	if ((spec = ef10_filter_entry_spec(table, filter_idx)) != NULL) {
		if (spec->efs_priority <= priority)
			ef10_filter_set_entry_busy(table, filter_idx);
	}
	EFSYS_UNLOCK(enp->en_eslp, state);

	if (spec == NULL) {
		rc = ENOENT;
		goto fail1;
	}

	if (spec->efs_priority > priority) {
		/*
		 * Applied filter stays, but overridden filter is removed since
		 * next user request to delete the applied filter should not
		 * restore outdated filter.
		 */
		if (spec->efs_overridden_spec != NULL) {
			EFSYS_ASSERT(spec->efs_overridden_spec->efs_overridden_spec ==
			    NULL);
			EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec),
			    spec->efs_overridden_spec);
			spec->efs_overridden_spec = NULL;
		}
	} else {
		/*
		 * Try to remove the hardware filter or replace it with the
		 * saved automatic filter. This may fail if the MC has
		 * rebooted (which frees all hardware filter resources).
		 */
		if (spec->efs_overridden_spec != NULL) {
			rc = efx_mcdi_filter_op_add(enp,
			    spec->efs_overridden_spec,
			    MC_CMD_FILTER_OP_IN_OP_REPLACE,
			    &table->eft_entry[filter_idx].efe_handle);
		} else if (ef10_filter_is_exclusive(spec)) {
			rc = efx_mcdi_filter_op_delete(enp,
			    MC_CMD_FILTER_OP_IN_OP_REMOVE,
			    &table->eft_entry[filter_idx].efe_handle);
		} else {
			rc = efx_mcdi_filter_op_delete(enp,
			    MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE,
			    &table->eft_entry[filter_idx].efe_handle);
		}

		/* Free the software table entry */
		EFSYS_LOCK(enp->en_eslp, state);
		ef10_filter_set_entry_not_busy(table, filter_idx);
		ef10_filter_set_entry(table, filter_idx,
		    spec->efs_overridden_spec);
		EFSYS_UNLOCK(enp->en_eslp, state);

		EFSYS_KMEM_FREE(enp->en_esip, sizeof (*spec), spec);

		/* Check result of hardware filter removal */
		if (rc != 0)
			goto fail2;
	}

	return (0);

fail2:
	EFSYS_PROBE(fail2);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

static			void
ef10_filter_delete_auto(
	__in		efx_nic_t *enp,
	__in		uint32_t filter_id)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	uint32_t filter_idx = filter_id % EFX_EF10_FILTER_TBL_ROWS;

	/*
	 * AUTO_OLD flag is cleared since the auto filter that is to be removed
	 * may not be the filter at the specified index itself, but the filter
	 * that is overridden by it.
	 */
	ef10_filter_set_entry_not_auto_old(table, filter_idx);

	(void) ef10_filter_delete_internal(enp, filter_idx,
	    EFX_FILTER_PRI_AUTO);
}

	__checkReturn	efx_rc_t
ef10_filter_delete(
	__in		efx_nic_t *enp,
	__inout		efx_filter_spec_t *spec)
{
	efx_rc_t rc;
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	efx_filter_spec_t *saved_spec;
	unsigned int hash;
	unsigned int depth;
	unsigned int i;
	efsys_lock_state_t state;
	boolean_t locked = B_FALSE;

	EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));

	hash = ef10_filter_hash(spec);

	EFSYS_LOCK(enp->en_eslp, state);
	locked = B_TRUE;

	depth = 1;
	for (;;) {
		i = (hash + depth) & (EFX_EF10_FILTER_TBL_ROWS - 1);
		saved_spec = ef10_filter_entry_spec(table, i);
		if (saved_spec && ef10_filter_equal(spec, saved_spec) &&
		    ef10_filter_same_dest(spec, saved_spec) &&
		    saved_spec->efs_priority == EFX_FILTER_PRI_MANUAL) {
			break;
		}
		if (depth == EF10_FILTER_SEARCH_LIMIT) {
			rc = ENOENT;
			goto fail1;
		}
		depth++;
	}

	EFSYS_UNLOCK(enp->en_eslp, state);
	locked = B_FALSE;

	rc = ef10_filter_delete_internal(enp, i, EFX_FILTER_PRI_MANUAL);
	if (rc != 0)
		goto fail2;

	return (0);

fail2:
	EFSYS_PROBE(fail2);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	if (locked)
		EFSYS_UNLOCK(enp->en_eslp, state);

	return (rc);
}

static	__checkReturn	efx_rc_t
efx_mcdi_get_parser_disp_info(
	__in				efx_nic_t *enp,
	__out_ecount(buffer_length)	uint32_t *buffer,
	__in				size_t buffer_length,
	__in				boolean_t encap,
	__out				size_t *list_lengthp)
{
	efx_mcdi_req_t req;
	EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_PARSER_DISP_INFO_IN_LEN,
		MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX);
	size_t matches_count;
	size_t list_size;
	efx_rc_t rc;

	req.emr_cmd = MC_CMD_GET_PARSER_DISP_INFO;
	req.emr_in_buf = payload;
	req.emr_in_length = MC_CMD_GET_PARSER_DISP_INFO_IN_LEN;
	req.emr_out_buf = payload;
	req.emr_out_length = MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX;

	MCDI_IN_SET_DWORD(req, GET_PARSER_DISP_INFO_OUT_OP, encap ?
	    MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_ENCAP_RX_MATCHES :
	    MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);

	efx_mcdi_execute(enp, &req);

	if (req.emr_rc != 0) {
		rc = req.emr_rc;
		goto fail1;
	}

	matches_count = MCDI_OUT_DWORD(req,
	    GET_PARSER_DISP_INFO_OUT_NUM_SUPPORTED_MATCHES);

	if (req.emr_out_length_used <
	    MC_CMD_GET_PARSER_DISP_INFO_OUT_LEN(matches_count)) {
		rc = EMSGSIZE;
		goto fail2;
	}

	*list_lengthp = matches_count;

	if (buffer_length < matches_count) {
		rc = ENOSPC;
		goto fail3;
	}

	/*
	 * Check that the elements in the list in the MCDI response are the size
	 * we expect, so we can just copy them directly. Any conversion of the
	 * flags is handled by the caller.
	 */
	EFX_STATIC_ASSERT(sizeof (uint32_t) ==
	    MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_LEN);

	list_size = matches_count *
		MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_LEN;
	memcpy(buffer,
	    MCDI_OUT2(req, uint32_t,
		    GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES),
	    list_size);

	return (0);

fail3:
	EFSYS_PROBE(fail3);
fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

	__checkReturn	efx_rc_t
ef10_filter_supported_filters(
	__in				efx_nic_t *enp,
	__out_ecount(buffer_length)	uint32_t *buffer,
	__in				size_t buffer_length,
	__out				size_t *list_lengthp)
{
	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
	size_t mcdi_list_length;
	size_t mcdi_encap_list_length;
	size_t list_length;
	uint32_t i;
	uint32_t next_buf_idx;
	size_t next_buf_length;
	efx_rc_t rc;
	boolean_t no_space = B_FALSE;
	efx_filter_match_flags_t all_filter_flags =
	    (EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_LOC_HOST |
	    EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_REM_PORT |
	    EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_PORT |
	    EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_INNER_VID |
	    EFX_FILTER_MATCH_OUTER_VID | EFX_FILTER_MATCH_IP_PROTO |
	    EFX_FILTER_MATCH_VNI_OR_VSID |
	    EFX_FILTER_MATCH_IFRM_LOC_MAC |
	    EFX_FILTER_MATCH_IFRM_UNKNOWN_MCAST_DST |
	    EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST |
	    EFX_FILTER_MATCH_ENCAP_TYPE |
	    EFX_FILTER_MATCH_UNKNOWN_MCAST_DST |
	    EFX_FILTER_MATCH_UNKNOWN_UCAST_DST);

	/*
	 * Two calls to MC_CMD_GET_PARSER_DISP_INFO are needed: one to get the
	 * list of supported filters for ordinary packets, and then another to
	 * get the list of supported filters for encapsulated packets. To
	 * distinguish the second list from the first, the
	 * EFX_FILTER_MATCH_ENCAP_TYPE flag is added to each filter for
	 * encapsulated packets.
	 */
	rc = efx_mcdi_get_parser_disp_info(enp, buffer, buffer_length, B_FALSE,
	    &mcdi_list_length);
	if (rc != 0) {
		if (rc == ENOSPC)
			no_space = B_TRUE;
		else
			goto fail1;
	}

	if (no_space) {
		next_buf_idx = 0;
		next_buf_length = 0;
	} else {
		EFSYS_ASSERT(mcdi_list_length <= buffer_length);
		next_buf_idx = mcdi_list_length;
		next_buf_length = buffer_length - mcdi_list_length;
	}

	if (encp->enc_tunnel_encapsulations_supported != 0) {
		rc = efx_mcdi_get_parser_disp_info(enp, &buffer[next_buf_idx],
		    next_buf_length, B_TRUE, &mcdi_encap_list_length);
		if (rc != 0) {
			if (rc == ENOSPC)
				no_space = B_TRUE;
			else
				goto fail2;
		} else {
			for (i = next_buf_idx;
			    i < next_buf_idx + mcdi_encap_list_length; i++)
				buffer[i] |= EFX_FILTER_MATCH_ENCAP_TYPE;
		}
	} else {
		mcdi_encap_list_length = 0;
	}

	if (no_space) {
		*list_lengthp = mcdi_list_length + mcdi_encap_list_length;
		rc = ENOSPC;
		goto fail3;
	}

	/*
	 * The static assertions in ef10_filter_init() ensure that the values of
	 * the EFX_FILTER_MATCH flags match those used by MCDI, so they don't
	 * need to be converted.
	 *
	 * In case support is added to MCDI for additional flags, remove any
	 * matches from the list which include flags we don't support. The order
	 * of the matches is preserved as they are ordered from highest to
	 * lowest priority.
	 */
	EFSYS_ASSERT(mcdi_list_length + mcdi_encap_list_length <=
	    buffer_length);
	list_length = 0;
	for (i = 0; i < mcdi_list_length + mcdi_encap_list_length; i++) {
		if ((buffer[i] & ~all_filter_flags) == 0) {
			buffer[list_length] = buffer[i];
			list_length++;
		}
	}

	*list_lengthp = list_length;

	return (0);

fail3:
	EFSYS_PROBE(fail3);
fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

static	__checkReturn	efx_rc_t
ef10_filter_insert_unicast(
	__in				efx_nic_t *enp,
	__in_ecount(6)			uint8_t const *addr,
	__in				efx_filter_flags_t filter_flags)
{
	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
	efx_filter_spec_t spec;
	efx_rc_t rc;

	/* Insert the filter for the local station address */
	efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
	    filter_flags,
	    eftp->eft_default_rxq);
	rc = efx_filter_spec_set_eth_local(&spec, EFX_FILTER_SPEC_VID_UNSPEC,
	    addr);
	if (rc != 0)
		goto fail1;

	rc = ef10_filter_add_internal(enp, &spec, EFX_FILTER_REPLACEMENT_NEVER,
	    &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
	if (rc != 0)
		goto fail2;

	eftp->eft_unicst_filter_count++;
	EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
		    EFX_EF10_FILTER_UNICAST_FILTERS_MAX);

	return (0);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);
	return (rc);
}

static	__checkReturn	efx_rc_t
ef10_filter_insert_all_unicast(
	__in				efx_nic_t *enp,
	__in				efx_filter_flags_t filter_flags)
{
	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
	efx_filter_spec_t spec;
	efx_rc_t rc;

	/* Insert the unknown unicast filter */
	efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
	    filter_flags,
	    eftp->eft_default_rxq);
	rc = efx_filter_spec_set_uc_def(&spec);
	if (rc != 0)
		goto fail1;
	rc = ef10_filter_add_internal(enp, &spec, EFX_FILTER_REPLACEMENT_NEVER,
	    &eftp->eft_unicst_filter_indexes[eftp->eft_unicst_filter_count]);
	if (rc != 0)
		goto fail2;

	eftp->eft_unicst_filter_count++;
	EFSYS_ASSERT(eftp->eft_unicst_filter_count <=
		    EFX_EF10_FILTER_UNICAST_FILTERS_MAX);

	return (0);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);
	return (rc);
}

static	__checkReturn	efx_rc_t
ef10_filter_insert_multicast_list(
	__in				efx_nic_t *enp,
	__in				boolean_t mulcst,
	__in				boolean_t brdcst,
	__in_ecount(6*count)		uint8_t const *addrs,
	__in				uint32_t count,
	__in				efx_filter_flags_t filter_flags,
	__in				boolean_t rollback)
{
	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
	efx_filter_spec_t spec;
	uint8_t addr[6];
	uint32_t i;
	uint32_t filter_index;
	uint32_t filter_count;
	efx_rc_t rc;

	if (mulcst == B_FALSE)
		count = 0;

	if (count + (brdcst ? 1 : 0) >
	    EFX_ARRAY_SIZE(eftp->eft_mulcst_filter_indexes)) {
		/* Too many MAC addresses */
		rc = EINVAL;
		goto fail1;
	}

	/* Insert/renew multicast address list filters */
	filter_count = 0;
	for (i = 0; i < count; i++) {
		efx_filter_spec_init_rx(&spec,
		    EFX_FILTER_PRI_AUTO,
		    filter_flags,
		    eftp->eft_default_rxq);

		rc = efx_filter_spec_set_eth_local(&spec,
		    EFX_FILTER_SPEC_VID_UNSPEC,
		    &addrs[i * EFX_MAC_ADDR_LEN]);
		if (rc != 0) {
			if (rollback == B_TRUE) {
				/* Only stop upon failure if told to rollback */
				goto rollback;
			} else {
				/*
				 * Don't try to add a filter with a corrupt
				 * specification.
				 */
				continue;
			}
		}

		rc = ef10_filter_add_internal(enp, &spec,
		    EFX_FILTER_REPLACEMENT_NEVER, &filter_index);

		if (rc == 0) {
			eftp->eft_mulcst_filter_indexes[filter_count] =
				filter_index;
			filter_count++;
		} else if (rollback == B_TRUE) {
			/* Only stop upon failure if told to rollback */
			goto rollback;
		}

	}

	if (brdcst == B_TRUE) {
		/* Insert/renew broadcast address filter */
		efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
		    filter_flags,
		    eftp->eft_default_rxq);

		EFX_MAC_BROADCAST_ADDR_SET(addr);
		rc = efx_filter_spec_set_eth_local(&spec,
		    EFX_FILTER_SPEC_VID_UNSPEC, addr);
		if ((rc != 0) && (rollback == B_TRUE)) {
			/* Only stop upon failure if told to rollback */
			goto rollback;
		}

		rc = ef10_filter_add_internal(enp, &spec,
		    EFX_FILTER_REPLACEMENT_NEVER, &filter_index);

		if (rc == 0) {
			eftp->eft_mulcst_filter_indexes[filter_count] =
				filter_index;
			filter_count++;
		} else if (rollback == B_TRUE) {
			/* Only stop upon failure if told to rollback */
			goto rollback;
		}
	}

	eftp->eft_mulcst_filter_count = filter_count;
	eftp->eft_using_all_mulcst = B_FALSE;

	return (0);

rollback:
	/* Remove any filters we have inserted */
	i = filter_count;
	while (i--) {
		ef10_filter_delete_auto(enp,
		    eftp->eft_mulcst_filter_indexes[i]);
	}
	eftp->eft_mulcst_filter_count = 0;

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

static	__checkReturn	efx_rc_t
ef10_filter_insert_all_multicast(
	__in				efx_nic_t *enp,
	__in				efx_filter_flags_t filter_flags)
{
	ef10_filter_table_t *eftp = enp->en_filter.ef_ef10_filter_table;
	efx_filter_spec_t spec;
	efx_rc_t rc;

	/* Insert the unknown multicast filter */
	efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
	    filter_flags,
	    eftp->eft_default_rxq);
	rc = efx_filter_spec_set_mc_def(&spec);
	if (rc != 0)
		goto fail1;

	rc = ef10_filter_add_internal(enp, &spec, EFX_FILTER_REPLACEMENT_NEVER,
	    &eftp->eft_mulcst_filter_indexes[0]);
	if (rc != 0)
		goto fail2;

	eftp->eft_mulcst_filter_count = 1;
	eftp->eft_using_all_mulcst = B_TRUE;

	/*
	 * FIXME: If brdcst == B_FALSE, add a filter to drop broadcast traffic.
	 */

	return (0);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

typedef struct ef10_filter_encap_entry_s {
	uint16_t		ether_type;
	efx_tunnel_protocol_t	encap_type;
	uint32_t		inner_frame_match;
} ef10_filter_encap_entry_t;

#define	EF10_ENCAP_FILTER_ENTRY(ipv, encap_type, inner_frame_match)	\
	{ EFX_ETHER_TYPE_##ipv, EFX_TUNNEL_PROTOCOL_##encap_type,	\
	    EFX_FILTER_INNER_FRAME_MATCH_UNKNOWN_##inner_frame_match }

static ef10_filter_encap_entry_t ef10_filter_encap_list[] = {
	EF10_ENCAP_FILTER_ENTRY(IPV4, VXLAN, UCAST_DST),
	EF10_ENCAP_FILTER_ENTRY(IPV4, VXLAN, MCAST_DST),
	EF10_ENCAP_FILTER_ENTRY(IPV6, VXLAN, UCAST_DST),
	EF10_ENCAP_FILTER_ENTRY(IPV6, VXLAN, MCAST_DST),

	EF10_ENCAP_FILTER_ENTRY(IPV4, GENEVE, UCAST_DST),
	EF10_ENCAP_FILTER_ENTRY(IPV4, GENEVE, MCAST_DST),
	EF10_ENCAP_FILTER_ENTRY(IPV6, GENEVE, UCAST_DST),
	EF10_ENCAP_FILTER_ENTRY(IPV6, GENEVE, MCAST_DST),

	EF10_ENCAP_FILTER_ENTRY(IPV4, NVGRE, UCAST_DST),
	EF10_ENCAP_FILTER_ENTRY(IPV4, NVGRE, MCAST_DST),
	EF10_ENCAP_FILTER_ENTRY(IPV6, NVGRE, UCAST_DST),
	EF10_ENCAP_FILTER_ENTRY(IPV6, NVGRE, MCAST_DST),
};

#undef EF10_ENCAP_FILTER_ENTRY

static	__checkReturn	efx_rc_t
ef10_filter_insert_encap_filters(
	__in		efx_nic_t *enp,
	__in		boolean_t mulcst,
	__in		efx_filter_flags_t filter_flags)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	uint32_t i;
	efx_rc_t rc;

	EFX_STATIC_ASSERT(EFX_ARRAY_SIZE(ef10_filter_encap_list) <=
			    EFX_ARRAY_SIZE(table->eft_encap_filter_indexes));

	/*
	 * On Medford, full-featured firmware can identify packets as being
	 * tunnel encapsulated, even if no encapsulated packet offloads are in
	 * use. When packets are identified as such, ordinary filters are not
	 * applied, only ones specific to encapsulated packets. Hence we need to
	 * insert filters for encapsulated packets in order to receive them.
	 *
	 * Separate filters need to be inserted for each ether type,
	 * encapsulation type, and inner frame type (unicast or multicast). To
	 * keep things simple and reduce the number of filters needed, catch-all
	 * filters for all combinations of types are inserted, even if
	 * all_unicst or all_mulcst have not been set. (These catch-all filters
	 * may well, however, fail to insert on unprivileged functions.)
	 */
	table->eft_encap_filter_count = 0;
	for (i = 0; i < EFX_ARRAY_SIZE(ef10_filter_encap_list); i++) {
		efx_filter_spec_t spec;
		ef10_filter_encap_entry_t *encap_filter =
			&ef10_filter_encap_list[i];

		/*
		 * Skip multicast filters if we've not been asked for
		 * any multicast traffic.
		 */
		if ((mulcst == B_FALSE) &&
		    (encap_filter->inner_frame_match ==
		    EFX_FILTER_INNER_FRAME_MATCH_UNKNOWN_MCAST_DST))
			continue;

		efx_filter_spec_init_rx(&spec, EFX_FILTER_PRI_AUTO,
					filter_flags,
					table->eft_default_rxq);
		efx_filter_spec_set_ether_type(&spec, encap_filter->ether_type);
		rc = efx_filter_spec_set_encap_type(&spec,
					    encap_filter->encap_type,
					    encap_filter->inner_frame_match);
		if (rc != 0)
			goto fail1;

		rc = ef10_filter_add_internal(enp, &spec,
		    EFX_FILTER_REPLACEMENT_NEVER,
		    &table->eft_encap_filter_indexes[
				    table->eft_encap_filter_count]);
		if (rc != 0) {
			if (rc != EACCES)
				goto fail2;
		} else {
			table->eft_encap_filter_count++;
		}
	}

	return (0);

fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

static			void
ef10_filter_remove_old(
	__in		efx_nic_t *enp)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	uint32_t i;

	for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
		if (ef10_filter_entry_is_auto_old(table, i)) {
			ef10_filter_delete_auto(enp, i);
		}
	}
}


static	__checkReturn	efx_rc_t
ef10_filter_get_workarounds(
	__in				efx_nic_t *enp)
{
	efx_nic_cfg_t *encp = &enp->en_nic_cfg;
	uint32_t implemented = 0;
	uint32_t enabled = 0;
	efx_rc_t rc;

	rc = efx_mcdi_get_workarounds(enp, &implemented, &enabled);
	if (rc == 0) {
		/* Check if chained multicast filter support is enabled */
		if (implemented & enabled & MC_CMD_GET_WORKAROUNDS_OUT_BUG26807)
			encp->enc_bug26807_workaround = B_TRUE;
		else
			encp->enc_bug26807_workaround = B_FALSE;
	} else if (rc == ENOTSUP) {
		/*
		 * Firmware is too old to support GET_WORKAROUNDS, and support
		 * for this workaround was implemented later.
		 */
		encp->enc_bug26807_workaround = B_FALSE;
	} else {
		goto fail1;
	}

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);

}

static			void
ef10_filter_remove_all_existing_filters(
	__in				efx_nic_t *enp)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	efx_port_t *epp = &(enp->en_port);
	unsigned int i;

	for (i = 0; i < table->eft_unicst_filter_count; i++) {
		ef10_filter_delete_auto(enp,
				table->eft_unicst_filter_indexes[i]);
	}
	table->eft_unicst_filter_count = 0;

	for (i = 0; i < table->eft_mulcst_filter_count; i++) {
		ef10_filter_delete_auto(enp,
				table->eft_mulcst_filter_indexes[i]);
	}
	table->eft_mulcst_filter_count = 0;

	for (i = 0; i < table->eft_encap_filter_count; i++) {
		ef10_filter_delete_auto(enp,
				table->eft_encap_filter_indexes[i]);
	}
	table->eft_encap_filter_count = 0;

	epp->ep_all_unicst_inserted = B_FALSE;
	epp->ep_all_mulcst_inserted = B_FALSE;
}

static			void
ef10_filter_mark_old_filters(
	__in				efx_nic_t *enp)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	unsigned int i;

	for (i = 0; i < table->eft_unicst_filter_count; i++) {
		ef10_filter_set_entry_auto_old(table,
					table->eft_unicst_filter_indexes[i]);
	}
	for (i = 0; i < table->eft_mulcst_filter_count; i++) {
		ef10_filter_set_entry_auto_old(table,
					table->eft_mulcst_filter_indexes[i]);
	}
	for (i = 0; i < table->eft_encap_filter_count; i++) {
		ef10_filter_set_entry_auto_old(table,
					table->eft_encap_filter_indexes[i]);
	}
}

static	__checkReturn	efx_rc_t
ef10_filter_insert_renew_unicst_filters(
	__in				efx_nic_t *enp,
	__in_ecount(6)			uint8_t const *mac_addr,
	__in				boolean_t all_unicst,
	__in				efx_filter_flags_t filter_flags,
	__out				boolean_t *all_unicst_inserted)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	efx_port_t *epp = &(enp->en_port);
	efx_rc_t rc;

	/*
	 * Firmware does not perform chaining on unicast filters. As traffic is
	 * therefore only delivered to the first matching filter, we should
	 * always insert the specific filter for our MAC address, to try and
	 * ensure we get that traffic.
	 *
	 * (If the filter for our MAC address has already been inserted by
	 * another function, we won't receive traffic sent to us, even if we
	 * insert a unicast mismatch filter. To prevent traffic stealing, this
	 * therefore relies on the privilege model only allowing functions to
	 * insert filters for their own MAC address unless explicitly given
	 * additional privileges by the user. This also means that, even on a
	 * privileged function, inserting a unicast mismatch filter may not
	 * catch all traffic in multi PCI function scenarios.)
	 */
	table->eft_unicst_filter_count = 0;
	rc = ef10_filter_insert_unicast(enp, mac_addr, filter_flags);
	*all_unicst_inserted = B_FALSE;
	if (all_unicst || (rc != 0)) {
		efx_rc_t all_unicst_rc;

		all_unicst_rc = ef10_filter_insert_all_unicast(enp,
						    filter_flags);
		if (all_unicst_rc == 0) {
			*all_unicst_inserted = B_TRUE;
			epp->ep_all_unicst_inserted = B_TRUE;
		} else if (rc != 0)
			goto fail1;
	}

	return (0);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

static	__checkReturn	efx_rc_t
ef10_filter_insert_renew_mulcst_filters(
	__in				efx_nic_t *enp,
	__in				boolean_t mulcst,
	__in				boolean_t all_mulcst,
	__in				boolean_t brdcst,
	__in_ecount(6*count)		uint8_t const *addrs,
	__in				uint32_t count,
	__in				efx_filter_flags_t filter_flags,
	__in				boolean_t all_unicst_inserted,
	__out				boolean_t *all_mulcst_inserted)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	efx_nic_cfg_t *encp = &enp->en_nic_cfg;
	efx_port_t *epp = &(enp->en_port);
	efx_rc_t rc;

	*all_mulcst_inserted = B_FALSE;

	if (all_mulcst == B_TRUE) {
		efx_rc_t all_mulcst_rc;

		/*
		 * Insert the all multicast filter. If that fails, try to insert
		 * all of our multicast filters (but without rollback on
		 * failure).
		 */
		all_mulcst_rc = ef10_filter_insert_all_multicast(enp,
							    filter_flags);
		if (all_mulcst_rc == 0) {
			epp->ep_all_mulcst_inserted = B_TRUE;
			*all_mulcst_inserted = B_TRUE;
		} else {
			rc = ef10_filter_insert_multicast_list(enp, B_TRUE,
			    brdcst, addrs, count, filter_flags, B_FALSE);
			if (rc != 0)
				goto fail1;
		}
	} else {
		/*
		 * Insert filters for multicast addresses.
		 * If any insertion fails, then rollback and try to insert the
		 * all multicast filter instead.
		 * If that also fails, try to insert all of the multicast
		 * filters (but without rollback on failure).
		 */
		rc = ef10_filter_insert_multicast_list(enp, mulcst, brdcst,
			    addrs, count, filter_flags, B_TRUE);
		if (rc != 0) {
			if ((table->eft_using_all_mulcst == B_FALSE) &&
			    (encp->enc_bug26807_workaround == B_TRUE)) {
				/*
				 * Multicast filter chaining is on, so remove
				 * old filters before inserting the multicast
				 * all filter to avoid duplicate delivery caused
				 * by packets matching multiple filters.
				 */
				ef10_filter_remove_old(enp);
				if (all_unicst_inserted == B_FALSE)
					epp->ep_all_unicst_inserted = B_FALSE;
				if (*all_mulcst_inserted == B_FALSE)
					epp->ep_all_mulcst_inserted = B_FALSE;
			}

			rc = ef10_filter_insert_all_multicast(enp,
							    filter_flags);
			if (rc == 0) {
				epp->ep_all_mulcst_inserted = B_TRUE;
				*all_mulcst_inserted = B_TRUE;
			} else {
				rc = ef10_filter_insert_multicast_list(enp,
				    mulcst, brdcst,
				    addrs, count, filter_flags, B_FALSE);
				if (rc != 0)
					goto fail2;
			}
		}
	}

	return (0);

fail2:
	EFSYS_PROBE1(fail2, efx_rc_t, rc);

fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	return (rc);
}

/*
 * Reconfigure all filters.
 * If all_unicst and/or all mulcst filters cannot be applied then
 * return ENOTSUP (Note the filters for the specified addresses are
 * still applied in this case).
 */
	__checkReturn	efx_rc_t
ef10_filter_reconfigure(
	__in				efx_nic_t *enp,
	__in_ecount(6)			uint8_t const *mac_addr,
	__in				boolean_t all_unicst,
	__in				boolean_t mulcst,
	__in				boolean_t all_mulcst,
	__in				boolean_t brdcst,
	__in_ecount(6*count)		uint8_t const *addrs,
	__in				uint32_t count)
{
	efx_nic_cfg_t *encp = &enp->en_nic_cfg;
	efx_port_t *epp = &(enp->en_port);
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;
	efx_filter_flags_t filter_flags;
	unsigned int i;
	boolean_t all_unicst_inserted = B_FALSE;
	boolean_t all_mulcst_inserted = B_FALSE;
	efx_rc_t rc;

	if (table->eft_default_rxq == NULL) {
		/*
		 * Filters direct traffic to the default RXQ, and so cannot be
		 * inserted until it is available. Any currently configured
		 * filters must be removed (ignore errors in case the MC
		 * has rebooted, which removes hardware filters).
		 */
		ef10_filter_remove_all_existing_filters(enp);
		return (0);
	}

	if (table->eft_using_rss)
		filter_flags = EFX_FILTER_FLAG_RX_RSS;
	else
		filter_flags = 0;

	/* Mark old filters which may need to be removed */
	ef10_filter_mark_old_filters(enp);

	/* Insert or renew unicast filters */
	rc = ef10_filter_insert_renew_unicst_filters(enp, mac_addr, all_unicst,
						     filter_flags,
						     &all_unicst_inserted);
	if (rc != 0)
		goto fail1;

	/*
	 * WORKAROUND_BUG26807 controls firmware support for chained multicast
	 * filters, and can only be enabled or disabled when the hardware filter
	 * table is empty.
	 *
	 * Chained multicast filters require support from the datapath firmware,
	 * and may not be available (e.g. low-latency variants or old Huntington
	 * firmware).
	 *
	 * Firmware will reset (FLR) functions which have inserted filters in
	 * the hardware filter table when the workaround is enabled/disabled.
	 * Functions without any hardware filters are not reset.
	 *
	 * Re-check if the workaround is enabled after adding unicast hardware
	 * filters. This ensures that encp->enc_bug26807_workaround matches the
	 * firmware state, and that later changes to enable/disable the
	 * workaround will result in this function seeing a reset (FLR).
	 *
	 * In common-code drivers, we only support multiple PCI function
	 * scenarios with firmware that supports multicast chaining, so we can
	 * assume it is enabled for such cases and hence simplify the filter
	 * insertion logic. Firmware that does not support multicast chaining
	 * does not support multiple PCI function configurations either, so
	 * filter insertion is much simpler and the same strategies can still be
	 * used.
	 */
	if ((rc = ef10_filter_get_workarounds(enp)) != 0)
		goto fail2;

	if ((table->eft_using_all_mulcst != all_mulcst) &&
	    (encp->enc_bug26807_workaround == B_TRUE)) {
		/*
		 * Multicast filter chaining is enabled, so traffic that matches
		 * more than one multicast filter will be replicated and
		 * delivered to multiple recipients.  To avoid this duplicate
		 * delivery, remove old multicast filters before inserting new
		 * multicast filters.
		 */
		ef10_filter_remove_old(enp);
		if (all_unicst_inserted == B_FALSE)
			epp->ep_all_unicst_inserted = B_FALSE;

		epp->ep_all_mulcst_inserted = B_FALSE;
	}

	/* Insert or renew multicast filters */
	rc = ef10_filter_insert_renew_mulcst_filters(enp, mulcst, all_mulcst,
						     brdcst, addrs, count,
						     filter_flags,
						     all_unicst_inserted,
						     &all_mulcst_inserted);
	if (rc != 0)
		goto fail3;

	if (encp->enc_tunnel_encapsulations_supported != 0) {
		/* Try to insert filters for encapsulated packets. */
		(void) ef10_filter_insert_encap_filters(enp,
					    mulcst || all_mulcst || brdcst,
					    filter_flags);
	}

	/* Remove old filters which were not renewed */
	ef10_filter_remove_old(enp);
	if (all_unicst_inserted == B_FALSE)
		epp->ep_all_unicst_inserted = B_FALSE;
	if (all_mulcst_inserted == B_FALSE)
		epp->ep_all_mulcst_inserted = B_FALSE;

	/* report if any optional flags were rejected */
	if (((all_unicst != B_FALSE) && (all_unicst_inserted == B_FALSE)) ||
	    ((all_mulcst != B_FALSE) && (all_mulcst_inserted == B_FALSE))) {
		rc = ENOTSUP;
	}

	return (rc);

fail3:
	EFSYS_PROBE(fail3);
fail2:
	EFSYS_PROBE(fail2);
fail1:
	EFSYS_PROBE1(fail1, efx_rc_t, rc);

	/* Clear auto old flags */
	for (i = 0; i < EFX_ARRAY_SIZE(table->eft_entry); i++) {
		if (ef10_filter_entry_is_auto_old(table, i)) {
			ef10_filter_set_entry_not_auto_old(table, i);
		}
	}

	return (rc);
}

		void
ef10_filter_get_default_rxq(
	__in		efx_nic_t *enp,
	__out		efx_rxq_t **erpp,
	__out		boolean_t *using_rss)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;

	*erpp = table->eft_default_rxq;
	*using_rss = table->eft_using_rss;
}


		void
ef10_filter_default_rxq_set(
	__in		efx_nic_t *enp,
	__in		efx_rxq_t *erp,
	__in		boolean_t using_rss)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;

#if EFSYS_OPT_RX_SCALE
	EFSYS_ASSERT((using_rss == B_FALSE) ||
	    (enp->en_rss_context != EF10_RSS_CONTEXT_INVALID));
	table->eft_using_rss = using_rss;
#else
	EFSYS_ASSERT(using_rss == B_FALSE);
	table->eft_using_rss = B_FALSE;
#endif
	table->eft_default_rxq = erp;
}

		void
ef10_filter_default_rxq_clear(
	__in		efx_nic_t *enp)
{
	ef10_filter_table_t *table = enp->en_filter.ef_ef10_filter_table;

	table->eft_default_rxq = NULL;
	table->eft_using_rss = B_FALSE;
}


#endif /* EFSYS_OPT_FILTER */

#endif /* EFX_OPTS_EF10() */