r8169.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846
  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/interrupt.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/firmware.h>
  28. #include <linux/pci-aspm.h>
  29. #include <linux/prefetch.h>
  30. #include <asm/system.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #define RTL8169_VERSION "2.3LK-NAPI"
  34. #define MODULENAME "r8169"
  35. #define PFX MODULENAME ": "
  36. #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
  37. #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
  38. #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
  39. #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
  40. #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
  41. #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
  42. #ifdef RTL8169_DEBUG
  43. #define assert(expr) \
  44. if (!(expr)) { \
  45. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  46. #expr,__FILE__,__func__,__LINE__); \
  47. }
  48. #define dprintk(fmt, args...) \
  49. do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
  50. #else
  51. #define assert(expr) do {} while (0)
  52. #define dprintk(fmt, args...) do {} while (0)
  53. #endif /* RTL8169_DEBUG */
  54. #define R8169_MSG_DEFAULT \
  55. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  56. #define TX_BUFFS_AVAIL(tp) \
  57. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
  58. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  59. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  60. static const int multicast_filter_limit = 32;
  61. /* MAC address length */
  62. #define MAC_ADDR_LEN 6
  63. #define MAX_READ_REQUEST_SHIFT 12
  64. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  65. #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
  66. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  67. #define R8169_REGS_SIZE 256
  68. #define R8169_NAPI_WEIGHT 64
  69. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  70. #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
  71. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  72. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  73. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  74. #define RTL8169_TX_TIMEOUT (6*HZ)
  75. #define RTL8169_PHY_TIMEOUT (10*HZ)
  76. #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
  77. #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
  78. #define RTL_EEPROM_SIG_ADDR 0x0000
  79. /* write/read MMIO register */
  80. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  81. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  82. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  83. #define RTL_R8(reg) readb (ioaddr + (reg))
  84. #define RTL_R16(reg) readw (ioaddr + (reg))
  85. #define RTL_R32(reg) readl (ioaddr + (reg))
  86. enum mac_version {
  87. RTL_GIGA_MAC_VER_01 = 0,
  88. RTL_GIGA_MAC_VER_02,
  89. RTL_GIGA_MAC_VER_03,
  90. RTL_GIGA_MAC_VER_04,
  91. RTL_GIGA_MAC_VER_05,
  92. RTL_GIGA_MAC_VER_06,
  93. RTL_GIGA_MAC_VER_07,
  94. RTL_GIGA_MAC_VER_08,
  95. RTL_GIGA_MAC_VER_09,
  96. RTL_GIGA_MAC_VER_10,
  97. RTL_GIGA_MAC_VER_11,
  98. RTL_GIGA_MAC_VER_12,
  99. RTL_GIGA_MAC_VER_13,
  100. RTL_GIGA_MAC_VER_14,
  101. RTL_GIGA_MAC_VER_15,
  102. RTL_GIGA_MAC_VER_16,
  103. RTL_GIGA_MAC_VER_17,
  104. RTL_GIGA_MAC_VER_18,
  105. RTL_GIGA_MAC_VER_19,
  106. RTL_GIGA_MAC_VER_20,
  107. RTL_GIGA_MAC_VER_21,
  108. RTL_GIGA_MAC_VER_22,
  109. RTL_GIGA_MAC_VER_23,
  110. RTL_GIGA_MAC_VER_24,
  111. RTL_GIGA_MAC_VER_25,
  112. RTL_GIGA_MAC_VER_26,
  113. RTL_GIGA_MAC_VER_27,
  114. RTL_GIGA_MAC_VER_28,
  115. RTL_GIGA_MAC_VER_29,
  116. RTL_GIGA_MAC_VER_30,
  117. RTL_GIGA_MAC_VER_31,
  118. RTL_GIGA_MAC_VER_32,
  119. RTL_GIGA_MAC_VER_33,
  120. RTL_GIGA_MAC_VER_34,
  121. RTL_GIGA_MAC_NONE = 0xff,
  122. };
  123. enum rtl_tx_desc_version {
  124. RTL_TD_0 = 0,
  125. RTL_TD_1 = 1,
  126. };
  127. #define _R(NAME,TD,FW) \
  128. { .name = NAME, .txd_version = TD, .fw_name = FW }
  129. static const struct {
  130. const char *name;
  131. enum rtl_tx_desc_version txd_version;
  132. const char *fw_name;
  133. } rtl_chip_infos[] = {
  134. /* PCI devices. */
  135. [RTL_GIGA_MAC_VER_01] =
  136. _R("RTL8169", RTL_TD_0, NULL),
  137. [RTL_GIGA_MAC_VER_02] =
  138. _R("RTL8169s", RTL_TD_0, NULL),
  139. [RTL_GIGA_MAC_VER_03] =
  140. _R("RTL8110s", RTL_TD_0, NULL),
  141. [RTL_GIGA_MAC_VER_04] =
  142. _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
  143. [RTL_GIGA_MAC_VER_05] =
  144. _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
  145. [RTL_GIGA_MAC_VER_06] =
  146. _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
  147. /* PCI-E devices. */
  148. [RTL_GIGA_MAC_VER_07] =
  149. _R("RTL8102e", RTL_TD_1, NULL),
  150. [RTL_GIGA_MAC_VER_08] =
  151. _R("RTL8102e", RTL_TD_1, NULL),
  152. [RTL_GIGA_MAC_VER_09] =
  153. _R("RTL8102e", RTL_TD_1, NULL),
  154. [RTL_GIGA_MAC_VER_10] =
  155. _R("RTL8101e", RTL_TD_0, NULL),
  156. [RTL_GIGA_MAC_VER_11] =
  157. _R("RTL8168b/8111b", RTL_TD_0, NULL),
  158. [RTL_GIGA_MAC_VER_12] =
  159. _R("RTL8168b/8111b", RTL_TD_0, NULL),
  160. [RTL_GIGA_MAC_VER_13] =
  161. _R("RTL8101e", RTL_TD_0, NULL),
  162. [RTL_GIGA_MAC_VER_14] =
  163. _R("RTL8100e", RTL_TD_0, NULL),
  164. [RTL_GIGA_MAC_VER_15] =
  165. _R("RTL8100e", RTL_TD_0, NULL),
  166. [RTL_GIGA_MAC_VER_16] =
  167. _R("RTL8101e", RTL_TD_0, NULL),
  168. [RTL_GIGA_MAC_VER_17] =
  169. _R("RTL8168b/8111b", RTL_TD_0, NULL),
  170. [RTL_GIGA_MAC_VER_18] =
  171. _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
  172. [RTL_GIGA_MAC_VER_19] =
  173. _R("RTL8168c/8111c", RTL_TD_1, NULL),
  174. [RTL_GIGA_MAC_VER_20] =
  175. _R("RTL8168c/8111c", RTL_TD_1, NULL),
  176. [RTL_GIGA_MAC_VER_21] =
  177. _R("RTL8168c/8111c", RTL_TD_1, NULL),
  178. [RTL_GIGA_MAC_VER_22] =
  179. _R("RTL8168c/8111c", RTL_TD_1, NULL),
  180. [RTL_GIGA_MAC_VER_23] =
  181. _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
  182. [RTL_GIGA_MAC_VER_24] =
  183. _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
  184. [RTL_GIGA_MAC_VER_25] =
  185. _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
  186. [RTL_GIGA_MAC_VER_26] =
  187. _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
  188. [RTL_GIGA_MAC_VER_27] =
  189. _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
  190. [RTL_GIGA_MAC_VER_28] =
  191. _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
  192. [RTL_GIGA_MAC_VER_29] =
  193. _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
  194. [RTL_GIGA_MAC_VER_30] =
  195. _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
  196. [RTL_GIGA_MAC_VER_31] =
  197. _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
  198. [RTL_GIGA_MAC_VER_32] =
  199. _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
  200. [RTL_GIGA_MAC_VER_33] =
  201. _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2),
  202. [RTL_GIGA_MAC_VER_34] =
  203. _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3)
  204. };
  205. #undef _R
  206. enum cfg_version {
  207. RTL_CFG_0 = 0x00,
  208. RTL_CFG_1,
  209. RTL_CFG_2
  210. };
  211. static void rtl_hw_start_8169(struct net_device *);
  212. static void rtl_hw_start_8168(struct net_device *);
  213. static void rtl_hw_start_8101(struct net_device *);
  214. static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
  215. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  216. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  217. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  218. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  219. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  220. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  221. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
  222. { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
  223. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  224. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  225. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  226. { 0x0001, 0x8168,
  227. PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
  228. {0,},
  229. };
  230. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  231. static int rx_buf_sz = 16383;
  232. static int use_dac;
  233. static struct {
  234. u32 msg_enable;
  235. } debug = { -1 };
  236. enum rtl_registers {
  237. MAC0 = 0, /* Ethernet hardware address. */
  238. MAC4 = 4,
  239. MAR0 = 8, /* Multicast filter. */
  240. CounterAddrLow = 0x10,
  241. CounterAddrHigh = 0x14,
  242. TxDescStartAddrLow = 0x20,
  243. TxDescStartAddrHigh = 0x24,
  244. TxHDescStartAddrLow = 0x28,
  245. TxHDescStartAddrHigh = 0x2c,
  246. FLASH = 0x30,
  247. ERSR = 0x36,
  248. ChipCmd = 0x37,
  249. TxPoll = 0x38,
  250. IntrMask = 0x3c,
  251. IntrStatus = 0x3e,
  252. TxConfig = 0x40,
  253. #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
  254. #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
  255. RxConfig = 0x44,
  256. #define RX128_INT_EN (1 << 15) /* 8111c and later */
  257. #define RX_MULTI_EN (1 << 14) /* 8111c only */
  258. #define RXCFG_FIFO_SHIFT 13
  259. /* No threshold before first PCI xfer */
  260. #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
  261. #define RXCFG_DMA_SHIFT 8
  262. /* Unlimited maximum PCI burst. */
  263. #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
  264. RxMissed = 0x4c,
  265. Cfg9346 = 0x50,
  266. Config0 = 0x51,
  267. Config1 = 0x52,
  268. Config2 = 0x53,
  269. Config3 = 0x54,
  270. Config4 = 0x55,
  271. Config5 = 0x56,
  272. MultiIntr = 0x5c,
  273. PHYAR = 0x60,
  274. PHYstatus = 0x6c,
  275. RxMaxSize = 0xda,
  276. CPlusCmd = 0xe0,
  277. IntrMitigate = 0xe2,
  278. RxDescAddrLow = 0xe4,
  279. RxDescAddrHigh = 0xe8,
  280. EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
  281. #define NoEarlyTx 0x3f /* Max value : no early transmit. */
  282. MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
  283. #define TxPacketMax (8064 >> 7)
  284. FuncEvent = 0xf0,
  285. FuncEventMask = 0xf4,
  286. FuncPresetState = 0xf8,
  287. FuncForceEvent = 0xfc,
  288. };
  289. enum rtl8110_registers {
  290. TBICSR = 0x64,
  291. TBI_ANAR = 0x68,
  292. TBI_LPAR = 0x6a,
  293. };
  294. enum rtl8168_8101_registers {
  295. CSIDR = 0x64,
  296. CSIAR = 0x68,
  297. #define CSIAR_FLAG 0x80000000
  298. #define CSIAR_WRITE_CMD 0x80000000
  299. #define CSIAR_BYTE_ENABLE 0x0f
  300. #define CSIAR_BYTE_ENABLE_SHIFT 12
  301. #define CSIAR_ADDR_MASK 0x0fff
  302. PMCH = 0x6f,
  303. EPHYAR = 0x80,
  304. #define EPHYAR_FLAG 0x80000000
  305. #define EPHYAR_WRITE_CMD 0x80000000
  306. #define EPHYAR_REG_MASK 0x1f
  307. #define EPHYAR_REG_SHIFT 16
  308. #define EPHYAR_DATA_MASK 0xffff
  309. DLLPR = 0xd0,
  310. #define PFM_EN (1 << 6)
  311. DBG_REG = 0xd1,
  312. #define FIX_NAK_1 (1 << 4)
  313. #define FIX_NAK_2 (1 << 3)
  314. TWSI = 0xd2,
  315. MCU = 0xd3,
  316. #define NOW_IS_OOB (1 << 7)
  317. #define EN_NDP (1 << 3)
  318. #define EN_OOB_RESET (1 << 2)
  319. EFUSEAR = 0xdc,
  320. #define EFUSEAR_FLAG 0x80000000
  321. #define EFUSEAR_WRITE_CMD 0x80000000
  322. #define EFUSEAR_READ_CMD 0x00000000
  323. #define EFUSEAR_REG_MASK 0x03ff
  324. #define EFUSEAR_REG_SHIFT 8
  325. #define EFUSEAR_DATA_MASK 0xff
  326. };
  327. enum rtl8168_registers {
  328. LED_FREQ = 0x1a,
  329. EEE_LED = 0x1b,
  330. ERIDR = 0x70,
  331. ERIAR = 0x74,
  332. #define ERIAR_FLAG 0x80000000
  333. #define ERIAR_WRITE_CMD 0x80000000
  334. #define ERIAR_READ_CMD 0x00000000
  335. #define ERIAR_ADDR_BYTE_ALIGN 4
  336. #define ERIAR_TYPE_SHIFT 16
  337. #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
  338. #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
  339. #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
  340. #define ERIAR_MASK_SHIFT 12
  341. #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
  342. #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
  343. #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
  344. EPHY_RXER_NUM = 0x7c,
  345. OCPDR = 0xb0, /* OCP GPHY access */
  346. #define OCPDR_WRITE_CMD 0x80000000
  347. #define OCPDR_READ_CMD 0x00000000
  348. #define OCPDR_REG_MASK 0x7f
  349. #define OCPDR_GPHY_REG_SHIFT 16
  350. #define OCPDR_DATA_MASK 0xffff
  351. OCPAR = 0xb4,
  352. #define OCPAR_FLAG 0x80000000
  353. #define OCPAR_GPHY_WRITE_CMD 0x8000f060
  354. #define OCPAR_GPHY_READ_CMD 0x0000f060
  355. RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
  356. MISC = 0xf0, /* 8168e only. */
  357. #define TXPLA_RST (1 << 29)
  358. #define PWM_EN (1 << 22)
  359. };
  360. enum rtl_register_content {
  361. /* InterruptStatusBits */
  362. SYSErr = 0x8000,
  363. PCSTimeout = 0x4000,
  364. SWInt = 0x0100,
  365. TxDescUnavail = 0x0080,
  366. RxFIFOOver = 0x0040,
  367. LinkChg = 0x0020,
  368. RxOverflow = 0x0010,
  369. TxErr = 0x0008,
  370. TxOK = 0x0004,
  371. RxErr = 0x0002,
  372. RxOK = 0x0001,
  373. /* RxStatusDesc */
  374. RxBOVF = (1 << 24),
  375. RxFOVF = (1 << 23),
  376. RxRWT = (1 << 22),
  377. RxRES = (1 << 21),
  378. RxRUNT = (1 << 20),
  379. RxCRC = (1 << 19),
  380. /* ChipCmdBits */
  381. StopReq = 0x80,
  382. CmdReset = 0x10,
  383. CmdRxEnb = 0x08,
  384. CmdTxEnb = 0x04,
  385. RxBufEmpty = 0x01,
  386. /* TXPoll register p.5 */
  387. HPQ = 0x80, /* Poll cmd on the high prio queue */
  388. NPQ = 0x40, /* Poll cmd on the low prio queue */
  389. FSWInt = 0x01, /* Forced software interrupt */
  390. /* Cfg9346Bits */
  391. Cfg9346_Lock = 0x00,
  392. Cfg9346_Unlock = 0xc0,
  393. /* rx_mode_bits */
  394. AcceptErr = 0x20,
  395. AcceptRunt = 0x10,
  396. AcceptBroadcast = 0x08,
  397. AcceptMulticast = 0x04,
  398. AcceptMyPhys = 0x02,
  399. AcceptAllPhys = 0x01,
  400. #define RX_CONFIG_ACCEPT_MASK 0x3f
  401. /* TxConfigBits */
  402. TxInterFrameGapShift = 24,
  403. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  404. /* Config1 register p.24 */
  405. LEDS1 = (1 << 7),
  406. LEDS0 = (1 << 6),
  407. MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
  408. Speed_down = (1 << 4),
  409. MEMMAP = (1 << 3),
  410. IOMAP = (1 << 2),
  411. VPD = (1 << 1),
  412. PMEnable = (1 << 0), /* Power Management Enable */
  413. /* Config2 register p. 25 */
  414. PCI_Clock_66MHz = 0x01,
  415. PCI_Clock_33MHz = 0x00,
  416. /* Config3 register p.25 */
  417. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  418. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  419. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  420. /* Config5 register p.27 */
  421. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  422. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  423. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  424. Spi_en = (1 << 3),
  425. LanWake = (1 << 1), /* LanWake enable/disable */
  426. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  427. /* TBICSR p.28 */
  428. TBIReset = 0x80000000,
  429. TBILoopback = 0x40000000,
  430. TBINwEnable = 0x20000000,
  431. TBINwRestart = 0x10000000,
  432. TBILinkOk = 0x02000000,
  433. TBINwComplete = 0x01000000,
  434. /* CPlusCmd p.31 */
  435. EnableBist = (1 << 15), // 8168 8101
  436. Mac_dbgo_oe = (1 << 14), // 8168 8101
  437. Normal_mode = (1 << 13), // unused
  438. Force_half_dup = (1 << 12), // 8168 8101
  439. Force_rxflow_en = (1 << 11), // 8168 8101
  440. Force_txflow_en = (1 << 10), // 8168 8101
  441. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  442. ASF = (1 << 8), // 8168 8101
  443. PktCntrDisable = (1 << 7), // 8168 8101
  444. Mac_dbgo_sel = 0x001c, // 8168
  445. RxVlan = (1 << 6),
  446. RxChkSum = (1 << 5),
  447. PCIDAC = (1 << 4),
  448. PCIMulRW = (1 << 3),
  449. INTT_0 = 0x0000, // 8168
  450. INTT_1 = 0x0001, // 8168
  451. INTT_2 = 0x0002, // 8168
  452. INTT_3 = 0x0003, // 8168
  453. /* rtl8169_PHYstatus */
  454. TBI_Enable = 0x80,
  455. TxFlowCtrl = 0x40,
  456. RxFlowCtrl = 0x20,
  457. _1000bpsF = 0x10,
  458. _100bps = 0x08,
  459. _10bps = 0x04,
  460. LinkStatus = 0x02,
  461. FullDup = 0x01,
  462. /* _TBICSRBit */
  463. TBILinkOK = 0x02000000,
  464. /* DumpCounterCommand */
  465. CounterDump = 0x8,
  466. };
  467. enum rtl_desc_bit {
  468. /* First doubleword. */
  469. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  470. RingEnd = (1 << 30), /* End of descriptor ring */
  471. FirstFrag = (1 << 29), /* First segment of a packet */
  472. LastFrag = (1 << 28), /* Final segment of a packet */
  473. };
  474. /* Generic case. */
  475. enum rtl_tx_desc_bit {
  476. /* First doubleword. */
  477. TD_LSO = (1 << 27), /* Large Send Offload */
  478. #define TD_MSS_MAX 0x07ffu /* MSS value */
  479. /* Second doubleword. */
  480. TxVlanTag = (1 << 17), /* Add VLAN tag */
  481. };
  482. /* 8169, 8168b and 810x except 8102e. */
  483. enum rtl_tx_desc_bit_0 {
  484. /* First doubleword. */
  485. #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
  486. TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
  487. TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
  488. TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
  489. };
  490. /* 8102e, 8168c and beyond. */
  491. enum rtl_tx_desc_bit_1 {
  492. /* Second doubleword. */
  493. #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
  494. TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
  495. TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
  496. TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
  497. };
  498. static const struct rtl_tx_desc_info {
  499. struct {
  500. u32 udp;
  501. u32 tcp;
  502. } checksum;
  503. u16 mss_shift;
  504. u16 opts_offset;
  505. } tx_desc_info [] = {
  506. [RTL_TD_0] = {
  507. .checksum = {
  508. .udp = TD0_IP_CS | TD0_UDP_CS,
  509. .tcp = TD0_IP_CS | TD0_TCP_CS
  510. },
  511. .mss_shift = TD0_MSS_SHIFT,
  512. .opts_offset = 0
  513. },
  514. [RTL_TD_1] = {
  515. .checksum = {
  516. .udp = TD1_IP_CS | TD1_UDP_CS,
  517. .tcp = TD1_IP_CS | TD1_TCP_CS
  518. },
  519. .mss_shift = TD1_MSS_SHIFT,
  520. .opts_offset = 1
  521. }
  522. };
  523. enum rtl_rx_desc_bit {
  524. /* Rx private */
  525. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  526. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  527. #define RxProtoUDP (PID1)
  528. #define RxProtoTCP (PID0)
  529. #define RxProtoIP (PID1 | PID0)
  530. #define RxProtoMask RxProtoIP
  531. IPFail = (1 << 16), /* IP checksum failed */
  532. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  533. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  534. RxVlanTag = (1 << 16), /* VLAN tag available */
  535. };
  536. #define RsvdMask 0x3fffc000
  537. struct TxDesc {
  538. __le32 opts1;
  539. __le32 opts2;
  540. __le64 addr;
  541. };
  542. struct RxDesc {
  543. __le32 opts1;
  544. __le32 opts2;
  545. __le64 addr;
  546. };
  547. struct ring_info {
  548. struct sk_buff *skb;
  549. u32 len;
  550. u8 __pad[sizeof(void *) - sizeof(u32)];
  551. };
  552. enum features {
  553. RTL_FEATURE_WOL = (1 << 0),
  554. RTL_FEATURE_MSI = (1 << 1),
  555. RTL_FEATURE_GMII = (1 << 2),
  556. };
  557. struct rtl8169_counters {
  558. __le64 tx_packets;
  559. __le64 rx_packets;
  560. __le64 tx_errors;
  561. __le32 rx_errors;
  562. __le16 rx_missed;
  563. __le16 align_errors;
  564. __le32 tx_one_collision;
  565. __le32 tx_multi_collision;
  566. __le64 rx_unicast;
  567. __le64 rx_broadcast;
  568. __le32 rx_multicast;
  569. __le16 tx_aborted;
  570. __le16 tx_underun;
  571. };
  572. struct rtl8169_private {
  573. void __iomem *mmio_addr; /* memory map physical address */
  574. struct pci_dev *pci_dev;
  575. struct net_device *dev;
  576. struct napi_struct napi;
  577. spinlock_t lock;
  578. u32 msg_enable;
  579. u16 txd_version;
  580. u16 mac_version;
  581. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  582. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  583. u32 dirty_rx;
  584. u32 dirty_tx;
  585. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  586. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  587. dma_addr_t TxPhyAddr;
  588. dma_addr_t RxPhyAddr;
  589. void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
  590. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  591. struct timer_list timer;
  592. u16 cp_cmd;
  593. u16 intr_event;
  594. u16 napi_event;
  595. u16 intr_mask;
  596. struct mdio_ops {
  597. void (*write)(void __iomem *, int, int);
  598. int (*read)(void __iomem *, int);
  599. } mdio_ops;
  600. struct pll_power_ops {
  601. void (*down)(struct rtl8169_private *);
  602. void (*up)(struct rtl8169_private *);
  603. } pll_power_ops;
  604. int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
  605. int (*get_settings)(struct net_device *, struct ethtool_cmd *);
  606. void (*phy_reset_enable)(struct rtl8169_private *tp);
  607. void (*hw_start)(struct net_device *);
  608. unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
  609. unsigned int (*link_ok)(void __iomem *);
  610. int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
  611. struct delayed_work task;
  612. unsigned features;
  613. struct mii_if_info mii;
  614. struct rtl8169_counters counters;
  615. u32 saved_wolopts;
  616. u32 opts1_mask;
  617. struct rtl_fw {
  618. const struct firmware *fw;
  619. #define RTL_VER_SIZE 32
  620. char version[RTL_VER_SIZE];
  621. struct rtl_fw_phy_action {
  622. __le32 *code;
  623. size_t size;
  624. } phy_action;
  625. } *rtl_fw;
  626. #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
  627. };
  628. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  629. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  630. module_param(use_dac, int, 0);
  631. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  632. module_param_named(debug, debug.msg_enable, int, 0);
  633. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  634. MODULE_LICENSE("GPL");
  635. MODULE_VERSION(RTL8169_VERSION);
  636. MODULE_FIRMWARE(FIRMWARE_8168D_1);
  637. MODULE_FIRMWARE(FIRMWARE_8168D_2);
  638. MODULE_FIRMWARE(FIRMWARE_8168E_1);
  639. MODULE_FIRMWARE(FIRMWARE_8168E_2);
  640. MODULE_FIRMWARE(FIRMWARE_8168E_3);
  641. MODULE_FIRMWARE(FIRMWARE_8105E_1);
  642. static int rtl8169_open(struct net_device *dev);
  643. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  644. struct net_device *dev);
  645. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
  646. static int rtl8169_init_ring(struct net_device *dev);
  647. static void rtl_hw_start(struct net_device *dev);
  648. static int rtl8169_close(struct net_device *dev);
  649. static void rtl_set_rx_mode(struct net_device *dev);
  650. static void rtl8169_tx_timeout(struct net_device *dev);
  651. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
  652. static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
  653. void __iomem *, u32 budget);
  654. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
  655. static void rtl8169_down(struct net_device *dev);
  656. static void rtl8169_rx_clear(struct rtl8169_private *tp);
  657. static int rtl8169_poll(struct napi_struct *napi, int budget);
  658. static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  659. {
  660. void __iomem *ioaddr = tp->mmio_addr;
  661. int i;
  662. RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  663. for (i = 0; i < 20; i++) {
  664. udelay(100);
  665. if (RTL_R32(OCPAR) & OCPAR_FLAG)
  666. break;
  667. }
  668. return RTL_R32(OCPDR);
  669. }
  670. static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
  671. {
  672. void __iomem *ioaddr = tp->mmio_addr;
  673. int i;
  674. RTL_W32(OCPDR, data);
  675. RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  676. for (i = 0; i < 20; i++) {
  677. udelay(100);
  678. if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
  679. break;
  680. }
  681. }
  682. static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
  683. {
  684. void __iomem *ioaddr = tp->mmio_addr;
  685. int i;
  686. RTL_W8(ERIDR, cmd);
  687. RTL_W32(ERIAR, 0x800010e8);
  688. msleep(2);
  689. for (i = 0; i < 5; i++) {
  690. udelay(100);
  691. if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
  692. break;
  693. }
  694. ocp_write(tp, 0x1, 0x30, 0x00000001);
  695. }
  696. #define OOB_CMD_RESET 0x00
  697. #define OOB_CMD_DRIVER_START 0x05
  698. #define OOB_CMD_DRIVER_STOP 0x06
  699. static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
  700. {
  701. return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
  702. }
  703. static void rtl8168_driver_start(struct rtl8169_private *tp)
  704. {
  705. u16 reg;
  706. int i;
  707. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
  708. reg = rtl8168_get_ocp_reg(tp);
  709. for (i = 0; i < 10; i++) {
  710. msleep(10);
  711. if (ocp_read(tp, 0x0f, reg) & 0x00000800)
  712. break;
  713. }
  714. }
  715. static void rtl8168_driver_stop(struct rtl8169_private *tp)
  716. {
  717. u16 reg;
  718. int i;
  719. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
  720. reg = rtl8168_get_ocp_reg(tp);
  721. for (i = 0; i < 10; i++) {
  722. msleep(10);
  723. if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
  724. break;
  725. }
  726. }
  727. static int r8168dp_check_dash(struct rtl8169_private *tp)
  728. {
  729. u16 reg = rtl8168_get_ocp_reg(tp);
  730. return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
  731. }
  732. static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  733. {
  734. int i;
  735. RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
  736. for (i = 20; i > 0; i--) {
  737. /*
  738. * Check if the RTL8169 has completed writing to the specified
  739. * MII register.
  740. */
  741. if (!(RTL_R32(PHYAR) & 0x80000000))
  742. break;
  743. udelay(25);
  744. }
  745. /*
  746. * According to hardware specs a 20us delay is required after write
  747. * complete indication, but before sending next command.
  748. */
  749. udelay(20);
  750. }
  751. static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
  752. {
  753. int i, value = -1;
  754. RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
  755. for (i = 20; i > 0; i--) {
  756. /*
  757. * Check if the RTL8169 has completed retrieving data from
  758. * the specified MII register.
  759. */
  760. if (RTL_R32(PHYAR) & 0x80000000) {
  761. value = RTL_R32(PHYAR) & 0xffff;
  762. break;
  763. }
  764. udelay(25);
  765. }
  766. /*
  767. * According to hardware specs a 20us delay is required after read
  768. * complete indication, but before sending next command.
  769. */
  770. udelay(20);
  771. return value;
  772. }
  773. static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
  774. {
  775. int i;
  776. RTL_W32(OCPDR, data |
  777. ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
  778. RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
  779. RTL_W32(EPHY_RXER_NUM, 0);
  780. for (i = 0; i < 100; i++) {
  781. mdelay(1);
  782. if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
  783. break;
  784. }
  785. }
  786. static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  787. {
  788. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
  789. (value & OCPDR_DATA_MASK));
  790. }
  791. static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
  792. {
  793. int i;
  794. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
  795. mdelay(1);
  796. RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
  797. RTL_W32(EPHY_RXER_NUM, 0);
  798. for (i = 0; i < 100; i++) {
  799. mdelay(1);
  800. if (RTL_R32(OCPAR) & OCPAR_FLAG)
  801. break;
  802. }
  803. return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
  804. }
  805. #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
  806. static void r8168dp_2_mdio_start(void __iomem *ioaddr)
  807. {
  808. RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
  809. }
  810. static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
  811. {
  812. RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
  813. }
  814. static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  815. {
  816. r8168dp_2_mdio_start(ioaddr);
  817. r8169_mdio_write(ioaddr, reg_addr, value);
  818. r8168dp_2_mdio_stop(ioaddr);
  819. }
  820. static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
  821. {
  822. int value;
  823. r8168dp_2_mdio_start(ioaddr);
  824. value = r8169_mdio_read(ioaddr, reg_addr);
  825. r8168dp_2_mdio_stop(ioaddr);
  826. return value;
  827. }
  828. static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
  829. {
  830. tp->mdio_ops.write(tp->mmio_addr, location, val);
  831. }
  832. static int rtl_readphy(struct rtl8169_private *tp, int location)
  833. {
  834. return tp->mdio_ops.read(tp->mmio_addr, location);
  835. }
  836. static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
  837. {
  838. rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
  839. }
  840. static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
  841. {
  842. int val;
  843. val = rtl_readphy(tp, reg_addr);
  844. rtl_writephy(tp, reg_addr, (val | p) & ~m);
  845. }
  846. static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
  847. int val)
  848. {
  849. struct rtl8169_private *tp = netdev_priv(dev);
  850. rtl_writephy(tp, location, val);
  851. }
  852. static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
  853. {
  854. struct rtl8169_private *tp = netdev_priv(dev);
  855. return rtl_readphy(tp, location);
  856. }
  857. static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
  858. {
  859. unsigned int i;
  860. RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  861. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  862. for (i = 0; i < 100; i++) {
  863. if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
  864. break;
  865. udelay(10);
  866. }
  867. }
  868. static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
  869. {
  870. u16 value = 0xffff;
  871. unsigned int i;
  872. RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  873. for (i = 0; i < 100; i++) {
  874. if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
  875. value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
  876. break;
  877. }
  878. udelay(10);
  879. }
  880. return value;
  881. }
  882. static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
  883. {
  884. unsigned int i;
  885. RTL_W32(CSIDR, value);
  886. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  887. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  888. for (i = 0; i < 100; i++) {
  889. if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
  890. break;
  891. udelay(10);
  892. }
  893. }
  894. static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
  895. {
  896. u32 value = ~0x00;
  897. unsigned int i;
  898. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  899. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  900. for (i = 0; i < 100; i++) {
  901. if (RTL_R32(CSIAR) & CSIAR_FLAG) {
  902. value = RTL_R32(CSIDR);
  903. break;
  904. }
  905. udelay(10);
  906. }
  907. return value;
  908. }
  909. static
  910. void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
  911. {
  912. unsigned int i;
  913. BUG_ON((addr & 3) || (mask == 0));
  914. RTL_W32(ERIDR, val);
  915. RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
  916. for (i = 0; i < 100; i++) {
  917. if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
  918. break;
  919. udelay(100);
  920. }
  921. }
  922. static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
  923. {
  924. u32 value = ~0x00;
  925. unsigned int i;
  926. RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
  927. for (i = 0; i < 100; i++) {
  928. if (RTL_R32(ERIAR) & ERIAR_FLAG) {
  929. value = RTL_R32(ERIDR);
  930. break;
  931. }
  932. udelay(100);
  933. }
  934. return value;
  935. }
  936. static void
  937. rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
  938. {
  939. u32 val;
  940. val = rtl_eri_read(ioaddr, addr, type);
  941. rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
  942. }
  943. struct exgmac_reg {
  944. u16 addr;
  945. u16 mask;
  946. u32 val;
  947. };
  948. static void rtl_write_exgmac_batch(void __iomem *ioaddr,
  949. const struct exgmac_reg *r, int len)
  950. {
  951. while (len-- > 0) {
  952. rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
  953. r++;
  954. }
  955. }
  956. static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
  957. {
  958. u8 value = 0xff;
  959. unsigned int i;
  960. RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  961. for (i = 0; i < 300; i++) {
  962. if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
  963. value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
  964. break;
  965. }
  966. udelay(100);
  967. }
  968. return value;
  969. }
  970. static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
  971. {
  972. RTL_W16(IntrMask, 0x0000);
  973. RTL_W16(IntrStatus, 0xffff);
  974. }
  975. static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
  976. {
  977. void __iomem *ioaddr = tp->mmio_addr;
  978. return RTL_R32(TBICSR) & TBIReset;
  979. }
  980. static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
  981. {
  982. return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
  983. }
  984. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  985. {
  986. return RTL_R32(TBICSR) & TBILinkOk;
  987. }
  988. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  989. {
  990. return RTL_R8(PHYstatus) & LinkStatus;
  991. }
  992. static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
  993. {
  994. void __iomem *ioaddr = tp->mmio_addr;
  995. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  996. }
  997. static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
  998. {
  999. unsigned int val;
  1000. val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
  1001. rtl_writephy(tp, MII_BMCR, val & 0xffff);
  1002. }
  1003. static void rtl_link_chg_patch(struct rtl8169_private *tp)
  1004. {
  1005. void __iomem *ioaddr = tp->mmio_addr;
  1006. struct net_device *dev = tp->dev;
  1007. if (!netif_running(dev))
  1008. return;
  1009. if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
  1010. if (RTL_R8(PHYstatus) & _1000bpsF) {
  1011. rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
  1012. 0x00000011, ERIAR_EXGMAC);
  1013. rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
  1014. 0x00000005, ERIAR_EXGMAC);
  1015. } else if (RTL_R8(PHYstatus) & _100bps) {
  1016. rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
  1017. 0x0000001f, ERIAR_EXGMAC);
  1018. rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
  1019. 0x00000005, ERIAR_EXGMAC);
  1020. } else {
  1021. rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
  1022. 0x0000001f, ERIAR_EXGMAC);
  1023. rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
  1024. 0x0000003f, ERIAR_EXGMAC);
  1025. }
  1026. /* Reset packet filter */
  1027. rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
  1028. ERIAR_EXGMAC);
  1029. rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
  1030. ERIAR_EXGMAC);
  1031. }
  1032. }
  1033. static void __rtl8169_check_link_status(struct net_device *dev,
  1034. struct rtl8169_private *tp,
  1035. void __iomem *ioaddr, bool pm)
  1036. {
  1037. unsigned long flags;
  1038. spin_lock_irqsave(&tp->lock, flags);
  1039. if (tp->link_ok(ioaddr)) {
  1040. rtl_link_chg_patch(tp);
  1041. /* This is to cancel a scheduled suspend if there's one. */
  1042. if (pm)
  1043. pm_request_resume(&tp->pci_dev->dev);
  1044. netif_carrier_on(dev);
  1045. if (net_ratelimit())
  1046. netif_info(tp, ifup, dev, "link up\n");
  1047. } else {
  1048. netif_carrier_off(dev);
  1049. netif_info(tp, ifdown, dev, "link down\n");
  1050. if (pm)
  1051. pm_schedule_suspend(&tp->pci_dev->dev, 100);
  1052. }
  1053. spin_unlock_irqrestore(&tp->lock, flags);
  1054. }
  1055. static void rtl8169_check_link_status(struct net_device *dev,
  1056. struct rtl8169_private *tp,
  1057. void __iomem *ioaddr)
  1058. {
  1059. __rtl8169_check_link_status(dev, tp, ioaddr, false);
  1060. }
  1061. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  1062. static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
  1063. {
  1064. void __iomem *ioaddr = tp->mmio_addr;
  1065. u8 options;
  1066. u32 wolopts = 0;
  1067. options = RTL_R8(Config1);
  1068. if (!(options & PMEnable))
  1069. return 0;
  1070. options = RTL_R8(Config3);
  1071. if (options & LinkUp)
  1072. wolopts |= WAKE_PHY;
  1073. if (options & MagicPacket)
  1074. wolopts |= WAKE_MAGIC;
  1075. options = RTL_R8(Config5);
  1076. if (options & UWF)
  1077. wolopts |= WAKE_UCAST;
  1078. if (options & BWF)
  1079. wolopts |= WAKE_BCAST;
  1080. if (options & MWF)
  1081. wolopts |= WAKE_MCAST;
  1082. return wolopts;
  1083. }
  1084. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1085. {
  1086. struct rtl8169_private *tp = netdev_priv(dev);
  1087. spin_lock_irq(&tp->lock);
  1088. wol->supported = WAKE_ANY;
  1089. wol->wolopts = __rtl8169_get_wol(tp);
  1090. spin_unlock_irq(&tp->lock);
  1091. }
  1092. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  1093. {
  1094. void __iomem *ioaddr = tp->mmio_addr;
  1095. unsigned int i;
  1096. static const struct {
  1097. u32 opt;
  1098. u16 reg;
  1099. u8 mask;
  1100. } cfg[] = {
  1101. { WAKE_ANY, Config1, PMEnable },
  1102. { WAKE_PHY, Config3, LinkUp },
  1103. { WAKE_MAGIC, Config3, MagicPacket },
  1104. { WAKE_UCAST, Config5, UWF },
  1105. { WAKE_BCAST, Config5, BWF },
  1106. { WAKE_MCAST, Config5, MWF },
  1107. { WAKE_ANY, Config5, LanWake }
  1108. };
  1109. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1110. for (i = 0; i < ARRAY_SIZE(cfg); i++) {
  1111. u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  1112. if (wolopts & cfg[i].opt)
  1113. options |= cfg[i].mask;
  1114. RTL_W8(cfg[i].reg, options);
  1115. }
  1116. RTL_W8(Cfg9346, Cfg9346_Lock);
  1117. }
  1118. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1119. {
  1120. struct rtl8169_private *tp = netdev_priv(dev);
  1121. spin_lock_irq(&tp->lock);
  1122. if (wol->wolopts)
  1123. tp->features |= RTL_FEATURE_WOL;
  1124. else
  1125. tp->features &= ~RTL_FEATURE_WOL;
  1126. __rtl8169_set_wol(tp, wol->wolopts);
  1127. spin_unlock_irq(&tp->lock);
  1128. device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
  1129. return 0;
  1130. }
  1131. static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
  1132. {
  1133. return rtl_chip_infos[tp->mac_version].fw_name;
  1134. }
  1135. static void rtl8169_get_drvinfo(struct net_device *dev,
  1136. struct ethtool_drvinfo *info)
  1137. {
  1138. struct rtl8169_private *tp = netdev_priv(dev);
  1139. struct rtl_fw *rtl_fw = tp->rtl_fw;
  1140. strcpy(info->driver, MODULENAME);
  1141. strcpy(info->version, RTL8169_VERSION);
  1142. strcpy(info->bus_info, pci_name(tp->pci_dev));
  1143. BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
  1144. strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
  1145. rtl_fw->version);
  1146. }
  1147. static int rtl8169_get_regs_len(struct net_device *dev)
  1148. {
  1149. return R8169_REGS_SIZE;
  1150. }
  1151. static int rtl8169_set_speed_tbi(struct net_device *dev,
  1152. u8 autoneg, u16 speed, u8 duplex, u32 ignored)
  1153. {
  1154. struct rtl8169_private *tp = netdev_priv(dev);
  1155. void __iomem *ioaddr = tp->mmio_addr;
  1156. int ret = 0;
  1157. u32 reg;
  1158. reg = RTL_R32(TBICSR);
  1159. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  1160. (duplex == DUPLEX_FULL)) {
  1161. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  1162. } else if (autoneg == AUTONEG_ENABLE)
  1163. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  1164. else {
  1165. netif_warn(tp, link, dev,
  1166. "incorrect speed setting refused in TBI mode\n");
  1167. ret = -EOPNOTSUPP;
  1168. }
  1169. return ret;
  1170. }
  1171. static int rtl8169_set_speed_xmii(struct net_device *dev,
  1172. u8 autoneg, u16 speed, u8 duplex, u32 adv)
  1173. {
  1174. struct rtl8169_private *tp = netdev_priv(dev);
  1175. int giga_ctrl, bmcr;
  1176. int rc = -EINVAL;
  1177. rtl_writephy(tp, 0x1f, 0x0000);
  1178. if (autoneg == AUTONEG_ENABLE) {
  1179. int auto_nego;
  1180. auto_nego = rtl_readphy(tp, MII_ADVERTISE);
  1181. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  1182. ADVERTISE_100HALF | ADVERTISE_100FULL);
  1183. if (adv & ADVERTISED_10baseT_Half)
  1184. auto_nego |= ADVERTISE_10HALF;
  1185. if (adv & ADVERTISED_10baseT_Full)
  1186. auto_nego |= ADVERTISE_10FULL;
  1187. if (adv & ADVERTISED_100baseT_Half)
  1188. auto_nego |= ADVERTISE_100HALF;
  1189. if (adv & ADVERTISED_100baseT_Full)
  1190. auto_nego |= ADVERTISE_100FULL;
  1191. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1192. giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
  1193. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  1194. /* The 8100e/8101e/8102e do Fast Ethernet only. */
  1195. if (tp->mii.supports_gmii) {
  1196. if (adv & ADVERTISED_1000baseT_Half)
  1197. giga_ctrl |= ADVERTISE_1000HALF;
  1198. if (adv & ADVERTISED_1000baseT_Full)
  1199. giga_ctrl |= ADVERTISE_1000FULL;
  1200. } else if (adv & (ADVERTISED_1000baseT_Half |
  1201. ADVERTISED_1000baseT_Full)) {
  1202. netif_info(tp, link, dev,
  1203. "PHY does not support 1000Mbps\n");
  1204. goto out;
  1205. }
  1206. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  1207. rtl_writephy(tp, MII_ADVERTISE, auto_nego);
  1208. rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
  1209. } else {
  1210. giga_ctrl = 0;
  1211. if (speed == SPEED_10)
  1212. bmcr = 0;
  1213. else if (speed == SPEED_100)
  1214. bmcr = BMCR_SPEED100;
  1215. else
  1216. goto out;
  1217. if (duplex == DUPLEX_FULL)
  1218. bmcr |= BMCR_FULLDPLX;
  1219. }
  1220. rtl_writephy(tp, MII_BMCR, bmcr);
  1221. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  1222. tp->mac_version == RTL_GIGA_MAC_VER_03) {
  1223. if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
  1224. rtl_writephy(tp, 0x17, 0x2138);
  1225. rtl_writephy(tp, 0x0e, 0x0260);
  1226. } else {
  1227. rtl_writephy(tp, 0x17, 0x2108);
  1228. rtl_writephy(tp, 0x0e, 0x0000);
  1229. }
  1230. }
  1231. rc = 0;
  1232. out:
  1233. return rc;
  1234. }
  1235. static int rtl8169_set_speed(struct net_device *dev,
  1236. u8 autoneg, u16 speed, u8 duplex, u32 advertising)
  1237. {
  1238. struct rtl8169_private *tp = netdev_priv(dev);
  1239. int ret;
  1240. ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
  1241. if (ret < 0)
  1242. goto out;
  1243. if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
  1244. (advertising & ADVERTISED_1000baseT_Full)) {
  1245. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  1246. }
  1247. out:
  1248. return ret;
  1249. }
  1250. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1251. {
  1252. struct rtl8169_private *tp = netdev_priv(dev);
  1253. unsigned long flags;
  1254. int ret;
  1255. del_timer_sync(&tp->timer);
  1256. spin_lock_irqsave(&tp->lock, flags);
  1257. ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
  1258. cmd->duplex, cmd->advertising);
  1259. spin_unlock_irqrestore(&tp->lock, flags);
  1260. return ret;
  1261. }
  1262. static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
  1263. {
  1264. if (dev->mtu > TD_MSS_MAX)
  1265. features &= ~NETIF_F_ALL_TSO;
  1266. return features;
  1267. }
  1268. static int rtl8169_set_features(struct net_device *dev, u32 features)
  1269. {
  1270. struct rtl8169_private *tp = netdev_priv(dev);
  1271. void __iomem *ioaddr = tp->mmio_addr;
  1272. unsigned long flags;
  1273. spin_lock_irqsave(&tp->lock, flags);
  1274. if (features & NETIF_F_RXCSUM)
  1275. tp->cp_cmd |= RxChkSum;
  1276. else
  1277. tp->cp_cmd &= ~RxChkSum;
  1278. if (dev->features & NETIF_F_HW_VLAN_RX)
  1279. tp->cp_cmd |= RxVlan;
  1280. else
  1281. tp->cp_cmd &= ~RxVlan;
  1282. RTL_W16(CPlusCmd, tp->cp_cmd);
  1283. RTL_R16(CPlusCmd);
  1284. spin_unlock_irqrestore(&tp->lock, flags);
  1285. return 0;
  1286. }
  1287. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  1288. struct sk_buff *skb)
  1289. {
  1290. return (vlan_tx_tag_present(skb)) ?
  1291. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  1292. }
  1293. static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
  1294. {
  1295. u32 opts2 = le32_to_cpu(desc->opts2);
  1296. if (opts2 & RxVlanTag)
  1297. __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
  1298. desc->opts2 = 0;
  1299. }
  1300. static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  1301. {
  1302. struct rtl8169_private *tp = netdev_priv(dev);
  1303. void __iomem *ioaddr = tp->mmio_addr;
  1304. u32 status;
  1305. cmd->supported =
  1306. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1307. cmd->port = PORT_FIBRE;
  1308. cmd->transceiver = XCVR_INTERNAL;
  1309. status = RTL_R32(TBICSR);
  1310. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  1311. cmd->autoneg = !!(status & TBINwEnable);
  1312. ethtool_cmd_speed_set(cmd, SPEED_1000);
  1313. cmd->duplex = DUPLEX_FULL; /* Always set */
  1314. return 0;
  1315. }
  1316. static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  1317. {
  1318. struct rtl8169_private *tp = netdev_priv(dev);
  1319. return mii_ethtool_gset(&tp->mii, cmd);
  1320. }
  1321. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1322. {
  1323. struct rtl8169_private *tp = netdev_priv(dev);
  1324. unsigned long flags;
  1325. int rc;
  1326. spin_lock_irqsave(&tp->lock, flags);
  1327. rc = tp->get_settings(dev, cmd);
  1328. spin_unlock_irqrestore(&tp->lock, flags);
  1329. return rc;
  1330. }
  1331. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1332. void *p)
  1333. {
  1334. struct rtl8169_private *tp = netdev_priv(dev);
  1335. unsigned long flags;
  1336. if (regs->len > R8169_REGS_SIZE)
  1337. regs->len = R8169_REGS_SIZE;
  1338. spin_lock_irqsave(&tp->lock, flags);
  1339. memcpy_fromio(p, tp->mmio_addr, regs->len);
  1340. spin_unlock_irqrestore(&tp->lock, flags);
  1341. }
  1342. static u32 rtl8169_get_msglevel(struct net_device *dev)
  1343. {
  1344. struct rtl8169_private *tp = netdev_priv(dev);
  1345. return tp->msg_enable;
  1346. }
  1347. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1348. {
  1349. struct rtl8169_private *tp = netdev_priv(dev);
  1350. tp->msg_enable = value;
  1351. }
  1352. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1353. "tx_packets",
  1354. "rx_packets",
  1355. "tx_errors",
  1356. "rx_errors",
  1357. "rx_missed",
  1358. "align_errors",
  1359. "tx_single_collisions",
  1360. "tx_multi_collisions",
  1361. "unicast",
  1362. "broadcast",
  1363. "multicast",
  1364. "tx_aborted",
  1365. "tx_underrun",
  1366. };
  1367. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1368. {
  1369. switch (sset) {
  1370. case ETH_SS_STATS:
  1371. return ARRAY_SIZE(rtl8169_gstrings);
  1372. default:
  1373. return -EOPNOTSUPP;
  1374. }
  1375. }
  1376. static void rtl8169_update_counters(struct net_device *dev)
  1377. {
  1378. struct rtl8169_private *tp = netdev_priv(dev);
  1379. void __iomem *ioaddr = tp->mmio_addr;
  1380. struct device *d = &tp->pci_dev->dev;
  1381. struct rtl8169_counters *counters;
  1382. dma_addr_t paddr;
  1383. u32 cmd;
  1384. int wait = 1000;
  1385. /*
  1386. * Some chips are unable to dump tally counters when the receiver
  1387. * is disabled.
  1388. */
  1389. if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
  1390. return;
  1391. counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
  1392. if (!counters)
  1393. return;
  1394. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  1395. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1396. RTL_W32(CounterAddrLow, cmd);
  1397. RTL_W32(CounterAddrLow, cmd | CounterDump);
  1398. while (wait--) {
  1399. if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
  1400. memcpy(&tp->counters, counters, sizeof(*counters));
  1401. break;
  1402. }
  1403. udelay(10);
  1404. }
  1405. RTL_W32(CounterAddrLow, 0);
  1406. RTL_W32(CounterAddrHigh, 0);
  1407. dma_free_coherent(d, sizeof(*counters), counters, paddr);
  1408. }
  1409. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1410. struct ethtool_stats *stats, u64 *data)
  1411. {
  1412. struct rtl8169_private *tp = netdev_priv(dev);
  1413. ASSERT_RTNL();
  1414. rtl8169_update_counters(dev);
  1415. data[0] = le64_to_cpu(tp->counters.tx_packets);
  1416. data[1] = le64_to_cpu(tp->counters.rx_packets);
  1417. data[2] = le64_to_cpu(tp->counters.tx_errors);
  1418. data[3] = le32_to_cpu(tp->counters.rx_errors);
  1419. data[4] = le16_to_cpu(tp->counters.rx_missed);
  1420. data[5] = le16_to_cpu(tp->counters.align_errors);
  1421. data[6] = le32_to_cpu(tp->counters.tx_one_collision);
  1422. data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
  1423. data[8] = le64_to_cpu(tp->counters.rx_unicast);
  1424. data[9] = le64_to_cpu(tp->counters.rx_broadcast);
  1425. data[10] = le32_to_cpu(tp->counters.rx_multicast);
  1426. data[11] = le16_to_cpu(tp->counters.tx_aborted);
  1427. data[12] = le16_to_cpu(tp->counters.tx_underun);
  1428. }
  1429. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1430. {
  1431. switch(stringset) {
  1432. case ETH_SS_STATS:
  1433. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1434. break;
  1435. }
  1436. }
  1437. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1438. .get_drvinfo = rtl8169_get_drvinfo,
  1439. .get_regs_len = rtl8169_get_regs_len,
  1440. .get_link = ethtool_op_get_link,
  1441. .get_settings = rtl8169_get_settings,
  1442. .set_settings = rtl8169_set_settings,
  1443. .get_msglevel = rtl8169_get_msglevel,
  1444. .set_msglevel = rtl8169_set_msglevel,
  1445. .get_regs = rtl8169_get_regs,
  1446. .get_wol = rtl8169_get_wol,
  1447. .set_wol = rtl8169_set_wol,
  1448. .get_strings = rtl8169_get_strings,
  1449. .get_sset_count = rtl8169_get_sset_count,
  1450. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1451. };
  1452. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1453. struct net_device *dev, u8 default_version)
  1454. {
  1455. void __iomem *ioaddr = tp->mmio_addr;
  1456. /*
  1457. * The driver currently handles the 8168Bf and the 8168Be identically
  1458. * but they can be identified more specifically through the test below
  1459. * if needed:
  1460. *
  1461. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1462. *
  1463. * Same thing for the 8101Eb and the 8101Ec:
  1464. *
  1465. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1466. */
  1467. static const struct rtl_mac_info {
  1468. u32 mask;
  1469. u32 val;
  1470. int mac_version;
  1471. } mac_info[] = {
  1472. /* 8168E family. */
  1473. { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
  1474. { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
  1475. { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
  1476. { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
  1477. /* 8168D family. */
  1478. { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
  1479. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  1480. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  1481. /* 8168DP family. */
  1482. { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  1483. { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
  1484. { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
  1485. /* 8168C family. */
  1486. { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
  1487. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  1488. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  1489. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  1490. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  1491. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  1492. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  1493. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  1494. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  1495. /* 8168B family. */
  1496. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  1497. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  1498. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  1499. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  1500. /* 8101 family. */
  1501. { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
  1502. { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
  1503. { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
  1504. { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
  1505. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  1506. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  1507. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  1508. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  1509. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  1510. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  1511. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  1512. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  1513. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  1514. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  1515. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  1516. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  1517. /* FIXME: where did these entries come from ? -- FR */
  1518. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  1519. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  1520. /* 8110 family. */
  1521. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  1522. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  1523. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  1524. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  1525. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  1526. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  1527. /* Catch-all */
  1528. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  1529. };
  1530. const struct rtl_mac_info *p = mac_info;
  1531. u32 reg;
  1532. reg = RTL_R32(TxConfig);
  1533. while ((reg & p->mask) != p->val)
  1534. p++;
  1535. tp->mac_version = p->mac_version;
  1536. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  1537. netif_notice(tp, probe, dev,
  1538. "unknown MAC, using family default\n");
  1539. tp->mac_version = default_version;
  1540. }
  1541. }
  1542. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  1543. {
  1544. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  1545. }
  1546. struct phy_reg {
  1547. u16 reg;
  1548. u16 val;
  1549. };
  1550. static void rtl_writephy_batch(struct rtl8169_private *tp,
  1551. const struct phy_reg *regs, int len)
  1552. {
  1553. while (len-- > 0) {
  1554. rtl_writephy(tp, regs->reg, regs->val);
  1555. regs++;
  1556. }
  1557. }
  1558. #define PHY_READ 0x00000000
  1559. #define PHY_DATA_OR 0x10000000
  1560. #define PHY_DATA_AND 0x20000000
  1561. #define PHY_BJMPN 0x30000000
  1562. #define PHY_READ_EFUSE 0x40000000
  1563. #define PHY_READ_MAC_BYTE 0x50000000
  1564. #define PHY_WRITE_MAC_BYTE 0x60000000
  1565. #define PHY_CLEAR_READCOUNT 0x70000000
  1566. #define PHY_WRITE 0x80000000
  1567. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  1568. #define PHY_COMP_EQ_SKIPN 0xa0000000
  1569. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  1570. #define PHY_WRITE_PREVIOUS 0xc0000000
  1571. #define PHY_SKIPN 0xd0000000
  1572. #define PHY_DELAY_MS 0xe0000000
  1573. #define PHY_WRITE_ERI_WORD 0xf0000000
  1574. struct fw_info {
  1575. u32 magic;
  1576. char version[RTL_VER_SIZE];
  1577. __le32 fw_start;
  1578. __le32 fw_len;
  1579. u8 chksum;
  1580. } __packed;
  1581. #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
  1582. static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1583. {
  1584. const struct firmware *fw = rtl_fw->fw;
  1585. struct fw_info *fw_info = (struct fw_info *)fw->data;
  1586. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  1587. char *version = rtl_fw->version;
  1588. bool rc = false;
  1589. if (fw->size < FW_OPCODE_SIZE)
  1590. goto out;
  1591. if (!fw_info->magic) {
  1592. size_t i, size, start;
  1593. u8 checksum = 0;
  1594. if (fw->size < sizeof(*fw_info))
  1595. goto out;
  1596. for (i = 0; i < fw->size; i++)
  1597. checksum += fw->data[i];
  1598. if (checksum != 0)
  1599. goto out;
  1600. start = le32_to_cpu(fw_info->fw_start);
  1601. if (start > fw->size)
  1602. goto out;
  1603. size = le32_to_cpu(fw_info->fw_len);
  1604. if (size > (fw->size - start) / FW_OPCODE_SIZE)
  1605. goto out;
  1606. memcpy(version, fw_info->version, RTL_VER_SIZE);
  1607. pa->code = (__le32 *)(fw->data + start);
  1608. pa->size = size;
  1609. } else {
  1610. if (fw->size % FW_OPCODE_SIZE)
  1611. goto out;
  1612. strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
  1613. pa->code = (__le32 *)fw->data;
  1614. pa->size = fw->size / FW_OPCODE_SIZE;
  1615. }
  1616. version[RTL_VER_SIZE - 1] = 0;
  1617. rc = true;
  1618. out:
  1619. return rc;
  1620. }
  1621. static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
  1622. struct rtl_fw_phy_action *pa)
  1623. {
  1624. bool rc = false;
  1625. size_t index;
  1626. for (index = 0; index < pa->size; index++) {
  1627. u32 action = le32_to_cpu(pa->code[index]);
  1628. u32 regno = (action & 0x0fff0000) >> 16;
  1629. switch(action & 0xf0000000) {
  1630. case PHY_READ:
  1631. case PHY_DATA_OR:
  1632. case PHY_DATA_AND:
  1633. case PHY_READ_EFUSE:
  1634. case PHY_CLEAR_READCOUNT:
  1635. case PHY_WRITE:
  1636. case PHY_WRITE_PREVIOUS:
  1637. case PHY_DELAY_MS:
  1638. break;
  1639. case PHY_BJMPN:
  1640. if (regno > index) {
  1641. netif_err(tp, ifup, tp->dev,
  1642. "Out of range of firmware\n");
  1643. goto out;
  1644. }
  1645. break;
  1646. case PHY_READCOUNT_EQ_SKIP:
  1647. if (index + 2 >= pa->size) {
  1648. netif_err(tp, ifup, tp->dev,
  1649. "Out of range of firmware\n");
  1650. goto out;
  1651. }
  1652. break;
  1653. case PHY_COMP_EQ_SKIPN:
  1654. case PHY_COMP_NEQ_SKIPN:
  1655. case PHY_SKIPN:
  1656. if (index + 1 + regno >= pa->size) {
  1657. netif_err(tp, ifup, tp->dev,
  1658. "Out of range of firmware\n");
  1659. goto out;
  1660. }
  1661. break;
  1662. case PHY_READ_MAC_BYTE:
  1663. case PHY_WRITE_MAC_BYTE:
  1664. case PHY_WRITE_ERI_WORD:
  1665. default:
  1666. netif_err(tp, ifup, tp->dev,
  1667. "Invalid action 0x%08x\n", action);
  1668. goto out;
  1669. }
  1670. }
  1671. rc = true;
  1672. out:
  1673. return rc;
  1674. }
  1675. static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1676. {
  1677. struct net_device *dev = tp->dev;
  1678. int rc = -EINVAL;
  1679. if (!rtl_fw_format_ok(tp, rtl_fw)) {
  1680. netif_err(tp, ifup, dev, "invalid firwmare\n");
  1681. goto out;
  1682. }
  1683. if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
  1684. rc = 0;
  1685. out:
  1686. return rc;
  1687. }
  1688. static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  1689. {
  1690. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  1691. u32 predata, count;
  1692. size_t index;
  1693. predata = count = 0;
  1694. for (index = 0; index < pa->size; ) {
  1695. u32 action = le32_to_cpu(pa->code[index]);
  1696. u32 data = action & 0x0000ffff;
  1697. u32 regno = (action & 0x0fff0000) >> 16;
  1698. if (!action)
  1699. break;
  1700. switch(action & 0xf0000000) {
  1701. case PHY_READ:
  1702. predata = rtl_readphy(tp, regno);
  1703. count++;
  1704. index++;
  1705. break;
  1706. case PHY_DATA_OR:
  1707. predata |= data;
  1708. index++;
  1709. break;
  1710. case PHY_DATA_AND:
  1711. predata &= data;
  1712. index++;
  1713. break;
  1714. case PHY_BJMPN:
  1715. index -= regno;
  1716. break;
  1717. case PHY_READ_EFUSE:
  1718. predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
  1719. index++;
  1720. break;
  1721. case PHY_CLEAR_READCOUNT:
  1722. count = 0;
  1723. index++;
  1724. break;
  1725. case PHY_WRITE:
  1726. rtl_writephy(tp, regno, data);
  1727. index++;
  1728. break;
  1729. case PHY_READCOUNT_EQ_SKIP:
  1730. index += (count == data) ? 2 : 1;
  1731. break;
  1732. case PHY_COMP_EQ_SKIPN:
  1733. if (predata == data)
  1734. index += regno;
  1735. index++;
  1736. break;
  1737. case PHY_COMP_NEQ_SKIPN:
  1738. if (predata != data)
  1739. index += regno;
  1740. index++;
  1741. break;
  1742. case PHY_WRITE_PREVIOUS:
  1743. rtl_writephy(tp, regno, predata);
  1744. index++;
  1745. break;
  1746. case PHY_SKIPN:
  1747. index += regno + 1;
  1748. break;
  1749. case PHY_DELAY_MS:
  1750. mdelay(data);
  1751. index++;
  1752. break;
  1753. case PHY_READ_MAC_BYTE:
  1754. case PHY_WRITE_MAC_BYTE:
  1755. case PHY_WRITE_ERI_WORD:
  1756. default:
  1757. BUG();
  1758. }
  1759. }
  1760. }
  1761. static void rtl_release_firmware(struct rtl8169_private *tp)
  1762. {
  1763. if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
  1764. release_firmware(tp->rtl_fw->fw);
  1765. kfree(tp->rtl_fw);
  1766. }
  1767. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  1768. }
  1769. static void rtl_apply_firmware(struct rtl8169_private *tp)
  1770. {
  1771. struct rtl_fw *rtl_fw = tp->rtl_fw;
  1772. /* TODO: release firmware once rtl_phy_write_fw signals failures. */
  1773. if (!IS_ERR_OR_NULL(rtl_fw))
  1774. rtl_phy_write_fw(tp, rtl_fw);
  1775. }
  1776. static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
  1777. {
  1778. if (rtl_readphy(tp, reg) != val)
  1779. netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
  1780. else
  1781. rtl_apply_firmware(tp);
  1782. }
  1783. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  1784. {
  1785. static const struct phy_reg phy_reg_init[] = {
  1786. { 0x1f, 0x0001 },
  1787. { 0x06, 0x006e },
  1788. { 0x08, 0x0708 },
  1789. { 0x15, 0x4000 },
  1790. { 0x18, 0x65c7 },
  1791. { 0x1f, 0x0001 },
  1792. { 0x03, 0x00a1 },
  1793. { 0x02, 0x0008 },
  1794. { 0x01, 0x0120 },
  1795. { 0x00, 0x1000 },
  1796. { 0x04, 0x0800 },
  1797. { 0x04, 0x0000 },
  1798. { 0x03, 0xff41 },
  1799. { 0x02, 0xdf60 },
  1800. { 0x01, 0x0140 },
  1801. { 0x00, 0x0077 },
  1802. { 0x04, 0x7800 },
  1803. { 0x04, 0x7000 },
  1804. { 0x03, 0x802f },
  1805. { 0x02, 0x4f02 },
  1806. { 0x01, 0x0409 },
  1807. { 0x00, 0xf0f9 },
  1808. { 0x04, 0x9800 },
  1809. { 0x04, 0x9000 },
  1810. { 0x03, 0xdf01 },
  1811. { 0x02, 0xdf20 },
  1812. { 0x01, 0xff95 },
  1813. { 0x00, 0xba00 },
  1814. { 0x04, 0xa800 },
  1815. { 0x04, 0xa000 },
  1816. { 0x03, 0xff41 },
  1817. { 0x02, 0xdf20 },
  1818. { 0x01, 0x0140 },
  1819. { 0x00, 0x00bb },
  1820. { 0x04, 0xb800 },
  1821. { 0x04, 0xb000 },
  1822. { 0x03, 0xdf41 },
  1823. { 0x02, 0xdc60 },
  1824. { 0x01, 0x6340 },
  1825. { 0x00, 0x007d },
  1826. { 0x04, 0xd800 },
  1827. { 0x04, 0xd000 },
  1828. { 0x03, 0xdf01 },
  1829. { 0x02, 0xdf20 },
  1830. { 0x01, 0x100a },
  1831. { 0x00, 0xa0ff },
  1832. { 0x04, 0xf800 },
  1833. { 0x04, 0xf000 },
  1834. { 0x1f, 0x0000 },
  1835. { 0x0b, 0x0000 },
  1836. { 0x00, 0x9200 }
  1837. };
  1838. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1839. }
  1840. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  1841. {
  1842. static const struct phy_reg phy_reg_init[] = {
  1843. { 0x1f, 0x0002 },
  1844. { 0x01, 0x90d0 },
  1845. { 0x1f, 0x0000 }
  1846. };
  1847. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1848. }
  1849. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  1850. {
  1851. struct pci_dev *pdev = tp->pci_dev;
  1852. if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
  1853. (pdev->subsystem_device != 0xe000))
  1854. return;
  1855. rtl_writephy(tp, 0x1f, 0x0001);
  1856. rtl_writephy(tp, 0x10, 0xf01b);
  1857. rtl_writephy(tp, 0x1f, 0x0000);
  1858. }
  1859. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  1860. {
  1861. static const struct phy_reg phy_reg_init[] = {
  1862. { 0x1f, 0x0001 },
  1863. { 0x04, 0x0000 },
  1864. { 0x03, 0x00a1 },
  1865. { 0x02, 0x0008 },
  1866. { 0x01, 0x0120 },
  1867. { 0x00, 0x1000 },
  1868. { 0x04, 0x0800 },
  1869. { 0x04, 0x9000 },
  1870. { 0x03, 0x802f },
  1871. { 0x02, 0x4f02 },
  1872. { 0x01, 0x0409 },
  1873. { 0x00, 0xf099 },
  1874. { 0x04, 0x9800 },
  1875. { 0x04, 0xa000 },
  1876. { 0x03, 0xdf01 },
  1877. { 0x02, 0xdf20 },
  1878. { 0x01, 0xff95 },
  1879. { 0x00, 0xba00 },
  1880. { 0x04, 0xa800 },
  1881. { 0x04, 0xf000 },
  1882. { 0x03, 0xdf01 },
  1883. { 0x02, 0xdf20 },
  1884. { 0x01, 0x101a },
  1885. { 0x00, 0xa0ff },
  1886. { 0x04, 0xf800 },
  1887. { 0x04, 0x0000 },
  1888. { 0x1f, 0x0000 },
  1889. { 0x1f, 0x0001 },
  1890. { 0x10, 0xf41b },
  1891. { 0x14, 0xfb54 },
  1892. { 0x18, 0xf5c7 },
  1893. { 0x1f, 0x0000 },
  1894. { 0x1f, 0x0001 },
  1895. { 0x17, 0x0cc0 },
  1896. { 0x1f, 0x0000 }
  1897. };
  1898. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1899. rtl8169scd_hw_phy_config_quirk(tp);
  1900. }
  1901. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  1902. {
  1903. static const struct phy_reg phy_reg_init[] = {
  1904. { 0x1f, 0x0001 },
  1905. { 0x04, 0x0000 },
  1906. { 0x03, 0x00a1 },
  1907. { 0x02, 0x0008 },
  1908. { 0x01, 0x0120 },
  1909. { 0x00, 0x1000 },
  1910. { 0x04, 0x0800 },
  1911. { 0x04, 0x9000 },
  1912. { 0x03, 0x802f },
  1913. { 0x02, 0x4f02 },
  1914. { 0x01, 0x0409 },
  1915. { 0x00, 0xf099 },
  1916. { 0x04, 0x9800 },
  1917. { 0x04, 0xa000 },
  1918. { 0x03, 0xdf01 },
  1919. { 0x02, 0xdf20 },
  1920. { 0x01, 0xff95 },
  1921. { 0x00, 0xba00 },
  1922. { 0x04, 0xa800 },
  1923. { 0x04, 0xf000 },
  1924. { 0x03, 0xdf01 },
  1925. { 0x02, 0xdf20 },
  1926. { 0x01, 0x101a },
  1927. { 0x00, 0xa0ff },
  1928. { 0x04, 0xf800 },
  1929. { 0x04, 0x0000 },
  1930. { 0x1f, 0x0000 },
  1931. { 0x1f, 0x0001 },
  1932. { 0x0b, 0x8480 },
  1933. { 0x1f, 0x0000 },
  1934. { 0x1f, 0x0001 },
  1935. { 0x18, 0x67c7 },
  1936. { 0x04, 0x2000 },
  1937. { 0x03, 0x002f },
  1938. { 0x02, 0x4360 },
  1939. { 0x01, 0x0109 },
  1940. { 0x00, 0x3022 },
  1941. { 0x04, 0x2800 },
  1942. { 0x1f, 0x0000 },
  1943. { 0x1f, 0x0001 },
  1944. { 0x17, 0x0cc0 },
  1945. { 0x1f, 0x0000 }
  1946. };
  1947. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1948. }
  1949. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  1950. {
  1951. static const struct phy_reg phy_reg_init[] = {
  1952. { 0x10, 0xf41b },
  1953. { 0x1f, 0x0000 }
  1954. };
  1955. rtl_writephy(tp, 0x1f, 0x0001);
  1956. rtl_patchphy(tp, 0x16, 1 << 0);
  1957. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1958. }
  1959. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  1960. {
  1961. static const struct phy_reg phy_reg_init[] = {
  1962. { 0x1f, 0x0001 },
  1963. { 0x10, 0xf41b },
  1964. { 0x1f, 0x0000 }
  1965. };
  1966. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1967. }
  1968. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  1969. {
  1970. static const struct phy_reg phy_reg_init[] = {
  1971. { 0x1f, 0x0000 },
  1972. { 0x1d, 0x0f00 },
  1973. { 0x1f, 0x0002 },
  1974. { 0x0c, 0x1ec8 },
  1975. { 0x1f, 0x0000 }
  1976. };
  1977. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1978. }
  1979. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  1980. {
  1981. static const struct phy_reg phy_reg_init[] = {
  1982. { 0x1f, 0x0001 },
  1983. { 0x1d, 0x3d98 },
  1984. { 0x1f, 0x0000 }
  1985. };
  1986. rtl_writephy(tp, 0x1f, 0x0000);
  1987. rtl_patchphy(tp, 0x14, 1 << 5);
  1988. rtl_patchphy(tp, 0x0d, 1 << 5);
  1989. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1990. }
  1991. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  1992. {
  1993. static const struct phy_reg phy_reg_init[] = {
  1994. { 0x1f, 0x0001 },
  1995. { 0x12, 0x2300 },
  1996. { 0x1f, 0x0002 },
  1997. { 0x00, 0x88d4 },
  1998. { 0x01, 0x82b1 },
  1999. { 0x03, 0x7002 },
  2000. { 0x08, 0x9e30 },
  2001. { 0x09, 0x01f0 },
  2002. { 0x0a, 0x5500 },
  2003. { 0x0c, 0x00c8 },
  2004. { 0x1f, 0x0003 },
  2005. { 0x12, 0xc096 },
  2006. { 0x16, 0x000a },
  2007. { 0x1f, 0x0000 },
  2008. { 0x1f, 0x0000 },
  2009. { 0x09, 0x2000 },
  2010. { 0x09, 0x0000 }
  2011. };
  2012. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2013. rtl_patchphy(tp, 0x14, 1 << 5);
  2014. rtl_patchphy(tp, 0x0d, 1 << 5);
  2015. rtl_writephy(tp, 0x1f, 0x0000);
  2016. }
  2017. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  2018. {
  2019. static const struct phy_reg phy_reg_init[] = {
  2020. { 0x1f, 0x0001 },
  2021. { 0x12, 0x2300 },
  2022. { 0x03, 0x802f },
  2023. { 0x02, 0x4f02 },
  2024. { 0x01, 0x0409 },
  2025. { 0x00, 0xf099 },
  2026. { 0x04, 0x9800 },
  2027. { 0x04, 0x9000 },
  2028. { 0x1d, 0x3d98 },
  2029. { 0x1f, 0x0002 },
  2030. { 0x0c, 0x7eb8 },
  2031. { 0x06, 0x0761 },
  2032. { 0x1f, 0x0003 },
  2033. { 0x16, 0x0f0a },
  2034. { 0x1f, 0x0000 }
  2035. };
  2036. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2037. rtl_patchphy(tp, 0x16, 1 << 0);
  2038. rtl_patchphy(tp, 0x14, 1 << 5);
  2039. rtl_patchphy(tp, 0x0d, 1 << 5);
  2040. rtl_writephy(tp, 0x1f, 0x0000);
  2041. }
  2042. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  2043. {
  2044. static const struct phy_reg phy_reg_init[] = {
  2045. { 0x1f, 0x0001 },
  2046. { 0x12, 0x2300 },
  2047. { 0x1d, 0x3d98 },
  2048. { 0x1f, 0x0002 },
  2049. { 0x0c, 0x7eb8 },
  2050. { 0x06, 0x5461 },
  2051. { 0x1f, 0x0003 },
  2052. { 0x16, 0x0f0a },
  2053. { 0x1f, 0x0000 }
  2054. };
  2055. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2056. rtl_patchphy(tp, 0x16, 1 << 0);
  2057. rtl_patchphy(tp, 0x14, 1 << 5);
  2058. rtl_patchphy(tp, 0x0d, 1 << 5);
  2059. rtl_writephy(tp, 0x1f, 0x0000);
  2060. }
  2061. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  2062. {
  2063. rtl8168c_3_hw_phy_config(tp);
  2064. }
  2065. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  2066. {
  2067. static const struct phy_reg phy_reg_init_0[] = {
  2068. /* Channel Estimation */
  2069. { 0x1f, 0x0001 },
  2070. { 0x06, 0x4064 },
  2071. { 0x07, 0x2863 },
  2072. { 0x08, 0x059c },
  2073. { 0x09, 0x26b4 },
  2074. { 0x0a, 0x6a19 },
  2075. { 0x0b, 0xdcc8 },
  2076. { 0x10, 0xf06d },
  2077. { 0x14, 0x7f68 },
  2078. { 0x18, 0x7fd9 },
  2079. { 0x1c, 0xf0ff },
  2080. { 0x1d, 0x3d9c },
  2081. { 0x1f, 0x0003 },
  2082. { 0x12, 0xf49f },
  2083. { 0x13, 0x070b },
  2084. { 0x1a, 0x05ad },
  2085. { 0x14, 0x94c0 },
  2086. /*
  2087. * Tx Error Issue
  2088. * Enhance line driver power
  2089. */
  2090. { 0x1f, 0x0002 },
  2091. { 0x06, 0x5561 },
  2092. { 0x1f, 0x0005 },
  2093. { 0x05, 0x8332 },
  2094. { 0x06, 0x5561 },
  2095. /*
  2096. * Can not link to 1Gbps with bad cable
  2097. * Decrease SNR threshold form 21.07dB to 19.04dB
  2098. */
  2099. { 0x1f, 0x0001 },
  2100. { 0x17, 0x0cc0 },
  2101. { 0x1f, 0x0000 },
  2102. { 0x0d, 0xf880 }
  2103. };
  2104. void __iomem *ioaddr = tp->mmio_addr;
  2105. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2106. /*
  2107. * Rx Error Issue
  2108. * Fine Tune Switching regulator parameter
  2109. */
  2110. rtl_writephy(tp, 0x1f, 0x0002);
  2111. rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
  2112. rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
  2113. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  2114. static const struct phy_reg phy_reg_init[] = {
  2115. { 0x1f, 0x0002 },
  2116. { 0x05, 0x669a },
  2117. { 0x1f, 0x0005 },
  2118. { 0x05, 0x8330 },
  2119. { 0x06, 0x669a },
  2120. { 0x1f, 0x0002 }
  2121. };
  2122. int val;
  2123. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2124. val = rtl_readphy(tp, 0x0d);
  2125. if ((val & 0x00ff) != 0x006c) {
  2126. static const u32 set[] = {
  2127. 0x0065, 0x0066, 0x0067, 0x0068,
  2128. 0x0069, 0x006a, 0x006b, 0x006c
  2129. };
  2130. int i;
  2131. rtl_writephy(tp, 0x1f, 0x0002);
  2132. val &= 0xff00;
  2133. for (i = 0; i < ARRAY_SIZE(set); i++)
  2134. rtl_writephy(tp, 0x0d, val | set[i]);
  2135. }
  2136. } else {
  2137. static const struct phy_reg phy_reg_init[] = {
  2138. { 0x1f, 0x0002 },
  2139. { 0x05, 0x6662 },
  2140. { 0x1f, 0x0005 },
  2141. { 0x05, 0x8330 },
  2142. { 0x06, 0x6662 }
  2143. };
  2144. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2145. }
  2146. /* RSET couple improve */
  2147. rtl_writephy(tp, 0x1f, 0x0002);
  2148. rtl_patchphy(tp, 0x0d, 0x0300);
  2149. rtl_patchphy(tp, 0x0f, 0x0010);
  2150. /* Fine tune PLL performance */
  2151. rtl_writephy(tp, 0x1f, 0x0002);
  2152. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  2153. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  2154. rtl_writephy(tp, 0x1f, 0x0005);
  2155. rtl_writephy(tp, 0x05, 0x001b);
  2156. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
  2157. rtl_writephy(tp, 0x1f, 0x0000);
  2158. }
  2159. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  2160. {
  2161. static const struct phy_reg phy_reg_init_0[] = {
  2162. /* Channel Estimation */
  2163. { 0x1f, 0x0001 },
  2164. { 0x06, 0x4064 },
  2165. { 0x07, 0x2863 },
  2166. { 0x08, 0x059c },
  2167. { 0x09, 0x26b4 },
  2168. { 0x0a, 0x6a19 },
  2169. { 0x0b, 0xdcc8 },
  2170. { 0x10, 0xf06d },
  2171. { 0x14, 0x7f68 },
  2172. { 0x18, 0x7fd9 },
  2173. { 0x1c, 0xf0ff },
  2174. { 0x1d, 0x3d9c },
  2175. { 0x1f, 0x0003 },
  2176. { 0x12, 0xf49f },
  2177. { 0x13, 0x070b },
  2178. { 0x1a, 0x05ad },
  2179. { 0x14, 0x94c0 },
  2180. /*
  2181. * Tx Error Issue
  2182. * Enhance line driver power
  2183. */
  2184. { 0x1f, 0x0002 },
  2185. { 0x06, 0x5561 },
  2186. { 0x1f, 0x0005 },
  2187. { 0x05, 0x8332 },
  2188. { 0x06, 0x5561 },
  2189. /*
  2190. * Can not link to 1Gbps with bad cable
  2191. * Decrease SNR threshold form 21.07dB to 19.04dB
  2192. */
  2193. { 0x1f, 0x0001 },
  2194. { 0x17, 0x0cc0 },
  2195. { 0x1f, 0x0000 },
  2196. { 0x0d, 0xf880 }
  2197. };
  2198. void __iomem *ioaddr = tp->mmio_addr;
  2199. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2200. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  2201. static const struct phy_reg phy_reg_init[] = {
  2202. { 0x1f, 0x0002 },
  2203. { 0x05, 0x669a },
  2204. { 0x1f, 0x0005 },
  2205. { 0x05, 0x8330 },
  2206. { 0x06, 0x669a },
  2207. { 0x1f, 0x0002 }
  2208. };
  2209. int val;
  2210. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2211. val = rtl_readphy(tp, 0x0d);
  2212. if ((val & 0x00ff) != 0x006c) {
  2213. static const u32 set[] = {
  2214. 0x0065, 0x0066, 0x0067, 0x0068,
  2215. 0x0069, 0x006a, 0x006b, 0x006c
  2216. };
  2217. int i;
  2218. rtl_writephy(tp, 0x1f, 0x0002);
  2219. val &= 0xff00;
  2220. for (i = 0; i < ARRAY_SIZE(set); i++)
  2221. rtl_writephy(tp, 0x0d, val | set[i]);
  2222. }
  2223. } else {
  2224. static const struct phy_reg phy_reg_init[] = {
  2225. { 0x1f, 0x0002 },
  2226. { 0x05, 0x2642 },
  2227. { 0x1f, 0x0005 },
  2228. { 0x05, 0x8330 },
  2229. { 0x06, 0x2642 }
  2230. };
  2231. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2232. }
  2233. /* Fine tune PLL performance */
  2234. rtl_writephy(tp, 0x1f, 0x0002);
  2235. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  2236. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  2237. /* Switching regulator Slew rate */
  2238. rtl_writephy(tp, 0x1f, 0x0002);
  2239. rtl_patchphy(tp, 0x0f, 0x0017);
  2240. rtl_writephy(tp, 0x1f, 0x0005);
  2241. rtl_writephy(tp, 0x05, 0x001b);
  2242. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
  2243. rtl_writephy(tp, 0x1f, 0x0000);
  2244. }
  2245. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  2246. {
  2247. static const struct phy_reg phy_reg_init[] = {
  2248. { 0x1f, 0x0002 },
  2249. { 0x10, 0x0008 },
  2250. { 0x0d, 0x006c },
  2251. { 0x1f, 0x0000 },
  2252. { 0x0d, 0xf880 },
  2253. { 0x1f, 0x0001 },
  2254. { 0x17, 0x0cc0 },
  2255. { 0x1f, 0x0001 },
  2256. { 0x0b, 0xa4d8 },
  2257. { 0x09, 0x281c },
  2258. { 0x07, 0x2883 },
  2259. { 0x0a, 0x6b35 },
  2260. { 0x1d, 0x3da4 },
  2261. { 0x1c, 0xeffd },
  2262. { 0x14, 0x7f52 },
  2263. { 0x18, 0x7fc6 },
  2264. { 0x08, 0x0601 },
  2265. { 0x06, 0x4063 },
  2266. { 0x10, 0xf074 },
  2267. { 0x1f, 0x0003 },
  2268. { 0x13, 0x0789 },
  2269. { 0x12, 0xf4bd },
  2270. { 0x1a, 0x04fd },
  2271. { 0x14, 0x84b0 },
  2272. { 0x1f, 0x0000 },
  2273. { 0x00, 0x9200 },
  2274. { 0x1f, 0x0005 },
  2275. { 0x01, 0x0340 },
  2276. { 0x1f, 0x0001 },
  2277. { 0x04, 0x4000 },
  2278. { 0x03, 0x1d21 },
  2279. { 0x02, 0x0c32 },
  2280. { 0x01, 0x0200 },
  2281. { 0x00, 0x5554 },
  2282. { 0x04, 0x4800 },
  2283. { 0x04, 0x4000 },
  2284. { 0x04, 0xf000 },
  2285. { 0x03, 0xdf01 },
  2286. { 0x02, 0xdf20 },
  2287. { 0x01, 0x101a },
  2288. { 0x00, 0xa0ff },
  2289. { 0x04, 0xf800 },
  2290. { 0x04, 0xf000 },
  2291. { 0x1f, 0x0000 },
  2292. { 0x1f, 0x0007 },
  2293. { 0x1e, 0x0023 },
  2294. { 0x16, 0x0000 },
  2295. { 0x1f, 0x0000 }
  2296. };
  2297. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2298. }
  2299. static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
  2300. {
  2301. static const struct phy_reg phy_reg_init[] = {
  2302. { 0x1f, 0x0001 },
  2303. { 0x17, 0x0cc0 },
  2304. { 0x1f, 0x0007 },
  2305. { 0x1e, 0x002d },
  2306. { 0x18, 0x0040 },
  2307. { 0x1f, 0x0000 }
  2308. };
  2309. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2310. rtl_patchphy(tp, 0x0d, 1 << 5);
  2311. }
  2312. static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
  2313. {
  2314. static const struct phy_reg phy_reg_init[] = {
  2315. /* Enable Delay cap */
  2316. { 0x1f, 0x0005 },
  2317. { 0x05, 0x8b80 },
  2318. { 0x06, 0xc896 },
  2319. { 0x1f, 0x0000 },
  2320. /* Channel estimation fine tune */
  2321. { 0x1f, 0x0001 },
  2322. { 0x0b, 0x6c20 },
  2323. { 0x07, 0x2872 },
  2324. { 0x1c, 0xefff },
  2325. { 0x1f, 0x0003 },
  2326. { 0x14, 0x6420 },
  2327. { 0x1f, 0x0000 },
  2328. /* Update PFM & 10M TX idle timer */
  2329. { 0x1f, 0x0007 },
  2330. { 0x1e, 0x002f },
  2331. { 0x15, 0x1919 },
  2332. { 0x1f, 0x0000 },
  2333. { 0x1f, 0x0007 },
  2334. { 0x1e, 0x00ac },
  2335. { 0x18, 0x0006 },
  2336. { 0x1f, 0x0000 }
  2337. };
  2338. rtl_apply_firmware(tp);
  2339. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2340. /* DCO enable for 10M IDLE Power */
  2341. rtl_writephy(tp, 0x1f, 0x0007);
  2342. rtl_writephy(tp, 0x1e, 0x0023);
  2343. rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
  2344. rtl_writephy(tp, 0x1f, 0x0000);
  2345. /* For impedance matching */
  2346. rtl_writephy(tp, 0x1f, 0x0002);
  2347. rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
  2348. rtl_writephy(tp, 0x1f, 0x0000);
  2349. /* PHY auto speed down */
  2350. rtl_writephy(tp, 0x1f, 0x0007);
  2351. rtl_writephy(tp, 0x1e, 0x002d);
  2352. rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
  2353. rtl_writephy(tp, 0x1f, 0x0000);
  2354. rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
  2355. rtl_writephy(tp, 0x1f, 0x0005);
  2356. rtl_writephy(tp, 0x05, 0x8b86);
  2357. rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
  2358. rtl_writephy(tp, 0x1f, 0x0000);
  2359. rtl_writephy(tp, 0x1f, 0x0005);
  2360. rtl_writephy(tp, 0x05, 0x8b85);
  2361. rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
  2362. rtl_writephy(tp, 0x1f, 0x0007);
  2363. rtl_writephy(tp, 0x1e, 0x0020);
  2364. rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
  2365. rtl_writephy(tp, 0x1f, 0x0006);
  2366. rtl_writephy(tp, 0x00, 0x5a00);
  2367. rtl_writephy(tp, 0x1f, 0x0000);
  2368. rtl_writephy(tp, 0x0d, 0x0007);
  2369. rtl_writephy(tp, 0x0e, 0x003c);
  2370. rtl_writephy(tp, 0x0d, 0x4007);
  2371. rtl_writephy(tp, 0x0e, 0x0000);
  2372. rtl_writephy(tp, 0x0d, 0x0000);
  2373. }
  2374. static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
  2375. {
  2376. static const struct phy_reg phy_reg_init[] = {
  2377. /* Enable Delay cap */
  2378. { 0x1f, 0x0004 },
  2379. { 0x1f, 0x0007 },
  2380. { 0x1e, 0x00ac },
  2381. { 0x18, 0x0006 },
  2382. { 0x1f, 0x0002 },
  2383. { 0x1f, 0x0000 },
  2384. { 0x1f, 0x0000 },
  2385. /* Channel estimation fine tune */
  2386. { 0x1f, 0x0003 },
  2387. { 0x09, 0xa20f },
  2388. { 0x1f, 0x0000 },
  2389. { 0x1f, 0x0000 },
  2390. /* Green Setting */
  2391. { 0x1f, 0x0005 },
  2392. { 0x05, 0x8b5b },
  2393. { 0x06, 0x9222 },
  2394. { 0x05, 0x8b6d },
  2395. { 0x06, 0x8000 },
  2396. { 0x05, 0x8b76 },
  2397. { 0x06, 0x8000 },
  2398. { 0x1f, 0x0000 }
  2399. };
  2400. rtl_apply_firmware(tp);
  2401. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2402. /* For 4-corner performance improve */
  2403. rtl_writephy(tp, 0x1f, 0x0005);
  2404. rtl_writephy(tp, 0x05, 0x8b80);
  2405. rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
  2406. rtl_writephy(tp, 0x1f, 0x0000);
  2407. /* PHY auto speed down */
  2408. rtl_writephy(tp, 0x1f, 0x0004);
  2409. rtl_writephy(tp, 0x1f, 0x0007);
  2410. rtl_writephy(tp, 0x1e, 0x002d);
  2411. rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
  2412. rtl_writephy(tp, 0x1f, 0x0002);
  2413. rtl_writephy(tp, 0x1f, 0x0000);
  2414. rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
  2415. /* improve 10M EEE waveform */
  2416. rtl_writephy(tp, 0x1f, 0x0005);
  2417. rtl_writephy(tp, 0x05, 0x8b86);
  2418. rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
  2419. rtl_writephy(tp, 0x1f, 0x0000);
  2420. /* Improve 2-pair detection performance */
  2421. rtl_writephy(tp, 0x1f, 0x0005);
  2422. rtl_writephy(tp, 0x05, 0x8b85);
  2423. rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
  2424. rtl_writephy(tp, 0x1f, 0x0000);
  2425. /* EEE setting */
  2426. rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
  2427. ERIAR_EXGMAC);
  2428. rtl_writephy(tp, 0x1f, 0x0005);
  2429. rtl_writephy(tp, 0x05, 0x8b85);
  2430. rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
  2431. rtl_writephy(tp, 0x1f, 0x0004);
  2432. rtl_writephy(tp, 0x1f, 0x0007);
  2433. rtl_writephy(tp, 0x1e, 0x0020);
  2434. rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
  2435. rtl_writephy(tp, 0x1f, 0x0002);
  2436. rtl_writephy(tp, 0x1f, 0x0000);
  2437. rtl_writephy(tp, 0x0d, 0x0007);
  2438. rtl_writephy(tp, 0x0e, 0x003c);
  2439. rtl_writephy(tp, 0x0d, 0x4007);
  2440. rtl_writephy(tp, 0x0e, 0x0000);
  2441. rtl_writephy(tp, 0x0d, 0x0000);
  2442. /* Green feature */
  2443. rtl_writephy(tp, 0x1f, 0x0003);
  2444. rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
  2445. rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
  2446. rtl_writephy(tp, 0x1f, 0x0000);
  2447. }
  2448. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  2449. {
  2450. static const struct phy_reg phy_reg_init[] = {
  2451. { 0x1f, 0x0003 },
  2452. { 0x08, 0x441d },
  2453. { 0x01, 0x9100 },
  2454. { 0x1f, 0x0000 }
  2455. };
  2456. rtl_writephy(tp, 0x1f, 0x0000);
  2457. rtl_patchphy(tp, 0x11, 1 << 12);
  2458. rtl_patchphy(tp, 0x19, 1 << 13);
  2459. rtl_patchphy(tp, 0x10, 1 << 15);
  2460. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2461. }
  2462. static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
  2463. {
  2464. static const struct phy_reg phy_reg_init[] = {
  2465. { 0x1f, 0x0005 },
  2466. { 0x1a, 0x0000 },
  2467. { 0x1f, 0x0000 },
  2468. { 0x1f, 0x0004 },
  2469. { 0x1c, 0x0000 },
  2470. { 0x1f, 0x0000 },
  2471. { 0x1f, 0x0001 },
  2472. { 0x15, 0x7701 },
  2473. { 0x1f, 0x0000 }
  2474. };
  2475. /* Disable ALDPS before ram code */
  2476. rtl_writephy(tp, 0x1f, 0x0000);
  2477. rtl_writephy(tp, 0x18, 0x0310);
  2478. msleep(100);
  2479. rtl_apply_firmware(tp);
  2480. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2481. }
  2482. static void rtl_hw_phy_config(struct net_device *dev)
  2483. {
  2484. struct rtl8169_private *tp = netdev_priv(dev);
  2485. rtl8169_print_mac_version(tp);
  2486. switch (tp->mac_version) {
  2487. case RTL_GIGA_MAC_VER_01:
  2488. break;
  2489. case RTL_GIGA_MAC_VER_02:
  2490. case RTL_GIGA_MAC_VER_03:
  2491. rtl8169s_hw_phy_config(tp);
  2492. break;
  2493. case RTL_GIGA_MAC_VER_04:
  2494. rtl8169sb_hw_phy_config(tp);
  2495. break;
  2496. case RTL_GIGA_MAC_VER_05:
  2497. rtl8169scd_hw_phy_config(tp);
  2498. break;
  2499. case RTL_GIGA_MAC_VER_06:
  2500. rtl8169sce_hw_phy_config(tp);
  2501. break;
  2502. case RTL_GIGA_MAC_VER_07:
  2503. case RTL_GIGA_MAC_VER_08:
  2504. case RTL_GIGA_MAC_VER_09:
  2505. rtl8102e_hw_phy_config(tp);
  2506. break;
  2507. case RTL_GIGA_MAC_VER_11:
  2508. rtl8168bb_hw_phy_config(tp);
  2509. break;
  2510. case RTL_GIGA_MAC_VER_12:
  2511. rtl8168bef_hw_phy_config(tp);
  2512. break;
  2513. case RTL_GIGA_MAC_VER_17:
  2514. rtl8168bef_hw_phy_config(tp);
  2515. break;
  2516. case RTL_GIGA_MAC_VER_18:
  2517. rtl8168cp_1_hw_phy_config(tp);
  2518. break;
  2519. case RTL_GIGA_MAC_VER_19:
  2520. rtl8168c_1_hw_phy_config(tp);
  2521. break;
  2522. case RTL_GIGA_MAC_VER_20:
  2523. rtl8168c_2_hw_phy_config(tp);
  2524. break;
  2525. case RTL_GIGA_MAC_VER_21:
  2526. rtl8168c_3_hw_phy_config(tp);
  2527. break;
  2528. case RTL_GIGA_MAC_VER_22:
  2529. rtl8168c_4_hw_phy_config(tp);
  2530. break;
  2531. case RTL_GIGA_MAC_VER_23:
  2532. case RTL_GIGA_MAC_VER_24:
  2533. rtl8168cp_2_hw_phy_config(tp);
  2534. break;
  2535. case RTL_GIGA_MAC_VER_25:
  2536. rtl8168d_1_hw_phy_config(tp);
  2537. break;
  2538. case RTL_GIGA_MAC_VER_26:
  2539. rtl8168d_2_hw_phy_config(tp);
  2540. break;
  2541. case RTL_GIGA_MAC_VER_27:
  2542. rtl8168d_3_hw_phy_config(tp);
  2543. break;
  2544. case RTL_GIGA_MAC_VER_28:
  2545. rtl8168d_4_hw_phy_config(tp);
  2546. break;
  2547. case RTL_GIGA_MAC_VER_29:
  2548. case RTL_GIGA_MAC_VER_30:
  2549. rtl8105e_hw_phy_config(tp);
  2550. break;
  2551. case RTL_GIGA_MAC_VER_31:
  2552. /* None. */
  2553. break;
  2554. case RTL_GIGA_MAC_VER_32:
  2555. case RTL_GIGA_MAC_VER_33:
  2556. rtl8168e_1_hw_phy_config(tp);
  2557. break;
  2558. case RTL_GIGA_MAC_VER_34:
  2559. rtl8168e_2_hw_phy_config(tp);
  2560. break;
  2561. default:
  2562. break;
  2563. }
  2564. }
  2565. static void rtl8169_phy_timer(unsigned long __opaque)
  2566. {
  2567. struct net_device *dev = (struct net_device *)__opaque;
  2568. struct rtl8169_private *tp = netdev_priv(dev);
  2569. struct timer_list *timer = &tp->timer;
  2570. void __iomem *ioaddr = tp->mmio_addr;
  2571. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  2572. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  2573. spin_lock_irq(&tp->lock);
  2574. if (tp->phy_reset_pending(tp)) {
  2575. /*
  2576. * A busy loop could burn quite a few cycles on nowadays CPU.
  2577. * Let's delay the execution of the timer for a few ticks.
  2578. */
  2579. timeout = HZ/10;
  2580. goto out_mod_timer;
  2581. }
  2582. if (tp->link_ok(ioaddr))
  2583. goto out_unlock;
  2584. netif_warn(tp, link, dev, "PHY reset until link up\n");
  2585. tp->phy_reset_enable(tp);
  2586. out_mod_timer:
  2587. mod_timer(timer, jiffies + timeout);
  2588. out_unlock:
  2589. spin_unlock_irq(&tp->lock);
  2590. }
  2591. #ifdef CONFIG_NET_POLL_CONTROLLER
  2592. /*
  2593. * Polling 'interrupt' - used by things like netconsole to send skbs
  2594. * without having to re-enable interrupts. It's not called while
  2595. * the interrupt routine is executing.
  2596. */
  2597. static void rtl8169_netpoll(struct net_device *dev)
  2598. {
  2599. struct rtl8169_private *tp = netdev_priv(dev);
  2600. struct pci_dev *pdev = tp->pci_dev;
  2601. disable_irq(pdev->irq);
  2602. rtl8169_interrupt(pdev->irq, dev);
  2603. enable_irq(pdev->irq);
  2604. }
  2605. #endif
  2606. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  2607. void __iomem *ioaddr)
  2608. {
  2609. iounmap(ioaddr);
  2610. pci_release_regions(pdev);
  2611. pci_clear_mwi(pdev);
  2612. pci_disable_device(pdev);
  2613. free_netdev(dev);
  2614. }
  2615. static void rtl8169_phy_reset(struct net_device *dev,
  2616. struct rtl8169_private *tp)
  2617. {
  2618. unsigned int i;
  2619. tp->phy_reset_enable(tp);
  2620. for (i = 0; i < 100; i++) {
  2621. if (!tp->phy_reset_pending(tp))
  2622. return;
  2623. msleep(1);
  2624. }
  2625. netif_err(tp, link, dev, "PHY reset failed\n");
  2626. }
  2627. static bool rtl_tbi_enabled(struct rtl8169_private *tp)
  2628. {
  2629. void __iomem *ioaddr = tp->mmio_addr;
  2630. return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
  2631. (RTL_R8(PHYstatus) & TBI_Enable);
  2632. }
  2633. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  2634. {
  2635. void __iomem *ioaddr = tp->mmio_addr;
  2636. rtl_hw_phy_config(dev);
  2637. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  2638. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  2639. RTL_W8(0x82, 0x01);
  2640. }
  2641. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  2642. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  2643. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  2644. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  2645. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  2646. RTL_W8(0x82, 0x01);
  2647. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  2648. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  2649. }
  2650. rtl8169_phy_reset(dev, tp);
  2651. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
  2652. ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  2653. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  2654. (tp->mii.supports_gmii ?
  2655. ADVERTISED_1000baseT_Half |
  2656. ADVERTISED_1000baseT_Full : 0));
  2657. if (rtl_tbi_enabled(tp))
  2658. netif_info(tp, link, dev, "TBI auto-negotiating\n");
  2659. }
  2660. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  2661. {
  2662. void __iomem *ioaddr = tp->mmio_addr;
  2663. u32 high;
  2664. u32 low;
  2665. low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
  2666. high = addr[4] | (addr[5] << 8);
  2667. spin_lock_irq(&tp->lock);
  2668. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2669. RTL_W32(MAC4, high);
  2670. RTL_R32(MAC4);
  2671. RTL_W32(MAC0, low);
  2672. RTL_R32(MAC0);
  2673. if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
  2674. const struct exgmac_reg e[] = {
  2675. { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
  2676. { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
  2677. { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
  2678. { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
  2679. low >> 16 },
  2680. };
  2681. rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
  2682. }
  2683. RTL_W8(Cfg9346, Cfg9346_Lock);
  2684. spin_unlock_irq(&tp->lock);
  2685. }
  2686. static int rtl_set_mac_address(struct net_device *dev, void *p)
  2687. {
  2688. struct rtl8169_private *tp = netdev_priv(dev);
  2689. struct sockaddr *addr = p;
  2690. if (!is_valid_ether_addr(addr->sa_data))
  2691. return -EADDRNOTAVAIL;
  2692. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  2693. rtl_rar_set(tp, dev->dev_addr);
  2694. return 0;
  2695. }
  2696. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2697. {
  2698. struct rtl8169_private *tp = netdev_priv(dev);
  2699. struct mii_ioctl_data *data = if_mii(ifr);
  2700. return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
  2701. }
  2702. static int rtl_xmii_ioctl(struct rtl8169_private *tp,
  2703. struct mii_ioctl_data *data, int cmd)
  2704. {
  2705. switch (cmd) {
  2706. case SIOCGMIIPHY:
  2707. data->phy_id = 32; /* Internal PHY */
  2708. return 0;
  2709. case SIOCGMIIREG:
  2710. data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
  2711. return 0;
  2712. case SIOCSMIIREG:
  2713. rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
  2714. return 0;
  2715. }
  2716. return -EOPNOTSUPP;
  2717. }
  2718. static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  2719. {
  2720. return -EOPNOTSUPP;
  2721. }
  2722. static const struct rtl_cfg_info {
  2723. void (*hw_start)(struct net_device *);
  2724. unsigned int region;
  2725. unsigned int align;
  2726. u16 intr_event;
  2727. u16 napi_event;
  2728. unsigned features;
  2729. u8 default_ver;
  2730. } rtl_cfg_infos [] = {
  2731. [RTL_CFG_0] = {
  2732. .hw_start = rtl_hw_start_8169,
  2733. .region = 1,
  2734. .align = 0,
  2735. .intr_event = SYSErr | LinkChg | RxOverflow |
  2736. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2737. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2738. .features = RTL_FEATURE_GMII,
  2739. .default_ver = RTL_GIGA_MAC_VER_01,
  2740. },
  2741. [RTL_CFG_1] = {
  2742. .hw_start = rtl_hw_start_8168,
  2743. .region = 2,
  2744. .align = 8,
  2745. .intr_event = SYSErr | LinkChg | RxOverflow |
  2746. TxErr | TxOK | RxOK | RxErr,
  2747. .napi_event = TxErr | TxOK | RxOK | RxOverflow,
  2748. .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
  2749. .default_ver = RTL_GIGA_MAC_VER_11,
  2750. },
  2751. [RTL_CFG_2] = {
  2752. .hw_start = rtl_hw_start_8101,
  2753. .region = 2,
  2754. .align = 8,
  2755. .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
  2756. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2757. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2758. .features = RTL_FEATURE_MSI,
  2759. .default_ver = RTL_GIGA_MAC_VER_13,
  2760. }
  2761. };
  2762. /* Cfg9346_Unlock assumed. */
  2763. static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
  2764. const struct rtl_cfg_info *cfg)
  2765. {
  2766. unsigned msi = 0;
  2767. u8 cfg2;
  2768. cfg2 = RTL_R8(Config2) & ~MSIEnable;
  2769. if (cfg->features & RTL_FEATURE_MSI) {
  2770. if (pci_enable_msi(pdev)) {
  2771. dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
  2772. } else {
  2773. cfg2 |= MSIEnable;
  2774. msi = RTL_FEATURE_MSI;
  2775. }
  2776. }
  2777. RTL_W8(Config2, cfg2);
  2778. return msi;
  2779. }
  2780. static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
  2781. {
  2782. if (tp->features & RTL_FEATURE_MSI) {
  2783. pci_disable_msi(pdev);
  2784. tp->features &= ~RTL_FEATURE_MSI;
  2785. }
  2786. }
  2787. static const struct net_device_ops rtl8169_netdev_ops = {
  2788. .ndo_open = rtl8169_open,
  2789. .ndo_stop = rtl8169_close,
  2790. .ndo_get_stats = rtl8169_get_stats,
  2791. .ndo_start_xmit = rtl8169_start_xmit,
  2792. .ndo_tx_timeout = rtl8169_tx_timeout,
  2793. .ndo_validate_addr = eth_validate_addr,
  2794. .ndo_change_mtu = rtl8169_change_mtu,
  2795. .ndo_fix_features = rtl8169_fix_features,
  2796. .ndo_set_features = rtl8169_set_features,
  2797. .ndo_set_mac_address = rtl_set_mac_address,
  2798. .ndo_do_ioctl = rtl8169_ioctl,
  2799. .ndo_set_multicast_list = rtl_set_rx_mode,
  2800. #ifdef CONFIG_NET_POLL_CONTROLLER
  2801. .ndo_poll_controller = rtl8169_netpoll,
  2802. #endif
  2803. };
  2804. static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
  2805. {
  2806. struct mdio_ops *ops = &tp->mdio_ops;
  2807. switch (tp->mac_version) {
  2808. case RTL_GIGA_MAC_VER_27:
  2809. ops->write = r8168dp_1_mdio_write;
  2810. ops->read = r8168dp_1_mdio_read;
  2811. break;
  2812. case RTL_GIGA_MAC_VER_28:
  2813. case RTL_GIGA_MAC_VER_31:
  2814. ops->write = r8168dp_2_mdio_write;
  2815. ops->read = r8168dp_2_mdio_read;
  2816. break;
  2817. default:
  2818. ops->write = r8169_mdio_write;
  2819. ops->read = r8169_mdio_read;
  2820. break;
  2821. }
  2822. }
  2823. static void r810x_phy_power_down(struct rtl8169_private *tp)
  2824. {
  2825. rtl_writephy(tp, 0x1f, 0x0000);
  2826. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2827. }
  2828. static void r810x_phy_power_up(struct rtl8169_private *tp)
  2829. {
  2830. rtl_writephy(tp, 0x1f, 0x0000);
  2831. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  2832. }
  2833. static void r810x_pll_power_down(struct rtl8169_private *tp)
  2834. {
  2835. void __iomem *ioaddr = tp->mmio_addr;
  2836. if (__rtl8169_get_wol(tp) & WAKE_ANY) {
  2837. rtl_writephy(tp, 0x1f, 0x0000);
  2838. rtl_writephy(tp, MII_BMCR, 0x0000);
  2839. if (tp->mac_version == RTL_GIGA_MAC_VER_29 ||
  2840. tp->mac_version == RTL_GIGA_MAC_VER_30)
  2841. RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
  2842. AcceptMulticast | AcceptMyPhys);
  2843. return;
  2844. }
  2845. r810x_phy_power_down(tp);
  2846. }
  2847. static void r810x_pll_power_up(struct rtl8169_private *tp)
  2848. {
  2849. r810x_phy_power_up(tp);
  2850. }
  2851. static void r8168_phy_power_up(struct rtl8169_private *tp)
  2852. {
  2853. rtl_writephy(tp, 0x1f, 0x0000);
  2854. switch (tp->mac_version) {
  2855. case RTL_GIGA_MAC_VER_11:
  2856. case RTL_GIGA_MAC_VER_12:
  2857. case RTL_GIGA_MAC_VER_17:
  2858. case RTL_GIGA_MAC_VER_18:
  2859. case RTL_GIGA_MAC_VER_19:
  2860. case RTL_GIGA_MAC_VER_20:
  2861. case RTL_GIGA_MAC_VER_21:
  2862. case RTL_GIGA_MAC_VER_22:
  2863. case RTL_GIGA_MAC_VER_23:
  2864. case RTL_GIGA_MAC_VER_24:
  2865. case RTL_GIGA_MAC_VER_25:
  2866. case RTL_GIGA_MAC_VER_26:
  2867. case RTL_GIGA_MAC_VER_27:
  2868. case RTL_GIGA_MAC_VER_28:
  2869. case RTL_GIGA_MAC_VER_31:
  2870. rtl_writephy(tp, 0x0e, 0x0000);
  2871. break;
  2872. default:
  2873. break;
  2874. }
  2875. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  2876. }
  2877. static void r8168_phy_power_down(struct rtl8169_private *tp)
  2878. {
  2879. rtl_writephy(tp, 0x1f, 0x0000);
  2880. switch (tp->mac_version) {
  2881. case RTL_GIGA_MAC_VER_32:
  2882. case RTL_GIGA_MAC_VER_33:
  2883. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
  2884. break;
  2885. case RTL_GIGA_MAC_VER_11:
  2886. case RTL_GIGA_MAC_VER_12:
  2887. case RTL_GIGA_MAC_VER_17:
  2888. case RTL_GIGA_MAC_VER_18:
  2889. case RTL_GIGA_MAC_VER_19:
  2890. case RTL_GIGA_MAC_VER_20:
  2891. case RTL_GIGA_MAC_VER_21:
  2892. case RTL_GIGA_MAC_VER_22:
  2893. case RTL_GIGA_MAC_VER_23:
  2894. case RTL_GIGA_MAC_VER_24:
  2895. case RTL_GIGA_MAC_VER_25:
  2896. case RTL_GIGA_MAC_VER_26:
  2897. case RTL_GIGA_MAC_VER_27:
  2898. case RTL_GIGA_MAC_VER_28:
  2899. case RTL_GIGA_MAC_VER_31:
  2900. rtl_writephy(tp, 0x0e, 0x0200);
  2901. default:
  2902. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2903. break;
  2904. }
  2905. }
  2906. static void r8168_pll_power_down(struct rtl8169_private *tp)
  2907. {
  2908. void __iomem *ioaddr = tp->mmio_addr;
  2909. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  2910. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  2911. tp->mac_version == RTL_GIGA_MAC_VER_31) &&
  2912. r8168dp_check_dash(tp)) {
  2913. return;
  2914. }
  2915. if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
  2916. tp->mac_version == RTL_GIGA_MAC_VER_24) &&
  2917. (RTL_R16(CPlusCmd) & ASF)) {
  2918. return;
  2919. }
  2920. if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
  2921. tp->mac_version == RTL_GIGA_MAC_VER_33)
  2922. rtl_ephy_write(ioaddr, 0x19, 0xff64);
  2923. if (__rtl8169_get_wol(tp) & WAKE_ANY) {
  2924. rtl_writephy(tp, 0x1f, 0x0000);
  2925. rtl_writephy(tp, MII_BMCR, 0x0000);
  2926. if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
  2927. tp->mac_version == RTL_GIGA_MAC_VER_33 ||
  2928. tp->mac_version == RTL_GIGA_MAC_VER_34)
  2929. RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
  2930. AcceptMulticast | AcceptMyPhys);
  2931. return;
  2932. }
  2933. r8168_phy_power_down(tp);
  2934. switch (tp->mac_version) {
  2935. case RTL_GIGA_MAC_VER_25:
  2936. case RTL_GIGA_MAC_VER_26:
  2937. case RTL_GIGA_MAC_VER_27:
  2938. case RTL_GIGA_MAC_VER_28:
  2939. case RTL_GIGA_MAC_VER_31:
  2940. case RTL_GIGA_MAC_VER_32:
  2941. case RTL_GIGA_MAC_VER_33:
  2942. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  2943. break;
  2944. }
  2945. }
  2946. static void r8168_pll_power_up(struct rtl8169_private *tp)
  2947. {
  2948. void __iomem *ioaddr = tp->mmio_addr;
  2949. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  2950. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  2951. tp->mac_version == RTL_GIGA_MAC_VER_31) &&
  2952. r8168dp_check_dash(tp)) {
  2953. return;
  2954. }
  2955. switch (tp->mac_version) {
  2956. case RTL_GIGA_MAC_VER_25:
  2957. case RTL_GIGA_MAC_VER_26:
  2958. case RTL_GIGA_MAC_VER_27:
  2959. case RTL_GIGA_MAC_VER_28:
  2960. case RTL_GIGA_MAC_VER_31:
  2961. case RTL_GIGA_MAC_VER_32:
  2962. case RTL_GIGA_MAC_VER_33:
  2963. RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
  2964. break;
  2965. }
  2966. r8168_phy_power_up(tp);
  2967. }
  2968. static void rtl_pll_power_op(struct rtl8169_private *tp,
  2969. void (*op)(struct rtl8169_private *))
  2970. {
  2971. if (op)
  2972. op(tp);
  2973. }
  2974. static void rtl_pll_power_down(struct rtl8169_private *tp)
  2975. {
  2976. rtl_pll_power_op(tp, tp->pll_power_ops.down);
  2977. }
  2978. static void rtl_pll_power_up(struct rtl8169_private *tp)
  2979. {
  2980. rtl_pll_power_op(tp, tp->pll_power_ops.up);
  2981. }
  2982. static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
  2983. {
  2984. struct pll_power_ops *ops = &tp->pll_power_ops;
  2985. switch (tp->mac_version) {
  2986. case RTL_GIGA_MAC_VER_07:
  2987. case RTL_GIGA_MAC_VER_08:
  2988. case RTL_GIGA_MAC_VER_09:
  2989. case RTL_GIGA_MAC_VER_10:
  2990. case RTL_GIGA_MAC_VER_16:
  2991. case RTL_GIGA_MAC_VER_29:
  2992. case RTL_GIGA_MAC_VER_30:
  2993. ops->down = r810x_pll_power_down;
  2994. ops->up = r810x_pll_power_up;
  2995. break;
  2996. case RTL_GIGA_MAC_VER_11:
  2997. case RTL_GIGA_MAC_VER_12:
  2998. case RTL_GIGA_MAC_VER_17:
  2999. case RTL_GIGA_MAC_VER_18:
  3000. case RTL_GIGA_MAC_VER_19:
  3001. case RTL_GIGA_MAC_VER_20:
  3002. case RTL_GIGA_MAC_VER_21:
  3003. case RTL_GIGA_MAC_VER_22:
  3004. case RTL_GIGA_MAC_VER_23:
  3005. case RTL_GIGA_MAC_VER_24:
  3006. case RTL_GIGA_MAC_VER_25:
  3007. case RTL_GIGA_MAC_VER_26:
  3008. case RTL_GIGA_MAC_VER_27:
  3009. case RTL_GIGA_MAC_VER_28:
  3010. case RTL_GIGA_MAC_VER_31:
  3011. case RTL_GIGA_MAC_VER_32:
  3012. case RTL_GIGA_MAC_VER_33:
  3013. case RTL_GIGA_MAC_VER_34:
  3014. ops->down = r8168_pll_power_down;
  3015. ops->up = r8168_pll_power_up;
  3016. break;
  3017. default:
  3018. ops->down = NULL;
  3019. ops->up = NULL;
  3020. break;
  3021. }
  3022. }
  3023. static void rtl_init_rxcfg(struct rtl8169_private *tp)
  3024. {
  3025. void __iomem *ioaddr = tp->mmio_addr;
  3026. switch (tp->mac_version) {
  3027. case RTL_GIGA_MAC_VER_01:
  3028. case RTL_GIGA_MAC_VER_02:
  3029. case RTL_GIGA_MAC_VER_03:
  3030. case RTL_GIGA_MAC_VER_04:
  3031. case RTL_GIGA_MAC_VER_05:
  3032. case RTL_GIGA_MAC_VER_06:
  3033. case RTL_GIGA_MAC_VER_10:
  3034. case RTL_GIGA_MAC_VER_11:
  3035. case RTL_GIGA_MAC_VER_12:
  3036. case RTL_GIGA_MAC_VER_13:
  3037. case RTL_GIGA_MAC_VER_14:
  3038. case RTL_GIGA_MAC_VER_15:
  3039. case RTL_GIGA_MAC_VER_16:
  3040. case RTL_GIGA_MAC_VER_17:
  3041. RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
  3042. break;
  3043. case RTL_GIGA_MAC_VER_18:
  3044. case RTL_GIGA_MAC_VER_19:
  3045. case RTL_GIGA_MAC_VER_20:
  3046. case RTL_GIGA_MAC_VER_21:
  3047. case RTL_GIGA_MAC_VER_22:
  3048. case RTL_GIGA_MAC_VER_23:
  3049. case RTL_GIGA_MAC_VER_24:
  3050. RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
  3051. break;
  3052. default:
  3053. RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
  3054. break;
  3055. }
  3056. }
  3057. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  3058. {
  3059. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  3060. }
  3061. static void rtl_hw_reset(struct rtl8169_private *tp)
  3062. {
  3063. void __iomem *ioaddr = tp->mmio_addr;
  3064. int i;
  3065. /* Soft reset the chip. */
  3066. RTL_W8(ChipCmd, CmdReset);
  3067. /* Check that the chip has finished the reset. */
  3068. for (i = 0; i < 100; i++) {
  3069. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  3070. break;
  3071. udelay(100);
  3072. }
  3073. rtl8169_init_ring_indexes(tp);
  3074. }
  3075. static int __devinit
  3076. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  3077. {
  3078. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  3079. const unsigned int region = cfg->region;
  3080. struct rtl8169_private *tp;
  3081. struct mii_if_info *mii;
  3082. struct net_device *dev;
  3083. void __iomem *ioaddr;
  3084. int chipset, i;
  3085. int rc;
  3086. if (netif_msg_drv(&debug)) {
  3087. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  3088. MODULENAME, RTL8169_VERSION);
  3089. }
  3090. dev = alloc_etherdev(sizeof (*tp));
  3091. if (!dev) {
  3092. if (netif_msg_drv(&debug))
  3093. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  3094. rc = -ENOMEM;
  3095. goto out;
  3096. }
  3097. SET_NETDEV_DEV(dev, &pdev->dev);
  3098. dev->netdev_ops = &rtl8169_netdev_ops;
  3099. tp = netdev_priv(dev);
  3100. tp->dev = dev;
  3101. tp->pci_dev = pdev;
  3102. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  3103. mii = &tp->mii;
  3104. mii->dev = dev;
  3105. mii->mdio_read = rtl_mdio_read;
  3106. mii->mdio_write = rtl_mdio_write;
  3107. mii->phy_id_mask = 0x1f;
  3108. mii->reg_num_mask = 0x1f;
  3109. mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
  3110. /* disable ASPM completely as that cause random device stop working
  3111. * problems as well as full system hangs for some PCIe devices users */
  3112. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  3113. PCIE_LINK_STATE_CLKPM);
  3114. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  3115. rc = pci_enable_device(pdev);
  3116. if (rc < 0) {
  3117. netif_err(tp, probe, dev, "enable failure\n");
  3118. goto err_out_free_dev_1;
  3119. }
  3120. if (pci_set_mwi(pdev) < 0)
  3121. netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
  3122. /* make sure PCI base addr 1 is MMIO */
  3123. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  3124. netif_err(tp, probe, dev,
  3125. "region #%d not an MMIO resource, aborting\n",
  3126. region);
  3127. rc = -ENODEV;
  3128. goto err_out_mwi_2;
  3129. }
  3130. /* check for weird/broken PCI region reporting */
  3131. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  3132. netif_err(tp, probe, dev,
  3133. "Invalid PCI region size(s), aborting\n");
  3134. rc = -ENODEV;
  3135. goto err_out_mwi_2;
  3136. }
  3137. rc = pci_request_regions(pdev, MODULENAME);
  3138. if (rc < 0) {
  3139. netif_err(tp, probe, dev, "could not request regions\n");
  3140. goto err_out_mwi_2;
  3141. }
  3142. tp->cp_cmd = RxChkSum;
  3143. if ((sizeof(dma_addr_t) > 4) &&
  3144. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
  3145. tp->cp_cmd |= PCIDAC;
  3146. dev->features |= NETIF_F_HIGHDMA;
  3147. } else {
  3148. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3149. if (rc < 0) {
  3150. netif_err(tp, probe, dev, "DMA configuration failed\n");
  3151. goto err_out_free_res_3;
  3152. }
  3153. }
  3154. /* ioremap MMIO region */
  3155. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  3156. if (!ioaddr) {
  3157. netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
  3158. rc = -EIO;
  3159. goto err_out_free_res_3;
  3160. }
  3161. tp->mmio_addr = ioaddr;
  3162. if (!pci_is_pcie(pdev))
  3163. netif_info(tp, probe, dev, "not PCI Express\n");
  3164. /* Identify chip attached to board */
  3165. rtl8169_get_mac_version(tp, dev, cfg->default_ver);
  3166. rtl_init_rxcfg(tp);
  3167. RTL_W16(IntrMask, 0x0000);
  3168. rtl_hw_reset(tp);
  3169. RTL_W16(IntrStatus, 0xffff);
  3170. pci_set_master(pdev);
  3171. /*
  3172. * Pretend we are using VLANs; This bypasses a nasty bug where
  3173. * Interrupts stop flowing on high load on 8110SCd controllers.
  3174. */
  3175. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  3176. tp->cp_cmd |= RxVlan;
  3177. rtl_init_mdio_ops(tp);
  3178. rtl_init_pll_power_ops(tp);
  3179. rtl8169_print_mac_version(tp);
  3180. chipset = tp->mac_version;
  3181. tp->txd_version = rtl_chip_infos[chipset].txd_version;
  3182. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3183. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  3184. RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
  3185. if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
  3186. tp->features |= RTL_FEATURE_WOL;
  3187. if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
  3188. tp->features |= RTL_FEATURE_WOL;
  3189. tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
  3190. RTL_W8(Cfg9346, Cfg9346_Lock);
  3191. if (rtl_tbi_enabled(tp)) {
  3192. tp->set_speed = rtl8169_set_speed_tbi;
  3193. tp->get_settings = rtl8169_gset_tbi;
  3194. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  3195. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  3196. tp->link_ok = rtl8169_tbi_link_ok;
  3197. tp->do_ioctl = rtl_tbi_ioctl;
  3198. } else {
  3199. tp->set_speed = rtl8169_set_speed_xmii;
  3200. tp->get_settings = rtl8169_gset_xmii;
  3201. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  3202. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  3203. tp->link_ok = rtl8169_xmii_link_ok;
  3204. tp->do_ioctl = rtl_xmii_ioctl;
  3205. }
  3206. spin_lock_init(&tp->lock);
  3207. /* Get MAC address */
  3208. for (i = 0; i < MAC_ADDR_LEN; i++)
  3209. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  3210. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  3211. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  3212. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  3213. dev->irq = pdev->irq;
  3214. dev->base_addr = (unsigned long) ioaddr;
  3215. netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
  3216. /* don't enable SG, IP_CSUM and TSO by default - it might not work
  3217. * properly for all devices */
  3218. dev->features |= NETIF_F_RXCSUM |
  3219. NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  3220. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  3221. NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  3222. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  3223. NETIF_F_HIGHDMA;
  3224. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  3225. /* 8110SCd requires hardware Rx VLAN - disallow toggling */
  3226. dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
  3227. tp->intr_mask = 0xffff;
  3228. tp->hw_start = cfg->hw_start;
  3229. tp->intr_event = cfg->intr_event;
  3230. tp->napi_event = cfg->napi_event;
  3231. tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
  3232. ~(RxBOVF | RxFOVF) : ~0;
  3233. init_timer(&tp->timer);
  3234. tp->timer.data = (unsigned long) dev;
  3235. tp->timer.function = rtl8169_phy_timer;
  3236. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  3237. rc = register_netdev(dev);
  3238. if (rc < 0)
  3239. goto err_out_msi_4;
  3240. pci_set_drvdata(pdev, dev);
  3241. netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
  3242. rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
  3243. (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
  3244. if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  3245. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  3246. tp->mac_version == RTL_GIGA_MAC_VER_31) {
  3247. rtl8168_driver_start(tp);
  3248. }
  3249. device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
  3250. if (pci_dev_run_wake(pdev))
  3251. pm_runtime_put_noidle(&pdev->dev);
  3252. netif_carrier_off(dev);
  3253. out:
  3254. return rc;
  3255. err_out_msi_4:
  3256. rtl_disable_msi(pdev, tp);
  3257. iounmap(ioaddr);
  3258. err_out_free_res_3:
  3259. pci_release_regions(pdev);
  3260. err_out_mwi_2:
  3261. pci_clear_mwi(pdev);
  3262. pci_disable_device(pdev);
  3263. err_out_free_dev_1:
  3264. free_netdev(dev);
  3265. goto out;
  3266. }
  3267. static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
  3268. {
  3269. struct net_device *dev = pci_get_drvdata(pdev);
  3270. struct rtl8169_private *tp = netdev_priv(dev);
  3271. if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  3272. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  3273. tp->mac_version == RTL_GIGA_MAC_VER_31) {
  3274. rtl8168_driver_stop(tp);
  3275. }
  3276. cancel_delayed_work_sync(&tp->task);
  3277. unregister_netdev(dev);
  3278. rtl_release_firmware(tp);
  3279. if (pci_dev_run_wake(pdev))
  3280. pm_runtime_get_noresume(&pdev->dev);
  3281. /* restore original MAC address */
  3282. rtl_rar_set(tp, dev->perm_addr);
  3283. rtl_disable_msi(pdev, tp);
  3284. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  3285. pci_set_drvdata(pdev, NULL);
  3286. }
  3287. static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
  3288. {
  3289. struct rtl_fw *rtl_fw;
  3290. const char *name;
  3291. int rc = -ENOMEM;
  3292. name = rtl_lookup_firmware_name(tp);
  3293. if (!name)
  3294. goto out_no_firmware;
  3295. rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
  3296. if (!rtl_fw)
  3297. goto err_warn;
  3298. rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
  3299. if (rc < 0)
  3300. goto err_free;
  3301. rc = rtl_check_firmware(tp, rtl_fw);
  3302. if (rc < 0)
  3303. goto err_release_firmware;
  3304. tp->rtl_fw = rtl_fw;
  3305. out:
  3306. return;
  3307. err_release_firmware:
  3308. release_firmware(rtl_fw->fw);
  3309. err_free:
  3310. kfree(rtl_fw);
  3311. err_warn:
  3312. netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
  3313. name, rc);
  3314. out_no_firmware:
  3315. tp->rtl_fw = NULL;
  3316. goto out;
  3317. }
  3318. static void rtl_request_firmware(struct rtl8169_private *tp)
  3319. {
  3320. if (IS_ERR(tp->rtl_fw))
  3321. rtl_request_uncached_firmware(tp);
  3322. }
  3323. static int rtl8169_open(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. int retval = -ENOMEM;
  3329. pm_runtime_get_sync(&pdev->dev);
  3330. /*
  3331. * Rx and Tx desscriptors needs 256 bytes alignment.
  3332. * dma_alloc_coherent provides more.
  3333. */
  3334. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  3335. &tp->TxPhyAddr, GFP_KERNEL);
  3336. if (!tp->TxDescArray)
  3337. goto err_pm_runtime_put;
  3338. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  3339. &tp->RxPhyAddr, GFP_KERNEL);
  3340. if (!tp->RxDescArray)
  3341. goto err_free_tx_0;
  3342. retval = rtl8169_init_ring(dev);
  3343. if (retval < 0)
  3344. goto err_free_rx_1;
  3345. INIT_DELAYED_WORK(&tp->task, NULL);
  3346. smp_mb();
  3347. rtl_request_firmware(tp);
  3348. retval = request_irq(dev->irq, rtl8169_interrupt,
  3349. (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
  3350. dev->name, dev);
  3351. if (retval < 0)
  3352. goto err_release_fw_2;
  3353. napi_enable(&tp->napi);
  3354. rtl8169_init_phy(dev, tp);
  3355. rtl8169_set_features(dev, dev->features);
  3356. rtl_pll_power_up(tp);
  3357. rtl_hw_start(dev);
  3358. tp->saved_wolopts = 0;
  3359. pm_runtime_put_noidle(&pdev->dev);
  3360. rtl8169_check_link_status(dev, tp, ioaddr);
  3361. out:
  3362. return retval;
  3363. err_release_fw_2:
  3364. rtl_release_firmware(tp);
  3365. rtl8169_rx_clear(tp);
  3366. err_free_rx_1:
  3367. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  3368. tp->RxPhyAddr);
  3369. tp->RxDescArray = NULL;
  3370. err_free_tx_0:
  3371. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  3372. tp->TxPhyAddr);
  3373. tp->TxDescArray = NULL;
  3374. err_pm_runtime_put:
  3375. pm_runtime_put_noidle(&pdev->dev);
  3376. goto out;
  3377. }
  3378. static void rtl_rx_close(struct rtl8169_private *tp)
  3379. {
  3380. void __iomem *ioaddr = tp->mmio_addr;
  3381. RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
  3382. }
  3383. static void rtl8169_hw_reset(struct rtl8169_private *tp)
  3384. {
  3385. void __iomem *ioaddr = tp->mmio_addr;
  3386. /* Disable interrupts */
  3387. rtl8169_irq_mask_and_ack(ioaddr);
  3388. rtl_rx_close(tp);
  3389. if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  3390. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  3391. tp->mac_version == RTL_GIGA_MAC_VER_31) {
  3392. while (RTL_R8(TxPoll) & NPQ)
  3393. udelay(20);
  3394. } else if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
  3395. RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
  3396. while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
  3397. udelay(100);
  3398. } else {
  3399. RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
  3400. udelay(100);
  3401. }
  3402. rtl_hw_reset(tp);
  3403. }
  3404. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  3405. {
  3406. void __iomem *ioaddr = tp->mmio_addr;
  3407. /* Set DMA burst size and Interframe Gap Time */
  3408. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  3409. (InterFrameGap << TxInterFrameGapShift));
  3410. }
  3411. static void rtl_hw_start(struct net_device *dev)
  3412. {
  3413. struct rtl8169_private *tp = netdev_priv(dev);
  3414. tp->hw_start(dev);
  3415. netif_start_queue(dev);
  3416. }
  3417. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  3418. void __iomem *ioaddr)
  3419. {
  3420. /*
  3421. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  3422. * register to be written before TxDescAddrLow to work.
  3423. * Switching from MMIO to I/O access fixes the issue as well.
  3424. */
  3425. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  3426. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  3427. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  3428. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  3429. }
  3430. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  3431. {
  3432. u16 cmd;
  3433. cmd = RTL_R16(CPlusCmd);
  3434. RTL_W16(CPlusCmd, cmd);
  3435. return cmd;
  3436. }
  3437. static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
  3438. {
  3439. /* Low hurts. Let's disable the filtering. */
  3440. RTL_W16(RxMaxSize, rx_buf_sz + 1);
  3441. }
  3442. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  3443. {
  3444. static const struct rtl_cfg2_info {
  3445. u32 mac_version;
  3446. u32 clk;
  3447. u32 val;
  3448. } cfg2_info [] = {
  3449. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  3450. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  3451. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  3452. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  3453. };
  3454. const struct rtl_cfg2_info *p = cfg2_info;
  3455. unsigned int i;
  3456. u32 clk;
  3457. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  3458. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  3459. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  3460. RTL_W32(0x7c, p->val);
  3461. break;
  3462. }
  3463. }
  3464. }
  3465. static void rtl_hw_start_8169(struct net_device *dev)
  3466. {
  3467. struct rtl8169_private *tp = netdev_priv(dev);
  3468. void __iomem *ioaddr = tp->mmio_addr;
  3469. struct pci_dev *pdev = tp->pci_dev;
  3470. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  3471. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  3472. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  3473. }
  3474. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3475. if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
  3476. tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  3477. tp->mac_version == RTL_GIGA_MAC_VER_03 ||
  3478. tp->mac_version == RTL_GIGA_MAC_VER_04)
  3479. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3480. rtl_init_rxcfg(tp);
  3481. RTL_W8(EarlyTxThres, NoEarlyTx);
  3482. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  3483. if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
  3484. tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  3485. tp->mac_version == RTL_GIGA_MAC_VER_03 ||
  3486. tp->mac_version == RTL_GIGA_MAC_VER_04)
  3487. rtl_set_rx_tx_config_registers(tp);
  3488. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  3489. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  3490. tp->mac_version == RTL_GIGA_MAC_VER_03) {
  3491. dprintk("Set MAC Reg C+CR Offset 0xE0. "
  3492. "Bit-3 and bit-14 MUST be 1\n");
  3493. tp->cp_cmd |= (1 << 14);
  3494. }
  3495. RTL_W16(CPlusCmd, tp->cp_cmd);
  3496. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  3497. /*
  3498. * Undocumented corner. Supposedly:
  3499. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  3500. */
  3501. RTL_W16(IntrMitigate, 0x0000);
  3502. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  3503. if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
  3504. tp->mac_version != RTL_GIGA_MAC_VER_02 &&
  3505. tp->mac_version != RTL_GIGA_MAC_VER_03 &&
  3506. tp->mac_version != RTL_GIGA_MAC_VER_04) {
  3507. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3508. rtl_set_rx_tx_config_registers(tp);
  3509. }
  3510. RTL_W8(Cfg9346, Cfg9346_Lock);
  3511. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  3512. RTL_R8(IntrMask);
  3513. RTL_W32(RxMissed, 0);
  3514. rtl_set_rx_mode(dev);
  3515. /* no early-rx interrupts */
  3516. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  3517. /* Enable all known interrupts by setting the interrupt mask. */
  3518. RTL_W16(IntrMask, tp->intr_event);
  3519. }
  3520. static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
  3521. {
  3522. int cap = pci_pcie_cap(pdev);
  3523. if (cap) {
  3524. u16 ctl;
  3525. pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
  3526. ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
  3527. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
  3528. }
  3529. }
  3530. static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
  3531. {
  3532. u32 csi;
  3533. csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
  3534. rtl_csi_write(ioaddr, 0x070c, csi | bits);
  3535. }
  3536. static void rtl_csi_access_enable_1(void __iomem *ioaddr)
  3537. {
  3538. rtl_csi_access_enable(ioaddr, 0x17000000);
  3539. }
  3540. static void rtl_csi_access_enable_2(void __iomem *ioaddr)
  3541. {
  3542. rtl_csi_access_enable(ioaddr, 0x27000000);
  3543. }
  3544. struct ephy_info {
  3545. unsigned int offset;
  3546. u16 mask;
  3547. u16 bits;
  3548. };
  3549. static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
  3550. {
  3551. u16 w;
  3552. while (len-- > 0) {
  3553. w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
  3554. rtl_ephy_write(ioaddr, e->offset, w);
  3555. e++;
  3556. }
  3557. }
  3558. static void rtl_disable_clock_request(struct pci_dev *pdev)
  3559. {
  3560. int cap = pci_pcie_cap(pdev);
  3561. if (cap) {
  3562. u16 ctl;
  3563. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  3564. ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  3565. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  3566. }
  3567. }
  3568. static void rtl_enable_clock_request(struct pci_dev *pdev)
  3569. {
  3570. int cap = pci_pcie_cap(pdev);
  3571. if (cap) {
  3572. u16 ctl;
  3573. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  3574. ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
  3575. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  3576. }
  3577. }
  3578. #define R8168_CPCMD_QUIRK_MASK (\
  3579. EnableBist | \
  3580. Mac_dbgo_oe | \
  3581. Force_half_dup | \
  3582. Force_rxflow_en | \
  3583. Force_txflow_en | \
  3584. Cxpl_dbg_sel | \
  3585. ASF | \
  3586. PktCntrDisable | \
  3587. Mac_dbgo_sel)
  3588. static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
  3589. {
  3590. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3591. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3592. rtl_tx_performance_tweak(pdev,
  3593. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3594. }
  3595. static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
  3596. {
  3597. rtl_hw_start_8168bb(ioaddr, pdev);
  3598. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3599. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  3600. }
  3601. static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
  3602. {
  3603. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  3604. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3605. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3606. rtl_disable_clock_request(pdev);
  3607. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3608. }
  3609. static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3610. {
  3611. static const struct ephy_info e_info_8168cp[] = {
  3612. { 0x01, 0, 0x0001 },
  3613. { 0x02, 0x0800, 0x1000 },
  3614. { 0x03, 0, 0x0042 },
  3615. { 0x06, 0x0080, 0x0000 },
  3616. { 0x07, 0, 0x2000 }
  3617. };
  3618. rtl_csi_access_enable_2(ioaddr);
  3619. rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  3620. __rtl_hw_start_8168cp(ioaddr, pdev);
  3621. }
  3622. static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3623. {
  3624. rtl_csi_access_enable_2(ioaddr);
  3625. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3626. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3627. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3628. }
  3629. static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3630. {
  3631. rtl_csi_access_enable_2(ioaddr);
  3632. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3633. /* Magic. */
  3634. RTL_W8(DBG_REG, 0x20);
  3635. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3636. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3637. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3638. }
  3639. static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3640. {
  3641. static const struct ephy_info e_info_8168c_1[] = {
  3642. { 0x02, 0x0800, 0x1000 },
  3643. { 0x03, 0, 0x0002 },
  3644. { 0x06, 0x0080, 0x0000 }
  3645. };
  3646. rtl_csi_access_enable_2(ioaddr);
  3647. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  3648. rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  3649. __rtl_hw_start_8168cp(ioaddr, pdev);
  3650. }
  3651. static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3652. {
  3653. static const struct ephy_info e_info_8168c_2[] = {
  3654. { 0x01, 0, 0x0001 },
  3655. { 0x03, 0x0400, 0x0220 }
  3656. };
  3657. rtl_csi_access_enable_2(ioaddr);
  3658. rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  3659. __rtl_hw_start_8168cp(ioaddr, pdev);
  3660. }
  3661. static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3662. {
  3663. rtl_hw_start_8168c_2(ioaddr, pdev);
  3664. }
  3665. static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
  3666. {
  3667. rtl_csi_access_enable_2(ioaddr);
  3668. __rtl_hw_start_8168cp(ioaddr, pdev);
  3669. }
  3670. static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
  3671. {
  3672. rtl_csi_access_enable_2(ioaddr);
  3673. rtl_disable_clock_request(pdev);
  3674. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3675. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3676. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3677. }
  3678. static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
  3679. {
  3680. rtl_csi_access_enable_1(ioaddr);
  3681. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3682. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3683. rtl_disable_clock_request(pdev);
  3684. }
  3685. static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
  3686. {
  3687. static const struct ephy_info e_info_8168d_4[] = {
  3688. { 0x0b, ~0, 0x48 },
  3689. { 0x19, 0x20, 0x50 },
  3690. { 0x0c, ~0, 0x20 }
  3691. };
  3692. int i;
  3693. rtl_csi_access_enable_1(ioaddr);
  3694. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3695. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3696. for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
  3697. const struct ephy_info *e = e_info_8168d_4 + i;
  3698. u16 w;
  3699. w = rtl_ephy_read(ioaddr, e->offset);
  3700. rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
  3701. }
  3702. rtl_enable_clock_request(pdev);
  3703. }
  3704. static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3705. {
  3706. static const struct ephy_info e_info_8168e_1[] = {
  3707. { 0x00, 0x0200, 0x0100 },
  3708. { 0x00, 0x0000, 0x0004 },
  3709. { 0x06, 0x0002, 0x0001 },
  3710. { 0x06, 0x0000, 0x0030 },
  3711. { 0x07, 0x0000, 0x2000 },
  3712. { 0x00, 0x0000, 0x0020 },
  3713. { 0x03, 0x5800, 0x2000 },
  3714. { 0x03, 0x0000, 0x0001 },
  3715. { 0x01, 0x0800, 0x1000 },
  3716. { 0x07, 0x0000, 0x4000 },
  3717. { 0x1e, 0x0000, 0x2000 },
  3718. { 0x19, 0xffff, 0xfe6c },
  3719. { 0x0a, 0x0000, 0x0040 }
  3720. };
  3721. rtl_csi_access_enable_2(ioaddr);
  3722. rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
  3723. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3724. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3725. rtl_disable_clock_request(pdev);
  3726. /* Reset tx FIFO pointer */
  3727. RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
  3728. RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
  3729. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  3730. }
  3731. static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3732. {
  3733. static const struct ephy_info e_info_8168e_2[] = {
  3734. { 0x09, 0x0000, 0x0080 },
  3735. { 0x19, 0x0000, 0x0224 }
  3736. };
  3737. rtl_csi_access_enable_1(ioaddr);
  3738. rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
  3739. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3740. rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3741. rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3742. rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  3743. rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  3744. rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  3745. rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
  3746. rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  3747. rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
  3748. ERIAR_EXGMAC);
  3749. RTL_W8(MaxTxPacketSize, 0x27);
  3750. rtl_disable_clock_request(pdev);
  3751. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  3752. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  3753. /* Adjust EEE LED frequency */
  3754. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  3755. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  3756. RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
  3757. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  3758. }
  3759. static void rtl_hw_start_8168(struct net_device *dev)
  3760. {
  3761. struct rtl8169_private *tp = netdev_priv(dev);
  3762. void __iomem *ioaddr = tp->mmio_addr;
  3763. struct pci_dev *pdev = tp->pci_dev;
  3764. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3765. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3766. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  3767. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  3768. RTL_W16(CPlusCmd, tp->cp_cmd);
  3769. RTL_W16(IntrMitigate, 0x5151);
  3770. /* Work around for RxFIFO overflow. */
  3771. if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
  3772. tp->mac_version == RTL_GIGA_MAC_VER_22) {
  3773. tp->intr_event |= RxFIFOOver | PCSTimeout;
  3774. tp->intr_event &= ~RxOverflow;
  3775. }
  3776. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  3777. rtl_set_rx_mode(dev);
  3778. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  3779. (InterFrameGap << TxInterFrameGapShift));
  3780. RTL_R8(IntrMask);
  3781. switch (tp->mac_version) {
  3782. case RTL_GIGA_MAC_VER_11:
  3783. rtl_hw_start_8168bb(ioaddr, pdev);
  3784. break;
  3785. case RTL_GIGA_MAC_VER_12:
  3786. case RTL_GIGA_MAC_VER_17:
  3787. rtl_hw_start_8168bef(ioaddr, pdev);
  3788. break;
  3789. case RTL_GIGA_MAC_VER_18:
  3790. rtl_hw_start_8168cp_1(ioaddr, pdev);
  3791. break;
  3792. case RTL_GIGA_MAC_VER_19:
  3793. rtl_hw_start_8168c_1(ioaddr, pdev);
  3794. break;
  3795. case RTL_GIGA_MAC_VER_20:
  3796. rtl_hw_start_8168c_2(ioaddr, pdev);
  3797. break;
  3798. case RTL_GIGA_MAC_VER_21:
  3799. rtl_hw_start_8168c_3(ioaddr, pdev);
  3800. break;
  3801. case RTL_GIGA_MAC_VER_22:
  3802. rtl_hw_start_8168c_4(ioaddr, pdev);
  3803. break;
  3804. case RTL_GIGA_MAC_VER_23:
  3805. rtl_hw_start_8168cp_2(ioaddr, pdev);
  3806. break;
  3807. case RTL_GIGA_MAC_VER_24:
  3808. rtl_hw_start_8168cp_3(ioaddr, pdev);
  3809. break;
  3810. case RTL_GIGA_MAC_VER_25:
  3811. case RTL_GIGA_MAC_VER_26:
  3812. case RTL_GIGA_MAC_VER_27:
  3813. rtl_hw_start_8168d(ioaddr, pdev);
  3814. break;
  3815. case RTL_GIGA_MAC_VER_28:
  3816. rtl_hw_start_8168d_4(ioaddr, pdev);
  3817. break;
  3818. case RTL_GIGA_MAC_VER_31:
  3819. rtl_hw_start_8168dp(ioaddr, pdev);
  3820. break;
  3821. case RTL_GIGA_MAC_VER_32:
  3822. case RTL_GIGA_MAC_VER_33:
  3823. rtl_hw_start_8168e_1(ioaddr, pdev);
  3824. break;
  3825. case RTL_GIGA_MAC_VER_34:
  3826. rtl_hw_start_8168e_2(ioaddr, pdev);
  3827. break;
  3828. default:
  3829. printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
  3830. dev->name, tp->mac_version);
  3831. break;
  3832. }
  3833. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3834. RTL_W8(Cfg9346, Cfg9346_Lock);
  3835. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  3836. RTL_W16(IntrMask, tp->intr_event);
  3837. }
  3838. #define R810X_CPCMD_QUIRK_MASK (\
  3839. EnableBist | \
  3840. Mac_dbgo_oe | \
  3841. Force_half_dup | \
  3842. Force_rxflow_en | \
  3843. Force_txflow_en | \
  3844. Cxpl_dbg_sel | \
  3845. ASF | \
  3846. PktCntrDisable | \
  3847. Mac_dbgo_sel)
  3848. static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3849. {
  3850. static const struct ephy_info e_info_8102e_1[] = {
  3851. { 0x01, 0, 0x6e65 },
  3852. { 0x02, 0, 0x091f },
  3853. { 0x03, 0, 0xc2f9 },
  3854. { 0x06, 0, 0xafb5 },
  3855. { 0x07, 0, 0x0e00 },
  3856. { 0x19, 0, 0xec80 },
  3857. { 0x01, 0, 0x2e65 },
  3858. { 0x01, 0, 0x6e65 }
  3859. };
  3860. u8 cfg1;
  3861. rtl_csi_access_enable_2(ioaddr);
  3862. RTL_W8(DBG_REG, FIX_NAK_1);
  3863. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3864. RTL_W8(Config1,
  3865. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  3866. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3867. cfg1 = RTL_R8(Config1);
  3868. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  3869. RTL_W8(Config1, cfg1 & ~LEDS0);
  3870. rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  3871. }
  3872. static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3873. {
  3874. rtl_csi_access_enable_2(ioaddr);
  3875. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3876. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  3877. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3878. }
  3879. static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3880. {
  3881. rtl_hw_start_8102e_2(ioaddr, pdev);
  3882. rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
  3883. }
  3884. static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3885. {
  3886. static const struct ephy_info e_info_8105e_1[] = {
  3887. { 0x07, 0, 0x4000 },
  3888. { 0x19, 0, 0x0200 },
  3889. { 0x19, 0, 0x0020 },
  3890. { 0x1e, 0, 0x2000 },
  3891. { 0x03, 0, 0x0001 },
  3892. { 0x19, 0, 0x0100 },
  3893. { 0x19, 0, 0x0004 },
  3894. { 0x0a, 0, 0x0020 }
  3895. };
  3896. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  3897. RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
  3898. /* Disable Early Tally Counter */
  3899. RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
  3900. RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
  3901. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  3902. rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
  3903. }
  3904. static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3905. {
  3906. rtl_hw_start_8105e_1(ioaddr, pdev);
  3907. rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
  3908. }
  3909. static void rtl_hw_start_8101(struct net_device *dev)
  3910. {
  3911. struct rtl8169_private *tp = netdev_priv(dev);
  3912. void __iomem *ioaddr = tp->mmio_addr;
  3913. struct pci_dev *pdev = tp->pci_dev;
  3914. if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
  3915. tp->mac_version == RTL_GIGA_MAC_VER_16) {
  3916. int cap = pci_pcie_cap(pdev);
  3917. if (cap) {
  3918. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
  3919. PCI_EXP_DEVCTL_NOSNOOP_EN);
  3920. }
  3921. }
  3922. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3923. switch (tp->mac_version) {
  3924. case RTL_GIGA_MAC_VER_07:
  3925. rtl_hw_start_8102e_1(ioaddr, pdev);
  3926. break;
  3927. case RTL_GIGA_MAC_VER_08:
  3928. rtl_hw_start_8102e_3(ioaddr, pdev);
  3929. break;
  3930. case RTL_GIGA_MAC_VER_09:
  3931. rtl_hw_start_8102e_2(ioaddr, pdev);
  3932. break;
  3933. case RTL_GIGA_MAC_VER_29:
  3934. rtl_hw_start_8105e_1(ioaddr, pdev);
  3935. break;
  3936. case RTL_GIGA_MAC_VER_30:
  3937. rtl_hw_start_8105e_2(ioaddr, pdev);
  3938. break;
  3939. }
  3940. RTL_W8(Cfg9346, Cfg9346_Lock);
  3941. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3942. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  3943. tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
  3944. RTL_W16(CPlusCmd, tp->cp_cmd);
  3945. RTL_W16(IntrMitigate, 0x0000);
  3946. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  3947. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3948. rtl_set_rx_tx_config_registers(tp);
  3949. RTL_R8(IntrMask);
  3950. rtl_set_rx_mode(dev);
  3951. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  3952. RTL_W16(IntrMask, tp->intr_event);
  3953. }
  3954. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  3955. {
  3956. if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
  3957. return -EINVAL;
  3958. dev->mtu = new_mtu;
  3959. netdev_update_features(dev);
  3960. return 0;
  3961. }
  3962. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  3963. {
  3964. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  3965. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  3966. }
  3967. static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
  3968. void **data_buff, struct RxDesc *desc)
  3969. {
  3970. dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
  3971. DMA_FROM_DEVICE);
  3972. kfree(*data_buff);
  3973. *data_buff = NULL;
  3974. rtl8169_make_unusable_by_asic(desc);
  3975. }
  3976. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  3977. {
  3978. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  3979. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  3980. }
  3981. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  3982. u32 rx_buf_sz)
  3983. {
  3984. desc->addr = cpu_to_le64(mapping);
  3985. wmb();
  3986. rtl8169_mark_to_asic(desc, rx_buf_sz);
  3987. }
  3988. static inline void *rtl8169_align(void *data)
  3989. {
  3990. return (void *)ALIGN((long)data, 16);
  3991. }
  3992. static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  3993. struct RxDesc *desc)
  3994. {
  3995. void *data;
  3996. dma_addr_t mapping;
  3997. struct device *d = &tp->pci_dev->dev;
  3998. struct net_device *dev = tp->dev;
  3999. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  4000. data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  4001. if (!data)
  4002. return NULL;
  4003. if (rtl8169_align(data) != data) {
  4004. kfree(data);
  4005. data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
  4006. if (!data)
  4007. return NULL;
  4008. }
  4009. mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
  4010. DMA_FROM_DEVICE);
  4011. if (unlikely(dma_mapping_error(d, mapping))) {
  4012. if (net_ratelimit())
  4013. netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
  4014. goto err_out;
  4015. }
  4016. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  4017. return data;
  4018. err_out:
  4019. kfree(data);
  4020. return NULL;
  4021. }
  4022. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  4023. {
  4024. unsigned int i;
  4025. for (i = 0; i < NUM_RX_DESC; i++) {
  4026. if (tp->Rx_databuff[i]) {
  4027. rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
  4028. tp->RxDescArray + i);
  4029. }
  4030. }
  4031. }
  4032. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  4033. {
  4034. desc->opts1 |= cpu_to_le32(RingEnd);
  4035. }
  4036. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  4037. {
  4038. unsigned int i;
  4039. for (i = 0; i < NUM_RX_DESC; i++) {
  4040. void *data;
  4041. if (tp->Rx_databuff[i])
  4042. continue;
  4043. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  4044. if (!data) {
  4045. rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
  4046. goto err_out;
  4047. }
  4048. tp->Rx_databuff[i] = data;
  4049. }
  4050. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  4051. return 0;
  4052. err_out:
  4053. rtl8169_rx_clear(tp);
  4054. return -ENOMEM;
  4055. }
  4056. static int rtl8169_init_ring(struct net_device *dev)
  4057. {
  4058. struct rtl8169_private *tp = netdev_priv(dev);
  4059. rtl8169_init_ring_indexes(tp);
  4060. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  4061. memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
  4062. return rtl8169_rx_fill(tp);
  4063. }
  4064. static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
  4065. struct TxDesc *desc)
  4066. {
  4067. unsigned int len = tx_skb->len;
  4068. dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
  4069. desc->opts1 = 0x00;
  4070. desc->opts2 = 0x00;
  4071. desc->addr = 0x00;
  4072. tx_skb->len = 0;
  4073. }
  4074. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  4075. unsigned int n)
  4076. {
  4077. unsigned int i;
  4078. for (i = 0; i < n; i++) {
  4079. unsigned int entry = (start + i) % NUM_TX_DESC;
  4080. struct ring_info *tx_skb = tp->tx_skb + entry;
  4081. unsigned int len = tx_skb->len;
  4082. if (len) {
  4083. struct sk_buff *skb = tx_skb->skb;
  4084. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  4085. tp->TxDescArray + entry);
  4086. if (skb) {
  4087. tp->dev->stats.tx_dropped++;
  4088. dev_kfree_skb(skb);
  4089. tx_skb->skb = NULL;
  4090. }
  4091. }
  4092. }
  4093. }
  4094. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  4095. {
  4096. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  4097. tp->cur_tx = tp->dirty_tx = 0;
  4098. }
  4099. static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
  4100. {
  4101. struct rtl8169_private *tp = netdev_priv(dev);
  4102. PREPARE_DELAYED_WORK(&tp->task, task);
  4103. schedule_delayed_work(&tp->task, 4);
  4104. }
  4105. static void rtl8169_wait_for_quiescence(struct net_device *dev)
  4106. {
  4107. struct rtl8169_private *tp = netdev_priv(dev);
  4108. void __iomem *ioaddr = tp->mmio_addr;
  4109. synchronize_irq(dev->irq);
  4110. /* Wait for any pending NAPI task to complete */
  4111. napi_disable(&tp->napi);
  4112. rtl8169_irq_mask_and_ack(ioaddr);
  4113. tp->intr_mask = 0xffff;
  4114. RTL_W16(IntrMask, tp->intr_event);
  4115. napi_enable(&tp->napi);
  4116. }
  4117. static void rtl8169_reinit_task(struct work_struct *work)
  4118. {
  4119. struct rtl8169_private *tp =
  4120. container_of(work, struct rtl8169_private, task.work);
  4121. struct net_device *dev = tp->dev;
  4122. int ret;
  4123. rtnl_lock();
  4124. if (!netif_running(dev))
  4125. goto out_unlock;
  4126. rtl8169_wait_for_quiescence(dev);
  4127. rtl8169_close(dev);
  4128. ret = rtl8169_open(dev);
  4129. if (unlikely(ret < 0)) {
  4130. if (net_ratelimit())
  4131. netif_err(tp, drv, dev,
  4132. "reinit failure (status = %d). Rescheduling\n",
  4133. ret);
  4134. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  4135. }
  4136. out_unlock:
  4137. rtnl_unlock();
  4138. }
  4139. static void rtl8169_reset_task(struct work_struct *work)
  4140. {
  4141. struct rtl8169_private *tp =
  4142. container_of(work, struct rtl8169_private, task.work);
  4143. struct net_device *dev = tp->dev;
  4144. int i;
  4145. rtnl_lock();
  4146. if (!netif_running(dev))
  4147. goto out_unlock;
  4148. rtl8169_wait_for_quiescence(dev);
  4149. for (i = 0; i < NUM_RX_DESC; i++)
  4150. rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
  4151. rtl8169_tx_clear(tp);
  4152. rtl8169_hw_reset(tp);
  4153. rtl_hw_start(dev);
  4154. netif_wake_queue(dev);
  4155. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  4156. out_unlock:
  4157. rtnl_unlock();
  4158. }
  4159. static void rtl8169_tx_timeout(struct net_device *dev)
  4160. {
  4161. struct rtl8169_private *tp = netdev_priv(dev);
  4162. rtl8169_hw_reset(tp);
  4163. /* Let's wait a bit while any (async) irq lands on */
  4164. rtl8169_schedule_work(dev, rtl8169_reset_task);
  4165. }
  4166. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  4167. u32 *opts)
  4168. {
  4169. struct skb_shared_info *info = skb_shinfo(skb);
  4170. unsigned int cur_frag, entry;
  4171. struct TxDesc * uninitialized_var(txd);
  4172. struct device *d = &tp->pci_dev->dev;
  4173. entry = tp->cur_tx;
  4174. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  4175. skb_frag_t *frag = info->frags + cur_frag;
  4176. dma_addr_t mapping;
  4177. u32 status, len;
  4178. void *addr;
  4179. entry = (entry + 1) % NUM_TX_DESC;
  4180. txd = tp->TxDescArray + entry;
  4181. len = frag->size;
  4182. addr = ((void *) page_address(frag->page)) + frag->page_offset;
  4183. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  4184. if (unlikely(dma_mapping_error(d, mapping))) {
  4185. if (net_ratelimit())
  4186. netif_err(tp, drv, tp->dev,
  4187. "Failed to map TX fragments DMA!\n");
  4188. goto err_out;
  4189. }
  4190. /* Anti gcc 2.95.3 bugware (sic) */
  4191. status = opts[0] | len |
  4192. (RingEnd * !((entry + 1) % NUM_TX_DESC));
  4193. txd->opts1 = cpu_to_le32(status);
  4194. txd->opts2 = cpu_to_le32(opts[1]);
  4195. txd->addr = cpu_to_le64(mapping);
  4196. tp->tx_skb[entry].len = len;
  4197. }
  4198. if (cur_frag) {
  4199. tp->tx_skb[entry].skb = skb;
  4200. txd->opts1 |= cpu_to_le32(LastFrag);
  4201. }
  4202. return cur_frag;
  4203. err_out:
  4204. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  4205. return -EIO;
  4206. }
  4207. static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
  4208. struct sk_buff *skb, u32 *opts)
  4209. {
  4210. const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
  4211. u32 mss = skb_shinfo(skb)->gso_size;
  4212. int offset = info->opts_offset;
  4213. if (mss) {
  4214. opts[0] |= TD_LSO;
  4215. opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
  4216. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  4217. const struct iphdr *ip = ip_hdr(skb);
  4218. if (ip->protocol == IPPROTO_TCP)
  4219. opts[offset] |= info->checksum.tcp;
  4220. else if (ip->protocol == IPPROTO_UDP)
  4221. opts[offset] |= info->checksum.udp;
  4222. else
  4223. WARN_ON_ONCE(1);
  4224. }
  4225. }
  4226. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  4227. struct net_device *dev)
  4228. {
  4229. struct rtl8169_private *tp = netdev_priv(dev);
  4230. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  4231. struct TxDesc *txd = tp->TxDescArray + entry;
  4232. void __iomem *ioaddr = tp->mmio_addr;
  4233. struct device *d = &tp->pci_dev->dev;
  4234. dma_addr_t mapping;
  4235. u32 status, len;
  4236. u32 opts[2];
  4237. int frags;
  4238. if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
  4239. netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
  4240. goto err_stop_0;
  4241. }
  4242. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  4243. goto err_stop_0;
  4244. len = skb_headlen(skb);
  4245. mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
  4246. if (unlikely(dma_mapping_error(d, mapping))) {
  4247. if (net_ratelimit())
  4248. netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
  4249. goto err_dma_0;
  4250. }
  4251. tp->tx_skb[entry].len = len;
  4252. txd->addr = cpu_to_le64(mapping);
  4253. opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
  4254. opts[0] = DescOwn;
  4255. rtl8169_tso_csum(tp, skb, opts);
  4256. frags = rtl8169_xmit_frags(tp, skb, opts);
  4257. if (frags < 0)
  4258. goto err_dma_1;
  4259. else if (frags)
  4260. opts[0] |= FirstFrag;
  4261. else {
  4262. opts[0] |= FirstFrag | LastFrag;
  4263. tp->tx_skb[entry].skb = skb;
  4264. }
  4265. txd->opts2 = cpu_to_le32(opts[1]);
  4266. wmb();
  4267. /* Anti gcc 2.95.3 bugware (sic) */
  4268. status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  4269. txd->opts1 = cpu_to_le32(status);
  4270. tp->cur_tx += frags + 1;
  4271. wmb();
  4272. RTL_W8(TxPoll, NPQ);
  4273. if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
  4274. netif_stop_queue(dev);
  4275. smp_rmb();
  4276. if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
  4277. netif_wake_queue(dev);
  4278. }
  4279. return NETDEV_TX_OK;
  4280. err_dma_1:
  4281. rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
  4282. err_dma_0:
  4283. dev_kfree_skb(skb);
  4284. dev->stats.tx_dropped++;
  4285. return NETDEV_TX_OK;
  4286. err_stop_0:
  4287. netif_stop_queue(dev);
  4288. dev->stats.tx_dropped++;
  4289. return NETDEV_TX_BUSY;
  4290. }
  4291. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  4292. {
  4293. struct rtl8169_private *tp = netdev_priv(dev);
  4294. struct pci_dev *pdev = tp->pci_dev;
  4295. u16 pci_status, pci_cmd;
  4296. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  4297. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  4298. netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
  4299. pci_cmd, pci_status);
  4300. /*
  4301. * The recovery sequence below admits a very elaborated explanation:
  4302. * - it seems to work;
  4303. * - I did not see what else could be done;
  4304. * - it makes iop3xx happy.
  4305. *
  4306. * Feel free to adjust to your needs.
  4307. */
  4308. if (pdev->broken_parity_status)
  4309. pci_cmd &= ~PCI_COMMAND_PARITY;
  4310. else
  4311. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  4312. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  4313. pci_write_config_word(pdev, PCI_STATUS,
  4314. pci_status & (PCI_STATUS_DETECTED_PARITY |
  4315. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  4316. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  4317. /* The infamous DAC f*ckup only happens at boot time */
  4318. if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
  4319. void __iomem *ioaddr = tp->mmio_addr;
  4320. netif_info(tp, intr, dev, "disabling PCI DAC\n");
  4321. tp->cp_cmd &= ~PCIDAC;
  4322. RTL_W16(CPlusCmd, tp->cp_cmd);
  4323. dev->features &= ~NETIF_F_HIGHDMA;
  4324. }
  4325. rtl8169_hw_reset(tp);
  4326. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  4327. }
  4328. static void rtl8169_tx_interrupt(struct net_device *dev,
  4329. struct rtl8169_private *tp,
  4330. void __iomem *ioaddr)
  4331. {
  4332. unsigned int dirty_tx, tx_left;
  4333. dirty_tx = tp->dirty_tx;
  4334. smp_rmb();
  4335. tx_left = tp->cur_tx - dirty_tx;
  4336. while (tx_left > 0) {
  4337. unsigned int entry = dirty_tx % NUM_TX_DESC;
  4338. struct ring_info *tx_skb = tp->tx_skb + entry;
  4339. u32 status;
  4340. rmb();
  4341. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  4342. if (status & DescOwn)
  4343. break;
  4344. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  4345. tp->TxDescArray + entry);
  4346. if (status & LastFrag) {
  4347. dev->stats.tx_packets++;
  4348. dev->stats.tx_bytes += tx_skb->skb->len;
  4349. dev_kfree_skb(tx_skb->skb);
  4350. tx_skb->skb = NULL;
  4351. }
  4352. dirty_tx++;
  4353. tx_left--;
  4354. }
  4355. if (tp->dirty_tx != dirty_tx) {
  4356. tp->dirty_tx = dirty_tx;
  4357. smp_wmb();
  4358. if (netif_queue_stopped(dev) &&
  4359. (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
  4360. netif_wake_queue(dev);
  4361. }
  4362. /*
  4363. * 8168 hack: TxPoll requests are lost when the Tx packets are
  4364. * too close. Let's kick an extra TxPoll request when a burst
  4365. * of start_xmit activity is detected (if it is not detected,
  4366. * it is slow enough). -- FR
  4367. */
  4368. smp_rmb();
  4369. if (tp->cur_tx != dirty_tx)
  4370. RTL_W8(TxPoll, NPQ);
  4371. }
  4372. }
  4373. static inline int rtl8169_fragmented_frame(u32 status)
  4374. {
  4375. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  4376. }
  4377. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  4378. {
  4379. u32 status = opts1 & RxProtoMask;
  4380. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  4381. ((status == RxProtoUDP) && !(opts1 & UDPFail)))
  4382. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4383. else
  4384. skb_checksum_none_assert(skb);
  4385. }
  4386. static struct sk_buff *rtl8169_try_rx_copy(void *data,
  4387. struct rtl8169_private *tp,
  4388. int pkt_size,
  4389. dma_addr_t addr)
  4390. {
  4391. struct sk_buff *skb;
  4392. struct device *d = &tp->pci_dev->dev;
  4393. data = rtl8169_align(data);
  4394. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  4395. prefetch(data);
  4396. skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
  4397. if (skb)
  4398. memcpy(skb->data, data, pkt_size);
  4399. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  4400. return skb;
  4401. }
  4402. static int rtl8169_rx_interrupt(struct net_device *dev,
  4403. struct rtl8169_private *tp,
  4404. void __iomem *ioaddr, u32 budget)
  4405. {
  4406. unsigned int cur_rx, rx_left;
  4407. unsigned int count;
  4408. cur_rx = tp->cur_rx;
  4409. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  4410. rx_left = min(rx_left, budget);
  4411. for (; rx_left > 0; rx_left--, cur_rx++) {
  4412. unsigned int entry = cur_rx % NUM_RX_DESC;
  4413. struct RxDesc *desc = tp->RxDescArray + entry;
  4414. u32 status;
  4415. rmb();
  4416. status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
  4417. if (status & DescOwn)
  4418. break;
  4419. if (unlikely(status & RxRES)) {
  4420. netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
  4421. status);
  4422. dev->stats.rx_errors++;
  4423. if (status & (RxRWT | RxRUNT))
  4424. dev->stats.rx_length_errors++;
  4425. if (status & RxCRC)
  4426. dev->stats.rx_crc_errors++;
  4427. if (status & RxFOVF) {
  4428. rtl8169_schedule_work(dev, rtl8169_reset_task);
  4429. dev->stats.rx_fifo_errors++;
  4430. }
  4431. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4432. } else {
  4433. struct sk_buff *skb;
  4434. dma_addr_t addr = le64_to_cpu(desc->addr);
  4435. int pkt_size = (status & 0x00001FFF) - 4;
  4436. /*
  4437. * The driver does not support incoming fragmented
  4438. * frames. They are seen as a symptom of over-mtu
  4439. * sized frames.
  4440. */
  4441. if (unlikely(rtl8169_fragmented_frame(status))) {
  4442. dev->stats.rx_dropped++;
  4443. dev->stats.rx_length_errors++;
  4444. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4445. continue;
  4446. }
  4447. skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
  4448. tp, pkt_size, addr);
  4449. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4450. if (!skb) {
  4451. dev->stats.rx_dropped++;
  4452. continue;
  4453. }
  4454. rtl8169_rx_csum(skb, status);
  4455. skb_put(skb, pkt_size);
  4456. skb->protocol = eth_type_trans(skb, dev);
  4457. rtl8169_rx_vlan_tag(desc, skb);
  4458. napi_gro_receive(&tp->napi, skb);
  4459. dev->stats.rx_bytes += pkt_size;
  4460. dev->stats.rx_packets++;
  4461. }
  4462. /* Work around for AMD plateform. */
  4463. if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
  4464. (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
  4465. desc->opts2 = 0;
  4466. cur_rx++;
  4467. }
  4468. }
  4469. count = cur_rx - tp->cur_rx;
  4470. tp->cur_rx = cur_rx;
  4471. tp->dirty_rx += count;
  4472. return count;
  4473. }
  4474. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  4475. {
  4476. struct net_device *dev = dev_instance;
  4477. struct rtl8169_private *tp = netdev_priv(dev);
  4478. void __iomem *ioaddr = tp->mmio_addr;
  4479. int handled = 0;
  4480. int status;
  4481. /* loop handling interrupts until we have no new ones or
  4482. * we hit a invalid/hotplug case.
  4483. */
  4484. status = RTL_R16(IntrStatus);
  4485. while (status && status != 0xffff) {
  4486. handled = 1;
  4487. /* Handle all of the error cases first. These will reset
  4488. * the chip, so just exit the loop.
  4489. */
  4490. if (unlikely(!netif_running(dev))) {
  4491. rtl8169_hw_reset(tp);
  4492. break;
  4493. }
  4494. if (unlikely(status & RxFIFOOver)) {
  4495. switch (tp->mac_version) {
  4496. /* Work around for rx fifo overflow */
  4497. case RTL_GIGA_MAC_VER_11:
  4498. case RTL_GIGA_MAC_VER_22:
  4499. case RTL_GIGA_MAC_VER_26:
  4500. netif_stop_queue(dev);
  4501. rtl8169_tx_timeout(dev);
  4502. goto done;
  4503. /* Testers needed. */
  4504. case RTL_GIGA_MAC_VER_17:
  4505. case RTL_GIGA_MAC_VER_19:
  4506. case RTL_GIGA_MAC_VER_20:
  4507. case RTL_GIGA_MAC_VER_21:
  4508. case RTL_GIGA_MAC_VER_23:
  4509. case RTL_GIGA_MAC_VER_24:
  4510. case RTL_GIGA_MAC_VER_27:
  4511. case RTL_GIGA_MAC_VER_28:
  4512. case RTL_GIGA_MAC_VER_31:
  4513. /* Experimental science. Pktgen proof. */
  4514. case RTL_GIGA_MAC_VER_12:
  4515. case RTL_GIGA_MAC_VER_25:
  4516. if (status == RxFIFOOver)
  4517. goto done;
  4518. break;
  4519. default:
  4520. break;
  4521. }
  4522. }
  4523. if (unlikely(status & SYSErr)) {
  4524. rtl8169_pcierr_interrupt(dev);
  4525. break;
  4526. }
  4527. if (status & LinkChg)
  4528. __rtl8169_check_link_status(dev, tp, ioaddr, true);
  4529. /* We need to see the lastest version of tp->intr_mask to
  4530. * avoid ignoring an MSI interrupt and having to wait for
  4531. * another event which may never come.
  4532. */
  4533. smp_rmb();
  4534. if (status & tp->intr_mask & tp->napi_event) {
  4535. RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
  4536. tp->intr_mask = ~tp->napi_event;
  4537. if (likely(napi_schedule_prep(&tp->napi)))
  4538. __napi_schedule(&tp->napi);
  4539. else
  4540. netif_info(tp, intr, dev,
  4541. "interrupt %04x in poll\n", status);
  4542. }
  4543. /* We only get a new MSI interrupt when all active irq
  4544. * sources on the chip have been acknowledged. So, ack
  4545. * everything we've seen and check if new sources have become
  4546. * active to avoid blocking all interrupts from the chip.
  4547. */
  4548. RTL_W16(IntrStatus,
  4549. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  4550. status = RTL_R16(IntrStatus);
  4551. }
  4552. done:
  4553. return IRQ_RETVAL(handled);
  4554. }
  4555. static int rtl8169_poll(struct napi_struct *napi, int budget)
  4556. {
  4557. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  4558. struct net_device *dev = tp->dev;
  4559. void __iomem *ioaddr = tp->mmio_addr;
  4560. int work_done;
  4561. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
  4562. rtl8169_tx_interrupt(dev, tp, ioaddr);
  4563. if (work_done < budget) {
  4564. napi_complete(napi);
  4565. /* We need for force the visibility of tp->intr_mask
  4566. * for other CPUs, as we can loose an MSI interrupt
  4567. * and potentially wait for a retransmit timeout if we don't.
  4568. * The posted write to IntrMask is safe, as it will
  4569. * eventually make it to the chip and we won't loose anything
  4570. * until it does.
  4571. */
  4572. tp->intr_mask = 0xffff;
  4573. wmb();
  4574. RTL_W16(IntrMask, tp->intr_event);
  4575. }
  4576. return work_done;
  4577. }
  4578. static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
  4579. {
  4580. struct rtl8169_private *tp = netdev_priv(dev);
  4581. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  4582. return;
  4583. dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
  4584. RTL_W32(RxMissed, 0);
  4585. }
  4586. static void rtl8169_down(struct net_device *dev)
  4587. {
  4588. struct rtl8169_private *tp = netdev_priv(dev);
  4589. void __iomem *ioaddr = tp->mmio_addr;
  4590. del_timer_sync(&tp->timer);
  4591. netif_stop_queue(dev);
  4592. napi_disable(&tp->napi);
  4593. spin_lock_irq(&tp->lock);
  4594. rtl8169_hw_reset(tp);
  4595. /*
  4596. * At this point device interrupts can not be enabled in any function,
  4597. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
  4598. * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
  4599. */
  4600. rtl8169_rx_missed(dev, ioaddr);
  4601. spin_unlock_irq(&tp->lock);
  4602. synchronize_irq(dev->irq);
  4603. /* Give a racing hard_start_xmit a few cycles to complete. */
  4604. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  4605. rtl8169_tx_clear(tp);
  4606. rtl8169_rx_clear(tp);
  4607. rtl_pll_power_down(tp);
  4608. }
  4609. static int rtl8169_close(struct net_device *dev)
  4610. {
  4611. struct rtl8169_private *tp = netdev_priv(dev);
  4612. struct pci_dev *pdev = tp->pci_dev;
  4613. pm_runtime_get_sync(&pdev->dev);
  4614. /* Update counters before going down */
  4615. rtl8169_update_counters(dev);
  4616. rtl8169_down(dev);
  4617. free_irq(dev->irq, dev);
  4618. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  4619. tp->RxPhyAddr);
  4620. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  4621. tp->TxPhyAddr);
  4622. tp->TxDescArray = NULL;
  4623. tp->RxDescArray = NULL;
  4624. pm_runtime_put_sync(&pdev->dev);
  4625. return 0;
  4626. }
  4627. static void rtl_set_rx_mode(struct net_device *dev)
  4628. {
  4629. struct rtl8169_private *tp = netdev_priv(dev);
  4630. void __iomem *ioaddr = tp->mmio_addr;
  4631. unsigned long flags;
  4632. u32 mc_filter[2]; /* Multicast hash filter */
  4633. int rx_mode;
  4634. u32 tmp = 0;
  4635. if (dev->flags & IFF_PROMISC) {
  4636. /* Unconditionally log net taps. */
  4637. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  4638. rx_mode =
  4639. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  4640. AcceptAllPhys;
  4641. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4642. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  4643. (dev->flags & IFF_ALLMULTI)) {
  4644. /* Too many to filter perfectly -- accept all multicasts. */
  4645. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  4646. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4647. } else {
  4648. struct netdev_hw_addr *ha;
  4649. rx_mode = AcceptBroadcast | AcceptMyPhys;
  4650. mc_filter[1] = mc_filter[0] = 0;
  4651. netdev_for_each_mc_addr(ha, dev) {
  4652. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  4653. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  4654. rx_mode |= AcceptMulticast;
  4655. }
  4656. }
  4657. spin_lock_irqsave(&tp->lock, flags);
  4658. tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
  4659. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  4660. u32 data = mc_filter[0];
  4661. mc_filter[0] = swab32(mc_filter[1]);
  4662. mc_filter[1] = swab32(data);
  4663. }
  4664. RTL_W32(MAR0 + 4, mc_filter[1]);
  4665. RTL_W32(MAR0 + 0, mc_filter[0]);
  4666. RTL_W32(RxConfig, tmp);
  4667. spin_unlock_irqrestore(&tp->lock, flags);
  4668. }
  4669. /**
  4670. * rtl8169_get_stats - Get rtl8169 read/write statistics
  4671. * @dev: The Ethernet Device to get statistics for
  4672. *
  4673. * Get TX/RX statistics for rtl8169
  4674. */
  4675. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  4676. {
  4677. struct rtl8169_private *tp = netdev_priv(dev);
  4678. void __iomem *ioaddr = tp->mmio_addr;
  4679. unsigned long flags;
  4680. if (netif_running(dev)) {
  4681. spin_lock_irqsave(&tp->lock, flags);
  4682. rtl8169_rx_missed(dev, ioaddr);
  4683. spin_unlock_irqrestore(&tp->lock, flags);
  4684. }
  4685. return &dev->stats;
  4686. }
  4687. static void rtl8169_net_suspend(struct net_device *dev)
  4688. {
  4689. struct rtl8169_private *tp = netdev_priv(dev);
  4690. if (!netif_running(dev))
  4691. return;
  4692. rtl_pll_power_down(tp);
  4693. netif_device_detach(dev);
  4694. netif_stop_queue(dev);
  4695. }
  4696. #ifdef CONFIG_PM
  4697. static int rtl8169_suspend(struct device *device)
  4698. {
  4699. struct pci_dev *pdev = to_pci_dev(device);
  4700. struct net_device *dev = pci_get_drvdata(pdev);
  4701. rtl8169_net_suspend(dev);
  4702. return 0;
  4703. }
  4704. static void __rtl8169_resume(struct net_device *dev)
  4705. {
  4706. struct rtl8169_private *tp = netdev_priv(dev);
  4707. netif_device_attach(dev);
  4708. rtl_pll_power_up(tp);
  4709. rtl8169_schedule_work(dev, rtl8169_reset_task);
  4710. }
  4711. static int rtl8169_resume(struct device *device)
  4712. {
  4713. struct pci_dev *pdev = to_pci_dev(device);
  4714. struct net_device *dev = pci_get_drvdata(pdev);
  4715. struct rtl8169_private *tp = netdev_priv(dev);
  4716. rtl8169_init_phy(dev, tp);
  4717. if (netif_running(dev))
  4718. __rtl8169_resume(dev);
  4719. return 0;
  4720. }
  4721. static int rtl8169_runtime_suspend(struct device *device)
  4722. {
  4723. struct pci_dev *pdev = to_pci_dev(device);
  4724. struct net_device *dev = pci_get_drvdata(pdev);
  4725. struct rtl8169_private *tp = netdev_priv(dev);
  4726. if (!tp->TxDescArray)
  4727. return 0;
  4728. spin_lock_irq(&tp->lock);
  4729. tp->saved_wolopts = __rtl8169_get_wol(tp);
  4730. __rtl8169_set_wol(tp, WAKE_ANY);
  4731. spin_unlock_irq(&tp->lock);
  4732. rtl8169_net_suspend(dev);
  4733. return 0;
  4734. }
  4735. static int rtl8169_runtime_resume(struct device *device)
  4736. {
  4737. struct pci_dev *pdev = to_pci_dev(device);
  4738. struct net_device *dev = pci_get_drvdata(pdev);
  4739. struct rtl8169_private *tp = netdev_priv(dev);
  4740. if (!tp->TxDescArray)
  4741. return 0;
  4742. spin_lock_irq(&tp->lock);
  4743. __rtl8169_set_wol(tp, tp->saved_wolopts);
  4744. tp->saved_wolopts = 0;
  4745. spin_unlock_irq(&tp->lock);
  4746. rtl8169_init_phy(dev, tp);
  4747. __rtl8169_resume(dev);
  4748. return 0;
  4749. }
  4750. static int rtl8169_runtime_idle(struct device *device)
  4751. {
  4752. struct pci_dev *pdev = to_pci_dev(device);
  4753. struct net_device *dev = pci_get_drvdata(pdev);
  4754. struct rtl8169_private *tp = netdev_priv(dev);
  4755. return tp->TxDescArray ? -EBUSY : 0;
  4756. }
  4757. static const struct dev_pm_ops rtl8169_pm_ops = {
  4758. .suspend = rtl8169_suspend,
  4759. .resume = rtl8169_resume,
  4760. .freeze = rtl8169_suspend,
  4761. .thaw = rtl8169_resume,
  4762. .poweroff = rtl8169_suspend,
  4763. .restore = rtl8169_resume,
  4764. .runtime_suspend = rtl8169_runtime_suspend,
  4765. .runtime_resume = rtl8169_runtime_resume,
  4766. .runtime_idle = rtl8169_runtime_idle,
  4767. };
  4768. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  4769. #else /* !CONFIG_PM */
  4770. #define RTL8169_PM_OPS NULL
  4771. #endif /* !CONFIG_PM */
  4772. static void rtl_shutdown(struct pci_dev *pdev)
  4773. {
  4774. struct net_device *dev = pci_get_drvdata(pdev);
  4775. struct rtl8169_private *tp = netdev_priv(dev);
  4776. void __iomem *ioaddr = tp->mmio_addr;
  4777. rtl8169_net_suspend(dev);
  4778. /* Restore original MAC address */
  4779. rtl_rar_set(tp, dev->perm_addr);
  4780. spin_lock_irq(&tp->lock);
  4781. rtl8169_hw_reset(tp);
  4782. spin_unlock_irq(&tp->lock);
  4783. if (system_state == SYSTEM_POWER_OFF) {
  4784. /* WoL fails with 8168b when the receiver is disabled. */
  4785. if ((tp->mac_version == RTL_GIGA_MAC_VER_11 ||
  4786. tp->mac_version == RTL_GIGA_MAC_VER_12 ||
  4787. tp->mac_version == RTL_GIGA_MAC_VER_17) &&
  4788. (tp->features & RTL_FEATURE_WOL)) {
  4789. pci_clear_master(pdev);
  4790. RTL_W8(ChipCmd, CmdRxEnb);
  4791. /* PCI commit */
  4792. RTL_R8(ChipCmd);
  4793. }
  4794. pci_wake_from_d3(pdev, true);
  4795. pci_set_power_state(pdev, PCI_D3hot);
  4796. }
  4797. }
  4798. static struct pci_driver rtl8169_pci_driver = {
  4799. .name = MODULENAME,
  4800. .id_table = rtl8169_pci_tbl,
  4801. .probe = rtl8169_init_one,
  4802. .remove = __devexit_p(rtl8169_remove_one),
  4803. .shutdown = rtl_shutdown,
  4804. .driver.pm = RTL8169_PM_OPS,
  4805. };
  4806. static int __init rtl8169_init_module(void)
  4807. {
  4808. return pci_register_driver(&rtl8169_pci_driver);
  4809. }
  4810. static void __exit rtl8169_cleanup_module(void)
  4811. {
  4812. pci_unregister_driver(&rtl8169_pci_driver);
  4813. }
  4814. module_init(rtl8169_init_module);
  4815. module_exit(rtl8169_cleanup_module);