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
|
/* $OpenBSD: if_vic.c,v 1.37 2006/12/03 14:52:45 reyk Exp $ */
/*
* Copyright (c) 2006 Reyk Floeter <reyk@openbsd.org>
* Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Driver for the VMware Virtual NIC ("vmxnet")
*/
#include "bpfilter.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/malloc.h>
#include <sys/timeout.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <machine/intr.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_ether.h>
#endif
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#define VIC_PCI_BAR PCI_MAPREG_START /* Base Address Register */
#define VIC_MAGIC 0xbabe864f
/* Register address offsets */
#define VIC_DATA_ADDR 0x0000 /* Shared data address */
#define VIC_DATA_LENGTH 0x0004 /* Shared data length */
#define VIC_Tx_ADDR 0x0008 /* Tx pointer address */
/* Command register */
#define VIC_CMD 0x000c /* Command register */
#define VIC_CMD_INTR_ACK 0x0001 /* Acknowledge interrupt */
#define VIC_CMD_MCASTFIL 0x0002 /* Multicast address filter */
#define VIC_CMD_MCASTFIL_LENGTH 2
#define VIC_CMD_IFF 0x0004 /* Interface flags */
#define VIC_CMD_IFF_PROMISC 0x0001 /* Promiscous enabled */
#define VIC_CMD_IFF_BROADCAST 0x0002 /* Broadcast enabled */
#define VIC_CMD_IFF_MULTICAST 0x0004 /* Multicast enabled */
#define VIC_CMD_INTR_DISABLE 0x0020 /* Enable interrupts */
#define VIC_CMD_INTR_ENABLE 0x0040 /* Disable interrupts */
#define VIC_CMD_Tx_DONE 0x0100 /* Tx done register */
#define VIC_CMD_NUM_Rx_BUF 0x0200 /* Number of Rx buffers */
#define VIC_CMD_NUM_Tx_BUF 0x0400 /* Number of Tx buffers */
#define VIC_CMD_NUM_PINNED_BUF 0x0800 /* Number of pinned buffers */
#define VIC_CMD_HWCAP 0x1000 /* Capability register */
#define VIC_CMD_HWCAP_SG (1<<0) /* Scatter-gather transmits */
#define VIC_CMD_HWCAP_CSUM_IPv4 (1<<1) /* TCP/UDP cksum */
#define VIC_CMD_HWCAP_CSUM_ALL (1<<3) /* Hardware cksum */
#define VIC_CMD_HWCAP_CSUM \
(VIC_CMD_HWCAP_CSUM_IPv4 | VIC_CMD_HWCAP_CSUM_ALL)
#define VIC_CMD_HWCAP_DMA_HIGH (1<<4) /* High DMA mapping */
#define VIC_CMD_HWCAP_TOE (1<<5) /* TCP offload engine */
#define VIC_CMD_HWCAP_TSO (1<<6) /* TCP segmentation offload */
#define VIC_CMD_HWCAP_TSO_SW (1<<7) /* Software TCP segmentation */
#define VIC_CMD_HWCAP_VPROM (1<<8) /* Virtual PROM available */
#define VIC_CMD_HWCAP_VLAN_Tx (1<<9) /* Hardware VLAN MTU Rx */
#define VIC_CMD_HWCAP_VLAN_Rx (1<<10) /* Hardware VLAN MTU Tx */
#define VIC_CMD_HWCAP_VLAN_SW (1<<11) /* Software VLAN MTU */
#define VIC_CMD_HWCAP_VLAN \
(VIC_CMD_HWCAP_VLAN_Tx | VIC_CMD_HWCAP_VLAN_Rx | \
VIC_CMD_HWCAP_VLAN_SW)
#define VIC_CMD_HWCAP_BITS \
"\20\01SG\02CSUM4\03CSUM\04HDMA\05TOE\06TSO" \
"\07TSOSW\10VPROM\13VLANTx\14VLANRx\15VLANSW"
#define VIC_CMD_FEATURE 0x2000 /* Additional feature register */
#define VIC_CMD_FEATURE_0_Tx (1<<0)
#define VIC_CMD_FEATURE_TSO (1<<1)
#define VIC_LLADDR 0x0010 /* MAC address register */
#define VIC_VERSION_MINOR 0x0018 /* Minor version register */
#define VIC_VERSION_MAJOR 0x001c /* Major version register */
#define VIC_VERSION_MAJOR_M 0xffff0000
/* Status register */
#define VIC_STATUS 0x0020
#define VIC_STATUS_CONNECTED (1<<0)
#define VIC_STATUS_ENABLED (1<<1)
#define VIC_TOE_ADDR 0x0024 /* TCP offload address */
/* Virtual PROM address */
#define VIC_VPROM 0x0028
#define VIC_VPROM_LENGTH 6
/* Shared DMA data structures */
struct vic_sg {
u_int32_t sg_addr_low;
u_int16_t sg_addr_high;
u_int16_t sg_length;
} __packed;
#define VIC_SG_MAX 6
#define VIC_SG_ADDR_MACH 0
#define VIC_SG_ADDR_PHYS 1
#define VIC_SG_ADDR_VIRT 3
struct vic_sgarray {
u_int16_t sa_addr_type;
u_int16_t sa_length;
struct vic_sg sa_sg[VIC_SG_MAX];
} __packed;
struct vic_rxdesc {
u_int64_t rx_physaddr;
u_int32_t rx_buflength;
u_int32_t rx_length;
u_int16_t rx_owner;
u_int16_t rx_flags;
u_int32_t rx_priv;
} __packed;
#define VIC_RX_FLAGS_CSUMHW_OK 0x0001
struct vic_txdesc {
u_int16_t tx_flags;
u_int16_t tx_owner;
u_int32_t tx_priv;
u_int32_t tx_tsomss;
struct vic_sgarray tx_sa;
} __packed;
#define VIC_TX_FLAGS_KEEP 0x0001
#define VIC_TX_FLAGS_TXURN 0x0002
#define VIC_TX_FLAGS_CSUMHW 0x0004
#define VIC_TX_FLAGS_TSO 0x0008
#define VIC_TX_FLAGS_PINNED 0x0010
#define VIC_TX_FLAGS_QRETRY 0x1000
struct vic_stats {
u_int32_t vs_tx_count;
u_int32_t vs_tx_packets;
u_int32_t vs_tx_0copy;
u_int32_t vs_tx_copy;
u_int32_t vs_tx_maxpending;
u_int32_t vs_tx_stopped;
u_int32_t vs_tx_overrun;
u_int32_t vs_intr;
u_int32_t vs_rx_packets;
u_int32_t vs_rx_underrun;
} __packed;
struct vic_data {
u_int32_t vd_magic;
u_int32_t vd_rx_length;
u_int32_t vd_rx_nextidx;
u_int32_t vd_rx_length2;
u_int32_t vd_rx_nextidx2;
u_int32_t vd_irq;
u_int32_t vd_iff;
u_int32_t vd_mcastfil[VIC_CMD_MCASTFIL_LENGTH];
u_int32_t vd_reserved1[1];
u_int32_t vd_tx_length;
u_int32_t vd_tx_curidx;
u_int32_t vd_tx_nextidx;
u_int32_t vd_tx_stopped;
u_int32_t vd_tx_triggerlvl;
u_int32_t vd_tx_queued;
u_int32_t vd_tx_minlength;
u_int32_t vd_reserved2[6];
u_int32_t vd_rx_saved_nextidx;
u_int32_t vd_rx_saved_nextidx2;
u_int32_t vd_tx_saved_nextidx;
u_int32_t vd_length;
u_int32_t vd_rx_offset;
u_int32_t vd_rx_offset2;
u_int32_t vd_tx_offset;
u_int32_t vd_debug;
u_int32_t vd_tx_physaddr;
u_int32_t vd_tx_physaddr_length;
u_int32_t vd_tx_maxlength;
struct vic_stats vd_stats;
} __packed;
#define VIC_OWNER_DRIVER 0
#define VIC_OWNER_DRIVER_PEND 1
#define VIC_OWNER_NIC 2
#define VIC_OWNER_NIC_PEND 3
#define VIC_JUMBO_FRAMELEN 9018
#define VIC_JUMBO_MTU (VIC_JUMBO_FRAMELEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
#define VIC_NBUF 100
#define VIC_NBUF_MAX 128
#define VIC_MAX_SCATTER 1 /* 8? */
#define VIC_QUEUE_SIZE VIC_NBUF_MAX
#define VIC_QUEUE2_SIZE 1
#define VIC_INC(_x, _y) (_x) = ((_x) + 1) % (_y)
#define VIC_INC_POS(_x, _y) (_x) = (++(_x)) % (_y) ? (_x) : 1
#define VIC_TX_TIMEOUT 5
#define VIC_MIN_FRAMELEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
#define VIC_TXURN_WARN(_sc) ((_sc)->sc_txpending >= ((_sc)->sc_ntxbuf - 5))
#define VIC_TXURN(_sc) ((_sc)->sc_txpending >= (_sc)->sc_ntxbuf)
struct vic_rxbuf {
bus_dmamap_t rxb_dmamap;
struct mbuf *rxb_m;
};
struct vic_txbuf {
bus_dmamap_t txb_dmamap;
struct mbuf *txb_m;
};
struct vic_softc {
struct device sc_dev;
pci_chipset_tag_t sc_pc;
pcitag_t sc_tag;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
bus_size_t sc_ios;
bus_dma_tag_t sc_dmat;
void *sc_ih;
struct timeout sc_tick;
struct arpcom sc_ac;
struct ifmedia sc_media;
u_int32_t sc_nrxbuf;
u_int32_t sc_ntxbuf;
u_int32_t sc_cap;
u_int32_t sc_feature;
u_int8_t sc_lladdr[ETHER_ADDR_LEN];
bus_dmamap_t sc_dma_map;
bus_dma_segment_t sc_dma_seg;
size_t sc_dma_size;
caddr_t sc_dma_kva;
#define VIC_DMA_DVA(_sc) ((_sc)->sc_dma_map->dm_segs[0].ds_addr)
#define VIC_DMA_KVA(_sc) ((void *)(_sc)->sc_dma_kva)
struct vic_data *sc_data;
struct vic_rxbuf *sc_rxbuf;
struct vic_rxdesc *sc_rxq;
struct vic_rxdesc *sc_rxq2;
struct vic_txbuf *sc_txbuf;
struct vic_txdesc *sc_txq;
volatile u_int sc_txpending;
};
struct cfdriver vic_cd = {
0, "vic", DV_IFNET
};
int vic_match(struct device *, void *, void *);
void vic_attach(struct device *, struct device *, void *);
struct cfattach vic_ca = {
sizeof(struct vic_softc), vic_match, vic_attach
};
int vic_intr(void *);
void vic_shutdown(void *);
int vic_map_pci(struct vic_softc *, struct pci_attach_args *);
int vic_query(struct vic_softc *);
int vic_alloc_data(struct vic_softc *);
int vic_init_data(struct vic_softc *sc);
int vic_uninit_data(struct vic_softc *sc);
u_int32_t vic_read(struct vic_softc *, bus_size_t);
void vic_write(struct vic_softc *, bus_size_t, u_int32_t);
u_int32_t vic_read_cmd(struct vic_softc *, u_int32_t);
int vic_alloc_dmamem(struct vic_softc *);
void vic_free_dmamem(struct vic_softc *);
void vic_link_state(struct vic_softc *);
void vic_rx_proc(struct vic_softc *);
void vic_tx_proc(struct vic_softc *);
void vic_iff(struct vic_softc *);
void vic_getlladdr(struct vic_softc *);
void vic_setlladdr(struct vic_softc *);
int vic_media_change(struct ifnet *);
void vic_media_status(struct ifnet *, struct ifmediareq *);
void vic_start(struct ifnet *);
int vic_load_txb(struct vic_softc *, struct vic_txbuf *,
struct mbuf *);
void vic_watchdog(struct ifnet *);
int vic_ioctl(struct ifnet *, u_long, caddr_t);
void vic_init(struct ifnet *);
void vic_stop(struct ifnet *);
void vic_tick(void *);
#define DEVNAME(_s) ((_s)->sc_dev.dv_xname)
struct mbuf *vic_alloc_mbuf(struct vic_softc *, bus_dmamap_t);
const struct pci_matchid vic_devices[] = {
{ PCI_VENDOR_VMWARE, PCI_PRODUCT_VMWARE_NET }
};
extern int ifqmaxlen;
int
vic_match(struct device *parent, void *match, void *aux)
{
return (pci_matchbyid((struct pci_attach_args *)aux,
vic_devices, sizeof(vic_devices)/sizeof(vic_devices[0])));
}
void
vic_attach(struct device *parent, struct device *self, void *aux)
{
struct vic_softc *sc = (struct vic_softc *)self;
struct pci_attach_args *pa = aux;
struct ifnet *ifp;
if (vic_map_pci(sc, pa) != 0) {
/* error printed by vic_map_pci */
return;
}
if (vic_query(sc) != 0) {
/* error printed by vic_query */
return;
}
if (vic_alloc_data(sc) != 0) {
/* error printed by vic_alloc */
return;
}
timeout_set(&sc->sc_tick, vic_tick, sc);
bcopy(sc->sc_lladdr, sc->sc_ac.ac_enaddr, ETHER_ADDR_LEN);
ifp = &sc->sc_ac.ac_if;
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_ioctl = vic_ioctl;
ifp->if_start = vic_start;
ifp->if_watchdog = vic_watchdog;
ifp->if_hardmtu = VIC_JUMBO_MTU;
strlcpy(ifp->if_xname, DEVNAME(sc), IFNAMSIZ);
IFQ_SET_MAXLEN(&ifp->if_snd, sc->sc_ntxbuf - 1);
IFQ_SET_READY(&ifp->if_snd);
ifp->if_capabilities = IFCAP_VLAN_MTU;
#if 0
/* XXX interface capabilities */
if (sc->sc_cap & VIC_CMD_HWCAP_VLAN)
ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
if (sc->sc_cap & VIC_CMD_HWCAP_CSUM)
ifp->if_capabilities |= IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 |
IFCAP_CSUM_UDPv4;
#endif
ifmedia_init(&sc->sc_media, 0, vic_media_change, vic_media_status);
ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
if_attach(ifp);
ether_ifattach(ifp);
return;
}
int
vic_map_pci(struct vic_softc *sc, struct pci_attach_args *pa)
{
pcireg_t memtype;
pci_intr_handle_t ih;
const char *intrstr;
sc->sc_pc = pa->pa_pc;
sc->sc_tag = pa->pa_tag;
sc->sc_dmat = pa->pa_dmat;
memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, VIC_PCI_BAR);
if (pci_mapreg_map(pa, VIC_PCI_BAR, memtype, 0, &sc->sc_iot,
&sc->sc_ioh, NULL, &sc->sc_ios, 0) != 0) {
printf(": unable to map system interface register\n");
return (1);
}
if (pci_intr_map(pa, &ih) != 0) {
printf(": unable to map interrupt\n");
goto unmap;
}
intrstr = pci_intr_string(pa->pa_pc, ih);
sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
vic_intr, sc, DEVNAME(sc));
if (sc->sc_ih == NULL) {
printf(": unable to map interrupt%s%s\n",
intrstr == NULL ? "" : " at ",
intrstr == NULL ? "" : intrstr);
goto unmap;
}
printf(": %s\n", intrstr);
return (0);
unmap:
bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
sc->sc_ios = 0;
return (1);
}
int
vic_query(struct vic_softc *sc)
{
u_int32_t major, minor;
major = vic_read(sc, VIC_VERSION_MAJOR);
minor = vic_read(sc, VIC_VERSION_MINOR);
/* Check for a supported version */
if ((major & VIC_VERSION_MAJOR_M) !=
(VIC_MAGIC & VIC_VERSION_MAJOR_M)) {
printf("%s: magic mismatch\n", DEVNAME(sc));
return (1);
}
if (VIC_MAGIC > major || VIC_MAGIC < minor) {
printf("%s: unsupported version (%X)\n", DEVNAME(sc),
major & ~VIC_VERSION_MAJOR_M);
return (1);
}
sc->sc_nrxbuf = vic_read_cmd(sc, VIC_CMD_NUM_Rx_BUF);
sc->sc_ntxbuf = vic_read_cmd(sc, VIC_CMD_NUM_Tx_BUF);
sc->sc_feature = vic_read_cmd(sc, VIC_CMD_FEATURE);
sc->sc_cap = vic_read_cmd(sc, VIC_CMD_HWCAP);
vic_getlladdr(sc);
printf("%s: VMXnet %04X, address %s\n", DEVNAME(sc),
major & ~VIC_VERSION_MAJOR_M, ether_sprintf(sc->sc_lladdr));
#ifdef VIC_DEBUG
printf("%s: feature 0x%8x, cap 0x%8x, rx/txbuf %d/%d\n", DEVNAME(sc),
sc->sc_feature, sc->sc_cap, sc->sc_nrxbuf, sc->sc_ntxbuf);
#endif
if (sc->sc_nrxbuf > VIC_NBUF_MAX || sc->sc_nrxbuf == 0)
sc->sc_nrxbuf = VIC_NBUF;
if (sc->sc_ntxbuf > VIC_NBUF_MAX || sc->sc_ntxbuf == 0)
sc->sc_ntxbuf = VIC_NBUF;
return (0);
}
int
vic_alloc_data(struct vic_softc *sc)
{
u_int8_t *kva;
u_int offset;
struct vic_rxdesc *rxd;
int i;
sc->sc_rxbuf = malloc(sizeof(struct vic_rxbuf) * sc->sc_nrxbuf,
M_NOWAIT, M_DEVBUF);
if (sc->sc_rxbuf == NULL) {
printf("%s: unable to allocate rxbuf\n", DEVNAME(sc));
goto err;
}
sc->sc_txbuf = malloc(sizeof(struct vic_txbuf) * sc->sc_ntxbuf,
M_NOWAIT, M_DEVBUF);
if (sc->sc_txbuf == NULL) {
printf("%s: unable to allocate txbuf\n", DEVNAME(sc));
goto freerx;
}
sc->sc_dma_size = sizeof(struct vic_data) +
(sc->sc_nrxbuf + VIC_QUEUE2_SIZE) * sizeof(struct vic_rxdesc) +
sc->sc_ntxbuf * sizeof(struct vic_txdesc);
if (vic_alloc_dmamem(sc) != 0) {
printf("%s: unable to allocate dma region\n", DEVNAME(sc));
goto freetx;
}
kva = VIC_DMA_KVA(sc);
/* set up basic vic data */
sc->sc_data = VIC_DMA_KVA(sc);
sc->sc_data->vd_magic = VIC_MAGIC;
sc->sc_data->vd_length = sc->sc_dma_size;
offset = sizeof(struct vic_data);
/* set up the rx ring */
sc->sc_rxq = (struct vic_rxdesc *)&kva[offset];
sc->sc_data->vd_rx_offset = offset;
sc->sc_data->vd_rx_length = sc->sc_nrxbuf;
offset += sizeof(struct vic_rxdesc) * sc->sc_nrxbuf;
/* set up the dummy rx ring 2 with an unusable entry */
sc->sc_rxq2 = (struct vic_rxdesc *)&kva[offset];
sc->sc_data->vd_rx_offset2 = offset;
sc->sc_data->vd_rx_length2 = VIC_QUEUE2_SIZE;
for (i = 0; i < VIC_QUEUE2_SIZE; i++) {
rxd = &sc->sc_rxq2[i];
rxd->rx_physaddr = 0;
rxd->rx_buflength = 0;
rxd->rx_length = 0;
rxd->rx_owner = VIC_OWNER_DRIVER;
offset += sizeof(struct vic_rxdesc);
}
/* set up the tx ring */
sc->sc_txq = (struct vic_txdesc *)&kva[offset];
sc->sc_data->vd_tx_offset = offset;
sc->sc_data->vd_tx_length = sc->sc_ntxbuf;
return (0);
freetx:
free(sc->sc_txbuf, M_DEVBUF);
freerx:
free(sc->sc_rxbuf, M_DEVBUF);
err:
return (1);
}
int
vic_init_data(struct vic_softc *sc)
{
struct vic_rxbuf *rxb;
struct vic_rxdesc *rxd;
struct vic_txbuf *txb;
int i;
for (i = 0; i < sc->sc_nrxbuf; i++) {
rxb = &sc->sc_rxbuf[i];
rxd = &sc->sc_rxq[i];
if (bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
MCLBYTES, 0, BUS_DMA_NOWAIT, &rxb->rxb_dmamap) != 0) {
printf("%s: unable to create dmamap for rxb %d\n",
DEVNAME(sc), i);
goto freerxbs;
}
rxb->rxb_m = vic_alloc_mbuf(sc, rxb->rxb_dmamap);
if (rxb->rxb_m == NULL) {
/* error already printed */
bus_dmamap_destroy(sc->sc_dmat, rxb->rxb_dmamap);
goto freerxbs;
}
bus_dmamap_sync(sc->sc_dmat, rxb->rxb_dmamap, 0,
rxb->rxb_m->m_pkthdr.len, BUS_DMASYNC_PREREAD);
rxd->rx_physaddr = rxb->rxb_dmamap->dm_segs[0].ds_addr;
rxd->rx_buflength = rxb->rxb_m->m_pkthdr.len; /* XXX? */
rxd->rx_length = 0;
rxd->rx_owner = VIC_OWNER_NIC;
}
for (i = 0; i < sc->sc_ntxbuf; i++) {
txb = &sc->sc_txbuf[i];
if (bus_dmamap_create(sc->sc_dmat, MCLBYTES,
(sc->sc_cap & VIC_CMD_HWCAP_SG) ? VIC_SG_MAX : 1,
MCLBYTES, 0, BUS_DMA_NOWAIT, &txb->txb_dmamap) != 0) {
printf("%s: unable to create dmamap for tx %d\n",
DEVNAME(sc), i);
goto freetxbs;
}
txb->txb_m = NULL;
}
return (0);
freetxbs:
while (i--) {
txb = &sc->sc_txbuf[i];
bus_dmamap_destroy(sc->sc_dmat, txb->txb_dmamap);
}
i = sc->sc_nrxbuf;
freerxbs:
while (i--) {
rxb = &sc->sc_rxbuf[i];
bus_dmamap_sync(sc->sc_dmat, rxb->rxb_dmamap, 0,
rxb->rxb_m->m_pkthdr.len, BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(sc->sc_dmat, rxb->rxb_dmamap);
bus_dmamap_destroy(sc->sc_dmat, rxb->rxb_dmamap);
}
return (1);
}
int
vic_uninit_data(struct vic_softc *sc)
{
struct vic_rxbuf *rxb;
struct vic_rxdesc *rxd;
struct vic_txbuf *txb;
int i;
for (i = 0; i < sc->sc_nrxbuf; i++) {
rxb = &sc->sc_rxbuf[i];
rxd = &sc->sc_rxq[i];
bus_dmamap_sync(sc->sc_dmat, rxb->rxb_dmamap, 0,
rxb->rxb_m->m_pkthdr.len, BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(sc->sc_dmat, rxb->rxb_dmamap);
bus_dmamap_destroy(sc->sc_dmat, rxb->rxb_dmamap);
m_freem(rxb->rxb_m);
rxb->rxb_m = NULL;
}
for (i = 0; i < sc->sc_ntxbuf; i++) {
txb = &sc->sc_txbuf[i];
bus_dmamap_destroy(sc->sc_dmat, txb->txb_dmamap);
}
return (0);
}
void
vic_link_state(struct vic_softc *sc)
{
struct ifnet *ifp = &sc->sc_ac.ac_if;
u_int32_t status;
int link_state = LINK_STATE_DOWN;
status = vic_read(sc, VIC_STATUS);
if (status & VIC_STATUS_CONNECTED)
link_state = LINK_STATE_FULL_DUPLEX;
if (ifp->if_link_state != link_state) {
ifp->if_link_state = link_state;
if_link_state_change(ifp);
}
}
void
vic_shutdown(void *self)
{
struct vic_softc *sc = (struct vic_softc *)self;
vic_stop(&sc->sc_ac.ac_if);
}
int
vic_intr(void *arg)
{
struct vic_softc *sc = (struct vic_softc *)arg;
vic_rx_proc(sc);
vic_tx_proc(sc);
vic_write(sc, VIC_CMD, VIC_CMD_INTR_ACK);
return (1);
}
void
vic_rx_proc(struct vic_softc *sc)
{
struct ifnet *ifp = &sc->sc_ac.ac_if;
struct vic_rxdesc *rxd;
struct vic_rxbuf *rxb;
struct mbuf *m;
int len, idx;
if ((ifp->if_flags & IFF_RUNNING) == 0)
return;
bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_map, 0, sc->sc_dma_size,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
for (;;) {
idx = sc->sc_data->vd_rx_nextidx;
if (idx >= sc->sc_data->vd_rx_length) {
ifp->if_ierrors++;
if (ifp->if_flags & IFF_DEBUG)
printf("%s: receive index error\n",
sc->sc_dev.dv_xname);
break;
}
rxd = &sc->sc_rxq[idx];
if (rxd->rx_owner != VIC_OWNER_DRIVER)
break;
rxb = &sc->sc_rxbuf[idx];
len = rxd->rx_length;
if (len < VIC_MIN_FRAMELEN) {
ifp->if_iqdrops++;
goto nextp;
}
if (rxb->rxb_m == NULL) {
ifp->if_ierrors++;
printf("%s: rxb %d has no mbuf\n", DEVNAME(sc), idx);
break;
}
bus_dmamap_sync(sc->sc_dmat, rxb->rxb_dmamap, 0,
rxb->rxb_m->m_pkthdr.len, BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(sc->sc_dmat, rxb->rxb_dmamap);
m = rxb->rxb_m;
rxb->rxb_m = NULL;
m->m_pkthdr.rcvif = ifp;
m->m_pkthdr.len = m->m_len = len;
/* Get new mbuf for the Rx queue */
rxb->rxb_m = vic_alloc_mbuf(sc, rxb->rxb_dmamap);
if (rxb->rxb_m == NULL) {
ifp->if_ierrors++;
printf("%s: mbuf alloc failed\n", DEVNAME(sc));
break;
}
rxd->rx_physaddr = rxb->rxb_dmamap->dm_segs[0].ds_addr;
rxd->rx_buflength = rxb->rxb_m->m_pkthdr.len;
rxd->rx_length = 0;
ifp->if_ipackets++;
#if NBPFILTER > 0
if (ifp->if_bpf)
bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
#endif
ether_input_mbuf(ifp, m);
nextp:
rxd->rx_owner = VIC_OWNER_NIC;
VIC_INC(sc->sc_data->vd_rx_nextidx, sc->sc_data->vd_rx_length);
}
bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_map, 0, sc->sc_dma_size,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
}
void
vic_tx_proc(struct vic_softc *sc)
{
struct ifnet *ifp = &sc->sc_ac.ac_if;
struct vic_txdesc *txd;
struct vic_txbuf *txb;
int idx;
bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_map, 0, sc->sc_dma_size,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
while (sc->sc_txpending > 0) {
idx = sc->sc_data->vd_tx_curidx;
if (idx >= sc->sc_data->vd_tx_length) {
ifp->if_oerrors++;
break;
}
txd = &sc->sc_txq[idx];
if (txd->tx_owner != VIC_OWNER_DRIVER)
break;
txb = &sc->sc_txbuf[idx];
if (txb->txb_m == NULL) {
printf("%s: tx ring is corrupt\n", DEVNAME(sc));
ifp->if_oerrors++;
break;
}
bus_dmamap_sync(sc->sc_dmat, txb->txb_dmamap, 0,
txb->txb_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->sc_dmat, txb->txb_dmamap);
m_freem(txb->txb_m);
txb->txb_m = NULL;
ifp->if_flags &= ~IFF_OACTIVE;
sc->sc_txpending--;
sc->sc_data->vd_tx_stopped = 0;
VIC_INC(sc->sc_data->vd_tx_curidx, sc->sc_data->vd_tx_length);
}
bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_map, 0, sc->sc_dma_size,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
vic_start(ifp);
}
void
vic_iff(struct vic_softc *sc)
{
struct arpcom *ac = &sc->sc_ac;
struct ifnet *ifp = &sc->sc_ac.ac_if;
struct ether_multi *enm;
struct ether_multistep step;
u_int32_t crc;
u_int flags = 0;
bzero(&sc->sc_data->vd_mcastfil, sizeof(sc->sc_data->vd_mcastfil));
ifp->if_flags &= ~IFF_ALLMULTI;
if ((ifp->if_flags & IFF_RUNNING) == 0)
goto domulti;
if (ifp->if_flags & IFF_PROMISC)
goto allmulti;
ETHER_FIRST_MULTI(step, ac, enm);
while (enm != NULL) {
if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN))
goto allmulti;
crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
crc >>= 26;
sc->sc_data->vd_mcastfil[crc >> 4] |= htole16(1 << (crc & 0xf));
ETHER_NEXT_MULTI(step, enm);
}
goto domulti;
allmulti:
ifp->if_flags |= IFF_ALLMULTI;
memset(&sc->sc_data->vd_mcastfil, 0xff,
sizeof(sc->sc_data->vd_mcastfil));
domulti:
vic_write(sc, VIC_CMD, VIC_CMD_MCASTFIL);
if (ifp->if_flags & IFF_RUNNING) {
flags = (ifp->if_flags & IFF_PROMISC) ?
VIC_CMD_IFF_PROMISC :
(VIC_CMD_IFF_BROADCAST | VIC_CMD_IFF_MULTICAST);
}
sc->sc_data->vd_iff = flags;
vic_write(sc, VIC_CMD, VIC_CMD_IFF);
}
void
vic_getlladdr(struct vic_softc *sc)
{
u_int32_t reg;
/* Get MAC address */
reg = (sc->sc_cap & VIC_CMD_HWCAP_VPROM) ? VIC_VPROM : VIC_LLADDR;
bus_space_barrier(sc->sc_iot, sc->sc_ioh, reg, ETHER_ADDR_LEN,
BUS_SPACE_BARRIER_READ);
bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, reg, sc->sc_lladdr,
ETHER_ADDR_LEN);
/* Update the MAC address register */
if (reg == VIC_VPROM)
vic_setlladdr(sc);
}
void
vic_setlladdr(struct vic_softc *sc)
{
bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, VIC_LLADDR,
sc->sc_lladdr, ETHER_ADDR_LEN);
bus_space_barrier(sc->sc_iot, sc->sc_ioh, VIC_LLADDR, ETHER_ADDR_LEN,
BUS_SPACE_BARRIER_WRITE);
}
int
vic_media_change(struct ifnet *ifp)
{
/* Ignore */
return (0);
}
void
vic_media_status(struct ifnet *ifp, struct ifmediareq *imr)
{
struct vic_softc *sc = (struct vic_softc *)ifp->if_softc;
imr->ifm_active = IFM_ETHER | IFM_AUTO;
imr->ifm_status = IFM_AVALID;
vic_link_state(sc);
if (ifp->if_link_state == LINK_STATE_UP &&
ifp->if_flags & IFF_UP)
imr->ifm_status |= IFM_ACTIVE;
}
void
vic_start(struct ifnet *ifp)
{
struct vic_softc *sc;
struct mbuf *m;
struct vic_txbuf *txb;
struct vic_txdesc *txd;
struct vic_sg *sge;
bus_dmamap_t dmap;
int i, idx;
int tx = 0;
if (!(ifp->if_flags & IFF_RUNNING))
return;
if (ifp->if_flags & IFF_OACTIVE)
return;
if (IFQ_IS_EMPTY(&ifp->if_snd))
return;
sc = (struct vic_softc *)ifp->if_softc;
bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_map, 0, sc->sc_dma_size,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
for (;;) {
if (VIC_TXURN(sc)) {
ifp->if_flags |= IFF_OACTIVE;
break;
}
IFQ_POLL(&ifp->if_snd, m);
if (m == NULL)
break;
idx = sc->sc_data->vd_tx_nextidx;
if (idx >= sc->sc_data->vd_tx_length) {
printf("%s: tx idx is corrupt\n", DEVNAME(sc));
ifp->if_oerrors++;
break;
}
txd = &sc->sc_txq[idx];
txb = &sc->sc_txbuf[idx];
if (txb->txb_m != NULL) {
printf("%s: tx ring is corrupt\n", DEVNAME(sc));
sc->sc_data->vd_tx_stopped = 1;
ifp->if_oerrors++;
break;
}
/*
* we're committed to sending it now. if we cant map it into
* dma memory then we drop it.
*/
IFQ_DEQUEUE(&ifp->if_snd, m);
if (vic_load_txb(sc, txb, m) != 0) {
m_freem(m);
ifp->if_oerrors++;
/* continue? */
break;
}
#if NBPFILTER > 0
if (ifp->if_bpf)
bpf_mtap(ifp->if_bpf, txb->txb_m, BPF_DIRECTION_OUT);
#endif
dmap = txb->txb_dmamap;
txd->tx_flags = VIC_TX_FLAGS_KEEP;
txd->tx_owner = VIC_OWNER_NIC;
txd->tx_sa.sa_addr_type = VIC_SG_ADDR_PHYS;
txd->tx_sa.sa_length = dmap->dm_nsegs;
for (i = 0; i < dmap->dm_nsegs; i++) {
sge = &txd->tx_sa.sa_sg[i];
sge->sg_length = dmap->dm_segs[i].ds_len;
sge->sg_addr_low = dmap->dm_segs[i].ds_addr;
}
if (VIC_TXURN_WARN(sc)) {
txd->tx_flags |= VIC_TX_FLAGS_TXURN;
}
bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
BUS_DMASYNC_PREWRITE);
ifp->if_opackets++;
sc->sc_txpending++;
VIC_INC(sc->sc_data->vd_tx_nextidx, sc->sc_data->vd_tx_length);
tx = 1;
}
bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_map, 0, sc->sc_dma_size,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
if (tx)
vic_read(sc, VIC_Tx_ADDR);
}
int
vic_load_txb(struct vic_softc *sc, struct vic_txbuf *txb, struct mbuf *m)
{
bus_dmamap_t dmap = txb->txb_dmamap;
struct mbuf *m0 = NULL;
int error;
error = bus_dmamap_load_mbuf(sc->sc_dmat, dmap, m, BUS_DMA_NOWAIT);
switch (error) {
case 0:
txb->txb_m = m;
break;
case EFBIG: /* mbuf chain is too fragmented */
MGETHDR(m0, M_DONTWAIT, MT_DATA);
if (m0 == NULL)
return (ENOBUFS);
if (m->m_pkthdr.len > MHLEN) {
MCLGET(m0, M_DONTWAIT);
if (!(m0->m_flags & M_EXT)) {
m_freem(m0);
return (ENOBUFS);
}
}
m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
error = bus_dmamap_load_mbuf(sc->sc_dmat, dmap, m0,
BUS_DMA_NOWAIT);
if (error != 0) {
m_freem(m0);
printf("%s: tx dmamap load error %d\n", DEVNAME(sc),
error);
return (ENOBUFS);
}
m_freem(m);
txb->txb_m = m0;
break;
default:
printf("%s: tx dmamap load error %d\n", DEVNAME(sc), error);
return (ENOBUFS);
}
return (0);
}
void
vic_watchdog(struct ifnet *ifp)
{
#if 0
struct vic_softc *sc = (struct vic_softc *)ifp->if_softc;
if (sc->sc_txpending && sc->sc_txtimeout > 0) {
if (--sc->sc_txtimeout == 0) {
printf("%s: device timeout\n", sc->sc_dev.dv_xname);
ifp->if_flags &= ~IFF_RUNNING;
vic_init(ifp);
ifp->if_oerrors++;
return;
}
}
if (!IFQ_IS_EMPTY(&ifp->if_snd))
vic_start(ifp);
#endif
}
int
vic_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
struct vic_softc *sc = (struct vic_softc *)ifp->if_softc;
struct ifaddr *ifa;
struct ifreq *ifr;
int s, error = 0;
s = splnet();
if ((error = ether_ioctl(ifp, &sc->sc_ac, cmd, data)) > 0) {
splx(s);
return (error);
}
switch (cmd) {
case SIOCSIFADDR:
ifa = (struct ifaddr *)data;
ifp->if_flags |= IFF_UP;
#ifdef INET
if (ifa->ifa_addr->sa_family == AF_INET)
arp_ifinit(&sc->sc_ac, ifa);
#endif
/* FALLTHROUGH */
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_UP) {
if (ifp->if_flags & IFF_RUNNING)
vic_iff(sc);
else
vic_init(ifp);
} else {
if (ifp->if_flags & IFF_RUNNING)
vic_stop(ifp);
}
break;
case SIOCSIFMTU:
if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ifp->if_hardmtu)
error = EINVAL;
else if (ifp->if_mtu != ifr->ifr_mtu)
ifp->if_mtu = ifr->ifr_mtu;
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
ifr = (struct ifreq *)data;
error = (cmd == SIOCADDMULTI) ?
ether_addmulti(ifr, &sc->sc_ac) :
ether_delmulti(ifr, &sc->sc_ac);
if (error == ENETRESET) {
if (ifp->if_flags & IFF_RUNNING)
vic_iff(sc);
error = 0;
}
break;
case SIOCGIFMEDIA:
case SIOCSIFMEDIA:
error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
break;
default:
error = ENOTTY;
}
if (error == ENETRESET) {
if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
(IFF_UP | IFF_RUNNING))
vic_init(ifp);
error = 0;
}
splx(s);
return (error);
}
void
vic_init(struct ifnet *ifp)
{
struct vic_softc *sc = (struct vic_softc *)ifp->if_softc;
int s;
if (vic_init_data(sc) != 0)
return;
sc->sc_data->vd_tx_curidx = 0;
sc->sc_data->vd_tx_nextidx = 0;
sc->sc_data->vd_tx_stopped = sc->sc_data->vd_tx_queued = 0;
sc->sc_data->vd_rx_nextidx = 0;
sc->sc_data->vd_rx_nextidx2 = 0;
sc->sc_data->vd_rx_saved_nextidx = 0;
sc->sc_data->vd_rx_saved_nextidx2 = 0;
sc->sc_data->vd_tx_saved_nextidx = 0;
bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_map, 0, sc->sc_dma_size,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
s = splnet();
vic_write(sc, VIC_DATA_ADDR, VIC_DMA_DVA(sc));
vic_write(sc, VIC_DATA_LENGTH, sc->sc_dma_size);
ifp->if_flags |= IFF_RUNNING;
ifp->if_flags &= ~IFF_OACTIVE;
vic_iff(sc);
vic_write(sc, VIC_CMD, VIC_CMD_INTR_ENABLE);
splx(s);
timeout_add(&sc->sc_tick, hz);
}
void
vic_stop(struct ifnet *ifp)
{
struct vic_softc *sc = (struct vic_softc *)ifp->if_softc;
int s;
s = splnet();
timeout_del(&sc->sc_tick);
ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_map, 0, sc->sc_dma_size,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
/* XXX wait for tx to complete */
while (sc->sc_txpending > 0) {
splx(s);
delay(1000);
s = splnet();
}
sc->sc_data->vd_tx_stopped = 1;
vic_write(sc, VIC_CMD, VIC_CMD_INTR_DISABLE);
vic_iff(sc);
vic_write(sc, VIC_DATA_ADDR, 0);
vic_uninit_data(sc);
splx(s);
}
struct mbuf *
vic_alloc_mbuf(struct vic_softc *sc, bus_dmamap_t map)
{
struct mbuf *m = NULL;
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL)
return (NULL);
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
m_freem(m);
return (NULL);
}
m->m_len = m->m_pkthdr.len = MCLBYTES;
if (bus_dmamap_load_mbuf(sc->sc_dmat, map, m, BUS_DMA_NOWAIT) != 0) {
printf("%s: could not load mbuf DMA map", DEVNAME(sc));
m_freem(m);
return (NULL);
}
return (m);
}
void
vic_tick(void *arg)
{
struct vic_softc *sc = (struct vic_softc *)arg;
vic_link_state(sc);
timeout_add(&sc->sc_tick, hz);
}
u_int32_t
vic_read(struct vic_softc *sc, bus_size_t r)
{
bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
BUS_SPACE_BARRIER_READ);
return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, r));
}
void
vic_write(struct vic_softc *sc, bus_size_t r, u_int32_t v)
{
bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
BUS_SPACE_BARRIER_WRITE);
}
u_int32_t
vic_read_cmd(struct vic_softc *sc, u_int32_t cmd)
{
vic_write(sc, VIC_CMD, cmd);
return (vic_read(sc, VIC_CMD));
}
int
vic_alloc_dmamem(struct vic_softc *sc)
{
int nsegs;
if (bus_dmamap_create(sc->sc_dmat, sc->sc_dma_size, 1,
sc->sc_dma_size, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
&sc->sc_dma_map) != 0)
goto err;
if (bus_dmamem_alloc(sc->sc_dmat, sc->sc_dma_size, 16, 0,
&sc->sc_dma_seg, 1, &nsegs, BUS_DMA_NOWAIT) != 0)
goto destroy;
if (bus_dmamem_map(sc->sc_dmat, &sc->sc_dma_seg, nsegs,
sc->sc_dma_size, &sc->sc_dma_kva, BUS_DMA_NOWAIT) != 0)
goto free;
if (bus_dmamap_load(sc->sc_dmat, sc->sc_dma_map, sc->sc_dma_kva,
sc->sc_dma_size, NULL, BUS_DMA_NOWAIT) != 0)
goto unmap;
bzero(sc->sc_dma_kva, sc->sc_dma_size);
return (0);
unmap:
bus_dmamem_unmap(sc->sc_dmat, sc->sc_dma_kva, sc->sc_dma_size);
free:
bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_seg, 1);
destroy:
bus_dmamap_destroy(sc->sc_dmat, sc->sc_dma_map);
err:
return (1);
}
void
vic_free_dmamem(struct vic_softc *sc)
{
bus_dmamap_unload(sc->sc_dmat, sc->sc_dma_map);
bus_dmamem_unmap(sc->sc_dmat, sc->sc_dma_kva, sc->sc_dma_size);
bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_seg, 1);
bus_dmamap_destroy(sc->sc_dmat, sc->sc_dma_map);
}
|