-
Notifications
You must be signed in to change notification settings - Fork 7
/
RealtekR1000SL.cpp
3684 lines (3169 loc) · 89.1 KB
/
RealtekR1000SL.cpp
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
/*
* RealtekR1000SL.cpp - OS specific and hardware generic methods
* RealtekR1000SL
*
* Copyright 2009 Chuck Fry. All rights reserved.
*
* This software incorporates code from Realtek's open source Linux drivers
* and the open source Mac OS X project RealtekR1000 by Dmitri Arekhta,
* as modified by PSYSTAR Corporation.
*
* Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
* copyright PSYSTAR Corporation, 2008
* 2006 (c) Dmitri Arekhta ([email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "RealtekR1000SL.h"
#include "impl_defs.h"
#define BaseClass IOEthernetController
OSDefineMetaClassAndStructors(RealtekR1000, IOEthernetController)
// **********************************
//
// Static Data Member Initialization
//
// **********************************
//
// Configuration data
//
//
// *** N.B.: The order of these entries MUST match
// *** the enum mcfg_methods in R1000Regs.h!!
//
const struct RtlChipInfo RealtekR1000::rtl_chip_info[] =
{
//
// RTL810x Family
//
{"RTL8100E",
MCFG_8100E_1,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8100E",
MCFG_8100E_2,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8101E",
MCFG_8101E_1,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8101E",
MCFG_8101E_2,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8101E",
MCFG_8101E_3,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8102E",
MCFG_8102E_1,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8102E",
MCFG_8102E_2,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8103E",
MCFG_8103E_1,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8103E",
MCFG_8103E_2,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8103E",
MCFG_8103E_3,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8105E",
MCFG_8105E_1,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8105E",
MCFG_8105E_2,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8105E",
MCFG_8105E_3,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8105E",
MCFG_8105E_4,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
//
// RTL8168/8111 Family
//
{"RTL8168B/8111B",
MCFG_8168B_1,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_4k,
EFUSE_NOT_SUPPORT},
{"RTL8168B/8111B",
MCFG_8168B_2,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_4k,
EFUSE_NOT_SUPPORT},
{"RTL8168B/8111B",
MCFG_8168B_3,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_4k,
EFUSE_NOT_SUPPORT},
{"RTL8168C/8111C",
MCFG_8168C_1,
1024,
RxCfg_128_int_en | RxCfg_fet_multi_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_6k,
EFUSE_NOT_SUPPORT},
{"RTL8168C/8111C",
MCFG_8168C_2,
1024,
RxCfg_128_int_en | RxCfg_fet_multi_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_6k,
EFUSE_NOT_SUPPORT},
{"RTL8168C/8111C",
MCFG_8168C_3,
1024,
RxCfg_128_int_en | RxCfg_fet_multi_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_6k,
EFUSE_NOT_SUPPORT},
{"RTL8168CP/8111CP",
MCFG_8168CP_1,
1024,
RxCfg_128_int_en | RxCfg_fet_multi_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_6k,
EFUSE_NOT_SUPPORT},
{"RTL8168CP/8111CP",
MCFG_8168CP_2,
1024,
RxCfg_128_int_en | RxCfg_fet_multi_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_6k,
EFUSE_NOT_SUPPORT},
{"RTL8168D/8111D",
MCFG_8168D_1,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168D/8111D",
MCFG_8168D_2,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168DP/8111DP",
MCFG_8168DP_1,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168DP/8111DP",
MCFG_8168DP_2,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168DP/8111DP",
MCFG_8168DP_3,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168E/8111E",
MCFG_8168E_1,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168E/8111E",
MCFG_8168E_2,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168E-VL/8111E-VL",
MCFG_8168E_VL_1,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168E-VL/8111E-VL",
MCFG_8168E_VL_2,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168F/8111F",
MCFG_8168F_1,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_SUPPORT},
{"RTL8168F/8111F",
MCFG_8168F_2,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k},
{"RTL81xx/20",
CFG_METHOD_20,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k},
{"RTL81xx/21",
CFG_METHOD_21,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k},
{"RTL81xx/22",
CFG_METHOD_22,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k},
{"RTL81xx/23",
CFG_METHOD_23,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k},
{"RTL81xx/24",
CFG_METHOD_24,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k},
//
// RTL8169/8110 Family
//
{"RTL8169",
MCFG_8169_1,
256,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_7k,
EFUSE_NOT_SUPPORT},
{"RTL8169S/8110S",
MCFG_8169S_1,
256,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_7k,
EFUSE_NOT_SUPPORT},
{"RTL8169S/8110S",
MCFG_8169S_2,
256,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_7k,
EFUSE_NOT_SUPPORT},
{"RTL8169SB/8110SB",
MCFG_8169SB_1,
256,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_7k,
EFUSE_NOT_SUPPORT},
{"RTL8169SC/8110SC",
MCFG_8169SC_1,
256,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_7k,
EFUSE_NOT_SUPPORT},
{"RTL8169SC/8110SC",
MCFG_8169SC_2,
256,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_7k,
EFUSE_NOT_SUPPORT},
//
// RTL8401
//
{"RTL8401",
MCFG_8401_1,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
{"RTL8401",
MCFG_8402_1,
1024,
(RX_FIFO_THRESH_NONE << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
0,
EFUSE_NOT_SUPPORT},
//
// RTL8411
//
{"RTL8411",
MCFG_8411_1,
1024,
RxCfg_128_int_en | (RX_DMA_BURST << RxCfgDMAShift),
0xff7e1880,
Jumbo_Frame_9k,
EFUSE_NOT_SUPPORT},
// *** end of table ***
{ 0 }
};
//
// Power management table
//
struct IOPMPowerState RealtekR1000::powerStateArray[ kR1000PowerStateCount ] =
{
// kR1000PowerStateOff
{ kIOPMPowerStateVersion1,0,0,0,0,0,0,0,0,0,0,0 },
// kR1000PowerStateOn
{ kIOPMPowerStateVersion1, // version
IOPMDeviceUsable, // capability
IOPMPowerOn, // pwr character
IOPMPowerOn, // pwr requirement
// below this line unused, 0 is OK
0, // static pwr in mw
0, // unbudgeted pwr
0, // pwr to attain
0, // time to attain in us
0, // settle up time
0, // time to lower
0, // settle down time
0 // power domain budget
}
};
/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
int RealtekR1000::max_interrupt_work = 20;
/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
The RTL chips use a 64 element hash table based on the Ethernet CRC. */
UInt32 RealtekR1000::multicast_filter_limit = 32;
const u32 RealtekR1000::ethernet_polynomial = 0x04c11db7U;
// Default interrupt mask. See chip-specific code for more.
const u16 RealtekR1000::r1000_intr_mask =
LinkChg | RxDescUnavail | RxFIFOOver | TxErr | TxOK | RxErr | RxOK ;
/*
* Initialization of driver instance,
* i.e. resources allocation and so on.
*/
// FIXME - be sure to initialize all member variables!
bool RealtekR1000::init(OSDictionary *properties)
{
//
// logging
//
strncpy(bsdName, "RealtekR1000", BSD_NAME_LEN);
DLog("init\n");
if (BaseClass::init(properties) == false)
{
DLog("BaseClass::init() returned false!\n");
return false;
}
//
// pointers to OS objects
//
pciDev = NULL;
workLoop = NULL;
intSource = NULL;
timerSource = NULL;
netStats = NULL;
etherStats = NULL;
transmitQueue = NULL;
etherif = NULL;
mediumDict = NULL;
//
// capabilities to advertise to OS
//
canOffload = 0;
//
// internal driver status
//
board_inited = false;
buffers_inited = false;
enabled = false;
linked = false;
activationLevel = kActivationLevelNone;
enabledForKDP = enabledForBSD = false;
powerState = kR1000PowerStateOn; // presume power is already on at startup
//
// Tx & Rx ring buffer management
//
R1000InitRingIndices();
rxdesc_space = NULL;
RxDescArray = NULL;
rx_descMd = NULL;
rxdesc_phy_dma_addr = NULL;
txdesc_space = NULL;
TxDescArray = NULL;
tx_descMd = NULL;
txdesc_phy_dma_addr = NULL;
for (int i = 0; i < NUM_TX_DESC; i++)
{
Tx_skbuff[i] = NULL;
}
InitializeBufferMemoryPointers();
//
// register addressing
//
pioBase = 0;
mmioBase = NULL;
forcedPio = true;
return true;
}
/*
* Calling before destroying driver instance.
* Frees all allocated resources.
*/
void RealtekR1000::free()
{
DLog("free\n");
// give back our buffer pool
FreeBufferMemory();
FreeDescriptorsMemory();
//free resource of base instance
if (intSource && workLoop)
{
//Detaching interrupt source from work loop
workLoop->removeEventSource(intSource);
}
// TODO - figure out if this order is significant!
// TODO - release contents of various pointer tables
RELEASE(etherif);
RELEASE(intSource);
RELEASE(timerSource);
RELEASE(mmioBase);
RELEASE(pciDev);
RELEASE(workLoop);
RELEASE(mediumDict);
BaseClass::free();
}
/*
* Starting driver.
*/
bool RealtekR1000::start(IOService *provider)
{
DLog("start\n");
if (!BaseClass::start(provider))
{
DLog("start: Failed, super::start returned false\n");
return false;
}
pciDev = OSDynamicCast(IOPCIDevice, provider);
if (pciDev == NULL)
{
DLog("start: Failed, provider is not IOPCIDevice\n");
return false;
}
// Increment OS reference count
pciDev->retain();
if (!pciDev->open(this))
{
DLog("start: Failed to open PCI Device/Nub\n");
return false;
}
bool success = false;
do
{
// FIXME - Defer adding Gigabit media until we've ID'd the chip
// See Apple driver: RTL8139PHY.cpp
// Assuming RTL8168B/8111B by default
// Actual chipset will be ID'd in R1000InitBoard(),
// called from R1000ProbeAndStartBoard() below
mcfg = MCFG_8168B_1;
//Adding Mac OS X PHY's
mediumDict = OSDictionary::withCapacity(MEDIUM_INDEX_COUNT + 1);
OSAddNetworkMedium(kIOMediumEthernetAuto, 0, MEDIUM_INDEX_AUTO);
OSAddNetworkMedium(kIOMediumEthernet10BaseT | kIOMediumOptionHalfDuplex, 10 * MBit, MEDIUM_INDEX_10HD);
OSAddNetworkMedium(kIOMediumEthernet10BaseT | kIOMediumOptionFullDuplex, 10 * MBit, MEDIUM_INDEX_10FD);
OSAddNetworkMedium(kIOMediumEthernet100BaseTX | kIOMediumOptionHalfDuplex, 100 * MBit, MEDIUM_INDEX_100HD);
OSAddNetworkMedium(kIOMediumEthernet100BaseTX | kIOMediumOptionFullDuplex, 100 * MBit, MEDIUM_INDEX_100FD);
OSAddNetworkMedium(kIOMediumEthernet1000BaseTX | kIOMediumOptionHalfDuplex, 1000 * MBit, MEDIUM_INDEX_1000HD);
OSAddNetworkMedium(kIOMediumEthernet1000BaseTX | kIOMediumOptionFullDuplex, 1000 * MBit, MEDIUM_INDEX_1000FD);
if (!publishMediumDictionary(mediumDict))
{
DLog("start: Failed, publishMediumDictionary returned false\n");
break;
}
if (!R1000ProbeAndStartBoard())
{
DLog("start: Failed, R1000ProbeAndStartBoard returned false\n");
break;
}
if (!AllocateDescriptorsMemory())
{
DLog("start: Failed, AllocateDescriptorsMemory returned false\n");
break;
}
if (!AllocateBufferMemory())
{
DLog("start: Failed, AllocateBufferMemory returned false\n");
break;
}
InitializeRingBufferDescriptors();
if (!R1000InitEventSources(provider))
{
DLog("start: Failed, R1000InitEventSources returned false\n");
break;
}
// set up power management
PMinit();
pciDev->joinPMtree(this);
success = true;
}
while ( false );
// Close our provider, it will be re-opened on demand when
// our enable() is called by a client.
if (pciDev != NULL)
{
pciDev->close(this);
}
do
{
// break if we've had an error before this
if ( false == success )
{
break;
}
// Attaching dynamic link layer
// Callback to configureInterface() method happens here
if (false == attachInterface((IONetworkInterface**)ðerif, false))
{
DLog("start: Failed 'attachInterface' in attaching to data link layer\n");
break;
}
// Do this here instead of in attachInterface() to allow us to cleanly finish initializing
etherif->registerService();
success = true;
}
while ( false );
DLog("start: returning '%d'\n",success);
return success;
}
/*
* Stopping driver.
*/
// FIXME - should we return buffer memory here?
void RealtekR1000::stop(IOService *provider)
{
DLog("stop\n");
detachInterface(etherif);
R1000StopBoard();
PMstop();
BaseClass::stop(provider);
}
bool RealtekR1000::OSAddNetworkMedium(ulong type, UInt32 bps, ulong index)
{
IONetworkMedium *medium = IONetworkMedium::medium(type, bps, 0, index);
if (!medium)
{
IOLog("Couldn't allocate medium\n");
return false;
}
if (!IONetworkMedium::addMedium(mediumDict, medium))
{
IOLog("Couldn't add medium\n");
return false;
}
mediumTable[index] = medium;
return true;
}
bool RealtekR1000::increaseActivationLevel(UInt32 level)
{
bool ret = false;
switch (level)
{
case kActivationLevelKDP:
{
if (!pciDev) break;
pciDev->open(this);
// PHY medium selection.
const IONetworkMedium *medium = getSelectedMedium();
if (!medium) {
DLog("Selected medium is NULL, forcing to autonegotiation\n");
medium = mediumTable[MEDIUM_INDEX_AUTO];
} else {
DLog("Selected medium index %u\n",(unsigned int)medium->getIndex());
}
selectMedium(medium);
// FIXME - why is this here? Anything to do with link_timer in Linux?
//timerSource->setTimeoutMS(TX_TIMEOUT);
ret = true;
break;
}
case kActivationLevelBSD:
{
if (!R1000OpenAdapter()) break;
transmitQueue->setCapacity(kTransmitQueueCapacity);
transmitQueue->start();
ret = true;
break;
}
}
return ret;
}
bool RealtekR1000::decreaseActivationLevel(UInt32 level)
{
switch (level)
{
case kActivationLevelKDP:
// FIXME - why is this here? Anything to do with link_timer in Linux?
//timerSource->cancelTimeout();
if (pciDev) pciDev->close(this);
break;
case kActivationLevelBSD:
transmitQueue->stop();
transmitQueue->setCapacity(0);
transmitQueue->flush();
R1000CloseAdapter();
break;
}
return true;
}
bool RealtekR1000::setActivationLevel(UInt32 level)
{
DLog("setActivationLevel(%u)\n", (unsigned int)level);
if (activationLevel == level)
return true;
bool success = false;
for ( ; activationLevel > level; activationLevel--) {
if (!(success = decreaseActivationLevel(activationLevel)))
break;
}
for ( ; activationLevel < level; activationLevel++ ) {
if (!(success = increaseActivationLevel(activationLevel+1)))
break;
}
return success;
}
/*
* A request from an interface client to enable the controller.
*/
IOReturn RealtekR1000::enable(IONetworkInterface *netif)
{
DLog("enable\n");
if (enabledForBSD)
return kIOReturnSuccess;
enabledForBSD = setActivationLevel(kActivationLevelBSD);
if (enabledForBSD) {
enabled = true;
return kIOReturnSuccess;
} else
return kIOReturnIOError;
}
/*
* A request from an interface client to disable the controller.
*/
IOReturn RealtekR1000::disable(IONetworkInterface *netif)
{
DLog("disable\n");
enabledForBSD = false;
setActivationLevel(enabledForKDP ? kActivationLevelKDP : kActivationLevelNone);
enabled = false;
return kIOReturnSuccess;
}
bool RealtekR1000::setLinkStatus( UInt32 status,
const IONetworkMedium *activeMedium,
UInt64 speedSt,
OSData *data )
{
// DLog("setLinkStatus speed=%lld\n", speedSt);
return BaseClass::setLinkStatus( status, activeMedium, speedSt, data );
}/* end setLinkStatus */
/*
* Transmits an output packet.
* packet - an mbuf chain containing the output packet to be sent on the network.
* param - a parameter provided by the caller.
*/
// TODO - implement checksum offload
UInt32 RealtekR1000::outputPacket(mbuf_t m, void *param)
{
// DLog("outputPacket, length = %lu\n", mbuf_pkthdr_len(m));
#ifdef DEBUG
if (!buffers_inited)
{
DLog("outputPacket: buffers not initialized!!\n");
return kIOReturnOutputStall;
}
#endif
// Is packet larger than MTU?
if (mbuf_pkthdr_len(m) > tx_pkt_len)
{
DLog("Tx Packet size is too big, dropping\n");
freePacket(m);
return kIOReturnOutputDropped;
}
ulong buf_len = static_cast<ulong>(mbuf_pkthdr_len(m));
// Allocate an entry in the Tx buffer descriptor ring
ulong entry = OSIncrementAtomic(&cur_tx) % n_tx_desc;
// Is this entry available?
if ((OSSwapLittleToHostInt32(TxDescArray[entry].status) & DescOwn))
{
DLog("TX_RING_IS_FULL, stalling\n");
return kIOReturnOutputStall;
}
// copy user packet into Tx buffer, coalescing if needed
uchar *data_ptr = TxBufferVirtualAddress(entry);
ulong pkt_snd_len = 0;
mbuf_t cur_buf = m;
while ((cur_buf != NULL) && (pkt_snd_len <= buf_len))
{
// Sanity check chunk size
size_t cur_buf_len = mbuf_len(cur_buf);
if (cur_buf_len > (buf_len - pkt_snd_len))
{
DLog("Tx Packet mbuf chain malformed, dropping\n");
freePacket(m);
return kIOReturnOutputDropped;
}
if (mbuf_data(cur_buf))
{
bcopy(mbuf_data(cur_buf), data_ptr, cur_buf_len);
data_ptr += cur_buf_len;
pkt_snd_len += static_cast<ulong>(cur_buf_len);
}
cur_buf = mbuf_next(cur_buf);
}
// final sanity check
if (buf_len != pkt_snd_len)
{
DLog("Tx Packet mbuf chain missing data, dropping\n");
freePacket(m);
return kIOReturnOutputDropped;
}
// now can log the packet as in the queue
Tx_skbuff[entry] = m;
// mark the descriptor as ready to send
if (entry == (n_tx_desc - 1))
{
TxDescArray[entry].status = OSSwapHostToLittleInt32((DescOwn | RingEnd | FirstFrag | LastFrag) | pkt_snd_len);
}
else
{
TxDescArray[entry].status = OSSwapHostToLittleInt32((DescOwn | FirstFrag | LastFrag) | pkt_snd_len);
}
// tell the chip there's work to do
WriteMMIO8(TxPoll, 0x40); // Normal Priority Queue bit
return kIOReturnOutputSuccess;
}
void RealtekR1000::getPacketBufferConstraints(IOPacketBufferConstraints *constraints) const
{
//DLog("getPacketBufferConstraints\n");
constraints->alignStart = kIOPacketBufferAlign4;
constraints->alignLength = kIOPacketBufferAlign4;
}
IOOutputQueue *RealtekR1000::createOutputQueue()
{
//DLog("createOutputQueue\n");
// Slight optimization
// switch back if Tx/Rx conflicts cause problems
//return IOGatedOutputQueue::withTarget(this, getWorkLoop());
return IOBasicOutputQueue::withTarget(this);
}
/*
* Returns a string describing the vendor of the network controller. The caller is responsible for releasing the string object returned.
*/
const OSString *RealtekR1000::newVendorString() const
{
return OSString::withCString("Realtek");
}
/*
* Returns a string describing the model of the network controller. The caller is responsible for releasing the string object returned.
*/
const OSString *RealtekR1000::newModelString() const
{
return OSString::withCString(rtl_chip_info[mcfg].name);
}
/*
* A client request to change the medium selection.
* This method is called when a client issues a command for the controller to change its
* current medium selection. The implementation must call setSelectedMedium() after the change
* has occurred. This method call is synchronized by the workloop's gate.
*/
IOReturn RealtekR1000::selectMedium(const IONetworkMedium *medium)
{
if (!medium) {
medium = mediumTable[MEDIUM_INDEX_AUTO];
}
if (!medium)
{
// DLog("Selected medium is NULL\n");
return kIOReturnBadArgument;
}
DLog("selectMedium, index=%u\n", (unsigned int)medium->getIndex());
switch (medium->getIndex())
{
case MEDIUM_INDEX_AUTO:
R1000SetMedium(SPEED_100, DUPLEX_FULL, AUTONEG_ENABLE);
break;
case MEDIUM_INDEX_10HD:
R1000SetMedium(SPEED_10, DUPLEX_HALF, AUTONEG_DISABLE);
break;
case MEDIUM_INDEX_10FD:
R1000SetMedium(SPEED_10, DUPLEX_FULL, AUTONEG_DISABLE);
break;
case MEDIUM_INDEX_100HD:
R1000SetMedium(SPEED_100, DUPLEX_HALF, AUTONEG_DISABLE);
break;
case MEDIUM_INDEX_100FD:
R1000SetMedium(SPEED_100, DUPLEX_FULL, AUTONEG_DISABLE);
break;
case MEDIUM_INDEX_1000HD:
R1000SetMedium(SPEED_1000, DUPLEX_HALF, AUTONEG_DISABLE);
break;
case MEDIUM_INDEX_1000FD:
R1000SetMedium(SPEED_1000, DUPLEX_FULL, AUTONEG_DISABLE);
break;
}
this->setSelectedMedium(medium);
if (ReadMMIO8(PHYstatus) & LinkStatus)