r8169.c 149 KB

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