r8169.c 150 KB

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