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
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* This file is part of the Chelsio T4 support code.
*
* Copyright (C) 2010-2013 Chelsio Communications. All rights reserved.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file included in this
* release for licensing terms and conditions.
*/
/*
* Copyright 2021 Oxide Computer Company
*/
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/atomic.h>
#include <sys/dlpi.h>
#include <sys/pattr.h>
#include <sys/strsubr.h>
#include <sys/stream.h>
#include <sys/strsun.h>
#include <inet/ip.h>
#include <inet/tcp.h>
#include "version.h"
#include "common/common.h"
#include "common/t4_msg.h"
#include "common/t4_regs.h"
#include "common/t4_regs_values.h"
/* TODO: Tune. */
int rx_buf_size = 8192;
int tx_copy_threshold = 256;
uint16_t rx_copy_threshold = 256;
/* Used to track coalesced tx work request */
struct txpkts {
mblk_t *tail; /* head is in the software descriptor */
uint64_t *flitp; /* ptr to flit where next pkt should start */
uint8_t npkt; /* # of packets in this work request */
uint8_t nflits; /* # of flits used by this work request */
uint16_t plen; /* total payload (sum of all packets) */
};
/* All information needed to tx a frame */
struct txinfo {
uint32_t len; /* Total length of frame */
uint32_t flags; /* Checksum and LSO flags */
uint32_t mss; /* MSS for LSO */
uint8_t nsegs; /* # of segments in the SGL, 0 means imm. tx */
uint8_t nflits; /* # of flits needed for the SGL */
uint8_t hdls_used; /* # of DMA handles used */
uint32_t txb_used; /* txb_space used */
struct ulptx_sgl sgl __attribute__((aligned(8)));
struct ulptx_sge_pair reserved[TX_SGL_SEGS / 2];
};
static int service_iq(struct sge_iq *iq, int budget);
static inline void init_iq(struct sge_iq *iq, struct adapter *sc, int tmr_idx,
int8_t pktc_idx, int qsize, uint8_t esize);
static inline void init_fl(struct sge_fl *fl, uint16_t qsize);
static inline void init_eq(struct adapter *sc, struct sge_eq *eq,
uint16_t eqtype, uint16_t qsize,uint8_t tx_chan, uint16_t iqid);
static int alloc_iq_fl(struct port_info *pi, struct sge_iq *iq,
struct sge_fl *fl, int intr_idx, int cong);
static int free_iq_fl(struct port_info *pi, struct sge_iq *iq,
struct sge_fl *fl);
static int alloc_fwq(struct adapter *sc);
static int free_fwq(struct adapter *sc);
#ifdef TCP_OFFLOAD_ENABLE
static int alloc_mgmtq(struct adapter *sc);
#endif
static int alloc_rxq(struct port_info *pi, struct sge_rxq *rxq, int intr_idx,
int i);
static int free_rxq(struct port_info *pi, struct sge_rxq *rxq);
#ifdef TCP_OFFLOAD_ENABLE
static int alloc_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq,
int intr_idx);
static int free_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq);
#endif
static int ctrl_eq_alloc(struct adapter *sc, struct sge_eq *eq);
static int eth_eq_alloc(struct adapter *sc, struct port_info *pi,
struct sge_eq *eq);
#ifdef TCP_OFFLOAD_ENABLE
static int ofld_eq_alloc(struct adapter *sc, struct port_info *pi,
struct sge_eq *eq);
#endif
static int alloc_eq(struct adapter *sc, struct port_info *pi,
struct sge_eq *eq);
static int free_eq(struct adapter *sc, struct sge_eq *eq);
#ifdef TCP_OFFLOAD_ENABLE
static int alloc_wrq(struct adapter *sc, struct port_info *pi,
struct sge_wrq *wrq, int idx);
static int free_wrq(struct adapter *sc, struct sge_wrq *wrq);
#endif
static int alloc_txq(struct port_info *pi, struct sge_txq *txq, int idx);
static int free_txq(struct port_info *pi, struct sge_txq *txq);
static int alloc_dma_memory(struct adapter *sc, size_t len, int flags,
ddi_device_acc_attr_t *acc_attr, ddi_dma_attr_t *dma_attr,
ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl, uint64_t *pba,
caddr_t *pva);
static int free_dma_memory(ddi_dma_handle_t *dhdl, ddi_acc_handle_t *ahdl);
static int alloc_desc_ring(struct adapter *sc, size_t len, int rw,
ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl, uint64_t *pba,
caddr_t *pva);
static int free_desc_ring(ddi_dma_handle_t *dhdl, ddi_acc_handle_t *ahdl);
static int alloc_tx_copybuffer(struct adapter *sc, size_t len,
ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl, uint64_t *pba,
caddr_t *pva);
static inline bool is_new_response(const struct sge_iq *iq,
struct rsp_ctrl **ctrl);
static inline void iq_next(struct sge_iq *iq);
static int refill_fl(struct adapter *sc, struct sge_fl *fl, int nbufs);
static void refill_sfl(void *arg);
static void add_fl_to_sfl(struct adapter *sc, struct sge_fl *fl);
static void free_fl_bufs(struct sge_fl *fl);
static mblk_t *get_fl_payload(struct adapter *sc, struct sge_fl *fl,
uint32_t len_newbuf, int *fl_bufs_used);
static int get_frame_txinfo(struct sge_txq *txq, mblk_t **fp,
struct txinfo *txinfo, int sgl_only);
static inline int fits_in_txb(struct sge_txq *txq, int len, int *waste);
static inline int copy_into_txb(struct sge_txq *txq, mblk_t *m, int len,
struct txinfo *txinfo);
static inline void add_seg(struct txinfo *txinfo, uint64_t ba, uint32_t len);
static inline int add_mblk(struct sge_txq *txq, struct txinfo *txinfo,
mblk_t *m, int len);
static void free_txinfo_resources(struct sge_txq *txq, struct txinfo *txinfo);
static int add_to_txpkts(struct sge_txq *txq, struct txpkts *txpkts, mblk_t *m,
struct txinfo *txinfo);
static void write_txpkts_wr(struct sge_txq *txq, struct txpkts *txpkts);
static int write_txpkt_wr(struct port_info *pi, struct sge_txq *txq, mblk_t *m,
struct txinfo *txinfo);
static inline void write_ulp_cpl_sgl(struct port_info *pi, struct sge_txq *txq,
struct txpkts *txpkts, struct txinfo *txinfo);
static inline void copy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to,
int len);
static inline void ring_tx_db(struct adapter *sc, struct sge_eq *eq);
static int reclaim_tx_descs(struct sge_txq *txq, int howmany);
static void write_txqflush_wr(struct sge_txq *txq);
static int t4_eth_rx(struct sge_iq *iq, const struct rss_header *rss,
mblk_t *m);
static inline void ring_fl_db(struct adapter *sc, struct sge_fl *fl);
static kstat_t *setup_port_config_kstats(struct port_info *pi);
static kstat_t *setup_port_info_kstats(struct port_info *pi);
static kstat_t *setup_rxq_kstats(struct port_info *pi, struct sge_rxq *rxq,
int idx);
static int update_rxq_kstats(kstat_t *ksp, int rw);
static int update_port_info_kstats(kstat_t *ksp, int rw);
static kstat_t *setup_txq_kstats(struct port_info *pi, struct sge_txq *txq,
int idx);
static int update_txq_kstats(kstat_t *ksp, int rw);
static int handle_sge_egr_update(struct sge_iq *, const struct rss_header *,
mblk_t *);
static int handle_fw_rpl(struct sge_iq *iq, const struct rss_header *rss,
mblk_t *m);
static inline int
reclaimable(struct sge_eq *eq)
{
unsigned int cidx;
cidx = eq->spg->cidx; /* stable snapshot */
cidx = be16_to_cpu(cidx);
if (cidx >= eq->cidx)
return (cidx - eq->cidx);
else
return (cidx + eq->cap - eq->cidx);
}
void
t4_sge_init(struct adapter *sc)
{
struct driver_properties *p = &sc->props;
ddi_dma_attr_t *dma_attr;
ddi_device_acc_attr_t *acc_attr;
uint32_t sge_control, sge_conm_ctrl;
int egress_threshold;
/*
* Device access and DMA attributes for descriptor rings
*/
acc_attr = &sc->sge.acc_attr_desc;
acc_attr->devacc_attr_version = DDI_DEVICE_ATTR_V0;
acc_attr->devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
acc_attr->devacc_attr_dataorder = DDI_STRICTORDER_ACC;
dma_attr = &sc->sge.dma_attr_desc;
dma_attr->dma_attr_version = DMA_ATTR_V0;
dma_attr->dma_attr_addr_lo = 0;
dma_attr->dma_attr_addr_hi = UINT64_MAX;
dma_attr->dma_attr_count_max = UINT64_MAX;
dma_attr->dma_attr_align = 512;
dma_attr->dma_attr_burstsizes = 0xfff;
dma_attr->dma_attr_minxfer = 1;
dma_attr->dma_attr_maxxfer = UINT64_MAX;
dma_attr->dma_attr_seg = UINT64_MAX;
dma_attr->dma_attr_sgllen = 1;
dma_attr->dma_attr_granular = 1;
dma_attr->dma_attr_flags = 0;
/*
* Device access and DMA attributes for tx buffers
*/
acc_attr = &sc->sge.acc_attr_tx;
acc_attr->devacc_attr_version = DDI_DEVICE_ATTR_V0;
acc_attr->devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
dma_attr = &sc->sge.dma_attr_tx;
dma_attr->dma_attr_version = DMA_ATTR_V0;
dma_attr->dma_attr_addr_lo = 0;
dma_attr->dma_attr_addr_hi = UINT64_MAX;
dma_attr->dma_attr_count_max = UINT64_MAX;
dma_attr->dma_attr_align = 1;
dma_attr->dma_attr_burstsizes = 0xfff;
dma_attr->dma_attr_minxfer = 1;
dma_attr->dma_attr_maxxfer = UINT64_MAX;
dma_attr->dma_attr_seg = UINT64_MAX;
dma_attr->dma_attr_sgllen = TX_SGL_SEGS;
dma_attr->dma_attr_granular = 1;
dma_attr->dma_attr_flags = 0;
/*
* Ingress Padding Boundary and Egress Status Page Size are set up by
* t4_fixup_host_params().
*/
sge_control = t4_read_reg(sc, A_SGE_CONTROL);
sc->sge.pktshift = G_PKTSHIFT(sge_control);
sc->sge.stat_len = (sge_control & F_EGRSTATUSPAGESIZE) ? 128 : 64;
/* t4_nex uses FLM packed mode */
sc->sge.fl_align = t4_fl_pkt_align(sc, true);
/*
* Device access and DMA attributes for rx buffers
*/
sc->sge.rxb_params.dip = sc->dip;
sc->sge.rxb_params.buf_size = rx_buf_size;
acc_attr = &sc->sge.rxb_params.acc_attr_rx;
acc_attr->devacc_attr_version = DDI_DEVICE_ATTR_V0;
acc_attr->devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
dma_attr = &sc->sge.rxb_params.dma_attr_rx;
dma_attr->dma_attr_version = DMA_ATTR_V0;
dma_attr->dma_attr_addr_lo = 0;
dma_attr->dma_attr_addr_hi = UINT64_MAX;
dma_attr->dma_attr_count_max = UINT64_MAX;
/*
* Low 4 bits of an rx buffer address have a special meaning to the SGE
* and an rx buf cannot have an address with any of these bits set.
* FL_ALIGN is >= 32 so we're sure things are ok.
*/
dma_attr->dma_attr_align = sc->sge.fl_align;
dma_attr->dma_attr_burstsizes = 0xfff;
dma_attr->dma_attr_minxfer = 1;
dma_attr->dma_attr_maxxfer = UINT64_MAX;
dma_attr->dma_attr_seg = UINT64_MAX;
dma_attr->dma_attr_sgllen = 1;
dma_attr->dma_attr_granular = 1;
dma_attr->dma_attr_flags = 0;
sc->sge.rxbuf_cache = rxbuf_cache_create(&sc->sge.rxb_params);
/*
* A FL with <= fl_starve_thres buffers is starving and a periodic
* timer will attempt to refill it. This needs to be larger than the
* SGE's Egress Congestion Threshold. If it isn't, then we can get
* stuck waiting for new packets while the SGE is waiting for us to
* give it more Free List entries. (Note that the SGE's Egress
* Congestion Threshold is in units of 2 Free List pointers.) For T4,
* there was only a single field to control this. For T5 there's the
* original field which now only applies to Unpacked Mode Free List
* buffers and a new field which only applies to Packed Mode Free List
* buffers.
*/
sge_conm_ctrl = t4_read_reg(sc, A_SGE_CONM_CTRL);
switch (CHELSIO_CHIP_VERSION(sc->params.chip)) {
case CHELSIO_T4:
egress_threshold = G_EGRTHRESHOLD(sge_conm_ctrl);
break;
case CHELSIO_T5:
egress_threshold = G_EGRTHRESHOLDPACKING(sge_conm_ctrl);
break;
case CHELSIO_T6:
default:
egress_threshold = G_T6_EGRTHRESHOLDPACKING(sge_conm_ctrl);
}
sc->sge.fl_starve_threshold = 2*egress_threshold + 1;
t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE0, rx_buf_size);
t4_write_reg(sc, A_SGE_INGRESS_RX_THRESHOLD,
V_THRESHOLD_0(p->counter_val[0]) |
V_THRESHOLD_1(p->counter_val[1]) |
V_THRESHOLD_2(p->counter_val[2]) |
V_THRESHOLD_3(p->counter_val[3]));
t4_write_reg(sc, A_SGE_TIMER_VALUE_0_AND_1,
V_TIMERVALUE0(us_to_core_ticks(sc, p->timer_val[0])) |
V_TIMERVALUE1(us_to_core_ticks(sc, p->timer_val[1])));
t4_write_reg(sc, A_SGE_TIMER_VALUE_2_AND_3,
V_TIMERVALUE2(us_to_core_ticks(sc, p->timer_val[2])) |
V_TIMERVALUE3(us_to_core_ticks(sc, p->timer_val[3])));
t4_write_reg(sc, A_SGE_TIMER_VALUE_4_AND_5,
V_TIMERVALUE4(us_to_core_ticks(sc, p->timer_val[4])) |
V_TIMERVALUE5(us_to_core_ticks(sc, p->timer_val[5])));
(void) t4_register_cpl_handler(sc, CPL_FW4_MSG, handle_fw_rpl);
(void) t4_register_cpl_handler(sc, CPL_FW6_MSG, handle_fw_rpl);
(void) t4_register_cpl_handler(sc, CPL_SGE_EGR_UPDATE, handle_sge_egr_update);
(void) t4_register_cpl_handler(sc, CPL_RX_PKT, t4_eth_rx);
(void) t4_register_fw_msg_handler(sc, FW6_TYPE_CMD_RPL,
t4_handle_fw_rpl);
}
/*
* Allocate and initialize the firmware event queue and the forwarded interrupt
* queues, if any. The adapter owns all these queues as they are not associated
* with any particular port.
*
* Returns errno on failure. Resources allocated up to that point may still be
* allocated. Caller is responsible for cleanup in case this function fails.
*/
int
t4_setup_adapter_queues(struct adapter *sc)
{
int rc;
ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
/*
* Firmware event queue
*/
rc = alloc_fwq(sc);
if (rc != 0)
return (rc);
#ifdef TCP_OFFLOAD_ENABLE
/*
* Management queue. This is just a control queue that uses the fwq as
* its associated iq.
*/
rc = alloc_mgmtq(sc);
#endif
return (rc);
}
/*
* Idempotent
*/
int
t4_teardown_adapter_queues(struct adapter *sc)
{
ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
(void) free_fwq(sc);
return (0);
}
static inline int
first_vector(struct port_info *pi)
{
struct adapter *sc = pi->adapter;
int rc = T4_EXTRA_INTR, i;
if (sc->intr_count == 1)
return (0);
for_each_port(sc, i) {
struct port_info *p = sc->port[i];
if (i == pi->port_id)
break;
#ifdef TCP_OFFLOAD_ENABLE
if (!(sc->flags & INTR_FWD))
rc += p->nrxq + p->nofldrxq;
else
rc += max(p->nrxq, p->nofldrxq);
#else
/*
* Not compiled with offload support and intr_count > 1. Only
* NIC queues exist and they'd better be taking direct
* interrupts.
*/
ASSERT(!(sc->flags & INTR_FWD));
rc += p->nrxq;
#endif
}
return (rc);
}
/*
* Given an arbitrary "index," come up with an iq that can be used by other
* queues (of this port) for interrupt forwarding, SGE egress updates, etc.
* The iq returned is guaranteed to be something that takes direct interrupts.
*/
static struct sge_iq *
port_intr_iq(struct port_info *pi, int idx)
{
struct adapter *sc = pi->adapter;
struct sge *s = &sc->sge;
struct sge_iq *iq = NULL;
if (sc->intr_count == 1)
return (&sc->sge.fwq);
#ifdef TCP_OFFLOAD_ENABLE
if (!(sc->flags & INTR_FWD)) {
idx %= pi->nrxq + pi->nofldrxq;
if (idx >= pi->nrxq) {
idx -= pi->nrxq;
iq = &s->ofld_rxq[pi->first_ofld_rxq + idx].iq;
} else
iq = &s->rxq[pi->first_rxq + idx].iq;
} else {
idx %= max(pi->nrxq, pi->nofldrxq);
if (pi->nrxq >= pi->nofldrxq)
iq = &s->rxq[pi->first_rxq + idx].iq;
else
iq = &s->ofld_rxq[pi->first_ofld_rxq + idx].iq;
}
#else
/*
* Not compiled with offload support and intr_count > 1. Only NIC
* queues exist and they'd better be taking direct interrupts.
*/
ASSERT(!(sc->flags & INTR_FWD));
idx %= pi->nrxq;
iq = &s->rxq[pi->first_rxq + idx].iq;
#endif
return (iq);
}
int
t4_setup_port_queues(struct port_info *pi)
{
int rc = 0, i, intr_idx, j;
struct sge_rxq *rxq;
struct sge_txq *txq;
#ifdef TCP_OFFLOAD_ENABLE
int iqid;
struct sge_wrq *ctrlq;
struct sge_ofld_rxq *ofld_rxq;
struct sge_wrq *ofld_txq;
#endif
struct adapter *sc = pi->adapter;
struct driver_properties *p = &sc->props;
pi->ksp_config = setup_port_config_kstats(pi);
pi->ksp_info = setup_port_info_kstats(pi);
/* Interrupt vector to start from (when using multiple vectors) */
intr_idx = first_vector(pi);
/*
* First pass over all rx queues (NIC and TOE):
* a) initialize iq and fl
* b) allocate queue iff it will take direct interrupts.
*/
for_each_rxq(pi, i, rxq) {
init_iq(&rxq->iq, sc, pi->tmr_idx, pi->pktc_idx, p->qsize_rxq,
RX_IQ_ESIZE);
init_fl(&rxq->fl, p->qsize_rxq / 8); /* 8 bufs in each entry */
if ((!(sc->flags & INTR_FWD))
#ifdef TCP_OFFLOAD_ENABLE
|| (sc->intr_count > 1 && pi->nrxq >= pi->nofldrxq)
#else
|| (sc->intr_count > 1 && pi->nrxq)
#endif
) {
rxq->iq.flags |= IQ_INTR;
rc = alloc_rxq(pi, rxq, intr_idx, i);
if (rc != 0)
goto done;
intr_idx++;
}
}
#ifdef TCP_OFFLOAD_ENABLE
for_each_ofld_rxq(pi, i, ofld_rxq) {
init_iq(&ofld_rxq->iq, sc, pi->tmr_idx, pi->pktc_idx,
p->qsize_rxq, RX_IQ_ESIZE);
init_fl(&ofld_rxq->fl, p->qsize_rxq / 8);
if (!(sc->flags & INTR_FWD) ||
(sc->intr_count > 1 && pi->nofldrxq > pi->nrxq)) {
ofld_rxq->iq.flags = IQ_INTR;
rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx);
if (rc != 0)
goto done;
intr_idx++;
}
}
#endif
/*
* Second pass over all rx queues (NIC and TOE). The queues forwarding
* their interrupts are allocated now.
*/
j = 0;
for_each_rxq(pi, i, rxq) {
if (rxq->iq.flags & IQ_INTR)
continue;
intr_idx = port_intr_iq(pi, j)->abs_id;
rc = alloc_rxq(pi, rxq, intr_idx, i);
if (rc != 0)
goto done;
j++;
}
#ifdef TCP_OFFLOAD_ENABLE
for_each_ofld_rxq(pi, i, ofld_rxq) {
if (ofld_rxq->iq.flags & IQ_INTR)
continue;
intr_idx = port_intr_iq(pi, j)->abs_id;
rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx);
if (rc != 0)
goto done;
j++;
}
#endif
/*
* Now the tx queues. Only one pass needed.
*/
j = 0;
for_each_txq(pi, i, txq) {
uint16_t iqid;
iqid = port_intr_iq(pi, j)->cntxt_id;
init_eq(sc, &txq->eq, EQ_ETH, p->qsize_txq, pi->tx_chan, iqid);
rc = alloc_txq(pi, txq, i);
if (rc != 0)
goto done;
}
#ifdef TCP_OFFLOAD_ENABLE
for_each_ofld_txq(pi, i, ofld_txq) {
uint16_t iqid;
iqid = port_intr_iq(pi, j)->cntxt_id;
init_eq(sc, &ofld_txq->eq, EQ_OFLD, p->qsize_txq, pi->tx_chan,
iqid);
rc = alloc_wrq(sc, pi, ofld_txq, i);
if (rc != 0)
goto done;
}
/*
* Finally, the control queue.
*/
ctrlq = &sc->sge.ctrlq[pi->port_id];
iqid = port_intr_iq(pi, 0)->cntxt_id;
init_eq(sc, &ctrlq->eq, EQ_CTRL, CTRL_EQ_QSIZE, pi->tx_chan, iqid);
rc = alloc_wrq(sc, pi, ctrlq, 0);
#endif
done:
if (rc != 0)
(void) t4_teardown_port_queues(pi);
return (rc);
}
/*
* Idempotent
*/
int
t4_teardown_port_queues(struct port_info *pi)
{
int i;
struct sge_rxq *rxq;
struct sge_txq *txq;
#ifdef TCP_OFFLOAD_ENABLE
struct adapter *sc = pi->adapter;
struct sge_ofld_rxq *ofld_rxq;
struct sge_wrq *ofld_txq;
#endif
if (pi->ksp_config != NULL) {
kstat_delete(pi->ksp_config);
pi->ksp_config = NULL;
}
if (pi->ksp_info != NULL) {
kstat_delete(pi->ksp_info);
pi->ksp_info = NULL;
}
#ifdef TCP_OFFLOAD_ENABLE
(void) free_wrq(sc, &sc->sge.ctrlq[pi->port_id]);
#endif
for_each_txq(pi, i, txq) {
(void) free_txq(pi, txq);
}
#ifdef TCP_OFFLOAD_ENABLE
for_each_ofld_txq(pi, i, ofld_txq) {
(void) free_wrq(sc, ofld_txq);
}
for_each_ofld_rxq(pi, i, ofld_rxq) {
if ((ofld_rxq->iq.flags & IQ_INTR) == 0)
(void) free_ofld_rxq(pi, ofld_rxq);
}
#endif
for_each_rxq(pi, i, rxq) {
if ((rxq->iq.flags & IQ_INTR) == 0)
(void) free_rxq(pi, rxq);
}
/*
* Then take down the rx queues that take direct interrupts.
*/
for_each_rxq(pi, i, rxq) {
if (rxq->iq.flags & IQ_INTR)
(void) free_rxq(pi, rxq);
}
#ifdef TCP_OFFLOAD_ENABLE
for_each_ofld_rxq(pi, i, ofld_rxq) {
if (ofld_rxq->iq.flags & IQ_INTR)
(void) free_ofld_rxq(pi, ofld_rxq);
}
#endif
return (0);
}
/* Deals with errors and forwarded interrupts */
uint_t
t4_intr_all(caddr_t arg1, caddr_t arg2)
{
(void) t4_intr_err(arg1, arg2);
(void) t4_intr(arg1, arg2);
return (DDI_INTR_CLAIMED);
}
static void
t4_intr_rx_work(struct sge_iq *iq)
{
mblk_t *mp = NULL;
struct sge_rxq *rxq = iq_to_rxq(iq); /* Use iff iq is part of rxq */
RXQ_LOCK(rxq);
if (!iq->polling) {
mp = t4_ring_rx(rxq, iq->qsize/8);
t4_write_reg(iq->adapter, MYPF_REG(A_SGE_PF_GTS),
V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_next));
}
RXQ_UNLOCK(rxq);
if (mp != NULL)
mac_rx_ring(rxq->port->mh, rxq->ring_handle, mp,
rxq->ring_gen_num);
}
/* Deals with interrupts on the given ingress queue */
/* ARGSUSED */
uint_t
t4_intr(caddr_t arg1, caddr_t arg2)
{
struct sge_iq *iq = (struct sge_iq *)arg2;
int state;
/* Right now receive polling is only enabled for MSI-X and
* when we have enough msi-x vectors i.e no interrupt forwarding.
*/
if (iq->adapter->props.multi_rings) {
t4_intr_rx_work(iq);
} else {
state = atomic_cas_uint(&iq->state, IQS_IDLE, IQS_BUSY);
if (state == IQS_IDLE) {
(void) service_iq(iq, 0);
(void) atomic_cas_uint(&iq->state, IQS_BUSY, IQS_IDLE);
}
}
return (DDI_INTR_CLAIMED);
}
/* Deals with error interrupts */
/* ARGSUSED */
uint_t
t4_intr_err(caddr_t arg1, caddr_t arg2)
{
/* LINTED: E_BAD_PTR_CAST_ALIGN */
struct adapter *sc = (struct adapter *)arg1;
t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0);
(void) t4_slow_intr_handler(sc);
return (DDI_INTR_CLAIMED);
}
/*
* t4_ring_rx - Process responses from an SGE response queue.
*
* This function processes responses from an SGE response queue up to the supplied budget.
* Responses include received packets as well as control messages from FW
* or HW.
* It returns a chain of mblks containing the received data, to be
* passed up to mac_ring_rx().
*/
mblk_t *
t4_ring_rx(struct sge_rxq *rxq, int budget)
{
struct sge_iq *iq = &rxq->iq;
struct sge_fl *fl = &rxq->fl; /* Use iff IQ_HAS_FL */
struct adapter *sc = iq->adapter;
struct rsp_ctrl *ctrl;
const struct rss_header *rss;
int ndescs = 0, fl_bufs_used = 0;
int rsp_type;
uint32_t lq;
mblk_t *mblk_head = NULL, **mblk_tail, *m;
struct cpl_rx_pkt *cpl;
uint32_t received_bytes = 0, pkt_len = 0;
bool csum_ok;
uint16_t err_vec;
mblk_tail = &mblk_head;
while (is_new_response(iq, &ctrl)) {
membar_consumer();
m = NULL;
rsp_type = G_RSPD_TYPE(ctrl->u.type_gen);
lq = be32_to_cpu(ctrl->pldbuflen_qid);
rss = (const void *)iq->cdesc;
switch (rsp_type) {
case X_RSPD_TYPE_FLBUF:
ASSERT(iq->flags & IQ_HAS_FL);
if (CPL_RX_PKT == rss->opcode) {
cpl = (void *)(rss + 1);
pkt_len = be16_to_cpu(cpl->len);
if (iq->polling && ((received_bytes + pkt_len) > budget))
goto done;
m = get_fl_payload(sc, fl, lq, &fl_bufs_used);
if (m == NULL)
goto done;
iq->intr_next = iq->intr_params;
m->b_rptr += sc->sge.pktshift;
if (sc->params.tp.rx_pkt_encap)
/* It is enabled only in T6 config file */
err_vec = G_T6_COMPR_RXERR_VEC(ntohs(cpl->err_vec));
else
err_vec = ntohs(cpl->err_vec);
csum_ok = cpl->csum_calc && !err_vec;
/* TODO: what about cpl->ip_frag? */
if (csum_ok && !cpl->ip_frag) {
mac_hcksum_set(m, 0, 0, 0, 0xffff,
HCK_FULLCKSUM_OK | HCK_FULLCKSUM |
HCK_IPV4_HDRCKSUM_OK);
rxq->rxcsum++;
}
rxq->rxpkts++;
rxq->rxbytes += pkt_len;
received_bytes += pkt_len;
*mblk_tail = m;
mblk_tail = &m->b_next;
break;
}
m = get_fl_payload(sc, fl, lq, &fl_bufs_used);
if (m == NULL)
goto done;
/* FALLTHROUGH */
case X_RSPD_TYPE_CPL:
ASSERT(rss->opcode < NUM_CPL_CMDS);
sc->cpl_handler[rss->opcode](iq, rss, m);
break;
default:
break;
}
iq_next(iq);
++ndescs;
if (!iq->polling && (ndescs == budget))
break;
}
done:
t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS),
V_CIDXINC(ndescs) | V_INGRESSQID(iq->cntxt_id) |
V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
if ((fl_bufs_used > 0) || (iq->flags & IQ_HAS_FL)) {
int starved;
FL_LOCK(fl);
fl->needed += fl_bufs_used;
starved = refill_fl(sc, fl, fl->cap / 8);
FL_UNLOCK(fl);
if (starved)
add_fl_to_sfl(sc, fl);
}
return (mblk_head);
}
/*
* Deals with anything and everything on the given ingress queue.
*/
static int
service_iq(struct sge_iq *iq, int budget)
{
struct sge_iq *q;
struct sge_rxq *rxq = iq_to_rxq(iq); /* Use iff iq is part of rxq */
struct sge_fl *fl = &rxq->fl; /* Use iff IQ_HAS_FL */
struct adapter *sc = iq->adapter;
struct rsp_ctrl *ctrl;
const struct rss_header *rss;
int ndescs = 0, limit, fl_bufs_used = 0;
int rsp_type;
uint32_t lq;
int starved;
mblk_t *m;
STAILQ_HEAD(, sge_iq) iql = STAILQ_HEAD_INITIALIZER(iql);
limit = budget ? budget : iq->qsize / 8;
/*
* We always come back and check the descriptor ring for new indirect
* interrupts and other responses after running a single handler.
*/
for (;;) {
while (is_new_response(iq, &ctrl)) {
membar_consumer();
m = NULL;
rsp_type = G_RSPD_TYPE(ctrl->u.type_gen);
lq = be32_to_cpu(ctrl->pldbuflen_qid);
rss = (const void *)iq->cdesc;
switch (rsp_type) {
case X_RSPD_TYPE_FLBUF:
ASSERT(iq->flags & IQ_HAS_FL);
m = get_fl_payload(sc, fl, lq, &fl_bufs_used);
if (m == NULL) {
/*
* Rearm the iq with a
* longer-than-default timer
*/
t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_CIDXINC(ndescs) |
V_INGRESSQID((u32)iq->cntxt_id) |
V_SEINTARM(V_QINTR_TIMER_IDX(SGE_NTIMERS-1)));
if (fl_bufs_used > 0) {
ASSERT(iq->flags & IQ_HAS_FL);
FL_LOCK(fl);
fl->needed += fl_bufs_used;
starved = refill_fl(sc, fl, fl->cap / 8);
FL_UNLOCK(fl);
if (starved)
add_fl_to_sfl(sc, fl);
}
return (0);
}
/* FALLTHRU */
case X_RSPD_TYPE_CPL:
ASSERT(rss->opcode < NUM_CPL_CMDS);
sc->cpl_handler[rss->opcode](iq, rss, m);
break;
case X_RSPD_TYPE_INTR:
/*
* Interrupts should be forwarded only to queues
* that are not forwarding their interrupts.
* This means service_iq can recurse but only 1
* level deep.
*/
ASSERT(budget == 0);
q = sc->sge.iqmap[lq - sc->sge.iq_start];
if (atomic_cas_uint(&q->state, IQS_IDLE,
IQS_BUSY) == IQS_IDLE) {
if (service_iq(q, q->qsize / 8) == 0) {
(void) atomic_cas_uint(
&q->state, IQS_BUSY,
IQS_IDLE);
} else {
STAILQ_INSERT_TAIL(&iql, q,
link);
}
}
break;
default:
break;
}
iq_next(iq);
if (++ndescs == limit) {
t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS),
V_CIDXINC(ndescs) |
V_INGRESSQID(iq->cntxt_id) |
V_SEINTARM(V_QINTR_TIMER_IDX(
X_TIMERREG_UPDATE_CIDX)));
ndescs = 0;
if (fl_bufs_used > 0) {
ASSERT(iq->flags & IQ_HAS_FL);
FL_LOCK(fl);
fl->needed += fl_bufs_used;
(void) refill_fl(sc, fl, fl->cap / 8);
FL_UNLOCK(fl);
fl_bufs_used = 0;
}
if (budget != 0)
return (EINPROGRESS);
}
}
if (STAILQ_EMPTY(&iql) != 0)
break;
/*
* Process the head only, and send it to the back of the list if
* it's still not done.
*/
q = STAILQ_FIRST(&iql);
STAILQ_REMOVE_HEAD(&iql, link);
if (service_iq(q, q->qsize / 8) == 0)
(void) atomic_cas_uint(&q->state, IQS_BUSY, IQS_IDLE);
else
STAILQ_INSERT_TAIL(&iql, q, link);
}
t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_CIDXINC(ndescs) |
V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_next));
if (iq->flags & IQ_HAS_FL) {
FL_LOCK(fl);
fl->needed += fl_bufs_used;
starved = refill_fl(sc, fl, fl->cap / 4);
FL_UNLOCK(fl);
if (starved != 0)
add_fl_to_sfl(sc, fl);
}
return (0);
}
#ifdef TCP_OFFLOAD_ENABLE
int
t4_mgmt_tx(struct adapter *sc, mblk_t *m)
{
return (t4_wrq_tx(sc, &sc->sge.mgmtq, m));
}
/*
* Doesn't fail. Holds on to work requests it can't send right away.
*/
int
t4_wrq_tx_locked(struct adapter *sc, struct sge_wrq *wrq, mblk_t *m0)
{
struct sge_eq *eq = &wrq->eq;
struct mblk_pair *wr_list = &wrq->wr_list;
int can_reclaim;
caddr_t dst;
mblk_t *wr, *next;
TXQ_LOCK_ASSERT_OWNED(wrq);
#ifdef TCP_OFFLOAD_ENABLE
ASSERT((eq->flags & EQ_TYPEMASK) == EQ_OFLD ||
(eq->flags & EQ_TYPEMASK) == EQ_CTRL);
#else
ASSERT((eq->flags & EQ_TYPEMASK) == EQ_CTRL);
#endif
if (m0 != NULL) {
if (wr_list->head != NULL)
wr_list->tail->b_next = m0;
else
wr_list->head = m0;
while (m0->b_next)
m0 = m0->b_next;
wr_list->tail = m0;
}
can_reclaim = reclaimable(eq);
eq->cidx += can_reclaim;
eq->avail += can_reclaim;
if (eq->cidx >= eq->cap)
eq->cidx -= eq->cap;
for (wr = wr_list->head; wr; wr = next) {
int ndesc, len = 0;
mblk_t *m;
next = wr->b_next;
wr->b_next = NULL;
for (m = wr; m; m = m->b_cont)
len += MBLKL(m);
ASSERT(len > 0 && (len & 0x7) == 0);
ASSERT(len <= SGE_MAX_WR_LEN);
ndesc = howmany(len, EQ_ESIZE);
if (eq->avail < ndesc) {
wr->b_next = next;
wrq->no_desc++;
break;
}
dst = (void *)&eq->desc[eq->pidx];
for (m = wr; m; m = m->b_cont)
copy_to_txd(eq, (void *)m->b_rptr, &dst, MBLKL(m));
eq->pidx += ndesc;
eq->avail -= ndesc;
if (eq->pidx >= eq->cap)
eq->pidx -= eq->cap;
eq->pending += ndesc;
if (eq->pending > 16)
ring_tx_db(sc, eq);
wrq->tx_wrs++;
freemsg(wr);
if (eq->avail < 8) {
can_reclaim = reclaimable(eq);
eq->cidx += can_reclaim;
eq->avail += can_reclaim;
if (eq->cidx >= eq->cap)
eq->cidx -= eq->cap;
}
}
if (eq->pending != 0)
ring_tx_db(sc, eq);
if (wr == NULL)
wr_list->head = wr_list->tail = NULL;
else {
wr_list->head = wr;
ASSERT(wr_list->tail->b_next == NULL);
}
return (0);
}
#endif
/* Per-packet header in a coalesced tx WR, before the SGL starts (in flits) */
#define TXPKTS_PKT_HDR ((\
sizeof (struct ulp_txpkt) + \
sizeof (struct ulptx_idata) + \
sizeof (struct cpl_tx_pkt_core)) / 8)
/* Header of a coalesced tx WR, before SGL of first packet (in flits) */
#define TXPKTS_WR_HDR (\
sizeof (struct fw_eth_tx_pkts_wr) / 8 + \
TXPKTS_PKT_HDR)
/* Header of a tx WR, before SGL of first packet (in flits) */
#define TXPKT_WR_HDR ((\
sizeof (struct fw_eth_tx_pkt_wr) + \
sizeof (struct cpl_tx_pkt_core)) / 8)
/* Header of a tx LSO WR, before SGL of first packet (in flits) */
#define TXPKT_LSO_WR_HDR ((\
sizeof (struct fw_eth_tx_pkt_wr) + \
sizeof(struct cpl_tx_pkt_lso_core) + \
sizeof (struct cpl_tx_pkt_core)) / 8)
mblk_t *
t4_eth_tx(void *arg, mblk_t *frame)
{
struct sge_txq *txq = (struct sge_txq *) arg;
struct port_info *pi = txq->port;
struct adapter *sc = pi->adapter;
struct sge_eq *eq = &txq->eq;
mblk_t *next_frame;
int rc, coalescing;
struct txpkts txpkts;
struct txinfo txinfo;
txpkts.npkt = 0; /* indicates there's nothing in txpkts */
coalescing = 0;
TXQ_LOCK(txq);
if (eq->avail < 8)
(void) reclaim_tx_descs(txq, 8);
for (; frame; frame = next_frame) {
if (eq->avail < 8)
break;
next_frame = frame->b_next;
frame->b_next = NULL;
if (next_frame != NULL)
coalescing = 1;
rc = get_frame_txinfo(txq, &frame, &txinfo, coalescing);
if (rc != 0) {
if (rc == ENOMEM) {
/* Short of resources, suspend tx */
frame->b_next = next_frame;
break;
}
/*
* Unrecoverable error for this frame, throw it
* away and move on to the next.
*/
freemsg(frame);
continue;
}
if (coalescing != 0 &&
add_to_txpkts(txq, &txpkts, frame, &txinfo) == 0) {
/* Successfully absorbed into txpkts */
write_ulp_cpl_sgl(pi, txq, &txpkts, &txinfo);
goto doorbell;
}
/*
* We weren't coalescing to begin with, or current frame could
* not be coalesced (add_to_txpkts flushes txpkts if a frame
* given to it can't be coalesced). Either way there should be
* nothing in txpkts.
*/
ASSERT(txpkts.npkt == 0);
/* We're sending out individual frames now */
coalescing = 0;
if (eq->avail < 8)
(void) reclaim_tx_descs(txq, 8);
rc = write_txpkt_wr(pi, txq, frame, &txinfo);
if (rc != 0) {
/* Short of hardware descriptors, suspend tx */
/*
* This is an unlikely but expensive failure. We've
* done all the hard work (DMA bindings etc.) and now we
* can't send out the frame. What's worse, we have to
* spend even more time freeing up everything in txinfo.
*/
txq->qfull++;
free_txinfo_resources(txq, &txinfo);
frame->b_next = next_frame;
break;
}
doorbell:
/* Fewer and fewer doorbells as the queue fills up */
if (eq->pending >= (1 << (fls(eq->qsize - eq->avail) / 2))) {
txq->txbytes += txinfo.len;
txq->txpkts++;
ring_tx_db(sc, eq);
}
(void) reclaim_tx_descs(txq, 32);
}
if (txpkts.npkt > 0)
write_txpkts_wr(txq, &txpkts);
/*
* frame not NULL means there was an error but we haven't thrown it
* away. This can happen when we're short of tx descriptors (qfull) or
* maybe even DMA handles (dma_hdl_failed). Either way, a credit flush
* and reclaim will get things going again.
*
* If eq->avail is already 0 we know a credit flush was requested in the
* WR that reduced it to 0 so we don't need another flush (we don't have
* any descriptor for a flush WR anyway, duh).
*/
if (frame && eq->avail > 0)
write_txqflush_wr(txq);
if (eq->pending != 0)
ring_tx_db(sc, eq);
(void) reclaim_tx_descs(txq, eq->qsize);
TXQ_UNLOCK(txq);
return (frame);
}
static inline void
init_iq(struct sge_iq *iq, struct adapter *sc, int tmr_idx, int8_t pktc_idx,
int qsize, uint8_t esize)
{
ASSERT(tmr_idx >= 0 && tmr_idx < SGE_NTIMERS);
ASSERT(pktc_idx < SGE_NCOUNTERS); /* -ve is ok, means don't use */
iq->flags = 0;
iq->adapter = sc;
iq->intr_params = V_QINTR_TIMER_IDX(tmr_idx);
iq->intr_pktc_idx = SGE_NCOUNTERS - 1;
if (pktc_idx >= 0) {
iq->intr_params |= F_QINTR_CNT_EN;
iq->intr_pktc_idx = pktc_idx;
}
iq->qsize = roundup(qsize, 16); /* See FW_IQ_CMD/iqsize */
iq->esize = max(esize, 16); /* See FW_IQ_CMD/iqesize */
}
static inline void
init_fl(struct sge_fl *fl, uint16_t qsize)
{
fl->qsize = qsize;
fl->allocb_fail = 0;
}
static inline void
init_eq(struct adapter *sc, struct sge_eq *eq, uint16_t eqtype, uint16_t qsize,
uint8_t tx_chan, uint16_t iqid)
{
struct sge *s = &sc->sge;
uint32_t r;
ASSERT(tx_chan < NCHAN);
ASSERT(eqtype <= EQ_TYPEMASK);
if (is_t5(sc->params.chip)) {
r = t4_read_reg(sc, A_SGE_EGRESS_QUEUES_PER_PAGE_PF);
r >>= S_QUEUESPERPAGEPF0 +
(S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf;
s->s_qpp = r & M_QUEUESPERPAGEPF0;
}
eq->flags = eqtype & EQ_TYPEMASK;
eq->tx_chan = tx_chan;
eq->iqid = iqid;
eq->qsize = qsize;
}
/*
* Allocates the ring for an ingress queue and an optional freelist. If the
* freelist is specified it will be allocated and then associated with the
* ingress queue.
*
* Returns errno on failure. Resources allocated up to that point may still be
* allocated. Caller is responsible for cleanup in case this function fails.
*
* If the ingress queue will take interrupts directly (iq->flags & IQ_INTR) then
* the intr_idx specifies the vector, starting from 0. Otherwise it specifies
* the index of the queue to which its interrupts will be forwarded.
*/
static int
alloc_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl,
int intr_idx, int cong)
{
int rc, i, cntxt_id;
size_t len;
struct fw_iq_cmd c;
struct adapter *sc = iq->adapter;
uint32_t v = 0;
len = iq->qsize * iq->esize;
rc = alloc_desc_ring(sc, len, DDI_DMA_READ, &iq->dhdl, &iq->ahdl,
&iq->ba, (caddr_t *)&iq->desc);
if (rc != 0)
return (rc);
bzero(&c, sizeof (c));
c.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
V_FW_IQ_CMD_VFN(0));
c.alloc_to_len16 = cpu_to_be32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
FW_LEN16(c));
/* Special handling for firmware event queue */
if (iq == &sc->sge.fwq)
v |= F_FW_IQ_CMD_IQASYNCH;
if (iq->flags & IQ_INTR)
ASSERT(intr_idx < sc->intr_count);
else
v |= F_FW_IQ_CMD_IQANDST;
v |= V_FW_IQ_CMD_IQANDSTINDEX(intr_idx);
c.type_to_iqandstindex = cpu_to_be32(v |
V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
V_FW_IQ_CMD_VIID(pi->viid) |
V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
c.iqdroprss_to_iqesize = cpu_to_be16(V_FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
F_FW_IQ_CMD_IQGTSMODE |
V_FW_IQ_CMD_IQINTCNTTHRESH(iq->intr_pktc_idx) |
V_FW_IQ_CMD_IQESIZE(ilog2(iq->esize) - 4));
c.iqsize = cpu_to_be16(iq->qsize);
c.iqaddr = cpu_to_be64(iq->ba);
if (cong >= 0)
c.iqns_to_fl0congen = BE_32(F_FW_IQ_CMD_IQFLINTCONGEN |
V_FW_IQ_CMD_IQTYPE(cong ?
FW_IQ_IQTYPE_NIC : FW_IQ_IQTYPE_OFLD));
if (fl != NULL) {
unsigned int chip_ver = CHELSIO_CHIP_VERSION(sc->params.chip);
mutex_init(&fl->lock, NULL, MUTEX_DRIVER,
DDI_INTR_PRI(sc->intr_pri));
fl->flags |= FL_MTX;
len = fl->qsize * RX_FL_ESIZE;
rc = alloc_desc_ring(sc, len, DDI_DMA_WRITE, &fl->dhdl,
&fl->ahdl, &fl->ba, (caddr_t *)&fl->desc);
if (rc != 0)
return (rc);
/* Allocate space for one software descriptor per buffer. */
fl->cap = (fl->qsize - sc->sge.stat_len / RX_FL_ESIZE) * 8;
fl->sdesc = kmem_zalloc(sizeof (struct fl_sdesc) * fl->cap,
KM_SLEEP);
fl->needed = fl->cap;
fl->lowat = roundup(sc->sge.fl_starve_threshold, 8);
c.iqns_to_fl0congen |=
cpu_to_be32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
F_FW_IQ_CMD_FL0PACKEN | F_FW_IQ_CMD_FL0PADEN);
if (cong >= 0) {
c.iqns_to_fl0congen |=
BE_32(V_FW_IQ_CMD_FL0CNGCHMAP(cong) |
F_FW_IQ_CMD_FL0CONGCIF |
F_FW_IQ_CMD_FL0CONGEN);
}
/* In T6, for egress queue type FL there is internal overhead
* of 16B for header going into FLM module. Hence the maximum
* allowed burst size is 448 bytes. For T4/T5, the hardware
* doesn't coalesce fetch requests if more than 64 bytes of
* Free List pointers are provided, so we use a 128-byte Fetch
* Burst Minimum there (T6 implements coalescing so we can use
* the smaller 64-byte value there).
*/
c.fl0dcaen_to_fl0cidxfthresh =
cpu_to_be16(V_FW_IQ_CMD_FL0FBMIN(chip_ver <= CHELSIO_T5
? X_FETCHBURSTMIN_128B
: X_FETCHBURSTMIN_64B) |
V_FW_IQ_CMD_FL0FBMAX(chip_ver <= CHELSIO_T5
? X_FETCHBURSTMAX_512B
: X_FETCHBURSTMAX_256B));
c.fl0size = cpu_to_be16(fl->qsize);
c.fl0addr = cpu_to_be64(fl->ba);
}
rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof (c), &c);
if (rc != 0) {
cxgb_printf(sc->dip, CE_WARN,
"failed to create ingress queue: %d", rc);
return (rc);
}
iq->cdesc = iq->desc;
iq->cidx = 0;
iq->gen = 1;
iq->intr_next = iq->intr_params;
iq->adapter = sc;
iq->cntxt_id = be16_to_cpu(c.iqid);
iq->abs_id = be16_to_cpu(c.physiqid);
iq->flags |= IQ_ALLOCATED;
mutex_init(&iq->lock, NULL,
MUTEX_DRIVER, DDI_INTR_PRI(DDI_INTR_PRI(sc->intr_pri)));
iq->polling = 0;
cntxt_id = iq->cntxt_id - sc->sge.iq_start;
if (cntxt_id >= sc->sge.iqmap_sz) {
panic("%s: iq->cntxt_id (%d) more than the max (%d)", __func__,
cntxt_id, sc->sge.iqmap_sz - 1);
}
sc->sge.iqmap[cntxt_id] = iq;
if (fl != NULL) {
fl->cntxt_id = be16_to_cpu(c.fl0id);
fl->pidx = fl->cidx = 0;
fl->copy_threshold = rx_copy_threshold;
cntxt_id = fl->cntxt_id - sc->sge.eq_start;
if (cntxt_id >= sc->sge.eqmap_sz) {
panic("%s: fl->cntxt_id (%d) more than the max (%d)",
__func__, cntxt_id, sc->sge.eqmap_sz - 1);
}
sc->sge.eqmap[cntxt_id] = (void *)fl;
FL_LOCK(fl);
(void) refill_fl(sc, fl, fl->lowat);
FL_UNLOCK(fl);
iq->flags |= IQ_HAS_FL;
}
if (is_t5(sc->params.chip) && cong >= 0) {
uint32_t param, val;
param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
V_FW_PARAMS_PARAM_YZ(iq->cntxt_id);
if (cong == 0)
val = 1 << 19;
else {
val = 2 << 19;
for (i = 0; i < 4; i++) {
if (cong & (1 << i))
val |= 1 << (i << 2);
}
}
rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
if (rc != 0) {
/* report error but carry on */
cxgb_printf(sc->dip, CE_WARN,
"failed to set congestion manager context for "
"ingress queue %d: %d", iq->cntxt_id, rc);
}
}
/* Enable IQ interrupts */
iq->state = IQS_IDLE;
t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_SEINTARM(iq->intr_params) |
V_INGRESSQID(iq->cntxt_id));
return (0);
}
static int
free_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl)
{
int rc;
if (iq != NULL) {
struct adapter *sc = iq->adapter;
dev_info_t *dip;
dip = pi ? pi->dip : sc->dip;
if (iq->flags & IQ_ALLOCATED) {
rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0,
FW_IQ_TYPE_FL_INT_CAP, iq->cntxt_id,
fl ? fl->cntxt_id : 0xffff, 0xffff);
if (rc != 0) {
cxgb_printf(dip, CE_WARN,
"failed to free queue %p: %d", iq, rc);
return (rc);
}
mutex_destroy(&iq->lock);
iq->flags &= ~IQ_ALLOCATED;
}
if (iq->desc != NULL) {
(void) free_desc_ring(&iq->dhdl, &iq->ahdl);
iq->desc = NULL;
}
bzero(iq, sizeof (*iq));
}
if (fl != NULL) {
if (fl->sdesc != NULL) {
FL_LOCK(fl);
free_fl_bufs(fl);
FL_UNLOCK(fl);
kmem_free(fl->sdesc, sizeof (struct fl_sdesc) *
fl->cap);
fl->sdesc = NULL;
}
if (fl->desc != NULL) {
(void) free_desc_ring(&fl->dhdl, &fl->ahdl);
fl->desc = NULL;
}
if (fl->flags & FL_MTX) {
mutex_destroy(&fl->lock);
fl->flags &= ~FL_MTX;
}
bzero(fl, sizeof (struct sge_fl));
}
return (0);
}
static int
alloc_fwq(struct adapter *sc)
{
int rc, intr_idx;
struct sge_iq *fwq = &sc->sge.fwq;
init_iq(fwq, sc, 0, 0, FW_IQ_QSIZE, FW_IQ_ESIZE);
fwq->flags |= IQ_INTR; /* always */
intr_idx = sc->intr_count > 1 ? 1 : 0;
rc = alloc_iq_fl(sc->port[0], fwq, NULL, intr_idx, -1);
if (rc != 0) {
cxgb_printf(sc->dip, CE_WARN,
"failed to create firmware event queue: %d.", rc);
return (rc);
}
return (0);
}
static int
free_fwq(struct adapter *sc)
{
return (free_iq_fl(NULL, &sc->sge.fwq, NULL));
}
#ifdef TCP_OFFLOAD_ENABLE
static int
alloc_mgmtq(struct adapter *sc)
{
int rc;
struct sge_wrq *mgmtq = &sc->sge.mgmtq;
init_eq(sc, &mgmtq->eq, EQ_CTRL, CTRL_EQ_QSIZE, sc->port[0]->tx_chan,
sc->sge.fwq.cntxt_id);
rc = alloc_wrq(sc, NULL, mgmtq, 0);
if (rc != 0) {
cxgb_printf(sc->dip, CE_WARN,
"failed to create management queue: %d\n", rc);
return (rc);
}
return (0);
}
#endif
static int
alloc_rxq(struct port_info *pi, struct sge_rxq *rxq, int intr_idx, int i)
{
int rc;
rxq->port = pi;
rc = alloc_iq_fl(pi, &rxq->iq, &rxq->fl, intr_idx,
t4_get_tp_ch_map(pi->adapter, pi->tx_chan));
if (rc != 0)
return (rc);
rxq->ksp = setup_rxq_kstats(pi, rxq, i);
return (rc);
}
static int
free_rxq(struct port_info *pi, struct sge_rxq *rxq)
{
int rc;
if (rxq->ksp != NULL) {
kstat_delete(rxq->ksp);
rxq->ksp = NULL;
}
rc = free_iq_fl(pi, &rxq->iq, &rxq->fl);
if (rc == 0)
bzero(&rxq->fl, sizeof (*rxq) - offsetof(struct sge_rxq, fl));
return (rc);
}
#ifdef TCP_OFFLOAD_ENABLE
static int
alloc_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq,
int intr_idx)
{
int rc;
rc = alloc_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl, intr_idx,
t4_get_tp_ch_map(pi->adapter, pi->tx_chan));
if (rc != 0)
return (rc);
return (rc);
}
static int
free_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq)
{
int rc;
rc = free_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl);
if (rc == 0)
bzero(&ofld_rxq->fl, sizeof (*ofld_rxq) -
offsetof(struct sge_ofld_rxq, fl));
return (rc);
}
#endif
static int
ctrl_eq_alloc(struct adapter *sc, struct sge_eq *eq)
{
int rc, cntxt_id;
struct fw_eq_ctrl_cmd c;
bzero(&c, sizeof (c));
c.op_to_vfn = BE_32(V_FW_CMD_OP(FW_EQ_CTRL_CMD) | F_FW_CMD_REQUEST |
F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_CTRL_CMD_PFN(sc->pf) |
V_FW_EQ_CTRL_CMD_VFN(0));
c.alloc_to_len16 = BE_32(F_FW_EQ_CTRL_CMD_ALLOC |
F_FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
c.cmpliqid_eqid = htonl(V_FW_EQ_CTRL_CMD_CMPLIQID(eq->iqid)); /* TODO */
c.physeqid_pkd = BE_32(0);
c.fetchszm_to_iqid =
BE_32(V_FW_EQ_CTRL_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
V_FW_EQ_CTRL_CMD_PCIECHN(eq->tx_chan) |
F_FW_EQ_CTRL_CMD_FETCHRO | V_FW_EQ_CTRL_CMD_IQID(eq->iqid));
c.dcaen_to_eqsize =
BE_32(V_FW_EQ_CTRL_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
V_FW_EQ_CTRL_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
V_FW_EQ_CTRL_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
V_FW_EQ_CTRL_CMD_EQSIZE(eq->qsize));
c.eqaddr = BE_64(eq->ba);
rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof (c), &c);
if (rc != 0) {
cxgb_printf(sc->dip, CE_WARN,
"failed to create control queue %d: %d", eq->tx_chan, rc);
return (rc);
}
eq->flags |= EQ_ALLOCATED;
eq->cntxt_id = G_FW_EQ_CTRL_CMD_EQID(BE_32(c.cmpliqid_eqid));
cntxt_id = eq->cntxt_id - sc->sge.eq_start;
if (cntxt_id >= sc->sge.eqmap_sz)
panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
cntxt_id, sc->sge.eqmap_sz - 1);
sc->sge.eqmap[cntxt_id] = eq;
return (rc);
}
static int
eth_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
{
int rc, cntxt_id;
struct fw_eq_eth_cmd c;
bzero(&c, sizeof (c));
c.op_to_vfn = BE_32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) |
V_FW_EQ_ETH_CMD_VFN(0));
c.alloc_to_len16 = BE_32(F_FW_EQ_ETH_CMD_ALLOC |
F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
c.autoequiqe_to_viid = BE_32(F_FW_EQ_ETH_CMD_AUTOEQUIQE |
F_FW_EQ_ETH_CMD_AUTOEQUEQE | V_FW_EQ_ETH_CMD_VIID(pi->viid));
c.fetchszm_to_iqid =
BE_32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
V_FW_EQ_ETH_CMD_PCIECHN(eq->tx_chan) | F_FW_EQ_ETH_CMD_FETCHRO |
V_FW_EQ_ETH_CMD_IQID(eq->iqid));
c.dcaen_to_eqsize = BE_32(V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
V_FW_EQ_ETH_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
V_FW_EQ_ETH_CMD_EQSIZE(eq->qsize));
c.eqaddr = BE_64(eq->ba);
rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof (c), &c);
if (rc != 0) {
cxgb_printf(pi->dip, CE_WARN,
"failed to create Ethernet egress queue: %d", rc);
return (rc);
}
eq->flags |= EQ_ALLOCATED;
eq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(BE_32(c.eqid_pkd));
cntxt_id = eq->cntxt_id - sc->sge.eq_start;
if (cntxt_id >= sc->sge.eqmap_sz)
panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
cntxt_id, sc->sge.eqmap_sz - 1);
sc->sge.eqmap[cntxt_id] = eq;
return (rc);
}
#ifdef TCP_OFFLOAD_ENABLE
static int
ofld_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
{
int rc, cntxt_id;
struct fw_eq_ofld_cmd c;
bzero(&c, sizeof (c));
c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_OFLD_CMD) | F_FW_CMD_REQUEST |
F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_OFLD_CMD_PFN(sc->pf) |
V_FW_EQ_OFLD_CMD_VFN(0));
c.alloc_to_len16 = htonl(F_FW_EQ_OFLD_CMD_ALLOC |
F_FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
c.fetchszm_to_iqid =
htonl(V_FW_EQ_OFLD_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
V_FW_EQ_OFLD_CMD_PCIECHN(eq->tx_chan) |
F_FW_EQ_OFLD_CMD_FETCHRO | V_FW_EQ_OFLD_CMD_IQID(eq->iqid));
c.dcaen_to_eqsize =
BE_32(V_FW_EQ_OFLD_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
V_FW_EQ_OFLD_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
V_FW_EQ_OFLD_CMD_CIDXFTHRESH(X_CIDXFLUSHTHRESH_32) |
V_FW_EQ_OFLD_CMD_EQSIZE(eq->qsize));
c.eqaddr = BE_64(eq->ba);
rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof (c), &c);
if (rc != 0) {
cxgb_printf(pi->dip, CE_WARN,
"failed to create egress queue for TCP offload: %d", rc);
return (rc);
}
eq->flags |= EQ_ALLOCATED;
eq->cntxt_id = G_FW_EQ_OFLD_CMD_EQID(BE_32(c.eqid_pkd));
cntxt_id = eq->cntxt_id - sc->sge.eq_start;
if (cntxt_id >= sc->sge.eqmap_sz)
panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
cntxt_id, sc->sge.eqmap_sz - 1);
sc->sge.eqmap[cntxt_id] = eq;
return (rc);
}
#endif
static int
alloc_eq(struct adapter *sc, struct port_info *pi, struct sge_eq *eq)
{
int rc;
size_t len;
mutex_init(&eq->lock, NULL, MUTEX_DRIVER, DDI_INTR_PRI(sc->intr_pri));
eq->flags |= EQ_MTX;
len = eq->qsize * EQ_ESIZE;
rc = alloc_desc_ring(sc, len, DDI_DMA_WRITE, &eq->desc_dhdl,
&eq->desc_ahdl, &eq->ba, (caddr_t *)&eq->desc);
if (rc != 0)
return (rc);
eq->cap = eq->qsize - sc->sge.stat_len / EQ_ESIZE;
eq->spg = (void *)&eq->desc[eq->cap];
eq->avail = eq->cap - 1; /* one less to avoid cidx = pidx */
eq->pidx = eq->cidx = 0;
eq->doorbells = sc->doorbells;
switch (eq->flags & EQ_TYPEMASK) {
case EQ_CTRL:
rc = ctrl_eq_alloc(sc, eq);
break;
case EQ_ETH:
rc = eth_eq_alloc(sc, pi, eq);
break;
#ifdef TCP_OFFLOAD_ENABLE
case EQ_OFLD:
rc = ofld_eq_alloc(sc, pi, eq);
break;
#endif
default:
panic("%s: invalid eq type %d.", __func__,
eq->flags & EQ_TYPEMASK);
}
if (eq->doorbells &
(DOORBELL_UDB | DOORBELL_UDBWC | DOORBELL_WCWR)) {
uint32_t s_qpp = sc->sge.s_qpp;
uint32_t mask = (1 << s_qpp) - 1;
volatile uint8_t *udb;
udb = (volatile uint8_t *)sc->reg1p + UDBS_DB_OFFSET;
udb += (eq->cntxt_id >> s_qpp) << PAGE_SHIFT; /* pg offset */
eq->udb_qid = eq->cntxt_id & mask; /* id in page */
if (eq->udb_qid > PAGE_SIZE / UDBS_SEG_SIZE)
eq->doorbells &= ~DOORBELL_WCWR;
else {
udb += eq->udb_qid << UDBS_SEG_SHIFT; /* seg offset */
eq->udb_qid = 0;
}
eq->udb = (volatile void *)udb;
}
if (rc != 0) {
cxgb_printf(sc->dip, CE_WARN,
"failed to allocate egress queue(%d): %d",
eq->flags & EQ_TYPEMASK, rc);
}
return (rc);
}
static int
free_eq(struct adapter *sc, struct sge_eq *eq)
{
int rc;
if (eq->flags & EQ_ALLOCATED) {
switch (eq->flags & EQ_TYPEMASK) {
case EQ_CTRL:
rc = -t4_ctrl_eq_free(sc, sc->mbox, sc->pf, 0,
eq->cntxt_id);
break;
case EQ_ETH:
rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0,
eq->cntxt_id);
break;
#ifdef TCP_OFFLOAD_ENABLE
case EQ_OFLD:
rc = -t4_ofld_eq_free(sc, sc->mbox, sc->pf, 0,
eq->cntxt_id);
break;
#endif
default:
panic("%s: invalid eq type %d.", __func__,
eq->flags & EQ_TYPEMASK);
}
if (rc != 0) {
cxgb_printf(sc->dip, CE_WARN,
"failed to free egress queue (%d): %d",
eq->flags & EQ_TYPEMASK, rc);
return (rc);
}
eq->flags &= ~EQ_ALLOCATED;
}
if (eq->desc != NULL) {
(void) free_desc_ring(&eq->desc_dhdl, &eq->desc_ahdl);
eq->desc = NULL;
}
if (eq->flags & EQ_MTX)
mutex_destroy(&eq->lock);
bzero(eq, sizeof (*eq));
return (0);
}
#ifdef TCP_OFFLOAD_ENABLE
/* ARGSUSED */
static int
alloc_wrq(struct adapter *sc, struct port_info *pi, struct sge_wrq *wrq,
int idx)
{
int rc;
rc = alloc_eq(sc, pi, &wrq->eq);
if (rc != 0)
return (rc);
wrq->adapter = sc;
wrq->wr_list.head = NULL;
wrq->wr_list.tail = NULL;
/*
* TODO: use idx to figure out what kind of wrq this is and install
* useful kstats for it.
*/
return (rc);
}
static int
free_wrq(struct adapter *sc, struct sge_wrq *wrq)
{
int rc;
rc = free_eq(sc, &wrq->eq);
if (rc != 0)
return (rc);
bzero(wrq, sizeof (*wrq));
return (0);
}
#endif
static int
alloc_txq(struct port_info *pi, struct sge_txq *txq, int idx)
{
int rc, i;
struct adapter *sc = pi->adapter;
struct sge_eq *eq = &txq->eq;
rc = alloc_eq(sc, pi, eq);
if (rc != 0)
return (rc);
txq->port = pi;
txq->sdesc = kmem_zalloc(sizeof (struct tx_sdesc) * eq->cap, KM_SLEEP);
txq->txb_size = eq->qsize * tx_copy_threshold;
rc = alloc_tx_copybuffer(sc, txq->txb_size, &txq->txb_dhdl,
&txq->txb_ahdl, &txq->txb_ba, &txq->txb_va);
if (rc == 0)
txq->txb_avail = txq->txb_size;
else
txq->txb_avail = txq->txb_size = 0;
/*
* TODO: is this too low? Worst case would need around 4 times qsize
* (all tx descriptors filled to the brim with SGLs, with each entry in
* the SGL coming from a distinct DMA handle). Increase tx_dhdl_total
* if you see too many dma_hdl_failed.
*/
txq->tx_dhdl_total = eq->qsize * 2;
txq->tx_dhdl = kmem_zalloc(sizeof (ddi_dma_handle_t) *
txq->tx_dhdl_total, KM_SLEEP);
for (i = 0; i < txq->tx_dhdl_total; i++) {
rc = ddi_dma_alloc_handle(sc->dip, &sc->sge.dma_attr_tx,
DDI_DMA_SLEEP, 0, &txq->tx_dhdl[i]);
if (rc != DDI_SUCCESS) {
cxgb_printf(sc->dip, CE_WARN,
"%s: failed to allocate DMA handle (%d)",
__func__, rc);
return (rc == DDI_DMA_NORESOURCES ? ENOMEM : EINVAL);
}
txq->tx_dhdl_avail++;
}
txq->ksp = setup_txq_kstats(pi, txq, idx);
return (rc);
}
static int
free_txq(struct port_info *pi, struct sge_txq *txq)
{
int i;
struct adapter *sc = pi->adapter;
struct sge_eq *eq = &txq->eq;
if (txq->ksp != NULL) {
kstat_delete(txq->ksp);
txq->ksp = NULL;
}
if (txq->txb_va != NULL) {
(void) free_desc_ring(&txq->txb_dhdl, &txq->txb_ahdl);
txq->txb_va = NULL;
}
if (txq->sdesc != NULL) {
struct tx_sdesc *sd;
ddi_dma_handle_t hdl;
TXQ_LOCK(txq);
while (eq->cidx != eq->pidx) {
sd = &txq->sdesc[eq->cidx];
for (i = sd->hdls_used; i; i--) {
hdl = txq->tx_dhdl[txq->tx_dhdl_cidx];
(void) ddi_dma_unbind_handle(hdl);
if (++txq->tx_dhdl_cidx == txq->tx_dhdl_total)
txq->tx_dhdl_cidx = 0;
}
ASSERT(sd->m);
freemsgchain(sd->m);
eq->cidx += sd->desc_used;
if (eq->cidx >= eq->cap)
eq->cidx -= eq->cap;
txq->txb_avail += txq->txb_used;
}
ASSERT(txq->tx_dhdl_cidx == txq->tx_dhdl_pidx);
ASSERT(txq->txb_avail == txq->txb_size);
TXQ_UNLOCK(txq);
kmem_free(txq->sdesc, sizeof (struct tx_sdesc) * eq->cap);
txq->sdesc = NULL;
}
if (txq->tx_dhdl != NULL) {
for (i = 0; i < txq->tx_dhdl_total; i++) {
if (txq->tx_dhdl[i] != NULL)
ddi_dma_free_handle(&txq->tx_dhdl[i]);
}
}
(void) free_eq(sc, &txq->eq);
bzero(txq, sizeof (*txq));
return (0);
}
/*
* Allocates a block of contiguous memory for DMA. Can be used to allocate
* memory for descriptor rings or for tx/rx copy buffers.
*
* Caller does not have to clean up anything if this function fails, it cleans
* up after itself.
*
* Caller provides the following:
* len length of the block of memory to allocate.
* flags DDI_DMA_* flags to use (CONSISTENT/STREAMING, READ/WRITE/RDWR)
* acc_attr device access attributes for the allocation.
* dma_attr DMA attributes for the allocation
*
* If the function is successful it fills up this information:
* dma_hdl DMA handle for the allocated memory
* acc_hdl access handle for the allocated memory
* ba bus address of the allocated memory
* va KVA of the allocated memory.
*/
static int
alloc_dma_memory(struct adapter *sc, size_t len, int flags,
ddi_device_acc_attr_t *acc_attr, ddi_dma_attr_t *dma_attr,
ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl,
uint64_t *pba, caddr_t *pva)
{
int rc;
ddi_dma_handle_t dhdl;
ddi_acc_handle_t ahdl;
ddi_dma_cookie_t cookie;
uint_t ccount;
caddr_t va;
size_t real_len;
*pva = NULL;
/*
* DMA handle.
*/
rc = ddi_dma_alloc_handle(sc->dip, dma_attr, DDI_DMA_SLEEP, 0, &dhdl);
if (rc != DDI_SUCCESS) {
cxgb_printf(sc->dip, CE_WARN,
"failed to allocate DMA handle: %d", rc);
return (rc == DDI_DMA_NORESOURCES ? ENOMEM : EINVAL);
}
/*
* Memory suitable for DMA.
*/
rc = ddi_dma_mem_alloc(dhdl, len, acc_attr,
flags & DDI_DMA_CONSISTENT ? DDI_DMA_CONSISTENT : DDI_DMA_STREAMING,
DDI_DMA_SLEEP, 0, &va, &real_len, &ahdl);
if (rc != DDI_SUCCESS) {
cxgb_printf(sc->dip, CE_WARN,
"failed to allocate DMA memory: %d", rc);
ddi_dma_free_handle(&dhdl);
return (ENOMEM);
}
if (len != real_len) {
cxgb_printf(sc->dip, CE_WARN,
"%s: len (%u) != real_len (%u)\n", len, real_len);
}
/*
* DMA bindings.
*/
rc = ddi_dma_addr_bind_handle(dhdl, NULL, va, real_len, flags, NULL,
NULL, &cookie, &ccount);
if (rc != DDI_DMA_MAPPED) {
cxgb_printf(sc->dip, CE_WARN,
"failed to map DMA memory: %d", rc);
ddi_dma_mem_free(&ahdl);
ddi_dma_free_handle(&dhdl);
return (ENOMEM);
}
if (ccount != 1) {
cxgb_printf(sc->dip, CE_WARN,
"unusable DMA mapping (%d segments)", ccount);
(void) free_desc_ring(&dhdl, &ahdl);
}
bzero(va, real_len);
*dma_hdl = dhdl;
*acc_hdl = ahdl;
*pba = cookie.dmac_laddress;
*pva = va;
return (0);
}
static int
free_dma_memory(ddi_dma_handle_t *dhdl, ddi_acc_handle_t *ahdl)
{
(void) ddi_dma_unbind_handle(*dhdl);
ddi_dma_mem_free(ahdl);
ddi_dma_free_handle(dhdl);
return (0);
}
static int
alloc_desc_ring(struct adapter *sc, size_t len, int rw,
ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl,
uint64_t *pba, caddr_t *pva)
{
ddi_device_acc_attr_t *acc_attr = &sc->sge.acc_attr_desc;
ddi_dma_attr_t *dma_attr = &sc->sge.dma_attr_desc;
return (alloc_dma_memory(sc, len, DDI_DMA_CONSISTENT | rw, acc_attr,
dma_attr, dma_hdl, acc_hdl, pba, pva));
}
static int
free_desc_ring(ddi_dma_handle_t *dhdl, ddi_acc_handle_t *ahdl)
{
return (free_dma_memory(dhdl, ahdl));
}
static int
alloc_tx_copybuffer(struct adapter *sc, size_t len,
ddi_dma_handle_t *dma_hdl, ddi_acc_handle_t *acc_hdl,
uint64_t *pba, caddr_t *pva)
{
ddi_device_acc_attr_t *acc_attr = &sc->sge.acc_attr_tx;
ddi_dma_attr_t *dma_attr = &sc->sge.dma_attr_desc; /* NOT dma_attr_tx */
return (alloc_dma_memory(sc, len, DDI_DMA_STREAMING | DDI_DMA_WRITE,
acc_attr, dma_attr, dma_hdl, acc_hdl, pba, pva));
}
static inline bool
is_new_response(const struct sge_iq *iq, struct rsp_ctrl **ctrl)
{
(void) ddi_dma_sync(iq->dhdl, (uintptr_t)iq->cdesc -
(uintptr_t)iq->desc, iq->esize, DDI_DMA_SYNC_FORKERNEL);
*ctrl = (void *)((uintptr_t)iq->cdesc +
(iq->esize - sizeof (struct rsp_ctrl)));
return ((((*ctrl)->u.type_gen >> S_RSPD_GEN) == iq->gen));
}
static inline void
iq_next(struct sge_iq *iq)
{
iq->cdesc = (void *) ((uintptr_t)iq->cdesc + iq->esize);
if (++iq->cidx == iq->qsize - 1) {
iq->cidx = 0;
iq->gen ^= 1;
iq->cdesc = iq->desc;
}
}
/*
* Fill up the freelist by upto nbufs and maybe ring its doorbell.
*
* Returns non-zero to indicate that it should be added to the list of starving
* freelists.
*/
static int
refill_fl(struct adapter *sc, struct sge_fl *fl, int nbufs)
{
uint64_t *d = &fl->desc[fl->pidx];
struct fl_sdesc *sd = &fl->sdesc[fl->pidx];
FL_LOCK_ASSERT_OWNED(fl);
ASSERT(nbufs >= 0);
if (nbufs > fl->needed)
nbufs = fl->needed;
while (nbufs--) {
if (sd->rxb != NULL) {
if (sd->rxb->ref_cnt == 1) {
/*
* Buffer is available for recycling. Two ways
* this can happen:
*
* a) All the packets DMA'd into it last time
* around were within the rx_copy_threshold
* and no part of the buffer was ever passed
* up (ref_cnt never went over 1).
*
* b) Packets DMA'd into the buffer were passed
* up but have all been freed by the upper
* layers by now (ref_cnt went over 1 but is
* now back to 1).
*
* Either way the bus address in the descriptor
* ring is already valid.
*/
ASSERT(*d == cpu_to_be64(sd->rxb->ba));
d++;
goto recycled;
} else {
/*
* Buffer still in use and we need a
* replacement. But first release our reference
* on the existing buffer.
*/
rxbuf_free(sd->rxb);
}
}
sd->rxb = rxbuf_alloc(sc->sge.rxbuf_cache, KM_NOSLEEP, 1);
if (sd->rxb == NULL)
break;
*d++ = cpu_to_be64(sd->rxb->ba);
recycled: fl->pending++;
sd++;
fl->needed--;
if (++fl->pidx == fl->cap) {
fl->pidx = 0;
sd = fl->sdesc;
d = fl->desc;
}
}
if (fl->pending >= 8)
ring_fl_db(sc, fl);
return (FL_RUNNING_LOW(fl) && !(fl->flags & FL_STARVING));
}
#ifndef TAILQ_FOREACH_SAFE
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = TAILQ_FIRST((head)); \
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
(var) = (tvar))
#endif
/*
* Attempt to refill all starving freelists.
*/
static void
refill_sfl(void *arg)
{
struct adapter *sc = arg;
struct sge_fl *fl, *fl_temp;
mutex_enter(&sc->sfl_lock);
TAILQ_FOREACH_SAFE(fl, &sc->sfl, link, fl_temp) {
FL_LOCK(fl);
(void) refill_fl(sc, fl, 64);
if (FL_NOT_RUNNING_LOW(fl) || fl->flags & FL_DOOMED) {
TAILQ_REMOVE(&sc->sfl, fl, link);
fl->flags &= ~FL_STARVING;
}
FL_UNLOCK(fl);
}
if (!TAILQ_EMPTY(&sc->sfl) != 0)
sc->sfl_timer = timeout(refill_sfl, sc, drv_usectohz(100000));
mutex_exit(&sc->sfl_lock);
}
static void
add_fl_to_sfl(struct adapter *sc, struct sge_fl *fl)
{
mutex_enter(&sc->sfl_lock);
FL_LOCK(fl);
if ((fl->flags & FL_DOOMED) == 0) {
if (TAILQ_EMPTY(&sc->sfl) != 0) {
sc->sfl_timer = timeout(refill_sfl, sc,
drv_usectohz(100000));
}
fl->flags |= FL_STARVING;
TAILQ_INSERT_TAIL(&sc->sfl, fl, link);
}
FL_UNLOCK(fl);
mutex_exit(&sc->sfl_lock);
}
static void
free_fl_bufs(struct sge_fl *fl)
{
struct fl_sdesc *sd;
unsigned int i;
FL_LOCK_ASSERT_OWNED(fl);
for (i = 0; i < fl->cap; i++) {
sd = &fl->sdesc[i];
if (sd->rxb != NULL) {
rxbuf_free(sd->rxb);
sd->rxb = NULL;
}
}
}
/*
* Note that fl->cidx and fl->offset are left unchanged in case of failure.
*/
static mblk_t *
get_fl_payload(struct adapter *sc, struct sge_fl *fl,
uint32_t len_newbuf, int *fl_bufs_used)
{
struct mblk_pair frame = {0};
struct rxbuf *rxb;
mblk_t *m = NULL;
uint_t nbuf = 0, len, copy, n;
uint32_t cidx, offset, rcidx, roffset;
/*
* The SGE won't pack a new frame into the current buffer if the entire
* payload doesn't fit in the remaining space. Move on to the next buf
* in that case.
*/
rcidx = fl->cidx;
roffset = fl->offset;
if (fl->offset > 0 && len_newbuf & F_RSPD_NEWBUF) {
fl->offset = 0;
if (++fl->cidx == fl->cap)
fl->cidx = 0;
nbuf++;
}
cidx = fl->cidx;
offset = fl->offset;
len = G_RSPD_LEN(len_newbuf); /* pktshift + payload length */
copy = (len <= fl->copy_threshold);
if (copy != 0) {
frame.head = m = allocb(len, BPRI_HI);
if (m == NULL) {
fl->allocb_fail++;
cmn_err(CE_WARN,"%s: mbuf allocation failure "
"count = %llu", __func__,
(unsigned long long)fl->allocb_fail);
fl->cidx = rcidx;
fl->offset = roffset;
return (NULL);
}
}
while (len) {
rxb = fl->sdesc[cidx].rxb;
n = min(len, rxb->buf_size - offset);
(void) ddi_dma_sync(rxb->dhdl, offset, n,
DDI_DMA_SYNC_FORKERNEL);
if (copy != 0)
bcopy(rxb->va + offset, m->b_wptr, n);
else {
m = desballoc((unsigned char *)rxb->va + offset, n,
BPRI_HI, &rxb->freefunc);
if (m == NULL) {
fl->allocb_fail++;
cmn_err(CE_WARN,
"%s: mbuf allocation failure "
"count = %llu", __func__,
(unsigned long long)fl->allocb_fail);
if (frame.head)
freemsgchain(frame.head);
fl->cidx = rcidx;
fl->offset = roffset;
return (NULL);
}
atomic_inc_uint(&rxb->ref_cnt);
if (frame.head != NULL)
frame.tail->b_cont = m;
else
frame.head = m;
frame.tail = m;
}
m->b_wptr += n;
len -= n;
offset += roundup(n, sc->sge.fl_align);
ASSERT(offset <= rxb->buf_size);
if (offset == rxb->buf_size) {
offset = 0;
if (++cidx == fl->cap)
cidx = 0;
nbuf++;
}
}
fl->cidx = cidx;
fl->offset = offset;
(*fl_bufs_used) += nbuf;
ASSERT(frame.head != NULL);
return (frame.head);
}
/*
* We'll do immediate data tx for non-LSO, but only when not coalescing. We're
* willing to use upto 2 hardware descriptors which means a maximum of 96 bytes
* of immediate data.
*/
#define IMM_LEN ( \
2 * EQ_ESIZE \
- sizeof (struct fw_eth_tx_pkt_wr) \
- sizeof (struct cpl_tx_pkt_core))
/*
* Returns non-zero on failure, no need to cleanup anything in that case.
*
* Note 1: We always try to pull up the mblk if required and return E2BIG only
* if this fails.
*
* Note 2: We'll also pullup incoming mblk if HW_LSO is set and the first mblk
* does not have the TCP header in it.
*/
static int
get_frame_txinfo(struct sge_txq *txq, mblk_t **fp, struct txinfo *txinfo,
int sgl_only)
{
uint32_t flags = 0, len, n;
mblk_t *m = *fp;
int rc;
TXQ_LOCK_ASSERT_OWNED(txq); /* will manipulate txb and dma_hdls */
mac_hcksum_get(m, NULL, NULL, NULL, NULL, &flags);
txinfo->flags = flags;
mac_lso_get(m, &txinfo->mss, &flags);
txinfo->flags |= flags;
if (flags & HW_LSO)
sgl_only = 1; /* Do not allow immediate data with LSO */
start: txinfo->nsegs = 0;
txinfo->hdls_used = 0;
txinfo->txb_used = 0;
txinfo->len = 0;
/* total length and a rough estimate of # of segments */
n = 0;
for (; m; m = m->b_cont) {
len = MBLKL(m);
n += (len / PAGE_SIZE) + 1;
txinfo->len += len;
}
m = *fp;
if (n >= TX_SGL_SEGS || (flags & HW_LSO && MBLKL(m) < 50)) {
txq->pullup_early++;
m = msgpullup(*fp, -1);
if (m == NULL) {
txq->pullup_failed++;
return (E2BIG); /* (*fp) left as it was */
}
freemsg(*fp);
*fp = m;
mac_hcksum_set(m, 0, 0, 0, 0, txinfo->flags);
}
if (txinfo->len <= IMM_LEN && !sgl_only)
return (0); /* nsegs = 0 tells caller to use imm. tx */
if (txinfo->len <= txq->copy_threshold &&
copy_into_txb(txq, m, txinfo->len, txinfo) == 0)
goto done;
for (; m; m = m->b_cont) {
len = MBLKL(m);
/* Use tx copy buffer if this mblk is small enough */
if (len <= txq->copy_threshold &&
copy_into_txb(txq, m, len, txinfo) == 0)
continue;
/* Add DMA bindings for this mblk to the SGL */
rc = add_mblk(txq, txinfo, m, len);
if (rc == E2BIG ||
(txinfo->nsegs == TX_SGL_SEGS && m->b_cont)) {
txq->pullup_late++;
m = msgpullup(*fp, -1);
if (m != NULL) {
free_txinfo_resources(txq, txinfo);
freemsg(*fp);
*fp = m;
mac_hcksum_set(m, 0, 0, 0, 0, txinfo->flags);
goto start;
}
txq->pullup_failed++;
rc = E2BIG;
}
if (rc != 0) {
free_txinfo_resources(txq, txinfo);
return (rc);
}
}
ASSERT(txinfo->nsegs > 0 && txinfo->nsegs <= TX_SGL_SEGS);
done:
/*
* Store the # of flits required to hold this frame's SGL in nflits. An
* SGL has a (ULPTX header + len0, addr0) tuple optionally followed by
* multiple (len0 + len1, addr0, addr1) tuples. If addr1 is not used
* then len1 must be set to 0.
*/
n = txinfo->nsegs - 1;
txinfo->nflits = (3 * n) / 2 + (n & 1) + 2;
if (n & 1)
txinfo->sgl.sge[n / 2].len[1] = cpu_to_be32(0);
txinfo->sgl.cmd_nsge = cpu_to_be32(V_ULPTX_CMD((u32)ULP_TX_SC_DSGL) |
V_ULPTX_NSGE(txinfo->nsegs));
return (0);
}
static inline int
fits_in_txb(struct sge_txq *txq, int len, int *waste)
{
if (txq->txb_avail < len)
return (0);
if (txq->txb_next + len <= txq->txb_size) {
*waste = 0;
return (1);
}
*waste = txq->txb_size - txq->txb_next;
return (txq->txb_avail - *waste < len ? 0 : 1);
}
#define TXB_CHUNK 64
/*
* Copies the specified # of bytes into txq's tx copy buffer and updates txinfo
* and txq to indicate resources used. Caller has to make sure that those many
* bytes are available in the mblk chain (b_cont linked).
*/
static inline int
copy_into_txb(struct sge_txq *txq, mblk_t *m, int len, struct txinfo *txinfo)
{
int waste, n;
TXQ_LOCK_ASSERT_OWNED(txq); /* will manipulate txb */
if (!fits_in_txb(txq, len, &waste)) {
txq->txb_full++;
return (ENOMEM);
}
if (waste != 0) {
ASSERT((waste & (TXB_CHUNK - 1)) == 0);
txinfo->txb_used += waste;
txq->txb_avail -= waste;
txq->txb_next = 0;
}
for (n = 0; n < len; m = m->b_cont) {
bcopy(m->b_rptr, txq->txb_va + txq->txb_next + n, MBLKL(m));
n += MBLKL(m);
}
add_seg(txinfo, txq->txb_ba + txq->txb_next, len);
n = roundup(len, TXB_CHUNK);
txinfo->txb_used += n;
txq->txb_avail -= n;
txq->txb_next += n;
ASSERT(txq->txb_next <= txq->txb_size);
if (txq->txb_next == txq->txb_size)
txq->txb_next = 0;
return (0);
}
static inline void
add_seg(struct txinfo *txinfo, uint64_t ba, uint32_t len)
{
ASSERT(txinfo->nsegs < TX_SGL_SEGS); /* must have room */
if (txinfo->nsegs != 0) {
int idx = txinfo->nsegs - 1;
txinfo->sgl.sge[idx / 2].len[idx & 1] = cpu_to_be32(len);
txinfo->sgl.sge[idx / 2].addr[idx & 1] = cpu_to_be64(ba);
} else {
txinfo->sgl.len0 = cpu_to_be32(len);
txinfo->sgl.addr0 = cpu_to_be64(ba);
}
txinfo->nsegs++;
}
/*
* This function cleans up any partially allocated resources when it fails so
* there's nothing for the caller to clean up in that case.
*
* EIO indicates permanent failure. Caller should drop the frame containing
* this mblk and continue.
*
* E2BIG indicates that the SGL length for this mblk exceeds the hardware
* limit. Caller should pull up the frame before trying to send it out.
* (This error means our pullup_early heuristic did not work for this frame)
*
* ENOMEM indicates a temporary shortage of resources (DMA handles, other DMA
* resources, etc.). Caller should suspend the tx queue and wait for reclaim to
* free up resources.
*/
static inline int
add_mblk(struct sge_txq *txq, struct txinfo *txinfo, mblk_t *m, int len)
{
ddi_dma_handle_t dhdl;
ddi_dma_cookie_t cookie;
uint_t ccount = 0;
int rc;
TXQ_LOCK_ASSERT_OWNED(txq); /* will manipulate dhdls */
if (txq->tx_dhdl_avail == 0) {
txq->dma_hdl_failed++;
return (ENOMEM);
}
dhdl = txq->tx_dhdl[txq->tx_dhdl_pidx];
rc = ddi_dma_addr_bind_handle(dhdl, NULL, (caddr_t)m->b_rptr, len,
DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_DONTWAIT, NULL, &cookie,
&ccount);
if (rc != DDI_DMA_MAPPED) {
txq->dma_map_failed++;
ASSERT(rc != DDI_DMA_INUSE && rc != DDI_DMA_PARTIAL_MAP);
return (rc == DDI_DMA_NORESOURCES ? ENOMEM : EIO);
}
if (ccount + txinfo->nsegs > TX_SGL_SEGS) {
(void) ddi_dma_unbind_handle(dhdl);
return (E2BIG);
}
add_seg(txinfo, cookie.dmac_laddress, cookie.dmac_size);
while (--ccount) {
ddi_dma_nextcookie(dhdl, &cookie);
add_seg(txinfo, cookie.dmac_laddress, cookie.dmac_size);
}
if (++txq->tx_dhdl_pidx == txq->tx_dhdl_total)
txq->tx_dhdl_pidx = 0;
txq->tx_dhdl_avail--;
txinfo->hdls_used++;
return (0);
}
/*
* Releases all the txq resources used up in the specified txinfo.
*/
static void
free_txinfo_resources(struct sge_txq *txq, struct txinfo *txinfo)
{
int n;
TXQ_LOCK_ASSERT_OWNED(txq); /* dhdls, txb */
n = txinfo->txb_used;
if (n > 0) {
txq->txb_avail += n;
if (n <= txq->txb_next)
txq->txb_next -= n;
else {
n -= txq->txb_next;
txq->txb_next = txq->txb_size - n;
}
}
for (n = txinfo->hdls_used; n > 0; n--) {
if (txq->tx_dhdl_pidx > 0)
txq->tx_dhdl_pidx--;
else
txq->tx_dhdl_pidx = txq->tx_dhdl_total - 1;
txq->tx_dhdl_avail++;
(void) ddi_dma_unbind_handle(txq->tx_dhdl[txq->tx_dhdl_pidx]);
}
}
/*
* Returns 0 to indicate that m has been accepted into a coalesced tx work
* request. It has either been folded into txpkts or txpkts was flushed and m
* has started a new coalesced work request (as the first frame in a fresh
* txpkts).
*
* Returns non-zero to indicate a failure - caller is responsible for
* transmitting m, if there was anything in txpkts it has been flushed.
*/
static int
add_to_txpkts(struct sge_txq *txq, struct txpkts *txpkts, mblk_t *m,
struct txinfo *txinfo)
{
struct sge_eq *eq = &txq->eq;
int can_coalesce;
struct tx_sdesc *txsd;
uint8_t flits;
TXQ_LOCK_ASSERT_OWNED(txq);
if (txpkts->npkt > 0) {
flits = TXPKTS_PKT_HDR + txinfo->nflits;
can_coalesce = (txinfo->flags & HW_LSO) == 0 &&
txpkts->nflits + flits <= TX_WR_FLITS &&
txpkts->nflits + flits <= eq->avail * 8 &&
txpkts->plen + txinfo->len < 65536;
if (can_coalesce != 0) {
txpkts->tail->b_next = m;
txpkts->tail = m;
txpkts->npkt++;
txpkts->nflits += flits;
txpkts->plen += txinfo->len;
txsd = &txq->sdesc[eq->pidx];
txsd->txb_used += txinfo->txb_used;
txsd->hdls_used += txinfo->hdls_used;
return (0);
}
/*
* Couldn't coalesce m into txpkts. The first order of business
* is to send txpkts on its way. Then we'll revisit m.
*/
write_txpkts_wr(txq, txpkts);
}
/*
* Check if we can start a new coalesced tx work request with m as
* the first packet in it.
*/
ASSERT(txpkts->npkt == 0);
ASSERT(txinfo->len < 65536);
flits = TXPKTS_WR_HDR + txinfo->nflits;
can_coalesce = (txinfo->flags & HW_LSO) == 0 &&
flits <= eq->avail * 8 && flits <= TX_WR_FLITS;
if (can_coalesce == 0)
return (EINVAL);
/*
* Start a fresh coalesced tx WR with m as the first frame in it.
*/
txpkts->tail = m;
txpkts->npkt = 1;
txpkts->nflits = flits;
txpkts->flitp = &eq->desc[eq->pidx].flit[2];
txpkts->plen = txinfo->len;
txsd = &txq->sdesc[eq->pidx];
txsd->m = m;
txsd->txb_used = txinfo->txb_used;
txsd->hdls_used = txinfo->hdls_used;
return (0);
}
/*
* Note that write_txpkts_wr can never run out of hardware descriptors (but
* write_txpkt_wr can). add_to_txpkts ensures that a frame is accepted for
* coalescing only if sufficient hardware descriptors are available.
*/
static void
write_txpkts_wr(struct sge_txq *txq, struct txpkts *txpkts)
{
struct sge_eq *eq = &txq->eq;
struct fw_eth_tx_pkts_wr *wr;
struct tx_sdesc *txsd;
uint32_t ctrl;
uint16_t ndesc;
TXQ_LOCK_ASSERT_OWNED(txq); /* pidx, avail */
ndesc = howmany(txpkts->nflits, 8);
wr = (void *)&eq->desc[eq->pidx];
wr->op_pkd = cpu_to_be32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR) |
V_FW_WR_IMMDLEN(0)); /* immdlen does not matter in this WR */
ctrl = V_FW_WR_LEN16(howmany(txpkts->nflits, 2));
if (eq->avail == ndesc)
ctrl |= F_FW_WR_EQUEQ | F_FW_WR_EQUIQ;
wr->equiq_to_len16 = cpu_to_be32(ctrl);
wr->plen = cpu_to_be16(txpkts->plen);
wr->npkt = txpkts->npkt;
wr->r3 = wr->type = 0;
/* Everything else already written */
txsd = &txq->sdesc[eq->pidx];
txsd->desc_used = ndesc;
txq->txb_used += txsd->txb_used / TXB_CHUNK;
txq->hdl_used += txsd->hdls_used;
ASSERT(eq->avail >= ndesc);
eq->pending += ndesc;
eq->avail -= ndesc;
eq->pidx += ndesc;
if (eq->pidx >= eq->cap)
eq->pidx -= eq->cap;
txq->txpkts_pkts += txpkts->npkt;
txq->txpkts_wrs++;
txpkts->npkt = 0; /* emptied */
}
static int
write_txpkt_wr(struct port_info *pi, struct sge_txq *txq, mblk_t *m,
struct txinfo *txinfo)
{
struct sge_eq *eq = &txq->eq;
struct fw_eth_tx_pkt_wr *wr;
struct cpl_tx_pkt_core *cpl;
uint32_t ctrl; /* used in many unrelated places */
uint64_t ctrl1;
int nflits, ndesc;
struct tx_sdesc *txsd;
caddr_t dst;
TXQ_LOCK_ASSERT_OWNED(txq); /* pidx, avail */
/*
* Do we have enough flits to send this frame out?
*/
ctrl = sizeof (struct cpl_tx_pkt_core);
if (txinfo->flags & HW_LSO) {
nflits = TXPKT_LSO_WR_HDR;
ctrl += sizeof(struct cpl_tx_pkt_lso_core);
} else
nflits = TXPKT_WR_HDR;
if (txinfo->nsegs > 0)
nflits += txinfo->nflits;
else {
nflits += howmany(txinfo->len, 8);
ctrl += txinfo->len;
}
ndesc = howmany(nflits, 8);
if (ndesc > eq->avail)
return (ENOMEM);
/* Firmware work request header */
wr = (void *)&eq->desc[eq->pidx];
wr->op_immdlen = cpu_to_be32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
V_FW_WR_IMMDLEN(ctrl));
ctrl = V_FW_WR_LEN16(howmany(nflits, 2));
if (eq->avail == ndesc)
ctrl |= F_FW_WR_EQUEQ | F_FW_WR_EQUIQ;
wr->equiq_to_len16 = cpu_to_be32(ctrl);
wr->r3 = 0;
if (txinfo->flags & HW_LSO) {
uint16_t etype;
struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
char *p = (void *)m->b_rptr;
ctrl = V_LSO_OPCODE((u32)CPL_TX_PKT_LSO) | F_LSO_FIRST_SLICE |
F_LSO_LAST_SLICE;
etype = ntohs(((struct ether_header *)p)->ether_type);
if (etype == ETHERTYPE_VLAN) {
ctrl |= V_LSO_ETHHDR_LEN(1);
etype = ntohs(((struct ether_vlan_header *)p)->ether_type);
p += sizeof (struct ether_vlan_header);
} else {
p += sizeof (struct ether_header);
}
switch (etype) {
case ETHERTYPE_IP:
ctrl |= V_LSO_IPHDR_LEN(IPH_HDR_LENGTH(p) / 4);
p += IPH_HDR_LENGTH(p);
break;
case ETHERTYPE_IPV6:
ctrl |= F_LSO_IPV6;
ctrl |= V_LSO_IPHDR_LEN(sizeof (ip6_t) / 4);
p += sizeof (ip6_t);
default:
break;
}
ctrl |= V_LSO_TCPHDR_LEN(TCP_HDR_LENGTH((tcph_t *)p) / 4);
lso->lso_ctrl = cpu_to_be32(ctrl);
lso->ipid_ofst = cpu_to_be16(0);
lso->mss = cpu_to_be16(txinfo->mss);
lso->seqno_offset = cpu_to_be32(0);
if (is_t4(pi->adapter->params.chip))
lso->len = cpu_to_be32(txinfo->len);
else
lso->len = cpu_to_be32(V_LSO_T5_XFER_SIZE(txinfo->len));
cpl = (void *)(lso + 1);
txq->tso_wrs++;
} else
cpl = (void *)(wr + 1);
/* Checksum offload */
ctrl1 = 0;
if (!(txinfo->flags & HCK_IPV4_HDRCKSUM))
ctrl1 |= F_TXPKT_IPCSUM_DIS;
if (!(txinfo->flags & HCK_FULLCKSUM))
ctrl1 |= F_TXPKT_L4CSUM_DIS;
if (ctrl1 == 0)
txq->txcsum++; /* some hardware assistance provided */
/* CPL header */
cpl->ctrl0 = cpu_to_be32(V_TXPKT_OPCODE(CPL_TX_PKT) |
V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(pi->adapter->pf));
cpl->pack = 0;
cpl->len = cpu_to_be16(txinfo->len);
cpl->ctrl1 = cpu_to_be64(ctrl1);
/* Software descriptor */
txsd = &txq->sdesc[eq->pidx];
txsd->m = m;
txsd->txb_used = txinfo->txb_used;
txsd->hdls_used = txinfo->hdls_used;
/* LINTED: E_ASSIGN_NARROW_CONV */
txsd->desc_used = ndesc;
txq->txb_used += txinfo->txb_used / TXB_CHUNK;
txq->hdl_used += txinfo->hdls_used;
eq->pending += ndesc;
eq->avail -= ndesc;
eq->pidx += ndesc;
if (eq->pidx >= eq->cap)
eq->pidx -= eq->cap;
/* SGL */
dst = (void *)(cpl + 1);
if (txinfo->nsegs > 0) {
txq->sgl_wrs++;
copy_to_txd(eq, (void *)&txinfo->sgl, &dst, txinfo->nflits * 8);
/* Need to zero-pad to a 16 byte boundary if not on one */
if ((uintptr_t)dst & 0xf)
/* LINTED: E_BAD_PTR_CAST_ALIGN */
*(uint64_t *)dst = 0;
} else {
txq->imm_wrs++;
#ifdef DEBUG
ctrl = txinfo->len;
#endif
for (; m; m = m->b_cont) {
copy_to_txd(eq, (void *)m->b_rptr, &dst, MBLKL(m));
#ifdef DEBUG
ctrl -= MBLKL(m);
#endif
}
ASSERT(ctrl == 0);
}
txq->txpkt_wrs++;
return (0);
}
static inline void
write_ulp_cpl_sgl(struct port_info *pi, struct sge_txq *txq,
struct txpkts *txpkts, struct txinfo *txinfo)
{
struct ulp_txpkt *ulpmc;
struct ulptx_idata *ulpsc;
struct cpl_tx_pkt_core *cpl;
uintptr_t flitp, start, end;
uint64_t ctrl;
caddr_t dst;
ASSERT(txpkts->npkt > 0);
start = (uintptr_t)txq->eq.desc;
end = (uintptr_t)txq->eq.spg;
/* Checksum offload */
ctrl = 0;
if (!(txinfo->flags & HCK_IPV4_HDRCKSUM))
ctrl |= F_TXPKT_IPCSUM_DIS;
if (!(txinfo->flags & HCK_FULLCKSUM))
ctrl |= F_TXPKT_L4CSUM_DIS;
if (ctrl == 0)
txq->txcsum++; /* some hardware assistance provided */
/*
* The previous packet's SGL must have ended at a 16 byte boundary (this
* is required by the firmware/hardware). It follows that flitp cannot
* wrap around between the ULPTX master command and ULPTX subcommand (8
* bytes each), and that it can not wrap around in the middle of the
* cpl_tx_pkt_core either.
*/
flitp = (uintptr_t)txpkts->flitp;
ASSERT((flitp & 0xf) == 0);
/* ULP master command */
ulpmc = (void *)flitp;
ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0));
ulpmc->len = htonl(howmany(sizeof (*ulpmc) + sizeof (*ulpsc) +
sizeof (*cpl) + 8 * txinfo->nflits, 16));
/* ULP subcommand */
ulpsc = (void *)(ulpmc + 1);
ulpsc->cmd_more = cpu_to_be32(V_ULPTX_CMD((u32)ULP_TX_SC_IMM) |
F_ULP_TX_SC_MORE);
ulpsc->len = cpu_to_be32(sizeof (struct cpl_tx_pkt_core));
flitp += sizeof (*ulpmc) + sizeof (*ulpsc);
if (flitp == end)
flitp = start;
/* CPL_TX_PKT */
cpl = (void *)flitp;
cpl->ctrl0 = cpu_to_be32(V_TXPKT_OPCODE(CPL_TX_PKT) |
V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(pi->adapter->pf));
cpl->pack = 0;
cpl->len = cpu_to_be16(txinfo->len);
cpl->ctrl1 = cpu_to_be64(ctrl);
flitp += sizeof (*cpl);
if (flitp == end)
flitp = start;
/* SGL for this frame */
dst = (caddr_t)flitp;
copy_to_txd(&txq->eq, (void *)&txinfo->sgl, &dst, txinfo->nflits * 8);
flitp = (uintptr_t)dst;
/* Zero pad and advance to a 16 byte boundary if not already at one. */
if (flitp & 0xf) {
/* no matter what, flitp should be on an 8 byte boundary */
ASSERT((flitp & 0x7) == 0);
*(uint64_t *)flitp = 0;
flitp += sizeof (uint64_t);
txpkts->nflits++;
}
if (flitp == end)
flitp = start;
txpkts->flitp = (void *)flitp;
}
static inline void
copy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to, int len)
{
if ((uintptr_t)(*to) + len <= (uintptr_t)eq->spg) {
bcopy(from, *to, len);
(*to) += len;
} else {
int portion = (uintptr_t)eq->spg - (uintptr_t)(*to);
bcopy(from, *to, portion);
from += portion;
portion = len - portion; /* remaining */
bcopy(from, (void *)eq->desc, portion);
(*to) = (caddr_t)eq->desc + portion;
}
}
static inline void
ring_tx_db(struct adapter *sc, struct sge_eq *eq)
{
int val, db_mode;
u_int db = eq->doorbells;
if (eq->pending > 1)
db &= ~DOORBELL_WCWR;
if (eq->pending > eq->pidx) {
int offset = eq->cap - (eq->pending - eq->pidx);
/* pidx has wrapped around since last doorbell */
(void) ddi_dma_sync(eq->desc_dhdl,
offset * sizeof (struct tx_desc), 0,
DDI_DMA_SYNC_FORDEV);
(void) ddi_dma_sync(eq->desc_dhdl,
0, eq->pidx * sizeof (struct tx_desc),
DDI_DMA_SYNC_FORDEV);
} else if (eq->pending > 0) {
(void) ddi_dma_sync(eq->desc_dhdl,
(eq->pidx - eq->pending) * sizeof (struct tx_desc),
eq->pending * sizeof (struct tx_desc),
DDI_DMA_SYNC_FORDEV);
}
membar_producer();
if (is_t4(sc->params.chip))
val = V_PIDX(eq->pending);
else
val = V_PIDX_T5(eq->pending);
db_mode = (1 << (ffs(db) - 1));
switch (db_mode) {
case DOORBELL_UDB:
*eq->udb = LE_32(V_QID(eq->udb_qid) | val);
break;
case DOORBELL_WCWR:
{
volatile uint64_t *dst, *src;
int i;
/*
* Queues whose 128B doorbell segment fits in
* the page do not use relative qid
* (udb_qid is always 0). Only queues with
* doorbell segments can do WCWR.
*/
ASSERT(eq->udb_qid == 0 && eq->pending == 1);
dst = (volatile void *)((uintptr_t)eq->udb +
UDBS_WR_OFFSET - UDBS_DB_OFFSET);
i = eq->pidx ? eq->pidx - 1 : eq->cap - 1;
src = (void *)&eq->desc[i];
while (src != (void *)&eq->desc[i + 1])
*dst++ = *src++;
membar_producer();
break;
}
case DOORBELL_UDBWC:
*eq->udb = LE_32(V_QID(eq->udb_qid) | val);
membar_producer();
break;
case DOORBELL_KDB:
t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
V_QID(eq->cntxt_id) | val);
break;
}
eq->pending = 0;
}
static int
reclaim_tx_descs(struct sge_txq *txq, int howmany)
{
struct tx_sdesc *txsd;
uint_t cidx, can_reclaim, reclaimed, txb_freed, hdls_freed;
struct sge_eq *eq = &txq->eq;
EQ_LOCK_ASSERT_OWNED(eq);
cidx = eq->spg->cidx; /* stable snapshot */
cidx = be16_to_cpu(cidx);
if (cidx >= eq->cidx)
can_reclaim = cidx - eq->cidx;
else
can_reclaim = cidx + eq->cap - eq->cidx;
if (can_reclaim == 0)
return (0);
txb_freed = hdls_freed = reclaimed = 0;
do {
int ndesc;
txsd = &txq->sdesc[eq->cidx];
ndesc = txsd->desc_used;
/* Firmware doesn't return "partial" credits. */
ASSERT(can_reclaim >= ndesc);
/*
* We always keep mblk around, even for immediate data. If mblk
* is NULL, this has to be the software descriptor for a credit
* flush work request.
*/
if (txsd->m != NULL)
freemsgchain(txsd->m);
#ifdef DEBUG
else {
ASSERT(txsd->txb_used == 0);
ASSERT(txsd->hdls_used == 0);
ASSERT(ndesc == 1);
}
#endif
txb_freed += txsd->txb_used;
hdls_freed += txsd->hdls_used;
reclaimed += ndesc;
eq->cidx += ndesc;
if (eq->cidx >= eq->cap)
eq->cidx -= eq->cap;
can_reclaim -= ndesc;
} while (can_reclaim && reclaimed < howmany);
eq->avail += reclaimed;
ASSERT(eq->avail < eq->cap); /* avail tops out at (cap - 1) */
txq->txb_avail += txb_freed;
txq->tx_dhdl_avail += hdls_freed;
ASSERT(txq->tx_dhdl_avail <= txq->tx_dhdl_total);
for (; hdls_freed; hdls_freed--) {
(void) ddi_dma_unbind_handle(txq->tx_dhdl[txq->tx_dhdl_cidx]);
if (++txq->tx_dhdl_cidx == txq->tx_dhdl_total)
txq->tx_dhdl_cidx = 0;
}
return (reclaimed);
}
static void
write_txqflush_wr(struct sge_txq *txq)
{
struct sge_eq *eq = &txq->eq;
struct fw_eq_flush_wr *wr;
struct tx_sdesc *txsd;
EQ_LOCK_ASSERT_OWNED(eq);
ASSERT(eq->avail > 0);
wr = (void *)&eq->desc[eq->pidx];
bzero(wr, sizeof (*wr));
wr->opcode = FW_EQ_FLUSH_WR;
wr->equiq_to_len16 = cpu_to_be32(V_FW_WR_LEN16(sizeof (*wr) / 16) |
F_FW_WR_EQUEQ | F_FW_WR_EQUIQ);
txsd = &txq->sdesc[eq->pidx];
txsd->m = NULL;
txsd->txb_used = 0;
txsd->hdls_used = 0;
txsd->desc_used = 1;
eq->pending++;
eq->avail--;
if (++eq->pidx == eq->cap)
eq->pidx = 0;
}
static int
t4_eth_rx(struct sge_iq *iq, const struct rss_header *rss, mblk_t *m)
{
bool csum_ok;
uint16_t err_vec;
struct sge_rxq *rxq = (void *)iq;
struct mblk_pair chain = {0};
struct adapter *sc = iq->adapter;
const struct cpl_rx_pkt *cpl = (const void *)(rss + 1);
iq->intr_next = iq->intr_params;
m->b_rptr += sc->sge.pktshift;
/* Compressed error vector is enabled for T6 only */
if (sc->params.tp.rx_pkt_encap)
/* It is enabled only in T6 config file */
err_vec = G_T6_COMPR_RXERR_VEC(ntohs(cpl->err_vec));
else
err_vec = ntohs(cpl->err_vec);
csum_ok = cpl->csum_calc && !err_vec;
/* TODO: what about cpl->ip_frag? */
if (csum_ok && !cpl->ip_frag) {
mac_hcksum_set(m, 0, 0, 0, 0xffff,
HCK_FULLCKSUM_OK | HCK_FULLCKSUM |
HCK_IPV4_HDRCKSUM_OK);
rxq->rxcsum++;
}
/* Add to the chain that we'll send up */
if (chain.head != NULL)
chain.tail->b_next = m;
else
chain.head = m;
chain.tail = m;
t4_mac_rx(rxq->port, rxq, chain.head);
rxq->rxpkts++;
rxq->rxbytes += be16_to_cpu(cpl->len);
return (0);
}
#define FL_HW_IDX(idx) ((idx) >> 3)
static inline void
ring_fl_db(struct adapter *sc, struct sge_fl *fl)
{
int desc_start, desc_last, ndesc;
uint32_t v = sc->params.arch.sge_fl_db ;
ndesc = FL_HW_IDX(fl->pending);
/* Hold back one credit if pidx = cidx */
if (FL_HW_IDX(fl->pidx) == FL_HW_IDX(fl->cidx))
ndesc--;
/*
* There are chances of ndesc modified above (to avoid pidx = cidx).
* If there is nothing to post, return.
*/
if (ndesc <= 0)
return;
desc_last = FL_HW_IDX(fl->pidx);
if (fl->pidx < fl->pending) {
/* There was a wrap */
desc_start = FL_HW_IDX(fl->pidx + fl->cap - fl->pending);
/* From desc_start to the end of list */
(void) ddi_dma_sync(fl->dhdl, desc_start * RX_FL_ESIZE, 0,
DDI_DMA_SYNC_FORDEV);
/* From start of list to the desc_last */
if (desc_last != 0)
(void) ddi_dma_sync(fl->dhdl, 0, desc_last *
RX_FL_ESIZE, DDI_DMA_SYNC_FORDEV);
} else {
/* There was no wrap, sync from start_desc to last_desc */
desc_start = FL_HW_IDX(fl->pidx - fl->pending);
(void) ddi_dma_sync(fl->dhdl, desc_start * RX_FL_ESIZE,
ndesc * RX_FL_ESIZE, DDI_DMA_SYNC_FORDEV);
}
if (is_t4(sc->params.chip))
v |= V_PIDX(ndesc);
else
v |= V_PIDX_T5(ndesc);
v |= V_QID(fl->cntxt_id) | V_PIDX(ndesc);
membar_producer();
t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL), v);
/*
* Update pending count:
* Deduct the number of descriptors posted
*/
fl->pending -= ndesc * 8;
}
static void
tx_reclaim_task(void *arg)
{
struct sge_txq *txq = arg;
TXQ_LOCK(txq);
reclaim_tx_descs(txq, txq->eq.qsize);
TXQ_UNLOCK(txq);
}
/* ARGSUSED */
static int
handle_sge_egr_update(struct sge_iq *iq, const struct rss_header *rss,
mblk_t *m)
{
const struct cpl_sge_egr_update *cpl = (const void *)(rss + 1);
unsigned int qid = G_EGR_QID(ntohl(cpl->opcode_qid));
struct adapter *sc = iq->adapter;
struct sge *s = &sc->sge;
struct sge_eq *eq;
struct sge_txq *txq;
txq = (void *)s->eqmap[qid - s->eq_start];
eq = &txq->eq;
txq->qflush++;
t4_mac_tx_update(txq->port, txq);
ddi_taskq_dispatch(sc->tq[eq->tx_chan], tx_reclaim_task,
(void *)txq, DDI_NOSLEEP);
return (0);
}
static int
handle_fw_rpl(struct sge_iq *iq, const struct rss_header *rss, mblk_t *m)
{
struct adapter *sc = iq->adapter;
const struct cpl_fw6_msg *cpl = (const void *)(rss + 1);
ASSERT(m == NULL);
if (cpl->type == FW_TYPE_RSSCPL || cpl->type == FW6_TYPE_RSSCPL) {
const struct rss_header *rss2;
rss2 = (const struct rss_header *)&cpl->data[0];
return (sc->cpl_handler[rss2->opcode](iq, rss2, m));
}
return (sc->fw_msg_handler[cpl->type](sc, &cpl->data[0]));
}
int
t4_alloc_tx_maps(struct adapter *sc, struct tx_maps *txmaps, int count,
int flags)
{
int i, rc;
txmaps->map_total = count;
txmaps->map_avail = txmaps->map_cidx = txmaps->map_pidx = 0;
txmaps->map = kmem_zalloc(sizeof (ddi_dma_handle_t) *
txmaps->map_total, flags);
for (i = 0; i < count; i++) {
rc = ddi_dma_alloc_handle(sc->dip, &sc->sge.dma_attr_tx,
DDI_DMA_SLEEP, 0, &txmaps->map[i]);
if (rc != DDI_SUCCESS) {
cxgb_printf(sc->dip, CE_WARN,
"%s: failed to allocate DMA handle (%d)",
__func__, rc);
return (rc == DDI_DMA_NORESOURCES ? ENOMEM : EINVAL);
}
txmaps->map_avail++;
}
return (0);
}
#define KS_UINIT(x) kstat_named_init(&kstatp->x, #x, KSTAT_DATA_ULONG)
#define KS_CINIT(x) kstat_named_init(&kstatp->x, #x, KSTAT_DATA_CHAR)
#define KS_U_SET(x, y) kstatp->x.value.ul = (y)
#define KS_U_FROM(x, y) kstatp->x.value.ul = (y)->x
#define KS_C_SET(x, ...) \
(void) snprintf(kstatp->x.value.c, 16, __VA_ARGS__)
/*
* cxgbe:X:config
*/
struct cxgbe_port_config_kstats {
kstat_named_t idx;
kstat_named_t nrxq;
kstat_named_t ntxq;
kstat_named_t first_rxq;
kstat_named_t first_txq;
kstat_named_t controller;
kstat_named_t factory_mac_address;
};
/*
* cxgbe:X:info
*/
struct cxgbe_port_info_kstats {
kstat_named_t transceiver;
kstat_named_t rx_ovflow0;
kstat_named_t rx_ovflow1;
kstat_named_t rx_ovflow2;
kstat_named_t rx_ovflow3;
kstat_named_t rx_trunc0;
kstat_named_t rx_trunc1;
kstat_named_t rx_trunc2;
kstat_named_t rx_trunc3;
kstat_named_t tx_pause;
kstat_named_t rx_pause;
};
static kstat_t *
setup_port_config_kstats(struct port_info *pi)
{
kstat_t *ksp;
struct cxgbe_port_config_kstats *kstatp;
int ndata;
dev_info_t *pdip = ddi_get_parent(pi->dip);
uint8_t *ma = &pi->hw_addr[0];
ndata = sizeof (struct cxgbe_port_config_kstats) /
sizeof (kstat_named_t);
ksp = kstat_create(T4_PORT_NAME, ddi_get_instance(pi->dip), "config",
"net", KSTAT_TYPE_NAMED, ndata, 0);
if (ksp == NULL) {
cxgb_printf(pi->dip, CE_WARN, "failed to initialize kstats.");
return (NULL);
}
kstatp = (struct cxgbe_port_config_kstats *)ksp->ks_data;
KS_UINIT(idx);
KS_UINIT(nrxq);
KS_UINIT(ntxq);
KS_UINIT(first_rxq);
KS_UINIT(first_txq);
KS_CINIT(controller);
KS_CINIT(factory_mac_address);
KS_U_SET(idx, pi->port_id);
KS_U_SET(nrxq, pi->nrxq);
KS_U_SET(ntxq, pi->ntxq);
KS_U_SET(first_rxq, pi->first_rxq);
KS_U_SET(first_txq, pi->first_txq);
KS_C_SET(controller, "%s%d", ddi_driver_name(pdip),
ddi_get_instance(pdip));
KS_C_SET(factory_mac_address, "%02X%02X%02X%02X%02X%02X",
ma[0], ma[1], ma[2], ma[3], ma[4], ma[5]);
/* Do NOT set ksp->ks_update. These kstats do not change. */
/* Install the kstat */
ksp->ks_private = (void *)pi;
kstat_install(ksp);
return (ksp);
}
static kstat_t *
setup_port_info_kstats(struct port_info *pi)
{
kstat_t *ksp;
struct cxgbe_port_info_kstats *kstatp;
int ndata;
ndata = sizeof (struct cxgbe_port_info_kstats) / sizeof (kstat_named_t);
ksp = kstat_create(T4_PORT_NAME, ddi_get_instance(pi->dip), "info",
"net", KSTAT_TYPE_NAMED, ndata, 0);
if (ksp == NULL) {
cxgb_printf(pi->dip, CE_WARN, "failed to initialize kstats.");
return (NULL);
}
kstatp = (struct cxgbe_port_info_kstats *)ksp->ks_data;
KS_CINIT(transceiver);
KS_UINIT(rx_ovflow0);
KS_UINIT(rx_ovflow1);
KS_UINIT(rx_ovflow2);
KS_UINIT(rx_ovflow3);
KS_UINIT(rx_trunc0);
KS_UINIT(rx_trunc1);
KS_UINIT(rx_trunc2);
KS_UINIT(rx_trunc3);
KS_UINIT(tx_pause);
KS_UINIT(rx_pause);
/* Install the kstat */
ksp->ks_update = update_port_info_kstats;
ksp->ks_private = (void *)pi;
kstat_install(ksp);
return (ksp);
}
static int
update_port_info_kstats(kstat_t *ksp, int rw)
{
struct cxgbe_port_info_kstats *kstatp =
(struct cxgbe_port_info_kstats *)ksp->ks_data;
struct port_info *pi = ksp->ks_private;
static const char *mod_str[] = { NULL, "LR", "SR", "ER", "TWINAX",
"active TWINAX", "LRM" };
uint32_t bgmap;
if (rw == KSTAT_WRITE)
return (0);
if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
KS_C_SET(transceiver, "unplugged");
else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
KS_C_SET(transceiver, "unknown");
else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
KS_C_SET(transceiver, "unsupported");
else if (pi->mod_type > 0 && pi->mod_type < ARRAY_SIZE(mod_str))
KS_C_SET(transceiver, "%s", mod_str[pi->mod_type]);
else
KS_C_SET(transceiver, "type %d", pi->mod_type);
#define GET_STAT(name) t4_read_reg64(pi->adapter, \
PORT_REG(pi->port_id, A_MPS_PORT_STAT_##name##_L))
#define GET_STAT_COM(name) t4_read_reg64(pi->adapter, \
A_MPS_STAT_##name##_L)
bgmap = G_NUMPORTS(t4_read_reg(pi->adapter, A_MPS_CMN_CTL));
if (bgmap == 0)
bgmap = (pi->port_id == 0) ? 0xf : 0;
else if (bgmap == 1)
bgmap = (pi->port_id < 2) ? (3 << (2 * pi->port_id)) : 0;
else
bgmap = 1;
KS_U_SET(rx_ovflow0, (bgmap & 1) ?
GET_STAT_COM(RX_BG_0_MAC_DROP_FRAME) : 0);
KS_U_SET(rx_ovflow1, (bgmap & 2) ?
GET_STAT_COM(RX_BG_1_MAC_DROP_FRAME) : 0);
KS_U_SET(rx_ovflow2, (bgmap & 4) ?
GET_STAT_COM(RX_BG_2_MAC_DROP_FRAME) : 0);
KS_U_SET(rx_ovflow3, (bgmap & 8) ?
GET_STAT_COM(RX_BG_3_MAC_DROP_FRAME) : 0);
KS_U_SET(rx_trunc0, (bgmap & 1) ?
GET_STAT_COM(RX_BG_0_MAC_TRUNC_FRAME) : 0);
KS_U_SET(rx_trunc1, (bgmap & 2) ?
GET_STAT_COM(RX_BG_1_MAC_TRUNC_FRAME) : 0);
KS_U_SET(rx_trunc2, (bgmap & 4) ?
GET_STAT_COM(RX_BG_2_MAC_TRUNC_FRAME) : 0);
KS_U_SET(rx_trunc3, (bgmap & 8) ?
GET_STAT_COM(RX_BG_3_MAC_TRUNC_FRAME) : 0);
KS_U_SET(tx_pause, GET_STAT(TX_PORT_PAUSE));
KS_U_SET(rx_pause, GET_STAT(RX_PORT_PAUSE));
return (0);
}
/*
* cxgbe:X:rxqY
*/
struct rxq_kstats {
kstat_named_t rxcsum;
kstat_named_t rxpkts;
kstat_named_t rxbytes;
kstat_named_t nomem;
};
static kstat_t *
setup_rxq_kstats(struct port_info *pi, struct sge_rxq *rxq, int idx)
{
struct kstat *ksp;
struct rxq_kstats *kstatp;
int ndata;
char str[16];
ndata = sizeof (struct rxq_kstats) / sizeof (kstat_named_t);
(void) snprintf(str, sizeof (str), "rxq%u", idx);
ksp = kstat_create(T4_PORT_NAME, ddi_get_instance(pi->dip), str, "rxq",
KSTAT_TYPE_NAMED, ndata, 0);
if (ksp == NULL) {
cxgb_printf(pi->dip, CE_WARN,
"%s: failed to initialize rxq kstats for queue %d.",
__func__, idx);
return (NULL);
}
kstatp = (struct rxq_kstats *)ksp->ks_data;
KS_UINIT(rxcsum);
KS_UINIT(rxpkts);
KS_UINIT(rxbytes);
KS_UINIT(nomem);
ksp->ks_update = update_rxq_kstats;
ksp->ks_private = (void *)rxq;
kstat_install(ksp);
return (ksp);
}
static int
update_rxq_kstats(kstat_t *ksp, int rw)
{
struct rxq_kstats *kstatp = (struct rxq_kstats *)ksp->ks_data;
struct sge_rxq *rxq = ksp->ks_private;
if (rw == KSTAT_WRITE)
return (0);
KS_U_FROM(rxcsum, rxq);
KS_U_FROM(rxpkts, rxq);
KS_U_FROM(rxbytes, rxq);
KS_U_FROM(nomem, rxq);
return (0);
}
/*
* cxgbe:X:txqY
*/
struct txq_kstats {
kstat_named_t txcsum;
kstat_named_t tso_wrs;
kstat_named_t imm_wrs;
kstat_named_t sgl_wrs;
kstat_named_t txpkt_wrs;
kstat_named_t txpkts_wrs;
kstat_named_t txpkts_pkts;
kstat_named_t txb_used;
kstat_named_t hdl_used;
kstat_named_t txb_full;
kstat_named_t dma_hdl_failed;
kstat_named_t dma_map_failed;
kstat_named_t qfull;
kstat_named_t qflush;
kstat_named_t pullup_early;
kstat_named_t pullup_late;
kstat_named_t pullup_failed;
};
static kstat_t *
setup_txq_kstats(struct port_info *pi, struct sge_txq *txq, int idx)
{
struct kstat *ksp;
struct txq_kstats *kstatp;
int ndata;
char str[16];
ndata = sizeof (struct txq_kstats) / sizeof (kstat_named_t);
(void) snprintf(str, sizeof (str), "txq%u", idx);
ksp = kstat_create(T4_PORT_NAME, ddi_get_instance(pi->dip), str, "txq",
KSTAT_TYPE_NAMED, ndata, 0);
if (ksp == NULL) {
cxgb_printf(pi->dip, CE_WARN,
"%s: failed to initialize txq kstats for queue %d.",
__func__, idx);
return (NULL);
}
kstatp = (struct txq_kstats *)ksp->ks_data;
KS_UINIT(txcsum);
KS_UINIT(tso_wrs);
KS_UINIT(imm_wrs);
KS_UINIT(sgl_wrs);
KS_UINIT(txpkt_wrs);
KS_UINIT(txpkts_wrs);
KS_UINIT(txpkts_pkts);
KS_UINIT(txb_used);
KS_UINIT(hdl_used);
KS_UINIT(txb_full);
KS_UINIT(dma_hdl_failed);
KS_UINIT(dma_map_failed);
KS_UINIT(qfull);
KS_UINIT(qflush);
KS_UINIT(pullup_early);
KS_UINIT(pullup_late);
KS_UINIT(pullup_failed);
ksp->ks_update = update_txq_kstats;
ksp->ks_private = (void *)txq;
kstat_install(ksp);
return (ksp);
}
static int
update_txq_kstats(kstat_t *ksp, int rw)
{
struct txq_kstats *kstatp = (struct txq_kstats *)ksp->ks_data;
struct sge_txq *txq = ksp->ks_private;
if (rw == KSTAT_WRITE)
return (0);
KS_U_FROM(txcsum, txq);
KS_U_FROM(tso_wrs, txq);
KS_U_FROM(imm_wrs, txq);
KS_U_FROM(sgl_wrs, txq);
KS_U_FROM(txpkt_wrs, txq);
KS_U_FROM(txpkts_wrs, txq);
KS_U_FROM(txpkts_pkts, txq);
KS_U_FROM(txb_used, txq);
KS_U_FROM(hdl_used, txq);
KS_U_FROM(txb_full, txq);
KS_U_FROM(dma_hdl_failed, txq);
KS_U_FROM(dma_map_failed, txq);
KS_U_FROM(qfull, txq);
KS_U_FROM(qflush, txq);
KS_U_FROM(pullup_early, txq);
KS_U_FROM(pullup_late, txq);
KS_U_FROM(pullup_failed, txq);
return (0);
}
|