-
Notifications
You must be signed in to change notification settings - Fork 1
/
RTL8139.cpp
1377 lines (1034 loc) · 37.5 KB
/
RTL8139.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
/*
* Copyright (c) 1998-2003, 2006 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* Copyright (c) 2001 Realtek Semiconductor Corp. All rights reserved.
*
* rtl8139.cpp
*
* HISTORY
*
* 09-Jul-01 Owen Wei at Realtek Semiconductor Corp. created for Realtek
* RTL8139 family NICs.
*
*/
#include "RTL8139.h"
#define super IOEthernetController
OSDefineMetaClassAndStructors( RTL8139, IOEthernetController ) ;
#pragma mark -
#pragma mark ¥¥¥ Event Logging ¥¥¥
#pragma mark -
#if USE_ELG
void RTL8139::AllocateEventLog( UInt32 size )
{
mach_timespec_t time;
IOByteCount length;
fpELGMemDesc = IOBufferMemoryDescriptor::withOptions( kIOMemoryPhysicallyContiguous,
kEvLogSize,
PAGE_SIZE );
if ( !fpELGMemDesc )
{
kprintf( "AllocateEventLog - RTL8139 evLog allocation failed " );
return;
}
fpELGMemDesc->prepare( kIODirectionNone );
fpELG = (elg*)fpELGMemDesc->getBytesNoCopy();
bzero( fpELG, kEvLogSize );
fpELG->physAddr = fpELGMemDesc->getPhysicalSegment64( 0, &length ); // offset: 0; length
fpELG->evLogBuf = (UInt8*)fpELG + sizeof( struct elg );
fpELG->evLogBufe = (UInt8*)fpELG + kEvLogSize - 0x20; // ??? overran buffer?
fpELG->evLogBufp = fpELG->evLogBuf;
// fpELG->evLogFlag = 0xFeedBeef; // continuous wraparound
fpELG->evLogFlag = 0x03330333; // > kEvLogSize - don't wrap - stop logging at buffer end
// fpELG->evLogFlag = 0x0099; // < #elements - count down and stop logging at 0
// fpELG->evLogFlag = 'step'; // stop at each ELG
IOGetTime( &time );
fpELG->startTimeSecs = time.tv_sec;
IOLog( "\033[32mRTL8139::AllocateEventLog - buffer=%8x phys=%16llx \033[0m \n",
(unsigned int)fpELG, fpELG->physAddr );
return;
}/* end AllocateEventLog */
void RTL8139::EvLog( UInt32 a, UInt32 b, UInt32 ascii, char* str )
{
register UInt32 *lp; /* Long pointer */
register elg *pe = fpELG; /* pointer to elg structure */
mach_timespec_t time;
UInt32 lefty;
// kprintf( "%08lx %08lx ", a, b ); kprintf( str ); kprintf( "\n" );
if ( pe->evLogFlag == 0 )
{
pe->lostEvents++; /* count this as a lost event */
return;
}
IOGetTime( &time );
if ( pe->evLogFlag <= kEvLogSize / 0x10 )
--pe->evLogFlag;
else if ( pe->evLogFlag == 0xDebeefed ) /// ??? do this in a separate routine
{
for ( lp = (UInt32*)pe->evLogBuf; lp < (UInt32*)pe->evLogBufe; lp++ )
*lp = 0xDebeefed;
pe->evLogBufp = pe->evLogBuf; // rewind
pe->evLogFlag = 0x03330333; // stop at end
}
/* handle buffer wrap around if any */
if ( pe->evLogBufp >= pe->evLogBufe )
{
pe->evLogBufp = pe->evLogBuf;
pe->wrapCount++;
if ( pe->evLogFlag != 0xFeedBeef ) // make 0xFeedBeef a symbolic ???
{
pe->evLogFlag = 0; /* stop tracing if wrap undesired */
// IOFlushProcessorCache( kernel_task, (IOVirtualAddress)fpELG, kEvLogSize );
return;
}
pe->startTimeSecs = time.tv_sec;
}
lp = (UInt32*)pe->evLogBufp;
pe->evLogBufp += 0x10;
/* compose interrupt level with 3 byte time stamp: */
// if ( fpRegs ) // don't read cell regs if clock disabled
// lefty = OSSwapInt32( fpRegs->RxCompletion ) << 24;
// else lefty = 0xFF000000;
lefty = time.tv_sec << 24; // put seconds on left for now.
*lp++ = lefty | (time.tv_nsec >> 10); // ~ 1 microsec resolution
*lp++ = a;
*lp++ = b;
*lp = ascii;
return;
}/* end EvLog */
UInt32 RTL8139::Alrt( UInt32 a, UInt32 b, UInt32 ascii, char* str )
{
char work [ 256 ];
char *bp = work;
UInt8 x;
int i;
EvLog( a, b, ascii, str );
EvLog( '****', '****', 'Alrt', "*** Alrt" );
*bp++ = '{'; // prepend p1 in hex:
for ( i = 7; i >= 0; --i )
{
x = a & 0x0F;
if ( x < 10 )
x += '0';
else x += 'A' - 10;
bp[ i ] = x;
a >>= 4;
}
bp += 8;
*bp++ = ' '; // prepend p2 in hex:
for ( i = 7; i >= 0; --i )
{
x = b & 0x0F;
if ( x < 10 )
x += '0';
else x += 'A' - 10;
bp[ i ] = x;
b >>= 4;
}
bp += 8;
*bp++ = '}';
*bp++ = ' ';
for ( i = sizeof( work ) - (int)(bp - work) - 1; i && (*bp++ = *str++); --i ) ;
bp[ -1 ] = '\n'; // insert new line character
*bp = 0; // add C string terminator
fpELG->alertCount++; // trigger anybody watching
fpELG->lastAlrt = ascii;
// fpELG->evLogFlag = 0; // stop logging but alertCount can continue increasing.
// if ( fpELG->evLogFlag == 0xFeedBeef )
// fpELG->evLogFlag = 333; // cruise to see what happens next.
// kprintf( work );
// panic( work );
// Debugger( work );
// IOLog( work );
return 0xDeadBeef;
}/* end Alrt */
#endif // USE_ELG
#pragma mark -
#pragma mark ¥¥¥ Override methods ¥¥¥
#pragma mark -
//---------------------------------------------------------------------------
bool RTL8139::init( OSDictionary *properties )
{
#if USE_ELG
AllocateEventLog( kEvLogSize );
ELG( this, fpELG, 'Rltk', "RTL8139::init - event logging set up." );
#endif /* USE_ELG */
if ( false == super::init( properties ) )
return false;
forceLinkChange = true;
phyStatusLast = 0;
fSpeed100 = false;
currentLevel = kActivationLevel0;
currentMediumIndex = MEDIUM_INDEX_NONE;
fLoopback = false;
fLoopbackMode = kSelectLoopbackPHY;
fTSD_ERTXTH = R_TSD_ERTXTH;
return true;
}/* end init */
//---------------------------------------------------------------------------
bool RTL8139::start( IOService *provider )
{
OSObject *builtinProperty;
bool success = false;
ELG( IOThreadSelf(), provider, 'Strt', "RTL8139::start - this, provider." );
DEBUG_LOG( "start() ===>\n" );
do
{
if ( false == super::start( provider ) ) // Start our superclass first
break;
// Save a reference to our provider.
pciNub = OSDynamicCast( IOPCIDevice, provider );
if ( 0 == pciNub )
break;
pciNub->retain(); // Retain provider, released in free().
if ( false == pciNub->open( this ) ) // Open our provider.
break;
fBuiltin = false;
builtinProperty = provider->getProperty( "built-in" );
if ( builtinProperty )
{
fBuiltin = true;
ELG( 0, 0, 'b-in', "RTL8139::start - found built-in property." );
}
if ( false == initEventSources( provider ) )
break;
// Allocate memory for descriptors. This function will leak memory
// if called more than once. So don't do it.
if ( false == allocateDescriptorMemory() )
break;
// Get the virtual address mapping of CSR registers located at
// Base Address Range 0 (0x10).
csrMap = pciNub->mapDeviceMemoryWithRegister( kIOPCIConfigBaseAddress1 );
if ( 0 == csrMap )
break;
csrBase = (volatile void*)csrMap->getVirtualAddress();
// Init PCI config space:
if ( false == initPCIConfigSpace( pciNub ) )
break;
// Reset chip to bring it to a known state.
if ( initAdapter( kResetChip ) == false )
{
IOLog( "%s: initAdapter() failed\n", getName() );
break;
}
registerEEPROM();
// Publish our media capabilities:
phyProbeMediaCapability();
if ( false == publishMediumDictionary( mediumDict ) )
break;
success = true;
} while ( false );
// Close our provider, it will be re-opened on demand when
// our enable() is called by a client.
if ( pciNub )
pciNub->close( this );
do
{
if ( false == success )
break;
success = false;
// Allocate and attach an IOEthernetInterface instance.
if ( false == attachInterface( (IONetworkInterface**)&netif, false) )
break;
// Optional: this driver supports kernel debugging.
attachDebuggerClient( &debugger );
// Trigger matching for clients of netif.
netif->registerService();
success = true;
}
while ( false );
DEBUG_LOG( "start() <===\n" );
return success;
}/* end start */
//---------------------------------------------------------------------------
void RTL8139::stop( IOService *provider )
{
ELG( 0, provider, 'stop', "RTL8139::stop" );
DEBUG_LOG( "stop() ===>\n" );
super::stop( provider );
DEBUG_LOG( "stop() <===\n" );
return;
}/* end stop */
//---------------------------------------------------------------------------
bool RTL8139::initEventSources( IOService *provider )
{
ELG( 0, 0, 'InES', "RTL8139::initEventSources - " );
DEBUG_LOG( "initEventSources() ===>\n" );
IOWorkLoop *wl = getWorkLoop();
if ( 0 == wl )
return false;
fTransmitQueue = getOutputQueue();
if ( 0 == fTransmitQueue )
return false;
fTransmitQueue->setCapacity( kTransmitQueueCapacity );
// Create an interrupt event source to handle hardware interrupts.
interruptSrc = IOInterruptEventSource::interruptEventSource(
this,
OSMemberFunctionCast( IOInterruptEventAction,
this,
&RTL8139::interruptOccurred ),
provider );
if ( !interruptSrc || (wl->addEventSource( interruptSrc ) != kIOReturnSuccess) )
return false;
// This is important. If the interrupt line is shared with other devices,
// then the interrupt vector will be enabled only if all corresponding
// interrupt event sources are enabled. To avoid masking interrupts for
// other devices that are sharing the interrupt line, the event source
// is enabled immediately. Hardware interrupt sources remain disabled.
interruptSrc->enable();
// Register a timer event source used as a watchdog timer:
timerSrc = IOTimerEventSource::timerEventSource(
this,
OSMemberFunctionCast( IOTimerEventSource::Action,
this,
&RTL8139::timeoutOccurred ) );
if ( !timerSrc || (wl->addEventSource( timerSrc ) != kIOReturnSuccess) )
return false;
// Create a dictionary to hold IONetworkMedium objects:
mediumDict = OSDictionary::withCapacity( 5 );
if ( 0 == mediumDict )
return false;
DEBUG_LOG( "initEventSources() <===\n" );
return true;
}/* end initEventSources */
//--------------------------------------------------------------------------
// Update PCI command register to enable the IO mapped PCI memory range,
// and bus-master interface.
bool RTL8139::initPCIConfigSpace( IOPCIDevice *provider )
{
UInt16 reg16;
ELG( 0, provider, 'iPCI', "RTL8139::initPCIConfigSpace" );
DEBUG_LOG( "pciConfigInit() ===>\n" );
reg16 = provider->configRead16( kIOPCIConfigCommand );
reg16 &= ~kIOPCICommandIOSpace;
reg16 |= ( kIOPCICommandBusMaster
| kIOPCICommandMemorySpace
| kIOPCICommandMemWrInvalidate );
provider->configWrite16( kIOPCIConfigCommand, reg16 );
provider->configWrite8( kIOPCIConfigCacheLineSize, 64 / sizeof( UInt32 ) );
provider->configWrite8( kIOPCIConfigLatencyTimer, 0xF8 );// max timer - low 3 bits ignored
DEBUG_LOG( "pciConfigInit() <===\n" );
return true;
}/* end initPCIConfigSpace */
//---------------------------------------------------------------------------
bool RTL8139::createWorkLoop()
{
DEBUG_LOG( "createWorkLoop() ===>\n" );
workLoop = IOWorkLoop::workLoop();
DEBUG_LOG( "createWorkLoop() <===\n" );
return (workLoop != 0);
}/* end createWorkLoop */
//---------------------------------------------------------------------------
IOWorkLoop* RTL8139::getWorkLoop( void ) const
{
// Override IOService::getWorkLoop() method to return the
// work loop we allocated in createWorkLoop().
DEBUG_LOG( "getWorkLoop() ===>\n" );
DEBUG_LOG( "getWorkLoop() <===\n" );
return workLoop;
}/* end getWorkLoop */
//---------------------------------------------------------------------------
bool RTL8139::configureInterface( IONetworkInterface *netif )
{
IONetworkData *data;
ELG( this, netif, 'cfgI', "RTL8139::configureInterface " );
DEBUG_LOG( "configureInterface() ===>\n" );
if ( false == super::configureInterface( netif ) )
return false;
// Get the generic network statistics structure:
data = netif->getParameter( kIONetworkStatsKey );
if ( !data || !(netStats = (IONetworkStats*)data->getBuffer()) )
return false;
// Get the Ethernet statistics structure:
data = netif->getParameter( kIOEthernetStatsKey );
if ( !data || !(etherStats = (IOEthernetStats*)data->getBuffer()) )
return false;
DEBUG_LOG( "configureInterface() <===\n" );
return true;
}/* end configureInterface */
//---------------------------------------------------------------------------
void RTL8139::free()
{
#define RELEASE(x) do { if(x) { (x)->release(); (x) = 0; } } while(0)
ELG( 0, 0, 'free', "RTL8139::free" );
DEBUG_LOG( "free() ===>\n" );
if ( interruptSrc && workLoop )
workLoop->removeEventSource( interruptSrc );
RELEASE( netif );
RELEASE( debugger );
RELEASE( interruptSrc );
RELEASE( timerSrc );
RELEASE( csrMap );
RELEASE( mediumDict );
RELEASE( pciNub );
RELEASE( workLoop );
if ( fpTxRxMD )
{
fpTxRxMD->complete();
fpTxRxMD->release();
fpTxRxMD = 0;
}
super::free();
DEBUG_LOG( "free() <===\n" );
return;
}/* end free */
//---------------------------------------------------------------------------
// Function: enableAdapter
//
// Enables the adapter & driver to the given level of support.
bool RTL8139::enableAdapter( UInt32 level )
{
UInt16 isr;
bool success = false;
ELG( 0, level, 'enbA', "RTL8139::enableAdapter - level" );
DEBUG_LOG( "enableAdapter() ===>\n" );
DEBUG_LOG( "enable level %ld\n", level);
switch ( level )
{
case kActivationLevel1:
// Open our provider (IOPCIDevice):
if ( (0 == pciNub) || (false == pciNub->open( this )) )
break;
// Perform a full initialization sequence:
if ( initAdapter( kFullInitialization ) != true )
break;
// Program the physical layer / transceiver:
if ( selectMedium( getSelectedMedium() ) != kIOReturnSuccess )
break;
// Start the periodic timer:
timerSrc->setTimeoutMS( kWatchdogTimerPeriod ); // ??? do this in Level 2???
// Unless we wait and ack PUN/LinkChg interrupts, the receiver
// will not work. This creates a problem when DB_HALT debug
// flag is set, since we will break into the debugger right
// away after this function returns. But we won't be able to
// attach since the receiver is deaf. I have no idea why this
// workaround (discovered through experimentation) is needed.
for ( int i = 0; i < 100; i++ )
{
isr = csrRead16( RTL_ISR );
if ( isr & R_ISR_PUN )
{
csrWrite16( RTL_ISR, R_ISR_PUN );
ELG( i, isr, 'enbA', "RTL8139::enableAdapter - cleared PUN interrupt" );
DEBUG_LOG( "cleared PUN interrupt %x in %d\n", isr, i );
break;
}
IOSleep( 10 );
}/* end FOR */
success = true;
break;
case kActivationLevel2:
workLoop->enableAllInterrupts();
success = true;
break;
}/* end SWITCH */
if ( false == success )
IOLog( "enable level %u failed\n", (unsigned int)level );
DEBUG_LOG( "enableAdapter() <===\n" );
return success;
}/* end enableAdapter */
//---------------------------------------------------------------------------
// Function: disableAdapter
// Disables the adapter & driver to the given level of support.
bool RTL8139::disableAdapter( UInt32 currentLevel )
{
bool success = false;
ELG( 0, currentLevel, 'disA', "RTL8139::disableAdapter" );
DEBUG_LOG( "disableAdapter() ===>\n" );
DEBUG_LOG( "disable currentLevel %ld\n", currentLevel );
switch ( currentLevel )
{
case kActivationLevel1:
timerSrc->cancelTimeout(); // Stop the timer event source.
initAdapter( kResetChip ); // Reset the hardware engine.
phySetMedium( MEDIUM_INDEX_NONE ); // Power down the PHY
if ( pciNub )
pciNub->close( this ); // Close our provider.
success = true;
break;
case kActivationLevel2:
disableHardwareInterrupts(); // KDP doesn't use interrupts.
workLoop->disableAllInterrupts();
// Stop the transmit queue. outputPacket() will not get called
// after this. KDP calls sendPacket() to send a packet in polled
// mode and that is unaffected by the state of the output queue.
fTransmitQueue->stop();
fTransmitQueue->flush();
setLinkStatus( kIONetworkLinkValid ); // Valid sans kIONetworkLinkActive
success = true;
break;
}/* end SWITCH */
if ( false == success )
IOLog( "disable currentLevel %u failed\n", (unsigned int)currentLevel );
DEBUG_LOG( "disableAdapter() <===\n" );
return success;
}/* end disableAdapter */
//---------------------------------------------------------------------------
// Function: setActivationLevel
//
// Sets the adapter's activation level.
//
// kActivationLevel0 : Adapter disabled.
// kActivationLevel1 : Adapter partially enabled to support KDP.
// kActivationLevel2 : Adapter completely enabled for KDP and BSD.
bool RTL8139::setActivationLevel( UInt32 level )
{
bool success = false;
UInt32 nextLevel;
ELG( 0, level, 'sLvl', "RTL8139::setActivationLevel" );
DEBUG_LOG( "setActivationLevel() ===>\n" );
DEBUG_LOG( "---> CURRENT LEVEL: %ld DESIRED LEVEL: %ld\n", currentLevel, level );
if ( currentLevel == level )
return true;
for ( ; currentLevel > level; currentLevel-- )
{
if ( (success = disableAdapter( currentLevel )) == false )
break;
}
for ( nextLevel = currentLevel + 1; currentLevel < level;
currentLevel++, nextLevel++ )
{
if ( (success = enableAdapter( nextLevel )) == false )
break;
}
DEBUG_LOG( "---> PRESENT LEVEL: %ld\n\n", currentLevel);
DEBUG_LOG( "setActivationLevel() <===\n" );
return success;
}/* end setActivationLevel */
//---------------------------------------------------------------------------
IOReturn RTL8139::enable( IONetworkInterface *netif )
{
ELG( 0, enabledByBSD, 'enbN', "RTL8139::enable - netif" );
DEBUG_LOG( "enable(netif) ===>\n" );
if ( true == enabledByBSD )
{
DEBUG_LOG( "enable() <===\n" );
return kIOReturnSuccess;
}
enabledByBSD = setActivationLevel( kActivationLevel2 );
DEBUG_LOG( "enable(netif) <===\n" );
return enabledByBSD ? kIOReturnSuccess : kIOReturnIOError;
}/* end enable netif */
//---------------------------------------------------------------------------
IOReturn RTL8139::disable( IONetworkInterface* /*netif*/ )
{
ELG( enabledByKDP, enabledByBSD, 'disN', "RTL8139::disable - netif" );
DEBUG_LOG( "disable(netif) ===>\n" );
enabledByBSD = false;
setActivationLevel( enabledByKDP ? kActivationLevel1 : kActivationLevel0 );
DEBUG_LOG( "disable(netif) <===\n" );
return kIOReturnSuccess;
}/* end disable netif */
//---------------------------------------------------------------------------
IOReturn RTL8139::enable( IOKernelDebugger* /* debugger */ )
{
ELG( enabledByKDP, enabledByBSD, 'enbD', "RTL8139::enable - debugger" );
if ( enabledByKDP || enabledByBSD )
{
enabledByKDP = true;
return kIOReturnSuccess;
}
enabledByKDP = setActivationLevel( kActivationLevel1 );
return enabledByKDP ? kIOReturnSuccess : kIOReturnIOError;
}/* end enable debugger */
//---------------------------------------------------------------------------
IOReturn RTL8139::disable( IOKernelDebugger* /* debugger */ )
{
ELG( 0, 0, 'disD', "RTL8139::disable - debugger" );
enabledByKDP = false;
if ( enabledByBSD == false )
setActivationLevel( kActivationLevel0 );
return kIOReturnSuccess;
}/* end disable debugger */
bool RTL8139::setLinkStatus( UInt32 status,
const IONetworkMedium *activeMedium,
UInt64 speed,
OSData *data )
{
ELG( speed / 1000000, status, ' SLS', "setLinkStatus" );
return super::setLinkStatus( status, activeMedium, speed, data );
}/* end setLinkStatus */
//---------------------------------------------------------------------------
void RTL8139::timeoutOccurred( IOTimerEventSource *timer )
{
UInt32 u32;
timerSrc->setTimeoutMS( kWatchdogTimerPeriod );
ELG( 0, 0, 'time', "RTL8139::timeoutOccurred" );
u32 = csrRead32( RTL_MPC ); // get the 24-bit Missed Packet Counter
if ( u32 )
{
etherStats->dot3StatsEntry.missedFrames += u32;
csrWrite32( RTL_MPC, 0 );
}
phyReportLinkStatus();
return;
}/* end timeoutOccurred */
//---------------------------------------------------------------------------
IOReturn RTL8139::setPromiscuousMode( bool enabled )
{
ELG( 0, enabled, 'setP', "RTL8139::setPromiscuousMode" );
DEBUG_LOG( "setPromiscuousMode() ===>\n" );
if ( enabled )
{
reg_rcr |= R_RCR_AAP; // allow all physical
csrWrite32( RTL_MAR0, 0xffffffff ); // Accept all multicast
csrWrite32( RTL_MAR4, 0xffffffff );
}
else
{
reg_rcr &= ~R_RCR_AAP;
csrWrite32( RTL_MAR0, reg_mar0 ); // Restore multicast hash filter.
csrWrite32( RTL_MAR4, reg_mar4 );
}
csrWrite32( RTL_RCR, reg_rcr );
DEBUG_LOG( "setPromiscuousMode RTL_RCR = 0x%lx\n", reg_rcr );
DEBUG_LOG( "setPromiscuousMode() <===\n" );
return kIOReturnSuccess;
}/* end setPromiscuousMode */
//---------------------------------------------------------------------------
IOReturn RTL8139::setMulticastMode( bool enabled )
{
ELG( 0, enabled, 'setM', "RTL8139::setMulticastMode" );
DEBUG_LOG( "setMulticastMode() ===>\n" );
// Always accept multicast packets. The R_RCR_AM flag is always set
// whenever the receiver is enabled. Nothing else is needed here.
DEBUG_LOG( "setMulticastMode RTL_RCR = 0x%lx\n", reg_rcr );
DEBUG_LOG( "setMulticastMode() <===\n" );
return kIOReturnSuccess;
}/* end setMulticastMode */
//---------------------------------------------------------------------------
static inline UInt32 rtl_ether_crc( int length, const unsigned char *data )
{
static unsigned const ethernet_polynomial = 0x04c11db7U;
unsigned char current_octet;
int crc = -1;
while ( --length >= 0 )
{
current_octet = *data++;
for ( int bit = 0; bit < 8; bit++, current_octet >>= 1 )
crc = (crc << 1) ^
((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
}
return crc;
}/* end rtl_ether_crc */
IOReturn RTL8139::setMulticastList( IOEthernetAddress *addrs, UInt32 count )
{
ELG( addrs, count, 'setL', "RTL8139::setMulticastList" );
DEBUG_LOG( "setMulticastList() ===>\n" );
for ( UInt32 i = 0; i < count; i++, addrs++ )
{
int bit = rtl_ether_crc( 6, (const UInt8*)addrs ) >> 26;
if ( bit < 32 )
reg_mar0 |= (1 << bit);
else reg_mar4 |= (1 << (bit - 32));
}
csrWrite32( RTL_MAR0, reg_mar0 );
csrWrite32( RTL_MAR4, reg_mar4 );
DEBUG_LOG( "setMulticastList() <===\n" );
return kIOReturnSuccess;
}/* end setMulticastList */
//---------------------------------------------------------------------------
void RTL8139::getPacketBufferConstraints(
IOPacketBufferConstraints *constraints ) const
{
// ELG( 0, kIOPacketBufferAlign1, 'gPBC', "RTL8139::getPacketBufferConstraints" );
DEBUG_LOG( "getPacketBufferConstraints() ===>\n" );
constraints->alignStart = kIOPacketBufferAlign1; // no restriction
constraints->alignLength = kIOPacketBufferAlign1; // no restriction
DEBUG_LOG( "getPacketBufferConstraints() <===\n" );
return;
}/* end getPacketBufferConstraints */
//---------------------------------------------------------------------------
IOReturn RTL8139::getHardwareAddress( IOEthernetAddress *address )
{
union
{
UInt8 bytes[4];
UInt32 int32;
} idr;
DEBUG_LOG( "getHardwareAddress() ===>\n" );
// Fetch the hardware address bootstrapped from EEPROM.
idr.int32 = OSSwapLittleToHostInt32( csrRead32( RTL_IDR0 ) );
address->bytes[0] = idr.bytes[0];
address->bytes[1] = idr.bytes[1];
address->bytes[2] = idr.bytes[2];
address->bytes[3] = idr.bytes[3];
idr.int32 = OSSwapLittleToHostInt32( csrRead32( RTL_IDR4 ) );
address->bytes[4] = idr.bytes[0];
address->bytes[5] = idr.bytes[1];
ELG( *(UInt16*)address->bytes, *(UInt32*)&address->bytes[2], 'gHWA', "RTL8139::getHardwareAddress" );
DEBUG_LOG( "getHardwareAddress() <===\n" );
return kIOReturnSuccess;
}/* end getHardwareAddress */
//---------------------------------------------------------------------------
IOOutputQueue* RTL8139::createOutputQueue()
{
ELG( 0, 0, 'crOQ', "RTL8139::createOutputQueue" );
DEBUG_LOG( "createOutputQueue() ===>\n" );
DEBUG_LOG( "createOutputQueue() <===\n" );
// An IOGatedOutputQueue will serialize all calls to the driver's
// outputPacket() function with its work loop. This essentially
// serializes all access to the driver and the hardware through
// the driver's work loop, which simplifies the driver but also
// carries a small performance cost (relatively for 10/100 Mb).
return IOGatedOutputQueue::withTarget( this, getWorkLoop() );
}/* end createOutputQueue */
//---------------------------------------------------------------------------
IOReturn RTL8139::selectMedium( const IONetworkMedium *medium )
{
bool success;
ELG( 0, medium, 'sMed', "RTL8139::selectMedium" );
if ( medium == 0 )
medium = phyGetMediumWithIndex( MEDIUM_INDEX_AUTO );
if ( medium == 0 )
return kIOReturnUnsupported;
success = phySetMedium( medium );
if ( success )
{
setCurrentMedium( medium );
forceLinkChange = true; // force link change
phyReportLinkStatus();
}
return success ? kIOReturnSuccess : kIOReturnIOError;
}/* end selectMedium */
//---------------------------------------------------------------------------
// Report human readable hardware information strings.
const OSString* RTL8139::newVendorString() const
{
DEBUG_LOG( "newVendorString() ===>\n" );
DEBUG_LOG( "newVendorString() <===\n" );
return OSString::withCString( "Realtek" );
}/* end newVendorString */
const OSString* RTL8139::newModelString() const
{
const char *model = "8139";
// FIXME: should do a better job of identifying the device type.
DEBUG_LOG( "newModelString() ===>\n" );
DEBUG_LOG( "newModelString() <===\n" );
return OSString::withCString(model);
}/* end newModelString */
//---------------------------------------------------------------------------
IOReturn RTL8139::registerWithPolicyMaker( IOService *policyMaker )
{
enum
{
kPowerStateOff = 0,
kPowerStateOn,
kPowerStateCount
};
static IOPMPowerState powerStateArray[ kPowerStateCount ] =
{
{ 1,0,0,0,0,0,0,0,0,0,0,0 },
{ 1, IOPMDeviceUsable, IOPMPowerOn, IOPMPowerOn, 0,0,0,0,0,0,0,0 }
};
IOReturn ret;
ELG( 0, 0, 'RwPM', "RTL8139::registerWithPolicyMaker" );
ret = policyMaker->registerPowerDriver( this, powerStateArray,
kPowerStateCount );
return ret;
}/* end registerWithPolicyMaker */
//---------------------------------------------------------------------------