r8169.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445
  1. /*
  2. * r8169.c: RealTek 8169/8168/8101 ethernet driver.
  3. *
  4. * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
  5. * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
  6. * Copyright (c) a lot of people too. Please respect their work.
  7. *
  8. * See MAINTAINERS file for support contact information.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/pci.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/delay.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/mii.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/crc32.h>
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <linux/tcp.h>
  23. #include <linux/init.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/firmware.h>
  27. #include <asm/system.h>
  28. #include <asm/io.h>
  29. #include <asm/irq.h>
  30. #define RTL8169_VERSION "2.3LK-NAPI"
  31. #define MODULENAME "r8169"
  32. #define PFX MODULENAME ": "
  33. #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
  34. #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
  35. #ifdef RTL8169_DEBUG
  36. #define assert(expr) \
  37. if (!(expr)) { \
  38. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  39. #expr,__FILE__,__func__,__LINE__); \
  40. }
  41. #define dprintk(fmt, args...) \
  42. do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
  43. #else
  44. #define assert(expr) do {} while (0)
  45. #define dprintk(fmt, args...) do {} while (0)
  46. #endif /* RTL8169_DEBUG */
  47. #define R8169_MSG_DEFAULT \
  48. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  49. #define TX_BUFFS_AVAIL(tp) \
  50. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
  51. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  52. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  53. static const int multicast_filter_limit = 32;
  54. /* MAC address length */
  55. #define MAC_ADDR_LEN 6
  56. #define MAX_READ_REQUEST_SHIFT 12
  57. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  58. #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  59. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  60. #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
  61. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  62. #define R8169_REGS_SIZE 256
  63. #define R8169_NAPI_WEIGHT 64
  64. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  65. #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
  66. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  67. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  68. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  69. #define RTL8169_TX_TIMEOUT (6*HZ)
  70. #define RTL8169_PHY_TIMEOUT (10*HZ)
  71. #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
  72. #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
  73. #define RTL_EEPROM_SIG_ADDR 0x0000
  74. /* write/read MMIO register */
  75. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  76. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  77. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  78. #define RTL_R8(reg) readb (ioaddr + (reg))
  79. #define RTL_R16(reg) readw (ioaddr + (reg))
  80. #define RTL_R32(reg) readl (ioaddr + (reg))
  81. enum mac_version {
  82. RTL_GIGA_MAC_NONE = 0x00,
  83. RTL_GIGA_MAC_VER_01 = 0x01, // 8169
  84. RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
  85. RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
  86. RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
  87. RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
  88. RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
  89. RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
  90. RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
  91. RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
  92. RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
  93. RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
  94. RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
  95. RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
  96. RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
  97. RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
  98. RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
  99. RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
  100. RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
  101. RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
  102. RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
  103. RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
  104. RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
  105. RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
  106. RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
  107. RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
  108. RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
  109. RTL_GIGA_MAC_VER_27 = 0x1b // 8168DP
  110. };
  111. #define _R(NAME,MAC,MASK) \
  112. { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
  113. static const struct {
  114. const char *name;
  115. u8 mac_version;
  116. u32 RxConfigMask; /* Clears the bits supported by this chip */
  117. } rtl_chip_info[] = {
  118. _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
  119. _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
  120. _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
  121. _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
  122. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
  123. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
  124. _R("RTL8102e", RTL_GIGA_MAC_VER_07, 0xff7e1880), // PCI-E
  125. _R("RTL8102e", RTL_GIGA_MAC_VER_08, 0xff7e1880), // PCI-E
  126. _R("RTL8102e", RTL_GIGA_MAC_VER_09, 0xff7e1880), // PCI-E
  127. _R("RTL8101e", RTL_GIGA_MAC_VER_10, 0xff7e1880), // PCI-E
  128. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
  129. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
  130. _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
  131. _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
  132. _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
  133. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
  134. _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
  135. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
  136. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
  137. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880), // PCI-E
  138. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, 0xff7e1880), // PCI-E
  139. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, 0xff7e1880), // PCI-E
  140. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, 0xff7e1880), // PCI-E
  141. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, 0xff7e1880), // PCI-E
  142. _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, 0xff7e1880), // PCI-E
  143. _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, 0xff7e1880), // PCI-E
  144. _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, 0xff7e1880) // PCI-E
  145. };
  146. #undef _R
  147. enum cfg_version {
  148. RTL_CFG_0 = 0x00,
  149. RTL_CFG_1,
  150. RTL_CFG_2
  151. };
  152. static void rtl_hw_start_8169(struct net_device *);
  153. static void rtl_hw_start_8168(struct net_device *);
  154. static void rtl_hw_start_8101(struct net_device *);
  155. static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
  156. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  157. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  158. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  159. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  160. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  161. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  162. { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
  163. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  164. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  165. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  166. { 0x0001, 0x8168,
  167. PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
  168. {0,},
  169. };
  170. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  171. static int rx_buf_sz = 16383;
  172. static int use_dac;
  173. static struct {
  174. u32 msg_enable;
  175. } debug = { -1 };
  176. enum rtl_registers {
  177. MAC0 = 0, /* Ethernet hardware address. */
  178. MAC4 = 4,
  179. MAR0 = 8, /* Multicast filter. */
  180. CounterAddrLow = 0x10,
  181. CounterAddrHigh = 0x14,
  182. TxDescStartAddrLow = 0x20,
  183. TxDescStartAddrHigh = 0x24,
  184. TxHDescStartAddrLow = 0x28,
  185. TxHDescStartAddrHigh = 0x2c,
  186. FLASH = 0x30,
  187. ERSR = 0x36,
  188. ChipCmd = 0x37,
  189. TxPoll = 0x38,
  190. IntrMask = 0x3c,
  191. IntrStatus = 0x3e,
  192. TxConfig = 0x40,
  193. RxConfig = 0x44,
  194. RxMissed = 0x4c,
  195. Cfg9346 = 0x50,
  196. Config0 = 0x51,
  197. Config1 = 0x52,
  198. Config2 = 0x53,
  199. Config3 = 0x54,
  200. Config4 = 0x55,
  201. Config5 = 0x56,
  202. MultiIntr = 0x5c,
  203. PHYAR = 0x60,
  204. PHYstatus = 0x6c,
  205. RxMaxSize = 0xda,
  206. CPlusCmd = 0xe0,
  207. IntrMitigate = 0xe2,
  208. RxDescAddrLow = 0xe4,
  209. RxDescAddrHigh = 0xe8,
  210. EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
  211. #define NoEarlyTx 0x3f /* Max value : no early transmit. */
  212. MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
  213. #define TxPacketMax (8064 >> 7)
  214. FuncEvent = 0xf0,
  215. FuncEventMask = 0xf4,
  216. FuncPresetState = 0xf8,
  217. FuncForceEvent = 0xfc,
  218. };
  219. enum rtl8110_registers {
  220. TBICSR = 0x64,
  221. TBI_ANAR = 0x68,
  222. TBI_LPAR = 0x6a,
  223. };
  224. enum rtl8168_8101_registers {
  225. CSIDR = 0x64,
  226. CSIAR = 0x68,
  227. #define CSIAR_FLAG 0x80000000
  228. #define CSIAR_WRITE_CMD 0x80000000
  229. #define CSIAR_BYTE_ENABLE 0x0f
  230. #define CSIAR_BYTE_ENABLE_SHIFT 12
  231. #define CSIAR_ADDR_MASK 0x0fff
  232. EPHYAR = 0x80,
  233. #define EPHYAR_FLAG 0x80000000
  234. #define EPHYAR_WRITE_CMD 0x80000000
  235. #define EPHYAR_REG_MASK 0x1f
  236. #define EPHYAR_REG_SHIFT 16
  237. #define EPHYAR_DATA_MASK 0xffff
  238. DBG_REG = 0xd1,
  239. #define FIX_NAK_1 (1 << 4)
  240. #define FIX_NAK_2 (1 << 3)
  241. EFUSEAR = 0xdc,
  242. #define EFUSEAR_FLAG 0x80000000
  243. #define EFUSEAR_WRITE_CMD 0x80000000
  244. #define EFUSEAR_READ_CMD 0x00000000
  245. #define EFUSEAR_REG_MASK 0x03ff
  246. #define EFUSEAR_REG_SHIFT 8
  247. #define EFUSEAR_DATA_MASK 0xff
  248. };
  249. enum rtl_register_content {
  250. /* InterruptStatusBits */
  251. SYSErr = 0x8000,
  252. PCSTimeout = 0x4000,
  253. SWInt = 0x0100,
  254. TxDescUnavail = 0x0080,
  255. RxFIFOOver = 0x0040,
  256. LinkChg = 0x0020,
  257. RxOverflow = 0x0010,
  258. TxErr = 0x0008,
  259. TxOK = 0x0004,
  260. RxErr = 0x0002,
  261. RxOK = 0x0001,
  262. /* RxStatusDesc */
  263. RxFOVF = (1 << 23),
  264. RxRWT = (1 << 22),
  265. RxRES = (1 << 21),
  266. RxRUNT = (1 << 20),
  267. RxCRC = (1 << 19),
  268. /* ChipCmdBits */
  269. CmdReset = 0x10,
  270. CmdRxEnb = 0x08,
  271. CmdTxEnb = 0x04,
  272. RxBufEmpty = 0x01,
  273. /* TXPoll register p.5 */
  274. HPQ = 0x80, /* Poll cmd on the high prio queue */
  275. NPQ = 0x40, /* Poll cmd on the low prio queue */
  276. FSWInt = 0x01, /* Forced software interrupt */
  277. /* Cfg9346Bits */
  278. Cfg9346_Lock = 0x00,
  279. Cfg9346_Unlock = 0xc0,
  280. /* rx_mode_bits */
  281. AcceptErr = 0x20,
  282. AcceptRunt = 0x10,
  283. AcceptBroadcast = 0x08,
  284. AcceptMulticast = 0x04,
  285. AcceptMyPhys = 0x02,
  286. AcceptAllPhys = 0x01,
  287. /* RxConfigBits */
  288. RxCfgFIFOShift = 13,
  289. RxCfgDMAShift = 8,
  290. /* TxConfigBits */
  291. TxInterFrameGapShift = 24,
  292. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  293. /* Config1 register p.24 */
  294. LEDS1 = (1 << 7),
  295. LEDS0 = (1 << 6),
  296. MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
  297. Speed_down = (1 << 4),
  298. MEMMAP = (1 << 3),
  299. IOMAP = (1 << 2),
  300. VPD = (1 << 1),
  301. PMEnable = (1 << 0), /* Power Management Enable */
  302. /* Config2 register p. 25 */
  303. PCI_Clock_66MHz = 0x01,
  304. PCI_Clock_33MHz = 0x00,
  305. /* Config3 register p.25 */
  306. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  307. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  308. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  309. /* Config5 register p.27 */
  310. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  311. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  312. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  313. LanWake = (1 << 1), /* LanWake enable/disable */
  314. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  315. /* TBICSR p.28 */
  316. TBIReset = 0x80000000,
  317. TBILoopback = 0x40000000,
  318. TBINwEnable = 0x20000000,
  319. TBINwRestart = 0x10000000,
  320. TBILinkOk = 0x02000000,
  321. TBINwComplete = 0x01000000,
  322. /* CPlusCmd p.31 */
  323. EnableBist = (1 << 15), // 8168 8101
  324. Mac_dbgo_oe = (1 << 14), // 8168 8101
  325. Normal_mode = (1 << 13), // unused
  326. Force_half_dup = (1 << 12), // 8168 8101
  327. Force_rxflow_en = (1 << 11), // 8168 8101
  328. Force_txflow_en = (1 << 10), // 8168 8101
  329. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  330. ASF = (1 << 8), // 8168 8101
  331. PktCntrDisable = (1 << 7), // 8168 8101
  332. Mac_dbgo_sel = 0x001c, // 8168
  333. RxVlan = (1 << 6),
  334. RxChkSum = (1 << 5),
  335. PCIDAC = (1 << 4),
  336. PCIMulRW = (1 << 3),
  337. INTT_0 = 0x0000, // 8168
  338. INTT_1 = 0x0001, // 8168
  339. INTT_2 = 0x0002, // 8168
  340. INTT_3 = 0x0003, // 8168
  341. /* rtl8169_PHYstatus */
  342. TBI_Enable = 0x80,
  343. TxFlowCtrl = 0x40,
  344. RxFlowCtrl = 0x20,
  345. _1000bpsF = 0x10,
  346. _100bps = 0x08,
  347. _10bps = 0x04,
  348. LinkStatus = 0x02,
  349. FullDup = 0x01,
  350. /* _TBICSRBit */
  351. TBILinkOK = 0x02000000,
  352. /* DumpCounterCommand */
  353. CounterDump = 0x8,
  354. };
  355. enum desc_status_bit {
  356. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  357. RingEnd = (1 << 30), /* End of descriptor ring */
  358. FirstFrag = (1 << 29), /* First segment of a packet */
  359. LastFrag = (1 << 28), /* Final segment of a packet */
  360. /* Tx private */
  361. LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
  362. MSSShift = 16, /* MSS value position */
  363. MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
  364. IPCS = (1 << 18), /* Calculate IP checksum */
  365. UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
  366. TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
  367. TxVlanTag = (1 << 17), /* Add VLAN tag */
  368. /* Rx private */
  369. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  370. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  371. #define RxProtoUDP (PID1)
  372. #define RxProtoTCP (PID0)
  373. #define RxProtoIP (PID1 | PID0)
  374. #define RxProtoMask RxProtoIP
  375. IPFail = (1 << 16), /* IP checksum failed */
  376. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  377. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  378. RxVlanTag = (1 << 16), /* VLAN tag available */
  379. };
  380. #define RsvdMask 0x3fffc000
  381. struct TxDesc {
  382. __le32 opts1;
  383. __le32 opts2;
  384. __le64 addr;
  385. };
  386. struct RxDesc {
  387. __le32 opts1;
  388. __le32 opts2;
  389. __le64 addr;
  390. };
  391. struct ring_info {
  392. struct sk_buff *skb;
  393. u32 len;
  394. u8 __pad[sizeof(void *) - sizeof(u32)];
  395. };
  396. enum features {
  397. RTL_FEATURE_WOL = (1 << 0),
  398. RTL_FEATURE_MSI = (1 << 1),
  399. RTL_FEATURE_GMII = (1 << 2),
  400. };
  401. struct rtl8169_counters {
  402. __le64 tx_packets;
  403. __le64 rx_packets;
  404. __le64 tx_errors;
  405. __le32 rx_errors;
  406. __le16 rx_missed;
  407. __le16 align_errors;
  408. __le32 tx_one_collision;
  409. __le32 tx_multi_collision;
  410. __le64 rx_unicast;
  411. __le64 rx_broadcast;
  412. __le32 rx_multicast;
  413. __le16 tx_aborted;
  414. __le16 tx_underun;
  415. };
  416. struct rtl8169_private {
  417. void __iomem *mmio_addr; /* memory map physical address */
  418. struct pci_dev *pci_dev; /* Index of PCI device */
  419. struct net_device *dev;
  420. struct napi_struct napi;
  421. spinlock_t lock; /* spin lock flag */
  422. u32 msg_enable;
  423. int chipset;
  424. int mac_version;
  425. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  426. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  427. u32 dirty_rx;
  428. u32 dirty_tx;
  429. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  430. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  431. dma_addr_t TxPhyAddr;
  432. dma_addr_t RxPhyAddr;
  433. void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
  434. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  435. struct timer_list timer;
  436. u16 cp_cmd;
  437. u16 intr_event;
  438. u16 napi_event;
  439. u16 intr_mask;
  440. int phy_1000_ctrl_reg;
  441. #ifdef CONFIG_R8169_VLAN
  442. struct vlan_group *vlgrp;
  443. #endif
  444. int (*set_speed)(struct net_device *, u8 autoneg, u16 speed, u8 duplex);
  445. int (*get_settings)(struct net_device *, struct ethtool_cmd *);
  446. void (*phy_reset_enable)(struct rtl8169_private *tp);
  447. void (*hw_start)(struct net_device *);
  448. unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
  449. unsigned int (*link_ok)(void __iomem *);
  450. int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
  451. int pcie_cap;
  452. struct delayed_work task;
  453. unsigned features;
  454. struct mii_if_info mii;
  455. struct rtl8169_counters counters;
  456. u32 saved_wolopts;
  457. };
  458. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  459. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  460. module_param(use_dac, int, 0);
  461. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  462. module_param_named(debug, debug.msg_enable, int, 0);
  463. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  464. MODULE_LICENSE("GPL");
  465. MODULE_VERSION(RTL8169_VERSION);
  466. MODULE_FIRMWARE(FIRMWARE_8168D_1);
  467. MODULE_FIRMWARE(FIRMWARE_8168D_2);
  468. static int rtl8169_open(struct net_device *dev);
  469. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  470. struct net_device *dev);
  471. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
  472. static int rtl8169_init_ring(struct net_device *dev);
  473. static void rtl_hw_start(struct net_device *dev);
  474. static int rtl8169_close(struct net_device *dev);
  475. static void rtl_set_rx_mode(struct net_device *dev);
  476. static void rtl8169_tx_timeout(struct net_device *dev);
  477. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
  478. static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
  479. void __iomem *, u32 budget);
  480. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
  481. static void rtl8169_down(struct net_device *dev);
  482. static void rtl8169_rx_clear(struct rtl8169_private *tp);
  483. static int rtl8169_poll(struct napi_struct *napi, int budget);
  484. static const unsigned int rtl8169_rx_config =
  485. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
  486. static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  487. {
  488. int i;
  489. RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
  490. for (i = 20; i > 0; i--) {
  491. /*
  492. * Check if the RTL8169 has completed writing to the specified
  493. * MII register.
  494. */
  495. if (!(RTL_R32(PHYAR) & 0x80000000))
  496. break;
  497. udelay(25);
  498. }
  499. /*
  500. * According to hardware specs a 20us delay is required after write
  501. * complete indication, but before sending next command.
  502. */
  503. udelay(20);
  504. }
  505. static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
  506. {
  507. int i, value = -1;
  508. RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
  509. for (i = 20; i > 0; i--) {
  510. /*
  511. * Check if the RTL8169 has completed retrieving data from
  512. * the specified MII register.
  513. */
  514. if (RTL_R32(PHYAR) & 0x80000000) {
  515. value = RTL_R32(PHYAR) & 0xffff;
  516. break;
  517. }
  518. udelay(25);
  519. }
  520. /*
  521. * According to hardware specs a 20us delay is required after read
  522. * complete indication, but before sending next command.
  523. */
  524. udelay(20);
  525. return value;
  526. }
  527. static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
  528. {
  529. r8169_mdio_write(tp->mmio_addr, location, val);
  530. }
  531. static int rtl_readphy(struct rtl8169_private *tp, int location)
  532. {
  533. return r8169_mdio_read(tp->mmio_addr, location);
  534. }
  535. static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
  536. {
  537. rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
  538. }
  539. static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
  540. {
  541. int val;
  542. val = rtl_readphy(tp, reg_addr);
  543. rtl_writephy(tp, reg_addr, (val | p) & ~m);
  544. }
  545. static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
  546. int val)
  547. {
  548. struct rtl8169_private *tp = netdev_priv(dev);
  549. rtl_writephy(tp, location, val);
  550. }
  551. static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
  552. {
  553. struct rtl8169_private *tp = netdev_priv(dev);
  554. return rtl_readphy(tp, location);
  555. }
  556. static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
  557. {
  558. unsigned int i;
  559. RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  560. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  561. for (i = 0; i < 100; i++) {
  562. if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
  563. break;
  564. udelay(10);
  565. }
  566. }
  567. static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
  568. {
  569. u16 value = 0xffff;
  570. unsigned int i;
  571. RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  572. for (i = 0; i < 100; i++) {
  573. if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
  574. value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
  575. break;
  576. }
  577. udelay(10);
  578. }
  579. return value;
  580. }
  581. static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
  582. {
  583. unsigned int i;
  584. RTL_W32(CSIDR, value);
  585. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  586. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  587. for (i = 0; i < 100; i++) {
  588. if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
  589. break;
  590. udelay(10);
  591. }
  592. }
  593. static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
  594. {
  595. u32 value = ~0x00;
  596. unsigned int i;
  597. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  598. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  599. for (i = 0; i < 100; i++) {
  600. if (RTL_R32(CSIAR) & CSIAR_FLAG) {
  601. value = RTL_R32(CSIDR);
  602. break;
  603. }
  604. udelay(10);
  605. }
  606. return value;
  607. }
  608. static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
  609. {
  610. u8 value = 0xff;
  611. unsigned int i;
  612. RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  613. for (i = 0; i < 300; i++) {
  614. if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
  615. value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
  616. break;
  617. }
  618. udelay(100);
  619. }
  620. return value;
  621. }
  622. static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
  623. {
  624. RTL_W16(IntrMask, 0x0000);
  625. RTL_W16(IntrStatus, 0xffff);
  626. }
  627. static void rtl8169_asic_down(void __iomem *ioaddr)
  628. {
  629. RTL_W8(ChipCmd, 0x00);
  630. rtl8169_irq_mask_and_ack(ioaddr);
  631. RTL_R16(CPlusCmd);
  632. }
  633. static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
  634. {
  635. void __iomem *ioaddr = tp->mmio_addr;
  636. return RTL_R32(TBICSR) & TBIReset;
  637. }
  638. static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
  639. {
  640. return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
  641. }
  642. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  643. {
  644. return RTL_R32(TBICSR) & TBILinkOk;
  645. }
  646. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  647. {
  648. return RTL_R8(PHYstatus) & LinkStatus;
  649. }
  650. static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
  651. {
  652. void __iomem *ioaddr = tp->mmio_addr;
  653. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  654. }
  655. static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
  656. {
  657. unsigned int val;
  658. val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
  659. rtl_writephy(tp, MII_BMCR, val & 0xffff);
  660. }
  661. static void __rtl8169_check_link_status(struct net_device *dev,
  662. struct rtl8169_private *tp,
  663. void __iomem *ioaddr,
  664. bool pm)
  665. {
  666. unsigned long flags;
  667. spin_lock_irqsave(&tp->lock, flags);
  668. if (tp->link_ok(ioaddr)) {
  669. /* This is to cancel a scheduled suspend if there's one. */
  670. if (pm)
  671. pm_request_resume(&tp->pci_dev->dev);
  672. netif_carrier_on(dev);
  673. netif_info(tp, ifup, dev, "link up\n");
  674. } else {
  675. netif_carrier_off(dev);
  676. netif_info(tp, ifdown, dev, "link down\n");
  677. if (pm)
  678. pm_schedule_suspend(&tp->pci_dev->dev, 100);
  679. }
  680. spin_unlock_irqrestore(&tp->lock, flags);
  681. }
  682. static void rtl8169_check_link_status(struct net_device *dev,
  683. struct rtl8169_private *tp,
  684. void __iomem *ioaddr)
  685. {
  686. __rtl8169_check_link_status(dev, tp, ioaddr, false);
  687. }
  688. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  689. static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
  690. {
  691. void __iomem *ioaddr = tp->mmio_addr;
  692. u8 options;
  693. u32 wolopts = 0;
  694. options = RTL_R8(Config1);
  695. if (!(options & PMEnable))
  696. return 0;
  697. options = RTL_R8(Config3);
  698. if (options & LinkUp)
  699. wolopts |= WAKE_PHY;
  700. if (options & MagicPacket)
  701. wolopts |= WAKE_MAGIC;
  702. options = RTL_R8(Config5);
  703. if (options & UWF)
  704. wolopts |= WAKE_UCAST;
  705. if (options & BWF)
  706. wolopts |= WAKE_BCAST;
  707. if (options & MWF)
  708. wolopts |= WAKE_MCAST;
  709. return wolopts;
  710. }
  711. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  712. {
  713. struct rtl8169_private *tp = netdev_priv(dev);
  714. spin_lock_irq(&tp->lock);
  715. wol->supported = WAKE_ANY;
  716. wol->wolopts = __rtl8169_get_wol(tp);
  717. spin_unlock_irq(&tp->lock);
  718. }
  719. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  720. {
  721. void __iomem *ioaddr = tp->mmio_addr;
  722. unsigned int i;
  723. static const struct {
  724. u32 opt;
  725. u16 reg;
  726. u8 mask;
  727. } cfg[] = {
  728. { WAKE_ANY, Config1, PMEnable },
  729. { WAKE_PHY, Config3, LinkUp },
  730. { WAKE_MAGIC, Config3, MagicPacket },
  731. { WAKE_UCAST, Config5, UWF },
  732. { WAKE_BCAST, Config5, BWF },
  733. { WAKE_MCAST, Config5, MWF },
  734. { WAKE_ANY, Config5, LanWake }
  735. };
  736. RTL_W8(Cfg9346, Cfg9346_Unlock);
  737. for (i = 0; i < ARRAY_SIZE(cfg); i++) {
  738. u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  739. if (wolopts & cfg[i].opt)
  740. options |= cfg[i].mask;
  741. RTL_W8(cfg[i].reg, options);
  742. }
  743. RTL_W8(Cfg9346, Cfg9346_Lock);
  744. }
  745. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  746. {
  747. struct rtl8169_private *tp = netdev_priv(dev);
  748. spin_lock_irq(&tp->lock);
  749. if (wol->wolopts)
  750. tp->features |= RTL_FEATURE_WOL;
  751. else
  752. tp->features &= ~RTL_FEATURE_WOL;
  753. __rtl8169_set_wol(tp, wol->wolopts);
  754. spin_unlock_irq(&tp->lock);
  755. device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
  756. return 0;
  757. }
  758. static void rtl8169_get_drvinfo(struct net_device *dev,
  759. struct ethtool_drvinfo *info)
  760. {
  761. struct rtl8169_private *tp = netdev_priv(dev);
  762. strcpy(info->driver, MODULENAME);
  763. strcpy(info->version, RTL8169_VERSION);
  764. strcpy(info->bus_info, pci_name(tp->pci_dev));
  765. }
  766. static int rtl8169_get_regs_len(struct net_device *dev)
  767. {
  768. return R8169_REGS_SIZE;
  769. }
  770. static int rtl8169_set_speed_tbi(struct net_device *dev,
  771. u8 autoneg, u16 speed, u8 duplex)
  772. {
  773. struct rtl8169_private *tp = netdev_priv(dev);
  774. void __iomem *ioaddr = tp->mmio_addr;
  775. int ret = 0;
  776. u32 reg;
  777. reg = RTL_R32(TBICSR);
  778. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  779. (duplex == DUPLEX_FULL)) {
  780. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  781. } else if (autoneg == AUTONEG_ENABLE)
  782. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  783. else {
  784. netif_warn(tp, link, dev,
  785. "incorrect speed setting refused in TBI mode\n");
  786. ret = -EOPNOTSUPP;
  787. }
  788. return ret;
  789. }
  790. static int rtl8169_set_speed_xmii(struct net_device *dev,
  791. u8 autoneg, u16 speed, u8 duplex)
  792. {
  793. struct rtl8169_private *tp = netdev_priv(dev);
  794. int giga_ctrl, bmcr;
  795. if (autoneg == AUTONEG_ENABLE) {
  796. int auto_nego;
  797. auto_nego = rtl_readphy(tp, MII_ADVERTISE);
  798. auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
  799. ADVERTISE_100HALF | ADVERTISE_100FULL);
  800. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  801. giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
  802. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  803. /* The 8100e/8101e/8102e do Fast Ethernet only. */
  804. if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
  805. (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
  806. (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
  807. (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
  808. (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
  809. (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
  810. (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
  811. (tp->mac_version != RTL_GIGA_MAC_VER_16)) {
  812. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  813. } else {
  814. netif_info(tp, link, dev,
  815. "PHY does not support 1000Mbps\n");
  816. }
  817. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  818. if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
  819. (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
  820. (tp->mac_version >= RTL_GIGA_MAC_VER_17)) {
  821. /*
  822. * Wake up the PHY.
  823. * Vendor specific (0x1f) and reserved (0x0e) MII
  824. * registers.
  825. */
  826. rtl_writephy(tp, 0x1f, 0x0000);
  827. rtl_writephy(tp, 0x0e, 0x0000);
  828. }
  829. rtl_writephy(tp, MII_ADVERTISE, auto_nego);
  830. rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
  831. } else {
  832. giga_ctrl = 0;
  833. if (speed == SPEED_10)
  834. bmcr = 0;
  835. else if (speed == SPEED_100)
  836. bmcr = BMCR_SPEED100;
  837. else
  838. return -EINVAL;
  839. if (duplex == DUPLEX_FULL)
  840. bmcr |= BMCR_FULLDPLX;
  841. rtl_writephy(tp, 0x1f, 0x0000);
  842. }
  843. tp->phy_1000_ctrl_reg = giga_ctrl;
  844. rtl_writephy(tp, MII_BMCR, bmcr);
  845. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  846. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  847. if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
  848. rtl_writephy(tp, 0x17, 0x2138);
  849. rtl_writephy(tp, 0x0e, 0x0260);
  850. } else {
  851. rtl_writephy(tp, 0x17, 0x2108);
  852. rtl_writephy(tp, 0x0e, 0x0000);
  853. }
  854. }
  855. return 0;
  856. }
  857. static int rtl8169_set_speed(struct net_device *dev,
  858. u8 autoneg, u16 speed, u8 duplex)
  859. {
  860. struct rtl8169_private *tp = netdev_priv(dev);
  861. int ret;
  862. ret = tp->set_speed(dev, autoneg, speed, duplex);
  863. if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  864. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  865. return ret;
  866. }
  867. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  868. {
  869. struct rtl8169_private *tp = netdev_priv(dev);
  870. unsigned long flags;
  871. int ret;
  872. spin_lock_irqsave(&tp->lock, flags);
  873. ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
  874. spin_unlock_irqrestore(&tp->lock, flags);
  875. return ret;
  876. }
  877. static u32 rtl8169_get_rx_csum(struct net_device *dev)
  878. {
  879. struct rtl8169_private *tp = netdev_priv(dev);
  880. return tp->cp_cmd & RxChkSum;
  881. }
  882. static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
  883. {
  884. struct rtl8169_private *tp = netdev_priv(dev);
  885. void __iomem *ioaddr = tp->mmio_addr;
  886. unsigned long flags;
  887. spin_lock_irqsave(&tp->lock, flags);
  888. if (data)
  889. tp->cp_cmd |= RxChkSum;
  890. else
  891. tp->cp_cmd &= ~RxChkSum;
  892. RTL_W16(CPlusCmd, tp->cp_cmd);
  893. RTL_R16(CPlusCmd);
  894. spin_unlock_irqrestore(&tp->lock, flags);
  895. return 0;
  896. }
  897. #ifdef CONFIG_R8169_VLAN
  898. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  899. struct sk_buff *skb)
  900. {
  901. return (vlan_tx_tag_present(skb)) ?
  902. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  903. }
  904. static void rtl8169_vlan_rx_register(struct net_device *dev,
  905. struct vlan_group *grp)
  906. {
  907. struct rtl8169_private *tp = netdev_priv(dev);
  908. void __iomem *ioaddr = tp->mmio_addr;
  909. unsigned long flags;
  910. spin_lock_irqsave(&tp->lock, flags);
  911. tp->vlgrp = grp;
  912. /*
  913. * Do not disable RxVlan on 8110SCd.
  914. */
  915. if (tp->vlgrp || (tp->mac_version == RTL_GIGA_MAC_VER_05))
  916. tp->cp_cmd |= RxVlan;
  917. else
  918. tp->cp_cmd &= ~RxVlan;
  919. RTL_W16(CPlusCmd, tp->cp_cmd);
  920. RTL_R16(CPlusCmd);
  921. spin_unlock_irqrestore(&tp->lock, flags);
  922. }
  923. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  924. struct sk_buff *skb, int polling)
  925. {
  926. u32 opts2 = le32_to_cpu(desc->opts2);
  927. struct vlan_group *vlgrp = tp->vlgrp;
  928. int ret;
  929. if (vlgrp && (opts2 & RxVlanTag)) {
  930. u16 vtag = swab16(opts2 & 0xffff);
  931. if (likely(polling))
  932. vlan_gro_receive(&tp->napi, vlgrp, vtag, skb);
  933. else
  934. __vlan_hwaccel_rx(skb, vlgrp, vtag, polling);
  935. ret = 0;
  936. } else
  937. ret = -1;
  938. desc->opts2 = 0;
  939. return ret;
  940. }
  941. #else /* !CONFIG_R8169_VLAN */
  942. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  943. struct sk_buff *skb)
  944. {
  945. return 0;
  946. }
  947. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  948. struct sk_buff *skb, int polling)
  949. {
  950. return -1;
  951. }
  952. #endif
  953. static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  954. {
  955. struct rtl8169_private *tp = netdev_priv(dev);
  956. void __iomem *ioaddr = tp->mmio_addr;
  957. u32 status;
  958. cmd->supported =
  959. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  960. cmd->port = PORT_FIBRE;
  961. cmd->transceiver = XCVR_INTERNAL;
  962. status = RTL_R32(TBICSR);
  963. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  964. cmd->autoneg = !!(status & TBINwEnable);
  965. cmd->speed = SPEED_1000;
  966. cmd->duplex = DUPLEX_FULL; /* Always set */
  967. return 0;
  968. }
  969. static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  970. {
  971. struct rtl8169_private *tp = netdev_priv(dev);
  972. return mii_ethtool_gset(&tp->mii, cmd);
  973. }
  974. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  975. {
  976. struct rtl8169_private *tp = netdev_priv(dev);
  977. unsigned long flags;
  978. int rc;
  979. spin_lock_irqsave(&tp->lock, flags);
  980. rc = tp->get_settings(dev, cmd);
  981. spin_unlock_irqrestore(&tp->lock, flags);
  982. return rc;
  983. }
  984. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  985. void *p)
  986. {
  987. struct rtl8169_private *tp = netdev_priv(dev);
  988. unsigned long flags;
  989. if (regs->len > R8169_REGS_SIZE)
  990. regs->len = R8169_REGS_SIZE;
  991. spin_lock_irqsave(&tp->lock, flags);
  992. memcpy_fromio(p, tp->mmio_addr, regs->len);
  993. spin_unlock_irqrestore(&tp->lock, flags);
  994. }
  995. static u32 rtl8169_get_msglevel(struct net_device *dev)
  996. {
  997. struct rtl8169_private *tp = netdev_priv(dev);
  998. return tp->msg_enable;
  999. }
  1000. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1001. {
  1002. struct rtl8169_private *tp = netdev_priv(dev);
  1003. tp->msg_enable = value;
  1004. }
  1005. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1006. "tx_packets",
  1007. "rx_packets",
  1008. "tx_errors",
  1009. "rx_errors",
  1010. "rx_missed",
  1011. "align_errors",
  1012. "tx_single_collisions",
  1013. "tx_multi_collisions",
  1014. "unicast",
  1015. "broadcast",
  1016. "multicast",
  1017. "tx_aborted",
  1018. "tx_underrun",
  1019. };
  1020. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1021. {
  1022. switch (sset) {
  1023. case ETH_SS_STATS:
  1024. return ARRAY_SIZE(rtl8169_gstrings);
  1025. default:
  1026. return -EOPNOTSUPP;
  1027. }
  1028. }
  1029. static void rtl8169_update_counters(struct net_device *dev)
  1030. {
  1031. struct rtl8169_private *tp = netdev_priv(dev);
  1032. void __iomem *ioaddr = tp->mmio_addr;
  1033. struct rtl8169_counters *counters;
  1034. dma_addr_t paddr;
  1035. u32 cmd;
  1036. int wait = 1000;
  1037. struct device *d = &tp->pci_dev->dev;
  1038. /*
  1039. * Some chips are unable to dump tally counters when the receiver
  1040. * is disabled.
  1041. */
  1042. if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
  1043. return;
  1044. counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
  1045. if (!counters)
  1046. return;
  1047. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  1048. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1049. RTL_W32(CounterAddrLow, cmd);
  1050. RTL_W32(CounterAddrLow, cmd | CounterDump);
  1051. while (wait--) {
  1052. if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
  1053. /* copy updated counters */
  1054. memcpy(&tp->counters, counters, sizeof(*counters));
  1055. break;
  1056. }
  1057. udelay(10);
  1058. }
  1059. RTL_W32(CounterAddrLow, 0);
  1060. RTL_W32(CounterAddrHigh, 0);
  1061. dma_free_coherent(d, sizeof(*counters), counters, paddr);
  1062. }
  1063. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1064. struct ethtool_stats *stats, u64 *data)
  1065. {
  1066. struct rtl8169_private *tp = netdev_priv(dev);
  1067. ASSERT_RTNL();
  1068. rtl8169_update_counters(dev);
  1069. data[0] = le64_to_cpu(tp->counters.tx_packets);
  1070. data[1] = le64_to_cpu(tp->counters.rx_packets);
  1071. data[2] = le64_to_cpu(tp->counters.tx_errors);
  1072. data[3] = le32_to_cpu(tp->counters.rx_errors);
  1073. data[4] = le16_to_cpu(tp->counters.rx_missed);
  1074. data[5] = le16_to_cpu(tp->counters.align_errors);
  1075. data[6] = le32_to_cpu(tp->counters.tx_one_collision);
  1076. data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
  1077. data[8] = le64_to_cpu(tp->counters.rx_unicast);
  1078. data[9] = le64_to_cpu(tp->counters.rx_broadcast);
  1079. data[10] = le32_to_cpu(tp->counters.rx_multicast);
  1080. data[11] = le16_to_cpu(tp->counters.tx_aborted);
  1081. data[12] = le16_to_cpu(tp->counters.tx_underun);
  1082. }
  1083. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1084. {
  1085. switch(stringset) {
  1086. case ETH_SS_STATS:
  1087. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1088. break;
  1089. }
  1090. }
  1091. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1092. .get_drvinfo = rtl8169_get_drvinfo,
  1093. .get_regs_len = rtl8169_get_regs_len,
  1094. .get_link = ethtool_op_get_link,
  1095. .get_settings = rtl8169_get_settings,
  1096. .set_settings = rtl8169_set_settings,
  1097. .get_msglevel = rtl8169_get_msglevel,
  1098. .set_msglevel = rtl8169_set_msglevel,
  1099. .get_rx_csum = rtl8169_get_rx_csum,
  1100. .set_rx_csum = rtl8169_set_rx_csum,
  1101. .set_tx_csum = ethtool_op_set_tx_csum,
  1102. .set_sg = ethtool_op_set_sg,
  1103. .set_tso = ethtool_op_set_tso,
  1104. .get_regs = rtl8169_get_regs,
  1105. .get_wol = rtl8169_get_wol,
  1106. .set_wol = rtl8169_set_wol,
  1107. .get_strings = rtl8169_get_strings,
  1108. .get_sset_count = rtl8169_get_sset_count,
  1109. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1110. };
  1111. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1112. void __iomem *ioaddr)
  1113. {
  1114. /*
  1115. * The driver currently handles the 8168Bf and the 8168Be identically
  1116. * but they can be identified more specifically through the test below
  1117. * if needed:
  1118. *
  1119. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1120. *
  1121. * Same thing for the 8101Eb and the 8101Ec:
  1122. *
  1123. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1124. */
  1125. static const struct {
  1126. u32 mask;
  1127. u32 val;
  1128. int mac_version;
  1129. } mac_info[] = {
  1130. /* 8168D family. */
  1131. { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
  1132. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  1133. { 0x7c800000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  1134. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  1135. /* 8168C family. */
  1136. { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
  1137. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  1138. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  1139. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  1140. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  1141. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  1142. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  1143. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  1144. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  1145. /* 8168B family. */
  1146. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  1147. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  1148. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  1149. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  1150. /* 8101 family. */
  1151. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  1152. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  1153. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  1154. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  1155. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  1156. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  1157. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  1158. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  1159. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  1160. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  1161. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  1162. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  1163. /* FIXME: where did these entries come from ? -- FR */
  1164. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  1165. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  1166. /* 8110 family. */
  1167. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  1168. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  1169. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  1170. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  1171. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  1172. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  1173. /* Catch-all */
  1174. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  1175. }, *p = mac_info;
  1176. u32 reg;
  1177. reg = RTL_R32(TxConfig);
  1178. while ((reg & p->mask) != p->val)
  1179. p++;
  1180. tp->mac_version = p->mac_version;
  1181. }
  1182. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  1183. {
  1184. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  1185. }
  1186. struct phy_reg {
  1187. u16 reg;
  1188. u16 val;
  1189. };
  1190. static void rtl_writephy_batch(struct rtl8169_private *tp,
  1191. const struct phy_reg *regs, int len)
  1192. {
  1193. while (len-- > 0) {
  1194. rtl_writephy(tp, regs->reg, regs->val);
  1195. regs++;
  1196. }
  1197. }
  1198. #define PHY_READ 0x00000000
  1199. #define PHY_DATA_OR 0x10000000
  1200. #define PHY_DATA_AND 0x20000000
  1201. #define PHY_BJMPN 0x30000000
  1202. #define PHY_READ_EFUSE 0x40000000
  1203. #define PHY_READ_MAC_BYTE 0x50000000
  1204. #define PHY_WRITE_MAC_BYTE 0x60000000
  1205. #define PHY_CLEAR_READCOUNT 0x70000000
  1206. #define PHY_WRITE 0x80000000
  1207. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  1208. #define PHY_COMP_EQ_SKIPN 0xa0000000
  1209. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  1210. #define PHY_WRITE_PREVIOUS 0xc0000000
  1211. #define PHY_SKIPN 0xd0000000
  1212. #define PHY_DELAY_MS 0xe0000000
  1213. #define PHY_WRITE_ERI_WORD 0xf0000000
  1214. static void
  1215. rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
  1216. {
  1217. __le32 *phytable = (__le32 *)fw->data;
  1218. struct net_device *dev = tp->dev;
  1219. size_t i;
  1220. if (fw->size % sizeof(*phytable)) {
  1221. netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
  1222. return;
  1223. }
  1224. for (i = 0; i < fw->size / sizeof(*phytable); i++) {
  1225. u32 action = le32_to_cpu(phytable[i]);
  1226. if (!action)
  1227. break;
  1228. if ((action & 0xf0000000) != PHY_WRITE) {
  1229. netif_err(tp, probe, dev,
  1230. "unknown action 0x%08x\n", action);
  1231. return;
  1232. }
  1233. }
  1234. while (i-- != 0) {
  1235. u32 action = le32_to_cpu(*phytable);
  1236. u32 data = action & 0x0000ffff;
  1237. u32 reg = (action & 0x0fff0000) >> 16;
  1238. switch(action & 0xf0000000) {
  1239. case PHY_WRITE:
  1240. rtl_writephy(tp, reg, data);
  1241. phytable++;
  1242. break;
  1243. default:
  1244. BUG();
  1245. }
  1246. }
  1247. }
  1248. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  1249. {
  1250. static const struct phy_reg phy_reg_init[] = {
  1251. { 0x1f, 0x0001 },
  1252. { 0x06, 0x006e },
  1253. { 0x08, 0x0708 },
  1254. { 0x15, 0x4000 },
  1255. { 0x18, 0x65c7 },
  1256. { 0x1f, 0x0001 },
  1257. { 0x03, 0x00a1 },
  1258. { 0x02, 0x0008 },
  1259. { 0x01, 0x0120 },
  1260. { 0x00, 0x1000 },
  1261. { 0x04, 0x0800 },
  1262. { 0x04, 0x0000 },
  1263. { 0x03, 0xff41 },
  1264. { 0x02, 0xdf60 },
  1265. { 0x01, 0x0140 },
  1266. { 0x00, 0x0077 },
  1267. { 0x04, 0x7800 },
  1268. { 0x04, 0x7000 },
  1269. { 0x03, 0x802f },
  1270. { 0x02, 0x4f02 },
  1271. { 0x01, 0x0409 },
  1272. { 0x00, 0xf0f9 },
  1273. { 0x04, 0x9800 },
  1274. { 0x04, 0x9000 },
  1275. { 0x03, 0xdf01 },
  1276. { 0x02, 0xdf20 },
  1277. { 0x01, 0xff95 },
  1278. { 0x00, 0xba00 },
  1279. { 0x04, 0xa800 },
  1280. { 0x04, 0xa000 },
  1281. { 0x03, 0xff41 },
  1282. { 0x02, 0xdf20 },
  1283. { 0x01, 0x0140 },
  1284. { 0x00, 0x00bb },
  1285. { 0x04, 0xb800 },
  1286. { 0x04, 0xb000 },
  1287. { 0x03, 0xdf41 },
  1288. { 0x02, 0xdc60 },
  1289. { 0x01, 0x6340 },
  1290. { 0x00, 0x007d },
  1291. { 0x04, 0xd800 },
  1292. { 0x04, 0xd000 },
  1293. { 0x03, 0xdf01 },
  1294. { 0x02, 0xdf20 },
  1295. { 0x01, 0x100a },
  1296. { 0x00, 0xa0ff },
  1297. { 0x04, 0xf800 },
  1298. { 0x04, 0xf000 },
  1299. { 0x1f, 0x0000 },
  1300. { 0x0b, 0x0000 },
  1301. { 0x00, 0x9200 }
  1302. };
  1303. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1304. }
  1305. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  1306. {
  1307. static const struct phy_reg phy_reg_init[] = {
  1308. { 0x1f, 0x0002 },
  1309. { 0x01, 0x90d0 },
  1310. { 0x1f, 0x0000 }
  1311. };
  1312. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1313. }
  1314. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  1315. {
  1316. struct pci_dev *pdev = tp->pci_dev;
  1317. u16 vendor_id, device_id;
  1318. pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
  1319. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
  1320. if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
  1321. return;
  1322. rtl_writephy(tp, 0x1f, 0x0001);
  1323. rtl_writephy(tp, 0x10, 0xf01b);
  1324. rtl_writephy(tp, 0x1f, 0x0000);
  1325. }
  1326. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  1327. {
  1328. static const struct phy_reg phy_reg_init[] = {
  1329. { 0x1f, 0x0001 },
  1330. { 0x04, 0x0000 },
  1331. { 0x03, 0x00a1 },
  1332. { 0x02, 0x0008 },
  1333. { 0x01, 0x0120 },
  1334. { 0x00, 0x1000 },
  1335. { 0x04, 0x0800 },
  1336. { 0x04, 0x9000 },
  1337. { 0x03, 0x802f },
  1338. { 0x02, 0x4f02 },
  1339. { 0x01, 0x0409 },
  1340. { 0x00, 0xf099 },
  1341. { 0x04, 0x9800 },
  1342. { 0x04, 0xa000 },
  1343. { 0x03, 0xdf01 },
  1344. { 0x02, 0xdf20 },
  1345. { 0x01, 0xff95 },
  1346. { 0x00, 0xba00 },
  1347. { 0x04, 0xa800 },
  1348. { 0x04, 0xf000 },
  1349. { 0x03, 0xdf01 },
  1350. { 0x02, 0xdf20 },
  1351. { 0x01, 0x101a },
  1352. { 0x00, 0xa0ff },
  1353. { 0x04, 0xf800 },
  1354. { 0x04, 0x0000 },
  1355. { 0x1f, 0x0000 },
  1356. { 0x1f, 0x0001 },
  1357. { 0x10, 0xf41b },
  1358. { 0x14, 0xfb54 },
  1359. { 0x18, 0xf5c7 },
  1360. { 0x1f, 0x0000 },
  1361. { 0x1f, 0x0001 },
  1362. { 0x17, 0x0cc0 },
  1363. { 0x1f, 0x0000 }
  1364. };
  1365. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1366. rtl8169scd_hw_phy_config_quirk(tp);
  1367. }
  1368. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  1369. {
  1370. static const struct phy_reg phy_reg_init[] = {
  1371. { 0x1f, 0x0001 },
  1372. { 0x04, 0x0000 },
  1373. { 0x03, 0x00a1 },
  1374. { 0x02, 0x0008 },
  1375. { 0x01, 0x0120 },
  1376. { 0x00, 0x1000 },
  1377. { 0x04, 0x0800 },
  1378. { 0x04, 0x9000 },
  1379. { 0x03, 0x802f },
  1380. { 0x02, 0x4f02 },
  1381. { 0x01, 0x0409 },
  1382. { 0x00, 0xf099 },
  1383. { 0x04, 0x9800 },
  1384. { 0x04, 0xa000 },
  1385. { 0x03, 0xdf01 },
  1386. { 0x02, 0xdf20 },
  1387. { 0x01, 0xff95 },
  1388. { 0x00, 0xba00 },
  1389. { 0x04, 0xa800 },
  1390. { 0x04, 0xf000 },
  1391. { 0x03, 0xdf01 },
  1392. { 0x02, 0xdf20 },
  1393. { 0x01, 0x101a },
  1394. { 0x00, 0xa0ff },
  1395. { 0x04, 0xf800 },
  1396. { 0x04, 0x0000 },
  1397. { 0x1f, 0x0000 },
  1398. { 0x1f, 0x0001 },
  1399. { 0x0b, 0x8480 },
  1400. { 0x1f, 0x0000 },
  1401. { 0x1f, 0x0001 },
  1402. { 0x18, 0x67c7 },
  1403. { 0x04, 0x2000 },
  1404. { 0x03, 0x002f },
  1405. { 0x02, 0x4360 },
  1406. { 0x01, 0x0109 },
  1407. { 0x00, 0x3022 },
  1408. { 0x04, 0x2800 },
  1409. { 0x1f, 0x0000 },
  1410. { 0x1f, 0x0001 },
  1411. { 0x17, 0x0cc0 },
  1412. { 0x1f, 0x0000 }
  1413. };
  1414. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1415. }
  1416. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  1417. {
  1418. static const struct phy_reg phy_reg_init[] = {
  1419. { 0x10, 0xf41b },
  1420. { 0x1f, 0x0000 }
  1421. };
  1422. rtl_writephy(tp, 0x1f, 0x0001);
  1423. rtl_patchphy(tp, 0x16, 1 << 0);
  1424. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1425. }
  1426. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  1427. {
  1428. static const struct phy_reg phy_reg_init[] = {
  1429. { 0x1f, 0x0001 },
  1430. { 0x10, 0xf41b },
  1431. { 0x1f, 0x0000 }
  1432. };
  1433. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1434. }
  1435. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  1436. {
  1437. static const struct phy_reg phy_reg_init[] = {
  1438. { 0x1f, 0x0000 },
  1439. { 0x1d, 0x0f00 },
  1440. { 0x1f, 0x0002 },
  1441. { 0x0c, 0x1ec8 },
  1442. { 0x1f, 0x0000 }
  1443. };
  1444. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1445. }
  1446. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  1447. {
  1448. static const struct phy_reg phy_reg_init[] = {
  1449. { 0x1f, 0x0001 },
  1450. { 0x1d, 0x3d98 },
  1451. { 0x1f, 0x0000 }
  1452. };
  1453. rtl_writephy(tp, 0x1f, 0x0000);
  1454. rtl_patchphy(tp, 0x14, 1 << 5);
  1455. rtl_patchphy(tp, 0x0d, 1 << 5);
  1456. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1457. }
  1458. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  1459. {
  1460. static const struct phy_reg phy_reg_init[] = {
  1461. { 0x1f, 0x0001 },
  1462. { 0x12, 0x2300 },
  1463. { 0x1f, 0x0002 },
  1464. { 0x00, 0x88d4 },
  1465. { 0x01, 0x82b1 },
  1466. { 0x03, 0x7002 },
  1467. { 0x08, 0x9e30 },
  1468. { 0x09, 0x01f0 },
  1469. { 0x0a, 0x5500 },
  1470. { 0x0c, 0x00c8 },
  1471. { 0x1f, 0x0003 },
  1472. { 0x12, 0xc096 },
  1473. { 0x16, 0x000a },
  1474. { 0x1f, 0x0000 },
  1475. { 0x1f, 0x0000 },
  1476. { 0x09, 0x2000 },
  1477. { 0x09, 0x0000 }
  1478. };
  1479. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1480. rtl_patchphy(tp, 0x14, 1 << 5);
  1481. rtl_patchphy(tp, 0x0d, 1 << 5);
  1482. rtl_writephy(tp, 0x1f, 0x0000);
  1483. }
  1484. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  1485. {
  1486. static const struct phy_reg phy_reg_init[] = {
  1487. { 0x1f, 0x0001 },
  1488. { 0x12, 0x2300 },
  1489. { 0x03, 0x802f },
  1490. { 0x02, 0x4f02 },
  1491. { 0x01, 0x0409 },
  1492. { 0x00, 0xf099 },
  1493. { 0x04, 0x9800 },
  1494. { 0x04, 0x9000 },
  1495. { 0x1d, 0x3d98 },
  1496. { 0x1f, 0x0002 },
  1497. { 0x0c, 0x7eb8 },
  1498. { 0x06, 0x0761 },
  1499. { 0x1f, 0x0003 },
  1500. { 0x16, 0x0f0a },
  1501. { 0x1f, 0x0000 }
  1502. };
  1503. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1504. rtl_patchphy(tp, 0x16, 1 << 0);
  1505. rtl_patchphy(tp, 0x14, 1 << 5);
  1506. rtl_patchphy(tp, 0x0d, 1 << 5);
  1507. rtl_writephy(tp, 0x1f, 0x0000);
  1508. }
  1509. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  1510. {
  1511. static const struct phy_reg phy_reg_init[] = {
  1512. { 0x1f, 0x0001 },
  1513. { 0x12, 0x2300 },
  1514. { 0x1d, 0x3d98 },
  1515. { 0x1f, 0x0002 },
  1516. { 0x0c, 0x7eb8 },
  1517. { 0x06, 0x5461 },
  1518. { 0x1f, 0x0003 },
  1519. { 0x16, 0x0f0a },
  1520. { 0x1f, 0x0000 }
  1521. };
  1522. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1523. rtl_patchphy(tp, 0x16, 1 << 0);
  1524. rtl_patchphy(tp, 0x14, 1 << 5);
  1525. rtl_patchphy(tp, 0x0d, 1 << 5);
  1526. rtl_writephy(tp, 0x1f, 0x0000);
  1527. }
  1528. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  1529. {
  1530. rtl8168c_3_hw_phy_config(tp);
  1531. }
  1532. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  1533. {
  1534. static const struct phy_reg phy_reg_init_0[] = {
  1535. /* Channel Estimation */
  1536. { 0x1f, 0x0001 },
  1537. { 0x06, 0x4064 },
  1538. { 0x07, 0x2863 },
  1539. { 0x08, 0x059c },
  1540. { 0x09, 0x26b4 },
  1541. { 0x0a, 0x6a19 },
  1542. { 0x0b, 0xdcc8 },
  1543. { 0x10, 0xf06d },
  1544. { 0x14, 0x7f68 },
  1545. { 0x18, 0x7fd9 },
  1546. { 0x1c, 0xf0ff },
  1547. { 0x1d, 0x3d9c },
  1548. { 0x1f, 0x0003 },
  1549. { 0x12, 0xf49f },
  1550. { 0x13, 0x070b },
  1551. { 0x1a, 0x05ad },
  1552. { 0x14, 0x94c0 },
  1553. /*
  1554. * Tx Error Issue
  1555. * enhance line driver power
  1556. */
  1557. { 0x1f, 0x0002 },
  1558. { 0x06, 0x5561 },
  1559. { 0x1f, 0x0005 },
  1560. { 0x05, 0x8332 },
  1561. { 0x06, 0x5561 },
  1562. /*
  1563. * Can not link to 1Gbps with bad cable
  1564. * Decrease SNR threshold form 21.07dB to 19.04dB
  1565. */
  1566. { 0x1f, 0x0001 },
  1567. { 0x17, 0x0cc0 },
  1568. { 0x1f, 0x0000 },
  1569. { 0x0d, 0xf880 }
  1570. };
  1571. void __iomem *ioaddr = tp->mmio_addr;
  1572. const struct firmware *fw;
  1573. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  1574. /*
  1575. * Rx Error Issue
  1576. * Fine Tune Switching regulator parameter
  1577. */
  1578. rtl_writephy(tp, 0x1f, 0x0002);
  1579. rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
  1580. rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
  1581. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  1582. static const struct phy_reg phy_reg_init[] = {
  1583. { 0x1f, 0x0002 },
  1584. { 0x05, 0x669a },
  1585. { 0x1f, 0x0005 },
  1586. { 0x05, 0x8330 },
  1587. { 0x06, 0x669a },
  1588. { 0x1f, 0x0002 }
  1589. };
  1590. int val;
  1591. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1592. val = rtl_readphy(tp, 0x0d);
  1593. if ((val & 0x00ff) != 0x006c) {
  1594. static const u32 set[] = {
  1595. 0x0065, 0x0066, 0x0067, 0x0068,
  1596. 0x0069, 0x006a, 0x006b, 0x006c
  1597. };
  1598. int i;
  1599. rtl_writephy(tp, 0x1f, 0x0002);
  1600. val &= 0xff00;
  1601. for (i = 0; i < ARRAY_SIZE(set); i++)
  1602. rtl_writephy(tp, 0x0d, val | set[i]);
  1603. }
  1604. } else {
  1605. static const struct phy_reg phy_reg_init[] = {
  1606. { 0x1f, 0x0002 },
  1607. { 0x05, 0x6662 },
  1608. { 0x1f, 0x0005 },
  1609. { 0x05, 0x8330 },
  1610. { 0x06, 0x6662 }
  1611. };
  1612. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1613. }
  1614. /* RSET couple improve */
  1615. rtl_writephy(tp, 0x1f, 0x0002);
  1616. rtl_patchphy(tp, 0x0d, 0x0300);
  1617. rtl_patchphy(tp, 0x0f, 0x0010);
  1618. /* Fine tune PLL performance */
  1619. rtl_writephy(tp, 0x1f, 0x0002);
  1620. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  1621. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  1622. rtl_writephy(tp, 0x1f, 0x0005);
  1623. rtl_writephy(tp, 0x05, 0x001b);
  1624. if (rtl_readphy(tp, 0x06) == 0xbf00 &&
  1625. request_firmware(&fw, FIRMWARE_8168D_1, &tp->pci_dev->dev) == 0) {
  1626. rtl_phy_write_fw(tp, fw);
  1627. release_firmware(fw);
  1628. } else {
  1629. netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
  1630. }
  1631. rtl_writephy(tp, 0x1f, 0x0000);
  1632. }
  1633. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  1634. {
  1635. static const struct phy_reg phy_reg_init_0[] = {
  1636. /* Channel Estimation */
  1637. { 0x1f, 0x0001 },
  1638. { 0x06, 0x4064 },
  1639. { 0x07, 0x2863 },
  1640. { 0x08, 0x059c },
  1641. { 0x09, 0x26b4 },
  1642. { 0x0a, 0x6a19 },
  1643. { 0x0b, 0xdcc8 },
  1644. { 0x10, 0xf06d },
  1645. { 0x14, 0x7f68 },
  1646. { 0x18, 0x7fd9 },
  1647. { 0x1c, 0xf0ff },
  1648. { 0x1d, 0x3d9c },
  1649. { 0x1f, 0x0003 },
  1650. { 0x12, 0xf49f },
  1651. { 0x13, 0x070b },
  1652. { 0x1a, 0x05ad },
  1653. { 0x14, 0x94c0 },
  1654. /*
  1655. * Tx Error Issue
  1656. * enhance line driver power
  1657. */
  1658. { 0x1f, 0x0002 },
  1659. { 0x06, 0x5561 },
  1660. { 0x1f, 0x0005 },
  1661. { 0x05, 0x8332 },
  1662. { 0x06, 0x5561 },
  1663. /*
  1664. * Can not link to 1Gbps with bad cable
  1665. * Decrease SNR threshold form 21.07dB to 19.04dB
  1666. */
  1667. { 0x1f, 0x0001 },
  1668. { 0x17, 0x0cc0 },
  1669. { 0x1f, 0x0000 },
  1670. { 0x0d, 0xf880 }
  1671. };
  1672. void __iomem *ioaddr = tp->mmio_addr;
  1673. const struct firmware *fw;
  1674. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  1675. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  1676. static const struct phy_reg phy_reg_init[] = {
  1677. { 0x1f, 0x0002 },
  1678. { 0x05, 0x669a },
  1679. { 0x1f, 0x0005 },
  1680. { 0x05, 0x8330 },
  1681. { 0x06, 0x669a },
  1682. { 0x1f, 0x0002 }
  1683. };
  1684. int val;
  1685. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1686. val = rtl_readphy(tp, 0x0d);
  1687. if ((val & 0x00ff) != 0x006c) {
  1688. static const u32 set[] = {
  1689. 0x0065, 0x0066, 0x0067, 0x0068,
  1690. 0x0069, 0x006a, 0x006b, 0x006c
  1691. };
  1692. int i;
  1693. rtl_writephy(tp, 0x1f, 0x0002);
  1694. val &= 0xff00;
  1695. for (i = 0; i < ARRAY_SIZE(set); i++)
  1696. rtl_writephy(tp, 0x0d, val | set[i]);
  1697. }
  1698. } else {
  1699. static const struct phy_reg phy_reg_init[] = {
  1700. { 0x1f, 0x0002 },
  1701. { 0x05, 0x2642 },
  1702. { 0x1f, 0x0005 },
  1703. { 0x05, 0x8330 },
  1704. { 0x06, 0x2642 }
  1705. };
  1706. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1707. }
  1708. /* Fine tune PLL performance */
  1709. rtl_writephy(tp, 0x1f, 0x0002);
  1710. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  1711. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  1712. /* Switching regulator Slew rate */
  1713. rtl_writephy(tp, 0x1f, 0x0002);
  1714. rtl_patchphy(tp, 0x0f, 0x0017);
  1715. rtl_writephy(tp, 0x1f, 0x0005);
  1716. rtl_writephy(tp, 0x05, 0x001b);
  1717. if (rtl_readphy(tp, 0x06) == 0xb300 &&
  1718. request_firmware(&fw, FIRMWARE_8168D_2, &tp->pci_dev->dev) == 0) {
  1719. rtl_phy_write_fw(tp, fw);
  1720. release_firmware(fw);
  1721. } else {
  1722. netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
  1723. }
  1724. rtl_writephy(tp, 0x1f, 0x0000);
  1725. }
  1726. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  1727. {
  1728. static const struct phy_reg phy_reg_init[] = {
  1729. { 0x1f, 0x0002 },
  1730. { 0x10, 0x0008 },
  1731. { 0x0d, 0x006c },
  1732. { 0x1f, 0x0000 },
  1733. { 0x0d, 0xf880 },
  1734. { 0x1f, 0x0001 },
  1735. { 0x17, 0x0cc0 },
  1736. { 0x1f, 0x0001 },
  1737. { 0x0b, 0xa4d8 },
  1738. { 0x09, 0x281c },
  1739. { 0x07, 0x2883 },
  1740. { 0x0a, 0x6b35 },
  1741. { 0x1d, 0x3da4 },
  1742. { 0x1c, 0xeffd },
  1743. { 0x14, 0x7f52 },
  1744. { 0x18, 0x7fc6 },
  1745. { 0x08, 0x0601 },
  1746. { 0x06, 0x4063 },
  1747. { 0x10, 0xf074 },
  1748. { 0x1f, 0x0003 },
  1749. { 0x13, 0x0789 },
  1750. { 0x12, 0xf4bd },
  1751. { 0x1a, 0x04fd },
  1752. { 0x14, 0x84b0 },
  1753. { 0x1f, 0x0000 },
  1754. { 0x00, 0x9200 },
  1755. { 0x1f, 0x0005 },
  1756. { 0x01, 0x0340 },
  1757. { 0x1f, 0x0001 },
  1758. { 0x04, 0x4000 },
  1759. { 0x03, 0x1d21 },
  1760. { 0x02, 0x0c32 },
  1761. { 0x01, 0x0200 },
  1762. { 0x00, 0x5554 },
  1763. { 0x04, 0x4800 },
  1764. { 0x04, 0x4000 },
  1765. { 0x04, 0xf000 },
  1766. { 0x03, 0xdf01 },
  1767. { 0x02, 0xdf20 },
  1768. { 0x01, 0x101a },
  1769. { 0x00, 0xa0ff },
  1770. { 0x04, 0xf800 },
  1771. { 0x04, 0xf000 },
  1772. { 0x1f, 0x0000 },
  1773. { 0x1f, 0x0007 },
  1774. { 0x1e, 0x0023 },
  1775. { 0x16, 0x0000 },
  1776. { 0x1f, 0x0000 }
  1777. };
  1778. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1779. }
  1780. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  1781. {
  1782. static const struct phy_reg phy_reg_init[] = {
  1783. { 0x1f, 0x0003 },
  1784. { 0x08, 0x441d },
  1785. { 0x01, 0x9100 },
  1786. { 0x1f, 0x0000 }
  1787. };
  1788. rtl_writephy(tp, 0x1f, 0x0000);
  1789. rtl_patchphy(tp, 0x11, 1 << 12);
  1790. rtl_patchphy(tp, 0x19, 1 << 13);
  1791. rtl_patchphy(tp, 0x10, 1 << 15);
  1792. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1793. }
  1794. static void rtl_hw_phy_config(struct net_device *dev)
  1795. {
  1796. struct rtl8169_private *tp = netdev_priv(dev);
  1797. rtl8169_print_mac_version(tp);
  1798. switch (tp->mac_version) {
  1799. case RTL_GIGA_MAC_VER_01:
  1800. break;
  1801. case RTL_GIGA_MAC_VER_02:
  1802. case RTL_GIGA_MAC_VER_03:
  1803. rtl8169s_hw_phy_config(tp);
  1804. break;
  1805. case RTL_GIGA_MAC_VER_04:
  1806. rtl8169sb_hw_phy_config(tp);
  1807. break;
  1808. case RTL_GIGA_MAC_VER_05:
  1809. rtl8169scd_hw_phy_config(tp);
  1810. break;
  1811. case RTL_GIGA_MAC_VER_06:
  1812. rtl8169sce_hw_phy_config(tp);
  1813. break;
  1814. case RTL_GIGA_MAC_VER_07:
  1815. case RTL_GIGA_MAC_VER_08:
  1816. case RTL_GIGA_MAC_VER_09:
  1817. rtl8102e_hw_phy_config(tp);
  1818. break;
  1819. case RTL_GIGA_MAC_VER_11:
  1820. rtl8168bb_hw_phy_config(tp);
  1821. break;
  1822. case RTL_GIGA_MAC_VER_12:
  1823. rtl8168bef_hw_phy_config(tp);
  1824. break;
  1825. case RTL_GIGA_MAC_VER_17:
  1826. rtl8168bef_hw_phy_config(tp);
  1827. break;
  1828. case RTL_GIGA_MAC_VER_18:
  1829. rtl8168cp_1_hw_phy_config(tp);
  1830. break;
  1831. case RTL_GIGA_MAC_VER_19:
  1832. rtl8168c_1_hw_phy_config(tp);
  1833. break;
  1834. case RTL_GIGA_MAC_VER_20:
  1835. rtl8168c_2_hw_phy_config(tp);
  1836. break;
  1837. case RTL_GIGA_MAC_VER_21:
  1838. rtl8168c_3_hw_phy_config(tp);
  1839. break;
  1840. case RTL_GIGA_MAC_VER_22:
  1841. rtl8168c_4_hw_phy_config(tp);
  1842. break;
  1843. case RTL_GIGA_MAC_VER_23:
  1844. case RTL_GIGA_MAC_VER_24:
  1845. rtl8168cp_2_hw_phy_config(tp);
  1846. break;
  1847. case RTL_GIGA_MAC_VER_25:
  1848. rtl8168d_1_hw_phy_config(tp);
  1849. break;
  1850. case RTL_GIGA_MAC_VER_26:
  1851. rtl8168d_2_hw_phy_config(tp);
  1852. break;
  1853. case RTL_GIGA_MAC_VER_27:
  1854. rtl8168d_3_hw_phy_config(tp);
  1855. break;
  1856. default:
  1857. break;
  1858. }
  1859. }
  1860. static void rtl8169_phy_timer(unsigned long __opaque)
  1861. {
  1862. struct net_device *dev = (struct net_device *)__opaque;
  1863. struct rtl8169_private *tp = netdev_priv(dev);
  1864. struct timer_list *timer = &tp->timer;
  1865. void __iomem *ioaddr = tp->mmio_addr;
  1866. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  1867. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  1868. if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  1869. return;
  1870. spin_lock_irq(&tp->lock);
  1871. if (tp->phy_reset_pending(tp)) {
  1872. /*
  1873. * A busy loop could burn quite a few cycles on nowadays CPU.
  1874. * Let's delay the execution of the timer for a few ticks.
  1875. */
  1876. timeout = HZ/10;
  1877. goto out_mod_timer;
  1878. }
  1879. if (tp->link_ok(ioaddr))
  1880. goto out_unlock;
  1881. netif_warn(tp, link, dev, "PHY reset until link up\n");
  1882. tp->phy_reset_enable(tp);
  1883. out_mod_timer:
  1884. mod_timer(timer, jiffies + timeout);
  1885. out_unlock:
  1886. spin_unlock_irq(&tp->lock);
  1887. }
  1888. static inline void rtl8169_delete_timer(struct net_device *dev)
  1889. {
  1890. struct rtl8169_private *tp = netdev_priv(dev);
  1891. struct timer_list *timer = &tp->timer;
  1892. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  1893. return;
  1894. del_timer_sync(timer);
  1895. }
  1896. static inline void rtl8169_request_timer(struct net_device *dev)
  1897. {
  1898. struct rtl8169_private *tp = netdev_priv(dev);
  1899. struct timer_list *timer = &tp->timer;
  1900. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  1901. return;
  1902. mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
  1903. }
  1904. #ifdef CONFIG_NET_POLL_CONTROLLER
  1905. /*
  1906. * Polling 'interrupt' - used by things like netconsole to send skbs
  1907. * without having to re-enable interrupts. It's not called while
  1908. * the interrupt routine is executing.
  1909. */
  1910. static void rtl8169_netpoll(struct net_device *dev)
  1911. {
  1912. struct rtl8169_private *tp = netdev_priv(dev);
  1913. struct pci_dev *pdev = tp->pci_dev;
  1914. disable_irq(pdev->irq);
  1915. rtl8169_interrupt(pdev->irq, dev);
  1916. enable_irq(pdev->irq);
  1917. }
  1918. #endif
  1919. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  1920. void __iomem *ioaddr)
  1921. {
  1922. iounmap(ioaddr);
  1923. pci_release_regions(pdev);
  1924. pci_clear_mwi(pdev);
  1925. pci_disable_device(pdev);
  1926. free_netdev(dev);
  1927. }
  1928. static void rtl8169_phy_reset(struct net_device *dev,
  1929. struct rtl8169_private *tp)
  1930. {
  1931. unsigned int i;
  1932. tp->phy_reset_enable(tp);
  1933. for (i = 0; i < 100; i++) {
  1934. if (!tp->phy_reset_pending(tp))
  1935. return;
  1936. msleep(1);
  1937. }
  1938. netif_err(tp, link, dev, "PHY reset failed\n");
  1939. }
  1940. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  1941. {
  1942. void __iomem *ioaddr = tp->mmio_addr;
  1943. rtl_hw_phy_config(dev);
  1944. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  1945. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1946. RTL_W8(0x82, 0x01);
  1947. }
  1948. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  1949. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  1950. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  1951. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  1952. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1953. RTL_W8(0x82, 0x01);
  1954. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  1955. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  1956. }
  1957. rtl8169_phy_reset(dev, tp);
  1958. /*
  1959. * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
  1960. * only 8101. Don't panic.
  1961. */
  1962. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
  1963. if (RTL_R8(PHYstatus) & TBI_Enable)
  1964. netif_info(tp, link, dev, "TBI auto-negotiating\n");
  1965. }
  1966. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  1967. {
  1968. void __iomem *ioaddr = tp->mmio_addr;
  1969. u32 high;
  1970. u32 low;
  1971. low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
  1972. high = addr[4] | (addr[5] << 8);
  1973. spin_lock_irq(&tp->lock);
  1974. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1975. RTL_W32(MAC4, high);
  1976. RTL_R32(MAC4);
  1977. RTL_W32(MAC0, low);
  1978. RTL_R32(MAC0);
  1979. RTL_W8(Cfg9346, Cfg9346_Lock);
  1980. spin_unlock_irq(&tp->lock);
  1981. }
  1982. static int rtl_set_mac_address(struct net_device *dev, void *p)
  1983. {
  1984. struct rtl8169_private *tp = netdev_priv(dev);
  1985. struct sockaddr *addr = p;
  1986. if (!is_valid_ether_addr(addr->sa_data))
  1987. return -EADDRNOTAVAIL;
  1988. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1989. rtl_rar_set(tp, dev->dev_addr);
  1990. return 0;
  1991. }
  1992. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1993. {
  1994. struct rtl8169_private *tp = netdev_priv(dev);
  1995. struct mii_ioctl_data *data = if_mii(ifr);
  1996. return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
  1997. }
  1998. static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  1999. {
  2000. switch (cmd) {
  2001. case SIOCGMIIPHY:
  2002. data->phy_id = 32; /* Internal PHY */
  2003. return 0;
  2004. case SIOCGMIIREG:
  2005. data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
  2006. return 0;
  2007. case SIOCSMIIREG:
  2008. rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
  2009. return 0;
  2010. }
  2011. return -EOPNOTSUPP;
  2012. }
  2013. static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  2014. {
  2015. return -EOPNOTSUPP;
  2016. }
  2017. static const struct rtl_cfg_info {
  2018. void (*hw_start)(struct net_device *);
  2019. unsigned int region;
  2020. unsigned int align;
  2021. u16 intr_event;
  2022. u16 napi_event;
  2023. unsigned features;
  2024. u8 default_ver;
  2025. } rtl_cfg_infos [] = {
  2026. [RTL_CFG_0] = {
  2027. .hw_start = rtl_hw_start_8169,
  2028. .region = 1,
  2029. .align = 0,
  2030. .intr_event = SYSErr | LinkChg | RxOverflow |
  2031. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2032. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2033. .features = RTL_FEATURE_GMII,
  2034. .default_ver = RTL_GIGA_MAC_VER_01,
  2035. },
  2036. [RTL_CFG_1] = {
  2037. .hw_start = rtl_hw_start_8168,
  2038. .region = 2,
  2039. .align = 8,
  2040. .intr_event = SYSErr | LinkChg | RxOverflow |
  2041. TxErr | TxOK | RxOK | RxErr,
  2042. .napi_event = TxErr | TxOK | RxOK | RxOverflow,
  2043. .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
  2044. .default_ver = RTL_GIGA_MAC_VER_11,
  2045. },
  2046. [RTL_CFG_2] = {
  2047. .hw_start = rtl_hw_start_8101,
  2048. .region = 2,
  2049. .align = 8,
  2050. .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
  2051. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2052. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2053. .features = RTL_FEATURE_MSI,
  2054. .default_ver = RTL_GIGA_MAC_VER_13,
  2055. }
  2056. };
  2057. /* Cfg9346_Unlock assumed. */
  2058. static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
  2059. const struct rtl_cfg_info *cfg)
  2060. {
  2061. unsigned msi = 0;
  2062. u8 cfg2;
  2063. cfg2 = RTL_R8(Config2) & ~MSIEnable;
  2064. if (cfg->features & RTL_FEATURE_MSI) {
  2065. if (pci_enable_msi(pdev)) {
  2066. dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
  2067. } else {
  2068. cfg2 |= MSIEnable;
  2069. msi = RTL_FEATURE_MSI;
  2070. }
  2071. }
  2072. RTL_W8(Config2, cfg2);
  2073. return msi;
  2074. }
  2075. static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
  2076. {
  2077. if (tp->features & RTL_FEATURE_MSI) {
  2078. pci_disable_msi(pdev);
  2079. tp->features &= ~RTL_FEATURE_MSI;
  2080. }
  2081. }
  2082. static const struct net_device_ops rtl8169_netdev_ops = {
  2083. .ndo_open = rtl8169_open,
  2084. .ndo_stop = rtl8169_close,
  2085. .ndo_get_stats = rtl8169_get_stats,
  2086. .ndo_start_xmit = rtl8169_start_xmit,
  2087. .ndo_tx_timeout = rtl8169_tx_timeout,
  2088. .ndo_validate_addr = eth_validate_addr,
  2089. .ndo_change_mtu = rtl8169_change_mtu,
  2090. .ndo_set_mac_address = rtl_set_mac_address,
  2091. .ndo_do_ioctl = rtl8169_ioctl,
  2092. .ndo_set_multicast_list = rtl_set_rx_mode,
  2093. #ifdef CONFIG_R8169_VLAN
  2094. .ndo_vlan_rx_register = rtl8169_vlan_rx_register,
  2095. #endif
  2096. #ifdef CONFIG_NET_POLL_CONTROLLER
  2097. .ndo_poll_controller = rtl8169_netpoll,
  2098. #endif
  2099. };
  2100. static int __devinit
  2101. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  2102. {
  2103. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  2104. const unsigned int region = cfg->region;
  2105. struct rtl8169_private *tp;
  2106. struct mii_if_info *mii;
  2107. struct net_device *dev;
  2108. void __iomem *ioaddr;
  2109. unsigned int i;
  2110. int rc;
  2111. if (netif_msg_drv(&debug)) {
  2112. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  2113. MODULENAME, RTL8169_VERSION);
  2114. }
  2115. dev = alloc_etherdev(sizeof (*tp));
  2116. if (!dev) {
  2117. if (netif_msg_drv(&debug))
  2118. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  2119. rc = -ENOMEM;
  2120. goto out;
  2121. }
  2122. SET_NETDEV_DEV(dev, &pdev->dev);
  2123. dev->netdev_ops = &rtl8169_netdev_ops;
  2124. tp = netdev_priv(dev);
  2125. tp->dev = dev;
  2126. tp->pci_dev = pdev;
  2127. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  2128. mii = &tp->mii;
  2129. mii->dev = dev;
  2130. mii->mdio_read = rtl_mdio_read;
  2131. mii->mdio_write = rtl_mdio_write;
  2132. mii->phy_id_mask = 0x1f;
  2133. mii->reg_num_mask = 0x1f;
  2134. mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
  2135. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  2136. rc = pci_enable_device(pdev);
  2137. if (rc < 0) {
  2138. netif_err(tp, probe, dev, "enable failure\n");
  2139. goto err_out_free_dev_1;
  2140. }
  2141. if (pci_set_mwi(pdev) < 0)
  2142. netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
  2143. /* make sure PCI base addr 1 is MMIO */
  2144. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  2145. netif_err(tp, probe, dev,
  2146. "region #%d not an MMIO resource, aborting\n",
  2147. region);
  2148. rc = -ENODEV;
  2149. goto err_out_mwi_2;
  2150. }
  2151. /* check for weird/broken PCI region reporting */
  2152. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  2153. netif_err(tp, probe, dev,
  2154. "Invalid PCI region size(s), aborting\n");
  2155. rc = -ENODEV;
  2156. goto err_out_mwi_2;
  2157. }
  2158. rc = pci_request_regions(pdev, MODULENAME);
  2159. if (rc < 0) {
  2160. netif_err(tp, probe, dev, "could not request regions\n");
  2161. goto err_out_mwi_2;
  2162. }
  2163. tp->cp_cmd = PCIMulRW | RxChkSum;
  2164. if ((sizeof(dma_addr_t) > 4) &&
  2165. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
  2166. tp->cp_cmd |= PCIDAC;
  2167. dev->features |= NETIF_F_HIGHDMA;
  2168. } else {
  2169. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2170. if (rc < 0) {
  2171. netif_err(tp, probe, dev, "DMA configuration failed\n");
  2172. goto err_out_free_res_3;
  2173. }
  2174. }
  2175. /* ioremap MMIO region */
  2176. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  2177. if (!ioaddr) {
  2178. netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
  2179. rc = -EIO;
  2180. goto err_out_free_res_3;
  2181. }
  2182. tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  2183. if (!tp->pcie_cap)
  2184. netif_info(tp, probe, dev, "no PCI Express capability\n");
  2185. RTL_W16(IntrMask, 0x0000);
  2186. /* Soft reset the chip. */
  2187. RTL_W8(ChipCmd, CmdReset);
  2188. /* Check that the chip has finished the reset. */
  2189. for (i = 0; i < 100; i++) {
  2190. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  2191. break;
  2192. msleep_interruptible(1);
  2193. }
  2194. RTL_W16(IntrStatus, 0xffff);
  2195. pci_set_master(pdev);
  2196. /* Identify chip attached to board */
  2197. rtl8169_get_mac_version(tp, ioaddr);
  2198. /* Use appropriate default if unknown */
  2199. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  2200. netif_notice(tp, probe, dev,
  2201. "unknown MAC, using family default\n");
  2202. tp->mac_version = cfg->default_ver;
  2203. }
  2204. rtl8169_print_mac_version(tp);
  2205. for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
  2206. if (tp->mac_version == rtl_chip_info[i].mac_version)
  2207. break;
  2208. }
  2209. if (i == ARRAY_SIZE(rtl_chip_info)) {
  2210. dev_err(&pdev->dev,
  2211. "driver bug, MAC version not found in rtl_chip_info\n");
  2212. goto err_out_msi_4;
  2213. }
  2214. tp->chipset = i;
  2215. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2216. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  2217. RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
  2218. if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
  2219. tp->features |= RTL_FEATURE_WOL;
  2220. if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
  2221. tp->features |= RTL_FEATURE_WOL;
  2222. tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
  2223. RTL_W8(Cfg9346, Cfg9346_Lock);
  2224. if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
  2225. (RTL_R8(PHYstatus) & TBI_Enable)) {
  2226. tp->set_speed = rtl8169_set_speed_tbi;
  2227. tp->get_settings = rtl8169_gset_tbi;
  2228. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  2229. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  2230. tp->link_ok = rtl8169_tbi_link_ok;
  2231. tp->do_ioctl = rtl_tbi_ioctl;
  2232. tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
  2233. } else {
  2234. tp->set_speed = rtl8169_set_speed_xmii;
  2235. tp->get_settings = rtl8169_gset_xmii;
  2236. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  2237. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  2238. tp->link_ok = rtl8169_xmii_link_ok;
  2239. tp->do_ioctl = rtl_xmii_ioctl;
  2240. }
  2241. spin_lock_init(&tp->lock);
  2242. tp->mmio_addr = ioaddr;
  2243. /* Get MAC address */
  2244. for (i = 0; i < MAC_ADDR_LEN; i++)
  2245. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  2246. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  2247. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  2248. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  2249. dev->irq = pdev->irq;
  2250. dev->base_addr = (unsigned long) ioaddr;
  2251. netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
  2252. #ifdef CONFIG_R8169_VLAN
  2253. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  2254. #endif
  2255. dev->features |= NETIF_F_GRO;
  2256. tp->intr_mask = 0xffff;
  2257. tp->hw_start = cfg->hw_start;
  2258. tp->intr_event = cfg->intr_event;
  2259. tp->napi_event = cfg->napi_event;
  2260. init_timer(&tp->timer);
  2261. tp->timer.data = (unsigned long) dev;
  2262. tp->timer.function = rtl8169_phy_timer;
  2263. rc = register_netdev(dev);
  2264. if (rc < 0)
  2265. goto err_out_msi_4;
  2266. pci_set_drvdata(pdev, dev);
  2267. netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
  2268. rtl_chip_info[tp->chipset].name,
  2269. dev->base_addr, dev->dev_addr,
  2270. (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
  2271. rtl8169_init_phy(dev, tp);
  2272. /*
  2273. * Pretend we are using VLANs; This bypasses a nasty bug where
  2274. * Interrupts stop flowing on high load on 8110SCd controllers.
  2275. */
  2276. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  2277. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | RxVlan);
  2278. device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
  2279. if (pci_dev_run_wake(pdev))
  2280. pm_runtime_put_noidle(&pdev->dev);
  2281. out:
  2282. return rc;
  2283. err_out_msi_4:
  2284. rtl_disable_msi(pdev, tp);
  2285. iounmap(ioaddr);
  2286. err_out_free_res_3:
  2287. pci_release_regions(pdev);
  2288. err_out_mwi_2:
  2289. pci_clear_mwi(pdev);
  2290. pci_disable_device(pdev);
  2291. err_out_free_dev_1:
  2292. free_netdev(dev);
  2293. goto out;
  2294. }
  2295. static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
  2296. {
  2297. struct net_device *dev = pci_get_drvdata(pdev);
  2298. struct rtl8169_private *tp = netdev_priv(dev);
  2299. cancel_delayed_work_sync(&tp->task);
  2300. unregister_netdev(dev);
  2301. if (pci_dev_run_wake(pdev))
  2302. pm_runtime_get_noresume(&pdev->dev);
  2303. /* restore original MAC address */
  2304. rtl_rar_set(tp, dev->perm_addr);
  2305. rtl_disable_msi(pdev, tp);
  2306. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  2307. pci_set_drvdata(pdev, NULL);
  2308. }
  2309. static int rtl8169_open(struct net_device *dev)
  2310. {
  2311. struct rtl8169_private *tp = netdev_priv(dev);
  2312. struct pci_dev *pdev = tp->pci_dev;
  2313. int retval = -ENOMEM;
  2314. pm_runtime_get_sync(&pdev->dev);
  2315. /*
  2316. * Rx and Tx desscriptors needs 256 bytes alignment.
  2317. * dma_alloc_coherent provides more.
  2318. */
  2319. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  2320. &tp->TxPhyAddr, GFP_KERNEL);
  2321. if (!tp->TxDescArray)
  2322. goto err_pm_runtime_put;
  2323. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  2324. &tp->RxPhyAddr, GFP_KERNEL);
  2325. if (!tp->RxDescArray)
  2326. goto err_free_tx_0;
  2327. retval = rtl8169_init_ring(dev);
  2328. if (retval < 0)
  2329. goto err_free_rx_1;
  2330. INIT_DELAYED_WORK(&tp->task, NULL);
  2331. smp_mb();
  2332. retval = request_irq(dev->irq, rtl8169_interrupt,
  2333. (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
  2334. dev->name, dev);
  2335. if (retval < 0)
  2336. goto err_release_ring_2;
  2337. napi_enable(&tp->napi);
  2338. rtl_hw_start(dev);
  2339. rtl8169_request_timer(dev);
  2340. tp->saved_wolopts = 0;
  2341. pm_runtime_put_noidle(&pdev->dev);
  2342. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  2343. out:
  2344. return retval;
  2345. err_release_ring_2:
  2346. rtl8169_rx_clear(tp);
  2347. err_free_rx_1:
  2348. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  2349. tp->RxPhyAddr);
  2350. tp->RxDescArray = NULL;
  2351. err_free_tx_0:
  2352. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  2353. tp->TxPhyAddr);
  2354. tp->TxDescArray = NULL;
  2355. err_pm_runtime_put:
  2356. pm_runtime_put_noidle(&pdev->dev);
  2357. goto out;
  2358. }
  2359. static void rtl8169_hw_reset(void __iomem *ioaddr)
  2360. {
  2361. /* Disable interrupts */
  2362. rtl8169_irq_mask_and_ack(ioaddr);
  2363. /* Reset the chipset */
  2364. RTL_W8(ChipCmd, CmdReset);
  2365. /* PCI commit */
  2366. RTL_R8(ChipCmd);
  2367. }
  2368. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  2369. {
  2370. void __iomem *ioaddr = tp->mmio_addr;
  2371. u32 cfg = rtl8169_rx_config;
  2372. cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  2373. RTL_W32(RxConfig, cfg);
  2374. /* Set DMA burst size and Interframe Gap Time */
  2375. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  2376. (InterFrameGap << TxInterFrameGapShift));
  2377. }
  2378. static void rtl_hw_start(struct net_device *dev)
  2379. {
  2380. struct rtl8169_private *tp = netdev_priv(dev);
  2381. void __iomem *ioaddr = tp->mmio_addr;
  2382. unsigned int i;
  2383. /* Soft reset the chip. */
  2384. RTL_W8(ChipCmd, CmdReset);
  2385. /* Check that the chip has finished the reset. */
  2386. for (i = 0; i < 100; i++) {
  2387. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  2388. break;
  2389. msleep_interruptible(1);
  2390. }
  2391. tp->hw_start(dev);
  2392. netif_start_queue(dev);
  2393. }
  2394. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  2395. void __iomem *ioaddr)
  2396. {
  2397. /*
  2398. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  2399. * register to be written before TxDescAddrLow to work.
  2400. * Switching from MMIO to I/O access fixes the issue as well.
  2401. */
  2402. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  2403. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  2404. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  2405. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  2406. }
  2407. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  2408. {
  2409. u16 cmd;
  2410. cmd = RTL_R16(CPlusCmd);
  2411. RTL_W16(CPlusCmd, cmd);
  2412. return cmd;
  2413. }
  2414. static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
  2415. {
  2416. /* Low hurts. Let's disable the filtering. */
  2417. RTL_W16(RxMaxSize, rx_buf_sz + 1);
  2418. }
  2419. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  2420. {
  2421. static const struct {
  2422. u32 mac_version;
  2423. u32 clk;
  2424. u32 val;
  2425. } cfg2_info [] = {
  2426. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  2427. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  2428. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  2429. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  2430. }, *p = cfg2_info;
  2431. unsigned int i;
  2432. u32 clk;
  2433. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  2434. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  2435. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  2436. RTL_W32(0x7c, p->val);
  2437. break;
  2438. }
  2439. }
  2440. }
  2441. static void rtl_hw_start_8169(struct net_device *dev)
  2442. {
  2443. struct rtl8169_private *tp = netdev_priv(dev);
  2444. void __iomem *ioaddr = tp->mmio_addr;
  2445. struct pci_dev *pdev = tp->pci_dev;
  2446. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  2447. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  2448. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  2449. }
  2450. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2451. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  2452. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2453. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  2454. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  2455. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2456. RTL_W8(EarlyTxThres, NoEarlyTx);
  2457. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  2458. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  2459. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2460. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  2461. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  2462. rtl_set_rx_tx_config_registers(tp);
  2463. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  2464. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2465. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  2466. dprintk("Set MAC Reg C+CR Offset 0xE0. "
  2467. "Bit-3 and bit-14 MUST be 1\n");
  2468. tp->cp_cmd |= (1 << 14);
  2469. }
  2470. RTL_W16(CPlusCmd, tp->cp_cmd);
  2471. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  2472. /*
  2473. * Undocumented corner. Supposedly:
  2474. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  2475. */
  2476. RTL_W16(IntrMitigate, 0x0000);
  2477. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2478. if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
  2479. (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
  2480. (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
  2481. (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
  2482. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2483. rtl_set_rx_tx_config_registers(tp);
  2484. }
  2485. RTL_W8(Cfg9346, Cfg9346_Lock);
  2486. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  2487. RTL_R8(IntrMask);
  2488. RTL_W32(RxMissed, 0);
  2489. rtl_set_rx_mode(dev);
  2490. /* no early-rx interrupts */
  2491. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  2492. /* Enable all known interrupts by setting the interrupt mask. */
  2493. RTL_W16(IntrMask, tp->intr_event);
  2494. }
  2495. static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
  2496. {
  2497. struct net_device *dev = pci_get_drvdata(pdev);
  2498. struct rtl8169_private *tp = netdev_priv(dev);
  2499. int cap = tp->pcie_cap;
  2500. if (cap) {
  2501. u16 ctl;
  2502. pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
  2503. ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
  2504. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
  2505. }
  2506. }
  2507. static void rtl_csi_access_enable(void __iomem *ioaddr)
  2508. {
  2509. u32 csi;
  2510. csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
  2511. rtl_csi_write(ioaddr, 0x070c, csi | 0x27000000);
  2512. }
  2513. struct ephy_info {
  2514. unsigned int offset;
  2515. u16 mask;
  2516. u16 bits;
  2517. };
  2518. static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
  2519. {
  2520. u16 w;
  2521. while (len-- > 0) {
  2522. w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
  2523. rtl_ephy_write(ioaddr, e->offset, w);
  2524. e++;
  2525. }
  2526. }
  2527. static void rtl_disable_clock_request(struct pci_dev *pdev)
  2528. {
  2529. struct net_device *dev = pci_get_drvdata(pdev);
  2530. struct rtl8169_private *tp = netdev_priv(dev);
  2531. int cap = tp->pcie_cap;
  2532. if (cap) {
  2533. u16 ctl;
  2534. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  2535. ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  2536. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  2537. }
  2538. }
  2539. #define R8168_CPCMD_QUIRK_MASK (\
  2540. EnableBist | \
  2541. Mac_dbgo_oe | \
  2542. Force_half_dup | \
  2543. Force_rxflow_en | \
  2544. Force_txflow_en | \
  2545. Cxpl_dbg_sel | \
  2546. ASF | \
  2547. PktCntrDisable | \
  2548. Mac_dbgo_sel)
  2549. static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
  2550. {
  2551. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2552. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2553. rtl_tx_performance_tweak(pdev,
  2554. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  2555. }
  2556. static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
  2557. {
  2558. rtl_hw_start_8168bb(ioaddr, pdev);
  2559. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2560. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  2561. }
  2562. static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
  2563. {
  2564. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  2565. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2566. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2567. rtl_disable_clock_request(pdev);
  2568. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2569. }
  2570. static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
  2571. {
  2572. static const struct ephy_info e_info_8168cp[] = {
  2573. { 0x01, 0, 0x0001 },
  2574. { 0x02, 0x0800, 0x1000 },
  2575. { 0x03, 0, 0x0042 },
  2576. { 0x06, 0x0080, 0x0000 },
  2577. { 0x07, 0, 0x2000 }
  2578. };
  2579. rtl_csi_access_enable(ioaddr);
  2580. rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  2581. __rtl_hw_start_8168cp(ioaddr, pdev);
  2582. }
  2583. static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
  2584. {
  2585. rtl_csi_access_enable(ioaddr);
  2586. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2587. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2588. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2589. }
  2590. static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
  2591. {
  2592. rtl_csi_access_enable(ioaddr);
  2593. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2594. /* Magic. */
  2595. RTL_W8(DBG_REG, 0x20);
  2596. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2597. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2598. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2599. }
  2600. static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
  2601. {
  2602. static const struct ephy_info e_info_8168c_1[] = {
  2603. { 0x02, 0x0800, 0x1000 },
  2604. { 0x03, 0, 0x0002 },
  2605. { 0x06, 0x0080, 0x0000 }
  2606. };
  2607. rtl_csi_access_enable(ioaddr);
  2608. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  2609. rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  2610. __rtl_hw_start_8168cp(ioaddr, pdev);
  2611. }
  2612. static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
  2613. {
  2614. static const struct ephy_info e_info_8168c_2[] = {
  2615. { 0x01, 0, 0x0001 },
  2616. { 0x03, 0x0400, 0x0220 }
  2617. };
  2618. rtl_csi_access_enable(ioaddr);
  2619. rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  2620. __rtl_hw_start_8168cp(ioaddr, pdev);
  2621. }
  2622. static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
  2623. {
  2624. rtl_hw_start_8168c_2(ioaddr, pdev);
  2625. }
  2626. static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
  2627. {
  2628. rtl_csi_access_enable(ioaddr);
  2629. __rtl_hw_start_8168cp(ioaddr, pdev);
  2630. }
  2631. static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
  2632. {
  2633. rtl_csi_access_enable(ioaddr);
  2634. rtl_disable_clock_request(pdev);
  2635. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2636. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2637. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  2638. }
  2639. static void rtl_hw_start_8168(struct net_device *dev)
  2640. {
  2641. struct rtl8169_private *tp = netdev_priv(dev);
  2642. void __iomem *ioaddr = tp->mmio_addr;
  2643. struct pci_dev *pdev = tp->pci_dev;
  2644. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2645. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2646. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  2647. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  2648. RTL_W16(CPlusCmd, tp->cp_cmd);
  2649. RTL_W16(IntrMitigate, 0x5151);
  2650. /* Work around for RxFIFO overflow. */
  2651. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  2652. tp->intr_event |= RxFIFOOver | PCSTimeout;
  2653. tp->intr_event &= ~RxOverflow;
  2654. }
  2655. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2656. rtl_set_rx_mode(dev);
  2657. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  2658. (InterFrameGap << TxInterFrameGapShift));
  2659. RTL_R8(IntrMask);
  2660. switch (tp->mac_version) {
  2661. case RTL_GIGA_MAC_VER_11:
  2662. rtl_hw_start_8168bb(ioaddr, pdev);
  2663. break;
  2664. case RTL_GIGA_MAC_VER_12:
  2665. case RTL_GIGA_MAC_VER_17:
  2666. rtl_hw_start_8168bef(ioaddr, pdev);
  2667. break;
  2668. case RTL_GIGA_MAC_VER_18:
  2669. rtl_hw_start_8168cp_1(ioaddr, pdev);
  2670. break;
  2671. case RTL_GIGA_MAC_VER_19:
  2672. rtl_hw_start_8168c_1(ioaddr, pdev);
  2673. break;
  2674. case RTL_GIGA_MAC_VER_20:
  2675. rtl_hw_start_8168c_2(ioaddr, pdev);
  2676. break;
  2677. case RTL_GIGA_MAC_VER_21:
  2678. rtl_hw_start_8168c_3(ioaddr, pdev);
  2679. break;
  2680. case RTL_GIGA_MAC_VER_22:
  2681. rtl_hw_start_8168c_4(ioaddr, pdev);
  2682. break;
  2683. case RTL_GIGA_MAC_VER_23:
  2684. rtl_hw_start_8168cp_2(ioaddr, pdev);
  2685. break;
  2686. case RTL_GIGA_MAC_VER_24:
  2687. rtl_hw_start_8168cp_3(ioaddr, pdev);
  2688. break;
  2689. case RTL_GIGA_MAC_VER_25:
  2690. case RTL_GIGA_MAC_VER_26:
  2691. case RTL_GIGA_MAC_VER_27:
  2692. rtl_hw_start_8168d(ioaddr, pdev);
  2693. break;
  2694. default:
  2695. printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
  2696. dev->name, tp->mac_version);
  2697. break;
  2698. }
  2699. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2700. RTL_W8(Cfg9346, Cfg9346_Lock);
  2701. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  2702. RTL_W16(IntrMask, tp->intr_event);
  2703. }
  2704. #define R810X_CPCMD_QUIRK_MASK (\
  2705. EnableBist | \
  2706. Mac_dbgo_oe | \
  2707. Force_half_dup | \
  2708. Force_rxflow_en | \
  2709. Force_txflow_en | \
  2710. Cxpl_dbg_sel | \
  2711. ASF | \
  2712. PktCntrDisable | \
  2713. PCIDAC | \
  2714. PCIMulRW)
  2715. static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  2716. {
  2717. static const struct ephy_info e_info_8102e_1[] = {
  2718. { 0x01, 0, 0x6e65 },
  2719. { 0x02, 0, 0x091f },
  2720. { 0x03, 0, 0xc2f9 },
  2721. { 0x06, 0, 0xafb5 },
  2722. { 0x07, 0, 0x0e00 },
  2723. { 0x19, 0, 0xec80 },
  2724. { 0x01, 0, 0x2e65 },
  2725. { 0x01, 0, 0x6e65 }
  2726. };
  2727. u8 cfg1;
  2728. rtl_csi_access_enable(ioaddr);
  2729. RTL_W8(DBG_REG, FIX_NAK_1);
  2730. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2731. RTL_W8(Config1,
  2732. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  2733. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2734. cfg1 = RTL_R8(Config1);
  2735. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  2736. RTL_W8(Config1, cfg1 & ~LEDS0);
  2737. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
  2738. rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  2739. }
  2740. static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  2741. {
  2742. rtl_csi_access_enable(ioaddr);
  2743. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  2744. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  2745. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  2746. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
  2747. }
  2748. static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
  2749. {
  2750. rtl_hw_start_8102e_2(ioaddr, pdev);
  2751. rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
  2752. }
  2753. static void rtl_hw_start_8101(struct net_device *dev)
  2754. {
  2755. struct rtl8169_private *tp = netdev_priv(dev);
  2756. void __iomem *ioaddr = tp->mmio_addr;
  2757. struct pci_dev *pdev = tp->pci_dev;
  2758. if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  2759. (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
  2760. int cap = tp->pcie_cap;
  2761. if (cap) {
  2762. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
  2763. PCI_EXP_DEVCTL_NOSNOOP_EN);
  2764. }
  2765. }
  2766. switch (tp->mac_version) {
  2767. case RTL_GIGA_MAC_VER_07:
  2768. rtl_hw_start_8102e_1(ioaddr, pdev);
  2769. break;
  2770. case RTL_GIGA_MAC_VER_08:
  2771. rtl_hw_start_8102e_3(ioaddr, pdev);
  2772. break;
  2773. case RTL_GIGA_MAC_VER_09:
  2774. rtl_hw_start_8102e_2(ioaddr, pdev);
  2775. break;
  2776. }
  2777. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2778. RTL_W8(MaxTxPacketSize, TxPacketMax);
  2779. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  2780. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  2781. RTL_W16(CPlusCmd, tp->cp_cmd);
  2782. RTL_W16(IntrMitigate, 0x0000);
  2783. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2784. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2785. rtl_set_rx_tx_config_registers(tp);
  2786. RTL_W8(Cfg9346, Cfg9346_Lock);
  2787. RTL_R8(IntrMask);
  2788. rtl_set_rx_mode(dev);
  2789. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2790. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  2791. RTL_W16(IntrMask, tp->intr_event);
  2792. }
  2793. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  2794. {
  2795. if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
  2796. return -EINVAL;
  2797. dev->mtu = new_mtu;
  2798. return 0;
  2799. }
  2800. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  2801. {
  2802. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  2803. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  2804. }
  2805. static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
  2806. void **data_buff, struct RxDesc *desc)
  2807. {
  2808. dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
  2809. DMA_FROM_DEVICE);
  2810. kfree(*data_buff);
  2811. *data_buff = NULL;
  2812. rtl8169_make_unusable_by_asic(desc);
  2813. }
  2814. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  2815. {
  2816. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  2817. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  2818. }
  2819. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  2820. u32 rx_buf_sz)
  2821. {
  2822. desc->addr = cpu_to_le64(mapping);
  2823. wmb();
  2824. rtl8169_mark_to_asic(desc, rx_buf_sz);
  2825. }
  2826. static inline void *rtl8169_align(void *data)
  2827. {
  2828. return (void *)ALIGN((long)data, 16);
  2829. }
  2830. static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  2831. struct RxDesc *desc)
  2832. {
  2833. void *data;
  2834. dma_addr_t mapping;
  2835. struct device *d = &tp->pci_dev->dev;
  2836. struct net_device *dev = tp->dev;
  2837. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  2838. data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  2839. if (!data)
  2840. return NULL;
  2841. if (rtl8169_align(data) != data) {
  2842. kfree(data);
  2843. data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
  2844. if (!data)
  2845. return NULL;
  2846. }
  2847. mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
  2848. DMA_FROM_DEVICE);
  2849. if (unlikely(dma_mapping_error(d, mapping))) {
  2850. if (net_ratelimit())
  2851. netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
  2852. goto err_out;
  2853. }
  2854. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  2855. return data;
  2856. err_out:
  2857. kfree(data);
  2858. return NULL;
  2859. }
  2860. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  2861. {
  2862. unsigned int i;
  2863. for (i = 0; i < NUM_RX_DESC; i++) {
  2864. if (tp->Rx_databuff[i]) {
  2865. rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
  2866. tp->RxDescArray + i);
  2867. }
  2868. }
  2869. }
  2870. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  2871. {
  2872. desc->opts1 |= cpu_to_le32(RingEnd);
  2873. }
  2874. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  2875. {
  2876. unsigned int i;
  2877. for (i = 0; i < NUM_RX_DESC; i++) {
  2878. void *data;
  2879. if (tp->Rx_databuff[i])
  2880. continue;
  2881. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  2882. if (!data) {
  2883. rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
  2884. goto err_out;
  2885. }
  2886. tp->Rx_databuff[i] = data;
  2887. }
  2888. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  2889. return 0;
  2890. err_out:
  2891. rtl8169_rx_clear(tp);
  2892. return -ENOMEM;
  2893. }
  2894. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  2895. {
  2896. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  2897. }
  2898. static int rtl8169_init_ring(struct net_device *dev)
  2899. {
  2900. struct rtl8169_private *tp = netdev_priv(dev);
  2901. rtl8169_init_ring_indexes(tp);
  2902. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  2903. memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
  2904. return rtl8169_rx_fill(tp);
  2905. }
  2906. static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
  2907. struct TxDesc *desc)
  2908. {
  2909. unsigned int len = tx_skb->len;
  2910. dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
  2911. desc->opts1 = 0x00;
  2912. desc->opts2 = 0x00;
  2913. desc->addr = 0x00;
  2914. tx_skb->len = 0;
  2915. }
  2916. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  2917. unsigned int n)
  2918. {
  2919. unsigned int i;
  2920. for (i = 0; i < n; i++) {
  2921. unsigned int entry = (start + i) % NUM_TX_DESC;
  2922. struct ring_info *tx_skb = tp->tx_skb + entry;
  2923. unsigned int len = tx_skb->len;
  2924. if (len) {
  2925. struct sk_buff *skb = tx_skb->skb;
  2926. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  2927. tp->TxDescArray + entry);
  2928. if (skb) {
  2929. tp->dev->stats.tx_dropped++;
  2930. dev_kfree_skb(skb);
  2931. tx_skb->skb = NULL;
  2932. }
  2933. }
  2934. }
  2935. }
  2936. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  2937. {
  2938. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  2939. tp->cur_tx = tp->dirty_tx = 0;
  2940. }
  2941. static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
  2942. {
  2943. struct rtl8169_private *tp = netdev_priv(dev);
  2944. PREPARE_DELAYED_WORK(&tp->task, task);
  2945. schedule_delayed_work(&tp->task, 4);
  2946. }
  2947. static void rtl8169_wait_for_quiescence(struct net_device *dev)
  2948. {
  2949. struct rtl8169_private *tp = netdev_priv(dev);
  2950. void __iomem *ioaddr = tp->mmio_addr;
  2951. synchronize_irq(dev->irq);
  2952. /* Wait for any pending NAPI task to complete */
  2953. napi_disable(&tp->napi);
  2954. rtl8169_irq_mask_and_ack(ioaddr);
  2955. tp->intr_mask = 0xffff;
  2956. RTL_W16(IntrMask, tp->intr_event);
  2957. napi_enable(&tp->napi);
  2958. }
  2959. static void rtl8169_reinit_task(struct work_struct *work)
  2960. {
  2961. struct rtl8169_private *tp =
  2962. container_of(work, struct rtl8169_private, task.work);
  2963. struct net_device *dev = tp->dev;
  2964. int ret;
  2965. rtnl_lock();
  2966. if (!netif_running(dev))
  2967. goto out_unlock;
  2968. rtl8169_wait_for_quiescence(dev);
  2969. rtl8169_close(dev);
  2970. ret = rtl8169_open(dev);
  2971. if (unlikely(ret < 0)) {
  2972. if (net_ratelimit())
  2973. netif_err(tp, drv, dev,
  2974. "reinit failure (status = %d). Rescheduling\n",
  2975. ret);
  2976. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  2977. }
  2978. out_unlock:
  2979. rtnl_unlock();
  2980. }
  2981. static void rtl8169_reset_task(struct work_struct *work)
  2982. {
  2983. struct rtl8169_private *tp =
  2984. container_of(work, struct rtl8169_private, task.work);
  2985. struct net_device *dev = tp->dev;
  2986. rtnl_lock();
  2987. if (!netif_running(dev))
  2988. goto out_unlock;
  2989. rtl8169_wait_for_quiescence(dev);
  2990. rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
  2991. rtl8169_tx_clear(tp);
  2992. if (tp->dirty_rx == tp->cur_rx) {
  2993. rtl8169_init_ring_indexes(tp);
  2994. rtl_hw_start(dev);
  2995. netif_wake_queue(dev);
  2996. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  2997. } else {
  2998. if (net_ratelimit())
  2999. netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
  3000. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3001. }
  3002. out_unlock:
  3003. rtnl_unlock();
  3004. }
  3005. static void rtl8169_tx_timeout(struct net_device *dev)
  3006. {
  3007. struct rtl8169_private *tp = netdev_priv(dev);
  3008. rtl8169_hw_reset(tp->mmio_addr);
  3009. /* Let's wait a bit while any (async) irq lands on */
  3010. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3011. }
  3012. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  3013. u32 opts1)
  3014. {
  3015. struct skb_shared_info *info = skb_shinfo(skb);
  3016. unsigned int cur_frag, entry;
  3017. struct TxDesc * uninitialized_var(txd);
  3018. struct device *d = &tp->pci_dev->dev;
  3019. entry = tp->cur_tx;
  3020. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  3021. skb_frag_t *frag = info->frags + cur_frag;
  3022. dma_addr_t mapping;
  3023. u32 status, len;
  3024. void *addr;
  3025. entry = (entry + 1) % NUM_TX_DESC;
  3026. txd = tp->TxDescArray + entry;
  3027. len = frag->size;
  3028. addr = ((void *) page_address(frag->page)) + frag->page_offset;
  3029. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  3030. if (unlikely(dma_mapping_error(d, mapping))) {
  3031. if (net_ratelimit())
  3032. netif_err(tp, drv, tp->dev,
  3033. "Failed to map TX fragments DMA!\n");
  3034. goto err_out;
  3035. }
  3036. /* anti gcc 2.95.3 bugware (sic) */
  3037. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  3038. txd->opts1 = cpu_to_le32(status);
  3039. txd->addr = cpu_to_le64(mapping);
  3040. tp->tx_skb[entry].len = len;
  3041. }
  3042. if (cur_frag) {
  3043. tp->tx_skb[entry].skb = skb;
  3044. txd->opts1 |= cpu_to_le32(LastFrag);
  3045. }
  3046. return cur_frag;
  3047. err_out:
  3048. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  3049. return -EIO;
  3050. }
  3051. static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
  3052. {
  3053. if (dev->features & NETIF_F_TSO) {
  3054. u32 mss = skb_shinfo(skb)->gso_size;
  3055. if (mss)
  3056. return LargeSend | ((mss & MSSMask) << MSSShift);
  3057. }
  3058. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3059. const struct iphdr *ip = ip_hdr(skb);
  3060. if (ip->protocol == IPPROTO_TCP)
  3061. return IPCS | TCPCS;
  3062. else if (ip->protocol == IPPROTO_UDP)
  3063. return IPCS | UDPCS;
  3064. WARN_ON(1); /* we need a WARN() */
  3065. }
  3066. return 0;
  3067. }
  3068. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  3069. struct net_device *dev)
  3070. {
  3071. struct rtl8169_private *tp = netdev_priv(dev);
  3072. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  3073. struct TxDesc *txd = tp->TxDescArray + entry;
  3074. void __iomem *ioaddr = tp->mmio_addr;
  3075. struct device *d = &tp->pci_dev->dev;
  3076. dma_addr_t mapping;
  3077. u32 status, len;
  3078. u32 opts1;
  3079. int frags;
  3080. if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
  3081. netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
  3082. goto err_stop_0;
  3083. }
  3084. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  3085. goto err_stop_0;
  3086. len = skb_headlen(skb);
  3087. mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
  3088. if (unlikely(dma_mapping_error(d, mapping))) {
  3089. if (net_ratelimit())
  3090. netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
  3091. goto err_dma_0;
  3092. }
  3093. tp->tx_skb[entry].len = len;
  3094. txd->addr = cpu_to_le64(mapping);
  3095. txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
  3096. opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
  3097. frags = rtl8169_xmit_frags(tp, skb, opts1);
  3098. if (frags < 0)
  3099. goto err_dma_1;
  3100. else if (frags)
  3101. opts1 |= FirstFrag;
  3102. else {
  3103. opts1 |= FirstFrag | LastFrag;
  3104. tp->tx_skb[entry].skb = skb;
  3105. }
  3106. wmb();
  3107. /* anti gcc 2.95.3 bugware (sic) */
  3108. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  3109. txd->opts1 = cpu_to_le32(status);
  3110. tp->cur_tx += frags + 1;
  3111. wmb();
  3112. RTL_W8(TxPoll, NPQ); /* set polling bit */
  3113. if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
  3114. netif_stop_queue(dev);
  3115. smp_rmb();
  3116. if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
  3117. netif_wake_queue(dev);
  3118. }
  3119. return NETDEV_TX_OK;
  3120. err_dma_1:
  3121. rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
  3122. err_dma_0:
  3123. dev_kfree_skb(skb);
  3124. dev->stats.tx_dropped++;
  3125. return NETDEV_TX_OK;
  3126. err_stop_0:
  3127. netif_stop_queue(dev);
  3128. dev->stats.tx_dropped++;
  3129. return NETDEV_TX_BUSY;
  3130. }
  3131. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  3132. {
  3133. struct rtl8169_private *tp = netdev_priv(dev);
  3134. struct pci_dev *pdev = tp->pci_dev;
  3135. void __iomem *ioaddr = tp->mmio_addr;
  3136. u16 pci_status, pci_cmd;
  3137. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  3138. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  3139. netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
  3140. pci_cmd, pci_status);
  3141. /*
  3142. * The recovery sequence below admits a very elaborated explanation:
  3143. * - it seems to work;
  3144. * - I did not see what else could be done;
  3145. * - it makes iop3xx happy.
  3146. *
  3147. * Feel free to adjust to your needs.
  3148. */
  3149. if (pdev->broken_parity_status)
  3150. pci_cmd &= ~PCI_COMMAND_PARITY;
  3151. else
  3152. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  3153. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  3154. pci_write_config_word(pdev, PCI_STATUS,
  3155. pci_status & (PCI_STATUS_DETECTED_PARITY |
  3156. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  3157. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  3158. /* The infamous DAC f*ckup only happens at boot time */
  3159. if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
  3160. netif_info(tp, intr, dev, "disabling PCI DAC\n");
  3161. tp->cp_cmd &= ~PCIDAC;
  3162. RTL_W16(CPlusCmd, tp->cp_cmd);
  3163. dev->features &= ~NETIF_F_HIGHDMA;
  3164. }
  3165. rtl8169_hw_reset(ioaddr);
  3166. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  3167. }
  3168. static void rtl8169_tx_interrupt(struct net_device *dev,
  3169. struct rtl8169_private *tp,
  3170. void __iomem *ioaddr)
  3171. {
  3172. unsigned int dirty_tx, tx_left;
  3173. dirty_tx = tp->dirty_tx;
  3174. smp_rmb();
  3175. tx_left = tp->cur_tx - dirty_tx;
  3176. while (tx_left > 0) {
  3177. unsigned int entry = dirty_tx % NUM_TX_DESC;
  3178. struct ring_info *tx_skb = tp->tx_skb + entry;
  3179. u32 status;
  3180. rmb();
  3181. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  3182. if (status & DescOwn)
  3183. break;
  3184. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  3185. tp->TxDescArray + entry);
  3186. if (status & LastFrag) {
  3187. dev->stats.tx_packets++;
  3188. dev->stats.tx_bytes += tx_skb->skb->len;
  3189. dev_kfree_skb(tx_skb->skb);
  3190. tx_skb->skb = NULL;
  3191. }
  3192. dirty_tx++;
  3193. tx_left--;
  3194. }
  3195. if (tp->dirty_tx != dirty_tx) {
  3196. tp->dirty_tx = dirty_tx;
  3197. smp_wmb();
  3198. if (netif_queue_stopped(dev) &&
  3199. (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
  3200. netif_wake_queue(dev);
  3201. }
  3202. /*
  3203. * 8168 hack: TxPoll requests are lost when the Tx packets are
  3204. * too close. Let's kick an extra TxPoll request when a burst
  3205. * of start_xmit activity is detected (if it is not detected,
  3206. * it is slow enough). -- FR
  3207. */
  3208. smp_rmb();
  3209. if (tp->cur_tx != dirty_tx)
  3210. RTL_W8(TxPoll, NPQ);
  3211. }
  3212. }
  3213. static inline int rtl8169_fragmented_frame(u32 status)
  3214. {
  3215. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  3216. }
  3217. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  3218. {
  3219. u32 status = opts1 & RxProtoMask;
  3220. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  3221. ((status == RxProtoUDP) && !(opts1 & UDPFail)))
  3222. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3223. else
  3224. skb_checksum_none_assert(skb);
  3225. }
  3226. static struct sk_buff *rtl8169_try_rx_copy(void *data,
  3227. struct rtl8169_private *tp,
  3228. int pkt_size,
  3229. dma_addr_t addr)
  3230. {
  3231. struct sk_buff *skb;
  3232. struct device *d = &tp->pci_dev->dev;
  3233. data = rtl8169_align(data);
  3234. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  3235. prefetch(data);
  3236. skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
  3237. if (skb)
  3238. memcpy(skb->data, data, pkt_size);
  3239. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  3240. return skb;
  3241. }
  3242. /*
  3243. * Warning : rtl8169_rx_interrupt() might be called :
  3244. * 1) from NAPI (softirq) context
  3245. * (polling = 1 : we should call netif_receive_skb())
  3246. * 2) from process context (rtl8169_reset_task())
  3247. * (polling = 0 : we must call netif_rx() instead)
  3248. */
  3249. static int rtl8169_rx_interrupt(struct net_device *dev,
  3250. struct rtl8169_private *tp,
  3251. void __iomem *ioaddr, u32 budget)
  3252. {
  3253. unsigned int cur_rx, rx_left;
  3254. unsigned int count;
  3255. int polling = (budget != ~(u32)0) ? 1 : 0;
  3256. cur_rx = tp->cur_rx;
  3257. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  3258. rx_left = min(rx_left, budget);
  3259. for (; rx_left > 0; rx_left--, cur_rx++) {
  3260. unsigned int entry = cur_rx % NUM_RX_DESC;
  3261. struct RxDesc *desc = tp->RxDescArray + entry;
  3262. u32 status;
  3263. rmb();
  3264. status = le32_to_cpu(desc->opts1);
  3265. if (status & DescOwn)
  3266. break;
  3267. if (unlikely(status & RxRES)) {
  3268. netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
  3269. status);
  3270. dev->stats.rx_errors++;
  3271. if (status & (RxRWT | RxRUNT))
  3272. dev->stats.rx_length_errors++;
  3273. if (status & RxCRC)
  3274. dev->stats.rx_crc_errors++;
  3275. if (status & RxFOVF) {
  3276. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3277. dev->stats.rx_fifo_errors++;
  3278. }
  3279. rtl8169_mark_to_asic(desc, rx_buf_sz);
  3280. } else {
  3281. struct sk_buff *skb;
  3282. dma_addr_t addr = le64_to_cpu(desc->addr);
  3283. int pkt_size = (status & 0x00001FFF) - 4;
  3284. /*
  3285. * The driver does not support incoming fragmented
  3286. * frames. They are seen as a symptom of over-mtu
  3287. * sized frames.
  3288. */
  3289. if (unlikely(rtl8169_fragmented_frame(status))) {
  3290. dev->stats.rx_dropped++;
  3291. dev->stats.rx_length_errors++;
  3292. rtl8169_mark_to_asic(desc, rx_buf_sz);
  3293. continue;
  3294. }
  3295. skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
  3296. tp, pkt_size, addr);
  3297. rtl8169_mark_to_asic(desc, rx_buf_sz);
  3298. if (!skb) {
  3299. dev->stats.rx_dropped++;
  3300. continue;
  3301. }
  3302. rtl8169_rx_csum(skb, status);
  3303. skb_put(skb, pkt_size);
  3304. skb->protocol = eth_type_trans(skb, dev);
  3305. if (rtl8169_rx_vlan_skb(tp, desc, skb, polling) < 0) {
  3306. if (likely(polling))
  3307. napi_gro_receive(&tp->napi, skb);
  3308. else
  3309. netif_rx(skb);
  3310. }
  3311. dev->stats.rx_bytes += pkt_size;
  3312. dev->stats.rx_packets++;
  3313. }
  3314. /* Work around for AMD plateform. */
  3315. if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
  3316. (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
  3317. desc->opts2 = 0;
  3318. cur_rx++;
  3319. }
  3320. }
  3321. count = cur_rx - tp->cur_rx;
  3322. tp->cur_rx = cur_rx;
  3323. tp->dirty_rx += count;
  3324. return count;
  3325. }
  3326. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  3327. {
  3328. struct net_device *dev = dev_instance;
  3329. struct rtl8169_private *tp = netdev_priv(dev);
  3330. void __iomem *ioaddr = tp->mmio_addr;
  3331. int handled = 0;
  3332. int status;
  3333. /* loop handling interrupts until we have no new ones or
  3334. * we hit a invalid/hotplug case.
  3335. */
  3336. status = RTL_R16(IntrStatus);
  3337. while (status && status != 0xffff) {
  3338. handled = 1;
  3339. /* Handle all of the error cases first. These will reset
  3340. * the chip, so just exit the loop.
  3341. */
  3342. if (unlikely(!netif_running(dev))) {
  3343. rtl8169_asic_down(ioaddr);
  3344. break;
  3345. }
  3346. /* Work around for rx fifo overflow */
  3347. if (unlikely(status & RxFIFOOver) &&
  3348. (tp->mac_version == RTL_GIGA_MAC_VER_11)) {
  3349. netif_stop_queue(dev);
  3350. rtl8169_tx_timeout(dev);
  3351. break;
  3352. }
  3353. if (unlikely(status & SYSErr)) {
  3354. rtl8169_pcierr_interrupt(dev);
  3355. break;
  3356. }
  3357. if (status & LinkChg)
  3358. __rtl8169_check_link_status(dev, tp, ioaddr, true);
  3359. /* We need to see the lastest version of tp->intr_mask to
  3360. * avoid ignoring an MSI interrupt and having to wait for
  3361. * another event which may never come.
  3362. */
  3363. smp_rmb();
  3364. if (status & tp->intr_mask & tp->napi_event) {
  3365. RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
  3366. tp->intr_mask = ~tp->napi_event;
  3367. if (likely(napi_schedule_prep(&tp->napi)))
  3368. __napi_schedule(&tp->napi);
  3369. else
  3370. netif_info(tp, intr, dev,
  3371. "interrupt %04x in poll\n", status);
  3372. }
  3373. /* We only get a new MSI interrupt when all active irq
  3374. * sources on the chip have been acknowledged. So, ack
  3375. * everything we've seen and check if new sources have become
  3376. * active to avoid blocking all interrupts from the chip.
  3377. */
  3378. RTL_W16(IntrStatus,
  3379. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  3380. status = RTL_R16(IntrStatus);
  3381. }
  3382. return IRQ_RETVAL(handled);
  3383. }
  3384. static int rtl8169_poll(struct napi_struct *napi, int budget)
  3385. {
  3386. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  3387. struct net_device *dev = tp->dev;
  3388. void __iomem *ioaddr = tp->mmio_addr;
  3389. int work_done;
  3390. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
  3391. rtl8169_tx_interrupt(dev, tp, ioaddr);
  3392. if (work_done < budget) {
  3393. napi_complete(napi);
  3394. /* We need for force the visibility of tp->intr_mask
  3395. * for other CPUs, as we can loose an MSI interrupt
  3396. * and potentially wait for a retransmit timeout if we don't.
  3397. * The posted write to IntrMask is safe, as it will
  3398. * eventually make it to the chip and we won't loose anything
  3399. * until it does.
  3400. */
  3401. tp->intr_mask = 0xffff;
  3402. wmb();
  3403. RTL_W16(IntrMask, tp->intr_event);
  3404. }
  3405. return work_done;
  3406. }
  3407. static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
  3408. {
  3409. struct rtl8169_private *tp = netdev_priv(dev);
  3410. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  3411. return;
  3412. dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
  3413. RTL_W32(RxMissed, 0);
  3414. }
  3415. static void rtl8169_down(struct net_device *dev)
  3416. {
  3417. struct rtl8169_private *tp = netdev_priv(dev);
  3418. void __iomem *ioaddr = tp->mmio_addr;
  3419. rtl8169_delete_timer(dev);
  3420. netif_stop_queue(dev);
  3421. napi_disable(&tp->napi);
  3422. spin_lock_irq(&tp->lock);
  3423. rtl8169_asic_down(ioaddr);
  3424. /*
  3425. * At this point device interrupts can not be enabled in any function,
  3426. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
  3427. * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
  3428. */
  3429. rtl8169_rx_missed(dev, ioaddr);
  3430. spin_unlock_irq(&tp->lock);
  3431. synchronize_irq(dev->irq);
  3432. /* Give a racing hard_start_xmit a few cycles to complete. */
  3433. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  3434. rtl8169_tx_clear(tp);
  3435. rtl8169_rx_clear(tp);
  3436. }
  3437. static int rtl8169_close(struct net_device *dev)
  3438. {
  3439. struct rtl8169_private *tp = netdev_priv(dev);
  3440. struct pci_dev *pdev = tp->pci_dev;
  3441. pm_runtime_get_sync(&pdev->dev);
  3442. /* update counters before going down */
  3443. rtl8169_update_counters(dev);
  3444. rtl8169_down(dev);
  3445. free_irq(dev->irq, dev);
  3446. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  3447. tp->RxPhyAddr);
  3448. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  3449. tp->TxPhyAddr);
  3450. tp->TxDescArray = NULL;
  3451. tp->RxDescArray = NULL;
  3452. pm_runtime_put_sync(&pdev->dev);
  3453. return 0;
  3454. }
  3455. static void rtl_set_rx_mode(struct net_device *dev)
  3456. {
  3457. struct rtl8169_private *tp = netdev_priv(dev);
  3458. void __iomem *ioaddr = tp->mmio_addr;
  3459. unsigned long flags;
  3460. u32 mc_filter[2]; /* Multicast hash filter */
  3461. int rx_mode;
  3462. u32 tmp = 0;
  3463. if (dev->flags & IFF_PROMISC) {
  3464. /* Unconditionally log net taps. */
  3465. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  3466. rx_mode =
  3467. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  3468. AcceptAllPhys;
  3469. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3470. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  3471. (dev->flags & IFF_ALLMULTI)) {
  3472. /* Too many to filter perfectly -- accept all multicasts. */
  3473. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  3474. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3475. } else {
  3476. struct netdev_hw_addr *ha;
  3477. rx_mode = AcceptBroadcast | AcceptMyPhys;
  3478. mc_filter[1] = mc_filter[0] = 0;
  3479. netdev_for_each_mc_addr(ha, dev) {
  3480. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  3481. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  3482. rx_mode |= AcceptMulticast;
  3483. }
  3484. }
  3485. spin_lock_irqsave(&tp->lock, flags);
  3486. tmp = rtl8169_rx_config | rx_mode |
  3487. (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  3488. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  3489. u32 data = mc_filter[0];
  3490. mc_filter[0] = swab32(mc_filter[1]);
  3491. mc_filter[1] = swab32(data);
  3492. }
  3493. RTL_W32(MAR0 + 4, mc_filter[1]);
  3494. RTL_W32(MAR0 + 0, mc_filter[0]);
  3495. RTL_W32(RxConfig, tmp);
  3496. spin_unlock_irqrestore(&tp->lock, flags);
  3497. }
  3498. /**
  3499. * rtl8169_get_stats - Get rtl8169 read/write statistics
  3500. * @dev: The Ethernet Device to get statistics for
  3501. *
  3502. * Get TX/RX statistics for rtl8169
  3503. */
  3504. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  3505. {
  3506. struct rtl8169_private *tp = netdev_priv(dev);
  3507. void __iomem *ioaddr = tp->mmio_addr;
  3508. unsigned long flags;
  3509. if (netif_running(dev)) {
  3510. spin_lock_irqsave(&tp->lock, flags);
  3511. rtl8169_rx_missed(dev, ioaddr);
  3512. spin_unlock_irqrestore(&tp->lock, flags);
  3513. }
  3514. return &dev->stats;
  3515. }
  3516. static void rtl8169_net_suspend(struct net_device *dev)
  3517. {
  3518. if (!netif_running(dev))
  3519. return;
  3520. netif_device_detach(dev);
  3521. netif_stop_queue(dev);
  3522. }
  3523. #ifdef CONFIG_PM
  3524. static int rtl8169_suspend(struct device *device)
  3525. {
  3526. struct pci_dev *pdev = to_pci_dev(device);
  3527. struct net_device *dev = pci_get_drvdata(pdev);
  3528. rtl8169_net_suspend(dev);
  3529. return 0;
  3530. }
  3531. static void __rtl8169_resume(struct net_device *dev)
  3532. {
  3533. netif_device_attach(dev);
  3534. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3535. }
  3536. static int rtl8169_resume(struct device *device)
  3537. {
  3538. struct pci_dev *pdev = to_pci_dev(device);
  3539. struct net_device *dev = pci_get_drvdata(pdev);
  3540. struct rtl8169_private *tp = netdev_priv(dev);
  3541. rtl8169_init_phy(dev, tp);
  3542. if (netif_running(dev))
  3543. __rtl8169_resume(dev);
  3544. return 0;
  3545. }
  3546. static int rtl8169_runtime_suspend(struct device *device)
  3547. {
  3548. struct pci_dev *pdev = to_pci_dev(device);
  3549. struct net_device *dev = pci_get_drvdata(pdev);
  3550. struct rtl8169_private *tp = netdev_priv(dev);
  3551. if (!tp->TxDescArray)
  3552. return 0;
  3553. spin_lock_irq(&tp->lock);
  3554. tp->saved_wolopts = __rtl8169_get_wol(tp);
  3555. __rtl8169_set_wol(tp, WAKE_ANY);
  3556. spin_unlock_irq(&tp->lock);
  3557. rtl8169_net_suspend(dev);
  3558. return 0;
  3559. }
  3560. static int rtl8169_runtime_resume(struct device *device)
  3561. {
  3562. struct pci_dev *pdev = to_pci_dev(device);
  3563. struct net_device *dev = pci_get_drvdata(pdev);
  3564. struct rtl8169_private *tp = netdev_priv(dev);
  3565. if (!tp->TxDescArray)
  3566. return 0;
  3567. spin_lock_irq(&tp->lock);
  3568. __rtl8169_set_wol(tp, tp->saved_wolopts);
  3569. tp->saved_wolopts = 0;
  3570. spin_unlock_irq(&tp->lock);
  3571. rtl8169_init_phy(dev, tp);
  3572. __rtl8169_resume(dev);
  3573. return 0;
  3574. }
  3575. static int rtl8169_runtime_idle(struct device *device)
  3576. {
  3577. struct pci_dev *pdev = to_pci_dev(device);
  3578. struct net_device *dev = pci_get_drvdata(pdev);
  3579. struct rtl8169_private *tp = netdev_priv(dev);
  3580. return tp->TxDescArray ? -EBUSY : 0;
  3581. }
  3582. static const struct dev_pm_ops rtl8169_pm_ops = {
  3583. .suspend = rtl8169_suspend,
  3584. .resume = rtl8169_resume,
  3585. .freeze = rtl8169_suspend,
  3586. .thaw = rtl8169_resume,
  3587. .poweroff = rtl8169_suspend,
  3588. .restore = rtl8169_resume,
  3589. .runtime_suspend = rtl8169_runtime_suspend,
  3590. .runtime_resume = rtl8169_runtime_resume,
  3591. .runtime_idle = rtl8169_runtime_idle,
  3592. };
  3593. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  3594. #else /* !CONFIG_PM */
  3595. #define RTL8169_PM_OPS NULL
  3596. #endif /* !CONFIG_PM */
  3597. static void rtl_shutdown(struct pci_dev *pdev)
  3598. {
  3599. struct net_device *dev = pci_get_drvdata(pdev);
  3600. struct rtl8169_private *tp = netdev_priv(dev);
  3601. void __iomem *ioaddr = tp->mmio_addr;
  3602. rtl8169_net_suspend(dev);
  3603. /* restore original MAC address */
  3604. rtl_rar_set(tp, dev->perm_addr);
  3605. spin_lock_irq(&tp->lock);
  3606. rtl8169_asic_down(ioaddr);
  3607. spin_unlock_irq(&tp->lock);
  3608. if (system_state == SYSTEM_POWER_OFF) {
  3609. /* WoL fails with some 8168 when the receiver is disabled. */
  3610. if (tp->features & RTL_FEATURE_WOL) {
  3611. pci_clear_master(pdev);
  3612. RTL_W8(ChipCmd, CmdRxEnb);
  3613. /* PCI commit */
  3614. RTL_R8(ChipCmd);
  3615. }
  3616. pci_wake_from_d3(pdev, true);
  3617. pci_set_power_state(pdev, PCI_D3hot);
  3618. }
  3619. }
  3620. static struct pci_driver rtl8169_pci_driver = {
  3621. .name = MODULENAME,
  3622. .id_table = rtl8169_pci_tbl,
  3623. .probe = rtl8169_init_one,
  3624. .remove = __devexit_p(rtl8169_remove_one),
  3625. .shutdown = rtl_shutdown,
  3626. .driver.pm = RTL8169_PM_OPS,
  3627. };
  3628. static int __init rtl8169_init_module(void)
  3629. {
  3630. return pci_register_driver(&rtl8169_pci_driver);
  3631. }
  3632. static void __exit rtl8169_cleanup_module(void)
  3633. {
  3634. pci_unregister_driver(&rtl8169_pci_driver);
  3635. }
  3636. module_init(rtl8169_init_module);
  3637. module_exit(rtl8169_cleanup_module);