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
|
/*-
* Copyright (c) 1999,2000,2001 Jonathan Lemon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/bpf.h>
#include <net/if_types.h>
#include <net/if_vlan_var.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <vm/vm.h> /* for vtophys */
#include <vm/pmap.h> /* for vtophys */
#include <machine/clock.h> /* for DELAY */
#include <machine/bus_memio.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include <dev/gx/if_gxreg.h>
#include <dev/gx/if_gxvar.h>
MODULE_DEPEND(gx, pci, 1, 1, 1);
MODULE_DEPEND(gx, ether, 1, 1, 1);
MODULE_DEPEND(gx, miibus, 1, 1, 1);
#include "miibus_if.h"
#define TUNABLE_TX_INTR_DELAY 100
#define TUNABLE_RX_INTR_DELAY 100
#define GX_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_IP_FRAGS)
/*
* Various supported device vendors/types and their names.
*/
struct gx_device {
u_int16_t vendor;
u_int16_t device;
int version_flags;
u_int32_t version_ipg;
char *name;
};
static struct gx_device gx_devs[] = {
{ INTEL_VENDORID, DEVICEID_WISEMAN,
GXF_FORCE_TBI | GXF_OLD_REGS,
10 | 2 << 10 | 10 << 20,
"Intel Gigabit Ethernet (82542)" },
{ INTEL_VENDORID, DEVICEID_LIVINGOOD_FIBER,
GXF_DMA | GXF_ENABLE_MWI | GXF_CSUM,
6 | 8 << 10 | 6 << 20,
"Intel Gigabit Ethernet (82543GC-F)" },
{ INTEL_VENDORID, DEVICEID_LIVINGOOD_COPPER,
GXF_DMA | GXF_ENABLE_MWI | GXF_CSUM,
8 | 8 << 10 | 6 << 20,
"Intel Gigabit Ethernet (82543GC-T)" },
#if 0
/* notyet.. */
{ INTEL_VENDORID, DEVICEID_CORDOVA_FIBER,
GXF_DMA | GXF_ENABLE_MWI | GXF_CSUM,
6 | 8 << 10 | 6 << 20,
"Intel Gigabit Ethernet (82544EI-F)" },
{ INTEL_VENDORID, DEVICEID_CORDOVA_COPPER,
GXF_DMA | GXF_ENABLE_MWI | GXF_CSUM,
8 | 8 << 10 | 6 << 20,
"Intel Gigabit Ethernet (82544EI-T)" },
{ INTEL_VENDORID, DEVICEID_CORDOVA2_COPPER,
GXF_DMA | GXF_ENABLE_MWI | GXF_CSUM,
8 | 8 << 10 | 6 << 20,
"Intel Gigabit Ethernet (82544GC-T)" },
#endif
{ 0, 0, 0, 0, NULL }
};
static struct gx_regs new_regs = {
GX_RX_RING_BASE, GX_RX_RING_LEN,
GX_RX_RING_HEAD, GX_RX_RING_TAIL,
GX_RX_INTR_DELAY, GX_RX_DMA_CTRL,
GX_TX_RING_BASE, GX_TX_RING_LEN,
GX_TX_RING_HEAD, GX_TX_RING_TAIL,
GX_TX_INTR_DELAY, GX_TX_DMA_CTRL,
};
static struct gx_regs old_regs = {
GX_RX_OLD_RING_BASE, GX_RX_OLD_RING_LEN,
GX_RX_OLD_RING_HEAD, GX_RX_OLD_RING_TAIL,
GX_RX_OLD_INTR_DELAY, GX_RX_OLD_DMA_CTRL,
GX_TX_OLD_RING_BASE, GX_TX_OLD_RING_LEN,
GX_TX_OLD_RING_HEAD, GX_TX_OLD_RING_TAIL,
GX_TX_OLD_INTR_DELAY, GX_TX_OLD_DMA_CTRL,
};
static int gx_probe(device_t dev);
static int gx_attach(device_t dev);
static int gx_detach(device_t dev);
static void gx_shutdown(device_t dev);
static void gx_intr(void *xsc);
static void gx_init(void *xsc);
static struct gx_device *gx_match(device_t dev);
static void gx_eeprom_getword(struct gx_softc *gx, int addr,
u_int16_t *dest);
static int gx_read_eeprom(struct gx_softc *gx, caddr_t dest, int off,
int cnt);
static int gx_ifmedia_upd(struct ifnet *ifp);
static void gx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
static int gx_miibus_readreg(device_t dev, int phy, int reg);
static void gx_miibus_writereg(device_t dev, int phy, int reg, int value);
static void gx_miibus_statchg(device_t dev);
static int gx_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
static void gx_setmulti(struct gx_softc *gx);
static void gx_reset(struct gx_softc *gx);
static void gx_phy_reset(struct gx_softc *gx);
static void gx_release(struct gx_softc *gx);
static void gx_stop(struct gx_softc *gx);
static void gx_watchdog(struct ifnet *ifp);
static void gx_start(struct ifnet *ifp);
static int gx_init_rx_ring(struct gx_softc *gx);
static void gx_free_rx_ring(struct gx_softc *gx);
static int gx_init_tx_ring(struct gx_softc *gx);
static void gx_free_tx_ring(struct gx_softc *gx);
static device_method_t gx_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, gx_probe),
DEVMETHOD(device_attach, gx_attach),
DEVMETHOD(device_detach, gx_detach),
DEVMETHOD(device_shutdown, gx_shutdown),
/* MII interface */
DEVMETHOD(miibus_readreg, gx_miibus_readreg),
DEVMETHOD(miibus_writereg, gx_miibus_writereg),
DEVMETHOD(miibus_statchg, gx_miibus_statchg),
{ 0, 0 }
};
static driver_t gx_driver = {
"gx",
gx_methods,
sizeof(struct gx_softc)
};
static devclass_t gx_devclass;
DRIVER_MODULE(gx, pci, gx_driver, gx_devclass, 0, 0);
DRIVER_MODULE(miibus, gx, miibus_driver, miibus_devclass, 0, 0);
static struct gx_device *
gx_match(device_t dev)
{
int i;
for (i = 0; gx_devs[i].name != NULL; i++) {
if ((pci_get_vendor(dev) == gx_devs[i].vendor) &&
(pci_get_device(dev) == gx_devs[i].device))
return (&gx_devs[i]);
}
return (NULL);
}
static int
gx_probe(device_t dev)
{
struct gx_device *gx_dev;
gx_dev = gx_match(dev);
if (gx_dev == NULL)
return (ENXIO);
device_set_desc(dev, gx_dev->name);
return (0);
}
static int
gx_attach(device_t dev)
{
struct gx_softc *gx;
struct gx_device *gx_dev;
struct ifnet *ifp;
u_int32_t command;
int rid, s;
int error = 0;
s = splimp();
gx = device_get_softc(dev);
bzero(gx, sizeof(struct gx_softc));
gx->gx_dev = dev;
gx_dev = gx_match(dev);
gx->gx_vflags = gx_dev->version_flags;
gx->gx_ipg = gx_dev->version_ipg;
mtx_init(&gx->gx_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
MTX_DEF | MTX_RECURSE);
GX_LOCK(gx);
/*
* Map control/status registers.
*/
command = pci_read_config(dev, PCIR_COMMAND, 4);
command |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN;
if (gx->gx_vflags & GXF_ENABLE_MWI)
command |= PCIM_CMD_MWIEN;
pci_write_config(dev, PCIR_COMMAND, command, 4);
command = pci_read_config(dev, PCIR_COMMAND, 4);
/* XXX check cache line size? */
if ((command & PCIM_CMD_MEMEN) == 0) {
device_printf(dev, "failed to enable memory mapping!\n");
error = ENXIO;
goto fail;
}
rid = GX_PCI_LOMEM;
gx->gx_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
0, ~0, 1, RF_ACTIVE);
#if 0
/* support PIO mode */
rid = PCI_LOIO;
gx->gx_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
0, ~0, 1, RF_ACTIVE);
#endif
if (gx->gx_res == NULL) {
device_printf(dev, "couldn't map memory\n");
error = ENXIO;
goto fail;
}
gx->gx_btag = rman_get_bustag(gx->gx_res);
gx->gx_bhandle = rman_get_bushandle(gx->gx_res);
/* Allocate interrupt */
rid = 0;
gx->gx_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
RF_SHAREABLE | RF_ACTIVE);
if (gx->gx_irq == NULL) {
device_printf(dev, "couldn't map interrupt\n");
error = ENXIO;
goto fail;
}
error = bus_setup_intr(dev, gx->gx_irq, INTR_TYPE_NET,
gx_intr, gx, &gx->gx_intrhand);
if (error) {
device_printf(dev, "couldn't setup irq\n");
goto fail;
}
/* compensate for different register mappings */
if (gx->gx_vflags & GXF_OLD_REGS)
gx->gx_reg = old_regs;
else
gx->gx_reg = new_regs;
if (gx_read_eeprom(gx, (caddr_t)&gx->arpcom.ac_enaddr,
GX_EEMAP_MAC, 3)) {
device_printf(dev, "failed to read station address\n");
error = ENXIO;
goto fail;
}
device_printf(dev, "Ethernet address: %6D\n",
gx->arpcom.ac_enaddr, ":");
/* Allocate the ring buffers. */
gx->gx_rdata = contigmalloc(sizeof(struct gx_ring_data), M_DEVBUF,
M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
if (gx->gx_rdata == NULL) {
device_printf(dev, "no memory for list buffers!\n");
error = ENXIO;
goto fail;
}
bzero(gx->gx_rdata, sizeof(struct gx_ring_data));
/* Set default tuneable values. */
gx->gx_tx_intr_delay = TUNABLE_TX_INTR_DELAY;
gx->gx_rx_intr_delay = TUNABLE_RX_INTR_DELAY;
/* Set up ifnet structure */
ifp = &gx->arpcom.ac_if;
ifp->if_softc = gx;
ifp->if_unit = device_get_unit(dev);
ifp->if_name = "gx";
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_ioctl = gx_ioctl;
ifp->if_output = ether_output;
ifp->if_start = gx_start;
ifp->if_watchdog = gx_watchdog;
ifp->if_init = gx_init;
ifp->if_mtu = ETHERMTU;
ifp->if_snd.ifq_maxlen = GX_TX_RING_CNT - 1;
ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
/* see if we can enable hardware checksumming */
if (gx->gx_vflags & GXF_CSUM) {
ifp->if_capabilities = IFCAP_HWCSUM;
ifp->if_capenable = ifp->if_capabilities;
}
/* figure out transciever type */
if (gx->gx_vflags & GXF_FORCE_TBI ||
CSR_READ_4(gx, GX_STATUS) & GX_STAT_TBIMODE)
gx->gx_tbimode = 1;
if (gx->gx_tbimode) {
/* SERDES transceiver */
ifmedia_init(&gx->gx_media, IFM_IMASK, gx_ifmedia_upd,
gx_ifmedia_sts);
ifmedia_add(&gx->gx_media,
IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
ifmedia_add(&gx->gx_media, IFM_ETHER|IFM_AUTO, 0, NULL);
ifmedia_set(&gx->gx_media, IFM_ETHER|IFM_AUTO);
} else {
/* GMII/MII transceiver */
gx_phy_reset(gx);
if (mii_phy_probe(dev, &gx->gx_miibus, gx_ifmedia_upd,
gx_ifmedia_sts)) {
device_printf(dev, "GMII/MII, PHY not detected\n");
error = ENXIO;
goto fail;
}
}
/*
* Call MI attach routines.
*/
ether_ifattach(ifp, gx->arpcom.ac_enaddr);
GX_UNLOCK(gx);
splx(s);
return (0);
fail:
GX_UNLOCK(gx);
gx_release(gx);
splx(s);
return (error);
}
static void
gx_release(struct gx_softc *gx)
{
bus_generic_detach(gx->gx_dev);
if (gx->gx_miibus)
device_delete_child(gx->gx_dev, gx->gx_miibus);
if (gx->gx_intrhand)
bus_teardown_intr(gx->gx_dev, gx->gx_irq, gx->gx_intrhand);
if (gx->gx_irq)
bus_release_resource(gx->gx_dev, SYS_RES_IRQ, 0, gx->gx_irq);
if (gx->gx_res)
bus_release_resource(gx->gx_dev, SYS_RES_MEMORY,
GX_PCI_LOMEM, gx->gx_res);
}
static void
gx_init(void *xsc)
{
struct gx_softc *gx = (struct gx_softc *)xsc;
struct ifmedia *ifm;
struct ifnet *ifp;
device_t dev;
u_int16_t *m;
u_int32_t ctrl;
int s, i, tmp;
dev = gx->gx_dev;
ifp = &gx->arpcom.ac_if;
s = splimp();
GX_LOCK(gx);
/* Disable host interrupts, halt chip. */
gx_reset(gx);
/* disable I/O, flush RX/TX FIFOs, and free RX/TX buffers */
gx_stop(gx);
/* Load our MAC address, invalidate other 15 RX addresses. */
m = (u_int16_t *)&gx->arpcom.ac_enaddr[0];
CSR_WRITE_4(gx, GX_RX_ADDR_BASE, (m[1] << 16) | m[0]);
CSR_WRITE_4(gx, GX_RX_ADDR_BASE + 4, m[2] | GX_RA_VALID);
for (i = 1; i < 16; i++)
CSR_WRITE_8(gx, GX_RX_ADDR_BASE + i * 8, (u_quad_t)0);
/* Program multicast filter. */
gx_setmulti(gx);
/* Init RX ring. */
gx_init_rx_ring(gx);
/* Init TX ring. */
gx_init_tx_ring(gx);
if (gx->gx_vflags & GXF_DMA) {
/* set up DMA control */
CSR_WRITE_4(gx, gx->gx_reg.r_rx_dma_ctrl, 0x00010000);
CSR_WRITE_4(gx, gx->gx_reg.r_tx_dma_ctrl, 0x00000000);
}
/* enable receiver */
ctrl = GX_RXC_ENABLE | GX_RXC_RX_THOLD_EIGHTH | GX_RXC_RX_BSIZE_2K;
ctrl |= GX_RXC_BCAST_ACCEPT;
/* Enable or disable promiscuous mode as needed. */
if (ifp->if_flags & IFF_PROMISC)
ctrl |= GX_RXC_UNI_PROMISC;
/* This is required if we want to accept jumbo frames */
if (ifp->if_mtu > ETHERMTU)
ctrl |= GX_RXC_LONG_PKT_ENABLE;
/* setup receive checksum control */
if (ifp->if_capenable & IFCAP_RXCSUM)
CSR_WRITE_4(gx, GX_RX_CSUM_CONTROL,
GX_CSUM_TCP/* | GX_CSUM_IP*/);
/* setup transmit checksum control */
if (ifp->if_capenable & IFCAP_TXCSUM)
ifp->if_hwassist = GX_CSUM_FEATURES;
ctrl |= GX_RXC_STRIP_ETHERCRC; /* not on 82542? */
CSR_WRITE_4(gx, GX_RX_CONTROL, ctrl);
/* enable transmitter */
ctrl = GX_TXC_ENABLE | GX_TXC_PAD_SHORT_PKTS | GX_TXC_COLL_RETRY_16;
/* XXX we should support half-duplex here too... */
ctrl |= GX_TXC_COLL_TIME_FDX;
CSR_WRITE_4(gx, GX_TX_CONTROL, ctrl);
/*
* set up recommended IPG times, which vary depending on chip type:
* IPG transmit time: 80ns
* IPG receive time 1: 20ns
* IPG receive time 2: 80ns
*/
CSR_WRITE_4(gx, GX_TX_IPG, gx->gx_ipg);
/* set up 802.3x MAC flow control address -- 01:80:c2:00:00:01 */
CSR_WRITE_4(gx, GX_FLOW_CTRL_BASE, 0x00C28001);
CSR_WRITE_4(gx, GX_FLOW_CTRL_BASE+4, 0x00000100);
/* set up 802.3x MAC flow control type -- 88:08 */
CSR_WRITE_4(gx, GX_FLOW_CTRL_TYPE, 0x8808);
/* Set up tuneables */
CSR_WRITE_4(gx, gx->gx_reg.r_rx_delay, gx->gx_rx_intr_delay);
CSR_WRITE_4(gx, gx->gx_reg.r_tx_delay, gx->gx_tx_intr_delay);
/*
* Configure chip for correct operation.
*/
ctrl = GX_CTRL_DUPLEX;
#if BYTE_ORDER == BIG_ENDIAN
ctrl |= GX_CTRL_BIGENDIAN;
#endif
ctrl |= GX_CTRL_VLAN_ENABLE;
if (gx->gx_tbimode) {
/*
* It seems that TXCW must be initialized from the EEPROM
* manually.
*
* XXX
* should probably read the eeprom and re-insert the
* values here.
*/
#define TXCONFIG_WORD 0x000001A0
CSR_WRITE_4(gx, GX_TX_CONFIG, TXCONFIG_WORD);
/* turn on hardware autonegotiate */
GX_SETBIT(gx, GX_TX_CONFIG, GX_TXCFG_AUTONEG);
} else {
/*
* Auto-detect speed from PHY, instead of using direct
* indication. The SLU bit doesn't force the link, but
* must be present for ASDE to work.
*/
gx_phy_reset(gx);
ctrl |= GX_CTRL_SET_LINK_UP | GX_CTRL_AUTOSPEED;
}
/*
* Take chip out of reset and start it running.
*/
CSR_WRITE_4(gx, GX_CTRL, ctrl);
/* Turn interrupts on. */
CSR_WRITE_4(gx, GX_INT_MASK_SET, GX_INT_WANTED);
ifp->if_flags |= IFF_RUNNING;
ifp->if_flags &= ~IFF_OACTIVE;
/*
* Set the current media.
*/
if (gx->gx_miibus != NULL) {
mii_mediachg(device_get_softc(gx->gx_miibus));
} else {
ifm = &gx->gx_media;
tmp = ifm->ifm_media;
ifm->ifm_media = ifm->ifm_cur->ifm_media;
gx_ifmedia_upd(ifp);
ifm->ifm_media = tmp;
}
/*
* XXX
* Have the LINK0 flag force the link in TBI mode.
*/
if (gx->gx_tbimode && ifp->if_flags & IFF_LINK0) {
GX_CLRBIT(gx, GX_TX_CONFIG, GX_TXCFG_AUTONEG);
GX_SETBIT(gx, GX_CTRL, GX_CTRL_SET_LINK_UP);
}
#if 0
printf("66mhz: %s 64bit: %s\n",
CSR_READ_4(gx, GX_STATUS) & GX_STAT_PCI66 ? "yes" : "no",
CSR_READ_4(gx, GX_STATUS) & GX_STAT_BUS64 ? "yes" : "no");
#endif
GX_UNLOCK(gx);
splx(s);
}
/*
* Stop all chip I/O so that the kernel's probe routines don't
* get confused by errant DMAs when rebooting.
*/
static void
gx_shutdown(device_t dev)
{
struct gx_softc *gx;
gx = device_get_softc(dev);
gx_reset(gx);
gx_stop(gx);
}
static int
gx_detach(device_t dev)
{
struct gx_softc *gx;
struct ifnet *ifp;
int s;
s = splimp();
gx = device_get_softc(dev);
ifp = &gx->arpcom.ac_if;
GX_LOCK(gx);
ether_ifdetach(ifp);
gx_reset(gx);
gx_stop(gx);
ifmedia_removeall(&gx->gx_media);
gx_release(gx);
contigfree(gx->gx_rdata, sizeof(struct gx_ring_data), M_DEVBUF);
GX_UNLOCK(gx);
mtx_destroy(&gx->gx_mtx);
splx(s);
return (0);
}
static void
gx_eeprom_getword(struct gx_softc *gx, int addr, u_int16_t *dest)
{
u_int16_t word = 0;
u_int32_t base, reg;
int x;
addr = (GX_EE_OPC_READ << GX_EE_ADDR_SIZE) |
(addr & ((1 << GX_EE_ADDR_SIZE) - 1));
base = CSR_READ_4(gx, GX_EEPROM_CTRL);
base &= ~(GX_EE_DATA_OUT | GX_EE_DATA_IN | GX_EE_CLOCK);
base |= GX_EE_SELECT;
CSR_WRITE_4(gx, GX_EEPROM_CTRL, base);
for (x = 1 << ((GX_EE_OPC_SIZE + GX_EE_ADDR_SIZE) - 1); x; x >>= 1) {
reg = base | (addr & x ? GX_EE_DATA_IN : 0);
CSR_WRITE_4(gx, GX_EEPROM_CTRL, reg);
DELAY(10);
CSR_WRITE_4(gx, GX_EEPROM_CTRL, reg | GX_EE_CLOCK);
DELAY(10);
CSR_WRITE_4(gx, GX_EEPROM_CTRL, reg);
DELAY(10);
}
for (x = 1 << 15; x; x >>= 1) {
CSR_WRITE_4(gx, GX_EEPROM_CTRL, base | GX_EE_CLOCK);
DELAY(10);
reg = CSR_READ_4(gx, GX_EEPROM_CTRL);
if (reg & GX_EE_DATA_OUT)
word |= x;
CSR_WRITE_4(gx, GX_EEPROM_CTRL, base);
DELAY(10);
}
CSR_WRITE_4(gx, GX_EEPROM_CTRL, base & ~GX_EE_SELECT);
DELAY(10);
*dest = word;
}
static int
gx_read_eeprom(struct gx_softc *gx, caddr_t dest, int off, int cnt)
{
u_int16_t *word;
int i;
word = (u_int16_t *)dest;
for (i = 0; i < cnt; i ++) {
gx_eeprom_getword(gx, off + i, word);
word++;
}
return (0);
}
/*
* Set media options.
*/
static int
gx_ifmedia_upd(struct ifnet *ifp)
{
struct gx_softc *gx;
struct ifmedia *ifm;
struct mii_data *mii;
gx = ifp->if_softc;
if (gx->gx_tbimode) {
ifm = &gx->gx_media;
if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
return (EINVAL);
switch (IFM_SUBTYPE(ifm->ifm_media)) {
case IFM_AUTO:
GX_SETBIT(gx, GX_CTRL, GX_CTRL_LINK_RESET);
GX_SETBIT(gx, GX_TX_CONFIG, GX_TXCFG_AUTONEG);
GX_CLRBIT(gx, GX_CTRL, GX_CTRL_LINK_RESET);
break;
case IFM_1000_SX:
device_printf(gx->gx_dev,
"manual config not supported yet.\n");
#if 0
GX_CLRBIT(gx, GX_TX_CONFIG, GX_TXCFG_AUTONEG);
config = /* bit symbols for 802.3z */0;
ctrl |= GX_CTRL_SET_LINK_UP;
if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
ctrl |= GX_CTRL_DUPLEX;
#endif
break;
default:
return (EINVAL);
}
} else {
ifm = &gx->gx_media;
/*
* 1000TX half duplex does not work.
*/
if (IFM_TYPE(ifm->ifm_media) == IFM_ETHER &&
IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_T &&
(IFM_OPTIONS(ifm->ifm_media) & IFM_FDX) == 0)
return (EINVAL);
mii = device_get_softc(gx->gx_miibus);
mii_mediachg(mii);
}
return (0);
}
/*
* Report current media status.
*/
static void
gx_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
{
struct gx_softc *gx;
struct mii_data *mii;
u_int32_t status;
gx = ifp->if_softc;
if (gx->gx_tbimode) {
ifmr->ifm_status = IFM_AVALID;
ifmr->ifm_active = IFM_ETHER;
status = CSR_READ_4(gx, GX_STATUS);
if ((status & GX_STAT_LINKUP) == 0)
return;
ifmr->ifm_status |= IFM_ACTIVE;
ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
} else {
mii = device_get_softc(gx->gx_miibus);
mii_pollstat(mii);
if ((mii->mii_media_active & (IFM_1000_T | IFM_HDX)) ==
(IFM_1000_T | IFM_HDX))
mii->mii_media_active = IFM_ETHER | IFM_NONE;
ifmr->ifm_active = mii->mii_media_active;
ifmr->ifm_status = mii->mii_media_status;
}
}
static void
gx_mii_shiftin(struct gx_softc *gx, int data, int length)
{
u_int32_t reg, x;
/*
* Set up default GPIO direction + PHY data out.
*/
reg = CSR_READ_4(gx, GX_CTRL);
reg &= ~(GX_CTRL_GPIO_DIR_MASK | GX_CTRL_PHY_IO | GX_CTRL_PHY_CLK);
reg |= GX_CTRL_GPIO_DIR | GX_CTRL_PHY_IO_DIR;
/*
* Shift in data to PHY.
*/
for (x = 1 << (length - 1); x; x >>= 1) {
if (data & x)
reg |= GX_CTRL_PHY_IO;
else
reg &= ~GX_CTRL_PHY_IO;
CSR_WRITE_4(gx, GX_CTRL, reg);
DELAY(10);
CSR_WRITE_4(gx, GX_CTRL, reg | GX_CTRL_PHY_CLK);
DELAY(10);
CSR_WRITE_4(gx, GX_CTRL, reg);
DELAY(10);
}
}
static u_int16_t
gx_mii_shiftout(struct gx_softc *gx)
{
u_int32_t reg;
u_int16_t data;
int x;
/*
* Set up default GPIO direction + PHY data in.
*/
reg = CSR_READ_4(gx, GX_CTRL);
reg &= ~(GX_CTRL_GPIO_DIR_MASK | GX_CTRL_PHY_IO | GX_CTRL_PHY_CLK);
reg |= GX_CTRL_GPIO_DIR;
CSR_WRITE_4(gx, GX_CTRL, reg);
DELAY(10);
CSR_WRITE_4(gx, GX_CTRL, reg | GX_CTRL_PHY_CLK);
DELAY(10);
CSR_WRITE_4(gx, GX_CTRL, reg);
DELAY(10);
/*
* Shift out data from PHY.
*/
data = 0;
for (x = 1 << 15; x; x >>= 1) {
CSR_WRITE_4(gx, GX_CTRL, reg | GX_CTRL_PHY_CLK);
DELAY(10);
if (CSR_READ_4(gx, GX_CTRL) & GX_CTRL_PHY_IO)
data |= x;
CSR_WRITE_4(gx, GX_CTRL, reg);
DELAY(10);
}
CSR_WRITE_4(gx, GX_CTRL, reg | GX_CTRL_PHY_CLK);
DELAY(10);
CSR_WRITE_4(gx, GX_CTRL, reg);
DELAY(10);
return (data);
}
static int
gx_miibus_readreg(device_t dev, int phy, int reg)
{
struct gx_softc *gx;
gx = device_get_softc(dev);
if (gx->gx_tbimode)
return (0);
/*
* XXX
* Note: Cordova has a MDIC register. livingood and < have mii bits
*/
gx_mii_shiftin(gx, GX_PHY_PREAMBLE, GX_PHY_PREAMBLE_LEN);
gx_mii_shiftin(gx, (GX_PHY_SOF << 12) | (GX_PHY_OP_READ << 10) |
(phy << 5) | reg, GX_PHY_READ_LEN);
return (gx_mii_shiftout(gx));
}
static void
gx_miibus_writereg(device_t dev, int phy, int reg, int value)
{
struct gx_softc *gx;
gx = device_get_softc(dev);
if (gx->gx_tbimode)
return;
gx_mii_shiftin(gx, GX_PHY_PREAMBLE, GX_PHY_PREAMBLE_LEN);
gx_mii_shiftin(gx, (GX_PHY_SOF << 30) | (GX_PHY_OP_WRITE << 28) |
(phy << 23) | (reg << 18) | (GX_PHY_TURNAROUND << 16) |
(value & 0xffff), GX_PHY_WRITE_LEN);
}
static void
gx_miibus_statchg(device_t dev)
{
struct gx_softc *gx;
struct mii_data *mii;
int reg, s;
gx = device_get_softc(dev);
if (gx->gx_tbimode)
return;
/*
* Set flow control behavior to mirror what PHY negotiated.
*/
mii = device_get_softc(gx->gx_miibus);
s = splimp();
GX_LOCK(gx);
reg = CSR_READ_4(gx, GX_CTRL);
if (mii->mii_media_active & IFM_FLAG0)
reg |= GX_CTRL_RX_FLOWCTRL;
else
reg &= ~GX_CTRL_RX_FLOWCTRL;
if (mii->mii_media_active & IFM_FLAG1)
reg |= GX_CTRL_TX_FLOWCTRL;
else
reg &= ~GX_CTRL_TX_FLOWCTRL;
CSR_WRITE_4(gx, GX_CTRL, reg);
GX_UNLOCK(gx);
splx(s);
}
static int
gx_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
{
struct gx_softc *gx = ifp->if_softc;
struct ifreq *ifr = (struct ifreq *)data;
struct mii_data *mii;
int s, mask, error = 0;
s = splimp();
GX_LOCK(gx);
switch (command) {
case SIOCSIFMTU:
if (ifr->ifr_mtu > GX_MAX_MTU) {
error = EINVAL;
} else {
ifp->if_mtu = ifr->ifr_mtu;
gx_init(gx);
}
break;
case SIOCSIFFLAGS:
if ((ifp->if_flags & IFF_UP) == 0) {
gx_stop(gx);
} else if (ifp->if_flags & IFF_RUNNING &&
((ifp->if_flags & IFF_PROMISC) !=
(gx->gx_if_flags & IFF_PROMISC))) {
if (ifp->if_flags & IFF_PROMISC)
GX_SETBIT(gx, GX_RX_CONTROL, GX_RXC_UNI_PROMISC);
else
GX_CLRBIT(gx, GX_RX_CONTROL, GX_RXC_UNI_PROMISC);
} else {
gx_init(gx);
}
gx->gx_if_flags = ifp->if_flags;
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
if (ifp->if_flags & IFF_RUNNING)
gx_setmulti(gx);
break;
case SIOCSIFMEDIA:
case SIOCGIFMEDIA:
if (gx->gx_miibus != NULL) {
mii = device_get_softc(gx->gx_miibus);
error = ifmedia_ioctl(ifp, ifr,
&mii->mii_media, command);
} else {
error = ifmedia_ioctl(ifp, ifr, &gx->gx_media, command);
}
break;
case SIOCSIFCAP:
mask = ifr->ifr_reqcap ^ ifp->if_capenable;
if (mask & IFCAP_HWCSUM) {
if (IFCAP_HWCSUM & ifp->if_capenable)
ifp->if_capenable &= ~IFCAP_HWCSUM;
else
ifp->if_capenable |= IFCAP_HWCSUM;
if (ifp->if_flags & IFF_RUNNING)
gx_init(gx);
}
break;
default:
error = ether_ioctl(ifp, command, data);
break;
}
GX_UNLOCK(gx);
splx(s);
return (error);
}
static void
gx_phy_reset(struct gx_softc *gx)
{
int reg;
GX_SETBIT(gx, GX_CTRL, GX_CTRL_SET_LINK_UP);
/*
* PHY reset is active low.
*/
reg = CSR_READ_4(gx, GX_CTRL_EXT);
reg &= ~(GX_CTRLX_GPIO_DIR_MASK | GX_CTRLX_PHY_RESET);
reg |= GX_CTRLX_GPIO_DIR;
CSR_WRITE_4(gx, GX_CTRL_EXT, reg | GX_CTRLX_PHY_RESET);
DELAY(10);
CSR_WRITE_4(gx, GX_CTRL_EXT, reg);
DELAY(10);
CSR_WRITE_4(gx, GX_CTRL_EXT, reg | GX_CTRLX_PHY_RESET);
DELAY(10);
#if 0
/* post-livingood (cordova) only */
GX_SETBIT(gx, GX_CTRL, 0x80000000);
DELAY(1000);
GX_CLRBIT(gx, GX_CTRL, 0x80000000);
#endif
}
static void
gx_reset(struct gx_softc *gx)
{
/* Disable host interrupts. */
CSR_WRITE_4(gx, GX_INT_MASK_CLR, GX_INT_ALL);
/* reset chip (THWAP!) */
GX_SETBIT(gx, GX_CTRL, GX_CTRL_DEVICE_RESET);
DELAY(10);
}
static void
gx_stop(struct gx_softc *gx)
{
struct ifnet *ifp;
ifp = &gx->arpcom.ac_if;
/* reset and flush transmitter */
CSR_WRITE_4(gx, GX_TX_CONTROL, GX_TXC_RESET);
/* reset and flush receiver */
CSR_WRITE_4(gx, GX_RX_CONTROL, GX_RXC_RESET);
/* reset link */
if (gx->gx_tbimode)
GX_SETBIT(gx, GX_CTRL, GX_CTRL_LINK_RESET);
/* Free the RX lists. */
gx_free_rx_ring(gx);
/* Free TX buffers. */
gx_free_tx_ring(gx);
ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
}
static void
gx_watchdog(struct ifnet *ifp)
{
struct gx_softc *gx;
gx = ifp->if_softc;
device_printf(gx->gx_dev, "watchdog timeout -- resetting\n");
gx_reset(gx);
gx_init(gx);
ifp->if_oerrors++;
}
/*
* Intialize a receive ring descriptor.
*/
static int
gx_newbuf(struct gx_softc *gx, int idx, struct mbuf *m)
{
struct mbuf *m_new = NULL;
struct gx_rx_desc *r;
if (m == NULL) {
MGETHDR(m_new, M_DONTWAIT, MT_DATA);
if (m_new == NULL) {
device_printf(gx->gx_dev,
"mbuf allocation failed -- packet dropped\n");
return (ENOBUFS);
}
MCLGET(m_new, M_DONTWAIT);
if ((m_new->m_flags & M_EXT) == 0) {
device_printf(gx->gx_dev,
"cluster allocation failed -- packet dropped\n");
m_freem(m_new);
return (ENOBUFS);
}
m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
} else {
m->m_len = m->m_pkthdr.len = MCLBYTES;
m->m_data = m->m_ext.ext_buf;
m->m_next = NULL;
m_new = m;
}
/*
* XXX
* this will _NOT_ work for large MTU's; it will overwrite
* the end of the buffer. E.g.: take this out for jumbograms,
* but then that breaks alignment.
*/
if (gx->arpcom.ac_if.if_mtu <= ETHERMTU)
m_adj(m_new, ETHER_ALIGN);
gx->gx_cdata.gx_rx_chain[idx] = m_new;
r = &gx->gx_rdata->gx_rx_ring[idx];
r->rx_addr = vtophys(mtod(m_new, caddr_t));
r->rx_staterr = 0;
return (0);
}
/*
* The receive ring can have up to 64K descriptors, which at 2K per mbuf
* cluster, could add up to 128M of memory. Due to alignment constraints,
* the number of descriptors must be a multiple of 8. For now, we
* allocate 256 entries and hope that our CPU is fast enough to keep up
* with the NIC.
*/
static int
gx_init_rx_ring(struct gx_softc *gx)
{
int i, error;
for (i = 0; i < GX_RX_RING_CNT; i++) {
error = gx_newbuf(gx, i, NULL);
if (error)
return (error);
}
/* bring receiver out of reset state, leave disabled */
CSR_WRITE_4(gx, GX_RX_CONTROL, 0);
/* set up ring registers */
CSR_WRITE_8(gx, gx->gx_reg.r_rx_base,
(u_quad_t)vtophys(gx->gx_rdata->gx_rx_ring));
CSR_WRITE_4(gx, gx->gx_reg.r_rx_length,
GX_RX_RING_CNT * sizeof(struct gx_rx_desc));
CSR_WRITE_4(gx, gx->gx_reg.r_rx_head, 0);
CSR_WRITE_4(gx, gx->gx_reg.r_rx_tail, GX_RX_RING_CNT - 1);
gx->gx_rx_tail_idx = 0;
return (0);
}
static void
gx_free_rx_ring(struct gx_softc *gx)
{
struct mbuf **mp;
int i;
mp = gx->gx_cdata.gx_rx_chain;
for (i = 0; i < GX_RX_RING_CNT; i++, mp++) {
if (*mp != NULL) {
m_freem(*mp);
*mp = NULL;
}
}
bzero((void *)gx->gx_rdata->gx_rx_ring,
GX_RX_RING_CNT * sizeof(struct gx_rx_desc));
/* release any partially-received packet chain */
if (gx->gx_pkthdr != NULL) {
m_freem(gx->gx_pkthdr);
gx->gx_pkthdr = NULL;
}
}
static int
gx_init_tx_ring(struct gx_softc *gx)
{
/* bring transmitter out of reset state, leave disabled */
CSR_WRITE_4(gx, GX_TX_CONTROL, 0);
/* set up ring registers */
CSR_WRITE_8(gx, gx->gx_reg.r_tx_base,
(u_quad_t)vtophys(gx->gx_rdata->gx_tx_ring));
CSR_WRITE_4(gx, gx->gx_reg.r_tx_length,
GX_TX_RING_CNT * sizeof(struct gx_tx_desc));
CSR_WRITE_4(gx, gx->gx_reg.r_tx_head, 0);
CSR_WRITE_4(gx, gx->gx_reg.r_tx_tail, 0);
gx->gx_tx_head_idx = 0;
gx->gx_tx_tail_idx = 0;
gx->gx_txcnt = 0;
/* set up initial TX context */
gx->gx_txcontext = GX_TXCONTEXT_NONE;
return (0);
}
static void
gx_free_tx_ring(struct gx_softc *gx)
{
struct mbuf **mp;
int i;
mp = gx->gx_cdata.gx_tx_chain;
for (i = 0; i < GX_TX_RING_CNT; i++, mp++) {
if (*mp != NULL) {
m_freem(*mp);
*mp = NULL;
}
}
bzero((void *)&gx->gx_rdata->gx_tx_ring,
GX_TX_RING_CNT * sizeof(struct gx_tx_desc));
}
static void
gx_setmulti(struct gx_softc *gx)
{
int i;
/* wipe out the multicast table */
for (i = 1; i < 128; i++)
CSR_WRITE_4(gx, GX_MULTICAST_BASE + i * 4, 0);
}
static void
gx_rxeof(struct gx_softc *gx)
{
struct gx_rx_desc *rx;
struct ifnet *ifp;
int idx, staterr, len;
struct mbuf *m;
gx->gx_rx_interrupts++;
ifp = &gx->arpcom.ac_if;
idx = gx->gx_rx_tail_idx;
while (gx->gx_rdata->gx_rx_ring[idx].rx_staterr & GX_RXSTAT_COMPLETED) {
rx = &gx->gx_rdata->gx_rx_ring[idx];
m = gx->gx_cdata.gx_rx_chain[idx];
/*
* gx_newbuf overwrites status and length bits, so we
* make a copy of them here.
*/
len = rx->rx_len;
staterr = rx->rx_staterr;
if (staterr & GX_INPUT_ERROR)
goto ierror;
if (gx_newbuf(gx, idx, NULL) == ENOBUFS)
goto ierror;
GX_INC(idx, GX_RX_RING_CNT);
if (staterr & GX_RXSTAT_INEXACT_MATCH) {
/*
* multicast packet, must verify against
* multicast address.
*/
}
if ((staterr & GX_RXSTAT_END_OF_PACKET) == 0) {
if (gx->gx_pkthdr == NULL) {
m->m_len = len;
m->m_pkthdr.len = len;
gx->gx_pkthdr = m;
gx->gx_pktnextp = &m->m_next;
} else {
m->m_len = len;
m->m_flags &= ~M_PKTHDR;
gx->gx_pkthdr->m_pkthdr.len += len;
*(gx->gx_pktnextp) = m;
gx->gx_pktnextp = &m->m_next;
}
continue;
}
if (gx->gx_pkthdr == NULL) {
m->m_len = len;
m->m_pkthdr.len = len;
} else {
m->m_len = len;
m->m_flags &= ~M_PKTHDR;
gx->gx_pkthdr->m_pkthdr.len += len;
*(gx->gx_pktnextp) = m;
m = gx->gx_pkthdr;
gx->gx_pkthdr = NULL;
}
ifp->if_ipackets++;
m->m_pkthdr.rcvif = ifp;
#define IP_CSMASK (GX_RXSTAT_IGNORE_CSUM | GX_RXSTAT_HAS_IP_CSUM)
#define TCP_CSMASK \
(GX_RXSTAT_IGNORE_CSUM | GX_RXSTAT_HAS_TCP_CSUM | GX_RXERR_TCP_CSUM)
if (ifp->if_capenable & IFCAP_RXCSUM) {
#if 0
/*
* Intel Erratum #23 indicates that the Receive IP
* Checksum offload feature has been completely
* disabled.
*/
if ((staterr & IP_CSUM_MASK) == GX_RXSTAT_HAS_IP_CSUM) {
m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
if ((staterr & GX_RXERR_IP_CSUM) == 0)
m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
}
#endif
if ((staterr & TCP_CSMASK) == GX_RXSTAT_HAS_TCP_CSUM) {
m->m_pkthdr.csum_flags |=
CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
m->m_pkthdr.csum_data = 0xffff;
}
}
/*
* If we received a packet with a vlan tag,
* mark the packet before it's passed up.
*/
if (staterr & GX_RXSTAT_VLAN_PKT) {
VLAN_INPUT_TAG(ifp, m, rx->rx_special, continue);
}
(*ifp->if_input)(ifp, m);
continue;
ierror:
ifp->if_ierrors++;
gx_newbuf(gx, idx, m);
/*
* XXX
* this isn't quite right. Suppose we have a packet that
* spans 5 descriptors (9K split into 2K buffers). If
* the 3rd descriptor sets an error, we need to ignore
* the last two. The way things stand now, the last two
* will be accepted as a single packet.
*
* we don't worry about this -- the chip may not set an
* error in this case, and the checksum of the upper layers
* will catch the error.
*/
if (gx->gx_pkthdr != NULL) {
m_freem(gx->gx_pkthdr);
gx->gx_pkthdr = NULL;
}
GX_INC(idx, GX_RX_RING_CNT);
}
gx->gx_rx_tail_idx = idx;
if (--idx < 0)
idx = GX_RX_RING_CNT - 1;
CSR_WRITE_4(gx, gx->gx_reg.r_rx_tail, idx);
}
static void
gx_txeof(struct gx_softc *gx)
{
struct ifnet *ifp;
int idx, cnt;
gx->gx_tx_interrupts++;
ifp = &gx->arpcom.ac_if;
idx = gx->gx_tx_head_idx;
cnt = gx->gx_txcnt;
/*
* If the system chipset performs I/O write buffering, it is
* possible for the PIO read of the head descriptor to bypass the
* memory write of the descriptor, resulting in reading a descriptor
* which has not been updated yet.
*/
while (cnt) {
struct gx_tx_desc_old *tx;
tx = (struct gx_tx_desc_old *)&gx->gx_rdata->gx_tx_ring[idx];
cnt--;
if ((tx->tx_command & GX_TXOLD_END_OF_PKT) == 0) {
GX_INC(idx, GX_TX_RING_CNT);
continue;
}
if ((tx->tx_status & GX_TXSTAT_DONE) == 0)
break;
ifp->if_opackets++;
m_freem(gx->gx_cdata.gx_tx_chain[idx]);
gx->gx_cdata.gx_tx_chain[idx] = NULL;
gx->gx_txcnt = cnt;
ifp->if_timer = 0;
GX_INC(idx, GX_TX_RING_CNT);
gx->gx_tx_head_idx = idx;
}
if (gx->gx_txcnt == 0)
ifp->if_flags &= ~IFF_OACTIVE;
}
static void
gx_intr(void *xsc)
{
struct gx_softc *gx;
struct ifnet *ifp;
u_int32_t intr;
int s;
gx = xsc;
ifp = &gx->arpcom.ac_if;
s = splimp();
gx->gx_interrupts++;
/* Disable host interrupts. */
CSR_WRITE_4(gx, GX_INT_MASK_CLR, GX_INT_ALL);
/*
* find out why we're being bothered.
* reading this register automatically clears all bits.
*/
intr = CSR_READ_4(gx, GX_INT_READ);
/* Check RX return ring producer/consumer */
if (intr & (GX_INT_RCV_TIMER | GX_INT_RCV_THOLD | GX_INT_RCV_OVERRUN))
gx_rxeof(gx);
/* Check TX ring producer/consumer */
if (intr & (GX_INT_XMIT_DONE | GX_INT_XMIT_EMPTY))
gx_txeof(gx);
/*
* handle other interrupts here.
*/
/*
* Link change interrupts are not reliable; the interrupt may
* not be generated if the link is lost. However, the register
* read is reliable, so check that. Use SEQ errors to possibly
* indicate that the link has changed.
*/
if (intr & GX_INT_LINK_CHANGE) {
if ((CSR_READ_4(gx, GX_STATUS) & GX_STAT_LINKUP) == 0) {
device_printf(gx->gx_dev, "link down\n");
} else {
device_printf(gx->gx_dev, "link up\n");
}
}
/* Turn interrupts on. */
CSR_WRITE_4(gx, GX_INT_MASK_SET, GX_INT_WANTED);
if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
gx_start(ifp);
splx(s);
}
/*
* Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
* pointers to descriptors.
*/
static int
gx_encap(struct gx_softc *gx, struct mbuf *m_head)
{
struct gx_tx_desc_data *tx = NULL;
struct gx_tx_desc_ctx *tctx;
struct mbuf *m;
int idx, cnt, csumopts, txcontext;
struct m_tag *mtag;
cnt = gx->gx_txcnt;
idx = gx->gx_tx_tail_idx;
txcontext = gx->gx_txcontext;
/*
* Insure we have at least 4 descriptors pre-allocated.
*/
if (cnt >= GX_TX_RING_CNT - 4)
return (ENOBUFS);
/*
* Set up the appropriate offload context if necessary.
*/
csumopts = 0;
if (m_head->m_pkthdr.csum_flags) {
if (m_head->m_pkthdr.csum_flags & CSUM_IP)
csumopts |= GX_TXTCP_OPT_IP_CSUM;
if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
csumopts |= GX_TXTCP_OPT_TCP_CSUM;
txcontext = GX_TXCONTEXT_TCPIP;
} else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
csumopts |= GX_TXTCP_OPT_TCP_CSUM;
txcontext = GX_TXCONTEXT_UDPIP;
} else if (txcontext == GX_TXCONTEXT_NONE)
txcontext = GX_TXCONTEXT_TCPIP;
if (txcontext == gx->gx_txcontext)
goto context_done;
tctx = (struct gx_tx_desc_ctx *)&gx->gx_rdata->gx_tx_ring[idx];
tctx->tx_ip_csum_start = ETHER_HDR_LEN;
tctx->tx_ip_csum_end = ETHER_HDR_LEN + sizeof(struct ip) - 1;
tctx->tx_ip_csum_offset =
ETHER_HDR_LEN + offsetof(struct ip, ip_sum);
tctx->tx_tcp_csum_start = ETHER_HDR_LEN + sizeof(struct ip);
tctx->tx_tcp_csum_end = 0;
if (txcontext == GX_TXCONTEXT_TCPIP)
tctx->tx_tcp_csum_offset = ETHER_HDR_LEN +
sizeof(struct ip) + offsetof(struct tcphdr, th_sum);
else
tctx->tx_tcp_csum_offset = ETHER_HDR_LEN +
sizeof(struct ip) + offsetof(struct udphdr, uh_sum);
tctx->tx_command = GX_TXCTX_EXTENSION | GX_TXCTX_INT_DELAY;
tctx->tx_type = 0;
tctx->tx_status = 0;
GX_INC(idx, GX_TX_RING_CNT);
cnt++;
}
context_done:
/*
* Start packing the mbufs in this chain into the transmit
* descriptors. Stop when we run out of descriptors or hit
* the end of the mbuf chain.
*/
for (m = m_head; m != NULL; m = m->m_next) {
if (m->m_len == 0)
continue;
if (cnt == GX_TX_RING_CNT) {
printf("overflow(2): %d, %d\n", cnt, GX_TX_RING_CNT);
return (ENOBUFS);
}
tx = (struct gx_tx_desc_data *)&gx->gx_rdata->gx_tx_ring[idx];
tx->tx_addr = vtophys(mtod(m, vm_offset_t));
tx->tx_status = 0;
tx->tx_len = m->m_len;
if (gx->arpcom.ac_if.if_hwassist) {
tx->tx_type = 1;
tx->tx_command = GX_TXTCP_EXTENSION;
tx->tx_options = csumopts;
} else {
/*
* This is really a struct gx_tx_desc_old.
*/
tx->tx_command = 0;
}
GX_INC(idx, GX_TX_RING_CNT);
cnt++;
}
if (tx != NULL) {
tx->tx_command |= GX_TXTCP_REPORT_STATUS | GX_TXTCP_INT_DELAY |
GX_TXTCP_ETHER_CRC | GX_TXTCP_END_OF_PKT;
mtag = VLAN_OUTPUT_TAG(&gx->arpcom.ac_if, m);
if (mtag != NULL) {
tx->tx_command |= GX_TXTCP_VLAN_ENABLE;
tx->tx_vlan = VLAN_TAG_VALUE(mtag);
}
gx->gx_txcnt = cnt;
gx->gx_tx_tail_idx = idx;
gx->gx_txcontext = txcontext;
idx = GX_PREV(idx, GX_TX_RING_CNT);
gx->gx_cdata.gx_tx_chain[idx] = m_head;
CSR_WRITE_4(gx, gx->gx_reg.r_tx_tail, gx->gx_tx_tail_idx);
}
return (0);
}
/*
* Main transmit routine. To avoid having to do mbuf copies, we put pointers
* to the mbuf data regions directly in the transmit descriptors.
*/
static void
gx_start(struct ifnet *ifp)
{
struct gx_softc *gx;
struct mbuf *m_head;
int s;
s = splimp();
gx = ifp->if_softc;
for (;;) {
IF_DEQUEUE(&ifp->if_snd, m_head);
if (m_head == NULL)
break;
/*
* Pack the data into the transmit ring. If we
* don't have room, set the OACTIVE flag and wait
* for the NIC to drain the ring.
*/
if (gx_encap(gx, m_head) != 0) {
IF_PREPEND(&ifp->if_snd, m_head);
ifp->if_flags |= IFF_OACTIVE;
break;
}
/*
* If there's a BPF listener, bounce a copy of this frame
* to him.
*/
BPF_MTAP(ifp, m_head);
/*
* Set a timeout in case the chip goes out to lunch.
*/
ifp->if_timer = 5;
}
splx(s);
}
|