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
|
/*********************************************************
* Copyright (C) 2004 VMware, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of the Common
* Development and Distribution License (the "License") version 1.0
* and no later version. You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* http://www.opensource.org/licenses/cddl1.php
*
* See the License for the specific language governing permissions
* and limitations under the License.
*
*********************************************************/
/*
* Copyright 2019 Joyent, Inc.
*/
#include <sys/types.h>
#include <sys/conf.h>
#include <sys/debug.h>
#include <sys/stropts.h>
#include <sys/stream.h>
#include <sys/strlog.h>
#include <sys/kmem.h>
#include <sys/stat.h>
#include <sys/kstat.h>
#include <sys/vtrace.h>
#include <sys/dlpi.h>
#include <sys/strsun.h>
#include <sys/ethernet.h>
#include <sys/modctl.h>
#include <sys/errno.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/gld.h>
#include <sys/pci.h>
#include <sys/strsubr.h>
/*
* This used to be defined in sys/gld.h, but was flagged as private,
* and we used it anyway. Now it no longer exists, and we're stuck
* with it for the time being.
*/
#ifndef GLD_MAX_MULTICAST
#define GLD_MAX_MULTICAST 64
#endif
#define __intptr_t_defined
#define _STDINT_H
#include "vm_basic_types.h"
#include "vmxnet2_def.h"
#include "vm_device_version.h"
#include "net.h"
#include "buildNumber.h"
#define SOLVMXNET_SUCCESS 1
#define SOLVMXNET_FAILURE 0
#ifdef SOLVMXNET_DEBUG_LEVEL
static int vxn_debug = SOLVMXNET_DEBUG_LEVEL;
#define DPRINTF(n, args) if (vxn_debug>(n)) cmn_err args
#else
#define DPRINTF(n, args)
#endif
static char ident[] = "VMware Ethernet Adapter b" BUILD_NUMBER_NUMERIC_STRING;
char _depends_on[] = {"misc/gld"};
#define MAX_NUM_RECV_BUFFERS 128
#define DEFAULT_NUM_RECV_BUFFERS 100
#define MAX_NUM_XMIT_BUFFERS 128
#define DEFAULT_NUM_XMIT_BUFFERS 100
#define CRC_POLYNOMIAL_LE 0xedb88320UL
#define SOLVMXNET_MAXNAME 20
#define MAX_TX_WAIT_ON_STOP 2000
#define ETHERALIGN 2
#define SLACKBYTES 4
#define MAXPKTBUF (14 + ETHERALIGN + ETHERMTU + SLACKBYTES)
#define QHIWATER (MAX_NUM_RECV_BUFFERS*ETHERMTU)
#define OUTB(dp, p, v) \
ddi_put8((dp)->vxnIOHdl, \
(uint8_t *)((caddr_t)((dp)->vxnIOp) + (p)), v)
#define OUTW(dp, p, v) \
ddi_put16((dp)->vxnIOHdl, \
(uint16_t *)((caddr_t)((dp)->vxnIOp) + (p)), v)
#define OUTL(dp, p, v) \
ddi_put32((dp)->vxnIOHdl, \
(uint32_t *)((caddr_t)((dp)->vxnIOp) + (p)), v)
#define INB(dp, p) \
ddi_get8((dp)->vxnIOHdl, \
(uint8_t *)(((caddr_t)(dp)->vxnIOp) + (p)))
#define INW(dp, p) \
ddi_get16((dp)->vxnIOHdl, \
(uint16_t *)(((caddr_t)(dp)->vxnIOp) + (p)))
#define INL(dp, p) \
ddi_get32((dp)->vxnIOHdl, \
(uint32_t *)(((caddr_t)(dp)->vxnIOp) + (p)))
#define VMXNET_INC(val, max) \
val++; \
if (UNLIKELY(val == max)) { \
val = 0; \
}
#define TX_RINGBUF_MBLK(dp, idx) (dp->txRingBuf[idx].mblk)
#define TX_RINGBUF_DMAMEM(dp, idx) (dp->txRingBuf[idx].dmaMem)
typedef struct {
caddr_t buf; /* Virtual address */
uint32_t phyBuf; /* Physical address */
size_t bufLen; /* Buffer length */
ddi_dma_cookie_t cookie; /* Dma cookie */
uint_t cookieCount; /* Cookie count */
ddi_dma_handle_t dmaHdl; /* Dma handle */
ddi_acc_handle_t dataAccHdl; /* Dada access handle */
} dma_buf_t;
typedef struct rx_dma_buf {
dma_buf_t dmaDesc; /* Dma descriptor */
mblk_t *mblk; /* Streams message block */
frtn_t freeCB; /* Free callback */
struct vxn_softc *softc; /* Back pointer to softc */
struct rx_dma_buf *next; /* Next one in list */
} rx_dma_buf_t;
typedef struct vxn_stats {
uint32_t errxmt; /* Transmit errors */
uint32_t errrcv; /* Receive errors */
uint32_t runt; /* Runt packets */
uint32_t norcvbuf; /* Buffer alloc errors */
uint32_t interrupts; /* Interrupts */
uint32_t defer; /* Deferred transmits */
} vxn_stats_t;
typedef struct tx_ring_buf {
mblk_t *mblk;
dma_buf_t dmaMem;
} tx_ring_buf_t;
typedef struct vxn_softc {
char drvName[SOLVMXNET_MAXNAME]; /* Driver name string */
int unit; /* Driver instance */
vxn_stats_t stats; /* Stats */
dev_info_t *dip; /* Info pointer */
ddi_iblock_cookie_t iblockCookie; /* Interrupt block cookie */
gld_mac_info_t *macInfo; /* GLD mac info */
ddi_acc_handle_t confHdl; /* Configuration space handle */
ddi_acc_handle_t vxnIOHdl; /* I/O space handle */
caddr_t vxnIOp; /* I/O space pointer */
boolean_t morphed; /* Adapter morphed ? */
kmutex_t intrlock; /* Interrupt lock */
kmutex_t xmitlock; /* Transmit lock */
kmutex_t rxlistlock; /* Rx free pool lock */
boolean_t nicActive; /* NIC active flag */
boolean_t inIntr; /* Interrupt processing flag */
struct ether_addr devAddr; /* MAC address */
uint32_t vxnNumRxBufs; /* Number of reveice buffers */
uint32_t vxnNumTxBufs; /* Number of transmit buffers */
dma_buf_t driverDataDmaMem; /* Driver Data (dma handle) */
Vmxnet2_DriverData *driverData; /* Driver Data */
void *driverDataPhy; /* Driver Data busaddr pointer */
Vmxnet2_RxRingEntry *rxRing; /* Receive ring */
Vmxnet2_TxRingEntry *txRing; /* Transmit ring */
ddi_dma_handle_t txDmaHdl; /* Tx buffers dma handle */
rx_dma_buf_t *rxRingBuffPtr[MAX_NUM_RECV_BUFFERS];
/* DMA buffers associated with rxRing */
tx_ring_buf_t txRingBuf[MAX_NUM_XMIT_BUFFERS]; /* tx Ring buffers */
rx_dma_buf_t *rxFreeBufList;
uint32_t rxNumFreeBufs; /* current # of buffers in pool */
uint32_t rxMaxFreeBufs; /* max # of buffers in pool */
uint32_t txPending; /* Pending transmits */
uint32_t maxTxFrags; /* Max Tx fragments */
int multiCount; /* Multicast address count */
struct ether_addr multicastList[GLD_MAX_MULTICAST]; /* Multicast list */
struct vxn_softc *next; /* Circular list of instances */
struct vxn_softc *prev;
} vxn_softc_t;
/* used for rx buffers or buffers allocated by ddi_dma_mem_alloc() */
static ddi_dma_attr_t vxn_dma_attrs = {
DMA_ATTR_V0, /* dma_attr version */
0, /* dma_attr_addr_lo */
(uint64_t)0xFFFFFFFF, /* dma_attr_addr_hi */
0x7FFFFFFF, /* dma_attr_count_max */
4, /* dma_attr_align */
0x3F, /* dma_attr_burstsizes */
1, /* dma_attr_minxfer */
(uint64_t)0xFFFFFFFF, /* dma_attr_maxxfer */
(uint64_t)0xFFFFFFFF, /* dma_attr_seg */
1, /* dma_attr_sgllen */
1, /* dma_attr_granular */
0, /* dma_attr_flags */
};
/* used for tx buffers */
static ddi_dma_attr_t vxn_dma_attrs_tx = {
DMA_ATTR_V0, /* dma_attr version */
0, /* dma_attr_addr_lo */
(uint64_t)0xFFFFFFFF, /* dma_attr_addr_hi */
0x7FFFFFFF, /* dma_attr_count_max */
1, /* dma_attr_align */
0x3F, /* dma_attr_burstsizes */
1, /* dma_attr_minxfer */
(uint64_t)0xFFFFFFFF, /* dma_attr_maxxfer */
(uint64_t)0xFFFFFFFF, /* dma_attr_seg */
1, /* dma_attr_sgllen */
1, /* dma_attr_granular */
0, /* dma_attr_flags */
};
static struct ether_addr etherbroadcastaddr = {
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
};
static struct ddi_device_acc_attr vxn_buf_attrs = {
DDI_DEVICE_ATTR_V0,
DDI_STRUCTURE_LE_ACC,
DDI_STRICTORDER_ACC
};
static struct ddi_device_acc_attr dev_attr = {
DDI_DEVICE_ATTR_V0,
DDI_STRUCTURE_LE_ACC,
DDI_STRICTORDER_ACC
};
static vxn_softc_t vxnList; /* for debugging */
static kmutex_t vxnListLock;
static void *Vxn_Memset(void *s, int c, size_t n);
static int Vxn_Reset(gld_mac_info_t *macInfo);
static int Vxn_SetPromiscuous(gld_mac_info_t *macInfo, int flag);
static int Vxn_GetStats(gld_mac_info_t *macInfo, struct gld_stats *gs);
static void Vxn_ApplyAddressFilter(vxn_softc_t *dp);
static int Vxn_SetMulticast(gld_mac_info_t *macinfo, uint8_t *ep, int flag);
static int Vxn_SetMacAddress(gld_mac_info_t *macInfo, uint8_t *mac);
static int Vxn_Start(gld_mac_info_t *macInfo);
static int Vxn_Stop(gld_mac_info_t *macInfo);
static void Vxn_FreeTxBuf(vxn_softc_t *dp, int idx);
static int Vxn_EncapTxBuf(vxn_softc_t *dp, mblk_t *mp, Vmxnet2_TxRingEntry *xre,
tx_ring_buf_t *txBuf);
static int Vxn_Send(gld_mac_info_t *macinfo, mblk_t *mp);
static boolean_t Vxn_TxComplete(vxn_softc_t *dp, boolean_t *reschedp);
static boolean_t Vxn_Receive(vxn_softc_t *dp);
static u_int Vxn_Interrupt(gld_mac_info_t *macInfo);
static void Vxn_ReclaimRxBuf(rx_dma_buf_t *rxDesc);
static void Vxn_FreeRxBuf(rx_dma_buf_t *rxDesc);
static rx_dma_buf_t *Vxn_AllocRxBuf(vxn_softc_t *dp, int cansleep);
static void Vxn_FreeInitBuffers(vxn_softc_t *dp);
static int Vxn_AllocInitBuffers(vxn_softc_t *dp);
static void Vxn_FreeDmaMem(dma_buf_t *dma);
static int Vxn_AllocDmaMem(vxn_softc_t *dp, int size, int cansleep, dma_buf_t *dma);
static void Vxn_FreeDriverData(vxn_softc_t *dp);
static int Vxn_AllocDriverData(vxn_softc_t *dp);
static int Vxn_Attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
static int Vxn_Detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
static int Vxn_AllocRxBufPool(vxn_softc_t *dp);
static void Vxn_FreeRxBufPool(vxn_softc_t *dp);
static rx_dma_buf_t * Vxn_AllocRxBufFromPool(vxn_softc_t *dp);
static void Vxn_FreeRxBufToPool(rx_dma_buf_t *rxDesc);
/*
*-----------------------------------------------------------------------------
* Vxn_Memset --
* memset() (Because bzero does not get resolved by module loader)
*
* Results:
* pointer to the memory area s
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void *
Vxn_Memset(void *s, int c, size_t n)
{
while (n--) {
((uint8_t *)s)[n] = c;
}
return s;
}
/*
*-----------------------------------------------------------------------------
* Vxn_Reset --
* Stub routine to reset hardware. Presently does nothing. Start/Stop should
* take care of resets.
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_Reset(gld_mac_info_t *macInfo)
{
return GLD_SUCCESS;
}
/*
*-----------------------------------------------------------------------------
* Vxn_SetPromiscuous --
* Set/Reset NIC to/from promiscuous mode
*
* Results:
* GLD_SUCCESS
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_SetPromiscuous(gld_mac_info_t *macInfo, int flag)
{
vxn_softc_t *dp = (vxn_softc_t *)macInfo->gldm_private;
Vmxnet2_DriverData *dd = dp->driverData;
mutex_enter(&dp->intrlock);
if (flag == GLD_MAC_PROMISC_PHYS) {
dd->ifflags |= VMXNET_IFF_PROMISC;
} else if (flag == GLD_MAC_PROMISC_MULTI) {
/*
* This should really set VMXNET_IFF_ALLMULTI,
* but unfortunately it doesn't exist. The next
* best thing would be to set the LADRFs to all
* 0xFFs and set VMXNET_IFF_MULTICAST, but that
* opens up a whole new set of potential pitfalls,
* so this is a reasonable temporary solution.
*/
dd->ifflags |= VMXNET_IFF_PROMISC;
} else if (flag == GLD_MAC_PROMISC_NONE) {
dd->ifflags &= ~VMXNET_IFF_PROMISC;
} else {
/* This could be GLD_MAC_PROMISC_NOOP? */
mutex_exit(&dp->intrlock);
cmn_err(CE_WARN, "%s%d: Vxn_SetPromiscuous: Unexpected mode flag: 0x%x",
dp->drvName, dp->unit, flag);
return GLD_FAILURE;
}
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_UPDATE_IFF);
mutex_exit(&dp->intrlock);
return GLD_SUCCESS;
}
/*
*-----------------------------------------------------------------------------
* Vxn_GetStats --
* Get driver specific stats
*
* Results:
* GLD_SUCCESS
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_GetStats(gld_mac_info_t *macInfo, struct gld_stats *gs)
{
vxn_softc_t *dp = (vxn_softc_t *)macInfo->gldm_private;
gs->glds_errxmt = dp->stats.errxmt;
gs->glds_errrcv = dp->stats.errrcv;
gs->glds_short = dp->stats.runt;
gs->glds_norcvbuf = dp->stats.norcvbuf;
gs->glds_intr = dp->stats.interrupts;
gs->glds_defer = dp->stats.defer;
return GLD_SUCCESS;
}
/*
*-----------------------------------------------------------------------------
* Vxn_ApplyAddressFilter --
* Go over multicast list and compute/apply address filter
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void
Vxn_ApplyAddressFilter(vxn_softc_t *dp)
{
uint8_t *ep;
int i, j, bit, byte;
uint32_t crc, poly = CRC_POLYNOMIAL_LE;
Vmxnet2_DriverData *dd = dp->driverData;
volatile uint16_t *mcastTable = (uint16_t *)dd->LADRF;
ASSERT(MUTEX_HELD(&dp->intrlock));
/* clear the multicast filter */
dd->LADRF[0] = 0;
dd->LADRF[1] = 0;
for (i = 0; i < dp->multiCount; i++) {
crc = 0xffffffff;
ep = (uint8_t *)&dp->multicastList[i].ether_addr_octet;
for (byte = 0; byte < 6; byte++) {
for (bit = *ep++, j = 0; j < 8; j++, bit >>= 1) {
int test;
test = ((bit ^ crc) & 0x01);
crc >>= 1;
if (test) {
crc = crc ^ poly;
}
}
}
crc = crc >> 26;
mcastTable[crc >> 4] |= 1 << (crc & 0xf);
}
}
/*
*-----------------------------------------------------------------------------
* Vxn_SetMulticast --
* Add delete entry from multicast list
*
* Results:
* GLD_FAILURE on failure
* GLD_SUCCESS on success
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_SetMulticast(gld_mac_info_t *macinfo, uint8_t *ep, int flag)
{
int i;
int copyLen;
vxn_softc_t *dp = (vxn_softc_t *)macinfo->gldm_private;
Vmxnet2_DriverData *dd = dp->driverData;
if (flag == GLD_MULTI_ENABLE) {
/*
* Exceeded multicast address limit
*/
if (dp->multiCount >= GLD_MAX_MULTICAST) {
return GLD_FAILURE;
}
/*
* Add mac address to multicast list
*/
bcopy(ep, dp->multicastList[dp->multiCount].ether_addr_octet,
ETHERADDRL);
dp->multiCount++;
}
else {
for (i=0; i<dp->multiCount; i++) {
if (bcmp(ep, dp->multicastList[i].ether_addr_octet, ETHERADDRL) == 0) {
goto found;
}
}
return GLD_FAILURE;
found:
/*
* Delete mac address from multicast list
*/
copyLen = (dp->multiCount - (i+1)) * sizeof(struct ether_addr);
if (copyLen > 0) {
bcopy(&dp->multicastList[i+1], &dp->multicastList[i], copyLen);
}
dp->multiCount--;
}
/*
* Compute address filter from list of addressed and apply it
*/
mutex_enter(&dp->intrlock);
Vxn_ApplyAddressFilter(dp);
if (dp->multiCount) {
ASSERT(dd->LADRF[0] || dd->LADRF[1]);
dd->ifflags |= VMXNET_IFF_MULTICAST;
} else {
ASSERT(!(dd->LADRF[0] || dd->LADRF[1]));
dd->ifflags &= ~VMXNET_IFF_MULTICAST;
}
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_UPDATE_IFF);
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_UPDATE_LADRF);
mutex_exit(&dp->intrlock);
return GLD_SUCCESS;
}
/*
*-----------------------------------------------------------------------------
* Vxn_SetMacAddress --
* Change device MAC address
*
* Results:
* GLD_SUCCESS
* GLD_FAILURE
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_SetMacAddress(gld_mac_info_t *macInfo, uint8_t *mac)
{
int i;
int err = GLD_SUCCESS;
vxn_softc_t * dp = (vxn_softc_t *)macInfo->gldm_private;
mutex_enter(&dp->intrlock);
mutex_enter(&dp->xmitlock);
/*
* Don't change MAC address on a running NIC
*/
if (dp->nicActive) {
err = GLD_FAILURE;
goto out;
}
/*
* Save new MAC address
*/
for (i = 0; i < 6; i++) {
dp->devAddr.ether_addr_octet[i] = mac[i];
}
/*
* Push new MAC address down into hardware
*/
for (i = 0; i < 6; i++) {
OUTB(dp, VMXNET_MAC_ADDR + i, mac[i]);
}
out:
mutex_exit(&dp->xmitlock);
mutex_exit(&dp->intrlock);
return err;
}
/*
*-----------------------------------------------------------------------------
* Vxn_Start --
* Device start routine. Called on "ifconfig plumb"
*
* Results:
* GLD_SUCCESS
* GLD_FAILURE
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_Start(gld_mac_info_t *macInfo)
{
int err = GLD_SUCCESS;
uint32_t r, capabilities, features;
vxn_softc_t * dp = (vxn_softc_t *)macInfo->gldm_private;
mutex_enter(&dp->intrlock);
mutex_enter(&dp->xmitlock);
if (!dp->nicActive) {
/*
* Register ring structure with hardware
*
* This downcast is OK because we requested a 32-bit physical address
*/
OUTL(dp, VMXNET_INIT_ADDR, (uint32_t)(uintptr_t)dp->driverDataPhy);
OUTL(dp, VMXNET_INIT_LENGTH, dp->driverData->length);
/*
* Make sure registeration succeded
*/
r = INL(dp, VMXNET_INIT_LENGTH);
if (!r) {
cmn_err(CE_WARN, "%s%d: Vxn_Start: failed to register ring",
dp->drvName, dp->unit);
err = GLD_FAILURE;
goto out;
}
/*
* Get maximum tx fragments supported
*/
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_GET_CAPABILITIES);
capabilities = INL(dp, VMXNET_COMMAND_ADDR);
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_GET_FEATURES);
features = INL(dp, VMXNET_COMMAND_ADDR);
DPRINTF(3, (CE_CONT, "%s%d: chip capabilities=0x%x features=0x%x\n",
dp->drvName, dp->unit, capabilities, features));
if ((capabilities & VMNET_CAP_SG) &&
(features & VMXNET_FEATURE_ZERO_COPY_TX)) {
dp->maxTxFrags = VMXNET2_SG_DEFAULT_LENGTH;
} else {
dp->maxTxFrags = 1;
}
ASSERT(dp->maxTxFrags >= 1);
/*
* Alloc Tx DMA handle
*/
vxn_dma_attrs_tx.dma_attr_sgllen = dp->maxTxFrags;
if (ddi_dma_alloc_handle(dp->dip, &vxn_dma_attrs_tx, DDI_DMA_SLEEP,
NULL, &dp->txDmaHdl) != DDI_SUCCESS) {
cmn_err(CE_WARN, "%s%d: Vxn_Start: failed to alloc tx dma handle",
dp->drvName, dp->unit);
err = GLD_FAILURE;
goto out;
}
/*
* Enable interrupts on the card
*/
dp->driverData->ifflags |= VMXNET_IFF_BROADCAST | VMXNET_IFF_DIRECTED;
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_INTR_ENABLE);
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_UPDATE_IFF);
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_UPDATE_LADRF);
dp->nicActive = TRUE;
}
out:
mutex_exit(&dp->xmitlock);
mutex_exit(&dp->intrlock);
return err;
}
/*
*-----------------------------------------------------------------------------
* Vxn_Stop --
* Device stop routine. Called on "ifconfig unplumb"
*
* Results:
* GLD_SUCCESS
* GLD_FAILURE
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_Stop(gld_mac_info_t *macInfo)
{
int i;
int err = GLD_SUCCESS;
vxn_softc_t * dp = (vxn_softc_t *)macInfo->gldm_private;
boolean_t resched;
mutex_enter(&dp->intrlock);
mutex_enter(&dp->xmitlock);
if (!dp->nicActive) {
goto out;
}
/*
* Disable interrupts
*/
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_INTR_DISABLE);
/*
* Wait for pending transmits
*/
if (dp->txPending) {
for (i=0; i < MAX_TX_WAIT_ON_STOP && dp->txPending; i++) {
delay(drv_usectohz(1000));
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_CHECK_TX_DONE);
(void) Vxn_TxComplete(dp, &resched);
/*
* Don't worry about rescheduling transmits - GLD handles
* this automatically.
*/
}
}
if (dp->txPending) {
cmn_err(CE_WARN, "%s%d: Vxn_Stop: giving up on %d pending transmits",
dp->drvName, dp->unit, dp->txPending);
}
OUTL(dp, VMXNET_INIT_ADDR, 0);
dp->nicActive = FALSE;
/*
* Free Tx DMA handle
*
* The ddi_dma_free_handle() man page says that ddi_dma_unbind_handle() must be called
* prior to calling ddi_dma_free_handle().
* However, call to ddi_dma_unbind_handle() is not required here, because
* ddi_dma_addr_bind_handle() and matching ddi_dma_unbind_handle() are called from
* Vxn_EncapTxBuf().
* xmitlock is held in Vxn_EncapTxBuf() as well as acquired above in Vxn_Stop().
*/
ddi_dma_free_handle(&dp->txDmaHdl);
dp->txDmaHdl = NULL;
out:
mutex_exit(&dp->xmitlock);
mutex_exit(&dp->intrlock);
return err;
}
/*
*-----------------------------------------------------------------------------
* Vxn_FreeTxBuf --
* Free transmit buffer
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void
Vxn_FreeTxBuf(vxn_softc_t *dp, int idx)
{
mblk_t **txMblkp = &TX_RINGBUF_MBLK(dp, idx);
dma_buf_t *dmaMem = &TX_RINGBUF_DMAMEM(dp, idx);
if (*txMblkp) {
freemsg(*txMblkp);
*txMblkp = NULL;
}
if (dmaMem->buf) {
Vxn_FreeDmaMem(dmaMem);
ASSERT(dmaMem->buf == NULL);
}
}
/*
*-----------------------------------------------------------------------------
* Vxn_EncapTxBuf --
* Go over dma mappings of Tx buffers and drop buffer physical address
* into ring entry
*
* Results:
* SOLVMXNET_SUCCESS on success
* SOLVMXNET_FAILURE on failure
*
* Side effects:
* None
*---------------- -------------------------------------------------------------
*/
static int
Vxn_EncapTxBuf(vxn_softc_t *dp,
mblk_t *mp,
Vmxnet2_TxRingEntry *xre,
tx_ring_buf_t *txBuf)
{
int frag;
int fragcount;
int rval;
mblk_t *tp;
mblk_t *mblk;
boolean_t needPullup = FALSE;
boolean_t dmaMemAlloced = FALSE;
ASSERT(txBuf);
ASSERT(txBuf->mblk == NULL);
ASSERT(MUTEX_HELD(&dp->xmitlock));
xre->sg.length = 0;
xre->flags = 0;
fragcount = 0;
for (tp = mp; tp != NULL; tp = tp->b_cont) {
fragcount++;
}
if (fragcount > dp->maxTxFrags) {
needPullup = TRUE;
}
pullup:
frag = 0;
if (needPullup) {
if (!(mblk = msgpullup(mp, -1))) {
cmn_err(CE_WARN, "%s%d: Vxn_EncapTxBuf: msgpullup failed",
dp->drvName, dp->unit);
goto err;
}
} else {
mblk = mp;
}
/*
* Go through message chain and drop packet pointers into ring
* scatter/gather array
*/
for (tp = mblk; tp != NULL; tp = tp->b_cont) {
uint_t nCookies;
ddi_dma_cookie_t dmaCookie;
int len = tp->b_wptr - tp->b_rptr;
if (len) {
/*
* Associate tx buffer with dma handle
*/
ASSERT(dp->txDmaHdl);
if ((rval = ddi_dma_addr_bind_handle(dp->txDmaHdl, NULL, (caddr_t)tp->b_rptr,
len, DDI_DMA_RDWR | DDI_DMA_STREAMING,
DDI_DMA_DONTWAIT, NULL,
&dmaCookie, &nCookies))
!= DDI_DMA_MAPPED) {
/*
* Try to handle bind failure caused by a page boundary spill
* by allocating a private dma buffer and copying data into it
*/
if ((rval == DDI_DMA_TOOBIG) && !dmaMemAlloced ) {
/*
* Force pullup
*/
if (!needPullup && (dp->maxTxFrags > 1)) {
needPullup = TRUE;
goto pullup;
}
if (Vxn_AllocDmaMem(dp, len, FALSE, &txBuf->dmaMem)
!= SOLVMXNET_SUCCESS) {
goto err;
}
dmaMemAlloced = TRUE;
/*
* Copy data into DMA capable buffer
*/
bcopy(tp->b_rptr, txBuf->dmaMem.buf, len);
/*
* Stick buffer physical addr in the ring
*/
xre->sg.sg[frag].addrLow = txBuf->dmaMem.phyBuf;
xre->sg.sg[frag].length = len;
frag++;
continue;
} else {
cmn_err(CE_WARN, "%s%d: Vxn_EncapTxBuf: failed (%d) to bind dma "
"handle for len %d. [dmaMemAlloced=%d]",
dp->drvName, dp->unit, rval, len, dmaMemAlloced);
goto err;
}
}
/*
* Extract tx buffer physical addresses from cookie
*/
while (nCookies) {
if (UNLIKELY(frag == dp->maxTxFrags)) {
(void)ddi_dma_unbind_handle(dp->txDmaHdl);
if (!needPullup) {
ASSERT(!dmaMemAlloced);
needPullup = TRUE;
goto pullup;
} else {
cmn_err(CE_WARN, "%s%d: Vxn_EncapTxBuf: "
"exceeded max (%d) fragments in message",
dp->drvName, dp->unit, dp->maxTxFrags);
goto err;
}
}
/*
* Stick it in the ring
*/
xre->sg.sg[frag].addrLow = dmaCookie.dmac_address;
xre->sg.sg[frag].length = dmaCookie.dmac_size;
frag++;
if (--nCookies) {
ddi_dma_nextcookie(dp->txDmaHdl, &dmaCookie);
}
}
(void)ddi_dma_unbind_handle(dp->txDmaHdl);
}
}
if (frag > 0) {
xre->sg.length = frag;
/* Give ownership to NIC */
xre->sg.addrType = NET_SG_PHYS_ADDR;
xre->ownership = VMXNET2_OWNERSHIP_NIC;
xre->flags |= VMXNET2_TX_CAN_KEEP;
txBuf->mblk = mblk;
/*
* If we called msgpullup to concatenate fragments, free
* original mblk now since we're going to return success.
*/
if (mblk != mp) {
freemsg(mp);
}
return SOLVMXNET_SUCCESS;
}
err:
if (mblk != NULL && mblk != mp) {
/*
* Free mblk allocated by msgpullup.
*/
freemsg(mblk);
}
if (dmaMemAlloced) {
ASSERT(txBuf->dmaMem.buf);
Vxn_FreeDmaMem(&txBuf->dmaMem);
}
return SOLVMXNET_FAILURE;
}
/*
*-----------------------------------------------------------------------------
* Vxn_Send --
* GLD Transmit routine. Starts packet hard tx.
*
* Results:
* GLD_SUCCESS on success
* GLD_FAILURE on failure
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_Send(gld_mac_info_t *macinfo, mblk_t *mp)
{
Vmxnet2_TxRingEntry *xre;
int err = GLD_SUCCESS;
vxn_softc_t *dp = (vxn_softc_t *)macinfo->gldm_private;
Vmxnet2_DriverData *dd = dp->driverData;
boolean_t resched = FALSE;
mutex_enter(&dp->xmitlock);
/*
* Check if ring entry at drop pointer is available
*/
if (TX_RINGBUF_MBLK(dp, dd->txDriverNext) != NULL) {
DPRINTF(3, (CE_NOTE, "%s%d: Vxn_Send: tx ring full",
dp->drvName, dp->unit));
err = GLD_NORESOURCES;
dd->txStopped = TRUE;
dp->stats.defer++;
goto out;
}
xre = &dp->txRing[dd->txDriverNext];
/*
* Drop packet into ring entry
*/
if (Vxn_EncapTxBuf(dp, mp, xre, &dp->txRingBuf[dd->txDriverNext])
!= SOLVMXNET_SUCCESS) {
err = GLD_FAILURE;
dp->stats.errxmt++;
goto out;
}
/*
* Increment drop pointer
*/
VMXNET_INC(dd->txDriverNext, dd->txRingLength);
dd->txNumDeferred++;
dp->txPending++;
/*
* Transmit, if number of pending packets > tx cluster length
*/
if (dd->txNumDeferred >= dd->txClusterLength) {
dd->txNumDeferred = 0;
/*
* Call hardware transmit
*/
INL(dp, VMXNET_TX_ADDR);
}
/*
* Clean up transmit ring. TX completion interrupts are not guaranteed
*/
(void) Vxn_TxComplete(dp, &resched);
out:
mutex_exit(&dp->xmitlock);
if (resched) {
/* Tell GLD to retry any deferred packets */
gld_sched(dp->macInfo);
}
return err;
}
/*
*-----------------------------------------------------------------------------
* Vxn_TxComplete --
* Scan Tx ring for completed transmits. Reclaim Tx buffers.
*
* Results:
* Returns TRUE if it found a completed transmit, FALSE otherwise.
* Also sets *reschedp to TRUE if the caller should call gld_sched
* to reschedule transmits (once all locks are dropped).
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static boolean_t
Vxn_TxComplete(vxn_softc_t *dp, boolean_t *reschedp)
{
Vmxnet2_DriverData *dd = dp->driverData;
boolean_t found = FALSE;
boolean_t needresched = FALSE;
ASSERT(MUTEX_HELD(&dp->xmitlock));
while (1) {
Vmxnet2_TxRingEntry *xre = &dp->txRing[dd->txDriverCur];
if (xre->ownership != VMXNET2_OWNERSHIP_DRIVER ||
(TX_RINGBUF_MBLK(dp, dd->txDriverCur) == NULL)) {
break;
}
found = TRUE;
Vxn_FreeTxBuf(dp, dd->txDriverCur);
dp->txPending--;
VMXNET_INC(dd->txDriverCur, dd->txRingLength);
if (dd->txStopped) {
needresched = TRUE;
dd->txStopped = FALSE;
}
}
*reschedp = needresched;
return found;
}
/*
*-----------------------------------------------------------------------------
* Vxn_Receive --
* Rx handler. First assembles the packets into a chain of mblks,
* then drops locks and passes them up the stack to GLD.
*
* Results:
* Returns TRUE if it find a packet ready for processing, FALSE
* otherwise.
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static boolean_t
Vxn_Receive(vxn_softc_t *dp)
{
int ringnext;
short pktlen;
Vmxnet2_DriverData *dd = dp->driverData;
rx_dma_buf_t *rxDesc;
rx_dma_buf_t *newRxDesc;
mblk_t *mblk;
mblk_t *head = NULL;
mblk_t **tail = &head;
mblk_t *next;
boolean_t found = FALSE; /* Did we find at least one packet? */
ASSERT(MUTEX_HELD(&dp->intrlock));
/*
* Walk receive ring looking for entries with ownership
* reverted back to driver
*/
while (1) {
Vmxnet2_RxRingEntry *rre;
rx_dma_buf_t **rbuf;
ringnext = dd->rxDriverNext;
rre = &dp->rxRing[ringnext];
rbuf = &dp->rxRingBuffPtr[ringnext];
if (rre->ownership != VMXNET2_OWNERSHIP_DRIVER) {
break;
}
found = TRUE;
pktlen = rre->actualLength;
if (pktlen < (60 - 4)) {
/*
* Ethernet header vlan tags are 4 bytes. Some vendors generate
* 60byte frames including vlan tags. When vlan tag
* is stripped, such frames become 60 - 4. (PR106153)
*/
dp->stats.errrcv++;
if (pktlen != 0) {
DPRINTF(3, (CE_CONT, "%s%d: runt packet\n", dp->drvName, dp->unit));
dp->stats.runt++;
}
} else {
/*
* Alloc new Rx buffer to replace current one
*/
newRxDesc = Vxn_AllocRxBufFromPool(dp);
if (newRxDesc) {
rxDesc = *rbuf;
mblk = rxDesc->mblk;
*rbuf = newRxDesc;
rre->paddr = newRxDesc->dmaDesc.phyBuf + ETHERALIGN;
rre->bufferLength = MAXPKTBUF - ETHERALIGN;
rre->actualLength = 0;
/*
* Advance write pointer past packet length
*/
mblk->b_wptr = mblk->b_rptr + pktlen;
/*
* Add to end of chain.
*/
mblk->b_next = NULL;
*tail = mblk;
tail = &mblk->b_next;
} else {
dp->stats.errrcv++;
dp->stats.norcvbuf++;
}
}
/* Give the descriptor back to NIC */
rre->ownership = VMXNET2_OWNERSHIP_NIC;
VMXNET_INC(dd->rxDriverNext, dd->rxRingLength);
}
/*
* Walk chain and pass mblks up to gld_recv one by one.
*/
mutex_exit(&dp->intrlock);
for (mblk = head; mblk != NULL; mblk = next) {
next = mblk->b_next;
mblk->b_next = NULL;
gld_recv(dp->macInfo, mblk);
}
mutex_enter(&dp->intrlock);
return (found);
}
/*
*-----------------------------------------------------------------------------
* Vxn_Interrupt --
* GLD interrupt handler. Scan: Rx ring for received packets, Tx ring for
* completed transmits
*
* Results:
* - DDI_INTR_CLAIMED (if we found something to do)
* - DDI_INTR_UNCLAIMED (if not)
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static u_int
Vxn_Interrupt(gld_mac_info_t *macInfo)
{
u_int ret = DDI_INTR_UNCLAIMED;
vxn_softc_t *dp = (vxn_softc_t *)macInfo->gldm_private;
boolean_t foundRx, foundTx;
boolean_t resched = FALSE;
mutex_enter(&dp->intrlock);
dp->inIntr = TRUE;
if (!dp->nicActive) {
goto out;
}
/*
* Ack interrupt
*/
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_INTR_ACK);
foundRx = Vxn_Receive(dp);
mutex_enter(&dp->xmitlock);
foundTx = Vxn_TxComplete(dp, &resched);
mutex_exit(&dp->xmitlock);
if (foundRx || foundTx) {
ret = DDI_INTR_CLAIMED;
dp->stats.interrupts++;
}
out:
dp->inIntr = FALSE;
mutex_exit(&dp->intrlock);
if (resched) {
gld_sched(dp->macInfo);
}
return ret;
}
/*
*-----------------------------------------------------------------------------
* Vxn_ReclaimRxBuf --
* Callback handler invoked by freemsg(). Frees Rx buffer memory and mappings
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void
Vxn_ReclaimRxBuf(rx_dma_buf_t *rxDesc)
{
Vxn_FreeRxBufToPool(rxDesc);
}
/*
*-----------------------------------------------------------------------------
* Vxn_FreeRxBuf --
* Free allocated Rx buffer
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void
Vxn_FreeRxBuf(rx_dma_buf_t *rxDesc)
{
ASSERT(rxDesc);
if (rxDesc->mblk) {
freemsg(rxDesc->mblk);
} else {
Vxn_FreeDmaMem(&rxDesc->dmaDesc);
kmem_free(rxDesc, sizeof(rx_dma_buf_t));
}
}
/*
*-----------------------------------------------------------------------------
* Vxn_AllocRxBuf --
* Allocate Rx buffer
*
* Results:
* Pointer to Rx buffer descriptor - on success
* NULL - on failure
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static rx_dma_buf_t *
Vxn_AllocRxBuf(vxn_softc_t *dp, int cansleep)
{
rx_dma_buf_t *rxDesc;
rxDesc = (rx_dma_buf_t *)kmem_zalloc(sizeof(rx_dma_buf_t),
cansleep ? KM_SLEEP : KM_NOSLEEP);
if (!rxDesc) {
cmn_err(CE_WARN, "%s%d: Vxn_AllocRxBuf: kmem_zalloc failed",
dp->drvName, dp->unit);
return NULL;
}
rxDesc->softc = dp;
/*
* Alloc dma-able packet memory
*/
if (Vxn_AllocDmaMem(dp, MAXPKTBUF, cansleep, &rxDesc->dmaDesc)
!= SOLVMXNET_SUCCESS) {
kmem_free(rxDesc, sizeof(rx_dma_buf_t));
return NULL;
}
/*
* Fill in free callback; fired by freemsg()
*/
rxDesc->freeCB.free_func = &Vxn_ReclaimRxBuf;
rxDesc->freeCB.free_arg = (caddr_t) rxDesc;
rxDesc->mblk = NULL;
return rxDesc;
}
/*
*-----------------------------------------------------------------------------
* Vxn_FreeInitBuffers --
* Free allocated Tx and Rx buffers
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void
Vxn_FreeInitBuffers(vxn_softc_t *dp)
{
int i;
for (i=0; i<dp->vxnNumRxBufs; i++) {
if (dp->rxRingBuffPtr[i]) {
Vxn_FreeRxBuf(dp->rxRingBuffPtr[i]);
dp->rxRingBuffPtr[i] = NULL;
}
}
for (i=0; i<dp->vxnNumTxBufs; i++) {
if (TX_RINGBUF_MBLK(dp, i)) {
Vxn_FreeTxBuf(dp, i);
}
}
/*
* Rx pool must get freed last. Rx buffers above will
* show up on the pool when freemsg callback fires.
*/
Vxn_FreeRxBufPool(dp);
}
/*
*-----------------------------------------------------------------------------
* Vxn_AllocRxBufPool --
* Allocate pool of rx buffers - 3 * configured Rx buffers
*
* Results:
* SOLVMXNET_SUCCESS/SOLVMXNET_FAILURE
*
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_AllocRxBufPool(vxn_softc_t *dp)
{
int i;
dp->rxFreeBufList = NULL;
// Allow list to double in size if needed. Any additional buffers
// that are allocated on the fly will be freed back to main memory.
dp->rxMaxFreeBufs = dp->vxnNumRxBufs * 6;
for (i = 0; i < dp->vxnNumRxBufs * 3; i++) {
rx_dma_buf_t *rxDesc;
/*
* Alloc rx buffer
*/
if (!(rxDesc = Vxn_AllocRxBuf(dp, TRUE))) {
cmn_err(CE_WARN, "%s%d: Vxn_AllocRxBufPool: failed to allocate memory",
dp->drvName, dp->unit);
dp->rxNumFreeBufs = i;
return SOLVMXNET_FAILURE;
}
/*
* Add to free list
*/
rxDesc->next = dp->rxFreeBufList;
dp->rxFreeBufList = rxDesc;
}
dp->rxNumFreeBufs = i;
return SOLVMXNET_SUCCESS;
}
/*
*-----------------------------------------------------------------------------
* Vxn_FreeRxBufPool --
* Free rx buffers pool
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void
Vxn_FreeRxBufPool(vxn_softc_t *dp)
{
while (dp->rxFreeBufList) {
rx_dma_buf_t *rxDesc = dp->rxFreeBufList;
/* unlink */
dp->rxFreeBufList = rxDesc->next;
ASSERT(rxDesc->mblk == NULL);
Vxn_FreeDmaMem(&rxDesc->dmaDesc);
kmem_free(rxDesc, sizeof(rx_dma_buf_t));
}
dp->rxNumFreeBufs = 0;
}
/*
*-----------------------------------------------------------------------------
* Vxn_AllocRxBufFromPool --
* Allocate Rx buffer from free pool
*
* Results:
* Pointer to Rx buffer descriptor - on success
* NULL - on failure
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static rx_dma_buf_t *
Vxn_AllocRxBufFromPool(vxn_softc_t *dp)
{
rx_dma_buf_t *rxDesc = NULL;
mutex_enter(&dp->rxlistlock);
if (dp->rxFreeBufList) {
rxDesc = dp->rxFreeBufList;
dp->rxFreeBufList = rxDesc->next;
ASSERT(dp->rxNumFreeBufs >= 1);
dp->rxNumFreeBufs--;
}
mutex_exit(&dp->rxlistlock);
if (!rxDesc) {
/*
* Try to allocate new descriptor from memory. Can't block here
* since we could be being called from interrupt context.
*/
DPRINTF(5, (CE_NOTE, "%s%d: allocating rx buf from memory",
dp->drvName, dp->unit));
if (!(rxDesc = Vxn_AllocRxBuf(dp, FALSE))) {
cmn_err(CE_WARN,
"%s%d: Vxn_AllocRxBufFromPool : pool rx alloc failed",
dp->drvName, dp->unit);
return NULL;
}
}
/*
* Allocate new message block for this buffer
*/
rxDesc->mblk = desballoc((uchar_t *)rxDesc->dmaDesc.buf + ETHERALIGN,
rxDesc->dmaDesc.bufLen - ETHERALIGN,
BPRI_MED, &rxDesc->freeCB);
if (!rxDesc->mblk) {
cmn_err(CE_WARN, "%s%d: Vxn_AllocRxBufFromPool : desballoc failed",
dp->drvName, dp->unit);
/* put back on free list */
Vxn_FreeRxBufToPool(rxDesc);
return NULL;
}
return rxDesc;
}
/*
*-----------------------------------------------------------------------------
* Vxn_FreeRxBufToPool --
* Return rx buffer to free pool
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void
Vxn_FreeRxBufToPool(rx_dma_buf_t *rxDesc)
{
vxn_softc_t *dp = rxDesc->softc;
rxDesc->mblk = NULL;
/*
* Insert on free list, or free if the list is full
*/
mutex_enter(&dp->rxlistlock);
if (dp->rxNumFreeBufs >= dp->rxMaxFreeBufs) {
DPRINTF(5, (CE_NOTE, "%s%d: freeing rx buf to memory",
dp->drvName, dp->unit));
Vxn_FreeRxBuf(rxDesc);
} else {
rxDesc->next = dp->rxFreeBufList;
dp->rxFreeBufList = rxDesc;
dp->rxNumFreeBufs++;
}
mutex_exit(&dp->rxlistlock);
}
/*
*-----------------------------------------------------------------------------
* Vxn_AllocInitBuffers --
* Allocated Rx buffers and init ring entries
*
* Results:
* SOLVMXNET_SUCCESS - on success
* SOLVMXNET_FAILURE - on failure
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_AllocInitBuffers(vxn_softc_t *dp)
{
Vmxnet2_DriverData *dd;
uint32_t i, offset;
dd = dp->driverData;
offset = sizeof(*dd);
/*
* Init shared structures
*/
dd->rxRingLength = dp->vxnNumRxBufs;
dd->rxRingOffset = offset;
dp->rxRing = (Vmxnet2_RxRingEntry *)((uintptr_t)dd + offset);
offset += dp->vxnNumRxBufs * sizeof(Vmxnet2_RxRingEntry);
dd->rxRingLength2 = 1;
dd->rxRingOffset2 = offset;
offset += sizeof(Vmxnet2_RxRingEntry);
dd->txRingLength = dp->vxnNumTxBufs;
dd->txRingOffset = offset;
dp->txRing = (Vmxnet2_TxRingEntry *)((uintptr_t)dd + offset);
offset += dp->vxnNumTxBufs * sizeof(Vmxnet2_TxRingEntry);
/*
* Alloc Rx buffers pool
*/
if ( Vxn_AllocRxBufPool(dp) != SOLVMXNET_SUCCESS) {
cmn_err(CE_WARN, "%s%d: Vxn_AllocInitBuffers: failed to alloc buf pool",
dp->drvName, dp->unit);
return SOLVMXNET_FAILURE;
}
/*
* Allocate receive buffers
*/
for (i = 0; i < dp->vxnNumRxBufs; i++) {
rx_dma_buf_t *rxDesc;
Vmxnet2_RxRingEntry *rre = &dp->rxRing[i];
if (!(rxDesc = Vxn_AllocRxBufFromPool(dp))) {
cmn_err(CE_WARN, "%s%d: Vxn_AllocInitBuffers: "
"failed to alloc buf from pool", dp->drvName, dp->unit);
goto err;
}
/*
* Init ring entries
*/
rre->paddr = rxDesc->dmaDesc.phyBuf + ETHERALIGN;
rre->bufferLength = MAXPKTBUF - ETHERALIGN;
rre->actualLength = 0;
dp->rxRingBuffPtr[i] = rxDesc;
rre->ownership = VMXNET2_OWNERSHIP_NIC;
}
dp->txDmaHdl = NULL;
/*
* Dummy recvRing2 tacked on to the end, with a single unusable entry
*/
dp->rxRing[i].paddr = 0;
dp->rxRing[i].bufferLength = 0;
dp->rxRing[i].actualLength = 0;
dp->rxRingBuffPtr[i] = NULL;
dp->rxRing[i].ownership = VMXNET2_OWNERSHIP_DRIVER;
dd->rxDriverNext = 0;
/*
* Give xmit ring ownership to DRIVER
*/
for (i = 0; i < dp->vxnNumTxBufs; i++) {
dp->txRing[i].ownership = VMXNET2_OWNERSHIP_DRIVER;
dp->txRingBuf[i].mblk = NULL;
dp->txRingBuf[i].dmaMem.buf = NULL;
dp->txRing[i].sg.sg[0].addrHi = 0;
}
dd->txDriverCur = dd->txDriverNext = 0;
dd->txStopped = FALSE;
return SOLVMXNET_SUCCESS;
err:
for (i=0; i<dp->vxnNumRxBufs; i++) {
if (dp->rxRingBuffPtr[i]) {
Vxn_FreeRxBuf(dp->rxRingBuffPtr[i]);
dp->rxRingBuffPtr[i] = NULL;
}
}
Vxn_FreeRxBufPool(dp);
return SOLVMXNET_FAILURE;
}
/*
*-----------------------------------------------------------------------------
* Vxn_FreeDmaMem --
* Free allocated dma memory
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void
Vxn_FreeDmaMem(dma_buf_t *dma)
{
ddi_dma_unbind_handle(dma->dmaHdl);
ddi_dma_mem_free(&dma->dataAccHdl);
ddi_dma_free_handle(&dma->dmaHdl);
dma->buf = NULL;
dma->phyBuf = (uint32_t)(uintptr_t)NULL;
dma->bufLen = 0;
}
/*
*-----------------------------------------------------------------------------
* Vxn_AllocDmaMem --
* Allocate dma-able memory and fill passed in dma descriptor pointer
* if successful
*
* Results:
* SOLVMXNET_SUCCESS on success
* SOLVMXNET_FAILURE on failure
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_AllocDmaMem(vxn_softc_t *dp, int size, int cansleep, dma_buf_t *dma)
{
/*
* Allocate handle
*/
if (ddi_dma_alloc_handle(dp->dip, &vxn_dma_attrs,
cansleep ? DDI_DMA_SLEEP : DDI_DMA_DONTWAIT,
NULL, &dma->dmaHdl) != DDI_SUCCESS) {
cmn_err(CE_WARN, "%s%d: Vxn_AllocDmaMem: failed to allocate handle",
dp->drvName, dp->unit);
return SOLVMXNET_FAILURE;
}
/*
* Allocate memory
*/
if (ddi_dma_mem_alloc(dma->dmaHdl, size, &vxn_buf_attrs, DDI_DMA_CONSISTENT,
cansleep ? DDI_DMA_SLEEP : DDI_DMA_DONTWAIT, NULL,
&dma->buf, &dma->bufLen, &dma->dataAccHdl)
!= DDI_SUCCESS) {
cmn_err(CE_WARN, "%s%d: Vxn_AllocDmaMem: "
"ddi_dma_mem_alloc %d bytes failed",
dp->drvName, dp->unit, size);
ddi_dma_free_handle(&dma->dmaHdl);
return SOLVMXNET_FAILURE;
}
/*
* Mapin memory
*/
if (ddi_dma_addr_bind_handle(dma->dmaHdl, NULL, dma->buf, dma->bufLen,
DDI_DMA_RDWR | DDI_DMA_STREAMING,
cansleep ? DDI_DMA_SLEEP : DDI_DMA_DONTWAIT,
NULL, &dma->cookie, &dma->cookieCount)
!= DDI_DMA_MAPPED) {
cmn_err(CE_WARN, "%s%d: Vxn_AllocDmaMem: failed to bind handle",
dp->drvName, dp->unit);
ddi_dma_mem_free(&dma->dataAccHdl);
ddi_dma_free_handle(&dma->dmaHdl);
return SOLVMXNET_FAILURE;
}
if (dma->cookieCount != 1) {
cmn_err(CE_WARN, "%s%d: Vxn_AllocDmaMem: too many DMA cookies",
dp->drvName, dp->unit);
Vxn_FreeDmaMem(dma);
return SOLVMXNET_FAILURE;
}
/*
* Save physical address (for easy use)
*/
dma->phyBuf = dma->cookie.dmac_address;
return SOLVMXNET_SUCCESS;
}
/*
*-----------------------------------------------------------------------------
* Vxn_FreeDriverData --
* Free driver data structures and Tx Rx buffers
*
* Results:
* None
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static void
Vxn_FreeDriverData(vxn_softc_t *dp)
{
Vxn_FreeInitBuffers(dp);
Vxn_FreeDmaMem(&dp->driverDataDmaMem);
}
/*
*-----------------------------------------------------------------------------
* Vxn_AllocDriverData --
* Allocate driver data structures and Tx Rx buffers on init
*
* Results:
* SOLVMXNET_SUCCESS on success
* SOLVMXNET_FAILURE on failure
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_AllocDriverData(vxn_softc_t *dp)
{
uint32_t r, driverDataSize;
/*
* Get configured receive buffers
*/
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_GET_NUM_RX_BUFFERS);
r = INL(dp, VMXNET_COMMAND_ADDR);
if (r == 0 || r > MAX_NUM_RECV_BUFFERS) {
r = DEFAULT_NUM_RECV_BUFFERS;
}
dp->vxnNumRxBufs = r;
/*
* Get configured transmit buffers
*/
OUTL(dp, VMXNET_COMMAND_ADDR, VMXNET_CMD_GET_NUM_TX_BUFFERS);
r = INL(dp, VMXNET_COMMAND_ADDR);
if (r == 0 || r > MAX_NUM_XMIT_BUFFERS) {
r = DEFAULT_NUM_XMIT_BUFFERS;
}
dp->vxnNumTxBufs = r;
/*
* Calculate shared data size and allocate memory for it
*/
driverDataSize =
sizeof(Vmxnet2_DriverData) +
/* numRecvBuffers + 1 for the dummy recvRing2 (used only by Windows) */
(dp->vxnNumRxBufs + 1) * sizeof(Vmxnet2_RxRingEntry) +
dp->vxnNumTxBufs * sizeof(Vmxnet2_TxRingEntry);
if (Vxn_AllocDmaMem(dp, driverDataSize, TRUE, &dp->driverDataDmaMem)
!= SOLVMXNET_SUCCESS) {
return SOLVMXNET_FAILURE;
}
/*
* Clear memory (bzero isn't resolved by module loader for some reason)
*/
ASSERT(dp->driverDataDmaMem.buf && dp->driverDataDmaMem.bufLen);
Vxn_Memset(dp->driverDataDmaMem.buf, 0, dp->driverDataDmaMem.bufLen);
dp->driverData = (Vmxnet2_DriverData *)dp->driverDataDmaMem.buf;
dp->driverDataPhy = (void *)(uintptr_t)dp->driverDataDmaMem.phyBuf;
/* So that the vmkernel can check it is compatible */
dp->driverData->magic = VMXNET2_MAGIC;
dp->driverData->length = driverDataSize;
/*
* Alloc rx/tx buffers, init ring, register with hardware etc.
*/
if (Vxn_AllocInitBuffers(dp) != SOLVMXNET_SUCCESS) {
Vxn_FreeDmaMem(&dp->driverDataDmaMem);
return SOLVMXNET_FAILURE;
}
DPRINTF(3, (CE_CONT, "%s%d: numRxBufs=(%d*%"FMT64"d) numTxBufs=(%d*%"FMT64"d)"
" driverDataSize=%d driverDataPhy=0x%p\n",
dp->drvName, dp->unit,
dp->vxnNumRxBufs, (uint64_t)sizeof(Vmxnet2_RxRingEntry),
dp->vxnNumTxBufs, (uint64_t)sizeof(Vmxnet2_TxRingEntry),
driverDataSize, dp->driverDataPhy));
return SOLVMXNET_SUCCESS;
}
/*
*-----------------------------------------------------------------------------
* Vxn_Attach --
* Probe and attach driver to stack
*
* Results:
* DDI_SUCCESS
* DDI_FAILURE
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_Attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int i, ret, len, unit;
const char *drvName;
ddi_acc_handle_t confHdl;
uint16_t vid, did;
uint8_t revid __unused;
struct pci_phys_spec *regs;
caddr_t vxnIOp;
ddi_acc_handle_t vxnIOHdl;
uint32_t vLow, vHigh;
gld_mac_info_t *macInfo;
vxn_softc_t *dp;
boolean_t morphed = FALSE;
uint_t regSpaceSize;
uint_t chip;
uint_t vxnIOSize;
if (cmd != DDI_ATTACH) {
return DDI_FAILURE;
}
unit = ddi_get_instance(dip);
drvName = ddi_driver_name(dip);
/*
* Check if chip is supported.
*/
if (pci_config_setup(dip, &confHdl) != DDI_SUCCESS) {
cmn_err(CE_WARN, "%s%d: pci_config_setup() failed", drvName, unit);
return DDI_FAILURE;
}
vid = pci_config_get16(confHdl, PCI_CONF_VENID);
did = pci_config_get16(confHdl, PCI_CONF_DEVID);
revid = pci_config_get8(confHdl, PCI_CONF_REVID);
if (vid == PCI_VENDOR_ID_VMWARE && did == PCI_DEVICE_ID_VMWARE_NET) {
/* Found vmxnet */
chip = VMXNET_CHIP;
}
else if (vid == PCI_VENDOR_ID_AMD && did == PCI_DEVICE_ID_AMD_VLANCE) {
/* Found vlance (maybe a vmxnet disguise) */
chip = LANCE_CHIP;
}
else {
/* Not Found */
DPRINTF(3, (CE_WARN, "%s: Vxn_Attach: wrong PCI venid/devid (0x%x, 0x%x)",
drvName, vid, did));
goto err;
}
DPRINTF(3, (CE_CONT, "%s%d: (vid: 0x%04x, did: 0x%04x, revid: 0x%02x)\n",
drvName, unit, vid, did, revid));
/*
* Get device properties
*/
regs = NULL;
len = 0;
if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"reg", (caddr_t)®s, &len) != DDI_PROP_SUCCESS) {
cmn_err(CE_WARN, "%s%d: Vxn_Attach: failed to get reg property",
drvName, unit);
goto err;
}
ASSERT(regs != NULL && len > 0);
/*
* Search device properties for IO-space
*/
for (i = 0; i <len / sizeof(struct pci_phys_spec); i++) {
if ((regs[i].pci_phys_hi & PCI_REG_ADDR_M) == PCI_ADDR_IO) {
regSpaceSize = regs[i].pci_size_low;
DPRINTF(5, (CE_CONT, "%s%d: Vxn_Attach: regSpaceSize=%d\n",
drvName, unit, regSpaceSize));
kmem_free(regs, len);
goto map_space_found;
}
}
cmn_err(CE_WARN, "%s%d: Vxn_Attach: failed to find IO space", drvName, unit);
kmem_free(regs, len);
goto err;
map_space_found:
/*
* Ensure we can access registers through IO space.
*/
ret = pci_config_get16(confHdl, PCI_CONF_COMM);
ret |= PCI_COMM_IO | PCI_COMM_ME;
pci_config_put16(confHdl, PCI_CONF_COMM, ret);
if (ddi_regs_map_setup(dip, i, (caddr_t *)&vxnIOp, 0, 0, &dev_attr,
&vxnIOHdl) != DDI_SUCCESS) {
cmn_err(CE_WARN, "%s%d: Vxn_Attach: ddi_regs_map_setup failed",
drvName, unit);
goto err;
}
if (chip == VMXNET_CHIP) {
vxnIOSize = VMXNET_CHIP_IO_RESV_SIZE;
}
else {
/*
* Since this is a vlance adapter we can only use it if
* its I/0 space is big enough for the adapter to be
* capable of morphing. This is the first requirement
* for this adapter to potentially be morphable. The
* layout of a morphable LANCE adapter is
*
* I/O space:
*
* |------------------|
* | LANCE IO PORTS |
* |------------------|
* | MORPH PORT |
* |------------------|
* | VMXNET IO PORTS |
* |------------------|
*
* VLance has 8 ports of size 4 bytes, the morph port is 4 bytes, and
* Vmxnet has 10 ports of size 4 bytes.
*
* We shift up the ioaddr with the size of the LANCE I/O space since
* we want to access the vmxnet ports. We also shift the ioaddr up by
* the MORPH_PORT_SIZE so other port access can be independent of
* whether we are Vmxnet or a morphed VLance. This means that when
* we want to access the MORPH port we need to subtract the size
* from ioaddr to get to it.
*/
vxnIOp += LANCE_CHIP_IO_RESV_SIZE + MORPH_PORT_SIZE;
vxnIOSize = LANCE_CHIP_IO_RESV_SIZE + MORPH_PORT_SIZE +
VMXNET_CHIP_IO_RESV_SIZE;
}
/*
* Do not attempt to morph non-morphable AMD PCnet
*/
if (vxnIOSize > regSpaceSize) {
cmn_err(CE_WARN, "%s%d: Vxn_Attach: "
"vlance device is not supported by this driver", drvName, unit);
goto err_free_regs_map;
}
/*
* Morph, if we found a vlance adapter
*/
if (chip == LANCE_CHIP) {
uint16_t magic;
/* Read morph port to verify that we can morph the adapter */
magic = ddi_get16(vxnIOHdl, (uint16_t *)(vxnIOp - MORPH_PORT_SIZE));
if (magic != LANCE_CHIP && magic != VMXNET_CHIP) {
cmn_err(CE_WARN, "%s%d: Vxn_Attach: Invalid magic, read: 0x%08X",
drvName, unit, magic);
goto err_free_regs_map;
}
/* Morph */
ddi_put16(vxnIOHdl, (uint16_t *)(vxnIOp - MORPH_PORT_SIZE), VMXNET_CHIP);
morphed = TRUE;
/* Verify that we morphed correctly */
magic = ddi_get16(vxnIOHdl, (uint16_t *)(vxnIOp - MORPH_PORT_SIZE));
if (magic != VMXNET_CHIP) {
cmn_err(CE_WARN, "%s%d: Vxn_Attach: Couldn't morph adapter."
" Invalid magic, read:: 0x%08X", drvName, unit, magic);
goto err_morph_back;
}
}
/*
* Check the version number of the device implementation
*/
vLow = (uint32_t)ddi_get32(vxnIOHdl,
(uint32_t *)(vxnIOp+VMXNET_LOW_VERSION));
vHigh = (uint32_t)ddi_get32(vxnIOHdl,
(uint32_t *)(vxnIOp+VMXNET_HIGH_VERSION));
if ((vLow & 0xffff0000) != (VMXNET2_MAGIC & 0xffff0000) ||
((VMXNET2_MAGIC < vLow) || (VMXNET2_MAGIC > vHigh))) {
cmn_err(CE_WARN, "%s%d: Vxn_Attach: driver version 0x%08X doesn't "
"match device 0x%08X:0x%08X",
drvName, unit, VMXNET2_MAGIC, vLow, vHigh);
goto err_version_mismatch;
}
/*
* Alloc soft state
*/
macInfo = gld_mac_alloc(dip);
if (!macInfo) {
cmn_err(CE_WARN, "%s%d: Vxn_Attach: gld_mac_alloc failed",
drvName, unit);
goto err_gld_mac_alloc;
}
dp = (vxn_softc_t *) kmem_zalloc(sizeof(vxn_softc_t), KM_SLEEP);
ASSERT(dp);
/*
* Get interrupt cookie
*/
if (ddi_get_iblock_cookie(dip, 0, &dp->iblockCookie) != DDI_SUCCESS) {
cmn_err(CE_WARN, "%s%d: Vxn_Attach: ddi_get_iblock_cookie failed",
drvName, unit);
goto err_get_iblock_cookie;
}
strncpy(dp->drvName, drvName, SOLVMXNET_MAXNAME);
dp->unit = unit;
dp->dip = dip;
dp->macInfo = macInfo;
dp->confHdl = confHdl;
dp->vxnIOHdl = vxnIOHdl;
dp->vxnIOp = vxnIOp;
dp->morphed = morphed;
dp->nicActive = FALSE;
dp->txPending = 0;
dp->maxTxFrags = 1;
/*
* Initialize mutexes
*/
mutex_init(&dp->intrlock, NULL, MUTEX_DRIVER, (void *)dp->iblockCookie);
mutex_init(&dp->xmitlock, NULL, MUTEX_DRIVER, (void *)dp->iblockCookie);
mutex_init(&dp->rxlistlock, NULL, MUTEX_DRIVER, (void *)dp->iblockCookie);
/*
* Allocate and initialize our private and shared data structures
*/
if (Vxn_AllocDriverData(dp) != SOLVMXNET_SUCCESS) {
goto err_alloc_driverdata;
}
/*
* Read the MAC address from the device
*/
for (i = 0; i < 6; i++) {
dp->devAddr.ether_addr_octet[i] =
(uint8_t)ddi_get8(vxnIOHdl, (uint8_t *)(vxnIOp + VMXNET_MAC_ADDR + i));
}
macInfo->gldm_vendor_addr = dp->devAddr.ether_addr_octet;
macInfo->gldm_broadcast_addr = etherbroadcastaddr.ether_addr_octet;
DPRINTF(3, (CE_CONT,
"MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
dp->devAddr.ether_addr_octet[0],
dp->devAddr.ether_addr_octet[1],
dp->devAddr.ether_addr_octet[2],
dp->devAddr.ether_addr_octet[3],
dp->devAddr.ether_addr_octet[4],
dp->devAddr.ether_addr_octet[5]));
/*
* Configure GLD entry points
*/
macInfo->gldm_devinfo = dip;
macInfo->gldm_private = (caddr_t)dp;
macInfo->gldm_cookie = dp->iblockCookie;
macInfo->gldm_reset = Vxn_Reset;
macInfo->gldm_start = Vxn_Start;
macInfo->gldm_stop = Vxn_Stop;
macInfo->gldm_set_mac_addr = Vxn_SetMacAddress;
macInfo->gldm_send = Vxn_Send;
macInfo->gldm_set_promiscuous = Vxn_SetPromiscuous;
macInfo->gldm_get_stats = Vxn_GetStats;
macInfo->gldm_ioctl = NULL;
macInfo->gldm_set_multicast= Vxn_SetMulticast;
macInfo->gldm_intr = Vxn_Interrupt;
macInfo->gldm_mctl = NULL;
macInfo->gldm_ident = (char *)ddi_driver_name(dip);
macInfo->gldm_type = DL_ETHER;
macInfo->gldm_minpkt = 0;
macInfo->gldm_maxpkt = ETHERMTU;
macInfo->gldm_addrlen = ETHERADDRL;
macInfo->gldm_saplen = -2;
macInfo->gldm_ppa = unit;
/*
* Register with GLD (Generic Lan Driver) framework
*/
if (gld_register(dip,
(char *)ddi_driver_name(dip), macInfo) != DDI_SUCCESS) {
goto err_gld_register;
}
/*
* Add interrupt to system.
*/
if (ddi_add_intr(dip, 0, NULL, NULL, gld_intr,
(caddr_t)macInfo) != DDI_SUCCESS) {
cmn_err(CE_WARN, "%s%d: ddi_add_intr failed", drvName, unit);
goto err_ddi_add_intr;
}
/*
* Add to list of interfaces.
*/
mutex_enter(&vxnListLock);
dp->next = &vxnList;
dp->prev = vxnList.prev;
vxnList.prev->next = dp;
vxnList.prev = dp;
mutex_exit(&vxnListLock);
/*
* Success
*/
return DDI_SUCCESS;
err_ddi_add_intr:
gld_unregister(macInfo);
err_gld_register:
Vxn_FreeDriverData(dp);
err_alloc_driverdata:
mutex_destroy(&dp->intrlock);
mutex_destroy(&dp->xmitlock);
err_get_iblock_cookie:
kmem_free(dp, sizeof(*dp));
gld_mac_free(macInfo);
err_gld_mac_alloc:
err_version_mismatch:
err_morph_back:
if (morphed) {
ddi_put16(vxnIOHdl, (uint16_t *)(vxnIOp - MORPH_PORT_SIZE), LANCE_CHIP);
}
err_free_regs_map:
ddi_regs_map_free(&vxnIOHdl);
err:
pci_config_teardown(&confHdl);
return DDI_FAILURE;
}
/*
*-----------------------------------------------------------------------------
* Vxn_Detach --
* Called on module unload
*
* Results:
* DDI_SUCCESS
* DDI_FAILURE
*
* Side effects:
* None
*-----------------------------------------------------------------------------
*/
static int
Vxn_Detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
gld_mac_info_t *macInfo;
vxn_softc_t *dp;
macInfo = (gld_mac_info_t *)ddi_get_driver_private(dip);
dp = (vxn_softc_t *)macInfo->gldm_private;
if (cmd == DDI_DETACH) {
/*
* Tear down interrupt
*/
ddi_remove_intr(dip, 0, macInfo->gldm_cookie);
gld_unregister(macInfo);
/*
* Quiesce hardware
*/
Vxn_Stop(macInfo);
/*
* Free driver-data, tx/rx buffers etc
*/
Vxn_FreeDriverData(dp);
/*
* Destroy locks
*/
mutex_destroy(&dp->intrlock);
mutex_destroy(&dp->xmitlock);
/*
* Unmorph
*/
if (dp->morphed) {
uint16_t magic;
/* Verify that we had morphed earlier */
magic = ddi_get16(dp->vxnIOHdl,
(uint16_t *)(dp->vxnIOp - MORPH_PORT_SIZE));
if (magic != VMXNET_CHIP) {
cmn_err(CE_WARN, "%s%d: Vxn_Detach: Adapter not morphed"
" magic=0x%08X", dp->drvName, dp->unit, magic);
}
else {
/* Unmorph */
ddi_put16(dp->vxnIOHdl,
(uint16_t *)(dp->vxnIOp - MORPH_PORT_SIZE), LANCE_CHIP);
/* Verify */
magic = ddi_get16(dp->vxnIOHdl,
(uint16_t *)(dp->vxnIOp - MORPH_PORT_SIZE));
if (magic != LANCE_CHIP) {
cmn_err(CE_WARN, "%s%d: Vxn_Detach: Unable to unmorph adapter"
" magic=0x%08X", dp->drvName, dp->unit, magic);
}
}
}
/*
* Release resister mappings
*/
ddi_regs_map_free(&dp->vxnIOHdl);
pci_config_teardown(&dp->confHdl);
/*
* Remove from list of interfaces.
*/
mutex_enter(&vxnListLock);
ASSERT(dp != &vxnList);
dp->prev->next = dp->next;
dp->next->prev = dp->prev;
mutex_exit(&vxnListLock);
/*
* Release memory
*/
kmem_free(dp, sizeof(*dp));
gld_mac_free(macInfo);
return DDI_SUCCESS;
}
else {
return DDI_FAILURE;
}
}
static struct module_info vxnminfo = {
0, /* mi_idnum */
"vmxnet", /* mi_idname */
0, /* mi_minpsz */
ETHERMTU, /* mi_maxpsz */
QHIWATER, /* mi_hiwat */
1, /* mi_lowat */
};
static struct qinit vxnrinit = {
NULL, /* qi_putp */
gld_rsrv, /* qi_srvp */
gld_open, /* qi_qopen */
gld_close, /* qi_qclose */
NULL, /* qi_qadmin */
&vxnminfo, /* qi_minfo */
NULL /* qi_mstat */
};
static struct qinit vxnwinit = {
gld_wput, /* qi_putp */
gld_wsrv, /* qi_srvp */
NULL, /* qi_qopen */
NULL, /* qi_qclose */
NULL, /* qi_qadmin */
&vxnminfo, /* qi_minfo */
NULL /* qi_mstat */
};
static struct streamtab vxn_info = {
&vxnrinit, /* st_rdinit */
&vxnwinit, /* st_wrinit */
NULL, /* st_muxrinit */
NULL /* st_muxwrinit */
};
static struct cb_ops cb_vxn_ops = {
nulldev, /* cb_open */
nulldev, /* cb_close */
nodev, /* cb_strategy */
nodev, /* cb_print */
nodev, /* cb_dump */
nodev, /* cb_read */
nodev, /* cb_write */
nodev, /* cb_ioctl */
nodev, /* cb_devmap */
nodev, /* cb_mmap */
nodev, /* cb_segmap */
nochpoll, /* cb_chpoll */
ddi_prop_op, /* cb_prop_op */
&vxn_info, /* cb_stream */
D_NEW|D_MP /* cb_flag */
};
static struct dev_ops vxn_ops = {
DEVO_REV, /* devo_rev */
0, /* devo_refcnt */
gld_getinfo, /* devo_getinfo */
nulldev, /* devo_identify */
nulldev, /* devo_probe */
Vxn_Attach, /* devo_attach */
Vxn_Detach, /* devo_detach */
nodev, /* devo_reset */
&cb_vxn_ops, /* devo_cb_ops */
NULL, /* devo_bus_ops */
ddi_power /* devo_power */
};
static struct modldrv modldrv = {
&mod_driverops,
ident,
&vxn_ops,
};
static struct modlinkage modlinkage = {
MODREV_1, {&modldrv, NULL,}
};
/*
* Module load entry point
*/
int
_init(void)
{
int err;
DPRINTF(5, (CE_CONT, "vxn: _init:\n"));
/* Initialize interface list */
vxnList.next = vxnList.prev = &vxnList;
mutex_init(&vxnListLock, NULL, MUTEX_DRIVER, NULL);
if ((err = mod_install(&modlinkage)) != 0) {
mutex_destroy(&vxnListLock);
}
return err;
}
/*
* Module unload entry point
*/
int
_fini(void)
{
int err;
DPRINTF(5, (CE_CONT, "vxn: _fini:\n"));
if ((err = mod_remove(&modlinkage)) == 0) {
mutex_destroy(&vxnListLock);
}
return err;
}
/*
* Module info entry point
*/
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
|