This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
RealtekRTL8100.cpp
4423 lines (3707 loc) · 150 KB
/
RealtekRTL8100.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
/* RealtekRTL8100.c -- RTL8100 driver class implementation.
*
* Copyright (c) 2013 Laura Müller <laura-mueller@uni-duesseldorf.de>
* All rights reserved.
*
* 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.
*
* Driver for Realtek RTL8100x PCIe ethernet controllers.
*
* This driver is based on Realtek's r8101 Linux driver (1.030.02).
*/
#include "RealtekRTL8100.h"
#pragma mark --- function prototypes ---
static inline UInt32 adjustIPv6Header(mbuf_t m);
static inline u32 ether_crc(int length, unsigned char *data);
#pragma mark --- public methods ---
OSDefineMetaClassAndStructors(RTL8100, super)
/* IOService (or its superclass) methods. */
bool RTL8100::init(OSDictionary *properties)
{
bool result;
result = super::init(properties);
if (result) {
workLoop = NULL;
commandGate = NULL;
pciDevice = NULL;
mediumDict = NULL;
txQueue = NULL;
interruptSource = NULL;
timerSource = NULL;
netif = NULL;
netStats = NULL;
etherStats = NULL;
baseMap = NULL;
baseAddr = NULL;
rxMbufCursor = NULL;
txNext2FreeMbuf = NULL;
txMbufCursor = NULL;
statBufDesc = NULL;
statPhyAddr = NULL;
statData = NULL;
isEnabled = false;
promiscusMode = false;
multicastMode = false;
linkUp = false;
rxPoll = false;
polling = false;
mtu = ETH_DATA_LEN;
powerState = 0;
speed = SPEED_1000;
duplex = DUPLEX_FULL;
autoneg = AUTONEG_ENABLE;
flowCtl = kFlowControlOff;
eeeAdv = 0;
eeeCap = 0;
pciDeviceData.vendor = 0;
pciDeviceData.device = 0;
pciDeviceData.subsystem_vendor = 0;
pciDeviceData.subsystem_device = 0;
linuxData.pci_dev = &pciDeviceData;
intrMitigateValue = 0;
wolCapable = false;
wolActive = false;
enableTSO4 = false;
enableTSO6 = false;
enableCSO6 = false;
disableASPM = false;
enableEEE = false;
}
done:
return result;
}
void RTL8100::free()
{
UInt32 i;
DebugLog("free() ===>\n");
if (workLoop) {
if (interruptSource) {
workLoop->removeEventSource(interruptSource);
RELEASE(interruptSource);
}
if (timerSource) {
workLoop->removeEventSource(timerSource);
RELEASE(timerSource);
}
workLoop->release();
workLoop = NULL;
}
RELEASE(commandGate);
RELEASE(txQueue);
RELEASE(mediumDict);
for (i = MEDIUM_INDEX_AUTO; i < MEDIUM_INDEX_COUNT; i++)
mediumTable[i] = NULL;
RELEASE(baseMap);
baseAddr = NULL;
linuxData.mmio_addr = NULL;
RELEASE(pciDevice);
freeDMADescriptors();
DebugLog("free() <===\n");
super::free();
}
static const char *onName = "enabled";
static const char *offName = "disabled";
/*! @function start
@abstract Starts the network controller.
@discussion After the controller driver has successfully matched
to a provider, this method is called to start the network controller.
IONetworkController will allocate resources and gather controller
properties in its implementation. No I/O will be performed until
the subclass tries to attach a client object. A driver must override
this method, and call super::start() at the beginning of its own
implementation. Then check the return value to make sure that its
superclass was started successfully before proceeding. Tasks that
are usually performed by a driver's start method are: resource
allocation, hardware initialization, allocation of IOEventSources
and attaching them to a workloop, publishing a medium dictionary,
and finally, attaching an interface object when it is ready to
handle client requests.
@param provider The provider that the controller was matched
(and attached) to.
@result Returns true on success, false otherwise.
*/
bool RTL8100::start(IOService *provider)
{
bool result;
result = super::start(provider);
if (!result) {
IOLog("Ethernet [RealtekRTL8100]: IOEthernetController::start failed.\n");
goto done;
}
multicastMode = false;
promiscusMode = false;
multicastFilter = 0;
pciDevice = OSDynamicCast(IOPCIDevice, provider);
if (!pciDevice) {
IOLog("Ethernet [RealtekRTL8100]: No provider.\n");
goto done;
}
pciDevice->retain();
if (!pciDevice->open(this)) {
IOLog("Ethernet [RealtekRTL8100]: Failed to open provider.\n");
goto error1;
}
getParams();
if (!initPCIConfigSpace(pciDevice)) {
goto error2;
}
if (!initRTL8100()) {
goto error2;
}
if (!setupMediumDict()) {
IOLog("Ethernet [RealtekRTL8100]: Failed to setup medium dictionary.\n");
goto error2;
}
commandGate = getCommandGate();
if (!commandGate) {
IOLog("Ethernet [RealtekRTL8100]: getCommandGate() failed.\n");
goto error3;
}
commandGate->retain();
if (!initEventSources(provider)) {
IOLog("Ethernet [RealtekRTL8100]: initEventSources() failed.\n");
goto error3;
}
result = attachInterface(reinterpret_cast<IONetworkInterface**>(&netif));
if (!result) {
IOLog("Ethernet [RealtekRTL8100]: attachInterface() failed.\n");
goto error3;
}
pciDevice->close(this);
result = true;
done:
return result;
error3:
RELEASE(commandGate);
error2:
pciDevice->close(this);
error1:
pciDevice->release();
pciDevice = NULL;
goto done;
}
/*! @function stop
@abstract Stops the network controller.
@discussion The counterpart of start(). The controller has been
instructed to stop running. The stop() method should release
resources and undo actions performed by the start() method.
Subclasses must override this method and call super::stop()
at the end of its implementation.
@param provider The provider that the controller was matched
(and attached) to. */
void RTL8100::stop(IOService *provider)
{
UInt32 i;
if (netif) {
detachInterface(netif);
netif = NULL;
}
if (workLoop) {
if (interruptSource) {
workLoop->removeEventSource(interruptSource);
RELEASE(interruptSource);
}
if (timerSource) {
workLoop->removeEventSource(timerSource);
RELEASE(timerSource);
}
workLoop->release();
workLoop = NULL;
}
RELEASE(commandGate);
RELEASE(txQueue);
RELEASE(mediumDict);
for (i = MEDIUM_INDEX_AUTO; i < MEDIUM_INDEX_COUNT; i++)
mediumTable[i] = NULL;
freeDMADescriptors();
RELEASE(baseMap);
baseAddr = NULL;
linuxData.mmio_addr = NULL;
RELEASE(pciDevice);
super::stop(provider);
}
/* Power Management Support */
static IOPMPowerState powerStateArray[kPowerStateCount] =
{
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, kIOPMDeviceUsable, kIOPMPowerOn, kIOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0}
};
/*! @function registerWithPolicyMaker
@abstract Implemented by controller drivers to register with
the power management policy-maker.
@discussion Drivers that are able to power manage their hardware
should override this method and register with the policy-maker
provided by calling IOService::registerPowerDriver().
IONetworkController will call this method before the initial
attempt is made to attach a client.
@param policyMaker The policy-maker chosen to manage power for
this network controller.
@result Returns kIOReturnSuccess on success, kIOReturnUnsupported if the
driver does not support power management, or an appropriate error
return code. The default return is kIOReturnUnsupported. */
IOReturn RTL8100::registerWithPolicyMaker(IOService *policyMaker)
{
DebugLog("registerWithPolicyMaker() ===>\n");
powerState = kPowerStateOn;
DebugLog("registerWithPolicyMaker() <===\n");
return policyMaker->registerPowerDriver(this, powerStateArray, kPowerStateCount);
}
/*! @function setPowerState
@abstract Requests a power managed driver to change the power state of its device.
@discussion A power managed driver must override <code>setPowerState</code> to take part in system power management. After a driver is registered with power management, the system uses <code>setPowerState</code> to power the device off and on for system sleep and wake.
Calls to @link PMinit PMinit@/link and @link registerPowerDriver registerPowerDriver@/link enable power management to change a device's power state using <code>setPowerState</code>. <code>setPowerState</code> is called in a clean and separate thread context.
@param powerStateOrdinal The number in the power state array of the state the driver is being instructed to switch to.
@param whatDevice A pointer to the power management object which registered to manage power for this device. In most cases, <code>whatDevice</code> will be equal to your driver's own <code>this</code> pointer.
@result The driver must return <code>IOPMAckImplied</code> if it has complied with the request when it returns. Otherwise if it has started the process of changing power state but not finished it, the driver should return a number of microseconds which is an upper limit of the time it will need to finish. Then, when it has completed the power switch, it should call @link acknowledgeSetPowerState acknowledgeSetPowerState@/link. */
IOReturn RTL8100::setPowerState(unsigned long powerStateOrdinal, IOService *policyMaker)
{
IOReturn result = IOPMAckImplied;
DebugLog("setPowerState() ===>\n");
if (powerStateOrdinal == powerState) {
DebugLog("Ethernet [RealtekRTL8100]: Already in power state %lu.\n", powerStateOrdinal);
goto done;
}
DebugLog("Ethernet [RealtekRTL8100]: switching to power state %lu.\n", powerStateOrdinal);
if (powerStateOrdinal == kPowerStateOff)
commandGate->runAction(setPowerStateSleepAction);
else
commandGate->runAction(setPowerStateWakeAction);
powerState = powerStateOrdinal;
done:
DebugLog("setPowerState() <===\n");
return result;
}
/*! @function systemWillShutdown
@abstract Handles system shutdown and restart notifications.
@discussion Overrides <code>IOService::systemWillShutdown</code> in order
to notify network clients that the power-managed controller should be disabled.
As a result, drivers can expect their <code>disable</code> method to be called
before system shutdown or restart. This implementation is synchronous and can
block before calling <code>IOService::systemWillShutdown</code> and return.
@param specifier
<code>kIOMessageSystemWillPowerOff</code> or <code>kIOMessageSystemWillRestart</code>.
@see //apple_ref/cpp/instm/IOService/systemWillShutdown/void/(IOOptionBits) IOService::systemWillShutdown */
void RTL8100::systemWillShutdown(IOOptionBits specifier)
{
DebugLog("systemWillShutdown() ===>\n");
if ((kIOMessageSystemWillPowerOff | kIOMessageSystemWillRestart) & specifier)
disable(netif);
/* Restore the original MAC address. */
rtl8101_rar_set(&linuxData, (UInt8 *)&origMacAddr.bytes);
DebugLog("systemWillShutdown() <===\n");
/* Must call super shutdown or system will stall. */
super::systemWillShutdown(specifier);
}
/* IONetworkController methods. */
/*! @function enable
@abstract A request from an interface client to enable the controller.
@discussion This method is called by an interface client to enable the controller.
Upon receiving this command, the controller driver must bring up the
hardware and become ready to transmit and receive packets. A driver
should also delay the allocation of most runtime resources until this
method is called in order to conserve system resources. This method call
is synchronized by the workloop's gate.
@param interface The interface client object that requested the enable.
@result Returns kIOReturnUnsupported. Drivers that override this method must
return kIOReturnSuccess on success, or an error code otherwise.
*/
IOReturn RTL8100::enable(IONetworkInterface *netif)
{
const IONetworkMedium *selectedMedium;
IOReturn result = kIOReturnError;
DebugLog("enable() ===>\n");
if (isEnabled) {
DebugLog("Ethernet [RealtekRTL8100]: Interface already enabled.\n");
result = kIOReturnSuccess;
goto done;
}
if (!pciDevice || pciDevice->isOpen()) {
IOLog("Ethernet [RealtekRTL8100]: Unable to open PCI device.\n");
goto done;
}
pciDevice->open(this);
if (!setupDMADescriptors()) {
IOLog("Ethernet [RealtekRTL8100]: Error allocating DMA descriptors.\n");
goto done;
}
selectedMedium = getSelectedMedium();
if (!selectedMedium) {
DebugLog("Ethernet [RealtekRTL8100]: No medium selected. Falling back to autonegotiation.\n");
selectedMedium = mediumTable[MEDIUM_INDEX_AUTO];
}
selectMedium(selectedMedium);
setLinkStatus(kIONetworkLinkValid);
enableRTL8100();
/* In case we are using an msi the interrupt hasn't been enabled by start(). */
interruptSource->enable();
txDescDoneCount = txDescDoneLast = 0;
deadlockWarn = 0;
needsUpdate = false;
isEnabled = true;
polling = false;
timerSource->setTimeoutMS(kTimeoutMS);
result = kIOReturnSuccess;
DebugLog("enable() <===\n");
done:
return result;
}
/*! @function disable
@abstract A request from an interface client to disable the controller.
@discussion This method is called by an interface client to disable the controller.
This method should stop the hardware and disable hardware interrupt
sources. Any resources allocated by enable() should also be deallocated.
This method call is synchronized by the workloop's gate.
@param interface The interface object that requested the disable.
@result kIOReturnUnsupported. Drivers that override this method must
return Returns kIOReturnSuccess on success, or an error code otherwise.
*/
IOReturn RTL8100::disable(IONetworkInterface *netif)
{
IOReturn result = kIOReturnSuccess;
DebugLog("disable() ===>\n");
if (!isEnabled)
goto done;
netif->stopOutputThread();
netif->flushOutputQueue();
polling = false;
isEnabled = false;
timerSource->cancelTimeout();
needsUpdate = false;
txDescDoneCount = txDescDoneLast = 0;
/* In case we are using msi disable the interrupt. */
interruptSource->disable();
disableRTL8100();
setLinkStatus(kIONetworkLinkValid);
linkUp = false;
txClearDescriptors();
if (pciDevice && pciDevice->isOpen())
pciDevice->close(this);
freeDMADescriptors();
DebugLog("disable() <===\n");
done:
return result;
}
/*! @function outputStart
@abstract An indication to the driver to dequeue and transmit packets
waiting in the interface output queue.
@discussion A driver that supports the pull output model must override this
method, which will be called by a per-interface output thread when a packet
is added to the interface output queue. In response, driver must verify that
free transmit resources are available, then dequeue one or more packets by
calling <code>IONetworkInterface::dequeueOutputPackets()</code>. Packets
removed from the queue are owned by the driver, and should be immediately
prepared for transmission. Additional software queueing at the driver layer
to store the dequeued packets for delayed transmission is highly discouraged
unless absolutely necessary. If transmit resources are exhausted, the driver
should quickly return <code>kIOReturnNoResources</code> to force the output
thread to retry later, otherwise the output thread will continue to call
this method until the output queue is empty. When driver creates a single
network interface, this method will execute in a single threaded context.
However, it is the driver's responsibility to protect transmit resources
that are shared with other driver threads. To simplify drivers that wish to
process output packets on their work loop context, the family provides an
option to force the output thread to always call this method through a
<code>runAction()</code>. However this can have negative performance
implications due to extra locking and serializing the output thread against
other work loop events. Another option that drivers can deploy to
synchronize against the output thread is to issue a thread stop before
touching any shared resources. But this should be used sparingly on the
data path since stopping the output thread can block.
@param interface The network interface with packet(s) to transmit.
@param options Always zero.
@result <code>kIOReturnSuccess</code> on success, output thread will
continue calling the driver until the output queue is empty.
<code>kIOReturnNoResources</code> when there is a temporary driver resource
shortage.
*/
IOReturn RTL8100::outputStart(IONetworkInterface *interface, IOOptionBits options )
{
IOPhysicalSegment txSegments[kMaxSegs];
mbuf_t m;
RtlDmaDesc *desc, *firstDesc;
IOReturn result = kIOReturnNoResources;
UInt32 cmd;
UInt32 opts2;
mbuf_tso_request_flags_t tsoFlags;
mbuf_csum_request_flags_t checksums;
UInt32 mssValue;
UInt32 opts1;
UInt32 vlanTag;
UInt32 numSegs;
UInt32 lastSeg;
UInt32 index;
UInt32 i;
//DebugLog("outputStart() ===>\n");
if (!(isEnabled && linkUp)) {
DebugLog("Ethernet [RealtekRTL8100]: Interface down. Dropping packets.\n");
goto done;
}
while ((txNumFreeDesc > (kMaxSegs + 3)) && (interface->dequeueOutputPackets(1, &m, NULL, NULL, NULL) == kIOReturnSuccess)) {
cmd = 0;
opts2 = 0;
if (mbuf_get_tso_requested(m, &tsoFlags, &mssValue)) {
DebugLog("Ethernet [RealtekRTL8100]: mbuf_get_tso_requested() failed. Dropping packet.\n");
freePacket(m);
continue;
}
if (tsoFlags & (MBUF_TSO_IPV4 | MBUF_TSO_IPV6)) {
if (tsoFlags & MBUF_TSO_IPV4) {
getTso4Command(&cmd, &opts2, mssValue, tsoFlags);
} else {
/* The pseudoheader checksum has to be adjusted first. */
adjustIPv6Header(m);
getTso6Command(&cmd, &opts2, mssValue, tsoFlags);
}
} else {
/* We use mssValue as a dummy here because it isn't needed anymore. */
mbuf_get_csum_requested(m, &checksums, &mssValue);
getChecksumCommand(&cmd, &opts2, checksums);
}
/* Finally get the physical segments. */
numSegs = txMbufCursor->getPhysicalSegmentsWithCoalesce(m, &txSegments[0], kMaxSegs);
/* Alloc required number of descriptors. As the descriptor which has been freed last must be
* considered to be still in use we never fill the ring completely but leave at least one
* unused.
*/
if (!numSegs) {
DebugLog("Ethernet [RealtekRTL8100]: getPhysicalSegmentsWithCoalesce() failed. Dropping packet.\n");
freePacket(m);
continue;
}
OSAddAtomic(-numSegs, &txNumFreeDesc);
index = txNextDescIndex;
txNextDescIndex = (txNextDescIndex + numSegs) & kTxDescMask;
firstDesc = &txDescArray[index];
lastSeg = numSegs - 1;
/* Next fill in the VLAN tag. */
opts2 |= (getVlanTagDemand(m, &vlanTag)) ? (OSSwapInt16(vlanTag) | TxVlanTag) : 0;
/* And finally fill in the descriptors. */
for (i = 0; i < numSegs; i++) {
desc = &txDescArray[index];
opts1 = (((UInt32)txSegments[i].length) | cmd);
opts1 |= (i == 0) ? FirstFrag : DescOwn;
if (i == lastSeg) {
opts1 |= LastFrag;
txMbufArray[index] = m;
} else {
txMbufArray[index] = NULL;
}
if (index == kTxLastDesc)
opts1 |= RingEnd;
desc->addr = OSSwapHostToLittleInt64(txSegments[i].location);
desc->opts2 = OSSwapHostToLittleInt32(opts2);
desc->opts1 = OSSwapHostToLittleInt32(opts1);
//DebugLog("opts1=0x%x, opts2=0x%x, addr=0x%llx, len=0x%llx\n", opts1, opts2, txSegments[i].location, txSegments[i].length);
++index &= kTxDescMask;
}
firstDesc->opts1 |= DescOwn;
}
/* Set the polling bit. */
WriteReg8(TxPoll, NPQ);
result = (txNumFreeDesc > (kMaxSegs + 3)) ? kIOReturnSuccess : kIOReturnNoResources;
done:
//DebugLog("outputStart() <===\n");
return result;
}
/*! @function getPacketBufferConstraints
@abstract Gets the controller's packet buffer constraints.
@discussion Called by start() to obtain the constraints on the
memory buffer for each mbuf packet allocated through allocatePacket().
Drivers can override this method to specify the buffer constraints
imposed by their bus master hardware. Note that outbound packets,
those that originate from the network stack, are not currently
subject to the constraints reported here.
@param constraints A pointer to an IOPacketBufferConstraints
structure that this method is expected to initialize.
See IOPacketBufferConstraints structure definition.
*/
void RTL8100::getPacketBufferConstraints(IOPacketBufferConstraints *constraints) const
{
DebugLog("getPacketBufferConstraints() ===>\n");
constraints->alignStart = kIOPacketBufferAlign8;
constraints->alignLength = kIOPacketBufferAlign8;
DebugLog("getPacketBufferConstraints() <===\n");
}
/*! @function createOutputQueue
@abstract Creates an IOOutputQueue to handle output packet queueing,
and also to resolve contention for the controller's transmitter from
multiple client threads.
@discussion This method is called by start() to create an IOOutputQueue object to
handle output packet queueing. The default implementation will always
return 0, hence no output queue will be created. A driver may override
this method and return a subclass of IOOutputQueue. IONetworkController
will keep a reference to the queue created, and will release this
object when IONetworkController is freed. Also see getOutputQueue().
@result Returns a newly allocated and initialized IOOutputQueue object.
*/
IOOutputQueue* RTL8100::createOutputQueue()
{
DebugLog("createOutputQueue() ===>\n");
DebugLog("createOutputQueue() <===\n");
return IOBasicOutputQueue::withTarget(this);
}
/*! @function newVendorString
@result Returns a string describing the vendor of the network controller.
The caller is responsible for releasing the string object returned. */
const OSString* RTL8100::newVendorString() const
{
DebugLog("newVendorString() ===>\n");
DebugLog("newVendorString() <===\n");
return OSString::withCString("Realtek");
}
/*! @function newModelString
@result Returns a string describing the model of the network controller.
The caller is responsible for releasing the string object returned. */
const OSString* RTL8100::newModelString() const
{
DebugLog("newModelString() ===>\n");
DebugLog("newModelString() <===\n");
return OSString::withCString(rtl_chip_info[linuxData.chipset].name);
}
/*! @function configureInterface
@abstract Configures a newly created network interface object.
@discussion This method configures an interface object that was created by
createInterface(). Subclasses can override this method to customize
and examine the interface object that will be attached to the
controller as a client.
@param interface The interface object to be configured.
@result Returns true if the operation was successful, false otherwise
(this will cause attachInterface() to fail and return 0).
*/
bool RTL8100::configureInterface(IONetworkInterface *interface)
{
char modelName[kNameLenght];
IONetworkData *data;
IOReturn error;
bool result;
DebugLog("configureInterface() ===>\n");
result = super::configureInterface(interface);
if (!result)
goto done;
/* Get the generic network statistics structure. */
data = interface->getParameter(kIONetworkStatsKey);
if (data) {
netStats = (IONetworkStats *)data->getBuffer();
if (!netStats) {
IOLog("Ethernet [RealtekRTL8100]: Error getting IONetworkStats\n.");
result = false;
goto done;
}
}
/* Get the Ethernet statistics structure. */
data = interface->getParameter(kIOEthernetStatsKey);
if (data) {
etherStats = (IOEthernetStats *)data->getBuffer();
if (!etherStats) {
IOLog("Ethernet [RealtekRTL8100]: Error getting IOEthernetStats\n.");
result = false;
goto done;
}
}
/* Enable support for the new network driver interface with packet scheduling. */
error = interface->configureOutputPullModel(512, 0, 0, IONetworkInterface::kOutputPacketSchedulingModelNormal);
if (error != kIOReturnSuccess) {
IOLog("Ethernet [RealtekRTL8100]: configureOutputPullModel() failed\n.");
result = false;
goto done;
}
/* Enable support for polled receive mode. */
if (rxPoll) {
error = interface->configureInputPacketPolling(kNumRxDesc, kIONetworkWorkLoopSynchronous);
if (error != kIOReturnSuccess) {
IOLog("Ethernet [RealtekRTL8100]: configureInputPacketPolling() failed\n.");
result = false;
goto done;
}
}
snprintf(modelName, kNameLenght, "Realtek %s PCI Express Fast Ethernet", rtl_chip_info[linuxData.chipset].name);
setProperty("model", modelName);
DebugLog("configureInterface() <===\n");
done:
return result;
}
/*! @function createWorkLoop
@abstract Method called by IONetworkController prior to the initial
getWorkLoop() call.
@discussion Before IONetworkController calls getWorkLoop() in its
start() method, it will call createWorkLoop() to make sure that a
subclass that wants to create a workloop, will do so before its
first use.
@result Returns true to indicate success, false otherwise. Returning false
will fail IONetworkController::start().
*/
bool RTL8100::createWorkLoop()
{
DebugLog("createWorkLoop() ===>\n");
workLoop = IOWorkLoop::workLoop();
DebugLog("createWorkLoop() <===\n");
return workLoop ? true : false;
}
/*! @function getWorkLoop
@abstract Returns the current work loop or <code>provider->getWorkLoop</code>.
@discussion This function returns a valid work loop that a client can use to add an IOCommandGate to. The intention is that an IOService client has data that needs to be protected but doesn't want to pay the cost of a dedicated thread. This data has to be accessed from a provider's call-out context as well. So to achieve both of these goals the client creates an IOCommandGate to lock access to his data but he registers it with the provider's work loop, i.e. the work loop which will make the completion call-outs. This avoids a potential deadlock because the work loop gate uses a recursive lock, which allows the same lock to be held multiple times by a single thread.
@result A work loop, either the current work loop or it walks up the @link getProvider getProvider@/link chain calling <code>getWorkLoop</code>. Eventually it will reach a valid work loop-based driver or the root of the I/O tree, where it will return a system-wide work loop. Returns 0 if it fails to find (or create) a work loop.*/
IOWorkLoop* RTL8100::getWorkLoop() const
{
DebugLog("getWorkLoop() ===>\n");
DebugLog("getWorkLoop() <===\n");
return workLoop;
}
/* Methods inherited from IOEthernetController. */
/*! @function getHardwareAddress
@abstract Gets the Ethernet controller's permanent station address.
@discussion Ethernet drivers must implement this method, by reading the
address from hardware and writing it to the buffer provided. This method
is called from the workloop context.
@param addrP Pointer to an IOEthernetAddress where the hardware address
should be returned.
@result Returns kIOReturnSuccess on success, or an error return code otherwise.
*/
IOReturn RTL8100::getHardwareAddress(IOEthernetAddress *addr)
{
IOReturn result = kIOReturnError;
DebugLog("getHardwareAddress() ===>\n");
if (addr) {
bcopy(&currMacAddr.bytes, addr->bytes, kIOEthernetAddressSize);
result = kIOReturnSuccess;
}
DebugLog("getHardwareAddress() <===\n");
return result;
}
/*! @function setHardwareAddress
@abstract Sets or changes the station address used by the Ethernet
controller.
@discussion This method is called in response to a client command to
change the station address used by the Ethernet controller. Implementation
of this method is optional. This method is called from the workloop context.
@param addrP Pointer to an IOEthernetAddress containing the new station
address.
@result The default implementation will always return kIOReturnUnsupported.
If overridden, drivers must return kIOReturnSuccess on success, or an error
return code otherwise.
*/
IOReturn RTL8100::setHardwareAddress(const IOEthernetAddress *addr)
{
IOReturn result = kIOReturnError;
DebugLog("setHardwareAddress() ===>\n");
if (addr) {
bcopy(addr->bytes, &currMacAddr.bytes, kIOEthernetAddressSize);
rtl8101_rar_set(&linuxData, (UInt8 *)&currMacAddr.bytes);
result = kIOReturnSuccess;
}
DebugLog("setHardwareAddress() <===\n");
return result;
}
/*! @function setPromiscuousMode
@abstract Enables or disables promiscuous mode.
@discussion Called by enablePacketFilter() or disablePacketFilter()
when there is a change in the activation state of the promiscuous
filter identified by kIOPacketFilterPromiscuous. This method is
called from the workloop context.
@param active True to enable promiscuous mode, false to disable it.
@result Returns kIOReturnUnsupported. If overridden, drivers must return
kIOReturnSuccess on success, or an error return code otherwise.
*/
IOReturn RTL8100::setPromiscuousMode(bool active)
{
UInt32 *filterAddr = (UInt32 *)&multicastFilter;
UInt32 mcFilter[2];
UInt32 rxMode;
DebugLog("setPromiscuousMode() ===>\n");
if (active) {
DebugLog("Ethernet [RealtekRTL8100]: Promiscuous mode enabled.\n");
rxMode = (AcceptBroadcast | AcceptMulticast | AcceptMyPhys | AcceptAllPhys);
mcFilter[1] = mcFilter[0] = 0xffffffff;
} else {
DebugLog("Ethernet [RealtekRTL8100]: Promiscuous mode disabled.\n");
rxMode = (AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
mcFilter[0] = *filterAddr++;
mcFilter[1] = *filterAddr;
}
promiscusMode = active;
rxMode |= (ReadReg32(RxConfig) & rxConfigMask);
WriteReg32(RxConfig, rxMode);
WriteReg32(MAR0, mcFilter[0]);
WriteReg32(MAR1, mcFilter[1]);
DebugLog("setPromiscuousMode() <===\n");
return kIOReturnSuccess;
}
/*! @function setMulticastMode
@abstract Enables or disables multicast mode.
@discussion Called by enablePacketFilter() or disablePacketFilter()
when there is a change in the activation state of the multicast filter
identified by kIOPacketFilterMulticast. This method is called from the
workloop context.
@param active True to enable multicast mode, false to disable it.
@result Returns kIOReturnUnsupported. If overridden, drivers must return
kIOReturnSuccess on success, or an error return code otherwise.
*/
IOReturn RTL8100::setMulticastMode(bool active)
{
UInt32 *filterAddr = (UInt32 *)&multicastFilter;
UInt32 mcFilter[2];
UInt32 rxMode;
DebugLog("setMulticastMode() ===>\n");
if (active) {
rxMode = (AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
mcFilter[0] = *filterAddr++;
mcFilter[1] = *filterAddr;
} else{
rxMode = (AcceptBroadcast | AcceptMyPhys);
mcFilter[1] = mcFilter[0] = 0;
}
multicastMode = active;
rxMode |= (ReadReg32(RxConfig) & rxConfigMask);
WriteReg32(RxConfig, rxMode);
WriteReg32(MAR0, mcFilter[0]);
WriteReg32(MAR1, mcFilter[1]);
DebugLog("setMulticastMode() <===\n");
return kIOReturnSuccess;
}
/*! @function setMulticastList
@abstract Sets the list of multicast addresses a multicast filter
should use to match against the destination address of an incoming frame.
@discussion This method sets the list of multicast addresses that the multicast filter
should use to match against the destination address of an incoming frame.
The frame should be accepted when a match occurs. Called when the multicast group membership of an interface
object is changed. Drivers that support kIOPacketFilterMulticast should
override this method and update the hardware multicast filter using the
list of Ethernet addresses provided. Perfect multicast filtering is
preferred if supported by the hardware, in order to reduce the number of
unwanted packets received. If the number of multicast addresses in the
list exceeds what the hardware is capable of supporting, or if perfect
filtering is not supported, then ideally the hardware should be programmed
to perform imperfect filtering, through some form of hash filtering
mechanism. Only as a last resort should the driver enable reception of
all multicast packets to satisfy this request. This method is called
from the workloop context, and only if the driver reports
kIOPacketFilterMulticast support in getPacketFilters().
@param addrs An array of Ethernet addresses. This argument must be
ignored if the count argument is 0.
@param count The number of Ethernet addresses in the list. This value
will be zero when the list becomes empty.
@result Returns kIOReturnUnsupported. Drivers must return kIOReturnSuccess to
indicate success, or an error return code otherwise.
*/
IOReturn RTL8100::setMulticastList(IOEthernetAddress *addrs, UInt32 count)
{
UInt32 *filterAddr = (UInt32 *)&multicastFilter;
UInt64 filter = 0;
UInt32 i, bitNumber;
DebugLog("setMulticastList() ===>\n");
if (count <= kMCFilterLimit) {
for (i = 0; i < count; i++, addrs++) {
bitNumber = ether_crc(6, reinterpret_cast<unsigned char *>(addrs)) >> 26;
filter |= (1 << (bitNumber & 0x3f));
}
multicastFilter = OSSwapInt64(filter);
} else {
multicastFilter = 0xffffffffffffffff;
}
WriteReg32(MAR0, *filterAddr++);
WriteReg32(MAR1, *filterAddr);
DebugLog("setMulticastList() <===\n");
return kIOReturnSuccess;
}
/*! @function getChecksumSupport
@abstract Gets checksums that are supported by the network controller for
the given checksum family.
@discussion A network controller that is capable of inserting and verifying
checksums on output and input packets, should override this method and
advertise its capability in order to assist or offload the software checksum
calculations performed by the protocol stacks.
@param checksumMask A pointer to the mask of supported checksums returned
by this method.
@param checksumFamily A value that specifies the checksum family.