r8169.c 208 KB

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