r8169.c 119 KB

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