r8169.c 151 KB

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