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
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
|
/* $OpenBSD: if_oce.c,v 1.40 2012/11/03 00:23:25 mikeb Exp $ */
/*
* Copyright (c) 2012 Mike Belopuhov
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*-
* Copyright (C) 2012 Emulex
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Emulex Corporation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Contact Information:
* freebsd-drivers@emulex.com
*
* Emulex
* 3333 Susan Street
* Costa Mesa, CA 92626
*/
#include "bpfilter.h"
#include "vlan.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/socket.h>
#include <sys/timeout.h>
#include <sys/pool.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#endif
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#if NVLAN > 0
#include <net/if_types.h>
#include <net/if_vlan_var.h>
#endif
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/if_ocereg.h>
#include <dev/pci/if_ocevar.h>
int oce_probe(struct device *parent, void *match, void *aux);
void oce_attach(struct device *parent, struct device *self, void *aux);
void oce_attachhook(void *arg);
void oce_attach_ifp(struct oce_softc *sc);
int oce_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
void oce_init(void *xsc);
void oce_stop(struct oce_softc *sc);
void oce_iff(struct oce_softc *sc);
int oce_pci_alloc(struct oce_softc *sc);
int oce_intr(void *arg);
int oce_alloc_intr(struct oce_softc *sc);
void oce_intr_enable(struct oce_softc *sc);
void oce_intr_disable(struct oce_softc *sc);
void oce_media_status(struct ifnet *ifp, struct ifmediareq *ifmr);
int oce_media_change(struct ifnet *ifp);
void oce_update_link_status(struct oce_softc *sc);
void oce_link_event(struct oce_softc *sc,
struct oce_async_cqe_link_state *acqe);
int oce_get_buf(struct oce_rq *rq);
int oce_alloc_rx_bufs(struct oce_rq *rq);
void oce_refill_rx(void *arg);
void oce_watchdog(struct ifnet *ifp);
void oce_start(struct ifnet *ifp);
int oce_encap(struct oce_softc *sc, struct mbuf **mpp, int wq_index);
void oce_txeof(struct oce_wq *wq);
void oce_discard_rx_comp(struct oce_rq *rq, struct oce_nic_rx_cqe *cqe);
int oce_cqe_vtp_valid(struct oce_softc *sc, struct oce_nic_rx_cqe *cqe);
int oce_cqe_portid_valid(struct oce_softc *sc, struct oce_nic_rx_cqe *cqe);
void oce_rxeof(struct oce_rq *rq, struct oce_nic_rx_cqe *cqe);
int oce_start_rq(struct oce_rq *rq);
void oce_stop_rq(struct oce_rq *rq);
void oce_free_posted_rxbuf(struct oce_rq *rq);
int oce_vid_config(struct oce_softc *sc);
void oce_set_macaddr(struct oce_softc *sc);
void oce_local_timer(void *arg);
#if defined(INET6) || defined(INET)
#ifdef OCE_LRO
int oce_init_lro(struct oce_softc *sc);
void oce_free_lro(struct oce_softc *sc);
void oce_rx_flush_lro(struct oce_rq *rq);
#endif
#ifdef OCE_TSO
struct mbuf * oce_tso_setup(struct oce_softc *sc, struct mbuf **mpp);
#endif
#endif
void oce_intr_mq(void *arg);
void oce_intr_wq(void *arg);
void oce_intr_rq(void *arg);
int oce_init_queues(struct oce_softc *sc);
void oce_release_queues(struct oce_softc *sc);
struct oce_wq *oce_create_wq(struct oce_softc *sc, struct oce_eq *eq,
uint32_t q_len);
void oce_drain_wq(struct oce_wq *wq);
void oce_destroy_wq(struct oce_wq *wq);
struct oce_rq *oce_create_rq(struct oce_softc *sc, struct oce_eq *eq,
uint32_t if_id, uint32_t q_len, uint32_t frag_size, uint32_t rss);
void oce_drain_rq(struct oce_rq *rq);
void oce_destroy_rq(struct oce_rq *rq);
struct oce_eq *oce_create_eq(struct oce_softc *sc);
static inline void oce_arm_eq(struct oce_eq *eq, int neqe, int rearm,
int clearint);
void oce_drain_eq(struct oce_eq *eq);
void oce_destroy_eq(struct oce_eq *eq);
struct oce_mq *oce_create_mq(struct oce_softc *sc, struct oce_eq *eq);
void oce_drain_mq(struct oce_mq *mq);
void oce_destroy_mq(struct oce_mq *mq);
struct oce_cq *oce_create_cq(struct oce_softc *sc, struct oce_eq *eq,
uint32_t q_len, uint32_t item_size, uint32_t is_eventable,
uint32_t nodelay, uint32_t ncoalesce);
static inline void oce_arm_cq(struct oce_cq *cq, int ncqe, int rearm);
void oce_destroy_cq(struct oce_cq *cq);
int oce_dma_alloc(struct oce_softc *sc, bus_size_t size,
struct oce_dma_mem *dma);
void oce_dma_free(struct oce_softc *sc, struct oce_dma_mem *dma);
struct oce_ring *oce_create_ring(struct oce_softc *sc, int q_len,
int num_entries, int max_segs);
void oce_destroy_ring(struct oce_softc *sc, struct oce_ring *ring);
int oce_load_ring(struct oce_softc *sc, struct oce_ring *ring,
struct phys_addr *pa_list, int max_segs);
static inline void *oce_ring_get(struct oce_ring *ring);
static inline void *oce_ring_first(struct oce_ring *ring);
static inline void *oce_ring_next(struct oce_ring *ring);
int oce_init_fw(struct oce_softc *sc);
int oce_mbox_init(struct oce_softc *sc);
int oce_mbox_dispatch(struct oce_softc *sc);
int oce_cmd(struct oce_softc *sc, int subsys, int opcode, int version,
void *payload, int length);
void oce_first_mcc(struct oce_softc *sc);
int oce_check_native_mode(struct oce_softc *sc);
int oce_create_iface(struct oce_softc *sc, uint8_t *macaddr);
int oce_config_vlan(struct oce_softc *sc, uint32_t if_id,
struct normal_vlan *vtag_arr, int vtag_cnt, int untagged, int promisc);
int oce_set_flow_control(struct oce_softc *sc, uint32_t flow_control);
int oce_config_rss(struct oce_softc *sc, uint32_t if_id, int enable);
int oce_update_mcast(struct oce_softc *sc,
uint8_t multi[][ETHER_ADDR_LEN], int naddr);
int oce_set_promisc(struct oce_softc *sc, int enable);
int oce_get_link_status(struct oce_softc *sc);
int oce_macaddr_get(struct oce_softc *sc, uint8_t *macaddr);
int oce_macaddr_add(struct oce_softc *sc, uint8_t *macaddr,
uint32_t if_id, uint32_t *pmac_id);
int oce_macaddr_del(struct oce_softc *sc, uint32_t if_id,
uint32_t pmac_id);
int oce_new_rq(struct oce_softc *sc, struct oce_rq *rq);
int oce_new_wq(struct oce_softc *sc, struct oce_wq *wq);
int oce_new_mq(struct oce_softc *sc, struct oce_mq *mq);
int oce_new_eq(struct oce_softc *sc, struct oce_eq *eq);
int oce_new_cq(struct oce_softc *sc, struct oce_cq *cq);
int oce_destroy_queue(struct oce_softc *sc, enum qtype qtype, uint32_t qid);
int oce_update_stats(struct oce_softc *sc, u_int64_t *rxe, u_int64_t *txe);
int oce_stats_be2(struct oce_softc *sc, uint64_t *rxe, uint64_t *txe);
int oce_stats_be3(struct oce_softc *sc, uint64_t *rxe, uint64_t *txe);
int oce_stats_xe(struct oce_softc *sc, uint64_t *rxe, uint64_t *txe);
struct cfdriver oce_cd = {
NULL, "oce", DV_IFNET
};
struct cfattach oce_ca = {
sizeof(struct oce_softc), oce_probe, oce_attach, NULL, NULL
};
const struct pci_matchid oce_devices[] = {
{ PCI_VENDOR_SERVERENGINES, PCI_PRODUCT_SERVERENGINES_BE2 },
{ PCI_VENDOR_SERVERENGINES, PCI_PRODUCT_SERVERENGINES_BE3 },
{ PCI_VENDOR_SERVERENGINES, PCI_PRODUCT_SERVERENGINES_OCBE2 },
{ PCI_VENDOR_SERVERENGINES, PCI_PRODUCT_SERVERENGINES_OCBE3 },
{ PCI_VENDOR_EMULEX, PCI_PRODUCT_EMULEX_XE201 },
};
int
oce_probe(struct device *parent, void *match, void *aux)
{
return (pci_matchbyid(aux, oce_devices, nitems(oce_devices)));
}
void
oce_attach(struct device *parent, struct device *self, void *aux)
{
struct pci_attach_args *pa = (struct pci_attach_args *)aux;
struct oce_softc *sc = (struct oce_softc *)self;
switch (PCI_PRODUCT(pa->pa_id)) {
case PCI_PRODUCT_SERVERENGINES_BE2:
case PCI_PRODUCT_SERVERENGINES_OCBE2:
SET(sc->flags, OCE_F_BE2);
break;
case PCI_PRODUCT_SERVERENGINES_BE3:
case PCI_PRODUCT_SERVERENGINES_OCBE3:
SET(sc->flags, OCE_F_BE3);
break;
case PCI_PRODUCT_EMULEX_XE201:
SET(sc->flags, OCE_F_XE201);
break;
}
sc->pa = *pa;
if (oce_pci_alloc(sc))
return;
sc->rss_enable = 0;
sc->tx_ring_size = OCE_TX_RING_SIZE;
sc->rx_ring_size = OCE_RX_RING_SIZE;
sc->rq_frag_size = OCE_RQ_BUF_SIZE;
sc->flow_control = OCE_FC_TX | OCE_FC_RX;
/* create the bootstrap mailbox */
if (oce_dma_alloc(sc, sizeof(struct oce_bmbx), &sc->bsmbx)) {
printf(": failed to allocate mailbox memory\n");
return;
}
if (oce_init_fw(sc))
goto fail_1;
if (oce_mbox_init(sc)) {
printf(": failed to initialize mailbox\n");
goto fail_1;
}
if (ISSET(sc->flags, OCE_F_BE3)) {
if (oce_check_native_mode(sc))
goto fail_1;
}
if (oce_macaddr_get(sc, sc->macaddr)) {
printf(": failed to fetch MAC address\n");
goto fail_1;
}
bcopy(sc->macaddr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
sc->nrqs = 1;
sc->nwqs = 1;
sc->intr_count = 1;
if (oce_alloc_intr(sc))
goto fail_1;
if (oce_init_queues(sc))
goto fail_1;
oce_attach_ifp(sc);
#ifdef OCE_LRO
if (oce_init_lro(sc))
goto fail_2;
#endif
timeout_set(&sc->timer, oce_local_timer, sc);
timeout_set(&sc->rxrefill, oce_refill_rx, sc);
mountroothook_establish(oce_attachhook, sc);
printf(", address %s\n", ether_sprintf(sc->arpcom.ac_enaddr));
return;
#ifdef OCE_LRO
fail_2:
ether_ifdetach(&sc->arpcom.ac_if);
if_detach(&sc->arpcom.ac_if);
oce_release_queues(sc);
#endif
fail_1:
oce_dma_free(sc, &sc->bsmbx);
}
void
oce_attachhook(void *arg)
{
struct oce_softc *sc = arg;
oce_get_link_status(sc);
oce_arm_cq(sc->mq->cq, 0, TRUE);
/*
* We need to get MCC async events. So enable intrs and arm
* first EQ, Other EQs will be armed after interface is UP
*/
oce_intr_enable(sc);
oce_arm_eq(sc->eq[0], 0, TRUE, FALSE);
/*
* Send first mcc cmd and after that we get gracious
* MCC notifications from FW
*/
oce_first_mcc(sc);
}
void
oce_attach_ifp(struct oce_softc *sc)
{
struct ifnet *ifp = &sc->arpcom.ac_if;
ifmedia_init(&sc->media, IFM_IMASK, oce_media_change, oce_media_status);
ifmedia_add(&sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
ifmedia_set(&sc->media, IFM_ETHER | IFM_AUTO);
strlcpy(ifp->if_xname, sc->dev.dv_xname, IFNAMSIZ);
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_ioctl = oce_ioctl;
ifp->if_start = oce_start;
ifp->if_watchdog = oce_watchdog;
ifp->if_hardmtu = OCE_MAX_MTU;
ifp->if_softc = sc;
IFQ_SET_MAXLEN(&ifp->if_snd, sc->tx_ring_size - 1);
IFQ_SET_READY(&ifp->if_snd);
/* oce splits jumbos into 2k chunks... */
m_clsetwms(ifp, MCLBYTES, 8, sc->rx_ring_size);
ifp->if_capabilities = IFCAP_VLAN_MTU;
#if NVLAN > 0
ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
#endif
#if defined(INET6) || defined(INET)
#ifdef OCE_TSO
ifp->if_capabilities |= IFCAP_TSO;
ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
#endif
#ifdef OCE_LRO
ifp->if_capabilities |= IFCAP_LRO;
#endif
#endif
if_attach(ifp);
ether_ifattach(ifp);
}
int
oce_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
{
struct oce_softc *sc = ifp->if_softc;
struct ifaddr *ifa = (struct ifaddr *)data;
struct ifreq *ifr = (struct ifreq *)data;
int s, error = 0;
s = splnet();
switch (command) {
case SIOCSIFADDR:
ifp->if_flags |= IFF_UP;
if (!(ifp->if_flags & IFF_RUNNING))
oce_init(sc);
#ifdef INET
if (ifa->ifa_addr->sa_family == AF_INET)
arp_ifinit(&sc->arpcom, ifa);
#endif
break;
case SIOCGIFMEDIA:
error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
break;
case SIOCSIFMTU:
if (ifr->ifr_mtu > OCE_MAX_MTU)
error = EINVAL;
else if (ifp->if_mtu != ifr->ifr_mtu) {
ifp->if_mtu = ifr->ifr_mtu;
oce_init(sc);
}
break;
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_UP) {
if (ifp->if_flags & IFF_RUNNING)
error = ENETRESET;
else
oce_init(sc);
} else {
if (ifp->if_flags & IFF_RUNNING)
oce_stop(sc);
}
break;
default:
error = ether_ioctl(ifp, &sc->arpcom, command, data);
break;
}
if (error == ENETRESET) {
if (ifp->if_flags & IFF_RUNNING)
oce_iff(sc);
error = 0;
}
splx(s);
return error;
}
void
oce_iff(struct oce_softc *sc)
{
uint8_t multi[OCE_MAX_MC_FILTER_SIZE][ETHER_ADDR_LEN];
struct ifnet *ifp = &sc->arpcom.ac_if;
struct arpcom *ac = &sc->arpcom;
struct ether_multi *enm;
struct ether_multistep step;
int naddr = 0, promisc = 0;
ifp->if_flags &= ~IFF_ALLMULTI;
if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0 ||
ac->ac_multicnt >= OCE_MAX_MC_FILTER_SIZE) {
ifp->if_flags |= IFF_ALLMULTI;
promisc = 1;
} else {
ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
while (enm != NULL) {
bcopy(enm->enm_addrlo, multi[naddr++], ETHER_ADDR_LEN);
ETHER_NEXT_MULTI(step, enm);
}
oce_update_mcast(sc, multi, naddr);
}
oce_set_promisc(sc, promisc);
}
int
oce_intr(void *arg)
{
struct oce_softc *sc = arg;
struct oce_eq *eq = sc->eq[0];
struct oce_eqe *eqe;
struct oce_cq *cq = NULL;
int i, claimed = 0, neqe = 0;
oce_dma_sync(&eq->ring->dma, BUS_DMASYNC_POSTREAD);
OCE_RING_FOREACH(eq->ring, eqe, eqe->evnt != 0) {
eqe->evnt = 0;
oce_dma_sync(&eq->ring->dma, BUS_DMASYNC_PREWRITE);
neqe++;
}
if (!neqe)
goto eq_arm; /* Spurious */
claimed = 1;
/* Clear EQ entries, but dont arm */
oce_arm_eq(eq, neqe, FALSE, TRUE);
/* Process TX, RX and MCC completion queues */
for (i = 0; i < eq->cq_valid; i++) {
cq = eq->cq[i];
(*cq->cq_intr)(cq->cb_arg);
}
/* Arm all CQs connected to this EQ */
for (i = 0; i < eq->cq_valid; i++) {
cq = eq->cq[i];
oce_arm_cq(cq, 0, TRUE);
}
eq_arm:
oce_arm_eq(eq, 0, TRUE, FALSE);
return (claimed);
}
int
oce_pci_alloc(struct oce_softc *sc)
{
struct pci_attach_args *pa = &sc->pa;
pcireg_t memtype, reg;
/* setup the device config region */
if (ISSET(sc->flags, OCE_F_BE2))
reg = OCE_BAR_CFG_BE2;
else
reg = OCE_BAR_CFG;
memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
if (pci_mapreg_map(pa, reg, memtype, 0, &sc->cfg_iot,
&sc->cfg_ioh, NULL, &sc->cfg_size,
IS_BE(sc) ? 0 : 32768)) {
printf(": can't find cfg mem space\n");
return (ENXIO);
}
/*
* Read the SLI_INTF register and determine whether we
* can use this port and its features
*/
reg = pci_conf_read(pa->pa_pc, pa->pa_tag, OCE_INTF_REG_OFFSET);
if (OCE_SLI_SIGNATURE(reg) != OCE_INTF_VALID_SIG) {
printf(": invalid signature\n");
goto fail_1;
}
if (OCE_SLI_REVISION(reg) != OCE_INTF_SLI_REV4) {
printf(": unsupported SLI revision\n");
goto fail_1;
}
if (OCE_SLI_IFTYPE(reg) == OCE_INTF_IF_TYPE_1)
SET(sc->flags, OCE_F_MBOX_ENDIAN_RQD);
if (OCE_SLI_HINT1(reg) == OCE_INTF_FUNC_RESET_REQD)
SET(sc->flags, OCE_F_RESET_RQD);
/* Lancer has one BAR (CFG) but BE3 has three (CFG, CSR, DB) */
if (IS_BE(sc)) {
/* set up CSR region */
reg = OCE_BAR_CSR;
memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
if (pci_mapreg_map(pa, reg, memtype, 0, &sc->csr_iot,
&sc->csr_ioh, NULL, &sc->csr_size, 0)) {
printf(": can't find csr mem space\n");
goto fail_1;
}
/* set up DB doorbell region */
reg = OCE_BAR_DB;
memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
if (pci_mapreg_map(pa, reg, memtype, 0, &sc->db_iot,
&sc->db_ioh, NULL, &sc->db_size, 0)) {
printf(": can't find csr mem space\n");
goto fail_2;
}
} else {
sc->csr_iot = sc->db_iot = sc->cfg_iot;
sc->csr_ioh = sc->db_ioh = sc->cfg_ioh;
}
return (0);
fail_2:
bus_space_unmap(sc->csr_iot, sc->csr_ioh, sc->csr_size);
fail_1:
bus_space_unmap(sc->cfg_iot, sc->cfg_ioh, sc->cfg_size);
return (ENXIO);
}
static inline uint32_t
oce_read_cfg(struct oce_softc *sc, bus_size_t off)
{
bus_space_barrier(sc->cfg_iot, sc->cfg_ioh, off, 4,
BUS_SPACE_BARRIER_READ);
return (bus_space_read_4(sc->cfg_iot, sc->cfg_ioh, off));
}
static inline uint32_t
oce_read_csr(struct oce_softc *sc, bus_size_t off)
{
bus_space_barrier(sc->csr_iot, sc->csr_ioh, off, 4,
BUS_SPACE_BARRIER_READ);
return (bus_space_read_4(sc->csr_iot, sc->csr_ioh, off));
}
static inline uint32_t
oce_read_db(struct oce_softc *sc, bus_size_t off)
{
bus_space_barrier(sc->db_iot, sc->db_ioh, off, 4,
BUS_SPACE_BARRIER_READ);
return (bus_space_read_4(sc->db_iot, sc->db_ioh, off));
}
static inline void
oce_write_cfg(struct oce_softc *sc, bus_size_t off, uint32_t val)
{
bus_space_write_4(sc->cfg_iot, sc->cfg_ioh, off, val);
bus_space_barrier(sc->cfg_iot, sc->cfg_ioh, off, 4,
BUS_SPACE_BARRIER_WRITE);
}
static inline void
oce_write_csr(struct oce_softc *sc, bus_size_t off, uint32_t val)
{
bus_space_write_4(sc->csr_iot, sc->csr_ioh, off, val);
bus_space_barrier(sc->csr_iot, sc->csr_ioh, off, 4,
BUS_SPACE_BARRIER_WRITE);
}
static inline void
oce_write_db(struct oce_softc *sc, bus_size_t off, uint32_t val)
{
bus_space_write_4(sc->db_iot, sc->db_ioh, off, val);
bus_space_barrier(sc->db_iot, sc->db_ioh, off, 4,
BUS_SPACE_BARRIER_WRITE);
}
int
oce_alloc_intr(struct oce_softc *sc)
{
const char *intrstr = NULL;
struct pci_attach_args *pa = &sc->pa;
pci_intr_handle_t ih;
/* We allocate a single interrupt resource */
if (pci_intr_map_msi(pa, &ih) != 0 &&
pci_intr_map(pa, &ih) != 0) {
printf(": couldn't map interrupt\n");
return (ENXIO);
}
intrstr = pci_intr_string(pa->pa_pc, ih);
if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, oce_intr, sc,
sc->dev.dv_xname) == NULL) {
printf(": couldn't establish interrupt\n");
if (intrstr != NULL)
printf(" at %s", intrstr);
printf("\n");
return (ENXIO);
}
printf(": %s", intrstr);
return (0);
}
void
oce_intr_enable(struct oce_softc *sc)
{
uint32_t reg;
reg = oce_read_cfg(sc, PCI_INTR_CTRL);
oce_write_cfg(sc, PCI_INTR_CTRL, reg | HOSTINTR_MASK);
}
void
oce_intr_disable(struct oce_softc *sc)
{
uint32_t reg;
reg = oce_read_cfg(sc, PCI_INTR_CTRL);
oce_write_cfg(sc, PCI_INTR_CTRL, reg & ~HOSTINTR_MASK);
}
void
oce_update_link_status(struct oce_softc *sc)
{
struct ifnet *ifp = &sc->arpcom.ac_if;
int speed = 0;
if (sc->link_status) {
if (sc->link_active == 0) {
switch (sc->link_speed) {
case 1: /* 10 Mbps */
speed = 10;
break;
case 2: /* 100 Mbps */
speed = 100;
break;
case 3: /* 1 Gbps */
speed = 1000;
break;
case 4: /* 10 Gbps */
speed = 10000;
break;
}
sc->link_active = 1;
ifp->if_baudrate = speed * 1000000ULL;
}
if (!LINK_STATE_IS_UP(ifp->if_link_state)) {
ifp->if_link_state = LINK_STATE_FULL_DUPLEX;
if_link_state_change(ifp);
}
} else {
if (sc->link_active == 1) {
ifp->if_baudrate = 0;
sc->link_active = 0;
}
if (ifp->if_link_state != LINK_STATE_DOWN) {
ifp->if_link_state = LINK_STATE_DOWN;
if_link_state_change(ifp);
}
}
}
void
oce_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
{
struct oce_softc *sc = ifp->if_softc;
ifmr->ifm_status = IFM_AVALID;
ifmr->ifm_active = IFM_ETHER;
if (oce_get_link_status(sc) == 0)
oce_update_link_status(sc);
if (!sc->link_status) {
ifmr->ifm_active |= IFM_NONE;
return;
}
ifmr->ifm_status |= IFM_ACTIVE;
switch (sc->link_speed) {
case 1: /* 10 Mbps */
ifmr->ifm_active |= IFM_10_T | IFM_FDX;
break;
case 2: /* 100 Mbps */
ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
break;
case 3: /* 1 Gbps */
ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
break;
case 4: /* 10 Gbps */
ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
break;
}
if (sc->flow_control & OCE_FC_TX)
ifmr->ifm_active |= IFM_FLOW | IFM_ETH_TXPAUSE;
if (sc->flow_control & OCE_FC_RX)
ifmr->ifm_active |= IFM_FLOW | IFM_ETH_RXPAUSE;
}
int
oce_media_change(struct ifnet *ifp)
{
return (0);
}
int
oce_encap(struct oce_softc *sc, struct mbuf **mpp, int wq_index)
{
struct ifnet *ifp = &sc->arpcom.ac_if;
struct mbuf *m = *mpp;
struct oce_wq *wq = sc->wq[wq_index];
struct oce_packet_desc *pd;
struct oce_nic_hdr_wqe *nichdr;
struct oce_nic_frag_wqe *nicfrag;
int i, nwqe, out, rc;
#ifdef OCE_TSO
if (m->m_pkthdr.csum_flags & CSUM_TSO) {
/* consolidate packet buffers for TSO/LSO segment offload */
#if defined(INET6) || defined(INET)
m = oce_tso_setup(sc, mpp);
#else
m = NULL; /* Huh? */
#endif
if (m == NULL)
goto error;
}
#endif
out = wq->packets_out + 1;
if (out == OCE_WQ_PACKET_ARRAY_SIZE)
out = 0;
if (out == wq->packets_in)
goto error;
pd = &wq->pckts[wq->packets_out];
rc = bus_dmamap_load_mbuf(wq->tag, pd->map, m, BUS_DMA_NOWAIT);
if (rc == EFBIG) {
if (m_defrag(m, M_DONTWAIT) ||
bus_dmamap_load_mbuf(wq->tag, pd->map, m, BUS_DMA_NOWAIT))
goto error;
*mpp = m;
} else if (rc != 0)
goto error;
pd->nsegs = pd->map->dm_nsegs;
nwqe = pd->nsegs + 1;
if (IS_BE(sc)) {
/*Dummy required only for BE3.*/
if (nwqe & 1)
nwqe++;
}
if (nwqe >= RING_NUM_FREE(wq->ring)) {
bus_dmamap_unload(wq->tag, pd->map);
goto error;
}
bus_dmamap_sync(wq->tag, pd->map, 0, pd->map->dm_mapsize,
BUS_DMASYNC_PREWRITE);
pd->mbuf = m;
wq->packets_out = out;
nichdr = oce_ring_get(wq->ring);
nichdr->u0.dw[0] = 0;
nichdr->u0.dw[1] = 0;
nichdr->u0.dw[2] = 0;
nichdr->u0.dw[3] = 0;
nichdr->u0.s.complete = 1;
nichdr->u0.s.event = 1;
nichdr->u0.s.crc = 1;
nichdr->u0.s.forward = 0;
nichdr->u0.s.ipcs = (m->m_pkthdr.csum_flags & M_IPV4_CSUM_OUT) ? 1 : 0;
nichdr->u0.s.udpcs = (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) ? 1 : 0;
nichdr->u0.s.tcpcs = (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) ? 1 : 0;
nichdr->u0.s.num_wqe = nwqe;
nichdr->u0.s.total_length = m->m_pkthdr.len;
#if NVLAN > 0
if (m->m_flags & M_VLANTAG) {
nichdr->u0.s.vlan = 1; /* Vlan present */
nichdr->u0.s.vlan_tag = m->m_pkthdr.ether_vtag;
}
#endif
#ifdef OCE_TSO
if (m->m_pkthdr.csum_flags & CSUM_TSO) {
if (m->m_pkthdr.tso_segsz) {
nichdr->u0.s.lso = 1;
nichdr->u0.s.lso_mss = m->m_pkthdr.tso_segsz;
}
if (!IS_BE(sc))
nichdr->u0.s.ipcs = 1;
}
#endif
oce_dma_sync(&wq->ring->dma, BUS_DMASYNC_PREREAD |
BUS_DMASYNC_PREWRITE);
wq->ring->nused++;
for (i = 0; i < pd->nsegs; i++) {
nicfrag = oce_ring_get(wq->ring);
nicfrag->u0.s.rsvd0 = 0;
nicfrag->u0.s.frag_pa_hi = ADDR_HI(pd->map->dm_segs[i].ds_addr);
nicfrag->u0.s.frag_pa_lo = ADDR_LO(pd->map->dm_segs[i].ds_addr);
nicfrag->u0.s.frag_len = pd->map->dm_segs[i].ds_len;
wq->ring->nused++;
}
if (nwqe > (pd->nsegs + 1)) {
nicfrag = oce_ring_get(wq->ring);
nicfrag->u0.dw[0] = 0;
nicfrag->u0.dw[1] = 0;
nicfrag->u0.dw[2] = 0;
nicfrag->u0.dw[3] = 0;
wq->ring->nused++;
pd->nsegs++;
}
ifp->if_opackets++;
oce_dma_sync(&wq->ring->dma, BUS_DMASYNC_POSTREAD |
BUS_DMASYNC_POSTWRITE);
oce_write_db(sc, PD_TXULP_DB, wq->id | (nwqe << 16));
return (0);
error:
m_freem(*mpp);
*mpp = NULL;
return (1);
}
void
oce_txeof(struct oce_wq *wq)
{
struct oce_softc *sc = (struct oce_softc *) wq->sc;
struct oce_packet_desc *pd;
struct ifnet *ifp = &sc->arpcom.ac_if;
struct mbuf *m;
uint32_t in;
if (wq->packets_out == wq->packets_in)
printf("%s: WQ transmit descriptor missing\n");
in = wq->packets_in + 1;
if (in == OCE_WQ_PACKET_ARRAY_SIZE)
in = 0;
pd = &wq->pckts[wq->packets_in];
wq->packets_in = in;
wq->ring->nused -= (pd->nsegs + 1);
bus_dmamap_sync(wq->tag, pd->map, 0, pd->map->dm_mapsize,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(wq->tag, pd->map);
m = pd->mbuf;
m_freem(m);
pd->mbuf = NULL;
if (ifp->if_flags & IFF_OACTIVE) {
if (wq->ring->nused < (wq->ring->nitems / 2)) {
ifp->if_flags &= ~IFF_OACTIVE;
oce_start(ifp);
}
}
if (wq->ring->nused == 0)
ifp->if_timer = 0;
}
#if OCE_TSO
#if defined(INET6) || defined(INET)
struct mbuf *
oce_tso_setup(struct oce_softc *sc, struct mbuf **mpp)
{
struct mbuf *m;
#ifdef INET
struct ip *ip;
#endif
#ifdef INET6
struct ip6_hdr *ip6;
#endif
struct ether_vlan_header *eh;
struct tcphdr *th;
uint16_t etype;
int total_len = 0, ehdrlen = 0;
m = *mpp;
if (M_WRITABLE(m) == 0) {
m = m_dup(*mpp, M_DONTWAIT);
if (!m)
return NULL;
m_freem(*mpp);
*mpp = m;
}
eh = mtod(m, struct ether_vlan_header *);
if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
etype = ntohs(eh->evl_proto);
ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
} else {
etype = ntohs(eh->evl_encap_proto);
ehdrlen = ETHER_HDR_LEN;
}
switch (etype) {
#ifdef INET
case ETHERTYPE_IP:
ip = (struct ip *)(m->m_data + ehdrlen);
if (ip->ip_p != IPPROTO_TCP)
return NULL;
th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
total_len = ehdrlen + (ip->ip_hl << 2) + (th->th_off << 2);
break;
#endif
#ifdef INET6
case ETHERTYPE_IPV6:
ip6 = (struct ip6_hdr *)(m->m_data + ehdrlen);
if (ip6->ip6_nxt != IPPROTO_TCP)
return NULL;
th = (struct tcphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr));
total_len = ehdrlen + sizeof(struct ip6_hdr) + (th->th_off << 2);
break;
#endif
default:
return NULL;
}
m = m_pullup(m, total_len);
if (!m)
return NULL;
*mpp = m;
return m;
}
#endif /* INET6 || INET */
#endif
void
oce_watchdog(struct ifnet *ifp)
{
printf("%s: watchdog timeout -- resetting\n", ifp->if_xname);
oce_init(ifp->if_softc);
ifp->if_oerrors++;
}
void
oce_start(struct ifnet *ifp)
{
struct oce_softc *sc = ifp->if_softc;
struct mbuf *m;
int pkts = 0;
if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
return;
for (;;) {
IFQ_DEQUEUE(&ifp->if_snd, m);
if (m == NULL)
break;
if (oce_encap(sc, &m, 0)) {
ifp->if_flags |= IFF_OACTIVE;
break;
}
#if NBPFILTER > 0
if (ifp->if_bpf)
bpf_mtap_ether(ifp->if_bpf, m, BPF_DIRECTION_OUT);
#endif
pkts++;
}
/* Set a timeout in case the chip goes out to lunch */
if (pkts)
ifp->if_timer = 5;
}
/* Handle the Completion Queue for transmit */
void
oce_intr_wq(void *arg)
{
struct oce_wq *wq = (struct oce_wq *)arg;
struct oce_cq *cq = wq->cq;
struct oce_nic_tx_cqe *cqe;
int ncqe = 0;
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_POSTREAD);
OCE_RING_FOREACH(cq->ring, cqe, WQ_CQE_VALID(cqe)) {
DW_SWAP((uint32_t *) cqe, sizeof(oce_wq_cqe));
oce_txeof(wq);
WQ_CQE_INVALIDATE(cqe);
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_PREWRITE);
ncqe++;
}
if (ncqe)
oce_arm_cq(cq, ncqe, FALSE);
}
void
oce_rxeof(struct oce_rq *rq, struct oce_nic_rx_cqe *cqe)
{
struct oce_softc *sc = (struct oce_softc *)rq->sc;
struct oce_packet_desc *pd;
struct ifnet *ifp = &sc->arpcom.ac_if;
struct mbuf *m = NULL, *tail = NULL;
int i, len, frag_len;
uint32_t out;
uint16_t vtag;
len = cqe->u0.s.pkt_size;
if (!len) {
/* partial DMA workaround for Lancer */
oce_discard_rx_comp(rq, cqe);
goto exit;
}
/* Get vlan_tag value */
if (IS_BE(sc))
vtag = BSWAP_16(cqe->u0.s.vlan_tag);
else
vtag = cqe->u0.s.vlan_tag;
for (i = 0; i < cqe->u0.s.num_fragments; i++) {
if (rq->packets_out == rq->packets_in) {
printf("%s: RQ transmit descriptor missing\n",
sc->dev.dv_xname);
}
out = rq->packets_out + 1;
if (out == OCE_RQ_PACKET_ARRAY_SIZE)
out = 0;
pd = &rq->pckts[rq->packets_out];
rq->packets_out = out;
bus_dmamap_sync(rq->tag, pd->map, 0, pd->map->dm_mapsize,
BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(rq->tag, pd->map);
rq->pending--;
frag_len = (len > rq->cfg.frag_size) ? rq->cfg.frag_size : len;
pd->mbuf->m_len = frag_len;
if (tail != NULL) {
/* additional fragments */
pd->mbuf->m_flags &= ~M_PKTHDR;
tail->m_next = pd->mbuf;
tail = pd->mbuf;
} else {
/* first fragment, fill out much of the packet header */
pd->mbuf->m_pkthdr.len = len;
pd->mbuf->m_pkthdr.csum_flags = 0;
if (IF_CSUM_ENABLED(ifp)) {
if (cqe->u0.s.ip_cksum_pass) {
if (!cqe->u0.s.ip_ver) { /* IPV4 */
pd->mbuf->m_pkthdr.csum_flags =
M_IPV4_CSUM_IN_OK;
}
}
if (cqe->u0.s.l4_cksum_pass) {
pd->mbuf->m_pkthdr.csum_flags |=
M_TCP_CSUM_IN_OK | M_UDP_CSUM_IN_OK;
}
}
m = tail = pd->mbuf;
}
pd->mbuf = NULL;
len -= frag_len;
}
if (m) {
if (!oce_cqe_portid_valid(sc, cqe)) {
m_freem(m);
goto exit;
}
/* Account for jumbo chains */
m_cluncount(m, 1);
m->m_pkthdr.rcvif = ifp;
#if NVLAN > 0
/* This determines if vlan tag is valid */
if (oce_cqe_vtp_valid(sc, cqe)) {
if (sc->function_mode & FNM_FLEX10_MODE) {
/* FLEX10. If QnQ is not set, neglect VLAN */
if (cqe->u0.s.qnq) {
m->m_pkthdr.ether_vtag = vtag;
m->m_flags |= M_VLANTAG;
}
} else if (sc->pvid != (vtag & VLAN_VID_MASK)) {
/*
* In UMC mode generally pvid will be striped by
* hw. But in some cases we have seen it comes
* with pvid. So if pvid == vlan, neglect vlan.
*/
m->m_pkthdr.ether_vtag = vtag;
m->m_flags |= M_VLANTAG;
}
}
#endif
ifp->if_ipackets++;
#ifdef OCE_LRO
#if defined(INET6) || defined(INET)
/* Try to queue to LRO */
if (IF_LRO_ENABLED(sc) &&
!(m->m_flags & M_VLANTAG) &&
(cqe->u0.s.ip_cksum_pass) &&
(cqe->u0.s.l4_cksum_pass) &&
(!cqe->u0.s.ip_ver) &&
(rq->lro.lro_cnt != 0)) {
if (tcp_lro_rx(&rq->lro, m, 0) == 0) {
rq->lro_pkts_queued ++;
goto exit;
}
/* If LRO posting fails then try to post to STACK */
}
#endif
#endif /* OCE_LRO */
#if NBPFILTER > 0
if (ifp->if_bpf)
bpf_mtap_ether(ifp->if_bpf, m, BPF_DIRECTION_IN);
#endif
ether_input_mbuf(ifp, m);
}
exit:
return;
}
void
oce_discard_rx_comp(struct oce_rq *rq, struct oce_nic_rx_cqe *cqe)
{
uint32_t out, i = 0;
struct oce_packet_desc *pd;
struct oce_softc *sc = (struct oce_softc *) rq->sc;
int num_frags = cqe->u0.s.num_fragments;
if (IS_XE201(sc) && cqe->u0.s.error) {
/*
* Lancer A0 workaround:
* num_frags will be 1 more than actual in case of error
*/
if (num_frags)
num_frags--;
}
for (i = 0; i < num_frags; i++) {
if (rq->packets_out == rq->packets_in) {
printf("%s: RQ transmit descriptor missing\n",
sc->dev.dv_xname);
}
out = rq->packets_out + 1;
if (out == OCE_RQ_PACKET_ARRAY_SIZE)
out = 0;
pd = &rq->pckts[rq->packets_out];
rq->packets_out = out;
bus_dmamap_sync(rq->tag, pd->map, 0, pd->map->dm_mapsize,
BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(rq->tag, pd->map);
rq->pending--;
m_freem(pd->mbuf);
}
}
int
oce_cqe_vtp_valid(struct oce_softc *sc, struct oce_nic_rx_cqe *cqe)
{
struct oce_nic_rx_cqe_v1 *cqe_v1;
if (IS_BE(sc) && ISSET(sc->flags, OCE_F_BE3_NATIVE)) {
cqe_v1 = (struct oce_nic_rx_cqe_v1 *)cqe;
return (cqe_v1->u0.s.vlan_tag_present);
}
return (cqe->u0.s.vlan_tag_present);
}
int
oce_cqe_portid_valid(struct oce_softc *sc, struct oce_nic_rx_cqe *cqe)
{
struct oce_nic_rx_cqe_v1 *cqe_v1;
if (IS_BE(sc) && ISSET(sc->flags, OCE_F_BE3_NATIVE)) {
cqe_v1 = (struct oce_nic_rx_cqe_v1 *)cqe;
if (sc->port_id != cqe_v1->u0.s.port)
return (0);
}
return (1);
}
#ifdef OCE_LRO
#if defined(INET6) || defined(INET)
void
oce_rx_flush_lro(struct oce_rq *rq)
{
struct lro_ctrl *lro = &rq->lro;
struct lro_entry *queued;
struct oce_softc *sc = (struct oce_softc *) rq->sc;
if (!IF_LRO_ENABLED(sc))
return;
while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
SLIST_REMOVE_HEAD(&lro->lro_active, next);
tcp_lro_flush(lro, queued);
}
rq->lro_pkts_queued = 0;
return;
}
int
oce_init_lro(struct oce_softc *sc)
{
struct lro_ctrl *lro = NULL;
int i = 0, rc = 0;
for (i = 0; i < sc->nrqs; i++) {
lro = &sc->rq[i]->lro;
rc = tcp_lro_init(lro);
if (rc != 0) {
printf("%s: LRO init failed\n");
return rc;
}
lro->ifp = &sc->arpcom.ac_if;
}
return rc;
}
void
oce_free_lro(struct oce_softc *sc)
{
struct lro_ctrl *lro = NULL;
int i = 0;
for (i = 0; i < sc->nrqs; i++) {
lro = &sc->rq[i]->lro;
if (lro)
tcp_lro_free(lro);
}
}
#endif /* INET6 || INET */
#endif /* OCE_LRO */
int
oce_get_buf(struct oce_rq *rq)
{
struct oce_softc *sc = (struct oce_softc *)rq->sc;
struct ifnet *ifp = &sc->arpcom.ac_if;
struct oce_packet_desc *pd;
struct oce_nic_rqe *rqe;
int in = rq->packets_in + 1;
if (in == OCE_RQ_PACKET_ARRAY_SIZE)
in = 0;
if (in == rq->packets_out)
return (0); /* no more room */
pd = &rq->pckts[rq->packets_in];
pd->mbuf = MCLGETI(NULL, M_DONTWAIT, ifp, MCLBYTES);
if (pd->mbuf == NULL)
return (0);
pd->mbuf->m_len = pd->mbuf->m_pkthdr.len = MCLBYTES;
if (bus_dmamap_load_mbuf(rq->tag, pd->map, pd->mbuf, BUS_DMA_NOWAIT)) {
m_freem(pd->mbuf);
pd->mbuf = NULL;
return (0);
}
rq->packets_in = in;
bus_dmamap_sync(rq->tag, pd->map, 0, pd->map->dm_mapsize,
BUS_DMASYNC_PREREAD);
oce_dma_sync(&rq->ring->dma, BUS_DMASYNC_PREREAD |
BUS_DMASYNC_PREWRITE);
rqe = oce_ring_get(rq->ring);
rqe->u0.s.frag_pa_hi = ADDR_HI(pd->map->dm_segs[0].ds_addr);
rqe->u0.s.frag_pa_lo = ADDR_LO(pd->map->dm_segs[0].ds_addr);
DW_SWAP(u32ptr(rqe), sizeof(struct oce_nic_rqe));
rq->pending++;
oce_dma_sync(&rq->ring->dma, BUS_DMASYNC_POSTREAD |
BUS_DMASYNC_POSTWRITE);
return (1);
}
int
oce_alloc_rx_bufs(struct oce_rq *rq)
{
struct oce_softc *sc = (struct oce_softc *)rq->sc;
int i, nbufs = 0;
while (oce_get_buf(rq))
nbufs++;
if (!nbufs)
return (0);
for (i = nbufs / OCE_MAX_RQ_POSTS; i > 0; i--) {
oce_write_db(sc, PD_RXULP_DB, rq->id |
(OCE_MAX_RQ_POSTS << 24));
nbufs -= OCE_MAX_RQ_POSTS;
}
if (nbufs > 0)
oce_write_db(sc, PD_RXULP_DB, rq->id | (nbufs << 24));
return (1);
}
void
oce_refill_rx(void *arg)
{
struct oce_softc *sc = arg;
struct oce_rq *rq;
int i, s;
s = splnet();
for_all_rq_queues(sc, rq, i) {
oce_alloc_rx_bufs(rq);
if (!rq->pending)
timeout_add(&sc->rxrefill, 1);
}
splx(s);
}
/* Handle the Completion Queue for receive */
void
oce_intr_rq(void *arg)
{
struct oce_rq *rq = (struct oce_rq *)arg;
struct oce_cq *cq = rq->cq;
struct oce_softc *sc = rq->sc;
struct oce_nic_rx_cqe *cqe;
struct ifnet *ifp = &sc->arpcom.ac_if;
int max_rsp, ncqe = 0, rq_buffers_used = 0;
max_rsp = IS_XE201(sc) ? 8 : OCE_MAX_RSP_HANDLED;
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_POSTREAD);
OCE_RING_FOREACH(cq->ring, cqe, RQ_CQE_VALID(cqe) && ncqe <= max_rsp) {
DW_SWAP((uint32_t *)cqe, sizeof(oce_rq_cqe));
if (cqe->u0.s.error == 0) {
oce_rxeof(rq, cqe);
} else {
ifp->if_ierrors++;
if (IS_XE201(sc))
/* Lancer A0 no buffer workaround */
oce_discard_rx_comp(rq, cqe);
else
/* Post L3/L4 errors to stack.*/
oce_rxeof(rq, cqe);
}
#ifdef OCE_LRO
#if defined(INET6) || defined(INET)
if (IF_LRO_ENABLED(sc) && rq->lro_pkts_queued >= 16)
oce_rx_flush_lro(rq);
#endif
#endif
RQ_CQE_INVALIDATE(cqe);
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_PREWRITE);
ncqe++;
}
#ifdef OCE_LRO
#if defined(INET6) || defined(INET)
if (IF_LRO_ENABLED(sc))
oce_rx_flush_lro(rq);
#endif
#endif
if (ncqe) {
oce_arm_cq(cq, ncqe, FALSE);
rq_buffers_used = OCE_RQ_PACKET_ARRAY_SIZE - rq->pending;
if (rq_buffers_used > 1 && !oce_alloc_rx_bufs(rq))
timeout_add(&sc->rxrefill, 1);
}
}
void
oce_set_macaddr(struct oce_softc *sc)
{
uint32_t old_pmac_id = sc->pmac_id;
int status = 0;
if (!bcmp(sc->macaddr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN))
return;
status = oce_macaddr_add(sc, sc->arpcom.ac_enaddr, sc->if_id,
&sc->pmac_id);
if (!status)
status = oce_macaddr_del(sc, sc->if_id, old_pmac_id);
else
printf("%s: failed to set MAC address\n", sc->dev.dv_xname);
}
void
oce_local_timer(void *arg)
{
struct oce_softc *sc = arg;
struct ifnet *ifp = &sc->arpcom.ac_if;
u_int64_t rxe, txe;
int s;
s = splnet();
if (!(oce_update_stats(sc, &rxe, &txe))) {
ifp->if_ierrors += (rxe > sc->rx_errors) ?
rxe - sc->rx_errors : sc->rx_errors - rxe;
sc->rx_errors = rxe;
ifp->if_oerrors += (txe > sc->tx_errors) ?
txe - sc->tx_errors : sc->tx_errors - txe;
sc->tx_errors = txe;
}
splx(s);
timeout_add_sec(&sc->timer, 1);
}
void
oce_stop(struct oce_softc *sc)
{
struct ifnet *ifp = &sc->arpcom.ac_if;
struct oce_rq *rq;
struct oce_wq *wq;
struct oce_eq *eq;
int i;
timeout_del(&sc->timer);
ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
/* Stop intrs and finish any bottom halves pending */
oce_intr_disable(sc);
/* Invalidate any pending cq and eq entries */
for_all_eq_queues(sc, eq, i)
oce_drain_eq(eq);
for_all_rq_queues(sc, rq, i) {
oce_destroy_queue(sc, QTYPE_RQ, rq->id);
oce_free_posted_rxbuf(rq);
oce_drain_rq(rq);
}
for_all_wq_queues(sc, wq, i)
oce_drain_wq(wq);
}
void
oce_init(void *arg)
{
struct oce_softc *sc = arg;
struct ifnet *ifp = &sc->arpcom.ac_if;
struct oce_eq *eq;
struct oce_rq *rq;
struct oce_wq *wq;
int i;
oce_stop(sc);
DELAY(10);
oce_set_macaddr(sc);
oce_iff(sc);
for_all_rq_queues(sc, rq, i) {
rq->cfg.mtu = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
ETHER_VLAN_ENCAP_LEN;
if (oce_new_rq(sc, rq)) {
printf("%s: failed to create rq\n", sc->dev.dv_xname);
goto error;
}
rq->pending = 0;
rq->ring->index = 0;
rq->packets_in = 0;
rq->packets_out = 0;
if (!oce_alloc_rx_bufs(rq)) {
printf("%s: failed to allocate rx buffers\n",
sc->dev.dv_xname);
goto error;
}
}
#ifdef OCE_RSS
/* RSS config */
if (sc->rss_enable) {
if (oce_config_rss(sc, (uint8_t)sc->if_id, 1)) {
printf("%s: failed to configure RSS\n",
sc->dev.dv_xname);
goto error;
}
}
#endif
for_all_rq_queues(sc, rq, i)
oce_arm_cq(rq->cq, 0, TRUE);
for_all_wq_queues(sc, wq, i)
oce_arm_cq(wq->cq, 0, TRUE);
oce_arm_cq(sc->mq->cq, 0, TRUE);
for_all_eq_queues(sc, eq, i)
oce_arm_eq(eq, 0, TRUE, FALSE);
if (oce_get_link_status(sc) == 0)
oce_update_link_status(sc);
ifp->if_flags |= IFF_RUNNING;
ifp->if_flags &= ~IFF_OACTIVE;
timeout_add_sec(&sc->timer, 1);
oce_intr_enable(sc);
return;
error:
oce_stop(sc);
}
void
oce_link_event(struct oce_softc *sc, struct oce_async_cqe_link_state *acqe)
{
/* Update Link status */
if ((acqe->u0.s.link_status & ~ASYNC_EVENT_LOGICAL) ==
ASYNC_EVENT_LINK_UP)
sc->link_status = ASYNC_EVENT_LINK_UP;
else
sc->link_status = ASYNC_EVENT_LINK_DOWN;
/* Update speed */
sc->link_speed = acqe->u0.s.speed;
sc->qos_link_speed = (uint32_t) acqe->u0.s.qos_link_speed * 10;
oce_update_link_status(sc);
}
/* Handle the Completion Queue for the Mailbox/Async notifications */
void
oce_intr_mq(void *arg)
{
struct oce_mq *mq = (struct oce_mq *)arg;
struct oce_softc *sc = mq->sc;
struct oce_cq *cq = mq->cq;
struct oce_mq_cqe *cqe;
struct oce_async_cqe_link_state *acqe;
struct oce_async_event_grp5_pvid_state *gcqe;
int evt_type, optype, ncqe = 0;
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_POSTREAD);
OCE_RING_FOREACH(cq->ring, cqe, MQ_CQE_VALID(cqe)) {
DW_SWAP((uint32_t *) cqe, sizeof(oce_mq_cqe));
if (cqe->u0.s.async_event) {
evt_type = cqe->u0.s.event_type;
optype = cqe->u0.s.async_type;
if (evt_type == ASYNC_EVENT_CODE_LINK_STATE) {
/* Link status evt */
acqe = (struct oce_async_cqe_link_state *)cqe;
oce_link_event(sc, acqe);
} else if ((evt_type == ASYNC_EVENT_GRP5) &&
(optype == ASYNC_EVENT_PVID_STATE)) {
/* GRP5 PVID */
gcqe =
(struct oce_async_event_grp5_pvid_state *)cqe;
if (gcqe->enabled)
sc->pvid = gcqe->tag & VLAN_VID_MASK;
else
sc->pvid = 0;
}
}
MQ_CQE_INVALIDATE(cqe);
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_PREWRITE);
ncqe++;
}
if (ncqe)
oce_arm_cq(cq, ncqe, FALSE /* TRUE */);
}
int
oce_init_queues(struct oce_softc *sc)
{
struct oce_wq *wq;
struct oce_rq *rq;
int i;
/* Create network interface on card */
if (oce_create_iface(sc, sc->macaddr))
goto error;
/* create all of the event queues */
for (i = 0; i < sc->intr_count; i++) {
sc->eq[i] = oce_create_eq(sc);
if (!sc->eq[i])
goto error;
}
/* alloc tx queues */
for_all_wq_queues(sc, wq, i) {
sc->wq[i] = oce_create_wq(sc, sc->eq[i], sc->tx_ring_size);
if (!sc->wq[i])
goto error;
}
/* alloc rx queues */
for_all_rq_queues(sc, rq, i) {
sc->rq[i] = oce_create_rq(sc, sc->eq[i == 0 ? 0 : i - 1],
sc->if_id, sc->rx_ring_size, sc->rq_frag_size,
(i == 0) ? 0 : sc->rss_enable);
if (!sc->rq[i])
goto error;
}
/* alloc mailbox queue */
sc->mq = oce_create_mq(sc, sc->eq[0]);
if (!sc->mq)
goto error;
return (0);
error:
oce_release_queues(sc);
return (1);
}
void
oce_release_queues(struct oce_softc *sc)
{
int i = 0;
struct oce_wq *wq;
struct oce_rq *rq;
struct oce_eq *eq;
for_all_rq_queues(sc, rq, i) {
if (rq)
oce_destroy_rq(sc->rq[i]);
}
for_all_wq_queues(sc, wq, i) {
if (wq)
oce_destroy_wq(sc->wq[i]);
}
if (sc->mq)
oce_destroy_mq(sc->mq);
for_all_eq_queues(sc, eq, i) {
if (eq)
oce_destroy_eq(sc->eq[i]);
}
}
/**
* @brief Function to create a WQ for NIC Tx
* @param sc software handle to the device
* @param qlen number of entries in the queue
* @returns the pointer to the WQ created or NULL on failure
*/
struct oce_wq *
oce_create_wq(struct oce_softc *sc, struct oce_eq *eq, uint32_t q_len)
{
struct oce_wq *wq;
struct oce_cq *cq;
int i;
if (q_len < 256 || q_len > 2048)
return (NULL);
wq = malloc(sizeof(struct oce_wq), M_DEVBUF, M_NOWAIT | M_ZERO);
if (!wq)
return (NULL);
wq->ring = oce_create_ring(sc, q_len, NIC_WQE_SIZE, 8);
if (!wq->ring) {
free(wq, M_DEVBUF);
return (NULL);
}
cq = oce_create_cq(sc, eq, CQ_LEN_512, sizeof(struct oce_nic_tx_cqe),
1, 0, 3);
if (!cq) {
oce_destroy_ring(sc, wq->ring);
free(wq, M_DEVBUF);
return (NULL);
}
wq->id = -1;
wq->sc = sc;
wq->tag = sc->pa.pa_dmat;
wq->cq = cq;
wq->cfg.q_len = q_len;
wq->cfg.wq_type = NIC_WQ_TYPE_STANDARD;
wq->cfg.eqd = OCE_DEFAULT_EQD;
wq->cfg.nbufs = 2 * wq->cfg.q_len;
for (i = 0; i < OCE_WQ_PACKET_ARRAY_SIZE; i++) {
if (bus_dmamap_create(wq->tag, OCE_MAX_TX_SIZE,
OCE_MAX_TX_ELEMENTS, PAGE_SIZE, 0, BUS_DMA_NOWAIT,
&wq->pckts[i].map)) {
oce_destroy_wq(wq);
return (NULL);
}
}
if (oce_new_wq(sc, wq)) {
oce_destroy_wq(wq);
return (NULL);
}
eq->cq[eq->cq_valid] = cq;
eq->cq_valid++;
cq->cb_arg = wq;
cq->cq_intr = oce_intr_wq;
return (wq);
}
void
oce_destroy_wq(struct oce_wq *wq)
{
struct oce_softc *sc = wq->sc;
int i;
if (wq->id >= 0)
oce_destroy_queue(sc, QTYPE_WQ, wq->id);
if (wq->cq != NULL)
oce_destroy_cq(wq->cq);
if (wq->ring != NULL)
oce_destroy_ring(sc, wq->ring);
for (i = 0; i < OCE_WQ_PACKET_ARRAY_SIZE; i++) {
if (wq->pckts[i].map != NULL) {
bus_dmamap_unload(wq->tag, wq->pckts[i].map);
bus_dmamap_destroy(wq->tag, wq->pckts[i].map);
}
}
free(wq, M_DEVBUF);
}
/**
* @brief function to allocate receive queue resources
* @param sc software handle to the device
* @param eq pointer to associated event queue
* @param if_id interface identifier index
* @param q_len length of receive queue
* @param frag_size size of an receive queue fragment
* @param rss is-rss-queue flag
* @returns the pointer to the RQ created or NULL on failure
*/
struct oce_rq *
oce_create_rq(struct oce_softc *sc, struct oce_eq *eq, uint32_t if_id,
uint32_t q_len, uint32_t frag_size, uint32_t rss)
{
struct oce_rq *rq;
struct oce_cq *cq;
int i;
if (ilog2(frag_size) <= 0)
return (NULL);
/* Hardware doesn't support any other value */
if (q_len != 1024)
return (NULL);
rq = malloc(sizeof(struct oce_rq), M_DEVBUF, M_NOWAIT | M_ZERO);
if (!rq)
return (NULL);
rq->ring = oce_create_ring(sc, q_len, sizeof(struct oce_nic_rqe), 2);
if (!rq->ring) {
free(rq, M_DEVBUF);
return (NULL);
}
cq = oce_create_cq(sc, eq, CQ_LEN_1024, sizeof(struct oce_nic_rx_cqe),
1, 0, 3);
if (!cq) {
oce_destroy_ring(sc, rq->ring);
free(rq, M_DEVBUF);
return (NULL);
}
rq->id = -1;
rq->sc = sc;
rq->tag = sc->pa.pa_dmat;
rq->cfg.if_id = if_id;
rq->cfg.q_len = q_len;
rq->cfg.frag_size = frag_size;
rq->cfg.is_rss_queue = rss;
for (i = 0; i < OCE_RQ_PACKET_ARRAY_SIZE; i++) {
if (bus_dmamap_create(rq->tag, frag_size, 1, frag_size, 0,
BUS_DMA_NOWAIT, &rq->pckts[i].map)) {
oce_destroy_rq(rq);
return (NULL);
}
}
rq->cq = cq;
eq->cq[eq->cq_valid] = cq;
eq->cq_valid++;
cq->cb_arg = rq;
cq->cq_intr = oce_intr_rq;
/* RX queue is created in oce_init */
return (rq);
}
void
oce_destroy_rq(struct oce_rq *rq)
{
struct oce_softc *sc = rq->sc;
int i;
if (rq->id >= 0)
oce_destroy_queue(sc, QTYPE_RQ, rq->id);
if (rq->cq != NULL)
oce_destroy_cq(rq->cq);
if (rq->ring != NULL)
oce_destroy_ring(sc, rq->ring);
for (i = 0; i < OCE_RQ_PACKET_ARRAY_SIZE; i++) {
if (rq->pckts[i].map != NULL) {
bus_dmamap_unload(rq->tag, rq->pckts[i].map);
bus_dmamap_destroy(rq->tag, rq->pckts[i].map);
}
if (rq->pckts[i].mbuf)
m_freem(rq->pckts[i].mbuf);
}
free(rq, M_DEVBUF);
}
struct oce_eq *
oce_create_eq(struct oce_softc *sc)
{
struct oce_eq *eq;
/* allocate an eq */
eq = malloc(sizeof(struct oce_eq), M_DEVBUF, M_NOWAIT | M_ZERO);
if (eq == NULL)
return (NULL);
eq->ring = oce_create_ring(sc, EQ_LEN_1024, EQE_SIZE_4, 8);
if (!eq->ring) {
free(eq, M_DEVBUF);
return (NULL);
}
eq->id = -1;
eq->sc = sc;
eq->cfg.q_len = EQ_LEN_1024; /* length of event queue */
eq->cfg.item_size = EQE_SIZE_4; /* size of a queue item */
eq->cfg.eqd = OCE_DEFAULT_EQD; /* event queue delay */
if (oce_new_eq(sc, eq)) {
oce_destroy_ring(sc, eq->ring);
free(eq, M_DEVBUF);
return (NULL);
}
return (eq);
}
void
oce_destroy_eq(struct oce_eq *eq)
{
struct oce_softc *sc = eq->sc;
if (eq->id >= 0)
oce_destroy_queue(sc, QTYPE_EQ, eq->id);
if (eq->ring != NULL)
oce_destroy_ring(sc, eq->ring);
free(eq, M_DEVBUF);
}
struct oce_mq *
oce_create_mq(struct oce_softc *sc, struct oce_eq *eq)
{
struct oce_mq *mq = NULL;
struct oce_cq *cq;
/* allocate the mq */
mq = malloc(sizeof(struct oce_mq), M_DEVBUF, M_NOWAIT | M_ZERO);
if (!mq)
return (NULL);
mq->ring = oce_create_ring(sc, 128, sizeof(struct oce_mbx), 8);
if (!mq->ring) {
free(mq, M_DEVBUF);
return (NULL);
}
cq = oce_create_cq(sc, eq, CQ_LEN_256, sizeof(struct oce_mq_cqe),
1, 0, 0);
if (!cq) {
oce_destroy_ring(sc, mq->ring);
free(mq, M_DEVBUF);
return (NULL);
}
mq->id = -1;
mq->sc = sc;
mq->cq = cq;
mq->cfg.q_len = 128;
if (oce_new_mq(sc, mq)) {
oce_destroy_cq(mq->cq);
oce_destroy_ring(sc, mq->ring);
free(mq, M_DEVBUF);
return (NULL);
}
eq->cq[eq->cq_valid] = cq;
eq->cq_valid++;
mq->cq->eq = eq;
mq->cq->cb_arg = mq;
mq->cq->cq_intr = oce_intr_mq;
return (mq);
}
void
oce_destroy_mq(struct oce_mq *mq)
{
struct oce_softc *sc = mq->sc;
if (mq->id >= 0)
oce_destroy_queue(sc, QTYPE_MQ, mq->id);
if (mq->ring != NULL)
oce_destroy_ring(sc, mq->ring);
if (mq->cq != NULL)
oce_destroy_cq(mq->cq);
free(mq, M_DEVBUF);
}
/**
* @brief Function to create a completion queue
* @param sc software handle to the device
* @param eq optional eq to be associated with to the cq
* @param q_len length of completion queue
* @param item_size size of completion queue items
* @param is_eventable event table
* @param nodelay no delay flag
* @param ncoalesce no coalescence flag
* @returns pointer to the cq created, NULL on failure
*/
struct oce_cq *
oce_create_cq(struct oce_softc *sc, struct oce_eq *eq, uint32_t q_len,
uint32_t item_size, uint32_t eventable, uint32_t nodelay,
uint32_t ncoalesce)
{
struct oce_cq *cq = NULL;
cq = malloc(sizeof(struct oce_cq), M_DEVBUF, M_NOWAIT | M_ZERO);
if (!cq)
return (NULL);
cq->ring = oce_create_ring(sc, q_len, item_size, 4);
if (!cq->ring) {
free(cq, M_DEVBUF);
return (NULL);
}
cq->sc = sc;
cq->eq = eq;
cq->cfg.q_len = q_len;
cq->cfg.item_size = item_size;
cq->cfg.nodelay = (uint8_t) nodelay;
cq->cfg.ncoalesce = ncoalesce;
cq->cfg.eventable = eventable;
if (oce_new_cq(sc, cq)) {
oce_destroy_ring(sc, cq->ring);
free(cq, M_DEVBUF);
return (NULL);
}
sc->cq[sc->ncqs++] = cq;
return (cq);
}
void
oce_destroy_cq(struct oce_cq *cq)
{
struct oce_softc *sc = cq->sc;
if (cq->ring != NULL) {
oce_destroy_queue(sc, QTYPE_CQ, cq->id);
oce_destroy_ring(sc, cq->ring);
}
free(cq, M_DEVBUF);
}
/**
* @brief Function to arm an EQ so that it can generate events
* @param eq pointer to event queue structure
* @param neqe number of EQEs to arm
* @param rearm rearm bit enable/disable
* @param clearint bit to clear the interrupt condition because of which
* EQEs are generated
*/
static inline void
oce_arm_eq(struct oce_eq *eq, int neqe, int rearm, int clearint)
{
oce_write_db(eq->sc, PD_EQ_DB, eq->id | PD_EQ_DB_EVENT |
(clearint << 9) | (neqe << 16) | (rearm << 29));
}
/**
* @brief Function to arm a CQ with CQEs
* @param cq pointer to the completion queue structure
* @param ncqe number of CQEs to arm
* @param rearm rearm bit enable/disable
*/
static inline void
oce_arm_cq(struct oce_cq *cq, int ncqe, int rearm)
{
oce_write_db(cq->sc, PD_CQ_DB, cq->id | (ncqe << 16) | (rearm << 29));
}
/**
* @brief function to cleanup the eqs used during stop
* @param eq pointer to event queue structure
* @returns the number of EQs processed
*/
void
oce_drain_eq(struct oce_eq *eq)
{
struct oce_eqe *eqe;
int neqe = 0;
oce_dma_sync(&eq->ring->dma, BUS_DMASYNC_POSTREAD);
OCE_RING_FOREACH(eq->ring, eqe, eqe->evnt != 0) {
eqe->evnt = 0;
oce_dma_sync(&eq->ring->dma, BUS_DMASYNC_POSTWRITE);
neqe++;
}
oce_arm_eq(eq, neqe, FALSE, TRUE);
}
void
oce_drain_wq(struct oce_wq *wq)
{
struct oce_cq *cq = wq->cq;
struct oce_nic_tx_cqe *cqe;
int ncqe = 0;
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_POSTREAD);
OCE_RING_FOREACH(cq->ring, cqe, WQ_CQE_VALID(cqe)) {
WQ_CQE_INVALIDATE(cqe);
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_POSTWRITE);
ncqe++;
}
oce_arm_cq(cq, ncqe, FALSE);
}
void
oce_drain_mq(struct oce_mq *mq)
{
struct oce_cq *cq = mq->cq;
struct oce_mq_cqe *cqe;
int ncqe = 0;
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_POSTREAD);
OCE_RING_FOREACH(cq->ring, cqe, MQ_CQE_VALID(cqe)) {
MQ_CQE_INVALIDATE(cqe);
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_POSTWRITE);
ncqe++;
}
oce_arm_cq(cq, ncqe, FALSE);
}
void
oce_drain_rq(struct oce_rq *rq)
{
struct oce_nic_rx_cqe *cqe;
struct oce_cq *cq = rq->cq;
int ncqe = 0;
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_POSTREAD);
OCE_RING_FOREACH(cq->ring, cqe, RQ_CQE_VALID(cqe)) {
RQ_CQE_INVALIDATE(cqe);
oce_dma_sync(&cq->ring->dma, BUS_DMASYNC_POSTWRITE);
ncqe++;
}
oce_arm_cq(cq, ncqe, FALSE);
}
void
oce_free_posted_rxbuf(struct oce_rq *rq)
{
struct oce_packet_desc *pd;
while (rq->pending) {
pd = &rq->pckts[rq->packets_out];
bus_dmamap_sync(rq->tag, pd->map, 0, pd->map->dm_mapsize,
BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(rq->tag, pd->map);
if (pd->mbuf != NULL) {
m_freem(pd->mbuf);
pd->mbuf = NULL;
}
if ((rq->packets_out + 1) == OCE_RQ_PACKET_ARRAY_SIZE)
rq->packets_out = 0;
else
rq->packets_out++;
rq->pending--;
}
}
int
oce_dma_alloc(struct oce_softc *sc, bus_size_t size, struct oce_dma_mem *dma)
{
int rc;
bzero(dma, sizeof(struct oce_dma_mem));
dma->tag = sc->pa.pa_dmat;
rc = bus_dmamap_create(dma->tag, size, 1, size, 0, BUS_DMA_NOWAIT,
&dma->map);
if (rc != 0) {
printf("%s: failed to allocate DMA handle", sc->dev.dv_xname);
goto fail_0;
}
rc = bus_dmamem_alloc(dma->tag, size, PAGE_SIZE, 0, &dma->segs, 1,
&dma->nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
if (rc != 0) {
printf("%s: failed to allocate DMA memory", sc->dev.dv_xname);
goto fail_1;
}
rc = bus_dmamem_map(dma->tag, &dma->segs, dma->nsegs, size,
&dma->vaddr, BUS_DMA_NOWAIT);
if (rc != 0) {
printf("%s: failed to map DMA memory", sc->dev.dv_xname);
goto fail_2;
}
rc = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size, NULL,
BUS_DMA_NOWAIT);
if (rc != 0) {
printf("%s: failed to load DMA memory", sc->dev.dv_xname);
goto fail_3;
}
bus_dmamap_sync(dma->tag, dma->map, 0, dma->map->dm_mapsize,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
dma->paddr = dma->map->dm_segs[0].ds_addr;
dma->size = size;
return (0);
fail_3:
bus_dmamem_unmap(dma->tag, dma->vaddr, size);
fail_2:
bus_dmamem_free(dma->tag, &dma->segs, dma->nsegs);
fail_1:
bus_dmamap_destroy(dma->tag, dma->map);
fail_0:
return (rc);
}
void
oce_dma_free(struct oce_softc *sc, struct oce_dma_mem *dma)
{
if (dma->tag == NULL)
return;
if (dma->map != NULL) {
oce_dma_sync(dma, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(dma->tag, dma->map);
if (dma->vaddr != 0) {
bus_dmamem_free(dma->tag, &dma->segs, dma->nsegs);
dma->vaddr = 0;
}
bus_dmamap_destroy(dma->tag, dma->map);
dma->map = NULL;
dma->tag = NULL;
}
}
struct oce_ring *
oce_create_ring(struct oce_softc *sc, int q_len, int item_size, int max_segs)
{
struct oce_dma_mem *dma;
struct oce_ring *ring;
bus_size_t size = q_len * item_size;
int rc;
if (size > max_segs * PAGE_SIZE)
return (NULL);
ring = malloc(sizeof(struct oce_ring), M_DEVBUF, M_NOWAIT | M_ZERO);
if (ring == NULL)
return (NULL);
ring->isize = item_size;
ring->nitems = q_len;
dma = &ring->dma;
dma->tag = sc->pa.pa_dmat;
rc = bus_dmamap_create(dma->tag, size, max_segs, PAGE_SIZE, 0,
BUS_DMA_NOWAIT, &dma->map);
if (rc != 0) {
printf("%s: failed to allocate DMA handle", sc->dev.dv_xname);
goto fail_0;
}
rc = bus_dmamem_alloc(dma->tag, size, 0, 0, &dma->segs, max_segs,
&dma->nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
if (rc != 0) {
printf("%s: failed to allocate DMA memory", sc->dev.dv_xname);
goto fail_1;
}
rc = bus_dmamem_map(dma->tag, &dma->segs, dma->nsegs, size,
&dma->vaddr, BUS_DMA_NOWAIT);
if (rc != 0) {
printf("%s: failed to map DMA memory", sc->dev.dv_xname);
goto fail_2;
}
bus_dmamap_sync(dma->tag, dma->map, 0, dma->map->dm_mapsize,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
dma->paddr = 0;
dma->size = size;
return (ring);
fail_2:
bus_dmamem_free(dma->tag, &dma->segs, dma->nsegs);
fail_1:
bus_dmamap_destroy(dma->tag, dma->map);
fail_0:
free(ring, M_DEVBUF);
return (NULL);
}
void
oce_destroy_ring(struct oce_softc *sc, struct oce_ring *ring)
{
oce_dma_free(sc, &ring->dma);
free(ring, M_DEVBUF);
}
int
oce_load_ring(struct oce_softc *sc, struct oce_ring *ring,
struct phys_addr *pa_list, int max_segs)
{
struct oce_dma_mem *dma = &ring->dma;
bus_dma_segment_t *segs;
int i, nsegs;
if (bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
ring->isize * ring->nitems, NULL, BUS_DMA_NOWAIT)) {
printf("%s: failed to load a ring map\n", sc->dev.dv_xname);
return (0);
}
segs = dma->map->dm_segs;
nsegs = dma->map->dm_nsegs;
if (nsegs > max_segs) {
printf("%s: too many segments", sc->dev.dv_xname);
return (0);
}
bus_dmamap_sync(dma->tag, dma->map, 0, dma->map->dm_mapsize,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
for (i = 0; i < nsegs; i++) {
pa_list[i].lo = ADDR_LO(segs[i].ds_addr);
pa_list[i].hi = ADDR_HI(segs[i].ds_addr);
}
return (nsegs);
}
static inline void *
oce_ring_get(struct oce_ring *ring)
{
int index = ring->index;
if (++ring->index == ring->nitems)
ring->index = 0;
return ((void *)(ring->dma.vaddr + index * ring->isize));
}
static inline void *
oce_ring_first(struct oce_ring *ring)
{
return ((void *)(ring->dma.vaddr + ring->index * ring->isize));
}
static inline void *
oce_ring_next(struct oce_ring *ring)
{
if (++ring->index == ring->nitems)
ring->index = 0;
return ((void *)(ring->dma.vaddr + ring->index * ring->isize));
}
/**
* @brief Wait for FW to become ready and reset it
* @param sc software handle to the device
*/
int
oce_init_fw(struct oce_softc *sc)
{
struct ioctl_common_function_reset cmd;
uint32_t reg;
int err = 0, tmo = 60000;
/* read semaphore CSR */
reg = oce_read_csr(sc, MPU_EP_SEMAPHORE(sc));
/* if host is ready then wait for fw ready else send POST */
if ((reg & MPU_EP_SEM_STAGE_MASK) <= POST_STAGE_AWAITING_HOST_RDY) {
reg = (reg & ~MPU_EP_SEM_STAGE_MASK) | POST_STAGE_CHIP_RESET;
oce_write_csr(sc, MPU_EP_SEMAPHORE(sc), reg);
}
/* wait for FW to become ready */
for (;;) {
if (--tmo == 0)
break;
DELAY(1000);
reg = oce_read_csr(sc, MPU_EP_SEMAPHORE(sc));
if (reg & MPU_EP_SEM_ERROR) {
printf(": POST failed: %#x\n", reg);
return (ENXIO);
}
if ((reg & MPU_EP_SEM_STAGE_MASK) == POST_STAGE_ARMFW_READY) {
/* reset FW */
if (ISSET(sc->flags, OCE_F_RESET_RQD)) {
bzero(&cmd, sizeof(cmd));
err = oce_cmd(sc, SUBSYS_COMMON,
OPCODE_COMMON_FUNCTION_RESET,
OCE_MBX_VER_V0, &cmd, sizeof(cmd));
}
return (err);
}
}
printf(": POST timed out: %#x\n", reg);
return (ENXIO);
}
static inline int
oce_mbox_wait(struct oce_softc *sc)
{
int i;
for (i = 0; i < 20000; i++) {
if (oce_read_db(sc, PD_MPU_MBOX_DB) & PD_MPU_MBOX_DB_READY)
return (0);
DELAY(100);
}
return (ETIMEDOUT);
}
/**
* @brief Mailbox dispatch
* @param sc software handle to the device
*/
int
oce_mbox_dispatch(struct oce_softc *sc)
{
uint32_t pa, reg;
int err;
pa = (uint32_t)((uint64_t)sc->bsmbx.paddr >> 34);
reg = PD_MPU_MBOX_DB_HI | (pa << PD_MPU_MBOX_DB_ADDR_SHIFT);
if ((err = oce_mbox_wait(sc)) != 0)
goto out;
oce_write_db(sc, PD_MPU_MBOX_DB, reg);
pa = (uint32_t)((uint64_t)sc->bsmbx.paddr >> 4) & 0x3fffffff;
reg = pa << PD_MPU_MBOX_DB_ADDR_SHIFT;
if ((err = oce_mbox_wait(sc)) != 0)
goto out;
oce_write_db(sc, PD_MPU_MBOX_DB, reg);
oce_dma_sync(&sc->bsmbx, BUS_DMASYNC_POSTWRITE);
if ((err = oce_mbox_wait(sc)) != 0)
goto out;
out:
oce_dma_sync(&sc->bsmbx, BUS_DMASYNC_PREREAD);
if (err)
printf("%s: mailbox timeout\n", sc->dev.dv_xname);
return (err);
}
/**
* @brief Function to initialize the hw with host endian information
* @param sc software handle to the device
* @returns 0 on success, ETIMEDOUT on failure
*/
int
oce_mbox_init(struct oce_softc *sc)
{
struct oce_bmbx *bmbx = OCE_MEM_KVA(&sc->bsmbx);
uint8_t *ptr = (uint8_t *)&bmbx->mbx;
if (!ISSET(sc->flags, OCE_F_MBOX_ENDIAN_RQD))
return (0);
/* Endian Signature */
*ptr++ = 0xff;
*ptr++ = 0x12;
*ptr++ = 0x34;
*ptr++ = 0xff;
*ptr++ = 0xff;
*ptr++ = 0x56;
*ptr++ = 0x78;
*ptr = 0xff;
return (oce_mbox_dispatch(sc));
}
int
oce_cmd(struct oce_softc *sc, int subsys, int opcode, int version,
void *payload, int length)
{
struct oce_bmbx *bmbx = OCE_MEM_KVA(&sc->bsmbx);
struct oce_mbx *mbx = &bmbx->mbx;
struct oce_dma_mem sgl;
struct mbx_hdr *hdr;
caddr_t epayload = NULL;
int err;
if (length > OCE_MBX_PAYLOAD) {
if (oce_dma_alloc(sc, length, &sgl))
return (-1);
epayload = OCE_MEM_KVA(&sgl);
}
oce_dma_sync(&sc->bsmbx, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
bzero(mbx, sizeof(struct oce_mbx));
mbx->payload_length = length;
if (epayload) {
mbx->u0.s.sge_count = 1;
oce_dma_sync(&sgl, BUS_DMASYNC_PREWRITE);
bcopy(payload, epayload, length);
mbx->payload.u0.u1.sgl[0].paddr = sgl.paddr;
mbx->payload.u0.u1.sgl[0].length = length;
hdr = OCE_MEM_KVA(&sgl);
} else {
mbx->u0.s.embedded = 1;
bcopy(payload, &mbx->payload, length);
hdr = (struct mbx_hdr *)&mbx->payload;
}
hdr->u0.req.opcode = opcode;
hdr->u0.req.subsystem = subsys;
hdr->u0.req.request_length = length - sizeof(*hdr);
hdr->u0.req.version = version;
if (opcode == OPCODE_COMMON_FUNCTION_RESET)
hdr->u0.req.timeout = 2 * MBX_TIMEOUT_SEC;
else
hdr->u0.req.timeout = MBX_TIMEOUT_SEC;
err = oce_mbox_dispatch(sc);
if (err == 0) {
if (epayload) {
oce_dma_sync(&sgl, BUS_DMASYNC_POSTWRITE);
bcopy(epayload, payload, length);
} else
bcopy(&mbx->payload, payload, length);
} else
printf("%s: mailbox error %d\n", sc->dev.dv_xname, err);
if (epayload)
oce_dma_free(sc, &sgl);
return (err);
}
/**
* @brief Firmware will send gracious notifications during
* attach only after sending first mcc commnad. We
* use MCC queue only for getting async and mailbox
* for sending cmds. So to get gracious notifications
* atleast send one dummy command on mcc.
*/
void
oce_first_mcc(struct oce_softc *sc)
{
struct oce_mbx *mbx;
struct oce_mq *mq = sc->mq;
struct mbx_hdr *hdr;
struct mbx_get_common_fw_version *cmd;
mbx = oce_ring_get(mq->ring);
bzero(mbx, sizeof(struct oce_mbx));
cmd = (struct mbx_get_common_fw_version *)&mbx->payload;
hdr = &cmd->hdr;
hdr->u0.req.subsystem = SUBSYS_COMMON;
hdr->u0.req.opcode = OPCODE_COMMON_GET_FW_VERSION;
hdr->u0.req.version = OCE_MBX_VER_V0;
hdr->u0.req.timeout = MBX_TIMEOUT_SEC;
hdr->u0.req.request_length = sizeof(*cmd) - sizeof(*hdr);
mbx->u0.s.embedded = 1;
mbx->payload_length = sizeof(*cmd);
oce_dma_sync(&mq->ring->dma, BUS_DMASYNC_PREREAD |
BUS_DMASYNC_PREWRITE);
oce_write_db(sc, PD_MQ_DB, mq->id | (1 << 16));
}
/**
* @brief Function for creating a network interface.
* @param sc software handle to the device
* @returns 0 on success, error otherwise
*/
int
oce_create_iface(struct oce_softc *sc, uint8_t *macaddr)
{
struct mbx_create_common_iface cmd;
uint32_t capab_flags, capab_en_flags;
int err = 0;
/* interface capabilities to give device when creating interface */
capab_flags = OCE_CAPAB_FLAGS;
/* capabilities to enable by default (others set dynamically) */
capab_en_flags = OCE_CAPAB_ENABLE;
if (IS_XE201(sc)) {
/* LANCER A0 workaround */
capab_en_flags &= ~MBX_RX_IFACE_FLAGS_PASS_L3L4_ERR;
capab_flags &= ~MBX_RX_IFACE_FLAGS_PASS_L3L4_ERR;
}
/* enable capabilities controlled via driver startup parameters */
if (sc->rss_enable)
capab_en_flags |= MBX_RX_IFACE_FLAGS_RSS;
else {
capab_en_flags &= ~MBX_RX_IFACE_FLAGS_RSS;
capab_flags &= ~MBX_RX_IFACE_FLAGS_RSS;
}
bzero(&cmd, sizeof(cmd));
cmd.params.req.version = 0;
cmd.params.req.cap_flags = htole32(capab_flags);
cmd.params.req.enable_flags = htole32(capab_en_flags);
if (macaddr != NULL) {
bcopy(macaddr, &cmd.params.req.mac_addr[0], ETHER_ADDR_LEN);
cmd.params.req.mac_invalid = 0;
} else
cmd.params.req.mac_invalid = 1;
err = oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_CREATE_IFACE,
OCE_MBX_VER_V0, &cmd, sizeof(cmd));
if (err)
return (err);
sc->if_id = letoh32(cmd.params.rsp.if_id);
if (macaddr != NULL)
sc->pmac_id = letoh32(cmd.params.rsp.pmac_id);
sc->nifs++;
sc->if_cap_flags = capab_en_flags;
/* Enable VLAN Promisc on HW */
err = oce_config_vlan(sc, (uint8_t)sc->if_id, NULL, 0, 1, 1);
if (err)
return (err);
/* set default flow control */
err = oce_set_flow_control(sc, sc->flow_control);
if (err)
return (err);
return (0);
}
/**
* @brief Function to send the mbx command to configure vlan
* @param sc software handle to the device
* @param if_id interface identifier index
* @param vtag_arr array of vlan tags
* @param vtag_cnt number of elements in array
* @param untagged boolean TRUE/FLASE
* @param promisc flag to enable/disable VLAN promiscuous mode
* @returns 0 on success, EIO on failure
*/
int
oce_config_vlan(struct oce_softc *sc, uint32_t if_id,
struct normal_vlan *vtag_arr, int vtag_cnt, int untagged, int promisc)
{
struct mbx_common_config_vlan cmd;
bzero(&cmd, sizeof(cmd));
cmd.params.req.if_id = if_id;
cmd.params.req.promisc = promisc;
cmd.params.req.untagged = untagged;
cmd.params.req.num_vlans = vtag_cnt;
if (!promisc)
bcopy(vtag_arr, cmd.params.req.tags.normal_vlans,
vtag_cnt * sizeof(struct normal_vlan));
return (oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_CONFIG_IFACE_VLAN,
OCE_MBX_VER_V0, &cmd, sizeof(cmd)));
}
/**
* @brief Function to set flow control capability in the hardware
* @param sc software handle to the device
* @param flow_control flow control flags to set
* @returns 0 on success, EIO on failure
*/
int
oce_set_flow_control(struct oce_softc *sc, uint32_t flow_control)
{
struct mbx_common_get_set_flow_control cmd;
bzero(&cmd, sizeof(cmd));
if (flow_control & OCE_FC_TX)
cmd.tx_flow_control = 1;
if (flow_control & OCE_FC_RX)
cmd.rx_flow_control = 1;
return (oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_SET_FLOW_CONTROL,
OCE_MBX_VER_V0, &cmd, sizeof(cmd)));
}
#ifdef OCE_RSS
/**
* @brief Function to set flow control capability in the hardware
* @param sc software handle to the device
* @param if_id interface id to read the address from
* @param enable 0=disable, OCE_RSS_xxx flags otherwise
* @returns 0 on success, EIO on failure
*/
int
oce_config_rss(struct oce_softc *sc, uint32_t if_id, int enable)
{
struct mbx_config_nic_rss cmd;
uint8_t *tbl = &cmd.params.req.cputable;
int i, j;
bzero(&cmd, sizeof(cmd));
if (enable)
cmd.params.req.enable_rss = RSS_ENABLE_IPV4 | RSS_ENABLE_IPV6 |
RSS_ENABLE_TCP_IPV4 | RSS_ENABLE_TCP_IPV6);
cmd.params.req.flush = OCE_FLUSH;
cmd.params.req.if_id = htole32(if_id);
arc4random_buf(cmd.params.req.hash, sizeof(cmd.params.req.hash));
/*
* Initialize the RSS CPU indirection table.
*
* The table is used to choose the queue to place incomming packets.
* Incomming packets are hashed. The lowest bits in the hash result
* are used as the index into the CPU indirection table.
* Each entry in the table contains the RSS CPU-ID returned by the NIC
* create. Based on the CPU ID, the receive completion is routed to
* the corresponding RSS CQs. (Non-RSS packets are always completed
* on the default (0) CQ).
*/
for (i = 0, j = 0; j < sc->nrqs; j++) {
if (sc->rq[j]->cfg.is_rss_queue)
tbl[i++] = sc->rq[j]->rss_cpuid;
}
if (i > 0)
cmd->params.req.cpu_tbl_sz_log2 = htole16(ilog2(i));
else
return (ENXIO);
return (oce_cmd(sc, SUBSYS_NIC, OPCODE_NIC_CONFIG_RSS, OCE_MBX_VER_V0,
&cmd, sizeof(cmd)));
}
#endif /* OCE_RSS */
/**
* @brief Function for hardware update multicast filter
* @param sc software handle to the device
* @param multi table of multicast addresses
* @param naddr number of multicast addresses in the table
*/
int
oce_update_mcast(struct oce_softc *sc,
uint8_t multi[][ETHER_ADDR_LEN], int naddr)
{
struct mbx_set_common_iface_multicast cmd;
bzero(&cmd, sizeof(cmd));
bcopy(&multi[0], &cmd.params.req.mac[0], naddr * ETHER_ADDR_LEN);
cmd.params.req.num_mac = htole16(naddr);
cmd.params.req.if_id = sc->if_id;
return (oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_SET_IFACE_MULTICAST,
OCE_MBX_VER_V0, &cmd, sizeof(cmd)));
}
/**
* @brief RXF function to enable/disable device promiscuous mode
* @param sc software handle to the device
* @param enable enable/disable flag
* @returns 0 on success, EIO on failure
* @note
* The OPCODE_NIC_CONFIG_PROMISCUOUS command deprecated for Lancer.
* This function uses the COMMON_SET_IFACE_RX_FILTER command instead.
*/
int
oce_set_promisc(struct oce_softc *sc, int enable)
{
struct mbx_set_common_iface_rx_filter cmd;
struct iface_rx_filter_ctx *req;
bzero(&cmd, sizeof(cmd));
req = &cmd.params.req;
req->if_id = sc->if_id;
req->iface_flags_mask = MBX_RX_IFACE_FLAGS_PROMISC |
MBX_RX_IFACE_FLAGS_VLAN_PROMISC;
if (enable)
req->iface_flags = req->iface_flags_mask;
return (oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_SET_IFACE_RX_FILTER,
OCE_MBX_VER_V0, &cmd, sizeof(cmd)));
}
/**
* @brief Function to query the link status from the hardware
* @param sc software handle to the device
* @param[out] link pointer to the structure returning link attributes
* @returns 0 on success, EIO on failure
*/
int
oce_get_link_status(struct oce_softc *sc)
{
struct mbx_query_common_link_config cmd;
struct link_status link;
int err;
bzero(&cmd, sizeof(cmd));
err = oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_QUERY_LINK_CONFIG,
OCE_MBX_VER_V0, &cmd, sizeof(cmd));
if (err)
return (err);
bcopy(&cmd.params.rsp, &link, sizeof(struct link_status));
link.logical_link_status = letoh32(link.logical_link_status);
link.qos_link_speed = letoh16(link.qos_link_speed);
if (link.logical_link_status == NTWK_LOGICAL_LINK_UP)
sc->link_status = NTWK_LOGICAL_LINK_UP;
else
sc->link_status = NTWK_LOGICAL_LINK_DOWN;
if (link.mac_speed > 0 && link.mac_speed < 5)
sc->link_speed = link.mac_speed;
else
sc->link_speed = 0;
sc->duplex = link.mac_duplex;
sc->qos_link_speed = (uint32_t )link.qos_link_speed * 10;
return (0);
}
int
oce_macaddr_get(struct oce_softc *sc, uint8_t *macaddr)
{
struct mbx_query_common_iface_mac cmd;
int err;
bzero(&cmd, sizeof(cmd));
cmd.params.req.type = MAC_ADDRESS_TYPE_NETWORK;
cmd.params.req.permanent = 1;
err = oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_QUERY_IFACE_MAC,
OCE_MBX_VER_V0, &cmd, sizeof(cmd));
if (err == 0)
bcopy(&cmd.params.rsp.mac.mac_addr[0], macaddr,
ETHER_ADDR_LEN);
return (err);
}
int
oce_macaddr_add(struct oce_softc *sc, uint8_t *enaddr, uint32_t if_id,
uint32_t *pmac_id)
{
struct mbx_add_common_iface_mac cmd;
int err;
bzero(&cmd, sizeof(cmd));
cmd.params.req.if_id = htole16(if_id);
bcopy(enaddr, cmd.params.req.mac_address, ETHER_ADDR_LEN);
err = oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_ADD_IFACE_MAC,
OCE_MBX_VER_V0, &cmd, sizeof(cmd));
if (err == 0)
*pmac_id = letoh32(cmd.params.rsp.pmac_id);
return (err);
}
int
oce_macaddr_del(struct oce_softc *sc, uint32_t if_id, uint32_t pmac_id)
{
struct mbx_del_common_iface_mac cmd;
bzero(&cmd, sizeof(cmd));
cmd.params.req.if_id = htole16(if_id);
cmd.params.req.pmac_id = htole32(pmac_id);
return (oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_DEL_IFACE_MAC,
OCE_MBX_VER_V0, &cmd, sizeof(cmd)));
}
int
oce_check_native_mode(struct oce_softc *sc)
{
struct mbx_common_set_function_cap cmd;
int err;
bzero(&cmd, sizeof(cmd));
cmd.params.req.valid_capability_flags = CAP_SW_TIMESTAMPS |
CAP_BE3_NATIVE_ERX_API;
cmd.params.req.capability_flags = CAP_BE3_NATIVE_ERX_API;
err = oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_SET_FUNCTIONAL_CAPS,
OCE_MBX_VER_V0, &cmd, sizeof(cmd));
if (err)
return (err);
if (cmd.params.rsp.capability_flags & CAP_BE3_NATIVE_ERX_API)
SET(sc->flags, OCE_F_BE3_NATIVE);
return (0);
}
int
oce_new_rq(struct oce_softc *sc, struct oce_rq *rq)
{
struct mbx_create_nic_rq cmd;
int err, npages;
bzero(&cmd, sizeof(cmd));
npages = oce_load_ring(sc, rq->ring, &cmd.params.req.pages[0],
nitems(cmd.params.req.pages));
if (!npages) {
printf("%s: failed to load the rq ring\n", __func__);
return (1);
}
if (IS_XE201(sc)) {
cmd.params.req.frag_size = rq->cfg.frag_size / 2048;
cmd.params.req.page_size = 1;
} else
cmd.params.req.frag_size = ilog2(rq->cfg.frag_size);
cmd.params.req.num_pages = npages;
cmd.params.req.cq_id = rq->cq->id;
cmd.params.req.if_id = htole32(sc->if_id);
cmd.params.req.max_frame_size = htole16(rq->cfg.mtu);
cmd.params.req.is_rss_queue = htole32(rq->cfg.is_rss_queue);
err = oce_cmd(sc, SUBSYS_NIC, OPCODE_NIC_CREATE_RQ,
IS_XE201(sc) ? OCE_MBX_VER_V1 : OCE_MBX_VER_V0, &cmd,
sizeof(cmd));
if (err)
return (err);
rq->id = letoh16(cmd.params.rsp.rq_id);
rq->rss_cpuid = cmd.params.rsp.rss_cpuid;
return (0);
}
int
oce_new_wq(struct oce_softc *sc, struct oce_wq *wq)
{
struct mbx_create_nic_wq cmd;
int err, npages;
bzero(&cmd, sizeof(cmd));
npages = oce_load_ring(sc, wq->ring, &cmd.params.req.pages[0],
nitems(cmd.params.req.pages));
if (!npages) {
printf("%s: failed to load the wq ring\n", __func__);
return (1);
}
if (IS_XE201(sc))
cmd.params.req.if_id = sc->if_id;
cmd.params.req.nic_wq_type = wq->cfg.wq_type;
cmd.params.req.num_pages = npages;
cmd.params.req.wq_size = ilog2(wq->cfg.q_len) + 1;
cmd.params.req.cq_id = htole16(wq->cq->id);
cmd.params.req.ulp_num = 1;
err = oce_cmd(sc, SUBSYS_NIC, OPCODE_NIC_CREATE_WQ,
IS_XE201(sc) ? OCE_MBX_VER_V1 : OCE_MBX_VER_V0, &cmd,
sizeof(cmd));
if (err)
return (err);
wq->id = letoh16(cmd.params.rsp.wq_id);
return (0);
}
int
oce_new_mq(struct oce_softc *sc, struct oce_mq *mq)
{
struct mbx_create_common_mq_ex cmd;
union oce_mq_ext_ctx *ctx;
int err, npages;
bzero(&cmd, sizeof(cmd));
npages = oce_load_ring(sc, mq->ring, &cmd.params.req.pages[0],
nitems(cmd.params.req.pages));
if (!npages) {
printf("%s: failed to load the mq ring\n", __func__);
return (-1);
}
ctx = &cmd.params.req.context;
ctx->v0.num_pages = npages;
ctx->v0.cq_id = mq->cq->id;
ctx->v0.ring_size = ilog2(mq->cfg.q_len) + 1;
ctx->v0.valid = 1;
/* Subscribe to Link State and Group 5 Events(bits 1 and 5 set) */
ctx->v0.async_evt_bitmap = 0xffffffff;
err = oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_CREATE_MQ_EXT,
OCE_MBX_VER_V0, &cmd, sizeof(cmd));
if (err)
return (err);
mq->id = letoh16(cmd.params.rsp.mq_id);
return (0);
}
int
oce_new_eq(struct oce_softc *sc, struct oce_eq *eq)
{
struct mbx_create_common_eq cmd;
int err, npages;
bzero(&cmd, sizeof(cmd));
npages = oce_load_ring(sc, eq->ring, &cmd.params.req.pages[0],
nitems(cmd.params.req.pages));
if (!npages) {
printf("%s: failed to load the eq ring\n", __func__);
return (-1);
}
cmd.params.req.ctx.num_pages = htole16(npages);
cmd.params.req.ctx.valid = 1;
cmd.params.req.ctx.size = (eq->cfg.item_size == 4) ? 0 : 1;
cmd.params.req.ctx.count = ilog2(eq->cfg.q_len / 256);
cmd.params.req.ctx.armed = 0;
cmd.params.req.ctx.delay_mult = htole32(eq->cfg.eqd);
err = oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_CREATE_EQ,
OCE_MBX_VER_V0, &cmd, sizeof(cmd));
if (err)
return (err);
eq->id = letoh16(cmd.params.rsp.eq_id);
return (0);
}
int
oce_new_cq(struct oce_softc *sc, struct oce_cq *cq)
{
struct mbx_create_common_cq cmd;
union oce_cq_ctx *ctx;
int err, npages;
bzero(&cmd, sizeof(cmd));
npages = oce_load_ring(sc, cq->ring, &cmd.params.req.pages[0],
nitems(cmd.params.req.pages));
if (!npages) {
printf("%s: failed to load the cq ring\n", __func__);
return (-1);
}
ctx = &cmd.params.req.cq_ctx;
if (IS_XE201(sc)) {
ctx->v2.num_pages = htole16(npages);
ctx->v2.page_size = 1; /* for 4K */
ctx->v2.eventable = cq->cfg.eventable;
ctx->v2.valid = 1;
ctx->v2.count = ilog2(cq->cfg.q_len / 256);
ctx->v2.nodelay = cq->cfg.nodelay;
ctx->v2.coalesce_wm = cq->cfg.ncoalesce;
ctx->v2.armed = 0;
ctx->v2.eq_id = cq->eq->id;
if (ctx->v2.count == 3) {
if (cq->cfg.q_len > (4*1024)-1)
ctx->v2.cqe_count = (4*1024)-1;
else
ctx->v2.cqe_count = cq->cfg.q_len;
}
} else {
ctx->v0.num_pages = htole16(npages);
ctx->v0.eventable = cq->cfg.eventable;
ctx->v0.valid = 1;
ctx->v0.count = ilog2(cq->cfg.q_len / 256);
ctx->v0.nodelay = cq->cfg.nodelay;
ctx->v0.coalesce_wm = cq->cfg.ncoalesce;
ctx->v0.armed = 0;
ctx->v0.eq_id = cq->eq->id;
}
err = oce_cmd(sc, SUBSYS_COMMON, OPCODE_COMMON_CREATE_CQ,
IS_XE201(sc) ? OCE_MBX_VER_V2 : OCE_MBX_VER_V0, &cmd,
sizeof(cmd));
if (err)
return (err);
cq->id = letoh16(cmd.params.rsp.cq_id);
return (0);
}
int
oce_destroy_queue(struct oce_softc *sc, enum qtype qtype, uint32_t qid)
{
struct mbx_destroy_common_mq cmd;
int opcode, subsys;
switch (qtype) {
case QTYPE_CQ:
opcode = OPCODE_COMMON_DESTROY_CQ;
subsys = SUBSYS_COMMON;
break;
case QTYPE_EQ:
opcode = OPCODE_COMMON_DESTROY_EQ;
subsys = SUBSYS_COMMON;
break;
case QTYPE_MQ:
opcode = OPCODE_COMMON_DESTROY_MQ;
subsys = SUBSYS_COMMON;
break;
case QTYPE_RQ:
opcode = OPCODE_NIC_DELETE_RQ;
subsys = SUBSYS_NIC;
break;
case QTYPE_WQ:
opcode = OPCODE_NIC_DELETE_WQ;
subsys = SUBSYS_NIC;
break;
default:
return (EINVAL);
}
bzero(&cmd, sizeof(cmd));
cmd.params.req.id = htole16(qid);
return (oce_cmd(sc, subsys, opcode, OCE_MBX_VER_V0, &cmd,
sizeof(cmd)));
}
int
oce_stats_be2(struct oce_softc *sc, uint64_t *rxe, uint64_t *txe)
{
struct mbx_get_nic_stats_v0 cmd;
struct oce_pmem_stats *ms;
struct oce_rxf_stats_v0 *rs;
struct oce_port_rxf_stats_v0 *ps;
int err;
bzero(&cmd, sizeof(cmd));
err = oce_cmd(sc, SUBSYS_NIC, OPCODE_NIC_GET_STATS, OCE_MBX_VER_V0,
&cmd, sizeof(cmd));
if (err)
return (err);
ms = &cmd.params.rsp.stats.pmem;
rs = &cmd.params.rsp.stats.rxf;
ps = &rs->port[sc->port_id];
*rxe = ps->rx_crc_errors + ps->rx_in_range_errors +
ps->rx_frame_too_long + ps->rx_dropped_runt +
ps->rx_ip_checksum_errs + ps->rx_tcp_checksum_errs +
ps->rx_udp_checksum_errs + ps->rxpp_fifo_overflow_drop +
ps->rx_dropped_tcp_length + ps->rx_dropped_too_small +
ps->rx_dropped_too_short + ps->rx_out_range_errors +
ps->rx_dropped_header_too_small + ps->rx_input_fifo_overflow_drop +
ps->rx_alignment_symbol_errors;
if (sc->if_id)
*rxe += rs->port1_jabber_events;
else
*rxe += rs->port0_jabber_events;
*rxe += ms->eth_red_drops;
*txe = 0; /* hardware doesn't provide any extra tx error statistics */
return (0);
}
int
oce_stats_be3(struct oce_softc *sc, uint64_t *rxe, uint64_t *txe)
{
struct mbx_get_nic_stats cmd;
struct oce_pmem_stats *ms;
struct oce_rxf_stats_v1 *rs;
struct oce_port_rxf_stats_v1 *ps;
int err;
bzero(&cmd, sizeof(cmd));
err = oce_cmd(sc, SUBSYS_NIC, OPCODE_NIC_GET_STATS, OCE_MBX_VER_V1,
&cmd, sizeof(cmd));
if (err)
return (err);
ms = &cmd.params.rsp.stats.pmem;
rs = &cmd.params.rsp.stats.rxf;
ps = &rs->port[sc->port_id];
*rxe = ps->rx_crc_errors + ps->rx_in_range_errors +
ps->rx_frame_too_long + ps->rx_dropped_runt +
ps->rx_ip_checksum_errs + ps->rx_tcp_checksum_errs +
ps->rx_udp_checksum_errs + ps->rxpp_fifo_overflow_drop +
ps->rx_dropped_tcp_length + ps->rx_dropped_too_small +
ps->rx_dropped_too_short + ps->rx_out_range_errors +
ps->rx_dropped_header_too_small + ps->rx_input_fifo_overflow_drop +
ps->rx_alignment_symbol_errors + ps->jabber_events;
*rxe += ms->eth_red_drops;
*txe = 0; /* hardware doesn't provide any extra tx error statistics */
return (0);
}
int
oce_stats_xe(struct oce_softc *sc, uint64_t *rxe, uint64_t *txe)
{
struct mbx_get_pport_stats cmd;
struct oce_pport_stats *pps;
int err;
bzero(&cmd, sizeof(cmd));
cmd.params.req.reset_stats = 0;
cmd.params.req.port_number = sc->if_id;
err = oce_cmd(sc, SUBSYS_NIC, OPCODE_NIC_GET_PPORT_STATS,
OCE_MBX_VER_V0, &cmd, sizeof(cmd));
if (err)
return (err);
pps = &cmd.params.rsp.pps;
*rxe = pps->rx_discards + pps->rx_errors + pps->rx_crc_errors +
pps->rx_alignment_errors + pps->rx_symbol_errors +
pps->rx_frames_too_long + pps->rx_internal_mac_errors +
pps->rx_undersize_pkts + pps->rx_oversize_pkts + pps->rx_jabbers +
pps->rx_control_frames_unknown_opcode + pps->rx_in_range_errors +
pps->rx_out_of_range_errors + pps->rx_ip_checksum_errors +
pps->rx_tcp_checksum_errors + pps->rx_udp_checksum_errors +
pps->rx_fifo_overflow + pps->rx_input_fifo_overflow +
pps->rx_drops_too_many_frags + pps->rx_drops_mtu;
*txe = pps->tx_discards + pps->tx_errors + pps->tx_internal_mac_errors;
return (0);
}
int
oce_update_stats(struct oce_softc *sc, uint64_t *rxe, uint64_t *txe)
{
if (ISSET(sc->flags, OCE_F_BE2))
return (oce_stats_be2(sc, rxe, txe));
if (ISSET(sc->flags, OCE_F_BE3))
return (oce_stats_be3(sc, rxe, txe));
return (oce_stats_xe(sc, rxe, txe));
}
|