r8169.c 148 KB

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