r8169.c 116 KB

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