r8169.c 209 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498
  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/interrupt.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/firmware.h>
  27. #include <linux/pci-aspm.h>
  28. #include <linux/prefetch.h>
  29. #include <linux/ipv6.h>
  30. #include <net/ip6_checksum.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #define RTL8169_VERSION "2.3LK-NAPI"
  34. #define MODULENAME "r8169"
  35. #define PFX MODULENAME ": "
  36. #define FIRMWARE_8168D_1 "/*(DEBLOBBED)*/"
  37. #define FIRMWARE_8168D_2 "/*(DEBLOBBED)*/"
  38. #define FIRMWARE_8168E_1 "/*(DEBLOBBED)*/"
  39. #define FIRMWARE_8168E_2 "/*(DEBLOBBED)*/"
  40. #define FIRMWARE_8168E_3 "/*(DEBLOBBED)*/"
  41. #define FIRMWARE_8168F_1 "/*(DEBLOBBED)*/"
  42. #define FIRMWARE_8168F_2 "/*(DEBLOBBED)*/"
  43. #define FIRMWARE_8105E_1 "/*(DEBLOBBED)*/"
  44. #define FIRMWARE_8402_1 "/*(DEBLOBBED)*/"
  45. #define FIRMWARE_8411_1 "/*(DEBLOBBED)*/"
  46. #define FIRMWARE_8411_2 "/*(DEBLOBBED)*/"
  47. #define FIRMWARE_8106E_1 "/*(DEBLOBBED)*/"
  48. #define FIRMWARE_8106E_2 "/*(DEBLOBBED)*/"
  49. #define FIRMWARE_8168G_2 "/*(DEBLOBBED)*/"
  50. #define FIRMWARE_8168G_3 "/*(DEBLOBBED)*/"
  51. #define FIRMWARE_8168H_1 "/*(DEBLOBBED)*/"
  52. #define FIRMWARE_8168H_2 "/*(DEBLOBBED)*/"
  53. #define FIRMWARE_8107E_1 "/*(DEBLOBBED)*/"
  54. #define FIRMWARE_8107E_2 "/*(DEBLOBBED)*/"
  55. #ifdef RTL8169_DEBUG
  56. #define assert(expr) \
  57. if (!(expr)) { \
  58. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  59. #expr,__FILE__,__func__,__LINE__); \
  60. }
  61. #define dprintk(fmt, args...) \
  62. do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
  63. #else
  64. #define assert(expr) do {} while (0)
  65. #define dprintk(fmt, args...) do {} while (0)
  66. #endif /* RTL8169_DEBUG */
  67. #define R8169_MSG_DEFAULT \
  68. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  69. #define TX_SLOTS_AVAIL(tp) \
  70. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
  71. /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
  72. #define TX_FRAGS_READY_FOR(tp,nr_frags) \
  73. (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
  74. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  75. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  76. static const int multicast_filter_limit = 32;
  77. #define MAX_READ_REQUEST_SHIFT 12
  78. #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
  79. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  80. #define R8169_REGS_SIZE 256
  81. #define R8169_NAPI_WEIGHT 64
  82. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  83. #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
  84. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  85. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  86. #define RTL8169_TX_TIMEOUT (6*HZ)
  87. #define RTL8169_PHY_TIMEOUT (10*HZ)
  88. /* write/read MMIO register */
  89. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  90. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  91. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  92. #define RTL_R8(reg) readb (ioaddr + (reg))
  93. #define RTL_R16(reg) readw (ioaddr + (reg))
  94. #define RTL_R32(reg) readl (ioaddr + (reg))
  95. enum mac_version {
  96. RTL_GIGA_MAC_VER_01 = 0,
  97. RTL_GIGA_MAC_VER_02,
  98. RTL_GIGA_MAC_VER_03,
  99. RTL_GIGA_MAC_VER_04,
  100. RTL_GIGA_MAC_VER_05,
  101. RTL_GIGA_MAC_VER_06,
  102. RTL_GIGA_MAC_VER_07,
  103. RTL_GIGA_MAC_VER_08,
  104. RTL_GIGA_MAC_VER_09,
  105. RTL_GIGA_MAC_VER_10,
  106. RTL_GIGA_MAC_VER_11,
  107. RTL_GIGA_MAC_VER_12,
  108. RTL_GIGA_MAC_VER_13,
  109. RTL_GIGA_MAC_VER_14,
  110. RTL_GIGA_MAC_VER_15,
  111. RTL_GIGA_MAC_VER_16,
  112. RTL_GIGA_MAC_VER_17,
  113. RTL_GIGA_MAC_VER_18,
  114. RTL_GIGA_MAC_VER_19,
  115. RTL_GIGA_MAC_VER_20,
  116. RTL_GIGA_MAC_VER_21,
  117. RTL_GIGA_MAC_VER_22,
  118. RTL_GIGA_MAC_VER_23,
  119. RTL_GIGA_MAC_VER_24,
  120. RTL_GIGA_MAC_VER_25,
  121. RTL_GIGA_MAC_VER_26,
  122. RTL_GIGA_MAC_VER_27,
  123. RTL_GIGA_MAC_VER_28,
  124. RTL_GIGA_MAC_VER_29,
  125. RTL_GIGA_MAC_VER_30,
  126. RTL_GIGA_MAC_VER_31,
  127. RTL_GIGA_MAC_VER_32,
  128. RTL_GIGA_MAC_VER_33,
  129. RTL_GIGA_MAC_VER_34,
  130. RTL_GIGA_MAC_VER_35,
  131. RTL_GIGA_MAC_VER_36,
  132. RTL_GIGA_MAC_VER_37,
  133. RTL_GIGA_MAC_VER_38,
  134. RTL_GIGA_MAC_VER_39,
  135. RTL_GIGA_MAC_VER_40,
  136. RTL_GIGA_MAC_VER_41,
  137. RTL_GIGA_MAC_VER_42,
  138. RTL_GIGA_MAC_VER_43,
  139. RTL_GIGA_MAC_VER_44,
  140. RTL_GIGA_MAC_VER_45,
  141. RTL_GIGA_MAC_VER_46,
  142. RTL_GIGA_MAC_VER_47,
  143. RTL_GIGA_MAC_VER_48,
  144. RTL_GIGA_MAC_VER_49,
  145. RTL_GIGA_MAC_VER_50,
  146. RTL_GIGA_MAC_VER_51,
  147. RTL_GIGA_MAC_NONE = 0xff,
  148. };
  149. enum rtl_tx_desc_version {
  150. RTL_TD_0 = 0,
  151. RTL_TD_1 = 1,
  152. };
  153. #define JUMBO_1K ETH_DATA_LEN
  154. #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
  155. #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
  156. #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
  157. #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
  158. #define _R(NAME,TD,FW,SZ,B) { \
  159. .name = NAME, \
  160. .txd_version = TD, \
  161. .fw_name = FW, \
  162. .jumbo_max = SZ, \
  163. .jumbo_tx_csum = B \
  164. }
  165. static const struct {
  166. const char *name;
  167. enum rtl_tx_desc_version txd_version;
  168. const char *fw_name;
  169. u16 jumbo_max;
  170. bool jumbo_tx_csum;
  171. } rtl_chip_infos[] = {
  172. /* PCI devices. */
  173. [RTL_GIGA_MAC_VER_01] =
  174. _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
  175. [RTL_GIGA_MAC_VER_02] =
  176. _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
  177. [RTL_GIGA_MAC_VER_03] =
  178. _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
  179. [RTL_GIGA_MAC_VER_04] =
  180. _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
  181. [RTL_GIGA_MAC_VER_05] =
  182. _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
  183. [RTL_GIGA_MAC_VER_06] =
  184. _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
  185. /* PCI-E devices. */
  186. [RTL_GIGA_MAC_VER_07] =
  187. _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
  188. [RTL_GIGA_MAC_VER_08] =
  189. _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
  190. [RTL_GIGA_MAC_VER_09] =
  191. _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
  192. [RTL_GIGA_MAC_VER_10] =
  193. _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
  194. [RTL_GIGA_MAC_VER_11] =
  195. _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
  196. [RTL_GIGA_MAC_VER_12] =
  197. _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
  198. [RTL_GIGA_MAC_VER_13] =
  199. _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
  200. [RTL_GIGA_MAC_VER_14] =
  201. _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
  202. [RTL_GIGA_MAC_VER_15] =
  203. _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
  204. [RTL_GIGA_MAC_VER_16] =
  205. _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
  206. [RTL_GIGA_MAC_VER_17] =
  207. _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
  208. [RTL_GIGA_MAC_VER_18] =
  209. _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
  210. [RTL_GIGA_MAC_VER_19] =
  211. _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
  212. [RTL_GIGA_MAC_VER_20] =
  213. _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
  214. [RTL_GIGA_MAC_VER_21] =
  215. _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
  216. [RTL_GIGA_MAC_VER_22] =
  217. _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
  218. [RTL_GIGA_MAC_VER_23] =
  219. _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
  220. [RTL_GIGA_MAC_VER_24] =
  221. _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
  222. [RTL_GIGA_MAC_VER_25] =
  223. _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
  224. JUMBO_9K, false),
  225. [RTL_GIGA_MAC_VER_26] =
  226. _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
  227. JUMBO_9K, false),
  228. [RTL_GIGA_MAC_VER_27] =
  229. _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
  230. [RTL_GIGA_MAC_VER_28] =
  231. _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
  232. [RTL_GIGA_MAC_VER_29] =
  233. _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
  234. JUMBO_1K, true),
  235. [RTL_GIGA_MAC_VER_30] =
  236. _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
  237. JUMBO_1K, true),
  238. [RTL_GIGA_MAC_VER_31] =
  239. _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
  240. [RTL_GIGA_MAC_VER_32] =
  241. _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
  242. JUMBO_9K, false),
  243. [RTL_GIGA_MAC_VER_33] =
  244. _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
  245. JUMBO_9K, false),
  246. [RTL_GIGA_MAC_VER_34] =
  247. _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
  248. JUMBO_9K, false),
  249. [RTL_GIGA_MAC_VER_35] =
  250. _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
  251. JUMBO_9K, false),
  252. [RTL_GIGA_MAC_VER_36] =
  253. _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
  254. JUMBO_9K, false),
  255. [RTL_GIGA_MAC_VER_37] =
  256. _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
  257. JUMBO_1K, true),
  258. [RTL_GIGA_MAC_VER_38] =
  259. _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
  260. JUMBO_9K, false),
  261. [RTL_GIGA_MAC_VER_39] =
  262. _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
  263. JUMBO_1K, true),
  264. [RTL_GIGA_MAC_VER_40] =
  265. _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
  266. JUMBO_9K, false),
  267. [RTL_GIGA_MAC_VER_41] =
  268. _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
  269. [RTL_GIGA_MAC_VER_42] =
  270. _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
  271. JUMBO_9K, false),
  272. [RTL_GIGA_MAC_VER_43] =
  273. _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
  274. JUMBO_1K, true),
  275. [RTL_GIGA_MAC_VER_44] =
  276. _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
  277. JUMBO_9K, false),
  278. [RTL_GIGA_MAC_VER_45] =
  279. _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
  280. JUMBO_9K, false),
  281. [RTL_GIGA_MAC_VER_46] =
  282. _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
  283. JUMBO_9K, false),
  284. [RTL_GIGA_MAC_VER_47] =
  285. _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
  286. JUMBO_1K, false),
  287. [RTL_GIGA_MAC_VER_48] =
  288. _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
  289. JUMBO_1K, false),
  290. [RTL_GIGA_MAC_VER_49] =
  291. _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
  292. JUMBO_9K, false),
  293. [RTL_GIGA_MAC_VER_50] =
  294. _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
  295. JUMBO_9K, false),
  296. [RTL_GIGA_MAC_VER_51] =
  297. _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
  298. JUMBO_9K, false),
  299. };
  300. #undef _R
  301. enum cfg_version {
  302. RTL_CFG_0 = 0x00,
  303. RTL_CFG_1,
  304. RTL_CFG_2
  305. };
  306. static const struct pci_device_id rtl8169_pci_tbl[] = {
  307. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  308. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  309. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
  310. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  311. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  312. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  313. { PCI_VENDOR_ID_DLINK, 0x4300,
  314. PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
  315. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  316. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
  317. { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
  318. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  319. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  320. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  321. { 0x0001, 0x8168,
  322. PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
  323. {0,},
  324. };
  325. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  326. static int rx_buf_sz = 16383;
  327. static int use_dac = -1;
  328. static struct {
  329. u32 msg_enable;
  330. } debug = { -1 };
  331. enum rtl_registers {
  332. MAC0 = 0, /* Ethernet hardware address. */
  333. MAC4 = 4,
  334. MAR0 = 8, /* Multicast filter. */
  335. CounterAddrLow = 0x10,
  336. CounterAddrHigh = 0x14,
  337. TxDescStartAddrLow = 0x20,
  338. TxDescStartAddrHigh = 0x24,
  339. TxHDescStartAddrLow = 0x28,
  340. TxHDescStartAddrHigh = 0x2c,
  341. FLASH = 0x30,
  342. ERSR = 0x36,
  343. ChipCmd = 0x37,
  344. TxPoll = 0x38,
  345. IntrMask = 0x3c,
  346. IntrStatus = 0x3e,
  347. TxConfig = 0x40,
  348. #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
  349. #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
  350. RxConfig = 0x44,
  351. #define RX128_INT_EN (1 << 15) /* 8111c and later */
  352. #define RX_MULTI_EN (1 << 14) /* 8111c only */
  353. #define RXCFG_FIFO_SHIFT 13
  354. /* No threshold before first PCI xfer */
  355. #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
  356. #define RX_EARLY_OFF (1 << 11)
  357. #define RXCFG_DMA_SHIFT 8
  358. /* Unlimited maximum PCI burst. */
  359. #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
  360. RxMissed = 0x4c,
  361. Cfg9346 = 0x50,
  362. Config0 = 0x51,
  363. Config1 = 0x52,
  364. Config2 = 0x53,
  365. #define PME_SIGNAL (1 << 5) /* 8168c and later */
  366. Config3 = 0x54,
  367. Config4 = 0x55,
  368. Config5 = 0x56,
  369. MultiIntr = 0x5c,
  370. PHYAR = 0x60,
  371. PHYstatus = 0x6c,
  372. RxMaxSize = 0xda,
  373. CPlusCmd = 0xe0,
  374. IntrMitigate = 0xe2,
  375. RxDescAddrLow = 0xe4,
  376. RxDescAddrHigh = 0xe8,
  377. EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
  378. #define NoEarlyTx 0x3f /* Max value : no early transmit. */
  379. MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
  380. #define TxPacketMax (8064 >> 7)
  381. #define EarlySize 0x27
  382. FuncEvent = 0xf0,
  383. FuncEventMask = 0xf4,
  384. FuncPresetState = 0xf8,
  385. IBCR0 = 0xf8,
  386. IBCR2 = 0xf9,
  387. IBIMR0 = 0xfa,
  388. IBISR0 = 0xfb,
  389. FuncForceEvent = 0xfc,
  390. };
  391. enum rtl8110_registers {
  392. TBICSR = 0x64,
  393. TBI_ANAR = 0x68,
  394. TBI_LPAR = 0x6a,
  395. };
  396. enum rtl8168_8101_registers {
  397. CSIDR = 0x64,
  398. CSIAR = 0x68,
  399. #define CSIAR_FLAG 0x80000000
  400. #define CSIAR_WRITE_CMD 0x80000000
  401. #define CSIAR_BYTE_ENABLE 0x0f
  402. #define CSIAR_BYTE_ENABLE_SHIFT 12
  403. #define CSIAR_ADDR_MASK 0x0fff
  404. #define CSIAR_FUNC_CARD 0x00000000
  405. #define CSIAR_FUNC_SDIO 0x00010000
  406. #define CSIAR_FUNC_NIC 0x00020000
  407. #define CSIAR_FUNC_NIC2 0x00010000
  408. PMCH = 0x6f,
  409. EPHYAR = 0x80,
  410. #define EPHYAR_FLAG 0x80000000
  411. #define EPHYAR_WRITE_CMD 0x80000000
  412. #define EPHYAR_REG_MASK 0x1f
  413. #define EPHYAR_REG_SHIFT 16
  414. #define EPHYAR_DATA_MASK 0xffff
  415. DLLPR = 0xd0,
  416. #define PFM_EN (1 << 6)
  417. #define TX_10M_PS_EN (1 << 7)
  418. DBG_REG = 0xd1,
  419. #define FIX_NAK_1 (1 << 4)
  420. #define FIX_NAK_2 (1 << 3)
  421. TWSI = 0xd2,
  422. MCU = 0xd3,
  423. #define NOW_IS_OOB (1 << 7)
  424. #define TX_EMPTY (1 << 5)
  425. #define RX_EMPTY (1 << 4)
  426. #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
  427. #define EN_NDP (1 << 3)
  428. #define EN_OOB_RESET (1 << 2)
  429. #define LINK_LIST_RDY (1 << 1)
  430. EFUSEAR = 0xdc,
  431. #define EFUSEAR_FLAG 0x80000000
  432. #define EFUSEAR_WRITE_CMD 0x80000000
  433. #define EFUSEAR_READ_CMD 0x00000000
  434. #define EFUSEAR_REG_MASK 0x03ff
  435. #define EFUSEAR_REG_SHIFT 8
  436. #define EFUSEAR_DATA_MASK 0xff
  437. MISC_1 = 0xf2,
  438. #define PFM_D3COLD_EN (1 << 6)
  439. };
  440. enum rtl8168_registers {
  441. LED_FREQ = 0x1a,
  442. EEE_LED = 0x1b,
  443. ERIDR = 0x70,
  444. ERIAR = 0x74,
  445. #define ERIAR_FLAG 0x80000000
  446. #define ERIAR_WRITE_CMD 0x80000000
  447. #define ERIAR_READ_CMD 0x00000000
  448. #define ERIAR_ADDR_BYTE_ALIGN 4
  449. #define ERIAR_TYPE_SHIFT 16
  450. #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
  451. #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
  452. #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
  453. #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
  454. #define ERIAR_MASK_SHIFT 12
  455. #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
  456. #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
  457. #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
  458. #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
  459. #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
  460. EPHY_RXER_NUM = 0x7c,
  461. OCPDR = 0xb0, /* OCP GPHY access */
  462. #define OCPDR_WRITE_CMD 0x80000000
  463. #define OCPDR_READ_CMD 0x00000000
  464. #define OCPDR_REG_MASK 0x7f
  465. #define OCPDR_GPHY_REG_SHIFT 16
  466. #define OCPDR_DATA_MASK 0xffff
  467. OCPAR = 0xb4,
  468. #define OCPAR_FLAG 0x80000000
  469. #define OCPAR_GPHY_WRITE_CMD 0x8000f060
  470. #define OCPAR_GPHY_READ_CMD 0x0000f060
  471. GPHY_OCP = 0xb8,
  472. RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
  473. MISC = 0xf0, /* 8168e only. */
  474. #define TXPLA_RST (1 << 29)
  475. #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
  476. #define PWM_EN (1 << 22)
  477. #define RXDV_GATED_EN (1 << 19)
  478. #define EARLY_TALLY_EN (1 << 16)
  479. };
  480. enum rtl_register_content {
  481. /* InterruptStatusBits */
  482. SYSErr = 0x8000,
  483. PCSTimeout = 0x4000,
  484. SWInt = 0x0100,
  485. TxDescUnavail = 0x0080,
  486. RxFIFOOver = 0x0040,
  487. LinkChg = 0x0020,
  488. RxOverflow = 0x0010,
  489. TxErr = 0x0008,
  490. TxOK = 0x0004,
  491. RxErr = 0x0002,
  492. RxOK = 0x0001,
  493. /* RxStatusDesc */
  494. RxBOVF = (1 << 24),
  495. RxFOVF = (1 << 23),
  496. RxRWT = (1 << 22),
  497. RxRES = (1 << 21),
  498. RxRUNT = (1 << 20),
  499. RxCRC = (1 << 19),
  500. /* ChipCmdBits */
  501. StopReq = 0x80,
  502. CmdReset = 0x10,
  503. CmdRxEnb = 0x08,
  504. CmdTxEnb = 0x04,
  505. RxBufEmpty = 0x01,
  506. /* TXPoll register p.5 */
  507. HPQ = 0x80, /* Poll cmd on the high prio queue */
  508. NPQ = 0x40, /* Poll cmd on the low prio queue */
  509. FSWInt = 0x01, /* Forced software interrupt */
  510. /* Cfg9346Bits */
  511. Cfg9346_Lock = 0x00,
  512. Cfg9346_Unlock = 0xc0,
  513. /* rx_mode_bits */
  514. AcceptErr = 0x20,
  515. AcceptRunt = 0x10,
  516. AcceptBroadcast = 0x08,
  517. AcceptMulticast = 0x04,
  518. AcceptMyPhys = 0x02,
  519. AcceptAllPhys = 0x01,
  520. #define RX_CONFIG_ACCEPT_MASK 0x3f
  521. /* TxConfigBits */
  522. TxInterFrameGapShift = 24,
  523. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  524. /* Config1 register p.24 */
  525. LEDS1 = (1 << 7),
  526. LEDS0 = (1 << 6),
  527. Speed_down = (1 << 4),
  528. MEMMAP = (1 << 3),
  529. IOMAP = (1 << 2),
  530. VPD = (1 << 1),
  531. PMEnable = (1 << 0), /* Power Management Enable */
  532. /* Config2 register p. 25 */
  533. ClkReqEn = (1 << 7), /* Clock Request Enable */
  534. MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
  535. PCI_Clock_66MHz = 0x01,
  536. PCI_Clock_33MHz = 0x00,
  537. /* Config3 register p.25 */
  538. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  539. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  540. Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
  541. Rdy_to_L23 = (1 << 1), /* L23 Enable */
  542. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  543. /* Config4 register */
  544. Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
  545. /* Config5 register p.27 */
  546. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  547. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  548. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  549. Spi_en = (1 << 3),
  550. LanWake = (1 << 1), /* LanWake enable/disable */
  551. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  552. ASPM_en = (1 << 0), /* ASPM enable */
  553. /* TBICSR p.28 */
  554. TBIReset = 0x80000000,
  555. TBILoopback = 0x40000000,
  556. TBINwEnable = 0x20000000,
  557. TBINwRestart = 0x10000000,
  558. TBILinkOk = 0x02000000,
  559. TBINwComplete = 0x01000000,
  560. /* CPlusCmd p.31 */
  561. EnableBist = (1 << 15), // 8168 8101
  562. Mac_dbgo_oe = (1 << 14), // 8168 8101
  563. Normal_mode = (1 << 13), // unused
  564. Force_half_dup = (1 << 12), // 8168 8101
  565. Force_rxflow_en = (1 << 11), // 8168 8101
  566. Force_txflow_en = (1 << 10), // 8168 8101
  567. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  568. ASF = (1 << 8), // 8168 8101
  569. PktCntrDisable = (1 << 7), // 8168 8101
  570. Mac_dbgo_sel = 0x001c, // 8168
  571. RxVlan = (1 << 6),
  572. RxChkSum = (1 << 5),
  573. PCIDAC = (1 << 4),
  574. PCIMulRW = (1 << 3),
  575. INTT_0 = 0x0000, // 8168
  576. INTT_1 = 0x0001, // 8168
  577. INTT_2 = 0x0002, // 8168
  578. INTT_3 = 0x0003, // 8168
  579. /* rtl8169_PHYstatus */
  580. TBI_Enable = 0x80,
  581. TxFlowCtrl = 0x40,
  582. RxFlowCtrl = 0x20,
  583. _1000bpsF = 0x10,
  584. _100bps = 0x08,
  585. _10bps = 0x04,
  586. LinkStatus = 0x02,
  587. FullDup = 0x01,
  588. /* _TBICSRBit */
  589. TBILinkOK = 0x02000000,
  590. /* ResetCounterCommand */
  591. CounterReset = 0x1,
  592. /* DumpCounterCommand */
  593. CounterDump = 0x8,
  594. /* magic enable v2 */
  595. MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
  596. };
  597. enum rtl_desc_bit {
  598. /* First doubleword. */
  599. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  600. RingEnd = (1 << 30), /* End of descriptor ring */
  601. FirstFrag = (1 << 29), /* First segment of a packet */
  602. LastFrag = (1 << 28), /* Final segment of a packet */
  603. };
  604. /* Generic case. */
  605. enum rtl_tx_desc_bit {
  606. /* First doubleword. */
  607. TD_LSO = (1 << 27), /* Large Send Offload */
  608. #define TD_MSS_MAX 0x07ffu /* MSS value */
  609. /* Second doubleword. */
  610. TxVlanTag = (1 << 17), /* Add VLAN tag */
  611. };
  612. /* 8169, 8168b and 810x except 8102e. */
  613. enum rtl_tx_desc_bit_0 {
  614. /* First doubleword. */
  615. #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
  616. TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
  617. TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
  618. TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
  619. };
  620. /* 8102e, 8168c and beyond. */
  621. enum rtl_tx_desc_bit_1 {
  622. /* First doubleword. */
  623. TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
  624. TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
  625. #define GTTCPHO_SHIFT 18
  626. #define GTTCPHO_MAX 0x7fU
  627. /* Second doubleword. */
  628. #define TCPHO_SHIFT 18
  629. #define TCPHO_MAX 0x3ffU
  630. #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
  631. TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
  632. TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
  633. TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
  634. TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
  635. };
  636. enum rtl_rx_desc_bit {
  637. /* Rx private */
  638. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  639. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  640. #define RxProtoUDP (PID1)
  641. #define RxProtoTCP (PID0)
  642. #define RxProtoIP (PID1 | PID0)
  643. #define RxProtoMask RxProtoIP
  644. IPFail = (1 << 16), /* IP checksum failed */
  645. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  646. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  647. RxVlanTag = (1 << 16), /* VLAN tag available */
  648. };
  649. #define RsvdMask 0x3fffc000
  650. struct TxDesc {
  651. __le32 opts1;
  652. __le32 opts2;
  653. __le64 addr;
  654. };
  655. struct RxDesc {
  656. __le32 opts1;
  657. __le32 opts2;
  658. __le64 addr;
  659. };
  660. struct ring_info {
  661. struct sk_buff *skb;
  662. u32 len;
  663. u8 __pad[sizeof(void *) - sizeof(u32)];
  664. };
  665. enum features {
  666. RTL_FEATURE_WOL = (1 << 0),
  667. RTL_FEATURE_MSI = (1 << 1),
  668. RTL_FEATURE_GMII = (1 << 2),
  669. };
  670. struct rtl8169_counters {
  671. __le64 tx_packets;
  672. __le64 rx_packets;
  673. __le64 tx_errors;
  674. __le32 rx_errors;
  675. __le16 rx_missed;
  676. __le16 align_errors;
  677. __le32 tx_one_collision;
  678. __le32 tx_multi_collision;
  679. __le64 rx_unicast;
  680. __le64 rx_broadcast;
  681. __le32 rx_multicast;
  682. __le16 tx_aborted;
  683. __le16 tx_underun;
  684. };
  685. struct rtl8169_tc_offsets {
  686. bool inited;
  687. __le64 tx_errors;
  688. __le32 tx_multi_collision;
  689. __le16 tx_aborted;
  690. };
  691. enum rtl_flag {
  692. RTL_FLAG_TASK_ENABLED,
  693. RTL_FLAG_TASK_SLOW_PENDING,
  694. RTL_FLAG_TASK_RESET_PENDING,
  695. RTL_FLAG_TASK_PHY_PENDING,
  696. RTL_FLAG_MAX
  697. };
  698. struct rtl8169_stats {
  699. u64 packets;
  700. u64 bytes;
  701. struct u64_stats_sync syncp;
  702. };
  703. struct rtl8169_private {
  704. void __iomem *mmio_addr; /* memory map physical address */
  705. struct pci_dev *pci_dev;
  706. struct net_device *dev;
  707. struct napi_struct napi;
  708. u32 msg_enable;
  709. u16 txd_version;
  710. u16 mac_version;
  711. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  712. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  713. u32 dirty_tx;
  714. struct rtl8169_stats rx_stats;
  715. struct rtl8169_stats tx_stats;
  716. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  717. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  718. dma_addr_t TxPhyAddr;
  719. dma_addr_t RxPhyAddr;
  720. void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
  721. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  722. struct timer_list timer;
  723. u16 cp_cmd;
  724. u16 event_slow;
  725. struct mdio_ops {
  726. void (*write)(struct rtl8169_private *, int, int);
  727. int (*read)(struct rtl8169_private *, int);
  728. } mdio_ops;
  729. struct pll_power_ops {
  730. void (*down)(struct rtl8169_private *);
  731. void (*up)(struct rtl8169_private *);
  732. } pll_power_ops;
  733. struct jumbo_ops {
  734. void (*enable)(struct rtl8169_private *);
  735. void (*disable)(struct rtl8169_private *);
  736. } jumbo_ops;
  737. struct csi_ops {
  738. void (*write)(struct rtl8169_private *, int, int);
  739. u32 (*read)(struct rtl8169_private *, int);
  740. } csi_ops;
  741. int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
  742. int (*get_settings)(struct net_device *, struct ethtool_cmd *);
  743. void (*phy_reset_enable)(struct rtl8169_private *tp);
  744. void (*hw_start)(struct net_device *);
  745. unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
  746. unsigned int (*link_ok)(void __iomem *);
  747. int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
  748. bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
  749. struct {
  750. DECLARE_BITMAP(flags, RTL_FLAG_MAX);
  751. struct mutex mutex;
  752. struct work_struct work;
  753. } wk;
  754. unsigned features;
  755. struct mii_if_info mii;
  756. dma_addr_t counters_phys_addr;
  757. struct rtl8169_counters *counters;
  758. struct rtl8169_tc_offsets tc_offset;
  759. u32 saved_wolopts;
  760. u32 opts1_mask;
  761. struct rtl_fw {
  762. const struct firmware *fw;
  763. #define RTL_VER_SIZE 32
  764. char version[RTL_VER_SIZE];
  765. struct rtl_fw_phy_action {
  766. __le32 *code;
  767. size_t size;
  768. } phy_action;
  769. } *rtl_fw;
  770. #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
  771. u32 ocp_base;
  772. };
  773. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  774. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  775. module_param(use_dac, int, 0);
  776. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  777. module_param_named(debug, debug.msg_enable, int, 0);
  778. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  779. MODULE_LICENSE("GPL");
  780. MODULE_VERSION(RTL8169_VERSION);
  781. /*(DEBLOBBED)*/
  782. static void rtl_lock_work(struct rtl8169_private *tp)
  783. {
  784. mutex_lock(&tp->wk.mutex);
  785. }
  786. static void rtl_unlock_work(struct rtl8169_private *tp)
  787. {
  788. mutex_unlock(&tp->wk.mutex);
  789. }
  790. static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
  791. {
  792. pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
  793. PCI_EXP_DEVCTL_READRQ, force);
  794. }
  795. struct rtl_cond {
  796. bool (*check)(struct rtl8169_private *);
  797. const char *msg;
  798. };
  799. static void rtl_udelay(unsigned int d)
  800. {
  801. udelay(d);
  802. }
  803. static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
  804. void (*delay)(unsigned int), unsigned int d, int n,
  805. bool high)
  806. {
  807. int i;
  808. for (i = 0; i < n; i++) {
  809. delay(d);
  810. if (c->check(tp) == high)
  811. return true;
  812. }
  813. netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
  814. c->msg, !high, n, d);
  815. return false;
  816. }
  817. static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
  818. const struct rtl_cond *c,
  819. unsigned int d, int n)
  820. {
  821. return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
  822. }
  823. static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
  824. const struct rtl_cond *c,
  825. unsigned int d, int n)
  826. {
  827. return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
  828. }
  829. static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
  830. const struct rtl_cond *c,
  831. unsigned int d, int n)
  832. {
  833. return rtl_loop_wait(tp, c, msleep, d, n, true);
  834. }
  835. static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
  836. const struct rtl_cond *c,
  837. unsigned int d, int n)
  838. {
  839. return rtl_loop_wait(tp, c, msleep, d, n, false);
  840. }
  841. #define DECLARE_RTL_COND(name) \
  842. static bool name ## _check(struct rtl8169_private *); \
  843. \
  844. static const struct rtl_cond name = { \
  845. .check = name ## _check, \
  846. .msg = #name \
  847. }; \
  848. \
  849. static bool name ## _check(struct rtl8169_private *tp)
  850. static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
  851. {
  852. if (reg & 0xffff0001) {
  853. netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
  854. return true;
  855. }
  856. return false;
  857. }
  858. DECLARE_RTL_COND(rtl_ocp_gphy_cond)
  859. {
  860. void __iomem *ioaddr = tp->mmio_addr;
  861. return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
  862. }
  863. static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
  864. {
  865. void __iomem *ioaddr = tp->mmio_addr;
  866. if (rtl_ocp_reg_failure(tp, reg))
  867. return;
  868. RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
  869. rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
  870. }
  871. static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
  872. {
  873. void __iomem *ioaddr = tp->mmio_addr;
  874. if (rtl_ocp_reg_failure(tp, reg))
  875. return 0;
  876. RTL_W32(GPHY_OCP, reg << 15);
  877. return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
  878. (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
  879. }
  880. static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
  881. {
  882. void __iomem *ioaddr = tp->mmio_addr;
  883. if (rtl_ocp_reg_failure(tp, reg))
  884. return;
  885. RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
  886. }
  887. static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
  888. {
  889. void __iomem *ioaddr = tp->mmio_addr;
  890. if (rtl_ocp_reg_failure(tp, reg))
  891. return 0;
  892. RTL_W32(OCPDR, reg << 15);
  893. return RTL_R32(OCPDR);
  894. }
  895. #define OCP_STD_PHY_BASE 0xa400
  896. static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
  897. {
  898. if (reg == 0x1f) {
  899. tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
  900. return;
  901. }
  902. if (tp->ocp_base != OCP_STD_PHY_BASE)
  903. reg -= 0x10;
  904. r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
  905. }
  906. static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
  907. {
  908. if (tp->ocp_base != OCP_STD_PHY_BASE)
  909. reg -= 0x10;
  910. return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
  911. }
  912. static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
  913. {
  914. if (reg == 0x1f) {
  915. tp->ocp_base = value << 4;
  916. return;
  917. }
  918. r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
  919. }
  920. static int mac_mcu_read(struct rtl8169_private *tp, int reg)
  921. {
  922. return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
  923. }
  924. DECLARE_RTL_COND(rtl_phyar_cond)
  925. {
  926. void __iomem *ioaddr = tp->mmio_addr;
  927. return RTL_R32(PHYAR) & 0x80000000;
  928. }
  929. static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
  930. {
  931. void __iomem *ioaddr = tp->mmio_addr;
  932. RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
  933. rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
  934. /*
  935. * According to hardware specs a 20us delay is required after write
  936. * complete indication, but before sending next command.
  937. */
  938. udelay(20);
  939. }
  940. static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
  941. {
  942. void __iomem *ioaddr = tp->mmio_addr;
  943. int value;
  944. RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
  945. value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
  946. RTL_R32(PHYAR) & 0xffff : ~0;
  947. /*
  948. * According to hardware specs a 20us delay is required after read
  949. * complete indication, but before sending next command.
  950. */
  951. udelay(20);
  952. return value;
  953. }
  954. DECLARE_RTL_COND(rtl_ocpar_cond)
  955. {
  956. void __iomem *ioaddr = tp->mmio_addr;
  957. return RTL_R32(OCPAR) & OCPAR_FLAG;
  958. }
  959. static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
  960. {
  961. void __iomem *ioaddr = tp->mmio_addr;
  962. RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
  963. RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
  964. RTL_W32(EPHY_RXER_NUM, 0);
  965. rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
  966. }
  967. static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
  968. {
  969. r8168dp_1_mdio_access(tp, reg,
  970. OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
  971. }
  972. static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
  973. {
  974. void __iomem *ioaddr = tp->mmio_addr;
  975. r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
  976. mdelay(1);
  977. RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
  978. RTL_W32(EPHY_RXER_NUM, 0);
  979. return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
  980. RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
  981. }
  982. #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
  983. static void r8168dp_2_mdio_start(void __iomem *ioaddr)
  984. {
  985. RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
  986. }
  987. static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
  988. {
  989. RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
  990. }
  991. static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
  992. {
  993. void __iomem *ioaddr = tp->mmio_addr;
  994. r8168dp_2_mdio_start(ioaddr);
  995. r8169_mdio_write(tp, reg, value);
  996. r8168dp_2_mdio_stop(ioaddr);
  997. }
  998. static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
  999. {
  1000. void __iomem *ioaddr = tp->mmio_addr;
  1001. int value;
  1002. r8168dp_2_mdio_start(ioaddr);
  1003. value = r8169_mdio_read(tp, reg);
  1004. r8168dp_2_mdio_stop(ioaddr);
  1005. return value;
  1006. }
  1007. static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
  1008. {
  1009. tp->mdio_ops.write(tp, location, val);
  1010. }
  1011. static int rtl_readphy(struct rtl8169_private *tp, int location)
  1012. {
  1013. return tp->mdio_ops.read(tp, location);
  1014. }
  1015. static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
  1016. {
  1017. rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
  1018. }
  1019. static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
  1020. {
  1021. int val;
  1022. val = rtl_readphy(tp, reg_addr);
  1023. rtl_writephy(tp, reg_addr, (val & ~m) | p);
  1024. }
  1025. static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
  1026. int val)
  1027. {
  1028. struct rtl8169_private *tp = netdev_priv(dev);
  1029. rtl_writephy(tp, location, val);
  1030. }
  1031. static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
  1032. {
  1033. struct rtl8169_private *tp = netdev_priv(dev);
  1034. return rtl_readphy(tp, location);
  1035. }
  1036. DECLARE_RTL_COND(rtl_ephyar_cond)
  1037. {
  1038. void __iomem *ioaddr = tp->mmio_addr;
  1039. return RTL_R32(EPHYAR) & EPHYAR_FLAG;
  1040. }
  1041. static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
  1042. {
  1043. void __iomem *ioaddr = tp->mmio_addr;
  1044. RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  1045. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  1046. rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
  1047. udelay(10);
  1048. }
  1049. static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
  1050. {
  1051. void __iomem *ioaddr = tp->mmio_addr;
  1052. RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  1053. return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
  1054. RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
  1055. }
  1056. DECLARE_RTL_COND(rtl_eriar_cond)
  1057. {
  1058. void __iomem *ioaddr = tp->mmio_addr;
  1059. return RTL_R32(ERIAR) & ERIAR_FLAG;
  1060. }
  1061. static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
  1062. u32 val, int type)
  1063. {
  1064. void __iomem *ioaddr = tp->mmio_addr;
  1065. BUG_ON((addr & 3) || (mask == 0));
  1066. RTL_W32(ERIDR, val);
  1067. RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
  1068. rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
  1069. }
  1070. static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
  1071. {
  1072. void __iomem *ioaddr = tp->mmio_addr;
  1073. RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
  1074. return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
  1075. RTL_R32(ERIDR) : ~0;
  1076. }
  1077. static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
  1078. u32 m, int type)
  1079. {
  1080. u32 val;
  1081. val = rtl_eri_read(tp, addr, type);
  1082. rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
  1083. }
  1084. static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  1085. {
  1086. void __iomem *ioaddr = tp->mmio_addr;
  1087. RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  1088. return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
  1089. RTL_R32(OCPDR) : ~0;
  1090. }
  1091. static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  1092. {
  1093. return rtl_eri_read(tp, reg, ERIAR_OOB);
  1094. }
  1095. static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  1096. {
  1097. switch (tp->mac_version) {
  1098. case RTL_GIGA_MAC_VER_27:
  1099. case RTL_GIGA_MAC_VER_28:
  1100. case RTL_GIGA_MAC_VER_31:
  1101. return r8168dp_ocp_read(tp, mask, reg);
  1102. case RTL_GIGA_MAC_VER_49:
  1103. case RTL_GIGA_MAC_VER_50:
  1104. case RTL_GIGA_MAC_VER_51:
  1105. return r8168ep_ocp_read(tp, mask, reg);
  1106. default:
  1107. BUG();
  1108. return ~0;
  1109. }
  1110. }
  1111. static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
  1112. u32 data)
  1113. {
  1114. void __iomem *ioaddr = tp->mmio_addr;
  1115. RTL_W32(OCPDR, data);
  1116. RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  1117. rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
  1118. }
  1119. static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
  1120. u32 data)
  1121. {
  1122. rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
  1123. data, ERIAR_OOB);
  1124. }
  1125. static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
  1126. {
  1127. switch (tp->mac_version) {
  1128. case RTL_GIGA_MAC_VER_27:
  1129. case RTL_GIGA_MAC_VER_28:
  1130. case RTL_GIGA_MAC_VER_31:
  1131. r8168dp_ocp_write(tp, mask, reg, data);
  1132. break;
  1133. case RTL_GIGA_MAC_VER_49:
  1134. case RTL_GIGA_MAC_VER_50:
  1135. case RTL_GIGA_MAC_VER_51:
  1136. r8168ep_ocp_write(tp, mask, reg, data);
  1137. break;
  1138. default:
  1139. BUG();
  1140. break;
  1141. }
  1142. }
  1143. static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
  1144. {
  1145. rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
  1146. ocp_write(tp, 0x1, 0x30, 0x00000001);
  1147. }
  1148. #define OOB_CMD_RESET 0x00
  1149. #define OOB_CMD_DRIVER_START 0x05
  1150. #define OOB_CMD_DRIVER_STOP 0x06
  1151. static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
  1152. {
  1153. return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
  1154. }
  1155. DECLARE_RTL_COND(rtl_ocp_read_cond)
  1156. {
  1157. u16 reg;
  1158. reg = rtl8168_get_ocp_reg(tp);
  1159. return ocp_read(tp, 0x0f, reg) & 0x00000800;
  1160. }
  1161. DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
  1162. {
  1163. return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
  1164. }
  1165. DECLARE_RTL_COND(rtl_ocp_tx_cond)
  1166. {
  1167. void __iomem *ioaddr = tp->mmio_addr;
  1168. return RTL_R8(IBISR0) & 0x20;
  1169. }
  1170. static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
  1171. {
  1172. void __iomem *ioaddr = tp->mmio_addr;
  1173. RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
  1174. rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
  1175. RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
  1176. RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
  1177. }
  1178. static void rtl8168dp_driver_start(struct rtl8169_private *tp)
  1179. {
  1180. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
  1181. rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
  1182. }
  1183. static void rtl8168ep_driver_start(struct rtl8169_private *tp)
  1184. {
  1185. ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
  1186. ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
  1187. rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
  1188. }
  1189. static void rtl8168_driver_start(struct rtl8169_private *tp)
  1190. {
  1191. switch (tp->mac_version) {
  1192. case RTL_GIGA_MAC_VER_27:
  1193. case RTL_GIGA_MAC_VER_28:
  1194. case RTL_GIGA_MAC_VER_31:
  1195. rtl8168dp_driver_start(tp);
  1196. break;
  1197. case RTL_GIGA_MAC_VER_49:
  1198. case RTL_GIGA_MAC_VER_50:
  1199. case RTL_GIGA_MAC_VER_51:
  1200. rtl8168ep_driver_start(tp);
  1201. break;
  1202. default:
  1203. BUG();
  1204. break;
  1205. }
  1206. }
  1207. static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
  1208. {
  1209. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
  1210. rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
  1211. }
  1212. static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
  1213. {
  1214. rtl8168ep_stop_cmac(tp);
  1215. ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
  1216. ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
  1217. rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
  1218. }
  1219. static void rtl8168_driver_stop(struct rtl8169_private *tp)
  1220. {
  1221. switch (tp->mac_version) {
  1222. case RTL_GIGA_MAC_VER_27:
  1223. case RTL_GIGA_MAC_VER_28:
  1224. case RTL_GIGA_MAC_VER_31:
  1225. rtl8168dp_driver_stop(tp);
  1226. break;
  1227. case RTL_GIGA_MAC_VER_49:
  1228. case RTL_GIGA_MAC_VER_50:
  1229. case RTL_GIGA_MAC_VER_51:
  1230. rtl8168ep_driver_stop(tp);
  1231. break;
  1232. default:
  1233. BUG();
  1234. break;
  1235. }
  1236. }
  1237. static int r8168dp_check_dash(struct rtl8169_private *tp)
  1238. {
  1239. u16 reg = rtl8168_get_ocp_reg(tp);
  1240. return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
  1241. }
  1242. static int r8168ep_check_dash(struct rtl8169_private *tp)
  1243. {
  1244. return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
  1245. }
  1246. static int r8168_check_dash(struct rtl8169_private *tp)
  1247. {
  1248. switch (tp->mac_version) {
  1249. case RTL_GIGA_MAC_VER_27:
  1250. case RTL_GIGA_MAC_VER_28:
  1251. case RTL_GIGA_MAC_VER_31:
  1252. return r8168dp_check_dash(tp);
  1253. case RTL_GIGA_MAC_VER_49:
  1254. case RTL_GIGA_MAC_VER_50:
  1255. case RTL_GIGA_MAC_VER_51:
  1256. return r8168ep_check_dash(tp);
  1257. default:
  1258. return 0;
  1259. }
  1260. }
  1261. struct exgmac_reg {
  1262. u16 addr;
  1263. u16 mask;
  1264. u32 val;
  1265. };
  1266. static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
  1267. const struct exgmac_reg *r, int len)
  1268. {
  1269. while (len-- > 0) {
  1270. rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
  1271. r++;
  1272. }
  1273. }
  1274. DECLARE_RTL_COND(rtl_efusear_cond)
  1275. {
  1276. void __iomem *ioaddr = tp->mmio_addr;
  1277. return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
  1278. }
  1279. static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
  1280. {
  1281. void __iomem *ioaddr = tp->mmio_addr;
  1282. RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  1283. return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
  1284. RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
  1285. }
  1286. static u16 rtl_get_events(struct rtl8169_private *tp)
  1287. {
  1288. void __iomem *ioaddr = tp->mmio_addr;
  1289. return RTL_R16(IntrStatus);
  1290. }
  1291. static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
  1292. {
  1293. void __iomem *ioaddr = tp->mmio_addr;
  1294. RTL_W16(IntrStatus, bits);
  1295. mmiowb();
  1296. }
  1297. static void rtl_irq_disable(struct rtl8169_private *tp)
  1298. {
  1299. void __iomem *ioaddr = tp->mmio_addr;
  1300. RTL_W16(IntrMask, 0);
  1301. mmiowb();
  1302. }
  1303. static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
  1304. {
  1305. void __iomem *ioaddr = tp->mmio_addr;
  1306. RTL_W16(IntrMask, bits);
  1307. }
  1308. #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
  1309. #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
  1310. #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
  1311. static void rtl_irq_enable_all(struct rtl8169_private *tp)
  1312. {
  1313. rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
  1314. }
  1315. static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
  1316. {
  1317. void __iomem *ioaddr = tp->mmio_addr;
  1318. rtl_irq_disable(tp);
  1319. rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
  1320. RTL_R8(ChipCmd);
  1321. }
  1322. static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
  1323. {
  1324. void __iomem *ioaddr = tp->mmio_addr;
  1325. return RTL_R32(TBICSR) & TBIReset;
  1326. }
  1327. static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
  1328. {
  1329. return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
  1330. }
  1331. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  1332. {
  1333. return RTL_R32(TBICSR) & TBILinkOk;
  1334. }
  1335. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  1336. {
  1337. return RTL_R8(PHYstatus) & LinkStatus;
  1338. }
  1339. static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
  1340. {
  1341. void __iomem *ioaddr = tp->mmio_addr;
  1342. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  1343. }
  1344. static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
  1345. {
  1346. unsigned int val;
  1347. val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
  1348. rtl_writephy(tp, MII_BMCR, val & 0xffff);
  1349. }
  1350. static void rtl_link_chg_patch(struct rtl8169_private *tp)
  1351. {
  1352. void __iomem *ioaddr = tp->mmio_addr;
  1353. struct net_device *dev = tp->dev;
  1354. if (!netif_running(dev))
  1355. return;
  1356. if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
  1357. tp->mac_version == RTL_GIGA_MAC_VER_38) {
  1358. if (RTL_R8(PHYstatus) & _1000bpsF) {
  1359. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
  1360. ERIAR_EXGMAC);
  1361. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
  1362. ERIAR_EXGMAC);
  1363. } else if (RTL_R8(PHYstatus) & _100bps) {
  1364. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
  1365. ERIAR_EXGMAC);
  1366. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
  1367. ERIAR_EXGMAC);
  1368. } else {
  1369. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
  1370. ERIAR_EXGMAC);
  1371. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
  1372. ERIAR_EXGMAC);
  1373. }
  1374. /* Reset packet filter */
  1375. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
  1376. ERIAR_EXGMAC);
  1377. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
  1378. ERIAR_EXGMAC);
  1379. } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  1380. tp->mac_version == RTL_GIGA_MAC_VER_36) {
  1381. if (RTL_R8(PHYstatus) & _1000bpsF) {
  1382. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
  1383. ERIAR_EXGMAC);
  1384. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
  1385. ERIAR_EXGMAC);
  1386. } else {
  1387. rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
  1388. ERIAR_EXGMAC);
  1389. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
  1390. ERIAR_EXGMAC);
  1391. }
  1392. } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
  1393. if (RTL_R8(PHYstatus) & _10bps) {
  1394. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
  1395. ERIAR_EXGMAC);
  1396. rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
  1397. ERIAR_EXGMAC);
  1398. } else {
  1399. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
  1400. ERIAR_EXGMAC);
  1401. }
  1402. }
  1403. }
  1404. static void __rtl8169_check_link_status(struct net_device *dev,
  1405. struct rtl8169_private *tp,
  1406. void __iomem *ioaddr, bool pm)
  1407. {
  1408. if (tp->link_ok(ioaddr)) {
  1409. rtl_link_chg_patch(tp);
  1410. /* This is to cancel a scheduled suspend if there's one. */
  1411. if (pm)
  1412. pm_request_resume(&tp->pci_dev->dev);
  1413. netif_carrier_on(dev);
  1414. if (net_ratelimit())
  1415. netif_info(tp, ifup, dev, "link up\n");
  1416. } else {
  1417. netif_carrier_off(dev);
  1418. netif_info(tp, ifdown, dev, "link down\n");
  1419. if (pm)
  1420. pm_schedule_suspend(&tp->pci_dev->dev, 5000);
  1421. }
  1422. }
  1423. static void rtl8169_check_link_status(struct net_device *dev,
  1424. struct rtl8169_private *tp,
  1425. void __iomem *ioaddr)
  1426. {
  1427. __rtl8169_check_link_status(dev, tp, ioaddr, false);
  1428. }
  1429. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  1430. static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
  1431. {
  1432. void __iomem *ioaddr = tp->mmio_addr;
  1433. u8 options;
  1434. u32 wolopts = 0;
  1435. options = RTL_R8(Config1);
  1436. if (!(options & PMEnable))
  1437. return 0;
  1438. options = RTL_R8(Config3);
  1439. if (options & LinkUp)
  1440. wolopts |= WAKE_PHY;
  1441. switch (tp->mac_version) {
  1442. case RTL_GIGA_MAC_VER_34:
  1443. case RTL_GIGA_MAC_VER_35:
  1444. case RTL_GIGA_MAC_VER_36:
  1445. case RTL_GIGA_MAC_VER_37:
  1446. case RTL_GIGA_MAC_VER_38:
  1447. case RTL_GIGA_MAC_VER_40:
  1448. case RTL_GIGA_MAC_VER_41:
  1449. case RTL_GIGA_MAC_VER_42:
  1450. case RTL_GIGA_MAC_VER_43:
  1451. case RTL_GIGA_MAC_VER_44:
  1452. case RTL_GIGA_MAC_VER_45:
  1453. case RTL_GIGA_MAC_VER_46:
  1454. case RTL_GIGA_MAC_VER_47:
  1455. case RTL_GIGA_MAC_VER_48:
  1456. case RTL_GIGA_MAC_VER_49:
  1457. case RTL_GIGA_MAC_VER_50:
  1458. case RTL_GIGA_MAC_VER_51:
  1459. if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
  1460. wolopts |= WAKE_MAGIC;
  1461. break;
  1462. default:
  1463. if (options & MagicPacket)
  1464. wolopts |= WAKE_MAGIC;
  1465. break;
  1466. }
  1467. options = RTL_R8(Config5);
  1468. if (options & UWF)
  1469. wolopts |= WAKE_UCAST;
  1470. if (options & BWF)
  1471. wolopts |= WAKE_BCAST;
  1472. if (options & MWF)
  1473. wolopts |= WAKE_MCAST;
  1474. return wolopts;
  1475. }
  1476. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1477. {
  1478. struct rtl8169_private *tp = netdev_priv(dev);
  1479. struct device *d = &tp->pci_dev->dev;
  1480. pm_runtime_get_noresume(d);
  1481. rtl_lock_work(tp);
  1482. wol->supported = WAKE_ANY;
  1483. if (pm_runtime_active(d))
  1484. wol->wolopts = __rtl8169_get_wol(tp);
  1485. else
  1486. wol->wolopts = tp->saved_wolopts;
  1487. rtl_unlock_work(tp);
  1488. pm_runtime_put_noidle(d);
  1489. }
  1490. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  1491. {
  1492. void __iomem *ioaddr = tp->mmio_addr;
  1493. unsigned int i, tmp;
  1494. static const struct {
  1495. u32 opt;
  1496. u16 reg;
  1497. u8 mask;
  1498. } cfg[] = {
  1499. { WAKE_PHY, Config3, LinkUp },
  1500. { WAKE_UCAST, Config5, UWF },
  1501. { WAKE_BCAST, Config5, BWF },
  1502. { WAKE_MCAST, Config5, MWF },
  1503. { WAKE_ANY, Config5, LanWake },
  1504. { WAKE_MAGIC, Config3, MagicPacket }
  1505. };
  1506. u8 options;
  1507. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1508. switch (tp->mac_version) {
  1509. case RTL_GIGA_MAC_VER_34:
  1510. case RTL_GIGA_MAC_VER_35:
  1511. case RTL_GIGA_MAC_VER_36:
  1512. case RTL_GIGA_MAC_VER_37:
  1513. case RTL_GIGA_MAC_VER_38:
  1514. case RTL_GIGA_MAC_VER_40:
  1515. case RTL_GIGA_MAC_VER_41:
  1516. case RTL_GIGA_MAC_VER_42:
  1517. case RTL_GIGA_MAC_VER_43:
  1518. case RTL_GIGA_MAC_VER_44:
  1519. case RTL_GIGA_MAC_VER_45:
  1520. case RTL_GIGA_MAC_VER_46:
  1521. case RTL_GIGA_MAC_VER_47:
  1522. case RTL_GIGA_MAC_VER_48:
  1523. case RTL_GIGA_MAC_VER_49:
  1524. case RTL_GIGA_MAC_VER_50:
  1525. case RTL_GIGA_MAC_VER_51:
  1526. tmp = ARRAY_SIZE(cfg) - 1;
  1527. if (wolopts & WAKE_MAGIC)
  1528. rtl_w0w1_eri(tp,
  1529. 0x0dc,
  1530. ERIAR_MASK_0100,
  1531. MagicPacket_v2,
  1532. 0x0000,
  1533. ERIAR_EXGMAC);
  1534. else
  1535. rtl_w0w1_eri(tp,
  1536. 0x0dc,
  1537. ERIAR_MASK_0100,
  1538. 0x0000,
  1539. MagicPacket_v2,
  1540. ERIAR_EXGMAC);
  1541. break;
  1542. default:
  1543. tmp = ARRAY_SIZE(cfg);
  1544. break;
  1545. }
  1546. for (i = 0; i < tmp; i++) {
  1547. options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  1548. if (wolopts & cfg[i].opt)
  1549. options |= cfg[i].mask;
  1550. RTL_W8(cfg[i].reg, options);
  1551. }
  1552. switch (tp->mac_version) {
  1553. case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
  1554. options = RTL_R8(Config1) & ~PMEnable;
  1555. if (wolopts)
  1556. options |= PMEnable;
  1557. RTL_W8(Config1, options);
  1558. break;
  1559. default:
  1560. options = RTL_R8(Config2) & ~PME_SIGNAL;
  1561. if (wolopts)
  1562. options |= PME_SIGNAL;
  1563. RTL_W8(Config2, options);
  1564. break;
  1565. }
  1566. RTL_W8(Cfg9346, Cfg9346_Lock);
  1567. }
  1568. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1569. {
  1570. struct rtl8169_private *tp = netdev_priv(dev);
  1571. struct device *d = &tp->pci_dev->dev;
  1572. pm_runtime_get_noresume(d);
  1573. rtl_lock_work(tp);
  1574. if (wol->wolopts)
  1575. tp->features |= RTL_FEATURE_WOL;
  1576. else
  1577. tp->features &= ~RTL_FEATURE_WOL;
  1578. if (pm_runtime_active(d))
  1579. __rtl8169_set_wol(tp, wol->wolopts);
  1580. else
  1581. tp->saved_wolopts = wol->wolopts;
  1582. rtl_unlock_work(tp);
  1583. device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
  1584. pm_runtime_put_noidle(d);
  1585. return 0;
  1586. }
  1587. static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
  1588. {
  1589. return rtl_chip_infos[tp->mac_version].fw_name;
  1590. }
  1591. static void rtl8169_get_drvinfo(struct net_device *dev,
  1592. struct ethtool_drvinfo *info)
  1593. {
  1594. struct rtl8169_private *tp = netdev_priv(dev);
  1595. struct rtl_fw *rtl_fw = tp->rtl_fw;
  1596. strlcpy(info->driver, MODULENAME, sizeof(info->driver));
  1597. strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
  1598. strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
  1599. BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
  1600. if (!IS_ERR_OR_NULL(rtl_fw))
  1601. strlcpy(info->fw_version, rtl_fw->version,
  1602. sizeof(info->fw_version));
  1603. }
  1604. static int rtl8169_get_regs_len(struct net_device *dev)
  1605. {
  1606. return R8169_REGS_SIZE;
  1607. }
  1608. static int rtl8169_set_speed_tbi(struct net_device *dev,
  1609. u8 autoneg, u16 speed, u8 duplex, u32 ignored)
  1610. {
  1611. struct rtl8169_private *tp = netdev_priv(dev);
  1612. void __iomem *ioaddr = tp->mmio_addr;
  1613. int ret = 0;
  1614. u32 reg;
  1615. reg = RTL_R32(TBICSR);
  1616. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  1617. (duplex == DUPLEX_FULL)) {
  1618. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  1619. } else if (autoneg == AUTONEG_ENABLE)
  1620. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  1621. else {
  1622. netif_warn(tp, link, dev,
  1623. "incorrect speed setting refused in TBI mode\n");
  1624. ret = -EOPNOTSUPP;
  1625. }
  1626. return ret;
  1627. }
  1628. static int rtl8169_set_speed_xmii(struct net_device *dev,
  1629. u8 autoneg, u16 speed, u8 duplex, u32 adv)
  1630. {
  1631. struct rtl8169_private *tp = netdev_priv(dev);
  1632. int giga_ctrl, bmcr;
  1633. int rc = -EINVAL;
  1634. rtl_writephy(tp, 0x1f, 0x0000);
  1635. if (autoneg == AUTONEG_ENABLE) {
  1636. int auto_nego;
  1637. auto_nego = rtl_readphy(tp, MII_ADVERTISE);
  1638. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  1639. ADVERTISE_100HALF | ADVERTISE_100FULL);
  1640. if (adv & ADVERTISED_10baseT_Half)
  1641. auto_nego |= ADVERTISE_10HALF;
  1642. if (adv & ADVERTISED_10baseT_Full)
  1643. auto_nego |= ADVERTISE_10FULL;
  1644. if (adv & ADVERTISED_100baseT_Half)
  1645. auto_nego |= ADVERTISE_100HALF;
  1646. if (adv & ADVERTISED_100baseT_Full)
  1647. auto_nego |= ADVERTISE_100FULL;
  1648. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1649. giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
  1650. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  1651. /* The 8100e/8101e/8102e do Fast Ethernet only. */
  1652. if (tp->mii.supports_gmii) {
  1653. if (adv & ADVERTISED_1000baseT_Half)
  1654. giga_ctrl |= ADVERTISE_1000HALF;
  1655. if (adv & ADVERTISED_1000baseT_Full)
  1656. giga_ctrl |= ADVERTISE_1000FULL;
  1657. } else if (adv & (ADVERTISED_1000baseT_Half |
  1658. ADVERTISED_1000baseT_Full)) {
  1659. netif_info(tp, link, dev,
  1660. "PHY does not support 1000Mbps\n");
  1661. goto out;
  1662. }
  1663. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  1664. rtl_writephy(tp, MII_ADVERTISE, auto_nego);
  1665. rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
  1666. } else {
  1667. giga_ctrl = 0;
  1668. if (speed == SPEED_10)
  1669. bmcr = 0;
  1670. else if (speed == SPEED_100)
  1671. bmcr = BMCR_SPEED100;
  1672. else
  1673. goto out;
  1674. if (duplex == DUPLEX_FULL)
  1675. bmcr |= BMCR_FULLDPLX;
  1676. }
  1677. rtl_writephy(tp, MII_BMCR, bmcr);
  1678. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  1679. tp->mac_version == RTL_GIGA_MAC_VER_03) {
  1680. if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
  1681. rtl_writephy(tp, 0x17, 0x2138);
  1682. rtl_writephy(tp, 0x0e, 0x0260);
  1683. } else {
  1684. rtl_writephy(tp, 0x17, 0x2108);
  1685. rtl_writephy(tp, 0x0e, 0x0000);
  1686. }
  1687. }
  1688. rc = 0;
  1689. out:
  1690. return rc;
  1691. }
  1692. static int rtl8169_set_speed(struct net_device *dev,
  1693. u8 autoneg, u16 speed, u8 duplex, u32 advertising)
  1694. {
  1695. struct rtl8169_private *tp = netdev_priv(dev);
  1696. int ret;
  1697. ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
  1698. if (ret < 0)
  1699. goto out;
  1700. if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
  1701. (advertising & ADVERTISED_1000baseT_Full) &&
  1702. !pci_is_pcie(tp->pci_dev)) {
  1703. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  1704. }
  1705. out:
  1706. return ret;
  1707. }
  1708. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1709. {
  1710. struct rtl8169_private *tp = netdev_priv(dev);
  1711. int ret;
  1712. del_timer_sync(&tp->timer);
  1713. rtl_lock_work(tp);
  1714. ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
  1715. cmd->duplex, cmd->advertising);
  1716. rtl_unlock_work(tp);
  1717. return ret;
  1718. }
  1719. static netdev_features_t rtl8169_fix_features(struct net_device *dev,
  1720. netdev_features_t features)
  1721. {
  1722. struct rtl8169_private *tp = netdev_priv(dev);
  1723. if (dev->mtu > TD_MSS_MAX)
  1724. features &= ~NETIF_F_ALL_TSO;
  1725. if (dev->mtu > JUMBO_1K &&
  1726. !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
  1727. features &= ~NETIF_F_IP_CSUM;
  1728. return features;
  1729. }
  1730. static void __rtl8169_set_features(struct net_device *dev,
  1731. netdev_features_t features)
  1732. {
  1733. struct rtl8169_private *tp = netdev_priv(dev);
  1734. void __iomem *ioaddr = tp->mmio_addr;
  1735. u32 rx_config;
  1736. rx_config = RTL_R32(RxConfig);
  1737. if (features & NETIF_F_RXALL)
  1738. rx_config |= (AcceptErr | AcceptRunt);
  1739. else
  1740. rx_config &= ~(AcceptErr | AcceptRunt);
  1741. RTL_W32(RxConfig, rx_config);
  1742. if (features & NETIF_F_RXCSUM)
  1743. tp->cp_cmd |= RxChkSum;
  1744. else
  1745. tp->cp_cmd &= ~RxChkSum;
  1746. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1747. tp->cp_cmd |= RxVlan;
  1748. else
  1749. tp->cp_cmd &= ~RxVlan;
  1750. tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
  1751. RTL_W16(CPlusCmd, tp->cp_cmd);
  1752. RTL_R16(CPlusCmd);
  1753. }
  1754. static int rtl8169_set_features(struct net_device *dev,
  1755. netdev_features_t features)
  1756. {
  1757. struct rtl8169_private *tp = netdev_priv(dev);
  1758. features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
  1759. rtl_lock_work(tp);
  1760. if (features ^ dev->features)
  1761. __rtl8169_set_features(dev, features);
  1762. rtl_unlock_work(tp);
  1763. return 0;
  1764. }
  1765. static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
  1766. {
  1767. return (skb_vlan_tag_present(skb)) ?
  1768. TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
  1769. }
  1770. static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
  1771. {
  1772. u32 opts2 = le32_to_cpu(desc->opts2);
  1773. if (opts2 & RxVlanTag)
  1774. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
  1775. }
  1776. static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  1777. {
  1778. struct rtl8169_private *tp = netdev_priv(dev);
  1779. void __iomem *ioaddr = tp->mmio_addr;
  1780. u32 status;
  1781. cmd->supported =
  1782. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1783. cmd->port = PORT_FIBRE;
  1784. cmd->transceiver = XCVR_INTERNAL;
  1785. status = RTL_R32(TBICSR);
  1786. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  1787. cmd->autoneg = !!(status & TBINwEnable);
  1788. ethtool_cmd_speed_set(cmd, SPEED_1000);
  1789. cmd->duplex = DUPLEX_FULL; /* Always set */
  1790. return 0;
  1791. }
  1792. static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  1793. {
  1794. struct rtl8169_private *tp = netdev_priv(dev);
  1795. return mii_ethtool_gset(&tp->mii, cmd);
  1796. }
  1797. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1798. {
  1799. struct rtl8169_private *tp = netdev_priv(dev);
  1800. int rc;
  1801. rtl_lock_work(tp);
  1802. rc = tp->get_settings(dev, cmd);
  1803. rtl_unlock_work(tp);
  1804. return rc;
  1805. }
  1806. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1807. void *p)
  1808. {
  1809. struct rtl8169_private *tp = netdev_priv(dev);
  1810. u32 __iomem *data = tp->mmio_addr;
  1811. u32 *dw = p;
  1812. int i;
  1813. rtl_lock_work(tp);
  1814. for (i = 0; i < R8169_REGS_SIZE; i += 4)
  1815. memcpy_fromio(dw++, data++, 4);
  1816. rtl_unlock_work(tp);
  1817. }
  1818. static u32 rtl8169_get_msglevel(struct net_device *dev)
  1819. {
  1820. struct rtl8169_private *tp = netdev_priv(dev);
  1821. return tp->msg_enable;
  1822. }
  1823. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1824. {
  1825. struct rtl8169_private *tp = netdev_priv(dev);
  1826. tp->msg_enable = value;
  1827. }
  1828. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1829. "tx_packets",
  1830. "rx_packets",
  1831. "tx_errors",
  1832. "rx_errors",
  1833. "rx_missed",
  1834. "align_errors",
  1835. "tx_single_collisions",
  1836. "tx_multi_collisions",
  1837. "unicast",
  1838. "broadcast",
  1839. "multicast",
  1840. "tx_aborted",
  1841. "tx_underrun",
  1842. };
  1843. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1844. {
  1845. switch (sset) {
  1846. case ETH_SS_STATS:
  1847. return ARRAY_SIZE(rtl8169_gstrings);
  1848. default:
  1849. return -EOPNOTSUPP;
  1850. }
  1851. }
  1852. DECLARE_RTL_COND(rtl_counters_cond)
  1853. {
  1854. void __iomem *ioaddr = tp->mmio_addr;
  1855. return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
  1856. }
  1857. static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
  1858. {
  1859. struct rtl8169_private *tp = netdev_priv(dev);
  1860. void __iomem *ioaddr = tp->mmio_addr;
  1861. dma_addr_t paddr = tp->counters_phys_addr;
  1862. u32 cmd;
  1863. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  1864. RTL_R32(CounterAddrHigh);
  1865. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1866. RTL_W32(CounterAddrLow, cmd);
  1867. RTL_W32(CounterAddrLow, cmd | counter_cmd);
  1868. return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
  1869. }
  1870. static bool rtl8169_reset_counters(struct net_device *dev)
  1871. {
  1872. struct rtl8169_private *tp = netdev_priv(dev);
  1873. /*
  1874. * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
  1875. * tally counters.
  1876. */
  1877. if (tp->mac_version < RTL_GIGA_MAC_VER_19)
  1878. return true;
  1879. return rtl8169_do_counters(dev, CounterReset);
  1880. }
  1881. static bool rtl8169_update_counters(struct net_device *dev)
  1882. {
  1883. struct rtl8169_private *tp = netdev_priv(dev);
  1884. void __iomem *ioaddr = tp->mmio_addr;
  1885. /*
  1886. * Some chips are unable to dump tally counters when the receiver
  1887. * is disabled.
  1888. */
  1889. if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
  1890. return true;
  1891. return rtl8169_do_counters(dev, CounterDump);
  1892. }
  1893. static bool rtl8169_init_counter_offsets(struct net_device *dev)
  1894. {
  1895. struct rtl8169_private *tp = netdev_priv(dev);
  1896. struct rtl8169_counters *counters = tp->counters;
  1897. bool ret = false;
  1898. /*
  1899. * rtl8169_init_counter_offsets is called from rtl_open. On chip
  1900. * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
  1901. * reset by a power cycle, while the counter values collected by the
  1902. * driver are reset at every driver unload/load cycle.
  1903. *
  1904. * To make sure the HW values returned by @get_stats64 match the SW
  1905. * values, we collect the initial values at first open(*) and use them
  1906. * as offsets to normalize the values returned by @get_stats64.
  1907. *
  1908. * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
  1909. * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
  1910. * set at open time by rtl_hw_start.
  1911. */
  1912. if (tp->tc_offset.inited)
  1913. return true;
  1914. /* If both, reset and update fail, propagate to caller. */
  1915. if (rtl8169_reset_counters(dev))
  1916. ret = true;
  1917. if (rtl8169_update_counters(dev))
  1918. ret = true;
  1919. tp->tc_offset.tx_errors = counters->tx_errors;
  1920. tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
  1921. tp->tc_offset.tx_aborted = counters->tx_aborted;
  1922. tp->tc_offset.inited = true;
  1923. return ret;
  1924. }
  1925. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1926. struct ethtool_stats *stats, u64 *data)
  1927. {
  1928. struct rtl8169_private *tp = netdev_priv(dev);
  1929. struct device *d = &tp->pci_dev->dev;
  1930. struct rtl8169_counters *counters = tp->counters;
  1931. ASSERT_RTNL();
  1932. pm_runtime_get_noresume(d);
  1933. if (pm_runtime_active(d))
  1934. rtl8169_update_counters(dev);
  1935. pm_runtime_put_noidle(d);
  1936. data[0] = le64_to_cpu(counters->tx_packets);
  1937. data[1] = le64_to_cpu(counters->rx_packets);
  1938. data[2] = le64_to_cpu(counters->tx_errors);
  1939. data[3] = le32_to_cpu(counters->rx_errors);
  1940. data[4] = le16_to_cpu(counters->rx_missed);
  1941. data[5] = le16_to_cpu(counters->align_errors);
  1942. data[6] = le32_to_cpu(counters->tx_one_collision);
  1943. data[7] = le32_to_cpu(counters->tx_multi_collision);
  1944. data[8] = le64_to_cpu(counters->rx_unicast);
  1945. data[9] = le64_to_cpu(counters->rx_broadcast);
  1946. data[10] = le32_to_cpu(counters->rx_multicast);
  1947. data[11] = le16_to_cpu(counters->tx_aborted);
  1948. data[12] = le16_to_cpu(counters->tx_underun);
  1949. }
  1950. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1951. {
  1952. switch(stringset) {
  1953. case ETH_SS_STATS:
  1954. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1955. break;
  1956. }
  1957. }
  1958. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1959. .get_drvinfo = rtl8169_get_drvinfo,
  1960. .get_regs_len = rtl8169_get_regs_len,
  1961. .get_link = ethtool_op_get_link,
  1962. .get_settings = rtl8169_get_settings,
  1963. .set_settings = rtl8169_set_settings,
  1964. .get_msglevel = rtl8169_get_msglevel,
  1965. .set_msglevel = rtl8169_set_msglevel,
  1966. .get_regs = rtl8169_get_regs,
  1967. .get_wol = rtl8169_get_wol,
  1968. .set_wol = rtl8169_set_wol,
  1969. .get_strings = rtl8169_get_strings,
  1970. .get_sset_count = rtl8169_get_sset_count,
  1971. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1972. .get_ts_info = ethtool_op_get_ts_info,
  1973. };
  1974. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1975. struct net_device *dev, u8 default_version)
  1976. {
  1977. void __iomem *ioaddr = tp->mmio_addr;
  1978. /*
  1979. * The driver currently handles the 8168Bf and the 8168Be identically
  1980. * but they can be identified more specifically through the test below
  1981. * if needed:
  1982. *
  1983. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1984. *
  1985. * Same thing for the 8101Eb and the 8101Ec:
  1986. *
  1987. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1988. */
  1989. static const struct rtl_mac_info {
  1990. u32 mask;
  1991. u32 val;
  1992. int mac_version;
  1993. } mac_info[] = {
  1994. /* 8168EP family. */
  1995. { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
  1996. { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
  1997. { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
  1998. /* 8168H family. */
  1999. { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
  2000. { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
  2001. /* 8168G family. */
  2002. { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
  2003. { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
  2004. { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
  2005. { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
  2006. /* 8168F family. */
  2007. { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
  2008. { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
  2009. { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
  2010. /* 8168E family. */
  2011. { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
  2012. { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
  2013. { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
  2014. { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
  2015. /* 8168D family. */
  2016. { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
  2017. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  2018. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  2019. /* 8168DP family. */
  2020. { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  2021. { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
  2022. { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
  2023. /* 8168C family. */
  2024. { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
  2025. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  2026. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  2027. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  2028. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  2029. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  2030. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  2031. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  2032. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  2033. /* 8168B family. */
  2034. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  2035. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  2036. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  2037. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  2038. /* 8101 family. */
  2039. { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
  2040. { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
  2041. { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
  2042. { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
  2043. { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
  2044. { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
  2045. { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
  2046. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  2047. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  2048. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  2049. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  2050. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  2051. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  2052. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  2053. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  2054. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  2055. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  2056. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  2057. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  2058. /* FIXME: where did these entries come from ? -- FR */
  2059. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  2060. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  2061. /* 8110 family. */
  2062. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  2063. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  2064. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  2065. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  2066. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  2067. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  2068. /* Catch-all */
  2069. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  2070. };
  2071. const struct rtl_mac_info *p = mac_info;
  2072. u32 reg;
  2073. reg = RTL_R32(TxConfig);
  2074. while ((reg & p->mask) != p->val)
  2075. p++;
  2076. tp->mac_version = p->mac_version;
  2077. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  2078. netif_notice(tp, probe, dev,
  2079. "unknown MAC, using family default\n");
  2080. tp->mac_version = default_version;
  2081. } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
  2082. tp->mac_version = tp->mii.supports_gmii ?
  2083. RTL_GIGA_MAC_VER_42 :
  2084. RTL_GIGA_MAC_VER_43;
  2085. } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
  2086. tp->mac_version = tp->mii.supports_gmii ?
  2087. RTL_GIGA_MAC_VER_45 :
  2088. RTL_GIGA_MAC_VER_47;
  2089. } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
  2090. tp->mac_version = tp->mii.supports_gmii ?
  2091. RTL_GIGA_MAC_VER_46 :
  2092. RTL_GIGA_MAC_VER_48;
  2093. }
  2094. }
  2095. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  2096. {
  2097. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  2098. }
  2099. struct phy_reg {
  2100. u16 reg;
  2101. u16 val;
  2102. };
  2103. static void rtl_writephy_batch(struct rtl8169_private *tp,
  2104. const struct phy_reg *regs, int len)
  2105. {
  2106. while (len-- > 0) {
  2107. rtl_writephy(tp, regs->reg, regs->val);
  2108. regs++;
  2109. }
  2110. }
  2111. #define PHY_READ 0x00000000
  2112. #define PHY_DATA_OR 0x10000000
  2113. #define PHY_DATA_AND 0x20000000
  2114. #define PHY_BJMPN 0x30000000
  2115. #define PHY_MDIO_CHG 0x40000000
  2116. #define PHY_CLEAR_READCOUNT 0x70000000
  2117. #define PHY_WRITE 0x80000000
  2118. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  2119. #define PHY_COMP_EQ_SKIPN 0xa0000000
  2120. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  2121. #define PHY_WRITE_PREVIOUS 0xc0000000
  2122. #define PHY_SKIPN 0xd0000000
  2123. #define PHY_DELAY_MS 0xe0000000
  2124. struct fw_info {
  2125. u32 magic;
  2126. char version[RTL_VER_SIZE];
  2127. __le32 fw_start;
  2128. __le32 fw_len;
  2129. u8 chksum;
  2130. } __packed;
  2131. #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
  2132. static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  2133. {
  2134. const struct firmware *fw = rtl_fw->fw;
  2135. struct fw_info *fw_info = (struct fw_info *)fw->data;
  2136. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  2137. char *version = rtl_fw->version;
  2138. bool rc = false;
  2139. if (fw->size < FW_OPCODE_SIZE)
  2140. goto out;
  2141. if (!fw_info->magic) {
  2142. size_t i, size, start;
  2143. u8 checksum = 0;
  2144. if (fw->size < sizeof(*fw_info))
  2145. goto out;
  2146. for (i = 0; i < fw->size; i++)
  2147. checksum += fw->data[i];
  2148. if (checksum != 0)
  2149. goto out;
  2150. start = le32_to_cpu(fw_info->fw_start);
  2151. if (start > fw->size)
  2152. goto out;
  2153. size = le32_to_cpu(fw_info->fw_len);
  2154. if (size > (fw->size - start) / FW_OPCODE_SIZE)
  2155. goto out;
  2156. memcpy(version, fw_info->version, RTL_VER_SIZE);
  2157. pa->code = (__le32 *)(fw->data + start);
  2158. pa->size = size;
  2159. } else {
  2160. if (fw->size % FW_OPCODE_SIZE)
  2161. goto out;
  2162. strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
  2163. pa->code = (__le32 *)fw->data;
  2164. pa->size = fw->size / FW_OPCODE_SIZE;
  2165. }
  2166. version[RTL_VER_SIZE - 1] = 0;
  2167. rc = true;
  2168. out:
  2169. return rc;
  2170. }
  2171. static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
  2172. struct rtl_fw_phy_action *pa)
  2173. {
  2174. bool rc = false;
  2175. size_t index;
  2176. for (index = 0; index < pa->size; index++) {
  2177. u32 action = le32_to_cpu(pa->code[index]);
  2178. u32 regno = (action & 0x0fff0000) >> 16;
  2179. switch(action & 0xf0000000) {
  2180. case PHY_READ:
  2181. case PHY_DATA_OR:
  2182. case PHY_DATA_AND:
  2183. case PHY_MDIO_CHG:
  2184. case PHY_CLEAR_READCOUNT:
  2185. case PHY_WRITE:
  2186. case PHY_WRITE_PREVIOUS:
  2187. case PHY_DELAY_MS:
  2188. break;
  2189. case PHY_BJMPN:
  2190. if (regno > index) {
  2191. netif_err(tp, ifup, tp->dev,
  2192. "Out of range of firmware\n");
  2193. goto out;
  2194. }
  2195. break;
  2196. case PHY_READCOUNT_EQ_SKIP:
  2197. if (index + 2 >= pa->size) {
  2198. netif_err(tp, ifup, tp->dev,
  2199. "Out of range of firmware\n");
  2200. goto out;
  2201. }
  2202. break;
  2203. case PHY_COMP_EQ_SKIPN:
  2204. case PHY_COMP_NEQ_SKIPN:
  2205. case PHY_SKIPN:
  2206. if (index + 1 + regno >= pa->size) {
  2207. netif_err(tp, ifup, tp->dev,
  2208. "Out of range of firmware\n");
  2209. goto out;
  2210. }
  2211. break;
  2212. default:
  2213. netif_err(tp, ifup, tp->dev,
  2214. "Invalid action 0x%08x\n", action);
  2215. goto out;
  2216. }
  2217. }
  2218. rc = true;
  2219. out:
  2220. return rc;
  2221. }
  2222. static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  2223. {
  2224. struct net_device *dev = tp->dev;
  2225. int rc = -EINVAL;
  2226. if (!rtl_fw_format_ok(tp, rtl_fw)) {
  2227. netif_err(tp, ifup, dev, "invalid firmware\n");
  2228. goto out;
  2229. }
  2230. if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
  2231. rc = 0;
  2232. out:
  2233. return rc;
  2234. }
  2235. static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  2236. {
  2237. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  2238. struct mdio_ops org, *ops = &tp->mdio_ops;
  2239. u32 predata, count;
  2240. size_t index;
  2241. predata = count = 0;
  2242. org.write = ops->write;
  2243. org.read = ops->read;
  2244. for (index = 0; index < pa->size; ) {
  2245. u32 action = le32_to_cpu(pa->code[index]);
  2246. u32 data = action & 0x0000ffff;
  2247. u32 regno = (action & 0x0fff0000) >> 16;
  2248. if (!action)
  2249. break;
  2250. switch(action & 0xf0000000) {
  2251. case PHY_READ:
  2252. predata = rtl_readphy(tp, regno);
  2253. count++;
  2254. index++;
  2255. break;
  2256. case PHY_DATA_OR:
  2257. predata |= data;
  2258. index++;
  2259. break;
  2260. case PHY_DATA_AND:
  2261. predata &= data;
  2262. index++;
  2263. break;
  2264. case PHY_BJMPN:
  2265. index -= regno;
  2266. break;
  2267. case PHY_MDIO_CHG:
  2268. if (data == 0) {
  2269. ops->write = org.write;
  2270. ops->read = org.read;
  2271. } else if (data == 1) {
  2272. ops->write = mac_mcu_write;
  2273. ops->read = mac_mcu_read;
  2274. }
  2275. index++;
  2276. break;
  2277. case PHY_CLEAR_READCOUNT:
  2278. count = 0;
  2279. index++;
  2280. break;
  2281. case PHY_WRITE:
  2282. rtl_writephy(tp, regno, data);
  2283. index++;
  2284. break;
  2285. case PHY_READCOUNT_EQ_SKIP:
  2286. index += (count == data) ? 2 : 1;
  2287. break;
  2288. case PHY_COMP_EQ_SKIPN:
  2289. if (predata == data)
  2290. index += regno;
  2291. index++;
  2292. break;
  2293. case PHY_COMP_NEQ_SKIPN:
  2294. if (predata != data)
  2295. index += regno;
  2296. index++;
  2297. break;
  2298. case PHY_WRITE_PREVIOUS:
  2299. rtl_writephy(tp, regno, predata);
  2300. index++;
  2301. break;
  2302. case PHY_SKIPN:
  2303. index += regno + 1;
  2304. break;
  2305. case PHY_DELAY_MS:
  2306. mdelay(data);
  2307. index++;
  2308. break;
  2309. default:
  2310. BUG();
  2311. }
  2312. }
  2313. ops->write = org.write;
  2314. ops->read = org.read;
  2315. }
  2316. static void rtl_release_firmware(struct rtl8169_private *tp)
  2317. {
  2318. if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
  2319. release_firmware(tp->rtl_fw->fw);
  2320. kfree(tp->rtl_fw);
  2321. }
  2322. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  2323. }
  2324. static void rtl_apply_firmware(struct rtl8169_private *tp)
  2325. {
  2326. struct rtl_fw *rtl_fw = tp->rtl_fw;
  2327. /* TODO: release firmware once rtl_phy_write_fw signals failures. */
  2328. if (!IS_ERR_OR_NULL(rtl_fw))
  2329. rtl_phy_write_fw(tp, rtl_fw);
  2330. }
  2331. static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
  2332. {
  2333. if (rtl_readphy(tp, reg) != val)
  2334. netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
  2335. else
  2336. rtl_apply_firmware(tp);
  2337. }
  2338. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  2339. {
  2340. static const struct phy_reg phy_reg_init[] = {
  2341. { 0x1f, 0x0001 },
  2342. { 0x06, 0x006e },
  2343. { 0x08, 0x0708 },
  2344. { 0x15, 0x4000 },
  2345. { 0x18, 0x65c7 },
  2346. { 0x1f, 0x0001 },
  2347. { 0x03, 0x00a1 },
  2348. { 0x02, 0x0008 },
  2349. { 0x01, 0x0120 },
  2350. { 0x00, 0x1000 },
  2351. { 0x04, 0x0800 },
  2352. { 0x04, 0x0000 },
  2353. { 0x03, 0xff41 },
  2354. { 0x02, 0xdf60 },
  2355. { 0x01, 0x0140 },
  2356. { 0x00, 0x0077 },
  2357. { 0x04, 0x7800 },
  2358. { 0x04, 0x7000 },
  2359. { 0x03, 0x802f },
  2360. { 0x02, 0x4f02 },
  2361. { 0x01, 0x0409 },
  2362. { 0x00, 0xf0f9 },
  2363. { 0x04, 0x9800 },
  2364. { 0x04, 0x9000 },
  2365. { 0x03, 0xdf01 },
  2366. { 0x02, 0xdf20 },
  2367. { 0x01, 0xff95 },
  2368. { 0x00, 0xba00 },
  2369. { 0x04, 0xa800 },
  2370. { 0x04, 0xa000 },
  2371. { 0x03, 0xff41 },
  2372. { 0x02, 0xdf20 },
  2373. { 0x01, 0x0140 },
  2374. { 0x00, 0x00bb },
  2375. { 0x04, 0xb800 },
  2376. { 0x04, 0xb000 },
  2377. { 0x03, 0xdf41 },
  2378. { 0x02, 0xdc60 },
  2379. { 0x01, 0x6340 },
  2380. { 0x00, 0x007d },
  2381. { 0x04, 0xd800 },
  2382. { 0x04, 0xd000 },
  2383. { 0x03, 0xdf01 },
  2384. { 0x02, 0xdf20 },
  2385. { 0x01, 0x100a },
  2386. { 0x00, 0xa0ff },
  2387. { 0x04, 0xf800 },
  2388. { 0x04, 0xf000 },
  2389. { 0x1f, 0x0000 },
  2390. { 0x0b, 0x0000 },
  2391. { 0x00, 0x9200 }
  2392. };
  2393. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2394. }
  2395. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  2396. {
  2397. static const struct phy_reg phy_reg_init[] = {
  2398. { 0x1f, 0x0002 },
  2399. { 0x01, 0x90d0 },
  2400. { 0x1f, 0x0000 }
  2401. };
  2402. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2403. }
  2404. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  2405. {
  2406. struct pci_dev *pdev = tp->pci_dev;
  2407. if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
  2408. (pdev->subsystem_device != 0xe000))
  2409. return;
  2410. rtl_writephy(tp, 0x1f, 0x0001);
  2411. rtl_writephy(tp, 0x10, 0xf01b);
  2412. rtl_writephy(tp, 0x1f, 0x0000);
  2413. }
  2414. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  2415. {
  2416. static const struct phy_reg phy_reg_init[] = {
  2417. { 0x1f, 0x0001 },
  2418. { 0x04, 0x0000 },
  2419. { 0x03, 0x00a1 },
  2420. { 0x02, 0x0008 },
  2421. { 0x01, 0x0120 },
  2422. { 0x00, 0x1000 },
  2423. { 0x04, 0x0800 },
  2424. { 0x04, 0x9000 },
  2425. { 0x03, 0x802f },
  2426. { 0x02, 0x4f02 },
  2427. { 0x01, 0x0409 },
  2428. { 0x00, 0xf099 },
  2429. { 0x04, 0x9800 },
  2430. { 0x04, 0xa000 },
  2431. { 0x03, 0xdf01 },
  2432. { 0x02, 0xdf20 },
  2433. { 0x01, 0xff95 },
  2434. { 0x00, 0xba00 },
  2435. { 0x04, 0xa800 },
  2436. { 0x04, 0xf000 },
  2437. { 0x03, 0xdf01 },
  2438. { 0x02, 0xdf20 },
  2439. { 0x01, 0x101a },
  2440. { 0x00, 0xa0ff },
  2441. { 0x04, 0xf800 },
  2442. { 0x04, 0x0000 },
  2443. { 0x1f, 0x0000 },
  2444. { 0x1f, 0x0001 },
  2445. { 0x10, 0xf41b },
  2446. { 0x14, 0xfb54 },
  2447. { 0x18, 0xf5c7 },
  2448. { 0x1f, 0x0000 },
  2449. { 0x1f, 0x0001 },
  2450. { 0x17, 0x0cc0 },
  2451. { 0x1f, 0x0000 }
  2452. };
  2453. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2454. rtl8169scd_hw_phy_config_quirk(tp);
  2455. }
  2456. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  2457. {
  2458. static const struct phy_reg phy_reg_init[] = {
  2459. { 0x1f, 0x0001 },
  2460. { 0x04, 0x0000 },
  2461. { 0x03, 0x00a1 },
  2462. { 0x02, 0x0008 },
  2463. { 0x01, 0x0120 },
  2464. { 0x00, 0x1000 },
  2465. { 0x04, 0x0800 },
  2466. { 0x04, 0x9000 },
  2467. { 0x03, 0x802f },
  2468. { 0x02, 0x4f02 },
  2469. { 0x01, 0x0409 },
  2470. { 0x00, 0xf099 },
  2471. { 0x04, 0x9800 },
  2472. { 0x04, 0xa000 },
  2473. { 0x03, 0xdf01 },
  2474. { 0x02, 0xdf20 },
  2475. { 0x01, 0xff95 },
  2476. { 0x00, 0xba00 },
  2477. { 0x04, 0xa800 },
  2478. { 0x04, 0xf000 },
  2479. { 0x03, 0xdf01 },
  2480. { 0x02, 0xdf20 },
  2481. { 0x01, 0x101a },
  2482. { 0x00, 0xa0ff },
  2483. { 0x04, 0xf800 },
  2484. { 0x04, 0x0000 },
  2485. { 0x1f, 0x0000 },
  2486. { 0x1f, 0x0001 },
  2487. { 0x0b, 0x8480 },
  2488. { 0x1f, 0x0000 },
  2489. { 0x1f, 0x0001 },
  2490. { 0x18, 0x67c7 },
  2491. { 0x04, 0x2000 },
  2492. { 0x03, 0x002f },
  2493. { 0x02, 0x4360 },
  2494. { 0x01, 0x0109 },
  2495. { 0x00, 0x3022 },
  2496. { 0x04, 0x2800 },
  2497. { 0x1f, 0x0000 },
  2498. { 0x1f, 0x0001 },
  2499. { 0x17, 0x0cc0 },
  2500. { 0x1f, 0x0000 }
  2501. };
  2502. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2503. }
  2504. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  2505. {
  2506. static const struct phy_reg phy_reg_init[] = {
  2507. { 0x10, 0xf41b },
  2508. { 0x1f, 0x0000 }
  2509. };
  2510. rtl_writephy(tp, 0x1f, 0x0001);
  2511. rtl_patchphy(tp, 0x16, 1 << 0);
  2512. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2513. }
  2514. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  2515. {
  2516. static const struct phy_reg phy_reg_init[] = {
  2517. { 0x1f, 0x0001 },
  2518. { 0x10, 0xf41b },
  2519. { 0x1f, 0x0000 }
  2520. };
  2521. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2522. }
  2523. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  2524. {
  2525. static const struct phy_reg phy_reg_init[] = {
  2526. { 0x1f, 0x0000 },
  2527. { 0x1d, 0x0f00 },
  2528. { 0x1f, 0x0002 },
  2529. { 0x0c, 0x1ec8 },
  2530. { 0x1f, 0x0000 }
  2531. };
  2532. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2533. }
  2534. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  2535. {
  2536. static const struct phy_reg phy_reg_init[] = {
  2537. { 0x1f, 0x0001 },
  2538. { 0x1d, 0x3d98 },
  2539. { 0x1f, 0x0000 }
  2540. };
  2541. rtl_writephy(tp, 0x1f, 0x0000);
  2542. rtl_patchphy(tp, 0x14, 1 << 5);
  2543. rtl_patchphy(tp, 0x0d, 1 << 5);
  2544. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2545. }
  2546. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  2547. {
  2548. static const struct phy_reg phy_reg_init[] = {
  2549. { 0x1f, 0x0001 },
  2550. { 0x12, 0x2300 },
  2551. { 0x1f, 0x0002 },
  2552. { 0x00, 0x88d4 },
  2553. { 0x01, 0x82b1 },
  2554. { 0x03, 0x7002 },
  2555. { 0x08, 0x9e30 },
  2556. { 0x09, 0x01f0 },
  2557. { 0x0a, 0x5500 },
  2558. { 0x0c, 0x00c8 },
  2559. { 0x1f, 0x0003 },
  2560. { 0x12, 0xc096 },
  2561. { 0x16, 0x000a },
  2562. { 0x1f, 0x0000 },
  2563. { 0x1f, 0x0000 },
  2564. { 0x09, 0x2000 },
  2565. { 0x09, 0x0000 }
  2566. };
  2567. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2568. rtl_patchphy(tp, 0x14, 1 << 5);
  2569. rtl_patchphy(tp, 0x0d, 1 << 5);
  2570. rtl_writephy(tp, 0x1f, 0x0000);
  2571. }
  2572. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  2573. {
  2574. static const struct phy_reg phy_reg_init[] = {
  2575. { 0x1f, 0x0001 },
  2576. { 0x12, 0x2300 },
  2577. { 0x03, 0x802f },
  2578. { 0x02, 0x4f02 },
  2579. { 0x01, 0x0409 },
  2580. { 0x00, 0xf099 },
  2581. { 0x04, 0x9800 },
  2582. { 0x04, 0x9000 },
  2583. { 0x1d, 0x3d98 },
  2584. { 0x1f, 0x0002 },
  2585. { 0x0c, 0x7eb8 },
  2586. { 0x06, 0x0761 },
  2587. { 0x1f, 0x0003 },
  2588. { 0x16, 0x0f0a },
  2589. { 0x1f, 0x0000 }
  2590. };
  2591. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2592. rtl_patchphy(tp, 0x16, 1 << 0);
  2593. rtl_patchphy(tp, 0x14, 1 << 5);
  2594. rtl_patchphy(tp, 0x0d, 1 << 5);
  2595. rtl_writephy(tp, 0x1f, 0x0000);
  2596. }
  2597. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  2598. {
  2599. static const struct phy_reg phy_reg_init[] = {
  2600. { 0x1f, 0x0001 },
  2601. { 0x12, 0x2300 },
  2602. { 0x1d, 0x3d98 },
  2603. { 0x1f, 0x0002 },
  2604. { 0x0c, 0x7eb8 },
  2605. { 0x06, 0x5461 },
  2606. { 0x1f, 0x0003 },
  2607. { 0x16, 0x0f0a },
  2608. { 0x1f, 0x0000 }
  2609. };
  2610. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2611. rtl_patchphy(tp, 0x16, 1 << 0);
  2612. rtl_patchphy(tp, 0x14, 1 << 5);
  2613. rtl_patchphy(tp, 0x0d, 1 << 5);
  2614. rtl_writephy(tp, 0x1f, 0x0000);
  2615. }
  2616. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  2617. {
  2618. rtl8168c_3_hw_phy_config(tp);
  2619. }
  2620. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  2621. {
  2622. static const struct phy_reg phy_reg_init_0[] = {
  2623. /* Channel Estimation */
  2624. { 0x1f, 0x0001 },
  2625. { 0x06, 0x4064 },
  2626. { 0x07, 0x2863 },
  2627. { 0x08, 0x059c },
  2628. { 0x09, 0x26b4 },
  2629. { 0x0a, 0x6a19 },
  2630. { 0x0b, 0xdcc8 },
  2631. { 0x10, 0xf06d },
  2632. { 0x14, 0x7f68 },
  2633. { 0x18, 0x7fd9 },
  2634. { 0x1c, 0xf0ff },
  2635. { 0x1d, 0x3d9c },
  2636. { 0x1f, 0x0003 },
  2637. { 0x12, 0xf49f },
  2638. { 0x13, 0x070b },
  2639. { 0x1a, 0x05ad },
  2640. { 0x14, 0x94c0 },
  2641. /*
  2642. * Tx Error Issue
  2643. * Enhance line driver power
  2644. */
  2645. { 0x1f, 0x0002 },
  2646. { 0x06, 0x5561 },
  2647. { 0x1f, 0x0005 },
  2648. { 0x05, 0x8332 },
  2649. { 0x06, 0x5561 },
  2650. /*
  2651. * Can not link to 1Gbps with bad cable
  2652. * Decrease SNR threshold form 21.07dB to 19.04dB
  2653. */
  2654. { 0x1f, 0x0001 },
  2655. { 0x17, 0x0cc0 },
  2656. { 0x1f, 0x0000 },
  2657. { 0x0d, 0xf880 }
  2658. };
  2659. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2660. /*
  2661. * Rx Error Issue
  2662. * Fine Tune Switching regulator parameter
  2663. */
  2664. rtl_writephy(tp, 0x1f, 0x0002);
  2665. rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
  2666. rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
  2667. if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
  2668. static const struct phy_reg phy_reg_init[] = {
  2669. { 0x1f, 0x0002 },
  2670. { 0x05, 0x669a },
  2671. { 0x1f, 0x0005 },
  2672. { 0x05, 0x8330 },
  2673. { 0x06, 0x669a },
  2674. { 0x1f, 0x0002 }
  2675. };
  2676. int val;
  2677. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2678. val = rtl_readphy(tp, 0x0d);
  2679. if ((val & 0x00ff) != 0x006c) {
  2680. static const u32 set[] = {
  2681. 0x0065, 0x0066, 0x0067, 0x0068,
  2682. 0x0069, 0x006a, 0x006b, 0x006c
  2683. };
  2684. int i;
  2685. rtl_writephy(tp, 0x1f, 0x0002);
  2686. val &= 0xff00;
  2687. for (i = 0; i < ARRAY_SIZE(set); i++)
  2688. rtl_writephy(tp, 0x0d, val | set[i]);
  2689. }
  2690. } else {
  2691. static const struct phy_reg phy_reg_init[] = {
  2692. { 0x1f, 0x0002 },
  2693. { 0x05, 0x6662 },
  2694. { 0x1f, 0x0005 },
  2695. { 0x05, 0x8330 },
  2696. { 0x06, 0x6662 }
  2697. };
  2698. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2699. }
  2700. /* RSET couple improve */
  2701. rtl_writephy(tp, 0x1f, 0x0002);
  2702. rtl_patchphy(tp, 0x0d, 0x0300);
  2703. rtl_patchphy(tp, 0x0f, 0x0010);
  2704. /* Fine tune PLL performance */
  2705. rtl_writephy(tp, 0x1f, 0x0002);
  2706. rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
  2707. rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
  2708. rtl_writephy(tp, 0x1f, 0x0005);
  2709. rtl_writephy(tp, 0x05, 0x001b);
  2710. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
  2711. rtl_writephy(tp, 0x1f, 0x0000);
  2712. }
  2713. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  2714. {
  2715. static const struct phy_reg phy_reg_init_0[] = {
  2716. /* Channel Estimation */
  2717. { 0x1f, 0x0001 },
  2718. { 0x06, 0x4064 },
  2719. { 0x07, 0x2863 },
  2720. { 0x08, 0x059c },
  2721. { 0x09, 0x26b4 },
  2722. { 0x0a, 0x6a19 },
  2723. { 0x0b, 0xdcc8 },
  2724. { 0x10, 0xf06d },
  2725. { 0x14, 0x7f68 },
  2726. { 0x18, 0x7fd9 },
  2727. { 0x1c, 0xf0ff },
  2728. { 0x1d, 0x3d9c },
  2729. { 0x1f, 0x0003 },
  2730. { 0x12, 0xf49f },
  2731. { 0x13, 0x070b },
  2732. { 0x1a, 0x05ad },
  2733. { 0x14, 0x94c0 },
  2734. /*
  2735. * Tx Error Issue
  2736. * Enhance line driver power
  2737. */
  2738. { 0x1f, 0x0002 },
  2739. { 0x06, 0x5561 },
  2740. { 0x1f, 0x0005 },
  2741. { 0x05, 0x8332 },
  2742. { 0x06, 0x5561 },
  2743. /*
  2744. * Can not link to 1Gbps with bad cable
  2745. * Decrease SNR threshold form 21.07dB to 19.04dB
  2746. */
  2747. { 0x1f, 0x0001 },
  2748. { 0x17, 0x0cc0 },
  2749. { 0x1f, 0x0000 },
  2750. { 0x0d, 0xf880 }
  2751. };
  2752. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2753. if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
  2754. static const struct phy_reg phy_reg_init[] = {
  2755. { 0x1f, 0x0002 },
  2756. { 0x05, 0x669a },
  2757. { 0x1f, 0x0005 },
  2758. { 0x05, 0x8330 },
  2759. { 0x06, 0x669a },
  2760. { 0x1f, 0x0002 }
  2761. };
  2762. int val;
  2763. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2764. val = rtl_readphy(tp, 0x0d);
  2765. if ((val & 0x00ff) != 0x006c) {
  2766. static const u32 set[] = {
  2767. 0x0065, 0x0066, 0x0067, 0x0068,
  2768. 0x0069, 0x006a, 0x006b, 0x006c
  2769. };
  2770. int i;
  2771. rtl_writephy(tp, 0x1f, 0x0002);
  2772. val &= 0xff00;
  2773. for (i = 0; i < ARRAY_SIZE(set); i++)
  2774. rtl_writephy(tp, 0x0d, val | set[i]);
  2775. }
  2776. } else {
  2777. static const struct phy_reg phy_reg_init[] = {
  2778. { 0x1f, 0x0002 },
  2779. { 0x05, 0x2642 },
  2780. { 0x1f, 0x0005 },
  2781. { 0x05, 0x8330 },
  2782. { 0x06, 0x2642 }
  2783. };
  2784. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2785. }
  2786. /* Fine tune PLL performance */
  2787. rtl_writephy(tp, 0x1f, 0x0002);
  2788. rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
  2789. rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
  2790. /* Switching regulator Slew rate */
  2791. rtl_writephy(tp, 0x1f, 0x0002);
  2792. rtl_patchphy(tp, 0x0f, 0x0017);
  2793. rtl_writephy(tp, 0x1f, 0x0005);
  2794. rtl_writephy(tp, 0x05, 0x001b);
  2795. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
  2796. rtl_writephy(tp, 0x1f, 0x0000);
  2797. }
  2798. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  2799. {
  2800. static const struct phy_reg phy_reg_init[] = {
  2801. { 0x1f, 0x0002 },
  2802. { 0x10, 0x0008 },
  2803. { 0x0d, 0x006c },
  2804. { 0x1f, 0x0000 },
  2805. { 0x0d, 0xf880 },
  2806. { 0x1f, 0x0001 },
  2807. { 0x17, 0x0cc0 },
  2808. { 0x1f, 0x0001 },
  2809. { 0x0b, 0xa4d8 },
  2810. { 0x09, 0x281c },
  2811. { 0x07, 0x2883 },
  2812. { 0x0a, 0x6b35 },
  2813. { 0x1d, 0x3da4 },
  2814. { 0x1c, 0xeffd },
  2815. { 0x14, 0x7f52 },
  2816. { 0x18, 0x7fc6 },
  2817. { 0x08, 0x0601 },
  2818. { 0x06, 0x4063 },
  2819. { 0x10, 0xf074 },
  2820. { 0x1f, 0x0003 },
  2821. { 0x13, 0x0789 },
  2822. { 0x12, 0xf4bd },
  2823. { 0x1a, 0x04fd },
  2824. { 0x14, 0x84b0 },
  2825. { 0x1f, 0x0000 },
  2826. { 0x00, 0x9200 },
  2827. { 0x1f, 0x0005 },
  2828. { 0x01, 0x0340 },
  2829. { 0x1f, 0x0001 },
  2830. { 0x04, 0x4000 },
  2831. { 0x03, 0x1d21 },
  2832. { 0x02, 0x0c32 },
  2833. { 0x01, 0x0200 },
  2834. { 0x00, 0x5554 },
  2835. { 0x04, 0x4800 },
  2836. { 0x04, 0x4000 },
  2837. { 0x04, 0xf000 },
  2838. { 0x03, 0xdf01 },
  2839. { 0x02, 0xdf20 },
  2840. { 0x01, 0x101a },
  2841. { 0x00, 0xa0ff },
  2842. { 0x04, 0xf800 },
  2843. { 0x04, 0xf000 },
  2844. { 0x1f, 0x0000 },
  2845. { 0x1f, 0x0007 },
  2846. { 0x1e, 0x0023 },
  2847. { 0x16, 0x0000 },
  2848. { 0x1f, 0x0000 }
  2849. };
  2850. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2851. }
  2852. static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
  2853. {
  2854. static const struct phy_reg phy_reg_init[] = {
  2855. { 0x1f, 0x0001 },
  2856. { 0x17, 0x0cc0 },
  2857. { 0x1f, 0x0007 },
  2858. { 0x1e, 0x002d },
  2859. { 0x18, 0x0040 },
  2860. { 0x1f, 0x0000 }
  2861. };
  2862. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2863. rtl_patchphy(tp, 0x0d, 1 << 5);
  2864. }
  2865. static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
  2866. {
  2867. static const struct phy_reg phy_reg_init[] = {
  2868. /* Enable Delay cap */
  2869. { 0x1f, 0x0005 },
  2870. { 0x05, 0x8b80 },
  2871. { 0x06, 0xc896 },
  2872. { 0x1f, 0x0000 },
  2873. /* Channel estimation fine tune */
  2874. { 0x1f, 0x0001 },
  2875. { 0x0b, 0x6c20 },
  2876. { 0x07, 0x2872 },
  2877. { 0x1c, 0xefff },
  2878. { 0x1f, 0x0003 },
  2879. { 0x14, 0x6420 },
  2880. { 0x1f, 0x0000 },
  2881. /* Update PFM & 10M TX idle timer */
  2882. { 0x1f, 0x0007 },
  2883. { 0x1e, 0x002f },
  2884. { 0x15, 0x1919 },
  2885. { 0x1f, 0x0000 },
  2886. { 0x1f, 0x0007 },
  2887. { 0x1e, 0x00ac },
  2888. { 0x18, 0x0006 },
  2889. { 0x1f, 0x0000 }
  2890. };
  2891. rtl_apply_firmware(tp);
  2892. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2893. /* DCO enable for 10M IDLE Power */
  2894. rtl_writephy(tp, 0x1f, 0x0007);
  2895. rtl_writephy(tp, 0x1e, 0x0023);
  2896. rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
  2897. rtl_writephy(tp, 0x1f, 0x0000);
  2898. /* For impedance matching */
  2899. rtl_writephy(tp, 0x1f, 0x0002);
  2900. rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
  2901. rtl_writephy(tp, 0x1f, 0x0000);
  2902. /* PHY auto speed down */
  2903. rtl_writephy(tp, 0x1f, 0x0007);
  2904. rtl_writephy(tp, 0x1e, 0x002d);
  2905. rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
  2906. rtl_writephy(tp, 0x1f, 0x0000);
  2907. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2908. rtl_writephy(tp, 0x1f, 0x0005);
  2909. rtl_writephy(tp, 0x05, 0x8b86);
  2910. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2911. rtl_writephy(tp, 0x1f, 0x0000);
  2912. rtl_writephy(tp, 0x1f, 0x0005);
  2913. rtl_writephy(tp, 0x05, 0x8b85);
  2914. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  2915. rtl_writephy(tp, 0x1f, 0x0007);
  2916. rtl_writephy(tp, 0x1e, 0x0020);
  2917. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
  2918. rtl_writephy(tp, 0x1f, 0x0006);
  2919. rtl_writephy(tp, 0x00, 0x5a00);
  2920. rtl_writephy(tp, 0x1f, 0x0000);
  2921. rtl_writephy(tp, 0x0d, 0x0007);
  2922. rtl_writephy(tp, 0x0e, 0x003c);
  2923. rtl_writephy(tp, 0x0d, 0x4007);
  2924. rtl_writephy(tp, 0x0e, 0x0000);
  2925. rtl_writephy(tp, 0x0d, 0x0000);
  2926. }
  2927. static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
  2928. {
  2929. const u16 w[] = {
  2930. addr[0] | (addr[1] << 8),
  2931. addr[2] | (addr[3] << 8),
  2932. addr[4] | (addr[5] << 8)
  2933. };
  2934. const struct exgmac_reg e[] = {
  2935. { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
  2936. { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
  2937. { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
  2938. { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
  2939. };
  2940. rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
  2941. }
  2942. static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
  2943. {
  2944. static const struct phy_reg phy_reg_init[] = {
  2945. /* Enable Delay cap */
  2946. { 0x1f, 0x0004 },
  2947. { 0x1f, 0x0007 },
  2948. { 0x1e, 0x00ac },
  2949. { 0x18, 0x0006 },
  2950. { 0x1f, 0x0002 },
  2951. { 0x1f, 0x0000 },
  2952. { 0x1f, 0x0000 },
  2953. /* Channel estimation fine tune */
  2954. { 0x1f, 0x0003 },
  2955. { 0x09, 0xa20f },
  2956. { 0x1f, 0x0000 },
  2957. { 0x1f, 0x0000 },
  2958. /* Green Setting */
  2959. { 0x1f, 0x0005 },
  2960. { 0x05, 0x8b5b },
  2961. { 0x06, 0x9222 },
  2962. { 0x05, 0x8b6d },
  2963. { 0x06, 0x8000 },
  2964. { 0x05, 0x8b76 },
  2965. { 0x06, 0x8000 },
  2966. { 0x1f, 0x0000 }
  2967. };
  2968. rtl_apply_firmware(tp);
  2969. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2970. /* For 4-corner performance improve */
  2971. rtl_writephy(tp, 0x1f, 0x0005);
  2972. rtl_writephy(tp, 0x05, 0x8b80);
  2973. rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
  2974. rtl_writephy(tp, 0x1f, 0x0000);
  2975. /* PHY auto speed down */
  2976. rtl_writephy(tp, 0x1f, 0x0004);
  2977. rtl_writephy(tp, 0x1f, 0x0007);
  2978. rtl_writephy(tp, 0x1e, 0x002d);
  2979. rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
  2980. rtl_writephy(tp, 0x1f, 0x0002);
  2981. rtl_writephy(tp, 0x1f, 0x0000);
  2982. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2983. /* improve 10M EEE waveform */
  2984. rtl_writephy(tp, 0x1f, 0x0005);
  2985. rtl_writephy(tp, 0x05, 0x8b86);
  2986. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2987. rtl_writephy(tp, 0x1f, 0x0000);
  2988. /* Improve 2-pair detection performance */
  2989. rtl_writephy(tp, 0x1f, 0x0005);
  2990. rtl_writephy(tp, 0x05, 0x8b85);
  2991. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  2992. rtl_writephy(tp, 0x1f, 0x0000);
  2993. /* EEE setting */
  2994. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
  2995. rtl_writephy(tp, 0x1f, 0x0005);
  2996. rtl_writephy(tp, 0x05, 0x8b85);
  2997. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  2998. rtl_writephy(tp, 0x1f, 0x0004);
  2999. rtl_writephy(tp, 0x1f, 0x0007);
  3000. rtl_writephy(tp, 0x1e, 0x0020);
  3001. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
  3002. rtl_writephy(tp, 0x1f, 0x0002);
  3003. rtl_writephy(tp, 0x1f, 0x0000);
  3004. rtl_writephy(tp, 0x0d, 0x0007);
  3005. rtl_writephy(tp, 0x0e, 0x003c);
  3006. rtl_writephy(tp, 0x0d, 0x4007);
  3007. rtl_writephy(tp, 0x0e, 0x0000);
  3008. rtl_writephy(tp, 0x0d, 0x0000);
  3009. /* Green feature */
  3010. rtl_writephy(tp, 0x1f, 0x0003);
  3011. rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
  3012. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
  3013. rtl_writephy(tp, 0x1f, 0x0000);
  3014. /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
  3015. rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
  3016. }
  3017. static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
  3018. {
  3019. /* For 4-corner performance improve */
  3020. rtl_writephy(tp, 0x1f, 0x0005);
  3021. rtl_writephy(tp, 0x05, 0x8b80);
  3022. rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
  3023. rtl_writephy(tp, 0x1f, 0x0000);
  3024. /* PHY auto speed down */
  3025. rtl_writephy(tp, 0x1f, 0x0007);
  3026. rtl_writephy(tp, 0x1e, 0x002d);
  3027. rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
  3028. rtl_writephy(tp, 0x1f, 0x0000);
  3029. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3030. /* Improve 10M EEE waveform */
  3031. rtl_writephy(tp, 0x1f, 0x0005);
  3032. rtl_writephy(tp, 0x05, 0x8b86);
  3033. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  3034. rtl_writephy(tp, 0x1f, 0x0000);
  3035. }
  3036. static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
  3037. {
  3038. static const struct phy_reg phy_reg_init[] = {
  3039. /* Channel estimation fine tune */
  3040. { 0x1f, 0x0003 },
  3041. { 0x09, 0xa20f },
  3042. { 0x1f, 0x0000 },
  3043. /* Modify green table for giga & fnet */
  3044. { 0x1f, 0x0005 },
  3045. { 0x05, 0x8b55 },
  3046. { 0x06, 0x0000 },
  3047. { 0x05, 0x8b5e },
  3048. { 0x06, 0x0000 },
  3049. { 0x05, 0x8b67 },
  3050. { 0x06, 0x0000 },
  3051. { 0x05, 0x8b70 },
  3052. { 0x06, 0x0000 },
  3053. { 0x1f, 0x0000 },
  3054. { 0x1f, 0x0007 },
  3055. { 0x1e, 0x0078 },
  3056. { 0x17, 0x0000 },
  3057. { 0x19, 0x00fb },
  3058. { 0x1f, 0x0000 },
  3059. /* Modify green table for 10M */
  3060. { 0x1f, 0x0005 },
  3061. { 0x05, 0x8b79 },
  3062. { 0x06, 0xaa00 },
  3063. { 0x1f, 0x0000 },
  3064. /* Disable hiimpedance detection (RTCT) */
  3065. { 0x1f, 0x0003 },
  3066. { 0x01, 0x328a },
  3067. { 0x1f, 0x0000 }
  3068. };
  3069. rtl_apply_firmware(tp);
  3070. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3071. rtl8168f_hw_phy_config(tp);
  3072. /* Improve 2-pair detection performance */
  3073. rtl_writephy(tp, 0x1f, 0x0005);
  3074. rtl_writephy(tp, 0x05, 0x8b85);
  3075. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  3076. rtl_writephy(tp, 0x1f, 0x0000);
  3077. }
  3078. static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
  3079. {
  3080. rtl_apply_firmware(tp);
  3081. rtl8168f_hw_phy_config(tp);
  3082. }
  3083. static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
  3084. {
  3085. static const struct phy_reg phy_reg_init[] = {
  3086. /* Channel estimation fine tune */
  3087. { 0x1f, 0x0003 },
  3088. { 0x09, 0xa20f },
  3089. { 0x1f, 0x0000 },
  3090. /* Modify green table for giga & fnet */
  3091. { 0x1f, 0x0005 },
  3092. { 0x05, 0x8b55 },
  3093. { 0x06, 0x0000 },
  3094. { 0x05, 0x8b5e },
  3095. { 0x06, 0x0000 },
  3096. { 0x05, 0x8b67 },
  3097. { 0x06, 0x0000 },
  3098. { 0x05, 0x8b70 },
  3099. { 0x06, 0x0000 },
  3100. { 0x1f, 0x0000 },
  3101. { 0x1f, 0x0007 },
  3102. { 0x1e, 0x0078 },
  3103. { 0x17, 0x0000 },
  3104. { 0x19, 0x00aa },
  3105. { 0x1f, 0x0000 },
  3106. /* Modify green table for 10M */
  3107. { 0x1f, 0x0005 },
  3108. { 0x05, 0x8b79 },
  3109. { 0x06, 0xaa00 },
  3110. { 0x1f, 0x0000 },
  3111. /* Disable hiimpedance detection (RTCT) */
  3112. { 0x1f, 0x0003 },
  3113. { 0x01, 0x328a },
  3114. { 0x1f, 0x0000 }
  3115. };
  3116. rtl_apply_firmware(tp);
  3117. rtl8168f_hw_phy_config(tp);
  3118. /* Improve 2-pair detection performance */
  3119. rtl_writephy(tp, 0x1f, 0x0005);
  3120. rtl_writephy(tp, 0x05, 0x8b85);
  3121. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  3122. rtl_writephy(tp, 0x1f, 0x0000);
  3123. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3124. /* Modify green table for giga */
  3125. rtl_writephy(tp, 0x1f, 0x0005);
  3126. rtl_writephy(tp, 0x05, 0x8b54);
  3127. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
  3128. rtl_writephy(tp, 0x05, 0x8b5d);
  3129. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
  3130. rtl_writephy(tp, 0x05, 0x8a7c);
  3131. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  3132. rtl_writephy(tp, 0x05, 0x8a7f);
  3133. rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
  3134. rtl_writephy(tp, 0x05, 0x8a82);
  3135. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  3136. rtl_writephy(tp, 0x05, 0x8a85);
  3137. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  3138. rtl_writephy(tp, 0x05, 0x8a88);
  3139. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  3140. rtl_writephy(tp, 0x1f, 0x0000);
  3141. /* uc same-seed solution */
  3142. rtl_writephy(tp, 0x1f, 0x0005);
  3143. rtl_writephy(tp, 0x05, 0x8b85);
  3144. rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
  3145. rtl_writephy(tp, 0x1f, 0x0000);
  3146. /* eee setting */
  3147. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
  3148. rtl_writephy(tp, 0x1f, 0x0005);
  3149. rtl_writephy(tp, 0x05, 0x8b85);
  3150. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  3151. rtl_writephy(tp, 0x1f, 0x0004);
  3152. rtl_writephy(tp, 0x1f, 0x0007);
  3153. rtl_writephy(tp, 0x1e, 0x0020);
  3154. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
  3155. rtl_writephy(tp, 0x1f, 0x0000);
  3156. rtl_writephy(tp, 0x0d, 0x0007);
  3157. rtl_writephy(tp, 0x0e, 0x003c);
  3158. rtl_writephy(tp, 0x0d, 0x4007);
  3159. rtl_writephy(tp, 0x0e, 0x0000);
  3160. rtl_writephy(tp, 0x0d, 0x0000);
  3161. /* Green feature */
  3162. rtl_writephy(tp, 0x1f, 0x0003);
  3163. rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
  3164. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
  3165. rtl_writephy(tp, 0x1f, 0x0000);
  3166. }
  3167. static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
  3168. {
  3169. rtl_apply_firmware(tp);
  3170. rtl_writephy(tp, 0x1f, 0x0a46);
  3171. if (rtl_readphy(tp, 0x10) & 0x0100) {
  3172. rtl_writephy(tp, 0x1f, 0x0bcc);
  3173. rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
  3174. } else {
  3175. rtl_writephy(tp, 0x1f, 0x0bcc);
  3176. rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
  3177. }
  3178. rtl_writephy(tp, 0x1f, 0x0a46);
  3179. if (rtl_readphy(tp, 0x13) & 0x0100) {
  3180. rtl_writephy(tp, 0x1f, 0x0c41);
  3181. rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
  3182. } else {
  3183. rtl_writephy(tp, 0x1f, 0x0c41);
  3184. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
  3185. }
  3186. /* Enable PHY auto speed down */
  3187. rtl_writephy(tp, 0x1f, 0x0a44);
  3188. rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
  3189. rtl_writephy(tp, 0x1f, 0x0bcc);
  3190. rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
  3191. rtl_writephy(tp, 0x1f, 0x0a44);
  3192. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3193. rtl_writephy(tp, 0x1f, 0x0a43);
  3194. rtl_writephy(tp, 0x13, 0x8084);
  3195. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3196. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3197. /* EEE auto-fallback function */
  3198. rtl_writephy(tp, 0x1f, 0x0a4b);
  3199. rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
  3200. /* Enable UC LPF tune function */
  3201. rtl_writephy(tp, 0x1f, 0x0a43);
  3202. rtl_writephy(tp, 0x13, 0x8012);
  3203. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3204. rtl_writephy(tp, 0x1f, 0x0c42);
  3205. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3206. /* Improve SWR Efficiency */
  3207. rtl_writephy(tp, 0x1f, 0x0bcd);
  3208. rtl_writephy(tp, 0x14, 0x5065);
  3209. rtl_writephy(tp, 0x14, 0xd065);
  3210. rtl_writephy(tp, 0x1f, 0x0bc8);
  3211. rtl_writephy(tp, 0x11, 0x5655);
  3212. rtl_writephy(tp, 0x1f, 0x0bcd);
  3213. rtl_writephy(tp, 0x14, 0x1065);
  3214. rtl_writephy(tp, 0x14, 0x9065);
  3215. rtl_writephy(tp, 0x14, 0x1065);
  3216. /* Check ALDPS bit, disable it if enabled */
  3217. rtl_writephy(tp, 0x1f, 0x0a43);
  3218. if (rtl_readphy(tp, 0x10) & 0x0004)
  3219. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3220. rtl_writephy(tp, 0x1f, 0x0000);
  3221. }
  3222. static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
  3223. {
  3224. rtl_apply_firmware(tp);
  3225. }
  3226. static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
  3227. {
  3228. u16 dout_tapbin;
  3229. u32 data;
  3230. rtl_apply_firmware(tp);
  3231. /* CHN EST parameters adjust - giga master */
  3232. rtl_writephy(tp, 0x1f, 0x0a43);
  3233. rtl_writephy(tp, 0x13, 0x809b);
  3234. rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
  3235. rtl_writephy(tp, 0x13, 0x80a2);
  3236. rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
  3237. rtl_writephy(tp, 0x13, 0x80a4);
  3238. rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
  3239. rtl_writephy(tp, 0x13, 0x809c);
  3240. rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
  3241. rtl_writephy(tp, 0x1f, 0x0000);
  3242. /* CHN EST parameters adjust - giga slave */
  3243. rtl_writephy(tp, 0x1f, 0x0a43);
  3244. rtl_writephy(tp, 0x13, 0x80ad);
  3245. rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
  3246. rtl_writephy(tp, 0x13, 0x80b4);
  3247. rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
  3248. rtl_writephy(tp, 0x13, 0x80ac);
  3249. rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
  3250. rtl_writephy(tp, 0x1f, 0x0000);
  3251. /* CHN EST parameters adjust - fnet */
  3252. rtl_writephy(tp, 0x1f, 0x0a43);
  3253. rtl_writephy(tp, 0x13, 0x808e);
  3254. rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
  3255. rtl_writephy(tp, 0x13, 0x8090);
  3256. rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
  3257. rtl_writephy(tp, 0x13, 0x8092);
  3258. rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
  3259. rtl_writephy(tp, 0x1f, 0x0000);
  3260. /* enable R-tune & PGA-retune function */
  3261. dout_tapbin = 0;
  3262. rtl_writephy(tp, 0x1f, 0x0a46);
  3263. data = rtl_readphy(tp, 0x13);
  3264. data &= 3;
  3265. data <<= 2;
  3266. dout_tapbin |= data;
  3267. data = rtl_readphy(tp, 0x12);
  3268. data &= 0xc000;
  3269. data >>= 14;
  3270. dout_tapbin |= data;
  3271. dout_tapbin = ~(dout_tapbin^0x08);
  3272. dout_tapbin <<= 12;
  3273. dout_tapbin &= 0xf000;
  3274. rtl_writephy(tp, 0x1f, 0x0a43);
  3275. rtl_writephy(tp, 0x13, 0x827a);
  3276. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3277. rtl_writephy(tp, 0x13, 0x827b);
  3278. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3279. rtl_writephy(tp, 0x13, 0x827c);
  3280. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3281. rtl_writephy(tp, 0x13, 0x827d);
  3282. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3283. rtl_writephy(tp, 0x1f, 0x0a43);
  3284. rtl_writephy(tp, 0x13, 0x0811);
  3285. rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
  3286. rtl_writephy(tp, 0x1f, 0x0a42);
  3287. rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
  3288. rtl_writephy(tp, 0x1f, 0x0000);
  3289. /* enable GPHY 10M */
  3290. rtl_writephy(tp, 0x1f, 0x0a44);
  3291. rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
  3292. rtl_writephy(tp, 0x1f, 0x0000);
  3293. /* SAR ADC performance */
  3294. rtl_writephy(tp, 0x1f, 0x0bca);
  3295. rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
  3296. rtl_writephy(tp, 0x1f, 0x0000);
  3297. rtl_writephy(tp, 0x1f, 0x0a43);
  3298. rtl_writephy(tp, 0x13, 0x803f);
  3299. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3300. rtl_writephy(tp, 0x13, 0x8047);
  3301. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3302. rtl_writephy(tp, 0x13, 0x804f);
  3303. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3304. rtl_writephy(tp, 0x13, 0x8057);
  3305. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3306. rtl_writephy(tp, 0x13, 0x805f);
  3307. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3308. rtl_writephy(tp, 0x13, 0x8067);
  3309. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3310. rtl_writephy(tp, 0x13, 0x806f);
  3311. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3312. rtl_writephy(tp, 0x1f, 0x0000);
  3313. /* disable phy pfm mode */
  3314. rtl_writephy(tp, 0x1f, 0x0a44);
  3315. rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
  3316. rtl_writephy(tp, 0x1f, 0x0000);
  3317. /* Check ALDPS bit, disable it if enabled */
  3318. rtl_writephy(tp, 0x1f, 0x0a43);
  3319. if (rtl_readphy(tp, 0x10) & 0x0004)
  3320. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3321. rtl_writephy(tp, 0x1f, 0x0000);
  3322. }
  3323. static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
  3324. {
  3325. u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
  3326. u16 rlen;
  3327. u32 data;
  3328. rtl_apply_firmware(tp);
  3329. /* CHIN EST parameter update */
  3330. rtl_writephy(tp, 0x1f, 0x0a43);
  3331. rtl_writephy(tp, 0x13, 0x808a);
  3332. rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
  3333. rtl_writephy(tp, 0x1f, 0x0000);
  3334. /* enable R-tune & PGA-retune function */
  3335. rtl_writephy(tp, 0x1f, 0x0a43);
  3336. rtl_writephy(tp, 0x13, 0x0811);
  3337. rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
  3338. rtl_writephy(tp, 0x1f, 0x0a42);
  3339. rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
  3340. rtl_writephy(tp, 0x1f, 0x0000);
  3341. /* enable GPHY 10M */
  3342. rtl_writephy(tp, 0x1f, 0x0a44);
  3343. rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
  3344. rtl_writephy(tp, 0x1f, 0x0000);
  3345. r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
  3346. data = r8168_mac_ocp_read(tp, 0xdd02);
  3347. ioffset_p3 = ((data & 0x80)>>7);
  3348. ioffset_p3 <<= 3;
  3349. data = r8168_mac_ocp_read(tp, 0xdd00);
  3350. ioffset_p3 |= ((data & (0xe000))>>13);
  3351. ioffset_p2 = ((data & (0x1e00))>>9);
  3352. ioffset_p1 = ((data & (0x01e0))>>5);
  3353. ioffset_p0 = ((data & 0x0010)>>4);
  3354. ioffset_p0 <<= 3;
  3355. ioffset_p0 |= (data & (0x07));
  3356. data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
  3357. if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
  3358. (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
  3359. rtl_writephy(tp, 0x1f, 0x0bcf);
  3360. rtl_writephy(tp, 0x16, data);
  3361. rtl_writephy(tp, 0x1f, 0x0000);
  3362. }
  3363. /* Modify rlen (TX LPF corner frequency) level */
  3364. rtl_writephy(tp, 0x1f, 0x0bcd);
  3365. data = rtl_readphy(tp, 0x16);
  3366. data &= 0x000f;
  3367. rlen = 0;
  3368. if (data > 3)
  3369. rlen = data - 3;
  3370. data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
  3371. rtl_writephy(tp, 0x17, data);
  3372. rtl_writephy(tp, 0x1f, 0x0bcd);
  3373. rtl_writephy(tp, 0x1f, 0x0000);
  3374. /* disable phy pfm mode */
  3375. rtl_writephy(tp, 0x1f, 0x0a44);
  3376. rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
  3377. rtl_writephy(tp, 0x1f, 0x0000);
  3378. /* Check ALDPS bit, disable it if enabled */
  3379. rtl_writephy(tp, 0x1f, 0x0a43);
  3380. if (rtl_readphy(tp, 0x10) & 0x0004)
  3381. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3382. rtl_writephy(tp, 0x1f, 0x0000);
  3383. }
  3384. static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
  3385. {
  3386. /* Enable PHY auto speed down */
  3387. rtl_writephy(tp, 0x1f, 0x0a44);
  3388. rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
  3389. rtl_writephy(tp, 0x1f, 0x0000);
  3390. /* patch 10M & ALDPS */
  3391. rtl_writephy(tp, 0x1f, 0x0bcc);
  3392. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
  3393. rtl_writephy(tp, 0x1f, 0x0a44);
  3394. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3395. rtl_writephy(tp, 0x1f, 0x0a43);
  3396. rtl_writephy(tp, 0x13, 0x8084);
  3397. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3398. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3399. rtl_writephy(tp, 0x1f, 0x0000);
  3400. /* Enable EEE auto-fallback function */
  3401. rtl_writephy(tp, 0x1f, 0x0a4b);
  3402. rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
  3403. rtl_writephy(tp, 0x1f, 0x0000);
  3404. /* Enable UC LPF tune function */
  3405. rtl_writephy(tp, 0x1f, 0x0a43);
  3406. rtl_writephy(tp, 0x13, 0x8012);
  3407. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3408. rtl_writephy(tp, 0x1f, 0x0000);
  3409. /* set rg_sel_sdm_rate */
  3410. rtl_writephy(tp, 0x1f, 0x0c42);
  3411. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3412. rtl_writephy(tp, 0x1f, 0x0000);
  3413. /* Check ALDPS bit, disable it if enabled */
  3414. rtl_writephy(tp, 0x1f, 0x0a43);
  3415. if (rtl_readphy(tp, 0x10) & 0x0004)
  3416. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3417. rtl_writephy(tp, 0x1f, 0x0000);
  3418. }
  3419. static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
  3420. {
  3421. /* patch 10M & ALDPS */
  3422. rtl_writephy(tp, 0x1f, 0x0bcc);
  3423. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
  3424. rtl_writephy(tp, 0x1f, 0x0a44);
  3425. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3426. rtl_writephy(tp, 0x1f, 0x0a43);
  3427. rtl_writephy(tp, 0x13, 0x8084);
  3428. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3429. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3430. rtl_writephy(tp, 0x1f, 0x0000);
  3431. /* Enable UC LPF tune function */
  3432. rtl_writephy(tp, 0x1f, 0x0a43);
  3433. rtl_writephy(tp, 0x13, 0x8012);
  3434. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3435. rtl_writephy(tp, 0x1f, 0x0000);
  3436. /* Set rg_sel_sdm_rate */
  3437. rtl_writephy(tp, 0x1f, 0x0c42);
  3438. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3439. rtl_writephy(tp, 0x1f, 0x0000);
  3440. /* Channel estimation parameters */
  3441. rtl_writephy(tp, 0x1f, 0x0a43);
  3442. rtl_writephy(tp, 0x13, 0x80f3);
  3443. rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
  3444. rtl_writephy(tp, 0x13, 0x80f0);
  3445. rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
  3446. rtl_writephy(tp, 0x13, 0x80ef);
  3447. rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
  3448. rtl_writephy(tp, 0x13, 0x80f6);
  3449. rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
  3450. rtl_writephy(tp, 0x13, 0x80ec);
  3451. rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
  3452. rtl_writephy(tp, 0x13, 0x80ed);
  3453. rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
  3454. rtl_writephy(tp, 0x13, 0x80f2);
  3455. rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
  3456. rtl_writephy(tp, 0x13, 0x80f4);
  3457. rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
  3458. rtl_writephy(tp, 0x1f, 0x0a43);
  3459. rtl_writephy(tp, 0x13, 0x8110);
  3460. rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
  3461. rtl_writephy(tp, 0x13, 0x810f);
  3462. rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
  3463. rtl_writephy(tp, 0x13, 0x8111);
  3464. rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
  3465. rtl_writephy(tp, 0x13, 0x8113);
  3466. rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
  3467. rtl_writephy(tp, 0x13, 0x8115);
  3468. rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
  3469. rtl_writephy(tp, 0x13, 0x810e);
  3470. rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
  3471. rtl_writephy(tp, 0x13, 0x810c);
  3472. rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
  3473. rtl_writephy(tp, 0x13, 0x810b);
  3474. rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
  3475. rtl_writephy(tp, 0x1f, 0x0a43);
  3476. rtl_writephy(tp, 0x13, 0x80d1);
  3477. rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
  3478. rtl_writephy(tp, 0x13, 0x80cd);
  3479. rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
  3480. rtl_writephy(tp, 0x13, 0x80d3);
  3481. rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
  3482. rtl_writephy(tp, 0x13, 0x80d5);
  3483. rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
  3484. rtl_writephy(tp, 0x13, 0x80d7);
  3485. rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
  3486. /* Force PWM-mode */
  3487. rtl_writephy(tp, 0x1f, 0x0bcd);
  3488. rtl_writephy(tp, 0x14, 0x5065);
  3489. rtl_writephy(tp, 0x14, 0xd065);
  3490. rtl_writephy(tp, 0x1f, 0x0bc8);
  3491. rtl_writephy(tp, 0x12, 0x00ed);
  3492. rtl_writephy(tp, 0x1f, 0x0bcd);
  3493. rtl_writephy(tp, 0x14, 0x1065);
  3494. rtl_writephy(tp, 0x14, 0x9065);
  3495. rtl_writephy(tp, 0x14, 0x1065);
  3496. rtl_writephy(tp, 0x1f, 0x0000);
  3497. /* Check ALDPS bit, disable it if enabled */
  3498. rtl_writephy(tp, 0x1f, 0x0a43);
  3499. if (rtl_readphy(tp, 0x10) & 0x0004)
  3500. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3501. rtl_writephy(tp, 0x1f, 0x0000);
  3502. }
  3503. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  3504. {
  3505. static const struct phy_reg phy_reg_init[] = {
  3506. { 0x1f, 0x0003 },
  3507. { 0x08, 0x441d },
  3508. { 0x01, 0x9100 },
  3509. { 0x1f, 0x0000 }
  3510. };
  3511. rtl_writephy(tp, 0x1f, 0x0000);
  3512. rtl_patchphy(tp, 0x11, 1 << 12);
  3513. rtl_patchphy(tp, 0x19, 1 << 13);
  3514. rtl_patchphy(tp, 0x10, 1 << 15);
  3515. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3516. }
  3517. static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
  3518. {
  3519. static const struct phy_reg phy_reg_init[] = {
  3520. { 0x1f, 0x0005 },
  3521. { 0x1a, 0x0000 },
  3522. { 0x1f, 0x0000 },
  3523. { 0x1f, 0x0004 },
  3524. { 0x1c, 0x0000 },
  3525. { 0x1f, 0x0000 },
  3526. { 0x1f, 0x0001 },
  3527. { 0x15, 0x7701 },
  3528. { 0x1f, 0x0000 }
  3529. };
  3530. /* Disable ALDPS before ram code */
  3531. rtl_writephy(tp, 0x1f, 0x0000);
  3532. rtl_writephy(tp, 0x18, 0x0310);
  3533. msleep(100);
  3534. rtl_apply_firmware(tp);
  3535. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3536. }
  3537. static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
  3538. {
  3539. /* Disable ALDPS before setting firmware */
  3540. rtl_writephy(tp, 0x1f, 0x0000);
  3541. rtl_writephy(tp, 0x18, 0x0310);
  3542. msleep(20);
  3543. rtl_apply_firmware(tp);
  3544. /* EEE setting */
  3545. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3546. rtl_writephy(tp, 0x1f, 0x0004);
  3547. rtl_writephy(tp, 0x10, 0x401f);
  3548. rtl_writephy(tp, 0x19, 0x7030);
  3549. rtl_writephy(tp, 0x1f, 0x0000);
  3550. }
  3551. static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
  3552. {
  3553. static const struct phy_reg phy_reg_init[] = {
  3554. { 0x1f, 0x0004 },
  3555. { 0x10, 0xc07f },
  3556. { 0x19, 0x7030 },
  3557. { 0x1f, 0x0000 }
  3558. };
  3559. /* Disable ALDPS before ram code */
  3560. rtl_writephy(tp, 0x1f, 0x0000);
  3561. rtl_writephy(tp, 0x18, 0x0310);
  3562. msleep(100);
  3563. rtl_apply_firmware(tp);
  3564. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3565. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3566. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3567. }
  3568. static void rtl_hw_phy_config(struct net_device *dev)
  3569. {
  3570. struct rtl8169_private *tp = netdev_priv(dev);
  3571. rtl8169_print_mac_version(tp);
  3572. switch (tp->mac_version) {
  3573. case RTL_GIGA_MAC_VER_01:
  3574. break;
  3575. case RTL_GIGA_MAC_VER_02:
  3576. case RTL_GIGA_MAC_VER_03:
  3577. rtl8169s_hw_phy_config(tp);
  3578. break;
  3579. case RTL_GIGA_MAC_VER_04:
  3580. rtl8169sb_hw_phy_config(tp);
  3581. break;
  3582. case RTL_GIGA_MAC_VER_05:
  3583. rtl8169scd_hw_phy_config(tp);
  3584. break;
  3585. case RTL_GIGA_MAC_VER_06:
  3586. rtl8169sce_hw_phy_config(tp);
  3587. break;
  3588. case RTL_GIGA_MAC_VER_07:
  3589. case RTL_GIGA_MAC_VER_08:
  3590. case RTL_GIGA_MAC_VER_09:
  3591. rtl8102e_hw_phy_config(tp);
  3592. break;
  3593. case RTL_GIGA_MAC_VER_11:
  3594. rtl8168bb_hw_phy_config(tp);
  3595. break;
  3596. case RTL_GIGA_MAC_VER_12:
  3597. rtl8168bef_hw_phy_config(tp);
  3598. break;
  3599. case RTL_GIGA_MAC_VER_17:
  3600. rtl8168bef_hw_phy_config(tp);
  3601. break;
  3602. case RTL_GIGA_MAC_VER_18:
  3603. rtl8168cp_1_hw_phy_config(tp);
  3604. break;
  3605. case RTL_GIGA_MAC_VER_19:
  3606. rtl8168c_1_hw_phy_config(tp);
  3607. break;
  3608. case RTL_GIGA_MAC_VER_20:
  3609. rtl8168c_2_hw_phy_config(tp);
  3610. break;
  3611. case RTL_GIGA_MAC_VER_21:
  3612. rtl8168c_3_hw_phy_config(tp);
  3613. break;
  3614. case RTL_GIGA_MAC_VER_22:
  3615. rtl8168c_4_hw_phy_config(tp);
  3616. break;
  3617. case RTL_GIGA_MAC_VER_23:
  3618. case RTL_GIGA_MAC_VER_24:
  3619. rtl8168cp_2_hw_phy_config(tp);
  3620. break;
  3621. case RTL_GIGA_MAC_VER_25:
  3622. rtl8168d_1_hw_phy_config(tp);
  3623. break;
  3624. case RTL_GIGA_MAC_VER_26:
  3625. rtl8168d_2_hw_phy_config(tp);
  3626. break;
  3627. case RTL_GIGA_MAC_VER_27:
  3628. rtl8168d_3_hw_phy_config(tp);
  3629. break;
  3630. case RTL_GIGA_MAC_VER_28:
  3631. rtl8168d_4_hw_phy_config(tp);
  3632. break;
  3633. case RTL_GIGA_MAC_VER_29:
  3634. case RTL_GIGA_MAC_VER_30:
  3635. rtl8105e_hw_phy_config(tp);
  3636. break;
  3637. case RTL_GIGA_MAC_VER_31:
  3638. /* None. */
  3639. break;
  3640. case RTL_GIGA_MAC_VER_32:
  3641. case RTL_GIGA_MAC_VER_33:
  3642. rtl8168e_1_hw_phy_config(tp);
  3643. break;
  3644. case RTL_GIGA_MAC_VER_34:
  3645. rtl8168e_2_hw_phy_config(tp);
  3646. break;
  3647. case RTL_GIGA_MAC_VER_35:
  3648. rtl8168f_1_hw_phy_config(tp);
  3649. break;
  3650. case RTL_GIGA_MAC_VER_36:
  3651. rtl8168f_2_hw_phy_config(tp);
  3652. break;
  3653. case RTL_GIGA_MAC_VER_37:
  3654. rtl8402_hw_phy_config(tp);
  3655. break;
  3656. case RTL_GIGA_MAC_VER_38:
  3657. rtl8411_hw_phy_config(tp);
  3658. break;
  3659. case RTL_GIGA_MAC_VER_39:
  3660. rtl8106e_hw_phy_config(tp);
  3661. break;
  3662. case RTL_GIGA_MAC_VER_40:
  3663. rtl8168g_1_hw_phy_config(tp);
  3664. break;
  3665. case RTL_GIGA_MAC_VER_42:
  3666. case RTL_GIGA_MAC_VER_43:
  3667. case RTL_GIGA_MAC_VER_44:
  3668. rtl8168g_2_hw_phy_config(tp);
  3669. break;
  3670. case RTL_GIGA_MAC_VER_45:
  3671. case RTL_GIGA_MAC_VER_47:
  3672. rtl8168h_1_hw_phy_config(tp);
  3673. break;
  3674. case RTL_GIGA_MAC_VER_46:
  3675. case RTL_GIGA_MAC_VER_48:
  3676. rtl8168h_2_hw_phy_config(tp);
  3677. break;
  3678. case RTL_GIGA_MAC_VER_49:
  3679. rtl8168ep_1_hw_phy_config(tp);
  3680. break;
  3681. case RTL_GIGA_MAC_VER_50:
  3682. case RTL_GIGA_MAC_VER_51:
  3683. rtl8168ep_2_hw_phy_config(tp);
  3684. break;
  3685. case RTL_GIGA_MAC_VER_41:
  3686. default:
  3687. break;
  3688. }
  3689. }
  3690. static void rtl_phy_work(struct rtl8169_private *tp)
  3691. {
  3692. struct timer_list *timer = &tp->timer;
  3693. void __iomem *ioaddr = tp->mmio_addr;
  3694. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  3695. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  3696. if (tp->phy_reset_pending(tp)) {
  3697. /*
  3698. * A busy loop could burn quite a few cycles on nowadays CPU.
  3699. * Let's delay the execution of the timer for a few ticks.
  3700. */
  3701. timeout = HZ/10;
  3702. goto out_mod_timer;
  3703. }
  3704. if (tp->link_ok(ioaddr))
  3705. return;
  3706. netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
  3707. tp->phy_reset_enable(tp);
  3708. out_mod_timer:
  3709. mod_timer(timer, jiffies + timeout);
  3710. }
  3711. static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
  3712. {
  3713. if (!test_and_set_bit(flag, tp->wk.flags))
  3714. schedule_work(&tp->wk.work);
  3715. }
  3716. static void rtl8169_phy_timer(unsigned long __opaque)
  3717. {
  3718. struct net_device *dev = (struct net_device *)__opaque;
  3719. struct rtl8169_private *tp = netdev_priv(dev);
  3720. rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
  3721. }
  3722. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  3723. void __iomem *ioaddr)
  3724. {
  3725. iounmap(ioaddr);
  3726. pci_release_regions(pdev);
  3727. pci_clear_mwi(pdev);
  3728. pci_disable_device(pdev);
  3729. free_netdev(dev);
  3730. }
  3731. DECLARE_RTL_COND(rtl_phy_reset_cond)
  3732. {
  3733. return tp->phy_reset_pending(tp);
  3734. }
  3735. static void rtl8169_phy_reset(struct net_device *dev,
  3736. struct rtl8169_private *tp)
  3737. {
  3738. tp->phy_reset_enable(tp);
  3739. rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
  3740. }
  3741. static bool rtl_tbi_enabled(struct rtl8169_private *tp)
  3742. {
  3743. void __iomem *ioaddr = tp->mmio_addr;
  3744. return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
  3745. (RTL_R8(PHYstatus) & TBI_Enable);
  3746. }
  3747. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  3748. {
  3749. void __iomem *ioaddr = tp->mmio_addr;
  3750. rtl_hw_phy_config(dev);
  3751. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  3752. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  3753. RTL_W8(0x82, 0x01);
  3754. }
  3755. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  3756. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  3757. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  3758. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  3759. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  3760. RTL_W8(0x82, 0x01);
  3761. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  3762. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  3763. }
  3764. rtl8169_phy_reset(dev, tp);
  3765. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
  3766. ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  3767. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  3768. (tp->mii.supports_gmii ?
  3769. ADVERTISED_1000baseT_Half |
  3770. ADVERTISED_1000baseT_Full : 0));
  3771. if (rtl_tbi_enabled(tp))
  3772. netif_info(tp, link, dev, "TBI auto-negotiating\n");
  3773. }
  3774. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  3775. {
  3776. void __iomem *ioaddr = tp->mmio_addr;
  3777. rtl_lock_work(tp);
  3778. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3779. RTL_W32(MAC4, addr[4] | addr[5] << 8);
  3780. RTL_R32(MAC4);
  3781. RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
  3782. RTL_R32(MAC0);
  3783. if (tp->mac_version == RTL_GIGA_MAC_VER_34)
  3784. rtl_rar_exgmac_set(tp, addr);
  3785. RTL_W8(Cfg9346, Cfg9346_Lock);
  3786. rtl_unlock_work(tp);
  3787. }
  3788. static int rtl_set_mac_address(struct net_device *dev, void *p)
  3789. {
  3790. struct rtl8169_private *tp = netdev_priv(dev);
  3791. struct device *d = &tp->pci_dev->dev;
  3792. struct sockaddr *addr = p;
  3793. if (!is_valid_ether_addr(addr->sa_data))
  3794. return -EADDRNOTAVAIL;
  3795. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  3796. pm_runtime_get_noresume(d);
  3797. if (pm_runtime_active(d))
  3798. rtl_rar_set(tp, dev->dev_addr);
  3799. pm_runtime_put_noidle(d);
  3800. return 0;
  3801. }
  3802. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  3803. {
  3804. struct rtl8169_private *tp = netdev_priv(dev);
  3805. struct mii_ioctl_data *data = if_mii(ifr);
  3806. return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
  3807. }
  3808. static int rtl_xmii_ioctl(struct rtl8169_private *tp,
  3809. struct mii_ioctl_data *data, int cmd)
  3810. {
  3811. switch (cmd) {
  3812. case SIOCGMIIPHY:
  3813. data->phy_id = 32; /* Internal PHY */
  3814. return 0;
  3815. case SIOCGMIIREG:
  3816. data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
  3817. return 0;
  3818. case SIOCSMIIREG:
  3819. rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
  3820. return 0;
  3821. }
  3822. return -EOPNOTSUPP;
  3823. }
  3824. static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  3825. {
  3826. return -EOPNOTSUPP;
  3827. }
  3828. static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
  3829. {
  3830. if (tp->features & RTL_FEATURE_MSI) {
  3831. pci_disable_msi(pdev);
  3832. tp->features &= ~RTL_FEATURE_MSI;
  3833. }
  3834. }
  3835. static void rtl_init_mdio_ops(struct rtl8169_private *tp)
  3836. {
  3837. struct mdio_ops *ops = &tp->mdio_ops;
  3838. switch (tp->mac_version) {
  3839. case RTL_GIGA_MAC_VER_27:
  3840. ops->write = r8168dp_1_mdio_write;
  3841. ops->read = r8168dp_1_mdio_read;
  3842. break;
  3843. case RTL_GIGA_MAC_VER_28:
  3844. case RTL_GIGA_MAC_VER_31:
  3845. ops->write = r8168dp_2_mdio_write;
  3846. ops->read = r8168dp_2_mdio_read;
  3847. break;
  3848. case RTL_GIGA_MAC_VER_40:
  3849. case RTL_GIGA_MAC_VER_41:
  3850. case RTL_GIGA_MAC_VER_42:
  3851. case RTL_GIGA_MAC_VER_43:
  3852. case RTL_GIGA_MAC_VER_44:
  3853. case RTL_GIGA_MAC_VER_45:
  3854. case RTL_GIGA_MAC_VER_46:
  3855. case RTL_GIGA_MAC_VER_47:
  3856. case RTL_GIGA_MAC_VER_48:
  3857. case RTL_GIGA_MAC_VER_49:
  3858. case RTL_GIGA_MAC_VER_50:
  3859. case RTL_GIGA_MAC_VER_51:
  3860. ops->write = r8168g_mdio_write;
  3861. ops->read = r8168g_mdio_read;
  3862. break;
  3863. default:
  3864. ops->write = r8169_mdio_write;
  3865. ops->read = r8169_mdio_read;
  3866. break;
  3867. }
  3868. }
  3869. static void rtl_speed_down(struct rtl8169_private *tp)
  3870. {
  3871. u32 adv;
  3872. int lpa;
  3873. rtl_writephy(tp, 0x1f, 0x0000);
  3874. lpa = rtl_readphy(tp, MII_LPA);
  3875. if (lpa & (LPA_10HALF | LPA_10FULL))
  3876. adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
  3877. else if (lpa & (LPA_100HALF | LPA_100FULL))
  3878. adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  3879. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
  3880. else
  3881. adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  3882. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  3883. (tp->mii.supports_gmii ?
  3884. ADVERTISED_1000baseT_Half |
  3885. ADVERTISED_1000baseT_Full : 0);
  3886. rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
  3887. adv);
  3888. }
  3889. static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
  3890. {
  3891. void __iomem *ioaddr = tp->mmio_addr;
  3892. switch (tp->mac_version) {
  3893. case RTL_GIGA_MAC_VER_25:
  3894. case RTL_GIGA_MAC_VER_26:
  3895. case RTL_GIGA_MAC_VER_29:
  3896. case RTL_GIGA_MAC_VER_30:
  3897. case RTL_GIGA_MAC_VER_32:
  3898. case RTL_GIGA_MAC_VER_33:
  3899. case RTL_GIGA_MAC_VER_34:
  3900. case RTL_GIGA_MAC_VER_37:
  3901. case RTL_GIGA_MAC_VER_38:
  3902. case RTL_GIGA_MAC_VER_39:
  3903. case RTL_GIGA_MAC_VER_40:
  3904. case RTL_GIGA_MAC_VER_41:
  3905. case RTL_GIGA_MAC_VER_42:
  3906. case RTL_GIGA_MAC_VER_43:
  3907. case RTL_GIGA_MAC_VER_44:
  3908. case RTL_GIGA_MAC_VER_45:
  3909. case RTL_GIGA_MAC_VER_46:
  3910. case RTL_GIGA_MAC_VER_47:
  3911. case RTL_GIGA_MAC_VER_48:
  3912. case RTL_GIGA_MAC_VER_49:
  3913. case RTL_GIGA_MAC_VER_50:
  3914. case RTL_GIGA_MAC_VER_51:
  3915. RTL_W32(RxConfig, RTL_R32(RxConfig) |
  3916. AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
  3917. break;
  3918. default:
  3919. break;
  3920. }
  3921. }
  3922. static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
  3923. {
  3924. if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
  3925. return false;
  3926. rtl_speed_down(tp);
  3927. rtl_wol_suspend_quirk(tp);
  3928. return true;
  3929. }
  3930. static void r810x_phy_power_down(struct rtl8169_private *tp)
  3931. {
  3932. rtl_writephy(tp, 0x1f, 0x0000);
  3933. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  3934. }
  3935. static void r810x_phy_power_up(struct rtl8169_private *tp)
  3936. {
  3937. rtl_writephy(tp, 0x1f, 0x0000);
  3938. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  3939. }
  3940. static void r810x_pll_power_down(struct rtl8169_private *tp)
  3941. {
  3942. void __iomem *ioaddr = tp->mmio_addr;
  3943. if (rtl_wol_pll_power_down(tp))
  3944. return;
  3945. r810x_phy_power_down(tp);
  3946. switch (tp->mac_version) {
  3947. case RTL_GIGA_MAC_VER_07:
  3948. case RTL_GIGA_MAC_VER_08:
  3949. case RTL_GIGA_MAC_VER_09:
  3950. case RTL_GIGA_MAC_VER_10:
  3951. case RTL_GIGA_MAC_VER_13:
  3952. case RTL_GIGA_MAC_VER_16:
  3953. break;
  3954. default:
  3955. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  3956. break;
  3957. }
  3958. }
  3959. static void r810x_pll_power_up(struct rtl8169_private *tp)
  3960. {
  3961. void __iomem *ioaddr = tp->mmio_addr;
  3962. r810x_phy_power_up(tp);
  3963. switch (tp->mac_version) {
  3964. case RTL_GIGA_MAC_VER_07:
  3965. case RTL_GIGA_MAC_VER_08:
  3966. case RTL_GIGA_MAC_VER_09:
  3967. case RTL_GIGA_MAC_VER_10:
  3968. case RTL_GIGA_MAC_VER_13:
  3969. case RTL_GIGA_MAC_VER_16:
  3970. break;
  3971. case RTL_GIGA_MAC_VER_47:
  3972. case RTL_GIGA_MAC_VER_48:
  3973. RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
  3974. break;
  3975. default:
  3976. RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
  3977. break;
  3978. }
  3979. }
  3980. static void r8168_phy_power_up(struct rtl8169_private *tp)
  3981. {
  3982. rtl_writephy(tp, 0x1f, 0x0000);
  3983. switch (tp->mac_version) {
  3984. case RTL_GIGA_MAC_VER_11:
  3985. case RTL_GIGA_MAC_VER_12:
  3986. case RTL_GIGA_MAC_VER_17:
  3987. case RTL_GIGA_MAC_VER_18:
  3988. case RTL_GIGA_MAC_VER_19:
  3989. case RTL_GIGA_MAC_VER_20:
  3990. case RTL_GIGA_MAC_VER_21:
  3991. case RTL_GIGA_MAC_VER_22:
  3992. case RTL_GIGA_MAC_VER_23:
  3993. case RTL_GIGA_MAC_VER_24:
  3994. case RTL_GIGA_MAC_VER_25:
  3995. case RTL_GIGA_MAC_VER_26:
  3996. case RTL_GIGA_MAC_VER_27:
  3997. case RTL_GIGA_MAC_VER_28:
  3998. case RTL_GIGA_MAC_VER_31:
  3999. rtl_writephy(tp, 0x0e, 0x0000);
  4000. break;
  4001. default:
  4002. break;
  4003. }
  4004. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  4005. }
  4006. static void r8168_phy_power_down(struct rtl8169_private *tp)
  4007. {
  4008. rtl_writephy(tp, 0x1f, 0x0000);
  4009. switch (tp->mac_version) {
  4010. case RTL_GIGA_MAC_VER_32:
  4011. case RTL_GIGA_MAC_VER_33:
  4012. case RTL_GIGA_MAC_VER_40:
  4013. case RTL_GIGA_MAC_VER_41:
  4014. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
  4015. break;
  4016. case RTL_GIGA_MAC_VER_11:
  4017. case RTL_GIGA_MAC_VER_12:
  4018. case RTL_GIGA_MAC_VER_17:
  4019. case RTL_GIGA_MAC_VER_18:
  4020. case RTL_GIGA_MAC_VER_19:
  4021. case RTL_GIGA_MAC_VER_20:
  4022. case RTL_GIGA_MAC_VER_21:
  4023. case RTL_GIGA_MAC_VER_22:
  4024. case RTL_GIGA_MAC_VER_23:
  4025. case RTL_GIGA_MAC_VER_24:
  4026. case RTL_GIGA_MAC_VER_25:
  4027. case RTL_GIGA_MAC_VER_26:
  4028. case RTL_GIGA_MAC_VER_27:
  4029. case RTL_GIGA_MAC_VER_28:
  4030. case RTL_GIGA_MAC_VER_31:
  4031. rtl_writephy(tp, 0x0e, 0x0200);
  4032. default:
  4033. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  4034. break;
  4035. }
  4036. }
  4037. static void r8168_pll_power_down(struct rtl8169_private *tp)
  4038. {
  4039. void __iomem *ioaddr = tp->mmio_addr;
  4040. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  4041. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  4042. tp->mac_version == RTL_GIGA_MAC_VER_31 ||
  4043. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  4044. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  4045. tp->mac_version == RTL_GIGA_MAC_VER_51) &&
  4046. r8168_check_dash(tp)) {
  4047. return;
  4048. }
  4049. if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
  4050. tp->mac_version == RTL_GIGA_MAC_VER_24) &&
  4051. (RTL_R16(CPlusCmd) & ASF)) {
  4052. return;
  4053. }
  4054. if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
  4055. tp->mac_version == RTL_GIGA_MAC_VER_33)
  4056. rtl_ephy_write(tp, 0x19, 0xff64);
  4057. if (rtl_wol_pll_power_down(tp))
  4058. return;
  4059. r8168_phy_power_down(tp);
  4060. switch (tp->mac_version) {
  4061. case RTL_GIGA_MAC_VER_25:
  4062. case RTL_GIGA_MAC_VER_26:
  4063. case RTL_GIGA_MAC_VER_27:
  4064. case RTL_GIGA_MAC_VER_28:
  4065. case RTL_GIGA_MAC_VER_31:
  4066. case RTL_GIGA_MAC_VER_32:
  4067. case RTL_GIGA_MAC_VER_33:
  4068. case RTL_GIGA_MAC_VER_44:
  4069. case RTL_GIGA_MAC_VER_45:
  4070. case RTL_GIGA_MAC_VER_46:
  4071. case RTL_GIGA_MAC_VER_50:
  4072. case RTL_GIGA_MAC_VER_51:
  4073. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  4074. break;
  4075. case RTL_GIGA_MAC_VER_40:
  4076. case RTL_GIGA_MAC_VER_41:
  4077. case RTL_GIGA_MAC_VER_49:
  4078. rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
  4079. 0xfc000000, ERIAR_EXGMAC);
  4080. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  4081. break;
  4082. }
  4083. }
  4084. static void r8168_pll_power_up(struct rtl8169_private *tp)
  4085. {
  4086. void __iomem *ioaddr = tp->mmio_addr;
  4087. switch (tp->mac_version) {
  4088. case RTL_GIGA_MAC_VER_25:
  4089. case RTL_GIGA_MAC_VER_26:
  4090. case RTL_GIGA_MAC_VER_27:
  4091. case RTL_GIGA_MAC_VER_28:
  4092. case RTL_GIGA_MAC_VER_31:
  4093. case RTL_GIGA_MAC_VER_32:
  4094. case RTL_GIGA_MAC_VER_33:
  4095. RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
  4096. break;
  4097. case RTL_GIGA_MAC_VER_44:
  4098. case RTL_GIGA_MAC_VER_45:
  4099. case RTL_GIGA_MAC_VER_46:
  4100. case RTL_GIGA_MAC_VER_50:
  4101. case RTL_GIGA_MAC_VER_51:
  4102. RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
  4103. break;
  4104. case RTL_GIGA_MAC_VER_40:
  4105. case RTL_GIGA_MAC_VER_41:
  4106. case RTL_GIGA_MAC_VER_49:
  4107. RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
  4108. rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
  4109. 0x00000000, ERIAR_EXGMAC);
  4110. break;
  4111. }
  4112. r8168_phy_power_up(tp);
  4113. }
  4114. static void rtl_generic_op(struct rtl8169_private *tp,
  4115. void (*op)(struct rtl8169_private *))
  4116. {
  4117. if (op)
  4118. op(tp);
  4119. }
  4120. static void rtl_pll_power_down(struct rtl8169_private *tp)
  4121. {
  4122. rtl_generic_op(tp, tp->pll_power_ops.down);
  4123. }
  4124. static void rtl_pll_power_up(struct rtl8169_private *tp)
  4125. {
  4126. rtl_generic_op(tp, tp->pll_power_ops.up);
  4127. /* give MAC/PHY some time to resume */
  4128. msleep(20);
  4129. }
  4130. static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
  4131. {
  4132. struct pll_power_ops *ops = &tp->pll_power_ops;
  4133. switch (tp->mac_version) {
  4134. case RTL_GIGA_MAC_VER_07:
  4135. case RTL_GIGA_MAC_VER_08:
  4136. case RTL_GIGA_MAC_VER_09:
  4137. case RTL_GIGA_MAC_VER_10:
  4138. case RTL_GIGA_MAC_VER_16:
  4139. case RTL_GIGA_MAC_VER_29:
  4140. case RTL_GIGA_MAC_VER_30:
  4141. case RTL_GIGA_MAC_VER_37:
  4142. case RTL_GIGA_MAC_VER_39:
  4143. case RTL_GIGA_MAC_VER_43:
  4144. case RTL_GIGA_MAC_VER_47:
  4145. case RTL_GIGA_MAC_VER_48:
  4146. ops->down = r810x_pll_power_down;
  4147. ops->up = r810x_pll_power_up;
  4148. break;
  4149. case RTL_GIGA_MAC_VER_11:
  4150. case RTL_GIGA_MAC_VER_12:
  4151. case RTL_GIGA_MAC_VER_17:
  4152. case RTL_GIGA_MAC_VER_18:
  4153. case RTL_GIGA_MAC_VER_19:
  4154. case RTL_GIGA_MAC_VER_20:
  4155. case RTL_GIGA_MAC_VER_21:
  4156. case RTL_GIGA_MAC_VER_22:
  4157. case RTL_GIGA_MAC_VER_23:
  4158. case RTL_GIGA_MAC_VER_24:
  4159. case RTL_GIGA_MAC_VER_25:
  4160. case RTL_GIGA_MAC_VER_26:
  4161. case RTL_GIGA_MAC_VER_27:
  4162. case RTL_GIGA_MAC_VER_28:
  4163. case RTL_GIGA_MAC_VER_31:
  4164. case RTL_GIGA_MAC_VER_32:
  4165. case RTL_GIGA_MAC_VER_33:
  4166. case RTL_GIGA_MAC_VER_34:
  4167. case RTL_GIGA_MAC_VER_35:
  4168. case RTL_GIGA_MAC_VER_36:
  4169. case RTL_GIGA_MAC_VER_38:
  4170. case RTL_GIGA_MAC_VER_40:
  4171. case RTL_GIGA_MAC_VER_41:
  4172. case RTL_GIGA_MAC_VER_42:
  4173. case RTL_GIGA_MAC_VER_44:
  4174. case RTL_GIGA_MAC_VER_45:
  4175. case RTL_GIGA_MAC_VER_46:
  4176. case RTL_GIGA_MAC_VER_49:
  4177. case RTL_GIGA_MAC_VER_50:
  4178. case RTL_GIGA_MAC_VER_51:
  4179. ops->down = r8168_pll_power_down;
  4180. ops->up = r8168_pll_power_up;
  4181. break;
  4182. default:
  4183. ops->down = NULL;
  4184. ops->up = NULL;
  4185. break;
  4186. }
  4187. }
  4188. static void rtl_init_rxcfg(struct rtl8169_private *tp)
  4189. {
  4190. void __iomem *ioaddr = tp->mmio_addr;
  4191. switch (tp->mac_version) {
  4192. case RTL_GIGA_MAC_VER_01:
  4193. case RTL_GIGA_MAC_VER_02:
  4194. case RTL_GIGA_MAC_VER_03:
  4195. case RTL_GIGA_MAC_VER_04:
  4196. case RTL_GIGA_MAC_VER_05:
  4197. case RTL_GIGA_MAC_VER_06:
  4198. case RTL_GIGA_MAC_VER_10:
  4199. case RTL_GIGA_MAC_VER_11:
  4200. case RTL_GIGA_MAC_VER_12:
  4201. case RTL_GIGA_MAC_VER_13:
  4202. case RTL_GIGA_MAC_VER_14:
  4203. case RTL_GIGA_MAC_VER_15:
  4204. case RTL_GIGA_MAC_VER_16:
  4205. case RTL_GIGA_MAC_VER_17:
  4206. RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
  4207. break;
  4208. case RTL_GIGA_MAC_VER_18:
  4209. case RTL_GIGA_MAC_VER_19:
  4210. case RTL_GIGA_MAC_VER_20:
  4211. case RTL_GIGA_MAC_VER_21:
  4212. case RTL_GIGA_MAC_VER_22:
  4213. case RTL_GIGA_MAC_VER_23:
  4214. case RTL_GIGA_MAC_VER_24:
  4215. case RTL_GIGA_MAC_VER_34:
  4216. case RTL_GIGA_MAC_VER_35:
  4217. RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
  4218. break;
  4219. case RTL_GIGA_MAC_VER_40:
  4220. case RTL_GIGA_MAC_VER_41:
  4221. case RTL_GIGA_MAC_VER_42:
  4222. case RTL_GIGA_MAC_VER_43:
  4223. case RTL_GIGA_MAC_VER_44:
  4224. case RTL_GIGA_MAC_VER_45:
  4225. case RTL_GIGA_MAC_VER_46:
  4226. case RTL_GIGA_MAC_VER_47:
  4227. case RTL_GIGA_MAC_VER_48:
  4228. case RTL_GIGA_MAC_VER_49:
  4229. case RTL_GIGA_MAC_VER_50:
  4230. case RTL_GIGA_MAC_VER_51:
  4231. RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
  4232. break;
  4233. default:
  4234. RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
  4235. break;
  4236. }
  4237. }
  4238. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  4239. {
  4240. tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
  4241. }
  4242. static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
  4243. {
  4244. void __iomem *ioaddr = tp->mmio_addr;
  4245. RTL_W8(Cfg9346, Cfg9346_Unlock);
  4246. rtl_generic_op(tp, tp->jumbo_ops.enable);
  4247. RTL_W8(Cfg9346, Cfg9346_Lock);
  4248. }
  4249. static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
  4250. {
  4251. void __iomem *ioaddr = tp->mmio_addr;
  4252. RTL_W8(Cfg9346, Cfg9346_Unlock);
  4253. rtl_generic_op(tp, tp->jumbo_ops.disable);
  4254. RTL_W8(Cfg9346, Cfg9346_Lock);
  4255. }
  4256. static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
  4257. {
  4258. void __iomem *ioaddr = tp->mmio_addr;
  4259. RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
  4260. RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
  4261. rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
  4262. }
  4263. static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
  4264. {
  4265. void __iomem *ioaddr = tp->mmio_addr;
  4266. RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
  4267. RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
  4268. rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4269. }
  4270. static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
  4271. {
  4272. void __iomem *ioaddr = tp->mmio_addr;
  4273. RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
  4274. }
  4275. static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
  4276. {
  4277. void __iomem *ioaddr = tp->mmio_addr;
  4278. RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
  4279. }
  4280. static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
  4281. {
  4282. void __iomem *ioaddr = tp->mmio_addr;
  4283. RTL_W8(MaxTxPacketSize, 0x3f);
  4284. RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
  4285. RTL_W8(Config4, RTL_R8(Config4) | 0x01);
  4286. rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
  4287. }
  4288. static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
  4289. {
  4290. void __iomem *ioaddr = tp->mmio_addr;
  4291. RTL_W8(MaxTxPacketSize, 0x0c);
  4292. RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
  4293. RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
  4294. rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4295. }
  4296. static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
  4297. {
  4298. rtl_tx_performance_tweak(tp->pci_dev,
  4299. PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
  4300. }
  4301. static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
  4302. {
  4303. rtl_tx_performance_tweak(tp->pci_dev,
  4304. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  4305. }
  4306. static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
  4307. {
  4308. void __iomem *ioaddr = tp->mmio_addr;
  4309. r8168b_0_hw_jumbo_enable(tp);
  4310. RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
  4311. }
  4312. static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
  4313. {
  4314. void __iomem *ioaddr = tp->mmio_addr;
  4315. r8168b_0_hw_jumbo_disable(tp);
  4316. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  4317. }
  4318. static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
  4319. {
  4320. struct jumbo_ops *ops = &tp->jumbo_ops;
  4321. switch (tp->mac_version) {
  4322. case RTL_GIGA_MAC_VER_11:
  4323. ops->disable = r8168b_0_hw_jumbo_disable;
  4324. ops->enable = r8168b_0_hw_jumbo_enable;
  4325. break;
  4326. case RTL_GIGA_MAC_VER_12:
  4327. case RTL_GIGA_MAC_VER_17:
  4328. ops->disable = r8168b_1_hw_jumbo_disable;
  4329. ops->enable = r8168b_1_hw_jumbo_enable;
  4330. break;
  4331. case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
  4332. case RTL_GIGA_MAC_VER_19:
  4333. case RTL_GIGA_MAC_VER_20:
  4334. case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
  4335. case RTL_GIGA_MAC_VER_22:
  4336. case RTL_GIGA_MAC_VER_23:
  4337. case RTL_GIGA_MAC_VER_24:
  4338. case RTL_GIGA_MAC_VER_25:
  4339. case RTL_GIGA_MAC_VER_26:
  4340. ops->disable = r8168c_hw_jumbo_disable;
  4341. ops->enable = r8168c_hw_jumbo_enable;
  4342. break;
  4343. case RTL_GIGA_MAC_VER_27:
  4344. case RTL_GIGA_MAC_VER_28:
  4345. ops->disable = r8168dp_hw_jumbo_disable;
  4346. ops->enable = r8168dp_hw_jumbo_enable;
  4347. break;
  4348. case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
  4349. case RTL_GIGA_MAC_VER_32:
  4350. case RTL_GIGA_MAC_VER_33:
  4351. case RTL_GIGA_MAC_VER_34:
  4352. ops->disable = r8168e_hw_jumbo_disable;
  4353. ops->enable = r8168e_hw_jumbo_enable;
  4354. break;
  4355. /*
  4356. * No action needed for jumbo frames with 8169.
  4357. * No jumbo for 810x at all.
  4358. */
  4359. case RTL_GIGA_MAC_VER_40:
  4360. case RTL_GIGA_MAC_VER_41:
  4361. case RTL_GIGA_MAC_VER_42:
  4362. case RTL_GIGA_MAC_VER_43:
  4363. case RTL_GIGA_MAC_VER_44:
  4364. case RTL_GIGA_MAC_VER_45:
  4365. case RTL_GIGA_MAC_VER_46:
  4366. case RTL_GIGA_MAC_VER_47:
  4367. case RTL_GIGA_MAC_VER_48:
  4368. case RTL_GIGA_MAC_VER_49:
  4369. case RTL_GIGA_MAC_VER_50:
  4370. case RTL_GIGA_MAC_VER_51:
  4371. default:
  4372. ops->disable = NULL;
  4373. ops->enable = NULL;
  4374. break;
  4375. }
  4376. }
  4377. DECLARE_RTL_COND(rtl_chipcmd_cond)
  4378. {
  4379. void __iomem *ioaddr = tp->mmio_addr;
  4380. return RTL_R8(ChipCmd) & CmdReset;
  4381. }
  4382. static void rtl_hw_reset(struct rtl8169_private *tp)
  4383. {
  4384. void __iomem *ioaddr = tp->mmio_addr;
  4385. RTL_W8(ChipCmd, CmdReset);
  4386. rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
  4387. }
  4388. static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
  4389. {
  4390. struct rtl_fw *rtl_fw;
  4391. const char *name;
  4392. int rc = -ENOMEM;
  4393. name = rtl_lookup_firmware_name(tp);
  4394. if (!name)
  4395. goto out_no_firmware;
  4396. rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
  4397. if (!rtl_fw)
  4398. goto err_warn;
  4399. rc = reject_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
  4400. if (rc < 0)
  4401. goto err_free;
  4402. rc = rtl_check_firmware(tp, rtl_fw);
  4403. if (rc < 0)
  4404. goto err_release_firmware;
  4405. tp->rtl_fw = rtl_fw;
  4406. out:
  4407. return;
  4408. err_release_firmware:
  4409. release_firmware(rtl_fw->fw);
  4410. err_free:
  4411. kfree(rtl_fw);
  4412. err_warn:
  4413. netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
  4414. name, rc);
  4415. out_no_firmware:
  4416. tp->rtl_fw = NULL;
  4417. goto out;
  4418. }
  4419. static void rtl_request_firmware(struct rtl8169_private *tp)
  4420. {
  4421. if (IS_ERR(tp->rtl_fw))
  4422. rtl_request_uncached_firmware(tp);
  4423. }
  4424. static void rtl_rx_close(struct rtl8169_private *tp)
  4425. {
  4426. void __iomem *ioaddr = tp->mmio_addr;
  4427. RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
  4428. }
  4429. DECLARE_RTL_COND(rtl_npq_cond)
  4430. {
  4431. void __iomem *ioaddr = tp->mmio_addr;
  4432. return RTL_R8(TxPoll) & NPQ;
  4433. }
  4434. DECLARE_RTL_COND(rtl_txcfg_empty_cond)
  4435. {
  4436. void __iomem *ioaddr = tp->mmio_addr;
  4437. return RTL_R32(TxConfig) & TXCFG_EMPTY;
  4438. }
  4439. static void rtl8169_hw_reset(struct rtl8169_private *tp)
  4440. {
  4441. void __iomem *ioaddr = tp->mmio_addr;
  4442. /* Disable interrupts */
  4443. rtl8169_irq_mask_and_ack(tp);
  4444. rtl_rx_close(tp);
  4445. if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  4446. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  4447. tp->mac_version == RTL_GIGA_MAC_VER_31) {
  4448. rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
  4449. } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
  4450. tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  4451. tp->mac_version == RTL_GIGA_MAC_VER_36 ||
  4452. tp->mac_version == RTL_GIGA_MAC_VER_37 ||
  4453. tp->mac_version == RTL_GIGA_MAC_VER_38 ||
  4454. tp->mac_version == RTL_GIGA_MAC_VER_40 ||
  4455. tp->mac_version == RTL_GIGA_MAC_VER_41 ||
  4456. tp->mac_version == RTL_GIGA_MAC_VER_42 ||
  4457. tp->mac_version == RTL_GIGA_MAC_VER_43 ||
  4458. tp->mac_version == RTL_GIGA_MAC_VER_44 ||
  4459. tp->mac_version == RTL_GIGA_MAC_VER_45 ||
  4460. tp->mac_version == RTL_GIGA_MAC_VER_46 ||
  4461. tp->mac_version == RTL_GIGA_MAC_VER_47 ||
  4462. tp->mac_version == RTL_GIGA_MAC_VER_48 ||
  4463. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  4464. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  4465. tp->mac_version == RTL_GIGA_MAC_VER_51) {
  4466. RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
  4467. rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
  4468. } else {
  4469. RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
  4470. udelay(100);
  4471. }
  4472. rtl_hw_reset(tp);
  4473. }
  4474. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  4475. {
  4476. void __iomem *ioaddr = tp->mmio_addr;
  4477. /* Set DMA burst size and Interframe Gap Time */
  4478. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  4479. (InterFrameGap << TxInterFrameGapShift));
  4480. }
  4481. static void rtl_hw_start(struct net_device *dev)
  4482. {
  4483. struct rtl8169_private *tp = netdev_priv(dev);
  4484. tp->hw_start(dev);
  4485. rtl_irq_enable_all(tp);
  4486. }
  4487. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  4488. void __iomem *ioaddr)
  4489. {
  4490. /*
  4491. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  4492. * register to be written before TxDescAddrLow to work.
  4493. * Switching from MMIO to I/O access fixes the issue as well.
  4494. */
  4495. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  4496. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  4497. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  4498. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  4499. }
  4500. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  4501. {
  4502. u16 cmd;
  4503. cmd = RTL_R16(CPlusCmd);
  4504. RTL_W16(CPlusCmd, cmd);
  4505. return cmd;
  4506. }
  4507. static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
  4508. {
  4509. /* Low hurts. Let's disable the filtering. */
  4510. RTL_W16(RxMaxSize, rx_buf_sz + 1);
  4511. }
  4512. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  4513. {
  4514. static const struct rtl_cfg2_info {
  4515. u32 mac_version;
  4516. u32 clk;
  4517. u32 val;
  4518. } cfg2_info [] = {
  4519. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  4520. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  4521. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  4522. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  4523. };
  4524. const struct rtl_cfg2_info *p = cfg2_info;
  4525. unsigned int i;
  4526. u32 clk;
  4527. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  4528. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  4529. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  4530. RTL_W32(0x7c, p->val);
  4531. break;
  4532. }
  4533. }
  4534. }
  4535. static void rtl_set_rx_mode(struct net_device *dev)
  4536. {
  4537. struct rtl8169_private *tp = netdev_priv(dev);
  4538. void __iomem *ioaddr = tp->mmio_addr;
  4539. u32 mc_filter[2]; /* Multicast hash filter */
  4540. int rx_mode;
  4541. u32 tmp = 0;
  4542. if (dev->flags & IFF_PROMISC) {
  4543. /* Unconditionally log net taps. */
  4544. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  4545. rx_mode =
  4546. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  4547. AcceptAllPhys;
  4548. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4549. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  4550. (dev->flags & IFF_ALLMULTI)) {
  4551. /* Too many to filter perfectly -- accept all multicasts. */
  4552. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  4553. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4554. } else {
  4555. struct netdev_hw_addr *ha;
  4556. rx_mode = AcceptBroadcast | AcceptMyPhys;
  4557. mc_filter[1] = mc_filter[0] = 0;
  4558. netdev_for_each_mc_addr(ha, dev) {
  4559. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  4560. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  4561. rx_mode |= AcceptMulticast;
  4562. }
  4563. }
  4564. if (dev->features & NETIF_F_RXALL)
  4565. rx_mode |= (AcceptErr | AcceptRunt);
  4566. tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
  4567. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  4568. u32 data = mc_filter[0];
  4569. mc_filter[0] = swab32(mc_filter[1]);
  4570. mc_filter[1] = swab32(data);
  4571. }
  4572. if (tp->mac_version == RTL_GIGA_MAC_VER_35)
  4573. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4574. RTL_W32(MAR0 + 4, mc_filter[1]);
  4575. RTL_W32(MAR0 + 0, mc_filter[0]);
  4576. RTL_W32(RxConfig, tmp);
  4577. }
  4578. static void rtl_hw_start_8169(struct net_device *dev)
  4579. {
  4580. struct rtl8169_private *tp = netdev_priv(dev);
  4581. void __iomem *ioaddr = tp->mmio_addr;
  4582. struct pci_dev *pdev = tp->pci_dev;
  4583. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  4584. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  4585. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  4586. }
  4587. RTL_W8(Cfg9346, Cfg9346_Unlock);
  4588. if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
  4589. tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  4590. tp->mac_version == RTL_GIGA_MAC_VER_03 ||
  4591. tp->mac_version == RTL_GIGA_MAC_VER_04)
  4592. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  4593. rtl_init_rxcfg(tp);
  4594. RTL_W8(EarlyTxThres, NoEarlyTx);
  4595. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  4596. if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
  4597. tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  4598. tp->mac_version == RTL_GIGA_MAC_VER_03 ||
  4599. tp->mac_version == RTL_GIGA_MAC_VER_04)
  4600. rtl_set_rx_tx_config_registers(tp);
  4601. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  4602. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  4603. tp->mac_version == RTL_GIGA_MAC_VER_03) {
  4604. dprintk("Set MAC Reg C+CR Offset 0xe0. "
  4605. "Bit-3 and bit-14 MUST be 1\n");
  4606. tp->cp_cmd |= (1 << 14);
  4607. }
  4608. RTL_W16(CPlusCmd, tp->cp_cmd);
  4609. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  4610. /*
  4611. * Undocumented corner. Supposedly:
  4612. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  4613. */
  4614. RTL_W16(IntrMitigate, 0x0000);
  4615. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  4616. if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
  4617. tp->mac_version != RTL_GIGA_MAC_VER_02 &&
  4618. tp->mac_version != RTL_GIGA_MAC_VER_03 &&
  4619. tp->mac_version != RTL_GIGA_MAC_VER_04) {
  4620. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  4621. rtl_set_rx_tx_config_registers(tp);
  4622. }
  4623. RTL_W8(Cfg9346, Cfg9346_Lock);
  4624. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  4625. RTL_R8(IntrMask);
  4626. RTL_W32(RxMissed, 0);
  4627. rtl_set_rx_mode(dev);
  4628. /* no early-rx interrupts */
  4629. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  4630. }
  4631. static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
  4632. {
  4633. if (tp->csi_ops.write)
  4634. tp->csi_ops.write(tp, addr, value);
  4635. }
  4636. static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
  4637. {
  4638. return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
  4639. }
  4640. static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
  4641. {
  4642. u32 csi;
  4643. csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
  4644. rtl_csi_write(tp, 0x070c, csi | bits);
  4645. }
  4646. static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
  4647. {
  4648. rtl_csi_access_enable(tp, 0x17000000);
  4649. }
  4650. static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
  4651. {
  4652. rtl_csi_access_enable(tp, 0x27000000);
  4653. }
  4654. DECLARE_RTL_COND(rtl_csiar_cond)
  4655. {
  4656. void __iomem *ioaddr = tp->mmio_addr;
  4657. return RTL_R32(CSIAR) & CSIAR_FLAG;
  4658. }
  4659. static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
  4660. {
  4661. void __iomem *ioaddr = tp->mmio_addr;
  4662. RTL_W32(CSIDR, value);
  4663. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  4664. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  4665. rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
  4666. }
  4667. static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
  4668. {
  4669. void __iomem *ioaddr = tp->mmio_addr;
  4670. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  4671. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  4672. return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
  4673. RTL_R32(CSIDR) : ~0;
  4674. }
  4675. static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
  4676. {
  4677. void __iomem *ioaddr = tp->mmio_addr;
  4678. RTL_W32(CSIDR, value);
  4679. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  4680. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
  4681. CSIAR_FUNC_NIC);
  4682. rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
  4683. }
  4684. static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
  4685. {
  4686. void __iomem *ioaddr = tp->mmio_addr;
  4687. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
  4688. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  4689. return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
  4690. RTL_R32(CSIDR) : ~0;
  4691. }
  4692. static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
  4693. {
  4694. void __iomem *ioaddr = tp->mmio_addr;
  4695. RTL_W32(CSIDR, value);
  4696. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  4697. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
  4698. CSIAR_FUNC_NIC2);
  4699. rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
  4700. }
  4701. static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
  4702. {
  4703. void __iomem *ioaddr = tp->mmio_addr;
  4704. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
  4705. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  4706. return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
  4707. RTL_R32(CSIDR) : ~0;
  4708. }
  4709. static void rtl_init_csi_ops(struct rtl8169_private *tp)
  4710. {
  4711. struct csi_ops *ops = &tp->csi_ops;
  4712. switch (tp->mac_version) {
  4713. case RTL_GIGA_MAC_VER_01:
  4714. case RTL_GIGA_MAC_VER_02:
  4715. case RTL_GIGA_MAC_VER_03:
  4716. case RTL_GIGA_MAC_VER_04:
  4717. case RTL_GIGA_MAC_VER_05:
  4718. case RTL_GIGA_MAC_VER_06:
  4719. case RTL_GIGA_MAC_VER_10:
  4720. case RTL_GIGA_MAC_VER_11:
  4721. case RTL_GIGA_MAC_VER_12:
  4722. case RTL_GIGA_MAC_VER_13:
  4723. case RTL_GIGA_MAC_VER_14:
  4724. case RTL_GIGA_MAC_VER_15:
  4725. case RTL_GIGA_MAC_VER_16:
  4726. case RTL_GIGA_MAC_VER_17:
  4727. ops->write = NULL;
  4728. ops->read = NULL;
  4729. break;
  4730. case RTL_GIGA_MAC_VER_37:
  4731. case RTL_GIGA_MAC_VER_38:
  4732. ops->write = r8402_csi_write;
  4733. ops->read = r8402_csi_read;
  4734. break;
  4735. case RTL_GIGA_MAC_VER_44:
  4736. ops->write = r8411_csi_write;
  4737. ops->read = r8411_csi_read;
  4738. break;
  4739. default:
  4740. ops->write = r8169_csi_write;
  4741. ops->read = r8169_csi_read;
  4742. break;
  4743. }
  4744. }
  4745. struct ephy_info {
  4746. unsigned int offset;
  4747. u16 mask;
  4748. u16 bits;
  4749. };
  4750. static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
  4751. int len)
  4752. {
  4753. u16 w;
  4754. while (len-- > 0) {
  4755. w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
  4756. rtl_ephy_write(tp, e->offset, w);
  4757. e++;
  4758. }
  4759. }
  4760. static void rtl_disable_clock_request(struct pci_dev *pdev)
  4761. {
  4762. pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
  4763. PCI_EXP_LNKCTL_CLKREQ_EN);
  4764. }
  4765. static void rtl_enable_clock_request(struct pci_dev *pdev)
  4766. {
  4767. pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
  4768. PCI_EXP_LNKCTL_CLKREQ_EN);
  4769. }
  4770. static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
  4771. {
  4772. void __iomem *ioaddr = tp->mmio_addr;
  4773. u8 data;
  4774. data = RTL_R8(Config3);
  4775. if (enable)
  4776. data |= Rdy_to_L23;
  4777. else
  4778. data &= ~Rdy_to_L23;
  4779. RTL_W8(Config3, data);
  4780. }
  4781. #define R8168_CPCMD_QUIRK_MASK (\
  4782. EnableBist | \
  4783. Mac_dbgo_oe | \
  4784. Force_half_dup | \
  4785. Force_rxflow_en | \
  4786. Force_txflow_en | \
  4787. Cxpl_dbg_sel | \
  4788. ASF | \
  4789. PktCntrDisable | \
  4790. Mac_dbgo_sel)
  4791. static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
  4792. {
  4793. void __iomem *ioaddr = tp->mmio_addr;
  4794. struct pci_dev *pdev = tp->pci_dev;
  4795. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4796. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4797. if (tp->dev->mtu <= ETH_DATA_LEN) {
  4798. rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
  4799. PCI_EXP_DEVCTL_NOSNOOP_EN);
  4800. }
  4801. }
  4802. static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
  4803. {
  4804. void __iomem *ioaddr = tp->mmio_addr;
  4805. rtl_hw_start_8168bb(tp);
  4806. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4807. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  4808. }
  4809. static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
  4810. {
  4811. void __iomem *ioaddr = tp->mmio_addr;
  4812. struct pci_dev *pdev = tp->pci_dev;
  4813. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  4814. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4815. if (tp->dev->mtu <= ETH_DATA_LEN)
  4816. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4817. rtl_disable_clock_request(pdev);
  4818. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4819. }
  4820. static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
  4821. {
  4822. static const struct ephy_info e_info_8168cp[] = {
  4823. { 0x01, 0, 0x0001 },
  4824. { 0x02, 0x0800, 0x1000 },
  4825. { 0x03, 0, 0x0042 },
  4826. { 0x06, 0x0080, 0x0000 },
  4827. { 0x07, 0, 0x2000 }
  4828. };
  4829. rtl_csi_access_enable_2(tp);
  4830. rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  4831. __rtl_hw_start_8168cp(tp);
  4832. }
  4833. static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
  4834. {
  4835. void __iomem *ioaddr = tp->mmio_addr;
  4836. struct pci_dev *pdev = tp->pci_dev;
  4837. rtl_csi_access_enable_2(tp);
  4838. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4839. if (tp->dev->mtu <= ETH_DATA_LEN)
  4840. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4841. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4842. }
  4843. static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
  4844. {
  4845. void __iomem *ioaddr = tp->mmio_addr;
  4846. struct pci_dev *pdev = tp->pci_dev;
  4847. rtl_csi_access_enable_2(tp);
  4848. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4849. /* Magic. */
  4850. RTL_W8(DBG_REG, 0x20);
  4851. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4852. if (tp->dev->mtu <= ETH_DATA_LEN)
  4853. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4854. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4855. }
  4856. static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
  4857. {
  4858. void __iomem *ioaddr = tp->mmio_addr;
  4859. static const struct ephy_info e_info_8168c_1[] = {
  4860. { 0x02, 0x0800, 0x1000 },
  4861. { 0x03, 0, 0x0002 },
  4862. { 0x06, 0x0080, 0x0000 }
  4863. };
  4864. rtl_csi_access_enable_2(tp);
  4865. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  4866. rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  4867. __rtl_hw_start_8168cp(tp);
  4868. }
  4869. static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
  4870. {
  4871. static const struct ephy_info e_info_8168c_2[] = {
  4872. { 0x01, 0, 0x0001 },
  4873. { 0x03, 0x0400, 0x0220 }
  4874. };
  4875. rtl_csi_access_enable_2(tp);
  4876. rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  4877. __rtl_hw_start_8168cp(tp);
  4878. }
  4879. static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
  4880. {
  4881. rtl_hw_start_8168c_2(tp);
  4882. }
  4883. static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
  4884. {
  4885. rtl_csi_access_enable_2(tp);
  4886. __rtl_hw_start_8168cp(tp);
  4887. }
  4888. static void rtl_hw_start_8168d(struct rtl8169_private *tp)
  4889. {
  4890. void __iomem *ioaddr = tp->mmio_addr;
  4891. struct pci_dev *pdev = tp->pci_dev;
  4892. rtl_csi_access_enable_2(tp);
  4893. rtl_disable_clock_request(pdev);
  4894. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4895. if (tp->dev->mtu <= ETH_DATA_LEN)
  4896. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4897. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4898. }
  4899. static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
  4900. {
  4901. void __iomem *ioaddr = tp->mmio_addr;
  4902. struct pci_dev *pdev = tp->pci_dev;
  4903. rtl_csi_access_enable_1(tp);
  4904. if (tp->dev->mtu <= ETH_DATA_LEN)
  4905. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4906. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4907. rtl_disable_clock_request(pdev);
  4908. }
  4909. static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
  4910. {
  4911. void __iomem *ioaddr = tp->mmio_addr;
  4912. struct pci_dev *pdev = tp->pci_dev;
  4913. static const struct ephy_info e_info_8168d_4[] = {
  4914. { 0x0b, 0x0000, 0x0048 },
  4915. { 0x19, 0x0020, 0x0050 },
  4916. { 0x0c, 0x0100, 0x0020 }
  4917. };
  4918. rtl_csi_access_enable_1(tp);
  4919. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4920. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4921. rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
  4922. rtl_enable_clock_request(pdev);
  4923. }
  4924. static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
  4925. {
  4926. void __iomem *ioaddr = tp->mmio_addr;
  4927. struct pci_dev *pdev = tp->pci_dev;
  4928. static const struct ephy_info e_info_8168e_1[] = {
  4929. { 0x00, 0x0200, 0x0100 },
  4930. { 0x00, 0x0000, 0x0004 },
  4931. { 0x06, 0x0002, 0x0001 },
  4932. { 0x06, 0x0000, 0x0030 },
  4933. { 0x07, 0x0000, 0x2000 },
  4934. { 0x00, 0x0000, 0x0020 },
  4935. { 0x03, 0x5800, 0x2000 },
  4936. { 0x03, 0x0000, 0x0001 },
  4937. { 0x01, 0x0800, 0x1000 },
  4938. { 0x07, 0x0000, 0x4000 },
  4939. { 0x1e, 0x0000, 0x2000 },
  4940. { 0x19, 0xffff, 0xfe6c },
  4941. { 0x0a, 0x0000, 0x0040 }
  4942. };
  4943. rtl_csi_access_enable_2(tp);
  4944. rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
  4945. if (tp->dev->mtu <= ETH_DATA_LEN)
  4946. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4947. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4948. rtl_disable_clock_request(pdev);
  4949. /* Reset tx FIFO pointer */
  4950. RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
  4951. RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
  4952. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  4953. }
  4954. static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
  4955. {
  4956. void __iomem *ioaddr = tp->mmio_addr;
  4957. struct pci_dev *pdev = tp->pci_dev;
  4958. static const struct ephy_info e_info_8168e_2[] = {
  4959. { 0x09, 0x0000, 0x0080 },
  4960. { 0x19, 0x0000, 0x0224 }
  4961. };
  4962. rtl_csi_access_enable_1(tp);
  4963. rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
  4964. if (tp->dev->mtu <= ETH_DATA_LEN)
  4965. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4966. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4967. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4968. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  4969. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4970. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  4971. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
  4972. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4973. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
  4974. RTL_W8(MaxTxPacketSize, EarlySize);
  4975. rtl_disable_clock_request(pdev);
  4976. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  4977. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  4978. /* Adjust EEE LED frequency */
  4979. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  4980. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  4981. RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
  4982. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  4983. }
  4984. static void rtl_hw_start_8168f(struct rtl8169_private *tp)
  4985. {
  4986. void __iomem *ioaddr = tp->mmio_addr;
  4987. struct pci_dev *pdev = tp->pci_dev;
  4988. rtl_csi_access_enable_2(tp);
  4989. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4990. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4991. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4992. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  4993. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4994. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  4995. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  4996. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4997. rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4998. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  4999. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
  5000. RTL_W8(MaxTxPacketSize, EarlySize);
  5001. rtl_disable_clock_request(pdev);
  5002. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5003. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  5004. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  5005. RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
  5006. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  5007. }
  5008. static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
  5009. {
  5010. void __iomem *ioaddr = tp->mmio_addr;
  5011. static const struct ephy_info e_info_8168f_1[] = {
  5012. { 0x06, 0x00c0, 0x0020 },
  5013. { 0x08, 0x0001, 0x0002 },
  5014. { 0x09, 0x0000, 0x0080 },
  5015. { 0x19, 0x0000, 0x0224 }
  5016. };
  5017. rtl_hw_start_8168f(tp);
  5018. rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
  5019. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
  5020. /* Adjust EEE LED frequency */
  5021. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  5022. }
  5023. static void rtl_hw_start_8411(struct rtl8169_private *tp)
  5024. {
  5025. static const struct ephy_info e_info_8168f_1[] = {
  5026. { 0x06, 0x00c0, 0x0020 },
  5027. { 0x0f, 0xffff, 0x5200 },
  5028. { 0x1e, 0x0000, 0x4000 },
  5029. { 0x19, 0x0000, 0x0224 }
  5030. };
  5031. rtl_hw_start_8168f(tp);
  5032. rtl_pcie_state_l2l3_enable(tp, false);
  5033. rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
  5034. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
  5035. }
  5036. static void rtl_hw_start_8168g(struct rtl8169_private *tp)
  5037. {
  5038. void __iomem *ioaddr = tp->mmio_addr;
  5039. struct pci_dev *pdev = tp->pci_dev;
  5040. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5041. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
  5042. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
  5043. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
  5044. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  5045. rtl_csi_access_enable_1(tp);
  5046. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5047. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  5048. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  5049. rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
  5050. RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
  5051. RTL_W8(MaxTxPacketSize, EarlySize);
  5052. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5053. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5054. /* Adjust EEE LED frequency */
  5055. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  5056. rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
  5057. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
  5058. rtl_pcie_state_l2l3_enable(tp, false);
  5059. }
  5060. static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
  5061. {
  5062. void __iomem *ioaddr = tp->mmio_addr;
  5063. static const struct ephy_info e_info_8168g_1[] = {
  5064. { 0x00, 0x0000, 0x0008 },
  5065. { 0x0c, 0x37d0, 0x0820 },
  5066. { 0x1e, 0x0000, 0x0001 },
  5067. { 0x19, 0x8000, 0x0000 }
  5068. };
  5069. rtl_hw_start_8168g(tp);
  5070. /* disable aspm and clock request before access ephy */
  5071. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5072. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5073. rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
  5074. }
  5075. static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
  5076. {
  5077. void __iomem *ioaddr = tp->mmio_addr;
  5078. static const struct ephy_info e_info_8168g_2[] = {
  5079. { 0x00, 0x0000, 0x0008 },
  5080. { 0x0c, 0x3df0, 0x0200 },
  5081. { 0x19, 0xffff, 0xfc00 },
  5082. { 0x1e, 0xffff, 0x20eb }
  5083. };
  5084. rtl_hw_start_8168g(tp);
  5085. /* disable aspm and clock request before access ephy */
  5086. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5087. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5088. rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
  5089. }
  5090. static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
  5091. {
  5092. void __iomem *ioaddr = tp->mmio_addr;
  5093. static const struct ephy_info e_info_8411_2[] = {
  5094. { 0x00, 0x0000, 0x0008 },
  5095. { 0x0c, 0x3df0, 0x0200 },
  5096. { 0x0f, 0xffff, 0x5200 },
  5097. { 0x19, 0x0020, 0x0000 },
  5098. { 0x1e, 0x0000, 0x2000 }
  5099. };
  5100. rtl_hw_start_8168g(tp);
  5101. /* disable aspm and clock request before access ephy */
  5102. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5103. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5104. rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
  5105. }
  5106. static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
  5107. {
  5108. void __iomem *ioaddr = tp->mmio_addr;
  5109. struct pci_dev *pdev = tp->pci_dev;
  5110. int rg_saw_cnt;
  5111. u32 data;
  5112. static const struct ephy_info e_info_8168h_1[] = {
  5113. { 0x1e, 0x0800, 0x0001 },
  5114. { 0x1d, 0x0000, 0x0800 },
  5115. { 0x05, 0xffff, 0x2089 },
  5116. { 0x06, 0xffff, 0x5881 },
  5117. { 0x04, 0xffff, 0x154a },
  5118. { 0x01, 0xffff, 0x068b }
  5119. };
  5120. /* disable aspm and clock request before access ephy */
  5121. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5122. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5123. rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
  5124. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5125. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
  5126. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
  5127. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
  5128. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  5129. rtl_csi_access_enable_1(tp);
  5130. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5131. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  5132. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  5133. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
  5134. rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
  5135. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
  5136. RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
  5137. RTL_W8(MaxTxPacketSize, EarlySize);
  5138. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5139. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5140. /* Adjust EEE LED frequency */
  5141. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  5142. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
  5143. RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
  5144. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
  5145. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
  5146. rtl_pcie_state_l2l3_enable(tp, false);
  5147. rtl_writephy(tp, 0x1f, 0x0c42);
  5148. rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
  5149. rtl_writephy(tp, 0x1f, 0x0000);
  5150. if (rg_saw_cnt > 0) {
  5151. u16 sw_cnt_1ms_ini;
  5152. sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
  5153. sw_cnt_1ms_ini &= 0x0fff;
  5154. data = r8168_mac_ocp_read(tp, 0xd412);
  5155. data &= ~0x0fff;
  5156. data |= sw_cnt_1ms_ini;
  5157. r8168_mac_ocp_write(tp, 0xd412, data);
  5158. }
  5159. data = r8168_mac_ocp_read(tp, 0xe056);
  5160. data &= ~0xf0;
  5161. data |= 0x70;
  5162. r8168_mac_ocp_write(tp, 0xe056, data);
  5163. data = r8168_mac_ocp_read(tp, 0xe052);
  5164. data &= ~0x6000;
  5165. data |= 0x8008;
  5166. r8168_mac_ocp_write(tp, 0xe052, data);
  5167. data = r8168_mac_ocp_read(tp, 0xe0d6);
  5168. data &= ~0x01ff;
  5169. data |= 0x017f;
  5170. r8168_mac_ocp_write(tp, 0xe0d6, data);
  5171. data = r8168_mac_ocp_read(tp, 0xd420);
  5172. data &= ~0x0fff;
  5173. data |= 0x047f;
  5174. r8168_mac_ocp_write(tp, 0xd420, data);
  5175. r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
  5176. r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
  5177. r8168_mac_ocp_write(tp, 0xc094, 0x0000);
  5178. r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
  5179. }
  5180. static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
  5181. {
  5182. void __iomem *ioaddr = tp->mmio_addr;
  5183. struct pci_dev *pdev = tp->pci_dev;
  5184. rtl8168ep_stop_cmac(tp);
  5185. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5186. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
  5187. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
  5188. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
  5189. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  5190. rtl_csi_access_enable_1(tp);
  5191. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5192. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  5193. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  5194. rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
  5195. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
  5196. RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
  5197. RTL_W8(MaxTxPacketSize, EarlySize);
  5198. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5199. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5200. /* Adjust EEE LED frequency */
  5201. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  5202. rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
  5203. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
  5204. rtl_pcie_state_l2l3_enable(tp, false);
  5205. }
  5206. static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
  5207. {
  5208. void __iomem *ioaddr = tp->mmio_addr;
  5209. static const struct ephy_info e_info_8168ep_1[] = {
  5210. { 0x00, 0xffff, 0x10ab },
  5211. { 0x06, 0xffff, 0xf030 },
  5212. { 0x08, 0xffff, 0x2006 },
  5213. { 0x0d, 0xffff, 0x1666 },
  5214. { 0x0c, 0x3ff0, 0x0000 }
  5215. };
  5216. /* disable aspm and clock request before access ephy */
  5217. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5218. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5219. rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
  5220. rtl_hw_start_8168ep(tp);
  5221. }
  5222. static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
  5223. {
  5224. void __iomem *ioaddr = tp->mmio_addr;
  5225. static const struct ephy_info e_info_8168ep_2[] = {
  5226. { 0x00, 0xffff, 0x10a3 },
  5227. { 0x19, 0xffff, 0xfc00 },
  5228. { 0x1e, 0xffff, 0x20ea }
  5229. };
  5230. /* disable aspm and clock request before access ephy */
  5231. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5232. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5233. rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
  5234. rtl_hw_start_8168ep(tp);
  5235. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
  5236. RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
  5237. }
  5238. static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
  5239. {
  5240. void __iomem *ioaddr = tp->mmio_addr;
  5241. u32 data;
  5242. static const struct ephy_info e_info_8168ep_3[] = {
  5243. { 0x00, 0xffff, 0x10a3 },
  5244. { 0x19, 0xffff, 0x7c00 },
  5245. { 0x1e, 0xffff, 0x20eb },
  5246. { 0x0d, 0xffff, 0x1666 }
  5247. };
  5248. /* disable aspm and clock request before access ephy */
  5249. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5250. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5251. rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
  5252. rtl_hw_start_8168ep(tp);
  5253. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
  5254. RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
  5255. data = r8168_mac_ocp_read(tp, 0xd3e2);
  5256. data &= 0xf000;
  5257. data |= 0x0271;
  5258. r8168_mac_ocp_write(tp, 0xd3e2, data);
  5259. data = r8168_mac_ocp_read(tp, 0xd3e4);
  5260. data &= 0xff00;
  5261. r8168_mac_ocp_write(tp, 0xd3e4, data);
  5262. data = r8168_mac_ocp_read(tp, 0xe860);
  5263. data |= 0x0080;
  5264. r8168_mac_ocp_write(tp, 0xe860, data);
  5265. }
  5266. static void rtl_hw_start_8168(struct net_device *dev)
  5267. {
  5268. struct rtl8169_private *tp = netdev_priv(dev);
  5269. void __iomem *ioaddr = tp->mmio_addr;
  5270. RTL_W8(Cfg9346, Cfg9346_Unlock);
  5271. RTL_W8(MaxTxPacketSize, TxPacketMax);
  5272. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  5273. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  5274. RTL_W16(CPlusCmd, tp->cp_cmd);
  5275. RTL_W16(IntrMitigate, 0x5151);
  5276. /* Work around for RxFIFO overflow. */
  5277. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  5278. tp->event_slow |= RxFIFOOver | PCSTimeout;
  5279. tp->event_slow &= ~RxOverflow;
  5280. }
  5281. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  5282. rtl_set_rx_tx_config_registers(tp);
  5283. RTL_R8(IntrMask);
  5284. switch (tp->mac_version) {
  5285. case RTL_GIGA_MAC_VER_11:
  5286. rtl_hw_start_8168bb(tp);
  5287. break;
  5288. case RTL_GIGA_MAC_VER_12:
  5289. case RTL_GIGA_MAC_VER_17:
  5290. rtl_hw_start_8168bef(tp);
  5291. break;
  5292. case RTL_GIGA_MAC_VER_18:
  5293. rtl_hw_start_8168cp_1(tp);
  5294. break;
  5295. case RTL_GIGA_MAC_VER_19:
  5296. rtl_hw_start_8168c_1(tp);
  5297. break;
  5298. case RTL_GIGA_MAC_VER_20:
  5299. rtl_hw_start_8168c_2(tp);
  5300. break;
  5301. case RTL_GIGA_MAC_VER_21:
  5302. rtl_hw_start_8168c_3(tp);
  5303. break;
  5304. case RTL_GIGA_MAC_VER_22:
  5305. rtl_hw_start_8168c_4(tp);
  5306. break;
  5307. case RTL_GIGA_MAC_VER_23:
  5308. rtl_hw_start_8168cp_2(tp);
  5309. break;
  5310. case RTL_GIGA_MAC_VER_24:
  5311. rtl_hw_start_8168cp_3(tp);
  5312. break;
  5313. case RTL_GIGA_MAC_VER_25:
  5314. case RTL_GIGA_MAC_VER_26:
  5315. case RTL_GIGA_MAC_VER_27:
  5316. rtl_hw_start_8168d(tp);
  5317. break;
  5318. case RTL_GIGA_MAC_VER_28:
  5319. rtl_hw_start_8168d_4(tp);
  5320. break;
  5321. case RTL_GIGA_MAC_VER_31:
  5322. rtl_hw_start_8168dp(tp);
  5323. break;
  5324. case RTL_GIGA_MAC_VER_32:
  5325. case RTL_GIGA_MAC_VER_33:
  5326. rtl_hw_start_8168e_1(tp);
  5327. break;
  5328. case RTL_GIGA_MAC_VER_34:
  5329. rtl_hw_start_8168e_2(tp);
  5330. break;
  5331. case RTL_GIGA_MAC_VER_35:
  5332. case RTL_GIGA_MAC_VER_36:
  5333. rtl_hw_start_8168f_1(tp);
  5334. break;
  5335. case RTL_GIGA_MAC_VER_38:
  5336. rtl_hw_start_8411(tp);
  5337. break;
  5338. case RTL_GIGA_MAC_VER_40:
  5339. case RTL_GIGA_MAC_VER_41:
  5340. rtl_hw_start_8168g_1(tp);
  5341. break;
  5342. case RTL_GIGA_MAC_VER_42:
  5343. rtl_hw_start_8168g_2(tp);
  5344. break;
  5345. case RTL_GIGA_MAC_VER_44:
  5346. rtl_hw_start_8411_2(tp);
  5347. break;
  5348. case RTL_GIGA_MAC_VER_45:
  5349. case RTL_GIGA_MAC_VER_46:
  5350. rtl_hw_start_8168h_1(tp);
  5351. break;
  5352. case RTL_GIGA_MAC_VER_49:
  5353. rtl_hw_start_8168ep_1(tp);
  5354. break;
  5355. case RTL_GIGA_MAC_VER_50:
  5356. rtl_hw_start_8168ep_2(tp);
  5357. break;
  5358. case RTL_GIGA_MAC_VER_51:
  5359. rtl_hw_start_8168ep_3(tp);
  5360. break;
  5361. default:
  5362. printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
  5363. dev->name, tp->mac_version);
  5364. break;
  5365. }
  5366. RTL_W8(Cfg9346, Cfg9346_Lock);
  5367. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  5368. rtl_set_rx_mode(dev);
  5369. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  5370. }
  5371. #define R810X_CPCMD_QUIRK_MASK (\
  5372. EnableBist | \
  5373. Mac_dbgo_oe | \
  5374. Force_half_dup | \
  5375. Force_rxflow_en | \
  5376. Force_txflow_en | \
  5377. Cxpl_dbg_sel | \
  5378. ASF | \
  5379. PktCntrDisable | \
  5380. Mac_dbgo_sel)
  5381. static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
  5382. {
  5383. void __iomem *ioaddr = tp->mmio_addr;
  5384. struct pci_dev *pdev = tp->pci_dev;
  5385. static const struct ephy_info e_info_8102e_1[] = {
  5386. { 0x01, 0, 0x6e65 },
  5387. { 0x02, 0, 0x091f },
  5388. { 0x03, 0, 0xc2f9 },
  5389. { 0x06, 0, 0xafb5 },
  5390. { 0x07, 0, 0x0e00 },
  5391. { 0x19, 0, 0xec80 },
  5392. { 0x01, 0, 0x2e65 },
  5393. { 0x01, 0, 0x6e65 }
  5394. };
  5395. u8 cfg1;
  5396. rtl_csi_access_enable_2(tp);
  5397. RTL_W8(DBG_REG, FIX_NAK_1);
  5398. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5399. RTL_W8(Config1,
  5400. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  5401. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  5402. cfg1 = RTL_R8(Config1);
  5403. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  5404. RTL_W8(Config1, cfg1 & ~LEDS0);
  5405. rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  5406. }
  5407. static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
  5408. {
  5409. void __iomem *ioaddr = tp->mmio_addr;
  5410. struct pci_dev *pdev = tp->pci_dev;
  5411. rtl_csi_access_enable_2(tp);
  5412. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5413. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  5414. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  5415. }
  5416. static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
  5417. {
  5418. rtl_hw_start_8102e_2(tp);
  5419. rtl_ephy_write(tp, 0x03, 0xc2f9);
  5420. }
  5421. static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
  5422. {
  5423. void __iomem *ioaddr = tp->mmio_addr;
  5424. static const struct ephy_info e_info_8105e_1[] = {
  5425. { 0x07, 0, 0x4000 },
  5426. { 0x19, 0, 0x0200 },
  5427. { 0x19, 0, 0x0020 },
  5428. { 0x1e, 0, 0x2000 },
  5429. { 0x03, 0, 0x0001 },
  5430. { 0x19, 0, 0x0100 },
  5431. { 0x19, 0, 0x0004 },
  5432. { 0x0a, 0, 0x0020 }
  5433. };
  5434. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  5435. RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
  5436. /* Disable Early Tally Counter */
  5437. RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
  5438. RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
  5439. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  5440. rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
  5441. rtl_pcie_state_l2l3_enable(tp, false);
  5442. }
  5443. static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
  5444. {
  5445. rtl_hw_start_8105e_1(tp);
  5446. rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
  5447. }
  5448. static void rtl_hw_start_8402(struct rtl8169_private *tp)
  5449. {
  5450. void __iomem *ioaddr = tp->mmio_addr;
  5451. static const struct ephy_info e_info_8402[] = {
  5452. { 0x19, 0xffff, 0xff64 },
  5453. { 0x1e, 0, 0x4000 }
  5454. };
  5455. rtl_csi_access_enable_2(tp);
  5456. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  5457. RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
  5458. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5459. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  5460. rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
  5461. rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5462. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
  5463. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
  5464. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  5465. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  5466. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5467. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5468. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
  5469. rtl_pcie_state_l2l3_enable(tp, false);
  5470. }
  5471. static void rtl_hw_start_8106(struct rtl8169_private *tp)
  5472. {
  5473. void __iomem *ioaddr = tp->mmio_addr;
  5474. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  5475. RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
  5476. RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
  5477. RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
  5478. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
  5479. rtl_pcie_state_l2l3_enable(tp, false);
  5480. }
  5481. static void rtl_hw_start_8101(struct net_device *dev)
  5482. {
  5483. struct rtl8169_private *tp = netdev_priv(dev);
  5484. void __iomem *ioaddr = tp->mmio_addr;
  5485. struct pci_dev *pdev = tp->pci_dev;
  5486. if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
  5487. tp->event_slow &= ~RxFIFOOver;
  5488. if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
  5489. tp->mac_version == RTL_GIGA_MAC_VER_16)
  5490. pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
  5491. PCI_EXP_DEVCTL_NOSNOOP_EN);
  5492. RTL_W8(Cfg9346, Cfg9346_Unlock);
  5493. RTL_W8(MaxTxPacketSize, TxPacketMax);
  5494. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  5495. tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
  5496. RTL_W16(CPlusCmd, tp->cp_cmd);
  5497. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  5498. rtl_set_rx_tx_config_registers(tp);
  5499. switch (tp->mac_version) {
  5500. case RTL_GIGA_MAC_VER_07:
  5501. rtl_hw_start_8102e_1(tp);
  5502. break;
  5503. case RTL_GIGA_MAC_VER_08:
  5504. rtl_hw_start_8102e_3(tp);
  5505. break;
  5506. case RTL_GIGA_MAC_VER_09:
  5507. rtl_hw_start_8102e_2(tp);
  5508. break;
  5509. case RTL_GIGA_MAC_VER_29:
  5510. rtl_hw_start_8105e_1(tp);
  5511. break;
  5512. case RTL_GIGA_MAC_VER_30:
  5513. rtl_hw_start_8105e_2(tp);
  5514. break;
  5515. case RTL_GIGA_MAC_VER_37:
  5516. rtl_hw_start_8402(tp);
  5517. break;
  5518. case RTL_GIGA_MAC_VER_39:
  5519. rtl_hw_start_8106(tp);
  5520. break;
  5521. case RTL_GIGA_MAC_VER_43:
  5522. rtl_hw_start_8168g_2(tp);
  5523. break;
  5524. case RTL_GIGA_MAC_VER_47:
  5525. case RTL_GIGA_MAC_VER_48:
  5526. rtl_hw_start_8168h_1(tp);
  5527. break;
  5528. }
  5529. RTL_W8(Cfg9346, Cfg9346_Lock);
  5530. RTL_W16(IntrMitigate, 0x0000);
  5531. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  5532. rtl_set_rx_mode(dev);
  5533. RTL_R8(IntrMask);
  5534. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  5535. }
  5536. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  5537. {
  5538. struct rtl8169_private *tp = netdev_priv(dev);
  5539. if (new_mtu < ETH_ZLEN ||
  5540. new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
  5541. return -EINVAL;
  5542. if (new_mtu > ETH_DATA_LEN)
  5543. rtl_hw_jumbo_enable(tp);
  5544. else
  5545. rtl_hw_jumbo_disable(tp);
  5546. dev->mtu = new_mtu;
  5547. netdev_update_features(dev);
  5548. return 0;
  5549. }
  5550. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  5551. {
  5552. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  5553. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  5554. }
  5555. static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
  5556. void **data_buff, struct RxDesc *desc)
  5557. {
  5558. dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
  5559. DMA_FROM_DEVICE);
  5560. kfree(*data_buff);
  5561. *data_buff = NULL;
  5562. rtl8169_make_unusable_by_asic(desc);
  5563. }
  5564. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  5565. {
  5566. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  5567. /* Force memory writes to complete before releasing descriptor */
  5568. dma_wmb();
  5569. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  5570. }
  5571. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  5572. u32 rx_buf_sz)
  5573. {
  5574. desc->addr = cpu_to_le64(mapping);
  5575. rtl8169_mark_to_asic(desc, rx_buf_sz);
  5576. }
  5577. static inline void *rtl8169_align(void *data)
  5578. {
  5579. return (void *)ALIGN((long)data, 16);
  5580. }
  5581. static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  5582. struct RxDesc *desc)
  5583. {
  5584. void *data;
  5585. dma_addr_t mapping;
  5586. struct device *d = &tp->pci_dev->dev;
  5587. struct net_device *dev = tp->dev;
  5588. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  5589. data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  5590. if (!data)
  5591. return NULL;
  5592. if (rtl8169_align(data) != data) {
  5593. kfree(data);
  5594. data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
  5595. if (!data)
  5596. return NULL;
  5597. }
  5598. mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
  5599. DMA_FROM_DEVICE);
  5600. if (unlikely(dma_mapping_error(d, mapping))) {
  5601. if (net_ratelimit())
  5602. netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
  5603. goto err_out;
  5604. }
  5605. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  5606. return data;
  5607. err_out:
  5608. kfree(data);
  5609. return NULL;
  5610. }
  5611. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  5612. {
  5613. unsigned int i;
  5614. for (i = 0; i < NUM_RX_DESC; i++) {
  5615. if (tp->Rx_databuff[i]) {
  5616. rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
  5617. tp->RxDescArray + i);
  5618. }
  5619. }
  5620. }
  5621. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  5622. {
  5623. desc->opts1 |= cpu_to_le32(RingEnd);
  5624. }
  5625. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  5626. {
  5627. unsigned int i;
  5628. for (i = 0; i < NUM_RX_DESC; i++) {
  5629. void *data;
  5630. if (tp->Rx_databuff[i])
  5631. continue;
  5632. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  5633. if (!data) {
  5634. rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
  5635. goto err_out;
  5636. }
  5637. tp->Rx_databuff[i] = data;
  5638. }
  5639. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  5640. return 0;
  5641. err_out:
  5642. rtl8169_rx_clear(tp);
  5643. return -ENOMEM;
  5644. }
  5645. static int rtl8169_init_ring(struct net_device *dev)
  5646. {
  5647. struct rtl8169_private *tp = netdev_priv(dev);
  5648. rtl8169_init_ring_indexes(tp);
  5649. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  5650. memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
  5651. return rtl8169_rx_fill(tp);
  5652. }
  5653. static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
  5654. struct TxDesc *desc)
  5655. {
  5656. unsigned int len = tx_skb->len;
  5657. dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
  5658. desc->opts1 = 0x00;
  5659. desc->opts2 = 0x00;
  5660. desc->addr = 0x00;
  5661. tx_skb->len = 0;
  5662. }
  5663. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  5664. unsigned int n)
  5665. {
  5666. unsigned int i;
  5667. for (i = 0; i < n; i++) {
  5668. unsigned int entry = (start + i) % NUM_TX_DESC;
  5669. struct ring_info *tx_skb = tp->tx_skb + entry;
  5670. unsigned int len = tx_skb->len;
  5671. if (len) {
  5672. struct sk_buff *skb = tx_skb->skb;
  5673. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  5674. tp->TxDescArray + entry);
  5675. if (skb) {
  5676. tp->dev->stats.tx_dropped++;
  5677. dev_kfree_skb_any(skb);
  5678. tx_skb->skb = NULL;
  5679. }
  5680. }
  5681. }
  5682. }
  5683. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  5684. {
  5685. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  5686. tp->cur_tx = tp->dirty_tx = 0;
  5687. }
  5688. static void rtl_reset_work(struct rtl8169_private *tp)
  5689. {
  5690. struct net_device *dev = tp->dev;
  5691. int i;
  5692. napi_disable(&tp->napi);
  5693. netif_stop_queue(dev);
  5694. synchronize_sched();
  5695. rtl8169_hw_reset(tp);
  5696. for (i = 0; i < NUM_RX_DESC; i++)
  5697. rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
  5698. rtl8169_tx_clear(tp);
  5699. rtl8169_init_ring_indexes(tp);
  5700. napi_enable(&tp->napi);
  5701. rtl_hw_start(dev);
  5702. netif_wake_queue(dev);
  5703. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  5704. }
  5705. static void rtl8169_tx_timeout(struct net_device *dev)
  5706. {
  5707. struct rtl8169_private *tp = netdev_priv(dev);
  5708. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5709. }
  5710. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  5711. u32 *opts)
  5712. {
  5713. struct skb_shared_info *info = skb_shinfo(skb);
  5714. unsigned int cur_frag, entry;
  5715. struct TxDesc *uninitialized_var(txd);
  5716. struct device *d = &tp->pci_dev->dev;
  5717. entry = tp->cur_tx;
  5718. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  5719. const skb_frag_t *frag = info->frags + cur_frag;
  5720. dma_addr_t mapping;
  5721. u32 status, len;
  5722. void *addr;
  5723. entry = (entry + 1) % NUM_TX_DESC;
  5724. txd = tp->TxDescArray + entry;
  5725. len = skb_frag_size(frag);
  5726. addr = skb_frag_address(frag);
  5727. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  5728. if (unlikely(dma_mapping_error(d, mapping))) {
  5729. if (net_ratelimit())
  5730. netif_err(tp, drv, tp->dev,
  5731. "Failed to map TX fragments DMA!\n");
  5732. goto err_out;
  5733. }
  5734. /* Anti gcc 2.95.3 bugware (sic) */
  5735. status = opts[0] | len |
  5736. (RingEnd * !((entry + 1) % NUM_TX_DESC));
  5737. txd->opts1 = cpu_to_le32(status);
  5738. txd->opts2 = cpu_to_le32(opts[1]);
  5739. txd->addr = cpu_to_le64(mapping);
  5740. tp->tx_skb[entry].len = len;
  5741. }
  5742. if (cur_frag) {
  5743. tp->tx_skb[entry].skb = skb;
  5744. txd->opts1 |= cpu_to_le32(LastFrag);
  5745. }
  5746. return cur_frag;
  5747. err_out:
  5748. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  5749. return -EIO;
  5750. }
  5751. static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
  5752. {
  5753. return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
  5754. }
  5755. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  5756. struct net_device *dev);
  5757. /* r8169_csum_workaround()
  5758. * The hw limites the value the transport offset. When the offset is out of the
  5759. * range, calculate the checksum by sw.
  5760. */
  5761. static void r8169_csum_workaround(struct rtl8169_private *tp,
  5762. struct sk_buff *skb)
  5763. {
  5764. if (skb_shinfo(skb)->gso_size) {
  5765. netdev_features_t features = tp->dev->features;
  5766. struct sk_buff *segs, *nskb;
  5767. features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
  5768. segs = skb_gso_segment(skb, features);
  5769. if (IS_ERR(segs) || !segs)
  5770. goto drop;
  5771. do {
  5772. nskb = segs;
  5773. segs = segs->next;
  5774. nskb->next = NULL;
  5775. rtl8169_start_xmit(nskb, tp->dev);
  5776. } while (segs);
  5777. dev_consume_skb_any(skb);
  5778. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5779. if (skb_checksum_help(skb) < 0)
  5780. goto drop;
  5781. rtl8169_start_xmit(skb, tp->dev);
  5782. } else {
  5783. struct net_device_stats *stats;
  5784. drop:
  5785. stats = &tp->dev->stats;
  5786. stats->tx_dropped++;
  5787. dev_kfree_skb_any(skb);
  5788. }
  5789. }
  5790. /* msdn_giant_send_check()
  5791. * According to the document of microsoft, the TCP Pseudo Header excludes the
  5792. * packet length for IPv6 TCP large packets.
  5793. */
  5794. static int msdn_giant_send_check(struct sk_buff *skb)
  5795. {
  5796. const struct ipv6hdr *ipv6h;
  5797. struct tcphdr *th;
  5798. int ret;
  5799. ret = skb_cow_head(skb, 0);
  5800. if (ret)
  5801. return ret;
  5802. ipv6h = ipv6_hdr(skb);
  5803. th = tcp_hdr(skb);
  5804. th->check = 0;
  5805. th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
  5806. return ret;
  5807. }
  5808. static inline __be16 get_protocol(struct sk_buff *skb)
  5809. {
  5810. __be16 protocol;
  5811. if (skb->protocol == htons(ETH_P_8021Q))
  5812. protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
  5813. else
  5814. protocol = skb->protocol;
  5815. return protocol;
  5816. }
  5817. static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
  5818. struct sk_buff *skb, u32 *opts)
  5819. {
  5820. u32 mss = skb_shinfo(skb)->gso_size;
  5821. if (mss) {
  5822. opts[0] |= TD_LSO;
  5823. opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
  5824. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5825. const struct iphdr *ip = ip_hdr(skb);
  5826. if (ip->protocol == IPPROTO_TCP)
  5827. opts[0] |= TD0_IP_CS | TD0_TCP_CS;
  5828. else if (ip->protocol == IPPROTO_UDP)
  5829. opts[0] |= TD0_IP_CS | TD0_UDP_CS;
  5830. else
  5831. WARN_ON_ONCE(1);
  5832. }
  5833. return true;
  5834. }
  5835. static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
  5836. struct sk_buff *skb, u32 *opts)
  5837. {
  5838. u32 transport_offset = (u32)skb_transport_offset(skb);
  5839. u32 mss = skb_shinfo(skb)->gso_size;
  5840. if (mss) {
  5841. if (transport_offset > GTTCPHO_MAX) {
  5842. netif_warn(tp, tx_err, tp->dev,
  5843. "Invalid transport offset 0x%x for TSO\n",
  5844. transport_offset);
  5845. return false;
  5846. }
  5847. switch (get_protocol(skb)) {
  5848. case htons(ETH_P_IP):
  5849. opts[0] |= TD1_GTSENV4;
  5850. break;
  5851. case htons(ETH_P_IPV6):
  5852. if (msdn_giant_send_check(skb))
  5853. return false;
  5854. opts[0] |= TD1_GTSENV6;
  5855. break;
  5856. default:
  5857. WARN_ON_ONCE(1);
  5858. break;
  5859. }
  5860. opts[0] |= transport_offset << GTTCPHO_SHIFT;
  5861. opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
  5862. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5863. u8 ip_protocol;
  5864. if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
  5865. return !(skb_checksum_help(skb) || eth_skb_pad(skb));
  5866. if (transport_offset > TCPHO_MAX) {
  5867. netif_warn(tp, tx_err, tp->dev,
  5868. "Invalid transport offset 0x%x\n",
  5869. transport_offset);
  5870. return false;
  5871. }
  5872. switch (get_protocol(skb)) {
  5873. case htons(ETH_P_IP):
  5874. opts[1] |= TD1_IPv4_CS;
  5875. ip_protocol = ip_hdr(skb)->protocol;
  5876. break;
  5877. case htons(ETH_P_IPV6):
  5878. opts[1] |= TD1_IPv6_CS;
  5879. ip_protocol = ipv6_hdr(skb)->nexthdr;
  5880. break;
  5881. default:
  5882. ip_protocol = IPPROTO_RAW;
  5883. break;
  5884. }
  5885. if (ip_protocol == IPPROTO_TCP)
  5886. opts[1] |= TD1_TCP_CS;
  5887. else if (ip_protocol == IPPROTO_UDP)
  5888. opts[1] |= TD1_UDP_CS;
  5889. else
  5890. WARN_ON_ONCE(1);
  5891. opts[1] |= transport_offset << TCPHO_SHIFT;
  5892. } else {
  5893. if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
  5894. return !eth_skb_pad(skb);
  5895. }
  5896. return true;
  5897. }
  5898. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  5899. struct net_device *dev)
  5900. {
  5901. struct rtl8169_private *tp = netdev_priv(dev);
  5902. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  5903. struct TxDesc *txd = tp->TxDescArray + entry;
  5904. void __iomem *ioaddr = tp->mmio_addr;
  5905. struct device *d = &tp->pci_dev->dev;
  5906. dma_addr_t mapping;
  5907. u32 status, len;
  5908. u32 opts[2];
  5909. int frags;
  5910. if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
  5911. netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
  5912. goto err_stop_0;
  5913. }
  5914. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  5915. goto err_stop_0;
  5916. opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
  5917. opts[0] = DescOwn;
  5918. if (!tp->tso_csum(tp, skb, opts)) {
  5919. r8169_csum_workaround(tp, skb);
  5920. return NETDEV_TX_OK;
  5921. }
  5922. len = skb_headlen(skb);
  5923. mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
  5924. if (unlikely(dma_mapping_error(d, mapping))) {
  5925. if (net_ratelimit())
  5926. netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
  5927. goto err_dma_0;
  5928. }
  5929. tp->tx_skb[entry].len = len;
  5930. txd->addr = cpu_to_le64(mapping);
  5931. frags = rtl8169_xmit_frags(tp, skb, opts);
  5932. if (frags < 0)
  5933. goto err_dma_1;
  5934. else if (frags)
  5935. opts[0] |= FirstFrag;
  5936. else {
  5937. opts[0] |= FirstFrag | LastFrag;
  5938. tp->tx_skb[entry].skb = skb;
  5939. }
  5940. txd->opts2 = cpu_to_le32(opts[1]);
  5941. skb_tx_timestamp(skb);
  5942. /* Force memory writes to complete before releasing descriptor */
  5943. dma_wmb();
  5944. /* Anti gcc 2.95.3 bugware (sic) */
  5945. status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  5946. txd->opts1 = cpu_to_le32(status);
  5947. /* Force all memory writes to complete before notifying device */
  5948. wmb();
  5949. tp->cur_tx += frags + 1;
  5950. RTL_W8(TxPoll, NPQ);
  5951. mmiowb();
  5952. if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
  5953. /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
  5954. * not miss a ring update when it notices a stopped queue.
  5955. */
  5956. smp_wmb();
  5957. netif_stop_queue(dev);
  5958. /* Sync with rtl_tx:
  5959. * - publish queue status and cur_tx ring index (write barrier)
  5960. * - refresh dirty_tx ring index (read barrier).
  5961. * May the current thread have a pessimistic view of the ring
  5962. * status and forget to wake up queue, a racing rtl_tx thread
  5963. * can't.
  5964. */
  5965. smp_mb();
  5966. if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
  5967. netif_wake_queue(dev);
  5968. }
  5969. return NETDEV_TX_OK;
  5970. err_dma_1:
  5971. rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
  5972. err_dma_0:
  5973. dev_kfree_skb_any(skb);
  5974. dev->stats.tx_dropped++;
  5975. return NETDEV_TX_OK;
  5976. err_stop_0:
  5977. netif_stop_queue(dev);
  5978. dev->stats.tx_dropped++;
  5979. return NETDEV_TX_BUSY;
  5980. }
  5981. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  5982. {
  5983. struct rtl8169_private *tp = netdev_priv(dev);
  5984. struct pci_dev *pdev = tp->pci_dev;
  5985. u16 pci_status, pci_cmd;
  5986. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  5987. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  5988. netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
  5989. pci_cmd, pci_status);
  5990. /*
  5991. * The recovery sequence below admits a very elaborated explanation:
  5992. * - it seems to work;
  5993. * - I did not see what else could be done;
  5994. * - it makes iop3xx happy.
  5995. *
  5996. * Feel free to adjust to your needs.
  5997. */
  5998. if (pdev->broken_parity_status)
  5999. pci_cmd &= ~PCI_COMMAND_PARITY;
  6000. else
  6001. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  6002. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  6003. pci_write_config_word(pdev, PCI_STATUS,
  6004. pci_status & (PCI_STATUS_DETECTED_PARITY |
  6005. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  6006. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  6007. /* The infamous DAC f*ckup only happens at boot time */
  6008. if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
  6009. void __iomem *ioaddr = tp->mmio_addr;
  6010. netif_info(tp, intr, dev, "disabling PCI DAC\n");
  6011. tp->cp_cmd &= ~PCIDAC;
  6012. RTL_W16(CPlusCmd, tp->cp_cmd);
  6013. dev->features &= ~NETIF_F_HIGHDMA;
  6014. }
  6015. rtl8169_hw_reset(tp);
  6016. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  6017. }
  6018. static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
  6019. {
  6020. unsigned int dirty_tx, tx_left;
  6021. dirty_tx = tp->dirty_tx;
  6022. smp_rmb();
  6023. tx_left = tp->cur_tx - dirty_tx;
  6024. while (tx_left > 0) {
  6025. unsigned int entry = dirty_tx % NUM_TX_DESC;
  6026. struct ring_info *tx_skb = tp->tx_skb + entry;
  6027. u32 status;
  6028. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  6029. if (status & DescOwn)
  6030. break;
  6031. /* This barrier is needed to keep us from reading
  6032. * any other fields out of the Tx descriptor until
  6033. * we know the status of DescOwn
  6034. */
  6035. dma_rmb();
  6036. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  6037. tp->TxDescArray + entry);
  6038. if (status & LastFrag) {
  6039. u64_stats_update_begin(&tp->tx_stats.syncp);
  6040. tp->tx_stats.packets++;
  6041. tp->tx_stats.bytes += tx_skb->skb->len;
  6042. u64_stats_update_end(&tp->tx_stats.syncp);
  6043. dev_kfree_skb_any(tx_skb->skb);
  6044. tx_skb->skb = NULL;
  6045. }
  6046. dirty_tx++;
  6047. tx_left--;
  6048. }
  6049. if (tp->dirty_tx != dirty_tx) {
  6050. tp->dirty_tx = dirty_tx;
  6051. /* Sync with rtl8169_start_xmit:
  6052. * - publish dirty_tx ring index (write barrier)
  6053. * - refresh cur_tx ring index and queue status (read barrier)
  6054. * May the current thread miss the stopped queue condition,
  6055. * a racing xmit thread can only have a right view of the
  6056. * ring status.
  6057. */
  6058. smp_mb();
  6059. if (netif_queue_stopped(dev) &&
  6060. TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
  6061. netif_wake_queue(dev);
  6062. }
  6063. /*
  6064. * 8168 hack: TxPoll requests are lost when the Tx packets are
  6065. * too close. Let's kick an extra TxPoll request when a burst
  6066. * of start_xmit activity is detected (if it is not detected,
  6067. * it is slow enough). -- FR
  6068. */
  6069. if (tp->cur_tx != dirty_tx) {
  6070. void __iomem *ioaddr = tp->mmio_addr;
  6071. RTL_W8(TxPoll, NPQ);
  6072. }
  6073. }
  6074. }
  6075. static inline int rtl8169_fragmented_frame(u32 status)
  6076. {
  6077. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  6078. }
  6079. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  6080. {
  6081. u32 status = opts1 & RxProtoMask;
  6082. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  6083. ((status == RxProtoUDP) && !(opts1 & UDPFail)))
  6084. skb->ip_summed = CHECKSUM_UNNECESSARY;
  6085. else
  6086. skb_checksum_none_assert(skb);
  6087. }
  6088. static struct sk_buff *rtl8169_try_rx_copy(void *data,
  6089. struct rtl8169_private *tp,
  6090. int pkt_size,
  6091. dma_addr_t addr)
  6092. {
  6093. struct sk_buff *skb;
  6094. struct device *d = &tp->pci_dev->dev;
  6095. data = rtl8169_align(data);
  6096. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  6097. prefetch(data);
  6098. skb = napi_alloc_skb(&tp->napi, pkt_size);
  6099. if (skb)
  6100. memcpy(skb->data, data, pkt_size);
  6101. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  6102. return skb;
  6103. }
  6104. static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
  6105. {
  6106. unsigned int cur_rx, rx_left;
  6107. unsigned int count;
  6108. cur_rx = tp->cur_rx;
  6109. for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
  6110. unsigned int entry = cur_rx % NUM_RX_DESC;
  6111. struct RxDesc *desc = tp->RxDescArray + entry;
  6112. u32 status;
  6113. status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
  6114. if (status & DescOwn)
  6115. break;
  6116. /* This barrier is needed to keep us from reading
  6117. * any other fields out of the Rx descriptor until
  6118. * we know the status of DescOwn
  6119. */
  6120. dma_rmb();
  6121. if (unlikely(status & RxRES)) {
  6122. netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
  6123. status);
  6124. dev->stats.rx_errors++;
  6125. if (status & (RxRWT | RxRUNT))
  6126. dev->stats.rx_length_errors++;
  6127. if (status & RxCRC)
  6128. dev->stats.rx_crc_errors++;
  6129. if (status & RxFOVF) {
  6130. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  6131. dev->stats.rx_fifo_errors++;
  6132. }
  6133. if ((status & (RxRUNT | RxCRC)) &&
  6134. !(status & (RxRWT | RxFOVF)) &&
  6135. (dev->features & NETIF_F_RXALL))
  6136. goto process_pkt;
  6137. } else {
  6138. struct sk_buff *skb;
  6139. dma_addr_t addr;
  6140. int pkt_size;
  6141. process_pkt:
  6142. addr = le64_to_cpu(desc->addr);
  6143. if (likely(!(dev->features & NETIF_F_RXFCS)))
  6144. pkt_size = (status & 0x00003fff) - 4;
  6145. else
  6146. pkt_size = status & 0x00003fff;
  6147. /*
  6148. * The driver does not support incoming fragmented
  6149. * frames. They are seen as a symptom of over-mtu
  6150. * sized frames.
  6151. */
  6152. if (unlikely(rtl8169_fragmented_frame(status))) {
  6153. dev->stats.rx_dropped++;
  6154. dev->stats.rx_length_errors++;
  6155. goto release_descriptor;
  6156. }
  6157. skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
  6158. tp, pkt_size, addr);
  6159. if (!skb) {
  6160. dev->stats.rx_dropped++;
  6161. goto release_descriptor;
  6162. }
  6163. rtl8169_rx_csum(skb, status);
  6164. skb_put(skb, pkt_size);
  6165. skb->protocol = eth_type_trans(skb, dev);
  6166. rtl8169_rx_vlan_tag(desc, skb);
  6167. if (skb->pkt_type == PACKET_MULTICAST)
  6168. dev->stats.multicast++;
  6169. napi_gro_receive(&tp->napi, skb);
  6170. u64_stats_update_begin(&tp->rx_stats.syncp);
  6171. tp->rx_stats.packets++;
  6172. tp->rx_stats.bytes += pkt_size;
  6173. u64_stats_update_end(&tp->rx_stats.syncp);
  6174. }
  6175. release_descriptor:
  6176. desc->opts2 = 0;
  6177. rtl8169_mark_to_asic(desc, rx_buf_sz);
  6178. }
  6179. count = cur_rx - tp->cur_rx;
  6180. tp->cur_rx = cur_rx;
  6181. return count;
  6182. }
  6183. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  6184. {
  6185. struct net_device *dev = dev_instance;
  6186. struct rtl8169_private *tp = netdev_priv(dev);
  6187. int handled = 0;
  6188. u16 status;
  6189. status = rtl_get_events(tp);
  6190. if (status && status != 0xffff) {
  6191. status &= RTL_EVENT_NAPI | tp->event_slow;
  6192. if (status) {
  6193. handled = 1;
  6194. rtl_irq_disable(tp);
  6195. napi_schedule(&tp->napi);
  6196. }
  6197. }
  6198. return IRQ_RETVAL(handled);
  6199. }
  6200. /*
  6201. * Workqueue context.
  6202. */
  6203. static void rtl_slow_event_work(struct rtl8169_private *tp)
  6204. {
  6205. struct net_device *dev = tp->dev;
  6206. u16 status;
  6207. status = rtl_get_events(tp) & tp->event_slow;
  6208. rtl_ack_events(tp, status);
  6209. if (unlikely(status & RxFIFOOver)) {
  6210. switch (tp->mac_version) {
  6211. /* Work around for rx fifo overflow */
  6212. case RTL_GIGA_MAC_VER_11:
  6213. netif_stop_queue(dev);
  6214. /* XXX - Hack alert. See rtl_task(). */
  6215. set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
  6216. default:
  6217. break;
  6218. }
  6219. }
  6220. if (unlikely(status & SYSErr))
  6221. rtl8169_pcierr_interrupt(dev);
  6222. if (status & LinkChg)
  6223. __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
  6224. rtl_irq_enable_all(tp);
  6225. }
  6226. static void rtl_task(struct work_struct *work)
  6227. {
  6228. static const struct {
  6229. int bitnr;
  6230. void (*action)(struct rtl8169_private *);
  6231. } rtl_work[] = {
  6232. /* XXX - keep rtl_slow_event_work() as first element. */
  6233. { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
  6234. { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
  6235. { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
  6236. };
  6237. struct rtl8169_private *tp =
  6238. container_of(work, struct rtl8169_private, wk.work);
  6239. struct net_device *dev = tp->dev;
  6240. int i;
  6241. rtl_lock_work(tp);
  6242. if (!netif_running(dev) ||
  6243. !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
  6244. goto out_unlock;
  6245. for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
  6246. bool pending;
  6247. pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
  6248. if (pending)
  6249. rtl_work[i].action(tp);
  6250. }
  6251. out_unlock:
  6252. rtl_unlock_work(tp);
  6253. }
  6254. static int rtl8169_poll(struct napi_struct *napi, int budget)
  6255. {
  6256. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  6257. struct net_device *dev = tp->dev;
  6258. u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
  6259. int work_done= 0;
  6260. u16 status;
  6261. status = rtl_get_events(tp);
  6262. rtl_ack_events(tp, status & ~tp->event_slow);
  6263. if (status & RTL_EVENT_NAPI_RX)
  6264. work_done = rtl_rx(dev, tp, (u32) budget);
  6265. if (status & RTL_EVENT_NAPI_TX)
  6266. rtl_tx(dev, tp);
  6267. if (status & tp->event_slow) {
  6268. enable_mask &= ~tp->event_slow;
  6269. rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
  6270. }
  6271. if (work_done < budget) {
  6272. napi_complete(napi);
  6273. rtl_irq_enable(tp, enable_mask);
  6274. mmiowb();
  6275. }
  6276. return work_done;
  6277. }
  6278. static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
  6279. {
  6280. struct rtl8169_private *tp = netdev_priv(dev);
  6281. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  6282. return;
  6283. dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
  6284. RTL_W32(RxMissed, 0);
  6285. }
  6286. static void rtl8169_down(struct net_device *dev)
  6287. {
  6288. struct rtl8169_private *tp = netdev_priv(dev);
  6289. void __iomem *ioaddr = tp->mmio_addr;
  6290. del_timer_sync(&tp->timer);
  6291. napi_disable(&tp->napi);
  6292. netif_stop_queue(dev);
  6293. rtl8169_hw_reset(tp);
  6294. /*
  6295. * At this point device interrupts can not be enabled in any function,
  6296. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
  6297. * and napi is disabled (rtl8169_poll).
  6298. */
  6299. rtl8169_rx_missed(dev, ioaddr);
  6300. /* Give a racing hard_start_xmit a few cycles to complete. */
  6301. synchronize_sched();
  6302. rtl8169_tx_clear(tp);
  6303. rtl8169_rx_clear(tp);
  6304. rtl_pll_power_down(tp);
  6305. }
  6306. static int rtl8169_close(struct net_device *dev)
  6307. {
  6308. struct rtl8169_private *tp = netdev_priv(dev);
  6309. struct pci_dev *pdev = tp->pci_dev;
  6310. pm_runtime_get_sync(&pdev->dev);
  6311. /* Update counters before going down */
  6312. rtl8169_update_counters(dev);
  6313. rtl_lock_work(tp);
  6314. clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  6315. rtl8169_down(dev);
  6316. rtl_unlock_work(tp);
  6317. cancel_work_sync(&tp->wk.work);
  6318. free_irq(pdev->irq, dev);
  6319. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  6320. tp->RxPhyAddr);
  6321. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  6322. tp->TxPhyAddr);
  6323. tp->TxDescArray = NULL;
  6324. tp->RxDescArray = NULL;
  6325. pm_runtime_put_sync(&pdev->dev);
  6326. return 0;
  6327. }
  6328. #ifdef CONFIG_NET_POLL_CONTROLLER
  6329. static void rtl8169_netpoll(struct net_device *dev)
  6330. {
  6331. struct rtl8169_private *tp = netdev_priv(dev);
  6332. rtl8169_interrupt(tp->pci_dev->irq, dev);
  6333. }
  6334. #endif
  6335. static int rtl_open(struct net_device *dev)
  6336. {
  6337. struct rtl8169_private *tp = netdev_priv(dev);
  6338. void __iomem *ioaddr = tp->mmio_addr;
  6339. struct pci_dev *pdev = tp->pci_dev;
  6340. int retval = -ENOMEM;
  6341. pm_runtime_get_sync(&pdev->dev);
  6342. /*
  6343. * Rx and Tx descriptors needs 256 bytes alignment.
  6344. * dma_alloc_coherent provides more.
  6345. */
  6346. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  6347. &tp->TxPhyAddr, GFP_KERNEL);
  6348. if (!tp->TxDescArray)
  6349. goto err_pm_runtime_put;
  6350. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  6351. &tp->RxPhyAddr, GFP_KERNEL);
  6352. if (!tp->RxDescArray)
  6353. goto err_free_tx_0;
  6354. retval = rtl8169_init_ring(dev);
  6355. if (retval < 0)
  6356. goto err_free_rx_1;
  6357. INIT_WORK(&tp->wk.work, rtl_task);
  6358. smp_mb();
  6359. rtl_request_firmware(tp);
  6360. retval = request_irq(pdev->irq, rtl8169_interrupt,
  6361. (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
  6362. dev->name, dev);
  6363. if (retval < 0)
  6364. goto err_release_fw_2;
  6365. rtl_lock_work(tp);
  6366. set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  6367. napi_enable(&tp->napi);
  6368. rtl8169_init_phy(dev, tp);
  6369. __rtl8169_set_features(dev, dev->features);
  6370. rtl_pll_power_up(tp);
  6371. rtl_hw_start(dev);
  6372. if (!rtl8169_init_counter_offsets(dev))
  6373. netif_warn(tp, hw, dev, "counter reset/update failed\n");
  6374. netif_start_queue(dev);
  6375. rtl_unlock_work(tp);
  6376. tp->saved_wolopts = 0;
  6377. pm_runtime_put_noidle(&pdev->dev);
  6378. rtl8169_check_link_status(dev, tp, ioaddr);
  6379. out:
  6380. return retval;
  6381. err_release_fw_2:
  6382. rtl_release_firmware(tp);
  6383. rtl8169_rx_clear(tp);
  6384. err_free_rx_1:
  6385. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  6386. tp->RxPhyAddr);
  6387. tp->RxDescArray = NULL;
  6388. err_free_tx_0:
  6389. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  6390. tp->TxPhyAddr);
  6391. tp->TxDescArray = NULL;
  6392. err_pm_runtime_put:
  6393. pm_runtime_put_noidle(&pdev->dev);
  6394. goto out;
  6395. }
  6396. static struct rtnl_link_stats64 *
  6397. rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  6398. {
  6399. struct rtl8169_private *tp = netdev_priv(dev);
  6400. void __iomem *ioaddr = tp->mmio_addr;
  6401. struct pci_dev *pdev = tp->pci_dev;
  6402. struct rtl8169_counters *counters = tp->counters;
  6403. unsigned int start;
  6404. pm_runtime_get_noresume(&pdev->dev);
  6405. if (netif_running(dev) && pm_runtime_active(&pdev->dev))
  6406. rtl8169_rx_missed(dev, ioaddr);
  6407. do {
  6408. start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
  6409. stats->rx_packets = tp->rx_stats.packets;
  6410. stats->rx_bytes = tp->rx_stats.bytes;
  6411. } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
  6412. do {
  6413. start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
  6414. stats->tx_packets = tp->tx_stats.packets;
  6415. stats->tx_bytes = tp->tx_stats.bytes;
  6416. } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
  6417. stats->rx_dropped = dev->stats.rx_dropped;
  6418. stats->tx_dropped = dev->stats.tx_dropped;
  6419. stats->rx_length_errors = dev->stats.rx_length_errors;
  6420. stats->rx_errors = dev->stats.rx_errors;
  6421. stats->rx_crc_errors = dev->stats.rx_crc_errors;
  6422. stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
  6423. stats->rx_missed_errors = dev->stats.rx_missed_errors;
  6424. stats->multicast = dev->stats.multicast;
  6425. /*
  6426. * Fetch additonal counter values missing in stats collected by driver
  6427. * from tally counters.
  6428. */
  6429. if (pm_runtime_active(&pdev->dev))
  6430. rtl8169_update_counters(dev);
  6431. /*
  6432. * Subtract values fetched during initalization.
  6433. * See rtl8169_init_counter_offsets for a description why we do that.
  6434. */
  6435. stats->tx_errors = le64_to_cpu(counters->tx_errors) -
  6436. le64_to_cpu(tp->tc_offset.tx_errors);
  6437. stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
  6438. le32_to_cpu(tp->tc_offset.tx_multi_collision);
  6439. stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
  6440. le16_to_cpu(tp->tc_offset.tx_aborted);
  6441. pm_runtime_put_noidle(&pdev->dev);
  6442. return stats;
  6443. }
  6444. static void rtl8169_net_suspend(struct net_device *dev)
  6445. {
  6446. struct rtl8169_private *tp = netdev_priv(dev);
  6447. if (!netif_running(dev))
  6448. return;
  6449. netif_device_detach(dev);
  6450. netif_stop_queue(dev);
  6451. rtl_lock_work(tp);
  6452. napi_disable(&tp->napi);
  6453. clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  6454. rtl_unlock_work(tp);
  6455. rtl_pll_power_down(tp);
  6456. }
  6457. #ifdef CONFIG_PM
  6458. static int rtl8169_suspend(struct device *device)
  6459. {
  6460. struct pci_dev *pdev = to_pci_dev(device);
  6461. struct net_device *dev = pci_get_drvdata(pdev);
  6462. rtl8169_net_suspend(dev);
  6463. return 0;
  6464. }
  6465. static void __rtl8169_resume(struct net_device *dev)
  6466. {
  6467. struct rtl8169_private *tp = netdev_priv(dev);
  6468. netif_device_attach(dev);
  6469. rtl_pll_power_up(tp);
  6470. rtl_lock_work(tp);
  6471. napi_enable(&tp->napi);
  6472. set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  6473. rtl_unlock_work(tp);
  6474. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  6475. }
  6476. static int rtl8169_resume(struct device *device)
  6477. {
  6478. struct pci_dev *pdev = to_pci_dev(device);
  6479. struct net_device *dev = pci_get_drvdata(pdev);
  6480. struct rtl8169_private *tp = netdev_priv(dev);
  6481. rtl8169_init_phy(dev, tp);
  6482. if (netif_running(dev))
  6483. __rtl8169_resume(dev);
  6484. return 0;
  6485. }
  6486. static int rtl8169_runtime_suspend(struct device *device)
  6487. {
  6488. struct pci_dev *pdev = to_pci_dev(device);
  6489. struct net_device *dev = pci_get_drvdata(pdev);
  6490. struct rtl8169_private *tp = netdev_priv(dev);
  6491. if (!tp->TxDescArray)
  6492. return 0;
  6493. rtl_lock_work(tp);
  6494. tp->saved_wolopts = __rtl8169_get_wol(tp);
  6495. __rtl8169_set_wol(tp, WAKE_ANY);
  6496. rtl_unlock_work(tp);
  6497. rtl8169_net_suspend(dev);
  6498. /* Update counters before going runtime suspend */
  6499. rtl8169_rx_missed(dev, tp->mmio_addr);
  6500. rtl8169_update_counters(dev);
  6501. return 0;
  6502. }
  6503. static int rtl8169_runtime_resume(struct device *device)
  6504. {
  6505. struct pci_dev *pdev = to_pci_dev(device);
  6506. struct net_device *dev = pci_get_drvdata(pdev);
  6507. struct rtl8169_private *tp = netdev_priv(dev);
  6508. rtl_rar_set(tp, dev->dev_addr);
  6509. if (!tp->TxDescArray)
  6510. return 0;
  6511. rtl_lock_work(tp);
  6512. __rtl8169_set_wol(tp, tp->saved_wolopts);
  6513. tp->saved_wolopts = 0;
  6514. rtl_unlock_work(tp);
  6515. rtl8169_init_phy(dev, tp);
  6516. __rtl8169_resume(dev);
  6517. return 0;
  6518. }
  6519. static int rtl8169_runtime_idle(struct device *device)
  6520. {
  6521. struct pci_dev *pdev = to_pci_dev(device);
  6522. struct net_device *dev = pci_get_drvdata(pdev);
  6523. struct rtl8169_private *tp = netdev_priv(dev);
  6524. return tp->TxDescArray ? -EBUSY : 0;
  6525. }
  6526. static const struct dev_pm_ops rtl8169_pm_ops = {
  6527. .suspend = rtl8169_suspend,
  6528. .resume = rtl8169_resume,
  6529. .freeze = rtl8169_suspend,
  6530. .thaw = rtl8169_resume,
  6531. .poweroff = rtl8169_suspend,
  6532. .restore = rtl8169_resume,
  6533. .runtime_suspend = rtl8169_runtime_suspend,
  6534. .runtime_resume = rtl8169_runtime_resume,
  6535. .runtime_idle = rtl8169_runtime_idle,
  6536. };
  6537. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  6538. #else /* !CONFIG_PM */
  6539. #define RTL8169_PM_OPS NULL
  6540. #endif /* !CONFIG_PM */
  6541. static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
  6542. {
  6543. void __iomem *ioaddr = tp->mmio_addr;
  6544. /* WoL fails with 8168b when the receiver is disabled. */
  6545. switch (tp->mac_version) {
  6546. case RTL_GIGA_MAC_VER_11:
  6547. case RTL_GIGA_MAC_VER_12:
  6548. case RTL_GIGA_MAC_VER_17:
  6549. pci_clear_master(tp->pci_dev);
  6550. RTL_W8(ChipCmd, CmdRxEnb);
  6551. /* PCI commit */
  6552. RTL_R8(ChipCmd);
  6553. break;
  6554. default:
  6555. break;
  6556. }
  6557. }
  6558. static void rtl_shutdown(struct pci_dev *pdev)
  6559. {
  6560. struct net_device *dev = pci_get_drvdata(pdev);
  6561. struct rtl8169_private *tp = netdev_priv(dev);
  6562. struct device *d = &pdev->dev;
  6563. pm_runtime_get_sync(d);
  6564. rtl8169_net_suspend(dev);
  6565. /* Restore original MAC address */
  6566. rtl_rar_set(tp, dev->perm_addr);
  6567. rtl8169_hw_reset(tp);
  6568. if (system_state == SYSTEM_POWER_OFF) {
  6569. if (__rtl8169_get_wol(tp) & WAKE_ANY) {
  6570. rtl_wol_suspend_quirk(tp);
  6571. rtl_wol_shutdown_quirk(tp);
  6572. }
  6573. pci_wake_from_d3(pdev, true);
  6574. pci_set_power_state(pdev, PCI_D3hot);
  6575. }
  6576. pm_runtime_put_noidle(d);
  6577. }
  6578. static void rtl_remove_one(struct pci_dev *pdev)
  6579. {
  6580. struct net_device *dev = pci_get_drvdata(pdev);
  6581. struct rtl8169_private *tp = netdev_priv(dev);
  6582. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  6583. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  6584. tp->mac_version == RTL_GIGA_MAC_VER_31 ||
  6585. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  6586. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  6587. tp->mac_version == RTL_GIGA_MAC_VER_51) &&
  6588. r8168_check_dash(tp)) {
  6589. rtl8168_driver_stop(tp);
  6590. }
  6591. netif_napi_del(&tp->napi);
  6592. unregister_netdev(dev);
  6593. dma_free_coherent(&tp->pci_dev->dev, sizeof(*tp->counters),
  6594. tp->counters, tp->counters_phys_addr);
  6595. rtl_release_firmware(tp);
  6596. if (pci_dev_run_wake(pdev))
  6597. pm_runtime_get_noresume(&pdev->dev);
  6598. /* restore original MAC address */
  6599. rtl_rar_set(tp, dev->perm_addr);
  6600. rtl_disable_msi(pdev, tp);
  6601. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  6602. }
  6603. static const struct net_device_ops rtl_netdev_ops = {
  6604. .ndo_open = rtl_open,
  6605. .ndo_stop = rtl8169_close,
  6606. .ndo_get_stats64 = rtl8169_get_stats64,
  6607. .ndo_start_xmit = rtl8169_start_xmit,
  6608. .ndo_tx_timeout = rtl8169_tx_timeout,
  6609. .ndo_validate_addr = eth_validate_addr,
  6610. .ndo_change_mtu = rtl8169_change_mtu,
  6611. .ndo_fix_features = rtl8169_fix_features,
  6612. .ndo_set_features = rtl8169_set_features,
  6613. .ndo_set_mac_address = rtl_set_mac_address,
  6614. .ndo_do_ioctl = rtl8169_ioctl,
  6615. .ndo_set_rx_mode = rtl_set_rx_mode,
  6616. #ifdef CONFIG_NET_POLL_CONTROLLER
  6617. .ndo_poll_controller = rtl8169_netpoll,
  6618. #endif
  6619. };
  6620. static const struct rtl_cfg_info {
  6621. void (*hw_start)(struct net_device *);
  6622. unsigned int region;
  6623. unsigned int align;
  6624. u16 event_slow;
  6625. unsigned features;
  6626. u8 default_ver;
  6627. } rtl_cfg_infos [] = {
  6628. [RTL_CFG_0] = {
  6629. .hw_start = rtl_hw_start_8169,
  6630. .region = 1,
  6631. .align = 0,
  6632. .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
  6633. .features = RTL_FEATURE_GMII,
  6634. .default_ver = RTL_GIGA_MAC_VER_01,
  6635. },
  6636. [RTL_CFG_1] = {
  6637. .hw_start = rtl_hw_start_8168,
  6638. .region = 2,
  6639. .align = 8,
  6640. .event_slow = SYSErr | LinkChg | RxOverflow,
  6641. .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
  6642. .default_ver = RTL_GIGA_MAC_VER_11,
  6643. },
  6644. [RTL_CFG_2] = {
  6645. .hw_start = rtl_hw_start_8101,
  6646. .region = 2,
  6647. .align = 8,
  6648. .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
  6649. PCSTimeout,
  6650. .features = RTL_FEATURE_MSI,
  6651. .default_ver = RTL_GIGA_MAC_VER_13,
  6652. }
  6653. };
  6654. /* Cfg9346_Unlock assumed. */
  6655. static unsigned rtl_try_msi(struct rtl8169_private *tp,
  6656. const struct rtl_cfg_info *cfg)
  6657. {
  6658. void __iomem *ioaddr = tp->mmio_addr;
  6659. unsigned msi = 0;
  6660. u8 cfg2;
  6661. cfg2 = RTL_R8(Config2) & ~MSIEnable;
  6662. if (cfg->features & RTL_FEATURE_MSI) {
  6663. if (pci_enable_msi(tp->pci_dev)) {
  6664. netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
  6665. } else {
  6666. cfg2 |= MSIEnable;
  6667. msi = RTL_FEATURE_MSI;
  6668. }
  6669. }
  6670. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  6671. RTL_W8(Config2, cfg2);
  6672. return msi;
  6673. }
  6674. DECLARE_RTL_COND(rtl_link_list_ready_cond)
  6675. {
  6676. void __iomem *ioaddr = tp->mmio_addr;
  6677. return RTL_R8(MCU) & LINK_LIST_RDY;
  6678. }
  6679. DECLARE_RTL_COND(rtl_rxtx_empty_cond)
  6680. {
  6681. void __iomem *ioaddr = tp->mmio_addr;
  6682. return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
  6683. }
  6684. static void rtl_hw_init_8168g(struct rtl8169_private *tp)
  6685. {
  6686. void __iomem *ioaddr = tp->mmio_addr;
  6687. u32 data;
  6688. tp->ocp_base = OCP_STD_PHY_BASE;
  6689. RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
  6690. if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
  6691. return;
  6692. if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
  6693. return;
  6694. RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
  6695. msleep(1);
  6696. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  6697. data = r8168_mac_ocp_read(tp, 0xe8de);
  6698. data &= ~(1 << 14);
  6699. r8168_mac_ocp_write(tp, 0xe8de, data);
  6700. if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
  6701. return;
  6702. data = r8168_mac_ocp_read(tp, 0xe8de);
  6703. data |= (1 << 15);
  6704. r8168_mac_ocp_write(tp, 0xe8de, data);
  6705. if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
  6706. return;
  6707. }
  6708. static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
  6709. {
  6710. rtl8168ep_stop_cmac(tp);
  6711. rtl_hw_init_8168g(tp);
  6712. }
  6713. static void rtl_hw_initialize(struct rtl8169_private *tp)
  6714. {
  6715. switch (tp->mac_version) {
  6716. case RTL_GIGA_MAC_VER_40:
  6717. case RTL_GIGA_MAC_VER_41:
  6718. case RTL_GIGA_MAC_VER_42:
  6719. case RTL_GIGA_MAC_VER_43:
  6720. case RTL_GIGA_MAC_VER_44:
  6721. case RTL_GIGA_MAC_VER_45:
  6722. case RTL_GIGA_MAC_VER_46:
  6723. case RTL_GIGA_MAC_VER_47:
  6724. case RTL_GIGA_MAC_VER_48:
  6725. rtl_hw_init_8168g(tp);
  6726. break;
  6727. case RTL_GIGA_MAC_VER_49:
  6728. case RTL_GIGA_MAC_VER_50:
  6729. case RTL_GIGA_MAC_VER_51:
  6730. rtl_hw_init_8168ep(tp);
  6731. break;
  6732. default:
  6733. break;
  6734. }
  6735. }
  6736. static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  6737. {
  6738. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  6739. const unsigned int region = cfg->region;
  6740. struct rtl8169_private *tp;
  6741. struct mii_if_info *mii;
  6742. struct net_device *dev;
  6743. void __iomem *ioaddr;
  6744. int chipset, i;
  6745. int rc;
  6746. if (netif_msg_drv(&debug)) {
  6747. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  6748. MODULENAME, RTL8169_VERSION);
  6749. }
  6750. dev = alloc_etherdev(sizeof (*tp));
  6751. if (!dev) {
  6752. rc = -ENOMEM;
  6753. goto out;
  6754. }
  6755. SET_NETDEV_DEV(dev, &pdev->dev);
  6756. dev->netdev_ops = &rtl_netdev_ops;
  6757. tp = netdev_priv(dev);
  6758. tp->dev = dev;
  6759. tp->pci_dev = pdev;
  6760. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  6761. mii = &tp->mii;
  6762. mii->dev = dev;
  6763. mii->mdio_read = rtl_mdio_read;
  6764. mii->mdio_write = rtl_mdio_write;
  6765. mii->phy_id_mask = 0x1f;
  6766. mii->reg_num_mask = 0x1f;
  6767. mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
  6768. /* disable ASPM completely as that cause random device stop working
  6769. * problems as well as full system hangs for some PCIe devices users */
  6770. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  6771. PCIE_LINK_STATE_CLKPM);
  6772. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  6773. rc = pci_enable_device(pdev);
  6774. if (rc < 0) {
  6775. netif_err(tp, probe, dev, "enable failure\n");
  6776. goto err_out_free_dev_1;
  6777. }
  6778. if (pci_set_mwi(pdev) < 0)
  6779. netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
  6780. /* make sure PCI base addr 1 is MMIO */
  6781. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  6782. netif_err(tp, probe, dev,
  6783. "region #%d not an MMIO resource, aborting\n",
  6784. region);
  6785. rc = -ENODEV;
  6786. goto err_out_mwi_2;
  6787. }
  6788. /* check for weird/broken PCI region reporting */
  6789. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  6790. netif_err(tp, probe, dev,
  6791. "Invalid PCI region size(s), aborting\n");
  6792. rc = -ENODEV;
  6793. goto err_out_mwi_2;
  6794. }
  6795. rc = pci_request_regions(pdev, MODULENAME);
  6796. if (rc < 0) {
  6797. netif_err(tp, probe, dev, "could not request regions\n");
  6798. goto err_out_mwi_2;
  6799. }
  6800. /* ioremap MMIO region */
  6801. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  6802. if (!ioaddr) {
  6803. netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
  6804. rc = -EIO;
  6805. goto err_out_free_res_3;
  6806. }
  6807. tp->mmio_addr = ioaddr;
  6808. if (!pci_is_pcie(pdev))
  6809. netif_info(tp, probe, dev, "not PCI Express\n");
  6810. /* Identify chip attached to board */
  6811. rtl8169_get_mac_version(tp, dev, cfg->default_ver);
  6812. tp->cp_cmd = 0;
  6813. if ((sizeof(dma_addr_t) > 4) &&
  6814. (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
  6815. tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
  6816. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
  6817. !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
  6818. /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
  6819. if (!pci_is_pcie(pdev))
  6820. tp->cp_cmd |= PCIDAC;
  6821. dev->features |= NETIF_F_HIGHDMA;
  6822. } else {
  6823. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  6824. if (rc < 0) {
  6825. netif_err(tp, probe, dev, "DMA configuration failed\n");
  6826. goto err_out_unmap_4;
  6827. }
  6828. }
  6829. rtl_init_rxcfg(tp);
  6830. rtl_irq_disable(tp);
  6831. rtl_hw_initialize(tp);
  6832. rtl_hw_reset(tp);
  6833. rtl_ack_events(tp, 0xffff);
  6834. pci_set_master(pdev);
  6835. rtl_init_mdio_ops(tp);
  6836. rtl_init_pll_power_ops(tp);
  6837. rtl_init_jumbo_ops(tp);
  6838. rtl_init_csi_ops(tp);
  6839. rtl8169_print_mac_version(tp);
  6840. chipset = tp->mac_version;
  6841. tp->txd_version = rtl_chip_infos[chipset].txd_version;
  6842. RTL_W8(Cfg9346, Cfg9346_Unlock);
  6843. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  6844. RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
  6845. switch (tp->mac_version) {
  6846. case RTL_GIGA_MAC_VER_34:
  6847. case RTL_GIGA_MAC_VER_35:
  6848. case RTL_GIGA_MAC_VER_36:
  6849. case RTL_GIGA_MAC_VER_37:
  6850. case RTL_GIGA_MAC_VER_38:
  6851. case RTL_GIGA_MAC_VER_40:
  6852. case RTL_GIGA_MAC_VER_41:
  6853. case RTL_GIGA_MAC_VER_42:
  6854. case RTL_GIGA_MAC_VER_43:
  6855. case RTL_GIGA_MAC_VER_44:
  6856. case RTL_GIGA_MAC_VER_45:
  6857. case RTL_GIGA_MAC_VER_46:
  6858. case RTL_GIGA_MAC_VER_47:
  6859. case RTL_GIGA_MAC_VER_48:
  6860. case RTL_GIGA_MAC_VER_49:
  6861. case RTL_GIGA_MAC_VER_50:
  6862. case RTL_GIGA_MAC_VER_51:
  6863. if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
  6864. tp->features |= RTL_FEATURE_WOL;
  6865. if ((RTL_R8(Config3) & LinkUp) != 0)
  6866. tp->features |= RTL_FEATURE_WOL;
  6867. break;
  6868. default:
  6869. if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
  6870. tp->features |= RTL_FEATURE_WOL;
  6871. break;
  6872. }
  6873. if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
  6874. tp->features |= RTL_FEATURE_WOL;
  6875. tp->features |= rtl_try_msi(tp, cfg);
  6876. RTL_W8(Cfg9346, Cfg9346_Lock);
  6877. if (rtl_tbi_enabled(tp)) {
  6878. tp->set_speed = rtl8169_set_speed_tbi;
  6879. tp->get_settings = rtl8169_gset_tbi;
  6880. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  6881. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  6882. tp->link_ok = rtl8169_tbi_link_ok;
  6883. tp->do_ioctl = rtl_tbi_ioctl;
  6884. } else {
  6885. tp->set_speed = rtl8169_set_speed_xmii;
  6886. tp->get_settings = rtl8169_gset_xmii;
  6887. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  6888. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  6889. tp->link_ok = rtl8169_xmii_link_ok;
  6890. tp->do_ioctl = rtl_xmii_ioctl;
  6891. }
  6892. mutex_init(&tp->wk.mutex);
  6893. u64_stats_init(&tp->rx_stats.syncp);
  6894. u64_stats_init(&tp->tx_stats.syncp);
  6895. /* Get MAC address */
  6896. if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  6897. tp->mac_version == RTL_GIGA_MAC_VER_36 ||
  6898. tp->mac_version == RTL_GIGA_MAC_VER_37 ||
  6899. tp->mac_version == RTL_GIGA_MAC_VER_38 ||
  6900. tp->mac_version == RTL_GIGA_MAC_VER_40 ||
  6901. tp->mac_version == RTL_GIGA_MAC_VER_41 ||
  6902. tp->mac_version == RTL_GIGA_MAC_VER_42 ||
  6903. tp->mac_version == RTL_GIGA_MAC_VER_43 ||
  6904. tp->mac_version == RTL_GIGA_MAC_VER_44 ||
  6905. tp->mac_version == RTL_GIGA_MAC_VER_45 ||
  6906. tp->mac_version == RTL_GIGA_MAC_VER_46 ||
  6907. tp->mac_version == RTL_GIGA_MAC_VER_47 ||
  6908. tp->mac_version == RTL_GIGA_MAC_VER_48 ||
  6909. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  6910. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  6911. tp->mac_version == RTL_GIGA_MAC_VER_51) {
  6912. u16 mac_addr[3];
  6913. *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
  6914. *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
  6915. if (is_valid_ether_addr((u8 *)mac_addr))
  6916. rtl_rar_set(tp, (u8 *)mac_addr);
  6917. }
  6918. for (i = 0; i < ETH_ALEN; i++)
  6919. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  6920. dev->ethtool_ops = &rtl8169_ethtool_ops;
  6921. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  6922. netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
  6923. /* don't enable SG, IP_CSUM and TSO by default - it might not work
  6924. * properly for all devices */
  6925. dev->features |= NETIF_F_RXCSUM |
  6926. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  6927. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  6928. NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
  6929. NETIF_F_HW_VLAN_CTAG_RX;
  6930. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  6931. NETIF_F_HIGHDMA;
  6932. tp->cp_cmd |= RxChkSum | RxVlan;
  6933. /*
  6934. * Pretend we are using VLANs; This bypasses a nasty bug where
  6935. * Interrupts stop flowing on high load on 8110SCd controllers.
  6936. */
  6937. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  6938. /* Disallow toggling */
  6939. dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  6940. if (tp->txd_version == RTL_TD_0)
  6941. tp->tso_csum = rtl8169_tso_csum_v1;
  6942. else if (tp->txd_version == RTL_TD_1) {
  6943. tp->tso_csum = rtl8169_tso_csum_v2;
  6944. dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  6945. } else
  6946. WARN_ON_ONCE(1);
  6947. dev->hw_features |= NETIF_F_RXALL;
  6948. dev->hw_features |= NETIF_F_RXFCS;
  6949. tp->hw_start = cfg->hw_start;
  6950. tp->event_slow = cfg->event_slow;
  6951. tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
  6952. ~(RxBOVF | RxFOVF) : ~0;
  6953. init_timer(&tp->timer);
  6954. tp->timer.data = (unsigned long) dev;
  6955. tp->timer.function = rtl8169_phy_timer;
  6956. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  6957. tp->counters = dma_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
  6958. &tp->counters_phys_addr, GFP_KERNEL);
  6959. if (!tp->counters) {
  6960. rc = -ENOMEM;
  6961. goto err_out_msi_5;
  6962. }
  6963. pci_set_drvdata(pdev, dev);
  6964. rc = register_netdev(dev);
  6965. if (rc < 0)
  6966. goto err_out_cnt_6;
  6967. netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
  6968. rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
  6969. (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
  6970. if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
  6971. netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
  6972. "tx checksumming: %s]\n",
  6973. rtl_chip_infos[chipset].jumbo_max,
  6974. rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
  6975. }
  6976. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  6977. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  6978. tp->mac_version == RTL_GIGA_MAC_VER_31 ||
  6979. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  6980. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  6981. tp->mac_version == RTL_GIGA_MAC_VER_51) &&
  6982. r8168_check_dash(tp)) {
  6983. rtl8168_driver_start(tp);
  6984. }
  6985. device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
  6986. if (pci_dev_run_wake(pdev))
  6987. pm_runtime_put_noidle(&pdev->dev);
  6988. netif_carrier_off(dev);
  6989. out:
  6990. return rc;
  6991. err_out_cnt_6:
  6992. dma_free_coherent(&pdev->dev, sizeof(*tp->counters), tp->counters,
  6993. tp->counters_phys_addr);
  6994. err_out_msi_5:
  6995. netif_napi_del(&tp->napi);
  6996. rtl_disable_msi(pdev, tp);
  6997. err_out_unmap_4:
  6998. iounmap(ioaddr);
  6999. err_out_free_res_3:
  7000. pci_release_regions(pdev);
  7001. err_out_mwi_2:
  7002. pci_clear_mwi(pdev);
  7003. pci_disable_device(pdev);
  7004. err_out_free_dev_1:
  7005. free_netdev(dev);
  7006. goto out;
  7007. }
  7008. static struct pci_driver rtl8169_pci_driver = {
  7009. .name = MODULENAME,
  7010. .id_table = rtl8169_pci_tbl,
  7011. .probe = rtl_init_one,
  7012. .remove = rtl_remove_one,
  7013. .shutdown = rtl_shutdown,
  7014. .driver.pm = RTL8169_PM_OPS,
  7015. };
  7016. module_pci_driver(rtl8169_pci_driver);